1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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>
30
31
32
33#define BFIELD(_reg, _mask) \
34 REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \
35 sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1)
36
37
38struct da9063_regulator_info {
39 struct regulator_desc desc;
40
41
42 unsigned n_current_limits;
43 const int *current_limits;
44
45
46 struct reg_field mode;
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;
52
53
54 struct reg_field oc_event;
55};
56
57
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
74
75
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)
85
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)
96
97
98struct da9063_dev_model {
99 const struct da9063_regulator_info *regulator_info;
100 unsigned n_regulators;
101 enum da9063_type type;
102};
103
104
105struct da9063_regulator {
106 struct regulator_desc desc;
107 struct regulator_dev *rdev;
108 struct da9063 *hw;
109 const struct da9063_regulator_info *info;
110
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;
116};
117
118
119struct da9063_regulators {
120 unsigned n_regulators;
121
122 struct da9063_regulator regulator[0];
123};
124
125
126enum {
127 BUCK_MODE_MANUAL,
128 BUCK_MODE_SLEEP,
129 BUCK_MODE_SYNC,
130 BUCK_MODE_AUTO
131};
132
133
134
135
136
137static const int da9063_buck_a_limits[] = {
138 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
139 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
140};
141
142
143
144static const int da9063_buck_b_limits[] = {
145 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
146 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
147};
148
149
150
151static const int da9063_bcores_merged_limits[] = {
152 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
153 2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
154};
155
156
157
158static const int da9063_bmem_bio_merged_limits[] = {
159 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
160 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
161};
162
163static int da9063_set_current_limit(struct regulator_dev *rdev,
164 int min_uA, int max_uA)
165{
166 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
167 const struct da9063_regulator_info *rinfo = regl->info;
168 int n, tval;
169
170 for (n = 0; n < rinfo->n_current_limits; n++) {
171 tval = rinfo->current_limits[n];
172 if (tval >= min_uA && tval <= max_uA)
173 return regmap_field_write(regl->ilimit, n);
174 }
175
176 return -EINVAL;
177}
178
179static int da9063_get_current_limit(struct regulator_dev *rdev)
180{
181 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
182 const struct da9063_regulator_info *rinfo = regl->info;
183 unsigned int sel;
184 int ret;
185
186 ret = regmap_field_read(regl->ilimit, &sel);
187 if (ret < 0)
188 return ret;
189
190 if (sel >= rinfo->n_current_limits)
191 sel = rinfo->n_current_limits - 1;
192
193 return rinfo->current_limits[sel];
194}
195
196static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
197{
198 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
199 unsigned val;
200
201 switch (mode) {
202 case REGULATOR_MODE_FAST:
203 val = BUCK_MODE_SYNC;
204 break;
205 case REGULATOR_MODE_NORMAL:
206 val = BUCK_MODE_AUTO;
207 break;
208 case REGULATOR_MODE_STANDBY:
209 val = BUCK_MODE_SLEEP;
210 break;
211 default:
212 return -EINVAL;
213 }
214
215 return regmap_field_write(regl->mode, val);
216}
217
218
219
220
221
222
223
224static unsigned da9063_buck_get_mode(struct regulator_dev *rdev)
225{
226 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
227 struct regmap_field *field;
228 unsigned int val, mode = 0;
229 int ret;
230
231 ret = regmap_field_read(regl->mode, &val);
232 if (ret < 0)
233 return ret;
234
235 switch (val) {
236 default:
237 case BUCK_MODE_MANUAL:
238 mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
239
240 break;
241 case BUCK_MODE_SLEEP:
242 return REGULATOR_MODE_STANDBY;
243 case BUCK_MODE_SYNC:
244 return REGULATOR_MODE_FAST;
245 case BUCK_MODE_AUTO:
246 return REGULATOR_MODE_NORMAL;
247 }
248
249
250 ret = regmap_field_read(regl->suspend, &val);
251 if (ret < 0)
252 return 0;
253
254
255 if (val)
256 field = regl->suspend_sleep;
257 else
258 field = regl->sleep;
259
260 ret = regmap_field_read(field, &val);
261 if (ret < 0)
262 return 0;
263
264 if (val)
265 mode &= REGULATOR_MODE_STANDBY;
266 else
267 mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
268
269 return mode;
270}
271
272
273
274
275
276
277static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
278{
279 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
280 unsigned val;
281
282 switch (mode) {
283 case REGULATOR_MODE_NORMAL:
284 val = 0;
285 break;
286 case REGULATOR_MODE_STANDBY:
287 val = 1;
288 break;
289 default:
290 return -EINVAL;
291 }
292
293 return regmap_field_write(regl->sleep, val);
294}
295
296static unsigned da9063_ldo_get_mode(struct regulator_dev *rdev)
297{
298 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
299 struct regmap_field *field;
300 int ret, val;
301
302
303 ret = regmap_field_read(regl->suspend, &val);
304 if (ret < 0)
305 return 0;
306
307
308 if (val)
309 field = regl->suspend_sleep;
310 else
311 field = regl->sleep;
312
313 ret = regmap_field_read(field, &val);
314 if (ret < 0)
315 return 0;
316
317 if (val)
318 return REGULATOR_MODE_STANDBY;
319 else
320 return REGULATOR_MODE_NORMAL;
321}
322
323static int da9063_buck_get_status(struct regulator_dev *rdev)
324{
325 int ret = regulator_is_enabled_regmap(rdev);
326
327 if (ret == 0) {
328 ret = REGULATOR_STATUS_OFF;
329 } else if (ret > 0) {
330 ret = da9063_buck_get_mode(rdev);
331 if (ret > 0)
332 ret = regulator_mode_to_status(ret);
333 else if (ret == 0)
334 ret = -EIO;
335 }
336
337 return ret;
338}
339
340static int da9063_ldo_get_status(struct regulator_dev *rdev)
341{
342 int ret = regulator_is_enabled_regmap(rdev);
343
344 if (ret == 0) {
345 ret = REGULATOR_STATUS_OFF;
346 } else if (ret > 0) {
347 ret = da9063_ldo_get_mode(rdev);
348 if (ret > 0)
349 ret = regulator_mode_to_status(ret);
350 else if (ret == 0)
351 ret = -EIO;
352 }
353
354 return ret;
355}
356
357static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
358{
359 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
360 const struct da9063_regulator_info *rinfo = regl->info;
361 int ret, sel;
362
363 sel = regulator_map_voltage_linear(rdev, uV, uV);
364 if (sel < 0)
365 return sel;
366
367 sel <<= ffs(rdev->desc->vsel_mask) - 1;
368
369 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
370 rdev->desc->vsel_mask, sel);
371
372 return ret;
373}
374
375static int da9063_suspend_enable(struct regulator_dev *rdev)
376{
377 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
378
379 return regmap_field_write(regl->suspend, 1);
380}
381
382static int da9063_suspend_disable(struct regulator_dev *rdev)
383{
384 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
385
386 return regmap_field_write(regl->suspend, 0);
387}
388
389static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
390{
391 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
392 int val;
393
394 switch (mode) {
395 case REGULATOR_MODE_FAST:
396 val = BUCK_MODE_SYNC;
397 break;
398 case REGULATOR_MODE_NORMAL:
399 val = BUCK_MODE_AUTO;
400 break;
401 case REGULATOR_MODE_STANDBY:
402 val = BUCK_MODE_SLEEP;
403 break;
404 default:
405 return -EINVAL;
406 }
407
408 return regmap_field_write(regl->mode, val);
409}
410
411static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
412{
413 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
414 unsigned val;
415
416 switch (mode) {
417 case REGULATOR_MODE_NORMAL:
418 val = 0;
419 break;
420 case REGULATOR_MODE_STANDBY:
421 val = 1;
422 break;
423 default:
424 return -EINVAL;
425 }
426
427 return regmap_field_write(regl->suspend_sleep, val);
428}
429
430static const struct regulator_ops da9063_buck_ops = {
431 .enable = regulator_enable_regmap,
432 .disable = regulator_disable_regmap,
433 .is_enabled = regulator_is_enabled_regmap,
434 .get_voltage_sel = regulator_get_voltage_sel_regmap,
435 .set_voltage_sel = regulator_set_voltage_sel_regmap,
436 .list_voltage = regulator_list_voltage_linear,
437 .set_current_limit = da9063_set_current_limit,
438 .get_current_limit = da9063_get_current_limit,
439 .set_mode = da9063_buck_set_mode,
440 .get_mode = da9063_buck_get_mode,
441 .get_status = da9063_buck_get_status,
442 .set_suspend_voltage = da9063_set_suspend_voltage,
443 .set_suspend_enable = da9063_suspend_enable,
444 .set_suspend_disable = da9063_suspend_disable,
445 .set_suspend_mode = da9063_buck_set_suspend_mode,
446};
447
448static const struct regulator_ops da9063_ldo_ops = {
449 .enable = regulator_enable_regmap,
450 .disable = regulator_disable_regmap,
451 .is_enabled = regulator_is_enabled_regmap,
452 .get_voltage_sel = regulator_get_voltage_sel_regmap,
453 .set_voltage_sel = regulator_set_voltage_sel_regmap,
454 .list_voltage = regulator_list_voltage_linear,
455 .set_mode = da9063_ldo_set_mode,
456 .get_mode = da9063_ldo_get_mode,
457 .get_status = da9063_ldo_get_status,
458 .set_suspend_voltage = da9063_set_suspend_voltage,
459 .set_suspend_enable = da9063_suspend_enable,
460 .set_suspend_disable = da9063_suspend_disable,
461 .set_suspend_mode = da9063_ldo_set_suspend_mode,
462};
463
464
465static const struct da9063_regulator_info da9063_regulator_info[] = {
466 {
467 DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
468 da9063_buck_a_limits),
469 DA9063_BUCK_COMMON_FIELDS(BCORE1),
470 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
471 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
472 DA9063_BCORE1_ILIM_MASK),
473 },
474 {
475 DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
476 da9063_buck_a_limits),
477 DA9063_BUCK_COMMON_FIELDS(BCORE2),
478 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE2_SEL),
479 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
480 DA9063_BCORE2_ILIM_MASK),
481 },
482 {
483 DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
484 da9063_buck_a_limits),
485 DA9063_BUCK_COMMON_FIELDS(BPRO),
486 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPRO_SEL),
487 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B,
488 DA9063_BPRO_ILIM_MASK),
489 },
490 {
491 DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
492 da9063_buck_b_limits),
493 DA9063_BUCK_COMMON_FIELDS(BMEM),
494 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
495 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
496 DA9063_BMEM_ILIM_MASK),
497 },
498 {
499 DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
500 da9063_buck_b_limits),
501 DA9063_BUCK_COMMON_FIELDS(BIO),
502 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VBIO_SEL),
503 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
504 DA9063_BIO_ILIM_MASK),
505 },
506 {
507 DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
508 da9063_buck_b_limits),
509 DA9063_BUCK_COMMON_FIELDS(BPERI),
510 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPERI_SEL),
511 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B,
512 DA9063_BPERI_ILIM_MASK),
513 },
514 {
515 DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
516 da9063_bcores_merged_limits),
517
518 DA9063_BUCK_COMMON_FIELDS(BCORE1),
519 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
520 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
521 DA9063_BCORE1_ILIM_MASK),
522 },
523 {
524 DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
525 da9063_bmem_bio_merged_limits),
526
527 DA9063_BUCK_COMMON_FIELDS(BMEM),
528 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
529 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
530 DA9063_BMEM_ILIM_MASK),
531 },
532 {
533 DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
534 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO3_SEL),
535 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
536 },
537 {
538 DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
539 .suspend = BFIELD(DA9063_REG_LDO7_CONT, DA9063_VLDO7_SEL),
540 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
541 },
542 {
543 DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
544 .suspend = BFIELD(DA9063_REG_LDO8_CONT, DA9063_VLDO8_SEL),
545 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
546 },
547 {
548 DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
549 .suspend = BFIELD(DA9063_REG_LDO9_CONT, DA9063_VLDO9_SEL),
550 },
551 {
552 DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
553 .suspend = BFIELD(DA9063_REG_LDO11_CONT, DA9063_VLDO11_SEL),
554 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
555 },
556
557
558 {
559 DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
560 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO1_SEL),
561 },
562 {
563 DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
564 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO2_SEL),
565 },
566 {
567 DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
568 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VLDO4_SEL),
569 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
570 },
571 {
572 DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
573 .suspend = BFIELD(DA9063_REG_LDO5_CONT, DA9063_VLDO5_SEL),
574 },
575 {
576 DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
577 .suspend = BFIELD(DA9063_REG_LDO6_CONT, DA9063_VLDO6_SEL),
578 },
579
580 {
581 DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
582 .suspend = BFIELD(DA9063_REG_LDO10_CONT, DA9063_VLDO10_SEL),
583 },
584};
585
586
587static struct da9063_dev_model regulators_models[] = {
588 {
589 .regulator_info = da9063_regulator_info,
590 .n_regulators = ARRAY_SIZE(da9063_regulator_info),
591 .type = PMIC_TYPE_DA9063,
592 },
593 {
594 .regulator_info = da9063_regulator_info,
595 .n_regulators = ARRAY_SIZE(da9063_regulator_info) - 6,
596 .type = PMIC_TYPE_DA9063L,
597 },
598 { }
599};
600
601
602static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
603{
604 struct da9063_regulators *regulators = data;
605 struct da9063 *hw = regulators->regulator[0].hw;
606 struct da9063_regulator *regl;
607 int bits, i , ret;
608
609 ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
610 if (ret < 0)
611 return IRQ_NONE;
612
613 for (i = regulators->n_regulators - 1; i >= 0; i--) {
614 regl = ®ulators->regulator[i];
615 if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
616 continue;
617
618 if (BIT(regl->info->oc_event.lsb) & bits)
619 regulator_notifier_call_chain(regl->rdev,
620 REGULATOR_EVENT_OVER_CURRENT, NULL);
621 }
622
623 return IRQ_HANDLED;
624}
625
626
627
628
629static const struct regulator_init_data *da9063_get_regulator_initdata(
630 const struct da9063_regulators_pdata *regl_pdata, int id)
631{
632 int i;
633
634 for (i = 0; i < regl_pdata->n_regulators; i++) {
635 if (id == regl_pdata->regulator_data[i].id)
636 return regl_pdata->regulator_data[i].initdata;
637 }
638
639 return NULL;
640}
641
642#ifdef CONFIG_OF
643static struct of_regulator_match da9063_matches[] = {
644 [DA9063_ID_BCORE1] = { .name = "bcore1" },
645 [DA9063_ID_BCORE2] = { .name = "bcore2" },
646 [DA9063_ID_BPRO] = { .name = "bpro", },
647 [DA9063_ID_BMEM] = { .name = "bmem", },
648 [DA9063_ID_BIO] = { .name = "bio", },
649 [DA9063_ID_BPERI] = { .name = "bperi", },
650 [DA9063_ID_BCORES_MERGED] = { .name = "bcores-merged" },
651 [DA9063_ID_BMEM_BIO_MERGED] = { .name = "bmem-bio-merged", },
652 [DA9063_ID_LDO3] = { .name = "ldo3", },
653 [DA9063_ID_LDO7] = { .name = "ldo7", },
654 [DA9063_ID_LDO8] = { .name = "ldo8", },
655 [DA9063_ID_LDO9] = { .name = "ldo9", },
656 [DA9063_ID_LDO11] = { .name = "ldo11", },
657
658 [DA9063_ID_LDO1] = { .name = "ldo1", },
659 [DA9063_ID_LDO2] = { .name = "ldo2", },
660 [DA9063_ID_LDO4] = { .name = "ldo4", },
661 [DA9063_ID_LDO5] = { .name = "ldo5", },
662 [DA9063_ID_LDO6] = { .name = "ldo6", },
663 [DA9063_ID_LDO10] = { .name = "ldo10", },
664};
665
666static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
667 struct platform_device *pdev,
668 struct of_regulator_match **da9063_reg_matches)
669{
670 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
671 struct da9063_regulators_pdata *pdata;
672 struct da9063_regulator_data *rdata;
673 struct device_node *node;
674 int da9063_matches_len = ARRAY_SIZE(da9063_matches);
675 int i, n, num;
676
677 if (da9063->type == PMIC_TYPE_DA9063L)
678 da9063_matches_len -= 6;
679
680 node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
681 if (!node) {
682 dev_err(&pdev->dev, "Regulators device node not found\n");
683 return ERR_PTR(-ENODEV);
684 }
685
686 num = of_regulator_match(&pdev->dev, node, da9063_matches,
687 da9063_matches_len);
688 of_node_put(node);
689 if (num < 0) {
690 dev_err(&pdev->dev, "Failed to match regulators\n");
691 return ERR_PTR(-EINVAL);
692 }
693
694 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
695 if (!pdata)
696 return ERR_PTR(-ENOMEM);
697
698 pdata->regulator_data = devm_kcalloc(&pdev->dev,
699 num, sizeof(*pdata->regulator_data),
700 GFP_KERNEL);
701 if (!pdata->regulator_data)
702 return ERR_PTR(-ENOMEM);
703 pdata->n_regulators = num;
704
705 n = 0;
706 for (i = 0; i < da9063_matches_len; i++) {
707 if (!da9063_matches[i].init_data)
708 continue;
709
710 rdata = &pdata->regulator_data[n];
711 rdata->id = i;
712 rdata->initdata = da9063_matches[i].init_data;
713
714 n++;
715 }
716
717 *da9063_reg_matches = da9063_matches;
718 return pdata;
719}
720#else
721static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
722 struct platform_device *pdev,
723 struct of_regulator_match **da9063_reg_matches)
724{
725 *da9063_reg_matches = NULL;
726 return ERR_PTR(-ENODEV);
727}
728#endif
729
730static int da9063_regulator_probe(struct platform_device *pdev)
731{
732 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
733 struct da9063_pdata *da9063_pdata = dev_get_platdata(da9063->dev);
734 struct of_regulator_match *da9063_reg_matches = NULL;
735 struct da9063_regulators_pdata *regl_pdata;
736 const struct da9063_dev_model *model;
737 struct da9063_regulators *regulators;
738 struct da9063_regulator *regl;
739 struct regulator_config config;
740 bool bcores_merged, bmem_bio_merged;
741 int id, irq, n, n_regulators, ret, val;
742 size_t size;
743
744 regl_pdata = da9063_pdata ? da9063_pdata->regulators_pdata : NULL;
745
746 if (!regl_pdata)
747 regl_pdata = da9063_parse_regulators_dt(pdev,
748 &da9063_reg_matches);
749
750 if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
751 dev_err(&pdev->dev,
752 "No regulators defined for the platform\n");
753 return -ENODEV;
754 }
755
756
757 for (model = regulators_models; model->regulator_info; model++) {
758 if (model->type == da9063->type)
759 break;
760 }
761 if (!model->regulator_info) {
762 dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
763 da9063->type);
764 return -ENODEV;
765 }
766
767 ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
768 if (ret < 0) {
769 dev_err(&pdev->dev,
770 "Error while reading BUCKs configuration\n");
771 return ret;
772 }
773 bcores_merged = val & DA9063_BCORE_MERGE;
774 bmem_bio_merged = val & DA9063_BUCK_MERGE;
775
776 n_regulators = model->n_regulators;
777 if (bcores_merged)
778 n_regulators -= 2;
779 else
780 n_regulators--;
781 if (bmem_bio_merged)
782 n_regulators -= 2;
783 else
784 n_regulators--;
785
786
787 size = sizeof(struct da9063_regulators) +
788 n_regulators * sizeof(struct da9063_regulator);
789 regulators = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
790 if (!regulators)
791 return -ENOMEM;
792
793 regulators->n_regulators = n_regulators;
794 platform_set_drvdata(pdev, regulators);
795
796
797 n = 0;
798 id = 0;
799 while (n < regulators->n_regulators) {
800
801 switch (id) {
802 case DA9063_ID_BCORE1:
803 case DA9063_ID_BCORE2:
804 if (bcores_merged) {
805 id++;
806 continue;
807 }
808 break;
809 case DA9063_ID_BMEM:
810 case DA9063_ID_BIO:
811 if (bmem_bio_merged) {
812 id++;
813 continue;
814 }
815 break;
816 case DA9063_ID_BCORES_MERGED:
817 if (!bcores_merged) {
818 id++;
819 continue;
820 }
821 break;
822 case DA9063_ID_BMEM_BIO_MERGED:
823 if (!bmem_bio_merged) {
824 id++;
825 continue;
826 }
827 break;
828 }
829
830
831 regl = ®ulators->regulator[n];
832 regl->hw = da9063;
833 regl->info = &model->regulator_info[id];
834 regl->desc = regl->info->desc;
835 regl->desc.type = REGULATOR_VOLTAGE;
836 regl->desc.owner = THIS_MODULE;
837
838 if (regl->info->mode.reg)
839 regl->mode = devm_regmap_field_alloc(&pdev->dev,
840 da9063->regmap, regl->info->mode);
841 if (regl->info->suspend.reg)
842 regl->suspend = devm_regmap_field_alloc(&pdev->dev,
843 da9063->regmap, regl->info->suspend);
844 if (regl->info->sleep.reg)
845 regl->sleep = devm_regmap_field_alloc(&pdev->dev,
846 da9063->regmap, regl->info->sleep);
847 if (regl->info->suspend_sleep.reg)
848 regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
849 da9063->regmap, regl->info->suspend_sleep);
850 if (regl->info->ilimit.reg)
851 regl->ilimit = devm_regmap_field_alloc(&pdev->dev,
852 da9063->regmap, regl->info->ilimit);
853
854
855 memset(&config, 0, sizeof(config));
856 config.dev = &pdev->dev;
857 config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
858 config.driver_data = regl;
859 if (da9063_reg_matches)
860 config.of_node = da9063_reg_matches[id].of_node;
861 config.regmap = da9063->regmap;
862 regl->rdev = devm_regulator_register(&pdev->dev, ®l->desc,
863 &config);
864 if (IS_ERR(regl->rdev)) {
865 dev_err(&pdev->dev,
866 "Failed to register %s regulator\n",
867 regl->desc.name);
868 return PTR_ERR(regl->rdev);
869 }
870 id++;
871 n++;
872 }
873
874
875 irq = platform_get_irq_byname(pdev, "LDO_LIM");
876 if (irq < 0) {
877 dev_err(&pdev->dev, "Failed to get IRQ.\n");
878 return irq;
879 }
880
881 ret = devm_request_threaded_irq(&pdev->dev, irq,
882 NULL, da9063_ldo_lim_event,
883 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
884 "LDO_LIM", regulators);
885 if (ret) {
886 dev_err(&pdev->dev, "Failed to request LDO_LIM IRQ.\n");
887 return ret;
888 }
889
890 return 0;
891}
892
893static struct platform_driver da9063_regulator_driver = {
894 .driver = {
895 .name = DA9063_DRVNAME_REGULATORS,
896 },
897 .probe = da9063_regulator_probe,
898};
899
900static int __init da9063_regulator_init(void)
901{
902 return platform_driver_register(&da9063_regulator_driver);
903}
904subsys_initcall(da9063_regulator_init);
905
906static void __exit da9063_regulator_cleanup(void)
907{
908 platform_driver_unregister(&da9063_regulator_driver);
909}
910module_exit(da9063_regulator_cleanup);
911
912
913
914MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
915MODULE_DESCRIPTION("DA9063 regulators driver");
916MODULE_LICENSE("GPL");
917MODULE_ALIAS("platform:" DA9063_DRVNAME_REGULATORS);
918