cx23885-i2c.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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...) do { \
  35. if (i2c_debug >= level) \
  36. printk(KERN_DEBUG "%s: " 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", __FUNCTION__,
  75. msg->len, joined_rlen);
  76. else
  77. dprintk(1, "%s(msg->len=%d)\n", __FUNCTION__, 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", __FUNCTION__);
  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. printk(" %02x", msg->buf[cnt]);
  128. if (!(ctrl & I2C_NOSTOP))
  129. printk(" >\n");
  130. }
  131. }
  132. return msg->len;
  133. eio:
  134. retval = -EIO;
  135. err:
  136. if (i2c_debug)
  137. printk(" 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", __FUNCTION__, 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", __FUNCTION__);
  158. return 0;
  159. }
  160. if (i2c_debug) {
  161. if (joined)
  162. printk(" R");
  163. else
  164. printk(" <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. printk(" %02x", msg->buf[cnt]);
  180. if (!(ctrl & I2C_NOSTOP))
  181. printk(" >\n");
  182. }
  183. }
  184. return msg->len;
  185. eio:
  186. retval = -EIO;
  187. err:
  188. if (i2c_debug)
  189. printk(" 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", __FUNCTION__, num);
  199. for (i = 0 ; i < num; i++) {
  200. dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  201. __FUNCTION__, 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 int attach_inform(struct i2c_client *client)
  226. {
  227. struct cx23885_i2c *bus = i2c_get_adapdata(client->adapter);
  228. struct cx23885_dev *dev = bus->dev;
  229. struct tuner_setup tun_setup;
  230. dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
  231. client->driver->driver.name, client->addr, client->name);
  232. if (!client->driver->command)
  233. return 0;
  234. if (dev->tuner_type != UNSET) {
  235. dprintk(1, "%s (tuner) i2c attach [addr=0x%x,client=%s]\n",
  236. client->driver->driver.name, client->addr,
  237. client->name);
  238. if ((dev->tuner_addr == ADDR_UNSET) ||
  239. (dev->tuner_addr == client->addr)) {
  240. dprintk(1, "%s (tuner || addr UNSET)\n",
  241. client->driver->driver.name);
  242. dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
  243. client->driver->driver.name,
  244. client->addr, client->name);
  245. tun_setup.mode_mask = T_ANALOG_TV;
  246. tun_setup.type = dev->tuner_type;
  247. tun_setup.addr = dev->tuner_addr;
  248. client->driver->command(client, TUNER_SET_TYPE_ADDR,
  249. &tun_setup);
  250. }
  251. }
  252. return 0;
  253. }
  254. static int detach_inform(struct i2c_client *client)
  255. {
  256. struct cx23885_dev *dev = i2c_get_adapdata(client->adapter);
  257. dprintk(1, "i2c detach [client=%s]\n", client->name);
  258. return 0;
  259. }
  260. void cx23885_call_i2c_clients(struct cx23885_i2c *bus,
  261. unsigned int cmd, void *arg)
  262. {
  263. if (bus->i2c_rc != 0)
  264. return;
  265. i2c_clients_command(&bus->i2c_adap, cmd, arg);
  266. }
  267. static u32 cx23885_functionality(struct i2c_adapter *adap)
  268. {
  269. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
  270. }
  271. static struct i2c_algorithm cx23885_i2c_algo_template = {
  272. .master_xfer = i2c_xfer,
  273. .functionality = cx23885_functionality,
  274. };
  275. /* ----------------------------------------------------------------------- */
  276. static struct i2c_adapter cx23885_i2c_adap_template = {
  277. .name = "cx23885",
  278. .owner = THIS_MODULE,
  279. .id = I2C_HW_B_CX23885,
  280. .algo = &cx23885_i2c_algo_template,
  281. .class = I2C_CLASS_TV_ANALOG,
  282. .client_register = attach_inform,
  283. .client_unregister = detach_inform,
  284. };
  285. static struct i2c_client cx23885_i2c_client_template = {
  286. .name = "cx23885 internal",
  287. };
  288. static char *i2c_devs[128] = {
  289. [ 0x1c >> 1 ] = "lgdt3303",
  290. [ 0x86 >> 1 ] = "tda9887",
  291. [ 0x32 >> 1 ] = "cx24227",
  292. [ 0x88 >> 1 ] = "cx25837",
  293. [ 0x84 >> 1 ] = "tda8295",
  294. [ 0xa0 >> 1 ] = "eeprom",
  295. [ 0xc0 >> 1 ] = "tuner/mt2131/tda8275",
  296. [ 0xc2 >> 1 ] = "tuner/mt2131/tda8275/xc5000",
  297. };
  298. static void do_i2c_scan(char *name, struct i2c_client *c)
  299. {
  300. unsigned char buf;
  301. int i, rc;
  302. for (i = 0; i < 128; i++) {
  303. c->addr = i;
  304. rc = i2c_master_recv(c, &buf, 0);
  305. if (rc < 0)
  306. continue;
  307. printk("%s: i2c scan: found device @ 0x%x [%s]\n",
  308. name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
  309. }
  310. }
  311. /* init + register i2c algo-bit adapter */
  312. int cx23885_i2c_register(struct cx23885_i2c *bus)
  313. {
  314. struct cx23885_dev *dev = bus->dev;
  315. dprintk(1, "%s(bus = %d)\n", __FUNCTION__, bus->nr);
  316. memcpy(&bus->i2c_adap, &cx23885_i2c_adap_template,
  317. sizeof(bus->i2c_adap));
  318. memcpy(&bus->i2c_algo, &cx23885_i2c_algo_template,
  319. sizeof(bus->i2c_algo));
  320. memcpy(&bus->i2c_client, &cx23885_i2c_client_template,
  321. sizeof(bus->i2c_client));
  322. bus->i2c_adap.dev.parent = &dev->pci->dev;
  323. strlcpy(bus->i2c_adap.name, bus->dev->name,
  324. sizeof(bus->i2c_adap.name));
  325. bus->i2c_algo.data = bus;
  326. bus->i2c_adap.algo_data = bus;
  327. i2c_set_adapdata(&bus->i2c_adap, bus);
  328. i2c_add_adapter(&bus->i2c_adap);
  329. bus->i2c_client.adapter = &bus->i2c_adap;
  330. if (0 == bus->i2c_rc) {
  331. printk("%s: i2c bus %d registered\n", dev->name, bus->nr);
  332. if (i2c_scan)
  333. do_i2c_scan(dev->name, &bus->i2c_client);
  334. } else
  335. printk("%s: i2c bus %d register FAILED\n", dev->name, bus->nr);
  336. return bus->i2c_rc;
  337. }
  338. int cx23885_i2c_unregister(struct cx23885_i2c *bus)
  339. {
  340. i2c_del_adapter(&bus->i2c_adap);
  341. return 0;
  342. }
  343. /* ----------------------------------------------------------------------- */
  344. /*
  345. * Local variables:
  346. * c-basic-offset: 8
  347. * End:
  348. */