summaryrefslogtreecommitdiff
path: root/drivers/regulator/tps65912-regulator.c
blob: da00d88f94b7921b89384134d96a0d176cbfe999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*
 * tps65912.c  --  TI tps65912
 *
 * Copyright 2011 Texas Instruments Inc.
 *
 * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under  the terms of the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the License, or (at your
 *  option) any later version.
 *
 * This driver is based on wm8350 implementation.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/mfd/tps65912.h>

/* DCDC's */
#define TPS65912_REG_DCDC1	0
#define TPS65912_REG_DCDC2	1
#define TPS65912_REG_DCDC3	2
#define TPS65912_REG_DCDC4	3

/* LDOs */
#define TPS65912_REG_LDO1	4
#define TPS65912_REG_LDO2	5
#define TPS65912_REG_LDO3	6
#define TPS65912_REG_LDO4	7
#define TPS65912_REG_LDO5	8
#define TPS65912_REG_LDO6	9
#define TPS65912_REG_LDO7	10
#define TPS65912_REG_LDO8	11
#define TPS65912_REG_LDO9	12
#define TPS65912_REG_LDO10	13

/* Number of step-down converters available */
#define TPS65912_NUM_DCDC	4

/* Number of LDO voltage regulators  available */
#define TPS65912_NUM_LDO	10

/* Number of total regulators available */
#define TPS65912_NUM_REGULATOR		(TPS65912_NUM_DCDC + TPS65912_NUM_LDO)

#define TPS65912_REG_ENABLED	0x80
#define OP_SELREG_MASK		0x40
#define OP_SELREG_SHIFT		6

struct tps_info {
	const char *name;
};

static struct tps_info tps65912_regs[] = {
	{
		.name = "DCDC1",
	},
	{
		.name = "DCDC2",
	},
	{
		.name = "DCDC3",
	},
	{
		.name = "DCDC4",
	},
	{
		.name = "LDO1",
	},
	{
		.name = "LDO2",
	},
	{
		.name = "LDO3",
	},
	{
		.name = "LDO4",
	},
	{
		.name = "LDO5",
	},
	{
		.name = "LDO6",
	},
	{
		.name = "LDO7",
	},
	{
		.name = "LDO8",
	},
	{
		.name = "LDO9",
	},
	{
		.name = "LDO10",
	},
};

struct tps65912_reg {
	struct regulator_desc desc[TPS65912_NUM_REGULATOR];
	struct tps65912 *mfd;
	struct regulator_dev *rdev[TPS65912_NUM_REGULATOR];
	struct tps_info *info[TPS65912_NUM_REGULATOR];
	/* for read/write access */
	struct mutex io_lock;
	int mode;
	int (*get_ctrl_reg)(int);
	int dcdc1_range;
	int dcdc2_range;
	int dcdc3_range;
	int dcdc4_range;
	int pwm_mode_reg;
	int eco_reg;
};

