PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / regulator / da9055-regulator.c
blobb14ebdad5dd2508f30854ac97e2b803cb5fa6e03
1 /*
2 * Regulator driver for DA9055 PMIC
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
6 * Author: David Dajun Chen <dchen@diasemi.com>
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.
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>
23 #include <linux/mfd/da9055/core.h>
24 #include <linux/mfd/da9055/reg.h>
25 #include <linux/mfd/da9055/pdata.h>
27 #define DA9055_MIN_UA 0
28 #define DA9055_MAX_UA 3
30 #define DA9055_LDO_MODE_SYNC 0
31 #define DA9055_LDO_MODE_SLEEP 1
33 #define DA9055_BUCK_MODE_SLEEP 1
34 #define DA9055_BUCK_MODE_SYNC 2
35 #define DA9055_BUCK_MODE_AUTO 3
37 /* DA9055 REGULATOR IDs */
38 #define DA9055_ID_BUCK1 0
39 #define DA9055_ID_BUCK2 1
40 #define DA9055_ID_LDO1 2
41 #define DA9055_ID_LDO2 3
42 #define DA9055_ID_LDO3 4
43 #define DA9055_ID_LDO4 5
44 #define DA9055_ID_LDO5 6
45 #define DA9055_ID_LDO6 7
47 /* DA9055 BUCK current limit */
48 static const int da9055_current_limits[] = { 500000, 600000, 700000, 800000 };
50 struct da9055_conf_reg {
51 int reg;
52 int sel_mask;
53 int en_mask;
56 struct da9055_volt_reg {
57 int reg_a;
58 int reg_b;
59 int sl_shift;
60 int v_mask;
63 struct da9055_mode_reg {
64 int reg;
65 int mask;
66 int shift;
69 struct da9055_regulator_info {
70 struct regulator_desc reg_desc;
71 struct da9055_conf_reg conf;
72 struct da9055_volt_reg volt;
73 struct da9055_mode_reg mode;
76 struct da9055_regulator {
77 struct da9055 *da9055;
78 struct da9055_regulator_info *info;
79 struct regulator_dev *rdev;
80 enum gpio_select reg_rselect;
83 static unsigned int da9055_buck_get_mode(struct regulator_dev *rdev)
85 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
86 struct da9055_regulator_info *info = regulator->info;
87 int ret, mode = 0;
89 ret = da9055_reg_read(regulator->da9055, info->mode.reg);
90 if (ret < 0)
91 return ret;
93 switch ((ret & info->mode.mask) >> info->mode.shift) {
94 case DA9055_BUCK_MODE_SYNC:
95 mode = REGULATOR_MODE_FAST;
96 break;
97 case DA9055_BUCK_MODE_AUTO:
98 mode = REGULATOR_MODE_NORMAL;
99 break;
100 case DA9055_BUCK_MODE_SLEEP:
101 mode = REGULATOR_MODE_STANDBY;
102 break;
105 return mode;
108 static int da9055_buck_set_mode(struct regulator_dev *rdev,
109 unsigned int mode)
111 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
112 struct da9055_regulator_info *info = regulator->info;
113 int val = 0;
115 switch (mode) {
116 case REGULATOR_MODE_FAST:
117 val = DA9055_BUCK_MODE_SYNC << info->mode.shift;
118 break;
119 case REGULATOR_MODE_NORMAL:
120 val = DA9055_BUCK_MODE_AUTO << info->mode.shift;
121 break;
122 case REGULATOR_MODE_STANDBY:
123 val = DA9055_BUCK_MODE_SLEEP << info->mode.shift;
124 break;
127 return da9055_reg_update(regulator->da9055, info->mode.reg,
128 info->mode.mask, val);
131 static unsigned int da9055_ldo_get_mode(struct regulator_dev *rdev)
133 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
134 struct da9055_regulator_info *info = regulator->info;
135 int ret;
137 ret = da9055_reg_read(regulator->da9055, info->volt.reg_b);
138 if (ret < 0)
139 return ret;
141 if (ret >> info->volt.sl_shift)
142 return REGULATOR_MODE_STANDBY;
143 else
144 return REGULATOR_MODE_NORMAL;
147 static int da9055_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
149 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
150 struct da9055_regulator_info *info = regulator->info;
151 struct da9055_volt_reg volt = info->volt;
152 int val = 0;
154 switch (mode) {
155 case REGULATOR_MODE_NORMAL:
156 case REGULATOR_MODE_FAST:
157 val = DA9055_LDO_MODE_SYNC;
158 break;
159 case REGULATOR_MODE_STANDBY:
160 val = DA9055_LDO_MODE_SLEEP;
161 break;
164 return da9055_reg_update(regulator->da9055, volt.reg_b,
165 1 << volt.sl_shift,
166 val << volt.sl_shift);
169 static int da9055_buck_get_current_limit(struct regulator_dev *rdev)
171 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
172 struct da9055_regulator_info *info = regulator->info;
173 int ret;
175 ret = da9055_reg_read(regulator->da9055, DA9055_REG_BUCK_LIM);
176 if (ret < 0)
177 return ret;
179 ret &= info->mode.mask;
180 return da9055_current_limits[ret >> info->mode.shift];
183 static int da9055_buck_set_current_limit(struct regulator_dev *rdev, int min_uA,
184 int max_uA)
186 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
187 struct da9055_regulator_info *info = regulator->info;
188 int i;
190 for (i = ARRAY_SIZE(da9055_current_limits) - 1; i >= 0; i--) {
191 if ((min_uA <= da9055_current_limits[i]) &&
192 (da9055_current_limits[i] <= max_uA))
193 return da9055_reg_update(regulator->da9055,
194 DA9055_REG_BUCK_LIM,
195 info->mode.mask,
196 i << info->mode.shift);
199 return -EINVAL;
202 static int da9055_regulator_get_voltage_sel(struct regulator_dev *rdev)
204 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
205 struct da9055_regulator_info *info = regulator->info;
206 struct da9055_volt_reg volt = info->volt;
207 int ret, sel;
210 * There are two voltage register set A & B for voltage ramping but
211 * either one of then can be active therefore we first determine
212 * the active register set.
214 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
215 if (ret < 0)
216 return ret;
218 ret &= info->conf.sel_mask;
220 /* Get the voltage for the active register set A/B */
221 if (ret == DA9055_REGUALTOR_SET_A)
222 ret = da9055_reg_read(regulator->da9055, volt.reg_a);
223 else
224 ret = da9055_reg_read(regulator->da9055, volt.reg_b);
226 if (ret < 0)
227 return ret;
229 sel = (ret & volt.v_mask);
230 return sel;
233 static int da9055_regulator_set_voltage_sel(struct regulator_dev *rdev,
234 unsigned int selector)
236 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
237 struct da9055_regulator_info *info = regulator->info;
238 int ret;
241 * Regulator register set A/B is not selected through GPIO therefore
242 * we use default register set A for voltage ramping.
244 if (regulator->reg_rselect == NO_GPIO) {
245 /* Select register set A */
246 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
247 info->conf.sel_mask, DA9055_SEL_REG_A);
248 if (ret < 0)
249 return ret;
251 /* Set the voltage */
252 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
253 info->volt.v_mask, selector);
257 * Here regulator register set A/B is selected through GPIO.
258 * Therefore we first determine the selected register set A/B and
259 * then set the desired voltage for that register set A/B.
261 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
262 if (ret < 0)
263 return ret;
265 ret &= info->conf.sel_mask;
267 /* Set the voltage */
268 if (ret == DA9055_REGUALTOR_SET_A)
269 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
270 info->volt.v_mask, selector);
271 else
272 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
273 info->volt.v_mask, selector);
276 static int da9055_regulator_set_suspend_voltage(struct regulator_dev *rdev,
277 int uV)
279 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
280 struct da9055_regulator_info *info = regulator->info;
281 int ret;
283 /* Select register set B for suspend voltage ramping. */
284 if (regulator->reg_rselect == NO_GPIO) {
285 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
286 info->conf.sel_mask, DA9055_SEL_REG_B);
287 if (ret < 0)
288 return ret;
291 ret = regulator_map_voltage_linear(rdev, uV, uV);
292 if (ret < 0)
293 return ret;
295 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
296 info->volt.v_mask, ret);
299 static int da9055_suspend_enable(struct regulator_dev *rdev)
301 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
302 struct da9055_regulator_info *info = regulator->info;
304 /* Select register set B for voltage ramping. */
305 if (regulator->reg_rselect == NO_GPIO)
306 return da9055_reg_update(regulator->da9055, info->conf.reg,
307 info->conf.sel_mask, DA9055_SEL_REG_B);
308 else
309 return 0;
312 static int da9055_suspend_disable(struct regulator_dev *rdev)
314 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
315 struct da9055_regulator_info *info = regulator->info;
317 /* Diselect register set B. */
318 if (regulator->reg_rselect == NO_GPIO)
319 return da9055_reg_update(regulator->da9055, info->conf.reg,
320 info->conf.sel_mask, DA9055_SEL_REG_A);
321 else
322 return 0;
325 static struct regulator_ops da9055_buck_ops = {
326 .get_mode = da9055_buck_get_mode,
327 .set_mode = da9055_buck_set_mode,
329 .get_current_limit = da9055_buck_get_current_limit,
330 .set_current_limit = da9055_buck_set_current_limit,
332 .get_voltage_sel = da9055_regulator_get_voltage_sel,
333 .set_voltage_sel = da9055_regulator_set_voltage_sel,
334 .list_voltage = regulator_list_voltage_linear,
335 .map_voltage = regulator_map_voltage_linear,
336 .is_enabled = regulator_is_enabled_regmap,
337 .enable = regulator_enable_regmap,
338 .disable = regulator_disable_regmap,
340 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
341 .set_suspend_enable = da9055_suspend_enable,
342 .set_suspend_disable = da9055_suspend_disable,
343 .set_suspend_mode = da9055_buck_set_mode,
346 static struct regulator_ops da9055_ldo_ops = {
347 .get_mode = da9055_ldo_get_mode,
348 .set_mode = da9055_ldo_set_mode,
350 .get_voltage_sel = da9055_regulator_get_voltage_sel,
351 .set_voltage_sel = da9055_regulator_set_voltage_sel,
352 .list_voltage = regulator_list_voltage_linear,
353 .map_voltage = regulator_map_voltage_linear,
354 .is_enabled = regulator_is_enabled_regmap,
355 .enable = regulator_enable_regmap,
356 .disable = regulator_disable_regmap,
358 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
359 .set_suspend_enable = da9055_suspend_enable,
360 .set_suspend_disable = da9055_suspend_disable,
361 .set_suspend_mode = da9055_ldo_set_mode,
365 #define DA9055_LDO(_id, step, min, max, vbits, voffset) \
367 .reg_desc = {\
368 .name = #_id,\
369 .ops = &da9055_ldo_ops,\
370 .type = REGULATOR_VOLTAGE,\
371 .id = DA9055_ID_##_id,\
372 .n_voltages = (max - min) / step + 1 + (voffset), \
373 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
374 .enable_mask = 1, \
375 .min_uV = (min) * 1000,\
376 .uV_step = (step) * 1000,\
377 .linear_min_sel = (voffset),\
378 .owner = THIS_MODULE,\
380 .conf = {\
381 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
382 .sel_mask = (1 << 4),\
383 .en_mask = 1,\
385 .volt = {\
386 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
387 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
388 .sl_shift = 7,\
389 .v_mask = (1 << (vbits)) - 1,\
393 #define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \
395 .reg_desc = {\
396 .name = #_id,\
397 .ops = &da9055_buck_ops,\
398 .type = REGULATOR_VOLTAGE,\
399 .id = DA9055_ID_##_id,\
400 .n_voltages = (max - min) / step + 1 + (voffset), \
401 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
402 .enable_mask = 1,\
403 .min_uV = (min) * 1000,\
404 .uV_step = (step) * 1000,\
405 .linear_min_sel = (voffset),\
406 .owner = THIS_MODULE,\
408 .conf = {\
409 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
410 .sel_mask = (1 << 4),\
411 .en_mask = 1,\
413 .volt = {\
414 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
415 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
416 .sl_shift = 7,\
417 .v_mask = (1 << (vbits)) - 1,\
419 .mode = {\
420 .reg = DA9055_REG_BCORE_MODE,\
421 .mask = (mbits),\
422 .shift = (sbits),\
426 static struct da9055_regulator_info da9055_regulator_info[] = {
427 DA9055_BUCK(BUCK1, 25, 725, 2075, 6, 9, 0xc, 2),
428 DA9055_BUCK(BUCK2, 25, 925, 2500, 6, 0, 3, 0),
429 DA9055_LDO(LDO1, 50, 900, 3300, 6, 2),
430 DA9055_LDO(LDO2, 50, 900, 3300, 6, 3),
431 DA9055_LDO(LDO3, 50, 900, 3300, 6, 2),
432 DA9055_LDO(LDO4, 50, 900, 3300, 6, 2),
433 DA9055_LDO(LDO5, 50, 900, 2750, 6, 2),
434 DA9055_LDO(LDO6, 20, 900, 3300, 7, 0),
438 * Configures regulator to be controlled either through GPIO 1 or 2.
439 * GPIO can control regulator state and/or select the regulator register
440 * set A/B for voltage ramping.
442 static int da9055_gpio_init(struct da9055_regulator *regulator,
443 struct regulator_config *config,
444 struct da9055_pdata *pdata, int id)
446 struct da9055_regulator_info *info = regulator->info;
447 int ret = 0;
449 if (pdata->gpio_ren && pdata->gpio_ren[id]) {
450 char name[18];
451 int gpio_mux = pdata->gpio_ren[id];
453 config->ena_gpio = pdata->ena_gpio[id];
454 config->ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
455 config->ena_gpio_invert = 1;
458 * GPI pin is muxed with regulator to control the
459 * regulator state.
461 sprintf(name, "DA9055 GPI %d", gpio_mux);
462 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
463 name);
464 if (ret < 0)
465 goto err;
468 * Let the regulator know that its state is controlled
469 * through GPI.
471 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
472 DA9055_E_GPI_MASK,
473 pdata->reg_ren[id]
474 << DA9055_E_GPI_SHIFT);
475 if (ret < 0)
476 goto err;
479 if (pdata->gpio_rsel && pdata->gpio_rsel[id]) {
480 char name[18];
481 int gpio_mux = pdata->gpio_rsel[id];
483 regulator->reg_rselect = pdata->reg_rsel[id];
486 * GPI pin is muxed with regulator to select the
487 * regulator register set A/B for voltage ramping.
489 sprintf(name, "DA9055 GPI %d", gpio_mux);
490 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
491 name);
492 if (ret < 0)
493 goto err;
496 * Let the regulator know that its register set A/B
497 * will be selected through GPI for voltage ramping.
499 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
500 DA9055_V_GPI_MASK,
501 pdata->reg_rsel[id]
502 << DA9055_V_GPI_SHIFT);
505 err:
506 return ret;
509 static irqreturn_t da9055_ldo5_6_oc_irq(int irq, void *data)
511 struct da9055_regulator *regulator = data;
513 regulator_notifier_call_chain(regulator->rdev,
514 REGULATOR_EVENT_OVER_CURRENT, NULL);
516 return IRQ_HANDLED;
519 static inline struct da9055_regulator_info *find_regulator_info(int id)
521 struct da9055_regulator_info *info;
522 int i;
524 for (i = 0; i < ARRAY_SIZE(da9055_regulator_info); i++) {
525 info = &da9055_regulator_info[i];
526 if (info->reg_desc.id == id)
527 return info;
530 return NULL;
533 static int da9055_regulator_probe(struct platform_device *pdev)
535 struct regulator_config config = { };
536 struct da9055_regulator *regulator;
537 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
538 struct da9055_pdata *pdata = dev_get_platdata(da9055->dev);
539 int ret, irq;
541 if (pdata == NULL || pdata->regulators[pdev->id] == NULL)
542 return -ENODEV;
544 regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9055_regulator),
545 GFP_KERNEL);
546 if (!regulator)
547 return -ENOMEM;
549 regulator->info = find_regulator_info(pdev->id);
550 if (regulator->info == NULL) {
551 dev_err(&pdev->dev, "invalid regulator ID specified\n");
552 return -EINVAL;
555 regulator->da9055 = da9055;
556 config.dev = &pdev->dev;
557 config.driver_data = regulator;
558 config.regmap = da9055->regmap;
560 if (pdata && pdata->regulators)
561 config.init_data = pdata->regulators[pdev->id];
563 ret = da9055_gpio_init(regulator, &config, pdata, pdev->id);
564 if (ret < 0)
565 return ret;
567 regulator->rdev = devm_regulator_register(&pdev->dev,
568 &regulator->info->reg_desc,
569 &config);
570 if (IS_ERR(regulator->rdev)) {
571 dev_err(&pdev->dev, "Failed to register regulator %s\n",
572 regulator->info->reg_desc.name);
573 return PTR_ERR(regulator->rdev);
576 /* Only LDO 5 and 6 has got the over current interrupt */
577 if (pdev->id == DA9055_ID_LDO5 || pdev->id == DA9055_ID_LDO6) {
578 irq = platform_get_irq_byname(pdev, "REGULATOR");
579 if (irq < 0)
580 return irq;
582 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
583 da9055_ldo5_6_oc_irq,
584 IRQF_TRIGGER_HIGH |
585 IRQF_ONESHOT |
586 IRQF_PROBE_SHARED,
587 pdev->name, regulator);
588 if (ret != 0) {
589 if (ret != -EBUSY) {
590 dev_err(&pdev->dev,
591 "Failed to request Regulator IRQ %d: %d\n",
592 irq, ret);
593 return ret;
598 platform_set_drvdata(pdev, regulator);
600 return 0;
603 static struct platform_driver da9055_regulator_driver = {
604 .probe = da9055_regulator_probe,
605 .driver = {
606 .name = "da9055-regulator",
607 .owner = THIS_MODULE,
611 static int __init da9055_regulator_init(void)
613 return platform_driver_register(&da9055_regulator_driver);
615 subsys_initcall(da9055_regulator_init);
617 static void __exit da9055_regulator_exit(void)
619 platform_driver_unregister(&da9055_regulator_driver);
621 module_exit(da9055_regulator_exit);
623 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
624 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC");
625 MODULE_LICENSE("GPL");
626 MODULE_ALIAS("platform:da9055-regulator");