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