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
34 #define CP_EN (1 << 0)
35 #define ADC_EN (1 << 1)
36 #define ID200_EN (1 << 4)
39 #define CHGDET_EN (1 << 1)
40 #define INT_EN (1 << 6)
43 #define SW_DM1_DM (0x0 << 0)
44 #define SW_DM1_U1 (0x1 << 0)
45 #define SW_DM1_HiZ (0x7 << 0)
46 #define SW_DP2_DP (0x0 << 3)
47 #define SW_DP2_U2 (0x1 << 3)
48 #define SW_DP2_HiZ (0x7 << 3)
51 #define IDNO (0xF << 0)
54 /* STATUS1 register */
55 #define CHGSTAT (3 << 4)
56 #define CHPORT (1 << 6)
57 #define DCPORT (1 << 7)
59 /* STATUS2 register */
60 #define TEMP_STAT (3 << 5)
71 enum lp8727_chg_stat
{
79 struct power_supply ac
;
80 struct power_supply usb
;
81 struct power_supply batt
;
86 struct i2c_client
*client
;
87 struct mutex xfer_lock
;
88 struct delayed_work work
;
89 struct lp8727_platform_data
*pdata
;
90 struct lp8727_psy
*psy
;
91 struct lp8727_chg_param
*chg_parm
;
92 enum lp8727_dev_id devid
;
93 unsigned long debounce_jiffies
;
97 static int lp8727_read_bytes(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
, u8 len
)
101 mutex_lock(&pchg
->xfer_lock
);
102 ret
= i2c_smbus_read_i2c_block_data(pchg
->client
, reg
, len
, data
);
103 mutex_unlock(&pchg
->xfer_lock
);
105 return (ret
!= len
) ? -EIO
: 0;
108 static inline int lp8727_read_byte(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
)
110 return lp8727_read_bytes(pchg
, reg
, data
, 1);
113 static int lp8727_write_byte(struct lp8727_chg
*pchg
, u8 reg
, u8 data
)
117 mutex_lock(&pchg
->xfer_lock
);
118 ret
= i2c_smbus_write_byte_data(pchg
->client
, reg
, data
);
119 mutex_unlock(&pchg
->xfer_lock
);
124 static int lp8727_is_charger_attached(const char *name
, int id
)
127 if (!strcmp(name
, "ac"))
128 return (id
== ID_TA
|| id
== ID_DEDICATED_CHG
) ? 1 : 0;
129 else if (!strcmp(name
, "usb"))
130 return (id
== ID_USB_CHG
) ? 1 : 0;
133 return (id
>= ID_TA
&& id
<= ID_USB_CHG
) ? 1 : 0;
136 static int lp8727_init_device(struct lp8727_chg
*pchg
)
140 u8 intstat
[LP8788_NUM_INTREGS
];
142 /* clear interrupts */
143 ret
= lp8727_read_bytes(pchg
, INT1
, intstat
, LP8788_NUM_INTREGS
);
148 val
= ID200_EN
| ADC_EN
| CP_EN
;
149 ret
= lp8727_write_byte(pchg
, CTRL1
, val
);
153 val
= INT_EN
| CHGDET_EN
;
154 ret
= lp8727_write_byte(pchg
, CTRL2
, val
);
161 static int lp8727_is_dedicated_charger(struct lp8727_chg
*pchg
)
164 lp8727_read_byte(pchg
, STATUS1
, &val
);
168 static int lp8727_is_usb_charger(struct lp8727_chg
*pchg
)
171 lp8727_read_byte(pchg
, STATUS1
, &val
);
175 static void lp8727_ctrl_switch(struct lp8727_chg
*pchg
, u8 sw
)
177 lp8727_write_byte(pchg
, SWCTRL
, sw
);
180 static void lp8727_id_detection(struct lp8727_chg
*pchg
, u8 id
, int vbusin
)
182 struct lp8727_platform_data
*pdata
= pchg
->pdata
;
184 u8 swctrl
= SW_DM1_HiZ
| SW_DP2_HiZ
;
189 pchg
->chg_parm
= pdata
? pdata
->ac
: NULL
;
192 if (lp8727_is_dedicated_charger(pchg
)) {
193 pchg
->chg_parm
= pdata
? pdata
->ac
: NULL
;
194 devid
= ID_DEDICATED_CHG
;
195 } else if (lp8727_is_usb_charger(pchg
)) {
196 pchg
->chg_parm
= pdata
? pdata
->usb
: NULL
;
198 swctrl
= SW_DM1_DM
| SW_DP2_DP
;
201 swctrl
= SW_DM1_DM
| SW_DP2_DP
;
206 pchg
->chg_parm
= NULL
;
211 lp8727_ctrl_switch(pchg
, swctrl
);
214 static void lp8727_enable_chgdet(struct lp8727_chg
*pchg
)
218 lp8727_read_byte(pchg
, CTRL2
, &val
);
220 lp8727_write_byte(pchg
, CTRL2
, val
);
223 static void lp8727_delayed_func(struct work_struct
*_work
)
225 u8 intstat
[2], idno
, vbus
;
226 struct lp8727_chg
*pchg
=
227 container_of(_work
, struct lp8727_chg
, work
.work
);
229 if (lp8727_read_bytes(pchg
, INT1
, intstat
, 2)) {
230 dev_err(pchg
->dev
, "can not read INT registers\n");
234 idno
= intstat
[0] & IDNO
;
235 vbus
= intstat
[0] & VBUS
;
237 lp8727_id_detection(pchg
, idno
, vbus
);
238 lp8727_enable_chgdet(pchg
);
240 power_supply_changed(&pchg
->psy
->ac
);
241 power_supply_changed(&pchg
->psy
->usb
);
242 power_supply_changed(&pchg
->psy
->batt
);
245 static irqreturn_t
lp8727_isr_func(int irq
, void *ptr
)
247 struct lp8727_chg
*pchg
= ptr
;
249 schedule_delayed_work(&pchg
->work
, pchg
->debounce_jiffies
);
253 static int lp8727_setup_irq(struct lp8727_chg
*pchg
)
256 int irq
= pchg
->client
->irq
;
257 unsigned delay_msec
= pchg
->pdata
? pchg
->pdata
->debounce_msec
:
258 DEFAULT_DEBOUNCE_MSEC
;
260 INIT_DELAYED_WORK(&pchg
->work
, lp8727_delayed_func
);
263 dev_warn(pchg
->dev
, "invalid irq number: %d\n", irq
);
267 ret
= request_threaded_irq(irq
, NULL
, lp8727_isr_func
,
268 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
275 pchg
->debounce_jiffies
= msecs_to_jiffies(delay_msec
);
280 static void lp8727_release_irq(struct lp8727_chg
*pchg
)
282 cancel_delayed_work_sync(&pchg
->work
);
285 free_irq(pchg
->irq
, pchg
);
288 static enum power_supply_property lp8727_charger_prop
[] = {
289 POWER_SUPPLY_PROP_ONLINE
,
292 static enum power_supply_property lp8727_battery_prop
[] = {
293 POWER_SUPPLY_PROP_STATUS
,
294 POWER_SUPPLY_PROP_HEALTH
,
295 POWER_SUPPLY_PROP_PRESENT
,
296 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
297 POWER_SUPPLY_PROP_CAPACITY
,
298 POWER_SUPPLY_PROP_TEMP
,
301 static char *battery_supplied_to
[] = {
305 static int lp8727_charger_get_property(struct power_supply
*psy
,
306 enum power_supply_property psp
,
307 union power_supply_propval
*val
)
309 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
311 if (psp
== POWER_SUPPLY_PROP_ONLINE
)
312 val
->intval
= lp8727_is_charger_attached(psy
->name
,
318 static int lp8727_battery_get_property(struct power_supply
*psy
,
319 enum power_supply_property psp
,
320 union power_supply_propval
*val
)
322 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
323 struct lp8727_platform_data
*pdata
= pchg
->pdata
;
327 case POWER_SUPPLY_PROP_STATUS
:
328 if (lp8727_is_charger_attached(psy
->name
, pchg
->devid
)) {
329 lp8727_read_byte(pchg
, STATUS1
, &read
);
330 if (((read
& CHGSTAT
) >> 4) == EOC
)
331 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
333 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
335 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
338 case POWER_SUPPLY_PROP_HEALTH
:
339 lp8727_read_byte(pchg
, STATUS2
, &read
);
340 read
= (read
& TEMP_STAT
) >> 5;
341 if (read
>= 0x1 && read
<= 0x3)
342 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
344 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
346 case POWER_SUPPLY_PROP_PRESENT
:
350 if (pdata
->get_batt_present
)
351 val
->intval
= pchg
->pdata
->get_batt_present();
353 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
357 if (pdata
->get_batt_level
)
358 val
->intval
= pchg
->pdata
->get_batt_level();
360 case POWER_SUPPLY_PROP_CAPACITY
:
364 if (pdata
->get_batt_capacity
)
365 val
->intval
= pchg
->pdata
->get_batt_capacity();
367 case POWER_SUPPLY_PROP_TEMP
:
371 if (pdata
->get_batt_temp
)
372 val
->intval
= pchg
->pdata
->get_batt_temp();
381 static void lp8727_charger_changed(struct power_supply
*psy
)
383 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
387 if (lp8727_is_charger_attached(psy
->name
, pchg
->devid
)) {
388 if (pchg
->chg_parm
) {
389 eoc_level
= pchg
->chg_parm
->eoc_level
;
390 ichg
= pchg
->chg_parm
->ichg
;
391 val
= (ichg
<< 4) | eoc_level
;
392 lp8727_write_byte(pchg
, CHGCTRL2
, val
);
397 static int lp8727_register_psy(struct lp8727_chg
*pchg
)
399 struct lp8727_psy
*psy
;
401 psy
= devm_kzalloc(pchg
->dev
, sizeof(*psy
), GFP_KERNEL
);
408 psy
->ac
.type
= POWER_SUPPLY_TYPE_MAINS
;
409 psy
->ac
.properties
= lp8727_charger_prop
;
410 psy
->ac
.num_properties
= ARRAY_SIZE(lp8727_charger_prop
);
411 psy
->ac
.get_property
= lp8727_charger_get_property
;
412 psy
->ac
.supplied_to
= battery_supplied_to
;
413 psy
->ac
.num_supplicants
= ARRAY_SIZE(battery_supplied_to
);
415 if (power_supply_register(pchg
->dev
, &psy
->ac
))
418 psy
->usb
.name
= "usb";
419 psy
->usb
.type
= POWER_SUPPLY_TYPE_USB
;
420 psy
->usb
.properties
= lp8727_charger_prop
;
421 psy
->usb
.num_properties
= ARRAY_SIZE(lp8727_charger_prop
);
422 psy
->usb
.get_property
= lp8727_charger_get_property
;
423 psy
->usb
.supplied_to
= battery_supplied_to
;
424 psy
->usb
.num_supplicants
= ARRAY_SIZE(battery_supplied_to
);
426 if (power_supply_register(pchg
->dev
, &psy
->usb
))
429 psy
->batt
.name
= "main_batt";
430 psy
->batt
.type
= POWER_SUPPLY_TYPE_BATTERY
;
431 psy
->batt
.properties
= lp8727_battery_prop
;
432 psy
->batt
.num_properties
= ARRAY_SIZE(lp8727_battery_prop
);
433 psy
->batt
.get_property
= lp8727_battery_get_property
;
434 psy
->batt
.external_power_changed
= lp8727_charger_changed
;
436 if (power_supply_register(pchg
->dev
, &psy
->batt
))
442 power_supply_unregister(&psy
->usb
);
444 power_supply_unregister(&psy
->ac
);
449 static void lp8727_unregister_psy(struct lp8727_chg
*pchg
)
451 struct lp8727_psy
*psy
= pchg
->psy
;
456 power_supply_unregister(&psy
->ac
);
457 power_supply_unregister(&psy
->usb
);
458 power_supply_unregister(&psy
->batt
);
461 static int lp8727_probe(struct i2c_client
*cl
, const struct i2c_device_id
*id
)
463 struct lp8727_chg
*pchg
;
466 if (!i2c_check_functionality(cl
->adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
))
469 pchg
= devm_kzalloc(&cl
->dev
, sizeof(*pchg
), GFP_KERNEL
);
474 pchg
->dev
= &cl
->dev
;
475 pchg
->pdata
= cl
->dev
.platform_data
;
476 i2c_set_clientdata(cl
, pchg
);
478 mutex_init(&pchg
->xfer_lock
);
480 ret
= lp8727_init_device(pchg
);
482 dev_err(pchg
->dev
, "i2c communication err: %d", ret
);
486 ret
= lp8727_register_psy(pchg
);
488 dev_err(pchg
->dev
, "power supplies register err: %d", ret
);
492 ret
= lp8727_setup_irq(pchg
);
494 dev_err(pchg
->dev
, "irq handler err: %d", ret
);
495 lp8727_unregister_psy(pchg
);
502 static int __devexit
lp8727_remove(struct i2c_client
*cl
)
504 struct lp8727_chg
*pchg
= i2c_get_clientdata(cl
);
506 lp8727_release_irq(pchg
);
507 lp8727_unregister_psy(pchg
);
511 static const struct i2c_device_id lp8727_ids
[] = {
515 MODULE_DEVICE_TABLE(i2c
, lp8727_ids
);
517 static struct i2c_driver lp8727_driver
= {
521 .probe
= lp8727_probe
,
522 .remove
= __devexit_p(lp8727_remove
),
523 .id_table
= lp8727_ids
,
525 module_i2c_driver(lp8727_driver
);
527 MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
528 MODULE_AUTHOR("Woogyom Kim <milo.kim@ti.com>, "
529 "Daniel Jeong <daniel.jeong@ti.com>");
530 MODULE_LICENSE("GPL");