Linux Audio

Check our new training course

Loading...
v4.6
 
  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 *
  7 * This module supports the PCA954x series of I2C multiplexer/switch chips
  8 * made by Philips Semiconductors.
  9 * This includes the:
 10 *	 PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547
 11 *	 and PCA9548.
 
 
 
 
 
 
 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
 31 *	pca9540.c from Jean Delvare <jdelvare@suse.de>.
 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
 38#include <linux/device.h>
 
 39#include <linux/gpio/consumer.h>
 40#include <linux/i2c.h>
 41#include <linux/i2c-mux.h>
 42#include <linux/i2c/pca954x.h>
 
 43#include <linux/module.h>
 44#include <linux/of.h>
 45#include <linux/pm.h>
 
 
 46#include <linux/slab.h>
 
 
 47
 48#define PCA954X_MAX_NCHANS 8
 49
 
 
 50enum pca_type {
 
 
 
 
 
 
 51	pca_9540,
 52	pca_9542,
 53	pca_9543,
 54	pca_9544,
 55	pca_9545,
 56	pca_9546,
 57	pca_9547,
 58	pca_9548,
 59};
 60
 61struct pca954x {
 62	enum pca_type type;
 63	struct i2c_adapter *virt_adaps[PCA954X_MAX_NCHANS];
 64
 65	u8 last_chan;		/* last register value */
 66};
 67
 68struct chip_desc {
 69	u8 nchans;
 70	u8 enable;	/* used for muxes only */
 
 71	enum muxtype {
 72		pca954x_ismux = 0,
 73		pca954x_isswi
 74	} muxtype;
 
 75};
 76
 77/* Provide specs for the PCA954x types we know about */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 78static const struct chip_desc chips[] = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 79	[pca_9540] = {
 80		.nchans = 2,
 81		.enable = 0x4,
 82		.muxtype = pca954x_ismux,
 
 
 
 
 
 
 
 
 83	},
 84	[pca_9543] = {
 85		.nchans = 2,
 
 86		.muxtype = pca954x_isswi,
 
 87	},
 88	[pca_9544] = {
 89		.nchans = 4,
 90		.enable = 0x4,
 
 91		.muxtype = pca954x_ismux,
 
 92	},
 93	[pca_9545] = {
 94		.nchans = 4,
 
 95		.muxtype = pca954x_isswi,
 
 
 
 
 
 
 96	},
 97	[pca_9547] = {
 98		.nchans = 8,
 99		.enable = 0x8,
100		.muxtype = pca954x_ismux,
 
101	},
102	[pca_9548] = {
103		.nchans = 8,
104		.muxtype = pca954x_isswi,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105	},
106};
107
108static const struct i2c_device_id pca954x_id[] = {
 
 
 
 
 
 
109	{ "pca9540", pca_9540 },
110	{ "pca9542", pca_9540 },
111	{ "pca9543", pca_9543 },
112	{ "pca9544", pca_9544 },
113	{ "pca9545", pca_9545 },
114	{ "pca9546", pca_9545 },
115	{ "pca9547", pca_9547 },
116	{ "pca9548", pca_9548 },
 
 
 
 
117	{ }
118};
119MODULE_DEVICE_TABLE(i2c, pca954x_id);
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121/* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
122   for this as they will try to lock adapter a second time */
123static int pca954x_reg_write(struct i2c_adapter *adap,
124			     struct i2c_client *client, u8 val)
125{
126	int ret = -ENODEV;
127
128	if (adap->algo->master_xfer) {
129		struct i2c_msg msg;
130		char buf[1];
131
132		msg.addr = client->addr;
133		msg.flags = 0;
134		msg.len = 1;
135		buf[0] = val;
136		msg.buf = buf;
137		ret = __i2c_transfer(adap, &msg, 1);
138	} else {
139		union i2c_smbus_data data;
140		ret = adap->algo->smbus_xfer(adap, client->addr,
141					     client->flags,
142					     I2C_SMBUS_WRITE,
143					     val, I2C_SMBUS_BYTE, &data);
144	}
145
146	return ret;
 
 
 
 
 
 
147}
148
149static int pca954x_select_chan(struct i2c_adapter *adap,
150			       void *client, u32 chan)
151{
152	struct pca954x *data = i2c_get_clientdata(client);
153	const struct chip_desc *chip = &chips[data->type];
154	u8 regval;
155	int ret = 0;
156
157	/* we make switches look like muxes, not sure how to be smarter */
158	if (chip->muxtype == pca954x_ismux)
159		regval = chan | chip->enable;
160	else
161		regval = 1 << chan;
162
163	/* Only select the channel if its different from the last channel */
164	if (data->last_chan != regval) {
165		ret = pca954x_reg_write(adap, client, regval);
166		data->last_chan = regval;
167	}
168
169	return ret;
170}
171
172static int pca954x_deselect_mux(struct i2c_adapter *adap,
173				void *client, u32 chan)
174{
175	struct pca954x *data = i2c_get_clientdata(client);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
177	/* Deselect active channel */
178	data->last_chan = 0;
179	return pca954x_reg_write(adap, client, data->last_chan);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180}
181
182/*
183 * I2C init/probing/exit functions
184 */
185static int pca954x_probe(struct i2c_client *client,
186			 const struct i2c_device_id *id)
187{
188	struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
189	struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
190	struct device_node *of_node = client->dev.of_node;
191	bool idle_disconnect_dt;
192	struct gpio_desc *gpio;
193	int num, force, class;
194	struct pca954x *data;
 
195	int ret;
196
197	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
198		return -ENODEV;
199
200	data = devm_kzalloc(&client->dev, sizeof(struct pca954x), GFP_KERNEL);
201	if (!data)
 
202		return -ENOMEM;
 
203
204	i2c_set_clientdata(client, data);
 
205
206	/* Get the mux out of reset if a reset GPIO is specified. */
207	gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_LOW);
208	if (IS_ERR(gpio))
209		return PTR_ERR(gpio);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
211	/* Write the mux register at addr to verify
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212	 * that the mux is in fact present. This also
213	 * initializes the mux to disconnected state.
 
214	 */
215	if (i2c_smbus_write_byte(client, 0) < 0) {
216		dev_warn(&client->dev, "probe failed\n");
217		return -ENODEV;
 
 
218	}
219
220	data->type = id->driver_data;
221	data->last_chan = 0;		   /* force the first selection */
222
223	idle_disconnect_dt = of_node &&
224		of_property_read_bool(of_node, "i2c-mux-idle-disconnect");
225
226	/* Now create an adapter for each channel */
227	for (num = 0; num < chips[data->type].nchans; num++) {
228		bool idle_disconnect_pd = false;
229
230		force = 0;			  /* dynamic adap number */
231		class = 0;			  /* no class by default */
232		if (pdata) {
233			if (num < pdata->num_modes) {
234				/* force static number */
235				force = pdata->modes[num].adap_id;
236				class = pdata->modes[num].class;
237			} else
238				/* discard unconfigured channels */
239				break;
240			idle_disconnect_pd = pdata->modes[num].deselect_on_exit;
241		}
242
243		data->virt_adaps[num] =
244			i2c_add_mux_adapter(adap, &client->dev, client,
245				force, num, class, pca954x_select_chan,
246				(idle_disconnect_pd || idle_disconnect_dt)
247					? pca954x_deselect_mux : NULL);
248
249		if (data->virt_adaps[num] == NULL) {
250			ret = -ENODEV;
251			dev_err(&client->dev,
252				"failed to register multiplexed adapter"
253				" %d as bus %d\n", num, force);
254			goto virt_reg_failed;
255		}
256	}
257
258	dev_info(&client->dev,
259		 "registered %d multiplexed busses for I2C %s %s\n",
260		 num, chips[data->type].muxtype == pca954x_ismux
 
 
 
 
 
261				? "mux" : "switch", client->name);
262
263	return 0;
264
265virt_reg_failed:
266	for (num--; num >= 0; num--)
267		i2c_del_mux_adapter(data->virt_adaps[num]);
268	return ret;
269}
270
271static int pca954x_remove(struct i2c_client *client)
272{
273	struct pca954x *data = i2c_get_clientdata(client);
274	const struct chip_desc *chip = &chips[data->type];
275	int i;
276
277	for (i = 0; i < chip->nchans; ++i)
278		if (data->virt_adaps[i]) {
279			i2c_del_mux_adapter(data->virt_adaps[i]);
280			data->virt_adaps[i] = NULL;
281		}
282
283	return 0;
 
 
284}
285
286#ifdef CONFIG_PM_SLEEP
287static int pca954x_resume(struct device *dev)
288{
289	struct i2c_client *client = to_i2c_client(dev);
290	struct pca954x *data = i2c_get_clientdata(client);
 
 
291
292	data->last_chan = 0;
293	return i2c_smbus_write_byte(client, 0);
 
 
 
294}
295#endif
296
297static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
298
299static struct i2c_driver pca954x_driver = {
300	.driver		= {
301		.name	= "pca954x",
302		.pm	= &pca954x_pm,
 
303	},
304	.probe		= pca954x_probe,
305	.remove		= pca954x_remove,
306	.id_table	= pca954x_id,
307};
308
309module_i2c_driver(pca954x_driver);
310
311MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
312MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
313MODULE_LICENSE("GPL v2");
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  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 *
  8 * This module supports the PCA954x and PCA984x series of I2C multiplexer/switch
  9 * chips made by NXP Semiconductors.
 10 * This includes the:
 11 *	 PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547,
 12 *	 PCA9548, PCA9846, PCA9847, PCA9848 and PCA9849.
 13 *
 14 * It's also compatible to Maxims MAX735x I2C switch chips, which are controlled
 15 * as the NXP PCA9548 and the MAX736x chips that act like the PCA9544.
 16 *
 17 * This includes the:
 18 *	 MAX7356, MAX7357, MAX7358, MAX7367, MAX7368 and MAX7369
 19 *
 20 * These chips are all controlled via the I2C bus itself, and all have a
 21 * single 8-bit register. The upstream "parent" bus fans out to two,
 22 * four, or eight downstream busses or channels; which of these
 23 * are selected is determined by the chip type and register contents. A
 24 * mux can select only one sub-bus at a time; a switch can select any
 25 * combination simultaneously.
 26 *
 27 * Based on:
 28 *	pca954x.c from Kumar Gala <galak@kernel.crashing.org>
 29 * Copyright (C) 2006
 30 *
 31 * Based on:
 32 *	pca954x.c from Ken Harrenstien
 33 * Copyright (C) 2004 Google, Inc. (Ken Harrenstien)
 34 *
 35 * Based on:
 36 *	i2c-virtual_cb.c from Brian Kuschak <bkuschak@yahoo.com>
 37 * and
 38 *	pca9540.c from Jean Delvare <jdelvare@suse.de>.
 
 
 
 
 39 */
 40
 41#include <linux/device.h>
 42#include <linux/delay.h>
 43#include <linux/gpio/consumer.h>
 44#include <linux/i2c.h>
 45#include <linux/i2c-mux.h>
 46#include <linux/interrupt.h>
 47#include <linux/irq.h>
 48#include <linux/module.h>
 
 49#include <linux/pm.h>
 50#include <linux/property.h>
 51#include <linux/regulator/consumer.h>
 52#include <linux/slab.h>
 53#include <linux/spinlock.h>
 54#include <dt-bindings/mux/mux.h>
 55
 56#define PCA954X_MAX_NCHANS 8
 57
 58#define PCA954X_IRQ_OFFSET 4
 59
 60enum pca_type {
 61	max_7356,
 62	max_7357,
 63	max_7358,
 64	max_7367,
 65	max_7368,
 66	max_7369,
 67	pca_9540,
 68	pca_9542,
 69	pca_9543,
 70	pca_9544,
 71	pca_9545,
 72	pca_9546,
 73	pca_9547,
 74	pca_9548,
 75	pca_9846,
 76	pca_9847,
 77	pca_9848,
 78	pca_9849,
 
 
 
 79};
 80
 81struct chip_desc {
 82	u8 nchans;
 83	u8 enable;	/* used for muxes only */
 84	u8 has_irq;
 85	enum muxtype {
 86		pca954x_ismux = 0,
 87		pca954x_isswi
 88	} muxtype;
 89	struct i2c_device_identity id;
 90};
 91
 92struct pca954x {
 93	const struct chip_desc *chip;
 94
 95	u8 last_chan;		/* last register value */
 96	/* MUX_IDLE_AS_IS, MUX_IDLE_DISCONNECT or >= 0 for channel */
 97	s32 idle_state;
 98
 99	struct i2c_client *client;
100
101	struct irq_domain *irq;
102	unsigned int irq_mask;
103	raw_spinlock_t lock;
104	struct regulator *supply;
105};
106
107/* Provide specs for the MAX735x, PCA954x and PCA984x types we know about */
108static const struct chip_desc chips[] = {
109	[max_7356] = {
110		.nchans = 8,
111		.muxtype = pca954x_isswi,
112		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
113	},
114	[max_7357] = {
115		.nchans = 8,
116		.muxtype = pca954x_isswi,
117		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
118		/*
119		 * No interrupt controller support. The interrupt
120		 * provides information about stuck channels.
121		 */
122	},
123	[max_7358] = {
124		.nchans = 8,
125		.muxtype = pca954x_isswi,
126		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
127		/*
128		 * No interrupt controller support. The interrupt
129		 * provides information about stuck channels.
130		 */
131	},
132	[max_7367] = {
133		.nchans = 4,
134		.muxtype = pca954x_isswi,
135		.has_irq = 1,
136		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
137	},
138	[max_7368] = {
139		.nchans = 4,
140		.muxtype = pca954x_isswi,
141		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
142	},
143	[max_7369] = {
144		.nchans = 4,
145		.enable = 0x4,
146		.muxtype = pca954x_ismux,
147		.has_irq = 1,
148		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
149	},
150	[pca_9540] = {
151		.nchans = 2,
152		.enable = 0x4,
153		.muxtype = pca954x_ismux,
154		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
155	},
156	[pca_9542] = {
157		.nchans = 2,
158		.enable = 0x4,
159		.has_irq = 1,
160		.muxtype = pca954x_ismux,
161		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
162	},
163	[pca_9543] = {
164		.nchans = 2,
165		.has_irq = 1,
166		.muxtype = pca954x_isswi,
167		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
168	},
169	[pca_9544] = {
170		.nchans = 4,
171		.enable = 0x4,
172		.has_irq = 1,
173		.muxtype = pca954x_ismux,
174		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
175	},
176	[pca_9545] = {
177		.nchans = 4,
178		.has_irq = 1,
179		.muxtype = pca954x_isswi,
180		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
181	},
182	[pca_9546] = {
183		.nchans = 4,
184		.muxtype = pca954x_isswi,
185		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
186	},
187	[pca_9547] = {
188		.nchans = 8,
189		.enable = 0x8,
190		.muxtype = pca954x_ismux,
191		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
192	},
193	[pca_9548] = {
194		.nchans = 8,
195		.muxtype = pca954x_isswi,
196		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
197	},
198	[pca_9846] = {
199		.nchans = 4,
200		.muxtype = pca954x_isswi,
201		.id = {
202			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
203			.part_id = 0x10b,
204		},
205	},
206	[pca_9847] = {
207		.nchans = 8,
208		.enable = 0x8,
209		.muxtype = pca954x_ismux,
210		.id = {
211			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
212			.part_id = 0x108,
213		},
214	},
215	[pca_9848] = {
216		.nchans = 8,
217		.muxtype = pca954x_isswi,
218		.id = {
219			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
220			.part_id = 0x10a,
221		},
222	},
223	[pca_9849] = {
224		.nchans = 4,
225		.enable = 0x4,
226		.muxtype = pca954x_ismux,
227		.id = {
228			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
229			.part_id = 0x109,
230		},
231	},
232};
233
234static const struct i2c_device_id pca954x_id[] = {
235	{ "max7356", max_7356 },
236	{ "max7357", max_7357 },
237	{ "max7358", max_7358 },
238	{ "max7367", max_7367 },
239	{ "max7368", max_7368 },
240	{ "max7369", max_7369 },
241	{ "pca9540", pca_9540 },
242	{ "pca9542", pca_9542 },
243	{ "pca9543", pca_9543 },
244	{ "pca9544", pca_9544 },
245	{ "pca9545", pca_9545 },
246	{ "pca9546", pca_9546 },
247	{ "pca9547", pca_9547 },
248	{ "pca9548", pca_9548 },
249	{ "pca9846", pca_9846 },
250	{ "pca9847", pca_9847 },
251	{ "pca9848", pca_9848 },
252	{ "pca9849", pca_9849 },
253	{ }
254};
255MODULE_DEVICE_TABLE(i2c, pca954x_id);
256
257static const struct of_device_id pca954x_of_match[] = {
258	{ .compatible = "maxim,max7356", .data = &chips[max_7356] },
259	{ .compatible = "maxim,max7357", .data = &chips[max_7357] },
260	{ .compatible = "maxim,max7358", .data = &chips[max_7358] },
261	{ .compatible = "maxim,max7367", .data = &chips[max_7367] },
262	{ .compatible = "maxim,max7368", .data = &chips[max_7368] },
263	{ .compatible = "maxim,max7369", .data = &chips[max_7369] },
264	{ .compatible = "nxp,pca9540", .data = &chips[pca_9540] },
265	{ .compatible = "nxp,pca9542", .data = &chips[pca_9542] },
266	{ .compatible = "nxp,pca9543", .data = &chips[pca_9543] },
267	{ .compatible = "nxp,pca9544", .data = &chips[pca_9544] },
268	{ .compatible = "nxp,pca9545", .data = &chips[pca_9545] },
269	{ .compatible = "nxp,pca9546", .data = &chips[pca_9546] },
270	{ .compatible = "nxp,pca9547", .data = &chips[pca_9547] },
271	{ .compatible = "nxp,pca9548", .data = &chips[pca_9548] },
272	{ .compatible = "nxp,pca9846", .data = &chips[pca_9846] },
273	{ .compatible = "nxp,pca9847", .data = &chips[pca_9847] },
274	{ .compatible = "nxp,pca9848", .data = &chips[pca_9848] },
275	{ .compatible = "nxp,pca9849", .data = &chips[pca_9849] },
276	{}
277};
278MODULE_DEVICE_TABLE(of, pca954x_of_match);
279
280/* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
281   for this as they will try to lock adapter a second time */
282static int pca954x_reg_write(struct i2c_adapter *adap,
283			     struct i2c_client *client, u8 val)
284{
285	union i2c_smbus_data dummy;
286
287	return __i2c_smbus_xfer(adap, client->addr, client->flags,
288				I2C_SMBUS_WRITE, val,
289				I2C_SMBUS_BYTE, &dummy);
290}
 
 
 
 
 
 
 
 
 
 
 
 
 
