pca9541.c 10 KB

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