blob: 193530549b1f2908aea970af311d1bc770b2b4bd [file] [log] [blame]
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +01001/*
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +02002 * max14577_charger.c - Battery charger driver for the Maxim 14577/77836
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +01003 *
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +02004 * Copyright (C) 2013,2014 Samsung Electronics
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +01005 * Krzysztof Kozlowski <k.kozlowski@samsung.com>
6 *
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.
11 *
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.
16 */
17
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
23struct max14577_charger {
24 struct device *dev;
25 struct max14577 *max14577;
26 struct power_supply charger;
27
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +020028 unsigned int charging_state;
29 unsigned int battery_state;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +010030};
31
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +020032/*
33 * Helper function for mapping values of STATUS2/CHGTYP register on max14577
34 * and max77836 chipsets to enum maxim_muic_charger_type.
35 */
36static enum max14577_muic_charger_type maxim_get_charger_type(
37 enum maxim_device_type dev_type, u8 val) {
38 switch (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:
45 return val;
46 case MAX14577_CHARGER_TYPE_DEAD_BATTERY:
47 case MAX14577_CHARGER_TYPE_RESERVED:
48 if (dev_type == MAXIM_DEVICE_TYPE_MAX77836)
49 val |= 0x8;
50 return val;
51 default:
52 WARN_ONCE(1, "max14577: Unsupported chgtyp register value 0x%02x", val);
53 return val;
54 }
55}
56
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +010057static int max14577_get_charger_state(struct max14577_charger *chg)
58{
59 struct regmap *rmap = chg->max14577->regmap;
60 int state = POWER_SUPPLY_STATUS_DISCHARGING;
61 u8 reg_data;
62
63 /*
64 * Charging occurs only if:
65 * - CHGCTRL2/MBCHOSTEN == 1
66 * - STATUS2/CGMBC == 1
67 *
68 * TODO:
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/
73 */
74 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
75 if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
76 goto state_set;
77
78 max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
79 if (reg_data & STATUS3_CGMBC_MASK) {
80 /* Charger or USB-cable is connected */
81 if (reg_data & STATUS3_EOC_MASK)
82 state = POWER_SUPPLY_STATUS_FULL;
83 else
84 state = POWER_SUPPLY_STATUS_CHARGING;
85 goto state_set;
86 }
87
88state_set:
89 chg->charging_state = state;
90 return state;
91}
92
93/*
94 * Supported charge types:
95 * - POWER_SUPPLY_CHARGE_TYPE_NONE
96 * - POWER_SUPPLY_CHARGE_TYPE_FAST
97 */
98static int max14577_get_charge_type(struct max14577_charger *chg)
99{
100 /*
101 * TODO: CHARGE_TYPE_TRICKLE (VCHGR_RC or EOC)?
102 * As spec says:
103 * [after reaching EOC interrupt]
104 * "When the battery is fully charged, the 30-minute (typ)
105 * top-off timer starts. The device continues to trickle
106 * charge the battery until the top-off timer runs out."
107 */
108 if (max14577_get_charger_state(chg) == POWER_SUPPLY_STATUS_CHARGING)
109 return POWER_SUPPLY_CHARGE_TYPE_FAST;
110 return POWER_SUPPLY_CHARGE_TYPE_NONE;
111}
112
113static int max14577_get_online(struct max14577_charger *chg)
114{
115 struct regmap *rmap = chg->max14577->regmap;
116 u8 reg_data;
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200117 enum max14577_muic_charger_type chg_type;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100118
119 max14577_read_reg(rmap, MAX14577_MUIC_REG_STATUS2, &reg_data);
120 reg_data = ((reg_data & STATUS2_CHGTYP_MASK) >> STATUS2_CHGTYP_SHIFT);
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200121 chg_type = maxim_get_charger_type(chg->max14577->dev_type, reg_data);
122 switch (chg_type) {
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100123 case MAX14577_CHARGER_TYPE_USB:
124 case MAX14577_CHARGER_TYPE_DEDICATED_CHG:
125 case MAX14577_CHARGER_TYPE_SPECIAL_500MA:
126 case MAX14577_CHARGER_TYPE_SPECIAL_1A:
127 case MAX14577_CHARGER_TYPE_DEAD_BATTERY:
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200128 case MAX77836_CHARGER_TYPE_SPECIAL_BIAS:
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100129 return 1;
130 case MAX14577_CHARGER_TYPE_NONE:
131 case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT:
132 case MAX14577_CHARGER_TYPE_RESERVED:
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200133 case MAX77836_CHARGER_TYPE_RESERVED:
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100134 default:
135 return 0;
136 }
137}
138
139/*
140 * Supported health statuses:
141 * - POWER_SUPPLY_HEALTH_DEAD
142 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
143 * - POWER_SUPPLY_HEALTH_GOOD
144 */
145static int max14577_get_battery_health(struct max14577_charger *chg)
146{
147 struct regmap *rmap = chg->max14577->regmap;
148 int state = POWER_SUPPLY_HEALTH_GOOD;
149 u8 reg_data;
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200150 enum max14577_muic_charger_type chg_type;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100151
152 max14577_read_reg(rmap, MAX14577_MUIC_REG_STATUS2, &reg_data);
153 reg_data = ((reg_data & STATUS2_CHGTYP_MASK) >> STATUS2_CHGTYP_SHIFT);
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200154 chg_type = maxim_get_charger_type(chg->max14577->dev_type, reg_data);
155 if (chg_type == MAX14577_CHARGER_TYPE_DEAD_BATTERY) {
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100156 state = POWER_SUPPLY_HEALTH_DEAD;
157 goto state_set;
158 }
159
160 max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
161 if (reg_data & STATUS3_OVP_MASK) {
162 state = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
163 goto state_set;
164 }
165
166state_set:
167 chg->battery_state = state;
168 return state;
169}
170
171/*
172 * Always returns 1.
173 * The max14577 chip doesn't report any status of battery presence.
174 * Lets assume that it will always be used with some battery.
175 */
176static int max14577_get_present(struct max14577_charger *chg)
177{
178 return 1;
179}
180
181/*
182 * Sets charger registers to proper and safe default values.
183 * Some of these values are equal to defaults in MAX14577E
184 * data sheet but there are minor differences.
185 */
186static void max14577_charger_reg_init(struct max14577_charger *chg)
187{
188 struct regmap *rmap = chg->max14577->regmap;
189 u8 reg_data;
190
191 /*
192 * Charger-Type Manual Detection, default off (set CHGTYPMAN to 0)
193 * Charger-Detection Enable, default on (set CHGDETEN to 1)
194 * Combined mask of CHGDETEN and CHGTYPMAN will zero the CHGTYPMAN bit
195 */
196 reg_data = 0x1 << CDETCTRL1_CHGDETEN_SHIFT;
197 max14577_update_reg(rmap, MAX14577_REG_CDETCTRL1,
198 CDETCTRL1_CHGDETEN_MASK | CDETCTRL1_CHGTYPMAN_MASK,
199 reg_data);
200
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200201 /* Battery Fast-Charge Timer, set to: 6hrs */
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100202 reg_data = 0x3 << CHGCTRL1_TCHW_SHIFT;
203 max14577_write_reg(rmap, MAX14577_REG_CHGCTRL1, reg_data);
204
205 /*
206 * Wall-Adapter Rapid Charge, default on
207 * Battery-Charger, default on
208 */
209 reg_data = 0x1 << CHGCTRL2_VCHGR_RC_SHIFT;
210 reg_data |= 0x1 << CHGCTRL2_MBCHOSTEN_SHIFT;
211 max14577_write_reg(rmap, MAX14577_REG_CHGCTRL2, reg_data);
212
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200213 /* Battery-Charger Constant Voltage (CV) Mode, set to: 4.35V */
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100214 reg_data = 0xf << CHGCTRL3_MBCCVWRC_SHIFT;
215 max14577_write_reg(rmap, MAX14577_REG_CHGCTRL3, reg_data);
216
217 /*
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200218 * Fast Battery-Charge Current Low,
219 * default 200-950mA (max14577) / 100-475mA (max77836)
220 *
221 * Fast Battery-Charge Current High,
222 * set to 450mA (max14577) / 225mA (max77836)
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100223 */
224 reg_data = 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT;
225 reg_data |= 0x5 << CHGCTRL4_MBCICHWRCH_SHIFT;
226 max14577_write_reg(rmap, MAX14577_REG_CHGCTRL4, reg_data);
227
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200228 /* End-of-Charge Current, set to 50mA (max14577) / 7.5mA (max77836) */
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100229 reg_data = 0x0 << CHGCTRL5_EOCS_SHIFT;
230 max14577_write_reg(rmap, MAX14577_REG_CHGCTRL5, reg_data);
231
232 /* Auto Charging Stop, default off */
233 reg_data = 0x0 << CHGCTRL6_AUTOSTOP_SHIFT;
234 max14577_write_reg(rmap, MAX14577_REG_CHGCTRL6, reg_data);
235
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200236 /* Overvoltage-Protection Threshold, set to 6.5V */
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100237 reg_data = 0x2 << CHGCTRL7_OTPCGHCVS_SHIFT;
238 max14577_write_reg(rmap, MAX14577_REG_CHGCTRL7, reg_data);
239}
240
241/* Support property from charger */
242static enum power_supply_property max14577_charger_props[] = {
243 POWER_SUPPLY_PROP_STATUS,
244 POWER_SUPPLY_PROP_CHARGE_TYPE,
245 POWER_SUPPLY_PROP_HEALTH,
246 POWER_SUPPLY_PROP_PRESENT,
247 POWER_SUPPLY_PROP_ONLINE,
248 POWER_SUPPLY_PROP_MODEL_NAME,
249 POWER_SUPPLY_PROP_MANUFACTURER,
250};
251
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200252static const char * const model_names[] = {
253 [MAXIM_DEVICE_TYPE_UNKNOWN] = "MAX14577-like",
254 [MAXIM_DEVICE_TYPE_MAX14577] = "MAX14577",
255 [MAXIM_DEVICE_TYPE_MAX77836] = "MAX77836",
256};
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100257static const char *manufacturer = "Maxim Integrated";
258
259static int max14577_charger_get_property(struct power_supply *psy,
260 enum power_supply_property psp,
261 union power_supply_propval *val)
262{
263 struct max14577_charger *chg = container_of(psy,
264 struct max14577_charger,
265 charger);
266 int ret = 0;
267
268 switch (psp) {
269 case POWER_SUPPLY_PROP_STATUS:
270 val->intval = max14577_get_charger_state(chg);
271 break;
272 case POWER_SUPPLY_PROP_CHARGE_TYPE:
273 val->intval = max14577_get_charge_type(chg);
274 break;
275 case POWER_SUPPLY_PROP_HEALTH:
276 val->intval = max14577_get_battery_health(chg);
277 break;
278 case POWER_SUPPLY_PROP_PRESENT:
279 val->intval = max14577_get_present(chg);
280 break;
281 case POWER_SUPPLY_PROP_ONLINE:
282 val->intval = max14577_get_online(chg);
283 break;
284 case POWER_SUPPLY_PROP_MODEL_NAME:
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200285 BUILD_BUG_ON(ARRAY_SIZE(model_names) != MAXIM_DEVICE_TYPE_NUM);
286 val->strval = model_names[chg->max14577->dev_type];
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100287 break;
288 case POWER_SUPPLY_PROP_MANUFACTURER:
289 val->strval = manufacturer;
290 break;
291 default:
292 return -EINVAL;
293 }
294
295 return ret;
296}
297
298static int max14577_charger_probe(struct platform_device *pdev)
299{
300 struct max14577_charger *chg;
301 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
302 int ret;
303
304 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
305 if (!chg)
306 return -ENOMEM;
307
308 platform_set_drvdata(pdev, chg);
309 chg->dev = &pdev->dev;
310 chg->max14577 = max14577;
311
312 max14577_charger_reg_init(chg);
313
314 chg->charger.name = "max14577-charger",
315 chg->charger.type = POWER_SUPPLY_TYPE_BATTERY,
316 chg->charger.properties = max14577_charger_props,
317 chg->charger.num_properties = ARRAY_SIZE(max14577_charger_props),
318 chg->charger.get_property = max14577_charger_get_property,
319
320 ret = power_supply_register(&pdev->dev, &chg->charger);
321 if (ret) {
322 dev_err(&pdev->dev, "failed: power supply register\n");
323 return ret;
324 }
325
326 return 0;
327}
328
329static int max14577_charger_remove(struct platform_device *pdev)
330{
331 struct max14577_charger *chg = platform_get_drvdata(pdev);
332
333 power_supply_unregister(&chg->charger);
334
335 return 0;
336}
337
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200338static const struct platform_device_id max14577_charger_id[] = {
339 { "max14577-charger", MAXIM_DEVICE_TYPE_MAX14577, },
340 { "max77836-charger", MAXIM_DEVICE_TYPE_MAX77836, },
341 { }
342};
343MODULE_DEVICE_TABLE(platform, max14577_charger_id);
344
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100345static struct platform_driver max14577_charger_driver = {
346 .driver = {
347 .owner = THIS_MODULE,
348 .name = "max14577-charger",
349 },
350 .probe = max14577_charger_probe,
351 .remove = max14577_charger_remove,
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200352 .id_table = max14577_charger_id,
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100353};
354module_platform_driver(max14577_charger_driver);
355
356MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200357MODULE_DESCRIPTION("Maxim 14577/77836 charger driver");
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100358MODULE_LICENSE("GPL");