treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / power / supply / max8903_charger.c
blob0bd39b0cc257659e486b2389dc4ba606cf0e282e
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * max8903_charger.c - Maxim 8903 USB/Adapter Charger Driver
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 */
9 #include <linux/gpio.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
14 #include <linux/of_gpio.h>
15 #include <linux/slab.h>
16 #include <linux/power_supply.h>
17 #include <linux/platform_device.h>
18 #include <linux/power/max8903_charger.h>
20 struct max8903_data {
21 struct max8903_pdata *pdata;
22 struct device *dev;
23 struct power_supply *psy;
24 struct power_supply_desc psy_desc;
25 bool fault;
26 bool usb_in;
27 bool ta_in;
30 static enum power_supply_property max8903_charger_props[] = {
31 POWER_SUPPLY_PROP_STATUS, /* Charger status output */
32 POWER_SUPPLY_PROP_ONLINE, /* External power source */
33 POWER_SUPPLY_PROP_HEALTH, /* Fault or OK */
36 static int max8903_get_property(struct power_supply *psy,
37 enum power_supply_property psp,
38 union power_supply_propval *val)
40 struct max8903_data *data = power_supply_get_drvdata(psy);
42 switch (psp) {
43 case POWER_SUPPLY_PROP_STATUS:
44 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
45 if (gpio_is_valid(data->pdata->chg)) {
46 if (gpio_get_value(data->pdata->chg) == 0)
47 val->intval = POWER_SUPPLY_STATUS_CHARGING;
48 else if (data->usb_in || data->ta_in)
49 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
50 else
51 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
53 break;
54 case POWER_SUPPLY_PROP_ONLINE:
55 val->intval = 0;
56 if (data->usb_in || data->ta_in)
57 val->intval = 1;
58 break;
59 case POWER_SUPPLY_PROP_HEALTH:
60 val->intval = POWER_SUPPLY_HEALTH_GOOD;
61 if (data->fault)
62 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
63 break;
64 default:
65 return -EINVAL;
68 return 0;
71 static irqreturn_t max8903_dcin(int irq, void *_data)
73 struct max8903_data *data = _data;
74 struct max8903_pdata *pdata = data->pdata;
75 bool ta_in;
76 enum power_supply_type old_type;
78 ta_in = gpio_get_value(pdata->dok) ? false : true;
80 if (ta_in == data->ta_in)
81 return IRQ_HANDLED;
83 data->ta_in = ta_in;
85 /* Set Current-Limit-Mode 1:DC 0:USB */
86 if (gpio_is_valid(pdata->dcm))
87 gpio_set_value(pdata->dcm, ta_in ? 1 : 0);
89 /* Charger Enable / Disable (cen is negated) */
90 if (gpio_is_valid(pdata->cen))
91 gpio_set_value(pdata->cen, ta_in ? 0 :
92 (data->usb_in ? 0 : 1));
94 dev_dbg(data->dev, "TA(DC-IN) Charger %s.\n", ta_in ?
95 "Connected" : "Disconnected");
97 old_type = data->psy_desc.type;
99 if (data->ta_in)
100 data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
101 else if (data->usb_in)
102 data->psy_desc.type = POWER_SUPPLY_TYPE_USB;
103 else
104 data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY;
106 if (old_type != data->psy_desc.type)
107 power_supply_changed(data->psy);
109 return IRQ_HANDLED;
112 static irqreturn_t max8903_usbin(int irq, void *_data)
114 struct max8903_data *data = _data;
115 struct max8903_pdata *pdata = data->pdata;
116 bool usb_in;
117 enum power_supply_type old_type;
119 usb_in = gpio_get_value(pdata->uok) ? false : true;
121 if (usb_in == data->usb_in)
122 return IRQ_HANDLED;
124 data->usb_in = usb_in;
126 /* Do not touch Current-Limit-Mode */
128 /* Charger Enable / Disable (cen is negated) */
129 if (gpio_is_valid(pdata->cen))
130 gpio_set_value(pdata->cen, usb_in ? 0 :
131 (data->ta_in ? 0 : 1));
133 dev_dbg(data->dev, "USB Charger %s.\n", usb_in ?
134 "Connected" : "Disconnected");
136 old_type = data->psy_desc.type;
138 if (data->ta_in)
139 data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
140 else if (data->usb_in)
141 data->psy_desc.type = POWER_SUPPLY_TYPE_USB;
142 else
143 data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY;
145 if (old_type != data->psy_desc.type)
146 power_supply_changed(data->psy);
148 return IRQ_HANDLED;
151 static irqreturn_t max8903_fault(int irq, void *_data)
153 struct max8903_data *data = _data;
154 struct max8903_pdata *pdata = data->pdata;
155 bool fault;
157 fault = gpio_get_value(pdata->flt) ? false : true;
159 if (fault == data->fault)
160 return IRQ_HANDLED;
162 data->fault = fault;
164 if (fault)
165 dev_err(data->dev, "Charger suffers a fault and stops.\n");
166 else
167 dev_err(data->dev, "Charger recovered from a fault.\n");
169 return IRQ_HANDLED;
172 static struct max8903_pdata *max8903_parse_dt_data(struct device *dev)
174 struct device_node *np = dev->of_node;
175 struct max8903_pdata *pdata = NULL;
177 if (!np)
178 return NULL;
180 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
181 if (!pdata)
182 return NULL;
184 pdata->dc_valid = false;
185 pdata->usb_valid = false;
187 pdata->cen = of_get_named_gpio(np, "cen-gpios", 0);
188 if (!gpio_is_valid(pdata->cen))
189 pdata->cen = -EINVAL;
191 pdata->chg = of_get_named_gpio(np, "chg-gpios", 0);
192 if (!gpio_is_valid(pdata->chg))
193 pdata->chg = -EINVAL;
195 pdata->flt = of_get_named_gpio(np, "flt-gpios", 0);
196 if (!gpio_is_valid(pdata->flt))
197 pdata->flt = -EINVAL;
199 pdata->usus = of_get_named_gpio(np, "usus-gpios", 0);
200 if (!gpio_is_valid(pdata->usus))
201 pdata->usus = -EINVAL;
203 pdata->dcm = of_get_named_gpio(np, "dcm-gpios", 0);
204 if (!gpio_is_valid(pdata->dcm))
205 pdata->dcm = -EINVAL;
207 pdata->dok = of_get_named_gpio(np, "dok-gpios", 0);
208 if (!gpio_is_valid(pdata->dok))
209 pdata->dok = -EINVAL;
210 else
211 pdata->dc_valid = true;
213 pdata->uok = of_get_named_gpio(np, "uok-gpios", 0);
214 if (!gpio_is_valid(pdata->uok))
215 pdata->uok = -EINVAL;
216 else
217 pdata->usb_valid = true;
219 return pdata;
222 static int max8903_setup_gpios(struct platform_device *pdev)
224 struct max8903_data *data = platform_get_drvdata(pdev);
225 struct device *dev = &pdev->dev;
226 struct max8903_pdata *pdata = pdev->dev.platform_data;
227 int ret = 0;
228 int gpio;
229 int ta_in = 0;
230 int usb_in = 0;
232 if (pdata->dc_valid) {
233 if (gpio_is_valid(pdata->dok)) {
234 ret = devm_gpio_request(dev, pdata->dok,
235 data->psy_desc.name);
236 if (ret) {
237 dev_err(dev,
238 "Failed GPIO request for dok: %d err %d\n",
239 pdata->dok, ret);
240 return ret;
243 gpio = pdata->dok; /* PULL_UPed Interrupt */
244 ta_in = gpio_get_value(gpio) ? 0 : 1;
245 } else {
246 dev_err(dev, "When DC is wired, DOK should be wired as well.\n");
247 return -EINVAL;
251 if (gpio_is_valid(pdata->dcm)) {
252 ret = devm_gpio_request(dev, pdata->dcm, data->psy_desc.name);
253 if (ret) {
254 dev_err(dev,
255 "Failed GPIO request for dcm: %d err %d\n",
256 pdata->dcm, ret);
257 return ret;
260 gpio = pdata->dcm; /* Output */
261 gpio_set_value(gpio, ta_in);
264 if (pdata->usb_valid) {
265 if (gpio_is_valid(pdata->uok)) {
266 ret = devm_gpio_request(dev, pdata->uok,
267 data->psy_desc.name);
268 if (ret) {
269 dev_err(dev,
270 "Failed GPIO request for uok: %d err %d\n",
271 pdata->uok, ret);
272 return ret;
275 gpio = pdata->uok;
276 usb_in = gpio_get_value(gpio) ? 0 : 1;
277 } else {
278 dev_err(dev, "When USB is wired, UOK should be wired."
279 "as well.\n");
280 return -EINVAL;
284 if (gpio_is_valid(pdata->cen)) {
285 ret = devm_gpio_request(dev, pdata->cen, data->psy_desc.name);
286 if (ret) {
287 dev_err(dev,
288 "Failed GPIO request for cen: %d err %d\n",
289 pdata->cen, ret);
290 return ret;
293 gpio_set_value(pdata->cen, (ta_in || usb_in) ? 0 : 1);
296 if (gpio_is_valid(pdata->chg)) {
297 ret = devm_gpio_request(dev, pdata->chg, data->psy_desc.name);
298 if (ret) {
299 dev_err(dev,
300 "Failed GPIO request for chg: %d err %d\n",
301 pdata->chg, ret);
302 return ret;
306 if (gpio_is_valid(pdata->flt)) {
307 ret = devm_gpio_request(dev, pdata->flt, data->psy_desc.name);
308 if (ret) {
309 dev_err(dev,
310 "Failed GPIO request for flt: %d err %d\n",
311 pdata->flt, ret);
312 return ret;
316 if (gpio_is_valid(pdata->usus)) {
317 ret = devm_gpio_request(dev, pdata->usus, data->psy_desc.name);
318 if (ret) {
319 dev_err(dev,
320 "Failed GPIO request for usus: %d err %d\n",
321 pdata->usus, ret);
322 return ret;
326 data->fault = false;
327 data->ta_in = ta_in;
328 data->usb_in = usb_in;
330 return 0;
333 static int max8903_probe(struct platform_device *pdev)
335 struct max8903_data *data;
336 struct device *dev = &pdev->dev;
337 struct max8903_pdata *pdata = pdev->dev.platform_data;
338 struct power_supply_config psy_cfg = {};
339 int ret = 0;
341 data = devm_kzalloc(dev, sizeof(struct max8903_data), GFP_KERNEL);
342 if (!data)
343 return -ENOMEM;
345 if (IS_ENABLED(CONFIG_OF) && !pdata && dev->of_node)
346 pdata = max8903_parse_dt_data(dev);
348 if (!pdata) {
349 dev_err(dev, "No platform data.\n");
350 return -EINVAL;
353 pdev->dev.platform_data = pdata;
354 data->pdata = pdata;
355 data->dev = dev;
356 platform_set_drvdata(pdev, data);
358 if (pdata->dc_valid == false && pdata->usb_valid == false) {
359 dev_err(dev, "No valid power sources.\n");
360 return -EINVAL;
363 ret = max8903_setup_gpios(pdev);
364 if (ret)
365 return ret;
367 data->psy_desc.name = "max8903_charger";
368 data->psy_desc.type = (data->ta_in) ? POWER_SUPPLY_TYPE_MAINS :
369 ((data->usb_in) ? POWER_SUPPLY_TYPE_USB :
370 POWER_SUPPLY_TYPE_BATTERY);
371 data->psy_desc.get_property = max8903_get_property;
372 data->psy_desc.properties = max8903_charger_props;
373 data->psy_desc.num_properties = ARRAY_SIZE(max8903_charger_props);
375 psy_cfg.of_node = dev->of_node;
376 psy_cfg.drv_data = data;
378 data->psy = devm_power_supply_register(dev, &data->psy_desc, &psy_cfg);
379 if (IS_ERR(data->psy)) {
380 dev_err(dev, "failed: power supply register.\n");
381 return PTR_ERR(data->psy);
384 if (pdata->dc_valid) {
385 ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->dok),
386 NULL, max8903_dcin,
387 IRQF_TRIGGER_FALLING |
388 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
389 "MAX8903 DC IN", data);
390 if (ret) {
391 dev_err(dev, "Cannot request irq %d for DC (%d)\n",
392 gpio_to_irq(pdata->dok), ret);
393 return ret;
397 if (pdata->usb_valid) {
398 ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->uok),
399 NULL, max8903_usbin,
400 IRQF_TRIGGER_FALLING |
401 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
402 "MAX8903 USB IN", data);
403 if (ret) {
404 dev_err(dev, "Cannot request irq %d for USB (%d)\n",
405 gpio_to_irq(pdata->uok), ret);
406 return ret;
410 if (gpio_is_valid(pdata->flt)) {
411 ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->flt),
412 NULL, max8903_fault,
413 IRQF_TRIGGER_FALLING |
414 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
415 "MAX8903 Fault", data);
416 if (ret) {
417 dev_err(dev, "Cannot request irq %d for Fault (%d)\n",
418 gpio_to_irq(pdata->flt), ret);
419 return ret;
423 return 0;
426 static const struct of_device_id max8903_match_ids[] = {
427 { .compatible = "maxim,max8903", },
428 { /* sentinel */ }
430 MODULE_DEVICE_TABLE(of, max8903_match_ids);
432 static struct platform_driver max8903_driver = {
433 .probe = max8903_probe,
434 .driver = {
435 .name = "max8903-charger",
436 .of_match_table = max8903_match_ids
440 module_platform_driver(max8903_driver);
442 MODULE_LICENSE("GPL");
443 MODULE_DESCRIPTION("MAX8903 Charger Driver");
444 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
445 MODULE_ALIAS("platform:max8903-charger");