PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / regulator / da9063-regulator.c
blob56727eb745df69171d77fac8313e6ea3ab2499ef
1 /*
2 * Regulator driver for DA9063 PMIC series
4 * Copyright 2012 Dialog Semiconductors Ltd.
5 * Copyright 2013 Philipp Zabel, Pengutronix
7 * Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
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/da9063/core.h>
27 #include <linux/mfd/da9063/pdata.h>
28 #include <linux/mfd/da9063/registers.h>
31 /* Definition for registering regmap bit fields using a mask */
32 #define BFIELD(_reg, _mask) \
33 REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \
34 sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1)
36 /* Regulator capabilities and registers description */
37 struct da9063_regulator_info {
38 struct regulator_desc desc;
40 /* Current limiting */
41 unsigned n_current_limits;
42 const int *current_limits;
44 /* DA9063 main register fields */
45 struct reg_field mode; /* buck mode of operation */
46 struct reg_field suspend;
47 struct reg_field sleep;
48 struct reg_field suspend_sleep;
49 unsigned int suspend_vsel_reg;
50 struct reg_field ilimit;
52 /* DA9063 event detection bit */
53 struct reg_field oc_event;
56 /* Macros for LDO */
57 #define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \
58 .desc.id = chip##_ID_##regl_name, \
59 .desc.name = __stringify(chip##_##regl_name), \
60 .desc.ops = &da9063_ldo_ops, \
61 .desc.min_uV = (min_mV) * 1000, \
62 .desc.uV_step = (step_mV) * 1000, \
63 .desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1), \
64 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
65 .desc.enable_mask = DA9063_LDO_EN, \
66 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
67 .desc.vsel_mask = DA9063_V##regl_name##_MASK, \
68 .desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \
69 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \
70 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \
71 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B
73 /* Macros for voltage DC/DC converters (BUCKs) */
74 #define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array) \
75 .desc.id = chip##_ID_##regl_name, \
76 .desc.name = __stringify(chip##_##regl_name), \
77 .desc.ops = &da9063_buck_ops, \
78 .desc.min_uV = (min_mV) * 1000, \
79 .desc.uV_step = (step_mV) * 1000, \
80 .desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \
81 .current_limits = limits_array, \
82 .n_current_limits = ARRAY_SIZE(limits_array)
84 #define DA9063_BUCK_COMMON_FIELDS(regl_name) \
85 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
86 .desc.enable_mask = DA9063_BUCK_EN, \
87 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
88 .desc.vsel_mask = DA9063_VBUCK_MASK, \
89 .desc.linear_min_sel = DA9063_VBUCK_BIAS, \
90 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \
91 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \
92 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \
93 .mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK)
95 /* Defines asignment of regulators info table to chip model */
96 struct da9063_dev_model {
97 const struct da9063_regulator_info *regulator_info;
98 unsigned n_regulators;
99 unsigned dev_model;
102 /* Single regulator settings */
103 struct da9063_regulator {
104 struct regulator_desc desc;
105 struct regulator_dev *rdev;
106 struct da9063 *hw;
107 const struct da9063_regulator_info *info;
109 struct regmap_field *mode;
110 struct regmap_field *suspend;
111 struct regmap_field *sleep;
112 struct regmap_field *suspend_sleep;
113 struct regmap_field *ilimit;
116 /* Encapsulates all information for the regulators driver */
117 struct da9063_regulators {
118 int irq_ldo_lim;
119 int irq_uvov;
121 unsigned n_regulators;
122 /* Array size to be defined during init. Keep at end. */
123 struct da9063_regulator regulator[0];
126 /* BUCK modes for DA9063 */
127 enum {
128 BUCK_MODE_MANUAL, /* 0 */
129 BUCK_MODE_SLEEP, /* 1 */
130 BUCK_MODE_SYNC, /* 2 */
131 BUCK_MODE_AUTO /* 3 */
134 /* Regulator operations */
136 /* Current limits array (in uA) for BCORE1, BCORE2, BPRO.
137 Entry indexes corresponds to register values. */
138 static const int da9063_buck_a_limits[] = {
139 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
140 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
143 /* Current limits array (in uA) for BMEM, BIO, BPERI.
144 Entry indexes corresponds to register values. */
145 static const int da9063_buck_b_limits[] = {
146 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
147 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
150 /* Current limits array (in uA) for merged BCORE1 and BCORE2.
151 Entry indexes corresponds to register values. */
152 static const int da9063_bcores_merged_limits[] = {
153 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
154 2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
157 /* Current limits array (in uA) for merged BMEM and BIO.
158 Entry indexes corresponds to register values. */
159 static const int da9063_bmem_bio_merged_limits[] = {
160 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
161 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
164 static int da9063_set_current_limit(struct regulator_dev *rdev,
165 int min_uA, int max_uA)
167 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
168 const struct da9063_regulator_info *rinfo = regl->info;
169 int n, tval;
171 for (n = 0; n < rinfo->n_current_limits; n++) {
172 tval = rinfo->current_limits[n];
173 if (tval >= min_uA && tval <= max_uA)
174 return regmap_field_write(regl->ilimit, n);
177 return -EINVAL;
180 static int da9063_get_current_limit(struct regulator_dev *rdev)
182 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
183 const struct da9063_regulator_info *rinfo = regl->info;
184 unsigned int sel;
185 int ret;
187 ret = regmap_field_read(regl->ilimit, &sel);
188 if (ret < 0)
189 return ret;
191 if (sel >= rinfo->n_current_limits)
192 sel = rinfo->n_current_limits - 1;
194 return rinfo->current_limits[sel];
197 static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
199 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
200 unsigned val;
202 switch (mode) {
203 case REGULATOR_MODE_FAST:
204 val = BUCK_MODE_SYNC;
205 break;
206 case REGULATOR_MODE_NORMAL:
207 val = BUCK_MODE_AUTO;
208 break;
209 case REGULATOR_MODE_STANDBY:
210 val = BUCK_MODE_SLEEP;
211 break;
212 default:
213 return -EINVAL;
216 return regmap_field_write(regl->mode, val);
220 * Bucks use single mode register field for normal operation
221 * and suspend state.
222 * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
225 static unsigned da9063_buck_get_mode(struct regulator_dev *rdev)
227 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
228 struct regmap_field *field;
229 unsigned int val, mode = 0;
230 int ret;
232 ret = regmap_field_read(regl->mode, &val);
233 if (ret < 0)
234 return ret;
236 switch (val) {
237 default:
238 case BUCK_MODE_MANUAL:
239 mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
240 /* Sleep flag bit decides the mode */
241 break;
242 case BUCK_MODE_SLEEP:
243 return REGULATOR_MODE_STANDBY;
244 case BUCK_MODE_SYNC:
245 return REGULATOR_MODE_FAST;
246 case BUCK_MODE_AUTO:
247 return REGULATOR_MODE_NORMAL;
250 /* Detect current regulator state */
251 ret = regmap_field_read(regl->suspend, &val);
252 if (ret < 0)
253 return 0;
255 /* Read regulator mode from proper register, depending on state */
256 if (val)
257 field = regl->suspend_sleep;
258 else
259 field = regl->sleep;
261 ret = regmap_field_read(field, &val);
262 if (ret < 0)
263 return 0;
265 if (val)
266 mode &= REGULATOR_MODE_STANDBY;
267 else
268 mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
270 return mode;
274 * LDOs use sleep flags - one for normal and one for suspend state.
275 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
278 static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
280 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
281 unsigned val;
283 switch (mode) {
284 case REGULATOR_MODE_NORMAL:
285 val = 0;
286 break;
287 case REGULATOR_MODE_STANDBY:
288 val = 1;
289 break;
290 default:
291 return -EINVAL;
294 return regmap_field_write(regl->sleep, val);
297 static unsigned da9063_ldo_get_mode(struct regulator_dev *rdev)
299 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
300 struct regmap_field *field;
301 int ret, val;
303 /* Detect current regulator state */
304 ret = regmap_field_read(regl->suspend, &val);
305 if (ret < 0)
306 return 0;
308 /* Read regulator mode from proper register, depending on state */
309 if (val)
310 field = regl->suspend_sleep;
311 else
312 field = regl->sleep;
314 ret = regmap_field_read(field, &val);
315 if (ret < 0)
316 return 0;
318 if (val)
319 return REGULATOR_MODE_STANDBY;
320 else
321 return REGULATOR_MODE_NORMAL;
324 static int da9063_buck_get_status(struct regulator_dev *rdev)
326 int ret = regulator_is_enabled_regmap(rdev);
328 if (ret == 0) {
329 ret = REGULATOR_STATUS_OFF;
330 } else if (ret > 0) {
331 ret = da9063_buck_get_mode(rdev);
332 if (ret > 0)
333 ret = regulator_mode_to_status(ret);
334 else if (ret == 0)
335 ret = -EIO;
338 return ret;
341 static int da9063_ldo_get_status(struct regulator_dev *rdev)
343 int ret = regulator_is_enabled_regmap(rdev);
345 if (ret == 0) {
346 ret = REGULATOR_STATUS_OFF;
347 } else if (ret > 0) {
348 ret = da9063_ldo_get_mode(rdev);
349 if (ret > 0)
350 ret = regulator_mode_to_status(ret);
351 else if (ret == 0)
352 ret = -EIO;
355 return ret;
358 static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
360 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
361 const struct da9063_regulator_info *rinfo = regl->info;
362 int ret, sel;
364 sel = regulator_map_voltage_linear(rdev, uV, uV);
365 if (sel < 0)
366 return -EINVAL;
368 sel <<= ffs(rdev->desc->vsel_mask) - 1;
370 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
371 rdev->desc->vsel_mask, sel);
373 return ret;
376 static int da9063_suspend_enable(struct regulator_dev *rdev)
378 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
380 return regmap_field_write(regl->suspend, 1);
383 static int da9063_suspend_disable(struct regulator_dev *rdev)
385 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
387 return regmap_field_write(regl->suspend, 0);
390 static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
392 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
393 int val;
395 switch (mode) {
396 case REGULATOR_MODE_FAST:
397 val = BUCK_MODE_SYNC;
398 break;
399 case REGULATOR_MODE_NORMAL:
400 val = BUCK_MODE_AUTO;
401 break;
402 case REGULATOR_MODE_STANDBY:
403 val = BUCK_MODE_SLEEP;
404 break;
405 default:
406 return -EINVAL;
409 return regmap_field_write(regl->mode, val);
412 static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
414 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
415 unsigned val;
417 switch (mode) {
418 case REGULATOR_MODE_NORMAL:
419 val = 0;
420 break;
421 case REGULATOR_MODE_STANDBY:
422 val = 1;
423 break;
424 default:
425 return -EINVAL;
428 return regmap_field_write(regl->suspend_sleep, val);
431 static struct regulator_ops da9063_buck_ops = {
432 .enable = regulator_enable_regmap,
433 .disable = regulator_disable_regmap,
434 .is_enabled = regulator_is_enabled_regmap,
435 .get_voltage_sel = regulator_get_voltage_sel_regmap,
436 .set_voltage_sel = regulator_set_voltage_sel_regmap,
437 .list_voltage = regulator_list_voltage_linear,
438 .set_current_limit = da9063_set_current_limit,
439 .get_current_limit = da9063_get_current_limit,
440 .set_mode = da9063_buck_set_mode,
441 .get_mode = da9063_buck_get_mode,
442 .get_status = da9063_buck_get_status,
443 .set_suspend_voltage = da9063_set_suspend_voltage,
444 .set_suspend_enable = da9063_suspend_enable,
445 .set_suspend_disable = da9063_suspend_disable,
446 .set_suspend_mode = da9063_buck_set_suspend_mode,
449 static struct regulator_ops da9063_ldo_ops = {
450 .enable = regulator_enable_regmap,
451 .disable = regulator_disable_regmap,
452 .is_enabled = regulator_is_enabled_regmap,
453 .get_voltage_sel = regulator_get_voltage_sel_regmap,
454 .set_voltage_sel = regulator_set_voltage_sel_regmap,
455 .list_voltage = regulator_list_voltage_linear,
456 .set_mode = da9063_ldo_set_mode,
457 .get_mode = da9063_ldo_get_mode,
458 .get_status = da9063_ldo_get_status,
459 .set_suspend_voltage = da9063_set_suspend_voltage,
460 .set_suspend_enable = da9063_suspend_enable,
461 .set_suspend_disable = da9063_suspend_disable,
462 .set_suspend_mode = da9063_ldo_set_suspend_mode,
465 /* Info of regulators for DA9063 */
466 static const struct da9063_regulator_info da9063_regulator_info[] = {
468 DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
469 da9063_buck_a_limits),
470 DA9063_BUCK_COMMON_FIELDS(BCORE1),
471 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
472 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
473 DA9063_BCORE1_ILIM_MASK),
476 DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
477 da9063_buck_a_limits),
478 DA9063_BUCK_COMMON_FIELDS(BCORE2),
479 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE2_SEL),
480 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
481 DA9063_BCORE2_ILIM_MASK),
484 DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
485 da9063_buck_a_limits),
486 DA9063_BUCK_COMMON_FIELDS(BPRO),
487 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPRO_SEL),
488 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B,
489 DA9063_BPRO_ILIM_MASK),
492 DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
493 da9063_buck_b_limits),
494 DA9063_BUCK_COMMON_FIELDS(BMEM),
495 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
496 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
497 DA9063_BMEM_ILIM_MASK),
500 DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
501 da9063_buck_b_limits),
502 DA9063_BUCK_COMMON_FIELDS(BIO),
503 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VBIO_SEL),
504 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
505 DA9063_BIO_ILIM_MASK),
508 DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
509 da9063_buck_b_limits),
510 DA9063_BUCK_COMMON_FIELDS(BPERI),
511 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPERI_SEL),
512 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B,
513 DA9063_BPERI_ILIM_MASK),
516 DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
517 da9063_bcores_merged_limits),
518 /* BCORES_MERGED uses the same register fields as BCORE1 */
519 DA9063_BUCK_COMMON_FIELDS(BCORE1),
520 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
521 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
522 DA9063_BCORE1_ILIM_MASK),
525 DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
526 da9063_bmem_bio_merged_limits),
527 /* BMEM_BIO_MERGED uses the same register fields as BMEM */
528 DA9063_BUCK_COMMON_FIELDS(BMEM),
529 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
530 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
531 DA9063_BMEM_ILIM_MASK),
534 DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
535 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO1_SEL),
538 DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
539 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO2_SEL),
542 DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
543 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO3_SEL),
544 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
547 DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
548 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VLDO4_SEL),
549 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
552 DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
553 .suspend = BFIELD(DA9063_REG_LDO5_CONT, DA9063_VLDO5_SEL),
556 DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
557 .suspend = BFIELD(DA9063_REG_LDO6_CONT, DA9063_VLDO6_SEL),
560 DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
561 .suspend = BFIELD(DA9063_REG_LDO7_CONT, DA9063_VLDO7_SEL),
562 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
565 DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
566 .suspend = BFIELD(DA9063_REG_LDO8_CONT, DA9063_VLDO8_SEL),
567 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
570 DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
571 .suspend = BFIELD(DA9063_REG_LDO9_CONT, DA9063_VLDO9_SEL),
574 DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
575 .suspend = BFIELD(DA9063_REG_LDO10_CONT, DA9063_VLDO10_SEL),
578 DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
579 .suspend = BFIELD(DA9063_REG_LDO11_CONT, DA9063_VLDO11_SEL),
580 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
584 /* Link chip model with regulators info table */
585 static struct da9063_dev_model regulators_models[] = {
587 .regulator_info = da9063_regulator_info,
588 .n_regulators = ARRAY_SIZE(da9063_regulator_info),
589 .dev_model = PMIC_DA9063,
594 /* Regulator interrupt handlers */
595 static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
597 struct da9063_regulators *regulators = data;
598 struct da9063 *hw = regulators->regulator[0].hw;
599 struct da9063_regulator *regl;
600 int bits, i , ret;
602 ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
603 if (ret < 0)
604 return IRQ_NONE;
606 for (i = regulators->n_regulators - 1; i >= 0; i--) {
607 regl = &regulators->regulator[i];
608 if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
609 continue;
611 if (BIT(regl->info->oc_event.lsb) & bits)
612 regulator_notifier_call_chain(regl->rdev,
613 REGULATOR_EVENT_OVER_CURRENT, NULL);
616 return IRQ_HANDLED;
620 * Probing and Initialisation functions
622 static const struct regulator_init_data *da9063_get_regulator_initdata(
623 const struct da9063_regulators_pdata *regl_pdata, int id)
625 int i;
627 for (i = 0; i < regl_pdata->n_regulators; i++) {
628 if (id == regl_pdata->regulator_data[i].id)
629 return regl_pdata->regulator_data[i].initdata;
632 return NULL;
635 #ifdef CONFIG_OF
636 static struct of_regulator_match da9063_matches[] = {
637 [DA9063_ID_BCORE1] = { .name = "bcore1" },
638 [DA9063_ID_BCORE2] = { .name = "bcore2" },
639 [DA9063_ID_BPRO] = { .name = "bpro", },
640 [DA9063_ID_BMEM] = { .name = "bmem", },
641 [DA9063_ID_BIO] = { .name = "bio", },
642 [DA9063_ID_BPERI] = { .name = "bperi", },
643 [DA9063_ID_BCORES_MERGED] = { .name = "bcores-merged" },
644 [DA9063_ID_BMEM_BIO_MERGED] = { .name = "bmem-bio-merged", },
645 [DA9063_ID_LDO1] = { .name = "ldo1", },
646 [DA9063_ID_LDO2] = { .name = "ldo2", },
647 [DA9063_ID_LDO3] = { .name = "ldo3", },
648 [DA9063_ID_LDO4] = { .name = "ldo4", },
649 [DA9063_ID_LDO5] = { .name = "ldo5", },
650 [DA9063_ID_LDO6] = { .name = "ldo6", },
651 [DA9063_ID_LDO7] = { .name = "ldo7", },
652 [DA9063_ID_LDO8] = { .name = "ldo8", },
653 [DA9063_ID_LDO9] = { .name = "ldo9", },
654 [DA9063_ID_LDO10] = { .name = "ldo10", },
655 [DA9063_ID_LDO11] = { .name = "ldo11", },
658 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
659 struct platform_device *pdev,
660 struct of_regulator_match **da9063_reg_matches)
662 struct da9063_regulators_pdata *pdata;
663 struct da9063_regulator_data *rdata;
664 struct device_node *node;
665 int i, n, num;
667 node = of_find_node_by_name(pdev->dev.parent->of_node, "regulators");
668 if (!node) {
669 dev_err(&pdev->dev, "Regulators device node not found\n");
670 return ERR_PTR(-ENODEV);
673 num = of_regulator_match(&pdev->dev, node, da9063_matches,
674 ARRAY_SIZE(da9063_matches));
675 if (num < 0) {
676 dev_err(&pdev->dev, "Failed to match regulators\n");
677 return ERR_PTR(-EINVAL);
680 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
681 if (!pdata)
682 return ERR_PTR(-ENOMEM);
684 pdata->regulator_data = devm_kzalloc(&pdev->dev,
685 num * sizeof(*pdata->regulator_data),
686 GFP_KERNEL);
687 if (!pdata->regulator_data)
688 return ERR_PTR(-ENOMEM);
689 pdata->n_regulators = num;
691 n = 0;
692 for (i = 0; i < ARRAY_SIZE(da9063_matches); i++) {
693 if (!da9063_matches[i].init_data)
694 continue;
696 rdata = &pdata->regulator_data[n];
697 rdata->id = i;
698 rdata->initdata = da9063_matches[i].init_data;
700 n++;
703 *da9063_reg_matches = da9063_matches;
704 return pdata;
706 #else
707 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
708 struct platform_device *pdev,
709 struct of_regulator_match **da9063_reg_matches)
711 da9063_reg_matches = NULL;
712 return ERR_PTR(-ENODEV);
714 #endif
716 static int da9063_regulator_probe(struct platform_device *pdev)
718 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
719 struct da9063_pdata *da9063_pdata = dev_get_platdata(da9063->dev);
720 struct of_regulator_match *da9063_reg_matches = NULL;
721 struct da9063_regulators_pdata *regl_pdata;
722 const struct da9063_dev_model *model;
723 struct da9063_regulators *regulators;
724 struct da9063_regulator *regl;
725 struct regulator_config config;
726 bool bcores_merged, bmem_bio_merged;
727 int id, irq, n, n_regulators, ret, val;
728 size_t size;
730 regl_pdata = da9063_pdata ? da9063_pdata->regulators_pdata : NULL;
732 if (!regl_pdata)
733 regl_pdata = da9063_parse_regulators_dt(pdev,
734 &da9063_reg_matches);
736 if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
737 dev_err(&pdev->dev,
738 "No regulators defined for the platform\n");
739 return PTR_ERR(regl_pdata);
742 /* Find regulators set for particular device model */
743 for (model = regulators_models; model->regulator_info; model++) {
744 if (model->dev_model == da9063->model)
745 break;
747 if (!model->regulator_info) {
748 dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
749 da9063->model);
750 return -ENODEV;
753 ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
754 if (ret < 0) {
755 dev_err(&pdev->dev,
756 "Error while reading BUCKs configuration\n");
757 return -EIO;
759 bcores_merged = val & DA9063_BCORE_MERGE;
760 bmem_bio_merged = val & DA9063_BUCK_MERGE;
762 n_regulators = model->n_regulators;
763 if (bcores_merged)
764 n_regulators -= 2; /* remove BCORE1, BCORE2 */
765 else
766 n_regulators--; /* remove BCORES_MERGED */
767 if (bmem_bio_merged)
768 n_regulators -= 2; /* remove BMEM, BIO */
769 else
770 n_regulators--; /* remove BMEM_BIO_MERGED */
772 /* Allocate memory required by usable regulators */
773 size = sizeof(struct da9063_regulators) +
774 n_regulators * sizeof(struct da9063_regulator);
775 regulators = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
776 if (!regulators) {
777 dev_err(&pdev->dev, "No memory for regulators\n");
778 return -ENOMEM;
781 regulators->n_regulators = n_regulators;
782 platform_set_drvdata(pdev, regulators);
784 /* Register all regulators declared in platform information */
785 n = 0;
786 id = 0;
787 while (n < regulators->n_regulators) {
788 /* Skip regulator IDs depending on merge mode configuration */
789 switch (id) {
790 case DA9063_ID_BCORE1:
791 case DA9063_ID_BCORE2:
792 if (bcores_merged) {
793 id++;
794 continue;
796 break;
797 case DA9063_ID_BMEM:
798 case DA9063_ID_BIO:
799 if (bmem_bio_merged) {
800 id++;
801 continue;
803 break;
804 case DA9063_ID_BCORES_MERGED:
805 if (!bcores_merged) {
806 id++;
807 continue;
809 break;
810 case DA9063_ID_BMEM_BIO_MERGED:
811 if (!bmem_bio_merged) {
812 id++;
813 continue;
815 break;
818 /* Initialise regulator structure */
819 regl = &regulators->regulator[n];
820 regl->hw = da9063;
821 regl->info = &model->regulator_info[id];
822 regl->desc = regl->info->desc;
823 regl->desc.type = REGULATOR_VOLTAGE;
824 regl->desc.owner = THIS_MODULE;
826 if (regl->info->mode.reg)
827 regl->mode = devm_regmap_field_alloc(&pdev->dev,
828 da9063->regmap, regl->info->mode);
829 if (regl->info->suspend.reg)
830 regl->suspend = devm_regmap_field_alloc(&pdev->dev,
831 da9063->regmap, regl->info->suspend);
832 if (regl->info->sleep.reg)
833 regl->sleep = devm_regmap_field_alloc(&pdev->dev,
834 da9063->regmap, regl->info->sleep);
835 if (regl->info->suspend_sleep.reg)
836 regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
837 da9063->regmap, regl->info->suspend_sleep);
838 if (regl->info->ilimit.reg)
839 regl->ilimit = devm_regmap_field_alloc(&pdev->dev,
840 da9063->regmap, regl->info->ilimit);
842 /* Register regulator */
843 memset(&config, 0, sizeof(config));
844 config.dev = &pdev->dev;
845 config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
846 config.driver_data = regl;
847 if (da9063_reg_matches)
848 config.of_node = da9063_reg_matches[id].of_node;
849 config.regmap = da9063->regmap;
850 regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
851 &config);
852 if (IS_ERR(regl->rdev)) {
853 dev_err(&pdev->dev,
854 "Failed to register %s regulator\n",
855 regl->desc.name);
856 return PTR_ERR(regl->rdev);
858 id++;
859 n++;
862 /* LDOs overcurrent event support */
863 irq = platform_get_irq_byname(pdev, "LDO_LIM");
864 if (irq < 0) {
865 dev_err(&pdev->dev, "Failed to get IRQ.\n");
866 return irq;
869 regulators->irq_ldo_lim = regmap_irq_get_virq(da9063->regmap_irq, irq);
870 if (regulators->irq_ldo_lim >= 0) {
871 ret = request_threaded_irq(regulators->irq_ldo_lim,
872 NULL, da9063_ldo_lim_event,
873 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
874 "LDO_LIM", regulators);
875 if (ret) {
876 dev_err(&pdev->dev,
877 "Failed to request LDO_LIM IRQ.\n");
878 regulators->irq_ldo_lim = -ENXIO;
882 return 0;
885 static int da9063_regulator_remove(struct platform_device *pdev)
887 struct da9063_regulators *regulators = platform_get_drvdata(pdev);
889 free_irq(regulators->irq_ldo_lim, regulators);
890 free_irq(regulators->irq_uvov, regulators);
892 return 0;
895 static struct platform_driver da9063_regulator_driver = {
896 .driver = {
897 .name = DA9063_DRVNAME_REGULATORS,
898 .owner = THIS_MODULE,
900 .probe = da9063_regulator_probe,
901 .remove = da9063_regulator_remove,
904 static int __init da9063_regulator_init(void)
906 return platform_driver_register(&da9063_regulator_driver);
908 subsys_initcall(da9063_regulator_init);
910 static void __exit da9063_regulator_cleanup(void)
912 platform_driver_unregister(&da9063_regulator_driver);
914 module_exit(da9063_regulator_cleanup);
917 /* Module information */
918 MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
919 MODULE_DESCRIPTION("DA9063 regulators driver");
920 MODULE_LICENSE("GPL");
921 MODULE_ALIAS("paltform:" DA9063_DRVNAME_REGULATORS);