au0828-i2c.c 9.7 KB

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