Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * I2C multiplexer
0004  *
0005  * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
0006  * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
0007  *
0008  * This module supports the PCA954x and PCA984x series of I2C multiplexer/switch
0009  * chips made by NXP Semiconductors.
0010  * This includes the:
0011  *   PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547,
0012  *   PCA9548, PCA9846, PCA9847, PCA9848 and PCA9849.
0013  *
0014  * These chips are all controlled via the I2C bus itself, and all have a
0015  * single 8-bit register. The upstream "parent" bus fans out to two,
0016  * four, or eight downstream busses or channels; which of these
0017  * are selected is determined by the chip type and register contents. A
0018  * mux can select only one sub-bus at a time; a switch can select any
0019  * combination simultaneously.
0020  *
0021  * Based on:
0022  *  pca954x.c from Kumar Gala <galak@kernel.crashing.org>
0023  * Copyright (C) 2006
0024  *
0025  * Based on:
0026  *  pca954x.c from Ken Harrenstien
0027  * Copyright (C) 2004 Google, Inc. (Ken Harrenstien)
0028  *
0029  * Based on:
0030  *  i2c-virtual_cb.c from Brian Kuschak <bkuschak@yahoo.com>
0031  * and
0032  *  pca9540.c from Jean Delvare <jdelvare@suse.de>.
0033  */
0034 
0035 #include <linux/device.h>
0036 #include <linux/delay.h>
0037 #include <linux/gpio/consumer.h>
0038 #include <linux/i2c.h>
0039 #include <linux/i2c-mux.h>
0040 #include <linux/interrupt.h>
0041 #include <linux/irq.h>
0042 #include <linux/module.h>
0043 #include <linux/pm.h>
0044 #include <linux/property.h>
0045 #include <linux/slab.h>
0046 #include <linux/spinlock.h>
0047 #include <dt-bindings/mux/mux.h>
0048 
0049 #define PCA954X_MAX_NCHANS 8
0050 
0051 #define PCA954X_IRQ_OFFSET 4
0052 
0053 enum pca_type {
0054     pca_9540,
0055     pca_9542,
0056     pca_9543,
0057     pca_9544,
0058     pca_9545,
0059     pca_9546,
0060     pca_9547,
0061     pca_9548,
0062     pca_9846,
0063     pca_9847,
0064     pca_9848,
0065     pca_9849,
0066 };
0067 
0068 struct chip_desc {
0069     u8 nchans;
0070     u8 enable;  /* used for muxes only */
0071     u8 has_irq;
0072     enum muxtype {
0073         pca954x_ismux = 0,
0074         pca954x_isswi
0075     } muxtype;
0076     struct i2c_device_identity id;
0077 };
0078 
0079 struct pca954x {
0080     const struct chip_desc *chip;
0081 
0082     u8 last_chan;       /* last register value */
0083     /* MUX_IDLE_AS_IS, MUX_IDLE_DISCONNECT or >= 0 for channel */
0084     s32 idle_state;
0085 
0086     struct i2c_client *client;
0087 
0088     struct irq_domain *irq;
0089     unsigned int irq_mask;
0090     raw_spinlock_t lock;
0091 };
0092 
0093 /* Provide specs for the PCA954x types we know about */
0094 static const struct chip_desc chips[] = {
0095     [pca_9540] = {
0096         .nchans = 2,
0097         .enable = 0x4,
0098         .muxtype = pca954x_ismux,
0099         .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
0100     },
0101     [pca_9542] = {
0102         .nchans = 2,
0103         .enable = 0x4,
0104         .has_irq = 1,
0105         .muxtype = pca954x_ismux,
0106         .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
0107     },
0108     [pca_9543] = {
0109         .nchans = 2,
0110         .has_irq = 1,
0111         .muxtype = pca954x_isswi,
0112         .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
0113     },
0114     [pca_9544] = {
0115         .nchans = 4,
0116         .enable = 0x4,
0117         .has_irq = 1,
0118         .muxtype = pca954x_ismux,
0119         .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
0120     },
0121     [pca_9545] = {
0122         .nchans = 4,
0123         .has_irq = 1,
0124         .muxtype = pca954x_isswi,
0125         .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
0126     },
0127     [pca_9546] = {
0128         .nchans = 4,
0129         .muxtype = pca954x_isswi,
0130         .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
0131     },
0132     [pca_9547] = {
0133         .nchans = 8,
0134         .enable = 0x8,
0135         .muxtype = pca954x_ismux,
0136         .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
0137     },
0138     [pca_9548] = {
0139         .nchans = 8,
0140         .muxtype = pca954x_isswi,
0141         .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
0142     },
0143     [pca_9846] = {
0144         .nchans = 4,
0145         .muxtype = pca954x_isswi,
0146         .id = {
0147             .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
0148             .part_id = 0x10b,
0149         },
0150     },
0151     [pca_9847] = {
0152         .nchans = 8,
0153         .enable = 0x8,
0154         .muxtype = pca954x_ismux,
0155         .id = {
0156             .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
0157             .part_id = 0x108,
0158         },
0159     },
0160     [pca_9848] = {
0161         .nchans = 8,
0162         .muxtype = pca954x_isswi,
0163         .id = {
0164             .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
0165             .part_id = 0x10a,
0166         },
0167     },
0168     [pca_9849] = {
0169         .nchans = 4,
0170         .enable = 0x4,
0171         .muxtype = pca954x_ismux,
0172         .id = {
0173             .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
0174             .part_id = 0x109,
0175         },
0176     },
0177 };
0178 
0179 static const struct i2c_device_id pca954x_id[] = {
0180     { "pca9540", pca_9540 },
0181     { "pca9542", pca_9542 },
0182     { "pca9543", pca_9543 },
0183     { "pca9544", pca_9544 },
0184     { "pca9545", pca_9545 },
0185     { "pca9546", pca_9546 },
0186     { "pca9547", pca_9547 },
0187     { "pca9548", pca_9548 },
0188     { "pca9846", pca_9846 },
0189     { "pca9847", pca_9847 },
0190     { "pca9848", pca_9848 },
0191     { "pca9849", pca_9849 },
0192     { }
0193 };
0194 MODULE_DEVICE_TABLE(i2c, pca954x_id);
0195 
0196 static const struct of_device_id pca954x_of_match[] = {
0197     { .compatible = "nxp,pca9540", .data = &chips[pca_9540] },
0198     { .compatible = "nxp,pca9542", .data = &chips[pca_9542] },
0199     { .compatible = "nxp,pca9543", .data = &chips[pca_9543] },
0200     { .compatible = "nxp,pca9544", .data = &chips[pca_9544] },
0201     { .compatible = "nxp,pca9545", .data = &chips[pca_9545] },
0202     { .compatible = "nxp,pca9546", .data = &chips[pca_9546] },
0203     { .compatible = "nxp,pca9547", .data = &chips[pca_9547] },
0204     { .compatible = "nxp,pca9548", .data = &chips[pca_9548] },
0205     { .compatible = "nxp,pca9846", .data = &chips[pca_9846] },
0206     { .compatible = "nxp,pca9847", .data = &chips[pca_9847] },
0207     { .compatible = "nxp,pca9848", .data = &chips[pca_9848] },
0208     { .compatible = "nxp,pca9849", .data = &chips[pca_9849] },
0209     {}
0210 };
0211 MODULE_DEVICE_TABLE(of, pca954x_of_match);
0212 
0213 /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
0214    for this as they will try to lock adapter a second time */
0215 static int pca954x_reg_write(struct i2c_adapter *adap,
0216                  struct i2c_client *client, u8 val)
0217 {
0218     union i2c_smbus_data dummy;
0219 
0220     return __i2c_smbus_xfer(adap, client->addr, client->flags,
0221                 I2C_SMBUS_WRITE, val,
0222                 I2C_SMBUS_BYTE, &dummy);
0223 }
0224 
0225 static u8 pca954x_regval(struct pca954x *data, u8 chan)
0226 {
0227     /* We make switches look like muxes, not sure how to be smarter. */
0228     if (data->chip->muxtype == pca954x_ismux)
0229         return chan | data->chip->enable;
0230     else
0231         return 1 << chan;
0232 }
0233 
0234 static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
0235 {
0236     struct pca954x *data = i2c_mux_priv(muxc);
0237     struct i2c_client *client = data->client;
0238     u8 regval;
0239     int ret = 0;
0240 
0241     regval = pca954x_regval(data, chan);
0242     /* Only select the channel if its different from the last channel */
0243     if (data->last_chan != regval) {
0244         ret = pca954x_reg_write(muxc->parent, client, regval);
0245         data->last_chan = ret < 0 ? 0 : regval;
0246     }
0247 
0248     return ret;
0249 }
0250 
0251 static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
0252 {
0253     struct pca954x *data = i2c_mux_priv(muxc);
0254     struct i2c_client *client = data->client;
0255     s32 idle_state;
0256 
0257     idle_state = READ_ONCE(data->idle_state);
0258     if (idle_state >= 0)
0259         /* Set the mux back to a predetermined channel */
0260         return pca954x_select_chan(muxc, idle_state);
0261 
0262     if (idle_state == MUX_IDLE_DISCONNECT) {
0263         /* Deselect active channel */
0264         data->last_chan = 0;
0265         return pca954x_reg_write(muxc->parent, client,
0266                      data->last_chan);
0267     }
0268 
0269     /* otherwise leave as-is */
0270 
0271     return 0;
0272 }
0273 
0274 static ssize_t idle_state_show(struct device *dev,
0275                     struct device_attribute *attr,
0276                     char *buf)
0277 {
0278     struct i2c_client *client = to_i2c_client(dev);
0279     struct i2c_mux_core *muxc = i2c_get_clientdata(client);
0280     struct pca954x *data = i2c_mux_priv(muxc);
0281 
0282     return sprintf(buf, "%d\n", READ_ONCE(data->idle_state));
0283 }
0284 
0285 static ssize_t idle_state_store(struct device *dev,
0286                 struct device_attribute *attr,
0287                 const char *buf, size_t count)
0288 {
0289     struct i2c_client *client = to_i2c_client(dev);
0290     struct i2c_mux_core *muxc = i2c_get_clientdata(client);
0291     struct pca954x *data = i2c_mux_priv(muxc);
0292     int val;
0293     int ret;
0294 
0295     ret = kstrtoint(buf, 0, &val);
0296     if (ret < 0)
0297         return ret;
0298 
0299     if (val != MUX_IDLE_AS_IS && val != MUX_IDLE_DISCONNECT &&
0300         (val < 0 || val >= data->chip->nchans))
0301         return -EINVAL;
0302 
0303     i2c_lock_bus(muxc->parent, I2C_LOCK_SEGMENT);
0304 
0305     WRITE_ONCE(data->idle_state, val);
0306     /*
0307      * Set the mux into a state consistent with the new
0308      * idle_state.
0309      */
0310     if (data->last_chan || val != MUX_IDLE_DISCONNECT)
0311         ret = pca954x_deselect_mux(muxc, 0);
0312 
0313     i2c_unlock_bus(muxc->parent, I2C_LOCK_SEGMENT);
0314 
0315     return ret < 0 ? ret : count;
0316 }
0317 
0318 static DEVICE_ATTR_RW(idle_state);
0319 
0320 static irqreturn_t pca954x_irq_handler(int irq, void *dev_id)
0321 {
0322     struct pca954x *data = dev_id;
0323     unsigned long pending;
0324     int ret, i;
0325 
0326     ret = i2c_smbus_read_byte(data->client);
0327     if (ret < 0)
0328         return IRQ_NONE;
0329 
0330     pending = (ret >> PCA954X_IRQ_OFFSET) & (BIT(data->chip->nchans) - 1);
0331     for_each_set_bit(i, &pending, data->chip->nchans)
0332         handle_nested_irq(irq_linear_revmap(data->irq, i));
0333 
0334     return IRQ_RETVAL(pending);
0335 }
0336 
0337 static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type)
0338 {
0339     if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW)
0340         return -EINVAL;
0341     return 0;
0342 }
0343 
0344 static struct irq_chip pca954x_irq_chip = {
0345     .name = "i2c-mux-pca954x",
0346     .irq_set_type = pca954x_irq_set_type,
0347 };
0348 
0349 static int pca954x_irq_setup(struct i2c_mux_core *muxc)
0350 {
0351     struct pca954x *data = i2c_mux_priv(muxc);
0352     struct i2c_client *client = data->client;
0353     int c, irq;
0354 
0355     if (!data->chip->has_irq || client->irq <= 0)
0356         return 0;
0357 
0358     raw_spin_lock_init(&data->lock);
0359 
0360     data->irq = irq_domain_add_linear(client->dev.of_node,
0361                       data->chip->nchans,
0362                       &irq_domain_simple_ops, data);
0363     if (!data->irq)
0364         return -ENODEV;
0365 
0366     for (c = 0; c < data->chip->nchans; c++) {
0367         irq = irq_create_mapping(data->irq, c);
0368         if (!irq) {
0369             dev_err(&client->dev, "failed irq create map\n");
0370             return -EINVAL;
0371         }
0372         irq_set_chip_data(irq, data);
0373         irq_set_chip_and_handler(irq, &pca954x_irq_chip,
0374             handle_simple_irq);
0375     }
0376 
0377     return 0;
0378 }
0379 
0380 static void pca954x_cleanup(struct i2c_mux_core *muxc)
0381 {
0382     struct pca954x *data = i2c_mux_priv(muxc);
0383     int c, irq;
0384 
0385     if (data->irq) {
0386         for (c = 0; c < data->chip->nchans; c++) {
0387             irq = irq_find_mapping(data->irq, c);
0388             irq_dispose_mapping(irq);
0389         }
0390         irq_domain_remove(data->irq);
0391     }
0392     i2c_mux_del_adapters(muxc);
0393 }
0394 
0395 static int pca954x_init(struct i2c_client *client, struct pca954x *data)
0396 {
0397     int ret;
0398 
0399     if (data->idle_state >= 0)
0400         data->last_chan = pca954x_regval(data, data->idle_state);
0401     else
0402         data->last_chan = 0; /* Disconnect multiplexer */
0403 
0404     ret = i2c_smbus_write_byte(client, data->last_chan);
0405     if (ret < 0)
0406         data->last_chan = 0;
0407 
0408     return ret;
0409 }
0410 
0411 /*
0412  * I2C init/probing/exit functions
0413  */
0414 static int pca954x_probe(struct i2c_client *client,
0415              const struct i2c_device_id *id)
0416 {
0417     struct i2c_adapter *adap = client->adapter;
0418     struct device *dev = &client->dev;
0419     struct gpio_desc *gpio;
0420     struct i2c_mux_core *muxc;
0421     struct pca954x *data;
0422     int num;
0423     int ret;
0424 
0425     if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
0426         return -ENODEV;
0427 
0428     muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0,
0429                  pca954x_select_chan, pca954x_deselect_mux);
0430     if (!muxc)
0431         return -ENOMEM;
0432     data = i2c_mux_priv(muxc);
0433 
0434     i2c_set_clientdata(client, muxc);
0435     data->client = client;
0436 
0437     /* Reset the mux if a reset GPIO is specified. */
0438     gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
0439     if (IS_ERR(gpio))
0440         return PTR_ERR(gpio);
0441     if (gpio) {
0442         udelay(1);
0443         gpiod_set_value_cansleep(gpio, 0);
0444         /* Give the chip some time to recover. */
0445         udelay(1);
0446     }
0447 
0448     data->chip = device_get_match_data(dev);
0449     if (!data->chip)
0450         data->chip = &chips[id->driver_data];
0451 
0452     if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) {
0453         struct i2c_device_identity id;
0454 
0455         ret = i2c_get_device_id(client, &id);
0456         if (ret && ret != -EOPNOTSUPP)
0457             return ret;
0458 
0459         if (!ret &&
0460             (id.manufacturer_id != data->chip->id.manufacturer_id ||
0461              id.part_id != data->chip->id.part_id)) {
0462             dev_warn(dev, "unexpected device id %03x-%03x-%x\n",
0463                  id.manufacturer_id, id.part_id,
0464                  id.die_revision);
0465             return -ENODEV;
0466         }
0467     }
0468 
0469     data->idle_state = MUX_IDLE_AS_IS;
0470     if (device_property_read_u32(dev, "idle-state", &data->idle_state)) {
0471         if (device_property_read_bool(dev, "i2c-mux-idle-disconnect"))
0472             data->idle_state = MUX_IDLE_DISCONNECT;
0473     }
0474 
0475     /*
0476      * Write the mux register at addr to verify
0477      * that the mux is in fact present. This also
0478      * initializes the mux to a channel
0479      * or disconnected state.
0480      */
0481     ret = pca954x_init(client, data);
0482     if (ret < 0) {
0483         dev_warn(dev, "probe failed\n");
0484         return -ENODEV;
0485     }
0486 
0487     ret = pca954x_irq_setup(muxc);
0488     if (ret)
0489         goto fail_cleanup;
0490 
0491     /* Now create an adapter for each channel */
0492     for (num = 0; num < data->chip->nchans; num++) {
0493         ret = i2c_mux_add_adapter(muxc, 0, num, 0);
0494         if (ret)
0495             goto fail_cleanup;
0496     }
0497 
0498     if (data->irq) {
0499         ret = devm_request_threaded_irq(dev, data->client->irq,
0500                         NULL, pca954x_irq_handler,
0501                         IRQF_ONESHOT | IRQF_SHARED,
0502                         "pca954x", data);
0503         if (ret)
0504             goto fail_cleanup;
0505     }
0506 
0507     /*
0508      * The attr probably isn't going to be needed in most cases,
0509      * so don't fail completely on error.
0510      */
0511     device_create_file(dev, &dev_attr_idle_state);
0512 
0513     dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n",
0514          num, data->chip->muxtype == pca954x_ismux
0515                 ? "mux" : "switch", client->name);
0516 
0517     return 0;
0518 
0519 fail_cleanup:
0520     pca954x_cleanup(muxc);
0521     return ret;
0522 }
0523 
0524 static int pca954x_remove(struct i2c_client *client)
0525 {
0526     struct i2c_mux_core *muxc = i2c_get_clientdata(client);
0527 
0528     device_remove_file(&client->dev, &dev_attr_idle_state);
0529 
0530     pca954x_cleanup(muxc);
0531     return 0;
0532 }
0533 
0534 #ifdef CONFIG_PM_SLEEP
0535 static int pca954x_resume(struct device *dev)
0536 {
0537     struct i2c_client *client = to_i2c_client(dev);
0538     struct i2c_mux_core *muxc = i2c_get_clientdata(client);
0539     struct pca954x *data = i2c_mux_priv(muxc);
0540     int ret;
0541 
0542     ret = pca954x_init(client, data);
0543     if (ret < 0)
0544         dev_err(&client->dev, "failed to verify mux presence\n");
0545 
0546     return ret;
0547 }
0548 #endif
0549 
0550 static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
0551 
0552 static struct i2c_driver pca954x_driver = {
0553     .driver     = {
0554         .name   = "pca954x",
0555         .pm = &pca954x_pm,
0556         .of_match_table = pca954x_of_match,
0557     },
0558     .probe      = pca954x_probe,
0559     .remove     = pca954x_remove,
0560     .id_table   = pca954x_id,
0561 };
0562 
0563 module_i2c_driver(pca954x_driver);
0564 
0565 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
0566 MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
0567 MODULE_LICENSE("GPL v2");