i2c-mux-pca954x.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/i2c/pca954x.h>
  42. #include <linux/module.h>
  43. #include <linux/of.h>
  44. #include <linux/of_device.h>
  45. #include <linux/pm.h>
  46. #include <linux/slab.h>
  47. #define PCA954X_MAX_NCHANS 8
  48. enum pca_type {
  49. pca_9540,
  50. pca_9542,
  51. pca_9543,
  52. pca_9544,
  53. pca_9545,
  54. pca_9546,
  55. pca_9547,
  56. pca_9548,
  57. };
  58. struct chip_desc {
  59. u8 nchans;
  60. u8 enable; /* used for muxes only */
  61. enum muxtype {
  62. pca954x_ismux = 0,
  63. pca954x_isswi
  64. } muxtype;
  65. };
  66. struct pca954x {
  67. const struct chip_desc *chip;
  68. u8 last_chan; /* last register value */
  69. u8 deselect;
  70. struct i2c_client *client;
  71. };
  72. /* Provide specs for the PCA954x types we know about */
  73. static const struct chip_desc chips[] = {
  74. [pca_9540] = {
  75. .nchans = 2,
  76. .enable = 0x4,
  77. .muxtype = pca954x_ismux,
  78. },
  79. [pca_9543] = {
  80. .nchans = 2,
  81. .muxtype = pca954x_isswi,
  82. },
  83. [pca_9544] = {
  84. .nchans = 4,
  85. .enable = 0x4,
  86. .muxtype = pca954x_ismux,
  87. },
  88. [pca_9545] = {
  89. .nchans = 4,
  90. .muxtype = pca954x_isswi,
  91. },
  92. [pca_9547] = {
  93. .nchans = 8,
  94. .enable = 0x8,
  95. .muxtype = pca954x_ismux,
  96. },
  97. [pca_9548] = {
  98. .nchans = 8,
  99. .muxtype = pca954x_isswi,
  100. },
  101. };
  102. static const struct i2c_device_id pca954x_id[] = {
  103. { "pca9540", pca_9540 },
  104. { "pca9542", pca_9540 },
  105. { "pca9543", pca_9543 },
  106. { "pca9544", pca_9544 },
  107. { "pca9545", pca_9545 },
  108. { "pca9546", pca_9545 },
  109. { "pca9547", pca_9547 },
  110. { "pca9548", pca_9548 },
  111. { }
  112. };
  113. MODULE_DEVICE_TABLE(i2c, pca954x_id);
  114. #ifdef CONFIG_OF
  115. static const struct of_device_id pca954x_of_match[] = {
  116. { .compatible = "nxp,pca9540", .data = &chips[pca_9540] },
  117. { .compatible = "nxp,pca9542", .data = &chips[pca_9542] },
  118. { .compatible = "nxp,pca9543", .data = &chips[pca_9543] },
  119. { .compatible = "nxp,pca9544", .data = &chips[pca_9544] },
  120. { .compatible = "nxp,pca9545", .data = &chips[pca_9545] },
  121. { .compatible = "nxp,pca9546", .data = &chips[pca_9546] },
  122. { .compatible = "nxp,pca9547", .data = &chips[pca_9547] },
  123. { .compatible = "nxp,pca9548", .data = &chips[pca_9548] },
  124. {}
  125. };
  126. #endif
  127. /* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
  128. for this as they will try to lock adapter a second time */
  129. static int pca954x_reg_write(struct i2c_adapter *adap,
  130. struct i2c_client *client, u8 val)
  131. {
  132. int ret = -ENODEV;
  133. if (adap->algo->master_xfer) {
  134. struct i2c_msg msg;
  135. char buf[1];
  136. msg.addr = client->addr;
  137. msg.flags = 0;
  138. msg.len = 1;
  139. buf[0] = val;
  140. msg.buf = buf;
  141. ret = __i2c_transfer(adap, &msg, 1);
  142. if (ret >= 0 && ret != 1)
  143. ret = -EREMOTEIO;
  144. } else {
  145. union i2c_smbus_data data;
  146. ret = adap->algo->smbus_xfer(adap, client->addr,
  147. client->flags,
  148. I2C_SMBUS_WRITE,
  149. val, I2C_SMBUS_BYTE, &data);
  150. }
  151. return ret;
  152. }
  153. static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
  154. {
  155. struct pca954x *data = i2c_mux_priv(muxc);
  156. struct i2c_client *client = data->client;
  157. const struct chip_desc *chip = data->chip;
  158. u8 regval;
  159. int ret = 0;
  160. /* we make switches look like muxes, not sure how to be smarter */
  161. if (chip->muxtype == pca954x_ismux)
  162. regval = chan | chip->enable;
  163. else
  164. regval = 1 << chan;
  165. /* Only select the channel if its different from the last channel */
  166. if (data->last_chan != regval) {
  167. ret = pca954x_reg_write(muxc->parent, client, regval);
  168. data->last_chan = ret < 0 ? 0 : regval;
  169. }
  170. return ret;
  171. }
  172. static int pca954x_deselect_mux(struct i2c_mux_core *muxc, u32 chan)
  173. {
  174. struct pca954x *data = i2c_mux_priv(muxc);
  175. struct i2c_client *client = data->client;
  176. if (!(data->deselect & (1 << chan)))
  177. return 0;
  178. /* Deselect active channel */
  179. data->last_chan = 0;
  180. return pca954x_reg_write(muxc->parent, client, data->last_chan);
  181. }
  182. /*
  183. * I2C init/probing/exit functions
  184. */
  185. static int pca954x_probe(struct i2c_client *client,
  186. const struct i2c_device_id *id)
  187. {
  188. struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
  189. struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
  190. struct device_node *of_node = client->dev.of_node;
  191. bool idle_disconnect_dt;
  192. struct gpio_desc *gpio;
  193. int num, force, class;
  194. struct i2c_mux_core *muxc;
  195. struct pca954x *data;
  196. const struct of_device_id *match;
  197. int ret;
  198. if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
  199. return -ENODEV;
  200. muxc = i2c_mux_alloc(adap, &client->dev,
  201. PCA954X_MAX_NCHANS, sizeof(*data), 0,
  202. pca954x_select_chan, pca954x_deselect_mux);
  203. if (!muxc)
  204. return -ENOMEM;
  205. data = i2c_mux_priv(muxc);
  206. i2c_set_clientdata(client, muxc);
  207. data->client = client;
  208. /* Get the mux out of reset if a reset GPIO is specified. */
  209. gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_LOW);
  210. if (IS_ERR(gpio))
  211. return PTR_ERR(gpio);
  212. /* Write the mux register at addr to verify
  213. * that the mux is in fact present. This also
  214. * initializes the mux to disconnected state.
  215. */
  216. if (i2c_smbus_write_byte(client, 0) < 0) {
  217. dev_warn(&client->dev, "probe failed\n");
  218. return -ENODEV;
  219. }
  220. match = of_match_device(of_match_ptr(pca954x_of_match), &client->dev);
  221. if (match)
  222. data->chip = of_device_get_match_data(&client->dev);
  223. else
  224. data->chip = &chips[id->driver_data];
  225. data->last_chan = 0; /* force the first selection */
  226. idle_disconnect_dt = of_node &&
  227. of_property_read_bool(of_node, "i2c-mux-idle-disconnect");
  228. /* Now create an adapter for each channel */
  229. for (num = 0; num < data->chip->nchans; num++) {
  230. bool idle_disconnect_pd = false;
  231. force = 0; /* dynamic adap number */
  232. class = 0; /* no class by default */
  233. if (pdata) {
  234. if (num < pdata->num_modes) {
  235. /* force static number */
  236. force = pdata->modes[num].adap_id;
  237. class = pdata->modes[num].class;
  238. } else
  239. /* discard unconfigured channels */
  240. break;
  241. idle_disconnect_pd = pdata->modes[num].deselect_on_exit;
  242. }
  243. data->deselect |= (idle_disconnect_pd ||
  244. idle_disconnect_dt) << num;
  245. ret = i2c_mux_add_adapter(muxc, force, num, class);
  246. if (ret) {
  247. dev_err(&client->dev,
  248. "failed to register multiplexed adapter"
  249. " %d as bus %d\n", num, force);
  250. goto virt_reg_failed;
  251. }
  252. }
  253. dev_info(&client->dev,
  254. "registered %d multiplexed busses for I2C %s %s\n",
  255. num, data->chip->muxtype == pca954x_ismux
  256. ? "mux" : "switch", client->name);
  257. return 0;
  258. virt_reg_failed:
  259. i2c_mux_del_adapters(muxc);
  260. return ret;
  261. }
  262. static int pca954x_remove(struct i2c_client *client)
  263. {
  264. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  265. i2c_mux_del_adapters(muxc);
  266. return 0;
  267. }
  268. #ifdef CONFIG_PM_SLEEP
  269. static int pca954x_resume(struct device *dev)
  270. {
  271. struct i2c_client *client = to_i2c_client(dev);
  272. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  273. struct pca954x *data = i2c_mux_priv(muxc);
  274. data->last_chan = 0;
  275. return i2c_smbus_write_byte(client, 0);
  276. }
  277. #endif
  278. static SIMPLE_DEV_PM_OPS(pca954x_pm, NULL, pca954x_resume);
  279. static struct i2c_driver pca954x_driver = {
  280. .driver = {
  281. .name = "pca954x",
  282. .pm = &pca954x_pm,
  283. .of_match_table = of_match_ptr(pca954x_of_match),
  284. },
  285. .probe = pca954x_probe,
  286. .remove = pca954x_remove,
  287. .id_table = pca954x_id,
  288. };
  289. module_i2c_driver(pca954x_driver);
  290. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  291. MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
  292. MODULE_LICENSE("GPL v2");