From patchwork Fri Sep 22 18:37:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 114074 Delivered-To: patch@linaro.org Received: by 10.140.106.117 with SMTP id d108csp3612137qgf; Fri, 22 Sep 2017 11:38:16 -0700 (PDT) X-Google-Smtp-Source: AOwi7QDWtI4/F1AQ6thStEszFbDkAe+DkmYxsm6Cxbhha2qA0o0R5lTj+gvRoNo8Jip77e0DyrDU X-Received: by 10.98.5.4 with SMTP id 4mr57031pff.175.1506105496856; Fri, 22 Sep 2017 11:38:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1506105496; cv=none; d=google.com; s=arc-20160816; b=BYtzpdILGcq9BfoaoYtrCRUxPEzIR030C1h4E5AWOJbk0/PisW0OBmG9+kGM6mBBvz 7+236wmFi4UtsAWT90w8lXvaHj8FKKORAvMSR5XhxpgtV2qsOkFNZbMncUgtu1tuzRqq NgEGsOhpgF1wcKC5n9U0oWnjlhhbx7XGpc7JJhCxUPklwCLmUM4H++MWhNh5I8+AIkRI VjDddQE/nF2uCE+4OkEW+a0T+v0t2y2YpDGZFHv5Rk6frGIlZVgUdZjgD3mkKMYpF+NI JGpUausOs8xsKO17x3Ru7FJfwOJpdcgqpsr/3IMy/QoZNF5IfN/oZJgvAJkQuwXnOYrp ZPzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dmarc-filter:arc-authentication-results; bh=fbXbx1JipFjyXnryy/XhNKTLN2qYmv5ReWP2Y8XwMf4=; b=ZPgYEAiIl9f0g03qqRhLSu43MidUnp/0/XhINZDGf902DyPYAzJbTisoXc1QuqgC+0 hUzM9FmHN2irCzj+cb77r+jS/oG2OobKg8dPhTx6VC6nMOlfQkKPdBR3tfq5FDoDM+8w qZMp4+AwxB7VI1CmjjgFBY41iqB98zxfhZXrjBcj4JABE/E+CoTRMb38RlBlrhtG4OLo BIl2aOj7C/d//0kMz+cQUZk9cq9bQzDDsICm9/DcgoOdr2YKvtFw2igVnECaQZYbxH3e VRe+B4Nx0jJ6lP9Eftv5UNWXvai7ID6ss5OKdJdDOj23fKaXiMJrE91xVOT818U920+S SvhA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e7si223179plt.712.2017.09.22.11.38.16; Fri, 22 Sep 2017 11:38:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752005AbdIVSiO (ORCPT + 26 others); Fri, 22 Sep 2017 14:38:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52158 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751845AbdIVSiM (ORCPT ); Fri, 22 Sep 2017 14:38:12 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 320A4C04D935; Fri, 22 Sep 2017 18:38:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 320A4C04D935 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=hdegoede@redhat.com Received: from dhcp-45-79.space.revspace.nl.com (ovpn-112-18.ams2.redhat.com [10.36.112.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id E902C5C6F9; Fri, 22 Sep 2017 18:38:06 +0000 (UTC) From: Hans de Goede To: MyungJoo Ham , Chanwoo Choi , Guenter Roeck , Heikki Krogerus , Darren Hart , Andy Shevchenko , Peter Rosin , Mathias Nyman Cc: Hans de Goede , linux-kernel@vger.kernel.org, platform-driver-x86@vger.kernel.org, devel@driverdev.osuosl.org, Kuppuswamy Sathyanarayanan , Sathyanarayanan Kuppuswamy Natarajan , Greg Kroah-Hartman , linux-usb@vger.kernel.org, Stephen Boyd Subject: [PATCH v3 01/14] mux: core: Add mux_control_get_optional() API Date: Fri, 22 Sep 2017 20:37:50 +0200 Message-Id: <20170922183803.10701-1-hdegoede@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Fri, 22 Sep 2017 18:38:12 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Stephen Boyd Sometimes drivers only use muxes under certain scenarios. For example, the chipidea usb controller may be connected to a usb switch on some platforms, and connected directly to a usb port on others. The driver won't know one way or the other though, so add a mux_control_get_optional() API that allows the driver to differentiate errors getting the mux from there not being a mux for the driver to use at all. Signed-off-by: Stephen Boyd Signed-off-by: Hans de Goede --- Documentation/driver-model/devres.txt | 1 + drivers/mux/core.c | 104 +++++++++++++++++++++++++++------- include/linux/mux/consumer.h | 4 ++ 3 files changed, 89 insertions(+), 20 deletions(-) -- 2.14.1 Acked-by: Chanwoo Choi diff --git a/Documentation/driver-model/devres.txt b/Documentation/driver-model/devres.txt index 69f08c0f23a8..5e41f3ac8a05 100644 --- a/Documentation/driver-model/devres.txt +++ b/Documentation/driver-model/devres.txt @@ -343,6 +343,7 @@ MUX devm_mux_chip_alloc() devm_mux_chip_register() devm_mux_control_get() + devm_mux_control_get_optional() PER-CPU MEM devm_alloc_percpu() diff --git a/drivers/mux/core.c b/drivers/mux/core.c index 6e5cf9d9cd99..244bceb17877 100644 --- a/drivers/mux/core.c +++ b/drivers/mux/core.c @@ -289,6 +289,9 @@ EXPORT_SYMBOL_GPL(devm_mux_chip_register); */ unsigned int mux_control_states(struct mux_control *mux) { + if (!mux) + return 0; + return mux->states; } EXPORT_SYMBOL_GPL(mux_control_states); @@ -338,6 +341,9 @@ int mux_control_select(struct mux_control *mux, unsigned int state) { int ret; + if (!mux) + return 0; + ret = down_killable(&mux->lock); if (ret < 0) return ret; @@ -370,6 +376,9 @@ int mux_control_try_select(struct mux_control *mux, unsigned int state) { int ret; + if (!mux) + return 0; + if (down_trylock(&mux->lock)) return -EBUSY; @@ -398,6 +407,9 @@ int mux_control_deselect(struct mux_control *mux) { int ret = 0; + if (!mux) + return 0; + if (mux->idle_state != MUX_IDLE_AS_IS && mux->idle_state != mux->cached_state) ret = mux_control_set(mux, mux->idle_state); @@ -423,14 +435,8 @@ static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np) return dev ? to_mux_chip(dev) : NULL; } -/** - * mux_control_get() - Get the mux-control for a device. - * @dev: The device that needs a mux-control. - * @mux_name: The name identifying the mux-control. - * - * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno. - */ -struct mux_control *mux_control_get(struct device *dev, const char *mux_name) +static struct mux_control * +__mux_control_get(struct device *dev, const char *mux_name, bool optional) { struct device_node *np = dev->of_node; struct of_phandle_args args; @@ -442,16 +448,22 @@ struct mux_control *mux_control_get(struct device *dev, const char *mux_name) if (mux_name) { index = of_property_match_string(np, "mux-control-names", mux_name); + if ((index == -EINVAL || index == -ENODATA) && optional) + return NULL; if (index < 0) { dev_err(dev, "mux controller '%s' not found\n", mux_name); return ERR_PTR(index); } + /* OF does point to a mux, so it's no longer optional */ + optional = false; } ret = of_parse_phandle_with_args(np, "mux-controls", "#mux-control-cells", index, &args); + if (ret == -ENOENT && optional) + return NULL; if (ret) { dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n", np, mux_name ?: "", index); @@ -484,8 +496,35 @@ struct mux_control *mux_control_get(struct device *dev, const char *mux_name) return &mux_chip->mux[controller]; } + +/** + * mux_control_get() - Get the mux-control for a device. + * @dev: The device that needs a mux-control. + * @mux_name: The name identifying the mux-control. + * + * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno. + */ +struct mux_control *mux_control_get(struct device *dev, const char *mux_name) +{ + return __mux_control_get(dev, mux_name, false); +} EXPORT_SYMBOL_GPL(mux_control_get); +/** + * mux_control_get_optional() - Get the optional mux-control for a device. + * @dev: The device that needs a mux-control. + * @mux_name: The name identifying the mux-control. + * + * Return: NULL if no mux with the provided name is found, a pointer to + * the named mux-control or an ERR_PTR with a negative errno. + */ +struct mux_control * +mux_control_get_optional(struct device *dev, const char *mux_name) +{ + return __mux_control_get(dev, mux_name, true); +} +EXPORT_SYMBOL_GPL(mux_control_get_optional); + /** * mux_control_put() - Put away the mux-control for good. * @mux: The mux-control to put away. @@ -494,6 +533,9 @@ EXPORT_SYMBOL_GPL(mux_control_get); */ void mux_control_put(struct mux_control *mux) { + if (!mux) + return; + put_device(&mux->chip->dev); } EXPORT_SYMBOL_GPL(mux_control_put); @@ -505,16 +547,8 @@ static void devm_mux_control_release(struct device *dev, void *res) mux_control_put(mux); } -/** - * devm_mux_control_get() - Get the mux-control for a device, with resource - * management. - * @dev: The device that needs a mux-control. - * @mux_name: The name identifying the mux-control. - * - * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno. - */ -struct mux_control *devm_mux_control_get(struct device *dev, - const char *mux_name) +static struct mux_control * +__devm_mux_control_get(struct device *dev, const char *mux_name, bool optional) { struct mux_control **ptr, *mux; @@ -522,8 +556,8 @@ struct mux_control *devm_mux_control_get(struct device *dev, if (!ptr) return ERR_PTR(-ENOMEM); - mux = mux_control_get(dev, mux_name); - if (IS_ERR(mux)) { + mux = __mux_control_get(dev, mux_name, optional); + if (IS_ERR_OR_NULL(mux)) { devres_free(ptr); return mux; } @@ -533,8 +567,38 @@ struct mux_control *devm_mux_control_get(struct device *dev, return mux; } + +/** + * devm_mux_control_get() - Get the mux-control for a device, with resource + * management. + * @dev: The device that needs a mux-control. + * @mux_name: The name identifying the mux-control. + * + * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno. + */ +struct mux_control * +devm_mux_control_get(struct device *dev, const char *mux_name) +{ + return __devm_mux_control_get(dev, mux_name, false); +} EXPORT_SYMBOL_GPL(devm_mux_control_get); +/** + * devm_mux_control_get_optional() - Get the optional mux-control for a device, + * with resource management. + * @dev: The device that needs a mux-control. + * @mux_name: The name identifying the mux-control. + * + * Return: NULL if no mux with the provided name is found, a pointer to + * the named mux-control or an ERR_PTR with a negative errno. + */ +struct mux_control * +devm_mux_control_get_optional(struct device *dev, const char *mux_name) +{ + return __devm_mux_control_get(dev, mux_name, true); +} +EXPORT_SYMBOL_GPL(devm_mux_control_get_optional); + /* * Using subsys_initcall instead of module_init here to try to ensure - for * the non-modular case - that the subsystem is initialized when mux consumers diff --git a/include/linux/mux/consumer.h b/include/linux/mux/consumer.h index ea96d4c82be7..fc98547bf494 100644 --- a/include/linux/mux/consumer.h +++ b/include/linux/mux/consumer.h @@ -26,9 +26,13 @@ int __must_check mux_control_try_select(struct mux_control *mux, int mux_control_deselect(struct mux_control *mux); struct mux_control *mux_control_get(struct device *dev, const char *mux_name); +struct mux_control *mux_control_get_optional(struct device *dev, + const char *mux_name); void mux_control_put(struct mux_control *mux); struct mux_control *devm_mux_control_get(struct device *dev, const char *mux_name); +struct mux_control *devm_mux_control_get_optional(struct device *dev, + const char *mux_name); #endif /* _LINUX_MUX_CONSUMER_H */