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 #define 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
, int *val
)
46 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_DETAILS_01
, &data
);
50 data
&= CHG_DETAILS_01_CHG_MASK
;
51 data
>>= CHG_DETAILS_01_CHG_SHIFT
;
54 case MAX77693_CHARGING_PREQUALIFICATION
:
55 case MAX77693_CHARGING_FAST_CONST_CURRENT
:
56 case MAX77693_CHARGING_FAST_CONST_VOLTAGE
:
57 case MAX77693_CHARGING_TOP_OFF
:
58 /* In high temp the charging current is reduced, but still charging */
59 case MAX77693_CHARGING_HIGH_TEMP
:
60 *val
= POWER_SUPPLY_STATUS_CHARGING
;
62 case MAX77693_CHARGING_DONE
:
63 *val
= POWER_SUPPLY_STATUS_FULL
;
65 case MAX77693_CHARGING_TIMER_EXPIRED
:
66 case MAX77693_CHARGING_THERMISTOR_SUSPEND
:
67 *val
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
69 case MAX77693_CHARGING_OFF
:
70 case MAX77693_CHARGING_OVER_TEMP
:
71 case MAX77693_CHARGING_WATCHDOG_EXPIRED
:
72 *val
= POWER_SUPPLY_STATUS_DISCHARGING
;
74 case MAX77693_CHARGING_RESERVED
:
76 *val
= POWER_SUPPLY_STATUS_UNKNOWN
;
82 static int max77693_get_charge_type(struct regmap
*regmap
, int *val
)
87 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_DETAILS_01
, &data
);
91 data
&= CHG_DETAILS_01_CHG_MASK
;
92 data
>>= CHG_DETAILS_01_CHG_SHIFT
;
95 case MAX77693_CHARGING_PREQUALIFICATION
:
97 * Top-off: trickle or fast? In top-off the current varies between
98 * 100 and 250 mA. It is higher than prequalification current.
100 case MAX77693_CHARGING_TOP_OFF
:
101 *val
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
103 case MAX77693_CHARGING_FAST_CONST_CURRENT
:
104 case MAX77693_CHARGING_FAST_CONST_VOLTAGE
:
105 /* In high temp the charging current is reduced, but still charging */
106 case MAX77693_CHARGING_HIGH_TEMP
:
107 *val
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
109 case MAX77693_CHARGING_DONE
:
110 case MAX77693_CHARGING_TIMER_EXPIRED
:
111 case MAX77693_CHARGING_THERMISTOR_SUSPEND
:
112 case MAX77693_CHARGING_OFF
:
113 case MAX77693_CHARGING_OVER_TEMP
:
114 case MAX77693_CHARGING_WATCHDOG_EXPIRED
:
115 *val
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
117 case MAX77693_CHARGING_RESERVED
:
119 *val
= POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
;
126 * Supported health statuses:
127 * - POWER_SUPPLY_HEALTH_DEAD
128 * - POWER_SUPPLY_HEALTH_GOOD
129 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
130 * - POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
131 * - POWER_SUPPLY_HEALTH_UNKNOWN
132 * - POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
134 static int max77693_get_battery_health(struct regmap
*regmap
, int *val
)
139 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_DETAILS_01
, &data
);
143 data
&= CHG_DETAILS_01_BAT_MASK
;
144 data
>>= CHG_DETAILS_01_BAT_SHIFT
;
147 case MAX77693_BATTERY_NOBAT
:
148 *val
= POWER_SUPPLY_HEALTH_DEAD
;
150 case MAX77693_BATTERY_PREQUALIFICATION
:
151 case MAX77693_BATTERY_GOOD
:
152 case MAX77693_BATTERY_LOWVOLTAGE
:
153 *val
= POWER_SUPPLY_HEALTH_GOOD
;
155 case MAX77693_BATTERY_TIMER_EXPIRED
:
157 * Took longer to charge than expected, charging suspended.
160 *val
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
162 case MAX77693_BATTERY_OVERVOLTAGE
:
163 *val
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
165 case MAX77693_BATTERY_OVERCURRENT
:
166 *val
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
168 case MAX77693_BATTERY_RESERVED
:
170 *val
= POWER_SUPPLY_HEALTH_UNKNOWN
;
177 static int max77693_get_present(struct regmap
*regmap
, int *val
)
183 * Read CHG_INT_OK register. High DETBAT bit here should be
184 * equal to value 0x0 in CHG_DETAILS_01/BAT field.
186 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_INT_OK
, &data
);
190 *val
= (data
& CHG_INT_OK_DETBAT_MASK
) ? 0 : 1;
195 static int max77693_get_online(struct regmap
*regmap
, int *val
)
200 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_INT_OK
, &data
);
204 *val
= (data
& CHG_INT_OK_CHGIN_MASK
) ? 1 : 0;
209 static enum power_supply_property max77693_charger_props
[] = {
210 POWER_SUPPLY_PROP_STATUS
,
211 POWER_SUPPLY_PROP_CHARGE_TYPE
,
212 POWER_SUPPLY_PROP_HEALTH
,
213 POWER_SUPPLY_PROP_PRESENT
,
214 POWER_SUPPLY_PROP_ONLINE
,
215 POWER_SUPPLY_PROP_MODEL_NAME
,
216 POWER_SUPPLY_PROP_MANUFACTURER
,
219 static int max77693_charger_get_property(struct power_supply
*psy
,
220 enum power_supply_property psp
,
221 union power_supply_propval
*val
)
223 struct max77693_charger
*chg
= power_supply_get_drvdata(psy
);
224 struct regmap
*regmap
= chg
->max77693
->regmap
;
228 case POWER_SUPPLY_PROP_STATUS
:
229 ret
= max77693_get_charger_state(regmap
, &val
->intval
);
231 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
232 ret
= max77693_get_charge_type(regmap
, &val
->intval
);
234 case POWER_SUPPLY_PROP_HEALTH
:
235 ret
= max77693_get_battery_health(regmap
, &val
->intval
);
237 case POWER_SUPPLY_PROP_PRESENT
:
238 ret
= max77693_get_present(regmap
, &val
->intval
);
240 case POWER_SUPPLY_PROP_ONLINE
:
241 ret
= max77693_get_online(regmap
, &val
->intval
);
243 case POWER_SUPPLY_PROP_MODEL_NAME
:
244 val
->strval
= max77693_charger_model
;
246 case POWER_SUPPLY_PROP_MANUFACTURER
:
247 val
->strval
= max77693_charger_manufacturer
;
256 static const struct power_supply_desc max77693_charger_desc
= {
257 .name
= MAX77693_CHARGER_NAME
,
258 .type
= POWER_SUPPLY_TYPE_BATTERY
,
259 .properties
= max77693_charger_props
,
260 .num_properties
= ARRAY_SIZE(max77693_charger_props
),
261 .get_property
= max77693_charger_get_property
,
264 static ssize_t
device_attr_store(struct device
*dev
,
265 struct device_attribute
*attr
, const char *buf
, size_t count
,
266 int (*fn
)(struct max77693_charger
*, unsigned long))
268 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
272 ret
= kstrtoul(buf
, 10, &val
);
283 static ssize_t
fast_charge_timer_show(struct device
*dev
,
284 struct device_attribute
*attr
, char *buf
)
286 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
287 unsigned int data
, val
;
290 ret
= regmap_read(chg
->max77693
->regmap
, MAX77693_CHG_REG_CHG_CNFG_01
,
295 data
&= CHG_CNFG_01_FCHGTIME_MASK
;
296 data
>>= CHG_CNFG_01_FCHGTIME_SHIFT
;
299 /* Starting from 4 hours, step by 2 hours */
300 val
= 4 + (data
- 1) * 2;
308 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
311 static int max77693_set_fast_charge_timer(struct max77693_charger
*chg
,
322 * Round down odd values.
326 data
= (hours
- 4) / 2 + 1;
335 data
<<= CHG_CNFG_01_FCHGTIME_SHIFT
;
337 return regmap_update_bits(chg
->max77693
->regmap
,
338 MAX77693_CHG_REG_CHG_CNFG_01
,
339 CHG_CNFG_01_FCHGTIME_MASK
, data
);
342 static ssize_t
fast_charge_timer_store(struct device
*dev
,
343 struct device_attribute
*attr
, const char *buf
, size_t count
)
345 return device_attr_store(dev
, attr
, buf
, count
,
346 max77693_set_fast_charge_timer
);
349 static ssize_t
top_off_threshold_current_show(struct device
*dev
,
350 struct device_attribute
*attr
, char *buf
)
352 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
353 unsigned int data
, val
;
356 ret
= regmap_read(chg
->max77693
->regmap
, MAX77693_CHG_REG_CHG_CNFG_03
,
361 data
&= CHG_CNFG_03_TOITH_MASK
;
362 data
>>= CHG_CNFG_03_TOITH_SHIFT
;
365 val
= 100000 + data
* 25000;
369 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
372 static int max77693_set_top_off_threshold_current(struct max77693_charger
*chg
,
377 if (uamp
< 100000 || uamp
> 350000)
381 data
= (uamp
- 100000) / 25000;
383 /* (200000, 350000> */
386 data
<<= CHG_CNFG_03_TOITH_SHIFT
;
388 return regmap_update_bits(chg
->max77693
->regmap
,
389 MAX77693_CHG_REG_CHG_CNFG_03
,
390 CHG_CNFG_03_TOITH_MASK
, data
);
393 static ssize_t
top_off_threshold_current_store(struct device
*dev
,
394 struct device_attribute
*attr
, const char *buf
, size_t count
)
396 return device_attr_store(dev
, attr
, buf
, count
,
397 max77693_set_top_off_threshold_current
);
400 static ssize_t
top_off_timer_show(struct device
*dev
,
401 struct device_attribute
*attr
, char *buf
)
403 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
404 unsigned int data
, val
;
407 ret
= regmap_read(chg
->max77693
->regmap
, MAX77693_CHG_REG_CHG_CNFG_03
,
412 data
&= CHG_CNFG_03_TOTIME_MASK
;
413 data
>>= CHG_CNFG_03_TOTIME_SHIFT
;
417 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
420 static int max77693_set_top_off_timer(struct max77693_charger
*chg
,
421 unsigned long minutes
)
429 data
<<= CHG_CNFG_03_TOTIME_SHIFT
;
431 return regmap_update_bits(chg
->max77693
->regmap
,
432 MAX77693_CHG_REG_CHG_CNFG_03
,
433 CHG_CNFG_03_TOTIME_MASK
, data
);
436 static ssize_t
top_off_timer_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_timer
);
443 static DEVICE_ATTR_RW(fast_charge_timer
);
444 static DEVICE_ATTR_RW(top_off_threshold_current
);
445 static DEVICE_ATTR_RW(top_off_timer
);
447 static int max77693_set_constant_volt(struct max77693_charger
*chg
,
462 if (uvolt
>= 3650000 && uvolt
< 4340000)
463 data
= (uvolt
- 3650000) / 25000;
464 else if (uvolt
>= 4340000 && uvolt
< 4350000)
466 else if (uvolt
>= 4350000 && uvolt
<= 4400000)
467 data
= 0x1d + (uvolt
- 4350000) / 25000;
469 dev_err(chg
->dev
, "Wrong value for charging constant voltage\n");
473 data
<<= CHG_CNFG_04_CHGCVPRM_SHIFT
;
475 dev_dbg(chg
->dev
, "Charging constant voltage: %u (0x%x)\n", uvolt
,
478 return regmap_update_bits(chg
->max77693
->regmap
,
479 MAX77693_CHG_REG_CHG_CNFG_04
,
480 CHG_CNFG_04_CHGCVPRM_MASK
, data
);
483 static int max77693_set_min_system_volt(struct max77693_charger
*chg
,
488 if (uvolt
< 3000000 || uvolt
> 3700000) {
489 dev_err(chg
->dev
, "Wrong value for minimum system regulation voltage\n");
493 data
= (uvolt
- 3000000) / 100000;
495 data
<<= CHG_CNFG_04_MINVSYS_SHIFT
;
497 dev_dbg(chg
->dev
, "Minimum system regulation voltage: %u (0x%x)\n",
500 return regmap_update_bits(chg
->max77693
->regmap
,
501 MAX77693_CHG_REG_CHG_CNFG_04
,
502 CHG_CNFG_04_MINVSYS_MASK
, data
);
505 static int max77693_set_thermal_regulation_temp(struct max77693_charger
*chg
,
515 data
= (cels
- 70) / 15;
518 dev_err(chg
->dev
, "Wrong value for thermal regulation loop temperature\n");
522 data
<<= CHG_CNFG_07_REGTEMP_SHIFT
;
524 dev_dbg(chg
->dev
, "Thermal regulation loop temperature: %u (0x%x)\n",
527 return regmap_update_bits(chg
->max77693
->regmap
,
528 MAX77693_CHG_REG_CHG_CNFG_07
,
529 CHG_CNFG_07_REGTEMP_MASK
, data
);
532 static int max77693_set_batttery_overcurrent(struct max77693_charger
*chg
,
537 if (uamp
&& (uamp
< 2000000 || uamp
> 3500000)) {
538 dev_err(chg
->dev
, "Wrong value for battery overcurrent\n");
543 data
= ((uamp
- 2000000) / 250000) + 1;
545 data
= 0; /* disable */
547 data
<<= CHG_CNFG_12_B2SOVRC_SHIFT
;
549 dev_dbg(chg
->dev
, "Battery overcurrent: %u (0x%x)\n", uamp
, data
);
551 return regmap_update_bits(chg
->max77693
->regmap
,
552 MAX77693_CHG_REG_CHG_CNFG_12
,
553 CHG_CNFG_12_B2SOVRC_MASK
, data
);
556 static int max77693_set_charge_input_threshold_volt(struct max77693_charger
*chg
,
568 data
= (uvolt
- 4700000) / 100000;
570 dev_err(chg
->dev
, "Wrong value for charge input voltage regulation threshold\n");
574 data
<<= CHG_CNFG_12_VCHGINREG_SHIFT
;
576 dev_dbg(chg
->dev
, "Charge input voltage regulation threshold: %u (0x%x)\n",
579 return regmap_update_bits(chg
->max77693
->regmap
,
580 MAX77693_CHG_REG_CHG_CNFG_12
,
581 CHG_CNFG_12_VCHGINREG_MASK
, data
);
585 * Sets charger registers to proper and safe default values.
587 static int max77693_reg_init(struct max77693_charger
*chg
)
592 /* Unlock charger register protection */
593 data
= (0x3 << CHG_CNFG_06_CHGPROT_SHIFT
);
594 ret
= regmap_update_bits(chg
->max77693
->regmap
,
595 MAX77693_CHG_REG_CHG_CNFG_06
,
596 CHG_CNFG_06_CHGPROT_MASK
, data
);
598 dev_err(chg
->dev
, "Error unlocking registers: %d\n", ret
);
602 ret
= max77693_set_fast_charge_timer(chg
, DEFAULT_FAST_CHARGE_TIMER
);
606 ret
= max77693_set_top_off_threshold_current(chg
,
607 DEFAULT_TOP_OFF_THRESHOLD_CURRENT
);
611 ret
= max77693_set_top_off_timer(chg
, DEFAULT_TOP_OFF_TIMER
);
615 ret
= max77693_set_constant_volt(chg
, chg
->constant_volt
);
619 ret
= max77693_set_min_system_volt(chg
, chg
->min_system_volt
);
623 ret
= max77693_set_thermal_regulation_temp(chg
,
624 chg
->thermal_regulation_temp
);
628 ret
= max77693_set_batttery_overcurrent(chg
, chg
->batttery_overcurrent
);
632 return max77693_set_charge_input_threshold_volt(chg
,
633 chg
->charge_input_threshold_volt
);
637 static int max77693_dt_init(struct device
*dev
, struct max77693_charger
*chg
)
639 struct device_node
*np
= dev
->of_node
;
642 dev_err(dev
, "no charger OF node\n");
646 if (of_property_read_u32(np
, "maxim,constant-microvolt",
647 &chg
->constant_volt
))
648 chg
->constant_volt
= DEFAULT_CONSTANT_VOLT
;
650 if (of_property_read_u32(np
, "maxim,min-system-microvolt",
651 &chg
->min_system_volt
))
652 chg
->min_system_volt
= DEFAULT_MIN_SYSTEM_VOLT
;
654 if (of_property_read_u32(np
, "maxim,thermal-regulation-celsius",
655 &chg
->thermal_regulation_temp
))
656 chg
->thermal_regulation_temp
= DEFAULT_THERMAL_REGULATION_TEMP
;
658 if (of_property_read_u32(np
, "maxim,battery-overcurrent-microamp",
659 &chg
->batttery_overcurrent
))
660 chg
->batttery_overcurrent
= DEFAULT_BATTERY_OVERCURRENT
;
662 if (of_property_read_u32(np
, "maxim,charge-input-threshold-microvolt",
663 &chg
->charge_input_threshold_volt
))
664 chg
->charge_input_threshold_volt
=
665 DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT
;
669 #else /* CONFIG_OF */
670 static int max77693_dt_init(struct device
*dev
, struct max77693_charger
*chg
)
674 #endif /* CONFIG_OF */
676 static int max77693_charger_probe(struct platform_device
*pdev
)
678 struct max77693_charger
*chg
;
679 struct power_supply_config psy_cfg
= {};
680 struct max77693_dev
*max77693
= dev_get_drvdata(pdev
->dev
.parent
);
683 chg
= devm_kzalloc(&pdev
->dev
, sizeof(*chg
), GFP_KERNEL
);
687 platform_set_drvdata(pdev
, chg
);
688 chg
->dev
= &pdev
->dev
;
689 chg
->max77693
= max77693
;
691 ret
= max77693_dt_init(&pdev
->dev
, chg
);
695 ret
= max77693_reg_init(chg
);
699 psy_cfg
.drv_data
= chg
;
701 ret
= device_create_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
703 dev_err(&pdev
->dev
, "failed: create fast charge timer sysfs entry\n");
707 ret
= device_create_file(&pdev
->dev
,
708 &dev_attr_top_off_threshold_current
);
710 dev_err(&pdev
->dev
, "failed: create top off current sysfs entry\n");
714 ret
= device_create_file(&pdev
->dev
, &dev_attr_top_off_timer
);
716 dev_err(&pdev
->dev
, "failed: create top off timer sysfs entry\n");
720 chg
->charger
= power_supply_register(&pdev
->dev
,
721 &max77693_charger_desc
,
723 if (IS_ERR(chg
->charger
)) {
724 dev_err(&pdev
->dev
, "failed: power supply register\n");
725 ret
= PTR_ERR(chg
->charger
);
732 device_remove_file(&pdev
->dev
, &dev_attr_top_off_timer
);
733 device_remove_file(&pdev
->dev
, &dev_attr_top_off_threshold_current
);
734 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
739 static int max77693_charger_remove(struct platform_device
*pdev
)
741 struct max77693_charger
*chg
= platform_get_drvdata(pdev
);
743 device_remove_file(&pdev
->dev
, &dev_attr_top_off_timer
);
744 device_remove_file(&pdev
->dev
, &dev_attr_top_off_threshold_current
);
745 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
747 power_supply_unregister(chg
->charger
);
752 static const struct platform_device_id max77693_charger_id
[] = {
753 { "max77693-charger", 0, },
756 MODULE_DEVICE_TABLE(platform
, max77693_charger_id
);
758 static struct platform_driver max77693_charger_driver
= {
760 .name
= "max77693-charger",
762 .probe
= max77693_charger_probe
,
763 .remove
= max77693_charger_remove
,
764 .id_table
= max77693_charger_id
,
766 module_platform_driver(max77693_charger_driver
);
768 MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
769 MODULE_DESCRIPTION("Maxim 77693 charger driver");
770 MODULE_LICENSE("GPL");