bttv-i2c.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. bttv-i2c.c -- all the i2c code is here
  3. bttv - Bt848 frame grabber driver
  4. Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
  5. & Marcus Metzler (mocm@thp.uni-koeln.de)
  6. (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
  7. (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
  8. - Multituner support and i2c address binding
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  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/init.h>
  23. #include <linux/delay.h>
  24. #include "bttvp.h"
  25. #include <media/v4l2-common.h>
  26. #include <linux/jiffies.h>
  27. #include <asm/io.h>
  28. static int i2c_debug;
  29. static int i2c_hw;
  30. static int i2c_scan;
  31. module_param(i2c_debug, int, 0644);
  32. MODULE_PARM_DESC(i2c_hw,"configure i2c debug level");
  33. module_param(i2c_hw, int, 0444);
  34. MODULE_PARM_DESC(i2c_hw,"force use of hardware i2c support, "
  35. "instead of software bitbang");
  36. module_param(i2c_scan, int, 0444);
  37. MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
  38. static unsigned int i2c_udelay = 5;
  39. module_param(i2c_udelay, int, 0444);
  40. MODULE_PARM_DESC(i2c_udelay,"soft i2c delay at insmod time, in usecs "
  41. "(should be 5 or higher). Lower value means higher bus speed.");
  42. /* ----------------------------------------------------------------------- */
  43. /* I2C functions - bitbanging adapter (software i2c) */
  44. static void bttv_bit_setscl(void *data, int state)
  45. {
  46. struct bttv *btv = (struct bttv*)data;
  47. if (state)
  48. btv->i2c_state |= 0x02;
  49. else
  50. btv->i2c_state &= ~0x02;
  51. btwrite(btv->i2c_state, BT848_I2C);
  52. btread(BT848_I2C);
  53. }
  54. static void bttv_bit_setsda(void *data, int state)
  55. {
  56. struct bttv *btv = (struct bttv*)data;
  57. if (state)
  58. btv->i2c_state |= 0x01;
  59. else
  60. btv->i2c_state &= ~0x01;
  61. btwrite(btv->i2c_state, BT848_I2C);
  62. btread(BT848_I2C);
  63. }
  64. static int bttv_bit_getscl(void *data)
  65. {
  66. struct bttv *btv = (struct bttv*)data;
  67. int state;
  68. state = btread(BT848_I2C) & 0x02 ? 1 : 0;
  69. return state;
  70. }
  71. static int bttv_bit_getsda(void *data)
  72. {
  73. struct bttv *btv = (struct bttv*)data;
  74. int state;
  75. state = btread(BT848_I2C) & 0x01;
  76. return state;
  77. }
  78. static struct i2c_algo_bit_data __devinitdata bttv_i2c_algo_bit_template = {
  79. .setsda = bttv_bit_setsda,
  80. .setscl = bttv_bit_setscl,
  81. .getsda = bttv_bit_getsda,
  82. .getscl = bttv_bit_getscl,
  83. .udelay = 16,
  84. .timeout = 200,
  85. };
  86. /* ----------------------------------------------------------------------- */
  87. /* I2C functions - hardware i2c */
  88. static u32 functionality(struct i2c_adapter *adap)
  89. {
  90. return I2C_FUNC_SMBUS_EMUL;
  91. }
  92. static int
  93. bttv_i2c_wait_done(struct bttv *btv)
  94. {
  95. int rc = 0;
  96. /* timeout */
  97. if (wait_event_interruptible_timeout(btv->i2c_queue,
  98. btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
  99. rc = -EIO;
  100. if (btv->i2c_done & BT848_INT_RACK)
  101. rc = 1;
  102. btv->i2c_done = 0;
  103. return rc;
  104. }
  105. #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
  106. BT848_I2C_SCL | BT848_I2C_SDA)
  107. static int
  108. bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
  109. {
  110. u32 xmit;
  111. int retval,cnt;
  112. /* sanity checks */
  113. if (0 == msg->len)
  114. return -EINVAL;
  115. /* start, address + first byte */
  116. xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
  117. if (msg->len > 1 || !last)
  118. xmit |= BT878_I2C_NOSTOP;
  119. btwrite(xmit, BT848_I2C);
  120. retval = bttv_i2c_wait_done(btv);
  121. if (retval < 0)
  122. goto err;
  123. if (retval == 0)
  124. goto eio;
  125. if (i2c_debug) {
  126. printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
  127. if (!(xmit & BT878_I2C_NOSTOP))
  128. printk(" >\n");
  129. }
  130. for (cnt = 1; cnt < msg->len; cnt++ ) {
  131. /* following bytes */
  132. xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
  133. if (cnt < msg->len-1 || !last)
  134. xmit |= BT878_I2C_NOSTOP;
  135. btwrite(xmit, BT848_I2C);
  136. retval = bttv_i2c_wait_done(btv);
  137. if (retval < 0)
  138. goto err;
  139. if (retval == 0)
  140. goto eio;
  141. if (i2c_debug) {
  142. printk(" %02x", msg->buf[cnt]);
  143. if (!(xmit & BT878_I2C_NOSTOP))
  144. printk(" >\n");
  145. }
  146. }
  147. return msg->len;
  148. eio:
  149. retval = -EIO;
  150. err:
  151. if (i2c_debug)
  152. printk(" ERR: %d\n",retval);
  153. return retval;
  154. }
  155. static int
  156. bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
  157. {
  158. u32 xmit;
  159. u32 cnt;
  160. int retval;
  161. for(cnt = 0; cnt < msg->len; cnt++) {
  162. xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
  163. if (cnt < msg->len-1)
  164. xmit |= BT848_I2C_W3B;
  165. if (cnt < msg->len-1 || !last)
  166. xmit |= BT878_I2C_NOSTOP;
  167. if (cnt)
  168. xmit |= BT878_I2C_NOSTART;
  169. btwrite(xmit, BT848_I2C);
  170. retval = bttv_i2c_wait_done(btv);
  171. if (retval < 0)
  172. goto err;
  173. if (retval == 0)
  174. goto eio;
  175. msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
  176. if (i2c_debug) {
  177. if (!(xmit & BT878_I2C_NOSTART))
  178. printk(" <R %02x", (msg->addr << 1) +1);
  179. printk(" =%02x", msg->buf[cnt]);
  180. if (!(xmit & BT878_I2C_NOSTOP))
  181. printk(" >\n");
  182. }
  183. }
  184. return msg->len;
  185. eio:
  186. retval = -EIO;
  187. err:
  188. if (i2c_debug)
  189. printk(" ERR: %d\n",retval);
  190. return retval;
  191. }
  192. static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  193. {
  194. struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap);
  195. struct bttv *btv = to_bttv(v4l2_dev);
  196. int retval = 0;
  197. int i;
  198. if (i2c_debug)
  199. printk("bt-i2c:");
  200. btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
  201. for (i = 0 ; i < num; i++) {
  202. if (msgs[i].flags & I2C_M_RD) {
  203. /* read */
  204. retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
  205. if (retval < 0)
  206. goto err;
  207. } else {
  208. /* write */
  209. retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
  210. if (retval < 0)
  211. goto err;
  212. }
  213. }
  214. return num;
  215. err:
  216. return retval;
  217. }
  218. static const struct i2c_algorithm bttv_algo = {
  219. .master_xfer = bttv_i2c_xfer,
  220. .functionality = functionality,
  221. };
  222. /* ----------------------------------------------------------------------- */
  223. /* I2C functions - common stuff */
  224. /* read I2C */
  225. int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
  226. {
  227. unsigned char buffer = 0;
  228. if (0 != btv->i2c_rc)
  229. return -1;
  230. if (bttv_verbose && NULL != probe_for)
  231. printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
  232. btv->c.nr,probe_for,addr);
  233. btv->i2c_client.addr = addr >> 1;
  234. if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
  235. if (NULL != probe_for) {
  236. if (bttv_verbose)
  237. printk("not found\n");
  238. } else
  239. printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
  240. btv->c.nr,addr);
  241. return -1;
  242. }
  243. if (bttv_verbose && NULL != probe_for)
  244. printk("found\n");
  245. return buffer;
  246. }
  247. /* write I2C */
  248. int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
  249. unsigned char b2, int both)
  250. {
  251. unsigned char buffer[2];
  252. int bytes = both ? 2 : 1;
  253. if (0 != btv->i2c_rc)
  254. return -1;
  255. btv->i2c_client.addr = addr >> 1;
  256. buffer[0] = b1;
  257. buffer[1] = b2;
  258. if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
  259. return -1;
  260. return 0;
  261. }
  262. /* read EEPROM content */
  263. void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
  264. {
  265. memset(eedata, 0, 256);
  266. if (0 != btv->i2c_rc)
  267. return;
  268. btv->i2c_client.addr = addr >> 1;
  269. tveeprom_read(&btv->i2c_client, eedata, 256);
  270. }
  271. static char *i2c_devs[128] = {
  272. [ 0x1c >> 1 ] = "lgdt330x",
  273. [ 0x30 >> 1 ] = "IR (hauppauge)",
  274. [ 0x80 >> 1 ] = "msp34xx",
  275. [ 0x86 >> 1 ] = "tda9887",
  276. [ 0xa0 >> 1 ] = "eeprom",
  277. [ 0xc0 >> 1 ] = "tuner (analog)",
  278. [ 0xc2 >> 1 ] = "tuner (analog)",
  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 < ARRAY_SIZE(i2c_devs); i++) {
  285. c->addr = i;
  286. rc = i2c_master_recv(c,&buf,0);
  287. if (rc < 0)
  288. continue;
  289. printk("%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 __devinit init_bttv_i2c(struct bttv *btv)
  295. {
  296. strlcpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE);
  297. if (i2c_hw)
  298. btv->use_i2c_hw = 1;
  299. if (btv->use_i2c_hw) {
  300. /* bt878 */
  301. strlcpy(btv->c.i2c_adap.name, "bt878",
  302. sizeof(btv->c.i2c_adap.name));
  303. btv->c.i2c_adap.id = I2C_HW_B_BT848; /* FIXME */
  304. btv->c.i2c_adap.algo = &bttv_algo;
  305. } else {
  306. /* bt848 */
  307. /* Prevents usage of invalid delay values */
  308. if (i2c_udelay<5)
  309. i2c_udelay=5;
  310. strlcpy(btv->c.i2c_adap.name, "bttv",
  311. sizeof(btv->c.i2c_adap.name));
  312. btv->c.i2c_adap.id = I2C_HW_B_BT848;
  313. memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
  314. sizeof(bttv_i2c_algo_bit_template));
  315. btv->i2c_algo.udelay = i2c_udelay;
  316. btv->i2c_algo.data = btv;
  317. btv->c.i2c_adap.algo_data = &btv->i2c_algo;
  318. }
  319. btv->c.i2c_adap.owner = THIS_MODULE;
  320. btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
  321. snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
  322. "bt%d #%d [%s]", btv->id, btv->c.nr,
  323. btv->use_i2c_hw ? "hw" : "sw");
  324. i2c_set_adapdata(&btv->c.i2c_adap, &btv->c.v4l2_dev);
  325. btv->i2c_client.adapter = &btv->c.i2c_adap;
  326. if (btv->use_i2c_hw) {
  327. btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
  328. } else {
  329. bttv_bit_setscl(btv,1);
  330. bttv_bit_setsda(btv,1);
  331. btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
  332. }
  333. if (0 == btv->i2c_rc && i2c_scan)
  334. do_i2c_scan(btv->c.v4l2_dev.name, &btv->i2c_client);
  335. return btv->i2c_rc;
  336. }
  337. int __devexit fini_bttv_i2c(struct bttv *btv)
  338. {
  339. if (0 != btv->i2c_rc)
  340. return 0;
  341. return i2c_del_adapter(&btv->c.i2c_adap);
  342. }
  343. /*
  344. * Local variables:
  345. * c-basic-offset: 8
  346. * End:
  347. */