xref: /openbmc/linux/drivers/i2c/busses/i2c-cadence.c (revision dff03381)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * I2C bus driver for the Cadence I2C controller.
4  *
5  * Copyright (C) 2009 - 2014 Xilinx, Inc.
6  */
7 
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/io.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/of.h>
16 #include <linux/pm_runtime.h>
17 
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_IMR_OFFSET		0x20 /* IRQ Mask Register, RO */
27 #define CDNS_I2C_IER_OFFSET		0x24 /* IRQ Enable Register, WO */
28 #define CDNS_I2C_IDR_OFFSET		0x28 /* IRQ Disable Register, WO */
29 
30 /* Control Register Bit mask definitions */
31 #define CDNS_I2C_CR_HOLD		BIT(4) /* Hold Bus bit */
32 #define CDNS_I2C_CR_ACK_EN		BIT(3)
33 #define CDNS_I2C_CR_NEA			BIT(2)
34 #define CDNS_I2C_CR_MS			BIT(1)
35 /* Read or Write Master transfer 0 = Transmitter, 1 = Receiver */
36 #define CDNS_I2C_CR_RW			BIT(0)
37 /* 1 = Auto init FIFO to zeroes */
38 #define CDNS_I2C_CR_CLR_FIFO		BIT(6)
39 #define CDNS_I2C_CR_DIVA_SHIFT		14
40 #define CDNS_I2C_CR_DIVA_MASK		(3 << CDNS_I2C_CR_DIVA_SHIFT)
41 #define CDNS_I2C_CR_DIVB_SHIFT		8
42 #define CDNS_I2C_CR_DIVB_MASK		(0x3f << CDNS_I2C_CR_DIVB_SHIFT)
43 
44 #define CDNS_I2C_CR_MASTER_EN_MASK	(CDNS_I2C_CR_NEA | \
45 					 CDNS_I2C_CR_ACK_EN | \
46 					 CDNS_I2C_CR_MS)
47 
48 #define CDNS_I2C_CR_SLAVE_EN_MASK	~CDNS_I2C_CR_MASTER_EN_MASK
49 
50 /* Status Register Bit mask definitions */
51 #define CDNS_I2C_SR_BA		BIT(8)
52 #define CDNS_I2C_SR_TXDV	BIT(6)
53 #define CDNS_I2C_SR_RXDV	BIT(5)
54 #define CDNS_I2C_SR_RXRW	BIT(3)
55 
56 /*
57  * I2C Address Register Bit mask definitions
58  * Normal addressing mode uses [6:0] bits. Extended addressing mode uses [9:0]
59  * bits. A write access to this register always initiates a transfer if the I2C
60  * is in master mode.
61  */
62 #define CDNS_I2C_ADDR_MASK	0x000003FF /* I2C Address Mask */
63 
64 /*
65  * I2C Interrupt Registers Bit mask definitions
66  * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
67  * bit definitions.
68  */
69 #define CDNS_I2C_IXR_ARB_LOST		BIT(9)
70 #define CDNS_I2C_IXR_RX_UNF		BIT(7)
71 #define CDNS_I2C_IXR_TX_OVF		BIT(6)
72 #define CDNS_I2C_IXR_RX_OVF		BIT(5)
73 #define CDNS_I2C_IXR_SLV_RDY		BIT(4)
74 #define CDNS_I2C_IXR_TO			BIT(3)
75 #define CDNS_I2C_IXR_NACK		BIT(2)
76 #define CDNS_I2C_IXR_DATA		BIT(1)
77 #define CDNS_I2C_IXR_COMP		BIT(0)
78 
79 #define CDNS_I2C_IXR_ALL_INTR_MASK	(CDNS_I2C_IXR_ARB_LOST | \
80 					 CDNS_I2C_IXR_RX_UNF | \
81 					 CDNS_I2C_IXR_TX_OVF | \
82 					 CDNS_I2C_IXR_RX_OVF | \
83 					 CDNS_I2C_IXR_SLV_RDY | \
84 					 CDNS_I2C_IXR_TO | \
85 					 CDNS_I2C_IXR_NACK | \
86 					 CDNS_I2C_IXR_DATA | \
87 					 CDNS_I2C_IXR_COMP)
88 
89 #define CDNS_I2C_IXR_ERR_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 | \
93 					 CDNS_I2C_IXR_NACK)
94 
95 #define CDNS_I2C_ENABLED_INTR_MASK	(CDNS_I2C_IXR_ARB_LOST | \
96 					 CDNS_I2C_IXR_RX_UNF | \
97 					 CDNS_I2C_IXR_TX_OVF | \
98 					 CDNS_I2C_IXR_RX_OVF | \
99 					 CDNS_I2C_IXR_NACK | \
100 					 CDNS_I2C_IXR_DATA | \
101 					 CDNS_I2C_IXR_COMP)
102 
103 #define CDNS_I2C_IXR_SLAVE_INTR_MASK	(CDNS_I2C_IXR_RX_UNF | \
104 					 CDNS_I2C_IXR_TX_OVF | \
105 					 CDNS_I2C_IXR_RX_OVF | \
106 					 CDNS_I2C_IXR_TO | \
107 					 CDNS_I2C_IXR_NACK | \
108 					 CDNS_I2C_IXR_DATA | \
109 					 CDNS_I2C_IXR_COMP)
110 
111 #define CDNS_I2C_TIMEOUT		msecs_to_jiffies(1000)
112 /* timeout for pm runtime autosuspend */
113 #define CNDS_I2C_PM_TIMEOUT		1000	/* ms */
114 
115 #define CDNS_I2C_FIFO_DEPTH		16
116 /* FIFO depth at which the DATA interrupt occurs */
117 #define CDNS_I2C_DATA_INTR_DEPTH	(CDNS_I2C_FIFO_DEPTH - 2)
118 #define CDNS_I2C_MAX_TRANSFER_SIZE	255
119 /* Transfer size in multiples of data interrupt depth */
120 #define CDNS_I2C_TRANSFER_SIZE	(CDNS_I2C_MAX_TRANSFER_SIZE - 3)
121 
122 #define DRIVER_NAME		"cdns-i2c"
123 
124 #define CDNS_I2C_DIVA_MAX	4
125 #define CDNS_I2C_DIVB_MAX	64
126 
127 #define CDNS_I2C_TIMEOUT_MAX	0xFF
128 
129 #define CDNS_I2C_BROKEN_HOLD_BIT	BIT(0)
130 
131 #define cdns_i2c_readreg(offset)       readl_relaxed(id->membase + offset)
132 #define cdns_i2c_writereg(val, offset) writel_relaxed(val, id->membase + offset)
133 
134 #if IS_ENABLED(CONFIG_I2C_SLAVE)
135 /**
136  * enum cdns_i2c_mode - I2C Controller current operating mode
137  *
138  * @CDNS_I2C_MODE_SLAVE:       I2C controller operating in slave mode
139  * @CDNS_I2C_MODE_MASTER:      I2C Controller operating in master mode
140  */
141 enum cdns_i2c_mode {
142 	CDNS_I2C_MODE_SLAVE,
143 	CDNS_I2C_MODE_MASTER,
144 };
145 
146 /**
147  * enum cdns_i2c_slave_state - Slave state when I2C is operating in slave mode
148  *
149  * @CDNS_I2C_SLAVE_STATE_IDLE: I2C slave idle
150  * @CDNS_I2C_SLAVE_STATE_SEND: I2C slave sending data to master
151  * @CDNS_I2C_SLAVE_STATE_RECV: I2C slave receiving data from master
152  */
153 enum cdns_i2c_slave_state {
154 	CDNS_I2C_SLAVE_STATE_IDLE,
155 	CDNS_I2C_SLAVE_STATE_SEND,
156 	CDNS_I2C_SLAVE_STATE_RECV,
157 };
158 #endif
159 
160 /**
161  * struct cdns_i2c - I2C device private data structure
162  *
163  * @dev:		Pointer to device structure
164  * @membase:		Base address of the I2C device
165  * @adap:		I2C adapter instance
166  * @p_msg:		Message pointer
167  * @err_status:		Error status in Interrupt Status Register
168  * @xfer_done:		Transfer complete status
169  * @p_send_buf:		Pointer to transmit buffer
170  * @p_recv_buf:		Pointer to receive buffer
171  * @send_count:		Number of bytes still expected to send
172  * @recv_count:		Number of bytes still expected to receive
173  * @curr_recv_count:	Number of bytes to be received in current transfer
174  * @irq:		IRQ number
175  * @input_clk:		Input clock to I2C controller
176  * @i2c_clk:		Maximum I2C clock speed
177  * @bus_hold_flag:	Flag used in repeated start for clearing HOLD bit
178  * @clk:		Pointer to struct clk
179  * @clk_rate_change_nb:	Notifier block for clock rate changes
180  * @quirks:		flag for broken hold bit usage in r1p10
181  * @ctrl_reg:		Cached value of the control register.
182  * @ctrl_reg_diva_divb: value of fields DIV_A and DIV_B from CR register
183  * @slave:		Registered slave instance.
184  * @dev_mode:		I2C operating role(master/slave).
185  * @slave_state:	I2C Slave state(idle/read/write).
186  */
187 struct cdns_i2c {
188 	struct device		*dev;
189 	void __iomem *membase;
190 	struct i2c_adapter adap;
191 	struct i2c_msg *p_msg;
192 	int err_status;
193 	struct completion xfer_done;
194 	unsigned char *p_send_buf;
195 	unsigned char *p_recv_buf;
196 	unsigned int send_count;
197 	unsigned int recv_count;
198 	unsigned int curr_recv_count;
199 	int irq;
200 	unsigned long input_clk;
201 	unsigned int i2c_clk;
202 	unsigned int bus_hold_flag;
203 	struct clk *clk;
204 	struct notifier_block clk_rate_change_nb;
205 	u32 quirks;
206 	u32 ctrl_reg;
207 #if IS_ENABLED(CONFIG_I2C_SLAVE)
208 	u16 ctrl_reg_diva_divb;
209 	struct i2c_client *slave;
210 	enum cdns_i2c_mode dev_mode;
211 	enum cdns_i2c_slave_state slave_state;
212 #endif
213 };
214 
215 struct cdns_platform_data {
216 	u32 quirks;
217 };
218 
219 #define to_cdns_i2c(_nb)	container_of(_nb, struct cdns_i2c, \
220 					     clk_rate_change_nb)
221 
222 /**
223  * cdns_i2c_clear_bus_hold - Clear bus hold bit
224  * @id:	Pointer to driver data struct
225  *
226  * Helper to clear the controller's bus hold bit.
227  */
228 static void cdns_i2c_clear_bus_hold(struct cdns_i2c *id)
229 {
230 	u32 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
231 	if (reg & CDNS_I2C_CR_HOLD)
232 		cdns_i2c_writereg(reg & ~CDNS_I2C_CR_HOLD, CDNS_I2C_CR_OFFSET);
233 }
234 
235 static inline bool cdns_is_holdquirk(struct cdns_i2c *id, bool hold_wrkaround)
236 {
237 	return (hold_wrkaround &&
238 		(id->curr_recv_count == CDNS_I2C_FIFO_DEPTH + 1));
239 }
240 
241 #if IS_ENABLED(CONFIG_I2C_SLAVE)
242 static void cdns_i2c_set_mode(enum cdns_i2c_mode mode, struct cdns_i2c *id)
243 {
244 	/* Disable all interrupts */
245 	cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK, CDNS_I2C_IDR_OFFSET);
246 
247 	/* Clear FIFO and transfer size */
248 	cdns_i2c_writereg(CDNS_I2C_CR_CLR_FIFO, CDNS_I2C_CR_OFFSET);
249 
250 	/* Update device mode and state */
251 	id->dev_mode = mode;
252 	id->slave_state = CDNS_I2C_SLAVE_STATE_IDLE;
253 
254 	switch (mode) {
255 	case CDNS_I2C_MODE_MASTER:
256 		/* Enable i2c master */
257 		cdns_i2c_writereg(id->ctrl_reg_diva_divb |
258 				  CDNS_I2C_CR_MASTER_EN_MASK,
259 				  CDNS_I2C_CR_OFFSET);
260 		/*
261 		 * This delay is needed to give the IP some time to switch to
262 		 * the master mode. With lower values(like 110 us) i2cdetect
263 		 * will not detect any slave and without this delay, the IP will
264 		 * trigger a timeout interrupt.
265 		 */
266 		usleep_range(115, 125);
267 		break;
268 	case CDNS_I2C_MODE_SLAVE:
269 		/* Enable i2c slave */
270 		cdns_i2c_writereg(id->ctrl_reg_diva_divb &
271 				  CDNS_I2C_CR_SLAVE_EN_MASK,
272 				  CDNS_I2C_CR_OFFSET);
273 
274 		/* Setting slave address */
275 		cdns_i2c_writereg(id->slave->addr & CDNS_I2C_ADDR_MASK,
276 				  CDNS_I2C_ADDR_OFFSET);
277 
278 		/* Enable slave send/receive interrupts */
279 		cdns_i2c_writereg(CDNS_I2C_IXR_SLAVE_INTR_MASK,
280 				  CDNS_I2C_IER_OFFSET);
281 		break;
282 	}
283 }
284 
285 static void cdns_i2c_slave_rcv_data(struct cdns_i2c *id)
286 {
287 	u8 bytes;
288 	unsigned char data;
289 
290 	/* Prepare backend for data reception */
291 	if (id->slave_state == CDNS_I2C_SLAVE_STATE_IDLE) {
292 		id->slave_state = CDNS_I2C_SLAVE_STATE_RECV;
293 		i2c_slave_event(id->slave, I2C_SLAVE_WRITE_REQUESTED, NULL);
294 	}
295 
296 	/* Fetch number of bytes to receive */
297 	bytes = cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
298 
299 	/* Read data and send to backend */
300 	while (bytes--) {
301 		data = cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET);
302 		i2c_slave_event(id->slave, I2C_SLAVE_WRITE_RECEIVED, &data);
303 	}
304 }
305 
306 static void cdns_i2c_slave_send_data(struct cdns_i2c *id)
307 {
308 	u8 data;
309 
310 	/* Prepare backend for data transmission */
311 	if (id->slave_state == CDNS_I2C_SLAVE_STATE_IDLE) {
312 		id->slave_state = CDNS_I2C_SLAVE_STATE_SEND;
313 		i2c_slave_event(id->slave, I2C_SLAVE_READ_REQUESTED, &data);
314 	} else {
315 		i2c_slave_event(id->slave, I2C_SLAVE_READ_PROCESSED, &data);
316 	}
317 
318 	/* Send data over bus */
319 	cdns_i2c_writereg(data, CDNS_I2C_DATA_OFFSET);
320 }
321 
322 /**
323  * cdns_i2c_slave_isr - Interrupt handler for the I2C device in slave role
324  * @ptr:       Pointer to I2C device private data
325  *
326  * This function handles the data interrupt and transfer complete interrupt of
327  * the I2C device in slave role.
328  *
329  * Return: IRQ_HANDLED always
330  */
331 static irqreturn_t cdns_i2c_slave_isr(void *ptr)
332 {
333 	struct cdns_i2c *id = ptr;
334 	unsigned int isr_status, i2c_status;
335 
336 	/* Fetch the interrupt status */
337 	isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
338 	cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
339 
340 	/* Ignore masked interrupts */
341 	isr_status &= ~cdns_i2c_readreg(CDNS_I2C_IMR_OFFSET);
342 
343 	/* Fetch transfer mode (send/receive) */
344 	i2c_status = cdns_i2c_readreg(CDNS_I2C_SR_OFFSET);
345 
346 	/* Handle data send/receive */
347 	if (i2c_status & CDNS_I2C_SR_RXRW) {
348 		/* Send data to master */
349 		if (isr_status & CDNS_I2C_IXR_DATA)
350 			cdns_i2c_slave_send_data(id);
351 
352 		if (isr_status & CDNS_I2C_IXR_COMP) {
353 			id->slave_state = CDNS_I2C_SLAVE_STATE_IDLE;
354 			i2c_slave_event(id->slave, I2C_SLAVE_STOP, NULL);
355 		}
356 	} else {
357 		/* Receive data from master */
358 		if (isr_status & CDNS_I2C_IXR_DATA)
359 			cdns_i2c_slave_rcv_data(id);
360 
361 		if (isr_status & CDNS_I2C_IXR_COMP) {
362 			cdns_i2c_slave_rcv_data(id);
363 			id->slave_state = CDNS_I2C_SLAVE_STATE_IDLE;
364 			i2c_slave_event(id->slave, I2C_SLAVE_STOP, NULL);
365 		}
366 	}
367 
368 	/* Master indicated xfer stop or fifo underflow/overflow */
369 	if (isr_status & (CDNS_I2C_IXR_NACK | CDNS_I2C_IXR_RX_OVF |
370 			  CDNS_I2C_IXR_RX_UNF | CDNS_I2C_IXR_TX_OVF)) {
371 		id->slave_state = CDNS_I2C_SLAVE_STATE_IDLE;
372 		i2c_slave_event(id->slave, I2C_SLAVE_STOP, NULL);
373 		cdns_i2c_writereg(CDNS_I2C_CR_CLR_FIFO, CDNS_I2C_CR_OFFSET);
374 	}
375 
376 	return IRQ_HANDLED;
377 }
378 #endif
379 
380 /**
381  * cdns_i2c_master_isr - Interrupt handler for the I2C device in master role
382  * @ptr:       Pointer to I2C device private data
383  *
384  * This function handles the data interrupt, transfer complete interrupt and
385  * the error interrupts of the I2C device in master role.
386  *
387  * Return: IRQ_HANDLED always
388  */
389 static irqreturn_t cdns_i2c_master_isr(void *ptr)
390 {
391 	unsigned int isr_status, avail_bytes;
392 	unsigned int bytes_to_send;
393 	bool updatetx;
394 	struct cdns_i2c *id = ptr;
395 	/* Signal completion only after everything is updated */
396 	int done_flag = 0;
397 	irqreturn_t status = IRQ_NONE;
398 
399 	isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
400 	cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
401 	id->err_status = 0;
402 
403 	/* Handling nack and arbitration lost interrupt */
404 	if (isr_status & (CDNS_I2C_IXR_NACK | CDNS_I2C_IXR_ARB_LOST)) {
405 		done_flag = 1;
406 		status = IRQ_HANDLED;
407 	}
408 
409 	/*
410 	 * Check if transfer size register needs to be updated again for a
411 	 * large data receive operation.
412 	 */
413 	updatetx = id->recv_count > id->curr_recv_count;
414 
415 	/* When receiving, handle data interrupt and completion interrupt */
416 	if (id->p_recv_buf &&
417 	    ((isr_status & CDNS_I2C_IXR_COMP) ||
418 	     (isr_status & CDNS_I2C_IXR_DATA))) {
419 		/* Read data if receive data valid is set */
420 		while (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) &
421 		       CDNS_I2C_SR_RXDV) {
422 			if (id->recv_count > 0) {
423 				*(id->p_recv_buf)++ =
424 					cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET);
425 				id->recv_count--;
426 				id->curr_recv_count--;
427 
428 				/*
429 				 * Clear hold bit that was set for FIFO control
430 				 * if RX data left is less than or equal to
431 				 * FIFO DEPTH unless repeated start is selected
432 				 */
433 				if (id->recv_count <= CDNS_I2C_FIFO_DEPTH &&
434 				    !id->bus_hold_flag)
435 					cdns_i2c_clear_bus_hold(id);
436 
437 			} else {
438 				dev_err(id->adap.dev.parent,
439 					"xfer_size reg rollover. xfer aborted!\n");
440 				id->err_status |= CDNS_I2C_IXR_TO;
441 				break;
442 			}
443 
444 			if (cdns_is_holdquirk(id, updatetx))
445 				break;
446 		}
447 
448 		/*
449 		 * The controller sends NACK to the slave when transfer size
450 		 * register reaches zero without considering the HOLD bit.
451 		 * This workaround is implemented for large data transfers to
452 		 * maintain transfer size non-zero while performing a large
453 		 * receive operation.
454 		 */
455 		if (cdns_is_holdquirk(id, updatetx)) {
456 			/* wait while fifo is full */
457 			while (cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET) !=
458 			       (id->curr_recv_count - CDNS_I2C_FIFO_DEPTH))
459 				;
460 
461 			/*
462 			 * Check number of bytes to be received against maximum
463 			 * transfer size and update register accordingly.
464 			 */
465 			if (((int)(id->recv_count) - CDNS_I2C_FIFO_DEPTH) >
466 			    CDNS_I2C_TRANSFER_SIZE) {
467 				cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
468 						  CDNS_I2C_XFER_SIZE_OFFSET);
469 				id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE +
470 						      CDNS_I2C_FIFO_DEPTH;
471 			} else {
472 				cdns_i2c_writereg(id->recv_count -
473 						  CDNS_I2C_FIFO_DEPTH,
474 						  CDNS_I2C_XFER_SIZE_OFFSET);
475 				id->curr_recv_count = id->recv_count;
476 			}
477 		}
478 
479 		/* Clear hold (if not repeated start) and signal completion */
480 		if ((isr_status & CDNS_I2C_IXR_COMP) && !id->recv_count) {
481 			if (!id->bus_hold_flag)
482 				cdns_i2c_clear_bus_hold(id);
483 			done_flag = 1;
484 		}
485 
486 		status = IRQ_HANDLED;
487 	}
488 
489 	/* When sending, handle transfer complete interrupt */
490 	if ((isr_status & CDNS_I2C_IXR_COMP) && !id->p_recv_buf) {
491 		/*
492 		 * If there is more data to be sent, calculate the
493 		 * space available in FIFO and fill with that many bytes.
494 		 */
495 		if (id->send_count) {
496 			avail_bytes = CDNS_I2C_FIFO_DEPTH -
497 			    cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
498 			if (id->send_count > avail_bytes)
499 				bytes_to_send = avail_bytes;
500 			else
501 				bytes_to_send = id->send_count;
502 
503 			while (bytes_to_send--) {
504 				cdns_i2c_writereg(
505 					(*(id->p_send_buf)++),
506 					 CDNS_I2C_DATA_OFFSET);
507 				id->send_count--;
508 			}
509 		} else {
510 			/*
511 			 * Signal the completion of transaction and
512 			 * clear the hold bus bit if there are no
513 			 * further messages to be processed.
514 			 */
515 			done_flag = 1;
516 		}
517 		if (!id->send_count && !id->bus_hold_flag)
518 			cdns_i2c_clear_bus_hold(id);
519 
520 		status = IRQ_HANDLED;
521 	}
522 
523 	/* Update the status for errors */
524 	id->err_status |= isr_status & CDNS_I2C_IXR_ERR_INTR_MASK;
525 	if (id->err_status)
526 		status = IRQ_HANDLED;
527 
528 	if (done_flag)
529 		complete(&id->xfer_done);
530 
531 	return status;
532 }
533 
534 /**
535  * cdns_i2c_isr - Interrupt handler for the I2C device
536  * @irq:	irq number for the I2C device
537  * @ptr:	void pointer to cdns_i2c structure
538  *
539  * This function passes the control to slave/master based on current role of
540  * i2c controller.
541  *
542  * Return: IRQ_HANDLED always
543  */
544 static irqreturn_t cdns_i2c_isr(int irq, void *ptr)
545 {
546 #if IS_ENABLED(CONFIG_I2C_SLAVE)
547 	struct cdns_i2c *id = ptr;
548 
549 	if (id->dev_mode == CDNS_I2C_MODE_SLAVE)
550 		return cdns_i2c_slave_isr(ptr);
551 #endif
552 	return cdns_i2c_master_isr(ptr);
553 }
554 
555 /**
556  * cdns_i2c_mrecv - Prepare and start a master receive operation
557  * @id:		pointer to the i2c device structure
558  */
559 static void cdns_i2c_mrecv(struct cdns_i2c *id)
560 {
561 	unsigned int ctrl_reg;
562 	unsigned int isr_status;
563 	unsigned long flags;
564 	bool hold_clear = false;
565 	bool irq_save = false;
566 
567 	u32 addr;
568 
569 	id->p_recv_buf = id->p_msg->buf;
570 	id->recv_count = id->p_msg->len;
571 
572 	/* Put the controller in master receive mode and clear the FIFO */
573 	ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
574 	ctrl_reg |= CDNS_I2C_CR_RW | CDNS_I2C_CR_CLR_FIFO;
575 
576 	/*
577 	 * Receive up to I2C_SMBUS_BLOCK_MAX data bytes, plus one message length
578 	 * byte, plus one checksum byte if PEC is enabled. p_msg->len will be 2 if
579 	 * PEC is enabled, otherwise 1.
580 	 */
581 	if (id->p_msg->flags & I2C_M_RECV_LEN)
582 		id->recv_count = I2C_SMBUS_BLOCK_MAX + id->p_msg->len;
583 
584 	id->curr_recv_count = id->recv_count;
585 
586 	/*
587 	 * Check for the message size against FIFO depth and set the
588 	 * 'hold bus' bit if it is greater than FIFO depth.
589 	 */
590 	if (id->recv_count > CDNS_I2C_FIFO_DEPTH)
591 		ctrl_reg |= CDNS_I2C_CR_HOLD;
592 
593 	cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
594 
595 	/* Clear the interrupts in interrupt status register */
596 	isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
597 	cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
598 
599 	/*
600 	 * The no. of bytes to receive is checked against the limit of
601 	 * max transfer size. Set transfer size register with no of bytes
602 	 * receive if it is less than transfer size and transfer size if
603 	 * it is more. Enable the interrupts.
604 	 */
605 	if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
606 		cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
607 				  CDNS_I2C_XFER_SIZE_OFFSET);
608 		id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
609 	} else {
610 		cdns_i2c_writereg(id->recv_count, CDNS_I2C_XFER_SIZE_OFFSET);
611 	}
612 
613 	/* Determine hold_clear based on number of bytes to receive and hold flag */
614 	if (!id->bus_hold_flag &&
615 	    ((id->p_msg->flags & I2C_M_RECV_LEN) != I2C_M_RECV_LEN) &&
616 	    (id->recv_count <= CDNS_I2C_FIFO_DEPTH)) {
617 		if (cdns_i2c_readreg(CDNS_I2C_CR_OFFSET) & CDNS_I2C_CR_HOLD) {
618 			hold_clear = true;
619 			if (id->quirks & CDNS_I2C_BROKEN_HOLD_BIT)
620 				irq_save = true;
621 		}
622 	}
623 
624 	addr = id->p_msg->addr;
625 	addr &= CDNS_I2C_ADDR_MASK;
626 
627 	if (hold_clear) {
628 		ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET) & ~CDNS_I2C_CR_HOLD;
629 		/*
630 		 * In case of Xilinx Zynq SOC, clear the HOLD bit before transfer size
631 		 * register reaches '0'. This is an IP bug which causes transfer size
632 		 * register overflow to 0xFF. To satisfy this timing requirement,
633 		 * disable the interrupts on current processor core between register
634 		 * writes to slave address register and control register.
635 		 */
636 		if (irq_save)
637 			local_irq_save(flags);
638 
639 		cdns_i2c_writereg(addr, CDNS_I2C_ADDR_OFFSET);
640 		cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
641 		/* Read it back to avoid bufferring and make sure write happens */
642 		cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
643 
644 		if (irq_save)
645 			local_irq_restore(flags);
646 	} else {
647 		cdns_i2c_writereg(addr, CDNS_I2C_ADDR_OFFSET);
648 	}
649 
650 	cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
651 }
652 
653 /**
654  * cdns_i2c_msend - Prepare and start a master send operation
655  * @id:		pointer to the i2c device
656  */
657 static void cdns_i2c_msend(struct cdns_i2c *id)
658 {
659 	unsigned int avail_bytes;
660 	unsigned int bytes_to_send;
661 	unsigned int ctrl_reg;
662 	unsigned int isr_status;
663 
664 	id->p_recv_buf = NULL;
665 	id->p_send_buf = id->p_msg->buf;
666 	id->send_count = id->p_msg->len;
667 
668 	/* Set the controller in Master transmit mode and clear the FIFO. */
669 	ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
670 	ctrl_reg &= ~CDNS_I2C_CR_RW;
671 	ctrl_reg |= CDNS_I2C_CR_CLR_FIFO;
672 
673 	/*
674 	 * Check for the message size against FIFO depth and set the
675 	 * 'hold bus' bit if it is greater than FIFO depth.
676 	 */
677 	if (id->send_count > CDNS_I2C_FIFO_DEPTH)
678 		ctrl_reg |= CDNS_I2C_CR_HOLD;
679 	cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
680 
681 	/* Clear the interrupts in interrupt status register. */
682 	isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
683 	cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
684 
685 	/*
686 	 * Calculate the space available in FIFO. Check the message length
687 	 * against the space available, and fill the FIFO accordingly.
688 	 * Enable the interrupts.
689 	 */
690 	avail_bytes = CDNS_I2C_FIFO_DEPTH -
691 				cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
692 
693 	if (id->send_count > avail_bytes)
694 		bytes_to_send = avail_bytes;
695 	else
696 		bytes_to_send = id->send_count;
697 
698 	while (bytes_to_send--) {
699 		cdns_i2c_writereg((*(id->p_send_buf)++), CDNS_I2C_DATA_OFFSET);
700 		id->send_count--;
701 	}
702 
703 	/*
704 	 * Clear the bus hold flag if there is no more data
705 	 * and if it is the last message.
706 	 */
707 	if (!id->bus_hold_flag && !id->send_count)
708 		cdns_i2c_clear_bus_hold(id);
709 	/* Set the slave address in address register - triggers operation. */
710 	cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
711 						CDNS_I2C_ADDR_OFFSET);
712 
713 	cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
714 }
715 
716 /**
717  * cdns_i2c_master_reset - Reset the interface
718  * @adap:	pointer to the i2c adapter driver instance
719  *
720  * This function cleanup the fifos, clear the hold bit and status
721  * and disable the interrupts.
722  */
723 static void cdns_i2c_master_reset(struct i2c_adapter *adap)
724 {
725 	struct cdns_i2c *id = adap->algo_data;
726 	u32 regval;
727 
728 	/* Disable the interrupts */
729 	cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK, CDNS_I2C_IDR_OFFSET);
730 	/* Clear the hold bit and fifos */
731 	regval = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
732 	regval &= ~CDNS_I2C_CR_HOLD;
733 	regval |= CDNS_I2C_CR_CLR_FIFO;
734 	cdns_i2c_writereg(regval, CDNS_I2C_CR_OFFSET);
735 	/* Update the transfercount register to zero */
736 	cdns_i2c_writereg(0, CDNS_I2C_XFER_SIZE_OFFSET);
737 	/* Clear the interrupt status register */
738 	regval = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
739 	cdns_i2c_writereg(regval, CDNS_I2C_ISR_OFFSET);
740 	/* Clear the status register */
741 	regval = cdns_i2c_readreg(CDNS_I2C_SR_OFFSET);
742 	cdns_i2c_writereg(regval, CDNS_I2C_SR_OFFSET);
743 }
744 
745 static int cdns_i2c_process_msg(struct cdns_i2c *id, struct i2c_msg *msg,
746 		struct i2c_adapter *adap)
747 {
748 	unsigned long time_left, msg_timeout;
749 	u32 reg;
750 
751 	id->p_msg = msg;
752 	id->err_status = 0;
753 	reinit_completion(&id->xfer_done);
754 
755 	/* Check for the TEN Bit mode on each msg */
756 	reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
757 	if (msg->flags & I2C_M_TEN) {
758 		if (reg & CDNS_I2C_CR_NEA)
759 			cdns_i2c_writereg(reg & ~CDNS_I2C_CR_NEA,
760 					CDNS_I2C_CR_OFFSET);
761 	} else {
762 		if (!(reg & CDNS_I2C_CR_NEA))
763 			cdns_i2c_writereg(reg | CDNS_I2C_CR_NEA,
764 					CDNS_I2C_CR_OFFSET);
765 	}
766 
767 	/* Check for the R/W flag on each msg */
768 	if (msg->flags & I2C_M_RD)
769 		cdns_i2c_mrecv(id);
770 	else
771 		cdns_i2c_msend(id);
772 
773 	/* Minimal time to execute this message */
774 	msg_timeout = msecs_to_jiffies((1000 * msg->len * BITS_PER_BYTE) / id->i2c_clk);
775 	/* Plus some wiggle room */
776 	msg_timeout += msecs_to_jiffies(500);
777 
778 	if (msg_timeout < adap->timeout)
779 		msg_timeout = adap->timeout;
780 
781 	/* Wait for the signal of completion */
782 	time_left = wait_for_completion_timeout(&id->xfer_done, msg_timeout);
783 	if (time_left == 0) {
784 		cdns_i2c_master_reset(adap);
785 		dev_err(id->adap.dev.parent,
786 				"timeout waiting on completion\n");
787 		return -ETIMEDOUT;
788 	}
789 
790 	cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK,
791 			  CDNS_I2C_IDR_OFFSET);
792 
793 	/* If it is bus arbitration error, try again */
794 	if (id->err_status & CDNS_I2C_IXR_ARB_LOST)
795 		return -EAGAIN;
796 
797 	if (msg->flags & I2C_M_RECV_LEN)
798 		msg->len += min_t(unsigned int, msg->buf[0], I2C_SMBUS_BLOCK_MAX);
799 
800 	return 0;
801 }
802 
803 /**
804  * cdns_i2c_master_xfer - The main i2c transfer function
805  * @adap:	pointer to the i2c adapter driver instance
806  * @msgs:	pointer to the i2c message structure
807  * @num:	the number of messages to transfer
808  *
809  * Initiates the send/recv activity based on the transfer message received.
810  *
811  * Return: number of msgs processed on success, negative error otherwise
812  */
813 static int cdns_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
814 				int num)
815 {
816 	int ret, count;
817 	u32 reg;
818 	struct cdns_i2c *id = adap->algo_data;
819 	bool hold_quirk;
820 #if IS_ENABLED(CONFIG_I2C_SLAVE)
821 	bool change_role = false;
822 #endif
823 
824 	ret = pm_runtime_resume_and_get(id->dev);
825 	if (ret < 0)
826 		return ret;
827 
828 #if IS_ENABLED(CONFIG_I2C_SLAVE)
829 	/* Check i2c operating mode and switch if possible */
830 	if (id->dev_mode == CDNS_I2C_MODE_SLAVE) {
831 		if (id->slave_state != CDNS_I2C_SLAVE_STATE_IDLE)
832 			return -EAGAIN;
833 
834 		/* Set mode to master */
835 		cdns_i2c_set_mode(CDNS_I2C_MODE_MASTER, id);
836 
837 		/* Mark flag to change role once xfer is completed */
838 		change_role = true;
839 	}
840 #endif
841 
842 	/* Check if the bus is free */
843 	if (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) & CDNS_I2C_SR_BA) {
844 		ret = -EAGAIN;
845 		goto out;
846 	}
847 
848 	hold_quirk = !!(id->quirks & CDNS_I2C_BROKEN_HOLD_BIT);
849 	/*
850 	 * Set the flag to one when multiple messages are to be
851 	 * processed with a repeated start.
852 	 */
853 	if (num > 1) {
854 		/*
855 		 * This controller does not give completion interrupt after a
856 		 * master receive message if HOLD bit is set (repeated start),
857 		 * resulting in SW timeout. Hence, if a receive message is
858 		 * followed by any other message, an error is returned
859 		 * indicating that this sequence is not supported.
860 		 */
861 		for (count = 0; (count < num - 1 && hold_quirk); count++) {
862 			if (msgs[count].flags & I2C_M_RD) {
863 				dev_warn(adap->dev.parent,
864 					 "Can't do repeated start after a receive message\n");
865 				ret = -EOPNOTSUPP;
866 				goto out;
867 			}
868 		}
869 		id->bus_hold_flag = 1;
870 		reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
871 		reg |= CDNS_I2C_CR_HOLD;
872 		cdns_i2c_writereg(reg, CDNS_I2C_CR_OFFSET);
873 	} else {
874 		id->bus_hold_flag = 0;
875 	}
876 
877 	/* Process the msg one by one */
878 	for (count = 0; count < num; count++, msgs++) {
879 		if (count == (num - 1))
880 			id->bus_hold_flag = 0;
881 
882 		ret = cdns_i2c_process_msg(id, msgs, adap);
883 		if (ret)
884 			goto out;
885 
886 		/* Report the other error interrupts to application */
887 		if (id->err_status) {
888 			cdns_i2c_master_reset(adap);
889 
890 			if (id->err_status & CDNS_I2C_IXR_NACK) {
891 				ret = -ENXIO;
892 				goto out;
893 			}
894 			ret = -EIO;
895 			goto out;
896 		}
897 	}
898 
899 	ret = num;
900 
901 out:
902 
903 #if IS_ENABLED(CONFIG_I2C_SLAVE)
904 	/* Switch i2c mode to slave */
905 	if (change_role)
906 		cdns_i2c_set_mode(CDNS_I2C_MODE_SLAVE, id);
907 #endif
908 
909 	pm_runtime_mark_last_busy(id->dev);
910 	pm_runtime_put_autosuspend(id->dev);
911 	return ret;
912 }
913 
914 /**
915  * cdns_i2c_func - Returns the supported features of the I2C driver
916  * @adap:	pointer to the i2c adapter structure
917  *
918  * Return: 32 bit value, each bit corresponding to a feature
919  */
920 static u32 cdns_i2c_func(struct i2c_adapter *adap)
921 {
922 	u32 func = I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
923 			(I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
924 			I2C_FUNC_SMBUS_BLOCK_DATA;
925 
926 #if IS_ENABLED(CONFIG_I2C_SLAVE)
927 	func |= I2C_FUNC_SLAVE;
928 #endif
929 
930 	return func;
931 }
932 
933 #if IS_ENABLED(CONFIG_I2C_SLAVE)
934 static int cdns_reg_slave(struct i2c_client *slave)
935 {
936 	int ret;
937 	struct cdns_i2c *id = container_of(slave->adapter, struct cdns_i2c,
938 									adap);
939 
940 	if (id->slave)
941 		return -EBUSY;
942 
943 	if (slave->flags & I2C_CLIENT_TEN)
944 		return -EAFNOSUPPORT;
945 
946 	ret = pm_runtime_resume_and_get(id->dev);
947 	if (ret < 0)
948 		return ret;
949 
950 	/* Store slave information */
951 	id->slave = slave;
952 
953 	/* Enable I2C slave */
954 	cdns_i2c_set_mode(CDNS_I2C_MODE_SLAVE, id);
955 
956 	return 0;
957 }
958 
959 static int cdns_unreg_slave(struct i2c_client *slave)
960 {
961 	struct cdns_i2c *id = container_of(slave->adapter, struct cdns_i2c,
962 									adap);
963 
964 	pm_runtime_put(id->dev);
965 
966 	/* Remove slave information */
967 	id->slave = NULL;
968 
969 	/* Enable I2C master */
970 	cdns_i2c_set_mode(CDNS_I2C_MODE_MASTER, id);
971 
972 	return 0;
973 }
974 #endif
975 
976 static const struct i2c_algorithm cdns_i2c_algo = {
977 	.master_xfer	= cdns_i2c_master_xfer,
978 	.functionality	= cdns_i2c_func,
979 #if IS_ENABLED(CONFIG_I2C_SLAVE)
980 	.reg_slave	= cdns_reg_slave,
981 	.unreg_slave	= cdns_unreg_slave,
982 #endif
983 };
984 
985 /**
986  * cdns_i2c_calc_divs - Calculate clock dividers
987  * @f:		I2C clock frequency
988  * @input_clk:	Input clock frequency
989  * @a:		First divider (return value)
990  * @b:		Second divider (return value)
991  *
992  * f is used as input and output variable. As input it is used as target I2C
993  * frequency. On function exit f holds the actually resulting I2C frequency.
994  *
995  * Return: 0 on success, negative errno otherwise.
996  */
997 static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
998 		unsigned int *a, unsigned int *b)
999 {
1000 	unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp;
1001 	unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0;
1002 	unsigned int last_error, current_error;
1003 
1004 	/* calculate (divisor_a+1) x (divisor_b+1) */
1005 	temp = input_clk / (22 * fscl);
1006 
1007 	/*
1008 	 * If the calculated value is negative or 0, the fscl input is out of
1009 	 * range. Return error.
1010 	 */
1011 	if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX)))
1012 		return -EINVAL;
1013 
1014 	last_error = -1;
1015 	for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) {
1016 		div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1));
1017 
1018 		if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX))
1019 			continue;
1020 		div_b--;
1021 
1022 		actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1));
1023 
1024 		if (actual_fscl > fscl)
1025 			continue;
1026 
1027 		current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) :
1028 							(fscl - actual_fscl));
1029 
1030 		if (last_error > current_error) {
1031 			calc_div_a = div_a;
1032 			calc_div_b = div_b;
1033 			best_fscl = actual_fscl;
1034 			last_error = current_error;
1035 		}
1036 	}
1037 
1038 	*a = calc_div_a;
1039 	*b = calc_div_b;
1040 	*f = best_fscl;
1041 
1042 	return 0;
1043 }
1044 
1045 /**
1046  * cdns_i2c_setclk - This function sets the serial clock rate for the I2C device
1047  * @clk_in:	I2C clock input frequency in Hz
1048  * @id:		Pointer to the I2C device structure
1049  *
1050  * The device must be idle rather than busy transferring data before setting
1051  * these device options.
1052  * The data rate is set by values in the control register.
1053  * The formula for determining the correct register values is
1054  *	Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
1055  * See the hardware data sheet for a full explanation of setting the serial
1056  * clock rate. The clock can not be faster than the input clock divide by 22.
1057  * The two most common clock rates are 100KHz and 400KHz.
1058  *
1059  * Return: 0 on success, negative error otherwise
1060  */
1061 static int cdns_i2c_setclk(unsigned long clk_in, struct cdns_i2c *id)
1062 {
1063 	unsigned int div_a, div_b;
1064 	unsigned int ctrl_reg;
1065 	int ret = 0;
1066 	unsigned long fscl = id->i2c_clk;
1067 
1068 	ret = cdns_i2c_calc_divs(&fscl, clk_in, &div_a, &div_b);
1069 	if (ret)
1070 		return ret;
1071 
1072 	ctrl_reg = id->ctrl_reg;
1073 	ctrl_reg &= ~(CDNS_I2C_CR_DIVA_MASK | CDNS_I2C_CR_DIVB_MASK);
1074 	ctrl_reg |= ((div_a << CDNS_I2C_CR_DIVA_SHIFT) |
1075 			(div_b << CDNS_I2C_CR_DIVB_SHIFT));
1076 	id->ctrl_reg = ctrl_reg;
1077 	cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
1078 #if IS_ENABLED(CONFIG_I2C_SLAVE)
1079 	id->ctrl_reg_diva_divb = ctrl_reg & (CDNS_I2C_CR_DIVA_MASK |
1080 				 CDNS_I2C_CR_DIVB_MASK);
1081 #endif
1082 	return 0;
1083 }
1084 
1085 /**
1086  * cdns_i2c_clk_notifier_cb - Clock rate change callback
1087  * @nb:		Pointer to notifier block
1088  * @event:	Notification reason
1089  * @data:	Pointer to notification data object
1090  *
1091  * This function is called when the cdns_i2c input clock frequency changes.
1092  * The callback checks whether a valid bus frequency can be generated after the
1093  * change. If so, the change is acknowledged, otherwise the change is aborted.
1094  * New dividers are written to the HW in the pre- or post change notification
1095  * depending on the scaling direction.
1096  *
1097  * Return:	NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
1098  *		to acknowledge the change, NOTIFY_DONE if the notification is
1099  *		considered irrelevant.
1100  */
1101 static int cdns_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
1102 		event, void *data)
1103 {
1104 	struct clk_notifier_data *ndata = data;
1105 	struct cdns_i2c *id = to_cdns_i2c(nb);
1106 
1107 	if (pm_runtime_suspended(id->dev))
1108 		return NOTIFY_OK;
1109 
1110 	switch (event) {
1111 	case PRE_RATE_CHANGE:
1112 	{
1113 		unsigned long input_clk = ndata->new_rate;
1114 		unsigned long fscl = id->i2c_clk;
1115 		unsigned int div_a, div_b;
1116 		int ret;
1117 
1118 		ret = cdns_i2c_calc_divs(&fscl, input_clk, &div_a, &div_b);
1119 		if (ret) {
1120 			dev_warn(id->adap.dev.parent,
1121 					"clock rate change rejected\n");
1122 			return NOTIFY_STOP;
1123 		}
1124 
1125 		/* scale up */
1126 		if (ndata->new_rate > ndata->old_rate)
1127 			cdns_i2c_setclk(ndata->new_rate, id);
1128 
1129 		return NOTIFY_OK;
1130 	}
1131 	case POST_RATE_CHANGE:
1132 		id->input_clk = ndata->new_rate;
1133 		/* scale down */
1134 		if (ndata->new_rate < ndata->old_rate)
1135 			cdns_i2c_setclk(ndata->new_rate, id);
1136 		return NOTIFY_OK;
1137 	case ABORT_RATE_CHANGE:
1138 		/* scale up */
1139 		if (ndata->new_rate > ndata->old_rate)
1140 			cdns_i2c_setclk(ndata->old_rate, id);
1141 		return NOTIFY_OK;
1142 	default:
1143 		return NOTIFY_DONE;
1144 	}
1145 }
1146 
1147 /**
1148  * cdns_i2c_runtime_suspend -  Runtime suspend method for the driver
1149  * @dev:	Address of the platform_device structure
1150  *
1151  * Put the driver into low power mode.
1152  *
1153  * Return: 0 always
1154  */
1155 static int __maybe_unused cdns_i2c_runtime_suspend(struct device *dev)
1156 {
1157 	struct cdns_i2c *xi2c = dev_get_drvdata(dev);
1158 
1159 	clk_disable(xi2c->clk);
1160 
1161 	return 0;
1162 }
1163 
1164 /**
1165  * cdns_i2c_init -  Controller initialisation
1166  * @id:		Device private data structure
1167  *
1168  * Initialise the i2c controller.
1169  *
1170  */
1171 static void cdns_i2c_init(struct cdns_i2c *id)
1172 {
1173 	cdns_i2c_writereg(id->ctrl_reg, CDNS_I2C_CR_OFFSET);
1174 	/*
1175 	 * Cadence I2C controller has a bug wherein it generates
1176 	 * invalid read transaction after HW timeout in master receiver mode.
1177 	 * HW timeout is not used by this driver and the interrupt is disabled.
1178 	 * But the feature itself cannot be disabled. Hence maximum value
1179 	 * is written to this register to reduce the chances of error.
1180 	 */
1181 	cdns_i2c_writereg(CDNS_I2C_TIMEOUT_MAX, CDNS_I2C_TIME_OUT_OFFSET);
1182 }
1183 
1184 /**
1185  * cdns_i2c_runtime_resume - Runtime resume
1186  * @dev:	Address of the platform_device structure
1187  *
1188  * Runtime resume callback.
1189  *
1190  * Return: 0 on success and error value on error
1191  */
1192 static int __maybe_unused cdns_i2c_runtime_resume(struct device *dev)
1193 {
1194 	struct cdns_i2c *xi2c = dev_get_drvdata(dev);
1195 	int ret;
1196 
1197 	ret = clk_enable(xi2c->clk);
1198 	if (ret) {
1199 		dev_err(dev, "Cannot enable clock.\n");
1200 		return ret;
1201 	}
1202 	cdns_i2c_init(xi2c);
1203 
1204 	return 0;
1205 }
1206 
1207 static const struct dev_pm_ops cdns_i2c_dev_pm_ops = {
1208 	SET_RUNTIME_PM_OPS(cdns_i2c_runtime_suspend,
1209 			   cdns_i2c_runtime_resume, NULL)
1210 };
1211 
1212 static const struct cdns_platform_data r1p10_i2c_def = {
1213 	.quirks = CDNS_I2C_BROKEN_HOLD_BIT,
1214 };
1215 
1216 static const struct of_device_id cdns_i2c_of_match[] = {
1217 	{ .compatible = "cdns,i2c-r1p10", .data = &r1p10_i2c_def },
1218 	{ .compatible = "cdns,i2c-r1p14",},
1219 	{ /* end of table */ }
1220 };
1221 MODULE_DEVICE_TABLE(of, cdns_i2c_of_match);
1222 
1223 /**
1224  * cdns_i2c_probe - Platform registration call
1225  * @pdev:	Handle to the platform device structure
1226  *
1227  * This function does all the memory allocation and registration for the i2c
1228  * device. User can modify the address mode to 10 bit address mode using the
1229  * ioctl call with option I2C_TENBIT.
1230  *
1231  * Return: 0 on success, negative error otherwise
1232  */
1233 static int cdns_i2c_probe(struct platform_device *pdev)
1234 {
1235 	struct resource *r_mem;
1236 	struct cdns_i2c *id;
1237 	int ret;
1238 	const struct of_device_id *match;
1239 
1240 	id = devm_kzalloc(&pdev->dev, sizeof(*id), GFP_KERNEL);
1241 	if (!id)
1242 		return -ENOMEM;
1243 
1244 	id->dev = &pdev->dev;
1245 	platform_set_drvdata(pdev, id);
1246 
1247 	match = of_match_node(cdns_i2c_of_match, pdev->dev.of_node);
1248 	if (match && match->data) {
1249 		const struct cdns_platform_data *data = match->data;
1250 		id->quirks = data->quirks;
1251 	}
1252 
1253 	id->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &r_mem);
1254 	if (IS_ERR(id->membase))
1255 		return PTR_ERR(id->membase);
1256 
1257 	ret = platform_get_irq(pdev, 0);
1258 	if (ret < 0)
1259 		return ret;
1260 	id->irq = ret;
1261 
1262 	id->adap.owner = THIS_MODULE;
1263 	id->adap.dev.of_node = pdev->dev.of_node;
1264 	id->adap.algo = &cdns_i2c_algo;
1265 	id->adap.timeout = CDNS_I2C_TIMEOUT;
1266 	id->adap.retries = 3;		/* Default retry value. */
1267 	id->adap.algo_data = id;
1268 	id->adap.dev.parent = &pdev->dev;
1269 	init_completion(&id->xfer_done);
1270 	snprintf(id->adap.name, sizeof(id->adap.name),
1271 		 "Cadence I2C at %08lx", (unsigned long)r_mem->start);
1272 
1273 	id->clk = devm_clk_get(&pdev->dev, NULL);
1274 	if (IS_ERR(id->clk))
1275 		return dev_err_probe(&pdev->dev, PTR_ERR(id->clk),
1276 				     "input clock not found.\n");
1277 
1278 	ret = clk_prepare_enable(id->clk);
1279 	if (ret)
1280 		dev_err(&pdev->dev, "Unable to enable clock.\n");
1281 
1282 	pm_runtime_set_autosuspend_delay(id->dev, CNDS_I2C_PM_TIMEOUT);
1283 	pm_runtime_use_autosuspend(id->dev);
1284 	pm_runtime_set_active(id->dev);
1285 	pm_runtime_enable(id->dev);
1286 
1287 	id->clk_rate_change_nb.notifier_call = cdns_i2c_clk_notifier_cb;
1288 	if (clk_notifier_register(id->clk, &id->clk_rate_change_nb))
1289 		dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
1290 	id->input_clk = clk_get_rate(id->clk);
1291 
1292 	ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
1293 			&id->i2c_clk);
1294 	if (ret || (id->i2c_clk > I2C_MAX_FAST_MODE_FREQ))
1295 		id->i2c_clk = I2C_MAX_STANDARD_MODE_FREQ;
1296 
1297 #if IS_ENABLED(CONFIG_I2C_SLAVE)
1298 	/* Set initial mode to master */
1299 	id->dev_mode = CDNS_I2C_MODE_MASTER;
1300 	id->slave_state = CDNS_I2C_SLAVE_STATE_IDLE;
1301 #endif
1302 	id->ctrl_reg = CDNS_I2C_CR_ACK_EN | CDNS_I2C_CR_NEA | CDNS_I2C_CR_MS;
1303 
1304 	ret = cdns_i2c_setclk(id->input_clk, id);
1305 	if (ret) {
1306 		dev_err(&pdev->dev, "invalid SCL clock: %u Hz\n", id->i2c_clk);
1307 		ret = -EINVAL;
1308 		goto err_clk_dis;
1309 	}
1310 
1311 	ret = devm_request_irq(&pdev->dev, id->irq, cdns_i2c_isr, 0,
1312 				 DRIVER_NAME, id);
1313 	if (ret) {
1314 		dev_err(&pdev->dev, "cannot get irq %d\n", id->irq);
1315 		goto err_clk_dis;
1316 	}
1317 	cdns_i2c_init(id);
1318 
1319 	ret = i2c_add_adapter(&id->adap);
1320 	if (ret < 0)
1321 		goto err_clk_dis;
1322 
1323 	dev_info(&pdev->dev, "%u kHz mmio %08lx irq %d\n",
1324 		 id->i2c_clk / 1000, (unsigned long)r_mem->start, id->irq);
1325 
1326 	return 0;
1327 
1328 err_clk_dis:
1329 	clk_notifier_unregister(id->clk, &id->clk_rate_change_nb);
1330 	clk_disable_unprepare(id->clk);
1331 	pm_runtime_disable(&pdev->dev);
1332 	pm_runtime_set_suspended(&pdev->dev);
1333 	return ret;
1334 }
1335 
1336 /**
1337  * cdns_i2c_remove - Unregister the device after releasing the resources
1338  * @pdev:	Handle to the platform device structure
1339  *
1340  * This function frees all the resources allocated to the device.
1341  *
1342  * Return: 0 always
1343  */
1344 static int cdns_i2c_remove(struct platform_device *pdev)
1345 {
1346 	struct cdns_i2c *id = platform_get_drvdata(pdev);
1347 
1348 	pm_runtime_disable(&pdev->dev);
1349 	pm_runtime_set_suspended(&pdev->dev);
1350 	pm_runtime_dont_use_autosuspend(&pdev->dev);
1351 
1352 	i2c_del_adapter(&id->adap);
1353 	clk_notifier_unregister(id->clk, &id->clk_rate_change_nb);
1354 	clk_disable_unprepare(id->clk);
1355 
1356 	return 0;
1357 }
1358 
1359 static struct platform_driver cdns_i2c_drv = {
1360 	.driver = {
1361 		.name  = DRIVER_NAME,
1362 		.of_match_table = cdns_i2c_of_match,
1363 		.pm = &cdns_i2c_dev_pm_ops,
1364 	},
1365 	.probe  = cdns_i2c_probe,
1366 	.remove = cdns_i2c_remove,
1367 };
1368 
1369 module_platform_driver(cdns_i2c_drv);
1370 
1371 MODULE_AUTHOR("Xilinx Inc.");
1372 MODULE_DESCRIPTION("Cadence I2C bus driver");
1373 MODULE_LICENSE("GPL");
1374