i2c-nomadik.c 26 KB

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