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