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

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

Pull i2c updates from Wolfram Sang:

- the first series of making i2c_device_id optional instead of
mandatory (in favor of alternatives like of_device_id).

This involves adding a new probe callback (probe_new) which removes
some peculiarities I2C had for a long time now. The new probe is
matching the other subsystems now and the old one will be removed
once all users are converted. It is expected to take a while but
there is ongoing interest in that.

- SMBus Host Notify introduced 4.9 got refactored. They are now using
interrupts instead of the alert callback which solves multiple
issues.

- new drivers for iMX LowPower I2C, Mellanox CPLD and its I2C mux

- significant refactoring for bcm2835 driver

- the usual set of driver updates and improvements

* 'i2c/for-4.10' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux: (46 commits)
i2c: fsl-lpi2c: read lpi2c fifo size in probe()
i2c: octeon: thunderx: Remove double-check after interrupt
i2c: octeon: thunderx: TWSI software reset in recovery
i2c: cadence: Allow Cadence I2C to be selected for Cadence Xtensa CPUs
i2c: sh_mobile: Add per-Generation fallback bindings
i2c: rcar: Add per-Generation fallback bindings
i2c: imx-lpi2c: add low power i2c bus driver
dt-bindings: i2c: imx-lpi2c: add devicetree bindings
i2c: designware-pcidrv: Add 10bit address feature to medfield/merrifield
i2c: pxa: Add support for the I2C units found in Armada 3700
i2c: pxa: Add definition of fast and high speed modes via the regs layout
dt-bindings: i2c: pxa: Update the documentation for the Armada 3700
i2c: qup: support SMBus block read
i2c: qup: add ACPI support
i2c: designware: Consolidate default functionality bits
i2c: i2c-mux-gpio: update mux with gpiod_set_array_value_cansleep
i2c: mux: pca954x: Add ACPI support for pca954x
i2c: use an IRQ to report Host Notify events, not alert
i2c: i801: remove SMBNTFDDAT reads as they always seem to return 0
i2c: i801: use the BIT() macro for FEATURES_* also
...