static int tps65912_get_range(struct tps65912_reg *pmic, int id)
{
	struct tps65912 *mfd = pmic->mfd;

	if (id > TPS65912_REG_DCDC4)
		return 0;

	switch (id) {
	case TPS65912_REG_DCDC1:
		pmic->dcdc1_range = tps65912_reg_read(mfd,
							TPS65912_DCDC1_LIMIT);
		if (pmic->dcdc1_range < 0)
			return pmic->dcdc1_range;
		pmic->dcdc1_range = (pmic->dcdc1_range &
			DCDC_LIMIT_RANGE_MASK) >> DCDC_LIMIT_RANGE_SHIFT;
		return pmic->dcdc1_range;
	case TPS65912_REG_DCDC2:
		pmic->dcdc2_range = tps65912_reg_read(mfd,
							TPS65912_DCDC2_LIMIT);
		if (pmic->dcdc2_range < 0)
			return pmic->dcdc2_range;
		pmic->dcdc2_range = (pmic->dcdc2_range &
			DCDC_LIMIT_RANGE_MASK) >> DCDC_LIMIT_RANGE_SHIFT;
		return pmic->dcdc2_range;
	case TPS65912_REG_DCDC3:
		pmic->dcdc3_range = tps65912_reg_read(mfd,
							TPS65912_DCDC3_LIMIT);
		if (pmic->dcdc3_range < 0)
			return pmic->dcdc3_range;
		pmic->dcdc3_range = (pmic->dcdc3_range &
			DCDC_LIMIT_RANGE_MASK) >> DCDC_LIMIT_RANGE_SHIFT;
		return pmic->dcdc3_range;
	case TPS65912_REG_DCDC4:
		pmic->dcdc4_range = tps65912_reg_read(mfd,
							TPS65912_DCDC4_LIMIT);
		if (pmic->dcdc4_range < 0)
			return pmic->dcdc4_range;
		pmic->dcdc4_range = (pmic->dcdc4_range &
			DCDC_LIMIT_RANGE_MASK) >> DCDC_LIMIT_RANGE_SHIFT;
		return pmic->dcdc4_range;
	default:
		return 0;
	}
}

static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
{
	unsigned long uv;

	uv = ((vsel * 12500) + 500000);
	return uv;
}

static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
{
	unsigned long uv;

	 uv = ((vsel * 12500) + 700000);
	return uv;
}

static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
{
	unsigned long uv;

	uv = ((vsel * 25000) + 500000);
	return uv;
}

static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
{
	unsigned long uv;

	if (vsel == 0x3f)
		uv = 3800000;
	else
		uv = ((vsel * 50000) + 500000);

	return uv;
}

static unsigned long tps65912_vsel_to_uv_ldo(u8 vsel)
{
	unsigned long uv = 0;

	if (vsel <= 32)
		uv = ((vsel * 25000) + 800000);
	else if (vsel > 32 && vsel <= 60)
		uv = (((vsel - 32) * 50000) + 1600000);
	else if (vsel > 60)
		uv = (((vsel - 60) * 100000) + 3000000);

	return uv;
}

static int tps65912_get_ctrl_register(int id)
{
	switch (id) {
	case TPS65912_REG_DCDC1:
		return TPS65912_DCDC1_AVS;
	case TPS65912_REG_DCDC2:
		return TPS65912_DCDC2_AVS;
	case TPS65912_REG_DCDC3:
		return TPS65912_DCDC3_AVS;
	case TPS65912_REG_DCDC4:
		return TPS65912_DCDC4_AVS;
	case TPS65912_REG_LDO1:
		return TPS65912_LDO1_AVS;
	case TPS65912_REG_LDO2:
		return TPS65912_LDO2_AVS;
	case TPS65912_REG_LDO3:
		return TPS65912_LDO3_AVS;
	case TPS65912_REG_LDO4:
		return TPS65912_LDO4_AVS;
	case TPS65912_REG_LDO5:
		return TPS65912_LDO5;
	case TPS65912_REG_LDO6:
		return TPS65912_LDO6;
	case TPS65912_REG_LDO7:
		return TPS65912_LDO7;
	case TPS65912_REG_LDO8:
		return TPS65912_LDO8;
	case TPS65912_REG_LDO9:
		return TPS65912_LDO9;
	case TPS65912_REG_LDO10:
		return TPS65912_LDO10;
	default:
		return -EINVAL;
	}
}

