1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
6 * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
8 * This module supports the PCA954x and PCA984x series of I2C multiplexer/switch
9 * chips made by NXP Semiconductors.
11 * PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547,
12 * PCA9548, PCA9846, PCA9847, PCA9848 and PCA9849.
14 * It's also compatible to Maxims MAX735x I2C switch chips, which are controlled
15 * as the NXP PCA9548 and the MAX736x chips that act like the PCA9544.
18 * MAX7356, MAX7357, MAX7358, MAX7367, MAX7368 and MAX7369
20 * These chips are all controlled via the I2C bus itself, and all have a
21 * single 8-bit register. The upstream "parent" bus fans out to two,
22 * four, or eight downstream busses or channels; which of these
23 * are selected is determined by the chip type and register contents. A
24 * mux can select only one sub-bus at a time; a switch can select any
25 * combination simultaneously.
28 * pca954x.c from Kumar Gala <galak@kernel.crashing.org>
32 * pca954x.c from Ken Harrenstien
33 * Copyright (C) 2004 Google, Inc. (Ken Harrenstien)
36 * i2c-virtual_cb.c from Brian Kuschak <bkuschak@yahoo.com>
38 * pca9540.c from Jean Delvare <jdelvare@suse.de>.
41 #include <linux/device.h>
42 #include <linux/delay.h>
43 #include <linux/gpio/consumer.h>
44 #include <linux/i2c.h>
45 #include <linux/i2c-mux.h>
46 #include <linux/interrupt.h>
47 #include <linux/irq.h>
48 #include <linux/module.h>
50 #include <linux/property.h>
51 #include <linux/regulator/consumer.h>
52 #include <linux/slab.h>
53 #include <linux/spinlock.h>
54 #include <dt-bindings/mux/mux.h>
56 #define PCA954X_MAX_NCHANS 8
58 #define PCA954X_IRQ_OFFSET 4
83 u8 enable; /* used for muxes only */
89 struct i2c_device_identity id;
93 const struct chip_desc *chip;
95 u8 last_chan; /* last register value */
96 /* MUX_IDLE_AS_IS, MUX_IDLE_DISCONNECT or >= 0 for channel */
99 struct i2c_client *client;
101 struct irq_domain *irq;
102 unsigned int irq_mask;
104 struct regulator *supply;
107 /* Provide specs for the MAX735x, PCA954x and PCA984x types we know about */
108 static const struct chip_desc chips[] = {
111 .muxtype = pca954x_isswi,
112 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
116 .muxtype = pca954x_isswi,
117 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
119 * No interrupt controller support. The interrupt
120 * provides information about stuck channels.
125 .muxtype = pca954x_isswi,
126 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
128 * No interrupt controller support. The interrupt
129 * provides information about stuck channels.
134 .muxtype = pca954x_isswi,
136 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
140 .muxtype = pca954x_isswi,
141 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
146 .muxtype = pca954x_ismux,
148 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
153 .muxtype = pca954x_ismux,
154 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
160 .muxtype = pca954x_ismux,
161 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
166 .muxtype = pca954x_isswi,
167 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
173 .muxtype = pca954x_ismux,
174 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
179 .muxtype = pca954x_isswi,
180 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
184 .muxtype = pca954x_isswi,
185 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
190 .muxtype = pca954x_ismux,
191 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
195 .muxtype = pca954x_isswi,
196 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
200 .muxtype = pca954x_isswi,
202 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
209 .muxtype = pca954x_ismux,
211 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
217 .muxtype = pca954x_isswi,
219 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
226 .muxtype = pca954x_ismux,
228 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
234 static const struct i2c_device_id pca954x_id[] = {
235 { "max7356", max_7356 },
236 { "max7357", max_7357 },
237 { "max7358", max_7358 },
238 { "max7367", max_7367 },
239 { "max7368", max_7368 },
240 { "max7369", max_7369 },
241 { "pca9540", pca_9540 },
242 { "pca9542", pca_9542 },
243 { "pca9543", pca_9543 },
244 { "pca9544", pca_9544 },
245 { "pca9545", pca_9545 },
246 { "pca9546", pca_9546 },
247 { "pca9547", pca_9547 },
248 { "pca9548", pca_9548 },
249 { "pca9846", pca_9846 },
250 { "pca9847", pca_9847 },
251 { "pca9848", pca_9848 },
252 { "pca9849", pca_9849 },
255 MODULE_DEVICE_TABLE(i2c, pca954x_id);
257 static const struct of_device_id pca954x_of_match[] = {
258 { .compatible = "maxim,max7356", .data = &chips[max_7356] },
259 { .compatible = "maxim,max7357", .data = &chips[max_7357] },
260 { .compatible = "maxim,max7358", .data = &chips[max_7358] },
261 { .compatible = "maxim,max7367", .data = &chips[max_7367] },
262 { .compatible = "maxim,max7368", .data = &chips[max_7368] },
263 { .compatible = "maxim,max7369", .data = &chips[max_7369] },
264 { .compatible = "nxp,pca9540", .data = &chips[pca_9540] },
265 { .compatible = "nxp,pca9542", .data = &chips[pca_9542] },
266 { .compatible = "nxp,pca9543", .data = &chips[pca_9543] },
267 { .compatible = "nxp,pca9544", .data = &chips[pca_9544] },
268 { .compatible = "nxp,pca9545", .data = &chips[pca_9545] },
269 { .compatible = "nxp,pca9546", .data = &chips[pca_9546] },
270 { .compatible = "nxp,pca9547", .data = &chips[pca_9547] },
271 { .compatible = "nxp,pca9548", .data = &chips[pca_9548] },
272 { .compatible = "nxp,pca9846", .data = &chips[pca_9846] },
273 { .compatible = "nxp,pca9847", .data = &chips[pca_9847] },
274 { .compatible = "nxp,pca9848", .data = &chips[pca_9848] },
275 { .compatible = "nxp,pca9849", .data = &chips[pca_9849] },
278 MODULE_DEVICE_TABLE(of, pca954x_of_match);
280 /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
281 for this as they will try to lock adapter a second time */
282 static int pca954x_reg_write(struct i2c_adapter *adap,
283 struct i2c_client *client, u8 val)
285 union i2c_smbus_data dummy;
287 return __i2c_smbus_xfer(adap, client->addr, client->flags,
288 I2C_SMBUS_WRITE, val,
289 I2C_SMBUS_BYTE, &dummy);
292 static u8 pca954x_regval(struct pca954x *data, u8 chan)
294 /* We make switches look like muxes, not sure how to be smarter. */
295 if (data->chip->muxtype == pca954x_ismux)
296 return chan | data->chip->enable;
301 static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
303 struct pca954x *data = i2c_mux_priv(muxc);
304 struct i2c_client *client = data->client;
308 regval = pca954x_regval(data, chan);
309 /* Only select the channel if its different from the last channel */
310 if (data->last_chan != regval) {
311 ret = pca954x_reg_write(muxc->parent, client, regval);
312 data->last_chan = ret < 0 ? 0 : regval;
318 static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
320 struct pca954x *data = i2c_mux_priv(muxc);
321 struct i2c_client *client = data->client;
324 idle_state = READ_ONCE(data->idle_state);
326 /* Set the mux back to a predetermined channel */
327 return pca954x_select_chan(muxc, idle_state);
329 if (idle_state == MUX_IDLE_DISCONNECT) {
330 /* Deselect active channel */
332 return pca954x_reg_write(muxc->parent, client,
336 /* otherwise leave as-is */
341 static ssize_t idle_state_show(struct device *dev,
342 struct device_attribute *attr,
345 struct i2c_client *client = to_i2c_client(dev);
346 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
347 struct pca954x *data = i2c_mux_priv(muxc);
349 return sprintf(buf, "%d\n", READ_ONCE(data->idle_state));
352 static ssize_t idle_state_store(struct device *dev,
353 struct device_attribute *attr,
354 const char *buf, size_t count)
356 struct i2c_client *client = to_i2c_client(dev);
357 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
358 struct pca954x *data = i2c_mux_priv(muxc);
362 ret = kstrtoint(buf, 0, &val);
366 if (val != MUX_IDLE_AS_IS && val != MUX_IDLE_DISCONNECT &&
367 (val < 0 || val >= data->chip->nchans))
370 i2c_lock_bus(muxc->parent, I2C_LOCK_SEGMENT);
372 WRITE_ONCE(data->idle_state, val);
374 * Set the mux into a state consistent with the new
377 if (data->last_chan || val != MUX_IDLE_DISCONNECT)
378 ret = pca954x_deselect_mux(muxc, 0);
380 i2c_unlock_bus(muxc->parent, I2C_LOCK_SEGMENT);
382 return ret < 0 ? ret : count;
385 static DEVICE_ATTR_RW(idle_state);
387 static irqreturn_t pca954x_irq_handler(int irq, void *dev_id)
389 struct pca954x *data = dev_id;
390 unsigned long pending;
393 ret = i2c_smbus_read_byte(data->client);
397 pending = (ret >> PCA954X_IRQ_OFFSET) & (BIT(data->chip->nchans) - 1);
398 for_each_set_bit(i, &pending, data->chip->nchans)
399 handle_nested_irq(irq_linear_revmap(data->irq, i));
401 return IRQ_RETVAL(pending);
404 static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type)
406 if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW)
411 static struct irq_chip pca954x_irq_chip = {
412 .name = "i2c-mux-pca954x",
413 .irq_set_type = pca954x_irq_set_type,
416 static int pca954x_irq_setup(struct i2c_mux_core *muxc)
418 struct pca954x *data = i2c_mux_priv(muxc);
419 struct i2c_client *client = data->client;
422 if (!data->chip->has_irq || client->irq <= 0)
425 raw_spin_lock_init(&data->lock);
427 data->irq = irq_domain_add_linear(client->dev.of_node,
429 &irq_domain_simple_ops, data);
433 for (c = 0; c < data->chip->nchans; c++) {
434 irq = irq_create_mapping(data->irq, c);
436 dev_err(&client->dev, "failed irq create map\n");
439 irq_set_chip_data(irq, data);
440 irq_set_chip_and_handler(irq, &pca954x_irq_chip,
447 static void pca954x_cleanup(struct i2c_mux_core *muxc)
449 struct pca954x *data = i2c_mux_priv(muxc);
452 regulator_disable(data->supply);
455 for (c = 0; c < data->chip->nchans; c++) {
456 irq = irq_find_mapping(data->irq, c);
457 irq_dispose_mapping(irq);
459 irq_domain_remove(data->irq);
461 i2c_mux_del_adapters(muxc);
464 static int pca954x_init(struct i2c_client *client, struct pca954x *data)
468 if (data->idle_state >= 0)
469 data->last_chan = pca954x_regval(data, data->idle_state);
471 data->last_chan = 0; /* Disconnect multiplexer */
473 ret = i2c_smbus_write_byte(client, data->last_chan);
481 * I2C init/probing/exit functions
483 static int pca954x_probe(struct i2c_client *client)
485 const struct i2c_device_id *id = i2c_client_get_device_id(client);
486 struct i2c_adapter *adap = client->adapter;
487 struct device *dev = &client->dev;
488 struct gpio_desc *gpio;
489 struct i2c_mux_core *muxc;
490 struct pca954x *data;
494 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
497 muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0,
498 pca954x_select_chan, pca954x_deselect_mux);
501 data = i2c_mux_priv(muxc);
503 i2c_set_clientdata(client, muxc);
504 data->client = client;
506 data->supply = devm_regulator_get(dev, "vdd");
507 if (IS_ERR(data->supply))
508 return dev_err_probe(dev, PTR_ERR(data->supply),
509 "Failed to request regulator\n");
511 ret = regulator_enable(data->supply);
513 return dev_err_probe(dev, ret,
514 "Failed to enable vdd supply\n");
516 /* Reset the mux if a reset GPIO is specified. */
517 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
524 gpiod_set_value_cansleep(gpio, 0);
525 /* Give the chip some time to recover. */
529 data->chip = device_get_match_data(dev);
531 data->chip = &chips[id->driver_data];
533 if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) {
534 struct i2c_device_identity id;
536 ret = i2c_get_device_id(client, &id);
537 if (ret && ret != -EOPNOTSUPP)
541 (id.manufacturer_id != data->chip->id.manufacturer_id ||
542 id.part_id != data->chip->id.part_id)) {
543 dev_warn(dev, "unexpected device id %03x-%03x-%x\n",
544 id.manufacturer_id, id.part_id,
551 data->idle_state = MUX_IDLE_AS_IS;
552 if (device_property_read_u32(dev, "idle-state", &data->idle_state)) {
553 if (device_property_read_bool(dev, "i2c-mux-idle-disconnect"))
554 data->idle_state = MUX_IDLE_DISCONNECT;
558 * Write the mux register at addr to verify
559 * that the mux is in fact present. This also
560 * initializes the mux to a channel
561 * or disconnected state.
563 ret = pca954x_init(client, data);
565 dev_warn(dev, "probe failed\n");
570 ret = pca954x_irq_setup(muxc);
574 /* Now create an adapter for each channel */
575 for (num = 0; num < data->chip->nchans; num++) {
576 ret = i2c_mux_add_adapter(muxc, 0, num, 0);
582 ret = devm_request_threaded_irq(dev, data->client->irq,
583 NULL, pca954x_irq_handler,
584 IRQF_ONESHOT | IRQF_SHARED,
591 * The attr probably isn't going to be needed in most cases,
592 * so don't fail completely on error.
594 device_create_file(dev, &dev_attr_idle_state);
596 dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n",
597 num, data->chip->muxtype == pca954x_ismux
598 ? "mux" : "switch", client->name);
603 pca954x_cleanup(muxc);
607 static void pca954x_remove(struct i2c_client *client)
609 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
611 device_remove_file(&client->dev, &dev_attr_idle_state);
613 pca954x_cleanup(muxc);
616 static int pca954x_resume(struct device *dev)
618 struct i2c_client *client = to_i2c_client(dev);
619 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
620 struct pca954x *data = i2c_mux_priv(muxc);
623 ret = pca954x_init(client, data);
625 dev_err(&client->dev, "failed to verify mux presence\n");
630 static DEFINE_SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
632 static struct i2c_driver pca954x_driver = {
635 .pm = pm_sleep_ptr(&pca954x_pm),
636 .of_match_table = pca954x_of_match,
638 .probe = pca954x_probe,
639 .remove = pca954x_remove,
640 .id_table = pca954x_id,
643 module_i2c_driver(pca954x_driver);
645 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
646 MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
647 MODULE_LICENSE("GPL v2");