291
292static u8 pca954x_regval(struct pca954x *data, u8 chan)
293{
294	/* We make switches look like muxes, not sure how to be smarter. */
295	if (data->chip->muxtype == pca954x_ismux)
296		return chan | data->chip->enable;
297	else
298		return 1 << chan;
299}
300
301static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
 
302{
303	struct pca954x *data = i2c_mux_priv(muxc);
304	struct i2c_client *client = data->client;
305	u8 regval;
306	int ret = 0;
307
308	regval = pca954x_regval(data, chan);
 
 
 
 
 
309	/* Only select the channel if its different from the last channel */
310	if (data->last_chan != regval) {
311		ret = pca954x_reg_write(muxc->parent, client, regval);
312		data->last_chan = ret < 0 ? 0 : regval;
313	}
314
315	return ret;
316}
317
318static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
 
319{
320	struct pca954x *data = i2c_mux_priv(muxc);
321	struct i2c_client *client = data->client;
322	s32 idle_state;
323
324	idle_state = READ_ONCE(data->idle_state);
325	if (idle_state >= 0)
326		/* Set the mux back to a predetermined channel */
327		return pca954x_select_chan(muxc, idle_state);
328
329	if (idle_state == MUX_IDLE_DISCONNECT) {
330		/* Deselect active channel */
331		data->last_chan = 0;
332		return pca954x_reg_write(muxc->parent, client,
333					 data->last_chan);
334	}
335
336	/* otherwise leave as-is */
337
338	return 0;
339}
340
341static ssize_t idle_state_show(struct device *dev,
342				    struct device_attribute *attr,
343				    char *buf)
344{
345	struct i2c_client *client = to_i2c_client(dev);
346	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
347	struct pca954x *data = i2c_mux_priv(muxc);
348
349	return sprintf(buf, "%d\n", READ_ONCE(data->idle_state));
350}
351
352static ssize_t idle_state_store(struct device *dev,
353				struct device_attribute *attr,
354				const char *buf, size_t count)
355{
356	struct i2c_client *client = to_i2c_client(dev);
357	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
358	struct pca954x *data = i2c_mux_priv(muxc);
359	int val;
360	int ret;
361
362	ret = kstrtoint(buf, 0, &val);
363	if (ret < 0)
364		return ret;
365
366	if (val != MUX_IDLE_AS_IS && val != MUX_IDLE_DISCONNECT &&
367	    (val < 0 || val >= data->chip->nchans))
368		return -EINVAL;
369
370	i2c_lock_bus(muxc->parent, I2C_LOCK_SEGMENT);
371
372	WRITE_ONCE(data->idle_state, val);
373	/*
374	 * Set the mux into a state consistent with the new
375	 * idle_state.
376	 */
377	if (data->last_chan || val != MUX_IDLE_DISCONNECT)
378		ret = pca954x_deselect_mux(muxc, 0);
379
380	i2c_unlock_bus(muxc->parent, I2C_LOCK_SEGMENT);
381
382	return ret < 0 ? ret : count;
383}
384
385static DEVICE_ATTR_RW(idle_state);
386
387static irqreturn_t pca954x_irq_handler(int irq, void *dev_id)
388{
389	struct pca954x *data = dev_id;
390	unsigned long pending;
391	int ret, i;
392
393	ret = i2c_smbus_read_byte(data->client);
394	if (ret < 0)
395		return IRQ_NONE;
396
397	pending = (ret >> PCA954X_IRQ_OFFSET) & (BIT(data->chip->nchans) - 1);
398	for_each_set_bit(i, &pending, data->chip->nchans)
399		handle_nested_irq(irq_linear_revmap(data->irq, i));
400
401	return IRQ_RETVAL(pending);
402}
403
404static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type)
405{
406	if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW)
407		return -EINVAL;
408	return 0;
409}
410
411static struct irq_chip pca954x_irq_chip = {
412	.name = "i2c-mux-pca954x",
413	.irq_set_type = pca954x_irq_set_type,
414};
415
416static int pca954x_irq_setup(struct i2c_mux_core *muxc)
417{
418	struct pca954x *data = i2c_mux_priv(muxc);
419	struct i2c_client *client = data->client;
420	int c, irq;
421
422	if (!data->chip->has_irq || client->irq <= 0)
423		return 0;
424
425	raw_spin_lock_init(&data->lock);
426
427	data->irq = irq_domain_add_linear(client->dev.of_node,
428					  data->chip->nchans,
429					  &irq_domain_simple_ops, data);
430	if (!data->irq)
431		return -ENODEV;
432
433	for (c = 0; c < data->chip->nchans; c++) {
434		irq = irq_create_mapping(data->irq, c);
435		if (!irq) {
436			dev_err(&client->dev, "failed irq create map\n");
437			return -EINVAL;
438		}
439		irq_set_chip_data(irq, data);
440		irq_set_chip_and_handler(irq, &pca954x_irq_chip,
441			handle_simple_irq);
442	}
443
444	return 0;
445}
446
447static void pca954x_cleanup(struct i2c_mux_core *muxc)
448{
449	struct pca954x *data = i2c_mux_priv(muxc);
450	int c, irq;
451
452	regulator_disable(data->supply);
453
454	if (data->irq) {
455		for (c = 0; c < data->chip->nchans; c++) {
456			irq = irq_find_mapping(data->irq, c);
457			irq_dispose_mapping(irq);
458		}
459		irq_domain_remove(data->irq);
460	}
461	i2c_mux_del_adapters(muxc);
462}
463
464static int pca954x_init(struct i2c_client *client, struct pca954x *data)
465{
466	int ret;
467
468	if (data->idle_state >= 0)
469		data->last_chan = pca954x_regval(data, data->idle_state);
470	else
471		data->last_chan = 0; /* Disconnect multiplexer */
472
473	ret = i2c_smbus_write_byte(client, data->last_chan);
474	if (ret < 0)
475		data->last_chan = 0;
476
477	return ret;
478}
479
480/*
481 * I2C init/probing/exit functions
482 */
483static int pca954x_probe(struct i2c_client *client)
 
