]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/regulator/ab3100.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[mirror_ubuntu-zesty-kernel.git] / drivers / regulator / ab3100.c
1 /*
2 * drivers/regulator/ab3100.c
3 *
4 * Copyright (C) 2008-2009 ST-Ericsson AB
5 * License terms: GNU General Public License (GPL) version 2
6 * Low-level control of the AB3100 IC Low Dropout (LDO)
7 * regulators, external regulator and buck converter
8 * Author: Mattias Wallin <mattias.wallin@stericsson.com>
9 * Author: Linus Walleij <linus.walleij@stericsson.com>
10 */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/err.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/mfd/abx500.h>
20 #include <linux/mfd/core.h>
21
22 /* LDO registers and some handy masking definitions for AB3100 */
23 #define AB3100_LDO_A 0x40
24 #define AB3100_LDO_C 0x41
25 #define AB3100_LDO_D 0x42
26 #define AB3100_LDO_E 0x43
27 #define AB3100_LDO_E_SLEEP 0x44
28 #define AB3100_LDO_F 0x45
29 #define AB3100_LDO_G 0x46
30 #define AB3100_LDO_H 0x47
31 #define AB3100_LDO_H_SLEEP_MODE 0
32 #define AB3100_LDO_H_SLEEP_EN 2
33 #define AB3100_LDO_ON 4
34 #define AB3100_LDO_H_VSEL_AC 5
35 #define AB3100_LDO_K 0x48
36 #define AB3100_LDO_EXT 0x49
37 #define AB3100_BUCK 0x4A
38 #define AB3100_BUCK_SLEEP 0x4B
39 #define AB3100_REG_ON_MASK 0x10
40
41 /**
42 * struct ab3100_regulator
43 * A struct passed around the individual regulator functions
44 * @platform_device: platform device holding this regulator
45 * @dev: handle to the device
46 * @plfdata: AB3100 platform data passed in at probe time
47 * @regreg: regulator register number in the AB3100
48 * @fixed_voltage: a fixed voltage for this regulator, if this
49 * 0 the voltages array is used instead.
50 * @typ_voltages: an array of available typical voltages for
51 * this regulator
52 * @voltages_len: length of the array of available voltages
53 */
54 struct ab3100_regulator {
55 struct regulator_dev *rdev;
56 struct device *dev;
57 struct ab3100_platform_data *plfdata;
58 u8 regreg;
59 int fixed_voltage;
60 int const *typ_voltages;
61 u8 voltages_len;
62 };
63
64 /* The order in which registers are initialized */
65 static const u8 ab3100_reg_init_order[AB3100_NUM_REGULATORS+2] = {
66 AB3100_LDO_A,
67 AB3100_LDO_C,
68 AB3100_LDO_E,
69 AB3100_LDO_E_SLEEP,
70 AB3100_LDO_F,
71 AB3100_LDO_G,
72 AB3100_LDO_H,
73 AB3100_LDO_K,
74 AB3100_LDO_EXT,
75 AB3100_BUCK,
76 AB3100_BUCK_SLEEP,
77 AB3100_LDO_D,
78 };
79
80 /* Preset (hardware defined) voltages for these regulators */
81 #define LDO_A_VOLTAGE 2750000
82 #define LDO_C_VOLTAGE 2650000
83 #define LDO_D_VOLTAGE 2650000
84
85 static const int ldo_e_buck_typ_voltages[] = {
86 1800000,
87 1400000,
88 1300000,
89 1200000,
90 1100000,
91 1050000,
92 900000,
93 };
94
95 static const int ldo_f_typ_voltages[] = {
96 1800000,
97 1400000,
98 1300000,
99 1200000,
100 1100000,
101 1050000,
102 2500000,
103 2650000,
104 };
105
106 static const int ldo_g_typ_voltages[] = {
107 2850000,
108 2750000,
109 1800000,
110 1500000,
111 };
112
113 static const int ldo_h_typ_voltages[] = {
114 2750000,
115 1800000,
116 1500000,
117 1200000,
118 };
119
120 static const int ldo_k_typ_voltages[] = {
121 2750000,
122 1800000,
123 };
124
125
126 /* The regulator devices */
127 static struct ab3100_regulator
128 ab3100_regulators[AB3100_NUM_REGULATORS] = {
129 {
130 .regreg = AB3100_LDO_A,
131 .fixed_voltage = LDO_A_VOLTAGE,
132 },
133 {
134 .regreg = AB3100_LDO_C,
135 .fixed_voltage = LDO_C_VOLTAGE,
136 },
137 {
138 .regreg = AB3100_LDO_D,
139 .fixed_voltage = LDO_D_VOLTAGE,
140 },
141 {
142 .regreg = AB3100_LDO_E,
143 .typ_voltages = ldo_e_buck_typ_voltages,
144 .voltages_len = ARRAY_SIZE(ldo_e_buck_typ_voltages),
145 },
146 {
147 .regreg = AB3100_LDO_F,
148 .typ_voltages = ldo_f_typ_voltages,
149 .voltages_len = ARRAY_SIZE(ldo_f_typ_voltages),
150 },
151 {
152 .regreg = AB3100_LDO_G,
153 .typ_voltages = ldo_g_typ_voltages,
154 .voltages_len = ARRAY_SIZE(ldo_g_typ_voltages),
155 },
156 {
157 .regreg = AB3100_LDO_H,
158 .typ_voltages = ldo_h_typ_voltages,
159 .voltages_len = ARRAY_SIZE(ldo_h_typ_voltages),
160 },
161 {
162 .regreg = AB3100_LDO_K,
163 .typ_voltages = ldo_k_typ_voltages,
164 .voltages_len = ARRAY_SIZE(ldo_k_typ_voltages),
165 },
166 {
167 .regreg = AB3100_LDO_EXT,
168 /* No voltages for the external regulator */
169 },
170 {
171 .regreg = AB3100_BUCK,
172 .typ_voltages = ldo_e_buck_typ_voltages,
173 .voltages_len = ARRAY_SIZE(ldo_e_buck_typ_voltages),
174 },
175 };
176
177 /*
178 * General functions for enable, disable and is_enabled used for
179 * LDO: A,C,E,F,G,H,K,EXT and BUCK
180 */
181 static int ab3100_enable_regulator(struct regulator_dev *reg)
182 {
183 struct ab3100_regulator *abreg = reg->reg_data;
184 int err;
185 u8 regval;
186
187 err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg,
188 &regval);
189 if (err) {
190 dev_warn(&reg->dev, "failed to get regid %d value\n",
191 abreg->regreg);
192 return err;
193 }
194
195 /* The regulator is already on, no reason to go further */
196 if (regval & AB3100_REG_ON_MASK)
197 return 0;
198
199 regval |= AB3100_REG_ON_MASK;
200
201 err = abx500_set_register_interruptible(abreg->dev, 0, abreg->regreg,
202 regval);
203 if (err) {
204 dev_warn(&reg->dev, "failed to set regid %d value\n",
205 abreg->regreg);
206 return err;
207 }
208
209 /* Per-regulator power on delay from spec */
210 switch (abreg->regreg) {
211 case AB3100_LDO_A: /* Fallthrough */
212 case AB3100_LDO_C: /* Fallthrough */
213 case AB3100_LDO_D: /* Fallthrough */
214 case AB3100_LDO_E: /* Fallthrough */
215 case AB3100_LDO_H: /* Fallthrough */
216 case AB3100_LDO_K:
217 udelay(200);
218 break;
219 case AB3100_LDO_F:
220 udelay(600);
221 break;
222 case AB3100_LDO_G:
223 udelay(400);
224 break;
225 case AB3100_BUCK:
226 mdelay(1);
227 break;
228 default:
229 break;
230 }
231
232 return 0;
233 }
234
235 static int ab3100_disable_regulator(struct regulator_dev *reg)
236 {
237 struct ab3100_regulator *abreg = reg->reg_data;
238 int err;
239 u8 regval;
240
241 /*
242 * LDO D is a special regulator. When it is disabled, the entire
243 * system is shut down. So this is handled specially.
244 */
245 pr_info("Called ab3100_disable_regulator\n");
246 if (abreg->regreg == AB3100_LDO_D) {
247 dev_info(&reg->dev, "disabling LDO D - shut down system\n");
248 /* Setting LDO D to 0x00 cuts the power to the SoC */
249 return abx500_set_register_interruptible(abreg->dev, 0,
250 AB3100_LDO_D, 0x00U);
251 }
252
253 /*
254 * All other regulators are handled here
255 */
256 err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg,
257 &regval);
258 if (err) {
259 dev_err(&reg->dev, "unable to get register 0x%x\n",
260 abreg->regreg);
261 return err;
262 }
263 regval &= ~AB3100_REG_ON_MASK;
264 return abx500_set_register_interruptible(abreg->dev, 0, abreg->regreg,
265 regval);
266 }
267
268 static int ab3100_is_enabled_regulator(struct regulator_dev *reg)
269 {
270 struct ab3100_regulator *abreg = reg->reg_data;
271 u8 regval;
272 int err;
273
274 err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg,
275 &regval);
276 if (err) {
277 dev_err(&reg->dev, "unable to get register 0x%x\n",
278 abreg->regreg);
279 return err;
280 }
281
282 return regval & AB3100_REG_ON_MASK;
283 }
284
285 static int ab3100_list_voltage_regulator(struct regulator_dev *reg,
286 unsigned selector)
287 {
288 struct ab3100_regulator *abreg = reg->reg_data;
289
290 if (selector >= abreg->voltages_len)
291 return -EINVAL;
292 return abreg->typ_voltages[selector];
293 }
294
295 static int ab3100_get_voltage_regulator(struct regulator_dev *reg)
296 {
297 struct ab3100_regulator *abreg = reg->reg_data;
298 u8 regval;
299 int err;
300
301 /* Return the voltage for fixed regulators immediately */
302 if (abreg->fixed_voltage)
303 return abreg->fixed_voltage;
304
305 /*
306 * For variable types, read out setting and index into
307 * supplied voltage list.
308 */
309 err = abx500_get_register_interruptible(abreg->dev, 0,
310 abreg->regreg, &regval);
311 if (err) {
312 dev_warn(&reg->dev,
313 "failed to get regulator value in register %02x\n",
314 abreg->regreg);
315 return err;
316 }
317
318 /* The 3 highest bits index voltages */
319 regval &= 0xE0;
320 regval >>= 5;
321
322 if (regval >= abreg->voltages_len) {
323 dev_err(&reg->dev,
324 "regulator register %02x contains an illegal voltage setting\n",
325 abreg->regreg);
326 return -EINVAL;
327 }
328
329 return abreg->typ_voltages[regval];
330 }
331
332 static int ab3100_get_best_voltage_index(struct regulator_dev *reg,
333 int min_uV, int max_uV)
334 {
335 struct ab3100_regulator *abreg = reg->reg_data;
336 int i;
337 int bestmatch;
338 int bestindex;
339
340 /*
341 * Locate the minimum voltage fitting the criteria on
342 * this regulator. The switchable voltages are not
343 * in strict falling order so we need to check them
344 * all for the best match.
345 */
346 bestmatch = INT_MAX;
347 bestindex = -1;
348 for (i = 0; i < abreg->voltages_len; i++) {
349 if (abreg->typ_voltages[i] <= max_uV &&
350 abreg->typ_voltages[i] >= min_uV &&
351 abreg->typ_voltages[i] < bestmatch) {
352 bestmatch = abreg->typ_voltages[i];
353 bestindex = i;
354 }
355 }
356
357 if (bestindex < 0) {
358 dev_warn(&reg->dev, "requested %d<=x<=%d uV, out of range!\n",
359 min_uV, max_uV);
360 return -EINVAL;
361 }
362 return bestindex;
363 }
364
365 static int ab3100_set_voltage_regulator(struct regulator_dev *reg,
366 int min_uV, int max_uV,
367 unsigned *selector)
368 {
369 struct ab3100_regulator *abreg = reg->reg_data;
370 u8 regval;
371 int err;
372 int bestindex;
373
374 bestindex = ab3100_get_best_voltage_index(reg, min_uV, max_uV);
375 if (bestindex < 0)
376 return bestindex;
377
378 *selector = bestindex;
379
380 err = abx500_get_register_interruptible(abreg->dev, 0,
381 abreg->regreg, &regval);
382 if (err) {
383 dev_warn(&reg->dev,
384 "failed to get regulator register %02x\n",
385 abreg->regreg);
386 return err;
387 }
388
389 /* The highest three bits control the variable regulators */
390 regval &= ~0xE0;
391 regval |= (bestindex << 5);
392
393 err = abx500_set_register_interruptible(abreg->dev, 0,
394 abreg->regreg, regval);
395 if (err)
396 dev_warn(&reg->dev, "failed to set regulator register %02x\n",
397 abreg->regreg);
398
399 return err;
400 }
401
402 static int ab3100_set_suspend_voltage_regulator(struct regulator_dev *reg,
403 int uV)
404 {
405 struct ab3100_regulator *abreg = reg->reg_data;
406 u8 regval;
407 int err;
408 int bestindex;
409 u8 targetreg;
410
411 if (abreg->regreg == AB3100_LDO_E)
412 targetreg = AB3100_LDO_E_SLEEP;
413 else if (abreg->regreg == AB3100_BUCK)
414 targetreg = AB3100_BUCK_SLEEP;
415 else
416 return -EINVAL;
417
418 /* LDO E and BUCK have special suspend voltages you can set */
419 bestindex = ab3100_get_best_voltage_index(reg, uV, uV);
420
421 err = abx500_get_register_interruptible(abreg->dev, 0,
422 targetreg, &regval);
423 if (err) {
424 dev_warn(&reg->dev,
425 "failed to get regulator register %02x\n",
426 targetreg);
427 return err;
428 }
429
430 /* The highest three bits control the variable regulators */
431 regval &= ~0xE0;
432 regval |= (bestindex << 5);
433
434 err = abx500_set_register_interruptible(abreg->dev, 0,
435 targetreg, regval);
436 if (err)
437 dev_warn(&reg->dev, "failed to set regulator register %02x\n",
438 abreg->regreg);
439
440 return err;
441 }
442
443 /*
444 * The external regulator can just define a fixed voltage.
445 */
446 static int ab3100_get_voltage_regulator_external(struct regulator_dev *reg)
447 {
448 struct ab3100_regulator *abreg = reg->reg_data;
449
450 return abreg->plfdata->external_voltage;
451 }
452
453 static struct regulator_ops regulator_ops_fixed = {
454 .enable = ab3100_enable_regulator,
455 .disable = ab3100_disable_regulator,
456 .is_enabled = ab3100_is_enabled_regulator,
457 .get_voltage = ab3100_get_voltage_regulator,
458 };
459
460 static struct regulator_ops regulator_ops_variable = {
461 .enable = ab3100_enable_regulator,
462 .disable = ab3100_disable_regulator,
463 .is_enabled = ab3100_is_enabled_regulator,
464 .get_voltage = ab3100_get_voltage_regulator,
465 .set_voltage = ab3100_set_voltage_regulator,
466 .list_voltage = ab3100_list_voltage_regulator,
467 };
468
469 static struct regulator_ops regulator_ops_variable_sleepable = {
470 .enable = ab3100_enable_regulator,
471 .disable = ab3100_disable_regulator,
472 .is_enabled = ab3100_is_enabled_regulator,
473 .get_voltage = ab3100_get_voltage_regulator,
474 .set_voltage = ab3100_set_voltage_regulator,
475 .set_suspend_voltage = ab3100_set_suspend_voltage_regulator,
476 .list_voltage = ab3100_list_voltage_regulator,
477 };
478
479 /*
480 * LDO EXT is an external regulator so it is really
481 * not possible to set any voltage locally here, AB3100
482 * is an on/off switch plain an simple. The external
483 * voltage is defined in the board set-up if any.
484 */
485 static struct regulator_ops regulator_ops_external = {
486 .enable = ab3100_enable_regulator,
487 .disable = ab3100_disable_regulator,
488 .is_enabled = ab3100_is_enabled_regulator,
489 .get_voltage = ab3100_get_voltage_regulator_external,
490 };
491
492 static struct regulator_desc
493 ab3100_regulator_desc[AB3100_NUM_REGULATORS] = {
494 {
495 .name = "LDO_A",
496 .id = AB3100_LDO_A,
497 .ops = &regulator_ops_fixed,
498 .type = REGULATOR_VOLTAGE,
499 .owner = THIS_MODULE,
500 },
501 {
502 .name = "LDO_C",
503 .id = AB3100_LDO_C,
504 .ops = &regulator_ops_fixed,
505 .type = REGULATOR_VOLTAGE,
506 .owner = THIS_MODULE,
507 },
508 {
509 .name = "LDO_D",
510 .id = AB3100_LDO_D,
511 .ops = &regulator_ops_fixed,
512 .type = REGULATOR_VOLTAGE,
513 .owner = THIS_MODULE,
514 },
515 {
516 .name = "LDO_E",
517 .id = AB3100_LDO_E,
518 .ops = &regulator_ops_variable_sleepable,
519 .n_voltages = ARRAY_SIZE(ldo_e_buck_typ_voltages),
520 .type = REGULATOR_VOLTAGE,
521 .owner = THIS_MODULE,
522 },
523 {
524 .name = "LDO_F",
525 .id = AB3100_LDO_F,
526 .ops = &regulator_ops_variable,
527 .n_voltages = ARRAY_SIZE(ldo_f_typ_voltages),
528 .type = REGULATOR_VOLTAGE,
529 .owner = THIS_MODULE,
530 },
531 {
532 .name = "LDO_G",
533 .id = AB3100_LDO_G,
534 .ops = &regulator_ops_variable,
535 .n_voltages = ARRAY_SIZE(ldo_g_typ_voltages),
536 .type = REGULATOR_VOLTAGE,
537 .owner = THIS_MODULE,
538 },
539 {
540 .name = "LDO_H",
541 .id = AB3100_LDO_H,
542 .ops = &regulator_ops_variable,
543 .n_voltages = ARRAY_SIZE(ldo_h_typ_voltages),
544 .type = REGULATOR_VOLTAGE,
545 .owner = THIS_MODULE,
546 },
547 {
548 .name = "LDO_K",
549 .id = AB3100_LDO_K,
550 .ops = &regulator_ops_variable,
551 .n_voltages = ARRAY_SIZE(ldo_k_typ_voltages),
552 .type = REGULATOR_VOLTAGE,
553 .owner = THIS_MODULE,
554 },
555 {
556 .name = "LDO_EXT",
557 .id = AB3100_LDO_EXT,
558 .ops = &regulator_ops_external,
559 .type = REGULATOR_VOLTAGE,
560 .owner = THIS_MODULE,
561 },
562 {
563 .name = "BUCK",
564 .id = AB3100_BUCK,
565 .ops = &regulator_ops_variable_sleepable,
566 .n_voltages = ARRAY_SIZE(ldo_e_buck_typ_voltages),
567 .type = REGULATOR_VOLTAGE,
568 .owner = THIS_MODULE,
569 },
570 };
571
572 /*
573 * NOTE: the following functions are regulators pluralis - it is the
574 * binding to the AB3100 core driver and the parent platform device
575 * for all the different regulators.
576 */
577
578 static int __devinit ab3100_regulators_probe(struct platform_device *pdev)
579 {
580 struct ab3100_platform_data *plfdata = mfd_get_data(pdev);
581 int err = 0;
582 u8 data;
583 int i;
584
585 /* Check chip state */
586 err = abx500_get_register_interruptible(&pdev->dev, 0,
587 AB3100_LDO_D, &data);
588 if (err) {
589 dev_err(&pdev->dev, "could not read initial status of LDO_D\n");
590 return err;
591 }
592 if (data & 0x10)
593 dev_notice(&pdev->dev,
594 "chip is already in active mode (Warm start)\n");
595 else
596 dev_notice(&pdev->dev,
597 "chip is in inactive mode (Cold start)\n");
598
599 /* Set up regulators */
600 for (i = 0; i < ARRAY_SIZE(ab3100_reg_init_order); i++) {
601 err = abx500_set_register_interruptible(&pdev->dev, 0,
602 ab3100_reg_init_order[i],
603 plfdata->reg_initvals[i]);
604 if (err) {
605 dev_err(&pdev->dev, "regulator initialization failed with error %d\n",
606 err);
607 return err;
608 }
609 }
610
611 /* Register the regulators */
612 for (i = 0; i < AB3100_NUM_REGULATORS; i++) {
613 struct ab3100_regulator *reg = &ab3100_regulators[i];
614 struct regulator_dev *rdev;
615
616 /*
617 * Initialize per-regulator struct.
618 * Inherit platform data, this comes down from the
619 * i2c boarddata, from the machine. So if you want to
620 * see what it looks like for a certain machine, go
621 * into the machine I2C setup.
622 */
623 reg->dev = &pdev->dev;
624 reg->plfdata = plfdata;
625
626 /*
627 * Register the regulator, pass around
628 * the ab3100_regulator struct
629 */
630 rdev = regulator_register(&ab3100_regulator_desc[i],
631 &pdev->dev,
632 &plfdata->reg_constraints[i],
633 reg);
634
635 if (IS_ERR(rdev)) {
636 err = PTR_ERR(rdev);
637 dev_err(&pdev->dev,
638 "%s: failed to register regulator %s err %d\n",
639 __func__, ab3100_regulator_desc[i].name,
640 err);
641 /* remove the already registered regulators */
642 while (--i >= 0)
643 regulator_unregister(ab3100_regulators[i].rdev);
644 return err;
645 }
646
647 /* Then set a pointer back to the registered regulator */
648 reg->rdev = rdev;
649 }
650
651 return 0;
652 }
653
654 static int __devexit ab3100_regulators_remove(struct platform_device *pdev)
655 {
656 int i;
657
658 for (i = 0; i < AB3100_NUM_REGULATORS; i++) {
659 struct ab3100_regulator *reg = &ab3100_regulators[i];
660
661 regulator_unregister(reg->rdev);
662 }
663 return 0;
664 }
665
666 static struct platform_driver ab3100_regulators_driver = {
667 .driver = {
668 .name = "ab3100-regulators",
669 .owner = THIS_MODULE,
670 },
671 .probe = ab3100_regulators_probe,
672 .remove = __devexit_p(ab3100_regulators_remove),
673 };
674
675 static __init int ab3100_regulators_init(void)
676 {
677 return platform_driver_register(&ab3100_regulators_driver);
678 }
679
680 static __exit void ab3100_regulators_exit(void)
681 {
682 platform_driver_unregister(&ab3100_regulators_driver);
683 }
684
685 subsys_initcall(ab3100_regulators_init);
686 module_exit(ab3100_regulators_exit);
687
688 MODULE_AUTHOR("Mattias Wallin <mattias.wallin@stericsson.com>");
689 MODULE_DESCRIPTION("AB3100 Regulator driver");
690 MODULE_LICENSE("GPL");
691 MODULE_ALIAS("platform:ab3100-regulators");