bttv-i2c.c 11 KB

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