au0828-i2c.c 8.5 KB

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