1 // SPDX-License-Identifier: GPL-2.0+
3 // max77693_charger.c - Battery charger driver for the Maxim 77693
5 // Copyright (C) 2014 Samsung Electronics
6 // Krzysztof Kozlowski <krzk@kernel.org>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/power_supply.h>
11 #include <linux/regmap.h>
12 #include <linux/mfd/max77693.h>
13 #include <linux/mfd/max77693-common.h>
14 #include <linux/mfd/max77693-private.h>
16 #define MAX77693_CHARGER_NAME "max77693-charger"
17 static const char *max77693_charger_model = "MAX77693";
18 static const char *max77693_charger_manufacturer = "Maxim Integrated";
20 struct max77693_charger {
22 struct max77693_dev *max77693;
23 struct power_supply *charger;
27 u32 thermal_regulation_temp;
28 u32 batttery_overcurrent;
29 u32 charge_input_threshold_volt;
32 static int max77693_get_charger_state(struct regmap *regmap, int *val)
37 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
41 data &= CHG_DETAILS_01_CHG_MASK;
42 data >>= CHG_DETAILS_01_CHG_SHIFT;
45 case MAX77693_CHARGING_PREQUALIFICATION:
46 case MAX77693_CHARGING_FAST_CONST_CURRENT:
47 case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
48 case MAX77693_CHARGING_TOP_OFF:
49 /* In high temp the charging current is reduced, but still charging */
50 case MAX77693_CHARGING_HIGH_TEMP:
51 *val = POWER_SUPPLY_STATUS_CHARGING;
53 case MAX77693_CHARGING_DONE:
54 *val = POWER_SUPPLY_STATUS_FULL;
56 case MAX77693_CHARGING_TIMER_EXPIRED:
57 case MAX77693_CHARGING_THERMISTOR_SUSPEND:
58 *val = POWER_SUPPLY_STATUS_NOT_CHARGING;
60 case MAX77693_CHARGING_OFF:
61 case MAX77693_CHARGING_OVER_TEMP:
62 case MAX77693_CHARGING_WATCHDOG_EXPIRED:
63 *val = POWER_SUPPLY_STATUS_DISCHARGING;
65 case MAX77693_CHARGING_RESERVED:
67 *val = POWER_SUPPLY_STATUS_UNKNOWN;
73 static int max77693_get_charge_type(struct regmap *regmap, int *val)
78 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
82 data &= CHG_DETAILS_01_CHG_MASK;
83 data >>= CHG_DETAILS_01_CHG_SHIFT;
86 case MAX77693_CHARGING_PREQUALIFICATION:
88 * Top-off: trickle or fast? In top-off the current varies between
89 * 100 and 250 mA. It is higher than prequalification current.
91 case MAX77693_CHARGING_TOP_OFF:
92 *val = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
94 case MAX77693_CHARGING_FAST_CONST_CURRENT:
95 case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
96 /* In high temp the charging current is reduced, but still charging */
97 case MAX77693_CHARGING_HIGH_TEMP:
98 *val = POWER_SUPPLY_CHARGE_TYPE_FAST;
100 case MAX77693_CHARGING_DONE:
101 case MAX77693_CHARGING_TIMER_EXPIRED:
102 case MAX77693_CHARGING_THERMISTOR_SUSPEND:
103 case MAX77693_CHARGING_OFF:
104 case MAX77693_CHARGING_OVER_TEMP:
105 case MAX77693_CHARGING_WATCHDOG_EXPIRED:
106 *val = POWER_SUPPLY_CHARGE_TYPE_NONE;
108 case MAX77693_CHARGING_RESERVED:
110 *val = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
117 * Supported health statuses:
118 * - POWER_SUPPLY_HEALTH_DEAD
119 * - POWER_SUPPLY_HEALTH_GOOD
120 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
121 * - POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
122 * - POWER_SUPPLY_HEALTH_UNKNOWN
123 * - POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
125 static int max77693_get_battery_health(struct regmap *regmap, int *val)
130 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
134 data &= CHG_DETAILS_01_BAT_MASK;
135 data >>= CHG_DETAILS_01_BAT_SHIFT;
138 case MAX77693_BATTERY_NOBAT:
139 *val = POWER_SUPPLY_HEALTH_DEAD;
141 case MAX77693_BATTERY_PREQUALIFICATION:
142 case MAX77693_BATTERY_GOOD:
143 case MAX77693_BATTERY_LOWVOLTAGE:
144 *val = POWER_SUPPLY_HEALTH_GOOD;
146 case MAX77693_BATTERY_TIMER_EXPIRED:
148 * Took longer to charge than expected, charging suspended.
151 *val = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
153 case MAX77693_BATTERY_OVERVOLTAGE:
154 *val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
156 case MAX77693_BATTERY_OVERCURRENT:
157 *val = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
159 case MAX77693_BATTERY_RESERVED:
161 *val = POWER_SUPPLY_HEALTH_UNKNOWN;
168 static int max77693_get_present(struct regmap *regmap, int *val)
174 * Read CHG_INT_OK register. High DETBAT bit here should be
175 * equal to value 0x0 in CHG_DETAILS_01/BAT field.
177 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
181 *val = (data & CHG_INT_OK_DETBAT_MASK) ? 0 : 1;
186 static int max77693_get_online(struct regmap *regmap, int *val)
191 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
195 *val = (data & CHG_INT_OK_CHGIN_MASK) ? 1 : 0;
200 static enum power_supply_property max77693_charger_props[] = {
201 POWER_SUPPLY_PROP_STATUS,
202 POWER_SUPPLY_PROP_CHARGE_TYPE,
203 POWER_SUPPLY_PROP_HEALTH,
204 POWER_SUPPLY_PROP_PRESENT,
205 POWER_SUPPLY_PROP_ONLINE,
206 POWER_SUPPLY_PROP_MODEL_NAME,
207 POWER_SUPPLY_PROP_MANUFACTURER,
210 static int max77693_charger_get_property(struct power_supply *psy,
211 enum power_supply_property psp,
212 union power_supply_propval *val)
214 struct max77693_charger *chg = power_supply_get_drvdata(psy);
215 struct regmap *regmap = chg->max77693->regmap;
219 case POWER_SUPPLY_PROP_STATUS:
220 ret = max77693_get_charger_state(regmap, &val->intval);
222 case POWER_SUPPLY_PROP_CHARGE_TYPE:
223 ret = max77693_get_charge_type(regmap, &val->intval);
225 case POWER_SUPPLY_PROP_HEALTH:
226 ret = max77693_get_battery_health(regmap, &val->intval);
228 case POWER_SUPPLY_PROP_PRESENT:
229 ret = max77693_get_present(regmap, &val->intval);
231 case POWER_SUPPLY_PROP_ONLINE:
232 ret = max77693_get_online(regmap, &val->intval);
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 const struct power_supply_desc max77693_charger_desc = {
248 .name = MAX77693_CHARGER_NAME,
249 .type = POWER_SUPPLY_TYPE_BATTERY,
250 .properties = max77693_charger_props,
251 .num_properties = ARRAY_SIZE(max77693_charger_props),
252 .get_property = max77693_charger_get_property,
255 static ssize_t device_attr_store(struct device *dev,
256 struct device_attribute *attr, const char *buf, size_t count,
257 int (*fn)(struct max77693_charger *, unsigned long))
259 struct max77693_charger *chg = dev_get_drvdata(dev);
263 ret = kstrtoul(buf, 10, &val);
274 static ssize_t fast_charge_timer_show(struct device *dev,
275 struct device_attribute *attr, char *buf)
277 struct max77693_charger *chg = dev_get_drvdata(dev);
278 unsigned int data, val;
281 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_01,
286 data &= CHG_CNFG_01_FCHGTIME_MASK;
287 data >>= CHG_CNFG_01_FCHGTIME_SHIFT;
290 /* Starting from 4 hours, step by 2 hours */
291 val = 4 + (data - 1) * 2;
299 return sysfs_emit(buf, "%u\n", val);
302 static int max77693_set_fast_charge_timer(struct max77693_charger *chg,
313 * Round down odd values.
317 data = (hours - 4) / 2 + 1;
326 data <<= CHG_CNFG_01_FCHGTIME_SHIFT;
328 return regmap_update_bits(chg->max77693->regmap,
329 MAX77693_CHG_REG_CHG_CNFG_01,
330 CHG_CNFG_01_FCHGTIME_MASK, data);
333 static ssize_t fast_charge_timer_store(struct device *dev,
334 struct device_attribute *attr, const char *buf, size_t count)
336 return device_attr_store(dev, attr, buf, count,
337 max77693_set_fast_charge_timer);
340 static ssize_t top_off_threshold_current_show(struct device *dev,
341 struct device_attribute *attr, char *buf)
343 struct max77693_charger *chg = dev_get_drvdata(dev);
344 unsigned int data, val;
347 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
352 data &= CHG_CNFG_03_TOITH_MASK;
353 data >>= CHG_CNFG_03_TOITH_SHIFT;
356 val = 100000 + data * 25000;
360 return sysfs_emit(buf, "%u\n", val);
363 static int max77693_set_top_off_threshold_current(struct max77693_charger *chg,
368 if (uamp < 100000 || uamp > 350000)
372 data = (uamp - 100000) / 25000;
374 /* (200000, 350000> */
377 data <<= CHG_CNFG_03_TOITH_SHIFT;
379 return regmap_update_bits(chg->max77693->regmap,
380 MAX77693_CHG_REG_CHG_CNFG_03,
381 CHG_CNFG_03_TOITH_MASK, data);
384 static ssize_t top_off_threshold_current_store(struct device *dev,
385 struct device_attribute *attr, const char *buf, size_t count)
387 return device_attr_store(dev, attr, buf, count,
388 max77693_set_top_off_threshold_current);
391 static ssize_t top_off_timer_show(struct device *dev,
392 struct device_attribute *attr, char *buf)
394 struct max77693_charger *chg = dev_get_drvdata(dev);
395 unsigned int data, val;
398 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
403 data &= CHG_CNFG_03_TOTIME_MASK;
404 data >>= CHG_CNFG_03_TOTIME_SHIFT;
408 return sysfs_emit(buf, "%u\n", val);
411 static int max77693_set_top_off_timer(struct max77693_charger *chg,
412 unsigned long minutes)
420 data <<= CHG_CNFG_03_TOTIME_SHIFT;
422 return regmap_update_bits(chg->max77693->regmap,
423 MAX77693_CHG_REG_CHG_CNFG_03,
424 CHG_CNFG_03_TOTIME_MASK, data);
427 static ssize_t top_off_timer_store(struct device *dev,
428 struct device_attribute *attr, const char *buf, size_t count)
430 return device_attr_store(dev, attr, buf, count,
431 max77693_set_top_off_timer);
434 static DEVICE_ATTR_RW(fast_charge_timer);
435 static DEVICE_ATTR_RW(top_off_threshold_current);
436 static DEVICE_ATTR_RW(top_off_timer);
438 static int max77693_set_constant_volt(struct max77693_charger *chg,
453 if (uvolt >= 3650000 && uvolt < 4340000)
454 data = (uvolt - 3650000) / 25000;
455 else if (uvolt >= 4340000 && uvolt < 4350000)
457 else if (uvolt >= 4350000 && uvolt <= 4400000)
458 data = 0x1d + (uvolt - 4350000) / 25000;
460 dev_err(chg->dev, "Wrong value for charging constant voltage\n");
464 data <<= CHG_CNFG_04_CHGCVPRM_SHIFT;
466 dev_dbg(chg->dev, "Charging constant voltage: %u (0x%x)\n", uvolt,
469 return regmap_update_bits(chg->max77693->regmap,
470 MAX77693_CHG_REG_CHG_CNFG_04,
471 CHG_CNFG_04_CHGCVPRM_MASK, data);
474 static int max77693_set_min_system_volt(struct max77693_charger *chg,
479 if (uvolt < 3000000 || uvolt > 3700000) {
480 dev_err(chg->dev, "Wrong value for minimum system regulation voltage\n");
484 data = (uvolt - 3000000) / 100000;
486 data <<= CHG_CNFG_04_MINVSYS_SHIFT;
488 dev_dbg(chg->dev, "Minimum system regulation voltage: %u (0x%x)\n",
491 return regmap_update_bits(chg->max77693->regmap,
492 MAX77693_CHG_REG_CHG_CNFG_04,
493 CHG_CNFG_04_MINVSYS_MASK, data);
496 static int max77693_set_thermal_regulation_temp(struct max77693_charger *chg,
506 data = (cels - 70) / 15;
509 dev_err(chg->dev, "Wrong value for thermal regulation loop temperature\n");
513 data <<= CHG_CNFG_07_REGTEMP_SHIFT;
515 dev_dbg(chg->dev, "Thermal regulation loop temperature: %u (0x%x)\n",
518 return regmap_update_bits(chg->max77693->regmap,
519 MAX77693_CHG_REG_CHG_CNFG_07,
520 CHG_CNFG_07_REGTEMP_MASK, data);
523 static int max77693_set_batttery_overcurrent(struct max77693_charger *chg,
528 if (uamp && (uamp < 2000000 || uamp > 3500000)) {
529 dev_err(chg->dev, "Wrong value for battery overcurrent\n");
534 data = ((uamp - 2000000) / 250000) + 1;
536 data = 0; /* disable */
538 data <<= CHG_CNFG_12_B2SOVRC_SHIFT;
540 dev_dbg(chg->dev, "Battery overcurrent: %u (0x%x)\n", uamp, data);
542 return regmap_update_bits(chg->max77693->regmap,
543 MAX77693_CHG_REG_CHG_CNFG_12,
544 CHG_CNFG_12_B2SOVRC_MASK, data);
547 static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg,
559 data = (uvolt - 4700000) / 100000;
562 dev_err(chg->dev, "Wrong value for charge input voltage regulation threshold\n");
566 data <<= CHG_CNFG_12_VCHGINREG_SHIFT;
568 dev_dbg(chg->dev, "Charge input voltage regulation threshold: %u (0x%x)\n",
571 return regmap_update_bits(chg->max77693->regmap,
572 MAX77693_CHG_REG_CHG_CNFG_12,
573 CHG_CNFG_12_VCHGINREG_MASK, data);
577 * Sets charger registers to proper and safe default values.
579 static int max77693_reg_init(struct max77693_charger *chg)
584 /* Unlock charger register protection */
585 data = (0x3 << CHG_CNFG_06_CHGPROT_SHIFT);
586 ret = regmap_update_bits(chg->max77693->regmap,
587 MAX77693_CHG_REG_CHG_CNFG_06,
588 CHG_CNFG_06_CHGPROT_MASK, data);
590 dev_err(chg->dev, "Error unlocking registers: %d\n", ret);
594 ret = max77693_set_fast_charge_timer(chg, DEFAULT_FAST_CHARGE_TIMER);
598 ret = max77693_set_top_off_threshold_current(chg,
599 DEFAULT_TOP_OFF_THRESHOLD_CURRENT);
603 ret = max77693_set_top_off_timer(chg, DEFAULT_TOP_OFF_TIMER);
607 ret = max77693_set_constant_volt(chg, chg->constant_volt);
611 ret = max77693_set_min_system_volt(chg, chg->min_system_volt);
615 ret = max77693_set_thermal_regulation_temp(chg,
616 chg->thermal_regulation_temp);
620 ret = max77693_set_batttery_overcurrent(chg, chg->batttery_overcurrent);
624 return max77693_set_charge_input_threshold_volt(chg,
625 chg->charge_input_threshold_volt);
629 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
631 struct device_node *np = dev->of_node;
634 dev_err(dev, "no charger OF node\n");
638 if (of_property_read_u32(np, "maxim,constant-microvolt",
639 &chg->constant_volt))
640 chg->constant_volt = DEFAULT_CONSTANT_VOLT;
642 if (of_property_read_u32(np, "maxim,min-system-microvolt",
643 &chg->min_system_volt))
644 chg->min_system_volt = DEFAULT_MIN_SYSTEM_VOLT;
646 if (of_property_read_u32(np, "maxim,thermal-regulation-celsius",
647 &chg->thermal_regulation_temp))
648 chg->thermal_regulation_temp = DEFAULT_THERMAL_REGULATION_TEMP;
650 if (of_property_read_u32(np, "maxim,battery-overcurrent-microamp",
651 &chg->batttery_overcurrent))
652 chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT;
654 if (of_property_read_u32(np, "maxim,charge-input-threshold-microvolt",
655 &chg->charge_input_threshold_volt))
656 chg->charge_input_threshold_volt =
657 DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT;
661 #else /* CONFIG_OF */
662 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
666 #endif /* CONFIG_OF */
668 static int max77693_charger_probe(struct platform_device *pdev)
670 struct max77693_charger *chg;
671 struct power_supply_config psy_cfg = {};
672 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
675 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
679 platform_set_drvdata(pdev, chg);
680 chg->dev = &pdev->dev;
681 chg->max77693 = max77693;
683 ret = max77693_dt_init(&pdev->dev, chg);
687 ret = max77693_reg_init(chg);
691 psy_cfg.drv_data = chg;
693 ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
695 dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n");
699 ret = device_create_file(&pdev->dev,
700 &dev_attr_top_off_threshold_current);
702 dev_err(&pdev->dev, "failed: create top off current sysfs entry\n");
706 ret = device_create_file(&pdev->dev, &dev_attr_top_off_timer);
708 dev_err(&pdev->dev, "failed: create top off timer sysfs entry\n");
712 chg->charger = power_supply_register(&pdev->dev,
713 &max77693_charger_desc,
715 if (IS_ERR(chg->charger)) {
716 dev_err(&pdev->dev, "failed: power supply register\n");
717 ret = PTR_ERR(chg->charger);
724 device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
725 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
726 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
731 static void max77693_charger_remove(struct platform_device *pdev)
733 struct max77693_charger *chg = platform_get_drvdata(pdev);
735 device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
736 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
737 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
739 power_supply_unregister(chg->charger);
742 static const struct platform_device_id max77693_charger_id[] = {
743 { "max77693-charger", 0, },
746 MODULE_DEVICE_TABLE(platform, max77693_charger_id);
748 static struct platform_driver max77693_charger_driver = {
750 .name = "max77693-charger",
752 .probe = max77693_charger_probe,
753 .remove_new = max77693_charger_remove,
754 .id_table = max77693_charger_id,
756 module_platform_driver(max77693_charger_driver);
758 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
759 MODULE_DESCRIPTION("Maxim 77693 charger driver");
760 MODULE_LICENSE("GPL");