i2c-designware.c 18 KB

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