x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / regulator / da9063-regulator.c
blobb5b11a545e14f6db40e3bfe7a122c84d457eda50
2 /*
3 * Regulator driver for DA9063 PMIC series
5 * Copyright 2012 Dialog Semiconductors Ltd.
6 * Copyright 2013 Philipp Zabel, Pengutronix
8 * Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/of.h>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/machine.h>
26 #include <linux/regulator/of_regulator.h>
27 #include <linux/mfd/da9063/core.h>
28 #include <linux/mfd/da9063/pdata.h>
29 #include <linux/mfd/da9063/registers.h>
32 /* Definition for registering regmap bit fields using a mask */
33 #define BFIELD(_reg, _mask) \
34 REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \
35 sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1)
37 /* Regulator capabilities and registers description */
38 struct da9063_regulator_info {
39 struct regulator_desc desc;
41 /* Current limiting */
42 unsigned n_current_limits;
43 const int *current_limits;
45 /* DA9063 main register fields */
46 struct reg_field mode; /* buck mode of operation */
47 struct reg_field suspend;
48 struct reg_field sleep;
49 struct reg_field suspend_sleep;
50 unsigned int suspend_vsel_reg;
51 struct reg_field ilimit;
53 /* DA9063 event detection bit */
54 struct reg_field oc_event;
57 /* Macros for LDO */
58 #define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \
59 .desc.id = chip##_ID_##regl_name, \
60 .desc.name = __stringify(chip##_##regl_name), \
61 .desc.ops = &da9063_ldo_ops, \
62 .desc.min_uV = (min_mV) * 1000, \
63 .desc.uV_step = (step_mV) * 1000, \
64 .desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1 \
65 + (DA9063_V##regl_name##_BIAS)), \
66 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
67 .desc.enable_mask = DA9063_LDO_EN, \
68 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
69 .desc.vsel_mask = DA9063_V##regl_name##_MASK, \
70 .desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \
71 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \
72 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \
73 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B
75 /* Macros for voltage DC/DC converters (BUCKs) */
76 #define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array) \
77 .desc.id = chip##_ID_##regl_name, \
78 .desc.name = __stringify(chip##_##regl_name), \
79 .desc.ops = &da9063_buck_ops, \
80 .desc.min_uV = (min_mV) * 1000, \
81 .desc.uV_step = (step_mV) * 1000, \
82 .desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \
83 .current_limits = limits_array, \
84 .n_current_limits = ARRAY_SIZE(limits_array)
86 #define DA9063_BUCK_COMMON_FIELDS(regl_name) \
87 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
88 .desc.enable_mask = DA9063_BUCK_EN, \
89 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
90 .desc.vsel_mask = DA9063_VBUCK_MASK, \
91 .desc.linear_min_sel = DA9063_VBUCK_BIAS, \
92 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \
93 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \
94 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \
95 .mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK)
97 /* Defines asignment of regulators info table to chip model */
98 struct da9063_dev_model {
99 const struct da9063_regulator_info *regulator_info;
100 unsigned n_regulators;
101 unsigned dev_model;
104 /* Single regulator settings */
105 struct da9063_regulator {
106 struct regulator_desc desc;
107 struct regulator_dev *rdev;
108 struct da9063 *hw;
109 const struct da9063_regulator_info *info;
111 struct regmap_field *mode;
112 struct regmap_field *suspend;
113 struct regmap_field *sleep;
114 struct regmap_field *suspend_sleep;
115 struct regmap_field *ilimit;
118 /* Encapsulates all information for the regulators driver */
119 struct da9063_regulators {
120 int irq_ldo_lim;
121 int irq_uvov;
123 unsigned n_regulators;
124 /* Array size to be defined during init. Keep at end. */
125 struct da9063_regulator regulator[0];
128 /* BUCK modes for DA9063 */
129 enum {
130 BUCK_MODE_MANUAL, /* 0 */
131 BUCK_MODE_SLEEP, /* 1 */
132 BUCK_MODE_SYNC, /* 2 */
133 BUCK_MODE_AUTO /* 3 */
136 /* Regulator operations */
138 /* Current limits array (in uA) for BCORE1, BCORE2, BPRO.
139 Entry indexes corresponds to register values. */
140 static const int da9063_buck_a_limits[] = {
141 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
142 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
145 /* Current limits array (in uA) for BMEM, BIO, BPERI.
146 Entry indexes corresponds to register values. */
147 static const int da9063_buck_b_limits[] = {
148 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
149 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
152 /* Current limits array (in uA) for merged BCORE1 and BCORE2.
153 Entry indexes corresponds to register values. */
154 static const int da9063_bcores_merged_limits[] = {
155 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
156 2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
159 /* Current limits array (in uA) for merged BMEM and BIO.
160 Entry indexes corresponds to register values. */
161 static const int da9063_bmem_bio_merged_limits[] = {
162 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
163 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
166 static int da9063_set_current_limit(struct regulator_dev *rdev,
167 int min_uA, int max_uA)
169 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
170 const struct da9063_regulator_info *rinfo = regl->info;
171 int n, tval;
173 for (n = 0; n < rinfo->n_current_limits; n++) {
174 tval = rinfo->current_limits[n];
175 if (tval >= min_uA && tval <= max_uA)
176 return regmap_field_write(regl->ilimit, n);
179 return -EINVAL;
182 static int da9063_get_current_limit(struct regulator_dev *rdev)
184 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
185 const struct da9063_regulator_info *rinfo = regl->info;
186 unsigned int sel;
187 int ret;
189 ret = regmap_field_read(regl->ilimit, &sel);
190 if (ret < 0)
191 return ret;
193 if (sel >= rinfo->n_current_limits)
194 sel = rinfo->n_current_limits - 1;
196 return rinfo->current_limits[sel];
199 static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
201 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
202 unsigned val;
204 switch (mode) {
205 case REGULATOR_MODE_FAST:
206 val = BUCK_MODE_SYNC;
207 break;
208 case REGULATOR_MODE_NORMAL:
209 val = BUCK_MODE_AUTO;
210 break;
211 case REGULATOR_MODE_STANDBY:
212 val = BUCK_MODE_SLEEP;
213 break;
214 default:
215 return -EINVAL;
218 return regmap_field_write(regl->mode, val);
222 * Bucks use single mode register field for normal operation
223 * and suspend state.
224 * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
227 static unsigned da9063_buck_get_mode(struct regulator_dev *rdev)
229 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
230 struct regmap_field *field;
231 unsigned int val, mode = 0;
232 int ret;
234 ret = regmap_field_read(regl->mode, &val);
235 if (ret < 0)
236 return ret;
238 switch (val) {
239 default:
240 case BUCK_MODE_MANUAL:
241 mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
242 /* Sleep flag bit decides the mode */
243 break;
244 case BUCK_MODE_SLEEP:
245 return REGULATOR_MODE_STANDBY;
246 case BUCK_MODE_SYNC:
247 return REGULATOR_MODE_FAST;
248 case BUCK_MODE_AUTO:
249 return REGULATOR_MODE_NORMAL;
252 /* Detect current regulator state */
253 ret = regmap_field_read(regl->suspend, &val);
254 if (ret < 0)
255 return 0;
257 /* Read regulator mode from proper register, depending on state */
258 if (val)
259 field = regl->suspend_sleep;
260 else
261 field = regl->sleep;
263 ret = regmap_field_read(field, &val);
264 if (ret < 0)
265 return 0;
267 if (val)
268 mode &= REGULATOR_MODE_STANDBY;
269 else
270 mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
272 return mode;
276 * LDOs use sleep flags - one for normal and one for suspend state.
277 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
280 static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
282 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
283 unsigned val;
285 switch (mode) {
286 case REGULATOR_MODE_NORMAL:
287 val = 0;
288 break;
289 case REGULATOR_MODE_STANDBY:
290 val = 1;
291 break;
292 default:
293 return -EINVAL;
296 return regmap_field_write(regl->sleep, val);
299 static unsigned da9063_ldo_get_mode(struct regulator_dev *rdev)
301 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
302 struct regmap_field *field;
303 int ret, val;
305 /* Detect current regulator state */
306 ret = regmap_field_read(regl->suspend, &val);
307 if (ret < 0)
308 return 0;
310 /* Read regulator mode from proper register, depending on state */
311 if (val)
312 field = regl->suspend_sleep;
313 else
314 field = regl->sleep;
316 ret = regmap_field_read(field, &val);
317 if (ret < 0)
318 return 0;
320 if (val)
321 return REGULATOR_MODE_STANDBY;
322 else
323 return REGULATOR_MODE_NORMAL;
326 static int da9063_buck_get_status(struct regulator_dev *rdev)
328 int ret = regulator_is_enabled_regmap(rdev);
330 if (ret == 0) {
331 ret = REGULATOR_STATUS_OFF;
332 } else if (ret > 0) {
333 ret = da9063_buck_get_mode(rdev);
334 if (ret > 0)
335 ret = regulator_mode_to_status(ret);
336 else if (ret == 0)
337 ret = -EIO;
340 return ret;
343 static int da9063_ldo_get_status(struct regulator_dev *rdev)
345 int ret = regulator_is_enabled_regmap(rdev);
347 if (ret == 0) {
348 ret = REGULATOR_STATUS_OFF;
349 } else if (ret > 0) {
350 ret = da9063_ldo_get_mode(rdev);
351 if (ret > 0)
352 ret = regulator_mode_to_status(ret);
353 else if (ret == 0)
354 ret = -EIO;
357 return ret;
360 static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
362 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
363 const struct da9063_regulator_info *rinfo = regl->info;
364 int ret, sel;
366 sel = regulator_map_voltage_linear(rdev, uV, uV);
367 if (sel < 0)
368 return -EINVAL;
370 sel <<= ffs(rdev->desc->vsel_mask) - 1;
372 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
373 rdev->desc->vsel_mask, sel);
375 return ret;
378 static int da9063_suspend_enable(struct regulator_dev *rdev)
380 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
382 return regmap_field_write(regl->suspend, 1);
385 static int da9063_suspend_disable(struct regulator_dev *rdev)
387 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
389 return regmap_field_write(regl->suspend, 0);
392 static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
394 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
395 int val;
397 switch (mode) {
398 case REGULATOR_MODE_FAST:
399 val = BUCK_MODE_SYNC;
400 break;
401 case REGULATOR_MODE_NORMAL:
402 val = BUCK_MODE_AUTO;
403 break;
404 case REGULATOR_MODE_STANDBY:
405 val = BUCK_MODE_SLEEP;
406 break;
407 default:
408 return -EINVAL;
411 return regmap_field_write(regl->mode, val);
414 static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
416 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
417 unsigned val;
419 switch (mode) {
420 case REGULATOR_MODE_NORMAL:
421 val = 0;
422 break;
423 case REGULATOR_MODE_STANDBY:
424 val = 1;
425 break;
426 default:
427 return -EINVAL;
430 return regmap_field_write(regl->suspend_sleep, val);
433 static struct regulator_ops da9063_buck_ops = {
434 .enable = regulator_enable_regmap,
435 .disable = regulator_disable_regmap,
436 .is_enabled = regulator_is_enabled_regmap,
437 .get_voltage_sel = regulator_get_voltage_sel_regmap,
438 .set_voltage_sel = regulator_set_voltage_sel_regmap,
439 .list_voltage = regulator_list_voltage_linear,
440 .set_current_limit = da9063_set_current_limit,
441 .get_current_limit = da9063_get_current_limit,
442 .set_mode = da9063_buck_set_mode,
443 .get_mode = da9063_buck_get_mode,
444 .get_status = da9063_buck_get_status,
445 .set_suspend_voltage = da9063_set_suspend_voltage,
446 .set_suspend_enable = da9063_suspend_enable,
447 .set_suspend_disable = da9063_suspend_disable,
448 .set_suspend_mode = da9063_buck_set_suspend_mode,
451 static struct regulator_ops da9063_ldo_ops = {
452 .enable = regulator_enable_regmap,
453 .disable = regulator_disable_regmap,
454 .is_enabled = regulator_is_enabled_regmap,
455 .get_voltage_sel = regulator_get_voltage_sel_regmap,
456 .set_voltage_sel = regulator_set_voltage_sel_regmap,
457 .list_voltage = regulator_list_voltage_linear,
458 .set_mode = da9063_ldo_set_mode,
459 .get_mode = da9063_ldo_get_mode,
460 .get_status = da9063_ldo_get_status,
461 .set_suspend_voltage = da9063_set_suspend_voltage,
462 .set_suspend_enable = da9063_suspend_enable,
463 .set_suspend_disable = da9063_suspend_disable,
464 .set_suspend_mode = da9063_ldo_set_suspend_mode,
467 /* Info of regulators for DA9063 */
468 static const struct da9063_regulator_info da9063_regulator_info[] = {
470 DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
471 da9063_buck_a_limits),
472 DA9063_BUCK_COMMON_FIELDS(BCORE1),
473 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
474 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
475 DA9063_BCORE1_ILIM_MASK),
478 DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
479 da9063_buck_a_limits),
480 DA9063_BUCK_COMMON_FIELDS(BCORE2),
481 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE2_SEL),
482 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
483 DA9063_BCORE2_ILIM_MASK),
486 DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
487 da9063_buck_a_limits),
488 DA9063_BUCK_COMMON_FIELDS(BPRO),
489 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPRO_SEL),
490 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B,
491 DA9063_BPRO_ILIM_MASK),
494 DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
495 da9063_buck_b_limits),
496 DA9063_BUCK_COMMON_FIELDS(BMEM),
497 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
498 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
499 DA9063_BMEM_ILIM_MASK),
502 DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
503 da9063_buck_b_limits),
504 DA9063_BUCK_COMMON_FIELDS(BIO),
505 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VBIO_SEL),
506 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
507 DA9063_BIO_ILIM_MASK),
510 DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
511 da9063_buck_b_limits),
512 DA9063_BUCK_COMMON_FIELDS(BPERI),
513 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPERI_SEL),
514 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B,
515 DA9063_BPERI_ILIM_MASK),
518 DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
519 da9063_bcores_merged_limits),
520 /* BCORES_MERGED uses the same register fields as BCORE1 */
521 DA9063_BUCK_COMMON_FIELDS(BCORE1),
522 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
523 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
524 DA9063_BCORE1_ILIM_MASK),
527 DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
528 da9063_bmem_bio_merged_limits),
529 /* BMEM_BIO_MERGED uses the same register fields as BMEM */
530 DA9063_BUCK_COMMON_FIELDS(BMEM),
531 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
532 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
533 DA9063_BMEM_ILIM_MASK),
536 DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
537 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO1_SEL),
540 DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
541 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO2_SEL),
544 DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
545 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO3_SEL),
546 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
549 DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
550 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VLDO4_SEL),
551 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
554 DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
555 .suspend = BFIELD(DA9063_REG_LDO5_CONT, DA9063_VLDO5_SEL),
558 DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
559 .suspend = BFIELD(DA9063_REG_LDO6_CONT, DA9063_VLDO6_SEL),
562 DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
563 .suspend = BFIELD(DA9063_REG_LDO7_CONT, DA9063_VLDO7_SEL),
564 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
567 DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
568 .suspend = BFIELD(DA9063_REG_LDO8_CONT, DA9063_VLDO8_SEL),
569 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
572 DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
573 .suspend = BFIELD(DA9063_REG_LDO9_CONT, DA9063_VLDO9_SEL),
576 DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
577 .suspend = BFIELD(DA9063_REG_LDO10_CONT, DA9063_VLDO10_SEL),
580 DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
581 .suspend = BFIELD(DA9063_REG_LDO11_CONT, DA9063_VLDO11_SEL),
582 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
586 /* Link chip model with regulators info table */
587 static struct da9063_dev_model regulators_models[] = {
589 .regulator_info = da9063_regulator_info,
590 .n_regulators = ARRAY_SIZE(da9063_regulator_info),
591 .dev_model = PMIC_DA9063,
596 /* Regulator interrupt handlers */
597 static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
599 struct da9063_regulators *regulators = data;
600 struct da9063 *hw = regulators->regulator[0].hw;
601 struct da9063_regulator *regl;
602 int bits, i , ret;
604 ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
605 if (ret < 0)
606 return IRQ_NONE;
608 for (i = regulators->n_regulators - 1; i >= 0; i--) {
609 regl = &regulators->regulator[i];
610 if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
611 continue;
613 if (BIT(regl->info->oc_event.lsb) & bits)
614 regulator_notifier_call_chain(regl->rdev,
615 REGULATOR_EVENT_OVER_CURRENT, NULL);
618 return IRQ_HANDLED;
622 * Probing and Initialisation functions
624 static const struct regulator_init_data *da9063_get_regulator_initdata(
625 const struct da9063_regulators_pdata *regl_pdata, int id)
627 int i;
629 for (i = 0; i < regl_pdata->n_regulators; i++) {
630 if (id == regl_pdata->regulator_data[i].id)
631 return regl_pdata->regulator_data[i].initdata;
634 return NULL;
637 #ifdef CONFIG_OF
638 static struct of_regulator_match da9063_matches[] = {
639 [DA9063_ID_BCORE1] = { .name = "bcore1" },
640 [DA9063_ID_BCORE2] = { .name = "bcore2" },
641 [DA9063_ID_BPRO] = { .name = "bpro", },
642 [DA9063_ID_BMEM] = { .name = "bmem", },
643 [DA9063_ID_BIO] = { .name = "bio", },
644 [DA9063_ID_BPERI] = { .name = "bperi", },
645 [DA9063_ID_BCORES_MERGED] = { .name = "bcores-merged" },
646 [DA9063_ID_BMEM_BIO_MERGED] = { .name = "bmem-bio-merged", },
647 [DA9063_ID_LDO1] = { .name = "ldo1", },
648 [DA9063_ID_LDO2] = { .name = "ldo2", },
649 [DA9063_ID_LDO3] = { .name = "ldo3", },
650 [DA9063_ID_LDO4] = { .name = "ldo4", },
651 [DA9063_ID_LDO5] = { .name = "ldo5", },
652 [DA9063_ID_LDO6] = { .name = "ldo6", },
653 [DA9063_ID_LDO7] = { .name = "ldo7", },
654 [DA9063_ID_LDO8] = { .name = "ldo8", },
655 [DA9063_ID_LDO9] = { .name = "ldo9", },
656 [DA9063_ID_LDO10] = { .name = "ldo10", },
657 [DA9063_ID_LDO11] = { .name = "ldo11", },
660 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
661 struct platform_device *pdev,
662 struct of_regulator_match **da9063_reg_matches)
664 struct da9063_regulators_pdata *pdata;
665 struct da9063_regulator_data *rdata;
666 struct device_node *node;
667 int i, n, num;
669 node = of_find_node_by_name(pdev->dev.parent->of_node, "regulators");
670 if (!node) {
671 dev_err(&pdev->dev, "Regulators device node not found\n");
672 return ERR_PTR(-ENODEV);
675 num = of_regulator_match(&pdev->dev, node, da9063_matches,
676 ARRAY_SIZE(da9063_matches));
677 if (num < 0) {
678 dev_err(&pdev->dev, "Failed to match regulators\n");
679 return ERR_PTR(-EINVAL);
682 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
683 if (!pdata)
684 return ERR_PTR(-ENOMEM);
686 pdata->regulator_data = devm_kzalloc(&pdev->dev,
687 num * sizeof(*pdata->regulator_data),
688 GFP_KERNEL);
689 if (!pdata->regulator_data)
690 return ERR_PTR(-ENOMEM);
691 pdata->n_regulators = num;
693 n = 0;
694 for (i = 0; i < ARRAY_SIZE(da9063_matches); i++) {
695 if (!da9063_matches[i].init_data)
696 continue;
698 rdata = &pdata->regulator_data[n];
699 rdata->id = i;
700 rdata->initdata = da9063_matches[i].init_data;
702 n++;
705 *da9063_reg_matches = da9063_matches;
706 return pdata;
708 #else
709 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
710 struct platform_device *pdev,
711 struct of_regulator_match **da9063_reg_matches)
713 da9063_reg_matches = NULL;
714 return ERR_PTR(-ENODEV);
716 #endif
718 static int da9063_regulator_probe(struct platform_device *pdev)
720 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
721 struct da9063_pdata *da9063_pdata = dev_get_platdata(da9063->dev);
722 struct of_regulator_match *da9063_reg_matches;
723 struct da9063_regulators_pdata *regl_pdata;
724 const struct da9063_dev_model *model;
725 struct da9063_regulators *regulators;
726 struct da9063_regulator *regl;
727 struct regulator_config config;
728 bool bcores_merged, bmem_bio_merged;
729 int id, irq, n, n_regulators, ret, val;
730 size_t size;
732 regl_pdata = da9063_pdata ? da9063_pdata->regulators_pdata : NULL;
734 if (!regl_pdata)
735 regl_pdata = da9063_parse_regulators_dt(pdev,
736 &da9063_reg_matches);
738 if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
739 dev_err(&pdev->dev,
740 "No regulators defined for the platform\n");
741 return PTR_ERR(regl_pdata);
744 /* Find regulators set for particular device model */
745 for (model = regulators_models; model->regulator_info; model++) {
746 if (model->dev_model == da9063->model)
747 break;
749 if (!model->regulator_info) {
750 dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
751 da9063->model);
752 return -ENODEV;
755 ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
756 if (ret < 0) {
757 dev_err(&pdev->dev,
758 "Error while reading BUCKs configuration\n");
759 return -EIO;
761 bcores_merged = val & DA9063_BCORE_MERGE;
762 bmem_bio_merged = val & DA9063_BUCK_MERGE;
764 n_regulators = model->n_regulators;
765 if (bcores_merged)
766 n_regulators -= 2; /* remove BCORE1, BCORE2 */
767 else
768 n_regulators--; /* remove BCORES_MERGED */
769 if (bmem_bio_merged)
770 n_regulators -= 2; /* remove BMEM, BIO */
771 else
772 n_regulators--; /* remove BMEM_BIO_MERGED */
774 /* Allocate memory required by usable regulators */
775 size = sizeof(struct da9063_regulators) +
776 n_regulators * sizeof(struct da9063_regulator);
777 regulators = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
778 if (!regulators) {
779 dev_err(&pdev->dev, "No memory for regulators\n");
780 return -ENOMEM;
783 regulators->n_regulators = n_regulators;
784 platform_set_drvdata(pdev, regulators);
786 /* Register all regulators declared in platform information */
787 n = 0;
788 id = 0;
789 while (n < regulators->n_regulators) {
790 /* Skip regulator IDs depending on merge mode configuration */
791 switch (id) {
792 case DA9063_ID_BCORE1:
793 case DA9063_ID_BCORE2:
794 if (bcores_merged) {
795 id++;
796 continue;
798 break;
799 case DA9063_ID_BMEM:
800 case DA9063_ID_BIO:
801 if (bmem_bio_merged) {
802 id++;
803 continue;
805 break;
806 case DA9063_ID_BCORES_MERGED:
807 if (!bcores_merged) {
808 id++;
809 continue;
811 break;
812 case DA9063_ID_BMEM_BIO_MERGED:
813 if (!bmem_bio_merged) {
814 id++;
815 continue;
817 break;
820 /* Initialise regulator structure */
821 regl = &regulators->regulator[n];
822 regl->hw = da9063;
823 regl->info = &model->regulator_info[id];
824 regl->desc = regl->info->desc;
825 regl->desc.type = REGULATOR_VOLTAGE;
826 regl->desc.owner = THIS_MODULE;
828 if (regl->info->mode.reg)
829 regl->mode = devm_regmap_field_alloc(&pdev->dev,
830 da9063->regmap, regl->info->mode);
831 if (regl->info->suspend.reg)
832 regl->suspend = devm_regmap_field_alloc(&pdev->dev,
833 da9063->regmap, regl->info->suspend);
834 if (regl->info->sleep.reg)
835 regl->sleep = devm_regmap_field_alloc(&pdev->dev,
836 da9063->regmap, regl->info->sleep);
837 if (regl->info->suspend_sleep.reg)
838 regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
839 da9063->regmap, regl->info->suspend_sleep);
840 if (regl->info->ilimit.reg)
841 regl->ilimit = devm_regmap_field_alloc(&pdev->dev,
842 da9063->regmap, regl->info->ilimit);
844 /* Register regulator */
845 memset(&config, 0, sizeof(config));
846 config.dev = &pdev->dev;
847 config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
848 config.driver_data = regl;
849 if (da9063_reg_matches)
850 config.of_node = da9063_reg_matches[id].of_node;
851 config.regmap = da9063->regmap;
852 regl->rdev = regulator_register(&regl->desc, &config);
853 if (IS_ERR(regl->rdev)) {
854 dev_err(&pdev->dev,
855 "Failed to register %s regulator\n",
856 regl->desc.name);
857 ret = PTR_ERR(regl->rdev);
858 goto err;
860 id++;
861 n++;
864 /* LDOs overcurrent event support */
865 irq = platform_get_irq_byname(pdev, "LDO_LIM");
866 if (irq < 0) {
867 ret = irq;
868 dev_err(&pdev->dev, "Failed to get IRQ.\n");
869 goto err;
872 regulators->irq_ldo_lim = regmap_irq_get_virq(da9063->regmap_irq, irq);
873 if (regulators->irq_ldo_lim >= 0) {
874 ret = request_threaded_irq(regulators->irq_ldo_lim,
875 NULL, da9063_ldo_lim_event,
876 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
877 "LDO_LIM", regulators);
878 if (ret) {
879 dev_err(&pdev->dev,
880 "Failed to request LDO_LIM IRQ.\n");
881 regulators->irq_ldo_lim = -ENXIO;
885 return 0;
887 err:
888 /* Wind back regulators registeration */
889 while (--n >= 0)
890 regulator_unregister(regulators->regulator[n].rdev);
892 return ret;
895 static int da9063_regulator_remove(struct platform_device *pdev)
897 struct da9063_regulators *regulators = platform_get_drvdata(pdev);
898 struct da9063_regulator *regl;
900 free_irq(regulators->irq_ldo_lim, regulators);
901 free_irq(regulators->irq_uvov, regulators);
903 for (regl = &regulators->regulator[regulators->n_regulators - 1];
904 regl >= &regulators->regulator[0]; regl--)
905 regulator_unregister(regl->rdev);
907 return 0;
910 static struct platform_driver da9063_regulator_driver = {
911 .driver = {
912 .name = DA9063_DRVNAME_REGULATORS,
913 .owner = THIS_MODULE,
915 .probe = da9063_regulator_probe,
916 .remove = da9063_regulator_remove,
919 static int __init da9063_regulator_init(void)
921 return platform_driver_register(&da9063_regulator_driver);
923 subsys_initcall(da9063_regulator_init);
925 static void __exit da9063_regulator_cleanup(void)
927 platform_driver_unregister(&da9063_regulator_driver);
929 module_exit(da9063_regulator_cleanup);
932 /* Module information */
933 MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
934 MODULE_DESCRIPTION("DA9063 regulators driver");
935 MODULE_LICENSE("GPL");
936 MODULE_ALIAS("paltform:" DA9063_DRVNAME_REGULATORS);