Linux Audio

Check our new training course

Linux kernel drivers training

May 6-19, 2025
Register
Loading...
v6.2
  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 * 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
 32 *	pca9540.c from Jean Delvare <jdelvare@suse.de>.
 
 
 
 
 33 */
 34
 
 35#include <linux/device.h>
 36#include <linux/delay.h>
 37#include <linux/gpio/consumer.h>
 38#include <linux/i2c.h>
 39#include <linux/i2c-mux.h>
 40#include <linux/interrupt.h>
 41#include <linux/irq.h>
 42#include <linux/module.h>
 
 
 43#include <linux/pm.h>
 44#include <linux/property.h>
 45#include <linux/slab.h>
 46#include <linux/spinlock.h>
 47#include <dt-bindings/mux/mux.h>
 48
 49#define PCA954X_MAX_NCHANS 8
 50
 51#define PCA954X_IRQ_OFFSET 4
 52
 53enum 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,
 62	pca_9846,
 63	pca_9847,
 64	pca_9848,
 65	pca_9849,
 66};
 67
 68struct chip_desc {
 69	u8 nchans;
 70	u8 enable;	/* used for muxes only */
 71	u8 has_irq;
 72	enum muxtype {
 73		pca954x_ismux = 0,
 74		pca954x_isswi
 75	} muxtype;
 76	struct i2c_device_identity id;
 77};
 78
 79struct pca954x {
 80	const struct chip_desc *chip;
 81
 82	u8 last_chan;		/* last register value */
 83	/* MUX_IDLE_AS_IS, MUX_IDLE_DISCONNECT or >= 0 for channel */
 84	s32 idle_state;
 85
 86	struct i2c_client *client;
 87
 88	struct irq_domain *irq;
 89	unsigned int irq_mask;
 90	raw_spinlock_t lock;
 91};
 92
 93/* 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,
 99		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
100	},
101	[pca_9542] = {
102		.nchans = 2,
103		.enable = 0x4,
104		.has_irq = 1,
105		.muxtype = pca954x_ismux,
106		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
107	},
108	[pca_9543] = {
109		.nchans = 2,
110		.has_irq = 1,
111		.muxtype = pca954x_isswi,
112		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
113	},
114	[pca_9544] = {
115		.nchans = 4,
116		.enable = 0x4,
117		.has_irq = 1,
118		.muxtype = pca954x_ismux,
119		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
120	},
121	[pca_9545] = {
122		.nchans = 4,
123		.has_irq = 1,
124		.muxtype = pca954x_isswi,
125		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
126	},
127	[pca_9546] = {
128		.nchans = 4,
129		.muxtype = pca954x_isswi,
130		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
131	},
132	[pca_9547] = {
133		.nchans = 8,
134		.enable = 0x8,
135		.muxtype = pca954x_ismux,
136		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
137	},
138	[pca_9548] = {
139		.nchans = 8,
140		.muxtype = pca954x_isswi,
141		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
142	},
143	[pca_9846] = {
144		.nchans = 4,
145		.muxtype = pca954x_isswi,
146		.id = {
147			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
148			.part_id = 0x10b,
149		},
150	},
151	[pca_9847] = {
152		.nchans = 8,
153		.enable = 0x8,
154		.muxtype = pca954x_ismux,
155		.id = {
156			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
157			.part_id = 0x108,
158		},
159	},
160	[pca_9848] = {
161		.nchans = 8,
162		.muxtype = pca954x_isswi,
163		.id = {
164			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
165			.part_id = 0x10a,
166		},
167	},
168	[pca_9849] = {
169		.nchans = 4,
170		.enable = 0x4,
171		.muxtype = pca954x_ismux,
172		.id = {
173			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
174			.part_id = 0x109,
175		},
176	},
177};
178
179static const struct i2c_device_id pca954x_id[] = {
180	{ "pca9540", pca_9540 },
181	{ "pca9542", pca_9542 },
182	{ "pca9543", pca_9543 },
183	{ "pca9544", pca_9544 },
184	{ "pca9545", pca_9545 },
185	{ "pca9546", pca_9546 },
186	{ "pca9547", pca_9547 },
187	{ "pca9548", pca_9548 },
188	{ "pca9846", pca_9846 },
189	{ "pca9847", pca_9847 },
190	{ "pca9848", pca_9848 },
191	{ "pca9849", pca_9849 },
192	{ }
193};
194MODULE_DEVICE_TABLE(i2c, pca954x_id);
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196static 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] },
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] },
209	{}
210};
211MODULE_DEVICE_TABLE(of, pca954x_of_match);
212
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 */
215static int pca954x_reg_write(struct i2c_adapter *adap,
216			     struct i2c_client *client, u8 val)
217{
218	union i2c_smbus_data dummy;
219
220	return __i2c_smbus_xfer(adap, client->addr, client->flags,
221				I2C_SMBUS_WRITE, val,
222				I2C_SMBUS_BYTE, &dummy);
223}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
225static 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
234static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
235{
236	struct pca954x *data = i2c_mux_priv(muxc);
237	struct i2c_client *client = data->client;
 
238	u8 regval;
239	int ret = 0;
240
241	regval = pca954x_regval(data, chan);
 
 
 
 
 
242	/* Only select the channel if its different from the last channel */
243	if (data->last_chan != regval) {
244		ret = pca954x_reg_write(muxc->parent, client, regval);
245		data->last_chan = ret < 0 ? 0 : regval;
246	}
247
248	return ret;
249}
250
251static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
252{
253	struct pca954x *data = i2c_mux_priv(muxc);
254	struct i2c_client *client = data->client;
255	s32 idle_state;
256
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);
261
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;
272}
273
274static 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
320static irqreturn_t pca954x_irq_handler(int irq, void *dev_id)
321{
322	struct pca954x *data = dev_id;
323	unsigned long pending;
324	int ret, i;
325
326	ret = i2c_smbus_read_byte(data->client);
327	if (ret < 0)
328		return IRQ_NONE;
329
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);
335}
336
337static 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",
346	.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;
353	int c, irq;
354
355	if (!data->chip->has_irq || client->irq <= 0)
356		return 0;
357
358	raw_spin_lock_init(&data->lock);
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);
368		if (!irq) {
369			dev_err(&client->dev, "failed irq create map\n");
370			return -EINVAL;
371		}
372		irq_set_chip_data(irq, data);
373		irq_set_chip_and_handler(irq, &pca954x_irq_chip,
374			handle_simple_irq);
375	}
376
377	return 0;
378}
379
380static 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);
393}
394
395static 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
411/*
412 * I2C init/probing/exit functions
413 */
414static int pca954x_probe(struct i2c_client *client)
 
