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