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-common.h>
24 #include <linux/mfd/max77693-private.h>
26 #define MAX77693_CHARGER_NAME "max77693-charger"
27 static const char *max77693_charger_model
= "MAX77693";
28 static const char *max77693_charger_manufacturer
= "Maxim Integrated";
30 struct max77693_charger
{
32 struct max77693_dev
*max77693
;
33 struct power_supply
*charger
;
37 u32 thermal_regulation_temp
;
38 u32 batttery_overcurrent
;
39 u32 charge_input_threshold_volt
;
42 static int max77693_get_charger_state(struct regmap
*regmap
, int *val
)
47 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_DETAILS_01
, &data
);
51 data
&= CHG_DETAILS_01_CHG_MASK
;
52 data
>>= CHG_DETAILS_01_CHG_SHIFT
;
55 case MAX77693_CHARGING_PREQUALIFICATION
:
56 case MAX77693_CHARGING_FAST_CONST_CURRENT
:
57 case MAX77693_CHARGING_FAST_CONST_VOLTAGE
:
58 case MAX77693_CHARGING_TOP_OFF
:
59 /* In high temp the charging current is reduced, but still charging */
60 case MAX77693_CHARGING_HIGH_TEMP
:
61 *val
= POWER_SUPPLY_STATUS_CHARGING
;
63 case MAX77693_CHARGING_DONE
:
64 *val
= POWER_SUPPLY_STATUS_FULL
;
66 case MAX77693_CHARGING_TIMER_EXPIRED
:
67 case MAX77693_CHARGING_THERMISTOR_SUSPEND
:
68 *val
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
70 case MAX77693_CHARGING_OFF
:
71 case MAX77693_CHARGING_OVER_TEMP
:
72 case MAX77693_CHARGING_WATCHDOG_EXPIRED
:
73 *val
= POWER_SUPPLY_STATUS_DISCHARGING
;
75 case MAX77693_CHARGING_RESERVED
:
77 *val
= POWER_SUPPLY_STATUS_UNKNOWN
;
83 static int max77693_get_charge_type(struct regmap
*regmap
, int *val
)
88 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_DETAILS_01
, &data
);
92 data
&= CHG_DETAILS_01_CHG_MASK
;
93 data
>>= CHG_DETAILS_01_CHG_SHIFT
;
96 case MAX77693_CHARGING_PREQUALIFICATION
:
98 * Top-off: trickle or fast? In top-off the current varies between
99 * 100 and 250 mA. It is higher than prequalification current.
101 case MAX77693_CHARGING_TOP_OFF
:
102 *val
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
104 case MAX77693_CHARGING_FAST_CONST_CURRENT
:
105 case MAX77693_CHARGING_FAST_CONST_VOLTAGE
:
106 /* In high temp the charging current is reduced, but still charging */
107 case MAX77693_CHARGING_HIGH_TEMP
:
108 *val
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
110 case MAX77693_CHARGING_DONE
:
111 case MAX77693_CHARGING_TIMER_EXPIRED
:
112 case MAX77693_CHARGING_THERMISTOR_SUSPEND
:
113 case MAX77693_CHARGING_OFF
:
114 case MAX77693_CHARGING_OVER_TEMP
:
115 case MAX77693_CHARGING_WATCHDOG_EXPIRED
:
116 *val
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
118 case MAX77693_CHARGING_RESERVED
:
120 *val
= POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
;
127 * Supported health statuses:
128 * - POWER_SUPPLY_HEALTH_DEAD
129 * - POWER_SUPPLY_HEALTH_GOOD
130 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
131 * - POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
132 * - POWER_SUPPLY_HEALTH_UNKNOWN
133 * - POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
135 static int max77693_get_battery_health(struct regmap
*regmap
, int *val
)
140 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_DETAILS_01
, &data
);
144 data
&= CHG_DETAILS_01_BAT_MASK
;
145 data
>>= CHG_DETAILS_01_BAT_SHIFT
;
148 case MAX77693_BATTERY_NOBAT
:
149 *val
= POWER_SUPPLY_HEALTH_DEAD
;
151 case MAX77693_BATTERY_PREQUALIFICATION
:
152 case MAX77693_BATTERY_GOOD
:
153 case MAX77693_BATTERY_LOWVOLTAGE
:
154 *val
= POWER_SUPPLY_HEALTH_GOOD
;
156 case MAX77693_BATTERY_TIMER_EXPIRED
:
158 * Took longer to charge than expected, charging suspended.
161 *val
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
163 case MAX77693_BATTERY_OVERVOLTAGE
:
164 *val
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
166 case MAX77693_BATTERY_OVERCURRENT
:
167 *val
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
169 case MAX77693_BATTERY_RESERVED
:
171 *val
= POWER_SUPPLY_HEALTH_UNKNOWN
;
178 static int max77693_get_present(struct regmap
*regmap
, int *val
)
184 * Read CHG_INT_OK register. High DETBAT bit here should be
185 * equal to value 0x0 in CHG_DETAILS_01/BAT field.
187 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_INT_OK
, &data
);
191 *val
= (data
& CHG_INT_OK_DETBAT_MASK
) ? 0 : 1;
196 static int max77693_get_online(struct regmap
*regmap
, int *val
)
201 ret
= regmap_read(regmap
, MAX77693_CHG_REG_CHG_INT_OK
, &data
);
205 *val
= (data
& CHG_INT_OK_CHGIN_MASK
) ? 1 : 0;
210 static enum power_supply_property max77693_charger_props
[] = {
211 POWER_SUPPLY_PROP_STATUS
,
212 POWER_SUPPLY_PROP_CHARGE_TYPE
,
213 POWER_SUPPLY_PROP_HEALTH
,
214 POWER_SUPPLY_PROP_PRESENT
,
215 POWER_SUPPLY_PROP_ONLINE
,
216 POWER_SUPPLY_PROP_MODEL_NAME
,
217 POWER_SUPPLY_PROP_MANUFACTURER
,
220 static int max77693_charger_get_property(struct power_supply
*psy
,
221 enum power_supply_property psp
,
222 union power_supply_propval
*val
)
224 struct max77693_charger
*chg
= power_supply_get_drvdata(psy
);
225 struct regmap
*regmap
= chg
->max77693
->regmap
;
229 case POWER_SUPPLY_PROP_STATUS
:
230 ret
= max77693_get_charger_state(regmap
, &val
->intval
);
232 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
233 ret
= max77693_get_charge_type(regmap
, &val
->intval
);
235 case POWER_SUPPLY_PROP_HEALTH
:
236 ret
= max77693_get_battery_health(regmap
, &val
->intval
);
238 case POWER_SUPPLY_PROP_PRESENT
:
239 ret
= max77693_get_present(regmap
, &val
->intval
);
241 case POWER_SUPPLY_PROP_ONLINE
:
242 ret
= max77693_get_online(regmap
, &val
->intval
);
244 case POWER_SUPPLY_PROP_MODEL_NAME
:
245 val
->strval
= max77693_charger_model
;
247 case POWER_SUPPLY_PROP_MANUFACTURER
:
248 val
->strval
= max77693_charger_manufacturer
;
257 static const struct power_supply_desc max77693_charger_desc
= {
258 .name
= MAX77693_CHARGER_NAME
,
259 .type
= POWER_SUPPLY_TYPE_BATTERY
,
260 .properties
= max77693_charger_props
,
261 .num_properties
= ARRAY_SIZE(max77693_charger_props
),
262 .get_property
= max77693_charger_get_property
,
265 static ssize_t
device_attr_store(struct device
*dev
,
266 struct device_attribute
*attr
, const char *buf
, size_t count
,
267 int (*fn
)(struct max77693_charger
*, unsigned long))
269 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
273 ret
= kstrtoul(buf
, 10, &val
);
284 static ssize_t
fast_charge_timer_show(struct device
*dev
,
285 struct device_attribute
*attr
, char *buf
)
287 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
288 unsigned int data
, val
;
291 ret
= regmap_read(chg
->max77693
->regmap
, MAX77693_CHG_REG_CHG_CNFG_01
,
296 data
&= CHG_CNFG_01_FCHGTIME_MASK
;
297 data
>>= CHG_CNFG_01_FCHGTIME_SHIFT
;
300 /* Starting from 4 hours, step by 2 hours */
301 val
= 4 + (data
- 1) * 2;
309 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
312 static int max77693_set_fast_charge_timer(struct max77693_charger
*chg
,
323 * Round down odd values.
327 data
= (hours
- 4) / 2 + 1;
336 data
<<= CHG_CNFG_01_FCHGTIME_SHIFT
;
338 return regmap_update_bits(chg
->max77693
->regmap
,
339 MAX77693_CHG_REG_CHG_CNFG_01
,
340 CHG_CNFG_01_FCHGTIME_MASK
, data
);
343 static ssize_t
fast_charge_timer_store(struct device
*dev
,
344 struct device_attribute
*attr
, const char *buf
, size_t count
)
346 return device_attr_store(dev
, attr
, buf
, count
,
347 max77693_set_fast_charge_timer
);
350 static ssize_t
top_off_threshold_current_show(struct device
*dev
,
351 struct device_attribute
*attr
, char *buf
)
353 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
354 unsigned int data
, val
;
357 ret
= regmap_read(chg
->max77693
->regmap
, MAX77693_CHG_REG_CHG_CNFG_03
,
362 data
&= CHG_CNFG_03_TOITH_MASK
;
363 data
>>= CHG_CNFG_03_TOITH_SHIFT
;
366 val
= 100000 + data
* 25000;
370 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
373 static int max77693_set_top_off_threshold_current(struct max77693_charger
*chg
,
378 if (uamp
< 100000 || uamp
> 350000)
382 data
= (uamp
- 100000) / 25000;
384 /* (200000, 350000> */
387 data
<<= CHG_CNFG_03_TOITH_SHIFT
;
389 return regmap_update_bits(chg
->max77693
->regmap
,
390 MAX77693_CHG_REG_CHG_CNFG_03
,
391 CHG_CNFG_03_TOITH_MASK
, data
);
394 static ssize_t
top_off_threshold_current_store(struct device
*dev
,
395 struct device_attribute
*attr
, const char *buf
, size_t count
)
397 return device_attr_store(dev
, attr
, buf
, count
,
398 max77693_set_top_off_threshold_current
);
401 static ssize_t
top_off_timer_show(struct device
*dev
,
402 struct device_attribute
*attr
, char *buf
)
404 struct max77693_charger
*chg
= dev_get_drvdata(dev
);
405 unsigned int data
, val
;
408 ret
= regmap_read(chg
->max77693
->regmap
, MAX77693_CHG_REG_CHG_CNFG_03
,
413 data
&= CHG_CNFG_03_TOTIME_MASK
;
414 data
>>= CHG_CNFG_03_TOTIME_SHIFT
;
418 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
421 static int max77693_set_top_off_timer(struct max77693_charger
*chg
,
422 unsigned long minutes
)
430 data
<<= CHG_CNFG_03_TOTIME_SHIFT
;
432 return regmap_update_bits(chg
->max77693
->regmap
,
433 MAX77693_CHG_REG_CHG_CNFG_03
,
434 CHG_CNFG_03_TOTIME_MASK
, data
);
437 static ssize_t
top_off_timer_store(struct device
*dev
,
438 struct device_attribute
*attr
, const char *buf
, size_t count
)
440 return device_attr_store(dev
, attr
, buf
, count
,
441 max77693_set_top_off_timer
);
444 static DEVICE_ATTR_RW(fast_charge_timer
);
445 static DEVICE_ATTR_RW(top_off_threshold_current
);
446 static DEVICE_ATTR_RW(top_off_timer
);
448 static int max77693_set_constant_volt(struct max77693_charger
*chg
,
463 if (uvolt
>= 3650000 && uvolt
< 4340000)
464 data
= (uvolt
- 3650000) / 25000;
465 else if (uvolt
>= 4340000 && uvolt
< 4350000)
467 else if (uvolt
>= 4350000 && uvolt
<= 4400000)
468 data
= 0x1d + (uvolt
- 4350000) / 25000;
470 dev_err(chg
->dev
, "Wrong value for charging constant voltage\n");
474 data
<<= CHG_CNFG_04_CHGCVPRM_SHIFT
;
476 dev_dbg(chg
->dev
, "Charging constant voltage: %u (0x%x)\n", uvolt
,
479 return regmap_update_bits(chg
->max77693
->regmap
,
480 MAX77693_CHG_REG_CHG_CNFG_04
,
481 CHG_CNFG_04_CHGCVPRM_MASK
, data
);
484 static int max77693_set_min_system_volt(struct max77693_charger
*chg
,
489 if (uvolt
< 3000000 || uvolt
> 3700000) {
490 dev_err(chg
->dev
, "Wrong value for minimum system regulation voltage\n");
494 data
= (uvolt
- 3000000) / 100000;
496 data
<<= CHG_CNFG_04_MINVSYS_SHIFT
;
498 dev_dbg(chg
->dev
, "Minimum system regulation voltage: %u (0x%x)\n",
501 return regmap_update_bits(chg
->max77693
->regmap
,
502 MAX77693_CHG_REG_CHG_CNFG_04
,
503 CHG_CNFG_04_MINVSYS_MASK
, data
);
506 static int max77693_set_thermal_regulation_temp(struct max77693_charger
*chg
,
516 data
= (cels
- 70) / 15;
519 dev_err(chg
->dev
, "Wrong value for thermal regulation loop temperature\n");
523 data
<<= CHG_CNFG_07_REGTEMP_SHIFT
;
525 dev_dbg(chg
->dev
, "Thermal regulation loop temperature: %u (0x%x)\n",
528 return regmap_update_bits(chg
->max77693
->regmap
,
529 MAX77693_CHG_REG_CHG_CNFG_07
,
530 CHG_CNFG_07_REGTEMP_MASK
, data
);
533 static int max77693_set_batttery_overcurrent(struct max77693_charger
*chg
,
538 if (uamp
&& (uamp
< 2000000 || uamp
> 3500000)) {
539 dev_err(chg
->dev
, "Wrong value for battery overcurrent\n");
544 data
= ((uamp
- 2000000) / 250000) + 1;
546 data
= 0; /* disable */
548 data
<<= CHG_CNFG_12_B2SOVRC_SHIFT
;
550 dev_dbg(chg
->dev
, "Battery overcurrent: %u (0x%x)\n", uamp
, data
);
552 return regmap_update_bits(chg
->max77693
->regmap
,
553 MAX77693_CHG_REG_CHG_CNFG_12
,
554 CHG_CNFG_12_B2SOVRC_MASK
, data
);
557 static int max77693_set_charge_input_threshold_volt(struct max77693_charger
*chg
,
569 data
= (uvolt
- 4700000) / 100000;
571 dev_err(chg
->dev
, "Wrong value for charge input voltage regulation threshold\n");
575 data
<<= CHG_CNFG_12_VCHGINREG_SHIFT
;
577 dev_dbg(chg
->dev
, "Charge input voltage regulation threshold: %u (0x%x)\n",
580 return regmap_update_bits(chg
->max77693
->regmap
,
581 MAX77693_CHG_REG_CHG_CNFG_12
,
582 CHG_CNFG_12_VCHGINREG_MASK
, data
);
586 * Sets charger registers to proper and safe default values.
588 static int max77693_reg_init(struct max77693_charger
*chg
)
593 /* Unlock charger register protection */
594 data
= (0x3 << CHG_CNFG_06_CHGPROT_SHIFT
);
595 ret
= regmap_update_bits(chg
->max77693
->regmap
,
596 MAX77693_CHG_REG_CHG_CNFG_06
,
597 CHG_CNFG_06_CHGPROT_MASK
, data
);
599 dev_err(chg
->dev
, "Error unlocking registers: %d\n", ret
);
603 ret
= max77693_set_fast_charge_timer(chg
, DEFAULT_FAST_CHARGE_TIMER
);
607 ret
= max77693_set_top_off_threshold_current(chg
,
608 DEFAULT_TOP_OFF_THRESHOLD_CURRENT
);
612 ret
= max77693_set_top_off_timer(chg
, DEFAULT_TOP_OFF_TIMER
);
616 ret
= max77693_set_constant_volt(chg
, chg
->constant_volt
);
620 ret
= max77693_set_min_system_volt(chg
, chg
->min_system_volt
);
624 ret
= max77693_set_thermal_regulation_temp(chg
,
625 chg
->thermal_regulation_temp
);
629 ret
= max77693_set_batttery_overcurrent(chg
, chg
->batttery_overcurrent
);
633 return max77693_set_charge_input_threshold_volt(chg
,
634 chg
->charge_input_threshold_volt
);
638 static int max77693_dt_init(struct device
*dev
, struct max77693_charger
*chg
)
640 struct device_node
*np
= dev
->of_node
;
643 dev_err(dev
, "no charger OF node\n");
647 if (of_property_read_u32(np
, "maxim,constant-microvolt",
648 &chg
->constant_volt
))
649 chg
->constant_volt
= DEFAULT_CONSTANT_VOLT
;
651 if (of_property_read_u32(np
, "maxim,min-system-microvolt",
652 &chg
->min_system_volt
))
653 chg
->min_system_volt
= DEFAULT_MIN_SYSTEM_VOLT
;
655 if (of_property_read_u32(np
, "maxim,thermal-regulation-celsius",
656 &chg
->thermal_regulation_temp
))
657 chg
->thermal_regulation_temp
= DEFAULT_THERMAL_REGULATION_TEMP
;
659 if (of_property_read_u32(np
, "maxim,battery-overcurrent-microamp",
660 &chg
->batttery_overcurrent
))
661 chg
->batttery_overcurrent
= DEFAULT_BATTERY_OVERCURRENT
;
663 if (of_property_read_u32(np
, "maxim,charge-input-threshold-microvolt",
664 &chg
->charge_input_threshold_volt
))
665 chg
->charge_input_threshold_volt
=
666 DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT
;
670 #else /* CONFIG_OF */
671 static int max77693_dt_init(struct device
*dev
, struct max77693_charger
*chg
)
675 #endif /* CONFIG_OF */
677 static int max77693_charger_probe(struct platform_device
*pdev
)
679 struct max77693_charger
*chg
;
680 struct power_supply_config psy_cfg
= {};
681 struct max77693_dev
*max77693
= dev_get_drvdata(pdev
->dev
.parent
);
684 chg
= devm_kzalloc(&pdev
->dev
, sizeof(*chg
), GFP_KERNEL
);
688 platform_set_drvdata(pdev
, chg
);
689 chg
->dev
= &pdev
->dev
;
690 chg
->max77693
= max77693
;
692 ret
= max77693_dt_init(&pdev
->dev
, chg
);
696 ret
= max77693_reg_init(chg
);
700 psy_cfg
.drv_data
= chg
;
702 ret
= device_create_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
704 dev_err(&pdev
->dev
, "failed: create fast charge timer sysfs entry\n");
708 ret
= device_create_file(&pdev
->dev
,
709 &dev_attr_top_off_threshold_current
);
711 dev_err(&pdev
->dev
, "failed: create top off current sysfs entry\n");
715 ret
= device_create_file(&pdev
->dev
, &dev_attr_top_off_timer
);
717 dev_err(&pdev
->dev
, "failed: create top off timer sysfs entry\n");
721 chg
->charger
= power_supply_register(&pdev
->dev
,
722 &max77693_charger_desc
,
724 if (IS_ERR(chg
->charger
)) {
725 dev_err(&pdev
->dev
, "failed: power supply register\n");
726 ret
= PTR_ERR(chg
->charger
);
733 device_remove_file(&pdev
->dev
, &dev_attr_top_off_timer
);
734 device_remove_file(&pdev
->dev
, &dev_attr_top_off_threshold_current
);
735 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
740 static int max77693_charger_remove(struct platform_device
*pdev
)
742 struct max77693_charger
*chg
= platform_get_drvdata(pdev
);
744 device_remove_file(&pdev
->dev
, &dev_attr_top_off_timer
);
745 device_remove_file(&pdev
->dev
, &dev_attr_top_off_threshold_current
);
746 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
748 power_supply_unregister(chg
->charger
);
753 static const struct platform_device_id max77693_charger_id
[] = {
754 { "max77693-charger", 0, },
757 MODULE_DEVICE_TABLE(platform
, max77693_charger_id
);
759 static struct platform_driver max77693_charger_driver
= {
761 .name
= "max77693-charger",
763 .probe
= max77693_charger_probe
,
764 .remove
= max77693_charger_remove
,
765 .id_table
= max77693_charger_id
,
767 module_platform_driver(max77693_charger_driver
);
769 MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
770 MODULE_DESCRIPTION("Maxim 77693 charger driver");
771 MODULE_LICENSE("GPL");