bttv-i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 attach_inform(struct i2c_client *client);
  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_hw,"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 __devinitdata 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. printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
  128. if (!(xmit & BT878_I2C_NOSTOP))
  129. printk(" >\n");
  130. }
  131. for (cnt = 1; cnt < msg->len; cnt++ ) {
  132. /* following bytes */
  133. xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
  134. if (cnt < msg->len-1 || !last)
  135. xmit |= BT878_I2C_NOSTOP;
  136. btwrite(xmit, BT848_I2C);
  137. retval = bttv_i2c_wait_done(btv);
  138. if (retval < 0)
  139. goto err;
  140. if (retval == 0)
  141. goto eio;
  142. if (i2c_debug) {
  143. printk(" %02x", msg->buf[cnt]);
  144. if (!(xmit & BT878_I2C_NOSTOP))
  145. printk(" >\n");
  146. }
  147. }
  148. return msg->len;
  149. eio:
  150. retval = -EIO;
  151. err:
  152. if (i2c_debug)
  153. printk(" ERR: %d\n",retval);
  154. return retval;
  155. }
  156. static int
  157. bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
  158. {
  159. u32 xmit;
  160. u32 cnt;
  161. int retval;
  162. for(cnt = 0; cnt < msg->len; cnt++) {
  163. xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
  164. if (cnt < msg->len-1)
  165. xmit |= BT848_I2C_W3B;
  166. if (cnt < msg->len-1 || !last)
  167. xmit |= BT878_I2C_NOSTOP;
  168. if (cnt)
  169. xmit |= BT878_I2C_NOSTART;
  170. btwrite(xmit, BT848_I2C);
  171. retval = bttv_i2c_wait_done(btv);
  172. if (retval < 0)
  173. goto err;
  174. if (retval == 0)
  175. goto eio;
  176. msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
  177. if (i2c_debug) {
  178. if (!(xmit & BT878_I2C_NOSTART))
  179. printk(" <R %02x", (msg->addr << 1) +1);
  180. printk(" =%02x", msg->buf[cnt]);
  181. if (!(xmit & BT878_I2C_NOSTOP))
  182. printk(" >\n");
  183. }
  184. }
  185. return msg->len;
  186. eio:
  187. retval = -EIO;
  188. err:
  189. if (i2c_debug)
  190. printk(" ERR: %d\n",retval);
  191. return retval;
  192. }
  193. static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  194. {
  195. struct bttv *btv = i2c_get_adapdata(i2c_adap);
  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. static int attach_inform(struct i2c_client *client)
  225. {
  226. struct bttv *btv = i2c_get_adapdata(client->adapter);
  227. int addr=ADDR_UNSET;
  228. if (ADDR_UNSET != bttv_tvcards[btv->c.type].tuner_addr)
  229. addr = bttv_tvcards[btv->c.type].tuner_addr;
  230. if (bttv_debug)
  231. printk(KERN_DEBUG "bttv%d: %s i2c attach [addr=0x%x,client=%s]\n",
  232. btv->c.nr, client->driver->driver.name, client->addr,
  233. client->name);
  234. if (!client->driver->command)
  235. return 0;
  236. if (client->driver->id == I2C_DRIVERID_MSP3400)
  237. btv->i2c_msp34xx_client = client;
  238. if (client->driver->id == I2C_DRIVERID_TVAUDIO)
  239. btv->i2c_tvaudio_client = client;
  240. if (btv->tuner_type != TUNER_ABSENT) {
  241. struct tuner_setup tun_setup;
  242. if (addr == ADDR_UNSET || addr == client->addr) {
  243. tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV | T_RADIO;
  244. tun_setup.type = btv->tuner_type;
  245. tun_setup.addr = addr;
  246. bttv_call_i2c_clients(btv, TUNER_SET_TYPE_ADDR, &tun_setup);
  247. }
  248. }
  249. return 0;
  250. }
  251. void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
  252. {
  253. if (0 != btv->i2c_rc)
  254. return;
  255. i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
  256. }
  257. /* read I2C */
  258. int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
  259. {
  260. unsigned char buffer = 0;
  261. if (0 != btv->i2c_rc)
  262. return -1;
  263. if (bttv_verbose && NULL != probe_for)
  264. printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
  265. btv->c.nr,probe_for,addr);
  266. btv->i2c_client.addr = addr >> 1;
  267. if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
  268. if (NULL != probe_for) {
  269. if (bttv_verbose)
  270. printk("not found\n");
  271. } else
  272. printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
  273. btv->c.nr,addr);
  274. return -1;
  275. }
  276. if (bttv_verbose && NULL != probe_for)
  277. printk("found\n");
  278. return buffer;
  279. }
  280. /* write I2C */
  281. int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
  282. unsigned char b2, int both)
  283. {
  284. unsigned char buffer[2];
  285. int bytes = both ? 2 : 1;
  286. if (0 != btv->i2c_rc)
  287. return -1;
  288. btv->i2c_client.addr = addr >> 1;
  289. buffer[0] = b1;
  290. buffer[1] = b2;
  291. if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
  292. return -1;
  293. return 0;
  294. }
  295. /* read EEPROM content */
  296. void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
  297. {
  298. memset(eedata, 0, 256);
  299. if (0 != btv->i2c_rc)
  300. return;
  301. btv->i2c_client.addr = addr >> 1;
  302. tveeprom_read(&btv->i2c_client, eedata, 256);
  303. }
  304. static char *i2c_devs[128] = {
  305. [ 0x1c >> 1 ] = "lgdt330x",
  306. [ 0x30 >> 1 ] = "IR (hauppauge)",
  307. [ 0x80 >> 1 ] = "msp34xx",
  308. [ 0x86 >> 1 ] = "tda9887",
  309. [ 0xa0 >> 1 ] = "eeprom",
  310. [ 0xc0 >> 1 ] = "tuner (analog)",
  311. [ 0xc2 >> 1 ] = "tuner (analog)",
  312. };
  313. static void do_i2c_scan(char *name, struct i2c_client *c)
  314. {
  315. unsigned char buf;
  316. int i,rc;
  317. for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
  318. c->addr = i;
  319. rc = i2c_master_recv(c,&buf,0);
  320. if (rc < 0)
  321. continue;
  322. printk("%s: i2c scan: found device @ 0x%x [%s]\n",
  323. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  324. }
  325. }
  326. /* init + register i2c algo-bit adapter */
  327. int __devinit init_bttv_i2c(struct bttv *btv)
  328. {
  329. strlcpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE);
  330. if (i2c_hw)
  331. btv->use_i2c_hw = 1;
  332. if (btv->use_i2c_hw) {
  333. /* bt878 */
  334. strlcpy(btv->c.i2c_adap.name, "bt878",
  335. sizeof(btv->c.i2c_adap.name));
  336. btv->c.i2c_adap.id = I2C_HW_B_BT848; /* FIXME */
  337. btv->c.i2c_adap.algo = &bttv_algo;
  338. } else {
  339. /* bt848 */
  340. /* Prevents usage of invalid delay values */
  341. if (i2c_udelay<5)
  342. i2c_udelay=5;
  343. strlcpy(btv->c.i2c_adap.name, "bttv",
  344. sizeof(btv->c.i2c_adap.name));
  345. btv->c.i2c_adap.id = I2C_HW_B_BT848;
  346. memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
  347. sizeof(bttv_i2c_algo_bit_template));
  348. btv->i2c_algo.udelay = i2c_udelay;
  349. btv->i2c_algo.data = btv;
  350. btv->c.i2c_adap.algo_data = &btv->i2c_algo;
  351. }
  352. btv->c.i2c_adap.owner = THIS_MODULE;
  353. btv->c.i2c_adap.class = I2C_CLASS_TV_ANALOG;
  354. btv->c.i2c_adap.client_register = attach_inform;
  355. btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
  356. snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
  357. "bt%d #%d [%s]", btv->id, btv->c.nr,
  358. btv->use_i2c_hw ? "hw" : "sw");
  359. i2c_set_adapdata(&btv->c.i2c_adap, btv);
  360. btv->i2c_client.adapter = &btv->c.i2c_adap;
  361. if (bttv_tvcards[btv->c.type].no_video)
  362. btv->c.i2c_adap.class &= ~I2C_CLASS_TV_ANALOG;
  363. if (bttv_tvcards[btv->c.type].has_dvb)
  364. btv->c.i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
  365. if (btv->use_i2c_hw) {
  366. btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
  367. } else {
  368. bttv_bit_setscl(btv,1);
  369. bttv_bit_setsda(btv,1);
  370. btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
  371. }
  372. if (0 == btv->i2c_rc && i2c_scan)
  373. do_i2c_scan(btv->c.name,&btv->i2c_client);
  374. return btv->i2c_rc;
  375. }
  376. int __devexit fini_bttv_i2c(struct bttv *btv)
  377. {
  378. if (0 != btv->i2c_rc)
  379. return 0;
  380. return i2c_del_adapter(&btv->c.i2c_adap);
  381. }
  382. /*
  383. * Local variables:
  384. * c-basic-offset: 8
  385. * End:
  386. */