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 and PCA954x series of I2C multiplexer/switch
8  * chips made by NXP Semiconductors.
9  * This includes the:
10  *	 PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547,
11  *	 PCA9548, PCA9846, PCA9847, PCA9848 and PCA9849.
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/delay.h>
40 #include <linux/gpio/consumer.h>
41 #include <linux/i2c.h>
42 #include <linux/i2c-mux.h>
43 #include <linux/interrupt.h>
44 #include <linux/irq.h>
45 #include <linux/module.h>
46 #include <linux/of.h>
47 #include <linux/of_device.h>
48 #include <linux/of_irq.h>
49 #include <linux/pm.h>
50 #include <linux/slab.h>
51 #include <linux/spinlock.h>
52 #include <dt-bindings/mux/mux.h>
53 
54 #define PCA954X_MAX_NCHANS 8
55 
56 #define PCA954X_IRQ_OFFSET 4
57 
58 enum pca_type {
59 	pca_9540,
60 	pca_9542,
61 	pca_9543,
62 	pca_9544,
63 	pca_9545,
64 	pca_9546,
65 	pca_9547,
66 	pca_9548,
67 	pca_9846,
68 	pca_9847,
69 	pca_9848,
70 	pca_9849,
71 };
72 
73 struct chip_desc {
74 	u8 nchans;
75 	u8 enable;	/* used for muxes only */
76 	u8 has_irq;
77 	enum muxtype {
78 		pca954x_ismux = 0,
79 		pca954x_isswi
80 	} muxtype;
81 	struct i2c_device_identity id;
82 };
83 
84 struct pca954x {
85 	const struct chip_desc *chip;
86 
87 	u8 last_chan;		/* last register value */
88 	/* MUX_IDLE_AS_IS, MUX_IDLE_DISCONNECT or >= 0 for channel */
89 	s32 idle_state;
90 
91 	struct i2c_client *client;
92 
93 	struct irq_domain *irq;
94 	unsigned int irq_mask;
95 	raw_spinlock_t lock;
96 };
97 
98 /* Provide specs for the PCA954x types we know about */
99 static const struct chip_desc chips[] = {
100 	[pca_9540] = {
101 		.nchans = 2,
102 		.enable = 0x4,
103 		.muxtype = pca954x_ismux,
104 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
105 	},
106 	[pca_9542] = {
107 		.nchans = 2,
108 		.enable = 0x4,
109 		.has_irq = 1,
110 		.muxtype = pca954x_ismux,
111 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
112 	},
113 	[pca_9543] = {
114 		.nchans = 2,
115 		.has_irq = 1,
116 		.muxtype = pca954x_isswi,
117 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
118 	},
119 	[pca_9544] = {
120 		.nchans = 4,
121 		.enable = 0x4,
122 		.has_irq = 1,
123 		.muxtype = pca954x_ismux,
124 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
125 	},
126 	[pca_9545] = {
127 		.nchans = 4,
128 		.has_irq = 1,
129 		.muxtype = pca954x_isswi,
130 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
131 	},
132 	[pca_9546] = {
133 		.nchans = 4,
134 		.muxtype = pca954x_isswi,
135 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
136 	},
137 	[pca_9547] = {
138 		.nchans = 8,
139 		.enable = 0x8,
140 		.muxtype = pca954x_ismux,
141 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
142 	},
143 	[pca_9548] = {
144 		.nchans = 8,
145 		.muxtype = pca954x_isswi,
146 		.id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
147 	},
148 	[pca_9846] = {
149 		.nchans = 4,
150 		.muxtype = pca954x_isswi,
151 		.id = {
152 			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
153 			.part_id = 0x10b,
154 		},
155 	},
156 	[pca_9847] = {
157 		.nchans = 8,
158 		.enable = 0x8,
159 		.muxtype = pca954x_ismux,
160 		.id = {
161 			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
162 			.part_id = 0x108,
163 		},
164 	},
165 	[pca_9848] = {
166 		.nchans = 8,
167 		.muxtype = pca954x_isswi,
168 		.id = {
169 			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
170 			.part_id = 0x10a,
171 		},
172 	},
173 	[pca_9849] = {
174 		.nchans = 4,
175 		.enable = 0x4,
176 		.muxtype = pca954x_ismux,
177 		.id = {
178 			.manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
179 			.part_id = 0x109,
180 		},
181 	},
182 };
183 
184 static const struct i2c_device_id pca954x_id[] = {
185 	{ "pca9540", pca_9540 },
186 	{ "pca9542", pca_9542 },
187 	{ "pca9543", pca_9543 },
188 	{ "pca9544", pca_9544 },
189 	{ "pca9545", pca_9545 },
190 	{ "pca9546", pca_9546 },
191 	{ "pca9547", pca_9547 },
192 	{ "pca9548", pca_9548 },
193 	{ "pca9846", pca_9846 },
194 	{ "pca9847", pca_9847 },
195 	{ "pca9848", pca_9848 },
196 	{ "pca9849", pca_9849 },
197 	{ }
198 };
199 MODULE_DEVICE_TABLE(i2c, pca954x_id);
200 
201 #ifdef CONFIG_OF
202 static const struct of_device_id pca954x_of_match[] = {
203 	{ .compatible = "nxp,pca9540", .data = &chips[pca_9540] },
204 	{ .compatible = "nxp,pca9542", .data = &chips[pca_9542] },
205 	{ .compatible = "nxp,pca9543", .data = &chips[pca_9543] },
206 	{ .compatible = "nxp,pca9544", .data = &chips[pca_9544] },
207 	{ .compatible = "nxp,pca9545", .data = &chips[pca_9545] },
208 	{ .compatible = "nxp,pca9546", .data = &chips[pca_9546] },
209 	{ .compatible = "nxp,pca9547", .data = &chips[pca_9547] },
210 	{ .compatible = "nxp,pca9548", .data = &chips[pca_9548] },
211 	{ .compatible = "nxp,pca9846", .data = &chips[pca_9846] },
212 	{ .compatible = "nxp,pca9847", .data = &chips[pca_9847] },
213 	{ .compatible = "nxp,pca9848", .data = &chips[pca_9848] },
214 	{ .compatible = "nxp,pca9849", .data = &chips[pca_9849] },
215 	{}
216 };
217 MODULE_DEVICE_TABLE(of, pca954x_of_match);
218 #endif
219 
220 /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
221    for this as they will try to lock adapter a second time */
222 static int pca954x_reg_write(struct i2c_adapter *adap,
223 			     struct i2c_client *client, u8 val)
224 {
225 	union i2c_smbus_data dummy;
226 
227 	return __i2c_smbus_xfer(adap, client->addr, client->flags,
228 				I2C_SMBUS_WRITE, val,
229 				I2C_SMBUS_BYTE, &dummy);
230 }
231 
232 static u8 pca954x_regval(struct pca954x *data, u8 chan)
233 {
234 	/* We make switches look like muxes, not sure how to be smarter. */
235 	if (data->chip->muxtype == pca954x_ismux)
236 		return chan | data->chip->enable;
237 	else
238 		return 1 << chan;
239 }
240 
241 static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
242 {
243 	struct pca954x *data = i2c_mux_priv(muxc);
244 	struct i2c_client *client = data->client;
245 	u8 regval;
246 	int ret = 0;
247 
248 	regval = pca954x_regval(data, chan);
249 	/* Only select the channel if its different from the last channel */
250 	if (data->last_chan != regval) {
251 		ret = pca954x_reg_write(muxc->parent, client, regval);
252 		data->last_chan = ret < 0 ? 0 : regval;
253 	}
254 
255 	return ret;
256 }
257 
258 static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
259 {
260 	struct pca954x *data = i2c_mux_priv(muxc);
261 	struct i2c_client *client = data->client;
262 	s32 idle_state;
263 
264 	idle_state = READ_ONCE(data->idle_state);
265 	if (idle_state >= 0)
266 		/* Set the mux back to a predetermined channel */
267 		return pca954x_select_chan(muxc, idle_state);
268 
269 	if (idle_state == MUX_IDLE_DISCONNECT) {
270 		/* Deselect active channel */
271 		data->last_chan = 0;
272 		return pca954x_reg_write(muxc->parent, client,
273 					 data->last_chan);
274 	}
275 
276 	/* otherwise leave as-is */
277 
278 	return 0;
279 }
280 
281 static ssize_t idle_state_show(struct device *dev,
282 				    struct device_attribute *attr,
283 				    char *buf)
284 {
285 	struct i2c_client *client = to_i2c_client(dev);
286 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
287 	struct pca954x *data = i2c_mux_priv(muxc);
288 
289 	return sprintf(buf, "%d\n", READ_ONCE(data->idle_state));
290 }
291 
292 static ssize_t idle_state_store(struct device *dev,
293 				struct device_attribute *attr,
294 				const char *buf, size_t count)
295 {
296 	struct i2c_client *client = to_i2c_client(dev);
297 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
298 	struct pca954x *data = i2c_mux_priv(muxc);
299 	int val;
300 	int ret;
301 
302 	ret = kstrtoint(buf, 0, &val);
303 	if (ret < 0)
304 		return ret;
305 
306 	if (val != MUX_IDLE_AS_IS && val != MUX_IDLE_DISCONNECT &&
307 	    (val < 0 || val >= data->chip->nchans))
308 		return -EINVAL;
309 
310 	i2c_lock_bus(muxc->parent, I2C_LOCK_SEGMENT);
311 
312 	WRITE_ONCE(data->idle_state, val);
313 	/*
314 	 * Set the mux into a state consistent with the new
315 	 * idle_state.
316 	 */
317 	if (data->last_chan || val != MUX_IDLE_DISCONNECT)
318 		ret = pca954x_deselect_mux(muxc, 0);
319 
320 	i2c_unlock_bus(muxc->parent, I2C_LOCK_SEGMENT);
321 
322 	return ret < 0 ? ret : count;
323 }
324 
325 static DEVICE_ATTR_RW(idle_state);
326 
327 static irqreturn_t pca954x_irq_handler(int irq, void *dev_id)
328 {
329 	struct pca954x *data = dev_id;
330 	unsigned int child_irq;
331 	int ret, i, handled = 0;
332 
333 	ret = i2c_smbus_read_byte(data->client);
334 	if (ret < 0)
335 		return IRQ_NONE;
336 
337 	for (i = 0; i < data->chip->nchans; i++) {
338 		if (ret & BIT(PCA954X_IRQ_OFFSET + i)) {
339 			child_irq = irq_linear_revmap(data->irq, i);
340 			handle_nested_irq(child_irq);
341 			handled++;
342 		}
343 	}
344 	return handled ? IRQ_HANDLED : IRQ_NONE;
345 }
346 
347 static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type)
348 {
349 	if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW)
350 		return -EINVAL;
351 	return 0;
352 }
353 
354 static struct irq_chip pca954x_irq_chip = {
355 	.name = "i2c-mux-pca954x",
356 	.irq_set_type = pca954x_irq_set_type,
357 };
358 
359 static int pca954x_irq_setup(struct i2c_mux_core *muxc)
360 {
361 	struct pca954x *data = i2c_mux_priv(muxc);
362 	struct i2c_client *client = data->client;
363 	int c, irq;
364 
365 	if (!data->chip->has_irq || client->irq <= 0)
366 		return 0;
367 
368 	raw_spin_lock_init(&data->lock);
369 
370 	data->irq = irq_domain_add_linear(client->dev.of_node,
371 					  data->chip->nchans,
372 					  &irq_domain_simple_ops, data);
373 	if (!data->irq)
374 		return -ENODEV;
375 
376 	for (c = 0; c < data->chip->nchans; c++) {
377 		irq = irq_create_mapping(data->irq, c);
378 		if (!irq) {
379 			dev_err(&client->dev, "failed irq create map\n");
380 			return -EINVAL;
381 		}
382 		irq_set_chip_data(irq, data);
383 		irq_set_chip_and_handler(irq, &pca954x_irq_chip,
384 			handle_simple_irq);
385 	}
386 
387 	return 0;
388 }
389 
390 static void pca954x_cleanup(struct i2c_mux_core *muxc)
391 {
392 	struct pca954x *data = i2c_mux_priv(muxc);
393 	struct i2c_client *client = data->client;
394 	int c, irq;
395 
396 	device_remove_file(&client->dev, &dev_attr_idle_state);
397 
398 	if (data->irq) {
399 		for (c = 0; c < data->chip->nchans; c++) {
400 			irq = irq_find_mapping(data->irq, c);
401 			irq_dispose_mapping(irq);
402 		}
403 		irq_domain_remove(data->irq);
404 	}
405 	i2c_mux_del_adapters(muxc);
406 }
407 
408 static int pca954x_init(struct i2c_client *client, struct pca954x *data)
409 {
410 	int ret;
411 
412 	if (data->idle_state >= 0)
413 		data->last_chan = pca954x_regval(data, data->idle_state);
414 	else
415 		data->last_chan = 0; /* Disconnect multiplexer */
416 
417 	ret = i2c_smbus_write_byte(client, data->last_chan);
418 	if (ret < 0)
419 		data->last_chan = 0;
420 
421 	return ret;
422 }
423 
424 /*
425  * I2C init/probing/exit functions
426  */
427 static int pca954x_probe(struct i2c_client *client,
428 			 const struct i2c_device_id *id)
429 {
430 	struct i2c_adapter *adap = client->adapter;
431 	struct device *dev = &client->dev;
432 	struct device_node *np = dev->of_node;
433 	struct gpio_desc *gpio;
434 	struct i2c_mux_core *muxc;
435 	struct pca954x *data;
436 	int num;
437 	int ret;
438 
439 	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
440 		return -ENODEV;
441 
442 	muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0,
443 			     pca954x_select_chan, pca954x_deselect_mux);
444 	if (!muxc)
445 		return -ENOMEM;
446 	data = i2c_mux_priv(muxc);
447 
448 	i2c_set_clientdata(client, muxc);
449 	data->client = client;
450 
451 	/* Reset the mux if a reset GPIO is specified. */
452 	gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
453 	if (IS_ERR(gpio))
454 		return PTR_ERR(gpio);
455 	if (gpio) {
456 		udelay(1);
457 		gpiod_set_value_cansleep(gpio, 0);
458 		/* Give the chip some time to recover. */
459 		udelay(1);
460 	}
461 
462 	data->chip = of_device_get_match_data(dev);
463 	if (!data->chip)
464 		data->chip = &chips[id->driver_data];
465 
466 	if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) {
467 		struct i2c_device_identity id;
468 
469 		ret = i2c_get_device_id(client, &id);
470 		if (ret && ret != -EOPNOTSUPP)
471 			return ret;
472 
473 		if (!ret &&
474 		    (id.manufacturer_id != data->chip->id.manufacturer_id ||
475 		     id.part_id != data->chip->id.part_id)) {
476 			dev_warn(dev, "unexpected device id %03x-%03x-%x\n",
477 				 id.manufacturer_id, id.part_id,
478 				 id.die_revision);
479 			return -ENODEV;
480 		}
481 	}
482 
483 	data->idle_state = MUX_IDLE_AS_IS;
484 	if (of_property_read_u32(np, "idle-state", &data->idle_state)) {
485 		if (np && of_property_read_bool(np, "i2c-mux-idle-disconnect"))
486 			data->idle_state = MUX_IDLE_DISCONNECT;
487 	}
488 
489 	/*
490 	 * Write the mux register at addr to verify
491 	 * that the mux is in fact present. This also
492 	 * initializes the mux to a channel
493 	 * or disconnected state.
494 	 */
495 	ret = pca954x_init(client, data);
496 	if (ret < 0) {
497 		dev_warn(dev, "probe failed\n");
498 		return -ENODEV;
499 	}
500 
501 	ret = pca954x_irq_setup(muxc);
502 	if (ret)
503 		goto fail_cleanup;
504 
505 	/* Now create an adapter for each channel */
506 	for (num = 0; num < data->chip->nchans; num++) {
507 		ret = i2c_mux_add_adapter(muxc, 0, num, 0);
508 		if (ret)
509 			goto fail_cleanup;
510 	}
511 
512 	if (data->irq) {
513 		ret = devm_request_threaded_irq(dev, data->client->irq,
514 						NULL, pca954x_irq_handler,
515 						IRQF_ONESHOT | IRQF_SHARED,
516 						"pca954x", data);
517 		if (ret)
518 			goto fail_cleanup;
519 	}
520 
521 	/*
522 	 * The attr probably isn't going to be needed in most cases,
523 	 * so don't fail completely on error.
524 	 */
525 	device_create_file(dev, &dev_attr_idle_state);
526 
527 	dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n",
528 		 num, data->chip->muxtype == pca954x_ismux
529 				? "mux" : "switch", client->name);
530 
531 	return 0;
532 
533 fail_cleanup:
534 	pca954x_cleanup(muxc);
535 	return ret;
536 }
537 
538 static int pca954x_remove(struct i2c_client *client)
539 {
540 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
541 
542 	pca954x_cleanup(muxc);
543 	return 0;
544 }
545 
546 #ifdef CONFIG_PM_SLEEP
547 static int pca954x_resume(struct device *dev)
548 {
549 	struct i2c_client *client = to_i2c_client(dev);
550 	struct i2c_mux_core *muxc = i2c_get_clientdata(client);
551 	struct pca954x *data = i2c_mux_priv(muxc);
552 	int ret;
553 
554 	ret = pca954x_init(client, data);
555 	if (ret < 0)
556 		dev_err(&client->dev, "failed to verify mux presence\n");
557 
558 	return ret;
559 }
560 #endif
561 
562 static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
563 
564 static struct i2c_driver pca954x_driver = {
565 	.driver		= {
566 		.name	= "pca954x",
567 		.pm	= &pca954x_pm,
568 		.of_match_table = of_match_ptr(pca954x_of_match),
569 	},
570 	.probe		= pca954x_probe,
571 	.remove		= pca954x_remove,
572 	.id_table	= pca954x_id,
573 };
574 
575 module_i2c_driver(pca954x_driver);
576 
577 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
578 MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
579 MODULE_LICENSE("GPL v2");
580