2 * TI LP8788 MFD - battery charger driver
4 * Copyright 2012 Texas Instruments
6 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/err.h>
15 #include <linux/iio/consumer.h>
16 #include <linux/interrupt.h>
17 #include <linux/irqdomain.h>
18 #include <linux/mfd/lp8788.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
25 /* register address */
26 #define LP8788_CHG_STATUS 0x07
27 #define LP8788_CHG_IDCIN 0x13
28 #define LP8788_CHG_IBATT 0x14
29 #define LP8788_CHG_VTERM 0x15
30 #define LP8788_CHG_EOC 0x16
33 #define LP8788_CHG_INPUT_STATE_M 0x03 /* Addr 07h */
34 #define LP8788_CHG_STATE_M 0x3C
35 #define LP8788_CHG_STATE_S 2
36 #define LP8788_NO_BATT_M BIT(6)
37 #define LP8788_BAD_BATT_M BIT(7)
38 #define LP8788_CHG_IBATT_M 0x1F /* Addr 14h */
39 #define LP8788_CHG_VTERM_M 0x0F /* Addr 15h */
40 #define LP8788_CHG_EOC_LEVEL_M 0x30 /* Addr 16h */
41 #define LP8788_CHG_EOC_LEVEL_S 4
42 #define LP8788_CHG_EOC_TIME_M 0x0E
43 #define LP8788_CHG_EOC_TIME_S 1
44 #define LP8788_CHG_EOC_MODE_M BIT(0)
46 #define LP8788_CHARGER_NAME "charger"
47 #define LP8788_BATTERY_NAME "main_batt"
49 #define LP8788_CHG_START 0x11
50 #define LP8788_CHG_END 0x1C
52 #define LP8788_ISEL_MAX 23
53 #define LP8788_ISEL_STEP 50
54 #define LP8788_VTERM_MIN 4100
55 #define LP8788_VTERM_STEP 25
56 #define LP8788_MAX_BATT_CAPACITY 100
57 #define LP8788_MAX_CHG_IRQS 11
59 enum lp8788_charging_state
{
62 LP8788_LOW_INPUT
= 0x3,
68 LP8788_SYSTEM_SUPPORT
= 0xC,
69 LP8788_HIGH_CURRENT
= 0xF,
73 enum lp8788_charger_adc_sel
{
79 enum lp8788_charger_input_state
{
80 LP8788_SYSTEM_SUPPLY
= 1,
85 * struct lp8788_chg_irq
86 * @which : lp8788 interrupt id
87 * @virq : Linux IRQ number from irq_domain
89 struct lp8788_chg_irq
{
90 enum lp8788_int_id which
;
95 * struct lp8788_charger
96 * @lp : used for accessing the registers of mfd lp8788 device
97 * @charger : power supply driver for the battery charger
98 * @battery : power supply driver for the battery
99 * @charger_work : work queue for charger input interrupts
100 * @chan : iio channels for getting adc values
101 * eg) battery voltage, capacity and temperature
102 * @irqs : charger dedicated interrupts
103 * @num_irqs : total numbers of charger interrupts
104 * @pdata : charger platform specific data
106 struct lp8788_charger
{
108 struct power_supply
*charger
;
109 struct power_supply
*battery
;
110 struct work_struct charger_work
;
111 struct iio_channel
*chan
[LP8788_NUM_CHG_ADC
];
112 struct lp8788_chg_irq irqs
[LP8788_MAX_CHG_IRQS
];
114 struct lp8788_charger_platform_data
*pdata
;
117 static char *battery_supplied_to
[] = {
121 static enum power_supply_property lp8788_charger_prop
[] = {
122 POWER_SUPPLY_PROP_ONLINE
,
123 POWER_SUPPLY_PROP_CURRENT_MAX
,
126 static enum power_supply_property lp8788_battery_prop
[] = {
127 POWER_SUPPLY_PROP_STATUS
,
128 POWER_SUPPLY_PROP_HEALTH
,
129 POWER_SUPPLY_PROP_PRESENT
,
130 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
131 POWER_SUPPLY_PROP_CAPACITY
,
132 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
133 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
134 POWER_SUPPLY_PROP_TEMP
,
137 static bool lp8788_is_charger_detected(struct lp8788_charger
*pchg
)
141 lp8788_read_byte(pchg
->lp
, LP8788_CHG_STATUS
, &data
);
142 data
&= LP8788_CHG_INPUT_STATE_M
;
144 return data
== LP8788_SYSTEM_SUPPLY
|| data
== LP8788_FULL_FUNCTION
;
147 static int lp8788_charger_get_property(struct power_supply
*psy
,
148 enum power_supply_property psp
,
149 union power_supply_propval
*val
)
151 struct lp8788_charger
*pchg
= dev_get_drvdata(psy
->dev
.parent
);
155 case POWER_SUPPLY_PROP_ONLINE
:
156 val
->intval
= lp8788_is_charger_detected(pchg
);
158 case POWER_SUPPLY_PROP_CURRENT_MAX
:
159 lp8788_read_byte(pchg
->lp
, LP8788_CHG_IDCIN
, &read
);
160 val
->intval
= LP8788_ISEL_STEP
*
161 (min_t(int, read
, LP8788_ISEL_MAX
) + 1);
170 static int lp8788_get_battery_status(struct lp8788_charger
*pchg
,
171 union power_supply_propval
*val
)
173 enum lp8788_charging_state state
;
177 ret
= lp8788_read_byte(pchg
->lp
, LP8788_CHG_STATUS
, &data
);
181 state
= (data
& LP8788_CHG_STATE_M
) >> LP8788_CHG_STATE_S
;
184 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
186 case LP8788_PRECHARGE
:
189 case LP8788_HIGH_CURRENT
:
190 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
192 case LP8788_MAINTENANCE
:
193 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
196 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
203 static int lp8788_get_battery_health(struct lp8788_charger
*pchg
,
204 union power_supply_propval
*val
)
209 ret
= lp8788_read_byte(pchg
->lp
, LP8788_CHG_STATUS
, &data
);
213 if (data
& LP8788_NO_BATT_M
)
214 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
215 else if (data
& LP8788_BAD_BATT_M
)
216 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
218 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
223 static int lp8788_get_battery_present(struct lp8788_charger
*pchg
,
224 union power_supply_propval
*val
)
229 ret
= lp8788_read_byte(pchg
->lp
, LP8788_CHG_STATUS
, &data
);
233 val
->intval
= !(data
& LP8788_NO_BATT_M
);
237 static int lp8788_get_vbatt_adc(struct lp8788_charger
*pchg
, int *result
)
239 struct iio_channel
*channel
= pchg
->chan
[LP8788_VBATT
];
244 return iio_read_channel_processed(channel
, result
);
247 static int lp8788_get_battery_voltage(struct lp8788_charger
*pchg
,
248 union power_supply_propval
*val
)
250 return lp8788_get_vbatt_adc(pchg
, &val
->intval
);
253 static int lp8788_get_battery_capacity(struct lp8788_charger
*pchg
,
254 union power_supply_propval
*val
)
256 struct lp8788
*lp
= pchg
->lp
;
257 struct lp8788_charger_platform_data
*pdata
= pchg
->pdata
;
258 unsigned int max_vbatt
;
260 enum lp8788_charging_state state
;
267 max_vbatt
= pdata
->max_vbatt_mv
;
271 ret
= lp8788_read_byte(lp
, LP8788_CHG_STATUS
, &data
);
275 state
= (data
& LP8788_CHG_STATE_M
) >> LP8788_CHG_STATE_S
;
277 if (state
== LP8788_MAINTENANCE
) {
278 val
->intval
= LP8788_MAX_BATT_CAPACITY
;
280 ret
= lp8788_get_vbatt_adc(pchg
, &vbatt
);
284 val
->intval
= (vbatt
* LP8788_MAX_BATT_CAPACITY
) / max_vbatt
;
285 val
->intval
= min(val
->intval
, LP8788_MAX_BATT_CAPACITY
);
291 static int lp8788_get_battery_temperature(struct lp8788_charger
*pchg
,
292 union power_supply_propval
*val
)
294 struct iio_channel
*channel
= pchg
->chan
[LP8788_BATT_TEMP
];
301 ret
= iio_read_channel_processed(channel
, &result
);
306 val
->intval
= result
* 10;
311 static int lp8788_get_battery_charging_current(struct lp8788_charger
*pchg
,
312 union power_supply_propval
*val
)
316 lp8788_read_byte(pchg
->lp
, LP8788_CHG_IBATT
, &read
);
317 read
&= LP8788_CHG_IBATT_M
;
318 val
->intval
= LP8788_ISEL_STEP
*
319 (min_t(int, read
, LP8788_ISEL_MAX
) + 1);
324 static int lp8788_get_charging_termination_voltage(struct lp8788_charger
*pchg
,
325 union power_supply_propval
*val
)
329 lp8788_read_byte(pchg
->lp
, LP8788_CHG_VTERM
, &read
);
330 read
&= LP8788_CHG_VTERM_M
;
331 val
->intval
= LP8788_VTERM_MIN
+ LP8788_VTERM_STEP
* read
;
336 static int lp8788_battery_get_property(struct power_supply
*psy
,
337 enum power_supply_property psp
,
338 union power_supply_propval
*val
)
340 struct lp8788_charger
*pchg
= dev_get_drvdata(psy
->dev
.parent
);
343 case POWER_SUPPLY_PROP_STATUS
:
344 return lp8788_get_battery_status(pchg
, val
);
345 case POWER_SUPPLY_PROP_HEALTH
:
346 return lp8788_get_battery_health(pchg
, val
);
347 case POWER_SUPPLY_PROP_PRESENT
:
348 return lp8788_get_battery_present(pchg
, val
);
349 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
350 return lp8788_get_battery_voltage(pchg
, val
);
351 case POWER_SUPPLY_PROP_CAPACITY
:
352 return lp8788_get_battery_capacity(pchg
, val
);
353 case POWER_SUPPLY_PROP_TEMP
:
354 return lp8788_get_battery_temperature(pchg
, val
);
355 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
356 return lp8788_get_battery_charging_current(pchg
, val
);
357 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
358 return lp8788_get_charging_termination_voltage(pchg
, val
);
364 static inline bool lp8788_is_valid_charger_register(u8 addr
)
366 return addr
>= LP8788_CHG_START
&& addr
<= LP8788_CHG_END
;
369 static int lp8788_update_charger_params(struct platform_device
*pdev
,
370 struct lp8788_charger
*pchg
)
372 struct lp8788
*lp
= pchg
->lp
;
373 struct lp8788_charger_platform_data
*pdata
= pchg
->pdata
;
374 struct lp8788_chg_param
*param
;
378 if (!pdata
|| !pdata
->chg_params
) {
379 dev_info(&pdev
->dev
, "skip updating charger parameters\n");
383 /* settting charging parameters */
384 for (i
= 0; i
< pdata
->num_chg_params
; i
++) {
385 param
= pdata
->chg_params
+ i
;
390 if (lp8788_is_valid_charger_register(param
->addr
)) {
391 ret
= lp8788_write_byte(lp
, param
->addr
, param
->val
);
400 static const struct power_supply_desc lp8788_psy_charger_desc
= {
401 .name
= LP8788_CHARGER_NAME
,
402 .type
= POWER_SUPPLY_TYPE_MAINS
,
403 .properties
= lp8788_charger_prop
,
404 .num_properties
= ARRAY_SIZE(lp8788_charger_prop
),
405 .get_property
= lp8788_charger_get_property
,
408 static const struct power_supply_desc lp8788_psy_battery_desc
= {
409 .name
= LP8788_BATTERY_NAME
,
410 .type
= POWER_SUPPLY_TYPE_BATTERY
,
411 .properties
= lp8788_battery_prop
,
412 .num_properties
= ARRAY_SIZE(lp8788_battery_prop
),
413 .get_property
= lp8788_battery_get_property
,
416 static int lp8788_psy_register(struct platform_device
*pdev
,
417 struct lp8788_charger
*pchg
)
419 struct power_supply_config charger_cfg
= {};
421 charger_cfg
.supplied_to
= battery_supplied_to
;
422 charger_cfg
.num_supplicants
= ARRAY_SIZE(battery_supplied_to
);
424 pchg
->charger
= power_supply_register(&pdev
->dev
,
425 &lp8788_psy_charger_desc
,
427 if (IS_ERR(pchg
->charger
))
430 pchg
->battery
= power_supply_register(&pdev
->dev
,
431 &lp8788_psy_battery_desc
, NULL
);
432 if (IS_ERR(pchg
->battery
)) {
433 power_supply_unregister(pchg
->charger
);
440 static void lp8788_psy_unregister(struct lp8788_charger
*pchg
)
442 power_supply_unregister(pchg
->battery
);
443 power_supply_unregister(pchg
->charger
);
446 static void lp8788_charger_event(struct work_struct
*work
)
448 struct lp8788_charger
*pchg
=
449 container_of(work
, struct lp8788_charger
, charger_work
);
450 struct lp8788_charger_platform_data
*pdata
= pchg
->pdata
;
451 enum lp8788_charger_event event
= lp8788_is_charger_detected(pchg
);
453 pdata
->charger_event(pchg
->lp
, event
);
456 static bool lp8788_find_irq_id(struct lp8788_charger
*pchg
, int virq
, int *id
)
461 for (i
= 0; i
< pchg
->num_irqs
; i
++) {
462 if (pchg
->irqs
[i
].virq
== virq
) {
463 *id
= pchg
->irqs
[i
].which
;
472 static irqreturn_t
lp8788_charger_irq_thread(int virq
, void *ptr
)
474 struct lp8788_charger
*pchg
= ptr
;
475 struct lp8788_charger_platform_data
*pdata
= pchg
->pdata
;
478 if (!lp8788_find_irq_id(pchg
, virq
, &id
))
482 case LP8788_INT_CHG_INPUT_STATE
:
483 case LP8788_INT_CHG_STATE
:
485 case LP8788_INT_BATT_LOW
:
486 case LP8788_INT_NO_BATT
:
487 power_supply_changed(pchg
->charger
);
488 power_supply_changed(pchg
->battery
);
494 /* report charger dectection event if used */
498 if (pdata
->charger_event
&& id
== LP8788_INT_CHG_INPUT_STATE
)
499 schedule_work(&pchg
->charger_work
);
505 static int lp8788_set_irqs(struct platform_device
*pdev
,
506 struct lp8788_charger
*pchg
, const char *name
)
509 struct irq_domain
*irqdm
= pchg
->lp
->irqdm
;
517 /* no error even if no irq resource */
518 r
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, name
);
522 irq_start
= r
->start
;
525 for (i
= irq_start
; i
<= irq_end
; i
++) {
526 nr_irq
= pchg
->num_irqs
;
528 virq
= irq_create_mapping(irqdm
, i
);
529 pchg
->irqs
[nr_irq
].virq
= virq
;
530 pchg
->irqs
[nr_irq
].which
= i
;
533 ret
= request_threaded_irq(virq
, NULL
,
534 lp8788_charger_irq_thread
,
546 for (i
= 0; i
< pchg
->num_irqs
; i
++)
547 free_irq(pchg
->irqs
[i
].virq
, pchg
);
551 static int lp8788_irq_register(struct platform_device
*pdev
,
552 struct lp8788_charger
*pchg
)
554 const char *name
[] = {
555 LP8788_CHG_IRQ
, LP8788_PRSW_IRQ
, LP8788_BATT_IRQ
560 INIT_WORK(&pchg
->charger_work
, lp8788_charger_event
);
563 for (i
= 0; i
< ARRAY_SIZE(name
); i
++) {
564 ret
= lp8788_set_irqs(pdev
, pchg
, name
[i
]);
566 dev_warn(&pdev
->dev
, "irq setup failed: %s\n", name
[i
]);
571 if (pchg
->num_irqs
> LP8788_MAX_CHG_IRQS
) {
572 dev_err(&pdev
->dev
, "invalid total number of irqs: %d\n",
581 static void lp8788_irq_unregister(struct platform_device
*pdev
,
582 struct lp8788_charger
*pchg
)
587 for (i
= 0; i
< pchg
->num_irqs
; i
++) {
588 irq
= pchg
->irqs
[i
].virq
;
596 static void lp8788_setup_adc_channel(struct device
*dev
,
597 struct lp8788_charger
*pchg
)
599 struct lp8788_charger_platform_data
*pdata
= pchg
->pdata
;
600 struct iio_channel
*chan
;
605 /* ADC channel for battery voltage */
606 chan
= iio_channel_get(dev
, pdata
->adc_vbatt
);
607 pchg
->chan
[LP8788_VBATT
] = IS_ERR(chan
) ? NULL
: chan
;
609 /* ADC channel for battery temperature */
610 chan
= iio_channel_get(dev
, pdata
->adc_batt_temp
);
611 pchg
->chan
[LP8788_BATT_TEMP
] = IS_ERR(chan
) ? NULL
: chan
;
614 static void lp8788_release_adc_channel(struct lp8788_charger
*pchg
)
618 for (i
= 0; i
< LP8788_NUM_CHG_ADC
; i
++) {
622 iio_channel_release(pchg
->chan
[i
]);
623 pchg
->chan
[i
] = NULL
;
627 static ssize_t
lp8788_show_charger_status(struct device
*dev
,
628 struct device_attribute
*attr
, char *buf
)
630 struct lp8788_charger
*pchg
= dev_get_drvdata(dev
);
631 enum lp8788_charging_state state
;
632 char *desc
[LP8788_MAX_CHG_STATE
] = {
633 [LP8788_OFF
] = "CHARGER OFF",
634 [LP8788_WARM_UP
] = "WARM UP",
635 [LP8788_LOW_INPUT
] = "LOW INPUT STATE",
636 [LP8788_PRECHARGE
] = "CHARGING - PRECHARGE",
637 [LP8788_CC
] = "CHARGING - CC",
638 [LP8788_CV
] = "CHARGING - CV",
639 [LP8788_MAINTENANCE
] = "NO CHARGING - MAINTENANCE",
640 [LP8788_BATTERY_FAULT
] = "BATTERY FAULT",
641 [LP8788_SYSTEM_SUPPORT
] = "SYSTEM SUPPORT",
642 [LP8788_HIGH_CURRENT
] = "HIGH CURRENT",
646 lp8788_read_byte(pchg
->lp
, LP8788_CHG_STATUS
, &data
);
647 state
= (data
& LP8788_CHG_STATE_M
) >> LP8788_CHG_STATE_S
;
649 return scnprintf(buf
, PAGE_SIZE
, "%s\n", desc
[state
]);
652 static ssize_t
lp8788_show_eoc_time(struct device
*dev
,
653 struct device_attribute
*attr
, char *buf
)
655 struct lp8788_charger
*pchg
= dev_get_drvdata(dev
);
656 char *stime
[] = { "400ms", "5min", "10min", "15min",
657 "20min", "25min", "30min" "No timeout" };
660 lp8788_read_byte(pchg
->lp
, LP8788_CHG_EOC
, &val
);
661 val
= (val
& LP8788_CHG_EOC_TIME_M
) >> LP8788_CHG_EOC_TIME_S
;
663 return scnprintf(buf
, PAGE_SIZE
, "End Of Charge Time: %s\n",
667 static ssize_t
lp8788_show_eoc_level(struct device
*dev
,
668 struct device_attribute
*attr
, char *buf
)
670 struct lp8788_charger
*pchg
= dev_get_drvdata(dev
);
671 char *abs_level
[] = { "25mA", "49mA", "75mA", "98mA" };
672 char *relative_level
[] = { "5%", "10%", "15%", "20%" };
677 lp8788_read_byte(pchg
->lp
, LP8788_CHG_EOC
, &val
);
679 mode
= val
& LP8788_CHG_EOC_MODE_M
;
680 val
= (val
& LP8788_CHG_EOC_LEVEL_M
) >> LP8788_CHG_EOC_LEVEL_S
;
681 level
= mode
? abs_level
[val
] : relative_level
[val
];
683 return scnprintf(buf
, PAGE_SIZE
, "End Of Charge Level: %s\n", level
);
686 static DEVICE_ATTR(charger_status
, S_IRUSR
, lp8788_show_charger_status
, NULL
);
687 static DEVICE_ATTR(eoc_time
, S_IRUSR
, lp8788_show_eoc_time
, NULL
);
688 static DEVICE_ATTR(eoc_level
, S_IRUSR
, lp8788_show_eoc_level
, NULL
);
690 static struct attribute
*lp8788_charger_attr
[] = {
691 &dev_attr_charger_status
.attr
,
692 &dev_attr_eoc_time
.attr
,
693 &dev_attr_eoc_level
.attr
,
697 static const struct attribute_group lp8788_attr_group
= {
698 .attrs
= lp8788_charger_attr
,
701 static int lp8788_charger_probe(struct platform_device
*pdev
)
703 struct lp8788
*lp
= dev_get_drvdata(pdev
->dev
.parent
);
704 struct lp8788_charger
*pchg
;
705 struct device
*dev
= &pdev
->dev
;
708 pchg
= devm_kzalloc(dev
, sizeof(struct lp8788_charger
), GFP_KERNEL
);
713 pchg
->pdata
= lp
->pdata
? lp
->pdata
->chg_pdata
: NULL
;
714 platform_set_drvdata(pdev
, pchg
);
716 ret
= lp8788_update_charger_params(pdev
, pchg
);
720 lp8788_setup_adc_channel(&pdev
->dev
, pchg
);
722 ret
= lp8788_psy_register(pdev
, pchg
);
726 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &lp8788_attr_group
);
728 lp8788_psy_unregister(pchg
);
732 ret
= lp8788_irq_register(pdev
, pchg
);
734 dev_warn(dev
, "failed to register charger irq: %d\n", ret
);
739 static int lp8788_charger_remove(struct platform_device
*pdev
)
741 struct lp8788_charger
*pchg
= platform_get_drvdata(pdev
);
743 flush_work(&pchg
->charger_work
);
744 lp8788_irq_unregister(pdev
, pchg
);
745 sysfs_remove_group(&pdev
->dev
.kobj
, &lp8788_attr_group
);
746 lp8788_psy_unregister(pchg
);
747 lp8788_release_adc_channel(pchg
);
752 static struct platform_driver lp8788_charger_driver
= {
753 .probe
= lp8788_charger_probe
,
754 .remove
= lp8788_charger_remove
,
756 .name
= LP8788_DEV_CHARGER
,
759 module_platform_driver(lp8788_charger_driver
);
761 MODULE_DESCRIPTION("TI LP8788 Charger Driver");
762 MODULE_AUTHOR("Milo Kim");
763 MODULE_LICENSE("GPL");
764 MODULE_ALIAS("platform:lp8788-charger");