blob: ab0f431876e14633b7f7b67dfcf9965be5d276fd [file] [log] [blame]
Woogyom Kim2165c8a2012-01-04 08:27:43 +04001/*
Kim, Milof7bae492012-01-26 22:59:08 -08002 * Driver for LP8727 Micro/Mini USB IC with integrated charger
Woogyom Kim2165c8a2012-01-04 08:27:43 +04003 *
Kim, Miloe39b8282012-01-29 17:28:18 -08004 * Copyright (C) 2011 Texas Instruments
Woogyom Kim2165c8a2012-01-04 08:27:43 +04005 * 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
Kim, Milo73368802012-01-26 22:58:51 -080029#define STATUS2 0x7
Woogyom Kim2165c8a2012-01-04 08:27:43 +040030#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
Kim, Milo27aefa32012-01-26 22:58:39 -080095static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len)
Woogyom Kim2165c8a2012-01-04 08:27:43 +040096{
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
Kim, Milo27aefa32012-01-26 22:58:39 -0800106static inline int lp8727_read_byte(struct lp8727_chg *pchg, u8 reg, u8 *data)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400107{
Kim, Milo27aefa32012-01-26 22:58:39 -0800108 return lp8727_read_bytes(pchg, reg, data, 1);
109}
110
111static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data)
112{
113 int ret;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400114
115 mutex_lock(&pchg->xfer_lock);
Kim, Milo27aefa32012-01-26 22:58:39 -0800116 ret = i2c_smbus_write_byte_data(pchg->client, reg, data);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400117 mutex_unlock(&pchg->xfer_lock);
118
119 return ret;
120}
121
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400122static int lp8727_is_charger_attached(const char *name, int id)
123{
124 if (name) {
125 if (!strcmp(name, "ac"))
126 return (id == ID_TA || id == ID_DEDICATED_CHG) ? 1 : 0;
127 else if (!strcmp(name, "usb"))
128 return (id == ID_USB_CHG) ? 1 : 0;
129 }
130
131 return (id >= ID_TA && id <= ID_USB_CHG) ? 1 : 0;
132}
133
Kim, Milo7da63342012-01-26 22:58:30 -0800134static int lp8727_init_device(struct lp8727_chg *pchg)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400135{
136 u8 val;
Kim, Milo7da63342012-01-26 22:58:30 -0800137 int ret;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400138
139 val = ID200_EN | ADC_EN | CP_EN;
Kim, Milo27aefa32012-01-26 22:58:39 -0800140 ret = lp8727_write_byte(pchg, CTRL1, val);
Kim, Milo7da63342012-01-26 22:58:30 -0800141 if (ret)
142 return ret;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400143
144 val = INT_EN | CHGDET_EN;
Kim, Milo27aefa32012-01-26 22:58:39 -0800145 ret = lp8727_write_byte(pchg, CTRL2, val);
Kim, Milo7da63342012-01-26 22:58:30 -0800146 if (ret)
147 return ret;
148
149 return 0;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400150}
151
152static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg)
153{
154 u8 val;
Kim, Milo27aefa32012-01-26 22:58:39 -0800155 lp8727_read_byte(pchg, STATUS1, &val);
Kim, Milo73368802012-01-26 22:58:51 -0800156 return val & DCPORT;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400157}
158
159static int lp8727_is_usb_charger(struct lp8727_chg *pchg)
160{
161 u8 val;
Kim, Milo27aefa32012-01-26 22:58:39 -0800162 lp8727_read_byte(pchg, STATUS1, &val);
Kim, Milo73368802012-01-26 22:58:51 -0800163 return val & CHPORT;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400164}
165
166static void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw)
167{
Kim, Milo27aefa32012-01-26 22:58:39 -0800168 lp8727_write_byte(pchg, SWCTRL, sw);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400169}
170
171static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin)
172{
173 u8 devid = ID_NONE;
174 u8 swctrl = SW_DM1_HiZ | SW_DP2_HiZ;
175
176 switch (id) {
177 case 0x5:
178 devid = ID_TA;
179 pchg->chg_parm = &pchg->pdata->ac;
180 break;
181 case 0xB:
182 if (lp8727_is_dedicated_charger(pchg)) {
183 pchg->chg_parm = &pchg->pdata->ac;
184 devid = ID_DEDICATED_CHG;
185 } else if (lp8727_is_usb_charger(pchg)) {
186 pchg->chg_parm = &pchg->pdata->usb;
187 devid = ID_USB_CHG;
188 swctrl = SW_DM1_DM | SW_DP2_DP;
189 } else if (vbusin) {
190 devid = ID_USB_DS;
191 swctrl = SW_DM1_DM | SW_DP2_DP;
192 }
193 break;
194 default:
195 devid = ID_NONE;
196 pchg->chg_parm = NULL;
197 break;
198 }
199
200 pchg->devid = devid;
201 lp8727_ctrl_switch(pchg, swctrl);
202}
203
204static void lp8727_enable_chgdet(struct lp8727_chg *pchg)
205{
206 u8 val;
207
Kim, Milo27aefa32012-01-26 22:58:39 -0800208 lp8727_read_byte(pchg, CTRL2, &val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400209 val |= CHGDET_EN;
Kim, Milo27aefa32012-01-26 22:58:39 -0800210 lp8727_write_byte(pchg, CTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400211}
212
213static void lp8727_delayed_func(struct work_struct *_work)
214{
215 u8 intstat[2], idno, vbus;
216 struct lp8727_chg *pchg =
217 container_of(_work, struct lp8727_chg, work.work);
218
Kim, Milo27aefa32012-01-26 22:58:39 -0800219 if (lp8727_read_bytes(pchg, INT1, intstat, 2)) {
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400220 dev_err(pchg->dev, "can not read INT registers\n");
221 return;
222 }
223
224 idno = intstat[0] & IDNO;
225 vbus = intstat[0] & VBUS;
226
227 lp8727_id_detection(pchg, idno, vbus);
228 lp8727_enable_chgdet(pchg);
229
230 power_supply_changed(&pchg->psy->ac);
231 power_supply_changed(&pchg->psy->usb);
232 power_supply_changed(&pchg->psy->batt);
233}
234
235static irqreturn_t lp8727_isr_func(int irq, void *ptr)
236{
237 struct lp8727_chg *pchg = ptr;
238 unsigned long delay = msecs_to_jiffies(DEBOUNCE_MSEC);
239
240 queue_delayed_work(pchg->irqthread, &pchg->work, delay);
241
242 return IRQ_HANDLED;
243}
244
Kim, Milo7da63342012-01-26 22:58:30 -0800245static int lp8727_intr_config(struct lp8727_chg *pchg)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400246{
247 INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
248
249 pchg->irqthread = create_singlethread_workqueue("lp8727-irqthd");
Kim, Milo7da63342012-01-26 22:58:30 -0800250 if (!pchg->irqthread) {
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400251 dev_err(pchg->dev, "can not create thread for lp8727\n");
Kim, Milo7da63342012-01-26 22:58:30 -0800252 return -ENOMEM;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400253 }
Kim, Milo7da63342012-01-26 22:58:30 -0800254
255 return request_threaded_irq(pchg->client->irq,
256 NULL,
257 lp8727_isr_func,
258 IRQF_TRIGGER_FALLING,
259 "lp8727_irq",
260 pchg);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400261}
262
263static enum power_supply_property lp8727_charger_prop[] = {
264 POWER_SUPPLY_PROP_ONLINE,
265};
266
267static enum power_supply_property lp8727_battery_prop[] = {
268 POWER_SUPPLY_PROP_STATUS,
269 POWER_SUPPLY_PROP_HEALTH,
270 POWER_SUPPLY_PROP_PRESENT,
271 POWER_SUPPLY_PROP_VOLTAGE_NOW,
272 POWER_SUPPLY_PROP_CAPACITY,
273 POWER_SUPPLY_PROP_TEMP,
274};
275
276static char *battery_supplied_to[] = {
277 "main_batt",
278};
279
280static int lp8727_charger_get_property(struct power_supply *psy,
281 enum power_supply_property psp,
282 union power_supply_propval *val)
283{
284 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
285
Kim, Miloce09aff2012-01-04 09:03:18 +0400286 if (psp == POWER_SUPPLY_PROP_ONLINE)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400287 val->intval = lp8727_is_charger_attached(psy->name,
288 pchg->devid);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400289
290 return 0;
291}
292
293static int lp8727_battery_get_property(struct power_supply *psy,
294 enum power_supply_property psp,
295 union power_supply_propval *val)
296{
297 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
298 u8 read;
299
300 switch (psp) {
301 case POWER_SUPPLY_PROP_STATUS:
302 if (lp8727_is_charger_attached(psy->name, pchg->devid)) {
Kim, Milo27aefa32012-01-26 22:58:39 -0800303 lp8727_read_byte(pchg, STATUS1, &read);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400304 if (((read & CHGSTAT) >> 4) == EOC)
305 val->intval = POWER_SUPPLY_STATUS_FULL;
306 else
307 val->intval = POWER_SUPPLY_STATUS_CHARGING;
308 } else {
309 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
310 }
311 break;
312 case POWER_SUPPLY_PROP_HEALTH:
Kim, Milo27aefa32012-01-26 22:58:39 -0800313 lp8727_read_byte(pchg, STATUS2, &read);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400314 read = (read & TEMP_STAT) >> 5;
315 if (read >= 0x1 && read <= 0x3)
316 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
317 else
318 val->intval = POWER_SUPPLY_HEALTH_GOOD;
319 break;
320 case POWER_SUPPLY_PROP_PRESENT:
321 if (pchg->pdata->get_batt_present)
322 val->intval = pchg->pdata->get_batt_present();
323 break;
324 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
325 if (pchg->pdata->get_batt_level)
326 val->intval = pchg->pdata->get_batt_level();
327 break;
328 case POWER_SUPPLY_PROP_CAPACITY:
329 if (pchg->pdata->get_batt_capacity)
330 val->intval = pchg->pdata->get_batt_capacity();
331 break;
332 case POWER_SUPPLY_PROP_TEMP:
333 if (pchg->pdata->get_batt_temp)
334 val->intval = pchg->pdata->get_batt_temp();
335 break;
336 default:
337 break;
338 }
339
340 return 0;
341}
342
343static void lp8727_charger_changed(struct power_supply *psy)
344{
345 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
346 u8 val;
347 u8 eoc_level, ichg;
348
349 if (lp8727_is_charger_attached(psy->name, pchg->devid)) {
350 if (pchg->chg_parm) {
351 eoc_level = pchg->chg_parm->eoc_level;
352 ichg = pchg->chg_parm->ichg;
353 val = (ichg << 4) | eoc_level;
Kim, Milo27aefa32012-01-26 22:58:39 -0800354 lp8727_write_byte(pchg, CHGCTRL2, val);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400355 }
356 }
357}
358
359static int lp8727_register_psy(struct lp8727_chg *pchg)
360{
361 struct lp8727_psy *psy;
362
363 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
364 if (!psy)
365 goto err_mem;
366
367 pchg->psy = psy;
368
369 psy->ac.name = "ac";
370 psy->ac.type = POWER_SUPPLY_TYPE_MAINS;
371 psy->ac.properties = lp8727_charger_prop;
372 psy->ac.num_properties = ARRAY_SIZE(lp8727_charger_prop);
373 psy->ac.get_property = lp8727_charger_get_property;
374 psy->ac.supplied_to = battery_supplied_to;
375 psy->ac.num_supplicants = ARRAY_SIZE(battery_supplied_to);
376
377 if (power_supply_register(pchg->dev, &psy->ac))
378 goto err_psy;
379
380 psy->usb.name = "usb";
381 psy->usb.type = POWER_SUPPLY_TYPE_USB;
382 psy->usb.properties = lp8727_charger_prop;
383 psy->usb.num_properties = ARRAY_SIZE(lp8727_charger_prop);
384 psy->usb.get_property = lp8727_charger_get_property;
385 psy->usb.supplied_to = battery_supplied_to;
386 psy->usb.num_supplicants = ARRAY_SIZE(battery_supplied_to);
387
388 if (power_supply_register(pchg->dev, &psy->usb))
389 goto err_psy;
390
391 psy->batt.name = "main_batt";
392 psy->batt.type = POWER_SUPPLY_TYPE_BATTERY;
393 psy->batt.properties = lp8727_battery_prop;
394 psy->batt.num_properties = ARRAY_SIZE(lp8727_battery_prop);
395 psy->batt.get_property = lp8727_battery_get_property;
396 psy->batt.external_power_changed = lp8727_charger_changed;
397
398 if (power_supply_register(pchg->dev, &psy->batt))
399 goto err_psy;
400
401 return 0;
402
403err_mem:
404 return -ENOMEM;
405err_psy:
406 kfree(psy);
407 return -EPERM;
408}
409
410static void lp8727_unregister_psy(struct lp8727_chg *pchg)
411{
412 struct lp8727_psy *psy = pchg->psy;
413
Kim, Miloce09aff2012-01-04 09:03:18 +0400414 if (!psy)
415 return;
416
417 power_supply_unregister(&psy->ac);
418 power_supply_unregister(&psy->usb);
419 power_supply_unregister(&psy->batt);
420 kfree(psy);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400421}
422
423static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id)
424{
425 struct lp8727_chg *pchg;
426 int ret;
427
Kim, Milo998a8e72011-11-17 21:43:06 -0800428 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
429 return -EIO;
430
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400431 pchg = kzalloc(sizeof(*pchg), GFP_KERNEL);
432 if (!pchg)
433 return -ENOMEM;
434
435 pchg->client = cl;
436 pchg->dev = &cl->dev;
437 pchg->pdata = cl->dev.platform_data;
438 i2c_set_clientdata(cl, pchg);
439
440 mutex_init(&pchg->xfer_lock);
441
Kim, Milo7da63342012-01-26 22:58:30 -0800442 ret = lp8727_init_device(pchg);
443 if (ret) {
444 dev_err(pchg->dev, "i2c communication err: %d", ret);
445 goto error;
446 }
447
448 ret = lp8727_intr_config(pchg);
449 if (ret) {
450 dev_err(pchg->dev, "irq handler err: %d", ret);
451 goto error;
452 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400453
454 ret = lp8727_register_psy(pchg);
Kim, Milo7da63342012-01-26 22:58:30 -0800455 if (ret) {
456 dev_err(pchg->dev, "power supplies register err: %d", ret);
457 goto error;
458 }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400459
460 return 0;
Kim, Milo7da63342012-01-26 22:58:30 -0800461
462error:
463 kfree(pchg);
464 return ret;
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400465}
466
467static int __devexit lp8727_remove(struct i2c_client *cl)
468{
469 struct lp8727_chg *pchg = i2c_get_clientdata(cl);
470
471 lp8727_unregister_psy(pchg);
472 free_irq(pchg->client->irq, pchg);
473 flush_workqueue(pchg->irqthread);
474 destroy_workqueue(pchg->irqthread);
475 kfree(pchg);
476 return 0;
477}
478
479static const struct i2c_device_id lp8727_ids[] = {
480 {"lp8727", 0},
Axel Lin455a0e22012-01-16 13:48:20 +0800481 { }
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400482};
Axel Line2c5f7d2012-01-12 20:45:02 +0800483MODULE_DEVICE_TABLE(i2c, lp8727_ids);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400484
485static struct i2c_driver lp8727_driver = {
486 .driver = {
487 .name = "lp8727",
488 },
489 .probe = lp8727_probe,
490 .remove = __devexit_p(lp8727_remove),
491 .id_table = lp8727_ids,
492};
493
494static int __init lp8727_init(void)
495{
496 return i2c_add_driver(&lp8727_driver);
497}
498
Kim, Miloce09aff2012-01-04 09:03:18 +0400499static void __exit lp8727_exit(void)
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400500{
501 i2c_del_driver(&lp8727_driver);
502}
503
504module_init(lp8727_init);
Kim, Miloce09aff2012-01-04 09:03:18 +0400505module_exit(lp8727_exit);
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400506
Kim, Miloe39b8282012-01-29 17:28:18 -0800507MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
Kim, Milo73368802012-01-26 22:58:51 -0800508MODULE_AUTHOR("Woogyom Kim <milo.kim@ti.com>, "
509 "Daniel Jeong <daniel.jeong@ti.com>");
Woogyom Kim2165c8a2012-01-04 08:27:43 +0400510MODULE_LICENSE("GPL");