i2c-designware.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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_CLR_INTR 0x40
  53. #define DW_IC_CLR_RX_UNDER 0x44
  54. #define DW_IC_CLR_RX_OVER 0x48
  55. #define DW_IC_CLR_TX_OVER 0x4c
  56. #define DW_IC_CLR_RD_REQ 0x50
  57. #define DW_IC_CLR_TX_ABRT 0x54
  58. #define DW_IC_CLR_RX_DONE 0x58
  59. #define DW_IC_CLR_ACTIVITY 0x5c
  60. #define DW_IC_CLR_STOP_DET 0x60
  61. #define DW_IC_CLR_START_DET 0x64
  62. #define DW_IC_CLR_GEN_CALL 0x68
  63. #define DW_IC_ENABLE 0x6c
  64. #define DW_IC_STATUS 0x70
  65. #define DW_IC_TXFLR 0x74
  66. #define DW_IC_RXFLR 0x78
  67. #define DW_IC_COMP_PARAM_1 0xf4
  68. #define DW_IC_TX_ABRT_SOURCE 0x80
  69. #define DW_IC_CON_MASTER 0x1
  70. #define DW_IC_CON_SPEED_STD 0x2
  71. #define DW_IC_CON_SPEED_FAST 0x4
  72. #define DW_IC_CON_10BITADDR_MASTER 0x10
  73. #define DW_IC_CON_RESTART_EN 0x20
  74. #define DW_IC_CON_SLAVE_DISABLE 0x40
  75. #define DW_IC_INTR_RX_UNDER 0x001
  76. #define DW_IC_INTR_RX_OVER 0x002
  77. #define DW_IC_INTR_RX_FULL 0x004
  78. #define DW_IC_INTR_TX_OVER 0x008
  79. #define DW_IC_INTR_TX_EMPTY 0x010
  80. #define DW_IC_INTR_RD_REQ 0x020
  81. #define DW_IC_INTR_TX_ABRT 0x040
  82. #define DW_IC_INTR_RX_DONE 0x080
  83. #define DW_IC_INTR_ACTIVITY 0x100
  84. #define DW_IC_INTR_STOP_DET 0x200
  85. #define DW_IC_INTR_START_DET 0x400
  86. #define DW_IC_INTR_GEN_CALL 0x800
  87. #define DW_IC_STATUS_ACTIVITY 0x1
  88. #define DW_IC_ERR_TX_ABRT 0x1
  89. /*
  90. * status codes
  91. */
  92. #define STATUS_IDLE 0x0
  93. #define STATUS_WRITE_IN_PROGRESS 0x1
  94. #define STATUS_READ_IN_PROGRESS 0x2
  95. #define TIMEOUT 20 /* ms */
  96. /*
  97. * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
  98. *
  99. * only expected abort codes are listed here
  100. * refer to the datasheet for the full list
  101. */
  102. #define ABRT_7B_ADDR_NOACK 0
  103. #define ABRT_10ADDR1_NOACK 1
  104. #define ABRT_10ADDR2_NOACK 2
  105. #define ABRT_TXDATA_NOACK 3
  106. #define ABRT_GCALL_NOACK 4
  107. #define ABRT_GCALL_READ 5
  108. #define ABRT_SBYTE_ACKDET 7
  109. #define ABRT_SBYTE_NORSTRT 9
  110. #define ABRT_10B_RD_NORSTRT 10
  111. #define ARB_MASTER_DIS 11
  112. #define ARB_LOST 12
  113. static char *abort_sources[] = {
  114. [ABRT_7B_ADDR_NOACK] =
  115. "slave address not acknowledged (7bit mode)",
  116. [ABRT_10ADDR1_NOACK] =
  117. "first address byte not acknowledged (10bit mode)",
  118. [ABRT_10ADDR2_NOACK] =
  119. "second address byte not acknowledged (10bit mode)",
  120. [ABRT_TXDATA_NOACK] =
  121. "data not acknowledged",
  122. [ABRT_GCALL_NOACK] =
  123. "no acknowledgement for a general call",
  124. [ABRT_GCALL_READ] =
  125. "read after general call",
  126. [ABRT_SBYTE_ACKDET] =
  127. "start byte acknowledged",
  128. [ABRT_SBYTE_NORSTRT] =
  129. "trying to send start byte when restart is disabled",
  130. [ABRT_10B_RD_NORSTRT] =
  131. "trying to read when restart is disabled (10bit mode)",
  132. [ARB_MASTER_DIS] =
  133. "trying to use disabled adapter",
  134. [ARB_LOST] =
  135. "lost arbitration",
  136. };
  137. /**
  138. * struct dw_i2c_dev - private i2c-designware data
  139. * @dev: driver model device node
  140. * @base: IO registers pointer
  141. * @cmd_complete: tx completion indicator
  142. * @pump_msg: continue in progress transfers
  143. * @lock: protect this struct and IO registers
  144. * @clk: input reference clock
  145. * @cmd_err: run time hadware error code
  146. * @msgs: points to an array of messages currently being transfered
  147. * @msgs_num: the number of elements in msgs
  148. * @msg_write_idx: the element index of the current tx message in the msgs
  149. * array
  150. * @tx_buf_len: the length of the current tx buffer
  151. * @tx_buf: the current tx buffer
  152. * @msg_read_idx: the element index of the current rx message in the msgs
  153. * array
  154. * @rx_buf_len: the length of the current rx buffer
  155. * @rx_buf: the current rx buffer
  156. * @msg_err: error status of the current transfer
  157. * @status: i2c master status, one of STATUS_*
  158. * @abort_source: copy of the TX_ABRT_SOURCE register
  159. * @irq: interrupt number for the i2c master
  160. * @adapter: i2c subsystem adapter node
  161. * @tx_fifo_depth: depth of the hardware tx fifo
  162. * @rx_fifo_depth: depth of the hardware rx fifo
  163. */
  164. struct dw_i2c_dev {
  165. struct device *dev;
  166. void __iomem *base;
  167. struct completion cmd_complete;
  168. struct tasklet_struct pump_msg;
  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 the i2c master */
  283. ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
  284. DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
  285. writel(ic_con, dev->base + DW_IC_CON);
  286. }
  287. /*
  288. * Waiting for bus not busy
  289. */
  290. static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  291. {
  292. int timeout = TIMEOUT;
  293. while (readl(dev->base + DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  294. if (timeout <= 0) {
  295. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  296. return -ETIMEDOUT;
  297. }
  298. timeout--;
  299. mdelay(1);
  300. }
  301. return 0;
  302. }
  303. /*
  304. * Initiate low level master read/write transaction.
  305. * This function is called from i2c_dw_xfer when starting a transfer.
  306. * This function is also called from dw_i2c_pump_msg to continue a transfer
  307. * that is longer than the size of the TX FIFO.
  308. */
  309. static void
  310. i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
  311. {
  312. struct i2c_msg *msgs = dev->msgs;
  313. u32 ic_con, intr_mask;
  314. int tx_limit = dev->tx_fifo_depth - readl(dev->base + DW_IC_TXFLR);
  315. int rx_limit = dev->rx_fifo_depth - readl(dev->base + DW_IC_RXFLR);
  316. u32 addr = msgs[dev->msg_write_idx].addr;
  317. u32 buf_len = dev->tx_buf_len;
  318. intr_mask = DW_IC_INTR_STOP_DET | DW_IC_INTR_TX_ABRT;
  319. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  320. /* Disable the adapter */
  321. writel(0, dev->base + DW_IC_ENABLE);
  322. /* set the slave (target) address */
  323. writel(msgs[dev->msg_write_idx].addr, dev->base + DW_IC_TAR);
  324. /* if the slave address is ten bit address, enable 10BITADDR */
  325. ic_con = readl(dev->base + DW_IC_CON);
  326. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
  327. ic_con |= DW_IC_CON_10BITADDR_MASTER;
  328. else
  329. ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
  330. writel(ic_con, dev->base + DW_IC_CON);
  331. /* Enable the adapter */
  332. writel(1, dev->base + DW_IC_ENABLE);
  333. }
  334. for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
  335. /* if target address has changed, we need to
  336. * reprogram the target address in the i2c
  337. * adapter when we are done with this transfer
  338. */
  339. if (msgs[dev->msg_write_idx].addr != addr)
  340. return;
  341. if (msgs[dev->msg_write_idx].len == 0) {
  342. dev_err(dev->dev,
  343. "%s: invalid message length\n", __func__);
  344. dev->msg_err = -EINVAL;
  345. return;
  346. }
  347. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  348. /* new i2c_msg */
  349. dev->tx_buf = msgs[dev->msg_write_idx].buf;
  350. buf_len = msgs[dev->msg_write_idx].len;
  351. }
  352. while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
  353. if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
  354. writel(0x100, dev->base + DW_IC_DATA_CMD);
  355. rx_limit--;
  356. } else
  357. writel(*(dev->tx_buf++),
  358. dev->base + DW_IC_DATA_CMD);
  359. tx_limit--; buf_len--;
  360. }
  361. dev->tx_buf_len = buf_len;
  362. if (buf_len > 0) {
  363. /* more bytes to be written */
  364. intr_mask |= DW_IC_INTR_TX_EMPTY;
  365. dev->status |= STATUS_WRITE_IN_PROGRESS;
  366. break;
  367. } else
  368. dev->status &= ~STATUS_WRITE_IN_PROGRESS;
  369. }
  370. writel(intr_mask, dev->base + DW_IC_INTR_MASK);
  371. }
  372. static void
  373. i2c_dw_read(struct dw_i2c_dev *dev)
  374. {
  375. struct i2c_msg *msgs = dev->msgs;
  376. u32 addr = msgs[dev->msg_read_idx].addr;
  377. int rx_valid = readl(dev->base + DW_IC_RXFLR);
  378. for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
  379. u32 len;
  380. u8 *buf;
  381. if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
  382. continue;
  383. /* different i2c client, reprogram the i2c adapter */
  384. if (msgs[dev->msg_read_idx].addr != addr)
  385. return;
  386. if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
  387. len = msgs[dev->msg_read_idx].len;
  388. buf = msgs[dev->msg_read_idx].buf;
  389. } else {
  390. len = dev->rx_buf_len;
  391. buf = dev->rx_buf;
  392. }
  393. for (; len > 0 && rx_valid > 0; len--, rx_valid--)
  394. *buf++ = readl(dev->base + DW_IC_DATA_CMD);
  395. if (len > 0) {
  396. dev->status |= STATUS_READ_IN_PROGRESS;
  397. dev->rx_buf_len = len;
  398. dev->rx_buf = buf;
  399. return;
  400. } else
  401. dev->status &= ~STATUS_READ_IN_PROGRESS;
  402. }
  403. }
  404. /*
  405. * Prepare controller for a transaction and call i2c_dw_xfer_msg
  406. */
  407. static int
  408. i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  409. {
  410. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  411. int ret;
  412. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  413. mutex_lock(&dev->lock);
  414. INIT_COMPLETION(dev->cmd_complete);
  415. dev->msgs = msgs;
  416. dev->msgs_num = num;
  417. dev->cmd_err = 0;
  418. dev->msg_write_idx = 0;
  419. dev->msg_read_idx = 0;
  420. dev->msg_err = 0;
  421. dev->status = STATUS_IDLE;
  422. ret = i2c_dw_wait_bus_not_busy(dev);
  423. if (ret < 0)
  424. goto done;
  425. /* start the transfers */
  426. i2c_dw_xfer_msg(dev);
  427. /* wait for tx to complete */
  428. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
  429. if (ret == 0) {
  430. dev_err(dev->dev, "controller timed out\n");
  431. i2c_dw_init(dev);
  432. ret = -ETIMEDOUT;
  433. goto done;
  434. } else if (ret < 0)
  435. goto done;
  436. if (dev->msg_err) {
  437. ret = dev->msg_err;
  438. goto done;
  439. }
  440. /* no error */
  441. if (likely(!dev->cmd_err)) {
  442. /* read rx fifo, and disable the adapter */
  443. do {
  444. i2c_dw_read(dev);
  445. } while (dev->status & STATUS_READ_IN_PROGRESS);
  446. writel(0, dev->base + DW_IC_ENABLE);
  447. ret = num;
  448. goto done;
  449. }
  450. /* We have an error */
  451. if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
  452. unsigned long abort_source = dev->abort_source;
  453. int i;
  454. for_each_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) {
  455. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  456. }
  457. }
  458. ret = -EIO;
  459. done:
  460. mutex_unlock(&dev->lock);
  461. return ret;
  462. }
  463. static u32 i2c_dw_func(struct i2c_adapter *adap)
  464. {
  465. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
  466. }
  467. static void dw_i2c_pump_msg(unsigned long data)
  468. {
  469. struct dw_i2c_dev *dev = (struct dw_i2c_dev *) data;
  470. u32 intr_mask;
  471. i2c_dw_read(dev);
  472. i2c_dw_xfer_msg(dev);
  473. intr_mask = DW_IC_INTR_STOP_DET | DW_IC_INTR_TX_ABRT;
  474. if (dev->status & STATUS_WRITE_IN_PROGRESS)
  475. intr_mask |= DW_IC_INTR_TX_EMPTY;
  476. writel(intr_mask, dev->base + DW_IC_INTR_MASK);
  477. }
  478. static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
  479. {
  480. u32 stat;
  481. /*
  482. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  483. * Ths unmasked raw version of interrupt status bits are available
  484. * in the IC_RAW_INTR_STAT register.
  485. *
  486. * That is,
  487. * stat = readl(IC_INTR_STAT);
  488. * equals to,
  489. * stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);
  490. *
  491. * The raw version might be useful for debugging purposes.
  492. */
  493. stat = readl(dev->base + DW_IC_INTR_STAT);
  494. /*
  495. * Do not use the IC_CLR_INTR register to clear interrupts, or
  496. * you'll miss some interrupts, triggered during the period from
  497. * readl(IC_INTR_STAT) to readl(IC_CLR_INTR).
  498. *
  499. * Instead, use the separately-prepared IC_CLR_* registers.
  500. */
  501. if (stat & DW_IC_INTR_RX_UNDER)
  502. readl(dev->base + DW_IC_CLR_RX_UNDER);
  503. if (stat & DW_IC_INTR_RX_OVER)
  504. readl(dev->base + DW_IC_CLR_RX_OVER);
  505. if (stat & DW_IC_INTR_TX_OVER)
  506. readl(dev->base + DW_IC_CLR_TX_OVER);
  507. if (stat & DW_IC_INTR_RD_REQ)
  508. readl(dev->base + DW_IC_CLR_RD_REQ);
  509. if (stat & DW_IC_INTR_TX_ABRT) {
  510. /*
  511. * The IC_TX_ABRT_SOURCE register is cleared whenever
  512. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  513. */
  514. dev->abort_source = readl(dev->base + DW_IC_TX_ABRT_SOURCE);
  515. readl(dev->base + DW_IC_CLR_TX_ABRT);
  516. }
  517. if (stat & DW_IC_INTR_RX_DONE)
  518. readl(dev->base + DW_IC_CLR_RX_DONE);
  519. if (stat & DW_IC_INTR_ACTIVITY)
  520. readl(dev->base + DW_IC_CLR_ACTIVITY);
  521. if (stat & DW_IC_INTR_STOP_DET)
  522. readl(dev->base + DW_IC_CLR_STOP_DET);
  523. if (stat & DW_IC_INTR_START_DET)
  524. readl(dev->base + DW_IC_CLR_START_DET);
  525. if (stat & DW_IC_INTR_GEN_CALL)
  526. readl(dev->base + DW_IC_CLR_GEN_CALL);
  527. return stat;
  528. }
  529. /*
  530. * Interrupt service routine. This gets called whenever an I2C interrupt
  531. * occurs.
  532. */
  533. static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
  534. {
  535. struct dw_i2c_dev *dev = dev_id;
  536. u32 stat;
  537. stat = i2c_dw_read_clear_intrbits(dev);
  538. dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
  539. if (stat & DW_IC_INTR_TX_ABRT) {
  540. dev->cmd_err |= DW_IC_ERR_TX_ABRT;
  541. dev->status = STATUS_IDLE;
  542. } else if (stat & DW_IC_INTR_TX_EMPTY)
  543. tasklet_schedule(&dev->pump_msg);
  544. writel(0, dev->base + DW_IC_INTR_MASK); /* disable interrupts */
  545. if (stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET))
  546. complete(&dev->cmd_complete);
  547. return IRQ_HANDLED;
  548. }
  549. static struct i2c_algorithm i2c_dw_algo = {
  550. .master_xfer = i2c_dw_xfer,
  551. .functionality = i2c_dw_func,
  552. };
  553. static int __devinit dw_i2c_probe(struct platform_device *pdev)
  554. {
  555. struct dw_i2c_dev *dev;
  556. struct i2c_adapter *adap;
  557. struct resource *mem, *ioarea;
  558. int irq, r;
  559. /* NOTE: driver uses the static register mapping */
  560. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  561. if (!mem) {
  562. dev_err(&pdev->dev, "no mem resource?\n");
  563. return -EINVAL;
  564. }
  565. irq = platform_get_irq(pdev, 0);
  566. if (irq < 0) {
  567. dev_err(&pdev->dev, "no irq resource?\n");
  568. return irq; /* -ENXIO */
  569. }
  570. ioarea = request_mem_region(mem->start, resource_size(mem),
  571. pdev->name);
  572. if (!ioarea) {
  573. dev_err(&pdev->dev, "I2C region already claimed\n");
  574. return -EBUSY;
  575. }
  576. dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
  577. if (!dev) {
  578. r = -ENOMEM;
  579. goto err_release_region;
  580. }
  581. init_completion(&dev->cmd_complete);
  582. tasklet_init(&dev->pump_msg, dw_i2c_pump_msg, (unsigned long) dev);
  583. mutex_init(&dev->lock);
  584. dev->dev = get_device(&pdev->dev);
  585. dev->irq = irq;
  586. platform_set_drvdata(pdev, dev);
  587. dev->clk = clk_get(&pdev->dev, NULL);
  588. if (IS_ERR(dev->clk)) {
  589. r = -ENODEV;
  590. goto err_free_mem;
  591. }
  592. clk_enable(dev->clk);
  593. dev->base = ioremap(mem->start, resource_size(mem));
  594. if (dev->base == NULL) {
  595. dev_err(&pdev->dev, "failure mapping io resources\n");
  596. r = -EBUSY;
  597. goto err_unuse_clocks;
  598. }
  599. {
  600. u32 param1 = readl(dev->base + DW_IC_COMP_PARAM_1);
  601. dev->tx_fifo_depth = ((param1 >> 16) & 0xff) + 1;
  602. dev->rx_fifo_depth = ((param1 >> 8) & 0xff) + 1;
  603. }
  604. i2c_dw_init(dev);
  605. writel(0, dev->base + DW_IC_INTR_MASK); /* disable IRQ */
  606. r = request_irq(dev->irq, i2c_dw_isr, 0, pdev->name, dev);
  607. if (r) {
  608. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  609. goto err_iounmap;
  610. }
  611. adap = &dev->adapter;
  612. i2c_set_adapdata(adap, dev);
  613. adap->owner = THIS_MODULE;
  614. adap->class = I2C_CLASS_HWMON;
  615. strlcpy(adap->name, "Synopsys DesignWare I2C adapter",
  616. sizeof(adap->name));
  617. adap->algo = &i2c_dw_algo;
  618. adap->dev.parent = &pdev->dev;
  619. adap->nr = pdev->id;
  620. r = i2c_add_numbered_adapter(adap);
  621. if (r) {
  622. dev_err(&pdev->dev, "failure adding adapter\n");
  623. goto err_free_irq;
  624. }
  625. return 0;
  626. err_free_irq:
  627. free_irq(dev->irq, dev);
  628. err_iounmap:
  629. iounmap(dev->base);
  630. err_unuse_clocks:
  631. clk_disable(dev->clk);
  632. clk_put(dev->clk);
  633. dev->clk = NULL;
  634. err_free_mem:
  635. platform_set_drvdata(pdev, NULL);
  636. put_device(&pdev->dev);
  637. kfree(dev);
  638. err_release_region:
  639. release_mem_region(mem->start, resource_size(mem));
  640. return r;
  641. }
  642. static int __devexit dw_i2c_remove(struct platform_device *pdev)
  643. {
  644. struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
  645. struct resource *mem;
  646. platform_set_drvdata(pdev, NULL);
  647. i2c_del_adapter(&dev->adapter);
  648. put_device(&pdev->dev);
  649. clk_disable(dev->clk);
  650. clk_put(dev->clk);
  651. dev->clk = NULL;
  652. writel(0, dev->base + DW_IC_ENABLE);
  653. free_irq(dev->irq, dev);
  654. kfree(dev);
  655. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  656. release_mem_region(mem->start, resource_size(mem));
  657. return 0;
  658. }
  659. /* work with hotplug and coldplug */
  660. MODULE_ALIAS("platform:i2c_designware");
  661. static struct platform_driver dw_i2c_driver = {
  662. .remove = __devexit_p(dw_i2c_remove),
  663. .driver = {
  664. .name = "i2c_designware",
  665. .owner = THIS_MODULE,
  666. },
  667. };
  668. static int __init dw_i2c_init_driver(void)
  669. {
  670. return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe);
  671. }
  672. module_init(dw_i2c_init_driver);
  673. static void __exit dw_i2c_exit_driver(void)
  674. {
  675. platform_driver_unregister(&dw_i2c_driver);
  676. }
  677. module_exit(dw_i2c_exit_driver);
  678. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  679. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
  680. MODULE_LICENSE("GPL");