bttv-i2c.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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@kernel.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, 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 (should be 5 or higher). Lower value means higher bus speed.");
  41. /* ----------------------------------------------------------------------- */
  42. /* I2C functions - bitbanging adapter (software i2c) */
  43. static void bttv_bit_setscl(void *data, int state)
  44. {
  45. struct bttv *btv = (struct bttv*)data;
  46. if (state)
  47. btv->i2c_state |= 0x02;
  48. else
  49. btv->i2c_state &= ~0x02;
  50. btwrite(btv->i2c_state, BT848_I2C);
  51. btread(BT848_I2C);
  52. }
  53. static void bttv_bit_setsda(void *data, int state)
  54. {
  55. struct bttv *btv = (struct bttv*)data;
  56. if (state)
  57. btv->i2c_state |= 0x01;
  58. else
  59. btv->i2c_state &= ~0x01;
  60. btwrite(btv->i2c_state, BT848_I2C);
  61. btread(BT848_I2C);
  62. }
  63. static int bttv_bit_getscl(void *data)
  64. {
  65. struct bttv *btv = (struct bttv*)data;
  66. int state;
  67. state = btread(BT848_I2C) & 0x02 ? 1 : 0;
  68. return state;
  69. }
  70. static int bttv_bit_getsda(void *data)
  71. {
  72. struct bttv *btv = (struct bttv*)data;
  73. int state;
  74. state = btread(BT848_I2C) & 0x01;
  75. return state;
  76. }
  77. static const struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
  78. .setsda = bttv_bit_setsda,
  79. .setscl = bttv_bit_setscl,
  80. .getsda = bttv_bit_getsda,
  81. .getscl = bttv_bit_getscl,
  82. .udelay = 16,
  83. .timeout = 200,
  84. };
  85. /* ----------------------------------------------------------------------- */
  86. /* I2C functions - hardware i2c */
  87. static u32 functionality(struct i2c_adapter *adap)
  88. {
  89. return I2C_FUNC_SMBUS_EMUL;
  90. }
  91. static int
  92. bttv_i2c_wait_done(struct bttv *btv)
  93. {
  94. int rc = 0;
  95. /* timeout */
  96. if (wait_event_interruptible_timeout(btv->i2c_queue,
  97. btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
  98. rc = -EIO;
  99. if (btv->i2c_done & BT848_INT_RACK)
  100. rc = 1;
  101. btv->i2c_done = 0;
  102. return rc;
  103. }
  104. #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
  105. BT848_I2C_SCL | BT848_I2C_SDA)
  106. static int
  107. bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
  108. {
  109. u32 xmit;
  110. int retval,cnt;
  111. /* sanity checks */
  112. if (0 == msg->len)
  113. return -EINVAL;
  114. /* start, address + first byte */
  115. xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
  116. if (msg->len > 1 || !last)
  117. xmit |= BT878_I2C_NOSTOP;
  118. btwrite(xmit, BT848_I2C);
  119. retval = bttv_i2c_wait_done(btv);
  120. if (retval < 0)
  121. goto err;
  122. if (retval == 0)
  123. goto eio;
  124. if (i2c_debug) {
  125. pr_cont(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
  126. }
  127. for (cnt = 1; cnt < msg->len; cnt++ ) {
  128. /* following bytes */
  129. xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
  130. if (cnt < msg->len-1 || !last)
  131. xmit |= BT878_I2C_NOSTOP;
  132. btwrite(xmit, BT848_I2C);
  133. retval = bttv_i2c_wait_done(btv);
  134. if (retval < 0)
  135. goto err;
  136. if (retval == 0)
  137. goto eio;
  138. if (i2c_debug)
  139. pr_cont(" %02x", msg->buf[cnt]);
  140. }
  141. if (i2c_debug && !(xmit & BT878_I2C_NOSTOP))
  142. pr_cont(">\n");
  143. return msg->len;
  144. eio:
  145. retval = -EIO;
  146. err:
  147. if (i2c_debug)
  148. pr_cont(" ERR: %d\n",retval);
  149. return retval;
  150. }
  151. static int
  152. bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
  153. {
  154. u32 xmit;
  155. u32 cnt;
  156. int retval;
  157. for (cnt = 0; cnt < msg->len; cnt++) {
  158. xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
  159. if (cnt < msg->len-1)
  160. xmit |= BT848_I2C_W3B;
  161. if (cnt < msg->len-1 || !last)
  162. xmit |= BT878_I2C_NOSTOP;
  163. if (cnt)
  164. xmit |= BT878_I2C_NOSTART;
  165. if (i2c_debug) {
  166. if (!(xmit & BT878_I2C_NOSTART))
  167. pr_cont(" <R %02x", (msg->addr << 1) +1);
  168. }
  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. pr_cont(" =%02x", msg->buf[cnt]);
  178. }
  179. if (i2c_debug && !(xmit & BT878_I2C_NOSTOP))
  180. pr_cont(" >\n");
  181. }
  182. return msg->len;
  183. eio:
  184. retval = -EIO;
  185. err:
  186. if (i2c_debug)
  187. pr_cont(" ERR: %d\n",retval);
  188. return retval;
  189. }
  190. static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  191. {
  192. struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap);
  193. struct bttv *btv = to_bttv(v4l2_dev);
  194. int retval = 0;
  195. int i;
  196. if (i2c_debug)
  197. pr_debug("bt-i2c:");
  198. btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
  199. for (i = 0 ; i < num; i++) {
  200. if (msgs[i].flags & I2C_M_RD) {
  201. /* read */
  202. retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
  203. if (retval < 0)
  204. goto err;
  205. } else {
  206. /* write */
  207. retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
  208. if (retval < 0)
  209. goto err;
  210. }
  211. }
  212. return num;
  213. err:
  214. return retval;
  215. }
  216. static const struct i2c_algorithm bttv_algo = {
  217. .master_xfer = bttv_i2c_xfer,
  218. .functionality = functionality,
  219. };
  220. /* ----------------------------------------------------------------------- */
  221. /* I2C functions - common stuff */
  222. /* read I2C */
  223. int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
  224. {
  225. unsigned char buffer = 0;
  226. if (0 != btv->i2c_rc)
  227. return -1;
  228. if (bttv_verbose && NULL != probe_for)
  229. pr_info("%d: i2c: checking for %s @ 0x%02x... ",
  230. btv->c.nr, probe_for, addr);
  231. btv->i2c_client.addr = addr >> 1;
  232. if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
  233. if (NULL != probe_for) {
  234. if (bttv_verbose)
  235. pr_cont("not found\n");
  236. } else
  237. pr_warn("%d: i2c read 0x%x: error\n",
  238. btv->c.nr, addr);
  239. return -1;
  240. }
  241. if (bttv_verbose && NULL != probe_for)
  242. pr_cont("found\n");
  243. return buffer;
  244. }
  245. /* write I2C */
  246. int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
  247. unsigned char b2, int both)
  248. {
  249. unsigned char buffer[2];
  250. int bytes = both ? 2 : 1;
  251. if (0 != btv->i2c_rc)
  252. return -1;
  253. btv->i2c_client.addr = addr >> 1;
  254. buffer[0] = b1;
  255. buffer[1] = b2;
  256. if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
  257. return -1;
  258. return 0;
  259. }
  260. /* read EEPROM content */
  261. void bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
  262. {
  263. memset(eedata, 0, 256);
  264. if (0 != btv->i2c_rc)
  265. return;
  266. btv->i2c_client.addr = addr >> 1;
  267. tveeprom_read(&btv->i2c_client, eedata, 256);
  268. }
  269. static char *i2c_devs[128] = {
  270. [ 0x1c >> 1 ] = "lgdt330x",
  271. [ 0x30 >> 1 ] = "IR (hauppauge)",
  272. [ 0x80 >> 1 ] = "msp34xx",
  273. [ 0x86 >> 1 ] = "tda9887",
  274. [ 0xa0 >> 1 ] = "eeprom",
  275. [ 0xc0 >> 1 ] = "tuner (analog)",
  276. [ 0xc2 >> 1 ] = "tuner (analog)",
  277. };
  278. static void do_i2c_scan(char *name, struct i2c_client *c)
  279. {
  280. unsigned char buf;
  281. int i,rc;
  282. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  283. c->addr = i;
  284. rc = i2c_master_recv(c,&buf,0);
  285. if (rc < 0)
  286. continue;
  287. pr_info("%s: i2c scan: found device @ 0x%x [%s]\n",
  288. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  289. }
  290. }
  291. /* init + register i2c adapter */
  292. int init_bttv_i2c(struct bttv *btv)
  293. {
  294. strlcpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE);
  295. if (i2c_hw)
  296. btv->use_i2c_hw = 1;
  297. if (btv->use_i2c_hw) {
  298. /* bt878 */
  299. strlcpy(btv->c.i2c_adap.name, "bt878",
  300. sizeof(btv->c.i2c_adap.name));
  301. btv->c.i2c_adap.algo = &bttv_algo;
  302. } else {
  303. /* bt848 */
  304. /* Prevents usage of invalid delay values */
  305. if (i2c_udelay<5)
  306. i2c_udelay=5;
  307. strlcpy(btv->c.i2c_adap.name, "bttv",
  308. sizeof(btv->c.i2c_adap.name));
  309. btv->i2c_algo = bttv_i2c_algo_bit_template;
  310. btv->i2c_algo.udelay = i2c_udelay;
  311. btv->i2c_algo.data = btv;
  312. btv->c.i2c_adap.algo_data = &btv->i2c_algo;
  313. }
  314. btv->c.i2c_adap.owner = THIS_MODULE;
  315. btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
  316. snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
  317. "bt%d #%d [%s]", btv->id, btv->c.nr,
  318. btv->use_i2c_hw ? "hw" : "sw");
  319. i2c_set_adapdata(&btv->c.i2c_adap, &btv->c.v4l2_dev);
  320. btv->i2c_client.adapter = &btv->c.i2c_adap;
  321. if (btv->use_i2c_hw) {
  322. btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
  323. } else {
  324. bttv_bit_setscl(btv,1);
  325. bttv_bit_setsda(btv,1);
  326. btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
  327. }
  328. if (0 == btv->i2c_rc && i2c_scan)
  329. do_i2c_scan(btv->c.v4l2_dev.name, &btv->i2c_client);
  330. return btv->i2c_rc;
  331. }
  332. int fini_bttv_i2c(struct bttv *btv)
  333. {
  334. if (btv->i2c_rc == 0)
  335. i2c_del_adapter(&btv->c.i2c_adap);
  336. return 0;
  337. }