aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorLiam Girdwood <lrg@slimlogic.co.uk>2008-10-10 08:22:20 -0400
committerLiam Girdwood <lrg@slimlogic.co.uk>2008-10-13 16:51:50 -0400
commita5766f11cfd3a0c03450d99c8fe548c2940be884 (patch)
treec511dd532fb20329d59c47b0f24b3ba587698319 /drivers
parenta447c0932445f92ce6f4c1bd020f62c5097a7842 (diff)
regulator: core - Rework machine API to remove string based functions.
This improves the machine level API in order to configure regulator constraints and consumers as platform data and removes the old string based API that required several calls to set up each regulator. The intention is to create a struct regulator_init_data, populate it's fields with constraints, consumers devices, etc and then register the regulator device from board.c in the standard Linux way. e.g. regulator LDO2 (supplying codec and sim) platform data. /* regulator LDO2 consumer devices */ static struct regulator_consumer_supply ldo2_consumers[] = { { .dev = &platform_audio_device.dev, .supply = "codec_avdd", }, { .dev = &platform_sim_device.dev, .supply = "sim_vcc", } }; /* regulator LDO2 constraints */ static struct regulator_init_data ldo2_data = { .constraints = { .min_uV = 3300000, .max_uV = 3300000, .valid_modes_mask = REGULATOR_MODE_NORMAL, .apply_uV = 1, }, .num_consumer_supplies = ARRAY_SIZE(ldo2_consumers), .consumer_supplies = ldo2_consumers, }; /* machine regulator devices with thier consumers and constraints */ static struct platform_device wm8350_regulator_devices[] = { { .name = "wm8350-regulator", .id = WM8350_LDO_2, .dev = { .platform_data = &ldo2_data, }, }, }; Changes in detail:- o Removed all const char* regulator config functions in machine API. o Created new struct regulator_init_data to contain regulator machine configuration constraints and consmuers. o Changed set_supply(), set_machine_constraints(), set_consumer_device_supply() to remove their string identifier parameters. Also made them static and moved functions nearer top of core.c. o Removed no longer used inline func to_rdev() o Added regulator_get_init_drvdata() to retrieve init data. o Added struct device* as parameter to regulator_register(). o Changed my email address. Signed-off-by: Eric Miao <eric.miao@marvell.com> Signed-off-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/bq24022.c21
-rw-r--r--drivers/regulator/core.c457
2 files changed, 235 insertions, 243 deletions
diff --git a/drivers/regulator/bq24022.c b/drivers/regulator/bq24022.c
index 263699d6152d..366565aba865 100644
--- a/drivers/regulator/bq24022.c
+++ b/drivers/regulator/bq24022.c
@@ -18,13 +18,13 @@
18#include <linux/regulator/bq24022.h> 18#include <linux/regulator/bq24022.h>
19#include <linux/regulator/driver.h> 19#include <linux/regulator/driver.h>
20 20
21
21static int bq24022_set_current_limit(struct regulator_dev *rdev, 22static int bq24022_set_current_limit(struct regulator_dev *rdev,
22 int min_uA, int max_uA) 23 int min_uA, int max_uA)
23{ 24{
24 struct platform_device *pdev = rdev_get_drvdata(rdev); 25 struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev);
25 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
26 26
27 dev_dbg(&pdev->dev, "setting current limit to %s mA\n", 27 dev_dbg(rdev_get_dev(rdev), "setting current limit to %s mA\n",
28 max_uA >= 500000 ? "500" : "100"); 28 max_uA >= 500000 ? "500" : "100");
29 29
30 /* REVISIT: maybe return error if min_uA != 0 ? */ 30 /* REVISIT: maybe return error if min_uA != 0 ? */
@@ -34,18 +34,16 @@ static int bq24022_set_current_limit(struct regulator_dev *rdev,
34 34
35static int bq24022_get_current_limit(struct regulator_dev *rdev) 35static int bq24022_get_current_limit(struct regulator_dev *rdev)
36{ 36{
37 struct platform_device *pdev = rdev_get_drvdata(rdev); 37 struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev);
38 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
39 38
40 return gpio_get_value(pdata->gpio_iset2) ? 500000 : 100000; 39 return gpio_get_value(pdata->gpio_iset2) ? 500000 : 100000;
41} 40}
42 41
43static int bq24022_enable(struct regulator_dev *rdev) 42static int bq24022_enable(struct regulator_dev *rdev)
44{ 43{
45 struct platform_device *pdev = rdev_get_drvdata(rdev); 44 struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev);
46 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
47 45
48 dev_dbg(&pdev->dev, "enabling charger\n"); 46 dev_dbg(rdev_get_dev(rdev), "enabling charger\n");
49 47
50 gpio_set_value(pdata->gpio_nce, 0); 48 gpio_set_value(pdata->gpio_nce, 0);
51 return 0; 49 return 0;
@@ -53,10 +51,9 @@ static int bq24022_enable(struct regulator_dev *rdev)
53 51
54static int bq24022_disable(struct regulator_dev *rdev) 52static int bq24022_disable(struct regulator_dev *rdev)
55{ 53{
56 struct platform_device *pdev = rdev_get_drvdata(rdev); 54 struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev);
57 struct bq24022_mach_info *pdata = pdev->dev.platform_data;
58 55
59 dev_dbg(&pdev->dev, "disabling charger\n"); 56 dev_dbg(rdev_get_dev(rdev), "disabling charger\n");
60 57
61 gpio_set_value(pdata->gpio_nce, 1); 58 gpio_set_value(pdata->gpio_nce, 1);
62 return 0; 59 return 0;
@@ -108,7 +105,7 @@ static int __init bq24022_probe(struct platform_device *pdev)
108 ret = gpio_direction_output(pdata->gpio_iset2, 0); 105 ret = gpio_direction_output(pdata->gpio_iset2, 0);
109 ret = gpio_direction_output(pdata->gpio_nce, 1); 106 ret = gpio_direction_output(pdata->gpio_nce, 1);
110 107
111 bq24022 = regulator_register(&bq24022_desc, pdev); 108 bq24022 = regulator_register(&bq24022_desc, &pdev->dev, pdata);
112 if (IS_ERR(bq24022)) { 109 if (IS_ERR(bq24022)) {
113 dev_dbg(&pdev->dev, "couldn't register regulator\n"); 110 dev_dbg(&pdev->dev, "couldn't register regulator\n");
114 ret = PTR_ERR(bq24022); 111 ret = PTR_ERR(bq24022);
diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 9c7986261568..84202eaace57 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -2,8 +2,9 @@
2 * core.c -- Voltage/Current Regulator framework. 2 * core.c -- Voltage/Current Regulator framework.
3 * 3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC. 4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
5 * 6 *
6 * Author: Liam Girdwood <liam.girdwood@wolfsonmicro.com> 7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
7 * 8 *
8 * This program is free software; you can redistribute it and/or modify it 9 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the 10 * under the terms of the GNU General Public License as published by the
@@ -64,14 +65,9 @@ struct regulator_map {
64 struct list_head list; 65 struct list_head list;
65 struct device *dev; 66 struct device *dev;
66 const char *supply; 67 const char *supply;
67 const char *regulator; 68 struct regulator_dev *regulator;
68}; 69};
69 70
70static inline struct regulator_dev *to_rdev(struct device *d)
71{
72 return container_of(d, struct regulator_dev, dev);
73}
74
75/* 71/*
76 * struct regulator 72 * struct regulator
77 * 73 *
@@ -227,7 +223,7 @@ static ssize_t device_requested_uA_show(struct device *dev,
227static ssize_t regulator_uV_show(struct device *dev, 223static ssize_t regulator_uV_show(struct device *dev,
228 struct device_attribute *attr, char *buf) 224 struct device_attribute *attr, char *buf)
229{ 225{
230 struct regulator_dev *rdev = to_rdev(dev); 226 struct regulator_dev *rdev = dev_get_drvdata(dev);
231 ssize_t ret; 227 ssize_t ret;
232 228
233 mutex_lock(&rdev->mutex); 229 mutex_lock(&rdev->mutex);
@@ -240,7 +236,7 @@ static ssize_t regulator_uV_show(struct device *dev,
240static ssize_t regulator_uA_show(struct device *dev, 236static ssize_t regulator_uA_show(struct device *dev,
241 struct device_attribute *attr, char *buf) 237 struct device_attribute *attr, char *buf)
242{ 238{
243 struct regulator_dev *rdev = to_rdev(dev); 239 struct regulator_dev *rdev = dev_get_drvdata(dev);
244 240
245 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); 241 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
246} 242}
@@ -248,7 +244,7 @@ static ssize_t regulator_uA_show(struct device *dev,
248static ssize_t regulator_opmode_show(struct device *dev, 244static ssize_t regulator_opmode_show(struct device *dev,
249 struct device_attribute *attr, char *buf) 245 struct device_attribute *attr, char *buf)
250{ 246{
251 struct regulator_dev *rdev = to_rdev(dev); 247 struct regulator_dev *rdev = dev_get_drvdata(dev);
252 int mode = _regulator_get_mode(rdev); 248 int mode = _regulator_get_mode(rdev);
253 249
254 switch (mode) { 250 switch (mode) {
@@ -267,7 +263,7 @@ static ssize_t regulator_opmode_show(struct device *dev,
267static ssize_t regulator_state_show(struct device *dev, 263static ssize_t regulator_state_show(struct device *dev,
268 struct device_attribute *attr, char *buf) 264 struct device_attribute *attr, char *buf)
269{ 265{
270 struct regulator_dev *rdev = to_rdev(dev); 266 struct regulator_dev *rdev = dev_get_drvdata(dev);
271 int state = _regulator_is_enabled(rdev); 267 int state = _regulator_is_enabled(rdev);
272 268
273 if (state > 0) 269 if (state > 0)
@@ -281,7 +277,7 @@ static ssize_t regulator_state_show(struct device *dev,
281static ssize_t regulator_min_uA_show(struct device *dev, 277static ssize_t regulator_min_uA_show(struct device *dev,
282 struct device_attribute *attr, char *buf) 278 struct device_attribute *attr, char *buf)
283{ 279{
284 struct regulator_dev *rdev = to_rdev(dev); 280 struct regulator_dev *rdev = dev_get_drvdata(dev);
285 281
286 if (!rdev->constraints) 282 if (!rdev->constraints)
287 return sprintf(buf, "constraint not defined\n"); 283 return sprintf(buf, "constraint not defined\n");
@@ -292,7 +288,7 @@ static ssize_t regulator_min_uA_show(struct device *dev,
292static ssize_t regulator_max_uA_show(struct device *dev, 288static ssize_t regulator_max_uA_show(struct device *dev,
293 struct device_attribute *attr, char *buf) 289 struct device_attribute *attr, char *buf)
294{ 290{
295 struct regulator_dev *rdev = to_rdev(dev); 291 struct regulator_dev *rdev = dev_get_drvdata(dev);
296 292
297 if (!rdev->constraints) 293 if (!rdev->constraints)
298 return sprintf(buf, "constraint not defined\n"); 294 return sprintf(buf, "constraint not defined\n");
@@ -303,7 +299,7 @@ static ssize_t regulator_max_uA_show(struct device *dev,
303static ssize_t regulator_min_uV_show(struct device *dev, 299static ssize_t regulator_min_uV_show(struct device *dev,
304 struct device_attribute *attr, char *buf) 300 struct device_attribute *attr, char *buf)
305{ 301{
306 struct regulator_dev *rdev = to_rdev(dev); 302 struct regulator_dev *rdev = dev_get_drvdata(dev);
307 303
308 if (!rdev->constraints) 304 if (!rdev->constraints)
309 return sprintf(buf, "constraint not defined\n"); 305 return sprintf(buf, "constraint not defined\n");
@@ -314,7 +310,7 @@ static ssize_t regulator_min_uV_show(struct device *dev,
314static ssize_t regulator_max_uV_show(struct device *dev, 310static ssize_t regulator_max_uV_show(struct device *dev,
315 struct device_attribute *attr, char *buf) 311 struct device_attribute *attr, char *buf)
316{ 312{
317 struct regulator_dev *rdev = to_rdev(dev); 313 struct regulator_dev *rdev = dev_get_drvdata(dev);
318 314
319 if (!rdev->constraints) 315 if (!rdev->constraints)
320 return sprintf(buf, "constraint not defined\n"); 316 return sprintf(buf, "constraint not defined\n");
@@ -325,7 +321,7 @@ static ssize_t regulator_max_uV_show(struct device *dev,
325static ssize_t regulator_total_uA_show(struct device *dev, 321static ssize_t regulator_total_uA_show(struct device *dev,
326 struct device_attribute *attr, char *buf) 322 struct device_attribute *attr, char *buf)
327{ 323{
328 struct regulator_dev *rdev = to_rdev(dev); 324 struct regulator_dev *rdev = dev_get_drvdata(dev);
329 struct regulator *regulator; 325 struct regulator *regulator;
330 int uA = 0; 326 int uA = 0;
331 327
@@ -339,14 +335,14 @@ static ssize_t regulator_total_uA_show(struct device *dev,
339static ssize_t regulator_num_users_show(struct device *dev, 335static ssize_t regulator_num_users_show(struct device *dev,
340 struct device_attribute *attr, char *buf) 336 struct device_attribute *attr, char *buf)
341{ 337{
342 struct regulator_dev *rdev = to_rdev(dev); 338 struct regulator_dev *rdev = dev_get_drvdata(dev);
343 return sprintf(buf, "%d\n", rdev->use_count); 339 return sprintf(buf, "%d\n", rdev->use_count);
344} 340}
345 341
346static ssize_t regulator_type_show(struct device *dev, 342static ssize_t regulator_type_show(struct device *dev,
347 struct device_attribute *attr, char *buf) 343 struct device_attribute *attr, char *buf)
348{ 344{
349 struct regulator_dev *rdev = to_rdev(dev); 345 struct regulator_dev *rdev = dev_get_drvdata(dev);
350 346
351 switch (rdev->desc->type) { 347 switch (rdev->desc->type) {
352 case REGULATOR_VOLTAGE: 348 case REGULATOR_VOLTAGE:
@@ -360,7 +356,7 @@ static ssize_t regulator_type_show(struct device *dev,
360static ssize_t regulator_suspend_mem_uV_show(struct device *dev, 356static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
361 struct device_attribute *attr, char *buf) 357 struct device_attribute *attr, char *buf)
362{ 358{
363 struct regulator_dev *rdev = to_rdev(dev); 359 struct regulator_dev *rdev = dev_get_drvdata(dev);
364 360
365 if (!rdev->constraints) 361 if (!rdev->constraints)
366 return sprintf(buf, "not defined\n"); 362 return sprintf(buf, "not defined\n");
@@ -370,7 +366,7 @@ static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
370static ssize_t regulator_suspend_disk_uV_show(struct device *dev, 366static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
371 struct device_attribute *attr, char *buf) 367 struct device_attribute *attr, char *buf)
372{ 368{
373 struct regulator_dev *rdev = to_rdev(dev); 369 struct regulator_dev *rdev = dev_get_drvdata(dev);
374 370
375 if (!rdev->constraints) 371 if (!rdev->constraints)
376 return sprintf(buf, "not defined\n"); 372 return sprintf(buf, "not defined\n");
@@ -380,7 +376,7 @@ static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
380static ssize_t regulator_suspend_standby_uV_show(struct device *dev, 376static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
381 struct device_attribute *attr, char *buf) 377 struct device_attribute *attr, char *buf)
382{ 378{
383 struct regulator_dev *rdev = to_rdev(dev); 379 struct regulator_dev *rdev = dev_get_drvdata(dev);
384 380
385 if (!rdev->constraints) 381 if (!rdev->constraints)
386 return sprintf(buf, "not defined\n"); 382 return sprintf(buf, "not defined\n");
@@ -406,7 +402,7 @@ static ssize_t suspend_opmode_show(struct regulator_dev *rdev,
406static ssize_t regulator_suspend_mem_mode_show(struct device *dev, 402static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
407 struct device_attribute *attr, char *buf) 403 struct device_attribute *attr, char *buf)
408{ 404{
409 struct regulator_dev *rdev = to_rdev(dev); 405 struct regulator_dev *rdev = dev_get_drvdata(dev);
410 406
411 if (!rdev->constraints) 407 if (!rdev->constraints)
412 return sprintf(buf, "not defined\n"); 408 return sprintf(buf, "not defined\n");
@@ -417,7 +413,7 @@ static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
417static ssize_t regulator_suspend_disk_mode_show(struct device *dev, 413static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
418 struct device_attribute *attr, char *buf) 414 struct device_attribute *attr, char *buf)
419{ 415{
420 struct regulator_dev *rdev = to_rdev(dev); 416 struct regulator_dev *rdev = dev_get_drvdata(dev);
421 417
422 if (!rdev->constraints) 418 if (!rdev->constraints)
423 return sprintf(buf, "not defined\n"); 419 return sprintf(buf, "not defined\n");
@@ -428,7 +424,7 @@ static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
428static ssize_t regulator_suspend_standby_mode_show(struct device *dev, 424static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
429 struct device_attribute *attr, char *buf) 425 struct device_attribute *attr, char *buf)
430{ 426{
431 struct regulator_dev *rdev = to_rdev(dev); 427 struct regulator_dev *rdev = dev_get_drvdata(dev);
432 428
433 if (!rdev->constraints) 429 if (!rdev->constraints)
434 return sprintf(buf, "not defined\n"); 430 return sprintf(buf, "not defined\n");
@@ -439,7 +435,7 @@ static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
439static ssize_t regulator_suspend_mem_state_show(struct device *dev, 435static ssize_t regulator_suspend_mem_state_show(struct device *dev,
440 struct device_attribute *attr, char *buf) 436 struct device_attribute *attr, char *buf)
441{ 437{
442 struct regulator_dev *rdev = to_rdev(dev); 438 struct regulator_dev *rdev = dev_get_drvdata(dev);
443 439
444 if (!rdev->constraints) 440 if (!rdev->constraints)
445 return sprintf(buf, "not defined\n"); 441 return sprintf(buf, "not defined\n");
@@ -453,7 +449,7 @@ static ssize_t regulator_suspend_mem_state_show(struct device *dev,
453static ssize_t regulator_suspend_disk_state_show(struct device *dev, 449static ssize_t regulator_suspend_disk_state_show(struct device *dev,
454 struct device_attribute *attr, char *buf) 450 struct device_attribute *attr, char *buf)
455{ 451{
456 struct regulator_dev *rdev = to_rdev(dev); 452 struct regulator_dev *rdev = dev_get_drvdata(dev);
457 453
458 if (!rdev->constraints) 454 if (!rdev->constraints)
459 return sprintf(buf, "not defined\n"); 455 return sprintf(buf, "not defined\n");
@@ -467,7 +463,7 @@ static ssize_t regulator_suspend_disk_state_show(struct device *dev,
467static ssize_t regulator_suspend_standby_state_show(struct device *dev, 463static ssize_t regulator_suspend_standby_state_show(struct device *dev,
468 struct device_attribute *attr, char *buf) 464 struct device_attribute *attr, char *buf)
469{ 465{
470 struct regulator_dev *rdev = to_rdev(dev); 466 struct regulator_dev *rdev = dev_get_drvdata(dev);
471 467
472 if (!rdev->constraints) 468 if (!rdev->constraints)
473 return sprintf(buf, "not defined\n"); 469 return sprintf(buf, "not defined\n");
@@ -512,7 +508,7 @@ static struct device_attribute regulator_dev_attrs[] = {
512 508
513static void regulator_dev_release(struct device *dev) 509static void regulator_dev_release(struct device *dev)
514{ 510{
515 struct regulator_dev *rdev = to_rdev(dev); 511 struct regulator_dev *rdev = dev_get_drvdata(dev);
516 kfree(rdev); 512 kfree(rdev);
517} 513}
518 514
@@ -569,8 +565,11 @@ static int suspend_set_state(struct regulator_dev *rdev,
569 565
570 /* enable & disable are mandatory for suspend control */ 566 /* enable & disable are mandatory for suspend control */
571 if (!rdev->desc->ops->set_suspend_enable || 567 if (!rdev->desc->ops->set_suspend_enable ||
572 !rdev->desc->ops->set_suspend_disable) 568 !rdev->desc->ops->set_suspend_disable) {
569 printk(KERN_ERR "%s: no way to set suspend state\n",
570 __func__);
573 return -EINVAL; 571 return -EINVAL;
572 }
574 573
575 if (rstate->enabled) 574 if (rstate->enabled)
576 ret = rdev->desc->ops->set_suspend_enable(rdev); 575 ret = rdev->desc->ops->set_suspend_enable(rdev);
@@ -656,6 +655,125 @@ static void print_constraints(struct regulator_dev *rdev)
656 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf); 655 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
657} 656}
658 657
658/**
659 * set_machine_constraints - sets regulator constraints
660 * @regulator: regulator source
661 *
662 * Allows platform initialisation code to define and constrain
663 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
664 * Constraints *must* be set by platform code in order for some
665 * regulator operations to proceed i.e. set_voltage, set_current_limit,
666 * set_mode.
667 */
668static int set_machine_constraints(struct regulator_dev *rdev,
669 struct regulation_constraints *constraints)
670{
671 int ret = 0;
672
673 rdev->constraints = constraints;
674
675 /* do we need to apply the constraint voltage */
676 if (rdev->constraints->apply_uV &&
677 rdev->constraints->min_uV == rdev->constraints->max_uV &&
678 rdev->desc->ops->set_voltage) {
679 ret = rdev->desc->ops->set_voltage(rdev,
680 rdev->constraints->min_uV, rdev->constraints->max_uV);
681 if (ret < 0) {
682 printk(KERN_ERR "%s: failed to apply %duV"
683 " constraint\n", __func__,
684 rdev->constraints->min_uV);
685 rdev->constraints = NULL;
686 goto out;
687 }
688 }
689
690 /* are we enabled at boot time by firmware / bootloader */
691 if (rdev->constraints->boot_on)
692 rdev->use_count = 1;
693
694 /* do we need to setup our suspend state */
695 if (constraints->initial_state)
696 ret = suspend_prepare(rdev, constraints->initial_state);
697
698 print_constraints(rdev);
699out:
700 return ret;
701}
702
703/**
704 * set_supply - set regulator supply regulator
705 * @regulator: regulator name
706 * @supply: supply regulator name
707 *
708 * Called by platform initialisation code to set the supply regulator for this
709 * regulator. This ensures that a regulators supply will also be enabled by the
710 * core if it's child is enabled.
711 */
712static int set_supply(struct regulator_dev *rdev,
713 struct regulator_dev *supply_rdev)
714{
715 int err;
716
717 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
718 "supply");
719 if (err) {
720 printk(KERN_ERR
721 "%s: could not add device link %s err %d\n",
722 __func__, supply_rdev->dev.kobj.name, err);
723 goto out;
724 }
725 rdev->supply = supply_rdev;
726 list_add(&rdev->slist, &supply_rdev->supply_list);
727out:
728 return err;
729}
730
731/**
732 * set_consumer_device_supply: Bind a regulator to a symbolic supply
733 * @regulator: regulator source
734 * @dev: device the supply applies to
735 * @supply: symbolic name for supply
736 *
737 * Allows platform initialisation code to map physical regulator
738 * sources to symbolic names for supplies for use by devices. Devices
739 * should use these symbolic names to request regulators, avoiding the
740 * need to provide board-specific regulator names as platform data.
741 */
742static int set_consumer_device_supply(struct regulator_dev *rdev,
743 struct device *consumer_dev, const char *supply)
744{
745 struct regulator_map *node;
746
747 if (supply == NULL)
748 return -EINVAL;
749
750 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
751 if (node == NULL)
752 return -ENOMEM;
753
754 node->regulator = rdev;
755 node->dev = consumer_dev;
756 node->supply = supply;
757
758 list_add(&node->list, &regulator_map_list);
759 return 0;
760}
761
762static void unset_consumer_device_supply(struct regulator_dev *rdev,
763 struct device *consumer_dev)
764{
765 struct regulator_map *node, *n;
766
767 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
768 if (rdev == node->regulator &&
769 consumer_dev == node->dev) {
770 list_del(&node->list);
771 kfree(node);
772 return;
773 }
774 }
775}
776
659#define REG_STR_SIZE 32 777#define REG_STR_SIZE 32
660 778
661static struct regulator *create_regulator(struct regulator_dev *rdev, 779static struct regulator *create_regulator(struct regulator_dev *rdev,
@@ -746,7 +864,6 @@ struct regulator *regulator_get(struct device *dev, const char *id)
746 struct regulator_dev *rdev; 864 struct regulator_dev *rdev;
747 struct regulator_map *map; 865 struct regulator_map *map;
748 struct regulator *regulator = ERR_PTR(-ENODEV); 866 struct regulator *regulator = ERR_PTR(-ENODEV);
749 const char *supply = id;
750 867
751 if (id == NULL) { 868 if (id == NULL) {
752 printk(KERN_ERR "regulator: get() with no identifier\n"); 869 printk(KERN_ERR "regulator: get() with no identifier\n");
@@ -758,15 +875,9 @@ struct regulator *regulator_get(struct device *dev, const char *id)
758 list_for_each_entry(map, &regulator_map_list, list) { 875 list_for_each_entry(map, &regulator_map_list, list) {
759 if (dev == map->dev && 876 if (dev == map->dev &&
760 strcmp(map->supply, id) == 0) { 877 strcmp(map->supply, id) == 0) {
761 supply = map->regulator; 878 rdev = map->regulator;
762 break;
763 }
764 }
765
766 list_for_each_entry(rdev, &regulator_list, list) {
767 if (strcmp(supply, rdev->desc->name) == 0 &&
768 try_module_get(rdev->owner))
769 goto found; 879 goto found;
880 }
770 } 881 }
771 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n", 882 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
772 id); 883 id);
@@ -774,12 +885,16 @@ struct regulator *regulator_get(struct device *dev, const char *id)
774 return regulator; 885 return regulator;
775 886
776found: 887found:
888 if (!try_module_get(rdev->owner))
889 goto out;
890
777 regulator = create_regulator(rdev, dev, id); 891 regulator = create_regulator(rdev, dev, id);
778 if (regulator == NULL) { 892 if (regulator == NULL) {
779 regulator = ERR_PTR(-ENOMEM); 893 regulator = ERR_PTR(-ENOMEM);
780 module_put(rdev->owner); 894 module_put(rdev->owner);
781 } 895 }
782 896
897out:
783 mutex_unlock(&regulator_list_mutex); 898 mutex_unlock(&regulator_list_mutex);
784 return regulator; 899 return regulator;
785} 900}
@@ -1559,11 +1674,12 @@ EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1559 * Returns 0 on success. 1674 * Returns 0 on success.
1560 */ 1675 */
1561struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, 1676struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1562 void *reg_data) 1677 struct device *dev, void *driver_data)
1563{ 1678{
1564 static atomic_t regulator_no = ATOMIC_INIT(0); 1679 static atomic_t regulator_no = ATOMIC_INIT(0);
1565 struct regulator_dev *rdev; 1680 struct regulator_dev *rdev;
1566 int ret; 1681 struct regulator_init_data *init_data = dev->platform_data;
1682 int ret, i;
1567 1683
1568 if (regulator_desc == NULL) 1684 if (regulator_desc == NULL)
1569 return ERR_PTR(-EINVAL); 1685 return ERR_PTR(-EINVAL);
@@ -1582,7 +1698,7 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1582 mutex_lock(&regulator_list_mutex); 1698 mutex_lock(&regulator_list_mutex);
1583 1699
1584 mutex_init(&rdev->mutex); 1700 mutex_init(&rdev->mutex);
1585 rdev->reg_data = reg_data; 1701 rdev->reg_data = driver_data;
1586 rdev->owner = regulator_desc->owner; 1702 rdev->owner = regulator_desc->owner;
1587 rdev->desc = regulator_desc; 1703 rdev->desc = regulator_desc;
1588 INIT_LIST_HEAD(&rdev->consumer_list); 1704 INIT_LIST_HEAD(&rdev->consumer_list);
@@ -1591,20 +1707,68 @@ struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1591 INIT_LIST_HEAD(&rdev->slist); 1707 INIT_LIST_HEAD(&rdev->slist);
1592 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); 1708 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1593 1709
1710 /* preform any regulator specific init */
1711 if (init_data->regulator_init) {
1712 ret = init_data->regulator_init(rdev->reg_data);
1713 if (ret < 0) {
1714 kfree(rdev);
1715 rdev = ERR_PTR(ret);
1716 goto out;
1717 }
1718 }
1719
1720 /* set regulator constraints */
1721 ret = set_machine_constraints(rdev, &init_data->constraints);
1722 if (ret < 0) {
1723 kfree(rdev);
1724 rdev = ERR_PTR(ret);
1725 goto out;
1726 }
1727
1728 /* register with sysfs */
1594 rdev->dev.class = &regulator_class; 1729 rdev->dev.class = &regulator_class;
1595 device_initialize(&rdev->dev); 1730 rdev->dev.parent = dev;
1596 snprintf(rdev->dev.bus_id, sizeof(rdev->dev.bus_id), 1731 snprintf(rdev->dev.bus_id, sizeof(rdev->dev.bus_id),
1597 "regulator_%ld_%s", 1732 "regulator.%d", atomic_inc_return(&regulator_no) - 1);
1598 (unsigned long)atomic_inc_return(&regulator_no) - 1, 1733 ret = device_register(&rdev->dev);
1599 regulator_desc->name); 1734 if (ret != 0) {
1600
1601 ret = device_add(&rdev->dev);
1602 if (ret == 0)
1603 list_add(&rdev->list, &regulator_list);
1604 else {
1605 kfree(rdev); 1735 kfree(rdev);
1606 rdev = ERR_PTR(ret); 1736 rdev = ERR_PTR(ret);
1737 goto out;
1738 }
1739
1740 dev_set_drvdata(&rdev->dev, rdev);
1741
1742 /* set supply regulator if it exists */
1743 if (init_data->supply_regulator_dev) {
1744 ret = set_supply(rdev,
1745 dev_get_drvdata(init_data->supply_regulator_dev));
1746 if (ret < 0) {
1747 device_unregister(&rdev->dev);
1748 kfree(rdev);
1749 rdev = ERR_PTR(ret);
1750 goto out;
1751 }
1752 }
1753
1754 /* add consumers devices */
1755 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1756 ret = set_consumer_device_supply(rdev,
1757 init_data->consumer_supplies[i].dev,
1758 init_data->consumer_supplies[i].supply);
1759 if (ret < 0) {
1760 for (--i; i >= 0; i--)
1761 unset_consumer_device_supply(rdev,
1762 init_data->consumer_supplies[i].dev);
1763 device_unregister(&rdev->dev);
1764 kfree(rdev);
1765 rdev = ERR_PTR(ret);
1766 goto out;
1767 }
1607 } 1768 }
1769
1770 list_add(&rdev->list, &regulator_list);
1771out:
1608 mutex_unlock(&regulator_list_mutex); 1772 mutex_unlock(&regulator_list_mutex);
1609 return rdev; 1773 return rdev;
1610} 1774}
@@ -1631,187 +1795,6 @@ void regulator_unregister(struct regulator_dev *rdev)
1631EXPORT_SYMBOL_GPL(regulator_unregister); 1795EXPORT_SYMBOL_GPL(regulator_unregister);
1632 1796
1633/** 1797/**
1634 * regulator_set_supply - set regulator supply regulator
1635 * @regulator: regulator name
1636 * @supply: supply regulator name
1637 *
1638 * Called by platform initialisation code to set the supply regulator for this
1639 * regulator. This ensures that a regulators supply will also be enabled by the
1640 * core if it's child is enabled.
1641 */
1642int regulator_set_supply(const char *regulator, const char *supply)
1643{
1644 struct regulator_dev *rdev, *supply_rdev;
1645 int err;
1646
1647 if (regulator == NULL || supply == NULL)
1648 return -EINVAL;
1649
1650 mutex_lock(&regulator_list_mutex);
1651
1652 list_for_each_entry(rdev, &regulator_list, list) {
1653 if (!strcmp(rdev->desc->name, regulator))
1654 goto found_regulator;
1655 }
1656 mutex_unlock(&regulator_list_mutex);
1657 return -ENODEV;
1658
1659found_regulator:
1660 list_for_each_entry(supply_rdev, &regulator_list, list) {
1661 if (!strcmp(supply_rdev->desc->name, supply))
1662 goto found_supply;
1663 }
1664 mutex_unlock(&regulator_list_mutex);
1665 return -ENODEV;
1666
1667found_supply:
1668 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
1669 "supply");
1670 if (err) {
1671 printk(KERN_ERR
1672 "%s: could not add device link %s err %d\n",
1673 __func__, supply_rdev->dev.kobj.name, err);
1674 goto out;
1675 }
1676 rdev->supply = supply_rdev;
1677 list_add(&rdev->slist, &supply_rdev->supply_list);
1678out:
1679 mutex_unlock(&regulator_list_mutex);
1680 return err;
1681}
1682EXPORT_SYMBOL_GPL(regulator_set_supply);
1683
1684/**
1685 * regulator_get_supply - get regulator supply regulator
1686 * @regulator: regulator name
1687 *
1688 * Returns the supply supply regulator name or NULL if no supply regulator
1689 * exists (i.e the regulator is supplied directly from USB, Line, Battery, etc)
1690 */
1691const char *regulator_get_supply(const char *regulator)
1692{
1693 struct regulator_dev *rdev;
1694
1695 if (regulator == NULL)
1696 return NULL;
1697
1698 mutex_lock(&regulator_list_mutex);
1699 list_for_each_entry(rdev, &regulator_list, list) {
1700 if (!strcmp(rdev->desc->name, regulator))
1701 goto found;
1702 }
1703 mutex_unlock(&regulator_list_mutex);
1704 return NULL;
1705
1706found:
1707 mutex_unlock(&regulator_list_mutex);
1708 if (rdev->supply)
1709 return rdev->supply->desc->name;
1710 else
1711 return NULL;
1712}
1713EXPORT_SYMBOL_GPL(regulator_get_supply);
1714
1715/**
1716 * regulator_set_machine_constraints - sets regulator constraints
1717 * @regulator: regulator source
1718 *
1719 * Allows platform initialisation code to define and constrain
1720 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1721 * Constraints *must* be set by platform code in order for some
1722 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1723 * set_mode.
1724 */
1725int regulator_set_machine_constraints(const char *regulator_name,
1726 struct regulation_constraints *constraints)
1727{
1728 struct regulator_dev *rdev;
1729 int ret = 0;
1730
1731 if (regulator_name == NULL)
1732 return -EINVAL;
1733
1734 mutex_lock(&regulator_list_mutex);
1735
1736 list_for_each_entry(rdev, &regulator_list, list) {
1737 if (!strcmp(regulator_name, rdev->desc->name))
1738 goto found;
1739 }
1740 ret = -ENODEV;
1741 goto out;
1742
1743found:
1744 mutex_lock(&rdev->mutex);
1745 rdev->constraints = constraints;
1746
1747 /* do we need to apply the constraint voltage */
1748 if (rdev->constraints->apply_uV &&
1749 rdev->constraints->min_uV == rdev->constraints->max_uV &&
1750 rdev->desc->ops->set_voltage) {
1751 ret = rdev->desc->ops->set_voltage(rdev,
1752 rdev->constraints->min_uV, rdev->constraints->max_uV);
1753 if (ret < 0) {
1754 printk(KERN_ERR "%s: failed to apply %duV"
1755 " constraint\n", __func__,
1756 rdev->constraints->min_uV);
1757 rdev->constraints = NULL;
1758 goto out;
1759 }
1760 }
1761
1762 /* are we enabled at boot time by firmware / bootloader */
1763 if (rdev->constraints->boot_on)
1764 rdev->use_count = 1;
1765
1766 /* do we need to setup our suspend state */
1767 if (constraints->initial_state)
1768 ret = suspend_prepare(rdev, constraints->initial_state);
1769
1770 print_constraints(rdev);
1771 mutex_unlock(&rdev->mutex);
1772
1773out:
1774 mutex_unlock(&regulator_list_mutex);
1775 return ret;
1776}
1777EXPORT_SYMBOL_GPL(regulator_set_machine_constraints);
1778
1779
1780/**
1781 * regulator_set_device_supply: Bind a regulator to a symbolic supply
1782 * @regulator: regulator source
1783 * @dev: device the supply applies to
1784 * @supply: symbolic name for supply
1785 *
1786 * Allows platform initialisation code to map physical regulator
1787 * sources to symbolic names for supplies for use by devices. Devices
1788 * should use these symbolic names to request regulators, avoiding the
1789 * need to provide board-specific regulator names as platform data.
1790 */
1791int regulator_set_device_supply(const char *regulator, struct device *dev,
1792 const char *supply)
1793{
1794 struct regulator_map *node;
1795
1796 if (regulator == NULL || supply == NULL)
1797 return -EINVAL;
1798
1799 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
1800 if (node == NULL)
1801 return -ENOMEM;
1802
1803 node->regulator = regulator;
1804 node->dev = dev;
1805 node->supply = supply;
1806
1807 mutex_lock(&regulator_list_mutex);
1808 list_add(&node->list, &regulator_map_list);
1809 mutex_unlock(&regulator_list_mutex);
1810 return 0;
1811}
1812EXPORT_SYMBOL_GPL(regulator_set_device_supply);
1813
1814/**
1815 * regulator_suspend_prepare: prepare regulators for system wide suspend 1798 * regulator_suspend_prepare: prepare regulators for system wide suspend
1816 * @state: system suspend state 1799 * @state: system suspend state
1817 * 1800 *
@@ -1893,6 +1876,18 @@ int rdev_get_id(struct regulator_dev *rdev)
1893} 1876}
1894EXPORT_SYMBOL_GPL(rdev_get_id); 1877EXPORT_SYMBOL_GPL(rdev_get_id);
1895 1878
1879struct device *rdev_get_dev(struct regulator_dev *rdev)
1880{
1881 return &rdev->dev;
1882}
1883EXPORT_SYMBOL_GPL(rdev_get_dev);
1884
1885void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
1886{
1887 return reg_init_data->driver_data;
1888}
1889EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
1890
1896static int __init regulator_init(void) 1891static int __init regulator_init(void)
1897{ 1892{
1898 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION); 1893 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);