au0828-i2c.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Driver for the Auvitek AU0828 USB bridge
  3. *
  4. * Copyright (c) 2008 Steven Toth <stoth@hauppauge.com>
  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, REG_201) & 0x08 ? 0 : 1;
  37. }
  38. static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
  39. {
  40. struct au0828_dev *dev = i2c_adap->algo_data;
  41. return au0828_read(dev, REG_201) & 0x02 ? 0 : 1;
  42. }
  43. static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
  44. {
  45. int count;
  46. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  47. if (!i2c_slave_did_read_ack(i2c_adap))
  48. break;
  49. udelay(I2C_WAIT_DELAY);
  50. }
  51. if (I2C_WAIT_RETRY == count)
  52. return 0;
  53. return 1;
  54. }
  55. static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
  56. {
  57. struct au0828_dev *dev = i2c_adap->algo_data;
  58. return au0828_read(dev, REG_201) & 0x01 ? 0 : 1;
  59. }
  60. static int i2c_wait_read_done(struct i2c_adapter *i2c_adap)
  61. {
  62. int count;
  63. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  64. if (!i2c_is_read_busy(i2c_adap))
  65. break;
  66. udelay(I2C_WAIT_DELAY);
  67. }
  68. if (I2C_WAIT_RETRY == count)
  69. return 0;
  70. return 1;
  71. }
  72. static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap)
  73. {
  74. struct au0828_dev *dev = i2c_adap->algo_data;
  75. return au0828_read(dev, REG_201) & 0x04 ? 1 : 0;
  76. }
  77. static int i2c_wait_write_done(struct i2c_adapter *i2c_adap)
  78. {
  79. int count;
  80. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  81. if (i2c_is_write_done(i2c_adap))
  82. break;
  83. udelay(I2C_WAIT_DELAY);
  84. }
  85. if (I2C_WAIT_RETRY == count)
  86. return 0;
  87. return 1;
  88. }
  89. static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
  90. {
  91. struct au0828_dev *dev = i2c_adap->algo_data;
  92. return au0828_read(dev, REG_201) & 0x10 ? 1 : 0;
  93. }
  94. static int i2c_wait_done(struct i2c_adapter *i2c_adap)
  95. {
  96. int count;
  97. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  98. if (!i2c_is_busy(i2c_adap))
  99. break;
  100. udelay(I2C_WAIT_DELAY);
  101. }
  102. if (I2C_WAIT_RETRY == count)
  103. return 0;
  104. return 1;
  105. }
  106. /* FIXME: Implement join handling correctly */
  107. static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
  108. const struct i2c_msg *msg, int joined_rlen)
  109. {
  110. int i, strobe = 0;
  111. struct au0828_dev *dev = i2c_adap->algo_data;
  112. dprintk(4, "%s()\n", __func__);
  113. au0828_write(dev, REG_2FF, 0x01);
  114. au0828_write(dev, REG_202, 0x07);
  115. /* Hardware needs 8 bit addresses */
  116. au0828_write(dev, REG_203, msg->addr << 1);
  117. dprintk(4, "SEND: %02x\n", msg->addr);
  118. for (i = 0; i < msg->len;) {
  119. dprintk(4, " %02x\n", msg->buf[i]);
  120. au0828_write(dev, REG_205, msg->buf[i]);
  121. strobe++;
  122. i++;
  123. if ((strobe >= 4) || (i >= msg->len)) {
  124. /* Strobe the byte into the bus */
  125. if (i < msg->len)
  126. au0828_write(dev, REG_200, 0x41);
  127. else
  128. au0828_write(dev, REG_200, 0x01);
  129. /* Reset strobe trigger */
  130. strobe = 0;
  131. if (!i2c_wait_write_done(i2c_adap))
  132. return -EIO;
  133. }
  134. }
  135. if (!i2c_wait_done(i2c_adap))
  136. return -EIO;
  137. dprintk(4, "\n");
  138. return msg->len;
  139. }
  140. /* FIXME: Implement join handling correctly */
  141. static int i2c_readbytes(struct i2c_adapter *i2c_adap,
  142. const struct i2c_msg *msg, int joined)
  143. {
  144. struct au0828_dev *dev = i2c_adap->algo_data;
  145. int i;
  146. dprintk(4, "%s()\n", __func__);
  147. au0828_write(dev, REG_2FF, 0x01);
  148. au0828_write(dev, REG_202, 0x07);
  149. /* Hardware needs 8 bit addresses */
  150. au0828_write(dev, REG_203, msg->addr << 1);
  151. dprintk(4, " RECV:\n");
  152. /* Deal with i2c_scan */
  153. if (msg->len == 0) {
  154. au0828_write(dev, REG_200, 0x20);
  155. if (i2c_wait_read_ack(i2c_adap))
  156. return -EIO;
  157. return 0;
  158. }
  159. for (i = 0; i < msg->len;) {
  160. i++;
  161. if (i < msg->len)
  162. au0828_write(dev, REG_200, 0x60);
  163. else
  164. au0828_write(dev, REG_200, 0x20);
  165. if (!i2c_wait_read_done(i2c_adap))
  166. return -EIO;
  167. msg->buf[i-1] = au0828_read(dev, REG_209) & 0xff;
  168. dprintk(4, " %02x\n", msg->buf[i-1]);
  169. }
  170. if (!i2c_wait_done(i2c_adap))
  171. return -EIO;
  172. dprintk(4, "\n");
  173. return msg->len;
  174. }
  175. static int i2c_xfer(struct i2c_adapter *i2c_adap,
  176. struct i2c_msg *msgs, int num)
  177. {
  178. int i, retval = 0;
  179. dprintk(4, "%s(num = %d)\n", __func__, num);
  180. for (i = 0; i < num; i++) {
  181. dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  182. __func__, num, msgs[i].addr, msgs[i].len);
  183. if (msgs[i].flags & I2C_M_RD) {
  184. /* read */
  185. retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
  186. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  187. msgs[i].addr == msgs[i + 1].addr) {
  188. /* write then read from same address */
  189. retval = i2c_sendbytes(i2c_adap, &msgs[i],
  190. msgs[i + 1].len);
  191. if (retval < 0)
  192. goto err;
  193. i++;
  194. retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
  195. } else {
  196. /* write */
  197. retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
  198. }
  199. if (retval < 0)
  200. goto err;
  201. }
  202. return num;
  203. err:
  204. return retval;
  205. }
  206. static int attach_inform(struct i2c_client *client)
  207. {
  208. dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
  209. client->driver->driver.name, client->addr, client->name);
  210. if (!client->driver->command)
  211. return 0;
  212. return 0;
  213. }
  214. static int detach_inform(struct i2c_client *client)
  215. {
  216. dprintk(1, "i2c detach [client=%s]\n", client->name);
  217. return 0;
  218. }
  219. void au0828_call_i2c_clients(struct au0828_dev *dev,
  220. unsigned int cmd, void *arg)
  221. {
  222. if (dev->i2c_rc != 0)
  223. return;
  224. i2c_clients_command(&dev->i2c_adap, cmd, arg);
  225. }
  226. static u32 au0828_functionality(struct i2c_adapter *adap)
  227. {
  228. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  229. }
  230. static struct i2c_algorithm au0828_i2c_algo_template = {
  231. .master_xfer = i2c_xfer,
  232. .functionality = au0828_functionality,
  233. };
  234. /* ----------------------------------------------------------------------- */
  235. static struct i2c_adapter au0828_i2c_adap_template = {
  236. .name = DRIVER_NAME,
  237. .owner = THIS_MODULE,
  238. .id = I2C_HW_B_AU0828,
  239. .algo = &au0828_i2c_algo_template,
  240. .class = I2C_CLASS_TV_ANALOG,
  241. .client_register = attach_inform,
  242. .client_unregister = detach_inform,
  243. };
  244. static struct i2c_client au0828_i2c_client_template = {
  245. .name = "au0828 internal",
  246. };
  247. static char *i2c_devs[128] = {
  248. [0x8e >> 1] = "au8522",
  249. [0xa0 >> 1] = "eeprom",
  250. [0xc2 >> 1] = "tuner/xc5000",
  251. };
  252. static void do_i2c_scan(char *name, struct i2c_client *c)
  253. {
  254. unsigned char buf;
  255. int i, rc;
  256. for (i = 0; i < 128; i++) {
  257. c->addr = i;
  258. rc = i2c_master_recv(c, &buf, 0);
  259. if (rc < 0)
  260. continue;
  261. printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
  262. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  263. }
  264. }
  265. /* init + register i2c algo-bit adapter */
  266. int au0828_i2c_register(struct au0828_dev *dev)
  267. {
  268. dprintk(1, "%s()\n", __func__);
  269. memcpy(&dev->i2c_adap, &au0828_i2c_adap_template,
  270. sizeof(dev->i2c_adap));
  271. memcpy(&dev->i2c_algo, &au0828_i2c_algo_template,
  272. sizeof(dev->i2c_algo));
  273. memcpy(&dev->i2c_client, &au0828_i2c_client_template,
  274. sizeof(dev->i2c_client));
  275. dev->i2c_adap.dev.parent = &dev->usbdev->dev;
  276. strlcpy(dev->i2c_adap.name, DRIVER_NAME,
  277. sizeof(dev->i2c_adap.name));
  278. dev->i2c_algo.data = dev;
  279. dev->i2c_adap.algo_data = dev;
  280. i2c_set_adapdata(&dev->i2c_adap, dev);
  281. i2c_add_adapter(&dev->i2c_adap);
  282. dev->i2c_client.adapter = &dev->i2c_adap;
  283. if (0 == dev->i2c_rc) {
  284. printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME);
  285. if (i2c_scan)
  286. do_i2c_scan(DRIVER_NAME, &dev->i2c_client);
  287. } else
  288. printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME);
  289. return dev->i2c_rc;
  290. }
  291. int au0828_i2c_unregister(struct au0828_dev *dev)
  292. {
  293. i2c_del_adapter(&dev->i2c_adap);
  294. return 0;
  295. }