Merge remote-tracking branch 'origin/upstream/linux-linaro-lsk-v3.10-android+android...
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / act8846.c
1 /*
2  * Regulator driver for Active-semi act8846 PMIC chip for rk29xx
3  *
4  * Copyright (C) 2010, 2011 ROCKCHIP, Inc.
5
6  * Based on act8846.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/regulator/act8846.h>
20 #include <mach/gpio.h>
21 #include <linux/delay.h>
22 #include <mach/iomux.h>
23 #include <linux/slab.h>
24 #ifdef CONFIG_HAS_EARLYSUSPEND
25 #include <linux/earlysuspend.h>
26 #endif
27
28
29 #if 0
30 #define DBG(x...)       printk(KERN_INFO x)
31 #else
32 #define DBG(x...)
33 #endif
34 #if 1
35 #define DBG_INFO(x...)  printk(KERN_INFO x)
36 #else
37 #define DBG_INFO(x...)
38 #endif
39 #define PM_CONTROL
40
41 struct act8846 {
42         struct device *dev;
43         struct mutex io_lock;
44         struct i2c_client *i2c;
45         int num_regulators;
46         struct regulator_dev **rdev;
47         struct early_suspend act8846_suspend;
48 };
49
50 struct act8846 *g_act8846;
51
52 static u8 act8846_reg_read(struct act8846 *act8846, u8 reg);
53 static int act8846_set_bits(struct act8846 *act8846, u8 reg, u16 mask, u16 val);
54
55
56 #define act8846_BUCK1_SET_VOL_BASE 0x10
57 #define act8846_BUCK2_SET_VOL_BASE 0x20
58 #define act8846_BUCK3_SET_VOL_BASE 0x30
59 #define act8846_BUCK4_SET_VOL_BASE 0x40
60
61 #define act8846_BUCK2_SLP_VOL_BASE 0x21
62 #define act8846_BUCK3_SLP_VOL_BASE 0x31
63 #define act8846_BUCK4_SLP_VOL_BASE 0x41
64
65 #define act8846_LDO1_SET_VOL_BASE 0x50
66 #define act8846_LDO2_SET_VOL_BASE 0x58
67 #define act8846_LDO3_SET_VOL_BASE 0x60
68 #define act8846_LDO4_SET_VOL_BASE 0x68
69 #define act8846_LDO5_SET_VOL_BASE 0x70
70 #define act8846_LDO6_SET_VOL_BASE 0x80
71 #define act8846_LDO7_SET_VOL_BASE 0x90
72 #define act8846_LDO8_SET_VOL_BASE 0xa0
73 //#define act8846_LDO9_SET_VOL_BASE 0xb1
74
75 #define act8846_BUCK1_CONTR_BASE 0x12
76 #define act8846_BUCK2_CONTR_BASE 0x22
77 #define act8846_BUCK3_CONTR_BASE 0x32
78 #define act8846_BUCK4_CONTR_BASE 0x42
79
80 #define act8846_LDO1_CONTR_BASE 0x51
81 #define act8846_LDO2_CONTR_BASE 0x59
82 #define act8846_LDO3_CONTR_BASE 0x61
83 #define act8846_LDO4_CONTR_BASE 0x69
84 #define act8846_LDO5_CONTR_BASE 0x71
85 #define act8846_LDO6_CONTR_BASE 0x81
86 #define act8846_LDO7_CONTR_BASE 0x91
87 #define act8846_LDO8_CONTR_BASE 0xa1
88 //#define act8846_LDO9_CONTR_BASE 0xb1
89
90 #define BUCK_VOL_MASK 0x3f
91 #define LDO_VOL_MASK 0x3f
92
93 #define VOL_MIN_IDX 0x00
94 #define VOL_MAX_IDX 0x3f
95
96 const static int buck_set_vol_base_addr[] = {
97         act8846_BUCK1_SET_VOL_BASE,
98         act8846_BUCK2_SET_VOL_BASE,
99         act8846_BUCK3_SET_VOL_BASE,
100         act8846_BUCK4_SET_VOL_BASE,
101 };
102 const static int buck_contr_base_addr[] = {
103         act8846_BUCK1_CONTR_BASE,
104         act8846_BUCK2_CONTR_BASE,
105         act8846_BUCK3_CONTR_BASE,
106         act8846_BUCK4_CONTR_BASE,
107 };
108 #define act8846_BUCK_SET_VOL_REG(x) (buck_set_vol_base_addr[x])
109 #define act8846_BUCK_CONTR_REG(x) (buck_contr_base_addr[x])
110
111
112 const static int ldo_set_vol_base_addr[] = {
113         act8846_LDO1_SET_VOL_BASE,
114         act8846_LDO2_SET_VOL_BASE,
115         act8846_LDO3_SET_VOL_BASE,
116         act8846_LDO4_SET_VOL_BASE, 
117         act8846_LDO5_SET_VOL_BASE, 
118         act8846_LDO6_SET_VOL_BASE, 
119         act8846_LDO7_SET_VOL_BASE, 
120         act8846_LDO8_SET_VOL_BASE, 
121 //      act8846_LDO9_SET_VOL_BASE, 
122 };
123 const static int ldo_contr_base_addr[] = {
124         act8846_LDO1_CONTR_BASE,
125         act8846_LDO2_CONTR_BASE,
126         act8846_LDO3_CONTR_BASE,
127         act8846_LDO4_CONTR_BASE,
128         act8846_LDO5_CONTR_BASE,
129         act8846_LDO6_CONTR_BASE,
130         act8846_LDO7_CONTR_BASE,
131         act8846_LDO8_CONTR_BASE,
132 //      act8846_LDO9_CONTR_BASE,
133 };
134 #define act8846_LDO_SET_VOL_REG(x) (ldo_set_vol_base_addr[x])
135 #define act8846_LDO_CONTR_REG(x) (ldo_contr_base_addr[x])
136
137 const static int buck_voltage_map[] = {
138          600, 625, 650, 675, 700, 725, 750, 775,
139          800, 825, 850, 875, 900, 925, 950, 975,
140          1000, 1025, 1050, 1075, 1100, 1125, 1150,
141          1175, 1200, 1250, 1300, 1350, 1400, 1450,
142          1500, 1550, 1600, 1650, 1700, 1750, 1800, 
143          1850, 1900, 1950, 2000, 2050, 2100, 2150, 
144          2200, 2250, 2300, 2350, 2400, 2500, 2600, 
145          2700, 2800, 2850, 2900, 3000, 3100, 3200,
146          3300, 3400, 3500, 3600, 3700, 3800, 3900,
147 };
148
149 const static int ldo_voltage_map[] = {
150          600, 625, 650, 675, 700, 725, 750, 775,
151          800, 825, 850, 875, 900, 925, 950, 975,
152          1000, 1025, 1050, 1075, 1100, 1125, 1150,
153          1175, 1200, 1250, 1300, 1350, 1400, 1450,
154          1500, 1550, 1600, 1650, 1700, 1750, 1800, 
155          1850, 1900, 1950, 2000, 2050, 2100, 2150, 
156          2200, 2250, 2300, 2350, 2400, 2500, 2600, 
157          2700, 2800, 2850, 2900, 3000, 3100, 3200,
158          3300, 3400, 3500, 3600, 3700, 3800, 3900,
159 };
160
161 static int act8846_ldo_list_voltage(struct regulator_dev *dev, unsigned index)
162 {
163         if (index >= ARRAY_SIZE(ldo_voltage_map))
164                 return -EINVAL;
165         return 1000 * ldo_voltage_map[index];
166 }
167 static int act8846_ldo_is_enabled(struct regulator_dev *dev)
168 {
169         struct act8846 *act8846 = rdev_get_drvdata(dev);
170         int ldo = rdev_get_id(dev) - ACT8846_LDO1;
171         u16 val;
172         u16 mask=0x80;
173         val = act8846_reg_read(act8846, act8846_LDO_CONTR_REG(ldo));     
174         if (val < 0)
175                 return val;
176         val=val&~0x7f;
177         if (val & mask)
178                 return 1;
179         else
180                 return 0;       
181 }
182 static int act8846_ldo_enable(struct regulator_dev *dev)
183 {
184         struct act8846 *act8846 = rdev_get_drvdata(dev);
185         int ldo= rdev_get_id(dev) - ACT8846_LDO1;
186         u16 mask=0x80;  
187         
188         return act8846_set_bits(act8846, act8846_LDO_CONTR_REG(ldo), mask, 0x80);
189         
190 }
191 static int act8846_ldo_disable(struct regulator_dev *dev)
192 {
193         struct act8846 *act8846 = rdev_get_drvdata(dev);
194         int ldo= rdev_get_id(dev) - ACT8846_LDO1;
195         u16 mask=0x80;
196         
197         return act8846_set_bits(act8846, act8846_LDO_CONTR_REG(ldo), mask, 0);
198
199 }
200 static int act8846_ldo_get_voltage(struct regulator_dev *dev)
201 {
202         struct act8846 *act8846 = rdev_get_drvdata(dev);
203         int ldo= rdev_get_id(dev) - ACT8846_LDO1;
204         u16 reg = 0;
205         int val;
206         reg = act8846_reg_read(act8846,act8846_LDO_SET_VOL_REG(ldo));
207         reg &= LDO_VOL_MASK;
208         val = 1000 * ldo_voltage_map[reg];      
209         return val;
210 }
211 static int act8846_ldo_set_voltage(struct regulator_dev *dev,
212                                   int min_uV, int max_uV,unsigned *selector)
213 {
214         struct act8846 *act8846 = rdev_get_drvdata(dev);
215         int ldo= rdev_get_id(dev) - ACT8846_LDO1;
216         int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
217         const int *vol_map =ldo_voltage_map;
218         u16 val;
219         int ret = 0;
220         if (min_vol < vol_map[VOL_MIN_IDX] ||
221             min_vol > vol_map[VOL_MAX_IDX])
222                 return -EINVAL;
223
224         for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
225                 if (vol_map[val] >= min_vol)
226                         break;  
227         }
228                 
229         if (vol_map[val] > max_vol)
230                 return -EINVAL;
231
232         ret = act8846_set_bits(act8846, act8846_LDO_SET_VOL_REG(ldo),
233                 LDO_VOL_MASK, val);
234         return ret;
235
236 }
237 static unsigned int act8846_ldo_get_mode(struct regulator_dev *dev)
238 {
239         struct act8846 *act8846 = rdev_get_drvdata(dev);
240         int ldo = rdev_get_id(dev) - ACT8846_LDO1;
241         u16 mask = 0x80;
242         u16 val;
243         val = act8846_reg_read(act8846, act8846_LDO_CONTR_REG(ldo));
244         if (val < 0) {
245                 return val;
246         }
247         val=val & mask;
248         if (val== mask)
249                 return REGULATOR_MODE_NORMAL;
250         else
251                 return REGULATOR_MODE_STANDBY;
252
253 }
254 static int act8846_ldo_set_mode(struct regulator_dev *dev, unsigned int mode)
255 {
256         struct act8846 *act8846 = rdev_get_drvdata(dev);
257         int ldo = rdev_get_id(dev) - ACT8846_LDO1;
258         u16 mask = 0x80;
259         switch(mode)
260         {
261         case REGULATOR_MODE_NORMAL:
262                 return act8846_set_bits(act8846, act8846_LDO_CONTR_REG(ldo), mask, mask);               
263         case REGULATOR_MODE_STANDBY:
264                 return act8846_set_bits(act8846, act8846_LDO_CONTR_REG(ldo), mask, 0);
265         default:
266                 printk("error:pmu_act8846 only lowpower and nomal mode\n");
267                 return -EINVAL;
268         }
269
270
271 }
272 static struct regulator_ops act8846_ldo_ops = {
273         .set_voltage = act8846_ldo_set_voltage,
274         .get_voltage = act8846_ldo_get_voltage,
275         .list_voltage = act8846_ldo_list_voltage,
276         .is_enabled = act8846_ldo_is_enabled,
277         .enable = act8846_ldo_enable,
278         .disable = act8846_ldo_disable,
279         .get_mode = act8846_ldo_get_mode,
280         .set_mode = act8846_ldo_set_mode,
281         
282 };
283
284 static int act8846_dcdc_list_voltage(struct regulator_dev *dev, unsigned index)
285 {
286         if (index >= ARRAY_SIZE(buck_voltage_map))
287                 return -EINVAL;
288         return 1000 * buck_voltage_map[index];
289 }
290 static int act8846_dcdc_is_enabled(struct regulator_dev *dev)
291 {
292         struct act8846 *act8846 = rdev_get_drvdata(dev);
293         int buck = rdev_get_id(dev) - ACT8846_DCDC1;
294         u16 val;
295         u16 mask=0x80;  
296         val = act8846_reg_read(act8846, act8846_BUCK_CONTR_REG(buck));
297         if (val < 0)
298                 return val;
299          val=val&~0x7f;
300         if (val & mask)
301                 return 1;
302         else
303                 return 0;       
304 }
305 static int act8846_dcdc_enable(struct regulator_dev *dev)
306 {
307         struct act8846 *act8846 = rdev_get_drvdata(dev);
308         int buck = rdev_get_id(dev) - ACT8846_DCDC1;
309         u16 mask=0x80;  
310         return act8846_set_bits(act8846, act8846_BUCK_CONTR_REG(buck), mask, 0x80);
311
312 }
313 static int act8846_dcdc_disable(struct regulator_dev *dev)
314 {
315         struct act8846 *act8846 = rdev_get_drvdata(dev);
316         int buck = rdev_get_id(dev) - ACT8846_DCDC1;
317         u16 mask=0x80;
318          return act8846_set_bits(act8846, act8846_BUCK_CONTR_REG(buck), mask, 0);
319 }
320 static int act8846_dcdc_get_voltage(struct regulator_dev *dev)
321 {
322         struct act8846 *act8846 = rdev_get_drvdata(dev);
323         int buck = rdev_get_id(dev) - ACT8846_DCDC1;
324         u16 reg = 0;
325         int val;
326         #ifdef CONFIG_ACT8846_SUPPORT_RESET
327         reg = act8846_reg_read(act8846,(act8846_BUCK_SET_VOL_REG(buck)+0x1));
328         #else
329         reg = act8846_reg_read(act8846,act8846_BUCK_SET_VOL_REG(buck));
330         #endif
331         reg &= BUCK_VOL_MASK;
332         DBG("%d\n", reg);
333         val = 1000 * buck_voltage_map[reg];     
334         DBG("%d\n", val);
335         return val;
336 }
337 static int act8846_dcdc_set_voltage(struct regulator_dev *dev,
338                                   int min_uV, int max_uV,unsigned *selector)
339 {
340         struct act8846 *act8846 = rdev_get_drvdata(dev);
341         int buck = rdev_get_id(dev) - ACT8846_DCDC1;
342         int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
343         const int *vol_map = buck_voltage_map;
344         u16 val;
345         int ret = 0;
346
347         DBG("%s, min_uV = %d, max_uV = %d!\n", __func__, min_uV, max_uV);
348         if (min_vol < vol_map[VOL_MIN_IDX] ||
349             min_vol > vol_map[VOL_MAX_IDX])
350                 return -EINVAL;
351
352         for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
353                 if (vol_map[val] >= min_vol)
354                         break;
355         }
356
357         if (vol_map[val] > max_vol)
358                 printk("WARNING:this voltage is not support!voltage set is %d mv\n",vol_map[val]);
359
360         #ifdef CONFIG_ACT8846_SUPPORT_RESET
361         ret = act8846_set_bits(act8846, (act8846_BUCK_SET_VOL_REG(buck) +0x1),BUCK_VOL_MASK, val);
362         #else
363         ret = act8846_set_bits(act8846, act8846_BUCK_SET_VOL_REG(buck) ,BUCK_VOL_MASK, val);
364         #endif
365         
366         return ret;
367 }
368 static int act8846_dcdc_set_sleep_voltage(struct regulator_dev *dev,
369                                             int uV)
370 {
371         struct act8846 *act8846 = rdev_get_drvdata(dev);
372         int buck = rdev_get_id(dev) - ACT8846_DCDC1;
373         int min_vol = uV / 1000,max_vol = uV / 1000;
374         const int *vol_map = buck_voltage_map;
375         u16 val;
376         int ret = 0;
377
378         DBG("%s, min_uV = %d, max_uV = %d!\n", __func__, min_uV, max_uV);
379         if (min_vol < vol_map[VOL_MIN_IDX] ||
380             min_vol > vol_map[VOL_MAX_IDX])
381                 return -EINVAL;
382
383         for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
384                 if (vol_map[val] >= min_vol)
385                         break;
386         }
387
388         if (vol_map[val] > max_vol)
389                 printk("WARNING:this voltage is not support!voltage set is %d mv\n",vol_map[val]);
390         #ifdef CONFIG_ACT8846_SUPPORT_RESET
391          ret = act8846_set_bits(act8846, (act8846_BUCK_SET_VOL_REG(buck) ),BUCK_VOL_MASK, val);
392         #else
393         ret = act8846_set_bits(act8846, (act8846_BUCK_SET_VOL_REG(buck) +0x01),BUCK_VOL_MASK, val);
394         #endif
395         
396         return ret;
397 }
398 static unsigned int act8846_dcdc_get_mode(struct regulator_dev *dev)
399 {
400         struct act8846 *act8846 = rdev_get_drvdata(dev);
401         int buck = rdev_get_id(dev) - ACT8846_DCDC1;
402         u16 mask = 0x08;
403         u16 val;
404         val = act8846_reg_read(act8846, act8846_BUCK_CONTR_REG(buck));
405         if (val < 0) {
406                 return val;
407         }
408         val=val & mask;
409         if (val== mask)
410                 return REGULATOR_MODE_NORMAL;
411         else
412                 return REGULATOR_MODE_STANDBY;
413
414 }
415 static int act8846_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
416 {
417         struct act8846 *act8846 = rdev_get_drvdata(dev);
418         int buck = rdev_get_id(dev) - ACT8846_DCDC1;
419         u16 mask = 0x80;
420         switch(mode)
421         {
422         case REGULATOR_MODE_STANDBY:
423                 return act8846_set_bits(act8846, act8846_BUCK_CONTR_REG(buck), mask, 0);
424         case REGULATOR_MODE_NORMAL:
425                 return act8846_set_bits(act8846, act8846_BUCK_CONTR_REG(buck), mask, mask);
426         default:
427                 printk("error:pmu_act8846 only powersave and pwm mode\n");
428                 return -EINVAL;
429         }
430
431
432 }
433 static int act8846_dcdc_set_voltage_time_sel(struct regulator_dev *dev,   unsigned int old_selector,
434                                      unsigned int new_selector)
435 {
436         struct act8846 *act8846 = rdev_get_drvdata(dev);
437         int ret =0,old_volt, new_volt;
438         
439         old_volt = act8846_dcdc_list_voltage(dev, old_selector);
440         if (old_volt < 0)
441                 return old_volt;
442         
443         new_volt = act8846_dcdc_list_voltage(dev, new_selector);
444         if (new_volt < 0)
445                 return new_volt;
446
447         return DIV_ROUND_UP(abs(old_volt - new_volt)*2, 25000);
448 }
449
450 static struct regulator_ops act8846_dcdc_ops = { 
451         .set_voltage = act8846_dcdc_set_voltage,
452         .get_voltage = act8846_dcdc_get_voltage,
453         .list_voltage= act8846_dcdc_list_voltage,
454         .is_enabled = act8846_dcdc_is_enabled,
455         .enable = act8846_dcdc_enable,
456         .disable = act8846_dcdc_disable,
457         .get_mode = act8846_dcdc_get_mode,
458         .set_mode = act8846_dcdc_set_mode,
459         .set_suspend_voltage = act8846_dcdc_set_sleep_voltage,
460         .set_voltage_time_sel = act8846_dcdc_set_voltage_time_sel,
461 };
462 static struct regulator_desc regulators[] = {
463
464         {
465                 .name = "DCDC1",
466                 .id = 0,
467                 .ops = &act8846_dcdc_ops,
468                 .n_voltages = ARRAY_SIZE(buck_voltage_map),
469                 .type = REGULATOR_VOLTAGE,
470                 .owner = THIS_MODULE,
471         },
472         {
473                 .name = "DCDC2",
474                 .id = 1,
475                 .ops = &act8846_dcdc_ops,
476                 .n_voltages = ARRAY_SIZE(buck_voltage_map),
477                 .type = REGULATOR_VOLTAGE,
478                 .owner = THIS_MODULE,
479         },
480         {
481                 .name = "DCDC3",
482                 .id = 2,
483                 .ops = &act8846_dcdc_ops,
484                 .n_voltages = ARRAY_SIZE(buck_voltage_map),
485                 .type = REGULATOR_VOLTAGE,
486                 .owner = THIS_MODULE,
487         },
488         {
489                 .name = "DCDC4",
490                 .id = 3,
491                 .ops = &act8846_dcdc_ops,
492                 .n_voltages = ARRAY_SIZE(buck_voltage_map),
493                 .type = REGULATOR_VOLTAGE,
494                 .owner = THIS_MODULE,
495         },
496
497         {
498                 .name = "LDO1",
499                 .id =4,
500                 .ops = &act8846_ldo_ops,
501                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
502                 .type = REGULATOR_VOLTAGE,
503                 .owner = THIS_MODULE,
504         },
505         {
506                 .name = "LDO2",
507                 .id = 5,
508                 .ops = &act8846_ldo_ops,
509                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
510                 .type = REGULATOR_VOLTAGE,
511                 .owner = THIS_MODULE,
512         },
513         {
514                 .name = "LDO3",
515                 .id = 6,
516                 .ops = &act8846_ldo_ops,
517                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
518                 .type = REGULATOR_VOLTAGE,
519                 .owner = THIS_MODULE,
520         },
521         {
522                 .name = "LDO4",
523                 .id = 7,
524                 .ops = &act8846_ldo_ops,
525                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
526                 .type = REGULATOR_VOLTAGE,
527                 .owner = THIS_MODULE,
528         },
529
530         {
531                 .name = "LDO5",
532                 .id =8,
533                 .ops = &act8846_ldo_ops,
534                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
535                 .type = REGULATOR_VOLTAGE,
536                 .owner = THIS_MODULE,
537         },
538         {
539                 .name = "LDO6",
540                 .id = 9,
541                 .ops = &act8846_ldo_ops,
542                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
543                 .type = REGULATOR_VOLTAGE,
544                 .owner = THIS_MODULE,
545         },
546         {
547                 .name = "LDO7",
548                 .id = 10,
549                 .ops = &act8846_ldo_ops,
550                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
551                 .type = REGULATOR_VOLTAGE,
552                 .owner = THIS_MODULE,
553         },
554         {
555                 .name = "LDO8",
556                 .id = 11,
557                 .ops = &act8846_ldo_ops,
558                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
559                 .type = REGULATOR_VOLTAGE,
560                 .owner = THIS_MODULE,
561         },
562         {
563                 .name = "LDO9",
564                 .id = 12,
565                 .ops = &act8846_ldo_ops,
566                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
567                 .type = REGULATOR_VOLTAGE,
568                 .owner = THIS_MODULE,
569         },
570 };
571
572 /*
573  *
574  */
575 static int act8846_i2c_read(struct i2c_client *i2c, char reg, int count,        u16 *dest)
576 {
577       int ret;
578     struct i2c_adapter *adap;
579     struct i2c_msg msgs[2];
580
581     if(!i2c)
582                 return ret;
583
584         if (count != 1)
585                 return -EIO;  
586   
587     adap = i2c->adapter;                
588     
589     msgs[0].addr = i2c->addr;
590     msgs[0].buf = &reg;
591     msgs[0].flags = i2c->flags;
592     msgs[0].len = 1;
593     msgs[0].scl_rate = 200*1000;
594     
595     msgs[1].buf = (u8 *)dest;
596     msgs[1].addr = i2c->addr;
597     msgs[1].flags = i2c->flags | I2C_M_RD;
598     msgs[1].len = 1;
599     msgs[1].scl_rate = 200*1000;
600     ret = i2c_transfer(adap, msgs, 2);
601
602         DBG("***run in %s %d msgs[1].buf = %d\n",__FUNCTION__,__LINE__,*(msgs[1].buf));
603
604         return 0;   
605 }
606
607 static int act8846_i2c_write(struct i2c_client *i2c, char reg, int count, const u16 src)
608 {
609         int ret=-1;
610         
611         struct i2c_adapter *adap;
612         struct i2c_msg msg;
613         char tx_buf[2];
614
615         if(!i2c)
616                 return ret;
617         if (count != 1)
618                 return -EIO;
619     
620         adap = i2c->adapter;            
621         tx_buf[0] = reg;
622         tx_buf[1] = src;
623         
624         msg.addr = i2c->addr;
625         msg.buf = &tx_buf[0];
626         msg.len = 1 +1;
627         msg.flags = i2c->flags;   
628         msg.scl_rate = 200*1000;        
629
630         ret = i2c_transfer(adap, &msg, 1);
631         return ret;     
632 }
633
634 static u8 act8846_reg_read(struct act8846 *act8846, u8 reg)
635 {
636         u16 val = 0;
637
638         mutex_lock(&act8846->io_lock);
639
640         act8846_i2c_read(act8846->i2c, reg, 1, &val);
641
642         DBG("reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
643
644         mutex_unlock(&act8846->io_lock);
645
646         return val & 0xff;      
647 }
648
649 static int act8846_set_bits(struct act8846 *act8846, u8 reg, u16 mask, u16 val)
650 {
651         u16 tmp;
652         int ret;
653
654         mutex_lock(&act8846->io_lock);
655
656         ret = act8846_i2c_read(act8846->i2c, reg, 1, &tmp);
657         DBG("1 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
658         tmp = (tmp & ~mask) | val;
659         if (ret == 0) {
660                 ret = act8846_i2c_write(act8846->i2c, reg, 1, tmp);
661                 DBG("reg write 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
662         }
663         act8846_i2c_read(act8846->i2c, reg, 1, &tmp);
664         DBG("2 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
665         mutex_unlock(&act8846->io_lock);
666
667         return 0;//ret; 
668 }
669 static int __devinit setup_regulators(struct act8846 *act8846, struct act8846_platform_data *pdata)
670 {       
671         int i, err;
672
673         act8846->num_regulators = pdata->num_regulators;
674         act8846->rdev = kcalloc(pdata->num_regulators,
675                                sizeof(struct regulator_dev *), GFP_KERNEL);
676         if (!act8846->rdev) {
677                 return -ENOMEM;
678         }
679         /* Instantiate the regulators */
680         for (i = 0; i < pdata->num_regulators; i++) {
681                 int id = pdata->regulators[i].id;
682                 act8846->rdev[i] = regulator_register(&regulators[id],
683                         act8846->dev, pdata->regulators[i].initdata, act8846);
684 /*
685                 if (IS_ERR(act8846->rdev[i])) {
686                         err = PTR_ERR(act8846->rdev[i]);
687                         dev_err(act8846->dev, "regulator init failed: %d\n",
688                                 err);
689                         goto error;
690                 }*/
691         }
692
693         return 0;
694 error:
695         while (--i >= 0)
696                 regulator_unregister(act8846->rdev[i]);
697         kfree(act8846->rdev);
698         act8846->rdev = NULL;
699         return err;
700 }
701
702
703 int act8846_device_shutdown(void)
704 {
705         int ret;
706         int err = -1;
707         struct act8846 *act8846 = g_act8846;
708         
709         printk("%s\n",__func__);
710
711         ret = act8846_reg_read(act8846,0xc3);
712         ret = act8846_set_bits(act8846, 0xc3,(0x1<<3),(0x1<<3));
713         ret = act8846_set_bits(act8846, 0xc3,(0x1<<4),(0x1<<4));
714         if (ret < 0) {
715                 printk("act8846 set 0xc3 error!\n");
716                 return err;
717         }
718         return 0;       
719 }
720 EXPORT_SYMBOL_GPL(act8846_device_shutdown);
721
722 __weak void  act8846_device_suspend(void) {}
723 __weak void  act8846_device_resume(void) {}
724 #ifdef CONFIG_PM
725 static int act8846_suspend(struct i2c_client *i2c, pm_message_t mesg)
726 {               
727         act8846_device_suspend();
728         return 0;
729 }
730
731 static int act8846_resume(struct i2c_client *i2c)
732 {
733         act8846_device_resume();
734         return 0;
735 }
736 #else
737 static int act8846_suspend(struct i2c_client *i2c, pm_message_t mesg)
738 {               
739         return 0;
740 }
741
742 static int act8846_resume(struct i2c_client *i2c)
743 {
744         return 0;
745 }
746 #endif
747
748
749 #ifdef CONFIG_HAS_EARLYSUSPEND
750 __weak void act8846_early_suspend(struct early_suspend *h) {}
751 __weak void act8846_late_resume(struct early_suspend *h) {}
752 #endif
753
754 static int __devinit act8846_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
755 {
756         struct act8846 *act8846;        
757         struct act8846_platform_data *pdata = i2c->dev.platform_data;
758         int ret;
759         act8846 = kzalloc(sizeof(struct act8846), GFP_KERNEL);
760         if (act8846 == NULL) {
761                 ret = -ENOMEM;          
762                 goto err;
763         }
764         act8846->i2c = i2c;
765         act8846->dev = &i2c->dev;
766         i2c_set_clientdata(i2c, act8846);
767         mutex_init(&act8846->io_lock);  
768
769         ret = act8846_reg_read(act8846,0x22);
770         if ((ret < 0) || (ret == 0xff)){
771                 printk("The device is not act8846 \n");
772                 return 0;
773         }
774
775         ret = act8846_set_bits(act8846, 0xf4,(0x1<<7),(0x0<<7));
776         if (ret < 0) {
777                 printk("act8846 set 0xf4 error!\n");
778                 goto err;
779         }
780         
781         if (pdata) {
782                 ret = setup_regulators(act8846, pdata);
783                 if (ret < 0)            
784                         goto err;
785         } else
786                 dev_warn(act8846->dev, "No platform init data supplied\n");
787
788         g_act8846 = act8846;
789         pdata->set_init(act8846);
790
791         #ifdef CONFIG_HAS_EARLYSUSPEND
792         act8846->act8846_suspend.suspend = act8846_early_suspend,
793         act8846->act8846_suspend.resume = act8846_late_resume,
794         act8846->act8846_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 1,
795         register_early_suspend(&act8846->act8846_suspend);
796         #endif
797         
798         return 0;
799
800 err:
801         return ret;     
802
803 }
804
805 static int __devexit act8846_i2c_remove(struct i2c_client *i2c)
806 {
807         struct act8846 *act8846 = i2c_get_clientdata(i2c);
808         int i;
809
810         for (i = 0; i < act8846->num_regulators; i++)
811                 if (act8846->rdev[i])
812                         regulator_unregister(act8846->rdev[i]);
813         kfree(act8846->rdev);
814         i2c_set_clientdata(i2c, NULL);
815         kfree(act8846);
816
817         return 0;
818 }
819
820 static const struct i2c_device_id act8846_i2c_id[] = {
821        { "act8846", 0 },
822        { }
823 };
824
825 MODULE_DEVICE_TABLE(i2c, act8846_i2c_id);
826
827 static struct i2c_driver act8846_i2c_driver = {
828         .driver = {
829                 .name = "act8846",
830                 .owner = THIS_MODULE,
831         },
832         .probe    = act8846_i2c_probe,
833         .remove   = __devexit_p(act8846_i2c_remove),
834         .id_table = act8846_i2c_id,
835         #ifdef CONFIG_PM
836         .suspend        = act8846_suspend,
837         .resume         = act8846_resume,
838         #endif
839 };
840
841 static int __init act8846_module_init(void)
842 {
843         int ret;
844         ret = i2c_add_driver(&act8846_i2c_driver);
845         if (ret != 0)
846                 pr_err("Failed to register I2C driver: %d\n", ret);
847         return ret;
848 }
849 //module_init(act8846_module_init);
850 //subsys_initcall(act8846_module_init);
851 //rootfs_initcall(act8846_module_init);
852 subsys_initcall_sync(act8846_module_init);
853
854 static void __exit act8846_module_exit(void)
855 {
856         i2c_del_driver(&act8846_i2c_driver);
857 }
858 module_exit(act8846_module_exit);
859
860 MODULE_LICENSE("GPL");
861 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
862 MODULE_DESCRIPTION("act8846 PMIC driver");
863