2 * Driver for LP8727 Micro/Mini USB IC with intergrated charger
4 * Copyright (C) 2011 National Semiconductor
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/interrupt.h>
15 #include <linux/i2c.h>
16 #include <linux/power_supply.h>
17 #include <linux/lp8727.h>
19 #define DEBOUNCE_MSEC 270
32 #define CP_EN (1 << 0)
33 #define ADC_EN (1 << 1)
34 #define ID200_EN (1 << 4)
37 #define CHGDET_EN (1 << 1)
38 #define INT_EN (1 << 6)
41 #define SW_DM1_DM (0x0 << 0)
42 #define SW_DM1_U1 (0x1 << 0)
43 #define SW_DM1_HiZ (0x7 << 0)
44 #define SW_DP2_DP (0x0 << 3)
45 #define SW_DP2_U2 (0x1 << 3)
46 #define SW_DP2_HiZ (0x7 << 3)
49 #define IDNO (0xF << 0)
52 /* STATUS1 register */
53 #define CHGSTAT (3 << 4)
54 #define CHPORT (1 << 6)
55 #define DCPORT (1 << 7)
57 /* STATUS2 register */
58 #define TEMP_STAT (3 << 5)
69 enum lp8727_chg_stat
{
77 struct power_supply ac
;
78 struct power_supply usb
;
79 struct power_supply batt
;
84 struct i2c_client
*client
;
85 struct mutex xfer_lock
;
86 struct delayed_work work
;
87 struct workqueue_struct
*irqthread
;
88 struct lp8727_platform_data
*pdata
;
89 struct lp8727_psy
*psy
;
90 struct lp8727_chg_param
*chg_parm
;
91 enum lp8727_dev_id devid
;
94 static int lp8727_i2c_read(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
, u8 len
)
98 mutex_lock(&pchg
->xfer_lock
);
99 ret
= i2c_smbus_read_i2c_block_data(pchg
->client
, reg
, len
, data
);
100 mutex_unlock(&pchg
->xfer_lock
);
102 return (ret
!= len
) ? -EIO
: 0;
105 static int lp8727_i2c_write(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
, u8 len
)
109 mutex_lock(&pchg
->xfer_lock
);
110 ret
= i2c_smbus_write_i2c_block_data(pchg
->client
, reg
, len
, data
);
111 mutex_unlock(&pchg
->xfer_lock
);
116 static inline int lp8727_i2c_read_byte(struct lp8727_chg
*pchg
, u8 reg
,
119 return lp8727_i2c_read(pchg
, reg
, data
, 1);
122 static inline int lp8727_i2c_write_byte(struct lp8727_chg
*pchg
, u8 reg
,
125 return lp8727_i2c_write(pchg
, reg
, data
, 1);
128 static int lp8727_is_charger_attached(const char *name
, int id
)
131 if (!strcmp(name
, "ac"))
132 return (id
== ID_TA
|| id
== ID_DEDICATED_CHG
) ? 1 : 0;
133 else if (!strcmp(name
, "usb"))
134 return (id
== ID_USB_CHG
) ? 1 : 0;
137 return (id
>= ID_TA
&& id
<= ID_USB_CHG
) ? 1 : 0;
140 static void lp8727_init_device(struct lp8727_chg
*pchg
)
144 val
= ID200_EN
| ADC_EN
| CP_EN
;
145 if (lp8727_i2c_write_byte(pchg
, CTRL1
, &val
))
146 dev_err(pchg
->dev
, "i2c write err : addr=0x%.2x\n", CTRL1
);
148 val
= INT_EN
| CHGDET_EN
;
149 if (lp8727_i2c_write_byte(pchg
, CTRL2
, &val
))
150 dev_err(pchg
->dev
, "i2c write err : addr=0x%.2x\n", CTRL2
);
153 static int lp8727_is_dedicated_charger(struct lp8727_chg
*pchg
)
156 lp8727_i2c_read_byte(pchg
, STATUS1
, &val
);
157 return (val
& DCPORT
);
160 static int lp8727_is_usb_charger(struct lp8727_chg
*pchg
)
163 lp8727_i2c_read_byte(pchg
, STATUS1
, &val
);
164 return (val
& CHPORT
);
167 static void lp8727_ctrl_switch(struct lp8727_chg
*pchg
, u8 sw
)
170 lp8727_i2c_write_byte(pchg
, SWCTRL
, &val
);
173 static void lp8727_id_detection(struct lp8727_chg
*pchg
, u8 id
, int vbusin
)
176 u8 swctrl
= SW_DM1_HiZ
| SW_DP2_HiZ
;
181 pchg
->chg_parm
= &pchg
->pdata
->ac
;
184 if (lp8727_is_dedicated_charger(pchg
)) {
185 pchg
->chg_parm
= &pchg
->pdata
->ac
;
186 devid
= ID_DEDICATED_CHG
;
187 } else if (lp8727_is_usb_charger(pchg
)) {
188 pchg
->chg_parm
= &pchg
->pdata
->usb
;
190 swctrl
= SW_DM1_DM
| SW_DP2_DP
;
193 swctrl
= SW_DM1_DM
| SW_DP2_DP
;
198 pchg
->chg_parm
= NULL
;
203 lp8727_ctrl_switch(pchg
, swctrl
);
206 static void lp8727_enable_chgdet(struct lp8727_chg
*pchg
)
210 lp8727_i2c_read_byte(pchg
, CTRL2
, &val
);
212 lp8727_i2c_write_byte(pchg
, CTRL2
, &val
);
215 static void lp8727_delayed_func(struct work_struct
*_work
)
217 u8 intstat
[2], idno
, vbus
;
218 struct lp8727_chg
*pchg
=
219 container_of(_work
, struct lp8727_chg
, work
.work
);
221 if (lp8727_i2c_read(pchg
, INT1
, intstat
, 2)) {
222 dev_err(pchg
->dev
, "can not read INT registers\n");
226 idno
= intstat
[0] & IDNO
;
227 vbus
= intstat
[0] & VBUS
;
229 lp8727_id_detection(pchg
, idno
, vbus
);
230 lp8727_enable_chgdet(pchg
);
232 power_supply_changed(&pchg
->psy
->ac
);
233 power_supply_changed(&pchg
->psy
->usb
);
234 power_supply_changed(&pchg
->psy
->batt
);
237 static irqreturn_t
lp8727_isr_func(int irq
, void *ptr
)
239 struct lp8727_chg
*pchg
= ptr
;
240 unsigned long delay
= msecs_to_jiffies(DEBOUNCE_MSEC
);
242 queue_delayed_work(pchg
->irqthread
, &pchg
->work
, delay
);
247 static void lp8727_intr_config(struct lp8727_chg
*pchg
)
249 INIT_DELAYED_WORK(&pchg
->work
, lp8727_delayed_func
);
251 pchg
->irqthread
= create_singlethread_workqueue("lp8727-irqthd");
252 if (!pchg
->irqthread
)
253 dev_err(pchg
->dev
, "can not create thread for lp8727\n");
255 if (request_threaded_irq(pchg
->client
->irq
,
258 IRQF_TRIGGER_FALLING
, "lp8727_irq", pchg
)) {
259 dev_err(pchg
->dev
, "lp8727 irq can not be registered\n");
263 static enum power_supply_property lp8727_charger_prop
[] = {
264 POWER_SUPPLY_PROP_ONLINE
,
267 static 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
,
276 static char *battery_supplied_to
[] = {
280 static int lp8727_charger_get_property(struct power_supply
*psy
,
281 enum power_supply_property psp
,
282 union power_supply_propval
*val
)
284 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
286 if (psp
== POWER_SUPPLY_PROP_ONLINE
)
287 val
->intval
= lp8727_is_charger_attached(psy
->name
,
293 static int lp8727_battery_get_property(struct power_supply
*psy
,
294 enum power_supply_property psp
,
295 union power_supply_propval
*val
)
297 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
301 case POWER_SUPPLY_PROP_STATUS
:
302 if (lp8727_is_charger_attached(psy
->name
, pchg
->devid
)) {
303 lp8727_i2c_read_byte(pchg
, STATUS1
, &read
);
304 if (((read
& CHGSTAT
) >> 4) == EOC
)
305 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
307 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
309 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
312 case POWER_SUPPLY_PROP_HEALTH
:
313 lp8727_i2c_read_byte(pchg
, STATUS2
, &read
);
314 read
= (read
& TEMP_STAT
) >> 5;
315 if (read
>= 0x1 && read
<= 0x3)
316 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
318 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
320 case POWER_SUPPLY_PROP_PRESENT
:
321 if (pchg
->pdata
->get_batt_present
)
322 val
->intval
= pchg
->pdata
->get_batt_present();
324 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
325 if (pchg
->pdata
->get_batt_level
)
326 val
->intval
= pchg
->pdata
->get_batt_level();
328 case POWER_SUPPLY_PROP_CAPACITY
:
329 if (pchg
->pdata
->get_batt_capacity
)
330 val
->intval
= pchg
->pdata
->get_batt_capacity();
332 case POWER_SUPPLY_PROP_TEMP
:
333 if (pchg
->pdata
->get_batt_temp
)
334 val
->intval
= pchg
->pdata
->get_batt_temp();
343 static void lp8727_charger_changed(struct power_supply
*psy
)
345 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
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
;
354 lp8727_i2c_write_byte(pchg
, CHGCTRL2
, &val
);
359 static int lp8727_register_psy(struct lp8727_chg
*pchg
)
361 struct lp8727_psy
*psy
;
363 psy
= kzalloc(sizeof(*psy
), GFP_KERNEL
);
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
);
377 if (power_supply_register(pchg
->dev
, &psy
->ac
))
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
);
388 if (power_supply_register(pchg
->dev
, &psy
->usb
))
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
;
398 if (power_supply_register(pchg
->dev
, &psy
->batt
))
410 static void lp8727_unregister_psy(struct lp8727_chg
*pchg
)
412 struct lp8727_psy
*psy
= pchg
->psy
;
417 power_supply_unregister(&psy
->ac
);
418 power_supply_unregister(&psy
->usb
);
419 power_supply_unregister(&psy
->batt
);
423 static int lp8727_probe(struct i2c_client
*cl
, const struct i2c_device_id
*id
)
425 struct lp8727_chg
*pchg
;
428 if (!i2c_check_functionality(cl
->adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
))
431 pchg
= kzalloc(sizeof(*pchg
), GFP_KERNEL
);
436 pchg
->dev
= &cl
->dev
;
437 pchg
->pdata
= cl
->dev
.platform_data
;
438 i2c_set_clientdata(cl
, pchg
);
440 mutex_init(&pchg
->xfer_lock
);
442 lp8727_init_device(pchg
);
443 lp8727_intr_config(pchg
);
445 ret
= lp8727_register_psy(pchg
);
448 "can not register power supplies. err=%d", ret
);
453 static int __devexit
lp8727_remove(struct i2c_client
*cl
)
455 struct lp8727_chg
*pchg
= i2c_get_clientdata(cl
);
457 lp8727_unregister_psy(pchg
);
458 free_irq(pchg
->client
->irq
, pchg
);
459 flush_workqueue(pchg
->irqthread
);
460 destroy_workqueue(pchg
->irqthread
);
465 static const struct i2c_device_id lp8727_ids
[] = {
470 static struct i2c_driver lp8727_driver
= {
474 .probe
= lp8727_probe
,
475 .remove
= __devexit_p(lp8727_remove
),
476 .id_table
= lp8727_ids
,
479 static int __init
lp8727_init(void)
481 return i2c_add_driver(&lp8727_driver
);
484 static void __exit
lp8727_exit(void)
486 i2c_del_driver(&lp8727_driver
);
489 module_init(lp8727_init
);
490 module_exit(lp8727_exit
);
492 MODULE_DESCRIPTION("National Semiconductor LP8727 charger driver");
494 ("Woogyom Kim <milo.kim@ti.com>, Daniel Jeong <daniel.jeong@ti.com>");
495 MODULE_LICENSE("GPL");