i2c-nomadik.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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,
  198. "flushing operation timed out giving up after %d attempts",
  199. LOOP_ATTEMPTS);
  200. return -ETIMEDOUT;
  201. }
  202. /**
  203. * disable_all_interrupts() - Disable all interrupts of this I2c Bus
  204. * @dev: private data of I2C Driver
  205. */
  206. static void disable_all_interrupts(struct nmk_i2c_dev *dev)
  207. {
  208. u32 mask = IRQ_MASK(0);
  209. writel(mask, dev->virtbase + I2C_IMSCR);
  210. }
  211. /**
  212. * clear_all_interrupts() - Clear all interrupts of I2C Controller
  213. * @dev: private data of I2C Driver
  214. */
  215. static void clear_all_interrupts(struct nmk_i2c_dev *dev)
  216. {
  217. u32 mask;
  218. mask = IRQ_MASK(I2C_CLEAR_ALL_INTS);
  219. writel(mask, dev->virtbase + I2C_ICR);
  220. }
  221. /**
  222. * init_hw() - initialize the I2C hardware
  223. * @dev: private data of I2C Driver
  224. */
  225. static int init_hw(struct nmk_i2c_dev *dev)
  226. {
  227. int stat;
  228. stat = flush_i2c_fifo(dev);
  229. if (stat)
  230. goto exit;
  231. /* disable the controller */
  232. i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
  233. disable_all_interrupts(dev);
  234. clear_all_interrupts(dev);
  235. dev->cli.operation = I2C_NO_OPERATION;
  236. exit:
  237. return stat;
  238. }
  239. /* enable peripheral, master mode operation */
  240. #define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE)
  241. /**
  242. * load_i2c_mcr_reg() - load the MCR register
  243. * @dev: private data of controller
  244. */
  245. static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev)
  246. {
  247. u32 mcr = 0;
  248. /* 7-bit address transaction */
  249. mcr |= GEN_MASK(1, I2C_MCR_AM, 12);
  250. mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);
  251. /* start byte procedure not applied */
  252. mcr |= GEN_MASK(0, I2C_MCR_SB, 11);
  253. /* check the operation, master read/write? */
  254. if (dev->cli.operation == I2C_WRITE)
  255. mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);
  256. else
  257. mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);
  258. /* stop or repeated start? */
  259. if (dev->stop)
  260. mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);
  261. else
  262. mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));
  263. mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);
  264. return mcr;
  265. }
  266. /**
  267. * setup_i2c_controller() - setup the controller
  268. * @dev: private data of controller
  269. */
  270. static void setup_i2c_controller(struct nmk_i2c_dev *dev)
  271. {
  272. u32 brcr1, brcr2;
  273. u32 i2c_clk, div;
  274. writel(0x0, dev->virtbase + I2C_CR);
  275. writel(0x0, dev->virtbase + I2C_HSMCR);
  276. writel(0x0, dev->virtbase + I2C_TFTR);
  277. writel(0x0, dev->virtbase + I2C_RFTR);
  278. writel(0x0, dev->virtbase + I2C_DMAR);
  279. /*
  280. * set the slsu:
  281. *
  282. * slsu defines the data setup time after SCL clock
  283. * stretching in terms of i2c clk cycles. The
  284. * needed setup time for the three modes are 250ns,
  285. * 100ns, 10ns respectively thus leading to the values
  286. * of 14, 6, 2 for a 48 MHz i2c clk.
  287. */
  288. writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR);
  289. i2c_clk = clk_get_rate(dev->clk);
  290. /* fallback to std. mode if machine has not provided it */
  291. if (dev->cfg.clk_freq == 0)
  292. dev->cfg.clk_freq = 100000;
  293. /*
  294. * The spec says, in case of std. mode the divider is
  295. * 2 whereas it is 3 for fast and fastplus mode of
  296. * operation. TODO - high speed support.
  297. */
  298. div = (dev->cfg.clk_freq > 100000) ? 3 : 2;
  299. /*
  300. * generate the mask for baud rate counters. The controller
  301. * has two baud rate counters. One is used for High speed
  302. * operation, and the other is for std, fast mode, fast mode
  303. * plus operation. Currently we do not supprt high speed mode
  304. * so set brcr1 to 0.
  305. */
  306. brcr1 = 0 << 16;
  307. brcr2 = (i2c_clk/(dev->cfg.clk_freq * div)) & 0xffff;
  308. /* set the baud rate counter register */
  309. writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
  310. /*
  311. * set the speed mode. Currently we support
  312. * only standard and fast mode of operation
  313. * TODO - support for fast mode plus (up to 1Mb/s)
  314. * and high speed (up to 3.4 Mb/s)
  315. */
  316. if (dev->cfg.sm > I2C_FREQ_MODE_FAST) {
  317. dev_err(&dev->pdev->dev,
  318. "do not support this mode defaulting to std. mode\n");
  319. brcr2 = i2c_clk/(100000 * 2) & 0xffff;
  320. writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
  321. writel(I2C_FREQ_MODE_STANDARD << 4,
  322. dev->virtbase + I2C_CR);
  323. }
  324. writel(dev->cfg.sm << 4, dev->virtbase + I2C_CR);
  325. /* set the Tx and Rx FIFO threshold */
  326. writel(dev->cfg.tft, dev->virtbase + I2C_TFTR);
  327. writel(dev->cfg.rft, dev->virtbase + I2C_RFTR);
  328. }
  329. /**
  330. * read_i2c() - Read from I2C client device
  331. * @dev: private data of I2C Driver
  332. *
  333. * This function reads from i2c client device when controller is in
  334. * master mode. There is a completion timeout. If there is no transfer
  335. * before timeout error is returned.
  336. */
  337. static int read_i2c(struct nmk_i2c_dev *dev)
  338. {
  339. u32 status = 0;
  340. u32 mcr;
  341. u32 irq_mask = 0;
  342. int timeout;
  343. mcr = load_i2c_mcr_reg(dev);
  344. writel(mcr, dev->virtbase + I2C_MCR);
  345. /* load the current CR value */
  346. writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
  347. dev->virtbase + I2C_CR);
  348. /* enable the controller */
  349. i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  350. init_completion(&dev->xfer_complete);
  351. /* enable interrupts by setting the mask */
  352. irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
  353. I2C_IT_MAL | I2C_IT_BERR);
  354. if (dev->stop)
  355. irq_mask |= I2C_IT_MTD;
  356. else
  357. irq_mask |= I2C_IT_MTDWS;
  358. irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
  359. writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
  360. dev->virtbase + I2C_IMSCR);
  361. timeout = wait_for_completion_timeout(
  362. &dev->xfer_complete, dev->adap.timeout);
  363. if (timeout < 0) {
  364. dev_err(&dev->pdev->dev,
  365. "wait_for_completion_timeout "
  366. "returned %d waiting for event\n", timeout);
  367. status = timeout;
  368. }
  369. if (timeout == 0) {
  370. /* Controller timed out */
  371. dev_err(&dev->pdev->dev, "read from slave 0x%x timed out\n",
  372. dev->cli.slave_adr);
  373. status = -ETIMEDOUT;
  374. }
  375. return status;
  376. }
  377. static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes)
  378. {
  379. int count;
  380. for (count = (no_bytes - 2);
  381. (count > 0) &&
  382. (dev->cli.count != 0);
  383. count--) {
  384. /* write to the Tx FIFO */
  385. writeb(*dev->cli.buffer,
  386. dev->virtbase + I2C_TFR);
  387. dev->cli.buffer++;
  388. dev->cli.count--;
  389. dev->cli.xfer_bytes++;
  390. }
  391. }
  392. /**
  393. * write_i2c() - Write data to I2C client.
  394. * @dev: private data of I2C Driver
  395. *
  396. * This function writes data to I2C client
  397. */
  398. static int write_i2c(struct nmk_i2c_dev *dev)
  399. {
  400. u32 status = 0;
  401. u32 mcr;
  402. u32 irq_mask = 0;
  403. int timeout;
  404. mcr = load_i2c_mcr_reg(dev);
  405. writel(mcr, dev->virtbase + I2C_MCR);
  406. /* load the current CR value */
  407. writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
  408. dev->virtbase + I2C_CR);
  409. /* enable the controller */
  410. i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
  411. init_completion(&dev->xfer_complete);
  412. /* enable interrupts by settings the masks */
  413. irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR);
  414. /* Fill the TX FIFO with transmit data */
  415. fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD);
  416. if (dev->cli.count != 0)
  417. irq_mask |= I2C_IT_TXFNE;
  418. /*
  419. * check if we want to transfer a single or multiple bytes, if so
  420. * set the MTDWS bit (Master Transaction Done Without Stop)
  421. * to start repeated start operation
  422. */
  423. if (dev->stop)
  424. irq_mask |= I2C_IT_MTD;
  425. else
  426. irq_mask |= I2C_IT_MTDWS;
  427. irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
  428. writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
  429. dev->virtbase + I2C_IMSCR);
  430. timeout = wait_for_completion_timeout(
  431. &dev->xfer_complete, dev->adap.timeout);
  432. if (timeout < 0) {
  433. dev_err(&dev->pdev->dev,
  434. "wait_for_completion_timeout "
  435. "returned %d waiting for event\n", timeout);
  436. status = timeout;
  437. }
  438. if (timeout == 0) {
  439. /* Controller timed out */
  440. dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n",
  441. dev->cli.slave_adr);
  442. status = -ETIMEDOUT;
  443. }
  444. return status;
  445. }
  446. /**
  447. * nmk_i2c_xfer_one() - transmit a single I2C message
  448. * @dev: device with a message encoded into it
  449. * @flags: message flags
  450. */
  451. static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags)
  452. {
  453. int status;
  454. if (flags & I2C_M_RD) {
  455. /* read operation */
  456. dev->cli.operation = I2C_READ;
  457. status = read_i2c(dev);
  458. } else {
  459. /* write operation */
  460. dev->cli.operation = I2C_WRITE;
  461. status = write_i2c(dev);
  462. }
  463. if (status || (dev->result)) {
  464. u32 i2c_sr;
  465. u32 cause;
  466. i2c_sr = readl(dev->virtbase + I2C_SR);
  467. /*
  468. * Check if the controller I2C operation status
  469. * is set to ABORT(11b).
  470. */
  471. if (((i2c_sr >> 2) & 0x3) == 0x3) {
  472. /* get the abort cause */
  473. cause = (i2c_sr >> 4) & 0x7;
  474. dev_err(&dev->pdev->dev, "%s\n",
  475. cause >= ARRAY_SIZE(abort_causes) ?
  476. "unknown reason" :
  477. abort_causes[cause]);
  478. }
  479. (void) init_hw(dev);
  480. status = status ? status : dev->result;
  481. }
  482. return status;
  483. }
  484. /**
  485. * nmk_i2c_xfer() - I2C transfer function used by kernel framework
  486. * @i2c_adap: Adapter pointer to the controller
  487. * @msgs: Pointer to data to be written.
  488. * @num_msgs: Number of messages to be executed
  489. *
  490. * This is the function called by the generic kernel i2c_transfer()
  491. * or i2c_smbus...() API calls. Note that this code is protected by the
  492. * semaphore set in the kernel i2c_transfer() function.
  493. *
  494. * NOTE:
  495. * READ TRANSFER : We impose a restriction of the first message to be the
  496. * index message for any read transaction.
  497. * - a no index is coded as '0',
  498. * - 2byte big endian index is coded as '3'
  499. * !!! msg[0].buf holds the actual index.
  500. * This is compatible with generic messages of smbus emulator
  501. * that send a one byte index.
  502. * eg. a I2C transation to read 2 bytes from index 0
  503. * idx = 0;
  504. * msg[0].addr = client->addr;
  505. * msg[0].flags = 0x0;
  506. * msg[0].len = 1;
  507. * msg[0].buf = &idx;
  508. *
  509. * msg[1].addr = client->addr;
  510. * msg[1].flags = I2C_M_RD;
  511. * msg[1].len = 2;
  512. * msg[1].buf = rd_buff
  513. * i2c_transfer(adap, msg, 2);
  514. *
  515. * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
  516. * If you want to emulate an SMBUS write transaction put the
  517. * index as first byte(or first and second) in the payload.
  518. * eg. a I2C transation to write 2 bytes from index 1
  519. * wr_buff[0] = 0x1;
  520. * wr_buff[1] = 0x23;
  521. * wr_buff[2] = 0x46;
  522. * msg[0].flags = 0x0;
  523. * msg[0].len = 3;
  524. * msg[0].buf = wr_buff;
  525. * i2c_transfer(adap, msg, 1);
  526. *
  527. * To read or write a block of data (multiple bytes) using SMBUS emulation
  528. * please use the i2c_smbus_read_i2c_block_data()
  529. * or i2c_smbus_write_i2c_block_data() API
  530. */
  531. static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
  532. struct i2c_msg msgs[], int num_msgs)
  533. {
  534. int status;
  535. int i;
  536. struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
  537. int j;
  538. dev->busy = true;
  539. if (dev->regulator)
  540. regulator_enable(dev->regulator);
  541. pm_runtime_get_sync(&dev->pdev->dev);
  542. clk_enable(dev->clk);
  543. status = init_hw(dev);
  544. if (status)
  545. goto out;
  546. /* Attempt three times to send the message queue */
  547. for (j = 0; j < 3; j++) {
  548. /* setup the i2c controller */
  549. setup_i2c_controller(dev);
  550. for (i = 0; i < num_msgs; i++) {
  551. if (unlikely(msgs[i].flags & I2C_M_TEN)) {
  552. dev_err(&dev->pdev->dev,
  553. "10 bit addressing not supported\n");
  554. status = -EINVAL;
  555. goto out;
  556. }
  557. dev->cli.slave_adr = msgs[i].addr;
  558. dev->cli.buffer = msgs[i].buf;
  559. dev->cli.count = msgs[i].len;
  560. dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
  561. dev->result = 0;
  562. status = nmk_i2c_xfer_one(dev, msgs[i].flags);
  563. if (status != 0)
  564. break;
  565. }
  566. if (status == 0)
  567. break;
  568. }
  569. out:
  570. clk_disable(dev->clk);
  571. pm_runtime_put_sync(&dev->pdev->dev);
  572. if (dev->regulator)
  573. regulator_disable(dev->regulator);
  574. dev->busy = false;
  575. /* return the no. messages processed */
  576. if (status)
  577. return status;
  578. else
  579. return num_msgs;
  580. }
  581. /**
  582. * disable_interrupts() - disable the interrupts
  583. * @dev: private data of controller
  584. * @irq: interrupt number
  585. */
  586. static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
  587. {
  588. irq = IRQ_MASK(irq);
  589. writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
  590. dev->virtbase + I2C_IMSCR);
  591. return 0;
  592. }
  593. /**
  594. * i2c_irq_handler() - interrupt routine
  595. * @irq: interrupt number
  596. * @arg: data passed to the handler
  597. *
  598. * This is the interrupt handler for the i2c driver. Currently
  599. * it handles the major interrupts like Rx & Tx FIFO management
  600. * interrupts, master transaction interrupts, arbitration and
  601. * bus error interrupts. The rest of the interrupts are treated as
  602. * unhandled.
  603. */
  604. static irqreturn_t i2c_irq_handler(int irq, void *arg)
  605. {
  606. struct nmk_i2c_dev *dev = arg;
  607. u32 tft, rft;
  608. u32 count;
  609. u32 misr;
  610. u32 src = 0;
  611. /* load Tx FIFO and Rx FIFO threshold values */
  612. tft = readl(dev->virtbase + I2C_TFTR);
  613. rft = readl(dev->virtbase + I2C_RFTR);
  614. /* read interrupt status register */
  615. misr = readl(dev->virtbase + I2C_MISR);
  616. src = __ffs(misr);
  617. switch ((1 << src)) {
  618. /* Transmit FIFO nearly empty interrupt */
  619. case I2C_IT_TXFNE:
  620. {
  621. if (dev->cli.operation == I2C_READ) {
  622. /*
  623. * in read operation why do we care for writing?
  624. * so disable the Transmit FIFO interrupt
  625. */
  626. disable_interrupts(dev, I2C_IT_TXFNE);
  627. } else {
  628. fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft));
  629. /*
  630. * if done, close the transfer by disabling the
  631. * corresponding TXFNE interrupt
  632. */
  633. if (dev->cli.count == 0)
  634. disable_interrupts(dev, I2C_IT_TXFNE);
  635. }
  636. }
  637. break;
  638. /*
  639. * Rx FIFO nearly full interrupt.
  640. * This is set when the numer of entries in Rx FIFO is
  641. * greater or equal than the threshold value programmed
  642. * in RFT
  643. */
  644. case I2C_IT_RXFNF:
  645. for (count = rft; count > 0; count--) {
  646. /* Read the Rx FIFO */
  647. *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
  648. dev->cli.buffer++;
  649. }
  650. dev->cli.count -= rft;
  651. dev->cli.xfer_bytes += rft;
  652. break;
  653. /* Rx FIFO full */
  654. case I2C_IT_RXFF:
  655. for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
  656. *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
  657. dev->cli.buffer++;
  658. }
  659. dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
  660. dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
  661. break;
  662. /* Master Transaction Done with/without stop */
  663. case I2C_IT_MTD:
  664. case I2C_IT_MTDWS:
  665. if (dev->cli.operation == I2C_READ) {
  666. while (!(readl(dev->virtbase + I2C_RISR)
  667. & I2C_IT_RXFE)) {
  668. if (dev->cli.count == 0)
  669. break;
  670. *dev->cli.buffer =
  671. readb(dev->virtbase + I2C_RFR);
  672. dev->cli.buffer++;
  673. dev->cli.count--;
  674. dev->cli.xfer_bytes++;
  675. }
  676. }
  677. disable_all_interrupts(dev);
  678. clear_all_interrupts(dev);
  679. if (dev->cli.count) {
  680. dev->result = -EIO;
  681. dev_err(&dev->pdev->dev,
  682. "%lu bytes still remain to be xfered\n",
  683. dev->cli.count);
  684. (void) init_hw(dev);
  685. }
  686. complete(&dev->xfer_complete);
  687. break;
  688. /* Master Arbitration lost interrupt */
  689. case I2C_IT_MAL:
  690. dev->result = -EIO;
  691. (void) init_hw(dev);
  692. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
  693. complete(&dev->xfer_complete);
  694. break;
  695. /*
  696. * Bus Error interrupt.
  697. * This happens when an unexpected start/stop condition occurs
  698. * during the transaction.
  699. */
  700. case I2C_IT_BERR:
  701. dev->result = -EIO;
  702. /* get the status */
  703. if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
  704. (void) init_hw(dev);
  705. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
  706. complete(&dev->xfer_complete);
  707. break;
  708. /*
  709. * Tx FIFO overrun interrupt.
  710. * This is set when a write operation in Tx FIFO is performed and
  711. * the Tx FIFO is full.
  712. */
  713. case I2C_IT_TXFOVR:
  714. dev->result = -EIO;
  715. (void) init_hw(dev);
  716. dev_err(&dev->pdev->dev, "Tx Fifo Over run\n");
  717. complete(&dev->xfer_complete);
  718. break;
  719. /* unhandled interrupts by this driver - TODO*/
  720. case I2C_IT_TXFE:
  721. case I2C_IT_TXFF:
  722. case I2C_IT_RXFE:
  723. case I2C_IT_RFSR:
  724. case I2C_IT_RFSE:
  725. case I2C_IT_WTSR:
  726. case I2C_IT_STD:
  727. dev_err(&dev->pdev->dev, "unhandled Interrupt\n");
  728. break;
  729. default:
  730. dev_err(&dev->pdev->dev, "spurious Interrupt..\n");
  731. break;
  732. }
  733. return IRQ_HANDLED;
  734. }
  735. #ifdef CONFIG_PM
  736. static int nmk_i2c_suspend(struct device *dev)
  737. {
  738. struct platform_device *pdev = to_platform_device(dev);
  739. struct nmk_i2c_dev *nmk_i2c = platform_get_drvdata(pdev);
  740. if (nmk_i2c->busy)
  741. return -EBUSY;
  742. return 0;
  743. }
  744. static int nmk_i2c_resume(struct device *dev)
  745. {
  746. return 0;
  747. }
  748. #else
  749. #define nmk_i2c_suspend NULL
  750. #define nmk_i2c_resume NULL
  751. #endif
  752. /*
  753. * We use noirq so that we suspend late and resume before the wakeup interrupt
  754. * to ensure that we do the !pm_runtime_suspended() check in resume before
  755. * there has been a regular pm runtime resume (via pm_runtime_get_sync()).
  756. */
  757. static const struct dev_pm_ops nmk_i2c_pm = {
  758. .suspend_noirq = nmk_i2c_suspend,
  759. .resume_noirq = nmk_i2c_resume,
  760. };
  761. static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
  762. {
  763. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  764. }
  765. static const struct i2c_algorithm nmk_i2c_algo = {
  766. .master_xfer = nmk_i2c_xfer,
  767. .functionality = nmk_i2c_functionality
  768. };
  769. static int __devinit nmk_i2c_probe(struct platform_device *pdev)
  770. {
  771. int ret = 0;
  772. struct resource *res;
  773. struct nmk_i2c_controller *pdata =
  774. pdev->dev.platform_data;
  775. struct nmk_i2c_dev *dev;
  776. struct i2c_adapter *adap;
  777. dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL);
  778. if (!dev) {
  779. dev_err(&pdev->dev, "cannot allocate memory\n");
  780. ret = -ENOMEM;
  781. goto err_no_mem;
  782. }
  783. dev->busy = false;
  784. dev->pdev = pdev;
  785. platform_set_drvdata(pdev, dev);
  786. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  787. if (!res) {
  788. ret = -ENOENT;
  789. goto err_no_resource;
  790. }
  791. if (request_mem_region(res->start, resource_size(res),
  792. DRIVER_NAME "I/O region") == NULL) {
  793. ret = -EBUSY;
  794. goto err_no_region;
  795. }
  796. dev->virtbase = ioremap(res->start, resource_size(res));
  797. if (!dev->virtbase) {
  798. ret = -ENOMEM;
  799. goto err_no_ioremap;
  800. }
  801. dev->irq = platform_get_irq(pdev, 0);
  802. ret = request_irq(dev->irq, i2c_irq_handler, 0,
  803. DRIVER_NAME, dev);
  804. if (ret) {
  805. dev_err(&pdev->dev, "cannot claim the irq %d\n", dev->irq);
  806. goto err_irq;
  807. }
  808. dev->regulator = regulator_get(&pdev->dev, "v-i2c");
  809. if (IS_ERR(dev->regulator)) {
  810. dev_warn(&pdev->dev, "could not get i2c regulator\n");
  811. dev->regulator = NULL;
  812. }
  813. pm_suspend_ignore_children(&pdev->dev, true);
  814. pm_runtime_enable(&pdev->dev);
  815. dev->clk = clk_get(&pdev->dev, NULL);
  816. if (IS_ERR(dev->clk)) {
  817. dev_err(&pdev->dev, "could not get i2c clock\n");
  818. ret = PTR_ERR(dev->clk);
  819. goto err_no_clk;
  820. }
  821. adap = &dev->adap;
  822. adap->dev.parent = &pdev->dev;
  823. adap->owner = THIS_MODULE;
  824. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  825. adap->algo = &nmk_i2c_algo;
  826. adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) :
  827. msecs_to_jiffies(20000);
  828. snprintf(adap->name, sizeof(adap->name),
  829. "Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start);
  830. /* fetch the controller id */
  831. adap->nr = pdev->id;
  832. /* fetch the controller configuration from machine */
  833. dev->cfg.clk_freq = pdata->clk_freq;
  834. dev->cfg.slsu = pdata->slsu;
  835. dev->cfg.tft = pdata->tft;
  836. dev->cfg.rft = pdata->rft;
  837. dev->cfg.sm = pdata->sm;
  838. i2c_set_adapdata(adap, dev);
  839. dev_info(&pdev->dev,
  840. "initialize %s on virtual base %p\n",
  841. adap->name, dev->virtbase);
  842. ret = i2c_add_numbered_adapter(adap);
  843. if (ret) {
  844. dev_err(&pdev->dev, "failed to add adapter\n");
  845. goto err_add_adap;
  846. }
  847. return 0;
  848. err_add_adap:
  849. clk_put(dev->clk);
  850. err_no_clk:
  851. if (dev->regulator)
  852. regulator_put(dev->regulator);
  853. pm_runtime_disable(&pdev->dev);
  854. free_irq(dev->irq, dev);
  855. err_irq:
  856. iounmap(dev->virtbase);
  857. err_no_ioremap:
  858. release_mem_region(res->start, resource_size(res));
  859. err_no_region:
  860. platform_set_drvdata(pdev, NULL);
  861. err_no_resource:
  862. kfree(dev);
  863. err_no_mem:
  864. return ret;
  865. }
  866. static int __devexit nmk_i2c_remove(struct platform_device *pdev)
  867. {
  868. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  869. struct nmk_i2c_dev *dev = platform_get_drvdata(pdev);
  870. i2c_del_adapter(&dev->adap);
  871. flush_i2c_fifo(dev);
  872. disable_all_interrupts(dev);
  873. clear_all_interrupts(dev);
  874. /* disable the controller */
  875. i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  876. free_irq(dev->irq, dev);
  877. iounmap(dev->virtbase);
  878. if (res)
  879. release_mem_region(res->start, resource_size(res));
  880. clk_put(dev->clk);
  881. if (dev->regulator)
  882. regulator_put(dev->regulator);
  883. pm_runtime_disable(&pdev->dev);
  884. platform_set_drvdata(pdev, NULL);
  885. kfree(dev);
  886. return 0;
  887. }
  888. static struct platform_driver nmk_i2c_driver = {
  889. .driver = {
  890. .owner = THIS_MODULE,
  891. .name = DRIVER_NAME,
  892. .pm = &nmk_i2c_pm,
  893. },
  894. .probe = nmk_i2c_probe,
  895. .remove = __devexit_p(nmk_i2c_remove),
  896. };
  897. static int __init nmk_i2c_init(void)
  898. {
  899. return platform_driver_register(&nmk_i2c_driver);
  900. }
  901. static void __exit nmk_i2c_exit(void)
  902. {
  903. platform_driver_unregister(&nmk_i2c_driver);
  904. }
  905. subsys_initcall(nmk_i2c_init);
  906. module_exit(nmk_i2c_exit);
  907. MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");
  908. MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
  909. MODULE_LICENSE("GPL");
  910. MODULE_ALIAS("platform:" DRIVER_NAME);