cx23885-i2c.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 -EIO;
  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. retval = i2c_wait_done(i2c_adap);
  101. if (retval < 0)
  102. goto err;
  103. if (retval == 0)
  104. goto eio;
  105. if (i2c_debug) {
  106. printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
  107. if (!(ctrl & I2C_NOSTOP))
  108. printk(" >\n");
  109. }
  110. for (cnt = 1; cnt < msg->len; cnt++) {
  111. /* following bytes */
  112. wdata = msg->buf[cnt];
  113. ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
  114. if (cnt < msg->len - 1)
  115. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  116. else if (joined_rlen)
  117. ctrl |= I2C_NOSTOP;
  118. cx_write(bus->reg_addr, addr);
  119. cx_write(bus->reg_wdata, wdata);
  120. cx_write(bus->reg_ctrl, ctrl);
  121. retval = i2c_wait_done(i2c_adap);
  122. if (retval < 0)
  123. goto err;
  124. if (retval == 0)
  125. goto eio;
  126. if (i2c_debug) {
  127. dprintk(1, " %02x", msg->buf[cnt]);
  128. if (!(ctrl & I2C_NOSTOP))
  129. dprintk(1, " >\n");
  130. }
  131. }
  132. return msg->len;
  133. eio:
  134. retval = -EIO;
  135. err:
  136. if (i2c_debug)
  137. printk(KERN_ERR " ERR: %d\n", retval);
  138. return retval;
  139. }
  140. static int i2c_readbytes(struct i2c_adapter *i2c_adap,
  141. const struct i2c_msg *msg, int joined)
  142. {
  143. struct cx23885_i2c *bus = i2c_adap->algo_data;
  144. struct cx23885_dev *dev = bus->dev;
  145. u32 ctrl, cnt;
  146. int retval;
  147. if (i2c_debug && !joined)
  148. dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
  149. /* Deal with i2c probe functions with zero payload */
  150. if (msg->len == 0) {
  151. cx_write(bus->reg_addr, msg->addr << 25);
  152. cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
  153. if (!i2c_wait_done(i2c_adap))
  154. return -EIO;
  155. if (!i2c_slave_did_ack(i2c_adap))
  156. return -EIO;
  157. dprintk(1, "%s() returns 0\n", __func__);
  158. return 0;
  159. }
  160. if (i2c_debug) {
  161. if (joined)
  162. dprintk(1, " R");
  163. else
  164. dprintk(1, " <R %02x", (msg->addr << 1) + 1);
  165. }
  166. for (cnt = 0; cnt < msg->len; cnt++) {
  167. ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
  168. if (cnt < msg->len - 1)
  169. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  170. cx_write(bus->reg_addr, msg->addr << 25);
  171. cx_write(bus->reg_ctrl, ctrl);
  172. retval = i2c_wait_done(i2c_adap);
  173. if (retval < 0)
  174. goto err;
  175. if (retval == 0)
  176. goto eio;
  177. msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
  178. if (i2c_debug) {
  179. dprintk(1, " %02x", msg->buf[cnt]);
  180. if (!(ctrl & I2C_NOSTOP))
  181. dprintk(1, " >\n");
  182. }
  183. }
  184. return msg->len;
  185. eio:
  186. retval = -EIO;
  187. err:
  188. if (i2c_debug)
  189. printk(KERN_ERR " ERR: %d\n", retval);
  190. return retval;
  191. }
  192. static int i2c_xfer(struct i2c_adapter *i2c_adap,
  193. struct i2c_msg *msgs, int num)
  194. {
  195. struct cx23885_i2c *bus = i2c_adap->algo_data;
  196. struct cx23885_dev *dev = bus->dev;
  197. int i, retval = 0;
  198. dprintk(1, "%s(num = %d)\n", __func__, num);
  199. for (i = 0 ; i < num; i++) {
  200. dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  201. __func__, num, msgs[i].addr, msgs[i].len);
  202. if (msgs[i].flags & I2C_M_RD) {
  203. /* read */
  204. retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
  205. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  206. msgs[i].addr == msgs[i + 1].addr) {
  207. /* write then read from same address */
  208. retval = i2c_sendbytes(i2c_adap, &msgs[i],
  209. msgs[i + 1].len);
  210. if (retval < 0)
  211. goto err;
  212. i++;
  213. retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
  214. } else {
  215. /* write */
  216. retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
  217. }
  218. if (retval < 0)
  219. goto err;
  220. }
  221. return num;
  222. err:
  223. return retval;
  224. }
  225. static u32 cx23885_functionality(struct i2c_adapter *adap)
  226. {
  227. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  228. }
  229. static struct i2c_algorithm cx23885_i2c_algo_template = {
  230. .master_xfer = i2c_xfer,
  231. .functionality = cx23885_functionality,
  232. };
  233. /* ----------------------------------------------------------------------- */
  234. static struct i2c_adapter cx23885_i2c_adap_template = {
  235. .name = "cx23885",
  236. .owner = THIS_MODULE,
  237. .algo = &cx23885_i2c_algo_template,
  238. };
  239. static struct i2c_client cx23885_i2c_client_template = {
  240. .name = "cx23885 internal",
  241. };
  242. static char *i2c_devs[128] = {
  243. [0x10 >> 1] = "tda10048",
  244. [0x12 >> 1] = "dib7000pc",
  245. [0x1c >> 1] = "lgdt3303",
  246. [0x86 >> 1] = "tda9887",
  247. [0x32 >> 1] = "cx24227",
  248. [0x88 >> 1] = "cx25837",
  249. [0x84 >> 1] = "tda8295",
  250. [0xa0 >> 1] = "eeprom",
  251. [0xc0 >> 1] = "tuner/mt2131/tda8275",
  252. [0xc2 >> 1] = "tuner/mt2131/tda8275/xc5000/xc3028",
  253. [0xc8 >> 1] = "tuner/xc3028L",
  254. };
  255. static void do_i2c_scan(char *name, struct i2c_client *c)
  256. {
  257. unsigned char buf;
  258. int i, rc;
  259. for (i = 0; i < 128; i++) {
  260. c->addr = i;
  261. rc = i2c_master_recv(c, &buf, 0);
  262. if (rc < 0)
  263. continue;
  264. printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
  265. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  266. }
  267. }
  268. /* init + register i2c algo-bit adapter */
  269. int cx23885_i2c_register(struct cx23885_i2c *bus)
  270. {
  271. struct cx23885_dev *dev = bus->dev;
  272. dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
  273. memcpy(&bus->i2c_adap, &cx23885_i2c_adap_template,
  274. sizeof(bus->i2c_adap));
  275. memcpy(&bus->i2c_algo, &cx23885_i2c_algo_template,
  276. sizeof(bus->i2c_algo));
  277. memcpy(&bus->i2c_client, &cx23885_i2c_client_template,
  278. sizeof(bus->i2c_client));
  279. bus->i2c_adap.dev.parent = &dev->pci->dev;
  280. strlcpy(bus->i2c_adap.name, bus->dev->name,
  281. sizeof(bus->i2c_adap.name));
  282. bus->i2c_algo.data = bus;
  283. bus->i2c_adap.algo_data = bus;
  284. i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
  285. i2c_add_adapter(&bus->i2c_adap);
  286. bus->i2c_client.adapter = &bus->i2c_adap;
  287. if (0 == bus->i2c_rc) {
  288. dprintk(1, "%s: i2c bus %d registered\n", dev->name, bus->nr);
  289. if (i2c_scan) {
  290. printk(KERN_INFO "%s: scan bus %d:\n",
  291. dev->name, bus->nr);
  292. do_i2c_scan(dev->name, &bus->i2c_client);
  293. }
  294. } else
  295. printk(KERN_WARNING "%s: i2c bus %d register FAILED\n",
  296. dev->name, bus->nr);
  297. /* Instantiate the IR receiver device, if present */
  298. if (0 == bus->i2c_rc) {
  299. struct i2c_board_info info;
  300. const unsigned short addr_list[] = {
  301. 0x6b, I2C_CLIENT_END
  302. };
  303. memset(&info, 0, sizeof(struct i2c_board_info));
  304. strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
  305. i2c_new_probed_device(&bus->i2c_adap, &info, addr_list);
  306. }
  307. return bus->i2c_rc;
  308. }
  309. int cx23885_i2c_unregister(struct cx23885_i2c *bus)
  310. {
  311. i2c_del_adapter(&bus->i2c_adap);
  312. return 0;
  313. }
  314. void cx23885_av_clk(struct cx23885_dev *dev, int enable)
  315. {
  316. /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
  317. char buffer[3];
  318. struct i2c_msg msg;
  319. dprintk(1, "%s(enabled = %d)\n", __func__, enable);
  320. /* Register 0x144 */
  321. buffer[0] = 0x01;
  322. buffer[1] = 0x44;
  323. if (enable == 1)
  324. buffer[2] = 0x05;
  325. else
  326. buffer[2] = 0x00;
  327. msg.addr = 0x44;
  328. msg.flags = I2C_M_TEN;
  329. msg.len = 3;
  330. msg.buf = buffer;
  331. i2c_xfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
  332. }
  333. /* ----------------------------------------------------------------------- */
  334. /*
  335. * Local variables:
  336. * c-basic-offset: 8
  337. * End:
  338. */