i2c-mux-pca9541.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. int ret;
  90. if (adap->algo->master_xfer) {
  91. struct i2c_msg msg;
  92. char buf[2];
  93. msg.addr = client->addr;
  94. msg.flags = 0;
  95. msg.len = 2;
  96. buf[0] = command;
  97. buf[1] = val;
  98. msg.buf = buf;
  99. ret = __i2c_transfer(adap, &msg, 1);
  100. } else {
  101. union i2c_smbus_data data;
  102. data.byte = val;
  103. ret = adap->algo->smbus_xfer(adap, client->addr,
  104. client->flags,
  105. I2C_SMBUS_WRITE,
  106. command,
  107. I2C_SMBUS_BYTE_DATA, &data);
  108. }
  109. return ret;
  110. }
  111. /*
  112. * Read from chip register. Don't use i2c_transfer()/i2c_smbus_xfer()
  113. * as they will try to lock adapter a second time.
  114. */
  115. static int pca9541_reg_read(struct i2c_client *client, u8 command)
  116. {
  117. struct i2c_adapter *adap = client->adapter;
  118. int ret;
  119. u8 val;
  120. if (adap->algo->master_xfer) {
  121. struct i2c_msg msg[2] = {
  122. {
  123. .addr = client->addr,
  124. .flags = 0,
  125. .len = 1,
  126. .buf = &command
  127. },
  128. {
  129. .addr = client->addr,
  130. .flags = I2C_M_RD,
  131. .len = 1,
  132. .buf = &val
  133. }
  134. };
  135. ret = __i2c_transfer(adap, msg, 2);
  136. if (ret == 2)
  137. ret = val;
  138. else if (ret >= 0)
  139. ret = -EIO;
  140. } else {
  141. union i2c_smbus_data data;
  142. ret = adap->algo->smbus_xfer(adap, client->addr,
  143. client->flags,
  144. I2C_SMBUS_READ,
  145. command,
  146. I2C_SMBUS_BYTE_DATA, &data);
  147. if (!ret)
  148. ret = data.byte;
  149. }
  150. return ret;
  151. }
  152. /*
  153. * Arbitration management functions
  154. */
  155. /* Release bus. Also reset NTESTON and BUSINIT if it was set. */
  156. static void pca9541_release_bus(struct i2c_client *client)
  157. {
  158. int reg;
  159. reg = pca9541_reg_read(client, PCA9541_CONTROL);
  160. if (reg >= 0 && !busoff(reg) && mybus(reg))
  161. pca9541_reg_write(client, PCA9541_CONTROL,
  162. (reg & PCA9541_CTL_NBUSON) >> 1);
  163. }
  164. /*
  165. * Arbitration is defined as a two-step process. A bus master can only activate
  166. * the slave bus if it owns it; otherwise it has to request ownership first.
  167. * This multi-step process ensures that access contention is resolved
  168. * gracefully.
  169. *
  170. * Bus Ownership Other master Action
  171. * state requested access
  172. * ----------------------------------------------------
  173. * off - yes wait for arbitration timeout or
  174. * for other master to drop request
  175. * off no no take ownership
  176. * off yes no turn on bus
  177. * on yes - done
  178. * on no - wait for arbitration timeout or
  179. * for other master to release bus
  180. *
  181. * The main contention point occurs if the slave bus is off and both masters
  182. * request ownership at the same time. In this case, one master will turn on
  183. * the slave bus, believing that it owns it. The other master will request
  184. * bus ownership. Result is that the bus is turned on, and master which did
  185. * _not_ own the slave bus before ends up owning it.
  186. */
  187. /* Control commands per PCA9541 datasheet */
  188. static const u8 pca9541_control[16] = {
  189. 4, 0, 1, 5, 4, 4, 5, 5, 0, 0, 1, 1, 0, 4, 5, 1
  190. };
  191. /*
  192. * Channel arbitration
  193. *
  194. * Return values:
  195. * <0: error
  196. * 0 : bus not acquired
  197. * 1 : bus acquired
  198. */
  199. static int pca9541_arbitrate(struct i2c_client *client)
  200. {
  201. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  202. struct pca9541 *data = i2c_mux_priv(muxc);
  203. int reg;
  204. reg = pca9541_reg_read(client, PCA9541_CONTROL);
  205. if (reg < 0)
  206. return reg;
  207. if (busoff(reg)) {
  208. int istat;
  209. /*
  210. * Bus is off. Request ownership or turn it on unless
  211. * other master requested ownership.
  212. */
  213. istat = pca9541_reg_read(client, PCA9541_ISTAT);
  214. if (!(istat & PCA9541_ISTAT_NMYTEST)
  215. || time_is_before_eq_jiffies(data->arb_timeout)) {
  216. /*
  217. * Other master did not request ownership,
  218. * or arbitration timeout expired. Take the bus.
  219. */
  220. pca9541_reg_write(client,
  221. PCA9541_CONTROL,
  222. pca9541_control[reg & 0x0f]
  223. | PCA9541_CTL_NTESTON);
  224. data->select_timeout = SELECT_DELAY_SHORT;
  225. } else {
  226. /*
  227. * Other master requested ownership.
  228. * Set extra long timeout to give it time to acquire it.
  229. */
  230. data->select_timeout = SELECT_DELAY_LONG * 2;
  231. }
  232. } else if (mybus(reg)) {
  233. /*
  234. * Bus is on, and we own it. We are done with acquisition.
  235. * Reset NTESTON and BUSINIT, then return success.
  236. */
  237. if (reg & (PCA9541_CTL_NTESTON | PCA9541_CTL_BUSINIT))
  238. pca9541_reg_write(client,
  239. PCA9541_CONTROL,
  240. reg & ~(PCA9541_CTL_NTESTON
  241. | PCA9541_CTL_BUSINIT));
  242. return 1;
  243. } else {
  244. /*
  245. * Other master owns the bus.
  246. * If arbitration timeout has expired, force ownership.
  247. * Otherwise request it.
  248. */
  249. data->select_timeout = SELECT_DELAY_LONG;
  250. if (time_is_before_eq_jiffies(data->arb_timeout)) {
  251. /* Time is up, take the bus and reset it. */
  252. pca9541_reg_write(client,
  253. PCA9541_CONTROL,
  254. pca9541_control[reg & 0x0f]
  255. | PCA9541_CTL_BUSINIT
  256. | PCA9541_CTL_NTESTON);
  257. } else {
  258. /* Request bus ownership if needed */
  259. if (!(reg & PCA9541_CTL_NTESTON))
  260. pca9541_reg_write(client,
  261. PCA9541_CONTROL,
  262. reg | PCA9541_CTL_NTESTON);
  263. }
  264. }
  265. return 0;
  266. }
  267. static int pca9541_select_chan(struct i2c_mux_core *muxc, u32 chan)
  268. {
  269. struct pca9541 *data = i2c_mux_priv(muxc);
  270. struct i2c_client *client = data->client;
  271. int ret;
  272. unsigned long timeout = jiffies + ARB2_TIMEOUT;
  273. /* give up after this time */
  274. data->arb_timeout = jiffies + ARB_TIMEOUT;
  275. /* force bus ownership after this time */
  276. do {
  277. ret = pca9541_arbitrate(client);
  278. if (ret)
  279. return ret < 0 ? ret : 0;
  280. if (data->select_timeout == SELECT_DELAY_SHORT)
  281. udelay(data->select_timeout);
  282. else
  283. msleep(data->select_timeout / 1000);
  284. } while (time_is_after_eq_jiffies(timeout));
  285. return -ETIMEDOUT;
  286. }
  287. static int pca9541_release_chan(struct i2c_mux_core *muxc, u32 chan)
  288. {
  289. struct pca9541 *data = i2c_mux_priv(muxc);
  290. struct i2c_client *client = data->client;
  291. pca9541_release_bus(client);
  292. return 0;
  293. }
  294. /*
  295. * I2C init/probing/exit functions
  296. */
  297. static int pca9541_probe(struct i2c_client *client,
  298. const struct i2c_device_id *id)
  299. {
  300. struct i2c_adapter *adap = client->adapter;
  301. struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
  302. struct i2c_mux_core *muxc;
  303. struct pca9541 *data;
  304. int force;
  305. int ret;
  306. if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE_DATA))
  307. return -ENODEV;
  308. /*
  309. * I2C accesses are unprotected here.
  310. * We have to lock the adapter before releasing the bus.
  311. */
  312. i2c_lock_adapter(adap);
  313. pca9541_release_bus(client);
  314. i2c_unlock_adapter(adap);
  315. /* Create mux adapter */
  316. force = 0;
  317. if (pdata)
  318. force = pdata->modes[0].adap_id;
  319. muxc = i2c_mux_alloc(adap, &client->dev, 1, sizeof(*data),
  320. I2C_MUX_ARBITRATOR,
  321. pca9541_select_chan, pca9541_release_chan);
  322. if (!muxc)
  323. return -ENOMEM;
  324. data = i2c_mux_priv(muxc);
  325. data->client = client;
  326. i2c_set_clientdata(client, muxc);
  327. ret = i2c_mux_add_adapter(muxc, force, 0, 0);
  328. if (ret)
  329. return ret;
  330. dev_info(&client->dev, "registered master selector for I2C %s\n",
  331. client->name);
  332. return 0;
  333. }
  334. static int pca9541_remove(struct i2c_client *client)
  335. {
  336. struct i2c_mux_core *muxc = i2c_get_clientdata(client);
  337. i2c_mux_del_adapters(muxc);
  338. return 0;
  339. }
  340. static struct i2c_driver pca9541_driver = {
  341. .driver = {
  342. .name = "pca9541",
  343. .of_match_table = of_match_ptr(pca9541_of_match),
  344. },
  345. .probe = pca9541_probe,
  346. .remove = pca9541_remove,
  347. .id_table = pca9541_id,
  348. };
  349. module_i2c_driver(pca9541_driver);
  350. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  351. MODULE_DESCRIPTION("PCA9541 I2C master selector driver");
  352. MODULE_LICENSE("GPL v2");