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_BUF_SIZE 40
53 #define LP8788_ISEL_MAX 23
54 #define LP8788_ISEL_STEP 50
55 #define LP8788_VTERM_MIN 4100
56 #define LP8788_VTERM_STEP 25
57 #define LP8788_MAX_BATT_CAPACITY 100
58 #define LP8788_MAX_CHG_IRQS 11
60 enum lp8788_charging_state {
63 LP8788_LOW_INPUT = 0x3,
69 LP8788_SYSTEM_SUPPORT = 0xC,
70 LP8788_HIGH_CURRENT = 0xF,
74 enum lp8788_charger_adc_sel {
80 enum lp8788_charger_input_state {
81 LP8788_SYSTEM_SUPPLY = 1,
86 * struct lp8788_chg_irq
87 * @which : lp8788 interrupt id
88 * @virq : Linux IRQ number from irq_domain
90 struct lp8788_chg_irq {
91 enum lp8788_int_id which;
96 * struct lp8788_charger
97 * @lp : used for accessing the registers of mfd lp8788 device
98 * @charger : power supply driver for the battery charger
99 * @battery : power supply driver for the battery
100 * @charger_work : work queue for charger input interrupts
101 * @chan : iio channels for getting adc values
102 * eg) battery voltage, capacity and temperature
103 * @irqs : charger dedicated interrupts
104 * @num_irqs : total numbers of charger interrupts
105 * @pdata : charger platform specific data
107 struct lp8788_charger {
109 struct power_supply charger;
110 struct power_supply battery;
111 struct work_struct charger_work;
112 struct iio_channel *chan[LP8788_NUM_CHG_ADC];
113 struct lp8788_chg_irq irqs[LP8788_MAX_CHG_IRQS];
115 struct lp8788_charger_platform_data *pdata;
118 static char *battery_supplied_to[] = {
122 static enum power_supply_property lp8788_charger_prop[] = {
123 POWER_SUPPLY_PROP_ONLINE,
124 POWER_SUPPLY_PROP_CURRENT_MAX,
127 static enum power_supply_property lp8788_battery_prop[] = {
128 POWER_SUPPLY_PROP_STATUS,
129 POWER_SUPPLY_PROP_HEALTH,
130 POWER_SUPPLY_PROP_PRESENT,
131 POWER_SUPPLY_PROP_VOLTAGE_NOW,
132 POWER_SUPPLY_PROP_CAPACITY,
133 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
134 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
135 POWER_SUPPLY_PROP_TEMP,
138 static bool lp8788_is_charger_detected(struct lp8788_charger *pchg)
142 lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data);
143 data &= LP8788_CHG_INPUT_STATE_M;
145 return data == LP8788_SYSTEM_SUPPLY || data == LP8788_FULL_FUNCTION;
148 static int lp8788_charger_get_property(struct power_supply *psy,
149 enum power_supply_property psp,
150 union power_supply_propval *val)
152 struct lp8788_charger *pchg = dev_get_drvdata(psy->dev->parent);
156 case POWER_SUPPLY_PROP_ONLINE:
157 val->intval = lp8788_is_charger_detected(pchg);
159 case POWER_SUPPLY_PROP_CURRENT_MAX:
160 lp8788_read_byte(pchg->lp, LP8788_CHG_IDCIN, &read);
161 val->intval = LP8788_ISEL_STEP *
162 (min_t(int, read, LP8788_ISEL_MAX) + 1);
171 static int lp8788_get_battery_status(struct lp8788_charger *pchg,
172 union power_supply_propval *val)
174 enum lp8788_charging_state state;
178 ret = lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data);
182 state = (data & LP8788_CHG_STATE_M) >> LP8788_CHG_STATE_S;
185 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
187 case LP8788_PRECHARGE:
190 case LP8788_HIGH_CURRENT:
191 val->intval = POWER_SUPPLY_STATUS_CHARGING;
193 case LP8788_MAINTENANCE:
194 val->intval = POWER_SUPPLY_STATUS_FULL;
197 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
204 static int lp8788_get_battery_health(struct lp8788_charger *pchg,
205 union power_supply_propval *val)
210 ret = lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data);
214 if (data & LP8788_NO_BATT_M)
215 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
216 else if (data & LP8788_BAD_BATT_M)
217 val->intval = POWER_SUPPLY_HEALTH_DEAD;
219 val->intval = POWER_SUPPLY_HEALTH_GOOD;
224 static int lp8788_get_battery_present(struct lp8788_charger *pchg,
225 union power_supply_propval *val)
230 ret = lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data);
234 val->intval = !(data & LP8788_NO_BATT_M);
238 static int lp8788_get_vbatt_adc(struct lp8788_charger *pchg,
239 unsigned int *result)
241 struct iio_channel *channel = pchg->chan[LP8788_VBATT];
249 ret = iio_read_channel_scale(channel, &scaleint, &scalepart);
250 if (ret != IIO_VAL_INT_PLUS_MICRO)
254 *result = (scaleint + scalepart * 1000000) / 1000;
259 static int lp8788_get_battery_voltage(struct lp8788_charger *pchg,
260 union power_supply_propval *val)
262 return lp8788_get_vbatt_adc(pchg, &val->intval);
265 static int lp8788_get_battery_capacity(struct lp8788_charger *pchg,
266 union power_supply_propval *val)
268 struct lp8788 *lp = pchg->lp;
269 struct lp8788_charger_platform_data *pdata = pchg->pdata;
270 unsigned int max_vbatt;
272 enum lp8788_charging_state state;
279 max_vbatt = pdata->max_vbatt_mv;
283 ret = lp8788_read_byte(lp, LP8788_CHG_STATUS, &data);
287 state = (data & LP8788_CHG_STATE_M) >> LP8788_CHG_STATE_S;
289 if (state == LP8788_MAINTENANCE) {
290 val->intval = LP8788_MAX_BATT_CAPACITY;
292 ret = lp8788_get_vbatt_adc(pchg, &vbatt);
296 val->intval = (vbatt * LP8788_MAX_BATT_CAPACITY) / max_vbatt;
297 val->intval = min(val->intval, LP8788_MAX_BATT_CAPACITY);
303 static int lp8788_get_battery_temperature(struct lp8788_charger *pchg,
304 union power_supply_propval *val)
306 struct iio_channel *channel = pchg->chan[LP8788_BATT_TEMP];
314 ret = iio_read_channel_scale(channel, &scaleint, &scalepart);
315 if (ret != IIO_VAL_INT_PLUS_MICRO)
319 val->intval = (scaleint + scalepart * 1000000) / 100;
324 static int lp8788_get_battery_charging_current(struct lp8788_charger *pchg,
325 union power_supply_propval *val)
329 lp8788_read_byte(pchg->lp, LP8788_CHG_IBATT, &read);
330 read &= LP8788_CHG_IBATT_M;
331 val->intval = LP8788_ISEL_STEP *
332 (min_t(int, read, LP8788_ISEL_MAX) + 1);
337 static int lp8788_get_charging_termination_voltage(struct lp8788_charger *pchg,
338 union power_supply_propval *val)
342 lp8788_read_byte(pchg->lp, LP8788_CHG_VTERM, &read);
343 read &= LP8788_CHG_VTERM_M;
344 val->intval = LP8788_VTERM_MIN + LP8788_VTERM_STEP * read;
349 static int lp8788_battery_get_property(struct power_supply *psy,
350 enum power_supply_property psp,
351 union power_supply_propval *val)
353 struct lp8788_charger *pchg = dev_get_drvdata(psy->dev->parent);
356 case POWER_SUPPLY_PROP_STATUS:
357 return lp8788_get_battery_status(pchg, val);
358 case POWER_SUPPLY_PROP_HEALTH:
359 return lp8788_get_battery_health(pchg, val);
360 case POWER_SUPPLY_PROP_PRESENT:
361 return lp8788_get_battery_present(pchg, val);
362 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
363 return lp8788_get_battery_voltage(pchg, val);
364 case POWER_SUPPLY_PROP_CAPACITY:
365 return lp8788_get_battery_capacity(pchg, val);
366 case POWER_SUPPLY_PROP_TEMP:
367 return lp8788_get_battery_temperature(pchg, val);
368 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
369 return lp8788_get_battery_charging_current(pchg, val);
370 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
371 return lp8788_get_charging_termination_voltage(pchg, val);
377 static inline bool lp8788_is_valid_charger_register(u8 addr)
379 return addr >= LP8788_CHG_START && addr <= LP8788_CHG_END;
382 static int lp8788_update_charger_params(struct lp8788_charger *pchg)
384 struct lp8788 *lp = pchg->lp;
385 struct lp8788_charger_platform_data *pdata = pchg->pdata;
386 struct lp8788_chg_param *param;
390 if (!pdata || !pdata->chg_params) {
391 dev_info(lp->dev, "skip updating charger parameters\n");
395 /* settting charging parameters */
396 for (i = 0; i < pdata->num_chg_params; i++) {
397 param = pdata->chg_params + i;
402 if (lp8788_is_valid_charger_register(param->addr)) {
403 ret = lp8788_write_byte(lp, param->addr, param->val);
412 static int lp8788_psy_register(struct platform_device *pdev,
413 struct lp8788_charger *pchg)
415 pchg->charger.name = LP8788_CHARGER_NAME;
416 pchg->charger.type = POWER_SUPPLY_TYPE_MAINS;
417 pchg->charger.properties = lp8788_charger_prop;
418 pchg->charger.num_properties = ARRAY_SIZE(lp8788_charger_prop);
419 pchg->charger.get_property = lp8788_charger_get_property;
420 pchg->charger.supplied_to = battery_supplied_to;
421 pchg->charger.num_supplicants = ARRAY_SIZE(battery_supplied_to);
423 if (power_supply_register(&pdev->dev, &pchg->charger))
426 pchg->battery.name = LP8788_BATTERY_NAME;
427 pchg->battery.type = POWER_SUPPLY_TYPE_BATTERY;
428 pchg->battery.properties = lp8788_battery_prop;
429 pchg->battery.num_properties = ARRAY_SIZE(lp8788_battery_prop);
430 pchg->battery.get_property = lp8788_battery_get_property;
432 if (power_supply_register(&pdev->dev, &pchg->battery))
438 static void lp8788_psy_unregister(struct lp8788_charger *pchg)
440 power_supply_unregister(&pchg->battery);
441 power_supply_unregister(&pchg->charger);
444 static void lp8788_charger_event(struct work_struct *work)
446 struct lp8788_charger *pchg =
447 container_of(work, struct lp8788_charger, charger_work);
448 struct lp8788_charger_platform_data *pdata = pchg->pdata;
449 enum lp8788_charger_event event = lp8788_is_charger_detected(pchg);
451 pdata->charger_event(pchg->lp, event);
454 static bool lp8788_find_irq_id(struct lp8788_charger *pchg, int virq, int *id)
459 for (i = 0; i < pchg->num_irqs; i++) {
460 if (pchg->irqs[i].virq == virq) {
461 *id = pchg->irqs[i].which;
470 static irqreturn_t lp8788_charger_irq_thread(int virq, void *ptr)
472 struct lp8788_charger *pchg = ptr;
473 struct lp8788_charger_platform_data *pdata = pchg->pdata;
476 if (!lp8788_find_irq_id(pchg, virq, &id))
480 case LP8788_INT_CHG_INPUT_STATE:
481 case LP8788_INT_CHG_STATE:
483 case LP8788_INT_BATT_LOW:
484 case LP8788_INT_NO_BATT:
485 power_supply_changed(&pchg->charger);
486 power_supply_changed(&pchg->battery);
492 /* report charger dectection event if used */
496 if (pdata->charger_event && id == LP8788_INT_CHG_INPUT_STATE)
497 schedule_work(&pchg->charger_work);
503 static int lp8788_set_irqs(struct platform_device *pdev,
504 struct lp8788_charger *pchg, const char *name)
507 struct irq_domain *irqdm = pchg->lp->irqdm;
515 /* no error even if no irq resource */
516 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, name);
520 irq_start = r->start;
523 for (i = irq_start; i <= irq_end; i++) {
524 nr_irq = pchg->num_irqs;
526 virq = irq_create_mapping(irqdm, i);
527 pchg->irqs[nr_irq].virq = virq;
528 pchg->irqs[nr_irq].which = i;
531 ret = request_threaded_irq(virq, NULL,
532 lp8788_charger_irq_thread,
544 for (i = 0; i < pchg->num_irqs; i++)
545 free_irq(pchg->irqs[i].virq, pchg);
549 static int lp8788_irq_register(struct platform_device *pdev,
550 struct lp8788_charger *pchg)
552 struct lp8788 *lp = pchg->lp;
553 const char *name[] = {
554 LP8788_CHG_IRQ, LP8788_PRSW_IRQ, LP8788_BATT_IRQ
559 INIT_WORK(&pchg->charger_work, lp8788_charger_event);
562 for (i = 0; i < ARRAY_SIZE(name); i++) {
563 ret = lp8788_set_irqs(pdev, pchg, name[i]);
565 dev_warn(lp->dev, "irq setup failed: %s\n", name[i]);
570 if (pchg->num_irqs > LP8788_MAX_CHG_IRQS) {
571 dev_err(lp->dev, "invalid total number of irqs: %d\n",
580 static void lp8788_irq_unregister(struct platform_device *pdev,
581 struct lp8788_charger *pchg)
586 for (i = 0; i < pchg->num_irqs; i++) {
587 irq = pchg->irqs[i].virq;
595 static void lp8788_setup_adc_channel(struct lp8788_charger *pchg)
597 struct lp8788_charger_platform_data *pdata = pchg->pdata;
598 struct device *dev = pchg->lp->dev;
599 struct iio_channel *chan;
600 enum lp8788_adc_id id;
601 const char *chan_name[LPADC_MAX] = {
602 [LPADC_VBATT_5P5] = "vbatt-5p5",
603 [LPADC_VBATT_6P0] = "vbatt-6p0",
604 [LPADC_VBATT_5P0] = "vbatt-5p0",
605 [LPADC_ADC1] = "adc1",
606 [LPADC_ADC2] = "adc2",
607 [LPADC_ADC3] = "adc3",
608 [LPADC_ADC4] = "adc4",
614 id = pdata->vbatt_adc;
616 case LPADC_VBATT_5P5:
617 case LPADC_VBATT_6P0:
618 case LPADC_VBATT_5P0:
619 chan = iio_channel_get(NULL, chan_name[id]);
620 pchg->chan[LP8788_VBATT] = IS_ERR(chan) ? NULL : chan;
623 dev_err(dev, "invalid ADC id for VBATT: %d\n", id);
624 pchg->chan[LP8788_VBATT] = NULL;
628 id = pdata->batt_temp_adc;
634 chan = iio_channel_get(NULL, chan_name[id]);
635 pchg->chan[LP8788_BATT_TEMP] = IS_ERR(chan) ? NULL : chan;
638 dev_err(dev, "invalid ADC id for BATT_TEMP : %d\n", id);
639 pchg->chan[LP8788_BATT_TEMP] = NULL;
644 static void lp8788_release_adc_channel(struct lp8788_charger *pchg)
648 for (i = 0; i < LP8788_NUM_CHG_ADC; i++) {
652 iio_channel_release(pchg->chan[i]);
653 pchg->chan[i] = NULL;
657 static ssize_t lp8788_show_charger_status(struct device *dev,
658 struct device_attribute *attr, char *buf)
660 struct lp8788_charger *pchg = dev_get_drvdata(dev);
661 enum lp8788_charging_state state;
662 char *desc[LP8788_MAX_CHG_STATE] = {
663 [LP8788_OFF] = "CHARGER OFF",
664 [LP8788_WARM_UP] = "WARM UP",
665 [LP8788_LOW_INPUT] = "LOW INPUT STATE",
666 [LP8788_PRECHARGE] = "CHARGING - PRECHARGE",
667 [LP8788_CC] = "CHARGING - CC",
668 [LP8788_CV] = "CHARGING - CV",
669 [LP8788_MAINTENANCE] = "NO CHARGING - MAINTENANCE",
670 [LP8788_BATTERY_FAULT] = "BATTERY FAULT",
671 [LP8788_SYSTEM_SUPPORT] = "SYSTEM SUPPORT",
672 [LP8788_HIGH_CURRENT] = "HIGH CURRENT",
676 lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data);
677 state = (data & LP8788_CHG_STATE_M) >> LP8788_CHG_STATE_S;
679 return scnprintf(buf, LP8788_BUF_SIZE, "%s\n", desc[state]);
682 static ssize_t lp8788_show_eoc_time(struct device *dev,
683 struct device_attribute *attr, char *buf)
685 struct lp8788_charger *pchg = dev_get_drvdata(dev);
686 char *stime[] = { "400ms", "5min", "10min", "15min",
687 "20min", "25min", "30min" "No timeout" };
690 lp8788_read_byte(pchg->lp, LP8788_CHG_EOC, &val);
691 val = (val & LP8788_CHG_EOC_TIME_M) >> LP8788_CHG_EOC_TIME_S;
693 return scnprintf(buf, LP8788_BUF_SIZE, "End Of Charge Time: %s\n",
697 static ssize_t lp8788_show_eoc_level(struct device *dev,
698 struct device_attribute *attr, char *buf)
700 struct lp8788_charger *pchg = dev_get_drvdata(dev);
701 char *abs_level[] = { "25mA", "49mA", "75mA", "98mA" };
702 char *relative_level[] = { "5%", "10%", "15%", "20%" };
707 lp8788_read_byte(pchg->lp, LP8788_CHG_EOC, &val);
709 mode = val & LP8788_CHG_EOC_MODE_M;
710 val = (val & LP8788_CHG_EOC_LEVEL_M) >> LP8788_CHG_EOC_LEVEL_S;
711 level = mode ? abs_level[val] : relative_level[val];
713 return scnprintf(buf, LP8788_BUF_SIZE, "End Of Charge Level: %s\n",
717 static DEVICE_ATTR(charger_status, S_IRUSR, lp8788_show_charger_status, NULL);
718 static DEVICE_ATTR(eoc_time, S_IRUSR, lp8788_show_eoc_time, NULL);
719 static DEVICE_ATTR(eoc_level, S_IRUSR, lp8788_show_eoc_level, NULL);
721 static struct attribute *lp8788_charger_attr[] = {
722 &dev_attr_charger_status.attr,
723 &dev_attr_eoc_time.attr,
724 &dev_attr_eoc_level.attr,
728 static const struct attribute_group lp8788_attr_group = {
729 .attrs = lp8788_charger_attr,
732 static __devinit int lp8788_charger_probe(struct platform_device *pdev)
734 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
735 struct lp8788_charger *pchg;
738 pchg = devm_kzalloc(lp->dev, sizeof(struct lp8788_charger), GFP_KERNEL);
743 pchg->pdata = lp->pdata ? lp->pdata->chg_pdata : NULL;
744 platform_set_drvdata(pdev, pchg);
746 ret = lp8788_update_charger_params(pchg);
750 lp8788_setup_adc_channel(pchg);
752 ret = lp8788_psy_register(pdev, pchg);
756 ret = sysfs_create_group(&pdev->dev.kobj, &lp8788_attr_group);
758 lp8788_psy_unregister(pchg);
762 ret = lp8788_irq_register(pdev, pchg);
764 dev_warn(lp->dev, "failed to register charger irq: %d\n", ret);
769 static int __devexit lp8788_charger_remove(struct platform_device *pdev)
771 struct lp8788_charger *pchg = platform_get_drvdata(pdev);
773 flush_work(&pchg->charger_work);
774 lp8788_irq_unregister(pdev, pchg);
775 sysfs_remove_group(&pdev->dev.kobj, &lp8788_attr_group);
776 lp8788_psy_unregister(pchg);
777 lp8788_release_adc_channel(pchg);
782 static struct platform_driver lp8788_charger_driver = {
783 .probe = lp8788_charger_probe,
784 .remove = __devexit_p(lp8788_charger_remove),
786 .name = LP8788_DEV_CHARGER,
787 .owner = THIS_MODULE,
790 module_platform_driver(lp8788_charger_driver);
792 MODULE_DESCRIPTION("TI LP8788 Charger Driver");
793 MODULE_AUTHOR("Milo Kim");
794 MODULE_LICENSE("GPL");
795 MODULE_ALIAS("platform:lp8788-charger");