i2c-designware.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. /**
  189. * i2c_dw_init() - initialize the designware i2c master hardware
  190. * @dev: device private data
  191. *
  192. * This functions configures and enables the I2C master.
  193. * This function is called during I2C init function, and in case of timeout at
  194. * run time.
  195. */
  196. static void i2c_dw_init(struct dw_i2c_dev *dev)
  197. {
  198. u32 input_clock_khz = clk_get_rate(dev->clk) / 1000;
  199. u32 ic_con;
  200. /* Disable the adapter */
  201. writel(0, dev->base + DW_IC_ENABLE);
  202. /* set standard and fast speed deviders for high/low periods */
  203. writel((input_clock_khz * 40 / 10000)+1, /* std speed high, 4us */
  204. dev->base + DW_IC_SS_SCL_HCNT);
  205. writel((input_clock_khz * 47 / 10000)+1, /* std speed low, 4.7us */
  206. dev->base + DW_IC_SS_SCL_LCNT);
  207. writel((input_clock_khz * 6 / 10000)+1, /* fast speed high, 0.6us */
  208. dev->base + DW_IC_FS_SCL_HCNT);
  209. writel((input_clock_khz * 13 / 10000)+1, /* fast speed low, 1.3us */
  210. dev->base + DW_IC_FS_SCL_LCNT);
  211. /* configure the i2c master */
  212. ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
  213. DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
  214. writel(ic_con, dev->base + DW_IC_CON);
  215. }
  216. /*
  217. * Waiting for bus not busy
  218. */
  219. static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  220. {
  221. int timeout = TIMEOUT;
  222. while (readl(dev->base + DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  223. if (timeout <= 0) {
  224. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  225. return -ETIMEDOUT;
  226. }
  227. timeout--;
  228. mdelay(1);
  229. }
  230. return 0;
  231. }
  232. /*
  233. * Initiate low level master read/write transaction.
  234. * This function is called from i2c_dw_xfer when starting a transfer.
  235. * This function is also called from dw_i2c_pump_msg to continue a transfer
  236. * that is longer than the size of the TX FIFO.
  237. */
  238. static void
  239. i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
  240. {
  241. struct i2c_msg *msgs = dev->msgs;
  242. u32 ic_con, intr_mask;
  243. int tx_limit = dev->tx_fifo_depth - readl(dev->base + DW_IC_TXFLR);
  244. int rx_limit = dev->rx_fifo_depth - readl(dev->base + DW_IC_RXFLR);
  245. u32 addr = msgs[dev->msg_write_idx].addr;
  246. u32 buf_len = dev->tx_buf_len;
  247. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  248. /* Disable the adapter */
  249. writel(0, dev->base + DW_IC_ENABLE);
  250. /* set the slave (target) address */
  251. writel(msgs[dev->msg_write_idx].addr, dev->base + DW_IC_TAR);
  252. /* if the slave address is ten bit address, enable 10BITADDR */
  253. ic_con = readl(dev->base + DW_IC_CON);
  254. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
  255. ic_con |= DW_IC_CON_10BITADDR_MASTER;
  256. else
  257. ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
  258. writel(ic_con, dev->base + DW_IC_CON);
  259. /* Enable the adapter */
  260. writel(1, dev->base + DW_IC_ENABLE);
  261. }
  262. for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
  263. /* if target address has changed, we need to
  264. * reprogram the target address in the i2c
  265. * adapter when we are done with this transfer
  266. */
  267. if (msgs[dev->msg_write_idx].addr != addr)
  268. return;
  269. if (msgs[dev->msg_write_idx].len == 0) {
  270. dev_err(dev->dev,
  271. "%s: invalid message length\n", __func__);
  272. dev->msg_err = -EINVAL;
  273. return;
  274. }
  275. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  276. /* new i2c_msg */
  277. dev->tx_buf = msgs[dev->msg_write_idx].buf;
  278. buf_len = msgs[dev->msg_write_idx].len;
  279. }
  280. while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
  281. if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
  282. writel(0x100, dev->base + DW_IC_DATA_CMD);
  283. rx_limit--;
  284. } else
  285. writel(*(dev->tx_buf++),
  286. dev->base + DW_IC_DATA_CMD);
  287. tx_limit--; buf_len--;
  288. }
  289. }
  290. intr_mask = DW_IC_INTR_STOP_DET | DW_IC_INTR_TX_ABRT;
  291. if (buf_len > 0) { /* more bytes to be written */
  292. intr_mask |= DW_IC_INTR_TX_EMPTY;
  293. dev->status |= STATUS_WRITE_IN_PROGRESS;
  294. } else
  295. dev->status &= ~STATUS_WRITE_IN_PROGRESS;
  296. writel(intr_mask, dev->base + DW_IC_INTR_MASK);
  297. dev->tx_buf_len = buf_len;
  298. }
  299. static void
  300. i2c_dw_read(struct dw_i2c_dev *dev)
  301. {
  302. struct i2c_msg *msgs = dev->msgs;
  303. u32 addr = msgs[dev->msg_read_idx].addr;
  304. int rx_valid = readl(dev->base + DW_IC_RXFLR);
  305. for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
  306. u32 len;
  307. u8 *buf;
  308. if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
  309. continue;
  310. /* different i2c client, reprogram the i2c adapter */
  311. if (msgs[dev->msg_read_idx].addr != addr)
  312. return;
  313. if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
  314. len = msgs[dev->msg_read_idx].len;
  315. buf = msgs[dev->msg_read_idx].buf;
  316. } else {
  317. len = dev->rx_buf_len;
  318. buf = dev->rx_buf;
  319. }
  320. for (; len > 0 && rx_valid > 0; len--, rx_valid--)
  321. *buf++ = readl(dev->base + DW_IC_DATA_CMD);
  322. if (len > 0) {
  323. dev->status |= STATUS_READ_IN_PROGRESS;
  324. dev->rx_buf_len = len;
  325. dev->rx_buf = buf;
  326. return;
  327. } else
  328. dev->status &= ~STATUS_READ_IN_PROGRESS;
  329. }
  330. }
  331. /*
  332. * Prepare controller for a transaction and call i2c_dw_xfer_msg
  333. */
  334. static int
  335. i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  336. {
  337. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  338. int ret;
  339. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  340. mutex_lock(&dev->lock);
  341. INIT_COMPLETION(dev->cmd_complete);
  342. dev->msgs = msgs;
  343. dev->msgs_num = num;
  344. dev->cmd_err = 0;
  345. dev->msg_write_idx = 0;
  346. dev->msg_read_idx = 0;
  347. dev->msg_err = 0;
  348. dev->status = STATUS_IDLE;
  349. ret = i2c_dw_wait_bus_not_busy(dev);
  350. if (ret < 0)
  351. goto done;
  352. /* start the transfers */
  353. i2c_dw_xfer_msg(dev);
  354. /* wait for tx to complete */
  355. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
  356. if (ret == 0) {
  357. dev_err(dev->dev, "controller timed out\n");
  358. i2c_dw_init(dev);
  359. ret = -ETIMEDOUT;
  360. goto done;
  361. } else if (ret < 0)
  362. goto done;
  363. if (dev->msg_err) {
  364. ret = dev->msg_err;
  365. goto done;
  366. }
  367. /* no error */
  368. if (likely(!dev->cmd_err)) {
  369. /* read rx fifo, and disable the adapter */
  370. do {
  371. i2c_dw_read(dev);
  372. } while (dev->status & STATUS_READ_IN_PROGRESS);
  373. writel(0, dev->base + DW_IC_ENABLE);
  374. ret = num;
  375. goto done;
  376. }
  377. /* We have an error */
  378. if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
  379. unsigned long abort_source = dev->abort_source;
  380. int i;
  381. for_each_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) {
  382. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  383. }
  384. }
  385. ret = -EIO;
  386. done:
  387. mutex_unlock(&dev->lock);
  388. return ret;
  389. }
  390. static u32 i2c_dw_func(struct i2c_adapter *adap)
  391. {
  392. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
  393. }
  394. static void dw_i2c_pump_msg(unsigned long data)
  395. {
  396. struct dw_i2c_dev *dev = (struct dw_i2c_dev *) data;
  397. u32 intr_mask;
  398. i2c_dw_read(dev);
  399. i2c_dw_xfer_msg(dev);
  400. intr_mask = DW_IC_INTR_STOP_DET | DW_IC_INTR_TX_ABRT;
  401. if (dev->status & STATUS_WRITE_IN_PROGRESS)
  402. intr_mask |= DW_IC_INTR_TX_EMPTY;
  403. writel(intr_mask, dev->base + DW_IC_INTR_MASK);
  404. }
  405. static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
  406. {
  407. u32 stat;
  408. /*
  409. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  410. * Ths unmasked raw version of interrupt status bits are available
  411. * in the IC_RAW_INTR_STAT register.
  412. *
  413. * That is,
  414. * stat = readl(IC_INTR_STAT);
  415. * equals to,
  416. * stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);
  417. *
  418. * The raw version might be useful for debugging purposes.
  419. */
  420. stat = readl(dev->base + DW_IC_INTR_STAT);
  421. /*
  422. * Do not use the IC_CLR_INTR register to clear interrupts, or
  423. * you'll miss some interrupts, triggered during the period from
  424. * readl(IC_INTR_STAT) to readl(IC_CLR_INTR).
  425. *
  426. * Instead, use the separately-prepared IC_CLR_* registers.
  427. */
  428. if (stat & DW_IC_INTR_RX_UNDER)
  429. readl(dev->base + DW_IC_CLR_RX_UNDER);
  430. if (stat & DW_IC_INTR_RX_OVER)
  431. readl(dev->base + DW_IC_CLR_RX_OVER);
  432. if (stat & DW_IC_INTR_TX_OVER)
  433. readl(dev->base + DW_IC_CLR_TX_OVER);
  434. if (stat & DW_IC_INTR_RD_REQ)
  435. readl(dev->base + DW_IC_CLR_RD_REQ);
  436. if (stat & DW_IC_INTR_TX_ABRT) {
  437. /*
  438. * The IC_TX_ABRT_SOURCE register is cleared whenever
  439. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  440. */
  441. dev->abort_source = readl(dev->base + DW_IC_TX_ABRT_SOURCE);
  442. readl(dev->base + DW_IC_CLR_TX_ABRT);
  443. }
  444. if (stat & DW_IC_INTR_RX_DONE)
  445. readl(dev->base + DW_IC_CLR_RX_DONE);
  446. if (stat & DW_IC_INTR_ACTIVITY)
  447. readl(dev->base + DW_IC_CLR_ACTIVITY);
  448. if (stat & DW_IC_INTR_STOP_DET)
  449. readl(dev->base + DW_IC_CLR_STOP_DET);
  450. if (stat & DW_IC_INTR_START_DET)
  451. readl(dev->base + DW_IC_CLR_START_DET);
  452. if (stat & DW_IC_INTR_GEN_CALL)
  453. readl(dev->base + DW_IC_CLR_GEN_CALL);
  454. return stat;
  455. }
  456. /*
  457. * Interrupt service routine. This gets called whenever an I2C interrupt
  458. * occurs.
  459. */
  460. static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
  461. {
  462. struct dw_i2c_dev *dev = dev_id;
  463. u32 stat;
  464. stat = i2c_dw_read_clear_intrbits(dev);
  465. dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
  466. if (stat & DW_IC_INTR_TX_ABRT) {
  467. dev->cmd_err |= DW_IC_ERR_TX_ABRT;
  468. dev->status = STATUS_IDLE;
  469. } else if (stat & DW_IC_INTR_TX_EMPTY)
  470. tasklet_schedule(&dev->pump_msg);
  471. writel(0, dev->base + DW_IC_INTR_MASK); /* disable interrupts */
  472. if (stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET))
  473. complete(&dev->cmd_complete);
  474. return IRQ_HANDLED;
  475. }
  476. static struct i2c_algorithm i2c_dw_algo = {
  477. .master_xfer = i2c_dw_xfer,
  478. .functionality = i2c_dw_func,
  479. };
  480. static int __devinit dw_i2c_probe(struct platform_device *pdev)
  481. {
  482. struct dw_i2c_dev *dev;
  483. struct i2c_adapter *adap;
  484. struct resource *mem, *ioarea;
  485. int irq, r;
  486. /* NOTE: driver uses the static register mapping */
  487. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  488. if (!mem) {
  489. dev_err(&pdev->dev, "no mem resource?\n");
  490. return -EINVAL;
  491. }
  492. irq = platform_get_irq(pdev, 0);
  493. if (irq < 0) {
  494. dev_err(&pdev->dev, "no irq resource?\n");
  495. return irq; /* -ENXIO */
  496. }
  497. ioarea = request_mem_region(mem->start, resource_size(mem),
  498. pdev->name);
  499. if (!ioarea) {
  500. dev_err(&pdev->dev, "I2C region already claimed\n");
  501. return -EBUSY;
  502. }
  503. dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
  504. if (!dev) {
  505. r = -ENOMEM;
  506. goto err_release_region;
  507. }
  508. init_completion(&dev->cmd_complete);
  509. tasklet_init(&dev->pump_msg, dw_i2c_pump_msg, (unsigned long) dev);
  510. mutex_init(&dev->lock);
  511. dev->dev = get_device(&pdev->dev);
  512. dev->irq = irq;
  513. platform_set_drvdata(pdev, dev);
  514. dev->clk = clk_get(&pdev->dev, NULL);
  515. if (IS_ERR(dev->clk)) {
  516. r = -ENODEV;
  517. goto err_free_mem;
  518. }
  519. clk_enable(dev->clk);
  520. dev->base = ioremap(mem->start, resource_size(mem));
  521. if (dev->base == NULL) {
  522. dev_err(&pdev->dev, "failure mapping io resources\n");
  523. r = -EBUSY;
  524. goto err_unuse_clocks;
  525. }
  526. {
  527. u32 param1 = readl(dev->base + DW_IC_COMP_PARAM_1);
  528. dev->tx_fifo_depth = ((param1 >> 16) & 0xff) + 1;
  529. dev->rx_fifo_depth = ((param1 >> 8) & 0xff) + 1;
  530. }
  531. i2c_dw_init(dev);
  532. writel(0, dev->base + DW_IC_INTR_MASK); /* disable IRQ */
  533. r = request_irq(dev->irq, i2c_dw_isr, 0, pdev->name, dev);
  534. if (r) {
  535. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  536. goto err_iounmap;
  537. }
  538. adap = &dev->adapter;
  539. i2c_set_adapdata(adap, dev);
  540. adap->owner = THIS_MODULE;
  541. adap->class = I2C_CLASS_HWMON;
  542. strlcpy(adap->name, "Synopsys DesignWare I2C adapter",
  543. sizeof(adap->name));
  544. adap->algo = &i2c_dw_algo;
  545. adap->dev.parent = &pdev->dev;
  546. adap->nr = pdev->id;
  547. r = i2c_add_numbered_adapter(adap);
  548. if (r) {
  549. dev_err(&pdev->dev, "failure adding adapter\n");
  550. goto err_free_irq;
  551. }
  552. return 0;
  553. err_free_irq:
  554. free_irq(dev->irq, dev);
  555. err_iounmap:
  556. iounmap(dev->base);
  557. err_unuse_clocks:
  558. clk_disable(dev->clk);
  559. clk_put(dev->clk);
  560. dev->clk = NULL;
  561. err_free_mem:
  562. platform_set_drvdata(pdev, NULL);
  563. put_device(&pdev->dev);
  564. kfree(dev);
  565. err_release_region:
  566. release_mem_region(mem->start, resource_size(mem));
  567. return r;
  568. }
  569. static int __devexit dw_i2c_remove(struct platform_device *pdev)
  570. {
  571. struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
  572. struct resource *mem;
  573. platform_set_drvdata(pdev, NULL);
  574. i2c_del_adapter(&dev->adapter);
  575. put_device(&pdev->dev);
  576. clk_disable(dev->clk);
  577. clk_put(dev->clk);
  578. dev->clk = NULL;
  579. writel(0, dev->base + DW_IC_ENABLE);
  580. free_irq(dev->irq, dev);
  581. kfree(dev);
  582. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  583. release_mem_region(mem->start, resource_size(mem));
  584. return 0;
  585. }
  586. /* work with hotplug and coldplug */
  587. MODULE_ALIAS("platform:i2c_designware");
  588. static struct platform_driver dw_i2c_driver = {
  589. .remove = __devexit_p(dw_i2c_remove),
  590. .driver = {
  591. .name = "i2c_designware",
  592. .owner = THIS_MODULE,
  593. },
  594. };
  595. static int __init dw_i2c_init_driver(void)
  596. {
  597. return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe);
  598. }
  599. module_init(dw_i2c_init_driver);
  600. static void __exit dw_i2c_exit_driver(void)
  601. {
  602. platform_driver_unregister(&dw_i2c_driver);
  603. }
  604. module_exit(dw_i2c_exit_driver);
  605. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  606. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
  607. MODULE_LICENSE("GPL");