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 * These chips are all controlled via the I2C bus itself, and all have a
15 * single 8-bit register. The upstream "parent" bus fans out to two,
16 * four, or eight downstream busses or channels; which of these
17 * are selected is determined by the chip type and register contents. A
18 * mux can select only one sub-bus at a time; a switch can select any
19 * combination simultaneously.
22 * pca954x.c from Kumar Gala <galak@kernel.crashing.org>
26 * pca954x.c from Ken Harrenstien
27 * Copyright (C) 2004 Google, Inc. (Ken Harrenstien)
30 * i2c-virtual_cb.c from Brian Kuschak <bkuschak@yahoo.com>
32 * pca9540.c from Jean Delvare <jdelvare@suse.de>.
35 #include <linux/device.h>
36 #include <linux/delay.h>
37 #include <linux/gpio/consumer.h>
38 #include <linux/i2c.h>
39 #include <linux/i2c-mux.h>
40 #include <linux/interrupt.h>
41 #include <linux/irq.h>
42 #include <linux/module.h>
44 #include <linux/property.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <dt-bindings/mux/mux.h>
49 #define PCA954X_MAX_NCHANS 8
51 #define PCA954X_IRQ_OFFSET 4
70 u8 enable
; /* used for muxes only */
76 struct i2c_device_identity id
;
80 const struct chip_desc
*chip
;
82 u8 last_chan
; /* last register value */
83 /* MUX_IDLE_AS_IS, MUX_IDLE_DISCONNECT or >= 0 for channel */
86 struct i2c_client
*client
;
88 struct irq_domain
*irq
;
89 unsigned int irq_mask
;
93 /* Provide specs for the PCA954x types we know about */
94 static const struct chip_desc chips
[] = {
98 .muxtype
= pca954x_ismux
,
99 .id
= { .manufacturer_id
= I2C_DEVICE_ID_NONE
},
105 .muxtype
= pca954x_ismux
,
106 .id
= { .manufacturer_id
= I2C_DEVICE_ID_NONE
},
111 .muxtype
= pca954x_isswi
,
112 .id
= { .manufacturer_id
= I2C_DEVICE_ID_NONE
},
118 .muxtype
= pca954x_ismux
,
119 .id
= { .manufacturer_id
= I2C_DEVICE_ID_NONE
},
124 .muxtype
= pca954x_isswi
,
125 .id
= { .manufacturer_id
= I2C_DEVICE_ID_NONE
},
129 .muxtype
= pca954x_isswi
,
130 .id
= { .manufacturer_id
= I2C_DEVICE_ID_NONE
},
135 .muxtype
= pca954x_ismux
,
136 .id
= { .manufacturer_id
= I2C_DEVICE_ID_NONE
},
140 .muxtype
= pca954x_isswi
,
141 .id
= { .manufacturer_id
= I2C_DEVICE_ID_NONE
},
145 .muxtype
= pca954x_isswi
,
147 .manufacturer_id
= I2C_DEVICE_ID_NXP_SEMICONDUCTORS
,
154 .muxtype
= pca954x_ismux
,
156 .manufacturer_id
= I2C_DEVICE_ID_NXP_SEMICONDUCTORS
,
162 .muxtype
= pca954x_isswi
,
164 .manufacturer_id
= I2C_DEVICE_ID_NXP_SEMICONDUCTORS
,
171 .muxtype
= pca954x_ismux
,
173 .manufacturer_id
= I2C_DEVICE_ID_NXP_SEMICONDUCTORS
,
179 static const struct i2c_device_id pca954x_id
[] = {
180 { "pca9540", pca_9540
},
181 { "pca9542", pca_9542
},
182 { "pca9543", pca_9543
},
183 { "pca9544", pca_9544
},
184 { "pca9545", pca_9545
},
185 { "pca9546", pca_9546
},
186 { "pca9547", pca_9547
},
187 { "pca9548", pca_9548
},
188 { "pca9846", pca_9846
},
189 { "pca9847", pca_9847
},
190 { "pca9848", pca_9848
},
191 { "pca9849", pca_9849
},
194 MODULE_DEVICE_TABLE(i2c
, pca954x_id
);
196 static const struct of_device_id pca954x_of_match
[] = {
197 { .compatible
= "nxp,pca9540", .data
= &chips
[pca_9540
] },
198 { .compatible
= "nxp,pca9542", .data
= &chips
[pca_9542
] },
199 { .compatible
= "nxp,pca9543", .data
= &chips
[pca_9543
] },
200 { .compatible
= "nxp,pca9544", .data
= &chips
[pca_9544
] },
201 { .compatible
= "nxp,pca9545", .data
= &chips
[pca_9545
] },
202 { .compatible
= "nxp,pca9546", .data
= &chips
[pca_9546
] },
203 { .compatible
= "nxp,pca9547", .data
= &chips
[pca_9547
] },
204 { .compatible
= "nxp,pca9548", .data
= &chips
[pca_9548
] },
205 { .compatible
= "nxp,pca9846", .data
= &chips
[pca_9846
] },
206 { .compatible
= "nxp,pca9847", .data
= &chips
[pca_9847
] },
207 { .compatible
= "nxp,pca9848", .data
= &chips
[pca_9848
] },
208 { .compatible
= "nxp,pca9849", .data
= &chips
[pca_9849
] },
211 MODULE_DEVICE_TABLE(of
, pca954x_of_match
);
213 /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
214 for this as they will try to lock adapter a second time */
215 static int pca954x_reg_write(struct i2c_adapter
*adap
,
216 struct i2c_client
*client
, u8 val
)
218 union i2c_smbus_data dummy
;
220 return __i2c_smbus_xfer(adap
, client
->addr
, client
->flags
,
221 I2C_SMBUS_WRITE
, val
,
222 I2C_SMBUS_BYTE
, &dummy
);
225 static u8
pca954x_regval(struct pca954x
*data
, u8 chan
)
227 /* We make switches look like muxes, not sure how to be smarter. */
228 if (data
->chip
->muxtype
== pca954x_ismux
)
229 return chan
| data
->chip
->enable
;
234 static int pca954x_select_chan(struct i2c_mux_core
*muxc
, u32 chan
)
236 struct pca954x
*data
= i2c_mux_priv(muxc
);
237 struct i2c_client
*client
= data
->client
;
241 regval
= pca954x_regval(data
, chan
);
242 /* Only select the channel if its different from the last channel */
243 if (data
->last_chan
!= regval
) {
244 ret
= pca954x_reg_write(muxc
->parent
, client
, regval
);
245 data
->last_chan
= ret
< 0 ? 0 : regval
;
251 static int pca954x_deselect_mux(struct i2c_mux_core
*muxc
, u32 chan
)
253 struct pca954x
*data
= i2c_mux_priv(muxc
);
254 struct i2c_client
*client
= data
->client
;
257 idle_state
= READ_ONCE(data
->idle_state
);
259 /* Set the mux back to a predetermined channel */
260 return pca954x_select_chan(muxc
, idle_state
);
262 if (idle_state
== MUX_IDLE_DISCONNECT
) {
263 /* Deselect active channel */
265 return pca954x_reg_write(muxc
->parent
, client
,
269 /* otherwise leave as-is */
274 static ssize_t
idle_state_show(struct device
*dev
,
275 struct device_attribute
*attr
,
278 struct i2c_client
*client
= to_i2c_client(dev
);
279 struct i2c_mux_core
*muxc
= i2c_get_clientdata(client
);
280 struct pca954x
*data
= i2c_mux_priv(muxc
);
282 return sprintf(buf
, "%d\n", READ_ONCE(data
->idle_state
));
285 static ssize_t
idle_state_store(struct device
*dev
,
286 struct device_attribute
*attr
,
287 const char *buf
, size_t count
)
289 struct i2c_client
*client
= to_i2c_client(dev
);
290 struct i2c_mux_core
*muxc
= i2c_get_clientdata(client
);
291 struct pca954x
*data
= i2c_mux_priv(muxc
);
295 ret
= kstrtoint(buf
, 0, &val
);
299 if (val
!= MUX_IDLE_AS_IS
&& val
!= MUX_IDLE_DISCONNECT
&&
300 (val
< 0 || val
>= data
->chip
->nchans
))
303 i2c_lock_bus(muxc
->parent
, I2C_LOCK_SEGMENT
);
305 WRITE_ONCE(data
->idle_state
, val
);
307 * Set the mux into a state consistent with the new
310 if (data
->last_chan
|| val
!= MUX_IDLE_DISCONNECT
)
311 ret
= pca954x_deselect_mux(muxc
, 0);
313 i2c_unlock_bus(muxc
->parent
, I2C_LOCK_SEGMENT
);
315 return ret
< 0 ? ret
: count
;
318 static DEVICE_ATTR_RW(idle_state
);
320 static irqreturn_t
pca954x_irq_handler(int irq
, void *dev_id
)
322 struct pca954x
*data
= dev_id
;
323 unsigned long pending
;
326 ret
= i2c_smbus_read_byte(data
->client
);
330 pending
= (ret
>> PCA954X_IRQ_OFFSET
) & (BIT(data
->chip
->nchans
) - 1);
331 for_each_set_bit(i
, &pending
, data
->chip
->nchans
)
332 handle_nested_irq(irq_linear_revmap(data
->irq
, i
));
334 return IRQ_RETVAL(pending
);
337 static int pca954x_irq_set_type(struct irq_data
*idata
, unsigned int type
)
339 if ((type
& IRQ_TYPE_SENSE_MASK
) != IRQ_TYPE_LEVEL_LOW
)
344 static struct irq_chip pca954x_irq_chip
= {
345 .name
= "i2c-mux-pca954x",
346 .irq_set_type
= pca954x_irq_set_type
,
349 static int pca954x_irq_setup(struct i2c_mux_core
*muxc
)
351 struct pca954x
*data
= i2c_mux_priv(muxc
);
352 struct i2c_client
*client
= data
->client
;
355 if (!data
->chip
->has_irq
|| client
->irq
<= 0)
358 raw_spin_lock_init(&data
->lock
);
360 data
->irq
= irq_domain_add_linear(client
->dev
.of_node
,
362 &irq_domain_simple_ops
, data
);
366 for (c
= 0; c
< data
->chip
->nchans
; c
++) {
367 irq
= irq_create_mapping(data
->irq
, c
);
369 dev_err(&client
->dev
, "failed irq create map\n");
372 irq_set_chip_data(irq
, data
);
373 irq_set_chip_and_handler(irq
, &pca954x_irq_chip
,
380 static void pca954x_cleanup(struct i2c_mux_core
*muxc
)
382 struct pca954x
*data
= i2c_mux_priv(muxc
);
386 for (c
= 0; c
< data
->chip
->nchans
; c
++) {
387 irq
= irq_find_mapping(data
->irq
, c
);
388 irq_dispose_mapping(irq
);
390 irq_domain_remove(data
->irq
);
392 i2c_mux_del_adapters(muxc
);
395 static int pca954x_init(struct i2c_client
*client
, struct pca954x
*data
)
399 if (data
->idle_state
>= 0)
400 data
->last_chan
= pca954x_regval(data
, data
->idle_state
);
402 data
->last_chan
= 0; /* Disconnect multiplexer */
404 ret
= i2c_smbus_write_byte(client
, data
->last_chan
);
412 * I2C init/probing/exit functions
414 static int pca954x_probe(struct i2c_client
*client
,
415 const struct i2c_device_id
*id
)
417 struct i2c_adapter
*adap
= client
->adapter
;
418 struct device
*dev
= &client
->dev
;
419 struct gpio_desc
*gpio
;
420 struct i2c_mux_core
*muxc
;
421 struct pca954x
*data
;
425 if (!i2c_check_functionality(adap
, I2C_FUNC_SMBUS_BYTE
))
428 muxc
= i2c_mux_alloc(adap
, dev
, PCA954X_MAX_NCHANS
, sizeof(*data
), 0,
429 pca954x_select_chan
, pca954x_deselect_mux
);
432 data
= i2c_mux_priv(muxc
);
434 i2c_set_clientdata(client
, muxc
);
435 data
->client
= client
;
437 /* Reset the mux if a reset GPIO is specified. */
438 gpio
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
440 return PTR_ERR(gpio
);
443 gpiod_set_value_cansleep(gpio
, 0);
444 /* Give the chip some time to recover. */
448 data
->chip
= device_get_match_data(dev
);
450 data
->chip
= &chips
[id
->driver_data
];
452 if (data
->chip
->id
.manufacturer_id
!= I2C_DEVICE_ID_NONE
) {
453 struct i2c_device_identity id
;
455 ret
= i2c_get_device_id(client
, &id
);
456 if (ret
&& ret
!= -EOPNOTSUPP
)
460 (id
.manufacturer_id
!= data
->chip
->id
.manufacturer_id
||
461 id
.part_id
!= data
->chip
->id
.part_id
)) {
462 dev_warn(dev
, "unexpected device id %03x-%03x-%x\n",
463 id
.manufacturer_id
, id
.part_id
,
469 data
->idle_state
= MUX_IDLE_AS_IS
;
470 if (device_property_read_u32(dev
, "idle-state", &data
->idle_state
)) {
471 if (device_property_read_bool(dev
, "i2c-mux-idle-disconnect"))
472 data
->idle_state
= MUX_IDLE_DISCONNECT
;
476 * Write the mux register at addr to verify
477 * that the mux is in fact present. This also
478 * initializes the mux to a channel
479 * or disconnected state.
481 ret
= pca954x_init(client
, data
);
483 dev_warn(dev
, "probe failed\n");
487 ret
= pca954x_irq_setup(muxc
);
491 /* Now create an adapter for each channel */
492 for (num
= 0; num
< data
->chip
->nchans
; num
++) {
493 ret
= i2c_mux_add_adapter(muxc
, 0, num
, 0);
499 ret
= devm_request_threaded_irq(dev
, data
->client
->irq
,
500 NULL
, pca954x_irq_handler
,
501 IRQF_ONESHOT
| IRQF_SHARED
,
508 * The attr probably isn't going to be needed in most cases,
509 * so don't fail completely on error.
511 device_create_file(dev
, &dev_attr_idle_state
);
513 dev_info(dev
, "registered %d multiplexed busses for I2C %s %s\n",
514 num
, data
->chip
->muxtype
== pca954x_ismux
515 ? "mux" : "switch", client
->name
);
520 pca954x_cleanup(muxc
);
524 static int pca954x_remove(struct i2c_client
*client
)
526 struct i2c_mux_core
*muxc
= i2c_get_clientdata(client
);
528 device_remove_file(&client
->dev
, &dev_attr_idle_state
);
530 pca954x_cleanup(muxc
);
534 #ifdef CONFIG_PM_SLEEP
535 static int pca954x_resume(struct device
*dev
)
537 struct i2c_client
*client
= to_i2c_client(dev
);
538 struct i2c_mux_core
*muxc
= i2c_get_clientdata(client
);
539 struct pca954x
*data
= i2c_mux_priv(muxc
);
542 ret
= pca954x_init(client
, data
);
544 dev_err(&client
->dev
, "failed to verify mux presence\n");
550 static SIMPLE_DEV_PM_OPS(pca954x_pm
, NULL
, pca954x_resume
);
552 static struct i2c_driver pca954x_driver
= {
556 .of_match_table
= pca954x_of_match
,
558 .probe
= pca954x_probe
,
559 .remove
= pca954x_remove
,
560 .id_table
= pca954x_id
,
563 module_i2c_driver(pca954x_driver
);
565 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
566 MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
567 MODULE_LICENSE("GPL v2");