au0828-i2c.c 9.0 KB

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