ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / board-pmu-rk808.c
1 #include <linux/regulator/machine.h>
2 #include <linux/mfd/rk808.h>
3 #include <mach/sram.h>
4 #include <linux/platform_device.h>
5
6 #include <mach/gpio.h>
7 #include <mach/iomux.h>
8 #include <mach/board.h>
9
10 #ifdef CONFIG_MFD_RK808
11
12 static int rk808_pre_init(struct rk808 *rk808)
13 {
14         int ret,val;
15          printk("%s,line=%d\n", __func__,__LINE__);
16          /***********set ILIM ************/
17         val = rk808_reg_read(rk808,RK808_BUCK3_CONFIG_REG);
18         val &= (~(0x7 <<0));
19         val |= (0x2 <<0);
20         ret = rk808_reg_write(rk808,RK808_BUCK3_CONFIG_REG,val);
21         if (ret < 0) {
22                 printk(KERN_ERR "Unable to write RK808_BUCK3_CONFIG_REG reg\n");
23                 return ret;
24         }
25
26         val = rk808_reg_read(rk808,RK808_BUCK4_CONFIG_REG);
27         val &= (~(0x7 <<0));
28         val |= (0x3 <<0);
29         ret = rk808_reg_write(rk808,RK808_BUCK4_CONFIG_REG,val);
30         if (ret < 0) {
31                 printk(KERN_ERR "Unable to write RK808_BUCK4_CONFIG_REG reg\n");
32                 return ret;
33         }
34         
35         val = rk808_reg_read(rk808,RK808_BOOST_CONFIG_REG);
36         val &= (~(0x7 <<0));
37         val |= (0x1 <<0);
38         ret = rk808_reg_write(rk808,RK808_BOOST_CONFIG_REG,val);
39         if (ret < 0) {
40                 printk(KERN_ERR "Unable to write RK808_BOOST_CONFIG_REG reg\n");
41                 return ret;
42         }
43         /*****************************************/
44         /***********set buck OTP function************/
45         ret = rk808_reg_write(rk808,0x6f,0x5a);
46         if (ret < 0) {
47                 printk(KERN_ERR "Unable to write 0x6f reg\n");
48                 return ret;
49         }
50         
51         ret = rk808_reg_write(rk808,0x91,0x80);
52         if (ret < 0) {
53                 printk(KERN_ERR "Unable to write 0x91 reg\n");
54                 return ret;
55         }
56
57         ret = rk808_reg_write(rk808,0x92,0x55);
58          if (ret <0) {
59                 printk(KERN_ERR "Unable to write 0x92 reg\n");
60                 return ret;
61         }
62         /*****************************************/
63         /***********set buck 12.5mv/us ************/
64         val = rk808_reg_read(rk808,RK808_BUCK1_CONFIG_REG);
65         val &= (~(0x3 <<3));
66         val |= (0x3 <<0);
67         ret = rk808_reg_write(rk808,RK808_BUCK1_CONFIG_REG,val);
68         if (ret < 0) {
69                 printk(KERN_ERR "Unable to write RK808_BUCK1_CONFIG_REG reg\n");
70                 return ret;
71         }
72
73         val = rk808_reg_read(rk808,RK808_BUCK2_CONFIG_REG);
74         val &= (~(0x3 <<3));
75         val |= (0x3 <<0);
76         ret = rk808_reg_write(rk808,RK808_BUCK2_CONFIG_REG,val);
77          if (ret <0) {
78                 printk(KERN_ERR "Unable to write RK808_BUCK2_CONFIG_REG reg\n");
79                 return ret;
80         }
81         /*****************************************/
82
83         /*******enable switch and boost***********/
84         val = rk808_reg_read(rk808,RK808_DCDC_EN_REG);
85         val |= (0x3 << 5);    //enable switch1/2
86 //      val |= (0x1 << 4);    //enable boost
87         ret = rk808_reg_write(rk808,RK808_DCDC_EN_REG,val);
88          if (ret <0) {
89                 printk(KERN_ERR "Unable to write RK808_DCDC_EN_REG reg\n");
90                 return ret;
91         }
92         /****************************************/
93         /***********set dc1\2 dvs voltge*********/
94          val = rk808_reg_read(rk808,RK808_BUCK1_DVS_REG);
95         val |= 0x34;    //set dc1 dvs 1.35v
96         ret = rk808_reg_write(rk808,RK808_BUCK1_DVS_REG,val);
97          if (ret <0) {
98                 printk(KERN_ERR "Unable to write RK808_BUCK1_DVS_REG reg\n");
99                 return ret;
100         }
101         
102           val = rk808_reg_read(rk808,RK808_BUCK2_DVS_REG);
103         val |= 0x34;    //set dc2 dvs 1.35v
104         ret = rk808_reg_write(rk808,RK808_BUCK2_DVS_REG,val);
105          if (ret <0) {
106                 printk(KERN_ERR "Unable to write RK808_BUCK2_DVS_REG reg\n");
107                 return ret;
108         }
109         /***********************************/
110
111         /****************set vbat low **********/
112          val = rk808_reg_read(rk808,RK808_VB_MON_REG);
113         val &= (~(0x7<<0));    //set vbat < 3.5v irq
114         val &= (~(0x1 <<4));
115         val |= (0x7 <<0);
116         val |= (0x1 << 4);
117         ret = rk808_reg_write(rk808,RK808_VB_MON_REG,val);
118          if (ret <0) {
119                 printk(KERN_ERR "Unable to write RK808_VB_MON_REG reg\n");
120                 return ret;
121         }
122         /**************************************/
123         /********set dcdc/ldo/switch off when in sleep******/
124           val = rk808_reg_read(rk808,RK808_SLEEP_SET_OFF_REG1);
125 //        val |= (0x3<<5);   
126         ret = rk808_reg_write(rk808,RK808_SLEEP_SET_OFF_REG1,val);
127          if (ret <0) {
128                 printk(KERN_ERR "Unable to write RK808_SLEEP_SET_OFF_REG1 reg\n");
129                 return ret;
130         }
131         val = rk808_reg_read(rk808,RK808_SLEEP_SET_OFF_REG2);
132 //        val |= (0x1<<4);     
133         ret = rk808_reg_write(rk808,RK808_SLEEP_SET_OFF_REG2,val);
134          if (ret <0) {
135                 printk(KERN_ERR "Unable to write RK808_SLEEP_SET_OFF_REG2 reg\n");
136                 return ret;
137         }
138         /**************************************************/
139         /**********mask int****************/
140          val = rk808_reg_read(rk808,RK808_INT_STS_MSK_REG1);
141          val |= (0x1<<0); //mask vout_lo_int    
142         ret = rk808_reg_write(rk808,RK808_INT_STS_MSK_REG1,val);
143          if (ret <0) {
144                 printk(KERN_ERR "Unable to write RK808_INT_STS_MSK_REG1 reg\n");
145                 return ret;
146         }
147         /**********************************/
148         printk("%s,line=%d\n", __func__,__LINE__);
149 }
150 static int rk808_set_init(struct rk808 *rk808)
151 {
152         struct regulator *dcdc;
153         struct regulator *ldo;
154         int i = 0;
155         printk("%s,line=%d\n", __func__,__LINE__);
156
157         #ifndef CONFIG_RK_CONFIG
158         g_pmic_type = PMIC_TYPE_RK808;
159         #endif
160         printk("%s:g_pmic_type=%d\n",__func__,g_pmic_type);
161         
162         for(i = 0; i < ARRAY_SIZE(rk808_dcdc_info); i++)
163         {
164
165                 if(rk808_dcdc_info[i].min_uv == 0 && rk808_dcdc_info[i].max_uv == 0)
166                         continue;
167                 dcdc =regulator_get(NULL, rk808_dcdc_info[i].name);
168                 regulator_set_voltage(dcdc, rk808_dcdc_info[i].min_uv, rk808_dcdc_info[i].max_uv);
169                  regulator_set_suspend_voltage(dcdc, rk808_dcdc_info[i].suspend_vol);
170                 regulator_enable(dcdc);
171                 printk("%s  %s =%duV end\n", __func__,rk808_dcdc_info[i].name, regulator_get_voltage(dcdc));
172                 regulator_put(dcdc);
173                 udelay(100);
174         }
175         
176         for(i = 0; i < ARRAY_SIZE(rk808_ldo_info); i++)
177         {
178                 if(rk808_ldo_info[i].min_uv == 0 && rk808_ldo_info[i].max_uv == 0)
179                         continue;
180                 ldo =regulator_get(NULL, rk808_ldo_info[i].name);
181
182                 regulator_set_voltage(ldo, rk808_ldo_info[i].min_uv, rk808_ldo_info[i].max_uv);
183                  regulator_set_suspend_voltage(ldo, rk808_ldo_info[i].suspend_vol);
184                 regulator_enable(ldo);
185                 printk("%s  %s =%duV end\n", __func__,rk808_ldo_info[i].name, regulator_get_voltage(ldo));
186                 regulator_put(ldo);
187         }
188
189         #ifdef CONFIG_RK_CONFIG
190         if(sram_gpio_init(get_port_config(pmic_slp).gpio, &pmic_sleep) < 0){
191                 printk(KERN_ERR "sram_gpio_init failed\n");
192                 return -EINVAL;
193         }
194         if(port_output_init(pmic_slp, 0, "pmic_slp") < 0){
195                 printk(KERN_ERR "port_output_init failed\n");
196                 return -EINVAL;
197         }
198         #else
199         if(sram_gpio_init(PMU_POWER_SLEEP, &pmic_sleep) < 0){
200                 printk(KERN_ERR "sram_gpio_init failed\n");
201                 return -EINVAL;
202         }
203         gpio_request(PMU_POWER_SLEEP, "NULL");
204         gpio_direction_output(PMU_POWER_SLEEP, GPIO_LOW);
205                 
206         #endif
207         
208         printk("%s,line=%d END\n", __func__,__LINE__);
209         
210         
211         return 0;
212 }
213
214 static struct regulator_consumer_supply rk808_buck1_supply[] = {
215         {
216                 .supply = "rk_dcdc1",
217         },
218
219          {
220                 .supply = "vdd_cpu",
221         },
222
223 };
224 static struct regulator_consumer_supply rk808_buck2_supply[] = {
225         {
226                 .supply = "rk_dcdc2",
227         },      
228
229          {
230                 .supply = "vdd_core",
231         },
232
233 };
234 static struct regulator_consumer_supply rk808_buck3_supply[] = {
235         {
236                 .supply = "rk_dcdc3",
237         },
238 };
239
240 static struct regulator_consumer_supply rk808_buck4_supply[] = {
241         {
242                 .supply = "rk_dcdc4",
243         },
244
245 };
246
247 static struct regulator_consumer_supply rk808_ldo1_supply[] = {
248         {
249                 .supply = "rk_ldo1",
250         },
251 };
252 static struct regulator_consumer_supply rk808_ldo2_supply[] = {
253         {
254                 .supply = "rk_ldo2",
255         },
256 };
257
258 static struct regulator_consumer_supply rk808_ldo3_supply[] = {
259         {
260                 .supply = "rk_ldo3",
261         },
262 };
263 static struct regulator_consumer_supply rk808_ldo4_supply[] = {
264         {
265                 .supply = "rk_ldo4",
266         },
267 };
268 static struct regulator_consumer_supply rk808_ldo5_supply[] = {
269         {
270                 .supply = "rk_ldo5",
271         },
272 };
273 static struct regulator_consumer_supply rk808_ldo6_supply[] = {
274         {
275                 .supply = "rk_ldo6",
276         },
277 };
278
279 static struct regulator_consumer_supply rk808_ldo7_supply[] = {
280         {
281                 .supply = "rk_ldo7",
282         },
283 };
284 static struct regulator_consumer_supply rk808_ldo8_supply[] = {
285         {
286                 .supply = "rk_ldo8",
287         },
288 };
289
290 static struct regulator_init_data rk808_buck1 = {
291         .constraints = {
292                 .name           = "RK_DCDC1",
293                 .min_uV                 = 700000,
294                 .max_uV                 = 1500000,
295                 .apply_uV               = 1,
296                 .always_on = 1,
297                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
298                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
299
300         },
301         .num_consumer_supplies = ARRAY_SIZE(rk808_buck1_supply),
302         .consumer_supplies =  rk808_buck1_supply,
303 };
304
305 /* */
306 static struct regulator_init_data rk808_buck2 = {
307         .constraints = {
308                 .name           = "RK_DCDC2",
309                 .min_uV                 = 700000,
310                 .max_uV                 = 1500000,
311                 .apply_uV               = 1,
312                 .always_on = 1,
313                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
314                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
315
316         },
317         .num_consumer_supplies = ARRAY_SIZE(rk808_buck2_supply),
318         .consumer_supplies =  rk808_buck2_supply,
319 };
320
321 /* */
322 static struct regulator_init_data rk808_buck3 = {
323         .constraints = {
324                 .name           = "RK_DCDC3",
325                 .min_uV                 = 1000000,
326                 .max_uV                 = 1800000,
327                 .apply_uV               = 1,
328                 .always_on = 1,
329                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
330                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
331
332         },
333         .num_consumer_supplies = ARRAY_SIZE(rk808_buck3_supply),
334         .consumer_supplies =  rk808_buck3_supply,
335 };
336
337 static struct regulator_init_data rk808_buck4 = {
338         .constraints = {
339                 .name           = "RK_DCDC4",
340                 .min_uV                 = 1800000,
341                 .max_uV                 = 3300000,
342                 .apply_uV               = 1,
343                 .always_on = 1,
344                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
345                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
346
347         },
348         .num_consumer_supplies = ARRAY_SIZE(rk808_buck4_supply),
349         .consumer_supplies =  rk808_buck4_supply,
350 };
351
352 static struct regulator_init_data rk808_ldo1 = {
353         .constraints = {
354                 .name           = "RK_LDO1",
355                 .min_uV                 = 1800000,
356                 .max_uV                 = 3400000,
357                 .apply_uV               = 1,
358                 
359                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
360                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
361
362         },
363         .num_consumer_supplies = ARRAY_SIZE(rk808_ldo1_supply),
364         .consumer_supplies =  rk808_ldo1_supply,
365 };
366
367 /* */
368 static struct regulator_init_data rk808_ldo2 = {
369         .constraints = {
370                 .name           = "RK_LDO2",
371                 .min_uV                 = 1800000,
372                 .max_uV                 = 3400000,
373                 .apply_uV               = 1,
374                 
375                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
376                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
377
378         },
379         .num_consumer_supplies = ARRAY_SIZE(rk808_ldo2_supply),
380         .consumer_supplies =  rk808_ldo2_supply,
381 };
382
383 /* */
384 static struct regulator_init_data rk808_ldo3 = {
385         .constraints = {
386                 .name           = "RK_LDO3",
387                 .min_uV                 = 800000,
388                 .max_uV                 = 2500000,
389                 .apply_uV               = 1,
390                 
391                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
392                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
393
394         },
395         .num_consumer_supplies = ARRAY_SIZE(rk808_ldo3_supply),
396         .consumer_supplies =  rk808_ldo3_supply,
397 };
398
399 /* */
400 static struct regulator_init_data rk808_ldo4 = {
401         .constraints = {
402                 .name           = "RK_LDO4",
403                 .min_uV                 = 1800000,
404                 .max_uV                 = 3400000,
405                 .apply_uV               = 1,
406                 
407                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
408                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
409
410         },
411         .num_consumer_supplies = ARRAY_SIZE(rk808_ldo4_supply),
412         .consumer_supplies =  rk808_ldo4_supply,
413 };
414
415 static struct regulator_init_data rk808_ldo5 = {
416         .constraints = {
417                 .name           = "RK_LDO5",
418                 .min_uV                 = 1800000,
419                 .max_uV                 = 3400000,
420                 .apply_uV               = 1,
421                 
422                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
423                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
424
425         },
426         .num_consumer_supplies = ARRAY_SIZE(rk808_ldo5_supply),
427         .consumer_supplies =  rk808_ldo5_supply,
428 };
429
430 static struct regulator_init_data rk808_ldo6 = {
431         .constraints = {
432                 .name           = "RK_LDO6",
433                 .min_uV                 = 800000,
434                 .max_uV                 = 2500000,
435                 .apply_uV               = 1,
436                 
437                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
438                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
439
440         },
441         .num_consumer_supplies = ARRAY_SIZE(rk808_ldo6_supply),
442         .consumer_supplies =  rk808_ldo6_supply,
443 };
444
445 static struct regulator_init_data rk808_ldo7 = {
446         .constraints = {
447                 .name           = "RK_LDO7",
448                 .min_uV                 = 800000,
449                 .max_uV                 = 2500000,
450                 .apply_uV               = 1,
451                 
452                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
453                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
454
455         },
456         .num_consumer_supplies = ARRAY_SIZE(rk808_ldo7_supply),
457         .consumer_supplies =  rk808_ldo7_supply,
458 };
459
460 static struct regulator_init_data rk808_ldo8 = {
461         .constraints = {
462                 .name           = "RK_LDO8",
463                 .min_uV                 = 1800000,
464                 .max_uV                 = 3400000,
465                 .apply_uV               = 1,
466                 
467                 .valid_ops_mask = REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE,
468                 .valid_modes_mask = REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL,
469
470         },
471         .num_consumer_supplies = ARRAY_SIZE(rk808_ldo8_supply),
472         .consumer_supplies =  rk808_ldo8_supply,
473 };
474
475 struct rk808_regulator_subdev rk808_regulator_subdev_id[] = {
476         {
477                 .id=0,
478                 .initdata=&rk808_buck1,         
479          },
480
481         {
482                 .id=1,
483                 .initdata=&rk808_buck2,         
484          },
485         {
486                 .id=2,
487                 .initdata=&rk808_buck3,         
488          },
489         {
490                 .id=3,
491                 .initdata=&rk808_buck4,         
492          },
493
494         {
495                 .id=4,
496                 .initdata=&rk808_ldo1,          
497          },
498
499         {
500                 .id=5,
501                 .initdata=&rk808_ldo2,          
502          },
503
504         {
505                 .id=6,
506                 .initdata=&rk808_ldo3,          
507          },
508
509         {
510                 .id=7,
511                 .initdata=&rk808_ldo4,          
512          },
513
514         {
515                 .id=8,
516                 .initdata=&rk808_ldo5,          
517          },
518
519         {
520                 .id=9,
521                 .initdata=&rk808_ldo6,          
522          },
523
524         {
525                 .id=10,
526                 .initdata=&rk808_ldo7,          
527          },
528
529         {
530                 .id=11,
531                 .initdata=&rk808_ldo8,          
532          },
533 };
534
535  struct rk808_platform_data rk808_data={
536         .pre_init=rk808_pre_init,
537         .set_init=rk808_set_init,
538         .num_regulators=12,
539         .regulators=rk808_regulator_subdev_id,
540         .irq    = (unsigned)RK808_HOST_IRQ,             
541         .irq_base = IRQ_BOARD_BASE,
542 };
543
544 #ifdef CONFIG_HAS_EARLYSUSPEND
545 void rk808_early_suspend(struct early_suspend *h)
546 {
547 }
548
549 void rk808_late_resume(struct early_suspend *h)
550 {
551 }
552 #endif
553 #ifdef CONFIG_PM
554
555 void rk808_device_suspend(void)
556 {
557 }
558 void rk808_device_resume(void)
559 {
560 }       
561 #endif
562
563 void __sramfunc board_pmu_rk808_suspend(void)
564 {       
565         #ifdef CONFIG_CLK_SWITCH_TO_32K
566          sram_gpio_set_value(pmic_sleep, GPIO_HIGH);  
567         #endif
568 }
569 void __sramfunc board_pmu_rk808_resume(void)
570 {
571         #ifdef CONFIG_CLK_SWITCH_TO_32K
572         sram_gpio_set_value(pmic_sleep, GPIO_LOW);  
573         sram_32k_udelay(1000);
574         #endif
575 }
576
577 #endif
578
579
580
581