pca954x.c 6.9 KB

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