2 * max77693_charger.c - Battery charger driver for the Maxim 77693
4 * Copyright (C) 2014 Samsung Electronics
5 * Krzysztof Kozlowski <k.kozlowski@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h>
21 #include <linux/regmap.h>
22 #include <linux/mfd/max77693.h>
23 #include <linux/mfd/max77693-private.h>
25 static const char *max77693_charger_name = "max77693-charger";
26 static const char *max77693_charger_model = "MAX77693";
27 static const char *max77693_charger_manufacturer = "Maxim Integrated";
29 struct max77693_charger {
31 struct max77693_dev *max77693;
32 struct power_supply charger;
36 u32 thermal_regulation_temp;
37 u32 batttery_overcurrent;
38 u32 charge_input_threshold_volt;
41 static int max77693_get_charger_state(struct regmap *regmap)
46 if (regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data) < 0)
47 return POWER_SUPPLY_STATUS_UNKNOWN;
49 data &= CHG_DETAILS_01_CHG_MASK;
50 data >>= CHG_DETAILS_01_CHG_SHIFT;
53 case MAX77693_CHARGING_PREQUALIFICATION:
54 case MAX77693_CHARGING_FAST_CONST_CURRENT:
55 case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
56 case MAX77693_CHARGING_TOP_OFF:
57 /* In high temp the charging current is reduced, but still charging */
58 case MAX77693_CHARGING_HIGH_TEMP:
59 state = POWER_SUPPLY_STATUS_CHARGING;
61 case MAX77693_CHARGING_DONE:
62 state = POWER_SUPPLY_STATUS_FULL;
64 case MAX77693_CHARGING_TIMER_EXPIRED:
65 case MAX77693_CHARGING_THERMISTOR_SUSPEND:
66 state = POWER_SUPPLY_STATUS_NOT_CHARGING;
68 case MAX77693_CHARGING_OFF:
69 case MAX77693_CHARGING_OVER_TEMP:
70 case MAX77693_CHARGING_WATCHDOG_EXPIRED:
71 state = POWER_SUPPLY_STATUS_DISCHARGING;
73 case MAX77693_CHARGING_RESERVED:
75 state = POWER_SUPPLY_STATUS_UNKNOWN;
81 static int max77693_get_charge_type(struct regmap *regmap)
86 if (regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data) < 0)
87 return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
89 data &= CHG_DETAILS_01_CHG_MASK;
90 data >>= CHG_DETAILS_01_CHG_SHIFT;
93 case MAX77693_CHARGING_PREQUALIFICATION:
95 * Top-off: trickle or fast? In top-off the current varies between
96 * 100 and 250 mA. It is higher than prequalification current.
98 case MAX77693_CHARGING_TOP_OFF:
99 state = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
101 case MAX77693_CHARGING_FAST_CONST_CURRENT:
102 case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
103 /* In high temp the charging current is reduced, but still charging */
104 case MAX77693_CHARGING_HIGH_TEMP:
105 state = POWER_SUPPLY_CHARGE_TYPE_FAST;
107 case MAX77693_CHARGING_DONE:
108 case MAX77693_CHARGING_TIMER_EXPIRED:
109 case MAX77693_CHARGING_THERMISTOR_SUSPEND:
110 case MAX77693_CHARGING_OFF:
111 case MAX77693_CHARGING_OVER_TEMP:
112 case MAX77693_CHARGING_WATCHDOG_EXPIRED:
113 state = POWER_SUPPLY_CHARGE_TYPE_NONE;
115 case MAX77693_CHARGING_RESERVED:
117 state = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
124 * Supported health statuses:
125 * - POWER_SUPPLY_HEALTH_DEAD
126 * - POWER_SUPPLY_HEALTH_GOOD
127 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
128 * - POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
129 * - POWER_SUPPLY_HEALTH_UNKNOWN
130 * - POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
132 static int max77693_get_battery_health(struct regmap *regmap)
137 if (regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data) < 0)
138 return POWER_SUPPLY_HEALTH_UNKNOWN;
140 data &= CHG_DETAILS_01_BAT_MASK;
141 data >>= CHG_DETAILS_01_BAT_SHIFT;
144 case MAX77693_BATTERY_NOBAT:
145 state = POWER_SUPPLY_HEALTH_DEAD;
147 case MAX77693_BATTERY_PREQUALIFICATION:
148 case MAX77693_BATTERY_GOOD:
149 case MAX77693_BATTERY_LOWVOLTAGE:
150 state = POWER_SUPPLY_HEALTH_GOOD;
152 case MAX77693_BATTERY_TIMER_EXPIRED:
154 * Took longer to charge than expected, charging suspended.
157 state = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
159 case MAX77693_BATTERY_OVERVOLTAGE:
160 state = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
162 case MAX77693_BATTERY_OVERCURRENT:
163 state = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
165 case MAX77693_BATTERY_RESERVED:
167 state = POWER_SUPPLY_HEALTH_UNKNOWN;
174 static int max77693_get_present(struct regmap *regmap)
179 * Read CHG_INT_OK register. High DETBAT bit here should be
180 * equal to value 0x0 in CHG_DETAILS_01/BAT field.
182 regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
183 if (data & CHG_INT_OK_DETBAT_MASK)
188 static int max77693_get_online(struct regmap *regmap)
192 regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
193 if (data & CHG_INT_OK_CHGIN_MASK)
198 static enum power_supply_property max77693_charger_props[] = {
199 POWER_SUPPLY_PROP_STATUS,
200 POWER_SUPPLY_PROP_CHARGE_TYPE,
201 POWER_SUPPLY_PROP_HEALTH,
202 POWER_SUPPLY_PROP_PRESENT,
203 POWER_SUPPLY_PROP_ONLINE,
204 POWER_SUPPLY_PROP_MODEL_NAME,
205 POWER_SUPPLY_PROP_MANUFACTURER,
208 static int max77693_charger_get_property(struct power_supply *psy,
209 enum power_supply_property psp,
210 union power_supply_propval *val)
212 struct max77693_charger *chg = container_of(psy,
213 struct max77693_charger,
215 struct regmap *regmap = chg->max77693->regmap;
219 case POWER_SUPPLY_PROP_STATUS:
220 val->intval = max77693_get_charger_state(regmap);
222 case POWER_SUPPLY_PROP_CHARGE_TYPE:
223 val->intval = max77693_get_charge_type(regmap);
225 case POWER_SUPPLY_PROP_HEALTH:
226 val->intval = max77693_get_battery_health(regmap);
228 case POWER_SUPPLY_PROP_PRESENT:
229 val->intval = max77693_get_present(regmap);
231 case POWER_SUPPLY_PROP_ONLINE:
232 val->intval = max77693_get_online(regmap);
234 case POWER_SUPPLY_PROP_MODEL_NAME:
235 val->strval = max77693_charger_model;
237 case POWER_SUPPLY_PROP_MANUFACTURER:
238 val->strval = max77693_charger_manufacturer;
247 static ssize_t device_attr_store(struct device *dev,
248 struct device_attribute *attr, const char *buf, size_t count,
249 int (*fn)(struct max77693_charger *, unsigned long))
251 struct max77693_charger *chg = dev_get_drvdata(dev);
255 ret = kstrtoul(buf, 10, &val);
266 static ssize_t fast_charge_timer_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
269 struct max77693_charger *chg = dev_get_drvdata(dev);
270 unsigned int data, val;
273 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_01,
278 data &= CHG_CNFG_01_FCHGTIME_MASK;
279 data >>= CHG_CNFG_01_FCHGTIME_SHIFT;
282 /* Starting from 4 hours, step by 2 hours */
283 val = 4 + (data - 1) * 2;
291 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
294 static int max77693_set_fast_charge_timer(struct max77693_charger *chg,
305 * Round down odd values.
309 data = (hours - 4) / 2 + 1;
318 data <<= CHG_CNFG_01_FCHGTIME_SHIFT;
320 return regmap_update_bits(chg->max77693->regmap,
321 MAX77693_CHG_REG_CHG_CNFG_01,
322 CHG_CNFG_01_FCHGTIME_MASK, data);
325 static ssize_t fast_charge_timer_store(struct device *dev,
326 struct device_attribute *attr, const char *buf, size_t count)
328 return device_attr_store(dev, attr, buf, count,
329 max77693_set_fast_charge_timer);
332 static ssize_t top_off_threshold_current_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
335 struct max77693_charger *chg = dev_get_drvdata(dev);
336 unsigned int data, val;
339 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
344 data &= CHG_CNFG_03_TOITH_MASK;
345 data >>= CHG_CNFG_03_TOITH_SHIFT;
348 val = 100000 + data * 25000;
352 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
355 static int max77693_set_top_off_threshold_current(struct max77693_charger *chg,
360 if (uamp < 100000 || uamp > 350000)
364 data = (uamp - 100000) / 25000;
366 /* (200000, 350000> */
369 data <<= CHG_CNFG_03_TOITH_SHIFT;
371 return regmap_update_bits(chg->max77693->regmap,
372 MAX77693_CHG_REG_CHG_CNFG_03,
373 CHG_CNFG_03_TOITH_MASK, data);
376 static ssize_t top_off_threshold_current_store(struct device *dev,
377 struct device_attribute *attr, const char *buf, size_t count)
379 return device_attr_store(dev, attr, buf, count,
380 max77693_set_top_off_threshold_current);
383 static ssize_t top_off_timer_show(struct device *dev,
384 struct device_attribute *attr, char *buf)
386 struct max77693_charger *chg = dev_get_drvdata(dev);
387 unsigned int data, val;
390 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
395 data &= CHG_CNFG_03_TOTIME_MASK;
396 data >>= CHG_CNFG_03_TOTIME_SHIFT;
400 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
403 static int max77693_set_top_off_timer(struct max77693_charger *chg,
404 unsigned long minutes)
412 data <<= CHG_CNFG_03_TOTIME_SHIFT;
414 return regmap_update_bits(chg->max77693->regmap,
415 MAX77693_CHG_REG_CHG_CNFG_03,
416 CHG_CNFG_03_TOTIME_MASK, data);
419 static ssize_t top_off_timer_store(struct device *dev,
420 struct device_attribute *attr, const char *buf, size_t count)
422 return device_attr_store(dev, attr, buf, count,
423 max77693_set_top_off_timer);
426 static DEVICE_ATTR_RW(fast_charge_timer);
427 static DEVICE_ATTR_RW(top_off_threshold_current);
428 static DEVICE_ATTR_RW(top_off_timer);
430 static int max77693_set_constant_volt(struct max77693_charger *chg,
445 if (uvolt >= 3650000 && uvolt < 4340000)
446 data = (uvolt - 3650000) / 25000;
447 else if (uvolt >= 4340000 && uvolt < 4350000)
449 else if (uvolt >= 4350000 && uvolt <= 4400000)
450 data = 0x1d + (uvolt - 4350000) / 25000;
452 dev_err(chg->dev, "Wrong value for charging constant voltage\n");
456 data <<= CHG_CNFG_04_CHGCVPRM_SHIFT;
458 dev_dbg(chg->dev, "Charging constant voltage: %u (0x%x)\n", uvolt,
461 return regmap_update_bits(chg->max77693->regmap,
462 MAX77693_CHG_REG_CHG_CNFG_04,
463 CHG_CNFG_04_CHGCVPRM_MASK, data);
466 static int max77693_set_min_system_volt(struct max77693_charger *chg,
471 if (uvolt < 3000000 || uvolt > 3700000) {
472 dev_err(chg->dev, "Wrong value for minimum system regulation voltage\n");
476 data = (uvolt - 3000000) / 100000;
478 data <<= CHG_CNFG_04_MINVSYS_SHIFT;
480 dev_dbg(chg->dev, "Minimum system regulation voltage: %u (0x%x)\n",
483 return regmap_update_bits(chg->max77693->regmap,
484 MAX77693_CHG_REG_CHG_CNFG_04,
485 CHG_CNFG_04_MINVSYS_MASK, data);
488 static int max77693_set_thermal_regulation_temp(struct max77693_charger *chg,
498 data = (cels - 70) / 15;
501 dev_err(chg->dev, "Wrong value for thermal regulation loop temperature\n");
505 data <<= CHG_CNFG_07_REGTEMP_SHIFT;
507 dev_dbg(chg->dev, "Thermal regulation loop temperature: %u (0x%x)\n",
510 return regmap_update_bits(chg->max77693->regmap,
511 MAX77693_CHG_REG_CHG_CNFG_07,
512 CHG_CNFG_07_REGTEMP_MASK, data);
515 static int max77693_set_batttery_overcurrent(struct max77693_charger *chg,
520 if (uamp && (uamp < 2000000 || uamp > 3500000)) {
521 dev_err(chg->dev, "Wrong value for battery overcurrent\n");
526 data = ((uamp - 2000000) / 250000) + 1;
528 data = 0; /* disable */
530 data <<= CHG_CNFG_12_B2SOVRC_SHIFT;
532 dev_dbg(chg->dev, "Battery overcurrent: %u (0x%x)\n", uamp, data);
534 return regmap_update_bits(chg->max77693->regmap,
535 MAX77693_CHG_REG_CHG_CNFG_12,
536 CHG_CNFG_12_B2SOVRC_MASK, data);
539 static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg,
551 data = (uvolt - 4700000) / 100000;
553 dev_err(chg->dev, "Wrong value for charge input voltage regulation threshold\n");
557 data <<= CHG_CNFG_12_VCHGINREG_SHIFT;
559 dev_dbg(chg->dev, "Charge input voltage regulation threshold: %u (0x%x)\n",
562 return regmap_update_bits(chg->max77693->regmap,
563 MAX77693_CHG_REG_CHG_CNFG_12,
564 CHG_CNFG_12_VCHGINREG_MASK, data);
568 * Sets charger registers to proper and safe default values.
570 static int max77693_reg_init(struct max77693_charger *chg)
575 /* Unlock charger register protection */
576 data = (0x3 << CHG_CNFG_06_CHGPROT_SHIFT);
577 ret = regmap_update_bits(chg->max77693->regmap,
578 MAX77693_CHG_REG_CHG_CNFG_06,
579 CHG_CNFG_06_CHGPROT_MASK, data);
581 dev_err(chg->dev, "Error unlocking registers: %d\n", ret);
585 ret = max77693_set_fast_charge_timer(chg, DEFAULT_FAST_CHARGE_TIMER);
589 ret = max77693_set_top_off_threshold_current(chg,
590 DEFAULT_TOP_OFF_THRESHOLD_CURRENT);
594 ret = max77693_set_top_off_timer(chg, DEFAULT_TOP_OFF_TIMER);
598 ret = max77693_set_constant_volt(chg, chg->constant_volt);
602 ret = max77693_set_min_system_volt(chg, chg->min_system_volt);
606 ret = max77693_set_thermal_regulation_temp(chg,
607 chg->thermal_regulation_temp);
611 ret = max77693_set_batttery_overcurrent(chg, chg->batttery_overcurrent);
615 return max77693_set_charge_input_threshold_volt(chg,
616 chg->charge_input_threshold_volt);
620 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
622 struct device_node *np = dev->of_node;
625 dev_err(dev, "no charger OF node\n");
629 if (of_property_read_u32(np, "maxim,constant-microvolt",
630 &chg->constant_volt))
631 chg->constant_volt = DEFAULT_CONSTANT_VOLT;
633 if (of_property_read_u32(np, "maxim,min-system-microvolt",
634 &chg->min_system_volt))
635 chg->min_system_volt = DEFAULT_MIN_SYSTEM_VOLT;
637 if (of_property_read_u32(np, "maxim,thermal-regulation-celsius",
638 &chg->thermal_regulation_temp))
639 chg->thermal_regulation_temp = DEFAULT_THERMAL_REGULATION_TEMP;
641 if (of_property_read_u32(np, "maxim,battery-overcurrent-microamp",
642 &chg->batttery_overcurrent))
643 chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT;
645 if (of_property_read_u32(np, "maxim,charge-input-threshold-microvolt",
646 &chg->charge_input_threshold_volt))
647 chg->charge_input_threshold_volt =
648 DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT;
652 #else /* CONFIG_OF */
653 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
657 #endif /* CONFIG_OF */
659 static int max77693_charger_probe(struct platform_device *pdev)
661 struct max77693_charger *chg;
662 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
665 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
669 platform_set_drvdata(pdev, chg);
670 chg->dev = &pdev->dev;
671 chg->max77693 = max77693;
673 ret = max77693_dt_init(&pdev->dev, chg);
677 ret = max77693_reg_init(chg);
681 chg->charger.name = max77693_charger_name;
682 chg->charger.type = POWER_SUPPLY_TYPE_BATTERY;
683 chg->charger.properties = max77693_charger_props;
684 chg->charger.num_properties = ARRAY_SIZE(max77693_charger_props);
685 chg->charger.get_property = max77693_charger_get_property;
687 ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
689 dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n");
693 ret = device_create_file(&pdev->dev,
694 &dev_attr_top_off_threshold_current);
696 dev_err(&pdev->dev, "failed: create top off current sysfs entry\n");
700 ret = device_create_file(&pdev->dev, &dev_attr_top_off_timer);
702 dev_err(&pdev->dev, "failed: create top off timer sysfs entry\n");
706 ret = power_supply_register(&pdev->dev, &chg->charger);
708 dev_err(&pdev->dev, "failed: power supply register\n");
715 device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
716 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
717 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
722 static int max77693_charger_remove(struct platform_device *pdev)
724 struct max77693_charger *chg = platform_get_drvdata(pdev);
726 device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
727 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
728 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
730 power_supply_unregister(&chg->charger);
735 static const struct platform_device_id max77693_charger_id[] = {
736 { "max77693-charger", 0, },
739 MODULE_DEVICE_TABLE(platform, max77693_charger_id);
741 static struct platform_driver max77693_charger_driver = {
743 .name = "max77693-charger",
745 .probe = max77693_charger_probe,
746 .remove = max77693_charger_remove,
747 .id_table = max77693_charger_id,
749 module_platform_driver(max77693_charger_driver);
751 MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
752 MODULE_DESCRIPTION("Maxim 77693 charger driver");
753 MODULE_LICENSE("GPL");