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

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>

+334 -330
+65 -73
Documentation/power/regulator/machine.txt
··· 2 2 =================================== 3 3 4 4 The regulator machine driver interface is intended for board/machine specific 5 - initialisation code to configure the regulator subsystem. Typical things that 6 - machine drivers would do are :- 5 + initialisation code to configure the regulator subsystem. 7 6 8 - 1. Regulator -> Device mapping. 9 - 2. Regulator supply configuration. 10 - 3. Power Domain constraint setting. 11 - 12 - 13 - 14 - 1. Regulator -> device mapping 15 - ============================== 16 7 Consider the following machine :- 17 8 18 9 Regulator-1 -+-> Regulator-2 --> [Consumer A @ 1.8 - 2.0V] ··· 12 21 13 22 The drivers for consumers A & B must be mapped to the correct regulator in 14 23 order to control their power supply. This mapping can be achieved in machine 15 - initialisation code by calling :- 24 + initialisation code by creating a struct regulator_consumer_supply for 25 + each regulator. 16 26 17 - int regulator_set_device_supply(const char *regulator, struct device *dev, 18 - const char *supply); 27 + struct regulator_consumer_supply { 28 + struct device *dev; /* consumer */ 29 + const char *supply; /* consumer supply - e.g. "vcc" */ 30 + }; 19 31 20 - and is shown with the following code :- 32 + e.g. for the machine above 21 33 22 - regulator_set_device_supply("Regulator-1", devB, "Vcc"); 23 - regulator_set_device_supply("Regulator-2", devA, "Vcc"); 34 + static struct regulator_consumer_supply regulator1_consumers[] = { 35 + { 36 + .dev = &platform_consumerB_device.dev, 37 + .supply = "Vcc", 38 + },}; 39 + 40 + static struct regulator_consumer_supply regulator2_consumers[] = { 41 + { 42 + .dev = &platform_consumerA_device.dev, 43 + .supply = "Vcc", 44 + },}; 24 45 25 46 This maps Regulator-1 to the 'Vcc' supply for Consumer B and maps Regulator-2 26 47 to the 'Vcc' supply for Consumer A. 27 48 49 + Constraints can now be registered by defining a struct regulator_init_data 50 + for each regulator power domain. This structure also maps the consumers 51 + to their supply regulator :- 28 52 29 - 2. Regulator supply configuration. 30 - ================================== 31 - Consider the following machine (again) :- 32 - 33 - Regulator-1 -+-> Regulator-2 --> [Consumer A @ 1.8 - 2.0V] 34 - | 35 - +-> [Consumer B @ 3.3V] 53 + static struct regulator_init_data regulator1_data = { 54 + .constraints = { 55 + .min_uV = 3300000, 56 + .max_uV = 3300000, 57 + .valid_modes_mask = REGULATOR_MODE_NORMAL, 58 + }, 59 + .num_consumer_supplies = ARRAY_SIZE(regulator1_consumers), 60 + .consumer_supplies = regulator1_consumers, 61 + }; 36 62 37 63 Regulator-1 supplies power to Regulator-2. This relationship must be registered 38 64 with the core so that Regulator-1 is also enabled when Consumer A enables it's 39 - supply (Regulator-2). 65 + supply (Regulator-2). The supply regulator is set by the supply_regulator_dev 66 + field below:- 40 67 41 - This relationship can be register with the core via :- 42 - 43 - int regulator_set_supply(const char *regulator, const char *regulator_supply); 44 - 45 - In this example we would use the following code :- 46 - 47 - regulator_set_supply("Regulator-2", "Regulator-1"); 48 - 49 - Relationships can be queried by calling :- 50 - 51 - const char *regulator_get_supply(const char *regulator); 52 - 53 - 54 - 3. Power Domain constraint setting. 55 - =================================== 56 - Each power domain within a system has physical constraints on voltage and 57 - current. This must be defined in software so that the power domain is always 58 - operated within specifications. 59 - 60 - Consider the following machine (again) :- 61 - 62 - Regulator-1 -+-> Regulator-2 --> [Consumer A @ 1.8 - 2.0V] 63 - | 64 - +-> [Consumer B @ 3.3V] 65 - 66 - This gives us two regulators and two power domains: 67 - 68 - Domain 1: Regulator-2, Consumer B. 69 - Domain 2: Consumer A. 70 - 71 - Constraints can be registered by calling :- 72 - 73 - int regulator_set_platform_constraints(const char *regulator, 74 - struct regulation_constraints *constraints); 75 - 76 - The example is defined as follows :- 77 - 78 - struct regulation_constraints domain_1 = { 79 - .min_uV = 3300000, 80 - .max_uV = 3300000, 81 - .valid_modes_mask = REGULATOR_MODE_NORMAL, 68 + static struct regulator_init_data regulator2_data = { 69 + .supply_regulator_dev = &platform_regulator1_device.dev, 70 + .constraints = { 71 + .min_uV = 1800000, 72 + .max_uV = 2000000, 73 + .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE, 74 + .valid_modes_mask = REGULATOR_MODE_NORMAL, 75 + }, 76 + .num_consumer_supplies = ARRAY_SIZE(regulator2_consumers), 77 + .consumer_supplies = regulator2_consumers, 82 78 }; 83 79 84 - struct regulation_constraints domain_2 = { 85 - .min_uV = 1800000, 86 - .max_uV = 2000000, 87 - .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE, 88 - .valid_modes_mask = REGULATOR_MODE_NORMAL, 89 - }; 80 + Finally the regulator devices must be registered in the usual manner. 90 81 91 - regulator_set_platform_constraints("Regulator-1", &domain_1); 92 - regulator_set_platform_constraints("Regulator-2", &domain_2); 82 + static struct platform_device regulator_devices[] = { 83 + { 84 + .name = "regulator", 85 + .id = DCDC_1, 86 + .dev = { 87 + .platform_data = &regulator1_data, 88 + }, 89 + }, 90 + { 91 + .name = "regulator", 92 + .id = DCDC_2, 93 + .dev = { 94 + .platform_data = &regulator2_data, 95 + }, 96 + }, 97 + }; 98 + /* register regulator 1 device */ 99 + platform_device_register(&wm8350_regulator_devices[0]); 100 + 101 + /* register regulator 2 device */ 102 + platform_device_register(&wm8350_regulator_devices[1]);
+4 -4
Documentation/power/regulator/regulator.txt
··· 10 10 11 11 Drivers can register a regulator by calling :- 12 12 13 - struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, 14 - void *reg_data); 13 + struct regulator_dev *regulator_register(struct device *dev, 14 + struct regulator_desc *regulator_desc); 15 15 16 - This will register the regulators capabilities and operations the regulator 17 - core. The core does not touch reg_data (private to regulator driver). 16 + This will register the regulators capabilities and operations to the regulator 17 + core. 18 18 19 19 Regulators can be unregistered by calling :- 20 20
+9 -12
drivers/regulator/bq24022.c
··· 18 18 #include <linux/regulator/bq24022.h> 19 19 #include <linux/regulator/driver.h> 20 20 21 + 21 22 static int bq24022_set_current_limit(struct regulator_dev *rdev, 22 23 int min_uA, int max_uA) 23 24 { 24 - struct platform_device *pdev = rdev_get_drvdata(rdev); 25 - struct bq24022_mach_info *pdata = pdev->dev.platform_data; 25 + struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev); 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 28 max_uA >= 500000 ? "500" : "100"); 29 29 30 30 /* REVISIT: maybe return error if min_uA != 0 ? */ ··· 34 34 35 35 static int bq24022_get_current_limit(struct regulator_dev *rdev) 36 36 { 37 - struct platform_device *pdev = rdev_get_drvdata(rdev); 38 - struct bq24022_mach_info *pdata = pdev->dev.platform_data; 37 + struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev); 39 38 40 39 return gpio_get_value(pdata->gpio_iset2) ? 500000 : 100000; 41 40 } 42 41 43 42 static int bq24022_enable(struct regulator_dev *rdev) 44 43 { 45 - struct platform_device *pdev = rdev_get_drvdata(rdev); 46 - struct bq24022_mach_info *pdata = pdev->dev.platform_data; 44 + struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev); 47 45 48 - dev_dbg(&pdev->dev, "enabling charger\n"); 46 + dev_dbg(rdev_get_dev(rdev), "enabling charger\n"); 49 47 50 48 gpio_set_value(pdata->gpio_nce, 0); 51 49 return 0; ··· 51 53 52 54 static int bq24022_disable(struct regulator_dev *rdev) 53 55 { 54 - struct platform_device *pdev = rdev_get_drvdata(rdev); 55 - struct bq24022_mach_info *pdata = pdev->dev.platform_data; 56 + struct bq24022_mach_info *pdata = rdev_get_drvdata(rdev); 56 57 57 - dev_dbg(&pdev->dev, "disabling charger\n"); 58 + dev_dbg(rdev_get_dev(rdev), "disabling charger\n"); 58 59 59 60 gpio_set_value(pdata->gpio_nce, 1); 60 61 return 0; ··· 105 108 ret = gpio_direction_output(pdata->gpio_iset2, 0); 106 109 ret = gpio_direction_output(pdata->gpio_nce, 1); 107 110 108 - bq24022 = regulator_register(&bq24022_desc, pdev); 111 + bq24022 = regulator_register(&bq24022_desc, &pdev->dev, pdata); 109 112 if (IS_ERR(bq24022)) { 110 113 dev_dbg(&pdev->dev, "couldn't register regulator\n"); 111 114 ret = PTR_ERR(bq24022);
+227 -232
drivers/regulator/core.c
··· 2 2 * core.c -- Voltage/Current Regulator framework. 3 3 * 4 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 9 * This program is free software; you can redistribute it and/or modify it 9 10 * under the terms of the GNU General Public License as published by the ··· 65 64 struct list_head list; 66 65 struct device *dev; 67 66 const char *supply; 68 - const char *regulator; 67 + struct regulator_dev *regulator; 69 68 }; 70 - 71 - static inline struct regulator_dev *to_rdev(struct device *d) 72 - { 73 - return container_of(d, struct regulator_dev, dev); 74 - } 75 69 76 70 /* 77 71 * struct regulator ··· 223 227 static ssize_t regulator_uV_show(struct device *dev, 224 228 struct device_attribute *attr, char *buf) 225 229 { 226 - struct regulator_dev *rdev = to_rdev(dev); 230 + struct regulator_dev *rdev = dev_get_drvdata(dev); 227 231 ssize_t ret; 228 232 229 233 mutex_lock(&rdev->mutex); ··· 236 240 static ssize_t regulator_uA_show(struct device *dev, 237 241 struct device_attribute *attr, char *buf) 238 242 { 239 - struct regulator_dev *rdev = to_rdev(dev); 243 + struct regulator_dev *rdev = dev_get_drvdata(dev); 240 244 241 245 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); 242 246 } ··· 244 248 static ssize_t regulator_opmode_show(struct device *dev, 245 249 struct device_attribute *attr, char *buf) 246 250 { 247 - struct regulator_dev *rdev = to_rdev(dev); 251 + struct regulator_dev *rdev = dev_get_drvdata(dev); 248 252 int mode = _regulator_get_mode(rdev); 249 253 250 254 switch (mode) { ··· 263 267 static ssize_t regulator_state_show(struct device *dev, 264 268 struct device_attribute *attr, char *buf) 265 269 { 266 - struct regulator_dev *rdev = to_rdev(dev); 270 + struct regulator_dev *rdev = dev_get_drvdata(dev); 267 271 int state = _regulator_is_enabled(rdev); 268 272 269 273 if (state > 0) ··· 277 281 static ssize_t regulator_min_uA_show(struct device *dev, 278 282 struct device_attribute *attr, char *buf) 279 283 { 280 - struct regulator_dev *rdev = to_rdev(dev); 284 + struct regulator_dev *rdev = dev_get_drvdata(dev); 281 285 282 286 if (!rdev->constraints) 283 287 return sprintf(buf, "constraint not defined\n"); ··· 288 292 static ssize_t regulator_max_uA_show(struct device *dev, 289 293 struct device_attribute *attr, char *buf) 290 294 { 291 - struct regulator_dev *rdev = to_rdev(dev); 295 + struct regulator_dev *rdev = dev_get_drvdata(dev); 292 296 293 297 if (!rdev->constraints) 294 298 return sprintf(buf, "constraint not defined\n"); ··· 299 303 static ssize_t regulator_min_uV_show(struct device *dev, 300 304 struct device_attribute *attr, char *buf) 301 305 { 302 - struct regulator_dev *rdev = to_rdev(dev); 306 + struct regulator_dev *rdev = dev_get_drvdata(dev); 303 307 304 308 if (!rdev->constraints) 305 309 return sprintf(buf, "constraint not defined\n"); ··· 310 314 static ssize_t regulator_max_uV_show(struct device *dev, 311 315 struct device_attribute *attr, char *buf) 312 316 { 313 - struct regulator_dev *rdev = to_rdev(dev); 317 + struct regulator_dev *rdev = dev_get_drvdata(dev); 314 318 315 319 if (!rdev->constraints) 316 320 return sprintf(buf, "constraint not defined\n"); ··· 321 325 static ssize_t regulator_total_uA_show(struct device *dev, 322 326 struct device_attribute *attr, char *buf) 323 327 { 324 - struct regulator_dev *rdev = to_rdev(dev); 328 + struct regulator_dev *rdev = dev_get_drvdata(dev); 325 329 struct regulator *regulator; 326 330 int uA = 0; 327 331 ··· 335 339 static ssize_t regulator_num_users_show(struct device *dev, 336 340 struct device_attribute *attr, char *buf) 337 341 { 338 - struct regulator_dev *rdev = to_rdev(dev); 342 + struct regulator_dev *rdev = dev_get_drvdata(dev); 339 343 return sprintf(buf, "%d\n", rdev->use_count); 340 344 } 341 345 342 346 static ssize_t regulator_type_show(struct device *dev, 343 347 struct device_attribute *attr, char *buf) 344 348 { 345 - struct regulator_dev *rdev = to_rdev(dev); 349 + struct regulator_dev *rdev = dev_get_drvdata(dev); 346 350 347 351 switch (rdev->desc->type) { 348 352 case REGULATOR_VOLTAGE: ··· 356 360 static ssize_t regulator_suspend_mem_uV_show(struct device *dev, 357 361 struct device_attribute *attr, char *buf) 358 362 { 359 - struct regulator_dev *rdev = to_rdev(dev); 363 + struct regulator_dev *rdev = dev_get_drvdata(dev); 360 364 361 365 if (!rdev->constraints) 362 366 return sprintf(buf, "not defined\n"); ··· 366 370 static ssize_t regulator_suspend_disk_uV_show(struct device *dev, 367 371 struct device_attribute *attr, char *buf) 368 372 { 369 - struct regulator_dev *rdev = to_rdev(dev); 373 + struct regulator_dev *rdev = dev_get_drvdata(dev); 370 374 371 375 if (!rdev->constraints) 372 376 return sprintf(buf, "not defined\n"); ··· 376 380 static ssize_t regulator_suspend_standby_uV_show(struct device *dev, 377 381 struct device_attribute *attr, char *buf) 378 382 { 379 - struct regulator_dev *rdev = to_rdev(dev); 383 + struct regulator_dev *rdev = dev_get_drvdata(dev); 380 384 381 385 if (!rdev->constraints) 382 386 return sprintf(buf, "not defined\n"); ··· 402 406 static ssize_t regulator_suspend_mem_mode_show(struct device *dev, 403 407 struct device_attribute *attr, char *buf) 404 408 { 405 - struct regulator_dev *rdev = to_rdev(dev); 409 + struct regulator_dev *rdev = dev_get_drvdata(dev); 406 410 407 411 if (!rdev->constraints) 408 412 return sprintf(buf, "not defined\n"); ··· 413 417 static ssize_t regulator_suspend_disk_mode_show(struct device *dev, 414 418 struct device_attribute *attr, char *buf) 415 419 { 416 - struct regulator_dev *rdev = to_rdev(dev); 420 + struct regulator_dev *rdev = dev_get_drvdata(dev); 417 421 418 422 if (!rdev->constraints) 419 423 return sprintf(buf, "not defined\n"); ··· 424 428 static ssize_t regulator_suspend_standby_mode_show(struct device *dev, 425 429 struct device_attribute *attr, char *buf) 426 430 { 427 - struct regulator_dev *rdev = to_rdev(dev); 431 + struct regulator_dev *rdev = dev_get_drvdata(dev); 428 432 429 433 if (!rdev->constraints) 430 434 return sprintf(buf, "not defined\n"); ··· 435 439 static ssize_t regulator_suspend_mem_state_show(struct device *dev, 436 440 struct device_attribute *attr, char *buf) 437 441 { 438 - struct regulator_dev *rdev = to_rdev(dev); 442 + struct regulator_dev *rdev = dev_get_drvdata(dev); 439 443 440 444 if (!rdev->constraints) 441 445 return sprintf(buf, "not defined\n"); ··· 449 453 static ssize_t regulator_suspend_disk_state_show(struct device *dev, 450 454 struct device_attribute *attr, char *buf) 451 455 { 452 - struct regulator_dev *rdev = to_rdev(dev); 456 + struct regulator_dev *rdev = dev_get_drvdata(dev); 453 457 454 458 if (!rdev->constraints) 455 459 return sprintf(buf, "not defined\n"); ··· 463 467 static ssize_t regulator_suspend_standby_state_show(struct device *dev, 464 468 struct device_attribute *attr, char *buf) 465 469 { 466 - struct regulator_dev *rdev = to_rdev(dev); 470 + struct regulator_dev *rdev = dev_get_drvdata(dev); 467 471 468 472 if (!rdev->constraints) 469 473 return sprintf(buf, "not defined\n"); ··· 508 512 509 513 static void regulator_dev_release(struct device *dev) 510 514 { 511 - struct regulator_dev *rdev = to_rdev(dev); 515 + struct regulator_dev *rdev = dev_get_drvdata(dev); 512 516 kfree(rdev); 513 517 } 514 518 ··· 565 569 566 570 /* enable & disable are mandatory for suspend control */ 567 571 if (!rdev->desc->ops->set_suspend_enable || 568 - !rdev->desc->ops->set_suspend_disable) 572 + !rdev->desc->ops->set_suspend_disable) { 573 + printk(KERN_ERR "%s: no way to set suspend state\n", 574 + __func__); 569 575 return -EINVAL; 576 + } 570 577 571 578 if (rstate->enabled) 572 579 ret = rdev->desc->ops->set_suspend_enable(rdev); ··· 653 654 count += sprintf(buf + count, "standby"); 654 655 655 656 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf); 657 + } 658 + 659 + /** 660 + * set_machine_constraints - sets regulator constraints 661 + * @regulator: regulator source 662 + * 663 + * Allows platform initialisation code to define and constrain 664 + * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: 665 + * Constraints *must* be set by platform code in order for some 666 + * regulator operations to proceed i.e. set_voltage, set_current_limit, 667 + * set_mode. 668 + */ 669 + static int set_machine_constraints(struct regulator_dev *rdev, 670 + struct regulation_constraints *constraints) 671 + { 672 + int ret = 0; 673 + 674 + rdev->constraints = constraints; 675 + 676 + /* do we need to apply the constraint voltage */ 677 + if (rdev->constraints->apply_uV && 678 + rdev->constraints->min_uV == rdev->constraints->max_uV && 679 + rdev->desc->ops->set_voltage) { 680 + ret = rdev->desc->ops->set_voltage(rdev, 681 + rdev->constraints->min_uV, rdev->constraints->max_uV); 682 + if (ret < 0) { 683 + printk(KERN_ERR "%s: failed to apply %duV" 684 + " constraint\n", __func__, 685 + rdev->constraints->min_uV); 686 + rdev->constraints = NULL; 687 + goto out; 688 + } 689 + } 690 + 691 + /* are we enabled at boot time by firmware / bootloader */ 692 + if (rdev->constraints->boot_on) 693 + rdev->use_count = 1; 694 + 695 + /* do we need to setup our suspend state */ 696 + if (constraints->initial_state) 697 + ret = suspend_prepare(rdev, constraints->initial_state); 698 + 699 + print_constraints(rdev); 700 + out: 701 + return ret; 702 + } 703 + 704 + /** 705 + * set_supply - set regulator supply regulator 706 + * @regulator: regulator name 707 + * @supply: supply regulator name 708 + * 709 + * Called by platform initialisation code to set the supply regulator for this 710 + * regulator. This ensures that a regulators supply will also be enabled by the 711 + * core if it's child is enabled. 712 + */ 713 + static int set_supply(struct regulator_dev *rdev, 714 + struct regulator_dev *supply_rdev) 715 + { 716 + int err; 717 + 718 + err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj, 719 + "supply"); 720 + if (err) { 721 + printk(KERN_ERR 722 + "%s: could not add device link %s err %d\n", 723 + __func__, supply_rdev->dev.kobj.name, err); 724 + goto out; 725 + } 726 + rdev->supply = supply_rdev; 727 + list_add(&rdev->slist, &supply_rdev->supply_list); 728 + out: 729 + return err; 730 + } 731 + 732 + /** 733 + * set_consumer_device_supply: Bind a regulator to a symbolic supply 734 + * @regulator: regulator source 735 + * @dev: device the supply applies to 736 + * @supply: symbolic name for supply 737 + * 738 + * Allows platform initialisation code to map physical regulator 739 + * sources to symbolic names for supplies for use by devices. Devices 740 + * should use these symbolic names to request regulators, avoiding the 741 + * need to provide board-specific regulator names as platform data. 742 + */ 743 + static int set_consumer_device_supply(struct regulator_dev *rdev, 744 + struct device *consumer_dev, const char *supply) 745 + { 746 + struct regulator_map *node; 747 + 748 + if (supply == NULL) 749 + return -EINVAL; 750 + 751 + node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL); 752 + if (node == NULL) 753 + return -ENOMEM; 754 + 755 + node->regulator = rdev; 756 + node->dev = consumer_dev; 757 + node->supply = supply; 758 + 759 + list_add(&node->list, &regulator_map_list); 760 + return 0; 761 + } 762 + 763 + static void unset_consumer_device_supply(struct regulator_dev *rdev, 764 + struct device *consumer_dev) 765 + { 766 + struct regulator_map *node, *n; 767 + 768 + list_for_each_entry_safe(node, n, &regulator_map_list, list) { 769 + if (rdev == node->regulator && 770 + consumer_dev == node->dev) { 771 + list_del(&node->list); 772 + kfree(node); 773 + return; 774 + } 775 + } 656 776 } 657 777 658 778 #define REG_STR_SIZE 32 ··· 864 746 struct regulator_dev *rdev; 865 747 struct regulator_map *map; 866 748 struct regulator *regulator = ERR_PTR(-ENODEV); 867 - const char *supply = id; 868 749 869 750 if (id == NULL) { 870 751 printk(KERN_ERR "regulator: get() with no identifier\n"); ··· 875 758 list_for_each_entry(map, &regulator_map_list, list) { 876 759 if (dev == map->dev && 877 760 strcmp(map->supply, id) == 0) { 878 - supply = map->regulator; 879 - break; 880 - } 881 - } 882 - 883 - list_for_each_entry(rdev, &regulator_list, list) { 884 - if (strcmp(supply, rdev->desc->name) == 0 && 885 - try_module_get(rdev->owner)) 761 + rdev = map->regulator; 886 762 goto found; 763 + } 887 764 } 888 765 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n", 889 766 id); ··· 885 774 return regulator; 886 775 887 776 found: 777 + if (!try_module_get(rdev->owner)) 778 + goto out; 779 + 888 780 regulator = create_regulator(rdev, dev, id); 889 781 if (regulator == NULL) { 890 782 regulator = ERR_PTR(-ENOMEM); 891 783 module_put(rdev->owner); 892 784 } 893 785 786 + out: 894 787 mutex_unlock(&regulator_list_mutex); 895 788 return regulator; 896 789 } ··· 1674 1559 * Returns 0 on success. 1675 1560 */ 1676 1561 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, 1677 - void *reg_data) 1562 + struct device *dev, void *driver_data) 1678 1563 { 1679 1564 static atomic_t regulator_no = ATOMIC_INIT(0); 1680 1565 struct regulator_dev *rdev; 1681 - int ret; 1566 + struct regulator_init_data *init_data = dev->platform_data; 1567 + int ret, i; 1682 1568 1683 1569 if (regulator_desc == NULL) 1684 1570 return ERR_PTR(-EINVAL); ··· 1698 1582 mutex_lock(&regulator_list_mutex); 1699 1583 1700 1584 mutex_init(&rdev->mutex); 1701 - rdev->reg_data = reg_data; 1585 + rdev->reg_data = driver_data; 1702 1586 rdev->owner = regulator_desc->owner; 1703 1587 rdev->desc = regulator_desc; 1704 1588 INIT_LIST_HEAD(&rdev->consumer_list); ··· 1707 1591 INIT_LIST_HEAD(&rdev->slist); 1708 1592 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); 1709 1593 1710 - rdev->dev.class = &regulator_class; 1711 - device_initialize(&rdev->dev); 1712 - snprintf(rdev->dev.bus_id, sizeof(rdev->dev.bus_id), 1713 - "regulator_%ld_%s", 1714 - (unsigned long)atomic_inc_return(&regulator_no) - 1, 1715 - regulator_desc->name); 1594 + /* preform any regulator specific init */ 1595 + if (init_data->regulator_init) { 1596 + ret = init_data->regulator_init(rdev->reg_data); 1597 + if (ret < 0) { 1598 + kfree(rdev); 1599 + rdev = ERR_PTR(ret); 1600 + goto out; 1601 + } 1602 + } 1716 1603 1717 - ret = device_add(&rdev->dev); 1718 - if (ret == 0) 1719 - list_add(&rdev->list, &regulator_list); 1720 - else { 1604 + /* set regulator constraints */ 1605 + ret = set_machine_constraints(rdev, &init_data->constraints); 1606 + if (ret < 0) { 1721 1607 kfree(rdev); 1722 1608 rdev = ERR_PTR(ret); 1609 + goto out; 1723 1610 } 1611 + 1612 + /* register with sysfs */ 1613 + rdev->dev.class = &regulator_class; 1614 + rdev->dev.parent = dev; 1615 + snprintf(rdev->dev.bus_id, sizeof(rdev->dev.bus_id), 1616 + "regulator.%d", atomic_inc_return(&regulator_no) - 1); 1617 + ret = device_register(&rdev->dev); 1618 + if (ret != 0) { 1619 + kfree(rdev); 1620 + rdev = ERR_PTR(ret); 1621 + goto out; 1622 + } 1623 + 1624 + dev_set_drvdata(&rdev->dev, rdev); 1625 + 1626 + /* set supply regulator if it exists */ 1627 + if (init_data->supply_regulator_dev) { 1628 + ret = set_supply(rdev, 1629 + dev_get_drvdata(init_data->supply_regulator_dev)); 1630 + if (ret < 0) { 1631 + device_unregister(&rdev->dev); 1632 + kfree(rdev); 1633 + rdev = ERR_PTR(ret); 1634 + goto out; 1635 + } 1636 + } 1637 + 1638 + /* add consumers devices */ 1639 + for (i = 0; i < init_data->num_consumer_supplies; i++) { 1640 + ret = set_consumer_device_supply(rdev, 1641 + init_data->consumer_supplies[i].dev, 1642 + init_data->consumer_supplies[i].supply); 1643 + if (ret < 0) { 1644 + for (--i; i >= 0; i--) 1645 + unset_consumer_device_supply(rdev, 1646 + init_data->consumer_supplies[i].dev); 1647 + device_unregister(&rdev->dev); 1648 + kfree(rdev); 1649 + rdev = ERR_PTR(ret); 1650 + goto out; 1651 + } 1652 + } 1653 + 1654 + list_add(&rdev->list, &regulator_list); 1655 + out: 1724 1656 mutex_unlock(&regulator_list_mutex); 1725 1657 return rdev; 1726 1658 } ··· 1793 1629 mutex_unlock(&regulator_list_mutex); 1794 1630 } 1795 1631 EXPORT_SYMBOL_GPL(regulator_unregister); 1796 - 1797 - /** 1798 - * regulator_set_supply - set regulator supply regulator 1799 - * @regulator: regulator name 1800 - * @supply: supply regulator name 1801 - * 1802 - * Called by platform initialisation code to set the supply regulator for this 1803 - * regulator. This ensures that a regulators supply will also be enabled by the 1804 - * core if it's child is enabled. 1805 - */ 1806 - int regulator_set_supply(const char *regulator, const char *supply) 1807 - { 1808 - struct regulator_dev *rdev, *supply_rdev; 1809 - int err; 1810 - 1811 - if (regulator == NULL || supply == NULL) 1812 - return -EINVAL; 1813 - 1814 - mutex_lock(&regulator_list_mutex); 1815 - 1816 - list_for_each_entry(rdev, &regulator_list, list) { 1817 - if (!strcmp(rdev->desc->name, regulator)) 1818 - goto found_regulator; 1819 - } 1820 - mutex_unlock(&regulator_list_mutex); 1821 - return -ENODEV; 1822 - 1823 - found_regulator: 1824 - list_for_each_entry(supply_rdev, &regulator_list, list) { 1825 - if (!strcmp(supply_rdev->desc->name, supply)) 1826 - goto found_supply; 1827 - } 1828 - mutex_unlock(&regulator_list_mutex); 1829 - return -ENODEV; 1830 - 1831 - found_supply: 1832 - err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj, 1833 - "supply"); 1834 - if (err) { 1835 - printk(KERN_ERR 1836 - "%s: could not add device link %s err %d\n", 1837 - __func__, supply_rdev->dev.kobj.name, err); 1838 - goto out; 1839 - } 1840 - rdev->supply = supply_rdev; 1841 - list_add(&rdev->slist, &supply_rdev->supply_list); 1842 - out: 1843 - mutex_unlock(&regulator_list_mutex); 1844 - return err; 1845 - } 1846 - EXPORT_SYMBOL_GPL(regulator_set_supply); 1847 - 1848 - /** 1849 - * regulator_get_supply - get regulator supply regulator 1850 - * @regulator: regulator name 1851 - * 1852 - * Returns the supply supply regulator name or NULL if no supply regulator 1853 - * exists (i.e the regulator is supplied directly from USB, Line, Battery, etc) 1854 - */ 1855 - const char *regulator_get_supply(const char *regulator) 1856 - { 1857 - struct regulator_dev *rdev; 1858 - 1859 - if (regulator == NULL) 1860 - return NULL; 1861 - 1862 - mutex_lock(&regulator_list_mutex); 1863 - list_for_each_entry(rdev, &regulator_list, list) { 1864 - if (!strcmp(rdev->desc->name, regulator)) 1865 - goto found; 1866 - } 1867 - mutex_unlock(&regulator_list_mutex); 1868 - return NULL; 1869 - 1870 - found: 1871 - mutex_unlock(&regulator_list_mutex); 1872 - if (rdev->supply) 1873 - return rdev->supply->desc->name; 1874 - else 1875 - return NULL; 1876 - } 1877 - EXPORT_SYMBOL_GPL(regulator_get_supply); 1878 - 1879 - /** 1880 - * regulator_set_machine_constraints - sets regulator constraints 1881 - * @regulator: regulator source 1882 - * 1883 - * Allows platform initialisation code to define and constrain 1884 - * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: 1885 - * Constraints *must* be set by platform code in order for some 1886 - * regulator operations to proceed i.e. set_voltage, set_current_limit, 1887 - * set_mode. 1888 - */ 1889 - int regulator_set_machine_constraints(const char *regulator_name, 1890 - struct regulation_constraints *constraints) 1891 - { 1892 - struct regulator_dev *rdev; 1893 - int ret = 0; 1894 - 1895 - if (regulator_name == NULL) 1896 - return -EINVAL; 1897 - 1898 - mutex_lock(&regulator_list_mutex); 1899 - 1900 - list_for_each_entry(rdev, &regulator_list, list) { 1901 - if (!strcmp(regulator_name, rdev->desc->name)) 1902 - goto found; 1903 - } 1904 - ret = -ENODEV; 1905 - goto out; 1906 - 1907 - found: 1908 - mutex_lock(&rdev->mutex); 1909 - rdev->constraints = constraints; 1910 - 1911 - /* do we need to apply the constraint voltage */ 1912 - if (rdev->constraints->apply_uV && 1913 - rdev->constraints->min_uV == rdev->constraints->max_uV && 1914 - rdev->desc->ops->set_voltage) { 1915 - ret = rdev->desc->ops->set_voltage(rdev, 1916 - rdev->constraints->min_uV, rdev->constraints->max_uV); 1917 - if (ret < 0) { 1918 - printk(KERN_ERR "%s: failed to apply %duV" 1919 - " constraint\n", __func__, 1920 - rdev->constraints->min_uV); 1921 - rdev->constraints = NULL; 1922 - goto out; 1923 - } 1924 - } 1925 - 1926 - /* are we enabled at boot time by firmware / bootloader */ 1927 - if (rdev->constraints->boot_on) 1928 - rdev->use_count = 1; 1929 - 1930 - /* do we need to setup our suspend state */ 1931 - if (constraints->initial_state) 1932 - ret = suspend_prepare(rdev, constraints->initial_state); 1933 - 1934 - print_constraints(rdev); 1935 - mutex_unlock(&rdev->mutex); 1936 - 1937 - out: 1938 - mutex_unlock(&regulator_list_mutex); 1939 - return ret; 1940 - } 1941 - EXPORT_SYMBOL_GPL(regulator_set_machine_constraints); 1942 - 1943 - 1944 - /** 1945 - * regulator_set_device_supply: Bind a regulator to a symbolic supply 1946 - * @regulator: regulator source 1947 - * @dev: device the supply applies to 1948 - * @supply: symbolic name for supply 1949 - * 1950 - * Allows platform initialisation code to map physical regulator 1951 - * sources to symbolic names for supplies for use by devices. Devices 1952 - * should use these symbolic names to request regulators, avoiding the 1953 - * need to provide board-specific regulator names as platform data. 1954 - */ 1955 - int regulator_set_device_supply(const char *regulator, struct device *dev, 1956 - const char *supply) 1957 - { 1958 - struct regulator_map *node; 1959 - 1960 - if (regulator == NULL || supply == NULL) 1961 - return -EINVAL; 1962 - 1963 - node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL); 1964 - if (node == NULL) 1965 - return -ENOMEM; 1966 - 1967 - node->regulator = regulator; 1968 - node->dev = dev; 1969 - node->supply = supply; 1970 - 1971 - mutex_lock(&regulator_list_mutex); 1972 - list_add(&node->list, &regulator_map_list); 1973 - mutex_unlock(&regulator_list_mutex); 1974 - return 0; 1975 - } 1976 - EXPORT_SYMBOL_GPL(regulator_set_device_supply); 1977 1632 1978 1633 /** 1979 1634 * regulator_suspend_prepare: prepare regulators for system wide suspend ··· 1875 1892 return rdev->desc->id; 1876 1893 } 1877 1894 EXPORT_SYMBOL_GPL(rdev_get_id); 1895 + 1896 + struct device *rdev_get_dev(struct regulator_dev *rdev) 1897 + { 1898 + return &rdev->dev; 1899 + } 1900 + EXPORT_SYMBOL_GPL(rdev_get_dev); 1901 + 1902 + void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) 1903 + { 1904 + return reg_init_data->driver_data; 1905 + } 1906 + EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); 1878 1907 1879 1908 static int __init regulator_init(void) 1880 1909 {
+5 -3
include/linux/regulator/driver.h
··· 18 18 #include <linux/device.h> 19 19 #include <linux/regulator/consumer.h> 20 20 21 - struct regulator_constraints; 22 21 struct regulator_dev; 22 + struct regulator_init_data; 23 23 24 24 /** 25 25 * struct regulator_ops - regulator operations. ··· 85 85 struct module *owner; 86 86 }; 87 87 88 - 89 88 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, 90 - void *reg_data); 89 + struct device *dev, void *driver_data); 91 90 void regulator_unregister(struct regulator_dev *rdev); 92 91 93 92 int regulator_notifier_call_chain(struct regulator_dev *rdev, 94 93 unsigned long event, void *data); 95 94 96 95 void *rdev_get_drvdata(struct regulator_dev *rdev); 96 + struct device *rdev_get_dev(struct regulator_dev *rdev); 97 97 int rdev_get_id(struct regulator_dev *rdev); 98 + 99 + void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data); 98 100 99 101 #endif
+24 -6
include/linux/regulator/machine.h
··· 89 89 unsigned apply_uV:1; /* apply uV constraint iff min == max */ 90 90 }; 91 91 92 - int regulator_set_supply(const char *regulator, const char *regulator_supply); 92 + /** 93 + * struct regulator_consumer_supply - supply -> device mapping 94 + * 95 + * This maps a supply name to a device. 96 + */ 97 + struct regulator_consumer_supply { 98 + struct device *dev; /* consumer */ 99 + const char *supply; /* consumer supply - e.g. "vcc" */ 100 + }; 93 101 94 - const char *regulator_get_supply(const char *regulator); 102 + /** 103 + * struct regulator_init_data - regulator platform initialisation data. 104 + * 105 + * Initialisation constraints, our supply and consumers supplies. 106 + */ 107 + struct regulator_init_data { 108 + struct device *supply_regulator_dev; /* or NULL for LINE */ 95 109 96 - int regulator_set_machine_constraints(const char *regulator, 97 - struct regulation_constraints *constraints); 110 + struct regulation_constraints constraints; 98 111 99 - int regulator_set_device_supply(const char *regulator, struct device *dev, 100 - const char *supply); 112 + int num_consumer_supplies; 113 + struct regulator_consumer_supply *consumer_supplies; 114 + 115 + /* optional regulator machine specific init */ 116 + int (*regulator_init)(void *driver_data); 117 + void *driver_data; /* core does not touch this */ 118 + }; 101 119 102 120 int regulator_suspend_prepare(suspend_state_t state); 103 121