Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * I2C multiplexer |
| 3 | * |
| 4 | * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it> |
| 5 | * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it> |
| 6 | * |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 7 | * This module supports the PCA954x and PCA954x series of I2C multiplexer/switch |
| 8 | * chips made by NXP Semiconductors. |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 9 | * This includes the: |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 10 | * PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547, |
| 11 | * PCA9548, PCA9846, PCA9847, PCA9848 and PCA9849. |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 12 | * |
| 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. |
| 19 | * |
| 20 | * Based on: |
| 21 | * pca954x.c from Kumar Gala <galak@kernel.crashing.org> |
| 22 | * Copyright (C) 2006 |
| 23 | * |
| 24 | * Based on: |
| 25 | * pca954x.c from Ken Harrenstien |
| 26 | * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) |
| 27 | * |
| 28 | * Based on: |
| 29 | * i2c-virtual_cb.c from Brian Kuschak <bkuschak@yahoo.com> |
| 30 | * and |
Jean Delvare | 7c81c60f | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 31 | * pca9540.c from Jean Delvare <jdelvare@suse.de>. |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 32 | * |
| 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. |
| 36 | */ |
| 37 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 38 | #include <linux/device.h> |
Mike Looijmans | a2f8445 | 2018-05-01 13:42:12 +0200 | [diff] [blame] | 39 | #include <linux/delay.h> |
Laurent Pinchart | 642653d | 2014-06-04 18:56:32 +0200 | [diff] [blame] | 40 | #include <linux/gpio/consumer.h> |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 41 | #include <linux/i2c.h> |
| 42 | #include <linux/i2c-mux.h> |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 43 | #include <linux/interrupt.h> |
| 44 | #include <linux/irq.h> |
Laurent Pinchart | 4b9b007 | 2013-11-29 01:51:22 +0100 | [diff] [blame] | 45 | #include <linux/module.h> |
Alexander Sverdlin | 72f0271 | 2015-01-23 16:41:29 +0100 | [diff] [blame] | 46 | #include <linux/of.h> |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 47 | #include <linux/of_device.h> |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 48 | #include <linux/of_irq.h> |
Wolfram Sang | 2be03ae | 2017-08-14 10:23:25 +0200 | [diff] [blame] | 49 | #include <linux/platform_data/pca954x.h> |
Jisheng Zhang | f5e596c | 2014-07-25 19:57:46 +0800 | [diff] [blame] | 50 | #include <linux/pm.h> |
Laurent Pinchart | 4b9b007 | 2013-11-29 01:51:22 +0100 | [diff] [blame] | 51 | #include <linux/slab.h> |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 52 | #include <linux/spinlock.h> |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 53 | |
| 54 | #define PCA954X_MAX_NCHANS 8 |
| 55 | |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 56 | #define PCA954X_IRQ_OFFSET 4 |
| 57 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 58 | enum pca_type { |
| 59 | pca_9540, |
| 60 | pca_9542, |
| 61 | pca_9543, |
| 62 | pca_9544, |
| 63 | pca_9545, |
| 64 | pca_9546, |
| 65 | pca_9547, |
| 66 | pca_9548, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 67 | pca_9846, |
| 68 | pca_9847, |
| 69 | pca_9848, |
| 70 | pca_9849, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 73 | struct chip_desc { |
| 74 | u8 nchans; |
| 75 | u8 enable; /* used for muxes only */ |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 76 | u8 has_irq; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 77 | enum muxtype { |
| 78 | pca954x_ismux = 0, |
| 79 | pca954x_isswi |
| 80 | } muxtype; |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 81 | struct i2c_device_identity id; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 84 | struct pca954x { |
| 85 | const struct chip_desc *chip; |
| 86 | |
| 87 | u8 last_chan; /* last register value */ |
| 88 | u8 deselect; |
| 89 | struct i2c_client *client; |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 90 | |
| 91 | struct irq_domain *irq; |
| 92 | unsigned int irq_mask; |
Julia Cartwright | 743cc37 | 2017-03-09 10:21:59 -0600 | [diff] [blame] | 93 | raw_spinlock_t lock; |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 96 | /* Provide specs for the PCA954x types we know about */ |
| 97 | static const struct chip_desc chips[] = { |
| 98 | [pca_9540] = { |
| 99 | .nchans = 2, |
| 100 | .enable = 0x4, |
| 101 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 102 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 103 | }, |
Phil Reid | f8251f1 | 2017-01-25 09:31:06 +0800 | [diff] [blame] | 104 | [pca_9542] = { |
| 105 | .nchans = 2, |
| 106 | .enable = 0x4, |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 107 | .has_irq = 1, |
Phil Reid | f8251f1 | 2017-01-25 09:31:06 +0800 | [diff] [blame] | 108 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 109 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Phil Reid | f8251f1 | 2017-01-25 09:31:06 +0800 | [diff] [blame] | 110 | }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 111 | [pca_9543] = { |
| 112 | .nchans = 2, |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 113 | .has_irq = 1, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 114 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 115 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 116 | }, |
| 117 | [pca_9544] = { |
| 118 | .nchans = 4, |
| 119 | .enable = 0x4, |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 120 | .has_irq = 1, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 121 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 122 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 123 | }, |
| 124 | [pca_9545] = { |
| 125 | .nchans = 4, |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 126 | .has_irq = 1, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 127 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 128 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 129 | }, |
Mike Looijmans | dbe4d69 | 2017-03-23 10:00:36 +0100 | [diff] [blame] | 130 | [pca_9546] = { |
| 131 | .nchans = 4, |
| 132 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 133 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Mike Looijmans | dbe4d69 | 2017-03-23 10:00:36 +0100 | [diff] [blame] | 134 | }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 135 | [pca_9547] = { |
| 136 | .nchans = 8, |
| 137 | .enable = 0x8, |
| 138 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 139 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 140 | }, |
| 141 | [pca_9548] = { |
| 142 | .nchans = 8, |
| 143 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 144 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 145 | }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 146 | [pca_9846] = { |
| 147 | .nchans = 4, |
| 148 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 149 | .id = { |
| 150 | .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, |
| 151 | .part_id = 0x10b, |
| 152 | }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 153 | }, |
| 154 | [pca_9847] = { |
| 155 | .nchans = 8, |
| 156 | .enable = 0x8, |
| 157 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 158 | .id = { |
| 159 | .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, |
| 160 | .part_id = 0x108, |
| 161 | }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 162 | }, |
| 163 | [pca_9848] = { |
| 164 | .nchans = 8, |
| 165 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 166 | .id = { |
| 167 | .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, |
| 168 | .part_id = 0x10a, |
| 169 | }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 170 | }, |
| 171 | [pca_9849] = { |
| 172 | .nchans = 4, |
| 173 | .enable = 0x4, |
| 174 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 175 | .id = { |
| 176 | .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, |
| 177 | .part_id = 0x109, |
| 178 | }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 179 | }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static const struct i2c_device_id pca954x_id[] = { |
| 183 | { "pca9540", pca_9540 }, |
Phil Reid | f8251f1 | 2017-01-25 09:31:06 +0800 | [diff] [blame] | 184 | { "pca9542", pca_9542 }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 185 | { "pca9543", pca_9543 }, |
| 186 | { "pca9544", pca_9544 }, |
| 187 | { "pca9545", pca_9545 }, |
Mike Looijmans | dbe4d69 | 2017-03-23 10:00:36 +0100 | [diff] [blame] | 188 | { "pca9546", pca_9546 }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 189 | { "pca9547", pca_9547 }, |
| 190 | { "pca9548", pca_9548 }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 191 | { "pca9846", pca_9846 }, |
| 192 | { "pca9847", pca_9847 }, |
| 193 | { "pca9848", pca_9848 }, |
| 194 | { "pca9849", pca_9849 }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 195 | { } |
| 196 | }; |
| 197 | MODULE_DEVICE_TABLE(i2c, pca954x_id); |
| 198 | |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 199 | #ifdef CONFIG_OF |
| 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] }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 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] }, |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 213 | {} |
| 214 | }; |
Javier Martinez Canillas | acf6ef1 | 2017-01-16 10:54:54 -0300 | [diff] [blame] | 215 | MODULE_DEVICE_TABLE(of, pca954x_of_match); |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 216 | #endif |
| 217 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 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) |
| 222 | { |
| 223 | int ret = -ENODEV; |
| 224 | |
| 225 | if (adap->algo->master_xfer) { |
| 226 | struct i2c_msg msg; |
| 227 | char buf[1]; |
| 228 | |
| 229 | msg.addr = client->addr; |
| 230 | msg.flags = 0; |
| 231 | msg.len = 1; |
| 232 | buf[0] = val; |
| 233 | msg.buf = buf; |
Alexander Sverdlin | 0a8237a | 2015-06-12 14:41:00 +0200 | [diff] [blame] | 234 | ret = __i2c_transfer(adap, &msg, 1); |
Russell King | 7f638c1 | 2016-12-17 12:10:56 +0000 | [diff] [blame] | 235 | |
| 236 | if (ret >= 0 && ret != 1) |
| 237 | ret = -EREMOTEIO; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 238 | } else { |
| 239 | union i2c_smbus_data data; |
| 240 | ret = adap->algo->smbus_xfer(adap, client->addr, |
| 241 | client->flags, |
| 242 | I2C_SMBUS_WRITE, |
| 243 | val, I2C_SMBUS_BYTE, &data); |
| 244 | } |
| 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 249 | static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan) |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 250 | { |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 251 | struct pca954x *data = i2c_mux_priv(muxc); |
| 252 | struct i2c_client *client = data->client; |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 253 | const struct chip_desc *chip = data->chip; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 254 | u8 regval; |
| 255 | int ret = 0; |
| 256 | |
| 257 | /* we make switches look like muxes, not sure how to be smarter */ |
| 258 | if (chip->muxtype == pca954x_ismux) |
| 259 | regval = chan | chip->enable; |
| 260 | else |
| 261 | regval = 1 << chan; |
| 262 | |
| 263 | /* Only select the channel if its different from the last channel */ |
| 264 | if (data->last_chan != regval) { |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 265 | ret = pca954x_reg_write(muxc->parent, client, regval); |
Russell King | 7f638c1 | 2016-12-17 12:10:56 +0000 | [diff] [blame] | 266 | data->last_chan = ret < 0 ? 0 : regval; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | return ret; |
| 270 | } |
| 271 | |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 272 | static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan) |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 273 | { |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 274 | struct pca954x *data = i2c_mux_priv(muxc); |
| 275 | struct i2c_client *client = data->client; |
| 276 | |
| 277 | if (!(data->deselect & (1 << chan))) |
| 278 | return 0; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 279 | |
| 280 | /* Deselect active channel */ |
| 281 | data->last_chan = 0; |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 282 | return pca954x_reg_write(muxc->parent, client, data->last_chan); |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 285 | static irqreturn_t pca954x_irq_handler(int irq, void *dev_id) |
| 286 | { |
| 287 | struct pca954x *data = dev_id; |
| 288 | unsigned int child_irq; |
| 289 | int ret, i, handled = 0; |
| 290 | |
| 291 | ret = i2c_smbus_read_byte(data->client); |
| 292 | if (ret < 0) |
| 293 | return IRQ_NONE; |
| 294 | |
| 295 | for (i = 0; i < data->chip->nchans; i++) { |
| 296 | if (ret & BIT(PCA954X_IRQ_OFFSET + i)) { |
| 297 | child_irq = irq_linear_revmap(data->irq, i); |
| 298 | handle_nested_irq(child_irq); |
| 299 | handled++; |
| 300 | } |
| 301 | } |
| 302 | return handled ? IRQ_HANDLED : IRQ_NONE; |
| 303 | } |
| 304 | |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 305 | static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type) |
| 306 | { |
| 307 | if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW) |
| 308 | return -EINVAL; |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static struct irq_chip pca954x_irq_chip = { |
| 313 | .name = "i2c-mux-pca954x", |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 314 | .irq_set_type = pca954x_irq_set_type, |
| 315 | }; |
| 316 | |
| 317 | static int pca954x_irq_setup(struct i2c_mux_core *muxc) |
| 318 | { |
| 319 | struct pca954x *data = i2c_mux_priv(muxc); |
| 320 | struct i2c_client *client = data->client; |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 321 | int c, irq; |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 322 | |
| 323 | if (!data->chip->has_irq || client->irq <= 0) |
| 324 | return 0; |
| 325 | |
Julia Cartwright | 743cc37 | 2017-03-09 10:21:59 -0600 | [diff] [blame] | 326 | raw_spin_lock_init(&data->lock); |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 327 | |
| 328 | data->irq = irq_domain_add_linear(client->dev.of_node, |
| 329 | data->chip->nchans, |
| 330 | &irq_domain_simple_ops, data); |
| 331 | if (!data->irq) |
| 332 | return -ENODEV; |
| 333 | |
| 334 | for (c = 0; c < data->chip->nchans; c++) { |
| 335 | irq = irq_create_mapping(data->irq, c); |
Phil Reid | e460617 | 2017-08-24 17:31:06 +0800 | [diff] [blame] | 336 | if (!irq) { |
| 337 | dev_err(&client->dev, "failed irq create map\n"); |
| 338 | return -EINVAL; |
| 339 | } |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 340 | irq_set_chip_data(irq, data); |
| 341 | irq_set_chip_and_handler(irq, &pca954x_irq_chip, |
| 342 | handle_simple_irq); |
| 343 | } |
| 344 | |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 345 | return 0; |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 346 | } |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 347 | |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 348 | static void pca954x_cleanup(struct i2c_mux_core *muxc) |
| 349 | { |
| 350 | struct pca954x *data = i2c_mux_priv(muxc); |
| 351 | int c, irq; |
| 352 | |
| 353 | if (data->irq) { |
| 354 | for (c = 0; c < data->chip->nchans; c++) { |
| 355 | irq = irq_find_mapping(data->irq, c); |
| 356 | irq_dispose_mapping(irq); |
| 357 | } |
| 358 | irq_domain_remove(data->irq); |
| 359 | } |
| 360 | i2c_mux_del_adapters(muxc); |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 361 | } |
| 362 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 363 | /* |
| 364 | * I2C init/probing/exit functions |
| 365 | */ |
Guenter Roeck | db79f2a | 2010-10-24 18:16:59 +0200 | [diff] [blame] | 366 | static int pca954x_probe(struct i2c_client *client, |
| 367 | const struct i2c_device_id *id) |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 368 | { |
| 369 | struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent); |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 370 | struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev); |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame^] | 371 | struct device *dev = &client->dev; |
| 372 | struct device_node *np = dev->of_node; |
Alexander Sverdlin | 72f0271 | 2015-01-23 16:41:29 +0100 | [diff] [blame] | 373 | bool idle_disconnect_dt; |
Laurent Pinchart | 4807e84 | 2014-06-03 13:15:26 +0200 | [diff] [blame] | 374 | struct gpio_desc *gpio; |
Jean Delvare | eee543e | 2012-10-05 22:23:51 +0200 | [diff] [blame] | 375 | int num, force, class; |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 376 | struct i2c_mux_core *muxc; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 377 | struct pca954x *data; |
Laurent Pinchart | bc12cfc | 2013-11-29 01:51:23 +0100 | [diff] [blame] | 378 | int ret; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 379 | |
| 380 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE)) |
Laurent Pinchart | bc12cfc | 2013-11-29 01:51:23 +0100 | [diff] [blame] | 381 | return -ENODEV; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 382 | |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame^] | 383 | muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0, |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 384 | pca954x_select_chan, pca954x_deselect_mux); |
| 385 | if (!muxc) |
Laurent Pinchart | bc12cfc | 2013-11-29 01:51:23 +0100 | [diff] [blame] | 386 | return -ENOMEM; |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 387 | data = i2c_mux_priv(muxc); |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 388 | |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 389 | i2c_set_clientdata(client, muxc); |
| 390 | data->client = client; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 391 | |
Mike Looijmans | a2f8445 | 2018-05-01 13:42:12 +0200 | [diff] [blame] | 392 | /* Reset the mux if a reset GPIO is specified. */ |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame^] | 393 | gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); |
Uwe Kleine-König | 58b59e0 | 2015-02-17 10:12:08 +0100 | [diff] [blame] | 394 | if (IS_ERR(gpio)) |
| 395 | return PTR_ERR(gpio); |
Mike Looijmans | a2f8445 | 2018-05-01 13:42:12 +0200 | [diff] [blame] | 396 | if (gpio) { |
| 397 | udelay(1); |
| 398 | gpiod_set_value_cansleep(gpio, 0); |
| 399 | /* Give the chip some time to recover. */ |
| 400 | udelay(1); |
| 401 | } |
Laurent Pinchart | 1209795 | 2013-11-29 01:51:25 +0100 | [diff] [blame] | 402 | |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame^] | 403 | data->chip = of_device_get_match_data(dev); |
Julia Lawall | b10d7a1 | 2018-05-21 11:49:08 +0200 | [diff] [blame] | 404 | if (!data->chip) |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 405 | data->chip = &chips[id->driver_data]; |
| 406 | |
| 407 | if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) { |
| 408 | struct i2c_device_identity id; |
| 409 | |
| 410 | ret = i2c_get_device_id(client, &id); |
| 411 | if (ret && ret != -EOPNOTSUPP) |
| 412 | return ret; |
| 413 | |
| 414 | if (!ret && |
| 415 | (id.manufacturer_id != data->chip->id.manufacturer_id || |
| 416 | id.part_id != data->chip->id.part_id)) { |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame^] | 417 | dev_warn(dev, "unexpected device id %03x-%03x-%x\n", |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 418 | id.manufacturer_id, id.part_id, |
| 419 | id.die_revision); |
| 420 | return -ENODEV; |
| 421 | } |
| 422 | } |
| 423 | |
Petri Gynther | cd823db | 2011-06-29 11:36:11 +0200 | [diff] [blame] | 424 | /* Write the mux register at addr to verify |
| 425 | * that the mux is in fact present. This also |
| 426 | * initializes the mux to disconnected state. |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 427 | */ |
Petri Gynther | cd823db | 2011-06-29 11:36:11 +0200 | [diff] [blame] | 428 | if (i2c_smbus_write_byte(client, 0) < 0) { |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame^] | 429 | dev_warn(dev, "probe failed\n"); |
Laurent Pinchart | bc12cfc | 2013-11-29 01:51:23 +0100 | [diff] [blame] | 430 | return -ENODEV; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 433 | data->last_chan = 0; /* force the first selection */ |
| 434 | |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame^] | 435 | idle_disconnect_dt = np && |
| 436 | of_property_read_bool(np, "i2c-mux-idle-disconnect"); |
Alexander Sverdlin | 72f0271 | 2015-01-23 16:41:29 +0100 | [diff] [blame] | 437 | |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 438 | ret = pca954x_irq_setup(muxc); |
| 439 | if (ret) |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 440 | goto fail_cleanup; |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 441 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 442 | /* Now create an adapter for each channel */ |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 443 | for (num = 0; num < data->chip->nchans; num++) { |
Alexander Sverdlin | 72f0271 | 2015-01-23 16:41:29 +0100 | [diff] [blame] | 444 | bool idle_disconnect_pd = false; |
| 445 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 446 | force = 0; /* dynamic adap number */ |
Jean Delvare | eee543e | 2012-10-05 22:23:51 +0200 | [diff] [blame] | 447 | class = 0; /* no class by default */ |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 448 | if (pdata) { |
Jean Delvare | eee543e | 2012-10-05 22:23:51 +0200 | [diff] [blame] | 449 | if (num < pdata->num_modes) { |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 450 | /* force static number */ |
| 451 | force = pdata->modes[num].adap_id; |
Jean Delvare | eee543e | 2012-10-05 22:23:51 +0200 | [diff] [blame] | 452 | class = pdata->modes[num].class; |
| 453 | } else |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 454 | /* discard unconfigured channels */ |
| 455 | break; |
Alexander Sverdlin | 72f0271 | 2015-01-23 16:41:29 +0100 | [diff] [blame] | 456 | idle_disconnect_pd = pdata->modes[num].deselect_on_exit; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 457 | } |
Alex Hemme | ad092de | 2016-11-19 10:48:38 +0100 | [diff] [blame] | 458 | data->deselect |= (idle_disconnect_pd || |
| 459 | idle_disconnect_dt) << num; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 460 | |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 461 | ret = i2c_mux_add_adapter(muxc, force, num, class); |
Peter Rosin | 0756ac3 | 2017-04-03 10:14:22 +0200 | [diff] [blame] | 462 | if (ret) |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 463 | goto fail_cleanup; |
| 464 | } |
| 465 | |
| 466 | if (data->irq) { |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame^] | 467 | ret = devm_request_threaded_irq(dev, data->client->irq, |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 468 | NULL, pca954x_irq_handler, |
| 469 | IRQF_ONESHOT | IRQF_SHARED, |
| 470 | "pca954x", data); |
| 471 | if (ret) |
| 472 | goto fail_cleanup; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 473 | } |
| 474 | |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame^] | 475 | dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n", |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 476 | num, data->chip->muxtype == pca954x_ismux |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 477 | ? "mux" : "switch", client->name); |
| 478 | |
| 479 | return 0; |
| 480 | |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 481 | fail_cleanup: |
| 482 | pca954x_cleanup(muxc); |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 483 | return ret; |
| 484 | } |
| 485 | |
Guenter Roeck | db79f2a | 2010-10-24 18:16:59 +0200 | [diff] [blame] | 486 | static int pca954x_remove(struct i2c_client *client) |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 487 | { |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 488 | struct i2c_mux_core *muxc = i2c_get_clientdata(client); |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 489 | |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 490 | pca954x_cleanup(muxc); |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
Jisheng Zhang | f5e596c | 2014-07-25 19:57:46 +0800 | [diff] [blame] | 494 | #ifdef CONFIG_PM_SLEEP |
| 495 | static int pca954x_resume(struct device *dev) |
| 496 | { |
| 497 | struct i2c_client *client = to_i2c_client(dev); |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 498 | struct i2c_mux_core *muxc = i2c_get_clientdata(client); |
| 499 | struct pca954x *data = i2c_mux_priv(muxc); |
Jisheng Zhang | f5e596c | 2014-07-25 19:57:46 +0800 | [diff] [blame] | 500 | |
| 501 | data->last_chan = 0; |
| 502 | return i2c_smbus_write_byte(client, 0); |
| 503 | } |
| 504 | #endif |
| 505 | |
| 506 | static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume); |
| 507 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 508 | static struct i2c_driver pca954x_driver = { |
| 509 | .driver = { |
| 510 | .name = "pca954x", |
Jisheng Zhang | f5e596c | 2014-07-25 19:57:46 +0800 | [diff] [blame] | 511 | .pm = &pca954x_pm, |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 512 | .of_match_table = of_match_ptr(pca954x_of_match), |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 513 | }, |
| 514 | .probe = pca954x_probe, |
Guenter Roeck | db79f2a | 2010-10-24 18:16:59 +0200 | [diff] [blame] | 515 | .remove = pca954x_remove, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 516 | .id_table = pca954x_id, |
| 517 | }; |
| 518 | |
Axel Lin | de05497 | 2012-03-26 21:47:19 +0200 | [diff] [blame] | 519 | module_i2c_driver(pca954x_driver); |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 520 | |
| 521 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 522 | MODULE_DESCRIPTION("PCA954x I2C mux/switch driver"); |
| 523 | MODULE_LICENSE("GPL v2"); |