hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
/*
 * Copyright (C) 2021 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
 
#include <common.h>
#include <dm.h>
#include <errno.h>
#include <power/rk8xx_pmic.h>
#include <power/pmic.h>
#include <power/regulator.h>
 
#ifndef CONFIG_SPL_BUILD
#define ENABLE_DRIVER
#endif
 
/* Not used or exisit register and configure */
#define NA            -1
 
/* rk806 buck*/
#define RK806_BUCK_ON_VSEL(n)        (0x1a + n - 1)
#define RK806_BUCK_SLP_VSEL(n)        (0x24 + n - 1)
#define RK806_BUCK_CONFIG(n)        (0x10 + n - 1)
#define RK806_BUCK_VSEL_MASK        0xff
 
/* RK806 LDO */
#define RK806_NLDO_ON_VSEL(n)        (0x43 + n - 1)
#define RK806_NLDO_SLP_VSEL(n)        (0x48 + n - 1)
#define RK806_NLDO_VSEL_MASK        0xff
#define RK806_PLDO_ON_VSEL(n)        (0x4e + n - 1)
#define RK806_PLDO_SLP_VSEL(n)        (0x54 + n - 1)
#define RK806_PLDO_VSEL_MASK        0xff
 
/* RK806 ENABLE */
#define RK806_POWER_EN(n)        (0x00 + n)
#define RK806_NLDO_EN(n)        (0x03 + n)
#define RK806_PLDO_EN(n)        (0x04 + n)
 
#define RK806_BUCK_SUSPEND_EN        0x06
#define RK806_NLDO_SUSPEND_EN        0x07
#define RK806_PLDO_SUSPEND_EN        0x08
 
#define RK806_RAMP_RATE_MASK1        0xc0
#define RK806_RAMP_RATE_REG1(n)        (0x10 + n)
#define RK806_RAMP_RATE_REG1_8        0xeb
#define RK806_RAMP_RATE_REG9_10        0xea
 
#define RK806_RAMP_RATE_4LSB_PER_1CLK    0x00/* LDO 100mV/uS buck 50mV/us */
#define RK806_RAMP_RATE_2LSB_PER_1CLK    0x01/* LDO 50mV/uS buck 25mV/us */
#define RK806_RAMP_RATE_1LSB_PER_1CLK    0x02/* LDO 25mV/uS buck 12.5mV/us */
#define RK806_RAMP_RATE_1LSB_PER_2CLK    0x03/* LDO 12.5mV/uS buck 6.25mV/us */
 
#define RK806_RAMP_RATE_1LSB_PER_4CLK    0x04/* LDO 6.28/2mV/uS buck 3.125mV/us */
#define RK806_RAMP_RATE_1LSB_PER_8CLK    0x05/* LDO 3.12mV/uS buck 1.56mV/us */
#define RK806_RAMP_RATE_1LSB_PER_13CLK    0x06/* LDO 1.9mV/uS buck 961mV/us */
#define RK806_RAMP_RATE_1LSB_PER_32CLK    0x07/* LDO 0.78mV/uS buck 0.39mV/us */
 
#define RK806_PLDO0_2_MSK(pldo)        (BIT(pldo + 5))
#define RK806_PLDO0_2_SET(pldo)        (BIT(pldo + 1) | RK806_PLDO0_2_MSK(pldo))
#define RK806_PLDO0_2_CLR(pldo)        RK806_PLDO0_2_MSK(pldo)
 
struct rk8xx_reg_info {
   uint min_uv;
   uint step_uv;
   u8 vsel_reg;
   u8 vsel_sleep_reg;
   u8 config_reg;
   u8 vsel_mask;
   u8 min_sel;
   /* only for buck now */
   u8 max_sel;
   u8 range_num;
};
 
