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;
201 * There are *two* current limit registers:
202 * - CHGIN limit, which limits the input current from the external charger;
203 * - Fast charge current limit, which limits the current going to the battery.
206 static int max77693_get_input_current_limit(struct regmap
*regmap
, int *val
)
211 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_CNFG_09
, &data
);
215 data
&= CHG_CNFG_09_CHGIN_ILIM_MASK
;
216 data
>>= CHG_CNFG_09_CHGIN_ILIM_SHIFT
;
219 /* The first four values (0x00..0x03) are 60mA */
222 *val
= data
* 20000; /* 20mA steps */
227 static int max77693_get_fast_charge_current(struct regmap
*regmap
, int *val
)
232 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_CNFG_02
, &data
);
236 data
&= CHG_CNFG_02_CC_MASK
;
237 data
>>= CHG_CNFG_02_CC_SHIFT
;
239 *val
= data
* 33300; /* 33.3mA steps */
244 static enum power_supply_property max77693_charger_props
[] = {
245 POWER_SUPPLY_PROP_STATUS
,
246 POWER_SUPPLY_PROP_CHARGE_TYPE
,
247 POWER_SUPPLY_PROP_HEALTH
,
248 POWER_SUPPLY_PROP_PRESENT
,
249 POWER_SUPPLY_PROP_ONLINE
,
250 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
251 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
252 POWER_SUPPLY_PROP_MODEL_NAME
,
253 POWER_SUPPLY_PROP_MANUFACTURER
,
256 static int max77693_charger_get_property(struct power_supply
*psy
,
257 enum power_supply_property psp
,
258 union power_supply_propval
*val
)
260 struct max77693_charger
*chg
= power_supply_get_drvdata(psy
);
261 struct regmap
*regmap
= chg
->max77693
->regmap
;
265 case POWER_SUPPLY_PROP_STATUS
:
266 ret
= max77693_get_charger_state(regmap
, &val
->intval
);
268 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
269 ret
= max77693_get_charge_type(regmap
, &val
->intval
);
271 case POWER_SUPPLY_PROP_HEALTH
:
272 ret
= max77693_get_battery_health(regmap
, &val
->intval
);
274 case POWER_SUPPLY_PROP_PRESENT
:
275 ret
= max77693_get_present(regmap
, &val
->intval
);
277 case POWER_SUPPLY_PROP_ONLINE
:
278 ret
= max77693_get_online(regmap
, &val
->intval
);
280 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
281 ret
= max77693_get_input_current_limit(regmap
, &val
->intval
);
283 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
284 ret
= max77693_get_fast_charge_current(regmap
, &val
->intval
);
286 case POWER_SUPPLY_PROP_MODEL_NAME
:
287 val
->strval
= max77693_charger_model
;
289 case POWER_SUPPLY_PROP_MANUFACTURER
:
290 val
->strval
= max77693_charger_manufacturer
;
299 static const struct power_supply_desc max77693_charger_desc
= {
300 .name
= MAX77693_CHARGER_NAME
,
301 .type
= POWER_SUPPLY_TYPE_BATTERY
,
302 .properties
= max77693_charger_props
,
303 .num_properties
= ARRAY_SIZE(max77693_charger_props
),
304 .get_property
= max77693_charger_get_property
,
307 static ssize_t
device_attr_store(struct device
*dev
,
308 struct device_attribute
*attr
, const char *buf
, size_t count
,
309 int (*fn
)(struct max77693_charger
*, unsigned long))
311 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
315 ret
= kstrtoul(buf
, 10, &val
);
326 static ssize_t
fast_charge_timer_show(struct device
*dev
,
327 struct device_attribute
*attr
, char *buf
)
329 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
330 unsigned int data
, val
;
333 ret
= regmap_read(chg
->max77693
->regmap
, MAX77693_CHG_REG_CHG_CNFG_01
,
338 data
&= CHG_CNFG_01_FCHGTIME_MASK
;
339 data
>>= CHG_CNFG_01_FCHGTIME_SHIFT
;
342 /* Starting from 4 hours, step by 2 hours */
343 val
= 4 + (data
- 1) * 2;
351 return sysfs_emit(buf
, "%u\n", val
);
354 static int max77693_set_fast_charge_timer(struct max77693_charger
*chg
,
365 * Round down odd values.
369 data
= (hours
- 4) / 2 + 1;
378 data
<<= CHG_CNFG_01_FCHGTIME_SHIFT
;
380 return regmap_update_bits(chg
->max77693
->regmap
,
381 MAX77693_CHG_REG_CHG_CNFG_01
,
382 CHG_CNFG_01_FCHGTIME_MASK
, data
);
385 static ssize_t
fast_charge_timer_store(struct device
*dev
,
386 struct device_attribute
*attr
, const char *buf
, size_t count
)
388 return device_attr_store(dev
, attr
, buf
, count
,
389 max77693_set_fast_charge_timer
);
392 static ssize_t
top_off_threshold_current_show(struct device
*dev
,
393 struct device_attribute
*attr
, char *buf
)
395 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
396 unsigned int data
, val
;
399 ret
= regmap_read(chg
->max77693
->regmap
, MAX77693_CHG_REG_CHG_CNFG_03
,
404 data
&= CHG_CNFG_03_TOITH_MASK
;
405 data
>>= CHG_CNFG_03_TOITH_SHIFT
;
408 val
= 100000 + data
* 25000;
412 return sysfs_emit(buf
, "%u\n", val
);
415 static int max77693_set_top_off_threshold_current(struct max77693_charger
*chg
,
420 if (uamp
< 100000 || uamp
> 350000)
424 data
= (uamp
- 100000) / 25000;
426 /* (200000, 350000> */
429 data
<<= CHG_CNFG_03_TOITH_SHIFT
;
431 return regmap_update_bits(chg
->max77693
->regmap
,
432 MAX77693_CHG_REG_CHG_CNFG_03
,
433 CHG_CNFG_03_TOITH_MASK
, data
);
436 static ssize_t
top_off_threshold_current_store(struct device
*dev
,
437 struct device_attribute
*attr
, const char *buf
, size_t count
)
439 return device_attr_store(dev
, attr
, buf
, count
,
440 max77693_set_top_off_threshold_current
);
443 static ssize_t
top_off_timer_show(struct device
*dev
,
444 struct device_attribute
*attr
, char *buf
)
446 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
447 unsigned int data
, val
;
450 ret
= regmap_read(chg
->max77693
->regmap
, MAX77693_CHG_REG_CHG_CNFG_03
,
455 data
&= CHG_CNFG_03_TOTIME_MASK
;
456 data
>>= CHG_CNFG_03_TOTIME_SHIFT
;
460 return sysfs_emit(buf
, "%u\n", val
);
463 static int max77693_set_top_off_timer(struct max77693_charger
*chg
,
464 unsigned long minutes
)
472 data
<<= CHG_CNFG_03_TOTIME_SHIFT
;
474 return regmap_update_bits(chg
->max77693
->regmap
,
475 MAX77693_CHG_REG_CHG_CNFG_03
,
476 CHG_CNFG_03_TOTIME_MASK
, data
);
479 static ssize_t
top_off_timer_store(struct device
*dev
,
480 struct device_attribute
*attr
, const char *buf
, size_t count
)
482 return device_attr_store(dev
, attr
, buf
, count
,
483 max77693_set_top_off_timer
);
486 static DEVICE_ATTR_RW(fast_charge_timer
);
487 static DEVICE_ATTR_RW(top_off_threshold_current
);
488 static DEVICE_ATTR_RW(top_off_timer
);
490 static int max77693_set_constant_volt(struct max77693_charger
*chg
,
505 if (uvolt
>= 3650000 && uvolt
< 4340000)
506 data
= (uvolt
- 3650000) / 25000;
507 else if (uvolt
>= 4340000 && uvolt
< 4350000)
509 else if (uvolt
>= 4350000 && uvolt
<= 4400000)
510 data
= 0x1d + (uvolt
- 4350000) / 25000;
512 dev_err(chg
->dev
, "Wrong value for charging constant voltage\n");
516 data
<<= CHG_CNFG_04_CHGCVPRM_SHIFT
;
518 dev_dbg(chg
->dev
, "Charging constant voltage: %u (0x%x)\n", uvolt
,
521 return regmap_update_bits(chg
->max77693
->regmap
,
522 MAX77693_CHG_REG_CHG_CNFG_04
,
523 CHG_CNFG_04_CHGCVPRM_MASK
, data
);
526 static int max77693_set_min_system_volt(struct max77693_charger
*chg
,
531 if (uvolt
< 3000000 || uvolt
> 3700000) {
532 dev_err(chg
->dev
, "Wrong value for minimum system regulation voltage\n");
536 data
= (uvolt
- 3000000) / 100000;
538 data
<<= CHG_CNFG_04_MINVSYS_SHIFT
;
540 dev_dbg(chg
->dev
, "Minimum system regulation voltage: %u (0x%x)\n",
543 return regmap_update_bits(chg
->max77693
->regmap
,
544 MAX77693_CHG_REG_CHG_CNFG_04
,
545 CHG_CNFG_04_MINVSYS_MASK
, data
);
548 static int max77693_set_thermal_regulation_temp(struct max77693_charger
*chg
,
558 data
= (cels
- 70) / 15;
561 dev_err(chg
->dev
, "Wrong value for thermal regulation loop temperature\n");
565 data
<<= CHG_CNFG_07_REGTEMP_SHIFT
;
567 dev_dbg(chg
->dev
, "Thermal regulation loop temperature: %u (0x%x)\n",
570 return regmap_update_bits(chg
->max77693
->regmap
,
571 MAX77693_CHG_REG_CHG_CNFG_07
,
572 CHG_CNFG_07_REGTEMP_MASK
, data
);
575 static int max77693_set_batttery_overcurrent(struct max77693_charger
*chg
,
580 if (uamp
&& (uamp
< 2000000 || uamp
> 3500000)) {
581 dev_err(chg
->dev
, "Wrong value for battery overcurrent\n");
586 data
= ((uamp
- 2000000) / 250000) + 1;
588 data
= 0; /* disable */
590 data
<<= CHG_CNFG_12_B2SOVRC_SHIFT
;
592 dev_dbg(chg
->dev
, "Battery overcurrent: %u (0x%x)\n", uamp
, data
);
594 return regmap_update_bits(chg
->max77693
->regmap
,
595 MAX77693_CHG_REG_CHG_CNFG_12
,
596 CHG_CNFG_12_B2SOVRC_MASK
, data
);
599 static int max77693_set_charge_input_threshold_volt(struct max77693_charger
*chg
,
611 data
= (uvolt
- 4700000) / 100000;
614 dev_err(chg
->dev
, "Wrong value for charge input voltage regulation threshold\n");
618 data
<<= CHG_CNFG_12_VCHGINREG_SHIFT
;
620 dev_dbg(chg
->dev
, "Charge input voltage regulation threshold: %u (0x%x)\n",
623 return regmap_update_bits(chg
->max77693
->regmap
,
624 MAX77693_CHG_REG_CHG_CNFG_12
,
625 CHG_CNFG_12_VCHGINREG_MASK
, data
);
629 * Sets charger registers to proper and safe default values.
631 static int max77693_reg_init(struct max77693_charger
*chg
)
636 /* Unlock charger register protection */
637 data
= (0x3 << CHG_CNFG_06_CHGPROT_SHIFT
);
638 ret
= regmap_update_bits(chg
->max77693
->regmap
,
639 MAX77693_CHG_REG_CHG_CNFG_06
,
640 CHG_CNFG_06_CHGPROT_MASK
, data
);
642 dev_err(chg
->dev
, "Error unlocking registers: %d\n", ret
);
646 ret
= max77693_set_fast_charge_timer(chg
, DEFAULT_FAST_CHARGE_TIMER
);
650 ret
= max77693_set_top_off_threshold_current(chg
,
651 DEFAULT_TOP_OFF_THRESHOLD_CURRENT
);
655 ret
= max77693_set_top_off_timer(chg
, DEFAULT_TOP_OFF_TIMER
);
659 ret
= max77693_set_constant_volt(chg
, chg
->constant_volt
);
663 ret
= max77693_set_min_system_volt(chg
, chg
->min_system_volt
);
667 ret
= max77693_set_thermal_regulation_temp(chg
,
668 chg
->thermal_regulation_temp
);
672 ret
= max77693_set_batttery_overcurrent(chg
, chg
->batttery_overcurrent
);
676 return max77693_set_charge_input_threshold_volt(chg
,
677 chg
->charge_input_threshold_volt
);
681 static int max77693_dt_init(struct device
*dev
, struct max77693_charger
*chg
)
683 struct device_node
*np
= dev
->of_node
;
686 dev_err(dev
, "no charger OF node\n");
690 if (of_property_read_u32(np
, "maxim,constant-microvolt",
691 &chg
->constant_volt
))
692 chg
->constant_volt
= DEFAULT_CONSTANT_VOLT
;
694 if (of_property_read_u32(np
, "maxim,min-system-microvolt",
695 &chg
->min_system_volt
))
696 chg
->min_system_volt
= DEFAULT_MIN_SYSTEM_VOLT
;
698 if (of_property_read_u32(np
, "maxim,thermal-regulation-celsius",
699 &chg
->thermal_regulation_temp
))
700 chg
->thermal_regulation_temp
= DEFAULT_THERMAL_REGULATION_TEMP
;
702 if (of_property_read_u32(np
, "maxim,battery-overcurrent-microamp",
703 &chg
->batttery_overcurrent
))
704 chg
->batttery_overcurrent
= DEFAULT_BATTERY_OVERCURRENT
;
706 if (of_property_read_u32(np
, "maxim,charge-input-threshold-microvolt",
707 &chg
->charge_input_threshold_volt
))
708 chg
->charge_input_threshold_volt
=
709 DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT
;
713 #else /* CONFIG_OF */
714 static int max77693_dt_init(struct device
*dev
, struct max77693_charger
*chg
)
718 #endif /* CONFIG_OF */
720 static int max77693_charger_probe(struct platform_device
*pdev
)
722 struct max77693_charger
*chg
;
723 struct power_supply_config psy_cfg
= {};
724 struct max77693_dev
*max77693
= dev_get_drvdata(pdev
->dev
.parent
);
727 chg
= devm_kzalloc(&pdev
->dev
, sizeof(*chg
), GFP_KERNEL
);
731 platform_set_drvdata(pdev
, chg
);
732 chg
->dev
= &pdev
->dev
;
733 chg
->max77693
= max77693
;
735 ret
= max77693_dt_init(&pdev
->dev
, chg
);
739 ret
= max77693_reg_init(chg
);
743 psy_cfg
.drv_data
= chg
;
745 ret
= device_create_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
747 dev_err(&pdev
->dev
, "failed: create fast charge timer sysfs entry\n");
751 ret
= device_create_file(&pdev
->dev
,
752 &dev_attr_top_off_threshold_current
);
754 dev_err(&pdev
->dev
, "failed: create top off current sysfs entry\n");
758 ret
= device_create_file(&pdev
->dev
, &dev_attr_top_off_timer
);
760 dev_err(&pdev
->dev
, "failed: create top off timer sysfs entry\n");
764 chg
->charger
= devm_power_supply_register(&pdev
->dev
,
765 &max77693_charger_desc
,
767 if (IS_ERR(chg
->charger
)) {
768 dev_err(&pdev
->dev
, "failed: power supply register\n");
769 ret
= PTR_ERR(chg
->charger
);
776 device_remove_file(&pdev
->dev
, &dev_attr_top_off_timer
);
777 device_remove_file(&pdev
->dev
, &dev_attr_top_off_threshold_current
);
778 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
783 static void max77693_charger_remove(struct platform_device
*pdev
)
785 device_remove_file(&pdev
->dev
, &dev_attr_top_off_timer
);
786 device_remove_file(&pdev
->dev
, &dev_attr_top_off_threshold_current
);
787 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
790 static const struct platform_device_id max77693_charger_id
[] = {
791 { "max77693-charger", 0, },
794 MODULE_DEVICE_TABLE(platform
, max77693_charger_id
);
796 static struct platform_driver max77693_charger_driver
= {
798 .name
= "max77693-charger",
800 .probe
= max77693_charger_probe
,
801 .remove
= max77693_charger_remove
,
802 .id_table
= max77693_charger_id
,
804 module_platform_driver(max77693_charger_driver
);
806 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
807 MODULE_DESCRIPTION("Maxim 77693 charger driver");
808 MODULE_LICENSE("GPL");