power_supply: charger-manager: Use power_supply_*() API for accessing function attrs
[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
25static const char *max77693_charger_name = "max77693-charger";
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;
32 struct power_supply charger;
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{
223 struct max77693_charger *chg = container_of(psy,
224 struct max77693_charger,
225 charger);
226 struct regmap *regmap = chg->max77693->regmap;
227 int ret = 0;
228
229 switch (psp) {
230 case POWER_SUPPLY_PROP_STATUS:
1ed522b3 231 ret = max77693_get_charger_state(regmap, &val->intval);
87c2d906
KK
232 break;
233 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1ed522b3 234 ret = max77693_get_charge_type(regmap, &val->intval);
87c2d906
KK
235 break;
236 case POWER_SUPPLY_PROP_HEALTH:
1ed522b3 237 ret = max77693_get_battery_health(regmap, &val->intval);
87c2d906
KK
238 break;
239 case POWER_SUPPLY_PROP_PRESENT:
1ed522b3 240 ret = max77693_get_present(regmap, &val->intval);
87c2d906
KK
241 break;
242 case POWER_SUPPLY_PROP_ONLINE:
1ed522b3 243 ret = max77693_get_online(regmap, &val->intval);
87c2d906
KK
244 break;
245 case POWER_SUPPLY_PROP_MODEL_NAME:
246 val->strval = max77693_charger_model;
247 break;
248 case POWER_SUPPLY_PROP_MANUFACTURER:
249 val->strval = max77693_charger_manufacturer;
250 break;
251 default:
252 return -EINVAL;
253 }
254
255 return ret;
256}
257
258static ssize_t device_attr_store(struct device *dev,
259 struct device_attribute *attr, const char *buf, size_t count,
260 int (*fn)(struct max77693_charger *, unsigned long))
261{
262 struct max77693_charger *chg = dev_get_drvdata(dev);
263 unsigned long val;
264 int ret;
265
266 ret = kstrtoul(buf, 10, &val);
267 if (ret)
268 return ret;
269
270 ret = fn(chg, val);
271 if (ret)
272 return ret;
273
274 return count;
275}
276
277static ssize_t fast_charge_timer_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
279{
280 struct max77693_charger *chg = dev_get_drvdata(dev);
281 unsigned int data, val;
282 int ret;
283
284 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_01,
285 &data);
286 if (ret < 0)
287 return ret;
288
289 data &= CHG_CNFG_01_FCHGTIME_MASK;
290 data >>= CHG_CNFG_01_FCHGTIME_SHIFT;
291 switch (data) {
292 case 0x1 ... 0x7:
293 /* Starting from 4 hours, step by 2 hours */
294 val = 4 + (data - 1) * 2;
295 break;
296 case 0x0:
297 default:
298 val = 0;
299 break;
300 }
301
302 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
303}
304
305static int max77693_set_fast_charge_timer(struct max77693_charger *chg,
306 unsigned long hours)
307{
308 unsigned int data;
309
310 /*
311 * 0x00 - disable
312 * 0x01 - 4h
313 * 0x02 - 6h
314 * ...
315 * 0x07 - 16h
316 * Round down odd values.
317 */
318 switch (hours) {
319 case 4 ... 16:
320 data = (hours - 4) / 2 + 1;
321 break;
322 case 0:
323 /* Disable */
324 data = 0;
325 break;
326 default:
327 return -EINVAL;
328 }
329 data <<= CHG_CNFG_01_FCHGTIME_SHIFT;
330
331 return regmap_update_bits(chg->max77693->regmap,
332 MAX77693_CHG_REG_CHG_CNFG_01,
333 CHG_CNFG_01_FCHGTIME_MASK, data);
334}
335
336static ssize_t fast_charge_timer_store(struct device *dev,
337 struct device_attribute *attr, const char *buf, size_t count)
338{
339 return device_attr_store(dev, attr, buf, count,
340 max77693_set_fast_charge_timer);
341}
342
343static ssize_t top_off_threshold_current_show(struct device *dev,
344 struct device_attribute *attr, char *buf)
345{
346 struct max77693_charger *chg = dev_get_drvdata(dev);
347 unsigned int data, val;
348 int ret;
349
350 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
351 &data);
352 if (ret < 0)
353 return ret;
354
355 data &= CHG_CNFG_03_TOITH_MASK;
356 data >>= CHG_CNFG_03_TOITH_SHIFT;
357
358 if (data <= 0x04)
359 val = 100000 + data * 25000;
360 else
361 val = data * 50000;
362
363 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
364}
365
366static int max77693_set_top_off_threshold_current(struct max77693_charger *chg,
367 unsigned long uamp)
368{
369 unsigned int data;
370
371 if (uamp < 100000 || uamp > 350000)
372 return -EINVAL;
373
374 if (uamp <= 200000)
375 data = (uamp - 100000) / 25000;
376 else
377 /* (200000, 350000> */
378 data = uamp / 50000;
379
380 data <<= CHG_CNFG_03_TOITH_SHIFT;
381
382 return regmap_update_bits(chg->max77693->regmap,
383 MAX77693_CHG_REG_CHG_CNFG_03,
384 CHG_CNFG_03_TOITH_MASK, data);
385}
386
387static ssize_t top_off_threshold_current_store(struct device *dev,
388 struct device_attribute *attr, const char *buf, size_t count)
389{
390 return device_attr_store(dev, attr, buf, count,
391 max77693_set_top_off_threshold_current);
392}
393
394static ssize_t top_off_timer_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
396{
397 struct max77693_charger *chg = dev_get_drvdata(dev);
398 unsigned int data, val;
399 int ret;
400
401 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
402 &data);
403 if (ret < 0)
404 return ret;
405
406 data &= CHG_CNFG_03_TOTIME_MASK;
407 data >>= CHG_CNFG_03_TOTIME_SHIFT;
408
409 val = data * 10;
410
411 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
412}
413
414static int max77693_set_top_off_timer(struct max77693_charger *chg,
415 unsigned long minutes)
416{
417 unsigned int data;
418
419 if (minutes > 70)
420 return -EINVAL;
421
422 data = minutes / 10;
423 data <<= CHG_CNFG_03_TOTIME_SHIFT;
424
425 return regmap_update_bits(chg->max77693->regmap,
426 MAX77693_CHG_REG_CHG_CNFG_03,
427 CHG_CNFG_03_TOTIME_MASK, data);
428}
429
430static ssize_t top_off_timer_store(struct device *dev,
431 struct device_attribute *attr, const char *buf, size_t count)
432{
433 return device_attr_store(dev, attr, buf, count,
434 max77693_set_top_off_timer);
435}
436
437static DEVICE_ATTR_RW(fast_charge_timer);
438static DEVICE_ATTR_RW(top_off_threshold_current);
439static DEVICE_ATTR_RW(top_off_timer);
440
441static int max77693_set_constant_volt(struct max77693_charger *chg,
442 unsigned int uvolt)
443{
444 unsigned int data;
445
446 /*
447 * 0x00 - 3.650 V
448 * 0x01 - 3.675 V
449 * ...
450 * 0x1b - 4.325 V
451 * 0x1c - 4.340 V
452 * 0x1d - 4.350 V
453 * 0x1e - 4.375 V
454 * 0x1f - 4.400 V
455 */
456 if (uvolt >= 3650000 && uvolt < 4340000)
457 data = (uvolt - 3650000) / 25000;
458 else if (uvolt >= 4340000 && uvolt < 4350000)
459 data = 0x1c;
460 else if (uvolt >= 4350000 && uvolt <= 4400000)
461 data = 0x1d + (uvolt - 4350000) / 25000;
462 else {
463 dev_err(chg->dev, "Wrong value for charging constant voltage\n");
464 return -EINVAL;
465 }
466
467 data <<= CHG_CNFG_04_CHGCVPRM_SHIFT;
468
469 dev_dbg(chg->dev, "Charging constant voltage: %u (0x%x)\n", uvolt,
470 data);
471
472 return regmap_update_bits(chg->max77693->regmap,
473 MAX77693_CHG_REG_CHG_CNFG_04,
474 CHG_CNFG_04_CHGCVPRM_MASK, data);
475}
476
477static int max77693_set_min_system_volt(struct max77693_charger *chg,
478 unsigned int uvolt)
479{
480 unsigned int data;
481
482 if (uvolt < 3000000 || uvolt > 3700000) {
483 dev_err(chg->dev, "Wrong value for minimum system regulation voltage\n");
484 return -EINVAL;
485 }
486
487 data = (uvolt - 3000000) / 100000;
488
489 data <<= CHG_CNFG_04_MINVSYS_SHIFT;
490
491 dev_dbg(chg->dev, "Minimum system regulation voltage: %u (0x%x)\n",
492 uvolt, data);
493
494 return regmap_update_bits(chg->max77693->regmap,
495 MAX77693_CHG_REG_CHG_CNFG_04,
496 CHG_CNFG_04_MINVSYS_MASK, data);
497}
498
499static int max77693_set_thermal_regulation_temp(struct max77693_charger *chg,
500 unsigned int cels)
501{
502 unsigned int data;
503
504 switch (cels) {
505 case 70:
506 case 85:
507 case 100:
508 case 115:
509 data = (cels - 70) / 15;
510 break;
511 default:
512 dev_err(chg->dev, "Wrong value for thermal regulation loop temperature\n");
513 return -EINVAL;
514 }
515
516 data <<= CHG_CNFG_07_REGTEMP_SHIFT;
517
518 dev_dbg(chg->dev, "Thermal regulation loop temperature: %u (0x%x)\n",
519 cels, data);
520
521 return regmap_update_bits(chg->max77693->regmap,
522 MAX77693_CHG_REG_CHG_CNFG_07,
523 CHG_CNFG_07_REGTEMP_MASK, data);
524}
525
526static int max77693_set_batttery_overcurrent(struct max77693_charger *chg,
527 unsigned int uamp)
528{
529 unsigned int data;
530
531 if (uamp && (uamp < 2000000 || uamp > 3500000)) {
532 dev_err(chg->dev, "Wrong value for battery overcurrent\n");
533 return -EINVAL;
534 }
535
536 if (uamp)
537 data = ((uamp - 2000000) / 250000) + 1;
538 else
539 data = 0; /* disable */
540
541 data <<= CHG_CNFG_12_B2SOVRC_SHIFT;
542
543 dev_dbg(chg->dev, "Battery overcurrent: %u (0x%x)\n", uamp, data);
544
545 return regmap_update_bits(chg->max77693->regmap,
546 MAX77693_CHG_REG_CHG_CNFG_12,
547 CHG_CNFG_12_B2SOVRC_MASK, data);
548}
549
550static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg,
551 unsigned int uvolt)
552{
553 unsigned int data;
554
555 switch (uvolt) {
556 case 4300000:
557 data = 0x0;
558 break;
559 case 4700000:
560 case 4800000:
561 case 4900000:
562 data = (uvolt - 4700000) / 100000;
563 default:
564 dev_err(chg->dev, "Wrong value for charge input voltage regulation threshold\n");
565 return -EINVAL;
566 }
567
568 data <<= CHG_CNFG_12_VCHGINREG_SHIFT;
569
570 dev_dbg(chg->dev, "Charge input voltage regulation threshold: %u (0x%x)\n",
571 uvolt, data);
572
573 return regmap_update_bits(chg->max77693->regmap,
574 MAX77693_CHG_REG_CHG_CNFG_12,
575 CHG_CNFG_12_VCHGINREG_MASK, data);
576}
577
578/*
579 * Sets charger registers to proper and safe default values.
580 */
581static int max77693_reg_init(struct max77693_charger *chg)
582{
583 int ret;
584 unsigned int data;
585
586 /* Unlock charger register protection */
587 data = (0x3 << CHG_CNFG_06_CHGPROT_SHIFT);
588 ret = regmap_update_bits(chg->max77693->regmap,
589 MAX77693_CHG_REG_CHG_CNFG_06,
590 CHG_CNFG_06_CHGPROT_MASK, data);
591 if (ret) {
592 dev_err(chg->dev, "Error unlocking registers: %d\n", ret);
593 return ret;
594 }
595
596 ret = max77693_set_fast_charge_timer(chg, DEFAULT_FAST_CHARGE_TIMER);
597 if (ret)
598 return ret;
599
600 ret = max77693_set_top_off_threshold_current(chg,
601 DEFAULT_TOP_OFF_THRESHOLD_CURRENT);
602 if (ret)
603 return ret;
604
605 ret = max77693_set_top_off_timer(chg, DEFAULT_TOP_OFF_TIMER);
606 if (ret)
607 return ret;
608
609 ret = max77693_set_constant_volt(chg, chg->constant_volt);
610 if (ret)
611 return ret;
612
613 ret = max77693_set_min_system_volt(chg, chg->min_system_volt);
614 if (ret)
615 return ret;
616
617 ret = max77693_set_thermal_regulation_temp(chg,
618 chg->thermal_regulation_temp);
619 if (ret)
620 return ret;
621
622 ret = max77693_set_batttery_overcurrent(chg, chg->batttery_overcurrent);
623 if (ret)
624 return ret;
625
5b40b3e6 626 return max77693_set_charge_input_threshold_volt(chg,
87c2d906 627 chg->charge_input_threshold_volt);
87c2d906
KK
628}
629
630#ifdef CONFIG_OF
631static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
632{
633 struct device_node *np = dev->of_node;
634
635 if (!np) {
636 dev_err(dev, "no charger OF node\n");
637 return -EINVAL;
638 }
639
640 if (of_property_read_u32(np, "maxim,constant-microvolt",
641 &chg->constant_volt))
642 chg->constant_volt = DEFAULT_CONSTANT_VOLT;
643
644 if (of_property_read_u32(np, "maxim,min-system-microvolt",
645 &chg->min_system_volt))
646 chg->min_system_volt = DEFAULT_MIN_SYSTEM_VOLT;
647
648 if (of_property_read_u32(np, "maxim,thermal-regulation-celsius",
649 &chg->thermal_regulation_temp))
650 chg->thermal_regulation_temp = DEFAULT_THERMAL_REGULATION_TEMP;
651
652 if (of_property_read_u32(np, "maxim,battery-overcurrent-microamp",
653 &chg->batttery_overcurrent))
654 chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT;
655
656 if (of_property_read_u32(np, "maxim,charge-input-threshold-microvolt",
657 &chg->charge_input_threshold_volt))
658 chg->charge_input_threshold_volt =
659 DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT;
660
661 return 0;
662}
663#else /* CONFIG_OF */
664static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
665{
666 return 0;
667}
668#endif /* CONFIG_OF */
669
670static int max77693_charger_probe(struct platform_device *pdev)
671{
672 struct max77693_charger *chg;
673 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
674 int ret;
675
676 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
677 if (!chg)
678 return -ENOMEM;
679
680 platform_set_drvdata(pdev, chg);
681 chg->dev = &pdev->dev;
682 chg->max77693 = max77693;
683
684 ret = max77693_dt_init(&pdev->dev, chg);
685 if (ret)
686 return ret;
687
688 ret = max77693_reg_init(chg);
689 if (ret)
690 return ret;
691
692 chg->charger.name = max77693_charger_name;
693 chg->charger.type = POWER_SUPPLY_TYPE_BATTERY;
694 chg->charger.properties = max77693_charger_props;
695 chg->charger.num_properties = ARRAY_SIZE(max77693_charger_props);
696 chg->charger.get_property = max77693_charger_get_property;
697
698 ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
699 if (ret) {
700 dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n");
701 goto err;
702 }
703
704 ret = device_create_file(&pdev->dev,
705 &dev_attr_top_off_threshold_current);
706 if (ret) {
707 dev_err(&pdev->dev, "failed: create top off current sysfs entry\n");
708 goto err;
709 }
710
711 ret = device_create_file(&pdev->dev, &dev_attr_top_off_timer);
712 if (ret) {
713 dev_err(&pdev->dev, "failed: create top off timer sysfs entry\n");
714 goto err;
715 }
716
2dc9215d 717 ret = power_supply_register(&pdev->dev, &chg->charger, NULL);
87c2d906
KK
718 if (ret) {
719 dev_err(&pdev->dev, "failed: power supply register\n");
720 goto err;
721 }
722
723 return 0;
724
725err:
726 device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
727 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
728 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
729
730 return ret;
731}
732
733static int max77693_charger_remove(struct platform_device *pdev)
734{
735 struct max77693_charger *chg = platform_get_drvdata(pdev);
736
737 device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
738 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
739 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
740
741 power_supply_unregister(&chg->charger);
742
743 return 0;
744}
745
746static const struct platform_device_id max77693_charger_id[] = {
747 { "max77693-charger", 0, },
748 { }
749};
750MODULE_DEVICE_TABLE(platform, max77693_charger_id);
751
752static struct platform_driver max77693_charger_driver = {
753 .driver = {
87c2d906
KK
754 .name = "max77693-charger",
755 },
756 .probe = max77693_charger_probe,
757 .remove = max77693_charger_remove,
758 .id_table = max77693_charger_id,
759};
760module_platform_driver(max77693_charger_driver);
761
762MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
763MODULE_DESCRIPTION("Maxim 77693 charger driver");
764MODULE_LICENSE("GPL");