1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * I2C bus driver for the Cadence I2C controller.
5 * Copyright (C) 2009 - 2014 Xilinx, Inc.
9 #include <linux/delay.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
18 /* Register offsets for the I2C device. */
19 #define CDNS_I2C_CR_OFFSET 0x00 /* Control Register, RW */
20 #define CDNS_I2C_SR_OFFSET 0x04 /* Status Register, RO */
21 #define CDNS_I2C_ADDR_OFFSET 0x08 /* I2C Address Register, RW */
22 #define CDNS_I2C_DATA_OFFSET 0x0C /* I2C Data Register, RW */
23 #define CDNS_I2C_ISR_OFFSET 0x10 /* IRQ Status Register, RW */
24 #define CDNS_I2C_XFER_SIZE_OFFSET 0x14 /* Transfer Size Register, RW */
25 #define CDNS_I2C_TIME_OUT_OFFSET 0x1C /* Time Out Register, RW */
26 #define CDNS_I2C_IER_OFFSET 0x24 /* IRQ Enable Register, WO */
27 #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)
43 /* Status Register Bit mask definitions */
44 #define CDNS_I2C_SR_BA BIT(8)
45 #define CDNS_I2C_SR_RXDV BIT(5)
48 * I2C Address Register Bit mask definitions
49 * Normal addressing mode uses [6:0] bits. Extended addressing mode uses [9:0]
50 * bits. A write access to this register always initiates a transfer if the I2C
53 #define CDNS_I2C_ADDR_MASK 0x000003FF /* I2C Address Mask */
56 * I2C Interrupt Registers Bit mask definitions
57 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
60 #define CDNS_I2C_IXR_ARB_LOST BIT(9)
61 #define CDNS_I2C_IXR_RX_UNF BIT(7)
62 #define CDNS_I2C_IXR_TX_OVF BIT(6)
63 #define CDNS_I2C_IXR_RX_OVF BIT(5)
64 #define CDNS_I2C_IXR_SLV_RDY BIT(4)
65 #define CDNS_I2C_IXR_TO BIT(3)
66 #define CDNS_I2C_IXR_NACK BIT(2)
67 #define CDNS_I2C_IXR_DATA BIT(1)
68 #define CDNS_I2C_IXR_COMP BIT(0)
70 #define CDNS_I2C_IXR_ALL_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
71 CDNS_I2C_IXR_RX_UNF | \
72 CDNS_I2C_IXR_TX_OVF | \
73 CDNS_I2C_IXR_RX_OVF | \
74 CDNS_I2C_IXR_SLV_RDY | \
80 #define CDNS_I2C_IXR_ERR_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 | \
86 #define CDNS_I2C_ENABLED_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
87 CDNS_I2C_IXR_RX_UNF | \
88 CDNS_I2C_IXR_TX_OVF | \
89 CDNS_I2C_IXR_RX_OVF | \
94 #define CDNS_I2C_TIMEOUT msecs_to_jiffies(1000)
95 /* timeout for pm runtime autosuspend */
96 #define CNDS_I2C_PM_TIMEOUT 1000 /* ms */
98 #define CDNS_I2C_FIFO_DEPTH 16
99 /* FIFO depth at which the DATA interrupt occurs */
100 #define CDNS_I2C_DATA_INTR_DEPTH (CDNS_I2C_FIFO_DEPTH - 2)
101 #define CDNS_I2C_MAX_TRANSFER_SIZE 255
102 /* Transfer size in multiples of data interrupt depth */
103 #define CDNS_I2C_TRANSFER_SIZE (CDNS_I2C_MAX_TRANSFER_SIZE - 3)
105 #define DRIVER_NAME "cdns-i2c"
107 #define CDNS_I2C_SPEED_MAX 400000
108 #define CDNS_I2C_SPEED_DEFAULT 100000
110 #define CDNS_I2C_DIVA_MAX 4
111 #define CDNS_I2C_DIVB_MAX 64
113 #define CDNS_I2C_TIMEOUT_MAX 0xFF
115 #define CDNS_I2C_BROKEN_HOLD_BIT BIT(0)
117 #define cdns_i2c_readreg(offset) readl_relaxed(id->membase + offset)
118 #define cdns_i2c_writereg(val, offset) writel_relaxed(val, id->membase + offset)
121 * struct cdns_i2c - I2C device private data structure
123 * @dev: Pointer to device structure
124 * @membase: Base address of the I2C device
125 * @adap: I2C adapter instance
126 * @p_msg: Message pointer
127 * @err_status: Error status in Interrupt Status Register
128 * @xfer_done: Transfer complete status
129 * @p_send_buf: Pointer to transmit buffer
130 * @p_recv_buf: Pointer to receive buffer
131 * @send_count: Number of bytes still expected to send
132 * @recv_count: Number of bytes still expected to receive
133 * @curr_recv_count: Number of bytes to be received in current transfer
135 * @input_clk: Input clock to I2C controller
136 * @i2c_clk: Maximum I2C clock speed
137 * @bus_hold_flag: Flag used in repeated start for clearing HOLD bit
138 * @clk: Pointer to struct clk
139 * @clk_rate_change_nb: Notifier block for clock rate changes
140 * @quirks: flag for broken hold bit usage in r1p10
144 void __iomem *membase;
145 struct i2c_adapter adap;
146 struct i2c_msg *p_msg;
148 struct completion xfer_done;
149 unsigned char *p_send_buf;
150 unsigned char *p_recv_buf;
151 unsigned int send_count;
152 unsigned int recv_count;
153 unsigned int curr_recv_count;
155 unsigned long input_clk;
156 unsigned int i2c_clk;
157 unsigned int bus_hold_flag;
159 struct notifier_block clk_rate_change_nb;
163 struct cdns_platform_data {
167 #define to_cdns_i2c(_nb) container_of(_nb, struct cdns_i2c, \
171 * cdns_i2c_clear_bus_hold - Clear bus hold bit
172 * @id: Pointer to driver data struct
174 * Helper to clear the controller's bus hold bit.
176 static void cdns_i2c_clear_bus_hold(struct cdns_i2c *id)
178 u32 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
179 if (reg & CDNS_I2C_CR_HOLD)
180 cdns_i2c_writereg(reg & ~CDNS_I2C_CR_HOLD, CDNS_I2C_CR_OFFSET);
183 static inline bool cdns_is_holdquirk(struct cdns_i2c *id, bool hold_wrkaround)
185 return (hold_wrkaround &&
186 (id->curr_recv_count == CDNS_I2C_FIFO_DEPTH + 1));
190 * cdns_i2c_isr - Interrupt handler for the I2C device
191 * @irq: irq number for the I2C device
192 * @ptr: void pointer to cdns_i2c structure
194 * This function handles the data interrupt, transfer complete interrupt and
195 * the error interrupts of the I2C device.
197 * Return: IRQ_HANDLED always
199 static irqreturn_t cdns_i2c_isr(int irq, void *ptr)
201 unsigned int isr_status, avail_bytes, updatetx;
202 unsigned int bytes_to_send;
204 struct cdns_i2c *id = ptr;
205 /* Signal completion only after everything is updated */
207 irqreturn_t status = IRQ_NONE;
209 isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
210 cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
213 /* Handling nack and arbitration lost interrupt */
214 if (isr_status & (CDNS_I2C_IXR_NACK | CDNS_I2C_IXR_ARB_LOST)) {
216 status = IRQ_HANDLED;
220 * Check if transfer size register needs to be updated again for a
221 * large data receive operation.
224 if (id->recv_count > id->curr_recv_count)
227 hold_quirk = (id->quirks & CDNS_I2C_BROKEN_HOLD_BIT) && updatetx;
229 /* When receiving, handle data interrupt and completion interrupt */
230 if (id->p_recv_buf &&
231 ((isr_status & CDNS_I2C_IXR_COMP) ||
232 (isr_status & CDNS_I2C_IXR_DATA))) {
233 /* Read data if receive data valid is set */
234 while (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) &
237 * Clear hold bit that was set for FIFO control if
238 * RX data left is less than FIFO depth, unless
239 * repeated start is selected.
241 if ((id->recv_count < CDNS_I2C_FIFO_DEPTH) &&
243 cdns_i2c_clear_bus_hold(id);
245 if (id->recv_count > 0) {
246 *(id->p_recv_buf)++ =
247 cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET);
249 id->curr_recv_count--;
251 dev_err(id->adap.dev.parent,
252 "xfer_size reg rollover. xfer aborted!\n");
253 id->err_status |= CDNS_I2C_IXR_TO;
257 if (cdns_is_holdquirk(id, hold_quirk))
262 * The controller sends NACK to the slave when transfer size
263 * register reaches zero without considering the HOLD bit.
264 * This workaround is implemented for large data transfers to
265 * maintain transfer size non-zero while performing a large
268 if (cdns_is_holdquirk(id, hold_quirk)) {
269 /* wait while fifo is full */
270 while (cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET) !=
271 (id->curr_recv_count - CDNS_I2C_FIFO_DEPTH))
275 * Check number of bytes to be received against maximum
276 * transfer size and update register accordingly.
278 if (((int)(id->recv_count) - CDNS_I2C_FIFO_DEPTH) >
279 CDNS_I2C_TRANSFER_SIZE) {
280 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
281 CDNS_I2C_XFER_SIZE_OFFSET);
282 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE +
285 cdns_i2c_writereg(id->recv_count -
287 CDNS_I2C_XFER_SIZE_OFFSET);
288 id->curr_recv_count = id->recv_count;
290 } else if (id->recv_count && !hold_quirk &&
291 !id->curr_recv_count) {
293 /* Set the slave address in address register*/
294 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
295 CDNS_I2C_ADDR_OFFSET);
297 if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
298 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
299 CDNS_I2C_XFER_SIZE_OFFSET);
300 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
302 cdns_i2c_writereg(id->recv_count,
303 CDNS_I2C_XFER_SIZE_OFFSET);
304 id->curr_recv_count = id->recv_count;
308 /* Clear hold (if not repeated start) and signal completion */
309 if ((isr_status & CDNS_I2C_IXR_COMP) && !id->recv_count) {
310 if (!id->bus_hold_flag)
311 cdns_i2c_clear_bus_hold(id);
315 status = IRQ_HANDLED;
318 /* When sending, handle transfer complete interrupt */
319 if ((isr_status & CDNS_I2C_IXR_COMP) && !id->p_recv_buf) {
321 * If there is more data to be sent, calculate the
322 * space available in FIFO and fill with that many bytes.
324 if (id->send_count) {
325 avail_bytes = CDNS_I2C_FIFO_DEPTH -
326 cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
327 if (id->send_count > avail_bytes)
328 bytes_to_send = avail_bytes;
330 bytes_to_send = id->send_count;
332 while (bytes_to_send--) {
334 (*(id->p_send_buf)++),
335 CDNS_I2C_DATA_OFFSET);
340 * Signal the completion of transaction and
341 * clear the hold bus bit if there are no
342 * further messages to be processed.
346 if (!id->send_count && !id->bus_hold_flag)
347 cdns_i2c_clear_bus_hold(id);
349 status = IRQ_HANDLED;
352 /* Update the status for errors */
353 id->err_status |= isr_status & CDNS_I2C_IXR_ERR_INTR_MASK;
355 status = IRQ_HANDLED;
358 complete(&id->xfer_done);
364 * cdns_i2c_mrecv - Prepare and start a master receive operation
365 * @id: pointer to the i2c device structure
367 static void cdns_i2c_mrecv(struct cdns_i2c *id)
369 unsigned int ctrl_reg;
370 unsigned int isr_status;
372 id->p_recv_buf = id->p_msg->buf;
373 id->recv_count = id->p_msg->len;
375 /* Put the controller in master receive mode and clear the FIFO */
376 ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
377 ctrl_reg |= CDNS_I2C_CR_RW | CDNS_I2C_CR_CLR_FIFO;
379 if (id->p_msg->flags & I2C_M_RECV_LEN)
380 id->recv_count = I2C_SMBUS_BLOCK_MAX + 1;
382 id->curr_recv_count = id->recv_count;
385 * Check for the message size against FIFO depth and set the
386 * 'hold bus' bit if it is greater than FIFO depth.
388 if ((id->recv_count > CDNS_I2C_FIFO_DEPTH) || id->bus_hold_flag)
389 ctrl_reg |= CDNS_I2C_CR_HOLD;
391 ctrl_reg = ctrl_reg & ~CDNS_I2C_CR_HOLD;
393 cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
395 /* Clear the interrupts in interrupt status register */
396 isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
397 cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
400 * The no. of bytes to receive is checked against the limit of
401 * max transfer size. Set transfer size register with no of bytes
402 * receive if it is less than transfer size and transfer size if
403 * it is more. Enable the interrupts.
405 if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
406 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
407 CDNS_I2C_XFER_SIZE_OFFSET);
408 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
410 cdns_i2c_writereg(id->recv_count, CDNS_I2C_XFER_SIZE_OFFSET);
413 /* Set the slave address in address register - triggers operation */
414 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
415 CDNS_I2C_ADDR_OFFSET);
416 /* Clear the bus hold flag if bytes to receive is less than FIFO size */
417 if (!id->bus_hold_flag &&
418 ((id->p_msg->flags & I2C_M_RECV_LEN) != I2C_M_RECV_LEN) &&
419 (id->recv_count <= CDNS_I2C_FIFO_DEPTH))
420 cdns_i2c_clear_bus_hold(id);
421 cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
425 * cdns_i2c_msend - Prepare and start a master send operation
426 * @id: pointer to the i2c device
428 static void cdns_i2c_msend(struct cdns_i2c *id)
430 unsigned int avail_bytes;
431 unsigned int bytes_to_send;
432 unsigned int ctrl_reg;
433 unsigned int isr_status;
435 id->p_recv_buf = NULL;
436 id->p_send_buf = id->p_msg->buf;
437 id->send_count = id->p_msg->len;
439 /* Set the controller in Master transmit mode and clear the FIFO. */
440 ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
441 ctrl_reg &= ~CDNS_I2C_CR_RW;
442 ctrl_reg |= CDNS_I2C_CR_CLR_FIFO;
445 * Check for the message size against FIFO depth and set the
446 * 'hold bus' bit if it is greater than FIFO depth.
448 if ((id->send_count > CDNS_I2C_FIFO_DEPTH) || id->bus_hold_flag)
449 ctrl_reg |= CDNS_I2C_CR_HOLD;
451 ctrl_reg = ctrl_reg & ~CDNS_I2C_CR_HOLD;
453 cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
455 /* Clear the interrupts in interrupt status register. */
456 isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
457 cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
460 * Calculate the space available in FIFO. Check the message length
461 * against the space available, and fill the FIFO accordingly.
462 * Enable the interrupts.
464 avail_bytes = CDNS_I2C_FIFO_DEPTH -
465 cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
467 if (id->send_count > avail_bytes)
468 bytes_to_send = avail_bytes;
470 bytes_to_send = id->send_count;
472 while (bytes_to_send--) {
473 cdns_i2c_writereg((*(id->p_send_buf)++), CDNS_I2C_DATA_OFFSET);
478 * Clear the bus hold flag if there is no more data
479 * and if it is the last message.
481 if (!id->bus_hold_flag && !id->send_count)
482 cdns_i2c_clear_bus_hold(id);
483 /* Set the slave address in address register - triggers operation. */
484 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
485 CDNS_I2C_ADDR_OFFSET);
487 cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
491 * cdns_i2c_master_reset - Reset the interface
492 * @adap: pointer to the i2c adapter driver instance
494 * This function cleanup the fifos, clear the hold bit and status
495 * and disable the interrupts.
497 static void cdns_i2c_master_reset(struct i2c_adapter *adap)
499 struct cdns_i2c *id = adap->algo_data;
502 /* Disable the interrupts */
503 cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK, CDNS_I2C_IDR_OFFSET);
504 /* Clear the hold bit and fifos */
505 regval = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
506 regval &= ~CDNS_I2C_CR_HOLD;
507 regval |= CDNS_I2C_CR_CLR_FIFO;
508 cdns_i2c_writereg(regval, CDNS_I2C_CR_OFFSET);
509 /* Update the transfercount register to zero */
510 cdns_i2c_writereg(0, CDNS_I2C_XFER_SIZE_OFFSET);
511 /* Clear the interrupt status register */
512 regval = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
513 cdns_i2c_writereg(regval, CDNS_I2C_ISR_OFFSET);
514 /* Clear the status register */
515 regval = cdns_i2c_readreg(CDNS_I2C_SR_OFFSET);
516 cdns_i2c_writereg(regval, CDNS_I2C_SR_OFFSET);
519 static int cdns_i2c_process_msg(struct cdns_i2c *id, struct i2c_msg *msg,
520 struct i2c_adapter *adap)
522 unsigned long time_left;
527 reinit_completion(&id->xfer_done);
529 /* Check for the TEN Bit mode on each msg */
530 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
531 if (msg->flags & I2C_M_TEN) {
532 if (reg & CDNS_I2C_CR_NEA)
533 cdns_i2c_writereg(reg & ~CDNS_I2C_CR_NEA,
536 if (!(reg & CDNS_I2C_CR_NEA))
537 cdns_i2c_writereg(reg | CDNS_I2C_CR_NEA,
541 /* Check for the R/W flag on each msg */
542 if (msg->flags & I2C_M_RD)
547 /* Wait for the signal of completion */
548 time_left = wait_for_completion_timeout(&id->xfer_done, adap->timeout);
549 if (time_left == 0) {
550 cdns_i2c_master_reset(adap);
551 dev_err(id->adap.dev.parent,
552 "timeout waiting on completion\n");
556 cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK,
557 CDNS_I2C_IDR_OFFSET);
559 /* If it is bus arbitration error, try again */
560 if (id->err_status & CDNS_I2C_IXR_ARB_LOST)
567 * cdns_i2c_master_xfer - The main i2c transfer function
568 * @adap: pointer to the i2c adapter driver instance
569 * @msgs: pointer to the i2c message structure
570 * @num: the number of messages to transfer
572 * Initiates the send/recv activity based on the transfer message received.
574 * Return: number of msgs processed on success, negative error otherwise
576 static int cdns_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
581 struct cdns_i2c *id = adap->algo_data;
584 ret = pm_runtime_get_sync(id->dev);
587 /* Check if the bus is free */
588 if (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) & CDNS_I2C_SR_BA) {
593 hold_quirk = !!(id->quirks & CDNS_I2C_BROKEN_HOLD_BIT);
595 * Set the flag to one when multiple messages are to be
596 * processed with a repeated start.
600 * This controller does not give completion interrupt after a
601 * master receive message if HOLD bit is set (repeated start),
602 * resulting in SW timeout. Hence, if a receive message is
603 * followed by any other message, an error is returned
604 * indicating that this sequence is not supported.
606 for (count = 0; (count < num - 1 && hold_quirk); count++) {
607 if (msgs[count].flags & I2C_M_RD) {
608 dev_warn(adap->dev.parent,
609 "Can't do repeated start after a receive message\n");
614 id->bus_hold_flag = 1;
615 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
616 reg |= CDNS_I2C_CR_HOLD;
617 cdns_i2c_writereg(reg, CDNS_I2C_CR_OFFSET);
619 id->bus_hold_flag = 0;
622 /* Process the msg one by one */
623 for (count = 0; count < num; count++, msgs++) {
624 if (count == (num - 1))
625 id->bus_hold_flag = 0;
627 ret = cdns_i2c_process_msg(id, msgs, adap);
631 /* Report the other error interrupts to application */
632 if (id->err_status) {
633 cdns_i2c_master_reset(adap);
635 if (id->err_status & CDNS_I2C_IXR_NACK) {
646 pm_runtime_mark_last_busy(id->dev);
647 pm_runtime_put_autosuspend(id->dev);
652 * cdns_i2c_func - Returns the supported features of the I2C driver
653 * @adap: pointer to the i2c adapter structure
655 * Return: 32 bit value, each bit corresponding to a feature
657 static u32 cdns_i2c_func(struct i2c_adapter *adap)
659 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
660 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
661 I2C_FUNC_SMBUS_BLOCK_DATA;
664 static const struct i2c_algorithm cdns_i2c_algo = {
665 .master_xfer = cdns_i2c_master_xfer,
666 .functionality = cdns_i2c_func,
670 * cdns_i2c_calc_divs - Calculate clock dividers
671 * @f: I2C clock frequency
672 * @input_clk: Input clock frequency
673 * @a: First divider (return value)
674 * @b: Second divider (return value)
676 * f is used as input and output variable. As input it is used as target I2C
677 * frequency. On function exit f holds the actually resulting I2C frequency.
679 * Return: 0 on success, negative errno otherwise.
681 static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
682 unsigned int *a, unsigned int *b)
684 unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp;
685 unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0;
686 unsigned int last_error, current_error;
688 /* calculate (divisor_a+1) x (divisor_b+1) */
689 temp = input_clk / (22 * fscl);
692 * If the calculated value is negative or 0, the fscl input is out of
693 * range. Return error.
695 if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX)))
699 for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) {
700 div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1));
702 if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX))
706 actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1));
708 if (actual_fscl > fscl)
711 current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) :
712 (fscl - actual_fscl));
714 if (last_error > current_error) {
717 best_fscl = actual_fscl;
718 last_error = current_error;
730 * cdns_i2c_setclk - This function sets the serial clock rate for the I2C device
731 * @clk_in: I2C clock input frequency in Hz
732 * @id: Pointer to the I2C device structure
734 * The device must be idle rather than busy transferring data before setting
735 * these device options.
736 * The data rate is set by values in the control register.
737 * The formula for determining the correct register values is
738 * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
739 * See the hardware data sheet for a full explanation of setting the serial
740 * clock rate. The clock can not be faster than the input clock divide by 22.
741 * The two most common clock rates are 100KHz and 400KHz.
743 * Return: 0 on success, negative error otherwise
745 static int cdns_i2c_setclk(unsigned long clk_in, struct cdns_i2c *id)
747 unsigned int div_a, div_b;
748 unsigned int ctrl_reg;
750 unsigned long fscl = id->i2c_clk;
752 ret = cdns_i2c_calc_divs(&fscl, clk_in, &div_a, &div_b);
756 ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
757 ctrl_reg &= ~(CDNS_I2C_CR_DIVA_MASK | CDNS_I2C_CR_DIVB_MASK);
758 ctrl_reg |= ((div_a << CDNS_I2C_CR_DIVA_SHIFT) |
759 (div_b << CDNS_I2C_CR_DIVB_SHIFT));
760 cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
766 * cdns_i2c_clk_notifier_cb - Clock rate change callback
767 * @nb: Pointer to notifier block
768 * @event: Notification reason
769 * @data: Pointer to notification data object
771 * This function is called when the cdns_i2c input clock frequency changes.
772 * The callback checks whether a valid bus frequency can be generated after the
773 * change. If so, the change is acknowledged, otherwise the change is aborted.
774 * New dividers are written to the HW in the pre- or post change notification
775 * depending on the scaling direction.
777 * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
778 * to acknowledge the change, NOTIFY_DONE if the notification is
779 * considered irrelevant.
781 static int cdns_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
784 struct clk_notifier_data *ndata = data;
785 struct cdns_i2c *id = to_cdns_i2c(nb);
787 if (pm_runtime_suspended(id->dev))
791 case PRE_RATE_CHANGE:
793 unsigned long input_clk = ndata->new_rate;
794 unsigned long fscl = id->i2c_clk;
795 unsigned int div_a, div_b;
798 ret = cdns_i2c_calc_divs(&fscl, input_clk, &div_a, &div_b);
800 dev_warn(id->adap.dev.parent,
801 "clock rate change rejected\n");
806 if (ndata->new_rate > ndata->old_rate)
807 cdns_i2c_setclk(ndata->new_rate, id);
811 case POST_RATE_CHANGE:
812 id->input_clk = ndata->new_rate;
814 if (ndata->new_rate < ndata->old_rate)
815 cdns_i2c_setclk(ndata->new_rate, id);
817 case ABORT_RATE_CHANGE:
819 if (ndata->new_rate > ndata->old_rate)
820 cdns_i2c_setclk(ndata->old_rate, id);
828 * cdns_i2c_runtime_suspend - Runtime suspend method for the driver
829 * @dev: Address of the platform_device structure
831 * Put the driver into low power mode.
835 static int __maybe_unused cdns_i2c_runtime_suspend(struct device *dev)
837 struct cdns_i2c *xi2c = dev_get_drvdata(dev);
839 clk_disable(xi2c->clk);
845 * cdns_i2c_runtime_resume - Runtime resume
846 * @dev: Address of the platform_device structure
848 * Runtime resume callback.
850 * Return: 0 on success and error value on error
852 static int __maybe_unused cdns_i2c_runtime_resume(struct device *dev)
854 struct cdns_i2c *xi2c = dev_get_drvdata(dev);
857 ret = clk_enable(xi2c->clk);
859 dev_err(dev, "Cannot enable clock.\n");
866 static const struct dev_pm_ops cdns_i2c_dev_pm_ops = {
867 SET_RUNTIME_PM_OPS(cdns_i2c_runtime_suspend,
868 cdns_i2c_runtime_resume, NULL)
871 static const struct cdns_platform_data r1p10_i2c_def = {
872 .quirks = CDNS_I2C_BROKEN_HOLD_BIT,
875 static const struct of_device_id cdns_i2c_of_match[] = {
876 { .compatible = "cdns,i2c-r1p10", .data = &r1p10_i2c_def },
877 { .compatible = "cdns,i2c-r1p14",},
878 { /* end of table */ }
880 MODULE_DEVICE_TABLE(of, cdns_i2c_of_match);
883 * cdns_i2c_probe - Platform registration call
884 * @pdev: Handle to the platform device structure
886 * This function does all the memory allocation and registration for the i2c
887 * device. User can modify the address mode to 10 bit address mode using the
888 * ioctl call with option I2C_TENBIT.
890 * Return: 0 on success, negative error otherwise
892 static int cdns_i2c_probe(struct platform_device *pdev)
894 struct resource *r_mem;
897 const struct of_device_id *match;
899 id = devm_kzalloc(&pdev->dev, sizeof(*id), GFP_KERNEL);
903 id->dev = &pdev->dev;
904 platform_set_drvdata(pdev, id);
906 match = of_match_node(cdns_i2c_of_match, pdev->dev.of_node);
907 if (match && match->data) {
908 const struct cdns_platform_data *data = match->data;
909 id->quirks = data->quirks;
912 r_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
913 id->membase = devm_ioremap_resource(&pdev->dev, r_mem);
914 if (IS_ERR(id->membase))
915 return PTR_ERR(id->membase);
917 id->irq = platform_get_irq(pdev, 0);
919 id->adap.owner = THIS_MODULE;
920 id->adap.dev.of_node = pdev->dev.of_node;
921 id->adap.algo = &cdns_i2c_algo;
922 id->adap.timeout = CDNS_I2C_TIMEOUT;
923 id->adap.retries = 3; /* Default retry value. */
924 id->adap.algo_data = id;
925 id->adap.dev.parent = &pdev->dev;
926 init_completion(&id->xfer_done);
927 snprintf(id->adap.name, sizeof(id->adap.name),
928 "Cadence I2C at %08lx", (unsigned long)r_mem->start);
930 id->clk = devm_clk_get(&pdev->dev, NULL);
931 if (IS_ERR(id->clk)) {
932 if (PTR_ERR(id->clk) != -EPROBE_DEFER)
933 dev_err(&pdev->dev, "input clock not found.\n");
934 return PTR_ERR(id->clk);
936 ret = clk_prepare_enable(id->clk);
938 dev_err(&pdev->dev, "Unable to enable clock.\n");
940 pm_runtime_set_autosuspend_delay(id->dev, CNDS_I2C_PM_TIMEOUT);
941 pm_runtime_use_autosuspend(id->dev);
942 pm_runtime_set_active(id->dev);
943 pm_runtime_enable(id->dev);
945 id->clk_rate_change_nb.notifier_call = cdns_i2c_clk_notifier_cb;
946 if (clk_notifier_register(id->clk, &id->clk_rate_change_nb))
947 dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
948 id->input_clk = clk_get_rate(id->clk);
950 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
952 if (ret || (id->i2c_clk > CDNS_I2C_SPEED_MAX))
953 id->i2c_clk = CDNS_I2C_SPEED_DEFAULT;
955 cdns_i2c_writereg(CDNS_I2C_CR_ACK_EN | CDNS_I2C_CR_NEA | CDNS_I2C_CR_MS,
958 ret = cdns_i2c_setclk(id->input_clk, id);
960 dev_err(&pdev->dev, "invalid SCL clock: %u Hz\n", id->i2c_clk);
965 ret = devm_request_irq(&pdev->dev, id->irq, cdns_i2c_isr, 0,
968 dev_err(&pdev->dev, "cannot get irq %d\n", id->irq);
973 * Cadence I2C controller has a bug wherein it generates
974 * invalid read transaction after HW timeout in master receiver mode.
975 * HW timeout is not used by this driver and the interrupt is disabled.
976 * But the feature itself cannot be disabled. Hence maximum value
977 * is written to this register to reduce the chances of error.
979 cdns_i2c_writereg(CDNS_I2C_TIMEOUT_MAX, CDNS_I2C_TIME_OUT_OFFSET);
981 ret = i2c_add_adapter(&id->adap);
985 dev_info(&pdev->dev, "%u kHz mmio %08lx irq %d\n",
986 id->i2c_clk / 1000, (unsigned long)r_mem->start, id->irq);
991 clk_disable_unprepare(id->clk);
992 pm_runtime_disable(&pdev->dev);
993 pm_runtime_set_suspended(&pdev->dev);
998 * cdns_i2c_remove - Unregister the device after releasing the resources
999 * @pdev: Handle to the platform device structure
1001 * This function frees all the resources allocated to the device.
1005 static int cdns_i2c_remove(struct platform_device *pdev)
1007 struct cdns_i2c *id = platform_get_drvdata(pdev);
1009 pm_runtime_disable(&pdev->dev);
1010 pm_runtime_set_suspended(&pdev->dev);
1011 pm_runtime_dont_use_autosuspend(&pdev->dev);
1013 i2c_del_adapter(&id->adap);
1014 clk_notifier_unregister(id->clk, &id->clk_rate_change_nb);
1015 clk_disable_unprepare(id->clk);
1020 static struct platform_driver cdns_i2c_drv = {
1022 .name = DRIVER_NAME,
1023 .of_match_table = cdns_i2c_of_match,
1024 .pm = &cdns_i2c_dev_pm_ops,
1026 .probe = cdns_i2c_probe,
1027 .remove = cdns_i2c_remove,
1030 module_platform_driver(cdns_i2c_drv);
1032 MODULE_AUTHOR("Xilinx Inc.");
1033 MODULE_DESCRIPTION("Cadence I2C bus driver");
1034 MODULE_LICENSE("GPL");