Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / power / supply / bd70528-charger.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 //
3 // Copyright (C) 2018 ROHM Semiconductors
4 //
5 // power-supply driver for ROHM BD70528 PMIC
6
7 /*
8  * BD70528 charger HW state machine.
9  *
10  * The thermal shutdown state is not drawn. From any other state but
11  * battery error and suspend it is possible to go to TSD/TMP states
12  * if temperature is out of bounds.
13  *
14  *  CHG_RST = H
15  *  or CHG_EN=L
16  *  or (DCIN2_UVLO=L && DCIN1_UVLO=L)
17  *  or (DCIN2_OVLO=H & DCIN1_UVKLO=L)
18  *
19  *  +--------------+         +--------------+
20  *  |              |         |              |
21  *  |  Any state   +-------> |    Suspend   |
22  *  |              |         |              |
23  *  +--------------+         +------+-------+
24  *                                  |
25  *  CHG_EN = H && BAT_DET = H &&    |
26  *  No errors (temp, bat_ov, UVLO,  |
27  *  OVLO...)                        |
28  *                                  |
29  *  BAT_OV or             +---------v----------+
30  *  (DBAT && TTRI)        |                    |
31  *      +-----------------+   Trickle Charge   | <---------------+
32  *      |                 |                    |                 |
33  *      |                 +-------+------------+                 |
34  *      |                         |                              |
35  *      |                         |     ^                        |
36  *      |        V_BAT > VTRI_TH  |     |  VBAT < VTRI_TH - 50mV |
37  *      |                         |     |                        |
38  *      |                         v     |                        |
39  *      |                               |                        |
40  *      |     BAT_OV or      +----------+----+                   |
41  *      |     (DBAT && TFST) |               |                   |
42  *      |   +----------------+  Fast Charge  |                   |
43  *      |   |                |               |                   |
44  *      v   v                +----+----------+                   |
45  *                                |                              |
46  *+----------------+   ILIM_DET=L |    ^ ILIM_DET                |
47  *|                |   & CV_DET=H |    | or CV_DET=L             |
48  *|  Battery Error |   & VBAT >   |    | or VBAT < VRECHG_TH     |
49  *|                |   VRECHG_TH  |    | or IBAT  > IFST/x       |
50  *+----------------+   & IBAT <   |    |                         |
51  *                     IFST/x     v    |                         |
52  *       ^                             |                         |
53  *       |                   +---------+-+                       |
54  *       |                   |           |                       |
55  *       +-------------------+  Top OFF  |                       |
56  *  BAT_OV = H or            |           |                       |
57  *  (DBAT && TFST)           +-----+-----+                       |
58  *                                 |                             |
59  *           Stay top-off for 15s  |                             |
60  *                                 v                             |
61  *                                                               |
62  *                            +--------+                         |
63  *                            |        |                         |
64  *                            |  Done  +-------------------------+
65  *                            |        |
66  *                            +--------+   VBAT < VRECHG_TH
67  */
68
69 #include <linux/kernel.h>
70 #include <linux/interrupt.h>
71 #include <linux/mfd/rohm-bd70528.h>
72 #include <linux/module.h>
73 #include <linux/platform_device.h>
74 #include <linux/power_supply.h>
75
76 #define CHG_STAT_SUSPEND        0x0
77 #define CHG_STAT_TRICKLE        0x1
78 #define CHG_STAT_FAST           0x3
79 #define CHG_STAT_TOPOFF         0xe
80 #define CHG_STAT_DONE           0xf
81 #define CHG_STAT_OTP_TRICKLE    0x10
82 #define CHG_STAT_OTP_FAST       0x11
83 #define CHG_STAT_OTP_DONE       0x12
84 #define CHG_STAT_TSD_TRICKLE    0x20
85 #define CHG_STAT_TSD_FAST       0x21
86 #define CHG_STAT_TSD_TOPOFF     0x22
87 #define CHG_STAT_BAT_ERR        0x7f
88
89 static const char *bd70528_charger_model = "BD70528";
90 static const char *bd70528_charger_manufacturer = "ROHM Semiconductors";
91
92 #define BD_ERR_IRQ_HND(_name_, _wrn_)                                   \
93 static irqreturn_t bd0528_##_name_##_interrupt(int irq, void *arg)      \
94 {                                                                       \
95         struct power_supply *psy = (struct power_supply *)arg;          \
96                                                                         \
97         power_supply_changed(psy);                                      \
98         dev_err(&psy->dev, (_wrn_));                                    \
99                                                                         \
100         return IRQ_HANDLED;                                             \
101 }
102
103 #define BD_INFO_IRQ_HND(_name_, _wrn_)                                  \
104 static irqreturn_t bd0528_##_name_##_interrupt(int irq, void *arg)      \
105 {                                                                       \
106         struct power_supply *psy = (struct power_supply *)arg;          \
107                                                                         \
108         power_supply_changed(psy);                                      \
109         dev_dbg(&psy->dev, (_wrn_));                                    \
110                                                                         \
111         return IRQ_HANDLED;                                             \
112 }
113
114 #define BD_IRQ_HND(_name_) bd0528_##_name_##_interrupt
115
116 struct bd70528_psy {
117         struct regmap *regmap;
118         struct device *dev;
119         struct power_supply *psy;
120 };
121
122 BD_ERR_IRQ_HND(BAT_OV_DET, "Battery overvoltage detected\n");
123 BD_ERR_IRQ_HND(DBAT_DET, "Dead battery detected\n");
124 BD_ERR_IRQ_HND(COLD_DET, "Battery cold\n");
125 BD_ERR_IRQ_HND(HOT_DET, "Battery hot\n");
126 BD_ERR_IRQ_HND(CHG_TSD, "Charger thermal shutdown\n");
127 BD_ERR_IRQ_HND(DCIN2_OV_DET, "DCIN2 overvoltage detected\n");
128
129 BD_INFO_IRQ_HND(BAT_OV_RES, "Battery voltage back to normal\n");
130 BD_INFO_IRQ_HND(COLD_RES, "Battery temperature back to normal\n");
131 BD_INFO_IRQ_HND(HOT_RES, "Battery temperature back to normal\n");
132 BD_INFO_IRQ_HND(BAT_RMV, "Battery removed\n");
133 BD_INFO_IRQ_HND(BAT_DET, "Battery detected\n");
134 BD_INFO_IRQ_HND(DCIN2_OV_RES, "DCIN2 voltage back to normal\n");
135 BD_INFO_IRQ_HND(DCIN2_RMV, "DCIN2 removed\n");
136 BD_INFO_IRQ_HND(DCIN2_DET, "DCIN2 detected\n");
137 BD_INFO_IRQ_HND(DCIN1_RMV, "DCIN1 removed\n");
138 BD_INFO_IRQ_HND(DCIN1_DET, "DCIN1 detected\n");
139
140 struct irq_name_pair {
141         const char *n;
142         irqreturn_t (*h)(int irq, void *arg);
143 };
144
145 static int bd70528_get_irqs(struct platform_device *pdev,
146                             struct bd70528_psy *bdpsy)
147 {
148         int irq, i, ret;
149         unsigned int mask;
150         static const struct irq_name_pair bd70528_chg_irqs[] = {
151                 { .n = "bd70528-bat-ov-res", .h = BD_IRQ_HND(BAT_OV_RES) },
152                 { .n = "bd70528-bat-ov-det", .h = BD_IRQ_HND(BAT_OV_DET) },
153                 { .n = "bd70528-bat-dead", .h = BD_IRQ_HND(DBAT_DET) },
154                 { .n = "bd70528-bat-warmed", .h = BD_IRQ_HND(COLD_RES) },
155                 { .n = "bd70528-bat-cold", .h = BD_IRQ_HND(COLD_DET) },
156                 { .n = "bd70528-bat-cooled", .h = BD_IRQ_HND(HOT_RES) },
157                 { .n = "bd70528-bat-hot", .h = BD_IRQ_HND(HOT_DET) },
158                 { .n = "bd70528-chg-tshd", .h = BD_IRQ_HND(CHG_TSD) },
159                 { .n = "bd70528-bat-removed", .h = BD_IRQ_HND(BAT_RMV) },
160                 { .n = "bd70528-bat-detected", .h = BD_IRQ_HND(BAT_DET) },
161                 { .n = "bd70528-dcin2-ov-res", .h = BD_IRQ_HND(DCIN2_OV_RES) },
162                 { .n = "bd70528-dcin2-ov-det", .h = BD_IRQ_HND(DCIN2_OV_DET) },
163                 { .n = "bd70528-dcin2-removed", .h = BD_IRQ_HND(DCIN2_RMV) },
164                 { .n = "bd70528-dcin2-detected", .h = BD_IRQ_HND(DCIN2_DET) },
165                 { .n = "bd70528-dcin1-removed", .h = BD_IRQ_HND(DCIN1_RMV) },
166                 { .n = "bd70528-dcin1-detected", .h = BD_IRQ_HND(DCIN1_DET) },
167         };
168
169         for (i = 0; i < ARRAY_SIZE(bd70528_chg_irqs); i++) {
170                 irq = platform_get_irq_byname(pdev, bd70528_chg_irqs[i].n);
171                 if (irq < 0) {
172                         dev_err(&pdev->dev, "Bad IRQ information for %s (%d)\n",
173                                 bd70528_chg_irqs[i].n, irq);
174                         return irq;
175                 }
176                 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
177                                                 bd70528_chg_irqs[i].h,
178                                                 IRQF_ONESHOT,
179                                                 bd70528_chg_irqs[i].n,
180                                                 bdpsy->psy);
181
182                 if (ret)
183                         return ret;
184         }
185         /*
186          * BD70528 irq controller is not touching the main mask register.
187          * So enable the charger block interrupts at main level. We can just
188          * leave them enabled as irq-controller should disable irqs
189          * from sub-registers when IRQ is disabled or freed.
190          */
191         mask = BD70528_REG_INT_BAT1_MASK | BD70528_REG_INT_BAT2_MASK;
192         ret = regmap_update_bits(bdpsy->regmap,
193                                  BD70528_REG_INT_MAIN_MASK, mask, 0);
194         if (ret)
195                 dev_err(&pdev->dev, "Failed to enable charger IRQs\n");
196
197         return ret;
198 }
199
200 static int bd70528_get_charger_status(struct bd70528_psy *bdpsy, int *val)
201 {
202         int ret;
203         unsigned int v;
204
205         ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_CURR_STAT, &v);
206         if (ret) {
207                 dev_err(bdpsy->dev, "Charger state read failure %d\n",
208                         ret);
209                 return ret;
210         }
211
212         switch (v & BD70528_MASK_CHG_STAT) {
213         case CHG_STAT_SUSPEND:
214         /* Maybe we should check the CHG_TTRI_EN? */
215         case CHG_STAT_OTP_TRICKLE:
216         case CHG_STAT_OTP_FAST:
217         case CHG_STAT_OTP_DONE:
218         case CHG_STAT_TSD_TRICKLE:
219         case CHG_STAT_TSD_FAST:
220         case CHG_STAT_TSD_TOPOFF:
221         case CHG_STAT_BAT_ERR:
222                 *val = POWER_SUPPLY_STATUS_NOT_CHARGING;
223                 break;
224         case CHG_STAT_DONE:
225                 *val = POWER_SUPPLY_STATUS_FULL;
226                 break;
227         case CHG_STAT_TRICKLE:
228         case CHG_STAT_FAST:
229         case CHG_STAT_TOPOFF:
230                 *val = POWER_SUPPLY_STATUS_CHARGING;
231                 break;
232         default:
233                 *val = POWER_SUPPLY_STATUS_UNKNOWN;
234                 break;
235         }
236
237         return 0;
238 }
239
240 static int bd70528_get_charge_type(struct bd70528_psy *bdpsy, int *val)
241 {
242         int ret;
243         unsigned int v;
244
245         ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_CURR_STAT, &v);
246         if (ret) {
247                 dev_err(bdpsy->dev, "Charger state read failure %d\n",
248                         ret);
249                 return ret;
250         }
251
252         switch (v & BD70528_MASK_CHG_STAT) {
253         case CHG_STAT_TRICKLE:
254                 *val = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
255                 break;
256         case CHG_STAT_FAST:
257         case CHG_STAT_TOPOFF:
258                 *val = POWER_SUPPLY_CHARGE_TYPE_FAST;
259                 break;
260         case CHG_STAT_DONE:
261         case CHG_STAT_SUSPEND:
262         /* Maybe we should check the CHG_TTRI_EN? */
263         case CHG_STAT_OTP_TRICKLE:
264         case CHG_STAT_OTP_FAST:
265         case CHG_STAT_OTP_DONE:
266         case CHG_STAT_TSD_TRICKLE:
267         case CHG_STAT_TSD_FAST:
268         case CHG_STAT_TSD_TOPOFF:
269         case CHG_STAT_BAT_ERR:
270                 *val = POWER_SUPPLY_CHARGE_TYPE_NONE;
271                 break;
272         default:
273                 *val = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
274                 break;
275         }
276
277         return 0;
278 }
279
280 static int bd70528_get_battery_health(struct bd70528_psy *bdpsy, int *val)
281 {
282         int ret;
283         unsigned int v;
284
285         ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_BAT_STAT, &v);
286         if (ret) {
287                 dev_err(bdpsy->dev, "Battery state read failure %d\n",
288                         ret);
289                 return ret;
290         }
291         /* No battery? */
292         if (!(v & BD70528_MASK_CHG_BAT_DETECT))
293                 *val = POWER_SUPPLY_HEALTH_DEAD;
294         else if (v & BD70528_MASK_CHG_BAT_OVERVOLT)
295                 *val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
296         else if (v & BD70528_MASK_CHG_BAT_TIMER)
297                 *val = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
298         else
299                 *val = POWER_SUPPLY_HEALTH_GOOD;
300
301         return 0;
302 }
303
304 static int bd70528_get_online(struct bd70528_psy *bdpsy, int *val)
305 {
306         int ret;
307         unsigned int v;
308
309         ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_IN_STAT, &v);
310         if (ret) {
311                 dev_err(bdpsy->dev, "DC1 IN state read failure %d\n",
312                         ret);
313                 return ret;
314         }
315
316         *val = (v & BD70528_MASK_CHG_DCIN1_UVLO) ? 1 : 0;
317
318         return 0;
319 }
320
321 static int bd70528_get_present(struct bd70528_psy *bdpsy, int *val)
322 {
323         int ret;
324         unsigned int v;
325
326         ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_BAT_STAT, &v);
327         if (ret) {
328                 dev_err(bdpsy->dev, "Battery state read failure %d\n",
329                         ret);
330                 return ret;
331         }
332
333         *val = (v & BD70528_MASK_CHG_BAT_DETECT) ? 1 : 0;
334
335         return 0;
336 }
337
338 struct linear_range {
339         int min;
340         int step;
341         int vals;
342         int low_sel;
343 };
344
345 static const struct linear_range current_limit_ranges[] = {
346         {
347                 .min = 5,
348                 .step = 1,
349                 .vals = 36,
350                 .low_sel = 0,
351         },
352         {
353                 .min = 40,
354                 .step = 5,
355                 .vals = 5,
356                 .low_sel = 0x23,
357         },
358         {
359                 .min = 60,
360                 .step = 20,
361                 .vals = 8,
362                 .low_sel = 0x27,
363         },
364         {
365                 .min = 200,
366                 .step = 50,
367                 .vals = 7,
368                 .low_sel = 0x2e,
369         }
370 };
371
372 /*
373  * BD70528 would support setting and getting own charge current/
374  * voltage for low temperatures. The driver currently only reads
375  * the charge current at room temperature. We do set both though.
376  */
377 static const struct linear_range warm_charge_curr[] = {
378         {
379                 .min = 10,
380                 .step = 10,
381                 .vals = 20,
382                 .low_sel = 0,
383         },
384         {
385                 .min = 200,
386                 .step = 25,
387                 .vals = 13,
388                 .low_sel = 0x13,
389         },
390 };
391
392 /*
393  * Cold charge current selectors are identical to warm charge current
394  * selectors. The difference is that only smaller currents are available
395  * at cold charge range.
396  */
397 #define MAX_COLD_CHG_CURR_SEL 0x15
398 #define MAX_WARM_CHG_CURR_SEL 0x1f
399 #define MIN_CHG_CURR_SEL 0x0
400
401 static int find_value_for_selector_low(const struct linear_range *r,
402                                        int selectors, unsigned int sel,
403                                        unsigned int *val)
404 {
405         int i;
406
407         for (i = 0; i < selectors; i++) {
408                 if (r[i].low_sel <= sel && r[i].low_sel + r[i].vals >= sel) {
409                         *val = r[i].min + (sel - r[i].low_sel) * r[i].step;
410                         return 0;
411                 }
412         }
413         return -EINVAL;
414 }
415
416 /*
417  * For BD70528 voltage/current limits we happily accept any value which
418  * belongs the range. We could check if value matching the selector is
419  * desired by computing the range min + (sel - sel_low) * range step - but
420  * I guess it is enough if we use voltage/current which is closest (below)
421  * the requested?
422  */
423 static int find_selector_for_value_low(const struct linear_range *r,
424                                        int selectors, unsigned int val,
425                                        unsigned int *sel, bool *found)
426 {
427         int i;
428         int ret = -EINVAL;
429
430         *found = false;
431         for (i = 0; i < selectors; i++) {
432                 if (r[i].min <= val) {
433                         if (r[i].min + r[i].step * r[i].vals >= val) {
434                                 *found = true;
435                                 *sel = r[i].low_sel + (val - r[i].min) /
436                                        r[i].step;
437                                 ret = 0;
438                                 break;
439                         }
440                         /*
441                          * If the range max is smaller than requested
442                          * we can set the max supported value from range
443                          */
444                         *sel = r[i].low_sel + r[i].vals;
445                         ret = 0;
446                 }
447         }
448         return ret;
449 }
450
451 static int get_charge_current(struct bd70528_psy *bdpsy, int *ma)
452 {
453         unsigned int sel;
454         int ret;
455
456         ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_CHG_CURR_WARM,
457                           &sel);
458         if (ret) {
459                 dev_err(bdpsy->dev,
460                         "Charge current reading failed (%d)\n", ret);
461                 return ret;
462         }
463
464         sel &= BD70528_MASK_CHG_CHG_CURR;
465
466         ret = find_value_for_selector_low(&warm_charge_curr[0],
467                                           ARRAY_SIZE(warm_charge_curr), sel,
468                                           ma);
469         if (ret) {
470                 dev_err(bdpsy->dev,
471                         "Unknown charge current value 0x%x\n",
472                         sel);
473         }
474
475         return ret;
476 }
477
478 static int get_current_limit(struct bd70528_psy *bdpsy, int *ma)
479 {
480         unsigned int sel;
481         int ret;
482
483         ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_DCIN_ILIM,
484                           &sel);
485
486         if (ret) {
487                 dev_err(bdpsy->dev,
488                         "Input current limit reading failed (%d)\n", ret);
489                 return ret;
490         }
491
492         sel &= BD70528_MASK_CHG_DCIN_ILIM;
493
494         ret = find_value_for_selector_low(&current_limit_ranges[0],
495                                           ARRAY_SIZE(current_limit_ranges), sel,
496                                           ma);
497
498         if (ret) {
499                 /* Unspecified values mean 500 mA */
500                 *ma = 500;
501         }
502         return 0;
503 }
504
505 static enum power_supply_property bd70528_charger_props[] = {
506         POWER_SUPPLY_PROP_STATUS,
507         POWER_SUPPLY_PROP_CHARGE_TYPE,
508         POWER_SUPPLY_PROP_HEALTH,
509         POWER_SUPPLY_PROP_PRESENT,
510         POWER_SUPPLY_PROP_ONLINE,
511         POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
512         POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
513         POWER_SUPPLY_PROP_MODEL_NAME,
514         POWER_SUPPLY_PROP_MANUFACTURER,
515 };
516
517 static int bd70528_charger_get_property(struct power_supply *psy,
518                                         enum power_supply_property psp,
519                                         union power_supply_propval *val)
520 {
521         struct bd70528_psy *bdpsy = power_supply_get_drvdata(psy);
522         int ret = 0;
523
524         switch (psp) {
525         case POWER_SUPPLY_PROP_STATUS:
526                 return bd70528_get_charger_status(bdpsy, &val->intval);
527         case POWER_SUPPLY_PROP_CHARGE_TYPE:
528                 return bd70528_get_charge_type(bdpsy, &val->intval);
529         case POWER_SUPPLY_PROP_HEALTH:
530                 return bd70528_get_battery_health(bdpsy, &val->intval);
531         case POWER_SUPPLY_PROP_PRESENT:
532                 return bd70528_get_present(bdpsy, &val->intval);
533         case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
534                 ret = get_current_limit(bdpsy, &val->intval);
535                 val->intval *= 1000;
536                 return ret;
537         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
538                 ret = get_charge_current(bdpsy, &val->intval);
539                 val->intval *= 1000;
540                 return ret;
541         case POWER_SUPPLY_PROP_ONLINE:
542                 return bd70528_get_online(bdpsy, &val->intval);
543         case POWER_SUPPLY_PROP_MODEL_NAME:
544                 val->strval = bd70528_charger_model;
545                 return 0;
546         case POWER_SUPPLY_PROP_MANUFACTURER:
547                 val->strval = bd70528_charger_manufacturer;
548                 return 0;
549         default:
550                 break;
551         }
552
553         return -EINVAL;
554 }
555
556 static int bd70528_prop_is_writable(struct power_supply *psy,
557                                     enum power_supply_property psp)
558 {
559         switch (psp) {
560         case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
561         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
562                 return 1;
563         default:
564                 break;
565         }
566         return 0;
567 }
568
569 static int set_charge_current(struct bd70528_psy *bdpsy, int ma)
570 {
571         unsigned int reg;
572         int ret = 0, tmpret;
573         bool found;
574
575         if (ma > 500) {
576                 dev_warn(bdpsy->dev,
577                          "Requested charge current %u exceed maximum (500mA)\n",
578                          ma);
579                 reg = MAX_WARM_CHG_CURR_SEL;
580                 goto set;
581         }
582         if (ma < 10) {
583                 dev_err(bdpsy->dev,
584                         "Requested charge current %u smaller than min (10mA)\n",
585                          ma);
586                 reg = MIN_CHG_CURR_SEL;
587                 ret = -EINVAL;
588                 goto set;
589         }
590
591         ret = find_selector_for_value_low(&warm_charge_curr[0],
592                                           ARRAY_SIZE(warm_charge_curr), ma,
593                                           &reg, &found);
594         if (ret) {
595                 reg = MIN_CHG_CURR_SEL;
596                 goto set;
597         }
598         if (!found) {
599                 /* There was a gap in supported values and we hit it */
600                 dev_warn(bdpsy->dev,
601                          "Unsupported charge current %u mA\n", ma);
602         }
603 set:
604
605         tmpret = regmap_update_bits(bdpsy->regmap,
606                                     BD70528_REG_CHG_CHG_CURR_WARM,
607                                     BD70528_MASK_CHG_CHG_CURR, reg);
608         if (tmpret)
609                 dev_err(bdpsy->dev,
610                         "Charge current write failure (%d)\n", tmpret);
611
612         if (reg > MAX_COLD_CHG_CURR_SEL)
613                 reg = MAX_COLD_CHG_CURR_SEL;
614
615         if (!tmpret)
616                 tmpret = regmap_update_bits(bdpsy->regmap,
617                                             BD70528_REG_CHG_CHG_CURR_COLD,
618                                             BD70528_MASK_CHG_CHG_CURR, reg);
619
620         if (!ret)
621                 ret = tmpret;
622
623         return ret;
624 }
625
626 #define MAX_CURR_LIMIT_SEL 0x34
627 #define MIN_CURR_LIMIT_SEL 0x0
628
629 static int set_current_limit(struct bd70528_psy *bdpsy, int ma)
630 {
631         unsigned int reg;
632         int ret = 0, tmpret;
633         bool found;
634
635         if (ma > 500) {
636                 dev_warn(bdpsy->dev,
637                          "Requested current limit %u exceed maximum (500mA)\n",
638                          ma);
639                 reg = MAX_CURR_LIMIT_SEL;
640                 goto set;
641         }
642         if (ma < 5) {
643                 dev_err(bdpsy->dev,
644                         "Requested current limit %u smaller than min (5mA)\n",
645                         ma);
646                 reg = MIN_CURR_LIMIT_SEL;
647                 ret = -EINVAL;
648                 goto set;
649         }
650
651         ret = find_selector_for_value_low(&current_limit_ranges[0],
652                                           ARRAY_SIZE(current_limit_ranges), ma,
653                                           &reg, &found);
654         if (ret) {
655                 reg = MIN_CURR_LIMIT_SEL;
656                 goto set;
657         }
658         if (!found) {
659                 /* There was a gap in supported values and we hit it ?*/
660                 dev_warn(bdpsy->dev, "Unsupported current limit %umA\n",
661                          ma);
662         }
663
664 set:
665         tmpret = regmap_update_bits(bdpsy->regmap,
666                                     BD70528_REG_CHG_DCIN_ILIM,
667                                     BD70528_MASK_CHG_DCIN_ILIM, reg);
668
669         if (!ret)
670                 ret = tmpret;
671
672         return ret;
673 }
674
675 static int bd70528_charger_set_property(struct power_supply *psy,
676                                         enum power_supply_property psp,
677                                         const union power_supply_propval *val)
678 {
679         struct bd70528_psy *bdpsy = power_supply_get_drvdata(psy);
680
681         switch (psp) {
682         case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
683                 return set_current_limit(bdpsy, val->intval / 1000);
684         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
685                 return set_charge_current(bdpsy, val->intval / 1000);
686         default:
687                 break;
688         }
689         return -EINVAL;
690 }
691
692 static const struct power_supply_desc bd70528_charger_desc = {
693         .name           = "bd70528-charger",
694         .type           = POWER_SUPPLY_TYPE_MAINS,
695         .properties     = bd70528_charger_props,
696         .num_properties = ARRAY_SIZE(bd70528_charger_props),
697         .get_property   = bd70528_charger_get_property,
698         .set_property   = bd70528_charger_set_property,
699         .property_is_writeable  = bd70528_prop_is_writable,
700 };
701
702 static int bd70528_power_probe(struct platform_device *pdev)
703 {
704         struct bd70528_psy *bdpsy;
705         struct power_supply_config cfg = {};
706
707         bdpsy = devm_kzalloc(&pdev->dev, sizeof(*bdpsy), GFP_KERNEL);
708         if (!bdpsy)
709                 return -ENOMEM;
710
711         bdpsy->regmap = dev_get_regmap(pdev->dev.parent, NULL);
712         if (!bdpsy->regmap) {
713                 dev_err(&pdev->dev, "No regmap found for chip\n");
714                 return -EINVAL;
715         }
716         bdpsy->dev = &pdev->dev;
717
718         platform_set_drvdata(pdev, bdpsy);
719         cfg.drv_data = bdpsy;
720         cfg.of_node = pdev->dev.parent->of_node;
721
722         bdpsy->psy = devm_power_supply_register(&pdev->dev,
723                                                 &bd70528_charger_desc, &cfg);
724         if (IS_ERR(bdpsy->psy)) {
725                 dev_err(&pdev->dev, "failed: power supply register\n");
726                 return PTR_ERR(bdpsy->psy);
727         }
728
729         return bd70528_get_irqs(pdev, bdpsy);
730 }
731
732 static struct platform_driver bd70528_power = {
733         .driver = {
734                 .name = "bd70528-power"
735         },
736         .probe = bd70528_power_probe,
737 };
738
739 module_platform_driver(bd70528_power);
740
741 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
742 MODULE_DESCRIPTION("BD70528 power-supply driver");
743 MODULE_LICENSE("GPL");
744 MODULE_ALIAS("platform:bd70528-power");