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
max77693_get_charger_state(struct regmap * regmap,int * val)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
max77693_get_charge_type(struct regmap * regmap,int * val)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 */
max77693_get_battery_health(struct regmap * regmap,int * val)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
max77693_get_present(struct regmap * regmap,int * val)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
max77693_get_online(struct regmap * regmap,int * val)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 /*
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.
204 */
205
max77693_get_input_current_limit(struct regmap * regmap,int * val)206 static int max77693_get_input_current_limit(struct regmap *regmap, int *val)
207 {
208 unsigned int data;
209 int ret;
210
211 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_CNFG_09, &data);
212 if (ret < 0)
213 return ret;
214
215 data &= CHG_CNFG_09_CHGIN_ILIM_MASK;
216 data >>= CHG_CNFG_09_CHGIN_ILIM_SHIFT;
217
218 if (data <= 0x03)
219 /* The first four values (0x00..0x03) are 60mA */
220 *val = 60000;
221 else
222 *val = data * 20000; /* 20mA steps */
223
224 return 0;
225 }
226
max77693_get_fast_charge_current(struct regmap * regmap,int * val)227 static int max77693_get_fast_charge_current(struct regmap *regmap, int *val)
228 {
229 unsigned int data;
230 int ret;
231
232 ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_CNFG_02, &data);
233 if (ret < 0)
234 return ret;
235
236 data &= CHG_CNFG_02_CC_MASK;
237 data >>= CHG_CNFG_02_CC_SHIFT;
238
239 *val = data * 33300; /* 33.3mA steps */
240
241 return 0;
242 }
243
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,
254 };
255
max77693_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)256 static int max77693_charger_get_property(struct power_supply *psy,
257 enum power_supply_property psp,
258 union power_supply_propval *val)
259 {
260 struct max77693_charger *chg = power_supply_get_drvdata(psy);
261 struct regmap *regmap = chg->max77693->regmap;
262 int ret = 0;
263
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;
294 }
295
296 return ret;
297 }
298
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,
305 };
306
device_attr_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count,int (* fn)(struct max77693_charger *,unsigned long))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))
310 {
311 struct max77693_charger *chg = dev_get_drvdata(dev);
312 unsigned long val;
313 int ret;
314
315 ret = kstrtoul(buf, 10, &val);
316 if (ret)
317 return ret;
318
319 ret = fn(chg, val);
320 if (ret)
321 return ret;
322
323 return count;
324 }
325
fast_charge_timer_show(struct device * dev,struct device_attribute * attr,char * buf)326 static ssize_t fast_charge_timer_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
328 {
329 struct max77693_charger *chg = dev_get_drvdata(dev);
330 unsigned int data, val;
331 int ret;
332
333 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_01,
334 &data);
335 if (ret < 0)
336 return ret;
337
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;
349 }
350
351 return sysfs_emit(buf, "%u\n", val);
352 }
353
max77693_set_fast_charge_timer(struct max77693_charger * chg,unsigned long hours)354 static int max77693_set_fast_charge_timer(struct max77693_charger *chg,
355 unsigned long hours)
356 {
357 unsigned int data;
358
359 /*
360 * 0x00 - disable
361 * 0x01 - 4h
362 * 0x02 - 6h
363 * ...
364 * 0x07 - 16h
365 * Round down odd values.
366 */
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;
377 }
378 data <<= CHG_CNFG_01_FCHGTIME_SHIFT;
379
380 return regmap_update_bits(chg->max77693->regmap,
381 MAX77693_CHG_REG_CHG_CNFG_01,
382 CHG_CNFG_01_FCHGTIME_MASK, data);
383 }
384
fast_charge_timer_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)385 static ssize_t fast_charge_timer_store(struct device *dev,
386 struct device_attribute *attr, const char *buf, size_t count)
387 {
388 return device_attr_store(dev, attr, buf, count,
389 max77693_set_fast_charge_timer);
390 }
391
top_off_threshold_current_show(struct device * dev,struct device_attribute * attr,char * buf)392 static ssize_t top_off_threshold_current_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
394 {
395 struct max77693_charger *chg = dev_get_drvdata(dev);
396 unsigned int data, val;
397 int ret;
398
399 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
400 &data);
401 if (ret < 0)
402 return ret;
403
404 data &= CHG_CNFG_03_TOITH_MASK;
405 data >>= CHG_CNFG_03_TOITH_SHIFT;
406
407 if (data <= 0x04)
408 val = 100000 + data * 25000;
409 else
410 val = data * 50000;
411
412 return sysfs_emit(buf, "%u\n", val);
413 }
414
max77693_set_top_off_threshold_current(struct max77693_charger * chg,unsigned long uamp)415 static int max77693_set_top_off_threshold_current(struct max77693_charger *chg,
416 unsigned long uamp)
417 {
418 unsigned int data;
419
420 if (uamp < 100000 || uamp > 350000)
421 return -EINVAL;
422
423 if (uamp <= 200000)
424 data = (uamp - 100000) / 25000;
425 else
426 /* (200000, 350000> */
427 data = uamp / 50000;
428
429 data <<= CHG_CNFG_03_TOITH_SHIFT;
430
431 return regmap_update_bits(chg->max77693->regmap,
432 MAX77693_CHG_REG_CHG_CNFG_03,
433 CHG_CNFG_03_TOITH_MASK, data);
434 }
435
top_off_threshold_current_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)436 static ssize_t top_off_threshold_current_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_threshold_current);
441 }
442
top_off_timer_show(struct device * dev,struct device_attribute * attr,char * buf)443 static ssize_t top_off_timer_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
445 {
446 struct max77693_charger *chg = dev_get_drvdata(dev);
447 unsigned int data, val;
448 int ret;
449
450 ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
451 &data);
452 if (ret < 0)
453 return ret;
454
455 data &= CHG_CNFG_03_TOTIME_MASK;
456 data >>= CHG_CNFG_03_TOTIME_SHIFT;
457
458 val = data * 10;
459
460 return sysfs_emit(buf, "%u\n", val);
461 }
462
max77693_set_top_off_timer(struct max77693_charger * chg,unsigned long minutes)463 static int max77693_set_top_off_timer(struct max77693_charger *chg,
464 unsigned long minutes)
465 {
466 unsigned int data;
467
468 if (minutes > 70)
469 return -EINVAL;
470
471 data = minutes / 10;
472 data <<= CHG_CNFG_03_TOTIME_SHIFT;
473
474 return regmap_update_bits(chg->max77693->regmap,
475 MAX77693_CHG_REG_CHG_CNFG_03,
476 CHG_CNFG_03_TOTIME_MASK, data);
477 }
478
top_off_timer_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)479 static ssize_t top_off_timer_store(struct device *dev,
480 struct device_attribute *attr, const char *buf, size_t count)
481 {
482 return device_attr_store(dev, attr, buf, count,
483 max77693_set_top_off_timer);
484 }
485
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);
489
max77693_set_constant_volt(struct max77693_charger * chg,unsigned int uvolt)490 static int max77693_set_constant_volt(struct max77693_charger *chg,
491 unsigned int uvolt)
492 {
493 unsigned int data;
494
495 /*
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
504 */
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;
514 }
515
516 data <<= CHG_CNFG_04_CHGCVPRM_SHIFT;
517
518 dev_dbg(chg->dev, "Charging constant voltage: %u (0x%x)\n", uvolt,
519 data);
520
521 return regmap_update_bits(chg->max77693->regmap,
522 MAX77693_CHG_REG_CHG_CNFG_04,
523 CHG_CNFG_04_CHGCVPRM_MASK, data);
524 }
525
max77693_set_min_system_volt(struct max77693_charger * chg,unsigned int uvolt)526 static int max77693_set_min_system_volt(struct max77693_charger *chg,
527 unsigned int uvolt)
528 {
529 unsigned int data;
530
531 if (uvolt < 3000000 || uvolt > 3700000) {
532 dev_err(chg->dev, "Wrong value for minimum system regulation voltage\n");
533 return -EINVAL;
534 }
535
536 data = (uvolt - 3000000) / 100000;
537
538 data <<= CHG_CNFG_04_MINVSYS_SHIFT;
539
540 dev_dbg(chg->dev, "Minimum system regulation voltage: %u (0x%x)\n",
541 uvolt, data);
542
543 return regmap_update_bits(chg->max77693->regmap,
544 MAX77693_CHG_REG_CHG_CNFG_04,
545 CHG_CNFG_04_MINVSYS_MASK, data);
546 }
547
max77693_set_thermal_regulation_temp(struct max77693_charger * chg,unsigned int cels)548 static int max77693_set_thermal_regulation_temp(struct max77693_charger *chg,
549 unsigned int cels)
550 {
551 unsigned int data;
552
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;
563 }
564
565 data <<= CHG_CNFG_07_REGTEMP_SHIFT;
566
567 dev_dbg(chg->dev, "Thermal regulation loop temperature: %u (0x%x)\n",
568 cels, data);
569
570 return regmap_update_bits(chg->max77693->regmap,
571 MAX77693_CHG_REG_CHG_CNFG_07,
572 CHG_CNFG_07_REGTEMP_MASK, data);
573 }
574
max77693_set_batttery_overcurrent(struct max77693_charger * chg,unsigned int uamp)575 static int max77693_set_batttery_overcurrent(struct max77693_charger *chg,
576 unsigned int uamp)
577 {
578 unsigned int data;
579
580 if (uamp && (uamp < 2000000 || uamp > 3500000)) {
581 dev_err(chg->dev, "Wrong value for battery overcurrent\n");
582 return -EINVAL;
583 }
584
585 if (uamp)
586 data = ((uamp - 2000000) / 250000) + 1;
587 else
588 data = 0; /* disable */
589
590 data <<= CHG_CNFG_12_B2SOVRC_SHIFT;
591
592 dev_dbg(chg->dev, "Battery overcurrent: %u (0x%x)\n", uamp, data);
593
594 return regmap_update_bits(chg->max77693->regmap,
595 MAX77693_CHG_REG_CHG_CNFG_12,
596 CHG_CNFG_12_B2SOVRC_MASK, data);
597 }
598
max77693_set_charge_input_threshold_volt(struct max77693_charger * chg,unsigned int uvolt)599 static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg,
600 unsigned int uvolt)
601 {
602 unsigned int data;
603
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;
616 }
617
618 data <<= CHG_CNFG_12_VCHGINREG_SHIFT;
619
620 dev_dbg(chg->dev, "Charge input voltage regulation threshold: %u (0x%x)\n",
621 uvolt, data);
622
623 return regmap_update_bits(chg->max77693->regmap,
624 MAX77693_CHG_REG_CHG_CNFG_12,
625 CHG_CNFG_12_VCHGINREG_MASK, data);
626 }
627
628 /*
629 * Sets charger registers to proper and safe default values.
630 */
max77693_reg_init(struct max77693_charger * chg)631 static int max77693_reg_init(struct max77693_charger *chg)
632 {
633 int ret;
634 unsigned int data;
635
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;
644 }
645
646 ret = max77693_set_fast_charge_timer(chg, DEFAULT_FAST_CHARGE_TIMER);
647 if (ret)
648 return ret;
649
650 ret = max77693_set_top_off_threshold_current(chg,
651 DEFAULT_TOP_OFF_THRESHOLD_CURRENT);
652 if (ret)
653 return ret;
654
655 ret = max77693_set_top_off_timer(chg, DEFAULT_TOP_OFF_TIMER);
656 if (ret)
657 return ret;
658
659 ret = max77693_set_constant_volt(chg, chg->constant_volt);
660 if (ret)
661 return ret;
662
663 ret = max77693_set_min_system_volt(chg, chg->min_system_volt);
664 if (ret)
665 return ret;
666
667 ret = max77693_set_thermal_regulation_temp(chg,
668 chg->thermal_regulation_temp);
669 if (ret)
670 return ret;
671
672 ret = max77693_set_batttery_overcurrent(chg, chg->batttery_overcurrent);
673 if (ret)
674 return ret;
675
676 return max77693_set_charge_input_threshold_volt(chg,
677 chg->charge_input_threshold_volt);
678 }
679
680 #ifdef CONFIG_OF
max77693_dt_init(struct device * dev,struct max77693_charger * chg)681 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
682 {
683 struct device_node *np = dev->of_node;
684
685 if (!np) {
686 dev_err(dev, "no charger OF node\n");
687 return -EINVAL;
688 }
689
690 if (of_property_read_u32(np, "maxim,constant-microvolt",
691 &chg->constant_volt))
692 chg->constant_volt = DEFAULT_CONSTANT_VOLT;
693
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;
697
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;
701
702 if (of_property_read_u32(np, "maxim,battery-overcurrent-microamp",
703 &chg->batttery_overcurrent))
704 chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT;
705
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;
710
711 return 0;
712 }
713 #else /* CONFIG_OF */
max77693_dt_init(struct device * dev,struct max77693_charger * chg)714 static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
715 {
716 return 0;
717 }
718 #endif /* CONFIG_OF */
719
max77693_charger_probe(struct platform_device * pdev)720 static int max77693_charger_probe(struct platform_device *pdev)
721 {
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;
726
727 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
728 if (!chg)
729 return -ENOMEM;
730
731 platform_set_drvdata(pdev, chg);
732 chg->dev = &pdev->dev;
733 chg->max77693 = max77693;
734
735 ret = max77693_dt_init(&pdev->dev, chg);
736 if (ret)
737 return ret;
738
739 ret = max77693_reg_init(chg);
740 if (ret)
741 return ret;
742
743 psy_cfg.drv_data = chg;
744
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;
749 }
750
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;
756 }
757
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;
762 }
763
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;
771 }
772
773 return 0;
774
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);
779
780 return ret;
781 }
782
max77693_charger_remove(struct platform_device * pdev)783 static void max77693_charger_remove(struct platform_device *pdev)
784 {
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);
788 }
789
790 static const struct platform_device_id max77693_charger_id[] = {
791 { "max77693-charger", 0, },
792 { }
793 };
794 MODULE_DEVICE_TABLE(platform, max77693_charger_id);
795
796 static struct platform_driver max77693_charger_driver = {
797 .driver = {
798 .name = "max77693-charger",
799 },
800 .probe = max77693_charger_probe,
801 .remove_new = max77693_charger_remove,
802 .id_table = max77693_charger_id,
803 };
804 module_platform_driver(max77693_charger_driver);
805
806 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
807 MODULE_DESCRIPTION("Maxim 77693 charger driver");
808 MODULE_LICENSE("GPL");
809