From patchwork Fri Dec 16 15:52:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 88319 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1481513qgi; Fri, 16 Dec 2016 07:53:47 -0800 (PST) X-Received: by 10.84.216.28 with SMTP id m28mr8328245pli.107.1481903627606; Fri, 16 Dec 2016 07:53:47 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p19si5632808pgk.133.2016.12.16.07.53.47; Fri, 16 Dec 2016 07:53:47 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@baylibre-com.20150623.gappssmtp.com; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1761849AbcLPPxh (ORCPT + 7 others); Fri, 16 Dec 2016 10:53:37 -0500 Received: from mail-wm0-f52.google.com ([74.125.82.52]:38349 "EHLO mail-wm0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1761798AbcLPPxA (ORCPT ); Fri, 16 Dec 2016 10:53:00 -0500 Received: by mail-wm0-f52.google.com with SMTP id f82so39644708wmf.1 for ; Fri, 16 Dec 2016 07:52:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=GAfaXK/uiPIXHnCUe5b+Fmj2Eom9w/1kT9LENX6OZRk=; b=Ld+4efmWPEooz6qAElC+PRoIii7DrzCsrCu9tn/43rbXv0ZuUz4TO7WH2zhwPjJnB4 9A1roOfVeOn/uvqkY/169/xGhgphb0pDbYCYrmLSJz0hieqMxWoPJNNIOuoy48SZrk9Y iaerYEb+yq4d4lbr2ZDDp6ydBKBmg+xKuIyg0+lYwklvKcW+b9sR/Bid6zrFVA7mhuXI H3Bz1IGdmWqo+xw9DFSEPh+piCh7OSklECuHNW1MaaQvKqAqisObsdmix+0DC+HOInvj sAtj76cjqho8u80Y4+ZHDE1F5ktEssCJ3sAZxSHAg014J5pxTVrJQ9hyS5Kg8U0LTt7B UuTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=GAfaXK/uiPIXHnCUe5b+Fmj2Eom9w/1kT9LENX6OZRk=; b=UJ38bfVLUhuUAdHhIyjwhKCJ8C1JkeOuPSB+9NrhonLea1/sCun6MbWGaVBXGJ8EiR B+7eX/KCpnPTkPto9GONu36CCLvJzAxYI2tJZ/PU1MvNgB9nhb0L1PecKQ+LgtZzyDAf qC29mllBONV9xNd4gXxJjSeplYTryCC/PlCdH4EyBG/F050YTZEfEaOuukkUv10h/tYj jCDfJkDmokLXjxbCsrR4fUD9tre/qeJJlgAaoibfy0H0JFGYOUlyziNQknyY1I7V60+H 0lSCTdnuwqSJvNPInD8E+3YDbmKOqw3S22ko5TA9L/dzFjDYOFGLv4OdvEU2a9sPhc1I /Fcg== X-Gm-Message-State: AIkVDXJBr5BdRJJNnrfc4TTAzeaT/5ckddzCXMtZO+o+xEXNGLdVhE+s0ZOg16Y6XNSq5KQ2 X-Received: by 10.28.26.197 with SMTP id a188mr3624519wma.93.1481903559828; Fri, 16 Dec 2016 07:52:39 -0800 (PST) Received: from localhost.localdomain ([90.63.244.31]) by smtp.gmail.com with ESMTPSA id f10sm7329843wjl.28.2016.12.16.07.52.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 16 Dec 2016 07:52:39 -0800 (PST) From: Bartosz Golaszewski To: Liam Girdwood , Mark Brown , Rob Herring , Mark Rutland Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Kevin Hilman , Patrick Titiano , Neil Armstrong , Bartosz Golaszewski Subject: [PATCH 1/3] regulator: add support for user space controlled regulators Date: Fri, 16 Dec 2016 16:52:28 +0100 Message-Id: <1481903550-3582-2-git-send-email-bgolaszewski@baylibre.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1481903550-3582-1-git-send-email-bgolaszewski@baylibre.com> References: <1481903550-3582-1-git-send-email-bgolaszewski@baylibre.com> Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Add a new flag to struct regulator_desc indicating whether a regulator can be controlled from user space and implement a routine in regulator core allowing to toggle the regulator state via the sysfs 'state' attribute. This is useful for gpio power switches. Signed-off-by: Bartosz Golaszewski --- drivers/regulator/core.c | 38 +++++++++++++++++++++++++++++++++++++- include/linux/regulator/driver.h | 5 +++++ 2 files changed, 42 insertions(+), 1 deletion(-) -- 2.9.3 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 5c1519b..f77de8f 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -99,6 +99,7 @@ struct regulator_supply_alias { }; static int _regulator_is_enabled(struct regulator_dev *rdev); +static int _regulator_enable(struct regulator_dev *rdev); static int _regulator_disable(struct regulator_dev *rdev); static int _regulator_get_voltage(struct regulator_dev *rdev); static int _regulator_get_current_limit(struct regulator_dev *rdev); @@ -401,7 +402,42 @@ static ssize_t regulator_state_show(struct device *dev, return ret; } -static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); +static ssize_t regulator_state_set(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct regulator_dev *rdev = dev_get_drvdata(dev); + bool enable; + ssize_t ret; + + if (!rdev->desc->userspace_control) + return -EPERM; + + if (sysfs_streq(buf, "enabled\n") || sysfs_streq(buf, "1")) + enable = true; + else if (sysfs_streq(buf, "disabled\n") || sysfs_streq(buf, "0")) + enable = false; + else + return -EINVAL; + + mutex_lock(&rdev->mutex); + + if ((enable && _regulator_is_enabled(rdev)) || + (!enable && !_regulator_is_enabled(rdev))) { + mutex_unlock(&rdev->mutex); + return -EBUSY; + } + + ret = enable ? _regulator_enable(rdev) : _regulator_disable(rdev); + + mutex_unlock(&rdev->mutex); + + if (ret) + return ret; + + return len; +} +static DEVICE_ATTR(state, 0644, regulator_state_show, regulator_state_set); static ssize_t regulator_status_show(struct device *dev, struct device_attribute *attr, char *buf) diff --git a/include/linux/regulator/driver.h b/include/linux/regulator/driver.h index 37b5324..0e7ad95 100644 --- a/include/linux/regulator/driver.h +++ b/include/linux/regulator/driver.h @@ -293,6 +293,9 @@ enum regulator_type { * @off_on_delay: guard time (in uS), before re-enabling a regulator * * @of_map_mode: Maps a hardware mode defined in a DeviceTree to a standard mode + * + * @userspace_control: A flag to indicate whether this regulator can be + * controlled from user-space. */ struct regulator_desc { const char *name; @@ -347,6 +350,8 @@ struct regulator_desc { unsigned int off_on_delay; unsigned int (*of_map_mode)(unsigned int mode); + + unsigned int userspace_control; }; /**