static const struct rk8xx_reg_info rk806_buck[] = {
   /* buck 1 */
   {  500000,   6250, RK806_BUCK_ON_VSEL(1), RK806_BUCK_SLP_VSEL(1), RK806_BUCK_CONFIG(1), RK806_BUCK_VSEL_MASK, 0x00, 0x9f, 3},
   {  1500000, 25000, RK806_BUCK_ON_VSEL(1), RK806_BUCK_SLP_VSEL(1), RK806_BUCK_CONFIG(1), RK806_BUCK_VSEL_MASK, 0xa0, 0xec, 3},
   {  3400000,     0, RK806_BUCK_ON_VSEL(1), RK806_BUCK_SLP_VSEL(1), RK806_BUCK_CONFIG(1), RK806_BUCK_VSEL_MASK, 0xed, 0xff, 3},
   /* buck 2 */
   {  500000,   6250, RK806_BUCK_ON_VSEL(2), RK806_BUCK_SLP_VSEL(2), RK806_BUCK_CONFIG(2), RK806_BUCK_VSEL_MASK, 0x00, 0x9f, 3},
   {  1500000, 25000, RK806_BUCK_ON_VSEL(2), RK806_BUCK_SLP_VSEL(2), RK806_BUCK_CONFIG(2), RK806_BUCK_VSEL_MASK, 0xa0, 0xed, 3},
   {  3400000,     0, RK806_BUCK_ON_VSEL(2), RK806_BUCK_SLP_VSEL(2), RK806_BUCK_CONFIG(2), RK806_BUCK_VSEL_MASK, 0xed, 0xff, 3},
   /* buck 3 */
   {  500000,   6250, RK806_BUCK_ON_VSEL(3), RK806_BUCK_SLP_VSEL(3), RK806_BUCK_CONFIG(3), RK806_BUCK_VSEL_MASK, 0x00, 0x9f, 3},
   {  1500000, 25000, RK806_BUCK_ON_VSEL(3), RK806_BUCK_SLP_VSEL(3), RK806_BUCK_CONFIG(3), RK806_BUCK_VSEL_MASK, 0xa0, 0xed, 3},
   {  3400000,     0, RK806_BUCK_ON_VSEL(3), RK806_BUCK_SLP_VSEL(3), RK806_BUCK_CONFIG(3), RK806_BUCK_VSEL_MASK, 0xed, 0xff, 3},
   /* buck 4 */
   {  500000,   6250, RK806_BUCK_ON_VSEL(4), RK806_BUCK_SLP_VSEL(4), RK806_BUCK_CONFIG(4), RK806_BUCK_VSEL_MASK, 0x00, 0x9f, 3},
   {  1500000, 25000, RK806_BUCK_ON_VSEL(4), RK806_BUCK_SLP_VSEL(4), RK806_BUCK_CONFIG(4), RK806_BUCK_VSEL_MASK, 0xa0, 0xed, 3},
   {  3400000,     0, RK806_BUCK_ON_VSEL(4), RK806_BUCK_SLP_VSEL(4), RK806_BUCK_CONFIG(4), RK806_BUCK_VSEL_MASK, 0xed, 0xff, 3},
   /* buck 5 */
   {  500000,   6250, RK806_BUCK_ON_VSEL(5), RK806_BUCK_SLP_VSEL(5), RK806_BUCK_CONFIG(5), RK806_BUCK_VSEL_MASK, 0x00, 0x9f, 3},
   {  1500000, 25000, RK806_BUCK_ON_VSEL(5), RK806_BUCK_SLP_VSEL(5), RK806_BUCK_CONFIG(5), RK806_BUCK_VSEL_MASK, 0xa0, 0xed, 3},
   {  3400000,     0, RK806_BUCK_ON_VSEL(5), RK806_BUCK_SLP_VSEL(5), RK806_BUCK_CONFIG(5), RK806_BUCK_VSEL_MASK, 0xed, 0xff, 3},
   /* buck 6 */
   {  500000,   6250, RK806_BUCK_ON_VSEL(6), RK806_BUCK_SLP_VSEL(6), RK806_BUCK_CONFIG(6), RK806_BUCK_VSEL_MASK, 0x00, 0x9f, 3},
   {  1500000, 25000, RK806_BUCK_ON_VSEL(6), RK806_BUCK_SLP_VSEL(6), RK806_BUCK_CONFIG(6), RK806_BUCK_VSEL_MASK, 0xa0, 0xed, 3},
   {  3400000,     0, RK806_BUCK_ON_VSEL(6), RK806_BUCK_SLP_VSEL(6), RK806_BUCK_CONFIG(6), RK806_BUCK_VSEL_MASK, 0xed, 0xff, 3},
   /* buck 7 */
   {  500000,   6250, RK806_BUCK_ON_VSEL(7), RK806_BUCK_SLP_VSEL(7), RK806_BUCK_CONFIG(7), RK806_BUCK_VSEL_MASK, 0x00, 0x9f, 3},
   {  1500000, 25000, RK806_BUCK_ON_VSEL(7), RK806_BUCK_SLP_VSEL(7), RK806_BUCK_CONFIG(7), RK806_BUCK_VSEL_MASK, 0xa0, 0xed, 3},
   {  3400000,     0, RK806_BUCK_ON_VSEL(7), RK806_BUCK_SLP_VSEL(7), RK806_BUCK_CONFIG(7), RK806_BUCK_VSEL_MASK, 0xed, 0xff, 3},
   /* buck 8 */
   {  500000,   6250, RK806_BUCK_ON_VSEL(8), RK806_BUCK_SLP_VSEL(8), RK806_BUCK_CONFIG(8), RK806_BUCK_VSEL_MASK, 0x00, 0x9f, 3},
   {  1500000, 25000, RK806_BUCK_ON_VSEL(8), RK806_BUCK_SLP_VSEL(8), RK806_BUCK_CONFIG(8), RK806_BUCK_VSEL_MASK, 0xa0, 0xed, 3},
   {  3400000,     0, RK806_BUCK_ON_VSEL(8), RK806_BUCK_SLP_VSEL(8), RK806_BUCK_CONFIG(8), RK806_BUCK_VSEL_MASK, 0xed, 0xff, 3},
   /* buck 9 */
   {  500000,   6250, RK806_BUCK_ON_VSEL(9), RK806_BUCK_SLP_VSEL(9), RK806_BUCK_CONFIG(9), RK806_BUCK_VSEL_MASK, 0x00, 0x9f, 3},
   {  1500000, 25000, RK806_BUCK_ON_VSEL(9), RK806_BUCK_SLP_VSEL(9), RK806_BUCK_CONFIG(9), RK806_BUCK_VSEL_MASK, 0xa0, 0xed, 3},
   {  3400000,     0, RK806_BUCK_ON_VSEL(9), RK806_BUCK_SLP_VSEL(9), RK806_BUCK_CONFIG(9), RK806_BUCK_VSEL_MASK, 0xed, 0xff, 3},
   /* buck 10 */
   {  500000,   6250, RK806_BUCK_ON_VSEL(10), RK806_BUCK_SLP_VSEL(10), RK806_BUCK_CONFIG(10), RK806_BUCK_VSEL_MASK, 0x00, 0x9f, 3},
   {  1500000, 25000, RK806_BUCK_ON_VSEL(10), RK806_BUCK_SLP_VSEL(10), RK806_BUCK_CONFIG(10), RK806_BUCK_VSEL_MASK, 0xa0, 0xed, 3},
   {  3400000,     0, RK806_BUCK_ON_VSEL(10), RK806_BUCK_SLP_VSEL(10), RK806_BUCK_CONFIG(10), RK806_BUCK_VSEL_MASK, 0xed, 0xff, 3},
};
 
