cx231xx-i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. static inline bool is_tuner(struct cx231xx *dev, struct cx231xx_i2c *bus,
  46. const struct i2c_msg *msg, int tuner_type)
  47. {
  48. if (bus->nr != dev->board.tuner_i2c_master)
  49. return false;
  50. if (msg->addr != dev->board.tuner_addr)
  51. return false;
  52. if (dev->tuner_type != tuner_type)
  53. return false;
  54. return true;
  55. }
  56. /*
  57. * cx231xx_i2c_send_bytes()
  58. */
  59. int cx231xx_i2c_send_bytes(struct i2c_adapter *i2c_adap,
  60. const struct i2c_msg *msg)
  61. {
  62. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  63. struct cx231xx *dev = bus->dev;
  64. struct cx231xx_i2c_xfer_data req_data;
  65. int status = 0;
  66. u16 size = 0;
  67. u8 loop = 0;
  68. u8 saddr_len = 1;
  69. u8 *buf_ptr = NULL;
  70. u16 saddr = 0;
  71. u8 need_gpio = 0;
  72. if (is_tuner(dev, bus, msg, TUNER_XC5000)) {
  73. size = msg->len;
  74. if (size == 2) { /* register write sub addr */
  75. /* Just writing sub address will cause problem
  76. * to XC5000. So ignore the request */
  77. return 0;
  78. } else if (size == 4) { /* register write with sub addr */
  79. if (msg->len >= 2)
  80. saddr = msg->buf[0] << 8 | msg->buf[1];
  81. else if (msg->len == 1)
  82. saddr = msg->buf[0];
  83. switch (saddr) {
  84. case 0x0000: /* start tuner calibration mode */
  85. need_gpio = 1;
  86. /* FW Loading is done */
  87. dev->xc_fw_load_done = 1;
  88. break;
  89. case 0x000D: /* Set signal source */
  90. case 0x0001: /* Set TV standard - Video */
  91. case 0x0002: /* Set TV standard - Audio */
  92. case 0x0003: /* Set RF Frequency */
  93. need_gpio = 1;
  94. break;
  95. default:
  96. if (dev->xc_fw_load_done)
  97. need_gpio = 1;
  98. break;
  99. }
  100. if (need_gpio) {
  101. dprintk1(1,
  102. "GPIO WRITE: addr 0x%x, len %d, saddr 0x%x\n",
  103. msg->addr, msg->len, saddr);
  104. return dev->cx231xx_gpio_i2c_write(dev,
  105. msg->addr,
  106. msg->buf,
  107. msg->len);
  108. }
  109. }
  110. /* special case for Xc5000 tuner case */
  111. saddr_len = 1;
  112. /* adjust the length to correct length */
  113. size -= saddr_len;
  114. buf_ptr = (u8 *) (msg->buf + 1);
  115. do {
  116. /* prepare xfer_data struct */
  117. req_data.dev_addr = msg->addr;
  118. req_data.direction = msg->flags;
  119. req_data.saddr_len = saddr_len;
  120. req_data.saddr_dat = msg->buf[0];
  121. req_data.buf_size = size > 16 ? 16 : size;
  122. req_data.p_buffer = (u8 *) (buf_ptr + loop * 16);
  123. bus->i2c_nostop = (size > 16) ? 1 : 0;
  124. bus->i2c_reserve = (loop == 0) ? 0 : 1;
  125. /* usb send command */
  126. status = dev->cx231xx_send_usb_command(bus, &req_data);
  127. loop++;
  128. if (size >= 16)
  129. size -= 16;
  130. else
  131. size = 0;
  132. } while (size > 0);
  133. bus->i2c_nostop = 0;
  134. bus->i2c_reserve = 0;
  135. } else { /* regular case */
  136. /* prepare xfer_data struct */
  137. req_data.dev_addr = msg->addr;
  138. req_data.direction = msg->flags;
  139. req_data.saddr_len = 0;
  140. req_data.saddr_dat = 0;
  141. req_data.buf_size = msg->len;
  142. req_data.p_buffer = msg->buf;
  143. /* usb send command */
  144. status = dev->cx231xx_send_usb_command(bus, &req_data);
  145. }
  146. return status < 0 ? status : 0;
  147. }
  148. /*
  149. * cx231xx_i2c_recv_bytes()
  150. * read a byte from the i2c device
  151. */
  152. static int cx231xx_i2c_recv_bytes(struct i2c_adapter *i2c_adap,
  153. const struct i2c_msg *msg)
  154. {
  155. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  156. struct cx231xx *dev = bus->dev;
  157. struct cx231xx_i2c_xfer_data req_data;
  158. int status = 0;
  159. u16 saddr = 0;
  160. u8 need_gpio = 0;
  161. if (is_tuner(dev, bus, msg, TUNER_XC5000)) {
  162. if (msg->len == 2)
  163. saddr = msg->buf[0] << 8 | msg->buf[1];
  164. else if (msg->len == 1)
  165. saddr = msg->buf[0];
  166. if (dev->xc_fw_load_done) {
  167. switch (saddr) {
  168. case 0x0009: /* BUSY check */
  169. dprintk1(1,
  170. "GPIO R E A D: Special case BUSY check \n");
  171. /*Try read BUSY register, just set it to zero*/
  172. msg->buf[0] = 0;
  173. if (msg->len == 2)
  174. msg->buf[1] = 0;
  175. return 0;
  176. case 0x0004: /* read Lock status */
  177. need_gpio = 1;
  178. break;
  179. }
  180. if (need_gpio) {
  181. /* this is a special case to handle Xceive tuner
  182. clock stretch issue with gpio based I2C */
  183. dprintk1(1,
  184. "GPIO R E A D: addr 0x%x, len %d, saddr 0x%x\n",
  185. msg->addr, msg->len,
  186. msg->buf[0] << 8 | msg->buf[1]);
  187. status =
  188. dev->cx231xx_gpio_i2c_write(dev, msg->addr,
  189. msg->buf,
  190. msg->len);
  191. status =
  192. dev->cx231xx_gpio_i2c_read(dev, msg->addr,
  193. msg->buf,
  194. msg->len);
  195. return status;
  196. }
  197. }
  198. /* prepare xfer_data struct */
  199. req_data.dev_addr = msg->addr;
  200. req_data.direction = msg->flags;
  201. req_data.saddr_len = msg->len;
  202. req_data.saddr_dat = msg->buf[0] << 8 | msg->buf[1];
  203. req_data.buf_size = msg->len;
  204. req_data.p_buffer = msg->buf;
  205. /* usb send command */
  206. status = dev->cx231xx_send_usb_command(bus, &req_data);
  207. } else {
  208. /* prepare xfer_data struct */
  209. req_data.dev_addr = msg->addr;
  210. req_data.direction = msg->flags;
  211. req_data.saddr_len = 0;
  212. req_data.saddr_dat = 0;
  213. req_data.buf_size = msg->len;
  214. req_data.p_buffer = msg->buf;
  215. /* usb send command */
  216. status = dev->cx231xx_send_usb_command(bus, &req_data);
  217. }
  218. return status < 0 ? status : 0;
  219. }
  220. /*
  221. * cx231xx_i2c_recv_bytes_with_saddr()
  222. * read a byte from the i2c device
  223. */
  224. static int cx231xx_i2c_recv_bytes_with_saddr(struct i2c_adapter *i2c_adap,
  225. const struct i2c_msg *msg1,
  226. const struct i2c_msg *msg2)
  227. {
  228. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  229. struct cx231xx *dev = bus->dev;
  230. struct cx231xx_i2c_xfer_data req_data;
  231. int status = 0;
  232. u16 saddr = 0;
  233. u8 need_gpio = 0;
  234. if (msg1->len == 2)
  235. saddr = msg1->buf[0] << 8 | msg1->buf[1];
  236. else if (msg1->len == 1)
  237. saddr = msg1->buf[0];
  238. if (is_tuner(dev, bus, msg2, TUNER_XC5000)) {
  239. if ((msg2->len < 16)) {
  240. dprintk1(1,
  241. "i2c_read: addr 0x%x, len %d, saddr 0x%x, len %d\n",
  242. msg2->addr, msg2->len, saddr, msg1->len);
  243. switch (saddr) {
  244. case 0x0008: /* read FW load status */
  245. need_gpio = 1;
  246. break;
  247. case 0x0004: /* read Lock status */
  248. need_gpio = 1;
  249. break;
  250. }
  251. if (need_gpio) {
  252. status =
  253. dev->cx231xx_gpio_i2c_write(dev, msg1->addr,
  254. msg1->buf,
  255. msg1->len);
  256. status =
  257. dev->cx231xx_gpio_i2c_read(dev, msg2->addr,
  258. msg2->buf,
  259. msg2->len);
  260. return status;
  261. }
  262. }
  263. }
  264. /* prepare xfer_data struct */
  265. req_data.dev_addr = msg2->addr;
  266. req_data.direction = msg2->flags;
  267. req_data.saddr_len = msg1->len;
  268. req_data.saddr_dat = saddr;
  269. req_data.buf_size = msg2->len;
  270. req_data.p_buffer = msg2->buf;
  271. /* usb send command */
  272. status = dev->cx231xx_send_usb_command(bus, &req_data);
  273. return status < 0 ? status : 0;
  274. }
  275. /*
  276. * cx231xx_i2c_check_for_device()
  277. * check if there is a i2c_device at the supplied address
  278. */
  279. static int cx231xx_i2c_check_for_device(struct i2c_adapter *i2c_adap,
  280. const struct i2c_msg *msg)
  281. {
  282. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  283. struct cx231xx *dev = bus->dev;
  284. struct cx231xx_i2c_xfer_data req_data;
  285. int status = 0;
  286. /* prepare xfer_data struct */
  287. req_data.dev_addr = msg->addr;
  288. req_data.direction = msg->flags;
  289. req_data.saddr_len = 0;
  290. req_data.saddr_dat = 0;
  291. req_data.buf_size = 0;
  292. req_data.p_buffer = NULL;
  293. /* usb send command */
  294. status = dev->cx231xx_send_usb_command(bus, &req_data);
  295. return status < 0 ? status : 0;
  296. }
  297. /*
  298. * cx231xx_i2c_xfer()
  299. * the main i2c transfer function
  300. */
  301. static int cx231xx_i2c_xfer(struct i2c_adapter *i2c_adap,
  302. struct i2c_msg msgs[], int num)
  303. {
  304. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  305. struct cx231xx *dev = bus->dev;
  306. int addr, rc, i, byte;
  307. if (num <= 0)
  308. return 0;
  309. mutex_lock(&dev->i2c_lock);
  310. for (i = 0; i < num; i++) {
  311. addr = msgs[i].addr >> 1;
  312. dprintk2(2, "%s %s addr=%x len=%d:",
  313. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  314. i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
  315. if (!msgs[i].len) {
  316. /* no len: check only for device presence */
  317. rc = cx231xx_i2c_check_for_device(i2c_adap, &msgs[i]);
  318. if (rc < 0) {
  319. dprintk2(2, " no device\n");
  320. mutex_unlock(&dev->i2c_lock);
  321. return rc;
  322. }
  323. } else if (msgs[i].flags & I2C_M_RD) {
  324. /* read bytes */
  325. rc = cx231xx_i2c_recv_bytes(i2c_adap, &msgs[i]);
  326. if (i2c_debug >= 2) {
  327. for (byte = 0; byte < msgs[i].len; byte++)
  328. printk(" %02x", msgs[i].buf[byte]);
  329. }
  330. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  331. msgs[i].addr == msgs[i + 1].addr
  332. && (msgs[i].len <= 2) && (bus->nr < 3)) {
  333. /* read bytes */
  334. rc = cx231xx_i2c_recv_bytes_with_saddr(i2c_adap,
  335. &msgs[i],
  336. &msgs[i + 1]);
  337. if (i2c_debug >= 2) {
  338. for (byte = 0; byte < msgs[i].len; byte++)
  339. printk(" %02x", msgs[i].buf[byte]);
  340. }
  341. i++;
  342. } else {
  343. /* write bytes */
  344. if (i2c_debug >= 2) {
  345. for (byte = 0; byte < msgs[i].len; byte++)
  346. printk(" %02x", msgs[i].buf[byte]);
  347. }
  348. rc = cx231xx_i2c_send_bytes(i2c_adap, &msgs[i]);
  349. }
  350. if (rc < 0)
  351. goto err;
  352. if (i2c_debug >= 2)
  353. printk("\n");
  354. }
  355. mutex_unlock(&dev->i2c_lock);
  356. return num;
  357. err:
  358. dprintk2(2, " ERROR: %i\n", rc);
  359. mutex_unlock(&dev->i2c_lock);
  360. return rc;
  361. }
  362. /* ----------------------------------------------------------- */
  363. /*
  364. * functionality()
  365. */
  366. static u32 functionality(struct i2c_adapter *adap)
  367. {
  368. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  369. }
  370. static struct i2c_algorithm cx231xx_algo = {
  371. .master_xfer = cx231xx_i2c_xfer,
  372. .functionality = functionality,
  373. };
  374. static struct i2c_adapter cx231xx_adap_template = {
  375. .owner = THIS_MODULE,
  376. .name = "cx231xx",
  377. .algo = &cx231xx_algo,
  378. };
  379. static struct i2c_client cx231xx_client_template = {
  380. .name = "cx231xx internal",
  381. };
  382. /* ----------------------------------------------------------- */
  383. /*
  384. * i2c_devs
  385. * incomplete list of known devices
  386. */
  387. static char *i2c_devs[128] = {
  388. [0x60 >> 1] = "colibri",
  389. [0x88 >> 1] = "hammerhead",
  390. [0x8e >> 1] = "CIR",
  391. [0x32 >> 1] = "GeminiIII",
  392. [0x02 >> 1] = "Aquarius",
  393. [0xa0 >> 1] = "eeprom",
  394. [0xc0 >> 1] = "tuner",
  395. [0xc2 >> 1] = "tuner",
  396. };
  397. /*
  398. * cx231xx_do_i2c_scan()
  399. * check i2c address range for devices
  400. */
  401. void cx231xx_do_i2c_scan(struct cx231xx *dev, struct i2c_client *c)
  402. {
  403. unsigned char buf;
  404. int i, rc;
  405. cx231xx_info(": Checking for I2C devices ..\n");
  406. for (i = 0; i < 128; i++) {
  407. c->addr = i;
  408. rc = i2c_master_recv(c, &buf, 0);
  409. if (rc < 0)
  410. continue;
  411. cx231xx_info("%s: i2c scan: found device @ 0x%x [%s]\n",
  412. dev->name, i << 1,
  413. i2c_devs[i] ? i2c_devs[i] : "???");
  414. }
  415. cx231xx_info(": Completed Checking for I2C devices.\n");
  416. }
  417. /*
  418. * cx231xx_i2c_register()
  419. * register i2c bus
  420. */
  421. int cx231xx_i2c_register(struct cx231xx_i2c *bus)
  422. {
  423. struct cx231xx *dev = bus->dev;
  424. BUG_ON(!dev->cx231xx_send_usb_command);
  425. memcpy(&bus->i2c_adap, &cx231xx_adap_template, sizeof(bus->i2c_adap));
  426. memcpy(&bus->i2c_algo, &cx231xx_algo, sizeof(bus->i2c_algo));
  427. memcpy(&bus->i2c_client, &cx231xx_client_template,
  428. sizeof(bus->i2c_client));
  429. bus->i2c_adap.dev.parent = &dev->udev->dev;
  430. strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
  431. bus->i2c_algo.data = bus;
  432. bus->i2c_adap.algo_data = bus;
  433. i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
  434. i2c_add_adapter(&bus->i2c_adap);
  435. bus->i2c_client.adapter = &bus->i2c_adap;
  436. if (0 == bus->i2c_rc) {
  437. if (i2c_scan)
  438. cx231xx_do_i2c_scan(dev, &bus->i2c_client);
  439. } else
  440. cx231xx_warn("%s: i2c bus %d register FAILED\n",
  441. dev->name, bus->nr);
  442. return bus->i2c_rc;
  443. }
  444. /*
  445. * cx231xx_i2c_unregister()
  446. * unregister i2c_bus
  447. */
  448. int cx231xx_i2c_unregister(struct cx231xx_i2c *bus)
  449. {
  450. i2c_del_adapter(&bus->i2c_adap);
  451. return 0;
  452. }