imx6: add pfuze100 regulator support
[openwrt/svn-archive/archive.git] / target / linux / imx6 / patches-3.10 / 0020-pfuze100-regulator.patch
1 --- a/drivers/regulator/Kconfig
2 +++ b/drivers/regulator/Kconfig
3 @@ -300,6 +300,14 @@ config REGULATOR_PCF50633
4 Say Y here to support the voltage regulators and convertors
5 on PCF50633
6
7 +config REGULATOR_PFUZE100
8 + tristate "Support regulators on Freescale PFUZE100 PMIC"
9 + depends on I2C
10 + select REGMAP_I2C
11 + help
12 + Say y here to support the regulators found on the Freescale PFUZE100
13 + PMIC.
14 +
15 config REGULATOR_RC5T583
16 tristate "RICOH RC5T583 Power regulators"
17 depends on MFD_RC5T583
18 --- a/drivers/regulator/Makefile
19 +++ b/drivers/regulator/Makefile
20 @@ -45,6 +45,7 @@ obj-$(CONFIG_REGULATOR_MC13783) += mc137
21 obj-$(CONFIG_REGULATOR_MC13892) += mc13892-regulator.o
22 obj-$(CONFIG_REGULATOR_MC13XXX_CORE) += mc13xxx-regulator-core.o
23 obj-$(CONFIG_REGULATOR_PALMAS) += palmas-regulator.o
24 +obj-$(CONFIG_REGULATOR_PFUZE100) += pfuze100-regulator.o
25 obj-$(CONFIG_REGULATOR_TPS51632) += tps51632-regulator.o
26 obj-$(CONFIG_REGULATOR_PCAP) += pcap-regulator.o
27 obj-$(CONFIG_REGULATOR_PCF50633) += pcf50633-regulator.o
28 --- /dev/null
29 +++ b/drivers/regulator/pfuze100-regulator.c
30 @@ -0,0 +1,445 @@
31 +/*
32 + * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
33 + *
34 + * This program is free software; you can redistribute it and/or modify
35 + * it under the terms of the GNU General Public License as published by
36 + * the Free Software Foundation; either version 2 of the License, or
37 + * (at your option) any later version.
38 + *
39 + * This program is distributed in the hope that it will be useful,
40 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 + * GNU General Public License for more details.
43 + *
44 + * You should have received a copy of the GNU General Public License
45 + * along with this program; if not, write to the Free Software
46 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
47 + */
48 +#include <linux/kernel.h>
49 +#include <linux/module.h>
50 +#include <linux/init.h>
51 +#include <linux/err.h>
52 +#include <linux/of.h>
53 +#include <linux/of_device.h>
54 +#include <linux/regulator/of_regulator.h>
55 +#include <linux/platform_device.h>
56 +#include <linux/regulator/driver.h>
57 +#include <linux/regulator/machine.h>
58 +#include <linux/regulator/pfuze100.h>
59 +#include <linux/i2c.h>
60 +#include <linux/slab.h>
61 +#include <linux/regmap.h>
62 +
63 +#define PFUZE_NUMREGS 128
64 +#define PFUZE100_VOL_OFFSET 0
65 +#define PFUZE100_STANDBY_OFFSET 1
66 +#define PFUZE100_MODE_OFFSET 3
67 +#define PFUZE100_CONF_OFFSET 4
68 +
69 +#define PFUZE100_DEVICEID 0x0
70 +#define PFUZE100_REVID 0x3
71 +#define PFUZE100_FABID 0x3
72 +
73 +#define PFUZE100_SW1ABVOL 0x20
74 +#define PFUZE100_SW1CVOL 0x2e
75 +#define PFUZE100_SW2VOL 0x35
76 +#define PFUZE100_SW3AVOL 0x3c
77 +#define PFUZE100_SW3BVOL 0x43
78 +#define PFUZE100_SW4VOL 0x4a
79 +#define PFUZE100_SWBSTCON1 0x66
80 +#define PFUZE100_VREFDDRCON 0x6a
81 +#define PFUZE100_VSNVSVOL 0x6b
82 +#define PFUZE100_VGEN1VOL 0x6c
83 +#define PFUZE100_VGEN2VOL 0x6d
84 +#define PFUZE100_VGEN3VOL 0x6e
85 +#define PFUZE100_VGEN4VOL 0x6f
86 +#define PFUZE100_VGEN5VOL 0x70
87 +#define PFUZE100_VGEN6VOL 0x71
88 +
89 +struct pfuze_regulator {
90 + struct regulator_desc desc;
91 + unsigned char stby_reg;
92 + unsigned char stby_mask;
93 +};
94 +
95 +struct pfuze_chip {
96 + struct regmap *regmap;
97 + struct device *dev;
98 + struct pfuze_regulator regulator_descs[PFUZE100_MAX_REGULATOR];
99 + struct regulator_dev *regulators[PFUZE100_MAX_REGULATOR];
100 +};
101 +
102 +static const int pfuze100_swbst[] = {
103 + 5000000, 5050000, 5100000, 5150000,
104 +};
105 +
106 +static const int pfuze100_vsnvs[] = {
107 + 1000000, 1100000, 1200000, 1300000, 1500000, 1800000, 3000000,
108 +};
109 +
110 +static const struct i2c_device_id pfuze_device_id[] = {
111 + {.name = "pfuze100"},
112 + {},
113 +};
114 +MODULE_DEVICE_TABLE(i2c, pfuze_device_id);
115 +
116 +static const struct of_device_id pfuze_dt_ids[] = {
117 + { .compatible = "fsl,pfuze100" },
118 + {},
119 +};
120 +MODULE_DEVICE_TABLE(of, pfuze_dt_ids);
121 +
122 +static int pfuze100_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
123 +{
124 + struct pfuze_chip *pfuze100 = rdev_get_drvdata(rdev);
125 + int id = rdev->desc->id;
126 + unsigned int ramp_bits;
127 + int ret;
128 +
129 + if (id < PFUZE100_SWBST) {
130 + ramp_delay = 12500 / ramp_delay;
131 + ramp_bits = (ramp_delay >> 1) - (ramp_delay >> 3);
132 + ret = regmap_update_bits(pfuze100->regmap,
133 + rdev->desc->vsel_reg + 4,
134 + 0xc0, ramp_bits << 6);
135 + if (ret < 0)
136 + dev_err(pfuze100->dev, "ramp failed, err %d\n", ret);
137 + } else
138 + ret = -EACCES;
139 +
140 + return ret;
141 +}
142 +
143 +static struct regulator_ops pfuze100_ldo_regulator_ops = {
144 + .enable = regulator_enable_regmap,
145 + .disable = regulator_disable_regmap,
146 + .is_enabled = regulator_is_enabled_regmap,
147 + .list_voltage = regulator_list_voltage_linear,
148 + .set_voltage_sel = regulator_set_voltage_sel_regmap,
149 + .get_voltage_sel = regulator_get_voltage_sel_regmap,
150 +};
151 +
152 +static struct regulator_ops pfuze100_fixed_regulator_ops = {
153 + .list_voltage = regulator_list_voltage_linear,
154 +};
155 +
156 +static struct regulator_ops pfuze100_sw_regulator_ops = {
157 + .list_voltage = regulator_list_voltage_linear,
158 + .set_voltage_sel = regulator_set_voltage_sel_regmap,
159 + .get_voltage_sel = regulator_get_voltage_sel_regmap,
160 + .set_voltage_time_sel = regulator_set_voltage_time_sel,
161 + .set_ramp_delay = pfuze100_set_ramp_delay,
162 +};
163 +
164 +static struct regulator_ops pfuze100_swb_regulator_ops = {
165 + .list_voltage = regulator_list_voltage_table,
166 + .map_voltage = regulator_map_voltage_ascend,
167 + .set_voltage_sel = regulator_set_voltage_sel_regmap,
168 + .get_voltage_sel = regulator_get_voltage_sel_regmap,
169 +
170 +};
171 +
172 +#define PFUZE100_FIXED_REG(_name, base, voltage) \
173 + [PFUZE100_ ## _name] = { \
174 + .desc = { \
175 + .name = #_name, \
176 + .n_voltages = 1, \
177 + .ops = &pfuze100_fixed_regulator_ops, \
178 + .type = REGULATOR_VOLTAGE, \
179 + .id = PFUZE100_ ## _name, \
180 + .owner = THIS_MODULE, \
181 + .min_uV = (voltage), \
182 + .enable_reg = (base), \
183 + .enable_mask = 0x10, \
184 + }, \
185 + }
186 +
187 +#define PFUZE100_SW_REG(_name, base, min, max, step) \
188 + [PFUZE100_ ## _name] = { \
189 + .desc = { \
190 + .name = #_name,\
191 + .n_voltages = ((max) - (min)) / (step) + 1, \
192 + .ops = &pfuze100_sw_regulator_ops, \
193 + .type = REGULATOR_VOLTAGE, \
194 + .id = PFUZE100_ ## _name, \
195 + .owner = THIS_MODULE, \
196 + .min_uV = (min), \
197 + .uV_step = (step), \
198 + .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
199 + .vsel_mask = 0x3f, \
200 + }, \
201 + .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
202 + .stby_mask = 0x3f, \
203 + }
204 +
205 +#define PFUZE100_SWB_REG(_name, base, mask, voltages) \
206 + [PFUZE100_ ## _name] = { \
207 + .desc = { \
208 + .name = #_name, \
209 + .n_voltages = ARRAY_SIZE(voltages), \
210 + .ops = &pfuze100_swb_regulator_ops, \
211 + .type = REGULATOR_VOLTAGE, \
212 + .id = PFUZE100_ ## _name, \
213 + .owner = THIS_MODULE, \
214 + .volt_table = voltages, \
215 + .vsel_reg = (base), \
216 + .vsel_mask = (mask), \
217 + }, \
218 + }
219 +
220 +#define PFUZE100_VGEN_REG(_name, base, min, max, step) \
221 + [PFUZE100_ ## _name] = { \
222 + .desc = { \
223 + .name = #_name, \
224 + .n_voltages = ((max) - (min)) / (step) + 1, \
225 + .ops = &pfuze100_ldo_regulator_ops, \
226 + .type = REGULATOR_VOLTAGE, \
227 + .id = PFUZE100_ ## _name, \
228 + .owner = THIS_MODULE, \
229 + .min_uV = (min), \
230 + .uV_step = (step), \
231 + .vsel_reg = (base), \
232 + .vsel_mask = 0xf, \
233 + .enable_reg = (base), \
234 + .enable_mask = 0x10, \
235 + }, \
236 + .stby_reg = (base), \
237 + .stby_mask = 0x20, \
238 + }
239 +
240 +static struct pfuze_regulator pfuze100_regulators[] = {
241 + PFUZE100_SW_REG(SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000),
242 + PFUZE100_SW_REG(SW1C, PFUZE100_SW1CVOL, 300000, 1875000, 25000),
243 + PFUZE100_SW_REG(SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000),
244 + PFUZE100_SW_REG(SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000),
245 + PFUZE100_SW_REG(SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000),
246 + PFUZE100_SW_REG(SW4, PFUZE100_SW4VOL, 400000, 1975000, 25000),
247 + PFUZE100_SWB_REG(SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst),
248 + PFUZE100_SWB_REG(VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
249 + PFUZE100_FIXED_REG(VREFDDR, PFUZE100_VREFDDRCON, 750000),
250 + PFUZE100_VGEN_REG(VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000),
251 + PFUZE100_VGEN_REG(VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
252 + PFUZE100_VGEN_REG(VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000),
253 + PFUZE100_VGEN_REG(VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000),
254 + PFUZE100_VGEN_REG(VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
255 + PFUZE100_VGEN_REG(VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
256 +};
257 +
258 +#ifdef CONFIG_OF
259 +static struct of_regulator_match pfuze100_matches[] = {
260 + { .name = "sw1ab", },
261 + { .name = "sw1c", },
262 + { .name = "sw2", },
263 + { .name = "sw3a", },
264 + { .name = "sw3b", },
265 + { .name = "sw4", },
266 + { .name = "swbst", },
267 + { .name = "vsnvs", },
268 + { .name = "vrefddr", },
269 + { .name = "vgen1", },
270 + { .name = "vgen2", },
271 + { .name = "vgen3", },
272 + { .name = "vgen4", },
273 + { .name = "vgen5", },
274 + { .name = "vgen6", },
275 +};
276 +
277 +static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
278 +{
279 + struct device *dev = chip->dev;
280 + struct device_node *np, *parent;
281 + int ret;
282 +
283 + np = of_node_get(dev->parent->of_node);
284 + if (!np)
285 + return 0;
286 +
287 + parent = of_find_node_by_name(np, "regulators");
288 + if (!parent) {
289 + dev_err(dev, "regulators node not found\n");
290 + return -EINVAL;
291 + }
292 +
293 + ret = of_regulator_match(dev, parent, pfuze100_matches,
294 + ARRAY_SIZE(pfuze100_matches));
295 +
296 + of_node_put(parent);
297 + if (ret < 0) {
298 + dev_err(dev, "Error parsing regulator init data: %d\n",
299 + ret);
300 + return ret;
301 + }
302 +
303 + return 0;
304 +}
305 +
306 +static inline struct regulator_init_data *match_init_data(int index)
307 +{
308 + return pfuze100_matches[index].init_data;
309 +}
310 +
311 +static inline struct device_node *match_of_node(int index)
312 +{
313 + return pfuze100_matches[index].of_node;
314 +}
315 +#else
316 +static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
317 +{
318 + return 0;
319 +}
320 +
321 +static inline struct regulator_init_data *match_init_data(int index)
322 +{
323 + return NULL;
324 +}
325 +
326 +static inline struct device_node *match_of_node(int index)
327 +{
328 + return NULL;
329 +}
330 +#endif
331 +
332 +static int pfuze_identify(struct pfuze_chip *pfuze_chip)
333 +{
334 + unsigned int value;
335 + int ret;
336 +
337 + ret = regmap_read(pfuze_chip->regmap, PFUZE100_DEVICEID, &value);
338 + if (ret)
339 + return ret;
340 +
341 + if (value & 0x0f) {
342 + dev_warn(pfuze_chip->dev, "Illegal ID: %x\n", value);
343 + return -ENODEV;
344 + }
345 +
346 + ret = regmap_read(pfuze_chip->regmap, PFUZE100_REVID, &value);
347 + if (ret)
348 + return ret;
349 + dev_info(pfuze_chip->dev,
350 + "Full lay: %x, Metal lay: %x\n",
351 + (value & 0xf0) >> 4, value & 0x0f);
352 +
353 + ret = regmap_read(pfuze_chip->regmap, PFUZE100_FABID, &value);
354 + if (ret)
355 + return ret;
356 + dev_info(pfuze_chip->dev, "FAB: %x, FIN: %x\n",
357 + (value & 0xc) >> 2, value & 0x3);
358 +
359 + return 0;
360 +}
361 +
362 +static const struct regmap_config pfuze_regmap_config = {
363 + .reg_bits = 8,
364 + .val_bits = 8,
365 + .max_register = PFUZE_NUMREGS - 1,
366 + .cache_type = REGCACHE_RBTREE,
367 +};
368 +
369 +static int pfuze100_regulator_probe(struct i2c_client *client,
370 + const struct i2c_device_id *id)
371 +{
372 + struct pfuze_chip *pfuze_chip;
373 + struct pfuze_regulator_platform_data *pdata =
374 + dev_get_platdata(&client->dev);
375 + struct regulator_config config = { };
376 + int i, ret;
377 +
378 + pfuze_chip = devm_kzalloc(&client->dev, sizeof(*pfuze_chip),
379 + GFP_KERNEL);
380 + if (!pfuze_chip)
381 + return -ENOMEM;
382 +
383 + i2c_set_clientdata(client, pfuze_chip);
384 +
385 + memcpy(pfuze_chip->regulator_descs, pfuze100_regulators,
386 + sizeof(pfuze_chip->regulator_descs));
387 +
388 + pfuze_chip->dev = &client->dev;
389 +
390 + pfuze_chip->regmap = devm_regmap_init_i2c(client, &pfuze_regmap_config);
391 + if (IS_ERR(pfuze_chip->regmap)) {
392 + ret = PTR_ERR(pfuze_chip->regmap);
393 + dev_err(&client->dev,
394 + "regmap allocation failed with err %d\n", ret);
395 + return ret;
396 + }
397 +
398 + ret = pfuze_identify(pfuze_chip);
399 + if (ret) {
400 + dev_err(&client->dev, "unrecognized pfuze chip ID!\n");
401 + return ret;
402 + }
403 +
404 + ret = pfuze_parse_regulators_dt(pfuze_chip);
405 + if (ret)
406 + return ret;
407 +
408 + for (i = 0; i < PFUZE100_MAX_REGULATOR; i++) {
409 + struct regulator_init_data *init_data;
410 + struct regulator_desc *desc;
411 + int val;
412 +
413 + desc = &pfuze_chip->regulator_descs[i].desc;
414 +
415 + if (pdata)
416 + init_data = pdata->init_data[i];
417 + else
418 + init_data = match_init_data(i);
419 +
420 + /* SW2~SW4 high bit check and modify the voltage value table */
421 + if (i > PFUZE100_SW1C && i < PFUZE100_SWBST) {
422 + regmap_read(pfuze_chip->regmap, desc->vsel_reg, &val);
423 + if (val & 0x40) {
424 + desc->min_uV = 800000;
425 + desc->uV_step = 50000;
426 + desc->n_voltages = 51;
427 + }
428 + }
429 +
430 + config.dev = &client->dev;
431 + config.init_data = init_data;
432 + config.driver_data = pfuze_chip;
433 + config.of_node = match_of_node(i);
434 +
435 + pfuze_chip->regulators[i] = regulator_register(desc, &config);
436 + if (IS_ERR(pfuze_chip->regulators[i])) {
437 + dev_err(&client->dev, "register regulator%s failed\n",
438 + pfuze100_regulators[i].desc.name);
439 + ret = PTR_ERR(pfuze_chip->regulators[i]);
440 + while (--i >= 0)
441 + regulator_unregister(pfuze_chip->regulators[i]);
442 + return ret;
443 + }
444 + }
445 +
446 + return 0;
447 +}
448 +
449 +static int pfuze100_regulator_remove(struct i2c_client *client)
450 +{
451 + int i;
452 + struct pfuze_chip *pfuze_chip = i2c_get_clientdata(client);
453 +
454 + for (i = 0; i < PFUZE100_MAX_REGULATOR; i++)
455 + regulator_unregister(pfuze_chip->regulators[i]);
456 +
457 + return 0;
458 +}
459 +
460 +static struct i2c_driver pfuze_driver = {
461 + .id_table = pfuze_device_id,
462 + .driver = {
463 + .name = "pfuze100-regulator",
464 + .owner = THIS_MODULE,
465 + .of_match_table = pfuze_dt_ids,
466 + },
467 + .probe = pfuze100_regulator_probe,
468 + .remove = pfuze100_regulator_remove,
469 +};
470 +module_i2c_driver(pfuze_driver);
471 +
472 +MODULE_AUTHOR("Robin Gong <b38343@freescale.com>");
473 +MODULE_DESCRIPTION("Regulator Driver for Freescale PFUZE100 PMIC");
474 +MODULE_LICENSE("GPL v2");
475 +MODULE_ALIAS("i2c:pfuze100-regulator");
476 --- /dev/null
477 +++ b/include/linux/regulator/pfuze100.h
478 @@ -0,0 +1,44 @@
479 +/*
480 + * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
481 + *
482 + * This program is free software; you can redistribute it and/or modify
483 + * it under the terms of the GNU General Public License as published by
484 + * the Free Software Foundation; either version 2 of the License, or
485 + * (at your option) any later version.
486 + *
487 + * This program is distributed in the hope that it will be useful,
488 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
489 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
490 + * GNU General Public License for more details.
491 + *
492 + * You should have received a copy of the GNU General Public License along
493 + * with this program; if not, write to the Free Software Foundation, Inc.,
494 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
495 + */
496 +#ifndef __LINUX_REG_PFUZE100_H
497 +#define __LINUX_REG_PFUZE100_H
498 +
499 +#define PFUZE100_SW1AB 0
500 +#define PFUZE100_SW1C 1
501 +#define PFUZE100_SW2 2
502 +#define PFUZE100_SW3A 3
503 +#define PFUZE100_SW3B 4
504 +#define PFUZE100_SW4 5
505 +#define PFUZE100_SWBST 6
506 +#define PFUZE100_VSNVS 7
507 +#define PFUZE100_VREFDDR 8
508 +#define PFUZE100_VGEN1 9
509 +#define PFUZE100_VGEN2 10
510 +#define PFUZE100_VGEN3 11
511 +#define PFUZE100_VGEN4 12
512 +#define PFUZE100_VGEN5 13
513 +#define PFUZE100_VGEN6 14
514 +#define PFUZE100_MAX_REGULATOR 15
515 +
516 +struct regulator_init_data;
517 +
518 +struct pfuze_regulator_platform_data {
519 + struct regulator_init_data *init_data[PFUZE100_MAX_REGULATOR];
520 +};
521 +
522 +#endif /* __LINUX_REG_PFUZE100_H */