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