From patchwork Tue Jan 29 17:23:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Shearman X-Patchwork-Id: 1032913 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="PygKKGH9"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43ptdg238Cz9sDB for ; Wed, 30 Jan 2019 04:24:07 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728677AbfA2RYG (ORCPT ); Tue, 29 Jan 2019 12:24:06 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:43169 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726374AbfA2RYG (ORCPT ); Tue, 29 Jan 2019 12:24:06 -0500 Received: by mail-wr1-f65.google.com with SMTP id r10so22968735wrs.10 for ; Tue, 29 Jan 2019 09:24:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vix6T066QZJh384btuq6v87Hto9SEwyIWsiIWnMIoRE=; b=PygKKGH9waVB7GunybzaHCRmbXIGbgfPgGz5BEsIfNeD2/m+8b3KvGT5V5QKfiZXDC E33JWMgQ/Ru4ho2IsUtPllmbqvWm6PA+q6V4cfFG3GyPkZLv4ml1QRAUIl3/Xai9vYRS CZaHxb2Ifs9ZigCAzt0f9/I+83PBaabf1qUnNTBcDWIJXap6R82Uf+Zi4bIC7+I2n9K+ geGMma/RLp05pXNPxDLuWk+qyG/uj86JdaY4IGqISc5+STVkKyifKg2OCFhO4K/017S4 I3vXiV4hS6c1tIukRAfyVi09KzVzYE75WryJp/IsPBeUX5TMper3Pa8/AXVnhHDp2E/W wT0w== 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:mime-version :content-transfer-encoding; bh=vix6T066QZJh384btuq6v87Hto9SEwyIWsiIWnMIoRE=; b=o+6EPL6LhPk3TqGd2CJK2DWNoo22minD7p1x855389xX8sGCno3SRrdyXYMIPsW7dm qR98ppo8gZdmHvOOCAZM/7wHqiauSqnhtZMaEudy0l2TOgS0gtC9wHxk0N2nGL114Mu/ 1u5+gWmhMfjxTa26kQpQ5TH9XHgkWdugtLtAu0nCmf7Z1tyV3xrrlO+RlsB+EI6ao4L4 ci+NEuUHKLQ35WS1Y5uH5zg1jw6tLyN7xnwUgo0fFDpUCZftXzgnBU7bWn2jhCc/D7cD 9058k9gOUWykjoR/q+vmDOPVLFmktLQpLCDObH+97C8G/ZQbbbaZY6zYIBgz5zsYXNQg D7sA== X-Gm-Message-State: AJcUukfDnqJW9Kcm/qD0/AZcHixJibwdPrDokbo5G9KjknAsjC8aphQk uSeg3XOmMQ/adLDsl2z+t9U= X-Google-Smtp-Source: ALg8bN71BcivzH+u8fuuPh6fwAg53G/X3cZ2cDKKTPiM5KuRiE9UKR8vqUW4Bil23n7eBEVJhz4LwQ== X-Received: by 2002:adf:d089:: with SMTP id y9mr27660993wrh.22.1548782643331; Tue, 29 Jan 2019 09:24:03 -0800 (PST) Received: from uks1.vyatta.net ([137.221.143.78]) by smtp.gmail.com with ESMTPSA id a132sm6783929wmh.5.2019.01.29.09.24.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Jan 2019 09:24:02 -0800 (PST) From: Robert Shearman To: Peter Rosin Cc: linux-i2c@vger.kernel.org, Robert Shearman Subject: [PATCH v2] i2c: mux: pca954x: allow management of device idle state via sysfs Date: Tue, 29 Jan 2019 17:23:51 +0000 Message-Id: <20190129172352.24466-1-robertshearman@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org From: Robert Shearman The behaviour, by default, to not deselect after each transfer is unsafe when there is a device with an address that conflicts with another device on another pca954x mux on the same parent bus, and it may not be convenient to use the platform data or devicetree to set the deselect mux, e.g. when running on x86_64 when ACPI is used to discover most of the device hierarchy. Therefore, provide the ability to set the idle state behaviour using a new sysfs file, idle_state as a complement to the method of instantiating the device via sysfs. The possible behaviours are disconnect, i.e. to deselect all channels from the mux, as-is (the default), i.e. leave the last channel selected, and set a predetermined channel. The current behaviour matches most closely with as-is, although the driver platform data can be used to select a mix of disconnect and as-is on a per-channel basis, and this capability is preserved. Signed-off-by: Robert Shearman --- Changes in v2: - change from exposing deselect mask to idle state with more options and less implementation specific - fix style issues - don't fail the create of the mux device if the device attribute file in sysfs failed to create .../ABI/testing/sysfs-bus-i2c-devices-pca954x | 20 ++++ drivers/i2c/muxes/i2c-mux-pca954x.c | 91 ++++++++++++++++++- 2 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-i2c-devices-pca954x diff --git a/Documentation/ABI/testing/sysfs-bus-i2c-devices-pca954x b/Documentation/ABI/testing/sysfs-bus-i2c-devices-pca954x new file mode 100644 index 000000000000..bb1c2e538c5f --- /dev/null +++ b/Documentation/ABI/testing/sysfs-bus-i2c-devices-pca954x @@ -0,0 +1,20 @@ +What: /sys/bus/i2c/.../idle_state +Date: January 2019 +KernelVersion: 5.0 +Contact: Robert Shearman +Description: + Value that can be written to control the behaviour of + the multiplexer on idle. Possible values: + -2 - disconnect on idle, i.e. deselect the last used + channel, which is useful when there is a device + with an address that conflicts with another + device on another pca954x mux on the same parent + bus. + -1 - leave the mux as-is, which is the most optimal + setting in terms of I2C operations and is the + default mode. + 0.. - set the mux to a predetermined channel, + which is useful if there is one channel that is + used almost always, and you want to reduce the + latency for normal operations after rare + transactions on other channels diff --git a/drivers/i2c/muxes/i2c-mux-pca954x.c b/drivers/i2c/muxes/i2c-mux-pca954x.c index bfabf985e830..c032396abdcc 100644 --- a/drivers/i2c/muxes/i2c-mux-pca954x.c +++ b/drivers/i2c/muxes/i2c-mux-pca954x.c @@ -50,6 +50,7 @@ #include #include #include +#include #define PCA954X_MAX_NCHANS 8 @@ -86,6 +87,9 @@ struct pca954x { u8 last_chan; /* last register value */ u8 deselect; + /* MUX_IDLE_AS_IS, MUX_IDLE_DISCONNECT or >= for channel */ + s8 idle_state; + struct i2c_client *client; struct irq_domain *irq; @@ -250,19 +254,81 @@ static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan) return ret; } -static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan) +static int __pca954x_deselect_mux(struct i2c_mux_core *muxc) { struct pca954x *data = i2c_mux_priv(muxc); struct i2c_client *client = data->client; - if (!(data->deselect & (1 << chan))) - return 0; + if (data->idle_state >= 0) + /* Set the mux back to a predetermined channel */ + return pca954x_select_chan(muxc, data->idle_state); /* Deselect active channel */ data->last_chan = 0; return pca954x_reg_write(muxc->parent, client, data->last_chan); } +static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan) +{ + struct pca954x *data = i2c_mux_priv(muxc); + + if (!(data->deselect & (1 << chan))) + return 0; + + return __pca954x_deselect_mux(muxc); +} + +static ssize_t idle_state_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct i2c_client *client = to_i2c_client(dev); + struct i2c_mux_core *muxc = i2c_get_clientdata(client); + struct pca954x *data = i2c_mux_priv(muxc); + + return sprintf(buf, "%d\n", READ_ONCE(data->idle_state)); +} + +static ssize_t idle_state_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct i2c_client *client = to_i2c_client(dev); + struct i2c_mux_core *muxc = i2c_get_clientdata(client); + struct pca954x *data = i2c_mux_priv(muxc); + int val; + int ret; + + ret = kstrtoint(buf, 0, &val); + if (ret < 0) + return ret; + + if (val != MUX_IDLE_AS_IS && val != MUX_IDLE_DISCONNECT && + (val < 0 || val >= data->chip->nchans)) + return -EINVAL; + + i2c_lock_bus(muxc->parent, I2C_LOCK_SEGMENT); + + WRITE_ONCE(data->idle_state, val); + if (val == MUX_IDLE_AS_IS) + data->deselect = 0; + else { + data->deselect = GENMASK(data->chip->nchans - 1, 0); + /* + * Set the mux into a state consistent with the new + * idle_state. + */ + if (data->last_chan || val != MUX_IDLE_DISCONNECT) + ret = __pca954x_deselect_mux(muxc); + } + + i2c_unlock_bus(muxc->parent, I2C_LOCK_SEGMENT); + + return ret < 0 ? ret : count; +} + +static DEVICE_ATTR_RW(idle_state); + static irqreturn_t pca954x_irq_handler(int irq, void *dev_id) { struct pca954x *data = dev_id; @@ -329,8 +395,11 @@ static int pca954x_irq_setup(struct i2c_mux_core *muxc) static void pca954x_cleanup(struct i2c_mux_core *muxc) { struct pca954x *data = i2c_mux_priv(muxc); + struct i2c_client *client = data->client; int c, irq; + device_remove_file(&client->dev, &dev_attr_idle_state); + if (data->irq) { for (c = 0; c < data->chip->nchans; c++) { irq = irq_find_mapping(data->irq, c); @@ -444,6 +513,16 @@ static int pca954x_probe(struct i2c_client *client, goto fail_cleanup; } + if (data->deselect == GENMASK(data->chip->nchans - 1, 0)) + data->idle_state = MUX_IDLE_DISCONNECT; + else + /* + * Some channels may be deselected, but this is safer + * than claiming idle-disconnect behaviour when + * inspected. + */ + data->idle_state = MUX_IDLE_AS_IS; + if (data->irq) { ret = devm_request_threaded_irq(dev, data->client->irq, NULL, pca954x_irq_handler, @@ -453,6 +532,12 @@ static int pca954x_probe(struct i2c_client *client, goto fail_cleanup; } + /* + * The attr probably isn't going to be needed in most cases, + * so don't fail completely on error. + */ + device_create_file(dev, &dev_attr_idle_state); + dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n", num, data->chip->muxtype == pca954x_ismux ? "mux" : "switch", client->name);