415{
416	const struct i2c_device_id *id = i2c_client_get_device_id(client);
417	struct i2c_adapter *adap = client->adapter;
418	struct device *dev = &client->dev;
 
419	struct gpio_desc *gpio;
 
420	struct i2c_mux_core *muxc;
421	struct pca954x *data;
422	int num;
423	int ret;
424
425	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
426		return -ENODEV;
427
428	muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0,
 
429			     pca954x_select_chan, pca954x_deselect_mux);
430	if (!muxc)
431		return -ENOMEM;
432	data = i2c_mux_priv(muxc);
433
434	i2c_set_clientdata(client, muxc);
435	data->client = client;
436
437	/* Reset the mux if a reset GPIO is specified. */
438	gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
439	if (IS_ERR(gpio))
440		return PTR_ERR(gpio);
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	}
447
448	data->chip = device_get_match_data(dev);
449	if (!data->chip)
 
 
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)) {
462			dev_warn(dev, "unexpected device id %03x-%03x-%x\n",
463				 id.manufacturer_id, id.part_id,
464				 id.die_revision);
465			return -ENODEV;
466		}
467	}
468
469	data->idle_state = MUX_IDLE_AS_IS;
470	if (device_property_read_u32(dev, "idle-state", &data->idle_state)) {
471		if (device_property_read_bool(dev, "i2c-mux-idle-disconnect"))
472			data->idle_state = MUX_IDLE_DISCONNECT;
473	}
474
475	/*
476	 * Write the mux register at addr to verify
477	 * that the mux is in fact present. This also
478	 * initializes the mux to a channel
479	 * or disconnected state.
480	 */
481	ret = pca954x_init(client, data);
482	if (ret < 0) {
483		dev_warn(dev, "probe failed\n");
484		return -ENODEV;
485	}
486
487	ret = pca954x_irq_setup(muxc);
488	if (ret)
489		goto fail_cleanup;
490
491	/* Now create an adapter for each channel */
492	for (num = 0; num < data->chip->nchans; num++) {
493		ret = i2c_mux_add_adapter(muxc, 0, num, 0);
494		if (ret)
495			goto fail_cleanup;
496	}
497
498	if (data->irq) {
499		ret = devm_request_threaded_irq(dev, data->client->irq,
500						NULL, pca954x_irq_handler,
501						IRQF_ONESHOT | IRQF_SHARED,
502						"pca954x", data);
503		if (ret)
504			goto fail_cleanup;
505	}
 
 
 
 
 
 
506
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
513	dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n",
 
