cx231xx-i2c.c 14 KB

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