bttv-i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include "bttvp.h"
  24. #include <linux/jiffies.h>
  25. #include <asm/io.h>
  26. static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
  27. static struct i2c_adapter bttv_i2c_adap_sw_template;
  28. static struct i2c_adapter bttv_i2c_adap_hw_template;
  29. static struct i2c_client bttv_i2c_client_template;
  30. static int attach_inform(struct i2c_client *client);
  31. static int i2c_debug = 0;
  32. static int i2c_hw = 0;
  33. static int i2c_scan = 0;
  34. module_param(i2c_debug, int, 0644);
  35. module_param(i2c_hw, int, 0444);
  36. module_param(i2c_scan, int, 0444);
  37. MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
  38. /* ----------------------------------------------------------------------- */
  39. /* I2C functions - bitbanging adapter (software i2c) */
  40. static void bttv_bit_setscl(void *data, int state)
  41. {
  42. struct bttv *btv = (struct bttv*)data;
  43. if (state)
  44. btv->i2c_state |= 0x02;
  45. else
  46. btv->i2c_state &= ~0x02;
  47. btwrite(btv->i2c_state, BT848_I2C);
  48. btread(BT848_I2C);
  49. }
  50. static void bttv_bit_setsda(void *data, int state)
  51. {
  52. struct bttv *btv = (struct bttv*)data;
  53. if (state)
  54. btv->i2c_state |= 0x01;
  55. else
  56. btv->i2c_state &= ~0x01;
  57. btwrite(btv->i2c_state, BT848_I2C);
  58. btread(BT848_I2C);
  59. }
  60. static int bttv_bit_getscl(void *data)
  61. {
  62. struct bttv *btv = (struct bttv*)data;
  63. int state;
  64. state = btread(BT848_I2C) & 0x02 ? 1 : 0;
  65. return state;
  66. }
  67. static int bttv_bit_getsda(void *data)
  68. {
  69. struct bttv *btv = (struct bttv*)data;
  70. int state;
  71. state = btread(BT848_I2C) & 0x01;
  72. return state;
  73. }
  74. static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
  75. .setsda = bttv_bit_setsda,
  76. .setscl = bttv_bit_setscl,
  77. .getsda = bttv_bit_getsda,
  78. .getscl = bttv_bit_getscl,
  79. .udelay = 16,
  80. .mdelay = 10,
  81. .timeout = 200,
  82. };
  83. static struct i2c_adapter bttv_i2c_adap_sw_template = {
  84. .owner = THIS_MODULE,
  85. .class = I2C_CLASS_TV_ANALOG,
  86. .name = "bt848",
  87. .id = I2C_HW_B_BT848,
  88. .client_register = attach_inform,
  89. };
  90. /* ----------------------------------------------------------------------- */
  91. /* I2C functions - hardware i2c */
  92. static int algo_control(struct i2c_adapter *adapter,
  93. unsigned int cmd, unsigned long arg)
  94. {
  95. return 0;
  96. }
  97. static u32 functionality(struct i2c_adapter *adap)
  98. {
  99. return I2C_FUNC_SMBUS_EMUL;
  100. }
  101. static int
  102. bttv_i2c_wait_done(struct bttv *btv)
  103. {
  104. int rc = 0;
  105. /* timeout */
  106. if (wait_event_interruptible_timeout(btv->i2c_queue,
  107. btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
  108. rc = -EIO;
  109. if (btv->i2c_done & BT848_INT_RACK)
  110. rc = 1;
  111. btv->i2c_done = 0;
  112. return rc;
  113. }
  114. #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
  115. BT848_I2C_SCL | BT848_I2C_SDA)
  116. static int
  117. bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
  118. {
  119. u32 xmit;
  120. int retval,cnt;
  121. /* sanity checks */
  122. if (0 == msg->len)
  123. return -EINVAL;
  124. /* start, address + first byte */
  125. xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
  126. if (msg->len > 1 || !last)
  127. xmit |= BT878_I2C_NOSTOP;
  128. btwrite(xmit, BT848_I2C);
  129. retval = bttv_i2c_wait_done(btv);
  130. if (retval < 0)
  131. goto err;
  132. if (retval == 0)
  133. goto eio;
  134. if (i2c_debug) {
  135. printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
  136. if (!(xmit & BT878_I2C_NOSTOP))
  137. printk(" >\n");
  138. }
  139. for (cnt = 1; cnt < msg->len; cnt++ ) {
  140. /* following bytes */
  141. xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
  142. if (cnt < msg->len-1 || !last)
  143. xmit |= BT878_I2C_NOSTOP;
  144. btwrite(xmit, BT848_I2C);
  145. retval = bttv_i2c_wait_done(btv);
  146. if (retval < 0)
  147. goto err;
  148. if (retval == 0)
  149. goto eio;
  150. if (i2c_debug) {
  151. printk(" %02x", msg->buf[cnt]);
  152. if (!(xmit & BT878_I2C_NOSTOP))
  153. printk(" >\n");
  154. }
  155. }
  156. return msg->len;
  157. eio:
  158. retval = -EIO;
  159. err:
  160. if (i2c_debug)
  161. printk(" ERR: %d\n",retval);
  162. return retval;
  163. }
  164. static int
  165. bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
  166. {
  167. u32 xmit;
  168. u32 cnt;
  169. int retval;
  170. for(cnt = 0; cnt < msg->len; cnt++) {
  171. xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
  172. if (cnt < msg->len-1)
  173. xmit |= BT848_I2C_W3B;
  174. if (cnt < msg->len-1 || !last)
  175. xmit |= BT878_I2C_NOSTOP;
  176. if (cnt)
  177. xmit |= BT878_I2C_NOSTART;
  178. btwrite(xmit, BT848_I2C);
  179. retval = bttv_i2c_wait_done(btv);
  180. if (retval < 0)
  181. goto err;
  182. if (retval == 0)
  183. goto eio;
  184. msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
  185. if (i2c_debug) {
  186. if (!(xmit & BT878_I2C_NOSTART))
  187. printk(" <R %02x", (msg->addr << 1) +1);
  188. printk(" =%02x", msg->buf[cnt]);
  189. if (!(xmit & BT878_I2C_NOSTOP))
  190. printk(" >\n");
  191. }
  192. }
  193. return msg->len;
  194. eio:
  195. retval = -EIO;
  196. err:
  197. if (i2c_debug)
  198. printk(" ERR: %d\n",retval);
  199. return retval;
  200. }
  201. static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  202. {
  203. struct bttv *btv = i2c_get_adapdata(i2c_adap);
  204. int retval = 0;
  205. int i;
  206. if (i2c_debug)
  207. printk("bt-i2c:");
  208. btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
  209. for (i = 0 ; i < num; i++) {
  210. if (msgs[i].flags & I2C_M_RD) {
  211. /* read */
  212. retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
  213. if (retval < 0)
  214. goto err;
  215. } else {
  216. /* write */
  217. retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
  218. if (retval < 0)
  219. goto err;
  220. }
  221. }
  222. return num;
  223. err:
  224. return retval;
  225. }
  226. static struct i2c_algorithm bttv_algo = {
  227. .master_xfer = bttv_i2c_xfer,
  228. .algo_control = algo_control,
  229. .functionality = functionality,
  230. };
  231. static struct i2c_adapter bttv_i2c_adap_hw_template = {
  232. .owner = THIS_MODULE,
  233. .class = I2C_CLASS_TV_ANALOG,
  234. .name = "bt878",
  235. .id = I2C_HW_B_BT848 /* FIXME */,
  236. .algo = &bttv_algo,
  237. .client_register = attach_inform,
  238. };
  239. /* ----------------------------------------------------------------------- */
  240. /* I2C functions - common stuff */
  241. static int attach_inform(struct i2c_client *client)
  242. {
  243. struct bttv *btv = i2c_get_adapdata(client->adapter);
  244. int addr=ADDR_UNSET;
  245. if (ADDR_UNSET != bttv_tvcards[btv->c.type].tuner_addr)
  246. addr = bttv_tvcards[btv->c.type].tuner_addr;
  247. if (bttv_debug)
  248. printk(KERN_DEBUG "bttv%d: %s i2c attach [addr=0x%x,client=%s]\n",
  249. btv->c.nr, client->driver->driver.name, client->addr,
  250. client->name);
  251. if (!client->driver->command)
  252. return 0;
  253. if (btv->tuner_type != UNSET) {
  254. struct tuner_setup tun_setup;
  255. if ((addr==ADDR_UNSET) ||
  256. (addr==client->addr)) {
  257. tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV | T_RADIO;
  258. tun_setup.type = btv->tuner_type;
  259. tun_setup.addr = addr;
  260. bttv_call_i2c_clients(btv, TUNER_SET_TYPE_ADDR, &tun_setup);
  261. }
  262. }
  263. return 0;
  264. }
  265. void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
  266. {
  267. if (0 != btv->i2c_rc)
  268. return;
  269. i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
  270. }
  271. static struct i2c_client bttv_i2c_client_template = {
  272. .name = "bttv internal",
  273. };
  274. /* read I2C */
  275. int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
  276. {
  277. unsigned char buffer = 0;
  278. if (0 != btv->i2c_rc)
  279. return -1;
  280. if (bttv_verbose && NULL != probe_for)
  281. printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
  282. btv->c.nr,probe_for,addr);
  283. btv->i2c_client.addr = addr >> 1;
  284. if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
  285. if (NULL != probe_for) {
  286. if (bttv_verbose)
  287. printk("not found\n");
  288. } else
  289. printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
  290. btv->c.nr,addr);
  291. return -1;
  292. }
  293. if (bttv_verbose && NULL != probe_for)
  294. printk("found\n");
  295. return buffer;
  296. }
  297. /* write I2C */
  298. int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
  299. unsigned char b2, int both)
  300. {
  301. unsigned char buffer[2];
  302. int bytes = both ? 2 : 1;
  303. if (0 != btv->i2c_rc)
  304. return -1;
  305. btv->i2c_client.addr = addr >> 1;
  306. buffer[0] = b1;
  307. buffer[1] = b2;
  308. if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
  309. return -1;
  310. return 0;
  311. }
  312. /* read EEPROM content */
  313. void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
  314. {
  315. memset(eedata, 0, 256);
  316. if (0 != btv->i2c_rc)
  317. return;
  318. btv->i2c_client.addr = addr >> 1;
  319. tveeprom_read(&btv->i2c_client, eedata, 256);
  320. }
  321. static char *i2c_devs[128] = {
  322. [ 0x1c >> 1 ] = "lgdt330x",
  323. [ 0x30 >> 1 ] = "IR (hauppauge)",
  324. [ 0x80 >> 1 ] = "msp34xx",
  325. [ 0x86 >> 1 ] = "tda9887",
  326. [ 0xa0 >> 1 ] = "eeprom",
  327. [ 0xc0 >> 1 ] = "tuner (analog)",
  328. [ 0xc2 >> 1 ] = "tuner (analog)",
  329. };
  330. static void do_i2c_scan(char *name, struct i2c_client *c)
  331. {
  332. unsigned char buf;
  333. int i,rc;
  334. for (i = 0; i < 128; i++) {
  335. c->addr = i;
  336. rc = i2c_master_recv(c,&buf,0);
  337. if (rc < 0)
  338. continue;
  339. printk("%s: i2c scan: found device @ 0x%x [%s]\n",
  340. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  341. }
  342. }
  343. /* init + register i2c algo-bit adapter */
  344. int __devinit init_bttv_i2c(struct bttv *btv)
  345. {
  346. memcpy(&btv->i2c_client, &bttv_i2c_client_template,
  347. sizeof(bttv_i2c_client_template));
  348. if (i2c_hw)
  349. btv->use_i2c_hw = 1;
  350. if (btv->use_i2c_hw) {
  351. /* bt878 */
  352. memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_hw_template,
  353. sizeof(bttv_i2c_adap_hw_template));
  354. } else {
  355. /* bt848 */
  356. memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_sw_template,
  357. sizeof(bttv_i2c_adap_sw_template));
  358. memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
  359. sizeof(bttv_i2c_algo_bit_template));
  360. btv->i2c_algo.data = btv;
  361. btv->c.i2c_adap.algo_data = &btv->i2c_algo;
  362. }
  363. btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
  364. snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
  365. "bt%d #%d [%s]", btv->id, btv->c.nr,
  366. btv->use_i2c_hw ? "hw" : "sw");
  367. i2c_set_adapdata(&btv->c.i2c_adap, btv);
  368. btv->i2c_client.adapter = &btv->c.i2c_adap;
  369. if (bttv_tvcards[btv->c.type].no_video)
  370. btv->c.i2c_adap.class &= ~I2C_CLASS_TV_ANALOG;
  371. if (bttv_tvcards[btv->c.type].has_dvb)
  372. btv->c.i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
  373. if (btv->use_i2c_hw) {
  374. btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
  375. } else {
  376. bttv_bit_setscl(btv,1);
  377. bttv_bit_setsda(btv,1);
  378. btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
  379. }
  380. if (0 == btv->i2c_rc && i2c_scan)
  381. do_i2c_scan(btv->c.name,&btv->i2c_client);
  382. return btv->i2c_rc;
  383. }
  384. int __devexit fini_bttv_i2c(struct bttv *btv)
  385. {
  386. if (0 != btv->i2c_rc)
  387. return 0;
  388. if (btv->use_i2c_hw) {
  389. return i2c_del_adapter(&btv->c.i2c_adap);
  390. } else {
  391. return i2c_bit_del_bus(&btv->c.i2c_adap);
  392. }
  393. }
  394. /*
  395. * Local variables:
  396. * c-basic-offset: 8
  397. * End:
  398. */