blob: a36bcaf62dd44fad1044f390db7e908b7f95acfd [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>
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +020022#include <linux/mfd/max14577.h>
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +010023
24struct max14577_charger {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010025 struct device *dev;
26 struct max14577 *max14577;
27 struct power_supply *charger;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +010028
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +020029 struct max14577_charger_platform_data *pdata;
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 Kozlowskidd18a662015-02-24 10:54:45 +010057static int max14577_get_charger_state(struct max14577_charger *chg, int *val)
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +010058{
59 struct regmap *rmap = chg->max14577->regmap;
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +010060 int ret;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +010061 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 */
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +010074 ret = max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
75 if (ret < 0)
76 goto out;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +010077
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +010078 if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0) {
79 *val = POWER_SUPPLY_STATUS_DISCHARGING;
80 goto out;
81 }
82
83 ret = max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
84 if (ret < 0)
85 goto out;
86
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +010087 if (reg_data & STATUS3_CGMBC_MASK) {
88 /* Charger or USB-cable is connected */
89 if (reg_data & STATUS3_EOC_MASK)
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +010090 *val = POWER_SUPPLY_STATUS_FULL;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +010091 else
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +010092 *val = POWER_SUPPLY_STATUS_CHARGING;
93 goto out;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +010094 }
95
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +010096 *val = POWER_SUPPLY_STATUS_DISCHARGING;
97
98out:
99 return ret;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100100}
101
102/*
103 * Supported charge types:
104 * - POWER_SUPPLY_CHARGE_TYPE_NONE
105 * - POWER_SUPPLY_CHARGE_TYPE_FAST
106 */
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100107static int max14577_get_charge_type(struct max14577_charger *chg, int *val)
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100108{
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100109 int ret, charging;
110
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100111 /*
112 * TODO: CHARGE_TYPE_TRICKLE (VCHGR_RC or EOC)?
113 * As spec says:
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."
118 */
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100119 ret = max14577_get_charger_state(chg, &charging);
120 if (ret < 0)
121 return ret;
122
123 if (charging == POWER_SUPPLY_STATUS_CHARGING)
124 *val = POWER_SUPPLY_CHARGE_TYPE_FAST;
125 else
126 *val = POWER_SUPPLY_CHARGE_TYPE_NONE;
127
128 return 0;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100129}
130
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100131static int max14577_get_online(struct max14577_charger *chg, int *val)
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100132{
133 struct regmap *rmap = chg->max14577->regmap;
134 u8 reg_data;
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100135 int ret;
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200136 enum max14577_muic_charger_type chg_type;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100137
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100138 ret = max14577_read_reg(rmap, MAX14577_MUIC_REG_STATUS2, &reg_data);
139 if (ret < 0)
140 return ret;
141
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100142 reg_data = ((reg_data & STATUS2_CHGTYP_MASK) >> STATUS2_CHGTYP_SHIFT);
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200143 chg_type = maxim_get_charger_type(chg->max14577->dev_type, reg_data);
144 switch (chg_type) {
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100145 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:
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200150 case MAX77836_CHARGER_TYPE_SPECIAL_BIAS:
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100151 *val = 1;
152 break;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100153 case MAX14577_CHARGER_TYPE_NONE:
154 case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT:
155 case MAX14577_CHARGER_TYPE_RESERVED:
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200156 case MAX77836_CHARGER_TYPE_RESERVED:
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100157 default:
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100158 *val = 0;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100159 }
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100160
161 return 0;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100162}
163
164/*
165 * Supported health statuses:
166 * - POWER_SUPPLY_HEALTH_DEAD
167 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
168 * - POWER_SUPPLY_HEALTH_GOOD
169 */
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100170static int max14577_get_battery_health(struct max14577_charger *chg, int *val)
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100171{
172 struct regmap *rmap = chg->max14577->regmap;
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100173 int ret;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100174 u8 reg_data;
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200175 enum max14577_muic_charger_type chg_type;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100176
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100177 ret = max14577_read_reg(rmap, MAX14577_MUIC_REG_STATUS2, &reg_data);
178 if (ret < 0)
179 goto out;
180
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100181 reg_data = ((reg_data & STATUS2_CHGTYP_MASK) >> STATUS2_CHGTYP_SHIFT);
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200182 chg_type = maxim_get_charger_type(chg->max14577->dev_type, reg_data);
183 if (chg_type == MAX14577_CHARGER_TYPE_DEAD_BATTERY) {
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100184 *val = POWER_SUPPLY_HEALTH_DEAD;
185 goto out;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100186 }
187
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100188 ret = max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
189 if (ret < 0)
190 goto out;
191
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100192 if (reg_data & STATUS3_OVP_MASK) {
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100193 *val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
194 goto out;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100195 }
196
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100197 /* Not dead, not overvoltage */
198 *val = POWER_SUPPLY_HEALTH_GOOD;
199
200out:
201 return ret;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100202}
203
204/*
205 * Always returns 1.
206 * The max14577 chip doesn't report any status of battery presence.
207 * Lets assume that it will always be used with some battery.
208 */
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100209static int max14577_get_present(struct max14577_charger *chg, int *val)
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100210{
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100211 *val = 1;
212
213 return 0;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100214}
215
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200216static int max14577_set_fast_charge_timer(struct max14577_charger *chg,
217 unsigned long hours)
218{
219 u8 reg_data;
220
221 switch (hours) {
222 case 5 ... 7:
223 reg_data = hours - 3;
224 break;
225 case 0:
226 /* Disable */
227 reg_data = 0x7;
228 break;
229 default:
230 dev_err(chg->dev, "Wrong value for Fast-Charge Timer: %lu\n",
231 hours);
232 return -EINVAL;
233 }
234 reg_data <<= CHGCTRL1_TCHW_SHIFT;
235
236 return max14577_update_reg(chg->max14577->regmap,
237 MAX14577_REG_CHGCTRL1, CHGCTRL1_TCHW_MASK, reg_data);
238}
239
240static int max14577_init_constant_voltage(struct max14577_charger *chg,
241 unsigned int uvolt)
242{
243 u8 reg_data;
244
245 if (uvolt < MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN ||
246 uvolt > MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX)
247 return -EINVAL;
248
249 if (uvolt == 4200000)
250 reg_data = 0x0;
251 else if (uvolt == MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX)
252 reg_data = 0x1f;
253 else if (uvolt <= 4280000) {
254 unsigned int val = uvolt;
255
256 val -= MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN;
257 val /= MAXIM_CHARGER_CONSTANT_VOLTAGE_STEP;
258 if (uvolt <= 4180000)
259 reg_data = 0x1 + val;
260 else
261 reg_data = val; /* Fix for gap between 4.18V and 4.22V */
262 } else
263 return -EINVAL;
264
265 reg_data <<= CHGCTRL3_MBCCVWRC_SHIFT;
266
267 return max14577_write_reg(chg->max14577->regmap,
268 MAX14577_CHG_REG_CHG_CTRL3, reg_data);
269}
270
271static int max14577_init_eoc(struct max14577_charger *chg,
272 unsigned int uamp)
273{
274 unsigned int current_bits = 0xf;
275 u8 reg_data;
276
277 switch (chg->max14577->dev_type) {
278 case MAXIM_DEVICE_TYPE_MAX77836:
279 if (uamp < 5000)
280 return -EINVAL; /* Requested current is too low */
281
282 if (uamp >= 7500 && uamp < 10000)
283 current_bits = 0x0;
284 else if (uamp <= 50000) {
285 /* <5000, 7499> and <10000, 50000> */
286 current_bits = uamp / 5000;
287 } else {
288 uamp = min(uamp, 100000U) - 50000U;
289 current_bits = 0xa + uamp / 10000;
290 }
291 break;
292
293 case MAXIM_DEVICE_TYPE_MAX14577:
294 default:
295 if (uamp < MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN)
296 return -EINVAL; /* Requested current is too low */
297
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;
301 break;
302 }
303
304 reg_data = current_bits << CHGCTRL5_EOCS_SHIFT;
305
306 return max14577_update_reg(chg->max14577->regmap,
307 MAX14577_CHG_REG_CHG_CTRL5, CHGCTRL5_EOCS_MASK,
308 reg_data);
309}
310
311static int max14577_init_fast_charge(struct max14577_charger *chg,
312 unsigned int uamp)
313{
314 u8 reg_data;
315 int ret;
316 const struct maxim_charger_current *limits =
317 &maxim_charger_currents[chg->max14577->dev_type];
318
319 ret = maxim_charger_calc_reg_current(limits, uamp, uamp, &reg_data);
320 if (ret) {
321 dev_err(chg->dev, "Wrong value for fast charge: %u\n", uamp);
322 return ret;
323 }
324
325 return max14577_update_reg(chg->max14577->regmap,
326 MAX14577_CHG_REG_CHG_CTRL4,
327 CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
328 reg_data);
329}
330
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100331/*
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.
335 */
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200336static int max14577_charger_reg_init(struct max14577_charger *chg)
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100337{
338 struct regmap *rmap = chg->max14577->regmap;
339 u8 reg_data;
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200340 int ret;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100341
342 /*
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
346 */
347 reg_data = 0x1 << CDETCTRL1_CHGDETEN_SHIFT;
348 max14577_update_reg(rmap, MAX14577_REG_CDETCTRL1,
349 CDETCTRL1_CHGDETEN_MASK | CDETCTRL1_CHGTYPMAN_MASK,
350 reg_data);
351
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100352 /*
353 * Wall-Adapter Rapid Charge, default on
354 * Battery-Charger, default on
355 */
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);
359
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100360 /* Auto Charging Stop, default off */
361 reg_data = 0x0 << CHGCTRL6_AUTOSTOP_SHIFT;
362 max14577_write_reg(rmap, MAX14577_REG_CHGCTRL6, reg_data);
363
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200364 ret = max14577_init_constant_voltage(chg, chg->pdata->constant_uvolt);
365 if (ret)
366 return ret;
367
368 ret = max14577_init_eoc(chg, chg->pdata->eoc_uamp);
369 if (ret)
370 return ret;
371
372 ret = max14577_init_fast_charge(chg, chg->pdata->fast_charge_uamp);
373 if (ret)
374 return ret;
375
376 ret = max14577_set_fast_charge_timer(chg,
377 MAXIM_CHARGER_FAST_CHARGE_TIMER_DEFAULT);
378 if (ret)
379 return ret;
380
381 /* Initialize Overvoltage-Protection Threshold */
382 switch (chg->pdata->ovp_uvolt) {
383 case 7500000:
384 reg_data = 0x0;
385 break;
386 case 6000000:
387 case 6500000:
388 case 7000000:
389 reg_data = 0x1 + (chg->pdata->ovp_uvolt - 6000000) / 500000;
390 break;
391 default:
392 dev_err(chg->dev, "Wrong value for OVP: %u\n",
393 chg->pdata->ovp_uvolt);
394 return -EINVAL;
395 }
396 reg_data <<= CHGCTRL7_OTPCGHCVS_SHIFT;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100397 max14577_write_reg(rmap, MAX14577_REG_CHGCTRL7, reg_data);
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200398
399 return 0;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100400}
401
402/* Support property from charger */
403static 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,
411};
412
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200413static const char * const model_names[] = {
414 [MAXIM_DEVICE_TYPE_UNKNOWN] = "MAX14577-like",
415 [MAXIM_DEVICE_TYPE_MAX14577] = "MAX14577",
416 [MAXIM_DEVICE_TYPE_MAX77836] = "MAX77836",
417};
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100418static const char *manufacturer = "Maxim Integrated";
419
420static int max14577_charger_get_property(struct power_supply *psy,
421 enum power_supply_property psp,
422 union power_supply_propval *val)
423{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100424 struct max14577_charger *chg = power_supply_get_drvdata(psy);
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100425 int ret = 0;
426
427 switch (psp) {
428 case POWER_SUPPLY_PROP_STATUS:
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100429 ret = max14577_get_charger_state(chg, &val->intval);
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100430 break;
431 case POWER_SUPPLY_PROP_CHARGE_TYPE:
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100432 ret = max14577_get_charge_type(chg, &val->intval);
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100433 break;
434 case POWER_SUPPLY_PROP_HEALTH:
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100435 ret = max14577_get_battery_health(chg, &val->intval);
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100436 break;
437 case POWER_SUPPLY_PROP_PRESENT:
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100438 ret = max14577_get_present(chg, &val->intval);
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100439 break;
440 case POWER_SUPPLY_PROP_ONLINE:
Krzysztof Kozlowskidd18a662015-02-24 10:54:45 +0100441 ret = max14577_get_online(chg, &val->intval);
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100442 break;
443 case POWER_SUPPLY_PROP_MODEL_NAME:
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200444 BUILD_BUG_ON(ARRAY_SIZE(model_names) != MAXIM_DEVICE_TYPE_NUM);
445 val->strval = model_names[chg->max14577->dev_type];
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100446 break;
447 case POWER_SUPPLY_PROP_MANUFACTURER:
448 val->strval = manufacturer;
449 break;
450 default:
451 return -EINVAL;
452 }
453
454 return ret;
455}
456
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100457static 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,
463};
464
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200465#ifdef CONFIG_OF
466static struct max14577_charger_platform_data *max14577_charger_dt_init(
467 struct platform_device *pdev)
468{
469 struct max14577_charger_platform_data *pdata;
470 struct device_node *np = pdev->dev.of_node;
471 int ret;
472
473 if (!np) {
474 dev_err(&pdev->dev, "No charger OF node\n");
475 return ERR_PTR(-EINVAL);
476 }
477
478 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
479 if (!pdata)
480 return ERR_PTR(-ENOMEM);
481
482 ret = of_property_read_u32(np, "maxim,constant-uvolt",
483 &pdata->constant_uvolt);
484 if (ret) {
485 dev_err(&pdev->dev, "Cannot parse maxim,constant-uvolt field from DT\n");
486 return ERR_PTR(ret);
487 }
488
489 ret = of_property_read_u32(np, "maxim,fast-charge-uamp",
490 &pdata->fast_charge_uamp);
491 if (ret) {
492 dev_err(&pdev->dev, "Cannot parse maxim,fast-charge-uamp field from DT\n");
493 return ERR_PTR(ret);
494 }
495
496 ret = of_property_read_u32(np, "maxim,eoc-uamp", &pdata->eoc_uamp);
497 if (ret) {
498 dev_err(&pdev->dev, "Cannot parse maxim,eoc-uamp field from DT\n");
499 return ERR_PTR(ret);
500 }
501
502 ret = of_property_read_u32(np, "maxim,ovp-uvolt", &pdata->ovp_uvolt);
503 if (ret) {
504 dev_err(&pdev->dev, "Cannot parse maxim,ovp-uvolt field from DT\n");
505 return ERR_PTR(ret);
506 }
507
508 return pdata;
509}
510#else /* CONFIG_OF */
511static struct max14577_charger_platform_data *max14577_charger_dt_init(
512 struct platform_device *pdev)
513{
514 return NULL;
515}
516#endif /* CONFIG_OF */
517
518static ssize_t show_fast_charge_timer(struct device *dev,
519 struct device_attribute *attr, char *buf)
520{
521 struct max14577_charger *chg = dev_get_drvdata(dev);
522 u8 reg_data;
523 int ret;
524 unsigned int val;
525
526 ret = max14577_read_reg(chg->max14577->regmap, MAX14577_REG_CHGCTRL1,
527 &reg_data);
528 if (ret)
529 return ret;
530
531 reg_data &= CHGCTRL1_TCHW_MASK;
532 reg_data >>= CHGCTRL1_TCHW_SHIFT;
533 switch (reg_data) {
534 case 0x2 ... 0x4:
535 val = reg_data + 3;
536 break;
537 case 0x7:
538 val = 0;
539 break;
540 default:
541 val = 5;
542 break;
543 }
544
545 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
546}
547
548static ssize_t store_fast_charge_timer(struct device *dev,
549 struct device_attribute *attr, const char *buf, size_t count)
550{
551 struct max14577_charger *chg = dev_get_drvdata(dev);
552 unsigned long val;
553 int ret;
554
555 ret = kstrtoul(buf, 10, &val);
556 if (ret)
557 return ret;
558
559 ret = max14577_set_fast_charge_timer(chg, val);
560 if (ret)
561 return ret;
562
563 return count;
564}
565
566static DEVICE_ATTR(fast_charge_timer, S_IRUGO | S_IWUSR,
567 show_fast_charge_timer, store_fast_charge_timer);
568
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100569static int max14577_charger_probe(struct platform_device *pdev)
570{
571 struct max14577_charger *chg;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100572 struct power_supply_config psy_cfg = {};
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100573 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
574 int ret;
575
576 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
577 if (!chg)
578 return -ENOMEM;
579
580 platform_set_drvdata(pdev, chg);
581 chg->dev = &pdev->dev;
582 chg->max14577 = max14577;
583
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200584 chg->pdata = max14577_charger_dt_init(pdev);
585 if (IS_ERR_OR_NULL(chg->pdata))
586 return PTR_ERR(chg->pdata);
587
588 ret = max14577_charger_reg_init(chg);
589 if (ret)
590 return ret;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100591
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200592 ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100593 if (ret) {
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200594 dev_err(&pdev->dev, "failed: create sysfs entry\n");
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100595 return ret;
596 }
597
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100598 psy_cfg.drv_data = chg;
599 chg->charger = power_supply_register(&pdev->dev, &max14577_charger_desc,
600 &psy_cfg);
601 if (IS_ERR(chg->charger)) {
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200602 dev_err(&pdev->dev, "failed: power supply register\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100603 ret = PTR_ERR(chg->charger);
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200604 goto err;
605 }
606
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);
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100611 return 0;
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200612
613err:
614 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
615
616 return ret;
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100617}
618
619static int max14577_charger_remove(struct platform_device *pdev)
620{
621 struct max14577_charger *chg = platform_get_drvdata(pdev);
622
Krzysztof Kozlowskie30110e2014-09-12 08:53:57 +0200623 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100624 power_supply_unregister(chg->charger);
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100625
626 return 0;
627}
628
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200629static const struct platform_device_id max14577_charger_id[] = {
630 { "max14577-charger", MAXIM_DEVICE_TYPE_MAX14577, },
631 { "max77836-charger", MAXIM_DEVICE_TYPE_MAX77836, },
632 { }
633};
634MODULE_DEVICE_TABLE(platform, max14577_charger_id);
635
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100636static struct platform_driver max14577_charger_driver = {
637 .driver = {
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100638 .name = "max14577-charger",
639 },
640 .probe = max14577_charger_probe,
641 .remove = max14577_charger_remove,
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200642 .id_table = max14577_charger_id,
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100643};
644module_platform_driver(max14577_charger_driver);
645
646MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
Krzysztof Kozlowski3682a8e2014-09-12 08:53:55 +0200647MODULE_DESCRIPTION("Maxim 14577/77836 charger driver");
Krzysztof Kozlowski55e64702013-12-06 12:32:12 +0100648MODULE_LICENSE("GPL");