514		 num, data->chip->muxtype == pca954x_ismux
515				? "mux" : "switch", client->name);
516
517	return 0;
518
519fail_cleanup:
520	pca954x_cleanup(muxc);
521	return ret;
522}
523
524static void pca954x_remove(struct i2c_client *client)
525{
526	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
527
528	device_remove_file(&client->dev, &dev_attr_idle_state);
529
530	pca954x_cleanup(muxc);
531}
532
533#ifdef CONFIG_PM_SLEEP
534static int pca954x_resume(struct device *dev)
535{
536	struct i2c_client *client = to_i2c_client(dev);
537	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
538	struct pca954x *data = i2c_mux_priv(muxc);
539	int ret;
540
541	ret = pca954x_init(client, data);
542	if (ret < 0)
543		dev_err(&client->dev, "failed to verify mux presence\n");
544
545	return ret;
 
546}
547#endif
548
549static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
550
551static struct i2c_driver pca954x_driver = {
552	.driver		= {
553		.name	= "pca954x",
554		.pm	= &pca954x_pm,
555		.of_match_table = pca954x_of_match,
 
556	},
557	.probe_new	= pca954x_probe,
558	.remove		= pca954x_remove,
559	.id_table	= pca954x_id,
560};
561
562module_i2c_driver(pca954x_driver);
563
564MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
565MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
566MODULE_LICENSE("GPL v2");
v4.10.11
 
  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/acpi.h>
 39#include <linux/device.h>
 
 40#include <linux/gpio/consumer.h>
 41#include <linux/i2c.h>
 42#include <linux/i2c-mux.h>
 43#include <linux/i2c/pca954x.h>
 
 44#include <linux/module.h>
 45#include <linux/of.h>
 46#include <linux/of_device.h>
 47#include <linux/pm.h>
 
 48#include <linux/slab.h>
 
 
 49
 50#define PCA954X_MAX_NCHANS 8
 51
 
 
 52enum pca_type {
 53	pca_9540,
 54	pca_9542,
 55	pca_9543,
 56	pca_9544,
 57	pca_9545,
 58	pca_9546,
 59	pca_9547,
 60	pca_9548,
 
 
 
 
 61};
 62
 63struct chip_desc {
 64	u8 nchans;
 65	u8 enable;	/* used for muxes only */
 
 66	enum muxtype {
 67		pca954x_ismux = 0,
 68		pca954x_isswi
 69	} muxtype;
 
 70};
 71
 72struct pca954x {
 73	const struct chip_desc *chip;
 74
 75	u8 last_chan;		/* last register value */
 76	u8 deselect;
 
 
 77	struct i2c_client *client;
 
 
 
 
 78};
 79
 80/* Provide specs for the PCA954x types we know about */
 81static const struct chip_desc chips[] = {
 82	[pca_9540] = {
 83		.nchans = 2,
 84		.enable = 0x4,
 85		.muxtype = pca954x_ismux,
 
 
 
 
 
 
 
 
 86	},
 87	[pca_9543] = {
 88		.nchans = 2,
 
 89		.muxtype = pca954x_isswi,
 
 90	},
 91	[pca_9544] = {
 92		.nchans = 4,
 93		.enable = 0x4,
 
 94		.muxtype = pca954x_ismux,
 
 95	},
 96	[pca_9545] = {
 97		.nchans = 4,
 
 98		.muxtype = pca954x_isswi,
 
 
 
 
 
 
 99	},
100	[pca_9547] = {
101		.nchans = 8,
102		.enable = 0x8,
103		.muxtype = pca954x_ismux,
 
104	},
105	[pca_9548] = {
106		.nchans = 8,
107		.muxtype = pca954x_isswi,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108	},
109};
110
111static const struct i2c_device_id pca954x_id[] = {
112	{ "pca9540", pca_9540 },
113	{ "pca9542", pca_9540 },
114	{ "pca9543", pca_9543 },
115	{ "pca9544", pca_9544 },
116	{ "pca9545", pca_9545 },
117	{ "pca9546", pca_9545 },
118	{ "pca9547", pca_9547 },
119	{ "pca9548", pca_9548 },
 
 
 
 
120	{ }
121};
122MODULE_DEVICE_TABLE(i2c, pca954x_id);
123
124#ifdef CONFIG_ACPI
125static const struct acpi_device_id pca954x_acpi_ids[] = {
126	{ .id = "PCA9540", .driver_data = pca_9540 },
127	{ .id = "PCA9542", .driver_data = pca_9540 },
128	{ .id = "PCA9543", .driver_data = pca_9543 },
129	{ .id = "PCA9544", .driver_data = pca_9544 },
130	{ .id = "PCA9545", .driver_data = pca_9545 },
131	{ .id = "PCA9546", .driver_data = pca_9545 },
132	{ .id = "PCA9547", .driver_data = pca_9547 },
133	{ .id = "PCA9548", .driver_data = pca_9548 },
134	{ }
135};
136MODULE_DEVICE_TABLE(acpi, pca954x_acpi_ids);
137#endif
138
139#ifdef CONFIG_OF
140static const struct of_device_id pca954x_of_match[] = {
141	{ .compatible = "nxp,pca9540", .data = &chips[pca_9540] },
142	{ .compatible = "nxp,pca9542", .data = &chips[pca_9542] },
143	{ .compatible = "nxp,pca9543", .data = &chips[pca_9543] },
144	{ .compatible = "nxp,pca9544", .data = &chips[pca_9544] },
145	{ .compatible = "nxp,pca9545", .data = &chips[pca_9545] },
146	{ .compatible = "nxp,pca9546", .data = &chips[pca_9546] },
147	{ .compatible = "nxp,pca9547", .data = &chips[pca_9547] },
148	{ .compatible = "nxp,pca9548", .data = &chips[pca_9548] },
 
 
 
 
149	{}
150};
151#endif
152
153/* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
154   for this as they will try to lock adapter a second time */
155static int pca954x_reg_write(struct i2c_adapter *adap,
156			     struct i2c_client *client, u8 val)
157{
158	int ret = -ENODEV;
159
160	if (adap->algo->master_xfer) {
161		struct i2c_msg msg;
162		char buf[1];
163
164		msg.addr = client->addr;
165		msg.flags = 0;
166		msg.len = 1;
167		buf[0] = val;
168		msg.buf = buf;
169		ret = __i2c_transfer(adap, &msg, 1);
170
171		if (ret >= 0 && ret != 1)
172			ret = -EREMOTEIO;
173	} else {
174		union i2c_smbus_data data;
175		ret = adap->algo->smbus_xfer(adap, client->addr,
176					     client->flags,
177					     I2C_SMBUS_WRITE,
178					     val, I2C_SMBUS_BYTE, &data);
179	}
180
181	return ret;
 
 
 
 
 
 
182}
183
184static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
185{
186	struct pca954x *data = i2c_mux_priv(muxc);
187	struct i2c_client *client = data->client;
188	const struct chip_desc *chip = data->chip;
189	u8 regval;
190	int ret = 0;
191
192	/* we make switches look like muxes, not sure how to be smarter */
193	if (chip->muxtype == pca954x_ismux)
194		regval = chan | chip->enable;
195	else
196		regval = 1 << chan;
197
198	/* Only select the channel if its different from the last channel */
199	if (data->last_chan != regval) {
200		ret = pca954x_reg_write(muxc->parent, client, regval);
201		data->last_chan = ret < 0 ? 0 : regval;
202	}
203
204	return ret;
205}
206
207static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
208{
209	struct pca954x *data = i2c_mux_priv(muxc);
210	struct i2c_client *client = data->client;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
212	if (!(data->deselect & (1 << chan)))
213		return 0;
214
215	/* Deselect active channel */
216	data->last_chan = 0;
217	return pca954x_reg_write(muxc->parent, client, data->last_chan);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218}
219
220/*
221 * I2C init/probing/exit functions
222 */
223static int pca954x_probe(struct i2c_client *client,
224			 const struct i2c_device_id *id)
225{
226	struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
227	struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
228	struct device_node *of_node = client->dev.of_node;
229	bool idle_disconnect_dt;
230	struct gpio_desc *gpio;
231	int num, force, class;
232	struct i2c_mux_core *muxc;
233	struct pca954x *data;
234	const struct of_device_id *match;
235	int ret;
236
237	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
238		return -ENODEV;
239
240	muxc = i2c_mux_alloc(adap, &client->dev,
241			     PCA954X_MAX_NCHANS, sizeof(*data), 0,
242			     pca954x_select_chan, pca954x_deselect_mux);
243	if (!muxc)
244		return -ENOMEM;
245	data = i2c_mux_priv(muxc);
246
247	i2c_set_clientdata(client, muxc);
248	data->client = client;
249
250	/* Get the mux out of reset if a reset GPIO is specified. */
251	gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_LOW);
252	if (IS_ERR(gpio))
253		return PTR_ERR(gpio);
254
255	/* Write the mux register at addr to verify
256	 * that the mux is in fact present. This also
257	 * initializes the mux to disconnected state.
258	 */
259	if (i2c_smbus_write_byte(client, 0) < 0) {
260		dev_warn(&client->dev, "probe failed\n");
261		return -ENODEV;
262	}
263
264	match = of_match_device(of_match_ptr(pca954x_of_match), &client->dev);
265	if (match)
266		data->chip = of_device_get_match_data(&client->dev);
267	else if (id)
268		data->chip = &chips[id->driver_data];
269	else {
270		const struct acpi_device_id *acpi_id;
271
272		acpi_id = acpi_match_device(ACPI_PTR(pca954x_acpi_ids),
273						&client->dev);
274		if (!acpi_id)
 
 
 
 
 
 
 
 
 
 
275			return -ENODEV;
276		data->chip = &chips[acpi_id->driver_data];
277	}
278
279	data->last_chan = 0;		   /* force the first selection */
 
 
 
 
280
281	idle_disconnect_dt = of_node &&
282		of_property_read_bool(of_node, "i2c-mux-idle-disconnect");
 
 
 
 
 
 
 
 
 
 
 
 
 
