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