1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ADP5061 I2C Programmable Linear Battery Charger
4 *
5 * Copyright 2018 Analog Devices Inc.
6 */
7
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/i2c.h>
12 #include <linux/delay.h>
13 #include <linux/pm.h>
14 #include <linux/power_supply.h>
15 #include <linux/platform_device.h>
16 #include <linux/of.h>
17 #include <linux/regmap.h>
18
19 /* ADP5061 registers definition */
20 #define ADP5061_ID 0x00
21 #define ADP5061_REV 0x01
22 #define ADP5061_VINX_SET 0x02
23 #define ADP5061_TERM_SET 0x03
24 #define ADP5061_CHG_CURR 0x04
25 #define ADP5061_VOLTAGE_TH 0x05
26 #define ADP5061_TIMER_SET 0x06
27 #define ADP5061_FUNC_SET_1 0x07
28 #define ADP5061_FUNC_SET_2 0x08
29 #define ADP5061_INT_EN 0x09
30 #define ADP5061_INT_ACT 0x0A
31 #define ADP5061_CHG_STATUS_1 0x0B
32 #define ADP5061_CHG_STATUS_2 0x0C
33 #define ADP5061_FAULT 0x0D
34 #define ADP5061_BATTERY_SHORT 0x10
35 #define ADP5061_IEND 0x11
36
37 /* ADP5061_VINX_SET */
38 #define ADP5061_VINX_SET_ILIM_MSK GENMASK(3, 0)
39 #define ADP5061_VINX_SET_ILIM_MODE(x) (((x) & 0x0F) << 0)
40
41 /* ADP5061_TERM_SET */
42 #define ADP5061_TERM_SET_VTRM_MSK GENMASK(7, 2)
43 #define ADP5061_TERM_SET_VTRM_MODE(x) (((x) & 0x3F) << 2)
44 #define ADP5061_TERM_SET_CHG_VLIM_MSK GENMASK(1, 0)
45 #define ADP5061_TERM_SET_CHG_VLIM_MODE(x) (((x) & 0x03) << 0)
46
47 /* ADP5061_CHG_CURR */
48 #define ADP5061_CHG_CURR_ICHG_MSK GENMASK(6, 2)
49 #define ADP5061_CHG_CURR_ICHG_MODE(x) (((x) & 0x1F) << 2)
50 #define ADP5061_CHG_CURR_ITRK_DEAD_MSK GENMASK(1, 0)
51 #define ADP5061_CHG_CURR_ITRK_DEAD_MODE(x) (((x) & 0x03) << 0)
52
53 /* ADP5061_VOLTAGE_TH */
54 #define ADP5061_VOLTAGE_TH_DIS_RCH_MSK BIT(7)
55 #define ADP5061_VOLTAGE_TH_DIS_RCH_MODE(x) (((x) & 0x01) << 7)
56 #define ADP5061_VOLTAGE_TH_VRCH_MSK GENMASK(6, 5)
57 #define ADP5061_VOLTAGE_TH_VRCH_MODE(x) (((x) & 0x03) << 5)
58 #define ADP5061_VOLTAGE_TH_VTRK_DEAD_MSK GENMASK(4, 3)
59 #define ADP5061_VOLTAGE_TH_VTRK_DEAD_MODE(x) (((x) & 0x03) << 3)
60 #define ADP5061_VOLTAGE_TH_VWEAK_MSK GENMASK(2, 0)
61 #define ADP5061_VOLTAGE_TH_VWEAK_MODE(x) (((x) & 0x07) << 0)
62
63 /* ADP5061_CHG_STATUS_1 */
64 #define ADP5061_CHG_STATUS_1_VIN_OV(x) (((x) >> 7) & 0x1)
65 #define ADP5061_CHG_STATUS_1_VIN_OK(x) (((x) >> 6) & 0x1)
66 #define ADP5061_CHG_STATUS_1_VIN_ILIM(x) (((x) >> 5) & 0x1)
67 #define ADP5061_CHG_STATUS_1_THERM_LIM(x) (((x) >> 4) & 0x1)
68 #define ADP5061_CHG_STATUS_1_CHDONE(x) (((x) >> 3) & 0x1)
69 #define ADP5061_CHG_STATUS_1_CHG_STATUS(x) (((x) >> 0) & 0x7)
70
71 /* ADP5061_CHG_STATUS_2 */
72 #define ADP5061_CHG_STATUS_2_THR_STATUS(x) (((x) >> 5) & 0x7)
73 #define ADP5061_CHG_STATUS_2_RCH_LIM_INFO(x) (((x) >> 3) & 0x1)
74 #define ADP5061_CHG_STATUS_2_BAT_STATUS(x) (((x) >> 0) & 0x7)
75
76 /* ADP5061_IEND */
77 #define ADP5061_IEND_IEND_MSK GENMASK(7, 5)
78 #define ADP5061_IEND_IEND_MODE(x) (((x) & 0x07) << 5)
79
80 #define ADP5061_NO_BATTERY 0x01
81 #define ADP5061_ICHG_MAX 1300 // mA
82
83 enum adp5061_chg_status {
84 ADP5061_CHG_OFF,
85 ADP5061_CHG_TRICKLE,
86 ADP5061_CHG_FAST_CC,
87 ADP5061_CHG_FAST_CV,
88 ADP5061_CHG_COMPLETE,
89 ADP5061_CHG_LDO_MODE,
90 ADP5061_CHG_TIMER_EXP,
91 ADP5061_CHG_BAT_DET,
92 };
93
94 static const int adp5061_chg_type[4] = {
95 [ADP5061_CHG_OFF] = POWER_SUPPLY_CHARGE_TYPE_NONE,
96 [ADP5061_CHG_TRICKLE] = POWER_SUPPLY_CHARGE_TYPE_TRICKLE,
97 [ADP5061_CHG_FAST_CC] = POWER_SUPPLY_CHARGE_TYPE_FAST,
98 [ADP5061_CHG_FAST_CV] = POWER_SUPPLY_CHARGE_TYPE_FAST,
99 };
100
101 static const int adp5061_vweak_th[8] = {
102 2700, 2800, 2900, 3000, 3100, 3200, 3300, 3400,
103 };
104
105 static const int adp5061_prechg_current[4] = {
106 5, 10, 20, 80,
107 };
108
109 static const int adp5061_vmin[4] = {
110 2000, 2500, 2600, 2900,
111 };
112
113 static const int adp5061_const_chg_vmax[4] = {
114 3200, 3400, 3700, 3800,
115 };
116
117 static const int adp5061_const_ichg[24] = {
118 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650,
119 700, 750, 800, 850, 900, 950, 1000, 1050, 1100, 1200, 1300,
120 };
121
122 static const int adp5061_vmax[36] = {
123 3800, 3820, 3840, 3860, 3880, 3900, 3920, 3940, 3960, 3980,
124 4000, 4020, 4040, 4060, 4080, 4100, 4120, 4140, 4160, 4180,
125 4200, 4220, 4240, 4260, 4280, 4300, 4320, 4340, 4360, 4380,
126 4400, 4420, 4440, 4460, 4480, 4500,
127 };
128
129 static const int adp5061_in_current_lim[16] = {
130 100, 150, 200, 250, 300, 400, 500, 600, 700,
131 800, 900, 1000, 1200, 1500, 1800, 2100,
132 };
133
134 static const int adp5061_iend[8] = {
135 12500, 32500, 52500, 72500, 92500, 117500, 142500, 170000,
136 };
137
138 struct adp5061_state {
139 struct i2c_client *client;
140 struct regmap *regmap;
141 struct power_supply *psy;
142 };
143
adp5061_get_array_index(const int * array,u8 size,int val)144 static int adp5061_get_array_index(const int *array, u8 size, int val)
145 {
146 int i;
147
148 for (i = 1; i < size; i++) {
149 if (val < array[i])
150 break;
151 }
152
153 return i-1;
154 }
155
adp5061_get_status(struct adp5061_state * st,u8 * status1,u8 * status2)156 static int adp5061_get_status(struct adp5061_state *st,
157 u8 *status1, u8 *status2)
158 {
159 u8 buf[2];
160 int ret;
161
162 /* CHG_STATUS1 and CHG_STATUS2 are adjacent regs */
163 ret = regmap_bulk_read(st->regmap, ADP5061_CHG_STATUS_1,
164 &buf[0], 2);
165 if (ret < 0)
166 return ret;
167
168 *status1 = buf[0];
169 *status2 = buf[1];
170
171 return ret;
172 }
173
adp5061_get_input_current_limit(struct adp5061_state * st,union power_supply_propval * val)174 static int adp5061_get_input_current_limit(struct adp5061_state *st,
175 union power_supply_propval *val)
176 {
177 unsigned int regval;
178 int mode, ret;
179
180 ret = regmap_read(st->regmap, ADP5061_VINX_SET, ®val);
181 if (ret < 0)
182 return ret;
183
184 mode = ADP5061_VINX_SET_ILIM_MODE(regval);
185 val->intval = adp5061_in_current_lim[mode] * 1000;
186
187 return ret;
188 }
189
adp5061_set_input_current_limit(struct adp5061_state * st,int val)190 static int adp5061_set_input_current_limit(struct adp5061_state *st, int val)
191 {
192 int index;
193
194 /* Convert from uA to mA */
195 val /= 1000;
196 index = adp5061_get_array_index(adp5061_in_current_lim,
197 ARRAY_SIZE(adp5061_in_current_lim),
198 val);
199 if (index < 0)
200 return index;
201
202 return regmap_update_bits(st->regmap, ADP5061_VINX_SET,
203 ADP5061_VINX_SET_ILIM_MSK,
204 ADP5061_VINX_SET_ILIM_MODE(index));
205 }
206
adp5061_set_min_voltage(struct adp5061_state * st,int val)207 static int adp5061_set_min_voltage(struct adp5061_state *st, int val)
208 {
209 int index;
210
211 /* Convert from uV to mV */
212 val /= 1000;
213 index = adp5061_get_array_index(adp5061_vmin,
214 ARRAY_SIZE(adp5061_vmin),
215 val);
216 if (index < 0)
217 return index;
218
219 return regmap_update_bits(st->regmap, ADP5061_VOLTAGE_TH,
220 ADP5061_VOLTAGE_TH_VTRK_DEAD_MSK,
221 ADP5061_VOLTAGE_TH_VTRK_DEAD_MODE(index));
222 }
223
adp5061_get_min_voltage(struct adp5061_state * st,union power_supply_propval * val)224 static int adp5061_get_min_voltage(struct adp5061_state *st,
225 union power_supply_propval *val)
226 {
227 unsigned int regval;
228 int ret;
229
230 ret = regmap_read(st->regmap, ADP5061_VOLTAGE_TH, ®val);
231 if (ret < 0)
232 return ret;
233
234 regval = ((regval & ADP5061_VOLTAGE_TH_VTRK_DEAD_MSK) >> 3);
235 val->intval = adp5061_vmin[regval] * 1000;
236
237 return ret;
238 }
239
adp5061_get_chg_volt_lim(struct adp5061_state * st,union power_supply_propval * val)240 static int adp5061_get_chg_volt_lim(struct adp5061_state *st,
241 union power_supply_propval *val)
242 {
243 unsigned int regval;
244 int mode, ret;
245
246 ret = regmap_read(st->regmap, ADP5061_TERM_SET, ®val);
247 if (ret < 0)
248 return ret;
249
250 mode = ADP5061_TERM_SET_CHG_VLIM_MODE(regval);
251 val->intval = adp5061_const_chg_vmax[mode] * 1000;
252
253 return ret;
254 }
255
adp5061_get_max_voltage(struct adp5061_state * st,union power_supply_propval * val)256 static int adp5061_get_max_voltage(struct adp5061_state *st,
257 union power_supply_propval *val)
258 {
259 unsigned int regval;
260 int ret;
261
262 ret = regmap_read(st->regmap, ADP5061_TERM_SET, ®val);
263 if (ret < 0)
264 return ret;
265
266 regval = ((regval & ADP5061_TERM_SET_VTRM_MSK) >> 2) - 0x0F;
267 if (regval >= ARRAY_SIZE(adp5061_vmax))
268 regval = ARRAY_SIZE(adp5061_vmax) - 1;
269
270 val->intval = adp5061_vmax[regval] * 1000;
271
272 return ret;
273 }
274
adp5061_set_max_voltage(struct adp5061_state * st,int val)275 static int adp5061_set_max_voltage(struct adp5061_state *st, int val)
276 {
277 int vmax_index;
278
279 /* Convert from uV to mV */
280 val /= 1000;
281 if (val > 4500)
282 val = 4500;
283
284 vmax_index = adp5061_get_array_index(adp5061_vmax,
285 ARRAY_SIZE(adp5061_vmax), val);
286 if (vmax_index < 0)
287 return vmax_index;
288
289 vmax_index += 0x0F;
290
291 return regmap_update_bits(st->regmap, ADP5061_TERM_SET,
292 ADP5061_TERM_SET_VTRM_MSK,
293 ADP5061_TERM_SET_VTRM_MODE(vmax_index));
294 }
295
adp5061_set_const_chg_vmax(struct adp5061_state * st,int val)296 static int adp5061_set_const_chg_vmax(struct adp5061_state *st, int val)
297 {
298 int index;
299
300 /* Convert from uV to mV */
301 val /= 1000;
302 index = adp5061_get_array_index(adp5061_const_chg_vmax,
303 ARRAY_SIZE(adp5061_const_chg_vmax),
304 val);
305 if (index < 0)
306 return index;
307
308 return regmap_update_bits(st->regmap, ADP5061_TERM_SET,
309 ADP5061_TERM_SET_CHG_VLIM_MSK,
310 ADP5061_TERM_SET_CHG_VLIM_MODE(index));
311 }
312
adp5061_set_const_chg_current(struct adp5061_state * st,int val)313 static int adp5061_set_const_chg_current(struct adp5061_state *st, int val)
314 {
315
316 int index;
317
318 /* Convert from uA to mA */
319 val /= 1000;
320 if (val > ADP5061_ICHG_MAX)
321 val = ADP5061_ICHG_MAX;
322
323 index = adp5061_get_array_index(adp5061_const_ichg,
324 ARRAY_SIZE(adp5061_const_ichg),
325 val);
326 if (index < 0)
327 return index;
328
329 return regmap_update_bits(st->regmap, ADP5061_CHG_CURR,
330 ADP5061_CHG_CURR_ICHG_MSK,
331 ADP5061_CHG_CURR_ICHG_MODE(index));
332 }
333
adp5061_get_const_chg_current(struct adp5061_state * st,union power_supply_propval * val)334 static int adp5061_get_const_chg_current(struct adp5061_state *st,
335 union power_supply_propval *val)
336 {
337 unsigned int regval;
338 int ret;
339
340 ret = regmap_read(st->regmap, ADP5061_CHG_CURR, ®val);
341 if (ret < 0)
342 return ret;
343
344 regval = ((regval & ADP5061_CHG_CURR_ICHG_MSK) >> 2);
345 if (regval >= ARRAY_SIZE(adp5061_const_ichg))
346 regval = ARRAY_SIZE(adp5061_const_ichg) - 1;
347
348 val->intval = adp5061_const_ichg[regval] * 1000;
349
350 return ret;
351 }
352
adp5061_get_prechg_current(struct adp5061_state * st,union power_supply_propval * val)353 static int adp5061_get_prechg_current(struct adp5061_state *st,
354 union power_supply_propval *val)
355 {
356 unsigned int regval;
357 int ret;
358
359 ret = regmap_read(st->regmap, ADP5061_CHG_CURR, ®val);
360 if (ret < 0)
361 return ret;
362
363 regval &= ADP5061_CHG_CURR_ITRK_DEAD_MSK;
364 val->intval = adp5061_prechg_current[regval] * 1000;
365
366 return ret;
367 }
368
adp5061_set_prechg_current(struct adp5061_state * st,int val)369 static int adp5061_set_prechg_current(struct adp5061_state *st, int val)
370 {
371 int index;
372
373 /* Convert from uA to mA */
374 val /= 1000;
375 index = adp5061_get_array_index(adp5061_prechg_current,
376 ARRAY_SIZE(adp5061_prechg_current),
377 val);
378 if (index < 0)
379 return index;
380
381 return regmap_update_bits(st->regmap, ADP5061_CHG_CURR,
382 ADP5061_CHG_CURR_ITRK_DEAD_MSK,
383 ADP5061_CHG_CURR_ITRK_DEAD_MODE(index));
384 }
385
adp5061_get_vweak_th(struct adp5061_state * st,union power_supply_propval * val)386 static int adp5061_get_vweak_th(struct adp5061_state *st,
387 union power_supply_propval *val)
388 {
389 unsigned int regval;
390 int ret;
391
392 ret = regmap_read(st->regmap, ADP5061_VOLTAGE_TH, ®val);
393 if (ret < 0)
394 return ret;
395
396 regval &= ADP5061_VOLTAGE_TH_VWEAK_MSK;
397 val->intval = adp5061_vweak_th[regval] * 1000;
398
399 return ret;
400 }
401
adp5061_set_vweak_th(struct adp5061_state * st,int val)402 static int adp5061_set_vweak_th(struct adp5061_state *st, int val)
403 {
404 int index;
405
406 /* Convert from uV to mV */
407 val /= 1000;
408 index = adp5061_get_array_index(adp5061_vweak_th,
409 ARRAY_SIZE(adp5061_vweak_th),
410 val);
411 if (index < 0)
412 return index;
413
414 return regmap_update_bits(st->regmap, ADP5061_VOLTAGE_TH,
415 ADP5061_VOLTAGE_TH_VWEAK_MSK,
416 ADP5061_VOLTAGE_TH_VWEAK_MODE(index));
417 }
418
adp5061_get_chg_type(struct adp5061_state * st,union power_supply_propval * val)419 static int adp5061_get_chg_type(struct adp5061_state *st,
420 union power_supply_propval *val)
421 {
422 u8 status1, status2;
423 int chg_type, ret;
424
425 ret = adp5061_get_status(st, &status1, &status2);
426 if (ret < 0)
427 return ret;
428
429 chg_type = ADP5061_CHG_STATUS_1_CHG_STATUS(status1);
430 if (chg_type >= ARRAY_SIZE(adp5061_chg_type))
431 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
432 else
433 val->intval = adp5061_chg_type[chg_type];
434
435 return ret;
436 }
437
adp5061_get_charger_status(struct adp5061_state * st,union power_supply_propval * val)438 static int adp5061_get_charger_status(struct adp5061_state *st,
439 union power_supply_propval *val)
440 {
441 u8 status1, status2;
442 int ret;
443
444 ret = adp5061_get_status(st, &status1, &status2);
445 if (ret < 0)
446 return ret;
447
448 switch (ADP5061_CHG_STATUS_1_CHG_STATUS(status1)) {
449 case ADP5061_CHG_OFF:
450 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
451 break;
452 case ADP5061_CHG_TRICKLE:
453 case ADP5061_CHG_FAST_CC:
454 case ADP5061_CHG_FAST_CV:
455 val->intval = POWER_SUPPLY_STATUS_CHARGING;
456 break;
457 case ADP5061_CHG_COMPLETE:
458 val->intval = POWER_SUPPLY_STATUS_FULL;
459 break;
460 case ADP5061_CHG_TIMER_EXP:
461 /* The battery must be discharging if there is a charge fault */
462 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
463 break;
464 default:
465 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
466 }
467
468 return ret;
469 }
470
adp5061_get_battery_status(struct adp5061_state * st,union power_supply_propval * val)471 static int adp5061_get_battery_status(struct adp5061_state *st,
472 union power_supply_propval *val)
473 {
474 u8 status1, status2;
475 int ret;
476
477 ret = adp5061_get_status(st, &status1, &status2);
478 if (ret < 0)
479 return ret;
480
481 switch (ADP5061_CHG_STATUS_2_BAT_STATUS(status2)) {
482 case 0x0: /* Battery monitor off */
483 case 0x1: /* No battery */
484 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
485 break;
486 case 0x2: /* VBAT < VTRK */
487 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
488 break;
489 case 0x3: /* VTRK < VBAT_SNS < VWEAK */
490 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
491 break;
492 case 0x4: /* VBAT_SNS > VWEAK */
493 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
494 break;
495 default:
496 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
497 break;
498 }
499
500 return ret;
501 }
502
adp5061_get_termination_current(struct adp5061_state * st,union power_supply_propval * val)503 static int adp5061_get_termination_current(struct adp5061_state *st,
504 union power_supply_propval *val)
505 {
506 unsigned int regval;
507 int ret;
508
509 ret = regmap_read(st->regmap, ADP5061_IEND, ®val);
510 if (ret < 0)
511 return ret;
512
513 regval = (regval & ADP5061_IEND_IEND_MSK) >> 5;
514 val->intval = adp5061_iend[regval];
515
516 return ret;
517 }
518
adp5061_set_termination_current(struct adp5061_state * st,int val)519 static int adp5061_set_termination_current(struct adp5061_state *st, int val)
520 {
521 int index;
522
523 index = adp5061_get_array_index(adp5061_iend,
524 ARRAY_SIZE(adp5061_iend),
525 val);
526 if (index < 0)
527 return index;
528
529 return regmap_update_bits(st->regmap, ADP5061_IEND,
530 ADP5061_IEND_IEND_MSK,
531 ADP5061_IEND_IEND_MODE(index));
532 }
533
adp5061_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)534 static int adp5061_get_property(struct power_supply *psy,
535 enum power_supply_property psp,
536 union power_supply_propval *val)
537 {
538 struct adp5061_state *st = power_supply_get_drvdata(psy);
539 u8 status1, status2;
540 int mode, ret;
541
542 switch (psp) {
543 case POWER_SUPPLY_PROP_PRESENT:
544 ret = adp5061_get_status(st, &status1, &status2);
545 if (ret < 0)
546 return ret;
547
548 mode = ADP5061_CHG_STATUS_2_BAT_STATUS(status2);
549 if (mode == ADP5061_NO_BATTERY)
550 val->intval = 0;
551 else
552 val->intval = 1;
553 break;
554 case POWER_SUPPLY_PROP_CHARGE_TYPE:
555 return adp5061_get_chg_type(st, val);
556 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
557 /* This property is used to indicate the input current
558 * limit into VINx (ILIM)
559 */
560 return adp5061_get_input_current_limit(st, val);
561 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
562 /* This property is used to indicate the termination
563 * voltage (VTRM)
564 */
565 return adp5061_get_max_voltage(st, val);
566 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
567 /*
568 * This property is used to indicate the trickle to fast
569 * charge threshold (VTRK_DEAD)
570 */
571 return adp5061_get_min_voltage(st, val);
572 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
573 /* This property is used to indicate the charging
574 * voltage limit (CHG_VLIM)
575 */
576 return adp5061_get_chg_volt_lim(st, val);
577 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
578 /*
579 * This property is used to indicate the value of the constant
580 * current charge (ICHG)
581 */
582 return adp5061_get_const_chg_current(st, val);
583 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
584 /*
585 * This property is used to indicate the value of the trickle
586 * and weak charge currents (ITRK_DEAD)
587 */
588 return adp5061_get_prechg_current(st, val);
589 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
590 /*
591 * This property is used to set the VWEAK threshold
592 * below this value, weak charge mode is entered
593 * above this value, fast chargerge mode is entered
594 */
595 return adp5061_get_vweak_th(st, val);
596 case POWER_SUPPLY_PROP_STATUS:
597 /*
598 * Indicate the charger status in relation to power
599 * supply status property
600 */
601 return adp5061_get_charger_status(st, val);
602 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
603 /*
604 * Indicate the battery status in relation to power
605 * supply capacity level property
606 */
607 return adp5061_get_battery_status(st, val);
608 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
609 /* Indicate the values of the termination current */
610 return adp5061_get_termination_current(st, val);
611 default:
612 return -EINVAL;
613 }
614
615 return 0;
616 }
617
adp5061_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)618 static int adp5061_set_property(struct power_supply *psy,
619 enum power_supply_property psp,
620 const union power_supply_propval *val)
621 {
622 struct adp5061_state *st = power_supply_get_drvdata(psy);
623
624 switch (psp) {
625 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
626 return adp5061_set_input_current_limit(st, val->intval);
627 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
628 return adp5061_set_max_voltage(st, val->intval);
629 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
630 return adp5061_set_min_voltage(st, val->intval);
631 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
632 return adp5061_set_const_chg_vmax(st, val->intval);
633 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
634 return adp5061_set_const_chg_current(st, val->intval);
635 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
636 return adp5061_set_prechg_current(st, val->intval);
637 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
638 return adp5061_set_vweak_th(st, val->intval);
639 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
640 return adp5061_set_termination_current(st, val->intval);
641 default:
642 return -EINVAL;
643 }
644
645 return 0;
646 }
647
adp5061_prop_writeable(struct power_supply * psy,enum power_supply_property psp)648 static int adp5061_prop_writeable(struct power_supply *psy,
649 enum power_supply_property psp)
650 {
651 switch (psp) {
652 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
653 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
654 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
655 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
656 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
657 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
658 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
659 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
660 return 1;
661 default:
662 return 0;
663 }
664 }
665
666 static enum power_supply_property adp5061_props[] = {
667 POWER_SUPPLY_PROP_PRESENT,
668 POWER_SUPPLY_PROP_CHARGE_TYPE,
669 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
670 POWER_SUPPLY_PROP_VOLTAGE_MAX,
671 POWER_SUPPLY_PROP_VOLTAGE_MIN,
672 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
673 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
674 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
675 POWER_SUPPLY_PROP_VOLTAGE_AVG,
676 POWER_SUPPLY_PROP_STATUS,
677 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
678 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
679 };
680
681 static const struct regmap_config adp5061_regmap_config = {
682 .reg_bits = 8,
683 .val_bits = 8,
684 };
685
686 static const struct power_supply_desc adp5061_desc = {
687 .name = "adp5061",
688 .type = POWER_SUPPLY_TYPE_USB,
689 .get_property = adp5061_get_property,
690 .set_property = adp5061_set_property,
691 .property_is_writeable = adp5061_prop_writeable,
692 .properties = adp5061_props,
693 .num_properties = ARRAY_SIZE(adp5061_props),
694 };
695
adp5061_probe(struct i2c_client * client)696 static int adp5061_probe(struct i2c_client *client)
697 {
698 struct power_supply_config psy_cfg = {};
699 struct adp5061_state *st;
700
701 st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
702 if (!st)
703 return -ENOMEM;
704
705 st->client = client;
706 st->regmap = devm_regmap_init_i2c(client,
707 &adp5061_regmap_config);
708 if (IS_ERR(st->regmap)) {
709 dev_err(&client->dev, "Failed to initialize register map\n");
710 return -EINVAL;
711 }
712
713 i2c_set_clientdata(client, st);
714 psy_cfg.drv_data = st;
715
716 st->psy = devm_power_supply_register(&client->dev,
717 &adp5061_desc,
718 &psy_cfg);
719
720 if (IS_ERR(st->psy)) {
721 dev_err(&client->dev, "Failed to register power supply\n");
722 return PTR_ERR(st->psy);
723 }
724
725 return 0;
726 }
727
728 static const struct i2c_device_id adp5061_id[] = {
729 { .name = "adp5061" },
730 { }
731 };
732 MODULE_DEVICE_TABLE(i2c, adp5061_id);
733
734 static struct i2c_driver adp5061_driver = {
735 .driver = {
736 .name = KBUILD_MODNAME,
737 },
738 .probe = adp5061_probe,
739 .id_table = adp5061_id,
740 };
741 module_i2c_driver(adp5061_driver);
742
743 MODULE_DESCRIPTION("Analog Devices adp5061 battery charger driver");
744 MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
745 MODULE_LICENSE("GPL v2");
746