Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * tps65910.c -- TI tps65910
4 *
5 * Copyright 2010 Texas Instruments Inc.
6 *
7 * Author: Graeme Gregory <gg@slimlogic.co.uk>
8 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
9 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/err.h>
15#include <linux/of.h>
16#include <linux/platform_device.h>
17#include <linux/regulator/driver.h>
18#include <linux/regulator/machine.h>
19#include <linux/slab.h>
20#include <linux/gpio.h>
21#include <linux/mfd/tps65910.h>
22#include <linux/regulator/of_regulator.h>
23
24#define TPS65910_SUPPLY_STATE_ENABLED 0x1
25#define EXT_SLEEP_CONTROL (TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1 | \
26 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2 | \
27 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3 | \
28 TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP)
29
30/* supported VIO voltages in microvolts */
31static const unsigned int VIO_VSEL_table[] = {
32 1500000, 1800000, 2500000, 3300000,
33};
34
35/* VSEL tables for TPS65910 specific LDOs and dcdc's */
36
37/* supported VRTC voltages in microvolts */
38static const unsigned int VRTC_VSEL_table[] = {
39 1800000,
40};
41
42/* supported VDD3 voltages in microvolts */
43static const unsigned int VDD3_VSEL_table[] = {
44 5000000,
45};
46
47/* supported VDIG1 voltages in microvolts */
48static const unsigned int VDIG1_VSEL_table[] = {
49 1200000, 1500000, 1800000, 2700000,
50};
51
52/* supported VDIG2 voltages in microvolts */
53static const unsigned int VDIG2_VSEL_table[] = {
54 1000000, 1100000, 1200000, 1800000,
55};
56
57/* supported VPLL voltages in microvolts */
58static const unsigned int VPLL_VSEL_table[] = {
59 1000000, 1100000, 1800000, 2500000,
60};
61
62/* supported VDAC voltages in microvolts */
63static const unsigned int VDAC_VSEL_table[] = {
64 1800000, 2600000, 2800000, 2850000,
65};
66
67/* supported VAUX1 voltages in microvolts */
68static const unsigned int VAUX1_VSEL_table[] = {
69 1800000, 2500000, 2800000, 2850000,
70};
71
72/* supported VAUX2 voltages in microvolts */
73static const unsigned int VAUX2_VSEL_table[] = {
74 1800000, 2800000, 2900000, 3300000,
75};
76
77/* supported VAUX33 voltages in microvolts */
78static const unsigned int VAUX33_VSEL_table[] = {
79 1800000, 2000000, 2800000, 3300000,
80};
81
82/* supported VMMC voltages in microvolts */
83static const unsigned int VMMC_VSEL_table[] = {
84 1800000, 2800000, 3000000, 3300000,
85};
86
87/* supported BBCH voltages in microvolts */
88static const unsigned int VBB_VSEL_table[] = {
89 3000000, 2520000, 3150000, 5000000,
90};
91
92struct tps_info {
93 const char *name;
94 const char *vin_name;
95 u8 n_voltages;
96 const unsigned int *voltage_table;
97 int enable_time_us;
98};
99
100static struct tps_info tps65910_regs[] = {
101 {
102 .name = "vrtc",
103 .vin_name = "vcc7",
104 .n_voltages = ARRAY_SIZE(VRTC_VSEL_table),
105 .voltage_table = VRTC_VSEL_table,
106 .enable_time_us = 2200,
107 },
108 {
109 .name = "vio",
110 .vin_name = "vccio",
111 .n_voltages = ARRAY_SIZE(VIO_VSEL_table),
112 .voltage_table = VIO_VSEL_table,
113 .enable_time_us = 350,
114 },
115 {
116 .name = "vdd1",
117 .vin_name = "vcc1",
118 .enable_time_us = 350,
119 },
120 {
121 .name = "vdd2",
122 .vin_name = "vcc2",
123 .enable_time_us = 350,
124 },
125 {
126 .name = "vdd3",
127 .n_voltages = ARRAY_SIZE(VDD3_VSEL_table),
128 .voltage_table = VDD3_VSEL_table,
129 .enable_time_us = 200,
130 },
131 {
132 .name = "vdig1",
133 .vin_name = "vcc6",
134 .n_voltages = ARRAY_SIZE(VDIG1_VSEL_table),
135 .voltage_table = VDIG1_VSEL_table,
136 .enable_time_us = 100,
137 },
138 {
139 .name = "vdig2",
140 .vin_name = "vcc6",
141 .n_voltages = ARRAY_SIZE(VDIG2_VSEL_table),
142 .voltage_table = VDIG2_VSEL_table,
143 .enable_time_us = 100,
144 },
145 {
146 .name = "vpll",
147 .vin_name = "vcc5",
148 .n_voltages = ARRAY_SIZE(VPLL_VSEL_table),
149 .voltage_table = VPLL_VSEL_table,
150 .enable_time_us = 100,
151 },
152 {
153 .name = "vdac",
154 .vin_name = "vcc5",
155 .n_voltages = ARRAY_SIZE(VDAC_VSEL_table),
156 .voltage_table = VDAC_VSEL_table,
157 .enable_time_us = 100,
158 },
159 {
160 .name = "vaux1",
161 .vin_name = "vcc4",
162 .n_voltages = ARRAY_SIZE(VAUX1_VSEL_table),
163 .voltage_table = VAUX1_VSEL_table,
164 .enable_time_us = 100,
165 },
166 {
167 .name = "vaux2",
168 .vin_name = "vcc4",
169 .n_voltages = ARRAY_SIZE(VAUX2_VSEL_table),
170 .voltage_table = VAUX2_VSEL_table,
171 .enable_time_us = 100,
172 },
173 {
174 .name = "vaux33",
175 .vin_name = "vcc3",
176 .n_voltages = ARRAY_SIZE(VAUX33_VSEL_table),
177 .voltage_table = VAUX33_VSEL_table,
178 .enable_time_us = 100,
179 },
180 {
181 .name = "vmmc",
182 .vin_name = "vcc3",
183 .n_voltages = ARRAY_SIZE(VMMC_VSEL_table),
184 .voltage_table = VMMC_VSEL_table,
185 .enable_time_us = 100,
186 },
187 {
188 .name = "vbb",
189 .vin_name = "vcc7",
190 .n_voltages = ARRAY_SIZE(VBB_VSEL_table),
191 .voltage_table = VBB_VSEL_table,
192 },
193};
194
195static struct tps_info tps65911_regs[] = {
196 {
197 .name = "vrtc",
198 .vin_name = "vcc7",
199 .enable_time_us = 2200,
200 },
201 {
202 .name = "vio",
203 .vin_name = "vccio",
204 .n_voltages = ARRAY_SIZE(VIO_VSEL_table),
205 .voltage_table = VIO_VSEL_table,
206 .enable_time_us = 350,
207 },
208 {
209 .name = "vdd1",
210 .vin_name = "vcc1",
211 .n_voltages = 0x4C,
212 .enable_time_us = 350,
213 },
214 {
215 .name = "vdd2",
216 .vin_name = "vcc2",
217 .n_voltages = 0x4C,
218 .enable_time_us = 350,
219 },
220 {
221 .name = "vddctrl",
222 .n_voltages = 0x44,
223 .enable_time_us = 900,
224 },
225 {
226 .name = "ldo1",
227 .vin_name = "vcc6",
228 .n_voltages = 0x33,
229 .enable_time_us = 420,
230 },
231 {
232 .name = "ldo2",
233 .vin_name = "vcc6",
234 .n_voltages = 0x33,
235 .enable_time_us = 420,
236 },
237 {
238 .name = "ldo3",
239 .vin_name = "vcc5",
240 .n_voltages = 0x1A,
241 .enable_time_us = 230,
242 },
243 {
244 .name = "ldo4",
245 .vin_name = "vcc5",
246 .n_voltages = 0x33,
247 .enable_time_us = 230,
248 },
249 {
250 .name = "ldo5",
251 .vin_name = "vcc4",
252 .n_voltages = 0x1A,
253 .enable_time_us = 230,
254 },
255 {
256 .name = "ldo6",
257 .vin_name = "vcc3",
258 .n_voltages = 0x1A,
259 .enable_time_us = 230,
260 },
261 {
262 .name = "ldo7",
263 .vin_name = "vcc3",
264 .n_voltages = 0x1A,
265 .enable_time_us = 230,
266 },
267 {
268 .name = "ldo8",
269 .vin_name = "vcc3",
270 .n_voltages = 0x1A,
271 .enable_time_us = 230,
272 },
273};
274
275#define EXT_CONTROL_REG_BITS(id, regs_offs, bits) (((regs_offs) << 8) | (bits))
276static unsigned int tps65910_ext_sleep_control[] = {
277 0,
278 EXT_CONTROL_REG_BITS(VIO, 1, 0),
279 EXT_CONTROL_REG_BITS(VDD1, 1, 1),
280 EXT_CONTROL_REG_BITS(VDD2, 1, 2),
281 EXT_CONTROL_REG_BITS(VDD3, 1, 3),
282 EXT_CONTROL_REG_BITS(VDIG1, 0, 1),
283 EXT_CONTROL_REG_BITS(VDIG2, 0, 2),
284 EXT_CONTROL_REG_BITS(VPLL, 0, 6),
285 EXT_CONTROL_REG_BITS(VDAC, 0, 7),
286 EXT_CONTROL_REG_BITS(VAUX1, 0, 3),
287 EXT_CONTROL_REG_BITS(VAUX2, 0, 4),
288 EXT_CONTROL_REG_BITS(VAUX33, 0, 5),
289 EXT_CONTROL_REG_BITS(VMMC, 0, 0),
290};
291
292static unsigned int tps65911_ext_sleep_control[] = {
293 0,
294 EXT_CONTROL_REG_BITS(VIO, 1, 0),
295 EXT_CONTROL_REG_BITS(VDD1, 1, 1),
296 EXT_CONTROL_REG_BITS(VDD2, 1, 2),
297 EXT_CONTROL_REG_BITS(VDDCTRL, 1, 3),
298 EXT_CONTROL_REG_BITS(LDO1, 0, 1),
299 EXT_CONTROL_REG_BITS(LDO2, 0, 2),
300 EXT_CONTROL_REG_BITS(LDO3, 0, 7),
301 EXT_CONTROL_REG_BITS(LDO4, 0, 6),
302 EXT_CONTROL_REG_BITS(LDO5, 0, 3),
303 EXT_CONTROL_REG_BITS(LDO6, 0, 0),
304 EXT_CONTROL_REG_BITS(LDO7, 0, 5),
305 EXT_CONTROL_REG_BITS(LDO8, 0, 4),
306};
307
308struct tps65910_reg {
309 struct regulator_desc *desc;
310 struct tps65910 *mfd;
311 struct regulator_dev **rdev;
312 struct tps_info **info;
313 int num_regulators;
314 int mode;
315 int (*get_ctrl_reg)(int);
316 unsigned int *ext_sleep_control;
317 unsigned int board_ext_control[TPS65910_NUM_REGS];
318};
319
320static int tps65910_get_ctrl_register(int id)
321{
322 switch (id) {
323 case TPS65910_REG_VRTC:
324 return TPS65910_VRTC;
325 case TPS65910_REG_VIO:
326 return TPS65910_VIO;
327 case TPS65910_REG_VDD1:
328 return TPS65910_VDD1;
329 case TPS65910_REG_VDD2:
330 return TPS65910_VDD2;
331 case TPS65910_REG_VDD3:
332 return TPS65910_VDD3;
333 case TPS65910_REG_VDIG1:
334 return TPS65910_VDIG1;
335 case TPS65910_REG_VDIG2:
336 return TPS65910_VDIG2;
337 case TPS65910_REG_VPLL:
338 return TPS65910_VPLL;
339 case TPS65910_REG_VDAC:
340 return TPS65910_VDAC;
341 case TPS65910_REG_VAUX1:
342 return TPS65910_VAUX1;
343 case TPS65910_REG_VAUX2:
344 return TPS65910_VAUX2;
345 case TPS65910_REG_VAUX33:
346 return TPS65910_VAUX33;
347 case TPS65910_REG_VMMC:
348 return TPS65910_VMMC;
349 case TPS65910_REG_VBB:
350 return TPS65910_BBCH;
351 default:
352 return -EINVAL;
353 }
354}
355
356static int tps65911_get_ctrl_register(int id)
357{
358 switch (id) {
359 case TPS65910_REG_VRTC:
360 return TPS65910_VRTC;
361 case TPS65910_REG_VIO:
362 return TPS65910_VIO;
363 case TPS65910_REG_VDD1:
364 return TPS65910_VDD1;
365 case TPS65910_REG_VDD2:
366 return TPS65910_VDD2;
367 case TPS65911_REG_VDDCTRL:
368 return TPS65911_VDDCTRL;
369 case TPS65911_REG_LDO1:
370 return TPS65911_LDO1;
371 case TPS65911_REG_LDO2:
372 return TPS65911_LDO2;
373 case TPS65911_REG_LDO3:
374 return TPS65911_LDO3;
375 case TPS65911_REG_LDO4:
376 return TPS65911_LDO4;
377 case TPS65911_REG_LDO5:
378 return TPS65911_LDO5;
379 case TPS65911_REG_LDO6:
380 return TPS65911_LDO6;
381 case TPS65911_REG_LDO7:
382 return TPS65911_LDO7;
383 case TPS65911_REG_LDO8:
384 return TPS65911_LDO8;
385 default:
386 return -EINVAL;
387 }
388}
389
390static int tps65910_set_mode(struct regulator_dev *dev, unsigned int mode)
391{
392 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
393 struct tps65910 *mfd = pmic->mfd;
394 int reg, value, id = rdev_get_id(dev);
395
396 reg = pmic->get_ctrl_reg(id);
397 if (reg < 0)
398 return reg;
399
400 switch (mode) {
401 case REGULATOR_MODE_NORMAL:
402 return tps65910_reg_update_bits(pmic->mfd, reg,
403 LDO_ST_MODE_BIT | LDO_ST_ON_BIT,
404 LDO_ST_ON_BIT);
405 case REGULATOR_MODE_IDLE:
406 value = LDO_ST_ON_BIT | LDO_ST_MODE_BIT;
407 return tps65910_reg_set_bits(mfd, reg, value);
408 case REGULATOR_MODE_STANDBY:
409 return tps65910_reg_clear_bits(mfd, reg, LDO_ST_ON_BIT);
410 }
411
412 return -EINVAL;
413}
414
415static unsigned int tps65910_get_mode(struct regulator_dev *dev)
416{
417 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
418 int ret, reg, value, id = rdev_get_id(dev);
419
420 reg = pmic->get_ctrl_reg(id);
421 if (reg < 0)
422 return reg;
423
424 ret = tps65910_reg_read(pmic->mfd, reg, &value);
425 if (ret < 0)
426 return ret;
427
428 if (!(value & LDO_ST_ON_BIT))
429 return REGULATOR_MODE_STANDBY;
430 else if (value & LDO_ST_MODE_BIT)
431 return REGULATOR_MODE_IDLE;
432 else
433 return REGULATOR_MODE_NORMAL;
434}
435
436static int tps65910_get_voltage_dcdc_sel(struct regulator_dev *dev)
437{
438 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
439 int ret, id = rdev_get_id(dev);
440 int opvsel = 0, srvsel = 0, vselmax = 0, mult = 0, sr = 0;
441
442 switch (id) {
443 case TPS65910_REG_VDD1:
444 ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD1_OP, &opvsel);
445 if (ret < 0)
446 return ret;
447 ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD1, &mult);
448 if (ret < 0)
449 return ret;
450 mult = (mult & VDD1_VGAIN_SEL_MASK) >> VDD1_VGAIN_SEL_SHIFT;
451 ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD1_SR, &srvsel);
452 if (ret < 0)
453 return ret;
454 sr = opvsel & VDD1_OP_CMD_MASK;
455 opvsel &= VDD1_OP_SEL_MASK;
456 srvsel &= VDD1_SR_SEL_MASK;
457 vselmax = 75;
458 break;
459 case TPS65910_REG_VDD2:
460 ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD2_OP, &opvsel);
461 if (ret < 0)
462 return ret;
463 ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD2, &mult);
464 if (ret < 0)
465 return ret;
466 mult = (mult & VDD2_VGAIN_SEL_MASK) >> VDD2_VGAIN_SEL_SHIFT;
467 ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD2_SR, &srvsel);
468 if (ret < 0)
469 return ret;
470 sr = opvsel & VDD2_OP_CMD_MASK;
471 opvsel &= VDD2_OP_SEL_MASK;
472 srvsel &= VDD2_SR_SEL_MASK;
473 vselmax = 75;
474 break;
475 case TPS65911_REG_VDDCTRL:
476 ret = tps65910_reg_read(pmic->mfd, TPS65911_VDDCTRL_OP,
477 &opvsel);
478 if (ret < 0)
479 return ret;
480 ret = tps65910_reg_read(pmic->mfd, TPS65911_VDDCTRL_SR,
481 &srvsel);
482 if (ret < 0)
483 return ret;
484 sr = opvsel & VDDCTRL_OP_CMD_MASK;
485 opvsel &= VDDCTRL_OP_SEL_MASK;
486 srvsel &= VDDCTRL_SR_SEL_MASK;
487 vselmax = 64;
488 break;
489 }
490
491 /* multiplier 0 == 1 but 2,3 normal */
492 if (!mult)
493 mult = 1;
494
495 if (sr) {
496 /* normalise to valid range */
497 if (srvsel < 3)
498 srvsel = 3;
499 if (srvsel > vselmax)
500 srvsel = vselmax;
501 return srvsel - 3;
502 } else {
503
504 /* normalise to valid range*/
505 if (opvsel < 3)
506 opvsel = 3;
507 if (opvsel > vselmax)
508 opvsel = vselmax;
509 return opvsel - 3;
510 }
511 return -EINVAL;
512}
513
514static int tps65910_get_voltage_sel(struct regulator_dev *dev)
515{
516 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
517 int ret, reg, value, id = rdev_get_id(dev);
518
519 reg = pmic->get_ctrl_reg(id);
520 if (reg < 0)
521 return reg;
522
523 ret = tps65910_reg_read(pmic->mfd, reg, &value);
524 if (ret < 0)
525 return ret;
526
527 switch (id) {
528 case TPS65910_REG_VIO:
529 case TPS65910_REG_VDIG1:
530 case TPS65910_REG_VDIG2:
531 case TPS65910_REG_VPLL:
532 case TPS65910_REG_VDAC:
533 case TPS65910_REG_VAUX1:
534 case TPS65910_REG_VAUX2:
535 case TPS65910_REG_VAUX33:
536 case TPS65910_REG_VMMC:
537 value &= LDO_SEL_MASK;
538 value >>= LDO_SEL_SHIFT;
539 break;
540 case TPS65910_REG_VBB:
541 value &= BBCH_BBSEL_MASK;
542 value >>= BBCH_BBSEL_SHIFT;
543 break;
544 default:
545 return -EINVAL;
546 }
547
548 return value;
549}
550
551static int tps65910_get_voltage_vdd3(struct regulator_dev *dev)
552{
553 return dev->desc->volt_table[0];
554}
555
556static int tps65911_get_voltage_sel(struct regulator_dev *dev)
557{
558 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
559 int ret, id = rdev_get_id(dev);
560 unsigned int value, reg;
561
562 reg = pmic->get_ctrl_reg(id);
563
564 ret = tps65910_reg_read(pmic->mfd, reg, &value);
565 if (ret < 0)
566 return ret;
567
568 switch (id) {
569 case TPS65911_REG_LDO1:
570 case TPS65911_REG_LDO2:
571 case TPS65911_REG_LDO4:
572 value &= LDO1_SEL_MASK;
573 value >>= LDO_SEL_SHIFT;
574 break;
575 case TPS65911_REG_LDO3:
576 case TPS65911_REG_LDO5:
577 case TPS65911_REG_LDO6:
578 case TPS65911_REG_LDO7:
579 case TPS65911_REG_LDO8:
580 value &= LDO3_SEL_MASK;
581 value >>= LDO_SEL_SHIFT;
582 break;
583 case TPS65910_REG_VIO:
584 value &= LDO_SEL_MASK;
585 value >>= LDO_SEL_SHIFT;
586 break;
587 default:
588 return -EINVAL;
589 }
590
591 return value;
592}
593
594static int tps65910_set_voltage_dcdc_sel(struct regulator_dev *dev,
595 unsigned selector)
596{
597 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
598 int id = rdev_get_id(dev), vsel;
599 int dcdc_mult = 0;
600
601 switch (id) {
602 case TPS65910_REG_VDD1:
603 dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
604 if (dcdc_mult == 1)
605 dcdc_mult--;
606 vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
607
608 tps65910_reg_update_bits(pmic->mfd, TPS65910_VDD1,
609 VDD1_VGAIN_SEL_MASK,
610 dcdc_mult << VDD1_VGAIN_SEL_SHIFT);
611 tps65910_reg_write(pmic->mfd, TPS65910_VDD1_OP, vsel);
612 break;
613 case TPS65910_REG_VDD2:
614 dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
615 if (dcdc_mult == 1)
616 dcdc_mult--;
617 vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
618
619 tps65910_reg_update_bits(pmic->mfd, TPS65910_VDD2,
620 VDD1_VGAIN_SEL_MASK,
621 dcdc_mult << VDD2_VGAIN_SEL_SHIFT);
622 tps65910_reg_write(pmic->mfd, TPS65910_VDD2_OP, vsel);
623 break;
624 case TPS65911_REG_VDDCTRL:
625 vsel = selector + 3;
626 tps65910_reg_write(pmic->mfd, TPS65911_VDDCTRL_OP, vsel);
627 }
628
629 return 0;
630}
631
632static int tps65910_set_voltage_sel(struct regulator_dev *dev,
633 unsigned selector)
634{
635 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
636 int reg, id = rdev_get_id(dev);
637
638 reg = pmic->get_ctrl_reg(id);
639 if (reg < 0)
640 return reg;
641
642 switch (id) {
643 case TPS65910_REG_VIO:
644 case TPS65910_REG_VDIG1:
645 case TPS65910_REG_VDIG2:
646 case TPS65910_REG_VPLL:
647 case TPS65910_REG_VDAC:
648 case TPS65910_REG_VAUX1:
649 case TPS65910_REG_VAUX2:
650 case TPS65910_REG_VAUX33:
651 case TPS65910_REG_VMMC:
652 return tps65910_reg_update_bits(pmic->mfd, reg, LDO_SEL_MASK,
653 selector << LDO_SEL_SHIFT);
654 case TPS65910_REG_VBB:
655 return tps65910_reg_update_bits(pmic->mfd, reg, BBCH_BBSEL_MASK,
656 selector << BBCH_BBSEL_SHIFT);
657 }
658
659 return -EINVAL;
660}
661
662static int tps65911_set_voltage_sel(struct regulator_dev *dev,
663 unsigned selector)
664{
665 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
666 int reg, id = rdev_get_id(dev);
667
668 reg = pmic->get_ctrl_reg(id);
669 if (reg < 0)
670 return reg;
671
672 switch (id) {
673 case TPS65911_REG_LDO1:
674 case TPS65911_REG_LDO2:
675 case TPS65911_REG_LDO4:
676 return tps65910_reg_update_bits(pmic->mfd, reg, LDO1_SEL_MASK,
677 selector << LDO_SEL_SHIFT);
678 case TPS65911_REG_LDO3:
679 case TPS65911_REG_LDO5:
680 case TPS65911_REG_LDO6:
681 case TPS65911_REG_LDO7:
682 case TPS65911_REG_LDO8:
683 return tps65910_reg_update_bits(pmic->mfd, reg, LDO3_SEL_MASK,
684 selector << LDO_SEL_SHIFT);
685 case TPS65910_REG_VIO:
686 return tps65910_reg_update_bits(pmic->mfd, reg, LDO_SEL_MASK,
687 selector << LDO_SEL_SHIFT);
688 case TPS65910_REG_VBB:
689 return tps65910_reg_update_bits(pmic->mfd, reg, BBCH_BBSEL_MASK,
690 selector << BBCH_BBSEL_SHIFT);
691 }
692
693 return -EINVAL;
694}
695
696
697static int tps65910_list_voltage_dcdc(struct regulator_dev *dev,
698 unsigned selector)
699{
700 int volt, mult = 1, id = rdev_get_id(dev);
701
702 switch (id) {
703 case TPS65910_REG_VDD1:
704 case TPS65910_REG_VDD2:
705 mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
706 volt = VDD1_2_MIN_VOLT +
707 (selector % VDD1_2_NUM_VOLT_FINE) * VDD1_2_OFFSET;
708 break;
709 case TPS65911_REG_VDDCTRL:
710 volt = VDDCTRL_MIN_VOLT + (selector * VDDCTRL_OFFSET);
711 break;
712 default:
713 BUG();
714 return -EINVAL;
715 }
716
717 return volt * 100 * mult;
718}
719
720static int tps65911_list_voltage(struct regulator_dev *dev, unsigned selector)
721{
722 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
723 int step_mv = 0, id = rdev_get_id(dev);
724
725 switch (id) {
726 case TPS65911_REG_LDO1:
727 case TPS65911_REG_LDO2:
728 case TPS65911_REG_LDO4:
729 /* The first 5 values of the selector correspond to 1V */
730 if (selector < 5)
731 selector = 0;
732 else
733 selector -= 4;
734
735 step_mv = 50;
736 break;
737 case TPS65911_REG_LDO3:
738 case TPS65911_REG_LDO5:
739 case TPS65911_REG_LDO6:
740 case TPS65911_REG_LDO7:
741 case TPS65911_REG_LDO8:
742 /* The first 3 values of the selector correspond to 1V */
743 if (selector < 3)
744 selector = 0;
745 else
746 selector -= 2;
747
748 step_mv = 100;
749 break;
750 case TPS65910_REG_VIO:
751 return pmic->info[id]->voltage_table[selector];
752 default:
753 return -EINVAL;
754 }
755
756 return (LDO_MIN_VOLT + selector * step_mv) * 1000;
757}
758
759/* Regulator ops (except VRTC) */
760static struct regulator_ops tps65910_ops_dcdc = {
761 .is_enabled = regulator_is_enabled_regmap,
762 .enable = regulator_enable_regmap,
763 .disable = regulator_disable_regmap,
764 .set_mode = tps65910_set_mode,
765 .get_mode = tps65910_get_mode,
766 .get_voltage_sel = tps65910_get_voltage_dcdc_sel,
767 .set_voltage_sel = tps65910_set_voltage_dcdc_sel,
768 .set_voltage_time_sel = regulator_set_voltage_time_sel,
769 .list_voltage = tps65910_list_voltage_dcdc,
770 .map_voltage = regulator_map_voltage_ascend,
771};
772
773static struct regulator_ops tps65910_ops_vdd3 = {
774 .is_enabled = regulator_is_enabled_regmap,
775 .enable = regulator_enable_regmap,
776 .disable = regulator_disable_regmap,
777 .set_mode = tps65910_set_mode,
778 .get_mode = tps65910_get_mode,
779 .get_voltage = tps65910_get_voltage_vdd3,
780 .list_voltage = regulator_list_voltage_table,
781 .map_voltage = regulator_map_voltage_ascend,
782};
783
784static struct regulator_ops tps65910_ops_vbb = {
785 .is_enabled = regulator_is_enabled_regmap,
786 .enable = regulator_enable_regmap,
787 .disable = regulator_disable_regmap,
788 .set_mode = tps65910_set_mode,
789 .get_mode = tps65910_get_mode,
790 .get_voltage_sel = tps65910_get_voltage_sel,
791 .set_voltage_sel = tps65910_set_voltage_sel,
792 .list_voltage = regulator_list_voltage_table,
793 .map_voltage = regulator_map_voltage_iterate,
794};
795
796static struct regulator_ops tps65910_ops = {
797 .is_enabled = regulator_is_enabled_regmap,
798 .enable = regulator_enable_regmap,
799 .disable = regulator_disable_regmap,
800 .set_mode = tps65910_set_mode,
801 .get_mode = tps65910_get_mode,
802 .get_voltage_sel = tps65910_get_voltage_sel,
803 .set_voltage_sel = tps65910_set_voltage_sel,
804 .list_voltage = regulator_list_voltage_table,
805 .map_voltage = regulator_map_voltage_ascend,
806};
807
808static struct regulator_ops tps65911_ops = {
809 .is_enabled = regulator_is_enabled_regmap,
810 .enable = regulator_enable_regmap,
811 .disable = regulator_disable_regmap,
812 .set_mode = tps65910_set_mode,
813 .get_mode = tps65910_get_mode,
814 .get_voltage_sel = tps65911_get_voltage_sel,
815 .set_voltage_sel = tps65911_set_voltage_sel,
816 .list_voltage = tps65911_list_voltage,
817 .map_voltage = regulator_map_voltage_ascend,
818};
819
820static int tps65910_set_ext_sleep_config(struct tps65910_reg *pmic,
821 int id, int ext_sleep_config)
822{
823 struct tps65910 *mfd = pmic->mfd;
824 u8 regoffs = (pmic->ext_sleep_control[id] >> 8) & 0xFF;
825 u8 bit_pos = (1 << pmic->ext_sleep_control[id] & 0xFF);
826 int ret;
827
828 /*
829 * Regulator can not be control from multiple external input EN1, EN2
830 * and EN3 together.
831 */
832 if (ext_sleep_config & EXT_SLEEP_CONTROL) {
833 int en_count;
834 en_count = ((ext_sleep_config &
835 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1) != 0);
836 en_count += ((ext_sleep_config &
837 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2) != 0);
838 en_count += ((ext_sleep_config &
839 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3) != 0);
840 en_count += ((ext_sleep_config &
841 TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP) != 0);
842 if (en_count > 1) {
843 dev_err(mfd->dev,
844 "External sleep control flag is not proper\n");
845 return -EINVAL;
846 }
847 }
848
849 pmic->board_ext_control[id] = ext_sleep_config;
850
851 /* External EN1 control */
852 if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1)
853 ret = tps65910_reg_set_bits(mfd,
854 TPS65910_EN1_LDO_ASS + regoffs, bit_pos);
855 else
856 ret = tps65910_reg_clear_bits(mfd,
857 TPS65910_EN1_LDO_ASS + regoffs, bit_pos);
858 if (ret < 0) {
859 dev_err(mfd->dev,
860 "Error in configuring external control EN1\n");
861 return ret;
862 }
863
864 /* External EN2 control */
865 if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2)
866 ret = tps65910_reg_set_bits(mfd,
867 TPS65910_EN2_LDO_ASS + regoffs, bit_pos);
868 else
869 ret = tps65910_reg_clear_bits(mfd,
870 TPS65910_EN2_LDO_ASS + regoffs, bit_pos);
871 if (ret < 0) {
872 dev_err(mfd->dev,
873 "Error in configuring external control EN2\n");
874 return ret;
875 }
876
877 /* External EN3 control for TPS65910 LDO only */
878 if ((tps65910_chip_id(mfd) == TPS65910) &&
879 (id >= TPS65910_REG_VDIG1)) {
880 if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3)
881 ret = tps65910_reg_set_bits(mfd,
882 TPS65910_EN3_LDO_ASS + regoffs, bit_pos);
883 else
884 ret = tps65910_reg_clear_bits(mfd,
885 TPS65910_EN3_LDO_ASS + regoffs, bit_pos);
886 if (ret < 0) {
887 dev_err(mfd->dev,
888 "Error in configuring external control EN3\n");
889 return ret;
890 }
891 }
892
893 /* Return if no external control is selected */
894 if (!(ext_sleep_config & EXT_SLEEP_CONTROL)) {
895 /* Clear all sleep controls */
896 ret = tps65910_reg_clear_bits(mfd,
897 TPS65910_SLEEP_KEEP_LDO_ON + regoffs, bit_pos);
898 if (!ret)
899 ret = tps65910_reg_clear_bits(mfd,
900 TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
901 if (ret < 0)
902 dev_err(mfd->dev,
903 "Error in configuring SLEEP register\n");
904 return ret;
905 }
906
907 /*
908 * For regulator that has separate operational and sleep register make
909 * sure that operational is used and clear sleep register to turn
910 * regulator off when external control is inactive
911 */
912 if ((id == TPS65910_REG_VDD1) ||
913 (id == TPS65910_REG_VDD2) ||
914 ((id == TPS65911_REG_VDDCTRL) &&
915 (tps65910_chip_id(mfd) == TPS65911))) {
916 int op_reg_add = pmic->get_ctrl_reg(id) + 1;
917 int sr_reg_add = pmic->get_ctrl_reg(id) + 2;
918 int opvsel, srvsel;
919
920 ret = tps65910_reg_read(pmic->mfd, op_reg_add, &opvsel);
921 if (ret < 0)
922 return ret;
923 ret = tps65910_reg_read(pmic->mfd, sr_reg_add, &srvsel);
924 if (ret < 0)
925 return ret;
926
927 if (opvsel & VDD1_OP_CMD_MASK) {
928 u8 reg_val = srvsel & VDD1_OP_SEL_MASK;
929
930 ret = tps65910_reg_write(pmic->mfd, op_reg_add,
931 reg_val);
932 if (ret < 0) {
933 dev_err(mfd->dev,
934 "Error in configuring op register\n");
935 return ret;
936 }
937 }
938 ret = tps65910_reg_write(pmic->mfd, sr_reg_add, 0);
939 if (ret < 0) {
940 dev_err(mfd->dev, "Error in setting sr register\n");
941 return ret;
942 }
943 }
944
945 ret = tps65910_reg_clear_bits(mfd,
946 TPS65910_SLEEP_KEEP_LDO_ON + regoffs, bit_pos);
947 if (!ret) {
948 if (ext_sleep_config & TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP)
949 ret = tps65910_reg_set_bits(mfd,
950 TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
951 else
952 ret = tps65910_reg_clear_bits(mfd,
953 TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
954 }
955 if (ret < 0)
956 dev_err(mfd->dev,
957 "Error in configuring SLEEP register\n");
958
959 return ret;
960}
961
962#ifdef CONFIG_OF
963
964static struct of_regulator_match tps65910_matches[] = {
965 { .name = "vrtc", .driver_data = (void *) &tps65910_regs[0] },
966 { .name = "vio", .driver_data = (void *) &tps65910_regs[1] },
967 { .name = "vdd1", .driver_data = (void *) &tps65910_regs[2] },
968 { .name = "vdd2", .driver_data = (void *) &tps65910_regs[3] },
969 { .name = "vdd3", .driver_data = (void *) &tps65910_regs[4] },
970 { .name = "vdig1", .driver_data = (void *) &tps65910_regs[5] },
971 { .name = "vdig2", .driver_data = (void *) &tps65910_regs[6] },
972 { .name = "vpll", .driver_data = (void *) &tps65910_regs[7] },
973 { .name = "vdac", .driver_data = (void *) &tps65910_regs[8] },
974 { .name = "vaux1", .driver_data = (void *) &tps65910_regs[9] },
975 { .name = "vaux2", .driver_data = (void *) &tps65910_regs[10] },
976 { .name = "vaux33", .driver_data = (void *) &tps65910_regs[11] },
977 { .name = "vmmc", .driver_data = (void *) &tps65910_regs[12] },
978 { .name = "vbb", .driver_data = (void *) &tps65910_regs[13] },
979};
980
981static struct of_regulator_match tps65911_matches[] = {
982 { .name = "vrtc", .driver_data = (void *) &tps65911_regs[0] },
983 { .name = "vio", .driver_data = (void *) &tps65911_regs[1] },
984 { .name = "vdd1", .driver_data = (void *) &tps65911_regs[2] },
985 { .name = "vdd2", .driver_data = (void *) &tps65911_regs[3] },
986 { .name = "vddctrl", .driver_data = (void *) &tps65911_regs[4] },
987 { .name = "ldo1", .driver_data = (void *) &tps65911_regs[5] },
988 { .name = "ldo2", .driver_data = (void *) &tps65911_regs[6] },
989 { .name = "ldo3", .driver_data = (void *) &tps65911_regs[7] },
990 { .name = "ldo4", .driver_data = (void *) &tps65911_regs[8] },
991 { .name = "ldo5", .driver_data = (void *) &tps65911_regs[9] },
992 { .name = "ldo6", .driver_data = (void *) &tps65911_regs[10] },
993 { .name = "ldo7", .driver_data = (void *) &tps65911_regs[11] },
994 { .name = "ldo8", .driver_data = (void *) &tps65911_regs[12] },
995};
996
997static struct tps65910_board *tps65910_parse_dt_reg_data(
998 struct platform_device *pdev,
999 struct of_regulator_match **tps65910_reg_matches)
1000{
1001 struct tps65910_board *pmic_plat_data;
1002 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
1003 struct device_node *np, *regulators;
1004 struct of_regulator_match *matches;
1005 unsigned int prop;
1006 int idx = 0, ret, count;
1007
1008 pmic_plat_data = devm_kzalloc(&pdev->dev, sizeof(*pmic_plat_data),
1009 GFP_KERNEL);
1010 if (!pmic_plat_data)
1011 return NULL;
1012
1013 np = pdev->dev.parent->of_node;
1014 regulators = of_get_child_by_name(np, "regulators");
1015 if (!regulators) {
1016 dev_err(&pdev->dev, "regulator node not found\n");
1017 return NULL;
1018 }
1019
1020 switch (tps65910_chip_id(tps65910)) {
1021 case TPS65910:
1022 count = ARRAY_SIZE(tps65910_matches);
1023 matches = tps65910_matches;
1024 break;
1025 case TPS65911:
1026 count = ARRAY_SIZE(tps65911_matches);
1027 matches = tps65911_matches;
1028 break;
1029 default:
1030 of_node_put(regulators);
1031 dev_err(&pdev->dev, "Invalid tps chip version\n");
1032 return NULL;
1033 }
1034
1035 ret = of_regulator_match(&pdev->dev, regulators, matches, count);
1036 of_node_put(regulators);
1037 if (ret < 0) {
1038 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
1039 ret);
1040 return NULL;
1041 }
1042
1043 *tps65910_reg_matches = matches;
1044
1045 for (idx = 0; idx < count; idx++) {
1046 if (!matches[idx].of_node)
1047 continue;
1048
1049 pmic_plat_data->tps65910_pmic_init_data[idx] =
1050 matches[idx].init_data;
1051
1052 ret = of_property_read_u32(matches[idx].of_node,
1053 "ti,regulator-ext-sleep-control", &prop);
1054 if (!ret)
1055 pmic_plat_data->regulator_ext_sleep_control[idx] = prop;
1056
1057 }
1058
1059 return pmic_plat_data;
1060}
1061#else
1062static inline struct tps65910_board *tps65910_parse_dt_reg_data(
1063 struct platform_device *pdev,
1064 struct of_regulator_match **tps65910_reg_matches)
1065{
1066 *tps65910_reg_matches = NULL;
1067 return NULL;
1068}
1069#endif
1070
1071static int tps65910_probe(struct platform_device *pdev)
1072{
1073 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
1074 struct regulator_config config = { };
1075 struct tps_info *info;
1076 struct regulator_dev *rdev;
1077 struct tps65910_reg *pmic;
1078 struct tps65910_board *pmic_plat_data;
1079 struct of_regulator_match *tps65910_reg_matches = NULL;
1080 int i, err;
1081
1082 pmic_plat_data = dev_get_platdata(tps65910->dev);
1083 if (!pmic_plat_data && tps65910->dev->of_node)
1084 pmic_plat_data = tps65910_parse_dt_reg_data(pdev,
1085 &tps65910_reg_matches);
1086
1087 if (!pmic_plat_data) {
1088 dev_err(&pdev->dev, "Platform data not found\n");
1089 return -EINVAL;
1090 }
1091
1092 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
1093 if (!pmic)
1094 return -ENOMEM;
1095
1096 pmic->mfd = tps65910;
1097 platform_set_drvdata(pdev, pmic);
1098
1099 /* Give control of all register to control port */
1100 err = tps65910_reg_set_bits(pmic->mfd, TPS65910_DEVCTRL,
1101 DEVCTRL_SR_CTL_I2C_SEL_MASK);
1102 if (err < 0)
1103 return err;
1104
1105 switch (tps65910_chip_id(tps65910)) {
1106 case TPS65910:
1107 BUILD_BUG_ON(TPS65910_NUM_REGS < ARRAY_SIZE(tps65910_regs));
1108 pmic->get_ctrl_reg = &tps65910_get_ctrl_register;
1109 pmic->num_regulators = ARRAY_SIZE(tps65910_regs);
1110 pmic->ext_sleep_control = tps65910_ext_sleep_control;
1111 info = tps65910_regs;
1112 /* Work around silicon erratum SWCZ010: output programmed
1113 * voltage level can go higher than expected or crash
1114 * Workaround: use no synchronization of DCDC clocks
1115 */
1116 tps65910_reg_clear_bits(pmic->mfd, TPS65910_DCDCCTRL,
1117 DCDCCTRL_DCDCCKSYNC_MASK);
1118 break;
1119 case TPS65911:
1120 BUILD_BUG_ON(TPS65910_NUM_REGS < ARRAY_SIZE(tps65911_regs));
1121 pmic->get_ctrl_reg = &tps65911_get_ctrl_register;
1122 pmic->num_regulators = ARRAY_SIZE(tps65911_regs);
1123 pmic->ext_sleep_control = tps65911_ext_sleep_control;
1124 info = tps65911_regs;
1125 break;
1126 default:
1127 dev_err(&pdev->dev, "Invalid tps chip version\n");
1128 return -ENODEV;
1129 }
1130
1131 pmic->desc = devm_kcalloc(&pdev->dev,
1132 pmic->num_regulators,
1133 sizeof(struct regulator_desc),
1134 GFP_KERNEL);
1135 if (!pmic->desc)
1136 return -ENOMEM;
1137
1138 pmic->info = devm_kcalloc(&pdev->dev,
1139 pmic->num_regulators,
1140 sizeof(struct tps_info *),
1141 GFP_KERNEL);
1142 if (!pmic->info)
1143 return -ENOMEM;
1144
1145 pmic->rdev = devm_kcalloc(&pdev->dev,
1146 pmic->num_regulators,
1147 sizeof(struct regulator_dev *),
1148 GFP_KERNEL);
1149 if (!pmic->rdev)
1150 return -ENOMEM;
1151
1152 for (i = 0; i < pmic->num_regulators; i++, info++) {
1153 /* Register the regulators */
1154 pmic->info[i] = info;
1155
1156 pmic->desc[i].name = info->name;
1157 pmic->desc[i].supply_name = info->vin_name;
1158 pmic->desc[i].id = i;
1159 pmic->desc[i].n_voltages = info->n_voltages;
1160 pmic->desc[i].enable_time = info->enable_time_us;
1161
1162 if (i == TPS65910_REG_VDD1 || i == TPS65910_REG_VDD2) {
1163 pmic->desc[i].ops = &tps65910_ops_dcdc;
1164 pmic->desc[i].n_voltages = VDD1_2_NUM_VOLT_FINE *
1165 VDD1_2_NUM_VOLT_COARSE;
1166 pmic->desc[i].ramp_delay = 12500;
1167 } else if (i == TPS65910_REG_VDD3) {
1168 if (tps65910_chip_id(tps65910) == TPS65910) {
1169 pmic->desc[i].ops = &tps65910_ops_vdd3;
1170 pmic->desc[i].volt_table = info->voltage_table;
1171 } else {
1172 pmic->desc[i].ops = &tps65910_ops_dcdc;
1173 pmic->desc[i].ramp_delay = 5000;
1174 }
1175 } else if (i == TPS65910_REG_VBB &&
1176 tps65910_chip_id(tps65910) == TPS65910) {
1177 pmic->desc[i].ops = &tps65910_ops_vbb;
1178 pmic->desc[i].volt_table = info->voltage_table;
1179 } else {
1180 if (tps65910_chip_id(tps65910) == TPS65910) {
1181 pmic->desc[i].ops = &tps65910_ops;
1182 pmic->desc[i].volt_table = info->voltage_table;
1183 } else {
1184 pmic->desc[i].ops = &tps65911_ops;
1185 }
1186 }
1187
1188 err = tps65910_set_ext_sleep_config(pmic, i,
1189 pmic_plat_data->regulator_ext_sleep_control[i]);
1190 /*
1191 * Failing on regulator for configuring externally control
1192 * is not a serious issue, just throw warning.
1193 */
1194 if (err < 0)
1195 dev_warn(tps65910->dev,
1196 "Failed to initialise ext control config\n");
1197
1198 pmic->desc[i].type = REGULATOR_VOLTAGE;
1199 pmic->desc[i].owner = THIS_MODULE;
1200 pmic->desc[i].enable_reg = pmic->get_ctrl_reg(i);
1201 pmic->desc[i].enable_mask = TPS65910_SUPPLY_STATE_ENABLED;
1202
1203 config.dev = tps65910->dev;
1204 config.init_data = pmic_plat_data->tps65910_pmic_init_data[i];
1205 config.driver_data = pmic;
1206 config.regmap = tps65910->regmap;
1207
1208 if (tps65910_reg_matches)
1209 config.of_node = tps65910_reg_matches[i].of_node;
1210
1211 rdev = devm_regulator_register(&pdev->dev, &pmic->desc[i],
1212 &config);
1213 if (IS_ERR(rdev)) {
1214 dev_err(tps65910->dev,
1215 "failed to register %s regulator\n",
1216 pdev->name);
1217 return PTR_ERR(rdev);
1218 }
1219
1220 /* Save regulator for cleanup */
1221 pmic->rdev[i] = rdev;
1222 }
1223 return 0;
1224}
1225
1226static void tps65910_shutdown(struct platform_device *pdev)
1227{
1228 struct tps65910_reg *pmic = platform_get_drvdata(pdev);
1229 int i;
1230
1231 /*
1232 * Before bootloader jumps to kernel, it makes sure that required
1233 * external control signals are in desired state so that given rails
1234 * can be configure accordingly.
1235 * If rails are configured to be controlled from external control
1236 * then before shutting down/rebooting the system, the external
1237 * control configuration need to be remove from the rails so that
1238 * its output will be available as per register programming even
1239 * if external controls are removed. This is require when the POR
1240 * value of the control signals are not in active state and before
1241 * bootloader initializes it, the system requires the rail output
1242 * to be active for booting.
1243 */
1244 for (i = 0; i < pmic->num_regulators; i++) {
1245 int err;
1246 if (!pmic->rdev[i])
1247 continue;
1248
1249 err = tps65910_set_ext_sleep_config(pmic, i, 0);
1250 if (err < 0)
1251 dev_err(&pdev->dev,
1252 "Error in clearing external control\n");
1253 }
1254}
1255
1256static struct platform_driver tps65910_driver = {
1257 .driver = {
1258 .name = "tps65910-pmic",
1259 },
1260 .probe = tps65910_probe,
1261 .shutdown = tps65910_shutdown,
1262};
1263
1264static int __init tps65910_init(void)
1265{
1266 return platform_driver_register(&tps65910_driver);
1267}
1268subsys_initcall(tps65910_init);
1269
1270static void __exit tps65910_cleanup(void)
1271{
1272 platform_driver_unregister(&tps65910_driver);
1273}
1274module_exit(tps65910_cleanup);
1275
1276MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1277MODULE_DESCRIPTION("TPS65910/TPS65911 voltage regulator driver");
1278MODULE_LICENSE("GPL v2");
1279MODULE_ALIAS("platform:tps65910-pmic");
1/*
2 * tps65910.c -- TI tps65910
3 *
4 * Copyright 2010 Texas Instruments Inc.
5 *
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
7 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/err.h>
20#include <linux/platform_device.h>
21#include <linux/regulator/driver.h>
22#include <linux/regulator/machine.h>
23#include <linux/slab.h>
24#include <linux/gpio.h>
25#include <linux/mfd/tps65910.h>
26#include <linux/regulator/of_regulator.h>
27
28#define TPS65910_SUPPLY_STATE_ENABLED 0x1
29#define EXT_SLEEP_CONTROL (TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1 | \
30 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2 | \
31 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3 | \
32 TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP)
33
34/* supported VIO voltages in millivolts */
35static const u16 VIO_VSEL_table[] = {
36 1500, 1800, 2500, 3300,
37};
38
39/* VSEL tables for TPS65910 specific LDOs and dcdc's */
40
41/* supported VDD3 voltages in millivolts */
42static const u16 VDD3_VSEL_table[] = {
43 5000,
44};
45
46/* supported VDIG1 voltages in millivolts */
47static const u16 VDIG1_VSEL_table[] = {
48 1200, 1500, 1800, 2700,
49};
50
51/* supported VDIG2 voltages in millivolts */
52static const u16 VDIG2_VSEL_table[] = {
53 1000, 1100, 1200, 1800,
54};
55
56/* supported VPLL voltages in millivolts */
57static const u16 VPLL_VSEL_table[] = {
58 1000, 1100, 1800, 2500,
59};
60
61/* supported VDAC voltages in millivolts */
62static const u16 VDAC_VSEL_table[] = {
63 1800, 2600, 2800, 2850,
64};
65
66/* supported VAUX1 voltages in millivolts */
67static const u16 VAUX1_VSEL_table[] = {
68 1800, 2500, 2800, 2850,
69};
70
71/* supported VAUX2 voltages in millivolts */
72static const u16 VAUX2_VSEL_table[] = {
73 1800, 2800, 2900, 3300,
74};
75
76/* supported VAUX33 voltages in millivolts */
77static const u16 VAUX33_VSEL_table[] = {
78 1800, 2000, 2800, 3300,
79};
80
81/* supported VMMC voltages in millivolts */
82static const u16 VMMC_VSEL_table[] = {
83 1800, 2800, 3000, 3300,
84};
85
86struct tps_info {
87 const char *name;
88 unsigned min_uV;
89 unsigned max_uV;
90 u8 n_voltages;
91 const u16 *voltage_table;
92 int enable_time_us;
93};
94
95static struct tps_info tps65910_regs[] = {
96 {
97 .name = "vrtc",
98 .enable_time_us = 2200,
99 },
100 {
101 .name = "vio",
102 .min_uV = 1500000,
103 .max_uV = 3300000,
104 .n_voltages = ARRAY_SIZE(VIO_VSEL_table),
105 .voltage_table = VIO_VSEL_table,
106 .enable_time_us = 350,
107 },
108 {
109 .name = "vdd1",
110 .min_uV = 600000,
111 .max_uV = 4500000,
112 .enable_time_us = 350,
113 },
114 {
115 .name = "vdd2",
116 .min_uV = 600000,
117 .max_uV = 4500000,
118 .enable_time_us = 350,
119 },
120 {
121 .name = "vdd3",
122 .min_uV = 5000000,
123 .max_uV = 5000000,
124 .n_voltages = ARRAY_SIZE(VDD3_VSEL_table),
125 .voltage_table = VDD3_VSEL_table,
126 .enable_time_us = 200,
127 },
128 {
129 .name = "vdig1",
130 .min_uV = 1200000,
131 .max_uV = 2700000,
132 .n_voltages = ARRAY_SIZE(VDIG1_VSEL_table),
133 .voltage_table = VDIG1_VSEL_table,
134 .enable_time_us = 100,
135 },
136 {
137 .name = "vdig2",
138 .min_uV = 1000000,
139 .max_uV = 1800000,
140 .n_voltages = ARRAY_SIZE(VDIG2_VSEL_table),
141 .voltage_table = VDIG2_VSEL_table,
142 .enable_time_us = 100,
143 },
144 {
145 .name = "vpll",
146 .min_uV = 1000000,
147 .max_uV = 2500000,
148 .n_voltages = ARRAY_SIZE(VPLL_VSEL_table),
149 .voltage_table = VPLL_VSEL_table,
150 .enable_time_us = 100,
151 },
152 {
153 .name = "vdac",
154 .min_uV = 1800000,
155 .max_uV = 2850000,
156 .n_voltages = ARRAY_SIZE(VDAC_VSEL_table),
157 .voltage_table = VDAC_VSEL_table,
158 .enable_time_us = 100,
159 },
160 {
161 .name = "vaux1",
162 .min_uV = 1800000,
163 .max_uV = 2850000,
164 .n_voltages = ARRAY_SIZE(VAUX1_VSEL_table),
165 .voltage_table = VAUX1_VSEL_table,
166 .enable_time_us = 100,
167 },
168 {
169 .name = "vaux2",
170 .min_uV = 1800000,
171 .max_uV = 3300000,
172 .n_voltages = ARRAY_SIZE(VAUX2_VSEL_table),
173 .voltage_table = VAUX2_VSEL_table,
174 .enable_time_us = 100,
175 },
176 {
177 .name = "vaux33",
178 .min_uV = 1800000,
179 .max_uV = 3300000,
180 .n_voltages = ARRAY_SIZE(VAUX33_VSEL_table),
181 .voltage_table = VAUX33_VSEL_table,
182 .enable_time_us = 100,
183 },
184 {
185 .name = "vmmc",
186 .min_uV = 1800000,
187 .max_uV = 3300000,
188 .n_voltages = ARRAY_SIZE(VMMC_VSEL_table),
189 .voltage_table = VMMC_VSEL_table,
190 .enable_time_us = 100,
191 },
192};
193
194static struct tps_info tps65911_regs[] = {
195 {
196 .name = "vrtc",
197 .enable_time_us = 2200,
198 },
199 {
200 .name = "vio",
201 .min_uV = 1500000,
202 .max_uV = 3300000,
203 .n_voltages = ARRAY_SIZE(VIO_VSEL_table),
204 .voltage_table = VIO_VSEL_table,
205 .enable_time_us = 350,
206 },
207 {
208 .name = "vdd1",
209 .min_uV = 600000,
210 .max_uV = 4500000,
211 .n_voltages = 73,
212 .enable_time_us = 350,
213 },
214 {
215 .name = "vdd2",
216 .min_uV = 600000,
217 .max_uV = 4500000,
218 .n_voltages = 73,
219 .enable_time_us = 350,
220 },
221 {
222 .name = "vddctrl",
223 .min_uV = 600000,
224 .max_uV = 1400000,
225 .n_voltages = 65,
226 .enable_time_us = 900,
227 },
228 {
229 .name = "ldo1",
230 .min_uV = 1000000,
231 .max_uV = 3300000,
232 .n_voltages = 47,
233 .enable_time_us = 420,
234 },
235 {
236 .name = "ldo2",
237 .min_uV = 1000000,
238 .max_uV = 3300000,
239 .n_voltages = 47,
240 .enable_time_us = 420,
241 },
242 {
243 .name = "ldo3",
244 .min_uV = 1000000,
245 .max_uV = 3300000,
246 .n_voltages = 24,
247 .enable_time_us = 230,
248 },
249 {
250 .name = "ldo4",
251 .min_uV = 1000000,
252 .max_uV = 3300000,
253 .n_voltages = 47,
254 .enable_time_us = 230,
255 },
256 {
257 .name = "ldo5",
258 .min_uV = 1000000,
259 .max_uV = 3300000,
260 .n_voltages = 24,
261 .enable_time_us = 230,
262 },
263 {
264 .name = "ldo6",
265 .min_uV = 1000000,
266 .max_uV = 3300000,
267 .n_voltages = 24,
268 .enable_time_us = 230,
269 },
270 {
271 .name = "ldo7",
272 .min_uV = 1000000,
273 .max_uV = 3300000,
274 .n_voltages = 24,
275 .enable_time_us = 230,
276 },
277 {
278 .name = "ldo8",
279 .min_uV = 1000000,
280 .max_uV = 3300000,
281 .n_voltages = 24,
282 .enable_time_us = 230,
283 },
284};
285
286#define EXT_CONTROL_REG_BITS(id, regs_offs, bits) (((regs_offs) << 8) | (bits))
287static unsigned int tps65910_ext_sleep_control[] = {
288 0,
289 EXT_CONTROL_REG_BITS(VIO, 1, 0),
290 EXT_CONTROL_REG_BITS(VDD1, 1, 1),
291 EXT_CONTROL_REG_BITS(VDD2, 1, 2),
292 EXT_CONTROL_REG_BITS(VDD3, 1, 3),
293 EXT_CONTROL_REG_BITS(VDIG1, 0, 1),
294 EXT_CONTROL_REG_BITS(VDIG2, 0, 2),
295 EXT_CONTROL_REG_BITS(VPLL, 0, 6),
296 EXT_CONTROL_REG_BITS(VDAC, 0, 7),
297 EXT_CONTROL_REG_BITS(VAUX1, 0, 3),
298 EXT_CONTROL_REG_BITS(VAUX2, 0, 4),
299 EXT_CONTROL_REG_BITS(VAUX33, 0, 5),
300 EXT_CONTROL_REG_BITS(VMMC, 0, 0),
301};
302
303static unsigned int tps65911_ext_sleep_control[] = {
304 0,
305 EXT_CONTROL_REG_BITS(VIO, 1, 0),
306 EXT_CONTROL_REG_BITS(VDD1, 1, 1),
307 EXT_CONTROL_REG_BITS(VDD2, 1, 2),
308 EXT_CONTROL_REG_BITS(VDDCTRL, 1, 3),
309 EXT_CONTROL_REG_BITS(LDO1, 0, 1),
310 EXT_CONTROL_REG_BITS(LDO2, 0, 2),
311 EXT_CONTROL_REG_BITS(LDO3, 0, 7),
312 EXT_CONTROL_REG_BITS(LDO4, 0, 6),
313 EXT_CONTROL_REG_BITS(LDO5, 0, 3),
314 EXT_CONTROL_REG_BITS(LDO6, 0, 0),
315 EXT_CONTROL_REG_BITS(LDO7, 0, 5),
316 EXT_CONTROL_REG_BITS(LDO8, 0, 4),
317};
318
319struct tps65910_reg {
320 struct regulator_desc *desc;
321 struct tps65910 *mfd;
322 struct regulator_dev **rdev;
323 struct tps_info **info;
324 struct mutex mutex;
325 int num_regulators;
326 int mode;
327 int (*get_ctrl_reg)(int);
328 unsigned int *ext_sleep_control;
329 unsigned int board_ext_control[TPS65910_NUM_REGS];
330};
331
332static inline int tps65910_read(struct tps65910_reg *pmic, u8 reg)
333{
334 unsigned int val;
335 int err;
336
337 err = tps65910_reg_read(pmic->mfd, reg, &val);
338 if (err)
339 return err;
340
341 return val;
342}
343
344static int tps65910_modify_bits(struct tps65910_reg *pmic, u8 reg,
345 u8 set_mask, u8 clear_mask)
346{
347 int err, data;
348
349 mutex_lock(&pmic->mutex);
350
351 data = tps65910_read(pmic, reg);
352 if (data < 0) {
353 dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
354 err = data;
355 goto out;
356 }
357
358 data &= ~clear_mask;
359 data |= set_mask;
360 err = tps65910_reg_write(pmic->mfd, reg, data);
361 if (err)
362 dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
363
364out:
365 mutex_unlock(&pmic->mutex);
366 return err;
367}
368
369static int tps65910_reg_read_locked(struct tps65910_reg *pmic, u8 reg)
370{
371 int data;
372
373 mutex_lock(&pmic->mutex);
374
375 data = tps65910_read(pmic, reg);
376 if (data < 0)
377 dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
378
379 mutex_unlock(&pmic->mutex);
380 return data;
381}
382
383static int tps65910_reg_write_locked(struct tps65910_reg *pmic, u8 reg, u8 val)
384{
385 int err;
386
387 mutex_lock(&pmic->mutex);
388
389 err = tps65910_reg_write(pmic->mfd, reg, val);
390 if (err < 0)
391 dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
392
393 mutex_unlock(&pmic->mutex);
394 return err;
395}
396
397static int tps65910_get_ctrl_register(int id)
398{
399 switch (id) {
400 case TPS65910_REG_VRTC:
401 return TPS65910_VRTC;
402 case TPS65910_REG_VIO:
403 return TPS65910_VIO;
404 case TPS65910_REG_VDD1:
405 return TPS65910_VDD1;
406 case TPS65910_REG_VDD2:
407 return TPS65910_VDD2;
408 case TPS65910_REG_VDD3:
409 return TPS65910_VDD3;
410 case TPS65910_REG_VDIG1:
411 return TPS65910_VDIG1;
412 case TPS65910_REG_VDIG2:
413 return TPS65910_VDIG2;
414 case TPS65910_REG_VPLL:
415 return TPS65910_VPLL;
416 case TPS65910_REG_VDAC:
417 return TPS65910_VDAC;
418 case TPS65910_REG_VAUX1:
419 return TPS65910_VAUX1;
420 case TPS65910_REG_VAUX2:
421 return TPS65910_VAUX2;
422 case TPS65910_REG_VAUX33:
423 return TPS65910_VAUX33;
424 case TPS65910_REG_VMMC:
425 return TPS65910_VMMC;
426 default:
427 return -EINVAL;
428 }
429}
430
431static int tps65911_get_ctrl_register(int id)
432{
433 switch (id) {
434 case TPS65910_REG_VRTC:
435 return TPS65910_VRTC;
436 case TPS65910_REG_VIO:
437 return TPS65910_VIO;
438 case TPS65910_REG_VDD1:
439 return TPS65910_VDD1;
440 case TPS65910_REG_VDD2:
441 return TPS65910_VDD2;
442 case TPS65911_REG_VDDCTRL:
443 return TPS65911_VDDCTRL;
444 case TPS65911_REG_LDO1:
445 return TPS65911_LDO1;
446 case TPS65911_REG_LDO2:
447 return TPS65911_LDO2;
448 case TPS65911_REG_LDO3:
449 return TPS65911_LDO3;
450 case TPS65911_REG_LDO4:
451 return TPS65911_LDO4;
452 case TPS65911_REG_LDO5:
453 return TPS65911_LDO5;
454 case TPS65911_REG_LDO6:
455 return TPS65911_LDO6;
456 case TPS65911_REG_LDO7:
457 return TPS65911_LDO7;
458 case TPS65911_REG_LDO8:
459 return TPS65911_LDO8;
460 default:
461 return -EINVAL;
462 }
463}
464
465static int tps65910_enable_time(struct regulator_dev *dev)
466{
467 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
468 int id = rdev_get_id(dev);
469 return pmic->info[id]->enable_time_us;
470}
471
472static int tps65910_set_mode(struct regulator_dev *dev, unsigned int mode)
473{
474 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
475 struct tps65910 *mfd = pmic->mfd;
476 int reg, value, id = rdev_get_id(dev);
477
478 reg = pmic->get_ctrl_reg(id);
479 if (reg < 0)
480 return reg;
481
482 switch (mode) {
483 case REGULATOR_MODE_NORMAL:
484 return tps65910_modify_bits(pmic, reg, LDO_ST_ON_BIT,
485 LDO_ST_MODE_BIT);
486 case REGULATOR_MODE_IDLE:
487 value = LDO_ST_ON_BIT | LDO_ST_MODE_BIT;
488 return tps65910_reg_set_bits(mfd, reg, value);
489 case REGULATOR_MODE_STANDBY:
490 return tps65910_reg_clear_bits(mfd, reg, LDO_ST_ON_BIT);
491 }
492
493 return -EINVAL;
494}
495
496static unsigned int tps65910_get_mode(struct regulator_dev *dev)
497{
498 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
499 int reg, value, id = rdev_get_id(dev);
500
501 reg = pmic->get_ctrl_reg(id);
502 if (reg < 0)
503 return reg;
504
505 value = tps65910_reg_read_locked(pmic, reg);
506 if (value < 0)
507 return value;
508
509 if (!(value & LDO_ST_ON_BIT))
510 return REGULATOR_MODE_STANDBY;
511 else if (value & LDO_ST_MODE_BIT)
512 return REGULATOR_MODE_IDLE;
513 else
514 return REGULATOR_MODE_NORMAL;
515}
516
517static int tps65910_get_voltage_dcdc_sel(struct regulator_dev *dev)
518{
519 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
520 int id = rdev_get_id(dev);
521 int opvsel = 0, srvsel = 0, vselmax = 0, mult = 0, sr = 0;
522
523 switch (id) {
524 case TPS65910_REG_VDD1:
525 opvsel = tps65910_reg_read_locked(pmic, TPS65910_VDD1_OP);
526 mult = tps65910_reg_read_locked(pmic, TPS65910_VDD1);
527 mult = (mult & VDD1_VGAIN_SEL_MASK) >> VDD1_VGAIN_SEL_SHIFT;
528 srvsel = tps65910_reg_read_locked(pmic, TPS65910_VDD1_SR);
529 sr = opvsel & VDD1_OP_CMD_MASK;
530 opvsel &= VDD1_OP_SEL_MASK;
531 srvsel &= VDD1_SR_SEL_MASK;
532 vselmax = 75;
533 break;
534 case TPS65910_REG_VDD2:
535 opvsel = tps65910_reg_read_locked(pmic, TPS65910_VDD2_OP);
536 mult = tps65910_reg_read_locked(pmic, TPS65910_VDD2);
537 mult = (mult & VDD2_VGAIN_SEL_MASK) >> VDD2_VGAIN_SEL_SHIFT;
538 srvsel = tps65910_reg_read_locked(pmic, TPS65910_VDD2_SR);
539 sr = opvsel & VDD2_OP_CMD_MASK;
540 opvsel &= VDD2_OP_SEL_MASK;
541 srvsel &= VDD2_SR_SEL_MASK;
542 vselmax = 75;
543 break;
544 case TPS65911_REG_VDDCTRL:
545 opvsel = tps65910_reg_read_locked(pmic, TPS65911_VDDCTRL_OP);
546 srvsel = tps65910_reg_read_locked(pmic, TPS65911_VDDCTRL_SR);
547 sr = opvsel & VDDCTRL_OP_CMD_MASK;
548 opvsel &= VDDCTRL_OP_SEL_MASK;
549 srvsel &= VDDCTRL_SR_SEL_MASK;
550 vselmax = 64;
551 break;
552 }
553
554 /* multiplier 0 == 1 but 2,3 normal */
555 if (!mult)
556 mult=1;
557
558 if (sr) {
559 /* normalise to valid range */
560 if (srvsel < 3)
561 srvsel = 3;
562 if (srvsel > vselmax)
563 srvsel = vselmax;
564 return srvsel - 3;
565 } else {
566
567 /* normalise to valid range*/
568 if (opvsel < 3)
569 opvsel = 3;
570 if (opvsel > vselmax)
571 opvsel = vselmax;
572 return opvsel - 3;
573 }
574 return -EINVAL;
575}
576
577static int tps65910_get_voltage_sel(struct regulator_dev *dev)
578{
579 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
580 int reg, value, id = rdev_get_id(dev);
581
582 reg = pmic->get_ctrl_reg(id);
583 if (reg < 0)
584 return reg;
585
586 value = tps65910_reg_read_locked(pmic, reg);
587 if (value < 0)
588 return value;
589
590 switch (id) {
591 case TPS65910_REG_VIO:
592 case TPS65910_REG_VDIG1:
593 case TPS65910_REG_VDIG2:
594 case TPS65910_REG_VPLL:
595 case TPS65910_REG_VDAC:
596 case TPS65910_REG_VAUX1:
597 case TPS65910_REG_VAUX2:
598 case TPS65910_REG_VAUX33:
599 case TPS65910_REG_VMMC:
600 value &= LDO_SEL_MASK;
601 value >>= LDO_SEL_SHIFT;
602 break;
603 default:
604 return -EINVAL;
605 }
606
607 return value;
608}
609
610static int tps65910_get_voltage_vdd3(struct regulator_dev *dev)
611{
612 return 5 * 1000 * 1000;
613}
614
615static int tps65911_get_voltage_sel(struct regulator_dev *dev)
616{
617 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
618 int id = rdev_get_id(dev);
619 u8 value, reg;
620
621 reg = pmic->get_ctrl_reg(id);
622
623 value = tps65910_reg_read_locked(pmic, reg);
624
625 switch (id) {
626 case TPS65911_REG_LDO1:
627 case TPS65911_REG_LDO2:
628 case TPS65911_REG_LDO4:
629 value &= LDO1_SEL_MASK;
630 value >>= LDO_SEL_SHIFT;
631 break;
632 case TPS65911_REG_LDO3:
633 case TPS65911_REG_LDO5:
634 case TPS65911_REG_LDO6:
635 case TPS65911_REG_LDO7:
636 case TPS65911_REG_LDO8:
637 value &= LDO3_SEL_MASK;
638 value >>= LDO_SEL_SHIFT;
639 break;
640 case TPS65910_REG_VIO:
641 value &= LDO_SEL_MASK;
642 value >>= LDO_SEL_SHIFT;
643 break;
644 default:
645 return -EINVAL;
646 }
647
648 return value;
649}
650
651static int tps65910_set_voltage_dcdc_sel(struct regulator_dev *dev,
652 unsigned selector)
653{
654 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
655 int id = rdev_get_id(dev), vsel;
656 int dcdc_mult = 0;
657
658 switch (id) {
659 case TPS65910_REG_VDD1:
660 dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
661 if (dcdc_mult == 1)
662 dcdc_mult--;
663 vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
664
665 tps65910_modify_bits(pmic, TPS65910_VDD1,
666 (dcdc_mult << VDD1_VGAIN_SEL_SHIFT),
667 VDD1_VGAIN_SEL_MASK);
668 tps65910_reg_write_locked(pmic, TPS65910_VDD1_OP, vsel);
669 break;
670 case TPS65910_REG_VDD2:
671 dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
672 if (dcdc_mult == 1)
673 dcdc_mult--;
674 vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
675
676 tps65910_modify_bits(pmic, TPS65910_VDD2,
677 (dcdc_mult << VDD2_VGAIN_SEL_SHIFT),
678 VDD1_VGAIN_SEL_MASK);
679 tps65910_reg_write_locked(pmic, TPS65910_VDD2_OP, vsel);
680 break;
681 case TPS65911_REG_VDDCTRL:
682 vsel = selector + 3;
683 tps65910_reg_write_locked(pmic, TPS65911_VDDCTRL_OP, vsel);
684 }
685
686 return 0;
687}
688
689static int tps65910_set_voltage_sel(struct regulator_dev *dev,
690 unsigned selector)
691{
692 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
693 int reg, id = rdev_get_id(dev);
694
695 reg = pmic->get_ctrl_reg(id);
696 if (reg < 0)
697 return reg;
698
699 switch (id) {
700 case TPS65910_REG_VIO:
701 case TPS65910_REG_VDIG1:
702 case TPS65910_REG_VDIG2:
703 case TPS65910_REG_VPLL:
704 case TPS65910_REG_VDAC:
705 case TPS65910_REG_VAUX1:
706 case TPS65910_REG_VAUX2:
707 case TPS65910_REG_VAUX33:
708 case TPS65910_REG_VMMC:
709 return tps65910_modify_bits(pmic, reg,
710 (selector << LDO_SEL_SHIFT), LDO_SEL_MASK);
711 }
712
713 return -EINVAL;
714}
715
716static int tps65911_set_voltage_sel(struct regulator_dev *dev,
717 unsigned selector)
718{
719 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
720 int reg, id = rdev_get_id(dev);
721
722 reg = pmic->get_ctrl_reg(id);
723 if (reg < 0)
724 return reg;
725
726 switch (id) {
727 case TPS65911_REG_LDO1:
728 case TPS65911_REG_LDO2:
729 case TPS65911_REG_LDO4:
730 return tps65910_modify_bits(pmic, reg,
731 (selector << LDO_SEL_SHIFT), LDO1_SEL_MASK);
732 case TPS65911_REG_LDO3:
733 case TPS65911_REG_LDO5:
734 case TPS65911_REG_LDO6:
735 case TPS65911_REG_LDO7:
736 case TPS65911_REG_LDO8:
737 return tps65910_modify_bits(pmic, reg,
738 (selector << LDO_SEL_SHIFT), LDO3_SEL_MASK);
739 case TPS65910_REG_VIO:
740 return tps65910_modify_bits(pmic, reg,
741 (selector << LDO_SEL_SHIFT), LDO_SEL_MASK);
742 }
743
744 return -EINVAL;
745}
746
747
748static int tps65910_list_voltage_dcdc(struct regulator_dev *dev,
749 unsigned selector)
750{
751 int volt, mult = 1, id = rdev_get_id(dev);
752
753 switch (id) {
754 case TPS65910_REG_VDD1:
755 case TPS65910_REG_VDD2:
756 mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
757 volt = VDD1_2_MIN_VOLT +
758 (selector % VDD1_2_NUM_VOLT_FINE) * VDD1_2_OFFSET;
759 break;
760 case TPS65911_REG_VDDCTRL:
761 volt = VDDCTRL_MIN_VOLT + (selector * VDDCTRL_OFFSET);
762 break;
763 default:
764 BUG();
765 return -EINVAL;
766 }
767
768 return volt * 100 * mult;
769}
770
771static int tps65910_list_voltage(struct regulator_dev *dev,
772 unsigned selector)
773{
774 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
775 int id = rdev_get_id(dev), voltage;
776
777 if (id < TPS65910_REG_VIO || id > TPS65910_REG_VMMC)
778 return -EINVAL;
779
780 if (selector >= pmic->info[id]->n_voltages)
781 return -EINVAL;
782 else
783 voltage = pmic->info[id]->voltage_table[selector] * 1000;
784
785 return voltage;
786}
787
788static int tps65911_list_voltage(struct regulator_dev *dev, unsigned selector)
789{
790 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
791 int step_mv = 0, id = rdev_get_id(dev);
792
793 switch(id) {
794 case TPS65911_REG_LDO1:
795 case TPS65911_REG_LDO2:
796 case TPS65911_REG_LDO4:
797 /* The first 5 values of the selector correspond to 1V */
798 if (selector < 5)
799 selector = 0;
800 else
801 selector -= 4;
802
803 step_mv = 50;
804 break;
805 case TPS65911_REG_LDO3:
806 case TPS65911_REG_LDO5:
807 case TPS65911_REG_LDO6:
808 case TPS65911_REG_LDO7:
809 case TPS65911_REG_LDO8:
810 /* The first 3 values of the selector correspond to 1V */
811 if (selector < 3)
812 selector = 0;
813 else
814 selector -= 2;
815
816 step_mv = 100;
817 break;
818 case TPS65910_REG_VIO:
819 return pmic->info[id]->voltage_table[selector] * 1000;
820 default:
821 return -EINVAL;
822 }
823
824 return (LDO_MIN_VOLT + selector * step_mv) * 1000;
825}
826
827static int tps65910_set_voltage_dcdc_time_sel(struct regulator_dev *dev,
828 unsigned int old_selector, unsigned int new_selector)
829{
830 int id = rdev_get_id(dev);
831 int old_volt, new_volt;
832
833 old_volt = tps65910_list_voltage_dcdc(dev, old_selector);
834 if (old_volt < 0)
835 return old_volt;
836
837 new_volt = tps65910_list_voltage_dcdc(dev, new_selector);
838 if (new_volt < 0)
839 return new_volt;
840
841 /* VDD1 and VDD2 are 12.5mV/us, VDDCTRL is 100mV/20us */
842 switch (id) {
843 case TPS65910_REG_VDD1:
844 case TPS65910_REG_VDD2:
845 return DIV_ROUND_UP(abs(old_volt - new_volt), 12500);
846 case TPS65911_REG_VDDCTRL:
847 return DIV_ROUND_UP(abs(old_volt - new_volt), 5000);
848 }
849 return -EINVAL;
850}
851
852/* Regulator ops (except VRTC) */
853static struct regulator_ops tps65910_ops_dcdc = {
854 .is_enabled = regulator_is_enabled_regmap,
855 .enable = regulator_enable_regmap,
856 .disable = regulator_disable_regmap,
857 .enable_time = tps65910_enable_time,
858 .set_mode = tps65910_set_mode,
859 .get_mode = tps65910_get_mode,
860 .get_voltage_sel = tps65910_get_voltage_dcdc_sel,
861 .set_voltage_sel = tps65910_set_voltage_dcdc_sel,
862 .set_voltage_time_sel = tps65910_set_voltage_dcdc_time_sel,
863 .list_voltage = tps65910_list_voltage_dcdc,
864};
865
866static struct regulator_ops tps65910_ops_vdd3 = {
867 .is_enabled = regulator_is_enabled_regmap,
868 .enable = regulator_enable_regmap,
869 .disable = regulator_disable_regmap,
870 .enable_time = tps65910_enable_time,
871 .set_mode = tps65910_set_mode,
872 .get_mode = tps65910_get_mode,
873 .get_voltage = tps65910_get_voltage_vdd3,
874 .list_voltage = tps65910_list_voltage,
875};
876
877static struct regulator_ops tps65910_ops = {
878 .is_enabled = regulator_is_enabled_regmap,
879 .enable = regulator_enable_regmap,
880 .disable = regulator_disable_regmap,
881 .enable_time = tps65910_enable_time,
882 .set_mode = tps65910_set_mode,
883 .get_mode = tps65910_get_mode,
884 .get_voltage_sel = tps65910_get_voltage_sel,
885 .set_voltage_sel = tps65910_set_voltage_sel,
886 .list_voltage = tps65910_list_voltage,
887};
888
889static struct regulator_ops tps65911_ops = {
890 .is_enabled = regulator_is_enabled_regmap,
891 .enable = regulator_enable_regmap,
892 .disable = regulator_disable_regmap,
893 .enable_time = tps65910_enable_time,
894 .set_mode = tps65910_set_mode,
895 .get_mode = tps65910_get_mode,
896 .get_voltage_sel = tps65911_get_voltage_sel,
897 .set_voltage_sel = tps65911_set_voltage_sel,
898 .list_voltage = tps65911_list_voltage,
899};
900
901static int tps65910_set_ext_sleep_config(struct tps65910_reg *pmic,
902 int id, int ext_sleep_config)
903{
904 struct tps65910 *mfd = pmic->mfd;
905 u8 regoffs = (pmic->ext_sleep_control[id] >> 8) & 0xFF;
906 u8 bit_pos = (1 << pmic->ext_sleep_control[id] & 0xFF);
907 int ret;
908
909 /*
910 * Regulator can not be control from multiple external input EN1, EN2
911 * and EN3 together.
912 */
913 if (ext_sleep_config & EXT_SLEEP_CONTROL) {
914 int en_count;
915 en_count = ((ext_sleep_config &
916 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1) != 0);
917 en_count += ((ext_sleep_config &
918 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2) != 0);
919 en_count += ((ext_sleep_config &
920 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3) != 0);
921 en_count += ((ext_sleep_config &
922 TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP) != 0);
923 if (en_count > 1) {
924 dev_err(mfd->dev,
925 "External sleep control flag is not proper\n");
926 return -EINVAL;
927 }
928 }
929
930 pmic->board_ext_control[id] = ext_sleep_config;
931
932 /* External EN1 control */
933 if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1)
934 ret = tps65910_reg_set_bits(mfd,
935 TPS65910_EN1_LDO_ASS + regoffs, bit_pos);
936 else
937 ret = tps65910_reg_clear_bits(mfd,
938 TPS65910_EN1_LDO_ASS + regoffs, bit_pos);
939 if (ret < 0) {
940 dev_err(mfd->dev,
941 "Error in configuring external control EN1\n");
942 return ret;
943 }
944
945 /* External EN2 control */
946 if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2)
947 ret = tps65910_reg_set_bits(mfd,
948 TPS65910_EN2_LDO_ASS + regoffs, bit_pos);
949 else
950 ret = tps65910_reg_clear_bits(mfd,
951 TPS65910_EN2_LDO_ASS + regoffs, bit_pos);
952 if (ret < 0) {
953 dev_err(mfd->dev,
954 "Error in configuring external control EN2\n");
955 return ret;
956 }
957
958 /* External EN3 control for TPS65910 LDO only */
959 if ((tps65910_chip_id(mfd) == TPS65910) &&
960 (id >= TPS65910_REG_VDIG1)) {
961 if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3)
962 ret = tps65910_reg_set_bits(mfd,
963 TPS65910_EN3_LDO_ASS + regoffs, bit_pos);
964 else
965 ret = tps65910_reg_clear_bits(mfd,
966 TPS65910_EN3_LDO_ASS + regoffs, bit_pos);
967 if (ret < 0) {
968 dev_err(mfd->dev,
969 "Error in configuring external control EN3\n");
970 return ret;
971 }
972 }
973
974 /* Return if no external control is selected */
975 if (!(ext_sleep_config & EXT_SLEEP_CONTROL)) {
976 /* Clear all sleep controls */
977 ret = tps65910_reg_clear_bits(mfd,
978 TPS65910_SLEEP_KEEP_LDO_ON + regoffs, bit_pos);
979 if (!ret)
980 ret = tps65910_reg_clear_bits(mfd,
981 TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
982 if (ret < 0)
983 dev_err(mfd->dev,
984 "Error in configuring SLEEP register\n");
985 return ret;
986 }
987
988 /*
989 * For regulator that has separate operational and sleep register make
990 * sure that operational is used and clear sleep register to turn
991 * regulator off when external control is inactive
992 */
993 if ((id == TPS65910_REG_VDD1) ||
994 (id == TPS65910_REG_VDD2) ||
995 ((id == TPS65911_REG_VDDCTRL) &&
996 (tps65910_chip_id(mfd) == TPS65911))) {
997 int op_reg_add = pmic->get_ctrl_reg(id) + 1;
998 int sr_reg_add = pmic->get_ctrl_reg(id) + 2;
999 int opvsel = tps65910_reg_read_locked(pmic, op_reg_add);
1000 int srvsel = tps65910_reg_read_locked(pmic, sr_reg_add);
1001 if (opvsel & VDD1_OP_CMD_MASK) {
1002 u8 reg_val = srvsel & VDD1_OP_SEL_MASK;
1003 ret = tps65910_reg_write_locked(pmic, op_reg_add,
1004 reg_val);
1005 if (ret < 0) {
1006 dev_err(mfd->dev,
1007 "Error in configuring op register\n");
1008 return ret;
1009 }
1010 }
1011 ret = tps65910_reg_write_locked(pmic, sr_reg_add, 0);
1012 if (ret < 0) {
1013 dev_err(mfd->dev, "Error in settting sr register\n");
1014 return ret;
1015 }
1016 }
1017
1018 ret = tps65910_reg_clear_bits(mfd,
1019 TPS65910_SLEEP_KEEP_LDO_ON + regoffs, bit_pos);
1020 if (!ret) {
1021 if (ext_sleep_config & TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP)
1022 ret = tps65910_reg_set_bits(mfd,
1023 TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
1024 else
1025 ret = tps65910_reg_clear_bits(mfd,
1026 TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
1027 }
1028 if (ret < 0)
1029 dev_err(mfd->dev,
1030 "Error in configuring SLEEP register\n");
1031
1032 return ret;
1033}
1034
1035#ifdef CONFIG_OF
1036
1037static struct of_regulator_match tps65910_matches[] = {
1038 { .name = "vrtc", .driver_data = (void *) &tps65910_regs[0] },
1039 { .name = "vio", .driver_data = (void *) &tps65910_regs[1] },
1040 { .name = "vdd1", .driver_data = (void *) &tps65910_regs[2] },
1041 { .name = "vdd2", .driver_data = (void *) &tps65910_regs[3] },
1042 { .name = "vdd3", .driver_data = (void *) &tps65910_regs[4] },
1043 { .name = "vdig1", .driver_data = (void *) &tps65910_regs[5] },
1044 { .name = "vdig2", .driver_data = (void *) &tps65910_regs[6] },
1045 { .name = "vpll", .driver_data = (void *) &tps65910_regs[7] },
1046 { .name = "vdac", .driver_data = (void *) &tps65910_regs[8] },
1047 { .name = "vaux1", .driver_data = (void *) &tps65910_regs[9] },
1048 { .name = "vaux2", .driver_data = (void *) &tps65910_regs[10] },
1049 { .name = "vaux33", .driver_data = (void *) &tps65910_regs[11] },
1050 { .name = "vmmc", .driver_data = (void *) &tps65910_regs[12] },
1051};
1052
1053static struct of_regulator_match tps65911_matches[] = {
1054 { .name = "vrtc", .driver_data = (void *) &tps65911_regs[0] },
1055 { .name = "vio", .driver_data = (void *) &tps65911_regs[1] },
1056 { .name = "vdd1", .driver_data = (void *) &tps65911_regs[2] },
1057 { .name = "vdd2", .driver_data = (void *) &tps65911_regs[3] },
1058 { .name = "vddctrl", .driver_data = (void *) &tps65911_regs[4] },
1059 { .name = "ldo1", .driver_data = (void *) &tps65911_regs[5] },
1060 { .name = "ldo2", .driver_data = (void *) &tps65911_regs[6] },
1061 { .name = "ldo3", .driver_data = (void *) &tps65911_regs[7] },
1062 { .name = "ldo4", .driver_data = (void *) &tps65911_regs[8] },
1063 { .name = "ldo5", .driver_data = (void *) &tps65911_regs[9] },
1064 { .name = "ldo6", .driver_data = (void *) &tps65911_regs[10] },
1065 { .name = "ldo7", .driver_data = (void *) &tps65911_regs[11] },
1066 { .name = "ldo8", .driver_data = (void *) &tps65911_regs[12] },
1067};
1068
1069static struct tps65910_board *tps65910_parse_dt_reg_data(
1070 struct platform_device *pdev,
1071 struct of_regulator_match **tps65910_reg_matches)
1072{
1073 struct tps65910_board *pmic_plat_data;
1074 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
1075 struct device_node *np = pdev->dev.parent->of_node;
1076 struct device_node *regulators;
1077 struct of_regulator_match *matches;
1078 unsigned int prop;
1079 int idx = 0, ret, count;
1080
1081 pmic_plat_data = devm_kzalloc(&pdev->dev, sizeof(*pmic_plat_data),
1082 GFP_KERNEL);
1083
1084 if (!pmic_plat_data) {
1085 dev_err(&pdev->dev, "Failure to alloc pdata for regulators.\n");
1086 return NULL;
1087 }
1088
1089 regulators = of_find_node_by_name(np, "regulators");
1090 if (!regulators) {
1091 dev_err(&pdev->dev, "regulator node not found\n");
1092 return NULL;
1093 }
1094
1095 switch (tps65910_chip_id(tps65910)) {
1096 case TPS65910:
1097 count = ARRAY_SIZE(tps65910_matches);
1098 matches = tps65910_matches;
1099 break;
1100 case TPS65911:
1101 count = ARRAY_SIZE(tps65911_matches);
1102 matches = tps65911_matches;
1103 break;
1104 default:
1105 dev_err(&pdev->dev, "Invalid tps chip version\n");
1106 return NULL;
1107 }
1108
1109 ret = of_regulator_match(pdev->dev.parent, regulators, matches, count);
1110 if (ret < 0) {
1111 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
1112 ret);
1113 return NULL;
1114 }
1115
1116 *tps65910_reg_matches = matches;
1117
1118 for (idx = 0; idx < count; idx++) {
1119 if (!matches[idx].init_data || !matches[idx].of_node)
1120 continue;
1121
1122 pmic_plat_data->tps65910_pmic_init_data[idx] =
1123 matches[idx].init_data;
1124
1125 ret = of_property_read_u32(matches[idx].of_node,
1126 "ti,regulator-ext-sleep-control", &prop);
1127 if (!ret)
1128 pmic_plat_data->regulator_ext_sleep_control[idx] = prop;
1129 }
1130
1131 return pmic_plat_data;
1132}
1133#else
1134static inline struct tps65910_board *tps65910_parse_dt_reg_data(
1135 struct platform_device *pdev,
1136 struct of_regulator_match **tps65910_reg_matches)
1137{
1138 *tps65910_reg_matches = NULL;
1139 return 0;
1140}
1141#endif
1142
1143static __devinit int tps65910_probe(struct platform_device *pdev)
1144{
1145 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
1146 struct regulator_config config = { };
1147 struct tps_info *info;
1148 struct regulator_init_data *reg_data;
1149 struct regulator_dev *rdev;
1150 struct tps65910_reg *pmic;
1151 struct tps65910_board *pmic_plat_data;
1152 struct of_regulator_match *tps65910_reg_matches = NULL;
1153 int i, err;
1154
1155 pmic_plat_data = dev_get_platdata(tps65910->dev);
1156 if (!pmic_plat_data && tps65910->dev->of_node)
1157 pmic_plat_data = tps65910_parse_dt_reg_data(pdev,
1158 &tps65910_reg_matches);
1159
1160 if (!pmic_plat_data) {
1161 dev_err(&pdev->dev, "Platform data not found\n");
1162 return -EINVAL;
1163 }
1164
1165 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
1166 if (!pmic) {
1167 dev_err(&pdev->dev, "Memory allocation failed for pmic\n");
1168 return -ENOMEM;
1169 }
1170
1171 mutex_init(&pmic->mutex);
1172 pmic->mfd = tps65910;
1173 platform_set_drvdata(pdev, pmic);
1174
1175 /* Give control of all register to control port */
1176 tps65910_reg_set_bits(pmic->mfd, TPS65910_DEVCTRL,
1177 DEVCTRL_SR_CTL_I2C_SEL_MASK);
1178
1179 switch(tps65910_chip_id(tps65910)) {
1180 case TPS65910:
1181 pmic->get_ctrl_reg = &tps65910_get_ctrl_register;
1182 pmic->num_regulators = ARRAY_SIZE(tps65910_regs);
1183 pmic->ext_sleep_control = tps65910_ext_sleep_control;
1184 info = tps65910_regs;
1185 break;
1186 case TPS65911:
1187 pmic->get_ctrl_reg = &tps65911_get_ctrl_register;
1188 pmic->num_regulators = ARRAY_SIZE(tps65911_regs);
1189 pmic->ext_sleep_control = tps65911_ext_sleep_control;
1190 info = tps65911_regs;
1191 break;
1192 default:
1193 dev_err(&pdev->dev, "Invalid tps chip version\n");
1194 return -ENODEV;
1195 }
1196
1197 pmic->desc = devm_kzalloc(&pdev->dev, pmic->num_regulators *
1198 sizeof(struct regulator_desc), GFP_KERNEL);
1199 if (!pmic->desc) {
1200 dev_err(&pdev->dev, "Memory alloc fails for desc\n");
1201 return -ENOMEM;
1202 }
1203
1204 pmic->info = devm_kzalloc(&pdev->dev, pmic->num_regulators *
1205 sizeof(struct tps_info *), GFP_KERNEL);
1206 if (!pmic->info) {
1207 dev_err(&pdev->dev, "Memory alloc fails for info\n");
1208 return -ENOMEM;
1209 }
1210
1211 pmic->rdev = devm_kzalloc(&pdev->dev, pmic->num_regulators *
1212 sizeof(struct regulator_dev *), GFP_KERNEL);
1213 if (!pmic->rdev) {
1214 dev_err(&pdev->dev, "Memory alloc fails for rdev\n");
1215 return -ENOMEM;
1216 }
1217
1218 for (i = 0; i < pmic->num_regulators && i < TPS65910_NUM_REGS;
1219 i++, info++) {
1220
1221 reg_data = pmic_plat_data->tps65910_pmic_init_data[i];
1222
1223 /* Regulator API handles empty constraints but not NULL
1224 * constraints */
1225 if (!reg_data)
1226 continue;
1227
1228 /* Register the regulators */
1229 pmic->info[i] = info;
1230
1231 pmic->desc[i].name = info->name;
1232 pmic->desc[i].id = i;
1233 pmic->desc[i].n_voltages = info->n_voltages;
1234
1235 if (i == TPS65910_REG_VDD1 || i == TPS65910_REG_VDD2) {
1236 pmic->desc[i].ops = &tps65910_ops_dcdc;
1237 pmic->desc[i].n_voltages = VDD1_2_NUM_VOLT_FINE *
1238 VDD1_2_NUM_VOLT_COARSE;
1239 } else if (i == TPS65910_REG_VDD3) {
1240 if (tps65910_chip_id(tps65910) == TPS65910)
1241 pmic->desc[i].ops = &tps65910_ops_vdd3;
1242 else
1243 pmic->desc[i].ops = &tps65910_ops_dcdc;
1244 } else {
1245 if (tps65910_chip_id(tps65910) == TPS65910)
1246 pmic->desc[i].ops = &tps65910_ops;
1247 else
1248 pmic->desc[i].ops = &tps65911_ops;
1249 }
1250
1251 err = tps65910_set_ext_sleep_config(pmic, i,
1252 pmic_plat_data->regulator_ext_sleep_control[i]);
1253 /*
1254 * Failing on regulator for configuring externally control
1255 * is not a serious issue, just throw warning.
1256 */
1257 if (err < 0)
1258 dev_warn(tps65910->dev,
1259 "Failed to initialise ext control config\n");
1260
1261 pmic->desc[i].type = REGULATOR_VOLTAGE;
1262 pmic->desc[i].owner = THIS_MODULE;
1263 pmic->desc[i].enable_reg = pmic->get_ctrl_reg(i);
1264 pmic->desc[i].enable_mask = TPS65910_SUPPLY_STATE_ENABLED;
1265
1266 config.dev = tps65910->dev;
1267 config.init_data = reg_data;
1268 config.driver_data = pmic;
1269 config.regmap = tps65910->regmap;
1270
1271 if (tps65910_reg_matches)
1272 config.of_node = tps65910_reg_matches[i].of_node;
1273
1274 rdev = regulator_register(&pmic->desc[i], &config);
1275 if (IS_ERR(rdev)) {
1276 dev_err(tps65910->dev,
1277 "failed to register %s regulator\n",
1278 pdev->name);
1279 err = PTR_ERR(rdev);
1280 goto err_unregister_regulator;
1281 }
1282
1283 /* Save regulator for cleanup */
1284 pmic->rdev[i] = rdev;
1285 }
1286 return 0;
1287
1288err_unregister_regulator:
1289 while (--i >= 0)
1290 regulator_unregister(pmic->rdev[i]);
1291 return err;
1292}
1293
1294static int __devexit tps65910_remove(struct platform_device *pdev)
1295{
1296 struct tps65910_reg *pmic = platform_get_drvdata(pdev);
1297 int i;
1298
1299 for (i = 0; i < pmic->num_regulators; i++)
1300 regulator_unregister(pmic->rdev[i]);
1301
1302 return 0;
1303}
1304
1305static void tps65910_shutdown(struct platform_device *pdev)
1306{
1307 struct tps65910_reg *pmic = platform_get_drvdata(pdev);
1308 int i;
1309
1310 /*
1311 * Before bootloader jumps to kernel, it makes sure that required
1312 * external control signals are in desired state so that given rails
1313 * can be configure accordingly.
1314 * If rails are configured to be controlled from external control
1315 * then before shutting down/rebooting the system, the external
1316 * control configuration need to be remove from the rails so that
1317 * its output will be available as per register programming even
1318 * if external controls are removed. This is require when the POR
1319 * value of the control signals are not in active state and before
1320 * bootloader initializes it, the system requires the rail output
1321 * to be active for booting.
1322 */
1323 for (i = 0; i < pmic->num_regulators; i++) {
1324 int err;
1325 if (!pmic->rdev[i])
1326 continue;
1327
1328 err = tps65910_set_ext_sleep_config(pmic, i, 0);
1329 if (err < 0)
1330 dev_err(&pdev->dev,
1331 "Error in clearing external control\n");
1332 }
1333}
1334
1335static struct platform_driver tps65910_driver = {
1336 .driver = {
1337 .name = "tps65910-pmic",
1338 .owner = THIS_MODULE,
1339 },
1340 .probe = tps65910_probe,
1341 .remove = __devexit_p(tps65910_remove),
1342 .shutdown = tps65910_shutdown,
1343};
1344
1345static int __init tps65910_init(void)
1346{
1347 return platform_driver_register(&tps65910_driver);
1348}
1349subsys_initcall(tps65910_init);
1350
1351static void __exit tps65910_cleanup(void)
1352{
1353 platform_driver_unregister(&tps65910_driver);
1354}
1355module_exit(tps65910_cleanup);
1356
1357MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1358MODULE_DESCRIPTION("TPS65910/TPS65911 voltage regulator driver");
1359MODULE_LICENSE("GPL v2");
1360MODULE_ALIAS("platform:tps65910-pmic");