blob: 7c9461d13313ea1c4d1ddd86d3cf7e6ac783b7ee [file] [log] [blame]
Steve Twissebf6dad02014-02-12 09:57:52 +00001
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +02002/*
3 * Regulator driver for DA9063 PMIC series
4 *
5 * Copyright 2012 Dialog Semiconductors Ltd.
6 * Copyright 2013 Philipp Zabel, Pengutronix
7 *
8 * Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>
9 *
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.
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/* 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)
36
37/* Regulator capabilities and registers description */
38struct da9063_regulator_info {
39 struct regulator_desc desc;
40
41 /* Current limiting */
42 unsigned n_current_limits;
43 const int *current_limits;
44
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;
52
53 /* DA9063 event detection bit */
54 struct reg_field oc_event;
55};
56
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, \
Steve Twissebf6dad02014-02-12 09:57:52 +000064 .desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1 \
65 + (DA9063_V##regl_name##_BIAS)), \
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +020066 .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/* 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)
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/* Defines asignment of regulators info table to chip model */
98struct da9063_dev_model {
99 const struct da9063_regulator_info *regulator_info;
100 unsigned n_regulators;
101 unsigned dev_model;
102};
103
104/* Single regulator settings */
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/* Encapsulates all information for the regulators driver */
119struct da9063_regulators {
120 int irq_ldo_lim;
121 int irq_uvov;
122
123 unsigned n_regulators;
124 /* Array size to be defined during init. Keep at end. */
125 struct da9063_regulator regulator[0];
126};
127
128/* BUCK modes for DA9063 */
129enum {
130 BUCK_MODE_MANUAL, /* 0 */
131 BUCK_MODE_SLEEP, /* 1 */
132 BUCK_MODE_SYNC, /* 2 */
133 BUCK_MODE_AUTO /* 3 */
134};
135
136/* Regulator operations */
137
138/* Current limits array (in uA) for BCORE1, BCORE2, BPRO.
139 Entry indexes corresponds to register values. */
140static const int da9063_buck_a_limits[] = {
141 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
142 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
143};
144
145/* Current limits array (in uA) for BMEM, BIO, BPERI.
146 Entry indexes corresponds to register values. */
147static const int da9063_buck_b_limits[] = {
148 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
149 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
150};
151
152/* Current limits array (in uA) for merged BCORE1 and BCORE2.
153 Entry indexes corresponds to register values. */
154static const int da9063_bcores_merged_limits[] = {
155 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
156 2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
157};
158
159/* Current limits array (in uA) for merged BMEM and BIO.
160 Entry indexes corresponds to register values. */
161static 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
164};
165
166static int da9063_set_current_limit(struct regulator_dev *rdev,
167 int min_uA, int max_uA)
168{
169 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
170 const struct da9063_regulator_info *rinfo = regl->info;
Axel Lin556dcf92013-08-30 20:07:38 +0800171 int n, tval;
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200172
173 for (n = 0; n < rinfo->n_current_limits; n++) {
174 tval = rinfo->current_limits[n];
Axel Lin556dcf92013-08-30 20:07:38 +0800175 if (tval >= min_uA && tval <= max_uA)
176 return regmap_field_write(regl->ilimit, n);
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200177 }
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200178
Axel Lin556dcf92013-08-30 20:07:38 +0800179 return -EINVAL;
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200180}
181
182static int da9063_get_current_limit(struct regulator_dev *rdev)
183{
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;
188
189 ret = regmap_field_read(regl->ilimit, &sel);
190 if (ret < 0)
191 return ret;
192
193 if (sel >= rinfo->n_current_limits)
194 sel = rinfo->n_current_limits - 1;
195
196 return rinfo->current_limits[sel];
197}
198
199static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
200{
201 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
202 unsigned val;
203
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;
216 }
217
218 return regmap_field_write(regl->mode, val);
219}
220
221/*
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.
225 */
226
227static unsigned da9063_buck_get_mode(struct regulator_dev *rdev)
228{
229 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
230 struct regmap_field *field;
231 unsigned int val, mode = 0;
232 int ret;
233
234 ret = regmap_field_read(regl->mode, &val);
235 if (ret < 0)
236 return ret;
237
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;
250 }
251
252 /* Detect current regulator state */
253 ret = regmap_field_read(regl->suspend, &val);
254 if (ret < 0)
255 return 0;
256
257 /* Read regulator mode from proper register, depending on state */
258 if (val)
259 field = regl->suspend_sleep;
260 else
261 field = regl->sleep;
262
263 ret = regmap_field_read(field, &val);
264 if (ret < 0)
265 return 0;
266
267 if (val)
268 mode &= REGULATOR_MODE_STANDBY;
269 else
270 mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
271
272 return mode;
273}
274
275/*
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.
278 */
279
280static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
281{
282 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
283 unsigned val;
284
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;
294 }
295
296 return regmap_field_write(regl->sleep, val);
297}
298
299static unsigned da9063_ldo_get_mode(struct regulator_dev *rdev)
300{
301 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
302 struct regmap_field *field;
303 int ret, val;
304
305 /* Detect current regulator state */
306 ret = regmap_field_read(regl->suspend, &val);
307 if (ret < 0)
308 return 0;
309
310 /* Read regulator mode from proper register, depending on state */
311 if (val)
312 field = regl->suspend_sleep;
313 else
314 field = regl->sleep;
315
316 ret = regmap_field_read(field, &val);
317 if (ret < 0)
318 return 0;
319
320 if (val)
321 return REGULATOR_MODE_STANDBY;
322 else
323 return REGULATOR_MODE_NORMAL;
324}
325
326static int da9063_buck_get_status(struct regulator_dev *rdev)
327{
328 int ret = regulator_is_enabled_regmap(rdev);
329
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;
338 }
339
340 return ret;
341}
342
343static int da9063_ldo_get_status(struct regulator_dev *rdev)
344{
345 int ret = regulator_is_enabled_regmap(rdev);
346
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;
355 }
356
357 return ret;
358}
359
360static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
361{
362 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
363 const struct da9063_regulator_info *rinfo = regl->info;
364 int ret, sel;
365
366 sel = regulator_map_voltage_linear(rdev, uV, uV);
367 if (sel < 0)
Sachin Kamat8a7963d2014-02-18 16:11:05 +0530368 return sel;
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200369
370 sel <<= ffs(rdev->desc->vsel_mask) - 1;
371
372 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
373 rdev->desc->vsel_mask, sel);
374
375 return ret;
376}
377
378static int da9063_suspend_enable(struct regulator_dev *rdev)
379{
380 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
381
382 return regmap_field_write(regl->suspend, 1);
383}
384
385static int da9063_suspend_disable(struct regulator_dev *rdev)
386{
387 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
388
389 return regmap_field_write(regl->suspend, 0);
390}
391
392static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
393{
394 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
395 int val;
396
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;
409 }
410
411 return regmap_field_write(regl->mode, val);
412}
413
414static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
415{
416 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
417 unsigned val;
418
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;
428 }
429
430 return regmap_field_write(regl->suspend_sleep, val);
431}
432
433static 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,
449};
450
451static 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,
465};
466
467/* Info of regulators for DA9063 */
468static const struct da9063_regulator_info da9063_regulator_info[] = {
469 {
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),
476 },
477 {
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),
484 },
485 {
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),
492 },
493 {
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),
500 },
501 {
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),
508 },
509 {
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),
516 },
517 {
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),
525 },
526 {
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),
534 },
535 {
536 DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
537 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO1_SEL),
538 },
539 {
540 DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
541 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO2_SEL),
542 },
543 {
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),
547 },
548 {
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),
552 },
553 {
554 DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
555 .suspend = BFIELD(DA9063_REG_LDO5_CONT, DA9063_VLDO5_SEL),
556 },
557 {
558 DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
559 .suspend = BFIELD(DA9063_REG_LDO6_CONT, DA9063_VLDO6_SEL),
560 },
561 {
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),
565 },
566 {
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),
570 },
571 {
572 DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
573 .suspend = BFIELD(DA9063_REG_LDO9_CONT, DA9063_VLDO9_SEL),
574 },
575 {
576 DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
577 .suspend = BFIELD(DA9063_REG_LDO10_CONT, DA9063_VLDO10_SEL),
578 },
579 {
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),
583 },
584};
585
586/* Link chip model with regulators info table */
587static struct da9063_dev_model regulators_models[] = {
588 {
589 .regulator_info = da9063_regulator_info,
590 .n_regulators = ARRAY_SIZE(da9063_regulator_info),
591 .dev_model = PMIC_DA9063,
592 },
593 { }
594};
595
596/* Regulator interrupt handlers */
Axel Lin632b3d62013-09-01 12:19:51 +0800597static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200598{
599 struct da9063_regulators *regulators = data;
600 struct da9063 *hw = regulators->regulator[0].hw;
601 struct da9063_regulator *regl;
602 int bits, i , ret;
603
604 ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
605 if (ret < 0)
606 return IRQ_NONE;
607
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;
612
613 if (BIT(regl->info->oc_event.lsb) & bits)
614 regulator_notifier_call_chain(regl->rdev,
615 REGULATOR_EVENT_OVER_CURRENT, NULL);
616 }
617
618 return IRQ_HANDLED;
619}
620
621/*
622 * Probing and Initialisation functions
623 */
624static const struct regulator_init_data *da9063_get_regulator_initdata(
625 const struct da9063_regulators_pdata *regl_pdata, int id)
626{
627 int i;
628
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;
632 }
633
634 return NULL;
635}
636
637#ifdef CONFIG_OF
638static 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", },
658};
659
660static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
661 struct platform_device *pdev,
662 struct of_regulator_match **da9063_reg_matches)
663{
664 struct da9063_regulators_pdata *pdata;
665 struct da9063_regulator_data *rdata;
666 struct device_node *node;
667 int i, n, num;
668
Sachin Kamat25cc2752014-02-14 17:19:58 +0530669 node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200670 if (!node) {
671 dev_err(&pdev->dev, "Regulators device node not found\n");
672 return ERR_PTR(-ENODEV);
673 }
674
675 num = of_regulator_match(&pdev->dev, node, da9063_matches,
676 ARRAY_SIZE(da9063_matches));
Sachin Kamat2da82152014-02-17 14:33:31 +0530677 of_node_put(node);
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200678 if (num < 0) {
679 dev_err(&pdev->dev, "Failed to match regulators\n");
680 return ERR_PTR(-EINVAL);
681 }
682
683 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
684 if (!pdata)
685 return ERR_PTR(-ENOMEM);
686
687 pdata->regulator_data = devm_kzalloc(&pdev->dev,
688 num * sizeof(*pdata->regulator_data),
689 GFP_KERNEL);
690 if (!pdata->regulator_data)
691 return ERR_PTR(-ENOMEM);
692 pdata->n_regulators = num;
693
694 n = 0;
695 for (i = 0; i < ARRAY_SIZE(da9063_matches); i++) {
696 if (!da9063_matches[i].init_data)
697 continue;
698
699 rdata = &pdata->regulator_data[n];
700 rdata->id = i;
701 rdata->initdata = da9063_matches[i].init_data;
702
703 n++;
704 };
705
706 *da9063_reg_matches = da9063_matches;
707 return pdata;
708}
709#else
710static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
711 struct platform_device *pdev,
712 struct of_regulator_match **da9063_reg_matches)
713{
Colin Ian Kingf377ed12014-03-11 11:25:09 +0000714 *da9063_reg_matches = NULL;
Geert Uytterhoevene8cb2042013-09-09 13:49:26 +0200715 return ERR_PTR(-ENODEV);
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200716}
717#endif
718
719static int da9063_regulator_probe(struct platform_device *pdev)
720{
721 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
722 struct da9063_pdata *da9063_pdata = dev_get_platdata(da9063->dev);
Geert Uytterhoeven0fdfd402013-09-09 13:49:27 +0200723 struct of_regulator_match *da9063_reg_matches = NULL;
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200724 struct da9063_regulators_pdata *regl_pdata;
725 const struct da9063_dev_model *model;
726 struct da9063_regulators *regulators;
727 struct da9063_regulator *regl;
728 struct regulator_config config;
729 bool bcores_merged, bmem_bio_merged;
730 int id, irq, n, n_regulators, ret, val;
731 size_t size;
732
733 regl_pdata = da9063_pdata ? da9063_pdata->regulators_pdata : NULL;
734
735 if (!regl_pdata)
736 regl_pdata = da9063_parse_regulators_dt(pdev,
737 &da9063_reg_matches);
738
739 if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
740 dev_err(&pdev->dev,
741 "No regulators defined for the platform\n");
742 return PTR_ERR(regl_pdata);
743 }
744
745 /* Find regulators set for particular device model */
746 for (model = regulators_models; model->regulator_info; model++) {
747 if (model->dev_model == da9063->model)
748 break;
749 }
750 if (!model->regulator_info) {
751 dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
752 da9063->model);
753 return -ENODEV;
754 }
755
756 ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
757 if (ret < 0) {
758 dev_err(&pdev->dev,
759 "Error while reading BUCKs configuration\n");
Sachin Kamat8a7963d2014-02-18 16:11:05 +0530760 return ret;
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200761 }
762 bcores_merged = val & DA9063_BCORE_MERGE;
763 bmem_bio_merged = val & DA9063_BUCK_MERGE;
764
765 n_regulators = model->n_regulators;
766 if (bcores_merged)
767 n_regulators -= 2; /* remove BCORE1, BCORE2 */
768 else
769 n_regulators--; /* remove BCORES_MERGED */
770 if (bmem_bio_merged)
771 n_regulators -= 2; /* remove BMEM, BIO */
772 else
773 n_regulators--; /* remove BMEM_BIO_MERGED */
774
775 /* Allocate memory required by usable regulators */
776 size = sizeof(struct da9063_regulators) +
777 n_regulators * sizeof(struct da9063_regulator);
778 regulators = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
Sachin Kamat8b5baa52014-02-20 14:23:01 +0530779 if (!regulators)
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200780 return -ENOMEM;
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200781
782 regulators->n_regulators = n_regulators;
783 platform_set_drvdata(pdev, regulators);
784
785 /* Register all regulators declared in platform information */
786 n = 0;
787 id = 0;
788 while (n < regulators->n_regulators) {
789 /* Skip regulator IDs depending on merge mode configuration */
790 switch (id) {
791 case DA9063_ID_BCORE1:
792 case DA9063_ID_BCORE2:
793 if (bcores_merged) {
794 id++;
795 continue;
796 }
797 break;
798 case DA9063_ID_BMEM:
799 case DA9063_ID_BIO:
800 if (bmem_bio_merged) {
801 id++;
802 continue;
803 }
804 break;
805 case DA9063_ID_BCORES_MERGED:
806 if (!bcores_merged) {
807 id++;
808 continue;
809 }
810 break;
811 case DA9063_ID_BMEM_BIO_MERGED:
812 if (!bmem_bio_merged) {
813 id++;
814 continue;
815 }
816 break;
817 }
818
819 /* Initialise regulator structure */
820 regl = &regulators->regulator[n];
821 regl->hw = da9063;
822 regl->info = &model->regulator_info[id];
823 regl->desc = regl->info->desc;
824 regl->desc.type = REGULATOR_VOLTAGE;
825 regl->desc.owner = THIS_MODULE;
826
827 if (regl->info->mode.reg)
828 regl->mode = devm_regmap_field_alloc(&pdev->dev,
829 da9063->regmap, regl->info->mode);
830 if (regl->info->suspend.reg)
831 regl->suspend = devm_regmap_field_alloc(&pdev->dev,
832 da9063->regmap, regl->info->suspend);
833 if (regl->info->sleep.reg)
834 regl->sleep = devm_regmap_field_alloc(&pdev->dev,
835 da9063->regmap, regl->info->sleep);
836 if (regl->info->suspend_sleep.reg)
837 regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
838 da9063->regmap, regl->info->suspend_sleep);
839 if (regl->info->ilimit.reg)
840 regl->ilimit = devm_regmap_field_alloc(&pdev->dev,
841 da9063->regmap, regl->info->ilimit);
842
843 /* Register regulator */
844 memset(&config, 0, sizeof(config));
845 config.dev = &pdev->dev;
846 config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
847 config.driver_data = regl;
848 if (da9063_reg_matches)
849 config.of_node = da9063_reg_matches[id].of_node;
850 config.regmap = da9063->regmap;
Jingoo Hane97bba92013-09-30 09:53:09 +0900851 regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
852 &config);
Axel Line5158002013-08-30 20:08:42 +0800853 if (IS_ERR(regl->rdev)) {
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200854 dev_err(&pdev->dev,
855 "Failed to register %s regulator\n",
856 regl->desc.name);
Jingoo Hane97bba92013-09-30 09:53:09 +0900857 return PTR_ERR(regl->rdev);
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200858 }
859 id++;
860 n++;
861 }
862
863 /* LDOs overcurrent event support */
864 irq = platform_get_irq_byname(pdev, "LDO_LIM");
865 if (irq < 0) {
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200866 dev_err(&pdev->dev, "Failed to get IRQ.\n");
Jingoo Hane97bba92013-09-30 09:53:09 +0900867 return irq;
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200868 }
869
870 regulators->irq_ldo_lim = regmap_irq_get_virq(da9063->regmap_irq, irq);
871 if (regulators->irq_ldo_lim >= 0) {
872 ret = request_threaded_irq(regulators->irq_ldo_lim,
873 NULL, da9063_ldo_lim_event,
874 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
875 "LDO_LIM", regulators);
876 if (ret) {
877 dev_err(&pdev->dev,
878 "Failed to request LDO_LIM IRQ.\n");
879 regulators->irq_ldo_lim = -ENXIO;
880 }
881 }
882
883 return 0;
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200884}
885
886static int da9063_regulator_remove(struct platform_device *pdev)
887{
888 struct da9063_regulators *regulators = platform_get_drvdata(pdev);
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200889
890 free_irq(regulators->irq_ldo_lim, regulators);
891 free_irq(regulators->irq_uvov, regulators);
892
Krystian Garbaciak69ca3e52013-07-29 19:00:45 +0200893 return 0;
894}
895
896static struct platform_driver da9063_regulator_driver = {
897 .driver = {
898 .name = DA9063_DRVNAME_REGULATORS,
899 .owner = THIS_MODULE,
900 },
901 .probe = da9063_regulator_probe,
902 .remove = da9063_regulator_remove,
903};
904
905static int __init da9063_regulator_init(void)
906{
907 return platform_driver_register(&da9063_regulator_driver);
908}
909subsys_initcall(da9063_regulator_init);
910
911static void __exit da9063_regulator_cleanup(void)
912{
913 platform_driver_unregister(&da9063_regulator_driver);
914}
915module_exit(da9063_regulator_cleanup);
916
917
918/* Module information */
919MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
920MODULE_DESCRIPTION("DA9063 regulators driver");
921MODULE_LICENSE("GPL");
922MODULE_ALIAS("paltform:" DA9063_DRVNAME_REGULATORS);