blob: d029c941a1e10af66e169e9bcfaef3115889ed75 [file] [log] [blame]
Ashish Jangamf6130be2012-11-01 13:57:56 +05301/*
2* Regulator driver for DA9055 PMIC
3*
4* Copyright(c) 2012 Dialog Semiconductor Ltd.
5*
6* Author: David Dajun Chen <dchen@diasemi.com>
7*
8* This program is free software; you can redistribute it and/or modify
9* it under the terms of the GNU General Public License as published by
10* the Free Software Foundation; either version 2 of the License, or
11* (at your option) any later version.
12*
13*/
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/err.h>
18#include <linux/gpio.h>
19#include <linux/platform_device.h>
20#include <linux/regulator/driver.h>
21#include <linux/regulator/machine.h>
Adam Thomson8b61a282014-02-06 18:03:13 +000022#include <linux/of.h>
23#include <linux/regulator/of_regulator.h>
Ashish Jangamf6130be2012-11-01 13:57:56 +053024
25#include <linux/mfd/da9055/core.h>
26#include <linux/mfd/da9055/reg.h>
27#include <linux/mfd/da9055/pdata.h>
28
29#define DA9055_MIN_UA 0
30#define DA9055_MAX_UA 3
31
32#define DA9055_LDO_MODE_SYNC 0
33#define DA9055_LDO_MODE_SLEEP 1
34
35#define DA9055_BUCK_MODE_SLEEP 1
36#define DA9055_BUCK_MODE_SYNC 2
37#define DA9055_BUCK_MODE_AUTO 3
38
39/* DA9055 REGULATOR IDs */
40#define DA9055_ID_BUCK1 0
41#define DA9055_ID_BUCK2 1
42#define DA9055_ID_LDO1 2
43#define DA9055_ID_LDO2 3
44#define DA9055_ID_LDO3 4
45#define DA9055_ID_LDO4 5
46#define DA9055_ID_LDO5 6
47#define DA9055_ID_LDO6 7
48
49/* DA9055 BUCK current limit */
50static const int da9055_current_limits[] = { 500000, 600000, 700000, 800000 };
51
52struct da9055_conf_reg {
53 int reg;
54 int sel_mask;
55 int en_mask;
56};
57
58struct da9055_volt_reg {
59 int reg_a;
60 int reg_b;
61 int sl_shift;
Ashish Jangamf6130be2012-11-01 13:57:56 +053062 int v_mask;
Ashish Jangamf6130be2012-11-01 13:57:56 +053063};
64
65struct da9055_mode_reg {
66 int reg;
67 int mask;
68 int shift;
69};
70
71struct da9055_regulator_info {
72 struct regulator_desc reg_desc;
73 struct da9055_conf_reg conf;
74 struct da9055_volt_reg volt;
75 struct da9055_mode_reg mode;
76};
77
78struct da9055_regulator {
79 struct da9055 *da9055;
80 struct da9055_regulator_info *info;
81 struct regulator_dev *rdev;
82 enum gpio_select reg_rselect;
83};
84
85static unsigned int da9055_buck_get_mode(struct regulator_dev *rdev)
86{
87 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
88 struct da9055_regulator_info *info = regulator->info;
89 int ret, mode = 0;
90
91 ret = da9055_reg_read(regulator->da9055, info->mode.reg);
92 if (ret < 0)
93 return ret;
94
95 switch ((ret & info->mode.mask) >> info->mode.shift) {
96 case DA9055_BUCK_MODE_SYNC:
97 mode = REGULATOR_MODE_FAST;
98 break;
99 case DA9055_BUCK_MODE_AUTO:
100 mode = REGULATOR_MODE_NORMAL;
101 break;
102 case DA9055_BUCK_MODE_SLEEP:
103 mode = REGULATOR_MODE_STANDBY;
104 break;
105 }
106
107 return mode;
108}
109
110static int da9055_buck_set_mode(struct regulator_dev *rdev,
111 unsigned int mode)
112{
113 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
114 struct da9055_regulator_info *info = regulator->info;
115 int val = 0;
116
117 switch (mode) {
118 case REGULATOR_MODE_FAST:
119 val = DA9055_BUCK_MODE_SYNC << info->mode.shift;
120 break;
121 case REGULATOR_MODE_NORMAL:
122 val = DA9055_BUCK_MODE_AUTO << info->mode.shift;
123 break;
124 case REGULATOR_MODE_STANDBY:
125 val = DA9055_BUCK_MODE_SLEEP << info->mode.shift;
126 break;
127 }
128
129 return da9055_reg_update(regulator->da9055, info->mode.reg,
130 info->mode.mask, val);
131}
132
133static unsigned int da9055_ldo_get_mode(struct regulator_dev *rdev)
134{
135 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
136 struct da9055_regulator_info *info = regulator->info;
137 int ret;
138
139 ret = da9055_reg_read(regulator->da9055, info->volt.reg_b);
140 if (ret < 0)
141 return ret;
142
143 if (ret >> info->volt.sl_shift)
144 return REGULATOR_MODE_STANDBY;
145 else
146 return REGULATOR_MODE_NORMAL;
147}
148
149static int da9055_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
150{
151 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
152 struct da9055_regulator_info *info = regulator->info;
153 struct da9055_volt_reg volt = info->volt;
154 int val = 0;
155
156 switch (mode) {
157 case REGULATOR_MODE_NORMAL:
158 case REGULATOR_MODE_FAST:
159 val = DA9055_LDO_MODE_SYNC;
160 break;
161 case REGULATOR_MODE_STANDBY:
162 val = DA9055_LDO_MODE_SLEEP;
163 break;
164 }
165
166 return da9055_reg_update(regulator->da9055, volt.reg_b,
167 1 << volt.sl_shift,
168 val << volt.sl_shift);
169}
170
171static int da9055_buck_get_current_limit(struct regulator_dev *rdev)
172{
173 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
174 struct da9055_regulator_info *info = regulator->info;
175 int ret;
176
177 ret = da9055_reg_read(regulator->da9055, DA9055_REG_BUCK_LIM);
178 if (ret < 0)
179 return ret;
180
181 ret &= info->mode.mask;
182 return da9055_current_limits[ret >> info->mode.shift];
183}
184
185static int da9055_buck_set_current_limit(struct regulator_dev *rdev, int min_uA,
186 int max_uA)
187{
188 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
189 struct da9055_regulator_info *info = regulator->info;
Axel Linf509fd42012-11-26 15:26:41 +0800190 int i;
Ashish Jangamf6130be2012-11-01 13:57:56 +0530191
Axel Linf509fd42012-11-26 15:26:41 +0800192 for (i = ARRAY_SIZE(da9055_current_limits) - 1; i >= 0; i--) {
193 if ((min_uA <= da9055_current_limits[i]) &&
194 (da9055_current_limits[i] <= max_uA))
195 return da9055_reg_update(regulator->da9055,
196 DA9055_REG_BUCK_LIM,
197 info->mode.mask,
198 i << info->mode.shift);
Ashish Jangamf6130be2012-11-01 13:57:56 +0530199 }
200
Axel Linf509fd42012-11-26 15:26:41 +0800201 return -EINVAL;
Ashish Jangamf6130be2012-11-01 13:57:56 +0530202}
203
204static int da9055_regulator_get_voltage_sel(struct regulator_dev *rdev)
205{
206 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
207 struct da9055_regulator_info *info = regulator->info;
208 struct da9055_volt_reg volt = info->volt;
209 int ret, sel;
210
211 /*
212 * There are two voltage register set A & B for voltage ramping but
213 * either one of then can be active therefore we first determine
214 * the active register set.
215 */
216 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
217 if (ret < 0)
218 return ret;
219
220 ret &= info->conf.sel_mask;
221
222 /* Get the voltage for the active register set A/B */
223 if (ret == DA9055_REGUALTOR_SET_A)
224 ret = da9055_reg_read(regulator->da9055, volt.reg_a);
225 else
226 ret = da9055_reg_read(regulator->da9055, volt.reg_b);
227
228 if (ret < 0)
229 return ret;
230
231 sel = (ret & volt.v_mask);
Axel Lin6b1f8a42012-11-27 10:26:14 +0800232 return sel;
Ashish Jangamf6130be2012-11-01 13:57:56 +0530233}
234
Ashish Jangamf6130be2012-11-01 13:57:56 +0530235static int da9055_regulator_set_voltage_sel(struct regulator_dev *rdev,
236 unsigned int selector)
237{
238 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
239 struct da9055_regulator_info *info = regulator->info;
240 int ret;
241
242 /*
243 * Regulator register set A/B is not selected through GPIO therefore
244 * we use default register set A for voltage ramping.
245 */
246 if (regulator->reg_rselect == NO_GPIO) {
247 /* Select register set A */
248 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
249 info->conf.sel_mask, DA9055_SEL_REG_A);
250 if (ret < 0)
251 return ret;
252
253 /* Set the voltage */
Axel Lin369cf602012-11-20 15:36:46 +0800254 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
255 info->volt.v_mask, selector);
Ashish Jangamf6130be2012-11-01 13:57:56 +0530256 }
257
258 /*
259 * Here regulator register set A/B is selected through GPIO.
260 * Therefore we first determine the selected register set A/B and
261 * then set the desired voltage for that register set A/B.
262 */
263 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
264 if (ret < 0)
265 return ret;
266
267 ret &= info->conf.sel_mask;
268
269 /* Set the voltage */
270 if (ret == DA9055_REGUALTOR_SET_A)
Axel Lin369cf602012-11-20 15:36:46 +0800271 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
272 info->volt.v_mask, selector);
Ashish Jangamf6130be2012-11-01 13:57:56 +0530273 else
Axel Lin369cf602012-11-20 15:36:46 +0800274 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
275 info->volt.v_mask, selector);
Ashish Jangamf6130be2012-11-01 13:57:56 +0530276}
277
278static int da9055_regulator_set_suspend_voltage(struct regulator_dev *rdev,
279 int uV)
280{
281 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
282 struct da9055_regulator_info *info = regulator->info;
283 int ret;
284
285 /* Select register set B for suspend voltage ramping. */
286 if (regulator->reg_rselect == NO_GPIO) {
287 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
288 info->conf.sel_mask, DA9055_SEL_REG_B);
289 if (ret < 0)
290 return ret;
291 }
292
Axel Lin6b1f8a42012-11-27 10:26:14 +0800293 ret = regulator_map_voltage_linear(rdev, uV, uV);
Ashish Jangamf6130be2012-11-01 13:57:56 +0530294 if (ret < 0)
295 return ret;
296
Axel Lin369cf602012-11-20 15:36:46 +0800297 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
298 info->volt.v_mask, ret);
Ashish Jangamf6130be2012-11-01 13:57:56 +0530299}
300
301static int da9055_suspend_enable(struct regulator_dev *rdev)
302{
303 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
304 struct da9055_regulator_info *info = regulator->info;
305
306 /* Select register set B for voltage ramping. */
307 if (regulator->reg_rselect == NO_GPIO)
308 return da9055_reg_update(regulator->da9055, info->conf.reg,
309 info->conf.sel_mask, DA9055_SEL_REG_B);
310 else
311 return 0;
312}
313
314static int da9055_suspend_disable(struct regulator_dev *rdev)
315{
316 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
317 struct da9055_regulator_info *info = regulator->info;
318
319 /* Diselect register set B. */
320 if (regulator->reg_rselect == NO_GPIO)
321 return da9055_reg_update(regulator->da9055, info->conf.reg,
322 info->conf.sel_mask, DA9055_SEL_REG_A);
323 else
324 return 0;
325}
326
Julia Lawall71242b42015-12-19 16:07:09 +0100327static const struct regulator_ops da9055_buck_ops = {
Ashish Jangamf6130be2012-11-01 13:57:56 +0530328 .get_mode = da9055_buck_get_mode,
329 .set_mode = da9055_buck_set_mode,
330
331 .get_current_limit = da9055_buck_get_current_limit,
332 .set_current_limit = da9055_buck_set_current_limit,
333
334 .get_voltage_sel = da9055_regulator_get_voltage_sel,
335 .set_voltage_sel = da9055_regulator_set_voltage_sel,
Axel Lin6b1f8a42012-11-27 10:26:14 +0800336 .list_voltage = regulator_list_voltage_linear,
337 .map_voltage = regulator_map_voltage_linear,
Ashish Jangamf6130be2012-11-01 13:57:56 +0530338 .is_enabled = regulator_is_enabled_regmap,
339 .enable = regulator_enable_regmap,
340 .disable = regulator_disable_regmap,
341
342 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
343 .set_suspend_enable = da9055_suspend_enable,
344 .set_suspend_disable = da9055_suspend_disable,
345 .set_suspend_mode = da9055_buck_set_mode,
346};
347
Julia Lawall71242b42015-12-19 16:07:09 +0100348static const struct regulator_ops da9055_ldo_ops = {
Ashish Jangamf6130be2012-11-01 13:57:56 +0530349 .get_mode = da9055_ldo_get_mode,
350 .set_mode = da9055_ldo_set_mode,
351
352 .get_voltage_sel = da9055_regulator_get_voltage_sel,
353 .set_voltage_sel = da9055_regulator_set_voltage_sel,
Axel Lin6b1f8a42012-11-27 10:26:14 +0800354 .list_voltage = regulator_list_voltage_linear,
355 .map_voltage = regulator_map_voltage_linear,
Ashish Jangamf6130be2012-11-01 13:57:56 +0530356 .is_enabled = regulator_is_enabled_regmap,
357 .enable = regulator_enable_regmap,
358 .disable = regulator_disable_regmap,
359
360 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
361 .set_suspend_enable = da9055_suspend_enable,
362 .set_suspend_disable = da9055_suspend_disable,
363 .set_suspend_mode = da9055_ldo_set_mode,
364
365};
366
367#define DA9055_LDO(_id, step, min, max, vbits, voffset) \
368{\
369 .reg_desc = {\
370 .name = #_id,\
371 .ops = &da9055_ldo_ops,\
372 .type = REGULATOR_VOLTAGE,\
373 .id = DA9055_ID_##_id,\
Axel Lin369cf602012-11-20 15:36:46 +0800374 .n_voltages = (max - min) / step + 1 + (voffset), \
Ashish Jangamf6130be2012-11-01 13:57:56 +0530375 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
376 .enable_mask = 1, \
377 .min_uV = (min) * 1000,\
378 .uV_step = (step) * 1000,\
Axel Lin6b1f8a42012-11-27 10:26:14 +0800379 .linear_min_sel = (voffset),\
Ashish Jangamf6130be2012-11-01 13:57:56 +0530380 .owner = THIS_MODULE,\
381 },\
382 .conf = {\
383 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
384 .sel_mask = (1 << 4),\
385 .en_mask = 1,\
386 },\
387 .volt = {\
388 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
389 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
390 .sl_shift = 7,\
Ashish Jangamf6130be2012-11-01 13:57:56 +0530391 .v_mask = (1 << (vbits)) - 1,\
Ashish Jangamf6130be2012-11-01 13:57:56 +0530392 },\
393}
394
395#define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \
396{\
397 .reg_desc = {\
398 .name = #_id,\
399 .ops = &da9055_buck_ops,\
400 .type = REGULATOR_VOLTAGE,\
401 .id = DA9055_ID_##_id,\
Axel Lin369cf602012-11-20 15:36:46 +0800402 .n_voltages = (max - min) / step + 1 + (voffset), \
Ashish Jangamf6130be2012-11-01 13:57:56 +0530403 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
404 .enable_mask = 1,\
405 .min_uV = (min) * 1000,\
406 .uV_step = (step) * 1000,\
Axel Lin6b1f8a42012-11-27 10:26:14 +0800407 .linear_min_sel = (voffset),\
Ashish Jangamf6130be2012-11-01 13:57:56 +0530408 .owner = THIS_MODULE,\
409 },\
410 .conf = {\
411 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
412 .sel_mask = (1 << 4),\
413 .en_mask = 1,\
414 },\
415 .volt = {\
416 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
417 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
418 .sl_shift = 7,\
Ashish Jangamf6130be2012-11-01 13:57:56 +0530419 .v_mask = (1 << (vbits)) - 1,\
Ashish Jangamf6130be2012-11-01 13:57:56 +0530420 },\
421 .mode = {\
422 .reg = DA9055_REG_BCORE_MODE,\
423 .mask = (mbits),\
424 .shift = (sbits),\
425 },\
426}
427
428static struct da9055_regulator_info da9055_regulator_info[] = {
429 DA9055_BUCK(BUCK1, 25, 725, 2075, 6, 9, 0xc, 2),
430 DA9055_BUCK(BUCK2, 25, 925, 2500, 6, 0, 3, 0),
431 DA9055_LDO(LDO1, 50, 900, 3300, 6, 2),
432 DA9055_LDO(LDO2, 50, 900, 3300, 6, 3),
433 DA9055_LDO(LDO3, 50, 900, 3300, 6, 2),
434 DA9055_LDO(LDO4, 50, 900, 3300, 6, 2),
435 DA9055_LDO(LDO5, 50, 900, 2750, 6, 2),
436 DA9055_LDO(LDO6, 20, 900, 3300, 7, 0),
437};
438
439/*
440 * Configures regulator to be controlled either through GPIO 1 or 2.
441 * GPIO can control regulator state and/or select the regulator register
442 * set A/B for voltage ramping.
443 */
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800444static int da9055_gpio_init(struct da9055_regulator *regulator,
445 struct regulator_config *config,
446 struct da9055_pdata *pdata, int id)
Ashish Jangamf6130be2012-11-01 13:57:56 +0530447{
448 struct da9055_regulator_info *info = regulator->info;
449 int ret = 0;
450
Adam Thomson8b61a282014-02-06 18:03:13 +0000451 if (!pdata)
452 return 0;
453
Ashish Jangamf6130be2012-11-01 13:57:56 +0530454 if (pdata->gpio_ren && pdata->gpio_ren[id]) {
455 char name[18];
456 int gpio_mux = pdata->gpio_ren[id];
457
458 config->ena_gpio = pdata->ena_gpio[id];
459 config->ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
460 config->ena_gpio_invert = 1;
461
462 /*
463 * GPI pin is muxed with regulator to control the
464 * regulator state.
465 */
466 sprintf(name, "DA9055 GPI %d", gpio_mux);
467 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
468 name);
469 if (ret < 0)
470 goto err;
471
472 /*
473 * Let the regulator know that its state is controlled
474 * through GPI.
475 */
476 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
477 DA9055_E_GPI_MASK,
478 pdata->reg_ren[id]
479 << DA9055_E_GPI_SHIFT);
480 if (ret < 0)
481 goto err;
482 }
483
Axel Lin26cbd302012-11-16 00:35:14 +0800484 if (pdata->gpio_rsel && pdata->gpio_rsel[id]) {
Ashish Jangamf6130be2012-11-01 13:57:56 +0530485 char name[18];
486 int gpio_mux = pdata->gpio_rsel[id];
487
488 regulator->reg_rselect = pdata->reg_rsel[id];
489
490 /*
491 * GPI pin is muxed with regulator to select the
492 * regulator register set A/B for voltage ramping.
493 */
494 sprintf(name, "DA9055 GPI %d", gpio_mux);
495 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
496 name);
497 if (ret < 0)
498 goto err;
499
500 /*
501 * Let the regulator know that its register set A/B
502 * will be selected through GPI for voltage ramping.
503 */
504 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
505 DA9055_V_GPI_MASK,
506 pdata->reg_rsel[id]
507 << DA9055_V_GPI_SHIFT);
508 }
509
510err:
511 return ret;
512}
513
514static irqreturn_t da9055_ldo5_6_oc_irq(int irq, void *data)
515{
516 struct da9055_regulator *regulator = data;
517
518 regulator_notifier_call_chain(regulator->rdev,
519 REGULATOR_EVENT_OVER_CURRENT, NULL);
520
521 return IRQ_HANDLED;
522}
523
524static inline struct da9055_regulator_info *find_regulator_info(int id)
525{
526 struct da9055_regulator_info *info;
527 int i;
528
529 for (i = 0; i < ARRAY_SIZE(da9055_regulator_info); i++) {
530 info = &da9055_regulator_info[i];
531 if (info->reg_desc.id == id)
532 return info;
533 }
534
535 return NULL;
536}
537
Adam Thomson8b61a282014-02-06 18:03:13 +0000538#ifdef CONFIG_OF
539static struct of_regulator_match da9055_reg_matches[] = {
540 { .name = "BUCK1", },
541 { .name = "BUCK2", },
542 { .name = "LDO1", },
543 { .name = "LDO2", },
544 { .name = "LDO3", },
545 { .name = "LDO4", },
546 { .name = "LDO5", },
547 { .name = "LDO6", },
548};
549
550static int da9055_regulator_dt_init(struct platform_device *pdev,
551 struct da9055_regulator *regulator,
552 struct regulator_config *config,
553 int regid)
554{
555 struct device_node *nproot, *np;
556 int ret;
557
558 nproot = of_node_get(pdev->dev.parent->of_node);
559 if (!nproot)
560 return -ENODEV;
561
Sachin Kamata4b4e662014-02-14 17:19:57 +0530562 np = of_get_child_by_name(nproot, "regulators");
Adam Thomson8b61a282014-02-06 18:03:13 +0000563 if (!np)
564 return -ENODEV;
565
566 ret = of_regulator_match(&pdev->dev, np, &da9055_reg_matches[regid], 1);
567 of_node_put(nproot);
568 if (ret < 0) {
569 dev_err(&pdev->dev, "Error matching regulator: %d\n", ret);
Sachin Kamatfe2eb722014-02-18 16:11:06 +0530570 return ret;
Adam Thomson8b61a282014-02-06 18:03:13 +0000571 }
572
573 config->init_data = da9055_reg_matches[regid].init_data;
574 config->of_node = da9055_reg_matches[regid].of_node;
575
576 if (!config->of_node)
577 return -ENODEV;
578
579 return 0;
580}
581#else
SeongJae Parkdae6cdb2014-02-12 14:11:07 +0900582static inline int da9055_regulator_dt_init(struct platform_device *pdev,
Adam Thomson8b61a282014-02-06 18:03:13 +0000583 struct da9055_regulator *regulator,
584 struct regulator_config *config,
585 int regid)
586{
587 return -ENODEV;
588}
589#endif /* CONFIG_OF */
590
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800591static int da9055_regulator_probe(struct platform_device *pdev)
Ashish Jangamf6130be2012-11-01 13:57:56 +0530592{
593 struct regulator_config config = { };
594 struct da9055_regulator *regulator;
595 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900596 struct da9055_pdata *pdata = dev_get_platdata(da9055->dev);
Ashish Jangamf6130be2012-11-01 13:57:56 +0530597 int ret, irq;
598
Ashish Jangamf6130be2012-11-01 13:57:56 +0530599 regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9055_regulator),
600 GFP_KERNEL);
601 if (!regulator)
602 return -ENOMEM;
603
604 regulator->info = find_regulator_info(pdev->id);
605 if (regulator->info == NULL) {
606 dev_err(&pdev->dev, "invalid regulator ID specified\n");
607 return -EINVAL;
608 }
609
610 regulator->da9055 = da9055;
611 config.dev = &pdev->dev;
612 config.driver_data = regulator;
613 config.regmap = da9055->regmap;
614
Adam Thomson8b61a282014-02-06 18:03:13 +0000615 if (pdata && pdata->regulators) {
Ashish Jangamf6130be2012-11-01 13:57:56 +0530616 config.init_data = pdata->regulators[pdev->id];
Adam Thomson8b61a282014-02-06 18:03:13 +0000617 } else {
618 ret = da9055_regulator_dt_init(pdev, regulator, &config,
619 pdev->id);
620 if (ret < 0)
621 return ret;
622 }
Ashish Jangamf6130be2012-11-01 13:57:56 +0530623
624 ret = da9055_gpio_init(regulator, &config, pdata, pdev->id);
625 if (ret < 0)
626 return ret;
627
Axel Lin0d328802013-09-03 14:26:10 +0800628 regulator->rdev = devm_regulator_register(&pdev->dev,
629 &regulator->info->reg_desc,
630 &config);
Ashish Jangamf6130be2012-11-01 13:57:56 +0530631 if (IS_ERR(regulator->rdev)) {
632 dev_err(&pdev->dev, "Failed to register regulator %s\n",
633 regulator->info->reg_desc.name);
Axel Lin0d328802013-09-03 14:26:10 +0800634 return PTR_ERR(regulator->rdev);
Ashish Jangamf6130be2012-11-01 13:57:56 +0530635 }
636
637 /* Only LDO 5 and 6 has got the over current interrupt */
638 if (pdev->id == DA9055_ID_LDO5 || pdev->id == DA9055_ID_LDO6) {
639 irq = platform_get_irq_byname(pdev, "REGULATOR");
Adam Thomsonf17083c2014-02-06 18:03:21 +0000640 if (irq < 0)
641 return irq;
642
Ashish Jangamf6130be2012-11-01 13:57:56 +0530643 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
644 da9055_ldo5_6_oc_irq,
645 IRQF_TRIGGER_HIGH |
646 IRQF_ONESHOT |
647 IRQF_PROBE_SHARED,
648 pdev->name, regulator);
649 if (ret != 0) {
650 if (ret != -EBUSY) {
651 dev_err(&pdev->dev,
652 "Failed to request Regulator IRQ %d: %d\n",
653 irq, ret);
Axel Lin0d328802013-09-03 14:26:10 +0800654 return ret;
Ashish Jangamf6130be2012-11-01 13:57:56 +0530655 }
656 }
657 }
658
659 platform_set_drvdata(pdev, regulator);
660
661 return 0;
Ashish Jangamf6130be2012-11-01 13:57:56 +0530662}
663
664static struct platform_driver da9055_regulator_driver = {
665 .probe = da9055_regulator_probe,
Ashish Jangamf6130be2012-11-01 13:57:56 +0530666 .driver = {
667 .name = "da9055-regulator",
Ashish Jangamf6130be2012-11-01 13:57:56 +0530668 },
669};
670
671static int __init da9055_regulator_init(void)
672{
673 return platform_driver_register(&da9055_regulator_driver);
674}
675subsys_initcall(da9055_regulator_init);
676
677static void __exit da9055_regulator_exit(void)
678{
679 platform_driver_unregister(&da9055_regulator_driver);
680}
681module_exit(da9055_regulator_exit);
682
683MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
684MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC");
685MODULE_LICENSE("GPL");
686MODULE_ALIAS("platform:da9055-regulator");