static const struct rk8xx_reg_info rk806_nldo[] = {
   /* nldo1 */
   {  500000, 12500, RK806_NLDO_ON_VSEL(1), RK806_NLDO_SLP_VSEL(1), NA, RK806_NLDO_VSEL_MASK, 0x00, },
   {  3400000,    0, RK806_NLDO_ON_VSEL(1), RK806_NLDO_SLP_VSEL(1), NA, RK806_NLDO_VSEL_MASK, 0xE8, },
   /* nldo2 */
   {  500000, 12500, RK806_NLDO_ON_VSEL(2), RK806_NLDO_SLP_VSEL(2), NA, RK806_NLDO_VSEL_MASK, 0x00, },
   {  3400000,    0, RK806_NLDO_ON_VSEL(2), RK806_NLDO_SLP_VSEL(2), NA, RK806_NLDO_VSEL_MASK, 0xE8, },
   /* nldo3 */
   {  500000, 12500, RK806_NLDO_ON_VSEL(3), RK806_NLDO_SLP_VSEL(3), NA, RK806_NLDO_VSEL_MASK, 0x00, },
   {  3400000,    0, RK806_NLDO_ON_VSEL(3), RK806_NLDO_SLP_VSEL(3), NA, RK806_NLDO_VSEL_MASK, 0xE8, },
   /* nldo4 */
   {  500000, 12500, RK806_NLDO_ON_VSEL(4), RK806_NLDO_SLP_VSEL(4), NA, RK806_NLDO_VSEL_MASK, 0x00, },
   {  3400000,    0, RK806_NLDO_ON_VSEL(4), RK806_NLDO_SLP_VSEL(4), NA, RK806_NLDO_VSEL_MASK, 0xE8, },
   /* nldo5 */
   {  500000, 12500, RK806_NLDO_ON_VSEL(5), RK806_NLDO_SLP_VSEL(5), NA, RK806_NLDO_VSEL_MASK, 0x00, },
   {  3400000,    0, RK806_NLDO_ON_VSEL(5), RK806_NLDO_SLP_VSEL(5), NA, RK806_NLDO_VSEL_MASK, 0xE8, },
};
 
static const struct rk8xx_reg_info rk806_pldo[] = {
   /* pldo1 */
   {  500000, 12500, RK806_PLDO_ON_VSEL(1), RK806_PLDO_SLP_VSEL(1), NA, RK806_PLDO_VSEL_MASK, 0x00, },
   {  3400000,    0, RK806_PLDO_ON_VSEL(1), RK806_PLDO_SLP_VSEL(1), NA, RK806_PLDO_VSEL_MASK, 0xE8, },
   /* pldo2 */
   {  500000, 12500, RK806_PLDO_ON_VSEL(2), RK806_PLDO_SLP_VSEL(2), NA, RK806_PLDO_VSEL_MASK, 0x00, },
   {  3400000,    0, RK806_PLDO_ON_VSEL(2), RK806_PLDO_SLP_VSEL(2), NA, RK806_PLDO_VSEL_MASK, 0xE8, },
   /* pldo3 */
   {  500000, 12500, RK806_PLDO_ON_VSEL(3), RK806_PLDO_SLP_VSEL(3), NA, RK806_PLDO_VSEL_MASK, 0x00, },
   {  3400000,    0, RK806_PLDO_ON_VSEL(3), RK806_PLDO_SLP_VSEL(3), NA, RK806_PLDO_VSEL_MASK, 0xE8, },
   /* pldo4 */
   {  500000, 12500, RK806_PLDO_ON_VSEL(4), RK806_PLDO_SLP_VSEL(4), NA, RK806_PLDO_VSEL_MASK, 0x00, },
   {  3400000,    0, RK806_PLDO_ON_VSEL(4), RK806_PLDO_SLP_VSEL(4), NA, RK806_PLDO_VSEL_MASK, 0xE8, },
   /* pldo5 */
   {  500000, 12500, RK806_PLDO_ON_VSEL(5), RK806_PLDO_SLP_VSEL(5), NA, RK806_PLDO_VSEL_MASK, 0x00, },
   {  3400000,    0, RK806_PLDO_ON_VSEL(5), RK806_PLDO_SLP_VSEL(5), NA, RK806_PLDO_VSEL_MASK, 0xE8, },
   /* pldo6 */
   {  500000, 12500, RK806_PLDO_ON_VSEL(6), RK806_PLDO_SLP_VSEL(6), NA, RK806_PLDO_VSEL_MASK, 0x00, },
   {  3400000,    0, RK806_PLDO_ON_VSEL(6), RK806_PLDO_SLP_VSEL(6), NA, RK806_PLDO_VSEL_MASK, 0xE8, },
};
 