static int tps65912_get_dcdc_sel_register(struct tps65912_reg *pmic, int id)
{
	struct tps65912 *mfd = pmic->mfd;
	int opvsel = 0, sr = 0;
	u8 reg = 0;

	if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_DCDC4)
		return -EINVAL;

	switch (id) {
	case TPS65912_REG_DCDC1:
		opvsel = tps65912_reg_read(mfd, TPS65912_DCDC1_OP);
		sr = ((opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT);
		if (sr)
			reg = TPS65912_DCDC1_AVS;
		else
			reg = TPS65912_DCDC1_OP;
		break;
	case TPS65912_REG_DCDC2:
		opvsel = tps65912_reg_read(mfd, TPS65912_DCDC2_OP);
		sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
		if (sr)
			reg = TPS65912_DCDC2_AVS;
		else
			reg = TPS65912_DCDC2_OP;
		break;
	case TPS65912_REG_DCDC3:
		opvsel = tps65912_reg_read(mfd, TPS65912_DCDC3_OP);
		sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
		if (sr)
			reg = TPS65912_DCDC3_AVS;
		else
			reg = TPS65912_DCDC3_OP;
		break;
	case TPS65912_REG_DCDC4:
		opvsel = tps65912_reg_read(mfd, TPS65912_DCDC4_OP);
		sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
		if (sr)
			reg = TPS65912_DCDC4_AVS;
		else
			reg = TPS65912_DCDC4_OP;
		break;
	}
	return reg;
}

static int tps65912_get_ldo_sel_register(struct tps65912_reg *pmic, int id)
{
	struct tps65912 *mfd = pmic->mfd;
	int opvsel = 0, sr = 0;
	u8 reg = 0;

	if (id < TPS65912_REG_LDO1 || id > TPS65912_REG_LDO10)
		return -EINVAL;

	switch (id) {
	case TPS65912_REG_LDO1:
		opvsel = tps65912_reg_read(mfd, TPS65912_LDO1_OP);
		sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
		if (sr)
			reg = TPS65912_LDO1_AVS;
		else
			reg = TPS65912_LDO1_OP;
		break;
	case TPS65912_REG_LDO2:
		opvsel = tps65912_reg_read(mfd, TPS65912_LDO2_OP);
		sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
		if (sr)
			reg = TPS65912_LDO2_AVS;
		else
			reg = TPS65912_LDO2_OP;
		break;
	case TPS65912_REG_LDO3:
		opvsel = tps65912_reg_read(mfd, TPS65912_LDO3_OP);
		sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
		if (sr)
			reg = TPS65912_LDO3_AVS;
		else
			reg = TPS65912_LDO3_OP;
		break;
	case TPS65912_REG_LDO4:
		opvsel = tps65912_reg_read(mfd, TPS65912_LDO4_OP);
		sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
		if (sr)
			reg = TPS65912_LDO4_AVS;
		else
			reg = TPS65912_LDO4_OP;
		break;
	case TPS65912_REG_LDO5:
		reg = TPS65912_LDO5;
		break;
	case TPS65912_REG_LDO6:
		reg = TPS65912_LDO6;
		break;
	case TPS65912_REG_LDO7:
		reg = TPS65912_LDO7;
		break;
	case TPS65912_REG_LDO8:
		reg = TPS65912_LDO8;
		break;
	case TPS65912_REG_LDO9:
		reg = TPS65912_LDO9;
		break;
	case TPS65912_REG_LDO10:
		reg = TPS65912_LDO10;
		break;
	}

	return reg;
}

static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
{
	switch (id) {
	case TPS65912_REG_DCDC1:
		pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
		pmic->eco_reg = TPS65912_DCDC1_AVS;
		break;
	case TPS65912_REG_DCDC2:
		pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
		pmic->eco_reg = TPS65912_DCDC2_AVS;
		break;
	case TPS65912_REG_DCDC3:
		pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
		pmic->eco_reg = TPS65912_DCDC3_AVS;
		break;
	case TPS65912_REG_DCDC4:
		pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
		pmic->eco_reg = TPS65912_DCDC4_AVS;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int tps65912_reg_is_enabled(struct regulator_dev *dev)
{
	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
	struct tps65912 *mfd = pmic->mfd;
	int reg, value, id = rdev_get_id(dev);

	if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
		return -EINVAL;

	reg = pmic->get_ctrl_reg(id);
	if (reg < 0)
		return reg;

	value = tps65912_reg_read(mfd, reg);
	if (value < 0)
		return value;

	return value & TPS65912_REG_ENABLED;
}

static int tps65912_reg_enable(struct regulator_dev *dev)
{
	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
	struct tps65912 *mfd = pmic->mfd;
	int id = rdev_get_id(dev);
	int reg;

	if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
		return -EINVAL;

	reg = pmic->get_ctrl_reg(id);
	if (reg < 0)
		return reg;

	return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED);
}

static int tps65912_reg_disable(struct regulator_dev *dev)
{
	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
	struct tps65912 *mfd = pmic->mfd;
	int id = rdev_get_id(dev), reg;

	reg = pmic->get_ctrl_reg(id);
	if (reg < 0)
		return reg;

	return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED);
}

