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> |
Jisheng Zhang | f5e596c | 2014-07-25 19:57:46 +0800 | [diff] [blame] | 49 | #include <linux/pm.h> |
Laurent Pinchart | 4b9b007 | 2013-11-29 01:51:22 +0100 | [diff] [blame] | 50 | #include <linux/slab.h> |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 51 | #include <linux/spinlock.h> |
Robert Shearman | f1fb64b | 2019-02-28 11:43:43 +0000 | [diff] [blame^] | 52 | #include <dt-bindings/mux/mux.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 */ |
Robert Shearman | f1fb64b | 2019-02-28 11:43:43 +0000 | [diff] [blame^] | 88 | /* MUX_IDLE_AS_IS, MUX_IDLE_DISCONNECT or >= 0 for channel */ |
| 89 | s8 idle_state; |
| 90 | |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 91 | struct i2c_client *client; |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 92 | |
| 93 | struct irq_domain *irq; |
| 94 | unsigned int irq_mask; |
Julia Cartwright | 743cc37 | 2017-03-09 10:21:59 -0600 | [diff] [blame] | 95 | raw_spinlock_t lock; |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 98 | /* Provide specs for the PCA954x types we know about */ |
| 99 | static const struct chip_desc chips[] = { |
| 100 | [pca_9540] = { |
| 101 | .nchans = 2, |
| 102 | .enable = 0x4, |
| 103 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 104 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 105 | }, |
Phil Reid | f8251f1 | 2017-01-25 09:31:06 +0800 | [diff] [blame] | 106 | [pca_9542] = { |
| 107 | .nchans = 2, |
| 108 | .enable = 0x4, |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 109 | .has_irq = 1, |
Phil Reid | f8251f1 | 2017-01-25 09:31:06 +0800 | [diff] [blame] | 110 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 111 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Phil Reid | f8251f1 | 2017-01-25 09:31:06 +0800 | [diff] [blame] | 112 | }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 113 | [pca_9543] = { |
| 114 | .nchans = 2, |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 115 | .has_irq = 1, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 116 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 117 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 118 | }, |
| 119 | [pca_9544] = { |
| 120 | .nchans = 4, |
| 121 | .enable = 0x4, |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 122 | .has_irq = 1, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 123 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 124 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 125 | }, |
| 126 | [pca_9545] = { |
| 127 | .nchans = 4, |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 128 | .has_irq = 1, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 129 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 130 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 131 | }, |
Mike Looijmans | dbe4d69 | 2017-03-23 10:00:36 +0100 | [diff] [blame] | 132 | [pca_9546] = { |
| 133 | .nchans = 4, |
| 134 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 135 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Mike Looijmans | dbe4d69 | 2017-03-23 10:00:36 +0100 | [diff] [blame] | 136 | }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 137 | [pca_9547] = { |
| 138 | .nchans = 8, |
| 139 | .enable = 0x8, |
| 140 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 141 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 142 | }, |
| 143 | [pca_9548] = { |
| 144 | .nchans = 8, |
| 145 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 146 | .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 147 | }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 148 | [pca_9846] = { |
| 149 | .nchans = 4, |
| 150 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 151 | .id = { |
| 152 | .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, |
| 153 | .part_id = 0x10b, |
| 154 | }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 155 | }, |
| 156 | [pca_9847] = { |
| 157 | .nchans = 8, |
| 158 | .enable = 0x8, |
| 159 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 160 | .id = { |
| 161 | .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, |
| 162 | .part_id = 0x108, |
| 163 | }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 164 | }, |
| 165 | [pca_9848] = { |
| 166 | .nchans = 8, |
| 167 | .muxtype = pca954x_isswi, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 168 | .id = { |
| 169 | .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, |
| 170 | .part_id = 0x10a, |
| 171 | }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 172 | }, |
| 173 | [pca_9849] = { |
| 174 | .nchans = 4, |
| 175 | .enable = 0x4, |
| 176 | .muxtype = pca954x_ismux, |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 177 | .id = { |
| 178 | .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, |
| 179 | .part_id = 0x109, |
| 180 | }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 181 | }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | static const struct i2c_device_id pca954x_id[] = { |
| 185 | { "pca9540", pca_9540 }, |
Phil Reid | f8251f1 | 2017-01-25 09:31:06 +0800 | [diff] [blame] | 186 | { "pca9542", pca_9542 }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 187 | { "pca9543", pca_9543 }, |
| 188 | { "pca9544", pca_9544 }, |
| 189 | { "pca9545", pca_9545 }, |
Mike Looijmans | dbe4d69 | 2017-03-23 10:00:36 +0100 | [diff] [blame] | 190 | { "pca9546", pca_9546 }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 191 | { "pca9547", pca_9547 }, |
| 192 | { "pca9548", pca_9548 }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 193 | { "pca9846", pca_9846 }, |
| 194 | { "pca9847", pca_9847 }, |
| 195 | { "pca9848", pca_9848 }, |
| 196 | { "pca9849", pca_9849 }, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 197 | { } |
| 198 | }; |
| 199 | MODULE_DEVICE_TABLE(i2c, pca954x_id); |
| 200 | |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 201 | #ifdef CONFIG_OF |
| 202 | static const struct of_device_id pca954x_of_match[] = { |
| 203 | { .compatible = "nxp,pca9540", .data = &chips[pca_9540] }, |
| 204 | { .compatible = "nxp,pca9542", .data = &chips[pca_9542] }, |
| 205 | { .compatible = "nxp,pca9543", .data = &chips[pca_9543] }, |
| 206 | { .compatible = "nxp,pca9544", .data = &chips[pca_9544] }, |
| 207 | { .compatible = "nxp,pca9545", .data = &chips[pca_9545] }, |
| 208 | { .compatible = "nxp,pca9546", .data = &chips[pca_9546] }, |
| 209 | { .compatible = "nxp,pca9547", .data = &chips[pca_9547] }, |
| 210 | { .compatible = "nxp,pca9548", .data = &chips[pca_9548] }, |
Adrian Fiergolski | 8f6d601 | 2017-12-25 22:26:46 +0100 | [diff] [blame] | 211 | { .compatible = "nxp,pca9846", .data = &chips[pca_9846] }, |
| 212 | { .compatible = "nxp,pca9847", .data = &chips[pca_9847] }, |
| 213 | { .compatible = "nxp,pca9848", .data = &chips[pca_9848] }, |
| 214 | { .compatible = "nxp,pca9849", .data = &chips[pca_9849] }, |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 215 | {} |
| 216 | }; |
Javier Martinez Canillas | acf6ef1 | 2017-01-16 10:54:54 -0300 | [diff] [blame] | 217 | MODULE_DEVICE_TABLE(of, pca954x_of_match); |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 218 | #endif |
| 219 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 220 | /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer() |
| 221 | for this as they will try to lock adapter a second time */ |
| 222 | static int pca954x_reg_write(struct i2c_adapter *adap, |
| 223 | struct i2c_client *client, u8 val) |
| 224 | { |
Peter Rosin | 1bcb852 | 2018-06-20 10:51:56 +0200 | [diff] [blame] | 225 | union i2c_smbus_data dummy; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 226 | |
Peter Rosin | 1bcb852 | 2018-06-20 10:51:56 +0200 | [diff] [blame] | 227 | return __i2c_smbus_xfer(adap, client->addr, client->flags, |
| 228 | I2C_SMBUS_WRITE, val, |
| 229 | I2C_SMBUS_BYTE, &dummy); |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 232 | static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan) |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 233 | { |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 234 | struct pca954x *data = i2c_mux_priv(muxc); |
| 235 | struct i2c_client *client = data->client; |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 236 | const struct chip_desc *chip = data->chip; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 237 | u8 regval; |
| 238 | int ret = 0; |
| 239 | |
| 240 | /* we make switches look like muxes, not sure how to be smarter */ |
| 241 | if (chip->muxtype == pca954x_ismux) |
| 242 | regval = chan | chip->enable; |
| 243 | else |
| 244 | regval = 1 << chan; |
| 245 | |
| 246 | /* Only select the channel if its different from the last channel */ |
| 247 | if (data->last_chan != regval) { |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 248 | ret = pca954x_reg_write(muxc->parent, client, regval); |
Russell King | 7f638c1 | 2016-12-17 12:10:56 +0000 | [diff] [blame] | 249 | data->last_chan = ret < 0 ? 0 : regval; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 255 | static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan) |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 256 | { |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 257 | struct pca954x *data = i2c_mux_priv(muxc); |
| 258 | struct i2c_client *client = data->client; |
Robert Shearman | f1fb64b | 2019-02-28 11:43:43 +0000 | [diff] [blame^] | 259 | s8 idle_state; |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 260 | |
Robert Shearman | f1fb64b | 2019-02-28 11:43:43 +0000 | [diff] [blame^] | 261 | idle_state = READ_ONCE(data->idle_state); |
| 262 | if (idle_state >= 0) |
| 263 | /* Set the mux back to a predetermined channel */ |
| 264 | return pca954x_select_chan(muxc, idle_state); |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 265 | |
Robert Shearman | f1fb64b | 2019-02-28 11:43:43 +0000 | [diff] [blame^] | 266 | if (idle_state == MUX_IDLE_DISCONNECT) { |
| 267 | /* Deselect active channel */ |
| 268 | data->last_chan = 0; |
| 269 | return pca954x_reg_write(muxc->parent, client, |
| 270 | data->last_chan); |
| 271 | } |
| 272 | |
| 273 | /* otherwise leave as-is */ |
| 274 | |
| 275 | return 0; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Robert Shearman | f1fb64b | 2019-02-28 11:43:43 +0000 | [diff] [blame^] | 278 | static ssize_t idle_state_show(struct device *dev, |
| 279 | struct device_attribute *attr, |
| 280 | char *buf) |
| 281 | { |
| 282 | struct i2c_client *client = to_i2c_client(dev); |
| 283 | struct i2c_mux_core *muxc = i2c_get_clientdata(client); |
| 284 | struct pca954x *data = i2c_mux_priv(muxc); |
| 285 | |
| 286 | return sprintf(buf, "%d\n", READ_ONCE(data->idle_state)); |
| 287 | } |
| 288 | |
| 289 | static ssize_t idle_state_store(struct device *dev, |
| 290 | struct device_attribute *attr, |
| 291 | const char *buf, size_t count) |
| 292 | { |
| 293 | struct i2c_client *client = to_i2c_client(dev); |
| 294 | struct i2c_mux_core *muxc = i2c_get_clientdata(client); |
| 295 | struct pca954x *data = i2c_mux_priv(muxc); |
| 296 | int val; |
| 297 | int ret; |
| 298 | |
| 299 | ret = kstrtoint(buf, 0, &val); |
| 300 | if (ret < 0) |
| 301 | return ret; |
| 302 | |
| 303 | if (val != MUX_IDLE_AS_IS && val != MUX_IDLE_DISCONNECT && |
| 304 | (val < 0 || val >= data->chip->nchans)) |
| 305 | return -EINVAL; |
| 306 | |
| 307 | i2c_lock_bus(muxc->parent, I2C_LOCK_SEGMENT); |
| 308 | |
| 309 | WRITE_ONCE(data->idle_state, val); |
| 310 | /* |
| 311 | * Set the mux into a state consistent with the new |
| 312 | * idle_state. |
| 313 | */ |
| 314 | if (data->last_chan || val != MUX_IDLE_DISCONNECT) |
| 315 | ret = pca954x_deselect_mux(muxc, 0); |
| 316 | |
| 317 | i2c_unlock_bus(muxc->parent, I2C_LOCK_SEGMENT); |
| 318 | |
| 319 | return ret < 0 ? ret : count; |
| 320 | } |
| 321 | |
| 322 | static DEVICE_ATTR_RW(idle_state); |
| 323 | |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 324 | static irqreturn_t pca954x_irq_handler(int irq, void *dev_id) |
| 325 | { |
| 326 | struct pca954x *data = dev_id; |
| 327 | unsigned int child_irq; |
| 328 | int ret, i, handled = 0; |
| 329 | |
| 330 | ret = i2c_smbus_read_byte(data->client); |
| 331 | if (ret < 0) |
| 332 | return IRQ_NONE; |
| 333 | |
| 334 | for (i = 0; i < data->chip->nchans; i++) { |
| 335 | if (ret & BIT(PCA954X_IRQ_OFFSET + i)) { |
| 336 | child_irq = irq_linear_revmap(data->irq, i); |
| 337 | handle_nested_irq(child_irq); |
| 338 | handled++; |
| 339 | } |
| 340 | } |
| 341 | return handled ? IRQ_HANDLED : IRQ_NONE; |
| 342 | } |
| 343 | |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 344 | static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type) |
| 345 | { |
| 346 | if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW) |
| 347 | return -EINVAL; |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static struct irq_chip pca954x_irq_chip = { |
| 352 | .name = "i2c-mux-pca954x", |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 353 | .irq_set_type = pca954x_irq_set_type, |
| 354 | }; |
| 355 | |
| 356 | static int pca954x_irq_setup(struct i2c_mux_core *muxc) |
| 357 | { |
| 358 | struct pca954x *data = i2c_mux_priv(muxc); |
| 359 | struct i2c_client *client = data->client; |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 360 | int c, irq; |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 361 | |
| 362 | if (!data->chip->has_irq || client->irq <= 0) |
| 363 | return 0; |
| 364 | |
Julia Cartwright | 743cc37 | 2017-03-09 10:21:59 -0600 | [diff] [blame] | 365 | raw_spin_lock_init(&data->lock); |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 366 | |
| 367 | data->irq = irq_domain_add_linear(client->dev.of_node, |
| 368 | data->chip->nchans, |
| 369 | &irq_domain_simple_ops, data); |
| 370 | if (!data->irq) |
| 371 | return -ENODEV; |
| 372 | |
| 373 | for (c = 0; c < data->chip->nchans; c++) { |
| 374 | irq = irq_create_mapping(data->irq, c); |
Phil Reid | e460617 | 2017-08-24 17:31:06 +0800 | [diff] [blame] | 375 | if (!irq) { |
| 376 | dev_err(&client->dev, "failed irq create map\n"); |
| 377 | return -EINVAL; |
| 378 | } |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 379 | irq_set_chip_data(irq, data); |
| 380 | irq_set_chip_and_handler(irq, &pca954x_irq_chip, |
| 381 | handle_simple_irq); |
| 382 | } |
| 383 | |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 384 | return 0; |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 385 | } |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 386 | |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 387 | static void pca954x_cleanup(struct i2c_mux_core *muxc) |
| 388 | { |
| 389 | struct pca954x *data = i2c_mux_priv(muxc); |
Robert Shearman | f1fb64b | 2019-02-28 11:43:43 +0000 | [diff] [blame^] | 390 | struct i2c_client *client = data->client; |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 391 | int c, irq; |
| 392 | |
Robert Shearman | f1fb64b | 2019-02-28 11:43:43 +0000 | [diff] [blame^] | 393 | device_remove_file(&client->dev, &dev_attr_idle_state); |
| 394 | |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 395 | if (data->irq) { |
| 396 | for (c = 0; c < data->chip->nchans; c++) { |
| 397 | irq = irq_find_mapping(data->irq, c); |
| 398 | irq_dispose_mapping(irq); |
| 399 | } |
| 400 | irq_domain_remove(data->irq); |
| 401 | } |
| 402 | i2c_mux_del_adapters(muxc); |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 403 | } |
| 404 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 405 | /* |
| 406 | * I2C init/probing/exit functions |
| 407 | */ |
Guenter Roeck | db79f2a | 2010-10-24 18:16:59 +0200 | [diff] [blame] | 408 | static int pca954x_probe(struct i2c_client *client, |
| 409 | const struct i2c_device_id *id) |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 410 | { |
Luca Ceresoli | f2e0821 | 2018-10-03 17:50:22 +0200 | [diff] [blame] | 411 | struct i2c_adapter *adap = client->adapter; |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame] | 412 | struct device *dev = &client->dev; |
| 413 | struct device_node *np = dev->of_node; |
Alexander Sverdlin | 72f0271 | 2015-01-23 16:41:29 +0100 | [diff] [blame] | 414 | bool idle_disconnect_dt; |
Laurent Pinchart | 4807e84 | 2014-06-03 13:15:26 +0200 | [diff] [blame] | 415 | struct gpio_desc *gpio; |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 416 | struct i2c_mux_core *muxc; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 417 | struct pca954x *data; |
Robert Shearman | ddd7c49 | 2019-02-28 11:43:41 +0000 | [diff] [blame] | 418 | int num; |
Laurent Pinchart | bc12cfc | 2013-11-29 01:51:23 +0100 | [diff] [blame] | 419 | int ret; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 420 | |
| 421 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE)) |
Laurent Pinchart | bc12cfc | 2013-11-29 01:51:23 +0100 | [diff] [blame] | 422 | return -ENODEV; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 423 | |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame] | 424 | muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0, |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 425 | pca954x_select_chan, pca954x_deselect_mux); |
| 426 | if (!muxc) |
Laurent Pinchart | bc12cfc | 2013-11-29 01:51:23 +0100 | [diff] [blame] | 427 | return -ENOMEM; |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 428 | data = i2c_mux_priv(muxc); |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 429 | |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 430 | i2c_set_clientdata(client, muxc); |
| 431 | data->client = client; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 432 | |
Mike Looijmans | a2f8445 | 2018-05-01 13:42:12 +0200 | [diff] [blame] | 433 | /* Reset the mux if a reset GPIO is specified. */ |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame] | 434 | gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); |
Uwe Kleine-König | 58b59e0 | 2015-02-17 10:12:08 +0100 | [diff] [blame] | 435 | if (IS_ERR(gpio)) |
| 436 | return PTR_ERR(gpio); |
Mike Looijmans | a2f8445 | 2018-05-01 13:42:12 +0200 | [diff] [blame] | 437 | if (gpio) { |
| 438 | udelay(1); |
| 439 | gpiod_set_value_cansleep(gpio, 0); |
| 440 | /* Give the chip some time to recover. */ |
| 441 | udelay(1); |
| 442 | } |
Laurent Pinchart | 1209795 | 2013-11-29 01:51:25 +0100 | [diff] [blame] | 443 | |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame] | 444 | data->chip = of_device_get_match_data(dev); |
Julia Lawall | b10d7a1 | 2018-05-21 11:49:08 +0200 | [diff] [blame] | 445 | if (!data->chip) |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 446 | data->chip = &chips[id->driver_data]; |
| 447 | |
| 448 | if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) { |
| 449 | struct i2c_device_identity id; |
| 450 | |
| 451 | ret = i2c_get_device_id(client, &id); |
| 452 | if (ret && ret != -EOPNOTSUPP) |
| 453 | return ret; |
| 454 | |
| 455 | if (!ret && |
| 456 | (id.manufacturer_id != data->chip->id.manufacturer_id || |
| 457 | id.part_id != data->chip->id.part_id)) { |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame] | 458 | dev_warn(dev, "unexpected device id %03x-%03x-%x\n", |
Peter Rosin | 2d74187 | 2018-01-22 08:40:02 +0100 | [diff] [blame] | 459 | id.manufacturer_id, id.part_id, |
| 460 | id.die_revision); |
| 461 | return -ENODEV; |
| 462 | } |
| 463 | } |
| 464 | |
Petri Gynther | cd823db | 2011-06-29 11:36:11 +0200 | [diff] [blame] | 465 | /* Write the mux register at addr to verify |
| 466 | * that the mux is in fact present. This also |
| 467 | * initializes the mux to disconnected state. |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 468 | */ |
Petri Gynther | cd823db | 2011-06-29 11:36:11 +0200 | [diff] [blame] | 469 | if (i2c_smbus_write_byte(client, 0) < 0) { |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame] | 470 | dev_warn(dev, "probe failed\n"); |
Laurent Pinchart | bc12cfc | 2013-11-29 01:51:23 +0100 | [diff] [blame] | 471 | return -ENODEV; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 474 | data->last_chan = 0; /* force the first selection */ |
Robert Shearman | f1fb64b | 2019-02-28 11:43:43 +0000 | [diff] [blame^] | 475 | data->idle_state = MUX_IDLE_AS_IS; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 476 | |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame] | 477 | idle_disconnect_dt = np && |
| 478 | of_property_read_bool(np, "i2c-mux-idle-disconnect"); |
Robert Shearman | f1fb64b | 2019-02-28 11:43:43 +0000 | [diff] [blame^] | 479 | if (idle_disconnect_dt) |
| 480 | data->idle_state = MUX_IDLE_DISCONNECT; |
Alexander Sverdlin | 72f0271 | 2015-01-23 16:41:29 +0100 | [diff] [blame] | 481 | |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 482 | ret = pca954x_irq_setup(muxc); |
| 483 | if (ret) |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 484 | goto fail_cleanup; |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 485 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 486 | /* Now create an adapter for each channel */ |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 487 | for (num = 0; num < data->chip->nchans; num++) { |
Robert Shearman | ddd7c49 | 2019-02-28 11:43:41 +0000 | [diff] [blame] | 488 | ret = i2c_mux_add_adapter(muxc, 0, num, 0); |
Peter Rosin | 0756ac3 | 2017-04-03 10:14:22 +0200 | [diff] [blame] | 489 | if (ret) |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 490 | goto fail_cleanup; |
| 491 | } |
| 492 | |
| 493 | if (data->irq) { |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame] | 494 | ret = devm_request_threaded_irq(dev, data->client->irq, |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 495 | NULL, pca954x_irq_handler, |
| 496 | IRQF_ONESHOT | IRQF_SHARED, |
| 497 | "pca954x", data); |
| 498 | if (ret) |
| 499 | goto fail_cleanup; |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Robert Shearman | f1fb64b | 2019-02-28 11:43:43 +0000 | [diff] [blame^] | 502 | /* |
| 503 | * The attr probably isn't going to be needed in most cases, |
| 504 | * so don't fail completely on error. |
| 505 | */ |
| 506 | device_create_file(dev, &dev_attr_idle_state); |
| 507 | |
Linus Walleij | 6856909 | 2018-06-05 15:42:36 +0200 | [diff] [blame] | 508 | dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n", |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 509 | num, data->chip->muxtype == pca954x_ismux |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 510 | ? "mux" : "switch", client->name); |
| 511 | |
| 512 | return 0; |
| 513 | |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 514 | fail_cleanup: |
| 515 | pca954x_cleanup(muxc); |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 516 | return ret; |
| 517 | } |
| 518 | |
Guenter Roeck | db79f2a | 2010-10-24 18:16:59 +0200 | [diff] [blame] | 519 | static int pca954x_remove(struct i2c_client *client) |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 520 | { |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 521 | struct i2c_mux_core *muxc = i2c_get_clientdata(client); |
Phil Reid | f211479 | 2017-01-25 09:31:08 +0800 | [diff] [blame] | 522 | |
Phil Reid | 148baf1 | 2017-08-24 17:31:05 +0800 | [diff] [blame] | 523 | pca954x_cleanup(muxc); |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
Jisheng Zhang | f5e596c | 2014-07-25 19:57:46 +0800 | [diff] [blame] | 527 | #ifdef CONFIG_PM_SLEEP |
| 528 | static int pca954x_resume(struct device *dev) |
| 529 | { |
| 530 | struct i2c_client *client = to_i2c_client(dev); |
Peter Rosin | 7fcac98 | 2016-04-20 08:40:14 +0200 | [diff] [blame] | 531 | struct i2c_mux_core *muxc = i2c_get_clientdata(client); |
| 532 | struct pca954x *data = i2c_mux_priv(muxc); |
Jisheng Zhang | f5e596c | 2014-07-25 19:57:46 +0800 | [diff] [blame] | 533 | |
| 534 | data->last_chan = 0; |
| 535 | return i2c_smbus_write_byte(client, 0); |
| 536 | } |
| 537 | #endif |
| 538 | |
| 539 | static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume); |
| 540 | |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 541 | static struct i2c_driver pca954x_driver = { |
| 542 | .driver = { |
| 543 | .name = "pca954x", |
Jisheng Zhang | f5e596c | 2014-07-25 19:57:46 +0800 | [diff] [blame] | 544 | .pm = &pca954x_pm, |
Peter Rosin | 8a191a7 | 2016-07-09 21:21:15 +0200 | [diff] [blame] | 545 | .of_match_table = of_match_ptr(pca954x_of_match), |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 546 | }, |
| 547 | .probe = pca954x_probe, |
Guenter Roeck | db79f2a | 2010-10-24 18:16:59 +0200 | [diff] [blame] | 548 | .remove = pca954x_remove, |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 549 | .id_table = pca954x_id, |
| 550 | }; |
| 551 | |
Axel Lin | de05497 | 2012-03-26 21:47:19 +0200 | [diff] [blame] | 552 | module_i2c_driver(pca954x_driver); |
Michael Lawnick | 7f52813 | 2010-08-11 18:21:03 +0200 | [diff] [blame] | 553 | |
| 554 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 555 | MODULE_DESCRIPTION("PCA954x I2C mux/switch driver"); |
| 556 | MODULE_LICENSE("GPL v2"); |