Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751942AbdF1K1j (ORCPT ); Wed, 28 Jun 2017 06:27:39 -0400 Received: from mail-pf0-f169.google.com ([209.85.192.169]:33785 "EHLO mail-pf0-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751787AbdF1K1Q (ORCPT ); Wed, 28 Jun 2017 06:27:16 -0400 From: Viresh Kumar To: Greg Kroah-Hartman Cc: Viresh Kumar , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Rafael Wysocki , Vincent Guittot , Stephen Boyd , Mark Brown , Shiraz Hashim , Rob Herring , rnayak@codeaurora.org Subject: [RFC 2/5] drivers: boot_constraint: Add support for supply constraints Date: Wed, 28 Jun 2017 15:56:35 +0530 Message-Id: X-Mailer: git-send-email 2.13.0.71.gd7076ec9c9cb In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4233 Lines: 156 This patch adds the first constraint type: power-supply. The constraint is set by setting a voltage range for the respective regulator device, which will be honored by the regulator core even if more users turn up. Once the device is probed, the regulator is released and the constraint is removed. Signed-off-by: Viresh Kumar --- drivers/base/boot_constraint.c | 92 +++++++++++++++++++++++++++++++++++++++++ include/linux/boot_constraint.h | 9 +++- 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/drivers/base/boot_constraint.c b/drivers/base/boot_constraint.c index 38740b8499ba..495344e6405b 100644 --- a/drivers/base/boot_constraint.c +++ b/drivers/base/boot_constraint.c @@ -15,6 +15,7 @@ #include #include #include +#include #include struct constraint { @@ -41,6 +42,8 @@ static LIST_HEAD(constraint_devices); static DEFINE_MUTEX(constraint_devices_mutex); /* Forward declarations of constraints */ +static int constraint_supply_add(struct constraint *constraint, void *data); +static void constraint_supply_remove(struct constraint *constraint); /* Boot constraints core */ @@ -113,6 +116,10 @@ static struct constraint *constraint_allocate(struct constraint_dev *cdev, void (*remove)(struct constraint *constraint); switch (type) { + case BOOT_CONSTRAINT_SUPPLY: + add = constraint_supply_add; + remove = constraint_supply_remove; + break; default: return ERR_PTR(-EINVAL); } @@ -208,3 +215,88 @@ void boot_constraints_remove(struct device *dev) unlock: mutex_unlock(&constraint_devices_mutex); } + + +/* Boot constraints */ + +/* Boot constraint - Supply */ + +struct constraint_supply { + struct boot_constraint_supply_info supply; + struct regulator *reg; +}; + +static int constraint_supply_add(struct constraint *constraint, void *data) +{ + struct boot_constraint_supply_info *supply = data; + struct constraint_supply *csupply; + struct device *dev = constraint->cdev->dev; + int ret; + + csupply = kzalloc(sizeof(*csupply), GFP_KERNEL); + if (!csupply) + return -ENOMEM; + + csupply->reg = regulator_get(dev, supply->name); + if (IS_ERR(csupply->reg)) { + ret = PTR_ERR(csupply->reg); + if (ret != -EPROBE_DEFER) { + dev_err(dev, "regulator_get() failed for %s (%d)\n", + supply->name, ret); + } + goto free; + } + + ret = regulator_set_voltage(csupply->reg, supply->u_volt_min, + supply->u_volt_max); + if (ret) { + dev_err(dev, "regulator_set_voltage %s failed (%d)\n", + supply->name, ret); + goto free_regulator; + } + + if (supply->enable) { + ret = regulator_enable(csupply->reg); + if (ret) { + dev_err(dev, "regulator_enable %s failed (%d)\n", + supply->name, ret); + goto remove_voltage; + } + } + + memcpy(&csupply->supply, supply, sizeof(*supply)); + csupply->supply.name = kstrdup_const(supply->name, GFP_KERNEL); + constraint->private = csupply; + + return 0; + +remove_voltage: + regulator_set_voltage(csupply->reg, 0, INT_MAX); +free_regulator: + regulator_put(csupply->reg); +free: + kfree(csupply); + + return ret; +} + +static void constraint_supply_remove(struct constraint *constraint) +{ + struct constraint_supply *csupply = constraint->private; + struct device *dev = constraint->cdev->dev; + int ret; + + if (csupply->supply.enable) { + ret = regulator_disable(csupply->reg); + if (ret) + dev_err(dev, "regulator_disable failed (%d)\n", ret); + } + + ret = regulator_set_voltage(csupply->reg, 0, INT_MAX); + if (ret) + dev_err(dev, "regulator_set_voltage failed (%d)\n", ret); + + regulator_put(csupply->reg); + kfree_const(csupply->supply.name); + kfree(csupply); +} diff --git a/include/linux/boot_constraint.h b/include/linux/boot_constraint.h index 41b5a62d2dbb..a98fdb403e8d 100644 --- a/include/linux/boot_constraint.h +++ b/include/linux/boot_constraint.h @@ -13,7 +13,14 @@ struct device; enum boot_constraint_type { - BOOT_CONSTRAINT_NONE, + BOOT_CONSTRAINT_SUPPLY, +}; + +struct boot_constraint_supply_info { + bool enable; + const char *name; + unsigned long u_volt_min; + unsigned long u_volt_max; }; #ifdef CONFIG_BOOT_CONSTRAINTS -- 2.13.0.71.gd7076ec9c9cb