2 * max8903_charger.c - Maxim 8903 USB/Adapter Charger Driver
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/gpio.h>
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/power_supply.h>
28 #include <linux/platform_device.h>
29 #include <linux/power/max8903_charger.h>
32 struct max8903_pdata pdata
;
34 struct power_supply psy
;
40 static enum power_supply_property max8903_charger_props
[] = {
41 POWER_SUPPLY_PROP_STATUS
, /* Charger status output */
42 POWER_SUPPLY_PROP_ONLINE
, /* External power source */
43 POWER_SUPPLY_PROP_HEALTH
, /* Fault or OK */
46 static int max8903_get_property(struct power_supply
*psy
,
47 enum power_supply_property psp
,
48 union power_supply_propval
*val
)
50 struct max8903_data
*data
= container_of(psy
,
51 struct max8903_data
, psy
);
54 case POWER_SUPPLY_PROP_STATUS
:
55 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
56 if (data
->pdata
.chg
) {
57 if (gpio_get_value(data
->pdata
.chg
) == 0)
58 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
59 else if (data
->usb_in
|| data
->ta_in
)
60 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
62 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
65 case POWER_SUPPLY_PROP_ONLINE
:
67 if (data
->usb_in
|| data
->ta_in
)
70 case POWER_SUPPLY_PROP_HEALTH
:
71 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
73 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
81 static irqreturn_t
max8903_dcin(int irq
, void *_data
)
83 struct max8903_data
*data
= _data
;
84 struct max8903_pdata
*pdata
= &data
->pdata
;
86 enum power_supply_type old_type
;
88 ta_in
= gpio_get_value(pdata
->dok
) ? false : true;
90 if (ta_in
== data
->ta_in
)
95 /* Set Current-Limit-Mode 1:DC 0:USB */
97 gpio_set_value(pdata
->dcm
, ta_in
? 1 : 0);
99 /* Charger Enable / Disable (cen is negated) */
101 gpio_set_value(pdata
->cen
, ta_in
? 0 :
102 (data
->usb_in
? 0 : 1));
104 dev_dbg(data
->dev
, "TA(DC-IN) Charger %s.\n", ta_in
?
105 "Connected" : "Disconnected");
107 old_type
= data
->psy
.type
;
110 data
->psy
.type
= POWER_SUPPLY_TYPE_MAINS
;
111 else if (data
->usb_in
)
112 data
->psy
.type
= POWER_SUPPLY_TYPE_USB
;
114 data
->psy
.type
= POWER_SUPPLY_TYPE_BATTERY
;
116 if (old_type
!= data
->psy
.type
)
117 power_supply_changed(&data
->psy
);
122 static irqreturn_t
max8903_usbin(int irq
, void *_data
)
124 struct max8903_data
*data
= _data
;
125 struct max8903_pdata
*pdata
= &data
->pdata
;
127 enum power_supply_type old_type
;
129 usb_in
= gpio_get_value(pdata
->uok
) ? false : true;
131 if (usb_in
== data
->usb_in
)
134 data
->usb_in
= usb_in
;
136 /* Do not touch Current-Limit-Mode */
138 /* Charger Enable / Disable (cen is negated) */
140 gpio_set_value(pdata
->cen
, usb_in
? 0 :
141 (data
->ta_in
? 0 : 1));
143 dev_dbg(data
->dev
, "USB Charger %s.\n", usb_in
?
144 "Connected" : "Disconnected");
146 old_type
= data
->psy
.type
;
149 data
->psy
.type
= POWER_SUPPLY_TYPE_MAINS
;
150 else if (data
->usb_in
)
151 data
->psy
.type
= POWER_SUPPLY_TYPE_USB
;
153 data
->psy
.type
= POWER_SUPPLY_TYPE_BATTERY
;
155 if (old_type
!= data
->psy
.type
)
156 power_supply_changed(&data
->psy
);
161 static irqreturn_t
max8903_fault(int irq
, void *_data
)
163 struct max8903_data
*data
= _data
;
164 struct max8903_pdata
*pdata
= &data
->pdata
;
167 fault
= gpio_get_value(pdata
->flt
) ? false : true;
169 if (fault
== data
->fault
)
175 dev_err(data
->dev
, "Charger suffers a fault and stops.\n");
177 dev_err(data
->dev
, "Charger recovered from a fault.\n");
182 static __devinit
int max8903_probe(struct platform_device
*pdev
)
184 struct max8903_data
*data
;
185 struct device
*dev
= &pdev
->dev
;
186 struct max8903_pdata
*pdata
= pdev
->dev
.platform_data
;
192 data
= kzalloc(sizeof(struct max8903_data
), GFP_KERNEL
);
194 dev_err(dev
, "Cannot allocate memory.\n");
197 memcpy(&data
->pdata
, pdata
, sizeof(struct max8903_pdata
));
199 platform_set_drvdata(pdev
, data
);
201 if (pdata
->dc_valid
== false && pdata
->usb_valid
== false) {
202 dev_err(dev
, "No valid power sources.\n");
207 if (pdata
->dc_valid
) {
208 if (pdata
->dok
&& gpio_is_valid(pdata
->dok
) &&
209 pdata
->dcm
&& gpio_is_valid(pdata
->dcm
)) {
210 gpio
= pdata
->dok
; /* PULL_UPed Interrupt */
211 ta_in
= gpio_get_value(gpio
) ? 0 : 1;
213 gpio
= pdata
->dcm
; /* Output */
214 gpio_set_value(gpio
, ta_in
);
216 dev_err(dev
, "When DC is wired, DOK and DCM should"
217 " be wired as well.\n");
223 if (gpio_is_valid(pdata
->dcm
))
224 gpio_set_value(pdata
->dcm
, 0);
226 dev_err(dev
, "Invalid pin: dcm.\n");
233 if (pdata
->usb_valid
) {
234 if (pdata
->uok
&& gpio_is_valid(pdata
->uok
)) {
236 usb_in
= gpio_get_value(gpio
) ? 0 : 1;
238 dev_err(dev
, "When USB is wired, UOK should be wired."
246 if (gpio_is_valid(pdata
->cen
)) {
247 gpio_set_value(pdata
->cen
, (ta_in
|| usb_in
) ? 0 : 1);
249 dev_err(dev
, "Invalid pin: cen.\n");
256 if (!gpio_is_valid(pdata
->chg
)) {
257 dev_err(dev
, "Invalid pin: chg.\n");
264 if (!gpio_is_valid(pdata
->flt
)) {
265 dev_err(dev
, "Invalid pin: flt.\n");
272 if (!gpio_is_valid(pdata
->usus
)) {
273 dev_err(dev
, "Invalid pin: usus.\n");
281 data
->usb_in
= usb_in
;
283 data
->psy
.name
= "max8903_charger";
284 data
->psy
.type
= (ta_in
) ? POWER_SUPPLY_TYPE_MAINS
:
285 ((usb_in
) ? POWER_SUPPLY_TYPE_USB
:
286 POWER_SUPPLY_TYPE_BATTERY
);
287 data
->psy
.get_property
= max8903_get_property
;
288 data
->psy
.properties
= max8903_charger_props
;
289 data
->psy
.num_properties
= ARRAY_SIZE(max8903_charger_props
);
291 ret
= power_supply_register(dev
, &data
->psy
);
293 dev_err(dev
, "failed: power supply register.\n");
297 if (pdata
->dc_valid
) {
298 ret
= request_threaded_irq(gpio_to_irq(pdata
->dok
),
300 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
301 "MAX8903 DC IN", data
);
303 dev_err(dev
, "Cannot request irq %d for DC (%d)\n",
304 gpio_to_irq(pdata
->dok
), ret
);
309 if (pdata
->usb_valid
) {
310 ret
= request_threaded_irq(gpio_to_irq(pdata
->uok
),
312 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
313 "MAX8903 USB IN", data
);
315 dev_err(dev
, "Cannot request irq %d for USB (%d)\n",
316 gpio_to_irq(pdata
->uok
), ret
);
322 ret
= request_threaded_irq(gpio_to_irq(pdata
->flt
),
324 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
325 "MAX8903 Fault", data
);
327 dev_err(dev
, "Cannot request irq %d for Fault (%d)\n",
328 gpio_to_irq(pdata
->flt
), ret
);
336 if (pdata
->usb_valid
)
337 free_irq(gpio_to_irq(pdata
->uok
), data
);
340 free_irq(gpio_to_irq(pdata
->dok
), data
);
342 power_supply_unregister(&data
->psy
);
348 static __devexit
int max8903_remove(struct platform_device
*pdev
)
350 struct max8903_data
*data
= platform_get_drvdata(pdev
);
353 struct max8903_pdata
*pdata
= &data
->pdata
;
356 free_irq(gpio_to_irq(pdata
->flt
), data
);
357 if (pdata
->usb_valid
)
358 free_irq(gpio_to_irq(pdata
->uok
), data
);
360 free_irq(gpio_to_irq(pdata
->dok
), data
);
361 power_supply_unregister(&data
->psy
);
368 static struct platform_driver max8903_driver
= {
369 .probe
= max8903_probe
,
370 .remove
= __devexit_p(max8903_remove
),
372 .name
= "max8903-charger",
373 .owner
= THIS_MODULE
,
377 static int __init
max8903_init(void)
379 return platform_driver_register(&max8903_driver
);
381 module_init(max8903_init
);
383 static void __exit
max8903_exit(void)
385 platform_driver_unregister(&max8903_driver
);
387 module_exit(max8903_exit
);
389 MODULE_LICENSE("GPL");
390 MODULE_DESCRIPTION("MAX8903 Charger Driver");
391 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
392 MODULE_ALIAS("max8903-charger");