Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011, Netlogic Microsystems Inc. |
| 3 | * Copyright 2004, Matt Porter <mporter@kernel.crashing.org> |
| 4 | * |
| 5 | * This file is licensed under the terms of the GNU General Public |
| 6 | * License version 2. This program is licensed "as is" without any |
| 7 | * warranty of any kind, whether express or implied. |
| 8 | */ |
| 9 | |
Thierry Reding | 84dbf80 | 2013-01-21 11:09:03 +0100 | [diff] [blame] | 10 | #include <linux/err.h> |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 14 | #include <linux/ioport.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/platform_device.h> |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 20 | #include <linux/of_device.h> |
| 21 | #include <linux/clk.h> |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 22 | |
| 23 | /* XLR I2C REGISTERS */ |
| 24 | #define XLR_I2C_CFG 0x00 |
| 25 | #define XLR_I2C_CLKDIV 0x01 |
| 26 | #define XLR_I2C_DEVADDR 0x02 |
| 27 | #define XLR_I2C_ADDR 0x03 |
| 28 | #define XLR_I2C_DATAOUT 0x04 |
| 29 | #define XLR_I2C_DATAIN 0x05 |
| 30 | #define XLR_I2C_STATUS 0x06 |
| 31 | #define XLR_I2C_STARTXFR 0x07 |
| 32 | #define XLR_I2C_BYTECNT 0x08 |
| 33 | #define XLR_I2C_HDSTATIM 0x09 |
| 34 | |
| 35 | /* XLR I2C REGISTERS FLAGS */ |
| 36 | #define XLR_I2C_BUS_BUSY 0x01 |
| 37 | #define XLR_I2C_SDOEMPTY 0x02 |
| 38 | #define XLR_I2C_RXRDY 0x04 |
| 39 | #define XLR_I2C_ACK_ERR 0x08 |
| 40 | #define XLR_I2C_ARB_STARTERR 0x30 |
| 41 | |
| 42 | /* Register Values */ |
| 43 | #define XLR_I2C_CFG_ADDR 0xF8 |
| 44 | #define XLR_I2C_CFG_NOADDR 0xFA |
| 45 | #define XLR_I2C_STARTXFR_ND 0x02 /* No Data */ |
| 46 | #define XLR_I2C_STARTXFR_RD 0x01 /* Read */ |
| 47 | #define XLR_I2C_STARTXFR_WR 0x00 /* Write */ |
| 48 | |
| 49 | #define XLR_I2C_TIMEOUT 10 /* timeout per byte in msec */ |
| 50 | |
| 51 | /* |
| 52 | * On XLR/XLS, we need to use __raw_ IO to read the I2C registers |
| 53 | * because they are in the big-endian MMIO area on the SoC. |
| 54 | * |
| 55 | * The readl/writel implementation on XLR/XLS byteswaps, because |
| 56 | * those are for its little-endian PCI space (see arch/mips/Kconfig). |
| 57 | */ |
| 58 | static inline void xlr_i2c_wreg(u32 __iomem *base, unsigned int reg, u32 val) |
| 59 | { |
| 60 | __raw_writel(val, base + reg); |
| 61 | } |
| 62 | |
| 63 | static inline u32 xlr_i2c_rdreg(u32 __iomem *base, unsigned int reg) |
| 64 | { |
| 65 | return __raw_readl(base + reg); |
| 66 | } |
| 67 | |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 68 | struct xlr_i2c_config { |
| 69 | u32 status_busy; /* value of STATUS[0] when busy */ |
| 70 | u32 cfg_extra; /* extra CFG bits to set */ |
| 71 | }; |
| 72 | |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 73 | struct xlr_i2c_private { |
| 74 | struct i2c_adapter adap; |
| 75 | u32 __iomem *iobase; |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 76 | const struct xlr_i2c_config *cfg; |
| 77 | struct clk *clk; |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 78 | }; |
| 79 | |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 80 | static int xlr_i2c_busy(struct xlr_i2c_private *priv, u32 status) |
| 81 | { |
| 82 | return (status & XLR_I2C_BUS_BUSY) == priv->cfg->status_busy; |
| 83 | } |
| 84 | |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 85 | static int xlr_i2c_tx(struct xlr_i2c_private *priv, u16 len, |
| 86 | u8 *buf, u16 addr) |
| 87 | { |
| 88 | struct i2c_adapter *adap = &priv->adap; |
| 89 | unsigned long timeout, stoptime, checktime; |
| 90 | u32 i2c_status; |
| 91 | int pos, timedout; |
Måns Rullgård | a45af72 | 2015-12-15 23:15:05 +0000 | [diff] [blame^] | 92 | u8 offset; |
| 93 | u32 xfer; |
| 94 | |
| 95 | if (!len) |
| 96 | return -EOPNOTSUPP; |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 97 | |
| 98 | offset = buf[0]; |
| 99 | xlr_i2c_wreg(priv->iobase, XLR_I2C_ADDR, offset); |
| 100 | xlr_i2c_wreg(priv->iobase, XLR_I2C_DEVADDR, addr); |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 101 | xlr_i2c_wreg(priv->iobase, XLR_I2C_CFG, |
| 102 | XLR_I2C_CFG_ADDR | priv->cfg->cfg_extra); |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 103 | |
| 104 | timeout = msecs_to_jiffies(XLR_I2C_TIMEOUT); |
| 105 | stoptime = jiffies + timeout; |
| 106 | timedout = 0; |
Måns Rullgård | a45af72 | 2015-12-15 23:15:05 +0000 | [diff] [blame^] | 107 | |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 108 | if (len == 1) { |
Måns Rullgård | a45af72 | 2015-12-15 23:15:05 +0000 | [diff] [blame^] | 109 | xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len - 1); |
| 110 | xfer = XLR_I2C_STARTXFR_ND; |
| 111 | pos = 1; |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 112 | } else { |
Måns Rullgård | a45af72 | 2015-12-15 23:15:05 +0000 | [diff] [blame^] | 113 | xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len - 2); |
| 114 | xlr_i2c_wreg(priv->iobase, XLR_I2C_DATAOUT, buf[1]); |
| 115 | xfer = XLR_I2C_STARTXFR_WR; |
| 116 | pos = 2; |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 117 | } |
| 118 | |
Måns Rullgård | a45af72 | 2015-12-15 23:15:05 +0000 | [diff] [blame^] | 119 | retry: |
| 120 | /* retry can only happen on the first byte */ |
| 121 | xlr_i2c_wreg(priv->iobase, XLR_I2C_STARTXFR, xfer); |
| 122 | |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 123 | while (!timedout) { |
| 124 | checktime = jiffies; |
| 125 | i2c_status = xlr_i2c_rdreg(priv->iobase, XLR_I2C_STATUS); |
| 126 | |
Måns Rullgård | a45af72 | 2015-12-15 23:15:05 +0000 | [diff] [blame^] | 127 | if ((i2c_status & XLR_I2C_SDOEMPTY) && pos < len) { |
| 128 | xlr_i2c_wreg(priv->iobase, XLR_I2C_DATAOUT, buf[pos++]); |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 129 | |
| 130 | /* reset timeout on successful xmit */ |
| 131 | stoptime = jiffies + timeout; |
| 132 | } |
| 133 | timedout = time_after(checktime, stoptime); |
| 134 | |
| 135 | if (i2c_status & XLR_I2C_ARB_STARTERR) { |
| 136 | if (timedout) |
| 137 | break; |
| 138 | goto retry; |
| 139 | } |
| 140 | |
| 141 | if (i2c_status & XLR_I2C_ACK_ERR) |
| 142 | return -EIO; |
| 143 | |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 144 | if (!xlr_i2c_busy(priv, i2c_status) && pos >= len) |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | dev_err(&adap->dev, "I2C transmit timeout\n"); |
| 148 | return -ETIMEDOUT; |
| 149 | } |
| 150 | |
| 151 | static int xlr_i2c_rx(struct xlr_i2c_private *priv, u16 len, u8 *buf, u16 addr) |
| 152 | { |
| 153 | struct i2c_adapter *adap = &priv->adap; |
| 154 | u32 i2c_status; |
| 155 | unsigned long timeout, stoptime, checktime; |
| 156 | int nbytes, timedout; |
Måns Rullgård | a45af72 | 2015-12-15 23:15:05 +0000 | [diff] [blame^] | 157 | |
| 158 | if (!len) |
| 159 | return -EOPNOTSUPP; |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 160 | |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 161 | xlr_i2c_wreg(priv->iobase, XLR_I2C_CFG, |
| 162 | XLR_I2C_CFG_NOADDR | priv->cfg->cfg_extra); |
Måns Rullgård | a45af72 | 2015-12-15 23:15:05 +0000 | [diff] [blame^] | 163 | xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len - 1); |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 164 | xlr_i2c_wreg(priv->iobase, XLR_I2C_DEVADDR, addr); |
| 165 | |
| 166 | timeout = msecs_to_jiffies(XLR_I2C_TIMEOUT); |
| 167 | stoptime = jiffies + timeout; |
| 168 | timedout = 0; |
| 169 | nbytes = 0; |
| 170 | retry: |
| 171 | xlr_i2c_wreg(priv->iobase, XLR_I2C_STARTXFR, XLR_I2C_STARTXFR_RD); |
| 172 | |
| 173 | while (!timedout) { |
| 174 | checktime = jiffies; |
| 175 | i2c_status = xlr_i2c_rdreg(priv->iobase, XLR_I2C_STATUS); |
| 176 | if (i2c_status & XLR_I2C_RXRDY) { |
Måns Rullgård | a45af72 | 2015-12-15 23:15:05 +0000 | [diff] [blame^] | 177 | if (nbytes >= len) |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 178 | return -EIO; /* should not happen */ |
| 179 | |
Måns Rullgård | a45af72 | 2015-12-15 23:15:05 +0000 | [diff] [blame^] | 180 | buf[nbytes++] = |
| 181 | xlr_i2c_rdreg(priv->iobase, XLR_I2C_DATAIN); |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 182 | |
| 183 | /* reset timeout on successful read */ |
| 184 | stoptime = jiffies + timeout; |
| 185 | } |
| 186 | |
| 187 | timedout = time_after(checktime, stoptime); |
| 188 | if (i2c_status & XLR_I2C_ARB_STARTERR) { |
| 189 | if (timedout) |
| 190 | break; |
| 191 | goto retry; |
| 192 | } |
| 193 | |
| 194 | if (i2c_status & XLR_I2C_ACK_ERR) |
| 195 | return -EIO; |
| 196 | |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 197 | if (!xlr_i2c_busy(priv, i2c_status)) |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | dev_err(&adap->dev, "I2C receive timeout\n"); |
| 202 | return -ETIMEDOUT; |
| 203 | } |
| 204 | |
| 205 | static int xlr_i2c_xfer(struct i2c_adapter *adap, |
| 206 | struct i2c_msg *msgs, int num) |
| 207 | { |
| 208 | struct i2c_msg *msg; |
| 209 | int i; |
| 210 | int ret = 0; |
| 211 | struct xlr_i2c_private *priv = i2c_get_adapdata(adap); |
| 212 | |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 213 | ret = clk_enable(priv->clk); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 217 | for (i = 0; ret == 0 && i < num; i++) { |
| 218 | msg = &msgs[i]; |
| 219 | if (msg->flags & I2C_M_RD) |
| 220 | ret = xlr_i2c_rx(priv, msg->len, &msg->buf[0], |
| 221 | msg->addr); |
| 222 | else |
| 223 | ret = xlr_i2c_tx(priv, msg->len, &msg->buf[0], |
| 224 | msg->addr); |
| 225 | } |
| 226 | |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 227 | clk_disable(priv->clk); |
| 228 | |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 229 | return (ret != 0) ? ret : num; |
| 230 | } |
| 231 | |
| 232 | static u32 xlr_func(struct i2c_adapter *adap) |
| 233 | { |
| 234 | /* Emulate SMBUS over I2C */ |
Måns Rullgård | a45af72 | 2015-12-15 23:15:05 +0000 | [diff] [blame^] | 235 | return (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) | I2C_FUNC_I2C; |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static struct i2c_algorithm xlr_i2c_algo = { |
| 239 | .master_xfer = xlr_i2c_xfer, |
| 240 | .functionality = xlr_func, |
| 241 | }; |
| 242 | |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 243 | static const struct xlr_i2c_config xlr_i2c_config_default = { |
| 244 | .status_busy = XLR_I2C_BUS_BUSY, |
| 245 | .cfg_extra = 0, |
| 246 | }; |
| 247 | |
| 248 | static const struct xlr_i2c_config xlr_i2c_config_tangox = { |
| 249 | .status_busy = 0, |
| 250 | .cfg_extra = 1 << 8, |
| 251 | }; |
| 252 | |
| 253 | static const struct of_device_id xlr_i2c_dt_ids[] = { |
| 254 | { |
| 255 | .compatible = "sigma,smp8642-i2c", |
| 256 | .data = &xlr_i2c_config_tangox, |
| 257 | }, |
| 258 | { } |
| 259 | }; |
| 260 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 261 | static int xlr_i2c_probe(struct platform_device *pdev) |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 262 | { |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 263 | const struct of_device_id *match; |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 264 | struct xlr_i2c_private *priv; |
| 265 | struct resource *res; |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 266 | struct clk *clk; |
| 267 | unsigned long clk_rate; |
| 268 | unsigned long clk_div; |
| 269 | u32 busfreq; |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 270 | int ret; |
| 271 | |
| 272 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 273 | if (!priv) |
| 274 | return -ENOMEM; |
| 275 | |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 276 | match = of_match_device(xlr_i2c_dt_ids, &pdev->dev); |
| 277 | if (match) |
| 278 | priv->cfg = match->data; |
| 279 | else |
| 280 | priv->cfg = &xlr_i2c_config_default; |
| 281 | |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 282 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 84dbf80 | 2013-01-21 11:09:03 +0100 | [diff] [blame] | 283 | priv->iobase = devm_ioremap_resource(&pdev->dev, res); |
| 284 | if (IS_ERR(priv->iobase)) |
| 285 | return PTR_ERR(priv->iobase); |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 286 | |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 287 | if (of_property_read_u32(pdev->dev.of_node, "clock-frequency", |
| 288 | &busfreq)) |
| 289 | busfreq = 100000; |
| 290 | |
| 291 | clk = devm_clk_get(&pdev->dev, NULL); |
| 292 | if (!IS_ERR(clk)) { |
| 293 | ret = clk_prepare_enable(clk); |
| 294 | if (ret) |
| 295 | return ret; |
| 296 | |
| 297 | clk_rate = clk_get_rate(clk); |
| 298 | clk_div = DIV_ROUND_UP(clk_rate, 2 * busfreq); |
| 299 | xlr_i2c_wreg(priv->iobase, XLR_I2C_CLKDIV, clk_div); |
| 300 | |
| 301 | clk_disable(clk); |
| 302 | priv->clk = clk; |
| 303 | } |
| 304 | |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 305 | priv->adap.dev.parent = &pdev->dev; |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 306 | priv->adap.dev.of_node = pdev->dev.of_node; |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 307 | priv->adap.owner = THIS_MODULE; |
| 308 | priv->adap.algo_data = priv; |
| 309 | priv->adap.algo = &xlr_i2c_algo; |
| 310 | priv->adap.nr = pdev->id; |
| 311 | priv->adap.class = I2C_CLASS_HWMON; |
| 312 | snprintf(priv->adap.name, sizeof(priv->adap.name), "xlr-i2c"); |
| 313 | |
| 314 | i2c_set_adapdata(&priv->adap, priv); |
| 315 | ret = i2c_add_numbered_adapter(&priv->adap); |
| 316 | if (ret < 0) { |
| 317 | dev_err(&priv->adap.dev, "Failed to add i2c bus.\n"); |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | platform_set_drvdata(pdev, priv); |
| 322 | dev_info(&priv->adap.dev, "Added I2C Bus.\n"); |
| 323 | return 0; |
| 324 | } |
| 325 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 326 | static int xlr_i2c_remove(struct platform_device *pdev) |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 327 | { |
| 328 | struct xlr_i2c_private *priv; |
| 329 | |
| 330 | priv = platform_get_drvdata(pdev); |
| 331 | i2c_del_adapter(&priv->adap); |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 332 | clk_unprepare(priv->clk); |
| 333 | |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static struct platform_driver xlr_i2c_driver = { |
| 338 | .probe = xlr_i2c_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 339 | .remove = xlr_i2c_remove, |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 340 | .driver = { |
| 341 | .name = "xlr-i2cbus", |
Måns Rullgård | 75d31c2 | 2015-11-02 02:03:36 +0000 | [diff] [blame] | 342 | .of_match_table = xlr_i2c_dt_ids, |
Ganesan Ramalingam | 401c343 | 2012-01-27 14:15:37 +0530 | [diff] [blame] | 343 | }, |
| 344 | }; |
| 345 | |
| 346 | module_platform_driver(xlr_i2c_driver); |
| 347 | |
| 348 | MODULE_AUTHOR("Ganesan Ramalingam <ganesanr@netlogicmicro.com>"); |
| 349 | MODULE_DESCRIPTION("XLR/XLS SoC I2C Controller driver"); |
| 350 | MODULE_LICENSE("GPL v2"); |
| 351 | MODULE_ALIAS("platform:xlr-i2cbus"); |