cx231xx-i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 to XC5000
  66. 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. dev->xc_fw_load_done = 1; /* FW Loading is done */
  77. break;
  78. case 0x000D: /* Set signal source */
  79. case 0x0001: /* Set TV standard - Video */
  80. case 0x0002: /* Set TV standard - Audio */
  81. case 0x0003: /* Set RF Frequency */
  82. need_gpio = 1;
  83. break;
  84. default:
  85. if (dev->xc_fw_load_done)
  86. need_gpio = 1;
  87. break;
  88. }
  89. if (need_gpio) {
  90. dprintk1(1,
  91. " GPIO W R I T E : addr 0x%x, len %d, saddr 0x%x\n",
  92. msg->addr, msg->len, saddr);
  93. return dev->cx231xx_gpio_i2c_write(dev,
  94. msg->addr,
  95. msg->buf,
  96. msg->len);
  97. }
  98. }
  99. /* special case for Xc5000 tuner case */
  100. saddr_len = 1;
  101. /* adjust the length to correct length */
  102. size -= saddr_len;
  103. buf_ptr = (u8 *) (msg->buf + 1);
  104. do {
  105. /* prepare xfer_data struct */
  106. req_data.dev_addr = msg->addr;
  107. req_data.direction = msg->flags;
  108. req_data.saddr_len = saddr_len;
  109. req_data.saddr_dat = msg->buf[0];
  110. req_data.buf_size = size > 16 ? 16 : size;
  111. req_data.p_buffer = (u8 *) (buf_ptr + loop * 16);
  112. bus->i2c_nostop = (size > 16) ? 1 : 0;
  113. bus->i2c_reserve = (loop == 0) ? 0 : 1;
  114. /* usb send command */
  115. status = dev->cx231xx_send_usb_command(bus, &req_data);
  116. loop++;
  117. if (size >= 16)
  118. size -= 16;
  119. else
  120. size = 0;
  121. } while (size > 0);
  122. bus->i2c_nostop = 0;
  123. bus->i2c_reserve = 0;
  124. } else { /* regular case */
  125. /* prepare xfer_data struct */
  126. req_data.dev_addr = msg->addr;
  127. req_data.direction = msg->flags;
  128. req_data.saddr_len = 0;
  129. req_data.saddr_dat = 0;
  130. req_data.buf_size = msg->len;
  131. req_data.p_buffer = msg->buf;
  132. /* usb send command */
  133. status = dev->cx231xx_send_usb_command(bus, &req_data);
  134. }
  135. return status < 0 ? status : 0;
  136. }
  137. /*
  138. * cx231xx_i2c_recv_bytes()
  139. * read a byte from the i2c device
  140. */
  141. static int cx231xx_i2c_recv_bytes(struct i2c_adapter *i2c_adap,
  142. const struct i2c_msg *msg)
  143. {
  144. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  145. struct cx231xx *dev = bus->dev;
  146. struct cx231xx_i2c_xfer_data req_data;
  147. int status = 0;
  148. u16 saddr = 0;
  149. u8 need_gpio = 0;
  150. if ((bus->nr == 1) && (msg->addr == 0x61)
  151. && dev->tuner_type == TUNER_XC5000) {
  152. if (msg->len == 2)
  153. saddr = msg->buf[0] << 8 | msg->buf[1];
  154. else if (msg->len == 1)
  155. saddr = msg->buf[0];
  156. if (dev->xc_fw_load_done) {
  157. switch (saddr) {
  158. case 0x0009: /* BUSY check */
  159. dprintk1(1,
  160. " GPIO R E A D : Special case BUSY check \n");
  161. /* Try to read BUSY register, just set it to zero */
  162. msg->buf[0] = 0;
  163. if (msg->len == 2)
  164. msg->buf[1] = 0;
  165. return 0;
  166. case 0x0004: /* read Lock status */
  167. need_gpio = 1;
  168. break;
  169. }
  170. if (need_gpio) {
  171. /* this is a special case to handle Xceive tuner clock stretch issue
  172. with gpio based I2C interface */
  173. dprintk1(1,
  174. " GPIO R E A D : addr 0x%x, len %d, saddr 0x%x\n",
  175. msg->addr, msg->len,
  176. msg->buf[0] << 8 | msg->buf[1]);
  177. status =
  178. dev->cx231xx_gpio_i2c_write(dev, msg->addr,
  179. msg->buf,
  180. msg->len);
  181. status =
  182. dev->cx231xx_gpio_i2c_read(dev, msg->addr,
  183. msg->buf,
  184. msg->len);
  185. return status;
  186. }
  187. }
  188. /* prepare xfer_data struct */
  189. req_data.dev_addr = msg->addr;
  190. req_data.direction = msg->flags;
  191. req_data.saddr_len = msg->len;
  192. req_data.saddr_dat = msg->buf[0] << 8 | msg->buf[1];
  193. req_data.buf_size = msg->len;
  194. req_data.p_buffer = msg->buf;
  195. /* usb send command */
  196. status = dev->cx231xx_send_usb_command(bus, &req_data);
  197. } else {
  198. /* prepare xfer_data struct */
  199. req_data.dev_addr = msg->addr;
  200. req_data.direction = msg->flags;
  201. req_data.saddr_len = 0;
  202. req_data.saddr_dat = 0;
  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. }
  208. return status < 0 ? status : 0;
  209. }
  210. /*
  211. * cx231xx_i2c_recv_bytes_with_saddr()
  212. * read a byte from the i2c device
  213. */
  214. static int cx231xx_i2c_recv_bytes_with_saddr(struct i2c_adapter *i2c_adap,
  215. const struct i2c_msg *msg1,
  216. const struct i2c_msg *msg2)
  217. {
  218. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  219. struct cx231xx *dev = bus->dev;
  220. struct cx231xx_i2c_xfer_data req_data;
  221. int status = 0;
  222. u16 saddr = 0;
  223. u8 need_gpio = 0;
  224. if (msg1->len == 2)
  225. saddr = msg1->buf[0] << 8 | msg1->buf[1];
  226. else if (msg1->len == 1)
  227. saddr = msg1->buf[0];
  228. if ((bus->nr == 1) && (msg2->addr == 0x61)
  229. && dev->tuner_type == TUNER_XC5000) {
  230. if ((msg2->len < 16)) {
  231. dprintk1(1,
  232. " i2c_read : addr 0x%x, len %d, subaddr 0x%x, leng %d\n",
  233. msg2->addr, msg2->len, saddr, msg1->len);
  234. switch (saddr) {
  235. case 0x0008: /* read FW load status */
  236. need_gpio = 1;
  237. break;
  238. case 0x0004: /* read Lock status */
  239. need_gpio = 1;
  240. break;
  241. }
  242. if (need_gpio) {
  243. status =
  244. dev->cx231xx_gpio_i2c_write(dev, msg1->addr,
  245. msg1->buf,
  246. msg1->len);
  247. status =
  248. dev->cx231xx_gpio_i2c_read(dev, msg2->addr,
  249. msg2->buf,
  250. msg2->len);
  251. return status;
  252. }
  253. }
  254. }
  255. /* prepare xfer_data struct */
  256. req_data.dev_addr = msg2->addr;
  257. req_data.direction = msg2->flags;
  258. req_data.saddr_len = msg1->len;
  259. req_data.saddr_dat = saddr;
  260. req_data.buf_size = msg2->len;
  261. req_data.p_buffer = msg2->buf;
  262. /* usb send command */
  263. status = dev->cx231xx_send_usb_command(bus, &req_data);
  264. return status < 0 ? status : 0;
  265. }
  266. /*
  267. * cx231xx_i2c_check_for_device()
  268. * check if there is a i2c_device at the supplied address
  269. */
  270. static int cx231xx_i2c_check_for_device(struct i2c_adapter *i2c_adap,
  271. const struct i2c_msg *msg)
  272. {
  273. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  274. struct cx231xx *dev = bus->dev;
  275. struct cx231xx_i2c_xfer_data req_data;
  276. int status = 0;
  277. /* prepare xfer_data struct */
  278. req_data.dev_addr = msg->addr;
  279. req_data.direction = msg->flags;
  280. req_data.saddr_len = 0;
  281. req_data.saddr_dat = 0;
  282. req_data.buf_size = 0;
  283. req_data.p_buffer = NULL;
  284. /* usb send command */
  285. status = dev->cx231xx_send_usb_command(bus, &req_data);
  286. return status < 0 ? status : 0;
  287. }
  288. /*
  289. * cx231xx_i2c_xfer()
  290. * the main i2c transfer function
  291. */
  292. static int cx231xx_i2c_xfer(struct i2c_adapter *i2c_adap,
  293. struct i2c_msg msgs[], int num)
  294. {
  295. struct cx231xx_i2c *bus = i2c_adap->algo_data;
  296. struct cx231xx *dev = bus->dev;
  297. int addr, rc, i, byte;
  298. if (num <= 0)
  299. return 0;
  300. for (i = 0; i < num; i++) {
  301. addr = msgs[i].addr >> 1;
  302. dprintk2(2, "%s %s addr=%x len=%d:",
  303. (msgs[i].flags & I2C_M_RD) ? "read" : "write",
  304. i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
  305. if (!msgs[i].len) { /* no len: check only for device presence */
  306. rc = cx231xx_i2c_check_for_device(i2c_adap, &msgs[i]);
  307. if (rc < 0) {
  308. dprintk2(2, " no device\n");
  309. return rc;
  310. }
  311. } else if (msgs[i].flags & I2C_M_RD) {
  312. /* read bytes */
  313. rc = cx231xx_i2c_recv_bytes(i2c_adap, &msgs[i]);
  314. if (i2c_debug >= 2) {
  315. for (byte = 0; byte < msgs[i].len; byte++)
  316. printk(" %02x", msgs[i].buf[byte]);
  317. }
  318. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  319. msgs[i].addr == msgs[i + 1].addr
  320. && (msgs[i].len <= 2) && (bus->nr < 2)) {
  321. /* read bytes */
  322. rc = cx231xx_i2c_recv_bytes_with_saddr(i2c_adap,
  323. &msgs[i],
  324. &msgs[i + 1]);
  325. if (i2c_debug >= 2) {
  326. for (byte = 0; byte < msgs[i].len; byte++)
  327. printk(" %02x", msgs[i].buf[byte]);
  328. }
  329. i++;
  330. } else {
  331. /* write bytes */
  332. if (i2c_debug >= 2) {
  333. for (byte = 0; byte < msgs[i].len; byte++)
  334. printk(" %02x", msgs[i].buf[byte]);
  335. }
  336. rc = cx231xx_i2c_send_bytes(i2c_adap, &msgs[i]);
  337. }
  338. if (rc < 0)
  339. goto err;
  340. if (i2c_debug >= 2)
  341. printk("\n");
  342. }
  343. return num;
  344. err:
  345. dprintk2(2, " ERROR: %i\n", rc);
  346. return rc;
  347. }
  348. /* ----------------------------------------------------------- */
  349. /*
  350. * functionality()
  351. */
  352. static u32 functionality(struct i2c_adapter *adap)
  353. {
  354. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  355. }
  356. /*
  357. * attach_inform()
  358. * gets called when a device attaches to the i2c bus
  359. * does some basic configuration
  360. */
  361. static int attach_inform(struct i2c_client *client)
  362. {
  363. struct cx231xx_i2c *bus = i2c_get_adapdata(client->adapter);
  364. struct cx231xx *dev = bus->dev;
  365. switch (client->addr << 1) {
  366. case 0x32:
  367. dprintk1(1, "attach_inform: Geminit III detected.\n");
  368. break;
  369. case 0x02:
  370. dprintk1(1, "attach_inform: Acquarius detected.\n");
  371. break;
  372. case 0xa0:
  373. dprintk1(1, "attach_inform: eeprom detected.\n");
  374. break;
  375. case 0x60:
  376. dprintk1(1, "attach_inform: Colibri detected.\n");
  377. break;
  378. case 0x8e:
  379. {
  380. struct IR_i2c *ir = i2c_get_clientdata(client);
  381. dprintk1(1, "attach_inform: IR detected (%s).\n",
  382. ir->phys);
  383. cx231xx_set_ir(dev, ir);
  384. break;
  385. }
  386. case 0x80:
  387. case 0x88:
  388. dprintk1(1, "attach_inform: Hammerhead detected.\n");
  389. break;
  390. default:
  391. if (!dev->tuner_addr)
  392. dev->tuner_addr = client->addr;
  393. dprintk1(1, "attach inform: detected I2C address %x\n",
  394. client->addr << 1);
  395. }
  396. return 0;
  397. }
  398. static int detach_inform(struct i2c_client *client)
  399. {
  400. dprintk1(1, "i2c detach [client=%s]\n", client->name);
  401. return 0;
  402. }
  403. static struct i2c_algorithm cx231xx_algo = {
  404. .master_xfer = cx231xx_i2c_xfer,
  405. .functionality = functionality,
  406. };
  407. static struct i2c_adapter cx231xx_adap_template = {
  408. .owner = THIS_MODULE,
  409. .class = I2C_CLASS_TV_ANALOG,
  410. .name = "cx231xx",
  411. .id = I2C_HW_B_CX231XX,
  412. .algo = &cx231xx_algo,
  413. .client_register = attach_inform,
  414. .client_unregister = detach_inform,
  415. };
  416. static struct i2c_client cx231xx_client_template = {
  417. .name = "cx231xx internal",
  418. };
  419. /* ----------------------------------------------------------- */
  420. /*
  421. * i2c_devs
  422. * incomplete list of known devices
  423. */
  424. static char *i2c_devs[128] = {
  425. [0x60 >> 1] = "colibri",
  426. [0x88 >> 1] = "hammerhead",
  427. [0x8e >> 1] = "CIR",
  428. [0x32 >> 1] = "GeminiIII",
  429. [0x02 >> 1] = "Aquarius",
  430. [0xa0 >> 1] = "eeprom",
  431. [0xc0 >> 1] = "tuner/XC3028",
  432. [0xc2 >> 1] = "tuner/XC5000",
  433. };
  434. /*
  435. * cx231xx_do_i2c_scan()
  436. * check i2c address range for devices
  437. */
  438. void cx231xx_do_i2c_scan(struct cx231xx *dev, struct i2c_client *c)
  439. {
  440. unsigned char buf;
  441. int i, rc;
  442. cx231xx_info(": Checking for I2C devices ..\n");
  443. for (i = 0; i < 128; i++) {
  444. c->addr = i;
  445. rc = i2c_master_recv(c, &buf, 0);
  446. if (rc < 0)
  447. continue;
  448. cx231xx_info("%s: i2c scan: found device @ 0x%x [%s]\n",
  449. dev->name, i << 1,
  450. i2c_devs[i] ? i2c_devs[i] : "???");
  451. }
  452. cx231xx_info(": Completed Checking for I2C devices.\n");
  453. }
  454. /*
  455. * cx231xx_i2c_call_clients()
  456. * send commands to all attached i2c devices
  457. */
  458. void cx231xx_i2c_call_clients(struct cx231xx_i2c *bus, unsigned int cmd,
  459. void *arg)
  460. {
  461. /* struct cx231xx *dev = bus->dev; */
  462. BUG_ON(NULL == bus->i2c_adap.algo_data);
  463. i2c_clients_command(&bus->i2c_adap, cmd, arg);
  464. }
  465. /*
  466. * cx231xx_i2c_register()
  467. * register i2c bus
  468. */
  469. int cx231xx_i2c_register(struct cx231xx_i2c *bus)
  470. {
  471. struct cx231xx *dev = bus->dev;
  472. BUG_ON(!dev->cx231xx_send_usb_command);
  473. cx231xx_info("%s(bus = %d)\n", __func__, bus->nr);
  474. memcpy(&bus->i2c_adap, &cx231xx_adap_template, sizeof(bus->i2c_adap));
  475. memcpy(&bus->i2c_algo, &cx231xx_algo, sizeof(bus->i2c_algo));
  476. memcpy(&bus->i2c_client, &cx231xx_client_template,
  477. sizeof(bus->i2c_client));
  478. bus->i2c_adap.dev.parent = &dev->udev->dev;
  479. strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
  480. bus->i2c_algo.data = bus;
  481. bus->i2c_adap.algo_data = bus;
  482. i2c_set_adapdata(&bus->i2c_adap, bus);
  483. i2c_add_adapter(&bus->i2c_adap);
  484. bus->i2c_client.adapter = &bus->i2c_adap;
  485. if (0 == bus->i2c_rc) {
  486. cx231xx_info("%s: i2c bus %d registered\n", dev->name, bus->nr);
  487. if (i2c_scan)
  488. cx231xx_do_i2c_scan(dev, &bus->i2c_client);
  489. } else
  490. cx231xx_warn("%s: i2c bus %d register FAILED\n",
  491. dev->name, bus->nr);
  492. return bus->i2c_rc;
  493. }
  494. /*
  495. * cx231xx_i2c_unregister()
  496. * unregister i2c_bus
  497. */
  498. int cx231xx_i2c_unregister(struct cx231xx_i2c *bus)
  499. {
  500. i2c_del_adapter(&bus->i2c_adap);
  501. return 0;
  502. }