]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/power/lp8727_charger.c
lp8727_charger: Add error check routine on probe()
[thirdparty/kernel/stable.git] / drivers / power / lp8727_charger.c
CommitLineData
2165c8a4
WK
1/*
2 * Driver for LP8727 Micro/Mini USB IC with intergrated charger
3 *
e39b828f 4 * Copyright (C) 2011 Texas Instruments
2165c8a4
WK
5 * Copyright (C) 2011 National Semiconductor
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 version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/interrupt.h>
16#include <linux/i2c.h>
17#include <linux/power_supply.h>
18#include <linux/lp8727.h>
19
20#define DEBOUNCE_MSEC 270
21
22/* Registers */
23#define CTRL1 0x1
24#define CTRL2 0x2
25#define SWCTRL 0x3
26#define INT1 0x4
27#define INT2 0x5
28#define STATUS1 0x6
29#define STATUS2 0x7
30#define CHGCTRL2 0x9
31
32/* CTRL1 register */
33#define CP_EN (1 << 0)
34#define ADC_EN (1 << 1)
35#define ID200_EN (1 << 4)
36
37/* CTRL2 register */
38#define CHGDET_EN (1 << 1)
39#define INT_EN (1 << 6)
40
41/* SWCTRL register */
42#define SW_DM1_DM (0x0 << 0)
43#define SW_DM1_U1 (0x1 << 0)
44#define SW_DM1_HiZ (0x7 << 0)
45#define SW_DP2_DP (0x0 << 3)
46#define SW_DP2_U2 (0x1 << 3)
47#define SW_DP2_HiZ (0x7 << 3)
48
49/* INT1 register */
50#define IDNO (0xF << 0)
51#define VBUS (1 << 4)
52
53/* STATUS1 register */
54#define CHGSTAT (3 << 4)
55#define CHPORT (1 << 6)
56#define DCPORT (1 << 7)
57
58/* STATUS2 register */
59#define TEMP_STAT (3 << 5)
60
61enum lp8727_dev_id {
62 ID_NONE,
63 ID_TA,
64 ID_DEDICATED_CHG,
65 ID_USB_CHG,
66 ID_USB_DS,
67 ID_MAX,
68};
69
70enum lp8727_chg_stat {
71 PRECHG,
72 CC,
73 CV,
74 EOC,
75};
76
77struct lp8727_psy {
78 struct power_supply ac;
79 struct power_supply usb;
80 struct power_supply batt;
81};
82
83struct lp8727_chg {
84 struct device *dev;
85 struct i2c_client *client;
86 struct mutex xfer_lock;
87 struct delayed_work work;
88 struct workqueue_struct *irqthread;
89 struct lp8727_platform_data *pdata;
90 struct lp8727_psy *psy;
91 struct lp8727_chg_param *chg_parm;
92 enum lp8727_dev_id devid;
93};
94
ce09affc 95static int lp8727_i2c_read(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len)
2165c8a4
WK
96{
97 s32 ret;
98
99 mutex_lock(&pchg->xfer_lock);
100 ret = i2c_smbus_read_i2c_block_data(pchg->client, reg, len, data);
101 mutex_unlock(&pchg->xfer_lock);
102
103 return (ret != len) ? -EIO : 0;
104}
105
ce09affc 106static int lp8727_i2c_write(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len)
2165c8a4
WK
107{
108 s32 ret;
109
110 mutex_lock(&pchg->xfer_lock);
111 ret = i2c_smbus_write_i2c_block_data(pchg->client, reg, len, data);
112 mutex_unlock(&pchg->xfer_lock);
113
114 return ret;
115}
116
117static inline int lp8727_i2c_read_byte(struct lp8727_chg *pchg, u8 reg,
ce09affc 118 u8 *data)
2165c8a4
WK
119{
120 return lp8727_i2c_read(pchg, reg, data, 1);
121}
122
123static inline int lp8727_i2c_write_byte(struct lp8727_chg *pchg, u8 reg,
ce09affc 124 u8 *data)
2165c8a4
WK
125{
126 return lp8727_i2c_write(pchg, reg, data, 1);
127}
128
129static int lp8727_is_charger_attached(const char *name, int id)
130{
131 if (name) {
132 if (!strcmp(name, "ac"))
133 return (id == ID_TA || id == ID_DEDICATED_CHG) ? 1 : 0;
134 else if (!strcmp(name, "usb"))
135 return (id == ID_USB_CHG) ? 1 : 0;
136 }
137
138 return (id >= ID_TA && id <= ID_USB_CHG) ? 1 : 0;
139}
140
7da6334e 141static int lp8727_init_device(struct lp8727_chg *pchg)
2165c8a4
WK
142{
143 u8 val;
7da6334e 144 int ret;
2165c8a4
WK
145
146 val = ID200_EN | ADC_EN | CP_EN;
7da6334e
KM
147 ret = lp8727_i2c_write_byte(pchg, CTRL1, &val);
148 if (ret)
149 return ret;
2165c8a4
WK
150
151 val = INT_EN | CHGDET_EN;
7da6334e
KM
152 ret = lp8727_i2c_write_byte(pchg, CTRL2, &val);
153 if (ret)
154 return ret;
155
156 return 0;
2165c8a4
WK
157}
158
159static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg)
160{
161 u8 val;
ce09affc 162 lp8727_i2c_read_byte(pchg, STATUS1, &val);
2165c8a4
WK
163 return (val & DCPORT);
164}
165
166static int lp8727_is_usb_charger(struct lp8727_chg *pchg)
167{
168 u8 val;
ce09affc 169 lp8727_i2c_read_byte(pchg, STATUS1, &val);
2165c8a4
WK
170 return (val & CHPORT);
171}
172
173static void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw)
174{
175 u8 val = sw;
ce09affc 176 lp8727_i2c_write_byte(pchg, SWCTRL, &val);
2165c8a4
WK
177}
178
179static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin)
180{
181 u8 devid = ID_NONE;
182 u8 swctrl = SW_DM1_HiZ | SW_DP2_HiZ;
183
184 switch (id) {
185 case 0x5:
186 devid = ID_TA;
187 pchg->chg_parm = &pchg->pdata->ac;
188 break;
189 case 0xB:
190 if (lp8727_is_dedicated_charger(pchg)) {
191 pchg->chg_parm = &pchg->pdata->ac;
192 devid = ID_DEDICATED_CHG;
193 } else if (lp8727_is_usb_charger(pchg)) {
194 pchg->chg_parm = &pchg->pdata->usb;
195 devid = ID_USB_CHG;
196 swctrl = SW_DM1_DM | SW_DP2_DP;
197 } else if (vbusin) {
198 devid = ID_USB_DS;
199 swctrl = SW_DM1_DM | SW_DP2_DP;
200 }
201 break;
202 default:
203 devid = ID_NONE;
204 pchg->chg_parm = NULL;
205 break;
206 }
207
208 pchg->devid = devid;
209 lp8727_ctrl_switch(pchg, swctrl);
210}
211
212static void lp8727_enable_chgdet(struct lp8727_chg *pchg)
213{
214 u8 val;
215
ce09affc 216 lp8727_i2c_read_byte(pchg, CTRL2, &val);
2165c8a4 217 val |= CHGDET_EN;
ce09affc 218 lp8727_i2c_write_byte(pchg, CTRL2, &val);
2165c8a4
WK
219}
220
221static void lp8727_delayed_func(struct work_struct *_work)
222{
223 u8 intstat[2], idno, vbus;
224 struct lp8727_chg *pchg =
225 container_of(_work, struct lp8727_chg, work.work);
226
227 if (lp8727_i2c_read(pchg, INT1, intstat, 2)) {
228 dev_err(pchg->dev, "can not read INT registers\n");
229 return;
230 }
231
232 idno = intstat[0] & IDNO;
233 vbus = intstat[0] & VBUS;
234
235 lp8727_id_detection(pchg, idno, vbus);
236 lp8727_enable_chgdet(pchg);
237
238 power_supply_changed(&pchg->psy->ac);
239 power_supply_changed(&pchg->psy->usb);
240 power_supply_changed(&pchg->psy->batt);
241}
242
243static irqreturn_t lp8727_isr_func(int irq, void *ptr)
244{
245 struct lp8727_chg *pchg = ptr;
246 unsigned long delay = msecs_to_jiffies(DEBOUNCE_MSEC);
247
248 queue_delayed_work(pchg->irqthread, &pchg->work, delay);
249
250 return IRQ_HANDLED;
251}
252
7da6334e 253static int lp8727_intr_config(struct lp8727_chg *pchg)
2165c8a4
WK
254{
255 INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
256
257 pchg->irqthread = create_singlethread_workqueue("lp8727-irqthd");
7da6334e 258 if (!pchg->irqthread) {
2165c8a4 259 dev_err(pchg->dev, "can not create thread for lp8727\n");
7da6334e 260 return -ENOMEM;
2165c8a4 261 }
7da6334e
KM
262
263 return request_threaded_irq(pchg->client->irq,
264 NULL,
265 lp8727_isr_func,
266 IRQF_TRIGGER_FALLING,
267 "lp8727_irq",
268 pchg);
2165c8a4
WK
269}
270
271static enum power_supply_property lp8727_charger_prop[] = {
272 POWER_SUPPLY_PROP_ONLINE,
273};
274
275static enum power_supply_property lp8727_battery_prop[] = {
276 POWER_SUPPLY_PROP_STATUS,
277 POWER_SUPPLY_PROP_HEALTH,
278 POWER_SUPPLY_PROP_PRESENT,
279 POWER_SUPPLY_PROP_VOLTAGE_NOW,
280 POWER_SUPPLY_PROP_CAPACITY,
281 POWER_SUPPLY_PROP_TEMP,
282};
283
284static char *battery_supplied_to[] = {
285 "main_batt",
286};
287
288static int lp8727_charger_get_property(struct power_supply *psy,
289 enum power_supply_property psp,
290 union power_supply_propval *val)
291{
292 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
293
ce09affc 294 if (psp == POWER_SUPPLY_PROP_ONLINE)
2165c8a4
WK
295 val->intval = lp8727_is_charger_attached(psy->name,
296 pchg->devid);
2165c8a4
WK
297
298 return 0;
299}
300
301static int lp8727_battery_get_property(struct power_supply *psy,
302 enum power_supply_property psp,
303 union power_supply_propval *val)
304{
305 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
306 u8 read;
307
308 switch (psp) {
309 case POWER_SUPPLY_PROP_STATUS:
310 if (lp8727_is_charger_attached(psy->name, pchg->devid)) {
ce09affc 311 lp8727_i2c_read_byte(pchg, STATUS1, &read);
2165c8a4
WK
312 if (((read & CHGSTAT) >> 4) == EOC)
313 val->intval = POWER_SUPPLY_STATUS_FULL;
314 else
315 val->intval = POWER_SUPPLY_STATUS_CHARGING;
316 } else {
317 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
318 }
319 break;
320 case POWER_SUPPLY_PROP_HEALTH:
ce09affc 321 lp8727_i2c_read_byte(pchg, STATUS2, &read);
2165c8a4
WK
322 read = (read & TEMP_STAT) >> 5;
323 if (read >= 0x1 && read <= 0x3)
324 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
325 else
326 val->intval = POWER_SUPPLY_HEALTH_GOOD;
327 break;
328 case POWER_SUPPLY_PROP_PRESENT:
329 if (pchg->pdata->get_batt_present)
330 val->intval = pchg->pdata->get_batt_present();
331 break;
332 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
333 if (pchg->pdata->get_batt_level)
334 val->intval = pchg->pdata->get_batt_level();
335 break;
336 case POWER_SUPPLY_PROP_CAPACITY:
337 if (pchg->pdata->get_batt_capacity)
338 val->intval = pchg->pdata->get_batt_capacity();
339 break;
340 case POWER_SUPPLY_PROP_TEMP:
341 if (pchg->pdata->get_batt_temp)
342 val->intval = pchg->pdata->get_batt_temp();
343 break;
344 default:
345 break;
346 }
347
348 return 0;
349}
350
351static void lp8727_charger_changed(struct power_supply *psy)
352{
353 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
354 u8 val;
355 u8 eoc_level, ichg;
356
357 if (lp8727_is_charger_attached(psy->name, pchg->devid)) {
358 if (pchg->chg_parm) {
359 eoc_level = pchg->chg_parm->eoc_level;
360 ichg = pchg->chg_parm->ichg;
361 val = (ichg << 4) | eoc_level;
ce09affc 362 lp8727_i2c_write_byte(pchg, CHGCTRL2, &val);
2165c8a4
WK
363 }
364 }
365}
366
367static int lp8727_register_psy(struct lp8727_chg *pchg)
368{
369 struct lp8727_psy *psy;
370
371 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
372 if (!psy)
373 goto err_mem;
374
375 pchg->psy = psy;
376
377 psy->ac.name = "ac";
378 psy->ac.type = POWER_SUPPLY_TYPE_MAINS;
379 psy->ac.properties = lp8727_charger_prop;
380 psy->ac.num_properties = ARRAY_SIZE(lp8727_charger_prop);
381 psy->ac.get_property = lp8727_charger_get_property;
382 psy->ac.supplied_to = battery_supplied_to;
383 psy->ac.num_supplicants = ARRAY_SIZE(battery_supplied_to);
384
385 if (power_supply_register(pchg->dev, &psy->ac))
386 goto err_psy;
387
388 psy->usb.name = "usb";
389 psy->usb.type = POWER_SUPPLY_TYPE_USB;
390 psy->usb.properties = lp8727_charger_prop;
391 psy->usb.num_properties = ARRAY_SIZE(lp8727_charger_prop);
392 psy->usb.get_property = lp8727_charger_get_property;
393 psy->usb.supplied_to = battery_supplied_to;
394 psy->usb.num_supplicants = ARRAY_SIZE(battery_supplied_to);
395
396 if (power_supply_register(pchg->dev, &psy->usb))
397 goto err_psy;
398
399 psy->batt.name = "main_batt";
400 psy->batt.type = POWER_SUPPLY_TYPE_BATTERY;
401 psy->batt.properties = lp8727_battery_prop;
402 psy->batt.num_properties = ARRAY_SIZE(lp8727_battery_prop);
403 psy->batt.get_property = lp8727_battery_get_property;
404 psy->batt.external_power_changed = lp8727_charger_changed;
405
406 if (power_supply_register(pchg->dev, &psy->batt))
407 goto err_psy;
408
409 return 0;
410
411err_mem:
412 return -ENOMEM;
413err_psy:
414 kfree(psy);
415 return -EPERM;
416}
417
418static void lp8727_unregister_psy(struct lp8727_chg *pchg)
419{
420 struct lp8727_psy *psy = pchg->psy;
421
ce09affc
KM
422 if (!psy)
423 return;
424
425 power_supply_unregister(&psy->ac);
426 power_supply_unregister(&psy->usb);
427 power_supply_unregister(&psy->batt);
428 kfree(psy);
2165c8a4
WK
429}
430
431static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id)
432{
433 struct lp8727_chg *pchg;
434 int ret;
435
998a8e7a
KM
436 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
437 return -EIO;
438
2165c8a4
WK
439 pchg = kzalloc(sizeof(*pchg), GFP_KERNEL);
440 if (!pchg)
441 return -ENOMEM;
442
443 pchg->client = cl;
444 pchg->dev = &cl->dev;
445 pchg->pdata = cl->dev.platform_data;
446 i2c_set_clientdata(cl, pchg);
447
448 mutex_init(&pchg->xfer_lock);
449
7da6334e
KM
450 ret = lp8727_init_device(pchg);
451 if (ret) {
452 dev_err(pchg->dev, "i2c communication err: %d", ret);
453 goto error;
454 }
455
456 ret = lp8727_intr_config(pchg);
457 if (ret) {
458 dev_err(pchg->dev, "irq handler err: %d", ret);
459 goto error;
460 }
2165c8a4
WK
461
462 ret = lp8727_register_psy(pchg);
7da6334e
KM
463 if (ret) {
464 dev_err(pchg->dev, "power supplies register err: %d", ret);
465 goto error;
466 }
2165c8a4
WK
467
468 return 0;
7da6334e
KM
469
470error:
471 kfree(pchg);
472 return ret;
2165c8a4
WK
473}
474
475static int __devexit lp8727_remove(struct i2c_client *cl)
476{
477 struct lp8727_chg *pchg = i2c_get_clientdata(cl);
478
479 lp8727_unregister_psy(pchg);
480 free_irq(pchg->client->irq, pchg);
481 flush_workqueue(pchg->irqthread);
482 destroy_workqueue(pchg->irqthread);
483 kfree(pchg);
484 return 0;
485}
486
487static const struct i2c_device_id lp8727_ids[] = {
488 {"lp8727", 0},
455a0e2c 489 { }
2165c8a4
WK
490};
491
492static struct i2c_driver lp8727_driver = {
493 .driver = {
494 .name = "lp8727",
495 },
496 .probe = lp8727_probe,
497 .remove = __devexit_p(lp8727_remove),
498 .id_table = lp8727_ids,
499};
500
501static int __init lp8727_init(void)
502{
503 return i2c_add_driver(&lp8727_driver);
504}
505
ce09affc 506static void __exit lp8727_exit(void)
2165c8a4
WK
507{
508 i2c_del_driver(&lp8727_driver);
509}
510
511module_init(lp8727_init);
ce09affc 512module_exit(lp8727_exit);
2165c8a4 513
e39b828f 514MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
ce09affc
KM
515MODULE_AUTHOR
516 ("Woogyom Kim <milo.kim@ti.com>, Daniel Jeong <daniel.jeong@ti.com>");
2165c8a4 517MODULE_LICENSE("GPL");