• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // max14577_charger.c - Battery charger driver for the Maxim 14577/77836
4 //
5 // Copyright (C) 2013,2014 Samsung Electronics
6 // Krzysztof Kozlowski <krzk@kernel.org>
7 
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>
13 
14 struct max14577_charger {
15 	struct device		*dev;
16 	struct max14577		*max14577;
17 	struct power_supply	*charger;
18 
19 	struct max14577_charger_platform_data	*pdata;
20 };
21 
22 /*
23  * Helper function for mapping values of STATUS2/CHGTYP register on max14577
24  * and max77836 chipsets to enum maxim_muic_charger_type.
25  */
maxim_get_charger_type(enum maxim_device_type dev_type,u8 val)26 static enum max14577_muic_charger_type maxim_get_charger_type(
27 		enum maxim_device_type dev_type, u8 val) {
28 	switch (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:
35 		return val;
36 	case MAX14577_CHARGER_TYPE_DEAD_BATTERY:
37 	case MAX14577_CHARGER_TYPE_RESERVED:
38 		if (dev_type == MAXIM_DEVICE_TYPE_MAX77836)
39 			val |= 0x8;
40 		return val;
41 	default:
42 		WARN_ONCE(1, "max14577: Unsupported chgtyp register value 0x%02x", val);
43 		return val;
44 	}
45 }
46 
max14577_get_charger_state(struct max14577_charger * chg,int * val)47 static int max14577_get_charger_state(struct max14577_charger *chg, int *val)
48 {
49 	struct regmap *rmap = chg->max14577->regmap;
50 	int ret;
51 	u8 reg_data;
52 
53 	/*
54 	 * Charging occurs only if:
55 	 *  - CHGCTRL2/MBCHOSTEN == 1
56 	 *  - STATUS2/CGMBC == 1
57 	 *
58 	 * TODO:
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/
63 	 */
64 	ret = max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
65 	if (ret < 0)
66 		goto out;
67 
68 	if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0) {
69 		*val = POWER_SUPPLY_STATUS_DISCHARGING;
70 		goto out;
71 	}
72 
73 	ret = max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
74 	if (ret < 0)
75 		goto out;
76 
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;
81 		else
82 			*val = POWER_SUPPLY_STATUS_CHARGING;
83 		goto out;
84 	}
85 
86 	*val = POWER_SUPPLY_STATUS_DISCHARGING;
87 
88 out:
89 	return ret;
90 }
91 
92 /*
93  * Supported charge types:
94  *  - POWER_SUPPLY_CHARGE_TYPE_NONE
95  *  - POWER_SUPPLY_CHARGE_TYPE_FAST
96  */
max14577_get_charge_type(struct max14577_charger * chg,int * val)97 static int max14577_get_charge_type(struct max14577_charger *chg, int *val)
98 {
99 	int ret, charging;
100 
101 	/*
102 	 * TODO: CHARGE_TYPE_TRICKLE (VCHGR_RC or EOC)?
103 	 * As spec says:
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."
108 	 */
109 	ret = max14577_get_charger_state(chg, &charging);
110 	if (ret < 0)
111 		return ret;
112 
113 	if (charging == POWER_SUPPLY_STATUS_CHARGING)
114 		*val = POWER_SUPPLY_CHARGE_TYPE_FAST;
115 	else
116 		*val = POWER_SUPPLY_CHARGE_TYPE_NONE;
117 
118 	return 0;
119 }
120 
max14577_get_online(struct max14577_charger * chg,int * val)121 static int max14577_get_online(struct max14577_charger *chg, int *val)
122 {
123 	struct regmap *rmap = chg->max14577->regmap;
124 	u8 reg_data;
125 	int ret;
126 	enum max14577_muic_charger_type chg_type;
127 
128 	ret = max14577_read_reg(rmap, MAX14577_MUIC_REG_STATUS2, &reg_data);
129 	if (ret < 0)
130 		return ret;
131 
132 	reg_data = ((reg_data & STATUS2_CHGTYP_MASK) >> STATUS2_CHGTYP_SHIFT);
133 	chg_type = maxim_get_charger_type(chg->max14577->dev_type, reg_data);
134 	switch (chg_type) {
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:
141 		*val = 1;
142 		break;
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:
147 	default:
148 		*val = 0;
149 	}
150 
151 	return 0;
152 }
153 
154 /*
155  * Supported health statuses:
156  *  - POWER_SUPPLY_HEALTH_DEAD
157  *  - POWER_SUPPLY_HEALTH_OVERVOLTAGE
158  *  - POWER_SUPPLY_HEALTH_GOOD
159  */
max14577_get_battery_health(struct max14577_charger * chg,int * val)160 static int max14577_get_battery_health(struct max14577_charger *chg, int *val)
161 {
162 	struct regmap *rmap = chg->max14577->regmap;
163 	int ret;
164 	u8 reg_data;
165 	enum max14577_muic_charger_type chg_type;
166 
167 	ret = max14577_read_reg(rmap, MAX14577_MUIC_REG_STATUS2, &reg_data);
168 	if (ret < 0)
169 		goto out;
170 
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;
175 		goto out;
176 	}
177 
178 	ret = max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
179 	if (ret < 0)
180 		goto out;
181 
182 	if (reg_data & STATUS3_OVP_MASK) {
183 		*val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
184 		goto out;
185 	}
186 
187 	/* Not dead, not overvoltage */
188 	*val = POWER_SUPPLY_HEALTH_GOOD;
189 
190 out:
191 	return ret;
192 }
193 
194 /*
195  * Always returns 1.
196  * The max14577 chip doesn't report any status of battery presence.
197  * Lets assume that it will always be used with some battery.
198  */
max14577_get_present(struct max14577_charger * chg,int * val)199 static int max14577_get_present(struct max14577_charger *chg, int *val)
200 {
201 	*val = 1;
202 
203 	return 0;
204 }
205 
max14577_set_fast_charge_timer(struct max14577_charger * chg,unsigned long hours)206 static int max14577_set_fast_charge_timer(struct max14577_charger *chg,
207 		unsigned long hours)
208 {
209 	u8 reg_data;
210 
211 	switch (hours) {
212 	case 5 ... 7:
213 		reg_data = hours - 3;
214 		break;
215 	case 0:
216 		/* Disable */
217 		reg_data = 0x7;
218 		break;
219 	default:
220 		dev_err(chg->dev, "Wrong value for Fast-Charge Timer: %lu\n",
221 				hours);
222 		return -EINVAL;
223 	}
224 	reg_data <<= CHGCTRL1_TCHW_SHIFT;
225 
226 	return max14577_update_reg(chg->max14577->regmap,
227 			MAX14577_REG_CHGCTRL1, CHGCTRL1_TCHW_MASK, reg_data);
228 }
229 
max14577_init_constant_voltage(struct max14577_charger * chg,unsigned int uvolt)230 static int max14577_init_constant_voltage(struct max14577_charger *chg,
231 		unsigned int uvolt)
232 {
233 	u8 reg_data;
234 
235 	if (uvolt < MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN ||
236 			uvolt > MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX)
237 		return -EINVAL;
238 
239 	if (uvolt == 4200000)
240 		reg_data = 0x0;
241 	else if (uvolt == MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX)
242 		reg_data = 0x1f;
243 	else if (uvolt <= 4280000) {
244 		unsigned int val = uvolt;
245 
246 		val -= MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN;
247 		val /= MAXIM_CHARGER_CONSTANT_VOLTAGE_STEP;
248 		if (uvolt <= 4180000)
249 			reg_data = 0x1 + val;
250 		else
251 			reg_data = val; /* Fix for gap between 4.18V and 4.22V */
252 	} else
253 		return -EINVAL;
254 
255 	reg_data <<= CHGCTRL3_MBCCVWRC_SHIFT;
256 
257 	return max14577_write_reg(chg->max14577->regmap,
258 			MAX14577_CHG_REG_CHG_CTRL3, reg_data);
259 }
260 
max14577_init_eoc(struct max14577_charger * chg,unsigned int uamp)261 static int max14577_init_eoc(struct max14577_charger *chg,
262 		unsigned int uamp)
263 {
264 	unsigned int current_bits;
265 	u8 reg_data;
266 
267 	switch (chg->max14577->dev_type) {
268 	case MAXIM_DEVICE_TYPE_MAX77836:
269 		if (uamp < 5000)
270 			return -EINVAL; /* Requested current is too low */
271 
272 		if (uamp >= 7500 && uamp < 10000)
273 			current_bits = 0x0;
274 		else if (uamp <= 50000) {
275 			/* <5000, 7499> and <10000, 50000> */
276 			current_bits = uamp / 5000;
277 		} else {
278 			uamp = min(uamp, 100000U) - 50000U;
279 			current_bits = 0xa + uamp / 10000;
280 		}
281 		break;
282 
283 	case MAXIM_DEVICE_TYPE_MAX14577:
284 	default:
285 		if (uamp < MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN)
286 			return -EINVAL; /* Requested current is too low */
287 
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;
291 		break;
292 	}
293 
294 	reg_data = current_bits << CHGCTRL5_EOCS_SHIFT;
295 
296 	return max14577_update_reg(chg->max14577->regmap,
297 			MAX14577_CHG_REG_CHG_CTRL5, CHGCTRL5_EOCS_MASK,
298 			reg_data);
299 }
300 
max14577_init_fast_charge(struct max14577_charger * chg,unsigned int uamp)301 static int max14577_init_fast_charge(struct max14577_charger *chg,
302 		unsigned int uamp)
303 {
304 	u8 reg_data;
305 	int ret;
306 	const struct maxim_charger_current *limits =
307 		&maxim_charger_currents[chg->max14577->dev_type];
308 
309 	ret = maxim_charger_calc_reg_current(limits, uamp, uamp, &reg_data);
310 	if (ret) {
311 		dev_err(chg->dev, "Wrong value for fast charge: %u\n", uamp);
312 		return ret;
313 	}
314 
315 	return max14577_update_reg(chg->max14577->regmap,
316 			MAX14577_CHG_REG_CHG_CTRL4,
317 			CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
318 			reg_data);
319 }
320 
321 /*
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.
325  */
max14577_charger_reg_init(struct max14577_charger * chg)326 static int max14577_charger_reg_init(struct max14577_charger *chg)
327 {
328 	struct regmap *rmap = chg->max14577->regmap;
329 	u8 reg_data;
330 	int ret;
331 
332 	/*
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
336 	 */
337 	reg_data = 0x1 << CDETCTRL1_CHGDETEN_SHIFT;
338 	max14577_update_reg(rmap, MAX14577_REG_CDETCTRL1,
339 			CDETCTRL1_CHGDETEN_MASK | CDETCTRL1_CHGTYPMAN_MASK,
340 			reg_data);
341 
342 	/*
343 	 * Wall-Adapter Rapid Charge, default on
344 	 * Battery-Charger, default on
345 	 */
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);
349 
350 	/* Auto Charging Stop, default off */
351 	reg_data = 0x0 << CHGCTRL6_AUTOSTOP_SHIFT;
352 	max14577_write_reg(rmap, MAX14577_REG_CHGCTRL6, reg_data);
353 
354 	ret = max14577_init_constant_voltage(chg, chg->pdata->constant_uvolt);
355 	if (ret)
356 		return ret;
357 
358 	ret = max14577_init_eoc(chg, chg->pdata->eoc_uamp);
359 	if (ret)
360 		return ret;
361 
362 	ret = max14577_init_fast_charge(chg, chg->pdata->fast_charge_uamp);
363 	if (ret)
364 		return ret;
365 
366 	ret = max14577_set_fast_charge_timer(chg,
367 			MAXIM_CHARGER_FAST_CHARGE_TIMER_DEFAULT);
368 	if (ret)
369 		return ret;
370 
371 	/* Initialize Overvoltage-Protection Threshold */
372 	switch (chg->pdata->ovp_uvolt) {
373 	case 7500000:
374 		reg_data = 0x0;
375 		break;
376 	case 6000000:
377 	case 6500000:
378 	case 7000000:
379 		reg_data = 0x1 + (chg->pdata->ovp_uvolt - 6000000) / 500000;
380 		break;
381 	default:
382 		dev_err(chg->dev, "Wrong value for OVP: %u\n",
383 				chg->pdata->ovp_uvolt);
384 		return -EINVAL;
385 	}
386 	reg_data <<= CHGCTRL7_OTPCGHCVS_SHIFT;
387 	max14577_write_reg(rmap, MAX14577_REG_CHGCTRL7, reg_data);
388 
389 	return 0;
390 }
391 
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,
401 };
402 
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",
407 };
408 static const char *manufacturer = "Maxim Integrated";
409 
max14577_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)410 static int max14577_charger_get_property(struct power_supply *psy,
411 			    enum power_supply_property psp,
412 			    union power_supply_propval *val)
413 {
414 	struct max14577_charger *chg = power_supply_get_drvdata(psy);
415 	int ret = 0;
416 
417 	switch (psp) {
418 	case POWER_SUPPLY_PROP_STATUS:
419 		ret = max14577_get_charger_state(chg, &val->intval);
420 		break;
421 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
422 		ret = max14577_get_charge_type(chg, &val->intval);
423 		break;
424 	case POWER_SUPPLY_PROP_HEALTH:
425 		ret = max14577_get_battery_health(chg, &val->intval);
426 		break;
427 	case POWER_SUPPLY_PROP_PRESENT:
428 		ret = max14577_get_present(chg, &val->intval);
429 		break;
430 	case POWER_SUPPLY_PROP_ONLINE:
431 		ret = max14577_get_online(chg, &val->intval);
432 		break;
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];
436 		break;
437 	case POWER_SUPPLY_PROP_MANUFACTURER:
438 		val->strval = manufacturer;
439 		break;
440 	default:
441 		return -EINVAL;
442 	}
443 
444 	return ret;
445 }
446 
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,
453 };
454 
455 #ifdef CONFIG_OF
max14577_charger_dt_init(struct platform_device * pdev)456 static struct max14577_charger_platform_data *max14577_charger_dt_init(
457 		struct platform_device *pdev)
458 {
459 	struct max14577_charger_platform_data *pdata;
460 	struct device_node *np = pdev->dev.of_node;
461 	int ret;
462 
463 	if (!np) {
464 		dev_err(&pdev->dev, "No charger OF node\n");
465 		return ERR_PTR(-EINVAL);
466 	}
467 
468 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
469 	if (!pdata)
470 		return ERR_PTR(-ENOMEM);
471 
472 	ret = of_property_read_u32(np, "maxim,constant-uvolt",
473 			&pdata->constant_uvolt);
474 	if (ret) {
475 		dev_err(&pdev->dev, "Cannot parse maxim,constant-uvolt field from DT\n");
476 		return ERR_PTR(ret);
477 	}
478 
479 	ret = of_property_read_u32(np, "maxim,fast-charge-uamp",
480 			&pdata->fast_charge_uamp);
481 	if (ret) {
482 		dev_err(&pdev->dev, "Cannot parse maxim,fast-charge-uamp field from DT\n");
483 		return ERR_PTR(ret);
484 	}
485 
486 	ret = of_property_read_u32(np, "maxim,eoc-uamp", &pdata->eoc_uamp);
487 	if (ret) {
488 		dev_err(&pdev->dev, "Cannot parse maxim,eoc-uamp field from DT\n");
489 		return ERR_PTR(ret);
490 	}
491 
492 	ret = of_property_read_u32(np, "maxim,ovp-uvolt", &pdata->ovp_uvolt);
493 	if (ret) {
494 		dev_err(&pdev->dev, "Cannot parse maxim,ovp-uvolt field from DT\n");
495 		return ERR_PTR(ret);
496 	}
497 
498 	return pdata;
499 }
500 #else /* CONFIG_OF */
max14577_charger_dt_init(struct platform_device * pdev)501 static struct max14577_charger_platform_data *max14577_charger_dt_init(
502 		struct platform_device *pdev)
503 {
504 	return NULL;
505 }
506 #endif /* CONFIG_OF */
507 
show_fast_charge_timer(struct device * dev,struct device_attribute * attr,char * buf)508 static ssize_t show_fast_charge_timer(struct device *dev,
509 		struct device_attribute *attr, char *buf)
510 {
511 	struct max14577_charger *chg = dev_get_drvdata(dev);
512 	u8 reg_data;
513 	int ret;
514 	unsigned int val;
515 
516 	ret = max14577_read_reg(chg->max14577->regmap, MAX14577_REG_CHGCTRL1,
517 			&reg_data);
518 	if (ret)
519 		return ret;
520 
521 	reg_data &= CHGCTRL1_TCHW_MASK;
522 	reg_data >>= CHGCTRL1_TCHW_SHIFT;
523 	switch (reg_data) {
524 	case 0x2 ... 0x4:
525 		val = reg_data + 3;
526 		break;
527 	case 0x7:
528 		val = 0;
529 		break;
530 	default:
531 		val = 5;
532 		break;
533 	}
534 
535 	return scnprintf(buf, PAGE_SIZE, "%u\n", val);
536 }
537 
store_fast_charge_timer(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)538 static ssize_t store_fast_charge_timer(struct device *dev,
539 		struct device_attribute *attr, const char *buf, size_t count)
540 {
541 	struct max14577_charger *chg = dev_get_drvdata(dev);
542 	unsigned long val;
543 	int ret;
544 
545 	ret = kstrtoul(buf, 10, &val);
546 	if (ret)
547 		return ret;
548 
549 	ret = max14577_set_fast_charge_timer(chg, val);
550 	if (ret)
551 		return ret;
552 
553 	return count;
554 }
555 
556 static DEVICE_ATTR(fast_charge_timer, S_IRUGO | S_IWUSR,
557 		show_fast_charge_timer, store_fast_charge_timer);
558 
max14577_charger_probe(struct platform_device * pdev)559 static int max14577_charger_probe(struct platform_device *pdev)
560 {
561 	struct max14577_charger *chg;
562 	struct power_supply_config psy_cfg = {};
563 	struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
564 	int ret;
565 
566 	chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
567 	if (!chg)
568 		return -ENOMEM;
569 
570 	platform_set_drvdata(pdev, chg);
571 	chg->dev = &pdev->dev;
572 	chg->max14577 = max14577;
573 
574 	chg->pdata = max14577_charger_dt_init(pdev);
575 	if (IS_ERR_OR_NULL(chg->pdata))
576 		return PTR_ERR(chg->pdata);
577 
578 	ret = max14577_charger_reg_init(chg);
579 	if (ret)
580 		return ret;
581 
582 	ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
583 	if (ret) {
584 		dev_err(&pdev->dev, "failed: create sysfs entry\n");
585 		return ret;
586 	}
587 
588 	psy_cfg.drv_data = chg;
589 	chg->charger = power_supply_register(&pdev->dev, &max14577_charger_desc,
590 						&psy_cfg);
591 	if (IS_ERR(chg->charger)) {
592 		dev_err(&pdev->dev, "failed: power supply register\n");
593 		ret = PTR_ERR(chg->charger);
594 		goto err;
595 	}
596 
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);
601 	return 0;
602 
603 err:
604 	device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
605 
606 	return ret;
607 }
608 
max14577_charger_remove(struct platform_device * pdev)609 static int max14577_charger_remove(struct platform_device *pdev)
610 {
611 	struct max14577_charger *chg = platform_get_drvdata(pdev);
612 
613 	device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
614 	power_supply_unregister(chg->charger);
615 
616 	return 0;
617 }
618 
619 static const struct platform_device_id max14577_charger_id[] = {
620 	{ "max14577-charger", MAXIM_DEVICE_TYPE_MAX14577, },
621 	{ "max77836-charger", MAXIM_DEVICE_TYPE_MAX77836, },
622 	{ }
623 };
624 MODULE_DEVICE_TABLE(platform, max14577_charger_id);
625 
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, },
631 	{ },
632 };
633 MODULE_DEVICE_TABLE(of, of_max14577_charger_dt_match);
634 
635 static struct platform_driver max14577_charger_driver = {
636 	.driver = {
637 		.name	= "max14577-charger",
638 		.of_match_table = of_max14577_charger_dt_match,
639 	},
640 	.probe		= max14577_charger_probe,
641 	.remove		= max14577_charger_remove,
642 	.id_table	= max14577_charger_id,
643 };
644 module_platform_driver(max14577_charger_driver);
645 
646 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
647 MODULE_DESCRIPTION("Maxim 14577/77836 charger driver");
648 MODULE_LICENSE("GPL");
649