2 * Driver for LP8727 Micro/Mini USB IC with integrated 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/platform_data/lp8727.h>
20 #define LP8788_NUM_INTREGS 2
21 #define DEFAULT_DEBOUNCE_MSEC 270
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
34 #define LP8727_CP_EN BIT(0)
35 #define LP8727_ADC_EN BIT(1)
36 #define LP8727_ID200_EN BIT(4)
39 #define LP8727_CHGDET_EN BIT(1)
40 #define LP8727_INT_EN BIT(6)
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)
49 #define LP8727_IDNO (0xF << 0)
50 #define LP8727_VBUS BIT(4)
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
58 /* STATUS2 register */
59 #define LP8727_TEMP_STAT (3 << 5)
60 #define LP8727_TEMP_SHIFT 5
62 /* CHGCTRL2 register */
63 #define LP8727_ICHG_SHIFT 4
68 LP8727_ID_DEDICATED_CHG
,
74 enum lp8727_die_temp
{
82 struct power_supply ac
;
83 struct power_supply usb
;
84 struct power_supply batt
;
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
;
100 static int lp8727_read_bytes(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
, u8 len
)
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
);
108 return (ret
!= len
) ? -EIO
: 0;
111 static inline int lp8727_read_byte(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
)
113 return lp8727_read_bytes(pchg
, reg
, data
, 1);
116 static int lp8727_write_byte(struct lp8727_chg
*pchg
, u8 reg
, u8 data
)
120 mutex_lock(&pchg
->xfer_lock
);
121 ret
= i2c_smbus_write_byte_data(pchg
->client
, reg
, data
);
122 mutex_unlock(&pchg
->xfer_lock
);
127 static bool lp8727_is_charger_attached(const char *name
, int id
)
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
;
134 return id
>= LP8727_ID_TA
&& id
<= LP8727_ID_USB_CHG
;
137 static int lp8727_init_device(struct lp8727_chg
*pchg
)
141 u8 intstat
[LP8788_NUM_INTREGS
];
143 /* clear interrupts */
144 ret
= lp8727_read_bytes(pchg
, LP8727_INT1
, intstat
, LP8788_NUM_INTREGS
);
149 val
= LP8727_ID200_EN
| LP8727_ADC_EN
| LP8727_CP_EN
;
150 ret
= lp8727_write_byte(pchg
, LP8727_CTRL1
, val
);
154 val
= LP8727_INT_EN
| LP8727_CHGDET_EN
;
155 return lp8727_write_byte(pchg
, LP8727_CTRL2
, val
);
158 static int lp8727_is_dedicated_charger(struct lp8727_chg
*pchg
)
161 lp8727_read_byte(pchg
, LP8727_STATUS1
, &val
);
162 return val
& LP8727_DCPORT
;
165 static int lp8727_is_usb_charger(struct lp8727_chg
*pchg
)
168 lp8727_read_byte(pchg
, LP8727_STATUS1
, &val
);
169 return val
& LP8727_CHPORT
;
172 static inline void lp8727_ctrl_switch(struct lp8727_chg
*pchg
, u8 sw
)
174 lp8727_write_byte(pchg
, LP8727_SWCTRL
, sw
);
177 static void lp8727_id_detection(struct lp8727_chg
*pchg
, u8 id
, int vbusin
)
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
;
185 devid
= LP8727_ID_TA
;
186 pchg
->chg_parm
= pdata
? pdata
->ac
: NULL
;
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
;
197 devid
= LP8727_ID_USB_DS
;
198 swctrl
= LP8727_SW_DM1_DM
| LP8727_SW_DP2_DP
;
202 devid
= LP8727_ID_NONE
;
203 pchg
->chg_parm
= NULL
;
208 lp8727_ctrl_switch(pchg
, swctrl
);
211 static void lp8727_enable_chgdet(struct lp8727_chg
*pchg
)
215 lp8727_read_byte(pchg
, LP8727_CTRL2
, &val
);
216 val
|= LP8727_CHGDET_EN
;
217 lp8727_write_byte(pchg
, LP8727_CTRL2
, val
);
220 static void lp8727_delayed_func(struct work_struct
*_work
)
222 struct lp8727_chg
*pchg
= container_of(_work
, struct lp8727_chg
,
224 u8 intstat
[LP8788_NUM_INTREGS
];
228 if (lp8727_read_bytes(pchg
, LP8727_INT1
, intstat
, LP8788_NUM_INTREGS
)) {
229 dev_err(pchg
->dev
, "can not read INT registers\n");
233 idno
= intstat
[0] & LP8727_IDNO
;
234 vbus
= intstat
[0] & LP8727_VBUS
;
236 lp8727_id_detection(pchg
, idno
, vbus
);
237 lp8727_enable_chgdet(pchg
);
239 power_supply_changed(&pchg
->psy
->ac
);
240 power_supply_changed(&pchg
->psy
->usb
);
241 power_supply_changed(&pchg
->psy
->batt
);
244 static irqreturn_t
lp8727_isr_func(int irq
, void *ptr
)
246 struct lp8727_chg
*pchg
= ptr
;
248 schedule_delayed_work(&pchg
->work
, pchg
->debounce_jiffies
);
252 static int lp8727_setup_irq(struct lp8727_chg
*pchg
)
255 int irq
= pchg
->client
->irq
;
256 unsigned delay_msec
= pchg
->pdata
? pchg
->pdata
->debounce_msec
:
257 DEFAULT_DEBOUNCE_MSEC
;
259 INIT_DELAYED_WORK(&pchg
->work
, lp8727_delayed_func
);
262 dev_warn(pchg
->dev
, "invalid irq number: %d\n", irq
);
266 ret
= request_threaded_irq(irq
, NULL
, lp8727_isr_func
,
267 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
274 pchg
->debounce_jiffies
= msecs_to_jiffies(delay_msec
);
279 static void lp8727_release_irq(struct lp8727_chg
*pchg
)
281 cancel_delayed_work_sync(&pchg
->work
);
284 free_irq(pchg
->irq
, pchg
);
287 static enum power_supply_property lp8727_charger_prop
[] = {
288 POWER_SUPPLY_PROP_ONLINE
,
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
,
300 static char *battery_supplied_to
[] = {
304 static int lp8727_charger_get_property(struct power_supply
*psy
,
305 enum power_supply_property psp
,
306 union power_supply_propval
*val
)
308 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
310 if (psp
!= POWER_SUPPLY_PROP_ONLINE
)
313 val
->intval
= lp8727_is_charger_attached(psy
->name
, pchg
->devid
);
318 static bool lp8727_is_high_temperature(enum lp8727_die_temp temp
)
321 case LP8788_TEMP_95C
:
322 case LP8788_TEMP_115C
:
323 case LP8788_TEMP_135C
:
330 static int lp8727_battery_get_property(struct power_supply
*psy
,
331 enum power_supply_property psp
,
332 union power_supply_propval
*val
)
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
;
340 case POWER_SUPPLY_PROP_STATUS
:
341 if (!lp8727_is_charger_attached(psy
->name
, pchg
->devid
)) {
342 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
346 lp8727_read_byte(pchg
, LP8727_STATUS1
, &read
);
348 val
->intval
= (read
& LP8727_CHGSTAT
) == LP8727_STAT_EOC
?
349 POWER_SUPPLY_STATUS_FULL
:
350 POWER_SUPPLY_STATUS_CHARGING
;
352 case POWER_SUPPLY_PROP_HEALTH
:
353 lp8727_read_byte(pchg
, LP8727_STATUS2
, &read
);
354 temp
= (read
& LP8727_TEMP_STAT
) >> LP8727_TEMP_SHIFT
;
356 val
->intval
= lp8727_is_high_temperature(temp
) ?
357 POWER_SUPPLY_HEALTH_OVERHEAT
:
358 POWER_SUPPLY_HEALTH_GOOD
;
360 case POWER_SUPPLY_PROP_PRESENT
:
364 if (pdata
->get_batt_present
)
365 val
->intval
= pchg
->pdata
->get_batt_present();
367 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
371 if (pdata
->get_batt_level
)
372 val
->intval
= pchg
->pdata
->get_batt_level();
374 case POWER_SUPPLY_PROP_CAPACITY
:
378 if (pdata
->get_batt_capacity
)
379 val
->intval
= pchg
->pdata
->get_batt_capacity();
381 case POWER_SUPPLY_PROP_TEMP
:
385 if (pdata
->get_batt_temp
)
386 val
->intval
= pchg
->pdata
->get_batt_temp();
395 static void lp8727_charger_changed(struct power_supply
*psy
)
397 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
402 /* skip if no charger exists */
403 if (!lp8727_is_charger_attached(psy
->name
, pchg
->devid
))
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
);
415 static int lp8727_register_psy(struct lp8727_chg
*pchg
)
417 struct lp8727_psy
*psy
;
419 psy
= devm_kzalloc(pchg
->dev
, sizeof(*psy
), GFP_KERNEL
);
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
);
433 if (power_supply_register(pchg
->dev
, &psy
->ac
))
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
);
444 if (power_supply_register(pchg
->dev
, &psy
->usb
))
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
;
454 if (power_supply_register(pchg
->dev
, &psy
->batt
))
460 power_supply_unregister(&psy
->usb
);
462 power_supply_unregister(&psy
->ac
);
467 static void lp8727_unregister_psy(struct lp8727_chg
*pchg
)
469 struct lp8727_psy
*psy
= pchg
->psy
;
474 power_supply_unregister(&psy
->ac
);
475 power_supply_unregister(&psy
->usb
);
476 power_supply_unregister(&psy
->batt
);
479 static int lp8727_probe(struct i2c_client
*cl
, const struct i2c_device_id
*id
)
481 struct lp8727_chg
*pchg
;
484 if (!i2c_check_functionality(cl
->adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
))
487 pchg
= devm_kzalloc(&cl
->dev
, sizeof(*pchg
), GFP_KERNEL
);
492 pchg
->dev
= &cl
->dev
;
493 pchg
->pdata
= cl
->dev
.platform_data
;
494 i2c_set_clientdata(cl
, pchg
);
496 mutex_init(&pchg
->xfer_lock
);
498 ret
= lp8727_init_device(pchg
);
500 dev_err(pchg
->dev
, "i2c communication err: %d", ret
);
504 ret
= lp8727_register_psy(pchg
);
506 dev_err(pchg
->dev
, "power supplies register err: %d", ret
);
510 ret
= lp8727_setup_irq(pchg
);
512 dev_err(pchg
->dev
, "irq handler err: %d", ret
);
513 lp8727_unregister_psy(pchg
);
520 static int __devexit
lp8727_remove(struct i2c_client
*cl
)
522 struct lp8727_chg
*pchg
= i2c_get_clientdata(cl
);
524 lp8727_release_irq(pchg
);
525 lp8727_unregister_psy(pchg
);
529 static const struct i2c_device_id lp8727_ids
[] = {
533 MODULE_DEVICE_TABLE(i2c
, lp8727_ids
);
535 static struct i2c_driver lp8727_driver
= {
539 .probe
= lp8727_probe
,
540 .remove
= __devexit_p(lp8727_remove
),
541 .id_table
= lp8727_ids
,
543 module_i2c_driver(lp8727_driver
);
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");