aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorYong Shen <yong.shen@linaro.org>2010-12-14 01:00:54 -0500
committerLiam Girdwood <lrg@slimlogic.co.uk>2011-01-12 09:33:03 -0500
commit167e3d8af98a111994c4d6b3c5cbc589aedbbc2a (patch)
tree16b54e06e33b949a0ca98542131610926d483bae /drivers
parent57c78e359a35c69eca4c88f107500f74ef7f0acf (diff)
make mc13783 regulator code generic
move some common functions and micros of mc13783 regulaor driver to a seperate file, which makes it possible for mc13892 to share code. Signed-off-by: Yong Shen <yong.shen@linaro.org> Acked-by: Sascha Hauer <s.hauer@pengutronix.de> Acked-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Signed-off-by: Liam Girdwood <lrg@slimlogic.co.uk>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/regulator/Kconfig4
-rw-r--r--drivers/regulator/Makefile1
-rw-r--r--drivers/regulator/mc13783-regulator.c325
-rw-r--r--drivers/regulator/mc13xxx-regulator-core.c234
-rw-r--r--drivers/regulator/mc13xxx.h101
5 files changed, 383 insertions, 282 deletions
diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
index da34981b1675..ce17a038b0b8 100644
--- a/drivers/regulator/Kconfig
+++ b/drivers/regulator/Kconfig
@@ -186,9 +186,13 @@ config REGULATOR_PCAP
186 This driver provides support for the voltage regulators of the 186 This driver provides support for the voltage regulators of the
187 PCAP2 PMIC. 187 PCAP2 PMIC.
188 188
189config REGULATOR_MC13XXX_CORE
190 bool
191
189config REGULATOR_MC13783 192config REGULATOR_MC13783
190 tristate "Support regulators on Freescale MC13783 PMIC" 193 tristate "Support regulators on Freescale MC13783 PMIC"
191 depends on MFD_MC13783 194 depends on MFD_MC13783
195 select REGULATOR_MC13XXX_CORE
192 help 196 help
193 Say y here to support the regulators found on the Freescale MC13783 197 Say y here to support the regulators found on the Freescale MC13783
194 PMIC. 198 PMIC.
diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile
index cf71df72ea5a..0b33bac4f2ab 100644
--- a/drivers/regulator/Makefile
+++ b/drivers/regulator/Makefile
@@ -30,6 +30,7 @@ obj-$(CONFIG_REGULATOR_DA903X) += da903x.o
30obj-$(CONFIG_REGULATOR_PCF50633) += pcf50633-regulator.o 30obj-$(CONFIG_REGULATOR_PCF50633) += pcf50633-regulator.o
31obj-$(CONFIG_REGULATOR_PCAP) += pcap-regulator.o 31obj-$(CONFIG_REGULATOR_PCAP) += pcap-regulator.o
32obj-$(CONFIG_REGULATOR_MC13783) += mc13783-regulator.o 32obj-$(CONFIG_REGULATOR_MC13783) += mc13783-regulator.o
33obj-$(CONFIG_REGULATOR_MC13XXX_CORE) += mc13xxx-regulator-core.o
33obj-$(CONFIG_REGULATOR_AB3100) += ab3100.o 34obj-$(CONFIG_REGULATOR_AB3100) += ab3100.o
34 35
35obj-$(CONFIG_REGULATOR_TPS65023) += tps65023-regulator.o 36obj-$(CONFIG_REGULATOR_TPS65023) += tps65023-regulator.o
diff --git a/drivers/regulator/mc13783-regulator.c b/drivers/regulator/mc13783-regulator.c
index 6a6e1d63d86f..3e5d0c3b4e53 100644
--- a/drivers/regulator/mc13783-regulator.c
+++ b/drivers/regulator/mc13783-regulator.c
@@ -1,6 +1,7 @@
1/* 1/*
2 * Regulator Driver for Freescale MC13783 PMIC 2 * Regulator Driver for Freescale MC13783 PMIC
3 * 3 *
4 * Copyright 2010 Yong Shen <yong.shen@linaro.org>
4 * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> 5 * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
5 * Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com> 6 * Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
6 * 7 *
@@ -17,6 +18,7 @@
17#include <linux/slab.h> 18#include <linux/slab.h>
18#include <linux/init.h> 19#include <linux/init.h>
19#include <linux/err.h> 20#include <linux/err.h>
21#include "mc13xxx.h"
20 22
21#define MC13783_REG_SWITCHERS5 29 23#define MC13783_REG_SWITCHERS5 29
22#define MC13783_REG_SWITCHERS5_SW3EN (1 << 20) 24#define MC13783_REG_SWITCHERS5_SW3EN (1 << 20)
@@ -89,16 +91,6 @@
89#define MC13783_REG_POWERMISC_PWGTSPI_M (3 << 15) 91#define MC13783_REG_POWERMISC_PWGTSPI_M (3 << 15)
90 92
91 93
92struct mc13783_regulator {
93 struct regulator_desc desc;
94 int reg;
95 int enable_bit;
96 int vsel_reg;
97 int vsel_shift;
98 int vsel_mask;
99 int const *voltages;
100};
101
102/* Voltage Values */ 94/* Voltage Values */
103static const int mc13783_sw3_val[] = { 95static const int mc13783_sw3_val[] = {
104 5000000, 5000000, 5000000, 5500000, 96 5000000, 5000000, 5000000, 5500000,
@@ -175,64 +167,26 @@ static const int mc13783_pwgtdrv_val[] = {
175 5500000, 167 5500000,
176}; 168};
177 169
178static struct regulator_ops mc13783_regulator_ops;
179static struct regulator_ops mc13783_fixed_regulator_ops;
180static struct regulator_ops mc13783_gpo_regulator_ops; 170static struct regulator_ops mc13783_gpo_regulator_ops;
181 171
182#define MC13783_DEFINE(prefix, _name, _reg, _vsel_reg, _voltages) \ 172#define MC13783_DEFINE(prefix, name, reg, vsel_reg, voltages) \
183 [MC13783_ ## prefix ## _ ## _name] = { \ 173 MC13xxx_DEFINE(MC13783_REG_, name, reg, vsel_reg, voltages, \
184 .desc = { \ 174 mc13xxx_regulator_ops)
185 .name = #prefix "_" #_name, \
186 .n_voltages = ARRAY_SIZE(_voltages), \
187 .ops = &mc13783_regulator_ops, \
188 .type = REGULATOR_VOLTAGE, \
189 .id = MC13783_ ## prefix ## _ ## _name, \
190 .owner = THIS_MODULE, \
191 }, \
192 .reg = MC13783_REG_ ## _reg, \
193 .enable_bit = MC13783_REG_ ## _reg ## _ ## _name ## EN, \
194 .vsel_reg = MC13783_REG_ ## _vsel_reg, \
195 .vsel_shift = MC13783_REG_ ## _vsel_reg ## _ ## _name ## VSEL,\
196 .vsel_mask = MC13783_REG_ ## _vsel_reg ## _ ## _name ## VSEL_M,\
197 .voltages = _voltages, \
198 }
199 175
200#define MC13783_FIXED_DEFINE(prefix, _name, _reg, _voltages) \ 176#define MC13783_FIXED_DEFINE(prefix, name, reg, voltages) \
201 [MC13783_ ## prefix ## _ ## _name] = { \ 177 MC13xxx_FIXED_DEFINE(MC13783_REG_, name, reg, voltages, \
202 .desc = { \ 178 mc13xxx_fixed_regulator_ops)
203 .name = #prefix "_" #_name, \
204 .n_voltages = ARRAY_SIZE(_voltages), \
205 .ops = &mc13783_fixed_regulator_ops, \
206 .type = REGULATOR_VOLTAGE, \
207 .id = MC13783_ ## prefix ## _ ## _name, \
208 .owner = THIS_MODULE, \
209 }, \
210 .reg = MC13783_REG_ ## _reg, \
211 .enable_bit = MC13783_REG_ ## _reg ## _ ## _name ## EN, \
212 .voltages = _voltages, \
213 }
214 179
215#define MC13783_GPO_DEFINE(prefix, _name, _reg, _voltages) \ 180#define MC13783_GPO_DEFINE(prefix, name, reg, voltages) \
216 [MC13783_ ## prefix ## _ ## _name] = { \ 181 MC13xxx_GPO_DEFINE(MC13783_REG_, name, reg, voltages, \
217 .desc = { \ 182 mc13783_gpo_regulator_ops)
218 .name = #prefix "_" #_name, \
219 .n_voltages = ARRAY_SIZE(_voltages), \
220 .ops = &mc13783_gpo_regulator_ops, \
221 .type = REGULATOR_VOLTAGE, \
222 .id = MC13783_ ## prefix ## _ ## _name, \
223 .owner = THIS_MODULE, \
224 }, \
225 .reg = MC13783_REG_ ## _reg, \
226 .enable_bit = MC13783_REG_ ## _reg ## _ ## _name ## EN, \
227 .voltages = _voltages, \
228 }
229 183
230#define MC13783_DEFINE_SW(_name, _reg, _vsel_reg, _voltages) \ 184#define MC13783_DEFINE_SW(_name, _reg, _vsel_reg, _voltages) \
231 MC13783_DEFINE(REG, _name, _reg, _vsel_reg, _voltages) 185 MC13783_DEFINE(REG, _name, _reg, _vsel_reg, _voltages)
232#define MC13783_DEFINE_REGU(_name, _reg, _vsel_reg, _voltages) \ 186#define MC13783_DEFINE_REGU(_name, _reg, _vsel_reg, _voltages) \
233 MC13783_DEFINE(REG, _name, _reg, _vsel_reg, _voltages) 187 MC13783_DEFINE(REG, _name, _reg, _vsel_reg, _voltages)
234 188
235static struct mc13783_regulator mc13783_regulators[] = { 189static struct mc13xxx_regulator mc13783_regulators[] = {
236 MC13783_DEFINE_SW(SW3, SWITCHERS5, SWITCHERS5, mc13783_sw3_val), 190 MC13783_DEFINE_SW(SW3, SWITCHERS5, SWITCHERS5, mc13783_sw3_val),
237 191
238 MC13783_FIXED_DEFINE(REG, VAUDIO, REGULATORMODE0, mc13783_vaudio_val), 192 MC13783_FIXED_DEFINE(REG, VAUDIO, REGULATORMODE0, mc13783_vaudio_val),
@@ -274,213 +228,16 @@ static struct mc13783_regulator mc13783_regulators[] = {
274 MC13783_GPO_DEFINE(REG, PWGT2SPI, POWERMISC, mc13783_pwgtdrv_val), 228 MC13783_GPO_DEFINE(REG, PWGT2SPI, POWERMISC, mc13783_pwgtdrv_val),
275}; 229};
276 230
277struct mc13783_regulator_priv { 231static int mc13783_powermisc_rmw(struct mc13xxx_regulator_priv *priv, u32 mask,
278 struct mc13783 *mc13783; 232 u32 val)
279 u32 powermisc_pwgt_state;
280 struct regulator_dev *regulators[];
281};
282
283static int mc13783_regulator_enable(struct regulator_dev *rdev)
284{
285 struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
286 int id = rdev_get_id(rdev);
287 int ret;
288
289 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
290
291 mc13783_lock(priv->mc13783);
292 ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg,
293 mc13783_regulators[id].enable_bit,
294 mc13783_regulators[id].enable_bit);
295 mc13783_unlock(priv->mc13783);
296
297 return ret;
298}
299
300static int mc13783_regulator_disable(struct regulator_dev *rdev)
301{
302 struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
303 int id = rdev_get_id(rdev);
304 int ret;
305
306 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
307
308 mc13783_lock(priv->mc13783);
309 ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg,
310 mc13783_regulators[id].enable_bit, 0);
311 mc13783_unlock(priv->mc13783);
312
313 return ret;
314}
315
316static int mc13783_regulator_is_enabled(struct regulator_dev *rdev)
317{
318 struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
319 int ret, id = rdev_get_id(rdev);
320 unsigned int val;
321
322 mc13783_lock(priv->mc13783);
323 ret = mc13783_reg_read(priv->mc13783, mc13783_regulators[id].reg, &val);
324 mc13783_unlock(priv->mc13783);
325
326 if (ret)
327 return ret;
328
329 return (val & mc13783_regulators[id].enable_bit) != 0;
330}
331
332static int mc13783_regulator_list_voltage(struct regulator_dev *rdev,
333 unsigned selector)
334{
335 int id = rdev_get_id(rdev);
336
337 if (selector >= mc13783_regulators[id].desc.n_voltages)
338 return -EINVAL;
339
340 return mc13783_regulators[id].voltages[selector];
341}
342
343static int mc13783_get_best_voltage_index(struct regulator_dev *rdev,
344 int min_uV, int max_uV)
345{
346 int reg_id = rdev_get_id(rdev);
347 int i;
348 int bestmatch;
349 int bestindex;
350
351 /*
352 * Locate the minimum voltage fitting the criteria on
353 * this regulator. The switchable voltages are not
354 * in strict falling order so we need to check them
355 * all for the best match.
356 */
357 bestmatch = INT_MAX;
358 bestindex = -1;
359 for (i = 0; i < mc13783_regulators[reg_id].desc.n_voltages; i++) {
360 if (mc13783_regulators[reg_id].voltages[i] >= min_uV &&
361 mc13783_regulators[reg_id].voltages[i] < bestmatch) {
362 bestmatch = mc13783_regulators[reg_id].voltages[i];
363 bestindex = i;
364 }
365 }
366
367 if (bestindex < 0 || bestmatch > max_uV) {
368 dev_warn(&rdev->dev, "no possible value for %d<=x<=%d uV\n",
369 min_uV, max_uV);
370 return -EINVAL;
371 }
372 return bestindex;
373}
374
375static int mc13783_regulator_set_voltage(struct regulator_dev *rdev,
376 int min_uV, int max_uV,
377 unsigned *selector)
378{
379 struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
380 int value, id = rdev_get_id(rdev);
381 int ret;
382
383 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
384 __func__, id, min_uV, max_uV);
385
386 /* Find the best index */
387 value = mc13783_get_best_voltage_index(rdev, min_uV, max_uV);
388 dev_dbg(rdev_get_dev(rdev), "%s best value: %d \n", __func__, value);
389 if (value < 0)
390 return value;
391
392 *selector = value;
393
394 mc13783_lock(priv->mc13783);
395 ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].vsel_reg,
396 mc13783_regulators[id].vsel_mask,
397 value << mc13783_regulators[id].vsel_shift);
398 mc13783_unlock(priv->mc13783);
399
400 return ret;
401}
402
403static int mc13783_regulator_get_voltage(struct regulator_dev *rdev)
404{
405 struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
406 int ret, id = rdev_get_id(rdev);
407 unsigned int val;
408
409 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
410
411 mc13783_lock(priv->mc13783);
412 ret = mc13783_reg_read(priv->mc13783,
413 mc13783_regulators[id].vsel_reg, &val);
414 mc13783_unlock(priv->mc13783);
415
416 if (ret)
417 return ret;
418
419 val = (val & mc13783_regulators[id].vsel_mask)
420 >> mc13783_regulators[id].vsel_shift;
421
422 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
423
424 BUG_ON(val < 0 || val > mc13783_regulators[id].desc.n_voltages);
425
426 return mc13783_regulators[id].voltages[val];
427}
428
429static struct regulator_ops mc13783_regulator_ops = {
430 .enable = mc13783_regulator_enable,
431 .disable = mc13783_regulator_disable,
432 .is_enabled = mc13783_regulator_is_enabled,
433 .list_voltage = mc13783_regulator_list_voltage,
434 .set_voltage = mc13783_regulator_set_voltage,
435 .get_voltage = mc13783_regulator_get_voltage,
436};
437
438static int mc13783_fixed_regulator_set_voltage(struct regulator_dev *rdev,
439 int min_uV, int max_uV,
440 unsigned int *selector)
441{
442 int id = rdev_get_id(rdev);
443
444 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
445 __func__, id, min_uV, max_uV);
446
447 *selector = 0;
448
449 if (min_uV >= mc13783_regulators[id].voltages[0] &&
450 max_uV <= mc13783_regulators[id].voltages[0])
451 return 0;
452 else
453 return -EINVAL;
454}
455
456static int mc13783_fixed_regulator_get_voltage(struct regulator_dev *rdev)
457{
458 int id = rdev_get_id(rdev);
459
460 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
461
462 return mc13783_regulators[id].voltages[0];
463}
464
465static struct regulator_ops mc13783_fixed_regulator_ops = {
466 .enable = mc13783_regulator_enable,
467 .disable = mc13783_regulator_disable,
468 .is_enabled = mc13783_regulator_is_enabled,
469 .list_voltage = mc13783_regulator_list_voltage,
470 .set_voltage = mc13783_fixed_regulator_set_voltage,
471 .get_voltage = mc13783_fixed_regulator_get_voltage,
472};
473
474static int mc13783_powermisc_rmw(struct mc13783_regulator_priv *priv, u32 mask,
475 u32 val)
476{ 233{
477 struct mc13783 *mc13783 = priv->mc13783; 234 struct mc13xxx *mc13783 = priv->mc13xxx;
478 int ret; 235 int ret;
479 u32 valread; 236 u32 valread;
480 237
481 BUG_ON(val & ~mask); 238 BUG_ON(val & ~mask);
482 239
483 ret = mc13783_reg_read(mc13783, MC13783_REG_POWERMISC, &valread); 240 ret = mc13xxx_reg_read(mc13783, MC13783_REG_POWERMISC, &valread);
484 if (ret) 241 if (ret)
485 return ret; 242 return ret;
486 243
@@ -495,15 +252,16 @@ static int mc13783_powermisc_rmw(struct mc13783_regulator_priv *priv, u32 mask,
495 valread = (valread & ~MC13783_REG_POWERMISC_PWGTSPI_M) | 252 valread = (valread & ~MC13783_REG_POWERMISC_PWGTSPI_M) |
496 priv->powermisc_pwgt_state; 253 priv->powermisc_pwgt_state;
497 254
498 return mc13783_reg_write(mc13783, MC13783_REG_POWERMISC, valread); 255 return mc13xxx_reg_write(mc13783, MC13783_REG_POWERMISC, valread);
499} 256}
500 257
501static int mc13783_gpo_regulator_enable(struct regulator_dev *rdev) 258static int mc13783_gpo_regulator_enable(struct regulator_dev *rdev)
502{ 259{
503 struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); 260 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
261 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
504 int id = rdev_get_id(rdev); 262 int id = rdev_get_id(rdev);
505 int ret; 263 int ret;
506 u32 en_val = mc13783_regulators[id].enable_bit; 264 u32 en_val = mc13xxx_regulators[id].enable_bit;
507 265
508 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); 266 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
509 267
@@ -512,17 +270,18 @@ static int mc13783_gpo_regulator_enable(struct regulator_dev *rdev)
512 id == MC13783_REG_PWGT2SPI) 270 id == MC13783_REG_PWGT2SPI)
513 en_val = 0; 271 en_val = 0;
514 272
515 mc13783_lock(priv->mc13783); 273 mc13xxx_lock(priv->mc13xxx);
516 ret = mc13783_powermisc_rmw(priv, mc13783_regulators[id].enable_bit, 274 ret = mc13783_powermisc_rmw(priv, mc13xxx_regulators[id].enable_bit,
517 en_val); 275 en_val);
518 mc13783_unlock(priv->mc13783); 276 mc13xxx_unlock(priv->mc13xxx);
519 277
520 return ret; 278 return ret;
521} 279}
522 280
523static int mc13783_gpo_regulator_disable(struct regulator_dev *rdev) 281static int mc13783_gpo_regulator_disable(struct regulator_dev *rdev)
524{ 282{
525 struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); 283 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
284 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
526 int id = rdev_get_id(rdev); 285 int id = rdev_get_id(rdev);
527 int ret; 286 int ret;
528 u32 dis_val = 0; 287 u32 dis_val = 0;
@@ -532,25 +291,26 @@ static int mc13783_gpo_regulator_disable(struct regulator_dev *rdev)
532 /* Power Gate disable value is 1 */ 291 /* Power Gate disable value is 1 */
533 if (id == MC13783_REG_PWGT1SPI || 292 if (id == MC13783_REG_PWGT1SPI ||
534 id == MC13783_REG_PWGT2SPI) 293 id == MC13783_REG_PWGT2SPI)
535 dis_val = mc13783_regulators[id].enable_bit; 294 dis_val = mc13xxx_regulators[id].enable_bit;
536 295
537 mc13783_lock(priv->mc13783); 296 mc13xxx_lock(priv->mc13xxx);
538 ret = mc13783_powermisc_rmw(priv, mc13783_regulators[id].enable_bit, 297 ret = mc13783_powermisc_rmw(priv, mc13xxx_regulators[id].enable_bit,
539 dis_val); 298 dis_val);
540 mc13783_unlock(priv->mc13783); 299 mc13xxx_unlock(priv->mc13xxx);
541 300
542 return ret; 301 return ret;
543} 302}
544 303
545static int mc13783_gpo_regulator_is_enabled(struct regulator_dev *rdev) 304static int mc13783_gpo_regulator_is_enabled(struct regulator_dev *rdev)
546{ 305{
547 struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); 306 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
307 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
548 int ret, id = rdev_get_id(rdev); 308 int ret, id = rdev_get_id(rdev);
549 unsigned int val; 309 unsigned int val;
550 310
551 mc13783_lock(priv->mc13783); 311 mc13xxx_lock(priv->mc13xxx);
552 ret = mc13783_reg_read(priv->mc13783, mc13783_regulators[id].reg, &val); 312 ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
553 mc13783_unlock(priv->mc13783); 313 mc13xxx_unlock(priv->mc13xxx);
554 314
555 if (ret) 315 if (ret)
556 return ret; 316 return ret;
@@ -560,22 +320,22 @@ static int mc13783_gpo_regulator_is_enabled(struct regulator_dev *rdev)
560 val = (val & ~MC13783_REG_POWERMISC_PWGTSPI_M) | 320 val = (val & ~MC13783_REG_POWERMISC_PWGTSPI_M) |
561 (priv->powermisc_pwgt_state ^ MC13783_REG_POWERMISC_PWGTSPI_M); 321 (priv->powermisc_pwgt_state ^ MC13783_REG_POWERMISC_PWGTSPI_M);
562 322
563 return (val & mc13783_regulators[id].enable_bit) != 0; 323 return (val & mc13xxx_regulators[id].enable_bit) != 0;
564} 324}
565 325
566static struct regulator_ops mc13783_gpo_regulator_ops = { 326static struct regulator_ops mc13783_gpo_regulator_ops = {
567 .enable = mc13783_gpo_regulator_enable, 327 .enable = mc13783_gpo_regulator_enable,
568 .disable = mc13783_gpo_regulator_disable, 328 .disable = mc13783_gpo_regulator_disable,
569 .is_enabled = mc13783_gpo_regulator_is_enabled, 329 .is_enabled = mc13783_gpo_regulator_is_enabled,
570 .list_voltage = mc13783_regulator_list_voltage, 330 .list_voltage = mc13xxx_regulator_list_voltage,
571 .set_voltage = mc13783_fixed_regulator_set_voltage, 331 .set_voltage = mc13xxx_fixed_regulator_set_voltage,
572 .get_voltage = mc13783_fixed_regulator_get_voltage, 332 .get_voltage = mc13xxx_fixed_regulator_get_voltage,
573}; 333};
574 334
575static int __devinit mc13783_regulator_probe(struct platform_device *pdev) 335static int __devinit mc13783_regulator_probe(struct platform_device *pdev)
576{ 336{
577 struct mc13783_regulator_priv *priv; 337 struct mc13xxx_regulator_priv *priv;
578 struct mc13783 *mc13783 = dev_get_drvdata(pdev->dev.parent); 338 struct mc13xxx *mc13783 = dev_get_drvdata(pdev->dev.parent);
579 struct mc13783_regulator_platform_data *pdata = 339 struct mc13783_regulator_platform_data *pdata =
580 dev_get_platdata(&pdev->dev); 340 dev_get_platdata(&pdev->dev);
581 struct mc13783_regulator_init_data *init_data; 341 struct mc13783_regulator_init_data *init_data;
@@ -589,7 +349,8 @@ static int __devinit mc13783_regulator_probe(struct platform_device *pdev)
589 if (!priv) 349 if (!priv)
590 return -ENOMEM; 350 return -ENOMEM;
591 351
592 priv->mc13783 = mc13783; 352 priv->mc13xxx_regulators = mc13783_regulators;
353 priv->mc13xxx = mc13783;
593 354
594 for (i = 0; i < pdata->num_regulators; i++) { 355 for (i = 0; i < pdata->num_regulators; i++) {
595 init_data = &pdata->regulators[i]; 356 init_data = &pdata->regulators[i];
@@ -619,7 +380,7 @@ err:
619 380
620static int __devexit mc13783_regulator_remove(struct platform_device *pdev) 381static int __devexit mc13783_regulator_remove(struct platform_device *pdev)
621{ 382{
622 struct mc13783_regulator_priv *priv = platform_get_drvdata(pdev); 383 struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev);
623 struct mc13783_regulator_platform_data *pdata = 384 struct mc13783_regulator_platform_data *pdata =
624 dev_get_platdata(&pdev->dev); 385 dev_get_platdata(&pdev->dev);
625 int i; 386 int i;
diff --git a/drivers/regulator/mc13xxx-regulator-core.c b/drivers/regulator/mc13xxx-regulator-core.c
new file mode 100644
index 000000000000..448e8f4cf41b
--- /dev/null
+++ b/drivers/regulator/mc13xxx-regulator-core.c
@@ -0,0 +1,234 @@
1/*
2 * Regulator Driver for Freescale MC13xxx PMIC
3 *
4 * Copyright 2010 Yong Shen <yong.shen@linaro.org>
5 *
6 * Based on mc13783 regulator driver :
7 * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
8 * Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * Regs infos taken from mc13xxx drivers from freescale and mc13xxx.pdf file
15 * from freescale
16 */
17
18#include <linux/mfd/mc13xxx.h>
19#include <linux/regulator/machine.h>
20#include <linux/regulator/driver.h>
21#include <linux/platform_device.h>
22#include <linux/kernel.h>
23#include <linux/slab.h>
24#include <linux/init.h>
25#include <linux/err.h>
26#include "mc13xxx.h"
27
28static int mc13xxx_regulator_enable(struct regulator_dev *rdev)
29{
30 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
31 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
32 int id = rdev_get_id(rdev);
33 int ret;
34
35 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
36
37 mc13xxx_lock(priv->mc13xxx);
38 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
39 mc13xxx_regulators[id].enable_bit,
40 mc13xxx_regulators[id].enable_bit);
41 mc13xxx_unlock(priv->mc13xxx);
42
43 return ret;
44}
45
46static int mc13xxx_regulator_disable(struct regulator_dev *rdev)
47{
48 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
49 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
50 int id = rdev_get_id(rdev);
51 int ret;
52
53 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
54
55 mc13xxx_lock(priv->mc13xxx);
56 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
57 mc13xxx_regulators[id].enable_bit, 0);
58 mc13xxx_unlock(priv->mc13xxx);
59
60 return ret;
61}
62
63static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev)
64{
65 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
66 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
67 int ret, id = rdev_get_id(rdev);
68 unsigned int val;
69
70 mc13xxx_lock(priv->mc13xxx);
71 ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
72 mc13xxx_unlock(priv->mc13xxx);
73
74 if (ret)
75 return ret;
76
77 return (val & mc13xxx_regulators[id].enable_bit) != 0;
78}
79
80int mc13xxx_regulator_list_voltage(struct regulator_dev *rdev,
81 unsigned selector)
82{
83 int id = rdev_get_id(rdev);
84 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
85 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
86
87 if (selector >= mc13xxx_regulators[id].desc.n_voltages)
88 return -EINVAL;
89
90 return mc13xxx_regulators[id].voltages[selector];
91}
92
93int mc13xxx_get_best_voltage_index(struct regulator_dev *rdev,
94 int min_uV, int max_uV)
95{
96 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
97 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
98 int reg_id = rdev_get_id(rdev);
99 int i;
100 int bestmatch;
101 int bestindex;
102
103 /*
104 * Locate the minimum voltage fitting the criteria on
105 * this regulator. The switchable voltages are not
106 * in strict falling order so we need to check them
107 * all for the best match.
108 */
109 bestmatch = INT_MAX;
110 bestindex = -1;
111 for (i = 0; i < mc13xxx_regulators[reg_id].desc.n_voltages; i++) {
112 if (mc13xxx_regulators[reg_id].voltages[i] >= min_uV &&
113 mc13xxx_regulators[reg_id].voltages[i] < bestmatch) {
114 bestmatch = mc13xxx_regulators[reg_id].voltages[i];
115 bestindex = i;
116 }
117 }
118
119 if (bestindex < 0 || bestmatch > max_uV) {
120 dev_warn(&rdev->dev, "no possible value for %d<=x<=%d uV\n",
121 min_uV, max_uV);
122 return -EINVAL;
123 }
124 return bestindex;
125}
126
127static int mc13xxx_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
128 int max_uV, unsigned *selector)
129{
130 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
131 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
132 int value, id = rdev_get_id(rdev);
133 int ret;
134
135 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
136 __func__, id, min_uV, max_uV);
137
138 /* Find the best index */
139 value = mc13xxx_get_best_voltage_index(rdev, min_uV, max_uV);
140 dev_dbg(rdev_get_dev(rdev), "%s best value: %d\n", __func__, value);
141 if (value < 0)
142 return value;
143
144 mc13xxx_lock(priv->mc13xxx);
145 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
146 mc13xxx_regulators[id].vsel_mask,
147 value << mc13xxx_regulators[id].vsel_shift);
148 mc13xxx_unlock(priv->mc13xxx);
149
150 return ret;
151}
152
153static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
154{
155 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
156 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
157 int ret, id = rdev_get_id(rdev);
158 unsigned int val;
159
160 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
161
162 mc13xxx_lock(priv->mc13xxx);
163 ret = mc13xxx_reg_read(priv->mc13xxx,
164 mc13xxx_regulators[id].vsel_reg, &val);
165 mc13xxx_unlock(priv->mc13xxx);
166
167 if (ret)
168 return ret;
169
170 val = (val & mc13xxx_regulators[id].vsel_mask)
171 >> mc13xxx_regulators[id].vsel_shift;
172
173 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
174
175 BUG_ON(val < 0 || val > mc13xxx_regulators[id].desc.n_voltages);
176
177 return mc13xxx_regulators[id].voltages[val];
178}
179
180struct regulator_ops mc13xxx_regulator_ops = {
181 .enable = mc13xxx_regulator_enable,
182 .disable = mc13xxx_regulator_disable,
183 .is_enabled = mc13xxx_regulator_is_enabled,
184 .list_voltage = mc13xxx_regulator_list_voltage,
185 .set_voltage = mc13xxx_regulator_set_voltage,
186 .get_voltage = mc13xxx_regulator_get_voltage,
187};
188
189int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
190 int max_uV, unsigned *selector)
191{
192 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
193 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
194 int id = rdev_get_id(rdev);
195
196 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
197 __func__, id, min_uV, max_uV);
198
199 if (min_uV >= mc13xxx_regulators[id].voltages[0] &&
200 max_uV <= mc13xxx_regulators[id].voltages[0])
201 return 0;
202 else
203 return -EINVAL;
204}
205
206int mc13xxx_fixed_regulator_get_voltage(struct regulator_dev *rdev)
207{
208 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
209 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
210 int id = rdev_get_id(rdev);
211
212 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
213
214 return mc13xxx_regulators[id].voltages[0];
215}
216
217struct regulator_ops mc13xxx_fixed_regulator_ops = {
218 .enable = mc13xxx_regulator_enable,
219 .disable = mc13xxx_regulator_disable,
220 .is_enabled = mc13xxx_regulator_is_enabled,
221 .list_voltage = mc13xxx_regulator_list_voltage,
222 .set_voltage = mc13xxx_fixed_regulator_set_voltage,
223 .get_voltage = mc13xxx_fixed_regulator_get_voltage,
224};
225
226int mc13xxx_sw_regulator_is_enabled(struct regulator_dev *rdev)
227{
228 return 1;
229}
230
231MODULE_LICENSE("GPL v2");
232MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
233MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
234MODULE_ALIAS("mc13xxx-regulator-core");
diff --git a/drivers/regulator/mc13xxx.h b/drivers/regulator/mc13xxx.h
new file mode 100644
index 000000000000..27758267e122
--- /dev/null
+++ b/drivers/regulator/mc13xxx.h
@@ -0,0 +1,101 @@
1/*
2 * mc13xxx.h - regulators for the Freescale mc13xxx PMIC
3 *
4 * Copyright (C) 2010 Yong Shen <yong.shen@linaro.org>
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 as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#ifndef __LINUX_REGULATOR_MC13XXX_H
13#define __LINUX_REGULATOR_MC13XXX_H
14
15#include <linux/regulator/driver.h>
16
17struct mc13xxx_regulator {
18 struct regulator_desc desc;
19 int reg;
20 int enable_bit;
21 int vsel_reg;
22 int vsel_shift;
23 int vsel_mask;
24 int hi_bit;
25 int const *voltages;
26};
27
28struct mc13xxx_regulator_priv {
29 struct mc13xxx *mc13xxx;
30 u32 powermisc_pwgt_state;
31 struct mc13xxx_regulator *mc13xxx_regulators;
32 struct regulator_dev *regulators[];
33};
34
35extern int mc13xxx_sw_regulator(struct regulator_dev *rdev);
36extern int mc13xxx_sw_regulator_is_enabled(struct regulator_dev *rdev);
37extern int mc13xxx_get_best_voltage_index(struct regulator_dev *rdev,
38 int min_uV, int max_uV);
39extern int mc13xxx_regulator_list_voltage(struct regulator_dev *rdev,
40 unsigned selector);
41extern int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev,
42 int min_uV, int max_uV, unsigned *selector);
43extern int mc13xxx_fixed_regulator_get_voltage(struct regulator_dev *rdev);
44
45extern struct regulator_ops mc13xxx_regulator_ops;
46extern struct regulator_ops mc13xxx_fixed_regulator_ops;
47
48#define MC13xxx_DEFINE(prefix, _name, _reg, _vsel_reg, _voltages, _ops) \
49 [prefix ## _name] = { \
50 .desc = { \
51 .name = #prefix "_" #_name, \
52 .n_voltages = ARRAY_SIZE(_voltages), \
53 .ops = &_ops, \
54 .type = REGULATOR_VOLTAGE, \
55 .id = prefix ## _name, \
56 .owner = THIS_MODULE, \
57 }, \
58 .reg = prefix ## _reg, \
59 .enable_bit = prefix ## _reg ## _ ## _name ## EN, \
60 .vsel_reg = prefix ## _vsel_reg, \
61 .vsel_shift = prefix ## _vsel_reg ## _ ## _name ## VSEL,\
62 .vsel_mask = prefix ## _vsel_reg ## _ ## _name ## VSEL_M,\
63 .voltages = _voltages, \
64 }
65
66#define MC13xxx_FIXED_DEFINE(prefix, _name, _reg, _voltages, _ops) \
67 [prefix ## _name] = { \
68 .desc = { \
69 .name = #prefix "_" #_name, \
70 .n_voltages = ARRAY_SIZE(_voltages), \
71 .ops = &_ops, \
72 .type = REGULATOR_VOLTAGE, \
73 .id = prefix ## _name, \
74 .owner = THIS_MODULE, \
75 }, \
76 .reg = prefix ## _reg, \
77 .enable_bit = prefix ## _reg ## _ ## _name ## EN, \
78 .voltages = _voltages, \
79 }
80
81#define MC13xxx_GPO_DEFINE(prefix, _name, _reg, _voltages, _ops) \
82 [prefix ## _name] = { \
83 .desc = { \
84 .name = #prefix "_" #_name, \
85 .n_voltages = ARRAY_SIZE(_voltages), \
86 .ops = &_ops, \
87 .type = REGULATOR_VOLTAGE, \
88 .id = prefix ## _name, \
89 .owner = THIS_MODULE, \
90 }, \
91 .reg = prefix ## _reg, \
92 .enable_bit = prefix ## _reg ## _ ## _name ## EN, \
93 .voltages = _voltages, \
94 }
95
96#define MC13xxx_DEFINE_SW(_name, _reg, _vsel_reg, _voltages, ops) \
97 MC13xxx_DEFINE(SW, _name, _reg, _vsel_reg, _voltages, ops)
98#define MC13xxx_DEFINE_REGU(_name, _reg, _vsel_reg, _voltages, ops) \
99 MC13xxx_DEFINE(REGU, _name, _reg, _vsel_reg, _voltages, ops)
100
101#endif