summaryrefslogtreecommitdiff
path: root/drivers/regulator/mc34708-regulator.c
blob: f91d479267343576d6fadb5e0bb7feee0f945fee (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
/*
 * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
 *
 * 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 program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */
#include <linux/mfd/mc-pmic.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/driver.h>
#include <linux/platform_device.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/err.h>
#include "mc34708.h"

static const int mc34708_sw1A[] = {
	650000, 662500, 675000, 687500, 700000, 712500,
	725000, 737500, 750000, 762500, 775000, 787500,
	800000, 812500, 825000, 837500, 850000, 862500,
	875000, 887500, 900000, 912500, 925000, 937500,
	950000, 962500, 975000, 987500, 1000000, 1012500,
	1025000, 1037500, 1050000, 1062500, 1075000, 1087500,
	1100000, 1112500, 1125000, 1137500, 1150000, 1162500,
	1175000, 1187500, 1200000, 1212500, 1225000, 1237500,
	1250000, 1262500, 1275000, 1287500, 1300000, 1312500,
	1325000, 1337500, 1350000, 1362500, 1375000, 1387500,
	1400000, 1412500, 1425000, 1437500,
};


static const int mc34708_sw2[] = {
	650000, 662500, 675000, 687500, 700000, 712500,
	725000, 737500, 750000, 762500, 775000, 787500,
	800000, 812500, 825000, 837500, 850000, 862500,
	875000, 887500, 900000, 912500, 925000, 937500,
	950000, 962500, 975000, 987500, 1000000, 1012500,
	1025000, 1037500, 1050000, 1062500, 1075000, 1087500,
	1100000, 1112500, 1125000, 1137500, 1150000, 1162500,
	1175000, 1187500, 1200000, 1212500, 1225000, 1237500,
	1250000, 1262500, 1275000, 1287500, 1300000, 1312500,
	1325000, 1337500, 1350000, 1362500, 1375000, 1387500,
	1400000, 1412500, 1425000, 1437500,
};

static const int mc34708_sw3[] = {
	650000, 675000, 700000, 725000, 750000, 775000,
	800000, 825000, 850000, 875000, 900000, 925000,
	950000, 975000, 1000000, 1025000, 1050000, 1075000,
	1100000, 1125000, 1150000, 1175000, 1200000, 1225000,
	1250000, 1275000, 1300000, 1325000, 1350000, 1375000,
	1400000, 1425000,
};

static const int mc34708_sw4A[] = {
	1200000, 1225000, 1250000, 1275000, 1300000, 1325000,
	1350000, 1375000, 1400000, 1425000, 1450000, 1475000,
	1500000, 1525000, 1550000, 1575000, 1600000, 1625000,
	1650000, 1675000, 1700000, 1725000, 1750000, 1775000,
	1800000, 1825000, 1850000, 2500000, 3150000,
};


static const int mc34708_sw5[] = {
	1200000, 1225000, 1250000, 1275000, 1300000, 1325000,
	1350000, 1375000, 1400000, 1425000, 1450000, 1475000,
	1500000, 1525000, 1550000, 1575000, 1600000, 1625000,
	1650000, 1675000, 1700000, 1725000, 1750000, 1775000,
	1800000, 1825000, 1850000,
};

static const int mc34708_swbst[] = {
	5000000, 5050000, 5100000, 5150000,
};

static const int mc34708_vpll[] = {
	1200000, 1250000, 1500000, 1800000,
};

static const int mc34708_vrefddr[] = {
	600000,
};

static const int mc34708_vusb[] = {
	3300000,
};

static const int mc34708_vusb2[] = {
	2500000, 2600000, 2750000, 3000000,
};

static const int mc34708_vdac[] = {
	2500000, 2600000, 2750000, 2775000,
};

static const int mc34708_vgen1[] = {
	1200000, 1250000, 1300000, 1350000,
	1400000, 1450000, 1500000, 1550000,
};

static const int mc34708_vgen2[] = {
	2500000, 2700000, 2800000, 2900000,
	3000000, 3100000, 3150000, 3300000,
};

static struct regulator_ops mc34708_regulator_ops;
static struct regulator_ops mc34708_fixed_regulator_ops;
/* sw regulators need special care due to the "hi bit" */
static struct regulator_ops mc34708_sw_regulator_ops;
static struct regulator_ops mc34708_sw4_regulator_ops;

#define MC34708_FIXED_VOL_DEFINE(name, reg, voltages)		\
	MC34708_FIXED_DEFINE(MC34708_, name, reg, voltages,	\
			mc34708_fixed_regulator_ops)

#define MC34708_SW_DEFINE(name, reg, vsel_reg, voltages)	\
	MC34708_DEFINE(MC34708_, name, reg, vsel_reg, voltages, \
			mc34708_sw_regulator_ops)

#define MC34708_DEFINE_REGU(name, reg, vsel_reg, voltages)	\
	MC34708_DEFINE(MC34708_, name, reg, vsel_reg, voltages, \
			mc34708_regulator_ops)

#define MC34708_SW4_DEFINE(name, reg, vsel_reg, voltages)	\
	MC34708_DEFINE(MC34708_, name, reg, vsel_reg, voltages, \
			mc34708_sw4_regulator_ops)

#define MC34708_REVISION	7

#define MC34708_SW1ABVOL	24
#define MC34708_SW1ABVOL_SW1AVSEL	0
#define MC34708_SW1ABVOL_SW1AVSEL_M	(0x3f<<0)
#define MC34708_SW1ABVOL_SW1AEN	0
#define MC34708_SW1ABVOL_SW1BVSEL	0
#define MC34708_SW1ABVOL_SW1BVSEL_M	(0x3f<<0)
#define MC34708_SW1ABVOL_SW1BEN	0

#define MC34708_SW23VOL	25
#define MC34708_SW23VOL_SW2VSEL	0
#define MC34708_SW23VOL_SW2VSEL_M	(0x3f<<0)
#define MC34708_SW23VOL_SW2EN	0
#define MC34708_SW23VOL_SW3VSEL	12
#define MC34708_SW23VOL_SW3VSEL_M	(0x3f<<12)
#define MC34708_SW23VOL_SW3EN	0

#define MC34708_SW4ABVOL	26
#define MC34708_SW4ABVOL_SW4AVSEL	0
#define MC34708_SW4ABVOL_SW4AVSEL_M	(0x1f<<0)
#define MC34708_SW4ABVOL_SW4AHI	10
#define MC34708_SW4ABVOL_SW4AHI_M	(0x3<<10)
#define MC34708_SW4ABVOL_SW4AEN	0
#define	MC34708_SW4ABVOL_SW4BVSEL	12
#define MC34708_SW4ABVOL_SW4BVSEL_M	(0x1f<<12)
#define MC34708_SW4ABVOL_SW4BHI	22
#define MC34708_SW4ABVOL_SW4BHI_M	(0x3<<22)
#define MC34708_SW4ABVOL_SW4BEN	0

#define MC34708_SW5VOL	27
#define MC34708_SW5VOL_SW5VSEL	0
#define MC34708_SW5VOL_SW5VSEL_M	(0x1f<<0)
#define MC34708_SW5VOL_SW5EN	0

#define MC34708_SW12OP	28
#define MC34708_SW12OP_SW1AMODE_M	(0xf<<0)
#define MC34708_SW12OP_SW1AMODE_VALUE	(0xc<<0) /*Normal:APS,Standby:PFM */
#define MC34708_SW12OP_SW2MODE_M	(0xf<<14)
#define MC34708_SW12OP_SW2MODE_VALUE	(0xc<<14) /*Normal:APS,Standby:PFM */

#define MC34708_SW345OP	29
#define MC34708_SW345OP_SW3MODE_M	(0xf<<0)
#define MC34708_SW345OP_SW3MODE_VALUE	(0x0<<0) /*Normal:OFF,Standby:OFF */
#define MC34708_SW345OP_SW4AMODE_M	(0xf<<6)
#define MC34708_SW345OP_SW4AMODE_VALUE	(0xc<<6) /*Normal:APS,Standby:PFM */
#define MC34708_SW345OP_SW4BMODE_M	(0xf<<12)
#define MC34708_SW345OP_SW4BMODE_VALUE	(0xc<<12) /*Normal:APS,Standby:PFM */
#define MC34708_SW345OP_SW5MODE_M	(0xf<<18)
#define MC34708_SW345OP_SW5MODE_VALUE	(0xc<<18) /*Normal:APS,Standby:PFM */

#define MC34708_REGULATORSET0	30
#define MC34708_REGULATORSET0_VGEN1VSEL	0
#define MC34708_REGULATORSET0_VGEN1VSEL_M	(0x7<<0)
#define MC34708_REGULATORSET0_VDACVSEL	4
#define MC34708_REGULATORSET0_VDACVSEL_M	(0x3<<4)
#define MC34708_REGULATORSET0_VGEN2VSEL	6
#define MC34708_REGULATORSET0_VGEN2VSEL_M	(0x7<<6)
#define MC34708_REGULATORSET0_VPLLVSEL	9
#define MC34708_REGULATORSET0_VPLLVSEL_M	(0x3<<9)
#define MC34708_REGULATORSET0_VUSB2VSEL	11
#define MC34708_REGULATORSET0_VUSB2VSEL_M	(0x3<<9)

#define MC34708_SWBSTCONTROL	31
#define MC34708_SWBSTCONTROL_SWBSTVSEL	0
#define MC34708_SWBSTCONTROL_SWBSTVSEL_M	(0x3<<0)
#define MC34708_SWBSTCONTROL_SWBSTMODE_M	(0x3<<5)
#define MC34708_SWBSTCONTROL_SWBSTMODE_VALUE	(0x2<<5)	/*auto mode */
#define MC34708_SWBSTCONTROL_SWBSTEN	0

#define MC34708_REGULATORMODE0	32
#define MC34708_REGULATORMODE0_VGEN1EN	0
#define MC34708_REGULATORMODE0_VUSBEN	3
#define MC34708_REGULATORMODE0_VDACEN	4
#define MC34708_REGULATORMODE0_VREFDDREN	10
#define MC34708_REGULATORMODE0_VGEN2EN	12
#define MC34708_REGULATORMODE0_VPLLEN	15
#define MC34708_REGULATORMODE0_VUSB2EN	18

#define MC34708_USBCONTROL	39
#define MC34708_USBCONTROL_SWHOLD_M	(0x1<<12)
#define MC34708_USBCONTROL_SWHOLD_NORM	(0x0<<12)

static struct mc34708_regulator mc34708_regulators[] = {
	MC34708_SW_DEFINE(SW1A, SW1ABVOL, SW1ABVOL, mc34708_sw1A),
	MC34708_SW_DEFINE(SW1B, SW1ABVOL, SW1ABVOL, mc34708_sw1A),
	MC34708_SW_DEFINE(SW2, SW23VOL, SW23VOL, mc34708_sw2),
	MC34708_SW_DEFINE(SW3, SW23VOL, SW23VOL, mc34708_sw3),
	MC34708_SW4_DEFINE(SW4A, SW4ABVOL, SW4ABVOL, mc34708_sw4A),
	MC34708_SW4_DEFINE(SW4B, SW4ABVOL, SW4ABVOL, mc34708_sw4A),
	MC34708_SW_DEFINE(SW5, SW5VOL, SW5VOL, mc34708_sw5),
	MC34708_SW_DEFINE(SWBST, SWBSTCONTROL, SWBSTCONTROL, mc34708_swbst),
	MC34708_DEFINE_REGU(VPLL, REGULATORMODE0, REGULATORSET0, mc34708_vpll),
	MC34708_FIXED_VOL_DEFINE(VREFDDR, REGULATORMODE0, mc34708_vrefddr),
	MC34708_FIXED_VOL_DEFINE(VUSB, REGULATORMODE0, mc34708_vusb),
	MC34708_DEFINE_REGU(VUSB2, REGULATORMODE0, REGULATORSET0,
			    mc34708_vusb2),
	MC34708_DEFINE_REGU(VDAC, REGULATORMODE0, REGULATORSET0, mc34708_vdac),
	MC34708_DEFINE_REGU(VGEN1, REGULATORMODE0, REGULATORSET0,
			    mc34708_vgen1),
	MC34708_DEFINE_REGU(VGEN2, REGULATORMODE0, REGULATORSET0,
			    mc34708_vgen2),
};

static int mc34708_regulator_enable(struct regulator_dev *rdev)
{
	struct mc34708_regulator_priv *priv = rdev_get_drvdata(rdev);
	struct mc34708_regulator *mc34708_regulators = priv->mc34708_regulators;
	int id = rdev_get_id(rdev);
	int ret;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);

	mc_pmic_lock(priv->mc34708);
	ret = mc_pmic_reg_rmw(priv->mc34708, mc34708_regulators[id].reg,
			      mc34708_regulators[id].enable_bit,
			      mc34708_regulators[id].enable_bit);
	mc_pmic_unlock(priv->mc34708);

	return ret;
}

