blob: 4ad665757dd815c4a8cdc990efaae29e9d0f81c3 [file] [log] [blame]
Andy Shevchenko40e31f02020-04-25 14:51:52 +03001// SPDX-License-Identifier: GPL-2.0
Michael Lawnick7f528132010-08-11 18:21:03 +02002/*
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 Shevchenko40e31f02020-04-25 14:51:52 +03008 * This module supports the PCA954x and PCA984x series of I2C multiplexer/switch
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +01009 * chips made by NXP Semiconductors.
Michael Lawnick7f528132010-08-11 18:21:03 +020010 * This includes the:
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +010011 * PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547,
12 * PCA9548, PCA9846, PCA9847, PCA9848 and PCA9849.
Michael Lawnick7f528132010-08-11 18:21:03 +020013 *
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 Delvare7c81c60f2014-01-29 20:40:08 +010032 * pca9540.c from Jean Delvare <jdelvare@suse.de>.
Michael Lawnick7f528132010-08-11 18:21:03 +020033 */
34
Michael Lawnick7f528132010-08-11 18:21:03 +020035#include <linux/device.h>
Mike Looijmansa2f84452018-05-01 13:42:12 +020036#include <linux/delay.h>
Laurent Pinchart642653d2014-06-04 18:56:32 +020037#include <linux/gpio/consumer.h>
Michael Lawnick7f528132010-08-11 18:21:03 +020038#include <linux/i2c.h>
39#include <linux/i2c-mux.h>
Phil Reidf2114792017-01-25 09:31:08 +080040#include <linux/interrupt.h>
41#include <linux/irq.h>
Laurent Pinchart4b9b0072013-11-29 01:51:22 +010042#include <linux/module.h>
Jisheng Zhangf5e596c2014-07-25 19:57:46 +080043#include <linux/pm.h>
Andy Shevchenko753aa362020-04-25 14:51:50 +030044#include <linux/property.h>
Laurent Pinchart4b9b0072013-11-29 01:51:22 +010045#include <linux/slab.h>
Phil Reidf2114792017-01-25 09:31:08 +080046#include <linux/spinlock.h>
Robert Shearmanf1fb64b2019-02-28 11:43:43 +000047#include <dt-bindings/mux/mux.h>
Michael Lawnick7f528132010-08-11 18:21:03 +020048
49#define PCA954X_MAX_NCHANS 8
50
Phil Reidf2114792017-01-25 09:31:08 +080051#define PCA954X_IRQ_OFFSET 4
52
Michael Lawnick7f528132010-08-11 18:21:03 +020053enum 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 Fiergolski8f6d6012017-12-25 22:26:46 +010062 pca_9846,
63 pca_9847,
64 pca_9848,
65 pca_9849,
Michael Lawnick7f528132010-08-11 18:21:03 +020066};
67
Michael Lawnick7f528132010-08-11 18:21:03 +020068struct chip_desc {
69 u8 nchans;
70 u8 enable; /* used for muxes only */
Phil Reidf2114792017-01-25 09:31:08 +080071 u8 has_irq;
Michael Lawnick7f528132010-08-11 18:21:03 +020072 enum muxtype {
73 pca954x_ismux = 0,
74 pca954x_isswi
75 } muxtype;
Peter Rosin2d741872018-01-22 08:40:02 +010076 struct i2c_device_identity id;
Michael Lawnick7f528132010-08-11 18:21:03 +020077};
78
Peter Rosin8a191a72016-07-09 21:21:15 +020079struct pca954x {
80 const struct chip_desc *chip;
81
82 u8 last_chan; /* last register value */
Robert Shearmanf1fb64b2019-02-28 11:43:43 +000083 /* MUX_IDLE_AS_IS, MUX_IDLE_DISCONNECT or >= 0 for channel */
Biwen Lie65e2282019-12-25 18:36:23 +080084 s32 idle_state;
Robert Shearmanf1fb64b2019-02-28 11:43:43 +000085
Peter Rosin8a191a72016-07-09 21:21:15 +020086 struct i2c_client *client;
Phil Reidf2114792017-01-25 09:31:08 +080087
88 struct irq_domain *irq;
89 unsigned int irq_mask;
Julia Cartwright743cc372017-03-09 10:21:59 -060090 raw_spinlock_t lock;
Peter Rosin8a191a72016-07-09 21:21:15 +020091};
92
Michael Lawnick7f528132010-08-11 18:21:03 +020093/* Provide specs for the PCA954x types we know about */
94static const struct chip_desc chips[] = {
95 [pca_9540] = {
96 .nchans = 2,
97 .enable = 0x4,
98 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +010099 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200100 },
Phil Reidf8251f12017-01-25 09:31:06 +0800101 [pca_9542] = {
102 .nchans = 2,
103 .enable = 0x4,
Phil Reidf2114792017-01-25 09:31:08 +0800104 .has_irq = 1,
Phil Reidf8251f12017-01-25 09:31:06 +0800105 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +0100106 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Phil Reidf8251f12017-01-25 09:31:06 +0800107 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200108 [pca_9543] = {
109 .nchans = 2,
Phil Reidf2114792017-01-25 09:31:08 +0800110 .has_irq = 1,
Michael Lawnick7f528132010-08-11 18:21:03 +0200111 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100112 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200113 },
114 [pca_9544] = {
115 .nchans = 4,
116 .enable = 0x4,
Phil Reidf2114792017-01-25 09:31:08 +0800117 .has_irq = 1,
Michael Lawnick7f528132010-08-11 18:21:03 +0200118 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +0100119 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200120 },
121 [pca_9545] = {
122 .nchans = 4,
Phil Reidf2114792017-01-25 09:31:08 +0800123 .has_irq = 1,
Michael Lawnick7f528132010-08-11 18:21:03 +0200124 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100125 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200126 },
Mike Looijmansdbe4d692017-03-23 10:00:36 +0100127 [pca_9546] = {
128 .nchans = 4,
129 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100130 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Mike Looijmansdbe4d692017-03-23 10:00:36 +0100131 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200132 [pca_9547] = {
133 .nchans = 8,
134 .enable = 0x8,
135 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +0100136 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200137 },
138 [pca_9548] = {
139 .nchans = 8,
140 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100141 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200142 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100143 [pca_9846] = {
144 .nchans = 4,
145 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100146 .id = {
147 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
148 .part_id = 0x10b,
149 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100150 },
151 [pca_9847] = {
152 .nchans = 8,
153 .enable = 0x8,
154 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +0100155 .id = {
156 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
157 .part_id = 0x108,
158 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100159 },
160 [pca_9848] = {
161 .nchans = 8,
162 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100163 .id = {
164 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
165 .part_id = 0x10a,
166 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100167 },
168 [pca_9849] = {
169 .nchans = 4,
170 .enable = 0x4,
171 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +0100172 .id = {
173 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
174 .part_id = 0x109,
175 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100176 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200177};
178
179static const struct i2c_device_id pca954x_id[] = {
180 { "pca9540", pca_9540 },
Phil Reidf8251f12017-01-25 09:31:06 +0800181 { "pca9542", pca_9542 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200182 { "pca9543", pca_9543 },
183 { "pca9544", pca_9544 },
184 { "pca9545", pca_9545 },
Mike Looijmansdbe4d692017-03-23 10:00:36 +0100185 { "pca9546", pca_9546 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200186 { "pca9547", pca_9547 },
187 { "pca9548", pca_9548 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100188 { "pca9846", pca_9846 },
189 { "pca9847", pca_9847 },
190 { "pca9848", pca_9848 },
191 { "pca9849", pca_9849 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200192 { }
193};
194MODULE_DEVICE_TABLE(i2c, pca954x_id);
195
Peter Rosin8a191a72016-07-09 21:21:15 +0200196static 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 Fiergolski8f6d6012017-12-25 22:26:46 +0100205 { .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 Rosin8a191a72016-07-09 21:21:15 +0200209 {}
210};
Javier Martinez Canillasacf6ef12017-01-16 10:54:54 -0300211MODULE_DEVICE_TABLE(of, pca954x_of_match);
Peter Rosin8a191a72016-07-09 21:21:15 +0200212
Michael Lawnick7f528132010-08-11 18:21:03 +0200213/* 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 */
215static int pca954x_reg_write(struct i2c_adapter *adap,
216 struct i2c_client *client, u8 val)
217{
Peter Rosin1bcb8522018-06-20 10:51:56 +0200218 union i2c_smbus_data dummy;
Michael Lawnick7f528132010-08-11 18:21:03 +0200219
Peter Rosin1bcb8522018-06-20 10:51:56 +0200220 return __i2c_smbus_xfer(adap, client->addr, client->flags,
221 I2C_SMBUS_WRITE, val,
222 I2C_SMBUS_BYTE, &dummy);
Michael Lawnick7f528132010-08-11 18:21:03 +0200223}
224
Biwen Lie65e2282019-12-25 18:36:23 +0800225static 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 Rosin7fcac982016-04-20 08:40:14 +0200234static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
Michael Lawnick7f528132010-08-11 18:21:03 +0200235{
Peter Rosin7fcac982016-04-20 08:40:14 +0200236 struct pca954x *data = i2c_mux_priv(muxc);
237 struct i2c_client *client = data->client;
Michael Lawnick7f528132010-08-11 18:21:03 +0200238 u8 regval;
239 int ret = 0;
240
Biwen Lie65e2282019-12-25 18:36:23 +0800241 regval = pca954x_regval(data, chan);
Michael Lawnick7f528132010-08-11 18:21:03 +0200242 /* Only select the channel if its different from the last channel */
243 if (data->last_chan != regval) {
Peter Rosin7fcac982016-04-20 08:40:14 +0200244 ret = pca954x_reg_write(muxc->parent, client, regval);
Russell King7f638c12016-12-17 12:10:56 +0000245 data->last_chan = ret < 0 ? 0 : regval;
Michael Lawnick7f528132010-08-11 18:21:03 +0200246 }
247
248 return ret;
249}
250
Peter Rosin7fcac982016-04-20 08:40:14 +0200251static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
Michael Lawnick7f528132010-08-11 18:21:03 +0200252{
Peter Rosin7fcac982016-04-20 08:40:14 +0200253 struct pca954x *data = i2c_mux_priv(muxc);
254 struct i2c_client *client = data->client;
Biwen Lie65e2282019-12-25 18:36:23 +0800255 s32 idle_state;
Peter Rosin7fcac982016-04-20 08:40:14 +0200256
Robert Shearmanf1fb64b2019-02-28 11:43:43 +0000257 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 Lawnick7f528132010-08-11 18:21:03 +0200261
Robert Shearmanf1fb64b2019-02-28 11:43:43 +0000262 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 Lawnick7f528132010-08-11 18:21:03 +0200272}
273
Robert Shearmanf1fb64b2019-02-28 11:43:43 +0000274static 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
285static 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
318static DEVICE_ATTR_RW(idle_state);
319
Phil Reidf2114792017-01-25 09:31:08 +0800320static irqreturn_t pca954x_irq_handler(int irq, void *dev_id)
321{
322 struct pca954x *data = dev_id;
Andy Shevchenko19bb2222020-04-25 14:51:49 +0300323 unsigned long pending;
324 int ret, i;
Phil Reidf2114792017-01-25 09:31:08 +0800325
326 ret = i2c_smbus_read_byte(data->client);
327 if (ret < 0)
328 return IRQ_NONE;
329
Andy Shevchenko19bb2222020-04-25 14:51:49 +0300330 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 Reidf2114792017-01-25 09:31:08 +0800335}
336
Phil Reidf2114792017-01-25 09:31:08 +0800337static 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
344static struct irq_chip pca954x_irq_chip = {
345 .name = "i2c-mux-pca954x",
Phil Reidf2114792017-01-25 09:31:08 +0800346 .irq_set_type = pca954x_irq_set_type,
347};
348
349static 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 Reid148baf12017-08-24 17:31:05 +0800353 int c, irq;
Phil Reidf2114792017-01-25 09:31:08 +0800354
355 if (!data->chip->has_irq || client->irq <= 0)
356 return 0;
357
Julia Cartwright743cc372017-03-09 10:21:59 -0600358 raw_spin_lock_init(&data->lock);
Phil Reidf2114792017-01-25 09:31:08 +0800359
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 Reide4606172017-08-24 17:31:06 +0800368 if (!irq) {
369 dev_err(&client->dev, "failed irq create map\n");
370 return -EINVAL;
371 }
Phil Reidf2114792017-01-25 09:31:08 +0800372 irq_set_chip_data(irq, data);
373 irq_set_chip_and_handler(irq, &pca954x_irq_chip,
374 handle_simple_irq);
375 }
376
Phil Reidf2114792017-01-25 09:31:08 +0800377 return 0;
Phil Reid148baf12017-08-24 17:31:05 +0800378}
Phil Reidf2114792017-01-25 09:31:08 +0800379
Phil Reid148baf12017-08-24 17:31:05 +0800380static 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 Reidf2114792017-01-25 09:31:08 +0800393}
394
Biwen Lie65e2282019-12-25 18:36:23 +0800395static 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 Lawnick7f528132010-08-11 18:21:03 +0200411/*
412 * I2C init/probing/exit functions
413 */
Guenter Roeckdb79f2a2010-10-24 18:16:59 +0200414static int pca954x_probe(struct i2c_client *client,
415 const struct i2c_device_id *id)
Michael Lawnick7f528132010-08-11 18:21:03 +0200416{
Luca Ceresolif2e08212018-10-03 17:50:22 +0200417 struct i2c_adapter *adap = client->adapter;
Linus Walleij68569092018-06-05 15:42:36 +0200418 struct device *dev = &client->dev;
Laurent Pinchart4807e842014-06-03 13:15:26 +0200419 struct gpio_desc *gpio;
Peter Rosin7fcac982016-04-20 08:40:14 +0200420 struct i2c_mux_core *muxc;
Michael Lawnick7f528132010-08-11 18:21:03 +0200421 struct pca954x *data;
Robert Shearmanddd7c492019-02-28 11:43:41 +0000422 int num;
Laurent Pinchartbc12cfc2013-11-29 01:51:23 +0100423 int ret;
Michael Lawnick7f528132010-08-11 18:21:03 +0200424
425 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
Laurent Pinchartbc12cfc2013-11-29 01:51:23 +0100426 return -ENODEV;
Michael Lawnick7f528132010-08-11 18:21:03 +0200427
Linus Walleij68569092018-06-05 15:42:36 +0200428 muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0,
Peter Rosin7fcac982016-04-20 08:40:14 +0200429 pca954x_select_chan, pca954x_deselect_mux);
430 if (!muxc)
Laurent Pinchartbc12cfc2013-11-29 01:51:23 +0100431 return -ENOMEM;
Peter Rosin7fcac982016-04-20 08:40:14 +0200432 data = i2c_mux_priv(muxc);
Michael Lawnick7f528132010-08-11 18:21:03 +0200433
Peter Rosin7fcac982016-04-20 08:40:14 +0200434 i2c_set_clientdata(client, muxc);
435 data->client = client;
Michael Lawnick7f528132010-08-11 18:21:03 +0200436
Mike Looijmansa2f84452018-05-01 13:42:12 +0200437 /* Reset the mux if a reset GPIO is specified. */
Linus Walleij68569092018-06-05 15:42:36 +0200438 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
Uwe Kleine-König58b59e02015-02-17 10:12:08 +0100439 if (IS_ERR(gpio))
440 return PTR_ERR(gpio);
Mike Looijmansa2f84452018-05-01 13:42:12 +0200441 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 Pinchart12097952013-11-29 01:51:25 +0100447
Andy Shevchenko753aa362020-04-25 14:51:50 +0300448 data->chip = device_get_match_data(dev);
Julia Lawallb10d7a12018-05-21 11:49:08 +0200449 if (!data->chip)
Peter Rosin2d741872018-01-22 08:40:02 +0100450 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 Walleij68569092018-06-05 15:42:36 +0200462 dev_warn(dev, "unexpected device id %03x-%03x-%x\n",
Peter Rosin2d741872018-01-22 08:40:02 +0100463 id.manufacturer_id, id.part_id,
464 id.die_revision);
465 return -ENODEV;
466 }
467 }
468
Biwen Lie65e2282019-12-25 18:36:23 +0800469 data->idle_state = MUX_IDLE_AS_IS;
Andy Shevchenko753aa362020-04-25 14:51:50 +0300470 if (device_property_read_u32(dev, "idle-state", &data->idle_state)) {
471 if (device_property_read_bool(dev, "i2c-mux-idle-disconnect"))
Biwen Lie65e2282019-12-25 18:36:23 +0800472 data->idle_state = MUX_IDLE_DISCONNECT;
473 }
474
475 /*
476 * Write the mux register at addr to verify
Petri Gynthercd823db2011-06-29 11:36:11 +0200477 * that the mux is in fact present. This also
Biwen Lie65e2282019-12-25 18:36:23 +0800478 * initializes the mux to a channel
479 * or disconnected state.
Michael Lawnick7f528132010-08-11 18:21:03 +0200480 */
Biwen Lie65e2282019-12-25 18:36:23 +0800481 ret = pca954x_init(client, data);
482 if (ret < 0) {
Linus Walleij68569092018-06-05 15:42:36 +0200483 dev_warn(dev, "probe failed\n");
Laurent Pinchartbc12cfc2013-11-29 01:51:23 +0100484 return -ENODEV;
Michael Lawnick7f528132010-08-11 18:21:03 +0200485 }
486
Phil Reidf2114792017-01-25 09:31:08 +0800487 ret = pca954x_irq_setup(muxc);
488 if (ret)
Phil Reid148baf12017-08-24 17:31:05 +0800489 goto fail_cleanup;
Phil Reidf2114792017-01-25 09:31:08 +0800490
Michael Lawnick7f528132010-08-11 18:21:03 +0200491 /* Now create an adapter for each channel */
Peter Rosin8a191a72016-07-09 21:21:15 +0200492 for (num = 0; num < data->chip->nchans; num++) {
Robert Shearmanddd7c492019-02-28 11:43:41 +0000493 ret = i2c_mux_add_adapter(muxc, 0, num, 0);
Peter Rosin0756ac32017-04-03 10:14:22 +0200494 if (ret)
Phil Reid148baf12017-08-24 17:31:05 +0800495 goto fail_cleanup;
496 }
497
498 if (data->irq) {
Linus Walleij68569092018-06-05 15:42:36 +0200499 ret = devm_request_threaded_irq(dev, data->client->irq,
Phil Reid148baf12017-08-24 17:31:05 +0800500 NULL, pca954x_irq_handler,
501 IRQF_ONESHOT | IRQF_SHARED,
502 "pca954x", data);
503 if (ret)
504 goto fail_cleanup;
Michael Lawnick7f528132010-08-11 18:21:03 +0200505 }
506
Robert Shearmanf1fb64b2019-02-28 11:43:43 +0000507 /*
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 Walleij68569092018-06-05 15:42:36 +0200513 dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n",
Peter Rosin8a191a72016-07-09 21:21:15 +0200514 num, data->chip->muxtype == pca954x_ismux
Michael Lawnick7f528132010-08-11 18:21:03 +0200515 ? "mux" : "switch", client->name);
516
517 return 0;
518
Phil Reid148baf12017-08-24 17:31:05 +0800519fail_cleanup:
520 pca954x_cleanup(muxc);
Michael Lawnick7f528132010-08-11 18:21:03 +0200521 return ret;
522}
523
Guenter Roeckdb79f2a2010-10-24 18:16:59 +0200524static int pca954x_remove(struct i2c_client *client)
Michael Lawnick7f528132010-08-11 18:21:03 +0200525{
Peter Rosin7fcac982016-04-20 08:40:14 +0200526 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
Phil Reidf2114792017-01-25 09:31:08 +0800527
Andy Shevchenko3093c642020-04-25 14:51:51 +0300528 device_remove_file(&client->dev, &dev_attr_idle_state);
529
Phil Reid148baf12017-08-24 17:31:05 +0800530 pca954x_cleanup(muxc);
Michael Lawnick7f528132010-08-11 18:21:03 +0200531 return 0;
532}
533
Jisheng Zhangf5e596c2014-07-25 19:57:46 +0800534#ifdef CONFIG_PM_SLEEP
535static int pca954x_resume(struct device *dev)
536{
537 struct i2c_client *client = to_i2c_client(dev);
Peter Rosin7fcac982016-04-20 08:40:14 +0200538 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
539 struct pca954x *data = i2c_mux_priv(muxc);
Biwen Lie65e2282019-12-25 18:36:23 +0800540 int ret;
Jisheng Zhangf5e596c2014-07-25 19:57:46 +0800541
Biwen Lie65e2282019-12-25 18:36:23 +0800542 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 Zhangf5e596c2014-07-25 19:57:46 +0800547}
548#endif
549
550static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
551
Michael Lawnick7f528132010-08-11 18:21:03 +0200552static struct i2c_driver pca954x_driver = {
553 .driver = {
554 .name = "pca954x",
Jisheng Zhangf5e596c2014-07-25 19:57:46 +0800555 .pm = &pca954x_pm,
Andy Shevchenko753aa362020-04-25 14:51:50 +0300556 .of_match_table = pca954x_of_match,
Michael Lawnick7f528132010-08-11 18:21:03 +0200557 },
558 .probe = pca954x_probe,
Guenter Roeckdb79f2a2010-10-24 18:16:59 +0200559 .remove = pca954x_remove,
Michael Lawnick7f528132010-08-11 18:21:03 +0200560 .id_table = pca954x_id,
561};
562
Axel Linde054972012-03-26 21:47:19 +0200563module_i2c_driver(pca954x_driver);
Michael Lawnick7f528132010-08-11 18:21:03 +0200564
565MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
566MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
567MODULE_LICENSE("GPL v2");