au0828-i2c.c 9.5 KB

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