283
284	/* Now create an adapter for each channel */
285	for (num = 0; num < data->chip->nchans; num++) {
286		bool idle_disconnect_pd = false;
 
 
 
287
288		force = 0;			  /* dynamic adap number */
289		class = 0;			  /* no class by default */
290		if (pdata) {
291			if (num < pdata->num_modes) {
292				/* force static number */
293				force = pdata->modes[num].adap_id;
294				class = pdata->modes[num].class;
295			} else
296				/* discard unconfigured channels */
297				break;
298			idle_disconnect_pd = pdata->modes[num].deselect_on_exit;
299		}
300		data->deselect |= (idle_disconnect_pd ||
301				   idle_disconnect_dt) << num;
302
303		ret = i2c_mux_add_adapter(muxc, force, num, class);
304
305		if (ret) {
306			dev_err(&client->dev,
307				"failed to register multiplexed adapter"
308				" %d as bus %d\n", num, force);
309			goto virt_reg_failed;
310		}
311	}
312
313	dev_info(&client->dev,
314		 "registered %d multiplexed busses for I2C %s %s\n",
315		 num, data->chip->muxtype == pca954x_ismux
316				? "mux" : "switch", client->name);
317
318	return 0;
319
320virt_reg_failed:
321	i2c_mux_del_adapters(muxc);
322	return ret;
323}
324
325static int pca954x_remove(struct i2c_client *client)
326{
327	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
328
329	i2c_mux_del_adapters(muxc);
330	return 0;
 
331}
332
333#ifdef CONFIG_PM_SLEEP
334static int pca954x_resume(struct device *dev)
335{
336	struct i2c_client *client = to_i2c_client(dev);
337	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
338	struct pca954x *data = i2c_mux_priv(muxc);
 
 
 
 
 
339
340	data->last_chan = 0;
341	return i2c_smbus_write_byte(client, 0);
342}
343#endif
344
345static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
346
347static struct i2c_driver pca954x_driver = {
348	.driver		= {
349		.name	= "pca954x",
350		.pm	= &pca954x_pm,
351		.of_match_table = of_match_ptr(pca954x_of_match),
352		.acpi_match_table = ACPI_PTR(pca954x_acpi_ids),
353	},
354	.probe		= pca954x_probe,
355	.remove		= pca954x_remove,
356	.id_table	= pca954x_id,
357};
358
359module_i2c_driver(pca954x_driver);
360
361MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
362MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
363MODULE_LICENSE("GPL v2");