cx23885-i2c.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Driver for the Conexant CX23885 PCIe bridge
  3. *
  4. * Copyright (c) 2006 Steven Toth <stoth@hauppauge.com>
  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 = 0;
  29. module_param(i2c_debug, int, 0644);
  30. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  31. static unsigned int i2c_scan = 0;
  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...) if (i2c_debug >= level) \
  35. printk(KERN_DEBUG "%s: " fmt, dev->name , ## arg)
  36. #define I2C_WAIT_DELAY 32
  37. #define I2C_WAIT_RETRY 64
  38. #define I2C_EXTEND (1 << 3)
  39. #define I2C_NOSTOP (1 << 4)
  40. static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
  41. {
  42. struct cx23885_i2c *bus = i2c_adap->algo_data;
  43. struct cx23885_dev *dev = bus->dev;
  44. return cx_read(bus->reg_stat) & 0x01;
  45. }
  46. static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
  47. {
  48. struct cx23885_i2c *bus = i2c_adap->algo_data;
  49. struct cx23885_dev *dev = bus->dev;
  50. return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
  51. }
  52. static int i2c_wait_done(struct i2c_adapter *i2c_adap)
  53. {
  54. int count;
  55. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  56. if (!i2c_is_busy(i2c_adap))
  57. break;
  58. udelay(I2C_WAIT_DELAY);
  59. }
  60. if (I2C_WAIT_RETRY == count)
  61. return 0;
  62. return 1;
  63. }
  64. static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
  65. const struct i2c_msg *msg, int last)
  66. {
  67. struct cx23885_i2c *bus = i2c_adap->algo_data;
  68. struct cx23885_dev *dev = bus->dev;
  69. u32 wdata, addr, ctrl;
  70. int retval, cnt;
  71. dprintk(1, "%s(msg->len=%d, last=%d)\n", __FUNCTION__, msg->len, last);
  72. /* Deal with i2c probe functions with zero payload */
  73. if (msg->len == 0) {
  74. cx_write(bus->reg_addr, msg->addr << 25);
  75. cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
  76. if (!i2c_wait_done(i2c_adap))
  77. return -EIO;
  78. if (!i2c_slave_did_ack(i2c_adap))
  79. return -EIO;
  80. dprintk(1, "%s() returns 0\n", __FUNCTION__);
  81. return 0;
  82. }
  83. /* dev, reg + first byte */
  84. addr = (msg->addr << 25) | msg->buf[0];
  85. wdata = msg->buf[0];
  86. ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
  87. if (msg->len > 1)
  88. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  89. cx_write(bus->reg_addr, addr);
  90. cx_write(bus->reg_wdata, wdata);
  91. cx_write(bus->reg_ctrl, ctrl);
  92. retval = i2c_wait_done(i2c_adap);
  93. if (retval < 0)
  94. goto err;
  95. if (retval == 0)
  96. goto eio;
  97. if (i2c_debug) {
  98. printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
  99. if (!(ctrl & I2C_NOSTOP))
  100. printk(" >\n");
  101. }
  102. for (cnt = 1; cnt < msg->len; cnt++ ) {
  103. /* following bytes */
  104. wdata = msg->buf[cnt];
  105. ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
  106. if (cnt < msg->len - 1)
  107. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  108. cx_write(bus->reg_addr, addr);
  109. cx_write(bus->reg_wdata, wdata);
  110. cx_write(bus->reg_ctrl, ctrl);
  111. retval = i2c_wait_done(i2c_adap);
  112. if (retval < 0)
  113. goto err;
  114. if (retval == 0)
  115. goto eio;
  116. if (i2c_debug) {
  117. printk(" %02x", msg->buf[cnt]);
  118. if (!(ctrl & I2C_NOSTOP))
  119. printk(" >\n");
  120. }
  121. }
  122. return msg->len;
  123. eio:
  124. retval = -EIO;
  125. err:
  126. printk(" 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 last)
  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. dprintk(1, "%s(msg->len=%d, last=%d)\n", __FUNCTION__, msg->len, last);
  137. /* Deal with i2c probe functions with zero payload */
  138. if (msg->len == 0) {
  139. cx_write(bus->reg_addr, msg->addr << 25);
  140. cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
  141. if (!i2c_wait_done(i2c_adap))
  142. return -EIO;
  143. if (!i2c_slave_did_ack(i2c_adap))
  144. return -EIO;
  145. dprintk(1, "%s() returns 0\n", __FUNCTION__);
  146. return 0;
  147. }
  148. if (i2c_debug)
  149. printk(" <R %02x", (msg->addr << 1) + 1);
  150. for(cnt = 0; cnt < msg->len; cnt++) {
  151. ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
  152. if (cnt < msg->len - 1)
  153. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  154. cx_write(bus->reg_addr, msg->addr << 25);
  155. cx_write(bus->reg_ctrl, ctrl);
  156. retval = i2c_wait_done(i2c_adap);
  157. if (retval < 0)
  158. goto err;
  159. if (retval == 0)
  160. goto eio;
  161. msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
  162. if (i2c_debug) {
  163. printk(" %02x", msg->buf[cnt]);
  164. if (!(ctrl & I2C_NOSTOP))
  165. printk(" >\n");
  166. }
  167. }
  168. return msg->len;
  169. eio:
  170. retval = -EIO;
  171. err:
  172. printk(" ERR: %d\n", retval);
  173. return retval;
  174. }
  175. static int i2c_xfer(struct i2c_adapter *i2c_adap,
  176. struct i2c_msg *msgs, int num)
  177. {
  178. struct cx23885_i2c *bus = i2c_adap->algo_data;
  179. struct cx23885_dev *dev = bus->dev;
  180. int i, retval = 0;
  181. dprintk(1, "%s(num = %d)\n", __FUNCTION__, num);
  182. for (i = 0 ; i < num; i++) {
  183. dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  184. __FUNCTION__, num, msgs[i].addr, msgs[i].len);
  185. if (msgs[i].flags & I2C_M_RD) {
  186. /* read */
  187. retval = i2c_readbytes(i2c_adap, &msgs[i], i+1 == num);
  188. if (retval < 0)
  189. goto err;
  190. } else {
  191. /* write */
  192. retval = i2c_sendbytes(i2c_adap, &msgs[i], i+1 == num);
  193. if (retval < 0)
  194. goto err;
  195. }
  196. }
  197. return num;
  198. err:
  199. return retval;
  200. }
  201. static int attach_inform(struct i2c_client *client)
  202. {
  203. struct cx23885_dev *dev = i2c_get_adapdata(client->adapter);
  204. dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
  205. client->driver->driver.name, client->addr, client->name);
  206. if (!client->driver->command)
  207. return 0;
  208. return 0;
  209. }
  210. static int detach_inform(struct i2c_client *client)
  211. {
  212. struct cx23885_dev *dev = i2c_get_adapdata(client->adapter);
  213. dprintk(1, "i2c detach [client=%s]\n", client->name);
  214. return 0;
  215. }
  216. void cx23885_call_i2c_clients(struct cx23885_i2c *bus,
  217. unsigned int cmd, void *arg)
  218. {
  219. if (bus->i2c_rc != 0)
  220. return;
  221. i2c_clients_command(&bus->i2c_adap, cmd, arg);
  222. }
  223. static u32 cx23885_functionality(struct i2c_adapter *adap)
  224. {
  225. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  226. }
  227. static struct i2c_algorithm cx23885_i2c_algo_template = {
  228. .master_xfer = i2c_xfer,
  229. .functionality = cx23885_functionality,
  230. };
  231. /* ----------------------------------------------------------------------- */
  232. static struct i2c_adapter cx23885_i2c_adap_template = {
  233. .name = "cx23885",
  234. .owner = THIS_MODULE,
  235. .id = I2C_HW_B_CX23885,
  236. .algo = &cx23885_i2c_algo_template,
  237. .client_register = attach_inform,
  238. .client_unregister = detach_inform,
  239. };
  240. static struct i2c_client cx23885_i2c_client_template = {
  241. .name = "cx23885 internal",
  242. };
  243. static char *i2c_devs[128] = {
  244. [ 0x1c >> 1 ] = "lgdt3303",
  245. [ 0x86 >> 1 ] = "tda9887",
  246. [ 0x32 >> 1 ] = "cx24227",
  247. [ 0x88 >> 1 ] = "cx25837",
  248. [ 0x84 >> 1 ] = "tda8295",
  249. [ 0xa0 >> 1 ] = "eeprom",
  250. [ 0xc0 >> 1 ] = "tuner/mt2131/tda8275",
  251. [ 0xc2 >> 1 ] = "tuner/mt2131/tda8275",
  252. };
  253. static void do_i2c_scan(char *name, struct i2c_client *c)
  254. {
  255. unsigned char buf;
  256. int i, rc;
  257. for (i = 0; i < 128; i++) {
  258. c->addr = i;
  259. rc = i2c_master_recv(c, &buf, 0);
  260. if (rc < 0)
  261. continue;
  262. printk("%s: i2c scan: found device @ 0x%x [%s]\n",
  263. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  264. }
  265. }
  266. /* init + register i2c algo-bit adapter */
  267. int cx23885_i2c_register(struct cx23885_i2c *bus)
  268. {
  269. struct cx23885_dev *dev = bus->dev;
  270. dprintk(1, "%s(bus = %d)\n", __FUNCTION__, bus->nr);
  271. memcpy(&bus->i2c_adap, &cx23885_i2c_adap_template,
  272. sizeof(bus->i2c_adap));
  273. memcpy(&bus->i2c_algo, &cx23885_i2c_algo_template,
  274. sizeof(bus->i2c_algo));
  275. memcpy(&bus->i2c_client, &cx23885_i2c_client_template,
  276. sizeof(bus->i2c_client));
  277. bus->i2c_adap.dev.parent = &dev->pci->dev;
  278. strlcpy(bus->i2c_adap.name, bus->dev->name,
  279. sizeof(bus->i2c_adap.name));
  280. bus->i2c_algo.data = bus;
  281. bus->i2c_adap.algo_data = bus;
  282. i2c_add_adapter(&bus->i2c_adap);
  283. bus->i2c_client.adapter = &bus->i2c_adap;
  284. if (0 == bus->i2c_rc) {
  285. printk("%s: i2c bus %d registered\n", dev->name, bus->nr);
  286. if (i2c_scan)
  287. do_i2c_scan(dev->name, &bus->i2c_client);
  288. } else
  289. printk("%s: i2c bus %d register FAILED\n", dev->name, bus->nr);
  290. return bus->i2c_rc;
  291. }
  292. int cx23885_i2c_unregister(struct cx23885_i2c *bus)
  293. {
  294. i2c_del_adapter(&bus->i2c_adap);
  295. return 0;
  296. }
  297. /* ----------------------------------------------------------------------- */
  298. EXPORT_SYMBOL(cx23885_call_i2c_clients);
  299. /*
  300. * Local variables:
  301. * c-basic-offset: 8
  302. * End:
  303. */