cx231xx-i2c.c 14 KB

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