i2c-nomadik.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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/amba/bus.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/pm_runtime.h>
  25. #include <linux/of.h>
  26. #include <linux/pinctrl/consumer.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_freq_mode {
  96. I2C_FREQ_MODE_STANDARD, /* up to 100 Kb/s */
  97. I2C_FREQ_MODE_FAST, /* up to 400 Kb/s */
  98. I2C_FREQ_MODE_HIGH_SPEED, /* up to 3.4 Mb/s */
  99. I2C_FREQ_MODE_FAST_PLUS, /* up to 1 Mb/s */
  100. };
  101. /**
  102. * struct i2c_vendor_data - per-vendor variations
  103. * @has_mtdws: variant has the MTDWS bit
  104. * @fifodepth: variant FIFO depth
  105. */
  106. struct i2c_vendor_data {
  107. bool has_mtdws;
  108. u32 fifodepth;
  109. };
  110. enum i2c_status {
  111. I2C_NOP,
  112. I2C_ON_GOING,
  113. I2C_OK,
  114. I2C_ABORT
  115. };
  116. /* operation */
  117. enum i2c_operation {
  118. I2C_NO_OPERATION = 0xff,
  119. I2C_WRITE = 0x00,
  120. I2C_READ = 0x01
  121. };
  122. /**
  123. * struct i2c_nmk_client - client specific data
  124. * @slave_adr: 7-bit slave address
  125. * @count: no. bytes to be transferred
  126. * @buffer: client data buffer
  127. * @xfer_bytes: bytes transferred till now
  128. * @operation: current I2C operation
  129. */
  130. struct i2c_nmk_client {
  131. unsigned short slave_adr;
  132. unsigned long count;
  133. unsigned char *buffer;
  134. unsigned long xfer_bytes;
  135. enum i2c_operation operation;
  136. };
  137. /**
  138. * struct nmk_i2c_dev - private data structure of the controller.
  139. * @vendor: vendor data for this variant.
  140. * @adev: parent amba device.
  141. * @adap: corresponding I2C adapter.
  142. * @irq: interrupt line for the controller.
  143. * @virtbase: virtual io memory area.
  144. * @clk: hardware i2c block clock.
  145. * @cli: holder of client specific data.
  146. * @clk_freq: clock frequency for the operation mode
  147. * @tft: Tx FIFO Threshold in bytes
  148. * @rft: Rx FIFO Threshold in bytes
  149. * @timeout Slave response timeout (ms)
  150. * @sm: speed mode
  151. * @stop: stop condition.
  152. * @xfer_complete: acknowledge completion for a I2C message.
  153. * @result: controller propogated result.
  154. */
  155. struct nmk_i2c_dev {
  156. struct i2c_vendor_data *vendor;
  157. struct amba_device *adev;
  158. struct i2c_adapter adap;
  159. int irq;
  160. void __iomem *virtbase;
  161. struct clk *clk;
  162. struct i2c_nmk_client cli;
  163. u32 clk_freq;
  164. unsigned char tft;
  165. unsigned char rft;
  166. int timeout;
  167. enum i2c_freq_mode sm;
  168. int stop;
  169. struct completion xfer_complete;
  170. int result;
  171. };
  172. /* controller's abort causes */
  173. static const char *abort_causes[] = {
  174. "no ack received after address transmission",
  175. "no ack received during data phase",
  176. "ack received after xmission of master code",
  177. "master lost arbitration",
  178. "slave restarts",
  179. "slave reset",
  180. "overflow, maxsize is 2047 bytes",
  181. };
  182. static inline void i2c_set_bit(void __iomem *reg, u32 mask)
  183. {
  184. writel(readl(reg) | mask, reg);
  185. }
  186. static inline void i2c_clr_bit(void __iomem *reg, u32 mask)
  187. {
  188. writel(readl(reg) & ~mask, reg);
  189. }
  190. /**
  191. * flush_i2c_fifo() - This function flushes the I2C FIFO
  192. * @dev: private data of I2C Driver
  193. *
  194. * This function flushes the I2C Tx and Rx FIFOs. It returns
  195. * 0 on successful flushing of FIFO
  196. */
  197. static int flush_i2c_fifo(struct nmk_i2c_dev *dev)
  198. {
  199. #define LOOP_ATTEMPTS 10
  200. int i;
  201. unsigned long timeout;
  202. /*
  203. * flush the transmit and receive FIFO. The flushing
  204. * operation takes several cycles before to be completed.
  205. * On the completion, the I2C internal logic clears these
  206. * bits, until then no one must access Tx, Rx FIFO and
  207. * should poll on these bits waiting for the completion.
  208. */
  209. writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR);
  210. for (i = 0; i < LOOP_ATTEMPTS; i++) {
  211. timeout = jiffies + dev->adap.timeout;
  212. while (!time_after(jiffies, timeout)) {
  213. if ((readl(dev->virtbase + I2C_CR) &
  214. (I2C_CR_FTX | I2C_CR_FRX)) == 0)
  215. return 0;
  216. }
  217. }
  218. dev_err(&dev->adev->dev,
  219. "flushing operation timed out giving up after %d attempts",
  220. LOOP_ATTEMPTS);
  221. return -ETIMEDOUT;
  222. }
  223. /**
  224. * disable_all_interrupts() - Disable all interrupts of this I2c Bus
  225. * @dev: private data of I2C Driver
  226. */
  227. static void disable_all_interrupts(struct nmk_i2c_dev *dev)
  228. {
  229. u32 mask = IRQ_MASK(0);
  230. writel(mask, dev->virtbase + I2C_IMSCR);
  231. }
  232. /**
  233. * clear_all_interrupts() - Clear all interrupts of I2C Controller
  234. * @dev: private data of I2C Driver
  235. */
  236. static void clear_all_interrupts(struct nmk_i2c_dev *dev)
  237. {
  238. u32 mask;
  239. mask = IRQ_MASK(I2C_CLEAR_ALL_INTS);
  240. writel(mask, dev->virtbase + I2C_ICR);
  241. }
  242. /**
  243. * init_hw() - initialize the I2C hardware
  244. * @dev: private data of I2C Driver
  245. */
  246. static int init_hw(struct nmk_i2c_dev *dev)
  247. {
  248. int stat;
  249. stat = flush_i2c_fifo(dev);
  250. if (stat)
  251. goto exit;
  252. /* disable the controller */
  253. i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
  254. disable_all_interrupts(dev);
  255. clear_all_interrupts(dev);
  256. dev->cli.operation = I2C_NO_OPERATION;
  257. exit:
  258. return stat;
  259. }
  260. /* enable peripheral, master mode operation */
  261. #define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE)
  262. /**
  263. * load_i2c_mcr_reg() - load the MCR register
  264. * @dev: private data of controller
  265. * @flags: message flags
  266. */
  267. static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev, u16 flags)
  268. {
  269. u32 mcr = 0;
  270. unsigned short slave_adr_3msb_bits;
  271. mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);
  272. if (unlikely(flags & I2C_M_TEN)) {
  273. /* 10-bit address transaction */
  274. mcr |= GEN_MASK(2, I2C_MCR_AM, 12);
  275. /*
  276. * Get the top 3 bits.
  277. * EA10 represents extended address in MCR. This includes
  278. * the extension (MSB bits) of the 7 bit address loaded
  279. * in A7
  280. */
  281. slave_adr_3msb_bits = (dev->cli.slave_adr >> 7) & 0x7;
  282. mcr |= GEN_MASK(slave_adr_3msb_bits, I2C_MCR_EA10, 8);
  283. } else {
  284. /* 7-bit address transaction */
  285. mcr |= GEN_MASK(1, I2C_MCR_AM, 12);
  286. }
  287. /* start byte procedure not applied */
  288. mcr |= GEN_MASK(0, I2C_MCR_SB, 11);
  289. /* check the operation, master read/write? */
  290. if (dev->cli.operation == I2C_WRITE)
  291. mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);
  292. else
  293. mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);
  294. /* stop or repeated start? */
  295. if (dev->stop)
  296. mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);
  297. else
  298. mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));
  299. mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);
  300. return mcr;
  301. }
  302. /**
  303. * setup_i2c_controller() - setup the controller
  304. * @dev: private data of controller
  305. */
  306. static void setup_i2c_controller(struct nmk_i2c_dev *dev)
  307. {
  308. u32 brcr1, brcr2;
  309. u32 i2c_clk, div;
  310. u32 ns;
  311. u16 slsu;
  312. writel(0x0, dev->virtbase + I2C_CR);
  313. writel(0x0, dev->virtbase + I2C_HSMCR);
  314. writel(0x0, dev->virtbase + I2C_TFTR);
  315. writel(0x0, dev->virtbase + I2C_RFTR);
  316. writel(0x0, dev->virtbase + I2C_DMAR);
  317. i2c_clk = clk_get_rate(dev->clk);
  318. /*
  319. * set the slsu:
  320. *
  321. * slsu defines the data setup time after SCL clock
  322. * stretching in terms of i2c clk cycles + 1 (zero means
  323. * "wait one cycle"), the needed setup time for the three
  324. * modes are 250ns, 100ns, 10ns respectively.
  325. *
  326. * As the time for one cycle T in nanoseconds is
  327. * T = (1/f) * 1000000000 =>
  328. * slsu = cycles / (1000000000 / f) + 1
  329. */
  330. ns = DIV_ROUND_UP_ULL(1000000000ULL, i2c_clk);
  331. switch (dev->sm) {
  332. case I2C_FREQ_MODE_FAST:
  333. case I2C_FREQ_MODE_FAST_PLUS:
  334. slsu = DIV_ROUND_UP(100, ns); /* Fast */
  335. break;
  336. case I2C_FREQ_MODE_HIGH_SPEED:
  337. slsu = DIV_ROUND_UP(10, ns); /* High */
  338. break;
  339. case I2C_FREQ_MODE_STANDARD:
  340. default:
  341. slsu = DIV_ROUND_UP(250, ns); /* Standard */
  342. break;
  343. }
  344. slsu += 1;
  345. dev_dbg(&dev->adev->dev, "calculated SLSU = %04x\n", slsu);
  346. writel(slsu << 16, dev->virtbase + I2C_SCR);
  347. /*
  348. * The spec says, in case of std. mode the divider is
  349. * 2 whereas it is 3 for fast and fastplus mode of
  350. * operation. TODO - high speed support.
  351. */
  352. div = (dev->clk_freq > 100000) ? 3 : 2;
  353. /*
  354. * generate the mask for baud rate counters. The controller
  355. * has two baud rate counters. One is used for High speed
  356. * operation, and the other is for std, fast mode, fast mode
  357. * plus operation. Currently we do not supprt high speed mode
  358. * so set brcr1 to 0.
  359. */
  360. brcr1 = 0 << 16;
  361. brcr2 = (i2c_clk/(dev->clk_freq * div)) & 0xffff;
  362. /* set the baud rate counter register */
  363. writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
  364. /*
  365. * set the speed mode. Currently we support
  366. * only standard and fast mode of operation
  367. * TODO - support for fast mode plus (up to 1Mb/s)
  368. * and high speed (up to 3.4 Mb/s)
  369. */
  370. if (dev->sm > I2C_FREQ_MODE_FAST) {
  371. dev_err(&dev->adev->dev,
  372. "do not support this mode defaulting to std. mode\n");
  373. brcr2 = i2c_clk/(100000 * 2) & 0xffff;
  374. writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
  375. writel(I2C_FREQ_MODE_STANDARD << 4,
  376. dev->virtbase + I2C_CR);
  377. }
  378. writel(dev->sm << 4, dev->virtbase + I2C_CR);
  379. /* set the Tx and Rx FIFO threshold */
  380. writel(dev->tft, dev->virtbase + I2C_TFTR);
  381. writel(dev->rft, dev->virtbase + I2C_RFTR);
  382. }
  383. /**
  384. * read_i2c() - Read from I2C client device
  385. * @dev: private data of I2C Driver
  386. * @flags: message flags
  387. *
  388. * This function reads from i2c client device when controller is in
  389. * master mode. There is a completion timeout. If there is no transfer
  390. * before timeout error is returned.
  391. */
  392. static int read_i2c(struct nmk_i2c_dev *dev, u16 flags)
  393. {
  394. int status = 0;
  395. u32 mcr, irq_mask;
  396. unsigned long timeout;
  397. mcr = load_i2c_mcr_reg(dev, flags);
  398. writel(mcr, dev->virtbase + I2C_MCR);
  399. /* load the current CR value */
  400. writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
  401. dev->virtbase + I2C_CR);
  402. /* enable the controller */
  403. i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  404. init_completion(&dev->xfer_complete);
  405. /* enable interrupts by setting the mask */
  406. irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
  407. I2C_IT_MAL | I2C_IT_BERR);
  408. if (dev->stop || !dev->vendor->has_mtdws)
  409. irq_mask |= I2C_IT_MTD;
  410. else
  411. irq_mask |= I2C_IT_MTDWS;
  412. irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
  413. writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
  414. dev->virtbase + I2C_IMSCR);
  415. timeout = wait_for_completion_timeout(
  416. &dev->xfer_complete, dev->adap.timeout);
  417. if (timeout == 0) {
  418. /* Controller timed out */
  419. dev_err(&dev->adev->dev, "read from slave 0x%x timed out\n",
  420. dev->cli.slave_adr);
  421. status = -ETIMEDOUT;
  422. }
  423. return status;
  424. }
  425. static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes)
  426. {
  427. int count;
  428. for (count = (no_bytes - 2);
  429. (count > 0) &&
  430. (dev->cli.count != 0);
  431. count--) {
  432. /* write to the Tx FIFO */
  433. writeb(*dev->cli.buffer,
  434. dev->virtbase + I2C_TFR);
  435. dev->cli.buffer++;
  436. dev->cli.count--;
  437. dev->cli.xfer_bytes++;
  438. }
  439. }
  440. /**
  441. * write_i2c() - Write data to I2C client.
  442. * @dev: private data of I2C Driver
  443. * @flags: message flags
  444. *
  445. * This function writes data to I2C client
  446. */
  447. static int write_i2c(struct nmk_i2c_dev *dev, u16 flags)
  448. {
  449. u32 status = 0;
  450. u32 mcr, irq_mask;
  451. unsigned long timeout;
  452. mcr = load_i2c_mcr_reg(dev, flags);
  453. writel(mcr, dev->virtbase + I2C_MCR);
  454. /* load the current CR value */
  455. writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
  456. dev->virtbase + I2C_CR);
  457. /* enable the controller */
  458. i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
  459. init_completion(&dev->xfer_complete);
  460. /* enable interrupts by settings the masks */
  461. irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR);
  462. /* Fill the TX FIFO with transmit data */
  463. fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD);
  464. if (dev->cli.count != 0)
  465. irq_mask |= I2C_IT_TXFNE;
  466. /*
  467. * check if we want to transfer a single or multiple bytes, if so
  468. * set the MTDWS bit (Master Transaction Done Without Stop)
  469. * to start repeated start operation
  470. */
  471. if (dev->stop || !dev->vendor->has_mtdws)
  472. irq_mask |= I2C_IT_MTD;
  473. else
  474. irq_mask |= I2C_IT_MTDWS;
  475. irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
  476. writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
  477. dev->virtbase + I2C_IMSCR);
  478. timeout = wait_for_completion_timeout(
  479. &dev->xfer_complete, dev->adap.timeout);
  480. if (timeout == 0) {
  481. /* Controller timed out */
  482. dev_err(&dev->adev->dev, "write to slave 0x%x timed out\n",
  483. dev->cli.slave_adr);
  484. status = -ETIMEDOUT;
  485. }
  486. return status;
  487. }
  488. /**
  489. * nmk_i2c_xfer_one() - transmit a single I2C message
  490. * @dev: device with a message encoded into it
  491. * @flags: message flags
  492. */
  493. static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags)
  494. {
  495. int status;
  496. if (flags & I2C_M_RD) {
  497. /* read operation */
  498. dev->cli.operation = I2C_READ;
  499. status = read_i2c(dev, flags);
  500. } else {
  501. /* write operation */
  502. dev->cli.operation = I2C_WRITE;
  503. status = write_i2c(dev, flags);
  504. }
  505. if (status || (dev->result)) {
  506. u32 i2c_sr;
  507. u32 cause;
  508. i2c_sr = readl(dev->virtbase + I2C_SR);
  509. /*
  510. * Check if the controller I2C operation status
  511. * is set to ABORT(11b).
  512. */
  513. if (((i2c_sr >> 2) & 0x3) == 0x3) {
  514. /* get the abort cause */
  515. cause = (i2c_sr >> 4) & 0x7;
  516. dev_err(&dev->adev->dev, "%s\n",
  517. cause >= ARRAY_SIZE(abort_causes) ?
  518. "unknown reason" :
  519. abort_causes[cause]);
  520. }
  521. (void) init_hw(dev);
  522. status = status ? status : dev->result;
  523. }
  524. return status;
  525. }
  526. /**
  527. * nmk_i2c_xfer() - I2C transfer function used by kernel framework
  528. * @i2c_adap: Adapter pointer to the controller
  529. * @msgs: Pointer to data to be written.
  530. * @num_msgs: Number of messages to be executed
  531. *
  532. * This is the function called by the generic kernel i2c_transfer()
  533. * or i2c_smbus...() API calls. Note that this code is protected by the
  534. * semaphore set in the kernel i2c_transfer() function.
  535. *
  536. * NOTE:
  537. * READ TRANSFER : We impose a restriction of the first message to be the
  538. * index message for any read transaction.
  539. * - a no index is coded as '0',
  540. * - 2byte big endian index is coded as '3'
  541. * !!! msg[0].buf holds the actual index.
  542. * This is compatible with generic messages of smbus emulator
  543. * that send a one byte index.
  544. * eg. a I2C transation to read 2 bytes from index 0
  545. * idx = 0;
  546. * msg[0].addr = client->addr;
  547. * msg[0].flags = 0x0;
  548. * msg[0].len = 1;
  549. * msg[0].buf = &idx;
  550. *
  551. * msg[1].addr = client->addr;
  552. * msg[1].flags = I2C_M_RD;
  553. * msg[1].len = 2;
  554. * msg[1].buf = rd_buff
  555. * i2c_transfer(adap, msg, 2);
  556. *
  557. * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
  558. * If you want to emulate an SMBUS write transaction put the
  559. * index as first byte(or first and second) in the payload.
  560. * eg. a I2C transation to write 2 bytes from index 1
  561. * wr_buff[0] = 0x1;
  562. * wr_buff[1] = 0x23;
  563. * wr_buff[2] = 0x46;
  564. * msg[0].flags = 0x0;
  565. * msg[0].len = 3;
  566. * msg[0].buf = wr_buff;
  567. * i2c_transfer(adap, msg, 1);
  568. *
  569. * To read or write a block of data (multiple bytes) using SMBUS emulation
  570. * please use the i2c_smbus_read_i2c_block_data()
  571. * or i2c_smbus_write_i2c_block_data() API
  572. */
  573. static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
  574. struct i2c_msg msgs[], int num_msgs)
  575. {
  576. int status = 0;
  577. int i;
  578. struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
  579. int j;
  580. pm_runtime_get_sync(&dev->adev->dev);
  581. /* Attempt three times to send the message queue */
  582. for (j = 0; j < 3; j++) {
  583. /* setup the i2c controller */
  584. setup_i2c_controller(dev);
  585. for (i = 0; i < num_msgs; i++) {
  586. dev->cli.slave_adr = msgs[i].addr;
  587. dev->cli.buffer = msgs[i].buf;
  588. dev->cli.count = msgs[i].len;
  589. dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
  590. dev->result = 0;
  591. status = nmk_i2c_xfer_one(dev, msgs[i].flags);
  592. if (status != 0)
  593. break;
  594. }
  595. if (status == 0)
  596. break;
  597. }
  598. pm_runtime_put_sync(&dev->adev->dev);
  599. /* return the no. messages processed */
  600. if (status)
  601. return status;
  602. else
  603. return num_msgs;
  604. }
  605. /**
  606. * disable_interrupts() - disable the interrupts
  607. * @dev: private data of controller
  608. * @irq: interrupt number
  609. */
  610. static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
  611. {
  612. irq = IRQ_MASK(irq);
  613. writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
  614. dev->virtbase + I2C_IMSCR);
  615. return 0;
  616. }
  617. /**
  618. * i2c_irq_handler() - interrupt routine
  619. * @irq: interrupt number
  620. * @arg: data passed to the handler
  621. *
  622. * This is the interrupt handler for the i2c driver. Currently
  623. * it handles the major interrupts like Rx & Tx FIFO management
  624. * interrupts, master transaction interrupts, arbitration and
  625. * bus error interrupts. The rest of the interrupts are treated as
  626. * unhandled.
  627. */
  628. static irqreturn_t i2c_irq_handler(int irq, void *arg)
  629. {
  630. struct nmk_i2c_dev *dev = arg;
  631. u32 tft, rft;
  632. u32 count;
  633. u32 misr, src;
  634. /* load Tx FIFO and Rx FIFO threshold values */
  635. tft = readl(dev->virtbase + I2C_TFTR);
  636. rft = readl(dev->virtbase + I2C_RFTR);
  637. /* read interrupt status register */
  638. misr = readl(dev->virtbase + I2C_MISR);
  639. src = __ffs(misr);
  640. switch ((1 << src)) {
  641. /* Transmit FIFO nearly empty interrupt */
  642. case I2C_IT_TXFNE:
  643. {
  644. if (dev->cli.operation == I2C_READ) {
  645. /*
  646. * in read operation why do we care for writing?
  647. * so disable the Transmit FIFO interrupt
  648. */
  649. disable_interrupts(dev, I2C_IT_TXFNE);
  650. } else {
  651. fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft));
  652. /*
  653. * if done, close the transfer by disabling the
  654. * corresponding TXFNE interrupt
  655. */
  656. if (dev->cli.count == 0)
  657. disable_interrupts(dev, I2C_IT_TXFNE);
  658. }
  659. }
  660. break;
  661. /*
  662. * Rx FIFO nearly full interrupt.
  663. * This is set when the numer of entries in Rx FIFO is
  664. * greater or equal than the threshold value programmed
  665. * in RFT
  666. */
  667. case I2C_IT_RXFNF:
  668. for (count = rft; count > 0; count--) {
  669. /* Read the Rx FIFO */
  670. *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
  671. dev->cli.buffer++;
  672. }
  673. dev->cli.count -= rft;
  674. dev->cli.xfer_bytes += rft;
  675. break;
  676. /* Rx FIFO full */
  677. case I2C_IT_RXFF:
  678. for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
  679. *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
  680. dev->cli.buffer++;
  681. }
  682. dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
  683. dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
  684. break;
  685. /* Master Transaction Done with/without stop */
  686. case I2C_IT_MTD:
  687. case I2C_IT_MTDWS:
  688. if (dev->cli.operation == I2C_READ) {
  689. while (!(readl(dev->virtbase + I2C_RISR)
  690. & I2C_IT_RXFE)) {
  691. if (dev->cli.count == 0)
  692. break;
  693. *dev->cli.buffer =
  694. readb(dev->virtbase + I2C_RFR);
  695. dev->cli.buffer++;
  696. dev->cli.count--;
  697. dev->cli.xfer_bytes++;
  698. }
  699. }
  700. disable_all_interrupts(dev);
  701. clear_all_interrupts(dev);
  702. if (dev->cli.count) {
  703. dev->result = -EIO;
  704. dev_err(&dev->adev->dev,
  705. "%lu bytes still remain to be xfered\n",
  706. dev->cli.count);
  707. (void) init_hw(dev);
  708. }
  709. complete(&dev->xfer_complete);
  710. break;
  711. /* Master Arbitration lost interrupt */
  712. case I2C_IT_MAL:
  713. dev->result = -EIO;
  714. (void) init_hw(dev);
  715. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
  716. complete(&dev->xfer_complete);
  717. break;
  718. /*
  719. * Bus Error interrupt.
  720. * This happens when an unexpected start/stop condition occurs
  721. * during the transaction.
  722. */
  723. case I2C_IT_BERR:
  724. dev->result = -EIO;
  725. /* get the status */
  726. if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
  727. (void) init_hw(dev);
  728. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
  729. complete(&dev->xfer_complete);
  730. break;
  731. /*
  732. * Tx FIFO overrun interrupt.
  733. * This is set when a write operation in Tx FIFO is performed and
  734. * the Tx FIFO is full.
  735. */
  736. case I2C_IT_TXFOVR:
  737. dev->result = -EIO;
  738. (void) init_hw(dev);
  739. dev_err(&dev->adev->dev, "Tx Fifo Over run\n");
  740. complete(&dev->xfer_complete);
  741. break;
  742. /* unhandled interrupts by this driver - TODO*/
  743. case I2C_IT_TXFE:
  744. case I2C_IT_TXFF:
  745. case I2C_IT_RXFE:
  746. case I2C_IT_RFSR:
  747. case I2C_IT_RFSE:
  748. case I2C_IT_WTSR:
  749. case I2C_IT_STD:
  750. dev_err(&dev->adev->dev, "unhandled Interrupt\n");
  751. break;
  752. default:
  753. dev_err(&dev->adev->dev, "spurious Interrupt..\n");
  754. break;
  755. }
  756. return IRQ_HANDLED;
  757. }
  758. #ifdef CONFIG_PM_SLEEP
  759. static int nmk_i2c_suspend_late(struct device *dev)
  760. {
  761. int ret;
  762. ret = pm_runtime_force_suspend(dev);
  763. if (ret)
  764. return ret;
  765. pinctrl_pm_select_sleep_state(dev);
  766. return 0;
  767. }
  768. static int nmk_i2c_resume_early(struct device *dev)
  769. {
  770. return pm_runtime_force_resume(dev);
  771. }
  772. #endif
  773. #ifdef CONFIG_PM
  774. static int nmk_i2c_runtime_suspend(struct device *dev)
  775. {
  776. struct amba_device *adev = to_amba_device(dev);
  777. struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev);
  778. clk_disable_unprepare(nmk_i2c->clk);
  779. pinctrl_pm_select_idle_state(dev);
  780. return 0;
  781. }
  782. static int nmk_i2c_runtime_resume(struct device *dev)
  783. {
  784. struct amba_device *adev = to_amba_device(dev);
  785. struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev);
  786. int ret;
  787. ret = clk_prepare_enable(nmk_i2c->clk);
  788. if (ret) {
  789. dev_err(dev, "can't prepare_enable clock\n");
  790. return ret;
  791. }
  792. pinctrl_pm_select_default_state(dev);
  793. ret = init_hw(nmk_i2c);
  794. if (ret) {
  795. clk_disable_unprepare(nmk_i2c->clk);
  796. pinctrl_pm_select_idle_state(dev);
  797. }
  798. return ret;
  799. }
  800. #endif
  801. static const struct dev_pm_ops nmk_i2c_pm = {
  802. SET_LATE_SYSTEM_SLEEP_PM_OPS(nmk_i2c_suspend_late, nmk_i2c_resume_early)
  803. SET_RUNTIME_PM_OPS(nmk_i2c_runtime_suspend,
  804. nmk_i2c_runtime_resume,
  805. NULL)
  806. };
  807. static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
  808. {
  809. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
  810. }
  811. static const struct i2c_algorithm nmk_i2c_algo = {
  812. .master_xfer = nmk_i2c_xfer,
  813. .functionality = nmk_i2c_functionality
  814. };
  815. static void nmk_i2c_of_probe(struct device_node *np,
  816. struct nmk_i2c_dev *nmk)
  817. {
  818. /* Default to 100 kHz if no frequency is given in the node */
  819. if (of_property_read_u32(np, "clock-frequency", &nmk->clk_freq))
  820. nmk->clk_freq = 100000;
  821. /* This driver only supports 'standard' and 'fast' modes of operation. */
  822. if (nmk->clk_freq <= 100000)
  823. nmk->sm = I2C_FREQ_MODE_STANDARD;
  824. else
  825. nmk->sm = I2C_FREQ_MODE_FAST;
  826. nmk->tft = 1; /* Tx FIFO threshold */
  827. nmk->rft = 8; /* Rx FIFO threshold */
  828. nmk->timeout = 200; /* Slave response timeout(ms) */
  829. }
  830. static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
  831. {
  832. int ret = 0;
  833. struct device_node *np = adev->dev.of_node;
  834. struct nmk_i2c_dev *dev;
  835. struct i2c_adapter *adap;
  836. struct i2c_vendor_data *vendor = id->data;
  837. u32 max_fifo_threshold = (vendor->fifodepth / 2) - 1;
  838. dev = devm_kzalloc(&adev->dev, sizeof(struct nmk_i2c_dev), GFP_KERNEL);
  839. if (!dev) {
  840. dev_err(&adev->dev, "cannot allocate memory\n");
  841. ret = -ENOMEM;
  842. goto err_no_mem;
  843. }
  844. dev->vendor = vendor;
  845. dev->adev = adev;
  846. nmk_i2c_of_probe(np, dev);
  847. if (dev->tft > max_fifo_threshold) {
  848. dev_warn(&adev->dev, "requested TX FIFO threshold %u, adjusted down to %u\n",
  849. dev->tft, max_fifo_threshold);
  850. dev->tft = max_fifo_threshold;
  851. }
  852. if (dev->rft > max_fifo_threshold) {
  853. dev_warn(&adev->dev, "requested RX FIFO threshold %u, adjusted down to %u\n",
  854. dev->rft, max_fifo_threshold);
  855. dev->rft = max_fifo_threshold;
  856. }
  857. amba_set_drvdata(adev, dev);
  858. dev->virtbase = devm_ioremap(&adev->dev, adev->res.start,
  859. resource_size(&adev->res));
  860. if (!dev->virtbase) {
  861. ret = -ENOMEM;
  862. goto err_no_mem;
  863. }
  864. dev->irq = adev->irq[0];
  865. ret = devm_request_irq(&adev->dev, dev->irq, i2c_irq_handler, 0,
  866. DRIVER_NAME, dev);
  867. if (ret) {
  868. dev_err(&adev->dev, "cannot claim the irq %d\n", dev->irq);
  869. goto err_no_mem;
  870. }
  871. pm_suspend_ignore_children(&adev->dev, true);
  872. dev->clk = devm_clk_get(&adev->dev, NULL);
  873. if (IS_ERR(dev->clk)) {
  874. dev_err(&adev->dev, "could not get i2c clock\n");
  875. ret = PTR_ERR(dev->clk);
  876. goto err_no_mem;
  877. }
  878. ret = clk_prepare_enable(dev->clk);
  879. if (ret) {
  880. dev_err(&adev->dev, "can't prepare_enable clock\n");
  881. goto err_no_mem;
  882. }
  883. init_hw(dev);
  884. adap = &dev->adap;
  885. adap->dev.of_node = np;
  886. adap->dev.parent = &adev->dev;
  887. adap->owner = THIS_MODULE;
  888. adap->class = I2C_CLASS_DEPRECATED;
  889. adap->algo = &nmk_i2c_algo;
  890. adap->timeout = msecs_to_jiffies(dev->timeout);
  891. snprintf(adap->name, sizeof(adap->name),
  892. "Nomadik I2C at %pR", &adev->res);
  893. i2c_set_adapdata(adap, dev);
  894. dev_info(&adev->dev,
  895. "initialize %s on virtual base %p\n",
  896. adap->name, dev->virtbase);
  897. ret = i2c_add_adapter(adap);
  898. if (ret) {
  899. dev_err(&adev->dev, "failed to add adapter\n");
  900. goto err_no_adap;
  901. }
  902. pm_runtime_put(&adev->dev);
  903. return 0;
  904. err_no_adap:
  905. clk_disable_unprepare(dev->clk);
  906. err_no_mem:
  907. return ret;
  908. }
  909. static int nmk_i2c_remove(struct amba_device *adev)
  910. {
  911. struct resource *res = &adev->res;
  912. struct nmk_i2c_dev *dev = amba_get_drvdata(adev);
  913. i2c_del_adapter(&dev->adap);
  914. flush_i2c_fifo(dev);
  915. disable_all_interrupts(dev);
  916. clear_all_interrupts(dev);
  917. /* disable the controller */
  918. i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  919. clk_disable_unprepare(dev->clk);
  920. if (res)
  921. release_mem_region(res->start, resource_size(res));
  922. return 0;
  923. }
  924. static struct i2c_vendor_data vendor_stn8815 = {
  925. .has_mtdws = false,
  926. .fifodepth = 16, /* Guessed from TFTR/RFTR = 7 */
  927. };
  928. static struct i2c_vendor_data vendor_db8500 = {
  929. .has_mtdws = true,
  930. .fifodepth = 32, /* Guessed from TFTR/RFTR = 15 */
  931. };
  932. static struct amba_id nmk_i2c_ids[] = {
  933. {
  934. .id = 0x00180024,
  935. .mask = 0x00ffffff,
  936. .data = &vendor_stn8815,
  937. },
  938. {
  939. .id = 0x00380024,
  940. .mask = 0x00ffffff,
  941. .data = &vendor_db8500,
  942. },
  943. {},
  944. };
  945. MODULE_DEVICE_TABLE(amba, nmk_i2c_ids);
  946. static struct amba_driver nmk_i2c_driver = {
  947. .drv = {
  948. .owner = THIS_MODULE,
  949. .name = DRIVER_NAME,
  950. .pm = &nmk_i2c_pm,
  951. },
  952. .id_table = nmk_i2c_ids,
  953. .probe = nmk_i2c_probe,
  954. .remove = nmk_i2c_remove,
  955. };
  956. static int __init nmk_i2c_init(void)
  957. {
  958. return amba_driver_register(&nmk_i2c_driver);
  959. }
  960. static void __exit nmk_i2c_exit(void)
  961. {
  962. amba_driver_unregister(&nmk_i2c_driver);
  963. }
  964. subsys_initcall(nmk_i2c_init);
  965. module_exit(nmk_i2c_exit);
  966. MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");
  967. MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
  968. MODULE_LICENSE("GPL");