Linux 4.19-rc7
[linux-2.6/btrfs-unstable.git] / drivers / regulator / da9062-regulator.c
blob34a70d9dc450e118f86b1e1ca8fcd954632775c0
1 /*
2 * Regulator device driver for DA9061 and DA9062.
3 * Copyright (C) 2015-2017 Dialog Semiconductor
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/of.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/regulator/machine.h>
25 #include <linux/regulator/of_regulator.h>
26 #include <linux/mfd/da9062/core.h>
27 #include <linux/mfd/da9062/registers.h>
29 /* Regulator IDs */
30 enum {
31 DA9061_ID_BUCK1,
32 DA9061_ID_BUCK2,
33 DA9061_ID_BUCK3,
34 DA9061_ID_LDO1,
35 DA9061_ID_LDO2,
36 DA9061_ID_LDO3,
37 DA9061_ID_LDO4,
38 DA9061_MAX_REGULATORS,
41 enum {
42 DA9062_ID_BUCK1,
43 DA9062_ID_BUCK2,
44 DA9062_ID_BUCK3,
45 DA9062_ID_BUCK4,
46 DA9062_ID_LDO1,
47 DA9062_ID_LDO2,
48 DA9062_ID_LDO3,
49 DA9062_ID_LDO4,
50 DA9062_MAX_REGULATORS,
53 /* Regulator capabilities and registers description */
54 struct da9062_regulator_info {
55 struct regulator_desc desc;
56 /* Current limiting */
57 unsigned int n_current_limits;
58 const int *current_limits;
59 /* Main register fields */
60 struct reg_field mode;
61 struct reg_field suspend;
62 struct reg_field sleep;
63 struct reg_field suspend_sleep;
64 unsigned int suspend_vsel_reg;
65 struct reg_field ilimit;
66 /* Event detection bit */
67 struct reg_field oc_event;
70 /* Single regulator settings */
71 struct da9062_regulator {
72 struct regulator_desc desc;
73 struct regulator_dev *rdev;
74 struct da9062 *hw;
75 const struct da9062_regulator_info *info;
77 struct regmap_field *mode;
78 struct regmap_field *suspend;
79 struct regmap_field *sleep;
80 struct regmap_field *suspend_sleep;
81 struct regmap_field *ilimit;
84 /* Encapsulates all information for the regulators driver */
85 struct da9062_regulators {
86 int irq_ldo_lim;
87 unsigned n_regulators;
88 /* Array size to be defined during init. Keep at end. */
89 struct da9062_regulator regulator[0];
92 /* BUCK modes */
93 enum {
94 BUCK_MODE_MANUAL, /* 0 */
95 BUCK_MODE_SLEEP, /* 1 */
96 BUCK_MODE_SYNC, /* 2 */
97 BUCK_MODE_AUTO /* 3 */
100 /* Regulator operations */
102 /* Current limits array (in uA)
103 * - DA9061_ID_[BUCK1|BUCK3]
104 * - DA9062_ID_[BUCK1|BUCK2|BUCK4]
105 * Entry indexes corresponds to register values.
107 static const int da9062_buck_a_limits[] = {
108 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
109 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
112 /* Current limits array (in uA)
113 * - DA9061_ID_BUCK2
114 * - DA9062_ID_BUCK3
115 * Entry indexes corresponds to register values.
117 static const int da9062_buck_b_limits[] = {
118 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
119 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
122 static int da9062_set_current_limit(struct regulator_dev *rdev,
123 int min_ua, int max_ua)
125 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
126 const struct da9062_regulator_info *rinfo = regl->info;
127 int n, tval;
129 for (n = 0; n < rinfo->n_current_limits; n++) {
130 tval = rinfo->current_limits[n];
131 if (tval >= min_ua && tval <= max_ua)
132 return regmap_field_write(regl->ilimit, n);
135 return -EINVAL;
138 static int da9062_get_current_limit(struct regulator_dev *rdev)
140 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
141 const struct da9062_regulator_info *rinfo = regl->info;
142 unsigned int sel;
143 int ret;
145 ret = regmap_field_read(regl->ilimit, &sel);
146 if (ret < 0)
147 return ret;
149 if (sel >= rinfo->n_current_limits)
150 sel = rinfo->n_current_limits - 1;
152 return rinfo->current_limits[sel];
155 static int da9062_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
157 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
158 unsigned val;
160 switch (mode) {
161 case REGULATOR_MODE_FAST:
162 val = BUCK_MODE_SYNC;
163 break;
164 case REGULATOR_MODE_NORMAL:
165 val = BUCK_MODE_AUTO;
166 break;
167 case REGULATOR_MODE_STANDBY:
168 val = BUCK_MODE_SLEEP;
169 break;
170 default:
171 return -EINVAL;
174 return regmap_field_write(regl->mode, val);
178 * Bucks use single mode register field for normal operation
179 * and suspend state.
180 * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
183 static unsigned da9062_buck_get_mode(struct regulator_dev *rdev)
185 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
186 struct regmap_field *field;
187 unsigned int val, mode = 0;
188 int ret;
190 ret = regmap_field_read(regl->mode, &val);
191 if (ret < 0)
192 return ret;
194 switch (val) {
195 default:
196 case BUCK_MODE_MANUAL:
197 mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
198 /* Sleep flag bit decides the mode */
199 break;
200 case BUCK_MODE_SLEEP:
201 return REGULATOR_MODE_STANDBY;
202 case BUCK_MODE_SYNC:
203 return REGULATOR_MODE_FAST;
204 case BUCK_MODE_AUTO:
205 return REGULATOR_MODE_NORMAL;
208 /* Detect current regulator state */
209 ret = regmap_field_read(regl->suspend, &val);
210 if (ret < 0)
211 return 0;
213 /* Read regulator mode from proper register, depending on state */
214 if (val)
215 field = regl->suspend_sleep;
216 else
217 field = regl->sleep;
219 ret = regmap_field_read(field, &val);
220 if (ret < 0)
221 return 0;
223 if (val)
224 mode &= REGULATOR_MODE_STANDBY;
225 else
226 mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
228 return mode;
232 * LDOs use sleep flags - one for normal and one for suspend state.
233 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
236 static int da9062_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
238 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
239 unsigned val;
241 switch (mode) {
242 case REGULATOR_MODE_NORMAL:
243 val = 0;
244 break;
245 case REGULATOR_MODE_STANDBY:
246 val = 1;
247 break;
248 default:
249 return -EINVAL;
252 return regmap_field_write(regl->sleep, val);
255 static unsigned da9062_ldo_get_mode(struct regulator_dev *rdev)
257 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
258 struct regmap_field *field;
259 int ret, val;
261 /* Detect current regulator state */
262 ret = regmap_field_read(regl->suspend, &val);
263 if (ret < 0)
264 return 0;
266 /* Read regulator mode from proper register, depending on state */
267 if (val)
268 field = regl->suspend_sleep;
269 else
270 field = regl->sleep;
272 ret = regmap_field_read(field, &val);
273 if (ret < 0)
274 return 0;
276 if (val)
277 return REGULATOR_MODE_STANDBY;
278 else
279 return REGULATOR_MODE_NORMAL;
282 static int da9062_buck_get_status(struct regulator_dev *rdev)
284 int ret = regulator_is_enabled_regmap(rdev);
286 if (ret == 0) {
287 ret = REGULATOR_STATUS_OFF;
288 } else if (ret > 0) {
289 ret = da9062_buck_get_mode(rdev);
290 if (ret > 0)
291 ret = regulator_mode_to_status(ret);
292 else if (ret == 0)
293 ret = -EIO;
296 return ret;
299 static int da9062_ldo_get_status(struct regulator_dev *rdev)
301 int ret = regulator_is_enabled_regmap(rdev);
303 if (ret == 0) {
304 ret = REGULATOR_STATUS_OFF;
305 } else if (ret > 0) {
306 ret = da9062_ldo_get_mode(rdev);
307 if (ret > 0)
308 ret = regulator_mode_to_status(ret);
309 else if (ret == 0)
310 ret = -EIO;
313 return ret;
316 static int da9062_set_suspend_voltage(struct regulator_dev *rdev, int uv)
318 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
319 const struct da9062_regulator_info *rinfo = regl->info;
320 int ret, sel;
322 sel = regulator_map_voltage_linear(rdev, uv, uv);
323 if (sel < 0)
324 return sel;
326 sel <<= ffs(rdev->desc->vsel_mask) - 1;
328 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
329 rdev->desc->vsel_mask, sel);
331 return ret;
334 static int da9062_suspend_enable(struct regulator_dev *rdev)
336 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
338 return regmap_field_write(regl->suspend, 1);
341 static int da9062_suspend_disable(struct regulator_dev *rdev)
343 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
345 return regmap_field_write(regl->suspend, 0);
348 static int da9062_buck_set_suspend_mode(struct regulator_dev *rdev,
349 unsigned mode)
351 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
352 int val;
354 switch (mode) {
355 case REGULATOR_MODE_FAST:
356 val = BUCK_MODE_SYNC;
357 break;
358 case REGULATOR_MODE_NORMAL:
359 val = BUCK_MODE_AUTO;
360 break;
361 case REGULATOR_MODE_STANDBY:
362 val = BUCK_MODE_SLEEP;
363 break;
364 default:
365 return -EINVAL;
368 return regmap_field_write(regl->mode, val);
371 static int da9062_ldo_set_suspend_mode(struct regulator_dev *rdev,
372 unsigned mode)
374 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
375 unsigned val;
377 switch (mode) {
378 case REGULATOR_MODE_NORMAL:
379 val = 0;
380 break;
381 case REGULATOR_MODE_STANDBY:
382 val = 1;
383 break;
384 default:
385 return -EINVAL;
388 return regmap_field_write(regl->suspend_sleep, val);
391 static const struct regulator_ops da9062_buck_ops = {
392 .enable = regulator_enable_regmap,
393 .disable = regulator_disable_regmap,
394 .is_enabled = regulator_is_enabled_regmap,
395 .get_voltage_sel = regulator_get_voltage_sel_regmap,
396 .set_voltage_sel = regulator_set_voltage_sel_regmap,
397 .list_voltage = regulator_list_voltage_linear,
398 .set_current_limit = da9062_set_current_limit,
399 .get_current_limit = da9062_get_current_limit,
400 .set_mode = da9062_buck_set_mode,
401 .get_mode = da9062_buck_get_mode,
402 .get_status = da9062_buck_get_status,
403 .set_suspend_voltage = da9062_set_suspend_voltage,
404 .set_suspend_enable = da9062_suspend_enable,
405 .set_suspend_disable = da9062_suspend_disable,
406 .set_suspend_mode = da9062_buck_set_suspend_mode,
409 static const struct regulator_ops da9062_ldo_ops = {
410 .enable = regulator_enable_regmap,
411 .disable = regulator_disable_regmap,
412 .is_enabled = regulator_is_enabled_regmap,
413 .get_voltage_sel = regulator_get_voltage_sel_regmap,
414 .set_voltage_sel = regulator_set_voltage_sel_regmap,
415 .list_voltage = regulator_list_voltage_linear,
416 .set_mode = da9062_ldo_set_mode,
417 .get_mode = da9062_ldo_get_mode,
418 .get_status = da9062_ldo_get_status,
419 .set_suspend_voltage = da9062_set_suspend_voltage,
420 .set_suspend_enable = da9062_suspend_enable,
421 .set_suspend_disable = da9062_suspend_disable,
422 .set_suspend_mode = da9062_ldo_set_suspend_mode,
425 /* DA9061 Regulator information */
426 static const struct da9062_regulator_info local_da9061_regulator_info[] = {
428 .desc.id = DA9061_ID_BUCK1,
429 .desc.name = "DA9061 BUCK1",
430 .desc.of_match = of_match_ptr("buck1"),
431 .desc.regulators_node = of_match_ptr("regulators"),
432 .desc.ops = &da9062_buck_ops,
433 .desc.min_uV = (300) * 1000,
434 .desc.uV_step = (10) * 1000,
435 .desc.n_voltages = ((1570) - (300))/(10) + 1,
436 .current_limits = da9062_buck_a_limits,
437 .n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
438 .desc.enable_reg = DA9062AA_BUCK1_CONT,
439 .desc.enable_mask = DA9062AA_BUCK1_EN_MASK,
440 .desc.vsel_reg = DA9062AA_VBUCK1_A,
441 .desc.vsel_mask = DA9062AA_VBUCK1_A_MASK,
442 .desc.linear_min_sel = 0,
443 .sleep = REG_FIELD(DA9062AA_VBUCK1_A,
444 __builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1,
445 sizeof(unsigned int) * 8 -
446 __builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1),
447 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B,
448 __builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1,
449 sizeof(unsigned int) * 8 -
450 __builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1),
451 .suspend_vsel_reg = DA9062AA_VBUCK1_B,
452 .mode = REG_FIELD(DA9062AA_BUCK1_CFG,
453 __builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1,
454 sizeof(unsigned int) * 8 -
455 __builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1),
456 .suspend = REG_FIELD(DA9062AA_DVC_1,
457 __builtin_ffs((int)DA9062AA_VBUCK1_SEL_MASK) - 1,
458 sizeof(unsigned int) * 8 -
459 __builtin_clz((DA9062AA_VBUCK1_SEL_MASK)) - 1),
460 .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_C,
461 __builtin_ffs((int)DA9062AA_BUCK1_ILIM_MASK) - 1,
462 sizeof(unsigned int) * 8 -
463 __builtin_clz((DA9062AA_BUCK1_ILIM_MASK)) - 1),
466 .desc.id = DA9061_ID_BUCK2,
467 .desc.name = "DA9061 BUCK2",
468 .desc.of_match = of_match_ptr("buck2"),
469 .desc.regulators_node = of_match_ptr("regulators"),
470 .desc.ops = &da9062_buck_ops,
471 .desc.min_uV = (800) * 1000,
472 .desc.uV_step = (20) * 1000,
473 .desc.n_voltages = ((3340) - (800))/(20) + 1,
474 .current_limits = da9062_buck_b_limits,
475 .n_current_limits = ARRAY_SIZE(da9062_buck_b_limits),
476 .desc.enable_reg = DA9062AA_BUCK3_CONT,
477 .desc.enable_mask = DA9062AA_BUCK3_EN_MASK,
478 .desc.vsel_reg = DA9062AA_VBUCK3_A,
479 .desc.vsel_mask = DA9062AA_VBUCK3_A_MASK,
480 .desc.linear_min_sel = 0,
481 .sleep = REG_FIELD(DA9062AA_VBUCK3_A,
482 __builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1,
483 sizeof(unsigned int) * 8 -
484 __builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1),
485 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B,
486 __builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1,
487 sizeof(unsigned int) * 8 -
488 __builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1),
489 .suspend_vsel_reg = DA9062AA_VBUCK3_B,
490 .mode = REG_FIELD(DA9062AA_BUCK3_CFG,
491 __builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1,
492 sizeof(unsigned int) * 8 -
493 __builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1),
494 .suspend = REG_FIELD(DA9062AA_DVC_1,
495 __builtin_ffs((int)DA9062AA_VBUCK3_SEL_MASK) - 1,
496 sizeof(unsigned int) * 8 -
497 __builtin_clz((DA9062AA_VBUCK3_SEL_MASK)) - 1),
498 .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_A,
499 __builtin_ffs((int)DA9062AA_BUCK3_ILIM_MASK) - 1,
500 sizeof(unsigned int) * 8 -
501 __builtin_clz((DA9062AA_BUCK3_ILIM_MASK)) - 1),
504 .desc.id = DA9061_ID_BUCK3,
505 .desc.name = "DA9061 BUCK3",
506 .desc.of_match = of_match_ptr("buck3"),
507 .desc.regulators_node = of_match_ptr("regulators"),
508 .desc.ops = &da9062_buck_ops,
509 .desc.min_uV = (530) * 1000,
510 .desc.uV_step = (10) * 1000,
511 .desc.n_voltages = ((1800) - (530))/(10) + 1,
512 .current_limits = da9062_buck_a_limits,
513 .n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
514 .desc.enable_reg = DA9062AA_BUCK4_CONT,
515 .desc.enable_mask = DA9062AA_BUCK4_EN_MASK,
516 .desc.vsel_reg = DA9062AA_VBUCK4_A,
517 .desc.vsel_mask = DA9062AA_VBUCK4_A_MASK,
518 .desc.linear_min_sel = 0,
519 .sleep = REG_FIELD(DA9062AA_VBUCK4_A,
520 __builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1,
521 sizeof(unsigned int) * 8 -
522 __builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1),
523 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B,
524 __builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1,
525 sizeof(unsigned int) * 8 -
526 __builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1),
527 .suspend_vsel_reg = DA9062AA_VBUCK4_B,
528 .mode = REG_FIELD(DA9062AA_BUCK4_CFG,
529 __builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1,
530 sizeof(unsigned int) * 8 -
531 __builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1),
532 .suspend = REG_FIELD(DA9062AA_DVC_1,
533 __builtin_ffs((int)DA9062AA_VBUCK4_SEL_MASK) - 1,
534 sizeof(unsigned int) * 8 -
535 __builtin_clz((DA9062AA_VBUCK4_SEL_MASK)) - 1),
536 .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_B,
537 __builtin_ffs((int)DA9062AA_BUCK4_ILIM_MASK) - 1,
538 sizeof(unsigned int) * 8 -
539 __builtin_clz((DA9062AA_BUCK4_ILIM_MASK)) - 1),
542 .desc.id = DA9061_ID_LDO1,
543 .desc.name = "DA9061 LDO1",
544 .desc.of_match = of_match_ptr("ldo1"),
545 .desc.regulators_node = of_match_ptr("regulators"),
546 .desc.ops = &da9062_ldo_ops,
547 .desc.min_uV = (900) * 1000,
548 .desc.uV_step = (50) * 1000,
549 .desc.n_voltages = ((3600) - (900))/(50) + 1,
550 .desc.enable_reg = DA9062AA_LDO1_CONT,
551 .desc.enable_mask = DA9062AA_LDO1_EN_MASK,
552 .desc.vsel_reg = DA9062AA_VLDO1_A,
553 .desc.vsel_mask = DA9062AA_VLDO1_A_MASK,
554 .desc.linear_min_sel = 0,
555 .sleep = REG_FIELD(DA9062AA_VLDO1_A,
556 __builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1,
557 sizeof(unsigned int) * 8 -
558 __builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1),
559 .suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B,
560 __builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1,
561 sizeof(unsigned int) * 8 -
562 __builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1),
563 .suspend_vsel_reg = DA9062AA_VLDO1_B,
564 .suspend = REG_FIELD(DA9062AA_DVC_1,
565 __builtin_ffs((int)DA9062AA_VLDO1_SEL_MASK) - 1,
566 sizeof(unsigned int) * 8 -
567 __builtin_clz((DA9062AA_VLDO1_SEL_MASK)) - 1),
568 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
569 __builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1,
570 sizeof(unsigned int) * 8 -
571 __builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1),
574 .desc.id = DA9061_ID_LDO2,
575 .desc.name = "DA9061 LDO2",
576 .desc.of_match = of_match_ptr("ldo2"),
577 .desc.regulators_node = of_match_ptr("regulators"),
578 .desc.ops = &da9062_ldo_ops,
579 .desc.min_uV = (900) * 1000,
580 .desc.uV_step = (50) * 1000,
581 .desc.n_voltages = ((3600) - (600))/(50) + 1,
582 .desc.enable_reg = DA9062AA_LDO2_CONT,
583 .desc.enable_mask = DA9062AA_LDO2_EN_MASK,
584 .desc.vsel_reg = DA9062AA_VLDO2_A,
585 .desc.vsel_mask = DA9062AA_VLDO2_A_MASK,
586 .desc.linear_min_sel = 0,
587 .sleep = REG_FIELD(DA9062AA_VLDO2_A,
588 __builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1,
589 sizeof(unsigned int) * 8 -
590 __builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1),
591 .suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B,
592 __builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1,
593 sizeof(unsigned int) * 8 -
594 __builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1),
595 .suspend_vsel_reg = DA9062AA_VLDO2_B,
596 .suspend = REG_FIELD(DA9062AA_DVC_1,
597 __builtin_ffs((int)DA9062AA_VLDO2_SEL_MASK) - 1,
598 sizeof(unsigned int) * 8 -
599 __builtin_clz((DA9062AA_VLDO2_SEL_MASK)) - 1),
600 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
601 __builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1,
602 sizeof(unsigned int) * 8 -
603 __builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1),
606 .desc.id = DA9061_ID_LDO3,
607 .desc.name = "DA9061 LDO3",
608 .desc.of_match = of_match_ptr("ldo3"),
609 .desc.regulators_node = of_match_ptr("regulators"),
610 .desc.ops = &da9062_ldo_ops,
611 .desc.min_uV = (900) * 1000,
612 .desc.uV_step = (50) * 1000,
613 .desc.n_voltages = ((3600) - (900))/(50) + 1,
614 .desc.enable_reg = DA9062AA_LDO3_CONT,
615 .desc.enable_mask = DA9062AA_LDO3_EN_MASK,
616 .desc.vsel_reg = DA9062AA_VLDO3_A,
617 .desc.vsel_mask = DA9062AA_VLDO3_A_MASK,
618 .desc.linear_min_sel = 0,
619 .sleep = REG_FIELD(DA9062AA_VLDO3_A,
620 __builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1,
621 sizeof(unsigned int) * 8 -
622 __builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1),
623 .suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B,
624 __builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1,
625 sizeof(unsigned int) * 8 -
626 __builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1),
627 .suspend_vsel_reg = DA9062AA_VLDO3_B,
628 .suspend = REG_FIELD(DA9062AA_DVC_1,
629 __builtin_ffs((int)DA9062AA_VLDO3_SEL_MASK) - 1,
630 sizeof(unsigned int) * 8 -
631 __builtin_clz((DA9062AA_VLDO3_SEL_MASK)) - 1),
632 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
633 __builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1,
634 sizeof(unsigned int) * 8 -
635 __builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1),
638 .desc.id = DA9061_ID_LDO4,
639 .desc.name = "DA9061 LDO4",
640 .desc.of_match = of_match_ptr("ldo4"),
641 .desc.regulators_node = of_match_ptr("regulators"),
642 .desc.ops = &da9062_ldo_ops,
643 .desc.min_uV = (900) * 1000,
644 .desc.uV_step = (50) * 1000,
645 .desc.n_voltages = ((3600) - (900))/(50) + 1,
646 .desc.enable_reg = DA9062AA_LDO4_CONT,
647 .desc.enable_mask = DA9062AA_LDO4_EN_MASK,
648 .desc.vsel_reg = DA9062AA_VLDO4_A,
649 .desc.vsel_mask = DA9062AA_VLDO4_A_MASK,
650 .desc.linear_min_sel = 0,
651 .sleep = REG_FIELD(DA9062AA_VLDO4_A,
652 __builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1,
653 sizeof(unsigned int) * 8 -
654 __builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1),
655 .suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B,
656 __builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1,
657 sizeof(unsigned int) * 8 -
658 __builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1),
659 .suspend_vsel_reg = DA9062AA_VLDO4_B,
660 .suspend = REG_FIELD(DA9062AA_DVC_1,
661 __builtin_ffs((int)DA9062AA_VLDO4_SEL_MASK) - 1,
662 sizeof(unsigned int) * 8 -
663 __builtin_clz((DA9062AA_VLDO4_SEL_MASK)) - 1),
664 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
665 __builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1,
666 sizeof(unsigned int) * 8 -
667 __builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1),
671 /* DA9062 Regulator information */
672 static const struct da9062_regulator_info local_da9062_regulator_info[] = {
674 .desc.id = DA9062_ID_BUCK1,
675 .desc.name = "DA9062 BUCK1",
676 .desc.of_match = of_match_ptr("buck1"),
677 .desc.regulators_node = of_match_ptr("regulators"),
678 .desc.ops = &da9062_buck_ops,
679 .desc.min_uV = (300) * 1000,
680 .desc.uV_step = (10) * 1000,
681 .desc.n_voltages = ((1570) - (300))/(10) + 1,
682 .current_limits = da9062_buck_a_limits,
683 .n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
684 .desc.enable_reg = DA9062AA_BUCK1_CONT,
685 .desc.enable_mask = DA9062AA_BUCK1_EN_MASK,
686 .desc.vsel_reg = DA9062AA_VBUCK1_A,
687 .desc.vsel_mask = DA9062AA_VBUCK1_A_MASK,
688 .desc.linear_min_sel = 0,
689 .sleep = REG_FIELD(DA9062AA_VBUCK1_A,
690 __builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1,
691 sizeof(unsigned int) * 8 -
692 __builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1),
693 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B,
694 __builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1,
695 sizeof(unsigned int) * 8 -
696 __builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1),
697 .suspend_vsel_reg = DA9062AA_VBUCK1_B,
698 .mode = REG_FIELD(DA9062AA_BUCK1_CFG,
699 __builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1,
700 sizeof(unsigned int) * 8 -
701 __builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1),
702 .suspend = REG_FIELD(DA9062AA_DVC_1,
703 __builtin_ffs((int)DA9062AA_VBUCK1_SEL_MASK) - 1,
704 sizeof(unsigned int) * 8 -
705 __builtin_clz((DA9062AA_VBUCK1_SEL_MASK)) - 1),
706 .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_C,
707 __builtin_ffs((int)DA9062AA_BUCK1_ILIM_MASK) - 1,
708 sizeof(unsigned int) * 8 -
709 __builtin_clz((DA9062AA_BUCK1_ILIM_MASK)) - 1),
712 .desc.id = DA9062_ID_BUCK2,
713 .desc.name = "DA9062 BUCK2",
714 .desc.of_match = of_match_ptr("buck2"),
715 .desc.regulators_node = of_match_ptr("regulators"),
716 .desc.ops = &da9062_buck_ops,
717 .desc.min_uV = (300) * 1000,
718 .desc.uV_step = (10) * 1000,
719 .desc.n_voltages = ((1570) - (300))/(10) + 1,
720 .current_limits = da9062_buck_a_limits,
721 .n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
722 .desc.enable_reg = DA9062AA_BUCK2_CONT,
723 .desc.enable_mask = DA9062AA_BUCK2_EN_MASK,
724 .desc.vsel_reg = DA9062AA_VBUCK2_A,
725 .desc.vsel_mask = DA9062AA_VBUCK2_A_MASK,
726 .desc.linear_min_sel = 0,
727 .sleep = REG_FIELD(DA9062AA_VBUCK2_A,
728 __builtin_ffs((int)DA9062AA_BUCK2_SL_A_MASK) - 1,
729 sizeof(unsigned int) * 8 -
730 __builtin_clz((DA9062AA_BUCK2_SL_A_MASK)) - 1),
731 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK2_B,
732 __builtin_ffs((int)DA9062AA_BUCK2_SL_B_MASK) - 1,
733 sizeof(unsigned int) * 8 -
734 __builtin_clz((DA9062AA_BUCK2_SL_B_MASK)) - 1),
735 .suspend_vsel_reg = DA9062AA_VBUCK2_B,
736 .mode = REG_FIELD(DA9062AA_BUCK2_CFG,
737 __builtin_ffs((int)DA9062AA_BUCK2_MODE_MASK) - 1,
738 sizeof(unsigned int) * 8 -
739 __builtin_clz((DA9062AA_BUCK2_MODE_MASK)) - 1),
740 .suspend = REG_FIELD(DA9062AA_DVC_1,
741 __builtin_ffs((int)DA9062AA_VBUCK2_SEL_MASK) - 1,
742 sizeof(unsigned int) * 8 -
743 __builtin_clz((DA9062AA_VBUCK2_SEL_MASK)) - 1),
744 .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_C,
745 __builtin_ffs((int)DA9062AA_BUCK2_ILIM_MASK) - 1,
746 sizeof(unsigned int) * 8 -
747 __builtin_clz((DA9062AA_BUCK2_ILIM_MASK)) - 1),
750 .desc.id = DA9062_ID_BUCK3,
751 .desc.name = "DA9062 BUCK3",
752 .desc.of_match = of_match_ptr("buck3"),
753 .desc.regulators_node = of_match_ptr("regulators"),
754 .desc.ops = &da9062_buck_ops,
755 .desc.min_uV = (800) * 1000,
756 .desc.uV_step = (20) * 1000,
757 .desc.n_voltages = ((3340) - (800))/(20) + 1,
758 .current_limits = da9062_buck_b_limits,
759 .n_current_limits = ARRAY_SIZE(da9062_buck_b_limits),
760 .desc.enable_reg = DA9062AA_BUCK3_CONT,
761 .desc.enable_mask = DA9062AA_BUCK3_EN_MASK,
762 .desc.vsel_reg = DA9062AA_VBUCK3_A,
763 .desc.vsel_mask = DA9062AA_VBUCK3_A_MASK,
764 .desc.linear_min_sel = 0,
765 .sleep = REG_FIELD(DA9062AA_VBUCK3_A,
766 __builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1,
767 sizeof(unsigned int) * 8 -
768 __builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1),
769 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B,
770 __builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1,
771 sizeof(unsigned int) * 8 -
772 __builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1),
773 .suspend_vsel_reg = DA9062AA_VBUCK3_B,
774 .mode = REG_FIELD(DA9062AA_BUCK3_CFG,
775 __builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1,
776 sizeof(unsigned int) * 8 -
777 __builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1),
778 .suspend = REG_FIELD(DA9062AA_DVC_1,
779 __builtin_ffs((int)DA9062AA_VBUCK3_SEL_MASK) - 1,
780 sizeof(unsigned int) * 8 -
781 __builtin_clz((DA9062AA_VBUCK3_SEL_MASK)) - 1),
782 .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_A,
783 __builtin_ffs((int)DA9062AA_BUCK3_ILIM_MASK) - 1,
784 sizeof(unsigned int) * 8 -
785 __builtin_clz((DA9062AA_BUCK3_ILIM_MASK)) - 1),
788 .desc.id = DA9062_ID_BUCK4,
789 .desc.name = "DA9062 BUCK4",
790 .desc.of_match = of_match_ptr("buck4"),
791 .desc.regulators_node = of_match_ptr("regulators"),
792 .desc.ops = &da9062_buck_ops,
793 .desc.min_uV = (530) * 1000,
794 .desc.uV_step = (10) * 1000,
795 .desc.n_voltages = ((1800) - (530))/(10) + 1,
796 .current_limits = da9062_buck_a_limits,
797 .n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
798 .desc.enable_reg = DA9062AA_BUCK4_CONT,
799 .desc.enable_mask = DA9062AA_BUCK4_EN_MASK,
800 .desc.vsel_reg = DA9062AA_VBUCK4_A,
801 .desc.vsel_mask = DA9062AA_VBUCK4_A_MASK,
802 .desc.linear_min_sel = 0,
803 .sleep = REG_FIELD(DA9062AA_VBUCK4_A,
804 __builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1,
805 sizeof(unsigned int) * 8 -
806 __builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1),
807 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B,
808 __builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1,
809 sizeof(unsigned int) * 8 -
810 __builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1),
811 .suspend_vsel_reg = DA9062AA_VBUCK4_B,
812 .mode = REG_FIELD(DA9062AA_BUCK4_CFG,
813 __builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1,
814 sizeof(unsigned int) * 8 -
815 __builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1),
816 .suspend = REG_FIELD(DA9062AA_DVC_1,
817 __builtin_ffs((int)DA9062AA_VBUCK4_SEL_MASK) - 1,
818 sizeof(unsigned int) * 8 -
819 __builtin_clz((DA9062AA_VBUCK4_SEL_MASK)) - 1),
820 .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_B,
821 __builtin_ffs((int)DA9062AA_BUCK4_ILIM_MASK) - 1,
822 sizeof(unsigned int) * 8 -
823 __builtin_clz((DA9062AA_BUCK4_ILIM_MASK)) - 1),
826 .desc.id = DA9062_ID_LDO1,
827 .desc.name = "DA9062 LDO1",
828 .desc.of_match = of_match_ptr("ldo1"),
829 .desc.regulators_node = of_match_ptr("regulators"),
830 .desc.ops = &da9062_ldo_ops,
831 .desc.min_uV = (900) * 1000,
832 .desc.uV_step = (50) * 1000,
833 .desc.n_voltages = ((3600) - (900))/(50) + 1,
834 .desc.enable_reg = DA9062AA_LDO1_CONT,
835 .desc.enable_mask = DA9062AA_LDO1_EN_MASK,
836 .desc.vsel_reg = DA9062AA_VLDO1_A,
837 .desc.vsel_mask = DA9062AA_VLDO1_A_MASK,
838 .desc.linear_min_sel = 0,
839 .sleep = REG_FIELD(DA9062AA_VLDO1_A,
840 __builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1,
841 sizeof(unsigned int) * 8 -
842 __builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1),
843 .suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B,
844 __builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1,
845 sizeof(unsigned int) * 8 -
846 __builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1),
847 .suspend_vsel_reg = DA9062AA_VLDO1_B,
848 .suspend = REG_FIELD(DA9062AA_DVC_1,
849 __builtin_ffs((int)DA9062AA_VLDO1_SEL_MASK) - 1,
850 sizeof(unsigned int) * 8 -
851 __builtin_clz((DA9062AA_VLDO1_SEL_MASK)) - 1),
852 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
853 __builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1,
854 sizeof(unsigned int) * 8 -
855 __builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1),
858 .desc.id = DA9062_ID_LDO2,
859 .desc.name = "DA9062 LDO2",
860 .desc.of_match = of_match_ptr("ldo2"),
861 .desc.regulators_node = of_match_ptr("regulators"),
862 .desc.ops = &da9062_ldo_ops,
863 .desc.min_uV = (900) * 1000,
864 .desc.uV_step = (50) * 1000,
865 .desc.n_voltages = ((3600) - (600))/(50) + 1,
866 .desc.enable_reg = DA9062AA_LDO2_CONT,
867 .desc.enable_mask = DA9062AA_LDO2_EN_MASK,
868 .desc.vsel_reg = DA9062AA_VLDO2_A,
869 .desc.vsel_mask = DA9062AA_VLDO2_A_MASK,
870 .desc.linear_min_sel = 0,
871 .sleep = REG_FIELD(DA9062AA_VLDO2_A,
872 __builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1,
873 sizeof(unsigned int) * 8 -
874 __builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1),
875 .suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B,
876 __builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1,
877 sizeof(unsigned int) * 8 -
878 __builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1),
879 .suspend_vsel_reg = DA9062AA_VLDO2_B,
880 .suspend = REG_FIELD(DA9062AA_DVC_1,
881 __builtin_ffs((int)DA9062AA_VLDO2_SEL_MASK) - 1,
882 sizeof(unsigned int) * 8 -
883 __builtin_clz((DA9062AA_VLDO2_SEL_MASK)) - 1),
884 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
885 __builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1,
886 sizeof(unsigned int) * 8 -
887 __builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1),
890 .desc.id = DA9062_ID_LDO3,
891 .desc.name = "DA9062 LDO3",
892 .desc.of_match = of_match_ptr("ldo3"),
893 .desc.regulators_node = of_match_ptr("regulators"),
894 .desc.ops = &da9062_ldo_ops,
895 .desc.min_uV = (900) * 1000,
896 .desc.uV_step = (50) * 1000,
897 .desc.n_voltages = ((3600) - (900))/(50) + 1,
898 .desc.enable_reg = DA9062AA_LDO3_CONT,
899 .desc.enable_mask = DA9062AA_LDO3_EN_MASK,
900 .desc.vsel_reg = DA9062AA_VLDO3_A,
901 .desc.vsel_mask = DA9062AA_VLDO3_A_MASK,
902 .desc.linear_min_sel = 0,
903 .sleep = REG_FIELD(DA9062AA_VLDO3_A,
904 __builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1,
905 sizeof(unsigned int) * 8 -
906 __builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1),
907 .suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B,
908 __builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1,
909 sizeof(unsigned int) * 8 -
910 __builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1),
911 .suspend_vsel_reg = DA9062AA_VLDO3_B,
912 .suspend = REG_FIELD(DA9062AA_DVC_1,
913 __builtin_ffs((int)DA9062AA_VLDO3_SEL_MASK) - 1,
914 sizeof(unsigned int) * 8 -
915 __builtin_clz((DA9062AA_VLDO3_SEL_MASK)) - 1),
916 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
917 __builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1,
918 sizeof(unsigned int) * 8 -
919 __builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1),
922 .desc.id = DA9062_ID_LDO4,
923 .desc.name = "DA9062 LDO4",
924 .desc.of_match = of_match_ptr("ldo4"),
925 .desc.regulators_node = of_match_ptr("regulators"),
926 .desc.ops = &da9062_ldo_ops,
927 .desc.min_uV = (900) * 1000,
928 .desc.uV_step = (50) * 1000,
929 .desc.n_voltages = ((3600) - (900))/(50) + 1,
930 .desc.enable_reg = DA9062AA_LDO4_CONT,
931 .desc.enable_mask = DA9062AA_LDO4_EN_MASK,
932 .desc.vsel_reg = DA9062AA_VLDO4_A,
933 .desc.vsel_mask = DA9062AA_VLDO4_A_MASK,
934 .desc.linear_min_sel = 0,
935 .sleep = REG_FIELD(DA9062AA_VLDO4_A,
936 __builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1,
937 sizeof(unsigned int) * 8 -
938 __builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1),
939 .suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B,
940 __builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1,
941 sizeof(unsigned int) * 8 -
942 __builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1),
943 .suspend_vsel_reg = DA9062AA_VLDO4_B,
944 .suspend = REG_FIELD(DA9062AA_DVC_1,
945 __builtin_ffs((int)DA9062AA_VLDO4_SEL_MASK) - 1,
946 sizeof(unsigned int) * 8 -
947 __builtin_clz((DA9062AA_VLDO4_SEL_MASK)) - 1),
948 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
949 __builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1,
950 sizeof(unsigned int) * 8 -
951 __builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1),
955 /* Regulator interrupt handlers */
956 static irqreturn_t da9062_ldo_lim_event(int irq, void *data)
958 struct da9062_regulators *regulators = data;
959 struct da9062 *hw = regulators->regulator[0].hw;
960 struct da9062_regulator *regl;
961 int handled = IRQ_NONE;
962 int bits, i, ret;
964 ret = regmap_read(hw->regmap, DA9062AA_STATUS_D, &bits);
965 if (ret < 0) {
966 dev_err(hw->dev,
967 "Failed to read LDO overcurrent indicator\n");
968 goto ldo_lim_error;
971 for (i = regulators->n_regulators - 1; i >= 0; i--) {
972 regl = &regulators->regulator[i];
973 if (regl->info->oc_event.reg != DA9062AA_STATUS_D)
974 continue;
976 if (BIT(regl->info->oc_event.lsb) & bits) {
977 regulator_notifier_call_chain(regl->rdev,
978 REGULATOR_EVENT_OVER_CURRENT, NULL);
979 handled = IRQ_HANDLED;
983 ldo_lim_error:
984 return handled;
987 static int da9062_regulator_probe(struct platform_device *pdev)
989 struct da9062 *chip = dev_get_drvdata(pdev->dev.parent);
990 struct da9062_regulators *regulators;
991 struct da9062_regulator *regl;
992 struct regulator_config config = { };
993 const struct da9062_regulator_info *rinfo;
994 int irq, n, ret;
995 size_t size;
996 int max_regulators;
998 switch (chip->chip_type) {
999 case COMPAT_TYPE_DA9061:
1000 max_regulators = DA9061_MAX_REGULATORS;
1001 rinfo = local_da9061_regulator_info;
1002 break;
1003 case COMPAT_TYPE_DA9062:
1004 max_regulators = DA9062_MAX_REGULATORS;
1005 rinfo = local_da9062_regulator_info;
1006 break;
1007 default:
1008 dev_err(chip->dev, "Unrecognised chip type\n");
1009 return -ENODEV;
1012 /* Allocate memory required by usable regulators */
1013 size = sizeof(struct da9062_regulators) +
1014 max_regulators * sizeof(struct da9062_regulator);
1015 regulators = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
1016 if (!regulators)
1017 return -ENOMEM;
1019 regulators->n_regulators = max_regulators;
1020 platform_set_drvdata(pdev, regulators);
1022 n = 0;
1023 while (n < regulators->n_regulators) {
1024 /* Initialise regulator structure */
1025 regl = &regulators->regulator[n];
1026 regl->hw = chip;
1027 regl->info = &rinfo[n];
1028 regl->desc = regl->info->desc;
1029 regl->desc.type = REGULATOR_VOLTAGE;
1030 regl->desc.owner = THIS_MODULE;
1032 if (regl->info->mode.reg)
1033 regl->mode = devm_regmap_field_alloc(
1034 &pdev->dev,
1035 chip->regmap,
1036 regl->info->mode);
1037 if (regl->info->suspend.reg)
1038 regl->suspend = devm_regmap_field_alloc(
1039 &pdev->dev,
1040 chip->regmap,
1041 regl->info->suspend);
1042 if (regl->info->sleep.reg)
1043 regl->sleep = devm_regmap_field_alloc(
1044 &pdev->dev,
1045 chip->regmap,
1046 regl->info->sleep);
1047 if (regl->info->suspend_sleep.reg)
1048 regl->suspend_sleep = devm_regmap_field_alloc(
1049 &pdev->dev,
1050 chip->regmap,
1051 regl->info->suspend_sleep);
1052 if (regl->info->ilimit.reg)
1053 regl->ilimit = devm_regmap_field_alloc(
1054 &pdev->dev,
1055 chip->regmap,
1056 regl->info->ilimit);
1058 /* Register regulator */
1059 memset(&config, 0, sizeof(config));
1060 config.dev = chip->dev;
1061 config.driver_data = regl;
1062 config.regmap = chip->regmap;
1064 regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
1065 &config);
1066 if (IS_ERR(regl->rdev)) {
1067 dev_err(&pdev->dev,
1068 "Failed to register %s regulator\n",
1069 regl->desc.name);
1070 return PTR_ERR(regl->rdev);
1073 n++;
1076 /* LDOs overcurrent event support */
1077 irq = platform_get_irq_byname(pdev, "LDO_LIM");
1078 if (irq < 0) {
1079 dev_err(&pdev->dev, "Failed to get IRQ.\n");
1080 return irq;
1082 regulators->irq_ldo_lim = irq;
1084 ret = devm_request_threaded_irq(&pdev->dev, irq,
1085 NULL, da9062_ldo_lim_event,
1086 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1087 "LDO_LIM", regulators);
1088 if (ret) {
1089 dev_warn(&pdev->dev,
1090 "Failed to request LDO_LIM IRQ.\n");
1091 regulators->irq_ldo_lim = -ENXIO;
1094 return 0;
1097 static struct platform_driver da9062_regulator_driver = {
1098 .driver = {
1099 .name = "da9062-regulators",
1101 .probe = da9062_regulator_probe,
1104 static int __init da9062_regulator_init(void)
1106 return platform_driver_register(&da9062_regulator_driver);
1108 subsys_initcall(da9062_regulator_init);
1110 static void __exit da9062_regulator_cleanup(void)
1112 platform_driver_unregister(&da9062_regulator_driver);
1114 module_exit(da9062_regulator_cleanup);
1116 /* Module information */
1117 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
1118 MODULE_DESCRIPTION("REGULATOR device driver for Dialog DA9062 and DA9061");
1119 MODULE_LICENSE("GPL");
1120 MODULE_ALIAS("platform:da9062-regulators");