static int mc34708_regulator_disable(struct regulator_dev *rdev)
{
	struct mc34708_regulator_priv *priv = rdev_get_drvdata(rdev);
	struct mc34708_regulator *mc34708_regulators = priv->mc34708_regulators;
	int id = rdev_get_id(rdev);
	int ret;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);

	mc_pmic_lock(priv->mc34708);
	ret = mc_pmic_reg_rmw(priv->mc34708, mc34708_regulators[id].reg,
			      mc34708_regulators[id].enable_bit, 0);
	mc_pmic_unlock(priv->mc34708);

	return ret;
}

static int mc34708_regulator_is_enabled(struct regulator_dev *rdev)
{
	struct mc34708_regulator_priv *priv = rdev_get_drvdata(rdev);
	struct mc34708_regulator *mc34708_regulators = priv->mc34708_regulators;
	int ret, id = rdev_get_id(rdev);
	unsigned int val;

	mc_pmic_lock(priv->mc34708);
	ret = mc_pmic_reg_read(priv->mc34708, mc34708_regulators[id].reg, &val);
	mc_pmic_unlock(priv->mc34708);

	if (ret)
		return ret;

	return (val & mc34708_regulators[id].enable_bit) != 0;
}

int
mc34708_regulator_list_voltage(struct regulator_dev *rdev, unsigned selector)
{
	int id = rdev_get_id(rdev);
	struct mc34708_regulator_priv *priv = rdev_get_drvdata(rdev);
	struct mc34708_regulator *mc34708_regulators = priv->mc34708_regulators;

	if (selector >= mc34708_regulators[id].desc.n_voltages)
		return -EINVAL;

	return mc34708_regulators[id].voltages[selector];
}

