| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Synopsys DesignWare I2C adapter driver. |
| 4 | * |
| 5 | * Based on the TI DAVINCI I2C adapter driver. |
| 6 | * |
| 7 | * Copyright (C) 2006 Texas Instruments. |
| 8 | * Copyright (C) 2007 MontaVista Software Inc. |
| 9 | * Copyright (C) 2009 Provigent Ltd. |
| 10 | */ |
| 11 | |
| 12 | #define DEFAULT_SYMBOL_NAMESPACE "I2C_DW_COMMON" |
| 13 | |
| 14 | #include <linux/acpi.h> |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/export.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/io.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/of.h> |
| 27 | #include <linux/pm.h> |
| 28 | #include <linux/pm_runtime.h> |
| 29 | #include <linux/property.h> |
| 30 | #include <linux/regmap.h> |
| 31 | #include <linux/swab.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <linux/units.h> |
| 34 | |
| 35 | #include "i2c-designware-core.h" |
| 36 | |
| 37 | static const char *const abort_sources[] = { |
| 38 | [ABRT_7B_ADDR_NOACK] = |
| 39 | "slave address not acknowledged (7bit mode)" , |
| 40 | [ABRT_10ADDR1_NOACK] = |
| 41 | "first address byte not acknowledged (10bit mode)" , |
| 42 | [ABRT_10ADDR2_NOACK] = |
| 43 | "second address byte not acknowledged (10bit mode)" , |
| 44 | [ABRT_TXDATA_NOACK] = |
| 45 | "data not acknowledged" , |
| 46 | [ABRT_GCALL_NOACK] = |
| 47 | "no acknowledgement for a general call" , |
| 48 | [ABRT_GCALL_READ] = |
| 49 | "read after general call" , |
| 50 | [ABRT_SBYTE_ACKDET] = |
| 51 | "start byte acknowledged" , |
| 52 | [ABRT_SBYTE_NORSTRT] = |
| 53 | "trying to send start byte when restart is disabled" , |
| 54 | [ABRT_10B_RD_NORSTRT] = |
| 55 | "trying to read when restart is disabled (10bit mode)" , |
| 56 | [ABRT_MASTER_DIS] = |
| 57 | "trying to use disabled adapter" , |
| 58 | [ARB_LOST] = |
| 59 | "lost arbitration" , |
| 60 | [ABRT_SLAVE_FLUSH_TXFIFO] = |
| 61 | "read command so flush old data in the TX FIFO" , |
| 62 | [ABRT_SLAVE_ARBLOST] = |
| 63 | "slave lost the bus while transmitting data to a remote master" , |
| 64 | [ABRT_SLAVE_RD_INTX] = |
| 65 | "incorrect slave-transmitter mode configuration" , |
| 66 | }; |
| 67 | |
| 68 | static int dw_reg_read(void *context, unsigned int reg, unsigned int *val) |
| 69 | { |
| 70 | struct dw_i2c_dev *dev = context; |
| 71 | |
| 72 | *val = readl(addr: dev->base + reg); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int dw_reg_write(void *context, unsigned int reg, unsigned int val) |
| 78 | { |
| 79 | struct dw_i2c_dev *dev = context; |
| 80 | |
| 81 | writel(val, addr: dev->base + reg); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int dw_reg_read_swab(void *context, unsigned int reg, unsigned int *val) |
| 87 | { |
| 88 | struct dw_i2c_dev *dev = context; |
| 89 | |
| 90 | *val = swab32(readl(dev->base + reg)); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int dw_reg_write_swab(void *context, unsigned int reg, unsigned int val) |
| 96 | { |
| 97 | struct dw_i2c_dev *dev = context; |
| 98 | |
| 99 | writel(swab32(val), addr: dev->base + reg); |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int dw_reg_read_word(void *context, unsigned int reg, unsigned int *val) |
| 105 | { |
| 106 | struct dw_i2c_dev *dev = context; |
| 107 | |
| 108 | *val = readw(addr: dev->base + reg) | |
| 109 | (readw(addr: dev->base + reg + 2) << 16); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int dw_reg_write_word(void *context, unsigned int reg, unsigned int val) |
| 115 | { |
| 116 | struct dw_i2c_dev *dev = context; |
| 117 | |
| 118 | writew(val, addr: dev->base + reg); |
| 119 | writew(val: val >> 16, addr: dev->base + reg + 2); |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * i2c_dw_init_regmap() - Initialize registers map |
| 126 | * @dev: device private data |
| 127 | * |
| 128 | * Autodetects needed register access mode and creates the regmap with |
| 129 | * corresponding read/write callbacks. This must be called before doing any |
| 130 | * other register access. |
| 131 | * |
| 132 | * Return: 0 on success, or negative errno otherwise. |
| 133 | */ |
| 134 | int i2c_dw_init_regmap(struct dw_i2c_dev *dev) |
| 135 | { |
| 136 | struct regmap_config map_cfg = { |
| 137 | .reg_bits = 32, |
| 138 | .val_bits = 32, |
| 139 | .reg_stride = 4, |
| 140 | .disable_locking = true, |
| 141 | .reg_read = dw_reg_read, |
| 142 | .reg_write = dw_reg_write, |
| 143 | .max_register = DW_IC_COMP_TYPE, |
| 144 | }; |
| 145 | u32 reg; |
| 146 | int ret; |
| 147 | |
| 148 | /* |
| 149 | * Skip detecting the registers map configuration if the regmap has |
| 150 | * already been provided by a higher code. |
| 151 | */ |
| 152 | if (dev->map) |
| 153 | return 0; |
| 154 | |
| 155 | ret = i2c_dw_acquire_lock(dev); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | reg = readl(addr: dev->base + DW_IC_COMP_TYPE); |
| 160 | i2c_dw_release_lock(dev); |
| 161 | |
| 162 | if ((dev->flags & MODEL_MASK) == MODEL_AMD_NAVI_GPU) |
| 163 | map_cfg.max_register = AMD_UCSI_INTR_REG; |
| 164 | |
| 165 | if (reg == swab32(DW_IC_COMP_TYPE_VALUE)) { |
| 166 | map_cfg.reg_read = dw_reg_read_swab; |
| 167 | map_cfg.reg_write = dw_reg_write_swab; |
| 168 | } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) { |
| 169 | map_cfg.reg_read = dw_reg_read_word; |
| 170 | map_cfg.reg_write = dw_reg_write_word; |
| 171 | } else if (reg != DW_IC_COMP_TYPE_VALUE) { |
| 172 | dev_err(dev->dev, |
| 173 | "Unknown Synopsys component type: 0x%08x\n" , reg); |
| 174 | return -ENODEV; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Note we'll check the return value of the regmap IO accessors only |
| 179 | * at the probe stage. The rest of the code won't do this because |
| 180 | * basically we have MMIO-based regmap, so none of the read/write methods |
| 181 | * can fail. |
| 182 | */ |
| 183 | dev->map = devm_regmap_init(dev->dev, NULL, dev, &map_cfg); |
| 184 | if (IS_ERR(ptr: dev->map)) { |
| 185 | dev_err(dev->dev, "Failed to init the registers map\n" ); |
| 186 | return PTR_ERR(ptr: dev->map); |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static const u32 supported_speeds[] = { |
| 193 | I2C_MAX_HIGH_SPEED_MODE_FREQ, |
| 194 | I2C_MAX_FAST_MODE_PLUS_FREQ, |
| 195 | I2C_MAX_FAST_MODE_FREQ, |
| 196 | I2C_MAX_STANDARD_MODE_FREQ, |
| 197 | }; |
| 198 | |
| 199 | static int i2c_dw_validate_speed(struct dw_i2c_dev *dev) |
| 200 | { |
| 201 | struct i2c_timings *t = &dev->timings; |
| 202 | unsigned int i; |
| 203 | |
| 204 | /* |
| 205 | * Only standard mode at 100kHz, fast mode at 400kHz, |
| 206 | * fast mode plus at 1MHz and high speed mode at 3.4MHz are supported. |
| 207 | */ |
| 208 | for (i = 0; i < ARRAY_SIZE(supported_speeds); i++) { |
| 209 | if (t->bus_freq_hz == supported_speeds[i]) |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | dev_err(dev->dev, |
| 214 | "%d Hz is unsupported, only 100kHz, 400kHz, 1MHz and 3.4MHz are supported\n" , |
| 215 | t->bus_freq_hz); |
| 216 | |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | |
| 220 | #ifdef CONFIG_OF |
| 221 | |
| 222 | #include <linux/platform_device.h> |
| 223 | |
| 224 | #define MSCC_ICPU_CFG_TWI_DELAY 0x0 |
| 225 | #define MSCC_ICPU_CFG_TWI_DELAY_ENABLE BIT(0) |
| 226 | #define MSCC_ICPU_CFG_TWI_SPIKE_FILTER 0x4 |
| 227 | |
| 228 | static int mscc_twi_set_sda_hold_time(struct dw_i2c_dev *dev) |
| 229 | { |
| 230 | writel(val: (dev->sda_hold_time << 1) | MSCC_ICPU_CFG_TWI_DELAY_ENABLE, |
| 231 | addr: dev->ext + MSCC_ICPU_CFG_TWI_DELAY); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static void i2c_dw_of_configure(struct device *device) |
| 237 | { |
| 238 | struct platform_device *pdev = to_platform_device(device); |
| 239 | struct dw_i2c_dev *dev = dev_get_drvdata(dev: device); |
| 240 | |
| 241 | switch (dev->flags & MODEL_MASK) { |
| 242 | case MODEL_MSCC_OCELOT: |
| 243 | dev->ext = devm_platform_ioremap_resource(pdev, index: 1); |
| 244 | if (!IS_ERR(ptr: dev->ext)) |
| 245 | dev->set_sda_hold_time = mscc_twi_set_sda_hold_time; |
| 246 | break; |
| 247 | default: |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | #else /* CONFIG_OF */ |
| 253 | |
| 254 | static inline void i2c_dw_of_configure(struct device *device) { } |
| 255 | |
| 256 | #endif /* CONFIG_OF */ |
| 257 | |
| 258 | #ifdef CONFIG_ACPI |
| 259 | |
| 260 | #include <linux/dmi.h> |
| 261 | |
| 262 | /* |
| 263 | * The HCNT/LCNT information coming from ACPI should be the most accurate |
| 264 | * for given platform. However, some systems get it wrong. On such systems |
| 265 | * we get better results by calculating those based on the input clock. |
| 266 | */ |
| 267 | static const struct dmi_system_id i2c_dw_no_acpi_params[] = { |
| 268 | { |
| 269 | .ident = "Dell Inspiron 7348" , |
| 270 | .matches = { |
| 271 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc." ), |
| 272 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7348" ), |
| 273 | }, |
| 274 | }, |
| 275 | {} |
| 276 | }; |
| 277 | |
| 278 | static void i2c_dw_acpi_params(struct device *device, char method[], |
| 279 | u16 *hcnt, u16 *lcnt, u32 *sda_hold) |
| 280 | { |
| 281 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; |
| 282 | acpi_handle handle = ACPI_HANDLE(device); |
| 283 | union acpi_object *obj; |
| 284 | |
| 285 | if (dmi_check_system(list: i2c_dw_no_acpi_params)) |
| 286 | return; |
| 287 | |
| 288 | if (ACPI_FAILURE(acpi_evaluate_object(handle, method, NULL, &buf))) |
| 289 | return; |
| 290 | |
| 291 | obj = (union acpi_object *)buf.pointer; |
| 292 | if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 3) { |
| 293 | const union acpi_object *objs = obj->package.elements; |
| 294 | |
| 295 | *hcnt = (u16)objs[0].integer.value; |
| 296 | *lcnt = (u16)objs[1].integer.value; |
| 297 | *sda_hold = (u32)objs[2].integer.value; |
| 298 | } |
| 299 | |
| 300 | kfree(objp: buf.pointer); |
| 301 | } |
| 302 | |
| 303 | static void i2c_dw_acpi_configure(struct device *device) |
| 304 | { |
| 305 | struct dw_i2c_dev *dev = dev_get_drvdata(dev: device); |
| 306 | struct i2c_timings *t = &dev->timings; |
| 307 | u32 ss_ht = 0, fp_ht = 0, hs_ht = 0, fs_ht = 0; |
| 308 | |
| 309 | /* |
| 310 | * Try to get SDA hold time and *CNT values from an ACPI method for |
| 311 | * selected speed modes. |
| 312 | */ |
| 313 | i2c_dw_acpi_params(device, method: "SSCN" , hcnt: &dev->ss_hcnt, lcnt: &dev->ss_lcnt, sda_hold: &ss_ht); |
| 314 | i2c_dw_acpi_params(device, method: "FMCN" , hcnt: &dev->fs_hcnt, lcnt: &dev->fs_lcnt, sda_hold: &fs_ht); |
| 315 | i2c_dw_acpi_params(device, method: "FPCN" , hcnt: &dev->fp_hcnt, lcnt: &dev->fp_lcnt, sda_hold: &fp_ht); |
| 316 | i2c_dw_acpi_params(device, method: "HSCN" , hcnt: &dev->hs_hcnt, lcnt: &dev->hs_lcnt, sda_hold: &hs_ht); |
| 317 | |
| 318 | switch (t->bus_freq_hz) { |
| 319 | case I2C_MAX_STANDARD_MODE_FREQ: |
| 320 | dev->sda_hold_time = ss_ht; |
| 321 | break; |
| 322 | case I2C_MAX_FAST_MODE_PLUS_FREQ: |
| 323 | dev->sda_hold_time = fp_ht; |
| 324 | break; |
| 325 | case I2C_MAX_HIGH_SPEED_MODE_FREQ: |
| 326 | dev->sda_hold_time = hs_ht; |
| 327 | break; |
| 328 | case I2C_MAX_FAST_MODE_FREQ: |
| 329 | default: |
| 330 | dev->sda_hold_time = fs_ht; |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | static u32 i2c_dw_acpi_round_bus_speed(struct device *device) |
| 336 | { |
| 337 | u32 acpi_speed; |
| 338 | int i; |
| 339 | |
| 340 | acpi_speed = i2c_acpi_find_bus_speed(dev: device); |
| 341 | /* |
| 342 | * Some DSDTs use a non standard speed, round down to the lowest |
| 343 | * standard speed. |
| 344 | */ |
| 345 | for (i = 0; i < ARRAY_SIZE(supported_speeds); i++) { |
| 346 | if (acpi_speed >= supported_speeds[i]) |
| 347 | return supported_speeds[i]; |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | #else /* CONFIG_ACPI */ |
| 354 | |
| 355 | static inline void i2c_dw_acpi_configure(struct device *device) { } |
| 356 | |
| 357 | static inline u32 i2c_dw_acpi_round_bus_speed(struct device *device) { return 0; } |
| 358 | |
| 359 | #endif /* CONFIG_ACPI */ |
| 360 | |
| 361 | static void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev) |
| 362 | { |
| 363 | u32 acpi_speed = i2c_dw_acpi_round_bus_speed(device: dev->dev); |
| 364 | struct i2c_timings *t = &dev->timings; |
| 365 | |
| 366 | /* |
| 367 | * Find bus speed from the "clock-frequency" device property, ACPI |
| 368 | * or by using fast mode if neither is set. |
| 369 | */ |
| 370 | if (acpi_speed && t->bus_freq_hz) |
| 371 | t->bus_freq_hz = min(t->bus_freq_hz, acpi_speed); |
| 372 | else if (acpi_speed || t->bus_freq_hz) |
| 373 | t->bus_freq_hz = max(t->bus_freq_hz, acpi_speed); |
| 374 | else |
| 375 | t->bus_freq_hz = I2C_MAX_FAST_MODE_FREQ; |
| 376 | } |
| 377 | |
| 378 | int i2c_dw_fw_parse_and_configure(struct dw_i2c_dev *dev) |
| 379 | { |
| 380 | struct i2c_timings *t = &dev->timings; |
| 381 | struct device *device = dev->dev; |
| 382 | struct fwnode_handle *fwnode = dev_fwnode(device); |
| 383 | |
| 384 | i2c_parse_fw_timings(dev: device, t, use_defaults: false); |
| 385 | |
| 386 | if (device_property_read_u32(dev: device, propname: "snps,bus-capacitance-pf" , val: &dev->bus_capacitance_pF)) |
| 387 | dev->bus_capacitance_pF = 100; |
| 388 | |
| 389 | dev->clk_freq_optimized = device_property_read_bool(dev: device, propname: "snps,clk-freq-optimized" ); |
| 390 | |
| 391 | i2c_dw_adjust_bus_speed(dev); |
| 392 | |
| 393 | if (is_of_node(fwnode)) |
| 394 | i2c_dw_of_configure(device); |
| 395 | else if (is_acpi_node(fwnode)) |
| 396 | i2c_dw_acpi_configure(device); |
| 397 | |
| 398 | return i2c_dw_validate_speed(dev); |
| 399 | } |
| 400 | EXPORT_SYMBOL_GPL(i2c_dw_fw_parse_and_configure); |
| 401 | |
| 402 | static u32 i2c_dw_read_scl_reg(struct dw_i2c_dev *dev, u32 reg) |
| 403 | { |
| 404 | u32 val; |
| 405 | int ret; |
| 406 | |
| 407 | ret = i2c_dw_acquire_lock(dev); |
| 408 | if (ret) |
| 409 | return 0; |
| 410 | |
| 411 | ret = regmap_read(map: dev->map, reg, val: &val); |
| 412 | i2c_dw_release_lock(dev); |
| 413 | |
| 414 | return ret ? 0 : val; |
| 415 | } |
| 416 | |
| 417 | u32 i2c_dw_scl_hcnt(struct dw_i2c_dev *dev, unsigned int reg, u32 ic_clk, |
| 418 | u32 tSYMBOL, u32 tf, int offset) |
| 419 | { |
| 420 | if (!ic_clk) |
| 421 | return i2c_dw_read_scl_reg(dev, reg); |
| 422 | |
| 423 | /* |
| 424 | * Conditional expression: |
| 425 | * |
| 426 | * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf) |
| 427 | * |
| 428 | * This is just experimental rule; the tHD;STA period turned |
| 429 | * out to be proportinal to (_HCNT + 3). With this setting, |
| 430 | * we could meet both tHIGH and tHD;STA timing specs. |
| 431 | * |
| 432 | * If unsure, you'd better to take this alternative. |
| 433 | * |
| 434 | * The reason why we need to take into account "tf" here, |
| 435 | * is the same as described in i2c_dw_scl_lcnt(). |
| 436 | */ |
| 437 | return DIV_ROUND_CLOSEST_ULL((u64)ic_clk * (tSYMBOL + tf), MICRO) - 3 + offset; |
| 438 | } |
| 439 | |
| 440 | u32 i2c_dw_scl_lcnt(struct dw_i2c_dev *dev, unsigned int reg, u32 ic_clk, |
| 441 | u32 tLOW, u32 tf, int offset) |
| 442 | { |
| 443 | if (!ic_clk) |
| 444 | return i2c_dw_read_scl_reg(dev, reg); |
| 445 | |
| 446 | /* |
| 447 | * Conditional expression: |
| 448 | * |
| 449 | * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf) |
| 450 | * |
| 451 | * DW I2C core starts counting the SCL CNTs for the LOW period |
| 452 | * of the SCL clock (tLOW) as soon as it pulls the SCL line. |
| 453 | * In order to meet the tLOW timing spec, we need to take into |
| 454 | * account the fall time of SCL signal (tf). Default tf value |
| 455 | * should be 0.3 us, for safety. |
| 456 | */ |
| 457 | return DIV_ROUND_CLOSEST_ULL((u64)ic_clk * (tLOW + tf), MICRO) - 1 + offset; |
| 458 | } |
| 459 | |
| 460 | int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev) |
| 461 | { |
| 462 | unsigned int reg; |
| 463 | int ret; |
| 464 | |
| 465 | ret = i2c_dw_acquire_lock(dev); |
| 466 | if (ret) |
| 467 | return ret; |
| 468 | |
| 469 | /* Configure SDA Hold Time if required */ |
| 470 | ret = regmap_read(map: dev->map, DW_IC_COMP_VERSION, val: ®); |
| 471 | if (ret) |
| 472 | goto err_release_lock; |
| 473 | |
| 474 | if (reg >= DW_IC_SDA_HOLD_MIN_VERS) { |
| 475 | if (!dev->sda_hold_time) { |
| 476 | /* Keep previous hold time setting if no one set it */ |
| 477 | ret = regmap_read(map: dev->map, DW_IC_SDA_HOLD, |
| 478 | val: &dev->sda_hold_time); |
| 479 | if (ret) |
| 480 | goto err_release_lock; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Workaround for avoiding TX arbitration lost in case I2C |
| 485 | * slave pulls SDA down "too quickly" after falling edge of |
| 486 | * SCL by enabling non-zero SDA RX hold. Specification says it |
| 487 | * extends incoming SDA low to high transition while SCL is |
| 488 | * high but it appears to help also above issue. |
| 489 | */ |
| 490 | if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK)) |
| 491 | dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT; |
| 492 | |
| 493 | dev_dbg(dev->dev, "SDA Hold Time TX:RX = %d:%d\n" , |
| 494 | dev->sda_hold_time & ~(u32)DW_IC_SDA_HOLD_RX_MASK, |
| 495 | dev->sda_hold_time >> DW_IC_SDA_HOLD_RX_SHIFT); |
| 496 | } else if (dev->set_sda_hold_time) { |
| 497 | dev->set_sda_hold_time(dev); |
| 498 | } else if (dev->sda_hold_time) { |
| 499 | dev_warn(dev->dev, |
| 500 | "Hardware too old to adjust SDA hold time.\n" ); |
| 501 | dev->sda_hold_time = 0; |
| 502 | } |
| 503 | |
| 504 | err_release_lock: |
| 505 | i2c_dw_release_lock(dev); |
| 506 | |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | void __i2c_dw_disable(struct dw_i2c_dev *dev) |
| 511 | { |
| 512 | struct i2c_timings *t = &dev->timings; |
| 513 | unsigned int raw_intr_stats, ic_stats; |
| 514 | unsigned int enable; |
| 515 | int timeout = 100; |
| 516 | bool abort_needed; |
| 517 | unsigned int status; |
| 518 | int ret; |
| 519 | |
| 520 | regmap_read(map: dev->map, DW_IC_RAW_INTR_STAT, val: &raw_intr_stats); |
| 521 | regmap_read(map: dev->map, DW_IC_STATUS, val: &ic_stats); |
| 522 | regmap_read(map: dev->map, DW_IC_ENABLE, val: &enable); |
| 523 | |
| 524 | abort_needed = (raw_intr_stats & DW_IC_INTR_MST_ON_HOLD) || |
| 525 | (ic_stats & DW_IC_STATUS_MASTER_HOLD_TX_FIFO_EMPTY); |
| 526 | if (abort_needed) { |
| 527 | if (!(enable & DW_IC_ENABLE_ENABLE)) { |
| 528 | regmap_write(map: dev->map, DW_IC_ENABLE, DW_IC_ENABLE_ENABLE); |
| 529 | /* |
| 530 | * Wait 10 times the signaling period of the highest I2C |
| 531 | * transfer supported by the driver (for 400KHz this is |
| 532 | * 25us) to ensure the I2C ENABLE bit is already set |
| 533 | * as described in the DesignWare I2C databook. |
| 534 | */ |
| 535 | fsleep(DIV_ROUND_CLOSEST_ULL(10 * MICRO, t->bus_freq_hz)); |
| 536 | /* Set ENABLE bit before setting ABORT */ |
| 537 | enable |= DW_IC_ENABLE_ENABLE; |
| 538 | } |
| 539 | |
| 540 | regmap_write(map: dev->map, DW_IC_ENABLE, val: enable | DW_IC_ENABLE_ABORT); |
| 541 | ret = regmap_read_poll_timeout(dev->map, DW_IC_ENABLE, enable, |
| 542 | !(enable & DW_IC_ENABLE_ABORT), 10, |
| 543 | 100); |
| 544 | if (ret) |
| 545 | dev_err(dev->dev, "timeout while trying to abort current transfer\n" ); |
| 546 | } |
| 547 | |
| 548 | do { |
| 549 | __i2c_dw_disable_nowait(dev); |
| 550 | /* |
| 551 | * The enable status register may be unimplemented, but |
| 552 | * in that case this test reads zero and exits the loop. |
| 553 | */ |
| 554 | regmap_read(map: dev->map, DW_IC_ENABLE_STATUS, val: &status); |
| 555 | if ((status & 1) == 0) |
| 556 | return; |
| 557 | |
| 558 | /* |
| 559 | * Wait 10 times the signaling period of the highest I2C |
| 560 | * transfer supported by the driver (for 400kHz this is |
| 561 | * 25us) as described in the DesignWare I2C databook. |
| 562 | */ |
| 563 | usleep_range(min: 25, max: 250); |
| 564 | } while (timeout--); |
| 565 | |
| 566 | dev_warn(dev->dev, "timeout in disabling adapter\n" ); |
| 567 | } |
| 568 | |
| 569 | u32 i2c_dw_clk_rate(struct dw_i2c_dev *dev) |
| 570 | { |
| 571 | /* |
| 572 | * Clock is not necessary if we got LCNT/HCNT values directly from |
| 573 | * the platform code. |
| 574 | */ |
| 575 | if (!dev->get_clk_rate_khz) { |
| 576 | dev_dbg_once(dev->dev, "Callback get_clk_rate_khz() is not defined\n" ); |
| 577 | return 0; |
| 578 | } |
| 579 | return dev->get_clk_rate_khz(dev); |
| 580 | } |
| 581 | |
| 582 | int i2c_dw_prepare_clk(struct dw_i2c_dev *dev, bool prepare) |
| 583 | { |
| 584 | int ret; |
| 585 | |
| 586 | if (prepare) { |
| 587 | /* Optional interface clock */ |
| 588 | ret = clk_prepare_enable(clk: dev->pclk); |
| 589 | if (ret) |
| 590 | return ret; |
| 591 | |
| 592 | ret = clk_prepare_enable(clk: dev->clk); |
| 593 | if (ret) |
| 594 | clk_disable_unprepare(clk: dev->pclk); |
| 595 | |
| 596 | return ret; |
| 597 | } |
| 598 | |
| 599 | clk_disable_unprepare(clk: dev->clk); |
| 600 | clk_disable_unprepare(clk: dev->pclk); |
| 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | EXPORT_SYMBOL_GPL(i2c_dw_prepare_clk); |
| 605 | |
| 606 | int i2c_dw_acquire_lock(struct dw_i2c_dev *dev) |
| 607 | { |
| 608 | int ret; |
| 609 | |
| 610 | if (!dev->acquire_lock) |
| 611 | return 0; |
| 612 | |
| 613 | ret = dev->acquire_lock(); |
| 614 | if (!ret) |
| 615 | return 0; |
| 616 | |
| 617 | dev_err(dev->dev, "couldn't acquire bus ownership\n" ); |
| 618 | |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | void i2c_dw_release_lock(struct dw_i2c_dev *dev) |
| 623 | { |
| 624 | if (dev->release_lock) |
| 625 | dev->release_lock(); |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * Waiting for bus not busy |
| 630 | */ |
| 631 | int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev) |
| 632 | { |
| 633 | unsigned int status; |
| 634 | int ret; |
| 635 | |
| 636 | ret = regmap_read_poll_timeout(dev->map, DW_IC_STATUS, status, |
| 637 | !(status & DW_IC_STATUS_ACTIVITY), |
| 638 | 1100, 20000); |
| 639 | if (ret) { |
| 640 | dev_warn(dev->dev, "timeout waiting for bus ready\n" ); |
| 641 | |
| 642 | i2c_recover_bus(adap: &dev->adapter); |
| 643 | |
| 644 | regmap_read(map: dev->map, DW_IC_STATUS, val: &status); |
| 645 | if (!(status & DW_IC_STATUS_ACTIVITY)) |
| 646 | ret = 0; |
| 647 | } |
| 648 | |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev) |
| 653 | { |
| 654 | unsigned long abort_source = dev->abort_source; |
| 655 | int i; |
| 656 | |
| 657 | if (abort_source & DW_IC_TX_ABRT_NOACK) { |
| 658 | for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
| 659 | dev_dbg(dev->dev, |
| 660 | "%s: %s\n" , __func__, abort_sources[i]); |
| 661 | return -EREMOTEIO; |
| 662 | } |
| 663 | |
| 664 | for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
| 665 | dev_err(dev->dev, "%s: %s\n" , __func__, abort_sources[i]); |
| 666 | |
| 667 | if (abort_source & DW_IC_TX_ARB_LOST) |
| 668 | return -EAGAIN; |
| 669 | if (abort_source & DW_IC_TX_ABRT_GCALL_READ) |
| 670 | return -EINVAL; /* wrong msgs[] data */ |
| 671 | |
| 672 | return -EIO; |
| 673 | } |
| 674 | |
| 675 | int i2c_dw_set_fifo_size(struct dw_i2c_dev *dev) |
| 676 | { |
| 677 | u32 tx_fifo_depth, rx_fifo_depth; |
| 678 | unsigned int param; |
| 679 | int ret; |
| 680 | |
| 681 | /* DW_IC_COMP_PARAM_1 not implement for IP issue */ |
| 682 | if ((dev->flags & MODEL_MASK) == MODEL_WANGXUN_SP) { |
| 683 | dev->tx_fifo_depth = TXGBE_TX_FIFO_DEPTH; |
| 684 | dev->rx_fifo_depth = TXGBE_RX_FIFO_DEPTH; |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Try to detect the FIFO depth if not set by interface driver, |
| 691 | * the depth could be from 2 to 256 from HW spec. |
| 692 | */ |
| 693 | ret = i2c_dw_acquire_lock(dev); |
| 694 | if (ret) |
| 695 | return ret; |
| 696 | |
| 697 | ret = regmap_read(map: dev->map, DW_IC_COMP_PARAM_1, val: ¶m); |
| 698 | i2c_dw_release_lock(dev); |
| 699 | if (ret) |
| 700 | return ret; |
| 701 | |
| 702 | tx_fifo_depth = ((param >> 16) & 0xff) + 1; |
| 703 | rx_fifo_depth = ((param >> 8) & 0xff) + 1; |
| 704 | if (!dev->tx_fifo_depth) { |
| 705 | dev->tx_fifo_depth = tx_fifo_depth; |
| 706 | dev->rx_fifo_depth = rx_fifo_depth; |
| 707 | } else if (tx_fifo_depth >= 2) { |
| 708 | dev->tx_fifo_depth = min_t(u32, dev->tx_fifo_depth, |
| 709 | tx_fifo_depth); |
| 710 | dev->rx_fifo_depth = min_t(u32, dev->rx_fifo_depth, |
| 711 | rx_fifo_depth); |
| 712 | } |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | u32 i2c_dw_func(struct i2c_adapter *adap) |
| 718 | { |
| 719 | struct dw_i2c_dev *dev = i2c_get_adapdata(adap); |
| 720 | |
| 721 | return dev->functionality; |
| 722 | } |
| 723 | |
| 724 | void i2c_dw_disable(struct dw_i2c_dev *dev) |
| 725 | { |
| 726 | unsigned int dummy; |
| 727 | int ret; |
| 728 | |
| 729 | ret = i2c_dw_acquire_lock(dev); |
| 730 | if (ret) |
| 731 | return; |
| 732 | |
| 733 | /* Disable controller */ |
| 734 | __i2c_dw_disable(dev); |
| 735 | |
| 736 | /* Disable all interrupts */ |
| 737 | __i2c_dw_write_intr_mask(dev, intr_mask: 0); |
| 738 | regmap_read(map: dev->map, DW_IC_CLR_INTR, val: &dummy); |
| 739 | |
| 740 | i2c_dw_release_lock(dev); |
| 741 | } |
| 742 | EXPORT_SYMBOL_GPL(i2c_dw_disable); |
| 743 | |
| 744 | int i2c_dw_probe(struct dw_i2c_dev *dev) |
| 745 | { |
| 746 | device_set_node(dev: &dev->adapter.dev, dev_fwnode(dev->dev)); |
| 747 | |
| 748 | switch (dev->mode) { |
| 749 | case DW_IC_SLAVE: |
| 750 | return i2c_dw_probe_slave(dev); |
| 751 | case DW_IC_MASTER: |
| 752 | return i2c_dw_probe_master(dev); |
| 753 | default: |
| 754 | dev_err(dev->dev, "Wrong operation mode: %d\n" , dev->mode); |
| 755 | return -EINVAL; |
| 756 | } |
| 757 | } |
| 758 | EXPORT_SYMBOL_GPL(i2c_dw_probe); |
| 759 | |
| 760 | static int i2c_dw_prepare(struct device *device) |
| 761 | { |
| 762 | /* |
| 763 | * If the ACPI companion device object is present for this device, |
| 764 | * it may be accessed during suspend and resume of other devices via |
| 765 | * I2C operation regions, so tell the PM core and middle layers to |
| 766 | * avoid skipping system suspend/resume callbacks for it in that case. |
| 767 | */ |
| 768 | return !has_acpi_companion(dev: device); |
| 769 | } |
| 770 | |
| 771 | static int i2c_dw_runtime_suspend(struct device *device) |
| 772 | { |
| 773 | struct dw_i2c_dev *dev = dev_get_drvdata(dev: device); |
| 774 | |
| 775 | if (dev->shared_with_punit) |
| 776 | return 0; |
| 777 | |
| 778 | i2c_dw_disable(dev); |
| 779 | i2c_dw_prepare_clk(dev, false); |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | static int i2c_dw_suspend(struct device *device) |
| 785 | { |
| 786 | struct dw_i2c_dev *dev = dev_get_drvdata(dev: device); |
| 787 | |
| 788 | i2c_mark_adapter_suspended(adap: &dev->adapter); |
| 789 | |
| 790 | return i2c_dw_runtime_suspend(device); |
| 791 | } |
| 792 | |
| 793 | static int i2c_dw_runtime_resume(struct device *device) |
| 794 | { |
| 795 | struct dw_i2c_dev *dev = dev_get_drvdata(dev: device); |
| 796 | |
| 797 | if (!dev->shared_with_punit) |
| 798 | i2c_dw_prepare_clk(dev, true); |
| 799 | |
| 800 | dev->init(dev); |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static int i2c_dw_resume(struct device *device) |
| 806 | { |
| 807 | struct dw_i2c_dev *dev = dev_get_drvdata(dev: device); |
| 808 | |
| 809 | i2c_dw_runtime_resume(device); |
| 810 | i2c_mark_adapter_resumed(adap: &dev->adapter); |
| 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | EXPORT_GPL_DEV_PM_OPS(i2c_dw_dev_pm_ops) = { |
| 816 | .prepare = pm_sleep_ptr(i2c_dw_prepare), |
| 817 | LATE_SYSTEM_SLEEP_PM_OPS(i2c_dw_suspend, i2c_dw_resume) |
| 818 | RUNTIME_PM_OPS(i2c_dw_runtime_suspend, i2c_dw_runtime_resume, NULL) |
| 819 | }; |
| 820 | |
| 821 | MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core" ); |
| 822 | MODULE_LICENSE("GPL" ); |
| 823 | |