1
2
3
4
5
6
7
8
9
10
11
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/gpio/consumer.h>
20#include <linux/platform_device.h>
21#include <linux/regulator/driver.h>
22#include <linux/regulator/machine.h>
23#include <linux/of.h>
24#include <linux/regulator/of_regulator.h>
25
26#include <linux/mfd/da9055/core.h>
27#include <linux/mfd/da9055/reg.h>
28#include <linux/mfd/da9055/pdata.h>
29
30#define DA9055_MIN_UA 0
31#define DA9055_MAX_UA 3
32
33#define DA9055_LDO_MODE_SYNC 0
34#define DA9055_LDO_MODE_SLEEP 1
35
36#define DA9055_BUCK_MODE_SLEEP 1
37#define DA9055_BUCK_MODE_SYNC 2
38#define DA9055_BUCK_MODE_AUTO 3
39
40
41#define DA9055_ID_BUCK1 0
42#define DA9055_ID_BUCK2 1
43#define DA9055_ID_LDO1 2
44#define DA9055_ID_LDO2 3
45#define DA9055_ID_LDO3 4
46#define DA9055_ID_LDO4 5
47#define DA9055_ID_LDO5 6
48#define DA9055_ID_LDO6 7
49
50
51static const int da9055_current_limits[] = { 500000, 600000, 700000, 800000 };
52
53struct da9055_conf_reg {
54 int reg;
55 int sel_mask;
56 int en_mask;
57};
58
59struct da9055_volt_reg {
60 int reg_a;
61 int reg_b;
62 int sl_shift;
63 int v_mask;
64};
65
66struct da9055_mode_reg {
67 int reg;
68 int mask;
69 int shift;
70};
71
72struct da9055_regulator_info {
73 struct regulator_desc reg_desc;
74 struct da9055_conf_reg conf;
75 struct da9055_volt_reg volt;
76 struct da9055_mode_reg mode;
77};
78
79struct da9055_regulator {
80 struct da9055 *da9055;
81 struct da9055_regulator_info *info;
82 struct regulator_dev *rdev;
83 enum gpio_select reg_rselect;
84};
85
86static unsigned int da9055_buck_get_mode(struct regulator_dev *rdev)
87{
88 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
89 struct da9055_regulator_info *info = regulator->info;
90 int ret, mode = 0;
91
92 ret = da9055_reg_read(regulator->da9055, info->mode.reg);
93 if (ret < 0)
94 return ret;
95
96 switch ((ret & info->mode.mask) >> info->mode.shift) {
97 case DA9055_BUCK_MODE_SYNC:
98 mode = REGULATOR_MODE_FAST;
99 break;
100 case DA9055_BUCK_MODE_AUTO:
101 mode = REGULATOR_MODE_NORMAL;
102 break;
103 case DA9055_BUCK_MODE_SLEEP:
104 mode = REGULATOR_MODE_STANDBY;
105 break;
106 }
107
108 return mode;
109}
110
111static int da9055_buck_set_mode(struct regulator_dev *rdev,
112 unsigned int mode)
113{
114 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
115 struct da9055_regulator_info *info = regulator->info;
116 int val = 0;
117
118 switch (mode) {
119 case REGULATOR_MODE_FAST:
120 val = DA9055_BUCK_MODE_SYNC << info->mode.shift;
121 break;
122 case REGULATOR_MODE_NORMAL:
123 val = DA9055_BUCK_MODE_AUTO << info->mode.shift;
124 break;
125 case REGULATOR_MODE_STANDBY:
126 val = DA9055_BUCK_MODE_SLEEP << info->mode.shift;
127 break;
128 }
129
130 return da9055_reg_update(regulator->da9055, info->mode.reg,
131 info->mode.mask, val);
132}
133
134static unsigned int da9055_ldo_get_mode(struct regulator_dev *rdev)
135{
136 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
137 struct da9055_regulator_info *info = regulator->info;
138 int ret;
139
140 ret = da9055_reg_read(regulator->da9055, info->volt.reg_b);
141 if (ret < 0)
142 return ret;
143
144 if (ret >> info->volt.sl_shift)
145 return REGULATOR_MODE_STANDBY;
146 else
147 return REGULATOR_MODE_NORMAL;
148}
149
150static int da9055_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
151{
152 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
153 struct da9055_regulator_info *info = regulator->info;
154 struct da9055_volt_reg volt = info->volt;
155 int val = 0;
156
157 switch (mode) {
158 case REGULATOR_MODE_NORMAL:
159 case REGULATOR_MODE_FAST:
160 val = DA9055_LDO_MODE_SYNC;
161 break;
162 case REGULATOR_MODE_STANDBY:
163 val = DA9055_LDO_MODE_SLEEP;
164 break;
165 }
166
167 return da9055_reg_update(regulator->da9055, volt.reg_b,
168 1 << volt.sl_shift,
169 val << volt.sl_shift);
170}
171
172static int da9055_buck_get_current_limit(struct regulator_dev *rdev)
173{
174 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
175 struct da9055_regulator_info *info = regulator->info;
176 int ret;
177
178 ret = da9055_reg_read(regulator->da9055, DA9055_REG_BUCK_LIM);
179 if (ret < 0)
180 return ret;
181
182 ret &= info->mode.mask;
183 return da9055_current_limits[ret >> info->mode.shift];
184}
185
186static int da9055_buck_set_current_limit(struct regulator_dev *rdev, int min_uA,
187 int max_uA)
188{
189 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
190 struct da9055_regulator_info *info = regulator->info;
191 int i;
192
193 for (i = ARRAY_SIZE(da9055_current_limits) - 1; i >= 0; i--) {
194 if ((min_uA <= da9055_current_limits[i]) &&
195 (da9055_current_limits[i] <= max_uA))
196 return da9055_reg_update(regulator->da9055,
197 DA9055_REG_BUCK_LIM,
198 info->mode.mask,
199 i << info->mode.shift);
200 }
201
202 return -EINVAL;
203}
204
205static int da9055_regulator_get_voltage_sel(struct regulator_dev *rdev)
206{
207 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
208 struct da9055_regulator_info *info = regulator->info;
209 struct da9055_volt_reg volt = info->volt;
210 int ret, sel;
211
212
213
214
215
216
217 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
218 if (ret < 0)
219 return ret;
220
221 ret &= info->conf.sel_mask;
222
223
224 if (ret == DA9055_REGUALTOR_SET_A)
225 ret = da9055_reg_read(regulator->da9055, volt.reg_a);
226 else
227 ret = da9055_reg_read(regulator->da9055, volt.reg_b);
228
229 if (ret < 0)
230 return ret;
231
232 sel = (ret & volt.v_mask);
233 return sel;
234}
235
236static int da9055_regulator_set_voltage_sel(struct regulator_dev *rdev,
237 unsigned int selector)
238{
239 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
240 struct da9055_regulator_info *info = regulator->info;
241 int ret;
242
243
244
245
246
247 if (regulator->reg_rselect == NO_GPIO) {
248
249 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
250 info->conf.sel_mask, DA9055_SEL_REG_A);
251 if (ret < 0)
252 return ret;
253
254
255 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
256 info->volt.v_mask, selector);
257 }
258
259
260
261
262
263
264 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
265 if (ret < 0)
266 return ret;
267
268 ret &= info->conf.sel_mask;
269
270
271 if (ret == DA9055_REGUALTOR_SET_A)
272 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
273 info->volt.v_mask, selector);
274 else
275 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
276 info->volt.v_mask, selector);
277}
278
279static int da9055_regulator_set_suspend_voltage(struct regulator_dev *rdev,
280 int uV)
281{
282 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
283 struct da9055_regulator_info *info = regulator->info;
284 int ret;
285
286
287 if (regulator->reg_rselect == NO_GPIO) {
288 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
289 info->conf.sel_mask, DA9055_SEL_REG_B);
290 if (ret < 0)
291 return ret;
292 }
293
294 ret = regulator_map_voltage_linear(rdev, uV, uV);
295 if (ret < 0)
296 return ret;
297
298 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
299 info->volt.v_mask, ret);
300}
301
302static int da9055_suspend_enable(struct regulator_dev *rdev)
303{
304 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
305 struct da9055_regulator_info *info = regulator->info;
306
307
308 if (regulator->reg_rselect == NO_GPIO)
309 return da9055_reg_update(regulator->da9055, info->conf.reg,
310 info->conf.sel_mask, DA9055_SEL_REG_B);
311 else
312 return 0;
313}
314
315static int da9055_suspend_disable(struct regulator_dev *rdev)
316{
317 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
318 struct da9055_regulator_info *info = regulator->info;
319
320
321 if (regulator->reg_rselect == NO_GPIO)
322 return da9055_reg_update(regulator->da9055, info->conf.reg,
323 info->conf.sel_mask, DA9055_SEL_REG_A);
324 else
325 return 0;
326}
327
328static const struct regulator_ops da9055_buck_ops = {
329 .get_mode = da9055_buck_get_mode,
330 .set_mode = da9055_buck_set_mode,
331
332 .get_current_limit = da9055_buck_get_current_limit,
333 .set_current_limit = da9055_buck_set_current_limit,
334
335 .get_voltage_sel = da9055_regulator_get_voltage_sel,
336 .set_voltage_sel = da9055_regulator_set_voltage_sel,
337 .list_voltage = regulator_list_voltage_linear,
338 .map_voltage = regulator_map_voltage_linear,
339 .is_enabled = regulator_is_enabled_regmap,
340 .enable = regulator_enable_regmap,
341 .disable = regulator_disable_regmap,
342
343 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
344 .set_suspend_enable = da9055_suspend_enable,
345 .set_suspend_disable = da9055_suspend_disable,
346 .set_suspend_mode = da9055_buck_set_mode,
347};
348
349static const struct regulator_ops da9055_ldo_ops = {
350 .get_mode = da9055_ldo_get_mode,
351 .set_mode = da9055_ldo_set_mode,
352
353 .get_voltage_sel = da9055_regulator_get_voltage_sel,
354 .set_voltage_sel = da9055_regulator_set_voltage_sel,
355 .list_voltage = regulator_list_voltage_linear,
356 .map_voltage = regulator_map_voltage_linear,
357 .is_enabled = regulator_is_enabled_regmap,
358 .enable = regulator_enable_regmap,
359 .disable = regulator_disable_regmap,
360
361 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
362 .set_suspend_enable = da9055_suspend_enable,
363 .set_suspend_disable = da9055_suspend_disable,
364 .set_suspend_mode = da9055_ldo_set_mode,
365
366};
367
368#define DA9055_LDO(_id, step, min, max, vbits, voffset) \
369{\
370 .reg_desc = {\
371 .name = #_id,\
372 .ops = &da9055_ldo_ops,\
373 .type = REGULATOR_VOLTAGE,\
374 .id = DA9055_ID_##_id,\
375 .n_voltages = (max - min) / step + 1 + (voffset), \
376 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
377 .enable_mask = 1, \
378 .min_uV = (min) * 1000,\
379 .uV_step = (step) * 1000,\
380 .linear_min_sel = (voffset),\
381 .owner = THIS_MODULE,\
382 },\
383 .conf = {\
384 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
385 .sel_mask = (1 << 4),\
386 .en_mask = 1,\
387 },\
388 .volt = {\
389 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
390 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
391 .sl_shift = 7,\
392 .v_mask = (1 << (vbits)) - 1,\
393 },\
394}
395
396#define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \
397{\
398 .reg_desc = {\
399 .name = #_id,\
400 .ops = &da9055_buck_ops,\
401 .type = REGULATOR_VOLTAGE,\
402 .id = DA9055_ID_##_id,\
403 .n_voltages = (max - min) / step + 1 + (voffset), \
404 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
405 .enable_mask = 1,\
406 .min_uV = (min) * 1000,\
407 .uV_step = (step) * 1000,\
408 .linear_min_sel = (voffset),\
409 .owner = THIS_MODULE,\
410 },\
411 .conf = {\
412 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
413 .sel_mask = (1 << 4),\
414 .en_mask = 1,\
415 },\
416 .volt = {\
417 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
418 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
419 .sl_shift = 7,\
420 .v_mask = (1 << (vbits)) - 1,\
421 },\
422 .mode = {\
423 .reg = DA9055_REG_BCORE_MODE,\
424 .mask = (mbits),\
425 .shift = (sbits),\
426 },\
427}
428
429static struct da9055_regulator_info da9055_regulator_info[] = {
430 DA9055_BUCK(BUCK1, 25, 725, 2075, 6, 9, 0xc, 2),
431 DA9055_BUCK(BUCK2, 25, 925, 2500, 6, 0, 3, 0),
432 DA9055_LDO(LDO1, 50, 900, 3300, 6, 2),
433 DA9055_LDO(LDO2, 50, 900, 3300, 6, 3),
434 DA9055_LDO(LDO3, 50, 900, 3300, 6, 2),
435 DA9055_LDO(LDO4, 50, 900, 3300, 6, 2),
436 DA9055_LDO(LDO5, 50, 900, 2750, 6, 2),
437 DA9055_LDO(LDO6, 20, 900, 3300, 7, 0),
438};
439
440
441
442
443
444
445static int da9055_gpio_init(struct da9055_regulator *regulator,
446 struct regulator_config *config,
447 struct da9055_pdata *pdata, int id)
448{
449 struct da9055_regulator_info *info = regulator->info;
450 int ret = 0;
451
452 if (!pdata)
453 return 0;
454
455 if (pdata->gpio_ren && pdata->gpio_ren[id]) {
456 char name[18];
457 int gpio_mux = pdata->gpio_ren[id];
458
459 config->ena_gpiod = pdata->ena_gpiods[id];
460 config->ena_gpio_invert = 1;
461
462
463
464
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
474
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
484 if (pdata->gpio_rsel && pdata->gpio_rsel[id]) {
485 char name[18];
486 int gpio_mux = pdata->gpio_rsel[id];
487
488 regulator->reg_rselect = pdata->reg_rsel[id];
489
490
491
492
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
502
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
538#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
562 np = of_get_child_by_name(nproot, "regulators");
563 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);
570 return ret;
571 }
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
582static inline int da9055_regulator_dt_init(struct platform_device *pdev,
583 struct da9055_regulator *regulator,
584 struct regulator_config *config,
585 int regid)
586{
587 return -ENODEV;
588}
589#endif
590
591static int da9055_regulator_probe(struct platform_device *pdev)
592{
593 struct regulator_config config = { };
594 struct da9055_regulator *regulator;
595 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
596 struct da9055_pdata *pdata = dev_get_platdata(da9055->dev);
597 int ret, irq;
598
599 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
615 if (pdata) {
616 config.init_data = pdata->regulators[pdev->id];
617 } else {
618 ret = da9055_regulator_dt_init(pdev, regulator, &config,
619 pdev->id);
620 if (ret < 0)
621 return ret;
622 }
623
624 ret = da9055_gpio_init(regulator, &config, pdata, pdev->id);
625 if (ret < 0)
626 return ret;
627
628 regulator->rdev = devm_regulator_register(&pdev->dev,
629 ®ulator->info->reg_desc,
630 &config);
631 if (IS_ERR(regulator->rdev)) {
632 dev_err(&pdev->dev, "Failed to register regulator %s\n",
633 regulator->info->reg_desc.name);
634 return PTR_ERR(regulator->rdev);
635 }
636
637
638 if (pdev->id == DA9055_ID_LDO5 || pdev->id == DA9055_ID_LDO6) {
639 irq = platform_get_irq_byname(pdev, "REGULATOR");
640 if (irq < 0)
641 return irq;
642
643 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);
654 return ret;
655 }
656 }
657 }
658
659 platform_set_drvdata(pdev, regulator);
660
661 return 0;
662}
663
664static struct platform_driver da9055_regulator_driver = {
665 .probe = da9055_regulator_probe,
666 .driver = {
667 .name = "da9055-regulator",
668 },
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");
687