cx23885-i2c.c 9.0 KB

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