blob: 5638fe8d759d745e11e3c14fbb173f0a2efd1912 [file] [log] [blame]
S Twiss4068e512015-05-19 14:10:30 +01001/*
2 * da9062-regulator.c - REGULATOR device driver for DA9062
3 * Copyright (C) 2015 Dialog Semiconductor Ltd.
4 *
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.
9 *
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.
14 */
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>
28
29/* Regulator IDs */
30enum {
31 DA9062_ID_BUCK1,
32 DA9062_ID_BUCK2,
33 DA9062_ID_BUCK3,
34 DA9062_ID_BUCK4,
35 DA9062_ID_LDO1,
36 DA9062_ID_LDO2,
37 DA9062_ID_LDO3,
38 DA9062_ID_LDO4,
39 DA9062_MAX_REGULATORS,
40};
41
42/* Regulator capabilities and registers description */
43struct da9062_regulator_info {
44 struct regulator_desc desc;
45 /* Current limiting */
46 unsigned int n_current_limits;
47 const int *current_limits;
48 /* Main register fields */
49 struct reg_field mode;
50 struct reg_field suspend;
51 struct reg_field sleep;
52 struct reg_field suspend_sleep;
53 unsigned int suspend_vsel_reg;
54 struct reg_field ilimit;
55 /* Event detection bit */
56 struct reg_field oc_event;
57};
58
59/* Single regulator settings */
60struct da9062_regulator {
61 struct regulator_desc desc;
62 struct regulator_dev *rdev;
63 struct da9062 *hw;
64 const struct da9062_regulator_info *info;
65
66 struct regmap_field *mode;
67 struct regmap_field *suspend;
68 struct regmap_field *sleep;
69 struct regmap_field *suspend_sleep;
70 struct regmap_field *ilimit;
71};
72
73/* Encapsulates all information for the regulators driver */
74struct da9062_regulators {
75 int irq_ldo_lim;
76 unsigned n_regulators;
77 /* Array size to be defined during init. Keep at end. */
78 struct da9062_regulator regulator[0];
79};
80
81/* BUCK modes */
82enum {
83 BUCK_MODE_MANUAL, /* 0 */
84 BUCK_MODE_SLEEP, /* 1 */
85 BUCK_MODE_SYNC, /* 2 */
86 BUCK_MODE_AUTO /* 3 */
87};
88
89/* Regulator operations */
90
91/* Current limits array (in uA) BUCK1 and BUCK3.
92 Entry indexes corresponds to register values. */
93static const int da9062_buck_a_limits[] = {
94 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
95 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
96};
97
98/* Current limits array (in uA) for BUCK2.
99 Entry indexes corresponds to register values. */
100static const int da9062_buck_b_limits[] = {
101 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
102 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
103};
104
105static int da9062_set_current_limit(struct regulator_dev *rdev,
106 int min_ua, int max_ua)
107{
108 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
109 const struct da9062_regulator_info *rinfo = regl->info;
110 int n, tval;
111
112 for (n = 0; n < rinfo->n_current_limits; n++) {
113 tval = rinfo->current_limits[n];
114 if (tval >= min_ua && tval <= max_ua)
115 return regmap_field_write(regl->ilimit, n);
116 }
117
118 return -EINVAL;
119}
120
121static int da9062_get_current_limit(struct regulator_dev *rdev)
122{
123 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
124 const struct da9062_regulator_info *rinfo = regl->info;
125 unsigned int sel;
126 int ret;
127
128 ret = regmap_field_read(regl->ilimit, &sel);
129 if (ret < 0)
130 return ret;
131
132 if (sel >= rinfo->n_current_limits)
133 sel = rinfo->n_current_limits - 1;
134
135 return rinfo->current_limits[sel];
136}
137
138static int da9062_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
139{
140 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
141 unsigned val;
142
143 switch (mode) {
144 case REGULATOR_MODE_FAST:
145 val = BUCK_MODE_SYNC;
146 break;
147 case REGULATOR_MODE_NORMAL:
148 val = BUCK_MODE_AUTO;
149 break;
150 case REGULATOR_MODE_STANDBY:
151 val = BUCK_MODE_SLEEP;
152 break;
153 default:
154 return -EINVAL;
155 }
156
157 return regmap_field_write(regl->mode, val);
158}
159
160/*
161 * Bucks use single mode register field for normal operation
162 * and suspend state.
163 * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
164 */
165
166static unsigned da9062_buck_get_mode(struct regulator_dev *rdev)
167{
168 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
169 struct regmap_field *field;
170 unsigned int val, mode = 0;
171 int ret;
172
173 ret = regmap_field_read(regl->mode, &val);
174 if (ret < 0)
175 return ret;
176
177 switch (val) {
178 default:
179 case BUCK_MODE_MANUAL:
180 mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
181 /* Sleep flag bit decides the mode */
182 break;
183 case BUCK_MODE_SLEEP:
184 return REGULATOR_MODE_STANDBY;
185 case BUCK_MODE_SYNC:
186 return REGULATOR_MODE_FAST;
187 case BUCK_MODE_AUTO:
188 return REGULATOR_MODE_NORMAL;
189 }
190
191 /* Detect current regulator state */
192 ret = regmap_field_read(regl->suspend, &val);
193 if (ret < 0)
194 return 0;
195
196 /* Read regulator mode from proper register, depending on state */
197 if (val)
198 field = regl->suspend_sleep;
199 else
200 field = regl->sleep;
201
202 ret = regmap_field_read(field, &val);
203 if (ret < 0)
204 return 0;
205
206 if (val)
207 mode &= REGULATOR_MODE_STANDBY;
208 else
209 mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
210
211 return mode;
212}
213
214/*
215 * LDOs use sleep flags - one for normal and one for suspend state.
216 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
217 */
218
219static int da9062_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
220{
221 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
222 unsigned val;
223
224 switch (mode) {
225 case REGULATOR_MODE_NORMAL:
226 val = 0;
227 break;
228 case REGULATOR_MODE_STANDBY:
229 val = 1;
230 break;
231 default:
232 return -EINVAL;
233 }
234
235 return regmap_field_write(regl->sleep, val);
236}
237
238static unsigned da9062_ldo_get_mode(struct regulator_dev *rdev)
239{
240 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
241 struct regmap_field *field;
242 int ret, val;
243
244 /* Detect current regulator state */
245 ret = regmap_field_read(regl->suspend, &val);
246 if (ret < 0)
247 return 0;
248
249 /* Read regulator mode from proper register, depending on state */
250 if (val)
251 field = regl->suspend_sleep;
252 else
253 field = regl->sleep;
254
255 ret = regmap_field_read(field, &val);
256 if (ret < 0)
257 return 0;
258
259 if (val)
260 return REGULATOR_MODE_STANDBY;
261 else
262 return REGULATOR_MODE_NORMAL;
263}
264
265static int da9062_buck_get_status(struct regulator_dev *rdev)
266{
267 int ret = regulator_is_enabled_regmap(rdev);
268
269 if (ret == 0) {
270 ret = REGULATOR_STATUS_OFF;
271 } else if (ret > 0) {
272 ret = da9062_buck_get_mode(rdev);
273 if (ret > 0)
274 ret = regulator_mode_to_status(ret);
275 else if (ret == 0)
276 ret = -EIO;
277 }
278
279 return ret;
280}
281
282static int da9062_ldo_get_status(struct regulator_dev *rdev)
283{
284 int ret = regulator_is_enabled_regmap(rdev);
285
286 if (ret == 0) {
287 ret = REGULATOR_STATUS_OFF;
288 } else if (ret > 0) {
289 ret = da9062_ldo_get_mode(rdev);
290 if (ret > 0)
291 ret = regulator_mode_to_status(ret);
292 else if (ret == 0)
293 ret = -EIO;
294 }
295
296 return ret;
297}
298
299static int da9062_set_suspend_voltage(struct regulator_dev *rdev, int uv)
300{
301 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
302 const struct da9062_regulator_info *rinfo = regl->info;
303 int ret, sel;
304
305 sel = regulator_map_voltage_linear(rdev, uv, uv);
306 if (sel < 0)
307 return sel;
308
309 sel <<= ffs(rdev->desc->vsel_mask) - 1;
310
311 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
312 rdev->desc->vsel_mask, sel);
313
314 return ret;
315}
316
317static int da9062_suspend_enable(struct regulator_dev *rdev)
318{
319 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
320
321 return regmap_field_write(regl->suspend, 1);
322}
323
324static int da9062_suspend_disable(struct regulator_dev *rdev)
325{
326 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
327
328 return regmap_field_write(regl->suspend, 0);
329}
330
331static int da9062_buck_set_suspend_mode(struct regulator_dev *rdev,
332 unsigned mode)
333{
334 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
335 int val;
336
337 switch (mode) {
338 case REGULATOR_MODE_FAST:
339 val = BUCK_MODE_SYNC;
340 break;
341 case REGULATOR_MODE_NORMAL:
342 val = BUCK_MODE_AUTO;
343 break;
344 case REGULATOR_MODE_STANDBY:
345 val = BUCK_MODE_SLEEP;
346 break;
347 default:
348 return -EINVAL;
349 }
350
351 return regmap_field_write(regl->mode, val);
352}
353
354static int da9062_ldo_set_suspend_mode(struct regulator_dev *rdev,
355 unsigned mode)
356{
357 struct da9062_regulator *regl = rdev_get_drvdata(rdev);
358 unsigned val;
359
360 switch (mode) {
361 case REGULATOR_MODE_NORMAL:
362 val = 0;
363 break;
364 case REGULATOR_MODE_STANDBY:
365 val = 1;
366 break;
367 default:
368 return -EINVAL;
369 }
370
371 return regmap_field_write(regl->suspend_sleep, val);
372}
373
374static struct regulator_ops da9062_buck_ops = {
375 .enable = regulator_enable_regmap,
376 .disable = regulator_disable_regmap,
377 .is_enabled = regulator_is_enabled_regmap,
378 .get_voltage_sel = regulator_get_voltage_sel_regmap,
379 .set_voltage_sel = regulator_set_voltage_sel_regmap,
380 .list_voltage = regulator_list_voltage_linear,
381 .set_current_limit = da9062_set_current_limit,
382 .get_current_limit = da9062_get_current_limit,
383 .set_mode = da9062_buck_set_mode,
384 .get_mode = da9062_buck_get_mode,
385 .get_status = da9062_buck_get_status,
386 .set_suspend_voltage = da9062_set_suspend_voltage,
387 .set_suspend_enable = da9062_suspend_enable,
388 .set_suspend_disable = da9062_suspend_disable,
389 .set_suspend_mode = da9062_buck_set_suspend_mode,
390};
391
392static struct regulator_ops da9062_ldo_ops = {
393 .enable = regulator_enable_regmap,
394 .disable = regulator_disable_regmap,
395 .is_enabled = regulator_is_enabled_regmap,
396 .get_voltage_sel = regulator_get_voltage_sel_regmap,
397 .set_voltage_sel = regulator_set_voltage_sel_regmap,
398 .list_voltage = regulator_list_voltage_linear,
399 .set_mode = da9062_ldo_set_mode,
400 .get_mode = da9062_ldo_get_mode,
401 .get_status = da9062_ldo_get_status,
402 .set_suspend_voltage = da9062_set_suspend_voltage,
403 .set_suspend_enable = da9062_suspend_enable,
404 .set_suspend_disable = da9062_suspend_disable,
405 .set_suspend_mode = da9062_ldo_set_suspend_mode,
406};
407
408/* Regulator information */
409static const struct da9062_regulator_info local_regulator_info[] = {
410 {
411 .desc.id = DA9062_ID_BUCK1,
412 .desc.name = "DA9062 BUCK1",
413 .desc.of_match = of_match_ptr("buck1"),
414 .desc.regulators_node = of_match_ptr("regulators"),
415 .desc.ops = &da9062_buck_ops,
416 .desc.min_uV = (300) * 1000,
417 .desc.uV_step = (10) * 1000,
418 .desc.n_voltages = ((1570) - (300))/(10) + 1,
419 .current_limits = da9062_buck_a_limits,
420 .n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
421 .desc.enable_reg = DA9062AA_BUCK1_CONT,
422 .desc.enable_mask = DA9062AA_BUCK1_EN_MASK,
423 .desc.vsel_reg = DA9062AA_VBUCK1_A,
424 .desc.vsel_mask = DA9062AA_VBUCK1_A_MASK,
425 .desc.linear_min_sel = 0,
426 .sleep = REG_FIELD(DA9062AA_VBUCK1_A,
427 __builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1,
428 sizeof(unsigned int) * 8 -
429 __builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1),
430 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B,
431 __builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1,
432 sizeof(unsigned int) * 8 -
433 __builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1),
434 .suspend_vsel_reg = DA9062AA_VBUCK1_B,
435 .mode = REG_FIELD(DA9062AA_BUCK1_CFG,
436 __builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1,
437 sizeof(unsigned int) * 8 -
438 __builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1),
439 .suspend = REG_FIELD(DA9062AA_DVC_1,
440 __builtin_ffs((int)DA9062AA_VBUCK1_SEL_MASK) - 1,
441 sizeof(unsigned int) * 8 -
442 __builtin_clz((DA9062AA_VBUCK1_SEL_MASK)) - 1),
443 .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_C,
444 __builtin_ffs((int)DA9062AA_BUCK1_ILIM_MASK) - 1,
445 sizeof(unsigned int) * 8 -
446 __builtin_clz((DA9062AA_BUCK1_ILIM_MASK)) - 1),
447 },
448 {
449 .desc.id = DA9062_ID_BUCK2,
450 .desc.name = "DA9062 BUCK2",
451 .desc.of_match = of_match_ptr("buck2"),
452 .desc.regulators_node = of_match_ptr("regulators"),
453 .desc.ops = &da9062_buck_ops,
454 .desc.min_uV = (300) * 1000,
455 .desc.uV_step = (10) * 1000,
456 .desc.n_voltages = ((1570) - (300))/(10) + 1,
457 .current_limits = da9062_buck_a_limits,
458 .n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
459 .desc.enable_reg = DA9062AA_BUCK2_CONT,
460 .desc.enable_mask = DA9062AA_BUCK2_EN_MASK,
461 .desc.vsel_reg = DA9062AA_VBUCK2_A,
462 .desc.vsel_mask = DA9062AA_VBUCK2_A_MASK,
463 .desc.linear_min_sel = 0,
464 .sleep = REG_FIELD(DA9062AA_VBUCK2_A,
465 __builtin_ffs((int)DA9062AA_BUCK2_SL_A_MASK) - 1,
466 sizeof(unsigned int) * 8 -
467 __builtin_clz((DA9062AA_BUCK2_SL_A_MASK)) - 1),
468 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK2_B,
469 __builtin_ffs((int)DA9062AA_BUCK2_SL_B_MASK) - 1,
470 sizeof(unsigned int) * 8 -
471 __builtin_clz((DA9062AA_BUCK2_SL_B_MASK)) - 1),
472 .suspend_vsel_reg = DA9062AA_VBUCK2_B,
473 .mode = REG_FIELD(DA9062AA_BUCK2_CFG,
474 __builtin_ffs((int)DA9062AA_BUCK2_MODE_MASK) - 1,
475 sizeof(unsigned int) * 8 -
476 __builtin_clz((DA9062AA_BUCK2_MODE_MASK)) - 1),
477 .suspend = REG_FIELD(DA9062AA_DVC_1,
478 __builtin_ffs((int)DA9062AA_VBUCK2_SEL_MASK) - 1,
479 sizeof(unsigned int) * 8 -
480 __builtin_clz((DA9062AA_VBUCK2_SEL_MASK)) - 1),
481 .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_C,
482 __builtin_ffs((int)DA9062AA_BUCK2_ILIM_MASK) - 1,
483 sizeof(unsigned int) * 8 -
484 __builtin_clz((DA9062AA_BUCK2_ILIM_MASK)) - 1),
485 },
486 {
487 .desc.id = DA9062_ID_BUCK3,
488 .desc.name = "DA9062 BUCK3",
489 .desc.of_match = of_match_ptr("buck3"),
490 .desc.regulators_node = of_match_ptr("regulators"),
491 .desc.ops = &da9062_buck_ops,
492 .desc.min_uV = (800) * 1000,
493 .desc.uV_step = (20) * 1000,
494 .desc.n_voltages = ((3340) - (800))/(20) + 1,
495 .current_limits = da9062_buck_b_limits,
496 .n_current_limits = ARRAY_SIZE(da9062_buck_b_limits),
497 .desc.enable_reg = DA9062AA_BUCK3_CONT,
498 .desc.enable_mask = DA9062AA_BUCK3_EN_MASK,
499 .desc.vsel_reg = DA9062AA_VBUCK3_A,
500 .desc.vsel_mask = DA9062AA_VBUCK3_A_MASK,
501 .desc.linear_min_sel = 0,
502 .sleep = REG_FIELD(DA9062AA_VBUCK3_A,
503 __builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1,
504 sizeof(unsigned int) * 8 -
505 __builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1),
506 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B,
507 __builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1,
508 sizeof(unsigned int) * 8 -
509 __builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1),
510 .suspend_vsel_reg = DA9062AA_VBUCK3_B,
511 .mode = REG_FIELD(DA9062AA_BUCK3_CFG,
512 __builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1,
513 sizeof(unsigned int) * 8 -
514 __builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1),
515 .suspend = REG_FIELD(DA9062AA_DVC_1,
516 __builtin_ffs((int)DA9062AA_VBUCK3_SEL_MASK) - 1,
517 sizeof(unsigned int) * 8 -
518 __builtin_clz((DA9062AA_VBUCK3_SEL_MASK)) - 1),
519 .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_A,
520 __builtin_ffs((int)DA9062AA_BUCK3_ILIM_MASK) - 1,
521 sizeof(unsigned int) * 8 -
522 __builtin_clz((DA9062AA_BUCK3_ILIM_MASK)) - 1),
523 },
524 {
525 .desc.id = DA9062_ID_BUCK4,
526 .desc.name = "DA9062 BUCK4",
527 .desc.of_match = of_match_ptr("buck4"),
528 .desc.regulators_node = of_match_ptr("regulators"),
529 .desc.ops = &da9062_buck_ops,
530 .desc.min_uV = (530) * 1000,
531 .desc.uV_step = (10) * 1000,
532 .desc.n_voltages = ((1800) - (530))/(10) + 1,
533 .current_limits = da9062_buck_a_limits,
534 .n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
535 .desc.enable_reg = DA9062AA_BUCK4_CONT,
536 .desc.enable_mask = DA9062AA_BUCK4_EN_MASK,
537 .desc.vsel_reg = DA9062AA_VBUCK4_A,
538 .desc.vsel_mask = DA9062AA_VBUCK4_A_MASK,
539 .desc.linear_min_sel = 0,
540 .sleep = REG_FIELD(DA9062AA_VBUCK4_A,
541 __builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1,
542 sizeof(unsigned int) * 8 -
543 __builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1),
544 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B,
545 __builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1,
546 sizeof(unsigned int) * 8 -
547 __builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1),
548 .suspend_vsel_reg = DA9062AA_VBUCK4_B,
549 .mode = REG_FIELD(DA9062AA_BUCK4_CFG,
550 __builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1,
551 sizeof(unsigned int) * 8 -
552 __builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1),
553 .suspend = REG_FIELD(DA9062AA_DVC_1,
554 __builtin_ffs((int)DA9062AA_VBUCK4_SEL_MASK) - 1,
555 sizeof(unsigned int) * 8 -
556 __builtin_clz((DA9062AA_VBUCK4_SEL_MASK)) - 1),
557 .ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_B,
558 __builtin_ffs((int)DA9062AA_BUCK4_ILIM_MASK) - 1,
559 sizeof(unsigned int) * 8 -
560 __builtin_clz((DA9062AA_BUCK4_ILIM_MASK)) - 1),
561 },
562 {
563 .desc.id = DA9062_ID_LDO1,
564 .desc.name = "DA9062 LDO1",
565 .desc.of_match = of_match_ptr("ldo1"),
566 .desc.regulators_node = of_match_ptr("regulators"),
567 .desc.ops = &da9062_ldo_ops,
568 .desc.min_uV = (900) * 1000,
569 .desc.uV_step = (50) * 1000,
570 .desc.n_voltages = ((3600) - (900))/(50) + 1,
571 .desc.enable_reg = DA9062AA_LDO1_CONT,
572 .desc.enable_mask = DA9062AA_LDO1_EN_MASK,
573 .desc.vsel_reg = DA9062AA_VLDO1_A,
574 .desc.vsel_mask = DA9062AA_VLDO1_A_MASK,
575 .desc.linear_min_sel = 0,
576 .sleep = REG_FIELD(DA9062AA_VLDO1_A,
577 __builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1,
578 sizeof(unsigned int) * 8 -
579 __builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1),
580 .suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B,
581 __builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1,
582 sizeof(unsigned int) * 8 -
583 __builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1),
584 .suspend_vsel_reg = DA9062AA_VLDO1_B,
585 .suspend = REG_FIELD(DA9062AA_DVC_1,
586 __builtin_ffs((int)DA9062AA_VLDO1_SEL_MASK) - 1,
587 sizeof(unsigned int) * 8 -
588 __builtin_clz((DA9062AA_VLDO1_SEL_MASK)) - 1),
589 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
590 __builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1,
591 sizeof(unsigned int) * 8 -
592 __builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1),
593 },
594 {
595 .desc.id = DA9062_ID_LDO2,
596 .desc.name = "DA9062 LDO2",
597 .desc.of_match = of_match_ptr("ldo2"),
598 .desc.regulators_node = of_match_ptr("regulators"),
599 .desc.ops = &da9062_ldo_ops,
600 .desc.min_uV = (900) * 1000,
601 .desc.uV_step = (50) * 1000,
602 .desc.n_voltages = ((3600) - (600))/(50) + 1,
603 .desc.enable_reg = DA9062AA_LDO2_CONT,
604 .desc.enable_mask = DA9062AA_LDO2_EN_MASK,
605 .desc.vsel_reg = DA9062AA_VLDO2_A,
606 .desc.vsel_mask = DA9062AA_VLDO2_A_MASK,
607 .desc.linear_min_sel = 0,
608 .sleep = REG_FIELD(DA9062AA_VLDO2_A,
609 __builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1,
610 sizeof(unsigned int) * 8 -
611 __builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1),
612 .suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B,
613 __builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1,
614 sizeof(unsigned int) * 8 -
615 __builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1),
616 .suspend_vsel_reg = DA9062AA_VLDO2_B,
617 .suspend = REG_FIELD(DA9062AA_DVC_1,
618 __builtin_ffs((int)DA9062AA_VLDO2_SEL_MASK) - 1,
619 sizeof(unsigned int) * 8 -
620 __builtin_clz((DA9062AA_VLDO2_SEL_MASK)) - 1),
621 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
622 __builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1,
623 sizeof(unsigned int) * 8 -
624 __builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1),
625 },
626 {
627 .desc.id = DA9062_ID_LDO3,
628 .desc.name = "DA9062 LDO3",
629 .desc.of_match = of_match_ptr("ldo3"),
630 .desc.regulators_node = of_match_ptr("regulators"),
631 .desc.ops = &da9062_ldo_ops,
632 .desc.min_uV = (900) * 1000,
633 .desc.uV_step = (50) * 1000,
634 .desc.n_voltages = ((3600) - (900))/(50) + 1,
635 .desc.enable_reg = DA9062AA_LDO3_CONT,
636 .desc.enable_mask = DA9062AA_LDO3_EN_MASK,
637 .desc.vsel_reg = DA9062AA_VLDO3_A,
638 .desc.vsel_mask = DA9062AA_VLDO3_A_MASK,
639 .desc.linear_min_sel = 0,
640 .sleep = REG_FIELD(DA9062AA_VLDO3_A,
641 __builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1,
642 sizeof(unsigned int) * 8 -
643 __builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1),
644 .suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B,
645 __builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1,
646 sizeof(unsigned int) * 8 -
647 __builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1),
648 .suspend_vsel_reg = DA9062AA_VLDO3_B,
649 .suspend = REG_FIELD(DA9062AA_DVC_1,
650 __builtin_ffs((int)DA9062AA_VLDO3_SEL_MASK) - 1,
651 sizeof(unsigned int) * 8 -
652 __builtin_clz((DA9062AA_VLDO3_SEL_MASK)) - 1),
653 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
654 __builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1,
655 sizeof(unsigned int) * 8 -
656 __builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1),
657 },
658 {
659 .desc.id = DA9062_ID_LDO4,
660 .desc.name = "DA9062 LDO4",
661 .desc.of_match = of_match_ptr("ldo4"),
662 .desc.regulators_node = of_match_ptr("regulators"),
663 .desc.ops = &da9062_ldo_ops,
664 .desc.min_uV = (900) * 1000,
665 .desc.uV_step = (50) * 1000,
666 .desc.n_voltages = ((3600) - (900))/(50) + 1,
667 .desc.enable_reg = DA9062AA_LDO4_CONT,
668 .desc.enable_mask = DA9062AA_LDO4_EN_MASK,
669 .desc.vsel_reg = DA9062AA_VLDO4_A,
670 .desc.vsel_mask = DA9062AA_VLDO4_A_MASK,
671 .desc.linear_min_sel = 0,
672 .sleep = REG_FIELD(DA9062AA_VLDO4_A,
673 __builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1,
674 sizeof(unsigned int) * 8 -
675 __builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1),
676 .suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B,
677 __builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1,
678 sizeof(unsigned int) * 8 -
679 __builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1),
680 .suspend_vsel_reg = DA9062AA_VLDO4_B,
681 .suspend = REG_FIELD(DA9062AA_DVC_1,
682 __builtin_ffs((int)DA9062AA_VLDO4_SEL_MASK) - 1,
683 sizeof(unsigned int) * 8 -
684 __builtin_clz((DA9062AA_VLDO4_SEL_MASK)) - 1),
685 .oc_event = REG_FIELD(DA9062AA_STATUS_D,
686 __builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1,
687 sizeof(unsigned int) * 8 -
688 __builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1),
689 },
690};
691
692/* Regulator interrupt handlers */
693static irqreturn_t da9062_ldo_lim_event(int irq, void *data)
694{
695 struct da9062_regulators *regulators = data;
696 struct da9062 *hw = regulators->regulator[0].hw;
697 struct da9062_regulator *regl;
698 int handled = IRQ_NONE;
699 int bits, i, ret;
700
701 ret = regmap_read(hw->regmap, DA9062AA_STATUS_D, &bits);
702 if (ret < 0) {
703 dev_err(hw->dev,
704 "Failed to read LDO overcurrent indicator\n");
705 goto ldo_lim_error;
706 }
707
708 for (i = regulators->n_regulators - 1; i >= 0; i--) {
709 regl = &regulators->regulator[i];
710 if (regl->info->oc_event.reg != DA9062AA_STATUS_D)
711 continue;
712
713 if (BIT(regl->info->oc_event.lsb) & bits) {
714 regulator_notifier_call_chain(regl->rdev,
715 REGULATOR_EVENT_OVER_CURRENT, NULL);
716 handled = IRQ_HANDLED;
717 }
718 }
719
720ldo_lim_error:
721 return handled;
722}
723
724static int da9062_regulator_probe(struct platform_device *pdev)
725{
726 struct da9062 *chip = dev_get_drvdata(pdev->dev.parent);
S Twiss4068e512015-05-19 14:10:30 +0100727 struct da9062_regulators *regulators;
728 struct da9062_regulator *regl;
729 struct regulator_config config = { };
730 int irq, n, ret;
731 size_t size;
732
733 /* Allocate memory required by usable regulators */
734 size = sizeof(struct da9062_regulators) +
735 DA9062_MAX_REGULATORS * sizeof(struct da9062_regulator);
736 regulators = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
737 if (!regulators)
738 return -ENOMEM;
739
740 regulators->n_regulators = DA9062_MAX_REGULATORS;
741 platform_set_drvdata(pdev, regulators);
742
743 n = 0;
744 while (n < regulators->n_regulators) {
745 /* Initialise regulator structure */
746 regl = &regulators->regulator[n];
747 regl->hw = chip;
748 regl->info = &local_regulator_info[n];
749 regl->desc = regl->info->desc;
750 regl->desc.type = REGULATOR_VOLTAGE;
751 regl->desc.owner = THIS_MODULE;
752
753 if (regl->info->mode.reg)
754 regl->mode = devm_regmap_field_alloc(
755 &pdev->dev,
756 chip->regmap,
757 regl->info->mode);
758 if (regl->info->suspend.reg)
759 regl->suspend = devm_regmap_field_alloc(
760 &pdev->dev,
761 chip->regmap,
762 regl->info->suspend);
763 if (regl->info->sleep.reg)
764 regl->sleep = devm_regmap_field_alloc(
765 &pdev->dev,
766 chip->regmap,
767 regl->info->sleep);
768 if (regl->info->suspend_sleep.reg)
769 regl->suspend_sleep = devm_regmap_field_alloc(
770 &pdev->dev,
771 chip->regmap,
772 regl->info->suspend_sleep);
773 if (regl->info->ilimit.reg)
774 regl->ilimit = devm_regmap_field_alloc(
775 &pdev->dev,
776 chip->regmap,
777 regl->info->ilimit);
778
779 /* Register regulator */
780 memset(&config, 0, sizeof(config));
781 config.dev = chip->dev;
782 config.driver_data = regl;
783 config.regmap = chip->regmap;
784
785 regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
786 &config);
787 if (IS_ERR(regl->rdev)) {
788 dev_err(&pdev->dev,
789 "Failed to register %s regulator\n",
790 regl->desc.name);
791 return PTR_ERR(regl->rdev);
792 }
793
794 n++;
795 }
796
797 /* LDOs overcurrent event support */
798 irq = platform_get_irq_byname(pdev, "LDO_LIM");
799 if (irq < 0) {
800 dev_err(&pdev->dev, "Failed to get IRQ.\n");
801 return irq;
802 }
803 regulators->irq_ldo_lim = irq;
804
805 ret = devm_request_threaded_irq(&pdev->dev, irq,
806 NULL, da9062_ldo_lim_event,
807 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
808 "LDO_LIM", regulators);
809 if (ret) {
810 dev_warn(&pdev->dev,
811 "Failed to request LDO_LIM IRQ.\n");
812 regulators->irq_ldo_lim = -ENXIO;
813 }
814
815 return 0;
816}
817
818static struct platform_driver da9062_regulator_driver = {
819 .driver = {
820 .name = "da9062-regulators",
S Twiss4068e512015-05-19 14:10:30 +0100821 },
822 .probe = da9062_regulator_probe,
823};
824
825static int __init da9062_regulator_init(void)
826{
827 return platform_driver_register(&da9062_regulator_driver);
828}
829subsys_initcall(da9062_regulator_init);
830
831static void __exit da9062_regulator_cleanup(void)
832{
833 platform_driver_unregister(&da9062_regulator_driver);
834}
835module_exit(da9062_regulator_cleanup);
836
837/* Module information */
838MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
839MODULE_DESCRIPTION("REGULATOR device driver for Dialog DA9062");
840MODULE_LICENSE("GPL");
Axel Linea6254e2015-05-25 16:32:38 +0800841MODULE_ALIAS("platform:da9062-regulators");