/drivers/media/video/bt8xx/bttv-i2c.c

https://gitlab.com/TeamCarbonXtreme/android_kernel_samsung_msm7x27 · C · 430 lines · 314 code · 55 blank · 61 comment · 77 complexity · 52d726c99618b924bfc7132ccd5670fe MD5 · raw file

  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_debug, "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.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. memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
  312. sizeof(bttv_i2c_algo_bit_template));
  313. btv->i2c_algo.udelay = i2c_udelay;
  314. btv->i2c_algo.data = btv;
  315. btv->c.i2c_adap.algo_data = &btv->i2c_algo;
  316. }
  317. btv->c.i2c_adap.owner = THIS_MODULE;
  318. btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
  319. snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
  320. "bt%d #%d [%s]", btv->id, btv->c.nr,
  321. btv->use_i2c_hw ? "hw" : "sw");
  322. i2c_set_adapdata(&btv->c.i2c_adap, &btv->c.v4l2_dev);
  323. btv->i2c_client.adapter = &btv->c.i2c_adap;
  324. if (btv->use_i2c_hw) {
  325. btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
  326. } else {
  327. bttv_bit_setscl(btv,1);
  328. bttv_bit_setsda(btv,1);
  329. btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
  330. }
  331. if (0 == btv->i2c_rc && i2c_scan)
  332. do_i2c_scan(btv->c.v4l2_dev.name, &btv->i2c_client);
  333. return btv->i2c_rc;
  334. }
  335. /* Instantiate the I2C IR receiver device, if present */
  336. void __devinit init_bttv_i2c_ir(struct bttv *btv)
  337. {
  338. if (0 == btv->i2c_rc) {
  339. struct i2c_board_info info;
  340. /* The external IR receiver is at i2c address 0x34 (0x35 for
  341. reads). Future Hauppauge cards will have an internal
  342. receiver at 0x30 (0x31 for reads). In theory, both can be
  343. fitted, and Hauppauge suggest an external overrides an
  344. internal.
  345. That's why we probe 0x1a (~0x34) first. CB
  346. */
  347. const unsigned short addr_list[] = {
  348. 0x1a, 0x18, 0x4b, 0x64, 0x30, 0x71,
  349. I2C_CLIENT_END
  350. };
  351. memset(&info, 0, sizeof(struct i2c_board_info));
  352. strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
  353. i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list);
  354. }
  355. }
  356. int __devexit fini_bttv_i2c(struct bttv *btv)
  357. {
  358. if (0 != btv->i2c_rc)
  359. return 0;
  360. return i2c_del_adapter(&btv->c.i2c_adap);
  361. }
  362. /*
  363. * Local variables:
  364. * c-basic-offset: 8
  365. * End:
  366. */