EXPORT_SYMBOL_GPL(mc34708_regulator_list_voltage);

int
mc34708_get_best_voltage_index(struct regulator_dev *rdev,
			       int min_uV, int max_uV)
{
	struct mc34708_regulator_priv *priv = rdev_get_drvdata(rdev);
	struct mc34708_regulator *mc34708_regulators = priv->mc34708_regulators;
	int reg_id = rdev_get_id(rdev);
	int i;
	int bestmatch;
	int bestindex;

	/*
	 * Locate the minimum voltage fitting the criteria on
	 * this regulator. The switchable voltages are not
	 * in strict falling order so we need to check them
	 * all for the best match.
	 */
	bestmatch = INT_MAX;
	bestindex = -1;
	for (i = 0; i < mc34708_regulators[reg_id].desc.n_voltages; i++) {
		if (mc34708_regulators[reg_id].voltages[i] >= min_uV &&
		    mc34708_regulators[reg_id].voltages[i] < bestmatch) {
			bestmatch = mc34708_regulators[reg_id].voltages[i];
			bestindex = i;
		}
	}

	if (bestindex < 0 || bestmatch > max_uV) {
		dev_warn(&rdev->dev, "no possible value for %d<=x<=%d uV\n",
			 min_uV, max_uV);
		return -EINVAL;
	}
	return bestindex;
}

