cx25821-i2c.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 "cx25821.h"
  25. #include <linux/i2c.h>
  26. static unsigned int i2c_debug;
  27. module_param(i2c_debug, int, 0644);
  28. MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
  29. static unsigned int i2c_scan;
  30. module_param(i2c_scan, int, 0444);
  31. MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
  32. #define dprintk(level, fmt, arg...) \
  33. do { \
  34. if (i2c_debug >= level) \
  35. printk(KERN_DEBUG "%s/0: " fmt, dev->name, ##arg); \
  36. } while (0)
  37. #define I2C_WAIT_DELAY 32
  38. #define I2C_WAIT_RETRY 64
  39. #define I2C_EXTEND (1 << 3)
  40. #define I2C_NOSTOP (1 << 4)
  41. static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
  42. {
  43. struct cx25821_i2c *bus = i2c_adap->algo_data;
  44. struct cx25821_dev *dev = bus->dev;
  45. return cx_read(bus->reg_stat) & 0x01;
  46. }
  47. static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
  48. {
  49. struct cx25821_i2c *bus = i2c_adap->algo_data;
  50. struct cx25821_dev *dev = bus->dev;
  51. return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
  52. }
  53. static int i2c_wait_done(struct i2c_adapter *i2c_adap)
  54. {
  55. int count;
  56. for (count = 0; count < I2C_WAIT_RETRY; count++) {
  57. if (!i2c_is_busy(i2c_adap))
  58. break;
  59. udelay(I2C_WAIT_DELAY);
  60. }
  61. if (I2C_WAIT_RETRY == count)
  62. return 0;
  63. return 1;
  64. }
  65. static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
  66. const struct i2c_msg *msg, int joined_rlen)
  67. {
  68. struct cx25821_i2c *bus = i2c_adap->algo_data;
  69. struct cx25821_dev *dev = bus->dev;
  70. u32 wdata, addr, ctrl;
  71. int retval, cnt;
  72. if (joined_rlen)
  73. dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__,
  74. msg->len, joined_rlen);
  75. else
  76. dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
  77. /* Deal with i2c probe functions with zero payload */
  78. if (msg->len == 0) {
  79. cx_write(bus->reg_addr, msg->addr << 25);
  80. cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
  81. if (!i2c_wait_done(i2c_adap))
  82. return -EIO;
  83. if (!i2c_slave_did_ack(i2c_adap))
  84. return -EIO;
  85. dprintk(1, "%s(): returns 0\n", __func__);
  86. return 0;
  87. }
  88. /* dev, reg + first byte */
  89. addr = (msg->addr << 25) | msg->buf[0];
  90. wdata = msg->buf[0];
  91. ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
  92. if (msg->len > 1)
  93. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  94. else if (joined_rlen)
  95. ctrl |= I2C_NOSTOP;
  96. cx_write(bus->reg_addr, addr);
  97. cx_write(bus->reg_wdata, wdata);
  98. cx_write(bus->reg_ctrl, ctrl);
  99. retval = i2c_wait_done(i2c_adap);
  100. if (retval < 0)
  101. goto err;
  102. if (retval == 0)
  103. goto eio;
  104. if (i2c_debug) {
  105. if (!(ctrl & I2C_NOSTOP))
  106. printk(" >\n");
  107. }
  108. for (cnt = 1; cnt < msg->len; cnt++) {
  109. /* following bytes */
  110. wdata = msg->buf[cnt];
  111. ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
  112. if (cnt < msg->len - 1)
  113. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  114. else if (joined_rlen)
  115. ctrl |= I2C_NOSTOP;
  116. cx_write(bus->reg_addr, addr);
  117. cx_write(bus->reg_wdata, wdata);
  118. cx_write(bus->reg_ctrl, ctrl);
  119. retval = i2c_wait_done(i2c_adap);
  120. if (retval < 0)
  121. goto err;
  122. if (retval == 0)
  123. goto eio;
  124. if (i2c_debug) {
  125. dprintk(1, " %02x", msg->buf[cnt]);
  126. if (!(ctrl & I2C_NOSTOP))
  127. dprintk(1, " >\n");
  128. }
  129. }
  130. return msg->len;
  131. eio:
  132. retval = -EIO;
  133. err:
  134. if (i2c_debug)
  135. pr_err(" ERR: %d\n", retval);
  136. return retval;
  137. }
  138. static int i2c_readbytes(struct i2c_adapter *i2c_adap,
  139. const struct i2c_msg *msg, int joined)
  140. {
  141. struct cx25821_i2c *bus = i2c_adap->algo_data;
  142. struct cx25821_dev *dev = bus->dev;
  143. u32 ctrl, cnt;
  144. int retval;
  145. if (i2c_debug && !joined)
  146. dprintk(1, "6-%s(msg->len=%d)\n", __func__, msg->len);
  147. /* Deal with i2c probe functions with zero payload */
  148. if (msg->len == 0) {
  149. cx_write(bus->reg_addr, msg->addr << 25);
  150. cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
  151. if (!i2c_wait_done(i2c_adap))
  152. return -EIO;
  153. if (!i2c_slave_did_ack(i2c_adap))
  154. return -EIO;
  155. dprintk(1, "%s(): returns 0\n", __func__);
  156. return 0;
  157. }
  158. if (i2c_debug) {
  159. if (joined)
  160. dprintk(1, " R");
  161. else
  162. dprintk(1, " <R %02x", (msg->addr << 1) + 1);
  163. }
  164. for (cnt = 0; cnt < msg->len; cnt++) {
  165. ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
  166. if (cnt < msg->len - 1)
  167. ctrl |= I2C_NOSTOP | I2C_EXTEND;
  168. cx_write(bus->reg_addr, msg->addr << 25);
  169. cx_write(bus->reg_ctrl, ctrl);
  170. retval = i2c_wait_done(i2c_adap);
  171. if (retval < 0)
  172. goto err;
  173. if (retval == 0)
  174. goto eio;
  175. msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
  176. if (i2c_debug) {
  177. dprintk(1, " %02x", msg->buf[cnt]);
  178. if (!(ctrl & I2C_NOSTOP))
  179. dprintk(1, " >\n");
  180. }
  181. }
  182. return msg->len;
  183. eio:
  184. retval = -EIO;
  185. err:
  186. if (i2c_debug)
  187. pr_err(" ERR: %d\n", retval);
  188. return retval;
  189. }
  190. static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  191. {
  192. struct cx25821_i2c *bus = i2c_adap->algo_data;
  193. struct cx25821_dev *dev = bus->dev;
  194. int i, retval = 0;
  195. dprintk(1, "%s(num = %d)\n", __func__, num);
  196. for (i = 0; i < num; i++) {
  197. dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
  198. __func__, num, msgs[i].addr, msgs[i].len);
  199. if (msgs[i].flags & I2C_M_RD) {
  200. /* read */
  201. retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
  202. } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
  203. msgs[i].addr == msgs[i + 1].addr) {
  204. /* write then read from same address */
  205. retval =
  206. i2c_sendbytes(i2c_adap, &msgs[i], msgs[i + 1].len);
  207. if (retval < 0)
  208. goto err;
  209. i++;
  210. retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
  211. } else {
  212. /* write */
  213. retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
  214. }
  215. if (retval < 0)
  216. goto err;
  217. }
  218. return num;
  219. err:
  220. return retval;
  221. }
  222. static u32 cx25821_functionality(struct i2c_adapter *adap)
  223. {
  224. return I2C_FUNC_SMBUS_EMUL |
  225. I2C_FUNC_I2C |
  226. I2C_FUNC_SMBUS_WORD_DATA |
  227. I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
  228. }
  229. static struct i2c_algorithm cx25821_i2c_algo_template = {
  230. .master_xfer = i2c_xfer,
  231. .functionality = cx25821_functionality,
  232. #ifdef NEED_ALGO_CONTROL
  233. .algo_control = dummy_algo_control,
  234. #endif
  235. };
  236. static struct i2c_adapter cx25821_i2c_adap_template = {
  237. .name = "cx25821",
  238. .owner = THIS_MODULE,
  239. .algo = &cx25821_i2c_algo_template,
  240. };
  241. static struct i2c_client cx25821_i2c_client_template = {
  242. .name = "cx25821 internal",
  243. };
  244. /* init + register i2c algo-bit adapter */
  245. int cx25821_i2c_register(struct cx25821_i2c *bus)
  246. {
  247. struct cx25821_dev *dev = bus->dev;
  248. dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
  249. memcpy(&bus->i2c_adap, &cx25821_i2c_adap_template,
  250. sizeof(bus->i2c_adap));
  251. memcpy(&bus->i2c_algo, &cx25821_i2c_algo_template,
  252. sizeof(bus->i2c_algo));
  253. memcpy(&bus->i2c_client, &cx25821_i2c_client_template,
  254. sizeof(bus->i2c_client));
  255. bus->i2c_adap.dev.parent = &dev->pci->dev;
  256. strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
  257. bus->i2c_algo.data = bus;
  258. bus->i2c_adap.algo_data = bus;
  259. i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
  260. i2c_add_adapter(&bus->i2c_adap);
  261. bus->i2c_client.adapter = &bus->i2c_adap;
  262. /* set up the I2c */
  263. bus->i2c_client.addr = (0x88 >> 1);
  264. return bus->i2c_rc;
  265. }
  266. int cx25821_i2c_unregister(struct cx25821_i2c *bus)
  267. {
  268. i2c_del_adapter(&bus->i2c_adap);
  269. return 0;
  270. }
  271. void cx25821_av_clk(struct cx25821_dev *dev, int enable)
  272. {
  273. /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
  274. char buffer[3];
  275. struct i2c_msg msg;
  276. dprintk(1, "%s(enabled = %d)\n", __func__, enable);
  277. /* Register 0x144 */
  278. buffer[0] = 0x01;
  279. buffer[1] = 0x44;
  280. if (enable == 1)
  281. buffer[2] = 0x05;
  282. else
  283. buffer[2] = 0x00;
  284. msg.addr = 0x44;
  285. msg.flags = I2C_M_TEN;
  286. msg.len = 3;
  287. msg.buf = buffer;
  288. i2c_xfer(&dev->i2c_bus[0].i2c_adap, &msg, 1);
  289. }
  290. int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value)
  291. {
  292. struct i2c_client *client = &bus->i2c_client;
  293. int retval = 0;
  294. int v = 0;
  295. u8 addr[2] = { 0, 0 };
  296. u8 buf[4] = { 0, 0, 0, 0 };
  297. struct i2c_msg msgs[2] = {
  298. {
  299. .addr = client->addr,
  300. .flags = 0,
  301. .len = 2,
  302. .buf = addr,
  303. }, {
  304. .addr = client->addr,
  305. .flags = I2C_M_RD,
  306. .len = 4,
  307. .buf = buf,
  308. }
  309. };
  310. addr[0] = (reg_addr >> 8);
  311. addr[1] = (reg_addr & 0xff);
  312. msgs[0].addr = 0x44;
  313. msgs[1].addr = 0x44;
  314. retval = i2c_xfer(client->adapter, msgs, 2);
  315. v = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  316. *value = v;
  317. return v;
  318. }
  319. int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value)
  320. {
  321. struct i2c_client *client = &bus->i2c_client;
  322. int retval = 0;
  323. u8 buf[6] = { 0, 0, 0, 0, 0, 0 };
  324. struct i2c_msg msgs[1] = {
  325. {
  326. .addr = client->addr,
  327. .flags = 0,
  328. .len = 6,
  329. .buf = buf,
  330. }
  331. };
  332. buf[0] = reg_addr >> 8;
  333. buf[1] = reg_addr & 0xff;
  334. buf[5] = (value >> 24) & 0xff;
  335. buf[4] = (value >> 16) & 0xff;
  336. buf[3] = (value >> 8) & 0xff;
  337. buf[2] = value & 0xff;
  338. client->flags = 0;
  339. msgs[0].addr = 0x44;
  340. retval = i2c_xfer(client->adapter, msgs, 1);
  341. return retval;
  342. }