i2c-mux-pca9541.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * I2C multiplexer driver for PCA9541 bus master selector
  3. *
  4. * Copyright (c) 2010 Ericsson AB.
  5. *
  6. * Author: Guenter Roeck <linux@roeck-us.net>
  7. *
  8. * Derived from:
  9. * pca954x.c
  10. *
  11. * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
  12. * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
  13. *
  14. * This file is licensed under the terms of the GNU General Public
  15. * License version 2. This program is licensed "as is" without any
  16. * warranty of any kind, whether express or implied.
  17. */
  18. #include <linux/delay.h>
  19. #include <linux/device.h>
  20. #include <linux/i2c.h>
  21. #include <linux/i2c-mux.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_data/pca954x.h>
  25. #include <linux/slab.h>
  26. /*
  27. * The PCA9541 is a bus master selector. It supports two I2C masters connected
  28. * to a single slave bus.
  29. *
  30. * Before each bus transaction, a master has to acquire bus ownership. After the
  31. * transaction is complete, bus ownership has to be released. This fits well
  32. * into the I2C multiplexer framework, which provides select and release
  33. * functions for this purpose. For this reason, this driver is modeled as
  34. * single-channel I2C bus multiplexer.
  35. *
  36. * This driver assumes that the two bus masters are controlled by two different
  37. * hosts. If a single host controls both masters, platform code has to ensure
  38. * that only one of the masters is instantiated at any given time.
  39. */
  40. #define PCA9541_CONTROL 0x01
  41. #define PCA9541_ISTAT 0x02
  42. #define PCA9541_CTL_MYBUS (1 << 0)
  43. #define PCA9541_CTL_NMYBUS (1 << 1)
  44. #define PCA9541_CTL_BUSON (1 << 2)
  45. #define PCA9541_CTL_NBUSON (1 << 3)
  46. #define PCA9541_CTL_BUSINIT (1 << 4)
  47. #define PCA9541_CTL_TESTON (1 << 6)
  48. #define PCA9541_CTL_NTESTON (1 << 7)
  49. #define PCA9541_ISTAT_INTIN (1 << 0)
  50. #define PCA9541_ISTAT_BUSINIT (1 << 1)
  51. #define PCA9541_ISTAT_BUSOK (1 << 2)
  52. #define PCA9541_ISTAT_BUSLOST (1 << 3)
  53. #define PCA9541_ISTAT_MYTEST (1 << 6)
  54. #define PCA9541_ISTAT_NMYTEST (1 << 7)
  55. #define BUSON (PCA9541_CTL_BUSON | PCA9541_CTL_NBUSON)
  56. #define MYBUS (PCA9541_CTL_MYBUS | PCA9541_CTL_NMYBUS)
  57. #define mybus(x) (!((x) & MYBUS) || ((x) & MYBUS) == MYBUS)
  58. #define busoff(x) (!((x) & BUSON) || ((x) & BUSON) == BUSON)
  59. /* arbitration timeouts, in jiffies */
  60. #define ARB_TIMEOUT (HZ / 8) /* 125 ms until forcing bus ownership */
  61. #define ARB2_TIMEOUT (HZ / 4) /* 250 ms until acquisition failure */
  62. /* arbitration retry delays, in us */
  63. #define SELECT_DELAY_SHORT 50
  64. #define SELECT_DELAY_LONG 1000
  65. struct pca9541 {
  66. struct i2c_client *client;
  67. unsigned long select_timeout;
  68. unsigned long arb_timeout;
  69. };
  70. static const struct i2c_device_id pca9541_id[] = {
  71. {"pca9541", 0},
  72. {}
  73. };
  74. MODULE_DEVICE_TABLE(i2c, pca9541_id);
  75. #ifdef CONFIG_OF
  76. static const struct of_device_id pca9541_of_match[] = {
  77. { .compatible = "nxp,pca9541" },
  78. {}
  79. };
  80. MODULE_DEVICE_TABLE(of, pca9541_of_match);
  81. #endif
  82. /*
  83. * Write to chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
  84. * as they will try to lock the adapter a second time.
  85. */
  86. static int pca9541_reg_write(struct i2c_client *client, u8 command, u8 val)
  87. {
  88. struct i2c_adapter *adap = client->adapter;
  89. union i2c_smbus_data data = { .byte = val };
  90. return __i2c_smbus_xfer(adap, client->addr, client->flags,
  91. I2C_SMBUS_WRITE, command,
  92. I2C_SMBUS_BYTE_DATA, &data);
  93. }
  94. /*
  95. * Read from chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
  96. * as they will try to lock adapter a second time.
  97. */
  98. static int pca9541_reg_read(struct i2c_client *client, u8 command)
  99. {
  100. struct i2c_adapter *adap = client->adapter;
  101. union i2c_smbus_data data;
  102. int ret;
  103. ret = __i2c_smbus_xfer(adap, client->addr, client->flags,
  104. I2C_SMBUS_READ, command,
  105. I2C_SMBUS_BYTE_DATA, &data);
  106. return ret ?: data.byte;
  107. }
  108. /*
  109. * Arbitration management functions
  110. */
  111. /* Release bus. Also reset NTESTON and BUSINIT if it was set. */
  112. static void pca9541_release_bus(struct i2c_client *client)
  113. {
  114. int reg;
  115. reg = pca9541_reg_read(client, PCA9541_CONTROL);
  116. if (reg >= 0 && !busoff(reg) && mybus(reg))
  117. pca9541_reg_write(client, PCA9541_CONTROL,
  118. (reg & PCA9541_CTL_NBUSON) >> 1);
  119. }
  120. /*
  121. * Arbitration is defined as a two-step process. A bus master can only activate
  122. * the slave bus if it owns it; otherwise it has to request ownership first.
  123. * This multi-step process ensures that access contention is resolved
  124. * gracefully.
  125. *
  126. * Bus Ownership Other master Action
  127. * state requested access
  128. * ----------------------------------------------------
  129. * off - yes wait for arbitration timeout or
  130. * for other master to drop request
  131. * off no no take ownership
  132. * off yes no turn on bus
  133. * on yes - done
  134. * on no - wait for arbitration timeout or
  135. * for other master to release bus
  136. *
  137. * The main contention point occurs if the slave bus is off and both masters
  138. * request ownership at the same time. In this case, one master will turn on
  139. * the slave bus, believing that it owns it. The other master will request
  140. * bus ownership. Result is that the bus is turned on, and master which did
  141. * _not_ own the slave bus before ends up owning it.
  142. */
  143. /* Control commands per PCA9541 datasheet */
  144. static const u8 pca9541_control[16] = {
  145. 4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1
  146. };
  147. /*
  148. * Channel arbitration
  149. *
  150. * Return values:
  151. * <0: error
  152. * 0 : bus not acquired
  153. * 1 : bus acquired
  154. */
  155. static int pca9541_arbitrate(struct i2c_client *client)
  156. {
  157. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  158. struct pca9541 *data = i2c_mux_priv(muxc);
  159. int reg;
  160. reg = pca9541_reg_read(client, PCA9541_CONTROL);
  161. if (reg < 0)
  162. return reg;
  163. if (busoff(reg)) {
  164. int istat;
  165. /*
  166. * Bus is off. Request ownership or turn it on unless
  167. * other master requested ownership.
  168. */
  169. istat = pca9541_reg_read(client, PCA9541_ISTAT);
  170. if (!(istat & PCA9541_ISTAT_NMYTEST)
  171. || time_is_before_eq_jiffies(data->arb_timeout)) {
  172. /*
  173. * Other master did not request ownership,
  174. * or arbitration timeout expired. Take the bus.
  175. */
  176. pca9541_reg_write(client,
  177. PCA9541_CONTROL,
  178. pca9541_control[reg & 0x0f]
  179. | PCA9541_CTL_NTESTON);
  180. data->select_timeout = SELECT_DELAY_SHORT;
  181. } else {
  182. /*
  183. * Other master requested ownership.
  184. * Set extra long timeout to give it time to acquire it.
  185. */
  186. data->select_timeout = SELECT_DELAY_LONG * 2;
  187. }
  188. } else if (mybus(reg)) {
  189. /*
  190. * Bus is on, and we own it. We are done with acquisition.
  191. * Reset NTESTON and BUSINIT, then return success.
  192. */
  193. if (reg & (PCA9541_CTL_NTESTON | PCA9541_CTL_BUSINIT))
  194. pca9541_reg_write(client,
  195. PCA9541_CONTROL,
  196. reg & ~(PCA9541_CTL_NTESTON
  197. | PCA9541_CTL_BUSINIT));
  198. return 1;
  199. } else {
  200. /*
  201. * Other master owns the bus.
  202. * If arbitration timeout has expired, force ownership.
  203. * Otherwise request it.
  204. */
  205. data->select_timeout = SELECT_DELAY_LONG;
  206. if (time_is_before_eq_jiffies(data->arb_timeout)) {
  207. /* Time is up, take the bus and reset it. */
  208. pca9541_reg_write(client,
  209. PCA9541_CONTROL,
  210. pca9541_control[reg & 0x0f]
  211. | PCA9541_CTL_BUSINIT
  212. | PCA9541_CTL_NTESTON);
  213. } else {
  214. /* Request bus ownership if needed */
  215. if (!(reg & PCA9541_CTL_NTESTON))
  216. pca9541_reg_write(client,
  217. PCA9541_CONTROL,
  218. reg | PCA9541_CTL_NTESTON);
  219. }
  220. }
  221. return 0;
  222. }
  223. static int pca9541_select_chan(struct i2c_mux_core *muxc, u32 chan)
  224. {
  225. struct pca9541 *data = i2c_mux_priv(muxc);
  226. struct i2c_client *client = data->client;
  227. int ret;
  228. unsigned long timeout = jiffies + ARB2_TIMEOUT;
  229. /* give up after this time */
  230. data->arb_timeout = jiffies + ARB_TIMEOUT;
  231. /* force bus ownership after this time */
  232. do {
  233. ret = pca9541_arbitrate(client);
  234. if (ret)
  235. return ret < 0 ? ret : 0;
  236. if (data->select_timeout == SELECT_DELAY_SHORT)
  237. udelay(data->select_timeout);
  238. else
  239. msleep(data->select_timeout / 1000);
  240. } while (time_is_after_eq_jiffies(timeout));
  241. return -ETIMEDOUT;
  242. }
  243. static int pca9541_release_chan(struct i2c_mux_core *muxc, u32 chan)
  244. {
  245. struct pca9541 *data = i2c_mux_priv(muxc);
  246. struct i2c_client *client = data->client;
  247. pca9541_release_bus(client);
  248. return 0;
  249. }
  250. /*
  251. * I2C init/probing/exit functions
  252. */
  253. static int pca9541_probe(struct i2c_client *client,
  254. const struct i2c_device_id *id)
  255. {
  256. struct i2c_adapter *adap = client->adapter;
  257. struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
  258. struct i2c_mux_core *muxc;
  259. struct pca9541 *data;
  260. int force;
  261. int ret;
  262. if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE_DATA))
  263. return -ENODEV;
  264. /*
  265. * I2C accesses are unprotected here.
  266. * We have to lock the I2C segment before releasing the bus.
  267. */
  268. i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
  269. pca9541_release_bus(client);
  270. i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
  271. /* Create mux adapter */
  272. force = 0;
  273. if (pdata)
  274. force = pdata->modes[0].adap_id;
  275. muxc = i2c_mux_alloc(adap, &client->dev, 1, sizeof(*data),
  276. I2C_MUX_ARBITRATOR,
  277. pca9541_select_chan, pca9541_release_chan);
  278. if (!muxc)
  279. return -ENOMEM;
  280. data = i2c_mux_priv(muxc);
  281. data->client = client;
  282. i2c_set_clientdata(client, muxc);
  283. ret = i2c_mux_add_adapter(muxc, force, 0, 0);
  284. if (ret)
  285. return ret;
  286. dev_info(&client->dev, "registered master selector for I2C %s\n",
  287. client->name);
  288. return 0;
  289. }
  290. static int pca9541_remove(struct i2c_client *client)
  291. {
  292. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  293. i2c_mux_del_adapters(muxc);
  294. return 0;
  295. }
  296. static struct i2c_driver pca9541_driver = {
  297. .driver = {
  298. .name = "pca9541",
  299. .of_match_table = of_match_ptr(pca9541_of_match),
  300. },
  301. .probe = pca9541_probe,
  302. .remove = pca9541_remove,
  303. .id_table = pca9541_id,
  304. };
  305. module_i2c_driver(pca9541_driver);
  306. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  307. MODULE_DESCRIPTION("PCA9541 I2C master selector driver");
  308. MODULE_LICENSE("GPL v2");