i2c-mux-pca954x.c 7.5 KB

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