EXPORT_SYMBOL_GPL(mc34708_get_best_voltage_index);

static int
mc34708_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
			      int max_uV, unsigned *selector)
{
	struct mc34708_regulator_priv *priv = rdev_get_drvdata(rdev);
	struct mc34708_regulator *mc34708_regulators = priv->mc34708_regulators;
	int value, id = rdev_get_id(rdev);
	int ret;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
		__func__, id, min_uV, max_uV);

	/* Find the best index */
	value = mc34708_get_best_voltage_index(rdev, min_uV, max_uV);
	dev_dbg(rdev_get_dev(rdev), "%s best value: %d\n", __func__, value);
	if (value < 0)
		return value;

	mc_pmic_lock(priv->mc34708);
	ret = mc_pmic_reg_rmw(priv->mc34708, mc34708_regulators[id].vsel_reg,
			      mc34708_regulators[id].vsel_mask,
			      value << mc34708_regulators[id].vsel_shift);
	mc_pmic_unlock(priv->mc34708);

	return ret;
}

static int mc34708_regulator_get_voltage(struct regulator_dev *rdev)
{
	struct mc34708_regulator_priv *priv = rdev_get_drvdata(rdev);
	struct mc34708_regulator *mc34708_regulators = priv->mc34708_regulators;
	int ret, id = rdev_get_id(rdev);
	unsigned int val;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);

	mc_pmic_lock(priv->mc34708);
	ret = mc_pmic_reg_read(priv->mc34708,
			       mc34708_regulators[id].vsel_reg, &val);
	mc_pmic_unlock(priv->mc34708);

	if (ret)
		return ret;

	val = (val & mc34708_regulators[id].vsel_mask)
	    >> mc34708_regulators[id].vsel_shift;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);

	BUG_ON(val > mc34708_regulators[id].desc.n_voltages);

	return mc34708_regulators[id].voltages[val];
}