+2272 -437
+20
Documentation/devicetree/bindings/i2c/i2c-imx-lpi2c.txt
··· 1 + * Freescale Low Power Inter IC (LPI2C) for i.MX 2 + 3 + Required properties: 4 + - compatible : 5 + - "fsl,imx7ulp-lpi2c" for LPI2C compatible with the one integrated on i.MX7ULP soc 6 + - "fsl,imx8dv-lpi2c" for LPI2C compatible with the one integrated on i.MX8DV soc 7 + - reg : address and length of the lpi2c master registers 8 + - interrupt-parent : core interrupt controller 9 + - interrupts : lpi2c interrupt 10 + - clocks : lpi2c clock specifier 11 + 12 + Examples: 13 + 14 + lpi2c7: lpi2c7@40A50000 { 15 + compatible = "fsl,imx8dv-lpi2c"; 16 + reg = <0x40A50000 0x10000>; 17 + interrupt-parent = <&intc>; 18 + interrupts = <GIC_SPI 37 IRQ_TYPE_LEVEL_HIGH>; 19 + clocks = <&clks IMX7ULP_CLK_LPI2C7>; 20 + };
+1
Documentation/devicetree/bindings/i2c/i2c-pxa.txt
··· 7 7 compatible processor, e.g. pxa168, pxa910, mmp2, mmp3. 8 8 For the pxa2xx/pxa3xx, an additional node "mrvl,pxa-i2c" is required 9 9 as shown in the example below. 10 + For the Armada 3700, the compatible should be "marvell,armada-3700-i2c". 10 11 11 12 Recommended properties : 12 13
+20 -12
Documentation/devicetree/bindings/i2c/i2c-rcar.txt
··· 1 1 I2C for R-Car platforms 2 2 3 3 Required properties: 4 - - compatible: Must be one of 5 - "renesas,i2c-rcar" 6 - "renesas,i2c-r8a7778" 7 - "renesas,i2c-r8a7779" 8 - "renesas,i2c-r8a7790" 9 - "renesas,i2c-r8a7791" 10 - "renesas,i2c-r8a7792" 11 - "renesas,i2c-r8a7793" 12 - "renesas,i2c-r8a7794" 13 - "renesas,i2c-r8a7795" 14 - "renesas,i2c-r8a7796" 4 + - compatible: 5 + "renesas,i2c-r8a7778" if the device is a part of a R8A7778 SoC. 6 + "renesas,i2c-r8a7779" if the device is a part of a R8A7779 SoC. 7 + "renesas,i2c-r8a7790" if the device is a part of a R8A7790 SoC. 8 + "renesas,i2c-r8a7791" if the device is a part of a R8A7791 SoC. 9 + "renesas,i2c-r8a7792" if the device is a part of a R8A7792 SoC. 10 + "renesas,i2c-r8a7793" if the device is a part of a R8A7793 SoC. 11 + "renesas,i2c-r8a7794" if the device is a part of a R8A7794 SoC. 12 + "renesas,i2c-r8a7795" if the device is a part of a R8A7795 SoC. 13 + "renesas,i2c-r8a7796" if the device is a part of a R8A7796 SoC. 14 + "renesas,rcar-gen1-i2c" for a generic R-Car Gen1 compatible device. 15 + "renesas,rcar-gen2-i2c" for a generic R-Car Gen2 compatible device. 16 + "renesas,rcar-gen3-i2c" for a generic R-Car Gen3 compatible device. 17 + "renesas,i2c-rcar" (deprecated) 18 + 19 + When compatible with the generic version, nodes must list the 20 + SoC-specific version corresponding to the platform first followed 21 + by the generic version. 22 + 15 23 - reg: physical base address of the controller and length of memory mapped 16 24 region. 17 25 - interrupts: interrupt specifier. ··· 41 33 i2c0: i2c@e6508000 { 42 34 #address-cells = <1>; 43 35 #size-cells = <0>; 44 - compatible = "renesas,i2c-r8a7791"; 36 + compatible = "renesas,i2c-r8a7791", "renesas,rcar-gen2-i2c"; 45 37 reg = <0 0xe6508000 0 0x40>; 46 38 interrupts = <0 287 IRQ_TYPE_LEVEL_HIGH>; 47 39 clocks = <&mstp9_clks R8A7791_CLK_I2C0>;
+14 -3
Documentation/devicetree/bindings/i2c/i2c-sh_mobile.txt
··· 1 1 Device tree configuration for Renesas IIC (sh_mobile) driver 2 2 3 3 Required properties: 4 - - compatible : "renesas,iic-<soctype>". "renesas,rmobile-iic" as fallback 5 - Examples with soctypes are: 4 + - compatible : 6 5 - "renesas,iic-r8a73a4" (R-Mobile APE6) 7 6 - "renesas,iic-r8a7740" (R-Mobile A1) 8 7 - "renesas,iic-r8a7790" (R-Car H2) ··· 11 12 - "renesas,iic-r8a7794" (R-Car E2) 12 13 - "renesas,iic-r8a7795" (R-Car H3) 13 14 - "renesas,iic-sh73a0" (SH-Mobile AG5) 15 + - "renesas,rcar-gen2-iic" (generic R-Car Gen2 compatible device) 16 + - "renesas,rcar-gen3-iic" (generic R-Car Gen3 compatible device) 17 + - "renesas,rmobile-iic" (generic device) 18 + 19 + When compatible with a generic R-Car version, nodes 20 + must list the SoC-specific version corresponding to 21 + the platform first followed by the generic R-Car 22 + version. 23 + 24 + renesas,rmobile-iic must always follow. 25 + 14 26 - reg : address start and address range size of device 15 27 - interrupts : interrupt of device 16 28 - clocks : clock for device ··· 41 31 Example: 42 32 43 33 iic0: i2c@e6500000 { 44 - compatible = "renesas,iic-r8a7790", "renesas,rmobile-iic"; 34 + compatible = "renesas,iic-r8a7790", "renesas,rcar-gen2-iic", 35 + "renesas,rmobile-iic"; 45 36 reg = <0 0xe6500000 0 0x425>; 46 37 interrupts = <0 174 IRQ_TYPE_LEVEL_HIGH>; 47 38 clocks = <&mstp3_clks R8A7790_CLK_IIC0>;
+47
Documentation/i2c/busses/i2c-mlxcpld
··· 1 + Driver i2c-mlxcpld 2 + 3 + Author: Michael Shych <michaelsh@mellanox.com> 4 + 5 + This is the Mellanox I2C controller logic, implemented in Lattice CPLD 6 + device. 7 + Device supports: 8 + - Master mode. 9 + - One physical bus. 10 + - Polling mode. 11 + 12 + This controller is equipped within the next Mellanox systems: 13 + "msx6710", "msx6720", "msb7700", "msn2700", "msx1410", "msn2410", "msb7800", 14 + "msn2740", "msn2100". 15 + 16 + The next transaction types are supported: 17 + - Receive Byte/Block. 18 + - Send Byte/Block. 19 + - Read Byte/Block. 20 + - Write Byte/Block. 21 + 22 + Registers: 23 + CTRL 0x1 - control reg. 24 + Resets all the registers. 25 + HALF_CYC 0x4 - cycle reg. 26 + Configure the width of I2C SCL half clock cycle (in 4 LPC_CLK 27 + units). 28 + I2C_HOLD 0x5 - hold reg. 29 + OE (output enable) is delayed by value set to this register 30 + (in LPC_CLK units) 31 + CMD 0x6 - command reg. 32 + Bit 0, 0 = write, 1 = read. 33 + Bits [7:1] - the 7bit Address of the I2C device. 34 + It should be written last as it triggers an I2C transaction. 35 + NUM_DATA 0x7 - data size reg. 36 + Number of data bytes to write in read transaction 37 + NUM_ADDR 0x8 - address reg. 38 + Number of address bytes to write in read transaction. 39 + STATUS 0x9 - status reg. 40 + Bit 0 - transaction is completed. 41 + Bit 4 - ACK/NACK. 42 + DATAx 0xa - 0x54 - 68 bytes data buffer regs. 43 + For write transaction address is specified in four first bytes 44 + (DATA1 - DATA4), data starting from DATA4. 45 + For read transactions address is sent in a separate transaction and 46 + specified in the four first bytes (DATA0 - DATA3). Data is read 47 + starting from DATA0.
+8 -4
Documentation/i2c/smbus-protocol
··· 200 200 [S] [HostAddr] [Wr] A [DevAddr] A [DataLow] A [DataHigh] A [P] 201 201 202 202 This is implemented in the following way in the Linux kernel: 203 - * I2C bus drivers which support SMBus Host Notify should call 204 - i2c_setup_smbus_host_notify() to setup SMBus Host Notify support. 205 - * I2C drivers for devices which can trigger SMBus Host Notify should implement 206 - the optional alert() callback. 203 + * I2C bus drivers which support SMBus Host Notify should report 204 + I2C_FUNC_SMBUS_HOST_NOTIFY. 205 + * I2C bus drivers trigger SMBus Host Notify by a call to 206 + i2c_handle_smbus_host_notify(). 207 + * I2C drivers for devices which can trigger SMBus Host Notify will have 208 + client->irq assigned to a Host Notify IRQ if noone else specified an other. 209 + 210 + There is currently no way to retrieve the data parameter from the client. 207 211 208 212 209 213 Packet Error Checking (PEC)
+9
MAINTAINERS
··· 8023 8023 Q: http://patchwork.ozlabs.org/project/netdev/list/ 8024 8024 F: drivers/net/ethernet/mellanox/mlxsw/ 8025 8025 8026 + MELLANOX MLXCPLD I2C AND MUX DRIVER 8027 + M: Vadim Pasternak <vadimp@mellanox.com> 8028 + M: Michael Shych <michaelsh@mellanox.com> 8029 + L: linux-i2c@vger.kernel.org 8030 + S: Supported 8031 + F: drivers/i2c/busses/i2c-mlxcpld.c 8032 + F: drivers/i2c/muxes/i2c-mux-mlxcpld.c 8033 + F: Documentation/i2c/busses/i2c-mlxcpld 8034 + 8026 8035 MELLANOX MLXCPLD LED DRIVER 8027 8036 M: Vadim Pasternak <vadimp@mellanox.com> 8028 8037 L: linux-leds@vger.kernel.org
+1
drivers/i2c/Kconfig
··· 7 7 config I2C 8 8 tristate "I2C support" 9 9 select RT_MUTEXES 10 + select IRQ_DOMAIN 10 11 ---help--- 11 12 I2C (pronounce: I-squared-C) is a slow serial bus protocol used in 12 13 many micro controller applications and developed by Philips. SMBus,
+23 -2
drivers/i2c/busses/Kconfig
··· 426 426 427 427 config I2C_CADENCE 428 428 tristate "Cadence I2C Controller" 429 - depends on ARCH_ZYNQ || ARM64 429 + depends on ARCH_ZYNQ || ARM64 || XTENSA 430 430 help 431 431 Say yes here to select Cadence I2C Host Controller. This controller is 432 432 e.g. used by Xilinx Zynq. ··· 597 597 This driver can also be built as a module. If so, the module 598 598 will be called i2c-imx. 599 599 600 + config I2C_IMX_LPI2C 601 + tristate "IMX Low Power I2C interface" 602 + depends on ARCH_MXC || COMPILE_TEST 603 + help 604 + Say Y here if you want to use the Low Power IIC bus controller 605 + on the Freescale i.MX processors. 606 + 607 + This driver can also be built as a module. If so, the module 608 + will be called i2c-imx-lpi2c. 609 + 600 610 config I2C_IOP3XX 601 611 tristate "Intel IOPx3xx and IXP4xx on-chip I2C interface" 602 612 depends on ARCH_IOP32X || ARCH_IOP33X || ARCH_IXP4XX || ARCH_IOP13XX ··· 773 763 774 764 config I2C_PXA 775 765 tristate "Intel PXA2XX I2C adapter" 776 - depends on ARCH_PXA || ARCH_MMP || (X86_32 && PCI && OF) 766 + depends on ARCH_PXA || ARCH_MMP || ARCH_MVEBU || (X86_32 && PCI && OF) 777 767 help 778 768 If you have devices in the PXA I2C bus, say yes to this option. 779 769 This driver can also be built as a module. If so, the module ··· 1159 1149 1160 1150 This support is also available as a module. If so, the module 1161 1151 will be called i2c-elektor. 1152 + 1153 + config I2C_MLXCPLD 1154 + tristate "Mellanox I2C driver" 1155 + depends on X86_64 1156 + help 1157 + This exposes the Mellanox platform I2C busses to the linux I2C layer 1158 + for X86 based systems. 1159 + Controller is implemented as CPLD logic. 1160 + 1161 + This driver can also be built as a module. If so, the module will be 1162 + called as i2c-mlxcpld. 1162 1163 1163 1164 config I2C_PCA_ISA 1164 1165 tristate "PCA9564/PCA9665 on an ISA bus"
+2
drivers/i2c/busses/Makefile
··· 56 56 obj-$(CONFIG_I2C_IBM_IIC) += i2c-ibm_iic.o 57 57 obj-$(CONFIG_I2C_IMG) += i2c-img-scb.o 58 58 obj-$(CONFIG_I2C_IMX) += i2c-imx.o 59 + obj-$(CONFIG_I2C_IMX_LPI2C) += i2c-imx-lpi2c.o 59 60 obj-$(CONFIG_I2C_IOP3XX) += i2c-iop3xx.o 60 61 obj-$(CONFIG_I2C_JZ4780) += i2c-jz4780.o 61 62 obj-$(CONFIG_I2C_KEMPLD) += i2c-kempld.o ··· 117 116 obj-$(CONFIG_I2C_BRCMSTB) += i2c-brcmstb.o 118 117 obj-$(CONFIG_I2C_CROS_EC_TUNNEL) += i2c-cros-ec-tunnel.o 119 118 obj-$(CONFIG_I2C_ELEKTOR) += i2c-elektor.o 119 + obj-$(CONFIG_I2C_MLXCPLD) += i2c-mlxcpld.o 120 120 obj-$(CONFIG_I2C_OPAL) += i2c-opal.o 121 121 obj-$(CONFIG_I2C_PCA_ISA) += i2c-pca-isa.o 122 122 obj-$(CONFIG_I2C_SIBYTE) += i2c-sibyte.o
+1 -1
drivers/i2c/busses/i2c-axxia.c
··· 489 489 .functionality = axxia_i2c_func, 490 490 }; 491 491 492 - static struct i2c_adapter_quirks axxia_i2c_quirks = { 492 + static const struct i2c_adapter_quirks axxia_i2c_quirks = { 493 493 .max_read_len = 255, 494 494 .max_write_len = 255, 495 495 };
+1 -1
drivers/i2c/busses/i2c-bcm-iproc.c
··· 395 395 .functionality = bcm_iproc_i2c_functionality, 396 396 }; 397 397 398 - static struct i2c_adapter_quirks bcm_iproc_i2c_quirks = { 398 + static const struct i2c_adapter_quirks bcm_iproc_i2c_quirks = { 399 399 /* need to reserve one byte in the FIFO for the slave address */ 400 400 .max_read_len = M_TX_RX_FIFO_SIZE - 1, 401 401 };
+147 -87
drivers/i2c/busses/i2c-bcm2835.c
··· 50 50 #define BCM2835_I2C_S_CLKT BIT(9) 51 51 #define BCM2835_I2C_S_LEN BIT(10) /* Fake bit for SW error reporting */ 52 52 53 - #define BCM2835_I2C_BITMSK_S 0x03FF 54 - 55 53 #define BCM2835_I2C_CDIV_MIN 0x0002 56 54 #define BCM2835_I2C_CDIV_MAX 0xFFFE 57 - 58 - #define BCM2835_I2C_TIMEOUT (msecs_to_jiffies(1000)) 59 55 60 56 struct bcm2835_i2c_dev { 61 57 struct device *dev; 62 58 void __iomem *regs; 63 59 struct clk *clk; 64 60 int irq; 61 + u32 bus_clk_rate; 65 62 struct i2c_adapter adapter; 66 63 struct completion completion; 64 + struct i2c_msg *curr_msg; 65 + int num_msgs; 67 66 u32 msg_err; 68 67 u8 *msg_buf; 69 68 size_t msg_buf_remaining; ··· 77 78 static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg) 78 79 { 79 80 return readl(i2c_dev->regs + reg); 81 + } 82 + 83 + static int bcm2835_i2c_set_divider(struct bcm2835_i2c_dev *i2c_dev) 84 + { 85 + u32 divider; 86 + 87 + divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), 88 + i2c_dev->bus_clk_rate); 89 + /* 90 + * Per the datasheet, the register is always interpreted as an even 91 + * number, by rounding down. In other words, the LSB is ignored. So, 92 + * if the LSB is set, increment the divider to avoid any issue. 93 + */ 94 + if (divider & 1) 95 + divider++; 96 + if ((divider < BCM2835_I2C_CDIV_MIN) || 97 + (divider > BCM2835_I2C_CDIV_MAX)) { 98 + dev_err_ratelimited(i2c_dev->dev, "Invalid clock-frequency\n"); 99 + return -EINVAL; 100 + } 101 + 102 + bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider); 103 + 104 + return 0; 80 105 } 81 106 82 107 static void bcm2835_fill_txfifo(struct bcm2835_i2c_dev *i2c_dev) ··· 133 110 } 134 111 } 135 112 113 + /* 114 + * Repeated Start Condition (Sr) 115 + * The BCM2835 ARM Peripherals datasheet mentions a way to trigger a Sr when it 116 + * talks about reading from a slave with 10 bit address. This is achieved by 117 + * issuing a write, poll the I2CS.TA flag and wait for it to be set, and then 118 + * issue a read. 119 + * A comment in https://github.com/raspberrypi/linux/issues/254 shows how the 120 + * firmware actually does it using polling and says that it's a workaround for 121 + * a problem in the state machine. 122 + * It turns out that it is possible to use the TXW interrupt to know when the 123 + * transfer is active, provided the FIFO has not been prefilled. 124 + */ 125 + 126 + static void bcm2835_i2c_start_transfer(struct bcm2835_i2c_dev *i2c_dev) 127 + { 128 + u32 c = BCM2835_I2C_C_ST | BCM2835_I2C_C_I2CEN; 129 + struct i2c_msg *msg = i2c_dev->curr_msg; 130 + bool last_msg = (i2c_dev->num_msgs == 1); 131 + 132 + if (!i2c_dev->num_msgs) 133 + return; 134 + 135 + i2c_dev->num_msgs--; 136 + i2c_dev->msg_buf = msg->buf; 137 + i2c_dev->msg_buf_remaining = msg->len; 138 + 139 + if (msg->flags & I2C_M_RD) 140 + c |= BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR; 141 + else 142 + c |= BCM2835_I2C_C_INTT; 143 + 144 + if (last_msg) 145 + c |= BCM2835_I2C_C_INTD; 146 + 147 + bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr); 148 + bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len); 149 + bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c); 150 + } 151 + 152 + /* 153 + * Note about I2C_C_CLEAR on error: 154 + * The I2C_C_CLEAR on errors will take some time to resolve -- if you were in 155 + * non-idle state and I2C_C_READ, it sets an abort_rx flag and runs through 156 + * the state machine to send a NACK and a STOP. Since we're setting CLEAR 157 + * without I2CEN, that NACK will be hanging around queued up for next time 158 + * we start the engine. 159 + */ 160 + 136 161 static irqreturn_t bcm2835_i2c_isr(int this_irq, void *data) 137 162 { 138 163 struct bcm2835_i2c_dev *i2c_dev = data; 139 164 u32 val, err; 140 165 141 166 val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S); 142 - val &= BCM2835_I2C_BITMSK_S; 143 - bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, val); 144 167 145 168 err = val & (BCM2835_I2C_S_CLKT | BCM2835_I2C_S_ERR); 146 169 if (err) { 147 170 i2c_dev->msg_err = err; 148 - complete(&i2c_dev->completion); 149 - return IRQ_HANDLED; 150 - } 151 - 152 - if (val & BCM2835_I2C_S_RXD) { 153 - bcm2835_drain_rxfifo(i2c_dev); 154 - if (!(val & BCM2835_I2C_S_DONE)) 155 - return IRQ_HANDLED; 171 + goto complete; 156 172 } 157 173 158 174 if (val & BCM2835_I2C_S_DONE) { 159 - if (i2c_dev->msg_buf_remaining) 175 + if (i2c_dev->curr_msg->flags & I2C_M_RD) { 176 + bcm2835_drain_rxfifo(i2c_dev); 177 + val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S); 178 + } 179 + 180 + if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining) 160 181 i2c_dev->msg_err = BCM2835_I2C_S_LEN; 161 182 else 162 183 i2c_dev->msg_err = 0; 163 - complete(&i2c_dev->completion); 184 + goto complete; 185 + } 186 + 187 + if (val & BCM2835_I2C_S_TXW) { 188 + if (!i2c_dev->msg_buf_remaining) { 189 + i2c_dev->msg_err = val | BCM2835_I2C_S_LEN; 190 + goto complete; 191 + } 192 + 193 + bcm2835_fill_txfifo(i2c_dev); 194 + 195 + if (i2c_dev->num_msgs && !i2c_dev->msg_buf_remaining) { 196 + i2c_dev->curr_msg++; 197 + bcm2835_i2c_start_transfer(i2c_dev); 198 + } 199 + 164 200 return IRQ_HANDLED; 165 201 } 166 202 167 - if (val & BCM2835_I2C_S_TXD) { 168 - bcm2835_fill_txfifo(i2c_dev); 203 + if (val & BCM2835_I2C_S_RXR) { 204 + if (!i2c_dev->msg_buf_remaining) { 205 + i2c_dev->msg_err = val | BCM2835_I2C_S_LEN; 206 + goto complete; 207 + } 208 + 209 + bcm2835_drain_rxfifo(i2c_dev); 169 210 return IRQ_HANDLED; 170 211 } 171 212 172 213 return IRQ_NONE; 173 - } 174 214 175 - static int bcm2835_i2c_xfer_msg(struct bcm2835_i2c_dev *i2c_dev, 176 - struct i2c_msg *msg) 177 - { 178 - u32 c; 179 - unsigned long time_left; 180 - 181 - i2c_dev->msg_buf = msg->buf; 182 - i2c_dev->msg_buf_remaining = msg->len; 183 - reinit_completion(&i2c_dev->completion); 184 - 215 + complete: 185 216 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR); 217 + bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_S, BCM2835_I2C_S_CLKT | 218 + BCM2835_I2C_S_ERR | BCM2835_I2C_S_DONE); 219 + complete(&i2c_dev->completion); 186 220 187 - if (msg->flags & I2C_M_RD) { 188 - c = BCM2835_I2C_C_READ | BCM2835_I2C_C_INTR; 189 - } else { 190 - c = BCM2835_I2C_C_INTT; 191 - bcm2835_fill_txfifo(i2c_dev); 192 - } 193 - c |= BCM2835_I2C_C_ST | BCM2835_I2C_C_INTD | BCM2835_I2C_C_I2CEN; 194 - 195 - bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_A, msg->addr); 196 - bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DLEN, msg->len); 197 - bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, c); 198 - 199 - time_left = wait_for_completion_timeout(&i2c_dev->completion, 200 - BCM2835_I2C_TIMEOUT); 201 - bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR); 202 - if (!time_left) { 203 - dev_err(i2c_dev->dev, "i2c transfer timed out\n"); 204 - return -ETIMEDOUT; 205 - } 206 - 207 - if (likely(!i2c_dev->msg_err)) 208 - return 0; 209 - 210 - if ((i2c_dev->msg_err & BCM2835_I2C_S_ERR) && 211 - (msg->flags & I2C_M_IGNORE_NAK)) 212 - return 0; 213 - 214 - dev_err(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err); 215 - 216 - if (i2c_dev->msg_err & BCM2835_I2C_S_ERR) 217 - return -EREMOTEIO; 218 - else 219 - return -EIO; 221 + return IRQ_HANDLED; 220 222 } 221 223 222 224 static int bcm2835_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], 223 225 int num) 224 226 { 225 227 struct bcm2835_i2c_dev *i2c_dev = i2c_get_adapdata(adap); 226 - int i; 227 - int ret = 0; 228 + unsigned long time_left; 229 + int i, ret; 228 230 229 - for (i = 0; i < num; i++) { 230 - ret = bcm2835_i2c_xfer_msg(i2c_dev, &msgs[i]); 231 - if (ret) 232 - break; 231 + for (i = 0; i < (num - 1); i++) 232 + if (msgs[i].flags & I2C_M_RD) { 233 + dev_warn_once(i2c_dev->dev, 234 + "only one read message supported, has to be last\n"); 235 + return -EOPNOTSUPP; 236 + } 237 + 238 + ret = bcm2835_i2c_set_divider(i2c_dev); 239 + if (ret) 240 + return ret; 241 + 242 + i2c_dev->curr_msg = msgs; 243 + i2c_dev->num_msgs = num; 244 + reinit_completion(&i2c_dev->completion); 245 + 246 + bcm2835_i2c_start_transfer(i2c_dev); 247 + 248 + time_left = wait_for_completion_timeout(&i2c_dev->completion, 249 + adap->timeout); 250 + if (!time_left) { 251 + bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 252 + BCM2835_I2C_C_CLEAR); 253 + dev_err(i2c_dev->dev, "i2c transfer timed out\n"); 254 + return -ETIMEDOUT; 233 255 } 234 256 235 - return ret ?: i; 257 + if (!i2c_dev->msg_err) 258 + return num; 259 + 260 + dev_dbg(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err); 261 + 262 + if (i2c_dev->msg_err & BCM2835_I2C_S_ERR) 263 + return -EREMOTEIO; 264 + 265 + return -EIO; 236 266 } 237 267 238 268 static u32 bcm2835_i2c_func(struct i2c_adapter *adap) ··· 311 235 { 312 236 struct bcm2835_i2c_dev *i2c_dev; 313 237 struct resource *mem, *irq; 314 - u32 bus_clk_rate, divider; 315 238 int ret; 316 239 struct i2c_adapter *adap; 317 240 ··· 334 259 } 335 260 336 261 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency", 337 - &bus_clk_rate); 262 + &i2c_dev->bus_clk_rate); 338 263 if (ret < 0) { 339 264 dev_warn(&pdev->dev, 340 265 "Could not read clock-frequency property\n"); 341 - bus_clk_rate = 100000; 266 + i2c_dev->bus_clk_rate = 100000; 342 267 } 343 - 344 - divider = DIV_ROUND_UP(clk_get_rate(i2c_dev->clk), bus_clk_rate); 345 - /* 346 - * Per the datasheet, the register is always interpreted as an even 347 - * number, by rounding down. In other words, the LSB is ignored. So, 348 - * if the LSB is set, increment the divider to avoid any issue. 349 - */ 350 - if (divider & 1) 351 - divider++; 352 - if ((divider < BCM2835_I2C_CDIV_MIN) || 353 - (divider > BCM2835_I2C_CDIV_MAX)) { 354 - dev_err(&pdev->dev, "Invalid clock-frequency\n"); 355 - return -ENODEV; 356 - } 357 - bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_DIV, divider); 358 268 359 269 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 360 270 if (!irq) {
+43 -3
drivers/i2c/busses/i2c-designware-core.c
··· 536 536 intr_mask = DW_IC_INTR_DEFAULT_MASK; 537 537 538 538 for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) { 539 + u32 flags = msgs[dev->msg_write_idx].flags; 540 + 539 541 /* 540 542 * if target address has changed, we need to 541 543 * reprogram the target address in the i2c ··· 583 581 * detected from the registers so we set it always 584 582 * when writing/reading the last byte. 585 583 */ 584 + 585 + /* 586 + * i2c-core.c always sets the buffer length of 587 + * I2C_FUNC_SMBUS_BLOCK_DATA to 1. The length will 588 + * be adjusted when receiving the first byte. 589 + * Thus we can't stop the transaction here. 590 + */ 586 591 if (dev->msg_write_idx == dev->msgs_num - 1 && 587 - buf_len == 1) 592 + buf_len == 1 && !(flags & I2C_M_RECV_LEN)) 588 593 cmd |= BIT(9); 589 594 590 595 if (need_restart) { ··· 616 607 dev->tx_buf = buf; 617 608 dev->tx_buf_len = buf_len; 618 609 619 - if (buf_len > 0) { 610 + /* 611 + * Because we don't know the buffer length in the 612 + * I2C_FUNC_SMBUS_BLOCK_DATA case, we can't stop 613 + * the transaction here. 614 + */ 615 + if (buf_len > 0 || flags & I2C_M_RECV_LEN) { 620 616 /* more bytes to be written */ 621 617 dev->status |= STATUS_WRITE_IN_PROGRESS; 622 618 break; ··· 640 626 intr_mask = 0; 641 627 642 628 dw_writel(dev, intr_mask, DW_IC_INTR_MASK); 629 + } 630 + 631 + static u8 632 + i2c_dw_recv_len(struct dw_i2c_dev *dev, u8 len) 633 + { 634 + struct i2c_msg *msgs = dev->msgs; 635 + u32 flags = msgs[dev->msg_read_idx].flags; 636 + 637 + /* 638 + * Adjust the buffer length and mask the flag 639 + * after receiving the first byte. 640 + */ 641 + len += (flags & I2C_CLIENT_PEC) ? 2 : 1; 642 + dev->tx_buf_len = len - min_t(u8, len, dev->rx_outstanding); 643 + msgs[dev->msg_read_idx].len = len; 644 + msgs[dev->msg_read_idx].flags &= ~I2C_M_RECV_LEN; 645 + 646 + return len; 643 647 } 644 648 645 649 static void ··· 684 652 rx_valid = dw_readl(dev, DW_IC_RXFLR); 685 653 686 654 for (; len > 0 && rx_valid > 0; len--, rx_valid--) { 687 - *buf++ = dw_readl(dev, DW_IC_DATA_CMD); 655 + u32 flags = msgs[dev->msg_read_idx].flags; 656 + 657 + *buf = dw_readl(dev, DW_IC_DATA_CMD); 658 + /* Ensure length byte is a valid value */ 659 + if (flags & I2C_M_RECV_LEN && 660 + *buf <= I2C_SMBUS_BLOCK_MAX && *buf > 0) { 661 + len = i2c_dw_recv_len(dev, *buf); 662 + } 663 + buf++; 688 664 dev->rx_outstanding--; 689 665 } 690 666
+8
drivers/i2c/busses/i2c-designware-core.h
··· 22 22 * 23 23 */ 24 24 25 + #include <linux/i2c.h> 26 + 27 + #define DW_IC_DEFAULT_FUNCTIONALITY (I2C_FUNC_I2C | \ 28 + I2C_FUNC_SMBUS_BYTE | \ 29 + I2C_FUNC_SMBUS_BYTE_DATA | \ 30 + I2C_FUNC_SMBUS_WORD_DATA | \ 31 + I2C_FUNC_SMBUS_BLOCK_DATA | \ 32 + I2C_FUNC_SMBUS_I2C_BLOCK) 25 33 26 34 #define DW_IC_CON_MASTER 0x1 27 35 #define DW_IC_CON_SPEED_STD 0x2
+3 -7
drivers/i2c/busses/i2c-designware-pcidrv.c
··· 71 71 DW_IC_CON_SLAVE_DISABLE | \ 72 72 DW_IC_CON_RESTART_EN) 73 73 74 - #define DW_DEFAULT_FUNCTIONALITY (I2C_FUNC_I2C | \ 75 - I2C_FUNC_SMBUS_BYTE | \ 76 - I2C_FUNC_SMBUS_BYTE_DATA | \ 77 - I2C_FUNC_SMBUS_WORD_DATA | \ 78 - I2C_FUNC_SMBUS_I2C_BLOCK) 79 - 80 74 /* Merrifield HCNT/LCNT/SDA hold time */ 81 75 static struct dw_scl_sda_cfg mrfld_config = { 82 76 .ss_hcnt = 0x2f8, ··· 141 147 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST, 142 148 .tx_fifo_depth = 32, 143 149 .rx_fifo_depth = 32, 150 + .functionality = I2C_FUNC_10BIT_ADDR, 144 151 .clk_khz = 25000, 145 152 .setup = mfld_setup, 146 153 }, ··· 150 155 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST, 151 156 .tx_fifo_depth = 64, 152 157 .rx_fifo_depth = 64, 158 + .functionality = I2C_FUNC_10BIT_ADDR, 153 159 .scl_sda_cfg = &mrfld_config, 154 160 .setup = mrfld_setup, 155 161 }, ··· 245 249 } 246 250 247 251 dev->functionality = controller->functionality | 248 - DW_DEFAULT_FUNCTIONALITY; 252 + DW_IC_DEFAULT_FUNCTIONALITY; 249 253 250 254 dev->master_cfg = controller->bus_cfg; 251 255 if (controller->scl_sda_cfg) {
+11 -12
drivers/i2c/busses/i2c-designware-platdrv.c
··· 176 176 dev->irq = irq; 177 177 platform_set_drvdata(pdev, dev); 178 178 179 - /* fast mode by default because of legacy reasons */ 180 - dev->clk_freq = 400000; 181 - 182 179 if (pdata) { 183 180 dev->clk_freq = pdata->i2c_scl_freq; 184 181 } else { ··· 190 193 } 191 194 192 195 acpi_speed = i2c_acpi_find_bus_speed(&pdev->dev); 193 - if (acpi_speed) 194 - dev->clk_freq = acpi_speed; 196 + /* 197 + * Find bus speed from the "clock-frequency" device property, ACPI 198 + * or by using fast mode if neither is set. 199 + */ 200 + if (acpi_speed && dev->clk_freq) 201 + dev->clk_freq = min(dev->clk_freq, acpi_speed); 202 + else if (acpi_speed || dev->clk_freq) 203 + dev->clk_freq = max(dev->clk_freq, acpi_speed); 204 + else 205 + dev->clk_freq = 400000; 195 206 196 207 if (has_acpi_companion(&pdev->dev)) 197 208 dw_i2c_acpi_configure(pdev); ··· 219 214 if (r) 220 215 return r; 221 216 222 - dev->functionality = 223 - I2C_FUNC_I2C | 224 - I2C_FUNC_10BIT_ADDR | 225 - I2C_FUNC_SMBUS_BYTE | 226 - I2C_FUNC_SMBUS_BYTE_DATA | 227 - I2C_FUNC_SMBUS_WORD_DATA | 228 - I2C_FUNC_SMBUS_I2C_BLOCK; 217 + dev->functionality = I2C_FUNC_10BIT_ADDR | DW_IC_DEFAULT_FUNCTIONALITY; 229 218 230 219 dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE | 231 220 DW_IC_CON_RESTART_EN;
+1 -1
drivers/i2c/busses/i2c-dln2.c
··· 182 182 .functionality = dln2_i2c_func, 183 183 }; 184 184 185 - static struct i2c_adapter_quirks dln2_i2c_quirks = { 185 + static const struct i2c_adapter_quirks dln2_i2c_quirks = { 186 186 .max_read_len = DLN2_I2C_MAX_XFER_SIZE, 187 187 .max_write_len = DLN2_I2C_MAX_XFER_SIZE, 188 188 };
+60 -63
drivers/i2c/busses/i2c-i801.c
··· 118 118 #define SMBSLVSTS(p) (16 + (p)->smba) /* ICH3 and later */ 119 119 #define SMBSLVCMD(p) (17 + (p)->smba) /* ICH3 and later */ 120 120 #define SMBNTFDADD(p) (20 + (p)->smba) /* ICH3 and later */ 121 - #define SMBNTFDDAT(p) (22 + (p)->smba) /* ICH3 and later */ 122 121 123 122 /* PCI Address Constants */ 124 123 #define SMBBAR 4 ··· 136 137 #define SBREG_SMBCTRL 0xc6000c 137 138 138 139 /* Host status bits for SMBPCISTS */ 139 - #define SMBPCISTS_INTS 0x08 140 + #define SMBPCISTS_INTS BIT(3) 140 141 141 142 /* Control bits for SMBPCICTL */ 142 - #define SMBPCICTL_INTDIS 0x0400 143 + #define SMBPCICTL_INTDIS BIT(10) 143 144 144 145 /* Host configuration bits for SMBHSTCFG */ 145 - #define SMBHSTCFG_HST_EN 1 146 - #define SMBHSTCFG_SMB_SMI_EN 2 147 - #define SMBHSTCFG_I2C_EN 4 148 - #define SMBHSTCFG_SPD_WD 0x10 146 + #define SMBHSTCFG_HST_EN BIT(0) 147 + #define SMBHSTCFG_SMB_SMI_EN BIT(1) 148 + #define SMBHSTCFG_I2C_EN BIT(2) 149 + #define SMBHSTCFG_SPD_WD BIT(4) 149 150 150 151 /* TCO configuration bits for TCOCTL */ 151 - #define TCOCTL_EN 0x0100 152 + #define TCOCTL_EN BIT(8) 152 153 153 154 /* Auxiliary status register bits, ICH4+ only */ 154 - #define SMBAUXSTS_CRCE 1 155 - #define SMBAUXSTS_STCO 2 155 + #define SMBAUXSTS_CRCE BIT(0) 156 + #define SMBAUXSTS_STCO BIT(1) 156 157 157 158 /* Auxiliary control register bits, ICH4+ only */ 158 - #define SMBAUXCTL_CRC 1 159 - #define SMBAUXCTL_E32B 2 159 + #define SMBAUXCTL_CRC BIT(0) 160 + #define SMBAUXCTL_E32B BIT(1) 160 161 161 162 /* Other settings */ 162 163 #define MAX_RETRIES 400 ··· 171 172 #define I801_I2C_BLOCK_DATA 0x18 /* ICH5 and later */ 172 173 173 174 /* I801 Host Control register bits */ 174 - #define SMBHSTCNT_INTREN 0x01 175 - #define SMBHSTCNT_KILL 0x02 176 - #define SMBHSTCNT_LAST_BYTE 0x20 177 - #define SMBHSTCNT_START 0x40 178 - #define SMBHSTCNT_PEC_EN 0x80 /* ICH3 and later */ 175 + #define SMBHSTCNT_INTREN BIT(0) 176 + #define SMBHSTCNT_KILL BIT(1) 177 + #define SMBHSTCNT_LAST_BYTE BIT(5) 178 + #define SMBHSTCNT_START BIT(6) 179 + #define SMBHSTCNT_PEC_EN BIT(7) /* ICH3 and later */ 179 180 180 181 /* I801 Hosts Status register bits */ 181 - #define SMBHSTSTS_BYTE_DONE 0x80 182 - #define SMBHSTSTS_INUSE_STS 0x40 183 - #define SMBHSTSTS_SMBALERT_STS 0x20 184 - #define SMBHSTSTS_FAILED 0x10 185 - #define SMBHSTSTS_BUS_ERR 0x08 186 - #define SMBHSTSTS_DEV_ERR 0x04 187 - #define SMBHSTSTS_INTR 0x02 188 - #define SMBHSTSTS_HOST_BUSY 0x01 182 + #define SMBHSTSTS_BYTE_DONE BIT(7) 183 + #define SMBHSTSTS_INUSE_STS BIT(6) 184 + #define SMBHSTSTS_SMBALERT_STS BIT(5) 185 + #define SMBHSTSTS_FAILED BIT(4) 186 + #define SMBHSTSTS_BUS_ERR BIT(3) 187 + #define SMBHSTSTS_DEV_ERR BIT(2) 188 + #define SMBHSTSTS_INTR BIT(1) 189 + #define SMBHSTSTS_HOST_BUSY BIT(0) 189 190 190 - /* Host Notify Status registers bits */ 191 - #define SMBSLVSTS_HST_NTFY_STS 1 191 + /* Host Notify Status register bits */ 192 + #define SMBSLVSTS_HST_NTFY_STS BIT(0) 192 193 193 - /* Host Notify Command registers bits */ 194 - #define SMBSLVCMD_HST_NTFY_INTREN 0x01 194 + /* Host Notify Command register bits */ 195 + #define SMBSLVCMD_HST_NTFY_INTREN BIT(0) 195 196 196 197 #define STATUS_ERROR_FLAGS (SMBHSTSTS_FAILED | SMBHSTSTS_BUS_ERR | \ 197 198 SMBHSTSTS_DEV_ERR) ··· 242 243 struct i2c_adapter adapter; 243 244 unsigned long smba; 244 245 unsigned char original_hstcfg; 246 + unsigned char original_slvcmd; 245 247 struct pci_dev *pci_dev; 246 248 unsigned int features; 247 249 ··· 269 269 */ 270 270 bool acpi_reserved; 271 271 struct mutex acpi_lock; 272 - struct smbus_host_notify *host_notify; 273 272 }; 274 273 275 - #define SMBHSTNTFY_SIZE 8 276 - 277 - #define FEATURE_SMBUS_PEC (1 << 0) 278 - #define FEATURE_BLOCK_BUFFER (1 << 1) 279 - #define FEATURE_BLOCK_PROC (1 << 2) 280 - #define FEATURE_I2C_BLOCK_READ (1 << 3) 281 - #define FEATURE_IRQ (1 << 4) 282 - #define FEATURE_HOST_NOTIFY (1 << 5) 274 + #define FEATURE_SMBUS_PEC BIT(0) 275 + #define FEATURE_BLOCK_BUFFER BIT(1) 276 + #define FEATURE_BLOCK_PROC BIT(2) 277 + #define FEATURE_I2C_BLOCK_READ BIT(3) 278 + #define FEATURE_IRQ BIT(4) 279 + #define FEATURE_HOST_NOTIFY BIT(5) 283 280 /* Not really a feature, but it's convenient to handle it as such */ 284 - #define FEATURE_IDF (1 << 15) 285 - #define FEATURE_TCO (1 << 16) 281 + #define FEATURE_IDF BIT(15) 282 + #define FEATURE_TCO BIT(16) 286 283 287 284 static const char *i801_feature_names[] = { 288 285 "SMBus PEC", ··· 579 582 static irqreturn_t i801_host_notify_isr(struct i801_priv *priv) 580 583 { 581 584 unsigned short addr; 582 - unsigned int data; 583 585 584 586 addr = inb_p(SMBNTFDADD(priv)) >> 1; 585 - data = inw_p(SMBNTFDDAT(priv)); 586 587 587 - i2c_handle_smbus_host_notify(priv->host_notify, addr, data); 588 + /* 589 + * With the tested platforms, reading SMBNTFDDAT (22 + (p)->smba) 590 + * always returns 0. Our current implementation doesn't provide 591 + * data, so we just ignore it. 592 + */ 593 + i2c_handle_smbus_host_notify(&priv->adapter, addr); 588 594 589 595 /* clear Host Notify bit and return */ 590 596 outb_p(SMBSLVSTS_HST_NTFY_STS, SMBSLVSTS(priv)); ··· 950 950 I2C_FUNC_SMBUS_HOST_NOTIFY : 0); 951 951 } 952 952 953 - static int i801_enable_host_notify(struct i2c_adapter *adapter) 953 + static void i801_enable_host_notify(struct i2c_adapter *adapter) 954 954 { 955 955 struct i801_priv *priv = i2c_get_adapdata(adapter); 956 956 957 957 if (!(priv->features & FEATURE_HOST_NOTIFY)) 958 - return -ENOTSUPP; 958 + return; 959 959 960 - if (!priv->host_notify) 961 - priv->host_notify = i2c_setup_smbus_host_notify(adapter); 962 - if (!priv->host_notify) 963 - return -ENOMEM; 960 + priv->original_slvcmd = inb_p(SMBSLVCMD(priv)); 964 961 965 - outb_p(SMBSLVCMD_HST_NTFY_INTREN, SMBSLVCMD(priv)); 962 + if (!(SMBSLVCMD_HST_NTFY_INTREN & priv->original_slvcmd)) 963 + outb_p(SMBSLVCMD_HST_NTFY_INTREN | priv->original_slvcmd, 964 + SMBSLVCMD(priv)); 965 + 966 966 /* clear Host Notify bit to allow a new notification */ 967 967 outb_p(SMBSLVSTS_HST_NTFY_STS, SMBSLVSTS(priv)); 968 + } 968 969 969 - return 0; 970 + static void i801_disable_host_notify(struct i801_priv *priv) 971 + { 972 + if (!(priv->features & FEATURE_HOST_NOTIFY)) 973 + return; 974 + 975 + outb_p(priv->original_slvcmd, SMBSLVCMD(priv)); 970 976 } 971 977 972 978 static const struct i2c_algorithm smbus_algorithm = { ··· 1639 1633 return err; 1640 1634 } 1641 1635 1642 - /* 1643 - * Enable Host Notify for chips that supports it. 1644 - * It is done after i2c_add_adapter() so that we are sure the work queue 1645 - * is not used if i2c_add_adapter() fails. 1646 - */ 1647 - err = i801_enable_host_notify(&priv->adapter); 1648 - if (err && err != -ENOTSUPP) 1649 - dev_warn(&dev->dev, "Unable to enable SMBus Host Notify\n"); 1636 + i801_enable_host_notify(&priv->adapter); 1650 1637 1651 1638 i801_probe_optional_slaves(priv); 1652 1639 /* We ignore errors - multiplexing is optional */ ··· 1662 1663 pm_runtime_forbid(&dev->dev); 1663 1664 pm_runtime_get_noresume(&dev->dev); 1664 1665 1666 + i801_disable_host_notify(priv); 1665 1667 i801_del_mux(priv); 1666 1668 i2c_del_adapter(&priv->adapter); 1667 1669 i801_acpi_remove(priv); ··· 1690 1690 { 1691 1691 struct pci_dev *pci_dev = to_pci_dev(dev); 1692 1692 struct i801_priv *priv = pci_get_drvdata(pci_dev); 1693 - int err; 1694 1693 1695 - err = i801_enable_host_notify(&priv->adapter); 1696 - if (err && err != -ENOTSUPP) 1697 - dev_warn(dev, "Unable to enable SMBus Host Notify\n"); 1694 + i801_enable_host_notify(&priv->adapter); 1698 1695 1699 1696 return 0; 1700 1697 }
+652
drivers/i2c/busses/i2c-imx-lpi2c.c
··· 1 + /* 2 + * This is i.MX low power i2c controller driver. 3 + * 4 + * Copyright 2016 Freescale Semiconductor, Inc. 5 + * 6 + * This program is free software; you can redistribute it and/or 7 + * modify it under the terms of the GNU General Public License 8 + * as published by the Free Software Foundation; either version 2 9 + * of the License, or (at your option) any later version. 10 + * 11 + * This program is distributed in the hope that it will be useful, 12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + * GNU General Public License for more details. 15 + * 16 + */ 17 + 18 + #include <linux/clk.h> 19 + #include <linux/completion.h> 20 + #include <linux/delay.h> 21 + #include <linux/err.h> 22 + #include <linux/errno.h> 23 + #include <linux/i2c.h> 24 + #include <linux/init.h> 25 + #include <linux/interrupt.h> 26 + #include <linux/io.h> 27 + #include <linux/kernel.h> 28 + #include <linux/module.h> 29 + #include <linux/of.h> 30 + #include <linux/of_device.h> 31 + #include <linux/platform_device.h> 32 + #include <linux/sched.h> 33 + #include <linux/slab.h> 34 + 35 + #define DRIVER_NAME "imx-lpi2c" 36 + 37 + #define LPI2C_PARAM 0x04 /* i2c RX/TX FIFO size */ 38 + #define LPI2C_MCR 0x10 /* i2c contrl register */ 39 + #define LPI2C_MSR 0x14 /* i2c status register */ 40 + #define LPI2C_MIER 0x18 /* i2c interrupt enable */ 41 + #define LPI2C_MCFGR0 0x20 /* i2c master configuration */ 42 + #define LPI2C_MCFGR1 0x24 /* i2c master configuration */ 43 + #define LPI2C_MCFGR2 0x28 /* i2c master configuration */ 44 + #define LPI2C_MCFGR3 0x2C /* i2c master configuration */ 45 + #define LPI2C_MCCR0 0x48 /* i2c master clk configuration */ 46 + #define LPI2C_MCCR1 0x50 /* i2c master clk configuration */ 47 + #define LPI2C_MFCR 0x58 /* i2c master FIFO control */ 48 + #define LPI2C_MFSR 0x5C /* i2c master FIFO status */ 49 + #define LPI2C_MTDR 0x60 /* i2c master TX data register */ 50 + #define LPI2C_MRDR 0x70 /* i2c master RX data register */ 51 + 52 + /* i2c command */ 53 + #define TRAN_DATA 0X00 54 + #define RECV_DATA 0X01 55 + #define GEN_STOP 0X02 56 + #define RECV_DISCARD 0X03 57 + #define GEN_START 0X04 58 + #define START_NACK 0X05 59 + #define START_HIGH 0X06 60 + #define START_HIGH_NACK 0X07 61 + 62 + #define MCR_MEN BIT(0) 63 + #define MCR_RST BIT(1) 64 + #define MCR_DOZEN BIT(2) 65 + #define MCR_DBGEN BIT(3) 66 + #define MCR_RTF BIT(8) 67 + #define MCR_RRF BIT(9) 68 + #define MSR_TDF BIT(0) 69 + #define MSR_RDF BIT(1) 70 + #define MSR_SDF BIT(9) 71 + #define MSR_NDF BIT(10) 72 + #define MSR_ALF BIT(11) 73 + #define MSR_MBF BIT(24) 74 + #define MSR_BBF BIT(25) 75 + #define MIER_TDIE BIT(0) 76 + #define MIER_RDIE BIT(1) 77 + #define MIER_SDIE BIT(9) 78 + #define MIER_NDIE BIT(10) 79 + #define MCFGR1_AUTOSTOP BIT(8) 80 + #define MCFGR1_IGNACK BIT(9) 81 + #define MRDR_RXEMPTY BIT(14) 82 + 83 + #define I2C_CLK_RATIO 2 84 + #define CHUNK_DATA 256 85 + 86 + #define LPI2C_DEFAULT_RATE 100000 87 + #define STARDARD_MAX_BITRATE 400000 88 + #define FAST_MAX_BITRATE 1000000 89 + #define FAST_PLUS_MAX_BITRATE 3400000 90 + #define HIGHSPEED_MAX_BITRATE 5000000 91 + 92 + enum lpi2c_imx_mode { 93 + STANDARD, /* 100+Kbps */ 94 + FAST, /* 400+Kbps */ 95 + FAST_PLUS, /* 1.0+Mbps */ 96 + HS, /* 3.4+Mbps */ 97 + ULTRA_FAST, /* 5.0+Mbps */ 98 + }; 99 + 100 + enum lpi2c_imx_pincfg { 101 + TWO_PIN_OD, 102 + TWO_PIN_OO, 103 + TWO_PIN_PP, 104 + FOUR_PIN_PP, 105 + }; 106 + 107 + struct lpi2c_imx_struct { 108 + struct i2c_adapter adapter; 109 + struct clk *clk; 110 + void __iomem *base; 111 + __u8 *rx_buf; 112 + __u8 *tx_buf; 113 + struct completion complete; 114 + unsigned int msglen; 115 + unsigned int delivered; 116 + unsigned int block_data; 117 + unsigned int bitrate; 118 + unsigned int txfifosize; 119 + unsigned int rxfifosize; 120 + enum lpi2c_imx_mode mode; 121 + }; 122 + 123 + static void lpi2c_imx_intctrl(struct lpi2c_imx_struct *lpi2c_imx, 124 + unsigned int enable) 125 + { 126 + writel(enable, lpi2c_imx->base + LPI2C_MIER); 127 + } 128 + 129 + static int lpi2c_imx_bus_busy(struct lpi2c_imx_struct *lpi2c_imx) 130 + { 131 + unsigned long orig_jiffies = jiffies; 132 + unsigned int temp; 133 + 134 + while (1) { 135 + temp = readl(lpi2c_imx->base + LPI2C_MSR); 136 + 137 + /* check for arbitration lost, clear if set */ 138 + if (temp & MSR_ALF) { 139 + writel(temp, lpi2c_imx->base + LPI2C_MSR); 140 + return -EAGAIN; 141 + } 142 + 143 + if (temp & (MSR_BBF | MSR_MBF)) 144 + break; 145 + 146 + if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) { 147 + dev_dbg(&lpi2c_imx->adapter.dev, "bus not work\n"); 148 + return -ETIMEDOUT; 149 + } 150 + schedule(); 151 + } 152 + 153 + return 0; 154 + } 155 + 156 + static void lpi2c_imx_set_mode(struct lpi2c_imx_struct *lpi2c_imx) 157 + { 158 + unsigned int bitrate = lpi2c_imx->bitrate; 159 + enum lpi2c_imx_mode mode; 160 + 161 + if (bitrate < STARDARD_MAX_BITRATE) 162 + mode = STANDARD; 163 + else if (bitrate < FAST_MAX_BITRATE) 164 + mode = FAST; 165 + else if (bitrate < FAST_PLUS_MAX_BITRATE) 166 + mode = FAST_PLUS; 167 + else if (bitrate < HIGHSPEED_MAX_BITRATE) 168 + mode = HS; 169 + else 170 + mode = ULTRA_FAST; 171 + 172 + lpi2c_imx->mode = mode; 173 + } 174 + 175 + static int lpi2c_imx_start(struct lpi2c_imx_struct *lpi2c_imx, 176 + struct i2c_msg *msgs) 177 + { 178 + unsigned int temp; 179 + u8 read; 180 + 181 + temp = readl(lpi2c_imx->base + LPI2C_MCR); 182 + temp |= MCR_RRF | MCR_RTF; 183 + writel(temp, lpi2c_imx->base + LPI2C_MCR); 184 + writel(0x7f00, lpi2c_imx->base + LPI2C_MSR); 185 + 186 + read = msgs->flags & I2C_M_RD; 187 + temp = (msgs->addr << 1 | read) | (GEN_START << 8); 188 + writel(temp, lpi2c_imx->base + LPI2C_MTDR); 189 + 190 + return lpi2c_imx_bus_busy(lpi2c_imx); 191 + } 192 + 193 + static void lpi2c_imx_stop(struct lpi2c_imx_struct *lpi2c_imx) 194 + { 195 + unsigned long orig_jiffies = jiffies; 196 + unsigned int temp; 197 + 198 + writel(GEN_STOP << 8, lpi2c_imx->base + LPI2C_MTDR); 199 + 200 + do { 201 + temp = readl(lpi2c_imx->base + LPI2C_MSR); 202 + if (temp & MSR_SDF) 203 + break; 204 + 205 + if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) { 206 + dev_dbg(&lpi2c_imx->adapter.dev, "stop timeout\n"); 207 + break; 208 + } 209 + schedule(); 210 + 211 + } while (1); 212 + } 213 + 214 + /* CLKLO = I2C_CLK_RATIO * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI/2 */ 215 + static int lpi2c_imx_config(struct lpi2c_imx_struct *lpi2c_imx) 216 + { 217 + u8 prescale, filt, sethold, clkhi, clklo, datavd; 218 + unsigned int clk_rate, clk_cycle; 219 + enum lpi2c_imx_pincfg pincfg; 220 + unsigned int temp; 221 + 222 + lpi2c_imx_set_mode(lpi2c_imx); 223 + 224 + clk_rate = clk_get_rate(lpi2c_imx->clk); 225 + if (lpi2c_imx->mode == HS || lpi2c_imx->mode == ULTRA_FAST) 226 + filt = 0; 227 + else 228 + filt = 2; 229 + 230 + for (prescale = 0; prescale <= 7; prescale++) { 231 + clk_cycle = clk_rate / ((1 << prescale) * lpi2c_imx->bitrate) 232 + - 3 - (filt >> 1); 233 + clkhi = (clk_cycle + I2C_CLK_RATIO) / (I2C_CLK_RATIO + 1); 234 + clklo = clk_cycle - clkhi; 235 + if (clklo < 64) 236 + break; 237 + } 238 + 239 + if (prescale > 7) 240 + return -EINVAL; 241 + 242 + /* set MCFGR1: PINCFG, PRESCALE, IGNACK */ 243 + if (lpi2c_imx->mode == ULTRA_FAST) 244 + pincfg = TWO_PIN_OO; 245 + else 246 + pincfg = TWO_PIN_OD; 247 + temp = prescale | pincfg << 24; 248 + 249 + if (lpi2c_imx->mode == ULTRA_FAST) 250 + temp |= MCFGR1_IGNACK; 251 + 252 + writel(temp, lpi2c_imx->base + LPI2C_MCFGR1); 253 + 254 + /* set MCFGR2: FILTSDA, FILTSCL */ 255 + temp = (filt << 16) | (filt << 24); 256 + writel(temp, lpi2c_imx->base + LPI2C_MCFGR2); 257 + 258 + /* set MCCR: DATAVD, SETHOLD, CLKHI, CLKLO */ 259 + sethold = clkhi; 260 + datavd = clkhi >> 1; 261 + temp = datavd << 24 | sethold << 16 | clkhi << 8 | clklo; 262 + 263 + if (lpi2c_imx->mode == HS) 264 + writel(temp, lpi2c_imx->base + LPI2C_MCCR1); 265 + else 266 + writel(temp, lpi2c_imx->base + LPI2C_MCCR0); 267 + 268 + return 0; 269 + } 270 + 271 + static int lpi2c_imx_master_enable(struct lpi2c_imx_struct *lpi2c_imx) 272 + { 273 + unsigned int temp; 274 + int ret; 275 + 276 + ret = clk_enable(lpi2c_imx->clk); 277 + if (ret) 278 + return ret; 279 + 280 + temp = MCR_RST; 281 + writel(temp, lpi2c_imx->base + LPI2C_MCR); 282 + writel(0, lpi2c_imx->base + LPI2C_MCR); 283 + 284 + ret = lpi2c_imx_config(lpi2c_imx); 285 + if (ret) 286 + goto clk_disable; 287 + 288 + temp = readl(lpi2c_imx->base + LPI2C_MCR); 289 + temp |= MCR_MEN; 290 + writel(temp, lpi2c_imx->base + LPI2C_MCR); 291 + 292 + return 0; 293 + 294 + clk_disable: 295 + clk_disable(lpi2c_imx->clk); 296 + 297 + return ret; 298 + } 299 + 300 + static int lpi2c_imx_master_disable(struct lpi2c_imx_struct *lpi2c_imx) 301 + { 302 + u32 temp; 303 + 304 + temp = readl(lpi2c_imx->base + LPI2C_MCR); 305 + temp &= ~MCR_MEN; 306 + writel(temp, lpi2c_imx->base + LPI2C_MCR); 307 + 308 + clk_disable(lpi2c_imx->clk); 309 + 310 + return 0; 311 + } 312 + 313 + static int lpi2c_imx_msg_complete(struct lpi2c_imx_struct *lpi2c_imx) 314 + { 315 + unsigned long timeout; 316 + 317 + timeout = wait_for_completion_timeout(&lpi2c_imx->complete, HZ); 318 + 319 + return timeout ? 0 : -ETIMEDOUT; 320 + } 321 + 322 + static int lpi2c_imx_txfifo_empty(struct lpi2c_imx_struct *lpi2c_imx) 323 + { 324 + unsigned long orig_jiffies = jiffies; 325 + u32 txcnt; 326 + 327 + do { 328 + txcnt = readl(lpi2c_imx->base + LPI2C_MFSR) & 0xff; 329 + 330 + if (readl(lpi2c_imx->base + LPI2C_MSR) & MSR_NDF) { 331 + dev_dbg(&lpi2c_imx->adapter.dev, "NDF detected\n"); 332 + return -EIO; 333 + } 334 + 335 + if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) { 336 + dev_dbg(&lpi2c_imx->adapter.dev, "txfifo empty timeout\n"); 337 + return -ETIMEDOUT; 338 + } 339 + schedule(); 340 + 341 + } while (txcnt); 342 + 343 + return 0; 344 + } 345 + 346 + static void lpi2c_imx_set_tx_watermark(struct lpi2c_imx_struct *lpi2c_imx) 347 + { 348 + writel(lpi2c_imx->txfifosize >> 1, lpi2c_imx->base + LPI2C_MFCR); 349 + } 350 + 351 + static void lpi2c_imx_set_rx_watermark(struct lpi2c_imx_struct *lpi2c_imx) 352 + { 353 + unsigned int temp, remaining; 354 + 355 + remaining = lpi2c_imx->msglen - lpi2c_imx->delivered; 356 + 357 + if (remaining > (lpi2c_imx->rxfifosize >> 1)) 358 + temp = lpi2c_imx->rxfifosize >> 1; 359 + else 360 + temp = 0; 361 + 362 + writel(temp << 16, lpi2c_imx->base + LPI2C_MFCR); 363 + } 364 + 365 + static void lpi2c_imx_write_txfifo(struct lpi2c_imx_struct *lpi2c_imx) 366 + { 367 + unsigned int data, txcnt; 368 + 369 + txcnt = readl(lpi2c_imx->base + LPI2C_MFSR) & 0xff; 370 + 371 + while (txcnt < lpi2c_imx->txfifosize) { 372 + if (lpi2c_imx->delivered == lpi2c_imx->msglen) 373 + break; 374 + 375 + data = lpi2c_imx->tx_buf[lpi2c_imx->delivered++]; 376 + writel(data, lpi2c_imx->base + LPI2C_MTDR); 377 + txcnt++; 378 + } 379 + 380 + if (lpi2c_imx->delivered < lpi2c_imx->msglen) 381 + lpi2c_imx_intctrl(lpi2c_imx, MIER_TDIE | MIER_NDIE); 382 + else 383 + complete(&lpi2c_imx->complete); 384 + } 385 + 386 + static void lpi2c_imx_read_rxfifo(struct lpi2c_imx_struct *lpi2c_imx) 387 + { 388 + unsigned int blocklen, remaining; 389 + unsigned int temp, data; 390 + 391 + do { 392 + data = readl(lpi2c_imx->base + LPI2C_MRDR); 393 + if (data & MRDR_RXEMPTY) 394 + break; 395 + 396 + lpi2c_imx->rx_buf[lpi2c_imx->delivered++] = data & 0xff; 397 + } while (1); 398 + 399 + /* 400 + * First byte is the length of remaining packet in the SMBus block 401 + * data read. Add it to msgs->len. 402 + */ 403 + if (lpi2c_imx->block_data) { 404 + blocklen = lpi2c_imx->rx_buf[0]; 405 + lpi2c_imx->msglen += blocklen; 406 + } 407 + 408 + remaining = lpi2c_imx->msglen - lpi2c_imx->delivered; 409 + 410 + if (!remaining) { 411 + complete(&lpi2c_imx->complete); 412 + return; 413 + } 414 + 415 + /* not finished, still waiting for rx data */ 416 + lpi2c_imx_set_rx_watermark(lpi2c_imx); 417 + 418 + /* multiple receive commands */ 419 + if (lpi2c_imx->block_data) { 420 + lpi2c_imx->block_data = 0; 421 + temp = remaining; 422 + temp |= (RECV_DATA << 8); 423 + writel(temp, lpi2c_imx->base + LPI2C_MTDR); 424 + } else if (!(lpi2c_imx->delivered & 0xff)) { 425 + temp = (remaining > CHUNK_DATA ? CHUNK_DATA : remaining) - 1; 426 + temp |= (RECV_DATA << 8); 427 + writel(temp, lpi2c_imx->base + LPI2C_MTDR); 428 + } 429 + 430 + lpi2c_imx_intctrl(lpi2c_imx, MIER_RDIE); 431 + } 432 + 433 + static void lpi2c_imx_write(struct lpi2c_imx_struct *lpi2c_imx, 434 + struct i2c_msg *msgs) 435 + { 436 + lpi2c_imx->tx_buf = msgs->buf; 437 + lpi2c_imx_set_tx_watermark(lpi2c_imx); 438 + lpi2c_imx_write_txfifo(lpi2c_imx); 439 + } 440 + 441 + static void lpi2c_imx_read(struct lpi2c_imx_struct *lpi2c_imx, 442 + struct i2c_msg *msgs) 443 + { 444 + unsigned int temp; 445 + 446 + lpi2c_imx->rx_buf = msgs->buf; 447 + lpi2c_imx->block_data = msgs->flags & I2C_M_RECV_LEN; 448 + 449 + lpi2c_imx_set_rx_watermark(lpi2c_imx); 450 + temp = msgs->len > CHUNK_DATA ? CHUNK_DATA - 1 : msgs->len - 1; 451 + temp |= (RECV_DATA << 8); 452 + writel(temp, lpi2c_imx->base + LPI2C_MTDR); 453 + 454 + lpi2c_imx_intctrl(lpi2c_imx, MIER_RDIE | MIER_NDIE); 455 + } 456 + 457 + static int lpi2c_imx_xfer(struct i2c_adapter *adapter, 458 + struct i2c_msg *msgs, int num) 459 + { 460 + struct lpi2c_imx_struct *lpi2c_imx = i2c_get_adapdata(adapter); 461 + unsigned int temp; 462 + int i, result; 463 + 464 + result = lpi2c_imx_master_enable(lpi2c_imx); 465 + if (result) 466 + return result; 467 + 468 + for (i = 0; i < num; i++) { 469 + result = lpi2c_imx_start(lpi2c_imx, &msgs[i]); 470 + if (result) 471 + goto disable; 472 + 473 + /* quick smbus */ 474 + if (num == 1 && msgs[0].len == 0) 475 + goto stop; 476 + 477 + lpi2c_imx->delivered = 0; 478 + lpi2c_imx->msglen = msgs[i].len; 479 + init_completion(&lpi2c_imx->complete); 480 + 481 + if (msgs[i].flags & I2C_M_RD) 482 + lpi2c_imx_read(lpi2c_imx, &msgs[i]); 483 + else 484 + lpi2c_imx_write(lpi2c_imx, &msgs[i]); 485 + 486 + result = lpi2c_imx_msg_complete(lpi2c_imx); 487 + if (result) 488 + goto stop; 489 + 490 + if (!(msgs[i].flags & I2C_M_RD)) { 491 + result = lpi2c_imx_txfifo_empty(lpi2c_imx); 492 + if (result) 493 + goto stop; 494 + } 495 + } 496 + 497 + stop: 498 + lpi2c_imx_stop(lpi2c_imx); 499 + 500 + temp = readl(lpi2c_imx->base + LPI2C_MSR); 501 + if ((temp & MSR_NDF) && !result) 502 + result = -EIO; 503 + 504 + disable: 505 + lpi2c_imx_master_disable(lpi2c_imx); 506 + 507 + dev_dbg(&lpi2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__, 508 + (result < 0) ? "error" : "success msg", 509 + (result < 0) ? result : num); 510 + 511 + return (result < 0) ? result : num; 512 + } 513 + 514 + static irqreturn_t lpi2c_imx_isr(int irq, void *dev_id) 515 + { 516 + struct lpi2c_imx_struct *lpi2c_imx = dev_id; 517 + unsigned int temp; 518 + 519 + lpi2c_imx_intctrl(lpi2c_imx, 0); 520 + temp = readl(lpi2c_imx->base + LPI2C_MSR); 521 + 522 + if (temp & MSR_RDF) 523 + lpi2c_imx_read_rxfifo(lpi2c_imx); 524 + 525 + if (temp & MSR_TDF) 526 + lpi2c_imx_write_txfifo(lpi2c_imx); 527 + 528 + if (temp & MSR_NDF) 529 + complete(&lpi2c_imx->complete); 530 + 531 + return IRQ_HANDLED; 532 + } 533 + 534 + static u32 lpi2c_imx_func(struct i2c_adapter *adapter) 535 + { 536 + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | 537 + I2C_FUNC_SMBUS_READ_BLOCK_DATA; 538 + } 539 + 540 + static struct i2c_algorithm lpi2c_imx_algo = { 541 + .master_xfer = lpi2c_imx_xfer, 542 + .functionality = lpi2c_imx_func, 543 + }; 544 + 545 + static const struct of_device_id lpi2c_imx_of_match[] = { 546 + { .compatible = "fsl,imx7ulp-lpi2c" }, 547 + { .compatible = "fsl,imx8dv-lpi2c" }, 548 + { }, 549 + }; 550 + MODULE_DEVICE_TABLE(of, lpi2c_imx_of_match); 551 + 552 + static int lpi2c_imx_probe(struct platform_device *pdev) 553 + { 554 + struct lpi2c_imx_struct *lpi2c_imx; 555 + struct resource *res; 556 + unsigned int temp; 557 + int irq, ret; 558 + 559 + lpi2c_imx = devm_kzalloc(&pdev->dev, sizeof(*lpi2c_imx), GFP_KERNEL); 560 + if (!lpi2c_imx) 561 + return -ENOMEM; 562 + 563 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 564 + lpi2c_imx->base = devm_ioremap_resource(&pdev->dev, res); 565 + if (IS_ERR(lpi2c_imx->base)) 566 + return PTR_ERR(lpi2c_imx->base); 567 + 568 + irq = platform_get_irq(pdev, 0); 569 + if (irq < 0) { 570 + dev_err(&pdev->dev, "can't get irq number\n"); 571 + return irq; 572 + } 573 + 574 + lpi2c_imx->adapter.owner = THIS_MODULE; 575 + lpi2c_imx->adapter.algo = &lpi2c_imx_algo; 576 + lpi2c_imx->adapter.dev.parent = &pdev->dev; 577 + lpi2c_imx->adapter.dev.of_node = pdev->dev.of_node; 578 + strlcpy(lpi2c_imx->adapter.name, pdev->name, 579 + sizeof(lpi2c_imx->adapter.name)); 580 + 581 + lpi2c_imx->clk = devm_clk_get(&pdev->dev, NULL); 582 + if (IS_ERR(lpi2c_imx->clk)) { 583 + dev_err(&pdev->dev, "can't get I2C peripheral clock\n"); 584 + return PTR_ERR(lpi2c_imx->clk); 585 + } 586 + 587 + ret = of_property_read_u32(pdev->dev.of_node, 588 + "clock-frequency", &lpi2c_imx->bitrate); 589 + if (ret) 590 + lpi2c_imx->bitrate = LPI2C_DEFAULT_RATE; 591 + 592 + ret = devm_request_irq(&pdev->dev, irq, lpi2c_imx_isr, 0, 593 + pdev->name, lpi2c_imx); 594 + if (ret) { 595 + dev_err(&pdev->dev, "can't claim irq %d\n", irq); 596 + return ret; 597 + } 598 + 599 + i2c_set_adapdata(&lpi2c_imx->adapter, lpi2c_imx); 600 + platform_set_drvdata(pdev, lpi2c_imx); 601 + 602 + ret = clk_prepare_enable(lpi2c_imx->clk); 603 + if (ret) { 604 + dev_err(&pdev->dev, "clk enable failed %d\n", ret); 605 + return ret; 606 + } 607 + 608 + temp = readl(lpi2c_imx->base + LPI2C_PARAM); 609 + lpi2c_imx->txfifosize = 1 << (temp & 0x0f); 610 + lpi2c_imx->rxfifosize = 1 << ((temp >> 8) & 0x0f); 611 + 612 + clk_disable(lpi2c_imx->clk); 613 + 614 + ret = i2c_add_adapter(&lpi2c_imx->adapter); 615 + if (ret) 616 + goto clk_unprepare; 617 + 618 + dev_info(&lpi2c_imx->adapter.dev, "LPI2C adapter registered\n"); 619 + 620 + return 0; 621 + 622 + clk_unprepare: 623 + clk_unprepare(lpi2c_imx->clk); 624 + 625 + return ret; 626 + } 627 + 628 + static int lpi2c_imx_remove(struct platform_device *pdev) 629 + { 630 + struct lpi2c_imx_struct *lpi2c_imx = platform_get_drvdata(pdev); 631 + 632 + i2c_del_adapter(&lpi2c_imx->adapter); 633 + 634 + clk_unprepare(lpi2c_imx->clk); 635 + 636 + return 0; 637 + } 638 + 639 + static struct platform_driver lpi2c_imx_driver = { 640 + .probe = lpi2c_imx_probe, 641 + .remove = lpi2c_imx_remove, 642 + .driver = { 643 + .name = DRIVER_NAME, 644 + .of_match_table = lpi2c_imx_of_match, 645 + }, 646 + }; 647 + 648 + module_platform_driver(lpi2c_imx_driver); 649 + 650 + MODULE_AUTHOR("Gao Pan <pandy.gao@nxp.com>"); 651 + MODULE_DESCRIPTION("I2C adapter driver for LPI2C bus"); 652 + MODULE_LICENSE("GPL");
+504
drivers/i2c/busses/i2c-mlxcpld.c
··· 1 + /* 2 + * Copyright (c) 2016 Mellanox Technologies. All rights reserved. 3 + * Copyright (c) 2016 Michael Shych <michaels@mellanox.com> 4 + * 5 + * Redistribution and use in source and binary forms, with or without 6 + * modification, are permitted provided that the following conditions are met: 7 + * 8 + * 1. Redistributions of source code must retain the above copyright 9 + * notice, this list of conditions and the following disclaimer. 10 + * 2. Redistributions in binary form must reproduce the above copyright 11 + * notice, this list of conditions and the following disclaimer in the 12 + * documentation and/or other materials provided with the distribution. 13 + * 3. Neither the names of the copyright holders nor the names of its 14 + * contributors may be used to endorse or promote products derived from 15 + * this software without specific prior written permission. 16 + * 17 + * Alternatively, this software may be distributed under the terms of the 18 + * GNU General Public License ("GPL") version 2 as published by the Free 19 + * Software Foundation. 20 + * 21 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 + * POSSIBILITY OF SUCH DAMAGE. 32 + */ 33 + 34 + #include <linux/delay.h> 35 + #include <linux/i2c.h> 36 + #include <linux/init.h> 37 + #include <linux/io.h> 38 + #include <linux/kernel.h> 39 + #include <linux/module.h> 40 + #include <linux/platform_device.h> 41 + 42 + /* General defines */ 43 + #define MLXPLAT_CPLD_LPC_I2C_BASE_ADDR 0x2000 44 + #define MLXCPLD_I2C_DEVICE_NAME "i2c_mlxcpld" 45 + #define MLXCPLD_I2C_VALID_FLAG (I2C_M_RECV_LEN | I2C_M_RD) 46 + #define MLXCPLD_I2C_BUS_NUM 1 47 + #define MLXCPLD_I2C_DATA_REG_SZ 36 48 + #define MLXCPLD_I2C_MAX_ADDR_LEN 4 49 + #define MLXCPLD_I2C_RETR_NUM 2 50 + #define MLXCPLD_I2C_XFER_TO 500000 /* usec */ 51 + #define MLXCPLD_I2C_POLL_TIME 2000 /* usec */ 52 + 53 + /* LPC I2C registers */ 54 + #define MLXCPLD_LPCI2C_LPF_REG 0x0 55 + #define MLXCPLD_LPCI2C_CTRL_REG 0x1 56 + #define MLXCPLD_LPCI2C_HALF_CYC_REG 0x4 57 + #define MLXCPLD_LPCI2C_I2C_HOLD_REG 0x5 58 + #define MLXCPLD_LPCI2C_CMD_REG 0x6 59 + #define MLXCPLD_LPCI2C_NUM_DAT_REG 0x7 60 + #define MLXCPLD_LPCI2C_NUM_ADDR_REG 0x8 61 + #define MLXCPLD_LPCI2C_STATUS_REG 0x9 62 + #define MLXCPLD_LPCI2C_DATA_REG 0xa 63 + 64 + /* LPC I2C masks and parametres */ 65 + #define MLXCPLD_LPCI2C_RST_SEL_MASK 0x1 66 + #define MLXCPLD_LPCI2C_TRANS_END 0x1 67 + #define MLXCPLD_LPCI2C_STATUS_NACK 0x10 68 + #define MLXCPLD_LPCI2C_NO_IND 0 69 + #define MLXCPLD_LPCI2C_ACK_IND 1 70 + #define MLXCPLD_LPCI2C_NACK_IND 2 71 + 72 + struct mlxcpld_i2c_curr_xfer { 73 + u8 cmd; 74 + u8 addr_width; 75 + u8 data_len; 76 + u8 msg_num; 77 + struct i2c_msg *msg; 78 + }; 79 + 80 + struct mlxcpld_i2c_priv { 81 + struct i2c_adapter adap; 82 + u32 base_addr; 83 + struct mutex lock; 84 + struct mlxcpld_i2c_curr_xfer xfer; 85 + struct device *dev; 86 + }; 87 + 88 + static void mlxcpld_i2c_lpc_write_buf(u8 *data, u8 len, u32 addr) 89 + { 90 + int i; 91 + 92 + for (i = 0; i < len - len % 4; i += 4) 93 + outl(*(u32 *)(data + i), addr + i); 94 + for (; i < len; ++i) 95 + outb(*(data + i), addr + i); 96 + } 97 + 98 + static void mlxcpld_i2c_lpc_read_buf(u8 *data, u8 len, u32 addr) 99 + { 100 + int i; 101 + 102 + for (i = 0; i < len - len % 4; i += 4) 103 + *(u32 *)(data + i) = inl(addr + i); 104 + for (; i < len; ++i) 105 + *(data + i) = inb(addr + i); 106 + } 107 + 108 + static void mlxcpld_i2c_read_comm(struct mlxcpld_i2c_priv *priv, u8 offs, 109 + u8 *data, u8 datalen) 110 + { 111 + u32 addr = priv->base_addr + offs; 112 + 113 + switch (datalen) { 114 + case 1: 115 + *(data) = inb(addr); 116 + break; 117 + case 2: 118 + *((u16 *)data) = inw(addr); 119 + break; 120 + case 3: 121 + *((u16 *)data) = inw(addr); 122 + *(data + 2) = inb(addr + 2); 123 + break; 124 + case 4: 125 + *((u32 *)data) = inl(addr); 126 + break; 127 + default: 128 + mlxcpld_i2c_lpc_read_buf(data, datalen, addr); 129 + break; 130 + } 131 + } 132 + 133 + static void mlxcpld_i2c_write_comm(struct mlxcpld_i2c_priv *priv, u8 offs, 134 + u8 *data, u8 datalen) 135 + { 136 + u32 addr = priv->base_addr + offs; 137 + 138 + switch (datalen) { 139 + case 1: 140 + outb(*(data), addr); 141 + break; 142 + case 2: 143 + outw(*((u16 *)data), addr); 144 + break; 145 + case 3: 146 + outw(*((u16 *)data), addr); 147 + outb(*(data + 2), addr + 2); 148 + break; 149 + case 4: 150 + outl(*((u32 *)data), addr); 151 + break; 152 + default: 153 + mlxcpld_i2c_lpc_write_buf(data, datalen, addr); 154 + break; 155 + } 156 + } 157 + 158 + /* 159 + * Check validity of received i2c messages parameters. 160 + * Returns 0 if OK, other - in case of invalid parameters. 161 + */ 162 + static int mlxcpld_i2c_check_msg_params(struct mlxcpld_i2c_priv *priv, 163 + struct i2c_msg *msgs, int num) 164 + { 165 + int i; 166 + 167 + if (!num) { 168 + dev_err(priv->dev, "Incorrect 0 num of messages\n"); 169 + return -EINVAL; 170 + } 171 + 172 + if (unlikely(msgs[0].addr > 0x7f)) { 173 + dev_err(priv->dev, "Invalid address 0x%03x\n", 174 + msgs[0].addr); 175 + return -EINVAL; 176 + } 177 + 178 + for (i = 0; i < num; ++i) { 179 + if (unlikely(!msgs[i].buf)) { 180 + dev_err(priv->dev, "Invalid buf in msg[%d]\n", 181 + i); 182 + return -EINVAL; 183 + } 184 + if (unlikely(msgs[0].addr != msgs[i].addr)) { 185 + dev_err(priv->dev, "Invalid addr in msg[%d]\n", 186 + i); 187 + return -EINVAL; 188 + } 189 + } 190 + 191 + return 0; 192 + } 193 + 194 + /* 195 + * Check if transfer is completed and status of operation. 196 + * Returns 0 - transfer completed (both ACK or NACK), 197 + * negative - transfer isn't finished. 198 + */ 199 + static int mlxcpld_i2c_check_status(struct mlxcpld_i2c_priv *priv, int *status) 200 + { 201 + u8 val; 202 + 203 + mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_STATUS_REG, &val, 1); 204 + 205 + if (val & MLXCPLD_LPCI2C_TRANS_END) { 206 + if (val & MLXCPLD_LPCI2C_STATUS_NACK) 207 + /* 208 + * The slave is unable to accept the data. No such 209 + * slave, command not understood, or unable to accept 210 + * any more data. 211 + */ 212 + *status = MLXCPLD_LPCI2C_NACK_IND; 213 + else 214 + *status = MLXCPLD_LPCI2C_ACK_IND; 215 + return 0; 216 + } 217 + *status = MLXCPLD_LPCI2C_NO_IND; 218 + 219 + return -EIO; 220 + } 221 + 222 + static void mlxcpld_i2c_set_transf_data(struct mlxcpld_i2c_priv *priv, 223 + struct i2c_msg *msgs, int num, 224 + u8 comm_len) 225 + { 226 + priv->xfer.msg = msgs; 227 + priv->xfer.msg_num = num; 228 + 229 + /* 230 + * All upper layers currently are never use transfer with more than 231 + * 2 messages. Actually, it's also not so relevant in Mellanox systems 232 + * because of HW limitation. Max size of transfer is not more than 32 233 + * bytes in the current x86 LPCI2C bridge. 234 + */ 235 + priv->xfer.cmd = msgs[num - 1].flags & I2C_M_RD; 236 + 237 + if (priv->xfer.cmd == I2C_M_RD && comm_len != msgs[0].len) { 238 + priv->xfer.addr_width = msgs[0].len; 239 + priv->xfer.data_len = comm_len - priv->xfer.addr_width; 240 + } else { 241 + priv->xfer.addr_width = 0; 242 + priv->xfer.data_len = comm_len; 243 + } 244 + } 245 + 246 + /* Reset CPLD LPCI2C block */ 247 + static void mlxcpld_i2c_reset(struct mlxcpld_i2c_priv *priv) 248 + { 249 + u8 val; 250 + 251 + mutex_lock(&priv->lock); 252 + 253 + mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_CTRL_REG, &val, 1); 254 + val &= ~MLXCPLD_LPCI2C_RST_SEL_MASK; 255 + mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_CTRL_REG, &val, 1); 256 + 257 + mutex_unlock(&priv->lock); 258 + } 259 + 260 + /* Make sure the CPLD is ready to start transmitting. */ 261 + static int mlxcpld_i2c_check_busy(struct mlxcpld_i2c_priv *priv) 262 + { 263 + u8 val; 264 + 265 + mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_STATUS_REG, &val, 1); 266 + 267 + if (val & MLXCPLD_LPCI2C_TRANS_END) 268 + return 0; 269 + 270 + return -EIO; 271 + } 272 + 273 + static int mlxcpld_i2c_wait_for_free(struct mlxcpld_i2c_priv *priv) 274 + { 275 + int timeout = 0; 276 + 277 + do { 278 + if (!mlxcpld_i2c_check_busy(priv)) 279 + break; 280 + usleep_range(MLXCPLD_I2C_POLL_TIME / 2, MLXCPLD_I2C_POLL_TIME); 281 + timeout += MLXCPLD_I2C_POLL_TIME; 282 + } while (timeout <= MLXCPLD_I2C_XFER_TO); 283 + 284 + if (timeout > MLXCPLD_I2C_XFER_TO) 285 + return -ETIMEDOUT; 286 + 287 + return 0; 288 + } 289 + 290 + /* 291 + * Wait for master transfer to complete. 292 + * It puts current process to sleep until we get interrupt or timeout expires. 293 + * Returns the number of transferred or read bytes or error (<0). 294 + */ 295 + static int mlxcpld_i2c_wait_for_tc(struct mlxcpld_i2c_priv *priv) 296 + { 297 + int status, i, timeout = 0; 298 + u8 datalen; 299 + 300 + do { 301 + usleep_range(MLXCPLD_I2C_POLL_TIME / 2, MLXCPLD_I2C_POLL_TIME); 302 + if (!mlxcpld_i2c_check_status(priv, &status)) 303 + break; 304 + timeout += MLXCPLD_I2C_POLL_TIME; 305 + } while (status == 0 && timeout < MLXCPLD_I2C_XFER_TO); 306 + 307 + switch (status) { 308 + case MLXCPLD_LPCI2C_NO_IND: 309 + return -ETIMEDOUT; 310 + 311 + case MLXCPLD_LPCI2C_ACK_IND: 312 + if (priv->xfer.cmd != I2C_M_RD) 313 + return (priv->xfer.addr_width + priv->xfer.data_len); 314 + 315 + if (priv->xfer.msg_num == 1) 316 + i = 0; 317 + else 318 + i = 1; 319 + 320 + if (!priv->xfer.msg[i].buf) 321 + return -EINVAL; 322 + 323 + /* 324 + * Actual read data len will be always the same as 325 + * requested len. 0xff (line pull-up) will be returned 326 + * if slave has no data to return. Thus don't read 327 + * MLXCPLD_LPCI2C_NUM_DAT_REG reg from CPLD. 328 + */ 329 + datalen = priv->xfer.data_len; 330 + 331 + mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_DATA_REG, 332 + priv->xfer.msg[i].buf, datalen); 333 + 334 + return datalen; 335 + 336 + case MLXCPLD_LPCI2C_NACK_IND: 337 + return -ENXIO; 338 + 339 + default: 340 + return -EINVAL; 341 + } 342 + } 343 + 344 + static void mlxcpld_i2c_xfer_msg(struct mlxcpld_i2c_priv *priv) 345 + { 346 + int i, len = 0; 347 + u8 cmd; 348 + 349 + mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_NUM_DAT_REG, 350 + &priv->xfer.data_len, 1); 351 + mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_NUM_ADDR_REG, 352 + &priv->xfer.addr_width, 1); 353 + 354 + for (i = 0; i < priv->xfer.msg_num; i++) { 355 + if ((priv->xfer.msg[i].flags & I2C_M_RD) != I2C_M_RD) { 356 + /* Don't write to CPLD buffer in read transaction */ 357 + mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_DATA_REG + 358 + len, priv->xfer.msg[i].buf, 359 + priv->xfer.msg[i].len); 360 + len += priv->xfer.msg[i].len; 361 + } 362 + } 363 + 364 + /* 365 + * Set target slave address with command for master transfer. 366 + * It should be latest executed function before CPLD transaction. 367 + */ 368 + cmd = (priv->xfer.msg[0].addr << 1) | priv->xfer.cmd; 369 + mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_CMD_REG, &cmd, 1); 370 + } 371 + 372 + /* 373 + * Generic lpc-i2c transfer. 374 + * Returns the number of processed messages or error (<0). 375 + */ 376 + static int mlxcpld_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, 377 + int num) 378 + { 379 + struct mlxcpld_i2c_priv *priv = i2c_get_adapdata(adap); 380 + u8 comm_len = 0; 381 + int i, err; 382 + 383 + err = mlxcpld_i2c_check_msg_params(priv, msgs, num); 384 + if (err) { 385 + dev_err(priv->dev, "Incorrect message\n"); 386 + return err; 387 + } 388 + 389 + for (i = 0; i < num; ++i) 390 + comm_len += msgs[i].len; 391 + 392 + /* Check bus state */ 393 + if (mlxcpld_i2c_wait_for_free(priv)) { 394 + dev_err(priv->dev, "LPCI2C bridge is busy\n"); 395 + 396 + /* 397 + * Usually it means something serious has happened. 398 + * We can not have unfinished previous transfer 399 + * so it doesn't make any sense to try to stop it. 400 + * Probably we were not able to recover from the 401 + * previous error. 402 + * The only reasonable thing - is soft reset. 403 + */ 404 + mlxcpld_i2c_reset(priv); 405 + if (mlxcpld_i2c_check_busy(priv)) { 406 + dev_err(priv->dev, "LPCI2C bridge is busy after reset\n"); 407 + return -EIO; 408 + } 409 + } 410 + 411 + mlxcpld_i2c_set_transf_data(priv, msgs, num, comm_len); 412 + 413 + mutex_lock(&priv->lock); 414 + 415 + /* Do real transfer. Can't fail */ 416 + mlxcpld_i2c_xfer_msg(priv); 417 + 418 + /* Wait for transaction complete */ 419 + err = mlxcpld_i2c_wait_for_tc(priv); 420 + 421 + mutex_unlock(&priv->lock); 422 + 423 + return err < 0 ? err : num; 424 + } 425 + 426 + static u32 mlxcpld_i2c_func(struct i2c_adapter *adap) 427 + { 428 + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SMBUS_BLOCK_DATA; 429 + } 430 + 431 + static const struct i2c_algorithm mlxcpld_i2c_algo = { 432 + .master_xfer = mlxcpld_i2c_xfer, 433 + .functionality = mlxcpld_i2c_func 434 + }; 435 + 436 + static struct i2c_adapter_quirks mlxcpld_i2c_quirks = { 437 + .flags = I2C_AQ_COMB_WRITE_THEN_READ, 438 + .max_read_len = MLXCPLD_I2C_DATA_REG_SZ - MLXCPLD_I2C_MAX_ADDR_LEN, 439 + .max_write_len = MLXCPLD_I2C_DATA_REG_SZ, 440 + .max_comb_1st_msg_len = 4, 441 + }; 442 + 443 + static struct i2c_adapter mlxcpld_i2c_adapter = { 444 + .owner = THIS_MODULE, 445 + .name = "i2c-mlxcpld", 446 + .class = I2C_CLASS_HWMON | I2C_CLASS_SPD, 447 + .algo = &mlxcpld_i2c_algo, 448 + .quirks = &mlxcpld_i2c_quirks, 449 + .retries = MLXCPLD_I2C_RETR_NUM, 450 + .nr = MLXCPLD_I2C_BUS_NUM, 451 + }; 452 + 453 + static int mlxcpld_i2c_probe(struct platform_device *pdev) 454 + { 455 + struct mlxcpld_i2c_priv *priv; 456 + int err; 457 + 458 + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 459 + if (!priv) 460 + return -ENOMEM; 461 + 462 + mutex_init(&priv->lock); 463 + platform_set_drvdata(pdev, priv); 464 + 465 + priv->dev = &pdev->dev; 466 + 467 + /* Register with i2c layer */ 468 + mlxcpld_i2c_adapter.timeout = usecs_to_jiffies(MLXCPLD_I2C_XFER_TO); 469 + priv->adap = mlxcpld_i2c_adapter; 470 + priv->adap.dev.parent = &pdev->dev; 471 + priv->base_addr = MLXPLAT_CPLD_LPC_I2C_BASE_ADDR; 472 + i2c_set_adapdata(&priv->adap, priv); 473 + 474 + err = i2c_add_numbered_adapter(&priv->adap); 475 + if (err) 476 + mutex_destroy(&priv->lock); 477 + 478 + return err; 479 + } 480 + 481 + static int mlxcpld_i2c_remove(struct platform_device *pdev) 482 + { 483 + struct mlxcpld_i2c_priv *priv = platform_get_drvdata(pdev); 484 + 485 + i2c_del_adapter(&priv->adap); 486 + mutex_destroy(&priv->lock); 487 + 488 + return 0; 489 + } 490 + 491 + static struct platform_driver mlxcpld_i2c_driver = { 492 + .probe = mlxcpld_i2c_probe, 493 + .remove = mlxcpld_i2c_remove, 494 + .driver = { 495 + .name = MLXCPLD_I2C_DEVICE_NAME, 496 + }, 497 + }; 498 + 499 + module_platform_driver(mlxcpld_i2c_driver); 500 + 501 + MODULE_AUTHOR("Michael Shych <michaels@mellanox.com>"); 502 + MODULE_DESCRIPTION("Mellanox I2C-CPLD controller driver"); 503 + MODULE_LICENSE("Dual BSD/GPL"); 504 + MODULE_ALIAS("platform:i2c-mlxcpld");
+5 -41
drivers/i2c/busses/i2c-octeon-core.c
··· 36 36 return (octeon_i2c_ctl_read(i2c) & TWSI_CTL_IFLG); 37 37 } 38 38 39 - static bool octeon_i2c_test_ready(struct octeon_i2c *i2c, bool *first) 40 - { 41 - if (octeon_i2c_test_iflg(i2c)) 42 - return true; 43 - 44 - if (*first) { 45 - *first = false; 46 - return false; 47 - } 48 - 49 - /* 50 - * IRQ has signaled an event but IFLG hasn't changed. 51 - * Sleep and retry once. 52 - */ 53 - usleep_range(I2C_OCTEON_EVENT_WAIT, 2 * I2C_OCTEON_EVENT_WAIT); 54 - return octeon_i2c_test_iflg(i2c); 55 - } 56 - 57 39 /** 58 40 * octeon_i2c_wait - wait for the IFLG to be set 59 41 * @i2c: The struct octeon_i2c ··· 45 63 static int octeon_i2c_wait(struct octeon_i2c *i2c) 46 64 { 47 65 long time_left; 48 - bool first = true; 49 66 50 67 /* 51 68 * Some chip revisions don't assert the irq in the interrupt ··· 61 80 } 62 81 63 82 i2c->int_enable(i2c); 64 - time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_ready(i2c, &first), 83 + time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c), 65 84 i2c->adap.timeout); 66 85 i2c->int_disable(i2c); 67 86 ··· 81 100 static bool octeon_i2c_hlc_test_valid(struct octeon_i2c *i2c) 82 101 { 83 102 return (__raw_readq(i2c->twsi_base + SW_TWSI(i2c)) & SW_TWSI_V) == 0; 84 - } 85 - 86 - static bool octeon_i2c_hlc_test_ready(struct octeon_i2c *i2c, bool *first) 87 - { 88 - /* check if valid bit is cleared */ 89 - if (octeon_i2c_hlc_test_valid(i2c)) 90 - return true; 91 - 92 - if (*first) { 93 - *first = false; 94 - return false; 95 - } 96 - 97 - /* 98 - * IRQ has signaled an event but valid bit isn't cleared. 99 - * Sleep and retry once. 100 - */ 101 - usleep_range(I2C_OCTEON_EVENT_WAIT, 2 * I2C_OCTEON_EVENT_WAIT); 102 - return octeon_i2c_hlc_test_valid(i2c); 103 103 } 104 104 105 105 static void octeon_i2c_hlc_int_clear(struct octeon_i2c *i2c) ··· 138 176 */ 139 177 static int octeon_i2c_hlc_wait(struct octeon_i2c *i2c) 140 178 { 141 - bool first = true; 142 179 int time_left; 143 180 144 181 /* ··· 156 195 157 196 i2c->hlc_int_enable(i2c); 158 197 time_left = wait_event_timeout(i2c->queue, 159 - octeon_i2c_hlc_test_ready(i2c, &first), 198 + octeon_i2c_hlc_test_valid(i2c), 160 199 i2c->adap.timeout); 161 200 i2c->hlc_int_disable(i2c); 162 201 if (!time_left) ··· 750 789 struct octeon_i2c *i2c = i2c_get_adapdata(adap); 751 790 752 791 octeon_i2c_hlc_disable(i2c); 792 + octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0); 793 + /* wait for software reset to settle */ 794 + udelay(5); 753 795 754 796 /* 755 797 * Bring control register to a good state regardless
+9 -23
drivers/i2c/busses/i2c-pxa-pci.c
··· 1 1 /* 2 + * CE4100 PCI-I2C glue code for PXA's driver 3 + * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de> 4 + * License: GPL v2 5 + * 2 6 * The CE4100's I2C device is more or less the same one as found on PXA. 3 7 * It does not support slave mode, the register slightly moved. This PCI 4 8 * device provides three bars, every contains a single I2C controller. 5 9 */ 6 - #include <linux/module.h> 10 + #include <linux/init.h> 7 11 #include <linux/pci.h> 8 12 #include <linux/platform_device.h> 9 13 #include <linux/i2c/pxa-i2c.h> ··· 138 134 return ret; 139 135 } 140 136 141 - static void ce4100_i2c_remove(struct pci_dev *dev) 142 - { 143 - struct ce4100_devices *sds; 144 - unsigned int i; 145 - 146 - sds = pci_get_drvdata(dev); 147 - 148 - for (i = 0; i < ARRAY_SIZE(sds->pdev); i++) 149 - platform_device_unregister(sds->pdev[i]); 150 - 151 - pci_disable_device(dev); 152 - kfree(sds); 153 - } 154 - 155 137 static const struct pci_device_id ce4100_i2c_devices[] = { 156 138 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2e68)}, 157 139 { }, 158 140 }; 159 - MODULE_DEVICE_TABLE(pci, ce4100_i2c_devices); 160 141 161 142 static struct pci_driver ce4100_i2c_driver = { 143 + .driver = { 144 + .suppress_bind_attrs = true, 145 + }, 162 146 .name = "ce4100_i2c", 163 147 .id_table = ce4100_i2c_devices, 164 148 .probe = ce4100_i2c_probe, 165 - .remove = ce4100_i2c_remove, 166 149 }; 167 - 168 - module_pci_driver(ce4100_i2c_driver); 169 - 170 - MODULE_DESCRIPTION("CE4100 PCI-I2C glue code for PXA's driver"); 171 - MODULE_LICENSE("GPL v2"); 172 - MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>"); 150 + builtin_pci_driver(ce4100_i2c_driver);
+24 -2
drivers/i2c/busses/i2c-pxa.c
··· 48 48 u32 isar; 49 49 u32 ilcr; 50 50 u32 iwcr; 51 + u32 fm; 52 + u32 hs; 51 53 }; 52 54 53 55 enum pxa_i2c_types { ··· 57 55 REGS_PXA3XX, 58 56 REGS_CE4100, 59 57 REGS_PXA910, 58 + REGS_A3700, 60 59 }; 60 + 61 + #define ICR_BUSMODE_FM (1 << 16) /* shifted fast mode for armada-3700 */ 62 + #define ICR_BUSMODE_HS (1 << 17) /* shifted high speed mode for armada-3700 */ 61 63 62 64 /* 63 65 * I2C registers definitions ··· 97 91 .ilcr = 0x28, 98 92 .iwcr = 0x30, 99 93 }, 94 + [REGS_A3700] = { 95 + .ibmr = 0x00, 96 + .idbr = 0x04, 97 + .icr = 0x08, 98 + .isr = 0x0c, 99 + .isar = 0x10, 100 + .fm = ICR_BUSMODE_FM, 101 + .hs = ICR_BUSMODE_HS, 102 + }, 100 103 }; 101 104 102 105 static const struct platform_device_id i2c_pxa_id_table[] = { ··· 113 98 { "pxa3xx-pwri2c", REGS_PXA3XX }, 114 99 { "ce4100-i2c", REGS_CE4100 }, 115 100 { "pxa910-i2c", REGS_PXA910 }, 101 + { "armada-3700-i2c", REGS_A3700 }, 116 102 { }, 117 103 }; 118 104 MODULE_DEVICE_TABLE(platform, i2c_pxa_id_table); ··· 209 193 unsigned char master_code; 210 194 unsigned long rate; 211 195 bool highmode_enter; 196 + u32 fm_mask; 197 + u32 hs_mask; 212 198 }; 213 199 214 200 #define _IBMR(i2c) ((i2c)->reg_ibmr) ··· 521 503 writel(i2c->slave_addr, _ISAR(i2c)); 522 504 523 505 /* set control register values */ 524 - writel(I2C_ICR_INIT | (i2c->fast_mode ? ICR_FM : 0), _ICR(i2c)); 525 - writel(readl(_ICR(i2c)) | (i2c->high_mode ? ICR_HS : 0), _ICR(i2c)); 506 + writel(I2C_ICR_INIT | (i2c->fast_mode ? i2c->fm_mask : 0), _ICR(i2c)); 507 + writel(readl(_ICR(i2c)) | (i2c->high_mode ? i2c->hs_mask : 0), _ICR(i2c)); 526 508 527 509 #ifdef CONFIG_I2C_PXA_SLAVE 528 510 dev_info(&i2c->adap.dev, "Enabling slave mode\n"); ··· 1155 1137 { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX }, 1156 1138 { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX }, 1157 1139 { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA910 }, 1140 + { .compatible = "marvell,armada-3700-i2c", .data = (void *)REGS_A3700 }, 1158 1141 {} 1159 1142 }; 1160 1143 MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids); ··· 1253 1234 i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr; 1254 1235 i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr; 1255 1236 i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr; 1237 + i2c->fm_mask = pxa_reg_layout[i2c_type].fm ? : ICR_FM; 1238 + i2c->hs_mask = pxa_reg_layout[i2c_type].hs ? : ICR_HS; 1239 + 1256 1240 if (i2c_type != REGS_CE4100) 1257 1241 i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar; 1258 1242
+103 -19
drivers/i2c/busses/i2c-qup.c
··· 14 14 * 15 15 */ 16 16 17 + #include <linux/acpi.h> 17 18 #include <linux/atomic.h> 18 19 #include <linux/clk.h> 19 20 #include <linux/delay.h> ··· 132 131 133 132 /* Max timeout in ms for 32k bytes */ 134 133 #define TOUT_MAX 300 134 + 135 + /* Default values. Use these if FW query fails */ 136 + #define DEFAULT_CLK_FREQ 100000 137 + #define DEFAULT_SRC_CLK 20000000 135 138 136 139 struct qup_i2c_block { 137 140 int count; ··· 530 525 return data_len; 531 526 } 532 527 528 + static bool qup_i2c_check_msg_len(struct i2c_msg *msg) 529 + { 530 + return ((msg->flags & I2C_M_RD) && (msg->flags & I2C_M_RECV_LEN)); 531 + } 532 + 533 + static int qup_i2c_set_tags_smb(u16 addr, u8 *tags, struct qup_i2c_dev *qup, 534 + struct i2c_msg *msg) 535 + { 536 + int len = 0; 537 + 538 + if (msg->len > 1) { 539 + tags[len++] = QUP_TAG_V2_DATARD_STOP; 540 + tags[len++] = qup_i2c_get_data_len(qup) - 1; 541 + } else { 542 + tags[len++] = QUP_TAG_V2_START; 543 + tags[len++] = addr & 0xff; 544 + 545 + if (msg->flags & I2C_M_TEN) 546 + tags[len++] = addr >> 8; 547 + 548 + tags[len++] = QUP_TAG_V2_DATARD; 549 + /* Read 1 byte indicating the length of the SMBus message */ 550 + tags[len++] = 1; 551 + } 552 + return len; 553 + } 554 + 533 555 static int qup_i2c_set_tags(u8 *tags, struct qup_i2c_dev *qup, 534 556 struct i2c_msg *msg, int is_dma) 535 557 { ··· 565 533 int data_len; 566 534 567 535 int last = (qup->blk.pos == (qup->blk.count - 1)) && (qup->is_last); 536 + 537 + /* Handle tags for SMBus block read */ 538 + if (qup_i2c_check_msg_len(msg)) 539 + return qup_i2c_set_tags_smb(addr, tags, qup, msg); 568 540 569 541 if (qup->blk.pos == 0) { 570 542 tags[len++] = QUP_TAG_V2_START; ··· 1092 1056 struct i2c_msg *msg) 1093 1057 { 1094 1058 u32 val; 1095 - int idx, pos = 0, ret = 0, total; 1059 + int idx, pos = 0, ret = 0, total, msg_offset = 0; 1096 1060 1061 + /* 1062 + * If the message length is already read in 1063 + * the first byte of the buffer, account for 1064 + * that by setting the offset 1065 + */ 1066 + if (qup_i2c_check_msg_len(msg) && (msg->len > 1)) 1067 + msg_offset = 1; 1097 1068 total = qup_i2c_get_data_len(qup); 1069 + total -= msg_offset; 1098 1070 1099 1071 /* 2 extra bytes for read tags */ 1100 1072 while (pos < (total + 2)) { ··· 1122 1078 1123 1079 if (pos >= (total + 2)) 1124 1080 goto out; 1125 - 1126 - msg->buf[qup->pos++] = val & 0xff; 1081 + msg->buf[qup->pos + msg_offset] = val & 0xff; 1082 + qup->pos++; 1127 1083 } 1128 1084 } 1129 1085 ··· 1163 1119 goto err; 1164 1120 1165 1121 qup->blk.pos++; 1122 + 1123 + /* Handle SMBus block read length */ 1124 + if (qup_i2c_check_msg_len(msg) && (msg->len == 1)) { 1125 + if (msg->buf[0] > I2C_SMBUS_BLOCK_MAX) { 1126 + ret = -EPROTO; 1127 + goto err; 1128 + } 1129 + msg->len += msg->buf[0]; 1130 + qup->pos = 0; 1131 + qup_i2c_set_blk_data(qup, msg); 1132 + /* set tag length for block read */ 1133 + qup->blk.tx_tag_len = 2; 1134 + qup_i2c_set_read_mode_v2(qup, msg->buf[0]); 1135 + } 1166 1136 } while (qup->blk.pos < qup->blk.count); 1167 1137 1168 1138 err: ··· 1259 1201 1260 1202 if (qup_i2c_poll_state_i2c_master(qup)) { 1261 1203 ret = -EIO; 1204 + goto out; 1205 + } 1206 + 1207 + if (qup_i2c_check_msg_len(&msgs[idx])) { 1208 + ret = -EINVAL; 1262 1209 goto out; 1263 1210 } 1264 1211 ··· 1421 1358 static int qup_i2c_probe(struct platform_device *pdev) 1422 1359 { 1423 1360 static const int blk_sizes[] = {4, 16, 32}; 1424 - struct device_node *node = pdev->dev.of_node; 1425 1361 struct qup_i2c_dev *qup; 1426 1362 unsigned long one_bit_t; 1427 1363 struct resource *res; 1428 1364 u32 io_mode, hw_ver, size; 1429 1365 int ret, fs_div, hs_div; 1430 - int src_clk_freq; 1431 - u32 clk_freq = 100000; 1366 + u32 src_clk_freq = DEFAULT_SRC_CLK; 1367 + u32 clk_freq = DEFAULT_CLK_FREQ; 1432 1368 int blocks; 1433 1369 1434 1370 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL); ··· 1438 1376 init_completion(&qup->xfer); 1439 1377 platform_set_drvdata(pdev, qup); 1440 1378 1441 - of_property_read_u32(node, "clock-frequency", &clk_freq); 1379 + ret = device_property_read_u32(qup->dev, "clock-frequency", &clk_freq); 1380 + if (ret) { 1381 + dev_notice(qup->dev, "using default clock-frequency %d", 1382 + DEFAULT_CLK_FREQ); 1383 + } 1442 1384 1443 1385 if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) { 1444 1386 qup->adap.algo = &qup_i2c_algo; ··· 1518 1452 return qup->irq; 1519 1453 } 1520 1454 1521 - qup->clk = devm_clk_get(qup->dev, "core"); 1522 - if (IS_ERR(qup->clk)) { 1523 - dev_err(qup->dev, "Could not get core clock\n"); 1524 - return PTR_ERR(qup->clk); 1525 - } 1455 + if (has_acpi_companion(qup->dev)) { 1456 + ret = device_property_read_u32(qup->dev, 1457 + "src-clock-hz", &src_clk_freq); 1458 + if (ret) { 1459 + dev_notice(qup->dev, "using default src-clock-hz %d", 1460 + DEFAULT_SRC_CLK); 1461 + } 1462 + ACPI_COMPANION_SET(&qup->adap.dev, ACPI_COMPANION(qup->dev)); 1463 + } else { 1464 + qup->clk = devm_clk_get(qup->dev, "core"); 1465 + if (IS_ERR(qup->clk)) { 1466 + dev_err(qup->dev, "Could not get core clock\n"); 1467 + return PTR_ERR(qup->clk); 1468 + } 1526 1469 1527 - qup->pclk = devm_clk_get(qup->dev, "iface"); 1528 - if (IS_ERR(qup->pclk)) { 1529 - dev_err(qup->dev, "Could not get iface clock\n"); 1530 - return PTR_ERR(qup->pclk); 1470 + qup->pclk = devm_clk_get(qup->dev, "iface"); 1471 + if (IS_ERR(qup->pclk)) { 1472 + dev_err(qup->dev, "Could not get iface clock\n"); 1473 + return PTR_ERR(qup->pclk); 1474 + } 1475 + qup_i2c_enable_clocks(qup); 1476 + src_clk_freq = clk_get_rate(qup->clk); 1531 1477 } 1532 - 1533 - qup_i2c_enable_clocks(qup); 1534 1478 1535 1479 /* 1536 1480 * Bootloaders might leave a pending interrupt on certain QUP's, ··· 1588 1512 size = QUP_INPUT_FIFO_SIZE(io_mode); 1589 1513 qup->in_fifo_sz = qup->in_blk_sz * (2 << size); 1590 1514 1591 - src_clk_freq = clk_get_rate(qup->clk); 1592 1515 fs_div = ((src_clk_freq / clk_freq) / 2) - 3; 1593 1516 hs_div = 3; 1594 1517 qup->clk_ctl = (hs_div << 8) | (fs_div & 0xff); ··· 1706 1631 }; 1707 1632 MODULE_DEVICE_TABLE(of, qup_i2c_dt_match); 1708 1633 1634 + #if IS_ENABLED(CONFIG_ACPI) 1635 + static const struct acpi_device_id qup_i2c_acpi_match[] = { 1636 + { "QCOM8010"}, 1637 + { }, 1638 + }; 1639 + MODULE_DEVICE_TABLE(acpi, qup_i2c_acpi_match); 1640 + #endif 1641 + 1709 1642 static struct platform_driver qup_i2c_driver = { 1710 1643 .probe = qup_i2c_probe, 1711 1644 .remove = qup_i2c_remove, ··· 1721 1638 .name = "i2c_qup", 1722 1639 .pm = &qup_i2c_qup_pm_ops, 1723 1640 .of_match_table = qup_i2c_dt_match, 1641 + .acpi_match_table = ACPI_PTR(qup_i2c_acpi_match), 1724 1642 }, 1725 1643 }; 1726 1644
+4 -1
drivers/i2c/busses/i2c-rcar.c
··· 793 793 }; 794 794 795 795 static const struct of_device_id rcar_i2c_dt_ids[] = { 796 - { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 }, 797 796 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 }, 798 797 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 }, 799 798 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 }, ··· 802 803 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 }, 803 804 { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 }, 804 805 { .compatible = "renesas,i2c-r8a7796", .data = (void *)I2C_RCAR_GEN3 }, 806 + { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 }, /* Deprecated */ 807 + { .compatible = "renesas,rcar-gen1-i2c", .data = (void *)I2C_RCAR_GEN1 }, 808 + { .compatible = "renesas,rcar-gen2-i2c", .data = (void *)I2C_RCAR_GEN2 }, 809 + { .compatible = "renesas,rcar-gen3-i2c", .data = (void *)I2C_RCAR_GEN3 }, 805 810 {}, 806 811 }; 807 812 MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
+3 -1
drivers/i2c/busses/i2c-sh_mobile.c
··· 827 827 }; 828 828 829 829 static const struct of_device_id sh_mobile_i2c_dt_ids[] = { 830 - { .compatible = "renesas,rmobile-iic", .data = &default_dt_config }, 831 830 { .compatible = "renesas,iic-r8a73a4", .data = &fast_clock_dt_config }, 832 831 { .compatible = "renesas,iic-r8a7740", .data = &r8a7740_dt_config }, 833 832 { .compatible = "renesas,iic-r8a7790", .data = &fast_clock_dt_config }, ··· 834 835 { .compatible = "renesas,iic-r8a7792", .data = &fast_clock_dt_config }, 835 836 { .compatible = "renesas,iic-r8a7793", .data = &fast_clock_dt_config }, 836 837 { .compatible = "renesas,iic-r8a7794", .data = &fast_clock_dt_config }, 838 + { .compatible = "renesas,rcar-gen2-iic", .data = &fast_clock_dt_config }, 837 839 { .compatible = "renesas,iic-r8a7795", .data = &fast_clock_dt_config }, 840 + { .compatible = "renesas,rcar-gen3-iic", .data = &fast_clock_dt_config }, 838 841 { .compatible = "renesas,iic-sh73a0", .data = &fast_clock_dt_config }, 842 + { .compatible = "renesas,rmobile-iic", .data = &default_dt_config }, 839 843 {}, 840 844 }; 841 845 MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
+3 -3
drivers/i2c/busses/i2c-uniphier-f.c
··· 528 528 if (!clk_rate) { 529 529 dev_err(dev, "input clock rate should not be zero\n"); 530 530 ret = -EINVAL; 531 - goto err; 531 + goto disable_clk; 532 532 } 533 533 534 534 init_completion(&priv->comp); ··· 547 547 pdev->name, priv); 548 548 if (ret) { 549 549 dev_err(dev, "failed to request irq %d\n", irq); 550 - goto err; 550 + goto disable_clk; 551 551 } 552 552 553 553 ret = i2c_add_adapter(&priv->adap); 554 - err: 554 + disable_clk: 555 555 if (ret) 556 556 clk_disable_unprepare(priv->clk); 557 557
+3 -3
drivers/i2c/busses/i2c-uniphier.c
··· 373 373 if (!clk_rate) { 374 374 dev_err(dev, "input clock rate should not be zero\n"); 375 375 ret = -EINVAL; 376 - goto err; 376 + goto disable_clk; 377 377 } 378 378 379 379 init_completion(&priv->comp); ··· 392 392 priv); 393 393 if (ret) { 394 394 dev_err(dev, "failed to request irq %d\n", irq); 395 - goto err; 395 + goto disable_clk; 396 396 } 397 397 398 398 ret = i2c_add_adapter(&priv->adap); 399 - err: 399 + disable_clk: 400 400 if (ret) 401 401 clk_disable_unprepare(priv->clk); 402 402
+1 -1
drivers/i2c/busses/i2c-viperboard.c
··· 354 354 .functionality = vprbrd_i2c_func, 355 355 }; 356 356 357 - static struct i2c_adapter_quirks vprbrd_quirks = { 357 + static const struct i2c_adapter_quirks vprbrd_quirks = { 358 358 .max_read_len = 2048, 359 359 .max_write_len = 2048, 360 360 };
+1
drivers/i2c/busses/i2c-xlp9xx.c
··· 393 393 init_completion(&priv->msg_complete); 394 394 priv->adapter.dev.parent = &pdev->dev; 395 395 priv->adapter.algo = &xlp9xx_i2c_algo; 396 + ACPI_COMPANION_SET(&priv->adapter.dev, ACPI_COMPANION(&pdev->dev)); 396 397 priv->adapter.dev.of_node = pdev->dev.of_node; 397 398 priv->dev = &pdev->dev; 398 399
+188 -9
drivers/i2c/i2c-core.c
··· 65 65 #define I2C_ADDR_OFFSET_TEN_BIT 0xa000 66 66 #define I2C_ADDR_OFFSET_SLAVE 0x1000 67 67 68 + #define I2C_ADDR_7BITS_MAX 0x77 69 + #define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1) 70 + 68 71 /* core_lock protects i2c_adapter_idr, and guarantees 69 72 that device detection, deletion of detected devices, and attach_adapter 70 73 calls are serialized */ ··· 679 676 680 677 /* ------------------------------------------------------------------------- */ 681 678 682 - static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, 679 + const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, 683 680 const struct i2c_client *client) 684 681 { 682 + if (!(id && client)) 683 + return NULL; 684 + 685 685 while (id->name[0]) { 686 686 if (strcmp(client->name, id->name) == 0) 687 687 return id; ··· 692 686 } 693 687 return NULL; 694 688 } 689 + EXPORT_SYMBOL_GPL(i2c_match_id); 695 690 696 691 static int i2c_device_match(struct device *dev, struct device_driver *drv) 697 692 { 698 693 struct i2c_client *client = i2c_verify_client(dev); 699 694 struct i2c_driver *driver; 700 695 701 - if (!client) 702 - return 0; 703 696 704 697 /* Attempt an OF style match */ 705 - if (of_driver_match_device(dev, drv)) 698 + if (i2c_of_match_device(drv->of_match_table, client)) 706 699 return 1; 707 700 708 701 /* Then ACPI style match */ ··· 709 704 return 1; 710 705 711 706 driver = to_i2c_driver(drv); 712 - /* match on an id table if there is one */ 713 - if (driver->id_table) 714 - return i2c_match_id(driver->id_table, client) != NULL; 707 + 708 + /* Finally an I2C match */ 709 + if (i2c_match_id(driver->id_table, client)) 710 + return 1; 715 711 716 712 return 0; 717 713 } ··· 899 893 adap->bus_recovery_info = NULL; 900 894 } 901 895 896 + static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client) 897 + { 898 + struct i2c_adapter *adap = client->adapter; 899 + unsigned int irq; 900 + 901 + if (!adap->host_notify_domain) 902 + return -ENXIO; 903 + 904 + if (client->flags & I2C_CLIENT_TEN) 905 + return -EINVAL; 906 + 907 + irq = irq_find_mapping(adap->host_notify_domain, client->addr); 908 + if (!irq) 909 + irq = irq_create_mapping(adap->host_notify_domain, 910 + client->addr); 911 + 912 + return irq > 0 ? irq : -ENXIO; 913 + } 914 + 902 915 static int i2c_device_probe(struct device *dev) 903 916 { 904 917 struct i2c_client *client = i2c_verify_client(dev); ··· 939 914 } 940 915 if (irq == -EPROBE_DEFER) 941 916 return irq; 917 + /* 918 + * ACPI and OF did not find any useful IRQ, try to see 919 + * if Host Notify can be used. 920 + */ 921 + if (irq < 0) { 922 + dev_dbg(dev, "Using Host Notify IRQ\n"); 923 + irq = i2c_smbus_host_notify_to_irq(client); 924 + } 942 925 if (irq < 0) 943 926 irq = 0; 944 927 ··· 954 921 } 955 922 956 923 driver = to_i2c_driver(dev->driver); 957 - if (!driver->probe || !driver->id_table) 924 + 925 + /* 926 + * An I2C ID table is not mandatory, if and only if, a suitable Device 927 + * Tree match table entry is supplied for the probing device. 928 + */ 929 + if (!driver->id_table && 930 + !i2c_of_match_device(dev->driver->of_match_table, client)) 958 931 return -ENODEV; 959 932 960 933 if (client->flags & I2C_CLIENT_WAKE) { ··· 995 956 if (status == -EPROBE_DEFER) 996 957 goto err_clear_wakeup_irq; 997 958 998 - status = driver->probe(client, i2c_match_id(driver->id_table, client)); 959 + /* 960 + * When there are no more users of probe(), 961 + * rename probe_new to probe. 962 + */ 963 + if (driver->probe_new) 964 + status = driver->probe_new(client); 965 + else if (driver->probe) 966 + status = driver->probe(client, 967 + i2c_match_id(driver->id_table, client)); 968 + else 969 + status = -EINVAL; 970 + 999 971 if (status) 1000 972 goto err_detach_pm_domain; 1001 973 ··· 1817 1767 return adapter; 1818 1768 } 1819 1769 EXPORT_SYMBOL(of_get_i2c_adapter_by_node); 1770 + 1771 + static const struct of_device_id* 1772 + i2c_of_match_device_sysfs(const struct of_device_id *matches, 1773 + struct i2c_client *client) 1774 + { 1775 + const char *name; 1776 + 1777 + for (; matches->compatible[0]; matches++) { 1778 + /* 1779 + * Adding devices through the i2c sysfs interface provides us 1780 + * a string to match which may be compatible with the device 1781 + * tree compatible strings, however with no actual of_node the 1782 + * of_match_device() will not match 1783 + */ 1784 + if (sysfs_streq(client->name, matches->compatible)) 1785 + return matches; 1786 + 1787 + name = strchr(matches->compatible, ','); 1788 + if (!name) 1789 + name = matches->compatible; 1790 + else 1791 + name++; 1792 + 1793 + if (sysfs_streq(client->name, name)) 1794 + return matches; 1795 + } 1796 + 1797 + return NULL; 1798 + } 1799 + 1800 + const struct of_device_id 1801 + *i2c_of_match_device(const struct of_device_id *matches, 1802 + struct i2c_client *client) 1803 + { 1804 + const struct of_device_id *match; 1805 + 1806 + if (!(client && matches)) 1807 + return NULL; 1808 + 1809 + match = of_match_device(matches, &client->dev); 1810 + if (match) 1811 + return match; 1812 + 1813 + return i2c_of_match_device_sysfs(matches, client); 1814 + } 1815 + EXPORT_SYMBOL_GPL(i2c_of_match_device); 1820 1816 #else 1821 1817 static void of_i2c_register_devices(struct i2c_adapter *adap) { } 1822 1818 #endif /* CONFIG_OF */ ··· 1896 1800 .unlock_bus = i2c_adapter_unlock_bus, 1897 1801 }; 1898 1802 1803 + static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap) 1804 + { 1805 + struct irq_domain *domain = adap->host_notify_domain; 1806 + irq_hw_number_t hwirq; 1807 + 1808 + if (!domain) 1809 + return; 1810 + 1811 + for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++) 1812 + irq_dispose_mapping(irq_find_mapping(domain, hwirq)); 1813 + 1814 + irq_domain_remove(domain); 1815 + adap->host_notify_domain = NULL; 1816 + } 1817 + 1818 + static int i2c_host_notify_irq_map(struct irq_domain *h, 1819 + unsigned int virq, 1820 + irq_hw_number_t hw_irq_num) 1821 + { 1822 + irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq); 1823 + 1824 + return 0; 1825 + } 1826 + 1827 + static const struct irq_domain_ops i2c_host_notify_irq_ops = { 1828 + .map = i2c_host_notify_irq_map, 1829 + }; 1830 + 1831 + static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap) 1832 + { 1833 + struct irq_domain *domain; 1834 + 1835 + if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY)) 1836 + return 0; 1837 + 1838 + domain = irq_domain_create_linear(adap->dev.fwnode, 1839 + I2C_ADDR_7BITS_COUNT, 1840 + &i2c_host_notify_irq_ops, adap); 1841 + if (!domain) 1842 + return -ENOMEM; 1843 + 1844 + adap->host_notify_domain = domain; 1845 + 1846 + return 0; 1847 + } 1848 + 1849 + /** 1850 + * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct 1851 + * I2C client. 1852 + * @adap: the adapter 1853 + * @addr: the I2C address of the notifying device 1854 + * Context: can't sleep 1855 + * 1856 + * Helper function to be called from an I2C bus driver's interrupt 1857 + * handler. It will schedule the Host Notify IRQ. 1858 + */ 1859 + int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr) 1860 + { 1861 + int irq; 1862 + 1863 + if (!adap) 1864 + return -EINVAL; 1865 + 1866 + irq = irq_find_mapping(adap->host_notify_domain, addr); 1867 + if (irq <= 0) 1868 + return -ENXIO; 1869 + 1870 + generic_handle_irq(irq); 1871 + 1872 + return 0; 1873 + } 1874 + EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify); 1875 + 1899 1876 static int i2c_register_adapter(struct i2c_adapter *adap) 1900 1877 { 1901 1878 int res = -EINVAL; ··· 1999 1830 /* Set default timeout to 1 second if not already set */ 2000 1831 if (adap->timeout == 0) 2001 1832 adap->timeout = HZ; 1833 + 1834 + /* register soft irqs for Host Notify */ 1835 + res = i2c_setup_host_notify_irq_domain(adap); 1836 + if (res) { 1837 + pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n", 1838 + adap->name, res); 1839 + goto out_list; 1840 + } 2002 1841 2003 1842 dev_set_name(&adap->dev, "i2c-%d", adap->nr); 2004 1843 adap->dev.bus = &i2c_bus_type; ··· 2244 2067 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); 2245 2068 2246 2069 pm_runtime_disable(&adap->dev); 2070 + 2071 + i2c_host_notify_irq_teardown(adap); 2247 2072 2248 2073 /* wait until all references to the device are gone 2249 2074 *
-102
drivers/i2c/i2c-smbus.c
··· 241 241 } 242 242 EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert); 243 243 244 - static void smbus_host_notify_work(struct work_struct *work) 245 - { 246 - struct alert_data alert; 247 - struct i2c_adapter *adapter; 248 - unsigned long flags; 249 - u16 payload; 250 - u8 addr; 251 - struct smbus_host_notify *data; 252 - 253 - data = container_of(work, struct smbus_host_notify, work); 254 - 255 - spin_lock_irqsave(&data->lock, flags); 256 - payload = data->payload; 257 - addr = data->addr; 258 - adapter = data->adapter; 259 - 260 - /* clear the pending bit and release the spinlock */ 261 - data->pending = false; 262 - spin_unlock_irqrestore(&data->lock, flags); 263 - 264 - if (!adapter || !addr) 265 - return; 266 - 267 - alert.type = I2C_PROTOCOL_SMBUS_HOST_NOTIFY; 268 - alert.addr = addr; 269 - alert.data = payload; 270 - 271 - device_for_each_child(&adapter->dev, &alert, smbus_do_alert); 272 - } 273 - 274 - /** 275 - * i2c_setup_smbus_host_notify - Allocate a new smbus_host_notify for the given 276 - * I2C adapter. 277 - * @adapter: the adapter we want to associate a Host Notify function 278 - * 279 - * Returns a struct smbus_host_notify pointer on success, and NULL on failure. 280 - * The resulting smbus_host_notify must not be freed afterwards, it is a 281 - * managed resource already. 282 - */ 283 - struct smbus_host_notify *i2c_setup_smbus_host_notify(struct i2c_adapter *adap) 284 - { 285 - struct smbus_host_notify *host_notify; 286 - 287 - host_notify = devm_kzalloc(&adap->dev, sizeof(struct smbus_host_notify), 288 - GFP_KERNEL); 289 - if (!host_notify) 290 - return NULL; 291 - 292 - host_notify->adapter = adap; 293 - 294 - spin_lock_init(&host_notify->lock); 295 - INIT_WORK(&host_notify->work, smbus_host_notify_work); 296 - 297 - return host_notify; 298 - } 299 - EXPORT_SYMBOL_GPL(i2c_setup_smbus_host_notify); 300 - 301 - /** 302 - * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct 303 - * I2C client. 304 - * @host_notify: the struct host_notify attached to the relevant adapter 305 - * @addr: the I2C address of the notifying device 306 - * @data: the payload of the notification 307 - * Context: can't sleep 308 - * 309 - * Helper function to be called from an I2C bus driver's interrupt 310 - * handler. It will schedule the Host Notify work, in turn calling the 311 - * corresponding I2C device driver's alert function. 312 - * 313 - * host_notify should be a valid pointer previously returned by 314 - * i2c_setup_smbus_host_notify(). 315 - */ 316 - int i2c_handle_smbus_host_notify(struct smbus_host_notify *host_notify, 317 - unsigned short addr, unsigned int data) 318 - { 319 - unsigned long flags; 320 - struct i2c_adapter *adapter; 321 - 322 - if (!host_notify || !host_notify->adapter) 323 - return -EINVAL; 324 - 325 - adapter = host_notify->adapter; 326 - 327 - spin_lock_irqsave(&host_notify->lock, flags); 328 - 329 - if (host_notify->pending) { 330 - spin_unlock_irqrestore(&host_notify->lock, flags); 331 - dev_warn(&adapter->dev, "Host Notify already scheduled.\n"); 332 - return -EBUSY; 333 - } 334 - 335 - host_notify->payload = data; 336 - host_notify->addr = addr; 337 - 338 - /* Mark that there is a pending notification and release the lock */ 339 - host_notify->pending = true; 340 - spin_unlock_irqrestore(&host_notify->lock, flags); 341 - 342 - return schedule_work(&host_notify->work); 343 - } 344 - EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify); 345 - 346 244 module_i2c_driver(smbalert_driver); 347 245 348 246 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
+11
drivers/i2c/muxes/Kconfig
··· 82 82 demultiplexer that uses the pinctrl subsystem. This is useful if you 83 83 want to change the I2C master at run-time depending on features. 84 84 85 + config I2C_MUX_MLXCPLD 86 + tristate "Mellanox CPLD based I2C multiplexer" 87 + help 88 + If you say yes to this option, support will be included for a 89 + CPLD based I2C multiplexer. This driver provides access to 90 + I2C busses connected through a MUX, which is controlled 91 + by a CPLD register. 92 + 93 + This driver can also be built as a module. If so, the module 94 + will be called i2c-mux-mlxcpld. 95 + 85 96 endmenu
+1
drivers/i2c/muxes/Makefile
··· 6 6 obj-$(CONFIG_I2C_DEMUX_PINCTRL) += i2c-demux-pinctrl.o 7 7 8 8 obj-$(CONFIG_I2C_MUX_GPIO) += i2c-mux-gpio.o 9 + obj-$(CONFIG_I2C_MUX_MLXCPLD) += i2c-mux-mlxcpld.o 9 10 obj-$(CONFIG_I2C_MUX_PCA9541) += i2c-mux-pca9541.o 10 11 obj-$(CONFIG_I2C_MUX_PCA954x) += i2c-mux-pca954x.o 11 12 obj-$(CONFIG_I2C_MUX_PINCTRL) += i2c-mux-pinctrl.o
+14 -4
drivers/i2c/muxes/i2c-mux-gpio.c
··· 21 21 struct gpiomux { 22 22 struct i2c_mux_gpio_platform_data data; 23 23 unsigned gpio_base; 24 + struct gpio_desc **gpios; 25 + int *values; 24 26 }; 25 27 26 28 static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val) ··· 30 28 int i; 31 29 32 30 for (i = 0; i < mux->data.n_gpios; i++) 33 - gpio_set_value_cansleep(mux->gpio_base + mux->data.gpios[i], 34 - val & (1 << i)); 31 + mux->values[i] = (val >> i) & 1; 32 + 33 + gpiod_set_array_value_cansleep(mux->data.n_gpios, 34 + mux->gpios, mux->values); 35 35 } 36 36 37 37 static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan) ··· 180 176 if (!parent) 181 177 return -EPROBE_DEFER; 182 178 183 - muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 0, 0, 179 + muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 180 + mux->data.n_gpios * sizeof(*mux->gpios) + 181 + mux->data.n_gpios * sizeof(*mux->values), 0, 184 182 i2c_mux_gpio_select, NULL); 185 183 if (!muxc) { 186 184 ret = -ENOMEM; 187 185 goto alloc_failed; 188 186 } 187 + mux->gpios = muxc->priv; 188 + mux->values = (int *)(mux->gpios + mux->data.n_gpios); 189 189 muxc->priv = mux; 190 190 191 191 platform_set_drvdata(pdev, muxc); ··· 227 219 goto err_request_gpio; 228 220 } 229 221 222 + gpio_desc = gpio_to_desc(gpio_base + mux->data.gpios[i]); 223 + mux->gpios[i] = gpio_desc; 224 + 230 225 if (!muxc->mux_locked) 231 226 continue; 232 227 233 - gpio_desc = gpio_to_desc(gpio_base + mux->data.gpios[i]); 234 228 gpio_dev = &gpio_desc->gdev->dev; 235 229 muxc->mux_locked = i2c_root_adapter(gpio_dev) == root; 236 230 }
+220
drivers/i2c/muxes/i2c-mux-mlxcpld.c
··· 1 + /* 2 + * drivers/i2c/muxes/i2c-mux-mlxcpld.c 3 + * Copyright (c) 2016 Mellanox Technologies. All rights reserved. 4 + * Copyright (c) 2016 Michael Shych <michaels@mellanox.com> 5 + * 6 + * Redistribution and use in source and binary forms, with or without 7 + * modification, are permitted provided that the following conditions are met: 8 + * 9 + * 1. Redistributions of source code must retain the above copyright 10 + * notice, this list of conditions and the following disclaimer. 11 + * 2. Redistributions in binary form must reproduce the above copyright 12 + * notice, this list of conditions and the following disclaimer in the 13 + * documentation and/or other materials provided with the distribution. 14 + * 3. Neither the names of the copyright holders nor the names of its 15 + * contributors may be used to endorse or promote products derived from 16 + * this software without specific prior written permission. 17 + * 18 + * Alternatively, this software may be distributed under the terms of the 19 + * GNU General Public License ("GPL") version 2 as published by the Free 20 + * Software Foundation. 21 + * 22 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 26 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 + * POSSIBILITY OF SUCH DAMAGE. 33 + */ 34 + 35 + #include <linux/device.h> 36 + #include <linux/i2c.h> 37 + #include <linux/i2c-mux.h> 38 + #include <linux/io.h> 39 + #include <linux/init.h> 40 + #include <linux/module.h> 41 + #include <linux/platform_device.h> 42 + #include <linux/slab.h> 43 + #include <linux/version.h> 44 + #include <linux/i2c/mlxcpld.h> 45 + 46 + #define CPLD_MUX_MAX_NCHANS 8 47 + 48 + /* mlxcpld_mux - mux control structure: 49 + * @last_chan - last register value 50 + * @client - I2C device client 51 + */ 52 + struct mlxcpld_mux { 53 + u8 last_chan; 54 + struct i2c_client *client; 55 + }; 56 + 57 + /* MUX logic description. 58 + * Driver can support different mux control logic, according to CPLD 59 + * implementation. 60 + * 61 + * Connectivity schema. 62 + * 63 + * i2c-mlxcpld Digital Analog 64 + * driver 65 + * *--------* * -> mux1 (virt bus2) -> mux -> | 66 + * | I2CLPC | i2c physical * -> mux2 (virt bus3) -> mux -> | 67 + * | bridge | bus 1 *---------* | 68 + * | logic |---------------------> * mux reg * | 69 + * | in CPLD| *---------* | 70 + * *--------* i2c-mux-mlxpcld ^ * -> muxn (virt busn) -> mux -> | 71 + * | driver | | 72 + * | *---------------* | Devices 73 + * | * CPLD (i2c bus)* select | 74 + * | * registers for *--------* 75 + * | * mux selection * deselect 76 + * | *---------------* 77 + * | | 78 + * <--------> <-----------> 79 + * i2c cntrl Board cntrl reg 80 + * reg space space (mux select, 81 + * IO, LED, WD, info) 82 + * 83 + */ 84 + 85 + static const struct i2c_device_id mlxcpld_mux_id[] = { 86 + { "mlxcpld_mux_module", 0 }, 87 + { } 88 + }; 89 + MODULE_DEVICE_TABLE(i2c, mlxcpld_mux_id); 90 + 91 + /* Write to mux register. Don't use i2c_transfer() and i2c_smbus_xfer() 92 + * for this as they will try to lock adapter a second time. 93 + */ 94 + static int mlxcpld_mux_reg_write(struct i2c_adapter *adap, 95 + struct i2c_client *client, u8 val) 96 + { 97 + struct mlxcpld_mux_plat_data *pdata = dev_get_platdata(&client->dev); 98 + 99 + if (adap->algo->master_xfer) { 100 + struct i2c_msg msg; 101 + u8 msgbuf[] = {pdata->sel_reg_addr, val}; 102 + 103 + msg.addr = client->addr; 104 + msg.flags = 0; 105 + msg.len = 2; 106 + msg.buf = msgbuf; 107 + return __i2c_transfer(adap, &msg, 1); 108 + } else if (adap->algo->smbus_xfer) { 109 + union i2c_smbus_data data; 110 + 111 + data.byte = val; 112 + return adap->algo->smbus_xfer(adap, client->addr, 113 + client->flags, I2C_SMBUS_WRITE, 114 + pdata->sel_reg_addr, 115 + I2C_SMBUS_BYTE_DATA, &data); 116 + } else 117 + return -ENODEV; 118 + } 119 + 120 + static int mlxcpld_mux_select_chan(struct i2c_mux_core *muxc, u32 chan) 121 + { 122 + struct mlxcpld_mux *data = i2c_mux_priv(muxc); 123 + struct i2c_client *client = data->client; 124 + u8 regval = chan + 1; 125 + int err = 0; 126 + 127 + /* Only select the channel if its different from the last channel */ 128 + if (data->last_chan != regval) { 129 + err = mlxcpld_mux_reg_write(muxc->parent, client, regval); 130 + if (err) 131 + data->last_chan = 0; 132 + else 133 + data->last_chan = regval; 134 + } 135 + 136 + return err; 137 + } 138 + 139 + static int mlxcpld_mux_deselect(struct i2c_mux_core *muxc, u32 chan) 140 + { 141 + struct mlxcpld_mux *data = i2c_mux_priv(muxc); 142 + struct i2c_client *client = data->client; 143 + 144 + /* Deselect active channel */ 145 + data->last_chan = 0; 146 + 147 + return mlxcpld_mux_reg_write(muxc->parent, client, data->last_chan); 148 + } 149 + 150 + /* Probe/reomove functions */ 151 + static int mlxcpld_mux_probe(struct i2c_client *client, 152 + const struct i2c_device_id *id) 153 + { 154 + struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent); 155 + struct mlxcpld_mux_plat_data *pdata = dev_get_platdata(&client->dev); 156 + struct i2c_mux_core *muxc; 157 + int num, force; 158 + struct mlxcpld_mux *data; 159 + int err; 160 + 161 + if (!pdata) 162 + return -EINVAL; 163 + 164 + if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) 165 + return -ENODEV; 166 + 167 + muxc = i2c_mux_alloc(adap, &client->dev, CPLD_MUX_MAX_NCHANS, 168 + sizeof(*data), 0, mlxcpld_mux_select_chan, 169 + mlxcpld_mux_deselect); 170 + if (!muxc) 171 + return -ENOMEM; 172 + 173 + data = i2c_mux_priv(muxc); 174 + i2c_set_clientdata(client, muxc); 175 + data->client = client; 176 + data->last_chan = 0; /* force the first selection */ 177 + 178 + /* Create an adapter for each channel. */ 179 + for (num = 0; num < CPLD_MUX_MAX_NCHANS; num++) { 180 + if (num >= pdata->num_adaps) 181 + /* discard unconfigured channels */ 182 + break; 183 + 184 + force = pdata->adap_ids[num]; 185 + 186 + err = i2c_mux_add_adapter(muxc, force, num, 0); 187 + if (err) 188 + goto virt_reg_failed; 189 + } 190 + 191 + return 0; 192 + 193 + virt_reg_failed: 194 + i2c_mux_del_adapters(muxc); 195 + return err; 196 + } 197 + 198 + static int mlxcpld_mux_remove(struct i2c_client *client) 199 + { 200 + struct i2c_mux_core *muxc = i2c_get_clientdata(client); 201 + 202 + i2c_mux_del_adapters(muxc); 203 + return 0; 204 + } 205 + 206 + static struct i2c_driver mlxcpld_mux_driver = { 207 + .driver = { 208 + .name = "mlxcpld-mux", 209 + }, 210 + .probe = mlxcpld_mux_probe, 211 + .remove = mlxcpld_mux_remove, 212 + .id_table = mlxcpld_mux_id, 213 + }; 214 + 215 + module_i2c_driver(mlxcpld_mux_driver); 216 + 217 + MODULE_AUTHOR("Michael Shych (michaels@mellanox.com)"); 218 + MODULE_DESCRIPTION("Mellanox I2C-CPLD-MUX driver"); 219 + MODULE_LICENSE("Dual BSD/GPL"); 220 + MODULE_ALIAS("platform:i2c-mux-mlxcpld");
+27 -1
drivers/i2c/muxes/i2c-mux-pca954x.c
··· 35 35 * warranty of any kind, whether express or implied. 36 36 */ 37 37 38 + #include <linux/acpi.h> 38 39 #include <linux/device.h> 39 40 #include <linux/gpio/consumer.h> 40 41 #include <linux/i2c.h> ··· 120 119 { } 121 120 }; 122 121 MODULE_DEVICE_TABLE(i2c, pca954x_id); 122 + 123 + #ifdef CONFIG_ACPI 124 + static const struct acpi_device_id pca954x_acpi_ids[] = { 125 + { .id = "PCA9540", .driver_data = pca_9540 }, 126 + { .id = "PCA9542", .driver_data = pca_9540 }, 127 + { .id = "PCA9543", .driver_data = pca_9543 }, 128 + { .id = "PCA9544", .driver_data = pca_9544 }, 129 + { .id = "PCA9545", .driver_data = pca_9545 }, 130 + { .id = "PCA9546", .driver_data = pca_9545 }, 131 + { .id = "PCA9547", .driver_data = pca_9547 }, 132 + { .id = "PCA9548", .driver_data = pca_9548 }, 133 + { } 134 + }; 135 + MODULE_DEVICE_TABLE(acpi, pca954x_acpi_ids); 136 + #endif 123 137 124 138 #ifdef CONFIG_OF 125 139 static const struct of_device_id pca954x_of_match[] = { ··· 261 245 match = of_match_device(of_match_ptr(pca954x_of_match), &client->dev); 262 246 if (match) 263 247 data->chip = of_device_get_match_data(&client->dev); 264 - else 248 + else if (id) 265 249 data->chip = &chips[id->driver_data]; 250 + else { 251 + const struct acpi_device_id *acpi_id; 252 + 253 + acpi_id = acpi_match_device(ACPI_PTR(pca954x_acpi_ids), 254 + &client->dev); 255 + if (!acpi_id) 256 + return -ENODEV; 257 + data->chip = &chips[acpi_id->driver_data]; 258 + } 266 259 267 260 data->last_chan = 0; /* force the first selection */ 268 261 ··· 346 321 .name = "pca954x", 347 322 .pm = &pca954x_pm, 348 323 .of_match_table = of_match_ptr(pca954x_of_match), 324 + .acpi_match_table = ACPI_PTR(pca954x_acpi_ids), 349 325 }, 350 326 .probe = pca954x_probe, 351 327 .remove = pca954x_remove,
+2 -3
drivers/mfd/88pm860x-core.c
··· 1132 1132 return 0; 1133 1133 } 1134 1134 1135 - static int pm860x_probe(struct i2c_client *client, 1136 - const struct i2c_device_id *id) 1135 + static int pm860x_probe(struct i2c_client *client) 1137 1136 { 1138 1137 struct pm860x_platform_data *pdata = dev_get_platdata(&client->dev); 1139 1138 struct device_node *node = client->dev.of_node; ··· 1258 1259 .pm = &pm860x_pm_ops, 1259 1260 .of_match_table = pm860x_dt_ids, 1260 1261 }, 1261 - .probe = pm860x_probe, 1262 + .probe_new = pm860x_probe, 1262 1263 .remove = pm860x_remove, 1263 1264 .id_table = pm860x_id_table, 1264 1265 };
-27
include/linux/i2c-smbus.h
··· 50 50 struct i2c_smbus_alert_setup *setup); 51 51 int i2c_handle_smbus_alert(struct i2c_client *ara); 52 52 53 - /** 54 - * smbus_host_notify - internal structure used by the Host Notify mechanism. 55 - * @adapter: the I2C adapter associated with this struct 56 - * @work: worker used to schedule the IRQ in the slave device 57 - * @lock: spinlock to check if a notification is already pending 58 - * @pending: flag set when a notification is pending (any new notification will 59 - * be rejected if pending is true) 60 - * @payload: the actual payload of the Host Notify event 61 - * @addr: the address of the slave device which raised the notification 62 - * 63 - * This struct needs to be allocated by i2c_setup_smbus_host_notify() and does 64 - * not need to be freed. Internally, i2c_setup_smbus_host_notify() uses a 65 - * managed resource to clean this up when the adapter get released. 66 - */ 67 - struct smbus_host_notify { 68 - struct i2c_adapter *adapter; 69 - struct work_struct work; 70 - spinlock_t lock; 71 - bool pending; 72 - u16 payload; 73 - u8 addr; 74 - }; 75 - 76 - struct smbus_host_notify *i2c_setup_smbus_host_notify(struct i2c_adapter *adap); 77 - int i2c_handle_smbus_host_notify(struct smbus_host_notify *host_notify, 78 - unsigned short addr, unsigned int data); 79 - 80 53 #endif /* _LINUX_I2C_SMBUS_H */
+25 -1
include/linux/i2c.h
··· 30 30 #include <linux/device.h> /* for struct device */ 31 31 #include <linux/sched.h> /* for completion */ 32 32 #include <linux/mutex.h> 33 + #include <linux/irqdomain.h> /* for Host Notify IRQ */ 33 34 #include <linux/of.h> /* for struct device_node */ 34 35 #include <linux/swab.h> /* for swab16 */ 35 36 #include <uapi/linux/i2c.h> ··· 136 135 * struct i2c_driver - represent an I2C device driver 137 136 * @class: What kind of i2c device we instantiate (for detect) 138 137 * @attach_adapter: Callback for bus addition (deprecated) 139 - * @probe: Callback for device binding 138 + * @probe: Callback for device binding - soon to be deprecated 139 + * @probe_new: New callback for device binding 140 140 * @remove: Callback for device unbinding 141 141 * @shutdown: Callback for device shutdown 142 142 * @alert: Alert callback, for example for the SMBus alert protocol ··· 179 177 /* Standard driver model interfaces */ 180 178 int (*probe)(struct i2c_client *, const struct i2c_device_id *); 181 179 int (*remove)(struct i2c_client *); 180 + 181 + /* New driver model interface to aid the seamless removal of the 182 + * current probe()'s, more commonly unused than used second parameter. 183 + */ 184 + int (*probe_new)(struct i2c_client *); 182 185 183 186 /* driver model interfaces that don't relate to enumeration */ 184 187 void (*shutdown)(struct i2c_client *); ··· 250 243 251 244 extern struct i2c_client *i2c_verify_client(struct device *dev); 252 245 extern struct i2c_adapter *i2c_verify_adapter(struct device *dev); 246 + extern const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, 247 + const struct i2c_client *client); 253 248 254 249 static inline struct i2c_client *kobj_to_i2c_client(struct kobject *kobj) 255 250 { ··· 576 567 577 568 struct i2c_bus_recovery_info *bus_recovery_info; 578 569 const struct i2c_adapter_quirks *quirks; 570 + 571 + struct irq_domain *host_notify_domain; 579 572 }; 580 573 #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev) 581 574 ··· 750 739 return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0); 751 740 } 752 741 742 + int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr); 753 743 /** 754 744 * module_i2c_driver() - Helper macro for registering a modular I2C driver 755 745 * @__i2c_driver: i2c_driver struct ··· 786 774 /* must call i2c_put_adapter() when done with returned i2c_adapter device */ 787 775 struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node); 788 776 777 + extern const struct of_device_id 778 + *i2c_of_match_device(const struct of_device_id *matches, 779 + struct i2c_client *client); 780 + 789 781 #else 790 782 791 783 static inline struct i2c_client *of_find_i2c_device_by_node(struct device_node *node) ··· 806 790 { 807 791 return NULL; 808 792 } 793 + 794 + static inline const struct of_device_id 795 + *i2c_of_match_device(const struct of_device_id *matches, 796 + struct i2c_client *client) 797 + { 798 + return NULL; 799 + } 800 + 809 801 #endif /* CONFIG_OF */ 810 802 811 803 #if IS_ENABLED(CONFIG_ACPI)
+52
include/linux/i2c/mlxcpld.h
··· 1 + /* 2 + * mlxcpld.h - Mellanox I2C multiplexer support in CPLD 3 + * 4 + * Copyright (c) 2016 Mellanox Technologies. All rights reserved. 5 + * Copyright (c) 2016 Michael Shych <michaels@mellanox.com> 6 + * 7 + * Redistribution and use in source and binary forms, with or without 8 + * modification, are permitted provided that the following conditions are met: 9 + * 10 + * 1. Redistributions of source code must retain the above copyright 11 + * notice, this list of conditions and the following disclaimer. 12 + * 2. Redistributions in binary form must reproduce the above copyright 13 + * notice, this list of conditions and the following disclaimer in the 14 + * documentation and/or other materials provided with the distribution. 15 + * 3. Neither the names of the copyright holders nor the names of its 16 + * contributors may be used to endorse or promote products derived from 17 + * this software without specific prior written permission. 18 + * 19 + * Alternatively, this software may be distributed under the terms of the 20 + * GNU General Public License ("GPL") version 2 as published by the Free 21 + * Software Foundation. 22 + * 23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 27 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 + * POSSIBILITY OF SUCH DAMAGE. 34 + */ 35 + 36 + #ifndef _LINUX_I2C_MLXCPLD_H 37 + #define _LINUX_I2C_MLXCPLD_H 38 + 39 + /* Platform data for the CPLD I2C multiplexers */ 40 + 41 + /* mlxcpld_mux_plat_data - per mux data, used with i2c_register_board_info 42 + * @adap_ids - adapter array 43 + * @num_adaps - number of adapters 44 + * @sel_reg_addr - mux select register offset in CPLD space 45 + */ 46 + struct mlxcpld_mux_plat_data { 47 + int *adap_ids; 48 + int num_adaps; 49 + int sel_reg_addr; 50 + }; 51 + 52 + #endif /* _LINUX_I2C_MLXCPLD_H */