i2c-designware.c 22 KB

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