1d211e62aSManivannan Sadhasivam // SPDX-License-Identifier: GPL-2.0-or-later 2d211e62aSManivannan Sadhasivam /* 3d211e62aSManivannan Sadhasivam * Actions Semiconductor Owl SoC's I2C driver 4d211e62aSManivannan Sadhasivam * 5d211e62aSManivannan Sadhasivam * Copyright (c) 2014 Actions Semi Inc. 6d211e62aSManivannan Sadhasivam * Author: David Liu <liuwei@actions-semi.com> 7d211e62aSManivannan Sadhasivam * 8d211e62aSManivannan Sadhasivam * Copyright (c) 2018 Linaro Ltd. 9d211e62aSManivannan Sadhasivam * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> 10d211e62aSManivannan Sadhasivam */ 11d211e62aSManivannan Sadhasivam 12d211e62aSManivannan Sadhasivam #include <linux/clk.h> 13d211e62aSManivannan Sadhasivam #include <linux/delay.h> 14d211e62aSManivannan Sadhasivam #include <linux/i2c.h> 15d211e62aSManivannan Sadhasivam #include <linux/interrupt.h> 16d211e62aSManivannan Sadhasivam #include <linux/io.h> 17d211e62aSManivannan Sadhasivam #include <linux/module.h> 18d211e62aSManivannan Sadhasivam #include <linux/of_device.h> 19d211e62aSManivannan Sadhasivam 20d211e62aSManivannan Sadhasivam /* I2C registers */ 21d211e62aSManivannan Sadhasivam #define OWL_I2C_REG_CTL 0x0000 22d211e62aSManivannan Sadhasivam #define OWL_I2C_REG_CLKDIV 0x0004 23d211e62aSManivannan Sadhasivam #define OWL_I2C_REG_STAT 0x0008 24d211e62aSManivannan Sadhasivam #define OWL_I2C_REG_ADDR 0x000C 25d211e62aSManivannan Sadhasivam #define OWL_I2C_REG_TXDAT 0x0010 26d211e62aSManivannan Sadhasivam #define OWL_I2C_REG_RXDAT 0x0014 27d211e62aSManivannan Sadhasivam #define OWL_I2C_REG_CMD 0x0018 28d211e62aSManivannan Sadhasivam #define OWL_I2C_REG_FIFOCTL 0x001C 29d211e62aSManivannan Sadhasivam #define OWL_I2C_REG_FIFOSTAT 0x0020 30d211e62aSManivannan Sadhasivam #define OWL_I2C_REG_DATCNT 0x0024 31d211e62aSManivannan Sadhasivam #define OWL_I2C_REG_RCNT 0x0028 32d211e62aSManivannan Sadhasivam 33d211e62aSManivannan Sadhasivam /* I2Cx_CTL Bit Mask */ 34d211e62aSManivannan Sadhasivam #define OWL_I2C_CTL_RB BIT(1) 35d211e62aSManivannan Sadhasivam #define OWL_I2C_CTL_GBCC(x) (((x) & 0x3) << 2) 36d211e62aSManivannan Sadhasivam #define OWL_I2C_CTL_GBCC_NONE OWL_I2C_CTL_GBCC(0) 37d211e62aSManivannan Sadhasivam #define OWL_I2C_CTL_GBCC_START OWL_I2C_CTL_GBCC(1) 38d211e62aSManivannan Sadhasivam #define OWL_I2C_CTL_GBCC_STOP OWL_I2C_CTL_GBCC(2) 39d211e62aSManivannan Sadhasivam #define OWL_I2C_CTL_GBCC_RSTART OWL_I2C_CTL_GBCC(3) 40d211e62aSManivannan Sadhasivam #define OWL_I2C_CTL_IRQE BIT(5) 41d211e62aSManivannan Sadhasivam #define OWL_I2C_CTL_EN BIT(7) 42d211e62aSManivannan Sadhasivam #define OWL_I2C_CTL_AE BIT(8) 43d211e62aSManivannan Sadhasivam #define OWL_I2C_CTL_SHSM BIT(10) 44d211e62aSManivannan Sadhasivam 45d211e62aSManivannan Sadhasivam #define OWL_I2C_DIV_FACTOR(x) ((x) & 0xff) 46d211e62aSManivannan Sadhasivam 47d211e62aSManivannan Sadhasivam /* I2Cx_STAT Bit Mask */ 48d211e62aSManivannan Sadhasivam #define OWL_I2C_STAT_RACK BIT(0) 49d211e62aSManivannan Sadhasivam #define OWL_I2C_STAT_BEB BIT(1) 50d211e62aSManivannan Sadhasivam #define OWL_I2C_STAT_IRQP BIT(2) 51d211e62aSManivannan Sadhasivam #define OWL_I2C_STAT_LAB BIT(3) 52d211e62aSManivannan Sadhasivam #define OWL_I2C_STAT_STPD BIT(4) 53d211e62aSManivannan Sadhasivam #define OWL_I2C_STAT_STAD BIT(5) 54d211e62aSManivannan Sadhasivam #define OWL_I2C_STAT_BBB BIT(6) 55d211e62aSManivannan Sadhasivam #define OWL_I2C_STAT_TCB BIT(7) 56d211e62aSManivannan Sadhasivam #define OWL_I2C_STAT_LBST BIT(8) 57d211e62aSManivannan Sadhasivam #define OWL_I2C_STAT_SAMB BIT(9) 58d211e62aSManivannan Sadhasivam #define OWL_I2C_STAT_SRGC BIT(10) 59d211e62aSManivannan Sadhasivam 60d211e62aSManivannan Sadhasivam /* I2Cx_CMD Bit Mask */ 61d211e62aSManivannan Sadhasivam #define OWL_I2C_CMD_SBE BIT(0) 62d211e62aSManivannan Sadhasivam #define OWL_I2C_CMD_RBE BIT(4) 63d211e62aSManivannan Sadhasivam #define OWL_I2C_CMD_DE BIT(8) 64d211e62aSManivannan Sadhasivam #define OWL_I2C_CMD_NS BIT(9) 65d211e62aSManivannan Sadhasivam #define OWL_I2C_CMD_SE BIT(10) 66d211e62aSManivannan Sadhasivam #define OWL_I2C_CMD_MSS BIT(11) 67d211e62aSManivannan Sadhasivam #define OWL_I2C_CMD_WRS BIT(12) 68d211e62aSManivannan Sadhasivam #define OWL_I2C_CMD_SECL BIT(15) 69d211e62aSManivannan Sadhasivam 70d211e62aSManivannan Sadhasivam #define OWL_I2C_CMD_AS(x) (((x) & 0x7) << 1) 71d211e62aSManivannan Sadhasivam #define OWL_I2C_CMD_SAS(x) (((x) & 0x7) << 5) 72d211e62aSManivannan Sadhasivam 73d211e62aSManivannan Sadhasivam /* I2Cx_FIFOCTL Bit Mask */ 74d211e62aSManivannan Sadhasivam #define OWL_I2C_FIFOCTL_NIB BIT(0) 75d211e62aSManivannan Sadhasivam #define OWL_I2C_FIFOCTL_RFR BIT(1) 76d211e62aSManivannan Sadhasivam #define OWL_I2C_FIFOCTL_TFR BIT(2) 77d211e62aSManivannan Sadhasivam 78d211e62aSManivannan Sadhasivam /* I2Cc_FIFOSTAT Bit Mask */ 79d211e62aSManivannan Sadhasivam #define OWL_I2C_FIFOSTAT_RNB BIT(1) 80d211e62aSManivannan Sadhasivam #define OWL_I2C_FIFOSTAT_RFE BIT(2) 81d211e62aSManivannan Sadhasivam #define OWL_I2C_FIFOSTAT_TFF BIT(5) 82d211e62aSManivannan Sadhasivam #define OWL_I2C_FIFOSTAT_TFD GENMASK(23, 16) 83d211e62aSManivannan Sadhasivam #define OWL_I2C_FIFOSTAT_RFD GENMASK(15, 8) 84d211e62aSManivannan Sadhasivam 85d211e62aSManivannan Sadhasivam /* I2C bus timeout */ 86d211e62aSManivannan Sadhasivam #define OWL_I2C_TIMEOUT msecs_to_jiffies(4 * 1000) 87d211e62aSManivannan Sadhasivam 88d211e62aSManivannan Sadhasivam #define OWL_I2C_MAX_RETRIES 50 89d211e62aSManivannan Sadhasivam 90d211e62aSManivannan Sadhasivam struct owl_i2c_dev { 91d211e62aSManivannan Sadhasivam struct i2c_adapter adap; 92d211e62aSManivannan Sadhasivam struct i2c_msg *msg; 93d211e62aSManivannan Sadhasivam struct completion msg_complete; 94d211e62aSManivannan Sadhasivam struct clk *clk; 95d211e62aSManivannan Sadhasivam spinlock_t lock; 96d211e62aSManivannan Sadhasivam void __iomem *base; 97d211e62aSManivannan Sadhasivam unsigned long clk_rate; 98d211e62aSManivannan Sadhasivam u32 bus_freq; 99d211e62aSManivannan Sadhasivam u32 msg_ptr; 100d211e62aSManivannan Sadhasivam int err; 101d211e62aSManivannan Sadhasivam }; 102d211e62aSManivannan Sadhasivam 103d211e62aSManivannan Sadhasivam static void owl_i2c_update_reg(void __iomem *reg, unsigned int val, bool state) 104d211e62aSManivannan Sadhasivam { 105d211e62aSManivannan Sadhasivam unsigned int regval; 106d211e62aSManivannan Sadhasivam 107d211e62aSManivannan Sadhasivam regval = readl(reg); 108d211e62aSManivannan Sadhasivam 109d211e62aSManivannan Sadhasivam if (state) 110d211e62aSManivannan Sadhasivam regval |= val; 111d211e62aSManivannan Sadhasivam else 112d211e62aSManivannan Sadhasivam regval &= ~val; 113d211e62aSManivannan Sadhasivam 114d211e62aSManivannan Sadhasivam writel(regval, reg); 115d211e62aSManivannan Sadhasivam } 116d211e62aSManivannan Sadhasivam 117d211e62aSManivannan Sadhasivam static void owl_i2c_reset(struct owl_i2c_dev *i2c_dev) 118d211e62aSManivannan Sadhasivam { 119d211e62aSManivannan Sadhasivam owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, 120d211e62aSManivannan Sadhasivam OWL_I2C_CTL_EN, false); 121d211e62aSManivannan Sadhasivam mdelay(1); 122d211e62aSManivannan Sadhasivam owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, 123d211e62aSManivannan Sadhasivam OWL_I2C_CTL_EN, true); 124d211e62aSManivannan Sadhasivam 125d211e62aSManivannan Sadhasivam /* Clear status registers */ 126d211e62aSManivannan Sadhasivam writel(0, i2c_dev->base + OWL_I2C_REG_STAT); 127d211e62aSManivannan Sadhasivam } 128d211e62aSManivannan Sadhasivam 129d211e62aSManivannan Sadhasivam static int owl_i2c_reset_fifo(struct owl_i2c_dev *i2c_dev) 130d211e62aSManivannan Sadhasivam { 131d211e62aSManivannan Sadhasivam unsigned int val, timeout = 0; 132d211e62aSManivannan Sadhasivam 133d211e62aSManivannan Sadhasivam /* Reset FIFO */ 134d211e62aSManivannan Sadhasivam owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL, 135d211e62aSManivannan Sadhasivam OWL_I2C_FIFOCTL_RFR | OWL_I2C_FIFOCTL_TFR, 136d211e62aSManivannan Sadhasivam true); 137d211e62aSManivannan Sadhasivam 138d211e62aSManivannan Sadhasivam /* Wait 50ms for FIFO reset complete */ 139d211e62aSManivannan Sadhasivam do { 140d211e62aSManivannan Sadhasivam val = readl(i2c_dev->base + OWL_I2C_REG_FIFOCTL); 141d211e62aSManivannan Sadhasivam if (!(val & (OWL_I2C_FIFOCTL_RFR | OWL_I2C_FIFOCTL_TFR))) 142d211e62aSManivannan Sadhasivam break; 143d211e62aSManivannan Sadhasivam usleep_range(500, 1000); 144d211e62aSManivannan Sadhasivam } while (timeout++ < OWL_I2C_MAX_RETRIES); 145d211e62aSManivannan Sadhasivam 146d211e62aSManivannan Sadhasivam if (timeout > OWL_I2C_MAX_RETRIES) { 147d211e62aSManivannan Sadhasivam dev_err(&i2c_dev->adap.dev, "FIFO reset timeout\n"); 148d211e62aSManivannan Sadhasivam return -ETIMEDOUT; 149d211e62aSManivannan Sadhasivam } 150d211e62aSManivannan Sadhasivam 151d211e62aSManivannan Sadhasivam return 0; 152d211e62aSManivannan Sadhasivam } 153d211e62aSManivannan Sadhasivam 154d211e62aSManivannan Sadhasivam static void owl_i2c_set_freq(struct owl_i2c_dev *i2c_dev) 155d211e62aSManivannan Sadhasivam { 156d211e62aSManivannan Sadhasivam unsigned int val; 157d211e62aSManivannan Sadhasivam 158d211e62aSManivannan Sadhasivam val = DIV_ROUND_UP(i2c_dev->clk_rate, i2c_dev->bus_freq * 16); 159d211e62aSManivannan Sadhasivam 160d211e62aSManivannan Sadhasivam /* Set clock divider factor */ 161d211e62aSManivannan Sadhasivam writel(OWL_I2C_DIV_FACTOR(val), i2c_dev->base + OWL_I2C_REG_CLKDIV); 162d211e62aSManivannan Sadhasivam } 163d211e62aSManivannan Sadhasivam 164d211e62aSManivannan Sadhasivam static irqreturn_t owl_i2c_interrupt(int irq, void *_dev) 165d211e62aSManivannan Sadhasivam { 166d211e62aSManivannan Sadhasivam struct owl_i2c_dev *i2c_dev = _dev; 167d211e62aSManivannan Sadhasivam struct i2c_msg *msg = i2c_dev->msg; 168d211e62aSManivannan Sadhasivam unsigned int stat, fifostat; 169d211e62aSManivannan Sadhasivam 170*06856269SBarry Song spin_lock(&i2c_dev->lock); 171d211e62aSManivannan Sadhasivam 172d211e62aSManivannan Sadhasivam i2c_dev->err = 0; 173d211e62aSManivannan Sadhasivam 174d211e62aSManivannan Sadhasivam /* Handle NACK from slave */ 175d211e62aSManivannan Sadhasivam fifostat = readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT); 176d211e62aSManivannan Sadhasivam if (fifostat & OWL_I2C_FIFOSTAT_RNB) { 177d211e62aSManivannan Sadhasivam i2c_dev->err = -ENXIO; 178d211e62aSManivannan Sadhasivam goto stop; 179d211e62aSManivannan Sadhasivam } 180d211e62aSManivannan Sadhasivam 181d211e62aSManivannan Sadhasivam /* Handle bus error */ 182d211e62aSManivannan Sadhasivam stat = readl(i2c_dev->base + OWL_I2C_REG_STAT); 183d211e62aSManivannan Sadhasivam if (stat & OWL_I2C_STAT_BEB) { 184d211e62aSManivannan Sadhasivam i2c_dev->err = -EIO; 185d211e62aSManivannan Sadhasivam goto stop; 186d211e62aSManivannan Sadhasivam } 187d211e62aSManivannan Sadhasivam 188d211e62aSManivannan Sadhasivam /* Handle FIFO read */ 189d211e62aSManivannan Sadhasivam if (msg->flags & I2C_M_RD) { 190d211e62aSManivannan Sadhasivam while ((readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) & 191d211e62aSManivannan Sadhasivam OWL_I2C_FIFOSTAT_RFE) && i2c_dev->msg_ptr < msg->len) { 192d211e62aSManivannan Sadhasivam msg->buf[i2c_dev->msg_ptr++] = readl(i2c_dev->base + 193d211e62aSManivannan Sadhasivam OWL_I2C_REG_RXDAT); 194d211e62aSManivannan Sadhasivam } 195d211e62aSManivannan Sadhasivam } else { 196d211e62aSManivannan Sadhasivam /* Handle the remaining bytes which were not sent */ 197d211e62aSManivannan Sadhasivam while (!(readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) & 198d211e62aSManivannan Sadhasivam OWL_I2C_FIFOSTAT_TFF) && i2c_dev->msg_ptr < msg->len) { 199d211e62aSManivannan Sadhasivam writel(msg->buf[i2c_dev->msg_ptr++], 200d211e62aSManivannan Sadhasivam i2c_dev->base + OWL_I2C_REG_TXDAT); 201d211e62aSManivannan Sadhasivam } 202d211e62aSManivannan Sadhasivam } 203d211e62aSManivannan Sadhasivam 204d211e62aSManivannan Sadhasivam stop: 205d211e62aSManivannan Sadhasivam /* Clear pending interrupts */ 206d211e62aSManivannan Sadhasivam owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_STAT, 207d211e62aSManivannan Sadhasivam OWL_I2C_STAT_IRQP, true); 208d211e62aSManivannan Sadhasivam 209d211e62aSManivannan Sadhasivam complete_all(&i2c_dev->msg_complete); 210*06856269SBarry Song spin_unlock(&i2c_dev->lock); 211d211e62aSManivannan Sadhasivam 212d211e62aSManivannan Sadhasivam return IRQ_HANDLED; 213d211e62aSManivannan Sadhasivam } 214d211e62aSManivannan Sadhasivam 215d211e62aSManivannan Sadhasivam static u32 owl_i2c_func(struct i2c_adapter *adap) 216d211e62aSManivannan Sadhasivam { 217d211e62aSManivannan Sadhasivam return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 218d211e62aSManivannan Sadhasivam } 219d211e62aSManivannan Sadhasivam 220d211e62aSManivannan Sadhasivam static int owl_i2c_check_bus_busy(struct i2c_adapter *adap) 221d211e62aSManivannan Sadhasivam { 222d211e62aSManivannan Sadhasivam struct owl_i2c_dev *i2c_dev = i2c_get_adapdata(adap); 223d211e62aSManivannan Sadhasivam unsigned long timeout; 224d211e62aSManivannan Sadhasivam 225d211e62aSManivannan Sadhasivam /* Check for Bus busy */ 226d211e62aSManivannan Sadhasivam timeout = jiffies + OWL_I2C_TIMEOUT; 227d211e62aSManivannan Sadhasivam while (readl(i2c_dev->base + OWL_I2C_REG_STAT) & OWL_I2C_STAT_BBB) { 228d211e62aSManivannan Sadhasivam if (time_after(jiffies, timeout)) { 229d211e62aSManivannan Sadhasivam dev_err(&adap->dev, "Bus busy timeout\n"); 230d211e62aSManivannan Sadhasivam return -ETIMEDOUT; 231d211e62aSManivannan Sadhasivam } 232d211e62aSManivannan Sadhasivam } 233d211e62aSManivannan Sadhasivam 234d211e62aSManivannan Sadhasivam return 0; 235d211e62aSManivannan Sadhasivam } 236d211e62aSManivannan Sadhasivam 237d211e62aSManivannan Sadhasivam static int owl_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, 238d211e62aSManivannan Sadhasivam int num) 239d211e62aSManivannan Sadhasivam { 240d211e62aSManivannan Sadhasivam struct owl_i2c_dev *i2c_dev = i2c_get_adapdata(adap); 241d211e62aSManivannan Sadhasivam struct i2c_msg *msg; 242d211e62aSManivannan Sadhasivam unsigned long time_left, flags; 243d211e62aSManivannan Sadhasivam unsigned int i2c_cmd, val; 244d211e62aSManivannan Sadhasivam unsigned int addr; 245d211e62aSManivannan Sadhasivam int ret, idx; 246d211e62aSManivannan Sadhasivam 247d211e62aSManivannan Sadhasivam spin_lock_irqsave(&i2c_dev->lock, flags); 248d211e62aSManivannan Sadhasivam 249d211e62aSManivannan Sadhasivam /* Reset I2C controller */ 250d211e62aSManivannan Sadhasivam owl_i2c_reset(i2c_dev); 251d211e62aSManivannan Sadhasivam 252d211e62aSManivannan Sadhasivam /* Set bus frequency */ 253d211e62aSManivannan Sadhasivam owl_i2c_set_freq(i2c_dev); 254d211e62aSManivannan Sadhasivam 255d211e62aSManivannan Sadhasivam /* 256d211e62aSManivannan Sadhasivam * Spinlock should be released before calling reset FIFO and 257d211e62aSManivannan Sadhasivam * bus busy check since those functions may sleep 258d211e62aSManivannan Sadhasivam */ 259d211e62aSManivannan Sadhasivam spin_unlock_irqrestore(&i2c_dev->lock, flags); 260d211e62aSManivannan Sadhasivam 261d211e62aSManivannan Sadhasivam /* Reset FIFO */ 262d211e62aSManivannan Sadhasivam ret = owl_i2c_reset_fifo(i2c_dev); 263d211e62aSManivannan Sadhasivam if (ret) 264d211e62aSManivannan Sadhasivam goto unlocked_err_exit; 265d211e62aSManivannan Sadhasivam 266d211e62aSManivannan Sadhasivam /* Check for bus busy */ 267d211e62aSManivannan Sadhasivam ret = owl_i2c_check_bus_busy(adap); 268d211e62aSManivannan Sadhasivam if (ret) 269d211e62aSManivannan Sadhasivam goto unlocked_err_exit; 270d211e62aSManivannan Sadhasivam 271d211e62aSManivannan Sadhasivam spin_lock_irqsave(&i2c_dev->lock, flags); 272d211e62aSManivannan Sadhasivam 273d211e62aSManivannan Sadhasivam /* Check for Arbitration lost */ 274d211e62aSManivannan Sadhasivam val = readl(i2c_dev->base + OWL_I2C_REG_STAT); 275d211e62aSManivannan Sadhasivam if (val & OWL_I2C_STAT_LAB) { 276d211e62aSManivannan Sadhasivam val &= ~OWL_I2C_STAT_LAB; 277d211e62aSManivannan Sadhasivam writel(val, i2c_dev->base + OWL_I2C_REG_STAT); 278d211e62aSManivannan Sadhasivam ret = -EAGAIN; 279d211e62aSManivannan Sadhasivam goto err_exit; 280d211e62aSManivannan Sadhasivam } 281d211e62aSManivannan Sadhasivam 282d211e62aSManivannan Sadhasivam reinit_completion(&i2c_dev->msg_complete); 283d211e62aSManivannan Sadhasivam 284d211e62aSManivannan Sadhasivam /* Enable I2C controller interrupt */ 285d211e62aSManivannan Sadhasivam owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, 286d211e62aSManivannan Sadhasivam OWL_I2C_CTL_IRQE, true); 287d211e62aSManivannan Sadhasivam 288d211e62aSManivannan Sadhasivam /* 289d211e62aSManivannan Sadhasivam * Select: FIFO enable, Master mode, Stop enable, Data count enable, 290d211e62aSManivannan Sadhasivam * Send start bit 291d211e62aSManivannan Sadhasivam */ 292d211e62aSManivannan Sadhasivam i2c_cmd = OWL_I2C_CMD_SECL | OWL_I2C_CMD_MSS | OWL_I2C_CMD_SE | 293d211e62aSManivannan Sadhasivam OWL_I2C_CMD_NS | OWL_I2C_CMD_DE | OWL_I2C_CMD_SBE; 294d211e62aSManivannan Sadhasivam 295d211e62aSManivannan Sadhasivam /* Handle repeated start condition */ 296d211e62aSManivannan Sadhasivam if (num > 1) { 297d211e62aSManivannan Sadhasivam /* Set internal address length and enable repeated start */ 298d211e62aSManivannan Sadhasivam i2c_cmd |= OWL_I2C_CMD_AS(msgs[0].len + 1) | 299d211e62aSManivannan Sadhasivam OWL_I2C_CMD_SAS(1) | OWL_I2C_CMD_RBE; 300d211e62aSManivannan Sadhasivam 301d211e62aSManivannan Sadhasivam /* Write slave address */ 302d211e62aSManivannan Sadhasivam addr = i2c_8bit_addr_from_msg(&msgs[0]); 303d211e62aSManivannan Sadhasivam writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT); 304d211e62aSManivannan Sadhasivam 305d211e62aSManivannan Sadhasivam /* Write internal register address */ 306d211e62aSManivannan Sadhasivam for (idx = 0; idx < msgs[0].len; idx++) 307d211e62aSManivannan Sadhasivam writel(msgs[0].buf[idx], 308d211e62aSManivannan Sadhasivam i2c_dev->base + OWL_I2C_REG_TXDAT); 309d211e62aSManivannan Sadhasivam 310d211e62aSManivannan Sadhasivam msg = &msgs[1]; 311d211e62aSManivannan Sadhasivam } else { 312d211e62aSManivannan Sadhasivam /* Set address length */ 313d211e62aSManivannan Sadhasivam i2c_cmd |= OWL_I2C_CMD_AS(1); 314d211e62aSManivannan Sadhasivam msg = &msgs[0]; 315d211e62aSManivannan Sadhasivam } 316d211e62aSManivannan Sadhasivam 317d211e62aSManivannan Sadhasivam i2c_dev->msg = msg; 318d211e62aSManivannan Sadhasivam i2c_dev->msg_ptr = 0; 319d211e62aSManivannan Sadhasivam 320d211e62aSManivannan Sadhasivam /* Set data count for the message */ 321d211e62aSManivannan Sadhasivam writel(msg->len, i2c_dev->base + OWL_I2C_REG_DATCNT); 322d211e62aSManivannan Sadhasivam 323d211e62aSManivannan Sadhasivam addr = i2c_8bit_addr_from_msg(msg); 324d211e62aSManivannan Sadhasivam writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT); 325d211e62aSManivannan Sadhasivam 326d211e62aSManivannan Sadhasivam if (!(msg->flags & I2C_M_RD)) { 327d211e62aSManivannan Sadhasivam /* Write data to FIFO */ 328d211e62aSManivannan Sadhasivam for (idx = 0; idx < msg->len; idx++) { 329d211e62aSManivannan Sadhasivam /* Check for FIFO full */ 330d211e62aSManivannan Sadhasivam if (readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) & 331d211e62aSManivannan Sadhasivam OWL_I2C_FIFOSTAT_TFF) 332d211e62aSManivannan Sadhasivam break; 333d211e62aSManivannan Sadhasivam 334d211e62aSManivannan Sadhasivam writel(msg->buf[idx], 335d211e62aSManivannan Sadhasivam i2c_dev->base + OWL_I2C_REG_TXDAT); 336d211e62aSManivannan Sadhasivam } 337d211e62aSManivannan Sadhasivam 338d211e62aSManivannan Sadhasivam i2c_dev->msg_ptr = idx; 339d211e62aSManivannan Sadhasivam } 340d211e62aSManivannan Sadhasivam 341d211e62aSManivannan Sadhasivam /* Ignore the NACK if needed */ 342d211e62aSManivannan Sadhasivam if (msg->flags & I2C_M_IGNORE_NAK) 343d211e62aSManivannan Sadhasivam owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL, 344d211e62aSManivannan Sadhasivam OWL_I2C_FIFOCTL_NIB, true); 345d211e62aSManivannan Sadhasivam else 346d211e62aSManivannan Sadhasivam owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL, 347d211e62aSManivannan Sadhasivam OWL_I2C_FIFOCTL_NIB, false); 348d211e62aSManivannan Sadhasivam 349d211e62aSManivannan Sadhasivam /* Start the transfer */ 350d211e62aSManivannan Sadhasivam writel(i2c_cmd, i2c_dev->base + OWL_I2C_REG_CMD); 351d211e62aSManivannan Sadhasivam 352d211e62aSManivannan Sadhasivam spin_unlock_irqrestore(&i2c_dev->lock, flags); 353d211e62aSManivannan Sadhasivam 354d211e62aSManivannan Sadhasivam time_left = wait_for_completion_timeout(&i2c_dev->msg_complete, 355d211e62aSManivannan Sadhasivam adap->timeout); 356d211e62aSManivannan Sadhasivam 357d211e62aSManivannan Sadhasivam spin_lock_irqsave(&i2c_dev->lock, flags); 358d211e62aSManivannan Sadhasivam if (time_left == 0) { 359d211e62aSManivannan Sadhasivam dev_err(&adap->dev, "Transaction timed out\n"); 360d211e62aSManivannan Sadhasivam /* Send stop condition and release the bus */ 361d211e62aSManivannan Sadhasivam owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, 362d211e62aSManivannan Sadhasivam OWL_I2C_CTL_GBCC_STOP | OWL_I2C_CTL_RB, 363d211e62aSManivannan Sadhasivam true); 364d211e62aSManivannan Sadhasivam ret = -ETIMEDOUT; 365d211e62aSManivannan Sadhasivam goto err_exit; 366d211e62aSManivannan Sadhasivam } 367d211e62aSManivannan Sadhasivam 368d211e62aSManivannan Sadhasivam ret = i2c_dev->err < 0 ? i2c_dev->err : num; 369d211e62aSManivannan Sadhasivam 370d211e62aSManivannan Sadhasivam err_exit: 371d211e62aSManivannan Sadhasivam spin_unlock_irqrestore(&i2c_dev->lock, flags); 372d211e62aSManivannan Sadhasivam 373d211e62aSManivannan Sadhasivam unlocked_err_exit: 374d211e62aSManivannan Sadhasivam /* Disable I2C controller */ 375d211e62aSManivannan Sadhasivam owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL, 376d211e62aSManivannan Sadhasivam OWL_I2C_CTL_EN, false); 377d211e62aSManivannan Sadhasivam 378d211e62aSManivannan Sadhasivam return ret; 379d211e62aSManivannan Sadhasivam } 380d211e62aSManivannan Sadhasivam 381d211e62aSManivannan Sadhasivam static const struct i2c_algorithm owl_i2c_algorithm = { 382d211e62aSManivannan Sadhasivam .master_xfer = owl_i2c_master_xfer, 383d211e62aSManivannan Sadhasivam .functionality = owl_i2c_func, 384d211e62aSManivannan Sadhasivam }; 385d211e62aSManivannan Sadhasivam 386d211e62aSManivannan Sadhasivam static const struct i2c_adapter_quirks owl_i2c_quirks = { 387d211e62aSManivannan Sadhasivam .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST, 388d211e62aSManivannan Sadhasivam .max_read_len = 240, 389d211e62aSManivannan Sadhasivam .max_write_len = 240, 390d211e62aSManivannan Sadhasivam .max_comb_1st_msg_len = 6, 391d211e62aSManivannan Sadhasivam .max_comb_2nd_msg_len = 240, 392d211e62aSManivannan Sadhasivam }; 393d211e62aSManivannan Sadhasivam 394d211e62aSManivannan Sadhasivam static int owl_i2c_probe(struct platform_device *pdev) 395d211e62aSManivannan Sadhasivam { 396d211e62aSManivannan Sadhasivam struct device *dev = &pdev->dev; 397d211e62aSManivannan Sadhasivam struct owl_i2c_dev *i2c_dev; 398d211e62aSManivannan Sadhasivam int ret, irq; 399d211e62aSManivannan Sadhasivam 400d211e62aSManivannan Sadhasivam i2c_dev = devm_kzalloc(dev, sizeof(*i2c_dev), GFP_KERNEL); 401d211e62aSManivannan Sadhasivam if (!i2c_dev) 402d211e62aSManivannan Sadhasivam return -ENOMEM; 403d211e62aSManivannan Sadhasivam 404e0442d76SDejin Zheng i2c_dev->base = devm_platform_ioremap_resource(pdev, 0); 405d211e62aSManivannan Sadhasivam if (IS_ERR(i2c_dev->base)) 406d211e62aSManivannan Sadhasivam return PTR_ERR(i2c_dev->base); 407d211e62aSManivannan Sadhasivam 408d211e62aSManivannan Sadhasivam irq = platform_get_irq(pdev, 0); 409e42688edSDejin Zheng if (irq < 0) 410d211e62aSManivannan Sadhasivam return irq; 411d211e62aSManivannan Sadhasivam 412d211e62aSManivannan Sadhasivam if (of_property_read_u32(dev->of_node, "clock-frequency", 413d211e62aSManivannan Sadhasivam &i2c_dev->bus_freq)) 41490224e64SAndy Shevchenko i2c_dev->bus_freq = I2C_MAX_STANDARD_MODE_FREQ; 415d211e62aSManivannan Sadhasivam 416d211e62aSManivannan Sadhasivam /* We support only frequencies of 100k and 400k for now */ 41790224e64SAndy Shevchenko if (i2c_dev->bus_freq != I2C_MAX_STANDARD_MODE_FREQ && 41890224e64SAndy Shevchenko i2c_dev->bus_freq != I2C_MAX_FAST_MODE_FREQ) { 419d211e62aSManivannan Sadhasivam dev_err(dev, "invalid clock-frequency %d\n", i2c_dev->bus_freq); 420d211e62aSManivannan Sadhasivam return -EINVAL; 421d211e62aSManivannan Sadhasivam } 422d211e62aSManivannan Sadhasivam 423d211e62aSManivannan Sadhasivam i2c_dev->clk = devm_clk_get(dev, NULL); 424d211e62aSManivannan Sadhasivam if (IS_ERR(i2c_dev->clk)) { 425d211e62aSManivannan Sadhasivam dev_err(dev, "failed to get clock\n"); 426d211e62aSManivannan Sadhasivam return PTR_ERR(i2c_dev->clk); 427d211e62aSManivannan Sadhasivam } 428d211e62aSManivannan Sadhasivam 429d211e62aSManivannan Sadhasivam ret = clk_prepare_enable(i2c_dev->clk); 430d211e62aSManivannan Sadhasivam if (ret) 431d211e62aSManivannan Sadhasivam return ret; 432d211e62aSManivannan Sadhasivam 433d211e62aSManivannan Sadhasivam i2c_dev->clk_rate = clk_get_rate(i2c_dev->clk); 434d211e62aSManivannan Sadhasivam if (!i2c_dev->clk_rate) { 435d211e62aSManivannan Sadhasivam dev_err(dev, "input clock rate should not be zero\n"); 436d211e62aSManivannan Sadhasivam ret = -EINVAL; 437d211e62aSManivannan Sadhasivam goto disable_clk; 438d211e62aSManivannan Sadhasivam } 439d211e62aSManivannan Sadhasivam 440d211e62aSManivannan Sadhasivam init_completion(&i2c_dev->msg_complete); 441d211e62aSManivannan Sadhasivam spin_lock_init(&i2c_dev->lock); 442d211e62aSManivannan Sadhasivam i2c_dev->adap.owner = THIS_MODULE; 443d211e62aSManivannan Sadhasivam i2c_dev->adap.algo = &owl_i2c_algorithm; 444d211e62aSManivannan Sadhasivam i2c_dev->adap.timeout = OWL_I2C_TIMEOUT; 445d211e62aSManivannan Sadhasivam i2c_dev->adap.quirks = &owl_i2c_quirks; 446d211e62aSManivannan Sadhasivam i2c_dev->adap.dev.parent = dev; 447d211e62aSManivannan Sadhasivam i2c_dev->adap.dev.of_node = dev->of_node; 448d211e62aSManivannan Sadhasivam snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name), 449d211e62aSManivannan Sadhasivam "%s", "OWL I2C adapter"); 450d211e62aSManivannan Sadhasivam i2c_set_adapdata(&i2c_dev->adap, i2c_dev); 451d211e62aSManivannan Sadhasivam 452d211e62aSManivannan Sadhasivam platform_set_drvdata(pdev, i2c_dev); 453d211e62aSManivannan Sadhasivam 454d211e62aSManivannan Sadhasivam ret = devm_request_irq(dev, irq, owl_i2c_interrupt, 0, pdev->name, 455d211e62aSManivannan Sadhasivam i2c_dev); 456d211e62aSManivannan Sadhasivam if (ret) { 457d211e62aSManivannan Sadhasivam dev_err(dev, "failed to request irq %d\n", irq); 458d211e62aSManivannan Sadhasivam goto disable_clk; 459d211e62aSManivannan Sadhasivam } 460d211e62aSManivannan Sadhasivam 461d211e62aSManivannan Sadhasivam return i2c_add_adapter(&i2c_dev->adap); 462d211e62aSManivannan Sadhasivam 463d211e62aSManivannan Sadhasivam disable_clk: 464d211e62aSManivannan Sadhasivam clk_disable_unprepare(i2c_dev->clk); 465d211e62aSManivannan Sadhasivam 466d211e62aSManivannan Sadhasivam return ret; 467d211e62aSManivannan Sadhasivam } 468d211e62aSManivannan Sadhasivam 469d211e62aSManivannan Sadhasivam static const struct of_device_id owl_i2c_of_match[] = { 47081482d13SParthiban Nallathambi { .compatible = "actions,s700-i2c" }, 471d211e62aSManivannan Sadhasivam { .compatible = "actions,s900-i2c" }, 472d211e62aSManivannan Sadhasivam { /* sentinel */ } 473d211e62aSManivannan Sadhasivam }; 474d211e62aSManivannan Sadhasivam MODULE_DEVICE_TABLE(of, owl_i2c_of_match); 475d211e62aSManivannan Sadhasivam 476d211e62aSManivannan Sadhasivam static struct platform_driver owl_i2c_driver = { 477d211e62aSManivannan Sadhasivam .probe = owl_i2c_probe, 478d211e62aSManivannan Sadhasivam .driver = { 479d211e62aSManivannan Sadhasivam .name = "owl-i2c", 480d211e62aSManivannan Sadhasivam .of_match_table = of_match_ptr(owl_i2c_of_match), 481d211e62aSManivannan Sadhasivam }, 482d211e62aSManivannan Sadhasivam }; 483d211e62aSManivannan Sadhasivam module_platform_driver(owl_i2c_driver); 484d211e62aSManivannan Sadhasivam 485d211e62aSManivannan Sadhasivam MODULE_AUTHOR("David Liu <liuwei@actions-semi.com>"); 486d211e62aSManivannan Sadhasivam MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>"); 487d211e62aSManivannan Sadhasivam MODULE_DESCRIPTION("Actions Semiconductor Owl SoC's I2C driver"); 488d211e62aSManivannan Sadhasivam MODULE_LICENSE("GPL"); 489