Skip to content

Commit f8c12fe

Browse files
broonieLiam Girdwood
authored andcommitted
regulator: Copy constraints from regulators when initialising them
Currently the regulator API uses the constraints structure passed in to the core throughout the lifetime of the object. This means that it is not possible to mark the constraints as __initdata so if the kernel supports many boards the constraints for all of them are kept around throughout the lifetime of the system, consuming memory needlessly. By copying constraints that are actually used we allow the use of __initdata, saving memory when multiple boards are supported. This also means the constraints can be const. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Signed-off-by: Liam Girdwood <lrg@slimlogic.co.uk>
1 parent f4d6adf commit f8c12fe

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

‎drivers/regulator/core.c‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -813,23 +813,26 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,
813813
* set_mode.
814814
*/
815815
static int set_machine_constraints(struct regulator_dev *rdev,
816-
struct regulation_constraints *constraints)
816+
const struct regulation_constraints *constraints)
817817
{
818818
int ret = 0;
819819
const char *name;
820820
struct regulator_ops *ops = rdev->desc->ops;
821821

822-
rdev->constraints = constraints;
822+
rdev->constraints = kmemdup(constraints, sizeof(*constraints),
823+
GFP_KERNEL);
824+
if (!rdev->constraints)
825+
return -ENOMEM;
823826

824827
name = rdev_get_name(rdev);
825828

826-
ret = machine_constraints_voltage(rdev, constraints);
829+
ret = machine_constraints_voltage(rdev, rdev->constraints);
827830
if (ret != 0)
828831
goto out;
829832

830833
/* do we need to setup our suspend state */
831834
if (constraints->initial_state) {
832-
ret = suspend_prepare(rdev, constraints->initial_state);
835+
ret = suspend_prepare(rdev, rdev->constraints->initial_state);
833836
if (ret < 0) {
834837
pr_err("failed to set suspend state for %s\n",
835838
name);
@@ -846,7 +849,7 @@ static int set_machine_constraints(struct regulator_dev *rdev,
846849
goto out;
847850
}
848851

849-
ret = ops->set_mode(rdev, constraints->initial_mode);
852+
ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
850853
if (ret < 0) {
851854
pr_err("failed to set initial mode for %s: %d\n",
852855
name, ret);
@@ -857,7 +860,8 @@ static int set_machine_constraints(struct regulator_dev *rdev,
857860
/* If the constraints say the regulator should be on at this point
858861
* and we have control then make sure it is enabled.
859862
*/
860-
if ((constraints->always_on || constraints->boot_on) && ops->enable) {
863+
if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
864+
ops->enable) {
861865
ret = ops->enable(rdev);
862866
if (ret < 0) {
863867
pr_err("failed to enable %s\n", name);
@@ -2289,7 +2293,7 @@ static int add_regulator_attributes(struct regulator_dev *rdev)
22892293
* Returns 0 on success.
22902294
*/
22912295
struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2292-
struct device *dev, struct regulator_init_data *init_data,
2296+
struct device *dev, const struct regulator_init_data *init_data,
22932297
void *driver_data)
22942298
{
22952299
static atomic_t regulator_no = ATOMIC_INIT(0);
@@ -2444,6 +2448,7 @@ void regulator_unregister(struct regulator_dev *rdev)
24442448
if (rdev->supply)
24452449
sysfs_remove_link(&rdev->dev.kobj, "supply");
24462450
device_unregister(&rdev->dev);
2451+
kfree(rdev->constraints);
24472452
mutex_unlock(&regulator_list_mutex);
24482453
}
24492454
EXPORT_SYMBOL_GPL(regulator_unregister);

‎include/linux/regulator/driver.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ struct regulator_dev {
192192
};
193193

194194
struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
195-
struct device *dev, struct regulator_init_data *init_data,
195+
struct device *dev, const struct regulator_init_data *init_data,
196196
void *driver_data);
197197
void regulator_unregister(struct regulator_dev *rdev);
198198

0 commit comments

Comments
 (0)