cx23885-i2c.c 9.6 KB

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