cx23885-i2c.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for the Conexant CX23885 PCIe bridge
  4. *
  5. * Copyright (c) 2006 Steven Toth <[email protected]>
  6. */
  7. #include "cx23885.h"
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/delay.h>
  11. #include <asm/io.h>
  12. #include <media/v4l2-common.h>
  13. static unsigned int i2c_debug;
  14. module_param(i2c_debug, int, 0644);
  15. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  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. #define dprintk(level, fmt, arg...)\
  20. do { if (i2c_debug >= level)\
  21. printk(KERN_DEBUG pr_fmt("%s: i2c:" fmt), \
  22. __func__, ##arg); \
  23. } while (0)
  24. #define I2C_WAIT_DELAY 32
  25. #define I2C_WAIT_RETRY 64
  26. #define I2C_EXTEND (1 << 3)
  27. #define I2C_NOSTOP (1 << 4)
  28. static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
  29. {
  30. struct cx23885_i2c *bus = i2c_adap->algo_data;
  31. struct cx23885_dev *dev = bus->dev;
  32. return cx_read(bus->reg_stat) & 0x01;
  33. }
  34. static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
  35. {
  36. struct cx23885_i2c *bus = i2c_adap->algo_data;
  37. struct cx23885_dev *dev = bus->dev;
  38. return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
  39. }
  40. static int i2c_wait_done(struct i2c_adapter *i2c_adap)
  41. {
  42. int count;
  43. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  44. if (!i2c_is_busy(i2c_adap))
  45. break;
  46. udelay(I2C_WAIT_DELAY);
  47. }
  48. if (I2C_WAIT_RETRY == count)
  49. return 0;
  50. return 1;
  51. }
  52. static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
  53. const struct i2c_msg *msg, int joined_rlen)
  54. {
  55. struct cx23885_i2c *bus = i2c_adap->algo_data;
  56. struct cx23885_dev *dev = bus->dev;
  57. u32 wdata, addr, ctrl;
  58. int retval, cnt;
  59. if (joined_rlen)
  60. dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__,
  61. msg->len, joined_rlen);
  62. else
  63. dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
  64. /* Deal with i2c probe functions with zero payload */
  65. if (msg->len == 0) {
  66. cx_write(bus->reg_addr, msg->addr << 25);
  67. cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
  68. if (!i2c_wait_done(i2c_adap))
  69. return -EIO;
  70. if (!i2c_slave_did_ack(i2c_adap))
  71. return -ENXIO;
  72. dprintk(1, "%s() returns 0\n", __func__);
  73. return 0;
  74. }
  75. /* dev, reg + first byte */
  76. addr = (msg->addr << 25) | msg->buf[0];
  77. wdata = msg->buf[0];
  78. ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
  79. if (msg->len > 1)
  80. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  81. else if (joined_rlen)
  82. ctrl |= I2C_NOSTOP;
  83. cx_write(bus->reg_addr, addr);
  84. cx_write(bus->reg_wdata, wdata);
  85. cx_write(bus->reg_ctrl, ctrl);
  86. if (!i2c_wait_done(i2c_adap))
  87. goto eio;
  88. if (i2c_debug) {
  89. printk(KERN_DEBUG " <W %02x %02x", msg->addr << 1, msg->buf[0]);
  90. if (!(ctrl & I2C_NOSTOP))
  91. pr_cont(" >\n");
  92. }
  93. for (cnt = 1; cnt < msg->len; cnt++) {
  94. /* following bytes */
  95. wdata = msg->buf[cnt];
  96. ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
  97. if (cnt < msg->len - 1)
  98. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  99. else if (joined_rlen)
  100. ctrl |= I2C_NOSTOP;
  101. cx_write(bus->reg_addr, addr);
  102. cx_write(bus->reg_wdata, wdata);
  103. cx_write(bus->reg_ctrl, ctrl);
  104. if (!i2c_wait_done(i2c_adap))
  105. goto eio;
  106. if (i2c_debug) {
  107. pr_cont(" %02x", msg->buf[cnt]);
  108. if (!(ctrl & I2C_NOSTOP))
  109. pr_cont(" >\n");
  110. }
  111. }
  112. return msg->len;
  113. eio:
  114. retval = -EIO;
  115. if (i2c_debug)
  116. pr_err(" ERR: %d\n", retval);
  117. return retval;
  118. }
  119. static int i2c_readbytes(struct i2c_adapter *i2c_adap,
  120. const struct i2c_msg *msg, int joined)
  121. {
  122. struct cx23885_i2c *bus = i2c_adap->algo_data;
  123. struct cx23885_dev *dev = bus->dev;
  124. u32 ctrl, cnt;
  125. int retval;
  126. if (i2c_debug && !joined)
  127. dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
  128. /* Deal with i2c probe functions with zero payload */
  129. if (msg->len == 0) {
  130. cx_write(bus->reg_addr, msg->addr << 25);
  131. cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
  132. if (!i2c_wait_done(i2c_adap))
  133. return -EIO;
  134. if (!i2c_slave_did_ack(i2c_adap))
  135. return -ENXIO;
  136. dprintk(1, "%s() returns 0\n", __func__);
  137. return 0;
  138. }
  139. if (i2c_debug) {
  140. if (joined)
  141. dprintk(1, " R");
  142. else
  143. dprintk(1, " <R %02x", (msg->addr << 1) + 1);
  144. }
  145. for (cnt = 0; cnt < msg->len; cnt++) {
  146. ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
  147. if (cnt < msg->len - 1)
  148. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  149. cx_write(bus->reg_addr, msg->addr << 25);
  150. cx_write(bus->reg_ctrl, ctrl);
  151. if (!i2c_wait_done(i2c_adap))
  152. goto eio;
  153. msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
  154. if (i2c_debug) {
  155. dprintk(1, " %02x", msg->buf[cnt]);
  156. if (!(ctrl & I2C_NOSTOP))
  157. dprintk(1, " >\n");
  158. }
  159. }
  160. return msg->len;
  161. eio:
  162. retval = -EIO;
  163. if (i2c_debug)
  164. pr_err(" ERR: %d\n", retval);
  165. return retval;
  166. }
  167. static int i2c_xfer(struct i2c_adapter *i2c_adap,
  168. struct i2c_msg *msgs, int num)
  169. {
  170. int i, retval = 0;
  171. dprintk(1, "%s(num = %d)\n", __func__, num);
  172. for (i = 0 ; i < num; i++) {
  173. dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  174. __func__, num, msgs[i].addr, msgs[i].len);
  175. if (msgs[i].flags & I2C_M_RD) {
  176. /* read */
  177. retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
  178. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  179. msgs[i].addr == msgs[i + 1].addr) {
  180. /* write then read from same address */
  181. retval = i2c_sendbytes(i2c_adap, &msgs[i],
  182. msgs[i + 1].len);
  183. if (retval < 0)
  184. goto err;
  185. i++;
  186. retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
  187. } else {
  188. /* write */
  189. retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
  190. }
  191. if (retval < 0)
  192. goto err;
  193. }
  194. return num;
  195. err:
  196. return retval;
  197. }
  198. static u32 cx23885_functionality(struct i2c_adapter *adap)
  199. {
  200. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  201. }
  202. static const struct i2c_algorithm cx23885_i2c_algo_template = {
  203. .master_xfer = i2c_xfer,
  204. .functionality = cx23885_functionality,
  205. };
  206. /* ----------------------------------------------------------------------- */
  207. static const struct i2c_adapter cx23885_i2c_adap_template = {
  208. .name = "cx23885",
  209. .owner = THIS_MODULE,
  210. .algo = &cx23885_i2c_algo_template,
  211. };
  212. static const struct i2c_client cx23885_i2c_client_template = {
  213. .name = "cx23885 internal",
  214. };
  215. static char *i2c_devs[128] = {
  216. [0x10 >> 1] = "tda10048",
  217. [0x12 >> 1] = "dib7000pc",
  218. [0x1c >> 1] = "lgdt3303",
  219. [0x80 >> 1] = "cs3308",
  220. [0x82 >> 1] = "cs3308",
  221. [0x86 >> 1] = "tda9887",
  222. [0x32 >> 1] = "cx24227",
  223. [0x88 >> 1] = "cx25837",
  224. [0x84 >> 1] = "tda8295",
  225. [0x98 >> 1] = "flatiron",
  226. [0xa0 >> 1] = "eeprom",
  227. [0xc0 >> 1] = "tuner/mt2131/tda8275",
  228. [0xc2 >> 1] = "tuner/mt2131/tda8275/xc5000/xc3028",
  229. [0xc8 >> 1] = "tuner/xc3028L",
  230. };
  231. static void do_i2c_scan(char *name, struct i2c_client *c)
  232. {
  233. unsigned char buf;
  234. int i, rc;
  235. for (i = 0; i < 128; i++) {
  236. c->addr = i;
  237. rc = i2c_master_recv(c, &buf, 0);
  238. if (rc < 0)
  239. continue;
  240. pr_info("%s: i2c scan: found device @ 0x%04x [%s]\n",
  241. name, i, i2c_devs[i] ? i2c_devs[i] : "???");
  242. }
  243. }
  244. /* init + register i2c adapter */
  245. int cx23885_i2c_register(struct cx23885_i2c *bus)
  246. {
  247. struct cx23885_dev *dev = bus->dev;
  248. dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
  249. bus->i2c_adap = cx23885_i2c_adap_template;
  250. bus->i2c_client = cx23885_i2c_client_template;
  251. bus->i2c_adap.dev.parent = &dev->pci->dev;
  252. strscpy(bus->i2c_adap.name, bus->dev->name,
  253. sizeof(bus->i2c_adap.name));
  254. bus->i2c_adap.algo_data = bus;
  255. i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
  256. i2c_add_adapter(&bus->i2c_adap);
  257. bus->i2c_client.adapter = &bus->i2c_adap;
  258. if (0 == bus->i2c_rc) {
  259. dprintk(1, "%s: i2c bus %d registered\n", dev->name, bus->nr);
  260. if (i2c_scan) {
  261. pr_info("%s: scan bus %d:\n",
  262. dev->name, bus->nr);
  263. do_i2c_scan(dev->name, &bus->i2c_client);
  264. }
  265. } else
  266. pr_warn("%s: i2c bus %d register FAILED\n",
  267. dev->name, bus->nr);
  268. /* Instantiate the IR receiver device, if present */
  269. if (0 == bus->i2c_rc) {
  270. struct i2c_board_info info;
  271. static const unsigned short addr_list[] = {
  272. 0x6b, I2C_CLIENT_END
  273. };
  274. memset(&info, 0, sizeof(struct i2c_board_info));
  275. strscpy(info.type, "ir_video", I2C_NAME_SIZE);
  276. /* Use quick read command for probe, some IR chips don't
  277. * support writes */
  278. i2c_new_scanned_device(&bus->i2c_adap, &info, addr_list,
  279. i2c_probe_func_quick_read);
  280. }
  281. return bus->i2c_rc;
  282. }
  283. int cx23885_i2c_unregister(struct cx23885_i2c *bus)
  284. {
  285. i2c_del_adapter(&bus->i2c_adap);
  286. return 0;
  287. }
  288. void cx23885_av_clk(struct cx23885_dev *dev, int enable)
  289. {
  290. /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
  291. char buffer[3];
  292. struct i2c_msg msg;
  293. dprintk(1, "%s(enabled = %d)\n", __func__, enable);
  294. /* Register 0x144 */
  295. buffer[0] = 0x01;
  296. buffer[1] = 0x44;
  297. if (enable == 1)
  298. buffer[2] = 0x05;
  299. else
  300. buffer[2] = 0x00;
  301. msg.addr = 0x44;
  302. msg.flags = I2C_M_TEN;
  303. msg.len = 3;
  304. msg.buf = buffer;
  305. i2c_xfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
  306. }