au0828-i2c.c 9.5 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 <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. /* FIXME: There is a problem with i2c communications with xc5000 that
  115. requires us to slow down the i2c clock until we have a better
  116. strategy (such as using the secondary i2c bus to do firmware
  117. loading */
  118. if ((msg->addr << 1) == 0xc2)
  119. au0828_write(dev, REG_202, 0x40);
  120. else
  121. au0828_write(dev, REG_202, 0x07);
  122. /* Hardware needs 8 bit addresses */
  123. au0828_write(dev, REG_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, REG_200, 0x20);
  134. if (!i2c_wait_done(i2c_adap))
  135. return -EIO;
  136. if (i2c_wait_read_ack(i2c_adap))
  137. return -EIO;
  138. return 0;
  139. }
  140. for (i = 0; i < msg->len;) {
  141. dprintk(4, " %02x\n", msg->buf[i]);
  142. au0828_write(dev, REG_205, msg->buf[i]);
  143. strobe++;
  144. i++;
  145. if ((strobe >= 4) || (i >= msg->len)) {
  146. /* Strobe the byte into the bus */
  147. if (i < msg->len)
  148. au0828_write(dev, REG_200, 0x41);
  149. else
  150. au0828_write(dev, REG_200, 0x01);
  151. /* Reset strobe trigger */
  152. strobe = 0;
  153. if (!i2c_wait_write_done(i2c_adap))
  154. return -EIO;
  155. }
  156. }
  157. if (!i2c_wait_done(i2c_adap))
  158. return -EIO;
  159. dprintk(4, "\n");
  160. return msg->len;
  161. }
  162. /* FIXME: Implement join handling correctly */
  163. static int i2c_readbytes(struct i2c_adapter *i2c_adap,
  164. const struct i2c_msg *msg, int joined)
  165. {
  166. struct au0828_dev *dev = i2c_adap->algo_data;
  167. int i;
  168. dprintk(4, "%s()\n", __func__);
  169. au0828_write(dev, REG_2FF, 0x01);
  170. /* FIXME: There is a problem with i2c communications with xc5000 that
  171. requires us to slow down the i2c clock until we have a better
  172. strategy (such as using the secondary i2c bus to do firmware
  173. loading */
  174. if ((msg->addr << 1) == 0xc2)
  175. au0828_write(dev, REG_202, 0x40);
  176. else
  177. au0828_write(dev, REG_202, 0x07);
  178. /* Hardware needs 8 bit addresses */
  179. au0828_write(dev, REG_203, msg->addr << 1);
  180. dprintk(4, " RECV:\n");
  181. /* Deal with i2c_scan */
  182. if (msg->len == 0) {
  183. au0828_write(dev, REG_200, 0x20);
  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, REG_200, 0x60);
  192. else
  193. au0828_write(dev, REG_200, 0x20);
  194. if (!i2c_wait_read_done(i2c_adap))
  195. return -EIO;
  196. msg->buf[i-1] = au0828_read(dev, REG_209) & 0xff;
  197. dprintk(4, " %02x\n", msg->buf[i-1]);
  198. }
  199. if (!i2c_wait_done(i2c_adap))
  200. return -EIO;
  201. dprintk(4, "\n");
  202. return msg->len;
  203. }
  204. static int i2c_xfer(struct i2c_adapter *i2c_adap,
  205. struct i2c_msg *msgs, int num)
  206. {
  207. int i, retval = 0;
  208. dprintk(4, "%s(num = %d)\n", __func__, num);
  209. for (i = 0; i < num; i++) {
  210. dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  211. __func__, num, msgs[i].addr, msgs[i].len);
  212. if (msgs[i].flags & I2C_M_RD) {
  213. /* read */
  214. retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
  215. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  216. msgs[i].addr == msgs[i + 1].addr) {
  217. /* write then read from same address */
  218. retval = i2c_sendbytes(i2c_adap, &msgs[i],
  219. msgs[i + 1].len);
  220. if (retval < 0)
  221. goto err;
  222. i++;
  223. retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
  224. } else {
  225. /* write */
  226. retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
  227. }
  228. if (retval < 0)
  229. goto err;
  230. }
  231. return num;
  232. err:
  233. return retval;
  234. }
  235. static int attach_inform(struct i2c_client *client)
  236. {
  237. dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
  238. client->driver->driver.name, client->addr, client->name);
  239. if (!client->driver->command)
  240. return 0;
  241. return 0;
  242. }
  243. static int detach_inform(struct i2c_client *client)
  244. {
  245. dprintk(1, "i2c detach [client=%s]\n", client->name);
  246. return 0;
  247. }
  248. void au0828_call_i2c_clients(struct au0828_dev *dev,
  249. unsigned int cmd, void *arg)
  250. {
  251. if (dev->i2c_rc != 0)
  252. return;
  253. i2c_clients_command(&dev->i2c_adap, cmd, arg);
  254. }
  255. static u32 au0828_functionality(struct i2c_adapter *adap)
  256. {
  257. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  258. }
  259. static struct i2c_algorithm au0828_i2c_algo_template = {
  260. .master_xfer = i2c_xfer,
  261. .functionality = au0828_functionality,
  262. };
  263. /* ----------------------------------------------------------------------- */
  264. static struct i2c_adapter au0828_i2c_adap_template = {
  265. .name = DRIVER_NAME,
  266. .owner = THIS_MODULE,
  267. .id = I2C_HW_B_AU0828,
  268. .algo = &au0828_i2c_algo_template,
  269. .client_register = attach_inform,
  270. .client_unregister = detach_inform,
  271. };
  272. static struct i2c_client au0828_i2c_client_template = {
  273. .name = "au0828 internal",
  274. };
  275. static char *i2c_devs[128] = {
  276. [0x8e >> 1] = "au8522",
  277. [0xa0 >> 1] = "eeprom",
  278. [0xc2 >> 1] = "tuner/xc5000",
  279. };
  280. static void do_i2c_scan(char *name, struct i2c_client *c)
  281. {
  282. unsigned char buf;
  283. int i, rc;
  284. for (i = 0; i < 128; i++) {
  285. c->addr = i;
  286. rc = i2c_master_recv(c, &buf, 0);
  287. if (rc < 0)
  288. continue;
  289. printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
  290. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  291. }
  292. }
  293. /* init + register i2c algo-bit adapter */
  294. int au0828_i2c_register(struct au0828_dev *dev)
  295. {
  296. dprintk(1, "%s()\n", __func__);
  297. memcpy(&dev->i2c_adap, &au0828_i2c_adap_template,
  298. sizeof(dev->i2c_adap));
  299. memcpy(&dev->i2c_algo, &au0828_i2c_algo_template,
  300. sizeof(dev->i2c_algo));
  301. memcpy(&dev->i2c_client, &au0828_i2c_client_template,
  302. sizeof(dev->i2c_client));
  303. dev->i2c_adap.dev.parent = &dev->usbdev->dev;
  304. strlcpy(dev->i2c_adap.name, DRIVER_NAME,
  305. sizeof(dev->i2c_adap.name));
  306. dev->i2c_adap.algo = &dev->i2c_algo;
  307. dev->i2c_adap.algo_data = dev;
  308. i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
  309. i2c_add_adapter(&dev->i2c_adap);
  310. dev->i2c_client.adapter = &dev->i2c_adap;
  311. if (0 == dev->i2c_rc) {
  312. printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME);
  313. if (i2c_scan)
  314. do_i2c_scan(DRIVER_NAME, &dev->i2c_client);
  315. } else
  316. printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME);
  317. return dev->i2c_rc;
  318. }
  319. int au0828_i2c_unregister(struct au0828_dev *dev)
  320. {
  321. i2c_del_adapter(&dev->i2c_adap);
  322. return 0;
  323. }