From: Rodolfo Giometti <giometti-k2GhghHVRtY@public.gmane.org>
To: linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Cc: Jean Delvare <khali-PUYAD+kWke1g9hUCZPvPmw@public.gmane.org>,
David Brownell <david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org>,
Kumar Gala
<galak-XVmvHMARGAS8U2dJNN8I7kB+6BGkLq7r@public.gmane.org>,
Peter Korsgaard <jacmet-OfajU3CKLf1/SzgSGea1oA@public.gmane.org>,
Rodolfo Giometti <giometti-k2GhghHVRtY@public.gmane.org>
Subject: [PATCH 6/6] i2c: driver for PCA954x I2C multiplexer series.
Date: Thu, 5 Feb 2009 14:36:13 +0100 [thread overview]
Message-ID: <1233840973-13268-7-git-send-email-giometti@linux.it> (raw)
In-Reply-To: <1233840973-13268-6-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
Signed-off-by: Rodolfo Giometti <giometti-k2GhghHVRtY@public.gmane.org>
---
drivers/i2c/muxes/Kconfig | 9 ++
drivers/i2c/muxes/Makefile | 2 +
drivers/i2c/muxes/pca954x.c | 325 +++++++++++++++++++++++++++++++++++++++++++
include/linux/i2c/pca954x.h | 13 ++
4 files changed, 349 insertions(+), 0 deletions(-)
create mode 100644 drivers/i2c/muxes/pca954x.c
create mode 100644 include/linux/i2c/pca954x.h
diff --git a/drivers/i2c/muxes/Kconfig b/drivers/i2c/muxes/Kconfig
index 95b9bde..72c7055 100644
--- a/drivers/i2c/muxes/Kconfig
+++ b/drivers/i2c/muxes/Kconfig
@@ -5,4 +5,13 @@
menu "Multiplexer I2C Chip support"
depends on I2C && I2C_MUX && EXPERIMENTAL
+config I2CMUX_PCA954x
+ tristate "Philips PCA953x I2C Mux/switches"
+ help
+ If you say yes here you get support for the Philips PCA954x
+ I2C mux/switch devices.
+
+ This driver can also be built as a module. If so, the module
+ will be called pca954x.
+
endmenu
diff --git a/drivers/i2c/muxes/Makefile b/drivers/i2c/muxes/Makefile
index 5e70bde..f752576 100644
--- a/drivers/i2c/muxes/Makefile
+++ b/drivers/i2c/muxes/Makefile
@@ -1,6 +1,8 @@
#
# Makefile for multiplexer I2C chip drivers.
+obj-$(CONFIG_I2CMUX_PCA954x) += pca954x.o
+
ifeq ($(CONFIG_I2C_DEBUG_CHIP),y)
EXTRA_CFLAGS += -DDEBUG
endif
diff --git a/drivers/i2c/muxes/pca954x.c b/drivers/i2c/muxes/pca954x.c
new file mode 100644
index 0000000..5643563
--- /dev/null
+++ b/drivers/i2c/muxes/pca954x.c
@@ -0,0 +1,325 @@
+/*
+ * I2C multiplexer
+ *
+ * Copyright (c) 2008-2009 Rodolfo Giometti <giometti-k2GhghHVRtY@public.gmane.org>
+ * Copyright (c) 2008-2009 Eurotech S.p.A. <info-ymFC7zkAqBI1GQ1Ptb7lUw@public.gmane.org>
+ *
+ * This module supports the PCA954x series of I2C multiplexer/switch chips
+ * made by Philips Semiconductors.
+ * This includes the:
+ * PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547
+ * and PCA9548.
+ *
+ * These chips are all controlled via the I2C bus itself, and all have a
+ * single 8-bit register (normally at 0x70). The upstream "parent" bus fans
+ * out to two, four, or eight downstream busses or channels; which of these
+ * are selected is determined by the chip type and register contents. A
+ * mux can select only one sub-bus at a time; a switch can select any
+ * combination simultaneously.
+ *
+ * Based on:
+ * pca954x.c from Kumar Gala <galak-XVmvHMARGAS8U2dJNN8I7kB+6BGkLq7r@public.gmane.org>
+ * Copyright (C) 2006
+ *
+ * Based on:
+ * pca954x.c from Ken Harrenstien
+ * Copyright (C) 2004 Google, Inc. (Ken Harrenstien)
+ *
+ * Based on:
+ * i2c-virtual_cb.c from Brian Kuschak <bkuschak-/E1597aS9LQAvxtiuMwx3w@public.gmane.org>
+ * and
+ * pca9540.c from Jean Delvare <khali-PUYAD+kWke1g9hUCZPvPmw@public.gmane.org>, which was
+ * based on pcf8574.c from the same project by Frodo Looijaard,
+ * Philip Edelbrock, Dan Eaton and Aurelien Jarno.
+ *
+ * This file is licensed under the terms of the GNU General Public
+ * License version 2. This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#include <linux/module.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+
+#include <linux/i2c/pca954x.h>
+
+#define PCA954X_MAX_NCHANS 8
+
+enum pca_type {
+ pca_9540,
+ pca_9542,
+ pca_9543,
+ pca_9544,
+ pca_9545,
+ pca_9546,
+ pca_9547,
+ pca_9548,
+};
+
+struct pca954x {
+ struct i2c_client *client;
+ struct i2c_client dev;
+ unsigned int type;
+ struct i2c_adapter *virt_adaps[PCA954X_MAX_NCHANS];
+
+ u8 last_chan;
+ unsigned int deselect_on_exit:1;
+ int (*is_chan_connected)(struct i2c_adapter *adap,
+ struct i2c_client *client, u32 chan);
+};
+
+struct chip_desc {
+ u8 nchans;
+ u8 enable; /* used for muxes only */
+ enum muxtype {
+ pca954x_ismux = 0,
+ pca954x_isswi
+ } muxtype;
+};
+
+/* Provide specs for the PCA954x types we know about */
+static struct chip_desc chips[] = {
+ [pca_9540] = {
+ .nchans = 2,
+ .enable = 0x4,
+ .muxtype = pca954x_ismux,
+ },
+ [pca_9542] = {
+ .nchans = 2,
+ .enable = 0x4,
+ .muxtype = pca954x_ismux,
+ },
+ [pca_9543] = {
+ .nchans = 2,
+ .enable = 0x0,
+ .muxtype = pca954x_isswi,
+ },
+ [pca_9544] = {
+ .nchans = 4,
+ .enable = 0x4,
+ .muxtype = pca954x_ismux,
+ },
+ [pca_9542] = {
+ .nchans = 4,
+ .enable = 0x0,
+ .muxtype = pca954x_isswi,
+ },
+ [pca_9542] = {
+ .nchans = 4,
+ .enable = 0x0,
+ .muxtype = pca954x_isswi,
+ },
+ [pca_9542] = {
+ .nchans = 8,
+ .enable = 0x8,
+ .muxtype = pca954x_ismux,
+ },
+ [pca_9542] = {
+ .nchans = 8,
+ .enable = 0x0,
+ .muxtype = pca954x_isswi,
+ },
+};
+
+static const struct i2c_device_id pca954x_id[] = {
+ { "pca9540", pca_9540 },
+ { "pca9542", pca_9542 },
+ { "pca9543", pca_9543 },
+ { "pca9544", pca_9544 },
+ { "pca9545", pca_9545 },
+ { "pca9546", pca_9546 },
+ { "pca9547", pca_9547 },
+ { "pca9548", pca_9548 },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, pca954x_id);
+
+static int pca954x_xfer(struct i2c_adapter *adap,
+ struct i2c_client *client, int read_write, u8 *val)
+{
+ int ret = -ENODEV;
+
+ if (adap->algo->master_xfer) {
+ struct i2c_msg msg;
+ char buf[1];
+
+ msg.addr = client->addr;
+ msg.flags = (read_write == I2C_SMBUS_READ ? I2C_M_RD : 0);
+ msg.len = 1;
+ buf[0] = *val;
+ msg.buf = buf;
+ ret = adap->algo->master_xfer(adap, &msg, 1);
+ } else if (adap->algo->smbus_xfer) {
+ union i2c_smbus_data data;
+ ret = adap->algo->smbus_xfer(adap, client->addr,
+ client->flags & I2C_M_TEN,
+ read_write,
+ *val, I2C_SMBUS_BYTE, &data);
+ }
+
+ return ret;
+}
+
+static int pca954x_select_chan(struct i2c_adapter *adap,
+ void *client, u32 chan)
+{
+ struct pca954x *data = i2c_get_clientdata((struct i2c_client *) client);
+ struct chip_desc *chip = &chips[data->type];
+ u8 regval = 0;
+ int ret = 0;
+
+ /* Callback to check for bus connection */
+ if (data->is_chan_connected) {
+ ret = data->is_chan_connected(adap, client, chan);
+ if (!ret)
+ return -EBUSY;
+ }
+
+ /* we make switches look like muxes, not sure how to be smarter */
+ if (chip->muxtype == pca954x_ismux)
+ regval = chan | chip->enable;
+ else
+ regval = 1 << chan;
+
+ /* Only select the channel if its different from the last channel */
+ if (data->last_chan != chan) {
+ ret = pca954x_xfer(adap, client, I2C_SMBUS_WRITE, ®val);
+ data->last_chan = chan;
+ }
+
+ return ret;
+}
+
+static int pca954x_deselect_mux(struct i2c_adapter *adap,
+ void *client, u32 chan)
+{
+ struct pca954x *data = i2c_get_clientdata((struct i2c_client *) client);
+ struct chip_desc *chip = &chips[data->type];
+ u8 regval = 0;
+
+ /* Check if we have to stay on the last channel we selected */
+ if (!data->deselect_on_exit && (chip->muxtype == pca954x_ismux))
+ return 0;
+
+ /* Signal we are deselecting active channel */
+ data->last_chan = -1;
+
+ regval = chan | ~chip->enable;
+ return pca954x_xfer(adap, client, I2C_SMBUS_WRITE, ®val);
+}
+
+/*
+ * I2C init/probing/exit functions
+ */
+
+static int pca954x_probe(struct i2c_client *client,
+ const struct i2c_device_id *id)
+{
+ struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
+ struct pca954x_platform_data *pdata = client->dev.platform_data;
+ int i, n, f;
+ struct pca954x *data;
+ int ret = -ENODEV;
+
+ if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
+ goto err;
+
+ data = kzalloc(sizeof(struct pca954x), GFP_KERNEL);
+ if (!data) {
+ ret = -ENOMEM;
+ goto err;
+ }
+
+ i2c_set_clientdata(client, data);
+
+ /* Read the mux register at addr. This does two things: it verifies
+ * that the mux is in fact present, and fetches its current
+ * contents for possible use with a future deselect algorithm.
+ */
+ i = i2c_smbus_read_byte(client);
+ if (i < 0) {
+ dev_warn(&client->dev, "probe failed\n");
+ goto exit_free;
+ }
+
+ data->type = id->driver_data;
+ data->last_chan = -1; /* force the first selection */
+
+ /* Now create virtual busses and adapters for them */
+ for (i = 0; i < chips[data->type].nchans; i++) {
+ f = 0; /* dynamic adap number */
+ if (pdata && (i < pdata->num_modes))
+ f = pdata->modes[i].adap_id; /* force static number */
+
+ data->virt_adaps[i] = i2c_add_mux_adapter(adap, client, f, i,
+ &pca954x_select_chan,
+ &pca954x_deselect_mux);
+ if (data->virt_adaps[i] == NULL) {
+ ret = -ENODEV;
+ goto virt_reg_failed;
+ }
+
+ data->deselect_on_exit = pdata->modes[i].deselect_on_exit;
+ data->is_chan_connected = pdata->modes[i].is_chan_connected;
+ }
+
+ dev_info(&client->dev, "registered %d virtual busses for I2C %s %s\n",
+ i, chips[data->type].muxtype == pca954x_ismux ?
+ "mux" : "switch", client->name);
+
+ return 0;
+
+virt_reg_failed:
+ for (n = 0; n < i; n++)
+ i2c_del_mux_adapter(data->virt_adaps[n]);
+ i2c_detach_client(client);
+exit_free:
+ kfree(data);
+err:
+ return ret;
+}
+
+static int __devexit pca954x_remove(struct i2c_client *client)
+{
+ struct pca954x *data = i2c_get_clientdata(client);
+ struct chip_desc *chip = &chips[data->type];
+ int i, err;
+
+ for (i = 0; i < chip->nchans; ++i)
+ if (data->virt_adaps[i]) {
+ err = i2c_del_mux_adapter(data->virt_adaps[i]);
+ if (err)
+ return err;
+ data->virt_adaps[i] = NULL;
+ }
+
+ kfree(data);
+ return 0;
+}
+
+static struct i2c_driver pca954x_driver = {
+ .driver = {
+ .name = "pca954x",
+ .owner = THIS_MODULE,
+ },
+ .probe = pca954x_probe,
+ .remove = __devexit_p(pca954x_remove),
+ .id_table = pca954x_id,
+};
+
+static int __init pca954x_init(void)
+{
+ return i2c_add_driver(&pca954x_driver);
+}
+
+static void __exit pca954x_exit(void)
+{
+ i2c_del_driver(&pca954x_driver);
+}
+
+module_init(pca954x_init);
+module_exit(pca954x_exit);
+
+MODULE_AUTHOR("Rodolfo Giometti <giometti-k2GhghHVRtY@public.gmane.org>");
+MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/i2c/pca954x.h b/include/linux/i2c/pca954x.h
new file mode 100644
index 0000000..18d5e79
--- /dev/null
+++ b/include/linux/i2c/pca954x.h
@@ -0,0 +1,13 @@
+/* Platform data for the PCA954x I2C multiplexers */
+
+struct pca954x_platform_mode {
+ int adap_id;
+ unsigned int deselect_on_exit:1;
+ int (*is_chan_connected)(struct i2c_adapter *adap,
+ struct i2c_client *client, u32 chan);
+};
+
+struct pca954x_platform_data {
+ struct pca954x_platform_mode *modes;
+ int num_modes;
+};
--
1.5.6.3
next prev parent reply other threads:[~2009-02-05 13:36 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-02-05 13:36 i2c bus multiplexing (Version 2) Rodolfo Giometti
[not found] ` <1233840973-13268-1-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36 ` [PATCH 1/6] i2c: put driver_unregister() out of core_lock protection Rodolfo Giometti
[not found] ` <1233840973-13268-2-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36 ` [PATCH 2/6] i2c: use rwsem instead of mutex Rodolfo Giometti
[not found] ` <1233840973-13268-3-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36 ` [PATCH 3/6] i2c: ignore active clients detaching during adapter removal Rodolfo Giometti
[not found] ` <1233840973-13268-4-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36 ` [PATCH 4/6] i2c: free adapters (de)registration from i2c "core_lock" mutex Rodolfo Giometti
[not found] ` <1233840973-13268-5-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36 ` [PATCH 5/6] i2c: Multiplexed I2C bus core support Rodolfo Giometti
[not found] ` <1233840973-13268-6-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36 ` Rodolfo Giometti [this message]
2009-02-06 0:02 ` [PATCH 3/6] i2c: ignore active clients detaching during adapter removal David Brownell
[not found] ` <200902051602.44036.david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org>
2009-02-06 10:22 ` Rodolfo Giometti
[not found] ` <20090206102220.GF28554-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-02-06 12:15 ` Jean Delvare
[not found] ` <20090206131520.232b53c3-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
2009-02-06 12:59 ` Rodolfo Giometti
[not found] ` <20090206125907.GA8581-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-02-06 21:15 ` Jean Delvare
[not found] ` <20090206221519.5fc3d2b9-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
2009-02-09 13:06 ` Rodolfo Giometti
[not found] ` <20090209130642.GV7975-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-02-10 11:08 ` Jean Delvare
[not found] ` <20090210120839.23592e38-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
2009-02-18 16:48 ` Rodolfo Giometti
[not found] ` <20090218164812.GA11217-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-05-28 7:50 ` Jean Delvare
[not found] ` <20090528095054.03fc2df3-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
2009-05-28 7:58 ` Rodolfo Giometti
[not found] ` <20090528075835.GH20243-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-05-28 8:22 ` Jean Delvare
2009-05-29 7:28 ` Rodolfo Giometti
[not found] ` <20090529072835.GA4575-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-05-29 7:46 ` Jean Delvare
2009-05-28 11:50 ` [PATCH 2/6] i2c: use rwsem instead of mutex Jean Delvare
2009-12-11 22:41 ` i2c bus multiplexing (Version 2) Muralidharan Karicheri
[not found] ` <loom.20091211T234046-786-eS7Uydv5nfjZ+VzJOa5vwg@public.gmane.org>
2010-04-16 11:18 ` Jean Delvare
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1233840973-13268-7-git-send-email-giometti@linux.it \
--to=giometti-k2ghghhvrty@public.gmane.org \
--cc=david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org \
--cc=galak-XVmvHMARGAS8U2dJNN8I7kB+6BGkLq7r@public.gmane.org \
--cc=jacmet-OfajU3CKLf1/SzgSGea1oA@public.gmane.org \
--cc=khali-PUYAD+kWke1g9hUCZPvPmw@public.gmane.org \
--cc=linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).