static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode)
{
	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
	struct tps65912 *mfd = pmic->mfd;
	int pwm_mode, eco, id = rdev_get_id(dev);

	tps65912_get_mode_regiters(pmic, id);

	pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
	eco = tps65912_reg_read(mfd, pmic->eco_reg);

	pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
	eco &= DCDC_AVS_ECO_MASK;

	switch (mode) {
	case REGULATOR_MODE_FAST:
		/* Verify if mode alredy set */
		if (pwm_mode && !eco)
			break;
		tps65912_set_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
		tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
		break;
	case REGULATOR_MODE_NORMAL:
	case REGULATOR_MODE_IDLE:
		if (!pwm_mode && !eco)
			break;
		tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
		tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
		break;
	case REGULATOR_MODE_STANDBY:
		if (!pwm_mode && eco)
			break;
		tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
		tps65912_set_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static unsigned int tps65912_get_mode(struct regulator_dev *dev)
{
	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
	struct tps65912 *mfd = pmic->mfd;
	int pwm_mode, eco, mode = 0, id = rdev_get_id(dev);

	tps65912_get_mode_regiters(pmic, id);

	pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
	eco = tps65912_reg_read(mfd, pmic->eco_reg);

	pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
	eco &= DCDC_AVS_ECO_MASK;

	if (pwm_mode && !eco)
		mode = REGULATOR_MODE_FAST;
	else if (!pwm_mode && !eco)
		mode = REGULATOR_MODE_NORMAL;
	else if (!pwm_mode && eco)
		mode = REGULATOR_MODE_STANDBY;

	return mode;
}

static int tps65912_get_voltage_dcdc(struct regulator_dev *dev)
{
	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
	struct tps65912 *mfd = pmic->mfd;
	int id = rdev_get_id(dev), voltage = 0, range;
	int opvsel = 0, avsel = 0, sr, vsel;

	switch (id) {
	case TPS65912_REG_DCDC1:
		opvsel = tps65912_reg_read(mfd, TPS65912_DCDC1_OP);
		avsel = tps65912_reg_read(mfd, TPS65912_DCDC1_AVS);
		range = pmic->dcdc1_range;
		break;
	case TPS65912_REG_DCDC2:
		opvsel = tps65912_reg_read(mfd, TPS65912_DCDC2_OP);
		avsel = tps65912_reg_read(mfd, TPS65912_DCDC2_AVS);
		range = pmic->dcdc2_range;
		break;
	case TPS65912_REG_DCDC3:
		opvsel = tps65912_reg_read(mfd, TPS65912_DCDC3_OP);
		avsel = tps65912_reg_read(mfd, TPS65912_DCDC3_AVS);
		range = pmic->dcdc3_range;
		break;
	case TPS65912_REG_DCDC4:
		opvsel = tps65912_reg_read(mfd, TPS65912_DCDC4_OP);
		avsel = tps65912_reg_read(mfd, TPS65912_DCDC4_AVS);
		range = pmic->dcdc4_range;
		break;
	default:
		return -EINVAL;
	}

	sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
	if (sr)
		vsel = avsel;
	else
		vsel = opvsel;
	vsel &= 0x3F;

	switch (range) {
	case 0:
		/* 0.5 - 1.2875V in 12.5mV steps */
		voltage = tps65912_vsel_to_uv_range0(vsel);
		break;
	case 1:
		/* 0.7 - 1.4875V in 12.5mV steps */
		voltage = tps65912_vsel_to_uv_range1(vsel);
		break;
	case 2:
		/* 0.5 - 2.075V in 25mV steps */
		voltage = tps65912_vsel_to_uv_range2(vsel);
		break;
	case 3:
		/* 0.5 - 3.8V in 50mV steps */
		voltage = tps65912_vsel_to_uv_range3(vsel);
		break;
	}
	return voltage;
}

static int tps65912_set_voltage_dcdc(struct regulator_dev *dev,
						unsigned selector)
{
	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
	struct tps65912 *mfd = pmic->mfd;
	int id = rdev_get_id(dev);
	int value;
	u8 reg;

	reg = tps65912_get_dcdc_sel_register(pmic, id);
	value = tps65912_reg_read(mfd, reg);
	value &= 0xC0;
	return tps65912_reg_write(mfd, reg, selector | value);
}

static int tps65912_get_voltage_ldo(struct regulator_dev *dev)
{
	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
	struct tps65912 *mfd = pmic->mfd;
	int id = rdev_get_id(dev);
	int vsel = 0;
	u8 reg;

	reg = tps65912_get_ldo_sel_register(pmic, id);
	vsel = tps65912_reg_read(mfd, reg);
	vsel &= 0x3F;

	return tps65912_vsel_to_uv_ldo(vsel);
}

static int tps65912_set_voltage_ldo(struct regulator_dev *dev,
						unsigned selector)
{
	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
	struct tps65912 *mfd = pmic->mfd;
	int id = rdev_get_id(dev), reg, value;

	reg = tps65912_get_ldo_sel_register(pmic, id);
	value = tps65912_reg_read(mfd, reg);
	value &= 0xC0;
	return tps65912_reg_write(mfd, reg, selector | value);
}

static int tps65912_list_voltage_dcdc(struct regulator_dev *dev,
					unsigned selector)
{
	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
	int range, voltage = 0, id = rdev_get_id(dev);

	switch (id) {
	case TPS65912_REG_DCDC1:
		range = pmic->dcdc1_range;
		break;
	case TPS65912_REG_DCDC2:
		range = pmic->dcdc2_range;
		break;
	case TPS65912_REG_DCDC3:
		range = pmic->dcdc3_range;
		break;
	case TPS65912_REG_DCDC4:
		range = pmic->dcdc4_range;
		break;
	default:
		return -EINVAL;
	}

	switch (range) {
	case 0:
		/* 0.5 - 1.2875V in 12.5mV steps */
		voltage = tps65912_vsel_to_uv_range0(selector);
		break;
	case 1:
		/* 0.7 - 1.4875V in 12.5mV steps */
		voltage = tps65912_vsel_to_uv_range1(selector);
		break;
	case 2:
		/* 0.5 - 2.075V in 25mV steps */
		voltage = tps65912_vsel_to_uv_range2(selector);
		break;
	case 3:
		/* 0.5 - 3.8V in 50mV steps */
		voltage = tps65912_vsel_to_uv_range3(selector);
		break;
	}
	return voltage;
}

static int tps65912_list_voltage_ldo(struct regulator_dev *dev,
					unsigned selector)
{
	int ldo = rdev_get_id(dev);

	if (ldo < TPS65912_REG_LDO1 || ldo > TPS65912_REG_LDO10)
		return -EINVAL;

	return tps65912_vsel_to_uv_ldo(selector);
}

/* Operations permitted on DCDCx */
static struct regulator_ops tps65912_ops_dcdc = {
	.is_enabled = tps65912_reg_is_enabled,
	.enable = tps65912_reg_enable,
	.disable = tps65912_reg_disable,
	.set_mode = tps65912_set_mode,
	.get_mode = tps65912_get_mode,
	.get_voltage = tps65912_get_voltage_dcdc,
	.set_voltage_sel = tps65912_set_voltage_dcdc,
	.list_voltage = tps65912_list_voltage_dcdc,
};

/* Operations permitted on LDOx */
static struct regulator_ops tps65912_ops_ldo = {
	.is_enabled = tps65912_reg_is_enabled,
	.enable = tps65912_reg_enable,
	.disable = tps65912_reg_disable,
	.get_voltage = tps65912_get_voltage_ldo,
	.set_voltage_sel = tps65912_set_voltage_ldo,
	.list_voltage = tps65912_list_voltage_ldo,
};

static __devinit int tps65912_probe(struct platform_device *pdev)
{
	struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
	struct tps_info *info;
	struct regulator_init_data *reg_data;
	struct regulator_dev *rdev;
	struct tps65912_reg *pmic;
	struct tps65912_board *pmic_plat_data;
	int i, err;

	pmic_plat_data = dev_get_platdata(tps65912->dev);
	if (!pmic_plat_data)
		return -EINVAL;

	reg_data = pmic_plat_data->tps65912_pmic_init_data;

	pmic = kzalloc(sizeof(*pmic), GFP_KERNEL);
	if (!pmic)
		return -ENOMEM;

	mutex_init(&pmic->io_lock);
	pmic->mfd = tps65912;
	platform_set_drvdata(pdev, pmic);

	pmic->get_ctrl_reg = &tps65912_get_ctrl_register;
	info = tps65912_regs;

	for (i = 0; i < TPS65912_NUM_REGULATOR; i++, info++, reg_data++) {
		int range = 0;
		/* Register the regulators */
		pmic->info[i] = info;

		pmic->desc[i].name = info->name;
		pmic->desc[i].id = i;
		pmic->desc[i].n_voltages = 64;
		pmic->desc[i].ops = (i > TPS65912_REG_DCDC4 ?
			&tps65912_ops_ldo : &tps65912_ops_dcdc);
		pmic->desc[i].type = REGULATOR_VOLTAGE;
		pmic->desc[i].owner = THIS_MODULE;
		range = tps65912_get_range(pmic, i);
		rdev = regulator_register(&pmic->desc[i],
					tps65912->dev, reg_data, pmic, NULL);
		if (IS_ERR(rdev)) {
			dev_err(tps65912->dev,
				"failed to register %s regulator\n",
				pdev->name);
			err = PTR_ERR(rdev);
			goto err;
		}

		/* Save regulator for cleanup */
		pmic->rdev[i] = rdev;
	}
	return 0;

err:
	while (--i >= 0)
		regulator_unregister(pmic->rdev[i]);

	kfree(pmic);
	return err;
}

static int __devexit tps65912_remove(struct platform_device *pdev)
{
	struct tps65912_reg *tps65912_reg = platform_get_drvdata(pdev);
	int i;

	for (i = 0; i < TPS65912_NUM_REGULATOR; i++)
		regulator_unregister(tps65912_reg->rdev[i]);

	kfree(tps65912_reg);
	return 0;
}

static struct platform_driver tps65912_driver = {
	.driver = {
		.name = "tps65912-pmic",
		.owner = THIS_MODULE,
	},
	.probe = tps65912_probe,
	.remove = __devexit_p(tps65912_remove),
};

/**
 * tps65912_init
 *
 * Module init function
 */
static int __init tps65912_init(void)
{
	return platform_driver_register(&tps65912_driver);
}
subsys_initcall(tps65912_init);

/**
 * tps65912_cleanup
 *
 * Module exit function
 */
static void __exit tps65912_cleanup(void)
{
	platform_driver_unregister(&tps65912_driver);
}
module_exit(tps65912_cleanup);

MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:tps65912-pmic");