1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * max8903_charger.c - Maxim 8903 USB/Adapter Charger Driver
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
9 #include <linux/gpio.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.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>
21 struct max8903_pdata
*pdata
;
23 struct power_supply
*psy
;
24 struct power_supply_desc psy_desc
;
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
);
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
;
51 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
54 case POWER_SUPPLY_PROP_ONLINE
:
56 if (data
->usb_in
|| data
->ta_in
)
59 case POWER_SUPPLY_PROP_HEALTH
:
60 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
62 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
71 static irqreturn_t
max8903_dcin(int irq
, void *_data
)
73 struct max8903_data
*data
= _data
;
74 struct max8903_pdata
*pdata
= data
->pdata
;
76 enum power_supply_type old_type
;
78 ta_in
= gpio_get_value(pdata
->dok
) ? false : true;
80 if (ta_in
== data
->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
;
100 data
->psy_desc
.type
= POWER_SUPPLY_TYPE_MAINS
;
101 else if (data
->usb_in
)
102 data
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB
;
104 data
->psy_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
106 if (old_type
!= data
->psy_desc
.type
)
107 power_supply_changed(data
->psy
);
112 static irqreturn_t
max8903_usbin(int irq
, void *_data
)
114 struct max8903_data
*data
= _data
;
115 struct max8903_pdata
*pdata
= data
->pdata
;
117 enum power_supply_type old_type
;
119 usb_in
= gpio_get_value(pdata
->uok
) ? false : true;
121 if (usb_in
== data
->usb_in
)
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
;
139 data
->psy_desc
.type
= POWER_SUPPLY_TYPE_MAINS
;
140 else if (data
->usb_in
)
141 data
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB
;
143 data
->psy_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
145 if (old_type
!= data
->psy_desc
.type
)
146 power_supply_changed(data
->psy
);
151 static irqreturn_t
max8903_fault(int irq
, void *_data
)
153 struct max8903_data
*data
= _data
;
154 struct max8903_pdata
*pdata
= data
->pdata
;
157 fault
= gpio_get_value(pdata
->flt
) ? false : true;
159 if (fault
== data
->fault
)
165 dev_err(data
->dev
, "Charger suffers a fault and stops.\n");
167 dev_err(data
->dev
, "Charger recovered from a fault.\n");
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
;
180 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
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
;
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
;
217 pdata
->usb_valid
= true;
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
;
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
);
238 "Failed GPIO request for dok: %d err %d\n",
243 gpio
= pdata
->dok
; /* PULL_UPed Interrupt */
244 ta_in
= gpio_get_value(gpio
) ? 0 : 1;
246 dev_err(dev
, "When DC is wired, DOK should be wired as well.\n");
251 if (gpio_is_valid(pdata
->dcm
)) {
252 ret
= devm_gpio_request(dev
, pdata
->dcm
, data
->psy_desc
.name
);
255 "Failed GPIO request for dcm: %d err %d\n",
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
);
270 "Failed GPIO request for uok: %d err %d\n",
276 usb_in
= gpio_get_value(gpio
) ? 0 : 1;
278 dev_err(dev
, "When USB is wired, UOK should be wired."
284 if (gpio_is_valid(pdata
->cen
)) {
285 ret
= devm_gpio_request(dev
, pdata
->cen
, data
->psy_desc
.name
);
288 "Failed GPIO request for cen: %d err %d\n",
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
);
300 "Failed GPIO request for chg: %d err %d\n",
306 if (gpio_is_valid(pdata
->flt
)) {
307 ret
= devm_gpio_request(dev
, pdata
->flt
, data
->psy_desc
.name
);
310 "Failed GPIO request for flt: %d err %d\n",
316 if (gpio_is_valid(pdata
->usus
)) {
317 ret
= devm_gpio_request(dev
, pdata
->usus
, data
->psy_desc
.name
);
320 "Failed GPIO request for usus: %d err %d\n",
328 data
->usb_in
= usb_in
;
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
= {};
341 data
= devm_kzalloc(dev
, sizeof(struct max8903_data
), GFP_KERNEL
);
345 if (IS_ENABLED(CONFIG_OF
) && !pdata
&& dev
->of_node
)
346 pdata
= max8903_parse_dt_data(dev
);
349 dev_err(dev
, "No platform data.\n");
353 pdev
->dev
.platform_data
= pdata
;
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");
363 ret
= max8903_setup_gpios(pdev
);
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
),
387 IRQF_TRIGGER_FALLING
|
388 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
389 "MAX8903 DC IN", data
);
391 dev_err(dev
, "Cannot request irq %d for DC (%d)\n",
392 gpio_to_irq(pdata
->dok
), ret
);
397 if (pdata
->usb_valid
) {
398 ret
= devm_request_threaded_irq(dev
, gpio_to_irq(pdata
->uok
),
400 IRQF_TRIGGER_FALLING
|
401 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
402 "MAX8903 USB IN", data
);
404 dev_err(dev
, "Cannot request irq %d for USB (%d)\n",
405 gpio_to_irq(pdata
->uok
), ret
);
410 if (gpio_is_valid(pdata
->flt
)) {
411 ret
= devm_request_threaded_irq(dev
, gpio_to_irq(pdata
->flt
),
413 IRQF_TRIGGER_FALLING
|
414 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
415 "MAX8903 Fault", data
);
417 dev_err(dev
, "Cannot request irq %d for Fault (%d)\n",
418 gpio_to_irq(pdata
->flt
), ret
);
426 static const struct of_device_id max8903_match_ids
[] = {
427 { .compatible
= "maxim,max8903", },
430 MODULE_DEVICE_TABLE(of
, max8903_match_ids
);
432 static struct platform_driver max8903_driver
= {
433 .probe
= max8903_probe
,
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");