static const struct rk8xx_reg_info *get_buck_reg(struct udevice *pmic,
                        int num, int uvolt)
{
   struct rk8xx_priv *priv = dev_get_priv(pmic);
 
   switch (priv->variant) {
   case RK806_ID:
       switch (num) {
       case 0 ... 9:
           if (uvolt < 1500000)
               return &rk806_buck[num * 3 + 0];
           else if (uvolt < 3400000)
               return &rk806_buck[num * 3 + 1];
           else
               return &rk806_buck[num * 3 + 2];
           break;
       }
   default:
       return &rk806_buck[num * 3 + 0];
   }
}
 
static int _buck_set_value(struct udevice *pmic, int buck, int uvolt)
{
   const struct rk8xx_reg_info *info = get_buck_reg(pmic, buck, uvolt);
   int mask = info->vsel_mask;
   int val;
 
   if (info->vsel_reg == NA)
       return -EINVAL;
 
   if (info->step_uv == 0)    /* Fixed voltage */
       val = info->min_sel;
   else
       val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel;
 
   debug("%s: volt=%d, buck=%d, reg=0x%x, mask=0x%x, val=0x%x\n",
         __func__, uvolt, buck + 1, info->vsel_reg, mask, val);
 
   return pmic_clrsetbits(pmic, info->vsel_reg, mask, val);
}
 
static int _buck_set_enable(struct udevice *pmic, int buck, bool enable)
{
   struct rk8xx_priv *priv = dev_get_priv(pmic);
   uint value, en_reg;
   int ret;
 
   switch (priv->variant) {
   case RK806_ID:
       en_reg = RK806_POWER_EN(buck / 4);
       if (enable)
           value = ((1 << buck % 4) | (1 << (buck % 4 + 4)));
       else
           value = ((0 << buck % 4) | (1 << (buck % 4 + 4)));
 
       ret = pmic_reg_write(pmic, en_reg, value);
       break;
   default:
       ret = -EINVAL;
   }
 
   return ret;
}
 
#ifdef ENABLE_DRIVER
static int _buck_set_suspend_value(struct udevice *pmic, int buck, int uvolt)
{
   const struct rk8xx_reg_info *info = get_buck_reg(pmic, buck, uvolt);
   int mask = info->vsel_mask;
   int val;
 
   if (info->vsel_sleep_reg == NA)
       return -EINVAL;
 
   if (info->step_uv == 0)
       val = info->min_sel;
   else
       val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel;
 
   debug("%s: volt=%d, buck=%d, reg=0x%x, mask=0x%x, val=0x%x\n",
         __func__, uvolt, buck + 1, info->vsel_sleep_reg, mask, val);
 
   return pmic_clrsetbits(pmic, info->vsel_sleep_reg, mask, val);
}
 
static int _buck_get_enable(struct udevice *pmic, int buck)
{
   struct rk8xx_priv *priv = dev_get_priv(pmic);
   uint mask = 0;
   int ret = 0;
 
   switch (priv->variant) {
   case RK806_ID:
       mask = 1 << buck % 4;
       ret = pmic_reg_read(pmic, RK806_POWER_EN(buck / 4));
       break;
   default:
       ret = 0;
   }
 
   if (ret < 0)
       return ret;
 
   return ret & mask ? true : false;
}
 
static int _buck_set_ramp_delay(struct udevice *pmic, int buck, u32 ramp_delay)
{
   const struct rk8xx_reg_info *info = get_buck_reg(pmic, buck, 0);
   struct rk8xx_priv *priv = dev_get_priv(pmic);
   int ramp_value = 0, reg_value;
   int ramp_reg1, ramp_reg2;
 
   if (info->config_reg == NA)
       return -EINVAL;
 
   switch (priv->variant) {
   case RK806_ID:
       switch (ramp_delay) {
       case 1 ... 390:
           ramp_value = RK806_RAMP_RATE_1LSB_PER_32CLK;
           break;
       case 391 ... 961:
           ramp_value = RK806_RAMP_RATE_1LSB_PER_13CLK;
           break;
       case 962 ... 1560:
           ramp_value = RK806_RAMP_RATE_1LSB_PER_8CLK;
           break;
       case 1561 ... 3125:
           ramp_value = RK806_RAMP_RATE_1LSB_PER_4CLK;
           break;
       case 3126 ... 6250:
           ramp_value = RK806_RAMP_RATE_1LSB_PER_2CLK;
           break;
       case 6251 ... 12500:
           ramp_value = RK806_RAMP_RATE_1LSB_PER_1CLK;
           break;
       case 12501 ... 25000:
           ramp_value = RK806_RAMP_RATE_2LSB_PER_1CLK;
           break;
       case 25001 ... 50000: /* 50mV/us */
           ramp_value = RK806_RAMP_RATE_4LSB_PER_1CLK;
           break;
       default:
           ramp_value = RK806_RAMP_RATE_1LSB_PER_32CLK;
           printf("buck%d ramp_delay: %d not supported\n",
                  buck, ramp_delay);
           break;
       }
       break;
   default:
       ramp_value = RK806_RAMP_RATE_1LSB_PER_32CLK;
       return -EINVAL;
   }
 
   ramp_reg1 = RK806_RAMP_RATE_REG1(buck);
   if (buck < 8)
       ramp_reg2 = RK806_RAMP_RATE_REG1_8;
   else
       ramp_reg2 = RK806_RAMP_RATE_REG9_10;
 
   reg_value = pmic_reg_read(pmic, ramp_reg1);
   if (reg_value < 0) {
       printf("buck%d read ramp reg(0x%x) error: %d", buck, ramp_reg1, reg_value);
       return reg_value;
   }
   reg_value &= 0x3f;
 
   pmic_reg_write(pmic,
              ramp_reg1,
              reg_value | (ramp_value & 0x03) << 0x06);
 
   reg_value = pmic_reg_read(pmic, ramp_reg2);
   if (reg_value < 0) {
       printf("buck%d read ramp reg(0x%x) error: %d", buck, ramp_reg2, reg_value);
       return reg_value;
   }
 
   return pmic_reg_write(pmic,
                 ramp_reg2,
                 reg_value | (ramp_value & 0x04) << (buck % 8));
}
 
