i2c-mux-pca954x.c 9.1 KB

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