Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'i2c/for-4.17' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux

Pull i2c updates from Wolfram Sang:

-I2C core now reports proper OF style module alias. I'd like to repeat
the note from the commit msg here (Thanks, Javier!):

NOTE: This patch may break out-of-tree drivers that were relying
on this behavior, and only had an I2C device ID table even
when the device was registered via OF.

There are no remaining drivers in mainline that do this, but
out-of-tree drivers have to be fixed and define a proper OF
device ID table to have module auto-loading working.

- new driver for the SynQuacer I2C controller

- major refactoring of the QUP driver

- the piix4 driver now uses request_muxed_region which should fix a
long standing resource conflict with the sp5100_tco watchdog

- a bunch of small core & driver improvements

* 'i2c/for-4.17' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux: (53 commits)
i2c: add support for Socionext SynQuacer I2C controller
dt-bindings: i2c: add binding for Socionext SynQuacer I2C
i2c: Update i2c_trace_msg static key to modern api
i2c: fix parameter of trace_i2c_result
i2c: imx: avoid taking clk_prepare mutex in PM callbacks
i2c: imx: use clk notifier for rate changes
i2c: make i2c_check_addr_validity() static
i2c: rcar: fix mask value of prohibited bit
dt-bindings: i2c: document R8A77965 bindings
i2c: pca-platform: drop gpio from platform data
i2c: pca-platform: use device_property_read_u32
i2c: pca-platform: unconditionally use devm_gpiod_get_optional
sh: sh7785lcr: add GPIO lookup table for i2c controller reset
i2c: qup: reorganization of driver code to remove polling for qup v2
i2c: qup: reorganization of driver code to remove polling for qup v1
i2c: qup: send NACK for last read sub transfers
i2c: qup: fix buffer overflow for multiple msg of maximum xfer len
i2c: qup: change completion timeout according to transfer length
i2c: qup: use the complete transfer length to choose DMA mode
i2c: qup: proper error handling for i2c error in BAM mode
...