static int _buck_set_suspend_enable(struct udevice *pmic, int buck, bool enable)
{
   uint mask;
   int ret;
 
   if (buck <= 7) {
       mask = 1 << buck;
       ret = pmic_clrsetbits(pmic, RK806_BUCK_SUSPEND_EN, mask,
                     enable ? mask : 0);
   } else {
       if (buck == 8)
           mask = 0x40;
       else
           mask = 0x80;
       ret = pmic_clrsetbits(pmic, RK806_NLDO_SUSPEND_EN, mask,
                     enable ? mask : 0);
   }
 
   return ret;
}
 
static int _buck_get_suspend_enable(struct udevice *pmic, int buck)
{
   uint mask, val;
   int ret;
 
   if (buck <= 7) {
       mask = 1 << buck % 7;
       val = pmic_reg_read(pmic, RK806_BUCK_SUSPEND_EN);
   } else {
       mask = 1 << ((buck - 7) + 6);
       val = pmic_reg_read(pmic, RK806_NLDO_SUSPEND_EN);
   }
 
   if (val < 0)
       return val;
   ret = val & mask ? 1 : 0;
 
   return ret;
}
 
static const struct rk8xx_reg_info *get_ldo_reg(struct udevice *pmic,
                       int num, int uvolt)
{
   struct rk8xx_priv *priv = dev_get_priv(pmic);
 
   switch (priv->variant) {
   case RK806_ID:
       if (uvolt < 3400000)
           return &rk806_nldo[num * 2];
       else
           return &rk806_nldo[num * 2 + 1];
   default:
       return &rk806_nldo[num * 2];
   }
}
 
static const struct rk8xx_reg_info *get_pldo_reg(struct udevice *pmic,
                        int num, int uvolt)
{
   struct rk8xx_priv *priv = dev_get_priv(pmic);
 
   switch (priv->variant) {
   case RK806_ID:
       if (uvolt < 3400000)
           return &rk806_pldo[num * 2];
       else
           return &rk806_pldo[num * 2 + 1];
   default:
       return &rk806_pldo[num * 2];
   }
}
 
static int _ldo_get_enable(struct udevice *pmic, int ldo)
{
   struct rk8xx_priv *priv = dev_get_priv(pmic);
   uint mask = 0;
   int ret = 0;
 
   switch (priv->variant) {
   case RK806_ID:
       if (ldo < 4) {
           mask = 1 << ldo % 4;
           ret = pmic_reg_read(pmic, RK806_NLDO_EN(ldo / 4));
       } else {
           mask = 1 << 2;
           ret = pmic_reg_read(pmic, RK806_NLDO_EN(2));
       }
       break;
   default:
       return false;
   }
 
   if (ret < 0)
       return ret;
 
   return ret & mask ? true : false;
}
 
static int _ldo_set_enable(struct udevice *pmic, int ldo, bool enable)
{
   struct rk8xx_priv *priv = dev_get_priv(pmic);
   uint value, en_reg;
   int ret = 0;
 
   switch (priv->variant) {
   case RK806_ID:
       if (ldo < 4) {
           en_reg = RK806_NLDO_EN(0);
           if (enable)
               value = ((1 << ldo % 4) | (1 << (ldo % 4 + 4)));
           else
               value = ((0 << ldo % 4) | (1 << (ldo % 4 + 4)));
           ret = pmic_reg_write(pmic, en_reg, value);
       } else {
           en_reg = RK806_NLDO_EN(2);
           if (enable)
               value = 0x44;
           else
               value = 0x40;
           ret = pmic_reg_write(pmic, en_reg, value);
       }
       break;
   default:
       return -EINVAL;
   }
 
   return ret;
}
 
static int _pldo_get_enable(struct udevice *pmic, int pldo)
{
   struct rk8xx_priv *priv = dev_get_priv(pmic);
   uint mask = 0, en_reg;
   int ret = 0;
 
   switch (priv->variant) {
   case RK806_ID:
       if ((pldo < 3) || (pldo == 5)) {
           en_reg = RK806_PLDO_EN(0);
           mask = RK806_PLDO0_2_SET(pldo);
           if (pldo == 5)
               mask = (1 << 0);
           ret = pmic_reg_read(pmic, en_reg);
       } else if ((pldo == 3) || (pldo == 4)) {
           en_reg = RK806_PLDO_EN(1);
           if (pldo == 3)
               mask = (1 << 0);
           else
               mask = (1 << 1);
           ret = pmic_reg_read(pmic, en_reg);
       }
       break;
 
   default:
       return -EINVAL;
   }
 
   if (ret < 0)
       return ret;
 
   return ret & mask ? true : false;
}
 