static struct regulator_ops mc34708_regulator_ops = {
	.enable = mc34708_regulator_enable,
	.disable = mc34708_regulator_disable,
	.is_enabled = mc34708_regulator_is_enabled,
	.list_voltage = mc34708_regulator_list_voltage,
	.set_voltage = mc34708_regulator_set_voltage,
	.get_voltage = mc34708_regulator_get_voltage,
};

EXPORT_SYMBOL_GPL(mc34708_regulator_ops);

int
mc34708_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
				    int max_uV, unsigned *selector)
{
	struct mc34708_regulator_priv *priv = rdev_get_drvdata(rdev);
	struct mc34708_regulator *mc34708_regulators = priv->mc34708_regulators;
	int id = rdev_get_id(rdev);

	dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
		__func__, id, min_uV, max_uV);

	if (min_uV >= mc34708_regulators[id].voltages[0] &&
	    max_uV <= mc34708_regulators[id].voltages[0])
		return 0;
	else
		return -EINVAL;
}

EXPORT_SYMBOL_GPL(mc34708_fixed_regulator_set_voltage);

int mc34708_fixed_regulator_get_voltage(struct regulator_dev *rdev)
{
	struct mc34708_regulator_priv *priv = rdev_get_drvdata(rdev);
	struct mc34708_regulator *mc34708_regulators = priv->mc34708_regulators;
	int id = rdev_get_id(rdev);

	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);

	return mc34708_regulators[id].voltages[0];
}

EXPORT_SYMBOL_GPL(mc34708_fixed_regulator_get_voltage);

static struct regulator_ops mc34708_fixed_regulator_ops = {
	.enable = mc34708_regulator_enable,
	.disable = mc34708_regulator_disable,
	.is_enabled = mc34708_regulator_is_enabled,
	.list_voltage = mc34708_regulator_list_voltage,
	.set_voltage = mc34708_fixed_regulator_set_voltage,
	.get_voltage = mc34708_fixed_regulator_get_voltage,
};

