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 lp8727_psy
*psy
;
92 struct lp8727_platform_data
*pdata
;
95 enum lp8727_dev_id devid
;
96 struct lp8727_chg_param
*chg_param
;
98 /* Interrupt Handling */
100 struct delayed_work work
;
101 unsigned long debounce_jiffies
;
104 static int lp8727_read_bytes(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
, u8 len
)
108 mutex_lock(&pchg
->xfer_lock
);
109 ret
= i2c_smbus_read_i2c_block_data(pchg
->client
, reg
, len
, data
);
110 mutex_unlock(&pchg
->xfer_lock
);
112 return (ret
!= len
) ? -EIO
: 0;
115 static inline int lp8727_read_byte(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
)
117 return lp8727_read_bytes(pchg
, reg
, data
, 1);
120 static int lp8727_write_byte(struct lp8727_chg
*pchg
, u8 reg
, u8 data
)
124 mutex_lock(&pchg
->xfer_lock
);
125 ret
= i2c_smbus_write_byte_data(pchg
->client
, reg
, data
);
126 mutex_unlock(&pchg
->xfer_lock
);
131 static bool lp8727_is_charger_attached(const char *name
, int id
)
133 if (!strcmp(name
, "ac"))
134 return id
== LP8727_ID_TA
|| id
== LP8727_ID_DEDICATED_CHG
;
135 else if (!strcmp(name
, "usb"))
136 return id
== LP8727_ID_USB_CHG
;
138 return id
>= LP8727_ID_TA
&& id
<= LP8727_ID_USB_CHG
;
141 static int lp8727_init_device(struct lp8727_chg
*pchg
)
145 u8 intstat
[LP8788_NUM_INTREGS
];
147 /* clear interrupts */
148 ret
= lp8727_read_bytes(pchg
, LP8727_INT1
, intstat
, LP8788_NUM_INTREGS
);
152 val
= LP8727_ID200_EN
| LP8727_ADC_EN
| LP8727_CP_EN
;
153 ret
= lp8727_write_byte(pchg
, LP8727_CTRL1
, val
);
157 val
= LP8727_INT_EN
| LP8727_CHGDET_EN
;
158 return lp8727_write_byte(pchg
, LP8727_CTRL2
, val
);
161 static int lp8727_is_dedicated_charger(struct lp8727_chg
*pchg
)
165 lp8727_read_byte(pchg
, LP8727_STATUS1
, &val
);
166 return val
& LP8727_DCPORT
;
169 static int lp8727_is_usb_charger(struct lp8727_chg
*pchg
)
173 lp8727_read_byte(pchg
, LP8727_STATUS1
, &val
);
174 return val
& LP8727_CHPORT
;
177 static inline void lp8727_ctrl_switch(struct lp8727_chg
*pchg
, u8 sw
)
179 lp8727_write_byte(pchg
, LP8727_SWCTRL
, sw
);
182 static void lp8727_id_detection(struct lp8727_chg
*pchg
, u8 id
, int vbusin
)
184 struct lp8727_platform_data
*pdata
= pchg
->pdata
;
185 u8 devid
= LP8727_ID_NONE
;
186 u8 swctrl
= LP8727_SW_DM1_HiZ
| LP8727_SW_DP2_HiZ
;
190 devid
= LP8727_ID_TA
;
191 pchg
->chg_param
= pdata
? pdata
->ac
: NULL
;
194 if (lp8727_is_dedicated_charger(pchg
)) {
195 pchg
->chg_param
= pdata
? pdata
->ac
: NULL
;
196 devid
= LP8727_ID_DEDICATED_CHG
;
197 } else if (lp8727_is_usb_charger(pchg
)) {
198 pchg
->chg_param
= pdata
? pdata
->usb
: NULL
;
199 devid
= LP8727_ID_USB_CHG
;
200 swctrl
= LP8727_SW_DM1_DM
| LP8727_SW_DP2_DP
;
202 devid
= LP8727_ID_USB_DS
;
203 swctrl
= LP8727_SW_DM1_DM
| LP8727_SW_DP2_DP
;
207 devid
= LP8727_ID_NONE
;
208 pchg
->chg_param
= NULL
;
213 lp8727_ctrl_switch(pchg
, swctrl
);
216 static void lp8727_enable_chgdet(struct lp8727_chg
*pchg
)
220 lp8727_read_byte(pchg
, LP8727_CTRL2
, &val
);
221 val
|= LP8727_CHGDET_EN
;
222 lp8727_write_byte(pchg
, LP8727_CTRL2
, val
);
225 static void lp8727_delayed_func(struct work_struct
*_work
)
227 struct lp8727_chg
*pchg
= container_of(_work
, struct lp8727_chg
,
229 u8 intstat
[LP8788_NUM_INTREGS
];
233 if (lp8727_read_bytes(pchg
, LP8727_INT1
, intstat
, LP8788_NUM_INTREGS
)) {
234 dev_err(pchg
->dev
, "can not read INT registers\n");
238 idno
= intstat
[0] & LP8727_IDNO
;
239 vbus
= intstat
[0] & LP8727_VBUS
;
241 lp8727_id_detection(pchg
, idno
, vbus
);
242 lp8727_enable_chgdet(pchg
);
244 power_supply_changed(&pchg
->psy
->ac
);
245 power_supply_changed(&pchg
->psy
->usb
);
246 power_supply_changed(&pchg
->psy
->batt
);
249 static irqreturn_t
lp8727_isr_func(int irq
, void *ptr
)
251 struct lp8727_chg
*pchg
= ptr
;
253 schedule_delayed_work(&pchg
->work
, pchg
->debounce_jiffies
);
257 static int lp8727_setup_irq(struct lp8727_chg
*pchg
)
260 int irq
= pchg
->client
->irq
;
261 unsigned delay_msec
= pchg
->pdata
? pchg
->pdata
->debounce_msec
:
262 DEFAULT_DEBOUNCE_MSEC
;
264 INIT_DELAYED_WORK(&pchg
->work
, lp8727_delayed_func
);
267 dev_warn(pchg
->dev
, "invalid irq number: %d\n", irq
);
271 ret
= request_threaded_irq(irq
, NULL
, lp8727_isr_func
,
272 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
279 pchg
->debounce_jiffies
= msecs_to_jiffies(delay_msec
);
284 static void lp8727_release_irq(struct lp8727_chg
*pchg
)
286 cancel_delayed_work_sync(&pchg
->work
);
289 free_irq(pchg
->irq
, pchg
);
292 static enum power_supply_property lp8727_charger_prop
[] = {
293 POWER_SUPPLY_PROP_ONLINE
,
296 static enum power_supply_property lp8727_battery_prop
[] = {
297 POWER_SUPPLY_PROP_STATUS
,
298 POWER_SUPPLY_PROP_HEALTH
,
299 POWER_SUPPLY_PROP_PRESENT
,
300 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
301 POWER_SUPPLY_PROP_CAPACITY
,
302 POWER_SUPPLY_PROP_TEMP
,
305 static char *battery_supplied_to
[] = {
309 static int lp8727_charger_get_property(struct power_supply
*psy
,
310 enum power_supply_property psp
,
311 union power_supply_propval
*val
)
313 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
315 if (psp
!= POWER_SUPPLY_PROP_ONLINE
)
318 val
->intval
= lp8727_is_charger_attached(psy
->name
, pchg
->devid
);
323 static bool lp8727_is_high_temperature(enum lp8727_die_temp temp
)
326 case LP8788_TEMP_95C
:
327 case LP8788_TEMP_115C
:
328 case LP8788_TEMP_135C
:
335 static int lp8727_battery_get_property(struct power_supply
*psy
,
336 enum power_supply_property psp
,
337 union power_supply_propval
*val
)
339 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
340 struct lp8727_platform_data
*pdata
= pchg
->pdata
;
341 enum lp8727_die_temp temp
;
345 case POWER_SUPPLY_PROP_STATUS
:
346 if (!lp8727_is_charger_attached(psy
->name
, pchg
->devid
)) {
347 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
351 lp8727_read_byte(pchg
, LP8727_STATUS1
, &read
);
353 val
->intval
= (read
& LP8727_CHGSTAT
) == LP8727_STAT_EOC
?
354 POWER_SUPPLY_STATUS_FULL
:
355 POWER_SUPPLY_STATUS_CHARGING
;
357 case POWER_SUPPLY_PROP_HEALTH
:
358 lp8727_read_byte(pchg
, LP8727_STATUS2
, &read
);
359 temp
= (read
& LP8727_TEMP_STAT
) >> LP8727_TEMP_SHIFT
;
361 val
->intval
= lp8727_is_high_temperature(temp
) ?
362 POWER_SUPPLY_HEALTH_OVERHEAT
:
363 POWER_SUPPLY_HEALTH_GOOD
;
365 case POWER_SUPPLY_PROP_PRESENT
:
369 if (pdata
->get_batt_present
)
370 val
->intval
= pchg
->pdata
->get_batt_present();
372 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
376 if (pdata
->get_batt_level
)
377 val
->intval
= pchg
->pdata
->get_batt_level();
379 case POWER_SUPPLY_PROP_CAPACITY
:
383 if (pdata
->get_batt_capacity
)
384 val
->intval
= pchg
->pdata
->get_batt_capacity();
386 case POWER_SUPPLY_PROP_TEMP
:
390 if (pdata
->get_batt_temp
)
391 val
->intval
= pchg
->pdata
->get_batt_temp();
400 static void lp8727_charger_changed(struct power_supply
*psy
)
402 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
->parent
);
407 /* skip if no charger exists */
408 if (!lp8727_is_charger_attached(psy
->name
, pchg
->devid
))
411 /* update charging parameters */
412 if (pchg
->chg_param
) {
413 eoc_level
= pchg
->chg_param
->eoc_level
;
414 ichg
= pchg
->chg_param
->ichg
;
415 val
= (ichg
<< LP8727_ICHG_SHIFT
) | eoc_level
;
416 lp8727_write_byte(pchg
, LP8727_CHGCTRL2
, val
);
420 static int lp8727_register_psy(struct lp8727_chg
*pchg
)
422 struct lp8727_psy
*psy
;
424 psy
= devm_kzalloc(pchg
->dev
, sizeof(*psy
), GFP_KERNEL
);
431 psy
->ac
.type
= POWER_SUPPLY_TYPE_MAINS
;
432 psy
->ac
.properties
= lp8727_charger_prop
;
433 psy
->ac
.num_properties
= ARRAY_SIZE(lp8727_charger_prop
);
434 psy
->ac
.get_property
= lp8727_charger_get_property
;
435 psy
->ac
.supplied_to
= battery_supplied_to
;
436 psy
->ac
.num_supplicants
= ARRAY_SIZE(battery_supplied_to
);
438 if (power_supply_register(pchg
->dev
, &psy
->ac
))
441 psy
->usb
.name
= "usb";
442 psy
->usb
.type
= POWER_SUPPLY_TYPE_USB
;
443 psy
->usb
.properties
= lp8727_charger_prop
;
444 psy
->usb
.num_properties
= ARRAY_SIZE(lp8727_charger_prop
);
445 psy
->usb
.get_property
= lp8727_charger_get_property
;
446 psy
->usb
.supplied_to
= battery_supplied_to
;
447 psy
->usb
.num_supplicants
= ARRAY_SIZE(battery_supplied_to
);
449 if (power_supply_register(pchg
->dev
, &psy
->usb
))
452 psy
->batt
.name
= "main_batt";
453 psy
->batt
.type
= POWER_SUPPLY_TYPE_BATTERY
;
454 psy
->batt
.properties
= lp8727_battery_prop
;
455 psy
->batt
.num_properties
= ARRAY_SIZE(lp8727_battery_prop
);
456 psy
->batt
.get_property
= lp8727_battery_get_property
;
457 psy
->batt
.external_power_changed
= lp8727_charger_changed
;
459 if (power_supply_register(pchg
->dev
, &psy
->batt
))
465 power_supply_unregister(&psy
->usb
);
467 power_supply_unregister(&psy
->ac
);
472 static void lp8727_unregister_psy(struct lp8727_chg
*pchg
)
474 struct lp8727_psy
*psy
= pchg
->psy
;
479 power_supply_unregister(&psy
->ac
);
480 power_supply_unregister(&psy
->usb
);
481 power_supply_unregister(&psy
->batt
);
484 static int lp8727_probe(struct i2c_client
*cl
, const struct i2c_device_id
*id
)
486 struct lp8727_chg
*pchg
;
489 if (!i2c_check_functionality(cl
->adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
))
492 pchg
= devm_kzalloc(&cl
->dev
, sizeof(*pchg
), GFP_KERNEL
);
497 pchg
->dev
= &cl
->dev
;
498 pchg
->pdata
= cl
->dev
.platform_data
;
499 i2c_set_clientdata(cl
, pchg
);
501 mutex_init(&pchg
->xfer_lock
);
503 ret
= lp8727_init_device(pchg
);
505 dev_err(pchg
->dev
, "i2c communication err: %d", ret
);
509 ret
= lp8727_register_psy(pchg
);
511 dev_err(pchg
->dev
, "power supplies register err: %d", ret
);
515 ret
= lp8727_setup_irq(pchg
);
517 dev_err(pchg
->dev
, "irq handler err: %d", ret
);
518 lp8727_unregister_psy(pchg
);
525 static int lp8727_remove(struct i2c_client
*cl
)
527 struct lp8727_chg
*pchg
= i2c_get_clientdata(cl
);
529 lp8727_release_irq(pchg
);
530 lp8727_unregister_psy(pchg
);
534 static const struct i2c_device_id lp8727_ids
[] = {
538 MODULE_DEVICE_TABLE(i2c
, lp8727_ids
);
540 static struct i2c_driver lp8727_driver
= {
544 .probe
= lp8727_probe
,
545 .remove
= lp8727_remove
,
546 .id_table
= lp8727_ids
,
548 module_i2c_driver(lp8727_driver
);
550 MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
551 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>, Daniel Jeong <daniel.jeong@ti.com>");
552 MODULE_LICENSE("GPL");