i2c-designware.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * Synopsys Designware I2C adapter driver (master only).
  3. *
  4. * Based on the TI DAVINCI I2C adapter driver.
  5. *
  6. * Copyright (C) 2006 Texas Instruments.
  7. * Copyright (C) 2007 MontaVista Software Inc.
  8. * Copyright (C) 2009 Provigent Ltd.
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. * ----------------------------------------------------------------------------
  26. *
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/delay.h>
  31. #include <linux/i2c.h>
  32. #include <linux/clk.h>
  33. #include <linux/errno.h>
  34. #include <linux/sched.h>
  35. #include <linux/err.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/io.h>
  39. /*
  40. * Registers offset
  41. */
  42. #define DW_IC_CON 0x0
  43. #define DW_IC_TAR 0x4
  44. #define DW_IC_DATA_CMD 0x10
  45. #define DW_IC_SS_SCL_HCNT 0x14
  46. #define DW_IC_SS_SCL_LCNT 0x18
  47. #define DW_IC_FS_SCL_HCNT 0x1c
  48. #define DW_IC_FS_SCL_LCNT 0x20
  49. #define DW_IC_INTR_STAT 0x2c
  50. #define DW_IC_INTR_MASK 0x30
  51. #define DW_IC_RAW_INTR_STAT 0x34
  52. #define DW_IC_RX_TL 0x38
  53. #define DW_IC_TX_TL 0x3c
  54. #define DW_IC_CLR_INTR 0x40
  55. #define DW_IC_CLR_RX_UNDER 0x44
  56. #define DW_IC_CLR_RX_OVER 0x48
  57. #define DW_IC_CLR_TX_OVER 0x4c
  58. #define DW_IC_CLR_RD_REQ 0x50
  59. #define DW_IC_CLR_TX_ABRT 0x54
  60. #define DW_IC_CLR_RX_DONE 0x58
  61. #define DW_IC_CLR_ACTIVITY 0x5c
  62. #define DW_IC_CLR_STOP_DET 0x60
  63. #define DW_IC_CLR_START_DET 0x64
  64. #define DW_IC_CLR_GEN_CALL 0x68
  65. #define DW_IC_ENABLE 0x6c
  66. #define DW_IC_STATUS 0x70
  67. #define DW_IC_TXFLR 0x74
  68. #define DW_IC_RXFLR 0x78
  69. #define DW_IC_COMP_PARAM_1 0xf4
  70. #define DW_IC_TX_ABRT_SOURCE 0x80
  71. #define DW_IC_CON_MASTER 0x1
  72. #define DW_IC_CON_SPEED_STD 0x2
  73. #define DW_IC_CON_SPEED_FAST 0x4
  74. #define DW_IC_CON_10BITADDR_MASTER 0x10
  75. #define DW_IC_CON_RESTART_EN 0x20
  76. #define DW_IC_CON_SLAVE_DISABLE 0x40
  77. #define DW_IC_INTR_RX_UNDER 0x001
  78. #define DW_IC_INTR_RX_OVER 0x002
  79. #define DW_IC_INTR_RX_FULL 0x004
  80. #define DW_IC_INTR_TX_OVER 0x008
  81. #define DW_IC_INTR_TX_EMPTY 0x010
  82. #define DW_IC_INTR_RD_REQ 0x020
  83. #define DW_IC_INTR_TX_ABRT 0x040
  84. #define DW_IC_INTR_RX_DONE 0x080
  85. #define DW_IC_INTR_ACTIVITY 0x100
  86. #define DW_IC_INTR_STOP_DET 0x200
  87. #define DW_IC_INTR_START_DET 0x400
  88. #define DW_IC_INTR_GEN_CALL 0x800
  89. #define DW_IC_STATUS_ACTIVITY 0x1
  90. #define DW_IC_ERR_TX_ABRT 0x1
  91. /*
  92. * status codes
  93. */
  94. #define STATUS_IDLE 0x0
  95. #define STATUS_WRITE_IN_PROGRESS 0x1
  96. #define STATUS_READ_IN_PROGRESS 0x2
  97. #define TIMEOUT 20 /* ms */
  98. /*
  99. * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
  100. *
  101. * only expected abort codes are listed here
  102. * refer to the datasheet for the full list
  103. */
  104. #define ABRT_7B_ADDR_NOACK 0
  105. #define ABRT_10ADDR1_NOACK 1
  106. #define ABRT_10ADDR2_NOACK 2
  107. #define ABRT_TXDATA_NOACK 3
  108. #define ABRT_GCALL_NOACK 4
  109. #define ABRT_GCALL_READ 5
  110. #define ABRT_SBYTE_ACKDET 7
  111. #define ABRT_SBYTE_NORSTRT 9
  112. #define ABRT_10B_RD_NORSTRT 10
  113. #define ARB_MASTER_DIS 11
  114. #define ARB_LOST 12
  115. static char *abort_sources[] = {
  116. [ABRT_7B_ADDR_NOACK] =
  117. "slave address not acknowledged (7bit mode)",
  118. [ABRT_10ADDR1_NOACK] =
  119. "first address byte not acknowledged (10bit mode)",
  120. [ABRT_10ADDR2_NOACK] =
  121. "second address byte not acknowledged (10bit mode)",
  122. [ABRT_TXDATA_NOACK] =
  123. "data not acknowledged",
  124. [ABRT_GCALL_NOACK] =
  125. "no acknowledgement for a general call",
  126. [ABRT_GCALL_READ] =
  127. "read after general call",
  128. [ABRT_SBYTE_ACKDET] =
  129. "start byte acknowledged",
  130. [ABRT_SBYTE_NORSTRT] =
  131. "trying to send start byte when restart is disabled",
  132. [ABRT_10B_RD_NORSTRT] =
  133. "trying to read when restart is disabled (10bit mode)",
  134. [ARB_MASTER_DIS] =
  135. "trying to use disabled adapter",
  136. [ARB_LOST] =
  137. "lost arbitration",
  138. };
  139. /**
  140. * struct dw_i2c_dev - private i2c-designware data
  141. * @dev: driver model device node
  142. * @base: IO registers pointer
  143. * @cmd_complete: tx completion indicator
  144. * @lock: protect this struct and IO registers
  145. * @clk: input reference clock
  146. * @cmd_err: run time hadware error code
  147. * @msgs: points to an array of messages currently being transfered
  148. * @msgs_num: the number of elements in msgs
  149. * @msg_write_idx: the element index of the current tx message in the msgs
  150. * array
  151. * @tx_buf_len: the length of the current tx buffer
  152. * @tx_buf: the current tx buffer
  153. * @msg_read_idx: the element index of the current rx message in the msgs
  154. * array
  155. * @rx_buf_len: the length of the current rx buffer
  156. * @rx_buf: the current rx buffer
  157. * @msg_err: error status of the current transfer
  158. * @status: i2c master status, one of STATUS_*
  159. * @abort_source: copy of the TX_ABRT_SOURCE register
  160. * @irq: interrupt number for the i2c master
  161. * @adapter: i2c subsystem adapter node
  162. * @tx_fifo_depth: depth of the hardware tx fifo
  163. * @rx_fifo_depth: depth of the hardware rx fifo
  164. */
  165. struct dw_i2c_dev {
  166. struct device *dev;
  167. void __iomem *base;
  168. struct completion cmd_complete;
  169. struct mutex lock;
  170. struct clk *clk;
  171. int cmd_err;
  172. struct i2c_msg *msgs;
  173. int msgs_num;
  174. int msg_write_idx;
  175. u32 tx_buf_len;
  176. u8 *tx_buf;
  177. int msg_read_idx;
  178. u32 rx_buf_len;
  179. u8 *rx_buf;
  180. int msg_err;
  181. unsigned int status;
  182. u32 abort_source;
  183. int irq;
  184. struct i2c_adapter adapter;
  185. unsigned int tx_fifo_depth;
  186. unsigned int rx_fifo_depth;
  187. };
  188. static u32
  189. i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
  190. {
  191. /*
  192. * DesignWare I2C core doesn't seem to have solid strategy to meet
  193. * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
  194. * will result in violation of the tHD;STA spec.
  195. */
  196. if (cond)
  197. /*
  198. * Conditional expression:
  199. *
  200. * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
  201. *
  202. * This is based on the DW manuals, and represents an ideal
  203. * configuration. The resulting I2C bus speed will be
  204. * faster than any of the others.
  205. *
  206. * If your hardware is free from tHD;STA issue, try this one.
  207. */
  208. return (ic_clk * tSYMBOL + 5000) / 10000 - 8 + offset;
  209. else
  210. /*
  211. * Conditional expression:
  212. *
  213. * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
  214. *
  215. * This is just experimental rule; the tHD;STA period turned
  216. * out to be proportinal to (_HCNT + 3). With this setting,
  217. * we could meet both tHIGH and tHD;STA timing specs.
  218. *
  219. * If unsure, you'd better to take this alternative.
  220. *
  221. * The reason why we need to take into account "tf" here,
  222. * is the same as described in i2c_dw_scl_lcnt().
  223. */
  224. return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 + offset;
  225. }
  226. static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
  227. {
  228. /*
  229. * Conditional expression:
  230. *
  231. * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
  232. *
  233. * DW I2C core starts counting the SCL CNTs for the LOW period
  234. * of the SCL clock (tLOW) as soon as it pulls the SCL line.
  235. * In order to meet the tLOW timing spec, we need to take into
  236. * account the fall time of SCL signal (tf). Default tf value
  237. * should be 0.3 us, for safety.
  238. */
  239. return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 + offset;
  240. }
  241. /**
  242. * i2c_dw_init() - initialize the designware i2c master hardware
  243. * @dev: device private data
  244. *
  245. * This functions configures and enables the I2C master.
  246. * This function is called during I2C init function, and in case of timeout at
  247. * run time.
  248. */
  249. static void i2c_dw_init(struct dw_i2c_dev *dev)
  250. {
  251. u32 input_clock_khz = clk_get_rate(dev->clk) / 1000;
  252. u32 ic_con, hcnt, lcnt;
  253. /* Disable the adapter */
  254. writel(0, dev->base + DW_IC_ENABLE);
  255. /* set standard and fast speed deviders for high/low periods */
  256. /* Standard-mode */
  257. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  258. 40, /* tHD;STA = tHIGH = 4.0 us */
  259. 3, /* tf = 0.3 us */
  260. 0, /* 0: DW default, 1: Ideal */
  261. 0); /* No offset */
  262. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  263. 47, /* tLOW = 4.7 us */
  264. 3, /* tf = 0.3 us */
  265. 0); /* No offset */
  266. writel(hcnt, dev->base + DW_IC_SS_SCL_HCNT);
  267. writel(lcnt, dev->base + DW_IC_SS_SCL_LCNT);
  268. dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  269. /* Fast-mode */
  270. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  271. 6, /* tHD;STA = tHIGH = 0.6 us */
  272. 3, /* tf = 0.3 us */
  273. 0, /* 0: DW default, 1: Ideal */
  274. 0); /* No offset */
  275. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  276. 13, /* tLOW = 1.3 us */
  277. 3, /* tf = 0.3 us */
  278. 0); /* No offset */
  279. writel(hcnt, dev->base + DW_IC_FS_SCL_HCNT);
  280. writel(lcnt, dev->base + DW_IC_FS_SCL_LCNT);
  281. dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  282. /* Configure Tx/Rx FIFO threshold levels */
  283. writel(dev->tx_fifo_depth - 1, dev->base + DW_IC_TX_TL);
  284. writel(0, dev->base + DW_IC_RX_TL);
  285. /* configure the i2c master */
  286. ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
  287. DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
  288. writel(ic_con, dev->base + DW_IC_CON);
  289. }
  290. /*
  291. * Waiting for bus not busy
  292. */
  293. static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  294. {
  295. int timeout = TIMEOUT;
  296. while (readl(dev->base + DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  297. if (timeout <= 0) {
  298. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  299. return -ETIMEDOUT;
  300. }
  301. timeout--;
  302. mdelay(1);
  303. }
  304. return 0;
  305. }
  306. static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
  307. {
  308. struct i2c_msg *msgs = dev->msgs;
  309. u32 ic_con;
  310. /* Disable the adapter */
  311. writel(0, dev->base + DW_IC_ENABLE);
  312. /* set the slave (target) address */
  313. writel(msgs[dev->msg_write_idx].addr, dev->base + DW_IC_TAR);
  314. /* if the slave address is ten bit address, enable 10BITADDR */
  315. ic_con = readl(dev->base + DW_IC_CON);
  316. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
  317. ic_con |= DW_IC_CON_10BITADDR_MASTER;
  318. else
  319. ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
  320. writel(ic_con, dev->base + DW_IC_CON);
  321. /* Enable the adapter */
  322. writel(1, dev->base + DW_IC_ENABLE);
  323. }
  324. /*
  325. * Initiate low level master read/write transaction.
  326. * This function is called from i2c_dw_xfer when starting a transfer.
  327. * This function is also called from i2c_dw_isr to continue a transfer
  328. * that is longer than the size of the TX FIFO.
  329. */
  330. static void
  331. i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
  332. {
  333. struct i2c_msg *msgs = dev->msgs;
  334. u32 intr_mask;
  335. int tx_limit, rx_limit;
  336. u32 addr = msgs[dev->msg_write_idx].addr;
  337. u32 buf_len = dev->tx_buf_len;
  338. u8 *buf = dev->tx_buf;;
  339. intr_mask = DW_IC_INTR_STOP_DET | DW_IC_INTR_TX_ABRT | DW_IC_INTR_RX_FULL;
  340. for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
  341. /* if target address has changed, we need to
  342. * reprogram the target address in the i2c
  343. * adapter when we are done with this transfer
  344. */
  345. if (msgs[dev->msg_write_idx].addr != addr)
  346. return;
  347. if (msgs[dev->msg_write_idx].len == 0) {
  348. dev_err(dev->dev,
  349. "%s: invalid message length\n", __func__);
  350. dev->msg_err = -EINVAL;
  351. return;
  352. }
  353. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  354. /* new i2c_msg */
  355. buf = msgs[dev->msg_write_idx].buf;
  356. buf_len = msgs[dev->msg_write_idx].len;
  357. }
  358. tx_limit = dev->tx_fifo_depth - readl(dev->base + DW_IC_TXFLR);
  359. rx_limit = dev->rx_fifo_depth - readl(dev->base + DW_IC_RXFLR);
  360. while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
  361. if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
  362. writel(0x100, dev->base + DW_IC_DATA_CMD);
  363. rx_limit--;
  364. } else
  365. writel(*buf++, dev->base + DW_IC_DATA_CMD);
  366. tx_limit--; buf_len--;
  367. }
  368. dev->tx_buf = buf;
  369. dev->tx_buf_len = buf_len;
  370. if (buf_len > 0) {
  371. /* more bytes to be written */
  372. intr_mask |= DW_IC_INTR_TX_EMPTY;
  373. dev->status |= STATUS_WRITE_IN_PROGRESS;
  374. break;
  375. } else
  376. dev->status &= ~STATUS_WRITE_IN_PROGRESS;
  377. }
  378. writel(intr_mask, dev->base + DW_IC_INTR_MASK);
  379. }
  380. static void
  381. i2c_dw_read(struct dw_i2c_dev *dev)
  382. {
  383. struct i2c_msg *msgs = dev->msgs;
  384. u32 addr = msgs[dev->msg_read_idx].addr;
  385. int rx_valid;
  386. for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
  387. u32 len;
  388. u8 *buf;
  389. if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
  390. continue;
  391. /* different i2c client, reprogram the i2c adapter */
  392. if (msgs[dev->msg_read_idx].addr != addr)
  393. return;
  394. if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
  395. len = msgs[dev->msg_read_idx].len;
  396. buf = msgs[dev->msg_read_idx].buf;
  397. } else {
  398. len = dev->rx_buf_len;
  399. buf = dev->rx_buf;
  400. }
  401. rx_valid = readl(dev->base + DW_IC_RXFLR);
  402. for (; len > 0 && rx_valid > 0; len--, rx_valid--)
  403. *buf++ = readl(dev->base + DW_IC_DATA_CMD);
  404. if (len > 0) {
  405. dev->status |= STATUS_READ_IN_PROGRESS;
  406. dev->rx_buf_len = len;
  407. dev->rx_buf = buf;
  408. return;
  409. } else
  410. dev->status &= ~STATUS_READ_IN_PROGRESS;
  411. }
  412. }
  413. /*
  414. * Prepare controller for a transaction and call i2c_dw_xfer_msg
  415. */
  416. static int
  417. i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  418. {
  419. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  420. int ret;
  421. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  422. mutex_lock(&dev->lock);
  423. INIT_COMPLETION(dev->cmd_complete);
  424. dev->msgs = msgs;
  425. dev->msgs_num = num;
  426. dev->cmd_err = 0;
  427. dev->msg_write_idx = 0;
  428. dev->msg_read_idx = 0;
  429. dev->msg_err = 0;
  430. dev->status = STATUS_IDLE;
  431. ret = i2c_dw_wait_bus_not_busy(dev);
  432. if (ret < 0)
  433. goto done;
  434. /* start the transfers */
  435. i2c_dw_xfer_init(dev);
  436. i2c_dw_xfer_msg(dev);
  437. /* wait for tx to complete */
  438. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
  439. if (ret == 0) {
  440. dev_err(dev->dev, "controller timed out\n");
  441. i2c_dw_init(dev);
  442. ret = -ETIMEDOUT;
  443. goto done;
  444. } else if (ret < 0)
  445. goto done;
  446. if (dev->msg_err) {
  447. ret = dev->msg_err;
  448. goto done;
  449. }
  450. /* no error */
  451. if (likely(!dev->cmd_err)) {
  452. /* Disable the adapter */
  453. writel(0, dev->base + DW_IC_ENABLE);
  454. ret = num;
  455. goto done;
  456. }
  457. /* We have an error */
  458. if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
  459. unsigned long abort_source = dev->abort_source;
  460. int i;
  461. for_each_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) {
  462. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  463. }
  464. }
  465. ret = -EIO;
  466. done:
  467. mutex_unlock(&dev->lock);
  468. return ret;
  469. }
  470. static u32 i2c_dw_func(struct i2c_adapter *adap)
  471. {
  472. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
  473. }
  474. static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
  475. {
  476. u32 stat;
  477. /*
  478. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  479. * Ths unmasked raw version of interrupt status bits are available
  480. * in the IC_RAW_INTR_STAT register.
  481. *
  482. * That is,
  483. * stat = readl(IC_INTR_STAT);
  484. * equals to,
  485. * stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);
  486. *
  487. * The raw version might be useful for debugging purposes.
  488. */
  489. stat = readl(dev->base + DW_IC_INTR_STAT);
  490. /*
  491. * Do not use the IC_CLR_INTR register to clear interrupts, or
  492. * you'll miss some interrupts, triggered during the period from
  493. * readl(IC_INTR_STAT) to readl(IC_CLR_INTR).
  494. *
  495. * Instead, use the separately-prepared IC_CLR_* registers.
  496. */
  497. if (stat & DW_IC_INTR_RX_UNDER)
  498. readl(dev->base + DW_IC_CLR_RX_UNDER);
  499. if (stat & DW_IC_INTR_RX_OVER)
  500. readl(dev->base + DW_IC_CLR_RX_OVER);
  501. if (stat & DW_IC_INTR_TX_OVER)
  502. readl(dev->base + DW_IC_CLR_TX_OVER);
  503. if (stat & DW_IC_INTR_RD_REQ)
  504. readl(dev->base + DW_IC_CLR_RD_REQ);
  505. if (stat & DW_IC_INTR_TX_ABRT) {
  506. /*
  507. * The IC_TX_ABRT_SOURCE register is cleared whenever
  508. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  509. */
  510. dev->abort_source = readl(dev->base + DW_IC_TX_ABRT_SOURCE);
  511. readl(dev->base + DW_IC_CLR_TX_ABRT);
  512. }
  513. if (stat & DW_IC_INTR_RX_DONE)
  514. readl(dev->base + DW_IC_CLR_RX_DONE);
  515. if (stat & DW_IC_INTR_ACTIVITY)
  516. readl(dev->base + DW_IC_CLR_ACTIVITY);
  517. if (stat & DW_IC_INTR_STOP_DET)
  518. readl(dev->base + DW_IC_CLR_STOP_DET);
  519. if (stat & DW_IC_INTR_START_DET)
  520. readl(dev->base + DW_IC_CLR_START_DET);
  521. if (stat & DW_IC_INTR_GEN_CALL)
  522. readl(dev->base + DW_IC_CLR_GEN_CALL);
  523. return stat;
  524. }
  525. /*
  526. * Interrupt service routine. This gets called whenever an I2C interrupt
  527. * occurs.
  528. */
  529. static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
  530. {
  531. struct dw_i2c_dev *dev = dev_id;
  532. u32 stat;
  533. stat = i2c_dw_read_clear_intrbits(dev);
  534. dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
  535. if (stat & DW_IC_INTR_TX_ABRT) {
  536. dev->cmd_err |= DW_IC_ERR_TX_ABRT;
  537. dev->status = STATUS_IDLE;
  538. }
  539. if (stat & DW_IC_INTR_RX_FULL)
  540. i2c_dw_read(dev);
  541. if (stat & DW_IC_INTR_TX_EMPTY)
  542. i2c_dw_xfer_msg(dev);
  543. /*
  544. * No need to modify or disable the interrupt mask here.
  545. * i2c_dw_xfer_msg() will take care of it according to
  546. * the current transmit status.
  547. */
  548. if (stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET))
  549. complete(&dev->cmd_complete);
  550. return IRQ_HANDLED;
  551. }
  552. static struct i2c_algorithm i2c_dw_algo = {
  553. .master_xfer = i2c_dw_xfer,
  554. .functionality = i2c_dw_func,
  555. };
  556. static int __devinit dw_i2c_probe(struct platform_device *pdev)
  557. {
  558. struct dw_i2c_dev *dev;
  559. struct i2c_adapter *adap;
  560. struct resource *mem, *ioarea;
  561. int irq, r;
  562. /* NOTE: driver uses the static register mapping */
  563. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  564. if (!mem) {
  565. dev_err(&pdev->dev, "no mem resource?\n");
  566. return -EINVAL;
  567. }
  568. irq = platform_get_irq(pdev, 0);
  569. if (irq < 0) {
  570. dev_err(&pdev->dev, "no irq resource?\n");
  571. return irq; /* -ENXIO */
  572. }
  573. ioarea = request_mem_region(mem->start, resource_size(mem),
  574. pdev->name);
  575. if (!ioarea) {
  576. dev_err(&pdev->dev, "I2C region already claimed\n");
  577. return -EBUSY;
  578. }
  579. dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
  580. if (!dev) {
  581. r = -ENOMEM;
  582. goto err_release_region;
  583. }
  584. init_completion(&dev->cmd_complete);
  585. mutex_init(&dev->lock);
  586. dev->dev = get_device(&pdev->dev);
  587. dev->irq = irq;
  588. platform_set_drvdata(pdev, dev);
  589. dev->clk = clk_get(&pdev->dev, NULL);
  590. if (IS_ERR(dev->clk)) {
  591. r = -ENODEV;
  592. goto err_free_mem;
  593. }
  594. clk_enable(dev->clk);
  595. dev->base = ioremap(mem->start, resource_size(mem));
  596. if (dev->base == NULL) {
  597. dev_err(&pdev->dev, "failure mapping io resources\n");
  598. r = -EBUSY;
  599. goto err_unuse_clocks;
  600. }
  601. {
  602. u32 param1 = readl(dev->base + DW_IC_COMP_PARAM_1);
  603. dev->tx_fifo_depth = ((param1 >> 16) & 0xff) + 1;
  604. dev->rx_fifo_depth = ((param1 >> 8) & 0xff) + 1;
  605. }
  606. i2c_dw_init(dev);
  607. writel(0, dev->base + DW_IC_INTR_MASK); /* disable IRQ */
  608. r = request_irq(dev->irq, i2c_dw_isr, 0, pdev->name, dev);
  609. if (r) {
  610. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  611. goto err_iounmap;
  612. }
  613. adap = &dev->adapter;
  614. i2c_set_adapdata(adap, dev);
  615. adap->owner = THIS_MODULE;
  616. adap->class = I2C_CLASS_HWMON;
  617. strlcpy(adap->name, "Synopsys DesignWare I2C adapter",
  618. sizeof(adap->name));
  619. adap->algo = &i2c_dw_algo;
  620. adap->dev.parent = &pdev->dev;
  621. adap->nr = pdev->id;
  622. r = i2c_add_numbered_adapter(adap);
  623. if (r) {
  624. dev_err(&pdev->dev, "failure adding adapter\n");
  625. goto err_free_irq;
  626. }
  627. return 0;
  628. err_free_irq:
  629. free_irq(dev->irq, dev);
  630. err_iounmap:
  631. iounmap(dev->base);
  632. err_unuse_clocks:
  633. clk_disable(dev->clk);
  634. clk_put(dev->clk);
  635. dev->clk = NULL;
  636. err_free_mem:
  637. platform_set_drvdata(pdev, NULL);
  638. put_device(&pdev->dev);
  639. kfree(dev);
  640. err_release_region:
  641. release_mem_region(mem->start, resource_size(mem));
  642. return r;
  643. }
  644. static int __devexit dw_i2c_remove(struct platform_device *pdev)
  645. {
  646. struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
  647. struct resource *mem;
  648. platform_set_drvdata(pdev, NULL);
  649. i2c_del_adapter(&dev->adapter);
  650. put_device(&pdev->dev);
  651. clk_disable(dev->clk);
  652. clk_put(dev->clk);
  653. dev->clk = NULL;
  654. writel(0, dev->base + DW_IC_ENABLE);
  655. free_irq(dev->irq, dev);
  656. kfree(dev);
  657. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  658. release_mem_region(mem->start, resource_size(mem));
  659. return 0;
  660. }
  661. /* work with hotplug and coldplug */
  662. MODULE_ALIAS("platform:i2c_designware");
  663. static struct platform_driver dw_i2c_driver = {
  664. .remove = __devexit_p(dw_i2c_remove),
  665. .driver = {
  666. .name = "i2c_designware",
  667. .owner = THIS_MODULE,
  668. },
  669. };
  670. static int __init dw_i2c_init_driver(void)
  671. {
  672. return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe);
  673. }
  674. module_init(dw_i2c_init_driver);
  675. static void __exit dw_i2c_exit_driver(void)
  676. {
  677. platform_driver_unregister(&dw_i2c_driver);
  678. }
  679. module_exit(dw_i2c_exit_driver);
  680. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  681. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
  682. MODULE_LICENSE("GPL");