xref: /linux/drivers/i2c/muxes/i2c-mux-pca954x.c (revision b615879dbfea6cf1236acbc3f2fb25ae84e07071)
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/reset.h>
53 #include <linux/slab.h>
54 #include <linux/spinlock.h>
55 #include <dt-bindings/mux/mux.h>
56 
57 #define PCA954X_MAX_NCHANS 8
58 
59 #define PCA954X_IRQ_OFFSET 4
60 
61 /*
62  * MAX7357's configuration register is writeable after POR, but
63  * can be locked by setting the basic mode bit. MAX7358 configuration
64  * register is locked by default and needs to be unlocked first.
65  * The configuration register holds the following settings:
66  */
67 #define MAX7357_CONF_INT_ENABLE			BIT(0)
68 #define MAX7357_CONF_FLUSH_OUT			BIT(1)
69 #define MAX7357_CONF_RELEASE_INT		BIT(2)
70 #define MAX7357_CONF_DISCON_SINGLE_CHAN		BIT(4)
71 #define MAX7357_CONF_PRECONNECT_TEST		BIT(7)
72 
73 #define MAX7357_POR_DEFAULT_CONF		MAX7357_CONF_INT_ENABLE
74 
75 enum pca_type {
76 	max_7356,
77 	max_7357,
78 	max_7358,
79 	max_7367,
80 	max_7368,
81 	max_7369,
82 	pca_9540,
83 	pca_9542,
84 	pca_9543,
85 	pca_9544,
86 	pca_9545,
87 	pca_9546,
88 	pca_9547,
89 	pca_9548,
90 	pca_9846,
91 	pca_9847,
92 	pca_9848,
93 	pca_9849,
94 };
95 
96 struct chip_desc {
97 	u8 nchans;
98 	u8 enable;	/* used for muxes only */
99 	u8 has_irq;
100 	enum muxtype {
101 		pca954x_ismux = 0,
102 		pca954x_isswi
103 	} muxtype;
104 	struct i2c_device_identity id;
105 };
106 
107 struct pca954x {
108 	const struct chip_desc *chip;
109 
110 	u8 last_chan;		/* last register value */
111 	/* MUX_IDLE_AS_IS, MUX_IDLE_DISCONNECT or >= 0 for channel */
112 	s32 idle_state;
113 
114 	struct i2c_client *client;
115 
116 	struct irq_domain *irq;
117 	unsigned int irq_mask;
118 	raw_spinlock_t lock;
119 	struct regulator *supply;
120 
121 	struct reset_control *reset_cont;
122 };
123 
124 /* Provide specs for the MAX735x, PCA954x and PCA984x types we know about */
125 static const struct chip_desc chips[] = {
126 	[max_7356] = {
127 		.nchans = 8,
128 		.muxtype = pca954x_isswi,
129 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
130 	},
131 	[max_7357] = {
132 		.nchans = 8,
133 		.muxtype = pca954x_isswi,
134 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
135 		/*
136 		 * No interrupt controller support. The interrupt
137 		 * provides information about stuck channels.
138 		 */
139 	},
140 	[max_7358] = {
141 		.nchans = 8,
142 		.muxtype = pca954x_isswi,
143 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
144 		/*
145 		 * No interrupt controller support. The interrupt
146 		 * provides information about stuck channels.
147 		 */
148 	},
149 	[max_7367] = {
150 		.nchans = 4,
151 		.muxtype = pca954x_isswi,
152 		.has_irq = 1,
153 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
154 	},
155 	[max_7368] = {
156 		.nchans = 4,
157 		.muxtype = pca954x_isswi,
158 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
159 	},
160 	[max_7369] = {
161 		.nchans = 4,
162 		.enable = 0x4,
163 		.muxtype = pca954x_ismux,
164 		.has_irq = 1,
165 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
166 	},
167 	[pca_9540] = {
168 		.nchans = 2,
169 		.enable = 0x4,
170 		.muxtype = pca954x_ismux,
171 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
172 	},
173 	[pca_9542] = {
174 		.nchans = 2,
175 		.enable = 0x4,
176 		.has_irq = 1,
177 		.muxtype = pca954x_ismux,
178 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
179 	},
180 	[pca_9543] = {
181 		.nchans = 2,
182 		.has_irq = 1,
183 		.muxtype = pca954x_isswi,
184 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
185 	},
186 	[pca_9544] = {
187 		.nchans = 4,
188 		.enable = 0x4,
189 		.has_irq = 1,
190 		.muxtype = pca954x_ismux,
191 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
192 	},
193 	[pca_9545] = {
194 		.nchans = 4,
195 		.has_irq = 1,
196 		.muxtype = pca954x_isswi,
197 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
198 	},
199 	[pca_9546] = {
200 		.nchans = 4,
201 		.muxtype = pca954x_isswi,
202 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
203 	},
204 	[pca_9547] = {
205 		.nchans = 8,
206 		.enable = 0x8,
207 		.muxtype = pca954x_ismux,
208 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
209 	},
210 	[pca_9548] = {
211 		.nchans = 8,
212 		.muxtype = pca954x_isswi,
213 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
214 	},
215 	[pca_9846] = {
216 		.nchans = 4,
217 		.muxtype = pca954x_isswi,
218 		.id = {
219 			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
220 			.part_id = 0x10b,
221 		},
222 	},
223 	[pca_9847] = {
224 		.nchans = 8,
225 		.enable = 0x8,
226 		.muxtype = pca954x_ismux,
227 		.id = {
228 			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
229 			.part_id = 0x108,
230 		},
231 	},
232 	[pca_9848] = {
233 		.nchans = 8,
234 		.muxtype = pca954x_isswi,
235 		.id = {
236 			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
237 			.part_id = 0x10a,
238 		},
239 	},
240 	[pca_9849] = {
241 		.nchans = 4,
242 		.enable = 0x4,
243 		.muxtype = pca954x_ismux,
244 		.id = {
245 			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
246 			.part_id = 0x109,
247 		},
248 	},
249 };
250 
251 static const struct i2c_device_id pca954x_id[] = {
252 	{ "max7356", max_7356 },
253 	{ "max7357", max_7357 },
254 	{ "max7358", max_7358 },
255 	{ "max7367", max_7367 },
256 	{ "max7368", max_7368 },
257 	{ "max7369", max_7369 },
258 	{ "pca9540", pca_9540 },
259 	{ "pca9542", pca_9542 },
260 	{ "pca9543", pca_9543 },
261 	{ "pca9544", pca_9544 },
262 	{ "pca9545", pca_9545 },
263 	{ "pca9546", pca_9546 },
264 	{ "pca9547", pca_9547 },
265 	{ "pca9548", pca_9548 },
266 	{ "pca9846", pca_9846 },
267 	{ "pca9847", pca_9847 },
268 	{ "pca9848", pca_9848 },
269 	{ "pca9849", pca_9849 },
270 	{ }
271 };
272 MODULE_DEVICE_TABLE(i2c, pca954x_id);
273 
274 static const struct of_device_id pca954x_of_match[] = {
275 	{ .compatible = "maxim,max7356", .data = &chips[max_7356] },
276 	{ .compatible = "maxim,max7357", .data = &chips[max_7357] },
277 	{ .compatible = "maxim,max7358", .data = &chips[max_7358] },
278 	{ .compatible = "maxim,max7367", .data = &chips[max_7367] },
279 	{ .compatible = "maxim,max7368", .data = &chips[max_7368] },
280 	{ .compatible = "maxim,max7369", .data = &chips[max_7369] },
281 	{ .compatible = "nxp,pca9540", .data = &chips[pca_9540] },
282 	{ .compatible = "nxp,pca9542", .data = &chips[pca_9542] },
283 	{ .compatible = "nxp,pca9543", .data = &chips[pca_9543] },
284 	{ .compatible = "nxp,pca9544", .data = &chips[pca_9544] },
285 	{ .compatible = "nxp,pca9545", .data = &chips[pca_9545] },
286 	{ .compatible = "nxp,pca9546", .data = &chips[pca_9546] },
287 	{ .compatible = "nxp,pca9547", .data = &chips[pca_9547] },
288 	{ .compatible = "nxp,pca9548", .data = &chips[pca_9548] },
289 	{ .compatible = "nxp,pca9846", .data = &chips[pca_9846] },
290 	{ .compatible = "nxp,pca9847", .data = &chips[pca_9847] },
291 	{ .compatible = "nxp,pca9848", .data = &chips[pca_9848] },
292 	{ .compatible = "nxp,pca9849", .data = &chips[pca_9849] },
293 	{}
294 };
295 MODULE_DEVICE_TABLE(of, pca954x_of_match);
296 
297 /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
298    for this as they will try to lock adapter a second time */
299 static int pca954x_reg_write(struct i2c_adapter *adap,
300 			     struct i2c_client *client, u8 val)
301 {
302 	union i2c_smbus_data dummy;
303 
304 	return __i2c_smbus_xfer(adap, client->addr, client->flags,
305 				I2C_SMBUS_WRITE, val,
306 				I2C_SMBUS_BYTE, &dummy);
307 }
308 
309 static u8 pca954x_regval(struct pca954x *data, u8 chan)
310 {
311 	/* We make switches look like muxes, not sure how to be smarter. */
312 	if (data->chip->muxtype == pca954x_ismux)
313 		return chan | data->chip->enable;
314 	else
315 		return 1 << chan;
316 }
317 
318 static void pca954x_reset_assert(struct pca954x *data)
319 {
320 	if (data->reset_cont)
321 		reset_control_assert(data->reset_cont);
322 }
323 
324 static void pca954x_reset_deassert(struct pca954x *data)
325 {
326 	if (data->reset_cont)
327 		reset_control_deassert(data->reset_cont);
328 }
329 
330 static void pca954x_reset_mux(struct pca954x *data)
331 {
332 	pca954x_reset_assert(data);
333 	udelay(1);
334 	pca954x_reset_deassert(data);
335 }
336 
337 static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
338 {
339 	struct pca954x *data = i2c_mux_priv(muxc);
340 	struct i2c_client *client = data->client;
341 	u8 regval;
342 	int ret = 0;
343 
344 	regval = pca954x_regval(data, chan);
345 	/* Only select the channel if its different from the last channel */
346 	if (data->last_chan != regval) {
347 		ret = pca954x_reg_write(muxc->parent, client, regval);
348 		data->last_chan = ret < 0 ? 0 : regval;
349 	}
350 	if (ret == -ETIMEDOUT && data->reset_cont)
351 		pca954x_reset_mux(data);
352 
353 	return ret;
354 }
355 
356 static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
357 {
358 	struct pca954x *data = i2c_mux_priv(muxc);
359 	struct i2c_client *client = data->client;
360 	s32 idle_state;
361 	int ret = 0;
362 
363 	idle_state = READ_ONCE(data->idle_state);
364 	if (idle_state >= 0)
365 		/* Set the mux back to a predetermined channel */
366 		return pca954x_select_chan(muxc, idle_state);
367 
368 	if (idle_state == MUX_IDLE_DISCONNECT) {
369 		/* Deselect active channel */
370 		data->last_chan = 0;
371 		ret = pca954x_reg_write(muxc->parent, client,
372 					data->last_chan);
373 		if (ret == -ETIMEDOUT && data->reset_cont)
374 			pca954x_reset_mux(data);
375 	}
376 
377 	/* otherwise leave as-is */
378 
379 	return 0;
380 }
381 
382 static ssize_t idle_state_show(struct device *dev,
383 				    struct device_attribute *attr,
384 				    char *buf)
385 {
386 	struct i2c_client *client = to_i2c_client(dev);
387 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
388 	struct pca954x *data = i2c_mux_priv(muxc);
389 
390 	return sprintf(buf, "%d\n", READ_ONCE(data->idle_state));
391 }
392 
393 static ssize_t idle_state_store(struct device *dev,
394 				struct device_attribute *attr,
395 				const char *buf, size_t count)
396 {
397 	struct i2c_client *client = to_i2c_client(dev);
398 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
399 	struct pca954x *data = i2c_mux_priv(muxc);
400 	int val;
401 	int ret;
402 
403 	ret = kstrtoint(buf, 0, &val);
404 	if (ret < 0)
405 		return ret;
406 
407 	if (val != MUX_IDLE_AS_IS && val != MUX_IDLE_DISCONNECT &&
408 	    (val < 0 || val >= data->chip->nchans))
409 		return -EINVAL;
410 
411 	i2c_lock_bus(muxc->parent, I2C_LOCK_SEGMENT);
412 
413 	WRITE_ONCE(data->idle_state, val);
414 	/*
415 	 * Set the mux into a state consistent with the new
416 	 * idle_state.
417 	 */
418 	if (data->last_chan || val != MUX_IDLE_DISCONNECT)
419 		ret = pca954x_deselect_mux(muxc, 0);
420 
421 	i2c_unlock_bus(muxc->parent, I2C_LOCK_SEGMENT);
422 
423 	return ret < 0 ? ret : count;
424 }
425 
426 static DEVICE_ATTR_RW(idle_state);
427 
428 static irqreturn_t pca954x_irq_handler(int irq, void *dev_id)
429 {
430 	struct pca954x *data = dev_id;
431 	unsigned long pending;
432 	int ret, i;
433 
434 	ret = i2c_smbus_read_byte(data->client);
435 	if (ret < 0)
436 		return IRQ_NONE;
437 
438 	pending = (ret >> PCA954X_IRQ_OFFSET) & (BIT(data->chip->nchans) - 1);
439 	for_each_set_bit(i, &pending, data->chip->nchans)
440 		handle_nested_irq(irq_find_mapping(data->irq, i));
441 
442 	return IRQ_RETVAL(pending);
443 }
444 
445 static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type)
446 {
447 	if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW)
448 		return -EINVAL;
449 	return 0;
450 }
451 
452 static struct irq_chip pca954x_irq_chip = {
453 	.name = "i2c-mux-pca954x",
454 	.irq_set_type = pca954x_irq_set_type,
455 };
456 
457 static int pca954x_irq_setup(struct i2c_mux_core *muxc)
458 {
459 	struct pca954x *data = i2c_mux_priv(muxc);
460 	struct i2c_client *client = data->client;
461 	int c, irq;
462 
463 	if (!data->chip->has_irq || client->irq <= 0)
464 		return 0;
465 
466 	raw_spin_lock_init(&data->lock);
467 
468 	data->irq = irq_domain_create_linear(dev_fwnode(&client->dev), data->chip->nchans,
469 					     &irq_domain_simple_ops, data);
470 	if (!data->irq)
471 		return -ENODEV;
472 
473 	for (c = 0; c < data->chip->nchans; c++) {
474 		irq = irq_create_mapping(data->irq, c);
475 		if (!irq) {
476 			dev_err(&client->dev, "failed irq create map\n");
477 			return -EINVAL;
478 		}
479 		irq_set_chip_data(irq, data);
480 		irq_set_chip_and_handler(irq, &pca954x_irq_chip,
481 			handle_simple_irq);
482 	}
483 
484 	return 0;
485 }
486 
487 static void pca954x_cleanup(struct i2c_mux_core *muxc)
488 {
489 	struct pca954x *data = i2c_mux_priv(muxc);
490 	int c, irq;
491 
492 	regulator_disable(data->supply);
493 
494 	if (data->irq) {
495 		for (c = 0; c < data->chip->nchans; c++) {
496 			irq = irq_find_mapping(data->irq, c);
497 			irq_dispose_mapping(irq);
498 		}
499 		irq_domain_remove(data->irq);
500 	}
501 	i2c_mux_del_adapters(muxc);
502 }
503 
504 static int pca954x_init(struct i2c_client *client, struct pca954x *data)
505 {
506 	int ret;
507 
508 	if (data->idle_state >= 0)
509 		data->last_chan = pca954x_regval(data, data->idle_state);
510 	else
511 		data->last_chan = 0; /* Disconnect multiplexer */
512 
513 	if (device_is_compatible(&client->dev, "maxim,max7357")) {
514 		if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
515 			u8 conf = MAX7357_POR_DEFAULT_CONF;
516 			/*
517 			 * The interrupt signal is shared with the reset pin. Release the
518 			 * interrupt after 1.6 seconds to allow using the pin as reset.
519 			 */
520 			conf |= MAX7357_CONF_RELEASE_INT;
521 
522 			if (device_property_read_bool(&client->dev, "maxim,isolate-stuck-channel"))
523 				conf |= MAX7357_CONF_DISCON_SINGLE_CHAN;
524 			if (device_property_read_bool(&client->dev,
525 						      "maxim,send-flush-out-sequence"))
526 				conf |= MAX7357_CONF_FLUSH_OUT;
527 			if (device_property_read_bool(&client->dev,
528 						      "maxim,preconnection-wiggle-test-enable"))
529 				conf |= MAX7357_CONF_PRECONNECT_TEST;
530 
531 			ret = i2c_smbus_write_byte_data(client, data->last_chan, conf);
532 		} else {
533 			dev_warn(&client->dev, "Write byte data not supported."
534 				 "Cannot enable enhanced mode features\n");
535 			ret = i2c_smbus_write_byte(client, data->last_chan);
536 		}
537 	} else {
538 		ret = i2c_smbus_write_byte(client, data->last_chan);
539 	}
540 
541 	if (ret < 0)
542 		data->last_chan = 0;
543 
544 	return ret;
545 }
546 
547 static int pca954x_get_reset(struct device *dev, struct pca954x *data)
548 {
549 	data->reset_cont = devm_reset_control_get_optional_shared(dev, NULL);
550 	if (IS_ERR(data->reset_cont))
551 		return dev_err_probe(dev, PTR_ERR(data->reset_cont),
552 				     "Failed to get reset\n");
553 
554 	return 0;
555 }
556 
557 /*
558  * I2C init/probing/exit functions
559  */
560 static int pca954x_probe(struct i2c_client *client)
561 {
562 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
563 	struct i2c_adapter *adap = client->adapter;
564 	struct device *dev = &client->dev;
565 	struct i2c_mux_core *muxc;
566 	struct pca954x *data;
567 	int num;
568 	int ret;
569 
570 	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
571 		return -ENODEV;
572 
573 	muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0,
574 			     pca954x_select_chan, pca954x_deselect_mux);
575 	if (!muxc)
576 		return -ENOMEM;
577 	data = i2c_mux_priv(muxc);
578 
579 	i2c_set_clientdata(client, muxc);
580 	data->client = client;
581 
582 	data->supply = devm_regulator_get(dev, "vdd");
583 	if (IS_ERR(data->supply))
584 		return dev_err_probe(dev, PTR_ERR(data->supply),
585 				"Failed to request regulator\n");
586 
587 	ret = regulator_enable(data->supply);
588 	if (ret)
589 		return dev_err_probe(dev, ret,
590 				     "Failed to enable vdd supply\n");
591 
592 	ret = pca954x_get_reset(dev, data);
593 	if (ret)
594 		goto fail_cleanup;
595 
596 	if (data->reset_cont) {
597 		udelay(1);
598 		pca954x_reset_deassert(data);
599 		/* Give the chip some time to recover. */
600 		udelay(1);
601 	}
602 
603 	data->chip = device_get_match_data(dev);
604 	if (!data->chip)
605 		data->chip = &chips[id->driver_data];
606 
607 	if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) {
608 		struct i2c_device_identity id;
609 
610 		ret = i2c_get_device_id(client, &id);
611 		if (ret && ret != -EOPNOTSUPP)
612 			goto fail_cleanup;
613 
614 		if (!ret &&
615 		    (id.manufacturer_id != data->chip->id.manufacturer_id ||
616 		     id.part_id != data->chip->id.part_id)) {
617 			dev_warn(dev, "unexpected device id %03x-%03x-%x\n",
618 				 id.manufacturer_id, id.part_id,
619 				 id.die_revision);
620 			ret = -ENODEV;
621 			goto fail_cleanup;
622 		}
623 	}
624 
625 	data->idle_state = MUX_IDLE_AS_IS;
626 	if (device_property_read_u32(dev, "idle-state", &data->idle_state)) {
627 		if (device_property_read_bool(dev, "i2c-mux-idle-disconnect"))
628 			data->idle_state = MUX_IDLE_DISCONNECT;
629 	}
630 
631 	/*
632 	 * Write the mux register at addr to verify
633 	 * that the mux is in fact present. This also
634 	 * initializes the mux to a channel
635 	 * or disconnected state.
636 	 */
637 	ret = pca954x_init(client, data);
638 	if (ret < 0) {
639 		dev_warn(dev, "probe failed\n");
640 		ret = -ENODEV;
641 		goto fail_cleanup;
642 	}
643 
644 	ret = pca954x_irq_setup(muxc);
645 	if (ret)
646 		goto fail_cleanup;
647 
648 	/* Now create an adapter for each channel */
649 	for (num = 0; num < data->chip->nchans; num++) {
650 		ret = i2c_mux_add_adapter(muxc, 0, num);
651 		if (ret)
652 			goto fail_cleanup;
653 	}
654 
655 	if (data->irq) {
656 		ret = devm_request_threaded_irq(dev, data->client->irq,
657 						NULL, pca954x_irq_handler,
658 						IRQF_ONESHOT | IRQF_SHARED,
659 						"pca954x", data);
660 		if (ret)
661 			goto fail_cleanup;
662 	}
663 
664 	/*
665 	 * The attr probably isn't going to be needed in most cases,
666 	 * so don't fail completely on error.
667 	 */
668 	device_create_file(dev, &dev_attr_idle_state);
669 
670 	dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n",
671 		 num, data->chip->muxtype == pca954x_ismux
672 				? "mux" : "switch", client->name);
673 
674 	return 0;
675 
676 fail_cleanup:
677 	pca954x_cleanup(muxc);
678 	return ret;
679 }
680 
681 static void pca954x_remove(struct i2c_client *client)
682 {
683 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
684 
685 	device_remove_file(&client->dev, &dev_attr_idle_state);
686 
687 	pca954x_cleanup(muxc);
688 }
689 
690 static int pca954x_resume(struct device *dev)
691 {
692 	struct i2c_client *client = to_i2c_client(dev);
693 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
694 	struct pca954x *data = i2c_mux_priv(muxc);
695 	int ret;
696 
697 	ret = pca954x_init(client, data);
698 	if (ret < 0)
699 		dev_err(&client->dev, "failed to verify mux presence\n");
700 
701 	return ret;
702 }
703 
704 static DEFINE_SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
705 
706 static struct i2c_driver pca954x_driver = {
707 	.driver		= {
708 		.name	= "pca954x",
709 		.pm	= pm_sleep_ptr(&pca954x_pm),
710 		.of_match_table = pca954x_of_match,
711 	},
712 	.probe		= pca954x_probe,
713 	.remove		= pca954x_remove,
714 	.id_table	= pca954x_id,
715 };
716 
717 module_i2c_driver(pca954x_driver);
718 
719 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
720 MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
721 MODULE_LICENSE("GPL v2");
722