Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Yong Shen and committed by
Liam Girdwood
167e3d8a 57c78e35

+383 -282
+4
drivers/regulator/Kconfig
··· 186 186 This driver provides support for the voltage regulators of the 187 187 PCAP2 PMIC. 188 188 189 + config REGULATOR_MC13XXX_CORE 190 + bool 191 + 189 192 config REGULATOR_MC13783 190 193 tristate "Support regulators on Freescale MC13783 PMIC" 191 194 depends on MFD_MC13783 195 + select REGULATOR_MC13XXX_CORE 192 196 help 193 197 Say y here to support the regulators found on the Freescale MC13783 194 198 PMIC.
+1
drivers/regulator/Makefile
··· 30 30 obj-$(CONFIG_REGULATOR_PCF50633) += pcf50633-regulator.o 31 31 obj-$(CONFIG_REGULATOR_PCAP) += pcap-regulator.o 32 32 obj-$(CONFIG_REGULATOR_MC13783) += mc13783-regulator.o 33 + obj-$(CONFIG_REGULATOR_MC13XXX_CORE) += mc13xxx-regulator-core.o 33 34 obj-$(CONFIG_REGULATOR_AB3100) += ab3100.o 34 35 35 36 obj-$(CONFIG_REGULATOR_TPS65023) += tps65023-regulator.o
+43 -282
drivers/regulator/mc13783-regulator.c
··· 1 1 /* 2 2 * Regulator Driver for Freescale MC13783 PMIC 3 3 * 4 + * Copyright 2010 Yong Shen <yong.shen@linaro.org> 4 5 * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> 5 6 * Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com> 6 7 * ··· 18 17 #include <linux/slab.h> 19 18 #include <linux/init.h> 20 19 #include <linux/err.h> 20 + #include "mc13xxx.h" 21 21 22 22 #define MC13783_REG_SWITCHERS5 29 23 23 #define MC13783_REG_SWITCHERS5_SW3EN (1 << 20) ··· 90 88 91 89 #define MC13783_REG_POWERMISC_PWGTSPI_M (3 << 15) 92 90 93 - 94 - struct mc13783_regulator { 95 - struct regulator_desc desc; 96 - int reg; 97 - int enable_bit; 98 - int vsel_reg; 99 - int vsel_shift; 100 - int vsel_mask; 101 - int const *voltages; 102 - }; 103 91 104 92 /* Voltage Values */ 105 93 static const int mc13783_sw3_val[] = { ··· 167 175 5500000, 168 176 }; 169 177 170 - static struct regulator_ops mc13783_regulator_ops; 171 - static struct regulator_ops mc13783_fixed_regulator_ops; 172 178 static struct regulator_ops mc13783_gpo_regulator_ops; 173 179 174 - #define MC13783_DEFINE(prefix, _name, _reg, _vsel_reg, _voltages) \ 175 - [MC13783_ ## prefix ## _ ## _name] = { \ 176 - .desc = { \ 177 - .name = #prefix "_" #_name, \ 178 - .n_voltages = ARRAY_SIZE(_voltages), \ 179 - .ops = &mc13783_regulator_ops, \ 180 - .type = REGULATOR_VOLTAGE, \ 181 - .id = MC13783_ ## prefix ## _ ## _name, \ 182 - .owner = THIS_MODULE, \ 183 - }, \ 184 - .reg = MC13783_REG_ ## _reg, \ 185 - .enable_bit = MC13783_REG_ ## _reg ## _ ## _name ## EN, \ 186 - .vsel_reg = MC13783_REG_ ## _vsel_reg, \ 187 - .vsel_shift = MC13783_REG_ ## _vsel_reg ## _ ## _name ## VSEL,\ 188 - .vsel_mask = MC13783_REG_ ## _vsel_reg ## _ ## _name ## VSEL_M,\ 189 - .voltages = _voltages, \ 190 - } 180 + #define MC13783_DEFINE(prefix, name, reg, vsel_reg, voltages) \ 181 + MC13xxx_DEFINE(MC13783_REG_, name, reg, vsel_reg, voltages, \ 182 + mc13xxx_regulator_ops) 191 183 192 - #define MC13783_FIXED_DEFINE(prefix, _name, _reg, _voltages) \ 193 - [MC13783_ ## prefix ## _ ## _name] = { \ 194 - .desc = { \ 195 - .name = #prefix "_" #_name, \ 196 - .n_voltages = ARRAY_SIZE(_voltages), \ 197 - .ops = &mc13783_fixed_regulator_ops, \ 198 - .type = REGULATOR_VOLTAGE, \ 199 - .id = MC13783_ ## prefix ## _ ## _name, \ 200 - .owner = THIS_MODULE, \ 201 - }, \ 202 - .reg = MC13783_REG_ ## _reg, \ 203 - .enable_bit = MC13783_REG_ ## _reg ## _ ## _name ## EN, \ 204 - .voltages = _voltages, \ 205 - } 184 + #define MC13783_FIXED_DEFINE(prefix, name, reg, voltages) \ 185 + MC13xxx_FIXED_DEFINE(MC13783_REG_, name, reg, voltages, \ 186 + mc13xxx_fixed_regulator_ops) 206 187 207 - #define MC13783_GPO_DEFINE(prefix, _name, _reg, _voltages) \ 208 - [MC13783_ ## prefix ## _ ## _name] = { \ 209 - .desc = { \ 210 - .name = #prefix "_" #_name, \ 211 - .n_voltages = ARRAY_SIZE(_voltages), \ 212 - .ops = &mc13783_gpo_regulator_ops, \ 213 - .type = REGULATOR_VOLTAGE, \ 214 - .id = MC13783_ ## prefix ## _ ## _name, \ 215 - .owner = THIS_MODULE, \ 216 - }, \ 217 - .reg = MC13783_REG_ ## _reg, \ 218 - .enable_bit = MC13783_REG_ ## _reg ## _ ## _name ## EN, \ 219 - .voltages = _voltages, \ 220 - } 188 + #define MC13783_GPO_DEFINE(prefix, name, reg, voltages) \ 189 + MC13xxx_GPO_DEFINE(MC13783_REG_, name, reg, voltages, \ 190 + mc13783_gpo_regulator_ops) 221 191 222 192 #define MC13783_DEFINE_SW(_name, _reg, _vsel_reg, _voltages) \ 223 193 MC13783_DEFINE(REG, _name, _reg, _vsel_reg, _voltages) 224 194 #define MC13783_DEFINE_REGU(_name, _reg, _vsel_reg, _voltages) \ 225 195 MC13783_DEFINE(REG, _name, _reg, _vsel_reg, _voltages) 226 196 227 - static struct mc13783_regulator mc13783_regulators[] = { 197 + static struct mc13xxx_regulator mc13783_regulators[] = { 228 198 MC13783_DEFINE_SW(SW3, SWITCHERS5, SWITCHERS5, mc13783_sw3_val), 229 199 230 200 MC13783_FIXED_DEFINE(REG, VAUDIO, REGULATORMODE0, mc13783_vaudio_val), ··· 228 274 MC13783_GPO_DEFINE(REG, PWGT2SPI, POWERMISC, mc13783_pwgtdrv_val), 229 275 }; 230 276 231 - struct mc13783_regulator_priv { 232 - struct mc13783 *mc13783; 233 - u32 powermisc_pwgt_state; 234 - struct regulator_dev *regulators[]; 235 - }; 236 - 237 - static int mc13783_regulator_enable(struct regulator_dev *rdev) 277 + static int mc13783_powermisc_rmw(struct mc13xxx_regulator_priv *priv, u32 mask, 278 + u32 val) 238 279 { 239 - struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); 240 - int id = rdev_get_id(rdev); 241 - int ret; 242 - 243 - dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); 244 - 245 - mc13783_lock(priv->mc13783); 246 - ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg, 247 - mc13783_regulators[id].enable_bit, 248 - mc13783_regulators[id].enable_bit); 249 - mc13783_unlock(priv->mc13783); 250 - 251 - return ret; 252 - } 253 - 254 - static int mc13783_regulator_disable(struct regulator_dev *rdev) 255 - { 256 - struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); 257 - int id = rdev_get_id(rdev); 258 - int ret; 259 - 260 - dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); 261 - 262 - mc13783_lock(priv->mc13783); 263 - ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg, 264 - mc13783_regulators[id].enable_bit, 0); 265 - mc13783_unlock(priv->mc13783); 266 - 267 - return ret; 268 - } 269 - 270 - static int mc13783_regulator_is_enabled(struct regulator_dev *rdev) 271 - { 272 - struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); 273 - int ret, id = rdev_get_id(rdev); 274 - unsigned int val; 275 - 276 - mc13783_lock(priv->mc13783); 277 - ret = mc13783_reg_read(priv->mc13783, mc13783_regulators[id].reg, &val); 278 - mc13783_unlock(priv->mc13783); 279 - 280 - if (ret) 281 - return ret; 282 - 283 - return (val & mc13783_regulators[id].enable_bit) != 0; 284 - } 285 - 286 - static int mc13783_regulator_list_voltage(struct regulator_dev *rdev, 287 - unsigned selector) 288 - { 289 - int id = rdev_get_id(rdev); 290 - 291 - if (selector >= mc13783_regulators[id].desc.n_voltages) 292 - return -EINVAL; 293 - 294 - return mc13783_regulators[id].voltages[selector]; 295 - } 296 - 297 - static int mc13783_get_best_voltage_index(struct regulator_dev *rdev, 298 - int min_uV, int max_uV) 299 - { 300 - int reg_id = rdev_get_id(rdev); 301 - int i; 302 - int bestmatch; 303 - int bestindex; 304 - 305 - /* 306 - * Locate the minimum voltage fitting the criteria on 307 - * this regulator. The switchable voltages are not 308 - * in strict falling order so we need to check them 309 - * all for the best match. 310 - */ 311 - bestmatch = INT_MAX; 312 - bestindex = -1; 313 - for (i = 0; i < mc13783_regulators[reg_id].desc.n_voltages; i++) { 314 - if (mc13783_regulators[reg_id].voltages[i] >= min_uV && 315 - mc13783_regulators[reg_id].voltages[i] < bestmatch) { 316 - bestmatch = mc13783_regulators[reg_id].voltages[i]; 317 - bestindex = i; 318 - } 319 - } 320 - 321 - if (bestindex < 0 || bestmatch > max_uV) { 322 - dev_warn(&rdev->dev, "no possible value for %d<=x<=%d uV\n", 323 - min_uV, max_uV); 324 - return -EINVAL; 325 - } 326 - return bestindex; 327 - } 328 - 329 - static int mc13783_regulator_set_voltage(struct regulator_dev *rdev, 330 - int min_uV, int max_uV, 331 - unsigned *selector) 332 - { 333 - struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); 334 - int value, id = rdev_get_id(rdev); 335 - int ret; 336 - 337 - dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n", 338 - __func__, id, min_uV, max_uV); 339 - 340 - /* Find the best index */ 341 - value = mc13783_get_best_voltage_index(rdev, min_uV, max_uV); 342 - dev_dbg(rdev_get_dev(rdev), "%s best value: %d \n", __func__, value); 343 - if (value < 0) 344 - return value; 345 - 346 - *selector = value; 347 - 348 - mc13783_lock(priv->mc13783); 349 - ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].vsel_reg, 350 - mc13783_regulators[id].vsel_mask, 351 - value << mc13783_regulators[id].vsel_shift); 352 - mc13783_unlock(priv->mc13783); 353 - 354 - return ret; 355 - } 356 - 357 - static int mc13783_regulator_get_voltage(struct regulator_dev *rdev) 358 - { 359 - struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); 360 - int ret, id = rdev_get_id(rdev); 361 - unsigned int val; 362 - 363 - dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); 364 - 365 - mc13783_lock(priv->mc13783); 366 - ret = mc13783_reg_read(priv->mc13783, 367 - mc13783_regulators[id].vsel_reg, &val); 368 - mc13783_unlock(priv->mc13783); 369 - 370 - if (ret) 371 - return ret; 372 - 373 - val = (val & mc13783_regulators[id].vsel_mask) 374 - >> mc13783_regulators[id].vsel_shift; 375 - 376 - dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val); 377 - 378 - BUG_ON(val < 0 || val > mc13783_regulators[id].desc.n_voltages); 379 - 380 - return mc13783_regulators[id].voltages[val]; 381 - } 382 - 383 - static struct regulator_ops mc13783_regulator_ops = { 384 - .enable = mc13783_regulator_enable, 385 - .disable = mc13783_regulator_disable, 386 - .is_enabled = mc13783_regulator_is_enabled, 387 - .list_voltage = mc13783_regulator_list_voltage, 388 - .set_voltage = mc13783_regulator_set_voltage, 389 - .get_voltage = mc13783_regulator_get_voltage, 390 - }; 391 - 392 - static int mc13783_fixed_regulator_set_voltage(struct regulator_dev *rdev, 393 - int min_uV, int max_uV, 394 - unsigned int *selector) 395 - { 396 - int id = rdev_get_id(rdev); 397 - 398 - dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n", 399 - __func__, id, min_uV, max_uV); 400 - 401 - *selector = 0; 402 - 403 - if (min_uV >= mc13783_regulators[id].voltages[0] && 404 - max_uV <= mc13783_regulators[id].voltages[0]) 405 - return 0; 406 - else 407 - return -EINVAL; 408 - } 409 - 410 - static int mc13783_fixed_regulator_get_voltage(struct regulator_dev *rdev) 411 - { 412 - int id = rdev_get_id(rdev); 413 - 414 - dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); 415 - 416 - return mc13783_regulators[id].voltages[0]; 417 - } 418 - 419 - static struct regulator_ops mc13783_fixed_regulator_ops = { 420 - .enable = mc13783_regulator_enable, 421 - .disable = mc13783_regulator_disable, 422 - .is_enabled = mc13783_regulator_is_enabled, 423 - .list_voltage = mc13783_regulator_list_voltage, 424 - .set_voltage = mc13783_fixed_regulator_set_voltage, 425 - .get_voltage = mc13783_fixed_regulator_get_voltage, 426 - }; 427 - 428 - static int mc13783_powermisc_rmw(struct mc13783_regulator_priv *priv, u32 mask, 429 - u32 val) 430 - { 431 - struct mc13783 *mc13783 = priv->mc13783; 280 + struct mc13xxx *mc13783 = priv->mc13xxx; 432 281 int ret; 433 282 u32 valread; 434 283 435 284 BUG_ON(val & ~mask); 436 285 437 - ret = mc13783_reg_read(mc13783, MC13783_REG_POWERMISC, &valread); 286 + ret = mc13xxx_reg_read(mc13783, MC13783_REG_POWERMISC, &valread); 438 287 if (ret) 439 288 return ret; 440 289 ··· 252 495 valread = (valread & ~MC13783_REG_POWERMISC_PWGTSPI_M) | 253 496 priv->powermisc_pwgt_state; 254 497 255 - return mc13783_reg_write(mc13783, MC13783_REG_POWERMISC, valread); 498 + return mc13xxx_reg_write(mc13783, MC13783_REG_POWERMISC, valread); 256 499 } 257 500 258 501 static int mc13783_gpo_regulator_enable(struct regulator_dev *rdev) 259 502 { 260 - struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); 503 + struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); 504 + struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; 261 505 int id = rdev_get_id(rdev); 262 506 int ret; 263 - u32 en_val = mc13783_regulators[id].enable_bit; 507 + u32 en_val = mc13xxx_regulators[id].enable_bit; 264 508 265 509 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); 266 510 ··· 270 512 id == MC13783_REG_PWGT2SPI) 271 513 en_val = 0; 272 514 273 - mc13783_lock(priv->mc13783); 274 - ret = mc13783_powermisc_rmw(priv, mc13783_regulators[id].enable_bit, 515 + mc13xxx_lock(priv->mc13xxx); 516 + ret = mc13783_powermisc_rmw(priv, mc13xxx_regulators[id].enable_bit, 275 517 en_val); 276 - mc13783_unlock(priv->mc13783); 518 + mc13xxx_unlock(priv->mc13xxx); 277 519 278 520 return ret; 279 521 } 280 522 281 523 static int mc13783_gpo_regulator_disable(struct regulator_dev *rdev) 282 524 { 283 - struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); 525 + struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); 526 + struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; 284 527 int id = rdev_get_id(rdev); 285 528 int ret; 286 529 u32 dis_val = 0; ··· 291 532 /* Power Gate disable value is 1 */ 292 533 if (id == MC13783_REG_PWGT1SPI || 293 534 id == MC13783_REG_PWGT2SPI) 294 - dis_val = mc13783_regulators[id].enable_bit; 535 + dis_val = mc13xxx_regulators[id].enable_bit; 295 536 296 - mc13783_lock(priv->mc13783); 297 - ret = mc13783_powermisc_rmw(priv, mc13783_regulators[id].enable_bit, 537 + mc13xxx_lock(priv->mc13xxx); 538 + ret = mc13783_powermisc_rmw(priv, mc13xxx_regulators[id].enable_bit, 298 539 dis_val); 299 - mc13783_unlock(priv->mc13783); 540 + mc13xxx_unlock(priv->mc13xxx); 300 541 301 542 return ret; 302 543 } 303 544 304 545 static int mc13783_gpo_regulator_is_enabled(struct regulator_dev *rdev) 305 546 { 306 - struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); 547 + struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); 548 + struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; 307 549 int ret, id = rdev_get_id(rdev); 308 550 unsigned int val; 309 551 310 - mc13783_lock(priv->mc13783); 311 - ret = mc13783_reg_read(priv->mc13783, mc13783_regulators[id].reg, &val); 312 - mc13783_unlock(priv->mc13783); 552 + mc13xxx_lock(priv->mc13xxx); 553 + ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val); 554 + mc13xxx_unlock(priv->mc13xxx); 313 555 314 556 if (ret) 315 557 return ret; ··· 320 560 val = (val & ~MC13783_REG_POWERMISC_PWGTSPI_M) | 321 561 (priv->powermisc_pwgt_state ^ MC13783_REG_POWERMISC_PWGTSPI_M); 322 562 323 - return (val & mc13783_regulators[id].enable_bit) != 0; 563 + return (val & mc13xxx_regulators[id].enable_bit) != 0; 324 564 } 325 565 326 566 static struct regulator_ops mc13783_gpo_regulator_ops = { 327 567 .enable = mc13783_gpo_regulator_enable, 328 568 .disable = mc13783_gpo_regulator_disable, 329 569 .is_enabled = mc13783_gpo_regulator_is_enabled, 330 - .list_voltage = mc13783_regulator_list_voltage, 331 - .set_voltage = mc13783_fixed_regulator_set_voltage, 332 - .get_voltage = mc13783_fixed_regulator_get_voltage, 570 + .list_voltage = mc13xxx_regulator_list_voltage, 571 + .set_voltage = mc13xxx_fixed_regulator_set_voltage, 572 + .get_voltage = mc13xxx_fixed_regulator_get_voltage, 333 573 }; 334 574 335 575 static int __devinit mc13783_regulator_probe(struct platform_device *pdev) 336 576 { 337 - struct mc13783_regulator_priv *priv; 338 - struct mc13783 *mc13783 = dev_get_drvdata(pdev->dev.parent); 577 + struct mc13xxx_regulator_priv *priv; 578 + struct mc13xxx *mc13783 = dev_get_drvdata(pdev->dev.parent); 339 579 struct mc13783_regulator_platform_data *pdata = 340 580 dev_get_platdata(&pdev->dev); 341 581 struct mc13783_regulator_init_data *init_data; ··· 349 589 if (!priv) 350 590 return -ENOMEM; 351 591 352 - priv->mc13783 = mc13783; 592 + priv->mc13xxx_regulators = mc13783_regulators; 593 + priv->mc13xxx = mc13783; 353 594 354 595 for (i = 0; i < pdata->num_regulators; i++) { 355 596 init_data = &pdata->regulators[i]; ··· 380 619 381 620 static int __devexit mc13783_regulator_remove(struct platform_device *pdev) 382 621 { 383 - struct mc13783_regulator_priv *priv = platform_get_drvdata(pdev); 622 + struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev); 384 623 struct mc13783_regulator_platform_data *pdata = 385 624 dev_get_platdata(&pdev->dev); 386 625 int i;
+234
drivers/regulator/mc13xxx-regulator-core.c
··· 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 + 28 + static 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 + 46 + static 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 + 63 + static 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 + 80 + int 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 + 93 + int 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 + 127 + static 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 + 153 + static 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 + 180 + struct 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 + 189 + int 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 + 206 + int 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 + 217 + struct 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 + 226 + int mc13xxx_sw_regulator_is_enabled(struct regulator_dev *rdev) 227 + { 228 + return 1; 229 + } 230 + 231 + MODULE_LICENSE("GPL v2"); 232 + MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>"); 233 + MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC"); 234 + MODULE_ALIAS("mc13xxx-regulator-core");
+101
drivers/regulator/mc13xxx.h
··· 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 + 17 + struct 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 + 28 + struct 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 + 35 + extern int mc13xxx_sw_regulator(struct regulator_dev *rdev); 36 + extern int mc13xxx_sw_regulator_is_enabled(struct regulator_dev *rdev); 37 + extern int mc13xxx_get_best_voltage_index(struct regulator_dev *rdev, 38 + int min_uV, int max_uV); 39 + extern int mc13xxx_regulator_list_voltage(struct regulator_dev *rdev, 40 + unsigned selector); 41 + extern int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, 42 + int min_uV, int max_uV, unsigned *selector); 43 + extern int mc13xxx_fixed_regulator_get_voltage(struct regulator_dev *rdev); 44 + 45 + extern struct regulator_ops mc13xxx_regulator_ops; 46 + extern 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