cx25821-i2c.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/module.h>
  25. #include <linux/i2c.h>
  26. #include "cx25821.h"
  27. static unsigned int i2c_debug;
  28. module_param(i2c_debug, int, 0644);
  29. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  30. static unsigned int i2c_scan;
  31. module_param(i2c_scan, int, 0444);
  32. MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
  33. #define dprintk(level, fmt, arg...) \
  34. do { \
  35. 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 cx25821_i2c *bus = i2c_adap->algo_data;
  45. struct cx25821_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 cx25821_i2c *bus = i2c_adap->algo_data;
  51. struct cx25821_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 cx25821_i2c *bus = i2c_adap->algo_data;
  70. struct cx25821_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. if (!(ctrl & I2C_NOSTOP))
  107. printk(" >\n");
  108. }
  109. for (cnt = 1; cnt < msg->len; cnt++) {
  110. /* following bytes */
  111. wdata = msg->buf[cnt];
  112. ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
  113. if (cnt < msg->len - 1)
  114. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  115. else if (joined_rlen)
  116. ctrl |= I2C_NOSTOP;
  117. cx_write(bus->reg_addr, addr);
  118. cx_write(bus->reg_wdata, wdata);
  119. cx_write(bus->reg_ctrl, ctrl);
  120. retval = i2c_wait_done(i2c_adap);
  121. if (retval < 0)
  122. goto err;
  123. if (retval == 0)
  124. goto eio;
  125. if (i2c_debug) {
  126. dprintk(1, " %02x", msg->buf[cnt]);
  127. if (!(ctrl & I2C_NOSTOP))
  128. dprintk(1, " >\n");
  129. }
  130. }
  131. return msg->len;
  132. eio:
  133. retval = -EIO;
  134. err:
  135. if (i2c_debug)
  136. pr_err(" ERR: %d\n", retval);
  137. return retval;
  138. }
  139. static int i2c_readbytes(struct i2c_adapter *i2c_adap,
  140. const struct i2c_msg *msg, int joined)
  141. {
  142. struct cx25821_i2c *bus = i2c_adap->algo_data;
  143. struct cx25821_dev *dev = bus->dev;
  144. u32 ctrl, cnt;
  145. int retval;
  146. if (i2c_debug && !joined)
  147. dprintk(1, "6-%s(msg->len=%d)\n", __func__, msg->len);
  148. /* Deal with i2c probe functions with zero payload */
  149. if (msg->len == 0) {
  150. cx_write(bus->reg_addr, msg->addr << 25);
  151. cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
  152. if (!i2c_wait_done(i2c_adap))
  153. return -EIO;
  154. if (!i2c_slave_did_ack(i2c_adap))
  155. return -EIO;
  156. dprintk(1, "%s(): returns 0\n", __func__);
  157. return 0;
  158. }
  159. if (i2c_debug) {
  160. if (joined)
  161. dprintk(1, " R");
  162. else
  163. dprintk(1, " <R %02x", (msg->addr << 1) + 1);
  164. }
  165. for (cnt = 0; cnt < msg->len; cnt++) {
  166. ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
  167. if (cnt < msg->len - 1)
  168. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  169. cx_write(bus->reg_addr, msg->addr << 25);
  170. cx_write(bus->reg_ctrl, ctrl);
  171. retval = i2c_wait_done(i2c_adap);
  172. if (retval < 0)
  173. goto err;
  174. if (retval == 0)
  175. goto eio;
  176. msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
  177. if (i2c_debug) {
  178. dprintk(1, " %02x", msg->buf[cnt]);
  179. if (!(ctrl & I2C_NOSTOP))
  180. dprintk(1, " >\n");
  181. }
  182. }
  183. return msg->len;
  184. eio:
  185. retval = -EIO;
  186. err:
  187. if (i2c_debug)
  188. pr_err(" ERR: %d\n", retval);
  189. return retval;
  190. }
  191. static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  192. {
  193. struct cx25821_i2c *bus = i2c_adap->algo_data;
  194. struct cx25821_dev *dev = bus->dev;
  195. int i, retval = 0;
  196. dprintk(1, "%s(num = %d)\n", __func__, num);
  197. for (i = 0; i < num; i++) {
  198. dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  199. __func__, num, msgs[i].addr, msgs[i].len);
  200. if (msgs[i].flags & I2C_M_RD) {
  201. /* read */
  202. retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
  203. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  204. msgs[i].addr == msgs[i + 1].addr) {
  205. /* write then read from same address */
  206. retval = i2c_sendbytes(i2c_adap, &msgs[i],
  207. msgs[i + 1].len);
  208. if (retval < 0)
  209. goto err;
  210. i++;
  211. retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
  212. } else {
  213. /* write */
  214. retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
  215. }
  216. if (retval < 0)
  217. goto err;
  218. }
  219. return num;
  220. err:
  221. return retval;
  222. }
  223. static u32 cx25821_functionality(struct i2c_adapter *adap)
  224. {
  225. return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C | I2C_FUNC_SMBUS_WORD_DATA |
  226. I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
  227. }
  228. static const struct i2c_algorithm cx25821_i2c_algo_template = {
  229. .master_xfer = i2c_xfer,
  230. .functionality = cx25821_functionality,
  231. #ifdef NEED_ALGO_CONTROL
  232. .algo_control = dummy_algo_control,
  233. #endif
  234. };
  235. static struct i2c_adapter cx25821_i2c_adap_template = {
  236. .name = "cx25821",
  237. .owner = THIS_MODULE,
  238. .algo = &cx25821_i2c_algo_template,
  239. };
  240. static struct i2c_client cx25821_i2c_client_template = {
  241. .name = "cx25821 internal",
  242. };
  243. /* init + register i2c adapter */
  244. int cx25821_i2c_register(struct cx25821_i2c *bus)
  245. {
  246. struct cx25821_dev *dev = bus->dev;
  247. dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
  248. bus->i2c_adap = cx25821_i2c_adap_template;
  249. bus->i2c_client = cx25821_i2c_client_template;
  250. bus->i2c_adap.dev.parent = &dev->pci->dev;
  251. strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
  252. bus->i2c_adap.algo_data = bus;
  253. i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
  254. i2c_add_adapter(&bus->i2c_adap);
  255. bus->i2c_client.adapter = &bus->i2c_adap;
  256. /* set up the I2c */
  257. bus->i2c_client.addr = (0x88 >> 1);
  258. return bus->i2c_rc;
  259. }
  260. int cx25821_i2c_unregister(struct cx25821_i2c *bus)
  261. {
  262. i2c_del_adapter(&bus->i2c_adap);
  263. return 0;
  264. }
  265. #if 0 /* Currently unused */
  266. static void cx25821_av_clk(struct cx25821_dev *dev, int enable)
  267. {
  268. /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
  269. char buffer[3];
  270. struct i2c_msg msg;
  271. dprintk(1, "%s(enabled = %d)\n", __func__, enable);
  272. /* Register 0x144 */
  273. buffer[0] = 0x01;
  274. buffer[1] = 0x44;
  275. if (enable == 1)
  276. buffer[2] = 0x05;
  277. else
  278. buffer[2] = 0x00;
  279. msg.addr = 0x44;
  280. msg.flags = I2C_M_TEN;
  281. msg.len = 3;
  282. msg.buf = buffer;
  283. i2c_xfer(&dev->i2c_bus[0].i2c_adap, &msg, 1);
  284. }
  285. #endif
  286. int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value)
  287. {
  288. struct i2c_client *client = &bus->i2c_client;
  289. int v = 0;
  290. u8 addr[2] = { 0, 0 };
  291. u8 buf[4] = { 0, 0, 0, 0 };
  292. struct i2c_msg msgs[2] = {
  293. {
  294. .addr = client->addr,
  295. .flags = 0,
  296. .len = 2,
  297. .buf = addr,
  298. }, {
  299. .addr = client->addr,
  300. .flags = I2C_M_RD,
  301. .len = 4,
  302. .buf = buf,
  303. }
  304. };
  305. addr[0] = (reg_addr >> 8);
  306. addr[1] = (reg_addr & 0xff);
  307. msgs[0].addr = 0x44;
  308. msgs[1].addr = 0x44;
  309. i2c_xfer(client->adapter, msgs, 2);
  310. v = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  311. *value = v;
  312. return v;
  313. }
  314. int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value)
  315. {
  316. struct i2c_client *client = &bus->i2c_client;
  317. int retval = 0;
  318. u8 buf[6] = { 0, 0, 0, 0, 0, 0 };
  319. struct i2c_msg msgs[1] = {
  320. {
  321. .addr = client->addr,
  322. .flags = 0,
  323. .len = 6,
  324. .buf = buf,
  325. }
  326. };
  327. buf[0] = reg_addr >> 8;
  328. buf[1] = reg_addr & 0xff;
  329. buf[5] = (value >> 24) & 0xff;
  330. buf[4] = (value >> 16) & 0xff;
  331. buf[3] = (value >> 8) & 0xff;
  332. buf[2] = value & 0xff;
  333. client->flags = 0;
  334. msgs[0].addr = 0x44;
  335. retval = i2c_xfer(client->adapter, msgs, 1);
  336. return retval;
  337. }