static int _pldo_set_enable(struct udevice *pmic, int pldo, bool enable)
{
   struct rk8xx_priv *priv = dev_get_priv(pmic);
   uint value, en_reg;
   int ret = 0;
 
   switch (priv->variant) {
   case RK806_ID:
       if (pldo < 3) {
           en_reg = RK806_PLDO_EN(0);
           if (enable)
               value = RK806_PLDO0_2_SET(pldo);
           else
               value = RK806_PLDO0_2_CLR(pldo);
           ret = pmic_reg_write(pmic, en_reg, value);
       } else if (pldo == 3) {
           en_reg = RK806_PLDO_EN(1);
           if (enable)
               value = ((1 << 0) | (1 << 4));
           else
               value = (1 << 4);
           ret = pmic_reg_write(pmic, en_reg, value);
       } else if (pldo == 4) {
           en_reg = RK806_PLDO_EN(1);
           if (enable)
               value = ((1 << 1) | (1 << 5));
           else
               value = ((0 << 1) | (1 << 5));
           ret = pmic_reg_write(pmic, en_reg, value);
       } else if (pldo == 5) {
           en_reg = RK806_PLDO_EN(0);
           if (enable)
               value = ((1 << 0) | (1 << 4));
           else
               value = ((0 << 0) | (1 << 4));
           ret = pmic_reg_write(pmic, en_reg, value);
       }
 
       break;
   default:
       return -EINVAL;
   }
 
   return ret;
}
 
static int _ldo_set_suspend_enable(struct udevice *pmic, int ldo, bool enable)
{
   uint mask;
   int ret;
 
   mask = 1 << ldo;
   ret = pmic_clrsetbits(pmic, RK806_NLDO_SUSPEND_EN, mask,
                 enable ? mask : 0);
 
   return ret;
}
 
static int _ldo_get_suspend_enable(struct udevice *pmic, int ldo)
{
   uint mask, val;
   int ret;
 
   mask = 1 << ldo;
   val = pmic_reg_read(pmic, RK806_NLDO_SUSPEND_EN);
 
   if (val < 0)
       return val;
   ret = val & mask ? 1 : 0;
 
   return ret;
}
 
static int buck_get_value(struct udevice *dev)
{
   int buck = dev->driver_data - 1;
   const struct rk8xx_reg_info *info = get_buck_reg(dev->parent, buck, 0);
   int mask = info->vsel_mask;
   int i, ret, val;
 
   if (info->vsel_reg == NA)
       return -EINVAL;
 
   ret = pmic_reg_read(dev->parent, info->vsel_reg);
   if (ret < 0)
       return ret;
 
   val = ret & mask;
   if (val >= info->min_sel && val <= info->max_sel)
       goto finish;
 
   /* unlucky to try */
   for (i = 1; i < info->range_num; i++) {
       info++;
       if (val <= info->max_sel && val >= info->min_sel)
           break;
   }
 
finish:
   return info->min_uv + (val - info->min_sel) * info->step_uv;
}
 
static int buck_set_value(struct udevice *dev, int uvolt)
{
   int buck = dev->driver_data - 1;
 
   return _buck_set_value(dev->parent, buck, uvolt);
}
 
static int buck_get_suspend_value(struct udevice *dev)
{
   int buck = dev->driver_data - 1;
   const struct rk8xx_reg_info *info = get_buck_reg(dev->parent, buck, 0);
   int mask = info->vsel_mask;
   int i, ret, val;
 
   if (info->vsel_sleep_reg == NA)
       return -EINVAL;
 
   ret = pmic_reg_read(dev->parent, info->vsel_sleep_reg);
   if (ret < 0)
       return ret;
 
   val = ret & mask;
   if (val <= info->max_sel && val >= info->min_sel)
       goto finish;
 
   /* unlucky to try */
   for (i = 1; i < info->range_num; i++) {
       info++;
       if (val <= info->max_sel && val >= info->min_sel)
           break;
   }
 
finish:
   return info->min_uv + (val - info->min_sel) * info->step_uv;
}
 
static int buck_set_suspend_value(struct udevice *dev, int uvolt)
{
   int buck = dev->driver_data - 1;
 
   return _buck_set_suspend_value(dev->parent, buck, uvolt);
}
 
static int buck_set_enable(struct udevice *dev, bool enable)
{
   int buck = dev->driver_data - 1;
 
   return _buck_set_enable(dev->parent, buck, enable);
}
 
static int buck_set_suspend_enable(struct udevice *dev, bool enable)
{
   int buck = dev->driver_data - 1;
 
   return _buck_set_suspend_enable(dev->parent, buck, enable);
}
 
static int buck_get_suspend_enable(struct udevice *dev)
{
   int buck = dev->driver_data - 1;
 
   return _buck_get_suspend_enable(dev->parent, buck);
}
 
static int buck_set_ramp_delay(struct udevice *dev, u32 ramp_delay)
{
   int buck = dev->driver_data - 1;
 
   return _buck_set_ramp_delay(dev->parent, buck, ramp_delay);
}
 
