i2c-cadence.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /*
  2. * I2C bus driver for the Cadence I2C controller.
  3. *
  4. * Copyright (C) 2009 - 2014 Xilinx, Inc.
  5. *
  6. * This program is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation;
  9. * either version 2 of the License, or (at your option) any
  10. * later version.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/i2c.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. /* Register offsets for the I2C device. */
  20. #define CDNS_I2C_CR_OFFSET 0x00 /* Control Register, RW */
  21. #define CDNS_I2C_SR_OFFSET 0x04 /* Status Register, RO */
  22. #define CDNS_I2C_ADDR_OFFSET 0x08 /* I2C Address Register, RW */
  23. #define CDNS_I2C_DATA_OFFSET 0x0C /* I2C Data Register, RW */
  24. #define CDNS_I2C_ISR_OFFSET 0x10 /* IRQ Status Register, RW */
  25. #define CDNS_I2C_XFER_SIZE_OFFSET 0x14 /* Transfer Size Register, RW */
  26. #define CDNS_I2C_TIME_OUT_OFFSET 0x1C /* Time Out Register, RW */
  27. #define CDNS_I2C_IER_OFFSET 0x24 /* IRQ Enable Register, WO */
  28. #define CDNS_I2C_IDR_OFFSET 0x28 /* IRQ Disable Register, WO */
  29. /* Control Register Bit mask definitions */
  30. #define CDNS_I2C_CR_HOLD BIT(4) /* Hold Bus bit */
  31. #define CDNS_I2C_CR_ACK_EN BIT(3)
  32. #define CDNS_I2C_CR_NEA BIT(2)
  33. #define CDNS_I2C_CR_MS BIT(1)
  34. /* Read or Write Master transfer 0 = Transmitter, 1 = Receiver */
  35. #define CDNS_I2C_CR_RW BIT(0)
  36. /* 1 = Auto init FIFO to zeroes */
  37. #define CDNS_I2C_CR_CLR_FIFO BIT(6)
  38. #define CDNS_I2C_CR_DIVA_SHIFT 14
  39. #define CDNS_I2C_CR_DIVA_MASK (3 << CDNS_I2C_CR_DIVA_SHIFT)
  40. #define CDNS_I2C_CR_DIVB_SHIFT 8
  41. #define CDNS_I2C_CR_DIVB_MASK (0x3f << CDNS_I2C_CR_DIVB_SHIFT)
  42. /* Status Register Bit mask definitions */
  43. #define CDNS_I2C_SR_BA BIT(8)
  44. #define CDNS_I2C_SR_RXDV BIT(5)
  45. /*
  46. * I2C Address Register Bit mask definitions
  47. * Normal addressing mode uses [6:0] bits. Extended addressing mode uses [9:0]
  48. * bits. A write access to this register always initiates a transfer if the I2C
  49. * is in master mode.
  50. */
  51. #define CDNS_I2C_ADDR_MASK 0x000003FF /* I2C Address Mask */
  52. /*
  53. * I2C Interrupt Registers Bit mask definitions
  54. * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
  55. * bit definitions.
  56. */
  57. #define CDNS_I2C_IXR_ARB_LOST BIT(9)
  58. #define CDNS_I2C_IXR_RX_UNF BIT(7)
  59. #define CDNS_I2C_IXR_TX_OVF BIT(6)
  60. #define CDNS_I2C_IXR_RX_OVF BIT(5)
  61. #define CDNS_I2C_IXR_SLV_RDY BIT(4)
  62. #define CDNS_I2C_IXR_TO BIT(3)
  63. #define CDNS_I2C_IXR_NACK BIT(2)
  64. #define CDNS_I2C_IXR_DATA BIT(1)
  65. #define CDNS_I2C_IXR_COMP BIT(0)
  66. #define CDNS_I2C_IXR_ALL_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
  67. CDNS_I2C_IXR_RX_UNF | \
  68. CDNS_I2C_IXR_TX_OVF | \
  69. CDNS_I2C_IXR_RX_OVF | \
  70. CDNS_I2C_IXR_SLV_RDY | \
  71. CDNS_I2C_IXR_TO | \
  72. CDNS_I2C_IXR_NACK | \
  73. CDNS_I2C_IXR_DATA | \
  74. CDNS_I2C_IXR_COMP)
  75. #define CDNS_I2C_IXR_ERR_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
  76. CDNS_I2C_IXR_RX_UNF | \
  77. CDNS_I2C_IXR_TX_OVF | \
  78. CDNS_I2C_IXR_RX_OVF | \
  79. CDNS_I2C_IXR_NACK)
  80. #define CDNS_I2C_ENABLED_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
  81. CDNS_I2C_IXR_RX_UNF | \
  82. CDNS_I2C_IXR_TX_OVF | \
  83. CDNS_I2C_IXR_RX_OVF | \
  84. CDNS_I2C_IXR_NACK | \
  85. CDNS_I2C_IXR_DATA | \
  86. CDNS_I2C_IXR_COMP)
  87. #define CDNS_I2C_TIMEOUT msecs_to_jiffies(1000)
  88. #define CDNS_I2C_FIFO_DEPTH 16
  89. /* FIFO depth at which the DATA interrupt occurs */
  90. #define CDNS_I2C_DATA_INTR_DEPTH (CDNS_I2C_FIFO_DEPTH - 2)
  91. #define CDNS_I2C_MAX_TRANSFER_SIZE 255
  92. /* Transfer size in multiples of data interrupt depth */
  93. #define CDNS_I2C_TRANSFER_SIZE (CDNS_I2C_MAX_TRANSFER_SIZE - 3)
  94. #define DRIVER_NAME "cdns-i2c"
  95. #define CDNS_I2C_SPEED_MAX 400000
  96. #define CDNS_I2C_SPEED_DEFAULT 100000
  97. #define CDNS_I2C_DIVA_MAX 4
  98. #define CDNS_I2C_DIVB_MAX 64
  99. #define CDNS_I2C_TIMEOUT_MAX 0xFF
  100. #define cdns_i2c_readreg(offset) readl_relaxed(id->membase + offset)
  101. #define cdns_i2c_writereg(val, offset) writel_relaxed(val, id->membase + offset)
  102. /**
  103. * struct cdns_i2c - I2C device private data structure
  104. * @membase: Base address of the I2C device
  105. * @adap: I2C adapter instance
  106. * @p_msg: Message pointer
  107. * @err_status: Error status in Interrupt Status Register
  108. * @xfer_done: Transfer complete status
  109. * @p_send_buf: Pointer to transmit buffer
  110. * @p_recv_buf: Pointer to receive buffer
  111. * @suspended: Flag holding the device's PM status
  112. * @send_count: Number of bytes still expected to send
  113. * @recv_count: Number of bytes still expected to receive
  114. * @irq: IRQ number
  115. * @input_clk: Input clock to I2C controller
  116. * @i2c_clk: Maximum I2C clock speed
  117. * @bus_hold_flag: Flag used in repeated start for clearing HOLD bit
  118. * @clk: Pointer to struct clk
  119. * @clk_rate_change_nb: Notifier block for clock rate changes
  120. */
  121. struct cdns_i2c {
  122. void __iomem *membase;
  123. struct i2c_adapter adap;
  124. struct i2c_msg *p_msg;
  125. int err_status;
  126. struct completion xfer_done;
  127. unsigned char *p_send_buf;
  128. unsigned char *p_recv_buf;
  129. u8 suspended;
  130. unsigned int send_count;
  131. unsigned int recv_count;
  132. int irq;
  133. unsigned long input_clk;
  134. unsigned int i2c_clk;
  135. unsigned int bus_hold_flag;
  136. struct clk *clk;
  137. struct notifier_block clk_rate_change_nb;
  138. };
  139. #define to_cdns_i2c(_nb) container_of(_nb, struct cdns_i2c, \
  140. clk_rate_change_nb)
  141. /**
  142. * cdns_i2c_clear_bus_hold() - Clear bus hold bit
  143. * @id: Pointer to driver data struct
  144. *
  145. * Helper to clear the controller's bus hold bit.
  146. */
  147. static void cdns_i2c_clear_bus_hold(struct cdns_i2c *id)
  148. {
  149. u32 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  150. if (reg & CDNS_I2C_CR_HOLD)
  151. cdns_i2c_writereg(reg & ~CDNS_I2C_CR_HOLD, CDNS_I2C_CR_OFFSET);
  152. }
  153. /**
  154. * cdns_i2c_isr - Interrupt handler for the I2C device
  155. * @irq: irq number for the I2C device
  156. * @ptr: void pointer to cdns_i2c structure
  157. *
  158. * This function handles the data interrupt, transfer complete interrupt and
  159. * the error interrupts of the I2C device.
  160. *
  161. * Return: IRQ_HANDLED always
  162. */
  163. static irqreturn_t cdns_i2c_isr(int irq, void *ptr)
  164. {
  165. unsigned int isr_status, avail_bytes;
  166. unsigned int bytes_to_recv, bytes_to_send;
  167. struct cdns_i2c *id = ptr;
  168. /* Signal completion only after everything is updated */
  169. int done_flag = 0;
  170. irqreturn_t status = IRQ_NONE;
  171. isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
  172. /* Handling nack and arbitration lost interrupt */
  173. if (isr_status & (CDNS_I2C_IXR_NACK | CDNS_I2C_IXR_ARB_LOST)) {
  174. done_flag = 1;
  175. status = IRQ_HANDLED;
  176. }
  177. /* Handling Data interrupt */
  178. if ((isr_status & CDNS_I2C_IXR_DATA) &&
  179. (id->recv_count >= CDNS_I2C_DATA_INTR_DEPTH)) {
  180. /* Always read data interrupt threshold bytes */
  181. bytes_to_recv = CDNS_I2C_DATA_INTR_DEPTH;
  182. id->recv_count -= CDNS_I2C_DATA_INTR_DEPTH;
  183. avail_bytes = cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
  184. /*
  185. * if the tranfer size register value is zero, then
  186. * check for the remaining bytes and update the
  187. * transfer size register.
  188. */
  189. if (!avail_bytes) {
  190. if (id->recv_count > CDNS_I2C_TRANSFER_SIZE)
  191. cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
  192. CDNS_I2C_XFER_SIZE_OFFSET);
  193. else
  194. cdns_i2c_writereg(id->recv_count,
  195. CDNS_I2C_XFER_SIZE_OFFSET);
  196. }
  197. /* Process the data received */
  198. while (bytes_to_recv--)
  199. *(id->p_recv_buf)++ =
  200. cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET);
  201. if (!id->bus_hold_flag &&
  202. (id->recv_count <= CDNS_I2C_FIFO_DEPTH))
  203. cdns_i2c_clear_bus_hold(id);
  204. status = IRQ_HANDLED;
  205. }
  206. /* Handling Transfer Complete interrupt */
  207. if (isr_status & CDNS_I2C_IXR_COMP) {
  208. if (!id->p_recv_buf) {
  209. /*
  210. * If the device is sending data If there is further
  211. * data to be sent. Calculate the available space
  212. * in FIFO and fill the FIFO with that many bytes.
  213. */
  214. if (id->send_count) {
  215. avail_bytes = CDNS_I2C_FIFO_DEPTH -
  216. cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
  217. if (id->send_count > avail_bytes)
  218. bytes_to_send = avail_bytes;
  219. else
  220. bytes_to_send = id->send_count;
  221. while (bytes_to_send--) {
  222. cdns_i2c_writereg(
  223. (*(id->p_send_buf)++),
  224. CDNS_I2C_DATA_OFFSET);
  225. id->send_count--;
  226. }
  227. } else {
  228. /*
  229. * Signal the completion of transaction and
  230. * clear the hold bus bit if there are no
  231. * further messages to be processed.
  232. */
  233. done_flag = 1;
  234. }
  235. if (!id->send_count && !id->bus_hold_flag)
  236. cdns_i2c_clear_bus_hold(id);
  237. } else {
  238. if (!id->bus_hold_flag)
  239. cdns_i2c_clear_bus_hold(id);
  240. /*
  241. * If the device is receiving data, then signal
  242. * the completion of transaction and read the data
  243. * present in the FIFO. Signal the completion of
  244. * transaction.
  245. */
  246. while (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) &
  247. CDNS_I2C_SR_RXDV) {
  248. *(id->p_recv_buf)++ =
  249. cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET);
  250. id->recv_count--;
  251. }
  252. done_flag = 1;
  253. }
  254. status = IRQ_HANDLED;
  255. }
  256. /* Update the status for errors */
  257. id->err_status = isr_status & CDNS_I2C_IXR_ERR_INTR_MASK;
  258. if (id->err_status)
  259. status = IRQ_HANDLED;
  260. cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
  261. if (done_flag)
  262. complete(&id->xfer_done);
  263. return status;
  264. }
  265. /**
  266. * cdns_i2c_mrecv - Prepare and start a master receive operation
  267. * @id: pointer to the i2c device structure
  268. */
  269. static void cdns_i2c_mrecv(struct cdns_i2c *id)
  270. {
  271. unsigned int ctrl_reg;
  272. unsigned int isr_status;
  273. id->p_recv_buf = id->p_msg->buf;
  274. id->recv_count = id->p_msg->len;
  275. /* Put the controller in master receive mode and clear the FIFO */
  276. ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  277. ctrl_reg |= CDNS_I2C_CR_RW | CDNS_I2C_CR_CLR_FIFO;
  278. if (id->p_msg->flags & I2C_M_RECV_LEN)
  279. id->recv_count = I2C_SMBUS_BLOCK_MAX + 1;
  280. /*
  281. * Check for the message size against FIFO depth and set the
  282. * 'hold bus' bit if it is greater than FIFO depth.
  283. */
  284. if (id->recv_count > CDNS_I2C_FIFO_DEPTH)
  285. ctrl_reg |= CDNS_I2C_CR_HOLD;
  286. cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
  287. /* Clear the interrupts in interrupt status register */
  288. isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
  289. cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
  290. /*
  291. * The no. of bytes to receive is checked against the limit of
  292. * max transfer size. Set transfer size register with no of bytes
  293. * receive if it is less than transfer size and transfer size if
  294. * it is more. Enable the interrupts.
  295. */
  296. if (id->recv_count > CDNS_I2C_TRANSFER_SIZE)
  297. cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
  298. CDNS_I2C_XFER_SIZE_OFFSET);
  299. else
  300. cdns_i2c_writereg(id->recv_count, CDNS_I2C_XFER_SIZE_OFFSET);
  301. /* Clear the bus hold flag if bytes to receive is less than FIFO size */
  302. if (!id->bus_hold_flag &&
  303. ((id->p_msg->flags & I2C_M_RECV_LEN) != I2C_M_RECV_LEN) &&
  304. (id->recv_count <= CDNS_I2C_FIFO_DEPTH))
  305. cdns_i2c_clear_bus_hold(id);
  306. /* Set the slave address in address register - triggers operation */
  307. cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
  308. CDNS_I2C_ADDR_OFFSET);
  309. cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
  310. }
  311. /**
  312. * cdns_i2c_msend - Prepare and start a master send operation
  313. * @id: pointer to the i2c device
  314. */
  315. static void cdns_i2c_msend(struct cdns_i2c *id)
  316. {
  317. unsigned int avail_bytes;
  318. unsigned int bytes_to_send;
  319. unsigned int ctrl_reg;
  320. unsigned int isr_status;
  321. id->p_recv_buf = NULL;
  322. id->p_send_buf = id->p_msg->buf;
  323. id->send_count = id->p_msg->len;
  324. /* Set the controller in Master transmit mode and clear the FIFO. */
  325. ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  326. ctrl_reg &= ~CDNS_I2C_CR_RW;
  327. ctrl_reg |= CDNS_I2C_CR_CLR_FIFO;
  328. /*
  329. * Check for the message size against FIFO depth and set the
  330. * 'hold bus' bit if it is greater than FIFO depth.
  331. */
  332. if (id->send_count > CDNS_I2C_FIFO_DEPTH)
  333. ctrl_reg |= CDNS_I2C_CR_HOLD;
  334. cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
  335. /* Clear the interrupts in interrupt status register. */
  336. isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
  337. cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
  338. /*
  339. * Calculate the space available in FIFO. Check the message length
  340. * against the space available, and fill the FIFO accordingly.
  341. * Enable the interrupts.
  342. */
  343. avail_bytes = CDNS_I2C_FIFO_DEPTH -
  344. cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
  345. if (id->send_count > avail_bytes)
  346. bytes_to_send = avail_bytes;
  347. else
  348. bytes_to_send = id->send_count;
  349. while (bytes_to_send--) {
  350. cdns_i2c_writereg((*(id->p_send_buf)++), CDNS_I2C_DATA_OFFSET);
  351. id->send_count--;
  352. }
  353. /*
  354. * Clear the bus hold flag if there is no more data
  355. * and if it is the last message.
  356. */
  357. if (!id->bus_hold_flag && !id->send_count)
  358. cdns_i2c_clear_bus_hold(id);
  359. /* Set the slave address in address register - triggers operation. */
  360. cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
  361. CDNS_I2C_ADDR_OFFSET);
  362. cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
  363. }
  364. /**
  365. * cdns_i2c_master_reset - Reset the interface
  366. * @adap: pointer to the i2c adapter driver instance
  367. *
  368. * This function cleanup the fifos, clear the hold bit and status
  369. * and disable the interrupts.
  370. */
  371. static void cdns_i2c_master_reset(struct i2c_adapter *adap)
  372. {
  373. struct cdns_i2c *id = adap->algo_data;
  374. u32 regval;
  375. /* Disable the interrupts */
  376. cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK, CDNS_I2C_IDR_OFFSET);
  377. /* Clear the hold bit and fifos */
  378. regval = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  379. regval &= ~CDNS_I2C_CR_HOLD;
  380. regval |= CDNS_I2C_CR_CLR_FIFO;
  381. cdns_i2c_writereg(regval, CDNS_I2C_CR_OFFSET);
  382. /* Update the transfercount register to zero */
  383. cdns_i2c_writereg(0, CDNS_I2C_XFER_SIZE_OFFSET);
  384. /* Clear the interupt status register */
  385. regval = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
  386. cdns_i2c_writereg(regval, CDNS_I2C_ISR_OFFSET);
  387. /* Clear the status register */
  388. regval = cdns_i2c_readreg(CDNS_I2C_SR_OFFSET);
  389. cdns_i2c_writereg(regval, CDNS_I2C_SR_OFFSET);
  390. }
  391. static int cdns_i2c_process_msg(struct cdns_i2c *id, struct i2c_msg *msg,
  392. struct i2c_adapter *adap)
  393. {
  394. int ret;
  395. u32 reg;
  396. id->p_msg = msg;
  397. id->err_status = 0;
  398. reinit_completion(&id->xfer_done);
  399. /* Check for the TEN Bit mode on each msg */
  400. reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  401. if (msg->flags & I2C_M_TEN) {
  402. if (reg & CDNS_I2C_CR_NEA)
  403. cdns_i2c_writereg(reg & ~CDNS_I2C_CR_NEA,
  404. CDNS_I2C_CR_OFFSET);
  405. } else {
  406. if (!(reg & CDNS_I2C_CR_NEA))
  407. cdns_i2c_writereg(reg | CDNS_I2C_CR_NEA,
  408. CDNS_I2C_CR_OFFSET);
  409. }
  410. /* Check for the R/W flag on each msg */
  411. if (msg->flags & I2C_M_RD)
  412. cdns_i2c_mrecv(id);
  413. else
  414. cdns_i2c_msend(id);
  415. /* Wait for the signal of completion */
  416. ret = wait_for_completion_timeout(&id->xfer_done, adap->timeout);
  417. if (!ret) {
  418. cdns_i2c_master_reset(adap);
  419. dev_err(id->adap.dev.parent,
  420. "timeout waiting on completion\n");
  421. return -ETIMEDOUT;
  422. }
  423. cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK,
  424. CDNS_I2C_IDR_OFFSET);
  425. /* If it is bus arbitration error, try again */
  426. if (id->err_status & CDNS_I2C_IXR_ARB_LOST)
  427. return -EAGAIN;
  428. return 0;
  429. }
  430. /**
  431. * cdns_i2c_master_xfer - The main i2c transfer function
  432. * @adap: pointer to the i2c adapter driver instance
  433. * @msgs: pointer to the i2c message structure
  434. * @num: the number of messages to transfer
  435. *
  436. * Initiates the send/recv activity based on the transfer message received.
  437. *
  438. * Return: number of msgs processed on success, negative error otherwise
  439. */
  440. static int cdns_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  441. int num)
  442. {
  443. int ret, count;
  444. u32 reg;
  445. struct cdns_i2c *id = adap->algo_data;
  446. /* Check if the bus is free */
  447. if (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) & CDNS_I2C_SR_BA)
  448. return -EAGAIN;
  449. /*
  450. * Set the flag to one when multiple messages are to be
  451. * processed with a repeated start.
  452. */
  453. if (num > 1) {
  454. id->bus_hold_flag = 1;
  455. reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  456. reg |= CDNS_I2C_CR_HOLD;
  457. cdns_i2c_writereg(reg, CDNS_I2C_CR_OFFSET);
  458. } else {
  459. id->bus_hold_flag = 0;
  460. }
  461. /* Process the msg one by one */
  462. for (count = 0; count < num; count++, msgs++) {
  463. if (count == (num - 1))
  464. id->bus_hold_flag = 0;
  465. ret = cdns_i2c_process_msg(id, msgs, adap);
  466. if (ret)
  467. return ret;
  468. /* Report the other error interrupts to application */
  469. if (id->err_status) {
  470. cdns_i2c_master_reset(adap);
  471. if (id->err_status & CDNS_I2C_IXR_NACK)
  472. return -ENXIO;
  473. return -EIO;
  474. }
  475. }
  476. return num;
  477. }
  478. /**
  479. * cdns_i2c_func - Returns the supported features of the I2C driver
  480. * @adap: pointer to the i2c adapter structure
  481. *
  482. * Return: 32 bit value, each bit corresponding to a feature
  483. */
  484. static u32 cdns_i2c_func(struct i2c_adapter *adap)
  485. {
  486. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
  487. (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
  488. I2C_FUNC_SMBUS_BLOCK_DATA;
  489. }
  490. static const struct i2c_algorithm cdns_i2c_algo = {
  491. .master_xfer = cdns_i2c_master_xfer,
  492. .functionality = cdns_i2c_func,
  493. };
  494. /**
  495. * cdns_i2c_calc_divs - Calculate clock dividers
  496. * @f: I2C clock frequency
  497. * @input_clk: Input clock frequency
  498. * @a: First divider (return value)
  499. * @b: Second divider (return value)
  500. *
  501. * f is used as input and output variable. As input it is used as target I2C
  502. * frequency. On function exit f holds the actually resulting I2C frequency.
  503. *
  504. * Return: 0 on success, negative errno otherwise.
  505. */
  506. static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
  507. unsigned int *a, unsigned int *b)
  508. {
  509. unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp;
  510. unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0;
  511. unsigned int last_error, current_error;
  512. /* calculate (divisor_a+1) x (divisor_b+1) */
  513. temp = input_clk / (22 * fscl);
  514. /*
  515. * If the calculated value is negative or 0, the fscl input is out of
  516. * range. Return error.
  517. */
  518. if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX)))
  519. return -EINVAL;
  520. last_error = -1;
  521. for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) {
  522. div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1));
  523. if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX))
  524. continue;
  525. div_b--;
  526. actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1));
  527. if (actual_fscl > fscl)
  528. continue;
  529. current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) :
  530. (fscl - actual_fscl));
  531. if (last_error > current_error) {
  532. calc_div_a = div_a;
  533. calc_div_b = div_b;
  534. best_fscl = actual_fscl;
  535. last_error = current_error;
  536. }
  537. }
  538. *a = calc_div_a;
  539. *b = calc_div_b;
  540. *f = best_fscl;
  541. return 0;
  542. }
  543. /**
  544. * cdns_i2c_setclk - This function sets the serial clock rate for the I2C device
  545. * @clk_in: I2C clock input frequency in Hz
  546. * @id: Pointer to the I2C device structure
  547. *
  548. * The device must be idle rather than busy transferring data before setting
  549. * these device options.
  550. * The data rate is set by values in the control register.
  551. * The formula for determining the correct register values is
  552. * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
  553. * See the hardware data sheet for a full explanation of setting the serial
  554. * clock rate. The clock can not be faster than the input clock divide by 22.
  555. * The two most common clock rates are 100KHz and 400KHz.
  556. *
  557. * Return: 0 on success, negative error otherwise
  558. */
  559. static int cdns_i2c_setclk(unsigned long clk_in, struct cdns_i2c *id)
  560. {
  561. unsigned int div_a, div_b;
  562. unsigned int ctrl_reg;
  563. int ret = 0;
  564. unsigned long fscl = id->i2c_clk;
  565. ret = cdns_i2c_calc_divs(&fscl, clk_in, &div_a, &div_b);
  566. if (ret)
  567. return ret;
  568. ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  569. ctrl_reg &= ~(CDNS_I2C_CR_DIVA_MASK | CDNS_I2C_CR_DIVB_MASK);
  570. ctrl_reg |= ((div_a << CDNS_I2C_CR_DIVA_SHIFT) |
  571. (div_b << CDNS_I2C_CR_DIVB_SHIFT));
  572. cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
  573. return 0;
  574. }
  575. /**
  576. * cdns_i2c_clk_notifier_cb - Clock rate change callback
  577. * @nb: Pointer to notifier block
  578. * @event: Notification reason
  579. * @data: Pointer to notification data object
  580. *
  581. * This function is called when the cdns_i2c input clock frequency changes.
  582. * The callback checks whether a valid bus frequency can be generated after the
  583. * change. If so, the change is acknowledged, otherwise the change is aborted.
  584. * New dividers are written to the HW in the pre- or post change notification
  585. * depending on the scaling direction.
  586. *
  587. * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
  588. * to acknowedge the change, NOTIFY_DONE if the notification is
  589. * considered irrelevant.
  590. */
  591. static int cdns_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
  592. event, void *data)
  593. {
  594. struct clk_notifier_data *ndata = data;
  595. struct cdns_i2c *id = to_cdns_i2c(nb);
  596. if (id->suspended)
  597. return NOTIFY_OK;
  598. switch (event) {
  599. case PRE_RATE_CHANGE:
  600. {
  601. unsigned long input_clk = ndata->new_rate;
  602. unsigned long fscl = id->i2c_clk;
  603. unsigned int div_a, div_b;
  604. int ret;
  605. ret = cdns_i2c_calc_divs(&fscl, input_clk, &div_a, &div_b);
  606. if (ret) {
  607. dev_warn(id->adap.dev.parent,
  608. "clock rate change rejected\n");
  609. return NOTIFY_STOP;
  610. }
  611. /* scale up */
  612. if (ndata->new_rate > ndata->old_rate)
  613. cdns_i2c_setclk(ndata->new_rate, id);
  614. return NOTIFY_OK;
  615. }
  616. case POST_RATE_CHANGE:
  617. id->input_clk = ndata->new_rate;
  618. /* scale down */
  619. if (ndata->new_rate < ndata->old_rate)
  620. cdns_i2c_setclk(ndata->new_rate, id);
  621. return NOTIFY_OK;
  622. case ABORT_RATE_CHANGE:
  623. /* scale up */
  624. if (ndata->new_rate > ndata->old_rate)
  625. cdns_i2c_setclk(ndata->old_rate, id);
  626. return NOTIFY_OK;
  627. default:
  628. return NOTIFY_DONE;
  629. }
  630. }
  631. /**
  632. * cdns_i2c_suspend - Suspend method for the driver
  633. * @_dev: Address of the platform_device structure
  634. *
  635. * Put the driver into low power mode.
  636. *
  637. * Return: 0 always
  638. */
  639. static int __maybe_unused cdns_i2c_suspend(struct device *_dev)
  640. {
  641. struct platform_device *pdev = container_of(_dev,
  642. struct platform_device, dev);
  643. struct cdns_i2c *xi2c = platform_get_drvdata(pdev);
  644. clk_disable(xi2c->clk);
  645. xi2c->suspended = 1;
  646. return 0;
  647. }
  648. /**
  649. * cdns_i2c_resume - Resume from suspend
  650. * @_dev: Address of the platform_device structure
  651. *
  652. * Resume operation after suspend.
  653. *
  654. * Return: 0 on success and error value on error
  655. */
  656. static int __maybe_unused cdns_i2c_resume(struct device *_dev)
  657. {
  658. struct platform_device *pdev = container_of(_dev,
  659. struct platform_device, dev);
  660. struct cdns_i2c *xi2c = platform_get_drvdata(pdev);
  661. int ret;
  662. ret = clk_enable(xi2c->clk);
  663. if (ret) {
  664. dev_err(_dev, "Cannot enable clock.\n");
  665. return ret;
  666. }
  667. xi2c->suspended = 0;
  668. return 0;
  669. }
  670. static SIMPLE_DEV_PM_OPS(cdns_i2c_dev_pm_ops, cdns_i2c_suspend,
  671. cdns_i2c_resume);
  672. /**
  673. * cdns_i2c_probe - Platform registration call
  674. * @pdev: Handle to the platform device structure
  675. *
  676. * This function does all the memory allocation and registration for the i2c
  677. * device. User can modify the address mode to 10 bit address mode using the
  678. * ioctl call with option I2C_TENBIT.
  679. *
  680. * Return: 0 on success, negative error otherwise
  681. */
  682. static int cdns_i2c_probe(struct platform_device *pdev)
  683. {
  684. struct resource *r_mem;
  685. struct cdns_i2c *id;
  686. int ret;
  687. id = devm_kzalloc(&pdev->dev, sizeof(*id), GFP_KERNEL);
  688. if (!id)
  689. return -ENOMEM;
  690. platform_set_drvdata(pdev, id);
  691. r_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  692. id->membase = devm_ioremap_resource(&pdev->dev, r_mem);
  693. if (IS_ERR(id->membase))
  694. return PTR_ERR(id->membase);
  695. id->irq = platform_get_irq(pdev, 0);
  696. id->adap.dev.of_node = pdev->dev.of_node;
  697. id->adap.algo = &cdns_i2c_algo;
  698. id->adap.timeout = CDNS_I2C_TIMEOUT;
  699. id->adap.retries = 3; /* Default retry value. */
  700. id->adap.algo_data = id;
  701. id->adap.dev.parent = &pdev->dev;
  702. init_completion(&id->xfer_done);
  703. snprintf(id->adap.name, sizeof(id->adap.name),
  704. "Cadence I2C at %08lx", (unsigned long)r_mem->start);
  705. id->clk = devm_clk_get(&pdev->dev, NULL);
  706. if (IS_ERR(id->clk)) {
  707. dev_err(&pdev->dev, "input clock not found.\n");
  708. return PTR_ERR(id->clk);
  709. }
  710. ret = clk_prepare_enable(id->clk);
  711. if (ret) {
  712. dev_err(&pdev->dev, "Unable to enable clock.\n");
  713. return ret;
  714. }
  715. id->clk_rate_change_nb.notifier_call = cdns_i2c_clk_notifier_cb;
  716. if (clk_notifier_register(id->clk, &id->clk_rate_change_nb))
  717. dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
  718. id->input_clk = clk_get_rate(id->clk);
  719. ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  720. &id->i2c_clk);
  721. if (ret || (id->i2c_clk > CDNS_I2C_SPEED_MAX))
  722. id->i2c_clk = CDNS_I2C_SPEED_DEFAULT;
  723. cdns_i2c_writereg(CDNS_I2C_CR_ACK_EN | CDNS_I2C_CR_NEA | CDNS_I2C_CR_MS,
  724. CDNS_I2C_CR_OFFSET);
  725. ret = cdns_i2c_setclk(id->input_clk, id);
  726. if (ret) {
  727. dev_err(&pdev->dev, "invalid SCL clock: %u Hz\n", id->i2c_clk);
  728. ret = -EINVAL;
  729. goto err_clk_dis;
  730. }
  731. ret = devm_request_irq(&pdev->dev, id->irq, cdns_i2c_isr, 0,
  732. DRIVER_NAME, id);
  733. if (ret) {
  734. dev_err(&pdev->dev, "cannot get irq %d\n", id->irq);
  735. goto err_clk_dis;
  736. }
  737. ret = i2c_add_adapter(&id->adap);
  738. if (ret < 0) {
  739. dev_err(&pdev->dev, "reg adap failed: %d\n", ret);
  740. goto err_clk_dis;
  741. }
  742. /*
  743. * Cadence I2C controller has a bug wherein it generates
  744. * invalid read transaction after HW timeout in master receiver mode.
  745. * HW timeout is not used by this driver and the interrupt is disabled.
  746. * But the feature itself cannot be disabled. Hence maximum value
  747. * is written to this register to reduce the chances of error.
  748. */
  749. cdns_i2c_writereg(CDNS_I2C_TIMEOUT_MAX, CDNS_I2C_TIME_OUT_OFFSET);
  750. dev_info(&pdev->dev, "%u kHz mmio %08lx irq %d\n",
  751. id->i2c_clk / 1000, (unsigned long)r_mem->start, id->irq);
  752. return 0;
  753. err_clk_dis:
  754. clk_disable_unprepare(id->clk);
  755. return ret;
  756. }
  757. /**
  758. * cdns_i2c_remove - Unregister the device after releasing the resources
  759. * @pdev: Handle to the platform device structure
  760. *
  761. * This function frees all the resources allocated to the device.
  762. *
  763. * Return: 0 always
  764. */
  765. static int cdns_i2c_remove(struct platform_device *pdev)
  766. {
  767. struct cdns_i2c *id = platform_get_drvdata(pdev);
  768. i2c_del_adapter(&id->adap);
  769. clk_notifier_unregister(id->clk, &id->clk_rate_change_nb);
  770. clk_disable_unprepare(id->clk);
  771. return 0;
  772. }
  773. static const struct of_device_id cdns_i2c_of_match[] = {
  774. { .compatible = "cdns,i2c-r1p10", },
  775. { /* end of table */ }
  776. };
  777. MODULE_DEVICE_TABLE(of, cdns_i2c_of_match);
  778. static struct platform_driver cdns_i2c_drv = {
  779. .driver = {
  780. .name = DRIVER_NAME,
  781. .of_match_table = cdns_i2c_of_match,
  782. .pm = &cdns_i2c_dev_pm_ops,
  783. },
  784. .probe = cdns_i2c_probe,
  785. .remove = cdns_i2c_remove,
  786. };
  787. module_platform_driver(cdns_i2c_drv);
  788. MODULE_AUTHOR("Xilinx Inc.");
  789. MODULE_DESCRIPTION("Cadence I2C bus driver");
  790. MODULE_LICENSE("GPL");