dma-fence: Add some more fence-merge-unwrap tests
[drm/drm-misc.git] / drivers / regulator / da9055-regulator.c
bloba0d3414aa79e6b267b75943bd3f14471ef5fe971
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Regulator driver for DA9055 PMIC
4 //
5 // Copyright(c) 2012 Dialog Semiconductor Ltd.
6 //
7 // Author: David Dajun Chen <dchen@diasemi.com>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/platform_device.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/machine.h>
16 #include <linux/of.h>
17 #include <linux/regulator/of_regulator.h>
19 #include <linux/mfd/da9055/core.h>
20 #include <linux/mfd/da9055/reg.h>
21 #include <linux/mfd/da9055/pdata.h>
23 #define DA9055_MIN_UA 0
24 #define DA9055_MAX_UA 3
26 #define DA9055_LDO_MODE_SYNC 0
27 #define DA9055_LDO_MODE_SLEEP 1
29 #define DA9055_BUCK_MODE_SLEEP 1
30 #define DA9055_BUCK_MODE_SYNC 2
31 #define DA9055_BUCK_MODE_AUTO 3
33 /* DA9055 REGULATOR IDs */
34 #define DA9055_ID_BUCK1 0
35 #define DA9055_ID_BUCK2 1
36 #define DA9055_ID_LDO1 2
37 #define DA9055_ID_LDO2 3
38 #define DA9055_ID_LDO3 4
39 #define DA9055_ID_LDO4 5
40 #define DA9055_ID_LDO5 6
41 #define DA9055_ID_LDO6 7
43 /* DA9055 BUCK current limit */
44 static const unsigned int da9055_current_limits[] = {
45 500000, 600000, 700000, 800000
48 struct da9055_conf_reg {
49 int reg;
50 int sel_mask;
51 int en_mask;
54 struct da9055_volt_reg {
55 int reg_a;
56 int reg_b;
57 int sl_shift;
58 int v_mask;
61 struct da9055_mode_reg {
62 int reg;
63 int mask;
64 int shift;
67 struct da9055_regulator_info {
68 struct regulator_desc reg_desc;
69 struct da9055_conf_reg conf;
70 struct da9055_volt_reg volt;
71 struct da9055_mode_reg mode;
74 struct da9055_regulator {
75 struct da9055 *da9055;
76 const struct da9055_regulator_info *info;
77 struct regulator_dev *rdev;
78 enum gpio_select reg_rselect;
81 static unsigned int da9055_buck_get_mode(struct regulator_dev *rdev)
83 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
84 const struct da9055_regulator_info *info = regulator->info;
85 int ret, mode = 0;
87 ret = da9055_reg_read(regulator->da9055, info->mode.reg);
88 if (ret < 0)
89 return ret;
91 switch ((ret & info->mode.mask) >> info->mode.shift) {
92 case DA9055_BUCK_MODE_SYNC:
93 mode = REGULATOR_MODE_FAST;
94 break;
95 case DA9055_BUCK_MODE_AUTO:
96 mode = REGULATOR_MODE_NORMAL;
97 break;
98 case DA9055_BUCK_MODE_SLEEP:
99 mode = REGULATOR_MODE_STANDBY;
100 break;
103 return mode;
106 static int da9055_buck_set_mode(struct regulator_dev *rdev,
107 unsigned int mode)
109 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
110 const struct da9055_regulator_info *info = regulator->info;
111 int val = 0;
113 switch (mode) {
114 case REGULATOR_MODE_FAST:
115 val = DA9055_BUCK_MODE_SYNC << info->mode.shift;
116 break;
117 case REGULATOR_MODE_NORMAL:
118 val = DA9055_BUCK_MODE_AUTO << info->mode.shift;
119 break;
120 case REGULATOR_MODE_STANDBY:
121 val = DA9055_BUCK_MODE_SLEEP << info->mode.shift;
122 break;
125 return da9055_reg_update(regulator->da9055, info->mode.reg,
126 info->mode.mask, val);
129 static unsigned int da9055_ldo_get_mode(struct regulator_dev *rdev)
131 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
132 const struct da9055_regulator_info *info = regulator->info;
133 int ret;
135 ret = da9055_reg_read(regulator->da9055, info->volt.reg_b);
136 if (ret < 0)
137 return ret;
139 if (ret >> info->volt.sl_shift)
140 return REGULATOR_MODE_STANDBY;
141 else
142 return REGULATOR_MODE_NORMAL;
145 static int da9055_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
147 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
148 const struct da9055_regulator_info *info = regulator->info;
149 struct da9055_volt_reg volt = info->volt;
150 int val = 0;
152 switch (mode) {
153 case REGULATOR_MODE_NORMAL:
154 case REGULATOR_MODE_FAST:
155 val = DA9055_LDO_MODE_SYNC;
156 break;
157 case REGULATOR_MODE_STANDBY:
158 val = DA9055_LDO_MODE_SLEEP;
159 break;
162 return da9055_reg_update(regulator->da9055, volt.reg_b,
163 1 << volt.sl_shift,
164 val << volt.sl_shift);
167 static int da9055_regulator_get_voltage_sel(struct regulator_dev *rdev)
169 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
170 const struct da9055_regulator_info *info = regulator->info;
171 struct da9055_volt_reg volt = info->volt;
172 int ret, sel;
175 * There are two voltage register set A & B for voltage ramping but
176 * either one of then can be active therefore we first determine
177 * the active register set.
179 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
180 if (ret < 0)
181 return ret;
183 ret &= info->conf.sel_mask;
185 /* Get the voltage for the active register set A/B */
186 if (ret == DA9055_REGUALTOR_SET_A)
187 ret = da9055_reg_read(regulator->da9055, volt.reg_a);
188 else
189 ret = da9055_reg_read(regulator->da9055, volt.reg_b);
191 if (ret < 0)
192 return ret;
194 sel = (ret & volt.v_mask);
195 return sel;
198 static int da9055_regulator_set_voltage_sel(struct regulator_dev *rdev,
199 unsigned int selector)
201 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
202 const struct da9055_regulator_info *info = regulator->info;
203 int ret;
206 * Regulator register set A/B is not selected through GPIO therefore
207 * we use default register set A for voltage ramping.
209 if (regulator->reg_rselect == NO_GPIO) {
210 /* Select register set A */
211 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
212 info->conf.sel_mask, DA9055_SEL_REG_A);
213 if (ret < 0)
214 return ret;
216 /* Set the voltage */
217 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
218 info->volt.v_mask, selector);
222 * Here regulator register set A/B is selected through GPIO.
223 * Therefore we first determine the selected register set A/B and
224 * then set the desired voltage for that register set A/B.
226 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
227 if (ret < 0)
228 return ret;
230 ret &= info->conf.sel_mask;
232 /* Set the voltage */
233 if (ret == DA9055_REGUALTOR_SET_A)
234 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
235 info->volt.v_mask, selector);
236 else
237 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
238 info->volt.v_mask, selector);
241 static int da9055_regulator_set_suspend_voltage(struct regulator_dev *rdev,
242 int uV)
244 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
245 const struct da9055_regulator_info *info = regulator->info;
246 int ret;
248 /* Select register set B for suspend voltage ramping. */
249 if (regulator->reg_rselect == NO_GPIO) {
250 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
251 info->conf.sel_mask, DA9055_SEL_REG_B);
252 if (ret < 0)
253 return ret;
256 ret = regulator_map_voltage_linear(rdev, uV, uV);
257 if (ret < 0)
258 return ret;
260 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
261 info->volt.v_mask, ret);
264 static int da9055_suspend_enable(struct regulator_dev *rdev)
266 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
267 const struct da9055_regulator_info *info = regulator->info;
269 /* Select register set B for voltage ramping. */
270 if (regulator->reg_rselect == NO_GPIO)
271 return da9055_reg_update(regulator->da9055, info->conf.reg,
272 info->conf.sel_mask, DA9055_SEL_REG_B);
273 else
274 return 0;
277 static int da9055_suspend_disable(struct regulator_dev *rdev)
279 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
280 const struct da9055_regulator_info *info = regulator->info;
282 /* Diselect register set B. */
283 if (regulator->reg_rselect == NO_GPIO)
284 return da9055_reg_update(regulator->da9055, info->conf.reg,
285 info->conf.sel_mask, DA9055_SEL_REG_A);
286 else
287 return 0;
290 static const struct regulator_ops da9055_buck_ops = {
291 .get_mode = da9055_buck_get_mode,
292 .set_mode = da9055_buck_set_mode,
294 .get_current_limit = regulator_get_current_limit_regmap,
295 .set_current_limit = regulator_set_current_limit_regmap,
297 .get_voltage_sel = da9055_regulator_get_voltage_sel,
298 .set_voltage_sel = da9055_regulator_set_voltage_sel,
299 .list_voltage = regulator_list_voltage_linear,
300 .map_voltage = regulator_map_voltage_linear,
301 .is_enabled = regulator_is_enabled_regmap,
302 .enable = regulator_enable_regmap,
303 .disable = regulator_disable_regmap,
305 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
306 .set_suspend_enable = da9055_suspend_enable,
307 .set_suspend_disable = da9055_suspend_disable,
308 .set_suspend_mode = da9055_buck_set_mode,
311 static const struct regulator_ops da9055_ldo_ops = {
312 .get_mode = da9055_ldo_get_mode,
313 .set_mode = da9055_ldo_set_mode,
315 .get_voltage_sel = da9055_regulator_get_voltage_sel,
316 .set_voltage_sel = da9055_regulator_set_voltage_sel,
317 .list_voltage = regulator_list_voltage_linear,
318 .map_voltage = regulator_map_voltage_linear,
319 .is_enabled = regulator_is_enabled_regmap,
320 .enable = regulator_enable_regmap,
321 .disable = regulator_disable_regmap,
323 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
324 .set_suspend_enable = da9055_suspend_enable,
325 .set_suspend_disable = da9055_suspend_disable,
326 .set_suspend_mode = da9055_ldo_set_mode,
330 #define DA9055_LDO(_id, step, min, max, vbits, voffset) \
332 .reg_desc = {\
333 .name = #_id,\
334 .of_match = of_match_ptr(#_id),\
335 .regulators_node = of_match_ptr("regulators"),\
336 .ops = &da9055_ldo_ops,\
337 .type = REGULATOR_VOLTAGE,\
338 .id = DA9055_ID_##_id,\
339 .n_voltages = (max - min) / step + 1 + (voffset), \
340 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
341 .enable_mask = 1, \
342 .min_uV = (min) * 1000,\
343 .uV_step = (step) * 1000,\
344 .linear_min_sel = (voffset),\
345 .owner = THIS_MODULE,\
347 .conf = {\
348 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
349 .sel_mask = (1 << 4),\
350 .en_mask = 1,\
352 .volt = {\
353 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
354 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
355 .sl_shift = 7,\
356 .v_mask = (1 << (vbits)) - 1,\
360 #define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \
362 .reg_desc = {\
363 .name = #_id,\
364 .of_match = of_match_ptr(#_id),\
365 .regulators_node = of_match_ptr("regulators"),\
366 .ops = &da9055_buck_ops,\
367 .type = REGULATOR_VOLTAGE,\
368 .id = DA9055_ID_##_id,\
369 .n_voltages = (max - min) / step + 1 + (voffset), \
370 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
371 .enable_mask = 1,\
372 .min_uV = (min) * 1000,\
373 .uV_step = (step) * 1000,\
374 .linear_min_sel = (voffset),\
375 .owner = THIS_MODULE,\
376 .curr_table = da9055_current_limits,\
377 .n_current_limits = ARRAY_SIZE(da9055_current_limits),\
378 .csel_reg = DA9055_REG_BUCK_LIM,\
379 .csel_mask = (mbits),\
381 .conf = {\
382 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
383 .sel_mask = (1 << 4),\
384 .en_mask = 1,\
386 .volt = {\
387 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
388 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
389 .sl_shift = 7,\
390 .v_mask = (1 << (vbits)) - 1,\
392 .mode = {\
393 .reg = DA9055_REG_BCORE_MODE,\
394 .mask = (mbits),\
395 .shift = (sbits),\
399 static const struct da9055_regulator_info da9055_regulator_info[] = {
400 DA9055_BUCK(BUCK1, 25, 725, 2075, 6, 9, 0xc, 2),
401 DA9055_BUCK(BUCK2, 25, 925, 2500, 6, 0, 3, 0),
402 DA9055_LDO(LDO1, 50, 900, 3300, 6, 2),
403 DA9055_LDO(LDO2, 50, 900, 3300, 6, 3),
404 DA9055_LDO(LDO3, 50, 900, 3300, 6, 2),
405 DA9055_LDO(LDO4, 50, 900, 3300, 6, 2),
406 DA9055_LDO(LDO5, 50, 900, 2750, 6, 2),
407 DA9055_LDO(LDO6, 20, 900, 3300, 7, 0),
411 * Configures regulator to be controlled either through GPIO 1 or 2.
412 * GPIO can control regulator state and/or select the regulator register
413 * set A/B for voltage ramping.
415 static int da9055_gpio_init(struct device *dev,
416 struct da9055_regulator *regulator,
417 struct regulator_config *config,
418 struct da9055_pdata *pdata, int id)
420 const struct da9055_regulator_info *info = regulator->info;
421 struct gpio_desc *ren;
422 struct gpio_desc *ena;
423 struct gpio_desc *rsel;
424 int ret = 0;
426 /* Look for "regulator-enable-gpios" GPIOs in the regulator node */
427 ren = devm_gpiod_get_optional(dev, "regulator-enable", GPIOD_IN);
428 if (IS_ERR(ren))
429 return PTR_ERR(ren);
431 if (ren) {
432 /* This GPIO is not optional at this point */
433 ena = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
434 if (IS_ERR(ena))
435 return PTR_ERR(ena);
437 config->ena_gpiod = ena;
440 * GPI pin is muxed with regulator to control the
441 * regulator state.
443 gpiod_set_consumer_name(ren, "DA9055 ren GPI");
446 * Let the regulator know that its state is controlled
447 * through GPI.
449 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
450 DA9055_E_GPI_MASK,
451 pdata->reg_ren[id]
452 << DA9055_E_GPI_SHIFT);
453 if (ret < 0)
454 return ret;
457 /* Look for "regulator-select-gpios" GPIOs in the regulator node */
458 rsel = devm_gpiod_get_optional(dev, "regulator-select", GPIOD_IN);
459 if (IS_ERR(rsel))
460 return PTR_ERR(rsel);
462 if (rsel) {
463 regulator->reg_rselect = pdata->reg_rsel[id];
466 * GPI pin is muxed with regulator to select the
467 * regulator register set A/B for voltage ramping.
469 gpiod_set_consumer_name(rsel, "DA9055 rsel GPI");
472 * Let the regulator know that its register set A/B
473 * will be selected through GPI for voltage ramping.
475 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
476 DA9055_V_GPI_MASK,
477 pdata->reg_rsel[id]
478 << DA9055_V_GPI_SHIFT);
481 return ret;
484 static irqreturn_t da9055_ldo5_6_oc_irq(int irq, void *data)
486 struct da9055_regulator *regulator = data;
488 regulator_notifier_call_chain(regulator->rdev,
489 REGULATOR_EVENT_OVER_CURRENT, NULL);
491 return IRQ_HANDLED;
494 static inline const struct da9055_regulator_info *find_regulator_info(int id)
496 const struct da9055_regulator_info *info;
497 int i;
499 for (i = 0; i < ARRAY_SIZE(da9055_regulator_info); i++) {
500 info = &da9055_regulator_info[i];
501 if (info->reg_desc.id == id)
502 return info;
505 return NULL;
508 static int da9055_regulator_probe(struct platform_device *pdev)
510 struct regulator_config config = { };
511 struct da9055_regulator *regulator;
512 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
513 struct da9055_pdata *pdata = dev_get_platdata(da9055->dev);
514 int ret, irq;
516 regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9055_regulator),
517 GFP_KERNEL);
518 if (!regulator)
519 return -ENOMEM;
521 regulator->info = find_regulator_info(pdev->id);
522 if (regulator->info == NULL) {
523 dev_err(&pdev->dev, "invalid regulator ID specified\n");
524 return -EINVAL;
527 regulator->da9055 = da9055;
528 config.dev = da9055->dev;
529 config.driver_data = regulator;
530 config.regmap = da9055->regmap;
532 if (pdata)
533 config.init_data = pdata->regulators[pdev->id];
535 ret = da9055_gpio_init(&pdev->dev, regulator, &config, pdata, pdev->id);
536 if (ret < 0)
537 return ret;
539 regulator->rdev = devm_regulator_register(&pdev->dev,
540 &regulator->info->reg_desc,
541 &config);
542 if (IS_ERR(regulator->rdev)) {
543 dev_err(&pdev->dev, "Failed to register regulator %s\n",
544 regulator->info->reg_desc.name);
545 return PTR_ERR(regulator->rdev);
548 /* Only LDO 5 and 6 has got the over current interrupt */
549 if (pdev->id == DA9055_ID_LDO5 || pdev->id == DA9055_ID_LDO6) {
550 irq = platform_get_irq_byname(pdev, "REGULATOR");
551 if (irq < 0)
552 return irq;
554 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
555 da9055_ldo5_6_oc_irq,
556 IRQF_TRIGGER_HIGH |
557 IRQF_ONESHOT |
558 IRQF_PROBE_SHARED,
559 pdev->name, regulator);
560 if (ret != 0) {
561 if (ret != -EBUSY) {
562 dev_err(&pdev->dev,
563 "Failed to request Regulator IRQ %d: %d\n",
564 irq, ret);
565 return ret;
570 platform_set_drvdata(pdev, regulator);
572 return 0;
575 static struct platform_driver da9055_regulator_driver = {
576 .probe = da9055_regulator_probe,
577 .driver = {
578 .name = "da9055-regulator",
579 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
583 static int __init da9055_regulator_init(void)
585 return platform_driver_register(&da9055_regulator_driver);
587 subsys_initcall(da9055_regulator_init);
589 static void __exit da9055_regulator_exit(void)
591 platform_driver_unregister(&da9055_regulator_driver);
593 module_exit(da9055_regulator_exit);
595 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
596 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC");
597 MODULE_LICENSE("GPL");
598 MODULE_ALIAS("platform:da9055-regulator");