2 * max14577_charger.c - Battery charger driver for the Maxim 14577/77836
4 * Copyright (C) 2013,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/mfd/max14577-private.h>
22 #include <linux/mfd/max14577.h>
24 struct max14577_charger
{
26 struct max14577
*max14577
;
27 struct power_supply
*charger
;
29 struct max14577_charger_platform_data
*pdata
;
33 * Helper function for mapping values of STATUS2/CHGTYP register on max14577
34 * and max77836 chipsets to enum maxim_muic_charger_type.
36 static enum max14577_muic_charger_type
maxim_get_charger_type(
37 enum maxim_device_type dev_type
, u8 val
) {
39 case MAX14577_CHARGER_TYPE_NONE
:
40 case MAX14577_CHARGER_TYPE_USB
:
41 case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT
:
42 case MAX14577_CHARGER_TYPE_DEDICATED_CHG
:
43 case MAX14577_CHARGER_TYPE_SPECIAL_500MA
:
44 case MAX14577_CHARGER_TYPE_SPECIAL_1A
:
46 case MAX14577_CHARGER_TYPE_DEAD_BATTERY
:
47 case MAX14577_CHARGER_TYPE_RESERVED
:
48 if (dev_type
== MAXIM_DEVICE_TYPE_MAX77836
)
52 WARN_ONCE(1, "max14577: Unsupported chgtyp register value 0x%02x", val
);
57 static int max14577_get_charger_state(struct max14577_charger
*chg
, int *val
)
59 struct regmap
*rmap
= chg
->max14577
->regmap
;
64 * Charging occurs only if:
65 * - CHGCTRL2/MBCHOSTEN == 1
66 * - STATUS2/CGMBC == 1
69 * - handle FULL after Top-off timer (EOC register may be off
70 * and the charger won't be charging although MBCHOSTEN is on)
71 * - handle properly dead-battery charging (respect timer)
72 * - handle timers (fast-charge and prequal) /MBCCHGERR/
74 ret
= max14577_read_reg(rmap
, MAX14577_CHG_REG_CHG_CTRL2
, ®_data
);
78 if ((reg_data
& CHGCTRL2_MBCHOSTEN_MASK
) == 0) {
79 *val
= POWER_SUPPLY_STATUS_DISCHARGING
;
83 ret
= max14577_read_reg(rmap
, MAX14577_CHG_REG_STATUS3
, ®_data
);
87 if (reg_data
& STATUS3_CGMBC_MASK
) {
88 /* Charger or USB-cable is connected */
89 if (reg_data
& STATUS3_EOC_MASK
)
90 *val
= POWER_SUPPLY_STATUS_FULL
;
92 *val
= POWER_SUPPLY_STATUS_CHARGING
;
96 *val
= POWER_SUPPLY_STATUS_DISCHARGING
;
103 * Supported charge types:
104 * - POWER_SUPPLY_CHARGE_TYPE_NONE
105 * - POWER_SUPPLY_CHARGE_TYPE_FAST
107 static int max14577_get_charge_type(struct max14577_charger
*chg
, int *val
)
112 * TODO: CHARGE_TYPE_TRICKLE (VCHGR_RC or EOC)?
114 * [after reaching EOC interrupt]
115 * "When the battery is fully charged, the 30-minute (typ)
116 * top-off timer starts. The device continues to trickle
117 * charge the battery until the top-off timer runs out."
119 ret
= max14577_get_charger_state(chg
, &charging
);
123 if (charging
== POWER_SUPPLY_STATUS_CHARGING
)
124 *val
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
126 *val
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
131 static int max14577_get_online(struct max14577_charger
*chg
, int *val
)
133 struct regmap
*rmap
= chg
->max14577
->regmap
;
136 enum max14577_muic_charger_type chg_type
;
138 ret
= max14577_read_reg(rmap
, MAX14577_MUIC_REG_STATUS2
, ®_data
);
142 reg_data
= ((reg_data
& STATUS2_CHGTYP_MASK
) >> STATUS2_CHGTYP_SHIFT
);
143 chg_type
= maxim_get_charger_type(chg
->max14577
->dev_type
, reg_data
);
145 case MAX14577_CHARGER_TYPE_USB
:
146 case MAX14577_CHARGER_TYPE_DEDICATED_CHG
:
147 case MAX14577_CHARGER_TYPE_SPECIAL_500MA
:
148 case MAX14577_CHARGER_TYPE_SPECIAL_1A
:
149 case MAX14577_CHARGER_TYPE_DEAD_BATTERY
:
150 case MAX77836_CHARGER_TYPE_SPECIAL_BIAS
:
153 case MAX14577_CHARGER_TYPE_NONE
:
154 case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT
:
155 case MAX14577_CHARGER_TYPE_RESERVED
:
156 case MAX77836_CHARGER_TYPE_RESERVED
:
165 * Supported health statuses:
166 * - POWER_SUPPLY_HEALTH_DEAD
167 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
168 * - POWER_SUPPLY_HEALTH_GOOD
170 static int max14577_get_battery_health(struct max14577_charger
*chg
, int *val
)
172 struct regmap
*rmap
= chg
->max14577
->regmap
;
175 enum max14577_muic_charger_type chg_type
;
177 ret
= max14577_read_reg(rmap
, MAX14577_MUIC_REG_STATUS2
, ®_data
);
181 reg_data
= ((reg_data
& STATUS2_CHGTYP_MASK
) >> STATUS2_CHGTYP_SHIFT
);
182 chg_type
= maxim_get_charger_type(chg
->max14577
->dev_type
, reg_data
);
183 if (chg_type
== MAX14577_CHARGER_TYPE_DEAD_BATTERY
) {
184 *val
= POWER_SUPPLY_HEALTH_DEAD
;
188 ret
= max14577_read_reg(rmap
, MAX14577_CHG_REG_STATUS3
, ®_data
);
192 if (reg_data
& STATUS3_OVP_MASK
) {
193 *val
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
197 /* Not dead, not overvoltage */
198 *val
= POWER_SUPPLY_HEALTH_GOOD
;
206 * The max14577 chip doesn't report any status of battery presence.
207 * Lets assume that it will always be used with some battery.
209 static int max14577_get_present(struct max14577_charger
*chg
, int *val
)
216 static int max14577_set_fast_charge_timer(struct max14577_charger
*chg
,
223 reg_data
= hours
- 3;
230 dev_err(chg
->dev
, "Wrong value for Fast-Charge Timer: %lu\n",
234 reg_data
<<= CHGCTRL1_TCHW_SHIFT
;
236 return max14577_update_reg(chg
->max14577
->regmap
,
237 MAX14577_REG_CHGCTRL1
, CHGCTRL1_TCHW_MASK
, reg_data
);
240 static int max14577_init_constant_voltage(struct max14577_charger
*chg
,
245 if (uvolt
< MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN
||
246 uvolt
> MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX
)
249 if (uvolt
== 4200000)
251 else if (uvolt
== MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX
)
253 else if (uvolt
<= 4280000) {
254 unsigned int val
= uvolt
;
256 val
-= MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN
;
257 val
/= MAXIM_CHARGER_CONSTANT_VOLTAGE_STEP
;
258 if (uvolt
<= 4180000)
259 reg_data
= 0x1 + val
;
261 reg_data
= val
; /* Fix for gap between 4.18V and 4.22V */
265 reg_data
<<= CHGCTRL3_MBCCVWRC_SHIFT
;
267 return max14577_write_reg(chg
->max14577
->regmap
,
268 MAX14577_CHG_REG_CHG_CTRL3
, reg_data
);
271 static int max14577_init_eoc(struct max14577_charger
*chg
,
274 unsigned int current_bits
= 0xf;
277 switch (chg
->max14577
->dev_type
) {
278 case MAXIM_DEVICE_TYPE_MAX77836
:
280 return -EINVAL
; /* Requested current is too low */
282 if (uamp
>= 7500 && uamp
< 10000)
284 else if (uamp
<= 50000) {
285 /* <5000, 7499> and <10000, 50000> */
286 current_bits
= uamp
/ 5000;
288 uamp
= min(uamp
, 100000U) - 50000U;
289 current_bits
= 0xa + uamp
/ 10000;
293 case MAXIM_DEVICE_TYPE_MAX14577
:
295 if (uamp
< MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN
)
296 return -EINVAL
; /* Requested current is too low */
298 uamp
= min(uamp
, MAX14577_CHARGER_EOC_CURRENT_LIMIT_MAX
);
299 uamp
-= MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN
;
300 current_bits
= uamp
/ MAX14577_CHARGER_EOC_CURRENT_LIMIT_STEP
;
304 reg_data
= current_bits
<< CHGCTRL5_EOCS_SHIFT
;
306 return max14577_update_reg(chg
->max14577
->regmap
,
307 MAX14577_CHG_REG_CHG_CTRL5
, CHGCTRL5_EOCS_MASK
,
311 static int max14577_init_fast_charge(struct max14577_charger
*chg
,
316 const struct maxim_charger_current
*limits
=
317 &maxim_charger_currents
[chg
->max14577
->dev_type
];
319 ret
= maxim_charger_calc_reg_current(limits
, uamp
, uamp
, ®_data
);
321 dev_err(chg
->dev
, "Wrong value for fast charge: %u\n", uamp
);
325 return max14577_update_reg(chg
->max14577
->regmap
,
326 MAX14577_CHG_REG_CHG_CTRL4
,
327 CHGCTRL4_MBCICHWRCL_MASK
| CHGCTRL4_MBCICHWRCH_MASK
,
332 * Sets charger registers to proper and safe default values.
333 * Some of these values are equal to defaults in MAX14577E
334 * data sheet but there are minor differences.
336 static int max14577_charger_reg_init(struct max14577_charger
*chg
)
338 struct regmap
*rmap
= chg
->max14577
->regmap
;
343 * Charger-Type Manual Detection, default off (set CHGTYPMAN to 0)
344 * Charger-Detection Enable, default on (set CHGDETEN to 1)
345 * Combined mask of CHGDETEN and CHGTYPMAN will zero the CHGTYPMAN bit
347 reg_data
= 0x1 << CDETCTRL1_CHGDETEN_SHIFT
;
348 max14577_update_reg(rmap
, MAX14577_REG_CDETCTRL1
,
349 CDETCTRL1_CHGDETEN_MASK
| CDETCTRL1_CHGTYPMAN_MASK
,
353 * Wall-Adapter Rapid Charge, default on
354 * Battery-Charger, default on
356 reg_data
= 0x1 << CHGCTRL2_VCHGR_RC_SHIFT
;
357 reg_data
|= 0x1 << CHGCTRL2_MBCHOSTEN_SHIFT
;
358 max14577_write_reg(rmap
, MAX14577_REG_CHGCTRL2
, reg_data
);
360 /* Auto Charging Stop, default off */
361 reg_data
= 0x0 << CHGCTRL6_AUTOSTOP_SHIFT
;
362 max14577_write_reg(rmap
, MAX14577_REG_CHGCTRL6
, reg_data
);
364 ret
= max14577_init_constant_voltage(chg
, chg
->pdata
->constant_uvolt
);
368 ret
= max14577_init_eoc(chg
, chg
->pdata
->eoc_uamp
);
372 ret
= max14577_init_fast_charge(chg
, chg
->pdata
->fast_charge_uamp
);
376 ret
= max14577_set_fast_charge_timer(chg
,
377 MAXIM_CHARGER_FAST_CHARGE_TIMER_DEFAULT
);
381 /* Initialize Overvoltage-Protection Threshold */
382 switch (chg
->pdata
->ovp_uvolt
) {
389 reg_data
= 0x1 + (chg
->pdata
->ovp_uvolt
- 6000000) / 500000;
392 dev_err(chg
->dev
, "Wrong value for OVP: %u\n",
393 chg
->pdata
->ovp_uvolt
);
396 reg_data
<<= CHGCTRL7_OTPCGHCVS_SHIFT
;
397 max14577_write_reg(rmap
, MAX14577_REG_CHGCTRL7
, reg_data
);
402 /* Support property from charger */
403 static enum power_supply_property max14577_charger_props
[] = {
404 POWER_SUPPLY_PROP_STATUS
,
405 POWER_SUPPLY_PROP_CHARGE_TYPE
,
406 POWER_SUPPLY_PROP_HEALTH
,
407 POWER_SUPPLY_PROP_PRESENT
,
408 POWER_SUPPLY_PROP_ONLINE
,
409 POWER_SUPPLY_PROP_MODEL_NAME
,
410 POWER_SUPPLY_PROP_MANUFACTURER
,
413 static const char * const model_names
[] = {
414 [MAXIM_DEVICE_TYPE_UNKNOWN
] = "MAX14577-like",
415 [MAXIM_DEVICE_TYPE_MAX14577
] = "MAX14577",
416 [MAXIM_DEVICE_TYPE_MAX77836
] = "MAX77836",
418 static const char *manufacturer
= "Maxim Integrated";
420 static int max14577_charger_get_property(struct power_supply
*psy
,
421 enum power_supply_property psp
,
422 union power_supply_propval
*val
)
424 struct max14577_charger
*chg
= power_supply_get_drvdata(psy
);
428 case POWER_SUPPLY_PROP_STATUS
:
429 ret
= max14577_get_charger_state(chg
, &val
->intval
);
431 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
432 ret
= max14577_get_charge_type(chg
, &val
->intval
);
434 case POWER_SUPPLY_PROP_HEALTH
:
435 ret
= max14577_get_battery_health(chg
, &val
->intval
);
437 case POWER_SUPPLY_PROP_PRESENT
:
438 ret
= max14577_get_present(chg
, &val
->intval
);
440 case POWER_SUPPLY_PROP_ONLINE
:
441 ret
= max14577_get_online(chg
, &val
->intval
);
443 case POWER_SUPPLY_PROP_MODEL_NAME
:
444 BUILD_BUG_ON(ARRAY_SIZE(model_names
) != MAXIM_DEVICE_TYPE_NUM
);
445 val
->strval
= model_names
[chg
->max14577
->dev_type
];
447 case POWER_SUPPLY_PROP_MANUFACTURER
:
448 val
->strval
= manufacturer
;
457 static const struct power_supply_desc max14577_charger_desc
= {
458 .name
= "max14577-charger",
459 .type
= POWER_SUPPLY_TYPE_BATTERY
,
460 .properties
= max14577_charger_props
,
461 .num_properties
= ARRAY_SIZE(max14577_charger_props
),
462 .get_property
= max14577_charger_get_property
,
466 static struct max14577_charger_platform_data
*max14577_charger_dt_init(
467 struct platform_device
*pdev
)
469 struct max14577_charger_platform_data
*pdata
;
470 struct device_node
*np
= pdev
->dev
.of_node
;
474 dev_err(&pdev
->dev
, "No charger OF node\n");
475 return ERR_PTR(-EINVAL
);
478 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
480 return ERR_PTR(-ENOMEM
);
482 ret
= of_property_read_u32(np
, "maxim,constant-uvolt",
483 &pdata
->constant_uvolt
);
485 dev_err(&pdev
->dev
, "Cannot parse maxim,constant-uvolt field from DT\n");
489 ret
= of_property_read_u32(np
, "maxim,fast-charge-uamp",
490 &pdata
->fast_charge_uamp
);
492 dev_err(&pdev
->dev
, "Cannot parse maxim,fast-charge-uamp field from DT\n");
496 ret
= of_property_read_u32(np
, "maxim,eoc-uamp", &pdata
->eoc_uamp
);
498 dev_err(&pdev
->dev
, "Cannot parse maxim,eoc-uamp field from DT\n");
502 ret
= of_property_read_u32(np
, "maxim,ovp-uvolt", &pdata
->ovp_uvolt
);
504 dev_err(&pdev
->dev
, "Cannot parse maxim,ovp-uvolt field from DT\n");
510 #else /* CONFIG_OF */
511 static struct max14577_charger_platform_data
*max14577_charger_dt_init(
512 struct platform_device
*pdev
)
516 #endif /* CONFIG_OF */
518 static ssize_t
show_fast_charge_timer(struct device
*dev
,
519 struct device_attribute
*attr
, char *buf
)
521 struct max14577_charger
*chg
= dev_get_drvdata(dev
);
526 ret
= max14577_read_reg(chg
->max14577
->regmap
, MAX14577_REG_CHGCTRL1
,
531 reg_data
&= CHGCTRL1_TCHW_MASK
;
532 reg_data
>>= CHGCTRL1_TCHW_SHIFT
;
545 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
548 static ssize_t
store_fast_charge_timer(struct device
*dev
,
549 struct device_attribute
*attr
, const char *buf
, size_t count
)
551 struct max14577_charger
*chg
= dev_get_drvdata(dev
);
555 ret
= kstrtoul(buf
, 10, &val
);
559 ret
= max14577_set_fast_charge_timer(chg
, val
);
566 static DEVICE_ATTR(fast_charge_timer
, S_IRUGO
| S_IWUSR
,
567 show_fast_charge_timer
, store_fast_charge_timer
);
569 static int max14577_charger_probe(struct platform_device
*pdev
)
571 struct max14577_charger
*chg
;
572 struct power_supply_config psy_cfg
= {};
573 struct max14577
*max14577
= dev_get_drvdata(pdev
->dev
.parent
);
576 chg
= devm_kzalloc(&pdev
->dev
, sizeof(*chg
), GFP_KERNEL
);
580 platform_set_drvdata(pdev
, chg
);
581 chg
->dev
= &pdev
->dev
;
582 chg
->max14577
= max14577
;
584 chg
->pdata
= max14577_charger_dt_init(pdev
);
585 if (IS_ERR_OR_NULL(chg
->pdata
))
586 return PTR_ERR(chg
->pdata
);
588 ret
= max14577_charger_reg_init(chg
);
592 ret
= device_create_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
594 dev_err(&pdev
->dev
, "failed: create sysfs entry\n");
598 psy_cfg
.drv_data
= chg
;
599 chg
->charger
= power_supply_register(&pdev
->dev
, &max14577_charger_desc
,
601 if (IS_ERR(chg
->charger
)) {
602 dev_err(&pdev
->dev
, "failed: power supply register\n");
603 ret
= PTR_ERR(chg
->charger
);
607 /* Check for valid values for charger */
608 BUILD_BUG_ON(MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN
+
609 MAX14577_CHARGER_EOC_CURRENT_LIMIT_STEP
* 0xf !=
610 MAX14577_CHARGER_EOC_CURRENT_LIMIT_MAX
);
614 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
619 static int max14577_charger_remove(struct platform_device
*pdev
)
621 struct max14577_charger
*chg
= platform_get_drvdata(pdev
);
623 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
624 power_supply_unregister(chg
->charger
);
629 static const struct platform_device_id max14577_charger_id
[] = {
630 { "max14577-charger", MAXIM_DEVICE_TYPE_MAX14577
, },
631 { "max77836-charger", MAXIM_DEVICE_TYPE_MAX77836
, },
634 MODULE_DEVICE_TABLE(platform
, max14577_charger_id
);
636 static struct platform_driver max14577_charger_driver
= {
638 .name
= "max14577-charger",
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 <k.kozlowski@samsung.com>");
647 MODULE_DESCRIPTION("Maxim 14577/77836 charger driver");
648 MODULE_LICENSE("GPL");