2 * Driver for LP8727 Micro/Mini USB IC with intergrated charger
4 * Copyright (C) 2011 Texas Instruments
5 * Copyright (C) 2011 National Semiconductor
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.
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>
20 #define DEBOUNCE_MSEC 270
33 #define CP_EN (1 << 0)
34 #define ADC_EN (1 << 1)
35 #define ID200_EN (1 << 4)
38 #define CHGDET_EN (1 << 1)
39 #define INT_EN (1 << 6)
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)
50 #define IDNO (0xF << 0)
53 /* STATUS1 register */
54 #define CHGSTAT (3 << 4)
55 #define CHPORT (1 << 6)
56 #define DCPORT (1 << 7)
58 /* STATUS2 register */
59 #define TEMP_STAT (3 << 5)
70 enum lp8727_chg_stat
{
78 struct power_supply ac
;
79 struct power_supply usb
;
80 struct power_supply batt
;
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
;
95 static int lp8727_i2c_read(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
, u8 len
)
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
);
103 return (ret
!= len
) ? -EIO
: 0;
106 static int lp8727_i2c_write(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
, u8 len
)
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
);
117 static inline int lp8727_i2c_read_byte(struct lp8727_chg
*pchg
, u8 reg
,
120 return lp8727_i2c_read(pchg
, reg
, data
, 1);
123 static inline int lp8727_i2c_write_byte(struct lp8727_chg
*pchg
, u8 reg
,
126 return lp8727_i2c_write(pchg
, reg
, data
, 1);
129 static int lp8727_is_charger_attached(const char *name
, int id
)
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;
138 return (id
>= ID_TA
&& id
<= ID_USB_CHG
) ? 1 : 0;
141 static void lp8727_init_device(struct lp8727_chg
*pchg
)
145 val
= ID200_EN
| ADC_EN
| CP_EN
;
146 if (lp8727_i2c_write_byte(pchg
, CTRL1
, &val
))
147 dev_err(pchg
->dev
, "i2c write err : addr=0x%.2x\n", CTRL1
);
149 val
= INT_EN
| CHGDET_EN
;
150 if (lp8727_i2c_write_byte(pchg
, CTRL2
, &val
))
151 dev_err(pchg
->dev
, "i2c write err : addr=0x%.2x\n", CTRL2
);
154 static int lp8727_is_dedicated_charger(struct lp8727_chg
*pchg
)
157 lp8727_i2c_read_byte(pchg
, STATUS1
, &val
);
158 return (val
& DCPORT
);
161 static int lp8727_is_usb_charger(struct lp8727_chg
*pchg
)
164 lp8727_i2c_read_byte(pchg
, STATUS1
, &val
);
165 return (val
& CHPORT
);
168 static void lp8727_ctrl_switch(struct lp8727_chg
*pchg
, u8 sw
)
171 lp8727_i2c_write_byte(pchg
, SWCTRL
, &val
);
174 static void lp8727_id_detection(struct lp8727_chg
*pchg
, u8 id
, int vbusin
)
177 u8 swctrl
= SW_DM1_HiZ
| SW_DP2_HiZ
;
182 pchg
->chg_parm
= &pchg
->pdata
->ac
;
185 if (lp8727_is_dedicated_charger(pchg
)) {
186 pchg
->chg_parm
= &pchg
->pdata
->ac
;
187 devid
= ID_DEDICATED_CHG
;
188 } else if (lp8727_is_usb_charger(pchg
)) {
189 pchg
->chg_parm
= &pchg
->pdata
->usb
;
191 swctrl
= SW_DM1_DM
| SW_DP2_DP
;
194 swctrl
= SW_DM1_DM
| SW_DP2_DP
;
199 pchg
->chg_parm
= NULL
;
204 lp8727_ctrl_switch(pchg
, swctrl
);
207 static void lp8727_enable_chgdet(struct lp8727_chg
*pchg
)
211 lp8727_i2c_read_byte(pchg
, CTRL2
, &val
);
213 lp8727_i2c_write_byte(pchg
, CTRL2
, &val
);
216 static void lp8727_delayed_func(struct work_struct
*_work
)
218 u8 intstat
[2], idno
, vbus
;
219 struct lp8727_chg
*pchg
=
220 container_of(_work
, struct lp8727_chg
, work
.work
);
222 if (lp8727_i2c_read(pchg
, INT1
, intstat
, 2)) {
223 dev_err(pchg
->dev
, "can not read INT registers\n");
227 idno
= intstat
[0] & IDNO
;
228 vbus
= intstat
[0] & VBUS
;
230 lp8727_id_detection(pchg
, idno
, vbus
);
231 lp8727_enable_chgdet(pchg
);
233 power_supply_changed(&pchg
->psy
->ac
);
234 power_supply_changed(&pchg
->psy
->usb
);
235 power_supply_changed(&pchg
->psy
->batt
);
238 static irqreturn_t
lp8727_isr_func(int irq
, void *ptr
)
240 struct lp8727_chg
*pchg
= ptr
;
241 unsigned long delay
= msecs_to_jiffies(DEBOUNCE_MSEC
);
243 queue_delayed_work(pchg
->irqthread
, &pchg
->work
, delay
);
248 static void lp8727_intr_config(struct lp8727_chg
*pchg
)
250 INIT_DELAYED_WORK(&pchg
->work
, lp8727_delayed_func
);
252 pchg
->irqthread
= create_singlethread_workqueue("lp8727-irqthd");
253 if (!pchg
->irqthread
)
254 dev_err(pchg
->dev
, "can not create thread for lp8727\n");
256 if (request_threaded_irq(pchg
->client
->irq
,
259 IRQF_TRIGGER_FALLING
, "lp8727_irq", pchg
)) {
260 dev_err(pchg
->dev
, "lp8727 irq can not be registered\n");
264 static enum power_supply_property lp8727_charger_prop
[] = {
265 POWER_SUPPLY_PROP_ONLINE
,
268 static enum power_supply_property lp8727_battery_prop
[] = {
269 POWER_SUPPLY_PROP_STATUS
,
270 POWER_SUPPLY_PROP_HEALTH
,
271 POWER_SUPPLY_PROP_PRESENT
,
272 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
273 POWER_SUPPLY_PROP_CAPACITY
,
274 POWER_SUPPLY_PROP_TEMP
,
277 static char *battery_supplied_to
[] = {
281 static int lp8727_charger_get_property(struct power_supply
*psy
,
282 enum power_supply_property psp
,
283 union power_supply_propval
*val
)
285 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
287 if (psp
== POWER_SUPPLY_PROP_ONLINE
)
288 val
->intval
= lp8727_is_charger_attached(psy
->name
,
294 static int lp8727_battery_get_property(struct power_supply
*psy
,
295 enum power_supply_property psp
,
296 union power_supply_propval
*val
)
298 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
302 case POWER_SUPPLY_PROP_STATUS
:
303 if (lp8727_is_charger_attached(psy
->name
, pchg
->devid
)) {
304 lp8727_i2c_read_byte(pchg
, STATUS1
, &read
);
305 if (((read
& CHGSTAT
) >> 4) == EOC
)
306 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
308 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
310 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
313 case POWER_SUPPLY_PROP_HEALTH
:
314 lp8727_i2c_read_byte(pchg
, STATUS2
, &read
);
315 read
= (read
& TEMP_STAT
) >> 5;
316 if (read
>= 0x1 && read
<= 0x3)
317 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
319 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
321 case POWER_SUPPLY_PROP_PRESENT
:
322 if (pchg
->pdata
->get_batt_present
)
323 val
->intval
= pchg
->pdata
->get_batt_present();
325 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
326 if (pchg
->pdata
->get_batt_level
)
327 val
->intval
= pchg
->pdata
->get_batt_level();
329 case POWER_SUPPLY_PROP_CAPACITY
:
330 if (pchg
->pdata
->get_batt_capacity
)
331 val
->intval
= pchg
->pdata
->get_batt_capacity();
333 case POWER_SUPPLY_PROP_TEMP
:
334 if (pchg
->pdata
->get_batt_temp
)
335 val
->intval
= pchg
->pdata
->get_batt_temp();
344 static void lp8727_charger_changed(struct power_supply
*psy
)
346 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
350 if (lp8727_is_charger_attached(psy
->name
, pchg
->devid
)) {
351 if (pchg
->chg_parm
) {
352 eoc_level
= pchg
->chg_parm
->eoc_level
;
353 ichg
= pchg
->chg_parm
->ichg
;
354 val
= (ichg
<< 4) | eoc_level
;
355 lp8727_i2c_write_byte(pchg
, CHGCTRL2
, &val
);
360 static int lp8727_register_psy(struct lp8727_chg
*pchg
)
362 struct lp8727_psy
*psy
;
364 psy
= kzalloc(sizeof(*psy
), GFP_KERNEL
);
371 psy
->ac
.type
= POWER_SUPPLY_TYPE_MAINS
;
372 psy
->ac
.properties
= lp8727_charger_prop
;
373 psy
->ac
.num_properties
= ARRAY_SIZE(lp8727_charger_prop
);
374 psy
->ac
.get_property
= lp8727_charger_get_property
;
375 psy
->ac
.supplied_to
= battery_supplied_to
;
376 psy
->ac
.num_supplicants
= ARRAY_SIZE(battery_supplied_to
);
378 if (power_supply_register(pchg
->dev
, &psy
->ac
))
381 psy
->usb
.name
= "usb";
382 psy
->usb
.type
= POWER_SUPPLY_TYPE_USB
;
383 psy
->usb
.properties
= lp8727_charger_prop
;
384 psy
->usb
.num_properties
= ARRAY_SIZE(lp8727_charger_prop
);
385 psy
->usb
.get_property
= lp8727_charger_get_property
;
386 psy
->usb
.supplied_to
= battery_supplied_to
;
387 psy
->usb
.num_supplicants
= ARRAY_SIZE(battery_supplied_to
);
389 if (power_supply_register(pchg
->dev
, &psy
->usb
))
392 psy
->batt
.name
= "main_batt";
393 psy
->batt
.type
= POWER_SUPPLY_TYPE_BATTERY
;
394 psy
->batt
.properties
= lp8727_battery_prop
;
395 psy
->batt
.num_properties
= ARRAY_SIZE(lp8727_battery_prop
);
396 psy
->batt
.get_property
= lp8727_battery_get_property
;
397 psy
->batt
.external_power_changed
= lp8727_charger_changed
;
399 if (power_supply_register(pchg
->dev
, &psy
->batt
))
411 static void lp8727_unregister_psy(struct lp8727_chg
*pchg
)
413 struct lp8727_psy
*psy
= pchg
->psy
;
418 power_supply_unregister(&psy
->ac
);
419 power_supply_unregister(&psy
->usb
);
420 power_supply_unregister(&psy
->batt
);
424 static int lp8727_probe(struct i2c_client
*cl
, const struct i2c_device_id
*id
)
426 struct lp8727_chg
*pchg
;
429 if (!i2c_check_functionality(cl
->adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
))
432 pchg
= kzalloc(sizeof(*pchg
), GFP_KERNEL
);
437 pchg
->dev
= &cl
->dev
;
438 pchg
->pdata
= cl
->dev
.platform_data
;
439 i2c_set_clientdata(cl
, pchg
);
441 mutex_init(&pchg
->xfer_lock
);
443 lp8727_init_device(pchg
);
444 lp8727_intr_config(pchg
);
446 ret
= lp8727_register_psy(pchg
);
449 "can not register power supplies. err=%d", ret
);
454 static int __devexit
lp8727_remove(struct i2c_client
*cl
)
456 struct lp8727_chg
*pchg
= i2c_get_clientdata(cl
);
458 lp8727_unregister_psy(pchg
);
459 free_irq(pchg
->client
->irq
, pchg
);
460 flush_workqueue(pchg
->irqthread
);
461 destroy_workqueue(pchg
->irqthread
);
466 static const struct i2c_device_id lp8727_ids
[] = {
471 static struct i2c_driver lp8727_driver
= {
475 .probe
= lp8727_probe
,
476 .remove
= __devexit_p(lp8727_remove
),
477 .id_table
= lp8727_ids
,
480 static int __init
lp8727_init(void)
482 return i2c_add_driver(&lp8727_driver
);
485 static void __exit
lp8727_exit(void)
487 i2c_del_driver(&lp8727_driver
);
490 module_init(lp8727_init
);
491 module_exit(lp8727_exit
);
493 MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
495 ("Woogyom Kim <milo.kim@ti.com>, Daniel Jeong <daniel.jeong@ti.com>");
496 MODULE_LICENSE("GPL");