2 * I2C bus driver for the Cadence I2C controller.
4 * Copyright (C) 2009 - 2014 Xilinx, Inc.
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
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
21 /* Register offsets for the I2C device. */
22 #define CDNS_I2C_CR_OFFSET 0x00 /* Control Register, RW */
23 #define CDNS_I2C_SR_OFFSET 0x04 /* Status Register, RO */
24 #define CDNS_I2C_ADDR_OFFSET 0x08 /* I2C Address Register, RW */
25 #define CDNS_I2C_DATA_OFFSET 0x0C /* I2C Data Register, RW */
26 #define CDNS_I2C_ISR_OFFSET 0x10 /* IRQ Status Register, RW */
27 #define CDNS_I2C_XFER_SIZE_OFFSET 0x14 /* Transfer Size Register, RW */
28 #define CDNS_I2C_TIME_OUT_OFFSET 0x1C /* Time Out Register, RW */
29 #define CDNS_I2C_IER_OFFSET 0x24 /* IRQ Enable Register, WO */
30 #define CDNS_I2C_IDR_OFFSET 0x28 /* IRQ Disable Register, WO */
32 /* Control Register Bit mask definitions */
33 #define CDNS_I2C_CR_HOLD BIT(4) /* Hold Bus bit */
34 #define CDNS_I2C_CR_ACK_EN BIT(3)
35 #define CDNS_I2C_CR_NEA BIT(2)
36 #define CDNS_I2C_CR_MS BIT(1)
37 /* Read or Write Master transfer 0 = Transmitter, 1 = Receiver */
38 #define CDNS_I2C_CR_RW BIT(0)
39 /* 1 = Auto init FIFO to zeroes */
40 #define CDNS_I2C_CR_CLR_FIFO BIT(6)
41 #define CDNS_I2C_CR_DIVA_SHIFT 14
42 #define CDNS_I2C_CR_DIVA_MASK (3 << CDNS_I2C_CR_DIVA_SHIFT)
43 #define CDNS_I2C_CR_DIVB_SHIFT 8
44 #define CDNS_I2C_CR_DIVB_MASK (0x3f << CDNS_I2C_CR_DIVB_SHIFT)
46 /* Status Register Bit mask definitions */
47 #define CDNS_I2C_SR_BA BIT(8)
48 #define CDNS_I2C_SR_RXDV BIT(5)
51 * I2C Address Register Bit mask definitions
52 * Normal addressing mode uses [6:0] bits. Extended addressing mode uses [9:0]
53 * bits. A write access to this register always initiates a transfer if the I2C
56 #define CDNS_I2C_ADDR_MASK 0x000003FF /* I2C Address Mask */
59 * I2C Interrupt Registers Bit mask definitions
60 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
63 #define CDNS_I2C_IXR_ARB_LOST BIT(9)
64 #define CDNS_I2C_IXR_RX_UNF BIT(7)
65 #define CDNS_I2C_IXR_TX_OVF BIT(6)
66 #define CDNS_I2C_IXR_RX_OVF BIT(5)
67 #define CDNS_I2C_IXR_SLV_RDY BIT(4)
68 #define CDNS_I2C_IXR_TO BIT(3)
69 #define CDNS_I2C_IXR_NACK BIT(2)
70 #define CDNS_I2C_IXR_DATA BIT(1)
71 #define CDNS_I2C_IXR_COMP BIT(0)
73 #define CDNS_I2C_IXR_ALL_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
74 CDNS_I2C_IXR_RX_UNF | \
75 CDNS_I2C_IXR_TX_OVF | \
76 CDNS_I2C_IXR_RX_OVF | \
77 CDNS_I2C_IXR_SLV_RDY | \
83 #define CDNS_I2C_IXR_ERR_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
84 CDNS_I2C_IXR_RX_UNF | \
85 CDNS_I2C_IXR_TX_OVF | \
86 CDNS_I2C_IXR_RX_OVF | \
89 #define CDNS_I2C_ENABLED_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
90 CDNS_I2C_IXR_RX_UNF | \
91 CDNS_I2C_IXR_TX_OVF | \
92 CDNS_I2C_IXR_RX_OVF | \
97 #define CDNS_I2C_TIMEOUT msecs_to_jiffies(1000)
99 #define CDNS_I2C_FIFO_DEPTH 16
100 /* FIFO depth at which the DATA interrupt occurs */
101 #define CDNS_I2C_DATA_INTR_DEPTH (CDNS_I2C_FIFO_DEPTH - 2)
102 #define CDNS_I2C_MAX_TRANSFER_SIZE 255
103 /* Transfer size in multiples of data interrupt depth */
104 #define CDNS_I2C_TRANSFER_SIZE (CDNS_I2C_MAX_TRANSFER_SIZE - 3)
106 #define DRIVER_NAME "cdns-i2c"
108 #define CDNS_I2C_SPEED_MAX 400000
109 #define CDNS_I2C_SPEED_DEFAULT 100000
111 #define CDNS_I2C_DIVA_MAX 4
112 #define CDNS_I2C_DIVB_MAX 64
114 #define CDNS_I2C_TIMEOUT_MAX 0xFF
116 #define cdns_i2c_readreg(offset) readl_relaxed(id->membase + offset)
117 #define cdns_i2c_writereg(val, offset) writel_relaxed(val, id->membase + offset)
120 * struct cdns_i2c - I2C device private data structure
121 * @membase: Base address of the I2C device
122 * @adap: I2C adapter instance
123 * @p_msg: Message pointer
124 * @err_status: Error status in Interrupt Status Register
125 * @xfer_done: Transfer complete status
126 * @p_send_buf: Pointer to transmit buffer
127 * @p_recv_buf: Pointer to receive buffer
128 * @suspended: Flag holding the device's PM status
129 * @send_count: Number of bytes still expected to send
130 * @recv_count: Number of bytes still expected to receive
131 * @curr_recv_count: Number of bytes to be received in current transfer
133 * @input_clk: Input clock to I2C controller
134 * @i2c_clk: Maximum I2C clock speed
135 * @bus_hold_flag: Flag used in repeated start for clearing HOLD bit
136 * @clk: Pointer to struct clk
137 * @clk_rate_change_nb: Notifier block for clock rate changes
140 void __iomem
*membase
;
141 struct i2c_adapter adap
;
142 struct i2c_msg
*p_msg
;
144 struct completion xfer_done
;
145 unsigned char *p_send_buf
;
146 unsigned char *p_recv_buf
;
148 unsigned int send_count
;
149 unsigned int recv_count
;
150 unsigned int curr_recv_count
;
152 unsigned long input_clk
;
153 unsigned int i2c_clk
;
154 unsigned int bus_hold_flag
;
156 struct notifier_block clk_rate_change_nb
;
159 #define to_cdns_i2c(_nb) container_of(_nb, struct cdns_i2c, \
163 * cdns_i2c_clear_bus_hold() - Clear bus hold bit
164 * @id: Pointer to driver data struct
166 * Helper to clear the controller's bus hold bit.
168 static void cdns_i2c_clear_bus_hold(struct cdns_i2c
*id
)
170 u32 reg
= cdns_i2c_readreg(CDNS_I2C_CR_OFFSET
);
171 if (reg
& CDNS_I2C_CR_HOLD
)
172 cdns_i2c_writereg(reg
& ~CDNS_I2C_CR_HOLD
, CDNS_I2C_CR_OFFSET
);
176 * cdns_i2c_isr - Interrupt handler for the I2C device
177 * @irq: irq number for the I2C device
178 * @ptr: void pointer to cdns_i2c structure
180 * This function handles the data interrupt, transfer complete interrupt and
181 * the error interrupts of the I2C device.
183 * Return: IRQ_HANDLED always
185 static irqreturn_t
cdns_i2c_isr(int irq
, void *ptr
)
187 unsigned int isr_status
, avail_bytes
, updatetx
;
188 unsigned int bytes_to_send
;
189 struct cdns_i2c
*id
= ptr
;
190 /* Signal completion only after everything is updated */
192 irqreturn_t status
= IRQ_NONE
;
194 isr_status
= cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET
);
195 cdns_i2c_writereg(isr_status
, CDNS_I2C_ISR_OFFSET
);
197 /* Handling nack and arbitration lost interrupt */
198 if (isr_status
& (CDNS_I2C_IXR_NACK
| CDNS_I2C_IXR_ARB_LOST
)) {
200 status
= IRQ_HANDLED
;
204 * Check if transfer size register needs to be updated again for a
205 * large data receive operation.
208 if (id
->recv_count
> id
->curr_recv_count
)
211 /* When receiving, handle data interrupt and completion interrupt */
212 if (id
->p_recv_buf
&&
213 ((isr_status
& CDNS_I2C_IXR_COMP
) ||
214 (isr_status
& CDNS_I2C_IXR_DATA
))) {
215 /* Read data if receive data valid is set */
216 while (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET
) &
219 * Clear hold bit that was set for FIFO control if
220 * RX data left is less than FIFO depth, unless
221 * repeated start is selected.
223 if ((id
->recv_count
< CDNS_I2C_FIFO_DEPTH
) &&
225 cdns_i2c_clear_bus_hold(id
);
227 *(id
->p_recv_buf
)++ =
228 cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET
);
230 id
->curr_recv_count
--;
233 (id
->curr_recv_count
== CDNS_I2C_FIFO_DEPTH
+ 1))
238 * The controller sends NACK to the slave when transfer size
239 * register reaches zero without considering the HOLD bit.
240 * This workaround is implemented for large data transfers to
241 * maintain transfer size non-zero while performing a large
245 (id
->curr_recv_count
== CDNS_I2C_FIFO_DEPTH
+ 1)) {
246 /* wait while fifo is full */
247 while (cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET
) !=
248 (id
->curr_recv_count
- CDNS_I2C_FIFO_DEPTH
))
252 * Check number of bytes to be received against maximum
253 * transfer size and update register accordingly.
255 if (((int)(id
->recv_count
) - CDNS_I2C_FIFO_DEPTH
) >
256 CDNS_I2C_TRANSFER_SIZE
) {
257 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE
,
258 CDNS_I2C_XFER_SIZE_OFFSET
);
259 id
->curr_recv_count
= CDNS_I2C_TRANSFER_SIZE
+
262 cdns_i2c_writereg(id
->recv_count
-
264 CDNS_I2C_XFER_SIZE_OFFSET
);
265 id
->curr_recv_count
= id
->recv_count
;
269 /* Clear hold (if not repeated start) and signal completion */
270 if ((isr_status
& CDNS_I2C_IXR_COMP
) && !id
->recv_count
) {
271 if (!id
->bus_hold_flag
)
272 cdns_i2c_clear_bus_hold(id
);
276 status
= IRQ_HANDLED
;
279 /* When sending, handle transfer complete interrupt */
280 if ((isr_status
& CDNS_I2C_IXR_COMP
) && !id
->p_recv_buf
) {
282 * If there is more data to be sent, calculate the
283 * space available in FIFO and fill with that many bytes.
285 if (id
->send_count
) {
286 avail_bytes
= CDNS_I2C_FIFO_DEPTH
-
287 cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET
);
288 if (id
->send_count
> avail_bytes
)
289 bytes_to_send
= avail_bytes
;
291 bytes_to_send
= id
->send_count
;
293 while (bytes_to_send
--) {
295 (*(id
->p_send_buf
)++),
296 CDNS_I2C_DATA_OFFSET
);
301 * Signal the completion of transaction and
302 * clear the hold bus bit if there are no
303 * further messages to be processed.
307 if (!id
->send_count
&& !id
->bus_hold_flag
)
308 cdns_i2c_clear_bus_hold(id
);
310 status
= IRQ_HANDLED
;
313 /* Update the status for errors */
314 id
->err_status
= isr_status
& CDNS_I2C_IXR_ERR_INTR_MASK
;
316 status
= IRQ_HANDLED
;
319 complete(&id
->xfer_done
);
325 * cdns_i2c_mrecv - Prepare and start a master receive operation
326 * @id: pointer to the i2c device structure
328 static void cdns_i2c_mrecv(struct cdns_i2c
*id
)
330 unsigned int ctrl_reg
;
331 unsigned int isr_status
;
333 id
->p_recv_buf
= id
->p_msg
->buf
;
334 id
->recv_count
= id
->p_msg
->len
;
336 /* Put the controller in master receive mode and clear the FIFO */
337 ctrl_reg
= cdns_i2c_readreg(CDNS_I2C_CR_OFFSET
);
338 ctrl_reg
|= CDNS_I2C_CR_RW
| CDNS_I2C_CR_CLR_FIFO
;
340 if (id
->p_msg
->flags
& I2C_M_RECV_LEN
)
341 id
->recv_count
= I2C_SMBUS_BLOCK_MAX
+ 1;
343 id
->curr_recv_count
= id
->recv_count
;
346 * Check for the message size against FIFO depth and set the
347 * 'hold bus' bit if it is greater than FIFO depth.
349 if (id
->recv_count
> CDNS_I2C_FIFO_DEPTH
)
350 ctrl_reg
|= CDNS_I2C_CR_HOLD
;
352 cdns_i2c_writereg(ctrl_reg
, CDNS_I2C_CR_OFFSET
);
354 /* Clear the interrupts in interrupt status register */
355 isr_status
= cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET
);
356 cdns_i2c_writereg(isr_status
, CDNS_I2C_ISR_OFFSET
);
359 * The no. of bytes to receive is checked against the limit of
360 * max transfer size. Set transfer size register with no of bytes
361 * receive if it is less than transfer size and transfer size if
362 * it is more. Enable the interrupts.
364 if (id
->recv_count
> CDNS_I2C_TRANSFER_SIZE
) {
365 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE
,
366 CDNS_I2C_XFER_SIZE_OFFSET
);
367 id
->curr_recv_count
= CDNS_I2C_TRANSFER_SIZE
;
369 cdns_i2c_writereg(id
->recv_count
, CDNS_I2C_XFER_SIZE_OFFSET
);
372 /* Clear the bus hold flag if bytes to receive is less than FIFO size */
373 if (!id
->bus_hold_flag
&&
374 ((id
->p_msg
->flags
& I2C_M_RECV_LEN
) != I2C_M_RECV_LEN
) &&
375 (id
->recv_count
<= CDNS_I2C_FIFO_DEPTH
))
376 cdns_i2c_clear_bus_hold(id
);
377 /* Set the slave address in address register - triggers operation */
378 cdns_i2c_writereg(id
->p_msg
->addr
& CDNS_I2C_ADDR_MASK
,
379 CDNS_I2C_ADDR_OFFSET
);
380 cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK
, CDNS_I2C_IER_OFFSET
);
384 * cdns_i2c_msend - Prepare and start a master send operation
385 * @id: pointer to the i2c device
387 static void cdns_i2c_msend(struct cdns_i2c
*id
)
389 unsigned int avail_bytes
;
390 unsigned int bytes_to_send
;
391 unsigned int ctrl_reg
;
392 unsigned int isr_status
;
394 id
->p_recv_buf
= NULL
;
395 id
->p_send_buf
= id
->p_msg
->buf
;
396 id
->send_count
= id
->p_msg
->len
;
398 /* Set the controller in Master transmit mode and clear the FIFO. */
399 ctrl_reg
= cdns_i2c_readreg(CDNS_I2C_CR_OFFSET
);
400 ctrl_reg
&= ~CDNS_I2C_CR_RW
;
401 ctrl_reg
|= CDNS_I2C_CR_CLR_FIFO
;
404 * Check for the message size against FIFO depth and set the
405 * 'hold bus' bit if it is greater than FIFO depth.
407 if (id
->send_count
> CDNS_I2C_FIFO_DEPTH
)
408 ctrl_reg
|= CDNS_I2C_CR_HOLD
;
409 cdns_i2c_writereg(ctrl_reg
, CDNS_I2C_CR_OFFSET
);
411 /* Clear the interrupts in interrupt status register. */
412 isr_status
= cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET
);
413 cdns_i2c_writereg(isr_status
, CDNS_I2C_ISR_OFFSET
);
416 * Calculate the space available in FIFO. Check the message length
417 * against the space available, and fill the FIFO accordingly.
418 * Enable the interrupts.
420 avail_bytes
= CDNS_I2C_FIFO_DEPTH
-
421 cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET
);
423 if (id
->send_count
> avail_bytes
)
424 bytes_to_send
= avail_bytes
;
426 bytes_to_send
= id
->send_count
;
428 while (bytes_to_send
--) {
429 cdns_i2c_writereg((*(id
->p_send_buf
)++), CDNS_I2C_DATA_OFFSET
);
434 * Clear the bus hold flag if there is no more data
435 * and if it is the last message.
437 if (!id
->bus_hold_flag
&& !id
->send_count
)
438 cdns_i2c_clear_bus_hold(id
);
439 /* Set the slave address in address register - triggers operation. */
440 cdns_i2c_writereg(id
->p_msg
->addr
& CDNS_I2C_ADDR_MASK
,
441 CDNS_I2C_ADDR_OFFSET
);
443 cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK
, CDNS_I2C_IER_OFFSET
);
447 * cdns_i2c_master_reset - Reset the interface
448 * @adap: pointer to the i2c adapter driver instance
450 * This function cleanup the fifos, clear the hold bit and status
451 * and disable the interrupts.
453 static void cdns_i2c_master_reset(struct i2c_adapter
*adap
)
455 struct cdns_i2c
*id
= adap
->algo_data
;
458 /* Disable the interrupts */
459 cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK
, CDNS_I2C_IDR_OFFSET
);
460 /* Clear the hold bit and fifos */
461 regval
= cdns_i2c_readreg(CDNS_I2C_CR_OFFSET
);
462 regval
&= ~CDNS_I2C_CR_HOLD
;
463 regval
|= CDNS_I2C_CR_CLR_FIFO
;
464 cdns_i2c_writereg(regval
, CDNS_I2C_CR_OFFSET
);
465 /* Update the transfercount register to zero */
466 cdns_i2c_writereg(0, CDNS_I2C_XFER_SIZE_OFFSET
);
467 /* Clear the interupt status register */
468 regval
= cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET
);
469 cdns_i2c_writereg(regval
, CDNS_I2C_ISR_OFFSET
);
470 /* Clear the status register */
471 regval
= cdns_i2c_readreg(CDNS_I2C_SR_OFFSET
);
472 cdns_i2c_writereg(regval
, CDNS_I2C_SR_OFFSET
);
475 static int cdns_i2c_process_msg(struct cdns_i2c
*id
, struct i2c_msg
*msg
,
476 struct i2c_adapter
*adap
)
478 unsigned long time_left
;
483 reinit_completion(&id
->xfer_done
);
485 /* Check for the TEN Bit mode on each msg */
486 reg
= cdns_i2c_readreg(CDNS_I2C_CR_OFFSET
);
487 if (msg
->flags
& I2C_M_TEN
) {
488 if (reg
& CDNS_I2C_CR_NEA
)
489 cdns_i2c_writereg(reg
& ~CDNS_I2C_CR_NEA
,
492 if (!(reg
& CDNS_I2C_CR_NEA
))
493 cdns_i2c_writereg(reg
| CDNS_I2C_CR_NEA
,
497 /* Check for the R/W flag on each msg */
498 if (msg
->flags
& I2C_M_RD
)
503 /* Wait for the signal of completion */
504 time_left
= wait_for_completion_timeout(&id
->xfer_done
, adap
->timeout
);
505 if (time_left
== 0) {
506 cdns_i2c_master_reset(adap
);
507 dev_err(id
->adap
.dev
.parent
,
508 "timeout waiting on completion\n");
512 cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK
,
513 CDNS_I2C_IDR_OFFSET
);
515 /* If it is bus arbitration error, try again */
516 if (id
->err_status
& CDNS_I2C_IXR_ARB_LOST
)
523 * cdns_i2c_master_xfer - The main i2c transfer function
524 * @adap: pointer to the i2c adapter driver instance
525 * @msgs: pointer to the i2c message structure
526 * @num: the number of messages to transfer
528 * Initiates the send/recv activity based on the transfer message received.
530 * Return: number of msgs processed on success, negative error otherwise
532 static int cdns_i2c_master_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
537 struct cdns_i2c
*id
= adap
->algo_data
;
539 /* Check if the bus is free */
540 if (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET
) & CDNS_I2C_SR_BA
)
544 * Set the flag to one when multiple messages are to be
545 * processed with a repeated start.
549 * This controller does not give completion interrupt after a
550 * master receive message if HOLD bit is set (repeated start),
551 * resulting in SW timeout. Hence, if a receive message is
552 * followed by any other message, an error is returned
553 * indicating that this sequence is not supported.
555 for (count
= 0; count
< num
- 1; count
++) {
556 if (msgs
[count
].flags
& I2C_M_RD
) {
557 dev_warn(adap
->dev
.parent
,
558 "Can't do repeated start after a receive message\n");
562 id
->bus_hold_flag
= 1;
563 reg
= cdns_i2c_readreg(CDNS_I2C_CR_OFFSET
);
564 reg
|= CDNS_I2C_CR_HOLD
;
565 cdns_i2c_writereg(reg
, CDNS_I2C_CR_OFFSET
);
567 id
->bus_hold_flag
= 0;
570 /* Process the msg one by one */
571 for (count
= 0; count
< num
; count
++, msgs
++) {
572 if (count
== (num
- 1))
573 id
->bus_hold_flag
= 0;
575 ret
= cdns_i2c_process_msg(id
, msgs
, adap
);
579 /* Report the other error interrupts to application */
580 if (id
->err_status
) {
581 cdns_i2c_master_reset(adap
);
583 if (id
->err_status
& CDNS_I2C_IXR_NACK
)
594 * cdns_i2c_func - Returns the supported features of the I2C driver
595 * @adap: pointer to the i2c adapter structure
597 * Return: 32 bit value, each bit corresponding to a feature
599 static u32
cdns_i2c_func(struct i2c_adapter
*adap
)
601 return I2C_FUNC_I2C
| I2C_FUNC_10BIT_ADDR
|
602 (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
) |
603 I2C_FUNC_SMBUS_BLOCK_DATA
;
606 static const struct i2c_algorithm cdns_i2c_algo
= {
607 .master_xfer
= cdns_i2c_master_xfer
,
608 .functionality
= cdns_i2c_func
,
612 * cdns_i2c_calc_divs - Calculate clock dividers
613 * @f: I2C clock frequency
614 * @input_clk: Input clock frequency
615 * @a: First divider (return value)
616 * @b: Second divider (return value)
618 * f is used as input and output variable. As input it is used as target I2C
619 * frequency. On function exit f holds the actually resulting I2C frequency.
621 * Return: 0 on success, negative errno otherwise.
623 static int cdns_i2c_calc_divs(unsigned long *f
, unsigned long input_clk
,
624 unsigned int *a
, unsigned int *b
)
626 unsigned long fscl
= *f
, best_fscl
= *f
, actual_fscl
, temp
;
627 unsigned int div_a
, div_b
, calc_div_a
= 0, calc_div_b
= 0;
628 unsigned int last_error
, current_error
;
630 /* calculate (divisor_a+1) x (divisor_b+1) */
631 temp
= input_clk
/ (22 * fscl
);
634 * If the calculated value is negative or 0, the fscl input is out of
635 * range. Return error.
637 if (!temp
|| (temp
> (CDNS_I2C_DIVA_MAX
* CDNS_I2C_DIVB_MAX
)))
641 for (div_a
= 0; div_a
< CDNS_I2C_DIVA_MAX
; div_a
++) {
642 div_b
= DIV_ROUND_UP(input_clk
, 22 * fscl
* (div_a
+ 1));
644 if ((div_b
< 1) || (div_b
> CDNS_I2C_DIVB_MAX
))
648 actual_fscl
= input_clk
/ (22 * (div_a
+ 1) * (div_b
+ 1));
650 if (actual_fscl
> fscl
)
653 current_error
= ((actual_fscl
> fscl
) ? (actual_fscl
- fscl
) :
654 (fscl
- actual_fscl
));
656 if (last_error
> current_error
) {
659 best_fscl
= actual_fscl
;
660 last_error
= current_error
;
672 * cdns_i2c_setclk - This function sets the serial clock rate for the I2C device
673 * @clk_in: I2C clock input frequency in Hz
674 * @id: Pointer to the I2C device structure
676 * The device must be idle rather than busy transferring data before setting
677 * these device options.
678 * The data rate is set by values in the control register.
679 * The formula for determining the correct register values is
680 * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
681 * See the hardware data sheet for a full explanation of setting the serial
682 * clock rate. The clock can not be faster than the input clock divide by 22.
683 * The two most common clock rates are 100KHz and 400KHz.
685 * Return: 0 on success, negative error otherwise
687 static int cdns_i2c_setclk(unsigned long clk_in
, struct cdns_i2c
*id
)
689 unsigned int div_a
, div_b
;
690 unsigned int ctrl_reg
;
692 unsigned long fscl
= id
->i2c_clk
;
694 ret
= cdns_i2c_calc_divs(&fscl
, clk_in
, &div_a
, &div_b
);
698 ctrl_reg
= cdns_i2c_readreg(CDNS_I2C_CR_OFFSET
);
699 ctrl_reg
&= ~(CDNS_I2C_CR_DIVA_MASK
| CDNS_I2C_CR_DIVB_MASK
);
700 ctrl_reg
|= ((div_a
<< CDNS_I2C_CR_DIVA_SHIFT
) |
701 (div_b
<< CDNS_I2C_CR_DIVB_SHIFT
));
702 cdns_i2c_writereg(ctrl_reg
, CDNS_I2C_CR_OFFSET
);
708 * cdns_i2c_clk_notifier_cb - Clock rate change callback
709 * @nb: Pointer to notifier block
710 * @event: Notification reason
711 * @data: Pointer to notification data object
713 * This function is called when the cdns_i2c input clock frequency changes.
714 * The callback checks whether a valid bus frequency can be generated after the
715 * change. If so, the change is acknowledged, otherwise the change is aborted.
716 * New dividers are written to the HW in the pre- or post change notification
717 * depending on the scaling direction.
719 * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
720 * to acknowedge the change, NOTIFY_DONE if the notification is
721 * considered irrelevant.
723 static int cdns_i2c_clk_notifier_cb(struct notifier_block
*nb
, unsigned long
726 struct clk_notifier_data
*ndata
= data
;
727 struct cdns_i2c
*id
= to_cdns_i2c(nb
);
733 case PRE_RATE_CHANGE
:
735 unsigned long input_clk
= ndata
->new_rate
;
736 unsigned long fscl
= id
->i2c_clk
;
737 unsigned int div_a
, div_b
;
740 ret
= cdns_i2c_calc_divs(&fscl
, input_clk
, &div_a
, &div_b
);
742 dev_warn(id
->adap
.dev
.parent
,
743 "clock rate change rejected\n");
748 if (ndata
->new_rate
> ndata
->old_rate
)
749 cdns_i2c_setclk(ndata
->new_rate
, id
);
753 case POST_RATE_CHANGE
:
754 id
->input_clk
= ndata
->new_rate
;
756 if (ndata
->new_rate
< ndata
->old_rate
)
757 cdns_i2c_setclk(ndata
->new_rate
, id
);
759 case ABORT_RATE_CHANGE
:
761 if (ndata
->new_rate
> ndata
->old_rate
)
762 cdns_i2c_setclk(ndata
->old_rate
, id
);
770 * cdns_i2c_suspend - Suspend method for the driver
771 * @_dev: Address of the platform_device structure
773 * Put the driver into low power mode.
777 static int __maybe_unused
cdns_i2c_suspend(struct device
*_dev
)
779 struct platform_device
*pdev
= container_of(_dev
,
780 struct platform_device
, dev
);
781 struct cdns_i2c
*xi2c
= platform_get_drvdata(pdev
);
783 clk_disable(xi2c
->clk
);
790 * cdns_i2c_resume - Resume from suspend
791 * @_dev: Address of the platform_device structure
793 * Resume operation after suspend.
795 * Return: 0 on success and error value on error
797 static int __maybe_unused
cdns_i2c_resume(struct device
*_dev
)
799 struct platform_device
*pdev
= container_of(_dev
,
800 struct platform_device
, dev
);
801 struct cdns_i2c
*xi2c
= platform_get_drvdata(pdev
);
804 ret
= clk_enable(xi2c
->clk
);
806 dev_err(_dev
, "Cannot enable clock.\n");
815 static SIMPLE_DEV_PM_OPS(cdns_i2c_dev_pm_ops
, cdns_i2c_suspend
,
819 * cdns_i2c_probe - Platform registration call
820 * @pdev: Handle to the platform device structure
822 * This function does all the memory allocation and registration for the i2c
823 * device. User can modify the address mode to 10 bit address mode using the
824 * ioctl call with option I2C_TENBIT.
826 * Return: 0 on success, negative error otherwise
828 static int cdns_i2c_probe(struct platform_device
*pdev
)
830 struct resource
*r_mem
;
834 id
= devm_kzalloc(&pdev
->dev
, sizeof(*id
), GFP_KERNEL
);
838 platform_set_drvdata(pdev
, id
);
840 r_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
841 id
->membase
= devm_ioremap_resource(&pdev
->dev
, r_mem
);
842 if (IS_ERR(id
->membase
))
843 return PTR_ERR(id
->membase
);
845 id
->irq
= platform_get_irq(pdev
, 0);
847 id
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
848 id
->adap
.algo
= &cdns_i2c_algo
;
849 id
->adap
.timeout
= CDNS_I2C_TIMEOUT
;
850 id
->adap
.retries
= 3; /* Default retry value. */
851 id
->adap
.algo_data
= id
;
852 id
->adap
.dev
.parent
= &pdev
->dev
;
853 init_completion(&id
->xfer_done
);
854 snprintf(id
->adap
.name
, sizeof(id
->adap
.name
),
855 "Cadence I2C at %08lx", (unsigned long)r_mem
->start
);
857 id
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
858 if (IS_ERR(id
->clk
)) {
859 dev_err(&pdev
->dev
, "input clock not found.\n");
860 return PTR_ERR(id
->clk
);
862 ret
= clk_prepare_enable(id
->clk
);
864 dev_err(&pdev
->dev
, "Unable to enable clock.\n");
867 id
->clk_rate_change_nb
.notifier_call
= cdns_i2c_clk_notifier_cb
;
868 if (clk_notifier_register(id
->clk
, &id
->clk_rate_change_nb
))
869 dev_warn(&pdev
->dev
, "Unable to register clock notifier.\n");
870 id
->input_clk
= clk_get_rate(id
->clk
);
872 ret
= of_property_read_u32(pdev
->dev
.of_node
, "clock-frequency",
874 if (ret
|| (id
->i2c_clk
> CDNS_I2C_SPEED_MAX
))
875 id
->i2c_clk
= CDNS_I2C_SPEED_DEFAULT
;
877 cdns_i2c_writereg(CDNS_I2C_CR_ACK_EN
| CDNS_I2C_CR_NEA
| CDNS_I2C_CR_MS
,
880 ret
= cdns_i2c_setclk(id
->input_clk
, id
);
882 dev_err(&pdev
->dev
, "invalid SCL clock: %u Hz\n", id
->i2c_clk
);
887 ret
= devm_request_irq(&pdev
->dev
, id
->irq
, cdns_i2c_isr
, 0,
890 dev_err(&pdev
->dev
, "cannot get irq %d\n", id
->irq
);
894 ret
= i2c_add_adapter(&id
->adap
);
896 dev_err(&pdev
->dev
, "reg adap failed: %d\n", ret
);
901 * Cadence I2C controller has a bug wherein it generates
902 * invalid read transaction after HW timeout in master receiver mode.
903 * HW timeout is not used by this driver and the interrupt is disabled.
904 * But the feature itself cannot be disabled. Hence maximum value
905 * is written to this register to reduce the chances of error.
907 cdns_i2c_writereg(CDNS_I2C_TIMEOUT_MAX
, CDNS_I2C_TIME_OUT_OFFSET
);
909 dev_info(&pdev
->dev
, "%u kHz mmio %08lx irq %d\n",
910 id
->i2c_clk
/ 1000, (unsigned long)r_mem
->start
, id
->irq
);
915 clk_disable_unprepare(id
->clk
);
920 * cdns_i2c_remove - Unregister the device after releasing the resources
921 * @pdev: Handle to the platform device structure
923 * This function frees all the resources allocated to the device.
927 static int cdns_i2c_remove(struct platform_device
*pdev
)
929 struct cdns_i2c
*id
= platform_get_drvdata(pdev
);
931 i2c_del_adapter(&id
->adap
);
932 clk_notifier_unregister(id
->clk
, &id
->clk_rate_change_nb
);
933 clk_disable_unprepare(id
->clk
);
938 static const struct of_device_id cdns_i2c_of_match
[] = {
939 { .compatible
= "cdns,i2c-r1p10", },
940 { /* end of table */ }
942 MODULE_DEVICE_TABLE(of
, cdns_i2c_of_match
);
944 static struct platform_driver cdns_i2c_drv
= {
947 .of_match_table
= cdns_i2c_of_match
,
948 .pm
= &cdns_i2c_dev_pm_ops
,
950 .probe
= cdns_i2c_probe
,
951 .remove
= cdns_i2c_remove
,
954 module_platform_driver(cdns_i2c_drv
);
956 MODULE_AUTHOR("Xilinx Inc.");
957 MODULE_DESCRIPTION("Cadence I2C bus driver");
958 MODULE_LICENSE("GPL");