rk: revert to v3.10
[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 int 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         if(ret < 0)
188                 printk("###################WARNING:set voltage is error!voltage set is %d mv %d\n",vol_map[val],ret);
189         
190         return ret;
191 }
192
193 static unsigned int syr82x_dcdc_get_mode(struct regulator_dev *dev)
194 {
195         struct syr82x *syr82x = rdev_get_drvdata(dev);
196         u16 mask = 0x40;
197         u16 val;
198         val = syr82x_reg_read(syr82x, SYR82X_BUCK1_SET_VOL_BASE);
199         if (val < 0) {
200                 return val;
201         }
202         val=val & mask;
203         if (val== mask)
204                 return REGULATOR_MODE_FAST;
205         else
206                 return REGULATOR_MODE_NORMAL;
207
208 }
209 static int syr82x_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
210 {
211         struct syr82x *syr82x = rdev_get_drvdata(dev);
212         u16 mask = 0x40;
213
214         switch(mode)
215         {
216         case REGULATOR_MODE_FAST:
217                 return syr82x_set_bits(syr82x,SYR82X_BUCK1_SET_VOL_BASE, mask, mask);
218         case REGULATOR_MODE_NORMAL:
219                 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SET_VOL_BASE, mask, 0);
220         default:
221                 printk("error:dcdc_syr82x only auto and pwm mode\n");
222                 return -EINVAL;
223         }
224 }
225 static int syr82x_dcdc_set_voltage_time_sel(struct regulator_dev *dev,   unsigned int old_selector,
226                                      unsigned int new_selector)
227 {
228         int old_volt, new_volt;
229         
230         old_volt = syr82x_dcdc_list_voltage(dev, old_selector);
231         if (old_volt < 0)
232                 return old_volt;
233         
234         new_volt = syr82x_dcdc_list_voltage(dev, new_selector);
235         if (new_volt < 0)
236                 return new_volt;
237
238         return DIV_ROUND_UP(abs(old_volt - new_volt)*4, 10000);
239 }
240 static int syr82x_dcdc_suspend_enable(struct regulator_dev *dev)
241 {
242         struct syr82x *syr82x = rdev_get_drvdata(dev);
243         u16 mask=0x80;  
244         
245         return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0x80);
246
247 }
248 static int syr82x_dcdc_suspend_disable(struct regulator_dev *dev)
249 {
250         struct syr82x *syr82x = rdev_get_drvdata(dev);
251         u16 mask=0x80;
252          return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0);
253 }
254 static int syr82x_dcdc_set_sleep_voltage(struct regulator_dev *dev,
255                                             int uV)
256 {
257         struct syr82x *syr82x = rdev_get_drvdata(dev);
258         const int *vol_map = buck_voltage_map;
259         u16 val;
260         int ret = 0;
261         
262         if (uV < vol_map[VOL_MIN_IDX] ||
263             uV > vol_map[VOL_MAX_IDX])
264                 return -EINVAL;
265
266         for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
267                 if (vol_map[val] >= uV)
268                         break;
269         }
270
271         if (vol_map[val] > uV)
272                 printk("WARNING:this voltage is not support!voltage set is %d mv\n",vol_map[val]);
273         ret = syr82x_set_bits(syr82x,SYR82X_BUCK1_SLP_VOL_BASE ,BUCK_VOL_MASK, val);    
274         return ret;
275 }
276
277
278 static int syr82x_dcdc_set_suspend_mode(struct regulator_dev *dev, unsigned int mode)
279 {
280         struct syr82x *syr82x = rdev_get_drvdata(dev);
281         u16 mask = 0x40;
282
283         switch(mode)
284         {
285         case REGULATOR_MODE_FAST:
286                 return syr82x_set_bits(syr82x,SYR82X_BUCK1_SLP_VOL_BASE, mask, mask);
287         case REGULATOR_MODE_NORMAL:
288                 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0);
289         default:
290                 printk("error:dcdc_syr82x only auto and pwm mode\n");
291                 return -EINVAL;
292         }
293 }
294
295 static struct regulator_ops syr82x_dcdc_ops = { 
296         .set_voltage = syr82x_dcdc_set_voltage,
297         .get_voltage = syr82x_dcdc_get_voltage,
298         .list_voltage= syr82x_dcdc_list_voltage,
299         .is_enabled = syr82x_dcdc_is_enabled,
300         .enable = syr82x_dcdc_enable,
301         .disable = syr82x_dcdc_disable,
302         .get_mode = syr82x_dcdc_get_mode,
303         .set_mode = syr82x_dcdc_set_mode,
304         .set_suspend_voltage = syr82x_dcdc_set_sleep_voltage,
305         .set_suspend_enable = syr82x_dcdc_suspend_enable,
306         .set_suspend_disable = syr82x_dcdc_suspend_disable,
307         .set_suspend_mode = syr82x_dcdc_set_suspend_mode,
308         .set_voltage_time_sel = syr82x_dcdc_set_voltage_time_sel,
309 };
310 static struct regulator_desc regulators[] = {
311
312         {
313                 .name = "SY_DCDC1",
314                 .id = 0,
315                 .ops = &syr82x_dcdc_ops,
316                 .n_voltages = ARRAY_SIZE(buck_voltage_map),
317                 .type = REGULATOR_VOLTAGE,
318                 .owner = THIS_MODULE,
319         },
320 };
321
322 static int syr82x_i2c_read(struct i2c_client *i2c, char reg, int count, u16 *dest)
323 {
324       int ret;
325     struct i2c_adapter *adap;
326     struct i2c_msg msgs[2];
327
328     if(!i2c)
329                 return ret;
330
331         if (count != 1)
332                 return -EIO;  
333   
334     adap = i2c->adapter;                
335     
336     msgs[0].addr = i2c->addr;
337     msgs[0].buf = &reg;
338     msgs[0].flags = i2c->flags;
339     msgs[0].len = 1;
340     msgs[0].scl_rate = SYR82X_SPEED;
341     
342     msgs[1].buf = (u8 *)dest;
343     msgs[1].addr = i2c->addr;
344     msgs[1].flags = i2c->flags | I2C_M_RD;
345     msgs[1].len = 1;
346     msgs[1].scl_rate = SYR82X_SPEED;
347     ret = i2c_transfer(adap, msgs, 2);
348
349         DBG("***run in %s %d msgs[1].buf = %d\n",__FUNCTION__,__LINE__,*(msgs[1].buf));
350
351         return ret;   
352 }
353
354 static int syr82x_i2c_write(struct i2c_client *i2c, char reg, int count, const u16 src)
355 {
356         int ret=-1;
357         
358         struct i2c_adapter *adap;
359         struct i2c_msg msg;
360         char tx_buf[2];
361
362         if(!i2c)
363                 return ret;
364         if (count != 1)
365                 return -EIO;
366     
367         adap = i2c->adapter;            
368         tx_buf[0] = reg;
369         tx_buf[1] = src;
370         
371         msg.addr = i2c->addr;
372         msg.buf = &tx_buf[0];
373         msg.len = 1 +1;
374         msg.flags = i2c->flags;   
375         msg.scl_rate = SYR82X_SPEED;    
376
377         ret = i2c_transfer(adap, &msg, 1);
378         return ret;     
379 }
380
381 static int syr82x_reg_read(struct syr82x *syr82x, u8 reg)
382 {
383         u16 val = 0;
384         int ret;
385
386         mutex_lock(&syr82x->io_lock);
387
388         ret = syr82x_i2c_read(syr82x->i2c, reg, 1, &val);
389
390         DBG("reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
391         if (ret < 0){
392                 mutex_unlock(&syr82x->io_lock);
393                 return ret;
394         }
395         mutex_unlock(&syr82x->io_lock);
396
397         return val & 0xff;      
398 }
399
400 static int syr82x_set_bits(struct syr82x *syr82x, u8 reg, u16 mask, u16 val)
401 {
402         u16 tmp;
403         int ret;
404
405         mutex_lock(&syr82x->io_lock);
406
407         ret = syr82x_i2c_read(syr82x->i2c, reg, 1, &tmp);
408         DBG("1 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
409         if (ret < 0){
410                 mutex_unlock(&syr82x->io_lock);
411                 return ret;
412         }
413         tmp = (tmp & ~mask) | val;
414         ret = syr82x_i2c_write(syr82x->i2c, reg, 1, tmp);
415         DBG("reg write 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
416         if (ret < 0){
417                 mutex_unlock(&syr82x->io_lock);
418                 return ret;
419         }
420         ret = syr82x_i2c_read(syr82x->i2c, reg, 1, &tmp);
421         DBG("2 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
422         if (ret < 0){
423                 mutex_unlock(&syr82x->io_lock);
424                 return ret;
425         }
426         mutex_unlock(&syr82x->io_lock);
427
428         return 0;//ret; 
429 }
430
431 #ifdef CONFIG_OF
432 static struct of_device_id syr82x_of_match[] = {
433         { .compatible = "silergy,syr82x"},
434         { },
435 };
436 MODULE_DEVICE_TABLE(of, syr82x_of_match);
437 #endif
438 #ifdef CONFIG_OF
439 static struct of_regulator_match syr82x_reg_matches[] = {
440         { .name = "syr82x_dcdc1" ,.driver_data = (void *)0},
441 };
442
443 static struct syr82x_board *syr82x_parse_dt(struct syr82x *syr82x)
444 {
445         struct syr82x_board *pdata;
446         struct device_node *regs;
447         struct device_node *syr82x_np;
448         int count;
449         DBG("%s,line=%d\n", __func__,__LINE__); 
450         
451         syr82x_np = of_node_get(syr82x->dev->of_node);
452         if (!syr82x_np) {
453                 printk("could not find pmic sub-node\n");
454                 return NULL;
455         }
456         
457         regs = of_find_node_by_name(syr82x_np, "regulators");
458         if (!regs)
459                 return NULL;
460         count = of_regulator_match(syr82x->dev, regs, syr82x_reg_matches,
461                                    syr82x_NUM_REGULATORS);
462         of_node_put(regs);
463         pdata = devm_kzalloc(syr82x->dev, sizeof(*pdata), GFP_KERNEL);
464         if (!pdata)
465                 return NULL;
466         pdata->syr82x_init_data[0] = syr82x_reg_matches[0].init_data;
467         pdata->of_node[0] = syr82x_reg_matches[0].of_node;
468         
469         return pdata;
470 }
471
472 #else
473 static struct syr82x_board *syr82x_parse_dt(struct i2c_client *i2c)
474 {
475         return NULL;
476 }
477 #endif
478
479 static int syr82x_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
480 {
481         struct syr82x *syr82x;  
482         struct syr82x_board *pdev ;
483         const struct of_device_id *match;
484         struct regulator_config config = { };
485         struct regulator_dev *sy_rdev;
486         struct regulator_init_data *reg_data;
487         const char *rail_name = NULL;
488         int ret;
489         
490         DBG("%s,line=%d\n", __func__,__LINE__); 
491
492         if (i2c->dev.of_node) {
493                 match = of_match_device(syr82x_of_match, &i2c->dev);
494                 if (!match) {
495                         printk("Failed to find matching dt id\n");
496                         return -EINVAL;
497                 }
498         }
499
500         syr82x = devm_kzalloc(&i2c->dev,sizeof(struct syr82x), GFP_KERNEL);
501         if (syr82x == NULL) {
502                 ret = -ENOMEM;          
503                 goto err;
504         }
505         syr82x->i2c = i2c;
506         syr82x->dev = &i2c->dev;
507         i2c_set_clientdata(i2c, syr82x);
508         g_syr82x = syr82x;
509                 
510         mutex_init(&syr82x->io_lock);   
511
512         ret = syr82x_reg_read(syr82x,SYR82X_ID1_REG);
513         if ((ret <0) ||(ret ==0xff) ||(ret ==0)){
514                 printk("The device is not syr82x %x \n",ret);
515                 goto err;
516         }
517         
518         ret = syr82x_set_bits(syr82x,SYR82X_CONTR_REG1,(1 << 6),(1<<6));  //10mv/2.4us
519
520         if (syr82x->dev->of_node)
521                 pdev = syr82x_parse_dt(syr82x);
522         
523         if (pdev) {
524                 syr82x->num_regulators = syr82x_NUM_REGULATORS;
525                 syr82x->rdev = kcalloc(syr82x_NUM_REGULATORS,sizeof(struct regulator_dev *), GFP_KERNEL);
526                 if (!syr82x->rdev) {
527                         return -ENOMEM;
528                 }
529                 /* Instantiate the regulators */
530                 reg_data = pdev->syr82x_init_data[0];
531                 config.dev = syr82x->dev;
532                 config.driver_data = syr82x;
533                 if (syr82x->dev->of_node)
534                         config.of_node = pdev->of_node[0];
535                         if (reg_data && reg_data->constraints.name)
536                                 rail_name = reg_data->constraints.name;
537                         else
538                                 rail_name = regulators[0].name;
539                         reg_data->supply_regulator = rail_name;
540         
541                 config.init_data =reg_data;
542                 sy_rdev = regulator_register(&regulators[0],&config);
543                 if (IS_ERR(sy_rdev)) {
544                         printk("failed to register regulator\n");
545                 goto err;
546                 }
547                 syr82x->rdev[0] = sy_rdev;
548         }
549         return 0;
550 err:
551         return ret;     
552
553 }
554
555 static int  syr82x_i2c_remove(struct i2c_client *i2c)
556 {
557         struct syr82x *syr82x = i2c_get_clientdata(i2c);
558
559         if (syr82x->rdev[0])
560                 regulator_unregister(syr82x->rdev[0]);
561         i2c_set_clientdata(i2c, NULL);
562         return 0;
563 }
564
565 static const struct i2c_device_id syr82x_i2c_id[] = {
566        { "syr82x", 0 },
567        { }
568 };
569
570 MODULE_DEVICE_TABLE(i2c, syr82x_i2c_id);
571
572 static struct i2c_driver syr82x_i2c_driver = {
573         .driver = {
574                 .name = "syr82x",
575                 .owner = THIS_MODULE,
576                 .of_match_table =of_match_ptr(syr82x_of_match),
577         },
578         .probe    = syr82x_i2c_probe,
579         .remove   = syr82x_i2c_remove,
580         .id_table = syr82x_i2c_id,
581 };
582
583 static int __init syr82x_module_init(void)
584 {
585         int ret;
586         ret = i2c_add_driver(&syr82x_i2c_driver);
587         if (ret != 0)
588                 pr_err("Failed to register I2C driver: %d\n", ret);
589         return ret;
590 }
591 subsys_initcall_sync(syr82x_module_init);
592
593 static void __exit syr82x_module_exit(void)
594 {
595         i2c_del_driver(&syr82x_i2c_driver);
596 }
597 module_exit(syr82x_module_exit);
598
599 MODULE_LICENSE("GPL");
600 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
601 MODULE_DESCRIPTION("syr82x PMIC driver");
602