au0828-i2c.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 512
  33. #define I2C_WAIT_RETRY 64
  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_addr == msg->addr) &&
  123. (msg->len == 64)) {
  124. /* Hack to speed up firmware load. The xc5000 lets us do up
  125. to 400 KHz when in firmware download mode */
  126. au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
  127. AU0828_I2C_CLK_250KHZ);
  128. } else {
  129. /* Use the i2c clock speed in the board configuration */
  130. au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
  131. dev->board.i2c_clk_divider);
  132. }
  133. /* Hardware needs 8 bit addresses */
  134. au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
  135. dprintk(4, "SEND: %02x\n", msg->addr);
  136. /* Deal with i2c_scan */
  137. if (msg->len == 0) {
  138. /* The analog tuner detection code makes use of the SMBUS_QUICK
  139. message (which involves a zero length i2c write). To avoid
  140. checking the status register when we didn't strobe out any
  141. actual bytes to the bus, just do a read check. This is
  142. consistent with how I saw i2c device checking done in the
  143. USB trace of the Windows driver */
  144. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  145. AU0828_I2C_TRIGGER_READ);
  146. if (!i2c_wait_done(i2c_adap))
  147. return -EIO;
  148. if (i2c_wait_read_ack(i2c_adap))
  149. return -EIO;
  150. return 0;
  151. }
  152. for (i = 0; i < msg->len;) {
  153. dprintk(4, " %02x\n", msg->buf[i]);
  154. au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]);
  155. strobe++;
  156. i++;
  157. if ((strobe >= 4) || (i >= msg->len)) {
  158. /* Strobe the byte into the bus */
  159. if (i < msg->len)
  160. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  161. AU0828_I2C_TRIGGER_WRITE |
  162. AU0828_I2C_TRIGGER_HOLD);
  163. else
  164. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  165. AU0828_I2C_TRIGGER_WRITE);
  166. /* Reset strobe trigger */
  167. strobe = 0;
  168. if (!i2c_wait_write_done(i2c_adap))
  169. return -EIO;
  170. }
  171. }
  172. if (!i2c_wait_done(i2c_adap))
  173. return -EIO;
  174. dprintk(4, "\n");
  175. return msg->len;
  176. }
  177. /* FIXME: Implement join handling correctly */
  178. static int i2c_readbytes(struct i2c_adapter *i2c_adap,
  179. const struct i2c_msg *msg, int joined)
  180. {
  181. struct au0828_dev *dev = i2c_adap->algo_data;
  182. int i;
  183. dprintk(4, "%s()\n", __func__);
  184. au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
  185. /* Set the I2C clock */
  186. au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
  187. dev->board.i2c_clk_divider);
  188. /* Hardware needs 8 bit addresses */
  189. au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
  190. dprintk(4, " RECV:\n");
  191. /* Deal with i2c_scan */
  192. if (msg->len == 0) {
  193. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  194. AU0828_I2C_TRIGGER_READ);
  195. if (i2c_wait_read_ack(i2c_adap))
  196. return -EIO;
  197. return 0;
  198. }
  199. for (i = 0; i < msg->len;) {
  200. i++;
  201. if (i < msg->len)
  202. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  203. AU0828_I2C_TRIGGER_READ |
  204. AU0828_I2C_TRIGGER_HOLD);
  205. else
  206. au0828_write(dev, AU0828_I2C_TRIGGER_200,
  207. AU0828_I2C_TRIGGER_READ);
  208. if (!i2c_wait_read_done(i2c_adap))
  209. return -EIO;
  210. msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) &
  211. 0xff;
  212. dprintk(4, " %02x\n", msg->buf[i-1]);
  213. }
  214. if (!i2c_wait_done(i2c_adap))
  215. return -EIO;
  216. dprintk(4, "\n");
  217. return msg->len;
  218. }
  219. static int i2c_xfer(struct i2c_adapter *i2c_adap,
  220. struct i2c_msg *msgs, int num)
  221. {
  222. int i, retval = 0;
  223. dprintk(4, "%s(num = %d)\n", __func__, num);
  224. for (i = 0; i < num; i++) {
  225. dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  226. __func__, num, msgs[i].addr, msgs[i].len);
  227. if (msgs[i].flags & I2C_M_RD) {
  228. /* read */
  229. retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
  230. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  231. msgs[i].addr == msgs[i + 1].addr) {
  232. /* write then read from same address */
  233. retval = i2c_sendbytes(i2c_adap, &msgs[i],
  234. msgs[i + 1].len);
  235. if (retval < 0)
  236. goto err;
  237. i++;
  238. retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
  239. } else {
  240. /* write */
  241. retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
  242. }
  243. if (retval < 0)
  244. goto err;
  245. }
  246. return num;
  247. err:
  248. return retval;
  249. }
  250. static u32 au0828_functionality(struct i2c_adapter *adap)
  251. {
  252. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  253. }
  254. static struct i2c_algorithm au0828_i2c_algo_template = {
  255. .master_xfer = i2c_xfer,
  256. .functionality = au0828_functionality,
  257. };
  258. /* ----------------------------------------------------------------------- */
  259. static struct i2c_adapter au0828_i2c_adap_template = {
  260. .name = DRIVER_NAME,
  261. .owner = THIS_MODULE,
  262. .algo = &au0828_i2c_algo_template,
  263. };
  264. static struct i2c_client au0828_i2c_client_template = {
  265. .name = "au0828 internal",
  266. };
  267. static char *i2c_devs[128] = {
  268. [0x8e >> 1] = "au8522",
  269. [0xa0 >> 1] = "eeprom",
  270. [0xc2 >> 1] = "tuner/xc5000",
  271. };
  272. static void do_i2c_scan(char *name, struct i2c_client *c)
  273. {
  274. unsigned char buf;
  275. int i, rc;
  276. for (i = 0; i < 128; i++) {
  277. c->addr = i;
  278. rc = i2c_master_recv(c, &buf, 0);
  279. if (rc < 0)
  280. continue;
  281. printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
  282. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  283. }
  284. }
  285. /* init + register i2c adapter */
  286. int au0828_i2c_register(struct au0828_dev *dev)
  287. {
  288. dprintk(1, "%s()\n", __func__);
  289. memcpy(&dev->i2c_adap, &au0828_i2c_adap_template,
  290. sizeof(dev->i2c_adap));
  291. memcpy(&dev->i2c_algo, &au0828_i2c_algo_template,
  292. sizeof(dev->i2c_algo));
  293. memcpy(&dev->i2c_client, &au0828_i2c_client_template,
  294. sizeof(dev->i2c_client));
  295. dev->i2c_adap.dev.parent = &dev->usbdev->dev;
  296. strlcpy(dev->i2c_adap.name, DRIVER_NAME,
  297. sizeof(dev->i2c_adap.name));
  298. dev->i2c_adap.algo = &dev->i2c_algo;
  299. dev->i2c_adap.algo_data = dev;
  300. i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
  301. i2c_add_adapter(&dev->i2c_adap);
  302. dev->i2c_client.adapter = &dev->i2c_adap;
  303. if (0 == dev->i2c_rc) {
  304. printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME);
  305. if (i2c_scan)
  306. do_i2c_scan(DRIVER_NAME, &dev->i2c_client);
  307. } else
  308. printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME);
  309. return dev->i2c_rc;
  310. }
  311. int au0828_i2c_unregister(struct au0828_dev *dev)
  312. {
  313. i2c_del_adapter(&dev->i2c_adap);
  314. return 0;
  315. }