EXPORT_SYMBOL_GPL(mc34708_fixed_regulator_ops);

int mc34708_sw_regulator_is_enabled(struct regulator_dev *rdev)
{
	return 1;
}

EXPORT_SYMBOL_GPL(mc34708_sw_regulator_is_enabled);

static int mc34708_sw4_regulator_get_voltage(struct regulator_dev *rdev)
{
	struct mc34708_regulator_priv *priv = rdev_get_drvdata(rdev);
	struct mc34708_regulator *mc34708_regulators = priv->mc34708_regulators;
	int ret, id = rdev_get_id(rdev);
	unsigned int val, hi;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);

	mc_pmic_lock(priv->mc34708);
	ret = mc_pmic_reg_read(priv->mc34708,
			       mc34708_regulators[id].vsel_reg, &val);
	mc_pmic_unlock(priv->mc34708);

	if (ret)
		return ret;
	hi = (val & MC34708_SW4ABVOL_SW4BHI_M) >> MC34708_SW4ABVOL_SW4BHI;
	val = (val & mc34708_regulators[id].vsel_mask)
	    >> mc34708_regulators[id].vsel_shift;
	dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);

	if (hi == 0x1)		/*2500000 */
		val = 27;
	else if (hi == 0x2)	/*3150000 */
		val = 28;

	return mc34708_regulators[id].voltages[val];
}

static int
mc34708_sw4_regulator_set_voltage(struct regulator_dev *rdev,
				  int min_uV, int max_uV, unsigned *selector)
{
	struct mc34708_regulator_priv *priv = rdev_get_drvdata(rdev);
	struct mc34708_regulator *mc34708_regulators = priv->mc34708_regulators;
	int value, id = rdev_get_id(rdev);
	int ret, hi;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
		__func__, id, min_uV, max_uV);

	/* Find the best index */
	value = mc34708_get_best_voltage_index(rdev, min_uV, max_uV);
	dev_dbg(rdev_get_dev(rdev), "%s best value: %d\n", __func__, value);
	if (value < 0)
		return value;
	if (value <= 26)
		hi = 0x0;
	else if (value == 27)
		hi = 0x1;
	else
		hi = 0x2;
	mc_pmic_lock(priv->mc34708);
	ret = mc_pmic_reg_rmw(priv->mc34708, mc34708_regulators[id].vsel_reg,
			      mc34708_regulators[id].vsel_mask |
			      MC34708_SW4ABVOL_SW4BHI_M,
			      value << mc34708_regulators[id].vsel_shift |
			      (hi << MC34708_SW4ABVOL_SW4BHI));
	mc_pmic_unlock(priv->mc34708);

	return ret;
}

static struct regulator_ops mc34708_sw4_regulator_ops = {
	.is_enabled = mc34708_sw_regulator_is_enabled,
	.list_voltage = mc34708_regulator_list_voltage,
	.set_voltage = mc34708_sw4_regulator_set_voltage,
	.get_voltage = mc34708_sw4_regulator_get_voltage,
};

static struct regulator_ops mc34708_sw_regulator_ops = {
	.is_enabled = mc34708_sw_regulator_is_enabled,
	.list_voltage = mc34708_regulator_list_voltage,
	.set_voltage = mc34708_regulator_set_voltage,
	.get_voltage = mc34708_regulator_get_voltage,
};

