au0828-i2c.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for the Auvitek AU0828 USB bridge
  4. *
  5. * Copyright (c) 2008 Steven Toth <[email protected]>
  6. */
  7. #include "au0828.h"
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/init.h>
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include "media/tuner.h"
  14. #include <media/v4l2-common.h>
  15. static int i2c_scan;
  16. module_param(i2c_scan, int, 0444);
  17. MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
  18. #define I2C_WAIT_DELAY 25
  19. #define I2C_WAIT_RETRY 1000
  20. static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
  21. {
  22. struct au0828_dev *dev = i2c_adap->algo_data;
  23. return au0828_read(dev, AU0828_I2C_STATUS_201) &
  24. AU0828_I2C_STATUS_NO_READ_ACK ? 0 : 1;
  25. }
  26. static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
  27. {
  28. int count;
  29. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  30. if (!i2c_slave_did_read_ack(i2c_adap))
  31. break;
  32. udelay(I2C_WAIT_DELAY);
  33. }
  34. if (I2C_WAIT_RETRY == count)
  35. return 0;
  36. return 1;
  37. }
  38. static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
  39. {
  40. struct au0828_dev *dev = i2c_adap->algo_data;
  41. return au0828_read(dev, AU0828_I2C_STATUS_201) &
  42. AU0828_I2C_STATUS_READ_DONE ? 0 : 1;
  43. }
  44. static int i2c_wait_read_done(struct i2c_adapter *i2c_adap)
  45. {
  46. int count;
  47. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  48. if (!i2c_is_read_busy(i2c_adap))
  49. break;
  50. udelay(I2C_WAIT_DELAY);
  51. }
  52. if (I2C_WAIT_RETRY == count)
  53. return 0;
  54. return 1;
  55. }
  56. static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap)
  57. {
  58. struct au0828_dev *dev = i2c_adap->algo_data;
  59. return au0828_read(dev, AU0828_I2C_STATUS_201) &
  60. AU0828_I2C_STATUS_WRITE_DONE ? 1 : 0;
  61. }
  62. static int i2c_wait_write_done(struct i2c_adapter *i2c_adap)
  63. {
  64. int count;
  65. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  66. if (i2c_is_write_done(i2c_adap))
  67. break;
  68. udelay(I2C_WAIT_DELAY);
  69. }
  70. if (I2C_WAIT_RETRY == count)
  71. return 0;
  72. return 1;
  73. }
  74. static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
  75. {
  76. struct au0828_dev *dev = i2c_adap->algo_data;
  77. return au0828_read(dev, AU0828_I2C_STATUS_201) &
  78. AU0828_I2C_STATUS_BUSY ? 1 : 0;
  79. }
  80. static int i2c_wait_done(struct i2c_adapter *i2c_adap)
  81. {
  82. int count;
  83. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  84. if (!i2c_is_busy(i2c_adap))
  85. break;
  86. udelay(I2C_WAIT_DELAY);
  87. }
  88. if (I2C_WAIT_RETRY == count)
  89. return 0;
  90. return 1;
  91. }
  92. /* FIXME: Implement join handling correctly */
  93. static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
  94. const struct i2c_msg *msg, int joined_rlen)
  95. {
  96. int i, strobe = 0;
  97. struct au0828_dev *dev = i2c_adap->algo_data;
  98. u8 i2c_speed = dev->board.i2c_clk_divider;
  99. dprintk(4, "%s()\n", __func__);
  100. au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
  101. if (((dev->board.tuner_type == TUNER_XC5000) ||
  102. (dev->board.tuner_type == TUNER_XC5000C)) &&
  103. (dev->board.tuner_addr == msg->addr)) {
  104. /*
  105. * Due to I2C clock stretch, we need to use a lower speed
  106. * on xc5000 for commands. However, firmware transfer can
  107. * speed up to 400 KHz.
  108. */
  109. if (msg->len == 64)
  110. i2c_speed = AU0828_I2C_CLK_250KHZ;
  111. else
  112. i2c_speed = AU0828_I2C_CLK_20KHZ;
  113. }
  114. /* Set the I2C clock */
  115. au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
  116. /* Hardware needs 8 bit addresses */
  117. au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
  118. dprintk(4, "SEND: %02x\n", msg->addr);
  119. /* Deal with i2c_scan */
  120. if (msg->len == 0) {
  121. /* The analog tuner detection code makes use of the SMBUS_QUICK
  122. message (which involves a zero length i2c write). To avoid
  123. checking the status register when we didn't strobe out any
  124. actual bytes to the bus, just do a read check. This is
  125. consistent with how I saw i2c device checking done in the
  126. USB trace of the Windows driver */
  127. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  128. AU0828_I2C_TRIGGER_READ);
  129. if (!i2c_wait_done(i2c_adap))
  130. return -EIO;
  131. if (i2c_wait_read_ack(i2c_adap))
  132. return -EIO;
  133. return 0;
  134. }
  135. for (i = 0; i < msg->len;) {
  136. dprintk(4, " %02x\n", msg->buf[i]);
  137. au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]);
  138. strobe++;
  139. i++;
  140. if ((strobe >= 4) || (i >= msg->len)) {
  141. /* Strobe the byte into the bus */
  142. if (i < msg->len)
  143. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  144. AU0828_I2C_TRIGGER_WRITE |
  145. AU0828_I2C_TRIGGER_HOLD);
  146. else
  147. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  148. AU0828_I2C_TRIGGER_WRITE);
  149. /* Reset strobe trigger */
  150. strobe = 0;
  151. if (!i2c_wait_write_done(i2c_adap))
  152. return -EIO;
  153. }
  154. }
  155. if (!i2c_wait_done(i2c_adap))
  156. return -EIO;
  157. dprintk(4, "\n");
  158. return msg->len;
  159. }
  160. /* FIXME: Implement join handling correctly */
  161. static int i2c_readbytes(struct i2c_adapter *i2c_adap,
  162. const struct i2c_msg *msg, int joined)
  163. {
  164. struct au0828_dev *dev = i2c_adap->algo_data;
  165. u8 i2c_speed = dev->board.i2c_clk_divider;
  166. int i;
  167. dprintk(4, "%s()\n", __func__);
  168. au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
  169. /*
  170. * Due to xc5000c clock stretch, we cannot use full speed at
  171. * readings from xc5000, as otherwise they'll fail.
  172. */
  173. if (((dev->board.tuner_type == TUNER_XC5000) ||
  174. (dev->board.tuner_type == TUNER_XC5000C)) &&
  175. (dev->board.tuner_addr == msg->addr))
  176. i2c_speed = AU0828_I2C_CLK_20KHZ;
  177. /* Set the I2C clock */
  178. au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
  179. /* Hardware needs 8 bit addresses */
  180. au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
  181. dprintk(4, " RECV:\n");
  182. /* Deal with i2c_scan */
  183. if (msg->len == 0) {
  184. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  185. AU0828_I2C_TRIGGER_READ);
  186. if (i2c_wait_read_ack(i2c_adap))
  187. return -EIO;
  188. return 0;
  189. }
  190. for (i = 0; i < msg->len;) {
  191. i++;
  192. if (i < msg->len)
  193. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  194. AU0828_I2C_TRIGGER_READ |
  195. AU0828_I2C_TRIGGER_HOLD);
  196. else
  197. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  198. AU0828_I2C_TRIGGER_READ);
  199. if (!i2c_wait_read_done(i2c_adap))
  200. return -EIO;
  201. msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) &
  202. 0xff;
  203. dprintk(4, " %02x\n", msg->buf[i-1]);
  204. }
  205. if (!i2c_wait_done(i2c_adap))
  206. return -EIO;
  207. dprintk(4, "\n");
  208. return msg->len;
  209. }
  210. static int i2c_xfer(struct i2c_adapter *i2c_adap,
  211. struct i2c_msg *msgs, int num)
  212. {
  213. int i, retval = 0;
  214. dprintk(4, "%s(num = %d)\n", __func__, num);
  215. for (i = 0; i < num; i++) {
  216. dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  217. __func__, num, msgs[i].addr, msgs[i].len);
  218. if (msgs[i].flags & I2C_M_RD) {
  219. /* read */
  220. retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
  221. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  222. msgs[i].addr == msgs[i + 1].addr) {
  223. /* write then read from same address */
  224. retval = i2c_sendbytes(i2c_adap, &msgs[i],
  225. msgs[i + 1].len);
  226. if (retval < 0)
  227. goto err;
  228. i++;
  229. retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
  230. } else {
  231. /* write */
  232. retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
  233. }
  234. if (retval < 0)
  235. goto err;
  236. }
  237. return num;
  238. err:
  239. return retval;
  240. }
  241. static u32 au0828_functionality(struct i2c_adapter *adap)
  242. {
  243. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  244. }
  245. static const struct i2c_algorithm au0828_i2c_algo_template = {
  246. .master_xfer = i2c_xfer,
  247. .functionality = au0828_functionality,
  248. };
  249. /* ----------------------------------------------------------------------- */
  250. static const struct i2c_adapter au0828_i2c_adap_template = {
  251. .name = KBUILD_MODNAME,
  252. .owner = THIS_MODULE,
  253. .algo = &au0828_i2c_algo_template,
  254. };
  255. static const struct i2c_client au0828_i2c_client_template = {
  256. .name = "au0828 internal",
  257. };
  258. static char *i2c_devs[128] = {
  259. [0x8e >> 1] = "au8522",
  260. [0xa0 >> 1] = "eeprom",
  261. [0xc2 >> 1] = "tuner/xc5000",
  262. };
  263. static void do_i2c_scan(char *name, struct i2c_client *c)
  264. {
  265. unsigned char buf;
  266. int i, rc;
  267. for (i = 0; i < 128; i++) {
  268. c->addr = i;
  269. rc = i2c_master_recv(c, &buf, 0);
  270. if (rc < 0)
  271. continue;
  272. pr_info("%s: i2c scan: found device @ 0x%x [%s]\n",
  273. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  274. }
  275. }
  276. /* init + register i2c adapter */
  277. int au0828_i2c_register(struct au0828_dev *dev)
  278. {
  279. dprintk(1, "%s()\n", __func__);
  280. dev->i2c_adap = au0828_i2c_adap_template;
  281. dev->i2c_algo = au0828_i2c_algo_template;
  282. dev->i2c_client = au0828_i2c_client_template;
  283. dev->i2c_adap.dev.parent = &dev->usbdev->dev;
  284. strscpy(dev->i2c_adap.name, KBUILD_MODNAME,
  285. sizeof(dev->i2c_adap.name));
  286. dev->i2c_adap.algo = &dev->i2c_algo;
  287. dev->i2c_adap.algo_data = dev;
  288. #ifdef CONFIG_VIDEO_AU0828_V4L2
  289. i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
  290. #else
  291. i2c_set_adapdata(&dev->i2c_adap, dev);
  292. #endif
  293. i2c_add_adapter(&dev->i2c_adap);
  294. dev->i2c_client.adapter = &dev->i2c_adap;
  295. if (0 == dev->i2c_rc) {
  296. pr_info("i2c bus registered\n");
  297. if (i2c_scan)
  298. do_i2c_scan(KBUILD_MODNAME, &dev->i2c_client);
  299. } else
  300. pr_info("i2c bus register FAILED\n");
  301. return dev->i2c_rc;
  302. }
  303. int au0828_i2c_unregister(struct au0828_dev *dev)
  304. {
  305. i2c_del_adapter(&dev->i2c_adap);
  306. return 0;
  307. }