summaryrefslogtreecommitdiff
path: root/drivers/regulator/pfuze100-regulator.c
blob: 17a8da1e2099c99d36eea75571d9db63356d72fb (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
/*
 * Copyright (C) 2011-2012 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/pfuze.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 "pfuze-regulator.h"

static const int pfuze100_sw1[] = {
#ifdef PFUZE100_FIRST_VERSION
	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,
#else
	300000, 325000, 350000, 375000, 400000, 425000, 450000, 475000,
	500000, 525000, 550000, 575000, 600000, 625000, 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, 1450000, 1475000,
	1500000, 1525000, 1550000, 1575000, 1600000, 1625000, 1650000, 1675000,
	1700000, 1725000, 1750000, 1775000, 1800000, 1825000, 1850000, 1875000,
#endif
};

#if  PFUZE100_SW2_VOL6
static const int pfuze100_sw2[] = {
	800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
	1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
	1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 1950000,
	2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000, 2350000,
	2400000, 2450000, 2500000, 2550000, 2600000, 2650000, 2700000, 2750000,
	2800000, 2850000, 2900000, 2950000, 3000000, 3050000, 3100000, 3150000,
	3200000, 3250000, 3300000, 3300000, 3300000, 3300000, 3300000, 3300000,
	3300000, 3300000, 3300000, 3300000, 3300000, 3300000, 3300000, 3950000,
};
#else
static const int pfuze100_sw2[] = {
	400000, 425000, 450000, 475000, 500000, 525000, 550000, 575000,
	600000, 625000, 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, 1450000, 1475000, 1500000, 1525000, 1550000, 1575000,
	1600000, 1625000, 1650000, 1675000, 1700000, 1725000, 1750000, 1775000,
	1800000, 1825000, 1850000, 1875000, 1900000, 1925000, 1950000, 1975000,
};
#endif

#if PFUZE100_SW3_VOL6
static const int pfuze100_sw3[] = {
	800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
	1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
	1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 1950000,
	2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000, 2350000,
	2400000, 2450000, 2500000, 2550000, 2600000, 2650000, 2700000, 2750000,
	2800000, 2850000, 2900000, 2950000, 3000000, 3050000, 3100000, 3150000,
	3200000, 3250000, 3300000, 3300000, 3300000, 3300000, 3300000, 3300000,
	3300000, 3300000, 3300000, 3750000, 3800000, 3850000, 3900000, 3950000,
};
#else
static const int pfuze100_sw3[] = {
	400000, 425000, 450000, 475000, 500000, 525000, 550000, 575000,
	600000, 625000, 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, 1450000, 1475000, 1500000, 1525000, 1550000, 1575000,
	1600000, 1625000, 1650000, 1675000, 1700000, 1725000, 1750000, 1775000,
	1800000, 1825000, 1850000, 1875000, 1900000, 1925000, 1950000, 1975000,
};
#endif

#if PFUZE100_SW4_VOL6
static const int pfuze100_sw4[] = {
	800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
	1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
	1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 1950000,
	2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000, 2350000,
	2400000, 2450000, 2500000, 2550000, 2600000, 2650000, 2700000, 2750000,
	2800000, 2850000, 2900000, 2950000, 3000000, 3050000, 3100000, 3150000,
	3200000, 3250000, 3300000, 3300000, 3300000, 3300000, 3300000, 3300000,
	3300000, 3300000, 3300000, 3300000, 3300000, 3300000, 3300000, 3950000,
};

#else
static const int pfuze100_sw4[] = {
	400000, 425000, 450000, 475000, 500000, 525000, 550000, 575000,
	600000, 625000, 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, 1450000, 1475000, 1500000, 1525000, 1550000, 1575000,
	1600000, 1625000, 1650000, 1675000, 1700000, 1725000, 1750000, 1775000,
	1800000, 1825000, 1850000, 1875000, 1900000, 1925000, 1950000, 1975000,
};
#endif

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

static const int pfuze100_vsnvs[] = {
	1200000, 1500000, 1800000, 3000000,
};

static const int pfuze100_vrefddr[] = {
	750000,
};

static const int pfuze100_vgen12[] = {

#ifdef PFUZE100_FIRST_VERSION
	1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
#else
	800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000,
	1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
#endif
};

static const int pfuze100_vgen36[] = {
	1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
	2600000, 2700000, 2800000, 2900000, 3000000, 3100000, 3200000, 3300000,
};

static struct regulator_ops pfuze100_ldo_regulator_ops;
static struct regulator_ops pfuze100_fixed_regulator_ops;
static struct regulator_ops pfuze100_sw_regulator_ops;

#define PFUZE100_FIXED_VOL_DEFINE(name,	reg, voltages)		\
	PFUZE_FIXED_DEFINE(PFUZE100_, name, reg, voltages,	\
			pfuze100_fixed_regulator_ops)

#define PFUZE100_SW_DEFINE(name, reg, voltages)	\
	PFUZE_SW_DEFINE(PFUZE100_, name, reg, voltages,	\
			pfuze100_sw_regulator_ops)

#define PFUZE100_SWBST_DEFINE(name, reg, voltages)	\
	PFUZE_SWBST_DEFINE(PFUZE100_, name, reg, voltages,	\
			pfuze100_sw_regulator_ops)

#define PFUZE100_VGEN_DEFINE(name, reg, voltages)	\
	PFUZE_DEFINE(PFUZE100_, name, reg, voltages,	\
			pfuze100_ldo_regulator_ops)
/* SW1A */
#define PFUZE100_SW1AVOL	32
#define PFUZE100_SW1AVOL_VSEL	0
#define PFUZE100_SW1AVOL_VSEL_M	(0x3f<<0)

#define PFUZE100_SW1AVOL_STBY	33
#define PFUZE100_SW1AVOL_STBY_VSEL	0
#define PFUZE100_SW1AVOL_STBY_VSEL_M	(0x3f<<0)

#define PFUZE100_SW1AOFF	34
#define PFUZE100_SW1AOFF_OFF_VAL	(0x0<<0)
#define PFUZE100_SW1AOFF_OFF_M	(0x3f<<0)

#define PFUZE100_SW1AMODE	35
#define PFUZE100_SW1AMODE_OMODE_VAL	(0x0<<5)
#define PFUZE100_SW1AMODE_OMODE_M	(0x1<<5)
#define PFUZE100_SW1AMODE_MODE_VAL	0x7	/*Auto */
#define PFUZE100_SW1AMODE_MODE_M	(0xf<<0)

#define PFUZE100_SW1ACON		36
#define PFUZE100_SW1ACON_SPEED_VAL	(0x1<<6)	/*default */
#define PFUZE100_SW1ACON_SPEED_M	(0x3<<6)
#define PFUZE100_SW1ACON_PHASE_VAL	(0x1<<4)	/*default */
#define PFUZE100_SW1ACON_PHASE_M	(0x3<<4)
#define PFUZE100_SW1ACON_FREQ_VAL	(0x1<<2)	/*1Mhz */
#define PFUZE100_SW1ACON_FREQ_M	(0x3<<2)
#define PFUZE100_SW1ACON_LIM_VAL	(0x0<<0)	/*2Imax */
#define PFUZE100_SW1ACON_LIM_M	(0x3<<0)

/*SW1B*/
#define PFUZE100_SW1BVOL	39
#define PFUZE100_SW1BVOL_VSEL	0
#define PFUZE100_SW1BVOL_VSEL_M	(0x3f<<0)

#define PFUZE100_SW1BVOL_STBY	40
#define PFUZE100_SW1BVOL_STBY_VSEL	0
#define PFUZE100_SW1BVOL_STBY_VSEL_M	(0x3f<<0)

#define PFUZE100_SW1BOFF	41
#define PFUZE100_SW1BOFF_OFF_VAL	0x0
#define PFUZE100_SW1BOFF_OFF_M	(0x3f<<0)

#define PFUZE100_SW1BMODE	42
#define PFUZE100_SW1BMODE_OMODE_VAL	(0x0<<5)
#define PFUZE100_SW1BMODE_OMODE_M	(0x1<<5)
#define PFUZE100_SW1BMODE_MODE_VAL	(0x7<<0)
#define PFUZE100_SW1BMODE_MODE_M	(0xf<<0)

#define PFUZE100_SW1BCON		43
#define PFUZE100_SW1BCON_SPEED_VAL	(0x1<<6)
#define PFUZE100_SW1BCON_SPEED_M	(0x3<<6)
#define PFUZE100_SW1BCON_PHASE_VAL	(0x1<<4)
#define PFUZE100_SW1BCON_PHASE_M	(0x3<<4)
#define PFUZE100_SW1BCON_FREQ_VAL	(0x1<<2)
#define PFUZE100_SW1BCON_FREQ_M	(0x3<<2)
#define PFUZE100_SW1BCON_LIM_VAL	(0x0<<0)
#define PFUZE100_SW1BCON_LIM_M	(0x3<<0)

/*SW1C*/
#define PFUZE100_SW1CVOL	46
#define PFUZE100_SW1CVOL_VSEL	0
#define PFUZE100_SW1CVOL_VSEL_M	(0x3f<<0)

#define PFUZE100_SW1CVOL_STBY	47
#define PFUZE100_SW1CVOL_STBY_VSEL	0
#define PFUZE100_SW1CVOL_STBY_VSEL_M	(0x3f<<0)

#define PFUZE100_SW1COFF	48
#define PFUZE100_SW1COFF_OFF_VAL	0x0
#define PFUZE100_SW1COFF_OFF_M	(0x3f<<0)

#define PFUZE100_SW1CMODE	49
#define PFUZE100_SW1CMODE_OMODE_VAL	(0x0<<5)
#define PFUZE100_SW1CMODE_OMODE_M	(0x1<<5)
#define PFUZE100_SW1CMODE_MODE_VAL	(0x7<<0)
#define PFUZE100_SW1CMODE_MODE_M	(0xf<<0)

#define PFUZE100_SW1CCON		50
#define PFUZE100_SW1CCON_SPEED_VAL	(0x1<<6)
#define PFUZE100_SW1CCON_SPEED_M	(0x3<<6)
#define PFUZE100_SW1CCON_PHASE_VAL	(0x1<<4)
#define PFUZE100_SW1CCON_PHASE_M	(0x3<<4)
#define PFUZE100_SW1CCON_FREQ_VAL	(0x1<<2)
#define PFUZE100_SW1CCON_FREQ_M		(0x3<<2)
#define PFUZE100_SW1CCON_LIM_VAL	(0x0<<0)
#define PFUZE100_SW1CCON_LIM_M		(0x3<<0)

/*SW2*/
#define PFUZE100_SW2VOL		53
#define PFUZE100_SW2VOL_VSEL	0
#define PFUZE100_SW2VOL_VSEL_M	(0x3f<<0)

#define PFUZE100_SW2VOL_STBY		54
#define PFUZE100_SW2VOL_STBY_VSEL	0
#define PFUZE100_SW2VOL_STBY_VSEL_M	(0x3f<<0)

#define PFUZE100_SW2OFF	55
#define PFUZE100_SW2OFF_OFF_VAL	0x0
#define PFUZE100_SW2OFF_OFF_M	(0x7f<<0)

#define PFUZE100_SW2MODE	56
#define PFUZE100_SW2MODE_OMODE_VAL	(0x0<<5)
#define PFUZE100_SW2MODE_OMODE_M	(0x1<<5)
#define PFUZE100_SW2MODE_MODE_VAL	(0x7<<0)
#define PFUZE100_SW2MODE_MODE_M		(0xf<<0)

#define PFUZE100_SW2CON		57
#define PFUZE100_SW2CON_SPEED_VAL	(0x1<<6)
#define PFUZE100_SW2CON_SPEED_M		(0x3<<6)
#define PFUZE100_SW2CON_PHASE_VAL	(0x1<<4)
#define PFUZE100_SW2CON_PHASE_M		(0x3<<4)
#define PFUZE100_SW2CON_FREQ_VAL	(0x1<<2)
#define PFUZE100_SW2CON_FREQ_M		(0x3<<2)
#define PFUZE100_SW2CON_LIM_VAL		(0x0<<0)
#define PFUZE100_SW2CON_LIM_M		(0x3<<0)

/*SW3A*/
#define PFUZE100_SW3AVOL	60
#define PFUZE100_SW3AVOL_VSEL	0
#define PFUZE100_SW3AVOL_VSEL_M	(0x3f<<0)

#define PFUZE100_SW3AVOL_STBY	61
#define PFUZE100_SW3AVOL_STBY_VSEL	0
#define PFUZE100_SW3AVOL_STBY_VSEL_M	(0x3f<<0)

#define PFUZE100_SW3AOFF	62
#define PFUZE100_SW3AOFF_OFF_VAL	0x0
#define PFUZE100_SW3AOFF_OFF_M	(0x3f<<0)

#define PFUZE100_SW3AMODE	63
#define PFUZE100_SW3AMODE_OMODE_VAL	(0x0<<5)
#define PFUZE100_SW3AMODE_OMODE_M	(0x1<<5)
#define PFUZE100_SW3AMODE_MODE_VAL	(0x7<<0)
#define PFUZE100_SW3AMODE_MODE_M	(0xf<<0)

#define PFUZE100_SW3ACON		64
#define PFUZE100_SW3ACON_SPEED_VAL	(0x1<<6)
#define PFUZE100_SW3ACON_SPEED_M	(0x3<<6)
#define PFUZE100_SW3ACON_PHASE_VAL	(0x1<<4)
#define PFUZE100_SW3ACON_PHASE_M	(0x3<<4)
#define PFUZE100_SW3ACON_FREQ_VAL	(0x1<<2)
#define PFUZE100_SW3ACON_FREQ_M		(0x3<<2)
#define PFUZE100_SW3ACON_LIM_VAL	(0x0<<0)
#define PFUZE100_SW3ACON_LIM_M		(0x3<<0)

/*SW3B*/
#define PFUZE100_SW3BVOL	67
#define PFUZE100_SW3BVOL_VSEL	0
#define PFUZE100_SW3BVOL_VSEL_M	(0x3f<<0)

#define PFUZE100_SW3BVOL_STBY	68
#define PFUZE100_SW3BVOL_STBY_VSEL	0
#define PFUZE100_SW3BVOL_STBY_VSEL_M	(0x3f<<0)

#define PFUZE100_SW3BOFF	69
#define PFUZE100_SW3BOFF_OFF_VAL	0x0
#define PFUZE100_SW3BOFF_OFF_M	(0x3f<<0)

#define PFUZE100_SW3BMODE	70
#define PFUZE100_SW3BMODE_OMODE_VAL	(0x0<<5)
#define PFUZE100_SW3BMODE_OMODE_M	(0x1<<5)
#define PFUZE100_SW3BMODE_MODE_VAL	(0x7<<0)
#define PFUZE100_SW3BMODE_MODE_M	(0xf<<0)

#define PFUZE100_SW3BCON		71
#define PFUZE100_SW3BCON_SPEED_VAL	(0x1<<6)
#define PFUZE100_SW3BCON_SPEED_M	(0x3<<6)
#define PFUZE100_SW3BCON_PHASE_VAL	(0x1<<4)
#define PFUZE100_SW3BCON_PHASE_M	(0x3<<4)
#define PFUZE100_SW3BCON_FREQ_VAL	(0x1<<2)
#define PFUZE100_SW3BCON_FREQ_M		(0x3<<2)
#define PFUZE100_SW3BCON_LIM_VAL	(0x0<<0)
#define PFUZE100_SW3BCON_LIM_M		(0x3<<0)

/*SW4*/
#define PFUZE100_SW4VOL		74
#define PFUZE100_SW4VOL_VSEL	0
#define PFUZE100_SW4VOL_VSEL_M	(0x3f<<0)

#define PFUZE100_SW4VOL_STBY		75
#define PFUZE100_SW4VOL_STBY_VSEL	0
#define PFUZE100_SW4VOL_STBY_VSEL_M	(0x3f<<0)

#define PFUZE100_SW4OFF		76
#define PFUZE100_SW4OFF_OFF_VAL	0x0
#define PFUZE100_SW4OFF_OFF_M	(0x3f<<0)

#define PFUZE100_SW4MODE	77
#define PFUZE100_SW4MODE_OMODE_VAL	(0x0<<5)
#define PFUZE100_SW4MODE_OMODE_M	(0x1<<5)
#define PFUZE100_SW4MODE_MODE_VAL	(0x7<<0)
#define PFUZE100_SW4MODE_MODE_M		(0xf<<0)

#define PFUZE100_SW4CON		78
#define PFUZE100_SW4CON_SPEED_VAL	(0x1<<6)
#define PFUZE100_SW4CON_SPEED_M		(0x3<<6)
#define PFUZE100_SW4CON_PHASE_VAL	(0x1<<4)
#define PFUZE100_SW4CON_PHASE_M		(0x3<<4)
#define PFUZE100_SW4CON_FREQ_VAL	(0x1<<2)
#define PFUZE100_SW4CON_FREQ_M		(0x3<<2)
#define PFUZE100_SW4CON_LIM_VAL		(0x0<<0)
#define PFUZE100_SW4CON_LIM_M		(0x3<<0)

 /*SWBST*/
#define PFUZE100_SWBSTCON1	102
#define PFUZE100_SWBSTCON1_SWBSTMOD_VAL	(0x1<<2)
#define PFUZE100_SWBSTCON1_SWBSTMOD_M	(0x3<<2)
#define PFUZE100_SWBSTCON1_VSEL	0
#define PFUZE100_SWBSTCON1_VSEL_M	(0x3<<0)
     /*VREFDDR*/
#define PFUZE100_VREFDDRCON	106
#define PFUZE100_VREFDDRCON_EN	(0x1<<4)
     /*VSNVS*/
#define PFUZE100_VSNVSVOL		107
#define PFUZE100_VSNVSVOL_VSEL	0
#define PFUZE100_VSNVSVOL_VSEL_M	(0x3<<0)
/*VGEN1*/
#define PFUZE100_VGEN1VOL		108
#define PFUZE100_VGEN1VOL_STBY	(0x1<<5)
#define PFUZE100_VGEN1VOL_EN	(0x1<<4)
#define PFUZE100_VGEN1VOL_VSEL	0
#ifdef PFUZE100_FIRST_VERSION
#define PFUZE100_VGEN1VOL_VSEL_M	(0x7<<0)
#else
#define PFUZE100_VGEN1VOL_VSEL_M	(0xf<<0)
#endif
/*VGEN2*/
#define PFUZE100_VGEN2VOL		109
#define PFUZE100_VGEN2VOL_STBY	(0x1<<5)
#define PFUZE100_VGEN2VOL_EN	(0x1<<4)
#define PFUZE100_VGEN2VOL_VSEL	0
#ifdef PFUZE100_FIRST_VERSION
#define PFUZE100_VGEN2VOL_VSEL_M	(0x7<<0)
#else
#define PFUZE100_VGEN2VOL_VSEL_M	(0xf<<0)
#endif
/*VGEN3*/
#define PFUZE100_VGEN3VOL		110
#define PFUZE100_VGEN3VOL_STBY	(0x1<<5)
#define PFUZE100_VGEN3VOL_EN	(0x1<<4)
#define PFUZE100_VGEN3VOL_VSEL	0
#define PFUZE100_VGEN3VOL_VSEL_M	(0xf<<0)
/*VGEN4*/
#define PFUZE100_VGEN4VOL		111
#define PFUZE100_VGEN4VOL_STBY	(0x1<<5)
#define PFUZE100_VGEN4VOL_EN	(0x1<<4)
#define PFUZE100_VGEN4VOL_VSEL	0
#define PFUZE100_VGEN4VOL_VSEL_M	(0xf<<0)
/*VGEN5*/
#define PFUZE100_VGEN5VOL		112
#define PFUZE100_VGEN5VOL_STBY	(0x1<<5)
#define PFUZE100_VGEN5VOL_EN	(0x1<<4)
#define PFUZE100_VGEN5VOL_VSEL	0
#define PFUZE100_VGEN5VOL_VSEL_M	(0xf<<0)
/*VGEN6*/
#define PFUZE100_VGEN6VOL		113
#define PFUZE100_VGEN6VOL_STBY	(0x1<<5)
#define PFUZE100_VGEN6VOL_EN	(0x1<<4)
#define PFUZE100_VGEN6VOL_VSEL	0
#define PFUZE100_VGEN6VOL_VSEL_M	(0xf<<0)
static struct pfuze_regulator pfuze100_regulators[] = {
	PFUZE100_SW_DEFINE(SW1A, SW1AVOL, pfuze100_sw1),
	PFUZE100_SW_DEFINE(SW1B, SW1BVOL, pfuze100_sw1),
	PFUZE100_SW_DEFINE(SW1C, SW1CVOL, pfuze100_sw1),
	PFUZE100_SW_DEFINE(SW2, SW2VOL, pfuze100_sw2),
	PFUZE100_SW_DEFINE(SW3A, SW3AVOL, pfuze100_sw3),
	PFUZE100_SW_DEFINE(SW3B, SW3BVOL, pfuze100_sw3),
	PFUZE100_SW_DEFINE(SW4, SW4VOL, pfuze100_sw4),
	PFUZE100_SWBST_DEFINE(SWBST, SWBSTCON1, pfuze100_swbst),
	PFUZE100_SWBST_DEFINE(VSNVS, VSNVSVOL, pfuze100_vsnvs),
	PFUZE100_FIXED_VOL_DEFINE(VREFDDR, VREFDDRCON, pfuze100_vrefddr),
	PFUZE100_VGEN_DEFINE(VGEN1, VGEN1VOL, pfuze100_vgen12),
	PFUZE100_VGEN_DEFINE(VGEN2, VGEN2VOL, pfuze100_vgen12),
	PFUZE100_VGEN_DEFINE(VGEN3, VGEN3VOL, pfuze100_vgen36),
	PFUZE100_VGEN_DEFINE(VGEN4, VGEN4VOL, pfuze100_vgen36),
	PFUZE100_VGEN_DEFINE(VGEN5, VGEN5VOL, pfuze100_vgen36),
	PFUZE100_VGEN_DEFINE(VGEN6, VGEN6VOL, pfuze100_vgen36),
};

static int pfuze100_regulator_enable(struct regulator_dev *rdev)
{
	struct pfuze_regulator_priv *priv = rdev_get_drvdata(rdev);
	int id = rdev_get_id(rdev);
	int ret;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
	pfuze_lock(priv->pfuze);
	ret = pfuze_reg_rmw(priv->pfuze, pfuze100_regulators[id].reg,
			    pfuze100_regulators[id].enable_bit,
			    pfuze100_regulators[id].enable_bit);
	pfuze_unlock(priv->pfuze);
	return ret;
}

static int pfuze100_regulator_disable(struct regulator_dev *rdev)
{
	struct pfuze_regulator_priv *priv = rdev_get_drvdata(rdev);
	int id = rdev_get_id(rdev);
	int ret;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
	pfuze_lock(priv->pfuze);
	ret = pfuze_reg_rmw(priv->pfuze, pfuze100_regulators[id].reg,
			    pfuze100_regulators[id].enable_bit, 0);
	pfuze_unlock(priv->pfuze);
	return ret;
}

static int pfuze100_regulator_is_enabled(struct regulator_dev *rdev)
{
	struct pfuze_regulator_priv *priv = rdev_get_drvdata(rdev);
	int id = rdev_get_id(rdev);
	int ret;
	unsigned char val;

	pfuze_lock(priv->pfuze);
	ret = pfuze_reg_read(priv->pfuze, pfuze100_regulators[id].reg, &val);
	pfuze_unlock(priv->pfuze);
	if (ret)
		return ret;
	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
	return (val & pfuze100_regulators[id].enable_bit) != 0;
}

int pfuze100_regulator_list_voltage(struct regulator_dev *rdev,
				    unsigned selector)
{
	int id = rdev_get_id(rdev);

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

int pfuze100_get_best_voltage_index(struct regulator_dev *rdev, int min_uV,
				    int max_uV)
{
	int reg_id = rdev_get_id(rdev);
	int i, bestmatch, bestindex;

	bestmatch = INT_MAX;
	bestindex = -1;
	for (i = 0; i < pfuze100_regulators[reg_id].desc.n_voltages; i++) {
		if (pfuze100_regulators[reg_id].voltages[i] >= min_uV &&
		    pfuze100_regulators[reg_id].voltages[i] < bestmatch) {
			bestmatch = pfuze100_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(pfuze100_get_best_voltage_index);

static int
pfuze100_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
			       int max_uV, unsigned *selector)
{

	struct pfuze_regulator_priv *priv = rdev_get_drvdata(rdev);
	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 = pfuze100_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;
	pfuze_lock(priv->pfuze);
	ret = pfuze_reg_rmw(priv->pfuze, pfuze100_regulators[id].reg,
			    pfuze100_regulators[id].vsel_mask,
			    value << pfuze100_regulators[id].vsel_shift);
	pfuze_unlock(priv->pfuze);
	return ret;

}

static int pfuze100_regulator_get_voltage(struct regulator_dev *rdev)
{
	struct pfuze_regulator_priv *priv = rdev_get_drvdata(rdev);
	int ret, id = rdev_get_id(rdev);
	unsigned char val;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
	pfuze_lock(priv->pfuze);
	ret = pfuze_reg_read(priv->pfuze, pfuze100_regulators[id].reg, &val);
	pfuze_unlock(priv->pfuze);
	if (ret)
		return ret;
	val = (val & pfuze100_regulators[id].vsel_mask)
	    >> pfuze100_regulators[id].vsel_shift;
	dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
	BUG_ON(val > pfuze100_regulators[id].desc.n_voltages);
	return pfuze100_regulators[id].voltages[val];
}

static int pfuze100_regulator_ldo_standby_enable(struct regulator_dev *rdev)
{
	struct pfuze_regulator_priv *priv = rdev_get_drvdata(rdev);
	int id = rdev_get_id(rdev);
	int ret;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
	pfuze_lock(priv->pfuze);
	ret = pfuze_reg_rmw(priv->pfuze, pfuze100_regulators[id].reg,
			    pfuze100_regulators[id].stby_bit,
			    0);
	pfuze_unlock(priv->pfuze);
	return ret;
}

static int pfuze100_regulator_ldo_standby_disable(struct regulator_dev *rdev)
{
	struct pfuze_regulator_priv *priv = rdev_get_drvdata(rdev);
	int id = rdev_get_id(rdev);
	int ret;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
	pfuze_lock(priv->pfuze);
	ret = pfuze_reg_rmw(priv->pfuze, pfuze100_regulators[id].reg,
			    pfuze100_regulators[id].stby_bit,
			    pfuze100_regulators[id].stby_bit);
	pfuze_unlock(priv->pfuze);
	return ret;
}

static struct regulator_ops pfuze100_ldo_regulator_ops = {
	.enable = pfuze100_regulator_enable,
	.disable = pfuze100_regulator_disable,
	.is_enabled = pfuze100_regulator_is_enabled,
	.list_voltage = pfuze100_regulator_list_voltage,
	.set_voltage = pfuze100_regulator_set_voltage,
	.get_voltage = pfuze100_regulator_get_voltage,
	.set_suspend_enable = pfuze100_regulator_ldo_standby_enable,
	.set_suspend_disable = pfuze100_regulator_ldo_standby_disable,
};

static int pfuze100_fixed_regulator_set_voltage(struct regulator_dev *rdev,
						int min_uV, int max_uV,
						unsigned *selector)
{
	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 >= pfuze100_regulators[id].voltages[0] &&
	    max_uV <= pfuze100_regulators[id].voltages[0])
		return 0;
	else
		return -EINVAL;

}

static int pfuze100_fixed_regulator_get_voltage(struct regulator_dev *rdev)
{
	int id = rdev_get_id(rdev);

	dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
	return pfuze100_regulators[id].voltages[0];
}

static struct regulator_ops pfuze100_fixed_regulator_ops = {
	.enable = pfuze100_regulator_enable,
	.disable = pfuze100_regulator_disable,
	.is_enabled = pfuze100_regulator_is_enabled,
	.set_voltage = pfuze100_fixed_regulator_set_voltage,
	.get_voltage = pfuze100_fixed_regulator_get_voltage,
};

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

static int
pfuze100_regulator_sw_standby_voltage(struct regulator_dev *rdev, int uV)
{

	struct pfuze_regulator_priv *priv = rdev_get_drvdata(rdev);
	int value, id = rdev_get_id(rdev);
	int ret;

	dev_dbg(rdev_get_dev(rdev), "%s id: %d set standby: %d\n",
		__func__, id, uV);
	/* Find the best index */
	value = pfuze100_get_best_voltage_index(rdev, uV, uV);
	if (value < 0)
		return value;
	pfuze_lock(priv->pfuze);
	ret = pfuze_reg_rmw(priv->pfuze, pfuze100_regulators[id].stby_reg,
			    pfuze100_regulators[id].stby_vsel_mask,
			    value << pfuze100_regulators[id].stby_vsel_shift);
	pfuze_unlock(priv->pfuze);
	return ret;

}

static int pfuze100_regulator_sw_standby_enable(struct regulator_dev *rdev)
{
	return 0;
}
static int pfuze100_regulator_sw_standby_disable(struct regulator_dev *rdev)
{
	return 0;
}
static struct regulator_ops pfuze100_sw_regulator_ops = {
	.is_enabled = pfuze100_sw_regulator_is_enabled,
	.list_voltage = pfuze100_regulator_list_voltage,
	.set_voltage = pfuze100_regulator_set_voltage,
	.get_voltage = pfuze100_regulator_get_voltage,
	.set_suspend_enable = pfuze100_regulator_sw_standby_enable,
	.set_suspend_disable = pfuze100_regulator_sw_standby_disable,
	.set_suspend_voltage = pfuze100_regulator_sw_standby_voltage,
};

static int __devinit pfuze100_regulator_probe(struct platform_device *pdev)
{
	struct pfuze_regulator_priv *priv;
	struct mc_pfuze *pfuze100 = dev_get_drvdata(pdev->dev.parent);
	struct pfuze_regulator_platform_data *pdata =
	    dev_get_platdata(&pdev->dev);
	struct pfuze_regulator_init_data *init_data;
	int i, ret;

	priv = kzalloc(sizeof(*priv) +
		       pdata->num_regulators * sizeof(priv->regulators[0]),
		       GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
	priv->pfuze_regulators = pfuze100_regulators;
	priv->pfuze = pfuze100;
	pfuze_lock(pfuze100);
	ret = pdata->pfuze_init(pfuze100);
	if (ret)
		goto err_free;
	pfuze_unlock(pfuze100);
	for (i = 0; i < pdata->num_regulators; i++) {
		init_data = &pdata->regulators[i];
		priv->regulators[i] =
		    regulator_register(&pfuze100_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",
				pfuze100_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:
	pfuze_unlock(pfuze100);
	kfree(priv);
	return ret;
}

static int __devexit pfuze100_regulator_remove(struct platform_device *pdev)
{
	struct pfuze_regulator_priv *priv = platform_get_drvdata(pdev);
	struct pfuze_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 pfuze100_regulator_driver = {
	.driver = {
		   .name = "pfuze100-regulator",
		   .owner = THIS_MODULE,
		   },
	.remove = __devexit_p(pfuze100_regulator_remove),
	.probe = pfuze100_regulator_probe,
};

static int __init pfuze100_regulator_init(void)
{
	return platform_driver_register(&pfuze100_regulator_driver);
}

subsys_initcall(pfuze100_regulator_init);
static void __exit pfuze100_regulator_exit(void)
{
	platform_driver_unregister(&pfuze100_regulator_driver);
}

module_exit(pfuze100_regulator_exit);

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