4 * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
7 * This module supports the PCA954x and PCA954x series of I2C multiplexer/switch
8 * chips made by NXP Semiconductors.
10 * PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547,
11 * PCA9548, PCA9846, PCA9847, PCA9848 and PCA9849.
13 * These chips are all controlled via the I2C bus itself, and all have a
14 * single 8-bit register. The upstream "parent" bus fans out to two,
15 * four, or eight downstream busses or channels; which of these
16 * are selected is determined by the chip type and register contents. A
17 * mux can select only one sub-bus at a time; a switch can select any
18 * combination simultaneously.
21 * pca954x.c from Kumar Gala <galak@kernel.crashing.org>
25 * pca954x.c from Ken Harrenstien
26 * Copyright (C) 2004 Google, Inc. (Ken Harrenstien)
29 * i2c-virtual_cb.c from Brian Kuschak <bkuschak@yahoo.com>
31 * pca9540.c from Jean Delvare <jdelvare@suse.de>.
33 * This file is licensed under the terms of the GNU General Public
34 * License version 2. This program is licensed "as is" without any
35 * warranty of any kind, whether express or implied.
38 #include <linux/device.h>
39 #include <linux/gpio/consumer.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c-mux.h>
42 #include <linux/interrupt.h>
43 #include <linux/irq.h>
44 #include <linux/module.h>
46 #include <linux/of_device.h>
47 #include <linux/of_irq.h>
48 #include <linux/platform_data/pca954x.h>
50 #include <linux/slab.h>
51 #include <linux/spinlock.h>
53 #define PCA954X_MAX_NCHANS 8
55 #define PCA954X_IRQ_OFFSET 4
74 u8 enable; /* used for muxes only */
80 struct i2c_device_identity id;
84 const struct chip_desc *chip;
86 u8 last_chan; /* last register value */
88 struct i2c_client *client;
90 struct irq_domain *irq;
91 unsigned int irq_mask;
95 /* Provide specs for the PCA954x types we know about */
96 static const struct chip_desc chips[] = {
100 .muxtype = pca954x_ismux,
101 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
107 .muxtype = pca954x_ismux,
108 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
113 .muxtype = pca954x_isswi,
114 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
120 .muxtype = pca954x_ismux,
121 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
126 .muxtype = pca954x_isswi,
127 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
131 .muxtype = pca954x_isswi,
132 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
137 .muxtype = pca954x_ismux,
138 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
142 .muxtype = pca954x_isswi,
143 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
147 .muxtype = pca954x_isswi,
149 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
156 .muxtype = pca954x_ismux,
158 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
164 .muxtype = pca954x_isswi,
166 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
173 .muxtype = pca954x_ismux,
175 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
181 static const struct i2c_device_id pca954x_id[] = {
182 { "pca9540", pca_9540 },
183 { "pca9542", pca_9542 },
184 { "pca9543", pca_9543 },
185 { "pca9544", pca_9544 },
186 { "pca9545", pca_9545 },
187 { "pca9546", pca_9546 },
188 { "pca9547", pca_9547 },
189 { "pca9548", pca_9548 },
190 { "pca9846", pca_9846 },
191 { "pca9847", pca_9847 },
192 { "pca9848", pca_9848 },
193 { "pca9849", pca_9849 },
196 MODULE_DEVICE_TABLE(i2c, pca954x_id);
199 static const struct of_device_id pca954x_of_match[] = {
200 { .compatible = "nxp,pca9540", .data = &chips[pca_9540] },
201 { .compatible = "nxp,pca9542", .data = &chips[pca_9542] },
202 { .compatible = "nxp,pca9543", .data = &chips[pca_9543] },
203 { .compatible = "nxp,pca9544", .data = &chips[pca_9544] },
204 { .compatible = "nxp,pca9545", .data = &chips[pca_9545] },
205 { .compatible = "nxp,pca9546", .data = &chips[pca_9546] },
206 { .compatible = "nxp,pca9547", .data = &chips[pca_9547] },
207 { .compatible = "nxp,pca9548", .data = &chips[pca_9548] },
208 { .compatible = "nxp,pca9846", .data = &chips[pca_9846] },
209 { .compatible = "nxp,pca9847", .data = &chips[pca_9847] },
210 { .compatible = "nxp,pca9848", .data = &chips[pca_9848] },
211 { .compatible = "nxp,pca9849", .data = &chips[pca_9849] },
214 MODULE_DEVICE_TABLE(of, pca954x_of_match);
217 /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
218 for this as they will try to lock adapter a second time */
219 static int pca954x_reg_write(struct i2c_adapter *adap,
220 struct i2c_client *client, u8 val)
224 if (adap->algo->master_xfer) {
228 msg.addr = client->addr;
233 ret = __i2c_transfer(adap, &msg, 1);
235 if (ret >= 0 && ret != 1)
238 union i2c_smbus_data data;
239 ret = adap->algo->smbus_xfer(adap, client->addr,
242 val, I2C_SMBUS_BYTE, &data);
248 static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
250 struct pca954x *data = i2c_mux_priv(muxc);
251 struct i2c_client *client = data->client;
252 const struct chip_desc *chip = data->chip;
256 /* we make switches look like muxes, not sure how to be smarter */
257 if (chip->muxtype == pca954x_ismux)
258 regval = chan | chip->enable;
262 /* Only select the channel if its different from the last channel */
263 if (data->last_chan != regval) {
264 ret = pca954x_reg_write(muxc->parent, client, regval);
265 data->last_chan = ret < 0 ? 0 : regval;
271 static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
273 struct pca954x *data = i2c_mux_priv(muxc);
274 struct i2c_client *client = data->client;
276 if (!(data->deselect & (1 << chan)))
279 /* Deselect active channel */
281 return pca954x_reg_write(muxc->parent, client, data->last_chan);
284 static irqreturn_t pca954x_irq_handler(int irq, void *dev_id)
286 struct pca954x *data = dev_id;
287 unsigned int child_irq;
288 int ret, i, handled = 0;
290 ret = i2c_smbus_read_byte(data->client);
294 for (i = 0; i < data->chip->nchans; i++) {
295 if (ret & BIT(PCA954X_IRQ_OFFSET + i)) {
296 child_irq = irq_linear_revmap(data->irq, i);
297 handle_nested_irq(child_irq);
301 return handled ? IRQ_HANDLED : IRQ_NONE;
304 static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type)
306 if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW)
311 static struct irq_chip pca954x_irq_chip = {
312 .name = "i2c-mux-pca954x",
313 .irq_set_type = pca954x_irq_set_type,
316 static int pca954x_irq_setup(struct i2c_mux_core *muxc)
318 struct pca954x *data = i2c_mux_priv(muxc);
319 struct i2c_client *client = data->client;
322 if (!data->chip->has_irq || client->irq <= 0)
325 raw_spin_lock_init(&data->lock);
327 data->irq = irq_domain_add_linear(client->dev.of_node,
329 &irq_domain_simple_ops, data);
333 for (c = 0; c < data->chip->nchans; c++) {
334 irq = irq_create_mapping(data->irq, c);
336 dev_err(&client->dev, "failed irq create map\n");
339 irq_set_chip_data(irq, data);
340 irq_set_chip_and_handler(irq, &pca954x_irq_chip,
347 static void pca954x_cleanup(struct i2c_mux_core *muxc)
349 struct pca954x *data = i2c_mux_priv(muxc);
353 for (c = 0; c < data->chip->nchans; c++) {
354 irq = irq_find_mapping(data->irq, c);
355 irq_dispose_mapping(irq);
357 irq_domain_remove(data->irq);
359 i2c_mux_del_adapters(muxc);
363 * I2C init/probing/exit functions
365 static int pca954x_probe(struct i2c_client *client,
366 const struct i2c_device_id *id)
368 struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
369 struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
370 struct device_node *of_node = client->dev.of_node;
371 bool idle_disconnect_dt;
372 struct gpio_desc *gpio;
373 int num, force, class;
374 struct i2c_mux_core *muxc;
375 struct pca954x *data;
376 const struct of_device_id *match;
379 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
382 muxc = i2c_mux_alloc(adap, &client->dev,
383 PCA954X_MAX_NCHANS, sizeof(*data), 0,
384 pca954x_select_chan, pca954x_deselect_mux);
387 data = i2c_mux_priv(muxc);
389 i2c_set_clientdata(client, muxc);
390 data->client = client;
392 /* Get the mux out of reset if a reset GPIO is specified. */
393 gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_LOW);
395 return PTR_ERR(gpio);
397 match = of_match_device(of_match_ptr(pca954x_of_match), &client->dev);
399 data->chip = of_device_get_match_data(&client->dev);
401 data->chip = &chips[id->driver_data];
403 if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) {
404 struct i2c_device_identity id;
406 ret = i2c_get_device_id(client, &id);
407 if (ret && ret != -EOPNOTSUPP)
411 (id.manufacturer_id != data->chip->id.manufacturer_id ||
412 id.part_id != data->chip->id.part_id)) {
413 dev_warn(&client->dev,
414 "unexpected device id %03x-%03x-%x\n",
415 id.manufacturer_id, id.part_id,
421 /* Write the mux register at addr to verify
422 * that the mux is in fact present. This also
423 * initializes the mux to disconnected state.
425 if (i2c_smbus_write_byte(client, 0) < 0) {
426 dev_warn(&client->dev, "probe failed\n");
430 data->last_chan = 0; /* force the first selection */
432 idle_disconnect_dt = of_node &&
433 of_property_read_bool(of_node, "i2c-mux-idle-disconnect");
435 ret = pca954x_irq_setup(muxc);
439 /* Now create an adapter for each channel */
440 for (num = 0; num < data->chip->nchans; num++) {
441 bool idle_disconnect_pd = false;
443 force = 0; /* dynamic adap number */
444 class = 0; /* no class by default */
446 if (num < pdata->num_modes) {
447 /* force static number */
448 force = pdata->modes[num].adap_id;
449 class = pdata->modes[num].class;
451 /* discard unconfigured channels */
453 idle_disconnect_pd = pdata->modes[num].deselect_on_exit;
455 data->deselect |= (idle_disconnect_pd ||
456 idle_disconnect_dt) << num;
458 ret = i2c_mux_add_adapter(muxc, force, num, class);
464 ret = devm_request_threaded_irq(&client->dev, data->client->irq,
465 NULL, pca954x_irq_handler,
466 IRQF_ONESHOT | IRQF_SHARED,
472 dev_info(&client->dev,
473 "registered %d multiplexed busses for I2C %s %s\n",
474 num, data->chip->muxtype == pca954x_ismux
475 ? "mux" : "switch", client->name);
480 pca954x_cleanup(muxc);
484 static int pca954x_remove(struct i2c_client *client)
486 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
488 pca954x_cleanup(muxc);
492 #ifdef CONFIG_PM_SLEEP
493 static int pca954x_resume(struct device *dev)
495 struct i2c_client *client = to_i2c_client(dev);
496 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
497 struct pca954x *data = i2c_mux_priv(muxc);
500 return i2c_smbus_write_byte(client, 0);
504 static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
506 static struct i2c_driver pca954x_driver = {
510 .of_match_table = of_match_ptr(pca954x_of_match),
512 .probe = pca954x_probe,
513 .remove = pca954x_remove,
514 .id_table = pca954x_id,
517 module_i2c_driver(pca954x_driver);
519 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
520 MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
521 MODULE_LICENSE("GPL v2");