static int __devinit mc34708_regulator_probe(struct platform_device *pdev)
{
	struct mc34708_regulator_priv *priv;
	struct mc_pmic *mc34708 = dev_get_drvdata(pdev->dev.parent);
	struct mc_pmic_regulator_platform_data *pdata =
	    dev_get_platdata(&pdev->dev);
	struct mc_pmic_regulator_init_data *init_data;
	int i, ret;
	u32 val = 0;

	priv = kzalloc(sizeof(*priv) +
		       pdata->num_regulators * sizeof(priv->regulators[0]),
		       GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->mc34708_regulators = mc34708_regulators;
	priv->mc34708 = mc34708;

	mc_pmic_lock(mc34708);
	ret = mc_pmic_reg_read(mc34708, MC34708_REVISION, &val);
	if (ret)
		goto err_free;
	ret = mc_pmic_reg_rmw(mc34708, MC34708_SW12OP,
			      MC34708_SW12OP_SW1AMODE_M |
			      MC34708_SW12OP_SW2MODE_M,
			      MC34708_SW12OP_SW1AMODE_VALUE |
			      MC34708_SW12OP_SW2MODE_VALUE);
	if (ret)
		goto err_free;
	ret = mc_pmic_reg_rmw(mc34708, MC34708_SW345OP,
			      MC34708_SW345OP_SW3MODE_M |
			      MC34708_SW345OP_SW4AMODE_M |
			      MC34708_SW345OP_SW4BMODE_M |
			      MC34708_SW345OP_SW5MODE_M,
			      MC34708_SW345OP_SW3MODE_VALUE |
			      MC34708_SW345OP_SW4AMODE_VALUE |
			      MC34708_SW345OP_SW4BMODE_VALUE |
			      MC34708_SW345OP_SW5MODE_VALUE);
	if (ret)
		goto err_free;
	ret = mc_pmic_reg_rmw(mc34708, MC34708_SWBSTCONTROL,
			      MC34708_SWBSTCONTROL_SWBSTMODE_M,
			      MC34708_SWBSTCONTROL_SWBSTMODE_VALUE);
	if (ret)
		goto err_free;
	ret = mc_pmic_reg_rmw(mc34708, MC34708_USBCONTROL,
			      MC34708_USBCONTROL_SWHOLD_M,
			      MC34708_USBCONTROL_SWHOLD_NORM);
	if (ret)
		goto err_free;
	mc_pmic_unlock(mc34708);
	dev_dbg(&pdev->dev, "PMIC MC34708 ID:0x%x\n", val);
	for (i = 0; i < pdata->num_regulators; i++) {
		init_data = &pdata->regulators[i];
		priv->regulators[i] =
		    regulator_register(&mc34708_regulators[init_data->id].desc,
				       &pdev->dev, init_data->init_data, priv);

		if (IS_ERR(priv->regulators[i])) {
			dev_err(&pdev->dev, "failed to register regulator %s\n",
				mc34708_regulators[i].desc.name);
			ret = PTR_ERR(priv->regulators[i]);
			goto err;
		}
	}

	platform_set_drvdata(pdev, priv);

	return 0;
 err:
	while (--i >= 0)
		regulator_unregister(priv->regulators[i]);

 err_free:
	mc_pmic_unlock(mc34708);
	kfree(priv);

	return ret;
}

static int __devexit mc34708_regulator_remove(struct platform_device *pdev)
{
	struct mc34708_regulator_priv *priv = platform_get_drvdata(pdev);
	struct mc_pmic_regulator_platform_data *pdata =
	    dev_get_platdata(&pdev->dev);
	int i;

	platform_set_drvdata(pdev, NULL);

	for (i = 0; i < pdata->num_regulators; i++)
		regulator_unregister(priv->regulators[i]);

	kfree(priv);
	return 0;
}

static struct platform_driver mc34708_regulator_driver = {
	.driver = {
		   .name = "mc34708-regulator",
		   .owner = THIS_MODULE,
		   },
	.remove = __devexit_p(mc34708_regulator_remove),
	.probe = mc34708_regulator_probe,
};

static int __init mc34708_regulator_init(void)
{
	return platform_driver_register(&mc34708_regulator_driver);
}

subsys_initcall(mc34708_regulator_init);

static void __exit mc34708_regulator_exit(void)
{
	platform_driver_unregister(&mc34708_regulator_driver);
}

module_exit(mc34708_regulator_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("Regulator Driver for Freescale MC34708 PMIC");
MODULE_ALIAS("mc34708-regulator");