Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752433AbbHNT24 (ORCPT ); Fri, 14 Aug 2015 15:28:56 -0400 Received: from mail-by2on0122.outbound.protection.outlook.com ([207.46.100.122]:42144 "EHLO na01-by2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751349AbbHNT2x (ORCPT ); Fri, 14 Aug 2015 15:28:53 -0400 X-Greylist: delayed 99043 seconds by postgrey-1.27 at vger.kernel.org; Fri, 14 Aug 2015 15:28:53 EDT Authentication-Results: spf=fail (sender IP is 192.88.168.50) smtp.mailfrom=freescale.com; nokia.com; dkim=none (message not signed) header.d=none; From: York Sun To: CC: , , York Sun , Paul Bolle , Peter Korsgaard , Alexander Sverdlin Subject: [Patch v4] driver/i2c/mux: Add register-based mux i2c-mux-reg Date: Fri, 14 Aug 2015 12:28:19 -0700 Message-ID: <1439580499-23710-1-git-send-email-yorksun@freescale.com> X-Mailer: git-send-email 1.7.9.5 X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1;BL2FFO11OLC005;1:9tINS/JrcLzYAnfhVvaN2rWIlkk/a/hmftSs+nGaXjZ1QsEdZViSDESwPEWgGZ0n+TaVDXGUg8v8IVtnl3k4XlvK302R5x0qG5qfdyGnbQlGKFRTPjVu+kfcgCDlfO0m/5S2hULD8JalIJQlS8PFN6O4iaN8EeL9YBd5d8dzNXVvnP5Q6Mp57BMGdzMAukpzAshmnJCoDcETUMEkmA67webiBPHuKyNx2qnPAIAWy4Q+GHwcDZvfPGhYeqxg1euN69FyFxgzeDGtDmc/u0IMWFOEoVij6PiDDmTdSnagBvZplzC605dBUSUyx2+sKDOK9iwI0QbFpqfwEUzWTk1+ED59rQnyohXGbZ4upwJKtMpHXzxJLh45x82b7FMPF0g+mGpjTSTD3aIUepPHURG1Aw== X-Forefront-Antispam-Report: CIP:192.88.168.50;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10019020)(6009001)(2980300002)(339900001)(3050300001)(199003)(189002)(479174004)(54534003)(19580405001)(47776003)(229853001)(6806004)(104016003)(50226001)(19580395003)(230783001)(48376002)(64706001)(106466001)(2351001)(46102003)(33646002)(81156007)(50986999)(4001540100001)(5001860100001)(87936001)(50466002)(97736004)(5001830100001)(5001920100001)(86362001)(105606002)(5003940100001)(68736005)(77156002)(77096005)(92566002)(36756003)(5001960100002)(110136002)(189998001)(85426001)(62966003)(2004002);DIR:OUT;SFP:1102;SCL:1;SRVR:BLUPR03MB152;H:tx30smr01.am.freescale.net;FPR:;SPF:Fail;PTR:InfoDomainNonexistent;A:1;MX:1;LANG:en; MIME-Version: 1.0 Content-Type: text/plain X-Microsoft-Exchange-Diagnostics: 1;BLUPR03MB152;2:F3v4Y07/bzXPVJk4tKfrBtqkGSH1xf+AWdVNxCDHrPE2TdR+/KpwySXHF2/wsxtj+gt9kntmnMwIKrigz+LxV0wSh2O5/M6jjVk72EmH44NE37Y1Ojmg2HcUKnufxNKfYmgmW31u/+fsjSb9+kCoh+qzbBIL+8/HG0jbQpcK8hg=;3:tcBvsGD1tIYw8m0woziCo+wqe5zfADLW0pSzLfhn3/DLSt8e5B1pfSr2bLuA9GOCIhOMmyMDIt+efd3ipVXz6SnBtyUgUKvLCATwxt6ExFkHX1DzS5t2kiB+DBJQ7hnTpCw7r3oPHLsfEEyOZxo0bK0c4NKGptG1BVNnlgFspts8NcAA0k/ScCTJXvLe0oKUE+h1I2/GnPj86TnAGK78MerJTnxMZtsV3S9Mth63mIA=;25:j03SL2fyQBS5gsXIS5pzmwpWWUw1psQ7l5BaDkRbx1JRYbZKnNw6SF9oGLhBRJE3Vd2KHlFebomYTtP5dZ8MSAMegUuvhgb8TkquSgPNf7dqKDcSfcfUKmDpyDmTt2ryzYEzomA+dcnRnw+O4Ky4+SSkKn8oAFwij1E4+CxDihnKzuZyNLa9BscDfpS9+9YRC8d/qKjlrRfaX2baCUhT9aKjC4cBR5wNjmQom7VIVzQ0aSRKGTXLA3XkZoS0yWfA X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BLUPR03MB152; X-Microsoft-Exchange-Diagnostics: 1;BLUPR03MB152;20:V/AelhT0zGXd9ikIXxRh0c5vzLRPaJpEVHox0n0AvmqWX6rM1WVn+aNLty/fI6goq7UymJ3pFfWzwiokjNnXAufL8xmsXiPntc90udYZnBnzOm2nuTxk9CJHoavBMPkpDo8h40nMCNMXcZkgVbtwKoLxliK1YPfcSxQ3QJBPqzfSoFNTUSLIRmZR6rHArMPg5oMLTgNIbbACo+igtVbAfowdB/Hc885aHQDxgqVVDnmNDGT7fCUvXc2+6cX21EMNYwNsKz2Uud89oJ6VaWj7C1cP9IFbZYhxmR2eyNb3QddP67MPjVVFTCEstckgwRIUQVeFIH5yJ11/aWdbPUQOfQXBCINQ6xN0RsgzGgnm9p0=;4:QCmEbN3hFzhTohz2M/raY8jjM8DV1Boyhf0gpKtB++abL2/RWQiZLbS25jRwld86bpnhYtolbHlfJt9THjXHl27BGGkLq3zGCyB8qhpP/bDL4ElRQsGNBL7aA5tGQkZ2DxHUsjAIB0wZrfXv9zVXcTOG5vlcZT6Z5wSUxHwO38gteWpQBD/Y6i82p5Qlz5H/3H/MQ2XQKcK035SaTGAT4p0kW4MQSox1wxVIrz4o/0RL/fUsnFN6JBPeJT0bOMYV+wugkSvH/6aQ/eXxjWIRjV1xTw+bvWbUmYzI3qddzi8= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(5005006)(3002001);SRVR:BLUPR03MB152;BCL:0;PCL:0;RULEID:;SRVR:BLUPR03MB152; X-Forefront-PRVS: 066898046A X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;BLUPR03MB152;23:/7KV1MUREszSmB7WjqevAmTwfj6xr6kkZTpNGmXCoG?= =?us-ascii?Q?LB2UcJGaPR7rPWaThIZaL7mcczPNIrAtTb5iLuYDFrXuIn0Fol6qe1KGigT+?= =?us-ascii?Q?c+sYls6bD9E+AuA/Mf3Dt1QYWXGE836NfFpgHIa+zuXE19z9Z2C71SApJzlu?= =?us-ascii?Q?+dopKpHahPcgPZuW6i/jjq6Ww2Sl94wBqvodj1QcAMBIPkS4Vu8EJ6WVV4kR?= =?us-ascii?Q?kT+Q/jIKFclgL79N471cFcYdMYGbN4LRAh2EoWuqqI0Eb+H69FmKcUpeNS9a?= =?us-ascii?Q?D8RGkahEVGStcUt834+Lu0WROE/1R+i5mxKAUF7Viztu+rwzEMl4sA/3xU0m?= =?us-ascii?Q?pDOiNIjD6xCCBaZAK2mSHfSMUx8oTajXbrvxeCX/xsfUJzSmSF3rXjjT5heZ?= =?us-ascii?Q?fwUxUFYERC8rBOOEQg4nl0BYM5NQdbOY6zNDi/SAUgRbhd65bOO7F7b0ZPFo?= =?us-ascii?Q?PbiapPnV/QnYdQcCil2e0/gTnJoaLhyFOGI1wag4+4FRY/GCED3Rk1uUAamA?= =?us-ascii?Q?X1ftmJalUHKw71s7X+P39/+E9KcpexHQn2MxDCfDYnpPOwR9zzmKfFeSLdvX?= =?us-ascii?Q?SKCy/eYovUD2FijMj/XAFAas9rTFmQEqqiTYt9vWNtX1dqdvgMgcAkz6CGQh?= =?us-ascii?Q?4rqaBytVLSIMSMLRnb+txYRVaETogPU0nea+Q9kPMHPzi7sPZV6Qit+3FZTO?= =?us-ascii?Q?I2EksY3L0zz77jlrw35vn34ghnMWmrCzuASIWGF15QgvM3gqEl8+eOGLUUyd?= =?us-ascii?Q?Lj8ka9a/0vtKG+0+ahnLfPmBNFArIC6/I+hvgPam4OheEEOLhPmh4yEncm/I?= =?us-ascii?Q?QpOFYJP6HhH8LpiBc2G73L0+wsq90BdKt3lwfeyWQ6MX3kbit1kSvXCZbnpQ?= =?us-ascii?Q?7zlRUJ8MRFMwSAXVDGutWO+LOHFLyTLwbfUDc7I88XFE5JthdRjqp5g3TX4w?= =?us-ascii?Q?X4WFXpsMFaBFbYq2A5ln16mtlKEpg4Q7/sMBy8pDN/8LfXvOewwh99nNsvJl?= =?us-ascii?Q?wd9FOUEnjeLzUzSO6bj2zdZlEBCbgOyzhOA+9xRl7Vtl1IaHzQFP+a9zRn32?= =?us-ascii?Q?iXuB2E7t59WKxOCfVcub5SY/mE9xbnksevwQaJw7n7GINm1JMofpo87vkYRw?= =?us-ascii?Q?aD2DcSEdXxcRz1DX+l4PaBP7AKKqSgp3APYgx4FnFGWfNQjikXqJiaNsQGUS?= =?us-ascii?Q?XwGen/pRgyY/4=3D?= X-Microsoft-Exchange-Diagnostics: 1;BLUPR03MB152;5:eyKE9Et3yvJUk553+3V0gr8dgbnWdcimrZuawGqmXkKYdMlYwvF0IVYvxeBY7If+Pyj7R8poI1XnGeGoemcUe0SF4UlKNL/3Ht3eT2HLSh/Eecxkhrm1n7fsMttFTLuR6uvHlFQdWXSzuC0OwOlD9A==;24:Zi/moOwD1Ai7TM29sa52Zq+AArevEdd2dS2EUykefdJq2+7U/H8kLBNiXcovnqnHulgrrA9nvdBoiPf+ZU61R2rYrEJ3t6wQJBzj+LoMmF8=;20:aZTrYL7yvHxeA76MA6XPgEWIilJegwMU7kgzGaGzoJLKnJ1u+52nbPYu30lhG/vLDN09IPLm2/dl+U1Fkfn4KA== X-OriginatorOrg: freescale.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2015 19:28:49.0330 (UTC) X-MS-Exchange-CrossTenant-Id: 710a03f5-10f6-4d38-9ff4-a80b81da590d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=710a03f5-10f6-4d38-9ff4-a80b81da590d;Ip=[192.88.168.50];Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR03MB152 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 15703 Lines: 519 Based on i2c-mux-gpio driver, similarly the register-based mux switch from one bus to another by setting a single register. The register can be on PCIe bus, local bus, or any memory-mapped address. The endianness of such register can be specified in device tree if used, or in platform data. Signed-off-by: York Sun CC: Wolfram Sang CC: Paul Bolle CC: Peter Korsgaard CC: Alexander Sverdlin --- Change log: v4: Rename no-read to write-only Revise binding document Revise endianness checking Reorder in Kconfig and Makefile Remove print for deferred probing Remove debug print for unloading driver v3: Add support of both big- and little-endian register Add readback after writing to register Add no-read option. By default, readback is alowed. Fix using chan_id transferred back from i2c-mux. It was mistakenly used as an index. It is actually the data to be written. v2: Update to GPLv2+ licence header Use iowrite instead of direct dereference the pointer to write register Add support of difference register size of 1/2/4 bytes Remove i2c_put_adapter(parent) in probe fucntion Replace multiple dev_info() with dev_dbg() Add idle_in_use variable to gate using idle value Add __iomem for register pointer Move platform data header file to include/linux/platform_data/ .../devicetree/bindings/i2c/i2c-mux-reg.txt | 74 +++++ drivers/i2c/muxes/Kconfig | 11 + drivers/i2c/muxes/Makefile | 1 + drivers/i2c/muxes/i2c-mux-reg.c | 298 ++++++++++++++++++++ include/linux/platform_data/i2c-mux-reg.h | 44 +++ 5 files changed, 428 insertions(+) create mode 100644 Documentation/devicetree/bindings/i2c/i2c-mux-reg.txt create mode 100644 drivers/i2c/muxes/i2c-mux-reg.c create mode 100644 include/linux/platform_data/i2c-mux-reg.h diff --git a/Documentation/devicetree/bindings/i2c/i2c-mux-reg.txt b/Documentation/devicetree/bindings/i2c/i2c-mux-reg.txt new file mode 100644 index 0000000..ecc5323 --- /dev/null +++ b/Documentation/devicetree/bindings/i2c/i2c-mux-reg.txt @@ -0,0 +1,74 @@ +Register-based I2C Bus Mux + +This binding describes an I2C bus multiplexer that uses a single register +to route the I2C signals. + +Required properties: +- compatible: i2c-mux-reg +- i2c-parent: The phandle of the I2C bus that this multiplexer's master-side + port is connected to. +* Standard I2C mux properties. See mux.txt in this directory. +* I2C child bus nodes. See mux.txt in this directory. + +Optional properties: +- reg: this pair of specifies the register to control the mux. + The depends on its parent node. It can be any memory-mapped + address. The size must be either 1, 2, or 4 bytes. If reg is omitted, the + resource of this device will be used. +- little-endian: The existence indicates the register is in little endian. +- big-endian: The existence indicates the register is in big endian. + If both little-endian and big-endian are omitted, the endianness of the + CPU will be used. +- write-only: The existence indicates reading the register is write-only. +- idle-state: value to set the muxer to when idle. When no value is + given, it defaults to the last value used. + +Whenever an access is made to a device on a child bus, the value set +in the revelant node's reg property will be output to the register. + +If an idle state is defined, using the idle-state (optional) property, +whenever an access is not being made to a device on a child bus, the +register will be set according to the idle value. + +If an idle state is not defined, the most recently used value will be +left programmed into the register. + +Example of a mux on PCIe card, the host is a powerpc SoC (big endian): + + i2c-mux { + /* the depends on the address translation + * of the parent device. If omitted, device resource + * will be used instead. The size is to determine + * whether iowrite32, iowrite16, or iowrite8 will be used. + */ + reg = <0x6028 0x4>; + little-endian; /* little endian register on PCIe */ + compatible = "i2c-mux-reg"; + #address-cells = <1>; + #size-cells = <0>; + i2c-parent = <&i2c1>; + i2c@0 { + reg = <0>; + #address-cells = <1>; + #size-cells = <0>; + + si5338: clock-generator@70 { + compatible = "silabs,si5338"; + reg = <0x70>; + /* other stuff */ + }; + }; + + i2c@1 { + /* data is written using iowrite32 */ + reg = <1>; + #address-cells = <1>; + #size-cells = <0>; + + si5338: clock-generator@70 { + compatible = "silabs,si5338"; + reg = <0x70>; + /* other stuff */ + }; + }; + }; diff --git a/drivers/i2c/muxes/Kconfig b/drivers/i2c/muxes/Kconfig index f6d313e..e48881a 100644 --- a/drivers/i2c/muxes/Kconfig +++ b/drivers/i2c/muxes/Kconfig @@ -60,4 +60,15 @@ config I2C_MUX_PINCTRL This driver can also be built as a module. If so, the module will be called pinctrl-i2cmux. +config I2C_MUX_REG + tristate "Register-based I2C multiplexer" + help + If you say yes to this option, support will be included for a + register based I2C multiplexer. This driver provides access to + I2C busses connected through a MUX, which is controlled + by a single register. + + This driver can also be built as a module. If so, the module + will be called i2c-mux-reg. + endmenu diff --git a/drivers/i2c/muxes/Makefile b/drivers/i2c/muxes/Makefile index 465778b..e89799b 100644 --- a/drivers/i2c/muxes/Makefile +++ b/drivers/i2c/muxes/Makefile @@ -7,5 +7,6 @@ obj-$(CONFIG_I2C_MUX_GPIO) += i2c-mux-gpio.o obj-$(CONFIG_I2C_MUX_PCA9541) += i2c-mux-pca9541.o obj-$(CONFIG_I2C_MUX_PCA954x) += i2c-mux-pca954x.o obj-$(CONFIG_I2C_MUX_PINCTRL) += i2c-mux-pinctrl.o +obj-$(CONFIG_I2C_MUX_REG) += i2c-mux-reg.o ccflags-$(CONFIG_I2C_DEBUG_BUS) := -DDEBUG diff --git a/drivers/i2c/muxes/i2c-mux-reg.c b/drivers/i2c/muxes/i2c-mux-reg.c new file mode 100644 index 0000000..39791d5 --- /dev/null +++ b/drivers/i2c/muxes/i2c-mux-reg.c @@ -0,0 +1,298 @@ +/* + * I2C multiplexer using a single register + * + * Copyright 2015 Freescale Semiconductor + * York Sun + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct regmux { + struct i2c_adapter *parent; + struct i2c_adapter **adap; /* child busses */ + struct i2c_mux_reg_platform_data data; +}; + +static int i2c_mux_reg_set(const struct regmux *mux, unsigned int chan_id) +{ + if (!mux->data.reg) + return -EINVAL; + + switch (mux->data.reg_size) { + case 4: + if (mux->data.little_endian) { + iowrite32(chan_id, mux->data.reg); + if (!mux->data.write_only) + ioread32(mux->data.reg); + } else { + iowrite32be(chan_id, mux->data.reg); + if (!mux->data.write_only) + ioread32(mux->data.reg); + } + break; + case 2: + if (mux->data.little_endian) { + iowrite16(chan_id, mux->data.reg); + if (!mux->data.write_only) + ioread16(mux->data.reg); + } else { + iowrite16be(chan_id, mux->data.reg); + if (!mux->data.write_only) + ioread16be(mux->data.reg); + } + break; + case 1: + iowrite8(chan_id, mux->data.reg); + if (!mux->data.write_only) + ioread8(mux->data.reg); + break; + default: + pr_err("Invalid register size\n"); + return -EINVAL; + } + + return 0; +} + +static int i2c_mux_reg_select(struct i2c_adapter *adap, void *data, + unsigned int chan) +{ + struct regmux *mux = data; + + return i2c_mux_reg_set(mux, chan); +} + +static int i2c_mux_reg_deselect(struct i2c_adapter *adap, void *data, + unsigned int chan) +{ + struct regmux *mux = data; + + if (mux->data.idle_in_use) + return i2c_mux_reg_set(mux, mux->data.idle); + + return 0; +} + +#ifdef CONFIG_OF +static int i2c_mux_reg_probe_dt(struct regmux *mux, + struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct device_node *adapter_np, *child; + struct i2c_adapter *adapter; + struct resource res; + unsigned *values; + int i = 0; + + if (!np) + return -ENODEV; + + adapter_np = of_parse_phandle(np, "i2c-parent", 0); + if (!adapter_np) { + dev_err(&pdev->dev, "Cannot parse i2c-parent\n"); + return -ENODEV; + } + adapter = of_find_i2c_adapter_by_node(adapter_np); + if (!adapter) + return -EPROBE_DEFER; + + mux->parent = adapter; + mux->data.parent = i2c_adapter_id(adapter); + put_device(&adapter->dev); + + mux->data.n_values = of_get_child_count(np); + if (of_find_property(np, "little-endian", NULL)) { + mux->data.little_endian = true; + } else if (of_find_property(np, "big-endian", NULL)) { + mux->data.little_endian = false; + } else { +#if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : \ + defined(__LITTLE_ENDIAN) + mux->data.little_endian = true; +#elif defined(__BYTE_ORDER) ? __BYTE_ORDER == __BIG_ENDIAN : \ + defined(__BIG_ENDIAN) + mux->data.little_endian = false; +#else +#error Endianness not defined? +#endif + } + if (of_find_property(np, "write-only", NULL)) + mux->data.write_only = true; + else + mux->data.write_only = false; + + values = devm_kzalloc(&pdev->dev, + sizeof(*mux->data.values) * mux->data.n_values, + GFP_KERNEL); + if (!values) { + dev_err(&pdev->dev, "Cannot allocate values array"); + return -ENOMEM; + } + + for_each_child_of_node(np, child) { + of_property_read_u32(child, "reg", values + i); + i++; + } + mux->data.values = values; + + if (!of_property_read_u32(np, "idle-state", &mux->data.idle)) + mux->data.idle_in_use = true; + + /* map address from "reg" if exists */ + if (of_address_to_resource(np, 0, &res)) { + mux->data.reg_size = resource_size(&res); + if (mux->data.reg_size > 4) { + dev_err(&pdev->dev, "Invalid address size\n"); + return -EINVAL; + } + mux->data.reg = devm_ioremap_resource(&pdev->dev, &res); + if (IS_ERR(mux->data.reg)) + return PTR_ERR(mux->data.reg); + } + + return 0; +} +#else +static int i2c_mux_reg_probe_dt(struct gpiomux *mux, + struct platform_device *pdev) +{ + return 0; +} +#endif + +static int i2c_mux_reg_probe(struct platform_device *pdev) +{ + struct regmux *mux; + struct i2c_adapter *parent; + struct resource *res; + int (*deselect)(struct i2c_adapter *, void *, u32); + unsigned int initial_state, class; + int i, ret, nr; + + mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); + if (!mux) + return -ENOMEM; + + platform_set_drvdata(pdev, mux); + + if (dev_get_platdata(&pdev->dev)) { + memcpy(&mux->data, dev_get_platdata(&pdev->dev), + sizeof(mux->data)); + + parent = i2c_get_adapter(mux->data.parent); + if (!parent) + return -EPROBE_DEFER; + + mux->parent = parent; + } else { + ret = i2c_mux_reg_probe_dt(mux, pdev); + if (ret < 0) { + dev_err(&pdev->dev, "Error parsing device tree"); + return ret; + } + } + + if (!mux->data.reg) { + dev_info(&pdev->dev, + "Register not set, using platform resource\n"); + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + mux->data.reg_size = resource_size(res); + if (mux->data.reg_size > 4) { + dev_err(&pdev->dev, "Invalid resource size\n"); + return -EINVAL; + } + mux->data.reg = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(mux->data.reg)) + return PTR_ERR(mux->data.reg); + } + + mux->adap = devm_kzalloc(&pdev->dev, + sizeof(*mux->adap) * mux->data.n_values, + GFP_KERNEL); + if (!mux->adap) { + dev_err(&pdev->dev, "Cannot allocate i2c_adapter structure"); + return -ENOMEM; + } + + if (mux->data.idle_in_use) { + initial_state = mux->data.idle; + deselect = i2c_mux_reg_deselect; + } else { + initial_state = mux->data.values[0]; + deselect = NULL; + } + + for (i = 0; i < mux->data.n_values; i++) { + nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0; + class = mux->data.classes ? mux->data.classes[i] : 0; + + mux->adap[i] = i2c_add_mux_adapter(mux->parent, &pdev->dev, mux, + nr, mux->data.values[i], + class, i2c_mux_reg_select, + deselect); + if (!mux->adap[i]) { + ret = -ENODEV; + dev_err(&pdev->dev, "Failed to add adapter %d\n", i); + goto add_adapter_failed; + } + } + + dev_dbg(&pdev->dev, "%d port mux on %s adapter\n", + mux->data.n_values, mux->parent->name); + + return 0; + +add_adapter_failed: + for (; i > 0; i--) + i2c_del_mux_adapter(mux->adap[i - 1]); + + return ret; +} + +static int i2c_mux_reg_remove(struct platform_device *pdev) +{ + struct regmux *mux = platform_get_drvdata(pdev); + int i; + + for (i = 0; i < mux->data.n_values; i++) + i2c_del_mux_adapter(mux->adap[i]); + + i2c_put_adapter(mux->parent); + + return 0; +} + +static const struct of_device_id i2c_mux_reg_of_match[] = { + { .compatible = "i2c-mux-reg", }, + {}, +}; +MODULE_DEVICE_TABLE(of, i2c_mux_reg_of_match); + +static struct platform_driver i2c_mux_reg_driver = { + .probe = i2c_mux_reg_probe, + .remove = i2c_mux_reg_remove, + .driver = { + .owner = THIS_MODULE, + .name = "i2c-mux-reg", + }, +}; + +module_platform_driver(i2c_mux_reg_driver); + +MODULE_DESCRIPTION("Register-based I2C multiplexer driver"); +MODULE_AUTHOR("York Sun "); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:i2c-mux-reg"); diff --git a/include/linux/platform_data/i2c-mux-reg.h b/include/linux/platform_data/i2c-mux-reg.h new file mode 100644 index 0000000..c68712a --- /dev/null +++ b/include/linux/platform_data/i2c-mux-reg.h @@ -0,0 +1,44 @@ +/* + * I2C multiplexer using a single register + * + * Copyright 2015 Freescale Semiconductor + * York Sun + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + */ + +#ifndef __LINUX_PLATFORM_DATA_I2C_MUX_REG_H +#define __LINUX_PLATFORM_DATA_I2C_MUX_REG_H + +/** + * struct i2c_mux_reg_platform_data - Platform-dependent data for i2c-mux-reg + * @parent: Parent I2C bus adapter number + * @base_nr: Base I2C bus number to number adapters from or zero for dynamic + * @values: Array of value for each channel + * @n_values: Number of multiplexer channels + * @little_endian: Indicating if the register is in little endian + * @write_only: Reading the register is not allowed by hardware + * @classes: Optional I2C auto-detection classes + * @idle: Value to write to mux when idle + * @idle_in_use: indicate if idle value is in use + * @reg: Virtual address of the register to switch channel + * @reg_size: register size in bytes + */ +struct i2c_mux_reg_platform_data { + int parent; + int base_nr; + const unsigned int *values; + int n_values; + bool little_endian; + bool write_only; + const unsigned int *classes; + u32 idle; + bool idle_in_use; + void __iomem *reg; + resource_size_t reg_size; +}; + +#endif /* __LINUX_PLATFORM_DATA_I2C_MUX_REG_H */ -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/