a623574c94e19702618d547ae6dc37cf5b3194b1
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 */
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/delay.h>
24 #include <linux/slab.h>
25 #include <linux/gpio.h>
26 #include <linux/mfd/tps65910.h>
28 #define TPS65910_SUPPLY_STATE_ENABLED 0x1
30 /* supported VIO voltages in milivolts */
31 static const u16 VIO_VSEL_table[] = {
32 1500, 1800, 2500, 3300,
33 };
35 /* VSEL tables for TPS65910 specific LDOs and dcdc's */
37 /* supported VDD3 voltages in milivolts */
38 static const u16 VDD3_VSEL_table[] = {
39 5000,
40 };
42 /* supported VDIG1 voltages in milivolts */
43 static const u16 VDIG1_VSEL_table[] = {
44 1200, 1500, 1800, 2700,
45 };
47 /* supported VDIG2 voltages in milivolts */
48 static const u16 VDIG2_VSEL_table[] = {
49 1000, 1100, 1200, 1800,
50 };
52 /* supported VPLL voltages in milivolts */
53 static const u16 VPLL_VSEL_table[] = {
54 1000, 1100, 1800, 2500,
55 };
57 /* supported VDAC voltages in milivolts */
58 static const u16 VDAC_VSEL_table[] = {
59 1800, 2600, 2800, 2850,
60 };
62 /* supported VAUX1 voltages in milivolts */
63 static const u16 VAUX1_VSEL_table[] = {
64 1800, 2500, 2800, 2850,
65 };
67 /* supported VAUX2 voltages in milivolts */
68 static const u16 VAUX2_VSEL_table[] = {
69 1800, 2800, 2900, 3300,
70 };
72 /* supported VAUX33 voltages in milivolts */
73 static const u16 VAUX33_VSEL_table[] = {
74 1800, 2000, 2800, 3300,
75 };
77 /* supported VMMC voltages in milivolts */
78 static const u16 VMMC_VSEL_table[] = {
79 1800, 2800, 3000, 3300,
80 };
82 struct tps_info {
83 const char *name;
84 unsigned min_uV;
85 unsigned max_uV;
86 u8 table_len;
87 const u16 *table;
88 };
90 static struct tps_info tps65910_regs[] = {
91 {
92 .name = "VRTC",
93 },
94 {
95 .name = "VIO",
96 .min_uV = 1500000,
97 .max_uV = 3300000,
98 .table_len = ARRAY_SIZE(VIO_VSEL_table),
99 .table = VIO_VSEL_table,
100 },
101 {
102 .name = "VDD1",
103 .min_uV = 600000,
104 .max_uV = 4500000,
105 },
106 {
107 .name = "VDD2",
108 .min_uV = 600000,
109 .max_uV = 4500000,
110 },
111 {
112 .name = "VDD3",
113 .min_uV = 5000000,
114 .max_uV = 5000000,
115 .table_len = ARRAY_SIZE(VDD3_VSEL_table),
116 .table = VDD3_VSEL_table,
117 },
118 {
119 .name = "VDIG1",
120 .min_uV = 1200000,
121 .max_uV = 2700000,
122 .table_len = ARRAY_SIZE(VDIG1_VSEL_table),
123 .table = VDIG1_VSEL_table,
124 },
125 {
126 .name = "VDIG2",
127 .min_uV = 1000000,
128 .max_uV = 1800000,
129 .table_len = ARRAY_SIZE(VDIG2_VSEL_table),
130 .table = VDIG2_VSEL_table,
131 },
132 {
133 .name = "VPLL",
134 .min_uV = 1000000,
135 .max_uV = 2500000,
136 .table_len = ARRAY_SIZE(VPLL_VSEL_table),
137 .table = VPLL_VSEL_table,
138 },
139 {
140 .name = "VDAC",
141 .min_uV = 1800000,
142 .max_uV = 2850000,
143 .table_len = ARRAY_SIZE(VDAC_VSEL_table),
144 .table = VDAC_VSEL_table,
145 },
146 {
147 .name = "VAUX1",
148 .min_uV = 1800000,
149 .max_uV = 2850000,
150 .table_len = ARRAY_SIZE(VAUX1_VSEL_table),
151 .table = VAUX1_VSEL_table,
152 },
153 {
154 .name = "VAUX2",
155 .min_uV = 1800000,
156 .max_uV = 3300000,
157 .table_len = ARRAY_SIZE(VAUX2_VSEL_table),
158 .table = VAUX2_VSEL_table,
159 },
160 {
161 .name = "VAUX33",
162 .min_uV = 1800000,
163 .max_uV = 3300000,
164 .table_len = ARRAY_SIZE(VAUX33_VSEL_table),
165 .table = VAUX33_VSEL_table,
166 },
167 {
168 .name = "VMMC",
169 .min_uV = 1800000,
170 .max_uV = 3300000,
171 .table_len = ARRAY_SIZE(VMMC_VSEL_table),
172 .table = VMMC_VSEL_table,
173 },
174 };
176 static struct tps_info tps65911_regs[] = {
177 {
178 .name = "VIO",
179 .min_uV = 1500000,
180 .max_uV = 3300000,
181 .table_len = ARRAY_SIZE(VIO_VSEL_table),
182 .table = VIO_VSEL_table,
183 },
184 {
185 .name = "VDD1",
186 .min_uV = 600000,
187 .max_uV = 4500000,
188 },
189 {
190 .name = "VDD2",
191 .min_uV = 600000,
192 .max_uV = 4500000,
193 },
194 {
195 .name = "VDDCTRL",
196 .min_uV = 600000,
197 .max_uV = 1400000,
198 },
199 {
200 .name = "LDO1",
201 .min_uV = 1000000,
202 .max_uV = 3300000,
203 },
204 {
205 .name = "LDO2",
206 .min_uV = 1000000,
207 .max_uV = 3300000,
208 },
209 {
210 .name = "LDO3",
211 .min_uV = 1000000,
212 .max_uV = 3300000,
213 },
214 {
215 .name = "LDO4",
216 .min_uV = 1000000,
217 .max_uV = 3300000,
218 },
219 {
220 .name = "LDO5",
221 .min_uV = 1000000,
222 .max_uV = 3300000,
223 },
224 {
225 .name = "LDO6",
226 .min_uV = 1000000,
227 .max_uV = 3300000,
228 },
229 {
230 .name = "LDO7",
231 .min_uV = 1000000,
232 .max_uV = 3300000,
233 },
234 {
235 .name = "LDO8",
236 .min_uV = 1000000,
237 .max_uV = 3300000,
238 },
239 };
241 struct tps65910_reg {
242 struct regulator_desc *desc;
243 struct tps65910 *mfd;
244 struct regulator_dev **rdev;
245 struct tps_info **info;
246 struct mutex mutex;
247 int num_regulators;
248 int mode;
249 int (*get_ctrl_reg)(int);
250 };
252 static inline int tps65910_read(struct tps65910_reg *pmic, u8 reg)
253 {
254 u8 val;
255 int err;
257 err = pmic->mfd->read(pmic->mfd, reg, 1, &val);
258 if (err)
259 return err;
261 return val;
262 }
264 static inline int tps65910_write(struct tps65910_reg *pmic, u8 reg, u8 val)
265 {
266 return pmic->mfd->write(pmic->mfd, reg, 1, &val);
267 }
269 static int tps65910_modify_bits(struct tps65910_reg *pmic, u8 reg,
270 u8 set_mask, u8 clear_mask)
271 {
272 int err, data;
274 mutex_lock(&pmic->mutex);
276 data = tps65910_read(pmic, reg);
277 if (data < 0) {
278 dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
279 err = data;
280 goto out;
281 }
283 data &= ~clear_mask;
284 data |= set_mask;
285 err = tps65910_write(pmic, reg, data);
286 if (err)
287 dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
289 out:
290 mutex_unlock(&pmic->mutex);
291 return err;
292 }
294 static int tps65910_reg_read(struct tps65910_reg *pmic, u8 reg)
295 {
296 int data;
298 mutex_lock(&pmic->mutex);
300 data = tps65910_read(pmic, reg);
301 if (data < 0)
302 dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
304 mutex_unlock(&pmic->mutex);
305 return data;
306 }
308 static int tps65910_reg_write(struct tps65910_reg *pmic, u8 reg, u8 val)
309 {
310 int err;
312 mutex_lock(&pmic->mutex);
314 err = tps65910_write(pmic, reg, val);
315 if (err < 0)
316 dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
318 mutex_unlock(&pmic->mutex);
319 return err;
320 }
322 static int tps65910_get_ctrl_register(int id)
323 {
324 switch (id) {
325 case TPS65910_REG_VRTC:
326 return TPS65910_VRTC;
327 case TPS65910_REG_VIO:
328 return TPS65910_VIO;
329 case TPS65910_REG_VDD1:
330 return TPS65910_VDD1;
331 case TPS65910_REG_VDD2:
332 return TPS65910_VDD2;
333 case TPS65910_REG_VDD3:
334 return TPS65910_VDD3;
335 case TPS65910_REG_VDIG1:
336 return TPS65910_VDIG1;
337 case TPS65910_REG_VDIG2:
338 return TPS65910_VDIG2;
339 case TPS65910_REG_VPLL:
340 return TPS65910_VPLL;
341 case TPS65910_REG_VDAC:
342 return TPS65910_VDAC;
343 case TPS65910_REG_VAUX1:
344 return TPS65910_VAUX1;
345 case TPS65910_REG_VAUX2:
346 return TPS65910_VAUX2;
347 case TPS65910_REG_VAUX33:
348 return TPS65910_VAUX33;
349 case TPS65910_REG_VMMC:
350 return TPS65910_VMMC;
351 default:
352 return -EINVAL;
353 }
354 }
356 static 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 }
390 static int tps65910_is_enabled(struct regulator_dev *dev)
391 {
392 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
393 int reg, value, id = rdev_get_id(dev);
395 reg = pmic->get_ctrl_reg(id);
396 if (reg < 0)
397 return reg;
399 value = tps65910_reg_read(pmic, reg);
400 if (value < 0)
401 return value;
403 return value & TPS65910_SUPPLY_STATE_ENABLED;
404 }
406 static int tps65910_enable(struct regulator_dev *dev)
407 {
408 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
409 struct tps65910 *mfd = pmic->mfd;
410 int reg, id = rdev_get_id(dev);
412 reg = pmic->get_ctrl_reg(id);
413 if (reg < 0)
414 return reg;
416 return tps65910_set_bits(mfd, reg, TPS65910_SUPPLY_STATE_ENABLED);
417 }
419 static int tps65910_disable(struct regulator_dev *dev)
420 {
421 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
422 struct tps65910 *mfd = pmic->mfd;
423 int reg, id = rdev_get_id(dev);
425 reg = pmic->get_ctrl_reg(id);
426 if (reg < 0)
427 return reg;
429 return tps65910_clear_bits(mfd, reg, TPS65910_SUPPLY_STATE_ENABLED);
430 }
433 static int tps65910_set_mode(struct regulator_dev *dev, unsigned int mode)
434 {
435 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
436 struct tps65910 *mfd = pmic->mfd;
437 int reg, value, id = rdev_get_id(dev);
439 reg = pmic->get_ctrl_reg(id);
440 if (reg < 0)
441 return reg;
443 switch (mode) {
444 case REGULATOR_MODE_NORMAL:
445 return tps65910_modify_bits(pmic, reg, LDO_ST_ON_BIT,
446 LDO_ST_MODE_BIT);
447 case REGULATOR_MODE_IDLE:
448 value = LDO_ST_ON_BIT | LDO_ST_MODE_BIT;
449 return tps65910_set_bits(mfd, reg, value);
450 case REGULATOR_MODE_STANDBY:
451 return tps65910_clear_bits(mfd, reg, LDO_ST_ON_BIT);
452 }
454 return -EINVAL;
455 }
457 static unsigned int tps65910_get_mode(struct regulator_dev *dev)
458 {
459 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
460 int reg, value, id = rdev_get_id(dev);
462 reg = pmic->get_ctrl_reg(id);
463 if (reg < 0)
464 return reg;
466 value = tps65910_reg_read(pmic, reg);
467 if (value < 0)
468 return value;
470 if (value & LDO_ST_ON_BIT)
471 return REGULATOR_MODE_STANDBY;
472 else if (value & LDO_ST_MODE_BIT)
473 return REGULATOR_MODE_IDLE;
474 else
475 return REGULATOR_MODE_NORMAL;
476 }
478 static int tps65910_get_voltage_dcdc(struct regulator_dev *dev)
479 {
480 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
481 int id = rdev_get_id(dev), voltage = 0;
482 int opvsel = 0, srvsel = 0, vselmax = 0, mult = 0, sr = 0;
484 switch (id) {
485 case TPS65910_REG_VDD1:
486 opvsel = tps65910_reg_read(pmic, TPS65910_VDD1_OP);
487 mult = tps65910_reg_read(pmic, TPS65910_VDD1);
488 mult = (mult & VDD1_VGAIN_SEL_MASK) >> VDD1_VGAIN_SEL_SHIFT;
489 srvsel = tps65910_reg_read(pmic, TPS65910_VDD1_SR);
490 sr = opvsel & VDD1_OP_CMD_MASK;
491 opvsel &= VDD1_OP_SEL_MASK;
492 srvsel &= VDD1_SR_SEL_MASK;
493 vselmax = 75;
494 break;
495 case TPS65910_REG_VDD2:
496 opvsel = tps65910_reg_read(pmic, TPS65910_VDD2_OP);
497 mult = tps65910_reg_read(pmic, TPS65910_VDD2);
498 mult = (mult & VDD2_VGAIN_SEL_MASK) >> VDD2_VGAIN_SEL_SHIFT;
499 srvsel = tps65910_reg_read(pmic, TPS65910_VDD2_SR);
500 sr = opvsel & VDD2_OP_CMD_MASK;
501 opvsel &= VDD2_OP_SEL_MASK;
502 srvsel &= VDD2_SR_SEL_MASK;
503 vselmax = 75;
504 break;
505 case TPS65911_REG_VDDCTRL:
506 opvsel = tps65910_reg_read(pmic, TPS65911_VDDCTRL_OP);
507 srvsel = tps65910_reg_read(pmic, TPS65911_VDDCTRL_SR);
508 sr = opvsel & VDDCTRL_OP_CMD_MASK;
509 opvsel &= VDDCTRL_OP_SEL_MASK;
510 srvsel &= VDDCTRL_SR_SEL_MASK;
511 vselmax = 64;
512 break;
513 }
515 /* multiplier 0 == 1 but 2,3 normal */
516 if (!mult)
517 mult=1;
519 if (sr) {
520 /* normalise to valid range */
521 if (srvsel < 3)
522 srvsel = 3;
523 if (srvsel > vselmax)
524 srvsel = vselmax;
525 srvsel -= 3;
527 voltage = (srvsel * VDD1_2_OFFSET + VDD1_2_MIN_VOLT) * 100;
528 } else {
530 /* normalise to valid range*/
531 if (opvsel < 3)
532 opvsel = 3;
533 if (opvsel > vselmax)
534 opvsel = vselmax;
535 opvsel -= 3;
537 voltage = (opvsel * VDD1_2_OFFSET + VDD1_2_MIN_VOLT) * 100;
538 }
540 voltage *= mult;
542 return voltage;
543 }
545 static int tps65910_get_voltage(struct regulator_dev *dev)
546 {
547 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
548 int reg, value, id = rdev_get_id(dev), voltage = 0;
550 reg = pmic->get_ctrl_reg(id);
551 if (reg < 0)
552 return reg;
554 value = tps65910_reg_read(pmic, reg);
555 if (value < 0)
556 return value;
558 switch (id) {
559 case TPS65910_REG_VIO:
560 case TPS65910_REG_VDIG1:
561 case TPS65910_REG_VDIG2:
562 case TPS65910_REG_VPLL:
563 case TPS65910_REG_VDAC:
564 case TPS65910_REG_VAUX1:
565 case TPS65910_REG_VAUX2:
566 case TPS65910_REG_VAUX33:
567 case TPS65910_REG_VMMC:
568 value &= LDO_SEL_MASK;
569 value >>= LDO_SEL_SHIFT;
570 break;
571 default:
572 return -EINVAL;
573 }
575 voltage = pmic->info[id]->table[value] * 1000;
577 return voltage;
578 }
580 static int tps65910_get_voltage_vdd3(struct regulator_dev *dev)
581 {
582 return 5 * 1000 * 1000;
583 }
585 static int tps65911_get_voltage(struct regulator_dev *dev)
586 {
587 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
588 int step_mv, id = rdev_get_id(dev);
589 u8 value, reg;
591 reg = pmic->get_ctrl_reg(id);
593 value = tps65910_reg_read(pmic, reg);
595 switch (id) {
596 case TPS65911_REG_LDO1:
597 case TPS65911_REG_LDO2:
598 case TPS65911_REG_LDO4:
599 value &= LDO1_SEL_MASK;
600 value >>= LDO_SEL_SHIFT;
601 /* The first 5 values of the selector correspond to 1V */
602 if (value < 5)
603 value = 0;
604 else
605 value -= 4;
607 step_mv = 50;
608 break;
609 case TPS65911_REG_LDO3:
610 case TPS65911_REG_LDO5:
611 case TPS65911_REG_LDO6:
612 case TPS65911_REG_LDO7:
613 case TPS65911_REG_LDO8:
614 value &= LDO3_SEL_MASK;
615 value >>= LDO_SEL_SHIFT;
616 /* The first 3 values of the selector correspond to 1V */
617 if (value < 3)
618 value = 0;
619 else
620 value -= 2;
622 step_mv = 100;
623 break;
624 case TPS65910_REG_VIO:
625 return pmic->info[id]->table[value] * 1000;
626 break;
627 default:
628 return -EINVAL;
629 }
631 return (LDO_MIN_VOLT + value * step_mv) * 1000;
632 }
634 static int tps65910_set_voltage_dcdc(struct regulator_dev *dev,
635 unsigned selector)
636 {
637 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
638 int id = rdev_get_id(dev), vsel;
639 int dcdc_mult = 0;
641 switch (id) {
642 case TPS65910_REG_VDD1:
643 dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
644 if (dcdc_mult == 1)
645 dcdc_mult--;
646 vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
648 tps65910_modify_bits(pmic, TPS65910_VDD1,
649 (dcdc_mult << VDD1_VGAIN_SEL_SHIFT),
650 VDD1_VGAIN_SEL_MASK);
651 tps65910_reg_write(pmic, TPS65910_VDD1_OP, vsel);
652 break;
653 case TPS65910_REG_VDD2:
654 dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
655 if (dcdc_mult == 1)
656 dcdc_mult--;
657 vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
659 tps65910_modify_bits(pmic, TPS65910_VDD2,
660 (dcdc_mult << VDD2_VGAIN_SEL_SHIFT),
661 VDD1_VGAIN_SEL_MASK);
662 tps65910_reg_write(pmic, TPS65910_VDD2_OP, vsel);
663 break;
664 case TPS65911_REG_VDDCTRL:
665 vsel = selector;
666 tps65910_reg_write(pmic, TPS65911_VDDCTRL_OP, vsel);
667 }
669 return 0;
670 }
672 static int tps65910_set_voltage(struct regulator_dev *dev, unsigned selector)
673 {
674 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
675 int reg, id = rdev_get_id(dev);
677 reg = pmic->get_ctrl_reg(id);
678 if (reg < 0)
679 return reg;
681 switch (id) {
682 case TPS65910_REG_VIO:
683 case TPS65910_REG_VDIG1:
684 case TPS65910_REG_VDIG2:
685 case TPS65910_REG_VPLL:
686 case TPS65910_REG_VDAC:
687 case TPS65910_REG_VAUX1:
688 case TPS65910_REG_VAUX2:
689 case TPS65910_REG_VAUX33:
690 case TPS65910_REG_VMMC:
691 return tps65910_modify_bits(pmic, reg,
692 (selector << LDO_SEL_SHIFT), LDO_SEL_MASK);
693 }
695 return -EINVAL;
696 }
698 static int tps65911_set_voltage(struct regulator_dev *dev, unsigned selector)
699 {
700 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
701 int reg, id = rdev_get_id(dev);
703 reg = pmic->get_ctrl_reg(id);
704 if (reg < 0)
705 return reg;
707 switch (id) {
708 case TPS65911_REG_LDO1:
709 case TPS65911_REG_LDO2:
710 case TPS65911_REG_LDO4:
711 return tps65910_modify_bits(pmic, reg,
712 (selector << LDO_SEL_SHIFT), LDO1_SEL_MASK);
713 case TPS65911_REG_LDO3:
714 case TPS65911_REG_LDO5:
715 case TPS65911_REG_LDO6:
716 case TPS65911_REG_LDO7:
717 case TPS65911_REG_LDO8:
718 case TPS65910_REG_VIO:
719 return tps65910_modify_bits(pmic, reg,
720 (selector << LDO_SEL_SHIFT), LDO3_SEL_MASK);
721 }
723 return -EINVAL;
724 }
727 static int tps65910_list_voltage_dcdc(struct regulator_dev *dev,
728 unsigned selector)
729 {
730 int volt, mult = 1, id = rdev_get_id(dev);
732 switch (id) {
733 case TPS65910_REG_VDD1:
734 case TPS65910_REG_VDD2:
735 mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
736 volt = VDD1_2_MIN_VOLT +
737 (selector % VDD1_2_NUM_VOLT_FINE) * VDD1_2_OFFSET;
738 break;
739 case TPS65911_REG_VDDCTRL:
740 volt = VDDCTRL_MIN_VOLT + (selector * VDDCTRL_OFFSET);
741 break;
742 default:
743 BUG();
744 return -EINVAL;
745 }
747 return volt * 100 * mult;
748 }
750 static int tps65910_list_voltage(struct regulator_dev *dev,
751 unsigned selector)
752 {
753 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
754 int id = rdev_get_id(dev), voltage;
756 if (id < TPS65910_REG_VIO || id > TPS65910_REG_VMMC)
757 return -EINVAL;
759 if (selector >= pmic->info[id]->table_len)
760 return -EINVAL;
761 else
762 voltage = pmic->info[id]->table[selector] * 1000;
764 return voltage;
765 }
767 static int tps65911_list_voltage(struct regulator_dev *dev, unsigned selector)
768 {
769 struct tps65910_reg *pmic = rdev_get_drvdata(dev);
770 int step_mv = 0, id = rdev_get_id(dev);
772 switch(id) {
773 case TPS65911_REG_LDO1:
774 case TPS65911_REG_LDO2:
775 case TPS65911_REG_LDO4:
776 /* The first 5 values of the selector correspond to 1V */
777 if (selector < 5)
778 selector = 0;
779 else
780 selector -= 4;
782 step_mv = 50;
783 break;
784 case TPS65911_REG_LDO3:
785 case TPS65911_REG_LDO5:
786 case TPS65911_REG_LDO6:
787 case TPS65911_REG_LDO7:
788 case TPS65911_REG_LDO8:
789 /* The first 3 values of the selector correspond to 1V */
790 if (selector < 3)
791 selector = 0;
792 else
793 selector -= 2;
795 step_mv = 100;
796 break;
797 case TPS65910_REG_VIO:
798 return pmic->info[id]->table[selector] * 1000;
799 default:
800 return -EINVAL;
801 }
803 return (LDO_MIN_VOLT + selector * step_mv) * 1000;
804 }
806 /* Regulator ops (except VRTC) */
807 static struct regulator_ops tps65910_ops_dcdc = {
808 .is_enabled = tps65910_is_enabled,
809 .enable = tps65910_enable,
810 .disable = tps65910_disable,
811 .set_mode = tps65910_set_mode,
812 .get_mode = tps65910_get_mode,
813 .get_voltage = tps65910_get_voltage_dcdc,
814 .set_voltage_sel = tps65910_set_voltage_dcdc,
815 .list_voltage = tps65910_list_voltage_dcdc,
816 };
818 static struct regulator_ops tps65910_ops_vdd3 = {
819 .is_enabled = tps65910_is_enabled,
820 .enable = tps65910_enable,
821 .disable = tps65910_disable,
822 .set_mode = tps65910_set_mode,
823 .get_mode = tps65910_get_mode,
824 .get_voltage = tps65910_get_voltage_vdd3,
825 .list_voltage = tps65910_list_voltage,
826 };
828 static struct regulator_ops tps65910_ops = {
829 .is_enabled = tps65910_is_enabled,
830 .enable = tps65910_enable,
831 .disable = tps65910_disable,
832 .set_mode = tps65910_set_mode,
833 .get_mode = tps65910_get_mode,
834 .get_voltage = tps65910_get_voltage,
835 .set_voltage_sel = tps65910_set_voltage,
836 .list_voltage = tps65910_list_voltage,
837 };
839 static struct regulator_ops tps65911_ops = {
840 .is_enabled = tps65910_is_enabled,
841 .enable = tps65910_enable,
842 .disable = tps65910_disable,
843 .set_mode = tps65910_set_mode,
844 .get_mode = tps65910_get_mode,
845 .get_voltage = tps65911_get_voltage,
846 .set_voltage_sel = tps65911_set_voltage,
847 .list_voltage = tps65911_list_voltage,
848 };
850 static __devinit int tps65910_probe(struct platform_device *pdev)
851 {
852 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
853 struct tps_info *info;
854 struct regulator_init_data *reg_data;
855 struct regulator_dev *rdev;
856 struct tps65910_reg *pmic;
857 struct tps65910_board *pmic_plat_data;
858 int i, err;
860 pmic_plat_data = dev_get_platdata(tps65910->dev);
861 if (!pmic_plat_data)
862 return -EINVAL;
864 reg_data = pmic_plat_data->tps65910_pmic_init_data;
866 pmic = kzalloc(sizeof(*pmic), GFP_KERNEL);
867 if (!pmic)
868 return -ENOMEM;
870 mutex_init(&pmic->mutex);
871 pmic->mfd = tps65910;
872 platform_set_drvdata(pdev, pmic);
874 /* Give control of all register to control port */
875 tps65910_set_bits(pmic->mfd, TPS65910_DEVCTRL,
876 DEVCTRL_SR_CTL_I2C_SEL_MASK);
878 switch(tps65910_chip_id(tps65910)) {
879 case TPS65910:
880 pmic->get_ctrl_reg = &tps65910_get_ctrl_register;
881 pmic->num_regulators = ARRAY_SIZE(tps65910_regs);
882 info = tps65910_regs;
883 break;
884 case TPS65911:
885 pmic->get_ctrl_reg = &tps65911_get_ctrl_register;
886 pmic->num_regulators = ARRAY_SIZE(tps65911_regs);
887 info = tps65911_regs;
888 break;
889 default:
890 pr_err("Invalid tps chip version\n");
891 kfree(pmic);
892 return -ENODEV;
893 }
895 pmic->desc = kcalloc(pmic->num_regulators,
896 sizeof(struct regulator_desc), GFP_KERNEL);
897 if (!pmic->desc) {
898 err = -ENOMEM;
899 goto err_free_pmic;
900 }
902 pmic->info = kcalloc(pmic->num_regulators,
903 sizeof(struct tps_info *), GFP_KERNEL);
904 if (!pmic->info) {
905 err = -ENOMEM;
906 goto err_free_desc;
907 }
909 pmic->rdev = kcalloc(pmic->num_regulators,
910 sizeof(struct regulator_dev *), GFP_KERNEL);
911 if (!pmic->rdev) {
912 err = -ENOMEM;
913 goto err_free_info;
914 }
916 for (i = 0; i < pmic->num_regulators; i++, info++, reg_data++) {
917 /* Register the regulators */
918 pmic->info[i] = info;
920 pmic->desc[i].name = info->name;
921 pmic->desc[i].id = i;
922 pmic->desc[i].n_voltages = info->table_len;
924 if (i == TPS65910_REG_VDD1 || i == TPS65910_REG_VDD2) {
925 pmic->desc[i].ops = &tps65910_ops_dcdc;
926 pmic->desc[i].n_voltages = VDD1_2_NUM_VOLT_FINE *
927 VDD1_2_NUM_VOLT_COARSE;
928 } else if (i == TPS65910_REG_VDD3) {
929 if (tps65910_chip_id(tps65910) == TPS65910)
930 pmic->desc[i].ops = &tps65910_ops_vdd3;
931 else
932 pmic->desc[i].ops = &tps65910_ops_dcdc;
933 } else {
934 if (tps65910_chip_id(tps65910) == TPS65910)
935 pmic->desc[i].ops = &tps65910_ops;
936 else
937 pmic->desc[i].ops = &tps65911_ops;
938 }
940 pmic->desc[i].type = REGULATOR_VOLTAGE;
941 pmic->desc[i].owner = THIS_MODULE;
943 rdev = regulator_register(&pmic->desc[i],
944 tps65910->dev, reg_data, pmic);
945 if (IS_ERR(rdev)) {
946 dev_err(tps65910->dev,
947 "failed to register %s regulator\n",
948 pdev->name);
949 err = PTR_ERR(rdev);
950 goto err_unregister_regulator;
951 }
953 /* Save regulator for cleanup */
954 pmic->rdev[i] = rdev;
955 }
956 return 0;
958 err_unregister_regulator:
959 while (--i >= 0)
960 regulator_unregister(pmic->rdev[i]);
961 kfree(pmic->rdev);
962 err_free_info:
963 kfree(pmic->info);
964 err_free_desc:
965 kfree(pmic->desc);
966 err_free_pmic:
967 kfree(pmic);
968 return err;
969 }
971 static int __devexit tps65910_remove(struct platform_device *pdev)
972 {
973 struct tps65910_reg *pmic = platform_get_drvdata(pdev);
974 int i;
976 for (i = 0; i < pmic->num_regulators; i++)
977 regulator_unregister(pmic->rdev[i]);
979 kfree(pmic->rdev);
980 kfree(pmic->info);
981 kfree(pmic->desc);
982 kfree(pmic);
983 return 0;
984 }
986 static struct platform_driver tps65910_driver = {
987 .driver = {
988 .name = "tps65910-pmic",
989 .owner = THIS_MODULE,
990 },
991 .probe = tps65910_probe,
992 .remove = __devexit_p(tps65910_remove),
993 };
995 static int __init tps65910_init(void)
996 {
997 return platform_driver_register(&tps65910_driver);
998 }
999 subsys_initcall(tps65910_init);
1001 static void __exit tps65910_cleanup(void)
1002 {
1003 platform_driver_unregister(&tps65910_driver);
1004 }
1005 module_exit(tps65910_cleanup);
1007 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1008 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
1009 MODULE_LICENSE("GPL v2");
1010 MODULE_ALIAS("platform:tps65910-pmic");