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