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