cx25821-i2c.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
  6. * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. *
  17. * GNU General Public License for more details.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include "cx25821.h"
  23. static unsigned int i2c_debug;
  24. module_param(i2c_debug, int, 0644);
  25. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  26. static unsigned int i2c_scan;
  27. module_param(i2c_scan, int, 0444);
  28. MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
  29. #define dprintk(level, fmt, arg...) \
  30. do { \
  31. 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 cx25821_i2c *bus = i2c_adap->algo_data;
  41. struct cx25821_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 cx25821_i2c *bus = i2c_adap->algo_data;
  47. struct cx25821_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 cx25821_i2c *bus = i2c_adap->algo_data;
  66. struct cx25821_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 -EIO;
  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. retval = i2c_wait_done(i2c_adap);
  97. if (retval < 0)
  98. goto err;
  99. if (retval == 0)
  100. goto eio;
  101. if (i2c_debug) {
  102. if (!(ctrl & I2C_NOSTOP))
  103. printk(" >\n");
  104. }
  105. for (cnt = 1; cnt < msg->len; cnt++) {
  106. /* following bytes */
  107. wdata = msg->buf[cnt];
  108. ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
  109. if (cnt < msg->len - 1)
  110. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  111. else if (joined_rlen)
  112. ctrl |= I2C_NOSTOP;
  113. cx_write(bus->reg_addr, addr);
  114. cx_write(bus->reg_wdata, wdata);
  115. cx_write(bus->reg_ctrl, ctrl);
  116. retval = i2c_wait_done(i2c_adap);
  117. if (retval < 0)
  118. goto err;
  119. if (retval == 0)
  120. goto eio;
  121. if (i2c_debug) {
  122. dprintk(1, " %02x", msg->buf[cnt]);
  123. if (!(ctrl & I2C_NOSTOP))
  124. dprintk(1, " >\n");
  125. }
  126. }
  127. return msg->len;
  128. eio:
  129. retval = -EIO;
  130. err:
  131. if (i2c_debug)
  132. pr_err(" ERR: %d\n", retval);
  133. return retval;
  134. }
  135. static int i2c_readbytes(struct i2c_adapter *i2c_adap,
  136. const struct i2c_msg *msg, int joined)
  137. {
  138. struct cx25821_i2c *bus = i2c_adap->algo_data;
  139. struct cx25821_dev *dev = bus->dev;
  140. u32 ctrl, cnt;
  141. int retval;
  142. if (i2c_debug && !joined)
  143. dprintk(1, "6-%s(msg->len=%d)\n", __func__, msg->len);
  144. /* Deal with i2c probe functions with zero payload */
  145. if (msg->len == 0) {
  146. cx_write(bus->reg_addr, msg->addr << 25);
  147. cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
  148. if (!i2c_wait_done(i2c_adap))
  149. return -EIO;
  150. if (!i2c_slave_did_ack(i2c_adap))
  151. return -EIO;
  152. dprintk(1, "%s(): returns 0\n", __func__);
  153. return 0;
  154. }
  155. if (i2c_debug) {
  156. if (joined)
  157. dprintk(1, " R");
  158. else
  159. dprintk(1, " <R %02x", (msg->addr << 1) + 1);
  160. }
  161. for (cnt = 0; cnt < msg->len; cnt++) {
  162. ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
  163. if (cnt < msg->len - 1)
  164. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  165. cx_write(bus->reg_addr, msg->addr << 25);
  166. cx_write(bus->reg_ctrl, ctrl);
  167. retval = i2c_wait_done(i2c_adap);
  168. if (retval < 0)
  169. goto err;
  170. if (retval == 0)
  171. goto eio;
  172. msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
  173. if (i2c_debug) {
  174. dprintk(1, " %02x", msg->buf[cnt]);
  175. if (!(ctrl & I2C_NOSTOP))
  176. dprintk(1, " >\n");
  177. }
  178. }
  179. return msg->len;
  180. eio:
  181. retval = -EIO;
  182. err:
  183. if (i2c_debug)
  184. pr_err(" ERR: %d\n", retval);
  185. return retval;
  186. }
  187. static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  188. {
  189. struct cx25821_i2c *bus = i2c_adap->algo_data;
  190. struct cx25821_dev *dev = bus->dev;
  191. int i, retval = 0;
  192. dprintk(1, "%s(num = %d)\n", __func__, num);
  193. for (i = 0; i < num; i++) {
  194. dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  195. __func__, num, msgs[i].addr, msgs[i].len);
  196. if (msgs[i].flags & I2C_M_RD) {
  197. /* read */
  198. retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
  199. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  200. msgs[i].addr == msgs[i + 1].addr) {
  201. /* write then read from same address */
  202. retval = i2c_sendbytes(i2c_adap, &msgs[i],
  203. msgs[i + 1].len);
  204. if (retval < 0)
  205. goto err;
  206. i++;
  207. retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
  208. } else {
  209. /* write */
  210. retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
  211. }
  212. if (retval < 0)
  213. goto err;
  214. }
  215. return num;
  216. err:
  217. return retval;
  218. }
  219. static u32 cx25821_functionality(struct i2c_adapter *adap)
  220. {
  221. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C | I2C_FUNC_SMBUS_WORD_DATA |
  222. I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
  223. }
  224. static const struct i2c_algorithm cx25821_i2c_algo_template = {
  225. .master_xfer = i2c_xfer,
  226. .functionality = cx25821_functionality,
  227. #ifdef NEED_ALGO_CONTROL
  228. .algo_control = dummy_algo_control,
  229. #endif
  230. };
  231. static const struct i2c_adapter cx25821_i2c_adap_template = {
  232. .name = "cx25821",
  233. .owner = THIS_MODULE,
  234. .algo = &cx25821_i2c_algo_template,
  235. };
  236. static const struct i2c_client cx25821_i2c_client_template = {
  237. .name = "cx25821 internal",
  238. };
  239. /* init + register i2c adapter */
  240. int cx25821_i2c_register(struct cx25821_i2c *bus)
  241. {
  242. struct cx25821_dev *dev = bus->dev;
  243. dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
  244. bus->i2c_adap = cx25821_i2c_adap_template;
  245. bus->i2c_client = cx25821_i2c_client_template;
  246. bus->i2c_adap.dev.parent = &dev->pci->dev;
  247. strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
  248. bus->i2c_adap.algo_data = bus;
  249. i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
  250. i2c_add_adapter(&bus->i2c_adap);
  251. bus->i2c_client.adapter = &bus->i2c_adap;
  252. /* set up the I2c */
  253. bus->i2c_client.addr = (0x88 >> 1);
  254. return bus->i2c_rc;
  255. }
  256. int cx25821_i2c_unregister(struct cx25821_i2c *bus)
  257. {
  258. i2c_del_adapter(&bus->i2c_adap);
  259. return 0;
  260. }
  261. #if 0 /* Currently unused */
  262. static void cx25821_av_clk(struct cx25821_dev *dev, int enable)
  263. {
  264. /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
  265. char buffer[3];
  266. struct i2c_msg msg;
  267. dprintk(1, "%s(enabled = %d)\n", __func__, enable);
  268. /* Register 0x144 */
  269. buffer[0] = 0x01;
  270. buffer[1] = 0x44;
  271. if (enable == 1)
  272. buffer[2] = 0x05;
  273. else
  274. buffer[2] = 0x00;
  275. msg.addr = 0x44;
  276. msg.flags = I2C_M_TEN;
  277. msg.len = 3;
  278. msg.buf = buffer;
  279. i2c_xfer(&dev->i2c_bus[0].i2c_adap, &msg, 1);
  280. }
  281. #endif
  282. int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value)
  283. {
  284. struct i2c_client *client = &bus->i2c_client;
  285. int v = 0;
  286. u8 addr[2] = { 0, 0 };
  287. u8 buf[4] = { 0, 0, 0, 0 };
  288. struct i2c_msg msgs[2] = {
  289. {
  290. .addr = client->addr,
  291. .flags = 0,
  292. .len = 2,
  293. .buf = addr,
  294. }, {
  295. .addr = client->addr,
  296. .flags = I2C_M_RD,
  297. .len = 4,
  298. .buf = buf,
  299. }
  300. };
  301. addr[0] = (reg_addr >> 8);
  302. addr[1] = (reg_addr & 0xff);
  303. msgs[0].addr = 0x44;
  304. msgs[1].addr = 0x44;
  305. i2c_xfer(client->adapter, msgs, 2);
  306. v = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  307. *value = v;
  308. return v;
  309. }
  310. int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value)
  311. {
  312. struct i2c_client *client = &bus->i2c_client;
  313. int retval = 0;
  314. u8 buf[6] = { 0, 0, 0, 0, 0, 0 };
  315. struct i2c_msg msgs[1] = {
  316. {
  317. .addr = client->addr,
  318. .flags = 0,
  319. .len = 6,
  320. .buf = buf,
  321. }
  322. };
  323. buf[0] = reg_addr >> 8;
  324. buf[1] = reg_addr & 0xff;
  325. buf[5] = (value >> 24) & 0xff;
  326. buf[4] = (value >> 16) & 0xff;
  327. buf[3] = (value >> 8) & 0xff;
  328. buf[2] = value & 0xff;
  329. client->flags = 0;
  330. msgs[0].addr = 0x44;
  331. retval = i2c_xfer(client->adapter, msgs, 1);
  332. return retval;
  333. }