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