bttv-i2c.c 12 KB

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