i2c-mux-pca954x.c 7.5 KB

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