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/platform_data/pca954x.h> 50 #include <linux/pm.h> 51 #include <linux/slab.h> 52 #include <linux/spinlock.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 u8 deselect; 89 struct i2c_client *client; 90 91 struct irq_domain *irq; 92 unsigned int irq_mask; 93 raw_spinlock_t lock; 94 }; 95 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 }, 103 }, 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 }, 110 }, 111 [pca_9543] = { 112 .nchans = 2, 113 .has_irq = 1, 114 .muxtype = pca954x_isswi, 115 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 116 }, 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 }, 123 }, 124 [pca_9545] = { 125 .nchans = 4, 126 .has_irq = 1, 127 .muxtype = pca954x_isswi, 128 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 129 }, 130 [pca_9546] = { 131 .nchans = 4, 132 .muxtype = pca954x_isswi, 133 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 134 }, 135 [pca_9547] = { 136 .nchans = 8, 137 .enable = 0x8, 138 .muxtype = pca954x_ismux, 139 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 140 }, 141 [pca_9548] = { 142 .nchans = 8, 143 .muxtype = pca954x_isswi, 144 .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 145 }, 146 [pca_9846] = { 147 .nchans = 4, 148 .muxtype = pca954x_isswi, 149 .id = { 150 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, 151 .part_id = 0x10b, 152 }, 153 }, 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, 161 }, 162 }, 163 [pca_9848] = { 164 .nchans = 8, 165 .muxtype = pca954x_isswi, 166 .id = { 167 .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, 168 .part_id = 0x10a, 169 }, 170 }, 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, 178 }, 179 }, 180 }; 181 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 }, 195 { } 196 }; 197 MODULE_DEVICE_TABLE(i2c, pca954x_id); 198 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] }, 213 {} 214 }; 215 MODULE_DEVICE_TABLE(of, pca954x_of_match); 216 #endif 217 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) 222 { 223 int ret = -ENODEV; 224 225 if (adap->algo->master_xfer) { 226 struct i2c_msg msg; 227 char buf[1]; 228 229 msg.addr = client->addr; 230 msg.flags = 0; 231 msg.len = 1; 232 buf[0] = val; 233 msg.buf = buf; 234 ret = __i2c_transfer(adap, &msg, 1); 235 236 if (ret >= 0 && ret != 1) 237 ret = -EREMOTEIO; 238 } else { 239 union i2c_smbus_data data; 240 ret = adap->algo->smbus_xfer(adap, client->addr, 241 client->flags, 242 I2C_SMBUS_WRITE, 243 val, I2C_SMBUS_BYTE, &data); 244 } 245 246 return ret; 247 } 248 249 static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan) 250 { 251 struct pca954x *data = i2c_mux_priv(muxc); 252 struct i2c_client *client = data->client; 253 const struct chip_desc *chip = data->chip; 254 u8 regval; 255 int ret = 0; 256 257 /* we make switches look like muxes, not sure how to be smarter */ 258 if (chip->muxtype == pca954x_ismux) 259 regval = chan | chip->enable; 260 else 261 regval = 1 << chan; 262 263 /* Only select the channel if its different from the last channel */ 264 if (data->last_chan != regval) { 265 ret = pca954x_reg_write(muxc->parent, client, regval); 266 data->last_chan = ret < 0 ? 0 : regval; 267 } 268 269 return ret; 270 } 271 272 static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan) 273 { 274 struct pca954x *data = i2c_mux_priv(muxc); 275 struct i2c_client *client = data->client; 276 277 if (!(data->deselect & (1 << chan))) 278 return 0; 279 280 /* Deselect active channel */ 281 data->last_chan = 0; 282 return pca954x_reg_write(muxc->parent, client, data->last_chan); 283 } 284 285 static irqreturn_t pca954x_irq_handler(int irq, void *dev_id) 286 { 287 struct pca954x *data = dev_id; 288 unsigned int child_irq; 289 int ret, i, handled = 0; 290 291 ret = i2c_smbus_read_byte(data->client); 292 if (ret < 0) 293 return IRQ_NONE; 294 295 for (i = 0; i < data->chip->nchans; i++) { 296 if (ret & BIT(PCA954X_IRQ_OFFSET + i)) { 297 child_irq = irq_linear_revmap(data->irq, i); 298 handle_nested_irq(child_irq); 299 handled++; 300 } 301 } 302 return handled ? IRQ_HANDLED : IRQ_NONE; 303 } 304 305 static int pca954x_irq_set_type(struct irq_data *idata, unsigned int type) 306 { 307 if ((type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_LOW) 308 return -EINVAL; 309 return 0; 310 } 311 312 static struct irq_chip pca954x_irq_chip = { 313 .name = "i2c-mux-pca954x", 314 .irq_set_type = pca954x_irq_set_type, 315 }; 316 317 static int pca954x_irq_setup(struct i2c_mux_core *muxc) 318 { 319 struct pca954x *data = i2c_mux_priv(muxc); 320 struct i2c_client *client = data->client; 321 int c, irq; 322 323 if (!data->chip->has_irq || client->irq <= 0) 324 return 0; 325 326 raw_spin_lock_init(&data->lock); 327 328 data->irq = irq_domain_add_linear(client->dev.of_node, 329 data->chip->nchans, 330 &irq_domain_simple_ops, data); 331 if (!data->irq) 332 return -ENODEV; 333 334 for (c = 0; c < data->chip->nchans; c++) { 335 irq = irq_create_mapping(data->irq, c); 336 if (!irq) { 337 dev_err(&client->dev, "failed irq create map\n"); 338 return -EINVAL; 339 } 340 irq_set_chip_data(irq, data); 341 irq_set_chip_and_handler(irq, &pca954x_irq_chip, 342 handle_simple_irq); 343 } 344 345 return 0; 346 } 347 348 static void pca954x_cleanup(struct i2c_mux_core *muxc) 349 { 350 struct pca954x *data = i2c_mux_priv(muxc); 351 int c, irq; 352 353 if (data->irq) { 354 for (c = 0; c < data->chip->nchans; c++) { 355 irq = irq_find_mapping(data->irq, c); 356 irq_dispose_mapping(irq); 357 } 358 irq_domain_remove(data->irq); 359 } 360 i2c_mux_del_adapters(muxc); 361 } 362 363 /* 364 * I2C init/probing/exit functions 365 */ 366 static int pca954x_probe(struct i2c_client *client, 367 const struct i2c_device_id *id) 368 { 369 struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent); 370 struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev); 371 struct device_node *of_node = client->dev.of_node; 372 bool idle_disconnect_dt; 373 struct gpio_desc *gpio; 374 int num, force, class; 375 struct i2c_mux_core *muxc; 376 struct pca954x *data; 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 /* Reset the mux if a reset GPIO is specified. */ 393 gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH); 394 if (IS_ERR(gpio)) 395 return PTR_ERR(gpio); 396 if (gpio) { 397 udelay(1); 398 gpiod_set_value_cansleep(gpio, 0); 399 /* Give the chip some time to recover. */ 400 udelay(1); 401 } 402 403 data->chip = of_device_get_match_data(&client->dev); 404 if (!data->chip) 405 data->chip = &chips[id->driver_data]; 406 407 if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) { 408 struct i2c_device_identity id; 409 410 ret = i2c_get_device_id(client, &id); 411 if (ret && ret != -EOPNOTSUPP) 412 return ret; 413 414 if (!ret && 415 (id.manufacturer_id != data->chip->id.manufacturer_id || 416 id.part_id != data->chip->id.part_id)) { 417 dev_warn(&client->dev, 418 "unexpected device id %03x-%03x-%x\n", 419 id.manufacturer_id, id.part_id, 420 id.die_revision); 421 return -ENODEV; 422 } 423 } 424 425 /* Write the mux register at addr to verify 426 * that the mux is in fact present. This also 427 * initializes the mux to disconnected state. 428 */ 429 if (i2c_smbus_write_byte(client, 0) < 0) { 430 dev_warn(&client->dev, "probe failed\n"); 431 return -ENODEV; 432 } 433 434 data->last_chan = 0; /* force the first selection */ 435 436 idle_disconnect_dt = of_node && 437 of_property_read_bool(of_node, "i2c-mux-idle-disconnect"); 438 439 ret = pca954x_irq_setup(muxc); 440 if (ret) 441 goto fail_cleanup; 442 443 /* Now create an adapter for each channel */ 444 for (num = 0; num < data->chip->nchans; num++) { 445 bool idle_disconnect_pd = false; 446 447 force = 0; /* dynamic adap number */ 448 class = 0; /* no class by default */ 449 if (pdata) { 450 if (num < pdata->num_modes) { 451 /* force static number */ 452 force = pdata->modes[num].adap_id; 453 class = pdata->modes[num].class; 454 } else 455 /* discard unconfigured channels */ 456 break; 457 idle_disconnect_pd = pdata->modes[num].deselect_on_exit; 458 } 459 data->deselect |= (idle_disconnect_pd || 460 idle_disconnect_dt) << num; 461 462 ret = i2c_mux_add_adapter(muxc, force, num, class); 463 if (ret) 464 goto fail_cleanup; 465 } 466 467 if (data->irq) { 468 ret = devm_request_threaded_irq(&client->dev, data->client->irq, 469 NULL, pca954x_irq_handler, 470 IRQF_ONESHOT | IRQF_SHARED, 471 "pca954x", data); 472 if (ret) 473 goto fail_cleanup; 474 } 475 476 dev_info(&client->dev, 477 "registered %d multiplexed busses for I2C %s %s\n", 478 num, data->chip->muxtype == pca954x_ismux 479 ? "mux" : "switch", client->name); 480 481 return 0; 482 483 fail_cleanup: 484 pca954x_cleanup(muxc); 485 return ret; 486 } 487 488 static int pca954x_remove(struct i2c_client *client) 489 { 490 struct i2c_mux_core *muxc = i2c_get_clientdata(client); 491 492 pca954x_cleanup(muxc); 493 return 0; 494 } 495 496 #ifdef CONFIG_PM_SLEEP 497 static int pca954x_resume(struct device *dev) 498 { 499 struct i2c_client *client = to_i2c_client(dev); 500 struct i2c_mux_core *muxc = i2c_get_clientdata(client); 501 struct pca954x *data = i2c_mux_priv(muxc); 502 503 data->last_chan = 0; 504 return i2c_smbus_write_byte(client, 0); 505 } 506 #endif 507 508 static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume); 509 510 static struct i2c_driver pca954x_driver = { 511 .driver = { 512 .name = "pca954x", 513 .pm = &pca954x_pm, 514 .of_match_table = of_match_ptr(pca954x_of_match), 515 }, 516 .probe = pca954x_probe, 517 .remove = pca954x_remove, 518 .id_table = pca954x_id, 519 }; 520 521 module_i2c_driver(pca954x_driver); 522 523 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); 524 MODULE_DESCRIPTION("PCA954x I2C mux/switch driver"); 525 MODULE_LICENSE("GPL v2"); 526