regulator: xz3216: Add a sentinel to xz3216_i2c_id[]
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / xz3216.c
1 /*
2  * Regulator driver for xz3216 DCDC chip for rk32xx
3  *
4  * Copyright (C) 2010, 2011 ROCKCHIP, Inc.
5
6  * Based on xz3216.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...)       pr_info(x)
37 #else
38 #define DBG(x...)
39 #endif
40
41 #define DBG_ERR(x...)   pr_err(x)
42
43 #define XZ3216_NUM_REGULATORS 1
44
45 #define XZ3216_BUCK1_SET_VOL_BASE 0x00
46 #define XZ3216_BUCK1_SLP_VOL_BASE 0x01
47 #define XZ3216_CONTR_REG1 0x02
48 #define XZ3216_ID1_REG 0x03
49 #define BUCK_VOL_MASK 0x3f
50 #define VOL_MIN_IDX 0x00
51 #define VOL_MAX_IDX 0x3
52
53 /* VSEL bit definitions */
54 #define VSEL_BUCK_EN    BIT(7)
55 #define VSEL_MODE       BIT(6)
56 #define VSEL_NSEL_MASK  0x3F
57
58 /* Control bit definitions */
59 #define CTL_OUTPUT_DISCHG       BIT(7)
60 #define CTL_SLEW_MASK           (0x7 << 4)
61 #define CTL_SLEW_SHIFT          4
62 #define CTL_RESET               BIT(2)
63
64 struct xz3216 {
65         struct device *dev;
66         struct i2c_client *i2c;
67         int num_regulators;
68         struct regulator_dev *rdev;
69         struct regulator_init_data *regulator;
70         struct regmap *regmap;
71         /* Voltage setting register */
72         unsigned int vol_reg;
73         unsigned int sleep_reg;
74         /* Voltage range and step(linear) */
75         unsigned int vsel_min;
76         unsigned int vsel_step;
77         unsigned int sleep_vol_cache;
78 };
79
80 struct xz3216_regulator {
81         struct device           *dev;
82         struct regulator_desc   *desc;
83         struct regulator_dev    *rdev;
84 };
85
86 struct xz3216_board {
87         struct regulator_init_data *xz3216_init_data;
88         struct device_node *of_node;
89 };
90
91 static unsigned int xz3216_dcdc_get_mode(struct regulator_dev *dev)
92 {
93         struct xz3216 *xz3216 = rdev_get_drvdata(dev);
94         unsigned int val;
95         int ret = 0;
96
97         ret = regmap_read(xz3216->regmap, xz3216->vol_reg, &val);
98         if (ret < 0)
99                 return ret;
100         if (val & VSEL_MODE)
101                 return REGULATOR_MODE_FAST;
102         else
103                 return REGULATOR_MODE_NORMAL;
104 }
105
106 static int xz3216_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
107 {
108         struct xz3216 *xz3216 = rdev_get_drvdata(dev);
109
110         switch (mode) {
111         case REGULATOR_MODE_FAST:
112                 regmap_update_bits(xz3216->regmap, xz3216->vol_reg,
113                                 VSEL_MODE, VSEL_MODE);
114         case REGULATOR_MODE_NORMAL:
115                 regmap_update_bits(xz3216->regmap, xz3216->vol_reg,
116                                                         VSEL_MODE, 0);
117         default:
118                 DBG("error:dcdc_xz3216 only auto and pwm mode\n");
119                 return -EINVAL;
120         }
121 }
122
123 static int xz3216_dcdc_suspend_enable(struct regulator_dev *dev)
124 {
125         struct xz3216 *xz3216 = rdev_get_drvdata(dev);
126         return regmap_update_bits(xz3216->regmap, XZ3216_BUCK1_SLP_VOL_BASE,
127                                   VSEL_BUCK_EN, VSEL_BUCK_EN);
128 }
129
130 static int xz3216_dcdc_suspend_disable(struct regulator_dev *dev)
131 {
132         struct xz3216 *xz3216 = rdev_get_drvdata(dev);
133         return regmap_update_bits(xz3216->regmap, XZ3216_BUCK1_SLP_VOL_BASE,
134                                   VSEL_BUCK_EN, 0);
135
136 }
137
138 static int xz3216_dcdc_set_sleep_voltage(struct regulator_dev *dev,
139                                          int uV)
140 {
141         struct xz3216 *xz3216 = rdev_get_drvdata(dev);
142         int ret;
143
144         if (xz3216->sleep_vol_cache == uV)
145                 return 0;
146         ret = regulator_map_voltage_linear(dev, uV, uV);
147         if (ret < 0)
148                 return ret;
149         ret = regmap_update_bits(xz3216->regmap, XZ3216_BUCK1_SLP_VOL_BASE,
150                                         VSEL_NSEL_MASK, ret);
151         if (ret < 0)
152                 return ret;
153         xz3216->sleep_vol_cache = uV;
154         return 0;
155
156 }
157
158 static int xz3216_dcdc_set_suspend_mode(struct regulator_dev *dev,
159                                          unsigned int mode)
160 {
161         struct xz3216 *xz3216 = rdev_get_drvdata(dev);
162
163         switch (mode) {
164         case REGULATOR_MODE_FAST:
165                 regmap_update_bits(xz3216->regmap, xz3216->vol_reg,
166                                                         VSEL_MODE, VSEL_MODE);
167         case REGULATOR_MODE_NORMAL:
168                 regmap_update_bits(xz3216->regmap, xz3216->vol_reg,
169                                                     VSEL_MODE, 0);
170         default:
171                 DBG_ERR("error:dcdc_xz3216 only auto and pwm mode\n");
172                 return -EINVAL;
173         }
174 }
175
176 static const int slew_rates[] = {
177         64000,
178         32000,
179         16000,
180          8000,
181          4000,
182          2000,
183          1000,
184           500,
185 };
186
187 static int xz3216_set_ramp(struct regulator_dev *rdev, int ramp)
188 {
189         struct xz3216 *xz3216 = rdev_get_drvdata(rdev);
190         int regval = -1, i;
191
192         for (i = 0; i < ARRAY_SIZE(slew_rates); i++) {
193                 if (ramp <= slew_rates[i])
194                         regval = i;
195                 else
196                         break;
197         }
198         if (regval < 0) {
199                 dev_err(xz3216->dev, "unsupported ramp value %d\n", ramp);
200                 return -EINVAL;
201         }
202
203         return regmap_update_bits(xz3216->regmap, XZ3216_CONTR_REG1,
204                                   CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT);
205 }
206
207 static struct regulator_ops xz3216_dcdc_ops = {
208         .set_voltage_sel = regulator_set_voltage_sel_regmap,
209         .get_voltage_sel = regulator_get_voltage_sel_regmap,
210         .list_voltage = regulator_list_voltage_linear,
211         .map_voltage = regulator_map_voltage_linear,
212         .is_enabled = regulator_is_enabled_regmap,
213         .enable = regulator_enable_regmap,
214         .disable = regulator_disable_regmap,
215         .get_mode = xz3216_dcdc_get_mode,
216         .set_mode = xz3216_dcdc_set_mode,
217         .set_suspend_voltage = xz3216_dcdc_set_sleep_voltage,
218         .set_suspend_enable = xz3216_dcdc_suspend_enable,
219         .set_suspend_disable = xz3216_dcdc_suspend_disable,
220         .set_suspend_mode = xz3216_dcdc_set_suspend_mode,
221         .set_ramp_delay = xz3216_set_ramp,
222         .set_voltage_time_sel = regulator_set_voltage_time_sel,
223 };
224
225 static struct regulator_desc regulators[] = {
226         {
227                 .name = "XZ_DCDC1",
228                 .supply_name = "vin",
229                 .id = 0,
230                 .ops = &xz3216_dcdc_ops,
231                 .n_voltages = 64,
232                 .type = REGULATOR_VOLTAGE,
233                 .enable_time = 400,
234                 .enable_reg = XZ3216_BUCK1_SET_VOL_BASE,
235                 .enable_mask = VSEL_BUCK_EN,
236                 .min_uV = 600000,
237                 .uV_step = 12500,
238                 .vsel_reg = XZ3216_BUCK1_SET_VOL_BASE,
239                 .vsel_mask = VSEL_NSEL_MASK,
240                 .owner = THIS_MODULE,
241         },
242 };
243
244 static const struct regmap_config xz3216_regmap_config = {
245         .reg_bits = 8,
246         .val_bits = 8,
247 };
248
249 #ifdef CONFIG_OF
250 static struct of_device_id xz3216_of_match[] = {
251         { .compatible = "xz3216"},
252         { },
253 };
254 MODULE_DEVICE_TABLE(of, xz3216_of_match);
255 #endif
256
257 #ifdef CONFIG_OF
258 static struct of_regulator_match xz3216_reg_matches[] = {
259         { .name = "xz_dcdc1", .driver_data = (void *)0},
260 };
261
262 static struct xz3216_board *xz3216_parse_dt(struct xz3216 *xz3216)
263 {
264         struct xz3216_board *pdata;
265         struct device_node *regs;
266         struct device_node *xz3216_np;
267         int count;
268
269         xz3216_np = of_node_get(xz3216->dev->of_node);
270         if (!xz3216_np) {
271                 DBG_ERR("could not find pmic sub-node\n");
272                 return NULL;
273         }
274         regs = of_find_node_by_name(xz3216_np, "regulators");
275         if (!regs)
276                 return NULL;
277         count = of_regulator_match(xz3216->dev, regs, xz3216_reg_matches,
278                                    XZ3216_NUM_REGULATORS);
279         of_node_put(regs);
280         pdata = devm_kzalloc(xz3216->dev, sizeof(*pdata), GFP_KERNEL);
281         if (!pdata)
282                 return NULL;
283         pdata->xz3216_init_data = xz3216_reg_matches[0].init_data;
284         pdata->of_node = xz3216_reg_matches[0].of_node;
285         return pdata;
286 }
287
288 #else
289 static struct xz3216_board *xz3216_parse_dt(struct i2c_client *i2c)
290 {
291         return NULL;
292 }
293 #endif
294
295 static int xz3216_i2c_probe(struct i2c_client *i2c,
296                             const struct i2c_device_id *id)
297 {
298         struct xz3216 *xz3216;
299         struct xz3216_board *pdev;
300         const struct of_device_id *match;
301         struct regulator_config config = { };
302         int ret;
303         DBG("%s, line=%d\n", __func__, __LINE__);
304         xz3216 = devm_kzalloc(&i2c->dev, sizeof(struct xz3216),
305                                                 GFP_KERNEL);
306         if (!xz3216) {
307                 ret = -ENOMEM;
308                 goto err;
309         }
310
311         if (i2c->dev.of_node) {
312                 match = of_match_device(xz3216_of_match, &i2c->dev);
313                 if (!match) {
314                         DBG_ERR("Failed to find matching dt id\n");
315                         return -EINVAL;
316                 }
317         }
318
319         xz3216->regmap = devm_regmap_init_i2c(i2c, &xz3216_regmap_config);
320         if (IS_ERR(xz3216->regmap)) {
321                 dev_err(&i2c->dev, "Failed to allocate regmap!\n");
322                 return PTR_ERR(xz3216->regmap);
323         }
324
325         xz3216->i2c = i2c;
326         xz3216->dev = &i2c->dev;
327         i2c_set_clientdata(i2c, xz3216);
328         pdev = dev_get_platdata(&i2c->dev);
329         if (!pdev)
330                 pdev = xz3216_parse_dt(xz3216);
331         if (pdev) {
332                 xz3216->num_regulators = XZ3216_NUM_REGULATORS;
333                 xz3216->rdev = kcalloc(XZ3216_NUM_REGULATORS,
334                                         sizeof(struct regulator_dev),
335                                         GFP_KERNEL);
336                 if (!xz3216->rdev)
337                         return -ENOMEM;
338                 /* Instantiate the regulators */
339                 xz3216->regulator = pdev->xz3216_init_data;
340                 if (xz3216->dev->of_node)
341                         config.of_node = pdev->of_node;
342                 config.dev = xz3216->dev;
343                 config.driver_data = xz3216;
344                 config.init_data = xz3216->regulator;
345                 xz3216->rdev = devm_regulator_register(xz3216->dev,
346                                                 &regulators[0], &config);
347                 ret = PTR_ERR_OR_ZERO(xz3216->rdev);
348                 if (ret < 0)
349                         dev_err(&i2c->dev, "Failed to register regulator!\n");
350                 return ret;
351         }
352         return 0;
353 err:
354         return ret;
355 }
356
357 static int xz3216_i2c_remove(struct i2c_client *i2c)
358 {
359         struct xz3216 *xz3216 = i2c_get_clientdata(i2c);
360
361         if (xz3216->rdev)
362                 regulator_unregister(xz3216->rdev);
363         i2c_set_clientdata(i2c, NULL);
364         return 0;
365 }
366
367 static const struct i2c_device_id xz3216_i2c_id[] = {
368         { "xz3216", 0 },
369         { }
370 };
371
372 MODULE_DEVICE_TABLE(i2c, xz3216_i2c_id);
373
374 static struct i2c_driver xz3216_i2c_driver = {
375         .driver = {
376                 .name = "xz3216",
377                 .owner = THIS_MODULE,
378                 .of_match_table = of_match_ptr(xz3216_of_match),
379         },
380         .probe = xz3216_i2c_probe,
381         .remove = xz3216_i2c_remove,
382         .id_table = xz3216_i2c_id,
383 };
384
385 static int __init xz3216_module_init(void)
386 {
387         int ret;
388
389         ret = i2c_add_driver(&xz3216_i2c_driver);
390         if (ret != 0)
391                 pr_err("Failed to register I2C driver: %d\n", ret);
392         return ret;
393 }
394 subsys_initcall_sync(xz3216_module_init);
395
396 static void __exit xz3216_module_exit(void)
397 {
398         i2c_del_driver(&xz3216_i2c_driver);
399 }
400 module_exit(xz3216_module_exit);
401
402 MODULE_LICENSE("GPL");
403 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
404 MODULE_DESCRIPTION("xz3216 PMIC driver");