cx231xx-i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. cx231xx-i2c.c - driver for Conexant Cx23100/101/102 USB video capture devices
  3. Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  4. Based on em28xx driver
  5. Based on Cx23885 driver
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/usb.h>
  21. #include <linux/i2c.h>
  22. #include <media/v4l2-common.h>
  23. #include <media/tuner.h>
  24. #include "cx231xx.h"
  25. /* ----------------------------------------------------------- */
  26. static unsigned int i2c_scan;
  27. module_param(i2c_scan, int, 0444);
  28. MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
  29. static unsigned int i2c_debug;
  30. module_param(i2c_debug, int, 0644);
  31. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  32. #define dprintk1(lvl, fmt, args...) \
  33. do { \
  34. if (i2c_debug >= lvl) { \
  35. printk(fmt, ##args); \
  36. } \
  37. } while (0)
  38. #define dprintk2(lvl, fmt, args...) \
  39. do { \
  40. if (i2c_debug >= lvl) { \
  41. printk(KERN_DEBUG "%s at %s: " fmt, \
  42. dev->name, __func__ , ##args); \
  43. } \
  44. } while (0)
  45. /*
  46. * cx231xx_i2c_send_bytes()
  47. */
  48. int cx231xx_i2c_send_bytes(struct i2c_adapter *i2c_adap,
  49. const struct i2c_msg *msg)
  50. {
  51. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  52. struct cx231xx *dev = bus->dev;
  53. struct cx231xx_i2c_xfer_data req_data;
  54. int status = 0;
  55. u16 size = 0;
  56. u8 loop = 0;
  57. u8 saddr_len = 1;
  58. u8 *buf_ptr = NULL;
  59. u16 saddr = 0;
  60. u8 need_gpio = 0;
  61. if ((bus->nr == 1) && (msg->addr == 0x61)
  62. && (dev->tuner_type == TUNER_XC5000)) {
  63. size = msg->len;
  64. if (size == 2) { /* register write sub addr */
  65. /* Just writing sub address will cause problem
  66. * to XC5000. So ignore the request */
  67. return 0;
  68. } else if (size == 4) { /* register write with sub addr */
  69. if (msg->len >= 2)
  70. saddr = msg->buf[0] << 8 | msg->buf[1];
  71. else if (msg->len == 1)
  72. saddr = msg->buf[0];
  73. switch (saddr) {
  74. case 0x0000: /* start tuner calibration mode */
  75. need_gpio = 1;
  76. /* FW Loading is done */
  77. dev->xc_fw_load_done = 1;
  78. break;
  79. case 0x000D: /* Set signal source */
  80. case 0x0001: /* Set TV standard - Video */
  81. case 0x0002: /* Set TV standard - Audio */
  82. case 0x0003: /* Set RF Frequency */
  83. need_gpio = 1;
  84. break;
  85. default:
  86. if (dev->xc_fw_load_done)
  87. need_gpio = 1;
  88. break;
  89. }
  90. if (need_gpio) {
  91. dprintk1(1,
  92. "GPIO WRITE: addr 0x%x, len %d, saddr 0x%x\n",
  93. msg->addr, msg->len, saddr);
  94. return dev->cx231xx_gpio_i2c_write(dev,
  95. msg->addr,
  96. msg->buf,
  97. msg->len);
  98. }
  99. }
  100. /* special case for Xc5000 tuner case */
  101. saddr_len = 1;
  102. /* adjust the length to correct length */
  103. size -= saddr_len;
  104. buf_ptr = (u8 *) (msg->buf + 1);
  105. do {
  106. /* prepare xfer_data struct */
  107. req_data.dev_addr = msg->addr;
  108. req_data.direction = msg->flags;
  109. req_data.saddr_len = saddr_len;
  110. req_data.saddr_dat = msg->buf[0];
  111. req_data.buf_size = size > 16 ? 16 : size;
  112. req_data.p_buffer = (u8 *) (buf_ptr + loop * 16);
  113. bus->i2c_nostop = (size > 16) ? 1 : 0;
  114. bus->i2c_reserve = (loop == 0) ? 0 : 1;
  115. /* usb send command */
  116. status = dev->cx231xx_send_usb_command(bus, &req_data);
  117. loop++;
  118. if (size >= 16)
  119. size -= 16;
  120. else
  121. size = 0;
  122. } while (size > 0);
  123. bus->i2c_nostop = 0;
  124. bus->i2c_reserve = 0;
  125. } else { /* regular case */
  126. /* prepare xfer_data struct */
  127. req_data.dev_addr = msg->addr;
  128. req_data.direction = msg->flags;
  129. req_data.saddr_len = 0;
  130. req_data.saddr_dat = 0;
  131. req_data.buf_size = msg->len;
  132. req_data.p_buffer = msg->buf;
  133. /* usb send command */
  134. status = dev->cx231xx_send_usb_command(bus, &req_data);
  135. }
  136. return status < 0 ? status : 0;
  137. }
  138. /*
  139. * cx231xx_i2c_recv_bytes()
  140. * read a byte from the i2c device
  141. */
  142. static int cx231xx_i2c_recv_bytes(struct i2c_adapter *i2c_adap,
  143. const struct i2c_msg *msg)
  144. {
  145. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  146. struct cx231xx *dev = bus->dev;
  147. struct cx231xx_i2c_xfer_data req_data;
  148. int status = 0;
  149. u16 saddr = 0;
  150. u8 need_gpio = 0;
  151. if ((bus->nr == 1) && (msg->addr == 0x61)
  152. && dev->tuner_type == TUNER_XC5000) {
  153. if (msg->len == 2)
  154. saddr = msg->buf[0] << 8 | msg->buf[1];
  155. else if (msg->len == 1)
  156. saddr = msg->buf[0];
  157. if (dev->xc_fw_load_done) {
  158. switch (saddr) {
  159. case 0x0009: /* BUSY check */
  160. dprintk1(1,
  161. "GPIO R E A D: Special case BUSY check \n");
  162. /*Try read BUSY register, just set it to zero*/
  163. msg->buf[0] = 0;
  164. if (msg->len == 2)
  165. msg->buf[1] = 0;
  166. return 0;
  167. case 0x0004: /* read Lock status */
  168. need_gpio = 1;
  169. break;
  170. }
  171. if (need_gpio) {
  172. /* this is a special case to handle Xceive tuner
  173. clock stretch issue with gpio based I2C */
  174. dprintk1(1,
  175. "GPIO R E A D: addr 0x%x, len %d, saddr 0x%x\n",
  176. msg->addr, msg->len,
  177. msg->buf[0] << 8 | msg->buf[1]);
  178. status =
  179. dev->cx231xx_gpio_i2c_write(dev, msg->addr,
  180. msg->buf,
  181. msg->len);
  182. status =
  183. dev->cx231xx_gpio_i2c_read(dev, msg->addr,
  184. msg->buf,
  185. msg->len);
  186. return status;
  187. }
  188. }
  189. /* prepare xfer_data struct */
  190. req_data.dev_addr = msg->addr;
  191. req_data.direction = msg->flags;
  192. req_data.saddr_len = msg->len;
  193. req_data.saddr_dat = msg->buf[0] << 8 | msg->buf[1];
  194. req_data.buf_size = msg->len;
  195. req_data.p_buffer = msg->buf;
  196. /* usb send command */
  197. status = dev->cx231xx_send_usb_command(bus, &req_data);
  198. } else {
  199. /* prepare xfer_data struct */
  200. req_data.dev_addr = msg->addr;
  201. req_data.direction = msg->flags;
  202. req_data.saddr_len = 0;
  203. req_data.saddr_dat = 0;
  204. req_data.buf_size = msg->len;
  205. req_data.p_buffer = msg->buf;
  206. /* usb send command */
  207. status = dev->cx231xx_send_usb_command(bus, &req_data);
  208. }
  209. return status < 0 ? status : 0;
  210. }
  211. /*
  212. * cx231xx_i2c_recv_bytes_with_saddr()
  213. * read a byte from the i2c device
  214. */
  215. static int cx231xx_i2c_recv_bytes_with_saddr(struct i2c_adapter *i2c_adap,
  216. const struct i2c_msg *msg1,
  217. const struct i2c_msg *msg2)
  218. {
  219. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  220. struct cx231xx *dev = bus->dev;
  221. struct cx231xx_i2c_xfer_data req_data;
  222. int status = 0;
  223. u16 saddr = 0;
  224. u8 need_gpio = 0;
  225. if (msg1->len == 2)
  226. saddr = msg1->buf[0] << 8 | msg1->buf[1];
  227. else if (msg1->len == 1)
  228. saddr = msg1->buf[0];
  229. if ((bus->nr == 1) && (msg2->addr == 0x61)
  230. && dev->tuner_type == TUNER_XC5000) {
  231. if ((msg2->len < 16)) {
  232. dprintk1(1,
  233. "i2c_read: addr 0x%x, len %d, saddr 0x%x, len %d\n",
  234. msg2->addr, msg2->len, saddr, msg1->len);
  235. switch (saddr) {
  236. case 0x0008: /* read FW load status */
  237. need_gpio = 1;
  238. break;
  239. case 0x0004: /* read Lock status */
  240. need_gpio = 1;
  241. break;
  242. }
  243. if (need_gpio) {
  244. status =
  245. dev->cx231xx_gpio_i2c_write(dev, msg1->addr,
  246. msg1->buf,
  247. msg1->len);
  248. status =
  249. dev->cx231xx_gpio_i2c_read(dev, msg2->addr,
  250. msg2->buf,
  251. msg2->len);
  252. return status;
  253. }
  254. }
  255. }
  256. /* prepare xfer_data struct */
  257. req_data.dev_addr = msg2->addr;
  258. req_data.direction = msg2->flags;
  259. req_data.saddr_len = msg1->len;
  260. req_data.saddr_dat = saddr;
  261. req_data.buf_size = msg2->len;
  262. req_data.p_buffer = msg2->buf;
  263. /* usb send command */
  264. status = dev->cx231xx_send_usb_command(bus, &req_data);
  265. return status < 0 ? status : 0;
  266. }
  267. /*
  268. * cx231xx_i2c_check_for_device()
  269. * check if there is a i2c_device at the supplied address
  270. */
  271. static int cx231xx_i2c_check_for_device(struct i2c_adapter *i2c_adap,
  272. const struct i2c_msg *msg)
  273. {
  274. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  275. struct cx231xx *dev = bus->dev;
  276. struct cx231xx_i2c_xfer_data req_data;
  277. int status = 0;
  278. /* prepare xfer_data struct */
  279. req_data.dev_addr = msg->addr;
  280. req_data.direction = msg->flags;
  281. req_data.saddr_len = 0;
  282. req_data.saddr_dat = 0;
  283. req_data.buf_size = 0;
  284. req_data.p_buffer = NULL;
  285. /* usb send command */
  286. status = dev->cx231xx_send_usb_command(bus, &req_data);
  287. return status < 0 ? status : 0;
  288. }
  289. /*
  290. * cx231xx_i2c_xfer()
  291. * the main i2c transfer function
  292. */
  293. static int cx231xx_i2c_xfer(struct i2c_adapter *i2c_adap,
  294. struct i2c_msg msgs[], int num)
  295. {
  296. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  297. struct cx231xx *dev = bus->dev;
  298. int addr, rc, i, byte;
  299. if (num <= 0)
  300. return 0;
  301. for (i = 0; i < num; i++) {
  302. addr = msgs[i].addr >> 1;
  303. dprintk2(2, "%s %s addr=%x len=%d:",
  304. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  305. i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
  306. if (!msgs[i].len) {
  307. /* no len: check only for device presence */
  308. rc = cx231xx_i2c_check_for_device(i2c_adap, &msgs[i]);
  309. if (rc < 0) {
  310. dprintk2(2, " no device\n");
  311. return rc;
  312. }
  313. } else if (msgs[i].flags & I2C_M_RD) {
  314. /* read bytes */
  315. rc = cx231xx_i2c_recv_bytes(i2c_adap, &msgs[i]);
  316. if (i2c_debug >= 2) {
  317. for (byte = 0; byte < msgs[i].len; byte++)
  318. printk(" %02x", msgs[i].buf[byte]);
  319. }
  320. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  321. msgs[i].addr == msgs[i + 1].addr
  322. && (msgs[i].len <= 2) && (bus->nr < 2)) {
  323. /* read bytes */
  324. rc = cx231xx_i2c_recv_bytes_with_saddr(i2c_adap,
  325. &msgs[i],
  326. &msgs[i + 1]);
  327. if (i2c_debug >= 2) {
  328. for (byte = 0; byte < msgs[i].len; byte++)
  329. printk(" %02x", msgs[i].buf[byte]);
  330. }
  331. i++;
  332. } else {
  333. /* write bytes */
  334. if (i2c_debug >= 2) {
  335. for (byte = 0; byte < msgs[i].len; byte++)
  336. printk(" %02x", msgs[i].buf[byte]);
  337. }
  338. rc = cx231xx_i2c_send_bytes(i2c_adap, &msgs[i]);
  339. }
  340. if (rc < 0)
  341. goto err;
  342. if (i2c_debug >= 2)
  343. printk("\n");
  344. }
  345. return num;
  346. err:
  347. dprintk2(2, " ERROR: %i\n", rc);
  348. return rc;
  349. }
  350. /* ----------------------------------------------------------- */
  351. /*
  352. * functionality()
  353. */
  354. static u32 functionality(struct i2c_adapter *adap)
  355. {
  356. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  357. }
  358. static struct i2c_algorithm cx231xx_algo = {
  359. .master_xfer = cx231xx_i2c_xfer,
  360. .functionality = functionality,
  361. };
  362. static struct i2c_adapter cx231xx_adap_template = {
  363. .owner = THIS_MODULE,
  364. .name = "cx231xx",
  365. .algo = &cx231xx_algo,
  366. };
  367. static struct i2c_client cx231xx_client_template = {
  368. .name = "cx231xx internal",
  369. };
  370. /* ----------------------------------------------------------- */
  371. /*
  372. * i2c_devs
  373. * incomplete list of known devices
  374. */
  375. static char *i2c_devs[128] = {
  376. [0x60 >> 1] = "colibri",
  377. [0x88 >> 1] = "hammerhead",
  378. [0x8e >> 1] = "CIR",
  379. [0x32 >> 1] = "GeminiIII",
  380. [0x02 >> 1] = "Aquarius",
  381. [0xa0 >> 1] = "eeprom",
  382. [0xc0 >> 1] = "tuner/XC3028",
  383. [0xc2 >> 1] = "tuner/XC5000",
  384. };
  385. /*
  386. * cx231xx_do_i2c_scan()
  387. * check i2c address range for devices
  388. */
  389. void cx231xx_do_i2c_scan(struct cx231xx *dev, struct i2c_client *c)
  390. {
  391. unsigned char buf;
  392. int i, rc;
  393. cx231xx_info(": Checking for I2C devices ..\n");
  394. for (i = 0; i < 128; i++) {
  395. c->addr = i;
  396. rc = i2c_master_recv(c, &buf, 0);
  397. if (rc < 0)
  398. continue;
  399. cx231xx_info("%s: i2c scan: found device @ 0x%x [%s]\n",
  400. dev->name, i << 1,
  401. i2c_devs[i] ? i2c_devs[i] : "???");
  402. }
  403. cx231xx_info(": Completed Checking for I2C devices.\n");
  404. }
  405. /*
  406. * cx231xx_i2c_register()
  407. * register i2c bus
  408. */
  409. int cx231xx_i2c_register(struct cx231xx_i2c *bus)
  410. {
  411. struct cx231xx *dev = bus->dev;
  412. BUG_ON(!dev->cx231xx_send_usb_command);
  413. memcpy(&bus->i2c_adap, &cx231xx_adap_template, sizeof(bus->i2c_adap));
  414. memcpy(&bus->i2c_algo, &cx231xx_algo, sizeof(bus->i2c_algo));
  415. memcpy(&bus->i2c_client, &cx231xx_client_template,
  416. sizeof(bus->i2c_client));
  417. bus->i2c_adap.dev.parent = &dev->udev->dev;
  418. strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
  419. bus->i2c_algo.data = bus;
  420. bus->i2c_adap.algo_data = bus;
  421. i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
  422. i2c_add_adapter(&bus->i2c_adap);
  423. bus->i2c_client.adapter = &bus->i2c_adap;
  424. if (0 == bus->i2c_rc) {
  425. if (i2c_scan)
  426. cx231xx_do_i2c_scan(dev, &bus->i2c_client);
  427. /* Instantiate the IR receiver device, if present */
  428. cx231xx_register_i2c_ir(dev);
  429. } else
  430. cx231xx_warn("%s: i2c bus %d register FAILED\n",
  431. dev->name, bus->nr);
  432. return bus->i2c_rc;
  433. }
  434. /*
  435. * cx231xx_i2c_unregister()
  436. * unregister i2c_bus
  437. */
  438. int cx231xx_i2c_unregister(struct cx231xx_i2c *bus)
  439. {
  440. i2c_del_adapter(&bus->i2c_adap);
  441. return 0;
  442. }