befs: Validate length of long symbolic links.
[linux-2.6/kvm.git] / drivers / regulator / tps65912-regulator.c
blob3a9313e00fac796b3d8799c90004a13fd2587142
1 /*
2 * tps65912.c -- TI tps65912
4 * Copyright 2011 Texas Instruments Inc.
6 * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * This driver is based on wm8350 implementation.
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/tps65912.h>
28 /* DCDC's */
29 #define TPS65912_REG_DCDC1 0
30 #define TPS65912_REG_DCDC2 1
31 #define TPS65912_REG_DCDC3 2
32 #define TPS65912_REG_DCDC4 3
34 /* LDOs */
35 #define TPS65912_REG_LDO1 4
36 #define TPS65912_REG_LDO2 5
37 #define TPS65912_REG_LDO3 6
38 #define TPS65912_REG_LDO4 7
39 #define TPS65912_REG_LDO5 8
40 #define TPS65912_REG_LDO6 9
41 #define TPS65912_REG_LDO7 10
42 #define TPS65912_REG_LDO8 11
43 #define TPS65912_REG_LDO9 12
44 #define TPS65912_REG_LDO10 13
46 #define TPS65912_MAX_REG_ID TPS65912_REG_LDO_10
48 /* Number of step-down converters available */
49 #define TPS65912_NUM_DCDC 4
51 /* Number of LDO voltage regulators available */
52 #define TPS65912_NUM_LDO 10
54 /* Number of total regulators available */
55 #define TPS65912_NUM_REGULATOR (TPS65912_NUM_DCDC + TPS65912_NUM_LDO)
57 #define TPS65912_REG_ENABLED 0x80
58 #define OP_SELREG_MASK 0x40
59 #define OP_SELREG_SHIFT 6
61 struct tps_info {
62 const char *name;
65 static struct tps_info tps65912_regs[] = {
67 .name = "DCDC1",
70 .name = "DCDC2",
73 .name = "DCDC3",
76 .name = "DCDC4",
79 .name = "LDO1",
82 .name = "LDO2",
85 .name = "LDO3",
88 .name = "LDO4",
91 .name = "LDO5",
94 .name = "LDO6",
97 .name = "LDO7",
100 .name = "LDO8",
103 .name = "LDO9",
106 .name = "LDO10",
110 struct tps65912_reg {
111 struct regulator_desc desc[TPS65912_NUM_REGULATOR];
112 struct tps65912 *mfd;
113 struct regulator_dev *rdev[TPS65912_NUM_REGULATOR];
114 struct tps_info *info[TPS65912_NUM_REGULATOR];
115 /* for read/write access */
116 struct mutex io_lock;
117 int mode;
118 int (*get_ctrl_reg)(int);
119 int dcdc1_range;
120 int dcdc2_range;
121 int dcdc3_range;
122 int dcdc4_range;
123 int pwm_mode_reg;
124 int eco_reg;
127 static int tps65912_get_range(struct tps65912_reg *pmic, int id)
129 struct tps65912 *mfd = pmic->mfd;
131 if (id > TPS65912_REG_DCDC4)
132 return 0;
134 switch (id) {
135 case TPS65912_REG_DCDC1:
136 pmic->dcdc1_range = tps65912_reg_read(mfd,
137 TPS65912_DCDC1_LIMIT);
138 if (pmic->dcdc1_range < 0)
139 return pmic->dcdc1_range;
140 pmic->dcdc1_range = (pmic->dcdc1_range &
141 DCDC_LIMIT_RANGE_MASK) >> DCDC_LIMIT_RANGE_SHIFT;
142 return pmic->dcdc1_range;
143 case TPS65912_REG_DCDC2:
144 pmic->dcdc2_range = tps65912_reg_read(mfd,
145 TPS65912_DCDC2_LIMIT);
146 if (pmic->dcdc2_range < 0)
147 return pmic->dcdc2_range;
148 pmic->dcdc2_range = (pmic->dcdc2_range &
149 DCDC_LIMIT_RANGE_MASK) >> DCDC_LIMIT_RANGE_SHIFT;
150 return pmic->dcdc2_range;
151 case TPS65912_REG_DCDC3:
152 pmic->dcdc3_range = tps65912_reg_read(mfd,
153 TPS65912_DCDC3_LIMIT);
154 if (pmic->dcdc3_range < 0)
155 return pmic->dcdc3_range;
156 pmic->dcdc3_range = (pmic->dcdc3_range &
157 DCDC_LIMIT_RANGE_MASK) >> DCDC_LIMIT_RANGE_SHIFT;
158 return pmic->dcdc3_range;
159 case TPS65912_REG_DCDC4:
160 pmic->dcdc4_range = tps65912_reg_read(mfd,
161 TPS65912_DCDC4_LIMIT);
162 if (pmic->dcdc4_range < 0)
163 return pmic->dcdc4_range;
164 pmic->dcdc4_range = (pmic->dcdc4_range &
165 DCDC_LIMIT_RANGE_MASK) >> DCDC_LIMIT_RANGE_SHIFT;
166 return pmic->dcdc4_range;
167 default:
168 return 0;
172 static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
174 unsigned long uv;
176 uv = ((vsel * 12500) + 500000);
177 return uv;
180 static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
182 unsigned long uv;
184 uv = ((vsel * 12500) + 700000);
185 return uv;
188 static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
190 unsigned long uv;
192 uv = ((vsel * 25000) + 500000);
193 return uv;
196 static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
198 unsigned long uv;
200 if (vsel == 0x3f)
201 uv = 3800000;
202 else
203 uv = ((vsel * 50000) + 500000);
205 return uv;
208 static unsigned long tps65912_vsel_to_uv_ldo(u8 vsel)
210 unsigned long uv = 0;
212 if (vsel <= 32)
213 uv = ((vsel * 25000) + 800000);
214 else if (vsel > 32 && vsel <= 60)
215 uv = (((vsel - 32) * 50000) + 1600000);
216 else if (vsel > 60)
217 uv = (((vsel - 60) * 100000) + 3000000);
219 return uv;
222 static int tps65912_get_ctrl_register(int id)
224 switch (id) {
225 case TPS65912_REG_DCDC1:
226 return TPS65912_DCDC1_AVS;
227 case TPS65912_REG_DCDC2:
228 return TPS65912_DCDC2_AVS;
229 case TPS65912_REG_DCDC3:
230 return TPS65912_DCDC3_AVS;
231 case TPS65912_REG_DCDC4:
232 return TPS65912_DCDC4_AVS;
233 case TPS65912_REG_LDO1:
234 return TPS65912_LDO1_AVS;
235 case TPS65912_REG_LDO2:
236 return TPS65912_LDO2_AVS;
237 case TPS65912_REG_LDO3:
238 return TPS65912_LDO3_AVS;
239 case TPS65912_REG_LDO4:
240 return TPS65912_LDO4_AVS;
241 case TPS65912_REG_LDO5:
242 return TPS65912_LDO5;
243 case TPS65912_REG_LDO6:
244 return TPS65912_LDO6;
245 case TPS65912_REG_LDO7:
246 return TPS65912_LDO7;
247 case TPS65912_REG_LDO8:
248 return TPS65912_LDO8;
249 case TPS65912_REG_LDO9:
250 return TPS65912_LDO9;
251 case TPS65912_REG_LDO10:
252 return TPS65912_LDO10;
253 default:
254 return -EINVAL;
258 static int tps65912_get_dcdc_sel_register(struct tps65912_reg *pmic, int id)
260 struct tps65912 *mfd = pmic->mfd;
261 int opvsel = 0, sr = 0;
262 u8 reg = 0;
264 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_DCDC4)
265 return -EINVAL;
267 switch (id) {
268 case TPS65912_REG_DCDC1:
269 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC1_OP);
270 sr = ((opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT);
271 if (sr)
272 reg = TPS65912_DCDC1_AVS;
273 else
274 reg = TPS65912_DCDC1_OP;
275 break;
276 case TPS65912_REG_DCDC2:
277 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC2_OP);
278 sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
279 if (sr)
280 reg = TPS65912_DCDC2_AVS;
281 else
282 reg = TPS65912_DCDC2_OP;
283 break;
284 case TPS65912_REG_DCDC3:
285 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC3_OP);
286 sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
287 if (sr)
288 reg = TPS65912_DCDC3_AVS;
289 else
290 reg = TPS65912_DCDC3_OP;
291 break;
292 case TPS65912_REG_DCDC4:
293 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC4_OP);
294 sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
295 if (sr)
296 reg = TPS65912_DCDC4_AVS;
297 else
298 reg = TPS65912_DCDC4_OP;
299 break;
301 return reg;
304 static int tps65912_get_ldo_sel_register(struct tps65912_reg *pmic, int id)
306 struct tps65912 *mfd = pmic->mfd;
307 int opvsel = 0, sr = 0;
308 u8 reg = 0;
310 if (id < TPS65912_REG_LDO1 || id > TPS65912_REG_LDO10)
311 return -EINVAL;
313 switch (id) {
314 case TPS65912_REG_LDO1:
315 opvsel = tps65912_reg_read(mfd, TPS65912_LDO1_OP);
316 sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
317 if (sr)
318 reg = TPS65912_LDO1_AVS;
319 else
320 reg = TPS65912_LDO1_OP;
321 break;
322 case TPS65912_REG_LDO2:
323 opvsel = tps65912_reg_read(mfd, TPS65912_LDO2_OP);
324 sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
325 if (sr)
326 reg = TPS65912_LDO2_AVS;
327 else
328 reg = TPS65912_LDO2_OP;
329 break;
330 case TPS65912_REG_LDO3:
331 opvsel = tps65912_reg_read(mfd, TPS65912_LDO3_OP);
332 sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
333 if (sr)
334 reg = TPS65912_LDO3_AVS;
335 else
336 reg = TPS65912_LDO3_OP;
337 break;
338 case TPS65912_REG_LDO4:
339 opvsel = tps65912_reg_read(mfd, TPS65912_LDO4_OP);
340 sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
341 if (sr)
342 reg = TPS65912_LDO4_AVS;
343 else
344 reg = TPS65912_LDO4_OP;
345 break;
346 case TPS65912_REG_LDO5:
347 reg = TPS65912_LDO5;
348 break;
349 case TPS65912_REG_LDO6:
350 reg = TPS65912_LDO6;
351 break;
352 case TPS65912_REG_LDO7:
353 reg = TPS65912_LDO7;
354 break;
355 case TPS65912_REG_LDO8:
356 reg = TPS65912_LDO8;
357 break;
358 case TPS65912_REG_LDO9:
359 reg = TPS65912_LDO9;
360 break;
361 case TPS65912_REG_LDO10:
362 reg = TPS65912_LDO10;
363 break;
366 return reg;
369 static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
371 switch (id) {
372 case TPS65912_REG_DCDC1:
373 pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
374 pmic->eco_reg = TPS65912_DCDC1_AVS;
375 break;
376 case TPS65912_REG_DCDC2:
377 pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
378 pmic->eco_reg = TPS65912_DCDC2_AVS;
379 break;
380 case TPS65912_REG_DCDC3:
381 pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
382 pmic->eco_reg = TPS65912_DCDC3_AVS;
383 break;
384 case TPS65912_REG_DCDC4:
385 pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
386 pmic->eco_reg = TPS65912_DCDC4_AVS;
387 break;
388 default:
389 return -EINVAL;
392 return 0;
395 static int tps65912_reg_is_enabled(struct regulator_dev *dev)
397 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
398 struct tps65912 *mfd = pmic->mfd;
399 int reg, value, id = rdev_get_id(dev);
401 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
402 return -EINVAL;
404 reg = pmic->get_ctrl_reg(id);
405 if (reg < 0)
406 return reg;
408 value = tps65912_reg_read(mfd, reg);
409 if (value < 0)
410 return value;
412 return value & TPS65912_REG_ENABLED;
415 static int tps65912_reg_enable(struct regulator_dev *dev)
417 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
418 struct tps65912 *mfd = pmic->mfd;
419 int id = rdev_get_id(dev);
420 int reg;
422 if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
423 return -EINVAL;
425 reg = pmic->get_ctrl_reg(id);
426 if (reg < 0)
427 return reg;
429 return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED);
432 static int tps65912_reg_disable(struct regulator_dev *dev)
434 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
435 struct tps65912 *mfd = pmic->mfd;
436 int id = rdev_get_id(dev), reg;
438 reg = pmic->get_ctrl_reg(id);
439 if (reg < 0)
440 return reg;
442 return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED);
445 static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode)
447 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
448 struct tps65912 *mfd = pmic->mfd;
449 int pwm_mode, eco, id = rdev_get_id(dev);
451 tps65912_get_mode_regiters(pmic, id);
453 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
454 eco = tps65912_reg_read(mfd, pmic->eco_reg);
456 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
457 eco &= DCDC_AVS_ECO_MASK;
459 switch (mode) {
460 case REGULATOR_MODE_FAST:
461 /* Verify if mode alredy set */
462 if (pwm_mode && !eco)
463 break;
464 tps65912_set_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
465 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
466 break;
467 case REGULATOR_MODE_NORMAL:
468 case REGULATOR_MODE_IDLE:
469 if (!pwm_mode && !eco)
470 break;
471 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
472 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
473 break;
474 case REGULATOR_MODE_STANDBY:
475 if (!pwm_mode && eco)
476 break;
477 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
478 tps65912_set_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
479 break;
480 default:
481 return -EINVAL;
484 return 0;
487 static unsigned int tps65912_get_mode(struct regulator_dev *dev)
489 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
490 struct tps65912 *mfd = pmic->mfd;
491 int pwm_mode, eco, mode = 0, id = rdev_get_id(dev);
493 tps65912_get_mode_regiters(pmic, id);
495 pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
496 eco = tps65912_reg_read(mfd, pmic->eco_reg);
498 pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
499 eco &= DCDC_AVS_ECO_MASK;
501 if (pwm_mode && !eco)
502 mode = REGULATOR_MODE_FAST;
503 else if (!pwm_mode && !eco)
504 mode = REGULATOR_MODE_NORMAL;
505 else if (!pwm_mode && eco)
506 mode = REGULATOR_MODE_STANDBY;
508 return mode;
511 static int tps65912_get_voltage_dcdc(struct regulator_dev *dev)
513 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
514 struct tps65912 *mfd = pmic->mfd;
515 int id = rdev_get_id(dev), voltage = 0, range;
516 int opvsel = 0, avsel = 0, sr, vsel;
518 switch (id) {
519 case TPS65912_REG_DCDC1:
520 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC1_OP);
521 avsel = tps65912_reg_read(mfd, TPS65912_DCDC1_AVS);
522 range = pmic->dcdc1_range;
523 break;
524 case TPS65912_REG_DCDC2:
525 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC2_OP);
526 avsel = tps65912_reg_read(mfd, TPS65912_DCDC2_AVS);
527 range = pmic->dcdc2_range;
528 break;
529 case TPS65912_REG_DCDC3:
530 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC3_OP);
531 avsel = tps65912_reg_read(mfd, TPS65912_DCDC3_AVS);
532 range = pmic->dcdc3_range;
533 break;
534 case TPS65912_REG_DCDC4:
535 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC4_OP);
536 avsel = tps65912_reg_read(mfd, TPS65912_DCDC4_AVS);
537 range = pmic->dcdc4_range;
538 break;
539 default:
540 return -EINVAL;
543 sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
544 if (sr)
545 vsel = avsel;
546 else
547 vsel = opvsel;
548 vsel &= 0x3F;
550 switch (range) {
551 case 0:
552 /* 0.5 - 1.2875V in 12.5mV steps */
553 voltage = tps65912_vsel_to_uv_range0(vsel);
554 break;
555 case 1:
556 /* 0.7 - 1.4875V in 12.5mV steps */
557 voltage = tps65912_vsel_to_uv_range1(vsel);
558 break;
559 case 2:
560 /* 0.5 - 2.075V in 25mV steps */
561 voltage = tps65912_vsel_to_uv_range2(vsel);
562 break;
563 case 3:
564 /* 0.5 - 3.8V in 50mV steps */
565 voltage = tps65912_vsel_to_uv_range3(vsel);
566 break;
568 return voltage;
571 static int tps65912_set_voltage_dcdc(struct regulator_dev *dev,
572 unsigned selector)
574 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
575 struct tps65912 *mfd = pmic->mfd;
576 int id = rdev_get_id(dev);
577 int value;
578 u8 reg;
580 reg = tps65912_get_dcdc_sel_register(pmic, id);
581 value = tps65912_reg_read(mfd, reg);
582 value &= 0xC0;
583 return tps65912_reg_write(mfd, reg, selector | value);
586 static int tps65912_get_voltage_ldo(struct regulator_dev *dev)
588 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
589 struct tps65912 *mfd = pmic->mfd;
590 int id = rdev_get_id(dev);
591 int vsel = 0;
592 u8 reg;
594 reg = tps65912_get_ldo_sel_register(pmic, id);
595 vsel = tps65912_reg_read(mfd, reg);
596 vsel &= 0x3F;
598 return tps65912_vsel_to_uv_ldo(vsel);
601 static int tps65912_set_voltage_ldo(struct regulator_dev *dev,
602 unsigned selector)
604 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
605 struct tps65912 *mfd = pmic->mfd;
606 int id = rdev_get_id(dev), reg, value;
608 reg = tps65912_get_ldo_sel_register(pmic, id);
609 value = tps65912_reg_read(mfd, reg);
610 value &= 0xC0;
611 return tps65912_reg_write(mfd, reg, selector | value);
614 static int tps65912_list_voltage_dcdc(struct regulator_dev *dev,
615 unsigned selector)
617 struct tps65912_reg *pmic = rdev_get_drvdata(dev);
618 int range, voltage = 0, id = rdev_get_id(dev);
620 switch (id) {
621 case TPS65912_REG_DCDC1:
622 range = pmic->dcdc1_range;
623 break;
624 case TPS65912_REG_DCDC2:
625 range = pmic->dcdc2_range;
626 break;
627 case TPS65912_REG_DCDC3:
628 range = pmic->dcdc3_range;
629 break;
630 case TPS65912_REG_DCDC4:
631 range = pmic->dcdc4_range;
632 break;
633 default:
634 return -EINVAL;
637 switch (range) {
638 case 0:
639 /* 0.5 - 1.2875V in 12.5mV steps */
640 voltage = tps65912_vsel_to_uv_range0(selector);
641 break;
642 case 1:
643 /* 0.7 - 1.4875V in 12.5mV steps */
644 voltage = tps65912_vsel_to_uv_range1(selector);
645 break;
646 case 2:
647 /* 0.5 - 2.075V in 25mV steps */
648 voltage = tps65912_vsel_to_uv_range2(selector);
649 break;
650 case 3:
651 /* 0.5 - 3.8V in 50mV steps */
652 voltage = tps65912_vsel_to_uv_range3(selector);
653 break;
655 return voltage;
658 static int tps65912_list_voltage_ldo(struct regulator_dev *dev,
659 unsigned selector)
661 int ldo = rdev_get_id(dev);
663 if (ldo < TPS65912_REG_LDO1 || ldo > TPS65912_REG_LDO10)
664 return -EINVAL;
666 return tps65912_vsel_to_uv_ldo(selector);
669 /* Operations permitted on DCDCx */
670 static struct regulator_ops tps65912_ops_dcdc = {
671 .is_enabled = tps65912_reg_is_enabled,
672 .enable = tps65912_reg_enable,
673 .disable = tps65912_reg_disable,
674 .set_mode = tps65912_set_mode,
675 .get_mode = tps65912_get_mode,
676 .get_voltage = tps65912_get_voltage_dcdc,
677 .set_voltage_sel = tps65912_set_voltage_dcdc,
678 .list_voltage = tps65912_list_voltage_dcdc,
681 /* Operations permitted on LDOx */
682 static struct regulator_ops tps65912_ops_ldo = {
683 .is_enabled = tps65912_reg_is_enabled,
684 .enable = tps65912_reg_enable,
685 .disable = tps65912_reg_disable,
686 .get_voltage = tps65912_get_voltage_ldo,
687 .set_voltage_sel = tps65912_set_voltage_ldo,
688 .list_voltage = tps65912_list_voltage_ldo,
691 static __devinit int tps65912_probe(struct platform_device *pdev)
693 struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
694 struct tps_info *info;
695 struct regulator_init_data *reg_data;
696 struct regulator_dev *rdev;
697 struct tps65912_reg *pmic;
698 struct tps65912_board *pmic_plat_data;
699 int i, err;
701 pmic_plat_data = dev_get_platdata(tps65912->dev);
702 if (!pmic_plat_data)
703 return -EINVAL;
705 reg_data = pmic_plat_data->tps65912_pmic_init_data;
707 pmic = kzalloc(sizeof(*pmic), GFP_KERNEL);
708 if (!pmic)
709 return -ENOMEM;
711 mutex_init(&pmic->io_lock);
712 pmic->mfd = tps65912;
713 platform_set_drvdata(pdev, pmic);
715 pmic->get_ctrl_reg = &tps65912_get_ctrl_register;
716 info = tps65912_regs;
718 for (i = 0; i < TPS65912_NUM_REGULATOR; i++, info++, reg_data++) {
719 int range = 0;
720 /* Register the regulators */
721 pmic->info[i] = info;
723 pmic->desc[i].name = info->name;
724 pmic->desc[i].id = i;
725 pmic->desc[i].n_voltages = 64;
726 pmic->desc[i].ops = (i > TPS65912_REG_DCDC4 ?
727 &tps65912_ops_ldo : &tps65912_ops_dcdc);
728 pmic->desc[i].type = REGULATOR_VOLTAGE;
729 pmic->desc[i].owner = THIS_MODULE;
730 range = tps65912_get_range(pmic, i);
731 rdev = regulator_register(&pmic->desc[i],
732 tps65912->dev, reg_data, pmic);
733 if (IS_ERR(rdev)) {
734 dev_err(tps65912->dev,
735 "failed to register %s regulator\n",
736 pdev->name);
737 err = PTR_ERR(rdev);
738 goto err;
741 /* Save regulator for cleanup */
742 pmic->rdev[i] = rdev;
744 return 0;
746 err:
747 while (--i >= 0)
748 regulator_unregister(pmic->rdev[i]);
750 kfree(pmic);
751 return err;
754 static int __devexit tps65912_remove(struct platform_device *pdev)
756 struct tps65912_reg *tps65912_reg = platform_get_drvdata(pdev);
757 int i;
759 for (i = 0; i < TPS65912_NUM_REGULATOR; i++)
760 regulator_unregister(tps65912_reg->rdev[i]);
762 kfree(tps65912_reg);
763 return 0;
766 static struct platform_driver tps65912_driver = {
767 .driver = {
768 .name = "tps65912-pmic",
769 .owner = THIS_MODULE,
771 .probe = tps65912_probe,
772 .remove = __devexit_p(tps65912_remove),
776 * tps65912_init
778 * Module init function
780 static int __init tps65912_init(void)
782 return platform_driver_register(&tps65912_driver);
784 subsys_initcall(tps65912_init);
787 * tps65912_cleanup
789 * Module exit function
791 static void __exit tps65912_cleanup(void)
793 platform_driver_unregister(&tps65912_driver);
795 module_exit(tps65912_cleanup);
797 MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
798 MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
799 MODULE_LICENSE("GPL v2");
800 MODULE_ALIAS("platform:tps65912-pmic");