i2c-omap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * TI OMAP I2C master mode driver
  3. *
  4. * Copyright (C) 2003 MontaVista Software, Inc.
  5. * Copyright (C) 2004 Texas Instruments.
  6. *
  7. * Updated to work with multiple I2C interfaces on 24xx by
  8. * Tony Lindgren <tony@atomide.com> and Imre Deak <imre.deak@nokia.com>
  9. * Copyright (C) 2005 Nokia Corporation
  10. *
  11. * Cleaned up by Juha Yrjölä <juha.yrjola@nokia.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/delay.h>
  29. #include <linux/i2c.h>
  30. #include <linux/err.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/completion.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/clk.h>
  35. #include <asm/io.h>
  36. /* timeout waiting for the controller to respond */
  37. #define OMAP_I2C_TIMEOUT (msecs_to_jiffies(1000))
  38. #define OMAP_I2C_REV_REG 0x00
  39. #define OMAP_I2C_IE_REG 0x04
  40. #define OMAP_I2C_STAT_REG 0x08
  41. #define OMAP_I2C_IV_REG 0x0c
  42. #define OMAP_I2C_SYSS_REG 0x10
  43. #define OMAP_I2C_BUF_REG 0x14
  44. #define OMAP_I2C_CNT_REG 0x18
  45. #define OMAP_I2C_DATA_REG 0x1c
  46. #define OMAP_I2C_SYSC_REG 0x20
  47. #define OMAP_I2C_CON_REG 0x24
  48. #define OMAP_I2C_OA_REG 0x28
  49. #define OMAP_I2C_SA_REG 0x2c
  50. #define OMAP_I2C_PSC_REG 0x30
  51. #define OMAP_I2C_SCLL_REG 0x34
  52. #define OMAP_I2C_SCLH_REG 0x38
  53. #define OMAP_I2C_SYSTEST_REG 0x3c
  54. /* I2C Interrupt Enable Register (OMAP_I2C_IE): */
  55. #define OMAP_I2C_IE_XRDY (1 << 4) /* TX data ready int enable */
  56. #define OMAP_I2C_IE_RRDY (1 << 3) /* RX data ready int enable */
  57. #define OMAP_I2C_IE_ARDY (1 << 2) /* Access ready int enable */
  58. #define OMAP_I2C_IE_NACK (1 << 1) /* No ack interrupt enable */
  59. #define OMAP_I2C_IE_AL (1 << 0) /* Arbitration lost int ena */
  60. /* I2C Status Register (OMAP_I2C_STAT): */
  61. #define OMAP_I2C_STAT_SBD (1 << 15) /* Single byte data */
  62. #define OMAP_I2C_STAT_BB (1 << 12) /* Bus busy */
  63. #define OMAP_I2C_STAT_ROVR (1 << 11) /* Receive overrun */
  64. #define OMAP_I2C_STAT_XUDF (1 << 10) /* Transmit underflow */
  65. #define OMAP_I2C_STAT_AAS (1 << 9) /* Address as slave */
  66. #define OMAP_I2C_STAT_AD0 (1 << 8) /* Address zero */
  67. #define OMAP_I2C_STAT_XRDY (1 << 4) /* Transmit data ready */
  68. #define OMAP_I2C_STAT_RRDY (1 << 3) /* Receive data ready */
  69. #define OMAP_I2C_STAT_ARDY (1 << 2) /* Register access ready */
  70. #define OMAP_I2C_STAT_NACK (1 << 1) /* No ack interrupt enable */
  71. #define OMAP_I2C_STAT_AL (1 << 0) /* Arbitration lost int ena */
  72. /* I2C Buffer Configuration Register (OMAP_I2C_BUF): */
  73. #define OMAP_I2C_BUF_RDMA_EN (1 << 15) /* RX DMA channel enable */
  74. #define OMAP_I2C_BUF_XDMA_EN (1 << 7) /* TX DMA channel enable */
  75. /* I2C Configuration Register (OMAP_I2C_CON): */
  76. #define OMAP_I2C_CON_EN (1 << 15) /* I2C module enable */
  77. #define OMAP_I2C_CON_BE (1 << 14) /* Big endian mode */
  78. #define OMAP_I2C_CON_STB (1 << 11) /* Start byte mode (master) */
  79. #define OMAP_I2C_CON_MST (1 << 10) /* Master/slave mode */
  80. #define OMAP_I2C_CON_TRX (1 << 9) /* TX/RX mode (master only) */
  81. #define OMAP_I2C_CON_XA (1 << 8) /* Expand address */
  82. #define OMAP_I2C_CON_RM (1 << 2) /* Repeat mode (master only) */
  83. #define OMAP_I2C_CON_STP (1 << 1) /* Stop cond (master only) */
  84. #define OMAP_I2C_CON_STT (1 << 0) /* Start condition (master) */
  85. /* I2C System Test Register (OMAP_I2C_SYSTEST): */
  86. #ifdef DEBUG
  87. #define OMAP_I2C_SYSTEST_ST_EN (1 << 15) /* System test enable */
  88. #define OMAP_I2C_SYSTEST_FREE (1 << 14) /* Free running mode */
  89. #define OMAP_I2C_SYSTEST_TMODE_MASK (3 << 12) /* Test mode select */
  90. #define OMAP_I2C_SYSTEST_TMODE_SHIFT (12) /* Test mode select */
  91. #define OMAP_I2C_SYSTEST_SCL_I (1 << 3) /* SCL line sense in */
  92. #define OMAP_I2C_SYSTEST_SCL_O (1 << 2) /* SCL line drive out */
  93. #define OMAP_I2C_SYSTEST_SDA_I (1 << 1) /* SDA line sense in */
  94. #define OMAP_I2C_SYSTEST_SDA_O (1 << 0) /* SDA line drive out */
  95. #endif
  96. /* I2C System Status register (OMAP_I2C_SYSS): */
  97. #define OMAP_I2C_SYSS_RDONE (1 << 0) /* Reset Done */
  98. /* I2C System Configuration Register (OMAP_I2C_SYSC): */
  99. #define OMAP_I2C_SYSC_SRST (1 << 1) /* Soft Reset */
  100. /* REVISIT: Use platform_data instead of module parameters */
  101. /* Fast Mode = 400 kHz, Standard = 100 kHz */
  102. static int clock = 100; /* Default: 100 kHz */
  103. module_param(clock, int, 0);
  104. MODULE_PARM_DESC(clock, "Set I2C clock in kHz: 400=fast mode (default == 100)");
  105. struct omap_i2c_dev {
  106. struct device *dev;
  107. void __iomem *base; /* virtual */
  108. int irq;
  109. struct clk *iclk; /* Interface clock */
  110. struct clk *fclk; /* Functional clock */
  111. struct completion cmd_complete;
  112. struct resource *ioarea;
  113. u16 cmd_err;
  114. u8 *buf;
  115. size_t buf_len;
  116. struct i2c_adapter adapter;
  117. unsigned rev1:1;
  118. };
  119. static inline void omap_i2c_write_reg(struct omap_i2c_dev *i2c_dev,
  120. int reg, u16 val)
  121. {
  122. __raw_writew(val, i2c_dev->base + reg);
  123. }
  124. static inline u16 omap_i2c_read_reg(struct omap_i2c_dev *i2c_dev, int reg)
  125. {
  126. return __raw_readw(i2c_dev->base + reg);
  127. }
  128. static int omap_i2c_get_clocks(struct omap_i2c_dev *dev)
  129. {
  130. if (cpu_is_omap16xx() || cpu_is_omap24xx()) {
  131. dev->iclk = clk_get(dev->dev, "i2c_ick");
  132. if (IS_ERR(dev->iclk)) {
  133. dev->iclk = NULL;
  134. return -ENODEV;
  135. }
  136. }
  137. dev->fclk = clk_get(dev->dev, "i2c_fck");
  138. if (IS_ERR(dev->fclk)) {
  139. if (dev->iclk != NULL) {
  140. clk_put(dev->iclk);
  141. dev->iclk = NULL;
  142. }
  143. dev->fclk = NULL;
  144. return -ENODEV;
  145. }
  146. return 0;
  147. }
  148. static void omap_i2c_put_clocks(struct omap_i2c_dev *dev)
  149. {
  150. clk_put(dev->fclk);
  151. dev->fclk = NULL;
  152. if (dev->iclk != NULL) {
  153. clk_put(dev->iclk);
  154. dev->iclk = NULL;
  155. }
  156. }
  157. static void omap_i2c_enable_clocks(struct omap_i2c_dev *dev)
  158. {
  159. if (dev->iclk != NULL)
  160. clk_enable(dev->iclk);
  161. clk_enable(dev->fclk);
  162. }
  163. static void omap_i2c_disable_clocks(struct omap_i2c_dev *dev)
  164. {
  165. if (dev->iclk != NULL)
  166. clk_disable(dev->iclk);
  167. clk_disable(dev->fclk);
  168. }
  169. static int omap_i2c_init(struct omap_i2c_dev *dev)
  170. {
  171. u16 psc = 0;
  172. unsigned long fclk_rate = 12000000;
  173. unsigned long timeout;
  174. if (!dev->rev1) {
  175. omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, OMAP_I2C_SYSC_SRST);
  176. /* For some reason we need to set the EN bit before the
  177. * reset done bit gets set. */
  178. timeout = jiffies + OMAP_I2C_TIMEOUT;
  179. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
  180. while (!(omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG) &
  181. OMAP_I2C_SYSS_RDONE)) {
  182. if (time_after(jiffies, timeout)) {
  183. dev_warn(dev->dev, "timeout waiting"
  184. "for controller reset\n");
  185. return -ETIMEDOUT;
  186. }
  187. msleep(1);
  188. }
  189. }
  190. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  191. if (cpu_class_is_omap1()) {
  192. struct clk *armxor_ck;
  193. armxor_ck = clk_get(NULL, "armxor_ck");
  194. if (IS_ERR(armxor_ck))
  195. dev_warn(dev->dev, "Could not get armxor_ck\n");
  196. else {
  197. fclk_rate = clk_get_rate(armxor_ck);
  198. clk_put(armxor_ck);
  199. }
  200. /* TRM for 5912 says the I2C clock must be prescaled to be
  201. * between 7 - 12 MHz. The XOR input clock is typically
  202. * 12, 13 or 19.2 MHz. So we should have code that produces:
  203. *
  204. * XOR MHz Divider Prescaler
  205. * 12 1 0
  206. * 13 2 1
  207. * 19.2 2 1
  208. */
  209. if (fclk_rate > 12000000)
  210. psc = fclk_rate / 12000000;
  211. }
  212. /* Setup clock prescaler to obtain approx 12MHz I2C module clock: */
  213. omap_i2c_write_reg(dev, OMAP_I2C_PSC_REG, psc);
  214. /* Program desired operating rate */
  215. fclk_rate /= (psc + 1) * 1000;
  216. if (psc > 2)
  217. psc = 2;
  218. omap_i2c_write_reg(dev, OMAP_I2C_SCLL_REG,
  219. fclk_rate / (clock * 2) - 7 + psc);
  220. omap_i2c_write_reg(dev, OMAP_I2C_SCLH_REG,
  221. fclk_rate / (clock * 2) - 7 + psc);
  222. /* Take the I2C module out of reset: */
  223. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
  224. /* Enable interrupts */
  225. omap_i2c_write_reg(dev, OMAP_I2C_IE_REG,
  226. (OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY |
  227. OMAP_I2C_IE_ARDY | OMAP_I2C_IE_NACK |
  228. OMAP_I2C_IE_AL));
  229. return 0;
  230. }
  231. /*
  232. * Waiting on Bus Busy
  233. */
  234. static int omap_i2c_wait_for_bb(struct omap_i2c_dev *dev)
  235. {
  236. unsigned long timeout;
  237. timeout = jiffies + OMAP_I2C_TIMEOUT;
  238. while (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG) & OMAP_I2C_STAT_BB) {
  239. if (time_after(jiffies, timeout)) {
  240. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  241. return -ETIMEDOUT;
  242. }
  243. msleep(1);
  244. }
  245. return 0;
  246. }
  247. /*
  248. * Low level master read/write transaction.
  249. */
  250. static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
  251. struct i2c_msg *msg, int stop)
  252. {
  253. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  254. int r;
  255. u16 w;
  256. dev_dbg(dev->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
  257. msg->addr, msg->len, msg->flags, stop);
  258. if (msg->len == 0)
  259. return -EINVAL;
  260. omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);
  261. /* REVISIT: Could the STB bit of I2C_CON be used with probing? */
  262. dev->buf = msg->buf;
  263. dev->buf_len = msg->len;
  264. omap_i2c_write_reg(dev, OMAP_I2C_CNT_REG, dev->buf_len);
  265. init_completion(&dev->cmd_complete);
  266. dev->cmd_err = 0;
  267. w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;
  268. if (msg->flags & I2C_M_TEN)
  269. w |= OMAP_I2C_CON_XA;
  270. if (!(msg->flags & I2C_M_RD))
  271. w |= OMAP_I2C_CON_TRX;
  272. if (stop)
  273. w |= OMAP_I2C_CON_STP;
  274. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
  275. r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
  276. OMAP_I2C_TIMEOUT);
  277. dev->buf_len = 0;
  278. if (r < 0)
  279. return r;
  280. if (r == 0) {
  281. dev_err(dev->dev, "controller timed out\n");
  282. omap_i2c_init(dev);
  283. return -ETIMEDOUT;
  284. }
  285. if (likely(!dev->cmd_err))
  286. return 0;
  287. /* We have an error */
  288. if (dev->cmd_err & (OMAP_I2C_STAT_AL | OMAP_I2C_STAT_ROVR |
  289. OMAP_I2C_STAT_XUDF)) {
  290. omap_i2c_init(dev);
  291. return -EIO;
  292. }
  293. if (dev->cmd_err & OMAP_I2C_STAT_NACK) {
  294. if (msg->flags & I2C_M_IGNORE_NAK)
  295. return 0;
  296. if (stop) {
  297. w = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
  298. w |= OMAP_I2C_CON_STP;
  299. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
  300. }
  301. return -EREMOTEIO;
  302. }
  303. return -EIO;
  304. }
  305. /*
  306. * Prepare controller for a transaction and call omap_i2c_xfer_msg
  307. * to do the work during IRQ processing.
  308. */
  309. static int
  310. omap_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  311. {
  312. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  313. int i;
  314. int r;
  315. omap_i2c_enable_clocks(dev);
  316. /* REVISIT: initialize and use adap->retries. This is an optional
  317. * feature */
  318. if ((r = omap_i2c_wait_for_bb(dev)) < 0)
  319. goto out;
  320. for (i = 0; i < num; i++) {
  321. r = omap_i2c_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  322. if (r != 0)
  323. break;
  324. }
  325. if (r == 0)
  326. r = num;
  327. out:
  328. omap_i2c_disable_clocks(dev);
  329. return r;
  330. }
  331. static u32
  332. omap_i2c_func(struct i2c_adapter *adap)
  333. {
  334. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  335. }
  336. static inline void
  337. omap_i2c_complete_cmd(struct omap_i2c_dev *dev, u16 err)
  338. {
  339. dev->cmd_err |= err;
  340. complete(&dev->cmd_complete);
  341. }
  342. static inline void
  343. omap_i2c_ack_stat(struct omap_i2c_dev *dev, u16 stat)
  344. {
  345. omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, stat);
  346. }
  347. static irqreturn_t
  348. omap_i2c_rev1_isr(int this_irq, void *dev_id)
  349. {
  350. struct omap_i2c_dev *dev = dev_id;
  351. u16 iv, w;
  352. iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG);
  353. switch (iv) {
  354. case 0x00: /* None */
  355. break;
  356. case 0x01: /* Arbitration lost */
  357. dev_err(dev->dev, "Arbitration lost\n");
  358. omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_AL);
  359. break;
  360. case 0x02: /* No acknowledgement */
  361. omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_NACK);
  362. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_STP);
  363. break;
  364. case 0x03: /* Register access ready */
  365. omap_i2c_complete_cmd(dev, 0);
  366. break;
  367. case 0x04: /* Receive data ready */
  368. if (dev->buf_len) {
  369. w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
  370. *dev->buf++ = w;
  371. dev->buf_len--;
  372. if (dev->buf_len) {
  373. *dev->buf++ = w >> 8;
  374. dev->buf_len--;
  375. }
  376. } else
  377. dev_err(dev->dev, "RRDY IRQ while no data requested\n");
  378. break;
  379. case 0x05: /* Transmit data ready */
  380. if (dev->buf_len) {
  381. w = *dev->buf++;
  382. dev->buf_len--;
  383. if (dev->buf_len) {
  384. w |= *dev->buf++ << 8;
  385. dev->buf_len--;
  386. }
  387. omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
  388. } else
  389. dev_err(dev->dev, "XRDY IRQ while no data to send\n");
  390. break;
  391. default:
  392. return IRQ_NONE;
  393. }
  394. return IRQ_HANDLED;
  395. }
  396. static irqreturn_t
  397. omap_i2c_isr(int this_irq, void *dev_id)
  398. {
  399. struct omap_i2c_dev *dev = dev_id;
  400. u16 bits;
  401. u16 stat, w;
  402. int count = 0;
  403. bits = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  404. while ((stat = (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG))) & bits) {
  405. dev_dbg(dev->dev, "IRQ (ISR = 0x%04x)\n", stat);
  406. if (count++ == 100) {
  407. dev_warn(dev->dev, "Too much work in one IRQ\n");
  408. break;
  409. }
  410. omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, stat);
  411. if (stat & OMAP_I2C_STAT_ARDY) {
  412. omap_i2c_complete_cmd(dev, 0);
  413. continue;
  414. }
  415. if (stat & OMAP_I2C_STAT_RRDY) {
  416. w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
  417. if (dev->buf_len) {
  418. *dev->buf++ = w;
  419. dev->buf_len--;
  420. if (dev->buf_len) {
  421. *dev->buf++ = w >> 8;
  422. dev->buf_len--;
  423. }
  424. } else
  425. dev_err(dev->dev, "RRDY IRQ while no data"
  426. "requested\n");
  427. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RRDY);
  428. continue;
  429. }
  430. if (stat & OMAP_I2C_STAT_XRDY) {
  431. w = 0;
  432. if (dev->buf_len) {
  433. w = *dev->buf++;
  434. dev->buf_len--;
  435. if (dev->buf_len) {
  436. w |= *dev->buf++ << 8;
  437. dev->buf_len--;
  438. }
  439. } else
  440. dev_err(dev->dev, "XRDY IRQ while no"
  441. "data to send\n");
  442. omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
  443. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_XRDY);
  444. continue;
  445. }
  446. if (stat & OMAP_I2C_STAT_ROVR) {
  447. dev_err(dev->dev, "Receive overrun\n");
  448. dev->cmd_err |= OMAP_I2C_STAT_ROVR;
  449. }
  450. if (stat & OMAP_I2C_STAT_XUDF) {
  451. dev_err(dev->dev, "Transmit overflow\n");
  452. dev->cmd_err |= OMAP_I2C_STAT_XUDF;
  453. }
  454. if (stat & OMAP_I2C_STAT_NACK) {
  455. omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_NACK);
  456. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG,
  457. OMAP_I2C_CON_STP);
  458. }
  459. if (stat & OMAP_I2C_STAT_AL) {
  460. dev_err(dev->dev, "Arbitration lost\n");
  461. omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_AL);
  462. }
  463. }
  464. return count ? IRQ_HANDLED : IRQ_NONE;
  465. }
  466. static const struct i2c_algorithm omap_i2c_algo = {
  467. .master_xfer = omap_i2c_xfer,
  468. .functionality = omap_i2c_func,
  469. };
  470. static int
  471. omap_i2c_probe(struct platform_device *pdev)
  472. {
  473. struct omap_i2c_dev *dev;
  474. struct i2c_adapter *adap;
  475. struct resource *mem, *irq, *ioarea;
  476. int r;
  477. /* NOTE: driver uses the static register mapping */
  478. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  479. if (!mem) {
  480. dev_err(&pdev->dev, "no mem resource?\n");
  481. return -ENODEV;
  482. }
  483. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  484. if (!irq) {
  485. dev_err(&pdev->dev, "no irq resource?\n");
  486. return -ENODEV;
  487. }
  488. ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1,
  489. pdev->name);
  490. if (!ioarea) {
  491. dev_err(&pdev->dev, "I2C region already claimed\n");
  492. return -EBUSY;
  493. }
  494. if (clock > 200)
  495. clock = 400; /* Fast mode */
  496. else
  497. clock = 100; /* Standard mode */
  498. dev = kzalloc(sizeof(struct omap_i2c_dev), GFP_KERNEL);
  499. if (!dev) {
  500. r = -ENOMEM;
  501. goto err_release_region;
  502. }
  503. dev->dev = &pdev->dev;
  504. dev->irq = irq->start;
  505. dev->base = (void __iomem *) IO_ADDRESS(mem->start);
  506. platform_set_drvdata(pdev, dev);
  507. if ((r = omap_i2c_get_clocks(dev)) != 0)
  508. goto err_free_mem;
  509. omap_i2c_enable_clocks(dev);
  510. if (cpu_is_omap15xx())
  511. dev->rev1 = omap_i2c_read_reg(dev, OMAP_I2C_REV_REG) < 0x20;
  512. /* reset ASAP, clearing any IRQs */
  513. omap_i2c_init(dev);
  514. r = request_irq(dev->irq, dev->rev1 ? omap_i2c_rev1_isr : omap_i2c_isr,
  515. 0, pdev->name, dev);
  516. if (r) {
  517. dev_err(dev->dev, "failure requesting irq %i\n", dev->irq);
  518. goto err_unuse_clocks;
  519. }
  520. r = omap_i2c_read_reg(dev, OMAP_I2C_REV_REG) & 0xff;
  521. dev_info(dev->dev, "bus %d rev%d.%d at %d kHz\n",
  522. pdev->id, r >> 4, r & 0xf, clock);
  523. adap = &dev->adapter;
  524. i2c_set_adapdata(adap, dev);
  525. adap->owner = THIS_MODULE;
  526. adap->class = I2C_CLASS_HWMON;
  527. strncpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
  528. adap->algo = &omap_i2c_algo;
  529. adap->dev.parent = &pdev->dev;
  530. /* i2c device drivers may be active on return from add_adapter() */
  531. r = i2c_add_adapter(adap);
  532. if (r) {
  533. dev_err(dev->dev, "failure adding adapter\n");
  534. goto err_free_irq;
  535. }
  536. omap_i2c_disable_clocks(dev);
  537. return 0;
  538. err_free_irq:
  539. free_irq(dev->irq, dev);
  540. err_unuse_clocks:
  541. omap_i2c_disable_clocks(dev);
  542. omap_i2c_put_clocks(dev);
  543. err_free_mem:
  544. platform_set_drvdata(pdev, NULL);
  545. kfree(dev);
  546. err_release_region:
  547. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  548. release_mem_region(mem->start, (mem->end - mem->start) + 1);
  549. return r;
  550. }
  551. static int
  552. omap_i2c_remove(struct platform_device *pdev)
  553. {
  554. struct omap_i2c_dev *dev = platform_get_drvdata(pdev);
  555. struct resource *mem;
  556. platform_set_drvdata(pdev, NULL);
  557. free_irq(dev->irq, dev);
  558. i2c_del_adapter(&dev->adapter);
  559. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  560. omap_i2c_put_clocks(dev);
  561. kfree(dev);
  562. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  563. release_mem_region(mem->start, (mem->end - mem->start) + 1);
  564. return 0;
  565. }
  566. static struct platform_driver omap_i2c_driver = {
  567. .probe = omap_i2c_probe,
  568. .remove = omap_i2c_remove,
  569. .driver = {
  570. .name = "i2c_omap",
  571. .owner = THIS_MODULE,
  572. },
  573. };
  574. /* I2C may be needed to bring up other drivers */
  575. static int __init
  576. omap_i2c_init_driver(void)
  577. {
  578. return platform_driver_register(&omap_i2c_driver);
  579. }
  580. subsys_initcall(omap_i2c_init_driver);
  581. static void __exit omap_i2c_exit_driver(void)
  582. {
  583. platform_driver_unregister(&omap_i2c_driver);
  584. }
  585. module_exit(omap_i2c_exit_driver);
  586. MODULE_AUTHOR("MontaVista Software, Inc. (and others)");
  587. MODULE_DESCRIPTION("TI OMAP I2C bus adapter");
  588. MODULE_LICENSE("GPL");