blob: 6d39d52040d4ab982f01066c47239ff295323427 [file] [log] [blame]
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +09001/*
2 * max8903_charger.c - Maxim 8903 USB/Adapter Charger Driver
3 *
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22
23#include <linux/gpio.h>
24#include <linux/interrupt.h>
Paul Gortmaker7e6d62d2011-07-03 15:28:29 -040025#include <linux/module.h>
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +090026#include <linux/slab.h>
27#include <linux/power_supply.h>
28#include <linux/platform_device.h>
29#include <linux/power/max8903_charger.h>
30
31struct max8903_data {
MyungJoo Ham464f29a2011-06-30 10:09:40 +090032 struct max8903_pdata pdata;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +090033 struct device *dev;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010034 struct power_supply *psy;
35 struct power_supply_desc psy_desc;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +090036 bool fault;
37 bool usb_in;
38 bool ta_in;
39};
40
41static enum power_supply_property max8903_charger_props[] = {
42 POWER_SUPPLY_PROP_STATUS, /* Charger status output */
43 POWER_SUPPLY_PROP_ONLINE, /* External power source */
44 POWER_SUPPLY_PROP_HEALTH, /* Fault or OK */
45};
46
47static int max8903_get_property(struct power_supply *psy,
48 enum power_supply_property psp,
49 union power_supply_propval *val)
50{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010051 struct max8903_data *data = power_supply_get_drvdata(psy);
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +090052
53 switch (psp) {
54 case POWER_SUPPLY_PROP_STATUS:
55 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
MyungJoo Ham464f29a2011-06-30 10:09:40 +090056 if (data->pdata.chg) {
57 if (gpio_get_value(data->pdata.chg) == 0)
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +090058 val->intval = POWER_SUPPLY_STATUS_CHARGING;
59 else if (data->usb_in || data->ta_in)
60 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
61 else
62 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
63 }
64 break;
65 case POWER_SUPPLY_PROP_ONLINE:
66 val->intval = 0;
67 if (data->usb_in || data->ta_in)
68 val->intval = 1;
69 break;
70 case POWER_SUPPLY_PROP_HEALTH:
71 val->intval = POWER_SUPPLY_HEALTH_GOOD;
72 if (data->fault)
73 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
74 break;
75 default:
76 return -EINVAL;
77 }
78 return 0;
79}
80
81static irqreturn_t max8903_dcin(int irq, void *_data)
82{
83 struct max8903_data *data = _data;
MyungJoo Ham464f29a2011-06-30 10:09:40 +090084 struct max8903_pdata *pdata = &data->pdata;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +090085 bool ta_in;
86 enum power_supply_type old_type;
87
88 ta_in = gpio_get_value(pdata->dok) ? false : true;
89
90 if (ta_in == data->ta_in)
91 return IRQ_HANDLED;
92
93 data->ta_in = ta_in;
94
95 /* Set Current-Limit-Mode 1:DC 0:USB */
96 if (pdata->dcm)
97 gpio_set_value(pdata->dcm, ta_in ? 1 : 0);
98
99 /* Charger Enable / Disable (cen is negated) */
100 if (pdata->cen)
101 gpio_set_value(pdata->cen, ta_in ? 0 :
102 (data->usb_in ? 0 : 1));
103
104 dev_dbg(data->dev, "TA(DC-IN) Charger %s.\n", ta_in ?
105 "Connected" : "Disconnected");
106
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100107 old_type = data->psy_desc.type;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900108
109 if (data->ta_in)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100110 data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900111 else if (data->usb_in)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100112 data->psy_desc.type = POWER_SUPPLY_TYPE_USB;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900113 else
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100114 data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900115
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100116 if (old_type != data->psy_desc.type)
117 power_supply_changed(data->psy);
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900118
119 return IRQ_HANDLED;
120}
121
122static irqreturn_t max8903_usbin(int irq, void *_data)
123{
124 struct max8903_data *data = _data;
MyungJoo Ham464f29a2011-06-30 10:09:40 +0900125 struct max8903_pdata *pdata = &data->pdata;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900126 bool usb_in;
127 enum power_supply_type old_type;
128
129 usb_in = gpio_get_value(pdata->uok) ? false : true;
130
131 if (usb_in == data->usb_in)
132 return IRQ_HANDLED;
133
134 data->usb_in = usb_in;
135
136 /* Do not touch Current-Limit-Mode */
137
138 /* Charger Enable / Disable (cen is negated) */
139 if (pdata->cen)
140 gpio_set_value(pdata->cen, usb_in ? 0 :
141 (data->ta_in ? 0 : 1));
142
143 dev_dbg(data->dev, "USB Charger %s.\n", usb_in ?
144 "Connected" : "Disconnected");
145
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100146 old_type = data->psy_desc.type;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900147
148 if (data->ta_in)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100149 data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900150 else if (data->usb_in)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100151 data->psy_desc.type = POWER_SUPPLY_TYPE_USB;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900152 else
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100153 data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900154
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100155 if (old_type != data->psy_desc.type)
156 power_supply_changed(data->psy);
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900157
158 return IRQ_HANDLED;
159}
160
161static irqreturn_t max8903_fault(int irq, void *_data)
162{
163 struct max8903_data *data = _data;
MyungJoo Ham464f29a2011-06-30 10:09:40 +0900164 struct max8903_pdata *pdata = &data->pdata;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900165 bool fault;
166
167 fault = gpio_get_value(pdata->flt) ? false : true;
168
169 if (fault == data->fault)
170 return IRQ_HANDLED;
171
172 data->fault = fault;
173
174 if (fault)
175 dev_err(data->dev, "Charger suffers a fault and stops.\n");
176 else
177 dev_err(data->dev, "Charger recovered from a fault.\n");
178
179 return IRQ_HANDLED;
180}
181
Bill Pembertonc8afa642012-11-19 13:22:23 -0500182static int max8903_probe(struct platform_device *pdev)
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900183{
184 struct max8903_data *data;
185 struct device *dev = &pdev->dev;
186 struct max8903_pdata *pdata = pdev->dev.platform_data;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100187 struct power_supply_config psy_cfg = {};
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900188 int ret = 0;
189 int gpio;
190 int ta_in = 0;
191 int usb_in = 0;
192
Jingoo Hanf3f66b32013-03-11 15:34:35 +0900193 data = devm_kzalloc(dev, sizeof(struct max8903_data), GFP_KERNEL);
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900194 if (data == NULL) {
195 dev_err(dev, "Cannot allocate memory.\n");
196 return -ENOMEM;
197 }
MyungJoo Ham464f29a2011-06-30 10:09:40 +0900198 memcpy(&data->pdata, pdata, sizeof(struct max8903_pdata));
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900199 data->dev = dev;
200 platform_set_drvdata(pdev, data);
201
202 if (pdata->dc_valid == false && pdata->usb_valid == false) {
203 dev_err(dev, "No valid power sources.\n");
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530204 return -EINVAL;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900205 }
206
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;
212
213 gpio = pdata->dcm; /* Output */
214 gpio_set_value(gpio, ta_in);
215 } else {
216 dev_err(dev, "When DC is wired, DOK and DCM should"
217 " be wired as well.\n");
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530218 return -EINVAL;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900219 }
220 } else {
221 if (pdata->dcm) {
222 if (gpio_is_valid(pdata->dcm))
223 gpio_set_value(pdata->dcm, 0);
224 else {
225 dev_err(dev, "Invalid pin: dcm.\n");
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530226 return -EINVAL;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900227 }
228 }
229 }
230
231 if (pdata->usb_valid) {
232 if (pdata->uok && gpio_is_valid(pdata->uok)) {
233 gpio = pdata->uok;
234 usb_in = gpio_get_value(gpio) ? 0 : 1;
235 } else {
236 dev_err(dev, "When USB is wired, UOK should be wired."
237 "as well.\n");
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530238 return -EINVAL;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900239 }
240 }
241
242 if (pdata->cen) {
243 if (gpio_is_valid(pdata->cen)) {
244 gpio_set_value(pdata->cen, (ta_in || usb_in) ? 0 : 1);
245 } else {
246 dev_err(dev, "Invalid pin: cen.\n");
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530247 return -EINVAL;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900248 }
249 }
250
251 if (pdata->chg) {
252 if (!gpio_is_valid(pdata->chg)) {
253 dev_err(dev, "Invalid pin: chg.\n");
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530254 return -EINVAL;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900255 }
256 }
257
258 if (pdata->flt) {
259 if (!gpio_is_valid(pdata->flt)) {
260 dev_err(dev, "Invalid pin: flt.\n");
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530261 return -EINVAL;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900262 }
263 }
264
265 if (pdata->usus) {
266 if (!gpio_is_valid(pdata->usus)) {
267 dev_err(dev, "Invalid pin: usus.\n");
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530268 return -EINVAL;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900269 }
270 }
271
272 data->fault = false;
273 data->ta_in = ta_in;
274 data->usb_in = usb_in;
275
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100276 data->psy_desc.name = "max8903_charger";
277 data->psy_desc.type = (ta_in) ? POWER_SUPPLY_TYPE_MAINS :
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900278 ((usb_in) ? POWER_SUPPLY_TYPE_USB :
279 POWER_SUPPLY_TYPE_BATTERY);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100280 data->psy_desc.get_property = max8903_get_property;
281 data->psy_desc.properties = max8903_charger_props;
282 data->psy_desc.num_properties = ARRAY_SIZE(max8903_charger_props);
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900283
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100284 psy_cfg.drv_data = data;
285
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530286 data->psy = devm_power_supply_register(dev, &data->psy_desc, &psy_cfg);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100287 if (IS_ERR(data->psy)) {
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900288 dev_err(dev, "failed: power supply register.\n");
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530289 return PTR_ERR(data->psy);
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900290 }
291
292 if (pdata->dc_valid) {
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530293 ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->dok),
294 NULL, max8903_dcin,
295 IRQF_TRIGGER_FALLING |
296 IRQF_TRIGGER_RISING,
297 "MAX8903 DC IN", data);
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900298 if (ret) {
299 dev_err(dev, "Cannot request irq %d for DC (%d)\n",
300 gpio_to_irq(pdata->dok), ret);
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530301 return ret;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900302 }
303 }
304
305 if (pdata->usb_valid) {
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530306 ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->uok),
307 NULL, max8903_usbin,
308 IRQF_TRIGGER_FALLING |
309 IRQF_TRIGGER_RISING,
310 "MAX8903 USB IN", data);
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900311 if (ret) {
312 dev_err(dev, "Cannot request irq %d for USB (%d)\n",
313 gpio_to_irq(pdata->uok), ret);
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530314 return ret;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900315 }
316 }
317
318 if (pdata->flt) {
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530319 ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->flt),
320 NULL, max8903_fault,
321 IRQF_TRIGGER_FALLING |
322 IRQF_TRIGGER_RISING,
323 "MAX8903 Fault", data);
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900324 if (ret) {
325 dev_err(dev, "Cannot request irq %d for Fault (%d)\n",
326 gpio_to_irq(pdata->flt), ret);
Vaishali Thakkar0df2dee2015-09-17 18:55:00 +0530327 return ret;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900328 }
329 }
330
331 return 0;
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900332}
333
334static struct platform_driver max8903_driver = {
335 .probe = max8903_probe,
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900336 .driver = {
337 .name = "max8903-charger",
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900338 },
339};
340
Axel Lin300bac72011-11-26 12:01:10 +0800341module_platform_driver(max8903_driver);
MyungJoo Hamb14a9cc2011-03-29 10:10:16 +0900342
343MODULE_LICENSE("GPL");
344MODULE_DESCRIPTION("MAX8903 Charger Driver");
345MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
Axel Linbd19c752011-06-24 22:26:36 +0800346MODULE_ALIAS("platform:max8903-charger");