Linux 4.19.133
[linux/fpc-iii.git] / drivers / i2c / muxes / i2c-mux-pca954x.c
blob24bd9275fde5fc8775407c56b80825aa1d4c1aaf
1 /*
2 * I2C multiplexer
4 * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
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.
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.
20 * Based on:
21 * pca954x.c from Kumar Gala <galak@kernel.crashing.org>
22 * Copyright (C) 2006
24 * Based on:
25 * pca954x.c from Ken Harrenstien
26 * Copyright (C) 2004 Google, Inc. (Ken Harrenstien)
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>.
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.
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/platform_data/pca954x.h>
50 #include <linux/pm.h>
51 #include <linux/slab.h>
52 #include <linux/spinlock.h>
54 #define PCA954X_MAX_NCHANS 8
56 #define PCA954X_IRQ_OFFSET 4
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,
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;
84 struct pca954x {
85 const struct chip_desc *chip;
87 u8 last_chan; /* last register value */
88 u8 deselect;
89 struct i2c_client *client;
91 struct irq_domain *irq;
92 unsigned int irq_mask;
93 raw_spinlock_t lock;
96 /* Provide specs for the PCA954x types we know about */
97 static const struct chip_desc chips[] = {
98 [pca_9540] = {
99 .nchans = 2,
100 .enable = 0x4,
101 .muxtype = pca954x_ismux,
102 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
104 [pca_9542] = {
105 .nchans = 2,
106 .enable = 0x4,
107 .has_irq = 1,
108 .muxtype = pca954x_ismux,
109 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
111 [pca_9543] = {
112 .nchans = 2,
113 .has_irq = 1,
114 .muxtype = pca954x_isswi,
115 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
117 [pca_9544] = {
118 .nchans = 4,
119 .enable = 0x4,
120 .has_irq = 1,
121 .muxtype = pca954x_ismux,
122 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
124 [pca_9545] = {
125 .nchans = 4,
126 .has_irq = 1,
127 .muxtype = pca954x_isswi,
128 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
130 [pca_9546] = {
131 .nchans = 4,
132 .muxtype = pca954x_isswi,
133 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
135 [pca_9547] = {
136 .nchans = 8,
137 .enable = 0x8,
138 .muxtype = pca954x_ismux,
139 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
141 [pca_9548] = {
142 .nchans = 8,
143 .muxtype = pca954x_isswi,
144 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE },
146 [pca_9846] = {
147 .nchans = 4,
148 .muxtype = pca954x_isswi,
149 .id = {
150 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
151 .part_id = 0x10b,
154 [pca_9847] = {
155 .nchans = 8,
156 .enable = 0x8,
157 .muxtype = pca954x_ismux,
158 .id = {
159 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
160 .part_id = 0x108,
163 [pca_9848] = {
164 .nchans = 8,
165 .muxtype = pca954x_isswi,
166 .id = {
167 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
168 .part_id = 0x10a,
171 [pca_9849] = {
172 .nchans = 4,
173 .enable = 0x4,
174 .muxtype = pca954x_ismux,
175 .id = {
176 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS,
177 .part_id = 0x109,
182 static const struct i2c_device_id pca954x_id[] = {
183 { "pca9540", pca_9540 },
184 { "pca9542", pca_9542 },
185 { "pca9543", pca_9543 },
186 { "pca9544", pca_9544 },
187 { "pca9545", pca_9545 },
188 { "pca9546", pca_9546 },
189 { "pca9547", pca_9547 },
190 { "pca9548", pca_9548 },
191 { "pca9846", pca_9846 },
192 { "pca9847", pca_9847 },
193 { "pca9848", pca_9848 },
194 { "pca9849", pca_9849 },
197 MODULE_DEVICE_TABLE(i2c, pca954x_id);
199 #ifdef CONFIG_OF
200 static const struct of_device_id pca954x_of_match[] = {
201 { .compatible = "nxp,pca9540", .data = &chips[pca_9540] },
202 { .compatible = "nxp,pca9542", .data = &chips[pca_9542] },
203 { .compatible = "nxp,pca9543", .data = &chips[pca_9543] },
204 { .compatible = "nxp,pca9544", .data = &chips[pca_9544] },
205 { .compatible = "nxp,pca9545", .data = &chips[pca_9545] },
206 { .compatible = "nxp,pca9546", .data = &chips[pca_9546] },
207 { .compatible = "nxp,pca9547", .data = &chips[pca_9547] },
208 { .compatible = "nxp,pca9548", .data = &chips[pca_9548] },
209 { .compatible = "nxp,pca9846", .data = &chips[pca_9846] },
210 { .compatible = "nxp,pca9847", .data = &chips[pca_9847] },
211 { .compatible = "nxp,pca9848", .data = &chips[pca_9848] },
212 { .compatible = "nxp,pca9849", .data = &chips[pca_9849] },
215 MODULE_DEVICE_TABLE(of, pca954x_of_match);
216 #endif
218 /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
219 for this as they will try to lock adapter a second time */
220 static int pca954x_reg_write(struct i2c_adapter *adap,
221 struct i2c_client *client, u8 val)
223 union i2c_smbus_data dummy;
225 return __i2c_smbus_xfer(adap, client->addr, client->flags,
226 I2C_SMBUS_WRITE, val,
227 I2C_SMBUS_BYTE, &dummy);
230 static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
232 struct pca954x *data = i2c_mux_priv(muxc);
233 struct i2c_client *client = data->client;
234 const struct chip_desc *chip = data->chip;
235 u8 regval;
236 int ret = 0;
238 /* we make switches look like muxes, not sure how to be smarter */
239 if (chip->muxtype == pca954x_ismux)
240 regval = chan | chip->enable;
241 else
242 regval = 1 << chan;
244 /* Only select the channel if its different from the last channel */
245 if (data->last_chan != regval) {
246 ret = pca954x_reg_write(muxc->parent, client, regval);
247 data->last_chan = ret < 0 ? 0 : regval;
250 return ret;
253 static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
255 struct pca954x *data = i2c_mux_priv(muxc);
256 struct i2c_client *client = data->client;
258 if (!(data->deselect & (1 << chan)))
259 return 0;
261 /* Deselect active channel */
262 data->last_chan = 0;
263 return pca954x_reg_write(muxc->parent, client, data->last_chan);
266 static irqreturn_t pca954x_irq_handler(int irq, void *dev_id)
268 struct pca954x *data = dev_id;
269 unsigned int child_irq;
270 int ret, i, handled = 0;
272 ret = i2c_smbus_read_byte(data->client);
273 if (ret < 0)
274 return IRQ_NONE;
276 for (i = 0; i < data->chip->nchans; i++) {
277 if (ret & BIT(PCA954X_IRQ_OFFSET + i)) {
278 child_irq = irq_linear_revmap(data->irq, i);
279 handle_nested_irq(child_irq);
280 handled++;
283 return handled ? IRQ_HANDLED : IRQ_NONE;
286 static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type)
288 if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW)
289 return -EINVAL;
290 return 0;
293 static struct irq_chip pca954x_irq_chip = {
294 .name = "i2c-mux-pca954x",
295 .irq_set_type = pca954x_irq_set_type,
298 static int pca954x_irq_setup(struct i2c_mux_core *muxc)
300 struct pca954x *data = i2c_mux_priv(muxc);
301 struct i2c_client *client = data->client;
302 int c, irq;
304 if (!data->chip->has_irq || client->irq <= 0)
305 return 0;
307 raw_spin_lock_init(&data->lock);
309 data->irq = irq_domain_add_linear(client->dev.of_node,
310 data->chip->nchans,
311 &irq_domain_simple_ops, data);
312 if (!data->irq)
313 return -ENODEV;
315 for (c = 0; c < data->chip->nchans; c++) {
316 irq = irq_create_mapping(data->irq, c);
317 if (!irq) {
318 dev_err(&client->dev, "failed irq create map\n");
319 return -EINVAL;
321 irq_set_chip_data(irq, data);
322 irq_set_chip_and_handler(irq, &pca954x_irq_chip,
323 handle_simple_irq);
326 return 0;
329 static void pca954x_cleanup(struct i2c_mux_core *muxc)
331 struct pca954x *data = i2c_mux_priv(muxc);
332 int c, irq;
334 if (data->irq) {
335 for (c = 0; c < data->chip->nchans; c++) {
336 irq = irq_find_mapping(data->irq, c);
337 irq_dispose_mapping(irq);
339 irq_domain_remove(data->irq);
341 i2c_mux_del_adapters(muxc);
345 * I2C init/probing/exit functions
347 static int pca954x_probe(struct i2c_client *client,
348 const struct i2c_device_id *id)
350 struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
351 struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
352 struct device *dev = &client->dev;
353 struct device_node *np = dev->of_node;
354 bool idle_disconnect_dt;
355 struct gpio_desc *gpio;
356 int num, force, class;
357 struct i2c_mux_core *muxc;
358 struct pca954x *data;
359 int ret;
361 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
362 return -ENODEV;
364 muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0,
365 pca954x_select_chan, pca954x_deselect_mux);
366 if (!muxc)
367 return -ENOMEM;
368 data = i2c_mux_priv(muxc);
370 i2c_set_clientdata(client, muxc);
371 data->client = client;
373 /* Reset the mux if a reset GPIO is specified. */
374 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
375 if (IS_ERR(gpio))
376 return PTR_ERR(gpio);
377 if (gpio) {
378 udelay(1);
379 gpiod_set_value_cansleep(gpio, 0);
380 /* Give the chip some time to recover. */
381 udelay(1);
384 data->chip = of_device_get_match_data(dev);
385 if (!data->chip)
386 data->chip = &chips[id->driver_data];
388 if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) {
389 struct i2c_device_identity id;
391 ret = i2c_get_device_id(client, &id);
392 if (ret && ret != -EOPNOTSUPP)
393 return ret;
395 if (!ret &&
396 (id.manufacturer_id != data->chip->id.manufacturer_id ||
397 id.part_id != data->chip->id.part_id)) {
398 dev_warn(dev, "unexpected device id %03x-%03x-%x\n",
399 id.manufacturer_id, id.part_id,
400 id.die_revision);
401 return -ENODEV;
405 /* Write the mux register at addr to verify
406 * that the mux is in fact present. This also
407 * initializes the mux to disconnected state.
409 if (i2c_smbus_write_byte(client, 0) < 0) {
410 dev_warn(dev, "probe failed\n");
411 return -ENODEV;
414 data->last_chan = 0; /* force the first selection */
416 idle_disconnect_dt = np &&
417 of_property_read_bool(np, "i2c-mux-idle-disconnect");
419 ret = pca954x_irq_setup(muxc);
420 if (ret)
421 goto fail_cleanup;
423 /* Now create an adapter for each channel */
424 for (num = 0; num < data->chip->nchans; num++) {
425 bool idle_disconnect_pd = false;
427 force = 0; /* dynamic adap number */
428 class = 0; /* no class by default */
429 if (pdata) {
430 if (num < pdata->num_modes) {
431 /* force static number */
432 force = pdata->modes[num].adap_id;
433 class = pdata->modes[num].class;
434 } else
435 /* discard unconfigured channels */
436 break;
437 idle_disconnect_pd = pdata->modes[num].deselect_on_exit;
439 data->deselect |= (idle_disconnect_pd ||
440 idle_disconnect_dt) << num;
442 ret = i2c_mux_add_adapter(muxc, force, num, class);
443 if (ret)
444 goto fail_cleanup;
447 if (data->irq) {
448 ret = devm_request_threaded_irq(dev, data->client->irq,
449 NULL, pca954x_irq_handler,
450 IRQF_ONESHOT | IRQF_SHARED,
451 "pca954x", data);
452 if (ret)
453 goto fail_cleanup;
456 dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n",
457 num, data->chip->muxtype == pca954x_ismux
458 ? "mux" : "switch", client->name);
460 return 0;
462 fail_cleanup:
463 pca954x_cleanup(muxc);
464 return ret;
467 static int pca954x_remove(struct i2c_client *client)
469 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
471 pca954x_cleanup(muxc);
472 return 0;
475 #ifdef CONFIG_PM_SLEEP
476 static int pca954x_resume(struct device *dev)
478 struct i2c_client *client = to_i2c_client(dev);
479 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
480 struct pca954x *data = i2c_mux_priv(muxc);
482 data->last_chan = 0;
483 return i2c_smbus_write_byte(client, 0);
485 #endif
487 static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
489 static struct i2c_driver pca954x_driver = {
490 .driver = {
491 .name = "pca954x",
492 .pm = &pca954x_pm,
493 .of_match_table = of_match_ptr(pca954x_of_match),
495 .probe = pca954x_probe,
496 .remove = pca954x_remove,
497 .id_table = pca954x_id,
500 module_i2c_driver(pca954x_driver);
502 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
503 MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
504 MODULE_LICENSE("GPL v2");