1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for LP8727 Micro/Mini USB IC with integrated charger
5 * Copyright (C) 2011 Texas Instruments
6 * Copyright (C) 2011 National Semiconductor
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/interrupt.h>
12 #include <linux/i2c.h>
13 #include <linux/power_supply.h>
14 #include <linux/platform_data/lp8727.h>
17 #define LP8788_NUM_INTREGS 2
18 #define DEFAULT_DEBOUNCE_MSEC 270
21 #define LP8727_CTRL1 0x1
22 #define LP8727_CTRL2 0x2
23 #define LP8727_SWCTRL 0x3
24 #define LP8727_INT1 0x4
25 #define LP8727_INT2 0x5
26 #define LP8727_STATUS1 0x6
27 #define LP8727_STATUS2 0x7
28 #define LP8727_CHGCTRL2 0x9
31 #define LP8727_CP_EN BIT(0)
32 #define LP8727_ADC_EN BIT(1)
33 #define LP8727_ID200_EN BIT(4)
36 #define LP8727_CHGDET_EN BIT(1)
37 #define LP8727_INT_EN BIT(6)
40 #define LP8727_SW_DM1_DM (0x0 << 0)
41 #define LP8727_SW_DM1_HiZ (0x7 << 0)
42 #define LP8727_SW_DP2_DP (0x0 << 3)
43 #define LP8727_SW_DP2_HiZ (0x7 << 3)
46 #define LP8727_IDNO (0xF << 0)
47 #define LP8727_VBUS BIT(4)
49 /* STATUS1 register */
50 #define LP8727_CHGSTAT (3 << 4)
51 #define LP8727_CHPORT BIT(6)
52 #define LP8727_DCPORT BIT(7)
53 #define LP8727_STAT_EOC 0x30
55 /* STATUS2 register */
56 #define LP8727_TEMP_STAT (3 << 5)
57 #define LP8727_TEMP_SHIFT 5
59 /* CHGCTRL2 register */
60 #define LP8727_ICHG_SHIFT 4
65 LP8727_ID_DEDICATED_CHG
,
71 enum lp8727_die_temp
{
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 lp8727_psy
*psy
;
89 struct lp8727_platform_data
*pdata
;
92 enum lp8727_dev_id devid
;
93 struct lp8727_chg_param
*chg_param
;
95 /* Interrupt Handling */
97 struct delayed_work work
;
98 unsigned long debounce_jiffies
;
101 static int lp8727_read_bytes(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
, u8 len
)
105 mutex_lock(&pchg
->xfer_lock
);
106 ret
= i2c_smbus_read_i2c_block_data(pchg
->client
, reg
, len
, data
);
107 mutex_unlock(&pchg
->xfer_lock
);
109 return (ret
!= len
) ? -EIO
: 0;
112 static inline int lp8727_read_byte(struct lp8727_chg
*pchg
, u8 reg
, u8
*data
)
114 return lp8727_read_bytes(pchg
, reg
, data
, 1);
117 static int lp8727_write_byte(struct lp8727_chg
*pchg
, u8 reg
, u8 data
)
121 mutex_lock(&pchg
->xfer_lock
);
122 ret
= i2c_smbus_write_byte_data(pchg
->client
, reg
, data
);
123 mutex_unlock(&pchg
->xfer_lock
);
128 static bool lp8727_is_charger_attached(const char *name
, int id
)
130 if (!strcmp(name
, "ac"))
131 return id
== LP8727_ID_TA
|| id
== LP8727_ID_DEDICATED_CHG
;
132 else if (!strcmp(name
, "usb"))
133 return id
== LP8727_ID_USB_CHG
;
135 return id
>= LP8727_ID_TA
&& id
<= LP8727_ID_USB_CHG
;
138 static int lp8727_init_device(struct lp8727_chg
*pchg
)
142 u8 intstat
[LP8788_NUM_INTREGS
];
144 /* clear interrupts */
145 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
)
162 lp8727_read_byte(pchg
, LP8727_STATUS1
, &val
);
163 return val
& LP8727_DCPORT
;
166 static int lp8727_is_usb_charger(struct lp8727_chg
*pchg
)
170 lp8727_read_byte(pchg
, LP8727_STATUS1
, &val
);
171 return val
& LP8727_CHPORT
;
174 static inline void lp8727_ctrl_switch(struct lp8727_chg
*pchg
, u8 sw
)
176 lp8727_write_byte(pchg
, LP8727_SWCTRL
, sw
);
179 static void lp8727_id_detection(struct lp8727_chg
*pchg
, u8 id
, int vbusin
)
181 struct lp8727_platform_data
*pdata
= pchg
->pdata
;
182 u8 devid
= LP8727_ID_NONE
;
183 u8 swctrl
= LP8727_SW_DM1_HiZ
| LP8727_SW_DP2_HiZ
;
187 devid
= LP8727_ID_TA
;
188 pchg
->chg_param
= pdata
? pdata
->ac
: NULL
;
191 if (lp8727_is_dedicated_charger(pchg
)) {
192 pchg
->chg_param
= pdata
? pdata
->ac
: NULL
;
193 devid
= LP8727_ID_DEDICATED_CHG
;
194 } else if (lp8727_is_usb_charger(pchg
)) {
195 pchg
->chg_param
= pdata
? pdata
->usb
: NULL
;
196 devid
= LP8727_ID_USB_CHG
;
197 swctrl
= LP8727_SW_DM1_DM
| LP8727_SW_DP2_DP
;
199 devid
= LP8727_ID_USB_DS
;
200 swctrl
= LP8727_SW_DM1_DM
| LP8727_SW_DP2_DP
;
204 devid
= LP8727_ID_NONE
;
205 pchg
->chg_param
= NULL
;
210 lp8727_ctrl_switch(pchg
, swctrl
);
213 static void lp8727_enable_chgdet(struct lp8727_chg
*pchg
)
217 lp8727_read_byte(pchg
, LP8727_CTRL2
, &val
);
218 val
|= LP8727_CHGDET_EN
;
219 lp8727_write_byte(pchg
, LP8727_CTRL2
, val
);
222 static void lp8727_delayed_func(struct work_struct
*_work
)
224 struct lp8727_chg
*pchg
= container_of(_work
, struct lp8727_chg
,
226 u8 intstat
[LP8788_NUM_INTREGS
];
230 if (lp8727_read_bytes(pchg
, LP8727_INT1
, intstat
, LP8788_NUM_INTREGS
)) {
231 dev_err(pchg
->dev
, "can not read INT registers\n");
235 idno
= intstat
[0] & LP8727_IDNO
;
236 vbus
= intstat
[0] & LP8727_VBUS
;
238 lp8727_id_detection(pchg
, idno
, vbus
);
239 lp8727_enable_chgdet(pchg
);
241 power_supply_changed(pchg
->psy
->ac
);
242 power_supply_changed(pchg
->psy
->usb
);
243 power_supply_changed(pchg
->psy
->batt
);
246 static irqreturn_t
lp8727_isr_func(int irq
, void *ptr
)
248 struct lp8727_chg
*pchg
= ptr
;
250 schedule_delayed_work(&pchg
->work
, pchg
->debounce_jiffies
);
254 static int lp8727_setup_irq(struct lp8727_chg
*pchg
)
257 int irq
= pchg
->client
->irq
;
258 unsigned delay_msec
= pchg
->pdata
? pchg
->pdata
->debounce_msec
:
259 DEFAULT_DEBOUNCE_MSEC
;
261 INIT_DELAYED_WORK(&pchg
->work
, lp8727_delayed_func
);
264 dev_warn(pchg
->dev
, "invalid irq number: %d\n", irq
);
268 ret
= request_threaded_irq(irq
, NULL
, lp8727_isr_func
,
269 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
276 pchg
->debounce_jiffies
= msecs_to_jiffies(delay_msec
);
281 static void lp8727_release_irq(struct lp8727_chg
*pchg
)
283 cancel_delayed_work_sync(&pchg
->work
);
286 free_irq(pchg
->irq
, pchg
);
289 static enum power_supply_property lp8727_charger_prop
[] = {
290 POWER_SUPPLY_PROP_ONLINE
,
293 static enum power_supply_property lp8727_battery_prop
[] = {
294 POWER_SUPPLY_PROP_STATUS
,
295 POWER_SUPPLY_PROP_HEALTH
,
296 POWER_SUPPLY_PROP_PRESENT
,
297 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
298 POWER_SUPPLY_PROP_CAPACITY
,
299 POWER_SUPPLY_PROP_TEMP
,
302 static char *battery_supplied_to
[] = {
306 static int lp8727_charger_get_property(struct power_supply
*psy
,
307 enum power_supply_property psp
,
308 union power_supply_propval
*val
)
310 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
.parent
);
312 if (psp
!= POWER_SUPPLY_PROP_ONLINE
)
315 val
->intval
= lp8727_is_charger_attached(psy
->desc
->name
, pchg
->devid
);
320 static bool lp8727_is_high_temperature(enum lp8727_die_temp temp
)
323 case LP8788_TEMP_95C
:
324 case LP8788_TEMP_115C
:
325 case LP8788_TEMP_135C
:
332 static int lp8727_battery_get_property(struct power_supply
*psy
,
333 enum power_supply_property psp
,
334 union power_supply_propval
*val
)
336 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
.parent
);
337 struct lp8727_platform_data
*pdata
= pchg
->pdata
;
338 enum lp8727_die_temp temp
;
342 case POWER_SUPPLY_PROP_STATUS
:
343 if (!lp8727_is_charger_attached(psy
->desc
->name
, pchg
->devid
)) {
344 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
348 lp8727_read_byte(pchg
, LP8727_STATUS1
, &read
);
350 val
->intval
= (read
& LP8727_CHGSTAT
) == LP8727_STAT_EOC
?
351 POWER_SUPPLY_STATUS_FULL
:
352 POWER_SUPPLY_STATUS_CHARGING
;
354 case POWER_SUPPLY_PROP_HEALTH
:
355 lp8727_read_byte(pchg
, LP8727_STATUS2
, &read
);
356 temp
= (read
& LP8727_TEMP_STAT
) >> LP8727_TEMP_SHIFT
;
358 val
->intval
= lp8727_is_high_temperature(temp
) ?
359 POWER_SUPPLY_HEALTH_OVERHEAT
:
360 POWER_SUPPLY_HEALTH_GOOD
;
362 case POWER_SUPPLY_PROP_PRESENT
:
366 if (pdata
->get_batt_present
)
367 val
->intval
= pdata
->get_batt_present();
369 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
373 if (pdata
->get_batt_level
)
374 val
->intval
= pdata
->get_batt_level();
376 case POWER_SUPPLY_PROP_CAPACITY
:
380 if (pdata
->get_batt_capacity
)
381 val
->intval
= pdata
->get_batt_capacity();
383 case POWER_SUPPLY_PROP_TEMP
:
387 if (pdata
->get_batt_temp
)
388 val
->intval
= pdata
->get_batt_temp();
397 static void lp8727_charger_changed(struct power_supply
*psy
)
399 struct lp8727_chg
*pchg
= dev_get_drvdata(psy
->dev
.parent
);
404 /* skip if no charger exists */
405 if (!lp8727_is_charger_attached(psy
->desc
->name
, pchg
->devid
))
408 /* update charging parameters */
409 if (pchg
->chg_param
) {
410 eoc_level
= pchg
->chg_param
->eoc_level
;
411 ichg
= pchg
->chg_param
->ichg
;
412 val
= (ichg
<< LP8727_ICHG_SHIFT
) | eoc_level
;
413 lp8727_write_byte(pchg
, LP8727_CHGCTRL2
, val
);
417 static const struct power_supply_desc lp8727_ac_desc
= {
419 .type
= POWER_SUPPLY_TYPE_MAINS
,
420 .properties
= lp8727_charger_prop
,
421 .num_properties
= ARRAY_SIZE(lp8727_charger_prop
),
422 .get_property
= lp8727_charger_get_property
,
425 static const struct power_supply_desc lp8727_usb_desc
= {
427 .type
= POWER_SUPPLY_TYPE_USB
,
428 .properties
= lp8727_charger_prop
,
429 .num_properties
= ARRAY_SIZE(lp8727_charger_prop
),
430 .get_property
= lp8727_charger_get_property
,
433 static const struct power_supply_desc lp8727_batt_desc
= {
435 .type
= POWER_SUPPLY_TYPE_BATTERY
,
436 .properties
= lp8727_battery_prop
,
437 .num_properties
= ARRAY_SIZE(lp8727_battery_prop
),
438 .get_property
= lp8727_battery_get_property
,
439 .external_power_changed
= lp8727_charger_changed
,
442 static int lp8727_register_psy(struct lp8727_chg
*pchg
)
444 struct power_supply_config psy_cfg
= {}; /* Only for ac and usb */
445 struct lp8727_psy
*psy
;
447 psy
= devm_kzalloc(pchg
->dev
, sizeof(*psy
), GFP_KERNEL
);
453 psy_cfg
.supplied_to
= battery_supplied_to
;
454 psy_cfg
.num_supplicants
= ARRAY_SIZE(battery_supplied_to
);
456 psy
->ac
= power_supply_register(pchg
->dev
, &lp8727_ac_desc
, &psy_cfg
);
460 psy
->usb
= power_supply_register(pchg
->dev
, &lp8727_usb_desc
,
462 if (IS_ERR(psy
->usb
))
465 psy
->batt
= power_supply_register(pchg
->dev
, &lp8727_batt_desc
, NULL
);
466 if (IS_ERR(psy
->batt
))
472 power_supply_unregister(psy
->usb
);
474 power_supply_unregister(psy
->ac
);
479 static void lp8727_unregister_psy(struct lp8727_chg
*pchg
)
481 struct lp8727_psy
*psy
= pchg
->psy
;
486 power_supply_unregister(psy
->ac
);
487 power_supply_unregister(psy
->usb
);
488 power_supply_unregister(psy
->batt
);
492 static struct lp8727_chg_param
493 *lp8727_parse_charge_pdata(struct device
*dev
, struct device_node
*np
)
495 struct lp8727_chg_param
*param
;
497 param
= devm_kzalloc(dev
, sizeof(*param
), GFP_KERNEL
);
501 of_property_read_u8(np
, "eoc-level", (u8
*)¶m
->eoc_level
);
502 of_property_read_u8(np
, "charging-current", (u8
*)¶m
->ichg
);
507 static struct lp8727_platform_data
*lp8727_parse_dt(struct device
*dev
)
509 struct device_node
*np
= dev
->of_node
;
510 struct device_node
*child
;
511 struct lp8727_platform_data
*pdata
;
514 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
516 return ERR_PTR(-ENOMEM
);
518 of_property_read_u32(np
, "debounce-ms", &pdata
->debounce_msec
);
520 /* If charging parameter is not defined, just skip parsing the dt */
521 if (of_get_child_count(np
) == 0)
524 for_each_child_of_node(np
, child
) {
525 of_property_read_string(child
, "charger-type", &type
);
527 if (!strcmp(type
, "ac"))
528 pdata
->ac
= lp8727_parse_charge_pdata(dev
, child
);
530 if (!strcmp(type
, "usb"))
531 pdata
->usb
= lp8727_parse_charge_pdata(dev
, child
);
537 static struct lp8727_platform_data
*lp8727_parse_dt(struct device
*dev
)
543 static int lp8727_probe(struct i2c_client
*cl
, const struct i2c_device_id
*id
)
545 struct lp8727_chg
*pchg
;
546 struct lp8727_platform_data
*pdata
;
549 if (!i2c_check_functionality(cl
->adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
))
552 if (cl
->dev
.of_node
) {
553 pdata
= lp8727_parse_dt(&cl
->dev
);
555 return PTR_ERR(pdata
);
557 pdata
= dev_get_platdata(&cl
->dev
);
560 pchg
= devm_kzalloc(&cl
->dev
, sizeof(*pchg
), GFP_KERNEL
);
565 pchg
->dev
= &cl
->dev
;
567 i2c_set_clientdata(cl
, pchg
);
569 mutex_init(&pchg
->xfer_lock
);
571 ret
= lp8727_init_device(pchg
);
573 dev_err(pchg
->dev
, "i2c communication err: %d", ret
);
577 ret
= lp8727_register_psy(pchg
);
579 dev_err(pchg
->dev
, "power supplies register err: %d", ret
);
583 ret
= lp8727_setup_irq(pchg
);
585 dev_err(pchg
->dev
, "irq handler err: %d", ret
);
586 lp8727_unregister_psy(pchg
);
593 static int lp8727_remove(struct i2c_client
*cl
)
595 struct lp8727_chg
*pchg
= i2c_get_clientdata(cl
);
597 lp8727_release_irq(pchg
);
598 lp8727_unregister_psy(pchg
);
602 static const struct of_device_id lp8727_dt_ids
[] = {
603 { .compatible
= "ti,lp8727", },
606 MODULE_DEVICE_TABLE(of
, lp8727_dt_ids
);
608 static const struct i2c_device_id lp8727_ids
[] = {
612 MODULE_DEVICE_TABLE(i2c
, lp8727_ids
);
614 static struct i2c_driver lp8727_driver
= {
617 .of_match_table
= of_match_ptr(lp8727_dt_ids
),
619 .probe
= lp8727_probe
,
620 .remove
= lp8727_remove
,
621 .id_table
= lp8727_ids
,
623 module_i2c_driver(lp8727_driver
);
625 MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
626 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>, Daniel Jeong <daniel.jeong@ti.com>");
627 MODULE_LICENSE("GPL");