rk3288:syb827:rename syb827 to syr82x for hardware modify
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / syr82x.c
1 /*
2  * Regulator driver for syr82x DCDC chip for rk32xx
3  *
4  * Copyright (C) 2010, 2011 ROCKCHIP, Inc.
5
6  * Based on syr82x.c that is work by zhangqing<zhangqing@rock-chips.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  */
13
14 #include <linux/bug.h>
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/kernel.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/mutex.h>
22 #include <linux/mfd/core.h>
23
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_gpio.h>
28 #include <linux/of.h>
29 #include <linux/of_device.h>
30 #include <linux/regulator/of_regulator.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/regmap.h>
34
35 #if 0
36 #define DBG(x...)       printk(KERN_INFO x)
37 #else
38 #define DBG(x...)
39 #endif
40 #if 1
41 #define DBG_INFO(x...)  printk(KERN_INFO x)
42 #else
43 #define DBG_INFO(x...)
44 #endif
45 #define PM_CONTROL
46
47 #define SYR82X_SPEED 200*1000
48 #define syr82x_NUM_REGULATORS 1
49
50 struct syr82x {
51         struct device *dev;
52         struct mutex io_lock;
53         struct i2c_client *i2c;
54         int num_regulators;
55         struct regulator_dev **rdev;
56         int irq_base;
57         int chip_irq;
58         int sleep_gpio; /* */
59         unsigned int dcdc_slp_voltage[3]; /* buckx_voltage in uV */
60         bool pmic_sleep;
61         struct regmap *regmap;
62 };
63
64 struct syr82x_regulator {
65         struct device           *dev;
66         struct regulator_desc   *desc;
67         struct regulator_dev    *rdev;
68 };
69 struct syr82x_board {
70         int irq;
71         int irq_base;
72         struct regulator_init_data *syr82x_init_data[syr82x_NUM_REGULATORS];
73         struct device_node *of_node[syr82x_NUM_REGULATORS];
74         int sleep_gpio; /* */
75         unsigned int dcdc_slp_voltage[3]; /* buckx_voltage in uV */
76         bool sleep;
77 };
78
79 struct syr82x_regulator_subdev {
80         int id;
81         struct regulator_init_data *initdata;
82         struct device_node *reg_node;
83 };
84
85 struct syr82x_platform_data {
86         int ono;
87         int num_regulators;
88         struct syr82x_regulator_subdev *regulators;
89         
90         int sleep_gpio; /* */
91         unsigned int dcdc_slp_voltage[3]; /* buckx_voltage in uV */
92         bool sleep;
93 };
94 struct syr82x *g_syr82x;
95
96 static u8 syr82x_reg_read(struct syr82x *syr82x, u8 reg);
97 static int syr82x_set_bits(struct syr82x *syr82x, u8 reg, u16 mask, u16 val);
98
99
100 #define SYR82X_BUCK1_SET_VOL_BASE 0x00
101 #define SYR82X_BUCK1_SLP_VOL_BASE 0x01
102 #define SYR82X_CONTR_REG1 0x02
103 #define SYR82X_ID1_REG 0x03
104 #define SYR82X_ID2_REG 0x04
105 #define SYR82X_CONTR_REG2 0x05
106
107 #define BUCK_VOL_MASK 0x3f
108 #define VOL_MIN_IDX 0x00
109 #define VOL_MAX_IDX 0x3f
110
111 const static int buck_voltage_map[] = {
112          712500, 725000, 737500,750000, 762500,775000,787500,800000,
113          812500, 825000, 837500,850000, 862500,875000,887500,900000,
114          912500, 925000, 937500,950000, 962500,975000,987500,1000000,
115          1012500, 1025000, 1037500,1050000, 1062500,1075000,1087500,1100000,
116          1112500, 1125000, 1137500,1150000, 1162500,1175000,1187500,1200000,
117          1212500, 1225000, 1237500,1250000, 1262500,1275000,1287500,1300000,
118          1312500, 1325000, 1337500,1350000, 1362500,1375000,1387500,1400000,
119          1412500, 1425000, 1437500,1450000, 1462500,1475000,1487500,1500000,
120 };
121
122 static int syr82x_dcdc_list_voltage(struct regulator_dev *dev, unsigned index)
123 {
124         if (index >= ARRAY_SIZE(buck_voltage_map))
125                 return -EINVAL;
126         return  buck_voltage_map[index];
127 }
128 static int syr82x_dcdc_is_enabled(struct regulator_dev *dev)
129 {
130         struct syr82x *syr82x = rdev_get_drvdata(dev);
131         u16 val;
132         u16 mask=0x80;  
133         val = syr82x_reg_read(syr82x, SYR82X_BUCK1_SET_VOL_BASE);
134         if (val < 0)
135                 return val;
136          val=val&~0x7f;
137         if (val & mask)
138                 return 1;
139         else
140                 return 0;       
141 }
142 static int syr82x_dcdc_enable(struct regulator_dev *dev)
143 {
144         struct syr82x *syr82x = rdev_get_drvdata(dev);
145         u16 mask=0x80;  
146
147         return syr82x_set_bits(syr82x, SYR82X_BUCK1_SET_VOL_BASE, mask, 0x80);
148
149 }
150 static int syr82x_dcdc_disable(struct regulator_dev *dev)
151 {
152         struct syr82x *syr82x = rdev_get_drvdata(dev);
153         u16 mask=0x80;
154          return syr82x_set_bits(syr82x, SYR82X_BUCK1_SET_VOL_BASE, mask, 0);
155 }
156 static int syr82x_dcdc_get_voltage(struct regulator_dev *dev)
157 {
158         struct syr82x *syr82x = rdev_get_drvdata(dev);
159         u16 reg = 0;
160         int val;
161         reg = syr82x_reg_read(syr82x,SYR82X_BUCK1_SET_VOL_BASE);
162         reg &= BUCK_VOL_MASK;
163         val = buck_voltage_map[reg];    
164         return val;
165 }
166 static int syr82x_dcdc_set_voltage(struct regulator_dev *dev,
167                                   int min_uV, int max_uV,unsigned *selector)
168 {
169         struct syr82x *syr82x = rdev_get_drvdata(dev);
170         const int *vol_map = buck_voltage_map;
171         u16 val;
172         int ret = 0;
173         
174         if (min_uV < vol_map[VOL_MIN_IDX] ||
175             min_uV > vol_map[VOL_MAX_IDX])
176                 return -EINVAL;
177
178         for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
179                 if (vol_map[val] >= min_uV)
180                         break;
181         }
182
183         if (vol_map[val] > max_uV)
184                 printk("WARNING:this voltage is not support!voltage set is %d mv\n",vol_map[val]);
185
186         ret = syr82x_set_bits(syr82x, SYR82X_BUCK1_SET_VOL_BASE ,BUCK_VOL_MASK, val);
187         
188         return ret;
189 }
190
191 static unsigned int syr82x_dcdc_get_mode(struct regulator_dev *dev)
192 {
193         struct syr82x *syr82x = rdev_get_drvdata(dev);
194         u16 mask = 0x40;
195         u16 val;
196         val = syr82x_reg_read(syr82x, SYR82X_BUCK1_SET_VOL_BASE);
197         if (val < 0) {
198                 return val;
199         }
200         val=val & mask;
201         if (val== mask)
202                 return REGULATOR_MODE_FAST;
203         else
204                 return REGULATOR_MODE_NORMAL;
205
206 }
207 static int syr82x_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
208 {
209         struct syr82x *syr82x = rdev_get_drvdata(dev);
210         u16 mask = 0x40;
211
212         switch(mode)
213         {
214         case REGULATOR_MODE_FAST:
215                 return syr82x_set_bits(syr82x,SYR82X_BUCK1_SET_VOL_BASE, mask, mask);
216         case REGULATOR_MODE_NORMAL:
217                 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SET_VOL_BASE, mask, 0);
218         default:
219                 printk("error:dcdc_syr82x only auto and pwm mode\n");
220                 return -EINVAL;
221         }
222 }
223 static int syr82x_dcdc_set_voltage_time_sel(struct regulator_dev *dev,   unsigned int old_selector,
224                                      unsigned int new_selector)
225 {
226         int old_volt, new_volt;
227         
228         old_volt = syr82x_dcdc_list_voltage(dev, old_selector);
229         if (old_volt < 0)
230                 return old_volt;
231         
232         new_volt = syr82x_dcdc_list_voltage(dev, new_selector);
233         if (new_volt < 0)
234                 return new_volt;
235
236         return DIV_ROUND_UP(abs(old_volt - new_volt)*4, 10000);
237 }
238 static int syr82x_dcdc_suspend_enable(struct regulator_dev *dev)
239 {
240         struct syr82x *syr82x = rdev_get_drvdata(dev);
241         u16 mask=0x80;  
242         
243         return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0x80);
244
245 }
246 static int syr82x_dcdc_suspend_disable(struct regulator_dev *dev)
247 {
248         struct syr82x *syr82x = rdev_get_drvdata(dev);
249         u16 mask=0x80;
250          return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0);
251 }
252 static int syr82x_dcdc_set_sleep_voltage(struct regulator_dev *dev,
253                                             int uV)
254 {
255         struct syr82x *syr82x = rdev_get_drvdata(dev);
256         const int *vol_map = buck_voltage_map;
257         u16 val;
258         int ret = 0;
259         
260         if (uV < vol_map[VOL_MIN_IDX] ||
261             uV > vol_map[VOL_MAX_IDX])
262                 return -EINVAL;
263
264         for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
265                 if (vol_map[val] >= uV)
266                         break;
267         }
268
269         if (vol_map[val] > uV)
270                 printk("WARNING:this voltage is not support!voltage set is %d mv\n",vol_map[val]);
271         ret = syr82x_set_bits(syr82x,SYR82X_BUCK1_SLP_VOL_BASE ,BUCK_VOL_MASK, val);    
272         return ret;
273 }
274
275
276 static int syr82x_dcdc_set_suspend_mode(struct regulator_dev *dev, unsigned int mode)
277 {
278         struct syr82x *syr82x = rdev_get_drvdata(dev);
279         u16 mask = 0x40;
280
281         switch(mode)
282         {
283         case REGULATOR_MODE_FAST:
284                 return syr82x_set_bits(syr82x,SYR82X_BUCK1_SLP_VOL_BASE, mask, mask);
285         case REGULATOR_MODE_NORMAL:
286                 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0);
287         default:
288                 printk("error:dcdc_syr82x only auto and pwm mode\n");
289                 return -EINVAL;
290         }
291 }
292
293 static struct regulator_ops syr82x_dcdc_ops = { 
294         .set_voltage = syr82x_dcdc_set_voltage,
295         .get_voltage = syr82x_dcdc_get_voltage,
296         .list_voltage= syr82x_dcdc_list_voltage,
297         .is_enabled = syr82x_dcdc_is_enabled,
298         .enable = syr82x_dcdc_enable,
299         .disable = syr82x_dcdc_disable,
300         .get_mode = syr82x_dcdc_get_mode,
301         .set_mode = syr82x_dcdc_set_mode,
302         .set_suspend_voltage = syr82x_dcdc_set_sleep_voltage,
303         .set_suspend_enable = syr82x_dcdc_suspend_enable,
304         .set_suspend_disable = syr82x_dcdc_suspend_disable,
305         .set_suspend_mode = syr82x_dcdc_set_suspend_mode,
306         .set_voltage_time_sel = syr82x_dcdc_set_voltage_time_sel,
307 };
308 static struct regulator_desc regulators[] = {
309
310         {
311                 .name = "SY_DCDC1",
312                 .id = 0,
313                 .ops = &syr82x_dcdc_ops,
314                 .n_voltages = ARRAY_SIZE(buck_voltage_map),
315                 .type = REGULATOR_VOLTAGE,
316                 .owner = THIS_MODULE,
317         },
318 };
319
320 static int syr82x_i2c_read(struct i2c_client *i2c, char reg, int count, u16 *dest)
321 {
322       int ret;
323     struct i2c_adapter *adap;
324     struct i2c_msg msgs[2];
325
326     if(!i2c)
327                 return ret;
328
329         if (count != 1)
330                 return -EIO;  
331   
332     adap = i2c->adapter;                
333     
334     msgs[0].addr = i2c->addr;
335     msgs[0].buf = &reg;
336     msgs[0].flags = i2c->flags;
337     msgs[0].len = 1;
338     msgs[0].scl_rate = SYR82X_SPEED;
339     
340     msgs[1].buf = (u8 *)dest;
341     msgs[1].addr = i2c->addr;
342     msgs[1].flags = i2c->flags | I2C_M_RD;
343     msgs[1].len = 1;
344     msgs[1].scl_rate = SYR82X_SPEED;
345     ret = i2c_transfer(adap, msgs, 2);
346
347         DBG("***run in %s %d msgs[1].buf = %d\n",__FUNCTION__,__LINE__,*(msgs[1].buf));
348
349         return 0;   
350 }
351
352 static int syr82x_i2c_write(struct i2c_client *i2c, char reg, int count, const u16 src)
353 {
354         int ret=-1;
355         
356         struct i2c_adapter *adap;
357         struct i2c_msg msg;
358         char tx_buf[2];
359
360         if(!i2c)
361                 return ret;
362         if (count != 1)
363                 return -EIO;
364     
365         adap = i2c->adapter;            
366         tx_buf[0] = reg;
367         tx_buf[1] = src;
368         
369         msg.addr = i2c->addr;
370         msg.buf = &tx_buf[0];
371         msg.len = 1 +1;
372         msg.flags = i2c->flags;   
373         msg.scl_rate = SYR82X_SPEED;    
374
375         ret = i2c_transfer(adap, &msg, 1);
376         return ret;     
377 }
378
379 static u8 syr82x_reg_read(struct syr82x *syr82x, u8 reg)
380 {
381         u16 val = 0;
382
383         mutex_lock(&syr82x->io_lock);
384
385         syr82x_i2c_read(syr82x->i2c, reg, 1, &val);
386
387         DBG("reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
388
389         mutex_unlock(&syr82x->io_lock);
390
391         return val & 0xff;      
392 }
393
394 static int syr82x_set_bits(struct syr82x *syr82x, u8 reg, u16 mask, u16 val)
395 {
396         u16 tmp;
397         int ret;
398
399         mutex_lock(&syr82x->io_lock);
400
401         ret = syr82x_i2c_read(syr82x->i2c, reg, 1, &tmp);
402         DBG("1 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
403         tmp = (tmp & ~mask) | val;
404         if (ret == 0) {
405                 ret = syr82x_i2c_write(syr82x->i2c, reg, 1, tmp);
406                 DBG("reg write 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
407         }
408         syr82x_i2c_read(syr82x->i2c, reg, 1, &tmp);
409         DBG("2 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
410         mutex_unlock(&syr82x->io_lock);
411
412         return 0;//ret; 
413 }
414
415 #ifdef CONFIG_OF
416 static struct of_device_id syr82x_of_match[] = {
417         { .compatible = "silergy,syr82x"},
418         { },
419 };
420 MODULE_DEVICE_TABLE(of, syr82x_of_match);
421 #endif
422 #ifdef CONFIG_OF
423 static struct of_regulator_match syr82x_reg_matches[] = {
424         { .name = "syr82x_dcdc1" ,.driver_data = (void *)0},
425 };
426
427 static struct syr82x_board *syr82x_parse_dt(struct syr82x *syr82x)
428 {
429         struct syr82x_board *pdata;
430         struct device_node *regs;
431         struct device_node *syr82x_np;
432         int gpio,count;
433         DBG("%s,line=%d\n", __func__,__LINE__); 
434         
435         syr82x_np = of_node_get(syr82x->dev->of_node);
436         if (!syr82x_np) {
437                 printk("could not find pmic sub-node\n");
438                 return NULL;
439         }
440         
441         regs = of_find_node_by_name(syr82x_np, "regulators");
442         if (!regs)
443                 return NULL;
444         count = of_regulator_match(syr82x->dev, regs, syr82x_reg_matches,
445                                    syr82x_NUM_REGULATORS);
446         of_node_put(regs);
447         pdata = devm_kzalloc(syr82x->dev, sizeof(*pdata), GFP_KERNEL);
448         if (!pdata)
449                 return NULL;
450         pdata->syr82x_init_data[0] = syr82x_reg_matches[0].init_data;
451         pdata->of_node[0] = syr82x_reg_matches[0].of_node;
452         
453         return pdata;
454 }
455
456 #else
457 static struct syr82x_board *syr82x_parse_dt(struct i2c_client *i2c)
458 {
459         return NULL;
460 }
461 #endif
462
463 static int syr82x_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
464 {
465         struct syr82x *syr82x;  
466         struct syr82x_board *pdev ;
467         const struct of_device_id *match;
468         struct regulator_config config = { };
469         struct regulator_dev *sy_rdev;
470         struct regulator_init_data *reg_data;
471         const char *rail_name = NULL;
472         int ret;
473         
474         DBG("%s,line=%d\n", __func__,__LINE__); 
475
476         if (i2c->dev.of_node) {
477                 match = of_match_device(syr82x_of_match, &i2c->dev);
478                 if (!match) {
479                         printk("Failed to find matching dt id\n");
480                         return -EINVAL;
481                 }
482         }
483
484         syr82x = devm_kzalloc(&i2c->dev,sizeof(struct syr82x), GFP_KERNEL);
485         if (syr82x == NULL) {
486                 ret = -ENOMEM;          
487                 goto err;
488         }
489         syr82x->i2c = i2c;
490         syr82x->dev = &i2c->dev;
491         i2c_set_clientdata(i2c, syr82x);
492         g_syr82x = syr82x;
493                 
494         mutex_init(&syr82x->io_lock);   
495
496         ret = syr82x_reg_read(syr82x,SYR82X_ID1_REG);
497         if ((ret <0) ||(ret ==0xff) ||(ret ==0)){
498                 printk("The device is not syr82x %x \n",ret);
499                 goto err;
500         }
501         
502         ret = syr82x_set_bits(syr82x,SYR82X_CONTR_REG1,(1 << 6),(1<<6));  //10mv/2.4us
503
504         if (syr82x->dev->of_node)
505                 pdev = syr82x_parse_dt(syr82x);
506         
507         if (pdev) {
508                 syr82x->num_regulators = syr82x_NUM_REGULATORS;
509                 syr82x->rdev = kcalloc(syr82x_NUM_REGULATORS,sizeof(struct regulator_dev *), GFP_KERNEL);
510                 if (!syr82x->rdev) {
511                         return -ENOMEM;
512                 }
513                 /* Instantiate the regulators */
514                 reg_data = pdev->syr82x_init_data[0];
515                 config.dev = syr82x->dev;
516                 config.driver_data = syr82x;
517                 if (syr82x->dev->of_node)
518                         config.of_node = pdev->of_node[0];
519                         if (reg_data && reg_data->constraints.name)
520                                 rail_name = reg_data->constraints.name;
521                         else
522                                 rail_name = regulators[0].name;
523                         reg_data->supply_regulator = rail_name;
524         
525                 config.init_data =reg_data;
526                 sy_rdev = regulator_register(&regulators[0],&config);
527                 if (IS_ERR(sy_rdev)) {
528                         printk("failed to register regulator\n");
529                 goto err;
530                 }
531                 syr82x->rdev[0] = sy_rdev;
532         }
533         return 0;
534 err:
535         return ret;     
536
537 }
538
539 static int  syr82x_i2c_remove(struct i2c_client *i2c)
540 {
541         struct syr82x *syr82x = i2c_get_clientdata(i2c);
542
543         if (syr82x->rdev[0])
544                 regulator_unregister(syr82x->rdev[0]);
545         i2c_set_clientdata(i2c, NULL);
546         return 0;
547 }
548
549 static const struct i2c_device_id syr82x_i2c_id[] = {
550        { "syr82x", 0 },
551        { }
552 };
553
554 MODULE_DEVICE_TABLE(i2c, syr82x_i2c_id);
555
556 static struct i2c_driver syr82x_i2c_driver = {
557         .driver = {
558                 .name = "syr82x",
559                 .owner = THIS_MODULE,
560                 .of_match_table =of_match_ptr(syr82x_of_match),
561         },
562         .probe    = syr82x_i2c_probe,
563         .remove   = syr82x_i2c_remove,
564         .id_table = syr82x_i2c_id,
565 };
566
567 static int __init syr82x_module_init(void)
568 {
569         int ret;
570         ret = i2c_add_driver(&syr82x_i2c_driver);
571         if (ret != 0)
572                 pr_err("Failed to register I2C driver: %d\n", ret);
573         return ret;
574 }
575 subsys_initcall_sync(syr82x_module_init);
576
577 static void __exit syr82x_module_exit(void)
578 {
579         i2c_del_driver(&syr82x_i2c_driver);
580 }
581 module_exit(syr82x_module_exit);
582
583 MODULE_LICENSE("GPL");
584 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
585 MODULE_DESCRIPTION("syr82x PMIC driver");
586