i2c-nomadik.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /*
  2. * Copyright (C) 2009 ST-Ericsson SA
  3. * Copyright (C) 2009 STMicroelectronics
  4. *
  5. * I2C master mode controller driver, used in Nomadik 8815
  6. * and Ux500 platforms.
  7. *
  8. * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
  9. * Author: Sachin Verma <sachin.verma@st.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2, as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/i2c.h>
  21. #include <linux/err.h>
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/pm_runtime.h>
  26. #include <plat/i2c.h>
  27. #define DRIVER_NAME "nmk-i2c"
  28. /* I2C Controller register offsets */
  29. #define I2C_CR (0x000)
  30. #define I2C_SCR (0x004)
  31. #define I2C_HSMCR (0x008)
  32. #define I2C_MCR (0x00C)
  33. #define I2C_TFR (0x010)
  34. #define I2C_SR (0x014)
  35. #define I2C_RFR (0x018)
  36. #define I2C_TFTR (0x01C)
  37. #define I2C_RFTR (0x020)
  38. #define I2C_DMAR (0x024)
  39. #define I2C_BRCR (0x028)
  40. #define I2C_IMSCR (0x02C)
  41. #define I2C_RISR (0x030)
  42. #define I2C_MISR (0x034)
  43. #define I2C_ICR (0x038)
  44. /* Control registers */
  45. #define I2C_CR_PE (0x1 << 0) /* Peripheral Enable */
  46. #define I2C_CR_OM (0x3 << 1) /* Operating mode */
  47. #define I2C_CR_SAM (0x1 << 3) /* Slave addressing mode */
  48. #define I2C_CR_SM (0x3 << 4) /* Speed mode */
  49. #define I2C_CR_SGCM (0x1 << 6) /* Slave general call mode */
  50. #define I2C_CR_FTX (0x1 << 7) /* Flush Transmit */
  51. #define I2C_CR_FRX (0x1 << 8) /* Flush Receive */
  52. #define I2C_CR_DMA_TX_EN (0x1 << 9) /* DMA Tx enable */
  53. #define I2C_CR_DMA_RX_EN (0x1 << 10) /* DMA Rx Enable */
  54. #define I2C_CR_DMA_SLE (0x1 << 11) /* DMA sync. logic enable */
  55. #define I2C_CR_LM (0x1 << 12) /* Loopback mode */
  56. #define I2C_CR_FON (0x3 << 13) /* Filtering on */
  57. #define I2C_CR_FS (0x3 << 15) /* Force stop enable */
  58. /* Master controller (MCR) register */
  59. #define I2C_MCR_OP (0x1 << 0) /* Operation */
  60. #define I2C_MCR_A7 (0x7f << 1) /* 7-bit address */
  61. #define I2C_MCR_EA10 (0x7 << 8) /* 10-bit Extended address */
  62. #define I2C_MCR_SB (0x1 << 11) /* Extended address */
  63. #define I2C_MCR_AM (0x3 << 12) /* Address type */
  64. #define I2C_MCR_STOP (0x1 << 14) /* Stop condition */
  65. #define I2C_MCR_LENGTH (0x7ff << 15) /* Transaction length */
  66. /* Status register (SR) */
  67. #define I2C_SR_OP (0x3 << 0) /* Operation */
  68. #define I2C_SR_STATUS (0x3 << 2) /* controller status */
  69. #define I2C_SR_CAUSE (0x7 << 4) /* Abort cause */
  70. #define I2C_SR_TYPE (0x3 << 7) /* Receive type */
  71. #define I2C_SR_LENGTH (0x7ff << 9) /* Transfer length */
  72. /* Interrupt mask set/clear (IMSCR) bits */
  73. #define I2C_IT_TXFE (0x1 << 0)
  74. #define I2C_IT_TXFNE (0x1 << 1)
  75. #define I2C_IT_TXFF (0x1 << 2)
  76. #define I2C_IT_TXFOVR (0x1 << 3)
  77. #define I2C_IT_RXFE (0x1 << 4)
  78. #define I2C_IT_RXFNF (0x1 << 5)
  79. #define I2C_IT_RXFF (0x1 << 6)
  80. #define I2C_IT_RFSR (0x1 << 16)
  81. #define I2C_IT_RFSE (0x1 << 17)
  82. #define I2C_IT_WTSR (0x1 << 18)
  83. #define I2C_IT_MTD (0x1 << 19)
  84. #define I2C_IT_STD (0x1 << 20)
  85. #define I2C_IT_MAL (0x1 << 24)
  86. #define I2C_IT_BERR (0x1 << 25)
  87. #define I2C_IT_MTDWS (0x1 << 28)
  88. #define GEN_MASK(val, mask, sb) (((val) << (sb)) & (mask))
  89. /* some bits in ICR are reserved */
  90. #define I2C_CLEAR_ALL_INTS 0x131f007f
  91. /* first three msb bits are reserved */
  92. #define IRQ_MASK(mask) (mask & 0x1fffffff)
  93. /* maximum threshold value */
  94. #define MAX_I2C_FIFO_THRESHOLD 15
  95. enum i2c_status {
  96. I2C_NOP,
  97. I2C_ON_GOING,
  98. I2C_OK,
  99. I2C_ABORT
  100. };
  101. /* operation */
  102. enum i2c_operation {
  103. I2C_NO_OPERATION = 0xff,
  104. I2C_WRITE = 0x00,
  105. I2C_READ = 0x01
  106. };
  107. /**
  108. * struct i2c_nmk_client - client specific data
  109. * @slave_adr: 7-bit slave address
  110. * @count: no. bytes to be transferred
  111. * @buffer: client data buffer
  112. * @xfer_bytes: bytes transferred till now
  113. * @operation: current I2C operation
  114. */
  115. struct i2c_nmk_client {
  116. unsigned short slave_adr;
  117. unsigned long count;
  118. unsigned char *buffer;
  119. unsigned long xfer_bytes;
  120. enum i2c_operation operation;
  121. };
  122. /**
  123. * struct nmk_i2c_dev - private data structure of the controller
  124. * @pdev: parent platform device
  125. * @adap: corresponding I2C adapter
  126. * @irq: interrupt line for the controller
  127. * @virtbase: virtual io memory area
  128. * @clk: hardware i2c block clock
  129. * @cfg: machine provided controller configuration
  130. * @cli: holder of client specific data
  131. * @stop: stop condition
  132. * @xfer_complete: acknowledge completion for a I2C message
  133. * @result: controller propogated result
  134. * @regulator: pointer to i2c regulator
  135. * @busy: Busy doing transfer
  136. */
  137. struct nmk_i2c_dev {
  138. struct platform_device *pdev;
  139. struct i2c_adapter adap;
  140. int irq;
  141. void __iomem *virtbase;
  142. struct clk *clk;
  143. struct nmk_i2c_controller cfg;
  144. struct i2c_nmk_client cli;
  145. int stop;
  146. struct completion xfer_complete;
  147. int result;
  148. struct regulator *regulator;
  149. bool busy;
  150. };
  151. /* controller's abort causes */
  152. static const char *abort_causes[] = {
  153. "no ack received after address transmission",
  154. "no ack received during data phase",
  155. "ack received after xmission of master code",
  156. "master lost arbitration",
  157. "slave restarts",
  158. "slave reset",
  159. "overflow, maxsize is 2047 bytes",
  160. };
  161. static inline void i2c_set_bit(void __iomem *reg, u32 mask)
  162. {
  163. writel(readl(reg) | mask, reg);
  164. }
  165. static inline void i2c_clr_bit(void __iomem *reg, u32 mask)
  166. {
  167. writel(readl(reg) & ~mask, reg);
  168. }
  169. /**
  170. * flush_i2c_fifo() - This function flushes the I2C FIFO
  171. * @dev: private data of I2C Driver
  172. *
  173. * This function flushes the I2C Tx and Rx FIFOs. It returns
  174. * 0 on successful flushing of FIFO
  175. */
  176. static int flush_i2c_fifo(struct nmk_i2c_dev *dev)
  177. {
  178. #define LOOP_ATTEMPTS 10
  179. int i;
  180. unsigned long timeout;
  181. /*
  182. * flush the transmit and receive FIFO. The flushing
  183. * operation takes several cycles before to be completed.
  184. * On the completion, the I2C internal logic clears these
  185. * bits, until then no one must access Tx, Rx FIFO and
  186. * should poll on these bits waiting for the completion.
  187. */
  188. writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR);
  189. for (i = 0; i < LOOP_ATTEMPTS; i++) {
  190. timeout = jiffies + dev->adap.timeout;
  191. while (!time_after(jiffies, timeout)) {
  192. if ((readl(dev->virtbase + I2C_CR) &
  193. (I2C_CR_FTX | I2C_CR_FRX)) == 0)
  194. return 0;
  195. }
  196. }
  197. dev_err(&dev->pdev->dev, "flushing operation timed out "
  198. "giving up after %d attempts", LOOP_ATTEMPTS);
  199. return -ETIMEDOUT;
  200. }
  201. /**
  202. * disable_all_interrupts() - Disable all interrupts of this I2c Bus
  203. * @dev: private data of I2C Driver
  204. */
  205. static void disable_all_interrupts(struct nmk_i2c_dev *dev)
  206. {
  207. u32 mask = IRQ_MASK(0);
  208. writel(mask, dev->virtbase + I2C_IMSCR);
  209. }
  210. /**
  211. * clear_all_interrupts() - Clear all interrupts of I2C Controller
  212. * @dev: private data of I2C Driver
  213. */
  214. static void clear_all_interrupts(struct nmk_i2c_dev *dev)
  215. {
  216. u32 mask;
  217. mask = IRQ_MASK(I2C_CLEAR_ALL_INTS);
  218. writel(mask, dev->virtbase + I2C_ICR);
  219. }
  220. /**
  221. * init_hw() - initialize the I2C hardware
  222. * @dev: private data of I2C Driver
  223. */
  224. static int init_hw(struct nmk_i2c_dev *dev)
  225. {
  226. int stat;
  227. stat = flush_i2c_fifo(dev);
  228. if (stat)
  229. goto exit;
  230. /* disable the controller */
  231. i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
  232. disable_all_interrupts(dev);
  233. clear_all_interrupts(dev);
  234. dev->cli.operation = I2C_NO_OPERATION;
  235. exit:
  236. return stat;
  237. }
  238. /* enable peripheral, master mode operation */
  239. #define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE)
  240. /**
  241. * load_i2c_mcr_reg() - load the MCR register
  242. * @dev: private data of controller
  243. */
  244. static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev)
  245. {
  246. u32 mcr = 0;
  247. /* 7-bit address transaction */
  248. mcr |= GEN_MASK(1, I2C_MCR_AM, 12);
  249. mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);
  250. /* start byte procedure not applied */
  251. mcr |= GEN_MASK(0, I2C_MCR_SB, 11);
  252. /* check the operation, master read/write? */
  253. if (dev->cli.operation == I2C_WRITE)
  254. mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);
  255. else
  256. mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);
  257. /* stop or repeated start? */
  258. if (dev->stop)
  259. mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);
  260. else
  261. mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));
  262. mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);
  263. return mcr;
  264. }
  265. /**
  266. * setup_i2c_controller() - setup the controller
  267. * @dev: private data of controller
  268. */
  269. static void setup_i2c_controller(struct nmk_i2c_dev *dev)
  270. {
  271. u32 brcr1, brcr2;
  272. u32 i2c_clk, div;
  273. writel(0x0, dev->virtbase + I2C_CR);
  274. writel(0x0, dev->virtbase + I2C_HSMCR);
  275. writel(0x0, dev->virtbase + I2C_TFTR);
  276. writel(0x0, dev->virtbase + I2C_RFTR);
  277. writel(0x0, dev->virtbase + I2C_DMAR);
  278. /*
  279. * set the slsu:
  280. *
  281. * slsu defines the data setup time after SCL clock
  282. * stretching in terms of i2c clk cycles. The
  283. * needed setup time for the three modes are 250ns,
  284. * 100ns, 10ns respectively thus leading to the values
  285. * of 14, 6, 2 for a 48 MHz i2c clk.
  286. */
  287. writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR);
  288. i2c_clk = clk_get_rate(dev->clk);
  289. /* fallback to std. mode if machine has not provided it */
  290. if (dev->cfg.clk_freq == 0)
  291. dev->cfg.clk_freq = 100000;
  292. /*
  293. * The spec says, in case of std. mode the divider is
  294. * 2 whereas it is 3 for fast and fastplus mode of
  295. * operation. TODO - high speed support.
  296. */
  297. div = (dev->cfg.clk_freq > 100000) ? 3 : 2;
  298. /*
  299. * generate the mask for baud rate counters. The controller
  300. * has two baud rate counters. One is used for High speed
  301. * operation, and the other is for std, fast mode, fast mode
  302. * plus operation. Currently we do not supprt high speed mode
  303. * so set brcr1 to 0.
  304. */
  305. brcr1 = 0 << 16;
  306. brcr2 = (i2c_clk/(dev->cfg.clk_freq * div)) & 0xffff;
  307. /* set the baud rate counter register */
  308. writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
  309. /*
  310. * set the speed mode. Currently we support
  311. * only standard and fast mode of operation
  312. * TODO - support for fast mode plus (up to 1Mb/s)
  313. * and high speed (up to 3.4 Mb/s)
  314. */
  315. if (dev->cfg.sm > I2C_FREQ_MODE_FAST) {
  316. dev_err(&dev->pdev->dev, "do not support this mode "
  317. "defaulting to std. mode\n");
  318. brcr2 = i2c_clk/(100000 * 2) & 0xffff;
  319. writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
  320. writel(I2C_FREQ_MODE_STANDARD << 4,
  321. dev->virtbase + I2C_CR);
  322. }
  323. writel(dev->cfg.sm << 4, dev->virtbase + I2C_CR);
  324. /* set the Tx and Rx FIFO threshold */
  325. writel(dev->cfg.tft, dev->virtbase + I2C_TFTR);
  326. writel(dev->cfg.rft, dev->virtbase + I2C_RFTR);
  327. }
  328. /**
  329. * read_i2c() - Read from I2C client device
  330. * @dev: private data of I2C Driver
  331. *
  332. * This function reads from i2c client device when controller is in
  333. * master mode. There is a completion timeout. If there is no transfer
  334. * before timeout error is returned.
  335. */
  336. static int read_i2c(struct nmk_i2c_dev *dev)
  337. {
  338. u32 status = 0;
  339. u32 mcr;
  340. u32 irq_mask = 0;
  341. int timeout;
  342. mcr = load_i2c_mcr_reg(dev);
  343. writel(mcr, dev->virtbase + I2C_MCR);
  344. /* load the current CR value */
  345. writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
  346. dev->virtbase + I2C_CR);
  347. /* enable the controller */
  348. i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  349. init_completion(&dev->xfer_complete);
  350. /* enable interrupts by setting the mask */
  351. irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
  352. I2C_IT_MAL | I2C_IT_BERR);
  353. if (dev->stop)
  354. irq_mask |= I2C_IT_MTD;
  355. else
  356. irq_mask |= I2C_IT_MTDWS;
  357. irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
  358. writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
  359. dev->virtbase + I2C_IMSCR);
  360. timeout = wait_for_completion_timeout(
  361. &dev->xfer_complete, dev->adap.timeout);
  362. if (timeout < 0) {
  363. dev_err(&dev->pdev->dev,
  364. "wait_for_completion_timeout"
  365. "returned %d waiting for event\n", timeout);
  366. status = timeout;
  367. }
  368. if (timeout == 0) {
  369. /* Controller timed out */
  370. dev_err(&dev->pdev->dev, "read from slave 0x%x timed out\n",
  371. dev->cli.slave_adr);
  372. status = -ETIMEDOUT;
  373. }
  374. return status;
  375. }
  376. static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes)
  377. {
  378. int count;
  379. for (count = (no_bytes - 2);
  380. (count > 0) &&
  381. (dev->cli.count != 0);
  382. count--) {
  383. /* write to the Tx FIFO */
  384. writeb(*dev->cli.buffer,
  385. dev->virtbase + I2C_TFR);
  386. dev->cli.buffer++;
  387. dev->cli.count--;
  388. dev->cli.xfer_bytes++;
  389. }
  390. }
  391. /**
  392. * write_i2c() - Write data to I2C client.
  393. * @dev: private data of I2C Driver
  394. *
  395. * This function writes data to I2C client
  396. */
  397. static int write_i2c(struct nmk_i2c_dev *dev)
  398. {
  399. u32 status = 0;
  400. u32 mcr;
  401. u32 irq_mask = 0;
  402. int timeout;
  403. mcr = load_i2c_mcr_reg(dev);
  404. writel(mcr, dev->virtbase + I2C_MCR);
  405. /* load the current CR value */
  406. writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
  407. dev->virtbase + I2C_CR);
  408. /* enable the controller */
  409. i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
  410. init_completion(&dev->xfer_complete);
  411. /* enable interrupts by settings the masks */
  412. irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR);
  413. /* Fill the TX FIFO with transmit data */
  414. fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD);
  415. if (dev->cli.count != 0)
  416. irq_mask |= I2C_IT_TXFNE;
  417. /*
  418. * check if we want to transfer a single or multiple bytes, if so
  419. * set the MTDWS bit (Master Transaction Done Without Stop)
  420. * to start repeated start operation
  421. */
  422. if (dev->stop)
  423. irq_mask |= I2C_IT_MTD;
  424. else
  425. irq_mask |= I2C_IT_MTDWS;
  426. irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
  427. writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
  428. dev->virtbase + I2C_IMSCR);
  429. timeout = wait_for_completion_timeout(
  430. &dev->xfer_complete, dev->adap.timeout);
  431. if (timeout < 0) {
  432. dev_err(&dev->pdev->dev,
  433. "wait_for_completion_timeout "
  434. "returned %d waiting for event\n", timeout);
  435. status = timeout;
  436. }
  437. if (timeout == 0) {
  438. /* Controller timed out */
  439. dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n",
  440. dev->cli.slave_adr);
  441. status = -ETIMEDOUT;
  442. }
  443. return status;
  444. }
  445. /**
  446. * nmk_i2c_xfer_one() - transmit a single I2C message
  447. * @dev: device with a message encoded into it
  448. * @flags: message flags
  449. */
  450. static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags)
  451. {
  452. int status;
  453. if (flags & I2C_M_RD) {
  454. /* read operation */
  455. dev->cli.operation = I2C_READ;
  456. status = read_i2c(dev);
  457. } else {
  458. /* write operation */
  459. dev->cli.operation = I2C_WRITE;
  460. status = write_i2c(dev);
  461. }
  462. if (status || (dev->result)) {
  463. u32 i2c_sr;
  464. u32 cause;
  465. i2c_sr = readl(dev->virtbase + I2C_SR);
  466. /*
  467. * Check if the controller I2C operation status
  468. * is set to ABORT(11b).
  469. */
  470. if (((i2c_sr >> 2) & 0x3) == 0x3) {
  471. /* get the abort cause */
  472. cause = (i2c_sr >> 4) & 0x7;
  473. dev_err(&dev->pdev->dev, "%s\n", cause
  474. >= ARRAY_SIZE(abort_causes) ?
  475. "unknown reason" :
  476. abort_causes[cause]);
  477. }
  478. (void) init_hw(dev);
  479. status = status ? status : dev->result;
  480. }
  481. return status;
  482. }
  483. /**
  484. * nmk_i2c_xfer() - I2C transfer function used by kernel framework
  485. * @i2c_adap: Adapter pointer to the controller
  486. * @msgs: Pointer to data to be written.
  487. * @num_msgs: Number of messages to be executed
  488. *
  489. * This is the function called by the generic kernel i2c_transfer()
  490. * or i2c_smbus...() API calls. Note that this code is protected by the
  491. * semaphore set in the kernel i2c_transfer() function.
  492. *
  493. * NOTE:
  494. * READ TRANSFER : We impose a restriction of the first message to be the
  495. * index message for any read transaction.
  496. * - a no index is coded as '0',
  497. * - 2byte big endian index is coded as '3'
  498. * !!! msg[0].buf holds the actual index.
  499. * This is compatible with generic messages of smbus emulator
  500. * that send a one byte index.
  501. * eg. a I2C transation to read 2 bytes from index 0
  502. * idx = 0;
  503. * msg[0].addr = client->addr;
  504. * msg[0].flags = 0x0;
  505. * msg[0].len = 1;
  506. * msg[0].buf = &idx;
  507. *
  508. * msg[1].addr = client->addr;
  509. * msg[1].flags = I2C_M_RD;
  510. * msg[1].len = 2;
  511. * msg[1].buf = rd_buff
  512. * i2c_transfer(adap, msg, 2);
  513. *
  514. * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
  515. * If you want to emulate an SMBUS write transaction put the
  516. * index as first byte(or first and second) in the payload.
  517. * eg. a I2C transation to write 2 bytes from index 1
  518. * wr_buff[0] = 0x1;
  519. * wr_buff[1] = 0x23;
  520. * wr_buff[2] = 0x46;
  521. * msg[0].flags = 0x0;
  522. * msg[0].len = 3;
  523. * msg[0].buf = wr_buff;
  524. * i2c_transfer(adap, msg, 1);
  525. *
  526. * To read or write a block of data (multiple bytes) using SMBUS emulation
  527. * please use the i2c_smbus_read_i2c_block_data()
  528. * or i2c_smbus_write_i2c_block_data() API
  529. */
  530. static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
  531. struct i2c_msg msgs[], int num_msgs)
  532. {
  533. int status;
  534. int i;
  535. struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
  536. int j;
  537. dev->busy = true;
  538. if (dev->regulator)
  539. regulator_enable(dev->regulator);
  540. pm_runtime_get_sync(&dev->pdev->dev);
  541. clk_enable(dev->clk);
  542. status = init_hw(dev);
  543. if (status)
  544. goto out;
  545. /* Attempt three times to send the message queue */
  546. for (j = 0; j < 3; j++) {
  547. /* setup the i2c controller */
  548. setup_i2c_controller(dev);
  549. for (i = 0; i < num_msgs; i++) {
  550. if (unlikely(msgs[i].flags & I2C_M_TEN)) {
  551. dev_err(&dev->pdev->dev, "10 bit addressing"
  552. "not supported\n");
  553. status = -EINVAL;
  554. goto out;
  555. }
  556. dev->cli.slave_adr = msgs[i].addr;
  557. dev->cli.buffer = msgs[i].buf;
  558. dev->cli.count = msgs[i].len;
  559. dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
  560. dev->result = 0;
  561. status = nmk_i2c_xfer_one(dev, msgs[i].flags);
  562. if (status != 0)
  563. break;
  564. }
  565. if (status == 0)
  566. break;
  567. }
  568. out:
  569. clk_disable(dev->clk);
  570. pm_runtime_put_sync(&dev->pdev->dev);
  571. if (dev->regulator)
  572. regulator_disable(dev->regulator);
  573. dev->busy = false;
  574. /* return the no. messages processed */
  575. if (status)
  576. return status;
  577. else
  578. return num_msgs;
  579. }
  580. /**
  581. * disable_interrupts() - disable the interrupts
  582. * @dev: private data of controller
  583. * @irq: interrupt number
  584. */
  585. static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
  586. {
  587. irq = IRQ_MASK(irq);
  588. writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
  589. dev->virtbase + I2C_IMSCR);
  590. return 0;
  591. }
  592. /**
  593. * i2c_irq_handler() - interrupt routine
  594. * @irq: interrupt number
  595. * @arg: data passed to the handler
  596. *
  597. * This is the interrupt handler for the i2c driver. Currently
  598. * it handles the major interrupts like Rx & Tx FIFO management
  599. * interrupts, master transaction interrupts, arbitration and
  600. * bus error interrupts. The rest of the interrupts are treated as
  601. * unhandled.
  602. */
  603. static irqreturn_t i2c_irq_handler(int irq, void *arg)
  604. {
  605. struct nmk_i2c_dev *dev = arg;
  606. u32 tft, rft;
  607. u32 count;
  608. u32 misr;
  609. u32 src = 0;
  610. /* load Tx FIFO and Rx FIFO threshold values */
  611. tft = readl(dev->virtbase + I2C_TFTR);
  612. rft = readl(dev->virtbase + I2C_RFTR);
  613. /* read interrupt status register */
  614. misr = readl(dev->virtbase + I2C_MISR);
  615. src = __ffs(misr);
  616. switch ((1 << src)) {
  617. /* Transmit FIFO nearly empty interrupt */
  618. case I2C_IT_TXFNE:
  619. {
  620. if (dev->cli.operation == I2C_READ) {
  621. /*
  622. * in read operation why do we care for writing?
  623. * so disable the Transmit FIFO interrupt
  624. */
  625. disable_interrupts(dev, I2C_IT_TXFNE);
  626. } else {
  627. fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft));
  628. /*
  629. * if done, close the transfer by disabling the
  630. * corresponding TXFNE interrupt
  631. */
  632. if (dev->cli.count == 0)
  633. disable_interrupts(dev, I2C_IT_TXFNE);
  634. }
  635. }
  636. break;
  637. /*
  638. * Rx FIFO nearly full interrupt.
  639. * This is set when the numer of entries in Rx FIFO is
  640. * greater or equal than the threshold value programmed
  641. * in RFT
  642. */
  643. case I2C_IT_RXFNF:
  644. for (count = rft; count > 0; count--) {
  645. /* Read the Rx FIFO */
  646. *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
  647. dev->cli.buffer++;
  648. }
  649. dev->cli.count -= rft;
  650. dev->cli.xfer_bytes += rft;
  651. break;
  652. /* Rx FIFO full */
  653. case I2C_IT_RXFF:
  654. for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
  655. *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
  656. dev->cli.buffer++;
  657. }
  658. dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
  659. dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
  660. break;
  661. /* Master Transaction Done with/without stop */
  662. case I2C_IT_MTD:
  663. case I2C_IT_MTDWS:
  664. if (dev->cli.operation == I2C_READ) {
  665. while (!(readl(dev->virtbase + I2C_RISR)
  666. & I2C_IT_RXFE)) {
  667. if (dev->cli.count == 0)
  668. break;
  669. *dev->cli.buffer =
  670. readb(dev->virtbase + I2C_RFR);
  671. dev->cli.buffer++;
  672. dev->cli.count--;
  673. dev->cli.xfer_bytes++;
  674. }
  675. }
  676. disable_all_interrupts(dev);
  677. clear_all_interrupts(dev);
  678. if (dev->cli.count) {
  679. dev->result = -EIO;
  680. dev_err(&dev->pdev->dev, "%lu bytes still remain to be"
  681. "xfered\n", dev->cli.count);
  682. (void) init_hw(dev);
  683. }
  684. complete(&dev->xfer_complete);
  685. break;
  686. /* Master Arbitration lost interrupt */
  687. case I2C_IT_MAL:
  688. dev->result = -EIO;
  689. (void) init_hw(dev);
  690. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
  691. complete(&dev->xfer_complete);
  692. break;
  693. /*
  694. * Bus Error interrupt.
  695. * This happens when an unexpected start/stop condition occurs
  696. * during the transaction.
  697. */
  698. case I2C_IT_BERR:
  699. dev->result = -EIO;
  700. /* get the status */
  701. if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
  702. (void) init_hw(dev);
  703. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
  704. complete(&dev->xfer_complete);
  705. break;
  706. /*
  707. * Tx FIFO overrun interrupt.
  708. * This is set when a write operation in Tx FIFO is performed and
  709. * the Tx FIFO is full.
  710. */
  711. case I2C_IT_TXFOVR:
  712. dev->result = -EIO;
  713. (void) init_hw(dev);
  714. dev_err(&dev->pdev->dev, "Tx Fifo Over run\n");
  715. complete(&dev->xfer_complete);
  716. break;
  717. /* unhandled interrupts by this driver - TODO*/
  718. case I2C_IT_TXFE:
  719. case I2C_IT_TXFF:
  720. case I2C_IT_RXFE:
  721. case I2C_IT_RFSR:
  722. case I2C_IT_RFSE:
  723. case I2C_IT_WTSR:
  724. case I2C_IT_STD:
  725. dev_err(&dev->pdev->dev, "unhandled Interrupt\n");
  726. break;
  727. default:
  728. dev_err(&dev->pdev->dev, "spurious Interrupt..\n");
  729. break;
  730. }
  731. return IRQ_HANDLED;
  732. }
  733. #ifdef CONFIG_PM
  734. static int nmk_i2c_suspend(struct device *dev)
  735. {
  736. struct platform_device *pdev = to_platform_device(dev);
  737. struct nmk_i2c_dev *nmk_i2c = platform_get_drvdata(pdev);
  738. if (nmk_i2c->busy)
  739. return -EBUSY;
  740. return 0;
  741. }
  742. static int nmk_i2c_resume(struct device *dev)
  743. {
  744. return 0;
  745. }
  746. #else
  747. #define nmk_i2c_suspend NULL
  748. #define nmk_i2c_resume NULL
  749. #endif
  750. /*
  751. * We use noirq so that we suspend late and resume before the wakeup interrupt
  752. * to ensure that we do the !pm_runtime_suspended() check in resume before
  753. * there has been a regular pm runtime resume (via pm_runtime_get_sync()).
  754. */
  755. static const struct dev_pm_ops nmk_i2c_pm = {
  756. .suspend_noirq = nmk_i2c_suspend,
  757. .resume_noirq = nmk_i2c_resume,
  758. };
  759. static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
  760. {
  761. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  762. }
  763. static const struct i2c_algorithm nmk_i2c_algo = {
  764. .master_xfer = nmk_i2c_xfer,
  765. .functionality = nmk_i2c_functionality
  766. };
  767. static int __devinit nmk_i2c_probe(struct platform_device *pdev)
  768. {
  769. int ret = 0;
  770. struct resource *res;
  771. struct nmk_i2c_controller *pdata =
  772. pdev->dev.platform_data;
  773. struct nmk_i2c_dev *dev;
  774. struct i2c_adapter *adap;
  775. dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL);
  776. if (!dev) {
  777. dev_err(&pdev->dev, "cannot allocate memory\n");
  778. ret = -ENOMEM;
  779. goto err_no_mem;
  780. }
  781. dev->busy = false;
  782. dev->pdev = pdev;
  783. platform_set_drvdata(pdev, dev);
  784. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  785. if (!res) {
  786. ret = -ENOENT;
  787. goto err_no_resource;
  788. }
  789. if (request_mem_region(res->start, resource_size(res),
  790. DRIVER_NAME "I/O region") == NULL) {
  791. ret = -EBUSY;
  792. goto err_no_region;
  793. }
  794. dev->virtbase = ioremap(res->start, resource_size(res));
  795. if (!dev->virtbase) {
  796. ret = -ENOMEM;
  797. goto err_no_ioremap;
  798. }
  799. dev->irq = platform_get_irq(pdev, 0);
  800. ret = request_irq(dev->irq, i2c_irq_handler, IRQF_DISABLED,
  801. DRIVER_NAME, dev);
  802. if (ret) {
  803. dev_err(&pdev->dev, "cannot claim the irq %d\n", dev->irq);
  804. goto err_irq;
  805. }
  806. dev->regulator = regulator_get(&pdev->dev, "v-i2c");
  807. if (IS_ERR(dev->regulator)) {
  808. dev_warn(&pdev->dev, "could not get i2c regulator\n");
  809. dev->regulator = NULL;
  810. }
  811. pm_suspend_ignore_children(&pdev->dev, true);
  812. pm_runtime_enable(&pdev->dev);
  813. dev->clk = clk_get(&pdev->dev, NULL);
  814. if (IS_ERR(dev->clk)) {
  815. dev_err(&pdev->dev, "could not get i2c clock\n");
  816. ret = PTR_ERR(dev->clk);
  817. goto err_no_clk;
  818. }
  819. adap = &dev->adap;
  820. adap->dev.parent = &pdev->dev;
  821. adap->owner = THIS_MODULE;
  822. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  823. adap->algo = &nmk_i2c_algo;
  824. adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) :
  825. msecs_to_jiffies(20000);
  826. snprintf(adap->name, sizeof(adap->name),
  827. "Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start);
  828. /* fetch the controller id */
  829. adap->nr = pdev->id;
  830. /* fetch the controller configuration from machine */
  831. dev->cfg.clk_freq = pdata->clk_freq;
  832. dev->cfg.slsu = pdata->slsu;
  833. dev->cfg.tft = pdata->tft;
  834. dev->cfg.rft = pdata->rft;
  835. dev->cfg.sm = pdata->sm;
  836. i2c_set_adapdata(adap, dev);
  837. dev_info(&pdev->dev, "initialize %s on virtual "
  838. "base %p\n", adap->name, dev->virtbase);
  839. ret = i2c_add_numbered_adapter(adap);
  840. if (ret) {
  841. dev_err(&pdev->dev, "failed to add adapter\n");
  842. goto err_add_adap;
  843. }
  844. return 0;
  845. err_add_adap:
  846. clk_put(dev->clk);
  847. err_no_clk:
  848. if (dev->regulator)
  849. regulator_put(dev->regulator);
  850. pm_runtime_disable(&pdev->dev);
  851. free_irq(dev->irq, dev);
  852. err_irq:
  853. iounmap(dev->virtbase);
  854. err_no_ioremap:
  855. release_mem_region(res->start, resource_size(res));
  856. err_no_region:
  857. platform_set_drvdata(pdev, NULL);
  858. err_no_resource:
  859. kfree(dev);
  860. err_no_mem:
  861. return ret;
  862. }
  863. static int __devexit nmk_i2c_remove(struct platform_device *pdev)
  864. {
  865. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  866. struct nmk_i2c_dev *dev = platform_get_drvdata(pdev);
  867. i2c_del_adapter(&dev->adap);
  868. flush_i2c_fifo(dev);
  869. disable_all_interrupts(dev);
  870. clear_all_interrupts(dev);
  871. /* disable the controller */
  872. i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  873. free_irq(dev->irq, dev);
  874. iounmap(dev->virtbase);
  875. if (res)
  876. release_mem_region(res->start, resource_size(res));
  877. clk_put(dev->clk);
  878. if (dev->regulator)
  879. regulator_put(dev->regulator);
  880. pm_runtime_disable(&pdev->dev);
  881. platform_set_drvdata(pdev, NULL);
  882. kfree(dev);
  883. return 0;
  884. }
  885. static struct platform_driver nmk_i2c_driver = {
  886. .driver = {
  887. .owner = THIS_MODULE,
  888. .name = DRIVER_NAME,
  889. .pm = &nmk_i2c_pm,
  890. },
  891. .probe = nmk_i2c_probe,
  892. .remove = __devexit_p(nmk_i2c_remove),
  893. };
  894. static int __init nmk_i2c_init(void)
  895. {
  896. return platform_driver_register(&nmk_i2c_driver);
  897. }
  898. static void __exit nmk_i2c_exit(void)
  899. {
  900. platform_driver_unregister(&nmk_i2c_driver);
  901. }
  902. subsys_initcall(nmk_i2c_init);
  903. module_exit(nmk_i2c_exit);
  904. MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");
  905. MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
  906. MODULE_LICENSE("GPL");
  907. MODULE_ALIAS("platform:" DRIVER_NAME);