i2c-mux-pca954x.c 11 KB

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