i2c-mux-pca9541.c 10.0 KB

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