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