ARM: configs: rockchip: enable es8323 support
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / act8931.c
1 /*
2  * Regulator driver for Active-semi act8931 PMIC chip
3  *
4  * Copyright (C) 2014 ROCKCHIP, Inc.
5  *
6  * Based on act8931.c in kernel 3.0 that is work by xhc<xhc@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/mutex.h>
15 #include <linux/bug.h>
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/kernel.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/of_gpio.h>
25 #include <linux/module.h>
26 #include <linux/regulator/of_regulator.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/regulator/driver.h>
29 #include <asm/system_misc.h>
30
31 #if 0
32 #define DBG(x...)       printk(KERN_INFO x)
33 #else
34 #define DBG(x...)
35 #endif
36
37 #define ACT8931_NUM_REGULATORS  7
38
39 struct act8931 *g_act8931;
40
41 struct act8931 {
42         unsigned int irq;
43         unsigned int pwr_hold_gpio;
44         struct device *dev;
45         struct mutex io_lock;
46         struct i2c_client *i2c;
47         int num_regulators;
48         struct regulator_dev **rdev;
49 };
50
51 struct act8931_board {
52         int irq_gpio;
53         int pwr_hold_gpio;
54         bool pm_off;
55         struct regulator_init_data *act8931_init_data[ACT8931_NUM_REGULATORS];
56         struct device_node *of_node[ACT8931_NUM_REGULATORS];
57 };
58
59 static u8 act8931_reg_read(struct act8931 *act8931, u8 reg);
60 static int act8931_set_bits(struct act8931 *act8931, u8 reg, u16 mask, u16 val);
61
62 #define ACT8931_DCDC1 0
63 #define ACT8931_DCDC2 1
64 #define ACT8931_DCDC3 2
65
66 #define ACT8931_LDO1  3
67 #define ACT8931_LDO2  4
68 #define ACT8931_LDO3  5
69 #define ACT8931_LDO4  6
70
71 #define act8931_BUCK1_SET_VOL_BASE 0x20
72 #define act8931_BUCK2_SET_VOL_BASE 0x30
73 #define act8931_BUCK3_SET_VOL_BASE 0x40
74 #define act8931_LDO1_SET_VOL_BASE 0x50
75 #define act8931_LDO2_SET_VOL_BASE 0x54
76 #define act8931_LDO3_SET_VOL_BASE 0x60
77 #define act8931_LDO4_SET_VOL_BASE 0x64
78
79 #define act8931_BUCK1_CONTR_BASE 0x22
80 #define act8931_BUCK2_CONTR_BASE 0x32
81 #define act8931_BUCK3_CONTR_BASE 0x42
82 #define act8931_LDO1_CONTR_BASE 0x51
83 #define act8931_LDO2_CONTR_BASE 0x55
84 #define act8931_LDO3_CONTR_BASE 0x61
85 #define act8931_LDO4_CONTR_BASE 0x65
86
87 #define BUCK_VOL_MASK 0x3f
88 #define LDO_VOL_MASK 0x3f
89
90 #define VOL_MIN_IDX 0x00
91 #define VOL_MAX_IDX 0x3f
92
93 #define INSTAT_MASK (1<<5)
94 #define CHGSTAT_MASK (1<<4)
95 #define INDAT_MASK (1<<1)
96 #define CHGDAT_MASK (1<<0)
97
98 #define INCON_MASK (1<<5)
99 #define CHGEOCIN_MASK (1<<4)
100 #define INDIS_MASK (1<<1)
101 #define CHGEOCOUT_MASK (1<<0)
102
103 int act8931_charge_det;
104 EXPORT_SYMBOL(act8931_charge_det);
105
106 int act8931_charge_ok;
107 EXPORT_SYMBOL(act8931_charge_ok);
108
109 static const int buck_set_vol_base_addr[] = {
110         act8931_BUCK1_SET_VOL_BASE,
111         act8931_BUCK2_SET_VOL_BASE,
112         act8931_BUCK3_SET_VOL_BASE,
113 };
114 static const int buck_contr_base_addr[] = {
115         act8931_BUCK1_CONTR_BASE,
116         act8931_BUCK2_CONTR_BASE,
117         act8931_BUCK3_CONTR_BASE,
118 };
119 #define act8931_BUCK_SET_VOL_REG(x) (buck_set_vol_base_addr[x])
120 #define act8931_BUCK_CONTR_REG(x) (buck_contr_base_addr[x])
121
122 static const int ldo_set_vol_base_addr[] = {
123         act8931_LDO1_SET_VOL_BASE,
124         act8931_LDO2_SET_VOL_BASE,
125         act8931_LDO3_SET_VOL_BASE,
126         act8931_LDO4_SET_VOL_BASE,
127 };
128 static const int ldo_contr_base_addr[] = {
129         act8931_LDO1_CONTR_BASE,
130         act8931_LDO2_CONTR_BASE,
131         act8931_LDO3_CONTR_BASE,
132         act8931_LDO4_CONTR_BASE,
133 };
134 #define act8931_LDO_SET_VOL_REG(x) (ldo_set_vol_base_addr[x])
135 #define act8931_LDO_CONTR_REG(x) (ldo_contr_base_addr[x])
136
137 static const 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 static const 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 act8931_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
168 static int act8931_ldo_is_enabled(struct regulator_dev *dev)
169 {
170         struct act8931 *act8931 = rdev_get_drvdata(dev);
171         int ldo = rdev_get_id(dev) - ACT8931_LDO1;
172         u16 val;
173         u16 mask = 0x80;
174
175         val = act8931_reg_read(act8931, act8931_LDO_CONTR_REG(ldo));
176         if (val < 0)
177                 return val;
178         val = val & ~0x7f;
179         if (val & mask)
180                 return 1;
181         else
182                 return 0;
183 }
184
185 static int act8931_ldo_enable(struct regulator_dev *dev)
186 {
187         struct act8931 *act8931 = rdev_get_drvdata(dev);
188         int ldo = rdev_get_id(dev) - ACT8931_LDO1;
189         u16 mask = 0x80;
190
191         return act8931_set_bits(act8931, act8931_LDO_CONTR_REG(ldo), mask,
192                                 0x80);
193 }
194
195 static int act8931_ldo_disable(struct regulator_dev *dev)
196 {
197         struct act8931 *act8931 = rdev_get_drvdata(dev);
198         int ldo = rdev_get_id(dev) - ACT8931_LDO1;
199         u16 mask = 0x80;
200
201         return act8931_set_bits(act8931, act8931_LDO_CONTR_REG(ldo), mask, 0);
202 }
203
204 static int act8931_ldo_get_voltage(struct regulator_dev *dev)
205 {
206         struct act8931 *act8931 = rdev_get_drvdata(dev);
207         int ldo = rdev_get_id(dev) - ACT8931_LDO1;
208         u16 reg = 0;
209         int val;
210
211         reg = act8931_reg_read(act8931, act8931_LDO_SET_VOL_REG(ldo));
212         reg &= LDO_VOL_MASK;
213         val = 1000 * ldo_voltage_map[reg];
214
215         return val;
216 }
217
218 static int act8931_ldo_set_voltage(struct regulator_dev *dev,
219                                    int min_uV, int max_uV, unsigned *selector)
220 {
221         struct act8931 *act8931 = rdev_get_drvdata(dev);
222         int ldo = rdev_get_id(dev) - ACT8931_LDO1;
223         int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
224         const int *vol_map = ldo_voltage_map;
225         u16 val;
226         int ret = 0;
227
228         if (min_vol < vol_map[VOL_MIN_IDX] || min_vol > vol_map[VOL_MAX_IDX])
229                 return -EINVAL;
230
231         for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++) {
232                 if (vol_map[val] >= min_vol)
233                         break;
234         }
235
236         if (vol_map[val] > max_vol)
237                 return -EINVAL;
238
239         ret = act8931_set_bits(act8931, act8931_LDO_SET_VOL_REG(ldo),
240                                LDO_VOL_MASK, val);
241
242         return ret;
243 }
244
245 static unsigned int act8931_ldo_get_mode(struct regulator_dev *dev)
246 {
247         struct act8931 *act8931 = rdev_get_drvdata(dev);
248         int ldo = rdev_get_id(dev) - ACT8931_LDO1;
249         u16 mask = 0xcf;
250         u16 val;
251
252         val = act8931_reg_read(act8931, act8931_LDO_CONTR_REG(ldo));
253         val = val | mask;
254
255         if (val == mask)
256                 return REGULATOR_MODE_NORMAL;
257         else
258                 return REGULATOR_MODE_STANDBY;
259 }
260
261 static int act8931_ldo_set_mode(struct regulator_dev *dev, unsigned int mode)
262 {
263         struct act8931 *act8931 = rdev_get_drvdata(dev);
264         int ldo = rdev_get_id(dev) - ACT8931_LDO1;
265         u16 mask = 0x20;
266
267         switch (mode) {
268         case REGULATOR_MODE_NORMAL:
269                 return act8931_set_bits(act8931, act8931_LDO_CONTR_REG(ldo),
270                                         mask, 0);
271         case REGULATOR_MODE_STANDBY:
272                 return act8931_set_bits(act8931, act8931_LDO_CONTR_REG(ldo),
273                                         mask, mask);
274         default:
275                 pr_err("pmu_act8931 only lowpower and normal mode\n");
276                 return -EINVAL;
277         }
278 }
279
280 static struct regulator_ops act8931_ldo_ops = {
281         .set_voltage = act8931_ldo_set_voltage,
282         .get_voltage = act8931_ldo_get_voltage,
283         .list_voltage = act8931_ldo_list_voltage,
284         .is_enabled = act8931_ldo_is_enabled,
285         .enable = act8931_ldo_enable,
286         .disable = act8931_ldo_disable,
287         .get_mode = act8931_ldo_get_mode,
288         .set_mode = act8931_ldo_set_mode,
289 };
290
291 static int act8931_dcdc_list_voltage(struct regulator_dev *dev, unsigned index)
292 {
293         if (index >= ARRAY_SIZE(buck_voltage_map))
294                 return -EINVAL;
295
296         return 1000 * buck_voltage_map[index];
297 }
298
299 static int act8931_dcdc_is_enabled(struct regulator_dev *dev)
300 {
301         struct act8931 *act8931 = rdev_get_drvdata(dev);
302         int buck = rdev_get_id(dev) - ACT8931_DCDC1;
303         u16 val;
304         u16 mask = 0x80;
305
306         val = act8931_reg_read(act8931, act8931_BUCK_CONTR_REG(buck));
307         if (val < 0)
308                 return val;
309         val = val & ~0x7f;
310         if (val & mask)
311                 return 1;
312         else
313                 return 0;
314 }
315
316 static int act8931_dcdc_enable(struct regulator_dev *dev)
317 {
318         struct act8931 *act8931 = rdev_get_drvdata(dev);
319         int buck = rdev_get_id(dev) - ACT8931_DCDC1;
320         u16 mask = 0x80;
321
322         return act8931_set_bits(act8931, act8931_BUCK_CONTR_REG(buck), mask,
323                                 0x80);
324 }
325
326 static int act8931_dcdc_disable(struct regulator_dev *dev)
327 {
328         struct act8931 *act8931 = rdev_get_drvdata(dev);
329         int buck = rdev_get_id(dev) - ACT8931_DCDC1;
330         u16 mask = 0x80;
331
332         return act8931_set_bits(act8931, act8931_BUCK_CONTR_REG(buck), mask, 0);
333 }
334
335 static int act8931_dcdc_get_voltage(struct regulator_dev *dev)
336 {
337         struct act8931 *act8931 = rdev_get_drvdata(dev);
338         int buck = rdev_get_id(dev) - ACT8931_DCDC1;
339         u16 reg = 0;
340         int val;
341
342         reg = act8931_reg_read(act8931, act8931_BUCK_SET_VOL_REG(buck));
343         reg &= BUCK_VOL_MASK;
344         DBG("%d\n", reg);
345         val = 1000 * buck_voltage_map[reg];
346         DBG("%d\n", val);
347
348         return val;
349 }
350
351 static int act8931_dcdc_set_voltage(struct regulator_dev *dev,
352                                     int min_uV, int max_uV, unsigned *selector)
353 {
354         struct act8931 *act8931 = rdev_get_drvdata(dev);
355         int buck = rdev_get_id(dev) - ACT8931_DCDC1;
356         int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
357         const int *vol_map = buck_voltage_map;
358         u16 val;
359         int ret = 0;
360
361         DBG("%s, min_uV = %d, max_uV = %d!\n", __func__, min_uV, max_uV);
362         if (min_vol < vol_map[VOL_MIN_IDX] ||
363             min_vol > vol_map[VOL_MAX_IDX])
364                 return -EINVAL;
365
366         for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++) {
367                 if (vol_map[val] >= min_vol)
368                         break;
369         }
370
371         if (vol_map[val] > max_vol)
372                 pr_warn("this voltage is not support!voltage set is %d mv\n",
373                         vol_map[val]);
374
375         ret = act8931_set_bits(act8931, act8931_BUCK_SET_VOL_REG(buck),
376                                BUCK_VOL_MASK, val);
377         ret = act8931_set_bits(act8931, act8931_BUCK_SET_VOL_REG(buck) + 0x01,
378                                BUCK_VOL_MASK, val);
379
380         return ret;
381 }
382
383 static unsigned int act8931_dcdc_get_mode(struct regulator_dev *dev)
384 {
385         struct act8931 *act8931 = rdev_get_drvdata(dev);
386         int buck = rdev_get_id(dev) - ACT8931_DCDC1;
387         u16 mask = 0xcf;
388         u16 val;
389
390         val = act8931_reg_read(act8931, act8931_BUCK_CONTR_REG(buck));
391         val = val | mask;
392         if (val == mask)
393                 return REGULATOR_MODE_STANDBY;
394         else
395                 return REGULATOR_MODE_NORMAL;
396 }
397
398 static int act8931_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
399 {
400         struct act8931 *act8931 = rdev_get_drvdata(dev);
401         int buck = rdev_get_id(dev) - ACT8931_DCDC1;
402         u16 mask = 0x20;
403
404         switch (mode) {
405         case REGULATOR_MODE_STANDBY:
406                 return act8931_set_bits(act8931, act8931_BUCK_CONTR_REG(buck),
407                                         mask, 0);
408         case REGULATOR_MODE_NORMAL:
409                 return act8931_set_bits(act8931, act8931_BUCK_CONTR_REG(buck),
410                                         mask, mask);
411         default:
412                 pr_err("pmu_act8931 only powersave and pwm mode\n");
413                 return -EINVAL;
414         }
415 }
416
417 static struct regulator_ops act8931_dcdc_ops = {
418         .set_voltage = act8931_dcdc_set_voltage,
419         .get_voltage = act8931_dcdc_get_voltage,
420         .list_voltage = act8931_dcdc_list_voltage,
421         .is_enabled = act8931_dcdc_is_enabled,
422         .enable = act8931_dcdc_enable,
423         .disable = act8931_dcdc_disable,
424         .get_mode = act8931_dcdc_get_mode,
425         .set_mode = act8931_dcdc_set_mode,
426 };
427
428 static struct regulator_desc regulators[] = {
429         {
430                 .name = "ACT_DCDC1",
431                 .id = 0,
432                 .ops = &act8931_dcdc_ops,
433                 .n_voltages = ARRAY_SIZE(buck_voltage_map),
434                 .type = REGULATOR_VOLTAGE,
435                 .owner = THIS_MODULE,
436         },
437         {
438                 .name = "ACT_DCDC2",
439                 .id = 1,
440                 .ops = &act8931_dcdc_ops,
441                 .n_voltages = ARRAY_SIZE(buck_voltage_map),
442                 .type = REGULATOR_VOLTAGE,
443                 .owner = THIS_MODULE,
444         },
445         {
446                 .name = "ACT_DCDC3",
447                 .id = 2,
448                 .ops = &act8931_dcdc_ops,
449                 .n_voltages = ARRAY_SIZE(buck_voltage_map),
450                 .type = REGULATOR_VOLTAGE,
451                 .owner = THIS_MODULE,
452         },
453         {
454                 .name = "ACT_LDO1",
455                 .id = 3,
456                 .ops = &act8931_ldo_ops,
457                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
458                 .type = REGULATOR_VOLTAGE,
459                 .owner = THIS_MODULE,
460         },
461         {
462                 .name = "ACT_LDO2",
463                 .id = 4,
464                 .ops = &act8931_ldo_ops,
465                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
466                 .type = REGULATOR_VOLTAGE,
467                 .owner = THIS_MODULE,
468         },
469         {
470                 .name = "ACT_LDO3",
471                 .id = 5,
472                 .ops = &act8931_ldo_ops,
473                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
474                 .type = REGULATOR_VOLTAGE,
475                 .owner = THIS_MODULE,
476         },
477         {
478                 .name = "ACT_LDO4",
479                 .id = 6,
480                 .ops = &act8931_ldo_ops,
481                 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
482                 .type = REGULATOR_VOLTAGE,
483                 .owner = THIS_MODULE,
484         },
485
486 };
487
488 static int act8931_i2c_read(struct i2c_client *i2c, char reg, int count,
489                             u16 *dest)
490 {
491         int ret;
492         struct i2c_adapter *adap;
493         struct i2c_msg msgs[2];
494
495         if (!i2c)
496                 return ret;
497
498         if (count != 1)
499                 return -EIO;
500
501         adap = i2c->adapter;
502
503         msgs[0].addr = i2c->addr;
504         msgs[0].buf = &reg;
505         msgs[0].flags = i2c->flags;
506         msgs[0].len = 1;
507         msgs[0].scl_rate = 200*1000;
508
509         msgs[1].buf = (u8 *)dest;
510         msgs[1].addr = i2c->addr;
511         msgs[1].flags = i2c->flags | I2C_M_RD;
512         msgs[1].len = 1;
513         msgs[1].scl_rate = 200*1000;
514         ret = i2c_transfer(adap, msgs, 2);
515
516         DBG("***run in %s %d msgs[1].buf = %d\n", __func__, __LINE__,
517             *(msgs[1].buf));
518
519         return 0;
520 }
521
522 static int act8931_i2c_write(struct i2c_client *i2c, char reg, int count,
523                              const u16 src)
524 {
525         struct i2c_adapter *adap;
526         struct i2c_msg msg;
527         char tx_buf[2];
528         int ret = -1;
529
530         if (!i2c)
531                 return ret;
532         if (count != 1)
533                 return -EIO;
534
535         adap = i2c->adapter;
536         tx_buf[0] = reg;
537         tx_buf[1] = src;
538
539         msg.addr = i2c->addr;
540         msg.buf = &tx_buf[0];
541         msg.len = 1 + 1;
542         msg.flags = i2c->flags;
543         msg.scl_rate = 200*1000;
544
545         ret = i2c_transfer(adap, &msg, 1);
546
547         return 0;
548 }
549
550 static u8 act8931_reg_read(struct act8931 *act8931, u8 reg)
551 {
552         u16 val = 0;
553
554         mutex_lock(&act8931->io_lock);
555
556         act8931_i2c_read(act8931->i2c, reg, 1, &val);
557
558         DBG("reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
559
560         mutex_unlock(&act8931->io_lock);
561
562         return val & 0xff;
563 }
564
565 static int act8931_set_bits(struct act8931 *act8931, u8 reg, u16 mask, u16 val)
566 {
567         u16 tmp;
568         int ret;
569
570         mutex_lock(&act8931->io_lock);
571
572         ret = act8931_i2c_read(act8931->i2c, reg, 1, &tmp);
573         DBG("1 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
574         tmp = (tmp & ~mask) | val;
575         if (ret == 0) {
576                 ret = act8931_i2c_write(act8931->i2c, reg, 1, tmp);
577                 DBG("reg write 0x%02x -> 0x%02x\n", (int)reg,
578                     (unsigned)val&0xff);
579         }
580         act8931_i2c_read(act8931->i2c, reg, 1, &tmp);
581         DBG("2 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
582         mutex_unlock(&act8931->io_lock);
583
584         return ret;
585 }
586
587 void act8931_device_shutdown(void)
588 {
589         int ret;
590         struct act8931 *act8931 = g_act8931;
591
592         pr_info("%s\n", __func__);
593
594         ret = act8931_reg_read(act8931, 0x01);
595         ret = act8931_set_bits(act8931, 0x01, (0x1<<5) | (0x3<<0),
596                                (0x1<<5) | (0x3<<0));
597         if (ret < 0)
598                 pr_err("act8931 set 0x01 error!\n");
599
600         mdelay(100);
601         arm_pm_restart('h', "charge");
602 }
603
604 #ifdef CONFIG_OF
605 static struct of_device_id act8931_of_match[] = {
606         { .compatible = "act,act8931"},
607         { },
608 };
609 MODULE_DEVICE_TABLE(of, act8931_of_match);
610 #endif
611
612 #ifdef CONFIG_OF
613 static struct of_regulator_match act8931_reg_matches[] = {
614         { .name = "act_dcdc1" },
615         { .name = "act_dcdc2" },
616         { .name = "act_dcdc3" },
617         { .name = "act_ldo1"  },
618         { .name = "act_ldo2"  },
619         { .name = "act_ldo3"  },
620         { .name = "act_ldo4"  },
621 };
622
623 static struct act8931_board *act8931_parse_dt(struct act8931 *act8931)
624 {
625         struct act8931_board *pdata;
626         struct device_node *regs;
627         struct device_node *node;
628         int i, count;
629
630         node = of_node_get(act8931->dev->of_node);
631         if (!node) {
632                 pr_err("%s: could not find pmic node\n", __func__);
633                 return NULL;
634         }
635
636         regs = of_get_child_by_name(node, "regulators");
637         if (!regs)
638                 return NULL;
639
640         count = of_regulator_match(act8931->dev, regs, act8931_reg_matches,
641                                    ACT8931_NUM_REGULATORS);
642         of_node_put(regs);
643
644         if ((count < 0) || (count > ACT8931_NUM_REGULATORS))
645                 return NULL;
646
647         pdata = devm_kzalloc(act8931->dev, sizeof(*pdata), GFP_KERNEL);
648         if (!pdata)
649                 return NULL;
650
651         for (i = 0; i < count; i++) {
652                 pdata->act8931_init_data[i] = act8931_reg_matches[i].init_data;
653                 pdata->of_node[i] = act8931_reg_matches[i].of_node;
654         }
655
656         pdata->irq_gpio = of_get_named_gpio(node, "gpios", 0);
657         if (!gpio_is_valid(pdata->irq_gpio)) {
658                 pr_err("%s: invalid gpio: %d\n", __func__, pdata->irq_gpio);
659                 return NULL;
660         }
661
662         pdata->pwr_hold_gpio = of_get_named_gpio(node, "gpios", 1);
663         if (!gpio_is_valid(pdata->pwr_hold_gpio)) {
664                 pr_err("%s: invalid gpio: %d\n", __func__,
665                        pdata->pwr_hold_gpio);
666                 return NULL;
667         }
668
669         pdata->pm_off = of_property_read_bool(node,
670                                         "act8931,system-power-controller");
671
672         return pdata;
673 }
674 #else
675 static struct act8931_board *act8931_parse_dt(struct act8931 *act8931)
676 {
677         return NULL;
678 }
679 #endif
680
681 static irqreturn_t act8931_irq_thread(int irq, void *dev_id)
682 {
683         struct act8931 *act8931 = (struct act8931 *)dev_id;
684         int ret;
685         u8 val;
686
687         val = act8931_reg_read(act8931, 0x78);
688         act8931_charge_det = (val & INDAT_MASK) ? 1 : 0;
689         act8931_charge_ok = (val & CHGDAT_MASK) ? 1 : 0;
690         DBG(charge_det ? "connect!" : "disconnect!");
691         DBG(charge_ok ? "charge ok!\n" : "charging or discharge!\n");
692
693         /* reset related regs according to spec */
694         ret = act8931_set_bits(act8931, 0x78, INSTAT_MASK | CHGSTAT_MASK,
695                                INSTAT_MASK | CHGSTAT_MASK);
696         if (ret < 0)
697                 pr_err("act8931 set 0x78 error!\n");
698
699         return IRQ_HANDLED;
700 }
701
702 static int act8931_i2c_probe(struct i2c_client *i2c,
703                              const struct i2c_device_id *id)
704 {
705         const struct of_device_id *match;
706         struct act8931 *act8931;
707         struct act8931_board *pdata;
708         struct regulator_init_data *reg_data;
709         struct regulator_config config = { };
710         const char *rail_name = NULL;
711         struct regulator_dev *rdev;
712         u8 val;
713         int i, ret;
714
715         pr_info("%s,line=%d\n", __func__, __LINE__);
716
717         if (i2c->dev.of_node) {
718                 match = of_match_device(act8931_of_match, &i2c->dev);
719                 if (!match) {
720                         pr_err("Failed to find matching dt id\n");
721                         return -EINVAL;
722                 }
723         }
724
725         act8931 = devm_kzalloc(&i2c->dev, sizeof(struct act8931), GFP_KERNEL);
726         if (act8931 == NULL) {
727                 ret = -ENOMEM;
728                 goto err;
729         }
730         act8931->i2c = i2c;
731         act8931->dev = &i2c->dev;
732         g_act8931 = act8931;
733
734         mutex_init(&act8931->io_lock);
735
736         ret = act8931_reg_read(act8931, 0x22);
737         if ((ret < 0) || (ret == 0xff)) {
738                 pr_err("The device is not act8931\n");
739                 return 0;
740         }
741
742         if (act8931->dev->of_node)
743                 pdata = act8931_parse_dt(act8931);
744
745         ret = act8931_reg_read(act8931, 0x01);
746         if (ret < 0)
747                 goto err;
748         ret = act8931_set_bits(act8931, 0x01, (0x1<<5) | (0x1<<0), (0x1<<0));
749         if (ret < 0) {
750                 pr_err("act8931 set 0x01 error!\n");
751                 goto err;
752         }
753
754         /* Initialize charge status */
755         val = act8931_reg_read(act8931, 0x78);
756         act8931_charge_det = (val & INDAT_MASK) ? 1 : 0;
757         act8931_charge_ok = (val & CHGDAT_MASK) ? 1 : 0;
758         DBG(charge_det ? "connect!" : "disconnect!");
759         DBG(charge_ok ? "charge ok!\n" : "charging or discharge!\n");
760
761         ret = act8931_set_bits(act8931, 0x78, INSTAT_MASK | CHGSTAT_MASK,
762                                INSTAT_MASK | CHGSTAT_MASK);
763         if (ret < 0) {
764                 pr_err("act8931 set 0x78 error!\n");
765                 goto err;
766         }
767
768         ret = act8931_set_bits(act8931, 0x79, INCON_MASK | CHGEOCIN_MASK
769                                | INDIS_MASK | CHGEOCOUT_MASK, INCON_MASK
770                                | CHGEOCIN_MASK | INDIS_MASK | CHGEOCOUT_MASK);
771         if (ret < 0) {
772                 pr_err("act8931 set 0x79 error!\n");
773                 goto err;
774         }
775
776         if (pdata) {
777                 act8931->num_regulators = ACT8931_NUM_REGULATORS;
778                 act8931->rdev = devm_kcalloc(act8931->dev,
779                                              ACT8931_NUM_REGULATORS,
780                                              sizeof(struct regulator_dev *),
781                                              GFP_KERNEL);
782                 if (!act8931->rdev)
783                         return -ENOMEM;
784
785                 /* Instantiate the regulators */
786                 for (i = 0; i < ACT8931_NUM_REGULATORS; i++) {
787                         reg_data = pdata->act8931_init_data[i];
788                         if (!reg_data)
789                                 continue;
790                         if (reg_data->constraints.name)
791                                 rail_name = reg_data->constraints.name;
792                         else
793                                 rail_name = regulators[i].name;
794                         reg_data->supply_regulator = rail_name;
795
796                         config.dev = act8931->dev;
797                         config.driver_data = act8931;
798                         if (act8931->dev->of_node)
799                                 config.of_node = pdata->of_node[i];
800                         config.init_data = reg_data;
801
802                         rdev = regulator_register(&regulators[i], &config);
803                         if (IS_ERR(rdev)) {
804                                 pr_err("failed to register %d regulator\n", i);
805                                 continue;
806                         }
807                         act8931->rdev[i] = rdev;
808                 }
809         }
810
811         if (pdata->pm_off && !pm_power_off)
812                 pm_power_off = act8931_device_shutdown;
813
814         act8931->pwr_hold_gpio = pdata->pwr_hold_gpio;
815         if (act8931->pwr_hold_gpio) {
816                 ret = gpio_request(act8931->pwr_hold_gpio, "act8931 pmic_hold");
817                 if (ret < 0) {
818                         pr_err("Failed to request gpio %d with ret %d\n",
819                                act8931->pwr_hold_gpio, ret);
820                         goto err;
821                 }
822                 gpio_direction_output(act8931->pwr_hold_gpio, 1);
823                 ret = gpio_get_value(act8931->pwr_hold_gpio);
824                 pr_info("%s: act8931_pmic_hold=%x\n", __func__, ret);
825         }
826
827         ret = gpio_request(pdata->irq_gpio, "act8931 irq");
828         if (ret) {
829                 pr_err("act8931 irq_gpio request fail\n");
830                 gpio_free(pdata->irq_gpio);
831                 goto err;
832         }
833         gpio_direction_input(pdata->irq_gpio);
834
835         act8931->irq = gpio_to_irq(pdata->irq_gpio);
836         ret = request_threaded_irq(act8931->irq, NULL, act8931_irq_thread,
837                                    IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
838                                    i2c->dev.driver->name, act8931);
839         if (ret < 0) {
840                 pr_err("request act8931 irq fail\n");
841                 goto err;
842         }
843         enable_irq_wake(act8931->irq);
844
845         i2c_set_clientdata(i2c, act8931);
846
847         return 0;
848
849 err:
850         return ret;
851 }
852
853 static int act8931_i2c_remove(struct i2c_client *i2c)
854 {
855         struct act8931 *act8931 = i2c_get_clientdata(i2c);
856         int i;
857
858         for (i = 0; i < act8931->num_regulators; i++)
859                 if (act8931->rdev[i])
860                         regulator_unregister(act8931->rdev[i]);
861         i2c_set_clientdata(i2c, NULL);
862
863         return 0;
864 }
865
866 static const struct i2c_device_id act8931_i2c_id[] = {
867         { "act8931", 0 },
868         { }
869 };
870 MODULE_DEVICE_TABLE(i2c, act8931_i2c_id);
871
872 static struct i2c_driver act8931_i2c_driver = {
873         .driver = {
874                 .name = "act8931",
875                 .owner = THIS_MODULE,
876                 .of_match_table = of_match_ptr(act8931_of_match),
877         },
878         .probe    = act8931_i2c_probe,
879         .remove   = act8931_i2c_remove,
880         .id_table = act8931_i2c_id,
881 };
882
883 static int __init act8931_module_init(void)
884 {
885         int ret;
886
887         ret = i2c_add_driver(&act8931_i2c_driver);
888         if (ret != 0)
889                 pr_err("Failed to register I2C driver: %d\n", ret);
890         return ret;
891 }
892 subsys_initcall_sync(act8931_module_init);
893
894 static void __exit act8931_module_exit(void)
895 {
896         i2c_del_driver(&act8931_i2c_driver);
897 }
898 module_exit(act8931_module_exit);
899
900 MODULE_LICENSE("GPL");
901 MODULE_AUTHOR("dkl <dkl@rock-chips.com>");
902 MODULE_DESCRIPTION("act8931 PMIC driver");