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 unsigned int charging_state
;
30 unsigned int battery_state
;
32 struct max14577_charger_platform_data
*pdata
;
36 * Helper function for mapping values of STATUS2/CHGTYP register on max14577
37 * and max77836 chipsets to enum maxim_muic_charger_type.
39 static enum max14577_muic_charger_type
maxim_get_charger_type(
40 enum maxim_device_type dev_type
, u8 val
) {
42 case MAX14577_CHARGER_TYPE_NONE
:
43 case MAX14577_CHARGER_TYPE_USB
:
44 case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT
:
45 case MAX14577_CHARGER_TYPE_DEDICATED_CHG
:
46 case MAX14577_CHARGER_TYPE_SPECIAL_500MA
:
47 case MAX14577_CHARGER_TYPE_SPECIAL_1A
:
49 case MAX14577_CHARGER_TYPE_DEAD_BATTERY
:
50 case MAX14577_CHARGER_TYPE_RESERVED
:
51 if (dev_type
== MAXIM_DEVICE_TYPE_MAX77836
)
55 WARN_ONCE(1, "max14577: Unsupported chgtyp register value 0x%02x", val
);
60 static int max14577_get_charger_state(struct max14577_charger
*chg
)
62 struct regmap
*rmap
= chg
->max14577
->regmap
;
63 int state
= POWER_SUPPLY_STATUS_DISCHARGING
;
67 * Charging occurs only if:
68 * - CHGCTRL2/MBCHOSTEN == 1
69 * - STATUS2/CGMBC == 1
72 * - handle FULL after Top-off timer (EOC register may be off
73 * and the charger won't be charging although MBCHOSTEN is on)
74 * - handle properly dead-battery charging (respect timer)
75 * - handle timers (fast-charge and prequal) /MBCCHGERR/
77 max14577_read_reg(rmap
, MAX14577_CHG_REG_CHG_CTRL2
, ®_data
);
78 if ((reg_data
& CHGCTRL2_MBCHOSTEN_MASK
) == 0)
81 max14577_read_reg(rmap
, MAX14577_CHG_REG_STATUS3
, ®_data
);
82 if (reg_data
& STATUS3_CGMBC_MASK
) {
83 /* Charger or USB-cable is connected */
84 if (reg_data
& STATUS3_EOC_MASK
)
85 state
= POWER_SUPPLY_STATUS_FULL
;
87 state
= POWER_SUPPLY_STATUS_CHARGING
;
92 chg
->charging_state
= state
;
97 * Supported charge types:
98 * - POWER_SUPPLY_CHARGE_TYPE_NONE
99 * - POWER_SUPPLY_CHARGE_TYPE_FAST
101 static int max14577_get_charge_type(struct max14577_charger
*chg
)
104 * TODO: CHARGE_TYPE_TRICKLE (VCHGR_RC or EOC)?
106 * [after reaching EOC interrupt]
107 * "When the battery is fully charged, the 30-minute (typ)
108 * top-off timer starts. The device continues to trickle
109 * charge the battery until the top-off timer runs out."
111 if (max14577_get_charger_state(chg
) == POWER_SUPPLY_STATUS_CHARGING
)
112 return POWER_SUPPLY_CHARGE_TYPE_FAST
;
113 return POWER_SUPPLY_CHARGE_TYPE_NONE
;
116 static int max14577_get_online(struct max14577_charger
*chg
)
118 struct regmap
*rmap
= chg
->max14577
->regmap
;
120 enum max14577_muic_charger_type chg_type
;
122 max14577_read_reg(rmap
, MAX14577_MUIC_REG_STATUS2
, ®_data
);
123 reg_data
= ((reg_data
& STATUS2_CHGTYP_MASK
) >> STATUS2_CHGTYP_SHIFT
);
124 chg_type
= maxim_get_charger_type(chg
->max14577
->dev_type
, reg_data
);
126 case MAX14577_CHARGER_TYPE_USB
:
127 case MAX14577_CHARGER_TYPE_DEDICATED_CHG
:
128 case MAX14577_CHARGER_TYPE_SPECIAL_500MA
:
129 case MAX14577_CHARGER_TYPE_SPECIAL_1A
:
130 case MAX14577_CHARGER_TYPE_DEAD_BATTERY
:
131 case MAX77836_CHARGER_TYPE_SPECIAL_BIAS
:
133 case MAX14577_CHARGER_TYPE_NONE
:
134 case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT
:
135 case MAX14577_CHARGER_TYPE_RESERVED
:
136 case MAX77836_CHARGER_TYPE_RESERVED
:
143 * Supported health statuses:
144 * - POWER_SUPPLY_HEALTH_DEAD
145 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
146 * - POWER_SUPPLY_HEALTH_GOOD
148 static int max14577_get_battery_health(struct max14577_charger
*chg
)
150 struct regmap
*rmap
= chg
->max14577
->regmap
;
151 int state
= POWER_SUPPLY_HEALTH_GOOD
;
153 enum max14577_muic_charger_type chg_type
;
155 max14577_read_reg(rmap
, MAX14577_MUIC_REG_STATUS2
, ®_data
);
156 reg_data
= ((reg_data
& STATUS2_CHGTYP_MASK
) >> STATUS2_CHGTYP_SHIFT
);
157 chg_type
= maxim_get_charger_type(chg
->max14577
->dev_type
, reg_data
);
158 if (chg_type
== MAX14577_CHARGER_TYPE_DEAD_BATTERY
) {
159 state
= POWER_SUPPLY_HEALTH_DEAD
;
163 max14577_read_reg(rmap
, MAX14577_CHG_REG_STATUS3
, ®_data
);
164 if (reg_data
& STATUS3_OVP_MASK
) {
165 state
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
170 chg
->battery_state
= state
;
176 * The max14577 chip doesn't report any status of battery presence.
177 * Lets assume that it will always be used with some battery.
179 static int max14577_get_present(struct max14577_charger
*chg
)
184 static int max14577_set_fast_charge_timer(struct max14577_charger
*chg
,
191 reg_data
= hours
- 3;
198 dev_err(chg
->dev
, "Wrong value for Fast-Charge Timer: %lu\n",
202 reg_data
<<= CHGCTRL1_TCHW_SHIFT
;
204 return max14577_update_reg(chg
->max14577
->regmap
,
205 MAX14577_REG_CHGCTRL1
, CHGCTRL1_TCHW_MASK
, reg_data
);
208 static int max14577_init_constant_voltage(struct max14577_charger
*chg
,
213 if (uvolt
< MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN
||
214 uvolt
> MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX
)
217 if (uvolt
== 4200000)
219 else if (uvolt
== MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX
)
221 else if (uvolt
<= 4280000) {
222 unsigned int val
= uvolt
;
224 val
-= MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN
;
225 val
/= MAXIM_CHARGER_CONSTANT_VOLTAGE_STEP
;
226 if (uvolt
<= 4180000)
227 reg_data
= 0x1 + val
;
229 reg_data
= val
; /* Fix for gap between 4.18V and 4.22V */
233 reg_data
<<= CHGCTRL3_MBCCVWRC_SHIFT
;
235 return max14577_write_reg(chg
->max14577
->regmap
,
236 MAX14577_CHG_REG_CHG_CTRL3
, reg_data
);
239 static int max14577_init_eoc(struct max14577_charger
*chg
,
242 unsigned int current_bits
= 0xf;
245 switch (chg
->max14577
->dev_type
) {
246 case MAXIM_DEVICE_TYPE_MAX77836
:
248 return -EINVAL
; /* Requested current is too low */
250 if (uamp
>= 7500 && uamp
< 10000)
252 else if (uamp
<= 50000) {
253 /* <5000, 7499> and <10000, 50000> */
254 current_bits
= uamp
/ 5000;
256 uamp
= min(uamp
, 100000U) - 50000U;
257 current_bits
= 0xa + uamp
/ 10000;
261 case MAXIM_DEVICE_TYPE_MAX14577
:
263 if (uamp
< MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN
)
264 return -EINVAL
; /* Requested current is too low */
266 uamp
= min(uamp
, MAX14577_CHARGER_EOC_CURRENT_LIMIT_MAX
);
267 uamp
-= MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN
;
268 current_bits
= uamp
/ MAX14577_CHARGER_EOC_CURRENT_LIMIT_STEP
;
272 reg_data
= current_bits
<< CHGCTRL5_EOCS_SHIFT
;
274 return max14577_update_reg(chg
->max14577
->regmap
,
275 MAX14577_CHG_REG_CHG_CTRL5
, CHGCTRL5_EOCS_MASK
,
279 static int max14577_init_fast_charge(struct max14577_charger
*chg
,
284 const struct maxim_charger_current
*limits
=
285 &maxim_charger_currents
[chg
->max14577
->dev_type
];
287 ret
= maxim_charger_calc_reg_current(limits
, uamp
, uamp
, ®_data
);
289 dev_err(chg
->dev
, "Wrong value for fast charge: %u\n", uamp
);
293 return max14577_update_reg(chg
->max14577
->regmap
,
294 MAX14577_CHG_REG_CHG_CTRL4
,
295 CHGCTRL4_MBCICHWRCL_MASK
| CHGCTRL4_MBCICHWRCH_MASK
,
300 * Sets charger registers to proper and safe default values.
301 * Some of these values are equal to defaults in MAX14577E
302 * data sheet but there are minor differences.
304 static int max14577_charger_reg_init(struct max14577_charger
*chg
)
306 struct regmap
*rmap
= chg
->max14577
->regmap
;
311 * Charger-Type Manual Detection, default off (set CHGTYPMAN to 0)
312 * Charger-Detection Enable, default on (set CHGDETEN to 1)
313 * Combined mask of CHGDETEN and CHGTYPMAN will zero the CHGTYPMAN bit
315 reg_data
= 0x1 << CDETCTRL1_CHGDETEN_SHIFT
;
316 max14577_update_reg(rmap
, MAX14577_REG_CDETCTRL1
,
317 CDETCTRL1_CHGDETEN_MASK
| CDETCTRL1_CHGTYPMAN_MASK
,
321 * Wall-Adapter Rapid Charge, default on
322 * Battery-Charger, default on
324 reg_data
= 0x1 << CHGCTRL2_VCHGR_RC_SHIFT
;
325 reg_data
|= 0x1 << CHGCTRL2_MBCHOSTEN_SHIFT
;
326 max14577_write_reg(rmap
, MAX14577_REG_CHGCTRL2
, reg_data
);
328 /* Auto Charging Stop, default off */
329 reg_data
= 0x0 << CHGCTRL6_AUTOSTOP_SHIFT
;
330 max14577_write_reg(rmap
, MAX14577_REG_CHGCTRL6
, reg_data
);
332 ret
= max14577_init_constant_voltage(chg
, chg
->pdata
->constant_uvolt
);
336 ret
= max14577_init_eoc(chg
, chg
->pdata
->eoc_uamp
);
340 ret
= max14577_init_fast_charge(chg
, chg
->pdata
->fast_charge_uamp
);
344 ret
= max14577_set_fast_charge_timer(chg
,
345 MAXIM_CHARGER_FAST_CHARGE_TIMER_DEFAULT
);
349 /* Initialize Overvoltage-Protection Threshold */
350 switch (chg
->pdata
->ovp_uvolt
) {
357 reg_data
= 0x1 + (chg
->pdata
->ovp_uvolt
- 6000000) / 500000;
360 dev_err(chg
->dev
, "Wrong value for OVP: %u\n",
361 chg
->pdata
->ovp_uvolt
);
364 reg_data
<<= CHGCTRL7_OTPCGHCVS_SHIFT
;
365 max14577_write_reg(rmap
, MAX14577_REG_CHGCTRL7
, reg_data
);
370 /* Support property from charger */
371 static enum power_supply_property max14577_charger_props
[] = {
372 POWER_SUPPLY_PROP_STATUS
,
373 POWER_SUPPLY_PROP_CHARGE_TYPE
,
374 POWER_SUPPLY_PROP_HEALTH
,
375 POWER_SUPPLY_PROP_PRESENT
,
376 POWER_SUPPLY_PROP_ONLINE
,
377 POWER_SUPPLY_PROP_MODEL_NAME
,
378 POWER_SUPPLY_PROP_MANUFACTURER
,
381 static const char * const model_names
[] = {
382 [MAXIM_DEVICE_TYPE_UNKNOWN
] = "MAX14577-like",
383 [MAXIM_DEVICE_TYPE_MAX14577
] = "MAX14577",
384 [MAXIM_DEVICE_TYPE_MAX77836
] = "MAX77836",
386 static const char *manufacturer
= "Maxim Integrated";
388 static int max14577_charger_get_property(struct power_supply
*psy
,
389 enum power_supply_property psp
,
390 union power_supply_propval
*val
)
392 struct max14577_charger
*chg
= container_of(psy
,
393 struct max14577_charger
,
398 case POWER_SUPPLY_PROP_STATUS
:
399 val
->intval
= max14577_get_charger_state(chg
);
401 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
402 val
->intval
= max14577_get_charge_type(chg
);
404 case POWER_SUPPLY_PROP_HEALTH
:
405 val
->intval
= max14577_get_battery_health(chg
);
407 case POWER_SUPPLY_PROP_PRESENT
:
408 val
->intval
= max14577_get_present(chg
);
410 case POWER_SUPPLY_PROP_ONLINE
:
411 val
->intval
= max14577_get_online(chg
);
413 case POWER_SUPPLY_PROP_MODEL_NAME
:
414 BUILD_BUG_ON(ARRAY_SIZE(model_names
) != MAXIM_DEVICE_TYPE_NUM
);
415 val
->strval
= model_names
[chg
->max14577
->dev_type
];
417 case POWER_SUPPLY_PROP_MANUFACTURER
:
418 val
->strval
= manufacturer
;
428 static struct max14577_charger_platform_data
*max14577_charger_dt_init(
429 struct platform_device
*pdev
)
431 struct max14577_charger_platform_data
*pdata
;
432 struct device_node
*np
= pdev
->dev
.of_node
;
436 dev_err(&pdev
->dev
, "No charger OF node\n");
437 return ERR_PTR(-EINVAL
);
440 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
442 return ERR_PTR(-ENOMEM
);
444 ret
= of_property_read_u32(np
, "maxim,constant-uvolt",
445 &pdata
->constant_uvolt
);
447 dev_err(&pdev
->dev
, "Cannot parse maxim,constant-uvolt field from DT\n");
451 ret
= of_property_read_u32(np
, "maxim,fast-charge-uamp",
452 &pdata
->fast_charge_uamp
);
454 dev_err(&pdev
->dev
, "Cannot parse maxim,fast-charge-uamp field from DT\n");
458 ret
= of_property_read_u32(np
, "maxim,eoc-uamp", &pdata
->eoc_uamp
);
460 dev_err(&pdev
->dev
, "Cannot parse maxim,eoc-uamp field from DT\n");
464 ret
= of_property_read_u32(np
, "maxim,ovp-uvolt", &pdata
->ovp_uvolt
);
466 dev_err(&pdev
->dev
, "Cannot parse maxim,ovp-uvolt field from DT\n");
472 #else /* CONFIG_OF */
473 static struct max14577_charger_platform_data
*max14577_charger_dt_init(
474 struct platform_device
*pdev
)
478 #endif /* CONFIG_OF */
480 static ssize_t
show_fast_charge_timer(struct device
*dev
,
481 struct device_attribute
*attr
, char *buf
)
483 struct max14577_charger
*chg
= dev_get_drvdata(dev
);
488 ret
= max14577_read_reg(chg
->max14577
->regmap
, MAX14577_REG_CHGCTRL1
,
493 reg_data
&= CHGCTRL1_TCHW_MASK
;
494 reg_data
>>= CHGCTRL1_TCHW_SHIFT
;
507 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
510 static ssize_t
store_fast_charge_timer(struct device
*dev
,
511 struct device_attribute
*attr
, const char *buf
, size_t count
)
513 struct max14577_charger
*chg
= dev_get_drvdata(dev
);
517 ret
= kstrtoul(buf
, 10, &val
);
521 ret
= max14577_set_fast_charge_timer(chg
, val
);
528 static DEVICE_ATTR(fast_charge_timer
, S_IRUGO
| S_IWUSR
,
529 show_fast_charge_timer
, store_fast_charge_timer
);
531 static int max14577_charger_probe(struct platform_device
*pdev
)
533 struct max14577_charger
*chg
;
534 struct max14577
*max14577
= dev_get_drvdata(pdev
->dev
.parent
);
537 chg
= devm_kzalloc(&pdev
->dev
, sizeof(*chg
), GFP_KERNEL
);
541 platform_set_drvdata(pdev
, chg
);
542 chg
->dev
= &pdev
->dev
;
543 chg
->max14577
= max14577
;
545 chg
->pdata
= max14577_charger_dt_init(pdev
);
546 if (IS_ERR_OR_NULL(chg
->pdata
))
547 return PTR_ERR(chg
->pdata
);
549 ret
= max14577_charger_reg_init(chg
);
553 chg
->charger
.name
= "max14577-charger",
554 chg
->charger
.type
= POWER_SUPPLY_TYPE_BATTERY
,
555 chg
->charger
.properties
= max14577_charger_props
,
556 chg
->charger
.num_properties
= ARRAY_SIZE(max14577_charger_props
),
557 chg
->charger
.get_property
= max14577_charger_get_property
,
559 ret
= device_create_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
561 dev_err(&pdev
->dev
, "failed: create sysfs entry\n");
565 ret
= power_supply_register(&pdev
->dev
, &chg
->charger
);
567 dev_err(&pdev
->dev
, "failed: power supply register\n");
571 /* Check for valid values for charger */
572 BUILD_BUG_ON(MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN
+
573 MAX14577_CHARGER_EOC_CURRENT_LIMIT_STEP
* 0xf !=
574 MAX14577_CHARGER_EOC_CURRENT_LIMIT_MAX
);
578 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
583 static int max14577_charger_remove(struct platform_device
*pdev
)
585 struct max14577_charger
*chg
= platform_get_drvdata(pdev
);
587 device_remove_file(&pdev
->dev
, &dev_attr_fast_charge_timer
);
588 power_supply_unregister(&chg
->charger
);
593 static const struct platform_device_id max14577_charger_id
[] = {
594 { "max14577-charger", MAXIM_DEVICE_TYPE_MAX14577
, },
595 { "max77836-charger", MAXIM_DEVICE_TYPE_MAX77836
, },
598 MODULE_DEVICE_TABLE(platform
, max14577_charger_id
);
600 static struct platform_driver max14577_charger_driver
= {
602 .name
= "max14577-charger",
604 .probe
= max14577_charger_probe
,
605 .remove
= max14577_charger_remove
,
606 .id_table
= max14577_charger_id
,
608 module_platform_driver(max14577_charger_driver
);
610 MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
611 MODULE_DESCRIPTION("Maxim 14577/77836 charger driver");
612 MODULE_LICENSE("GPL");