bttv-i2c.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include "bttvp.h"
  26. #include <media/v4l2-common.h>
  27. #include <linux/jiffies.h>
  28. #include <asm/io.h>
  29. static int i2c_debug;
  30. static int i2c_hw;
  31. static int i2c_scan;
  32. module_param(i2c_debug, int, 0644);
  33. MODULE_PARM_DESC(i2c_debug, "configure i2c debug level");
  34. module_param(i2c_hw, int, 0444);
  35. MODULE_PARM_DESC(i2c_hw,"force use of hardware i2c support, "
  36. "instead of software bitbang");
  37. module_param(i2c_scan, int, 0444);
  38. MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
  39. static unsigned int i2c_udelay = 5;
  40. module_param(i2c_udelay, int, 0444);
  41. MODULE_PARM_DESC(i2c_udelay,"soft i2c delay at insmod time, in usecs "
  42. "(should be 5 or higher). Lower value means higher bus speed.");
  43. /* ----------------------------------------------------------------------- */
  44. /* I2C functions - bitbanging adapter (software i2c) */
  45. static void bttv_bit_setscl(void *data, int state)
  46. {
  47. struct bttv *btv = (struct bttv*)data;
  48. if (state)
  49. btv->i2c_state |= 0x02;
  50. else
  51. btv->i2c_state &= ~0x02;
  52. btwrite(btv->i2c_state, BT848_I2C);
  53. btread(BT848_I2C);
  54. }
  55. static void bttv_bit_setsda(void *data, int state)
  56. {
  57. struct bttv *btv = (struct bttv*)data;
  58. if (state)
  59. btv->i2c_state |= 0x01;
  60. else
  61. btv->i2c_state &= ~0x01;
  62. btwrite(btv->i2c_state, BT848_I2C);
  63. btread(BT848_I2C);
  64. }
  65. static int bttv_bit_getscl(void *data)
  66. {
  67. struct bttv *btv = (struct bttv*)data;
  68. int state;
  69. state = btread(BT848_I2C) & 0x02 ? 1 : 0;
  70. return state;
  71. }
  72. static int bttv_bit_getsda(void *data)
  73. {
  74. struct bttv *btv = (struct bttv*)data;
  75. int state;
  76. state = btread(BT848_I2C) & 0x01;
  77. return state;
  78. }
  79. static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
  80. .setsda = bttv_bit_setsda,
  81. .setscl = bttv_bit_setscl,
  82. .getsda = bttv_bit_getsda,
  83. .getscl = bttv_bit_getscl,
  84. .udelay = 16,
  85. .timeout = 200,
  86. };
  87. /* ----------------------------------------------------------------------- */
  88. /* I2C functions - hardware i2c */
  89. static u32 functionality(struct i2c_adapter *adap)
  90. {
  91. return I2C_FUNC_SMBUS_EMUL;
  92. }
  93. static int
  94. bttv_i2c_wait_done(struct bttv *btv)
  95. {
  96. int rc = 0;
  97. /* timeout */
  98. if (wait_event_interruptible_timeout(btv->i2c_queue,
  99. btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
  100. rc = -EIO;
  101. if (btv->i2c_done & BT848_INT_RACK)
  102. rc = 1;
  103. btv->i2c_done = 0;
  104. return rc;
  105. }
  106. #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
  107. BT848_I2C_SCL | BT848_I2C_SDA)
  108. static int
  109. bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
  110. {
  111. u32 xmit;
  112. int retval,cnt;
  113. /* sanity checks */
  114. if (0 == msg->len)
  115. return -EINVAL;
  116. /* start, address + first byte */
  117. xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
  118. if (msg->len > 1 || !last)
  119. xmit |= BT878_I2C_NOSTOP;
  120. btwrite(xmit, BT848_I2C);
  121. retval = bttv_i2c_wait_done(btv);
  122. if (retval < 0)
  123. goto err;
  124. if (retval == 0)
  125. goto eio;
  126. if (i2c_debug) {
  127. pr_cont(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
  128. }
  129. for (cnt = 1; cnt < msg->len; cnt++ ) {
  130. /* following bytes */
  131. xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
  132. if (cnt < msg->len-1 || !last)
  133. xmit |= BT878_I2C_NOSTOP;
  134. btwrite(xmit, BT848_I2C);
  135. retval = bttv_i2c_wait_done(btv);
  136. if (retval < 0)
  137. goto err;
  138. if (retval == 0)
  139. goto eio;
  140. if (i2c_debug)
  141. pr_cont(" %02x", msg->buf[cnt]);
  142. }
  143. if (i2c_debug && !(xmit & BT878_I2C_NOSTOP))
  144. pr_cont(">\n");
  145. return msg->len;
  146. eio:
  147. retval = -EIO;
  148. err:
  149. if (i2c_debug)
  150. pr_cont(" ERR: %d\n",retval);
  151. return retval;
  152. }
  153. static int
  154. bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
  155. {
  156. u32 xmit;
  157. u32 cnt;
  158. int retval;
  159. for (cnt = 0; cnt < msg->len; cnt++) {
  160. xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
  161. if (cnt < msg->len-1)
  162. xmit |= BT848_I2C_W3B;
  163. if (cnt < msg->len-1 || !last)
  164. xmit |= BT878_I2C_NOSTOP;
  165. if (cnt)
  166. xmit |= BT878_I2C_NOSTART;
  167. if (i2c_debug) {
  168. if (!(xmit & BT878_I2C_NOSTART))
  169. pr_cont(" <R %02x", (msg->addr << 1) +1);
  170. }
  171. btwrite(xmit, BT848_I2C);
  172. retval = bttv_i2c_wait_done(btv);
  173. if (retval < 0)
  174. goto err;
  175. if (retval == 0)
  176. goto eio;
  177. msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
  178. if (i2c_debug) {
  179. pr_cont(" =%02x", msg->buf[cnt]);
  180. }
  181. if (i2c_debug && !(xmit & BT878_I2C_NOSTOP))
  182. pr_cont(" >\n");
  183. }
  184. return msg->len;
  185. eio:
  186. retval = -EIO;
  187. err:
  188. if (i2c_debug)
  189. pr_cont(" 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. pr_debug("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. pr_info("%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. pr_cont("not found\n");
  238. } else
  239. pr_warn("%d: i2c read 0x%x: error\n",
  240. btv->c.nr, addr);
  241. return -1;
  242. }
  243. if (bttv_verbose && NULL != probe_for)
  244. pr_cont("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 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. pr_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 adapter */
  294. int 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.algo = &bttv_algo;
  304. } else {
  305. /* bt848 */
  306. /* Prevents usage of invalid delay values */
  307. if (i2c_udelay<5)
  308. i2c_udelay=5;
  309. strlcpy(btv->c.i2c_adap.name, "bttv",
  310. sizeof(btv->c.i2c_adap.name));
  311. btv->i2c_algo = bttv_i2c_algo_bit_template;
  312. btv->i2c_algo.udelay = i2c_udelay;
  313. btv->i2c_algo.data = btv;
  314. btv->c.i2c_adap.algo_data = &btv->i2c_algo;
  315. }
  316. btv->c.i2c_adap.owner = THIS_MODULE;
  317. btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
  318. snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
  319. "bt%d #%d [%s]", btv->id, btv->c.nr,
  320. btv->use_i2c_hw ? "hw" : "sw");
  321. i2c_set_adapdata(&btv->c.i2c_adap, &btv->c.v4l2_dev);
  322. btv->i2c_client.adapter = &btv->c.i2c_adap;
  323. if (btv->use_i2c_hw) {
  324. btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
  325. } else {
  326. bttv_bit_setscl(btv,1);
  327. bttv_bit_setsda(btv,1);
  328. btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
  329. }
  330. if (0 == btv->i2c_rc && i2c_scan)
  331. do_i2c_scan(btv->c.v4l2_dev.name, &btv->i2c_client);
  332. return btv->i2c_rc;
  333. }
  334. int fini_bttv_i2c(struct bttv *btv)
  335. {
  336. if (btv->i2c_rc == 0)
  337. i2c_del_adapter(&btv->c.i2c_adap);
  338. return 0;
  339. }