au0828-i2c.c 9.4 KB

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