Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / power / supply / max77693_charger.c
blobcdea35c0d1de1111311d198d042bfa5b10da52b7
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // max77693_charger.c - Battery charger driver for the Maxim 77693
4 //
5 // Copyright (C) 2014 Samsung Electronics
6 // Krzysztof Kozlowski <krzk@kernel.org>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/power_supply.h>
11 #include <linux/regmap.h>
12 #include <linux/mfd/max77693.h>
13 #include <linux/mfd/max77693-common.h>
14 #include <linux/mfd/max77693-private.h>
16 #define MAX77693_CHARGER_NAME "max77693-charger"
17 static const char *max77693_charger_model = "MAX77693";
18 static const char *max77693_charger_manufacturer = "Maxim Integrated";
20 struct max77693_charger {
21 struct device *dev;
22 struct max77693_dev *max77693;
23 struct power_supply *charger;
25 u32 constant_volt;
26 u32 min_system_volt;
27 u32 thermal_regulation_temp;
28 u32 batttery_overcurrent;
29 u32 charge_input_threshold_volt;
32 static int max77693_get_charger_state(struct regmap *regmap, int *val)
34 int ret;
35 unsigned int data;
37 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
38 if (ret < 0)
39 return ret;
41 data &= CHG_DETAILS_01_CHG_MASK;
42 data >>= CHG_DETAILS_01_CHG_SHIFT;
44 switch (data) {
45 case MAX77693_CHARGING_PREQUALIFICATION:
46 case MAX77693_CHARGING_FAST_CONST_CURRENT:
47 case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
48 case MAX77693_CHARGING_TOP_OFF:
49 /* In high temp the charging current is reduced, but still charging */
50 case MAX77693_CHARGING_HIGH_TEMP:
51 *val = POWER_SUPPLY_STATUS_CHARGING;
52 break;
53 case MAX77693_CHARGING_DONE:
54 *val = POWER_SUPPLY_STATUS_FULL;
55 break;
56 case MAX77693_CHARGING_TIMER_EXPIRED:
57 case MAX77693_CHARGING_THERMISTOR_SUSPEND:
58 *val = POWER_SUPPLY_STATUS_NOT_CHARGING;
59 break;
60 case MAX77693_CHARGING_OFF:
61 case MAX77693_CHARGING_OVER_TEMP:
62 case MAX77693_CHARGING_WATCHDOG_EXPIRED:
63 *val = POWER_SUPPLY_STATUS_DISCHARGING;
64 break;
65 case MAX77693_CHARGING_RESERVED:
66 default:
67 *val = POWER_SUPPLY_STATUS_UNKNOWN;
70 return 0;
73 static int max77693_get_charge_type(struct regmap *regmap, int *val)
75 int ret;
76 unsigned int data;
78 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
79 if (ret < 0)
80 return ret;
82 data &= CHG_DETAILS_01_CHG_MASK;
83 data >>= CHG_DETAILS_01_CHG_SHIFT;
85 switch (data) {
86 case MAX77693_CHARGING_PREQUALIFICATION:
88 * Top-off: trickle or fast? In top-off the current varies between
89 * 100 and 250 mA. It is higher than prequalification current.
91 case MAX77693_CHARGING_TOP_OFF:
92 *val = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
93 break;
94 case MAX77693_CHARGING_FAST_CONST_CURRENT:
95 case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
96 /* In high temp the charging current is reduced, but still charging */
97 case MAX77693_CHARGING_HIGH_TEMP:
98 *val = POWER_SUPPLY_CHARGE_TYPE_FAST;
99 break;
100 case MAX77693_CHARGING_DONE:
101 case MAX77693_CHARGING_TIMER_EXPIRED:
102 case MAX77693_CHARGING_THERMISTOR_SUSPEND:
103 case MAX77693_CHARGING_OFF:
104 case MAX77693_CHARGING_OVER_TEMP:
105 case MAX77693_CHARGING_WATCHDOG_EXPIRED:
106 *val = POWER_SUPPLY_CHARGE_TYPE_NONE;
107 break;
108 case MAX77693_CHARGING_RESERVED:
109 default:
110 *val = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
113 return 0;
117 * Supported health statuses:
118 * - POWER_SUPPLY_HEALTH_DEAD
119 * - POWER_SUPPLY_HEALTH_GOOD
120 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
121 * - POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
122 * - POWER_SUPPLY_HEALTH_UNKNOWN
123 * - POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
125 static int max77693_get_battery_health(struct regmap *regmap, int *val)
127 int ret;
128 unsigned int data;
130 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
131 if (ret < 0)
132 return ret;
134 data &= CHG_DETAILS_01_BAT_MASK;
135 data >>= CHG_DETAILS_01_BAT_SHIFT;
137 switch (data) {
138 case MAX77693_BATTERY_NOBAT:
139 *val = POWER_SUPPLY_HEALTH_DEAD;
140 break;
141 case MAX77693_BATTERY_PREQUALIFICATION:
142 case MAX77693_BATTERY_GOOD:
143 case MAX77693_BATTERY_LOWVOLTAGE:
144 *val = POWER_SUPPLY_HEALTH_GOOD;
145 break;
146 case MAX77693_BATTERY_TIMER_EXPIRED:
148 * Took longer to charge than expected, charging suspended.
149 * Damaged battery?
151 *val = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
152 break;
153 case MAX77693_BATTERY_OVERVOLTAGE:
154 *val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
155 break;
156 case MAX77693_BATTERY_OVERCURRENT:
157 *val = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
158 break;
159 case MAX77693_BATTERY_RESERVED:
160 default:
161 *val = POWER_SUPPLY_HEALTH_UNKNOWN;
162 break;
165 return 0;
168 static int max77693_get_present(struct regmap *regmap, int *val)
170 unsigned int data;
171 int ret;
174 * Read CHG_INT_OK register. High DETBAT bit here should be
175 * equal to value 0x0 in CHG_DETAILS_01/BAT field.
177 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
178 if (ret < 0)
179 return ret;
181 *val = (data & CHG_INT_OK_DETBAT_MASK) ? 0 : 1;
183 return 0;
186 static int max77693_get_online(struct regmap *regmap, int *val)
188 unsigned int data;
189 int ret;
191 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
192 if (ret < 0)
193 return ret;
195 *val = (data & CHG_INT_OK_CHGIN_MASK) ? 1 : 0;
197 return 0;
201 * There are *two* current limit registers:
202 * - CHGIN limit, which limits the input current from the external charger;
203 * - Fast charge current limit, which limits the current going to the battery.
206 static int max77693_get_input_current_limit(struct regmap *regmap, int *val)
208 unsigned int data;
209 int ret;
211 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_CNFG_09, &data);
212 if (ret < 0)
213 return ret;
215 data &= CHG_CNFG_09_CHGIN_ILIM_MASK;
216 data >>= CHG_CNFG_09_CHGIN_ILIM_SHIFT;
218 if (data <= 0x03)
219 /* The first four values (0x00..0x03) are 60mA */
220 *val = 60000;
221 else
222 *val = data * 20000; /* 20mA steps */
224 return 0;
227 static int max77693_get_fast_charge_current(struct regmap *regmap, int *val)
229 unsigned int data;
230 int ret;
232 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_CNFG_02, &data);
233 if (ret < 0)
234 return ret;
236 data &= CHG_CNFG_02_CC_MASK;
237 data >>= CHG_CNFG_02_CC_SHIFT;
239 *val = data * 33300; /* 33.3mA steps */
241 return 0;
244 static enum power_supply_property max77693_charger_props[] = {
245 POWER_SUPPLY_PROP_STATUS,
246 POWER_SUPPLY_PROP_CHARGE_TYPE,
247 POWER_SUPPLY_PROP_HEALTH,
248 POWER_SUPPLY_PROP_PRESENT,
249 POWER_SUPPLY_PROP_ONLINE,
250 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
251 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
252 POWER_SUPPLY_PROP_MODEL_NAME,
253 POWER_SUPPLY_PROP_MANUFACTURER,
256 static int max77693_charger_get_property(struct power_supply *psy,
257 enum power_supply_property psp,
258 union power_supply_propval *val)
260 struct max77693_charger *chg = power_supply_get_drvdata(psy);
261 struct regmap *regmap = chg->max77693->regmap;
262 int ret = 0;
264 switch (psp) {
265 case POWER_SUPPLY_PROP_STATUS:
266 ret = max77693_get_charger_state(regmap, &val->intval);
267 break;
268 case POWER_SUPPLY_PROP_CHARGE_TYPE:
269 ret = max77693_get_charge_type(regmap, &val->intval);
270 break;
271 case POWER_SUPPLY_PROP_HEALTH:
272 ret = max77693_get_battery_health(regmap, &val->intval);
273 break;
274 case POWER_SUPPLY_PROP_PRESENT:
275 ret = max77693_get_present(regmap, &val->intval);
276 break;
277 case POWER_SUPPLY_PROP_ONLINE:
278 ret = max77693_get_online(regmap, &val->intval);
279 break;
280 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
281 ret = max77693_get_input_current_limit(regmap, &val->intval);
282 break;
283 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
284 ret = max77693_get_fast_charge_current(regmap, &val->intval);
285 break;
286 case POWER_SUPPLY_PROP_MODEL_NAME:
287 val->strval = max77693_charger_model;
288 break;
289 case POWER_SUPPLY_PROP_MANUFACTURER:
290 val->strval = max77693_charger_manufacturer;
291 break;
292 default:
293 return -EINVAL;
296 return ret;
299 static const struct power_supply_desc max77693_charger_desc = {
300 .name = MAX77693_CHARGER_NAME,
301 .type = POWER_SUPPLY_TYPE_BATTERY,
302 .properties = max77693_charger_props,
303 .num_properties = ARRAY_SIZE(max77693_charger_props),
304 .get_property = max77693_charger_get_property,
307 static ssize_t device_attr_store(struct device *dev,
308 struct device_attribute *attr, const char *buf, size_t count,
309 int (*fn)(struct max77693_charger *, unsigned long))
311 struct max77693_charger *chg = dev_get_drvdata(dev);
312 unsigned long val;
313 int ret;
315 ret = kstrtoul(buf, 10, &val);
316 if (ret)
317 return ret;
319 ret = fn(chg, val);
320 if (ret)
321 return ret;
323 return count;
326 static ssize_t fast_charge_timer_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
329 struct max77693_charger *chg = dev_get_drvdata(dev);
330 unsigned int data, val;
331 int ret;
333 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_01,
334 &data);
335 if (ret < 0)
336 return ret;
338 data &= CHG_CNFG_01_FCHGTIME_MASK;
339 data >>= CHG_CNFG_01_FCHGTIME_SHIFT;
340 switch (data) {
341 case 0x1 ... 0x7:
342 /* Starting from 4 hours, step by 2 hours */
343 val = 4 + (data - 1) * 2;
344 break;
345 case 0x0:
346 default:
347 val = 0;
348 break;
351 return sysfs_emit(buf, "%u\n", val);
354 static int max77693_set_fast_charge_timer(struct max77693_charger *chg,
355 unsigned long hours)
357 unsigned int data;
360 * 0x00 - disable
361 * 0x01 - 4h
362 * 0x02 - 6h
363 * ...
364 * 0x07 - 16h
365 * Round down odd values.
367 switch (hours) {
368 case 4 ... 16:
369 data = (hours - 4) / 2 + 1;
370 break;
371 case 0:
372 /* Disable */
373 data = 0;
374 break;
375 default:
376 return -EINVAL;
378 data <<= CHG_CNFG_01_FCHGTIME_SHIFT;
380 return regmap_update_bits(chg->max77693->regmap,
381 MAX77693_CHG_REG_CHG_CNFG_01,
382 CHG_CNFG_01_FCHGTIME_MASK, data);
385 static ssize_t fast_charge_timer_store(struct device *dev,
386 struct device_attribute *attr, const char *buf, size_t count)
388 return device_attr_store(dev, attr, buf, count,
389 max77693_set_fast_charge_timer);
392 static ssize_t top_off_threshold_current_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
395 struct max77693_charger *chg = dev_get_drvdata(dev);
396 unsigned int data, val;
397 int ret;
399 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
400 &data);
401 if (ret < 0)
402 return ret;
404 data &= CHG_CNFG_03_TOITH_MASK;
405 data >>= CHG_CNFG_03_TOITH_SHIFT;
407 if (data <= 0x04)
408 val = 100000 + data * 25000;
409 else
410 val = data * 50000;
412 return sysfs_emit(buf, "%u\n", val);
415 static int max77693_set_top_off_threshold_current(struct max77693_charger *chg,
416 unsigned long uamp)
418 unsigned int data;
420 if (uamp < 100000 || uamp > 350000)
421 return -EINVAL;
423 if (uamp <= 200000)
424 data = (uamp - 100000) / 25000;
425 else
426 /* (200000, 350000> */
427 data = uamp / 50000;
429 data <<= CHG_CNFG_03_TOITH_SHIFT;
431 return regmap_update_bits(chg->max77693->regmap,
432 MAX77693_CHG_REG_CHG_CNFG_03,
433 CHG_CNFG_03_TOITH_MASK, data);
436 static ssize_t top_off_threshold_current_store(struct device *dev,
437 struct device_attribute *attr, const char *buf, size_t count)
439 return device_attr_store(dev, attr, buf, count,
440 max77693_set_top_off_threshold_current);
443 static ssize_t top_off_timer_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
446 struct max77693_charger *chg = dev_get_drvdata(dev);
447 unsigned int data, val;
448 int ret;
450 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
451 &data);
452 if (ret < 0)
453 return ret;
455 data &= CHG_CNFG_03_TOTIME_MASK;
456 data >>= CHG_CNFG_03_TOTIME_SHIFT;
458 val = data * 10;
460 return sysfs_emit(buf, "%u\n", val);
463 static int max77693_set_top_off_timer(struct max77693_charger *chg,
464 unsigned long minutes)
466 unsigned int data;
468 if (minutes > 70)
469 return -EINVAL;
471 data = minutes / 10;
472 data <<= CHG_CNFG_03_TOTIME_SHIFT;
474 return regmap_update_bits(chg->max77693->regmap,
475 MAX77693_CHG_REG_CHG_CNFG_03,
476 CHG_CNFG_03_TOTIME_MASK, data);
479 static ssize_t top_off_timer_store(struct device *dev,
480 struct device_attribute *attr, const char *buf, size_t count)
482 return device_attr_store(dev, attr, buf, count,
483 max77693_set_top_off_timer);
486 static DEVICE_ATTR_RW(fast_charge_timer);
487 static DEVICE_ATTR_RW(top_off_threshold_current);
488 static DEVICE_ATTR_RW(top_off_timer);
490 static int max77693_set_constant_volt(struct max77693_charger *chg,
491 unsigned int uvolt)
493 unsigned int data;
496 * 0x00 - 3.650 V
497 * 0x01 - 3.675 V
498 * ...
499 * 0x1b - 4.325 V
500 * 0x1c - 4.340 V
501 * 0x1d - 4.350 V
502 * 0x1e - 4.375 V
503 * 0x1f - 4.400 V
505 if (uvolt >= 3650000 && uvolt < 4340000)
506 data = (uvolt - 3650000) / 25000;
507 else if (uvolt >= 4340000 && uvolt < 4350000)
508 data = 0x1c;
509 else if (uvolt >= 4350000 && uvolt <= 4400000)
510 data = 0x1d + (uvolt - 4350000) / 25000;
511 else {
512 dev_err(chg->dev, "Wrong value for charging constant voltage\n");
513 return -EINVAL;
516 data <<= CHG_CNFG_04_CHGCVPRM_SHIFT;
518 dev_dbg(chg->dev, "Charging constant voltage: %u (0x%x)\n", uvolt,
519 data);
521 return regmap_update_bits(chg->max77693->regmap,
522 MAX77693_CHG_REG_CHG_CNFG_04,
523 CHG_CNFG_04_CHGCVPRM_MASK, data);
526 static int max77693_set_min_system_volt(struct max77693_charger *chg,
527 unsigned int uvolt)
529 unsigned int data;
531 if (uvolt < 3000000 || uvolt > 3700000) {
532 dev_err(chg->dev, "Wrong value for minimum system regulation voltage\n");
533 return -EINVAL;
536 data = (uvolt - 3000000) / 100000;
538 data <<= CHG_CNFG_04_MINVSYS_SHIFT;
540 dev_dbg(chg->dev, "Minimum system regulation voltage: %u (0x%x)\n",
541 uvolt, data);
543 return regmap_update_bits(chg->max77693->regmap,
544 MAX77693_CHG_REG_CHG_CNFG_04,
545 CHG_CNFG_04_MINVSYS_MASK, data);
548 static int max77693_set_thermal_regulation_temp(struct max77693_charger *chg,
549 unsigned int cels)
551 unsigned int data;
553 switch (cels) {
554 case 70:
555 case 85:
556 case 100:
557 case 115:
558 data = (cels - 70) / 15;
559 break;
560 default:
561 dev_err(chg->dev, "Wrong value for thermal regulation loop temperature\n");
562 return -EINVAL;
565 data <<= CHG_CNFG_07_REGTEMP_SHIFT;
567 dev_dbg(chg->dev, "Thermal regulation loop temperature: %u (0x%x)\n",
568 cels, data);
570 return regmap_update_bits(chg->max77693->regmap,
571 MAX77693_CHG_REG_CHG_CNFG_07,
572 CHG_CNFG_07_REGTEMP_MASK, data);
575 static int max77693_set_batttery_overcurrent(struct max77693_charger *chg,
576 unsigned int uamp)
578 unsigned int data;
580 if (uamp && (uamp < 2000000 || uamp > 3500000)) {
581 dev_err(chg->dev, "Wrong value for battery overcurrent\n");
582 return -EINVAL;
585 if (uamp)
586 data = ((uamp - 2000000) / 250000) + 1;
587 else
588 data = 0; /* disable */
590 data <<= CHG_CNFG_12_B2SOVRC_SHIFT;
592 dev_dbg(chg->dev, "Battery overcurrent: %u (0x%x)\n", uamp, data);
594 return regmap_update_bits(chg->max77693->regmap,
595 MAX77693_CHG_REG_CHG_CNFG_12,
596 CHG_CNFG_12_B2SOVRC_MASK, data);
599 static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg,
600 unsigned int uvolt)
602 unsigned int data;
604 switch (uvolt) {
605 case 4300000:
606 data = 0x0;
607 break;
608 case 4700000:
609 case 4800000:
610 case 4900000:
611 data = (uvolt - 4700000) / 100000;
612 break;
613 default:
614 dev_err(chg->dev, "Wrong value for charge input voltage regulation threshold\n");
615 return -EINVAL;
618 data <<= CHG_CNFG_12_VCHGINREG_SHIFT;
620 dev_dbg(chg->dev, "Charge input voltage regulation threshold: %u (0x%x)\n",
621 uvolt, data);
623 return regmap_update_bits(chg->max77693->regmap,
624 MAX77693_CHG_REG_CHG_CNFG_12,
625 CHG_CNFG_12_VCHGINREG_MASK, data);
629 * Sets charger registers to proper and safe default values.
631 static int max77693_reg_init(struct max77693_charger *chg)
633 int ret;
634 unsigned int data;
636 /* Unlock charger register protection */
637 data = (0x3 << CHG_CNFG_06_CHGPROT_SHIFT);
638 ret = regmap_update_bits(chg->max77693->regmap,
639 MAX77693_CHG_REG_CHG_CNFG_06,
640 CHG_CNFG_06_CHGPROT_MASK, data);
641 if (ret) {
642 dev_err(chg->dev, "Error unlocking registers: %d\n", ret);
643 return ret;
646 ret = max77693_set_fast_charge_timer(chg, DEFAULT_FAST_CHARGE_TIMER);
647 if (ret)
648 return ret;
650 ret = max77693_set_top_off_threshold_current(chg,
651 DEFAULT_TOP_OFF_THRESHOLD_CURRENT);
652 if (ret)
653 return ret;
655 ret = max77693_set_top_off_timer(chg, DEFAULT_TOP_OFF_TIMER);
656 if (ret)
657 return ret;
659 ret = max77693_set_constant_volt(chg, chg->constant_volt);
660 if (ret)
661 return ret;
663 ret = max77693_set_min_system_volt(chg, chg->min_system_volt);
664 if (ret)
665 return ret;
667 ret = max77693_set_thermal_regulation_temp(chg,
668 chg->thermal_regulation_temp);
669 if (ret)
670 return ret;
672 ret = max77693_set_batttery_overcurrent(chg, chg->batttery_overcurrent);
673 if (ret)
674 return ret;
676 return max77693_set_charge_input_threshold_volt(chg,
677 chg->charge_input_threshold_volt);
680 #ifdef CONFIG_OF
681 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
683 struct device_node *np = dev->of_node;
685 if (!np) {
686 dev_err(dev, "no charger OF node\n");
687 return -EINVAL;
690 if (of_property_read_u32(np, "maxim,constant-microvolt",
691 &chg->constant_volt))
692 chg->constant_volt = DEFAULT_CONSTANT_VOLT;
694 if (of_property_read_u32(np, "maxim,min-system-microvolt",
695 &chg->min_system_volt))
696 chg->min_system_volt = DEFAULT_MIN_SYSTEM_VOLT;
698 if (of_property_read_u32(np, "maxim,thermal-regulation-celsius",
699 &chg->thermal_regulation_temp))
700 chg->thermal_regulation_temp = DEFAULT_THERMAL_REGULATION_TEMP;
702 if (of_property_read_u32(np, "maxim,battery-overcurrent-microamp",
703 &chg->batttery_overcurrent))
704 chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT;
706 if (of_property_read_u32(np, "maxim,charge-input-threshold-microvolt",
707 &chg->charge_input_threshold_volt))
708 chg->charge_input_threshold_volt =
709 DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT;
711 return 0;
713 #else /* CONFIG_OF */
714 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
716 return 0;
718 #endif /* CONFIG_OF */
720 static int max77693_charger_probe(struct platform_device *pdev)
722 struct max77693_charger *chg;
723 struct power_supply_config psy_cfg = {};
724 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
725 int ret;
727 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
728 if (!chg)
729 return -ENOMEM;
731 platform_set_drvdata(pdev, chg);
732 chg->dev = &pdev->dev;
733 chg->max77693 = max77693;
735 ret = max77693_dt_init(&pdev->dev, chg);
736 if (ret)
737 return ret;
739 ret = max77693_reg_init(chg);
740 if (ret)
741 return ret;
743 psy_cfg.drv_data = chg;
745 ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
746 if (ret) {
747 dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n");
748 goto err;
751 ret = device_create_file(&pdev->dev,
752 &dev_attr_top_off_threshold_current);
753 if (ret) {
754 dev_err(&pdev->dev, "failed: create top off current sysfs entry\n");
755 goto err;
758 ret = device_create_file(&pdev->dev, &dev_attr_top_off_timer);
759 if (ret) {
760 dev_err(&pdev->dev, "failed: create top off timer sysfs entry\n");
761 goto err;
764 chg->charger = devm_power_supply_register(&pdev->dev,
765 &max77693_charger_desc,
766 &psy_cfg);
767 if (IS_ERR(chg->charger)) {
768 dev_err(&pdev->dev, "failed: power supply register\n");
769 ret = PTR_ERR(chg->charger);
770 goto err;
773 return 0;
775 err:
776 device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
777 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
778 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
780 return ret;
783 static void max77693_charger_remove(struct platform_device *pdev)
785 device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
786 device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
787 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
790 static const struct platform_device_id max77693_charger_id[] = {
791 { "max77693-charger", 0, },
794 MODULE_DEVICE_TABLE(platform, max77693_charger_id);
796 static struct platform_driver max77693_charger_driver = {
797 .driver = {
798 .name = "max77693-charger",
800 .probe = max77693_charger_probe,
801 .remove = max77693_charger_remove,
802 .id_table = max77693_charger_id,
804 module_platform_driver(max77693_charger_driver);
806 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
807 MODULE_DESCRIPTION("Maxim 77693 charger driver");
808 MODULE_LICENSE("GPL");