static int buck_get_enable(struct udevice *dev)
{
   int buck = dev->driver_data - 1;
 
   return _buck_get_enable(dev->parent, buck);
}
 
static int ldo_get_value(struct udevice *dev)
{
   int ldo = dev->driver_data - 1;
   const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, 0);
   int mask = info->vsel_mask;
   int ret, val;
 
   if (info->vsel_reg == NA)
       return -EINVAL;
 
   ret = pmic_reg_read(dev->parent, info->vsel_reg);
   if (ret < 0)
       return ret;
   val = ret & mask;
 
   return info->min_uv + val * info->step_uv;
}
 
static int ldo_set_value(struct udevice *dev, int uvolt)
{
   int ldo = dev->driver_data - 1;
   const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, uvolt);
   int mask = info->vsel_mask;
   int val;
 
   if (info->vsel_reg == NA)
       return -EINVAL;
 
   if (info->step_uv == 0)
       val = info->min_sel;
   else
       val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel;
 
   debug("%s: volt=%d, ldo=%d, reg=0x%x, mask=0x%x, val=0x%x\n",
         __func__, uvolt, ldo + 1, info->vsel_reg, mask, val);
 
   return pmic_clrsetbits(dev->parent, info->vsel_reg, mask, val);
}
 
static int pldo_get_value(struct udevice *dev)
{
   int ldo = dev->driver_data - 1;
   const struct rk8xx_reg_info *info = get_pldo_reg(dev->parent, ldo, 0);
   int mask = info->vsel_mask;
   int ret, val;
 
   if (info->vsel_reg == NA)
       return -EINVAL;
 
   ret = pmic_reg_read(dev->parent, info->vsel_reg);
   if (ret < 0)
       return ret;
   val = ret & mask;
 
   return info->min_uv + val * info->step_uv;
}
 
static int pldo_set_value(struct udevice *dev, int uvolt)
{
   int ldo = dev->driver_data - 1;
   const struct rk8xx_reg_info *info = get_pldo_reg(dev->parent, ldo, uvolt);
   int mask = info->vsel_mask;
   int val;
 
   if (info->vsel_reg == NA)
       return -EINVAL;
 
   if (info->step_uv == 0)
       val = info->min_sel;
   else
       val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel;
 
   debug("%s: volt=%d, ldo=%d, reg=0x%x, mask=0x%x, val=0x%x\n",
         __func__, uvolt, ldo + 1, info->vsel_reg, mask, val);
 
   return pmic_clrsetbits(dev->parent, info->vsel_reg, mask, val);
}
 
static int ldo_set_suspend_value(struct udevice *dev, int uvolt)
{
   int ldo = dev->driver_data - 1;
   const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, uvolt);
   int mask = info->vsel_mask;
   int val;
 
   if (info->vsel_sleep_reg == NA)
       return -EINVAL;
 
   if (info->step_uv == 0)
       val = info->min_sel;
   else
       val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel;
 
   debug("%s: volt=%d, ldo=%d, reg=0x%x, mask=0x%x, val=0x%x\n",
         __func__, uvolt, ldo + 1, info->vsel_sleep_reg, mask, val);
 
   return pmic_clrsetbits(dev->parent, info->vsel_sleep_reg, mask, val);
}
 
static int ldo_get_suspend_value(struct udevice *dev)
{
   int ldo = dev->driver_data - 1;
   const struct rk8xx_reg_info *info = get_ldo_reg(dev->parent, ldo, 0);
   int mask = info->vsel_mask;
   int val, ret;
 
   if (info->vsel_sleep_reg == NA)
       return -EINVAL;
 
   ret = pmic_reg_read(dev->parent, info->vsel_sleep_reg);
   if (ret < 0)
       return ret;
 
   val = ret & mask;
 
   return info->min_uv + val * info->step_uv;
}
 
static int ldo_set_enable(struct udevice *dev, bool enable)
{
   int ldo = dev->driver_data - 1;
 
   return _ldo_set_enable(dev->parent, ldo, enable);
}
 
static int ldo_set_suspend_enable(struct udevice *dev, bool enable)
{
   int ldo = dev->driver_data - 1;
 
   return _ldo_set_suspend_enable(dev->parent, ldo, enable);
}
 
static int ldo_get_suspend_enable(struct udevice *dev)
{
   int ldo = dev->driver_data - 1;
 
   return _ldo_get_suspend_enable(dev->parent, ldo);
}
 
static int ldo_get_enable(struct udevice *dev)
{
   int ldo = dev->driver_data - 1;
 
   return _ldo_get_enable(dev->parent, ldo);
}
 
static int pldo_set_enable(struct udevice *dev, bool enable)
{
   int ldo = dev->driver_data - 1;
 
   return _pldo_set_enable(dev->parent, ldo, enable);
}
 
static int pldo_get_enable(struct udevice *dev)
{
   int ldo = dev->driver_data - 1;
 
   return _pldo_get_enable(dev->parent, ldo);
}
 
static int pldo_set_suspend_value(struct udevice *dev, int uvolt)
{
   int ldo = dev->driver_data - 1;
   const struct rk8xx_reg_info *info = get_pldo_reg(dev->parent, ldo, uvolt);;
   int mask = info->vsel_mask;
   int val;
 
   if (info->vsel_sleep_reg == NA)
       return -EINVAL;
 
   if (info->step_uv == 0)
       val = info->min_sel;
   else
       val = ((uvolt - info->min_uv) / info->step_uv) + info->min_sel;
 
   return pmic_clrsetbits(dev->parent, info->vsel_sleep_reg, mask, val);
}
 
