1 /*
2  * max77693_charger.c - Battery charger driver for the Maxim 77693
3  *
4  * Copyright (C) 2014 Samsung Electronics
5  * Krzysztof Kozlowski <krzk@kernel.org>
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/regmap.h>
22 #include <linux/mfd/max77693.h>
23 #include <linux/mfd/max77693-common.h>
24 #include <linux/mfd/max77693-private.h>
25 
26 #define MAX77693_CHARGER_NAME				"max77693-charger"
27 static const char *max77693_charger_model		= "MAX77693";
28 static const char *max77693_charger_manufacturer	= "Maxim Integrated";
29 
30 struct max77693_charger {
31 	struct device		*dev;
32 	struct max77693_dev	*max77693;
33 	struct power_supply	*charger;
34 
35 	u32 constant_volt;
36 	u32 min_system_volt;
37 	u32 thermal_regulation_temp;
38 	u32 batttery_overcurrent;
39 	u32 charge_input_threshold_volt;
40 };
41 
max77693_get_charger_state(struct regmap * regmap,int * val)42 static int max77693_get_charger_state(struct regmap *regmap, int *val)
43 {
44 	int ret;
45 	unsigned int data;
46 
47 	ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
48 	if (ret < 0)
49 		return ret;
50 
51 	data &= CHG_DETAILS_01_CHG_MASK;
52 	data >>= CHG_DETAILS_01_CHG_SHIFT;
53 
54 	switch (data) {
55 	case MAX77693_CHARGING_PREQUALIFICATION:
56 	case MAX77693_CHARGING_FAST_CONST_CURRENT:
57 	case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
58 	case MAX77693_CHARGING_TOP_OFF:
59 	/* In high temp the charging current is reduced, but still charging */
60 	case MAX77693_CHARGING_HIGH_TEMP:
61 		*val = POWER_SUPPLY_STATUS_CHARGING;
62 		break;
63 	case MAX77693_CHARGING_DONE:
64 		*val = POWER_SUPPLY_STATUS_FULL;
65 		break;
66 	case MAX77693_CHARGING_TIMER_EXPIRED:
67 	case MAX77693_CHARGING_THERMISTOR_SUSPEND:
68 		*val = POWER_SUPPLY_STATUS_NOT_CHARGING;
69 		break;
70 	case MAX77693_CHARGING_OFF:
71 	case MAX77693_CHARGING_OVER_TEMP:
72 	case MAX77693_CHARGING_WATCHDOG_EXPIRED:
73 		*val = POWER_SUPPLY_STATUS_DISCHARGING;
74 		break;
75 	case MAX77693_CHARGING_RESERVED:
76 	default:
77 		*val = POWER_SUPPLY_STATUS_UNKNOWN;
78 	}
79 
80 	return 0;
81 }
82 
max77693_get_charge_type(struct regmap * regmap,int * val)83 static int max77693_get_charge_type(struct regmap *regmap, int *val)
84 {
85 	int ret;
86 	unsigned int data;
87 
88 	ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
89 	if (ret < 0)
90 		return ret;
91 
92 	data &= CHG_DETAILS_01_CHG_MASK;
93 	data >>= CHG_DETAILS_01_CHG_SHIFT;
94 
95 	switch (data) {
96 	case MAX77693_CHARGING_PREQUALIFICATION:
97 	/*
98 	 * Top-off: trickle or fast? In top-off the current varies between
99 	 * 100 and 250 mA. It is higher than prequalification current.
100 	 */
101 	case MAX77693_CHARGING_TOP_OFF:
102 		*val = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
103 		break;
104 	case MAX77693_CHARGING_FAST_CONST_CURRENT:
105 	case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
106 	/* In high temp the charging current is reduced, but still charging */
107 	case MAX77693_CHARGING_HIGH_TEMP:
108 		*val = POWER_SUPPLY_CHARGE_TYPE_FAST;
109 		break;
110 	case MAX77693_CHARGING_DONE:
111 	case MAX77693_CHARGING_TIMER_EXPIRED:
112 	case MAX77693_CHARGING_THERMISTOR_SUSPEND:
113 	case MAX77693_CHARGING_OFF:
114 	case MAX77693_CHARGING_OVER_TEMP:
115 	case MAX77693_CHARGING_WATCHDOG_EXPIRED:
116 		*val = POWER_SUPPLY_CHARGE_TYPE_NONE;
117 		break;
118 	case MAX77693_CHARGING_RESERVED:
119 	default:
120 		*val = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
121 	}
122 
123 	return 0;
124 }
125 
126 /*
127  * Supported health statuses:
128  *  - POWER_SUPPLY_HEALTH_DEAD
129  *  - POWER_SUPPLY_HEALTH_GOOD
130  *  - POWER_SUPPLY_HEALTH_OVERVOLTAGE
131  *  - POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
132  *  - POWER_SUPPLY_HEALTH_UNKNOWN
133  *  - POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
134  */
max77693_get_battery_health(struct regmap * regmap,int * val)135 static int max77693_get_battery_health(struct regmap *regmap, int *val)
136 {
137 	int ret;
138 	unsigned int data;
139 
140 	ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
141 	if (ret < 0)
142 		return ret;
143 
144 	data &= CHG_DETAILS_01_BAT_MASK;
145 	data >>= CHG_DETAILS_01_BAT_SHIFT;
146 
147 	switch (data) {
148 	case MAX77693_BATTERY_NOBAT:
149 		*val = POWER_SUPPLY_HEALTH_DEAD;
150 		break;
151 	case MAX77693_BATTERY_PREQUALIFICATION:
152 	case MAX77693_BATTERY_GOOD:
153 	case MAX77693_BATTERY_LOWVOLTAGE:
154 		*val = POWER_SUPPLY_HEALTH_GOOD;
155 		break;
156 	case MAX77693_BATTERY_TIMER_EXPIRED:
157 		/*
158 		 * Took longer to charge than expected, charging suspended.
159 		 * Damaged battery?
160 		 */
161 		*val = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
162 		break;
163 	case MAX77693_BATTERY_OVERVOLTAGE:
164 		*val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
165 		break;
166 	case MAX77693_BATTERY_OVERCURRENT:
167 		*val = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
168 		break;
169 	case MAX77693_BATTERY_RESERVED:
170 	default:
171 		*val = POWER_SUPPLY_HEALTH_UNKNOWN;
172 		break;
173 	}
174 
175 	return 0;
176 }
177 
max77693_get_present(struct regmap * regmap,int * val)178 static int max77693_get_present(struct regmap *regmap, int *val)
179 {
180 	unsigned int data;
181 	int ret;
182 
183 	/*
184 	 * Read CHG_INT_OK register. High DETBAT bit here should be
185 	 * equal to value 0x0 in CHG_DETAILS_01/BAT field.
186 	 */
187 	ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
188 	if (ret < 0)
189 		return ret;
190 
191 	*val = (data & CHG_INT_OK_DETBAT_MASK) ? 0 : 1;
192 
193 	return 0;
194 }
195 
max77693_get_online(struct regmap * regmap,int * val)196 static int max77693_get_online(struct regmap *regmap, int *val)
197 {
198 	unsigned int data;
199 	int ret;
200 
201 	ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
202 	if (ret < 0)
203 		return ret;
204 
205 	*val = (data & CHG_INT_OK_CHGIN_MASK) ? 1 : 0;
206 
207 	return 0;
208 }
209 
210 static enum power_supply_property max77693_charger_props[] = {
211 	POWER_SUPPLY_PROP_STATUS,
212 	POWER_SUPPLY_PROP_CHARGE_TYPE,
213 	POWER_SUPPLY_PROP_HEALTH,
214 	POWER_SUPPLY_PROP_PRESENT,
215 	POWER_SUPPLY_PROP_ONLINE,
216 	POWER_SUPPLY_PROP_MODEL_NAME,
217 	POWER_SUPPLY_PROP_MANUFACTURER,
218 };
219 
max77693_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)220 static int max77693_charger_get_property(struct power_supply *psy,
221 			    enum power_supply_property psp,
222 			    union power_supply_propval *val)
223 {
224 	struct max77693_charger *chg = power_supply_get_drvdata(psy);
225 	struct regmap *regmap = chg->max77693->regmap;
226 	int ret = 0;
227 
228 	switch (psp) {
229 	case POWER_SUPPLY_PROP_STATUS:
230 		ret = max77693_get_charger_state(regmap, &val->intval);
231 		break;
232 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
233 		ret = max77693_get_charge_type(regmap, &val->intval);
234 		break;
235 	case POWER_SUPPLY_PROP_HEALTH:
236 		ret = max77693_get_battery_health(regmap, &val->intval);
237 		break;
238 	case POWER_SUPPLY_PROP_PRESENT:
239 		ret = max77693_get_present(regmap, &val->intval);
240 		break;
241 	case POWER_SUPPLY_PROP_ONLINE:
242 		ret = max77693_get_online(regmap, &val->intval);
243 		break;
244 	case POWER_SUPPLY_PROP_MODEL_NAME:
245 		val->strval = max77693_charger_model;
246 		break;
247 	case POWER_SUPPLY_PROP_MANUFACTURER:
248 		val->strval = max77693_charger_manufacturer;
249 		break;
250 	default:
251 		return -EINVAL;
252 	}
253 
254 	return ret;
255 }
256 
257 static const struct power_supply_desc max77693_charger_desc = {
258 	.name		= MAX77693_CHARGER_NAME,
259 	.type		= POWER_SUPPLY_TYPE_BATTERY,
260 	.properties	= max77693_charger_props,
261 	.num_properties	= ARRAY_SIZE(max77693_charger_props),
262 	.get_property	= max77693_charger_get_property,
263 };
264 
device_attr_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count,int (* fn)(struct max77693_charger *,unsigned long))265 static ssize_t device_attr_store(struct device *dev,
266 		struct device_attribute *attr, const char *buf, size_t count,
267 		int (*fn)(struct max77693_charger *, unsigned long))
268 {
269 	struct max77693_charger *chg = dev_get_drvdata(dev);
270 	unsigned long val;
271 	int ret;
272 
273 	ret = kstrtoul(buf, 10, &val);
274 	if (ret)
275 		return ret;
276 
277 	ret = fn(chg, val);
278 	if (ret)
279 		return ret;
280 
281 	return count;
282 }
283 
fast_charge_timer_show(struct device * dev,struct device_attribute * attr,char * buf)284 static ssize_t fast_charge_timer_show(struct device *dev,
285 		struct device_attribute *attr, char *buf)
286 {
287 	struct max77693_charger *chg = dev_get_drvdata(dev);
288 	unsigned int data, val;
289 	int ret;
290 
291 	ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_01,
292 			&data);
293 	if (ret < 0)
294 		return ret;
295 
296 	data &= CHG_CNFG_01_FCHGTIME_MASK;
297 	data >>= CHG_CNFG_01_FCHGTIME_SHIFT;
298 	switch (data) {
299 	case 0x1 ... 0x7:
300 		/* Starting from 4 hours, step by 2 hours */
301 		val = 4 + (data - 1) * 2;
302 		break;
303 	case 0x0:
304 	default:
305 		val = 0;
306 		break;
307 	}
308 
309 	return scnprintf(buf, PAGE_SIZE, "%u\n", val);
310 }
311 
max77693_set_fast_charge_timer(struct max77693_charger * chg,unsigned long hours)312 static int max77693_set_fast_charge_timer(struct max77693_charger *chg,
313 		unsigned long hours)
314 {
315 	unsigned int data;
316 
317 	/*
318 	 * 0x00 - disable
319 	 * 0x01 - 4h
320 	 * 0x02 - 6h
321 	 * ...
322 	 * 0x07 - 16h
323 	 * Round down odd values.
324 	 */
325 	switch (hours) {
326 	case 4 ... 16:
327 		data = (hours - 4) / 2 + 1;
328 		break;
329 	case 0:
330 		/* Disable */
331 		data = 0;
332 		break;
333 	default:
334 		return -EINVAL;
335 	}
336 	data <<= CHG_CNFG_01_FCHGTIME_SHIFT;
337 
338 	return regmap_update_bits(chg->max77693->regmap,
339 			MAX77693_CHG_REG_CHG_CNFG_01,
340 			CHG_CNFG_01_FCHGTIME_MASK, data);
341 }
342 
fast_charge_timer_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)343 static ssize_t fast_charge_timer_store(struct device *dev,
344 		struct device_attribute *attr, const char *buf, size_t count)
345 {
346 	return device_attr_store(dev, attr, buf, count,
347 			max77693_set_fast_charge_timer);
348 }
349 
top_off_threshold_current_show(struct device * dev,struct device_attribute * attr,char * buf)350 static ssize_t top_off_threshold_current_show(struct device *dev,
351 		struct device_attribute *attr, char *buf)
352 {
353 	struct max77693_charger *chg = dev_get_drvdata(dev);
354 	unsigned int data, val;
355 	int ret;
356 
357 	ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
358 			&data);
359 	if (ret < 0)
360 		return ret;
361 
362 	data &= CHG_CNFG_03_TOITH_MASK;
363 	data >>= CHG_CNFG_03_TOITH_SHIFT;
364 
365 	if (data <= 0x04)
366 		val = 100000 + data * 25000;
367 	else
368 		val = data * 50000;
369 
370 	return scnprintf(buf, PAGE_SIZE, "%u\n", val);
371 }
372 
max77693_set_top_off_threshold_current(struct max77693_charger * chg,unsigned long uamp)373 static int max77693_set_top_off_threshold_current(struct max77693_charger *chg,
374 		unsigned long uamp)
375 {
376 	unsigned int data;
377 
378 	if (uamp < 100000 || uamp > 350000)
379 		return -EINVAL;
380 
381 	if (uamp <= 200000)
382 		data = (uamp - 100000) / 25000;
383 	else
384 		/* (200000, 350000> */
385 		data = uamp / 50000;
386 
387 	data <<= CHG_CNFG_03_TOITH_SHIFT;
388 
389 	return regmap_update_bits(chg->max77693->regmap,
390 			MAX77693_CHG_REG_CHG_CNFG_03,
391 			CHG_CNFG_03_TOITH_MASK, data);
392 }
393 
top_off_threshold_current_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)394 static ssize_t top_off_threshold_current_store(struct device *dev,
395 		struct device_attribute *attr, const char *buf, size_t count)
396 {
397 	return device_attr_store(dev, attr, buf, count,
398 			max77693_set_top_off_threshold_current);
399 }
400 
top_off_timer_show(struct device * dev,struct device_attribute * attr,char * buf)401 static ssize_t top_off_timer_show(struct device *dev,
402 		struct device_attribute *attr, char *buf)
403 {
404 	struct max77693_charger *chg = dev_get_drvdata(dev);
405 	unsigned int data, val;
406 	int ret;
407 
408 	ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
409 			&data);
410 	if (ret < 0)
411 		return ret;
412 
413 	data &= CHG_CNFG_03_TOTIME_MASK;
414 	data >>= CHG_CNFG_03_TOTIME_SHIFT;
415 
416 	val = data * 10;
417 
418 	return scnprintf(buf, PAGE_SIZE, "%u\n", val);
419 }
420 
max77693_set_top_off_timer(struct max77693_charger * chg,unsigned long minutes)421 static int max77693_set_top_off_timer(struct max77693_charger *chg,
422 		unsigned long minutes)
423 {
424 	unsigned int data;
425 
426 	if (minutes > 70)
427 		return -EINVAL;
428 
429 	data = minutes / 10;
430 	data <<= CHG_CNFG_03_TOTIME_SHIFT;
431 
432 	return regmap_update_bits(chg->max77693->regmap,
433 			MAX77693_CHG_REG_CHG_CNFG_03,
434 			CHG_CNFG_03_TOTIME_MASK, data);
435 }
436 
top_off_timer_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)437 static ssize_t top_off_timer_store(struct device *dev,
438 		struct device_attribute *attr, const char *buf, size_t count)
439 {
440 	return device_attr_store(dev, attr, buf, count,
441 			max77693_set_top_off_timer);
442 }
443 
444 static DEVICE_ATTR_RW(fast_charge_timer);
445 static DEVICE_ATTR_RW(top_off_threshold_current);
446 static DEVICE_ATTR_RW(top_off_timer);
447 
max77693_set_constant_volt(struct max77693_charger * chg,unsigned int uvolt)448 static int max77693_set_constant_volt(struct max77693_charger *chg,
449 		unsigned int uvolt)
450 {
451 	unsigned int data;
452 
453 	/*
454 	 * 0x00 - 3.650 V
455 	 * 0x01 - 3.675 V
456 	 * ...
457 	 * 0x1b - 4.325 V
458 	 * 0x1c - 4.340 V
459 	 * 0x1d - 4.350 V
460 	 * 0x1e - 4.375 V
461 	 * 0x1f - 4.400 V
462 	 */
463 	if (uvolt >= 3650000 && uvolt < 4340000)
464 		data = (uvolt - 3650000) / 25000;
465 	else if (uvolt >= 4340000 && uvolt < 4350000)
466 		data = 0x1c;
467 	else if (uvolt >= 4350000 && uvolt <= 4400000)
468 		data = 0x1d + (uvolt - 4350000) / 25000;
469 	else {
470 		dev_err(chg->dev, "Wrong value for charging constant voltage\n");
471 		return -EINVAL;
472 	}
473 
474 	data <<= CHG_CNFG_04_CHGCVPRM_SHIFT;
475 
476 	dev_dbg(chg->dev, "Charging constant voltage: %u (0x%x)\n", uvolt,
477 			data);
478 
479 	return regmap_update_bits(chg->max77693->regmap,
480 			MAX77693_CHG_REG_CHG_CNFG_04,
481 			CHG_CNFG_04_CHGCVPRM_MASK, data);
482 }
483 
max77693_set_min_system_volt(struct max77693_charger * chg,unsigned int uvolt)484 static int max77693_set_min_system_volt(struct max77693_charger *chg,
485 		unsigned int uvolt)
486 {
487 	unsigned int data;
488 
489 	if (uvolt < 3000000 || uvolt > 3700000) {
490 		dev_err(chg->dev, "Wrong value for minimum system regulation voltage\n");
491 		return -EINVAL;
492 	}
493 
494 	data = (uvolt - 3000000) / 100000;
495 
496 	data <<= CHG_CNFG_04_MINVSYS_SHIFT;
497 
498 	dev_dbg(chg->dev, "Minimum system regulation voltage: %u (0x%x)\n",
499 			uvolt, data);
500 
501 	return regmap_update_bits(chg->max77693->regmap,
502 			MAX77693_CHG_REG_CHG_CNFG_04,
503 			CHG_CNFG_04_MINVSYS_MASK, data);
504 }
505 
max77693_set_thermal_regulation_temp(struct max77693_charger * chg,unsigned int cels)506 static int max77693_set_thermal_regulation_temp(struct max77693_charger *chg,
507 		unsigned int cels)
508 {
509 	unsigned int data;
510 
511 	switch (cels) {
512 	case 70:
513 	case 85:
514 	case 100:
515 	case 115:
516 		data = (cels - 70) / 15;
517 		break;
518 	default:
519 		dev_err(chg->dev, "Wrong value for thermal regulation loop temperature\n");
520 		return -EINVAL;
521 	}
522 
523 	data <<= CHG_CNFG_07_REGTEMP_SHIFT;
524 
525 	dev_dbg(chg->dev, "Thermal regulation loop temperature: %u (0x%x)\n",
526 			cels, data);
527 
528 	return regmap_update_bits(chg->max77693->regmap,
529 			MAX77693_CHG_REG_CHG_CNFG_07,
530 			CHG_CNFG_07_REGTEMP_MASK, data);
531 }
532 
max77693_set_batttery_overcurrent(struct max77693_charger * chg,unsigned int uamp)533 static int max77693_set_batttery_overcurrent(struct max77693_charger *chg,
534 		unsigned int uamp)
535 {
536 	unsigned int data;
537 
538 	if (uamp && (uamp < 2000000 || uamp > 3500000)) {
539 		dev_err(chg->dev, "Wrong value for battery overcurrent\n");
540 		return -EINVAL;
541 	}
542 
543 	if (uamp)
544 		data = ((uamp - 2000000) / 250000) + 1;
545 	else
546 		data = 0; /* disable */
547 
548 	data <<= CHG_CNFG_12_B2SOVRC_SHIFT;
549 
550 	dev_dbg(chg->dev, "Battery overcurrent: %u (0x%x)\n", uamp, data);
551 
552 	return regmap_update_bits(chg->max77693->regmap,
553 			MAX77693_CHG_REG_CHG_CNFG_12,
554 			CHG_CNFG_12_B2SOVRC_MASK, data);
555 }
556 
max77693_set_charge_input_threshold_volt(struct max77693_charger * chg,unsigned int uvolt)557 static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg,
558 		unsigned int uvolt)
559 {
560 	unsigned int data;
561 
562 	switch (uvolt) {
563 	case 4300000:
564 		data = 0x0;
565 		break;
566 	case 4700000:
567 	case 4800000:
568 	case 4900000:
569 		data = (uvolt - 4700000) / 100000;
570 		break;
571 	default:
572 		dev_err(chg->dev, "Wrong value for charge input voltage regulation threshold\n");
573 		return -EINVAL;
574 	}
575 
576 	data <<= CHG_CNFG_12_VCHGINREG_SHIFT;
577 
578 	dev_dbg(chg->dev, "Charge input voltage regulation threshold: %u (0x%x)\n",
579 			uvolt, data);
580 
581 	return regmap_update_bits(chg->max77693->regmap,
582 			MAX77693_CHG_REG_CHG_CNFG_12,
583 			CHG_CNFG_12_VCHGINREG_MASK, data);
584 }
585 
586 /*
587  * Sets charger registers to proper and safe default values.
588  */
max77693_reg_init(struct max77693_charger * chg)589 static int max77693_reg_init(struct max77693_charger *chg)
590 {
591 	int ret;
592 	unsigned int data;
593 
594 	/* Unlock charger register protection */
595 	data = (0x3 << CHG_CNFG_06_CHGPROT_SHIFT);
596 	ret = regmap_update_bits(chg->max77693->regmap,
597 				MAX77693_CHG_REG_CHG_CNFG_06,
598 				CHG_CNFG_06_CHGPROT_MASK, data);
599 	if (ret) {
600 		dev_err(chg->dev, "Error unlocking registers: %d\n", ret);
601 		return ret;
602 	}
603 
604 	ret = max77693_set_fast_charge_timer(chg, DEFAULT_FAST_CHARGE_TIMER);
605 	if (ret)
606 		return ret;
607 
608 	ret = max77693_set_top_off_threshold_current(chg,
609 			DEFAULT_TOP_OFF_THRESHOLD_CURRENT);
610 	if (ret)
611 		return ret;
612 
613 	ret = max77693_set_top_off_timer(chg, DEFAULT_TOP_OFF_TIMER);
614 	if (ret)
615 		return ret;
616 
617 	ret = max77693_set_constant_volt(chg, chg->constant_volt);
618 	if (ret)
619 		return ret;
620 
621 	ret = max77693_set_min_system_volt(chg, chg->min_system_volt);
622 	if (ret)
623 		return ret;
624 
625 	ret = max77693_set_thermal_regulation_temp(chg,
626 			chg->thermal_regulation_temp);
627 	if (ret)
628 		return ret;
629 
630 	ret = max77693_set_batttery_overcurrent(chg, chg->batttery_overcurrent);
631 	if (ret)
632 		return ret;
633 
634 	return max77693_set_charge_input_threshold_volt(chg,
635 			chg->charge_input_threshold_volt);
636 }
637 
638 #ifdef CONFIG_OF
max77693_dt_init(struct device * dev,struct max77693_charger * chg)639 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
640 {
641 	struct device_node *np = dev->of_node;
642 
643 	if (!np) {
644 		dev_err(dev, "no charger OF node\n");
645 		return -EINVAL;
646 	}
647 
648 	if (of_property_read_u32(np, "maxim,constant-microvolt",
649 			&chg->constant_volt))
650 		chg->constant_volt = DEFAULT_CONSTANT_VOLT;
651 
652 	if (of_property_read_u32(np, "maxim,min-system-microvolt",
653 			&chg->min_system_volt))
654 		chg->min_system_volt = DEFAULT_MIN_SYSTEM_VOLT;
655 
656 	if (of_property_read_u32(np, "maxim,thermal-regulation-celsius",
657 			&chg->thermal_regulation_temp))
658 		chg->thermal_regulation_temp = DEFAULT_THERMAL_REGULATION_TEMP;
659 
660 	if (of_property_read_u32(np, "maxim,battery-overcurrent-microamp",
661 			&chg->batttery_overcurrent))
662 		chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT;
663 
664 	if (of_property_read_u32(np, "maxim,charge-input-threshold-microvolt",
665 			&chg->charge_input_threshold_volt))
666 		chg->charge_input_threshold_volt =
667 			DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT;
668 
669 	return 0;
670 }
671 #else /* CONFIG_OF */
max77693_dt_init(struct device * dev,struct max77693_charger * chg)672 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
673 {
674 	return 0;
675 }
676 #endif /* CONFIG_OF */
677 
max77693_charger_probe(struct platform_device * pdev)678 static int max77693_charger_probe(struct platform_device *pdev)
679 {
680 	struct max77693_charger *chg;
681 	struct power_supply_config psy_cfg = {};
682 	struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
683 	int ret;
684 
685 	chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
686 	if (!chg)
687 		return -ENOMEM;
688 
689 	platform_set_drvdata(pdev, chg);
690 	chg->dev = &pdev->dev;
691 	chg->max77693 = max77693;
692 
693 	ret = max77693_dt_init(&pdev->dev, chg);
694 	if (ret)
695 		return ret;
696 
697 	ret = max77693_reg_init(chg);
698 	if (ret)
699 		return ret;
700 
701 	psy_cfg.drv_data = chg;
702 
703 	ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
704 	if (ret) {
705 		dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n");
706 		goto err;
707 	}
708 
709 	ret = device_create_file(&pdev->dev,
710 			&dev_attr_top_off_threshold_current);
711 	if (ret) {
712 		dev_err(&pdev->dev, "failed: create top off current sysfs entry\n");
713 		goto err;
714 	}
715 
716 	ret = device_create_file(&pdev->dev, &dev_attr_top_off_timer);
717 	if (ret) {
718 		dev_err(&pdev->dev, "failed: create top off timer sysfs entry\n");
719 		goto err;
720 	}
721 
722 	chg->charger = power_supply_register(&pdev->dev,
723 						&max77693_charger_desc,
724 						&psy_cfg);
725 	if (IS_ERR(chg->charger)) {
726 		dev_err(&pdev->dev, "failed: power supply register\n");
727 		ret = PTR_ERR(chg->charger);
728 		goto err;
729 	}
730 
731 	return 0;
732 
733 err:
734 	device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
735 	device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
736 	device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
737 
738 	return ret;
739 }
740 
max77693_charger_remove(struct platform_device * pdev)741 static int max77693_charger_remove(struct platform_device *pdev)
742 {
743 	struct max77693_charger *chg = platform_get_drvdata(pdev);
744 
745 	device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
746 	device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
747 	device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
748 
749 	power_supply_unregister(chg->charger);
750 
751 	return 0;
752 }
753 
754 static const struct platform_device_id max77693_charger_id[] = {
755 	{ "max77693-charger", 0, },
756 	{ }
757 };
758 MODULE_DEVICE_TABLE(platform, max77693_charger_id);
759 
760 static struct platform_driver max77693_charger_driver = {
761 	.driver = {
762 		.name	= "max77693-charger",
763 	},
764 	.probe		= max77693_charger_probe,
765 	.remove		= max77693_charger_remove,
766 	.id_table	= max77693_charger_id,
767 };
768 module_platform_driver(max77693_charger_driver);
769 
770 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
771 MODULE_DESCRIPTION("Maxim 77693 charger driver");
772 MODULE_LICENSE("GPL");
773