lp8727_charger: Make some cosmetic changes in lp8727_delayed_func()
[deliverable/linux.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 LP8788_NUM_INTREGS 2
21 #define DEFAULT_DEBOUNCE_MSEC 270
22
23 /* Registers */
24 #define LP8727_CTRL1 0x1
25 #define LP8727_CTRL2 0x2
26 #define LP8727_SWCTRL 0x3
27 #define LP8727_INT1 0x4
28 #define LP8727_INT2 0x5
29 #define LP8727_STATUS1 0x6
30 #define LP8727_STATUS2 0x7
31 #define LP8727_CHGCTRL2 0x9
32
33 /* CTRL1 register */
34 #define LP8727_CP_EN BIT(0)
35 #define LP8727_ADC_EN BIT(1)
36 #define LP8727_ID200_EN BIT(4)
37
38 /* CTRL2 register */
39 #define LP8727_CHGDET_EN BIT(1)
40 #define LP8727_INT_EN BIT(6)
41
42 /* SWCTRL register */
43 #define LP8727_SW_DM1_DM (0x0 << 0)
44 #define LP8727_SW_DM1_HiZ (0x7 << 0)
45 #define LP8727_SW_DP2_DP (0x0 << 3)
46 #define LP8727_SW_DP2_HiZ (0x7 << 3)
47
48 /* INT1 register */
49 #define LP8727_IDNO (0xF << 0)
50 #define LP8727_VBUS BIT(4)
51
52 /* STATUS1 register */
53 #define LP8727_CHGSTAT (3 << 4)
54 #define LP8727_CHPORT BIT(6)
55 #define LP8727_DCPORT BIT(7)
56 #define LP8727_STAT_EOC 0x30
57
58 /* STATUS2 register */
59 #define LP8727_TEMP_STAT (3 << 5)
60 #define LP8727_TEMP_SHIFT 5
61
62 /* CHGCTRL2 register */
63 #define LP8727_ICHG_SHIFT 4
64
65 enum lp8727_dev_id {
66 LP8727_ID_NONE,
67 LP8727_ID_TA,
68 LP8727_ID_DEDICATED_CHG,
69 LP8727_ID_USB_CHG,
70 LP8727_ID_USB_DS,
71 LP8727_ID_MAX,
72 };
73
74 enum lp8727_die_temp {
75 LP8788_TEMP_75C,
76 LP8788_TEMP_95C,
77 LP8788_TEMP_115C,
78 LP8788_TEMP_135C,
79 };
80
81 struct lp8727_psy {
82 struct power_supply ac;
83 struct power_supply usb;
84 struct power_supply batt;
85 };
86
87 struct lp8727_chg {
88 struct device *dev;
89 struct i2c_client *client;
90 struct mutex xfer_lock;
91 struct delayed_work work;
92 struct lp8727_platform_data *pdata;
93 struct lp8727_psy *psy;
94 struct lp8727_chg_param *chg_parm;
95 enum lp8727_dev_id devid;
96 unsigned long debounce_jiffies;
97 int irq;
98 };
99
100 static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len)
101 {
102 s32 ret;
103
104 mutex_lock(&pchg->xfer_lock);
105 ret = i2c_smbus_read_i2c_block_data(pchg->client, reg, len, data);
106 mutex_unlock(&pchg->xfer_lock);
107
108 return (ret != len) ? -EIO : 0;
109 }
110
111 static inline int lp8727_read_byte(struct lp8727_chg *pchg, u8 reg, u8 *data)
112 {
113 return lp8727_read_bytes(pchg, reg, data, 1);
114 }
115
116 static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data)
117 {
118 int ret;
119
120 mutex_lock(&pchg->xfer_lock);
121 ret = i2c_smbus_write_byte_data(pchg->client, reg, data);
122 mutex_unlock(&pchg->xfer_lock);
123
124 return ret;
125 }
126
127 static bool lp8727_is_charger_attached(const char *name, int id)
128 {
129 if (!strcmp(name, "ac"))
130 return id == LP8727_ID_TA || id == LP8727_ID_DEDICATED_CHG;
131 else if (!strcmp(name, "usb"))
132 return id == LP8727_ID_USB_CHG;
133
134 return id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG;
135 }
136
137 static int lp8727_init_device(struct lp8727_chg *pchg)
138 {
139 u8 val;
140 int ret;
141 u8 intstat[LP8788_NUM_INTREGS];
142
143 /* clear interrupts */
144 ret = lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS);
145 if (ret)
146 return ret;
147
148
149 val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN;
150 ret = lp8727_write_byte(pchg, LP8727_CTRL1, val);
151 if (ret)
152 return ret;
153
154 val = LP8727_INT_EN | LP8727_CHGDET_EN;
155 return lp8727_write_byte(pchg, LP8727_CTRL2, val);
156 }
157
158 static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg)
159 {
160 u8 val;
161 lp8727_read_byte(pchg, LP8727_STATUS1, &val);
162 return val & LP8727_DCPORT;
163 }
164
165 static int lp8727_is_usb_charger(struct lp8727_chg *pchg)
166 {
167 u8 val;
168 lp8727_read_byte(pchg, LP8727_STATUS1, &val);
169 return val & LP8727_CHPORT;
170 }
171
172 static inline void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw)
173 {
174 lp8727_write_byte(pchg, LP8727_SWCTRL, sw);
175 }
176
177 static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin)
178 {
179 struct lp8727_platform_data *pdata = pchg->pdata;
180 u8 devid = LP8727_ID_NONE;
181 u8 swctrl = LP8727_SW_DM1_HiZ | LP8727_SW_DP2_HiZ;
182
183 switch (id) {
184 case 0x5:
185 devid = LP8727_ID_TA;
186 pchg->chg_parm = pdata ? pdata->ac : NULL;
187 break;
188 case 0xB:
189 if (lp8727_is_dedicated_charger(pchg)) {
190 pchg->chg_parm = pdata ? pdata->ac : NULL;
191 devid = LP8727_ID_DEDICATED_CHG;
192 } else if (lp8727_is_usb_charger(pchg)) {
193 pchg->chg_parm = pdata ? pdata->usb : NULL;
194 devid = LP8727_ID_USB_CHG;
195 swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
196 } else if (vbusin) {
197 devid = LP8727_ID_USB_DS;
198 swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
199 }
200 break;
201 default:
202 devid = LP8727_ID_NONE;
203 pchg->chg_parm = NULL;
204 break;
205 }
206
207 pchg->devid = devid;
208 lp8727_ctrl_switch(pchg, swctrl);
209 }
210
211 static void lp8727_enable_chgdet(struct lp8727_chg *pchg)
212 {
213 u8 val;
214
215 lp8727_read_byte(pchg, LP8727_CTRL2, &val);
216 val |= LP8727_CHGDET_EN;
217 lp8727_write_byte(pchg, LP8727_CTRL2, val);
218 }
219
220 static void lp8727_delayed_func(struct work_struct *_work)
221 {
222 struct lp8727_chg *pchg = container_of(_work, struct lp8727_chg,
223 work.work);
224 u8 intstat[LP8788_NUM_INTREGS];
225 u8 idno;
226 u8 vbus;
227
228 if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS)) {
229 dev_err(pchg->dev, "can not read INT registers\n");
230 return;
231 }
232
233 idno = intstat[0] & LP8727_IDNO;
234 vbus = intstat[0] & LP8727_VBUS;
235
236 lp8727_id_detection(pchg, idno, vbus);
237 lp8727_enable_chgdet(pchg);
238
239 power_supply_changed(&pchg->psy->ac);
240 power_supply_changed(&pchg->psy->usb);
241 power_supply_changed(&pchg->psy->batt);
242 }
243
244 static irqreturn_t lp8727_isr_func(int irq, void *ptr)
245 {
246 struct lp8727_chg *pchg = ptr;
247
248 schedule_delayed_work(&pchg->work, pchg->debounce_jiffies);
249 return IRQ_HANDLED;
250 }
251
252 static int lp8727_setup_irq(struct lp8727_chg *pchg)
253 {
254 int ret;
255 int irq = pchg->client->irq;
256 unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec :
257 DEFAULT_DEBOUNCE_MSEC;
258
259 INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
260
261 if (irq <= 0) {
262 dev_warn(pchg->dev, "invalid irq number: %d\n", irq);
263 return 0;
264 }
265
266 ret = request_threaded_irq(irq, NULL, lp8727_isr_func,
267 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
268 "lp8727_irq", pchg);
269
270 if (ret)
271 return ret;
272
273 pchg->irq = irq;
274 pchg->debounce_jiffies = msecs_to_jiffies(delay_msec);
275
276 return 0;
277 }
278
279 static void lp8727_release_irq(struct lp8727_chg *pchg)
280 {
281 cancel_delayed_work_sync(&pchg->work);
282
283 if (pchg->irq)
284 free_irq(pchg->irq, pchg);
285 }
286
287 static enum power_supply_property lp8727_charger_prop[] = {
288 POWER_SUPPLY_PROP_ONLINE,
289 };
290
291 static enum power_supply_property lp8727_battery_prop[] = {
292 POWER_SUPPLY_PROP_STATUS,
293 POWER_SUPPLY_PROP_HEALTH,
294 POWER_SUPPLY_PROP_PRESENT,
295 POWER_SUPPLY_PROP_VOLTAGE_NOW,
296 POWER_SUPPLY_PROP_CAPACITY,
297 POWER_SUPPLY_PROP_TEMP,
298 };
299
300 static char *battery_supplied_to[] = {
301 "main_batt",
302 };
303
304 static int lp8727_charger_get_property(struct power_supply *psy,
305 enum power_supply_property psp,
306 union power_supply_propval *val)
307 {
308 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
309
310 if (psp != POWER_SUPPLY_PROP_ONLINE)
311 return -EINVAL;
312
313 val->intval = lp8727_is_charger_attached(psy->name, pchg->devid);
314
315 return 0;
316 }
317
318 static bool lp8727_is_high_temperature(enum lp8727_die_temp temp)
319 {
320 switch (temp) {
321 case LP8788_TEMP_95C:
322 case LP8788_TEMP_115C:
323 case LP8788_TEMP_135C:
324 return true;
325 default:
326 return false;
327 }
328 }
329
330 static int lp8727_battery_get_property(struct power_supply *psy,
331 enum power_supply_property psp,
332 union power_supply_propval *val)
333 {
334 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
335 struct lp8727_platform_data *pdata = pchg->pdata;
336 enum lp8727_die_temp temp;
337 u8 read;
338
339 switch (psp) {
340 case POWER_SUPPLY_PROP_STATUS:
341 if (!lp8727_is_charger_attached(psy->name, pchg->devid)) {
342 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
343 return 0;
344 }
345
346 lp8727_read_byte(pchg, LP8727_STATUS1, &read);
347
348 val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ?
349 POWER_SUPPLY_STATUS_FULL :
350 POWER_SUPPLY_STATUS_CHARGING;
351 break;
352 case POWER_SUPPLY_PROP_HEALTH:
353 lp8727_read_byte(pchg, LP8727_STATUS2, &read);
354 temp = (read & LP8727_TEMP_STAT) >> LP8727_TEMP_SHIFT;
355
356 val->intval = lp8727_is_high_temperature(temp) ?
357 POWER_SUPPLY_HEALTH_OVERHEAT :
358 POWER_SUPPLY_HEALTH_GOOD;
359 break;
360 case POWER_SUPPLY_PROP_PRESENT:
361 if (!pdata)
362 return -EINVAL;
363
364 if (pdata->get_batt_present)
365 val->intval = pchg->pdata->get_batt_present();
366 break;
367 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
368 if (!pdata)
369 return -EINVAL;
370
371 if (pdata->get_batt_level)
372 val->intval = pchg->pdata->get_batt_level();
373 break;
374 case POWER_SUPPLY_PROP_CAPACITY:
375 if (!pdata)
376 return -EINVAL;
377
378 if (pdata->get_batt_capacity)
379 val->intval = pchg->pdata->get_batt_capacity();
380 break;
381 case POWER_SUPPLY_PROP_TEMP:
382 if (!pdata)
383 return -EINVAL;
384
385 if (pdata->get_batt_temp)
386 val->intval = pchg->pdata->get_batt_temp();
387 break;
388 default:
389 break;
390 }
391
392 return 0;
393 }
394
395 static void lp8727_charger_changed(struct power_supply *psy)
396 {
397 struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
398 u8 eoc_level;
399 u8 ichg;
400 u8 val;
401
402 /* skip if no charger exists */
403 if (!lp8727_is_charger_attached(psy->name, pchg->devid))
404 return;
405
406 /* update charging parameters */
407 if (pchg->chg_parm) {
408 eoc_level = pchg->chg_parm->eoc_level;
409 ichg = pchg->chg_parm->ichg;
410 val = (ichg << LP8727_ICHG_SHIFT) | eoc_level;
411 lp8727_write_byte(pchg, LP8727_CHGCTRL2, val);
412 }
413 }
414
415 static int lp8727_register_psy(struct lp8727_chg *pchg)
416 {
417 struct lp8727_psy *psy;
418
419 psy = devm_kzalloc(pchg->dev, sizeof(*psy), GFP_KERNEL);
420 if (!psy)
421 return -ENOMEM;
422
423 pchg->psy = psy;
424
425 psy->ac.name = "ac";
426 psy->ac.type = POWER_SUPPLY_TYPE_MAINS;
427 psy->ac.properties = lp8727_charger_prop;
428 psy->ac.num_properties = ARRAY_SIZE(lp8727_charger_prop);
429 psy->ac.get_property = lp8727_charger_get_property;
430 psy->ac.supplied_to = battery_supplied_to;
431 psy->ac.num_supplicants = ARRAY_SIZE(battery_supplied_to);
432
433 if (power_supply_register(pchg->dev, &psy->ac))
434 goto err_psy_ac;
435
436 psy->usb.name = "usb";
437 psy->usb.type = POWER_SUPPLY_TYPE_USB;
438 psy->usb.properties = lp8727_charger_prop;
439 psy->usb.num_properties = ARRAY_SIZE(lp8727_charger_prop);
440 psy->usb.get_property = lp8727_charger_get_property;
441 psy->usb.supplied_to = battery_supplied_to;
442 psy->usb.num_supplicants = ARRAY_SIZE(battery_supplied_to);
443
444 if (power_supply_register(pchg->dev, &psy->usb))
445 goto err_psy_usb;
446
447 psy->batt.name = "main_batt";
448 psy->batt.type = POWER_SUPPLY_TYPE_BATTERY;
449 psy->batt.properties = lp8727_battery_prop;
450 psy->batt.num_properties = ARRAY_SIZE(lp8727_battery_prop);
451 psy->batt.get_property = lp8727_battery_get_property;
452 psy->batt.external_power_changed = lp8727_charger_changed;
453
454 if (power_supply_register(pchg->dev, &psy->batt))
455 goto err_psy_batt;
456
457 return 0;
458
459 err_psy_batt:
460 power_supply_unregister(&psy->usb);
461 err_psy_usb:
462 power_supply_unregister(&psy->ac);
463 err_psy_ac:
464 return -EPERM;
465 }
466
467 static void lp8727_unregister_psy(struct lp8727_chg *pchg)
468 {
469 struct lp8727_psy *psy = pchg->psy;
470
471 if (!psy)
472 return;
473
474 power_supply_unregister(&psy->ac);
475 power_supply_unregister(&psy->usb);
476 power_supply_unregister(&psy->batt);
477 }
478
479 static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id)
480 {
481 struct lp8727_chg *pchg;
482 int ret;
483
484 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
485 return -EIO;
486
487 pchg = devm_kzalloc(&cl->dev, sizeof(*pchg), GFP_KERNEL);
488 if (!pchg)
489 return -ENOMEM;
490
491 pchg->client = cl;
492 pchg->dev = &cl->dev;
493 pchg->pdata = cl->dev.platform_data;
494 i2c_set_clientdata(cl, pchg);
495
496 mutex_init(&pchg->xfer_lock);
497
498 ret = lp8727_init_device(pchg);
499 if (ret) {
500 dev_err(pchg->dev, "i2c communication err: %d", ret);
501 return ret;
502 }
503
504 ret = lp8727_register_psy(pchg);
505 if (ret) {
506 dev_err(pchg->dev, "power supplies register err: %d", ret);
507 return ret;
508 }
509
510 ret = lp8727_setup_irq(pchg);
511 if (ret) {
512 dev_err(pchg->dev, "irq handler err: %d", ret);
513 lp8727_unregister_psy(pchg);
514 return ret;
515 }
516
517 return 0;
518 }
519
520 static int __devexit lp8727_remove(struct i2c_client *cl)
521 {
522 struct lp8727_chg *pchg = i2c_get_clientdata(cl);
523
524 lp8727_release_irq(pchg);
525 lp8727_unregister_psy(pchg);
526 return 0;
527 }
528
529 static const struct i2c_device_id lp8727_ids[] = {
530 {"lp8727", 0},
531 { }
532 };
533 MODULE_DEVICE_TABLE(i2c, lp8727_ids);
534
535 static struct i2c_driver lp8727_driver = {
536 .driver = {
537 .name = "lp8727",
538 },
539 .probe = lp8727_probe,
540 .remove = __devexit_p(lp8727_remove),
541 .id_table = lp8727_ids,
542 };
543 module_i2c_driver(lp8727_driver);
544
545 MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
546 MODULE_AUTHOR("Woogyom Kim <milo.kim@ti.com>, "
547 "Daniel Jeong <daniel.jeong@ti.com>");
548 MODULE_LICENSE("GPL");
This page took 0.056581 seconds and 5 git commands to generate.