Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753281AbdDCNbS (ORCPT ); Mon, 3 Apr 2017 09:31:18 -0400 Received: from mail-sn1nam01on0082.outbound.protection.outlook.com ([104.47.32.82]:62912 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752463AbdDCNbP (ORCPT ); Mon, 3 Apr 2017 09:31:15 -0400 Authentication-Results: spf=pass (sender IP is 137.71.25.57) smtp.mailfrom=analog.com; vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=bestguesspass action=none header.from=analog.com; From: To: , , , , CC: , , , , "Michael Hennerich" Subject: [PATCH v3 1/2] i2c:mux:ltc4306: LTC4306 and LTC4305 I2C multiplexer/switch Date: Mon, 3 Apr 2017 15:31:36 +0200 Message-ID: <1491226297-4970-1-git-send-email-michael.hennerich@analog.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:137.71.25.57;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(6009001)(39410400002)(39400400002)(39850400002)(39450400003)(39840400002)(2980300002)(438002)(199003)(189002)(6306002)(86152003)(54906002)(50986999)(106466001)(4326008)(5003940100001)(38730400002)(107886003)(2876002)(77096006)(47776003)(2906002)(36756003)(33646002)(356003)(966004)(8936002)(8676002)(6666003)(7636002)(50226002)(305945005)(189998001)(1720100001)(50466002)(86362001)(48376002)(5660300001)(562404015);DIR:OUT;SFP:1101;SCL:1;SRVR:BY2PR0301MB1973;H:nwd2mta2.analog.com;FPR:;SPF:Pass;MLV:sfv;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;BY2FFO11OLC004;1:7hqeZ+qVmglXF8rL3kxolc8cIRvx2/wwqWlKSBDNyts4id6/OVL7G2gEqeWv/fqbAHUKiB0ZgAqhth+U/+q/yjnyGID87lPRpV6tq1fQfEmpndf/+RLyKRxaiofikJB+C5VJdLwrlWZIXsx9mTSZJJ2PpTphhP6FTvO72eOMQWECDK7e3kO0Pu5p9OePZ47pzvda03ocRTy5bRoJH/2tvFRg4P9fLyPERiEnSi92S/o8JAQGCNO9BBYvyIISTbg73Oupu+a2nqqqsHgq9Vmg8pJj3bdugGuX/jKcVNSxSL7Kb+uZeN7Vh8J+eYPKbwiDk4tHyNIohBjfAqxR7+p6Y1V3PNrjdqalVYWI2YAhuMCweTSV/zDA72i1b0K5t+gyFM/SS5zKQBuOQCvqswLz66teOmOUJ8fsFb3al4kJEFO9JpZNugatY+EPNLpDBSU05Fni2OPty7GnOHM94PFZh7wGrEqgP+JOs0rXrGv/EqC+vv/kInRFDSUx/OaXVDYQolIx1aVum3uCFe14v+zp/m6IrtEzg75mQTlUxchmvdyb+KS6/lY8w9R3juT36npRhs6CMxLhF5RIVH0SjkzD8Agw/WNwvMhJ0p/rBwXc7L0YFrzBPLDETpUWYcVURaPw X-MS-Office365-Filtering-Correlation-Id: 1d525913-3582-4bae-f6a5-08d47a95b085 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(8251501002)(2017030254075)(201703131423075)(201703031133081)(201702281549075);SRVR:BY2PR0301MB1973; X-Microsoft-Exchange-Diagnostics: 1;BY2PR0301MB1973;3:beQtbQ/uNbb8VUEkFxWDg5jgPKR6BKDWYAiULKgxvpGvuFUkNx/kI+/dsGQrDMq+ShgOdv6HOHQoCa/mN4QWNSB9/7RZs+2FMvx/8go0ZavuaUnHOFTPESR9UXVM/ldIEso585jLi9U5Q7VppPMMA0wyYgCwX2S+NxkldB9b1u+ZfGFsfoaE4S4QqApyYBpWnTZmDgLqbY1UtnUXCh+3Zm+gHfRxEfNZHgtZ5pzz2FkBhtob+CVfp8AIt6M9JOq4KeFzCmstXU9FrU7pnGFxSjdjxwArKrTV0/hz5j6dfBPn3Ayg/+PKtx2iP3DOw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Blck6uXkzS2Lo00COlPKyVuLCKbLFahJd/sTg1wUuLhHbWjx5aVdSjIf/3/yk74un1gHJL4is2sCPKqNEOfmXyIuhs3ePIgyA1vR+SYTqMF5I6IZxvcy9cufe3L6zaYD751lP1eyeK/9p8D6LOVJope5No53UDQaJePQi/vHbU8KuQW8hMHGrZw9PfPhe4acbsK/7UaPSWkhFvUXn3KiWznrf42beYoR3YTDhTzgYkRsS6Lgdk6F320GHDLDyMS/Wht2P82VopF+AFdCROQPR5NDpGvhaLBMiXe+TAB66dKEfRrm5hwCaXWHSRZLoyYdtL5800hPxPEzzjypHF53q5c5xHsgo8NrmZ85c4OG+xEGvDaBcZc/UiV9UoNY/blXwydfsvyT9+G4yTji685PKkv89vOvSSV8rkHgo7Ov5xXFHE6s7gNxxG+jPKgjFtQnSbhUozDUVygIerGjVgGK8tm249Iz3EF2HuHs8rv/H60vxOeMx5XPt+0 X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(9452136761055)(232431446821674)(211171220733660)(170811661138872); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(601004)(2401047)(13017025)(13015025)(13018025)(13024025)(13023025)(8121501046)(5005006)(10201501046)(3002001)(93006095)(93004095)(6055026)(6041248)(201703131423075)(201702281528075)(201703061421075)(20161123562025)(20161123560025)(20161123555025)(20161123564025)(6072148);SRVR:BY2PR0301MB1973;BCL:0;PCL:0;RULEID:;SRVR:BY2PR0301MB1973; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;BY2PR0301MB1973;4:T74wCmdh3a8EbxFY0m+ukNl0YuNAnoASpl+YjXXw?= =?us-ascii?Q?EZgNjRuo3WHWAmfAzXOfT+22rSxynf0OsagrhwhOPEQdVpJvEbIDeAog8Aca?= =?us-ascii?Q?7fNMk4B2bQtt10PoCELKQ4SFQKP6clZA+YjqolD5SIkEl9V7T7AusdjQ3O5+?= =?us-ascii?Q?zE+BgVODfRcYHQXsNBeVHrio4Z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wZzrFVDTqa2w8LYh0qALX?= =?us-ascii?Q?j4hsNRMXAX8RPpAgmNG7yVfC+n9dvGWwUMfkzQBpdqfADOlzDgAd+40BEBVO?= =?us-ascii?Q?rkgo5Z8u/H3Hifi2lH75W6PVmru2VHHRoFLoof8vVRUxAx7VRVhDbaKUV/xH?= =?us-ascii?Q?rYID3giBDW4vLqSRhybJNd4JP?= X-Microsoft-Exchange-Diagnostics: 1;BY2PR0301MB1973;6:Ir2nqMKoo5VbVuQY0eMsY8t4CNbEbUCjXIsjWAHs49rUmGEgqWElrn2BkQwWZCAtsz3/j5U/1YqAD4+PUukvcOKGKNi+nkti4A2P8jWNwgmQY+zDBIvYUyJjOtK5DlBAEMmnjAMzxy/ZX8/wA+F3SoJDMy80lWax5VkaBlhPt532j9lzzNb9NugIsHUwkzTHNG3caGKJafzGlbM/ictxlQjVT/iDIGbtu6dH+6Z/fT/qra8KQzATGyHiWDgYYW2afw9NEZfN97wGCZgcjzgDjrFi17fPjDTeZo4Tg9oEvZCKrcmZX8DHUZ+ITFpD6fMcqatAPI6ZHsFMj/OUJgom4YaKHWNe2fb3UbdV523JCuqs9/KWC9bbBO71MS5pWO9onSpJQ9pxKkeQrNQlDk+MDynoSc394TVTf1nl4L5zF9s=;5:eFJBZRBvqKHEzFGi9x+EeN9EXIyRrwVbUhlcNV65TailjbbVj0YjASTXGFDXDsfbVni3wR00KpvAceGGOwzMOuh7RoIhpwgIwx2Krb09vYaRfPTXmsDM1b3YcrN990VYQIIT6gNb8ZCxyNNbE9sUSQ==;24:ReBr0TMLE/FfGWXyWVkI0Sk4FzKP2e/YxbRk3IVpN2Txdv0TYugC65HE/fGhGTEogLZ20qTOFdg6rk3IdvSb3sosVDjeIkHPifehCYql0J0= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;BY2PR0301MB1973;7:apZuU69OihAvAkKHyF7seCPGU6Jk3Ht9ZiRFrCoOuOk6hrUQi+6GrhcHU3ottLND63TYIuJTu8XdwF1ljkgXZfoEWCaZtIsMk0gTwqe7jDlNn4TGYj5s7C+qFSEyioZx61deMQWx5Ke33TaEndCzzWZxByv0jdjMCeZWvR8YHfv9aDLCtg6D+W6rTy+N8DzflSe91HcX/t2e98QBVR2pXOBFwpapP+7o75n0i056rF1hJsl4ptYmGL6WJ2m7F9Jkwsp3UhHWwk6zS6OQkaGjKRSf0Ynj7RqBSAEwnVExkW/q9iIE9m92mEHqzlA/oBeJxOSLVDbZDRcHnKMrDQiymQ== X-OriginatorOrg: analog.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Apr 2017 13:31:09.3403 (UTC) X-MS-Exchange-CrossTenant-Id: eaa689b4-8f87-40e0-9c6f-7228de4d754a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=eaa689b4-8f87-40e0-9c6f-7228de4d754a;Ip=[137.71.25.57];Helo=[nwd2mta2.analog.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR0301MB1973 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 12652 Lines: 459 From: Michael Hennerich This patch adds support for the Analog Devices / Linear Technology LTC4306 and LTC4305 4/2 Channel I2C Bus Multiplexer/Switches. The LTC4306 optionally provides two general purpose input/output pins (GPIOs) that can be configured as logic inputs, opendrain outputs or push-pull outputs via the generic GPIOLIB framework. Signed-off-by: Michael Hennerich --- Changes since v1: - Sort makefile entries - Sort driver includes - Use proper defines - Miscellaneous coding style fixups - Rename mux select callback - Revise i2c-mux-idle-disconnect handling - Add ENABLE GPIO handling on error and device removal. - Remove surplus of_match_device call. Changes since v2: - Stop double error reporting (i2c_mux_add_adapter) - Change subject - Split dt bindings to separate patch --- MAINTAINERS | 8 + drivers/i2c/muxes/Kconfig | 10 + drivers/i2c/muxes/Makefile | 1 + drivers/i2c/muxes/i2c-mux-ltc4306.c | 363 ++++++++++++++++++++++++++++++++++++ 4 files changed, 382 insertions(+) create mode 100644 drivers/i2c/muxes/i2c-mux-ltc4306.c diff --git a/MAINTAINERS b/MAINTAINERS index c776906..9a27a19 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -7698,6 +7698,14 @@ S: Maintained F: Documentation/hwmon/ltc4261 F: drivers/hwmon/ltc4261.c +LTC4306 I2C MULTIPLEXER DRIVER +M: Michael Hennerich +W: http://ez.analog.com/community/linux-device-drivers +L: linux-i2c@vger.kernel.org +S: Supported +F: drivers/i2c/muxes/i2c-mux-ltc4306.c +F: Documentation/devicetree/bindings/i2c/i2c-mux-ltc4306.txt + LTP (Linux Test Project) M: Mike Frysinger M: Cyril Hrubis diff --git a/drivers/i2c/muxes/Kconfig b/drivers/i2c/muxes/Kconfig index 10b3d17..f501b3b 100644 --- a/drivers/i2c/muxes/Kconfig +++ b/drivers/i2c/muxes/Kconfig @@ -30,6 +30,16 @@ config I2C_MUX_GPIO This driver can also be built as a module. If so, the module will be called i2c-mux-gpio. +config I2C_MUX_LTC4306 + tristate "LTC LTC4306/5 I2C multiplexer" + select GPIOLIB + help + If you say yes here you get support for the LTC LTC4306 or LTC4305 + I2C mux/switch devices. + + This driver can also be built as a module. If so, the module + will be called i2c-mux-ltc4306. + config I2C_MUX_PCA9541 tristate "NXP PCA9541 I2C Master Selector" help diff --git a/drivers/i2c/muxes/Makefile b/drivers/i2c/muxes/Makefile index 9948fa4..ff7618c 100644 --- a/drivers/i2c/muxes/Makefile +++ b/drivers/i2c/muxes/Makefile @@ -6,6 +6,7 @@ obj-$(CONFIG_I2C_ARB_GPIO_CHALLENGE) += i2c-arb-gpio-challenge.o obj-$(CONFIG_I2C_DEMUX_PINCTRL) += i2c-demux-pinctrl.o obj-$(CONFIG_I2C_MUX_GPIO) += i2c-mux-gpio.o +obj-$(CONFIG_I2C_MUX_LTC4306) += i2c-mux-ltc4306.o obj-$(CONFIG_I2C_MUX_MLXCPLD) += i2c-mux-mlxcpld.o obj-$(CONFIG_I2C_MUX_PCA9541) += i2c-mux-pca9541.o obj-$(CONFIG_I2C_MUX_PCA954x) += i2c-mux-pca954x.o diff --git a/drivers/i2c/muxes/i2c-mux-ltc4306.c b/drivers/i2c/muxes/i2c-mux-ltc4306.c new file mode 100644 index 0000000..34ac47d --- /dev/null +++ b/drivers/i2c/muxes/i2c-mux-ltc4306.c @@ -0,0 +1,363 @@ +/* + * Linear Technology LTC4306 and LTC4305 I2C multiplexer/switch + * + * Copyright (C) 2017 Analog Devices Inc. + * + * Licensed under the GPL-2. + * + * Based on: i2c-mux-pca954x.c + * + * Datasheet: http://cds.linear.com/docs/en/datasheet/4306.pdf + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LTC4305_MAX_NCHANS 2 +#define LTC4306_MAX_NCHANS 4 + +#define LTC_REG_STATUS 0x0 +#define LTC_REG_CONFIG 0x1 +#define LTC_REG_MODE 0x2 +#define LTC_REG_SWITCH 0x3 + +#define LTC_DOWNSTREAM_ACCL_EN BIT(6) +#define LTC_UPSTREAM_ACCL_EN BIT(7) + +#define LTC_GPIO_ALL_INPUT 0xC0 + +enum ltc_type { + ltc_4305, + ltc_4306, +}; + +struct chip_desc { + u8 nchans; + u8 num_gpios; +}; + +struct ltc4306 { + struct i2c_client *client; + struct gpio_desc *en_gpio; + struct gpio_chip gpiochip; + const struct chip_desc *chip; + u8 regs[LTC_REG_SWITCH + 1]; +}; + +/* Provide specs for the PCA954x types we know about */ +static const struct chip_desc chips[] = { + [ltc_4305] = { + .nchans = LTC4305_MAX_NCHANS, + }, + [ltc_4306] = { + .nchans = LTC4306_MAX_NCHANS, + .num_gpios = 2, + }, +}; + +static int ltc4306_gpio_get(struct gpio_chip *chip, unsigned int offset) +{ + struct ltc4306 *data = gpiochip_get_data(chip); + int ret = 0; + + if (gpiochip_line_is_open_drain(chip, offset) || + (data->regs[LTC_REG_MODE] & BIT(7 - offset))) { + /* Open Drain or Input */ + ret = i2c_smbus_read_byte_data(data->client, LTC_REG_CONFIG); + if (ret < 0) + return ret; + + return !!(ret & BIT(1 - offset)); + } else { + return !!(data->regs[LTC_REG_CONFIG] & BIT(5 - offset)); + } +} + +static void ltc4306_gpio_set(struct gpio_chip *chip, unsigned int offset, + int value) +{ + struct ltc4306 *data = gpiochip_get_data(chip); + + if (value) + data->regs[LTC_REG_CONFIG] |= BIT(5 - offset); + else + data->regs[LTC_REG_CONFIG] &= ~BIT(5 - offset); + + i2c_smbus_write_byte_data(data->client, LTC_REG_CONFIG, + data->regs[LTC_REG_CONFIG]); +} + +static int ltc4306_gpio_direction_input(struct gpio_chip *chip, + unsigned int offset) +{ + struct ltc4306 *data = gpiochip_get_data(chip); + + data->regs[LTC_REG_MODE] |= BIT(7 - offset); + + return i2c_smbus_write_byte_data(data->client, LTC_REG_MODE, + data->regs[LTC_REG_MODE]); +} + +static int ltc4306_gpio_direction_output(struct gpio_chip *chip, + unsigned int offset, int value) +{ + struct ltc4306 *data = gpiochip_get_data(chip); + + ltc4306_gpio_set(chip, offset, value); + data->regs[LTC_REG_MODE] &= ~BIT(7 - offset); + + return i2c_smbus_write_byte_data(data->client, LTC_REG_MODE, + data->regs[LTC_REG_MODE]); +} + +static int ltc4306_gpio_set_config(struct gpio_chip *chip, + unsigned int offset, unsigned long config) +{ + struct ltc4306 *data = gpiochip_get_data(chip); + + switch (pinconf_to_config_param(config)) { + case PIN_CONFIG_DRIVE_OPEN_DRAIN: + data->regs[LTC_REG_MODE] &= ~BIT(4 - offset); + break; + case PIN_CONFIG_DRIVE_PUSH_PULL: + data->regs[LTC_REG_MODE] |= BIT(4 - offset); + break; + default: + return -ENOTSUPP; + } + + return i2c_smbus_write_byte_data(data->client, LTC_REG_MODE, + data->regs[LTC_REG_MODE]); +} + +static int ltc4306_gpio_init(struct ltc4306 *data) +{ + if (!data->chip->num_gpios) + return 0; + + data->gpiochip.label = dev_name(&data->client->dev); + data->gpiochip.base = -1; + data->gpiochip.ngpio = data->chip->num_gpios; + data->gpiochip.parent = &data->client->dev; + data->gpiochip.can_sleep = true; + data->gpiochip.direction_input = ltc4306_gpio_direction_input; + data->gpiochip.direction_output = ltc4306_gpio_direction_output; + data->gpiochip.get = ltc4306_gpio_get; + data->gpiochip.set = ltc4306_gpio_set; + data->gpiochip.set_config = ltc4306_gpio_set_config; + data->gpiochip.owner = THIS_MODULE; + + /* gpiolib assumes all GPIOs default input */ + data->regs[LTC_REG_MODE] |= LTC_GPIO_ALL_INPUT; + i2c_smbus_write_byte_data(data->client, LTC_REG_MODE, + data->regs[LTC_REG_MODE]); + + return devm_gpiochip_add_data(&data->client->dev, + &data->gpiochip, data); +} + +/* + * Write to chip register. Don't use i2c_transfer()/i2c_smbus_xfer() + * as they will try to lock the adapter a second time. + */ +static int ltc4306_reg_write(struct i2c_adapter *adap, + struct i2c_client *client, u8 reg, u8 val) +{ + int ret; + + if (adap->algo->master_xfer) { + struct i2c_msg msg; + char buf[2]; + + msg.addr = client->addr; + msg.flags = 0; + msg.len = 2; + buf[0] = reg; + buf[1] = val; + msg.buf = buf; + ret = __i2c_transfer(adap, &msg, 1); + } else { + union i2c_smbus_data data; + + data.byte = val; + ret = adap->algo->smbus_xfer(adap, client->addr, + client->flags, + I2C_SMBUS_WRITE, + reg, + I2C_SMBUS_BYTE_DATA, &data); + } + + return ret; +} + +static int ltc4306_select_mux(struct i2c_mux_core *muxc, u32 chan) +{ + struct ltc4306 *data = i2c_mux_priv(muxc); + struct i2c_client *client = data->client; + u8 regval; + int ret = 0; + + regval = BIT(7 - chan); + + /* Only select the channel if its different from the last channel */ + if (data->regs[LTC_REG_SWITCH] != regval) { + ret = ltc4306_reg_write(muxc->parent, client, + LTC_REG_SWITCH, regval); + data->regs[LTC_REG_SWITCH] = ret < 0 ? 0 : regval; + } + + return ret; +} + +static int ltc4306_deselect_mux(struct i2c_mux_core *muxc, u32 chan) +{ + struct ltc4306 *data = i2c_mux_priv(muxc); + struct i2c_client *client = data->client; + + /* Deselect all channels */ + data->regs[LTC_REG_SWITCH] = 0; + + return ltc4306_reg_write(muxc->parent, client, + LTC_REG_SWITCH, data->regs[LTC_REG_SWITCH]); +} + +static const struct i2c_device_id ltc4306_id[] = { + { "ltc4305", ltc_4305 }, + { "ltc4306", ltc_4306 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, ltc4306_id); + +static const struct of_device_id ltc4306_of_match[] = { + { .compatible = "lltc,ltc4305", .data = &chips[ltc_4305] }, + { .compatible = "lltc,ltc4306", .data = &chips[ltc_4306] }, + { } +}; +MODULE_DEVICE_TABLE(of, ltc4306_of_match); + +static int ltc4306_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent); + struct device_node *of_node = client->dev.of_node; + bool idle_disconnect_dt = false; + struct i2c_mux_core *muxc; + struct ltc4306 *data; + int num, ret; + + if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE_DATA)) + return -ENODEV; + + if (of_node) { + idle_disconnect_dt = + of_property_read_bool(of_node, + "i2c-mux-idle-disconnect"); + } + + muxc = i2c_mux_alloc(adap, &client->dev, + LTC4306_MAX_NCHANS, sizeof(*data), 0, + ltc4306_select_mux, idle_disconnect_dt ? + ltc4306_deselect_mux : NULL); + if (!muxc) + return -ENOMEM; + data = i2c_mux_priv(muxc); + + i2c_set_clientdata(client, muxc); + data->client = client; + + /* Enable the mux if an enable GPIO is specified. */ + data->en_gpio = devm_gpiod_get_optional(&client->dev, "enable", + GPIOD_OUT_HIGH); + if (IS_ERR(data->en_gpio)) + return PTR_ERR(data->en_gpio); + + /* + * Write the mux register at addr to verify + * that the mux is in fact present. This also + * initializes the mux to disconnected state. + */ + if (i2c_smbus_write_byte_data(client, LTC_REG_SWITCH, 0) < 0) { + dev_warn(&client->dev, "probe failed\n"); + ret = -ENODEV; + goto gpio_default; + } + + if (of_node) { + data->chip = of_device_get_match_data(&client->dev); + + if (of_property_read_bool(of_node, + "ltc,downstream-accelerators-enable")) + data->regs[LTC_REG_CONFIG] |= LTC_DOWNSTREAM_ACCL_EN; + + if (of_property_read_bool(of_node, + "ltc,upstream-accelerators-enable")) + data->regs[LTC_REG_CONFIG] |= LTC_UPSTREAM_ACCL_EN; + + if (i2c_smbus_write_byte_data(client, LTC_REG_CONFIG, + data->regs[LTC_REG_CONFIG]) < 0) { + dev_warn(&client->dev, "probe failed\n"); + ret = -ENODEV; + goto gpio_default; + } + } else { + data->chip = &chips[id->driver_data]; + } + + ret = ltc4306_gpio_init(data); + if (ret < 0) + goto gpio_default; + + /* Now create an adapter for each channel */ + for (num = 0; num < data->chip->nchans; num++) { + ret = i2c_mux_add_adapter(muxc, 0, num, 0); + if (ret) + goto add_adapter_failed; + } + + dev_info(&client->dev, + "registered %d multiplexed busses for I2C switch %s\n", + num, client->name); + + return 0; + +add_adapter_failed: + i2c_mux_del_adapters(muxc); +gpio_default: + gpiod_direction_input(data->en_gpio); + return ret; +} + +static int ltc4306_remove(struct i2c_client *client) +{ + struct i2c_mux_core *muxc = i2c_get_clientdata(client); + struct ltc4306 *data = i2c_mux_priv(muxc); + + i2c_mux_del_adapters(muxc); + gpiod_direction_input(data->en_gpio); + + return 0; +} + +static struct i2c_driver ltc4306_driver = { + .driver = { + .name = "ltc4306", + .of_match_table = of_match_ptr(ltc4306_of_match), + }, + .probe = ltc4306_probe, + .remove = ltc4306_remove, + .id_table = ltc4306_id, +}; + +module_i2c_driver(ltc4306_driver); + +MODULE_AUTHOR("Michael Hennerich "); +MODULE_DESCRIPTION("Linear Technology LTC4306, LTC4305 I2C mux/switch driver"); +MODULE_LICENSE("GPL v2"); -- 2.7.4