i2c-designware.c 22 KB

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