au0828-i2c.c 9.7 KB

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