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