blob: bfabf985e8308c809156bce0e11cc84f0ca8f901 [file] [log] [blame]
Michael Lawnick7f528132010-08-11 18:21:03 +02001/*
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 Fiergolski8f6d6012017-12-25 22:26:46 +01007 * This module supports the PCA954x and PCA954x series of I2C multiplexer/switch
8 * chips made by NXP Semiconductors.
Michael Lawnick7f528132010-08-11 18:21:03 +02009 * This includes the:
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +010010 * PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547,
11 * PCA9548, PCA9846, PCA9847, PCA9848 and PCA9849.
Michael Lawnick7f528132010-08-11 18:21:03 +020012 *
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 Delvare7c81c60f2014-01-29 20:40:08 +010031 * pca9540.c from Jean Delvare <jdelvare@suse.de>.
Michael Lawnick7f528132010-08-11 18:21:03 +020032 *
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 Lawnick7f528132010-08-11 18:21:03 +020038#include <linux/device.h>
Mike Looijmansa2f84452018-05-01 13:42:12 +020039#include <linux/delay.h>
Laurent Pinchart642653d2014-06-04 18:56:32 +020040#include <linux/gpio/consumer.h>
Michael Lawnick7f528132010-08-11 18:21:03 +020041#include <linux/i2c.h>
42#include <linux/i2c-mux.h>
Phil Reidf2114792017-01-25 09:31:08 +080043#include <linux/interrupt.h>
44#include <linux/irq.h>
Laurent Pinchart4b9b0072013-11-29 01:51:22 +010045#include <linux/module.h>
Alexander Sverdlin72f02712015-01-23 16:41:29 +010046#include <linux/of.h>
Peter Rosin8a191a72016-07-09 21:21:15 +020047#include <linux/of_device.h>
Phil Reidf2114792017-01-25 09:31:08 +080048#include <linux/of_irq.h>
Wolfram Sang2be03ae2017-08-14 10:23:25 +020049#include <linux/platform_data/pca954x.h>
Jisheng Zhangf5e596c2014-07-25 19:57:46 +080050#include <linux/pm.h>
Laurent Pinchart4b9b0072013-11-29 01:51:22 +010051#include <linux/slab.h>
Phil Reidf2114792017-01-25 09:31:08 +080052#include <linux/spinlock.h>
Michael Lawnick7f528132010-08-11 18:21:03 +020053
54#define PCA954X_MAX_NCHANS 8
55
Phil Reidf2114792017-01-25 09:31:08 +080056#define PCA954X_IRQ_OFFSET 4
57
Michael Lawnick7f528132010-08-11 18:21:03 +020058enum 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 Fiergolski8f6d6012017-12-25 22:26:46 +010067 pca_9846,
68 pca_9847,
69 pca_9848,
70 pca_9849,
Michael Lawnick7f528132010-08-11 18:21:03 +020071};
72
Michael Lawnick7f528132010-08-11 18:21:03 +020073struct chip_desc {
74 u8 nchans;
75 u8 enable; /* used for muxes only */
Phil Reidf2114792017-01-25 09:31:08 +080076 u8 has_irq;
Michael Lawnick7f528132010-08-11 18:21:03 +020077 enum muxtype {
78 pca954x_ismux = 0,
79 pca954x_isswi
80 } muxtype;
Peter Rosin2d741872018-01-22 08:40:02 +010081 struct i2c_device_identity id;
Michael Lawnick7f528132010-08-11 18:21:03 +020082};
83
Peter Rosin8a191a72016-07-09 21:21:15 +020084struct pca954x {
85 const struct chip_desc *chip;
86
87 u8 last_chan; /* last register value */
88 u8 deselect;
89 struct i2c_client *client;
Phil Reidf2114792017-01-25 09:31:08 +080090
91 struct irq_domain *irq;
92 unsigned int irq_mask;
Julia Cartwright743cc372017-03-09 10:21:59 -060093 raw_spinlock_t lock;
Peter Rosin8a191a72016-07-09 21:21:15 +020094};
95
Michael Lawnick7f528132010-08-11 18:21:03 +020096/* Provide specs for the PCA954x types we know about */
97static const struct chip_desc chips[] = {
98 [pca_9540] = {
99 .nchans = 2,
100 .enable = 0x4,
101 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +0100102 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200103 },
Phil Reidf8251f12017-01-25 09:31:06 +0800104 [pca_9542] = {
105 .nchans = 2,
106 .enable = 0x4,
Phil Reidf2114792017-01-25 09:31:08 +0800107 .has_irq = 1,
Phil Reidf8251f12017-01-25 09:31:06 +0800108 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +0100109 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Phil Reidf8251f12017-01-25 09:31:06 +0800110 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200111 [pca_9543] = {
112 .nchans = 2,
Phil Reidf2114792017-01-25 09:31:08 +0800113 .has_irq = 1,
Michael Lawnick7f528132010-08-11 18:21:03 +0200114 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100115 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200116 },
117 [pca_9544] = {
118 .nchans = 4,
119 .enable = 0x4,
Phil Reidf2114792017-01-25 09:31:08 +0800120 .has_irq = 1,
Michael Lawnick7f528132010-08-11 18:21:03 +0200121 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +0100122 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200123 },
124 [pca_9545] = {
125 .nchans = 4,
Phil Reidf2114792017-01-25 09:31:08 +0800126 .has_irq = 1,
Michael Lawnick7f528132010-08-11 18:21:03 +0200127 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100128 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200129 },
Mike Looijmansdbe4d692017-03-23 10:00:36 +0100130 [pca_9546] = {
131 .nchans = 4,
132 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100133 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Mike Looijmansdbe4d692017-03-23 10:00:36 +0100134 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200135 [pca_9547] = {
136 .nchans = 8,
137 .enable = 0x8,
138 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +0100139 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200140 },
141 [pca_9548] = {
142 .nchans = 8,
143 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100144 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
Michael Lawnick7f528132010-08-11 18:21:03 +0200145 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100146 [pca_9846] = {
147 .nchans = 4,
148 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100149 .id = {
150 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
151 .part_id = 0x10b,
152 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100153 },
154 [pca_9847] = {
155 .nchans = 8,
156 .enable = 0x8,
157 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +0100158 .id = {
159 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
160 .part_id = 0x108,
161 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100162 },
163 [pca_9848] = {
164 .nchans = 8,
165 .muxtype = pca954x_isswi,
Peter Rosin2d741872018-01-22 08:40:02 +0100166 .id = {
167 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
168 .part_id = 0x10a,
169 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100170 },
171 [pca_9849] = {
172 .nchans = 4,
173 .enable = 0x4,
174 .muxtype = pca954x_ismux,
Peter Rosin2d741872018-01-22 08:40:02 +0100175 .id = {
176 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
177 .part_id = 0x109,
178 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100179 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200180};
181
182static const struct i2c_device_id pca954x_id[] = {
183 { "pca9540", pca_9540 },
Phil Reidf8251f12017-01-25 09:31:06 +0800184 { "pca9542", pca_9542 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200185 { "pca9543", pca_9543 },
186 { "pca9544", pca_9544 },
187 { "pca9545", pca_9545 },
Mike Looijmansdbe4d692017-03-23 10:00:36 +0100188 { "pca9546", pca_9546 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200189 { "pca9547", pca_9547 },
190 { "pca9548", pca_9548 },
Adrian Fiergolski8f6d6012017-12-25 22:26:46 +0100191 { "pca9846", pca_9846 },
192 { "pca9847", pca_9847 },
193 { "pca9848", pca_9848 },
194 { "pca9849", pca_9849 },
Michael Lawnick7f528132010-08-11 18:21:03 +0200195 { }
196};
197MODULE_DEVICE_TABLE(i2c, pca954x_id);
198
Peter Rosin8a191a72016-07-09 21:21:15 +0200199#ifdef CONFIG_OF
200static 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 Fiergolski8f6d6012017-12-25 22:26:46 +0100209 { .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 Rosin8a191a72016-07-09 21:21:15 +0200213 {}
214};
Javier Martinez Canillasacf6ef12017-01-16 10:54:54 -0300215MODULE_DEVICE_TABLE(of, pca954x_of_match);
Peter Rosin8a191a72016-07-09 21:21:15 +0200216#endif
217
Michael Lawnick7f528132010-08-11 18:21:03 +0200218/* 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 */
220static int pca954x_reg_write(struct i2c_adapter *adap,
221 struct i2c_client *client, u8 val)
222{
Peter Rosin1bcb8522018-06-20 10:51:56 +0200223 union i2c_smbus_data dummy;
Michael Lawnick7f528132010-08-11 18:21:03 +0200224
Peter Rosin1bcb8522018-06-20 10:51:56 +0200225 return __i2c_smbus_xfer(adap, client->addr, client->flags,
226 I2C_SMBUS_WRITE, val,
227 I2C_SMBUS_BYTE, &dummy);
Michael Lawnick7f528132010-08-11 18:21:03 +0200228}
229
Peter Rosin7fcac982016-04-20 08:40:14 +0200230static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
Michael Lawnick7f528132010-08-11 18:21:03 +0200231{
Peter Rosin7fcac982016-04-20 08:40:14 +0200232 struct pca954x *data = i2c_mux_priv(muxc);
233 struct i2c_client *client = data->client;
Peter Rosin8a191a72016-07-09 21:21:15 +0200234 const struct chip_desc *chip = data->chip;
Michael Lawnick7f528132010-08-11 18:21:03 +0200235 u8 regval;
236 int ret = 0;
237
238 /* we make switches look like muxes, not sure how to be smarter */
239 if (chip->muxtype == pca954x_ismux)
240 regval = chan | chip->enable;
241 else
242 regval = 1 << chan;
243
244 /* Only select the channel if its different from the last channel */
245 if (data->last_chan != regval) {
Peter Rosin7fcac982016-04-20 08:40:14 +0200246 ret = pca954x_reg_write(muxc->parent, client, regval);
Russell King7f638c12016-12-17 12:10:56 +0000247 data->last_chan = ret < 0 ? 0 : regval;
Michael Lawnick7f528132010-08-11 18:21:03 +0200248 }
249
250 return ret;
251}
252
Peter Rosin7fcac982016-04-20 08:40:14 +0200253static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
Michael Lawnick7f528132010-08-11 18:21:03 +0200254{
Peter Rosin7fcac982016-04-20 08:40:14 +0200255 struct pca954x *data = i2c_mux_priv(muxc);
256 struct i2c_client *client = data->client;
257
258 if (!(data->deselect & (1 << chan)))
259 return 0;
Michael Lawnick7f528132010-08-11 18:21:03 +0200260
261 /* Deselect active channel */
262 data->last_chan = 0;
Peter Rosin7fcac982016-04-20 08:40:14 +0200263 return pca954x_reg_write(muxc->parent, client, data->last_chan);
Michael Lawnick7f528132010-08-11 18:21:03 +0200264}
265
Phil Reidf2114792017-01-25 09:31:08 +0800266static irqreturn_t pca954x_irq_handler(int irq, void *dev_id)
267{
268 struct pca954x *data = dev_id;
269 unsigned int child_irq;
270 int ret, i, handled = 0;
271
272 ret = i2c_smbus_read_byte(data->client);
273 if (ret < 0)
274 return IRQ_NONE;
275
276 for (i = 0; i < data->chip->nchans; i++) {
277 if (ret & BIT(PCA954X_IRQ_OFFSET + i)) {
278 child_irq = irq_linear_revmap(data->irq, i);
279 handle_nested_irq(child_irq);
280 handled++;
281 }
282 }
283 return handled ? IRQ_HANDLED : IRQ_NONE;
284}
285
Phil Reidf2114792017-01-25 09:31:08 +0800286static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type)
287{
288 if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW)
289 return -EINVAL;
290 return 0;
291}
292
293static struct irq_chip pca954x_irq_chip = {
294 .name = "i2c-mux-pca954x",
Phil Reidf2114792017-01-25 09:31:08 +0800295 .irq_set_type = pca954x_irq_set_type,
296};
297
298static int pca954x_irq_setup(struct i2c_mux_core *muxc)
299{
300 struct pca954x *data = i2c_mux_priv(muxc);
301 struct i2c_client *client = data->client;
Phil Reid148baf12017-08-24 17:31:05 +0800302 int c, irq;
Phil Reidf2114792017-01-25 09:31:08 +0800303
304 if (!data->chip->has_irq || client->irq <= 0)
305 return 0;
306
Julia Cartwright743cc372017-03-09 10:21:59 -0600307 raw_spin_lock_init(&data->lock);
Phil Reidf2114792017-01-25 09:31:08 +0800308
309 data->irq = irq_domain_add_linear(client->dev.of_node,
310 data->chip->nchans,
311 &irq_domain_simple_ops, data);
312 if (!data->irq)
313 return -ENODEV;
314
315 for (c = 0; c < data->chip->nchans; c++) {
316 irq = irq_create_mapping(data->irq, c);
Phil Reide4606172017-08-24 17:31:06 +0800317 if (!irq) {
318 dev_err(&client->dev, "failed irq create map\n");
319 return -EINVAL;
320 }
Phil Reidf2114792017-01-25 09:31:08 +0800321 irq_set_chip_data(irq, data);
322 irq_set_chip_and_handler(irq, &pca954x_irq_chip,
323 handle_simple_irq);
324 }
325
Phil Reidf2114792017-01-25 09:31:08 +0800326 return 0;
Phil Reid148baf12017-08-24 17:31:05 +0800327}
Phil Reidf2114792017-01-25 09:31:08 +0800328
Phil Reid148baf12017-08-24 17:31:05 +0800329static void pca954x_cleanup(struct i2c_mux_core *muxc)
330{
331 struct pca954x *data = i2c_mux_priv(muxc);
332 int c, irq;
333
334 if (data->irq) {
335 for (c = 0; c < data->chip->nchans; c++) {
336 irq = irq_find_mapping(data->irq, c);
337 irq_dispose_mapping(irq);
338 }
339 irq_domain_remove(data->irq);
340 }
341 i2c_mux_del_adapters(muxc);
Phil Reidf2114792017-01-25 09:31:08 +0800342}
343
Michael Lawnick7f528132010-08-11 18:21:03 +0200344/*
345 * I2C init/probing/exit functions
346 */
Guenter Roeckdb79f2a2010-10-24 18:16:59 +0200347static int pca954x_probe(struct i2c_client *client,
348 const struct i2c_device_id *id)
Michael Lawnick7f528132010-08-11 18:21:03 +0200349{
Luca Ceresolif2e08212018-10-03 17:50:22 +0200350 struct i2c_adapter *adap = client->adapter;
Jingoo Han6d4028c2013-07-30 16:59:33 +0900351 struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
Linus Walleij68569092018-06-05 15:42:36 +0200352 struct device *dev = &client->dev;
353 struct device_node *np = dev->of_node;
Alexander Sverdlin72f02712015-01-23 16:41:29 +0100354 bool idle_disconnect_dt;
Laurent Pinchart4807e842014-06-03 13:15:26 +0200355 struct gpio_desc *gpio;
Jean Delvareeee543e2012-10-05 22:23:51 +0200356 int num, force, class;
Peter Rosin7fcac982016-04-20 08:40:14 +0200357 struct i2c_mux_core *muxc;
Michael Lawnick7f528132010-08-11 18:21:03 +0200358 struct pca954x *data;
Laurent Pinchartbc12cfc2013-11-29 01:51:23 +0100359 int ret;
Michael Lawnick7f528132010-08-11 18:21:03 +0200360
361 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
Laurent Pinchartbc12cfc2013-11-29 01:51:23 +0100362 return -ENODEV;
Michael Lawnick7f528132010-08-11 18:21:03 +0200363
Linus Walleij68569092018-06-05 15:42:36 +0200364 muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0,
Peter Rosin7fcac982016-04-20 08:40:14 +0200365 pca954x_select_chan, pca954x_deselect_mux);
366 if (!muxc)
Laurent Pinchartbc12cfc2013-11-29 01:51:23 +0100367 return -ENOMEM;
Peter Rosin7fcac982016-04-20 08:40:14 +0200368 data = i2c_mux_priv(muxc);
Michael Lawnick7f528132010-08-11 18:21:03 +0200369
Peter Rosin7fcac982016-04-20 08:40:14 +0200370 i2c_set_clientdata(client, muxc);
371 data->client = client;
Michael Lawnick7f528132010-08-11 18:21:03 +0200372
Mike Looijmansa2f84452018-05-01 13:42:12 +0200373 /* Reset the mux if a reset GPIO is specified. */
Linus Walleij68569092018-06-05 15:42:36 +0200374 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
Uwe Kleine-König58b59e02015-02-17 10:12:08 +0100375 if (IS_ERR(gpio))
376 return PTR_ERR(gpio);
Mike Looijmansa2f84452018-05-01 13:42:12 +0200377 if (gpio) {
378 udelay(1);
379 gpiod_set_value_cansleep(gpio, 0);
380 /* Give the chip some time to recover. */
381 udelay(1);
382 }
Laurent Pinchart12097952013-11-29 01:51:25 +0100383
Linus Walleij68569092018-06-05 15:42:36 +0200384 data->chip = of_device_get_match_data(dev);
Julia Lawallb10d7a12018-05-21 11:49:08 +0200385 if (!data->chip)
Peter Rosin2d741872018-01-22 08:40:02 +0100386 data->chip = &chips[id->driver_data];
387
388 if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) {
389 struct i2c_device_identity id;
390
391 ret = i2c_get_device_id(client, &id);
392 if (ret && ret != -EOPNOTSUPP)
393 return ret;
394
395 if (!ret &&
396 (id.manufacturer_id != data->chip->id.manufacturer_id ||
397 id.part_id != data->chip->id.part_id)) {
Linus Walleij68569092018-06-05 15:42:36 +0200398 dev_warn(dev, "unexpected device id %03x-%03x-%x\n",
Peter Rosin2d741872018-01-22 08:40:02 +0100399 id.manufacturer_id, id.part_id,
400 id.die_revision);
401 return -ENODEV;
402 }
403 }
404
Petri Gynthercd823db2011-06-29 11:36:11 +0200405 /* Write the mux register at addr to verify
406 * that the mux is in fact present. This also
407 * initializes the mux to disconnected state.
Michael Lawnick7f528132010-08-11 18:21:03 +0200408 */
Petri Gynthercd823db2011-06-29 11:36:11 +0200409 if (i2c_smbus_write_byte(client, 0) < 0) {
Linus Walleij68569092018-06-05 15:42:36 +0200410 dev_warn(dev, "probe failed\n");
Laurent Pinchartbc12cfc2013-11-29 01:51:23 +0100411 return -ENODEV;
Michael Lawnick7f528132010-08-11 18:21:03 +0200412 }
413
Michael Lawnick7f528132010-08-11 18:21:03 +0200414 data->last_chan = 0; /* force the first selection */
415
Linus Walleij68569092018-06-05 15:42:36 +0200416 idle_disconnect_dt = np &&
417 of_property_read_bool(np, "i2c-mux-idle-disconnect");
Alexander Sverdlin72f02712015-01-23 16:41:29 +0100418
Phil Reidf2114792017-01-25 09:31:08 +0800419 ret = pca954x_irq_setup(muxc);
420 if (ret)
Phil Reid148baf12017-08-24 17:31:05 +0800421 goto fail_cleanup;
Phil Reidf2114792017-01-25 09:31:08 +0800422
Michael Lawnick7f528132010-08-11 18:21:03 +0200423 /* Now create an adapter for each channel */
Peter Rosin8a191a72016-07-09 21:21:15 +0200424 for (num = 0; num < data->chip->nchans; num++) {
Alexander Sverdlin72f02712015-01-23 16:41:29 +0100425 bool idle_disconnect_pd = false;
426
Michael Lawnick7f528132010-08-11 18:21:03 +0200427 force = 0; /* dynamic adap number */
Jean Delvareeee543e2012-10-05 22:23:51 +0200428 class = 0; /* no class by default */
Michael Lawnick7f528132010-08-11 18:21:03 +0200429 if (pdata) {
Jean Delvareeee543e2012-10-05 22:23:51 +0200430 if (num < pdata->num_modes) {
Michael Lawnick7f528132010-08-11 18:21:03 +0200431 /* force static number */
432 force = pdata->modes[num].adap_id;
Jean Delvareeee543e2012-10-05 22:23:51 +0200433 class = pdata->modes[num].class;
434 } else
Michael Lawnick7f528132010-08-11 18:21:03 +0200435 /* discard unconfigured channels */
436 break;
Alexander Sverdlin72f02712015-01-23 16:41:29 +0100437 idle_disconnect_pd = pdata->modes[num].deselect_on_exit;
Michael Lawnick7f528132010-08-11 18:21:03 +0200438 }
Alex Hemmead092de2016-11-19 10:48:38 +0100439 data->deselect |= (idle_disconnect_pd ||
440 idle_disconnect_dt) << num;
Michael Lawnick7f528132010-08-11 18:21:03 +0200441
Peter Rosin7fcac982016-04-20 08:40:14 +0200442 ret = i2c_mux_add_adapter(muxc, force, num, class);
Peter Rosin0756ac32017-04-03 10:14:22 +0200443 if (ret)
Phil Reid148baf12017-08-24 17:31:05 +0800444 goto fail_cleanup;
445 }
446
447 if (data->irq) {
Linus Walleij68569092018-06-05 15:42:36 +0200448 ret = devm_request_threaded_irq(dev, data->client->irq,
Phil Reid148baf12017-08-24 17:31:05 +0800449 NULL, pca954x_irq_handler,
450 IRQF_ONESHOT | IRQF_SHARED,
451 "pca954x", data);
452 if (ret)
453 goto fail_cleanup;
Michael Lawnick7f528132010-08-11 18:21:03 +0200454 }
455
Linus Walleij68569092018-06-05 15:42:36 +0200456 dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n",
Peter Rosin8a191a72016-07-09 21:21:15 +0200457 num, data->chip->muxtype == pca954x_ismux
Michael Lawnick7f528132010-08-11 18:21:03 +0200458 ? "mux" : "switch", client->name);
459
460 return 0;
461
Phil Reid148baf12017-08-24 17:31:05 +0800462fail_cleanup:
463 pca954x_cleanup(muxc);
Michael Lawnick7f528132010-08-11 18:21:03 +0200464 return ret;
465}
466
Guenter Roeckdb79f2a2010-10-24 18:16:59 +0200467static int pca954x_remove(struct i2c_client *client)
Michael Lawnick7f528132010-08-11 18:21:03 +0200468{
Peter Rosin7fcac982016-04-20 08:40:14 +0200469 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
Phil Reidf2114792017-01-25 09:31:08 +0800470
Phil Reid148baf12017-08-24 17:31:05 +0800471 pca954x_cleanup(muxc);
Michael Lawnick7f528132010-08-11 18:21:03 +0200472 return 0;
473}
474
Jisheng Zhangf5e596c2014-07-25 19:57:46 +0800475#ifdef CONFIG_PM_SLEEP
476static int pca954x_resume(struct device *dev)
477{
478 struct i2c_client *client = to_i2c_client(dev);
Peter Rosin7fcac982016-04-20 08:40:14 +0200479 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
480 struct pca954x *data = i2c_mux_priv(muxc);
Jisheng Zhangf5e596c2014-07-25 19:57:46 +0800481
482 data->last_chan = 0;
483 return i2c_smbus_write_byte(client, 0);
484}
485#endif
486
487static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
488
Michael Lawnick7f528132010-08-11 18:21:03 +0200489static struct i2c_driver pca954x_driver = {
490 .driver = {
491 .name = "pca954x",
Jisheng Zhangf5e596c2014-07-25 19:57:46 +0800492 .pm = &pca954x_pm,
Peter Rosin8a191a72016-07-09 21:21:15 +0200493 .of_match_table = of_match_ptr(pca954x_of_match),
Michael Lawnick7f528132010-08-11 18:21:03 +0200494 },
495 .probe = pca954x_probe,
Guenter Roeckdb79f2a2010-10-24 18:16:59 +0200496 .remove = pca954x_remove,
Michael Lawnick7f528132010-08-11 18:21:03 +0200497 .id_table = pca954x_id,
498};
499
Axel Linde054972012-03-26 21:47:19 +0200500module_i2c_driver(pca954x_driver);
Michael Lawnick7f528132010-08-11 18:21:03 +0200501
502MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
503MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
504MODULE_LICENSE("GPL v2");