mxl111sf-i2c.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * mxl111sf-i2c.c - driver for the MaxLinear MXL111SF
  3. *
  4. * Copyright (C) 2010-2014 Michael Krufky <mkrufky@linuxtv.org>
  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. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include "mxl111sf-i2c.h"
  21. #include "mxl111sf.h"
  22. /* SW-I2C ----------------------------------------------------------------- */
  23. #define SW_I2C_ADDR 0x1a
  24. #define SW_I2C_EN 0x02
  25. #define SW_SCL_OUT 0x04
  26. #define SW_SDA_OUT 0x08
  27. #define SW_SDA_IN 0x04
  28. #define SW_I2C_BUSY_ADDR 0x2f
  29. #define SW_I2C_BUSY 0x02
  30. static int mxl111sf_i2c_bitbang_sendbyte(struct mxl111sf_state *state,
  31. u8 byte)
  32. {
  33. int i, ret;
  34. u8 data = 0;
  35. mxl_i2c("(0x%02x)", byte);
  36. ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &data);
  37. if (mxl_fail(ret))
  38. goto fail;
  39. for (i = 0; i < 8; i++) {
  40. data = (byte & (0x80 >> i)) ? SW_SDA_OUT : 0;
  41. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  42. 0x10 | SW_I2C_EN | data);
  43. if (mxl_fail(ret))
  44. goto fail;
  45. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  46. 0x10 | SW_I2C_EN | data | SW_SCL_OUT);
  47. if (mxl_fail(ret))
  48. goto fail;
  49. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  50. 0x10 | SW_I2C_EN | data);
  51. if (mxl_fail(ret))
  52. goto fail;
  53. }
  54. /* last bit was 0 so we need to release SDA */
  55. if (!(byte & 1)) {
  56. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  57. 0x10 | SW_I2C_EN | SW_SDA_OUT);
  58. if (mxl_fail(ret))
  59. goto fail;
  60. }
  61. /* CLK high for ACK readback */
  62. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  63. 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
  64. if (mxl_fail(ret))
  65. goto fail;
  66. ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &data);
  67. if (mxl_fail(ret))
  68. goto fail;
  69. /* drop the CLK after getting ACK, SDA will go high right away */
  70. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  71. 0x10 | SW_I2C_EN | SW_SDA_OUT);
  72. if (mxl_fail(ret))
  73. goto fail;
  74. if (data & SW_SDA_IN)
  75. ret = -EIO;
  76. fail:
  77. return ret;
  78. }
  79. static int mxl111sf_i2c_bitbang_recvbyte(struct mxl111sf_state *state,
  80. u8 *pbyte)
  81. {
  82. int i, ret;
  83. u8 byte = 0;
  84. u8 data = 0;
  85. mxl_i2c("()");
  86. *pbyte = 0;
  87. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  88. 0x10 | SW_I2C_EN | SW_SDA_OUT);
  89. if (mxl_fail(ret))
  90. goto fail;
  91. for (i = 0; i < 8; i++) {
  92. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  93. 0x10 | SW_I2C_EN |
  94. SW_SCL_OUT | SW_SDA_OUT);
  95. if (mxl_fail(ret))
  96. goto fail;
  97. ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &data);
  98. if (mxl_fail(ret))
  99. goto fail;
  100. if (data & SW_SDA_IN)
  101. byte |= (0x80 >> i);
  102. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  103. 0x10 | SW_I2C_EN | SW_SDA_OUT);
  104. if (mxl_fail(ret))
  105. goto fail;
  106. }
  107. *pbyte = byte;
  108. fail:
  109. return ret;
  110. }
  111. static int mxl111sf_i2c_start(struct mxl111sf_state *state)
  112. {
  113. int ret;
  114. mxl_i2c("()");
  115. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  116. 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
  117. if (mxl_fail(ret))
  118. goto fail;
  119. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  120. 0x10 | SW_I2C_EN | SW_SCL_OUT);
  121. if (mxl_fail(ret))
  122. goto fail;
  123. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  124. 0x10 | SW_I2C_EN); /* start */
  125. mxl_fail(ret);
  126. fail:
  127. return ret;
  128. }
  129. static int mxl111sf_i2c_stop(struct mxl111sf_state *state)
  130. {
  131. int ret;
  132. mxl_i2c("()");
  133. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  134. 0x10 | SW_I2C_EN); /* stop */
  135. if (mxl_fail(ret))
  136. goto fail;
  137. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  138. 0x10 | SW_I2C_EN | SW_SCL_OUT);
  139. if (mxl_fail(ret))
  140. goto fail;
  141. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  142. 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
  143. if (mxl_fail(ret))
  144. goto fail;
  145. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  146. 0x10 | SW_SCL_OUT | SW_SDA_OUT);
  147. mxl_fail(ret);
  148. fail:
  149. return ret;
  150. }
  151. static int mxl111sf_i2c_ack(struct mxl111sf_state *state)
  152. {
  153. int ret;
  154. u8 b = 0;
  155. mxl_i2c("()");
  156. ret = mxl111sf_read_reg(state, SW_I2C_BUSY_ADDR, &b);
  157. if (mxl_fail(ret))
  158. goto fail;
  159. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  160. 0x10 | SW_I2C_EN);
  161. if (mxl_fail(ret))
  162. goto fail;
  163. /* pull SDA low */
  164. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  165. 0x10 | SW_I2C_EN | SW_SCL_OUT);
  166. if (mxl_fail(ret))
  167. goto fail;
  168. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  169. 0x10 | SW_I2C_EN | SW_SDA_OUT);
  170. mxl_fail(ret);
  171. fail:
  172. return ret;
  173. }
  174. static int mxl111sf_i2c_nack(struct mxl111sf_state *state)
  175. {
  176. int ret;
  177. mxl_i2c("()");
  178. /* SDA high to signal last byte read from slave */
  179. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  180. 0x10 | SW_I2C_EN | SW_SCL_OUT | SW_SDA_OUT);
  181. if (mxl_fail(ret))
  182. goto fail;
  183. ret = mxl111sf_write_reg(state, SW_I2C_ADDR,
  184. 0x10 | SW_I2C_EN | SW_SDA_OUT);
  185. mxl_fail(ret);
  186. fail:
  187. return ret;
  188. }
  189. /* ------------------------------------------------------------------------ */
  190. static int mxl111sf_i2c_sw_xfer_msg(struct mxl111sf_state *state,
  191. struct i2c_msg *msg)
  192. {
  193. int i, ret;
  194. mxl_i2c("()");
  195. if (msg->flags & I2C_M_RD) {
  196. ret = mxl111sf_i2c_start(state);
  197. if (mxl_fail(ret))
  198. goto fail;
  199. ret = mxl111sf_i2c_bitbang_sendbyte(state,
  200. (msg->addr << 1) | 0x01);
  201. if (mxl_fail(ret)) {
  202. mxl111sf_i2c_stop(state);
  203. goto fail;
  204. }
  205. for (i = 0; i < msg->len; i++) {
  206. ret = mxl111sf_i2c_bitbang_recvbyte(state,
  207. &msg->buf[i]);
  208. if (mxl_fail(ret)) {
  209. mxl111sf_i2c_stop(state);
  210. goto fail;
  211. }
  212. if (i < msg->len - 1)
  213. mxl111sf_i2c_ack(state);
  214. }
  215. mxl111sf_i2c_nack(state);
  216. ret = mxl111sf_i2c_stop(state);
  217. if (mxl_fail(ret))
  218. goto fail;
  219. } else {
  220. ret = mxl111sf_i2c_start(state);
  221. if (mxl_fail(ret))
  222. goto fail;
  223. ret = mxl111sf_i2c_bitbang_sendbyte(state,
  224. (msg->addr << 1) & 0xfe);
  225. if (mxl_fail(ret)) {
  226. mxl111sf_i2c_stop(state);
  227. goto fail;
  228. }
  229. for (i = 0; i < msg->len; i++) {
  230. ret = mxl111sf_i2c_bitbang_sendbyte(state,
  231. msg->buf[i]);
  232. if (mxl_fail(ret)) {
  233. mxl111sf_i2c_stop(state);
  234. goto fail;
  235. }
  236. }
  237. /* FIXME: we only want to do this on the last transaction */
  238. mxl111sf_i2c_stop(state);
  239. }
  240. fail:
  241. return ret;
  242. }
  243. /* HW-I2C ----------------------------------------------------------------- */
  244. #define USB_WRITE_I2C_CMD 0x99
  245. #define USB_READ_I2C_CMD 0xdd
  246. #define USB_END_I2C_CMD 0xfe
  247. #define USB_WRITE_I2C_CMD_LEN 26
  248. #define USB_READ_I2C_CMD_LEN 24
  249. #define I2C_MUX_REG 0x30
  250. #define I2C_CONTROL_REG 0x00
  251. #define I2C_SLAVE_ADDR_REG 0x08
  252. #define I2C_DATA_REG 0x0c
  253. #define I2C_INT_STATUS_REG 0x10
  254. static int mxl111sf_i2c_send_data(struct mxl111sf_state *state,
  255. u8 index, u8 *wdata)
  256. {
  257. int ret = mxl111sf_ctrl_msg(state->d, wdata[0],
  258. &wdata[1], 25, NULL, 0);
  259. mxl_fail(ret);
  260. return ret;
  261. }
  262. static int mxl111sf_i2c_get_data(struct mxl111sf_state *state,
  263. u8 index, u8 *wdata, u8 *rdata)
  264. {
  265. int ret = mxl111sf_ctrl_msg(state->d, wdata[0],
  266. &wdata[1], 25, rdata, 24);
  267. mxl_fail(ret);
  268. return ret;
  269. }
  270. static u8 mxl111sf_i2c_check_status(struct mxl111sf_state *state)
  271. {
  272. u8 status = 0;
  273. u8 buf[26];
  274. mxl_i2c_adv("()");
  275. buf[0] = USB_READ_I2C_CMD;
  276. buf[1] = 0x00;
  277. buf[2] = I2C_INT_STATUS_REG;
  278. buf[3] = 0x00;
  279. buf[4] = 0x00;
  280. buf[5] = USB_END_I2C_CMD;
  281. mxl111sf_i2c_get_data(state, 0, buf, buf);
  282. if (buf[1] & 0x04)
  283. status = 1;
  284. return status;
  285. }
  286. static u8 mxl111sf_i2c_check_fifo(struct mxl111sf_state *state)
  287. {
  288. u8 status = 0;
  289. u8 buf[26];
  290. mxl_i2c("()");
  291. buf[0] = USB_READ_I2C_CMD;
  292. buf[1] = 0x00;
  293. buf[2] = I2C_MUX_REG;
  294. buf[3] = 0x00;
  295. buf[4] = 0x00;
  296. buf[5] = I2C_INT_STATUS_REG;
  297. buf[6] = 0x00;
  298. buf[7] = 0x00;
  299. buf[8] = USB_END_I2C_CMD;
  300. mxl111sf_i2c_get_data(state, 0, buf, buf);
  301. if (0x08 == (buf[1] & 0x08))
  302. status = 1;
  303. if ((buf[5] & 0x02) == 0x02)
  304. mxl_i2c("(buf[5] & 0x02) == 0x02"); /* FIXME */
  305. return status;
  306. }
  307. static int mxl111sf_i2c_readagain(struct mxl111sf_state *state,
  308. u8 count, u8 *rbuf)
  309. {
  310. u8 i2c_w_data[26];
  311. u8 i2c_r_data[24];
  312. u8 i = 0;
  313. u8 fifo_status = 0;
  314. int status = 0;
  315. mxl_i2c("read %d bytes", count);
  316. while ((fifo_status == 0) && (i++ < 5))
  317. fifo_status = mxl111sf_i2c_check_fifo(state);
  318. i2c_w_data[0] = 0xDD;
  319. i2c_w_data[1] = 0x00;
  320. for (i = 2; i < 26; i++)
  321. i2c_w_data[i] = 0xFE;
  322. for (i = 0; i < count; i++) {
  323. i2c_w_data[2+(i*3)] = 0x0C;
  324. i2c_w_data[3+(i*3)] = 0x00;
  325. i2c_w_data[4+(i*3)] = 0x00;
  326. }
  327. mxl111sf_i2c_get_data(state, 0, i2c_w_data, i2c_r_data);
  328. /* Check for I2C NACK status */
  329. if (mxl111sf_i2c_check_status(state) == 1) {
  330. mxl_i2c("error!");
  331. } else {
  332. for (i = 0; i < count; i++) {
  333. rbuf[i] = i2c_r_data[(i*3)+1];
  334. mxl_i2c("%02x\t %02x",
  335. i2c_r_data[(i*3)+1],
  336. i2c_r_data[(i*3)+2]);
  337. }
  338. status = 1;
  339. }
  340. return status;
  341. }
  342. #define HWI2C400 1
  343. static int mxl111sf_i2c_hw_xfer_msg(struct mxl111sf_state *state,
  344. struct i2c_msg *msg)
  345. {
  346. int i, k, ret = 0;
  347. u16 index = 0;
  348. u8 buf[26];
  349. u8 i2c_r_data[24];
  350. u16 block_len;
  351. u16 left_over_len;
  352. u8 rd_status[8];
  353. u8 ret_status;
  354. u8 readbuff[26];
  355. mxl_i2c("addr: 0x%02x, read buff len: %d, write buff len: %d",
  356. msg->addr, (msg->flags & I2C_M_RD) ? msg->len : 0,
  357. (!(msg->flags & I2C_M_RD)) ? msg->len : 0);
  358. for (index = 0; index < 26; index++)
  359. buf[index] = USB_END_I2C_CMD;
  360. /* command to indicate data payload is destined for I2C interface */
  361. buf[0] = USB_WRITE_I2C_CMD;
  362. buf[1] = 0x00;
  363. /* enable I2C interface */
  364. buf[2] = I2C_MUX_REG;
  365. buf[3] = 0x80;
  366. buf[4] = 0x00;
  367. /* enable I2C interface */
  368. buf[5] = I2C_MUX_REG;
  369. buf[6] = 0x81;
  370. buf[7] = 0x00;
  371. /* set Timeout register on I2C interface */
  372. buf[8] = 0x14;
  373. buf[9] = 0xff;
  374. buf[10] = 0x00;
  375. #if 0
  376. /* enable Interrupts on I2C interface */
  377. buf[8] = 0x24;
  378. buf[9] = 0xF7;
  379. buf[10] = 0x00;
  380. #endif
  381. buf[11] = 0x24;
  382. buf[12] = 0xF7;
  383. buf[13] = 0x00;
  384. ret = mxl111sf_i2c_send_data(state, 0, buf);
  385. /* write data on I2C bus */
  386. if (!(msg->flags & I2C_M_RD) && (msg->len > 0)) {
  387. mxl_i2c("%d\t%02x", msg->len, msg->buf[0]);
  388. /* control register on I2C interface to initialize I2C bus */
  389. buf[2] = I2C_CONTROL_REG;
  390. buf[3] = 0x5E;
  391. buf[4] = (HWI2C400) ? 0x03 : 0x0D;
  392. /* I2C Slave device Address */
  393. buf[5] = I2C_SLAVE_ADDR_REG;
  394. buf[6] = (msg->addr);
  395. buf[7] = 0x00;
  396. buf[8] = USB_END_I2C_CMD;
  397. ret = mxl111sf_i2c_send_data(state, 0, buf);
  398. /* check for slave device status */
  399. if (mxl111sf_i2c_check_status(state) == 1) {
  400. mxl_i2c("NACK writing slave address %02x",
  401. msg->addr);
  402. /* if NACK, stop I2C bus and exit */
  403. buf[2] = I2C_CONTROL_REG;
  404. buf[3] = 0x4E;
  405. buf[4] = (HWI2C400) ? 0x03 : 0x0D;
  406. ret = -EIO;
  407. goto exit;
  408. }
  409. /* I2C interface can do I2C operations in block of 8 bytes of
  410. I2C data. calculation to figure out number of blocks of i2c
  411. data required to program */
  412. block_len = (msg->len / 8);
  413. left_over_len = (msg->len % 8);
  414. index = 0;
  415. mxl_i2c("block_len %d, left_over_len %d",
  416. block_len, left_over_len);
  417. for (index = 0; index < block_len; index++) {
  418. for (i = 0; i < 8; i++) {
  419. /* write data on I2C interface */
  420. buf[2+(i*3)] = I2C_DATA_REG;
  421. buf[3+(i*3)] = msg->buf[(index*8)+i];
  422. buf[4+(i*3)] = 0x00;
  423. }
  424. ret = mxl111sf_i2c_send_data(state, 0, buf);
  425. /* check for I2C NACK status */
  426. if (mxl111sf_i2c_check_status(state) == 1) {
  427. mxl_i2c("NACK writing slave address %02x",
  428. msg->addr);
  429. /* if NACK, stop I2C bus and exit */
  430. buf[2] = I2C_CONTROL_REG;
  431. buf[3] = 0x4E;
  432. buf[4] = (HWI2C400) ? 0x03 : 0x0D;
  433. ret = -EIO;
  434. goto exit;
  435. }
  436. }
  437. if (left_over_len) {
  438. for (k = 0; k < 26; k++)
  439. buf[k] = USB_END_I2C_CMD;
  440. buf[0] = 0x99;
  441. buf[1] = 0x00;
  442. for (i = 0; i < left_over_len; i++) {
  443. buf[2+(i*3)] = I2C_DATA_REG;
  444. buf[3+(i*3)] = msg->buf[(index*8)+i];
  445. mxl_i2c("index = %d %d data %d",
  446. index, i, msg->buf[(index*8)+i]);
  447. buf[4+(i*3)] = 0x00;
  448. }
  449. ret = mxl111sf_i2c_send_data(state, 0, buf);
  450. /* check for I2C NACK status */
  451. if (mxl111sf_i2c_check_status(state) == 1) {
  452. mxl_i2c("NACK writing slave address %02x",
  453. msg->addr);
  454. /* if NACK, stop I2C bus and exit */
  455. buf[2] = I2C_CONTROL_REG;
  456. buf[3] = 0x4E;
  457. buf[4] = (HWI2C400) ? 0x03 : 0x0D;
  458. ret = -EIO;
  459. goto exit;
  460. }
  461. }
  462. /* issue I2C STOP after write */
  463. buf[2] = I2C_CONTROL_REG;
  464. buf[3] = 0x4E;
  465. buf[4] = (HWI2C400) ? 0x03 : 0x0D;
  466. }
  467. /* read data from I2C bus */
  468. if ((msg->flags & I2C_M_RD) && (msg->len > 0)) {
  469. mxl_i2c("read buf len %d", msg->len);
  470. /* command to indicate data payload is
  471. destined for I2C interface */
  472. buf[2] = I2C_CONTROL_REG;
  473. buf[3] = 0xDF;
  474. buf[4] = (HWI2C400) ? 0x03 : 0x0D;
  475. /* I2C xfer length */
  476. buf[5] = 0x14;
  477. buf[6] = (msg->len & 0xFF);
  478. buf[7] = 0;
  479. /* I2C slave device Address */
  480. buf[8] = I2C_SLAVE_ADDR_REG;
  481. buf[9] = msg->addr;
  482. buf[10] = 0x00;
  483. buf[11] = USB_END_I2C_CMD;
  484. ret = mxl111sf_i2c_send_data(state, 0, buf);
  485. /* check for I2C NACK status */
  486. if (mxl111sf_i2c_check_status(state) == 1) {
  487. mxl_i2c("NACK reading slave address %02x",
  488. msg->addr);
  489. /* if NACK, stop I2C bus and exit */
  490. buf[2] = I2C_CONTROL_REG;
  491. buf[3] = 0xC7;
  492. buf[4] = (HWI2C400) ? 0x03 : 0x0D;
  493. ret = -EIO;
  494. goto exit;
  495. }
  496. /* I2C interface can do I2C operations in block of 8 bytes of
  497. I2C data. calculation to figure out number of blocks of
  498. i2c data required to program */
  499. block_len = ((msg->len) / 8);
  500. left_over_len = ((msg->len) % 8);
  501. index = 0;
  502. mxl_i2c("block_len %d, left_over_len %d",
  503. block_len, left_over_len);
  504. /* command to read data from I2C interface */
  505. buf[0] = USB_READ_I2C_CMD;
  506. buf[1] = 0x00;
  507. for (index = 0; index < block_len; index++) {
  508. /* setup I2C read request packet on I2C interface */
  509. for (i = 0; i < 8; i++) {
  510. buf[2+(i*3)] = I2C_DATA_REG;
  511. buf[3+(i*3)] = 0x00;
  512. buf[4+(i*3)] = 0x00;
  513. }
  514. ret = mxl111sf_i2c_get_data(state, 0, buf, i2c_r_data);
  515. /* check for I2C NACK status */
  516. if (mxl111sf_i2c_check_status(state) == 1) {
  517. mxl_i2c("NACK reading slave address %02x",
  518. msg->addr);
  519. /* if NACK, stop I2C bus and exit */
  520. buf[2] = I2C_CONTROL_REG;
  521. buf[3] = 0xC7;
  522. buf[4] = (HWI2C400) ? 0x03 : 0x0D;
  523. ret = -EIO;
  524. goto exit;
  525. }
  526. /* copy data from i2c data payload to read buffer */
  527. for (i = 0; i < 8; i++) {
  528. rd_status[i] = i2c_r_data[(i*3)+2];
  529. if (rd_status[i] == 0x04) {
  530. if (i < 7) {
  531. mxl_i2c("i2c fifo empty!"
  532. " @ %d", i);
  533. msg->buf[(index*8)+i] =
  534. i2c_r_data[(i*3)+1];
  535. /* read again */
  536. ret_status =
  537. mxl111sf_i2c_readagain(
  538. state, 8-(i+1),
  539. readbuff);
  540. if (ret_status == 1) {
  541. for (k = 0;
  542. k < 8-(i+1);
  543. k++) {
  544. msg->buf[(index*8)+(k+i+1)] =
  545. readbuff[k];
  546. mxl_i2c("read data: %02x\t %02x",
  547. msg->buf[(index*8)+(k+i)],
  548. (index*8)+(k+i));
  549. mxl_i2c("read data: %02x\t %02x",
  550. msg->buf[(index*8)+(k+i+1)],
  551. readbuff[k]);
  552. }
  553. goto stop_copy;
  554. } else {
  555. mxl_i2c("readagain "
  556. "ERROR!");
  557. }
  558. } else {
  559. msg->buf[(index*8)+i] =
  560. i2c_r_data[(i*3)+1];
  561. }
  562. } else {
  563. msg->buf[(index*8)+i] =
  564. i2c_r_data[(i*3)+1];
  565. }
  566. }
  567. stop_copy:
  568. ;
  569. }
  570. if (left_over_len) {
  571. for (k = 0; k < 26; k++)
  572. buf[k] = USB_END_I2C_CMD;
  573. buf[0] = 0xDD;
  574. buf[1] = 0x00;
  575. for (i = 0; i < left_over_len; i++) {
  576. buf[2+(i*3)] = I2C_DATA_REG;
  577. buf[3+(i*3)] = 0x00;
  578. buf[4+(i*3)] = 0x00;
  579. }
  580. ret = mxl111sf_i2c_get_data(state, 0, buf,
  581. i2c_r_data);
  582. /* check for I2C NACK status */
  583. if (mxl111sf_i2c_check_status(state) == 1) {
  584. mxl_i2c("NACK reading slave address %02x",
  585. msg->addr);
  586. /* if NACK, stop I2C bus and exit */
  587. buf[2] = I2C_CONTROL_REG;
  588. buf[3] = 0xC7;
  589. buf[4] = (HWI2C400) ? 0x03 : 0x0D;
  590. ret = -EIO;
  591. goto exit;
  592. }
  593. for (i = 0; i < left_over_len; i++) {
  594. msg->buf[(block_len*8)+i] =
  595. i2c_r_data[(i*3)+1];
  596. mxl_i2c("read data: %02x\t %02x",
  597. i2c_r_data[(i*3)+1],
  598. i2c_r_data[(i*3)+2]);
  599. }
  600. }
  601. /* indicate I2C interface to issue NACK
  602. after next I2C read op */
  603. buf[0] = USB_WRITE_I2C_CMD;
  604. buf[1] = 0x00;
  605. /* control register */
  606. buf[2] = I2C_CONTROL_REG;
  607. buf[3] = 0x17;
  608. buf[4] = (HWI2C400) ? 0x03 : 0x0D;
  609. buf[5] = USB_END_I2C_CMD;
  610. ret = mxl111sf_i2c_send_data(state, 0, buf);
  611. /* control register */
  612. buf[2] = I2C_CONTROL_REG;
  613. buf[3] = 0xC7;
  614. buf[4] = (HWI2C400) ? 0x03 : 0x0D;
  615. }
  616. exit:
  617. /* STOP and disable I2C MUX */
  618. buf[0] = USB_WRITE_I2C_CMD;
  619. buf[1] = 0x00;
  620. /* de-initilize I2C BUS */
  621. buf[5] = USB_END_I2C_CMD;
  622. mxl111sf_i2c_send_data(state, 0, buf);
  623. /* Control Register */
  624. buf[2] = I2C_CONTROL_REG;
  625. buf[3] = 0xDF;
  626. buf[4] = 0x03;
  627. /* disable I2C interface */
  628. buf[5] = I2C_MUX_REG;
  629. buf[6] = 0x00;
  630. buf[7] = 0x00;
  631. /* de-initilize I2C BUS */
  632. buf[8] = USB_END_I2C_CMD;
  633. mxl111sf_i2c_send_data(state, 0, buf);
  634. /* disable I2C interface */
  635. buf[2] = I2C_MUX_REG;
  636. buf[3] = 0x81;
  637. buf[4] = 0x00;
  638. /* disable I2C interface */
  639. buf[5] = I2C_MUX_REG;
  640. buf[6] = 0x00;
  641. buf[7] = 0x00;
  642. /* disable I2C interface */
  643. buf[8] = I2C_MUX_REG;
  644. buf[9] = 0x00;
  645. buf[10] = 0x00;
  646. buf[11] = USB_END_I2C_CMD;
  647. mxl111sf_i2c_send_data(state, 0, buf);
  648. return ret;
  649. }
  650. /* ------------------------------------------------------------------------ */
  651. int mxl111sf_i2c_xfer(struct i2c_adapter *adap,
  652. struct i2c_msg msg[], int num)
  653. {
  654. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  655. struct mxl111sf_state *state = d->priv;
  656. int hwi2c = (state->chip_rev > MXL111SF_V6);
  657. int i, ret;
  658. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  659. return -EAGAIN;
  660. for (i = 0; i < num; i++) {
  661. ret = (hwi2c) ?
  662. mxl111sf_i2c_hw_xfer_msg(state, &msg[i]) :
  663. mxl111sf_i2c_sw_xfer_msg(state, &msg[i]);
  664. if (mxl_fail(ret)) {
  665. mxl_debug_adv("failed with error %d on i2c "
  666. "transaction %d of %d, %sing %d bytes "
  667. "to/from 0x%02x", ret, i+1, num,
  668. (msg[i].flags & I2C_M_RD) ?
  669. "read" : "writ",
  670. msg[i].len, msg[i].addr);
  671. break;
  672. }
  673. }
  674. mutex_unlock(&d->i2c_mutex);
  675. return i == num ? num : -EREMOTEIO;
  676. }