1 /*
2 * Battery power supply driver for X-Powers AXP20X and AXP22X PMICs
3 *
4 * Copyright 2016 Free Electrons NextThing Co.
5 * Quentin Schulz <quentin.schulz@free-electrons.com>
6 *
7 * This driver is based on a previous upstreaming attempt by:
8 * Bruno Prémont <bonbons@linux-vserver.org>
9 *
10 * This file is subject to the terms and conditions of the GNU General
11 * Public License. See the file "COPYING" in the main directory of this
12 * archive for more details.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20 #include <linux/bitfield.h>
21 #include <linux/err.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/module.h>
25 #include <linux/of.h>
26 #include <linux/platform_device.h>
27 #include <linux/power_supply.h>
28 #include <linux/regmap.h>
29 #include <linux/slab.h>
30 #include <linux/time.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/consumer.h>
33 #include <linux/mfd/axp20x.h>
34
35 #define AXP20X_PWR_STATUS_BAT_CHARGING BIT(2)
36 #define AXP717_PWR_STATUS_MASK GENMASK(6, 5)
37 #define AXP717_PWR_STATUS_BAT_STANDBY 0
38 #define AXP717_PWR_STATUS_BAT_CHRG 1
39 #define AXP717_PWR_STATUS_BAT_DISCHRG 2
40
41 #define AXP20X_PWR_OP_BATT_PRESENT BIT(5)
42 #define AXP20X_PWR_OP_BATT_ACTIVATED BIT(3)
43 #define AXP717_PWR_OP_BATT_PRESENT BIT(3)
44
45 #define AXP717_BATT_PMU_FAULT_MASK GENMASK(2, 0)
46 #define AXP717_BATT_UVLO_2_5V BIT(2)
47 #define AXP717_BATT_OVER_TEMP BIT(1)
48 #define AXP717_BATT_UNDER_TEMP BIT(0)
49
50 #define AXP209_FG_PERCENT GENMASK(6, 0)
51 #define AXP22X_FG_VALID BIT(7)
52
53 #define AXP20X_CHRG_CTRL1_ENABLE BIT(7)
54 #define AXP20X_CHRG_CTRL1_TGT_VOLT GENMASK(6, 5)
55 #define AXP20X_CHRG_CTRL1_TGT_4_1V (0 << 5)
56 #define AXP20X_CHRG_CTRL1_TGT_4_15V (1 << 5)
57 #define AXP20X_CHRG_CTRL1_TGT_4_2V (2 << 5)
58 #define AXP20X_CHRG_CTRL1_TGT_4_36V (3 << 5)
59
60 #define AXP22X_CHRG_CTRL1_TGT_4_22V (1 << 5)
61 #define AXP22X_CHRG_CTRL1_TGT_4_24V (3 << 5)
62
63 #define AXP717_CHRG_ENABLE BIT(1)
64 #define AXP717_CHRG_CV_VOLT_MASK GENMASK(2, 0)
65 #define AXP717_CHRG_CV_4_0V 0
66 #define AXP717_CHRG_CV_4_1V 1
67 #define AXP717_CHRG_CV_4_2V 2
68 #define AXP717_CHRG_CV_4_35V 3
69 #define AXP717_CHRG_CV_4_4V 4
70 /* Values 5 and 6 reserved. */
71 #define AXP717_CHRG_CV_5_0V 7
72
73 #define AXP813_CHRG_CTRL1_TGT_4_35V (3 << 5)
74
75 #define AXP20X_CHRG_CTRL1_TGT_CURR GENMASK(3, 0)
76 #define AXP717_ICC_CHARGER_LIM_MASK GENMASK(5, 0)
77
78 #define AXP717_ITERM_CHG_LIM_MASK GENMASK(3, 0)
79 #define AXP717_ITERM_CC_STEP 64000
80
81 #define AXP20X_V_OFF_MASK GENMASK(2, 0)
82 #define AXP717_V_OFF_MASK GENMASK(6, 4)
83
84 #define AXP717_BAT_VMIN_MIN_UV 2600000
85 #define AXP717_BAT_VMIN_MAX_UV 3300000
86 #define AXP717_BAT_VMIN_STEP 100000
87 #define AXP717_BAT_CV_MIN_UV 4000000
88 #define AXP717_BAT_CV_MAX_UV 5000000
89 #define AXP717_BAT_CC_MIN_UA 0
90 #define AXP717_BAT_CC_MAX_UA 3008000
91
92 #define AXP717_TS_PIN_DISABLE BIT(4)
93
94 struct axp20x_batt_ps;
95
96 struct axp_data {
97 int ccc_scale;
98 int ccc_offset;
99 unsigned int ccc_reg;
100 unsigned int ccc_mask;
101 bool has_fg_valid;
102 const struct power_supply_desc *bat_ps_desc;
103 int (*get_max_voltage)(struct axp20x_batt_ps *batt, int *val);
104 int (*set_max_voltage)(struct axp20x_batt_ps *batt, int val);
105 int (*cfg_iio_chan)(struct platform_device *pdev,
106 struct axp20x_batt_ps *axp_batt);
107 void (*set_bat_info)(struct platform_device *pdev,
108 struct axp20x_batt_ps *axp_batt,
109 struct power_supply_battery_info *info);
110 };
111
112 struct axp20x_batt_ps {
113 struct regmap *regmap;
114 struct power_supply *batt;
115 struct device *dev;
116 struct iio_channel *batt_chrg_i;
117 struct iio_channel *batt_dischrg_i;
118 struct iio_channel *batt_v;
119 /* Maximum constant charge current */
120 unsigned int max_ccc;
121 const struct axp_data *data;
122 bool ts_disable;
123 };
124
axp20x_battery_get_max_voltage(struct axp20x_batt_ps * axp20x_batt,int * val)125 static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
126 int *val)
127 {
128 int ret, reg;
129
130 ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
131 if (ret)
132 return ret;
133
134 switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
135 case AXP20X_CHRG_CTRL1_TGT_4_1V:
136 *val = 4100000;
137 break;
138 case AXP20X_CHRG_CTRL1_TGT_4_15V:
139 *val = 4150000;
140 break;
141 case AXP20X_CHRG_CTRL1_TGT_4_2V:
142 *val = 4200000;
143 break;
144 case AXP20X_CHRG_CTRL1_TGT_4_36V:
145 *val = 4360000;
146 break;
147 default:
148 return -EINVAL;
149 }
150
151 return 0;
152 }
153
axp22x_battery_get_max_voltage(struct axp20x_batt_ps * axp20x_batt,int * val)154 static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
155 int *val)
156 {
157 int ret, reg;
158
159 ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
160 if (ret)
161 return ret;
162
163 switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
164 case AXP20X_CHRG_CTRL1_TGT_4_1V:
165 *val = 4100000;
166 break;
167 case AXP20X_CHRG_CTRL1_TGT_4_2V:
168 *val = 4200000;
169 break;
170 case AXP22X_CHRG_CTRL1_TGT_4_22V:
171 *val = 4220000;
172 break;
173 case AXP22X_CHRG_CTRL1_TGT_4_24V:
174 *val = 4240000;
175 break;
176 default:
177 return -EINVAL;
178 }
179
180 return 0;
181 }
182
axp717_battery_get_max_voltage(struct axp20x_batt_ps * axp20x_batt,int * val)183 static int axp717_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
184 int *val)
185 {
186 int ret, reg;
187
188 ret = regmap_read(axp20x_batt->regmap, AXP717_CV_CHG_SET, ®);
189 if (ret)
190 return ret;
191
192 switch (reg & AXP717_CHRG_CV_VOLT_MASK) {
193 case AXP717_CHRG_CV_4_0V:
194 *val = 4000000;
195 return 0;
196 case AXP717_CHRG_CV_4_1V:
197 *val = 4100000;
198 return 0;
199 case AXP717_CHRG_CV_4_2V:
200 *val = 4200000;
201 return 0;
202 case AXP717_CHRG_CV_4_35V:
203 *val = 4350000;
204 return 0;
205 case AXP717_CHRG_CV_4_4V:
206 *val = 4400000;
207 return 0;
208 case AXP717_CHRG_CV_5_0V:
209 *val = 5000000;
210 return 0;
211 default:
212 return -EINVAL;
213 }
214 }
215
axp813_battery_get_max_voltage(struct axp20x_batt_ps * axp20x_batt,int * val)216 static int axp813_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
217 int *val)
218 {
219 int ret, reg;
220
221 ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
222 if (ret)
223 return ret;
224
225 switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
226 case AXP20X_CHRG_CTRL1_TGT_4_1V:
227 *val = 4100000;
228 break;
229 case AXP20X_CHRG_CTRL1_TGT_4_15V:
230 *val = 4150000;
231 break;
232 case AXP20X_CHRG_CTRL1_TGT_4_2V:
233 *val = 4200000;
234 break;
235 case AXP813_CHRG_CTRL1_TGT_4_35V:
236 *val = 4350000;
237 break;
238 default:
239 return -EINVAL;
240 }
241
242 return 0;
243 }
244
axp20x_get_constant_charge_current(struct axp20x_batt_ps * axp,int * val)245 static int axp20x_get_constant_charge_current(struct axp20x_batt_ps *axp,
246 int *val)
247 {
248 int ret;
249
250 ret = regmap_read(axp->regmap, AXP20X_CHRG_CTRL1, val);
251 if (ret)
252 return ret;
253
254 *val &= AXP20X_CHRG_CTRL1_TGT_CURR;
255
256 *val = *val * axp->data->ccc_scale + axp->data->ccc_offset;
257
258 return 0;
259 }
260
axp717_get_constant_charge_current(struct axp20x_batt_ps * axp,int * val)261 static int axp717_get_constant_charge_current(struct axp20x_batt_ps *axp,
262 int *val)
263 {
264 int ret;
265
266 ret = regmap_read(axp->regmap, AXP717_ICC_CHG_SET, val);
267 if (ret)
268 return ret;
269
270 *val = FIELD_GET(AXP717_ICC_CHARGER_LIM_MASK, *val) *
271 axp->data->ccc_scale;
272
273 return 0;
274 }
275
axp20x_battery_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)276 static int axp20x_battery_get_prop(struct power_supply *psy,
277 enum power_supply_property psp,
278 union power_supply_propval *val)
279 {
280 struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
281 int ret = 0, reg, val1;
282
283 switch (psp) {
284 case POWER_SUPPLY_PROP_PRESENT:
285 case POWER_SUPPLY_PROP_ONLINE:
286 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
287 ®);
288 if (ret)
289 return ret;
290
291 val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
292 break;
293
294 case POWER_SUPPLY_PROP_STATUS:
295 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
296 ®);
297 if (ret)
298 return ret;
299
300 if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
301 val->intval = POWER_SUPPLY_STATUS_CHARGING;
302 return 0;
303 }
304
305 ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
306 &val1);
307 if (ret)
308 return ret;
309
310 if (val1) {
311 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
312 return 0;
313 }
314
315 ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
316 if (ret)
317 return ret;
318
319 /*
320 * Fuel Gauge data takes 7 bits but the stored value seems to be
321 * directly the raw percentage without any scaling to 7 bits.
322 */
323 if ((val1 & AXP209_FG_PERCENT) == 100)
324 val->intval = POWER_SUPPLY_STATUS_FULL;
325 else
326 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
327 break;
328
329 case POWER_SUPPLY_PROP_HEALTH:
330 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
331 &val1);
332 if (ret)
333 return ret;
334
335 if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
336 val->intval = POWER_SUPPLY_HEALTH_DEAD;
337 return 0;
338 }
339
340 val->intval = POWER_SUPPLY_HEALTH_GOOD;
341 break;
342
343 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
344 ret = axp20x_get_constant_charge_current(axp20x_batt,
345 &val->intval);
346 if (ret)
347 return ret;
348 break;
349
350 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
351 val->intval = axp20x_batt->max_ccc;
352 break;
353
354 case POWER_SUPPLY_PROP_CURRENT_NOW:
355 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
356 ®);
357 if (ret)
358 return ret;
359
360 if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
361 ret = iio_read_channel_processed(axp20x_batt->batt_chrg_i, &val->intval);
362 } else {
363 ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i, &val1);
364 val->intval = -val1;
365 }
366 if (ret)
367 return ret;
368
369 /* IIO framework gives mA but Power Supply framework gives uA */
370 val->intval *= 1000;
371 break;
372
373 case POWER_SUPPLY_PROP_CAPACITY:
374 /* When no battery is present, return capacity is 100% */
375 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
376 ®);
377 if (ret)
378 return ret;
379
380 if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
381 val->intval = 100;
382 return 0;
383 }
384
385 ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, ®);
386 if (ret)
387 return ret;
388
389 if (axp20x_batt->data->has_fg_valid && !(reg & AXP22X_FG_VALID))
390 return -EINVAL;
391
392 /*
393 * Fuel Gauge data takes 7 bits but the stored value seems to be
394 * directly the raw percentage without any scaling to 7 bits.
395 */
396 val->intval = reg & AXP209_FG_PERCENT;
397 break;
398
399 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
400 return axp20x_batt->data->get_max_voltage(axp20x_batt,
401 &val->intval);
402
403 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
404 ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, ®);
405 if (ret)
406 return ret;
407
408 val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
409 break;
410
411 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
412 ret = iio_read_channel_processed(axp20x_batt->batt_v,
413 &val->intval);
414 if (ret)
415 return ret;
416
417 /* IIO framework gives mV but Power Supply framework gives uV */
418 val->intval *= 1000;
419 break;
420
421 default:
422 return -EINVAL;
423 }
424
425 return 0;
426 }
427
axp717_battery_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)428 static int axp717_battery_get_prop(struct power_supply *psy,
429 enum power_supply_property psp,
430 union power_supply_propval *val)
431 {
432 struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
433 int ret = 0, reg;
434
435 switch (psp) {
436 case POWER_SUPPLY_PROP_PRESENT:
437 case POWER_SUPPLY_PROP_ONLINE:
438 ret = regmap_read(axp20x_batt->regmap, AXP717_ON_INDICATE,
439 ®);
440 if (ret)
441 return ret;
442
443 val->intval = FIELD_GET(AXP717_PWR_OP_BATT_PRESENT, reg);
444 return 0;
445
446 case POWER_SUPPLY_PROP_STATUS:
447 ret = regmap_read(axp20x_batt->regmap, AXP717_PMU_STATUS_2,
448 ®);
449 if (ret)
450 return ret;
451
452 switch (FIELD_GET(AXP717_PWR_STATUS_MASK, reg)) {
453 case AXP717_PWR_STATUS_BAT_STANDBY:
454 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
455 return 0;
456
457 case AXP717_PWR_STATUS_BAT_CHRG:
458 val->intval = POWER_SUPPLY_STATUS_CHARGING;
459 return 0;
460
461 case AXP717_PWR_STATUS_BAT_DISCHRG:
462 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
463 return 0;
464
465 default:
466 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
467 return 0;
468 }
469
470 /*
471 * If a fault is detected it must also be cleared; if the
472 * condition persists it should reappear. A restart was not
473 * sufficient to clear the bit in testing despite the register
474 * listed as POR.
475 */
476 case POWER_SUPPLY_PROP_HEALTH:
477 ret = regmap_read(axp20x_batt->regmap, AXP717_PMU_FAULT,
478 ®);
479 if (ret)
480 return ret;
481
482 switch (reg & AXP717_BATT_PMU_FAULT_MASK) {
483 case AXP717_BATT_UVLO_2_5V:
484 val->intval = POWER_SUPPLY_HEALTH_DEAD;
485 regmap_write_bits(axp20x_batt->regmap,
486 AXP717_PMU_FAULT,
487 AXP717_BATT_UVLO_2_5V,
488 AXP717_BATT_UVLO_2_5V);
489 return 0;
490
491 case AXP717_BATT_OVER_TEMP:
492 val->intval = POWER_SUPPLY_HEALTH_HOT;
493 regmap_write_bits(axp20x_batt->regmap,
494 AXP717_PMU_FAULT,
495 AXP717_BATT_OVER_TEMP,
496 AXP717_BATT_OVER_TEMP);
497 return 0;
498
499 case AXP717_BATT_UNDER_TEMP:
500 val->intval = POWER_SUPPLY_HEALTH_COLD;
501 regmap_write_bits(axp20x_batt->regmap,
502 AXP717_PMU_FAULT,
503 AXP717_BATT_UNDER_TEMP,
504 AXP717_BATT_UNDER_TEMP);
505 return 0;
506
507 default:
508 val->intval = POWER_SUPPLY_HEALTH_GOOD;
509 return 0;
510 }
511
512 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
513 ret = axp717_get_constant_charge_current(axp20x_batt,
514 &val->intval);
515 if (ret)
516 return ret;
517 return 0;
518
519 case POWER_SUPPLY_PROP_CURRENT_NOW:
520 /*
521 * The offset of this value is currently unknown and is
522 * not documented in the datasheet. Based on
523 * observation it's assumed to be somewhere around
524 * 450ma. I will leave the value raw for now.
525 */
526 ret = iio_read_channel_processed(axp20x_batt->batt_chrg_i, &val->intval);
527 if (ret)
528 return ret;
529 /* IIO framework gives mA but Power Supply framework gives uA */
530 val->intval *= 1000;
531 return 0;
532
533 case POWER_SUPPLY_PROP_CAPACITY:
534 ret = regmap_read(axp20x_batt->regmap, AXP717_ON_INDICATE,
535 ®);
536 if (ret)
537 return ret;
538
539 if (!FIELD_GET(AXP717_PWR_OP_BATT_PRESENT, reg))
540 return -ENODEV;
541
542 ret = regmap_read(axp20x_batt->regmap,
543 AXP717_BATT_PERCENT_DATA, ®);
544 if (ret)
545 return ret;
546
547 /*
548 * Fuel Gauge data takes 7 bits but the stored value seems to be
549 * directly the raw percentage without any scaling to 7 bits.
550 */
551 val->intval = reg & AXP209_FG_PERCENT;
552 return 0;
553
554 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
555 return axp20x_batt->data->get_max_voltage(axp20x_batt,
556 &val->intval);
557
558 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
559 ret = regmap_read(axp20x_batt->regmap,
560 AXP717_VSYS_V_POWEROFF, ®);
561 if (ret)
562 return ret;
563
564 val->intval = AXP717_BAT_VMIN_MIN_UV + AXP717_BAT_VMIN_STEP *
565 (reg & AXP717_V_OFF_MASK);
566 return 0;
567
568 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
569 ret = iio_read_channel_processed(axp20x_batt->batt_v,
570 &val->intval);
571 if (ret)
572 return ret;
573
574 /* IIO framework gives mV but Power Supply framework gives uV */
575 val->intval *= 1000;
576 return 0;
577
578 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
579 ret = regmap_read(axp20x_batt->regmap,
580 AXP717_ITERM_CHG_SET, ®);
581 if (ret)
582 return ret;
583
584 val->intval = (reg & AXP717_ITERM_CHG_LIM_MASK) * AXP717_ITERM_CC_STEP;
585 return 0;
586
587 default:
588 return -EINVAL;
589 }
590 }
591
axp22x_battery_set_max_voltage(struct axp20x_batt_ps * axp20x_batt,int val)592 static int axp22x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
593 int val)
594 {
595 switch (val) {
596 case 4100000:
597 val = AXP20X_CHRG_CTRL1_TGT_4_1V;
598 break;
599
600 case 4200000:
601 val = AXP20X_CHRG_CTRL1_TGT_4_2V;
602 break;
603
604 default:
605 /*
606 * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
607 * can be set to 4.22V and 4.24V, but these voltages are too
608 * high for Lithium based batteries (AXP PMICs are supposed to
609 * be used with these kinds of battery).
610 */
611 return -EINVAL;
612 }
613
614 return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
615 AXP20X_CHRG_CTRL1_TGT_VOLT, val);
616 }
617
axp20x_battery_set_max_voltage(struct axp20x_batt_ps * axp20x_batt,int val)618 static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
619 int val)
620 {
621 switch (val) {
622 case 4100000:
623 val = AXP20X_CHRG_CTRL1_TGT_4_1V;
624 break;
625
626 case 4150000:
627 val = AXP20X_CHRG_CTRL1_TGT_4_15V;
628 break;
629
630 case 4200000:
631 val = AXP20X_CHRG_CTRL1_TGT_4_2V;
632 break;
633
634 default:
635 /*
636 * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
637 * can be set to 4.22V and 4.24V, but these voltages are too
638 * high for Lithium based batteries (AXP PMICs are supposed to
639 * be used with these kinds of battery).
640 */
641 return -EINVAL;
642 }
643
644 return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
645 AXP20X_CHRG_CTRL1_TGT_VOLT, val);
646 }
647
axp717_battery_set_max_voltage(struct axp20x_batt_ps * axp20x_batt,int val)648 static int axp717_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
649 int val)
650 {
651 switch (val) {
652 case 4000000:
653 val = AXP717_CHRG_CV_4_0V;
654 break;
655
656 case 4100000:
657 val = AXP717_CHRG_CV_4_1V;
658 break;
659
660 case 4200000:
661 val = AXP717_CHRG_CV_4_2V;
662 break;
663
664 default:
665 /*
666 * AXP717 can go up to 4.35, 4.4, and 5.0 volts which
667 * seem too high for lithium batteries, so do not allow.
668 */
669 return -EINVAL;
670 }
671
672 return regmap_update_bits(axp20x_batt->regmap,
673 AXP717_CV_CHG_SET,
674 AXP717_CHRG_CV_VOLT_MASK, val);
675 }
676
axp20x_set_constant_charge_current(struct axp20x_batt_ps * axp_batt,int charge_current)677 static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
678 int charge_current)
679 {
680 if (charge_current > axp_batt->max_ccc)
681 return -EINVAL;
682
683 charge_current = (charge_current - axp_batt->data->ccc_offset) /
684 axp_batt->data->ccc_scale;
685
686 if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
687 return -EINVAL;
688
689 return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
690 AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
691 }
692
axp717_set_constant_charge_current(struct axp20x_batt_ps * axp,int charge_current)693 static int axp717_set_constant_charge_current(struct axp20x_batt_ps *axp,
694 int charge_current)
695 {
696 int val;
697
698 if (charge_current > axp->max_ccc)
699 return -EINVAL;
700
701 if (charge_current > AXP717_BAT_CC_MAX_UA || charge_current < 0)
702 return -EINVAL;
703
704 val = (charge_current - axp->data->ccc_offset) /
705 axp->data->ccc_scale;
706
707 return regmap_update_bits(axp->regmap, AXP717_ICC_CHG_SET,
708 AXP717_ICC_CHARGER_LIM_MASK, val);
709 }
710
axp20x_set_max_constant_charge_current(struct axp20x_batt_ps * axp,int charge_current)711 static int axp20x_set_max_constant_charge_current(struct axp20x_batt_ps *axp,
712 int charge_current)
713 {
714 bool lower_max = false;
715
716 charge_current = (charge_current - axp->data->ccc_offset) /
717 axp->data->ccc_scale;
718
719 if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
720 return -EINVAL;
721
722 charge_current = charge_current * axp->data->ccc_scale +
723 axp->data->ccc_offset;
724
725 if (charge_current > axp->max_ccc)
726 dev_warn(axp->dev,
727 "Setting max constant charge current higher than previously defined. Note that increasing the constant charge current may damage your battery.\n");
728 else
729 lower_max = true;
730
731 axp->max_ccc = charge_current;
732
733 if (lower_max) {
734 int current_cc;
735
736 axp20x_get_constant_charge_current(axp, ¤t_cc);
737 if (current_cc > charge_current)
738 axp20x_set_constant_charge_current(axp, charge_current);
739 }
740
741 return 0;
742 }
axp20x_set_voltage_min_design(struct axp20x_batt_ps * axp_batt,int min_voltage)743 static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
744 int min_voltage)
745 {
746 int val1 = (min_voltage - 2600000) / 100000;
747
748 if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
749 return -EINVAL;
750
751 return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
752 AXP20X_V_OFF_MASK, val1);
753 }
754
axp717_set_voltage_min_design(struct axp20x_batt_ps * axp_batt,int min_voltage)755 static int axp717_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
756 int min_voltage)
757 {
758 int val1 = (min_voltage - AXP717_BAT_VMIN_MIN_UV) / AXP717_BAT_VMIN_STEP;
759
760 if (val1 < 0 || val1 > AXP717_V_OFF_MASK)
761 return -EINVAL;
762
763 return regmap_update_bits(axp_batt->regmap,
764 AXP717_VSYS_V_POWEROFF,
765 AXP717_V_OFF_MASK, val1);
766 }
767
axp20x_battery_set_prop(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)768 static int axp20x_battery_set_prop(struct power_supply *psy,
769 enum power_supply_property psp,
770 const union power_supply_propval *val)
771 {
772 struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
773
774 switch (psp) {
775 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
776 return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
777
778 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
779 return axp20x_batt->data->set_max_voltage(axp20x_batt, val->intval);
780
781 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
782 return axp20x_set_constant_charge_current(axp20x_batt,
783 val->intval);
784 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
785 return axp20x_set_max_constant_charge_current(axp20x_batt,
786 val->intval);
787 case POWER_SUPPLY_PROP_STATUS:
788 switch (val->intval) {
789 case POWER_SUPPLY_STATUS_CHARGING:
790 return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
791 AXP20X_CHRG_CTRL1_ENABLE, AXP20X_CHRG_CTRL1_ENABLE);
792
793 case POWER_SUPPLY_STATUS_DISCHARGING:
794 case POWER_SUPPLY_STATUS_NOT_CHARGING:
795 return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
796 AXP20X_CHRG_CTRL1_ENABLE, 0);
797 }
798 fallthrough;
799 default:
800 return -EINVAL;
801 }
802 }
803
axp717_battery_set_prop(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)804 static int axp717_battery_set_prop(struct power_supply *psy,
805 enum power_supply_property psp,
806 const union power_supply_propval *val)
807 {
808 struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
809
810 switch (psp) {
811 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
812 return axp717_set_voltage_min_design(axp20x_batt, val->intval);
813
814 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
815 return axp20x_batt->data->set_max_voltage(axp20x_batt, val->intval);
816
817 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
818 return axp717_set_constant_charge_current(axp20x_batt,
819 val->intval);
820 case POWER_SUPPLY_PROP_STATUS:
821 switch (val->intval) {
822 case POWER_SUPPLY_STATUS_CHARGING:
823 return regmap_update_bits(axp20x_batt->regmap,
824 AXP717_MODULE_EN_CONTROL_2,
825 AXP717_CHRG_ENABLE,
826 AXP717_CHRG_ENABLE);
827
828 case POWER_SUPPLY_STATUS_DISCHARGING:
829 case POWER_SUPPLY_STATUS_NOT_CHARGING:
830 return regmap_update_bits(axp20x_batt->regmap,
831 AXP717_MODULE_EN_CONTROL_2,
832 AXP717_CHRG_ENABLE, 0);
833 }
834 return -EINVAL;
835 default:
836 return -EINVAL;
837 }
838 }
839
840 static enum power_supply_property axp20x_battery_props[] = {
841 POWER_SUPPLY_PROP_PRESENT,
842 POWER_SUPPLY_PROP_ONLINE,
843 POWER_SUPPLY_PROP_STATUS,
844 POWER_SUPPLY_PROP_VOLTAGE_NOW,
845 POWER_SUPPLY_PROP_CURRENT_NOW,
846 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
847 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
848 POWER_SUPPLY_PROP_HEALTH,
849 POWER_SUPPLY_PROP_VOLTAGE_MAX,
850 POWER_SUPPLY_PROP_VOLTAGE_MIN,
851 POWER_SUPPLY_PROP_CAPACITY,
852 };
853
854 static enum power_supply_property axp717_battery_props[] = {
855 POWER_SUPPLY_PROP_PRESENT,
856 POWER_SUPPLY_PROP_ONLINE,
857 POWER_SUPPLY_PROP_STATUS,
858 POWER_SUPPLY_PROP_VOLTAGE_NOW,
859 POWER_SUPPLY_PROP_CURRENT_NOW,
860 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
861 POWER_SUPPLY_PROP_HEALTH,
862 POWER_SUPPLY_PROP_VOLTAGE_MAX,
863 POWER_SUPPLY_PROP_VOLTAGE_MIN,
864 POWER_SUPPLY_PROP_CAPACITY,
865 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
866 };
867
axp20x_battery_prop_writeable(struct power_supply * psy,enum power_supply_property psp)868 static int axp20x_battery_prop_writeable(struct power_supply *psy,
869 enum power_supply_property psp)
870 {
871 return psp == POWER_SUPPLY_PROP_STATUS ||
872 psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
873 psp == POWER_SUPPLY_PROP_VOLTAGE_MAX ||
874 psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT ||
875 psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
876 }
877
axp717_battery_prop_writeable(struct power_supply * psy,enum power_supply_property psp)878 static int axp717_battery_prop_writeable(struct power_supply *psy,
879 enum power_supply_property psp)
880 {
881 return psp == POWER_SUPPLY_PROP_STATUS ||
882 psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
883 psp == POWER_SUPPLY_PROP_VOLTAGE_MAX ||
884 psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
885 }
886
887 static const struct power_supply_desc axp209_batt_ps_desc = {
888 .name = "axp20x-battery",
889 .type = POWER_SUPPLY_TYPE_BATTERY,
890 .properties = axp20x_battery_props,
891 .num_properties = ARRAY_SIZE(axp20x_battery_props),
892 .property_is_writeable = axp20x_battery_prop_writeable,
893 .get_property = axp20x_battery_get_prop,
894 .set_property = axp20x_battery_set_prop,
895 };
896
897 static const struct power_supply_desc axp717_batt_ps_desc = {
898 .name = "axp20x-battery",
899 .type = POWER_SUPPLY_TYPE_BATTERY,
900 .properties = axp717_battery_props,
901 .num_properties = ARRAY_SIZE(axp717_battery_props),
902 .property_is_writeable = axp717_battery_prop_writeable,
903 .get_property = axp717_battery_get_prop,
904 .set_property = axp717_battery_set_prop,
905 };
906
axp209_bat_cfg_iio_channels(struct platform_device * pdev,struct axp20x_batt_ps * axp_batt)907 static int axp209_bat_cfg_iio_channels(struct platform_device *pdev,
908 struct axp20x_batt_ps *axp_batt)
909 {
910 axp_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
911 if (IS_ERR(axp_batt->batt_v)) {
912 if (PTR_ERR(axp_batt->batt_v) == -ENODEV)
913 return -EPROBE_DEFER;
914 return PTR_ERR(axp_batt->batt_v);
915 }
916
917 axp_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
918 "batt_chrg_i");
919 if (IS_ERR(axp_batt->batt_chrg_i)) {
920 if (PTR_ERR(axp_batt->batt_chrg_i) == -ENODEV)
921 return -EPROBE_DEFER;
922 return PTR_ERR(axp_batt->batt_chrg_i);
923 }
924
925 axp_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
926 "batt_dischrg_i");
927 if (IS_ERR(axp_batt->batt_dischrg_i)) {
928 if (PTR_ERR(axp_batt->batt_dischrg_i) == -ENODEV)
929 return -EPROBE_DEFER;
930 return PTR_ERR(axp_batt->batt_dischrg_i);
931 }
932
933 return 0;
934 }
935
axp717_bat_cfg_iio_channels(struct platform_device * pdev,struct axp20x_batt_ps * axp_batt)936 static int axp717_bat_cfg_iio_channels(struct platform_device *pdev,
937 struct axp20x_batt_ps *axp_batt)
938 {
939 axp_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
940 if (IS_ERR(axp_batt->batt_v)) {
941 if (PTR_ERR(axp_batt->batt_v) == -ENODEV)
942 return -EPROBE_DEFER;
943 return PTR_ERR(axp_batt->batt_v);
944 }
945
946 axp_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
947 "batt_chrg_i");
948 if (IS_ERR(axp_batt->batt_chrg_i)) {
949 if (PTR_ERR(axp_batt->batt_chrg_i) == -ENODEV)
950 return -EPROBE_DEFER;
951 return PTR_ERR(axp_batt->batt_chrg_i);
952 }
953
954 return 0;
955 }
956
axp209_set_battery_info(struct platform_device * pdev,struct axp20x_batt_ps * axp_batt,struct power_supply_battery_info * info)957 static void axp209_set_battery_info(struct platform_device *pdev,
958 struct axp20x_batt_ps *axp_batt,
959 struct power_supply_battery_info *info)
960 {
961 int vmin = info->voltage_min_design_uv;
962 int ccc = info->constant_charge_current_max_ua;
963
964 if (vmin > 0 && axp20x_set_voltage_min_design(axp_batt, vmin))
965 dev_err(&pdev->dev,
966 "couldn't set voltage_min_design\n");
967
968 /* Set max to unverified value to be able to set CCC */
969 axp_batt->max_ccc = ccc;
970
971 if (ccc <= 0 || axp20x_set_constant_charge_current(axp_batt, ccc)) {
972 dev_err(&pdev->dev,
973 "couldn't set ccc from DT: fallback to min value\n");
974 ccc = 300000;
975 axp_batt->max_ccc = ccc;
976 axp20x_set_constant_charge_current(axp_batt, ccc);
977 }
978 }
979
axp717_set_battery_info(struct platform_device * pdev,struct axp20x_batt_ps * axp_batt,struct power_supply_battery_info * info)980 static void axp717_set_battery_info(struct platform_device *pdev,
981 struct axp20x_batt_ps *axp_batt,
982 struct power_supply_battery_info *info)
983 {
984 int vmin = info->voltage_min_design_uv;
985 int vmax = info->voltage_max_design_uv;
986 int ccc = info->constant_charge_current_max_ua;
987 int val;
988
989 axp_batt->ts_disable = (device_property_read_bool(axp_batt->dev,
990 "x-powers,no-thermistor"));
991
992 /*
993 * Under rare conditions an incorrectly programmed efuse for
994 * the temp sensor on the PMIC may trigger a fault condition.
995 * Allow users to hard-code if the ts pin is not used to work
996 * around this problem. Note that this requires the battery
997 * be correctly defined in the device tree with a monitored
998 * battery node.
999 */
1000 if (axp_batt->ts_disable) {
1001 regmap_update_bits(axp_batt->regmap,
1002 AXP717_TS_PIN_CFG,
1003 AXP717_TS_PIN_DISABLE,
1004 AXP717_TS_PIN_DISABLE);
1005 }
1006
1007 if (vmin > 0 && axp717_set_voltage_min_design(axp_batt, vmin))
1008 dev_err(&pdev->dev,
1009 "couldn't set voltage_min_design\n");
1010
1011 if (vmax > 0 && axp717_battery_set_max_voltage(axp_batt, vmax))
1012 dev_err(&pdev->dev,
1013 "couldn't set voltage_max_design\n");
1014
1015 axp717_get_constant_charge_current(axp_batt, &val);
1016 axp_batt->max_ccc = ccc;
1017 if (ccc <= 0 || axp717_set_constant_charge_current(axp_batt, ccc)) {
1018 dev_err(&pdev->dev,
1019 "couldn't set ccc from DT: current ccc is %d\n",
1020 val);
1021 }
1022 }
1023
1024 static const struct axp_data axp209_data = {
1025 .ccc_scale = 100000,
1026 .ccc_offset = 300000,
1027 .ccc_reg = AXP20X_CHRG_CTRL1,
1028 .ccc_mask = AXP20X_CHRG_CTRL1_TGT_CURR,
1029 .bat_ps_desc = &axp209_batt_ps_desc,
1030 .get_max_voltage = axp20x_battery_get_max_voltage,
1031 .set_max_voltage = axp20x_battery_set_max_voltage,
1032 .cfg_iio_chan = axp209_bat_cfg_iio_channels,
1033 .set_bat_info = axp209_set_battery_info,
1034 };
1035
1036 static const struct axp_data axp221_data = {
1037 .ccc_scale = 150000,
1038 .ccc_offset = 300000,
1039 .ccc_reg = AXP20X_CHRG_CTRL1,
1040 .ccc_mask = AXP20X_CHRG_CTRL1_TGT_CURR,
1041 .has_fg_valid = true,
1042 .bat_ps_desc = &axp209_batt_ps_desc,
1043 .get_max_voltage = axp22x_battery_get_max_voltage,
1044 .set_max_voltage = axp22x_battery_set_max_voltage,
1045 .cfg_iio_chan = axp209_bat_cfg_iio_channels,
1046 .set_bat_info = axp209_set_battery_info,
1047 };
1048
1049 static const struct axp_data axp717_data = {
1050 .ccc_scale = 64000,
1051 .ccc_offset = 0,
1052 .ccc_reg = AXP717_ICC_CHG_SET,
1053 .ccc_mask = AXP717_ICC_CHARGER_LIM_MASK,
1054 .bat_ps_desc = &axp717_batt_ps_desc,
1055 .get_max_voltage = axp717_battery_get_max_voltage,
1056 .set_max_voltage = axp717_battery_set_max_voltage,
1057 .cfg_iio_chan = axp717_bat_cfg_iio_channels,
1058 .set_bat_info = axp717_set_battery_info,
1059 };
1060
1061 static const struct axp_data axp813_data = {
1062 .ccc_scale = 200000,
1063 .ccc_offset = 200000,
1064 .ccc_reg = AXP20X_CHRG_CTRL1,
1065 .ccc_mask = AXP20X_CHRG_CTRL1_TGT_CURR,
1066 .has_fg_valid = true,
1067 .bat_ps_desc = &axp209_batt_ps_desc,
1068 .get_max_voltage = axp813_battery_get_max_voltage,
1069 .set_max_voltage = axp20x_battery_set_max_voltage,
1070 .cfg_iio_chan = axp209_bat_cfg_iio_channels,
1071 .set_bat_info = axp209_set_battery_info,
1072 };
1073
1074 static const struct of_device_id axp20x_battery_ps_id[] = {
1075 {
1076 .compatible = "x-powers,axp209-battery-power-supply",
1077 .data = (void *)&axp209_data,
1078 }, {
1079 .compatible = "x-powers,axp221-battery-power-supply",
1080 .data = (void *)&axp221_data,
1081 }, {
1082 .compatible = "x-powers,axp717-battery-power-supply",
1083 .data = (void *)&axp717_data,
1084 }, {
1085 .compatible = "x-powers,axp813-battery-power-supply",
1086 .data = (void *)&axp813_data,
1087 }, { /* sentinel */ },
1088 };
1089 MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
1090
axp20x_power_probe(struct platform_device * pdev)1091 static int axp20x_power_probe(struct platform_device *pdev)
1092 {
1093 struct axp20x_batt_ps *axp20x_batt;
1094 struct power_supply_config psy_cfg = {};
1095 struct power_supply_battery_info *info;
1096 struct device *dev = &pdev->dev;
1097 int ret;
1098
1099 if (!of_device_is_available(pdev->dev.of_node))
1100 return -ENODEV;
1101
1102 axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
1103 GFP_KERNEL);
1104 if (!axp20x_batt)
1105 return -ENOMEM;
1106
1107 axp20x_batt->dev = &pdev->dev;
1108
1109 axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
1110 platform_set_drvdata(pdev, axp20x_batt);
1111
1112 psy_cfg.drv_data = axp20x_batt;
1113 psy_cfg.of_node = pdev->dev.of_node;
1114
1115 axp20x_batt->data = (struct axp_data *)of_device_get_match_data(dev);
1116
1117 ret = axp20x_batt->data->cfg_iio_chan(pdev, axp20x_batt);
1118 if (ret)
1119 return ret;
1120
1121 axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
1122 axp20x_batt->data->bat_ps_desc,
1123 &psy_cfg);
1124 if (IS_ERR(axp20x_batt->batt)) {
1125 dev_err(&pdev->dev, "failed to register power supply: %ld\n",
1126 PTR_ERR(axp20x_batt->batt));
1127 return PTR_ERR(axp20x_batt->batt);
1128 }
1129
1130 if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
1131 axp20x_batt->data->set_bat_info(pdev, axp20x_batt, info);
1132 power_supply_put_battery_info(axp20x_batt->batt, info);
1133 }
1134
1135 /*
1136 * Update max CCC to a valid value if battery info is present or set it
1137 * to current register value by default.
1138 */
1139 axp20x_get_constant_charge_current(axp20x_batt, &axp20x_batt->max_ccc);
1140
1141 return 0;
1142 }
1143
1144 static struct platform_driver axp20x_batt_driver = {
1145 .probe = axp20x_power_probe,
1146 .driver = {
1147 .name = "axp20x-battery-power-supply",
1148 .of_match_table = axp20x_battery_ps_id,
1149 },
1150 };
1151
1152 module_platform_driver(axp20x_batt_driver);
1153
1154 MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
1155 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
1156 MODULE_LICENSE("GPL");
1157