484{
485	const struct i2c_device_id *id = i2c_client_get_device_id(client);
486	struct i2c_adapter *adap = client->adapter;
487	struct device *dev = &client->dev;
 
488	struct gpio_desc *gpio;
489	struct i2c_mux_core *muxc;
490	struct pca954x *data;
491	int num;
492	int ret;
493
494	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
495		return -ENODEV;
496
497	muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0,
498			     pca954x_select_chan, pca954x_deselect_mux);
499	if (!muxc)
500		return -ENOMEM;
501	data = i2c_mux_priv(muxc);
502
503	i2c_set_clientdata(client, muxc);
504	data->client = client;
505
506	data->supply = devm_regulator_get(dev, "vdd");
507	if (IS_ERR(data->supply))
508		return dev_err_probe(dev, PTR_ERR(data->supply),
509				"Failed to request regulator\n");
510
511	ret = regulator_enable(data->supply);
512	if (ret)
513		return dev_err_probe(dev, ret,
514				     "Failed to enable vdd supply\n");
515
516	/* Reset the mux if a reset GPIO is specified. */
517	gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
518	if (IS_ERR(gpio)) {
519		ret = PTR_ERR(gpio);
520		goto fail_cleanup;
521	}
522	if (gpio) {
523		udelay(1);
524		gpiod_set_value_cansleep(gpio, 0);
525		/* Give the chip some time to recover. */
526		udelay(1);
527	}
528
529	data->chip = device_get_match_data(dev);
530	if (!data->chip)
531		data->chip = &chips[id->driver_data];
532
533	if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) {
534		struct i2c_device_identity id;
535
536		ret = i2c_get_device_id(client, &id);
537		if (ret && ret != -EOPNOTSUPP)
538			goto fail_cleanup;
539
540		if (!ret &&
541		    (id.manufacturer_id != data->chip->id.manufacturer_id ||
542		     id.part_id != data->chip->id.part_id)) {
543			dev_warn(dev, "unexpected device id %03x-%03x-%x\n",
544				 id.manufacturer_id, id.part_id,
545				 id.die_revision);
546			ret = -ENODEV;
547			goto fail_cleanup;
548		}
549	}
550
551	data->idle_state = MUX_IDLE_AS_IS;
552	if (device_property_read_u32(dev, "idle-state", &data->idle_state)) {
553		if (device_property_read_bool(dev, "i2c-mux-idle-disconnect"))
554			data->idle_state = MUX_IDLE_DISCONNECT;
555	}
556
557	/*
558	 * Write the mux register at addr to verify
559	 * that the mux is in fact present. This also
560	 * initializes the mux to a channel
561	 * or disconnected state.
562	 */
563	ret = pca954x_init(client, data);
564	if (ret < 0) {
565		dev_warn(dev, "probe failed\n");
566		ret = -ENODEV;
567		goto fail_cleanup;
568	}
569
570	ret = pca954x_irq_setup(muxc);
571	if (ret)
572		goto fail_cleanup;
 
 
573
574	/* Now create an adapter for each channel */
575	for (num = 0; num < data->chip->nchans; num++) {
576		ret = i2c_mux_add_adapter(muxc, 0, num, 0);
577		if (ret)
578			goto fail_cleanup;
579	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
580
581	if (data->irq) {
582		ret = devm_request_threaded_irq(dev, data->client->irq,
583						NULL, pca954x_irq_handler,
584						IRQF_ONESHOT | IRQF_SHARED,
585						"pca954x", data);
586		if (ret)
587			goto fail_cleanup;
588	}
589
590	/*
591	 * The attr probably isn't going to be needed in most cases,
592	 * so don't fail completely on error.
593	 */
594	device_create_file(dev, &dev_attr_idle_state);
595
596	dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n",
597		 num, data->chip->muxtype == pca954x_ismux
598				? "mux" : "switch", client->name);
599
600	return 0;
601
602fail_cleanup:
603	pca954x_cleanup(muxc);
 
604	return ret;
605}
606
607static void pca954x_remove(struct i2c_client *client)
608{
609	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
 
 
 
 
 
 
 
 
610
611	device_remove_file(&client->dev, &dev_attr_idle_state);
612
613	pca954x_cleanup(muxc);
614}
615
 
616static int pca954x_resume(struct device *dev)
617{
618	struct i2c_client *client = to_i2c_client(dev);
619	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
620	struct pca954x *data = i2c_mux_priv(muxc);
621	int ret;
622
623	ret = pca954x_init(client, data);
624	if (ret < 0)
625		dev_err(&client->dev, "failed to verify mux presence\n");
626
627	return ret;
628}
 
629
630static DEFINE_SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
631
632static struct i2c_driver pca954x_driver = {
633	.driver		= {
634		.name	= "pca954x",
635		.pm	= pm_sleep_ptr(&pca954x_pm),
636		.of_match_table = pca954x_of_match,
637	},
638	.probe		= pca954x_probe,
639	.remove		= pca954x_remove,
640	.id_table	= pca954x_id,
641};
642
643module_i2c_driver(pca954x_driver);
644
645MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
646MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
647MODULE_LICENSE("GPL v2");