regulator: rockchip: lp8752: support lp8752 regulator
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / lp8752.c
1 /*
2  * LP8752 High Performance Power Management Unit : System Interface Driver
3  *
4  * Author: zhangqing <zhangqing@rock-chips.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/err.h>
16 #include <linux/irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/gpio.h>
19 #include <linux/regmap.h>
20 #include <linux/uaccess.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/regulator/machine.h>
24
25 #define LP8752_CTRL_BUCK0               0x02
26 #define LP8752_CTRL_BUCK1               0x04
27 #define LP8752_CTRL_BUCK2               0x06
28 #define LP8752_CTRL_BUCK3               0x08
29
30 #define LP8752_VOUT_BUCK0               0x0a
31 #define LP8752_VOUT_BUCK1               0x0c
32 #define LP8752_VOUT_BUCK2               0x0e
33 #define LP8752_VOUT_BUCK3               0x10
34
35 #define LP8752_BUCK_VSEL_MASK           0xff
36 #define LP8752_REG_MAX                  0x2f
37
38 enum lp8752_bucks {
39         LP8752_BUCK0 = 0,
40         LP8752_BUCK1,
41         LP8752_BUCK2,
42         LP8752_BUCK3,
43         LP8752_BUCK_MAX,
44 };
45
46 static const struct regulator_linear_range lp8752_buck_voltage_ranges[] = {
47         REGULATOR_LINEAR_RANGE(500000, 0, 23, 10000),
48         REGULATOR_LINEAR_RANGE(735000, 24, 157, 5000),
49         REGULATOR_LINEAR_RANGE(1420000, 158, 255, 20000),
50 };
51
52 struct lp8752_platform_data {
53         int nphase;
54         struct device_node *of_node[LP8752_BUCK_MAX];
55         struct regulator_desc desc;
56         struct regulator_init_data *buck_data[LP8752_BUCK_MAX];
57 };
58
59 struct lp8752_mphase {
60         int nreg;
61         int buck_id[LP8752_BUCK_MAX];
62 };
63
64 struct lp8752_chip {
65         int nphase;
66         struct device *dev;
67         struct regmap *regmap;
68         struct lp8752_platform_data *pdata;
69         struct regulator_dev *rdev[LP8752_BUCK_MAX];
70 };
71
72 static int lp8752_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
73 {
74         struct lp8752_chip *pchip = rdev_get_drvdata(rdev);
75         u8 msk = BIT(1);
76         int ret;
77
78         switch (mode) {
79         case REGULATOR_MODE_FAST:
80                 /* forced pwm mode */
81                 ret = regmap_update_bits(pchip->regmap,
82                                          rdev->desc->enable_reg,
83                                          msk, 0x1);
84         case REGULATOR_MODE_NORMAL:
85                 /* automatic pwm/pfm mode */
86                 ret = regmap_update_bits(pchip->regmap,
87                                          rdev->desc->enable_reg,
88                                          msk, 0x0);
89         default:
90                 break;
91         }
92
93         return ret;
94 }
95
96 static unsigned int lp8752_buck_get_mode(struct regulator_dev *rdev)
97 {
98         struct lp8752_chip *pchip = rdev_get_drvdata(rdev);
99         int ret;
100         unsigned int reg;
101
102         ret = regmap_read(pchip->regmap, rdev->desc->enable_reg, &reg);
103         if (ret < 0) {
104                 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
105                 return ret;
106         }
107
108         return (reg & BIT(1)) ?  REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
109 }
110
111 static struct regulator_ops lp8752_buck_ops = {
112         .map_voltage = regulator_map_voltage_linear_range,
113         .list_voltage = regulator_list_voltage_linear_range,
114         .set_voltage_sel = regulator_set_voltage_sel_regmap,
115         .get_voltage_sel = regulator_get_voltage_sel_regmap,
116         .enable = regulator_enable_regmap,
117         .disable = regulator_disable_regmap,
118         .is_enabled = regulator_is_enabled_regmap,
119         .set_mode = lp8752_buck_set_mode,
120         .get_mode = lp8752_buck_get_mode,
121 };
122
123 #define lp8752_rail(_id) "lp8752_buck"#_id
124 #define vin(_id) "vin"#_id
125
126 static const struct lp8752_mphase mphase_buck[] = {
127         { 1, { LP8752_BUCK0} },
128         { 2, { LP8752_BUCK0, LP8752_BUCK3 } },
129         { 3, { LP8752_BUCK0, LP8752_BUCK2, LP8752_BUCK3 } },
130         { 4, { LP8752_BUCK0, LP8752_BUCK1, LP8752_BUCK2, LP8752_BUCK3 } },
131 };
132
133 static struct of_regulator_match lp8752_reg_matches[] = {
134         { .name = "lp8752_buck0", .driver_data = (void *)0 },
135         { .name = "lp8752_buck1", .driver_data = (void *)1 },
136         { .name = "lp8752_buck2", .driver_data = (void *)2 },
137         { .name = "lp8752_buck3", .driver_data = (void *)3 },
138 };
139
140 static int lp8752_init_data(struct lp8752_chip *pchip)
141 {
142         int icnt, buck_id, count;
143         struct lp8752_platform_data *pdata = pchip->pdata;
144         struct device_node *regs, *lp8752_np;
145
146         lp8752_np = of_node_get(pchip->dev->of_node);
147         if (!lp8752_np) {
148                 dev_err(pchip->dev, "Failed to find device node\n");
149                 return -ENODEV;
150         }
151
152         regs = of_find_node_by_name(lp8752_np, "regulators");
153         if (!regs)
154                 return -ENODEV;
155
156         count = of_regulator_match(pchip->dev, regs, lp8752_reg_matches,
157                                    LP8752_BUCK_MAX);
158         of_node_put(regs);
159         if ((count <= 0) || (count > LP8752_BUCK_MAX))
160                 return -ENODEV;
161
162         pchip->nphase = (count - 1) & 0xf;
163
164         /* set default data based on multi-phase config */
165         for (icnt = 0; icnt < mphase_buck[pchip->nphase].nreg; icnt++) {
166                 buck_id = mphase_buck[pchip->nphase].buck_id[icnt];
167                 pdata->buck_data[buck_id] =
168                 lp8752_reg_matches[buck_id].init_data;
169                 pdata->of_node[buck_id] = lp8752_reg_matches[buck_id].of_node;
170         }
171
172         return 0;
173 }
174
175 #define lp8752_buck_desc(_id)\
176 {\
177         .name = lp8752_rail(_id),\
178         .id   = LP8752_BUCK##_id,\
179         .supply_name = vin(_id),\
180         .ops  = &lp8752_buck_ops,\
181         .n_voltages = LP8752_BUCK_VSEL_MASK + 1,\
182         .linear_ranges = lp8752_buck_voltage_ranges,\
183         .n_linear_ranges = ARRAY_SIZE(lp8752_buck_voltage_ranges),\
184         .type = REGULATOR_VOLTAGE,\
185         .enable_reg = LP8752_CTRL_BUCK##_id,\
186         .enable_mask = BIT(7),\
187         .vsel_reg = LP8752_VOUT_BUCK##_id,\
188         .vsel_mask = LP8752_BUCK_VSEL_MASK,\
189         .enable_time = 400,\
190         .owner = THIS_MODULE,\
191 }
192
193 static struct regulator_desc lp8752_reg_desc[] = {
194         lp8752_buck_desc(0),
195         lp8752_buck_desc(1),
196         lp8752_buck_desc(2),
197         lp8752_buck_desc(3),
198 };
199
200 static int lp8752_regulator_init(struct lp8752_chip *pchip)
201 {
202         int ret, icnt, buck_id;
203         struct lp8752_platform_data *pdata = pchip->pdata;
204         struct regulator_config rconfig = { };
205
206         rconfig.regmap = pchip->regmap;
207         rconfig.dev = pchip->dev;
208         rconfig.driver_data = pchip;
209
210         for (icnt = 0; icnt < mphase_buck[pchip->nphase].nreg; icnt++) {
211                 buck_id = mphase_buck[pchip->nphase].buck_id[icnt];
212                 rconfig.init_data = pdata->buck_data[buck_id];
213                 rconfig.of_node = pdata->of_node[buck_id];
214                 pchip->rdev[buck_id] =
215                 devm_regulator_register(pchip->dev,
216                                         &lp8752_reg_desc[buck_id],
217                                         &rconfig);
218                 if (IS_ERR(pchip->rdev[buck_id])) {
219                         ret = PTR_ERR(pchip->rdev[buck_id]);
220                         pchip->rdev[buck_id] = NULL;
221                         dev_err(pchip->dev, "regulator init failed: buck %d\n",
222                                 buck_id);
223                         return ret;
224                 }
225         }
226
227         return 0;
228 }
229
230 static const struct regmap_config lp8752_regmap = {
231         .reg_bits = 8,
232         .val_bits = 8,
233         .max_register = LP8752_REG_MAX,
234 };
235
236 static int lp8752_probe(struct i2c_client *client,
237                         const struct i2c_device_id *id)
238 {
239         int ret;
240         struct lp8752_chip *pchip;
241         struct lp8752_platform_data *pdata = dev_get_platdata(&client->dev);
242
243         pchip = devm_kzalloc(&client->dev,
244                              sizeof(struct lp8752_chip), GFP_KERNEL);
245         if (!pchip)
246                 return -ENOMEM;
247
248         pchip->dev = &client->dev;
249         pchip->regmap = devm_regmap_init_i2c(client, &lp8752_regmap);
250         if (IS_ERR(pchip->regmap)) {
251                 ret = PTR_ERR(pchip->regmap);
252                 dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
253                 return ret;
254         }
255         i2c_set_clientdata(client, pchip);
256
257         ret = regmap_update_bits(pchip->regmap,
258                                  LP8752_CTRL_BUCK0,
259                                  (1 << 0), 0x1);
260         ret = regmap_update_bits(pchip->regmap,
261                                  LP8752_CTRL_BUCK2,
262                                  (1 << 0), 0x1);
263
264         if (!pdata) {
265                 pchip->pdata = devm_kzalloc(pchip->dev,
266                                             sizeof(struct lp8752_platform_data),
267                                             GFP_KERNEL);
268                 if (!pchip->pdata)
269                         return -ENOMEM;
270
271                 ret = lp8752_init_data(pchip);
272                 if (ret < 0) {
273                         dev_err(&client->dev, "fail to initialize chip\n");
274                         return ret;
275                 }
276         } else {
277                 pchip->pdata = pdata;
278                 pchip->nphase = pdata->nphase;
279         }
280
281         lp8752_regulator_init(pchip);
282
283         return 0;
284 }
285
286 static const struct of_device_id lp8752_of_match[] = {
287         { .compatible = "ti,lp8752", },
288         {},
289 };
290 MODULE_DEVICE_TABLE(of, lp8752_of_match);
291
292 static const struct i2c_device_id lp8752_id[] = {
293         {"lp8752", 0},
294         {}
295 };
296 MODULE_DEVICE_TABLE(i2c, lp8752_id);
297
298 static struct i2c_driver lp8752_i2c_driver = {
299         .probe = lp8752_probe,
300         .id_table = lp8752_id,
301         .driver = {
302                 .name = "lp8752",
303                 .of_match_table = of_match_ptr(lp8752_of_match),
304         },
305 };
306
307 module_i2c_driver(lp8752_i2c_driver);
308
309 MODULE_DESCRIPTION("Texas Instruments lp8752 driver");
310 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
311 MODULE_LICENSE("GPL v2");