i2c-mux-pca954x.c 12 KB

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