i2c-mux-pca954x.c 7.3 KB

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