+1977 -778
+4
Documentation/devicetree/bindings/eeprom/at24.txt
··· 41 41 "nxp", 42 42 "ramtron", 43 43 "renesas", 44 + "rohm", 44 45 "st", 45 46 46 47 Some vendors use different model names for chips which are just 47 48 variants of the above. Known such exceptions are listed below: 48 49 50 + "nxp,se97b" - the fallback is "atmel,24c02", 49 51 "renesas,r1ex24002" - the fallback is "atmel,24c02" 52 + "renesas,r1ex24128" - the fallback is "atmel,24c128" 53 + "rohm,br24t01" - the fallback is "atmel,24c01" 50 54 51 55 - reg: The I2C address of the EEPROM. 52 56
+2
Documentation/devicetree/bindings/i2c/i2c-rcar.txt
··· 13 13 "renesas,i2c-r8a7794" if the device is a part of a R8A7794 SoC. 14 14 "renesas,i2c-r8a7795" if the device is a part of a R8A7795 SoC. 15 15 "renesas,i2c-r8a7796" if the device is a part of a R8A7796 SoC. 16 + "renesas,i2c-r8a77965" if the device is a part of a R8A77965 SoC. 16 17 "renesas,i2c-r8a77970" if the device is a part of a R8A77970 SoC. 18 + "renesas,i2c-r8a77995" if the device is a part of a R8A77995 SoC. 17 19 "renesas,rcar-gen1-i2c" for a generic R-Car Gen1 compatible device. 18 20 "renesas,rcar-gen2-i2c" for a generic R-Car Gen2 or RZ/G1 compatible 19 21 device.
+1
Documentation/devicetree/bindings/i2c/i2c-sh_mobile.txt
··· 13 13 - "renesas,iic-r8a7794" (R-Car E2) 14 14 - "renesas,iic-r8a7795" (R-Car H3) 15 15 - "renesas,iic-r8a7796" (R-Car M3-W) 16 + - "renesas,iic-r8a77965" (R-Car M3-N) 16 17 - "renesas,iic-sh73a0" (SH-Mobile AG5) 17 18 - "renesas,rcar-gen2-iic" (generic R-Car Gen2 or RZ/G1 18 19 compatible device)
+29
Documentation/devicetree/bindings/i2c/i2c-synquacer.txt
··· 1 + Socionext SynQuacer I2C 2 + 3 + Required properties: 4 + - compatible : Must be "socionext,synquacer-i2c" 5 + - reg : Offset and length of the register set for the device 6 + - interrupts : A single interrupt specifier 7 + - #address-cells : Must be <1>; 8 + - #size-cells : Must be <0>; 9 + - clock-names : Must contain "pclk". 10 + - clocks : Must contain an entry for each name in clock-names. 11 + (See the common clock bindings.) 12 + 13 + Optional properties: 14 + - clock-frequency : Desired I2C bus clock frequency in Hz. As only Normal and 15 + Fast modes are supported, possible values are 100000 and 16 + 400000. 17 + 18 + Example : 19 + 20 + i2c@51210000 { 21 + compatible = "socionext,synquacer-i2c"; 22 + reg = <0x51210000 0x1000>; 23 + interrupts = <GIC_SPI 165 IRQ_TYPE_LEVEL_HIGH>; 24 + #address-cells = <1>; 25 + #size-cells = <0>; 26 + clock-names = "pclk"; 27 + clocks = <&clk_i2c>; 28 + clock-frequency = <400000>; 29 + };
+8 -1
MAINTAINERS
··· 6559 6559 F: include/linux/i2c-mux.h 6560 6560 6561 6561 I2C MV64XXX MARVELL AND ALLWINNER DRIVER 6562 - M: Gregory CLEMENT <gregory.clement@free-electrons.com> 6562 + M: Gregory CLEMENT <gregory.clement@bootlin.com> 6563 6563 L: linux-i2c@vger.kernel.org 6564 6564 S: Maintained 6565 6565 F: drivers/i2c/busses/i2c-mv64xxx.c ··· 12901 12901 F: include/media/soc* 12902 12902 F: drivers/media/i2c/soc_camera/ 12903 12903 F: drivers/media/platform/soc_camera/ 12904 + 12905 + SOCIONEXT SYNQUACER I2C DRIVER 12906 + M: Ard Biesheuvel <ard.biesheuvel@linaro.org> 12907 + L: linux-i2c@vger.kernel.org 12908 + S: Maintained 12909 + F: drivers/i2c/busses/i2c-synquacer.c 12910 + F: Documentation/devicetree/bindings/i2c/i2c-synquacer.txt 12904 12911 12905 12912 SOCIONEXT UNIPHIER SOUND DRIVER 12906 12913 M: Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>
+10 -1
arch/sh/boards/board-sh7785lcr.c
··· 25 25 #include <linux/io.h> 26 26 #include <linux/clk.h> 27 27 #include <linux/errno.h> 28 + #include <linux/gpio/machine.h> 28 29 #include <mach/sh7785lcr.h> 29 30 #include <cpu/sh7785.h> 30 31 #include <asm/heartbeat.h> ··· 244 243 }, 245 244 }; 246 245 246 + static struct gpiod_lookup_table i2c_gpio_table = { 247 + .dev_id = "i2c.0", 248 + .table = { 249 + GPIO_LOOKUP("pfc-sh7757", 0, "reset-gpios", GPIO_ACTIVE_LOW), 250 + { }, 251 + }, 252 + }; 253 + 247 254 static struct i2c_pca9564_pf_platform_data i2c_platform_data = { 248 - .gpio = 0, 249 255 .i2c_clock_speed = I2C_PCA_CON_330kHz, 250 256 .timeout = HZ, 251 257 }; ··· 291 283 i2c_device.num_resources = ARRAY_SIZE(i2c_proto_resources); 292 284 } 293 285 286 + gpiod_add_lookup_table(&i2c_gpio_table); 294 287 return platform_add_devices(sh7785lcr_devices, 295 288 ARRAY_SIZE(sh7785lcr_devices)); 296 289 }
+10
drivers/i2c/busses/Kconfig
··· 979 979 This interface is used to connect to specific PMIC devices (like the 980 980 AXP221). 981 981 982 + config I2C_SYNQUACER 983 + tristate "Socionext SynQuacer I2C controller" 984 + depends on ARCH_SYNQUACER || COMPILE_TEST 985 + help 986 + Say Y here to include support for the I2C controller used in some 987 + Fujitsu and Socionext SoCs. 988 + 989 + This driver can also be built as a module. If so, the module 990 + will be called i2c-synquacer. 991 + 982 992 config I2C_TEGRA 983 993 tristate "NVIDIA Tegra internal I2C controller" 984 994 depends on ARCH_TEGRA
+1
drivers/i2c/busses/Makefile
··· 97 97 obj-$(CONFIG_I2C_STM32F7) += i2c-stm32f7.o 98 98 obj-$(CONFIG_I2C_STU300) += i2c-stu300.o 99 99 obj-$(CONFIG_I2C_SUN6I_P2WI) += i2c-sun6i-p2wi.o 100 + obj-$(CONFIG_I2C_SYNQUACER) += i2c-synquacer.o 100 101 obj-$(CONFIG_I2C_TEGRA) += i2c-tegra.o 101 102 obj-$(CONFIG_I2C_TEGRA_BPMP) += i2c-tegra-bpmp.o 102 103 obj-$(CONFIG_I2C_UNIPHIER) += i2c-uniphier.o
+1 -1
drivers/i2c/busses/i2c-designware-master.c
··· 163 163 if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK)) 164 164 dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT; 165 165 dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD); 166 - } else { 166 + } else if (dev->sda_hold_time) { 167 167 dev_warn(dev->dev, 168 168 "Hardware too old to adjust SDA hold time.\n"); 169 169 }
+57 -6
drivers/i2c/busses/i2c-exynos5.c
··· 128 128 #define HSI2C_TIMEOUT_EN (1u << 31) 129 129 #define HSI2C_TIMEOUT_MASK 0xff 130 130 131 + /* I2C_MANUAL_CMD register bits */ 132 + #define HSI2C_CMD_READ_DATA (1u << 4) 133 + #define HSI2C_CMD_SEND_STOP (1u << 2) 134 + 131 135 /* I2C_TRANS_STATUS register bits */ 132 136 #define HSI2C_MASTER_BUSY (1u << 17) 133 137 #define HSI2C_SLAVE_BUSY (1u << 16) ··· 445 441 i2c->state = -ETIMEDOUT; 446 442 goto stop; 447 443 } 448 - 449 - trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS); 450 - if ((trans_status & HSI2C_MASTER_ST_MASK) == HSI2C_MASTER_ST_LOSE) { 451 - i2c->state = -EAGAIN; 452 - goto stop; 453 - } 454 444 } else if (int_status & HSI2C_INT_I2C) { 455 445 trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS); 456 446 if (trans_status & HSI2C_NO_DEV_ACK) { ··· 542 544 return -EBUSY; 543 545 } 544 546 547 + static void exynos5_i2c_bus_recover(struct exynos5_i2c *i2c) 548 + { 549 + u32 val; 550 + 551 + val = readl(i2c->regs + HSI2C_CTL) | HSI2C_RXCHON; 552 + writel(val, i2c->regs + HSI2C_CTL); 553 + val = readl(i2c->regs + HSI2C_CONF) & ~HSI2C_AUTO_MODE; 554 + writel(val, i2c->regs + HSI2C_CONF); 555 + 556 + /* 557 + * Specification says master should send nine clock pulses. It can be 558 + * emulated by sending manual read command (nine pulses for read eight 559 + * bits + one pulse for NACK). 560 + */ 561 + writel(HSI2C_CMD_READ_DATA, i2c->regs + HSI2C_MANUAL_CMD); 562 + exynos5_i2c_wait_bus_idle(i2c); 563 + writel(HSI2C_CMD_SEND_STOP, i2c->regs + HSI2C_MANUAL_CMD); 564 + exynos5_i2c_wait_bus_idle(i2c); 565 + 566 + val = readl(i2c->regs + HSI2C_CTL) & ~HSI2C_RXCHON; 567 + writel(val, i2c->regs + HSI2C_CTL); 568 + val = readl(i2c->regs + HSI2C_CONF) | HSI2C_AUTO_MODE; 569 + writel(val, i2c->regs + HSI2C_CONF); 570 + } 571 + 572 + static void exynos5_i2c_bus_check(struct exynos5_i2c *i2c) 573 + { 574 + unsigned long timeout; 575 + 576 + if (i2c->variant->hw != HSI2C_EXYNOS7) 577 + return; 578 + 579 + /* 580 + * HSI2C_MASTER_ST_LOSE state in EXYNOS7 variant before transaction 581 + * indicates that bus is stuck (SDA is low). In such case bus recovery 582 + * can be performed. 583 + */ 584 + timeout = jiffies + msecs_to_jiffies(100); 585 + for (;;) { 586 + u32 st = readl(i2c->regs + HSI2C_TRANS_STATUS); 587 + 588 + if ((st & HSI2C_MASTER_ST_MASK) != HSI2C_MASTER_ST_LOSE) 589 + return; 590 + 591 + if (time_is_before_jiffies(timeout)) 592 + return; 593 + 594 + exynos5_i2c_bus_recover(i2c); 595 + } 596 + } 597 + 545 598 /* 546 599 * exynos5_i2c_message_start: Configures the bus and starts the xfer 547 600 * i2c: struct exynos5_i2c pointer for the current bus ··· 646 597 647 598 writel(fifo_ctl, i2c->regs + HSI2C_FIFO_CTL); 648 599 writel(i2c_ctl, i2c->regs + HSI2C_CTL); 600 + 601 + exynos5_i2c_bus_check(i2c); 649 602 650 603 /* 651 604 * Enable interrupts before starting the transfer so that we don't
+27 -9
drivers/i2c/busses/i2c-imx.c
··· 194 194 struct imx_i2c_struct { 195 195 struct i2c_adapter adapter; 196 196 struct clk *clk; 197 + struct notifier_block clk_change_nb; 197 198 void __iomem *base; 198 199 wait_queue_head_t queue; 199 200 unsigned long i2csr; ··· 468 467 return 0; 469 468 } 470 469 471 - static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx) 470 + static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx, 471 + unsigned int i2c_clk_rate) 472 472 { 473 473 struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div; 474 - unsigned int i2c_clk_rate; 475 474 unsigned int div; 476 475 int i; 477 476 478 477 /* Divider value calculation */ 479 - i2c_clk_rate = clk_get_rate(i2c_imx->clk); 480 478 if (i2c_imx->cur_clk == i2c_clk_rate) 481 479 return; 482 480 ··· 510 510 #endif 511 511 } 512 512 513 + static int i2c_imx_clk_notifier_call(struct notifier_block *nb, 514 + unsigned long action, void *data) 515 + { 516 + struct clk_notifier_data *ndata = data; 517 + struct imx_i2c_struct *i2c_imx = container_of(&ndata->clk, 518 + struct imx_i2c_struct, 519 + clk); 520 + 521 + if (action & POST_RATE_CHANGE) 522 + i2c_imx_set_clk(i2c_imx, ndata->new_rate); 523 + 524 + return NOTIFY_OK; 525 + } 526 + 513 527 static int i2c_imx_start(struct imx_i2c_struct *i2c_imx) 514 528 { 515 529 unsigned int temp = 0; 516 530 int result; 517 531 518 532 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); 519 - 520 - i2c_imx_set_clk(i2c_imx); 521 533 522 534 imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR); 523 535 /* Enable I2C controller */ ··· 1143 1131 "clock-frequency", &i2c_imx->bitrate); 1144 1132 if (ret < 0 && pdata && pdata->bitrate) 1145 1133 i2c_imx->bitrate = pdata->bitrate; 1134 + i2c_imx->clk_change_nb.notifier_call = i2c_imx_clk_notifier_call; 1135 + clk_notifier_register(i2c_imx->clk, &i2c_imx->clk_change_nb); 1136 + i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk)); 1146 1137 1147 1138 /* Set up chip registers to defaults */ 1148 1139 imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN, ··· 1156 1141 ret = i2c_imx_init_recovery_info(i2c_imx, pdev); 1157 1142 /* Give it another chance if pinctrl used is not ready yet */ 1158 1143 if (ret == -EPROBE_DEFER) 1159 - goto rpm_disable; 1144 + goto clk_notifier_unregister; 1160 1145 1161 1146 /* Add I2C adapter */ 1162 1147 ret = i2c_add_numbered_adapter(&i2c_imx->adapter); 1163 1148 if (ret < 0) 1164 - goto rpm_disable; 1149 + goto clk_notifier_unregister; 1165 1150 1166 1151 pm_runtime_mark_last_busy(&pdev->dev); 1167 1152 pm_runtime_put_autosuspend(&pdev->dev); ··· 1177 1162 1178 1163 return 0; /* Return OK */ 1179 1164 1165 + clk_notifier_unregister: 1166 + clk_notifier_unregister(i2c_imx->clk, &i2c_imx->clk_change_nb); 1180 1167 rpm_disable: 1181 1168 pm_runtime_put_noidle(&pdev->dev); 1182 1169 pm_runtime_disable(&pdev->dev); ··· 1212 1195 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR); 1213 1196 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR); 1214 1197 1198 + clk_notifier_unregister(i2c_imx->clk, &i2c_imx->clk_change_nb); 1215 1199 clk_disable_unprepare(i2c_imx->clk); 1216 1200 1217 1201 pm_runtime_put_noidle(&pdev->dev); ··· 1226 1208 { 1227 1209 struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev); 1228 1210 1229 - clk_disable_unprepare(i2c_imx->clk); 1211 + clk_disable(i2c_imx->clk); 1230 1212 1231 1213 return 0; 1232 1214 } ··· 1236 1218 struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev); 1237 1219 int ret; 1238 1220 1239 - ret = clk_prepare_enable(i2c_imx->clk); 1221 + ret = clk_enable(i2c_imx->clk); 1240 1222 if (ret) 1241 1223 dev_err(dev, "can't enable I2C clock, ret=%d\n", ret); 1242 1224
+6 -2
drivers/i2c/busses/i2c-mv64xxx.c
··· 845 845 */ 846 846 if (of_device_is_compatible(np, "marvell,mv78230-i2c")) { 847 847 drv_data->offload_enabled = true; 848 - drv_data->errata_delay = true; 848 + /* The delay is only needed in standard mode (100kHz) */ 849 + if (bus_freq <= 100000) 850 + drv_data->errata_delay = true; 849 851 } 850 852 851 853 if (of_device_is_compatible(np, "marvell,mv78230-a0-i2c")) { 852 854 drv_data->offload_enabled = false; 853 - drv_data->errata_delay = true; 855 + /* The delay is only needed in standard mode (100kHz) */ 856 + if (bus_freq <= 100000) 857 + drv_data->errata_delay = true; 854 858 } 855 859 856 860 if (of_device_is_compatible(np, "allwinner,sun6i-a31-i2c"))
+10 -24
drivers/i2c/busses/i2c-pca-platform.c
··· 173 173 i2c->adap.dev.parent = &pdev->dev; 174 174 i2c->adap.dev.of_node = np; 175 175 176 + i2c->gpio = devm_gpiod_get_optional(&pdev->dev, "reset-gpios", GPIOD_OUT_LOW); 177 + if (IS_ERR(i2c->gpio)) 178 + return PTR_ERR(i2c->gpio); 179 + 180 + i2c->adap.timeout = HZ; 181 + ret = device_property_read_u32(&pdev->dev, "clock-frequency", 182 + &i2c->algo_data.i2c_clock); 183 + if (ret) 184 + i2c->algo_data.i2c_clock = 59000; 185 + 176 186 if (platform_data) { 177 187 i2c->adap.timeout = platform_data->timeout; 178 188 i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed; 179 - if (gpio_is_valid(platform_data->gpio)) { 180 - ret = devm_gpio_request_one(&pdev->dev, 181 - platform_data->gpio, 182 - GPIOF_ACTIVE_LOW, 183 - i2c->adap.name); 184 - if (ret == 0) { 185 - i2c->gpio = gpio_to_desc(platform_data->gpio); 186 - gpiod_direction_output(i2c->gpio, 0); 187 - } else { 188 - dev_warn(&pdev->dev, "Registering gpio failed!\n"); 189 - i2c->gpio = NULL; 190 - } 191 - } 192 - } else if (np) { 193 - i2c->adap.timeout = HZ; 194 - i2c->gpio = devm_gpiod_get_optional(&pdev->dev, "reset-gpios", GPIOD_OUT_LOW); 195 - if (IS_ERR(i2c->gpio)) 196 - return PTR_ERR(i2c->gpio); 197 - of_property_read_u32_index(np, "clock-frequency", 0, 198 - &i2c->algo_data.i2c_clock); 199 - } else { 200 - i2c->adap.timeout = HZ; 201 - i2c->algo_data.i2c_clock = 59000; 202 - i2c->gpio = NULL; 203 189 } 204 190 205 191 i2c->algo_data.data = i2c;
+28 -33
drivers/i2c/busses/i2c-piix4.c
··· 40 40 #include <linux/dmi.h> 41 41 #include <linux/acpi.h> 42 42 #include <linux/io.h> 43 - #include <linux/mutex.h> 44 43 45 44 46 45 /* PIIX4 SMBus address offsets */ ··· 152 153 153 154 /* 154 155 * SB800 globals 155 - * piix4_mutex_sb800 protects piix4_port_sel_sb800 and the pair 156 - * of I/O ports at SB800_PIIX4_SMB_IDX. 157 156 */ 158 - static DEFINE_MUTEX(piix4_mutex_sb800); 159 157 static u8 piix4_port_sel_sb800; 160 158 static u8 piix4_port_mask_sb800; 161 159 static u8 piix4_port_shift_sb800; ··· 294 298 else 295 299 smb_en = (aux) ? 0x28 : 0x2c; 296 300 297 - mutex_lock(&piix4_mutex_sb800); 301 + if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2, "sb800_piix4_smb")) { 302 + dev_err(&PIIX4_dev->dev, 303 + "SMB base address index region 0x%x already in use.\n", 304 + SB800_PIIX4_SMB_IDX); 305 + return -EBUSY; 306 + } 307 + 298 308 outb_p(smb_en, SB800_PIIX4_SMB_IDX); 299 309 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1); 300 310 outb_p(smb_en + 1, SB800_PIIX4_SMB_IDX); 301 311 smba_en_hi = inb_p(SB800_PIIX4_SMB_IDX + 1); 302 - mutex_unlock(&piix4_mutex_sb800); 312 + 313 + release_region(SB800_PIIX4_SMB_IDX, 2); 303 314 304 315 if (!smb_en) { 305 316 smb_en_status = smba_en_lo & 0x10; ··· 376 373 break; 377 374 } 378 375 } else { 379 - mutex_lock(&piix4_mutex_sb800); 376 + if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2, 377 + "sb800_piix4_smb")) { 378 + release_region(piix4_smba, SMBIOSIZE); 379 + return -EBUSY; 380 + } 381 + 380 382 outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX); 381 383 port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1); 382 384 piix4_port_sel_sb800 = (port_sel & 0x01) ? ··· 389 381 SB800_PIIX4_PORT_IDX; 390 382 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK; 391 383 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT; 392 - mutex_unlock(&piix4_mutex_sb800); 384 + release_region(SB800_PIIX4_SMB_IDX, 2); 393 385 } 394 386 395 387 dev_info(&PIIX4_dev->dev, ··· 470 462 471 463 /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */ 472 464 if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */ 473 - msleep(2); 465 + usleep_range(2000, 2100); 474 466 else 475 - msleep(1); 467 + usleep_range(250, 500); 476 468 477 469 while ((++timeout < MAX_TIMEOUT) && 478 470 ((temp = inb_p(SMBHSTSTS)) & 0x01)) 479 - msleep(1); 471 + usleep_range(250, 500); 480 472 481 473 /* If the SMBus is still busy, we give up */ 482 474 if (timeout == MAX_TIMEOUT) { ··· 687 679 u8 port; 688 680 int retval; 689 681 690 - mutex_lock(&piix4_mutex_sb800); 682 + if (!request_muxed_region(SB800_PIIX4_SMB_IDX, 2, "sb800_piix4_smb")) 683 + return -EBUSY; 691 684 692 685 /* Request the SMBUS semaphore, avoid conflicts with the IMC */ 693 686 smbslvcnt = inb_p(SMBSLVCNT); ··· 704 695 } while (--retries); 705 696 /* SMBus is still owned by the IMC, we give up */ 706 697 if (!retries) { 707 - mutex_unlock(&piix4_mutex_sb800); 708 - return -EBUSY; 698 + retval = -EBUSY; 699 + goto release; 709 700 } 710 701 711 702 /* ··· 762 753 if ((size == I2C_SMBUS_BLOCK_DATA) && adapdata->notify_imc) 763 754 piix4_imc_wakeup(); 764 755 765 - mutex_unlock(&piix4_mutex_sb800); 766 - 756 + release: 757 + release_region(SB800_PIIX4_SMB_IDX, 2); 767 758 return retval; 768 759 } 769 760 ··· 908 899 bool notify_imc = false; 909 900 is_sb800 = true; 910 901 911 - if (!request_region(SB800_PIIX4_SMB_IDX, 2, "smba_idx")) { 912 - dev_err(&dev->dev, 913 - "SMBus base address index region 0x%x already in use!\n", 914 - SB800_PIIX4_SMB_IDX); 915 - return -EBUSY; 916 - } 917 - 918 902 if (dev->vendor == PCI_VENDOR_ID_AMD && 919 903 dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) { 920 904 u8 imc; ··· 924 922 925 923 /* base address location etc changed in SB800 */ 926 924 retval = piix4_setup_sb800(dev, id, 0); 927 - if (retval < 0) { 928 - release_region(SB800_PIIX4_SMB_IDX, 2); 925 + if (retval < 0) 929 926 return retval; 930 - } 931 927 932 928 /* 933 929 * Try to register multiplexed main SMBus adapter, 934 930 * give up if we can't 935 931 */ 936 932 retval = piix4_add_adapters_sb800(dev, retval, notify_imc); 937 - if (retval < 0) { 938 - release_region(SB800_PIIX4_SMB_IDX, 2); 933 + if (retval < 0) 939 934 return retval; 940 - } 941 935 } else { 942 936 retval = piix4_setup(dev, id); 943 937 if (retval < 0) ··· 981 983 982 984 if (adapdata->smba) { 983 985 i2c_del_adapter(adap); 984 - if (adapdata->port == (0 << piix4_port_shift_sb800)) { 986 + if (adapdata->port == (0 << piix4_port_shift_sb800)) 985 987 release_region(adapdata->smba, SMBIOSIZE); 986 - if (adapdata->sb800_main) 987 - release_region(SB800_PIIX4_SMB_IDX, 2); 988 - } 989 988 kfree(adapdata); 990 989 kfree(adap); 991 990 }
+875 -622
drivers/i2c/busses/i2c-qup.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 1 2 /* 2 - * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved. 3 + * Copyright (c) 2009-2013, 2016-2018, The Linux Foundation. All rights reserved. 3 4 * Copyright (c) 2014, Sony Mobile Communications AB. 4 - * 5 - * 6 - * This program is free software; you can redistribute it and/or modify 7 - * it under the terms of the GNU General Public License version 2 and 8 - * only version 2 as published by the Free Software Foundation. 9 - * 10 - * This program is distributed in the hope that it will be useful, 11 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 - * GNU General Public License for more details. 14 5 * 15 6 */ 16 7 ··· 64 73 #define QUP_IN_SVC_FLAG BIT(9) 65 74 #define QUP_MX_OUTPUT_DONE BIT(10) 66 75 #define QUP_MX_INPUT_DONE BIT(11) 76 + #define OUT_BLOCK_WRITE_REQ BIT(12) 77 + #define IN_BLOCK_READ_REQ BIT(13) 67 78 68 79 /* I2C mini core related values */ 80 + #define QUP_NO_INPUT BIT(7) 69 81 #define QUP_CLOCK_AUTO_GATE BIT(13) 70 82 #define I2C_MINI_CORE (2 << 8) 71 83 #define I2C_N_VAL 15 ··· 107 113 #define QUP_TAG_V2_DATAWR 0x82 108 114 #define QUP_TAG_V2_DATAWR_STOP 0x83 109 115 #define QUP_TAG_V2_DATARD 0x85 116 + #define QUP_TAG_V2_DATARD_NACK 0x86 110 117 #define QUP_TAG_V2_DATARD_STOP 0x87 111 118 112 119 /* Status, Error flags */ ··· 122 127 #define ONE_BYTE 0x1 123 128 #define QUP_I2C_MX_CONFIG_DURING_RUN BIT(31) 124 129 130 + /* Maximum transfer length for single DMA descriptor */ 125 131 #define MX_TX_RX_LEN SZ_64K 126 132 #define MX_BLOCKS (MX_TX_RX_LEN / QUP_READ_LIMIT) 133 + /* Maximum transfer length for all DMA descriptors */ 134 + #define MX_DMA_TX_RX_LEN (2 * MX_TX_RX_LEN) 135 + #define MX_DMA_BLOCKS (MX_DMA_TX_RX_LEN / QUP_READ_LIMIT) 127 136 128 - /* Max timeout in ms for 32k bytes */ 129 - #define TOUT_MAX 300 137 + /* 138 + * Minimum transfer timeout for i2c transfers in seconds. It will be added on 139 + * the top of maximum transfer time calculated from i2c bus speed to compensate 140 + * the overheads. 141 + */ 142 + #define TOUT_MIN 2 130 143 131 144 /* Default values. Use these if FW query fails */ 132 145 #define DEFAULT_CLK_FREQ 100000 133 146 #define DEFAULT_SRC_CLK 20000000 134 147 148 + /* 149 + * Max tags length (start, stop and maximum 2 bytes address) for each QUP 150 + * data transfer 151 + */ 152 + #define QUP_MAX_TAGS_LEN 4 153 + /* Max data length for each DATARD tags */ 154 + #define RECV_MAX_DATA_LEN 254 155 + /* TAG length for DATA READ in RX FIFO */ 156 + #define READ_RX_TAGS_LEN 2 157 + 158 + /* 159 + * count: no of blocks 160 + * pos: current block number 161 + * tx_tag_len: tx tag length for current block 162 + * rx_tag_len: rx tag length for current block 163 + * data_len: remaining data length for current message 164 + * cur_blk_len: data length for current block 165 + * total_tx_len: total tx length including tag bytes for current QUP transfer 166 + * total_rx_len: total rx length including tag bytes for current QUP transfer 167 + * tx_fifo_data_pos: current byte number in TX FIFO word 168 + * tx_fifo_free: number of free bytes in current QUP block write. 169 + * rx_fifo_data_pos: current byte number in RX FIFO word 170 + * fifo_available: number of available bytes in RX FIFO for current 171 + * QUP block read 172 + * tx_fifo_data: QUP TX FIFO write works on word basis (4 bytes). New byte write 173 + * to TX FIFO will be appended in this data and will be written to 174 + * TX FIFO when all the 4 bytes are available. 175 + * rx_fifo_data: QUP RX FIFO read works on word basis (4 bytes). This will 176 + * contains the 4 bytes of RX data. 177 + * cur_data: pointer to tell cur data position for current message 178 + * cur_tx_tags: pointer to tell cur position in tags 179 + * tx_tags_sent: all tx tag bytes have been written in FIFO word 180 + * send_last_word: for tx FIFO, last word send is pending in current block 181 + * rx_bytes_read: if all the bytes have been read from rx FIFO. 182 + * rx_tags_fetched: all the rx tag bytes have been fetched from rx fifo word 183 + * is_tx_blk_mode: whether tx uses block or FIFO mode in case of non BAM xfer. 184 + * is_rx_blk_mode: whether rx uses block or FIFO mode in case of non BAM xfer. 185 + * tags: contains tx tag bytes for current QUP transfer 186 + */ 135 187 struct qup_i2c_block { 136 - int count; 137 - int pos; 138 - int tx_tag_len; 139 - int rx_tag_len; 140 - int data_len; 141 - u8 tags[6]; 188 + int count; 189 + int pos; 190 + int tx_tag_len; 191 + int rx_tag_len; 192 + int data_len; 193 + int cur_blk_len; 194 + int total_tx_len; 195 + int total_rx_len; 196 + int tx_fifo_data_pos; 197 + int tx_fifo_free; 198 + int rx_fifo_data_pos; 199 + int fifo_available; 200 + u32 tx_fifo_data; 201 + u32 rx_fifo_data; 202 + u8 *cur_data; 203 + u8 *cur_tx_tags; 204 + bool tx_tags_sent; 205 + bool send_last_word; 206 + bool rx_tags_fetched; 207 + bool rx_bytes_read; 208 + bool is_tx_blk_mode; 209 + bool is_rx_blk_mode; 210 + u8 tags[6]; 142 211 }; 143 212 144 213 struct qup_i2c_tag { ··· 214 155 struct qup_i2c_tag tag; 215 156 struct dma_chan *dma; 216 157 struct scatterlist *sg; 158 + unsigned int sg_cnt; 217 159 }; 218 160 219 161 struct qup_i2c_dev { ··· 231 171 int out_blk_sz; 232 172 int in_blk_sz; 233 173 174 + int blk_xfer_limit; 234 175 unsigned long one_byte_t; 176 + unsigned long xfer_timeout; 235 177 struct qup_i2c_block blk; 236 178 237 179 struct i2c_msg *msg; ··· 246 184 247 185 /* To check if this is the last msg */ 248 186 bool is_last; 187 + bool is_smbus_read; 249 188 250 189 /* To configure when bus is in run state */ 251 - int config_run; 190 + u32 config_run; 252 191 253 192 /* dma parameters */ 254 193 bool is_dma; 194 + /* To check if the current transfer is using DMA */ 195 + bool use_dma; 196 + unsigned int max_xfer_sg_len; 197 + unsigned int tag_buf_pos; 198 + /* The threshold length above which block mode will be used */ 199 + unsigned int blk_mode_threshold; 255 200 struct dma_pool *dpool; 256 201 struct qup_i2c_tag start_tag; 257 202 struct qup_i2c_bam brx; 258 203 struct qup_i2c_bam btx; 259 204 260 205 struct completion xfer; 206 + /* function to write data in tx fifo */ 207 + void (*write_tx_fifo)(struct qup_i2c_dev *qup); 208 + /* function to read data from rx fifo */ 209 + void (*read_rx_fifo)(struct qup_i2c_dev *qup); 210 + /* function to write tags in tx fifo for i2c read transfer */ 211 + void (*write_rx_tags)(struct qup_i2c_dev *qup); 261 212 }; 262 213 263 214 static irqreturn_t qup_i2c_interrupt(int irq, void *dev) 264 215 { 265 216 struct qup_i2c_dev *qup = dev; 217 + struct qup_i2c_block *blk = &qup->blk; 266 218 u32 bus_err; 267 219 u32 qup_err; 268 220 u32 opflags; ··· 302 226 if (bus_err) 303 227 writel(bus_err, qup->base + QUP_I2C_STATUS); 304 228 229 + /* 230 + * Check for BAM mode and returns if already error has come for current 231 + * transfer. In Error case, sometimes, QUP generates more than one 232 + * interrupt. 233 + */ 234 + if (qup->use_dma && (qup->qup_err || qup->bus_err)) 235 + return IRQ_HANDLED; 236 + 305 237 /* Reset the QUP State in case of error */ 306 238 if (qup_err || bus_err) { 307 - writel(QUP_RESET_STATE, qup->base + QUP_STATE); 239 + /* 240 + * Don’t reset the QUP state in case of BAM mode. The BAM 241 + * flush operation needs to be scheduled in transfer function 242 + * which will clear the remaining schedule descriptors in BAM 243 + * HW FIFO and generates the BAM interrupt. 244 + */ 245 + if (!qup->use_dma) 246 + writel(QUP_RESET_STATE, qup->base + QUP_STATE); 308 247 goto done; 309 248 } 310 249 311 - if (opflags & QUP_IN_SVC_FLAG) 250 + if (opflags & QUP_OUT_SVC_FLAG) { 251 + writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL); 252 + 253 + if (opflags & OUT_BLOCK_WRITE_REQ) { 254 + blk->tx_fifo_free += qup->out_blk_sz; 255 + if (qup->msg->flags & I2C_M_RD) 256 + qup->write_rx_tags(qup); 257 + else 258 + qup->write_tx_fifo(qup); 259 + } 260 + } 261 + 262 + if (opflags & QUP_IN_SVC_FLAG) { 312 263 writel(QUP_IN_SVC_FLAG, qup->base + QUP_OPERATIONAL); 313 264 314 - if (opflags & QUP_OUT_SVC_FLAG) 315 - writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL); 265 + if (!blk->is_rx_blk_mode) { 266 + blk->fifo_available += qup->in_fifo_sz; 267 + qup->read_rx_fifo(qup); 268 + } else if (opflags & IN_BLOCK_READ_REQ) { 269 + blk->fifo_available += qup->in_blk_sz; 270 + qup->read_rx_fifo(qup); 271 + } 272 + } 273 + 274 + if (qup->msg->flags & I2C_M_RD) { 275 + if (!blk->rx_bytes_read) 276 + return IRQ_HANDLED; 277 + } else { 278 + /* 279 + * Ideally, QUP_MAX_OUTPUT_DONE_FLAG should be checked 280 + * for FIFO mode also. But, QUP_MAX_OUTPUT_DONE_FLAG lags 281 + * behind QUP_OUTPUT_SERVICE_FLAG sometimes. The only reason 282 + * of interrupt for write message in FIFO mode is 283 + * QUP_MAX_OUTPUT_DONE_FLAG condition. 284 + */ 285 + if (blk->is_tx_blk_mode && !(opflags & QUP_MX_OUTPUT_DONE)) 286 + return IRQ_HANDLED; 287 + } 316 288 317 289 done: 318 290 qup->qup_err = qup_err; ··· 427 303 return 0; 428 304 } 429 305 430 - /** 431 - * qup_i2c_wait_ready - wait for a give number of bytes in tx/rx path 432 - * @qup: The qup_i2c_dev device 433 - * @op: The bit/event to wait on 434 - * @val: value of the bit to wait on, 0 or 1 435 - * @len: The length the bytes to be transferred 436 - */ 437 - static int qup_i2c_wait_ready(struct qup_i2c_dev *qup, int op, bool val, 438 - int len) 306 + /* Check if I2C bus returns to IDLE state */ 307 + static int qup_i2c_bus_active(struct qup_i2c_dev *qup, int len) 439 308 { 440 309 unsigned long timeout; 441 - u32 opflags; 442 310 u32 status; 443 - u32 shift = __ffs(op); 444 311 int ret = 0; 445 312 446 - len *= qup->one_byte_t; 447 - /* timeout after a wait of twice the max time */ 448 313 timeout = jiffies + len * 4; 449 - 450 314 for (;;) { 451 - opflags = readl(qup->base + QUP_OPERATIONAL); 452 315 status = readl(qup->base + QUP_I2C_STATUS); 316 + if (!(status & I2C_STATUS_BUS_ACTIVE)) 317 + break; 453 318 454 - if (((opflags & op) >> shift) == val) { 455 - if ((op == QUP_OUT_NOT_EMPTY) && qup->is_last) { 456 - if (!(status & I2C_STATUS_BUS_ACTIVE)) { 457 - ret = 0; 458 - goto done; 459 - } 460 - } else { 461 - ret = 0; 462 - goto done; 463 - } 464 - } 465 - 466 - if (time_after(jiffies, timeout)) { 319 + if (time_after(jiffies, timeout)) 467 320 ret = -ETIMEDOUT; 468 - goto done; 469 - } 321 + 470 322 usleep_range(len, len * 2); 471 323 } 472 324 473 - done: 474 - if (qup->bus_err || qup->qup_err) 475 - ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO; 476 - 477 325 return ret; 478 326 } 479 327 480 - static void qup_i2c_set_write_mode_v2(struct qup_i2c_dev *qup, 481 - struct i2c_msg *msg) 328 + static void qup_i2c_write_tx_fifo_v1(struct qup_i2c_dev *qup) 482 329 { 483 - /* Number of entries to shift out, including the tags */ 484 - int total = msg->len + qup->blk.tx_tag_len; 485 - 486 - total |= qup->config_run; 487 - 488 - if (total < qup->out_fifo_sz) { 489 - /* FIFO mode */ 490 - writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE); 491 - writel(total, qup->base + QUP_MX_WRITE_CNT); 492 - } else { 493 - /* BLOCK mode (transfer data on chunks) */ 494 - writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN, 495 - qup->base + QUP_IO_MODE); 496 - writel(total, qup->base + QUP_MX_OUTPUT_CNT); 497 - } 498 - } 499 - 500 - static void qup_i2c_set_write_mode(struct qup_i2c_dev *qup, struct i2c_msg *msg) 501 - { 502 - /* Number of entries to shift out, including the start */ 503 - int total = msg->len + 1; 504 - 505 - if (total < qup->out_fifo_sz) { 506 - /* FIFO mode */ 507 - writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE); 508 - writel(total, qup->base + QUP_MX_WRITE_CNT); 509 - } else { 510 - /* BLOCK mode (transfer data on chunks) */ 511 - writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN, 512 - qup->base + QUP_IO_MODE); 513 - writel(total, qup->base + QUP_MX_OUTPUT_CNT); 514 - } 515 - } 516 - 517 - static int check_for_fifo_space(struct qup_i2c_dev *qup) 518 - { 519 - int ret; 520 - 521 - ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE); 522 - if (ret) 523 - goto out; 524 - 525 - ret = qup_i2c_wait_ready(qup, QUP_OUT_FULL, 526 - RESET_BIT, 4 * ONE_BYTE); 527 - if (ret) { 528 - /* Fifo is full. Drain out the fifo */ 529 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 530 - if (ret) 531 - goto out; 532 - 533 - ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, 534 - RESET_BIT, 256 * ONE_BYTE); 535 - if (ret) { 536 - dev_err(qup->dev, "timeout for fifo out full"); 537 - goto out; 538 - } 539 - 540 - ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE); 541 - if (ret) 542 - goto out; 543 - } 544 - 545 - out: 546 - return ret; 547 - } 548 - 549 - static int qup_i2c_issue_write(struct qup_i2c_dev *qup, struct i2c_msg *msg) 550 - { 330 + struct qup_i2c_block *blk = &qup->blk; 331 + struct i2c_msg *msg = qup->msg; 551 332 u32 addr = msg->addr << 1; 552 333 u32 qup_tag; 553 334 int idx; 554 335 u32 val; 555 - int ret = 0; 556 336 557 337 if (qup->pos == 0) { 558 338 val = QUP_TAG_START | addr; 559 339 idx = 1; 340 + blk->tx_fifo_free--; 560 341 } else { 561 342 val = 0; 562 343 idx = 0; 563 344 } 564 345 565 - while (qup->pos < msg->len) { 566 - /* Check that there's space in the FIFO for our pair */ 567 - ret = check_for_fifo_space(qup); 568 - if (ret) 569 - return ret; 570 - 346 + while (blk->tx_fifo_free && qup->pos < msg->len) { 571 347 if (qup->pos == msg->len - 1) 572 348 qup_tag = QUP_TAG_STOP; 573 349 else ··· 484 460 485 461 qup->pos++; 486 462 idx++; 463 + blk->tx_fifo_free--; 487 464 } 488 - 489 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 490 - 491 - return ret; 492 465 } 493 466 494 467 static void qup_i2c_set_blk_data(struct qup_i2c_dev *qup, 495 468 struct i2c_msg *msg) 496 469 { 497 - memset(&qup->blk, 0, sizeof(qup->blk)); 498 - 470 + qup->blk.pos = 0; 499 471 qup->blk.data_len = msg->len; 500 - qup->blk.count = (msg->len + QUP_READ_LIMIT - 1) / QUP_READ_LIMIT; 501 - 502 - /* 4 bytes for first block and 2 writes for rest */ 503 - qup->blk.tx_tag_len = 4 + (qup->blk.count - 1) * 2; 504 - 505 - /* There are 2 tag bytes that are read in to fifo for every block */ 506 - if (msg->flags & I2C_M_RD) 507 - qup->blk.rx_tag_len = qup->blk.count * 2; 508 - } 509 - 510 - static int qup_i2c_send_data(struct qup_i2c_dev *qup, int tlen, u8 *tbuf, 511 - int dlen, u8 *dbuf) 512 - { 513 - u32 val = 0, idx = 0, pos = 0, i = 0, t; 514 - int len = tlen + dlen; 515 - u8 *buf = tbuf; 516 - int ret = 0; 517 - 518 - while (len > 0) { 519 - ret = check_for_fifo_space(qup); 520 - if (ret) 521 - return ret; 522 - 523 - t = (len >= 4) ? 4 : len; 524 - 525 - while (idx < t) { 526 - if (!i && (pos >= tlen)) { 527 - buf = dbuf; 528 - pos = 0; 529 - i = 1; 530 - } 531 - val |= buf[pos++] << (idx++ * 8); 532 - } 533 - 534 - writel(val, qup->base + QUP_OUT_FIFO_BASE); 535 - idx = 0; 536 - val = 0; 537 - len -= 4; 538 - } 539 - 540 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 541 - 542 - return ret; 472 + qup->blk.count = DIV_ROUND_UP(msg->len, qup->blk_xfer_limit); 543 473 } 544 474 545 475 static int qup_i2c_get_data_len(struct qup_i2c_dev *qup) 546 476 { 547 477 int data_len; 548 478 549 - if (qup->blk.data_len > QUP_READ_LIMIT) 550 - data_len = QUP_READ_LIMIT; 479 + if (qup->blk.data_len > qup->blk_xfer_limit) 480 + data_len = qup->blk_xfer_limit; 551 481 else 552 482 data_len = qup->blk.data_len; 553 483 ··· 518 540 { 519 541 int len = 0; 520 542 521 - if (msg->len > 1) { 543 + if (qup->is_smbus_read) { 522 544 tags[len++] = QUP_TAG_V2_DATARD_STOP; 523 - tags[len++] = qup_i2c_get_data_len(qup) - 1; 545 + tags[len++] = qup_i2c_get_data_len(qup); 524 546 } else { 525 547 tags[len++] = QUP_TAG_V2_START; 526 548 tags[len++] = addr & 0xff; ··· 536 558 } 537 559 538 560 static int qup_i2c_set_tags(u8 *tags, struct qup_i2c_dev *qup, 539 - struct i2c_msg *msg, int is_dma) 561 + struct i2c_msg *msg) 540 562 { 541 563 u16 addr = i2c_8bit_addr_from_msg(msg); 542 564 int len = 0; ··· 564 586 tags[len++] = QUP_TAG_V2_DATAWR_STOP; 565 587 } else { 566 588 if (msg->flags & I2C_M_RD) 567 - tags[len++] = QUP_TAG_V2_DATARD; 589 + tags[len++] = qup->blk.pos == (qup->blk.count - 1) ? 590 + QUP_TAG_V2_DATARD_NACK : 591 + QUP_TAG_V2_DATARD; 568 592 else 569 593 tags[len++] = QUP_TAG_V2_DATAWR; 570 594 } ··· 579 599 else 580 600 tags[len++] = data_len; 581 601 582 - if ((msg->flags & I2C_M_RD) && last && is_dma) { 583 - tags[len++] = QUP_BAM_INPUT_EOT; 584 - tags[len++] = QUP_BAM_FLUSH_STOP; 585 - } 586 - 587 602 return len; 588 603 } 589 604 590 - static int qup_i2c_issue_xfer_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg) 591 - { 592 - int data_len = 0, tag_len, index; 593 - int ret; 594 - 595 - tag_len = qup_i2c_set_tags(qup->blk.tags, qup, msg, 0); 596 - index = msg->len - qup->blk.data_len; 597 - 598 - /* only tags are written for read */ 599 - if (!(msg->flags & I2C_M_RD)) 600 - data_len = qup_i2c_get_data_len(qup); 601 - 602 - ret = qup_i2c_send_data(qup, tag_len, qup->blk.tags, 603 - data_len, &msg->buf[index]); 604 - qup->blk.data_len -= data_len; 605 - 606 - return ret; 607 - } 608 605 609 606 static void qup_i2c_bam_cb(void *data) 610 607 { ··· 641 684 return 0; 642 685 } 643 686 644 - static int qup_i2c_bam_do_xfer(struct qup_i2c_dev *qup, struct i2c_msg *msg, 645 - int num) 687 + static int qup_i2c_bam_make_desc(struct qup_i2c_dev *qup, struct i2c_msg *msg) 646 688 { 647 - struct dma_async_tx_descriptor *txd, *rxd = NULL; 648 - int ret = 0, idx = 0, limit = QUP_READ_LIMIT; 649 - dma_cookie_t cookie_rx, cookie_tx; 650 - u32 rx_nents = 0, tx_nents = 0, len, blocks, rem; 651 - u32 i, tlen, tx_len, tx_buf = 0, rx_buf = 0, off = 0; 689 + int ret = 0, limit = QUP_READ_LIMIT; 690 + u32 len = 0, blocks, rem; 691 + u32 i = 0, tlen, tx_len = 0; 652 692 u8 *tags; 653 693 654 - while (idx < num) { 655 - tx_len = 0, len = 0, i = 0; 694 + qup->blk_xfer_limit = QUP_READ_LIMIT; 695 + qup_i2c_set_blk_data(qup, msg); 656 696 657 - qup->is_last = (idx == (num - 1)); 697 + blocks = qup->blk.count; 698 + rem = msg->len - (blocks - 1) * limit; 658 699 659 - qup_i2c_set_blk_data(qup, msg); 700 + if (msg->flags & I2C_M_RD) { 701 + while (qup->blk.pos < blocks) { 702 + tlen = (i == (blocks - 1)) ? rem : limit; 703 + tags = &qup->start_tag.start[qup->tag_buf_pos + len]; 704 + len += qup_i2c_set_tags(tags, qup, msg); 705 + qup->blk.data_len -= tlen; 660 706 661 - blocks = qup->blk.count; 662 - rem = msg->len - (blocks - 1) * limit; 663 - 664 - if (msg->flags & I2C_M_RD) { 665 - rx_nents += (blocks * 2) + 1; 666 - tx_nents += 1; 667 - 668 - while (qup->blk.pos < blocks) { 669 - tlen = (i == (blocks - 1)) ? rem : limit; 670 - tags = &qup->start_tag.start[off + len]; 671 - len += qup_i2c_set_tags(tags, qup, msg, 1); 672 - qup->blk.data_len -= tlen; 673 - 674 - /* scratch buf to read the start and len tags */ 675 - ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++], 676 - &qup->brx.tag.start[0], 677 - 2, qup, DMA_FROM_DEVICE); 678 - 679 - if (ret) 680 - return ret; 681 - 682 - ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++], 683 - &msg->buf[limit * i], 684 - tlen, qup, 685 - DMA_FROM_DEVICE); 686 - if (ret) 687 - return ret; 688 - 689 - i++; 690 - qup->blk.pos = i; 691 - } 692 - ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++], 693 - &qup->start_tag.start[off], 694 - len, qup, DMA_TO_DEVICE); 695 - if (ret) 696 - return ret; 697 - 698 - off += len; 699 - /* scratch buf to read the BAM EOT and FLUSH tags */ 700 - ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++], 707 + /* scratch buf to read the start and len tags */ 708 + ret = qup_sg_set_buf(&qup->brx.sg[qup->brx.sg_cnt++], 701 709 &qup->brx.tag.start[0], 702 710 2, qup, DMA_FROM_DEVICE); 711 + 703 712 if (ret) 704 713 return ret; 705 - } else { 706 - tx_nents += (blocks * 2); 707 714 708 - while (qup->blk.pos < blocks) { 709 - tlen = (i == (blocks - 1)) ? rem : limit; 710 - tags = &qup->start_tag.start[off + tx_len]; 711 - len = qup_i2c_set_tags(tags, qup, msg, 1); 712 - qup->blk.data_len -= tlen; 715 + ret = qup_sg_set_buf(&qup->brx.sg[qup->brx.sg_cnt++], 716 + &msg->buf[limit * i], 717 + tlen, qup, 718 + DMA_FROM_DEVICE); 719 + if (ret) 720 + return ret; 713 721 714 - ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++], 715 - tags, len, 716 - qup, DMA_TO_DEVICE); 717 - if (ret) 718 - return ret; 719 - 720 - tx_len += len; 721 - ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++], 722 - &msg->buf[limit * i], 723 - tlen, qup, DMA_TO_DEVICE); 724 - if (ret) 725 - return ret; 726 - i++; 727 - qup->blk.pos = i; 728 - } 729 - off += tx_len; 730 - 731 - if (idx == (num - 1)) { 732 - len = 1; 733 - if (rx_nents) { 734 - qup->btx.tag.start[0] = 735 - QUP_BAM_INPUT_EOT; 736 - len++; 737 - } 738 - qup->btx.tag.start[len - 1] = 739 - QUP_BAM_FLUSH_STOP; 740 - ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++], 741 - &qup->btx.tag.start[0], 742 - len, qup, DMA_TO_DEVICE); 743 - if (ret) 744 - return ret; 745 - tx_nents += 1; 746 - } 722 + i++; 723 + qup->blk.pos = i; 747 724 } 748 - idx++; 749 - msg++; 725 + ret = qup_sg_set_buf(&qup->btx.sg[qup->btx.sg_cnt++], 726 + &qup->start_tag.start[qup->tag_buf_pos], 727 + len, qup, DMA_TO_DEVICE); 728 + if (ret) 729 + return ret; 730 + 731 + qup->tag_buf_pos += len; 732 + } else { 733 + while (qup->blk.pos < blocks) { 734 + tlen = (i == (blocks - 1)) ? rem : limit; 735 + tags = &qup->start_tag.start[qup->tag_buf_pos + tx_len]; 736 + len = qup_i2c_set_tags(tags, qup, msg); 737 + qup->blk.data_len -= tlen; 738 + 739 + ret = qup_sg_set_buf(&qup->btx.sg[qup->btx.sg_cnt++], 740 + tags, len, 741 + qup, DMA_TO_DEVICE); 742 + if (ret) 743 + return ret; 744 + 745 + tx_len += len; 746 + ret = qup_sg_set_buf(&qup->btx.sg[qup->btx.sg_cnt++], 747 + &msg->buf[limit * i], 748 + tlen, qup, DMA_TO_DEVICE); 749 + if (ret) 750 + return ret; 751 + i++; 752 + qup->blk.pos = i; 753 + } 754 + 755 + qup->tag_buf_pos += tx_len; 750 756 } 751 757 752 - txd = dmaengine_prep_slave_sg(qup->btx.dma, qup->btx.sg, tx_nents, 758 + return 0; 759 + } 760 + 761 + static int qup_i2c_bam_schedule_desc(struct qup_i2c_dev *qup) 762 + { 763 + struct dma_async_tx_descriptor *txd, *rxd = NULL; 764 + int ret = 0; 765 + dma_cookie_t cookie_rx, cookie_tx; 766 + u32 len = 0; 767 + u32 tx_cnt = qup->btx.sg_cnt, rx_cnt = qup->brx.sg_cnt; 768 + 769 + /* schedule the EOT and FLUSH I2C tags */ 770 + len = 1; 771 + if (rx_cnt) { 772 + qup->btx.tag.start[0] = QUP_BAM_INPUT_EOT; 773 + len++; 774 + 775 + /* scratch buf to read the BAM EOT FLUSH tags */ 776 + ret = qup_sg_set_buf(&qup->brx.sg[rx_cnt++], 777 + &qup->brx.tag.start[0], 778 + 1, qup, DMA_FROM_DEVICE); 779 + if (ret) 780 + return ret; 781 + } 782 + 783 + qup->btx.tag.start[len - 1] = QUP_BAM_FLUSH_STOP; 784 + ret = qup_sg_set_buf(&qup->btx.sg[tx_cnt++], &qup->btx.tag.start[0], 785 + len, qup, DMA_TO_DEVICE); 786 + if (ret) 787 + return ret; 788 + 789 + txd = dmaengine_prep_slave_sg(qup->btx.dma, qup->btx.sg, tx_cnt, 753 790 DMA_MEM_TO_DEV, 754 791 DMA_PREP_INTERRUPT | DMA_PREP_FENCE); 755 792 if (!txd) { ··· 752 801 goto desc_err; 753 802 } 754 803 755 - if (!rx_nents) { 804 + if (!rx_cnt) { 756 805 txd->callback = qup_i2c_bam_cb; 757 806 txd->callback_param = qup; 758 807 } ··· 765 814 766 815 dma_async_issue_pending(qup->btx.dma); 767 816 768 - if (rx_nents) { 817 + if (rx_cnt) { 769 818 rxd = dmaengine_prep_slave_sg(qup->brx.dma, qup->brx.sg, 770 - rx_nents, DMA_DEV_TO_MEM, 819 + rx_cnt, DMA_DEV_TO_MEM, 771 820 DMA_PREP_INTERRUPT); 772 821 if (!rxd) { 773 822 dev_err(qup->dev, "failed to get rx desc\n"); ··· 789 838 dma_async_issue_pending(qup->brx.dma); 790 839 } 791 840 792 - if (!wait_for_completion_timeout(&qup->xfer, TOUT_MAX * HZ)) { 841 + if (!wait_for_completion_timeout(&qup->xfer, qup->xfer_timeout)) { 793 842 dev_err(qup->dev, "normal trans timed out\n"); 794 843 ret = -ETIMEDOUT; 795 844 } 796 845 797 846 if (ret || qup->bus_err || qup->qup_err) { 847 + reinit_completion(&qup->xfer); 848 + 798 849 if (qup_i2c_change_state(qup, QUP_RUN_STATE)) { 799 850 dev_err(qup->dev, "change to run state timed out"); 800 851 goto desc_err; 801 852 } 802 - 803 - if (rx_nents) 804 - writel(QUP_BAM_INPUT_EOT, 805 - qup->base + QUP_OUT_FIFO_BASE); 806 - 807 - writel(QUP_BAM_FLUSH_STOP, qup->base + QUP_OUT_FIFO_BASE); 808 853 809 854 qup_i2c_flush(qup); 810 855 ··· 808 861 if (!wait_for_completion_timeout(&qup->xfer, HZ)) 809 862 dev_err(qup->dev, "flush timed out\n"); 810 863 811 - qup_i2c_rel_dma(qup); 812 - 813 864 ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO; 814 865 } 815 866 816 867 desc_err: 817 - dma_unmap_sg(qup->dev, qup->btx.sg, tx_nents, DMA_TO_DEVICE); 868 + dma_unmap_sg(qup->dev, qup->btx.sg, tx_cnt, DMA_TO_DEVICE); 818 869 819 - if (rx_nents) 820 - dma_unmap_sg(qup->dev, qup->brx.sg, rx_nents, 870 + if (rx_cnt) 871 + dma_unmap_sg(qup->dev, qup->brx.sg, rx_cnt, 821 872 DMA_FROM_DEVICE); 822 873 823 874 return ret; 875 + } 876 + 877 + static void qup_i2c_bam_clear_tag_buffers(struct qup_i2c_dev *qup) 878 + { 879 + qup->btx.sg_cnt = 0; 880 + qup->brx.sg_cnt = 0; 881 + qup->tag_buf_pos = 0; 824 882 } 825 883 826 884 static int qup_i2c_bam_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, ··· 833 881 { 834 882 struct qup_i2c_dev *qup = i2c_get_adapdata(adap); 835 883 int ret = 0; 884 + int idx = 0; 836 885 837 886 enable_irq(qup->irq); 838 887 ret = qup_i2c_req_dma(qup); ··· 856 903 goto out; 857 904 858 905 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL); 906 + qup_i2c_bam_clear_tag_buffers(qup); 859 907 860 - qup->msg = msg; 861 - ret = qup_i2c_bam_do_xfer(qup, qup->msg, num); 908 + for (idx = 0; idx < num; idx++) { 909 + qup->msg = msg + idx; 910 + qup->is_last = idx == (num - 1); 911 + 912 + ret = qup_i2c_bam_make_desc(qup, qup->msg); 913 + if (ret) 914 + break; 915 + 916 + /* 917 + * Make DMA descriptor and schedule the BAM transfer if its 918 + * already crossed the maximum length. Since the memory for all 919 + * tags buffers have been taken for 2 maximum possible 920 + * transfers length so it will never cross the buffer actual 921 + * length. 922 + */ 923 + if (qup->btx.sg_cnt > qup->max_xfer_sg_len || 924 + qup->brx.sg_cnt > qup->max_xfer_sg_len || 925 + qup->is_last) { 926 + ret = qup_i2c_bam_schedule_desc(qup); 927 + if (ret) 928 + break; 929 + 930 + qup_i2c_bam_clear_tag_buffers(qup); 931 + } 932 + } 933 + 862 934 out: 863 935 disable_irq(qup->irq); 864 936 ··· 897 919 unsigned long left; 898 920 int ret = 0; 899 921 900 - left = wait_for_completion_timeout(&qup->xfer, HZ); 922 + left = wait_for_completion_timeout(&qup->xfer, qup->xfer_timeout); 901 923 if (!left) { 902 924 writel(1, qup->base + QUP_SW_RESET); 903 925 ret = -ETIMEDOUT; ··· 909 931 return ret; 910 932 } 911 933 912 - static int qup_i2c_write_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg) 934 + static void qup_i2c_read_rx_fifo_v1(struct qup_i2c_dev *qup) 913 935 { 914 - int ret = 0; 936 + struct qup_i2c_block *blk = &qup->blk; 937 + struct i2c_msg *msg = qup->msg; 938 + u32 val = 0; 939 + int idx = 0; 915 940 916 - qup->msg = msg; 917 - qup->pos = 0; 918 - enable_irq(qup->irq); 919 - qup_i2c_set_blk_data(qup, msg); 920 - qup_i2c_set_write_mode_v2(qup, msg); 921 - 922 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 923 - if (ret) 924 - goto err; 925 - 926 - writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL); 927 - 928 - do { 929 - ret = qup_i2c_issue_xfer_v2(qup, msg); 930 - if (ret) 931 - goto err; 932 - 933 - ret = qup_i2c_wait_for_complete(qup, msg); 934 - if (ret) 935 - goto err; 936 - 937 - qup->blk.pos++; 938 - } while (qup->blk.pos < qup->blk.count); 939 - 940 - ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE); 941 - 942 - err: 943 - disable_irq(qup->irq); 944 - qup->msg = NULL; 945 - 946 - return ret; 947 - } 948 - 949 - static int qup_i2c_write_one(struct qup_i2c_dev *qup, struct i2c_msg *msg) 950 - { 951 - int ret; 952 - 953 - qup->msg = msg; 954 - qup->pos = 0; 955 - 956 - enable_irq(qup->irq); 957 - 958 - qup_i2c_set_write_mode(qup, msg); 959 - 960 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 961 - if (ret) 962 - goto err; 963 - 964 - writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL); 965 - 966 - do { 967 - ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE); 968 - if (ret) 969 - goto err; 970 - 971 - ret = qup_i2c_issue_write(qup, msg); 972 - if (ret) 973 - goto err; 974 - 975 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 976 - if (ret) 977 - goto err; 978 - 979 - ret = qup_i2c_wait_for_complete(qup, msg); 980 - if (ret) 981 - goto err; 982 - } while (qup->pos < msg->len); 983 - 984 - /* Wait for the outstanding data in the fifo to drain */ 985 - ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE); 986 - err: 987 - disable_irq(qup->irq); 988 - qup->msg = NULL; 989 - 990 - return ret; 991 - } 992 - 993 - static void qup_i2c_set_read_mode(struct qup_i2c_dev *qup, int len) 994 - { 995 - if (len < qup->in_fifo_sz) { 996 - /* FIFO mode */ 997 - writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE); 998 - writel(len, qup->base + QUP_MX_READ_CNT); 999 - } else { 1000 - /* BLOCK mode (transfer data on chunks) */ 1001 - writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN, 1002 - qup->base + QUP_IO_MODE); 1003 - writel(len, qup->base + QUP_MX_INPUT_CNT); 941 + while (blk->fifo_available && qup->pos < msg->len) { 942 + if ((idx & 1) == 0) { 943 + /* Reading 2 words at time */ 944 + val = readl(qup->base + QUP_IN_FIFO_BASE); 945 + msg->buf[qup->pos++] = val & 0xFF; 946 + } else { 947 + msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT; 948 + } 949 + idx++; 950 + blk->fifo_available--; 1004 951 } 952 + 953 + if (qup->pos == msg->len) 954 + blk->rx_bytes_read = true; 1005 955 } 1006 956 1007 - static void qup_i2c_set_read_mode_v2(struct qup_i2c_dev *qup, int len) 957 + static void qup_i2c_write_rx_tags_v1(struct qup_i2c_dev *qup) 1008 958 { 1009 - int tx_len = qup->blk.tx_tag_len; 1010 - 1011 - len += qup->blk.rx_tag_len; 1012 - len |= qup->config_run; 1013 - tx_len |= qup->config_run; 1014 - 1015 - if (len < qup->in_fifo_sz) { 1016 - /* FIFO mode */ 1017 - writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE); 1018 - writel(tx_len, qup->base + QUP_MX_WRITE_CNT); 1019 - writel(len, qup->base + QUP_MX_READ_CNT); 1020 - } else { 1021 - /* BLOCK mode (transfer data on chunks) */ 1022 - writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN, 1023 - qup->base + QUP_IO_MODE); 1024 - writel(tx_len, qup->base + QUP_MX_OUTPUT_CNT); 1025 - writel(len, qup->base + QUP_MX_INPUT_CNT); 1026 - } 1027 - } 1028 - 1029 - static void qup_i2c_issue_read(struct qup_i2c_dev *qup, struct i2c_msg *msg) 1030 - { 959 + struct i2c_msg *msg = qup->msg; 1031 960 u32 addr, len, val; 1032 961 1033 962 addr = i2c_8bit_addr_from_msg(msg); ··· 946 1061 writel(val, qup->base + QUP_OUT_FIFO_BASE); 947 1062 } 948 1063 949 - 950 - static int qup_i2c_read_fifo(struct qup_i2c_dev *qup, struct i2c_msg *msg) 1064 + static void qup_i2c_conf_v1(struct qup_i2c_dev *qup) 951 1065 { 952 - u32 val = 0; 953 - int idx; 954 - int ret = 0; 1066 + struct qup_i2c_block *blk = &qup->blk; 1067 + u32 qup_config = I2C_MINI_CORE | I2C_N_VAL; 1068 + u32 io_mode = QUP_REPACK_EN; 955 1069 956 - for (idx = 0; qup->pos < msg->len; idx++) { 957 - if ((idx & 1) == 0) { 958 - /* Check that FIFO have data */ 959 - ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY, 960 - SET_BIT, 4 * ONE_BYTE); 961 - if (ret) 962 - return ret; 1070 + blk->is_tx_blk_mode = 1071 + blk->total_tx_len > qup->out_fifo_sz ? true : false; 1072 + blk->is_rx_blk_mode = 1073 + blk->total_rx_len > qup->in_fifo_sz ? true : false; 963 1074 964 - /* Reading 2 words at time */ 965 - val = readl(qup->base + QUP_IN_FIFO_BASE); 1075 + if (blk->is_tx_blk_mode) { 1076 + io_mode |= QUP_OUTPUT_BLK_MODE; 1077 + writel(0, qup->base + QUP_MX_WRITE_CNT); 1078 + writel(blk->total_tx_len, qup->base + QUP_MX_OUTPUT_CNT); 1079 + } else { 1080 + writel(0, qup->base + QUP_MX_OUTPUT_CNT); 1081 + writel(blk->total_tx_len, qup->base + QUP_MX_WRITE_CNT); 1082 + } 966 1083 967 - msg->buf[qup->pos++] = val & 0xFF; 1084 + if (blk->total_rx_len) { 1085 + if (blk->is_rx_blk_mode) { 1086 + io_mode |= QUP_INPUT_BLK_MODE; 1087 + writel(0, qup->base + QUP_MX_READ_CNT); 1088 + writel(blk->total_rx_len, qup->base + QUP_MX_INPUT_CNT); 968 1089 } else { 969 - msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT; 1090 + writel(0, qup->base + QUP_MX_INPUT_CNT); 1091 + writel(blk->total_rx_len, qup->base + QUP_MX_READ_CNT); 970 1092 } 1093 + } else { 1094 + qup_config |= QUP_NO_INPUT; 971 1095 } 972 1096 973 - return ret; 1097 + writel(qup_config, qup->base + QUP_CONFIG); 1098 + writel(io_mode, qup->base + QUP_IO_MODE); 974 1099 } 975 1100 976 - static int qup_i2c_read_fifo_v2(struct qup_i2c_dev *qup, 977 - struct i2c_msg *msg) 1101 + static void qup_i2c_clear_blk_v1(struct qup_i2c_block *blk) 978 1102 { 979 - u32 val; 980 - int idx, pos = 0, ret = 0, total, msg_offset = 0; 981 - 982 - /* 983 - * If the message length is already read in 984 - * the first byte of the buffer, account for 985 - * that by setting the offset 986 - */ 987 - if (qup_i2c_check_msg_len(msg) && (msg->len > 1)) 988 - msg_offset = 1; 989 - total = qup_i2c_get_data_len(qup); 990 - total -= msg_offset; 991 - 992 - /* 2 extra bytes for read tags */ 993 - while (pos < (total + 2)) { 994 - /* Check that FIFO have data */ 995 - ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY, 996 - SET_BIT, 4 * ONE_BYTE); 997 - if (ret) { 998 - dev_err(qup->dev, "timeout for fifo not empty"); 999 - return ret; 1000 - } 1001 - val = readl(qup->base + QUP_IN_FIFO_BASE); 1002 - 1003 - for (idx = 0; idx < 4; idx++, val >>= 8, pos++) { 1004 - /* first 2 bytes are tag bytes */ 1005 - if (pos < 2) 1006 - continue; 1007 - 1008 - if (pos >= (total + 2)) 1009 - goto out; 1010 - msg->buf[qup->pos + msg_offset] = val & 0xff; 1011 - qup->pos++; 1012 - } 1013 - } 1014 - 1015 - out: 1016 - qup->blk.data_len -= total; 1017 - 1018 - return ret; 1103 + blk->tx_fifo_free = 0; 1104 + blk->fifo_available = 0; 1105 + blk->rx_bytes_read = false; 1019 1106 } 1020 1107 1021 - static int qup_i2c_read_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg) 1108 + static int qup_i2c_conf_xfer_v1(struct qup_i2c_dev *qup, bool is_rx) 1022 1109 { 1023 - int ret = 0; 1024 - 1025 - qup->msg = msg; 1026 - qup->pos = 0; 1027 - enable_irq(qup->irq); 1028 - qup_i2c_set_blk_data(qup, msg); 1029 - qup_i2c_set_read_mode_v2(qup, msg->len); 1030 - 1031 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 1032 - if (ret) 1033 - goto err; 1034 - 1035 - writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL); 1036 - 1037 - do { 1038 - ret = qup_i2c_issue_xfer_v2(qup, msg); 1039 - if (ret) 1040 - goto err; 1041 - 1042 - ret = qup_i2c_wait_for_complete(qup, msg); 1043 - if (ret) 1044 - goto err; 1045 - 1046 - ret = qup_i2c_read_fifo_v2(qup, msg); 1047 - if (ret) 1048 - goto err; 1049 - 1050 - qup->blk.pos++; 1051 - 1052 - /* Handle SMBus block read length */ 1053 - if (qup_i2c_check_msg_len(msg) && (msg->len == 1)) { 1054 - if (msg->buf[0] > I2C_SMBUS_BLOCK_MAX) { 1055 - ret = -EPROTO; 1056 - goto err; 1057 - } 1058 - msg->len += msg->buf[0]; 1059 - qup->pos = 0; 1060 - qup_i2c_set_blk_data(qup, msg); 1061 - /* set tag length for block read */ 1062 - qup->blk.tx_tag_len = 2; 1063 - qup_i2c_set_read_mode_v2(qup, msg->buf[0]); 1064 - } 1065 - } while (qup->blk.pos < qup->blk.count); 1066 - 1067 - err: 1068 - disable_irq(qup->irq); 1069 - qup->msg = NULL; 1070 - 1071 - return ret; 1072 - } 1073 - 1074 - static int qup_i2c_read_one(struct qup_i2c_dev *qup, struct i2c_msg *msg) 1075 - { 1110 + struct qup_i2c_block *blk = &qup->blk; 1076 1111 int ret; 1077 1112 1078 - qup->msg = msg; 1079 - qup->pos = 0; 1080 - 1081 - enable_irq(qup->irq); 1082 - qup_i2c_set_read_mode(qup, msg->len); 1083 - 1113 + qup_i2c_clear_blk_v1(blk); 1114 + qup_i2c_conf_v1(qup); 1084 1115 ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 1085 1116 if (ret) 1086 - goto err; 1117 + return ret; 1087 1118 1088 1119 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL); 1089 1120 1090 1121 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE); 1091 1122 if (ret) 1092 - goto err; 1123 + return ret; 1093 1124 1094 - qup_i2c_issue_read(qup, msg); 1125 + reinit_completion(&qup->xfer); 1126 + enable_irq(qup->irq); 1127 + if (!blk->is_tx_blk_mode) { 1128 + blk->tx_fifo_free = qup->out_fifo_sz; 1129 + 1130 + if (is_rx) 1131 + qup_i2c_write_rx_tags_v1(qup); 1132 + else 1133 + qup_i2c_write_tx_fifo_v1(qup); 1134 + } 1095 1135 1096 1136 ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 1097 1137 if (ret) 1098 1138 goto err; 1099 1139 1100 - do { 1101 - ret = qup_i2c_wait_for_complete(qup, msg); 1102 - if (ret) 1103 - goto err; 1140 + ret = qup_i2c_wait_for_complete(qup, qup->msg); 1141 + if (ret) 1142 + goto err; 1104 1143 1105 - ret = qup_i2c_read_fifo(qup, msg); 1106 - if (ret) 1107 - goto err; 1108 - } while (qup->pos < msg->len); 1144 + ret = qup_i2c_bus_active(qup, ONE_BYTE); 1109 1145 1110 1146 err: 1111 1147 disable_irq(qup->irq); 1112 - qup->msg = NULL; 1113 - 1114 1148 return ret; 1149 + } 1150 + 1151 + static int qup_i2c_write_one(struct qup_i2c_dev *qup) 1152 + { 1153 + struct i2c_msg *msg = qup->msg; 1154 + struct qup_i2c_block *blk = &qup->blk; 1155 + 1156 + qup->pos = 0; 1157 + blk->total_tx_len = msg->len + 1; 1158 + blk->total_rx_len = 0; 1159 + 1160 + return qup_i2c_conf_xfer_v1(qup, false); 1161 + } 1162 + 1163 + static int qup_i2c_read_one(struct qup_i2c_dev *qup) 1164 + { 1165 + struct qup_i2c_block *blk = &qup->blk; 1166 + 1167 + qup->pos = 0; 1168 + blk->total_tx_len = 2; 1169 + blk->total_rx_len = qup->msg->len; 1170 + 1171 + return qup_i2c_conf_xfer_v1(qup, true); 1115 1172 } 1116 1173 1117 1174 static int qup_i2c_xfer(struct i2c_adapter *adap, ··· 1094 1267 goto out; 1095 1268 } 1096 1269 1270 + qup->msg = &msgs[idx]; 1097 1271 if (msgs[idx].flags & I2C_M_RD) 1098 - ret = qup_i2c_read_one(qup, &msgs[idx]); 1272 + ret = qup_i2c_read_one(qup); 1099 1273 else 1100 - ret = qup_i2c_write_one(qup, &msgs[idx]); 1274 + ret = qup_i2c_write_one(qup); 1101 1275 1102 1276 if (ret) 1103 1277 break; ··· 1118 1290 return ret; 1119 1291 } 1120 1292 1293 + /* 1294 + * Configure registers related with reconfiguration during run and call it 1295 + * before each i2c sub transfer. 1296 + */ 1297 + static void qup_i2c_conf_count_v2(struct qup_i2c_dev *qup) 1298 + { 1299 + struct qup_i2c_block *blk = &qup->blk; 1300 + u32 qup_config = I2C_MINI_CORE | I2C_N_VAL_V2; 1301 + 1302 + if (blk->is_tx_blk_mode) 1303 + writel(qup->config_run | blk->total_tx_len, 1304 + qup->base + QUP_MX_OUTPUT_CNT); 1305 + else 1306 + writel(qup->config_run | blk->total_tx_len, 1307 + qup->base + QUP_MX_WRITE_CNT); 1308 + 1309 + if (blk->total_rx_len) { 1310 + if (blk->is_rx_blk_mode) 1311 + writel(qup->config_run | blk->total_rx_len, 1312 + qup->base + QUP_MX_INPUT_CNT); 1313 + else 1314 + writel(qup->config_run | blk->total_rx_len, 1315 + qup->base + QUP_MX_READ_CNT); 1316 + } else { 1317 + qup_config |= QUP_NO_INPUT; 1318 + } 1319 + 1320 + writel(qup_config, qup->base + QUP_CONFIG); 1321 + } 1322 + 1323 + /* 1324 + * Configure registers related with transfer mode (FIFO/Block) 1325 + * before starting of i2c transfer. It will be called only once in 1326 + * QUP RESET state. 1327 + */ 1328 + static void qup_i2c_conf_mode_v2(struct qup_i2c_dev *qup) 1329 + { 1330 + struct qup_i2c_block *blk = &qup->blk; 1331 + u32 io_mode = QUP_REPACK_EN; 1332 + 1333 + if (blk->is_tx_blk_mode) { 1334 + io_mode |= QUP_OUTPUT_BLK_MODE; 1335 + writel(0, qup->base + QUP_MX_WRITE_CNT); 1336 + } else { 1337 + writel(0, qup->base + QUP_MX_OUTPUT_CNT); 1338 + } 1339 + 1340 + if (blk->is_rx_blk_mode) { 1341 + io_mode |= QUP_INPUT_BLK_MODE; 1342 + writel(0, qup->base + QUP_MX_READ_CNT); 1343 + } else { 1344 + writel(0, qup->base + QUP_MX_INPUT_CNT); 1345 + } 1346 + 1347 + writel(io_mode, qup->base + QUP_IO_MODE); 1348 + } 1349 + 1350 + /* Clear required variables before starting of any QUP v2 sub transfer. */ 1351 + static void qup_i2c_clear_blk_v2(struct qup_i2c_block *blk) 1352 + { 1353 + blk->send_last_word = false; 1354 + blk->tx_tags_sent = false; 1355 + blk->tx_fifo_data = 0; 1356 + blk->tx_fifo_data_pos = 0; 1357 + blk->tx_fifo_free = 0; 1358 + 1359 + blk->rx_tags_fetched = false; 1360 + blk->rx_bytes_read = false; 1361 + blk->rx_fifo_data = 0; 1362 + blk->rx_fifo_data_pos = 0; 1363 + blk->fifo_available = 0; 1364 + } 1365 + 1366 + /* Receive data from RX FIFO for read message in QUP v2 i2c transfer. */ 1367 + static void qup_i2c_recv_data(struct qup_i2c_dev *qup) 1368 + { 1369 + struct qup_i2c_block *blk = &qup->blk; 1370 + int j; 1371 + 1372 + for (j = blk->rx_fifo_data_pos; 1373 + blk->cur_blk_len && blk->fifo_available; 1374 + blk->cur_blk_len--, blk->fifo_available--) { 1375 + if (j == 0) 1376 + blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE); 1377 + 1378 + *(blk->cur_data++) = blk->rx_fifo_data; 1379 + blk->rx_fifo_data >>= 8; 1380 + 1381 + if (j == 3) 1382 + j = 0; 1383 + else 1384 + j++; 1385 + } 1386 + 1387 + blk->rx_fifo_data_pos = j; 1388 + } 1389 + 1390 + /* Receive tags for read message in QUP v2 i2c transfer. */ 1391 + static void qup_i2c_recv_tags(struct qup_i2c_dev *qup) 1392 + { 1393 + struct qup_i2c_block *blk = &qup->blk; 1394 + 1395 + blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE); 1396 + blk->rx_fifo_data >>= blk->rx_tag_len * 8; 1397 + blk->rx_fifo_data_pos = blk->rx_tag_len; 1398 + blk->fifo_available -= blk->rx_tag_len; 1399 + } 1400 + 1401 + /* 1402 + * Read the data and tags from RX FIFO. Since in read case, the tags will be 1403 + * preceded by received data bytes so 1404 + * 1. Check if rx_tags_fetched is false i.e. the start of QUP block so receive 1405 + * all tag bytes and discard that. 1406 + * 2. Read the data from RX FIFO. When all the data bytes have been read then 1407 + * set rx_bytes_read to true. 1408 + */ 1409 + static void qup_i2c_read_rx_fifo_v2(struct qup_i2c_dev *qup) 1410 + { 1411 + struct qup_i2c_block *blk = &qup->blk; 1412 + 1413 + if (!blk->rx_tags_fetched) { 1414 + qup_i2c_recv_tags(qup); 1415 + blk->rx_tags_fetched = true; 1416 + } 1417 + 1418 + qup_i2c_recv_data(qup); 1419 + if (!blk->cur_blk_len) 1420 + blk->rx_bytes_read = true; 1421 + } 1422 + 1423 + /* 1424 + * Write bytes in TX FIFO for write message in QUP v2 i2c transfer. QUP TX FIFO 1425 + * write works on word basis (4 bytes). Append new data byte write for TX FIFO 1426 + * in tx_fifo_data and write to TX FIFO when all the 4 bytes are present. 1427 + */ 1428 + static void 1429 + qup_i2c_write_blk_data(struct qup_i2c_dev *qup, u8 **data, unsigned int *len) 1430 + { 1431 + struct qup_i2c_block *blk = &qup->blk; 1432 + unsigned int j; 1433 + 1434 + for (j = blk->tx_fifo_data_pos; *len && blk->tx_fifo_free; 1435 + (*len)--, blk->tx_fifo_free--) { 1436 + blk->tx_fifo_data |= *(*data)++ << (j * 8); 1437 + if (j == 3) { 1438 + writel(blk->tx_fifo_data, 1439 + qup->base + QUP_OUT_FIFO_BASE); 1440 + blk->tx_fifo_data = 0x0; 1441 + j = 0; 1442 + } else { 1443 + j++; 1444 + } 1445 + } 1446 + 1447 + blk->tx_fifo_data_pos = j; 1448 + } 1449 + 1450 + /* Transfer tags for read message in QUP v2 i2c transfer. */ 1451 + static void qup_i2c_write_rx_tags_v2(struct qup_i2c_dev *qup) 1452 + { 1453 + struct qup_i2c_block *blk = &qup->blk; 1454 + 1455 + qup_i2c_write_blk_data(qup, &blk->cur_tx_tags, &blk->tx_tag_len); 1456 + if (blk->tx_fifo_data_pos) 1457 + writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE); 1458 + } 1459 + 1460 + /* 1461 + * Write the data and tags in TX FIFO. Since in write case, both tags and data 1462 + * need to be written and QUP write tags can have maximum 256 data length, so 1463 + * 1464 + * 1. Check if tx_tags_sent is false i.e. the start of QUP block so write the 1465 + * tags to TX FIFO and set tx_tags_sent to true. 1466 + * 2. Check if send_last_word is true. It will be set when last few data bytes 1467 + * (less than 4 bytes) are reamining to be written in FIFO because of no FIFO 1468 + * space. All this data bytes are available in tx_fifo_data so write this 1469 + * in FIFO. 1470 + * 3. Write the data to TX FIFO and check for cur_blk_len. If it is non zero 1471 + * then more data is pending otherwise following 3 cases can be possible 1472 + * a. if tx_fifo_data_pos is zero i.e. all the data bytes in this block 1473 + * have been written in TX FIFO so nothing else is required. 1474 + * b. tx_fifo_free is non zero i.e tx FIFO is free so copy the remaining data 1475 + * from tx_fifo_data to tx FIFO. Since, qup_i2c_write_blk_data do write 1476 + * in 4 bytes and FIFO space is in multiple of 4 bytes so tx_fifo_free 1477 + * will be always greater than or equal to 4 bytes. 1478 + * c. tx_fifo_free is zero. In this case, last few bytes (less than 4 1479 + * bytes) are copied to tx_fifo_data but couldn't be sent because of 1480 + * FIFO full so make send_last_word true. 1481 + */ 1482 + static void qup_i2c_write_tx_fifo_v2(struct qup_i2c_dev *qup) 1483 + { 1484 + struct qup_i2c_block *blk = &qup->blk; 1485 + 1486 + if (!blk->tx_tags_sent) { 1487 + qup_i2c_write_blk_data(qup, &blk->cur_tx_tags, 1488 + &blk->tx_tag_len); 1489 + blk->tx_tags_sent = true; 1490 + } 1491 + 1492 + if (blk->send_last_word) 1493 + goto send_last_word; 1494 + 1495 + qup_i2c_write_blk_data(qup, &blk->cur_data, &blk->cur_blk_len); 1496 + if (!blk->cur_blk_len) { 1497 + if (!blk->tx_fifo_data_pos) 1498 + return; 1499 + 1500 + if (blk->tx_fifo_free) 1501 + goto send_last_word; 1502 + 1503 + blk->send_last_word = true; 1504 + } 1505 + 1506 + return; 1507 + 1508 + send_last_word: 1509 + writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE); 1510 + } 1511 + 1512 + /* 1513 + * Main transfer function which read or write i2c data. 1514 + * The QUP v2 supports reconfiguration during run in which multiple i2c sub 1515 + * transfers can be scheduled. 1516 + */ 1517 + static int 1518 + qup_i2c_conf_xfer_v2(struct qup_i2c_dev *qup, bool is_rx, bool is_first, 1519 + bool change_pause_state) 1520 + { 1521 + struct qup_i2c_block *blk = &qup->blk; 1522 + struct i2c_msg *msg = qup->msg; 1523 + int ret; 1524 + 1525 + /* 1526 + * Check if its SMBus Block read for which the top level read will be 1527 + * done into 2 QUP reads. One with message length 1 while other one is 1528 + * with actual length. 1529 + */ 1530 + if (qup_i2c_check_msg_len(msg)) { 1531 + if (qup->is_smbus_read) { 1532 + /* 1533 + * If the message length is already read in 1534 + * the first byte of the buffer, account for 1535 + * that by setting the offset 1536 + */ 1537 + blk->cur_data += 1; 1538 + is_first = false; 1539 + } else { 1540 + change_pause_state = false; 1541 + } 1542 + } 1543 + 1544 + qup->config_run = is_first ? 0 : QUP_I2C_MX_CONFIG_DURING_RUN; 1545 + 1546 + qup_i2c_clear_blk_v2(blk); 1547 + qup_i2c_conf_count_v2(qup); 1548 + 1549 + /* If it is first sub transfer, then configure i2c bus clocks */ 1550 + if (is_first) { 1551 + ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 1552 + if (ret) 1553 + return ret; 1554 + 1555 + writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL); 1556 + 1557 + ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE); 1558 + if (ret) 1559 + return ret; 1560 + } 1561 + 1562 + reinit_completion(&qup->xfer); 1563 + enable_irq(qup->irq); 1564 + /* 1565 + * In FIFO mode, tx FIFO can be written directly while in block mode the 1566 + * it will be written after getting OUT_BLOCK_WRITE_REQ interrupt 1567 + */ 1568 + if (!blk->is_tx_blk_mode) { 1569 + blk->tx_fifo_free = qup->out_fifo_sz; 1570 + 1571 + if (is_rx) 1572 + qup_i2c_write_rx_tags_v2(qup); 1573 + else 1574 + qup_i2c_write_tx_fifo_v2(qup); 1575 + } 1576 + 1577 + ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 1578 + if (ret) 1579 + goto err; 1580 + 1581 + ret = qup_i2c_wait_for_complete(qup, msg); 1582 + if (ret) 1583 + goto err; 1584 + 1585 + /* Move to pause state for all the transfers, except last one */ 1586 + if (change_pause_state) { 1587 + ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE); 1588 + if (ret) 1589 + goto err; 1590 + } 1591 + 1592 + err: 1593 + disable_irq(qup->irq); 1594 + return ret; 1595 + } 1596 + 1597 + /* 1598 + * Transfer one read/write message in i2c transfer. It splits the message into 1599 + * multiple of blk_xfer_limit data length blocks and schedule each 1600 + * QUP block individually. 1601 + */ 1602 + static int qup_i2c_xfer_v2_msg(struct qup_i2c_dev *qup, int msg_id, bool is_rx) 1603 + { 1604 + int ret = 0; 1605 + unsigned int data_len, i; 1606 + struct i2c_msg *msg = qup->msg; 1607 + struct qup_i2c_block *blk = &qup->blk; 1608 + u8 *msg_buf = msg->buf; 1609 + 1610 + qup->blk_xfer_limit = is_rx ? RECV_MAX_DATA_LEN : QUP_READ_LIMIT; 1611 + qup_i2c_set_blk_data(qup, msg); 1612 + 1613 + for (i = 0; i < blk->count; i++) { 1614 + data_len = qup_i2c_get_data_len(qup); 1615 + blk->pos = i; 1616 + blk->cur_tx_tags = blk->tags; 1617 + blk->cur_blk_len = data_len; 1618 + blk->tx_tag_len = 1619 + qup_i2c_set_tags(blk->cur_tx_tags, qup, qup->msg); 1620 + 1621 + blk->cur_data = msg_buf; 1622 + 1623 + if (is_rx) { 1624 + blk->total_tx_len = blk->tx_tag_len; 1625 + blk->rx_tag_len = 2; 1626 + blk->total_rx_len = blk->rx_tag_len + data_len; 1627 + } else { 1628 + blk->total_tx_len = blk->tx_tag_len + data_len; 1629 + blk->total_rx_len = 0; 1630 + } 1631 + 1632 + ret = qup_i2c_conf_xfer_v2(qup, is_rx, !msg_id && !i, 1633 + !qup->is_last || i < blk->count - 1); 1634 + if (ret) 1635 + return ret; 1636 + 1637 + /* Handle SMBus block read length */ 1638 + if (qup_i2c_check_msg_len(msg) && msg->len == 1 && 1639 + !qup->is_smbus_read) { 1640 + if (msg->buf[0] > I2C_SMBUS_BLOCK_MAX) 1641 + return -EPROTO; 1642 + 1643 + msg->len = msg->buf[0]; 1644 + qup->is_smbus_read = true; 1645 + ret = qup_i2c_xfer_v2_msg(qup, msg_id, true); 1646 + qup->is_smbus_read = false; 1647 + if (ret) 1648 + return ret; 1649 + 1650 + msg->len += 1; 1651 + } 1652 + 1653 + msg_buf += data_len; 1654 + blk->data_len -= qup->blk_xfer_limit; 1655 + } 1656 + 1657 + return ret; 1658 + } 1659 + 1660 + /* 1661 + * QUP v2 supports 3 modes 1662 + * Programmed IO using FIFO mode : Less than FIFO size 1663 + * Programmed IO using Block mode : Greater than FIFO size 1664 + * DMA using BAM : Appropriate for any transaction size but the address should 1665 + * be DMA applicable 1666 + * 1667 + * This function determines the mode which will be used for this transfer. An 1668 + * i2c transfer contains multiple message. Following are the rules to determine 1669 + * the mode used. 1670 + * 1. Determine complete length, maximum tx and rx length for complete transfer. 1671 + * 2. If complete transfer length is greater than fifo size then use the DMA 1672 + * mode. 1673 + * 3. In FIFO or block mode, tx and rx can operate in different mode so check 1674 + * for maximum tx and rx length to determine mode. 1675 + */ 1676 + static int 1677 + qup_i2c_determine_mode_v2(struct qup_i2c_dev *qup, 1678 + struct i2c_msg msgs[], int num) 1679 + { 1680 + int idx; 1681 + bool no_dma = false; 1682 + unsigned int max_tx_len = 0, max_rx_len = 0, total_len = 0; 1683 + 1684 + /* All i2c_msgs should be transferred using either dma or cpu */ 1685 + for (idx = 0; idx < num; idx++) { 1686 + if (msgs[idx].len == 0) 1687 + return -EINVAL; 1688 + 1689 + if (msgs[idx].flags & I2C_M_RD) 1690 + max_rx_len = max_t(unsigned int, max_rx_len, 1691 + msgs[idx].len); 1692 + else 1693 + max_tx_len = max_t(unsigned int, max_tx_len, 1694 + msgs[idx].len); 1695 + 1696 + if (is_vmalloc_addr(msgs[idx].buf)) 1697 + no_dma = true; 1698 + 1699 + total_len += msgs[idx].len; 1700 + } 1701 + 1702 + if (!no_dma && qup->is_dma && 1703 + (total_len > qup->out_fifo_sz || total_len > qup->in_fifo_sz)) { 1704 + qup->use_dma = true; 1705 + } else { 1706 + qup->blk.is_tx_blk_mode = max_tx_len > qup->out_fifo_sz - 1707 + QUP_MAX_TAGS_LEN ? true : false; 1708 + qup->blk.is_rx_blk_mode = max_rx_len > qup->in_fifo_sz - 1709 + READ_RX_TAGS_LEN ? true : false; 1710 + } 1711 + 1712 + return 0; 1713 + } 1714 + 1121 1715 static int qup_i2c_xfer_v2(struct i2c_adapter *adap, 1122 1716 struct i2c_msg msgs[], 1123 1717 int num) 1124 1718 { 1125 1719 struct qup_i2c_dev *qup = i2c_get_adapdata(adap); 1126 - int ret, len, idx = 0, use_dma = 0; 1720 + int ret, idx = 0; 1127 1721 1128 1722 qup->bus_err = 0; 1129 1723 qup->qup_err = 0; 1130 1724 1131 1725 ret = pm_runtime_get_sync(qup->dev); 1132 1726 if (ret < 0) 1727 + goto out; 1728 + 1729 + ret = qup_i2c_determine_mode_v2(qup, msgs, num); 1730 + if (ret) 1133 1731 goto out; 1134 1732 1135 1733 writel(1, qup->base + QUP_SW_RESET); ··· 1567 1313 writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG); 1568 1314 writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN); 1569 1315 1570 - if ((qup->is_dma)) { 1571 - /* All i2c_msgs should be transferred using either dma or cpu */ 1572 - for (idx = 0; idx < num; idx++) { 1573 - if (msgs[idx].len == 0) { 1574 - ret = -EINVAL; 1575 - goto out; 1576 - } 1577 - 1578 - len = (msgs[idx].len > qup->out_fifo_sz) || 1579 - (msgs[idx].len > qup->in_fifo_sz); 1580 - 1581 - if ((!is_vmalloc_addr(msgs[idx].buf)) && len) { 1582 - use_dma = 1; 1583 - } else { 1584 - use_dma = 0; 1585 - break; 1586 - } 1587 - } 1316 + if (qup_i2c_poll_state_i2c_master(qup)) { 1317 + ret = -EIO; 1318 + goto out; 1588 1319 } 1589 1320 1590 - idx = 0; 1591 - 1592 - do { 1593 - if (msgs[idx].len == 0) { 1594 - ret = -EINVAL; 1595 - goto out; 1596 - } 1597 - 1598 - if (qup_i2c_poll_state_i2c_master(qup)) { 1599 - ret = -EIO; 1600 - goto out; 1601 - } 1602 - 1603 - qup->is_last = (idx == (num - 1)); 1604 - if (idx) 1605 - qup->config_run = QUP_I2C_MX_CONFIG_DURING_RUN; 1606 - else 1607 - qup->config_run = 0; 1608 - 1321 + if (qup->use_dma) { 1609 1322 reinit_completion(&qup->xfer); 1323 + ret = qup_i2c_bam_xfer(adap, &msgs[0], num); 1324 + qup->use_dma = false; 1325 + } else { 1326 + qup_i2c_conf_mode_v2(qup); 1610 1327 1611 - if (use_dma) { 1612 - ret = qup_i2c_bam_xfer(adap, &msgs[idx], num); 1613 - } else { 1614 - if (msgs[idx].flags & I2C_M_RD) 1615 - ret = qup_i2c_read_one_v2(qup, &msgs[idx]); 1616 - else 1617 - ret = qup_i2c_write_one_v2(qup, &msgs[idx]); 1328 + for (idx = 0; idx < num; idx++) { 1329 + qup->msg = &msgs[idx]; 1330 + qup->is_last = idx == (num - 1); 1331 + 1332 + ret = qup_i2c_xfer_v2_msg(qup, idx, 1333 + !!(msgs[idx].flags & I2C_M_RD)); 1334 + if (ret) 1335 + break; 1618 1336 } 1619 - } while ((idx++ < (num - 1)) && !use_dma && !ret); 1337 + qup->msg = NULL; 1338 + } 1620 1339 1621 1340 if (!ret) 1622 - ret = qup_i2c_change_state(qup, QUP_RESET_STATE); 1341 + ret = qup_i2c_bus_active(qup, ONE_BYTE); 1342 + 1343 + if (!ret) 1344 + qup_i2c_change_state(qup, QUP_RESET_STATE); 1623 1345 1624 1346 if (ret == 0) 1625 1347 ret = num; ··· 1659 1429 u32 src_clk_freq = DEFAULT_SRC_CLK; 1660 1430 u32 clk_freq = DEFAULT_CLK_FREQ; 1661 1431 int blocks; 1432 + bool is_qup_v1; 1662 1433 1663 1434 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL); 1664 1435 if (!qup) ··· 1678 1447 if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) { 1679 1448 qup->adap.algo = &qup_i2c_algo; 1680 1449 qup->adap.quirks = &qup_i2c_quirks; 1450 + is_qup_v1 = true; 1681 1451 } else { 1682 1452 qup->adap.algo = &qup_i2c_algo_v2; 1453 + is_qup_v1 = false; 1683 1454 ret = qup_i2c_req_dma(qup); 1684 1455 1685 1456 if (ret == -EPROBE_DEFER) ··· 1689 1456 else if (ret != 0) 1690 1457 goto nodma; 1691 1458 1692 - blocks = (MX_BLOCKS << 1) + 1; 1459 + qup->max_xfer_sg_len = (MX_BLOCKS << 1); 1460 + blocks = (MX_DMA_BLOCKS << 1) + 1; 1693 1461 qup->btx.sg = devm_kzalloc(&pdev->dev, 1694 1462 sizeof(*qup->btx.sg) * blocks, 1695 1463 GFP_KERNEL); ··· 1807 1573 ret = -EIO; 1808 1574 goto fail; 1809 1575 } 1810 - qup->out_blk_sz = blk_sizes[size] / 2; 1576 + qup->out_blk_sz = blk_sizes[size]; 1811 1577 1812 1578 size = QUP_INPUT_BLOCK_SIZE(io_mode); 1813 1579 if (size >= ARRAY_SIZE(blk_sizes)) { 1814 1580 ret = -EIO; 1815 1581 goto fail; 1816 1582 } 1817 - qup->in_blk_sz = blk_sizes[size] / 2; 1583 + qup->in_blk_sz = blk_sizes[size]; 1584 + 1585 + if (is_qup_v1) { 1586 + /* 1587 + * in QUP v1, QUP_CONFIG uses N as 15 i.e 16 bits constitutes a 1588 + * single transfer but the block size is in bytes so divide the 1589 + * in_blk_sz and out_blk_sz by 2 1590 + */ 1591 + qup->in_blk_sz /= 2; 1592 + qup->out_blk_sz /= 2; 1593 + qup->write_tx_fifo = qup_i2c_write_tx_fifo_v1; 1594 + qup->read_rx_fifo = qup_i2c_read_rx_fifo_v1; 1595 + qup->write_rx_tags = qup_i2c_write_rx_tags_v1; 1596 + } else { 1597 + qup->write_tx_fifo = qup_i2c_write_tx_fifo_v2; 1598 + qup->read_rx_fifo = qup_i2c_read_rx_fifo_v2; 1599 + qup->write_rx_tags = qup_i2c_write_rx_tags_v2; 1600 + } 1818 1601 1819 1602 size = QUP_OUTPUT_FIFO_SIZE(io_mode); 1820 1603 qup->out_fifo_sz = qup->out_blk_sz * (2 << size); ··· 1849 1598 */ 1850 1599 one_bit_t = (USEC_PER_SEC / clk_freq) + 1; 1851 1600 qup->one_byte_t = one_bit_t * 9; 1601 + qup->xfer_timeout = TOUT_MIN * HZ + 1602 + usecs_to_jiffies(MX_DMA_TX_RX_LEN * qup->one_byte_t); 1852 1603 1853 1604 dev_dbg(qup->dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n", 1854 1605 qup->in_blk_sz, qup->in_fifo_sz,
+2 -2
drivers/i2c/busses/i2c-rcar.c
··· 102 102 #define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR) 103 103 #define RCAR_IRQ_STOP (MST) 104 104 105 - #define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF) 106 - #define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF) 105 + #define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0x7F) 106 + #define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0x7F) 107 107 108 108 #define ID_LAST_MSG (1 << 0) 109 109 #define ID_FIRST_MSG (1 << 1)
+19 -16
drivers/i2c/busses/i2c-scmi.c
··· 182 182 status = acpi_evaluate_object(smbus_cmi->handle, method, &input, 183 183 &buffer); 184 184 if (ACPI_FAILURE(status)) { 185 - ACPI_ERROR((AE_INFO, "Evaluating %s: %i", method, status)); 185 + acpi_handle_err(smbus_cmi->handle, 186 + "Failed to evaluate %s: %i\n", method, status); 186 187 return -EIO; 187 188 } 188 189 ··· 191 190 if (pkg && pkg->type == ACPI_TYPE_PACKAGE) 192 191 obj = pkg->package.elements; 193 192 else { 194 - ACPI_ERROR((AE_INFO, "Invalid argument type")); 193 + acpi_handle_err(smbus_cmi->handle, "Invalid argument type\n"); 195 194 result = -EIO; 196 195 goto out; 197 196 } 198 197 if (obj == NULL || obj->type != ACPI_TYPE_INTEGER) { 199 - ACPI_ERROR((AE_INFO, "Invalid argument type")); 198 + acpi_handle_err(smbus_cmi->handle, "Invalid argument type\n"); 200 199 result = -EIO; 201 200 goto out; 202 201 } 203 202 204 203 result = obj->integer.value; 205 - ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s return status: %i\n", 206 - method, result)); 204 + acpi_handle_debug(smbus_cmi->handle, "%s return status: %i\n", method, 205 + result); 207 206 208 207 switch (result) { 209 208 case ACPI_SMBUS_STATUS_OK: ··· 228 227 229 228 obj = pkg->package.elements + 1; 230 229 if (obj->type != ACPI_TYPE_INTEGER) { 231 - ACPI_ERROR((AE_INFO, "Invalid argument type")); 230 + acpi_handle_err(smbus_cmi->handle, "Invalid argument type\n"); 232 231 result = -EIO; 233 232 goto out; 234 233 } ··· 240 239 case I2C_SMBUS_BYTE_DATA: 241 240 case I2C_SMBUS_WORD_DATA: 242 241 if (obj->type != ACPI_TYPE_INTEGER) { 243 - ACPI_ERROR((AE_INFO, "Invalid argument type")); 242 + acpi_handle_err(smbus_cmi->handle, 243 + "Invalid argument type\n"); 244 244 result = -EIO; 245 245 goto out; 246 246 } ··· 252 250 break; 253 251 case I2C_SMBUS_BLOCK_DATA: 254 252 if (obj->type != ACPI_TYPE_BUFFER) { 255 - ACPI_ERROR((AE_INFO, "Invalid argument type")); 253 + acpi_handle_err(smbus_cmi->handle, 254 + "Invalid argument type\n"); 256 255 result = -EIO; 257 256 goto out; 258 257 } ··· 303 300 const char *name) 304 301 { 305 302 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 303 + struct acpi_handle *handle = smbus_cmi->handle; 306 304 union acpi_object *obj; 307 305 acpi_status status; 308 306 ··· 312 308 smbus_cmi->methods->mt_info, 313 309 NULL, &buffer); 314 310 if (ACPI_FAILURE(status)) { 315 - ACPI_ERROR((AE_INFO, "Evaluating %s: %i", 316 - smbus_cmi->methods->mt_info, status)); 311 + acpi_handle_err(handle, "Failed to evaluate %s: %i\n", 312 + smbus_cmi->methods->mt_info, status); 317 313 return -EIO; 318 314 } 319 315 ··· 321 317 if (obj && obj->type == ACPI_TYPE_PACKAGE) 322 318 obj = obj->package.elements; 323 319 else { 324 - ACPI_ERROR((AE_INFO, "Invalid argument type")); 320 + acpi_handle_err(handle, "Invalid argument type\n"); 325 321 kfree(buffer.pointer); 326 322 return -EIO; 327 323 } 328 324 329 325 if (obj->type != ACPI_TYPE_INTEGER) { 330 - ACPI_ERROR((AE_INFO, "Invalid argument type")); 326 + acpi_handle_err(handle, "Invalid argument type\n"); 331 327 kfree(buffer.pointer); 332 328 return -EIO; 333 329 } else 334 - ACPI_DEBUG_PRINT((ACPI_DB_INFO, "SMBus CMI Version %x" 335 - "\n", (int)obj->integer.value)); 330 + acpi_handle_debug(handle, "SMBus CMI Version %x\n", 331 + (int)obj->integer.value); 336 332 337 333 kfree(buffer.pointer); 338 334 smbus_cmi->cap_info = 1; ··· 341 337 else if (!strcmp(name, smbus_cmi->methods->mt_sbw)) 342 338 smbus_cmi->cap_write = 1; 343 339 else 344 - ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unsupported CMI method: %s\n", 345 - name)); 340 + acpi_handle_debug(handle, "Unsupported CMI method: %s\n", name); 346 341 347 342 return 0; 348 343 }
+1 -1
drivers/i2c/busses/i2c-stm32f4.c
··· 349 349 static void stm32f4_i2c_terminate_xfer(struct stm32f4_i2c_dev *i2c_dev) 350 350 { 351 351 struct stm32f4_i2c_msg *msg = &i2c_dev->msg; 352 - void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2; 352 + void __iomem *reg; 353 353 354 354 stm32f4_i2c_disable_irq(i2c_dev); 355 355
+667
drivers/i2c/busses/i2c-synquacer.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Copyright (C) 2012 FUJITSU SEMICONDUCTOR LIMITED 4 + */ 5 + 6 + #include <linux/acpi.h> 7 + #include <linux/clk.h> 8 + #include <linux/delay.h> 9 + #include <linux/device.h> 10 + #include <linux/err.h> 11 + #include <linux/errno.h> 12 + #include <linux/i2c.h> 13 + #include <linux/interrupt.h> 14 + #include <linux/io.h> 15 + #include <linux/kernel.h> 16 + #include <linux/module.h> 17 + #include <linux/platform_device.h> 18 + #include <linux/sched.h> 19 + #include <linux/slab.h> 20 + #include <linux/spinlock.h> 21 + 22 + #define WAIT_PCLK(n, rate) \ 23 + ndelay(DIV_ROUND_UP(DIV_ROUND_UP(1000000000, rate), n) + 10) 24 + 25 + /* I2C register address definitions */ 26 + #define SYNQUACER_I2C_REG_BSR (0x00 << 2) // Bus Status 27 + #define SYNQUACER_I2C_REG_BCR (0x01 << 2) // Bus Control 28 + #define SYNQUACER_I2C_REG_CCR (0x02 << 2) // Clock Control 29 + #define SYNQUACER_I2C_REG_ADR (0x03 << 2) // Address 30 + #define SYNQUACER_I2C_REG_DAR (0x04 << 2) // Data 31 + #define SYNQUACER_I2C_REG_CSR (0x05 << 2) // Expansion CS 32 + #define SYNQUACER_I2C_REG_FSR (0x06 << 2) // Bus Clock Freq 33 + #define SYNQUACER_I2C_REG_BC2R (0x07 << 2) // Bus Control 2 34 + 35 + /* I2C register bit definitions */ 36 + #define SYNQUACER_I2C_BSR_FBT BIT(0) // First Byte Transfer 37 + #define SYNQUACER_I2C_BSR_GCA BIT(1) // General Call Address 38 + #define SYNQUACER_I2C_BSR_AAS BIT(2) // Address as Slave 39 + #define SYNQUACER_I2C_BSR_TRX BIT(3) // Transfer/Receive 40 + #define SYNQUACER_I2C_BSR_LRB BIT(4) // Last Received Bit 41 + #define SYNQUACER_I2C_BSR_AL BIT(5) // Arbitration Lost 42 + #define SYNQUACER_I2C_BSR_RSC BIT(6) // Repeated Start Cond. 43 + #define SYNQUACER_I2C_BSR_BB BIT(7) // Bus Busy 44 + 45 + #define SYNQUACER_I2C_BCR_INT BIT(0) // Interrupt 46 + #define SYNQUACER_I2C_BCR_INTE BIT(1) // Interrupt Enable 47 + #define SYNQUACER_I2C_BCR_GCAA BIT(2) // Gen. Call Access Ack. 48 + #define SYNQUACER_I2C_BCR_ACK BIT(3) // Acknowledge 49 + #define SYNQUACER_I2C_BCR_MSS BIT(4) // Master Slave Select 50 + #define SYNQUACER_I2C_BCR_SCC BIT(5) // Start Condition Cont. 51 + #define SYNQUACER_I2C_BCR_BEIE BIT(6) // Bus Error Int Enable 52 + #define SYNQUACER_I2C_BCR_BER BIT(7) // Bus Error 53 + 54 + #define SYNQUACER_I2C_CCR_CS_MASK (0x1f) // CCR Clock Period Sel. 55 + #define SYNQUACER_I2C_CCR_EN BIT(5) // Enable 56 + #define SYNQUACER_I2C_CCR_FM BIT(6) // Speed Mode Select 57 + 58 + #define SYNQUACER_I2C_CSR_CS_MASK (0x3f) // CSR Clock Period Sel. 59 + 60 + #define SYNQUACER_I2C_BC2R_SCLL BIT(0) // SCL Low Drive 61 + #define SYNQUACER_I2C_BC2R_SDAL BIT(1) // SDA Low Drive 62 + #define SYNQUACER_I2C_BC2R_SCLS BIT(4) // SCL Status 63 + #define SYNQUACER_I2C_BC2R_SDAS BIT(5) // SDA Status 64 + 65 + /* PCLK frequency */ 66 + #define SYNQUACER_I2C_BUS_CLK_FR(rate) (((rate) / 20000000) + 1) 67 + 68 + /* STANDARD MODE frequency */ 69 + #define SYNQUACER_I2C_CLK_MASTER_STD(rate) \ 70 + DIV_ROUND_UP(DIV_ROUND_UP((rate), 100000) - 2, 2) 71 + /* FAST MODE frequency */ 72 + #define SYNQUACER_I2C_CLK_MASTER_FAST(rate) \ 73 + DIV_ROUND_UP((DIV_ROUND_UP((rate), 400000) - 2) * 2, 3) 74 + 75 + /* (clkrate <= 18000000) */ 76 + /* calculate the value of CS bits in CCR register on standard mode */ 77 + #define SYNQUACER_I2C_CCR_CS_STD_MAX_18M(rate) \ 78 + ((SYNQUACER_I2C_CLK_MASTER_STD(rate) - 65) \ 79 + & SYNQUACER_I2C_CCR_CS_MASK) 80 + 81 + /* calculate the value of CS bits in CSR register on standard mode */ 82 + #define SYNQUACER_I2C_CSR_CS_STD_MAX_18M(rate) 0x00 83 + 84 + /* calculate the value of CS bits in CCR register on fast mode */ 85 + #define SYNQUACER_I2C_CCR_CS_FAST_MAX_18M(rate) \ 86 + ((SYNQUACER_I2C_CLK_MASTER_FAST(rate) - 1) \ 87 + & SYNQUACER_I2C_CCR_CS_MASK) 88 + 89 + /* calculate the value of CS bits in CSR register on fast mode */ 90 + #define SYNQUACER_I2C_CSR_CS_FAST_MAX_18M(rate) 0x00 91 + 92 + /* (clkrate > 18000000) */ 93 + /* calculate the value of CS bits in CCR register on standard mode */ 94 + #define SYNQUACER_I2C_CCR_CS_STD_MIN_18M(rate) \ 95 + ((SYNQUACER_I2C_CLK_MASTER_STD(rate) - 1) \ 96 + & SYNQUACER_I2C_CCR_CS_MASK) 97 + 98 + /* calculate the value of CS bits in CSR register on standard mode */ 99 + #define SYNQUACER_I2C_CSR_CS_STD_MIN_18M(rate) \ 100 + (((SYNQUACER_I2C_CLK_MASTER_STD(rate) - 1) >> 5) \ 101 + & SYNQUACER_I2C_CSR_CS_MASK) 102 + 103 + /* calculate the value of CS bits in CCR register on fast mode */ 104 + #define SYNQUACER_I2C_CCR_CS_FAST_MIN_18M(rate) \ 105 + ((SYNQUACER_I2C_CLK_MASTER_FAST(rate) - 1) \ 106 + & SYNQUACER_I2C_CCR_CS_MASK) 107 + 108 + /* calculate the value of CS bits in CSR register on fast mode */ 109 + #define SYNQUACER_I2C_CSR_CS_FAST_MIN_18M(rate) \ 110 + (((SYNQUACER_I2C_CLK_MASTER_FAST(rate) - 1) >> 5) \ 111 + & SYNQUACER_I2C_CSR_CS_MASK) 112 + 113 + /* min I2C clock frequency 14M */ 114 + #define SYNQUACER_I2C_MIN_CLK_RATE (14 * 1000000) 115 + /* max I2C clock frequency 200M */ 116 + #define SYNQUACER_I2C_MAX_CLK_RATE (200 * 1000000) 117 + /* I2C clock frequency 18M */ 118 + #define SYNQUACER_I2C_CLK_RATE_18M (18 * 1000000) 119 + 120 + #define SYNQUACER_I2C_SPEED_FM 400 // Fast Mode 121 + #define SYNQUACER_I2C_SPEED_SM 100 // Standard Mode 122 + 123 + enum i2c_state { 124 + STATE_IDLE, 125 + STATE_START, 126 + STATE_READ, 127 + STATE_WRITE 128 + }; 129 + 130 + struct synquacer_i2c { 131 + struct completion completion; 132 + 133 + struct i2c_msg *msg; 134 + u32 msg_num; 135 + u32 msg_idx; 136 + u32 msg_ptr; 137 + 138 + int irq; 139 + struct device *dev; 140 + void __iomem *base; 141 + struct clk *pclk; 142 + u32 pclkrate; 143 + u32 speed_khz; 144 + u32 timeout_ms; 145 + enum i2c_state state; 146 + struct i2c_adapter adapter; 147 + 148 + bool is_suspended; 149 + }; 150 + 151 + static inline int is_lastmsg(struct synquacer_i2c *i2c) 152 + { 153 + return i2c->msg_idx >= (i2c->msg_num - 1); 154 + } 155 + 156 + static inline int is_msglast(struct synquacer_i2c *i2c) 157 + { 158 + return i2c->msg_ptr == (i2c->msg->len - 1); 159 + } 160 + 161 + static inline int is_msgend(struct synquacer_i2c *i2c) 162 + { 163 + return i2c->msg_ptr >= i2c->msg->len; 164 + } 165 + 166 + static inline unsigned long calc_timeout_ms(struct synquacer_i2c *i2c, 167 + struct i2c_msg *msgs, 168 + int num) 169 + { 170 + unsigned long bit_count = 0; 171 + int i; 172 + 173 + for (i = 0; i < num; i++, msgs++) 174 + bit_count += msgs->len; 175 + 176 + return DIV_ROUND_UP((bit_count * 9 + num * 10) * 3, 200) + 10; 177 + } 178 + 179 + static void synquacer_i2c_stop(struct synquacer_i2c *i2c, int ret) 180 + { 181 + /* 182 + * clear IRQ (INT=0, BER=0) 183 + * set Stop Condition (MSS=0) 184 + * Interrupt Disable 185 + */ 186 + writeb(0, i2c->base + SYNQUACER_I2C_REG_BCR); 187 + 188 + i2c->state = STATE_IDLE; 189 + 190 + i2c->msg_ptr = 0; 191 + i2c->msg = NULL; 192 + i2c->msg_idx++; 193 + i2c->msg_num = 0; 194 + if (ret) 195 + i2c->msg_idx = ret; 196 + 197 + complete(&i2c->completion); 198 + } 199 + 200 + static void synquacer_i2c_hw_init(struct synquacer_i2c *i2c) 201 + { 202 + unsigned char ccr_cs, csr_cs; 203 + u32 rt = i2c->pclkrate; 204 + 205 + /* Set own Address */ 206 + writeb(0, i2c->base + SYNQUACER_I2C_REG_ADR); 207 + 208 + /* Set PCLK frequency */ 209 + writeb(SYNQUACER_I2C_BUS_CLK_FR(i2c->pclkrate), 210 + i2c->base + SYNQUACER_I2C_REG_FSR); 211 + 212 + switch (i2c->speed_khz) { 213 + case SYNQUACER_I2C_SPEED_FM: 214 + if (i2c->pclkrate <= SYNQUACER_I2C_CLK_RATE_18M) { 215 + ccr_cs = SYNQUACER_I2C_CCR_CS_FAST_MAX_18M(rt); 216 + csr_cs = SYNQUACER_I2C_CSR_CS_FAST_MAX_18M(rt); 217 + } else { 218 + ccr_cs = SYNQUACER_I2C_CCR_CS_FAST_MIN_18M(rt); 219 + csr_cs = SYNQUACER_I2C_CSR_CS_FAST_MIN_18M(rt); 220 + } 221 + 222 + /* Set Clock and enable, Set fast mode */ 223 + writeb(ccr_cs | SYNQUACER_I2C_CCR_FM | 224 + SYNQUACER_I2C_CCR_EN, 225 + i2c->base + SYNQUACER_I2C_REG_CCR); 226 + writeb(csr_cs, i2c->base + SYNQUACER_I2C_REG_CSR); 227 + break; 228 + case SYNQUACER_I2C_SPEED_SM: 229 + if (i2c->pclkrate <= SYNQUACER_I2C_CLK_RATE_18M) { 230 + ccr_cs = SYNQUACER_I2C_CCR_CS_STD_MAX_18M(rt); 231 + csr_cs = SYNQUACER_I2C_CSR_CS_STD_MAX_18M(rt); 232 + } else { 233 + ccr_cs = SYNQUACER_I2C_CCR_CS_STD_MIN_18M(rt); 234 + csr_cs = SYNQUACER_I2C_CSR_CS_STD_MIN_18M(rt); 235 + } 236 + 237 + /* Set Clock and enable, Set standard mode */ 238 + writeb(ccr_cs | SYNQUACER_I2C_CCR_EN, 239 + i2c->base + SYNQUACER_I2C_REG_CCR); 240 + writeb(csr_cs, i2c->base + SYNQUACER_I2C_REG_CSR); 241 + break; 242 + default: 243 + WARN_ON(1); 244 + } 245 + 246 + /* clear IRQ (INT=0, BER=0), Interrupt Disable */ 247 + writeb(0, i2c->base + SYNQUACER_I2C_REG_BCR); 248 + writeb(0, i2c->base + SYNQUACER_I2C_REG_BC2R); 249 + } 250 + 251 + static void synquacer_i2c_hw_reset(struct synquacer_i2c *i2c) 252 + { 253 + /* Disable clock */ 254 + writeb(0, i2c->base + SYNQUACER_I2C_REG_CCR); 255 + writeb(0, i2c->base + SYNQUACER_I2C_REG_CSR); 256 + 257 + WAIT_PCLK(100, i2c->pclkrate); 258 + } 259 + 260 + static int synquacer_i2c_master_start(struct synquacer_i2c *i2c, 261 + struct i2c_msg *pmsg) 262 + { 263 + unsigned char bsr, bcr; 264 + 265 + writeb(i2c_8bit_addr_from_msg(pmsg), i2c->base + SYNQUACER_I2C_REG_DAR); 266 + 267 + dev_dbg(i2c->dev, "slave:0x%02x\n", pmsg->addr); 268 + 269 + /* Generate Start Condition */ 270 + bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); 271 + bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR); 272 + dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr); 273 + 274 + if ((bsr & SYNQUACER_I2C_BSR_BB) && 275 + !(bcr & SYNQUACER_I2C_BCR_MSS)) { 276 + dev_dbg(i2c->dev, "bus is busy"); 277 + return -EBUSY; 278 + } 279 + 280 + if (bsr & SYNQUACER_I2C_BSR_BB) { /* Bus is busy */ 281 + dev_dbg(i2c->dev, "Continuous Start"); 282 + writeb(bcr | SYNQUACER_I2C_BCR_SCC, 283 + i2c->base + SYNQUACER_I2C_REG_BCR); 284 + } else { 285 + if (bcr & SYNQUACER_I2C_BCR_MSS) { 286 + dev_dbg(i2c->dev, "not in master mode"); 287 + return -EAGAIN; 288 + } 289 + dev_dbg(i2c->dev, "Start Condition"); 290 + /* Start Condition + Enable Interrupts */ 291 + writeb(bcr | SYNQUACER_I2C_BCR_MSS | 292 + SYNQUACER_I2C_BCR_INTE | SYNQUACER_I2C_BCR_BEIE, 293 + i2c->base + SYNQUACER_I2C_REG_BCR); 294 + } 295 + 296 + WAIT_PCLK(10, i2c->pclkrate); 297 + 298 + /* get BSR & BCR registers */ 299 + bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); 300 + bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR); 301 + dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr); 302 + 303 + if ((bsr & SYNQUACER_I2C_BSR_AL) || 304 + !(bcr & SYNQUACER_I2C_BCR_MSS)) { 305 + dev_dbg(i2c->dev, "arbitration lost\n"); 306 + return -EAGAIN; 307 + } 308 + 309 + return 0; 310 + } 311 + 312 + static int synquacer_i2c_doxfer(struct synquacer_i2c *i2c, 313 + struct i2c_msg *msgs, int num) 314 + { 315 + unsigned char bsr; 316 + unsigned long timeout; 317 + int ret; 318 + 319 + if (i2c->is_suspended) 320 + return -EBUSY; 321 + 322 + synquacer_i2c_hw_init(i2c); 323 + bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); 324 + if (bsr & SYNQUACER_I2C_BSR_BB) { 325 + dev_err(i2c->dev, "cannot get bus (bus busy)\n"); 326 + return -EBUSY; 327 + } 328 + 329 + reinit_completion(&i2c->completion); 330 + 331 + i2c->msg = msgs; 332 + i2c->msg_num = num; 333 + i2c->msg_ptr = 0; 334 + i2c->msg_idx = 0; 335 + i2c->state = STATE_START; 336 + 337 + ret = synquacer_i2c_master_start(i2c, i2c->msg); 338 + if (ret < 0) { 339 + dev_dbg(i2c->dev, "Address failed: (%d)\n", ret); 340 + return ret; 341 + } 342 + 343 + timeout = wait_for_completion_timeout(&i2c->completion, 344 + msecs_to_jiffies(i2c->timeout_ms)); 345 + if (timeout == 0) { 346 + dev_dbg(i2c->dev, "timeout\n"); 347 + return -EAGAIN; 348 + } 349 + 350 + ret = i2c->msg_idx; 351 + if (ret != num) { 352 + dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); 353 + return -EAGAIN; 354 + } 355 + 356 + /* wait 2 clock periods to ensure the stop has been through the bus */ 357 + udelay(DIV_ROUND_UP(2 * 1000, i2c->speed_khz)); 358 + 359 + return 0; 360 + } 361 + 362 + static irqreturn_t synquacer_i2c_isr(int irq, void *dev_id) 363 + { 364 + struct synquacer_i2c *i2c = dev_id; 365 + 366 + unsigned char byte; 367 + unsigned char bsr, bcr; 368 + int ret; 369 + 370 + bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR); 371 + bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); 372 + dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr); 373 + 374 + if (bcr & SYNQUACER_I2C_BCR_BER) { 375 + dev_err(i2c->dev, "bus error\n"); 376 + synquacer_i2c_stop(i2c, -EAGAIN); 377 + goto out; 378 + } 379 + if ((bsr & SYNQUACER_I2C_BSR_AL) || 380 + !(bcr & SYNQUACER_I2C_BCR_MSS)) { 381 + dev_dbg(i2c->dev, "arbitration lost\n"); 382 + synquacer_i2c_stop(i2c, -EAGAIN); 383 + goto out; 384 + } 385 + 386 + switch (i2c->state) { 387 + case STATE_START: 388 + if (bsr & SYNQUACER_I2C_BSR_LRB) { 389 + dev_dbg(i2c->dev, "ack was not received\n"); 390 + synquacer_i2c_stop(i2c, -EAGAIN); 391 + goto out; 392 + } 393 + 394 + if (i2c->msg->flags & I2C_M_RD) 395 + i2c->state = STATE_READ; 396 + else 397 + i2c->state = STATE_WRITE; 398 + 399 + if (is_lastmsg(i2c) && i2c->msg->len == 0) { 400 + synquacer_i2c_stop(i2c, 0); 401 + goto out; 402 + } 403 + 404 + if (i2c->state == STATE_READ) 405 + goto prepare_read; 406 + 407 + /* fallthru */ 408 + 409 + case STATE_WRITE: 410 + if (bsr & SYNQUACER_I2C_BSR_LRB) { 411 + dev_dbg(i2c->dev, "WRITE: No Ack\n"); 412 + synquacer_i2c_stop(i2c, -EAGAIN); 413 + goto out; 414 + } 415 + 416 + if (!is_msgend(i2c)) { 417 + writeb(i2c->msg->buf[i2c->msg_ptr++], 418 + i2c->base + SYNQUACER_I2C_REG_DAR); 419 + 420 + /* clear IRQ, and continue */ 421 + writeb(SYNQUACER_I2C_BCR_BEIE | 422 + SYNQUACER_I2C_BCR_MSS | 423 + SYNQUACER_I2C_BCR_INTE, 424 + i2c->base + SYNQUACER_I2C_REG_BCR); 425 + break; 426 + } 427 + if (is_lastmsg(i2c)) { 428 + synquacer_i2c_stop(i2c, 0); 429 + break; 430 + } 431 + dev_dbg(i2c->dev, "WRITE: Next Message\n"); 432 + 433 + i2c->msg_ptr = 0; 434 + i2c->msg_idx++; 435 + i2c->msg++; 436 + 437 + /* send the new start */ 438 + ret = synquacer_i2c_master_start(i2c, i2c->msg); 439 + if (ret < 0) { 440 + dev_dbg(i2c->dev, "restart error (%d)\n", ret); 441 + synquacer_i2c_stop(i2c, -EAGAIN); 442 + break; 443 + } 444 + i2c->state = STATE_START; 445 + break; 446 + 447 + case STATE_READ: 448 + byte = readb(i2c->base + SYNQUACER_I2C_REG_DAR); 449 + if (!(bsr & SYNQUACER_I2C_BSR_FBT)) /* data */ 450 + i2c->msg->buf[i2c->msg_ptr++] = byte; 451 + else /* address */ 452 + dev_dbg(i2c->dev, "address:0x%02x. ignore it.\n", byte); 453 + 454 + prepare_read: 455 + if (is_msglast(i2c)) { 456 + writeb(SYNQUACER_I2C_BCR_MSS | 457 + SYNQUACER_I2C_BCR_BEIE | 458 + SYNQUACER_I2C_BCR_INTE, 459 + i2c->base + SYNQUACER_I2C_REG_BCR); 460 + break; 461 + } 462 + if (!is_msgend(i2c)) { 463 + writeb(SYNQUACER_I2C_BCR_MSS | 464 + SYNQUACER_I2C_BCR_BEIE | 465 + SYNQUACER_I2C_BCR_INTE | 466 + SYNQUACER_I2C_BCR_ACK, 467 + i2c->base + SYNQUACER_I2C_REG_BCR); 468 + break; 469 + } 470 + if (is_lastmsg(i2c)) { 471 + /* last message, send stop and complete */ 472 + dev_dbg(i2c->dev, "READ: Send Stop\n"); 473 + synquacer_i2c_stop(i2c, 0); 474 + break; 475 + } 476 + dev_dbg(i2c->dev, "READ: Next Transfer\n"); 477 + 478 + i2c->msg_ptr = 0; 479 + i2c->msg_idx++; 480 + i2c->msg++; 481 + 482 + ret = synquacer_i2c_master_start(i2c, i2c->msg); 483 + if (ret < 0) { 484 + dev_dbg(i2c->dev, "restart error (%d)\n", ret); 485 + synquacer_i2c_stop(i2c, -EAGAIN); 486 + } else { 487 + i2c->state = STATE_START; 488 + } 489 + break; 490 + default: 491 + dev_err(i2c->dev, "called in err STATE (%d)\n", i2c->state); 492 + break; 493 + } 494 + 495 + out: 496 + WAIT_PCLK(10, i2c->pclkrate); 497 + return IRQ_HANDLED; 498 + } 499 + 500 + static int synquacer_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, 501 + int num) 502 + { 503 + struct synquacer_i2c *i2c; 504 + int retry; 505 + int ret; 506 + 507 + i2c = i2c_get_adapdata(adap); 508 + i2c->timeout_ms = calc_timeout_ms(i2c, msgs, num); 509 + 510 + dev_dbg(i2c->dev, "calculated timeout %d ms\n", i2c->timeout_ms); 511 + 512 + for (retry = 0; retry < adap->retries; retry++) { 513 + ret = synquacer_i2c_doxfer(i2c, msgs, num); 514 + if (ret != -EAGAIN) 515 + return ret; 516 + 517 + dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); 518 + 519 + synquacer_i2c_hw_reset(i2c); 520 + } 521 + return -EIO; 522 + } 523 + 524 + static u32 synquacer_i2c_functionality(struct i2c_adapter *adap) 525 + { 526 + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 527 + } 528 + 529 + static const struct i2c_algorithm synquacer_i2c_algo = { 530 + .master_xfer = synquacer_i2c_xfer, 531 + .functionality = synquacer_i2c_functionality, 532 + }; 533 + 534 + static struct i2c_adapter synquacer_i2c_ops = { 535 + .owner = THIS_MODULE, 536 + .name = "synquacer_i2c-adapter", 537 + .algo = &synquacer_i2c_algo, 538 + .retries = 5, 539 + }; 540 + 541 + static int synquacer_i2c_probe(struct platform_device *pdev) 542 + { 543 + struct synquacer_i2c *i2c; 544 + struct resource *r; 545 + u32 bus_speed; 546 + int ret; 547 + 548 + i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); 549 + if (!i2c) 550 + return -ENOMEM; 551 + 552 + bus_speed = i2c_acpi_find_bus_speed(&pdev->dev); 553 + if (!bus_speed) 554 + device_property_read_u32(&pdev->dev, "clock-frequency", 555 + &bus_speed); 556 + 557 + device_property_read_u32(&pdev->dev, "socionext,pclk-rate", 558 + &i2c->pclkrate); 559 + 560 + i2c->pclk = devm_clk_get(&pdev->dev, "pclk"); 561 + if (IS_ERR(i2c->pclk) && PTR_ERR(i2c->pclk) == -EPROBE_DEFER) 562 + return -EPROBE_DEFER; 563 + if (!IS_ERR_OR_NULL(i2c->pclk)) { 564 + dev_dbg(&pdev->dev, "clock source %p\n", i2c->pclk); 565 + 566 + ret = clk_prepare_enable(i2c->pclk); 567 + if (ret) { 568 + dev_err(&pdev->dev, "failed to enable clock (%d)\n", 569 + ret); 570 + return ret; 571 + } 572 + i2c->pclkrate = clk_get_rate(i2c->pclk); 573 + } 574 + 575 + if (i2c->pclkrate < SYNQUACER_I2C_MIN_CLK_RATE || 576 + i2c->pclkrate > SYNQUACER_I2C_MAX_CLK_RATE) { 577 + dev_err(&pdev->dev, "PCLK missing or out of range (%d)\n", 578 + i2c->pclkrate); 579 + return -EINVAL; 580 + } 581 + 582 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 583 + i2c->base = devm_ioremap_resource(&pdev->dev, r); 584 + if (IS_ERR(i2c->base)) 585 + return PTR_ERR(i2c->base); 586 + 587 + i2c->irq = platform_get_irq(pdev, 0); 588 + if (i2c->irq < 0) { 589 + dev_err(&pdev->dev, "no IRQ resource found\n"); 590 + return -ENODEV; 591 + } 592 + 593 + ret = devm_request_irq(&pdev->dev, i2c->irq, synquacer_i2c_isr, 594 + 0, dev_name(&pdev->dev), i2c); 595 + if (ret < 0) { 596 + dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); 597 + return ret; 598 + } 599 + 600 + i2c->state = STATE_IDLE; 601 + i2c->dev = &pdev->dev; 602 + i2c->adapter = synquacer_i2c_ops; 603 + i2c_set_adapdata(&i2c->adapter, i2c); 604 + i2c->adapter.dev.parent = &pdev->dev; 605 + i2c->adapter.nr = pdev->id; 606 + init_completion(&i2c->completion); 607 + 608 + if (bus_speed < 400000) 609 + i2c->speed_khz = SYNQUACER_I2C_SPEED_SM; 610 + else 611 + i2c->speed_khz = SYNQUACER_I2C_SPEED_FM; 612 + 613 + synquacer_i2c_hw_init(i2c); 614 + 615 + ret = i2c_add_numbered_adapter(&i2c->adapter); 616 + if (ret) { 617 + dev_err(&pdev->dev, "failed to add bus to i2c core\n"); 618 + return ret; 619 + } 620 + 621 + platform_set_drvdata(pdev, i2c); 622 + 623 + dev_info(&pdev->dev, "%s: synquacer_i2c adapter\n", 624 + dev_name(&i2c->adapter.dev)); 625 + 626 + return 0; 627 + } 628 + 629 + static int synquacer_i2c_remove(struct platform_device *pdev) 630 + { 631 + struct synquacer_i2c *i2c = platform_get_drvdata(pdev); 632 + 633 + i2c_del_adapter(&i2c->adapter); 634 + if (!IS_ERR(i2c->pclk)) 635 + clk_disable_unprepare(i2c->pclk); 636 + 637 + return 0; 638 + }; 639 + 640 + static const struct of_device_id synquacer_i2c_dt_ids[] = { 641 + { .compatible = "socionext,synquacer-i2c" }, 642 + { /* sentinel */ } 643 + }; 644 + MODULE_DEVICE_TABLE(of, synquacer_i2c_dt_ids); 645 + 646 + #ifdef CONFIG_ACPI 647 + static const struct acpi_device_id synquacer_i2c_acpi_ids[] = { 648 + { "SCX0003" }, 649 + { /* sentinel */ } 650 + }; 651 + MODULE_DEVICE_TABLE(acpi, synquacer_i2c_acpi_ids); 652 + #endif 653 + 654 + static struct platform_driver synquacer_i2c_driver = { 655 + .probe = synquacer_i2c_probe, 656 + .remove = synquacer_i2c_remove, 657 + .driver = { 658 + .name = "synquacer_i2c", 659 + .of_match_table = of_match_ptr(synquacer_i2c_dt_ids), 660 + .acpi_match_table = ACPI_PTR(synquacer_i2c_acpi_ids), 661 + }, 662 + }; 663 + module_platform_driver(synquacer_i2c_driver); 664 + 665 + MODULE_AUTHOR("Fujitsu Semiconductor Ltd"); 666 + MODULE_DESCRIPTION("Socionext SynQuacer I2C Driver"); 667 + MODULE_LICENSE("GPL v2");
+4 -4
drivers/i2c/busses/i2c-xiic.c
··· 851 851 MODULE_DEVICE_TABLE(of, xiic_of_match); 852 852 #endif 853 853 854 - static int __maybe_unused cdns_i2c_runtime_suspend(struct device *dev) 854 + static int __maybe_unused xiic_i2c_runtime_suspend(struct device *dev) 855 855 { 856 856 struct xiic_i2c *i2c = dev_get_drvdata(dev); 857 857 ··· 860 860 return 0; 861 861 } 862 862 863 - static int __maybe_unused cdns_i2c_runtime_resume(struct device *dev) 863 + static int __maybe_unused xiic_i2c_runtime_resume(struct device *dev) 864 864 { 865 865 struct xiic_i2c *i2c = dev_get_drvdata(dev); 866 866 int ret; ··· 875 875 } 876 876 877 877 static const struct dev_pm_ops xiic_dev_pm_ops = { 878 - SET_RUNTIME_PM_OPS(cdns_i2c_runtime_suspend, 879 - cdns_i2c_runtime_resume, NULL) 878 + SET_RUNTIME_PM_OPS(xiic_i2c_runtime_suspend, 879 + xiic_i2c_runtime_resume, NULL) 880 880 }; 881 881 static struct platform_driver xiic_i2c_driver = { 882 882 .probe = xiic_i2c_probe,
+65 -13
drivers/i2c/busses/i2c-xlp9xx.c
··· 16 16 #include <linux/kernel.h> 17 17 #include <linux/module.h> 18 18 #include <linux/platform_device.h> 19 + #include <linux/delay.h> 19 20 20 21 #define XLP9XX_I2C_DIV 0x0 21 22 #define XLP9XX_I2C_CTRL 0x1 ··· 36 35 #define XLP9XX_I2C_WAITCNT 0xF 37 36 #define XLP9XX_I2C_TIMEOUT 0X10 38 37 #define XLP9XX_I2C_GENCALLADDR 0x11 38 + 39 + #define XLP9XX_I2C_STATUS_BUSY BIT(0) 39 40 40 41 #define XLP9XX_I2C_CMD_START BIT(7) 41 42 #define XLP9XX_I2C_CMD_STOP BIT(6) ··· 74 71 #define XLP9XX_I2C_HIGH_FREQ 400000 75 72 #define XLP9XX_I2C_FIFO_SIZE 0x80U 76 73 #define XLP9XX_I2C_TIMEOUT_MS 1000 74 + #define XLP9XX_I2C_BUSY_TIMEOUT 50 77 75 78 76 #define XLP9XX_I2C_FIFO_WCNT_MASK 0xff 79 77 #define XLP9XX_I2C_STATUS_ERRMASK (XLP9XX_I2C_INTEN_ARLOST | \ ··· 129 125 { 130 126 u32 thres; 131 127 132 - thres = min(priv->msg_buf_remaining, XLP9XX_I2C_FIFO_SIZE); 128 + if (priv->len_recv) 129 + /* interrupt after the first read to examine 130 + * the length byte before proceeding further 131 + */ 132 + thres = 1; 133 + else if (priv->msg_buf_remaining > XLP9XX_I2C_FIFO_SIZE) 134 + thres = XLP9XX_I2C_FIFO_SIZE; 135 + else 136 + thres = priv->msg_buf_remaining; 137 + 133 138 xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_MFIFOCTRL, 134 139 thres << XLP9XX_I2C_MFIFOCTRL_HITH_SHIFT); 135 140 } ··· 157 144 158 145 static void xlp9xx_i2c_drain_rx_fifo(struct xlp9xx_i2c_dev *priv) 159 146 { 160 - u32 len, i; 147 + u32 len, i, val; 161 148 u8 rlen, *buf = priv->msg_buf; 162 149 163 150 len = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_FIFOWCNT) & ··· 169 156 rlen = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_MRXFIFO); 170 157 *buf++ = rlen; 171 158 len--; 159 + 172 160 if (priv->client_pec) 173 161 ++rlen; 174 162 /* update remaining bytes and message length */ 175 163 priv->msg_buf_remaining = rlen; 176 164 priv->msg_len = rlen + 1; 177 165 priv->len_recv = false; 166 + 167 + /* Update transfer length to read only actual data */ 168 + val = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_CTRL); 169 + val = (val & ~XLP9XX_I2C_CTRL_MCTLEN_MASK) | 170 + ((rlen + 1) << XLP9XX_I2C_CTRL_MCTLEN_SHIFT); 171 + xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CTRL, val); 172 + } else { 173 + len = min(priv->msg_buf_remaining, len); 174 + for (i = 0; i < len; i++, buf++) 175 + *buf = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_MRXFIFO); 176 + 177 + priv->msg_buf_remaining -= len; 178 178 } 179 179 180 - len = min(priv->msg_buf_remaining, len); 181 - for (i = 0; i < len; i++, buf++) 182 - *buf = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_MRXFIFO); 183 - 184 - priv->msg_buf_remaining -= len; 185 180 priv->msg_buf = buf; 186 181 187 182 if (priv->msg_buf_remaining) ··· 243 222 xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_INTEN, 0); 244 223 complete(&priv->msg_complete); 245 224 return IRQ_HANDLED; 225 + } 226 + 227 + static int xlp9xx_i2c_check_bus_status(struct xlp9xx_i2c_dev *priv) 228 + { 229 + u32 status; 230 + u32 busy_timeout = XLP9XX_I2C_BUSY_TIMEOUT; 231 + 232 + while (busy_timeout) { 233 + status = xlp9xx_read_i2c_reg(priv, XLP9XX_I2C_STATUS); 234 + if ((status & XLP9XX_I2C_STATUS_BUSY) == 0) 235 + break; 236 + 237 + busy_timeout--; 238 + usleep_range(1000, 1100); 239 + } 240 + 241 + if (!busy_timeout) 242 + return -EIO; 243 + 244 + return 0; 246 245 } 247 246 248 247 static int xlp9xx_i2c_init(struct xlp9xx_i2c_dev *priv) ··· 352 311 353 312 /* set cmd reg */ 354 313 cmd = XLP9XX_I2C_CMD_START; 355 - cmd |= (priv->msg_read ? XLP9XX_I2C_CMD_READ : XLP9XX_I2C_CMD_WRITE); 314 + if (msg->len) 315 + cmd |= (priv->msg_read ? 316 + XLP9XX_I2C_CMD_READ : XLP9XX_I2C_CMD_WRITE); 356 317 if (last_msg) 357 318 cmd |= XLP9XX_I2C_CMD_STOP; 358 319 ··· 363 320 timeleft = msecs_to_jiffies(XLP9XX_I2C_TIMEOUT_MS); 364 321 timeleft = wait_for_completion_timeout(&priv->msg_complete, timeleft); 365 322 366 - if (priv->msg_err) { 323 + if (priv->msg_err & XLP9XX_I2C_INTEN_BUSERR) { 367 324 dev_dbg(priv->dev, "transfer error %x!\n", priv->msg_err); 368 - if (priv->msg_err & XLP9XX_I2C_INTEN_BUSERR) 369 - xlp9xx_i2c_init(priv); 325 + xlp9xx_write_i2c_reg(priv, XLP9XX_I2C_CMD, XLP9XX_I2C_CMD_STOP); 370 326 return -EIO; 327 + } else if (priv->msg_err & XLP9XX_I2C_INTEN_NACKADDR) { 328 + return -ENXIO; 371 329 } 372 330 373 331 if (timeleft == 0) { ··· 389 345 int i, ret; 390 346 struct xlp9xx_i2c_dev *priv = i2c_get_adapdata(adap); 391 347 348 + ret = xlp9xx_i2c_check_bus_status(priv); 349 + if (ret) { 350 + xlp9xx_i2c_init(priv); 351 + ret = xlp9xx_i2c_check_bus_status(priv); 352 + if (ret) 353 + return ret; 354 + } 355 + 392 356 for (i = 0; i < num; i++) { 393 357 ret = xlp9xx_i2c_xfer_msg(priv, &msgs[i], i == num - 1); 394 358 if (ret != 0) ··· 408 356 409 357 static u32 xlp9xx_i2c_functionality(struct i2c_adapter *adapter) 410 358 { 411 - return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C | 412 - I2C_FUNC_10BIT_ADDR; 359 + return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SMBUS_READ_BLOCK_DATA | 360 + I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR; 413 361 } 414 362 415 363 static const struct i2c_algorithm xlp9xx_i2c_algo = {
+50 -8
drivers/i2c/i2c-core-base.c
··· 58 58 #define I2C_ADDR_7BITS_MAX 0x77 59 59 #define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1) 60 60 61 + #define I2C_ADDR_DEVICE_ID 0x7c 62 + 61 63 /* 62 64 * core_lock protects i2c_adapter_idr, and guarantees that device detection, 63 65 * deletion of detected devices, and attach_adapter calls are serialized ··· 69 67 70 68 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); 71 69 72 - static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE; 70 + static DEFINE_STATIC_KEY_FALSE(i2c_trace_msg_key); 73 71 static bool is_registered; 74 72 75 73 int i2c_transfer_trace_reg(void) 76 74 { 77 - static_key_slow_inc(&i2c_trace_msg); 75 + static_branch_inc(&i2c_trace_msg_key); 78 76 return 0; 79 77 } 80 78 81 79 void i2c_transfer_trace_unreg(void) 82 80 { 83 - static_key_slow_dec(&i2c_trace_msg); 81 + static_branch_dec(&i2c_trace_msg_key); 84 82 } 85 83 86 84 const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, ··· 125 123 { 126 124 struct i2c_client *client = to_i2c_client(dev); 127 125 int rc; 126 + 127 + rc = of_device_uevent_modalias(dev, env); 128 + if (rc != -ENODEV) 129 + return rc; 128 130 129 131 rc = acpi_device_uevent_modalias(dev, env); 130 132 if (rc != -ENODEV) ··· 445 439 struct i2c_client *client = to_i2c_client(dev); 446 440 int len; 447 441 442 + len = of_device_modalias(dev, buf, PAGE_SIZE); 443 + if (len != -ENODEV) 444 + return len; 445 + 448 446 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1); 449 447 if (len != -ENODEV) 450 448 return len; ··· 517 507 518 508 /* This is a permissive address validity check, I2C address map constraints 519 509 * are purposely not enforced, except for the general call address. */ 520 - int i2c_check_addr_validity(unsigned addr, unsigned short flags) 510 + static int i2c_check_addr_validity(unsigned int addr, unsigned short flags) 521 511 { 522 512 if (flags & I2C_CLIENT_TEN) { 523 513 /* 10-bit address, all values are valid */ ··· 1848 1838 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num)) 1849 1839 return -EOPNOTSUPP; 1850 1840 1851 - /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets 1841 + /* 1842 + * i2c_trace_msg_key gets enabled when tracepoint i2c_transfer gets 1852 1843 * enabled. This is an efficient way of keeping the for-loop from 1853 1844 * being executed when not needed. 1854 1845 */ 1855 - if (static_key_false(&i2c_trace_msg)) { 1846 + if (static_branch_unlikely(&i2c_trace_msg_key)) { 1856 1847 int i; 1857 1848 for (i = 0; i < num; i++) 1858 1849 if (msgs[i].flags & I2C_M_RD) ··· 1872 1861 break; 1873 1862 } 1874 1863 1875 - if (static_key_false(&i2c_trace_msg)) { 1864 + if (static_branch_unlikely(&i2c_trace_msg_key)) { 1876 1865 int i; 1877 1866 for (i = 0; i < ret; i++) 1878 1867 if (msgs[i].flags & I2C_M_RD) 1879 1868 trace_i2c_reply(adap, &msgs[i], i); 1880 - trace_i2c_result(adap, i, ret); 1869 + trace_i2c_result(adap, num, ret); 1881 1870 } 1882 1871 1883 1872 return ret; ··· 1978 1967 return (ret == 1) ? count : ret; 1979 1968 } 1980 1969 EXPORT_SYMBOL(i2c_transfer_buffer_flags); 1970 + 1971 + /** 1972 + * i2c_get_device_id - get manufacturer, part id and die revision of a device 1973 + * @client: The device to query 1974 + * @id: The queried information 1975 + * 1976 + * Returns negative errno on error, zero on success. 1977 + */ 1978 + int i2c_get_device_id(const struct i2c_client *client, 1979 + struct i2c_device_identity *id) 1980 + { 1981 + struct i2c_adapter *adap = client->adapter; 1982 + union i2c_smbus_data raw_id; 1983 + int ret; 1984 + 1985 + if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) 1986 + return -EOPNOTSUPP; 1987 + 1988 + raw_id.block[0] = 3; 1989 + ret = i2c_smbus_xfer(adap, I2C_ADDR_DEVICE_ID, 0, 1990 + I2C_SMBUS_READ, client->addr << 1, 1991 + I2C_SMBUS_I2C_BLOCK_DATA, &raw_id); 1992 + if (ret) 1993 + return ret; 1994 + 1995 + id->manufacturer_id = (raw_id.block[1] << 4) | (raw_id.block[2] >> 4); 1996 + id->part_id = ((raw_id.block[2] & 0xf) << 5) | (raw_id.block[3] >> 3); 1997 + id->die_revision = raw_id.block[3] & 0x7; 1998 + return 0; 1999 + } 2000 + EXPORT_SYMBOL_GPL(i2c_get_device_id); 1981 2001 1982 2002 /* ---------------------------------------------------- 1983 2003 * the i2c address scanning function
+11 -19
drivers/i2c/i2c-core-of.c
··· 4 4 * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de> 5 5 * based on a previous patch from Jon Smirl <jonsmirl@gmail.com> 6 6 * 7 - * Copyright (C) 2013 Wolfram Sang <wsa@the-dreams.de> 7 + * Copyright (C) 2013, 2018 Wolfram Sang <wsa@the-dreams.de> 8 8 * 9 9 * This program is free software; you can redistribute it and/or modify it 10 10 * under the terms of the GNU General Public License as published by the Free ··· 25 25 static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap, 26 26 struct device_node *node) 27 27 { 28 - struct i2c_client *result; 28 + struct i2c_client *client; 29 29 struct i2c_board_info info = {}; 30 30 struct dev_archdata dev_ad = {}; 31 - const __be32 *addr_be; 32 31 u32 addr; 33 - int len; 32 + int ret; 34 33 35 34 dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node); 36 35 ··· 39 40 return ERR_PTR(-EINVAL); 40 41 } 41 42 42 - addr_be = of_get_property(node, "reg", &len); 43 - if (!addr_be || (len < sizeof(*addr_be))) { 43 + ret = of_property_read_u32(node, "reg", &addr); 44 + if (ret) { 44 45 dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", node); 45 - return ERR_PTR(-EINVAL); 46 + return ERR_PTR(ret); 46 47 } 47 48 48 - addr = be32_to_cpup(addr_be); 49 49 if (addr & I2C_TEN_BIT_ADDRESS) { 50 50 addr &= ~I2C_TEN_BIT_ADDRESS; 51 51 info.flags |= I2C_CLIENT_TEN; ··· 55 57 info.flags |= I2C_CLIENT_SLAVE; 56 58 } 57 59 58 - if (i2c_check_addr_validity(addr, info.flags)) { 59 - dev_err(&adap->dev, "of_i2c: invalid addr=%x on %pOF\n", 60 - addr, node); 61 - return ERR_PTR(-EINVAL); 62 - } 63 - 64 60 info.addr = addr; 65 - info.of_node = of_node_get(node); 66 61 info.archdata = &dev_ad; 62 + info.of_node = of_node_get(node); 67 63 68 64 if (of_property_read_bool(node, "host-notify")) 69 65 info.flags |= I2C_CLIENT_HOST_NOTIFY; ··· 65 73 if (of_get_property(node, "wakeup-source", NULL)) 66 74 info.flags |= I2C_CLIENT_WAKE; 67 75 68 - result = i2c_new_device(adap, &info); 69 - if (result == NULL) { 76 + client = i2c_new_device(adap, &info); 77 + if (!client) { 70 78 dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node); 71 79 of_node_put(node); 72 80 return ERR_PTR(-EINVAL); 73 81 } 74 - return result; 82 + return client; 75 83 } 76 84 77 85 void of_i2c_register_devices(struct i2c_adapter *adap) ··· 95 103 96 104 client = of_i2c_register_device(adap, node); 97 105 if (IS_ERR(client)) { 98 - dev_warn(&adap->dev, 106 + dev_err(&adap->dev, 99 107 "Failed to create I2C device for %pOF\n", 100 108 node); 101 109 of_node_clear_flag(node, OF_POPULATED);
+10 -6
drivers/i2c/i2c-core-smbus.c
··· 308 308 msg->buf[0] = init_val; 309 309 } 310 310 311 - /* Simulate a SMBus command using the i2c protocol 312 - No checking of parameters is done! */ 311 + /* 312 + * Simulate a SMBus command using the I2C protocol. 313 + * No checking of parameters is done! 314 + */ 313 315 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, 314 316 unsigned short flags, 315 317 char read_write, u8 command, int size, 316 318 union i2c_smbus_data *data) 317 319 { 318 - /* So we need to generate a series of msgs. In the case of writing, we 319 - need to use only one message; when reading, we need two. We initialize 320 - most things with sane defaults, to keep the code below somewhat 321 - simpler. */ 320 + /* 321 + * So we need to generate a series of msgs. In the case of writing, we 322 + * need to use only one message; when reading, we need two. We 323 + * initialize most things with sane defaults, to keep the code below 324 + * somewhat simpler. 325 + */ 322 326 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; 323 327 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; 324 328 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
-1
drivers/i2c/i2c-core.h
··· 27 27 extern struct list_head __i2c_board_list; 28 28 extern int __i2c_first_dynamic_bus_num; 29 29 30 - int i2c_check_addr_validity(unsigned addr, unsigned short flags); 31 30 int i2c_check_7bit_addr_validity_strict(unsigned short addr); 32 31 33 32 #ifdef CONFIG_ACPI
+49 -6
drivers/i2c/muxes/i2c-mux-pca954x.c
··· 77 77 pca954x_ismux = 0, 78 78 pca954x_isswi 79 79 } muxtype; 80 + struct i2c_device_identity id; 80 81 }; 81 82 82 83 struct pca954x { ··· 98 97 .nchans = 2, 99 98 .enable = 0x4, 100 99 .muxtype = pca954x_ismux, 100 + .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 101 101 }, 102 102 [pca_9542] = { 103 103 .nchans = 2, 104 104 .enable = 0x4, 105 105 .has_irq = 1, 106 106 .muxtype = pca954x_ismux, 107 + .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 107 108 }, 108 109 [pca_9543] = { 109 110 .nchans = 2, 110 111 .has_irq = 1, 111 112 .muxtype = pca954x_isswi, 113 + .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 112 114 }, 113 115 [pca_9544] = { 114 116 .nchans = 4, 115 117 .enable = 0x4, 116 118 .has_irq = 1, 117 119 .muxtype = pca954x_ismux, 120 + .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 118 121 }, 119 122 [pca_9545] = { 120 123 .nchans = 4, 121 124 .has_irq = 1, 122 125 .muxtype = pca954x_isswi, 126 + .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 123 127 }, 124 128 [pca_9546] = { 125 129 .nchans = 4, 126 130 .muxtype = pca954x_isswi, 131 + .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 127 132 }, 128 133 [pca_9547] = { 129 134 .nchans = 8, 130 135 .enable = 0x8, 131 136 .muxtype = pca954x_ismux, 137 + .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 132 138 }, 133 139 [pca_9548] = { 134 140 .nchans = 8, 135 141 .muxtype = pca954x_isswi, 142 + .id = { .manufacturer_id = I2C_DEVICE_ID_NONE }, 136 143 }, 137 144 [pca_9846] = { 138 145 .nchans = 4, 139 146 .muxtype = pca954x_isswi, 147 + .id = { 148 + .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, 149 + .part_id = 0x10b, 150 + }, 140 151 }, 141 152 [pca_9847] = { 142 153 .nchans = 8, 143 154 .enable = 0x8, 144 155 .muxtype = pca954x_ismux, 156 + .id = { 157 + .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, 158 + .part_id = 0x108, 159 + }, 145 160 }, 146 161 [pca_9848] = { 147 162 .nchans = 8, 148 163 .muxtype = pca954x_isswi, 164 + .id = { 165 + .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, 166 + .part_id = 0x10a, 167 + }, 149 168 }, 150 169 [pca_9849] = { 151 170 .nchans = 4, 152 171 .enable = 0x4, 153 172 .muxtype = pca954x_ismux, 173 + .id = { 174 + .manufacturer_id = I2C_DEVICE_ID_NXP_SEMICONDUCTORS, 175 + .part_id = 0x109, 176 + }, 154 177 }, 155 178 }; 156 179 ··· 394 369 if (IS_ERR(gpio)) 395 370 return PTR_ERR(gpio); 396 371 372 + match = of_match_device(of_match_ptr(pca954x_of_match), &client->dev); 373 + if (match) 374 + data->chip = of_device_get_match_data(&client->dev); 375 + else 376 + data->chip = &chips[id->driver_data]; 377 + 378 + if (data->chip->id.manufacturer_id != I2C_DEVICE_ID_NONE) { 379 + struct i2c_device_identity id; 380 + 381 + ret = i2c_get_device_id(client, &id); 382 + if (ret && ret != -EOPNOTSUPP) 383 + return ret; 384 + 385 + if (!ret && 386 + (id.manufacturer_id != data->chip->id.manufacturer_id || 387 + id.part_id != data->chip->id.part_id)) { 388 + dev_warn(&client->dev, 389 + "unexpected device id %03x-%03x-%x\n", 390 + id.manufacturer_id, id.part_id, 391 + id.die_revision); 392 + return -ENODEV; 393 + } 394 + } 395 + 397 396 /* Write the mux register at addr to verify 398 397 * that the mux is in fact present. This also 399 398 * initializes the mux to disconnected state. ··· 426 377 dev_warn(&client->dev, "probe failed\n"); 427 378 return -ENODEV; 428 379 } 429 - 430 - match = of_match_device(of_match_ptr(pca954x_of_match), &client->dev); 431 - if (match) 432 - data->chip = of_device_get_match_data(&client->dev); 433 - else 434 - data->chip = &chips[id->driver_data]; 435 380 436 381 data->last_chan = 0; /* force the first selection */ 437 382
-3
include/linux/i2c-pca-platform.h
··· 3 3 #define I2C_PCA9564_PLATFORM_H 4 4 5 5 struct i2c_pca9564_pf_platform_data { 6 - int gpio; /* pin to reset chip. driver will work when 7 - * not supplied (negative value), but it 8 - * cannot exit some error conditions then */ 9 6 int i2c_clock_speed; /* values are defined in linux/i2c-algo-pca.h */ 10 7 int timeout; /* timeout in jiffies */ 11 8 };
+30
include/linux/i2c.h
··· 47 47 struct i2c_adapter; 48 48 struct i2c_client; 49 49 struct i2c_driver; 50 + struct i2c_device_identity; 50 51 union i2c_smbus_data; 51 52 struct i2c_board_info; 52 53 enum i2c_slave_event; ··· 187 186 extern s32 188 187 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client, 189 188 u8 command, u8 length, u8 *values); 189 + int i2c_get_device_id(const struct i2c_client *client, 190 + struct i2c_device_identity *id); 190 191 #endif /* I2C */ 192 + 193 + /** 194 + * struct i2c_device_identity - i2c client device identification 195 + * @manufacturer_id: 0 - 4095, database maintained by NXP 196 + * @part_id: 0 - 511, according to manufacturer 197 + * @die_revision: 0 - 7, according to manufacturer 198 + */ 199 + struct i2c_device_identity { 200 + u16 manufacturer_id; 201 + #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS 0 202 + #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_1 1 203 + #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_2 2 204 + #define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_3 3 205 + #define I2C_DEVICE_ID_RAMTRON_INTERNATIONAL 4 206 + #define I2C_DEVICE_ID_ANALOG_DEVICES 5 207 + #define I2C_DEVICE_ID_STMICROELECTRONICS 6 208 + #define I2C_DEVICE_ID_ON_SEMICONDUCTOR 7 209 + #define I2C_DEVICE_ID_SPRINTEK_CORPORATION 8 210 + #define I2C_DEVICE_ID_ESPROS_PHOTONICS_AG 9 211 + #define I2C_DEVICE_ID_FUJITSU_SEMICONDUCTOR 10 212 + #define I2C_DEVICE_ID_FLIR 11 213 + #define I2C_DEVICE_ID_O2MICRO 12 214 + #define I2C_DEVICE_ID_ATMEL 13 215 + #define I2C_DEVICE_ID_NONE 0xffff 216 + u16 part_id; 217 + u8 die_revision; 218 + }; 191 219 192 220 enum i2c_alert_protocol { 193 221 I2C_PROTOCOL_SMBUS_ALERT,