|
| 1 | +/* |
| 2 | + * Synopsys DesignWare I2C adapter driver. |
| 3 | + * |
| 4 | + * Based on the TI DAVINCI I2C adapter driver. |
| 5 | + * |
| 6 | + * Copyright (C) 2006 Texas Instruments. |
| 7 | + * Copyright (C) 2007 MontaVista Software Inc. |
| 8 | + * Copyright (C) 2009 Provigent Ltd. |
| 9 | + * |
| 10 | + * ---------------------------------------------------------------------------- |
| 11 | + * |
| 12 | + * This program is free software; you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU General Public License as published by |
| 14 | + * the Free Software Foundation; either version 2 of the License, or |
| 15 | + * (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU General Public License for more details. |
| 21 | + * ---------------------------------------------------------------------------- |
| 22 | + * |
| 23 | + */ |
| 24 | +#include <linux/delay.h> |
| 25 | +#include <linux/export.h> |
| 26 | +#include <linux/errno.h> |
| 27 | +#include <linux/err.h> |
| 28 | +#include <linux/i2c.h> |
| 29 | +#include <linux/interrupt.h> |
| 30 | +#include <linux/io.h> |
| 31 | +#include <linux/module.h> |
| 32 | +#include <linux/pm_runtime.h> |
| 33 | + |
| 34 | +#include "i2c-designware-core.h" |
| 35 | + |
| 36 | +static char *abort_sources[] = { |
| 37 | + [ABRT_7B_ADDR_NOACK] = |
| 38 | + "slave address not acknowledged (7bit mode)", |
| 39 | + [ABRT_10ADDR1_NOACK] = |
| 40 | + "first address byte not acknowledged (10bit mode)", |
| 41 | + [ABRT_10ADDR2_NOACK] = |
| 42 | + "second address byte not acknowledged (10bit mode)", |
| 43 | + [ABRT_TXDATA_NOACK] = |
| 44 | + "data not acknowledged", |
| 45 | + [ABRT_GCALL_NOACK] = |
| 46 | + "no acknowledgement for a general call", |
| 47 | + [ABRT_GCALL_READ] = |
| 48 | + "read after general call", |
| 49 | + [ABRT_SBYTE_ACKDET] = |
| 50 | + "start byte acknowledged", |
| 51 | + [ABRT_SBYTE_NORSTRT] = |
| 52 | + "trying to send start byte when restart is disabled", |
| 53 | + [ABRT_10B_RD_NORSTRT] = |
| 54 | + "trying to read when restart is disabled (10bit mode)", |
| 55 | + [ABRT_MASTER_DIS] = |
| 56 | + "trying to use disabled adapter", |
| 57 | + [ARB_LOST] = |
| 58 | + "lost arbitration", |
| 59 | +}; |
| 60 | + |
| 61 | +u32 dw_readl(struct dw_i2c_dev *dev, int offset) |
| 62 | +{ |
| 63 | + u32 value; |
| 64 | + |
| 65 | + if (dev->flags & ACCESS_16BIT) |
| 66 | + value = readw_relaxed(dev->base + offset) | |
| 67 | + (readw_relaxed(dev->base + offset + 2) << 16); |
| 68 | + else |
| 69 | + value = readl_relaxed(dev->base + offset); |
| 70 | + |
| 71 | + if (dev->flags & ACCESS_SWAP) |
| 72 | + return swab32(value); |
| 73 | + else |
| 74 | + return value; |
| 75 | +} |
| 76 | + |
| 77 | +void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset) |
| 78 | +{ |
| 79 | + if (dev->flags & ACCESS_SWAP) |
| 80 | + b = swab32(b); |
| 81 | + |
| 82 | + if (dev->flags & ACCESS_16BIT) { |
| 83 | + writew_relaxed((u16)b, dev->base + offset); |
| 84 | + writew_relaxed((u16)(b >> 16), dev->base + offset + 2); |
| 85 | + } else { |
| 86 | + writel_relaxed(b, dev->base + offset); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset) |
| 91 | +{ |
| 92 | + /* |
| 93 | + * DesignWare I2C core doesn't seem to have solid strategy to meet |
| 94 | + * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec |
| 95 | + * will result in violation of the tHD;STA spec. |
| 96 | + */ |
| 97 | + if (cond) |
| 98 | + /* |
| 99 | + * Conditional expression: |
| 100 | + * |
| 101 | + * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH |
| 102 | + * |
| 103 | + * This is based on the DW manuals, and represents an ideal |
| 104 | + * configuration. The resulting I2C bus speed will be |
| 105 | + * faster than any of the others. |
| 106 | + * |
| 107 | + * If your hardware is free from tHD;STA issue, try this one. |
| 108 | + */ |
| 109 | + return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset; |
| 110 | + else |
| 111 | + /* |
| 112 | + * Conditional expression: |
| 113 | + * |
| 114 | + * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf) |
| 115 | + * |
| 116 | + * This is just experimental rule; the tHD;STA period turned |
| 117 | + * out to be proportinal to (_HCNT + 3). With this setting, |
| 118 | + * we could meet both tHIGH and tHD;STA timing specs. |
| 119 | + * |
| 120 | + * If unsure, you'd better to take this alternative. |
| 121 | + * |
| 122 | + * The reason why we need to take into account "tf" here, |
| 123 | + * is the same as described in i2c_dw_scl_lcnt(). |
| 124 | + */ |
| 125 | + return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000 |
| 126 | + - 3 + offset; |
| 127 | +} |
| 128 | + |
| 129 | +u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset) |
| 130 | +{ |
| 131 | + /* |
| 132 | + * Conditional expression: |
| 133 | + * |
| 134 | + * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf) |
| 135 | + * |
| 136 | + * DW I2C core starts counting the SCL CNTs for the LOW period |
| 137 | + * of the SCL clock (tLOW) as soon as it pulls the SCL line. |
| 138 | + * In order to meet the tLOW timing spec, we need to take into |
| 139 | + * account the fall time of SCL signal (tf). Default tf value |
| 140 | + * should be 0.3 us, for safety. |
| 141 | + */ |
| 142 | + return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset; |
| 143 | +} |
| 144 | + |
| 145 | +void __i2c_dw_enable(struct dw_i2c_dev *dev, bool enable) |
| 146 | +{ |
| 147 | + dw_writel(dev, enable, DW_IC_ENABLE); |
| 148 | +} |
| 149 | + |
| 150 | +void __i2c_dw_enable_and_wait(struct dw_i2c_dev *dev, bool enable) |
| 151 | +{ |
| 152 | + int timeout = 100; |
| 153 | + |
| 154 | + do { |
| 155 | + __i2c_dw_enable(dev, enable); |
| 156 | + if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == enable) |
| 157 | + return; |
| 158 | + |
| 159 | + /* |
| 160 | + * Wait 10 times the signaling period of the highest I2C |
| 161 | + * transfer supported by the driver (for 400KHz this is |
| 162 | + * 25us) as described in the DesignWare I2C databook. |
| 163 | + */ |
| 164 | + usleep_range(25, 250); |
| 165 | + } while (timeout--); |
| 166 | + |
| 167 | + dev_warn(dev->dev, "timeout in %sabling adapter\n", |
| 168 | + enable ? "en" : "dis"); |
| 169 | +} |
| 170 | + |
| 171 | +unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev) |
| 172 | +{ |
| 173 | + /* |
| 174 | + * Clock is not necessary if we got LCNT/HCNT values directly from |
| 175 | + * the platform code. |
| 176 | + */ |
| 177 | + if (WARN_ON_ONCE(!dev->get_clk_rate_khz)) |
| 178 | + return 0; |
| 179 | + return dev->get_clk_rate_khz(dev); |
| 180 | +} |
| 181 | + |
| 182 | +int i2c_dw_acquire_lock(struct dw_i2c_dev *dev) |
| 183 | +{ |
| 184 | + int ret; |
| 185 | + |
| 186 | + if (!dev->acquire_lock) |
| 187 | + return 0; |
| 188 | + |
| 189 | + ret = dev->acquire_lock(dev); |
| 190 | + if (!ret) |
| 191 | + return 0; |
| 192 | + |
| 193 | + dev_err(dev->dev, "couldn't acquire bus ownership\n"); |
| 194 | + |
| 195 | + return ret; |
| 196 | +} |
| 197 | + |
| 198 | +void i2c_dw_release_lock(struct dw_i2c_dev *dev) |
| 199 | +{ |
| 200 | + if (dev->release_lock) |
| 201 | + dev->release_lock(dev); |
| 202 | +} |
| 203 | + |
| 204 | +/* |
| 205 | + * Waiting for bus not busy |
| 206 | + */ |
| 207 | +int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev) |
| 208 | +{ |
| 209 | + int timeout = TIMEOUT; |
| 210 | + |
| 211 | + while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) { |
| 212 | + if (timeout <= 0) { |
| 213 | + dev_warn(dev->dev, "timeout waiting for bus ready\n"); |
| 214 | + return -ETIMEDOUT; |
| 215 | + } |
| 216 | + timeout--; |
| 217 | + usleep_range(1000, 1100); |
| 218 | + } |
| 219 | + |
| 220 | + return 0; |
| 221 | +} |
| 222 | + |
| 223 | +int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev) |
| 224 | +{ |
| 225 | + unsigned long abort_source = dev->abort_source; |
| 226 | + int i; |
| 227 | + |
| 228 | + if (abort_source & DW_IC_TX_ABRT_NOACK) { |
| 229 | + for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
| 230 | + dev_dbg(dev->dev, |
| 231 | + "%s: %s\n", __func__, abort_sources[i]); |
| 232 | + return -EREMOTEIO; |
| 233 | + } |
| 234 | + |
| 235 | + for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
| 236 | + dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]); |
| 237 | + |
| 238 | + if (abort_source & DW_IC_TX_ARB_LOST) |
| 239 | + return -EAGAIN; |
| 240 | + else if (abort_source & DW_IC_TX_ABRT_GCALL_READ) |
| 241 | + return -EINVAL; /* wrong msgs[] data */ |
| 242 | + else |
| 243 | + return -EIO; |
| 244 | +} |
| 245 | + |
| 246 | +u32 i2c_dw_func(struct i2c_adapter *adap) |
| 247 | +{ |
| 248 | + struct dw_i2c_dev *dev = i2c_get_adapdata(adap); |
| 249 | + |
| 250 | + return dev->functionality; |
| 251 | +} |
| 252 | + |
| 253 | +void i2c_dw_disable(struct dw_i2c_dev *dev) |
| 254 | +{ |
| 255 | + /* Disable controller */ |
| 256 | + __i2c_dw_enable_and_wait(dev, false); |
| 257 | + |
| 258 | + /* Disable all interupts */ |
| 259 | + dw_writel(dev, 0, DW_IC_INTR_MASK); |
| 260 | + dw_readl(dev, DW_IC_CLR_INTR); |
| 261 | +} |
| 262 | + |
| 263 | +void i2c_dw_disable_int(struct dw_i2c_dev *dev) |
| 264 | +{ |
| 265 | + dw_writel(dev, 0, DW_IC_INTR_MASK); |
| 266 | +} |
| 267 | + |
| 268 | +u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev) |
| 269 | +{ |
| 270 | + return dw_readl(dev, DW_IC_COMP_PARAM_1); |
| 271 | +} |
| 272 | +EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param); |
| 273 | + |
| 274 | +MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core"); |
| 275 | +MODULE_LICENSE("GPL"); |
0 commit comments