static int pldo_get_suspend_value(struct udevice *dev)
{
   int ldo = dev->driver_data - 1;
   const struct rk8xx_reg_info *info = get_pldo_reg(dev->parent, ldo, 0);
   int mask = info->vsel_mask;
   int val, ret;
 
   if (info->vsel_sleep_reg == NA)
       return -EINVAL;
 
   ret = pmic_reg_read(dev->parent, info->vsel_sleep_reg);
   if (ret < 0)
       return ret;
 
   val = ret & mask;
 
   return info->min_uv + val * info->step_uv;
}
 
static int _pldo_set_suspend_enable(struct udevice *pmic, int ldo, bool enable)
{
   uint mask;
   int ret;
 
   if (ldo < 5)
       mask = 1 << (ldo + 1);
   else
       mask = 1;
   ret = pmic_clrsetbits(pmic, RK806_PLDO_SUSPEND_EN, mask,
                 enable ? mask : 0);
 
   return ret;
}
 
static int _pldo_get_suspend_enable(struct udevice *pmic, int ldo)
{
   uint mask, val;
   int ret;
 
   if (ldo < 5)
       mask = 1 << (ldo + 1);
   else
       mask = 1;
   val = pmic_reg_read(pmic, RK806_PLDO_SUSPEND_EN);
 
   if (val < 0)
       return val;
   ret = val & mask ? 1 : 0;
 
   return ret;
}
 
static int pldo_set_suspend_enable(struct udevice *dev, bool enable)
{
   int ldo = dev->driver_data - 1;
 
   return _pldo_set_suspend_enable(dev->parent, ldo, enable);
}
 
static int pldo_get_suspend_enable(struct udevice *dev)
{
   int ldo = dev->driver_data - 1;
 
   return _pldo_get_suspend_enable(dev->parent, ldo);
}
 
static int rk8xx_buck_probe(struct udevice *dev)
{
   struct dm_regulator_uclass_platdata *uc_pdata;
 
   uc_pdata = dev_get_uclass_platdata(dev);
   uc_pdata->type = REGULATOR_TYPE_BUCK;
   uc_pdata->mode_count = 0;
 
   return 0;
}
 
static int rk8xx_ldo_probe(struct udevice *dev)
{
   struct dm_regulator_uclass_platdata *uc_pdata;
 
   uc_pdata = dev_get_uclass_platdata(dev);
   uc_pdata->type = REGULATOR_TYPE_LDO;
   uc_pdata->mode_count = 0;
 
   return 0;
}
 
static int rk8xx_pldo_probe(struct udevice *dev)
{
   struct dm_regulator_uclass_platdata *uc_pdata;
 
   uc_pdata = dev_get_uclass_platdata(dev);
   uc_pdata->type = REGULATOR_TYPE_LDO;
   uc_pdata->mode_count = 0;
 
   return 0;
}
 
static const struct dm_regulator_ops rk8xx_buck_ops = {
   .get_value  = buck_get_value,
   .set_value  = buck_set_value,
   .set_suspend_value = buck_set_suspend_value,
   .get_suspend_value = buck_get_suspend_value,
   .get_enable = buck_get_enable,
   .set_enable = buck_set_enable,
   .set_suspend_enable = buck_set_suspend_enable,
   .get_suspend_enable = buck_get_suspend_enable,
   .set_ramp_delay = buck_set_ramp_delay,
};
 
static const struct dm_regulator_ops rk8xx_ldo_ops = {
   .get_value  = ldo_get_value,
   .set_value  = ldo_set_value,
   .set_suspend_value = ldo_set_suspend_value,
   .get_suspend_value = ldo_get_suspend_value,
   .get_enable = ldo_get_enable,
   .set_enable = ldo_set_enable,
   .set_suspend_enable = ldo_set_suspend_enable,
   .get_suspend_enable = ldo_get_suspend_enable,
};
 
static const struct dm_regulator_ops rk8xx_pldo_ops = {
   .get_value  = pldo_get_value,
   .set_value  = pldo_set_value,
   .set_suspend_value = pldo_set_suspend_value,
   .get_suspend_value = pldo_get_suspend_value,
   .get_enable = pldo_get_enable,
   .set_enable = pldo_set_enable,
   .set_suspend_enable = pldo_set_suspend_enable,
   .get_suspend_enable = pldo_get_suspend_enable,
};
 
U_BOOT_DRIVER(rk8xx_spi_buck) = {
   .name = "rk8xx_spi_buck",
   .id = UCLASS_REGULATOR,
   .ops = &rk8xx_buck_ops,
   .probe = rk8xx_buck_probe,
};
 
U_BOOT_DRIVER(rk8xx_spi_ldo) = {
   .name = "rk8xx_spi_ldo",
   .id = UCLASS_REGULATOR,
   .ops = &rk8xx_ldo_ops,
   .probe = rk8xx_ldo_probe,
};
 
U_BOOT_DRIVER(rk8xx_spi_pldo) = {
   .name = "rk8xx_spi_pldo",
   .id = UCLASS_REGULATOR,
   .ops = &rk8xx_pldo_ops,
   .probe = rk8xx_pldo_probe,
};
#endif