1 // SPDX-License-Identifier: GPL-2.0+
3 // max14577_charger.c - Battery charger driver for the Maxim 14577/77836
5 // Copyright (C) 2013,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/mfd/max14577-private.h>
12 #include <linux/mfd/max14577.h>
14 struct max14577_charger
{
16 struct max14577
*max14577
;
17 struct power_supply
*charger
;
19 struct max14577_charger_platform_data
*pdata
;
23 * Helper function for mapping values of STATUS2/CHGTYP register on max14577
24 * and max77836 chipsets to enum maxim_muic_charger_type.
26 static enum max14577_muic_charger_type
maxim_get_charger_type(
27 enum maxim_device_type dev_type
, u8 val
) {
29 case MAX14577_CHARGER_TYPE_NONE
:
30 case MAX14577_CHARGER_TYPE_USB
:
31 case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT
:
32 case MAX14577_CHARGER_TYPE_DEDICATED_CHG
:
33 case MAX14577_CHARGER_TYPE_SPECIAL_500MA
:
34 case MAX14577_CHARGER_TYPE_SPECIAL_1A
:
36 case MAX14577_CHARGER_TYPE_DEAD_BATTERY
:
37 case MAX14577_CHARGER_TYPE_RESERVED
:
38 if (dev_type
== MAXIM_DEVICE_TYPE_MAX77836
)
42 WARN_ONCE(1, "max14577: Unsupported chgtyp register value 0x%02x", val
);
47 static int max14577_get_charger_state(struct max14577_charger
*chg
, int *val
)
49 struct regmap
*rmap
= chg
->max14577
->regmap
;
54 * Charging occurs only if:
55 * - CHGCTRL2/MBCHOSTEN == 1
56 * - STATUS2/CGMBC == 1
59 * - handle FULL after Top-off timer (EOC register may be off
60 * and the charger won't be charging although MBCHOSTEN is on)
61 * - handle properly dead-battery charging (respect timer)
62 * - handle timers (fast-charge and prequal) /MBCCHGERR/
64 ret
= max14577_read_reg(rmap
, MAX14577_CHG_REG_CHG_CTRL2
, ®_data
);
68 if ((reg_data
& CHGCTRL2_MBCHOSTEN_MASK
) == 0) {
69 *val
= POWER_SUPPLY_STATUS_DISCHARGING
;
73 ret
= max14577_read_reg(rmap
, MAX14577_CHG_REG_STATUS3
, ®_data
);
77 if (reg_data
& STATUS3_CGMBC_MASK
) {
78 /* Charger or USB-cable is connected */
79 if (reg_data
& STATUS3_EOC_MASK
)
80 *val
= POWER_SUPPLY_STATUS_FULL
;
82 *val
= POWER_SUPPLY_STATUS_CHARGING
;
86 *val
= POWER_SUPPLY_STATUS_DISCHARGING
;
93 * Supported charge types:
94 * - POWER_SUPPLY_CHARGE_TYPE_NONE
95 * - POWER_SUPPLY_CHARGE_TYPE_FAST
97 static int max14577_get_charge_type(struct max14577_charger
*chg
, int *val
)
102 * TODO: CHARGE_TYPE_TRICKLE (VCHGR_RC or EOC)?
104 * [after reaching EOC interrupt]
105 * "When the battery is fully charged, the 30-minute (typ)
106 * top-off timer starts. The device continues to trickle
107 * charge the battery until the top-off timer runs out."
109 ret
= max14577_get_charger_state(chg
, &charging
);
113 if (charging
== POWER_SUPPLY_STATUS_CHARGING
)
114 *val
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
116 *val
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
121 static int max14577_get_online(struct max14577_charger
*chg
, int *val
)
123 struct regmap
*rmap
= chg
->max14577
->regmap
;
126 enum max14577_muic_charger_type chg_type
;
128 ret
= max14577_read_reg(rmap
, MAX14577_MUIC_REG_STATUS2
, ®_data
);
132 reg_data
= ((reg_data
& STATUS2_CHGTYP_MASK
) >> STATUS2_CHGTYP_SHIFT
);
133 chg_type
= maxim_get_charger_type(chg
->max14577
->dev_type
, reg_data
);
135 case MAX14577_CHARGER_TYPE_USB
:
136 case MAX14577_CHARGER_TYPE_DEDICATED_CHG
:
137 case MAX14577_CHARGER_TYPE_SPECIAL_500MA
:
138 case MAX14577_CHARGER_TYPE_SPECIAL_1A
:
139 case MAX14577_CHARGER_TYPE_DEAD_BATTERY
:
140 case MAX77836_CHARGER_TYPE_SPECIAL_BIAS
:
143 case MAX14577_CHARGER_TYPE_NONE
:
144 case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT
:
145 case MAX14577_CHARGER_TYPE_RESERVED
:
146 case MAX77836_CHARGER_TYPE_RESERVED
:
155 * Supported health statuses:
156 * - POWER_SUPPLY_HEALTH_DEAD
157 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
158 * - POWER_SUPPLY_HEALTH_GOOD
160 static int max14577_get_battery_health(struct max14577_charger
*chg
, int *val
)
162 struct regmap
*rmap
= chg
->max14577
->regmap
;
165 enum max14577_muic_charger_type chg_type
;
167 ret
= max14577_read_reg(rmap
, MAX14577_MUIC_REG_STATUS2
, ®_data
);
171 reg_data
= ((reg_data
& STATUS2_CHGTYP_MASK
) >> STATUS2_CHGTYP_SHIFT
);
172 chg_type
= maxim_get_charger_type(chg
->max14577
->dev_type
, reg_data
);
173 if (chg_type
== MAX14577_CHARGER_TYPE_DEAD_BATTERY
) {
174 *val
= POWER_SUPPLY_HEALTH_DEAD
;
178 ret
= max14577_read_reg(rmap
, MAX14577_CHG_REG_STATUS3
, ®_data
);
182 if (reg_data
& STATUS3_OVP_MASK
) {
183 *val
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
187 /* Not dead, not overvoltage */
188 *val
= POWER_SUPPLY_HEALTH_GOOD
;
196 * The max14577 chip doesn't report any status of battery presence.
197 * Lets assume that it will always be used with some battery.
199 static int max14577_get_present(struct max14577_charger
*chg
, int *val
)
206 static int max14577_set_fast_charge_timer(struct max14577_charger
*chg
,
213 reg_data
= hours
- 3;
220 dev_err(chg
->dev
, "Wrong value for Fast-Charge Timer: %lu\n",
224 reg_data
<<= CHGCTRL1_TCHW_SHIFT
;
226 return max14577_update_reg(chg
->max14577
->regmap
,
227 MAX14577_REG_CHGCTRL1
, CHGCTRL1_TCHW_MASK
, reg_data
);
230 static int max14577_init_constant_voltage(struct max14577_charger
*chg
,
235 if (uvolt
< MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN
||
236 uvolt
> MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX
)
239 if (uvolt
== 4200000)
241 else if (uvolt
== MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX
)
243 else if (uvolt
<= 4280000) {
244 unsigned int val
= uvolt
;
246 val
-= MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN
;
247 val
/= MAXIM_CHARGER_CONSTANT_VOLTAGE_STEP
;
248 if (uvolt
<= 4180000)
249 reg_data
= 0x1 + val
;
251 reg_data
= val
; /* Fix for gap between 4.18V and 4.22V */
255 reg_data
<<= CHGCTRL3_MBCCVWRC_SHIFT
;
257 return max14577_write_reg(chg
->max14577
->regmap
,
258 MAX14577_CHG_REG_CHG_CTRL3
, reg_data
);
261 static int max14577_init_eoc(struct max14577_charger
*chg
,
264 unsigned int current_bits
= 0xf;
267 switch (chg
->max14577
->dev_type
) {
268 case MAXIM_DEVICE_TYPE_MAX77836
:
270 return -EINVAL
; /* Requested current is too low */
272 if (uamp
>= 7500 && uamp
< 10000)
274 else if (uamp
<= 50000) {
275 /* <5000, 7499> and <10000, 50000> */
276 current_bits
= uamp
/ 5000;
278 uamp
= min(uamp
, 100000U) - 50000U;
279 current_bits
= 0xa + uamp
/ 10000;
283 case MAXIM_DEVICE_TYPE_MAX14577
:
285 if (uamp
< MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN
)
286 return -EINVAL
; /* Requested current is too low */
288 uamp
= min(uamp
, MAX14577_CHARGER_EOC_CURRENT_LIMIT_MAX
);
289 uamp
-= MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN
;
290 current_bits
= uamp
/ MAX14577_CHARGER_EOC_CURRENT_LIMIT_STEP
;
294 reg_data
= current_bits
<< CHGCTRL5_EOCS_SHIFT
;
296 return max14577_update_reg(chg
->max14577
->regmap
,
297 MAX14577_CHG_REG_CHG_CTRL5
, CHGCTRL5_EOCS_MASK
,
301 static int max14577_init_fast_charge(struct max14577_charger
*chg
,
306 const struct maxim_charger_current
*limits
=
307 &maxim_charger_currents
[chg
->max14577
->dev_type
];
309 ret
= maxim_charger_calc_reg_current(limits
, uamp
, uamp
, ®_data
);
311 dev_err(chg
->dev
, "Wrong value for fast charge: %u\n", uamp
);
315 return max14577_update_reg(chg
->max14577
->regmap
,
316 MAX14577_CHG_REG_CHG_CTRL4
,
317 CHGCTRL4_MBCICHWRCL_MASK
| CHGCTRL4_MBCICHWRCH_MASK
,
322 * Sets charger registers to proper and safe default values.
323 * Some of these values are equal to defaults in MAX14577E
324 * data sheet but there are minor differences.
326 static int max14577_charger_reg_init(struct max14577_charger
*chg
)
328 struct regmap
*rmap
= chg
->max14577
->regmap
;
333 * Charger-Type Manual Detection, default off (set CHGTYPMAN to 0)
334 * Charger-Detection Enable, default on (set CHGDETEN to 1)
335 * Combined mask of CHGDETEN and CHGTYPMAN will zero the CHGTYPMAN bit
337 reg_data
= 0x1 << CDETCTRL1_CHGDETEN_SHIFT
;
338 max14577_update_reg(rmap
, MAX14577_REG_CDETCTRL1
,
339 CDETCTRL1_CHGDETEN_MASK
| CDETCTRL1_CHGTYPMAN_MASK
,
343 * Wall-Adapter Rapid Charge, default on
344 * Battery-Charger, default on
346 reg_data
= 0x1 << CHGCTRL2_VCHGR_RC_SHIFT
;
347 reg_data
|= 0x1 << CHGCTRL2_MBCHOSTEN_SHIFT
;
348 max14577_write_reg(rmap
, MAX14577_REG_CHGCTRL2
, reg_data
);
350 /* Auto Charging Stop, default off */
351 reg_data
= 0x0 << CHGCTRL6_AUTOSTOP_SHIFT
;
352 max14577_write_reg(rmap
, MAX14577_REG_CHGCTRL6
, reg_data
);
354 ret
= max14577_init_constant_voltage(chg
, chg
->pdata
->constant_uvolt
);
358 ret
= max14577_init_eoc(chg
, chg
->pdata
->eoc_uamp
);
362 ret
= max14577_init_fast_charge(chg
, chg
->pdata
->fast_charge_uamp
);
366 ret
= max14577_set_fast_charge_timer(chg
,
367 MAXIM_CHARGER_FAST_CHARGE_TIMER_DEFAULT
);
371 /* Initialize Overvoltage-Protection Threshold */
372 switch (chg
->pdata
->ovp_uvolt
) {
379 reg_data
= 0x1 + (chg
->pdata
->ovp_uvolt
- 6000000) / 500000;
382 dev_err(chg
->dev
, "Wrong value for OVP: %u\n",
383 chg
->pdata
->ovp_uvolt
);
386 reg_data
<<= CHGCTRL7_OTPCGHCVS_SHIFT
;
387 max14577_write_reg(rmap
, MAX14577_REG_CHGCTRL7
, reg_data
);
392 /* Support property from charger */
393 static enum power_supply_property max14577_charger_props
[] = {
394 POWER_SUPPLY_PROP_STATUS
,
395 POWER_SUPPLY_PROP_CHARGE_TYPE
,
396 POWER_SUPPLY_PROP_HEALTH
,
397 POWER_SUPPLY_PROP_PRESENT
,
398 POWER_SUPPLY_PROP_ONLINE
,
399 POWER_SUPPLY_PROP_MODEL_NAME
,
400 POWER_SUPPLY_PROP_MANUFACTURER
,
403 static const char * const model_names
[] = {
404 [MAXIM_DEVICE_TYPE_UNKNOWN
] = "MAX14577-like",
405 [MAXIM_DEVICE_TYPE_MAX14577
] = "MAX14577",
406 [MAXIM_DEVICE_TYPE_MAX77836
] = "MAX77836",
408 static const char *manufacturer
= "Maxim Integrated";
410 static int max14577_charger_get_property(struct power_supply
*psy
,
411 enum power_supply_property psp
,
412 union power_supply_propval
*val
)
414 struct max14577_charger
*chg
= power_supply_get_drvdata(psy
);
418 case POWER_SUPPLY_PROP_STATUS
:
419 ret
= max14577_get_charger_state(chg
, &val
->intval
);
421 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
422 ret
= max14577_get_charge_type(chg
, &val
->intval
);
424 case POWER_SUPPLY_PROP_HEALTH
:
425 ret
= max14577_get_battery_health(chg
, &val
->intval
);
427 case POWER_SUPPLY_PROP_PRESENT
:
428 ret
= max14577_get_present(chg
, &val
->intval
);
430 case POWER_SUPPLY_PROP_ONLINE
:
431 ret
= max14577_get_online(chg
, &val
->intval
);
433 case POWER_SUPPLY_PROP_MODEL_NAME
:
434 BUILD_BUG_ON(ARRAY_SIZE(model_names
) != MAXIM_DEVICE_TYPE_NUM
);
435 val
->strval
= model_names
[chg
->max14577
->dev_type
];
437 case POWER_SUPPLY_PROP_MANUFACTURER
:
438 val
->strval
= manufacturer
;
447 static const struct power_supply_desc max14577_charger_desc
= {
448 .name
= "max14577-charger",
449 .type
= POWER_SUPPLY_TYPE_BATTERY
,
450 .properties
= max14577_charger_props
,
451 .num_properties
= ARRAY_SIZE(max14577_charger_props
),
452 .get_property
= max14577_charger_get_property
,
456 static struct max14577_charger_platform_data
*max14577_charger_dt_init(
457 struct platform_device
*pdev
)
459 struct max14577_charger_platform_data
*pdata
;
460 struct device_node
*np
= pdev
->dev
.of_node
;
464 dev_err(&pdev
->dev
, "No charger OF node\n");
465 return ERR_PTR(-EINVAL
);
468 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
470 return ERR_PTR(-ENOMEM
);
472 ret
= of_property_read_u32(np
, "maxim,constant-uvolt",
473 &pdata
->constant_uvolt
);
475 dev_err(&pdev
->dev
, "Cannot parse maxim,constant-uvolt field from DT\n");
479 ret
= of_property_read_u32(np
, "maxim,fast-charge-uamp",
480 &pdata
->fast_charge_uamp
);
482 dev_err(&pdev
->dev
, "Cannot parse maxim,fast-charge-uamp field from DT\n");
486 ret
= of_property_read_u32(np
, "maxim,eoc-uamp", &pdata
->eoc_uamp
);
488 dev_err(&pdev
->dev
, "Cannot parse maxim,eoc-uamp field from DT\n");
492 ret
= of_property_read_u32(np
, "maxim,ovp-uvolt", &pdata
->ovp_uvolt
);
494 dev_err(&pdev
->dev
, "Cannot parse maxim,ovp-uvolt field from DT\n");
500 #else /* CONFIG_OF */
501 static struct max14577_charger_platform_data
*max14577_charger_dt_init(
502 struct platform_device
*pdev
)
506 #endif /* CONFIG_OF */
508 static ssize_t
show_fast_charge_timer(struct device
*dev
,
509 struct device_attribute
*attr
, char *buf
)
511 struct max14577_charger
*chg
= dev_get_drvdata(dev
);
516 ret
= max14577_read_reg(chg
->max14577
->regmap
, MAX14577_REG_CHGCTRL1
,
521 reg_data
&= CHGCTRL1_TCHW_MASK
;
522 reg_data
>>= CHGCTRL1_TCHW_SHIFT
;
535 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
538 static ssize_t
store_fast_charge_timer(struct device
*dev
,
539 struct device_attribute
*attr
, const char *buf
, size_t count
)
541 struct max14577_charger
*chg
= dev_get_drvdata(dev
);
545 ret
= kstrtoul(buf
, 10, &val
);
549 ret
= max14577_set_fast_charge_timer(chg
, val
);
556 static DEVICE_ATTR(fast_charge_timer
, S_IRUGO
| S_IWUSR
,
557 show_fast_charge_timer
, store_fast_charge_timer
);
559 static int max14577_charger_probe(struct platform_device
*pdev
)
561 struct max14577_charger
*chg
;
562 struct power_supply_config psy_cfg
= {};
563 struct max14577
*max14577
= dev_get_drvdata(pdev
->dev
.parent
);
566 chg
= devm_kzalloc(&pdev
->dev
, sizeof(*chg
), GFP_KERNEL
);
570 platform_set_drvdata(pdev
, chg
);
571 chg
->dev
= &pdev
->dev
;
572 chg
->max14577
= max14577
;
574 chg
->pdata
= max14577_charger_dt_init(pdev
);
575 if (IS_ERR_OR_NULL(chg
->pdata
))
576 return PTR_ERR(chg
->pdata
);
578 ret
= max14577_charger_reg_init(chg
);
582 ret
= device_create_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
584 dev_err(&pdev
->dev
, "failed: create sysfs entry\n");
588 psy_cfg
.drv_data
= chg
;
589 chg
->charger
= power_supply_register(&pdev
->dev
, &max14577_charger_desc
,
591 if (IS_ERR(chg
->charger
)) {
592 dev_err(&pdev
->dev
, "failed: power supply register\n");
593 ret
= PTR_ERR(chg
->charger
);
597 /* Check for valid values for charger */
598 BUILD_BUG_ON(MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN
+
599 MAX14577_CHARGER_EOC_CURRENT_LIMIT_STEP
* 0xf !=
600 MAX14577_CHARGER_EOC_CURRENT_LIMIT_MAX
);
604 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
609 static int max14577_charger_remove(struct platform_device
*pdev
)
611 struct max14577_charger
*chg
= platform_get_drvdata(pdev
);
613 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
614 power_supply_unregister(chg
->charger
);
619 static const struct platform_device_id max14577_charger_id
[] = {
620 { "max14577-charger", MAXIM_DEVICE_TYPE_MAX14577
, },
621 { "max77836-charger", MAXIM_DEVICE_TYPE_MAX77836
, },
624 MODULE_DEVICE_TABLE(platform
, max14577_charger_id
);
626 static const struct of_device_id of_max14577_charger_dt_match
[] = {
627 { .compatible
= "maxim,max14577-charger",
628 .data
= (void *)MAXIM_DEVICE_TYPE_MAX14577
, },
629 { .compatible
= "maxim,max77836-charger",
630 .data
= (void *)MAXIM_DEVICE_TYPE_MAX77836
, },
633 MODULE_DEVICE_TABLE(of
, of_max14577_charger_dt_match
);
635 static struct platform_driver max14577_charger_driver
= {
637 .name
= "max14577-charger",
638 .of_match_table
= of_max14577_charger_dt_match
,
640 .probe
= max14577_charger_probe
,
641 .remove
= max14577_charger_remove
,
642 .id_table
= max14577_charger_id
,
644 module_platform_driver(max14577_charger_driver
);
646 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
647 MODULE_DESCRIPTION("Maxim 14577/77836 charger driver");
648 MODULE_LICENSE("GPL");