Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * I2C adapter for the IMG Serial Control Bus (SCB) IP block.
0004  *
0005  * Copyright (C) 2009, 2010, 2012, 2014 Imagination Technologies Ltd.
0006  *
0007  * There are three ways that this I2C controller can be driven:
0008  *
0009  * - Raw control of the SDA and SCK signals.
0010  *
0011  *   This corresponds to MODE_RAW, which takes control of the signals
0012  *   directly for a certain number of clock cycles (the INT_TIMING
0013  *   interrupt can be used for timing).
0014  *
0015  * - Atomic commands. A low level I2C symbol (such as generate
0016  *   start/stop/ack/nack bit, generate byte, receive byte, and receive
0017  *   ACK) is given to the hardware, with detection of completion by bits
0018  *   in the LINESTAT register.
0019  *
0020  *   This mode of operation is used by MODE_ATOMIC, which uses an I2C
0021  *   state machine in the interrupt handler to compose/react to I2C
0022  *   transactions using atomic mode commands, and also by MODE_SEQUENCE,
0023  *   which emits a simple fixed sequence of atomic mode commands.
0024  *
0025  *   Due to software control, the use of atomic commands usually results
0026  *   in suboptimal use of the bus, with gaps between the I2C symbols while
0027  *   the driver decides what to do next.
0028  *
0029  * - Automatic mode. A bus address, and whether to read/write is
0030  *   specified, and the hardware takes care of the I2C state machine,
0031  *   using a FIFO to send/receive bytes of data to an I2C slave. The
0032  *   driver just has to keep the FIFO drained or filled in response to the
0033  *   appropriate FIFO interrupts.
0034  *
0035  *   This corresponds to MODE_AUTOMATIC, which manages the FIFOs and deals
0036  *   with control of repeated start bits between I2C messages.
0037  *
0038  *   Use of automatic mode and the FIFO can make much more efficient use
0039  *   of the bus compared to individual atomic commands, with potentially
0040  *   no wasted time between I2C symbols or I2C messages.
0041  *
0042  * In most cases MODE_AUTOMATIC is used, however if any of the messages in
0043  * a transaction are zero byte writes (e.g. used by i2cdetect for probing
0044  * the bus), MODE_ATOMIC must be used since automatic mode is normally
0045  * started by the writing of data into the FIFO.
0046  *
0047  * The other modes are used in specific circumstances where MODE_ATOMIC and
0048  * MODE_AUTOMATIC aren't appropriate. MODE_RAW is used to implement a bus
0049  * recovery routine. MODE_SEQUENCE is used to reset the bus and make sure
0050  * it is in a sane state.
0051  *
0052  * Notice that the driver implements a timer-based timeout mechanism.
0053  * The reason for this mechanism is to reduce the number of interrupts
0054  * received in automatic mode.
0055  *
0056  * The driver would get a slave event and transaction done interrupts for
0057  * each atomic mode command that gets completed. However, these events are
0058  * not needed in automatic mode, becase those atomic mode commands are
0059  * managed automatically by the hardware.
0060  *
0061  * In practice, normal I2C transactions will be complete well before you
0062  * get the timer interrupt, as the timer is re-scheduled during FIFO
0063  * maintenance and disabled after the transaction is complete.
0064  *
0065  * In this way normal automatic mode operation isn't impacted by
0066  * unnecessary interrupts, but the exceptional abort condition can still be
0067  * detected (with a slight delay).
0068  */
0069 
0070 #include <linux/bitops.h>
0071 #include <linux/clk.h>
0072 #include <linux/completion.h>
0073 #include <linux/err.h>
0074 #include <linux/i2c.h>
0075 #include <linux/init.h>
0076 #include <linux/interrupt.h>
0077 #include <linux/io.h>
0078 #include <linux/kernel.h>
0079 #include <linux/module.h>
0080 #include <linux/of_platform.h>
0081 #include <linux/platform_device.h>
0082 #include <linux/pm_runtime.h>
0083 #include <linux/slab.h>
0084 #include <linux/timer.h>
0085 
0086 /* Register offsets */
0087 
0088 #define SCB_STATUS_REG          0x00
0089 #define SCB_OVERRIDE_REG        0x04
0090 #define SCB_READ_ADDR_REG       0x08
0091 #define SCB_READ_COUNT_REG      0x0c
0092 #define SCB_WRITE_ADDR_REG      0x10
0093 #define SCB_READ_DATA_REG       0x14
0094 #define SCB_WRITE_DATA_REG      0x18
0095 #define SCB_FIFO_STATUS_REG     0x1c
0096 #define SCB_CONTROL_SOFT_RESET      0x1f
0097 #define SCB_CLK_SET_REG         0x3c
0098 #define SCB_INT_STATUS_REG      0x40
0099 #define SCB_INT_CLEAR_REG       0x44
0100 #define SCB_INT_MASK_REG        0x48
0101 #define SCB_CONTROL_REG         0x4c
0102 #define SCB_TIME_TPL_REG        0x50
0103 #define SCB_TIME_TPH_REG        0x54
0104 #define SCB_TIME_TP2S_REG       0x58
0105 #define SCB_TIME_TBI_REG        0x60
0106 #define SCB_TIME_TSL_REG        0x64
0107 #define SCB_TIME_TDL_REG        0x68
0108 #define SCB_TIME_TSDL_REG       0x6c
0109 #define SCB_TIME_TSDH_REG       0x70
0110 #define SCB_READ_XADDR_REG      0x74
0111 #define SCB_WRITE_XADDR_REG     0x78
0112 #define SCB_WRITE_COUNT_REG     0x7c
0113 #define SCB_CORE_REV_REG        0x80
0114 #define SCB_TIME_TCKH_REG       0x84
0115 #define SCB_TIME_TCKL_REG       0x88
0116 #define SCB_FIFO_FLUSH_REG      0x8c
0117 #define SCB_READ_FIFO_REG       0x94
0118 #define SCB_CLEAR_REG           0x98
0119 
0120 /* SCB_CONTROL_REG bits */
0121 
0122 #define SCB_CONTROL_CLK_ENABLE      0x1e0
0123 #define SCB_CONTROL_TRANSACTION_HALT    0x200
0124 
0125 #define FIFO_READ_FULL          BIT(0)
0126 #define FIFO_READ_EMPTY         BIT(1)
0127 #define FIFO_WRITE_FULL         BIT(2)
0128 #define FIFO_WRITE_EMPTY        BIT(3)
0129 
0130 /* SCB_CLK_SET_REG bits */
0131 #define SCB_FILT_DISABLE        BIT(31)
0132 #define SCB_FILT_BYPASS         BIT(30)
0133 #define SCB_FILT_INC_MASK       0x7f
0134 #define SCB_FILT_INC_SHIFT      16
0135 #define SCB_INC_MASK            0x7f
0136 #define SCB_INC_SHIFT           8
0137 
0138 /* SCB_INT_*_REG bits */
0139 
0140 #define INT_BUS_INACTIVE        BIT(0)
0141 #define INT_UNEXPECTED_START        BIT(1)
0142 #define INT_SCLK_LOW_TIMEOUT        BIT(2)
0143 #define INT_SDAT_LOW_TIMEOUT        BIT(3)
0144 #define INT_WRITE_ACK_ERR       BIT(4)
0145 #define INT_ADDR_ACK_ERR        BIT(5)
0146 #define INT_FIFO_FULL           BIT(9)
0147 #define INT_FIFO_FILLING        BIT(10)
0148 #define INT_FIFO_EMPTY          BIT(11)
0149 #define INT_FIFO_EMPTYING       BIT(12)
0150 #define INT_TRANSACTION_DONE        BIT(15)
0151 #define INT_SLAVE_EVENT         BIT(16)
0152 #define INT_MASTER_HALTED       BIT(17)
0153 #define INT_TIMING          BIT(18)
0154 #define INT_STOP_DETECTED       BIT(19)
0155 
0156 #define INT_FIFO_FULL_FILLING   (INT_FIFO_FULL  | INT_FIFO_FILLING)
0157 
0158 /* Level interrupts need clearing after handling instead of before */
0159 #define INT_LEVEL           0x01e00
0160 
0161 /* Don't allow any interrupts while the clock may be off */
0162 #define INT_ENABLE_MASK_INACTIVE    0x00000
0163 
0164 /* Interrupt masks for the different driver modes */
0165 
0166 #define INT_ENABLE_MASK_RAW     INT_TIMING
0167 
0168 #define INT_ENABLE_MASK_ATOMIC      (INT_TRANSACTION_DONE | \
0169                      INT_SLAVE_EVENT      | \
0170                      INT_ADDR_ACK_ERR     | \
0171                      INT_WRITE_ACK_ERR)
0172 
0173 #define INT_ENABLE_MASK_AUTOMATIC   (INT_SCLK_LOW_TIMEOUT | \
0174                      INT_ADDR_ACK_ERR     | \
0175                      INT_WRITE_ACK_ERR    | \
0176                      INT_FIFO_FULL        | \
0177                      INT_FIFO_FILLING     | \
0178                      INT_FIFO_EMPTY       | \
0179                      INT_MASTER_HALTED    | \
0180                      INT_STOP_DETECTED)
0181 
0182 #define INT_ENABLE_MASK_WAITSTOP    (INT_SLAVE_EVENT      | \
0183                      INT_ADDR_ACK_ERR     | \
0184                      INT_WRITE_ACK_ERR)
0185 
0186 /* SCB_STATUS_REG fields */
0187 
0188 #define LINESTAT_SCLK_LINE_STATUS   BIT(0)
0189 #define LINESTAT_SCLK_EN        BIT(1)
0190 #define LINESTAT_SDAT_LINE_STATUS   BIT(2)
0191 #define LINESTAT_SDAT_EN        BIT(3)
0192 #define LINESTAT_DET_START_STATUS   BIT(4)
0193 #define LINESTAT_DET_STOP_STATUS    BIT(5)
0194 #define LINESTAT_DET_ACK_STATUS     BIT(6)
0195 #define LINESTAT_DET_NACK_STATUS    BIT(7)
0196 #define LINESTAT_BUS_IDLE       BIT(8)
0197 #define LINESTAT_T_DONE_STATUS      BIT(9)
0198 #define LINESTAT_SCLK_OUT_STATUS    BIT(10)
0199 #define LINESTAT_SDAT_OUT_STATUS    BIT(11)
0200 #define LINESTAT_GEN_LINE_MASK_STATUS   BIT(12)
0201 #define LINESTAT_START_BIT_DET      BIT(13)
0202 #define LINESTAT_STOP_BIT_DET       BIT(14)
0203 #define LINESTAT_ACK_DET        BIT(15)
0204 #define LINESTAT_NACK_DET       BIT(16)
0205 #define LINESTAT_INPUT_HELD_V       BIT(17)
0206 #define LINESTAT_ABORT_DET      BIT(18)
0207 #define LINESTAT_ACK_OR_NACK_DET    (LINESTAT_ACK_DET | LINESTAT_NACK_DET)
0208 #define LINESTAT_INPUT_DATA     0xff000000
0209 #define LINESTAT_INPUT_DATA_SHIFT   24
0210 
0211 #define LINESTAT_CLEAR_SHIFT        13
0212 #define LINESTAT_LATCHED        (0x3f << LINESTAT_CLEAR_SHIFT)
0213 
0214 /* SCB_OVERRIDE_REG fields */
0215 
0216 #define OVERRIDE_SCLK_OVR       BIT(0)
0217 #define OVERRIDE_SCLKEN_OVR     BIT(1)
0218 #define OVERRIDE_SDAT_OVR       BIT(2)
0219 #define OVERRIDE_SDATEN_OVR     BIT(3)
0220 #define OVERRIDE_MASTER         BIT(9)
0221 #define OVERRIDE_LINE_OVR_EN        BIT(10)
0222 #define OVERRIDE_DIRECT         BIT(11)
0223 #define OVERRIDE_CMD_SHIFT      4
0224 #define OVERRIDE_CMD_MASK       0x1f
0225 #define OVERRIDE_DATA_SHIFT     24
0226 
0227 #define OVERRIDE_SCLK_DOWN      (OVERRIDE_LINE_OVR_EN | \
0228                      OVERRIDE_SCLKEN_OVR)
0229 #define OVERRIDE_SCLK_UP        (OVERRIDE_LINE_OVR_EN | \
0230                      OVERRIDE_SCLKEN_OVR | \
0231                      OVERRIDE_SCLK_OVR)
0232 #define OVERRIDE_SDAT_DOWN      (OVERRIDE_LINE_OVR_EN | \
0233                      OVERRIDE_SDATEN_OVR)
0234 #define OVERRIDE_SDAT_UP        (OVERRIDE_LINE_OVR_EN | \
0235                      OVERRIDE_SDATEN_OVR | \
0236                      OVERRIDE_SDAT_OVR)
0237 
0238 /* OVERRIDE_CMD values */
0239 
0240 #define CMD_PAUSE           0x00
0241 #define CMD_GEN_DATA            0x01
0242 #define CMD_GEN_START           0x02
0243 #define CMD_GEN_STOP            0x03
0244 #define CMD_GEN_ACK         0x04
0245 #define CMD_GEN_NACK            0x05
0246 #define CMD_RET_DATA            0x08
0247 #define CMD_RET_ACK         0x09
0248 
0249 /* Fixed timing values */
0250 
0251 #define TIMEOUT_TBI         0x0
0252 #define TIMEOUT_TSL         0xffff
0253 #define TIMEOUT_TDL         0x0
0254 
0255 /* Transaction timeout */
0256 
0257 #define IMG_I2C_TIMEOUT         (msecs_to_jiffies(1000))
0258 
0259 /*
0260  * Worst incs are 1 (innacurate) and 16*256 (irregular).
0261  * So a sensible inc is the logarithmic mean: 64 (2^6), which is
0262  * in the middle of the valid range (0-127).
0263  */
0264 #define SCB_OPT_INC     64
0265 
0266 /* Setup the clock enable filtering for 25 ns */
0267 #define SCB_FILT_GLITCH     25
0268 
0269 /*
0270  * Bits to return from interrupt handler functions for different modes.
0271  * This delays completion until we've finished with the registers, so that the
0272  * function waiting for completion can safely disable the clock to save power.
0273  */
0274 #define ISR_COMPLETE_M      BIT(31)
0275 #define ISR_FATAL_M     BIT(30)
0276 #define ISR_WAITSTOP        BIT(29)
0277 #define ISR_STATUS_M        0x0000ffff  /* contains +ve errno */
0278 #define ISR_COMPLETE(err)   (ISR_COMPLETE_M | (ISR_STATUS_M & (err)))
0279 #define ISR_FATAL(err)      (ISR_COMPLETE(err) | ISR_FATAL_M)
0280 
0281 #define IMG_I2C_PM_TIMEOUT  1000 /* ms */
0282 
0283 enum img_i2c_mode {
0284     MODE_INACTIVE,
0285     MODE_RAW,
0286     MODE_ATOMIC,
0287     MODE_AUTOMATIC,
0288     MODE_SEQUENCE,
0289     MODE_FATAL,
0290     MODE_WAITSTOP,
0291     MODE_SUSPEND,
0292 };
0293 
0294 /* Timing parameters for i2c modes (in ns) */
0295 struct img_i2c_timings {
0296     const char *name;
0297     unsigned int max_bitrate;
0298     unsigned int tckh, tckl, tsdh, tsdl;
0299     unsigned int tp2s, tpl, tph;
0300 };
0301 
0302 /* The timings array must be ordered from slower to faster */
0303 static struct img_i2c_timings timings[] = {
0304     /* Standard mode */
0305     {
0306         .name = "standard",
0307         .max_bitrate = I2C_MAX_STANDARD_MODE_FREQ,
0308         .tckh = 4000,
0309         .tckl = 4700,
0310         .tsdh = 4700,
0311         .tsdl = 8700,
0312         .tp2s = 4700,
0313         .tpl = 4700,
0314         .tph = 4000,
0315     },
0316     /* Fast mode */
0317     {
0318         .name = "fast",
0319         .max_bitrate = I2C_MAX_FAST_MODE_FREQ,
0320         .tckh = 600,
0321         .tckl = 1300,
0322         .tsdh = 600,
0323         .tsdl = 1200,
0324         .tp2s = 1300,
0325         .tpl = 600,
0326         .tph = 600,
0327     },
0328 };
0329 
0330 /* Reset dance */
0331 static u8 img_i2c_reset_seq[] = { CMD_GEN_START,
0332                   CMD_GEN_DATA, 0xff,
0333                   CMD_RET_ACK,
0334                   CMD_GEN_START,
0335                   CMD_GEN_STOP,
0336                   0 };
0337 /* Just issue a stop (after an abort condition) */
0338 static u8 img_i2c_stop_seq[] = {  CMD_GEN_STOP,
0339                   0 };
0340 
0341 /* We're interested in different interrupts depending on the mode */
0342 static unsigned int img_i2c_int_enable_by_mode[] = {
0343     [MODE_INACTIVE]  = INT_ENABLE_MASK_INACTIVE,
0344     [MODE_RAW]       = INT_ENABLE_MASK_RAW,
0345     [MODE_ATOMIC]    = INT_ENABLE_MASK_ATOMIC,
0346     [MODE_AUTOMATIC] = INT_ENABLE_MASK_AUTOMATIC,
0347     [MODE_SEQUENCE]  = INT_ENABLE_MASK_ATOMIC,
0348     [MODE_FATAL]     = 0,
0349     [MODE_WAITSTOP]  = INT_ENABLE_MASK_WAITSTOP,
0350     [MODE_SUSPEND]   = 0,
0351 };
0352 
0353 /* Atomic command names */
0354 static const char * const img_i2c_atomic_cmd_names[] = {
0355     [CMD_PAUSE] = "PAUSE",
0356     [CMD_GEN_DATA]  = "GEN_DATA",
0357     [CMD_GEN_START] = "GEN_START",
0358     [CMD_GEN_STOP]  = "GEN_STOP",
0359     [CMD_GEN_ACK]   = "GEN_ACK",
0360     [CMD_GEN_NACK]  = "GEN_NACK",
0361     [CMD_RET_DATA]  = "RET_DATA",
0362     [CMD_RET_ACK]   = "RET_ACK",
0363 };
0364 
0365 struct img_i2c {
0366     struct i2c_adapter adap;
0367 
0368     void __iomem *base;
0369 
0370     /*
0371      * The scb core clock is used to get the input frequency, and to disable
0372      * it after every set of transactions to save some power.
0373      */
0374     struct clk *scb_clk, *sys_clk;
0375     unsigned int bitrate;
0376     bool need_wr_rd_fence;
0377 
0378     /* state */
0379     struct completion msg_complete;
0380     spinlock_t lock;    /* lock before doing anything with the state */
0381     struct i2c_msg msg;
0382 
0383     /* After the last transaction, wait for a stop bit */
0384     bool last_msg;
0385     int msg_status;
0386 
0387     enum img_i2c_mode mode;
0388     u32 int_enable;     /* depends on mode */
0389     u32 line_status;    /* line status over command */
0390 
0391     /*
0392      * To avoid slave event interrupts in automatic mode, use a timer to
0393      * poll the abort condition if we don't get an interrupt for too long.
0394      */
0395     struct timer_list check_timer;
0396     bool t_halt;
0397 
0398     /* atomic mode state */
0399     bool at_t_done;
0400     bool at_slave_event;
0401     int at_cur_cmd;
0402     u8 at_cur_data;
0403 
0404     /* Sequence: either reset or stop. See img_i2c_sequence. */
0405     u8 *seq;
0406 
0407     /* raw mode */
0408     unsigned int raw_timeout;
0409 };
0410 
0411 static int img_i2c_runtime_suspend(struct device *dev);
0412 static int img_i2c_runtime_resume(struct device *dev);
0413 
0414 static void img_i2c_writel(struct img_i2c *i2c, u32 offset, u32 value)
0415 {
0416     writel(value, i2c->base + offset);
0417 }
0418 
0419 static u32 img_i2c_readl(struct img_i2c *i2c, u32 offset)
0420 {
0421     return readl(i2c->base + offset);
0422 }
0423 
0424 /*
0425  * The code to read from the master read fifo, and write to the master
0426  * write fifo, checks a bit in an SCB register before every byte to
0427  * ensure that the fifo is not full (write fifo) or empty (read fifo).
0428  * Due to clock domain crossing inside the SCB block the updated value
0429  * of this bit is only visible after 2 cycles.
0430  *
0431  * The scb_wr_rd_fence() function does 2 dummy writes (to the read-only
0432  * revision register), and it's called after reading from or writing to the
0433  * fifos to ensure that subsequent reads of the fifo status bits do not read
0434  * stale values.
0435  */
0436 static void img_i2c_wr_rd_fence(struct img_i2c *i2c)
0437 {
0438     if (i2c->need_wr_rd_fence) {
0439         img_i2c_writel(i2c, SCB_CORE_REV_REG, 0);
0440         img_i2c_writel(i2c, SCB_CORE_REV_REG, 0);
0441     }
0442 }
0443 
0444 static void img_i2c_switch_mode(struct img_i2c *i2c, enum img_i2c_mode mode)
0445 {
0446     i2c->mode = mode;
0447     i2c->int_enable = img_i2c_int_enable_by_mode[mode];
0448     i2c->line_status = 0;
0449 }
0450 
0451 static void img_i2c_raw_op(struct img_i2c *i2c)
0452 {
0453     i2c->raw_timeout = 0;
0454     img_i2c_writel(i2c, SCB_OVERRIDE_REG,
0455         OVERRIDE_SCLKEN_OVR |
0456         OVERRIDE_SDATEN_OVR |
0457         OVERRIDE_MASTER |
0458         OVERRIDE_LINE_OVR_EN |
0459         OVERRIDE_DIRECT |
0460         ((i2c->at_cur_cmd & OVERRIDE_CMD_MASK) << OVERRIDE_CMD_SHIFT) |
0461         (i2c->at_cur_data << OVERRIDE_DATA_SHIFT));
0462 }
0463 
0464 static const char *img_i2c_atomic_op_name(unsigned int cmd)
0465 {
0466     if (unlikely(cmd >= ARRAY_SIZE(img_i2c_atomic_cmd_names)))
0467         return "UNKNOWN";
0468     return img_i2c_atomic_cmd_names[cmd];
0469 }
0470 
0471 /* Send a single atomic mode command to the hardware */
0472 static void img_i2c_atomic_op(struct img_i2c *i2c, int cmd, u8 data)
0473 {
0474     i2c->at_cur_cmd = cmd;
0475     i2c->at_cur_data = data;
0476 
0477     /* work around lack of data setup time when generating data */
0478     if (cmd == CMD_GEN_DATA && i2c->mode == MODE_ATOMIC) {
0479         u32 line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
0480 
0481         if (line_status & LINESTAT_SDAT_LINE_STATUS && !(data & 0x80)) {
0482             /* hold the data line down for a moment */
0483             img_i2c_switch_mode(i2c, MODE_RAW);
0484             img_i2c_raw_op(i2c);
0485             return;
0486         }
0487     }
0488 
0489     dev_dbg(i2c->adap.dev.parent,
0490         "atomic cmd=%s (%d) data=%#x\n",
0491         img_i2c_atomic_op_name(cmd), cmd, data);
0492     i2c->at_t_done = (cmd == CMD_RET_DATA || cmd == CMD_RET_ACK);
0493     i2c->at_slave_event = false;
0494     i2c->line_status = 0;
0495 
0496     img_i2c_writel(i2c, SCB_OVERRIDE_REG,
0497         ((cmd & OVERRIDE_CMD_MASK) << OVERRIDE_CMD_SHIFT) |
0498         OVERRIDE_MASTER |
0499         OVERRIDE_DIRECT |
0500         (data << OVERRIDE_DATA_SHIFT));
0501 }
0502 
0503 /* Start a transaction in atomic mode */
0504 static void img_i2c_atomic_start(struct img_i2c *i2c)
0505 {
0506     img_i2c_switch_mode(i2c, MODE_ATOMIC);
0507     img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
0508     img_i2c_atomic_op(i2c, CMD_GEN_START, 0x00);
0509 }
0510 
0511 static void img_i2c_soft_reset(struct img_i2c *i2c)
0512 {
0513     i2c->t_halt = false;
0514     img_i2c_writel(i2c, SCB_CONTROL_REG, 0);
0515     img_i2c_writel(i2c, SCB_CONTROL_REG,
0516                SCB_CONTROL_CLK_ENABLE | SCB_CONTROL_SOFT_RESET);
0517 }
0518 
0519 /*
0520  * Enable or release transaction halt for control of repeated starts.
0521  * In version 3.3 of the IP when transaction halt is set, an interrupt
0522  * will be generated after each byte of a transfer instead of after
0523  * every transfer but before the stop bit.
0524  * Due to this behaviour we have to be careful that every time we
0525  * release the transaction halt we have to re-enable it straight away
0526  * so that we only process a single byte, not doing so will result in
0527  * all remaining bytes been processed and a stop bit being issued,
0528  * which will prevent us having a repeated start.
0529  */
0530 static void img_i2c_transaction_halt(struct img_i2c *i2c, bool t_halt)
0531 {
0532     u32 val;
0533 
0534     if (i2c->t_halt == t_halt)
0535         return;
0536     i2c->t_halt = t_halt;
0537     val = img_i2c_readl(i2c, SCB_CONTROL_REG);
0538     if (t_halt)
0539         val |= SCB_CONTROL_TRANSACTION_HALT;
0540     else
0541         val &= ~SCB_CONTROL_TRANSACTION_HALT;
0542     img_i2c_writel(i2c, SCB_CONTROL_REG, val);
0543 }
0544 
0545 /* Drain data from the FIFO into the buffer (automatic mode) */
0546 static void img_i2c_read_fifo(struct img_i2c *i2c)
0547 {
0548     while (i2c->msg.len) {
0549         u32 fifo_status;
0550         u8 data;
0551 
0552         img_i2c_wr_rd_fence(i2c);
0553         fifo_status = img_i2c_readl(i2c, SCB_FIFO_STATUS_REG);
0554         if (fifo_status & FIFO_READ_EMPTY)
0555             break;
0556 
0557         data = img_i2c_readl(i2c, SCB_READ_DATA_REG);
0558         *i2c->msg.buf = data;
0559 
0560         img_i2c_writel(i2c, SCB_READ_FIFO_REG, 0xff);
0561         i2c->msg.len--;
0562         i2c->msg.buf++;
0563     }
0564 }
0565 
0566 /* Fill the FIFO with data from the buffer (automatic mode) */
0567 static void img_i2c_write_fifo(struct img_i2c *i2c)
0568 {
0569     while (i2c->msg.len) {
0570         u32 fifo_status;
0571 
0572         img_i2c_wr_rd_fence(i2c);
0573         fifo_status = img_i2c_readl(i2c, SCB_FIFO_STATUS_REG);
0574         if (fifo_status & FIFO_WRITE_FULL)
0575             break;
0576 
0577         img_i2c_writel(i2c, SCB_WRITE_DATA_REG, *i2c->msg.buf);
0578         i2c->msg.len--;
0579         i2c->msg.buf++;
0580     }
0581 
0582     /* Disable fifo emptying interrupt if nothing more to write */
0583     if (!i2c->msg.len)
0584         i2c->int_enable &= ~INT_FIFO_EMPTYING;
0585 }
0586 
0587 /* Start a read transaction in automatic mode */
0588 static void img_i2c_read(struct img_i2c *i2c)
0589 {
0590     img_i2c_switch_mode(i2c, MODE_AUTOMATIC);
0591     if (!i2c->last_msg)
0592         i2c->int_enable |= INT_SLAVE_EVENT;
0593 
0594     img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
0595     img_i2c_writel(i2c, SCB_READ_ADDR_REG, i2c->msg.addr);
0596     img_i2c_writel(i2c, SCB_READ_COUNT_REG, i2c->msg.len);
0597 
0598     mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
0599 }
0600 
0601 /* Start a write transaction in automatic mode */
0602 static void img_i2c_write(struct img_i2c *i2c)
0603 {
0604     img_i2c_switch_mode(i2c, MODE_AUTOMATIC);
0605     if (!i2c->last_msg)
0606         i2c->int_enable |= INT_SLAVE_EVENT;
0607 
0608     img_i2c_writel(i2c, SCB_WRITE_ADDR_REG, i2c->msg.addr);
0609     img_i2c_writel(i2c, SCB_WRITE_COUNT_REG, i2c->msg.len);
0610 
0611     mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
0612     img_i2c_write_fifo(i2c);
0613 
0614     /* img_i2c_write_fifo() may modify int_enable */
0615     img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
0616 }
0617 
0618 /*
0619  * Indicate that the transaction is complete. This is called from the
0620  * ISR to wake up the waiting thread, after which the ISR must not
0621  * access any more SCB registers.
0622  */
0623 static void img_i2c_complete_transaction(struct img_i2c *i2c, int status)
0624 {
0625     img_i2c_switch_mode(i2c, MODE_INACTIVE);
0626     if (status) {
0627         i2c->msg_status = status;
0628         img_i2c_transaction_halt(i2c, false);
0629     }
0630     complete(&i2c->msg_complete);
0631 }
0632 
0633 static unsigned int img_i2c_raw_atomic_delay_handler(struct img_i2c *i2c,
0634                     u32 int_status, u32 line_status)
0635 {
0636     /* Stay in raw mode for this, so we don't just loop infinitely */
0637     img_i2c_atomic_op(i2c, i2c->at_cur_cmd, i2c->at_cur_data);
0638     img_i2c_switch_mode(i2c, MODE_ATOMIC);
0639     return 0;
0640 }
0641 
0642 static unsigned int img_i2c_raw(struct img_i2c *i2c, u32 int_status,
0643                 u32 line_status)
0644 {
0645     if (int_status & INT_TIMING) {
0646         if (i2c->raw_timeout == 0)
0647             return img_i2c_raw_atomic_delay_handler(i2c,
0648                 int_status, line_status);
0649         --i2c->raw_timeout;
0650     }
0651     return 0;
0652 }
0653 
0654 static unsigned int img_i2c_sequence(struct img_i2c *i2c, u32 int_status)
0655 {
0656     static const unsigned int continue_bits[] = {
0657         [CMD_GEN_START] = LINESTAT_START_BIT_DET,
0658         [CMD_GEN_DATA]  = LINESTAT_INPUT_HELD_V,
0659         [CMD_RET_ACK]   = LINESTAT_ACK_DET | LINESTAT_NACK_DET,
0660         [CMD_RET_DATA]  = LINESTAT_INPUT_HELD_V,
0661         [CMD_GEN_STOP]  = LINESTAT_STOP_BIT_DET,
0662     };
0663     int next_cmd = -1;
0664     u8 next_data = 0x00;
0665 
0666     if (int_status & INT_SLAVE_EVENT)
0667         i2c->at_slave_event = true;
0668     if (int_status & INT_TRANSACTION_DONE)
0669         i2c->at_t_done = true;
0670 
0671     if (!i2c->at_slave_event || !i2c->at_t_done)
0672         return 0;
0673 
0674     /* wait if no continue bits are set */
0675     if (i2c->at_cur_cmd >= 0 &&
0676         i2c->at_cur_cmd < ARRAY_SIZE(continue_bits)) {
0677         unsigned int cont_bits = continue_bits[i2c->at_cur_cmd];
0678 
0679         if (cont_bits) {
0680             cont_bits |= LINESTAT_ABORT_DET;
0681             if (!(i2c->line_status & cont_bits))
0682                 return 0;
0683         }
0684     }
0685 
0686     /* follow the sequence of commands in i2c->seq */
0687     next_cmd = *i2c->seq;
0688     /* stop on a nil */
0689     if (!next_cmd) {
0690         img_i2c_writel(i2c, SCB_OVERRIDE_REG, 0);
0691         return ISR_COMPLETE(0);
0692     }
0693     /* when generating data, the next byte is the data */
0694     if (next_cmd == CMD_GEN_DATA) {
0695         ++i2c->seq;
0696         next_data = *i2c->seq;
0697     }
0698     ++i2c->seq;
0699     img_i2c_atomic_op(i2c, next_cmd, next_data);
0700 
0701     return 0;
0702 }
0703 
0704 static void img_i2c_reset_start(struct img_i2c *i2c)
0705 {
0706     /* Initiate the magic dance */
0707     img_i2c_switch_mode(i2c, MODE_SEQUENCE);
0708     img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
0709     i2c->seq = img_i2c_reset_seq;
0710     i2c->at_slave_event = true;
0711     i2c->at_t_done = true;
0712     i2c->at_cur_cmd = -1;
0713 
0714     /* img_i2c_reset_seq isn't empty so the following won't fail */
0715     img_i2c_sequence(i2c, 0);
0716 }
0717 
0718 static void img_i2c_stop_start(struct img_i2c *i2c)
0719 {
0720     /* Initiate a stop bit sequence */
0721     img_i2c_switch_mode(i2c, MODE_SEQUENCE);
0722     img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
0723     i2c->seq = img_i2c_stop_seq;
0724     i2c->at_slave_event = true;
0725     i2c->at_t_done = true;
0726     i2c->at_cur_cmd = -1;
0727 
0728     /* img_i2c_stop_seq isn't empty so the following won't fail */
0729     img_i2c_sequence(i2c, 0);
0730 }
0731 
0732 static unsigned int img_i2c_atomic(struct img_i2c *i2c,
0733                    u32 int_status,
0734                    u32 line_status)
0735 {
0736     int next_cmd = -1;
0737     u8 next_data = 0x00;
0738 
0739     if (int_status & INT_SLAVE_EVENT)
0740         i2c->at_slave_event = true;
0741     if (int_status & INT_TRANSACTION_DONE)
0742         i2c->at_t_done = true;
0743 
0744     if (!i2c->at_slave_event || !i2c->at_t_done)
0745         goto next_atomic_cmd;
0746     if (i2c->line_status & LINESTAT_ABORT_DET) {
0747         dev_dbg(i2c->adap.dev.parent, "abort condition detected\n");
0748         next_cmd = CMD_GEN_STOP;
0749         i2c->msg_status = -EIO;
0750         goto next_atomic_cmd;
0751     }
0752 
0753     /* i2c->at_cur_cmd may have completed */
0754     switch (i2c->at_cur_cmd) {
0755     case CMD_GEN_START:
0756         next_cmd = CMD_GEN_DATA;
0757         next_data = i2c_8bit_addr_from_msg(&i2c->msg);
0758         break;
0759     case CMD_GEN_DATA:
0760         if (i2c->line_status & LINESTAT_INPUT_HELD_V)
0761             next_cmd = CMD_RET_ACK;
0762         break;
0763     case CMD_RET_ACK:
0764         if (i2c->line_status & LINESTAT_ACK_DET ||
0765             (i2c->line_status & LINESTAT_NACK_DET &&
0766             i2c->msg.flags & I2C_M_IGNORE_NAK)) {
0767             if (i2c->msg.len == 0) {
0768                 next_cmd = CMD_GEN_STOP;
0769             } else if (i2c->msg.flags & I2C_M_RD) {
0770                 next_cmd = CMD_RET_DATA;
0771             } else {
0772                 next_cmd = CMD_GEN_DATA;
0773                 next_data = *i2c->msg.buf;
0774                 --i2c->msg.len;
0775                 ++i2c->msg.buf;
0776             }
0777         } else if (i2c->line_status & LINESTAT_NACK_DET) {
0778             i2c->msg_status = -EIO;
0779             next_cmd = CMD_GEN_STOP;
0780         }
0781         break;
0782     case CMD_RET_DATA:
0783         if (i2c->line_status & LINESTAT_INPUT_HELD_V) {
0784             *i2c->msg.buf = (i2c->line_status &
0785                         LINESTAT_INPUT_DATA)
0786                     >> LINESTAT_INPUT_DATA_SHIFT;
0787             --i2c->msg.len;
0788             ++i2c->msg.buf;
0789             if (i2c->msg.len)
0790                 next_cmd = CMD_GEN_ACK;
0791             else
0792                 next_cmd = CMD_GEN_NACK;
0793         }
0794         break;
0795     case CMD_GEN_ACK:
0796         if (i2c->line_status & LINESTAT_ACK_DET) {
0797             next_cmd = CMD_RET_DATA;
0798         } else {
0799             i2c->msg_status = -EIO;
0800             next_cmd = CMD_GEN_STOP;
0801         }
0802         break;
0803     case CMD_GEN_NACK:
0804         next_cmd = CMD_GEN_STOP;
0805         break;
0806     case CMD_GEN_STOP:
0807         img_i2c_writel(i2c, SCB_OVERRIDE_REG, 0);
0808         return ISR_COMPLETE(0);
0809     default:
0810         dev_err(i2c->adap.dev.parent, "bad atomic command %d\n",
0811             i2c->at_cur_cmd);
0812         i2c->msg_status = -EIO;
0813         next_cmd = CMD_GEN_STOP;
0814         break;
0815     }
0816 
0817 next_atomic_cmd:
0818     if (next_cmd != -1) {
0819         /* don't actually stop unless we're the last transaction */
0820         if (next_cmd == CMD_GEN_STOP && !i2c->msg_status &&
0821                         !i2c->last_msg)
0822             return ISR_COMPLETE(0);
0823         img_i2c_atomic_op(i2c, next_cmd, next_data);
0824     }
0825     return 0;
0826 }
0827 
0828 /*
0829  * Timer function to check if something has gone wrong in automatic mode (so we
0830  * don't have to handle so many interrupts just to catch an exception).
0831  */
0832 static void img_i2c_check_timer(struct timer_list *t)
0833 {
0834     struct img_i2c *i2c = from_timer(i2c, t, check_timer);
0835     unsigned long flags;
0836     unsigned int line_status;
0837 
0838     spin_lock_irqsave(&i2c->lock, flags);
0839     line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
0840 
0841     /* check for an abort condition */
0842     if (line_status & LINESTAT_ABORT_DET) {
0843         dev_dbg(i2c->adap.dev.parent,
0844             "abort condition detected by check timer\n");
0845         /* enable slave event interrupt mask to trigger irq */
0846         img_i2c_writel(i2c, SCB_INT_MASK_REG,
0847                    i2c->int_enable | INT_SLAVE_EVENT);
0848     }
0849 
0850     spin_unlock_irqrestore(&i2c->lock, flags);
0851 }
0852 
0853 static unsigned int img_i2c_auto(struct img_i2c *i2c,
0854                  unsigned int int_status,
0855                  unsigned int line_status)
0856 {
0857     if (int_status & (INT_WRITE_ACK_ERR | INT_ADDR_ACK_ERR))
0858         return ISR_COMPLETE(EIO);
0859 
0860     if (line_status & LINESTAT_ABORT_DET) {
0861         dev_dbg(i2c->adap.dev.parent, "abort condition detected\n");
0862         /* empty the read fifo */
0863         if ((i2c->msg.flags & I2C_M_RD) &&
0864             (int_status & INT_FIFO_FULL_FILLING))
0865             img_i2c_read_fifo(i2c);
0866         /* use atomic mode and try to force a stop bit */
0867         i2c->msg_status = -EIO;
0868         img_i2c_stop_start(i2c);
0869         return 0;
0870     }
0871 
0872     /* Enable transaction halt on start bit */
0873     if (!i2c->last_msg && line_status & LINESTAT_START_BIT_DET) {
0874         img_i2c_transaction_halt(i2c, !i2c->last_msg);
0875         /* we're no longer interested in the slave event */
0876         i2c->int_enable &= ~INT_SLAVE_EVENT;
0877     }
0878 
0879     mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
0880 
0881     if (int_status & INT_STOP_DETECTED) {
0882         /* Drain remaining data in FIFO and complete transaction */
0883         if (i2c->msg.flags & I2C_M_RD)
0884             img_i2c_read_fifo(i2c);
0885         return ISR_COMPLETE(0);
0886     }
0887 
0888     if (i2c->msg.flags & I2C_M_RD) {
0889         if (int_status & (INT_FIFO_FULL_FILLING | INT_MASTER_HALTED)) {
0890             img_i2c_read_fifo(i2c);
0891             if (i2c->msg.len == 0)
0892                 return ISR_WAITSTOP;
0893         }
0894     } else {
0895         if (int_status & (INT_FIFO_EMPTY | INT_MASTER_HALTED)) {
0896             if ((int_status & INT_FIFO_EMPTY) &&
0897                 i2c->msg.len == 0)
0898                 return ISR_WAITSTOP;
0899             img_i2c_write_fifo(i2c);
0900         }
0901     }
0902     if (int_status & INT_MASTER_HALTED) {
0903         /*
0904          * Release and then enable transaction halt, to
0905          * allow only a single byte to proceed.
0906          */
0907         img_i2c_transaction_halt(i2c, false);
0908         img_i2c_transaction_halt(i2c, !i2c->last_msg);
0909     }
0910 
0911     return 0;
0912 }
0913 
0914 static irqreturn_t img_i2c_isr(int irq, void *dev_id)
0915 {
0916     struct img_i2c *i2c = (struct img_i2c *)dev_id;
0917     u32 int_status, line_status;
0918     /* We handle transaction completion AFTER accessing registers */
0919     unsigned int hret;
0920 
0921     /* Read interrupt status register. */
0922     int_status = img_i2c_readl(i2c, SCB_INT_STATUS_REG);
0923     /* Clear detected interrupts. */
0924     img_i2c_writel(i2c, SCB_INT_CLEAR_REG, int_status);
0925 
0926     /*
0927      * Read line status and clear it until it actually is clear.  We have
0928      * to be careful not to lose any line status bits that get latched.
0929      */
0930     line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
0931     if (line_status & LINESTAT_LATCHED) {
0932         img_i2c_writel(i2c, SCB_CLEAR_REG,
0933                   (line_status & LINESTAT_LATCHED)
0934                 >> LINESTAT_CLEAR_SHIFT);
0935         img_i2c_wr_rd_fence(i2c);
0936     }
0937 
0938     spin_lock(&i2c->lock);
0939 
0940     /* Keep track of line status bits received */
0941     i2c->line_status &= ~LINESTAT_INPUT_DATA;
0942     i2c->line_status |= line_status;
0943 
0944     /*
0945      * Certain interrupts indicate that sclk low timeout is not
0946      * a problem. If any of these are set, just continue.
0947      */
0948     if ((int_status & INT_SCLK_LOW_TIMEOUT) &&
0949         !(int_status & (INT_SLAVE_EVENT |
0950                 INT_FIFO_EMPTY |
0951                 INT_FIFO_FULL))) {
0952         dev_crit(i2c->adap.dev.parent,
0953              "fatal: clock low timeout occurred %s addr 0x%02x\n",
0954              (i2c->msg.flags & I2C_M_RD) ? "reading" : "writing",
0955              i2c->msg.addr);
0956         hret = ISR_FATAL(EIO);
0957         goto out;
0958     }
0959 
0960     if (i2c->mode == MODE_ATOMIC)
0961         hret = img_i2c_atomic(i2c, int_status, line_status);
0962     else if (i2c->mode == MODE_AUTOMATIC)
0963         hret = img_i2c_auto(i2c, int_status, line_status);
0964     else if (i2c->mode == MODE_SEQUENCE)
0965         hret = img_i2c_sequence(i2c, int_status);
0966     else if (i2c->mode == MODE_WAITSTOP && (int_status & INT_SLAVE_EVENT) &&
0967              (line_status & LINESTAT_STOP_BIT_DET))
0968         hret = ISR_COMPLETE(0);
0969     else if (i2c->mode == MODE_RAW)
0970         hret = img_i2c_raw(i2c, int_status, line_status);
0971     else
0972         hret = 0;
0973 
0974     /* Clear detected level interrupts. */
0975     img_i2c_writel(i2c, SCB_INT_CLEAR_REG, int_status & INT_LEVEL);
0976 
0977 out:
0978     if (hret & ISR_WAITSTOP) {
0979         /*
0980          * Only wait for stop on last message.
0981          * Also we may already have detected the stop bit.
0982          */
0983         if (!i2c->last_msg || i2c->line_status & LINESTAT_STOP_BIT_DET)
0984             hret = ISR_COMPLETE(0);
0985         else
0986             img_i2c_switch_mode(i2c, MODE_WAITSTOP);
0987     }
0988 
0989     /* now we've finished using regs, handle transaction completion */
0990     if (hret & ISR_COMPLETE_M) {
0991         int status = -(hret & ISR_STATUS_M);
0992 
0993         img_i2c_complete_transaction(i2c, status);
0994         if (hret & ISR_FATAL_M)
0995             img_i2c_switch_mode(i2c, MODE_FATAL);
0996     }
0997 
0998     /* Enable interrupts (int_enable may be altered by changing mode) */
0999     img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
1000 
1001     spin_unlock(&i2c->lock);
1002 
1003     return IRQ_HANDLED;
1004 }
1005 
1006 /* Force a bus reset sequence and wait for it to complete */
1007 static int img_i2c_reset_bus(struct img_i2c *i2c)
1008 {
1009     unsigned long flags;
1010     unsigned long time_left;
1011 
1012     spin_lock_irqsave(&i2c->lock, flags);
1013     reinit_completion(&i2c->msg_complete);
1014     img_i2c_reset_start(i2c);
1015     spin_unlock_irqrestore(&i2c->lock, flags);
1016 
1017     time_left = wait_for_completion_timeout(&i2c->msg_complete,
1018                           IMG_I2C_TIMEOUT);
1019     if (time_left == 0)
1020         return -ETIMEDOUT;
1021     return 0;
1022 }
1023 
1024 static int img_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
1025             int num)
1026 {
1027     struct img_i2c *i2c = i2c_get_adapdata(adap);
1028     bool atomic = false;
1029     int i, ret;
1030     unsigned long time_left;
1031 
1032     if (i2c->mode == MODE_SUSPEND) {
1033         WARN(1, "refusing to service transaction in suspended state\n");
1034         return -EIO;
1035     }
1036 
1037     if (i2c->mode == MODE_FATAL)
1038         return -EIO;
1039 
1040     for (i = 0; i < num; i++) {
1041         /*
1042          * 0 byte reads are not possible because the slave could try
1043          * and pull the data line low, preventing a stop bit.
1044          */
1045         if (!msgs[i].len && msgs[i].flags & I2C_M_RD)
1046             return -EIO;
1047         /*
1048          * 0 byte writes are possible and used for probing, but we
1049          * cannot do them in automatic mode, so use atomic mode
1050          * instead.
1051          *
1052          * Also, the I2C_M_IGNORE_NAK mode can only be implemented
1053          * in atomic mode.
1054          */
1055         if (!msgs[i].len ||
1056             (msgs[i].flags & I2C_M_IGNORE_NAK))
1057             atomic = true;
1058     }
1059 
1060     ret = pm_runtime_resume_and_get(adap->dev.parent);
1061     if (ret < 0)
1062         return ret;
1063 
1064     for (i = 0; i < num; i++) {
1065         struct i2c_msg *msg = &msgs[i];
1066         unsigned long flags;
1067 
1068         spin_lock_irqsave(&i2c->lock, flags);
1069 
1070         /*
1071          * Make a copy of the message struct. We mustn't modify the
1072          * original or we'll confuse drivers and i2c-dev.
1073          */
1074         i2c->msg = *msg;
1075         i2c->msg_status = 0;
1076 
1077         /*
1078          * After the last message we must have waited for a stop bit.
1079          * Not waiting can cause problems when the clock is disabled
1080          * before the stop bit is sent, and the linux I2C interface
1081          * requires separate transfers not to joined with repeated
1082          * start.
1083          */
1084         i2c->last_msg = (i == num - 1);
1085         reinit_completion(&i2c->msg_complete);
1086 
1087         /*
1088          * Clear line status and all interrupts before starting a
1089          * transfer, as we may have unserviced interrupts from
1090          * previous transfers that might be handled in the context
1091          * of the new transfer.
1092          */
1093         img_i2c_writel(i2c, SCB_INT_CLEAR_REG, ~0);
1094         img_i2c_writel(i2c, SCB_CLEAR_REG, ~0);
1095 
1096         if (atomic) {
1097             img_i2c_atomic_start(i2c);
1098         } else {
1099             /*
1100              * Enable transaction halt if not the last message in
1101              * the queue so that we can control repeated starts.
1102              */
1103             img_i2c_transaction_halt(i2c, !i2c->last_msg);
1104 
1105             if (msg->flags & I2C_M_RD)
1106                 img_i2c_read(i2c);
1107             else
1108                 img_i2c_write(i2c);
1109 
1110             /*
1111              * Release and then enable transaction halt, to
1112              * allow only a single byte to proceed.
1113              * This doesn't have an effect on the initial transfer
1114              * but will allow the following transfers to start
1115              * processing if the previous transfer was marked as
1116              * complete while the i2c block was halted.
1117              */
1118             img_i2c_transaction_halt(i2c, false);
1119             img_i2c_transaction_halt(i2c, !i2c->last_msg);
1120         }
1121         spin_unlock_irqrestore(&i2c->lock, flags);
1122 
1123         time_left = wait_for_completion_timeout(&i2c->msg_complete,
1124                               IMG_I2C_TIMEOUT);
1125         del_timer_sync(&i2c->check_timer);
1126 
1127         if (time_left == 0) {
1128             dev_err(adap->dev.parent, "i2c transfer timed out\n");
1129             i2c->msg_status = -ETIMEDOUT;
1130             break;
1131         }
1132 
1133         if (i2c->msg_status)
1134             break;
1135     }
1136 
1137     pm_runtime_mark_last_busy(adap->dev.parent);
1138     pm_runtime_put_autosuspend(adap->dev.parent);
1139 
1140     return i2c->msg_status ? i2c->msg_status : num;
1141 }
1142 
1143 static u32 img_i2c_func(struct i2c_adapter *adap)
1144 {
1145     return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1146 }
1147 
1148 static const struct i2c_algorithm img_i2c_algo = {
1149     .master_xfer = img_i2c_xfer,
1150     .functionality = img_i2c_func,
1151 };
1152 
1153 static int img_i2c_init(struct img_i2c *i2c)
1154 {
1155     unsigned int clk_khz, bitrate_khz, clk_period, tckh, tckl, tsdh;
1156     unsigned int i, data, prescale, inc, int_bitrate, filt;
1157     struct img_i2c_timings timing;
1158     u32 rev;
1159     int ret;
1160 
1161     ret = pm_runtime_resume_and_get(i2c->adap.dev.parent);
1162     if (ret < 0)
1163         return ret;
1164 
1165     rev = img_i2c_readl(i2c, SCB_CORE_REV_REG);
1166     if ((rev & 0x00ffffff) < 0x00020200) {
1167         dev_info(i2c->adap.dev.parent,
1168              "Unknown hardware revision (%d.%d.%d.%d)\n",
1169              (rev >> 24) & 0xff, (rev >> 16) & 0xff,
1170              (rev >> 8) & 0xff, rev & 0xff);
1171         pm_runtime_mark_last_busy(i2c->adap.dev.parent);
1172         pm_runtime_put_autosuspend(i2c->adap.dev.parent);
1173         return -EINVAL;
1174     }
1175 
1176     /* Fencing enabled by default. */
1177     i2c->need_wr_rd_fence = true;
1178 
1179     /* Determine what mode we're in from the bitrate */
1180     timing = timings[0];
1181     for (i = 0; i < ARRAY_SIZE(timings); i++) {
1182         if (i2c->bitrate <= timings[i].max_bitrate) {
1183             timing = timings[i];
1184             break;
1185         }
1186     }
1187     if (i2c->bitrate > timings[ARRAY_SIZE(timings) - 1].max_bitrate) {
1188         dev_warn(i2c->adap.dev.parent,
1189              "requested bitrate (%u) is higher than the max bitrate supported (%u)\n",
1190              i2c->bitrate,
1191              timings[ARRAY_SIZE(timings) - 1].max_bitrate);
1192         timing = timings[ARRAY_SIZE(timings) - 1];
1193         i2c->bitrate = timing.max_bitrate;
1194     }
1195 
1196     bitrate_khz = i2c->bitrate / 1000;
1197     clk_khz = clk_get_rate(i2c->scb_clk) / 1000;
1198 
1199     /* Find the prescale that would give us that inc (approx delay = 0) */
1200     prescale = SCB_OPT_INC * clk_khz / (256 * 16 * bitrate_khz);
1201     prescale = clamp_t(unsigned int, prescale, 1, 8);
1202     clk_khz /= prescale;
1203 
1204     /* Setup the clock increment value */
1205     inc = (256 * 16 * bitrate_khz) / clk_khz;
1206 
1207     /*
1208      * The clock generation logic allows to filter glitches on the bus.
1209      * This filter is able to remove bus glitches shorter than 50ns.
1210      * If the clock enable rate is greater than 20 MHz, no filtering
1211      * is required, so we need to disable it.
1212      * If it's between the 20-40 MHz range, there's no need to divide
1213      * the clock to get a filter.
1214      */
1215     if (clk_khz < 20000) {
1216         filt = SCB_FILT_DISABLE;
1217     } else if (clk_khz < 40000) {
1218         filt = SCB_FILT_BYPASS;
1219     } else {
1220         /* Calculate filter clock */
1221         filt = (64000 / ((clk_khz / 1000) * SCB_FILT_GLITCH));
1222 
1223         /* Scale up if needed */
1224         if (64000 % ((clk_khz / 1000) * SCB_FILT_GLITCH))
1225             inc++;
1226 
1227         if (filt > SCB_FILT_INC_MASK)
1228             filt = SCB_FILT_INC_MASK;
1229 
1230         filt = (filt & SCB_FILT_INC_MASK) << SCB_FILT_INC_SHIFT;
1231     }
1232     data = filt | ((inc & SCB_INC_MASK) << SCB_INC_SHIFT) | (prescale - 1);
1233     img_i2c_writel(i2c, SCB_CLK_SET_REG, data);
1234 
1235     /* Obtain the clock period of the fx16 clock in ns */
1236     clk_period = (256 * 1000000) / (clk_khz * inc);
1237 
1238     /* Calculate the bitrate in terms of internal clock pulses */
1239     int_bitrate = 1000000 / (bitrate_khz * clk_period);
1240     if ((1000000 % (bitrate_khz * clk_period)) >=
1241         ((bitrate_khz * clk_period) / 2))
1242         int_bitrate++;
1243 
1244     /*
1245      * Setup clock duty cycle, start with 50% and adjust TCKH and TCKL
1246      * values from there if they don't meet minimum timing requirements
1247      */
1248     tckh = int_bitrate / 2;
1249     tckl = int_bitrate - tckh;
1250 
1251     /* Adjust TCKH and TCKL values */
1252     data = DIV_ROUND_UP(timing.tckl, clk_period);
1253 
1254     if (tckl < data) {
1255         tckl = data;
1256         tckh = int_bitrate - tckl;
1257     }
1258 
1259     if (tckh > 0)
1260         --tckh;
1261 
1262     if (tckl > 0)
1263         --tckl;
1264 
1265     img_i2c_writel(i2c, SCB_TIME_TCKH_REG, tckh);
1266     img_i2c_writel(i2c, SCB_TIME_TCKL_REG, tckl);
1267 
1268     /* Setup TSDH value */
1269     tsdh = DIV_ROUND_UP(timing.tsdh, clk_period);
1270 
1271     if (tsdh > 1)
1272         data = tsdh - 1;
1273     else
1274         data = 0x01;
1275     img_i2c_writel(i2c, SCB_TIME_TSDH_REG, data);
1276 
1277     /* This value is used later */
1278     tsdh = data;
1279 
1280     /* Setup TPL value */
1281     data = timing.tpl / clk_period;
1282     if (data > 0)
1283         --data;
1284     img_i2c_writel(i2c, SCB_TIME_TPL_REG, data);
1285 
1286     /* Setup TPH value */
1287     data = timing.tph / clk_period;
1288     if (data > 0)
1289         --data;
1290     img_i2c_writel(i2c, SCB_TIME_TPH_REG, data);
1291 
1292     /* Setup TSDL value to TPL + TSDH + 2 */
1293     img_i2c_writel(i2c, SCB_TIME_TSDL_REG, data + tsdh + 2);
1294 
1295     /* Setup TP2S value */
1296     data = timing.tp2s / clk_period;
1297     if (data > 0)
1298         --data;
1299     img_i2c_writel(i2c, SCB_TIME_TP2S_REG, data);
1300 
1301     img_i2c_writel(i2c, SCB_TIME_TBI_REG, TIMEOUT_TBI);
1302     img_i2c_writel(i2c, SCB_TIME_TSL_REG, TIMEOUT_TSL);
1303     img_i2c_writel(i2c, SCB_TIME_TDL_REG, TIMEOUT_TDL);
1304 
1305     /* Take module out of soft reset and enable clocks */
1306     img_i2c_soft_reset(i2c);
1307 
1308     /* Disable all interrupts */
1309     img_i2c_writel(i2c, SCB_INT_MASK_REG, 0);
1310 
1311     /* Clear all interrupts */
1312     img_i2c_writel(i2c, SCB_INT_CLEAR_REG, ~0);
1313 
1314     /* Clear the scb_line_status events */
1315     img_i2c_writel(i2c, SCB_CLEAR_REG, ~0);
1316 
1317     /* Enable interrupts */
1318     img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
1319 
1320     /* Perform a synchronous sequence to reset the bus */
1321     ret = img_i2c_reset_bus(i2c);
1322 
1323     pm_runtime_mark_last_busy(i2c->adap.dev.parent);
1324     pm_runtime_put_autosuspend(i2c->adap.dev.parent);
1325 
1326     return ret;
1327 }
1328 
1329 static int img_i2c_probe(struct platform_device *pdev)
1330 {
1331     struct device_node *node = pdev->dev.of_node;
1332     struct img_i2c *i2c;
1333     int irq, ret;
1334     u32 val;
1335 
1336     i2c = devm_kzalloc(&pdev->dev, sizeof(struct img_i2c), GFP_KERNEL);
1337     if (!i2c)
1338         return -ENOMEM;
1339 
1340     i2c->base = devm_platform_ioremap_resource(pdev, 0);
1341     if (IS_ERR(i2c->base))
1342         return PTR_ERR(i2c->base);
1343 
1344     irq = platform_get_irq(pdev, 0);
1345     if (irq < 0)
1346         return irq;
1347 
1348     i2c->sys_clk = devm_clk_get(&pdev->dev, "sys");
1349     if (IS_ERR(i2c->sys_clk)) {
1350         dev_err(&pdev->dev, "can't get system clock\n");
1351         return PTR_ERR(i2c->sys_clk);
1352     }
1353 
1354     i2c->scb_clk = devm_clk_get(&pdev->dev, "scb");
1355     if (IS_ERR(i2c->scb_clk)) {
1356         dev_err(&pdev->dev, "can't get core clock\n");
1357         return PTR_ERR(i2c->scb_clk);
1358     }
1359 
1360     ret = devm_request_irq(&pdev->dev, irq, img_i2c_isr, 0,
1361                    pdev->name, i2c);
1362     if (ret) {
1363         dev_err(&pdev->dev, "can't request irq %d\n", irq);
1364         return ret;
1365     }
1366 
1367     /* Set up the exception check timer */
1368     timer_setup(&i2c->check_timer, img_i2c_check_timer, 0);
1369 
1370     i2c->bitrate = timings[0].max_bitrate;
1371     if (!of_property_read_u32(node, "clock-frequency", &val))
1372         i2c->bitrate = val;
1373 
1374     i2c_set_adapdata(&i2c->adap, i2c);
1375     i2c->adap.dev.parent = &pdev->dev;
1376     i2c->adap.dev.of_node = node;
1377     i2c->adap.owner = THIS_MODULE;
1378     i2c->adap.algo = &img_i2c_algo;
1379     i2c->adap.retries = 5;
1380     i2c->adap.nr = pdev->id;
1381     snprintf(i2c->adap.name, sizeof(i2c->adap.name), "IMG SCB I2C");
1382 
1383     img_i2c_switch_mode(i2c, MODE_INACTIVE);
1384     spin_lock_init(&i2c->lock);
1385     init_completion(&i2c->msg_complete);
1386 
1387     platform_set_drvdata(pdev, i2c);
1388 
1389     pm_runtime_set_autosuspend_delay(&pdev->dev, IMG_I2C_PM_TIMEOUT);
1390     pm_runtime_use_autosuspend(&pdev->dev);
1391     pm_runtime_enable(&pdev->dev);
1392     if (!pm_runtime_enabled(&pdev->dev)) {
1393         ret = img_i2c_runtime_resume(&pdev->dev);
1394         if (ret)
1395             return ret;
1396     }
1397 
1398     ret = img_i2c_init(i2c);
1399     if (ret)
1400         goto rpm_disable;
1401 
1402     ret = i2c_add_numbered_adapter(&i2c->adap);
1403     if (ret < 0)
1404         goto rpm_disable;
1405 
1406     return 0;
1407 
1408 rpm_disable:
1409     if (!pm_runtime_enabled(&pdev->dev))
1410         img_i2c_runtime_suspend(&pdev->dev);
1411     pm_runtime_disable(&pdev->dev);
1412     pm_runtime_dont_use_autosuspend(&pdev->dev);
1413     return ret;
1414 }
1415 
1416 static int img_i2c_remove(struct platform_device *dev)
1417 {
1418     struct img_i2c *i2c = platform_get_drvdata(dev);
1419 
1420     i2c_del_adapter(&i2c->adap);
1421     pm_runtime_disable(&dev->dev);
1422     if (!pm_runtime_status_suspended(&dev->dev))
1423         img_i2c_runtime_suspend(&dev->dev);
1424 
1425     return 0;
1426 }
1427 
1428 static int img_i2c_runtime_suspend(struct device *dev)
1429 {
1430     struct img_i2c *i2c = dev_get_drvdata(dev);
1431 
1432     clk_disable_unprepare(i2c->scb_clk);
1433     clk_disable_unprepare(i2c->sys_clk);
1434 
1435     return 0;
1436 }
1437 
1438 static int img_i2c_runtime_resume(struct device *dev)
1439 {
1440     struct img_i2c *i2c = dev_get_drvdata(dev);
1441     int ret;
1442 
1443     ret = clk_prepare_enable(i2c->sys_clk);
1444     if (ret) {
1445         dev_err(dev, "Unable to enable sys clock\n");
1446         return ret;
1447     }
1448 
1449     ret = clk_prepare_enable(i2c->scb_clk);
1450     if (ret) {
1451         dev_err(dev, "Unable to enable scb clock\n");
1452         clk_disable_unprepare(i2c->sys_clk);
1453         return ret;
1454     }
1455 
1456     return 0;
1457 }
1458 
1459 #ifdef CONFIG_PM_SLEEP
1460 static int img_i2c_suspend(struct device *dev)
1461 {
1462     struct img_i2c *i2c = dev_get_drvdata(dev);
1463     int ret;
1464 
1465     ret = pm_runtime_force_suspend(dev);
1466     if (ret)
1467         return ret;
1468 
1469     img_i2c_switch_mode(i2c, MODE_SUSPEND);
1470 
1471     return 0;
1472 }
1473 
1474 static int img_i2c_resume(struct device *dev)
1475 {
1476     struct img_i2c *i2c = dev_get_drvdata(dev);
1477     int ret;
1478 
1479     ret = pm_runtime_force_resume(dev);
1480     if (ret)
1481         return ret;
1482 
1483     img_i2c_init(i2c);
1484 
1485     return 0;
1486 }
1487 #endif /* CONFIG_PM_SLEEP */
1488 
1489 static const struct dev_pm_ops img_i2c_pm = {
1490     SET_RUNTIME_PM_OPS(img_i2c_runtime_suspend,
1491                img_i2c_runtime_resume,
1492                NULL)
1493     SET_SYSTEM_SLEEP_PM_OPS(img_i2c_suspend, img_i2c_resume)
1494 };
1495 
1496 static const struct of_device_id img_scb_i2c_match[] = {
1497     { .compatible = "img,scb-i2c" },
1498     { }
1499 };
1500 MODULE_DEVICE_TABLE(of, img_scb_i2c_match);
1501 
1502 static struct platform_driver img_scb_i2c_driver = {
1503     .driver = {
1504         .name       = "img-i2c-scb",
1505         .of_match_table = img_scb_i2c_match,
1506         .pm     = &img_i2c_pm,
1507     },
1508     .probe = img_i2c_probe,
1509     .remove = img_i2c_remove,
1510 };
1511 module_platform_driver(img_scb_i2c_driver);
1512 
1513 MODULE_AUTHOR("James Hogan <jhogan@kernel.org>");
1514 MODULE_DESCRIPTION("IMG host I2C driver");
1515 MODULE_LICENSE("GPL v2");