Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * I2C bus driver for the Cadence I2C controller.
0004  *
0005  * Copyright (C) 2009 - 2014 Xilinx, Inc.
0006  */
0007 
0008 #include <linux/clk.h>
0009 #include <linux/delay.h>
0010 #include <linux/i2c.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/io.h>
0013 #include <linux/module.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/of.h>
0016 #include <linux/pm_runtime.h>
0017 
0018 /* Register offsets for the I2C device. */
0019 #define CDNS_I2C_CR_OFFSET      0x00 /* Control Register, RW */
0020 #define CDNS_I2C_SR_OFFSET      0x04 /* Status Register, RO */
0021 #define CDNS_I2C_ADDR_OFFSET        0x08 /* I2C Address Register, RW */
0022 #define CDNS_I2C_DATA_OFFSET        0x0C /* I2C Data Register, RW */
0023 #define CDNS_I2C_ISR_OFFSET     0x10 /* IRQ Status Register, RW */
0024 #define CDNS_I2C_XFER_SIZE_OFFSET   0x14 /* Transfer Size Register, RW */
0025 #define CDNS_I2C_TIME_OUT_OFFSET    0x1C /* Time Out Register, RW */
0026 #define CDNS_I2C_IMR_OFFSET     0x20 /* IRQ Mask Register, RO */
0027 #define CDNS_I2C_IER_OFFSET     0x24 /* IRQ Enable Register, WO */
0028 #define CDNS_I2C_IDR_OFFSET     0x28 /* IRQ Disable Register, WO */
0029 
0030 /* Control Register Bit mask definitions */
0031 #define CDNS_I2C_CR_HOLD        BIT(4) /* Hold Bus bit */
0032 #define CDNS_I2C_CR_ACK_EN      BIT(3)
0033 #define CDNS_I2C_CR_NEA         BIT(2)
0034 #define CDNS_I2C_CR_MS          BIT(1)
0035 /* Read or Write Master transfer 0 = Transmitter, 1 = Receiver */
0036 #define CDNS_I2C_CR_RW          BIT(0)
0037 /* 1 = Auto init FIFO to zeroes */
0038 #define CDNS_I2C_CR_CLR_FIFO        BIT(6)
0039 #define CDNS_I2C_CR_DIVA_SHIFT      14
0040 #define CDNS_I2C_CR_DIVA_MASK       (3 << CDNS_I2C_CR_DIVA_SHIFT)
0041 #define CDNS_I2C_CR_DIVB_SHIFT      8
0042 #define CDNS_I2C_CR_DIVB_MASK       (0x3f << CDNS_I2C_CR_DIVB_SHIFT)
0043 
0044 #define CDNS_I2C_CR_MASTER_EN_MASK  (CDNS_I2C_CR_NEA | \
0045                      CDNS_I2C_CR_ACK_EN | \
0046                      CDNS_I2C_CR_MS)
0047 
0048 #define CDNS_I2C_CR_SLAVE_EN_MASK   ~CDNS_I2C_CR_MASTER_EN_MASK
0049 
0050 /* Status Register Bit mask definitions */
0051 #define CDNS_I2C_SR_BA      BIT(8)
0052 #define CDNS_I2C_SR_TXDV    BIT(6)
0053 #define CDNS_I2C_SR_RXDV    BIT(5)
0054 #define CDNS_I2C_SR_RXRW    BIT(3)
0055 
0056 /*
0057  * I2C Address Register Bit mask definitions
0058  * Normal addressing mode uses [6:0] bits. Extended addressing mode uses [9:0]
0059  * bits. A write access to this register always initiates a transfer if the I2C
0060  * is in master mode.
0061  */
0062 #define CDNS_I2C_ADDR_MASK  0x000003FF /* I2C Address Mask */
0063 
0064 /*
0065  * I2C Interrupt Registers Bit mask definitions
0066  * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
0067  * bit definitions.
0068  */
0069 #define CDNS_I2C_IXR_ARB_LOST       BIT(9)
0070 #define CDNS_I2C_IXR_RX_UNF     BIT(7)
0071 #define CDNS_I2C_IXR_TX_OVF     BIT(6)
0072 #define CDNS_I2C_IXR_RX_OVF     BIT(5)
0073 #define CDNS_I2C_IXR_SLV_RDY        BIT(4)
0074 #define CDNS_I2C_IXR_TO         BIT(3)
0075 #define CDNS_I2C_IXR_NACK       BIT(2)
0076 #define CDNS_I2C_IXR_DATA       BIT(1)
0077 #define CDNS_I2C_IXR_COMP       BIT(0)
0078 
0079 #define CDNS_I2C_IXR_ALL_INTR_MASK  (CDNS_I2C_IXR_ARB_LOST | \
0080                      CDNS_I2C_IXR_RX_UNF | \
0081                      CDNS_I2C_IXR_TX_OVF | \
0082                      CDNS_I2C_IXR_RX_OVF | \
0083                      CDNS_I2C_IXR_SLV_RDY | \
0084                      CDNS_I2C_IXR_TO | \
0085                      CDNS_I2C_IXR_NACK | \
0086                      CDNS_I2C_IXR_DATA | \
0087                      CDNS_I2C_IXR_COMP)
0088 
0089 #define CDNS_I2C_IXR_ERR_INTR_MASK  (CDNS_I2C_IXR_ARB_LOST | \
0090                      CDNS_I2C_IXR_RX_UNF | \
0091                      CDNS_I2C_IXR_TX_OVF | \
0092                      CDNS_I2C_IXR_RX_OVF | \
0093                      CDNS_I2C_IXR_NACK)
0094 
0095 #define CDNS_I2C_ENABLED_INTR_MASK  (CDNS_I2C_IXR_ARB_LOST | \
0096                      CDNS_I2C_IXR_RX_UNF | \
0097                      CDNS_I2C_IXR_TX_OVF | \
0098                      CDNS_I2C_IXR_RX_OVF | \
0099                      CDNS_I2C_IXR_NACK | \
0100                      CDNS_I2C_IXR_DATA | \
0101                      CDNS_I2C_IXR_COMP)
0102 
0103 #define CDNS_I2C_IXR_SLAVE_INTR_MASK    (CDNS_I2C_IXR_RX_UNF | \
0104                      CDNS_I2C_IXR_TX_OVF | \
0105                      CDNS_I2C_IXR_RX_OVF | \
0106                      CDNS_I2C_IXR_TO | \
0107                      CDNS_I2C_IXR_NACK | \
0108                      CDNS_I2C_IXR_DATA | \
0109                      CDNS_I2C_IXR_COMP)
0110 
0111 #define CDNS_I2C_TIMEOUT        msecs_to_jiffies(1000)
0112 /* timeout for pm runtime autosuspend */
0113 #define CNDS_I2C_PM_TIMEOUT     1000    /* ms */
0114 
0115 #define CDNS_I2C_FIFO_DEPTH     16
0116 /* FIFO depth at which the DATA interrupt occurs */
0117 #define CDNS_I2C_DATA_INTR_DEPTH    (CDNS_I2C_FIFO_DEPTH - 2)
0118 #define CDNS_I2C_MAX_TRANSFER_SIZE  255
0119 /* Transfer size in multiples of data interrupt depth */
0120 #define CDNS_I2C_TRANSFER_SIZE  (CDNS_I2C_MAX_TRANSFER_SIZE - 3)
0121 
0122 #define DRIVER_NAME     "cdns-i2c"
0123 
0124 #define CDNS_I2C_DIVA_MAX   4
0125 #define CDNS_I2C_DIVB_MAX   64
0126 
0127 #define CDNS_I2C_TIMEOUT_MAX    0xFF
0128 
0129 #define CDNS_I2C_BROKEN_HOLD_BIT    BIT(0)
0130 
0131 #define cdns_i2c_readreg(offset)       readl_relaxed(id->membase + offset)
0132 #define cdns_i2c_writereg(val, offset) writel_relaxed(val, id->membase + offset)
0133 
0134 #if IS_ENABLED(CONFIG_I2C_SLAVE)
0135 /**
0136  * enum cdns_i2c_mode - I2C Controller current operating mode
0137  *
0138  * @CDNS_I2C_MODE_SLAVE:       I2C controller operating in slave mode
0139  * @CDNS_I2C_MODE_MASTER:      I2C Controller operating in master mode
0140  */
0141 enum cdns_i2c_mode {
0142     CDNS_I2C_MODE_SLAVE,
0143     CDNS_I2C_MODE_MASTER,
0144 };
0145 
0146 /**
0147  * enum cdns_i2c_slave_state - Slave state when I2C is operating in slave mode
0148  *
0149  * @CDNS_I2C_SLAVE_STATE_IDLE: I2C slave idle
0150  * @CDNS_I2C_SLAVE_STATE_SEND: I2C slave sending data to master
0151  * @CDNS_I2C_SLAVE_STATE_RECV: I2C slave receiving data from master
0152  */
0153 enum cdns_i2c_slave_state {
0154     CDNS_I2C_SLAVE_STATE_IDLE,
0155     CDNS_I2C_SLAVE_STATE_SEND,
0156     CDNS_I2C_SLAVE_STATE_RECV,
0157 };
0158 #endif
0159 
0160 /**
0161  * struct cdns_i2c - I2C device private data structure
0162  *
0163  * @dev:        Pointer to device structure
0164  * @membase:        Base address of the I2C device
0165  * @adap:       I2C adapter instance
0166  * @p_msg:      Message pointer
0167  * @err_status:     Error status in Interrupt Status Register
0168  * @xfer_done:      Transfer complete status
0169  * @p_send_buf:     Pointer to transmit buffer
0170  * @p_recv_buf:     Pointer to receive buffer
0171  * @send_count:     Number of bytes still expected to send
0172  * @recv_count:     Number of bytes still expected to receive
0173  * @curr_recv_count:    Number of bytes to be received in current transfer
0174  * @irq:        IRQ number
0175  * @input_clk:      Input clock to I2C controller
0176  * @i2c_clk:        Maximum I2C clock speed
0177  * @bus_hold_flag:  Flag used in repeated start for clearing HOLD bit
0178  * @clk:        Pointer to struct clk
0179  * @clk_rate_change_nb: Notifier block for clock rate changes
0180  * @quirks:     flag for broken hold bit usage in r1p10
0181  * @ctrl_reg:       Cached value of the control register.
0182  * @ctrl_reg_diva_divb: value of fields DIV_A and DIV_B from CR register
0183  * @slave:      Registered slave instance.
0184  * @dev_mode:       I2C operating role(master/slave).
0185  * @slave_state:    I2C Slave state(idle/read/write).
0186  */
0187 struct cdns_i2c {
0188     struct device       *dev;
0189     void __iomem *membase;
0190     struct i2c_adapter adap;
0191     struct i2c_msg *p_msg;
0192     int err_status;
0193     struct completion xfer_done;
0194     unsigned char *p_send_buf;
0195     unsigned char *p_recv_buf;
0196     unsigned int send_count;
0197     unsigned int recv_count;
0198     unsigned int curr_recv_count;
0199     int irq;
0200     unsigned long input_clk;
0201     unsigned int i2c_clk;
0202     unsigned int bus_hold_flag;
0203     struct clk *clk;
0204     struct notifier_block clk_rate_change_nb;
0205     u32 quirks;
0206     u32 ctrl_reg;
0207 #if IS_ENABLED(CONFIG_I2C_SLAVE)
0208     u16 ctrl_reg_diva_divb;
0209     struct i2c_client *slave;
0210     enum cdns_i2c_mode dev_mode;
0211     enum cdns_i2c_slave_state slave_state;
0212 #endif
0213 };
0214 
0215 struct cdns_platform_data {
0216     u32 quirks;
0217 };
0218 
0219 #define to_cdns_i2c(_nb)    container_of(_nb, struct cdns_i2c, \
0220                          clk_rate_change_nb)
0221 
0222 /**
0223  * cdns_i2c_clear_bus_hold - Clear bus hold bit
0224  * @id: Pointer to driver data struct
0225  *
0226  * Helper to clear the controller's bus hold bit.
0227  */
0228 static void cdns_i2c_clear_bus_hold(struct cdns_i2c *id)
0229 {
0230     u32 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
0231     if (reg & CDNS_I2C_CR_HOLD)
0232         cdns_i2c_writereg(reg & ~CDNS_I2C_CR_HOLD, CDNS_I2C_CR_OFFSET);
0233 }
0234 
0235 static inline bool cdns_is_holdquirk(struct cdns_i2c *id, bool hold_wrkaround)
0236 {
0237     return (hold_wrkaround &&
0238         (id->curr_recv_count == CDNS_I2C_FIFO_DEPTH + 1));
0239 }
0240 
0241 #if IS_ENABLED(CONFIG_I2C_SLAVE)
0242 static void cdns_i2c_set_mode(enum cdns_i2c_mode mode, struct cdns_i2c *id)
0243 {
0244     /* Disable all interrupts */
0245     cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK, CDNS_I2C_IDR_OFFSET);
0246 
0247     /* Clear FIFO and transfer size */
0248     cdns_i2c_writereg(CDNS_I2C_CR_CLR_FIFO, CDNS_I2C_CR_OFFSET);
0249 
0250     /* Update device mode and state */
0251     id->dev_mode = mode;
0252     id->slave_state = CDNS_I2C_SLAVE_STATE_IDLE;
0253 
0254     switch (mode) {
0255     case CDNS_I2C_MODE_MASTER:
0256         /* Enable i2c master */
0257         cdns_i2c_writereg(id->ctrl_reg_diva_divb |
0258                   CDNS_I2C_CR_MASTER_EN_MASK,
0259                   CDNS_I2C_CR_OFFSET);
0260         /*
0261          * This delay is needed to give the IP some time to switch to
0262          * the master mode. With lower values(like 110 us) i2cdetect
0263          * will not detect any slave and without this delay, the IP will
0264          * trigger a timeout interrupt.
0265          */
0266         usleep_range(115, 125);
0267         break;
0268     case CDNS_I2C_MODE_SLAVE:
0269         /* Enable i2c slave */
0270         cdns_i2c_writereg(id->ctrl_reg_diva_divb &
0271                   CDNS_I2C_CR_SLAVE_EN_MASK,
0272                   CDNS_I2C_CR_OFFSET);
0273 
0274         /* Setting slave address */
0275         cdns_i2c_writereg(id->slave->addr & CDNS_I2C_ADDR_MASK,
0276                   CDNS_I2C_ADDR_OFFSET);
0277 
0278         /* Enable slave send/receive interrupts */
0279         cdns_i2c_writereg(CDNS_I2C_IXR_SLAVE_INTR_MASK,
0280                   CDNS_I2C_IER_OFFSET);
0281         break;
0282     }
0283 }
0284 
0285 static void cdns_i2c_slave_rcv_data(struct cdns_i2c *id)
0286 {
0287     u8 bytes;
0288     unsigned char data;
0289 
0290     /* Prepare backend for data reception */
0291     if (id->slave_state == CDNS_I2C_SLAVE_STATE_IDLE) {
0292         id->slave_state = CDNS_I2C_SLAVE_STATE_RECV;
0293         i2c_slave_event(id->slave, I2C_SLAVE_WRITE_REQUESTED, NULL);
0294     }
0295 
0296     /* Fetch number of bytes to receive */
0297     bytes = cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
0298 
0299     /* Read data and send to backend */
0300     while (bytes--) {
0301         data = cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET);
0302         i2c_slave_event(id->slave, I2C_SLAVE_WRITE_RECEIVED, &data);
0303     }
0304 }
0305 
0306 static void cdns_i2c_slave_send_data(struct cdns_i2c *id)
0307 {
0308     u8 data;
0309 
0310     /* Prepare backend for data transmission */
0311     if (id->slave_state == CDNS_I2C_SLAVE_STATE_IDLE) {
0312         id->slave_state = CDNS_I2C_SLAVE_STATE_SEND;
0313         i2c_slave_event(id->slave, I2C_SLAVE_READ_REQUESTED, &data);
0314     } else {
0315         i2c_slave_event(id->slave, I2C_SLAVE_READ_PROCESSED, &data);
0316     }
0317 
0318     /* Send data over bus */
0319     cdns_i2c_writereg(data, CDNS_I2C_DATA_OFFSET);
0320 }
0321 
0322 /**
0323  * cdns_i2c_slave_isr - Interrupt handler for the I2C device in slave role
0324  * @ptr:       Pointer to I2C device private data
0325  *
0326  * This function handles the data interrupt and transfer complete interrupt of
0327  * the I2C device in slave role.
0328  *
0329  * Return: IRQ_HANDLED always
0330  */
0331 static irqreturn_t cdns_i2c_slave_isr(void *ptr)
0332 {
0333     struct cdns_i2c *id = ptr;
0334     unsigned int isr_status, i2c_status;
0335 
0336     /* Fetch the interrupt status */
0337     isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
0338     cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
0339 
0340     /* Ignore masked interrupts */
0341     isr_status &= ~cdns_i2c_readreg(CDNS_I2C_IMR_OFFSET);
0342 
0343     /* Fetch transfer mode (send/receive) */
0344     i2c_status = cdns_i2c_readreg(CDNS_I2C_SR_OFFSET);
0345 
0346     /* Handle data send/receive */
0347     if (i2c_status & CDNS_I2C_SR_RXRW) {
0348         /* Send data to master */
0349         if (isr_status & CDNS_I2C_IXR_DATA)
0350             cdns_i2c_slave_send_data(id);
0351 
0352         if (isr_status & CDNS_I2C_IXR_COMP) {
0353             id->slave_state = CDNS_I2C_SLAVE_STATE_IDLE;
0354             i2c_slave_event(id->slave, I2C_SLAVE_STOP, NULL);
0355         }
0356     } else {
0357         /* Receive data from master */
0358         if (isr_status & CDNS_I2C_IXR_DATA)
0359             cdns_i2c_slave_rcv_data(id);
0360 
0361         if (isr_status & CDNS_I2C_IXR_COMP) {
0362             cdns_i2c_slave_rcv_data(id);
0363             id->slave_state = CDNS_I2C_SLAVE_STATE_IDLE;
0364             i2c_slave_event(id->slave, I2C_SLAVE_STOP, NULL);
0365         }
0366     }
0367 
0368     /* Master indicated xfer stop or fifo underflow/overflow */
0369     if (isr_status & (CDNS_I2C_IXR_NACK | CDNS_I2C_IXR_RX_OVF |
0370               CDNS_I2C_IXR_RX_UNF | CDNS_I2C_IXR_TX_OVF)) {
0371         id->slave_state = CDNS_I2C_SLAVE_STATE_IDLE;
0372         i2c_slave_event(id->slave, I2C_SLAVE_STOP, NULL);
0373         cdns_i2c_writereg(CDNS_I2C_CR_CLR_FIFO, CDNS_I2C_CR_OFFSET);
0374     }
0375 
0376     return IRQ_HANDLED;
0377 }
0378 #endif
0379 
0380 /**
0381  * cdns_i2c_master_isr - Interrupt handler for the I2C device in master role
0382  * @ptr:       Pointer to I2C device private data
0383  *
0384  * This function handles the data interrupt, transfer complete interrupt and
0385  * the error interrupts of the I2C device in master role.
0386  *
0387  * Return: IRQ_HANDLED always
0388  */
0389 static irqreturn_t cdns_i2c_master_isr(void *ptr)
0390 {
0391     unsigned int isr_status, avail_bytes;
0392     unsigned int bytes_to_send;
0393     bool updatetx;
0394     struct cdns_i2c *id = ptr;
0395     /* Signal completion only after everything is updated */
0396     int done_flag = 0;
0397     irqreturn_t status = IRQ_NONE;
0398 
0399     isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
0400     cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
0401     id->err_status = 0;
0402 
0403     /* Handling nack and arbitration lost interrupt */
0404     if (isr_status & (CDNS_I2C_IXR_NACK | CDNS_I2C_IXR_ARB_LOST)) {
0405         done_flag = 1;
0406         status = IRQ_HANDLED;
0407     }
0408 
0409     /*
0410      * Check if transfer size register needs to be updated again for a
0411      * large data receive operation.
0412      */
0413     updatetx = id->recv_count > id->curr_recv_count;
0414 
0415     /* When receiving, handle data interrupt and completion interrupt */
0416     if (id->p_recv_buf &&
0417         ((isr_status & CDNS_I2C_IXR_COMP) ||
0418          (isr_status & CDNS_I2C_IXR_DATA))) {
0419         /* Read data if receive data valid is set */
0420         while (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) &
0421                CDNS_I2C_SR_RXDV) {
0422             if (id->recv_count > 0) {
0423                 *(id->p_recv_buf)++ =
0424                     cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET);
0425                 id->recv_count--;
0426                 id->curr_recv_count--;
0427 
0428                 /*
0429                  * Clear hold bit that was set for FIFO control
0430                  * if RX data left is less than or equal to
0431                  * FIFO DEPTH unless repeated start is selected
0432                  */
0433                 if (id->recv_count <= CDNS_I2C_FIFO_DEPTH &&
0434                     !id->bus_hold_flag)
0435                     cdns_i2c_clear_bus_hold(id);
0436 
0437             } else {
0438                 dev_err(id->adap.dev.parent,
0439                     "xfer_size reg rollover. xfer aborted!\n");
0440                 id->err_status |= CDNS_I2C_IXR_TO;
0441                 break;
0442             }
0443 
0444             if (cdns_is_holdquirk(id, updatetx))
0445                 break;
0446         }
0447 
0448         /*
0449          * The controller sends NACK to the slave when transfer size
0450          * register reaches zero without considering the HOLD bit.
0451          * This workaround is implemented for large data transfers to
0452          * maintain transfer size non-zero while performing a large
0453          * receive operation.
0454          */
0455         if (cdns_is_holdquirk(id, updatetx)) {
0456             /* wait while fifo is full */
0457             while (cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET) !=
0458                    (id->curr_recv_count - CDNS_I2C_FIFO_DEPTH))
0459                 ;
0460 
0461             /*
0462              * Check number of bytes to be received against maximum
0463              * transfer size and update register accordingly.
0464              */
0465             if (((int)(id->recv_count) - CDNS_I2C_FIFO_DEPTH) >
0466                 CDNS_I2C_TRANSFER_SIZE) {
0467                 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
0468                           CDNS_I2C_XFER_SIZE_OFFSET);
0469                 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE +
0470                               CDNS_I2C_FIFO_DEPTH;
0471             } else {
0472                 cdns_i2c_writereg(id->recv_count -
0473                           CDNS_I2C_FIFO_DEPTH,
0474                           CDNS_I2C_XFER_SIZE_OFFSET);
0475                 id->curr_recv_count = id->recv_count;
0476             }
0477         }
0478 
0479         /* Clear hold (if not repeated start) and signal completion */
0480         if ((isr_status & CDNS_I2C_IXR_COMP) && !id->recv_count) {
0481             if (!id->bus_hold_flag)
0482                 cdns_i2c_clear_bus_hold(id);
0483             done_flag = 1;
0484         }
0485 
0486         status = IRQ_HANDLED;
0487     }
0488 
0489     /* When sending, handle transfer complete interrupt */
0490     if ((isr_status & CDNS_I2C_IXR_COMP) && !id->p_recv_buf) {
0491         /*
0492          * If there is more data to be sent, calculate the
0493          * space available in FIFO and fill with that many bytes.
0494          */
0495         if (id->send_count) {
0496             avail_bytes = CDNS_I2C_FIFO_DEPTH -
0497                 cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
0498             if (id->send_count > avail_bytes)
0499                 bytes_to_send = avail_bytes;
0500             else
0501                 bytes_to_send = id->send_count;
0502 
0503             while (bytes_to_send--) {
0504                 cdns_i2c_writereg(
0505                     (*(id->p_send_buf)++),
0506                      CDNS_I2C_DATA_OFFSET);
0507                 id->send_count--;
0508             }
0509         } else {
0510             /*
0511              * Signal the completion of transaction and
0512              * clear the hold bus bit if there are no
0513              * further messages to be processed.
0514              */
0515             done_flag = 1;
0516         }
0517         if (!id->send_count && !id->bus_hold_flag)
0518             cdns_i2c_clear_bus_hold(id);
0519 
0520         status = IRQ_HANDLED;
0521     }
0522 
0523     /* Update the status for errors */
0524     id->err_status |= isr_status & CDNS_I2C_IXR_ERR_INTR_MASK;
0525     if (id->err_status)
0526         status = IRQ_HANDLED;
0527 
0528     if (done_flag)
0529         complete(&id->xfer_done);
0530 
0531     return status;
0532 }
0533 
0534 /**
0535  * cdns_i2c_isr - Interrupt handler for the I2C device
0536  * @irq:    irq number for the I2C device
0537  * @ptr:    void pointer to cdns_i2c structure
0538  *
0539  * This function passes the control to slave/master based on current role of
0540  * i2c controller.
0541  *
0542  * Return: IRQ_HANDLED always
0543  */
0544 static irqreturn_t cdns_i2c_isr(int irq, void *ptr)
0545 {
0546 #if IS_ENABLED(CONFIG_I2C_SLAVE)
0547     struct cdns_i2c *id = ptr;
0548 
0549     if (id->dev_mode == CDNS_I2C_MODE_SLAVE)
0550         return cdns_i2c_slave_isr(ptr);
0551 #endif
0552     return cdns_i2c_master_isr(ptr);
0553 }
0554 
0555 /**
0556  * cdns_i2c_mrecv - Prepare and start a master receive operation
0557  * @id:     pointer to the i2c device structure
0558  */
0559 static void cdns_i2c_mrecv(struct cdns_i2c *id)
0560 {
0561     unsigned int ctrl_reg;
0562     unsigned int isr_status;
0563     unsigned long flags;
0564     bool hold_clear = false;
0565     bool irq_save = false;
0566 
0567     u32 addr;
0568 
0569     id->p_recv_buf = id->p_msg->buf;
0570     id->recv_count = id->p_msg->len;
0571 
0572     /* Put the controller in master receive mode and clear the FIFO */
0573     ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
0574     ctrl_reg |= CDNS_I2C_CR_RW | CDNS_I2C_CR_CLR_FIFO;
0575 
0576     /*
0577      * Receive up to I2C_SMBUS_BLOCK_MAX data bytes, plus one message length
0578      * byte, plus one checksum byte if PEC is enabled. p_msg->len will be 2 if
0579      * PEC is enabled, otherwise 1.
0580      */
0581     if (id->p_msg->flags & I2C_M_RECV_LEN)
0582         id->recv_count = I2C_SMBUS_BLOCK_MAX + id->p_msg->len;
0583 
0584     id->curr_recv_count = id->recv_count;
0585 
0586     /*
0587      * Check for the message size against FIFO depth and set the
0588      * 'hold bus' bit if it is greater than FIFO depth.
0589      */
0590     if (id->recv_count > CDNS_I2C_FIFO_DEPTH)
0591         ctrl_reg |= CDNS_I2C_CR_HOLD;
0592 
0593     cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
0594 
0595     /* Clear the interrupts in interrupt status register */
0596     isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
0597     cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
0598 
0599     /*
0600      * The no. of bytes to receive is checked against the limit of
0601      * max transfer size. Set transfer size register with no of bytes
0602      * receive if it is less than transfer size and transfer size if
0603      * it is more. Enable the interrupts.
0604      */
0605     if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
0606         cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
0607                   CDNS_I2C_XFER_SIZE_OFFSET);
0608         id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
0609     } else {
0610         cdns_i2c_writereg(id->recv_count, CDNS_I2C_XFER_SIZE_OFFSET);
0611     }
0612 
0613     /* Determine hold_clear based on number of bytes to receive and hold flag */
0614     if (!id->bus_hold_flag &&
0615         ((id->p_msg->flags & I2C_M_RECV_LEN) != I2C_M_RECV_LEN) &&
0616         (id->recv_count <= CDNS_I2C_FIFO_DEPTH)) {
0617         if (cdns_i2c_readreg(CDNS_I2C_CR_OFFSET) & CDNS_I2C_CR_HOLD) {
0618             hold_clear = true;
0619             if (id->quirks & CDNS_I2C_BROKEN_HOLD_BIT)
0620                 irq_save = true;
0621         }
0622     }
0623 
0624     addr = id->p_msg->addr;
0625     addr &= CDNS_I2C_ADDR_MASK;
0626 
0627     if (hold_clear) {
0628         ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET) & ~CDNS_I2C_CR_HOLD;
0629         /*
0630          * In case of Xilinx Zynq SOC, clear the HOLD bit before transfer size
0631          * register reaches '0'. This is an IP bug which causes transfer size
0632          * register overflow to 0xFF. To satisfy this timing requirement,
0633          * disable the interrupts on current processor core between register
0634          * writes to slave address register and control register.
0635          */
0636         if (irq_save)
0637             local_irq_save(flags);
0638 
0639         cdns_i2c_writereg(addr, CDNS_I2C_ADDR_OFFSET);
0640         cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
0641         /* Read it back to avoid bufferring and make sure write happens */
0642         cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
0643 
0644         if (irq_save)
0645             local_irq_restore(flags);
0646     } else {
0647         cdns_i2c_writereg(addr, CDNS_I2C_ADDR_OFFSET);
0648     }
0649 
0650     cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
0651 }
0652 
0653 /**
0654  * cdns_i2c_msend - Prepare and start a master send operation
0655  * @id:     pointer to the i2c device
0656  */
0657 static void cdns_i2c_msend(struct cdns_i2c *id)
0658 {
0659     unsigned int avail_bytes;
0660     unsigned int bytes_to_send;
0661     unsigned int ctrl_reg;
0662     unsigned int isr_status;
0663 
0664     id->p_recv_buf = NULL;
0665     id->p_send_buf = id->p_msg->buf;
0666     id->send_count = id->p_msg->len;
0667 
0668     /* Set the controller in Master transmit mode and clear the FIFO. */
0669     ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
0670     ctrl_reg &= ~CDNS_I2C_CR_RW;
0671     ctrl_reg |= CDNS_I2C_CR_CLR_FIFO;
0672 
0673     /*
0674      * Check for the message size against FIFO depth and set the
0675      * 'hold bus' bit if it is greater than FIFO depth.
0676      */
0677     if (id->send_count > CDNS_I2C_FIFO_DEPTH)
0678         ctrl_reg |= CDNS_I2C_CR_HOLD;
0679     cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
0680 
0681     /* Clear the interrupts in interrupt status register. */
0682     isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
0683     cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
0684 
0685     /*
0686      * Calculate the space available in FIFO. Check the message length
0687      * against the space available, and fill the FIFO accordingly.
0688      * Enable the interrupts.
0689      */
0690     avail_bytes = CDNS_I2C_FIFO_DEPTH -
0691                 cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
0692 
0693     if (id->send_count > avail_bytes)
0694         bytes_to_send = avail_bytes;
0695     else
0696         bytes_to_send = id->send_count;
0697 
0698     while (bytes_to_send--) {
0699         cdns_i2c_writereg((*(id->p_send_buf)++), CDNS_I2C_DATA_OFFSET);
0700         id->send_count--;
0701     }
0702 
0703     /*
0704      * Clear the bus hold flag if there is no more data
0705      * and if it is the last message.
0706      */
0707     if (!id->bus_hold_flag && !id->send_count)
0708         cdns_i2c_clear_bus_hold(id);
0709     /* Set the slave address in address register - triggers operation. */
0710     cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
0711                         CDNS_I2C_ADDR_OFFSET);
0712 
0713     cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
0714 }
0715 
0716 /**
0717  * cdns_i2c_master_reset - Reset the interface
0718  * @adap:   pointer to the i2c adapter driver instance
0719  *
0720  * This function cleanup the fifos, clear the hold bit and status
0721  * and disable the interrupts.
0722  */
0723 static void cdns_i2c_master_reset(struct i2c_adapter *adap)
0724 {
0725     struct cdns_i2c *id = adap->algo_data;
0726     u32 regval;
0727 
0728     /* Disable the interrupts */
0729     cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK, CDNS_I2C_IDR_OFFSET);
0730     /* Clear the hold bit and fifos */
0731     regval = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
0732     regval &= ~CDNS_I2C_CR_HOLD;
0733     regval |= CDNS_I2C_CR_CLR_FIFO;
0734     cdns_i2c_writereg(regval, CDNS_I2C_CR_OFFSET);
0735     /* Update the transfercount register to zero */
0736     cdns_i2c_writereg(0, CDNS_I2C_XFER_SIZE_OFFSET);
0737     /* Clear the interrupt status register */
0738     regval = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
0739     cdns_i2c_writereg(regval, CDNS_I2C_ISR_OFFSET);
0740     /* Clear the status register */
0741     regval = cdns_i2c_readreg(CDNS_I2C_SR_OFFSET);
0742     cdns_i2c_writereg(regval, CDNS_I2C_SR_OFFSET);
0743 }
0744 
0745 static int cdns_i2c_process_msg(struct cdns_i2c *id, struct i2c_msg *msg,
0746         struct i2c_adapter *adap)
0747 {
0748     unsigned long time_left, msg_timeout;
0749     u32 reg;
0750 
0751     id->p_msg = msg;
0752     id->err_status = 0;
0753     reinit_completion(&id->xfer_done);
0754 
0755     /* Check for the TEN Bit mode on each msg */
0756     reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
0757     if (msg->flags & I2C_M_TEN) {
0758         if (reg & CDNS_I2C_CR_NEA)
0759             cdns_i2c_writereg(reg & ~CDNS_I2C_CR_NEA,
0760                     CDNS_I2C_CR_OFFSET);
0761     } else {
0762         if (!(reg & CDNS_I2C_CR_NEA))
0763             cdns_i2c_writereg(reg | CDNS_I2C_CR_NEA,
0764                     CDNS_I2C_CR_OFFSET);
0765     }
0766 
0767     /* Check for the R/W flag on each msg */
0768     if (msg->flags & I2C_M_RD)
0769         cdns_i2c_mrecv(id);
0770     else
0771         cdns_i2c_msend(id);
0772 
0773     /* Minimal time to execute this message */
0774     msg_timeout = msecs_to_jiffies((1000 * msg->len * BITS_PER_BYTE) / id->i2c_clk);
0775     /* Plus some wiggle room */
0776     msg_timeout += msecs_to_jiffies(500);
0777 
0778     if (msg_timeout < adap->timeout)
0779         msg_timeout = adap->timeout;
0780 
0781     /* Wait for the signal of completion */
0782     time_left = wait_for_completion_timeout(&id->xfer_done, msg_timeout);
0783     if (time_left == 0) {
0784         cdns_i2c_master_reset(adap);
0785         dev_err(id->adap.dev.parent,
0786                 "timeout waiting on completion\n");
0787         return -ETIMEDOUT;
0788     }
0789 
0790     cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK,
0791               CDNS_I2C_IDR_OFFSET);
0792 
0793     /* If it is bus arbitration error, try again */
0794     if (id->err_status & CDNS_I2C_IXR_ARB_LOST)
0795         return -EAGAIN;
0796 
0797     if (msg->flags & I2C_M_RECV_LEN)
0798         msg->len += min_t(unsigned int, msg->buf[0], I2C_SMBUS_BLOCK_MAX);
0799 
0800     return 0;
0801 }
0802 
0803 /**
0804  * cdns_i2c_master_xfer - The main i2c transfer function
0805  * @adap:   pointer to the i2c adapter driver instance
0806  * @msgs:   pointer to the i2c message structure
0807  * @num:    the number of messages to transfer
0808  *
0809  * Initiates the send/recv activity based on the transfer message received.
0810  *
0811  * Return: number of msgs processed on success, negative error otherwise
0812  */
0813 static int cdns_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
0814                 int num)
0815 {
0816     int ret, count;
0817     u32 reg;
0818     struct cdns_i2c *id = adap->algo_data;
0819     bool hold_quirk;
0820 #if IS_ENABLED(CONFIG_I2C_SLAVE)
0821     bool change_role = false;
0822 #endif
0823 
0824     ret = pm_runtime_resume_and_get(id->dev);
0825     if (ret < 0)
0826         return ret;
0827 
0828 #if IS_ENABLED(CONFIG_I2C_SLAVE)
0829     /* Check i2c operating mode and switch if possible */
0830     if (id->dev_mode == CDNS_I2C_MODE_SLAVE) {
0831         if (id->slave_state != CDNS_I2C_SLAVE_STATE_IDLE)
0832             return -EAGAIN;
0833 
0834         /* Set mode to master */
0835         cdns_i2c_set_mode(CDNS_I2C_MODE_MASTER, id);
0836 
0837         /* Mark flag to change role once xfer is completed */
0838         change_role = true;
0839     }
0840 #endif
0841 
0842     /* Check if the bus is free */
0843     if (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) & CDNS_I2C_SR_BA) {
0844         ret = -EAGAIN;
0845         goto out;
0846     }
0847 
0848     hold_quirk = !!(id->quirks & CDNS_I2C_BROKEN_HOLD_BIT);
0849     /*
0850      * Set the flag to one when multiple messages are to be
0851      * processed with a repeated start.
0852      */
0853     if (num > 1) {
0854         /*
0855          * This controller does not give completion interrupt after a
0856          * master receive message if HOLD bit is set (repeated start),
0857          * resulting in SW timeout. Hence, if a receive message is
0858          * followed by any other message, an error is returned
0859          * indicating that this sequence is not supported.
0860          */
0861         for (count = 0; (count < num - 1 && hold_quirk); count++) {
0862             if (msgs[count].flags & I2C_M_RD) {
0863                 dev_warn(adap->dev.parent,
0864                      "Can't do repeated start after a receive message\n");
0865                 ret = -EOPNOTSUPP;
0866                 goto out;
0867             }
0868         }
0869         id->bus_hold_flag = 1;
0870         reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
0871         reg |= CDNS_I2C_CR_HOLD;
0872         cdns_i2c_writereg(reg, CDNS_I2C_CR_OFFSET);
0873     } else {
0874         id->bus_hold_flag = 0;
0875     }
0876 
0877     /* Process the msg one by one */
0878     for (count = 0; count < num; count++, msgs++) {
0879         if (count == (num - 1))
0880             id->bus_hold_flag = 0;
0881 
0882         ret = cdns_i2c_process_msg(id, msgs, adap);
0883         if (ret)
0884             goto out;
0885 
0886         /* Report the other error interrupts to application */
0887         if (id->err_status) {
0888             cdns_i2c_master_reset(adap);
0889 
0890             if (id->err_status & CDNS_I2C_IXR_NACK) {
0891                 ret = -ENXIO;
0892                 goto out;
0893             }
0894             ret = -EIO;
0895             goto out;
0896         }
0897     }
0898 
0899     ret = num;
0900 
0901 out:
0902 
0903 #if IS_ENABLED(CONFIG_I2C_SLAVE)
0904     /* Switch i2c mode to slave */
0905     if (change_role)
0906         cdns_i2c_set_mode(CDNS_I2C_MODE_SLAVE, id);
0907 #endif
0908 
0909     pm_runtime_mark_last_busy(id->dev);
0910     pm_runtime_put_autosuspend(id->dev);
0911     return ret;
0912 }
0913 
0914 /**
0915  * cdns_i2c_func - Returns the supported features of the I2C driver
0916  * @adap:   pointer to the i2c adapter structure
0917  *
0918  * Return: 32 bit value, each bit corresponding to a feature
0919  */
0920 static u32 cdns_i2c_func(struct i2c_adapter *adap)
0921 {
0922     u32 func = I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
0923             (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
0924             I2C_FUNC_SMBUS_BLOCK_DATA;
0925 
0926 #if IS_ENABLED(CONFIG_I2C_SLAVE)
0927     func |= I2C_FUNC_SLAVE;
0928 #endif
0929 
0930     return func;
0931 }
0932 
0933 #if IS_ENABLED(CONFIG_I2C_SLAVE)
0934 static int cdns_reg_slave(struct i2c_client *slave)
0935 {
0936     int ret;
0937     struct cdns_i2c *id = container_of(slave->adapter, struct cdns_i2c,
0938                                     adap);
0939 
0940     if (id->slave)
0941         return -EBUSY;
0942 
0943     if (slave->flags & I2C_CLIENT_TEN)
0944         return -EAFNOSUPPORT;
0945 
0946     ret = pm_runtime_resume_and_get(id->dev);
0947     if (ret < 0)
0948         return ret;
0949 
0950     /* Store slave information */
0951     id->slave = slave;
0952 
0953     /* Enable I2C slave */
0954     cdns_i2c_set_mode(CDNS_I2C_MODE_SLAVE, id);
0955 
0956     return 0;
0957 }
0958 
0959 static int cdns_unreg_slave(struct i2c_client *slave)
0960 {
0961     struct cdns_i2c *id = container_of(slave->adapter, struct cdns_i2c,
0962                                     adap);
0963 
0964     pm_runtime_put(id->dev);
0965 
0966     /* Remove slave information */
0967     id->slave = NULL;
0968 
0969     /* Enable I2C master */
0970     cdns_i2c_set_mode(CDNS_I2C_MODE_MASTER, id);
0971 
0972     return 0;
0973 }
0974 #endif
0975 
0976 static const struct i2c_algorithm cdns_i2c_algo = {
0977     .master_xfer    = cdns_i2c_master_xfer,
0978     .functionality  = cdns_i2c_func,
0979 #if IS_ENABLED(CONFIG_I2C_SLAVE)
0980     .reg_slave  = cdns_reg_slave,
0981     .unreg_slave    = cdns_unreg_slave,
0982 #endif
0983 };
0984 
0985 /**
0986  * cdns_i2c_calc_divs - Calculate clock dividers
0987  * @f:      I2C clock frequency
0988  * @input_clk:  Input clock frequency
0989  * @a:      First divider (return value)
0990  * @b:      Second divider (return value)
0991  *
0992  * f is used as input and output variable. As input it is used as target I2C
0993  * frequency. On function exit f holds the actually resulting I2C frequency.
0994  *
0995  * Return: 0 on success, negative errno otherwise.
0996  */
0997 static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
0998         unsigned int *a, unsigned int *b)
0999 {
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");