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.
25 * pca954x.c from Ken Harrenstien
26 * Copyright (C) 2004 Google, Inc. (Ken Harrenstien)
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/delay.h>
40 #include <linux/gpio/consumer.h>
41 #include <linux/i2c.h>
42 #include <linux/i2c-mux.h>
43 #include <linux/interrupt.h>
44 #include <linux/irq.h>
45 #include <linux/module.h>
47 #include <linux/of_device.h>
48 #include <linux/of_irq.h>
49 #include <linux/platform_data/pca954x.h>
51 #include <linux/slab.h>
52 #include <linux/spinlock.h>
54 #define PCA954X_MAX_NCHANS 8
56 #define PCA954X_IRQ_OFFSET 4
75 u8 enable; /* used for muxes only */
81 struct i2c_device_identity id;
85 const struct chip_desc *chip;
87 u8 last_chan; /* last register value */
89 struct i2c_client *client;
91 struct irq_domain *irq;
92 unsigned int irq_mask;
96 /* Provide specs for the PCA954x types we know about */
97 static const struct chip_desc chips[] = {
101 .muxtype = pca954x_ismux,
102 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
108 .muxtype = pca954x_ismux,
109 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
114 .muxtype = pca954x_isswi,
115 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
121 .muxtype = pca954x_ismux,
122 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
127 .muxtype = pca954x_isswi,
128 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
132 .muxtype = pca954x_isswi,
133 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
138 .muxtype = pca954x_ismux,
139 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
143 .muxtype = pca954x_isswi,
144 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
148 .muxtype = pca954x_isswi,
150 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
157 .muxtype = pca954x_ismux,
159 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
165 .muxtype = pca954x_isswi,
167 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
174 .muxtype = pca954x_ismux,
176 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
182 static const struct i2c_device_id pca954x_id[] = {
183 { "pca9540", pca_9540 },
184 { "pca9542", pca_9542 },
185 { "pca9543", pca_9543 },
186 { "pca9544", pca_9544 },
187 { "pca9545", pca_9545 },
188 { "pca9546", pca_9546 },
189 { "pca9547", pca_9547 },
190 { "pca9548", pca_9548 },
191 { "pca9846", pca_9846 },
192 { "pca9847", pca_9847 },
193 { "pca9848", pca_9848 },
194 { "pca9849", pca_9849 },
197 MODULE_DEVICE_TABLE(i2c, pca954x_id);
200 static const struct of_device_id pca954x_of_match[] = {
201 { .compatible = "nxp,pca9540", .data = &chips[pca_9540] },
202 { .compatible = "nxp,pca9542", .data = &chips[pca_9542] },
203 { .compatible = "nxp,pca9543", .data = &chips[pca_9543] },
204 { .compatible = "nxp,pca9544", .data = &chips[pca_9544] },
205 { .compatible = "nxp,pca9545", .data = &chips[pca_9545] },
206 { .compatible = "nxp,pca9546", .data = &chips[pca_9546] },
207 { .compatible = "nxp,pca9547", .data = &chips[pca_9547] },
208 { .compatible = "nxp,pca9548", .data = &chips[pca_9548] },
209 { .compatible = "nxp,pca9846", .data = &chips[pca_9846] },
210 { .compatible = "nxp,pca9847", .data = &chips[pca_9847] },
211 { .compatible = "nxp,pca9848", .data = &chips[pca_9848] },
212 { .compatible = "nxp,pca9849", .data = &chips[pca_9849] },
215 MODULE_DEVICE_TABLE(of, pca954x_of_match);
218 /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
219 for this as they will try to lock adapter a second time */
220 static int pca954x_reg_write(struct i2c_adapter *adap,
221 struct i2c_client *client, u8 val)
223 union i2c_smbus_data dummy;
225 return __i2c_smbus_xfer(adap, client->addr, client->flags,
226 I2C_SMBUS_WRITE, val,
227 I2C_SMBUS_BYTE, &dummy);
230 static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
232 struct pca954x *data = i2c_mux_priv(muxc);
233 struct i2c_client *client = data->client;
234 const struct chip_desc *chip = data->chip;
238 /* we make switches look like muxes, not sure how to be smarter */
239 if (chip->muxtype == pca954x_ismux)
240 regval = chan | chip->enable;
244 /* Only select the channel if its different from the last channel */
245 if (data->last_chan != regval) {
246 ret = pca954x_reg_write(muxc->parent, client, regval);
247 data->last_chan = ret < 0 ? 0 : regval;
253 static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
255 struct pca954x *data = i2c_mux_priv(muxc);
256 struct i2c_client *client = data->client;
258 if (!(data->deselect & (1 << chan)))
261 /* Deselect active channel */
263 return pca954x_reg_write(muxc->parent, client, data->last_chan);
266 static irqreturn_t pca954x_irq_handler(int irq, void *dev_id)
268 struct pca954x *data = dev_id;
269 unsigned int child_irq;
270 int ret, i, handled = 0;
272 ret = i2c_smbus_read_byte(data->client);
276 for (i = 0; i < data->chip->nchans; i++) {
277 if (ret & BIT(PCA954X_IRQ_OFFSET + i)) {
278 child_irq = irq_linear_revmap(data->irq, i);
279 handle_nested_irq(child_irq);
283 return handled ? IRQ_HANDLED : IRQ_NONE;
286 static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type)
288 if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW)
293 static struct irq_chip pca954x_irq_chip = {
294 .name = "i2c-mux-pca954x",
295 .irq_set_type = pca954x_irq_set_type,
298 static int pca954x_irq_setup(struct i2c_mux_core *muxc)
300 struct pca954x *data = i2c_mux_priv(muxc);
301 struct i2c_client *client = data->client;
304 if (!data->chip->has_irq || client->irq <= 0)
307 raw_spin_lock_init(&data->lock);
309 data->irq = irq_domain_add_linear(client->dev.of_node,
311 &irq_domain_simple_ops, data);
315 for (c = 0; c < data->chip->nchans; c++) {
316 irq = irq_create_mapping(data->irq, c);
318 dev_err(&client->dev, "failed irq create map\n");
321 irq_set_chip_data(irq, data);
322 irq_set_chip_and_handler(irq, &pca954x_irq_chip,
329 static void pca954x_cleanup(struct i2c_mux_core *muxc)
331 struct pca954x *data = i2c_mux_priv(muxc);
335 for (c = 0; c < data->chip->nchans; c++) {
336 irq = irq_find_mapping(data->irq, c);
337 irq_dispose_mapping(irq);
339 irq_domain_remove(data->irq);
341 i2c_mux_del_adapters(muxc);
345 * I2C init/probing/exit functions
347 static int pca954x_probe(struct i2c_client *client,
348 const struct i2c_device_id *id)
350 struct i2c_adapter *adap = client->adapter;
351 struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
352 struct device *dev = &client->dev;
353 struct device_node *np = dev->of_node;
354 bool idle_disconnect_dt;
355 struct gpio_desc *gpio;
356 int num, force, class;
357 struct i2c_mux_core *muxc;
358 struct pca954x *data;
361 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
364 muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0,
365 pca954x_select_chan, pca954x_deselect_mux);
368 data = i2c_mux_priv(muxc);
370 i2c_set_clientdata(client, muxc);
371 data->client = client;
373 /* Reset the mux if a reset GPIO is specified. */
374 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
376 return PTR_ERR(gpio);
379 gpiod_set_value_cansleep(gpio, 0);
380 /* Give the chip some time to recover. */
384 data->chip = of_device_get_match_data(dev);
386 data->chip = &chips[id->driver_data];
388 if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) {
389 struct i2c_device_identity id;
391 ret = i2c_get_device_id(client, &id);
392 if (ret && ret != -EOPNOTSUPP)
396 (id.manufacturer_id != data->chip->id.manufacturer_id ||
397 id.part_id != data->chip->id.part_id)) {
398 dev_warn(dev, "unexpected device id %03x-%03x-%x\n",
399 id.manufacturer_id, id.part_id,
405 /* Write the mux register at addr to verify
406 * that the mux is in fact present. This also
407 * initializes the mux to disconnected state.
409 if (i2c_smbus_write_byte(client, 0) < 0) {
410 dev_warn(dev, "probe failed\n");
414 data->last_chan = 0; /* force the first selection */
416 idle_disconnect_dt = np &&
417 of_property_read_bool(np, "i2c-mux-idle-disconnect");
419 ret = pca954x_irq_setup(muxc);
423 /* Now create an adapter for each channel */
424 for (num = 0; num < data->chip->nchans; num++) {
425 bool idle_disconnect_pd = false;
427 force = 0; /* dynamic adap number */
428 class = 0; /* no class by default */
430 if (num < pdata->num_modes) {
431 /* force static number */
432 force = pdata->modes[num].adap_id;
433 class = pdata->modes[num].class;
435 /* discard unconfigured channels */
437 idle_disconnect_pd = pdata->modes[num].deselect_on_exit;
439 data->deselect |= (idle_disconnect_pd ||
440 idle_disconnect_dt) << num;
442 ret = i2c_mux_add_adapter(muxc, force, num, class);
448 ret = devm_request_threaded_irq(dev, data->client->irq,
449 NULL, pca954x_irq_handler,
450 IRQF_ONESHOT | IRQF_SHARED,
456 dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n",
457 num, data->chip->muxtype == pca954x_ismux
458 ? "mux" : "switch", client->name);
463 pca954x_cleanup(muxc);
467 static int pca954x_remove(struct i2c_client *client)
469 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
471 pca954x_cleanup(muxc);
475 #ifdef CONFIG_PM_SLEEP
476 static int pca954x_resume(struct device *dev)
478 struct i2c_client *client = to_i2c_client(dev);
479 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
480 struct pca954x *data = i2c_mux_priv(muxc);
483 return i2c_smbus_write_byte(client, 0);
487 static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
489 static struct i2c_driver pca954x_driver = {
493 .of_match_table = of_match_ptr(pca954x_of_match),
495 .probe = pca954x_probe,
496 .remove = pca954x_remove,
497 .id_table = pca954x_id,
500 module_i2c_driver(pca954x_driver);
503 MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
504 MODULE_LICENSE("GPL v2");