aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2018-08-21 20:40:46 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2018-08-21 20:40:46 -0400
commit99cc7ad46b62ef20b0478147677bebd1157bd9cf (patch)
treef07f33826e6947108234707030867d969230611d
parent0214f46b3a0383d6e33c297e7706216b6a550e4b (diff)
parent19358d4488db7e6a04e940730cc75909d7d1e0d8 (diff)
Merge branch 'i2c/for-4.19' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux
Pull i2c updates from Wolfram Sang: - the core has now a lockless variant of i2c_smbus_xfer. Some open coded versions of this got removed in drivers. This also enables proper SCCB support in regmap. - locking got a more precise naming. i2c_{un}lock_adapter() had to go, and we know use i2c_lock_bus() consistently with flags like I2C_LOCK_ROOT_ADAPTER and I2C_LOCK_SEGMENT to avoid ambiguity. - the gpio fault injector got a new delicate testcase - the bus recovery procedure got fixed to handle the new testcase correctly - a new quirk flag for controllers not able to handle zero length messages together with driver updates to use it - new drivers: FSI bus attached I2C masters, GENI I2C controller, Owl family S900 - and a good set of driver improvements and bugfixes * 'i2c/for-4.19' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux: (77 commits) i2c: rcar: implement STOP and REP_START according to docs i2c: rcar: refactor private flags i2c: core: ACPI: Make acpi_gsb_i2c_read_bytes() check i2c_transfer return value i2c: core: ACPI: Properly set status byte to 0 for multi-byte writes dt-bindings: i2c: rcar: Add r8a774a1 support dt-bindings: i2c: sh_mobile: Add r8a774a1 support i2c: imx: Simplify stopped state tracking i2c: imx: Fix race condition in dma read i2c: pasemi: remove hardcoded bus numbers on smbus i2c: designware: Add SPDX license tag i2c: designware: Convert to use struct i2c_timings i2c: core: Parse SDA hold time from firmware i2c: designware-pcidrv: Mark expected switch fall-through i2c: amd8111: Mark expected switch fall-through i2c: sh_mobile: use core to detect 'no zero length read' quirk i2c: xlr: use core to detect 'no zero length' quirk i2c: rcar: use core to detect 'no zero length' quirk i2c: stu300: use core to detect 'no zero length' quirk i2c: pmcmsp: use core to detect 'no zero length' quirk i2c: mxs: use core to detect 'no zero length' quirk ...
-rw-r--r--Documentation/devicetree/bindings/eeprom/at24.txt2
-rw-r--r--Documentation/devicetree/bindings/i2c/i2c-fsi.txt40
-rw-r--r--Documentation/devicetree/bindings/i2c/i2c-owl.txt27
-rw-r--r--Documentation/devicetree/bindings/i2c/i2c-rcar.txt5
-rw-r--r--Documentation/devicetree/bindings/i2c/i2c-sh_mobile.txt4
-rw-r--r--Documentation/i2c/busses/i2c-i80111
-rw-r--r--Documentation/i2c/gpio-fault-injection49
-rw-r--r--MAINTAINERS16
-rw-r--r--drivers/i2c/busses/Kconfig42
-rw-r--r--drivers/i2c/busses/Makefile3
-rw-r--r--drivers/i2c/busses/i2c-amd8111.c1
-rw-r--r--drivers/i2c/busses/i2c-aspeed.c40
-rw-r--r--drivers/i2c/busses/i2c-brcmstb.c8
-rw-r--r--drivers/i2c/busses/i2c-davinci.c4
-rw-r--r--drivers/i2c/busses/i2c-designware-baytrail.c10
-rw-r--r--drivers/i2c/busses/i2c-designware-common.c91
-rw-r--r--drivers/i2c/busses/i2c-designware-core.h24
-rw-r--r--drivers/i2c/busses/i2c-designware-master.c223
-rw-r--r--drivers/i2c/busses/i2c-designware-pcidrv.c17
-rw-r--r--drivers/i2c/busses/i2c-designware-platdrv.c68
-rw-r--r--drivers/i2c/busses/i2c-designware-slave.c62
-rw-r--r--drivers/i2c/busses/i2c-exynos5.c22
-rw-r--r--drivers/i2c/busses/i2c-fsi.c752
-rw-r--r--drivers/i2c/busses/i2c-gpio.c97
-rw-r--r--drivers/i2c/busses/i2c-i801.c4
-rw-r--r--drivers/i2c/busses/i2c-imx.c23
-rw-r--r--drivers/i2c/busses/i2c-mxs.c8
-rw-r--r--drivers/i2c/busses/i2c-owl.c495
-rw-r--r--drivers/i2c/busses/i2c-pasemi.c3
-rw-r--r--drivers/i2c/busses/i2c-pmcmsp.c17
-rw-r--r--drivers/i2c/busses/i2c-qcom-geni.c673
-rw-r--r--drivers/i2c/busses/i2c-rcar.c73
-rw-r--r--drivers/i2c/busses/i2c-s3c2410.c4
-rw-r--r--drivers/i2c/busses/i2c-sh_mobile.c10
-rw-r--r--drivers/i2c/busses/i2c-sprd.c8
-rw-r--r--drivers/i2c/busses/i2c-stu300.c12
-rw-r--r--drivers/i2c/busses/i2c-tegra.c90
-rw-r--r--drivers/i2c/busses/i2c-xlr.c11
-rw-r--r--drivers/i2c/i2c-core-acpi.c19
-rw-r--r--drivers/i2c/i2c-core-base.c85
-rw-r--r--drivers/i2c/i2c-core-slave.c8
-rw-r--r--drivers/i2c/i2c-mux.c4
-rw-r--r--drivers/i2c/muxes/i2c-mux-mlxcpld.c28
-rw-r--r--drivers/i2c/muxes/i2c-mux-pca9541.c75
-rw-r--r--drivers/i2c/muxes/i2c-mux-pca954x.c51
-rw-r--r--drivers/iio/temperature/mlx90614.c4
-rw-r--r--drivers/input/touchscreen/rohm_bu21023.c4
-rw-r--r--drivers/media/dvb-frontends/af9013.c8
-rw-r--r--drivers/media/dvb-frontends/drxk_hard.c4
-rw-r--r--drivers/media/dvb-frontends/rtl2830.c12
-rw-r--r--drivers/media/dvb-frontends/tda1004x.c6
-rw-r--r--drivers/media/tuners/tda18271-common.c8
-rw-r--r--drivers/mfd/88pm860x-i2c.c8
-rw-r--r--drivers/misc/eeprom/at24.c17
-rw-r--r--include/linux/i2c.h33
55 files changed, 2765 insertions, 658 deletions
diff --git a/Documentation/devicetree/bindings/eeprom/at24.txt b/Documentation/devicetree/bindings/eeprom/at24.txt
index 61d833abafbf..aededdbc262b 100644
--- a/Documentation/devicetree/bindings/eeprom/at24.txt
+++ b/Documentation/devicetree/bindings/eeprom/at24.txt
@@ -72,6 +72,8 @@ Optional properties:
72 72
73 - wp-gpios: GPIO to which the write-protect pin of the chip is connected. 73 - wp-gpios: GPIO to which the write-protect pin of the chip is connected.
74 74
75 - address-width: number of address bits (one of 8, 16).
76
75Example: 77Example:
76 78
77eeprom@52 { 79eeprom@52 {
diff --git a/Documentation/devicetree/bindings/i2c/i2c-fsi.txt b/Documentation/devicetree/bindings/i2c/i2c-fsi.txt
new file mode 100644
index 000000000000..b1be2ceb7e69
--- /dev/null
+++ b/Documentation/devicetree/bindings/i2c/i2c-fsi.txt
@@ -0,0 +1,40 @@
1Device-tree bindings for FSI-attached I2C master and busses
2-----------------------------------------------------------
3
4Required properties:
5 - compatible = "ibm,i2c-fsi";
6 - reg = < address size >; : The FSI CFAM address and address
7 space size.
8 - #address-cells = <1>; : Number of address cells in child
9 nodes.
10 - #size-cells = <0>; : Number of size cells in child nodes.
11 - child nodes : Nodes to describe busses off the I2C
12 master.
13
14Child node required properties:
15 - reg = < port number > : The port number on the I2C master.
16
17Child node optional properties:
18 - child nodes : Nodes to describe devices on the I2C
19 bus.
20
21Examples:
22
23 i2c@1800 {
24 compatible = "ibm,i2c-fsi";
25 reg = < 0x1800 0x400 >;
26 #address-cells = <1>;
27 #size-cells = <0>;
28
29 i2c-bus@0 {
30 reg = <0>;
31 };
32
33 i2c-bus@1 {
34 reg = <1>;
35
36 eeprom@50 {
37 compatible = "vendor,dev-name";
38 };
39 };
40 };
diff --git a/Documentation/devicetree/bindings/i2c/i2c-owl.txt b/Documentation/devicetree/bindings/i2c/i2c-owl.txt
new file mode 100644
index 000000000000..b743fe444e9f
--- /dev/null
+++ b/Documentation/devicetree/bindings/i2c/i2c-owl.txt
@@ -0,0 +1,27 @@
1Actions Semiconductor Owl I2C controller
2
3Required properties:
4
5- compatible : Should be "actions,s900-i2c".
6- reg : Offset and length of the register set for the device.
7- #address-cells : Should be 1.
8- #size-cells : Should be 0.
9- interrupts : A single interrupt specifier.
10- clocks : Phandle of the clock feeding the I2C controller.
11
12Optional properties:
13
14- clock-frequency : Desired I2C bus clock frequency in Hz. As only Normal and
15 Fast modes are supported, possible values are 100000 and
16 400000.
17Examples:
18
19 i2c0: i2c@e0170000 {
20 compatible = "actions,s900-i2c";
21 reg = <0 0xe0170000 0 0x1000>;
22 #address-cells = <1>;
23 #size-cells = <0>;
24 interrupts = <GIC_SPI 25 IRQ_TYPE_LEVEL_HIGH>;
25 clocks = <&clock CLK_I2C0>;
26 clock-frequency = <100000>;
27 };
diff --git a/Documentation/devicetree/bindings/i2c/i2c-rcar.txt b/Documentation/devicetree/bindings/i2c/i2c-rcar.txt
index 7ce8fae55537..39cd21d95810 100644
--- a/Documentation/devicetree/bindings/i2c/i2c-rcar.txt
+++ b/Documentation/devicetree/bindings/i2c/i2c-rcar.txt
@@ -4,6 +4,7 @@ Required properties:
4- compatible: 4- compatible:
5 "renesas,i2c-r8a7743" if the device is a part of a R8A7743 SoC. 5 "renesas,i2c-r8a7743" if the device is a part of a R8A7743 SoC.
6 "renesas,i2c-r8a7745" if the device is a part of a R8A7745 SoC. 6 "renesas,i2c-r8a7745" if the device is a part of a R8A7745 SoC.
7 "renesas,i2c-r8a774a1" if the device is a part of a R8A774A1 SoC.
7 "renesas,i2c-r8a7778" if the device is a part of a R8A7778 SoC. 8 "renesas,i2c-r8a7778" if the device is a part of a R8A7778 SoC.
8 "renesas,i2c-r8a7779" if the device is a part of a R8A7779 SoC. 9 "renesas,i2c-r8a7779" if the device is a part of a R8A7779 SoC.
9 "renesas,i2c-r8a7790" if the device is a part of a R8A7790 SoC. 10 "renesas,i2c-r8a7790" if the device is a part of a R8A7790 SoC.
@@ -16,11 +17,13 @@ Required properties:
16 "renesas,i2c-r8a77965" if the device is a part of a R8A77965 SoC. 17 "renesas,i2c-r8a77965" if the device is a part of a R8A77965 SoC.
17 "renesas,i2c-r8a77970" if the device is a part of a R8A77970 SoC. 18 "renesas,i2c-r8a77970" if the device is a part of a R8A77970 SoC.
18 "renesas,i2c-r8a77980" if the device is a part of a R8A77980 SoC. 19 "renesas,i2c-r8a77980" if the device is a part of a R8A77980 SoC.
20 "renesas,i2c-r8a77990" if the device is a part of a R8A77990 SoC.
19 "renesas,i2c-r8a77995" if the device is a part of a R8A77995 SoC. 21 "renesas,i2c-r8a77995" if the device is a part of a R8A77995 SoC.
20 "renesas,rcar-gen1-i2c" for a generic R-Car Gen1 compatible device. 22 "renesas,rcar-gen1-i2c" for a generic R-Car Gen1 compatible device.
21 "renesas,rcar-gen2-i2c" for a generic R-Car Gen2 or RZ/G1 compatible 23 "renesas,rcar-gen2-i2c" for a generic R-Car Gen2 or RZ/G1 compatible
22 device. 24 device.
23 "renesas,rcar-gen3-i2c" for a generic R-Car Gen3 compatible device. 25 "renesas,rcar-gen3-i2c" for a generic R-Car Gen3 or RZ/G2 compatible
26 device.
24 "renesas,i2c-rcar" (deprecated) 27 "renesas,i2c-rcar" (deprecated)
25 28
26 When compatible with the generic version, nodes must list the 29 When compatible with the generic version, nodes must list the
diff --git a/Documentation/devicetree/bindings/i2c/i2c-sh_mobile.txt b/Documentation/devicetree/bindings/i2c/i2c-sh_mobile.txt
index fc7e17802746..872673adff5a 100644
--- a/Documentation/devicetree/bindings/i2c/i2c-sh_mobile.txt
+++ b/Documentation/devicetree/bindings/i2c/i2c-sh_mobile.txt
@@ -6,6 +6,7 @@ Required properties:
6 - "renesas,iic-r8a7740" (R-Mobile A1) 6 - "renesas,iic-r8a7740" (R-Mobile A1)
7 - "renesas,iic-r8a7743" (RZ/G1M) 7 - "renesas,iic-r8a7743" (RZ/G1M)
8 - "renesas,iic-r8a7745" (RZ/G1E) 8 - "renesas,iic-r8a7745" (RZ/G1E)
9 - "renesas,iic-r8a774a1" (RZ/G2M)
9 - "renesas,iic-r8a7790" (R-Car H2) 10 - "renesas,iic-r8a7790" (R-Car H2)
10 - "renesas,iic-r8a7791" (R-Car M2-W) 11 - "renesas,iic-r8a7791" (R-Car M2-W)
11 - "renesas,iic-r8a7792" (R-Car V2H) 12 - "renesas,iic-r8a7792" (R-Car V2H)
@@ -17,7 +18,8 @@ Required properties:
17 - "renesas,iic-sh73a0" (SH-Mobile AG5) 18 - "renesas,iic-sh73a0" (SH-Mobile AG5)
18 - "renesas,rcar-gen2-iic" (generic R-Car Gen2 or RZ/G1 19 - "renesas,rcar-gen2-iic" (generic R-Car Gen2 or RZ/G1
19 compatible device) 20 compatible device)
20 - "renesas,rcar-gen3-iic" (generic R-Car Gen3 compatible device) 21 - "renesas,rcar-gen3-iic" (generic R-Car Gen3 or RZ/G2
22 compatible device)
21 - "renesas,rmobile-iic" (generic device) 23 - "renesas,rmobile-iic" (generic device)
22 24
23 When compatible with a generic R-Car version, nodes 25 When compatible with a generic R-Car version, nodes
diff --git a/Documentation/i2c/busses/i2c-i801 b/Documentation/i2c/busses/i2c-i801
index 65514c251318..d1ee484a787d 100644
--- a/Documentation/i2c/busses/i2c-i801
+++ b/Documentation/i2c/busses/i2c-i801
@@ -21,24 +21,21 @@ Supported adapters:
21 * Intel DH89xxCC (PCH) 21 * Intel DH89xxCC (PCH)
22 * Intel Panther Point (PCH) 22 * Intel Panther Point (PCH)
23 * Intel Lynx Point (PCH) 23 * Intel Lynx Point (PCH)
24 * Intel Lynx Point-LP (PCH)
25 * Intel Avoton (SOC) 24 * Intel Avoton (SOC)
26 * Intel Wellsburg (PCH) 25 * Intel Wellsburg (PCH)
27 * Intel Coleto Creek (PCH) 26 * Intel Coleto Creek (PCH)
28 * Intel Wildcat Point (PCH) 27 * Intel Wildcat Point (PCH)
29 * Intel Wildcat Point-LP (PCH)
30 * Intel BayTrail (SOC) 28 * Intel BayTrail (SOC)
31 * Intel Braswell (SOC) 29 * Intel Braswell (SOC)
32 * Intel Sunrise Point-H (PCH) 30 * Intel Sunrise Point (PCH)
33 * Intel Sunrise Point-LP (PCH) 31 * Intel Kaby Lake (PCH)
34 * Intel Kaby Lake-H (PCH)
35 * Intel DNV (SOC) 32 * Intel DNV (SOC)
36 * Intel Broxton (SOC) 33 * Intel Broxton (SOC)
37 * Intel Lewisburg (PCH) 34 * Intel Lewisburg (PCH)
38 * Intel Gemini Lake (SOC) 35 * Intel Gemini Lake (SOC)
39 * Intel Cannon Lake-H (PCH) 36 * Intel Cannon Lake (PCH)
40 * Intel Cannon Lake-LP (PCH)
41 * Intel Cedar Fork (PCH) 37 * Intel Cedar Fork (PCH)
38 * Intel Ice Lake (PCH)
42 Datasheets: Publicly available at the Intel website 39 Datasheets: Publicly available at the Intel website
43 40
44On Intel Patsburg and later chipsets, both the normal host SMBus controller 41On Intel Patsburg and later chipsets, both the normal host SMBus controller
diff --git a/Documentation/i2c/gpio-fault-injection b/Documentation/i2c/gpio-fault-injection
index e0c4f775e239..a4ce62090fd5 100644
--- a/Documentation/i2c/gpio-fault-injection
+++ b/Documentation/i2c/gpio-fault-injection
@@ -34,21 +34,48 @@ I2C specification version 4, section 3.1.16) using the helpers of the Linux I2C
34core (see 'struct bus_recovery_info'). However, the bus recovery will not 34core (see 'struct bus_recovery_info'). However, the bus recovery will not
35succeed because SDA is still pinned low until you manually release it again 35succeed because SDA is still pinned low until you manually release it again
36with "echo 1 > sda". A test with an automatic release can be done with the 36with "echo 1 > sda". A test with an automatic release can be done with the
37'incomplete_transfer' file. 37following class of fault injectors.
38 38
39"incomplete_transfer" 39Introduction to incomplete transfers
40--------------------- 40------------------------------------
41
42The following fault injectors create situations where SDA will be held low by a
43device. Bus recovery should be able to fix these situations. But please note:
44there are I2C client devices which detect a stuck SDA on their side and release
45it on their own after a few milliseconds. Also, there might be an external
46device deglitching and monitoring the I2C bus. It could also detect a stuck SDA
47and will init a bus recovery on its own. If you want to implement bus recovery
48in a bus master driver, make sure you checked your hardware setup for such
49devices before. And always verify with a scope or logic analyzer!
50
51"incomplete_address_phase"
52--------------------------
41 53
42This file is write only and you need to write the address of an existing I2C 54This file is write only and you need to write the address of an existing I2C
43client device to it. Then, a transfer to this device will be started, but it 55client device to it. Then, a read transfer to this device will be started, but
44will stop at the ACK phase after the address of the client has been 56it will stop at the ACK phase after the address of the client has been
45transmitted. Because the device will ACK its presence, this results in SDA 57transmitted. Because the device will ACK its presence, this results in SDA
46being pulled low by the device while SCL is high. So, similar to the "sda" file 58being pulled low by the device while SCL is high. So, similar to the "sda" file
47above, the bus master under test should detect this condition and try a bus 59above, the bus master under test should detect this condition and try a bus
48recovery. This time, however, it should succeed and the device should release 60recovery. This time, however, it should succeed and the device should release
49SDA after toggling SCL. Please note: there are I2C client devices which detect 61SDA after toggling SCL.
50a stuck SDA on their side and release it on their own after a few milliseconds. 62
51Also, there are external devices deglitching and monitoring the I2C bus. They 63"incomplete_write_byte"
52can also detect a stuck SDA and will init a bus recovery on their own. If you 64-----------------------
53want to implement bus recovery in a bus master driver, make sure you checked 65
54your hardware setup carefully before. 66Similar to above, this file is write only and you need to write the address of
67an existing I2C client device to it.
68
69The injector will again stop at one ACK phase, so the device will keep SDA low
70because it acknowledges data. However, there are two differences compared to
71'incomplete_address_phase':
72
73a) the message sent out will be a write message
74b) after the address byte, a 0x00 byte will be transferred. Then, stop at ACK.
75
76This is a highly delicate state, the device is set up to write any data to
77register 0x00 (if it has registers) when further clock pulses happen on SCL.
78This is why bus recovery (up to 9 clock pulses) must either check SDA or send
79additional STOP conditions to ensure the bus has been released. Otherwise
80random data will be written to a device!
81
diff --git a/MAINTAINERS b/MAINTAINERS
index 5df1b3643196..e21e61f380fa 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5961,6 +5961,14 @@ F: fs/crypto/
5961F: include/linux/fscrypt*.h 5961F: include/linux/fscrypt*.h
5962F: Documentation/filesystems/fscrypt.rst 5962F: Documentation/filesystems/fscrypt.rst
5963 5963
5964FSI-ATTACHED I2C DRIVER
5965M: Eddie James <eajames@linux.vnet.ibm.com>
5966L: linux-i2c@vger.kernel.org
5967L: openbmc@lists.ozlabs.org (moderated for non-subscribers)
5968S: Maintained
5969F: drivers/i2c/busses/i2c-fsi.c
5970F: Documentation/devicetree/bindings/i2c/i2c-fsi.txt
5971
5964FSNOTIFY: FILESYSTEM NOTIFICATION INFRASTRUCTURE 5972FSNOTIFY: FILESYSTEM NOTIFICATION INFRASTRUCTURE
5965M: Jan Kara <jack@suse.cz> 5973M: Jan Kara <jack@suse.cz>
5966R: Amir Goldstein <amir73il@gmail.com> 5974R: Amir Goldstein <amir73il@gmail.com>
@@ -12014,6 +12022,14 @@ L: netdev@vger.kernel.org
12014S: Maintained 12022S: Maintained
12015F: drivers/net/ethernet/qualcomm/emac/ 12023F: drivers/net/ethernet/qualcomm/emac/
12016 12024
12025QUALCOMM GENERIC INTERFACE I2C DRIVER
12026M: Alok Chauhan <alokc@codeaurora.org>
12027M: Karthikeyan Ramasubramanian <kramasub@codeaurora.org>
12028L: linux-i2c@vger.kernel.org
12029L: linux-arm-msm@vger.kernel.org
12030S: Supported
12031F: drivers/i2c/busses/i2c-qcom-geni.c
12032
12017QUALCOMM HEXAGON ARCHITECTURE 12033QUALCOMM HEXAGON ARCHITECTURE
12018M: Richard Kuo <rkuo@codeaurora.org> 12034M: Richard Kuo <rkuo@codeaurora.org>
12019L: linux-hexagon@vger.kernel.org 12035L: linux-hexagon@vger.kernel.org
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index 4f8df2ec87b1..451d4ae50e66 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -116,24 +116,21 @@ config I2C_I801
116 DH89xxCC (PCH) 116 DH89xxCC (PCH)
117 Panther Point (PCH) 117 Panther Point (PCH)
118 Lynx Point (PCH) 118 Lynx Point (PCH)
119 Lynx Point-LP (PCH)
120 Avoton (SOC) 119 Avoton (SOC)
121 Wellsburg (PCH) 120 Wellsburg (PCH)
122 Coleto Creek (PCH) 121 Coleto Creek (PCH)
123 Wildcat Point (PCH) 122 Wildcat Point (PCH)
124 Wildcat Point-LP (PCH)
125 BayTrail (SOC) 123 BayTrail (SOC)
126 Braswell (SOC) 124 Braswell (SOC)
127 Sunrise Point-H (PCH) 125 Sunrise Point (PCH)
128 Sunrise Point-LP (PCH) 126 Kaby Lake (PCH)
129 Kaby Lake-H (PCH)
130 DNV (SOC) 127 DNV (SOC)
131 Broxton (SOC) 128 Broxton (SOC)
132 Lewisburg (PCH) 129 Lewisburg (PCH)
133 Gemini Lake (SOC) 130 Gemini Lake (SOC)
134 Cannon Lake-H (PCH) 131 Cannon Lake (PCH)
135 Cannon Lake-LP (PCH)
136 Cedar Fork (PCH) 132 Cedar Fork (PCH)
133 Ice Lake (PCH)
137 134
138 This driver can also be built as a module. If so, the module 135 This driver can also be built as a module. If so, the module
139 will be called i2c-i801. 136 will be called i2c-i801.
@@ -762,6 +759,13 @@ config I2C_OMAP
762 Like OMAP1510/1610/1710/5912 and OMAP242x. 759 Like OMAP1510/1610/1710/5912 and OMAP242x.
763 For details see http://www.ti.com/omap. 760 For details see http://www.ti.com/omap.
764 761
762config I2C_OWL
763 tristate "Actions Semiconductor Owl I2C Controller"
764 depends on ARCH_ACTIONS || COMPILE_TEST
765 help
766 Say Y here if you want to use the I2C bus controller on
767 the Actions Semiconductor Owl SoC's.
768
765config I2C_PASEMI 769config I2C_PASEMI
766 tristate "PA Semi SMBus interface" 770 tristate "PA Semi SMBus interface"
767 depends on PPC_PASEMI && PCI 771 depends on PPC_PASEMI && PCI
@@ -828,6 +832,19 @@ config I2C_PXA_SLAVE
828 is necessary for systems where the PXA may be a target on the 832 is necessary for systems where the PXA may be a target on the
829 I2C bus. 833 I2C bus.
830 834
835config I2C_QCOM_GENI
836 tristate "Qualcomm Technologies Inc.'s GENI based I2C controller"
837 depends on ARCH_QCOM || COMPILE_TEST
838 depends on QCOM_GENI_SE
839 help
840 This driver supports GENI serial engine based I2C controller in
841 master mode on the Qualcomm Technologies Inc.'s SoCs. If you say
842 yes to this option, support will be included for the built-in I2C
843 interface on the Qualcomm Technologies Inc.'s SoCs.
844
845 This driver can also be built as a module. If so, the module
846 will be called i2c-qcom-geni.
847
831config I2C_QUP 848config I2C_QUP
832 tristate "Qualcomm QUP based I2C controller" 849 tristate "Qualcomm QUP based I2C controller"
833 depends on ARCH_QCOM 850 depends on ARCH_QCOM
@@ -1330,4 +1347,15 @@ config I2C_ZX2967
1330 This driver can also be built as a module. If so, the module will be 1347 This driver can also be built as a module. If so, the module will be
1331 called i2c-zx2967. 1348 called i2c-zx2967.
1332 1349
1350config I2C_FSI
1351 tristate "FSI I2C driver"
1352 depends on FSI
1353 help
1354 Driver for FSI bus attached I2C masters. These are I2C masters that
1355 are connected to the system over an FSI bus, instead of the more
1356 common PCI or MMIO interface.
1357
1358 This driver can also be built as a module. If so, the module will be
1359 called as i2c-fsi.
1360
1333endmenu 1361endmenu
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index 5a869144a0c5..18b26af82b1c 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -76,6 +76,7 @@ obj-$(CONFIG_I2C_MXS) += i2c-mxs.o
76obj-$(CONFIG_I2C_NOMADIK) += i2c-nomadik.o 76obj-$(CONFIG_I2C_NOMADIK) += i2c-nomadik.o
77obj-$(CONFIG_I2C_OCORES) += i2c-ocores.o 77obj-$(CONFIG_I2C_OCORES) += i2c-ocores.o
78obj-$(CONFIG_I2C_OMAP) += i2c-omap.o 78obj-$(CONFIG_I2C_OMAP) += i2c-omap.o
79obj-$(CONFIG_I2C_OWL) += i2c-owl.o
79obj-$(CONFIG_I2C_PASEMI) += i2c-pasemi.o 80obj-$(CONFIG_I2C_PASEMI) += i2c-pasemi.o
80obj-$(CONFIG_I2C_PCA_PLATFORM) += i2c-pca-platform.o 81obj-$(CONFIG_I2C_PCA_PLATFORM) += i2c-pca-platform.o
81obj-$(CONFIG_I2C_PMCMSP) += i2c-pmcmsp.o 82obj-$(CONFIG_I2C_PMCMSP) += i2c-pmcmsp.o
@@ -83,6 +84,7 @@ obj-$(CONFIG_I2C_PNX) += i2c-pnx.o
83obj-$(CONFIG_I2C_PUV3) += i2c-puv3.o 84obj-$(CONFIG_I2C_PUV3) += i2c-puv3.o
84obj-$(CONFIG_I2C_PXA) += i2c-pxa.o 85obj-$(CONFIG_I2C_PXA) += i2c-pxa.o
85obj-$(CONFIG_I2C_PXA_PCI) += i2c-pxa-pci.o 86obj-$(CONFIG_I2C_PXA_PCI) += i2c-pxa-pci.o
87obj-$(CONFIG_I2C_QCOM_GENI) += i2c-qcom-geni.o
86obj-$(CONFIG_I2C_QUP) += i2c-qup.o 88obj-$(CONFIG_I2C_QUP) += i2c-qup.o
87obj-$(CONFIG_I2C_RIIC) += i2c-riic.o 89obj-$(CONFIG_I2C_RIIC) += i2c-riic.o
88obj-$(CONFIG_I2C_RK3X) += i2c-rk3x.o 90obj-$(CONFIG_I2C_RK3X) += i2c-rk3x.o
@@ -137,5 +139,6 @@ obj-$(CONFIG_I2C_PCA_ISA) += i2c-pca-isa.o
137obj-$(CONFIG_I2C_SIBYTE) += i2c-sibyte.o 139obj-$(CONFIG_I2C_SIBYTE) += i2c-sibyte.o
138obj-$(CONFIG_I2C_XGENE_SLIMPRO) += i2c-xgene-slimpro.o 140obj-$(CONFIG_I2C_XGENE_SLIMPRO) += i2c-xgene-slimpro.o
139obj-$(CONFIG_SCx200_ACB) += scx200_acb.o 141obj-$(CONFIG_SCx200_ACB) += scx200_acb.o
142obj-$(CONFIG_I2C_FSI) += i2c-fsi.o
140 143
141ccflags-$(CONFIG_I2C_DEBUG_BUS) := -DDEBUG 144ccflags-$(CONFIG_I2C_DEBUG_BUS) := -DDEBUG
diff --git a/drivers/i2c/busses/i2c-amd8111.c b/drivers/i2c/busses/i2c-amd8111.c
index 95a80a8f81b5..134567f3019f 100644
--- a/drivers/i2c/busses/i2c-amd8111.c
+++ b/drivers/i2c/busses/i2c-amd8111.c
@@ -384,6 +384,7 @@ static s32 amd8111_access(struct i2c_adapter * adap, u16 addr,
384 if (status) 384 if (status)
385 return status; 385 return status;
386 len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX); 386 len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
387 /* fall through */
387 case I2C_SMBUS_I2C_BLOCK_DATA: 388 case I2C_SMBUS_I2C_BLOCK_DATA:
388 for (i = 0; i < len; i++) { 389 for (i = 0; i < len; i++) {
389 status = amd_ec_read(smbus, AMD_SMB_DATA + i, 390 status = amd_ec_read(smbus, AMD_SMB_DATA + i,
diff --git a/drivers/i2c/busses/i2c-aspeed.c b/drivers/i2c/busses/i2c-aspeed.c
index 60e4d0e939a3..a4f956c6d567 100644
--- a/drivers/i2c/busses/i2c-aspeed.c
+++ b/drivers/i2c/busses/i2c-aspeed.c
@@ -111,22 +111,22 @@
111#define ASPEED_I2CD_DEV_ADDR_MASK GENMASK(6, 0) 111#define ASPEED_I2CD_DEV_ADDR_MASK GENMASK(6, 0)
112 112
113enum aspeed_i2c_master_state { 113enum aspeed_i2c_master_state {
114 ASPEED_I2C_MASTER_INACTIVE,
114 ASPEED_I2C_MASTER_START, 115 ASPEED_I2C_MASTER_START,
115 ASPEED_I2C_MASTER_TX_FIRST, 116 ASPEED_I2C_MASTER_TX_FIRST,
116 ASPEED_I2C_MASTER_TX, 117 ASPEED_I2C_MASTER_TX,
117 ASPEED_I2C_MASTER_RX_FIRST, 118 ASPEED_I2C_MASTER_RX_FIRST,
118 ASPEED_I2C_MASTER_RX, 119 ASPEED_I2C_MASTER_RX,
119 ASPEED_I2C_MASTER_STOP, 120 ASPEED_I2C_MASTER_STOP,
120 ASPEED_I2C_MASTER_INACTIVE,
121}; 121};
122 122
123enum aspeed_i2c_slave_state { 123enum aspeed_i2c_slave_state {
124 ASPEED_I2C_SLAVE_STOP,
124 ASPEED_I2C_SLAVE_START, 125 ASPEED_I2C_SLAVE_START,
125 ASPEED_I2C_SLAVE_READ_REQUESTED, 126 ASPEED_I2C_SLAVE_READ_REQUESTED,
126 ASPEED_I2C_SLAVE_READ_PROCESSED, 127 ASPEED_I2C_SLAVE_READ_PROCESSED,
127 ASPEED_I2C_SLAVE_WRITE_REQUESTED, 128 ASPEED_I2C_SLAVE_WRITE_REQUESTED,
128 ASPEED_I2C_SLAVE_WRITE_RECEIVED, 129 ASPEED_I2C_SLAVE_WRITE_RECEIVED,
129 ASPEED_I2C_SLAVE_STOP,
130}; 130};
131 131
132struct aspeed_i2c_bus { 132struct aspeed_i2c_bus {
@@ -234,7 +234,6 @@ static bool aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus)
234 bool irq_handled = true; 234 bool irq_handled = true;
235 u8 value; 235 u8 value;
236 236
237 spin_lock(&bus->lock);
238 if (!slave) { 237 if (!slave) {
239 irq_handled = false; 238 irq_handled = false;
240 goto out; 239 goto out;
@@ -325,7 +324,6 @@ static bool aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus)
325 writel(status_ack, bus->base + ASPEED_I2C_INTR_STS_REG); 324 writel(status_ack, bus->base + ASPEED_I2C_INTR_STS_REG);
326 325
327out: 326out:
328 spin_unlock(&bus->lock);
329 return irq_handled; 327 return irq_handled;
330} 328}
331#endif /* CONFIG_I2C_SLAVE */ 329#endif /* CONFIG_I2C_SLAVE */
@@ -389,7 +387,6 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)
389 u8 recv_byte; 387 u8 recv_byte;
390 int ret; 388 int ret;
391 389
392 spin_lock(&bus->lock);
393 irq_status = readl(bus->base + ASPEED_I2C_INTR_STS_REG); 390 irq_status = readl(bus->base + ASPEED_I2C_INTR_STS_REG);
394 /* Ack all interrupt bits. */ 391 /* Ack all interrupt bits. */
395 writel(irq_status, bus->base + ASPEED_I2C_INTR_STS_REG); 392 writel(irq_status, bus->base + ASPEED_I2C_INTR_STS_REG);
@@ -407,7 +404,7 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)
407 */ 404 */
408 ret = aspeed_i2c_is_irq_error(irq_status); 405 ret = aspeed_i2c_is_irq_error(irq_status);
409 if (ret < 0) { 406 if (ret < 0) {
410 dev_dbg(bus->dev, "received error interrupt: 0x%08x", 407 dev_dbg(bus->dev, "received error interrupt: 0x%08x\n",
411 irq_status); 408 irq_status);
412 bus->cmd_err = ret; 409 bus->cmd_err = ret;
413 bus->master_state = ASPEED_I2C_MASTER_INACTIVE; 410 bus->master_state = ASPEED_I2C_MASTER_INACTIVE;
@@ -416,7 +413,7 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)
416 413
417 /* We are in an invalid state; reset bus to a known state. */ 414 /* We are in an invalid state; reset bus to a known state. */
418 if (!bus->msgs) { 415 if (!bus->msgs) {
419 dev_err(bus->dev, "bus in unknown state"); 416 dev_err(bus->dev, "bus in unknown state\n");
420 bus->cmd_err = -EIO; 417 bus->cmd_err = -EIO;
421 if (bus->master_state != ASPEED_I2C_MASTER_STOP) 418 if (bus->master_state != ASPEED_I2C_MASTER_STOP)
422 aspeed_i2c_do_stop(bus); 419 aspeed_i2c_do_stop(bus);
@@ -431,7 +428,7 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)
431 */ 428 */
432 if (bus->master_state == ASPEED_I2C_MASTER_START) { 429 if (bus->master_state == ASPEED_I2C_MASTER_START) {
433 if (unlikely(!(irq_status & ASPEED_I2CD_INTR_TX_ACK))) { 430 if (unlikely(!(irq_status & ASPEED_I2CD_INTR_TX_ACK))) {
434 pr_devel("no slave present at %02x", msg->addr); 431 pr_devel("no slave present at %02x\n", msg->addr);
435 status_ack |= ASPEED_I2CD_INTR_TX_NAK; 432 status_ack |= ASPEED_I2CD_INTR_TX_NAK;
436 bus->cmd_err = -ENXIO; 433 bus->cmd_err = -ENXIO;
437 aspeed_i2c_do_stop(bus); 434 aspeed_i2c_do_stop(bus);
@@ -451,11 +448,11 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)
451 switch (bus->master_state) { 448 switch (bus->master_state) {
452 case ASPEED_I2C_MASTER_TX: 449 case ASPEED_I2C_MASTER_TX:
453 if (unlikely(irq_status & ASPEED_I2CD_INTR_TX_NAK)) { 450 if (unlikely(irq_status & ASPEED_I2CD_INTR_TX_NAK)) {
454 dev_dbg(bus->dev, "slave NACKed TX"); 451 dev_dbg(bus->dev, "slave NACKed TX\n");
455 status_ack |= ASPEED_I2CD_INTR_TX_NAK; 452 status_ack |= ASPEED_I2CD_INTR_TX_NAK;
456 goto error_and_stop; 453 goto error_and_stop;
457 } else if (unlikely(!(irq_status & ASPEED_I2CD_INTR_TX_ACK))) { 454 } else if (unlikely(!(irq_status & ASPEED_I2CD_INTR_TX_ACK))) {
458 dev_err(bus->dev, "slave failed to ACK TX"); 455 dev_err(bus->dev, "slave failed to ACK TX\n");
459 goto error_and_stop; 456 goto error_and_stop;
460 } 457 }
461 status_ack |= ASPEED_I2CD_INTR_TX_ACK; 458 status_ack |= ASPEED_I2CD_INTR_TX_ACK;
@@ -478,7 +475,7 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)
478 /* fallthrough intended */ 475 /* fallthrough intended */
479 case ASPEED_I2C_MASTER_RX: 476 case ASPEED_I2C_MASTER_RX:
480 if (unlikely(!(irq_status & ASPEED_I2CD_INTR_RX_DONE))) { 477 if (unlikely(!(irq_status & ASPEED_I2CD_INTR_RX_DONE))) {
481 dev_err(bus->dev, "master failed to RX"); 478 dev_err(bus->dev, "master failed to RX\n");
482 goto error_and_stop; 479 goto error_and_stop;
483 } 480 }
484 status_ack |= ASPEED_I2CD_INTR_RX_DONE; 481 status_ack |= ASPEED_I2CD_INTR_RX_DONE;
@@ -509,7 +506,7 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)
509 goto out_no_complete; 506 goto out_no_complete;
510 case ASPEED_I2C_MASTER_STOP: 507 case ASPEED_I2C_MASTER_STOP:
511 if (unlikely(!(irq_status & ASPEED_I2CD_INTR_NORMAL_STOP))) { 508 if (unlikely(!(irq_status & ASPEED_I2CD_INTR_NORMAL_STOP))) {
512 dev_err(bus->dev, "master failed to STOP"); 509 dev_err(bus->dev, "master failed to STOP\n");
513 bus->cmd_err = -EIO; 510 bus->cmd_err = -EIO;
514 /* Do not STOP as we have already tried. */ 511 /* Do not STOP as we have already tried. */
515 } else { 512 } else {
@@ -520,7 +517,7 @@ static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus)
520 goto out_complete; 517 goto out_complete;
521 case ASPEED_I2C_MASTER_INACTIVE: 518 case ASPEED_I2C_MASTER_INACTIVE:
522 dev_err(bus->dev, 519 dev_err(bus->dev,
523 "master received interrupt 0x%08x, but is inactive", 520 "master received interrupt 0x%08x, but is inactive\n",
524 irq_status); 521 irq_status);
525 bus->cmd_err = -EIO; 522 bus->cmd_err = -EIO;
526 /* Do not STOP as we should be inactive. */ 523 /* Do not STOP as we should be inactive. */
@@ -547,22 +544,29 @@ out_no_complete:
547 dev_err(bus->dev, 544 dev_err(bus->dev,
548 "irq handled != irq. expected 0x%08x, but was 0x%08x\n", 545 "irq handled != irq. expected 0x%08x, but was 0x%08x\n",
549 irq_status, status_ack); 546 irq_status, status_ack);
550 spin_unlock(&bus->lock);
551 return !!irq_status; 547 return !!irq_status;
552} 548}
553 549
554static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) 550static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id)
555{ 551{
556 struct aspeed_i2c_bus *bus = dev_id; 552 struct aspeed_i2c_bus *bus = dev_id;
553 bool ret;
554
555 spin_lock(&bus->lock);
557 556
558#if IS_ENABLED(CONFIG_I2C_SLAVE) 557#if IS_ENABLED(CONFIG_I2C_SLAVE)
559 if (aspeed_i2c_slave_irq(bus)) { 558 if (aspeed_i2c_slave_irq(bus)) {
560 dev_dbg(bus->dev, "irq handled by slave.\n"); 559 dev_dbg(bus->dev, "irq handled by slave.\n");
561 return IRQ_HANDLED; 560 ret = true;
561 goto out;
562 } 562 }
563#endif /* CONFIG_I2C_SLAVE */ 563#endif /* CONFIG_I2C_SLAVE */
564 564
565 return aspeed_i2c_master_irq(bus) ? IRQ_HANDLED : IRQ_NONE; 565 ret = aspeed_i2c_master_irq(bus);
566
567out:
568 spin_unlock(&bus->lock);
569 return ret ? IRQ_HANDLED : IRQ_NONE;
566} 570}
567 571
568static int aspeed_i2c_master_xfer(struct i2c_adapter *adap, 572static int aspeed_i2c_master_xfer(struct i2c_adapter *adap,
@@ -851,7 +855,7 @@ static int aspeed_i2c_probe_bus(struct platform_device *pdev)
851 bus->rst = devm_reset_control_get_shared(&pdev->dev, NULL); 855 bus->rst = devm_reset_control_get_shared(&pdev->dev, NULL);
852 if (IS_ERR(bus->rst)) { 856 if (IS_ERR(bus->rst)) {
853 dev_err(&pdev->dev, 857 dev_err(&pdev->dev,
854 "missing or invalid reset controller device tree entry"); 858 "missing or invalid reset controller device tree entry\n");
855 return PTR_ERR(bus->rst); 859 return PTR_ERR(bus->rst);
856 } 860 }
857 reset_control_deassert(bus->rst); 861 reset_control_deassert(bus->rst);
@@ -868,7 +872,7 @@ static int aspeed_i2c_probe_bus(struct platform_device *pdev)
868 if (!match) 872 if (!match)
869 bus->get_clk_reg_val = aspeed_i2c_24xx_get_clk_reg_val; 873 bus->get_clk_reg_val = aspeed_i2c_24xx_get_clk_reg_val;
870 else 874 else
871 bus->get_clk_reg_val = match->data; 875 bus->get_clk_reg_val = (u32 (*)(u32))match->data;
872 876
873 /* Initialize the I2C adapter */ 877 /* Initialize the I2C adapter */
874 spin_lock_init(&bus->lock); 878 spin_lock_init(&bus->lock);
diff --git a/drivers/i2c/busses/i2c-brcmstb.c b/drivers/i2c/busses/i2c-brcmstb.c
index 78792b4d6437..826d32049996 100644
--- a/drivers/i2c/busses/i2c-brcmstb.c
+++ b/drivers/i2c/busses/i2c-brcmstb.c
@@ -689,9 +689,9 @@ static int brcmstb_i2c_suspend(struct device *dev)
689{ 689{
690 struct brcmstb_i2c_dev *i2c_dev = dev_get_drvdata(dev); 690 struct brcmstb_i2c_dev *i2c_dev = dev_get_drvdata(dev);
691 691
692 i2c_lock_adapter(&i2c_dev->adapter); 692 i2c_lock_bus(&i2c_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
693 i2c_dev->is_suspended = true; 693 i2c_dev->is_suspended = true;
694 i2c_unlock_adapter(&i2c_dev->adapter); 694 i2c_unlock_bus(&i2c_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
695 695
696 return 0; 696 return 0;
697} 697}
@@ -700,10 +700,10 @@ static int brcmstb_i2c_resume(struct device *dev)
700{ 700{
701 struct brcmstb_i2c_dev *i2c_dev = dev_get_drvdata(dev); 701 struct brcmstb_i2c_dev *i2c_dev = dev_get_drvdata(dev);
702 702
703 i2c_lock_adapter(&i2c_dev->adapter); 703 i2c_lock_bus(&i2c_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
704 brcmstb_i2c_set_bsc_reg_defaults(i2c_dev); 704 brcmstb_i2c_set_bsc_reg_defaults(i2c_dev);
705 i2c_dev->is_suspended = false; 705 i2c_dev->is_suspended = false;
706 i2c_unlock_adapter(&i2c_dev->adapter); 706 i2c_unlock_bus(&i2c_dev->adapter, I2C_LOCK_ROOT_ADAPTER);
707 707
708 return 0; 708 return 0;
709} 709}
diff --git a/drivers/i2c/busses/i2c-davinci.c b/drivers/i2c/busses/i2c-davinci.c
index 7379043711df..11caafa0e050 100644
--- a/drivers/i2c/busses/i2c-davinci.c
+++ b/drivers/i2c/busses/i2c-davinci.c
@@ -718,14 +718,14 @@ static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
718 718
719 dev = container_of(nb, struct davinci_i2c_dev, freq_transition); 719 dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
720 720
721 i2c_lock_adapter(&dev->adapter); 721 i2c_lock_bus(&dev->adapter, I2C_LOCK_ROOT_ADAPTER);
722 if (val == CPUFREQ_PRECHANGE) { 722 if (val == CPUFREQ_PRECHANGE) {
723 davinci_i2c_reset_ctrl(dev, 0); 723 davinci_i2c_reset_ctrl(dev, 0);
724 } else if (val == CPUFREQ_POSTCHANGE) { 724 } else if (val == CPUFREQ_POSTCHANGE) {
725 i2c_davinci_calc_clk_dividers(dev); 725 i2c_davinci_calc_clk_dividers(dev);
726 davinci_i2c_reset_ctrl(dev, 1); 726 davinci_i2c_reset_ctrl(dev, 1);
727 } 727 }
728 i2c_unlock_adapter(&dev->adapter); 728 i2c_unlock_bus(&dev->adapter, I2C_LOCK_ROOT_ADAPTER);
729 729
730 return 0; 730 return 0;
731} 731}
diff --git a/drivers/i2c/busses/i2c-designware-baytrail.c b/drivers/i2c/busses/i2c-designware-baytrail.c
index dbda8c9c8a1c..a2a275cfc1f6 100644
--- a/drivers/i2c/busses/i2c-designware-baytrail.c
+++ b/drivers/i2c/busses/i2c-designware-baytrail.c
@@ -1,15 +1,7 @@
1// SPDX-License-Identifier: GPL-2.0
1/* 2/*
2 * Intel BayTrail PMIC I2C bus semaphore implementaion 3 * Intel BayTrail PMIC I2C bus semaphore implementaion
3 * Copyright (c) 2014, Intel Corporation. 4 * Copyright (c) 2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */ 5 */
14#include <linux/delay.h> 6#include <linux/delay.h>
15#include <linux/device.h> 7#include <linux/device.h>
diff --git a/drivers/i2c/busses/i2c-designware-common.c b/drivers/i2c/busses/i2c-designware-common.c
index 48914dfc8ce8..69ec4a791f23 100644
--- a/drivers/i2c/busses/i2c-designware-common.c
+++ b/drivers/i2c/busses/i2c-designware-common.c
@@ -1,3 +1,4 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
1/* 2/*
2 * Synopsys DesignWare I2C adapter driver. 3 * Synopsys DesignWare I2C adapter driver.
3 * 4 *
@@ -6,20 +7,6 @@
6 * Copyright (C) 2006 Texas Instruments. 7 * Copyright (C) 2006 Texas Instruments.
7 * Copyright (C) 2007 MontaVista Software Inc. 8 * Copyright (C) 2007 MontaVista Software Inc.
8 * Copyright (C) 2009 Provigent Ltd. 9 * Copyright (C) 2009 Provigent Ltd.
9 *
10 * ----------------------------------------------------------------------------
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 * ----------------------------------------------------------------------------
22 *
23 */ 10 */
24#include <linux/clk.h> 11#include <linux/clk.h>
25#include <linux/delay.h> 12#include <linux/delay.h>
@@ -31,6 +18,7 @@
31#include <linux/io.h> 18#include <linux/io.h>
32#include <linux/module.h> 19#include <linux/module.h>
33#include <linux/pm_runtime.h> 20#include <linux/pm_runtime.h>
21#include <linux/swab.h>
34 22
35#include "i2c-designware-core.h" 23#include "i2c-designware-core.h"
36 24
@@ -94,6 +82,40 @@ void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
94 } 82 }
95} 83}
96 84
85/**
86 * i2c_dw_set_reg_access() - Set register access flags
87 * @dev: device private data
88 *
89 * Autodetects needed register access mode and sets access flags accordingly.
90 * This must be called before doing any other register access.
91 */
92int i2c_dw_set_reg_access(struct dw_i2c_dev *dev)
93{
94 u32 reg;
95 int ret;
96
97 ret = i2c_dw_acquire_lock(dev);
98 if (ret)
99 return ret;
100
101 reg = dw_readl(dev, DW_IC_COMP_TYPE);
102 i2c_dw_release_lock(dev);
103
104 if (reg == swab32(DW_IC_COMP_TYPE_VALUE)) {
105 /* Configure register endianess access */
106 dev->flags |= ACCESS_SWAP;
107 } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
108 /* Configure register access mode 16bit */
109 dev->flags |= ACCESS_16BIT;
110 } else if (reg != DW_IC_COMP_TYPE_VALUE) {
111 dev_err(dev->dev,
112 "Unknown Synopsys component type: 0x%08x\n", reg);
113 return -ENODEV;
114 }
115
116 return 0;
117}
118
97u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset) 119u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
98{ 120{
99 /* 121 /*
@@ -149,6 +171,47 @@ u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
149 return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset; 171 return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset;
150} 172}
151 173
174int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev)
175{
176 u32 reg;
177 int ret;
178
179 ret = i2c_dw_acquire_lock(dev);
180 if (ret)
181 return ret;
182
183 /* Configure SDA Hold Time if required */
184 reg = dw_readl(dev, DW_IC_COMP_VERSION);
185 if (reg >= DW_IC_SDA_HOLD_MIN_VERS) {
186 if (!dev->sda_hold_time) {
187 /* Keep previous hold time setting if no one set it */
188 dev->sda_hold_time = dw_readl(dev, DW_IC_SDA_HOLD);
189 }
190
191 /*
192 * Workaround for avoiding TX arbitration lost in case I2C
193 * slave pulls SDA down "too quickly" after falling egde of
194 * SCL by enabling non-zero SDA RX hold. Specification says it
195 * extends incoming SDA low to high transition while SCL is
196 * high but it apprears to help also above issue.
197 */
198 if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK))
199 dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT;
200
201 dev_dbg(dev->dev, "SDA Hold Time TX:RX = %d:%d\n",
202 dev->sda_hold_time & ~(u32)DW_IC_SDA_HOLD_RX_MASK,
203 dev->sda_hold_time >> DW_IC_SDA_HOLD_RX_SHIFT);
204 } else if (dev->sda_hold_time) {
205 dev_warn(dev->dev,
206 "Hardware too old to adjust SDA hold time.\n");
207 dev->sda_hold_time = 0;
208 }
209
210 i2c_dw_release_lock(dev);
211
212 return 0;
213}
214
152void __i2c_dw_disable(struct dw_i2c_dev *dev) 215void __i2c_dw_disable(struct dw_i2c_dev *dev)
153{ 216{
154 int timeout = 100; 217 int timeout = 100;
diff --git a/drivers/i2c/busses/i2c-designware-core.h b/drivers/i2c/busses/i2c-designware-core.h
index d690e648bc01..e367b1af4ab2 100644
--- a/drivers/i2c/busses/i2c-designware-core.h
+++ b/drivers/i2c/busses/i2c-designware-core.h
@@ -1,3 +1,4 @@
1/* SPDX-License-Identifier: GPL-2.0-or-later */
1/* 2/*
2 * Synopsys DesignWare I2C adapter driver. 3 * Synopsys DesignWare I2C adapter driver.
3 * 4 *
@@ -6,20 +7,6 @@
6 * Copyright (C) 2006 Texas Instruments. 7 * Copyright (C) 2006 Texas Instruments.
7 * Copyright (C) 2007 MontaVista Software Inc. 8 * Copyright (C) 2007 MontaVista Software Inc.
8 * Copyright (C) 2009 Provigent Ltd. 9 * Copyright (C) 2009 Provigent Ltd.
9 *
10 * ----------------------------------------------------------------------------
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 * ----------------------------------------------------------------------------
22 *
23 */ 10 */
24 11
25#include <linux/i2c.h> 12#include <linux/i2c.h>
@@ -212,7 +199,8 @@
212 * @tx_fifo_depth: depth of the hardware tx fifo 199 * @tx_fifo_depth: depth of the hardware tx fifo
213 * @rx_fifo_depth: depth of the hardware rx fifo 200 * @rx_fifo_depth: depth of the hardware rx fifo
214 * @rx_outstanding: current master-rx elements in tx fifo 201 * @rx_outstanding: current master-rx elements in tx fifo
215 * @clk_freq: bus clock frequency 202 * @timings: bus clock frequency, SDA hold and other timings
203 * @sda_hold_time: SDA hold value
216 * @ss_hcnt: standard speed HCNT value 204 * @ss_hcnt: standard speed HCNT value
217 * @ss_lcnt: standard speed LCNT value 205 * @ss_lcnt: standard speed LCNT value
218 * @fs_hcnt: fast speed HCNT value 206 * @fs_hcnt: fast speed HCNT value
@@ -264,10 +252,8 @@ struct dw_i2c_dev {
264 unsigned int tx_fifo_depth; 252 unsigned int tx_fifo_depth;
265 unsigned int rx_fifo_depth; 253 unsigned int rx_fifo_depth;
266 int rx_outstanding; 254 int rx_outstanding;
267 u32 clk_freq; 255 struct i2c_timings timings;
268 u32 sda_hold_time; 256 u32 sda_hold_time;
269 u32 sda_falling_time;
270 u32 scl_falling_time;
271 u16 ss_hcnt; 257 u16 ss_hcnt;
272 u16 ss_lcnt; 258 u16 ss_lcnt;
273 u16 fs_hcnt; 259 u16 fs_hcnt;
@@ -295,8 +281,10 @@ struct dw_i2c_dev {
295 281
296u32 dw_readl(struct dw_i2c_dev *dev, int offset); 282u32 dw_readl(struct dw_i2c_dev *dev, int offset);
297void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset); 283void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset);
284int i2c_dw_set_reg_access(struct dw_i2c_dev *dev);
298u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset); 285u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset);
299u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset); 286u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset);
287int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev);
300unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev); 288unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev);
301int i2c_dw_prepare_clk(struct dw_i2c_dev *dev, bool prepare); 289int i2c_dw_prepare_clk(struct dw_i2c_dev *dev, bool prepare);
302int i2c_dw_acquire_lock(struct dw_i2c_dev *dev); 290int i2c_dw_acquire_lock(struct dw_i2c_dev *dev);
diff --git a/drivers/i2c/busses/i2c-designware-master.c b/drivers/i2c/busses/i2c-designware-master.c
index 27436a937492..e18442b9973a 100644
--- a/drivers/i2c/busses/i2c-designware-master.c
+++ b/drivers/i2c/busses/i2c-designware-master.c
@@ -1,3 +1,4 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
1/* 2/*
2 * Synopsys DesignWare I2C adapter driver (master only). 3 * Synopsys DesignWare I2C adapter driver (master only).
3 * 4 *
@@ -6,20 +7,6 @@
6 * Copyright (C) 2006 Texas Instruments. 7 * Copyright (C) 2006 Texas Instruments.
7 * Copyright (C) 2007 MontaVista Software Inc. 8 * Copyright (C) 2007 MontaVista Software Inc.
8 * Copyright (C) 2009 Provigent Ltd. 9 * Copyright (C) 2009 Provigent Ltd.
9 *
10 * ----------------------------------------------------------------------------
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 * ----------------------------------------------------------------------------
22 *
23 */ 10 */
24#include <linux/delay.h> 11#include <linux/delay.h>
25#include <linux/err.h> 12#include <linux/err.h>
@@ -45,90 +32,79 @@ static void i2c_dw_configure_fifo_master(struct dw_i2c_dev *dev)
45 dw_writel(dev, dev->master_cfg, DW_IC_CON); 32 dw_writel(dev, dev->master_cfg, DW_IC_CON);
46} 33}
47 34
48/** 35static int i2c_dw_set_timings_master(struct dw_i2c_dev *dev)
49 * i2c_dw_init() - Initialize the designware I2C master hardware
50 * @dev: device private data
51 *
52 * This functions configures and enables the I2C master.
53 * This function is called during I2C init function, and in case of timeout at
54 * run time.
55 */
56static int i2c_dw_init_master(struct dw_i2c_dev *dev)
57{ 36{
58 u32 hcnt, lcnt; 37 u32 ic_clk = i2c_dw_clk_rate(dev);
59 u32 reg, comp_param1; 38 const char *mode_str, *fp_str = "";
39 u32 comp_param1;
60 u32 sda_falling_time, scl_falling_time; 40 u32 sda_falling_time, scl_falling_time;
41 struct i2c_timings *t = &dev->timings;
61 int ret; 42 int ret;
62 43
63 ret = i2c_dw_acquire_lock(dev); 44 ret = i2c_dw_acquire_lock(dev);
64 if (ret) 45 if (ret)
65 return ret; 46 return ret;
66
67 reg = dw_readl(dev, DW_IC_COMP_TYPE);
68 if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
69 /* Configure register endianess access */
70 dev->flags |= ACCESS_SWAP;
71 } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
72 /* Configure register access mode 16bit */
73 dev->flags |= ACCESS_16BIT;
74 } else if (reg != DW_IC_COMP_TYPE_VALUE) {
75 dev_err(dev->dev,
76 "Unknown Synopsys component type: 0x%08x\n", reg);
77 i2c_dw_release_lock(dev);
78 return -ENODEV;
79 }
80
81 comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1); 47 comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1);
48 i2c_dw_release_lock(dev);
82 49
83 /* Disable the adapter */ 50 /* Set standard and fast speed dividers for high/low periods */
84 __i2c_dw_disable(dev); 51 sda_falling_time = t->sda_fall_ns ?: 300; /* ns */
85 52 scl_falling_time = t->scl_fall_ns ?: 300; /* ns */
86 /* Set standard and fast speed deviders for high/low periods */
87
88 sda_falling_time = dev->sda_falling_time ?: 300; /* ns */
89 scl_falling_time = dev->scl_falling_time ?: 300; /* ns */
90 53
91 /* Set SCL timing parameters for standard-mode */ 54 /* Calculate SCL timing parameters for standard mode if not set */
92 if (dev->ss_hcnt && dev->ss_lcnt) { 55 if (!dev->ss_hcnt || !dev->ss_lcnt) {
93 hcnt = dev->ss_hcnt; 56 dev->ss_hcnt =
94 lcnt = dev->ss_lcnt; 57 i2c_dw_scl_hcnt(ic_clk,
95 } else {
96 hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev),
97 4000, /* tHD;STA = tHIGH = 4.0 us */ 58 4000, /* tHD;STA = tHIGH = 4.0 us */
98 sda_falling_time, 59 sda_falling_time,
99 0, /* 0: DW default, 1: Ideal */ 60 0, /* 0: DW default, 1: Ideal */
100 0); /* No offset */ 61 0); /* No offset */
101 lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev), 62 dev->ss_lcnt =
63 i2c_dw_scl_lcnt(ic_clk,
102 4700, /* tLOW = 4.7 us */ 64 4700, /* tLOW = 4.7 us */
103 scl_falling_time, 65 scl_falling_time,
104 0); /* No offset */ 66 0); /* No offset */
105 } 67 }
106 dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT); 68 dev_dbg(dev->dev, "Standard Mode HCNT:LCNT = %d:%d\n",
107 dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT); 69 dev->ss_hcnt, dev->ss_lcnt);
108 dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt); 70
109 71 /*
110 /* Set SCL timing parameters for fast-mode or fast-mode plus */ 72 * Set SCL timing parameters for fast mode or fast mode plus. Only
111 if ((dev->clk_freq == 1000000) && dev->fp_hcnt && dev->fp_lcnt) { 73 * difference is the timing parameter values since the registers are
112 hcnt = dev->fp_hcnt; 74 * the same.
113 lcnt = dev->fp_lcnt; 75 */
114 } else if (dev->fs_hcnt && dev->fs_lcnt) { 76 if (t->bus_freq_hz == 1000000) {
115 hcnt = dev->fs_hcnt; 77 /*
116 lcnt = dev->fs_lcnt; 78 * Check are fast mode plus parameters available and use
117 } else { 79 * fast mode if not.
118 hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev), 80 */
81 if (dev->fp_hcnt && dev->fp_lcnt) {
82 dev->fs_hcnt = dev->fp_hcnt;
83 dev->fs_lcnt = dev->fp_lcnt;
84 fp_str = " Plus";
85 }
86 }
87 /*
88 * Calculate SCL timing parameters for fast mode if not set. They are
89 * needed also in high speed mode.
90 */
91 if (!dev->fs_hcnt || !dev->fs_lcnt) {
92 dev->fs_hcnt =
93 i2c_dw_scl_hcnt(ic_clk,
119 600, /* tHD;STA = tHIGH = 0.6 us */ 94 600, /* tHD;STA = tHIGH = 0.6 us */
120 sda_falling_time, 95 sda_falling_time,
121 0, /* 0: DW default, 1: Ideal */ 96 0, /* 0: DW default, 1: Ideal */
122 0); /* No offset */ 97 0); /* No offset */
123 lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev), 98 dev->fs_lcnt =
99 i2c_dw_scl_lcnt(ic_clk,
124 1300, /* tLOW = 1.3 us */ 100 1300, /* tLOW = 1.3 us */
125 scl_falling_time, 101 scl_falling_time,
126 0); /* No offset */ 102 0); /* No offset */
127 } 103 }
128 dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT); 104 dev_dbg(dev->dev, "Fast Mode%s HCNT:LCNT = %d:%d\n",
129 dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT); 105 fp_str, dev->fs_hcnt, dev->fs_lcnt);
130 dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
131 106
107 /* Check is high speed possible and fall back to fast mode if not */
132 if ((dev->master_cfg & DW_IC_CON_SPEED_MASK) == 108 if ((dev->master_cfg & DW_IC_CON_SPEED_MASK) ==
133 DW_IC_CON_SPEED_HIGH) { 109 DW_IC_CON_SPEED_HIGH) {
134 if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK) 110 if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK)
@@ -136,37 +112,70 @@ static int i2c_dw_init_master(struct dw_i2c_dev *dev)
136 dev_err(dev->dev, "High Speed not supported!\n"); 112 dev_err(dev->dev, "High Speed not supported!\n");
137 dev->master_cfg &= ~DW_IC_CON_SPEED_MASK; 113 dev->master_cfg &= ~DW_IC_CON_SPEED_MASK;
138 dev->master_cfg |= DW_IC_CON_SPEED_FAST; 114 dev->master_cfg |= DW_IC_CON_SPEED_FAST;
115 dev->hs_hcnt = 0;
116 dev->hs_lcnt = 0;
139 } else if (dev->hs_hcnt && dev->hs_lcnt) { 117 } else if (dev->hs_hcnt && dev->hs_lcnt) {
140 hcnt = dev->hs_hcnt; 118 dev_dbg(dev->dev, "High Speed Mode HCNT:LCNT = %d:%d\n",
141 lcnt = dev->hs_lcnt; 119 dev->hs_hcnt, dev->hs_lcnt);
142 dw_writel(dev, hcnt, DW_IC_HS_SCL_HCNT);
143 dw_writel(dev, lcnt, DW_IC_HS_SCL_LCNT);
144 dev_dbg(dev->dev, "HighSpeed-mode HCNT:LCNT = %d:%d\n",
145 hcnt, lcnt);
146 } 120 }
147 } 121 }
148 122
149 /* Configure SDA Hold Time if required */ 123 ret = i2c_dw_set_sda_hold(dev);
150 reg = dw_readl(dev, DW_IC_COMP_VERSION); 124 if (ret)
151 if (reg >= DW_IC_SDA_HOLD_MIN_VERS) { 125 goto out;
152 if (!dev->sda_hold_time) { 126
153 /* Keep previous hold time setting if no one set it */ 127 switch (dev->master_cfg & DW_IC_CON_SPEED_MASK) {
154 dev->sda_hold_time = dw_readl(dev, DW_IC_SDA_HOLD); 128 case DW_IC_CON_SPEED_STD:
155 } 129 mode_str = "Standard Mode";
156 /* 130 break;
157 * Workaround for avoiding TX arbitration lost in case I2C 131 case DW_IC_CON_SPEED_HIGH:
158 * slave pulls SDA down "too quickly" after falling egde of 132 mode_str = "High Speed Mode";
159 * SCL by enabling non-zero SDA RX hold. Specification says it 133 break;
160 * extends incoming SDA low to high transition while SCL is 134 default:
161 * high but it apprears to help also above issue. 135 mode_str = "Fast Mode";
162 */
163 if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK))
164 dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT;
165 dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD);
166 } else if (dev->sda_hold_time) {
167 dev_warn(dev->dev,
168 "Hardware too old to adjust SDA hold time.\n");
169 } 136 }
137 dev_dbg(dev->dev, "Bus speed: %s%s\n", mode_str, fp_str);
138
139out:
140 return ret;
141}
142
143/**
144 * i2c_dw_init() - Initialize the designware I2C master hardware
145 * @dev: device private data
146 *
147 * This functions configures and enables the I2C master.
148 * This function is called during I2C init function, and in case of timeout at
149 * run time.
150 */
151static int i2c_dw_init_master(struct dw_i2c_dev *dev)
152{
153 int ret;
154
155 ret = i2c_dw_acquire_lock(dev);
156 if (ret)
157 return ret;
158
159 /* Disable the adapter */
160 __i2c_dw_disable(dev);
161
162 /* Write standard speed timing parameters */
163 dw_writel(dev, dev->ss_hcnt, DW_IC_SS_SCL_HCNT);
164 dw_writel(dev, dev->ss_lcnt, DW_IC_SS_SCL_LCNT);
165
166 /* Write fast mode/fast mode plus timing parameters */
167 dw_writel(dev, dev->fs_hcnt, DW_IC_FS_SCL_HCNT);
168 dw_writel(dev, dev->fs_lcnt, DW_IC_FS_SCL_LCNT);
169
170 /* Write high speed timing parameters if supported */
171 if (dev->hs_hcnt && dev->hs_lcnt) {
172 dw_writel(dev, dev->hs_hcnt, DW_IC_HS_SCL_HCNT);
173 dw_writel(dev, dev->hs_lcnt, DW_IC_HS_SCL_LCNT);
174 }
175
176 /* Write SDA hold time if supported */
177 if (dev->sda_hold_time)
178 dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD);
170 179
171 i2c_dw_configure_fifo_master(dev); 180 i2c_dw_configure_fifo_master(dev);
172 i2c_dw_release_lock(dev); 181 i2c_dw_release_lock(dev);
@@ -253,13 +262,6 @@ i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
253 break; 262 break;
254 } 263 }
255 264
256 if (msgs[dev->msg_write_idx].len == 0) {
257 dev_err(dev->dev,
258 "%s: invalid message length\n", __func__);
259 dev->msg_err = -EINVAL;
260 break;
261 }
262
263 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) { 265 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
264 /* new i2c_msg */ 266 /* new i2c_msg */
265 buf = msgs[dev->msg_write_idx].buf; 267 buf = msgs[dev->msg_write_idx].buf;
@@ -502,6 +504,10 @@ static const struct i2c_algorithm i2c_dw_algo = {
502 .functionality = i2c_dw_func, 504 .functionality = i2c_dw_func,
503}; 505};
504 506
507static const struct i2c_adapter_quirks i2c_dw_quirks = {
508 .flags = I2C_AQ_NO_ZERO_LEN,
509};
510
505static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev) 511static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
506{ 512{
507 u32 stat; 513 u32 stat;
@@ -681,6 +687,14 @@ int i2c_dw_probe(struct dw_i2c_dev *dev)
681 dev->disable = i2c_dw_disable; 687 dev->disable = i2c_dw_disable;
682 dev->disable_int = i2c_dw_disable_int; 688 dev->disable_int = i2c_dw_disable_int;
683 689
690 ret = i2c_dw_set_reg_access(dev);
691 if (ret)
692 return ret;
693
694 ret = i2c_dw_set_timings_master(dev);
695 if (ret)
696 return ret;
697
684 ret = dev->init(dev); 698 ret = dev->init(dev);
685 if (ret) 699 if (ret)
686 return ret; 700 return ret;
@@ -689,6 +703,7 @@ int i2c_dw_probe(struct dw_i2c_dev *dev)
689 "Synopsys DesignWare I2C adapter"); 703 "Synopsys DesignWare I2C adapter");
690 adap->retries = 3; 704 adap->retries = 3;
691 adap->algo = &i2c_dw_algo; 705 adap->algo = &i2c_dw_algo;
706 adap->quirks = &i2c_dw_quirks;
692 adap->dev.parent = dev->dev; 707 adap->dev.parent = dev->dev;
693 i2c_set_adapdata(adap, dev); 708 i2c_set_adapdata(adap, dev);
694 709
diff --git a/drivers/i2c/busses/i2c-designware-pcidrv.c b/drivers/i2c/busses/i2c-designware-pcidrv.c
index 86e1bd0b82e9..d50f80487214 100644
--- a/drivers/i2c/busses/i2c-designware-pcidrv.c
+++ b/drivers/i2c/busses/i2c-designware-pcidrv.c
@@ -1,3 +1,4 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
1/* 2/*
2 * Synopsys DesignWare I2C adapter driver (master only). 3 * Synopsys DesignWare I2C adapter driver (master only).
3 * 4 *
@@ -7,22 +8,7 @@
7 * Copyright (C) 2007 MontaVista Software Inc. 8 * Copyright (C) 2007 MontaVista Software Inc.
8 * Copyright (C) 2009 Provigent Ltd. 9 * Copyright (C) 2009 Provigent Ltd.
9 * Copyright (C) 2011, 2015, 2016 Intel Corporation. 10 * Copyright (C) 2011, 2015, 2016 Intel Corporation.
10 *
11 * ----------------------------------------------------------------------------
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 * ----------------------------------------------------------------------------
23 *
24 */ 11 */
25
26#include <linux/acpi.h> 12#include <linux/acpi.h>
27#include <linux/delay.h> 13#include <linux/delay.h>
28#include <linux/err.h> 14#include <linux/err.h>
@@ -105,6 +91,7 @@ static int mfld_setup(struct pci_dev *pdev, struct dw_pci_controller *c)
105 case 0x0817: 91 case 0x0817:
106 c->bus_cfg &= ~DW_IC_CON_SPEED_MASK; 92 c->bus_cfg &= ~DW_IC_CON_SPEED_MASK;
107 c->bus_cfg |= DW_IC_CON_SPEED_STD; 93 c->bus_cfg |= DW_IC_CON_SPEED_STD;
94 /* fall through */
108 case 0x0818: 95 case 0x0818:
109 case 0x0819: 96 case 0x0819:
110 c->bus_num = pdev->device - 0x817 + 3; 97 c->bus_num = pdev->device - 0x817 + 3;
diff --git a/drivers/i2c/busses/i2c-designware-platdrv.c b/drivers/i2c/busses/i2c-designware-platdrv.c
index 5660daf6c92e..1a8d2da5b000 100644
--- a/drivers/i2c/busses/i2c-designware-platdrv.c
+++ b/drivers/i2c/busses/i2c-designware-platdrv.c
@@ -1,3 +1,4 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
1/* 2/*
2 * Synopsys DesignWare I2C adapter driver. 3 * Synopsys DesignWare I2C adapter driver.
3 * 4 *
@@ -6,20 +7,6 @@
6 * Copyright (C) 2006 Texas Instruments. 7 * Copyright (C) 2006 Texas Instruments.
7 * Copyright (C) 2007 MontaVista Software Inc. 8 * Copyright (C) 2007 MontaVista Software Inc.
8 * Copyright (C) 2009 Provigent Ltd. 9 * Copyright (C) 2009 Provigent Ltd.
9 *
10 * ----------------------------------------------------------------------------
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 * ----------------------------------------------------------------------------
22 *
23 */ 10 */
24#include <linux/acpi.h> 11#include <linux/acpi.h>
25#include <linux/clk-provider.h> 12#include <linux/clk-provider.h>
@@ -96,6 +83,7 @@ static void dw_i2c_acpi_params(struct platform_device *pdev, char method[],
96static int dw_i2c_acpi_configure(struct platform_device *pdev) 83static int dw_i2c_acpi_configure(struct platform_device *pdev)
97{ 84{
98 struct dw_i2c_dev *dev = platform_get_drvdata(pdev); 85 struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
86 struct i2c_timings *t = &dev->timings;
99 u32 ss_ht = 0, fp_ht = 0, hs_ht = 0, fs_ht = 0; 87 u32 ss_ht = 0, fp_ht = 0, hs_ht = 0, fs_ht = 0;
100 acpi_handle handle = ACPI_HANDLE(&pdev->dev); 88 acpi_handle handle = ACPI_HANDLE(&pdev->dev);
101 const struct acpi_device_id *id; 89 const struct acpi_device_id *id;
@@ -115,7 +103,7 @@ static int dw_i2c_acpi_configure(struct platform_device *pdev)
115 dw_i2c_acpi_params(pdev, "HSCN", &dev->hs_hcnt, &dev->hs_lcnt, &hs_ht); 103 dw_i2c_acpi_params(pdev, "HSCN", &dev->hs_hcnt, &dev->hs_lcnt, &hs_ht);
116 dw_i2c_acpi_params(pdev, "FMCN", &dev->fs_hcnt, &dev->fs_lcnt, &fs_ht); 104 dw_i2c_acpi_params(pdev, "FMCN", &dev->fs_hcnt, &dev->fs_lcnt, &fs_ht);
117 105
118 switch (dev->clk_freq) { 106 switch (t->bus_freq_hz) {
119 case 100000: 107 case 100000:
120 dev->sda_hold_time = ss_ht; 108 dev->sda_hold_time = ss_ht;
121 break; 109 break;
@@ -175,6 +163,8 @@ static inline int dw_i2c_acpi_configure(struct platform_device *pdev)
175 163
176static void i2c_dw_configure_master(struct dw_i2c_dev *dev) 164static void i2c_dw_configure_master(struct dw_i2c_dev *dev)
177{ 165{
166 struct i2c_timings *t = &dev->timings;
167
178 dev->functionality = I2C_FUNC_10BIT_ADDR | DW_IC_DEFAULT_FUNCTIONALITY; 168 dev->functionality = I2C_FUNC_10BIT_ADDR | DW_IC_DEFAULT_FUNCTIONALITY;
179 169
180 dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE | 170 dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
@@ -182,7 +172,7 @@ static void i2c_dw_configure_master(struct dw_i2c_dev *dev)
182 172
183 dev->mode = DW_IC_MASTER; 173 dev->mode = DW_IC_MASTER;
184 174
185 switch (dev->clk_freq) { 175 switch (t->bus_freq_hz) {
186 case 100000: 176 case 100000:
187 dev->master_cfg |= DW_IC_CON_SPEED_STD; 177 dev->master_cfg |= DW_IC_CON_SPEED_STD;
188 break; 178 break;
@@ -240,7 +230,8 @@ static int dw_i2c_plat_probe(struct platform_device *pdev)
240 struct dw_i2c_platform_data *pdata = dev_get_platdata(&pdev->dev); 230 struct dw_i2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
241 struct i2c_adapter *adap; 231 struct i2c_adapter *adap;
242 struct dw_i2c_dev *dev; 232 struct dw_i2c_dev *dev;
243 u32 acpi_speed, ht = 0; 233 struct i2c_timings *t;
234 u32 acpi_speed;
244 struct resource *mem; 235 struct resource *mem;
245 int i, irq, ret; 236 int i, irq, ret;
246 static const int supported_speeds[] = { 237 static const int supported_speeds[] = {
@@ -272,18 +263,11 @@ static int dw_i2c_plat_probe(struct platform_device *pdev)
272 reset_control_deassert(dev->rst); 263 reset_control_deassert(dev->rst);
273 } 264 }
274 265
275 if (pdata) { 266 t = &dev->timings;
276 dev->clk_freq = pdata->i2c_scl_freq; 267 if (pdata)
277 } else { 268 t->bus_freq_hz = pdata->i2c_scl_freq;
278 device_property_read_u32(&pdev->dev, "i2c-sda-hold-time-ns", 269 else
279 &ht); 270 i2c_parse_fw_timings(&pdev->dev, t, false);
280 device_property_read_u32(&pdev->dev, "i2c-sda-falling-time-ns",
281 &dev->sda_falling_time);
282 device_property_read_u32(&pdev->dev, "i2c-scl-falling-time-ns",
283 &dev->scl_falling_time);
284 device_property_read_u32(&pdev->dev, "clock-frequency",
285 &dev->clk_freq);
286 }
287 271
288 acpi_speed = i2c_acpi_find_bus_speed(&pdev->dev); 272 acpi_speed = i2c_acpi_find_bus_speed(&pdev->dev);
289 /* 273 /*
@@ -300,12 +284,12 @@ static int dw_i2c_plat_probe(struct platform_device *pdev)
300 * Find bus speed from the "clock-frequency" device property, ACPI 284 * Find bus speed from the "clock-frequency" device property, ACPI
301 * or by using fast mode if neither is set. 285 * or by using fast mode if neither is set.
302 */ 286 */
303 if (acpi_speed && dev->clk_freq) 287 if (acpi_speed && t->bus_freq_hz)
304 dev->clk_freq = min(dev->clk_freq, acpi_speed); 288 t->bus_freq_hz = min(t->bus_freq_hz, acpi_speed);
305 else if (acpi_speed || dev->clk_freq) 289 else if (acpi_speed || t->bus_freq_hz)
306 dev->clk_freq = max(dev->clk_freq, acpi_speed); 290 t->bus_freq_hz = max(t->bus_freq_hz, acpi_speed);
307 else 291 else
308 dev->clk_freq = 400000; 292 t->bus_freq_hz = 400000;
309 293
310 if (has_acpi_companion(&pdev->dev)) 294 if (has_acpi_companion(&pdev->dev))
311 dw_i2c_acpi_configure(pdev); 295 dw_i2c_acpi_configure(pdev);
@@ -314,11 +298,11 @@ static int dw_i2c_plat_probe(struct platform_device *pdev)
314 * Only standard mode at 100kHz, fast mode at 400kHz, 298 * Only standard mode at 100kHz, fast mode at 400kHz,
315 * fast mode plus at 1MHz and high speed mode at 3.4MHz are supported. 299 * fast mode plus at 1MHz and high speed mode at 3.4MHz are supported.
316 */ 300 */
317 if (dev->clk_freq != 100000 && dev->clk_freq != 400000 301 if (t->bus_freq_hz != 100000 && t->bus_freq_hz != 400000 &&
318 && dev->clk_freq != 1000000 && dev->clk_freq != 3400000) { 302 t->bus_freq_hz != 1000000 && t->bus_freq_hz != 3400000) {
319 dev_err(&pdev->dev, 303 dev_err(&pdev->dev,
320 "%d Hz is unsupported, only 100kHz, 400kHz, 1MHz and 3.4MHz are supported\n", 304 "%d Hz is unsupported, only 100kHz, 400kHz, 1MHz and 3.4MHz are supported\n",
321 dev->clk_freq); 305 t->bus_freq_hz);
322 ret = -EINVAL; 306 ret = -EINVAL;
323 goto exit_reset; 307 goto exit_reset;
324 } 308 }
@@ -334,12 +318,14 @@ static int dw_i2c_plat_probe(struct platform_device *pdev)
334 318
335 dev->clk = devm_clk_get(&pdev->dev, NULL); 319 dev->clk = devm_clk_get(&pdev->dev, NULL);
336 if (!i2c_dw_prepare_clk(dev, true)) { 320 if (!i2c_dw_prepare_clk(dev, true)) {
321 u64 clk_khz;
322
337 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz; 323 dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;
324 clk_khz = dev->get_clk_rate_khz(dev);
338 325
339 if (!dev->sda_hold_time && ht) 326 if (!dev->sda_hold_time && t->sda_hold_ns)
340 dev->sda_hold_time = div_u64( 327 dev->sda_hold_time =
341 (u64)dev->get_clk_rate_khz(dev) * ht + 500000, 328 div_u64(clk_khz * t->sda_hold_ns + 500000, 1000000);
342 1000000);
343 } 329 }
344 330
345 dw_i2c_set_fifo_size(dev, pdev->id); 331 dw_i2c_set_fifo_size(dev, pdev->id);
diff --git a/drivers/i2c/busses/i2c-designware-slave.c b/drivers/i2c/busses/i2c-designware-slave.c
index 8ce2cd368477..e7f9305b2dd9 100644
--- a/drivers/i2c/busses/i2c-designware-slave.c
+++ b/drivers/i2c/busses/i2c-designware-slave.c
@@ -1,23 +1,10 @@
1// SPDX-License-Identifier: GPL-2.0
1/* 2/*
2 * Synopsys DesignWare I2C adapter driver (slave only). 3 * Synopsys DesignWare I2C adapter driver (slave only).
3 * 4 *
4 * Based on the Synopsys DesignWare I2C adapter driver (master). 5 * Based on the Synopsys DesignWare I2C adapter driver (master).
5 * 6 *
6 * Copyright (C) 2016 Synopsys Inc. 7 * Copyright (C) 2016 Synopsys Inc.
7 *
8 * ----------------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * ----------------------------------------------------------------------------
20 *
21 */ 8 */
22#include <linux/delay.h> 9#include <linux/delay.h>
23#include <linux/err.h> 10#include <linux/err.h>
@@ -51,53 +38,18 @@ static void i2c_dw_configure_fifo_slave(struct dw_i2c_dev *dev)
51 */ 38 */
52static int i2c_dw_init_slave(struct dw_i2c_dev *dev) 39static int i2c_dw_init_slave(struct dw_i2c_dev *dev)
53{ 40{
54 u32 reg, comp_param1;
55 int ret; 41 int ret;
56 42
57 ret = i2c_dw_acquire_lock(dev); 43 ret = i2c_dw_acquire_lock(dev);
58 if (ret) 44 if (ret)
59 return ret; 45 return ret;
60 46
61 reg = dw_readl(dev, DW_IC_COMP_TYPE);
62 if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
63 /* Configure register endianness access. */
64 dev->flags |= ACCESS_SWAP;
65 } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
66 /* Configure register access mode 16bit. */
67 dev->flags |= ACCESS_16BIT;
68 } else if (reg != DW_IC_COMP_TYPE_VALUE) {
69 dev_err(dev->dev,
70 "Unknown Synopsys component type: 0x%08x\n", reg);
71 i2c_dw_release_lock(dev);
72 return -ENODEV;
73 }
74
75 comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1);
76
77 /* Disable the adapter. */ 47 /* Disable the adapter. */
78 __i2c_dw_disable(dev); 48 __i2c_dw_disable(dev);
79 49
80 /* Configure SDA Hold Time if required. */ 50 /* Write SDA hold time if supported */
81 reg = dw_readl(dev, DW_IC_COMP_VERSION); 51 if (dev->sda_hold_time)
82 if (reg >= DW_IC_SDA_HOLD_MIN_VERS) {
83 if (!dev->sda_hold_time) {
84 /* Keep previous hold time setting if no one set it. */
85 dev->sda_hold_time = dw_readl(dev, DW_IC_SDA_HOLD);
86 }
87 /*
88 * Workaround for avoiding TX arbitration lost in case I2C
89 * slave pulls SDA down "too quickly" after falling egde of
90 * SCL by enabling non-zero SDA RX hold. Specification says it
91 * extends incoming SDA low to high transition while SCL is
92 * high but it apprears to help also above issue.
93 */
94 if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK))
95 dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT;
96 dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD); 52 dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD);
97 } else {
98 dev_warn(dev->dev,
99 "Hardware too old to adjust SDA hold time.\n");
100 }
101 53
102 i2c_dw_configure_fifo_slave(dev); 54 i2c_dw_configure_fifo_slave(dev);
103 i2c_dw_release_lock(dev); 55 i2c_dw_release_lock(dev);
@@ -299,6 +251,14 @@ int i2c_dw_probe_slave(struct dw_i2c_dev *dev)
299 dev->disable = i2c_dw_disable; 251 dev->disable = i2c_dw_disable;
300 dev->disable_int = i2c_dw_disable_int; 252 dev->disable_int = i2c_dw_disable_int;
301 253
254 ret = i2c_dw_set_reg_access(dev);
255 if (ret)
256 return ret;
257
258 ret = i2c_dw_set_sda_hold(dev);
259 if (ret)
260 return ret;
261
302 ret = dev->init(dev); 262 ret = dev->init(dev);
303 if (ret) 263 if (ret)
304 return ret; 264 return ret;
diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
index de82ad8ff534..c1ce2299a76e 100644
--- a/drivers/i2c/busses/i2c-exynos5.c
+++ b/drivers/i2c/busses/i2c-exynos5.c
@@ -176,7 +176,10 @@
176 176
177#define EXYNOS5_I2C_TIMEOUT (msecs_to_jiffies(100)) 177#define EXYNOS5_I2C_TIMEOUT (msecs_to_jiffies(100))
178 178
179#define HSI2C_EXYNOS7 BIT(0) 179enum i2c_type_exynos {
180 I2C_TYPE_EXYNOS5,
181 I2C_TYPE_EXYNOS7,
182};
180 183
181struct exynos5_i2c { 184struct exynos5_i2c {
182 struct i2c_adapter adap; 185 struct i2c_adapter adap;
@@ -212,27 +215,30 @@ struct exynos5_i2c {
212/** 215/**
213 * struct exynos_hsi2c_variant - platform specific HSI2C driver data 216 * struct exynos_hsi2c_variant - platform specific HSI2C driver data
214 * @fifo_depth: the fifo depth supported by the HSI2C module 217 * @fifo_depth: the fifo depth supported by the HSI2C module
218 * @hw: the hardware variant of Exynos I2C controller
215 * 219 *
216 * Specifies platform specific configuration of HSI2C module. 220 * Specifies platform specific configuration of HSI2C module.
217 * Note: A structure for driver specific platform data is used for future 221 * Note: A structure for driver specific platform data is used for future
218 * expansion of its usage. 222 * expansion of its usage.
219 */ 223 */
220struct exynos_hsi2c_variant { 224struct exynos_hsi2c_variant {
221 unsigned int fifo_depth; 225 unsigned int fifo_depth;
222 unsigned int hw; 226 enum i2c_type_exynos hw;
223}; 227};
224 228
225static const struct exynos_hsi2c_variant exynos5250_hsi2c_data = { 229static const struct exynos_hsi2c_variant exynos5250_hsi2c_data = {
226 .fifo_depth = 64, 230 .fifo_depth = 64,
231 .hw = I2C_TYPE_EXYNOS5,
227}; 232};
228 233
229static const struct exynos_hsi2c_variant exynos5260_hsi2c_data = { 234static const struct exynos_hsi2c_variant exynos5260_hsi2c_data = {
230 .fifo_depth = 16, 235 .fifo_depth = 16,
236 .hw = I2C_TYPE_EXYNOS5,
231}; 237};
232 238
233static const struct exynos_hsi2c_variant exynos7_hsi2c_data = { 239static const struct exynos_hsi2c_variant exynos7_hsi2c_data = {
234 .fifo_depth = 16, 240 .fifo_depth = 16,
235 .hw = HSI2C_EXYNOS7, 241 .hw = I2C_TYPE_EXYNOS7,
236}; 242};
237 243
238static const struct of_device_id exynos5_i2c_match[] = { 244static const struct of_device_id exynos5_i2c_match[] = {
@@ -300,7 +306,7 @@ static int exynos5_i2c_set_timing(struct exynos5_i2c *i2c, bool hs_timings)
300 */ 306 */
301 t_ftl_cycle = (readl(i2c->regs + HSI2C_CONF) >> 16) & 0x7; 307 t_ftl_cycle = (readl(i2c->regs + HSI2C_CONF) >> 16) & 0x7;
302 temp = clkin / op_clk - 8 - t_ftl_cycle; 308 temp = clkin / op_clk - 8 - t_ftl_cycle;
303 if (i2c->variant->hw != HSI2C_EXYNOS7) 309 if (i2c->variant->hw != I2C_TYPE_EXYNOS7)
304 temp -= t_ftl_cycle; 310 temp -= t_ftl_cycle;
305 div = temp / 512; 311 div = temp / 512;
306 clk_cycle = temp / (div + 1) - 2; 312 clk_cycle = temp / (div + 1) - 2;
@@ -424,7 +430,7 @@ static irqreturn_t exynos5_i2c_irq(int irqno, void *dev_id)
424 writel(int_status, i2c->regs + HSI2C_INT_STATUS); 430 writel(int_status, i2c->regs + HSI2C_INT_STATUS);
425 431
426 /* handle interrupt related to the transfer status */ 432 /* handle interrupt related to the transfer status */
427 if (i2c->variant->hw == HSI2C_EXYNOS7) { 433 if (i2c->variant->hw == I2C_TYPE_EXYNOS7) {
428 if (int_status & HSI2C_INT_TRANS_DONE) { 434 if (int_status & HSI2C_INT_TRANS_DONE) {
429 i2c->trans_done = 1; 435 i2c->trans_done = 1;
430 i2c->state = 0; 436 i2c->state = 0;
@@ -571,7 +577,7 @@ static void exynos5_i2c_bus_check(struct exynos5_i2c *i2c)
571{ 577{
572 unsigned long timeout; 578 unsigned long timeout;
573 579
574 if (i2c->variant->hw != HSI2C_EXYNOS7) 580 if (i2c->variant->hw != I2C_TYPE_EXYNOS7)
575 return; 581 return;
576 582
577 /* 583 /*
@@ -612,7 +618,7 @@ static void exynos5_i2c_message_start(struct exynos5_i2c *i2c, int stop)
612 unsigned long flags; 618 unsigned long flags;
613 unsigned short trig_lvl; 619 unsigned short trig_lvl;
614 620
615 if (i2c->variant->hw == HSI2C_EXYNOS7) 621 if (i2c->variant->hw == I2C_TYPE_EXYNOS7)
616 int_en |= HSI2C_INT_I2C_TRANS; 622 int_en |= HSI2C_INT_I2C_TRANS;
617 else 623 else
618 int_en |= HSI2C_INT_I2C; 624 int_en |= HSI2C_INT_I2C;
diff --git a/drivers/i2c/busses/i2c-fsi.c b/drivers/i2c/busses/i2c-fsi.c
new file mode 100644
index 000000000000..1e2be2219a60
--- /dev/null
+++ b/drivers/i2c/busses/i2c-fsi.c
@@ -0,0 +1,752 @@
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * FSI-attached I2C master algorithm
4 *
5 * Copyright 2018 IBM Corporation
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/bitfield.h>
14#include <linux/bitops.h>
15#include <linux/delay.h>
16#include <linux/device.h>
17#include <linux/errno.h>
18#include <linux/fsi.h>
19#include <linux/i2c.h>
20#include <linux/jiffies.h>
21#include <linux/kernel.h>
22#include <linux/list.h>
23#include <linux/module.h>
24#include <linux/mutex.h>
25#include <linux/of.h>
26#include <linux/slab.h>
27
28#define FSI_ENGID_I2C 0x7
29
30#define I2C_DEFAULT_CLK_DIV 6
31
32/* i2c registers */
33#define I2C_FSI_FIFO 0x00
34#define I2C_FSI_CMD 0x04
35#define I2C_FSI_MODE 0x08
36#define I2C_FSI_WATER_MARK 0x0C
37#define I2C_FSI_INT_MASK 0x10
38#define I2C_FSI_INT_COND 0x14
39#define I2C_FSI_OR_INT_MASK 0x14
40#define I2C_FSI_INTS 0x18
41#define I2C_FSI_AND_INT_MASK 0x18
42#define I2C_FSI_STAT 0x1C
43#define I2C_FSI_RESET_I2C 0x1C
44#define I2C_FSI_ESTAT 0x20
45#define I2C_FSI_RESET_ERR 0x20
46#define I2C_FSI_RESID_LEN 0x24
47#define I2C_FSI_SET_SCL 0x24
48#define I2C_FSI_PORT_BUSY 0x28
49#define I2C_FSI_RESET_SCL 0x2C
50#define I2C_FSI_SET_SDA 0x30
51#define I2C_FSI_RESET_SDA 0x34
52
53/* cmd register */
54#define I2C_CMD_WITH_START BIT(31)
55#define I2C_CMD_WITH_ADDR BIT(30)
56#define I2C_CMD_RD_CONT BIT(29)
57#define I2C_CMD_WITH_STOP BIT(28)
58#define I2C_CMD_FORCELAUNCH BIT(27)
59#define I2C_CMD_ADDR GENMASK(23, 17)
60#define I2C_CMD_READ BIT(16)
61#define I2C_CMD_LEN GENMASK(15, 0)
62
63/* mode register */
64#define I2C_MODE_CLKDIV GENMASK(31, 16)
65#define I2C_MODE_PORT GENMASK(15, 10)
66#define I2C_MODE_ENHANCED BIT(3)
67#define I2C_MODE_DIAG BIT(2)
68#define I2C_MODE_PACE_ALLOW BIT(1)
69#define I2C_MODE_WRAP BIT(0)
70
71/* watermark register */
72#define I2C_WATERMARK_HI GENMASK(15, 12)
73#define I2C_WATERMARK_LO GENMASK(7, 4)
74
75#define I2C_FIFO_HI_LVL 4
76#define I2C_FIFO_LO_LVL 4
77
78/* interrupt register */
79#define I2C_INT_INV_CMD BIT(15)
80#define I2C_INT_PARITY BIT(14)
81#define I2C_INT_BE_OVERRUN BIT(13)
82#define I2C_INT_BE_ACCESS BIT(12)
83#define I2C_INT_LOST_ARB BIT(11)
84#define I2C_INT_NACK BIT(10)
85#define I2C_INT_DAT_REQ BIT(9)
86#define I2C_INT_CMD_COMP BIT(8)
87#define I2C_INT_STOP_ERR BIT(7)
88#define I2C_INT_BUSY BIT(6)
89#define I2C_INT_IDLE BIT(5)
90
91/* status register */
92#define I2C_STAT_INV_CMD BIT(31)
93#define I2C_STAT_PARITY BIT(30)
94#define I2C_STAT_BE_OVERRUN BIT(29)
95#define I2C_STAT_BE_ACCESS BIT(28)
96#define I2C_STAT_LOST_ARB BIT(27)
97#define I2C_STAT_NACK BIT(26)
98#define I2C_STAT_DAT_REQ BIT(25)
99#define I2C_STAT_CMD_COMP BIT(24)
100#define I2C_STAT_STOP_ERR BIT(23)
101#define I2C_STAT_MAX_PORT GENMASK(19, 16)
102#define I2C_STAT_ANY_INT BIT(15)
103#define I2C_STAT_SCL_IN BIT(11)
104#define I2C_STAT_SDA_IN BIT(10)
105#define I2C_STAT_PORT_BUSY BIT(9)
106#define I2C_STAT_SELF_BUSY BIT(8)
107#define I2C_STAT_FIFO_COUNT GENMASK(7, 0)
108
109#define I2C_STAT_ERR (I2C_STAT_INV_CMD | \
110 I2C_STAT_PARITY | \
111 I2C_STAT_BE_OVERRUN | \
112 I2C_STAT_BE_ACCESS | \
113 I2C_STAT_LOST_ARB | \
114 I2C_STAT_NACK | \
115 I2C_STAT_STOP_ERR)
116#define I2C_STAT_ANY_RESP (I2C_STAT_ERR | \
117 I2C_STAT_DAT_REQ | \
118 I2C_STAT_CMD_COMP)
119
120/* extended status register */
121#define I2C_ESTAT_FIFO_SZ GENMASK(31, 24)
122#define I2C_ESTAT_SCL_IN_SY BIT(15)
123#define I2C_ESTAT_SDA_IN_SY BIT(14)
124#define I2C_ESTAT_S_SCL BIT(13)
125#define I2C_ESTAT_S_SDA BIT(12)
126#define I2C_ESTAT_M_SCL BIT(11)
127#define I2C_ESTAT_M_SDA BIT(10)
128#define I2C_ESTAT_HI_WATER BIT(9)
129#define I2C_ESTAT_LO_WATER BIT(8)
130#define I2C_ESTAT_PORT_BUSY BIT(7)
131#define I2C_ESTAT_SELF_BUSY BIT(6)
132#define I2C_ESTAT_VERSION GENMASK(4, 0)
133
134/* port busy register */
135#define I2C_PORT_BUSY_RESET BIT(31)
136
137/* wait for command complete or data request */
138#define I2C_CMD_SLEEP_MAX_US 500
139#define I2C_CMD_SLEEP_MIN_US 50
140
141/* wait after reset; choose time from legacy driver */
142#define I2C_RESET_SLEEP_MAX_US 2000
143#define I2C_RESET_SLEEP_MIN_US 1000
144
145/* choose timeout length from legacy driver; it's well tested */
146#define I2C_ABORT_TIMEOUT msecs_to_jiffies(100)
147
148struct fsi_i2c_master {
149 struct fsi_device *fsi;
150 u8 fifo_size;
151 struct list_head ports;
152 struct mutex lock;
153};
154
155struct fsi_i2c_port {
156 struct list_head list;
157 struct i2c_adapter adapter;
158 struct fsi_i2c_master *master;
159 u16 port;
160 u16 xfrd;
161};
162
163static int fsi_i2c_read_reg(struct fsi_device *fsi, unsigned int reg,
164 u32 *data)
165{
166 int rc;
167 __be32 data_be;
168
169 rc = fsi_device_read(fsi, reg, &data_be, sizeof(data_be));
170 if (rc)
171 return rc;
172
173 *data = be32_to_cpu(data_be);
174
175 return 0;
176}
177
178static int fsi_i2c_write_reg(struct fsi_device *fsi, unsigned int reg,
179 u32 *data)
180{
181 __be32 data_be = cpu_to_be32p(data);
182
183 return fsi_device_write(fsi, reg, &data_be, sizeof(data_be));
184}
185
186static int fsi_i2c_dev_init(struct fsi_i2c_master *i2c)
187{
188 int rc;
189 u32 mode = I2C_MODE_ENHANCED, extended_status, watermark;
190 u32 interrupt = 0;
191
192 /* since we use polling, disable interrupts */
193 rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_INT_MASK, &interrupt);
194 if (rc)
195 return rc;
196
197 mode |= FIELD_PREP(I2C_MODE_CLKDIV, I2C_DEFAULT_CLK_DIV);
198 rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_MODE, &mode);
199 if (rc)
200 return rc;
201
202 rc = fsi_i2c_read_reg(i2c->fsi, I2C_FSI_ESTAT, &extended_status);
203 if (rc)
204 return rc;
205
206 i2c->fifo_size = FIELD_GET(I2C_ESTAT_FIFO_SZ, extended_status);
207 watermark = FIELD_PREP(I2C_WATERMARK_HI,
208 i2c->fifo_size - I2C_FIFO_HI_LVL);
209 watermark |= FIELD_PREP(I2C_WATERMARK_LO, I2C_FIFO_LO_LVL);
210
211 return fsi_i2c_write_reg(i2c->fsi, I2C_FSI_WATER_MARK, &watermark);
212}
213
214static int fsi_i2c_set_port(struct fsi_i2c_port *port)
215{
216 int rc;
217 struct fsi_device *fsi = port->master->fsi;
218 u32 mode, dummy = 0;
219
220 rc = fsi_i2c_read_reg(fsi, I2C_FSI_MODE, &mode);
221 if (rc)
222 return rc;
223
224 if (FIELD_GET(I2C_MODE_PORT, mode) == port->port)
225 return 0;
226
227 mode = (mode & ~I2C_MODE_PORT) | FIELD_PREP(I2C_MODE_PORT, port->port);
228 rc = fsi_i2c_write_reg(fsi, I2C_FSI_MODE, &mode);
229 if (rc)
230 return rc;
231
232 /* reset engine when port is changed */
233 return fsi_i2c_write_reg(fsi, I2C_FSI_RESET_ERR, &dummy);
234}
235
236static int fsi_i2c_start(struct fsi_i2c_port *port, struct i2c_msg *msg,
237 bool stop)
238{
239 struct fsi_i2c_master *i2c = port->master;
240 u32 cmd = I2C_CMD_WITH_START | I2C_CMD_WITH_ADDR;
241
242 port->xfrd = 0;
243
244 if (msg->flags & I2C_M_RD)
245 cmd |= I2C_CMD_READ;
246
247 if (stop || msg->flags & I2C_M_STOP)
248 cmd |= I2C_CMD_WITH_STOP;
249
250 cmd |= FIELD_PREP(I2C_CMD_ADDR, msg->addr);
251 cmd |= FIELD_PREP(I2C_CMD_LEN, msg->len);
252
253 return fsi_i2c_write_reg(i2c->fsi, I2C_FSI_CMD, &cmd);
254}
255
256static int fsi_i2c_get_op_bytes(int op_bytes)
257{
258 /* fsi is limited to max 4 byte aligned ops */
259 if (op_bytes > 4)
260 return 4;
261 else if (op_bytes == 3)
262 return 2;
263 return op_bytes;
264}
265
266static int fsi_i2c_write_fifo(struct fsi_i2c_port *port, struct i2c_msg *msg,
267 u8 fifo_count)
268{
269 int write;
270 int rc;
271 struct fsi_i2c_master *i2c = port->master;
272 int bytes_to_write = i2c->fifo_size - fifo_count;
273 int bytes_remaining = msg->len - port->xfrd;
274
275 bytes_to_write = min(bytes_to_write, bytes_remaining);
276
277 while (bytes_to_write) {
278 write = fsi_i2c_get_op_bytes(bytes_to_write);
279
280 rc = fsi_device_write(i2c->fsi, I2C_FSI_FIFO,
281 &msg->buf[port->xfrd], write);
282 if (rc)
283 return rc;
284
285 port->xfrd += write;
286 bytes_to_write -= write;
287 }
288
289 return 0;
290}
291
292static int fsi_i2c_read_fifo(struct fsi_i2c_port *port, struct i2c_msg *msg,
293 u8 fifo_count)
294{
295 int read;
296 int rc;
297 struct fsi_i2c_master *i2c = port->master;
298 int bytes_to_read;
299 int xfr_remaining = msg->len - port->xfrd;
300 u32 dummy;
301
302 bytes_to_read = min_t(int, fifo_count, xfr_remaining);
303
304 while (bytes_to_read) {
305 read = fsi_i2c_get_op_bytes(bytes_to_read);
306
307 if (xfr_remaining) {
308 rc = fsi_device_read(i2c->fsi, I2C_FSI_FIFO,
309 &msg->buf[port->xfrd], read);
310 if (rc)
311 return rc;
312
313 port->xfrd += read;
314 xfr_remaining -= read;
315 } else {
316 /* no more buffer but data in fifo, need to clear it */
317 rc = fsi_device_read(i2c->fsi, I2C_FSI_FIFO, &dummy,
318 read);
319 if (rc)
320 return rc;
321 }
322
323 bytes_to_read -= read;
324 }
325
326 return 0;
327}
328
329static int fsi_i2c_get_scl(struct i2c_adapter *adap)
330{
331 u32 stat = 0;
332 struct fsi_i2c_port *port = adap->algo_data;
333 struct fsi_i2c_master *i2c = port->master;
334
335 fsi_i2c_read_reg(i2c->fsi, I2C_FSI_STAT, &stat);
336
337 return !!(stat & I2C_STAT_SCL_IN);
338}
339
340static void fsi_i2c_set_scl(struct i2c_adapter *adap, int val)
341{
342 u32 dummy = 0;
343 struct fsi_i2c_port *port = adap->algo_data;
344 struct fsi_i2c_master *i2c = port->master;
345
346 if (val)
347 fsi_i2c_write_reg(i2c->fsi, I2C_FSI_SET_SCL, &dummy);
348 else
349 fsi_i2c_write_reg(i2c->fsi, I2C_FSI_RESET_SCL, &dummy);
350}
351
352static int fsi_i2c_get_sda(struct i2c_adapter *adap)
353{
354 u32 stat = 0;
355 struct fsi_i2c_port *port = adap->algo_data;
356 struct fsi_i2c_master *i2c = port->master;
357
358 fsi_i2c_read_reg(i2c->fsi, I2C_FSI_STAT, &stat);
359
360 return !!(stat & I2C_STAT_SDA_IN);
361}
362
363static void fsi_i2c_set_sda(struct i2c_adapter *adap, int val)
364{
365 u32 dummy = 0;
366 struct fsi_i2c_port *port = adap->algo_data;
367 struct fsi_i2c_master *i2c = port->master;
368
369 if (val)
370 fsi_i2c_write_reg(i2c->fsi, I2C_FSI_SET_SDA, &dummy);
371 else
372 fsi_i2c_write_reg(i2c->fsi, I2C_FSI_RESET_SDA, &dummy);
373}
374
375static void fsi_i2c_prepare_recovery(struct i2c_adapter *adap)
376{
377 int rc;
378 u32 mode;
379 struct fsi_i2c_port *port = adap->algo_data;
380 struct fsi_i2c_master *i2c = port->master;
381
382 rc = fsi_i2c_read_reg(i2c->fsi, I2C_FSI_MODE, &mode);
383 if (rc)
384 return;
385
386 mode |= I2C_MODE_DIAG;
387 fsi_i2c_write_reg(i2c->fsi, I2C_FSI_MODE, &mode);
388}
389
390static void fsi_i2c_unprepare_recovery(struct i2c_adapter *adap)
391{
392 int rc;
393 u32 mode;
394 struct fsi_i2c_port *port = adap->algo_data;
395 struct fsi_i2c_master *i2c = port->master;
396
397 rc = fsi_i2c_read_reg(i2c->fsi, I2C_FSI_MODE, &mode);
398 if (rc)
399 return;
400
401 mode &= ~I2C_MODE_DIAG;
402 fsi_i2c_write_reg(i2c->fsi, I2C_FSI_MODE, &mode);
403}
404
405static int fsi_i2c_reset_bus(struct fsi_i2c_master *i2c,
406 struct fsi_i2c_port *port)
407{
408 int rc;
409 u32 stat, dummy = 0;
410
411 /* force bus reset, ignore errors */
412 i2c_recover_bus(&port->adapter);
413
414 /* reset errors */
415 rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_RESET_ERR, &dummy);
416 if (rc)
417 return rc;
418
419 /* wait for command complete */
420 usleep_range(I2C_RESET_SLEEP_MIN_US, I2C_RESET_SLEEP_MAX_US);
421
422 rc = fsi_i2c_read_reg(i2c->fsi, I2C_FSI_STAT, &stat);
423 if (rc)
424 return rc;
425
426 if (stat & I2C_STAT_CMD_COMP)
427 return 0;
428
429 /* failed to get command complete; reset engine again */
430 rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_RESET_I2C, &dummy);
431 if (rc)
432 return rc;
433
434 /* re-init engine again */
435 return fsi_i2c_dev_init(i2c);
436}
437
438static int fsi_i2c_reset_engine(struct fsi_i2c_master *i2c, u16 port)
439{
440 int rc;
441 u32 mode, dummy = 0;
442
443 /* reset engine */
444 rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_RESET_I2C, &dummy);
445 if (rc)
446 return rc;
447
448 /* re-init engine */
449 rc = fsi_i2c_dev_init(i2c);
450 if (rc)
451 return rc;
452
453 rc = fsi_i2c_read_reg(i2c->fsi, I2C_FSI_MODE, &mode);
454 if (rc)
455 return rc;
456
457 /* set port; default after reset is 0 */
458 if (port) {
459 mode &= ~I2C_MODE_PORT;
460 mode |= FIELD_PREP(I2C_MODE_PORT, port);
461 rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_MODE, &mode);
462 if (rc)
463 return rc;
464 }
465
466 /* reset busy register; hw workaround */
467 dummy = I2C_PORT_BUSY_RESET;
468 rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_PORT_BUSY, &dummy);
469 if (rc)
470 return rc;
471
472 return 0;
473}
474
475static int fsi_i2c_abort(struct fsi_i2c_port *port, u32 status)
476{
477 int rc;
478 unsigned long start;
479 u32 cmd = I2C_CMD_WITH_STOP;
480 u32 stat;
481 struct fsi_i2c_master *i2c = port->master;
482 struct fsi_device *fsi = i2c->fsi;
483
484 rc = fsi_i2c_reset_engine(i2c, port->port);
485 if (rc)
486 return rc;
487
488 rc = fsi_i2c_read_reg(fsi, I2C_FSI_STAT, &stat);
489 if (rc)
490 return rc;
491
492 /* if sda is low, peform full bus reset */
493 if (!(stat & I2C_STAT_SDA_IN)) {
494 rc = fsi_i2c_reset_bus(i2c, port);
495 if (rc)
496 return rc;
497 }
498
499 /* skip final stop command for these errors */
500 if (status & (I2C_STAT_PARITY | I2C_STAT_LOST_ARB | I2C_STAT_STOP_ERR))
501 return 0;
502
503 /* write stop command */
504 rc = fsi_i2c_write_reg(fsi, I2C_FSI_CMD, &cmd);
505 if (rc)
506 return rc;
507
508 /* wait until we see command complete in the master */
509 start = jiffies;
510
511 do {
512 rc = fsi_i2c_read_reg(fsi, I2C_FSI_STAT, &status);
513 if (rc)
514 return rc;
515
516 if (status & I2C_STAT_CMD_COMP)
517 return 0;
518
519 usleep_range(I2C_CMD_SLEEP_MIN_US, I2C_CMD_SLEEP_MAX_US);
520 } while (time_after(start + I2C_ABORT_TIMEOUT, jiffies));
521
522 return -ETIMEDOUT;
523}
524
525static int fsi_i2c_handle_status(struct fsi_i2c_port *port,
526 struct i2c_msg *msg, u32 status)
527{
528 int rc;
529 u8 fifo_count;
530
531 if (status & I2C_STAT_ERR) {
532 rc = fsi_i2c_abort(port, status);
533 if (rc)
534 return rc;
535
536 if (status & I2C_STAT_INV_CMD)
537 return -EINVAL;
538
539 if (status & (I2C_STAT_PARITY | I2C_STAT_BE_OVERRUN |
540 I2C_STAT_BE_ACCESS))
541 return -EPROTO;
542
543 if (status & I2C_STAT_NACK)
544 return -ENXIO;
545
546 if (status & I2C_STAT_LOST_ARB)
547 return -EAGAIN;
548
549 if (status & I2C_STAT_STOP_ERR)
550 return -EBADMSG;
551
552 return -EIO;
553 }
554
555 if (status & I2C_STAT_DAT_REQ) {
556 fifo_count = FIELD_GET(I2C_STAT_FIFO_COUNT, status);
557
558 if (msg->flags & I2C_M_RD)
559 return fsi_i2c_read_fifo(port, msg, fifo_count);
560
561 return fsi_i2c_write_fifo(port, msg, fifo_count);
562 }
563
564 if (status & I2C_STAT_CMD_COMP) {
565 if (port->xfrd < msg->len)
566 return -ENODATA;
567
568 return msg->len;
569 }
570
571 return 0;
572}
573
574static int fsi_i2c_wait(struct fsi_i2c_port *port, struct i2c_msg *msg,
575 unsigned long timeout)
576{
577 u32 status = 0;
578 int rc;
579 unsigned long start = jiffies;
580
581 do {
582 rc = fsi_i2c_read_reg(port->master->fsi, I2C_FSI_STAT,
583 &status);
584 if (rc)
585 return rc;
586
587 if (status & I2C_STAT_ANY_RESP) {
588 rc = fsi_i2c_handle_status(port, msg, status);
589 if (rc < 0)
590 return rc;
591
592 /* cmd complete and all data xfrd */
593 if (rc == msg->len)
594 return 0;
595
596 /* need to xfr more data, but maybe don't need wait */
597 continue;
598 }
599
600 usleep_range(I2C_CMD_SLEEP_MIN_US, I2C_CMD_SLEEP_MAX_US);
601 } while (time_after(start + timeout, jiffies));
602
603 return -ETIMEDOUT;
604}
605
606static int fsi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
607 int num)
608{
609 int i, rc;
610 unsigned long start_time;
611 struct fsi_i2c_port *port = adap->algo_data;
612 struct fsi_i2c_master *master = port->master;
613 struct i2c_msg *msg;
614
615 mutex_lock(&master->lock);
616
617 rc = fsi_i2c_set_port(port);
618 if (rc)
619 goto unlock;
620
621 for (i = 0; i < num; i++) {
622 msg = msgs + i;
623 start_time = jiffies;
624
625 rc = fsi_i2c_start(port, msg, i == num - 1);
626 if (rc)
627 goto unlock;
628
629 rc = fsi_i2c_wait(port, msg,
630 adap->timeout - (jiffies - start_time));
631 if (rc)
632 goto unlock;
633 }
634
635unlock:
636 mutex_unlock(&master->lock);
637 return rc ? : num;
638}
639
640static u32 fsi_i2c_functionality(struct i2c_adapter *adap)
641{
642 return I2C_FUNC_I2C | I2C_FUNC_PROTOCOL_MANGLING |
643 I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SMBUS_BLOCK_DATA;
644}
645
646static struct i2c_bus_recovery_info fsi_i2c_bus_recovery_info = {
647 .recover_bus = i2c_generic_scl_recovery,
648 .get_scl = fsi_i2c_get_scl,
649 .set_scl = fsi_i2c_set_scl,
650 .get_sda = fsi_i2c_get_sda,
651 .set_sda = fsi_i2c_set_sda,
652 .prepare_recovery = fsi_i2c_prepare_recovery,
653 .unprepare_recovery = fsi_i2c_unprepare_recovery,
654};
655
656static const struct i2c_algorithm fsi_i2c_algorithm = {
657 .master_xfer = fsi_i2c_xfer,
658 .functionality = fsi_i2c_functionality,
659};
660
661static int fsi_i2c_probe(struct device *dev)
662{
663 struct fsi_i2c_master *i2c;
664 struct fsi_i2c_port *port;
665 struct device_node *np;
666 int rc;
667 u32 port_no;
668
669 i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
670 if (!i2c)
671 return -ENOMEM;
672
673 mutex_init(&i2c->lock);
674 i2c->fsi = to_fsi_dev(dev);
675 INIT_LIST_HEAD(&i2c->ports);
676
677 rc = fsi_i2c_dev_init(i2c);
678 if (rc)
679 return rc;
680
681 /* Add adapter for each i2c port of the master. */
682 for_each_available_child_of_node(dev->of_node, np) {
683 rc = of_property_read_u32(np, "reg", &port_no);
684 if (rc || port_no > USHRT_MAX)
685 continue;
686
687 port = kzalloc(sizeof(*port), GFP_KERNEL);
688 if (!port)
689 break;
690
691 port->master = i2c;
692 port->port = port_no;
693
694 port->adapter.owner = THIS_MODULE;
695 port->adapter.dev.of_node = np;
696 port->adapter.dev.parent = dev;
697 port->adapter.algo = &fsi_i2c_algorithm;
698 port->adapter.bus_recovery_info = &fsi_i2c_bus_recovery_info;
699 port->adapter.algo_data = port;
700
701 snprintf(port->adapter.name, sizeof(port->adapter.name),
702 "i2c_bus-%u", port_no);
703
704 rc = i2c_add_adapter(&port->adapter);
705 if (rc < 0) {
706 dev_err(dev, "Failed to register adapter: %d\n", rc);
707 kfree(port);
708 continue;
709 }
710
711 list_add(&port->list, &i2c->ports);
712 }
713
714 dev_set_drvdata(dev, i2c);
715
716 return 0;
717}
718
719static int fsi_i2c_remove(struct device *dev)
720{
721 struct fsi_i2c_master *i2c = dev_get_drvdata(dev);
722 struct fsi_i2c_port *port, *tmp;
723
724 list_for_each_entry_safe(port, tmp, &i2c->ports, list) {
725 list_del(&port->list);
726 i2c_del_adapter(&port->adapter);
727 kfree(port);
728 }
729
730 return 0;
731}
732
733static const struct fsi_device_id fsi_i2c_ids[] = {
734 { FSI_ENGID_I2C, FSI_VERSION_ANY },
735 { }
736};
737
738static struct fsi_driver fsi_i2c_driver = {
739 .id_table = fsi_i2c_ids,
740 .drv = {
741 .name = "i2c-fsi",
742 .bus = &fsi_bus_type,
743 .probe = fsi_i2c_probe,
744 .remove = fsi_i2c_remove,
745 },
746};
747
748module_fsi_driver(fsi_i2c_driver);
749
750MODULE_AUTHOR("Eddie James <eajames@us.ibm.com>");
751MODULE_DESCRIPTION("FSI attached I2C master");
752MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c
index 66f85bbf3591..c008d209f0b8 100644
--- a/drivers/i2c/busses/i2c-gpio.c
+++ b/drivers/i2c/busses/i2c-gpio.c
@@ -78,49 +78,43 @@ static struct dentry *i2c_gpio_debug_dir;
78#define getscl(bd) ((bd)->getscl((bd)->data)) 78#define getscl(bd) ((bd)->getscl((bd)->data))
79 79
80#define WIRE_ATTRIBUTE(wire) \ 80#define WIRE_ATTRIBUTE(wire) \
81static int fops_##wire##_get(void *data, u64 *val) \ 81static int fops_##wire##_get(void *data, u64 *val) \
82{ \ 82{ \
83 struct i2c_gpio_private_data *priv = data; \ 83 struct i2c_gpio_private_data *priv = data; \
84 \ 84 \
85 i2c_lock_adapter(&priv->adap); \ 85 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
86 *val = get##wire(&priv->bit_data); \ 86 *val = get##wire(&priv->bit_data); \
87 i2c_unlock_adapter(&priv->adap); \ 87 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
88 return 0; \ 88 return 0; \
89} \ 89} \
90static int fops_##wire##_set(void *data, u64 val) \ 90static int fops_##wire##_set(void *data, u64 val) \
91{ \ 91{ \
92 struct i2c_gpio_private_data *priv = data; \ 92 struct i2c_gpio_private_data *priv = data; \
93 \ 93 \
94 i2c_lock_adapter(&priv->adap); \ 94 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
95 set##wire(&priv->bit_data, val); \ 95 set##wire(&priv->bit_data, val); \
96 i2c_unlock_adapter(&priv->adap); \ 96 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
97 return 0; \ 97 return 0; \
98} \ 98} \
99DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n") 99DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
100 100
101WIRE_ATTRIBUTE(scl); 101WIRE_ATTRIBUTE(scl);
102WIRE_ATTRIBUTE(sda); 102WIRE_ATTRIBUTE(sda);
103 103
104static int fops_incomplete_transfer_set(void *data, u64 addr) 104static void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data *priv,
105 u32 pattern, u8 pattern_size)
105{ 106{
106 struct i2c_gpio_private_data *priv = data;
107 struct i2c_algo_bit_data *bit_data = &priv->bit_data; 107 struct i2c_algo_bit_data *bit_data = &priv->bit_data;
108 int i, pattern; 108 int i;
109 109
110 if (addr > 0x7f) 110 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
111 return -EINVAL;
112
113 /* ADDR (7 bit) + RD (1 bit) + SDA hi (1 bit) */
114 pattern = (addr << 2) | 3;
115
116 i2c_lock_adapter(&priv->adap);
117 111
118 /* START condition */ 112 /* START condition */
119 setsda(bit_data, 0); 113 setsda(bit_data, 0);
120 udelay(bit_data->udelay); 114 udelay(bit_data->udelay);
121 115
122 /* Send ADDR+RD, request ACK, don't send STOP */ 116 /* Send pattern, request ACK, don't send STOP */
123 for (i = 8; i >= 0; i--) { 117 for (i = pattern_size - 1; i >= 0; i--) {
124 setscl(bit_data, 0); 118 setscl(bit_data, 0);
125 udelay(bit_data->udelay / 2); 119 udelay(bit_data->udelay / 2);
126 setsda(bit_data, (pattern >> i) & 1); 120 setsda(bit_data, (pattern >> i) & 1);
@@ -129,11 +123,44 @@ static int fops_incomplete_transfer_set(void *data, u64 addr)
129 udelay(bit_data->udelay); 123 udelay(bit_data->udelay);
130 } 124 }
131 125
132 i2c_unlock_adapter(&priv->adap); 126 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
127}
128
129static int fops_incomplete_addr_phase_set(void *data, u64 addr)
130{
131 struct i2c_gpio_private_data *priv = data;
132 u32 pattern;
133
134 if (addr > 0x7f)
135 return -EINVAL;
136
137 /* ADDR (7 bit) + RD (1 bit) + Client ACK, keep SDA hi (1 bit) */
138 pattern = (addr << 2) | 3;
139
140 i2c_gpio_incomplete_transfer(priv, pattern, 9);
141
142 return 0;
143}
144DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase, NULL, fops_incomplete_addr_phase_set, "%llu\n");
145
146static int fops_incomplete_write_byte_set(void *data, u64 addr)
147{
148 struct i2c_gpio_private_data *priv = data;
149 u32 pattern;
150
151 if (addr > 0x7f)
152 return -EINVAL;
153
154 /* ADDR (7 bit) + WR (1 bit) + Client ACK (1 bit) */
155 pattern = (addr << 2) | 1;
156 /* 0x00 (8 bit) + Client ACK, keep SDA hi (1 bit) */
157 pattern = (pattern << 9) | 1;
158
159 i2c_gpio_incomplete_transfer(priv, pattern, 18);
133 160
134 return 0; 161 return 0;
135} 162}
136DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_transfer, NULL, fops_incomplete_transfer_set, "%llu\n"); 163DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n");
137 164
138static void i2c_gpio_fault_injector_init(struct platform_device *pdev) 165static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
139{ 166{
@@ -156,8 +183,10 @@ static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
156 183
157 debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl); 184 debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
158 debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda); 185 debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
159 debugfs_create_file_unsafe("incomplete_transfer", 0200, priv->debug_dir, 186 debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->debug_dir,
160 priv, &fops_incomplete_transfer); 187 priv, &fops_incomplete_addr_phase);
188 debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->debug_dir,
189 priv, &fops_incomplete_write_byte);
161} 190}
162 191
163static void i2c_gpio_fault_injector_exit(struct platform_device *pdev) 192static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
diff --git a/drivers/i2c/busses/i2c-i801.c b/drivers/i2c/busses/i2c-i801.c
index aa726607645e..941c223f6491 100644
--- a/drivers/i2c/busses/i2c-i801.c
+++ b/drivers/i2c/busses/i2c-i801.c
@@ -70,6 +70,7 @@
70 * Cannon Lake-H (PCH) 0xa323 32 hard yes yes yes 70 * Cannon Lake-H (PCH) 0xa323 32 hard yes yes yes
71 * Cannon Lake-LP (PCH) 0x9da3 32 hard yes yes yes 71 * Cannon Lake-LP (PCH) 0x9da3 32 hard yes yes yes
72 * Cedar Fork (PCH) 0x18df 32 hard yes yes yes 72 * Cedar Fork (PCH) 0x18df 32 hard yes yes yes
73 * Ice Lake-LP (PCH) 0x34a3 32 hard yes yes yes
73 * 74 *
74 * Features supported by this driver: 75 * Features supported by this driver:
75 * Software PEC no 76 * Software PEC no
@@ -220,6 +221,7 @@
220#define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330 221#define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
221#define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0 222#define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0
222#define PCI_DEVICE_ID_INTEL_GEMINILAKE_SMBUS 0x31d4 223#define PCI_DEVICE_ID_INTEL_GEMINILAKE_SMBUS 0x31d4
224#define PCI_DEVICE_ID_INTEL_ICELAKE_LP_SMBUS 0x34a3
223#define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30 225#define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
224#define PCI_DEVICE_ID_INTEL_BROXTON_SMBUS 0x5ad4 226#define PCI_DEVICE_ID_INTEL_BROXTON_SMBUS 0x5ad4
225#define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22 227#define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
@@ -1034,6 +1036,7 @@ static const struct pci_device_id i801_ids[] = {
1034 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS) }, 1036 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS) },
1035 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS) }, 1037 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS) },
1036 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS) }, 1038 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS) },
1039 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICELAKE_LP_SMBUS) },
1037 { 0, } 1040 { 0, }
1038}; 1041};
1039 1042
@@ -1518,6 +1521,7 @@ static int i801_probe(struct pci_dev *dev, const struct pci_device_id *id)
1518 case PCI_DEVICE_ID_INTEL_CDF_SMBUS: 1521 case PCI_DEVICE_ID_INTEL_CDF_SMBUS:
1519 case PCI_DEVICE_ID_INTEL_DNV_SMBUS: 1522 case PCI_DEVICE_ID_INTEL_DNV_SMBUS:
1520 case PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS: 1523 case PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS:
1524 case PCI_DEVICE_ID_INTEL_ICELAKE_LP_SMBUS:
1521 priv->features |= FEATURE_I2C_BLOCK_READ; 1525 priv->features |= FEATURE_I2C_BLOCK_READ;
1522 priv->features |= FEATURE_IRQ; 1526 priv->features |= FEATURE_IRQ;
1523 priv->features |= FEATURE_SMBUS_PEC; 1527 priv->features |= FEATURE_SMBUS_PEC;
diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index 498c5e891649..c406700789e1 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -421,10 +421,14 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
421 return -EAGAIN; 421 return -EAGAIN;
422 } 422 }
423 423
424 if (for_busy && (temp & I2SR_IBB)) 424 if (for_busy && (temp & I2SR_IBB)) {
425 i2c_imx->stopped = 0;
425 break; 426 break;
426 if (!for_busy && !(temp & I2SR_IBB)) 427 }
428 if (!for_busy && !(temp & I2SR_IBB)) {
429 i2c_imx->stopped = 1;
427 break; 430 break;
431 }
428 if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) { 432 if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
429 dev_dbg(&i2c_imx->adapter.dev, 433 dev_dbg(&i2c_imx->adapter.dev,
430 "<%s> I2C bus is busy\n", __func__); 434 "<%s> I2C bus is busy\n", __func__);
@@ -538,7 +542,6 @@ static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
538 result = i2c_imx_bus_busy(i2c_imx, 1); 542 result = i2c_imx_bus_busy(i2c_imx, 1);
539 if (result) 543 if (result)
540 return result; 544 return result;
541 i2c_imx->stopped = 0;
542 545
543 temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK; 546 temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
544 temp &= ~I2CR_DMAEN; 547 temp &= ~I2CR_DMAEN;
@@ -567,10 +570,8 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
567 udelay(i2c_imx->disable_delay); 570 udelay(i2c_imx->disable_delay);
568 } 571 }
569 572
570 if (!i2c_imx->stopped) { 573 if (!i2c_imx->stopped)
571 i2c_imx_bus_busy(i2c_imx, 0); 574 i2c_imx_bus_busy(i2c_imx, 0);
572 i2c_imx->stopped = 1;
573 }
574 575
575 /* Disable I2C controller */ 576 /* Disable I2C controller */
576 temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN, 577 temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
@@ -668,9 +669,6 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
668 struct imx_i2c_dma *dma = i2c_imx->dma; 669 struct imx_i2c_dma *dma = i2c_imx->dma;
669 struct device *dev = &i2c_imx->adapter.dev; 670 struct device *dev = &i2c_imx->adapter.dev;
670 671
671 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
672 temp |= I2CR_DMAEN;
673 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
674 672
675 dma->chan_using = dma->chan_rx; 673 dma->chan_using = dma->chan_rx;
676 dma->dma_transfer_dir = DMA_DEV_TO_MEM; 674 dma->dma_transfer_dir = DMA_DEV_TO_MEM;
@@ -727,7 +725,6 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
727 temp &= ~(I2CR_MSTA | I2CR_MTX); 725 temp &= ~(I2CR_MSTA | I2CR_MTX);
728 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); 726 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
729 i2c_imx_bus_busy(i2c_imx, 0); 727 i2c_imx_bus_busy(i2c_imx, 0);
730 i2c_imx->stopped = 1;
731 } else { 728 } else {
732 /* 729 /*
733 * For i2c master receiver repeat restart operation like: 730 * For i2c master receiver repeat restart operation like:
@@ -783,6 +780,7 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bo
783 int i, result; 780 int i, result;
784 unsigned int temp; 781 unsigned int temp;
785 int block_data = msgs->flags & I2C_M_RECV_LEN; 782 int block_data = msgs->flags & I2C_M_RECV_LEN;
783 int use_dma = i2c_imx->dma && msgs->len >= DMA_THRESHOLD && !block_data;
786 784
787 dev_dbg(&i2c_imx->adapter.dev, 785 dev_dbg(&i2c_imx->adapter.dev,
788 "<%s> write slave address: addr=0x%x\n", 786 "<%s> write slave address: addr=0x%x\n",
@@ -809,12 +807,14 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bo
809 */ 807 */
810 if ((msgs->len - 1) || block_data) 808 if ((msgs->len - 1) || block_data)
811 temp &= ~I2CR_TXAK; 809 temp &= ~I2CR_TXAK;
810 if (use_dma)
811 temp |= I2CR_DMAEN;
812 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); 812 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
813 imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */ 813 imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */
814 814
815 dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__); 815 dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
816 816
817 if (i2c_imx->dma && msgs->len >= DMA_THRESHOLD && !block_data) 817 if (use_dma)
818 return i2c_imx_dma_read(i2c_imx, msgs, is_lastmsg); 818 return i2c_imx_dma_read(i2c_imx, msgs, is_lastmsg);
819 819
820 /* read data */ 820 /* read data */
@@ -850,7 +850,6 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bo
850 temp &= ~(I2CR_MSTA | I2CR_MTX); 850 temp &= ~(I2CR_MSTA | I2CR_MTX);
851 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); 851 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
852 i2c_imx_bus_busy(i2c_imx, 0); 852 i2c_imx_bus_busy(i2c_imx, 0);
853 i2c_imx->stopped = 1;
854 } else { 853 } else {
855 /* 854 /*
856 * For i2c master receiver repeat restart operation like: 855 * For i2c master receiver repeat restart operation like:
diff --git a/drivers/i2c/busses/i2c-mxs.c b/drivers/i2c/busses/i2c-mxs.c
index 642c58946d8d..7d79317a1046 100644
--- a/drivers/i2c/busses/i2c-mxs.c
+++ b/drivers/i2c/busses/i2c-mxs.c
@@ -567,9 +567,6 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
567 dev_dbg(i2c->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n", 567 dev_dbg(i2c->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
568 msg->addr, msg->len, msg->flags, stop); 568 msg->addr, msg->len, msg->flags, stop);
569 569
570 if (msg->len == 0)
571 return -EINVAL;
572
573 /* 570 /*
574 * The MX28 I2C IP block can only do PIO READ for transfer of to up 571 * The MX28 I2C IP block can only do PIO READ for transfer of to up
575 * 4 bytes of length. The write transfer is not limited as it can use 572 * 4 bytes of length. The write transfer is not limited as it can use
@@ -683,6 +680,10 @@ static const struct i2c_algorithm mxs_i2c_algo = {
683 .functionality = mxs_i2c_func, 680 .functionality = mxs_i2c_func,
684}; 681};
685 682
683static const struct i2c_adapter_quirks mxs_i2c_quirks = {
684 .flags = I2C_AQ_NO_ZERO_LEN,
685};
686
686static void mxs_i2c_derive_timing(struct mxs_i2c_dev *i2c, uint32_t speed) 687static void mxs_i2c_derive_timing(struct mxs_i2c_dev *i2c, uint32_t speed)
687{ 688{
688 /* The I2C block clock runs at 24MHz */ 689 /* The I2C block clock runs at 24MHz */
@@ -854,6 +855,7 @@ static int mxs_i2c_probe(struct platform_device *pdev)
854 strlcpy(adap->name, "MXS I2C adapter", sizeof(adap->name)); 855 strlcpy(adap->name, "MXS I2C adapter", sizeof(adap->name));
855 adap->owner = THIS_MODULE; 856 adap->owner = THIS_MODULE;
856 adap->algo = &mxs_i2c_algo; 857 adap->algo = &mxs_i2c_algo;
858 adap->quirks = &mxs_i2c_quirks;
857 adap->dev.parent = dev; 859 adap->dev.parent = dev;
858 adap->nr = pdev->id; 860 adap->nr = pdev->id;
859 adap->dev.of_node = pdev->dev.of_node; 861 adap->dev.of_node = pdev->dev.of_node;
diff --git a/drivers/i2c/busses/i2c-owl.c b/drivers/i2c/busses/i2c-owl.c
new file mode 100644
index 000000000000..96b4572e6d9c
--- /dev/null
+++ b/drivers/i2c/busses/i2c-owl.c
@@ -0,0 +1,495 @@
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Actions Semiconductor Owl SoC's I2C driver
4 *
5 * Copyright (c) 2014 Actions Semi Inc.
6 * Author: David Liu <liuwei@actions-semi.com>
7 *
8 * Copyright (c) 2018 Linaro Ltd.
9 * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
10 */
11
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/i2c.h>
15#include <linux/interrupt.h>
16#include <linux/io.h>
17#include <linux/module.h>
18#include <linux/of_device.h>
19
20/* I2C registers */
21#define OWL_I2C_REG_CTL 0x0000
22#define OWL_I2C_REG_CLKDIV 0x0004
23#define OWL_I2C_REG_STAT 0x0008
24#define OWL_I2C_REG_ADDR 0x000C
25#define OWL_I2C_REG_TXDAT 0x0010
26#define OWL_I2C_REG_RXDAT 0x0014
27#define OWL_I2C_REG_CMD 0x0018
28#define OWL_I2C_REG_FIFOCTL 0x001C
29#define OWL_I2C_REG_FIFOSTAT 0x0020
30#define OWL_I2C_REG_DATCNT 0x0024
31#define OWL_I2C_REG_RCNT 0x0028
32
33/* I2Cx_CTL Bit Mask */
34#define OWL_I2C_CTL_RB BIT(1)
35#define OWL_I2C_CTL_GBCC(x) (((x) & 0x3) << 2)
36#define OWL_I2C_CTL_GBCC_NONE OWL_I2C_CTL_GBCC(0)
37#define OWL_I2C_CTL_GBCC_START OWL_I2C_CTL_GBCC(1)
38#define OWL_I2C_CTL_GBCC_STOP OWL_I2C_CTL_GBCC(2)
39#define OWL_I2C_CTL_GBCC_RSTART OWL_I2C_CTL_GBCC(3)
40#define OWL_I2C_CTL_IRQE BIT(5)
41#define OWL_I2C_CTL_EN BIT(7)
42#define OWL_I2C_CTL_AE BIT(8)
43#define OWL_I2C_CTL_SHSM BIT(10)
44
45#define OWL_I2C_DIV_FACTOR(x) ((x) & 0xff)
46
47/* I2Cx_STAT Bit Mask */
48#define OWL_I2C_STAT_RACK BIT(0)
49#define OWL_I2C_STAT_BEB BIT(1)
50#define OWL_I2C_STAT_IRQP BIT(2)
51#define OWL_I2C_STAT_LAB BIT(3)
52#define OWL_I2C_STAT_STPD BIT(4)
53#define OWL_I2C_STAT_STAD BIT(5)
54#define OWL_I2C_STAT_BBB BIT(6)
55#define OWL_I2C_STAT_TCB BIT(7)
56#define OWL_I2C_STAT_LBST BIT(8)
57#define OWL_I2C_STAT_SAMB BIT(9)
58#define OWL_I2C_STAT_SRGC BIT(10)
59
60/* I2Cx_CMD Bit Mask */
61#define OWL_I2C_CMD_SBE BIT(0)
62#define OWL_I2C_CMD_RBE BIT(4)
63#define OWL_I2C_CMD_DE BIT(8)
64#define OWL_I2C_CMD_NS BIT(9)
65#define OWL_I2C_CMD_SE BIT(10)
66#define OWL_I2C_CMD_MSS BIT(11)
67#define OWL_I2C_CMD_WRS BIT(12)
68#define OWL_I2C_CMD_SECL BIT(15)
69
70#define OWL_I2C_CMD_AS(x) (((x) & 0x7) << 1)
71#define OWL_I2C_CMD_SAS(x) (((x) & 0x7) << 5)
72
73/* I2Cx_FIFOCTL Bit Mask */
74#define OWL_I2C_FIFOCTL_NIB BIT(0)
75#define OWL_I2C_FIFOCTL_RFR BIT(1)
76#define OWL_I2C_FIFOCTL_TFR BIT(2)
77
78/* I2Cc_FIFOSTAT Bit Mask */
79#define OWL_I2C_FIFOSTAT_RNB BIT(1)
80#define OWL_I2C_FIFOSTAT_RFE BIT(2)
81#define OWL_I2C_FIFOSTAT_TFF BIT(5)
82#define OWL_I2C_FIFOSTAT_TFD GENMASK(23, 16)
83#define OWL_I2C_FIFOSTAT_RFD GENMASK(15, 8)
84
85/* I2C bus timeout */
86#define OWL_I2C_TIMEOUT msecs_to_jiffies(4 * 1000)
87
88#define OWL_I2C_MAX_RETRIES 50
89
90#define OWL_I2C_DEF_SPEED_HZ 100000
91#define OWL_I2C_MAX_SPEED_HZ 400000
92
93struct owl_i2c_dev {
94 struct i2c_adapter adap;
95 struct i2c_msg *msg;
96 struct completion msg_complete;
97 struct clk *clk;
98 spinlock_t lock;
99 void __iomem *base;
100 unsigned long clk_rate;
101 u32 bus_freq;
102 u32 msg_ptr;
103 int err;
104};
105
106static void owl_i2c_update_reg(void __iomem *reg, unsigned int val, bool state)
107{
108 unsigned int regval;
109
110 regval = readl(reg);
111
112 if (state)
113 regval |= val;
114 else
115 regval &= ~val;
116
117 writel(regval, reg);
118}
119
120static void owl_i2c_reset(struct owl_i2c_dev *i2c_dev)
121{
122 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
123 OWL_I2C_CTL_EN, false);
124 mdelay(1);
125 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
126 OWL_I2C_CTL_EN, true);
127
128 /* Clear status registers */
129 writel(0, i2c_dev->base + OWL_I2C_REG_STAT);
130}
131
132static int owl_i2c_reset_fifo(struct owl_i2c_dev *i2c_dev)
133{
134 unsigned int val, timeout = 0;
135
136 /* Reset FIFO */
137 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL,
138 OWL_I2C_FIFOCTL_RFR | OWL_I2C_FIFOCTL_TFR,
139 true);
140
141 /* Wait 50ms for FIFO reset complete */
142 do {
143 val = readl(i2c_dev->base + OWL_I2C_REG_FIFOCTL);
144 if (!(val & (OWL_I2C_FIFOCTL_RFR | OWL_I2C_FIFOCTL_TFR)))
145 break;
146 usleep_range(500, 1000);
147 } while (timeout++ < OWL_I2C_MAX_RETRIES);
148
149 if (timeout > OWL_I2C_MAX_RETRIES) {
150 dev_err(&i2c_dev->adap.dev, "FIFO reset timeout\n");
151 return -ETIMEDOUT;
152 }
153
154 return 0;
155}
156
157static void owl_i2c_set_freq(struct owl_i2c_dev *i2c_dev)
158{
159 unsigned int val;
160
161 val = DIV_ROUND_UP(i2c_dev->clk_rate, i2c_dev->bus_freq * 16);
162
163 /* Set clock divider factor */
164 writel(OWL_I2C_DIV_FACTOR(val), i2c_dev->base + OWL_I2C_REG_CLKDIV);
165}
166
167static irqreturn_t owl_i2c_interrupt(int irq, void *_dev)
168{
169 struct owl_i2c_dev *i2c_dev = _dev;
170 struct i2c_msg *msg = i2c_dev->msg;
171 unsigned long flags;
172 unsigned int stat, fifostat;
173
174 spin_lock_irqsave(&i2c_dev->lock, flags);
175
176 i2c_dev->err = 0;
177
178 /* Handle NACK from slave */
179 fifostat = readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT);
180 if (fifostat & OWL_I2C_FIFOSTAT_RNB) {
181 i2c_dev->err = -ENXIO;
182 goto stop;
183 }
184
185 /* Handle bus error */
186 stat = readl(i2c_dev->base + OWL_I2C_REG_STAT);
187 if (stat & OWL_I2C_STAT_BEB) {
188 i2c_dev->err = -EIO;
189 goto stop;
190 }
191
192 /* Handle FIFO read */
193 if (msg->flags & I2C_M_RD) {
194 while ((readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
195 OWL_I2C_FIFOSTAT_RFE) && i2c_dev->msg_ptr < msg->len) {
196 msg->buf[i2c_dev->msg_ptr++] = readl(i2c_dev->base +
197 OWL_I2C_REG_RXDAT);
198 }
199 } else {
200 /* Handle the remaining bytes which were not sent */
201 while (!(readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
202 OWL_I2C_FIFOSTAT_TFF) && i2c_dev->msg_ptr < msg->len) {
203 writel(msg->buf[i2c_dev->msg_ptr++],
204 i2c_dev->base + OWL_I2C_REG_TXDAT);
205 }
206 }
207
208stop:
209 /* Clear pending interrupts */
210 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_STAT,
211 OWL_I2C_STAT_IRQP, true);
212
213 complete_all(&i2c_dev->msg_complete);
214 spin_unlock_irqrestore(&i2c_dev->lock, flags);
215
216 return IRQ_HANDLED;
217}
218
219static u32 owl_i2c_func(struct i2c_adapter *adap)
220{
221 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
222}
223
224static int owl_i2c_check_bus_busy(struct i2c_adapter *adap)
225{
226 struct owl_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
227 unsigned long timeout;
228
229 /* Check for Bus busy */
230 timeout = jiffies + OWL_I2C_TIMEOUT;
231 while (readl(i2c_dev->base + OWL_I2C_REG_STAT) & OWL_I2C_STAT_BBB) {
232 if (time_after(jiffies, timeout)) {
233 dev_err(&adap->dev, "Bus busy timeout\n");
234 return -ETIMEDOUT;
235 }
236 }
237
238 return 0;
239}
240
241static int owl_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
242 int num)
243{
244 struct owl_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
245 struct i2c_msg *msg;
246 unsigned long time_left, flags;
247 unsigned int i2c_cmd, val;
248 unsigned int addr;
249 int ret, idx;
250
251 spin_lock_irqsave(&i2c_dev->lock, flags);
252
253 /* Reset I2C controller */
254 owl_i2c_reset(i2c_dev);
255
256 /* Set bus frequency */
257 owl_i2c_set_freq(i2c_dev);
258
259 /*
260 * Spinlock should be released before calling reset FIFO and
261 * bus busy check since those functions may sleep
262 */
263 spin_unlock_irqrestore(&i2c_dev->lock, flags);
264
265 /* Reset FIFO */
266 ret = owl_i2c_reset_fifo(i2c_dev);
267 if (ret)
268 goto unlocked_err_exit;
269
270 /* Check for bus busy */
271 ret = owl_i2c_check_bus_busy(adap);
272 if (ret)
273 goto unlocked_err_exit;
274
275 spin_lock_irqsave(&i2c_dev->lock, flags);
276
277 /* Check for Arbitration lost */
278 val = readl(i2c_dev->base + OWL_I2C_REG_STAT);
279 if (val & OWL_I2C_STAT_LAB) {
280 val &= ~OWL_I2C_STAT_LAB;
281 writel(val, i2c_dev->base + OWL_I2C_REG_STAT);
282 ret = -EAGAIN;
283 goto err_exit;
284 }
285
286 reinit_completion(&i2c_dev->msg_complete);
287
288 /* Enable I2C controller interrupt */
289 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
290 OWL_I2C_CTL_IRQE, true);
291
292 /*
293 * Select: FIFO enable, Master mode, Stop enable, Data count enable,
294 * Send start bit
295 */
296 i2c_cmd = OWL_I2C_CMD_SECL | OWL_I2C_CMD_MSS | OWL_I2C_CMD_SE |
297 OWL_I2C_CMD_NS | OWL_I2C_CMD_DE | OWL_I2C_CMD_SBE;
298
299 /* Handle repeated start condition */
300 if (num > 1) {
301 /* Set internal address length and enable repeated start */
302 i2c_cmd |= OWL_I2C_CMD_AS(msgs[0].len + 1) |
303 OWL_I2C_CMD_SAS(1) | OWL_I2C_CMD_RBE;
304
305 /* Write slave address */
306 addr = i2c_8bit_addr_from_msg(&msgs[0]);
307 writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT);
308
309 /* Write internal register address */
310 for (idx = 0; idx < msgs[0].len; idx++)
311 writel(msgs[0].buf[idx],
312 i2c_dev->base + OWL_I2C_REG_TXDAT);
313
314 msg = &msgs[1];
315 } else {
316 /* Set address length */
317 i2c_cmd |= OWL_I2C_CMD_AS(1);
318 msg = &msgs[0];
319 }
320
321 i2c_dev->msg = msg;
322 i2c_dev->msg_ptr = 0;
323
324 /* Set data count for the message */
325 writel(msg->len, i2c_dev->base + OWL_I2C_REG_DATCNT);
326
327 addr = i2c_8bit_addr_from_msg(msg);
328 writel(addr, i2c_dev->base + OWL_I2C_REG_TXDAT);
329
330 if (!(msg->flags & I2C_M_RD)) {
331 /* Write data to FIFO */
332 for (idx = 0; idx < msg->len; idx++) {
333 /* Check for FIFO full */
334 if (readl(i2c_dev->base + OWL_I2C_REG_FIFOSTAT) &
335 OWL_I2C_FIFOSTAT_TFF)
336 break;
337
338 writel(msg->buf[idx],
339 i2c_dev->base + OWL_I2C_REG_TXDAT);
340 }
341
342 i2c_dev->msg_ptr = idx;
343 }
344
345 /* Ignore the NACK if needed */
346 if (msg->flags & I2C_M_IGNORE_NAK)
347 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL,
348 OWL_I2C_FIFOCTL_NIB, true);
349 else
350 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_FIFOCTL,
351 OWL_I2C_FIFOCTL_NIB, false);
352
353 /* Start the transfer */
354 writel(i2c_cmd, i2c_dev->base + OWL_I2C_REG_CMD);
355
356 spin_unlock_irqrestore(&i2c_dev->lock, flags);
357
358 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
359 adap->timeout);
360
361 spin_lock_irqsave(&i2c_dev->lock, flags);
362 if (time_left == 0) {
363 dev_err(&adap->dev, "Transaction timed out\n");
364 /* Send stop condition and release the bus */
365 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
366 OWL_I2C_CTL_GBCC_STOP | OWL_I2C_CTL_RB,
367 true);
368 ret = -ETIMEDOUT;
369 goto err_exit;
370 }
371
372 ret = i2c_dev->err < 0 ? i2c_dev->err : num;
373
374err_exit:
375 spin_unlock_irqrestore(&i2c_dev->lock, flags);
376
377unlocked_err_exit:
378 /* Disable I2C controller */
379 owl_i2c_update_reg(i2c_dev->base + OWL_I2C_REG_CTL,
380 OWL_I2C_CTL_EN, false);
381
382 return ret;
383}
384
385static const struct i2c_algorithm owl_i2c_algorithm = {
386 .master_xfer = owl_i2c_master_xfer,
387 .functionality = owl_i2c_func,
388};
389
390static const struct i2c_adapter_quirks owl_i2c_quirks = {
391 .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST,
392 .max_read_len = 240,
393 .max_write_len = 240,
394 .max_comb_1st_msg_len = 6,
395 .max_comb_2nd_msg_len = 240,
396};
397
398static int owl_i2c_probe(struct platform_device *pdev)
399{
400 struct device *dev = &pdev->dev;
401 struct owl_i2c_dev *i2c_dev;
402 struct resource *res;
403 int ret, irq;
404
405 i2c_dev = devm_kzalloc(dev, sizeof(*i2c_dev), GFP_KERNEL);
406 if (!i2c_dev)
407 return -ENOMEM;
408
409 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
410 i2c_dev->base = devm_ioremap_resource(dev, res);
411 if (IS_ERR(i2c_dev->base))
412 return PTR_ERR(i2c_dev->base);
413
414 irq = platform_get_irq(pdev, 0);
415 if (irq < 0) {
416 dev_err(dev, "failed to get IRQ number\n");
417 return irq;
418 }
419
420 if (of_property_read_u32(dev->of_node, "clock-frequency",
421 &i2c_dev->bus_freq))
422 i2c_dev->bus_freq = OWL_I2C_DEF_SPEED_HZ;
423
424 /* We support only frequencies of 100k and 400k for now */
425 if (i2c_dev->bus_freq != OWL_I2C_DEF_SPEED_HZ &&
426 i2c_dev->bus_freq != OWL_I2C_MAX_SPEED_HZ) {
427 dev_err(dev, "invalid clock-frequency %d\n", i2c_dev->bus_freq);
428 return -EINVAL;
429 }
430
431 i2c_dev->clk = devm_clk_get(dev, NULL);
432 if (IS_ERR(i2c_dev->clk)) {
433 dev_err(dev, "failed to get clock\n");
434 return PTR_ERR(i2c_dev->clk);
435 }
436
437 ret = clk_prepare_enable(i2c_dev->clk);
438 if (ret)
439 return ret;
440
441 i2c_dev->clk_rate = clk_get_rate(i2c_dev->clk);
442 if (!i2c_dev->clk_rate) {
443 dev_err(dev, "input clock rate should not be zero\n");
444 ret = -EINVAL;
445 goto disable_clk;
446 }
447
448 init_completion(&i2c_dev->msg_complete);
449 spin_lock_init(&i2c_dev->lock);
450 i2c_dev->adap.owner = THIS_MODULE;
451 i2c_dev->adap.algo = &owl_i2c_algorithm;
452 i2c_dev->adap.timeout = OWL_I2C_TIMEOUT;
453 i2c_dev->adap.quirks = &owl_i2c_quirks;
454 i2c_dev->adap.dev.parent = dev;
455 i2c_dev->adap.dev.of_node = dev->of_node;
456 snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name),
457 "%s", "OWL I2C adapter");
458 i2c_set_adapdata(&i2c_dev->adap, i2c_dev);
459
460 platform_set_drvdata(pdev, i2c_dev);
461
462 ret = devm_request_irq(dev, irq, owl_i2c_interrupt, 0, pdev->name,
463 i2c_dev);
464 if (ret) {
465 dev_err(dev, "failed to request irq %d\n", irq);
466 goto disable_clk;
467 }
468
469 return i2c_add_adapter(&i2c_dev->adap);
470
471disable_clk:
472 clk_disable_unprepare(i2c_dev->clk);
473
474 return ret;
475}
476
477static const struct of_device_id owl_i2c_of_match[] = {
478 { .compatible = "actions,s900-i2c" },
479 { /* sentinel */ }
480};
481MODULE_DEVICE_TABLE(of, owl_i2c_of_match);
482
483static struct platform_driver owl_i2c_driver = {
484 .probe = owl_i2c_probe,
485 .driver = {
486 .name = "owl-i2c",
487 .of_match_table = of_match_ptr(owl_i2c_of_match),
488 },
489};
490module_platform_driver(owl_i2c_driver);
491
492MODULE_AUTHOR("David Liu <liuwei@actions-semi.com>");
493MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
494MODULE_DESCRIPTION("Actions Semiconductor Owl SoC's I2C driver");
495MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/busses/i2c-pasemi.c b/drivers/i2c/busses/i2c-pasemi.c
index 55fd5c6f3cca..50803e5d995b 100644
--- a/drivers/i2c/busses/i2c-pasemi.c
+++ b/drivers/i2c/busses/i2c-pasemi.c
@@ -365,7 +365,6 @@ static int pasemi_smb_probe(struct pci_dev *dev,
365 smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD; 365 smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
366 smbus->adapter.algo = &smbus_algorithm; 366 smbus->adapter.algo = &smbus_algorithm;
367 smbus->adapter.algo_data = smbus; 367 smbus->adapter.algo_data = smbus;
368 smbus->adapter.nr = PCI_FUNC(dev->devfn);
369 368
370 /* set up the sysfs linkage to our parent device */ 369 /* set up the sysfs linkage to our parent device */
371 smbus->adapter.dev.parent = &dev->dev; 370 smbus->adapter.dev.parent = &dev->dev;
@@ -373,7 +372,7 @@ static int pasemi_smb_probe(struct pci_dev *dev,
373 reg_write(smbus, REG_CTL, (CTL_MTR | CTL_MRR | 372 reg_write(smbus, REG_CTL, (CTL_MTR | CTL_MRR |
374 (CLK_100K_DIV & CTL_CLK_M))); 373 (CLK_100K_DIV & CTL_CLK_M)));
375 374
376 error = i2c_add_numbered_adapter(&smbus->adapter); 375 error = i2c_add_adapter(&smbus->adapter);
377 if (error) 376 if (error)
378 goto out_release_region; 377 goto out_release_region;
379 378
diff --git a/drivers/i2c/busses/i2c-pmcmsp.c b/drivers/i2c/busses/i2c-pmcmsp.c
index dae8ac618a52..0829cb696d9d 100644
--- a/drivers/i2c/busses/i2c-pmcmsp.c
+++ b/drivers/i2c/busses/i2c-pmcmsp.c
@@ -444,16 +444,6 @@ static enum pmcmsptwi_xfer_result pmcmsptwi_xfer_cmd(
444{ 444{
445 enum pmcmsptwi_xfer_result retval; 445 enum pmcmsptwi_xfer_result retval;
446 446
447 if ((cmd->type == MSP_TWI_CMD_WRITE && cmd->write_len == 0) ||
448 (cmd->type == MSP_TWI_CMD_READ && cmd->read_len == 0) ||
449 (cmd->type == MSP_TWI_CMD_WRITE_READ &&
450 (cmd->read_len == 0 || cmd->write_len == 0))) {
451 dev_err(&pmcmsptwi_adapter.dev,
452 "%s: Cannot transfer less than 1 byte\n",
453 __func__);
454 return -EINVAL;
455 }
456
457 mutex_lock(&data->lock); 447 mutex_lock(&data->lock);
458 dev_dbg(&pmcmsptwi_adapter.dev, 448 dev_dbg(&pmcmsptwi_adapter.dev,
459 "Setting address to 0x%04x\n", cmd->addr); 449 "Setting address to 0x%04x\n", cmd->addr);
@@ -532,11 +522,6 @@ static int pmcmsptwi_master_xfer(struct i2c_adapter *adap,
532 cmd.write_data = msg->buf; 522 cmd.write_data = msg->buf;
533 } 523 }
534 524
535 if (msg->len == 0) {
536 dev_err(&adap->dev, "Zero-byte messages unsupported\n");
537 return -EINVAL;
538 }
539
540 cmd.addr = msg->addr; 525 cmd.addr = msg->addr;
541 526
542 if (msg->flags & I2C_M_TEN) { 527 if (msg->flags & I2C_M_TEN) {
@@ -578,7 +563,7 @@ static u32 pmcmsptwi_i2c_func(struct i2c_adapter *adapter)
578} 563}
579 564
580static const struct i2c_adapter_quirks pmcmsptwi_i2c_quirks = { 565static const struct i2c_adapter_quirks pmcmsptwi_i2c_quirks = {
581 .flags = I2C_AQ_COMB_WRITE_THEN_READ, 566 .flags = I2C_AQ_COMB_WRITE_THEN_READ | I2C_AQ_NO_ZERO_LEN,
582 .max_write_len = MSP_MAX_BYTES_PER_RW, 567 .max_write_len = MSP_MAX_BYTES_PER_RW,
583 .max_read_len = MSP_MAX_BYTES_PER_RW, 568 .max_read_len = MSP_MAX_BYTES_PER_RW,
584 .max_comb_1st_msg_len = MSP_MAX_BYTES_PER_RW, 569 .max_comb_1st_msg_len = MSP_MAX_BYTES_PER_RW,
diff --git a/drivers/i2c/busses/i2c-qcom-geni.c b/drivers/i2c/busses/i2c-qcom-geni.c
new file mode 100644
index 000000000000..36732eb688a4
--- /dev/null
+++ b/drivers/i2c/busses/i2c-qcom-geni.c
@@ -0,0 +1,673 @@
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
3
4#include <linux/clk.h>
5#include <linux/dma-mapping.h>
6#include <linux/err.h>
7#include <linux/i2c.h>
8#include <linux/interrupt.h>
9#include <linux/io.h>
10#include <linux/module.h>
11#include <linux/of.h>
12#include <linux/of_platform.h>
13#include <linux/platform_device.h>
14#include <linux/pm_runtime.h>
15#include <linux/qcom-geni-se.h>
16#include <linux/spinlock.h>
17
18#define SE_I2C_TX_TRANS_LEN 0x26c
19#define SE_I2C_RX_TRANS_LEN 0x270
20#define SE_I2C_SCL_COUNTERS 0x278
21
22#define SE_I2C_ERR (M_CMD_OVERRUN_EN | M_ILLEGAL_CMD_EN | M_CMD_FAILURE_EN |\
23 M_GP_IRQ_1_EN | M_GP_IRQ_3_EN | M_GP_IRQ_4_EN)
24#define SE_I2C_ABORT BIT(1)
25
26/* M_CMD OP codes for I2C */
27#define I2C_WRITE 0x1
28#define I2C_READ 0x2
29#define I2C_WRITE_READ 0x3
30#define I2C_ADDR_ONLY 0x4
31#define I2C_BUS_CLEAR 0x6
32#define I2C_STOP_ON_BUS 0x7
33/* M_CMD params for I2C */
34#define PRE_CMD_DELAY BIT(0)
35#define TIMESTAMP_BEFORE BIT(1)
36#define STOP_STRETCH BIT(2)
37#define TIMESTAMP_AFTER BIT(3)
38#define POST_COMMAND_DELAY BIT(4)
39#define IGNORE_ADD_NACK BIT(6)
40#define READ_FINISHED_WITH_ACK BIT(7)
41#define BYPASS_ADDR_PHASE BIT(8)
42#define SLV_ADDR_MSK GENMASK(15, 9)
43#define SLV_ADDR_SHFT 9
44/* I2C SCL COUNTER fields */
45#define HIGH_COUNTER_MSK GENMASK(29, 20)
46#define HIGH_COUNTER_SHFT 20
47#define LOW_COUNTER_MSK GENMASK(19, 10)
48#define LOW_COUNTER_SHFT 10
49#define CYCLE_COUNTER_MSK GENMASK(9, 0)
50
51enum geni_i2c_err_code {
52 GP_IRQ0,
53 NACK,
54 GP_IRQ2,
55 BUS_PROTO,
56 ARB_LOST,
57 GP_IRQ5,
58 GENI_OVERRUN,
59 GENI_ILLEGAL_CMD,
60 GENI_ABORT_DONE,
61 GENI_TIMEOUT,
62};
63
64#define DM_I2C_CB_ERR ((BIT(NACK) | BIT(BUS_PROTO) | BIT(ARB_LOST)) \
65 << 5)
66
67#define I2C_AUTO_SUSPEND_DELAY 250
68#define KHZ(freq) (1000 * freq)
69#define PACKING_BYTES_PW 4
70
71#define ABORT_TIMEOUT HZ
72#define XFER_TIMEOUT HZ
73#define RST_TIMEOUT HZ
74
75struct geni_i2c_dev {
76 struct geni_se se;
77 u32 tx_wm;
78 int irq;
79 int err;
80 struct i2c_adapter adap;
81 struct completion done;
82 struct i2c_msg *cur;
83 int cur_wr;
84 int cur_rd;
85 spinlock_t lock;
86 u32 clk_freq_out;
87 const struct geni_i2c_clk_fld *clk_fld;
88 int suspended;
89};
90
91struct geni_i2c_err_log {
92 int err;
93 const char *msg;
94};
95
96static const struct geni_i2c_err_log gi2c_log[] = {
97 [GP_IRQ0] = {-EIO, "Unknown I2C err GP_IRQ0"},
98 [NACK] = {-ENXIO, "NACK: slv unresponsive, check its power/reset-ln"},
99 [GP_IRQ2] = {-EIO, "Unknown I2C err GP IRQ2"},
100 [BUS_PROTO] = {-EPROTO, "Bus proto err, noisy/unepxected start/stop"},
101 [ARB_LOST] = {-EAGAIN, "Bus arbitration lost, clock line undriveable"},
102 [GP_IRQ5] = {-EIO, "Unknown I2C err GP IRQ5"},
103 [GENI_OVERRUN] = {-EIO, "Cmd overrun, check GENI cmd-state machine"},
104 [GENI_ILLEGAL_CMD] = {-EIO, "Illegal cmd, check GENI cmd-state machine"},
105 [GENI_ABORT_DONE] = {-ETIMEDOUT, "Abort after timeout successful"},
106 [GENI_TIMEOUT] = {-ETIMEDOUT, "I2C TXN timed out"},
107};
108
109struct geni_i2c_clk_fld {
110 u32 clk_freq_out;
111 u8 clk_div;
112 u8 t_high_cnt;
113 u8 t_low_cnt;
114 u8 t_cycle_cnt;
115};
116
117/*
118 * Hardware uses the underlying formula to calculate time periods of
119 * SCL clock cycle. Firmware uses some additional cycles excluded from the
120 * below formula and it is confirmed that the time periods are within
121 * specification limits.
122 *
123 * time of high period of SCL: t_high = (t_high_cnt * clk_div) / source_clock
124 * time of low period of SCL: t_low = (t_low_cnt * clk_div) / source_clock
125 * time of full period of SCL: t_cycle = (t_cycle_cnt * clk_div) / source_clock
126 * clk_freq_out = t / t_cycle
127 * source_clock = 19.2 MHz
128 */
129static const struct geni_i2c_clk_fld geni_i2c_clk_map[] = {
130 {KHZ(100), 7, 10, 11, 26},
131 {KHZ(400), 2, 5, 12, 24},
132 {KHZ(1000), 1, 3, 9, 18},
133};
134
135static int geni_i2c_clk_map_idx(struct geni_i2c_dev *gi2c)
136{
137 int i;
138 const struct geni_i2c_clk_fld *itr = geni_i2c_clk_map;
139
140 for (i = 0; i < ARRAY_SIZE(geni_i2c_clk_map); i++, itr++) {
141 if (itr->clk_freq_out == gi2c->clk_freq_out) {
142 gi2c->clk_fld = itr;
143 return 0;
144 }
145 }
146 return -EINVAL;
147}
148
149static void qcom_geni_i2c_conf(struct geni_i2c_dev *gi2c)
150{
151 const struct geni_i2c_clk_fld *itr = gi2c->clk_fld;
152 u32 val;
153
154 writel_relaxed(0, gi2c->se.base + SE_GENI_CLK_SEL);
155
156 val = (itr->clk_div << CLK_DIV_SHFT) | SER_CLK_EN;
157 writel_relaxed(val, gi2c->se.base + GENI_SER_M_CLK_CFG);
158
159 val = itr->t_high_cnt << HIGH_COUNTER_SHFT;
160 val |= itr->t_low_cnt << LOW_COUNTER_SHFT;
161 val |= itr->t_cycle_cnt;
162 writel_relaxed(val, gi2c->se.base + SE_I2C_SCL_COUNTERS);
163}
164
165static void geni_i2c_err_misc(struct geni_i2c_dev *gi2c)
166{
167 u32 m_cmd = readl_relaxed(gi2c->se.base + SE_GENI_M_CMD0);
168 u32 m_stat = readl_relaxed(gi2c->se.base + SE_GENI_M_IRQ_STATUS);
169 u32 geni_s = readl_relaxed(gi2c->se.base + SE_GENI_STATUS);
170 u32 geni_ios = readl_relaxed(gi2c->se.base + SE_GENI_IOS);
171 u32 dma = readl_relaxed(gi2c->se.base + SE_GENI_DMA_MODE_EN);
172 u32 rx_st, tx_st;
173
174 if (dma) {
175 rx_st = readl_relaxed(gi2c->se.base + SE_DMA_RX_IRQ_STAT);
176 tx_st = readl_relaxed(gi2c->se.base + SE_DMA_TX_IRQ_STAT);
177 } else {
178 rx_st = readl_relaxed(gi2c->se.base + SE_GENI_RX_FIFO_STATUS);
179 tx_st = readl_relaxed(gi2c->se.base + SE_GENI_TX_FIFO_STATUS);
180 }
181 dev_dbg(gi2c->se.dev, "DMA:%d tx_stat:0x%x, rx_stat:0x%x, irq-stat:0x%x\n",
182 dma, tx_st, rx_st, m_stat);
183 dev_dbg(gi2c->se.dev, "m_cmd:0x%x, geni_status:0x%x, geni_ios:0x%x\n",
184 m_cmd, geni_s, geni_ios);
185}
186
187static void geni_i2c_err(struct geni_i2c_dev *gi2c, int err)
188{
189 if (!gi2c->err)
190 gi2c->err = gi2c_log[err].err;
191 if (gi2c->cur)
192 dev_dbg(gi2c->se.dev, "len:%d, slv-addr:0x%x, RD/WR:%d\n",
193 gi2c->cur->len, gi2c->cur->addr, gi2c->cur->flags);
194
195 if (err != NACK && err != GENI_ABORT_DONE) {
196 dev_err(gi2c->se.dev, "%s\n", gi2c_log[err].msg);
197 geni_i2c_err_misc(gi2c);
198 }
199}
200
201static irqreturn_t geni_i2c_irq(int irq, void *dev)
202{
203 struct geni_i2c_dev *gi2c = dev;
204 int j;
205 u32 m_stat;
206 u32 rx_st;
207 u32 dm_tx_st;
208 u32 dm_rx_st;
209 u32 dma;
210 struct i2c_msg *cur;
211 unsigned long flags;
212
213 spin_lock_irqsave(&gi2c->lock, flags);
214 m_stat = readl_relaxed(gi2c->se.base + SE_GENI_M_IRQ_STATUS);
215 rx_st = readl_relaxed(gi2c->se.base + SE_GENI_RX_FIFO_STATUS);
216 dm_tx_st = readl_relaxed(gi2c->se.base + SE_DMA_TX_IRQ_STAT);
217 dm_rx_st = readl_relaxed(gi2c->se.base + SE_DMA_RX_IRQ_STAT);
218 dma = readl_relaxed(gi2c->se.base + SE_GENI_DMA_MODE_EN);
219 cur = gi2c->cur;
220
221 if (!cur ||
222 m_stat & (M_CMD_FAILURE_EN | M_CMD_ABORT_EN) ||
223 dm_rx_st & (DM_I2C_CB_ERR)) {
224 if (m_stat & M_GP_IRQ_1_EN)
225 geni_i2c_err(gi2c, NACK);
226 if (m_stat & M_GP_IRQ_3_EN)
227 geni_i2c_err(gi2c, BUS_PROTO);
228 if (m_stat & M_GP_IRQ_4_EN)
229 geni_i2c_err(gi2c, ARB_LOST);
230 if (m_stat & M_CMD_OVERRUN_EN)
231 geni_i2c_err(gi2c, GENI_OVERRUN);
232 if (m_stat & M_ILLEGAL_CMD_EN)
233 geni_i2c_err(gi2c, GENI_ILLEGAL_CMD);
234 if (m_stat & M_CMD_ABORT_EN)
235 geni_i2c_err(gi2c, GENI_ABORT_DONE);
236 if (m_stat & M_GP_IRQ_0_EN)
237 geni_i2c_err(gi2c, GP_IRQ0);
238
239 /* Disable the TX Watermark interrupt to stop TX */
240 if (!dma)
241 writel_relaxed(0, gi2c->se.base +
242 SE_GENI_TX_WATERMARK_REG);
243 goto irqret;
244 }
245
246 if (dma) {
247 dev_dbg(gi2c->se.dev, "i2c dma tx:0x%x, dma rx:0x%x\n",
248 dm_tx_st, dm_rx_st);
249 goto irqret;
250 }
251
252 if (cur->flags & I2C_M_RD &&
253 m_stat & (M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN)) {
254 u32 rxcnt = rx_st & RX_FIFO_WC_MSK;
255
256 for (j = 0; j < rxcnt; j++) {
257 u32 val;
258 int p = 0;
259
260 val = readl_relaxed(gi2c->se.base + SE_GENI_RX_FIFOn);
261 while (gi2c->cur_rd < cur->len && p < sizeof(val)) {
262 cur->buf[gi2c->cur_rd++] = val & 0xff;
263 val >>= 8;
264 p++;
265 }
266 if (gi2c->cur_rd == cur->len)
267 break;
268 }
269 } else if (!(cur->flags & I2C_M_RD) &&
270 m_stat & M_TX_FIFO_WATERMARK_EN) {
271 for (j = 0; j < gi2c->tx_wm; j++) {
272 u32 temp;
273 u32 val = 0;
274 int p = 0;
275
276 while (gi2c->cur_wr < cur->len && p < sizeof(val)) {
277 temp = cur->buf[gi2c->cur_wr++];
278 val |= temp << (p * 8);
279 p++;
280 }
281 writel_relaxed(val, gi2c->se.base + SE_GENI_TX_FIFOn);
282 /* TX Complete, Disable the TX Watermark interrupt */
283 if (gi2c->cur_wr == cur->len) {
284 writel_relaxed(0, gi2c->se.base +
285 SE_GENI_TX_WATERMARK_REG);
286 break;
287 }
288 }
289 }
290irqret:
291 if (m_stat)
292 writel_relaxed(m_stat, gi2c->se.base + SE_GENI_M_IRQ_CLEAR);
293
294 if (dma) {
295 if (dm_tx_st)
296 writel_relaxed(dm_tx_st, gi2c->se.base +
297 SE_DMA_TX_IRQ_CLR);
298 if (dm_rx_st)
299 writel_relaxed(dm_rx_st, gi2c->se.base +
300 SE_DMA_RX_IRQ_CLR);
301 }
302 /* if this is err with done-bit not set, handle that through timeout. */
303 if (m_stat & M_CMD_DONE_EN || m_stat & M_CMD_ABORT_EN)
304 complete(&gi2c->done);
305 else if (dm_tx_st & TX_DMA_DONE || dm_tx_st & TX_RESET_DONE)
306 complete(&gi2c->done);
307 else if (dm_rx_st & RX_DMA_DONE || dm_rx_st & RX_RESET_DONE)
308 complete(&gi2c->done);
309
310 spin_unlock_irqrestore(&gi2c->lock, flags);
311 return IRQ_HANDLED;
312}
313
314static void geni_i2c_abort_xfer(struct geni_i2c_dev *gi2c)
315{
316 u32 val;
317 unsigned long time_left = ABORT_TIMEOUT;
318 unsigned long flags;
319
320 spin_lock_irqsave(&gi2c->lock, flags);
321 geni_i2c_err(gi2c, GENI_TIMEOUT);
322 gi2c->cur = NULL;
323 geni_se_abort_m_cmd(&gi2c->se);
324 spin_unlock_irqrestore(&gi2c->lock, flags);
325 do {
326 time_left = wait_for_completion_timeout(&gi2c->done, time_left);
327 val = readl_relaxed(gi2c->se.base + SE_GENI_M_IRQ_STATUS);
328 } while (!(val & M_CMD_ABORT_EN) && time_left);
329
330 if (!(val & M_CMD_ABORT_EN))
331 dev_err(gi2c->se.dev, "Timeout abort_m_cmd\n");
332}
333
334static void geni_i2c_rx_fsm_rst(struct geni_i2c_dev *gi2c)
335{
336 u32 val;
337 unsigned long time_left = RST_TIMEOUT;
338
339 writel_relaxed(1, gi2c->se.base + SE_DMA_RX_FSM_RST);
340 do {
341 time_left = wait_for_completion_timeout(&gi2c->done, time_left);
342 val = readl_relaxed(gi2c->se.base + SE_DMA_RX_IRQ_STAT);
343 } while (!(val & RX_RESET_DONE) && time_left);
344
345 if (!(val & RX_RESET_DONE))
346 dev_err(gi2c->se.dev, "Timeout resetting RX_FSM\n");
347}
348
349static void geni_i2c_tx_fsm_rst(struct geni_i2c_dev *gi2c)
350{
351 u32 val;
352 unsigned long time_left = RST_TIMEOUT;
353
354 writel_relaxed(1, gi2c->se.base + SE_DMA_TX_FSM_RST);
355 do {
356 time_left = wait_for_completion_timeout(&gi2c->done, time_left);
357 val = readl_relaxed(gi2c->se.base + SE_DMA_TX_IRQ_STAT);
358 } while (!(val & TX_RESET_DONE) && time_left);
359
360 if (!(val & TX_RESET_DONE))
361 dev_err(gi2c->se.dev, "Timeout resetting TX_FSM\n");
362}
363
364static int geni_i2c_rx_one_msg(struct geni_i2c_dev *gi2c, struct i2c_msg *msg,
365 u32 m_param)
366{
367 dma_addr_t rx_dma;
368 enum geni_se_xfer_mode mode;
369 unsigned long time_left = XFER_TIMEOUT;
370
371 gi2c->cur = msg;
372 mode = msg->len > 32 ? GENI_SE_DMA : GENI_SE_FIFO;
373 geni_se_select_mode(&gi2c->se, mode);
374 writel_relaxed(msg->len, gi2c->se.base + SE_I2C_RX_TRANS_LEN);
375 geni_se_setup_m_cmd(&gi2c->se, I2C_READ, m_param);
376 if (mode == GENI_SE_DMA) {
377 int ret;
378
379 ret = geni_se_rx_dma_prep(&gi2c->se, msg->buf, msg->len,
380 &rx_dma);
381 if (ret) {
382 mode = GENI_SE_FIFO;
383 geni_se_select_mode(&gi2c->se, mode);
384 }
385 }
386
387 time_left = wait_for_completion_timeout(&gi2c->done, XFER_TIMEOUT);
388 if (!time_left)
389 geni_i2c_abort_xfer(gi2c);
390
391 gi2c->cur_rd = 0;
392 if (mode == GENI_SE_DMA) {
393 if (gi2c->err)
394 geni_i2c_rx_fsm_rst(gi2c);
395 geni_se_rx_dma_unprep(&gi2c->se, rx_dma, msg->len);
396 }
397 return gi2c->err;
398}
399
400static int geni_i2c_tx_one_msg(struct geni_i2c_dev *gi2c, struct i2c_msg *msg,
401 u32 m_param)
402{
403 dma_addr_t tx_dma;
404 enum geni_se_xfer_mode mode;
405 unsigned long time_left;
406
407 gi2c->cur = msg;
408 mode = msg->len > 32 ? GENI_SE_DMA : GENI_SE_FIFO;
409 geni_se_select_mode(&gi2c->se, mode);
410 writel_relaxed(msg->len, gi2c->se.base + SE_I2C_TX_TRANS_LEN);
411 geni_se_setup_m_cmd(&gi2c->se, I2C_WRITE, m_param);
412 if (mode == GENI_SE_DMA) {
413 int ret;
414
415 ret = geni_se_tx_dma_prep(&gi2c->se, msg->buf, msg->len,
416 &tx_dma);
417 if (ret) {
418 mode = GENI_SE_FIFO;
419 geni_se_select_mode(&gi2c->se, mode);
420 }
421 }
422
423 if (mode == GENI_SE_FIFO) /* Get FIFO IRQ */
424 writel_relaxed(1, gi2c->se.base + SE_GENI_TX_WATERMARK_REG);
425
426 time_left = wait_for_completion_timeout(&gi2c->done, XFER_TIMEOUT);
427 if (!time_left)
428 geni_i2c_abort_xfer(gi2c);
429
430 gi2c->cur_wr = 0;
431 if (mode == GENI_SE_DMA) {
432 if (gi2c->err)
433 geni_i2c_tx_fsm_rst(gi2c);
434 geni_se_tx_dma_unprep(&gi2c->se, tx_dma, msg->len);
435 }
436 return gi2c->err;
437}
438
439static int geni_i2c_xfer(struct i2c_adapter *adap,
440 struct i2c_msg msgs[],
441 int num)
442{
443 struct geni_i2c_dev *gi2c = i2c_get_adapdata(adap);
444 int i, ret;
445
446 gi2c->err = 0;
447 reinit_completion(&gi2c->done);
448 ret = pm_runtime_get_sync(gi2c->se.dev);
449 if (ret < 0) {
450 dev_err(gi2c->se.dev, "error turning SE resources:%d\n", ret);
451 pm_runtime_put_noidle(gi2c->se.dev);
452 /* Set device in suspended since resume failed */
453 pm_runtime_set_suspended(gi2c->se.dev);
454 return ret;
455 }
456
457 qcom_geni_i2c_conf(gi2c);
458 for (i = 0; i < num; i++) {
459 u32 m_param = i < (num - 1) ? STOP_STRETCH : 0;
460
461 m_param |= ((msgs[i].addr << SLV_ADDR_SHFT) & SLV_ADDR_MSK);
462
463 if (msgs[i].flags & I2C_M_RD)
464 ret = geni_i2c_rx_one_msg(gi2c, &msgs[i], m_param);
465 else
466 ret = geni_i2c_tx_one_msg(gi2c, &msgs[i], m_param);
467
468 if (ret)
469 break;
470 }
471 if (ret == 0)
472 ret = num;
473
474 pm_runtime_mark_last_busy(gi2c->se.dev);
475 pm_runtime_put_autosuspend(gi2c->se.dev);
476 gi2c->cur = NULL;
477 gi2c->err = 0;
478 return ret;
479}
480
481static u32 geni_i2c_func(struct i2c_adapter *adap)
482{
483 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
484}
485
486static const struct i2c_algorithm geni_i2c_algo = {
487 .master_xfer = geni_i2c_xfer,
488 .functionality = geni_i2c_func,
489};
490
491static int geni_i2c_probe(struct platform_device *pdev)
492{
493 struct geni_i2c_dev *gi2c;
494 struct resource *res;
495 u32 proto, tx_depth;
496 int ret;
497
498 gi2c = devm_kzalloc(&pdev->dev, sizeof(*gi2c), GFP_KERNEL);
499 if (!gi2c)
500 return -ENOMEM;
501
502 gi2c->se.dev = &pdev->dev;
503 gi2c->se.wrapper = dev_get_drvdata(pdev->dev.parent);
504 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
505 gi2c->se.base = devm_ioremap_resource(&pdev->dev, res);
506 if (IS_ERR(gi2c->se.base))
507 return PTR_ERR(gi2c->se.base);
508
509 gi2c->se.clk = devm_clk_get(&pdev->dev, "se");
510 if (IS_ERR(gi2c->se.clk)) {
511 ret = PTR_ERR(gi2c->se.clk);
512 dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
513 return ret;
514 }
515
516 ret = device_property_read_u32(&pdev->dev, "clock-frequency",
517 &gi2c->clk_freq_out);
518 if (ret) {
519 dev_info(&pdev->dev,
520 "Bus frequency not specified, default to 100kHz.\n");
521 gi2c->clk_freq_out = KHZ(100);
522 }
523
524 gi2c->irq = platform_get_irq(pdev, 0);
525 if (gi2c->irq < 0) {
526 dev_err(&pdev->dev, "IRQ error for i2c-geni\n");
527 return gi2c->irq;
528 }
529
530 ret = geni_i2c_clk_map_idx(gi2c);
531 if (ret) {
532 dev_err(&pdev->dev, "Invalid clk frequency %d Hz: %d\n",
533 gi2c->clk_freq_out, ret);
534 return ret;
535 }
536
537 gi2c->adap.algo = &geni_i2c_algo;
538 init_completion(&gi2c->done);
539 spin_lock_init(&gi2c->lock);
540 platform_set_drvdata(pdev, gi2c);
541 ret = devm_request_irq(&pdev->dev, gi2c->irq, geni_i2c_irq,
542 IRQF_TRIGGER_HIGH, "i2c_geni", gi2c);
543 if (ret) {
544 dev_err(&pdev->dev, "Request_irq failed:%d: err:%d\n",
545 gi2c->irq, ret);
546 return ret;
547 }
548 /* Disable the interrupt so that the system can enter low-power mode */
549 disable_irq(gi2c->irq);
550 i2c_set_adapdata(&gi2c->adap, gi2c);
551 gi2c->adap.dev.parent = &pdev->dev;
552 gi2c->adap.dev.of_node = pdev->dev.of_node;
553 strlcpy(gi2c->adap.name, "Geni-I2C", sizeof(gi2c->adap.name));
554
555 ret = geni_se_resources_on(&gi2c->se);
556 if (ret) {
557 dev_err(&pdev->dev, "Error turning on resources %d\n", ret);
558 return ret;
559 }
560 proto = geni_se_read_proto(&gi2c->se);
561 tx_depth = geni_se_get_tx_fifo_depth(&gi2c->se);
562 if (proto != GENI_SE_I2C) {
563 dev_err(&pdev->dev, "Invalid proto %d\n", proto);
564 geni_se_resources_off(&gi2c->se);
565 return -ENXIO;
566 }
567 gi2c->tx_wm = tx_depth - 1;
568 geni_se_init(&gi2c->se, gi2c->tx_wm, tx_depth);
569 geni_se_config_packing(&gi2c->se, BITS_PER_BYTE, PACKING_BYTES_PW,
570 true, true, true);
571 ret = geni_se_resources_off(&gi2c->se);
572 if (ret) {
573 dev_err(&pdev->dev, "Error turning off resources %d\n", ret);
574 return ret;
575 }
576
577 dev_dbg(&pdev->dev, "i2c fifo/se-dma mode. fifo depth:%d\n", tx_depth);
578
579 ret = i2c_add_adapter(&gi2c->adap);
580 if (ret) {
581 dev_err(&pdev->dev, "Error adding i2c adapter %d\n", ret);
582 return ret;
583 }
584
585 gi2c->suspended = 1;
586 pm_runtime_set_suspended(gi2c->se.dev);
587 pm_runtime_set_autosuspend_delay(gi2c->se.dev, I2C_AUTO_SUSPEND_DELAY);
588 pm_runtime_use_autosuspend(gi2c->se.dev);
589 pm_runtime_enable(gi2c->se.dev);
590
591 return 0;
592}
593
594static int geni_i2c_remove(struct platform_device *pdev)
595{
596 struct geni_i2c_dev *gi2c = platform_get_drvdata(pdev);
597
598 pm_runtime_disable(gi2c->se.dev);
599 i2c_del_adapter(&gi2c->adap);
600 return 0;
601}
602
603static int __maybe_unused geni_i2c_runtime_suspend(struct device *dev)
604{
605 int ret;
606 struct geni_i2c_dev *gi2c = dev_get_drvdata(dev);
607
608 disable_irq(gi2c->irq);
609 ret = geni_se_resources_off(&gi2c->se);
610 if (ret) {
611 enable_irq(gi2c->irq);
612 return ret;
613
614 } else {
615 gi2c->suspended = 1;
616 }
617
618 return 0;
619}
620
621static int __maybe_unused geni_i2c_runtime_resume(struct device *dev)
622{
623 int ret;
624 struct geni_i2c_dev *gi2c = dev_get_drvdata(dev);
625
626 ret = geni_se_resources_on(&gi2c->se);
627 if (ret)
628 return ret;
629
630 enable_irq(gi2c->irq);
631 gi2c->suspended = 0;
632 return 0;
633}
634
635static int __maybe_unused geni_i2c_suspend_noirq(struct device *dev)
636{
637 struct geni_i2c_dev *gi2c = dev_get_drvdata(dev);
638
639 if (!gi2c->suspended) {
640 geni_i2c_runtime_suspend(dev);
641 pm_runtime_disable(dev);
642 pm_runtime_set_suspended(dev);
643 pm_runtime_enable(dev);
644 }
645 return 0;
646}
647
648static const struct dev_pm_ops geni_i2c_pm_ops = {
649 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(geni_i2c_suspend_noirq, NULL)
650 SET_RUNTIME_PM_OPS(geni_i2c_runtime_suspend, geni_i2c_runtime_resume,
651 NULL)
652};
653
654static const struct of_device_id geni_i2c_dt_match[] = {
655 { .compatible = "qcom,geni-i2c" },
656 {}
657};
658MODULE_DEVICE_TABLE(of, geni_i2c_dt_match);
659
660static struct platform_driver geni_i2c_driver = {
661 .probe = geni_i2c_probe,
662 .remove = geni_i2c_remove,
663 .driver = {
664 .name = "geni_i2c",
665 .pm = &geni_i2c_pm_ops,
666 .of_match_table = geni_i2c_dt_match,
667 },
668};
669
670module_platform_driver(geni_i2c_driver);
671
672MODULE_DESCRIPTION("I2C Controller Driver for GENI based QUP cores");
673MODULE_LICENSE("GPL v2");
diff --git a/drivers/i2c/busses/i2c-rcar.c b/drivers/i2c/busses/i2c-rcar.c
index 3c1c817f6968..43ad933df0f0 100644
--- a/drivers/i2c/busses/i2c-rcar.c
+++ b/drivers/i2c/busses/i2c-rcar.c
@@ -19,6 +19,7 @@
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details. 20 * GNU General Public License for more details.
21 */ 21 */
22#include <linux/bitops.h>
22#include <linux/clk.h> 23#include <linux/clk.h>
23#include <linux/delay.h> 24#include <linux/delay.h>
24#include <linux/dmaengine.h> 25#include <linux/dmaengine.h>
@@ -112,9 +113,10 @@
112#define ID_ARBLOST (1 << 3) 113#define ID_ARBLOST (1 << 3)
113#define ID_NACK (1 << 4) 114#define ID_NACK (1 << 4)
114/* persistent flags */ 115/* persistent flags */
115#define ID_P_NO_RXDMA (1 << 30) /* HW forbids RXDMA sometimes */ 116#define ID_P_REP_AFTER_RD BIT(29)
116#define ID_P_PM_BLOCKED (1 << 31) 117#define ID_P_NO_RXDMA BIT(30) /* HW forbids RXDMA sometimes */
117#define ID_P_MASK (ID_P_PM_BLOCKED | ID_P_NO_RXDMA) 118#define ID_P_PM_BLOCKED BIT(31)
119#define ID_P_MASK GENMASK(31, 29)
118 120
119enum rcar_i2c_type { 121enum rcar_i2c_type {
120 I2C_RCAR_GEN1, 122 I2C_RCAR_GEN1,
@@ -183,8 +185,6 @@ static void rcar_i2c_set_scl(struct i2c_adapter *adap, int val)
183 rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr); 185 rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr);
184}; 186};
185 187
186/* No get_sda, because the HW only reports its bus free logic, not SDA itself */
187
188static void rcar_i2c_set_sda(struct i2c_adapter *adap, int val) 188static void rcar_i2c_set_sda(struct i2c_adapter *adap, int val)
189{ 189{
190 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); 190 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
@@ -197,10 +197,19 @@ static void rcar_i2c_set_sda(struct i2c_adapter *adap, int val)
197 rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr); 197 rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr);
198}; 198};
199 199
200static int rcar_i2c_get_bus_free(struct i2c_adapter *adap)
201{
202 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
203
204 return !(rcar_i2c_read(priv, ICMCR) & FSDA);
205
206};
207
200static struct i2c_bus_recovery_info rcar_i2c_bri = { 208static struct i2c_bus_recovery_info rcar_i2c_bri = {
201 .get_scl = rcar_i2c_get_scl, 209 .get_scl = rcar_i2c_get_scl,
202 .set_scl = rcar_i2c_set_scl, 210 .set_scl = rcar_i2c_set_scl,
203 .set_sda = rcar_i2c_set_sda, 211 .set_sda = rcar_i2c_set_sda,
212 .get_bus_free = rcar_i2c_get_bus_free,
204 .recover_bus = i2c_generic_scl_recovery, 213 .recover_bus = i2c_generic_scl_recovery,
205}; 214};
206static void rcar_i2c_init(struct rcar_i2c_priv *priv) 215static void rcar_i2c_init(struct rcar_i2c_priv *priv)
@@ -215,7 +224,7 @@ static void rcar_i2c_init(struct rcar_i2c_priv *priv)
215 224
216static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv) 225static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
217{ 226{
218 int i, ret; 227 int i;
219 228
220 for (i = 0; i < LOOP_TIMEOUT; i++) { 229 for (i = 0; i < LOOP_TIMEOUT; i++) {
221 /* make sure that bus is not busy */ 230 /* make sure that bus is not busy */
@@ -226,13 +235,7 @@ static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
226 235
227 /* Waiting did not help, try to recover */ 236 /* Waiting did not help, try to recover */
228 priv->recovery_icmcr = MDBS | OBPC | FSDA | FSCL; 237 priv->recovery_icmcr = MDBS | OBPC | FSDA | FSCL;
229 ret = i2c_recover_bus(&priv->adap); 238 return i2c_recover_bus(&priv->adap);
230
231 /* No failure when recovering, so check bus busy bit again */
232 if (ret == 0)
233 ret = (rcar_i2c_read(priv, ICMCR) & FSDA) ? -EBUSY : 0;
234
235 return ret;
236} 239}
237 240
238static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, struct i2c_timings *t) 241static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, struct i2c_timings *t)
@@ -343,7 +346,10 @@ static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
343 rcar_i2c_write(priv, ICMSR, 0); 346 rcar_i2c_write(priv, ICMSR, 0);
344 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START); 347 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
345 } else { 348 } else {
346 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START); 349 if (priv->flags & ID_P_REP_AFTER_RD)
350 priv->flags &= ~ID_P_REP_AFTER_RD;
351 else
352 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
347 rcar_i2c_write(priv, ICMSR, 0); 353 rcar_i2c_write(priv, ICMSR, 0);
348 } 354 }
349 rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND); 355 rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
@@ -548,15 +554,15 @@ static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
548 priv->pos++; 554 priv->pos++;
549 } 555 }
550 556
551 /* 557 /* If next received data is the _LAST_, go to new phase. */
552 * If next received data is the _LAST_, go to STOP phase. Might be 558 if (priv->pos + 1 == msg->len) {
553 * overwritten by REP START when setting up a new msg. Not elegant 559 if (priv->flags & ID_LAST_MSG) {
554 * but the only stable sequence for REP START I have found so far. 560 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
555 * If you want to change this code, make sure sending one transfer with 561 } else {
556 * four messages (WR-RD-WR-RD) works! 562 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
557 */ 563 priv->flags |= ID_P_REP_AFTER_RD;
558 if (priv->pos + 1 >= msg->len) 564 }
559 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP); 565 }
560 566
561 if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG)) 567 if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
562 rcar_i2c_next_msg(priv); 568 rcar_i2c_next_msg(priv);
@@ -624,9 +630,11 @@ static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
624 struct rcar_i2c_priv *priv = ptr; 630 struct rcar_i2c_priv *priv = ptr;
625 u32 msr, val; 631 u32 msr, val;
626 632
627 /* Clear START or STOP as soon as we can */ 633 /* Clear START or STOP immediately, except for REPSTART after read */
628 val = rcar_i2c_read(priv, ICMCR); 634 if (likely(!(priv->flags & ID_P_REP_AFTER_RD))) {
629 rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA); 635 val = rcar_i2c_read(priv, ICMCR);
636 rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA);
637 }
630 638
631 msr = rcar_i2c_read(priv, ICMSR); 639 msr = rcar_i2c_read(priv, ICMSR);
632 640
@@ -795,14 +803,8 @@ static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
795 if (ret < 0) 803 if (ret < 0)
796 goto out; 804 goto out;
797 805
798 for (i = 0; i < num; i++) { 806 for (i = 0; i < num; i++)
799 /* This HW can't send STOP after address phase */
800 if (msgs[i].len == 0) {
801 ret = -EOPNOTSUPP;
802 goto out;
803 }
804 rcar_i2c_request_dma(priv, msgs + i); 807 rcar_i2c_request_dma(priv, msgs + i);
805 }
806 808
807 /* init first message */ 809 /* init first message */
808 priv->msg = msgs; 810 priv->msg = msgs;
@@ -889,6 +891,10 @@ static const struct i2c_algorithm rcar_i2c_algo = {
889 .unreg_slave = rcar_unreg_slave, 891 .unreg_slave = rcar_unreg_slave,
890}; 892};
891 893
894static const struct i2c_adapter_quirks rcar_i2c_quirks = {
895 .flags = I2C_AQ_NO_ZERO_LEN,
896};
897
892static const struct of_device_id rcar_i2c_dt_ids[] = { 898static const struct of_device_id rcar_i2c_dt_ids[] = {
893 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 }, 899 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
894 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 }, 900 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
@@ -942,6 +948,7 @@ static int rcar_i2c_probe(struct platform_device *pdev)
942 adap->dev.parent = dev; 948 adap->dev.parent = dev;
943 adap->dev.of_node = dev->of_node; 949 adap->dev.of_node = dev->of_node;
944 adap->bus_recovery_info = &rcar_i2c_bri; 950 adap->bus_recovery_info = &rcar_i2c_bri;
951 adap->quirks = &rcar_i2c_quirks;
945 i2c_set_adapdata(adap, priv); 952 i2c_set_adapdata(adap, priv);
946 strlcpy(adap->name, pdev->name, sizeof(adap->name)); 953 strlcpy(adap->name, pdev->name, sizeof(adap->name));
947 954
diff --git a/drivers/i2c/busses/i2c-s3c2410.c b/drivers/i2c/busses/i2c-s3c2410.c
index 9fe2b6951895..2f2e28d60ef5 100644
--- a/drivers/i2c/busses/i2c-s3c2410.c
+++ b/drivers/i2c/busses/i2c-s3c2410.c
@@ -919,9 +919,9 @@ static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
919 919
920 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) || 920 if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
921 (val == CPUFREQ_PRECHANGE && delta_f > 0)) { 921 (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
922 i2c_lock_adapter(&i2c->adap); 922 i2c_lock_bus(&i2c->adap, I2C_LOCK_ROOT_ADAPTER);
923 ret = s3c24xx_i2c_clockrate(i2c, &got); 923 ret = s3c24xx_i2c_clockrate(i2c, &got);
924 i2c_unlock_adapter(&i2c->adap); 924 i2c_unlock_bus(&i2c->adap, I2C_LOCK_ROOT_ADAPTER);
925 925
926 if (ret < 0) 926 if (ret < 0)
927 dev_err(i2c->dev, "cannot find frequency (%d)\n", ret); 927 dev_err(i2c->dev, "cannot find frequency (%d)\n", ret);
diff --git a/drivers/i2c/busses/i2c-sh_mobile.c b/drivers/i2c/busses/i2c-sh_mobile.c
index 5fda4188a9e5..9c7f6f8ceb22 100644
--- a/drivers/i2c/busses/i2c-sh_mobile.c
+++ b/drivers/i2c/busses/i2c-sh_mobile.c
@@ -613,11 +613,6 @@ static void sh_mobile_i2c_xfer_dma(struct sh_mobile_i2c_data *pd)
613static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg, 613static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg,
614 bool do_init) 614 bool do_init)
615{ 615{
616 if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
617 dev_err(pd->dev, "Unsupported zero length i2c read\n");
618 return -EOPNOTSUPP;
619 }
620
621 if (do_init) { 616 if (do_init) {
622 /* Initialize channel registers */ 617 /* Initialize channel registers */
623 iic_wr(pd, ICCR, ICCR_SCP); 618 iic_wr(pd, ICCR, ICCR_SCP);
@@ -758,6 +753,10 @@ static const struct i2c_algorithm sh_mobile_i2c_algorithm = {
758 .master_xfer = sh_mobile_i2c_xfer, 753 .master_xfer = sh_mobile_i2c_xfer,
759}; 754};
760 755
756static const struct i2c_adapter_quirks sh_mobile_i2c_quirks = {
757 .flags = I2C_AQ_NO_ZERO_LEN_READ,
758};
759
761/* 760/*
762 * r8a7740 chip has lasting errata on I2C I/O pad reset. 761 * r8a7740 chip has lasting errata on I2C I/O pad reset.
763 * this is work-around for it. 762 * this is work-around for it.
@@ -925,6 +924,7 @@ static int sh_mobile_i2c_probe(struct platform_device *dev)
925 924
926 adap->owner = THIS_MODULE; 925 adap->owner = THIS_MODULE;
927 adap->algo = &sh_mobile_i2c_algorithm; 926 adap->algo = &sh_mobile_i2c_algorithm;
927 adap->quirks = &sh_mobile_i2c_quirks;
928 adap->dev.parent = &dev->dev; 928 adap->dev.parent = &dev->dev;
929 adap->retries = 5; 929 adap->retries = 5;
930 adap->nr = dev->id; 930 adap->nr = dev->id;
diff --git a/drivers/i2c/busses/i2c-sprd.c b/drivers/i2c/busses/i2c-sprd.c
index 4053259bccb8..a94e724f51dc 100644
--- a/drivers/i2c/busses/i2c-sprd.c
+++ b/drivers/i2c/busses/i2c-sprd.c
@@ -590,9 +590,9 @@ static int __maybe_unused sprd_i2c_suspend_noirq(struct device *pdev)
590{ 590{
591 struct sprd_i2c *i2c_dev = dev_get_drvdata(pdev); 591 struct sprd_i2c *i2c_dev = dev_get_drvdata(pdev);
592 592
593 i2c_lock_adapter(&i2c_dev->adap); 593 i2c_lock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
594 i2c_dev->is_suspended = true; 594 i2c_dev->is_suspended = true;
595 i2c_unlock_adapter(&i2c_dev->adap); 595 i2c_unlock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
596 596
597 return pm_runtime_force_suspend(pdev); 597 return pm_runtime_force_suspend(pdev);
598} 598}
@@ -601,9 +601,9 @@ static int __maybe_unused sprd_i2c_resume_noirq(struct device *pdev)
601{ 601{
602 struct sprd_i2c *i2c_dev = dev_get_drvdata(pdev); 602 struct sprd_i2c *i2c_dev = dev_get_drvdata(pdev);
603 603
604 i2c_lock_adapter(&i2c_dev->adap); 604 i2c_lock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
605 i2c_dev->is_suspended = false; 605 i2c_dev->is_suspended = false;
606 i2c_unlock_adapter(&i2c_dev->adap); 606 i2c_unlock_bus(&i2c_dev->adap, I2C_LOCK_ROOT_ADAPTER);
607 607
608 return pm_runtime_force_resume(pdev); 608 return pm_runtime_force_resume(pdev);
609} 609}
diff --git a/drivers/i2c/busses/i2c-stu300.c b/drivers/i2c/busses/i2c-stu300.c
index fce52bdab2b7..5503fa171df0 100644
--- a/drivers/i2c/busses/i2c-stu300.c
+++ b/drivers/i2c/busses/i2c-stu300.c
@@ -673,12 +673,6 @@ static int stu300_xfer_msg(struct i2c_adapter *adap,
673 msg->addr, msg->len, msg->flags, stop); 673 msg->addr, msg->len, msg->flags, stop);
674 } 674 }
675 675
676 /* Zero-length messages are not supported by this hardware */
677 if (msg->len == 0) {
678 ret = -EINVAL;
679 goto exit_disable;
680 }
681
682 /* 676 /*
683 * For some reason, sending the address sometimes fails when running 677 * For some reason, sending the address sometimes fails when running
684 * on the 13 MHz clock. No interrupt arrives. This is a work around, 678 * on the 13 MHz clock. No interrupt arrives. This is a work around,
@@ -863,6 +857,10 @@ static const struct i2c_algorithm stu300_algo = {
863 .functionality = stu300_func, 857 .functionality = stu300_func,
864}; 858};
865 859
860static const struct i2c_adapter_quirks stu300_quirks = {
861 .flags = I2C_AQ_NO_ZERO_LEN,
862};
863
866static int stu300_probe(struct platform_device *pdev) 864static int stu300_probe(struct platform_device *pdev)
867{ 865{
868 struct stu300_dev *dev; 866 struct stu300_dev *dev;
@@ -920,6 +918,8 @@ static int stu300_probe(struct platform_device *pdev)
920 adap->algo = &stu300_algo; 918 adap->algo = &stu300_algo;
921 adap->dev.parent = &pdev->dev; 919 adap->dev.parent = &pdev->dev;
922 adap->dev.of_node = pdev->dev.of_node; 920 adap->dev.of_node = pdev->dev.of_node;
921 adap->quirks = &stu300_quirks;
922
923 i2c_set_adapdata(adap, dev); 923 i2c_set_adapdata(adap, dev);
924 924
925 /* i2c device drivers may be active on return from add_adapter() */ 925 /* i2c device drivers may be active on return from add_adapter() */
diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
index 797def5319f1..60c8561fbe65 100644
--- a/drivers/i2c/busses/i2c-tegra.c
+++ b/drivers/i2c/busses/i2c-tegra.c
@@ -115,6 +115,18 @@
115 115
116#define I2C_CONFIG_LOAD_TIMEOUT 1000000 116#define I2C_CONFIG_LOAD_TIMEOUT 1000000
117 117
118#define I2C_MST_FIFO_CONTROL 0x0b4
119#define I2C_MST_FIFO_CONTROL_RX_FLUSH BIT(0)
120#define I2C_MST_FIFO_CONTROL_TX_FLUSH BIT(1)
121#define I2C_MST_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 4)
122#define I2C_MST_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 16)
123
124#define I2C_MST_FIFO_STATUS 0x0b8
125#define I2C_MST_FIFO_STATUS_RX_MASK 0xff
126#define I2C_MST_FIFO_STATUS_RX_SHIFT 0
127#define I2C_MST_FIFO_STATUS_TX_MASK 0xff0000
128#define I2C_MST_FIFO_STATUS_TX_SHIFT 16
129
118/* 130/*
119 * msg_end_type: The bus control which need to be send at end of transfer. 131 * msg_end_type: The bus control which need to be send at end of transfer.
120 * @MSG_END_STOP: Send stop pulse at end of transfer. 132 * @MSG_END_STOP: Send stop pulse at end of transfer.
@@ -154,6 +166,7 @@ struct tegra_i2c_hw_feature {
154 u16 clk_divisor_fast_plus_mode; 166 u16 clk_divisor_fast_plus_mode;
155 bool has_multi_master_mode; 167 bool has_multi_master_mode;
156 bool has_slcg_override_reg; 168 bool has_slcg_override_reg;
169 bool has_mst_fifo;
157}; 170};
158 171
159/** 172/**
@@ -266,13 +279,24 @@ static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
266static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev) 279static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
267{ 280{
268 unsigned long timeout = jiffies + HZ; 281 unsigned long timeout = jiffies + HZ;
269 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL); 282 unsigned int offset;
283 u32 mask, val;
284
285 if (i2c_dev->hw->has_mst_fifo) {
286 mask = I2C_MST_FIFO_CONTROL_TX_FLUSH |
287 I2C_MST_FIFO_CONTROL_RX_FLUSH;
288 offset = I2C_MST_FIFO_CONTROL;
289 } else {
290 mask = I2C_FIFO_CONTROL_TX_FLUSH |
291 I2C_FIFO_CONTROL_RX_FLUSH;
292 offset = I2C_FIFO_CONTROL;
293 }
270 294
271 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH; 295 val = i2c_readl(i2c_dev, offset);
272 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL); 296 val |= mask;
297 i2c_writel(i2c_dev, val, offset);
273 298
274 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) & 299 while (i2c_readl(i2c_dev, offset) & mask) {
275 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
276 if (time_after(jiffies, timeout)) { 300 if (time_after(jiffies, timeout)) {
277 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n"); 301 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
278 return -ETIMEDOUT; 302 return -ETIMEDOUT;
@@ -290,9 +314,15 @@ static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
290 size_t buf_remaining = i2c_dev->msg_buf_remaining; 314 size_t buf_remaining = i2c_dev->msg_buf_remaining;
291 int words_to_transfer; 315 int words_to_transfer;
292 316
293 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS); 317 if (i2c_dev->hw->has_mst_fifo) {
294 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >> 318 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
295 I2C_FIFO_STATUS_RX_SHIFT; 319 rx_fifo_avail = (val & I2C_MST_FIFO_STATUS_RX_MASK) >>
320 I2C_MST_FIFO_STATUS_RX_SHIFT;
321 } else {
322 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
323 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
324 I2C_FIFO_STATUS_RX_SHIFT;
325 }
296 326
297 /* Rounds down to not include partial word at the end of buf */ 327 /* Rounds down to not include partial word at the end of buf */
298 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD; 328 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
@@ -321,6 +351,7 @@ static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
321 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0); 351 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
322 i2c_dev->msg_buf_remaining = buf_remaining; 352 i2c_dev->msg_buf_remaining = buf_remaining;
323 i2c_dev->msg_buf = buf; 353 i2c_dev->msg_buf = buf;
354
324 return 0; 355 return 0;
325} 356}
326 357
@@ -332,9 +363,15 @@ static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
332 size_t buf_remaining = i2c_dev->msg_buf_remaining; 363 size_t buf_remaining = i2c_dev->msg_buf_remaining;
333 int words_to_transfer; 364 int words_to_transfer;
334 365
335 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS); 366 if (i2c_dev->hw->has_mst_fifo) {
336 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >> 367 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
337 I2C_FIFO_STATUS_TX_SHIFT; 368 tx_fifo_avail = (val & I2C_MST_FIFO_STATUS_TX_MASK) >>
369 I2C_MST_FIFO_STATUS_TX_SHIFT;
370 } else {
371 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
372 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
373 I2C_FIFO_STATUS_TX_SHIFT;
374 }
338 375
339 /* Rounds down to not include partial word at the end of buf */ 376 /* Rounds down to not include partial word at the end of buf */
340 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD; 377 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
@@ -516,9 +553,15 @@ static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
516 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2); 553 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
517 } 554 }
518 555
519 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT | 556 if (i2c_dev->hw->has_mst_fifo) {
520 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT; 557 val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) |
521 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL); 558 I2C_MST_FIFO_CONTROL_RX_TRIG(1);
559 i2c_writel(i2c_dev, val, I2C_MST_FIFO_CONTROL);
560 } else {
561 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
562 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
563 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
564 }
522 565
523 err = tegra_i2c_flush_fifos(i2c_dev); 566 err = tegra_i2c_flush_fifos(i2c_dev);
524 if (err) 567 if (err)
@@ -802,6 +845,7 @@ static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
802 .has_config_load_reg = false, 845 .has_config_load_reg = false,
803 .has_multi_master_mode = false, 846 .has_multi_master_mode = false,
804 .has_slcg_override_reg = false, 847 .has_slcg_override_reg = false,
848 .has_mst_fifo = false,
805}; 849};
806 850
807static const struct tegra_i2c_hw_feature tegra30_i2c_hw = { 851static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
@@ -814,6 +858,7 @@ static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
814 .has_config_load_reg = false, 858 .has_config_load_reg = false,
815 .has_multi_master_mode = false, 859 .has_multi_master_mode = false,
816 .has_slcg_override_reg = false, 860 .has_slcg_override_reg = false,
861 .has_mst_fifo = false,
817}; 862};
818 863
819static const struct tegra_i2c_hw_feature tegra114_i2c_hw = { 864static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
@@ -826,6 +871,7 @@ static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
826 .has_config_load_reg = false, 871 .has_config_load_reg = false,
827 .has_multi_master_mode = false, 872 .has_multi_master_mode = false,
828 .has_slcg_override_reg = false, 873 .has_slcg_override_reg = false,
874 .has_mst_fifo = false,
829}; 875};
830 876
831static const struct tegra_i2c_hw_feature tegra124_i2c_hw = { 877static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
@@ -838,6 +884,7 @@ static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
838 .has_config_load_reg = true, 884 .has_config_load_reg = true,
839 .has_multi_master_mode = false, 885 .has_multi_master_mode = false,
840 .has_slcg_override_reg = true, 886 .has_slcg_override_reg = true,
887 .has_mst_fifo = false,
841}; 888};
842 889
843static const struct tegra_i2c_hw_feature tegra210_i2c_hw = { 890static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
@@ -850,10 +897,25 @@ static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
850 .has_config_load_reg = true, 897 .has_config_load_reg = true,
851 .has_multi_master_mode = true, 898 .has_multi_master_mode = true,
852 .has_slcg_override_reg = true, 899 .has_slcg_override_reg = true,
900 .has_mst_fifo = false,
901};
902
903static const struct tegra_i2c_hw_feature tegra194_i2c_hw = {
904 .has_continue_xfer_support = true,
905 .has_per_pkt_xfer_complete_irq = true,
906 .has_single_clk_source = true,
907 .clk_divisor_hs_mode = 1,
908 .clk_divisor_std_fast_mode = 0x19,
909 .clk_divisor_fast_plus_mode = 0x10,
910 .has_config_load_reg = true,
911 .has_multi_master_mode = true,
912 .has_slcg_override_reg = true,
913 .has_mst_fifo = true,
853}; 914};
854 915
855/* Match table for of_platform binding */ 916/* Match table for of_platform binding */
856static const struct of_device_id tegra_i2c_of_match[] = { 917static const struct of_device_id tegra_i2c_of_match[] = {
918 { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
857 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, }, 919 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
858 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, }, 920 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
859 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, }, 921 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
diff --git a/drivers/i2c/busses/i2c-xlr.c b/drivers/i2c/busses/i2c-xlr.c
index 484bfa15d58e..34cd4b308540 100644
--- a/drivers/i2c/busses/i2c-xlr.c
+++ b/drivers/i2c/busses/i2c-xlr.c
@@ -173,9 +173,6 @@ static int xlr_i2c_tx(struct xlr_i2c_private *priv, u16 len,
173 u8 offset; 173 u8 offset;
174 u32 xfer; 174 u32 xfer;
175 175
176 if (!len)
177 return -EOPNOTSUPP;
178
179 offset = buf[0]; 176 offset = buf[0];
180 xlr_i2c_wreg(priv->iobase, XLR_I2C_ADDR, offset); 177 xlr_i2c_wreg(priv->iobase, XLR_I2C_ADDR, offset);
181 xlr_i2c_wreg(priv->iobase, XLR_I2C_DEVADDR, addr); 178 xlr_i2c_wreg(priv->iobase, XLR_I2C_DEVADDR, addr);
@@ -241,9 +238,6 @@ static int xlr_i2c_rx(struct xlr_i2c_private *priv, u16 len, u8 *buf, u16 addr)
241 unsigned long timeout, stoptime, checktime; 238 unsigned long timeout, stoptime, checktime;
242 int nbytes, timedout; 239 int nbytes, timedout;
243 240
244 if (!len)
245 return -EOPNOTSUPP;
246
247 xlr_i2c_wreg(priv->iobase, XLR_I2C_CFG, 241 xlr_i2c_wreg(priv->iobase, XLR_I2C_CFG,
248 XLR_I2C_CFG_NOADDR | priv->cfg->cfg_extra); 242 XLR_I2C_CFG_NOADDR | priv->cfg->cfg_extra);
249 xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len - 1); 243 xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len - 1);
@@ -340,6 +334,10 @@ static const struct i2c_algorithm xlr_i2c_algo = {
340 .functionality = xlr_func, 334 .functionality = xlr_func,
341}; 335};
342 336
337static const struct i2c_adapter_quirks xlr_i2c_quirks = {
338 .flags = I2C_AQ_NO_ZERO_LEN,
339};
340
343static const struct xlr_i2c_config xlr_i2c_config_default = { 341static const struct xlr_i2c_config xlr_i2c_config_default = {
344 .status_busy = XLR_I2C_BUS_BUSY, 342 .status_busy = XLR_I2C_BUS_BUSY,
345 .cfg_extra = 0, 343 .cfg_extra = 0,
@@ -427,6 +425,7 @@ static int xlr_i2c_probe(struct platform_device *pdev)
427 priv->adap.owner = THIS_MODULE; 425 priv->adap.owner = THIS_MODULE;
428 priv->adap.algo_data = priv; 426 priv->adap.algo_data = priv;
429 priv->adap.algo = &xlr_i2c_algo; 427 priv->adap.algo = &xlr_i2c_algo;
428 priv->adap.quirks = &xlr_i2c_quirks;
430 priv->adap.nr = pdev->id; 429 priv->adap.nr = pdev->id;
431 priv->adap.class = I2C_CLASS_HWMON; 430 priv->adap.class = I2C_CLASS_HWMON;
432 snprintf(priv->adap.name, sizeof(priv->adap.name), "xlr-i2c"); 431 snprintf(priv->adap.name, sizeof(priv->adap.name), "xlr-i2c");
diff --git a/drivers/i2c/i2c-core-acpi.c b/drivers/i2c/i2c-core-acpi.c
index 7c3b4740b94b..32affd3fa8bd 100644
--- a/drivers/i2c/i2c-core-acpi.c
+++ b/drivers/i2c/i2c-core-acpi.c
@@ -453,8 +453,12 @@ static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
453 else 453 else
454 dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n", 454 dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
455 data_len, client->addr, cmd, ret); 455 data_len, client->addr, cmd, ret);
456 } else { 456 /* 2 transfers must have completed successfully */
457 } else if (ret == 2) {
457 memcpy(data, buffer, data_len); 458 memcpy(data, buffer, data_len);
459 ret = 0;
460 } else {
461 ret = -EIO;
458 } 462 }
459 463
460 kfree(buffer); 464 kfree(buffer);
@@ -482,11 +486,16 @@ static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
482 msgs[0].buf = buffer; 486 msgs[0].buf = buffer;
483 487
484 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 488 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
485 if (ret < 0)
486 dev_err(&client->adapter->dev, "i2c write failed\n");
487 489
488 kfree(buffer); 490 kfree(buffer);
489 return ret; 491
492 if (ret < 0) {
493 dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
494 return ret;
495 }
496
497 /* 1 transfer must have completed successfully */
498 return (ret == 1) ? 0 : -EIO;
490} 499}
491 500
492static acpi_status 501static acpi_status
@@ -590,8 +599,6 @@ i2c_acpi_space_handler(u32 function, acpi_physical_address command,
590 if (action == ACPI_READ) { 599 if (action == ACPI_READ) {
591 status = acpi_gsb_i2c_read_bytes(client, command, 600 status = acpi_gsb_i2c_read_bytes(client, command,
592 gsb->data, info->access_length); 601 gsb->data, info->access_length);
593 if (status > 0)
594 status = 0;
595 } else { 602 } else {
596 status = acpi_gsb_i2c_write_bytes(client, command, 603 status = acpi_gsb_i2c_write_bytes(client, command,
597 gsb->data, info->access_length); 604 gsb->data, info->access_length);
diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 15c95aaa484c..5a937109a289 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -158,6 +158,22 @@ static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
158 gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val); 158 gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val);
159} 159}
160 160
161static int i2c_generic_bus_free(struct i2c_adapter *adap)
162{
163 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
164 int ret = -EOPNOTSUPP;
165
166 if (bri->get_bus_free)
167 ret = bri->get_bus_free(adap);
168 else if (bri->get_sda)
169 ret = bri->get_sda(adap);
170
171 if (ret < 0)
172 return ret;
173
174 return ret ? 0 : -EBUSY;
175}
176
161/* 177/*
162 * We are generating clock pulses. ndelay() determines durating of clk pulses. 178 * We are generating clock pulses. ndelay() determines durating of clk pulses.
163 * We will generate clock with rate 100 KHz and so duration of both clock levels 179 * We will generate clock with rate 100 KHz and so duration of both clock levels
@@ -169,21 +185,28 @@ static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
169int i2c_generic_scl_recovery(struct i2c_adapter *adap) 185int i2c_generic_scl_recovery(struct i2c_adapter *adap)
170{ 186{
171 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 187 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
172 int i = 0, val = 1, ret = 0; 188 int i = 0, scl = 1, ret;
173 189
174 if (bri->prepare_recovery) 190 if (bri->prepare_recovery)
175 bri->prepare_recovery(adap); 191 bri->prepare_recovery(adap);
176 192
177 bri->set_scl(adap, val); 193 /*
194 * If we can set SDA, we will always create a STOP to ensure additional
195 * pulses will do no harm. This is achieved by letting SDA follow SCL
196 * half a cycle later. Check the 'incomplete_write_byte' fault injector
197 * for details.
198 */
199 bri->set_scl(adap, scl);
200 ndelay(RECOVERY_NDELAY / 2);
178 if (bri->set_sda) 201 if (bri->set_sda)
179 bri->set_sda(adap, 1); 202 bri->set_sda(adap, scl);
180 ndelay(RECOVERY_NDELAY); 203 ndelay(RECOVERY_NDELAY / 2);
181 204
182 /* 205 /*
183 * By this time SCL is high, as we need to give 9 falling-rising edges 206 * By this time SCL is high, as we need to give 9 falling-rising edges
184 */ 207 */
185 while (i++ < RECOVERY_CLK_CNT * 2) { 208 while (i++ < RECOVERY_CLK_CNT * 2) {
186 if (val) { 209 if (scl) {
187 /* SCL shouldn't be low here */ 210 /* SCL shouldn't be low here */
188 if (!bri->get_scl(adap)) { 211 if (!bri->get_scl(adap)) {
189 dev_err(&adap->dev, 212 dev_err(&adap->dev,
@@ -191,41 +214,27 @@ int i2c_generic_scl_recovery(struct i2c_adapter *adap)
191 ret = -EBUSY; 214 ret = -EBUSY;
192 break; 215 break;
193 } 216 }
194 /* Break if SDA is high */
195 if (bri->get_sda && bri->get_sda(adap))
196 break;
197 } 217 }
198 218
199 val = !val; 219 scl = !scl;
200 bri->set_scl(adap, val); 220 bri->set_scl(adap, scl);
201 221 /* Creating STOP again, see above */
202 /*
203 * If we can set SDA, we will always create STOP here to ensure
204 * the additional pulses will do no harm. This is achieved by
205 * letting SDA follow SCL half a cycle later.
206 */
207 ndelay(RECOVERY_NDELAY / 2); 222 ndelay(RECOVERY_NDELAY / 2);
208 if (bri->set_sda) 223 if (bri->set_sda)
209 bri->set_sda(adap, val); 224 bri->set_sda(adap, scl);
210 ndelay(RECOVERY_NDELAY / 2); 225 ndelay(RECOVERY_NDELAY / 2);
211 }
212 226
213 /* check if recovery actually succeeded */ 227 if (scl) {
214 if (bri->get_sda && !bri->get_sda(adap)) 228 ret = i2c_generic_bus_free(adap);
215 ret = -EBUSY; 229 if (ret == 0)
216 230 break;
217 /* If all went well, send STOP for a sane bus state. */ 231 }
218 if (ret == 0 && bri->set_sda) {
219 bri->set_scl(adap, 0);
220 ndelay(RECOVERY_NDELAY / 2);
221 bri->set_sda(adap, 0);
222 ndelay(RECOVERY_NDELAY / 2);
223 bri->set_scl(adap, 1);
224 ndelay(RECOVERY_NDELAY / 2);
225 bri->set_sda(adap, 1);
226 ndelay(RECOVERY_NDELAY / 2);
227 } 232 }
228 233
234 /* If we can't check bus status, assume recovery worked */
235 if (ret == -EOPNOTSUPP)
236 ret = 0;
237
229 if (bri->unprepare_recovery) 238 if (bri->unprepare_recovery)
230 bri->unprepare_recovery(adap); 239 bri->unprepare_recovery(adap);
231 240
@@ -274,6 +283,10 @@ static void i2c_init_recovery(struct i2c_adapter *adap)
274 err_str = "no {get|set}_scl() found"; 283 err_str = "no {get|set}_scl() found";
275 goto err; 284 goto err;
276 } 285 }
286 if (!bri->set_sda && !bri->get_sda) {
287 err_str = "either get_sda() or set_sda() needed";
288 goto err;
289 }
277 } 290 }
278 291
279 return; 292 return;
@@ -1563,6 +1576,8 @@ void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_de
1563 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns); 1576 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1564 if (ret && use_defaults) 1577 if (ret && use_defaults)
1565 t->sda_fall_ns = t->scl_fall_ns; 1578 t->sda_fall_ns = t->scl_fall_ns;
1579
1580 device_property_read_u32(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns);
1566} 1581}
1567EXPORT_SYMBOL_GPL(i2c_parse_fw_timings); 1582EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1568 1583
@@ -1826,9 +1841,15 @@ static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs,
1826 if (msgs[i].flags & I2C_M_RD) { 1841 if (msgs[i].flags & I2C_M_RD) {
1827 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len)) 1842 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1828 return i2c_quirk_error(adap, &msgs[i], "msg too long"); 1843 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1844
1845 if (q->flags & I2C_AQ_NO_ZERO_LEN_READ && len == 0)
1846 return i2c_quirk_error(adap, &msgs[i], "no zero length");
1829 } else { 1847 } else {
1830 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len)) 1848 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1831 return i2c_quirk_error(adap, &msgs[i], "msg too long"); 1849 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1850
1851 if (q->flags & I2C_AQ_NO_ZERO_LEN_WRITE && len == 0)
1852 return i2c_quirk_error(adap, &msgs[i], "no zero length");
1832 } 1853 }
1833 } 1854 }
1834 1855
diff --git a/drivers/i2c/i2c-core-slave.c b/drivers/i2c/i2c-core-slave.c
index 4a78c65e9971..47a9f70a24a9 100644
--- a/drivers/i2c/i2c-core-slave.c
+++ b/drivers/i2c/i2c-core-slave.c
@@ -47,9 +47,9 @@ int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
47 47
48 client->slave_cb = slave_cb; 48 client->slave_cb = slave_cb;
49 49
50 i2c_lock_adapter(client->adapter); 50 i2c_lock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
51 ret = client->adapter->algo->reg_slave(client); 51 ret = client->adapter->algo->reg_slave(client);
52 i2c_unlock_adapter(client->adapter); 52 i2c_unlock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
53 53
54 if (ret) { 54 if (ret) {
55 client->slave_cb = NULL; 55 client->slave_cb = NULL;
@@ -69,9 +69,9 @@ int i2c_slave_unregister(struct i2c_client *client)
69 return -EOPNOTSUPP; 69 return -EOPNOTSUPP;
70 } 70 }
71 71
72 i2c_lock_adapter(client->adapter); 72 i2c_lock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
73 ret = client->adapter->algo->unreg_slave(client); 73 ret = client->adapter->algo->unreg_slave(client);
74 i2c_unlock_adapter(client->adapter); 74 i2c_unlock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
75 75
76 if (ret == 0) 76 if (ret == 0)
77 client->slave_cb = NULL; 77 client->slave_cb = NULL;
diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c
index 29646aa6132e..f330690b4125 100644
--- a/drivers/i2c/i2c-mux.c
+++ b/drivers/i2c/i2c-mux.c
@@ -87,8 +87,8 @@ static int __i2c_mux_smbus_xfer(struct i2c_adapter *adap,
87 87
88 ret = muxc->select(muxc, priv->chan_id); 88 ret = muxc->select(muxc, priv->chan_id);
89 if (ret >= 0) 89 if (ret >= 0)
90 ret = parent->algo->smbus_xfer(parent, addr, flags, 90 ret = __i2c_smbus_xfer(parent, addr, flags,
91 read_write, command, size, data); 91 read_write, command, size, data);
92 if (muxc->deselect) 92 if (muxc->deselect)
93 muxc->deselect(muxc, priv->chan_id); 93 muxc->deselect(muxc, priv->chan_id);
94 94
diff --git a/drivers/i2c/muxes/i2c-mux-mlxcpld.c b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
index 12ad8d65faf6..f2bf3e57ed67 100644
--- a/drivers/i2c/muxes/i2c-mux-mlxcpld.c
+++ b/drivers/i2c/muxes/i2c-mux-mlxcpld.c
@@ -94,31 +94,11 @@ static int mlxcpld_mux_reg_write(struct i2c_adapter *adap,
94 struct i2c_client *client, u8 val) 94 struct i2c_client *client, u8 val)
95{ 95{
96 struct mlxcpld_mux_plat_data *pdata = dev_get_platdata(&client->dev); 96 struct mlxcpld_mux_plat_data *pdata = dev_get_platdata(&client->dev);
97 int ret = -ENODEV; 97 union i2c_smbus_data data = { .byte = val };
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 ret = __i2c_transfer(adap, &msg, 1);
108
109 if (ret >= 0 && ret != 1)
110 ret = -EREMOTEIO;
111 } else if (adap->algo->smbus_xfer) {
112 union i2c_smbus_data data;
113
114 data.byte = val;
115 ret = adap->algo->smbus_xfer(adap, client->addr,
116 client->flags, I2C_SMBUS_WRITE,
117 pdata->sel_reg_addr,
118 I2C_SMBUS_BYTE_DATA, &data);
119 }
120 98
121 return ret; 99 return __i2c_smbus_xfer(adap, client->addr, client->flags,
100 I2C_SMBUS_WRITE, pdata->sel_reg_addr,
101 I2C_SMBUS_BYTE_DATA, &data);
122} 102}
123 103
124static int mlxcpld_mux_select_chan(struct i2c_mux_core *muxc, u32 chan) 104static int mlxcpld_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
diff --git a/drivers/i2c/muxes/i2c-mux-pca9541.c b/drivers/i2c/muxes/i2c-mux-pca9541.c
index 6a39adaf433f..9e75d6b9140b 100644
--- a/drivers/i2c/muxes/i2c-mux-pca9541.c
+++ b/drivers/i2c/muxes/i2c-mux-pca9541.c
@@ -99,31 +99,11 @@ MODULE_DEVICE_TABLE(of, pca9541_of_match);
99static int pca9541_reg_write(struct i2c_client *client, u8 command, u8 val) 99static int pca9541_reg_write(struct i2c_client *client, u8 command, u8 val)
100{ 100{
101 struct i2c_adapter *adap = client->adapter; 101 struct i2c_adapter *adap = client->adapter;
102 int ret; 102 union i2c_smbus_data data = { .byte = val };
103
104 if (adap->algo->master_xfer) {
105 struct i2c_msg msg;
106 char buf[2];
107
108 msg.addr = client->addr;
109 msg.flags = 0;
110 msg.len = 2;
111 buf[0] = command;
112 buf[1] = val;
113 msg.buf = buf;
114 ret = __i2c_transfer(adap, &msg, 1);
115 } else {
116 union i2c_smbus_data data;
117
118 data.byte = val;
119 ret = adap->algo->smbus_xfer(adap, client->addr,
120 client->flags,
121 I2C_SMBUS_WRITE,
122 command,
123 I2C_SMBUS_BYTE_DATA, &data);
124 }
125 103
126 return ret; 104 return __i2c_smbus_xfer(adap, client->addr, client->flags,
105 I2C_SMBUS_WRITE, command,
106 I2C_SMBUS_BYTE_DATA, &data);
127} 107}
128 108
129/* 109/*
@@ -133,41 +113,14 @@ static int pca9541_reg_write(struct i2c_client *client, u8 command, u8 val)
133static int pca9541_reg_read(struct i2c_client *client, u8 command) 113static int pca9541_reg_read(struct i2c_client *client, u8 command)
134{ 114{
135 struct i2c_adapter *adap = client->adapter; 115 struct i2c_adapter *adap = client->adapter;
116 union i2c_smbus_data data;
136 int ret; 117 int ret;
137 u8 val; 118
138 119 ret = __i2c_smbus_xfer(adap, client->addr, client->flags,
139 if (adap->algo->master_xfer) { 120 I2C_SMBUS_READ, command,
140 struct i2c_msg msg[2] = { 121 I2C_SMBUS_BYTE_DATA, &data);
141 { 122
142 .addr = client->addr, 123 return ret ?: data.byte;
143 .flags = 0,
144 .len = 1,
145 .buf = &command
146 },
147 {
148 .addr = client->addr,
149 .flags = I2C_M_RD,
150 .len = 1,
151 .buf = &val
152 }
153 };
154 ret = __i2c_transfer(adap, msg, 2);
155 if (ret == 2)
156 ret = val;
157 else if (ret >= 0)
158 ret = -EIO;
159 } else {
160 union i2c_smbus_data data;
161
162 ret = adap->algo->smbus_xfer(adap, client->addr,
163 client->flags,
164 I2C_SMBUS_READ,
165 command,
166 I2C_SMBUS_BYTE_DATA, &data);
167 if (!ret)
168 ret = data.byte;
169 }
170 return ret;
171} 124}
172 125
173/* 126/*
@@ -345,11 +298,11 @@ static int pca9541_probe(struct i2c_client *client,
345 298
346 /* 299 /*
347 * I2C accesses are unprotected here. 300 * I2C accesses are unprotected here.
348 * We have to lock the adapter before releasing the bus. 301 * We have to lock the I2C segment before releasing the bus.
349 */ 302 */
350 i2c_lock_adapter(adap); 303 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
351 pca9541_release_bus(client); 304 pca9541_release_bus(client);
352 i2c_unlock_adapter(adap); 305 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
353 306
354 /* Create mux adapter */ 307 /* Create mux adapter */
355 308
diff --git a/drivers/i2c/muxes/i2c-mux-pca954x.c b/drivers/i2c/muxes/i2c-mux-pca954x.c
index fbc748027087..24bd9275fde5 100644
--- a/drivers/i2c/muxes/i2c-mux-pca954x.c
+++ b/drivers/i2c/muxes/i2c-mux-pca954x.c
@@ -220,30 +220,11 @@ MODULE_DEVICE_TABLE(of, pca954x_of_match);
220static int pca954x_reg_write(struct i2c_adapter *adap, 220static int pca954x_reg_write(struct i2c_adapter *adap,
221 struct i2c_client *client, u8 val) 221 struct i2c_client *client, u8 val)
222{ 222{
223 int ret = -ENODEV; 223 union i2c_smbus_data dummy;
224
225 if (adap->algo->master_xfer) {
226 struct i2c_msg msg;
227 char buf[1];
228
229 msg.addr = client->addr;
230 msg.flags = 0;
231 msg.len = 1;
232 buf[0] = val;
233 msg.buf = buf;
234 ret = __i2c_transfer(adap, &msg, 1);
235
236 if (ret >= 0 && ret != 1)
237 ret = -EREMOTEIO;
238 } else {
239 union i2c_smbus_data data;
240 ret = adap->algo->smbus_xfer(adap, client->addr,
241 client->flags,
242 I2C_SMBUS_WRITE,
243 val, I2C_SMBUS_BYTE, &data);
244 }
245 224
246 return ret; 225 return __i2c_smbus_xfer(adap, client->addr, client->flags,
226 I2C_SMBUS_WRITE, val,
227 I2C_SMBUS_BYTE, &dummy);
247} 228}
248 229
249static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan) 230static int pca954x_select_chan(struct i2c_mux_core *muxc, u32 chan)
@@ -368,7 +349,8 @@ static int pca954x_probe(struct i2c_client *client,
368{ 349{
369 struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent); 350 struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
370 struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev); 351 struct pca954x_platform_data *pdata = dev_get_platdata(&client->dev);
371 struct device_node *of_node = client->dev.of_node; 352 struct device *dev = &client->dev;
353 struct device_node *np = dev->of_node;
372 bool idle_disconnect_dt; 354 bool idle_disconnect_dt;
373 struct gpio_desc *gpio; 355 struct gpio_desc *gpio;
374 int num, force, class; 356 int num, force, class;
@@ -379,8 +361,7 @@ static int pca954x_probe(struct i2c_client *client,
379 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE)) 361 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
380 return -ENODEV; 362 return -ENODEV;
381 363
382 muxc = i2c_mux_alloc(adap, &client->dev, 364 muxc = i2c_mux_alloc(adap, dev, PCA954X_MAX_NCHANS, sizeof(*data), 0,
383 PCA954X_MAX_NCHANS, sizeof(*data), 0,
384 pca954x_select_chan, pca954x_deselect_mux); 365 pca954x_select_chan, pca954x_deselect_mux);
385 if (!muxc) 366 if (!muxc)
386 return -ENOMEM; 367 return -ENOMEM;
@@ -390,7 +371,7 @@ static int pca954x_probe(struct i2c_client *client,
390 data->client = client; 371 data->client = client;
391 372
392 /* Reset the mux if a reset GPIO is specified. */ 373 /* Reset the mux if a reset GPIO is specified. */
393 gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH); 374 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
394 if (IS_ERR(gpio)) 375 if (IS_ERR(gpio))
395 return PTR_ERR(gpio); 376 return PTR_ERR(gpio);
396 if (gpio) { 377 if (gpio) {
@@ -400,7 +381,7 @@ static int pca954x_probe(struct i2c_client *client,
400 udelay(1); 381 udelay(1);
401 } 382 }
402 383
403 data->chip = of_device_get_match_data(&client->dev); 384 data->chip = of_device_get_match_data(dev);
404 if (!data->chip) 385 if (!data->chip)
405 data->chip = &chips[id->driver_data]; 386 data->chip = &chips[id->driver_data];
406 387
@@ -414,8 +395,7 @@ static int pca954x_probe(struct i2c_client *client,
414 if (!ret && 395 if (!ret &&
415 (id.manufacturer_id != data->chip->id.manufacturer_id || 396 (id.manufacturer_id != data->chip->id.manufacturer_id ||
416 id.part_id != data->chip->id.part_id)) { 397 id.part_id != data->chip->id.part_id)) {
417 dev_warn(&client->dev, 398 dev_warn(dev, "unexpected device id %03x-%03x-%x\n",
418 "unexpected device id %03x-%03x-%x\n",
419 id.manufacturer_id, id.part_id, 399 id.manufacturer_id, id.part_id,
420 id.die_revision); 400 id.die_revision);
421 return -ENODEV; 401 return -ENODEV;
@@ -427,14 +407,14 @@ static int pca954x_probe(struct i2c_client *client,
427 * initializes the mux to disconnected state. 407 * initializes the mux to disconnected state.
428 */ 408 */
429 if (i2c_smbus_write_byte(client, 0) < 0) { 409 if (i2c_smbus_write_byte(client, 0) < 0) {
430 dev_warn(&client->dev, "probe failed\n"); 410 dev_warn(dev, "probe failed\n");
431 return -ENODEV; 411 return -ENODEV;
432 } 412 }
433 413
434 data->last_chan = 0; /* force the first selection */ 414 data->last_chan = 0; /* force the first selection */
435 415
436 idle_disconnect_dt = of_node && 416 idle_disconnect_dt = np &&
437 of_property_read_bool(of_node, "i2c-mux-idle-disconnect"); 417 of_property_read_bool(np, "i2c-mux-idle-disconnect");
438 418
439 ret = pca954x_irq_setup(muxc); 419 ret = pca954x_irq_setup(muxc);
440 if (ret) 420 if (ret)
@@ -465,7 +445,7 @@ static int pca954x_probe(struct i2c_client *client,
465 } 445 }
466 446
467 if (data->irq) { 447 if (data->irq) {
468 ret = devm_request_threaded_irq(&client->dev, data->client->irq, 448 ret = devm_request_threaded_irq(dev, data->client->irq,
469 NULL, pca954x_irq_handler, 449 NULL, pca954x_irq_handler,
470 IRQF_ONESHOT | IRQF_SHARED, 450 IRQF_ONESHOT | IRQF_SHARED,
471 "pca954x", data); 451 "pca954x", data);
@@ -473,8 +453,7 @@ static int pca954x_probe(struct i2c_client *client,
473 goto fail_cleanup; 453 goto fail_cleanup;
474 } 454 }
475 455
476 dev_info(&client->dev, 456 dev_info(dev, "registered %d multiplexed busses for I2C %s %s\n",
477 "registered %d multiplexed busses for I2C %s %s\n",
478 num, data->chip->muxtype == pca954x_ismux 457 num, data->chip->muxtype == pca954x_ismux
479 ? "mux" : "switch", client->name); 458 ? "mux" : "switch", client->name);
480 459
diff --git a/drivers/iio/temperature/mlx90614.c b/drivers/iio/temperature/mlx90614.c
index d619e8634a00..13a4cec64ea8 100644
--- a/drivers/iio/temperature/mlx90614.c
+++ b/drivers/iio/temperature/mlx90614.c
@@ -433,11 +433,11 @@ static int mlx90614_wakeup(struct mlx90614_data *data)
433 433
434 dev_dbg(&data->client->dev, "Requesting wake-up"); 434 dev_dbg(&data->client->dev, "Requesting wake-up");
435 435
436 i2c_lock_adapter(data->client->adapter); 436 i2c_lock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER);
437 gpiod_direction_output(data->wakeup_gpio, 0); 437 gpiod_direction_output(data->wakeup_gpio, 0);
438 msleep(MLX90614_TIMING_WAKEUP); 438 msleep(MLX90614_TIMING_WAKEUP);
439 gpiod_direction_input(data->wakeup_gpio); 439 gpiod_direction_input(data->wakeup_gpio);
440 i2c_unlock_adapter(data->client->adapter); 440 i2c_unlock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER);
441 441
442 data->ready_timestamp = jiffies + 442 data->ready_timestamp = jiffies +
443 msecs_to_jiffies(MLX90614_TIMING_STARTUP); 443 msecs_to_jiffies(MLX90614_TIMING_STARTUP);
diff --git a/drivers/input/touchscreen/rohm_bu21023.c b/drivers/input/touchscreen/rohm_bu21023.c
index bda0500c9b57..714affdd742f 100644
--- a/drivers/input/touchscreen/rohm_bu21023.c
+++ b/drivers/input/touchscreen/rohm_bu21023.c
@@ -304,7 +304,7 @@ static int rohm_i2c_burst_read(struct i2c_client *client, u8 start, void *buf,
304 msg[1].len = len; 304 msg[1].len = len;
305 msg[1].buf = buf; 305 msg[1].buf = buf;
306 306
307 i2c_lock_adapter(adap); 307 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
308 308
309 for (i = 0; i < 2; i++) { 309 for (i = 0; i < 2; i++) {
310 if (__i2c_transfer(adap, &msg[i], 1) < 0) { 310 if (__i2c_transfer(adap, &msg[i], 1) < 0) {
@@ -313,7 +313,7 @@ static int rohm_i2c_burst_read(struct i2c_client *client, u8 start, void *buf,
313 } 313 }
314 } 314 }
315 315
316 i2c_unlock_adapter(adap); 316 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
317 317
318 return ret; 318 return ret;
319} 319}
diff --git a/drivers/media/dvb-frontends/af9013.c b/drivers/media/dvb-frontends/af9013.c
index f3acbb57d48c..35a93b251aab 100644
--- a/drivers/media/dvb-frontends/af9013.c
+++ b/drivers/media/dvb-frontends/af9013.c
@@ -1311,10 +1311,10 @@ static int af9013_wregs(struct i2c_client *client, u8 cmd, u16 reg,
1311 memcpy(&buf[3], val, len); 1311 memcpy(&buf[3], val, len);
1312 1312
1313 if (lock) 1313 if (lock)
1314 i2c_lock_adapter(client->adapter); 1314 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
1315 ret = __i2c_transfer(client->adapter, msg, 1); 1315 ret = __i2c_transfer(client->adapter, msg, 1);
1316 if (lock) 1316 if (lock)
1317 i2c_unlock_adapter(client->adapter); 1317 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
1318 if (ret < 0) { 1318 if (ret < 0) {
1319 goto err; 1319 goto err;
1320 } else if (ret != 1) { 1320 } else if (ret != 1) {
@@ -1352,10 +1352,10 @@ static int af9013_rregs(struct i2c_client *client, u8 cmd, u16 reg,
1352 buf[2] = cmd; 1352 buf[2] = cmd;
1353 1353
1354 if (lock) 1354 if (lock)
1355 i2c_lock_adapter(client->adapter); 1355 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
1356 ret = __i2c_transfer(client->adapter, msg, 2); 1356 ret = __i2c_transfer(client->adapter, msg, 2);
1357 if (lock) 1357 if (lock)
1358 i2c_unlock_adapter(client->adapter); 1358 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
1359 if (ret < 0) { 1359 if (ret < 0) {
1360 goto err; 1360 goto err;
1361 } else if (ret != 2) { 1361 } else if (ret != 2) {
diff --git a/drivers/media/dvb-frontends/drxk_hard.c b/drivers/media/dvb-frontends/drxk_hard.c
index f1886945a7bc..84ac3f73f8fe 100644
--- a/drivers/media/dvb-frontends/drxk_hard.c
+++ b/drivers/media/dvb-frontends/drxk_hard.c
@@ -213,7 +213,7 @@ static inline u32 log10times100(u32 value)
213 213
214static int drxk_i2c_lock(struct drxk_state *state) 214static int drxk_i2c_lock(struct drxk_state *state)
215{ 215{
216 i2c_lock_adapter(state->i2c); 216 i2c_lock_bus(state->i2c, I2C_LOCK_SEGMENT);
217 state->drxk_i2c_exclusive_lock = true; 217 state->drxk_i2c_exclusive_lock = true;
218 218
219 return 0; 219 return 0;
@@ -224,7 +224,7 @@ static void drxk_i2c_unlock(struct drxk_state *state)
224 if (!state->drxk_i2c_exclusive_lock) 224 if (!state->drxk_i2c_exclusive_lock)
225 return; 225 return;
226 226
227 i2c_unlock_adapter(state->i2c); 227 i2c_unlock_bus(state->i2c, I2C_LOCK_SEGMENT);
228 state->drxk_i2c_exclusive_lock = false; 228 state->drxk_i2c_exclusive_lock = false;
229} 229}
230 230
diff --git a/drivers/media/dvb-frontends/rtl2830.c b/drivers/media/dvb-frontends/rtl2830.c
index adc9046d5a90..c0659568471b 100644
--- a/drivers/media/dvb-frontends/rtl2830.c
+++ b/drivers/media/dvb-frontends/rtl2830.c
@@ -24,9 +24,9 @@ static int rtl2830_bulk_write(struct i2c_client *client, unsigned int reg,
24 struct rtl2830_dev *dev = i2c_get_clientdata(client); 24 struct rtl2830_dev *dev = i2c_get_clientdata(client);
25 int ret; 25 int ret;
26 26
27 i2c_lock_adapter(client->adapter); 27 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
28 ret = regmap_bulk_write(dev->regmap, reg, val, val_count); 28 ret = regmap_bulk_write(dev->regmap, reg, val, val_count);
29 i2c_unlock_adapter(client->adapter); 29 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
30 return ret; 30 return ret;
31} 31}
32 32
@@ -36,9 +36,9 @@ static int rtl2830_update_bits(struct i2c_client *client, unsigned int reg,
36 struct rtl2830_dev *dev = i2c_get_clientdata(client); 36 struct rtl2830_dev *dev = i2c_get_clientdata(client);
37 int ret; 37 int ret;
38 38
39 i2c_lock_adapter(client->adapter); 39 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
40 ret = regmap_update_bits(dev->regmap, reg, mask, val); 40 ret = regmap_update_bits(dev->regmap, reg, mask, val);
41 i2c_unlock_adapter(client->adapter); 41 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
42 return ret; 42 return ret;
43} 43}
44 44
@@ -48,9 +48,9 @@ static int rtl2830_bulk_read(struct i2c_client *client, unsigned int reg,
48 struct rtl2830_dev *dev = i2c_get_clientdata(client); 48 struct rtl2830_dev *dev = i2c_get_clientdata(client);
49 int ret; 49 int ret;
50 50
51 i2c_lock_adapter(client->adapter); 51 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
52 ret = regmap_bulk_read(dev->regmap, reg, val, val_count); 52 ret = regmap_bulk_read(dev->regmap, reg, val, val_count);
53 i2c_unlock_adapter(client->adapter); 53 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
54 return ret; 54 return ret;
55} 55}
56 56
diff --git a/drivers/media/dvb-frontends/tda1004x.c b/drivers/media/dvb-frontends/tda1004x.c
index d402e4b722ca..e506f66657bb 100644
--- a/drivers/media/dvb-frontends/tda1004x.c
+++ b/drivers/media/dvb-frontends/tda1004x.c
@@ -329,7 +329,7 @@ static int tda1004x_do_upload(struct tda1004x_state *state,
329 tda1004x_write_byteI(state, dspCodeCounterReg, 0); 329 tda1004x_write_byteI(state, dspCodeCounterReg, 0);
330 fw_msg.addr = state->config->demod_address; 330 fw_msg.addr = state->config->demod_address;
331 331
332 i2c_lock_adapter(state->i2c); 332 i2c_lock_bus(state->i2c, I2C_LOCK_SEGMENT);
333 buf[0] = dspCodeInReg; 333 buf[0] = dspCodeInReg;
334 while (pos != len) { 334 while (pos != len) {
335 // work out how much to send this time 335 // work out how much to send this time
@@ -342,14 +342,14 @@ static int tda1004x_do_upload(struct tda1004x_state *state,
342 fw_msg.len = tx_size + 1; 342 fw_msg.len = tx_size + 1;
343 if (__i2c_transfer(state->i2c, &fw_msg, 1) != 1) { 343 if (__i2c_transfer(state->i2c, &fw_msg, 1) != 1) {
344 printk(KERN_ERR "tda1004x: Error during firmware upload\n"); 344 printk(KERN_ERR "tda1004x: Error during firmware upload\n");
345 i2c_unlock_adapter(state->i2c); 345 i2c_unlock_bus(state->i2c, I2C_LOCK_SEGMENT);
346 return -EIO; 346 return -EIO;
347 } 347 }
348 pos += tx_size; 348 pos += tx_size;
349 349
350 dprintk("%s: fw_pos=0x%x\n", __func__, pos); 350 dprintk("%s: fw_pos=0x%x\n", __func__, pos);
351 } 351 }
352 i2c_unlock_adapter(state->i2c); 352 i2c_unlock_bus(state->i2c, I2C_LOCK_SEGMENT);
353 353
354 /* give the DSP a chance to settle 03/10/05 Hac */ 354 /* give the DSP a chance to settle 03/10/05 Hac */
355 msleep(100); 355 msleep(100);
diff --git a/drivers/media/tuners/tda18271-common.c b/drivers/media/tuners/tda18271-common.c
index 7e81cd887c13..054b3b747dae 100644
--- a/drivers/media/tuners/tda18271-common.c
+++ b/drivers/media/tuners/tda18271-common.c
@@ -225,7 +225,7 @@ static int __tda18271_write_regs(struct dvb_frontend *fe, int idx, int len,
225 */ 225 */
226 if (lock_i2c) { 226 if (lock_i2c) {
227 tda18271_i2c_gate_ctrl(fe, 1); 227 tda18271_i2c_gate_ctrl(fe, 1);
228 i2c_lock_adapter(priv->i2c_props.adap); 228 i2c_lock_bus(priv->i2c_props.adap, I2C_LOCK_SEGMENT);
229 } 229 }
230 while (len) { 230 while (len) {
231 if (max > len) 231 if (max > len)
@@ -246,7 +246,7 @@ static int __tda18271_write_regs(struct dvb_frontend *fe, int idx, int len,
246 len -= max; 246 len -= max;
247 } 247 }
248 if (lock_i2c) { 248 if (lock_i2c) {
249 i2c_unlock_adapter(priv->i2c_props.adap); 249 i2c_unlock_bus(priv->i2c_props.adap, I2C_LOCK_SEGMENT);
250 tda18271_i2c_gate_ctrl(fe, 0); 250 tda18271_i2c_gate_ctrl(fe, 0);
251 } 251 }
252 252
@@ -300,7 +300,7 @@ int tda18271_init_regs(struct dvb_frontend *fe)
300 * as those could cause bad things 300 * as those could cause bad things
301 */ 301 */
302 tda18271_i2c_gate_ctrl(fe, 1); 302 tda18271_i2c_gate_ctrl(fe, 1);
303 i2c_lock_adapter(priv->i2c_props.adap); 303 i2c_lock_bus(priv->i2c_props.adap, I2C_LOCK_SEGMENT);
304 304
305 /* initialize registers */ 305 /* initialize registers */
306 switch (priv->id) { 306 switch (priv->id) {
@@ -516,7 +516,7 @@ int tda18271_init_regs(struct dvb_frontend *fe)
516 /* synchronize */ 516 /* synchronize */
517 __tda18271_write_regs(fe, R_EP1, 1, false); 517 __tda18271_write_regs(fe, R_EP1, 1, false);
518 518
519 i2c_unlock_adapter(priv->i2c_props.adap); 519 i2c_unlock_bus(priv->i2c_props.adap, I2C_LOCK_SEGMENT);
520 tda18271_i2c_gate_ctrl(fe, 0); 520 tda18271_i2c_gate_ctrl(fe, 0);
521 521
522 return 0; 522 return 0;
diff --git a/drivers/mfd/88pm860x-i2c.c b/drivers/mfd/88pm860x-i2c.c
index 84e313107233..7b9052ea7413 100644
--- a/drivers/mfd/88pm860x-i2c.c
+++ b/drivers/mfd/88pm860x-i2c.c
@@ -146,14 +146,14 @@ int pm860x_page_reg_write(struct i2c_client *i2c, int reg,
146 unsigned char zero; 146 unsigned char zero;
147 int ret; 147 int ret;
148 148
149 i2c_lock_adapter(i2c->adapter); 149 i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
150 read_device(i2c, 0xFA, 0, &zero); 150 read_device(i2c, 0xFA, 0, &zero);
151 read_device(i2c, 0xFB, 0, &zero); 151 read_device(i2c, 0xFB, 0, &zero);
152 read_device(i2c, 0xFF, 0, &zero); 152 read_device(i2c, 0xFF, 0, &zero);
153 ret = write_device(i2c, reg, 1, &data); 153 ret = write_device(i2c, reg, 1, &data);
154 read_device(i2c, 0xFE, 0, &zero); 154 read_device(i2c, 0xFE, 0, &zero);
155 read_device(i2c, 0xFC, 0, &zero); 155 read_device(i2c, 0xFC, 0, &zero);
156 i2c_unlock_adapter(i2c->adapter); 156 i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
157 return ret; 157 return ret;
158} 158}
159EXPORT_SYMBOL(pm860x_page_reg_write); 159EXPORT_SYMBOL(pm860x_page_reg_write);
@@ -164,14 +164,14 @@ int pm860x_page_bulk_read(struct i2c_client *i2c, int reg,
164 unsigned char zero = 0; 164 unsigned char zero = 0;
165 int ret; 165 int ret;
166 166
167 i2c_lock_adapter(i2c->adapter); 167 i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
168 read_device(i2c, 0xfa, 0, &zero); 168 read_device(i2c, 0xfa, 0, &zero);
169 read_device(i2c, 0xfb, 0, &zero); 169 read_device(i2c, 0xfb, 0, &zero);
170 read_device(i2c, 0xff, 0, &zero); 170 read_device(i2c, 0xff, 0, &zero);
171 ret = read_device(i2c, reg, count, buf); 171 ret = read_device(i2c, reg, count, buf);
172 read_device(i2c, 0xFE, 0, &zero); 172 read_device(i2c, 0xFE, 0, &zero);
173 read_device(i2c, 0xFC, 0, &zero); 173 read_device(i2c, 0xFC, 0, &zero);
174 i2c_unlock_adapter(i2c->adapter); 174 i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
175 return ret; 175 return ret;
176} 176}
177EXPORT_SYMBOL(pm860x_page_bulk_read); 177EXPORT_SYMBOL(pm860x_page_bulk_read);
diff --git a/drivers/misc/eeprom/at24.c b/drivers/misc/eeprom/at24.c
index f5cc517d1131..7e50e1d6f58c 100644
--- a/drivers/misc/eeprom/at24.c
+++ b/drivers/misc/eeprom/at24.c
@@ -478,6 +478,23 @@ static void at24_properties_to_pdata(struct device *dev,
478 if (device_property_present(dev, "no-read-rollover")) 478 if (device_property_present(dev, "no-read-rollover"))
479 chip->flags |= AT24_FLAG_NO_RDROL; 479 chip->flags |= AT24_FLAG_NO_RDROL;
480 480
481 err = device_property_read_u32(dev, "address-width", &val);
482 if (!err) {
483 switch (val) {
484 case 8:
485 if (chip->flags & AT24_FLAG_ADDR16)
486 dev_warn(dev, "Override address width to be 8, while default is 16\n");
487 chip->flags &= ~AT24_FLAG_ADDR16;
488 break;
489 case 16:
490 chip->flags |= AT24_FLAG_ADDR16;
491 break;
492 default:
493 dev_warn(dev, "Bad \"address-width\" property: %u\n",
494 val);
495 }
496 }
497
481 err = device_property_read_u32(dev, "size", &val); 498 err = device_property_read_u32(dev, "size", &val);
482 if (!err) 499 if (!err)
483 chip->byte_len = val; 500 chip->byte_len = val;
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 465afb092fa7..36f357ecdf67 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -564,6 +564,7 @@ struct i2c_lock_operations {
564 * @scl_fall_ns: time SCL signal takes to fall in ns; t(f) in the I2C specification 564 * @scl_fall_ns: time SCL signal takes to fall in ns; t(f) in the I2C specification
565 * @scl_int_delay_ns: time IP core additionally needs to setup SCL in ns 565 * @scl_int_delay_ns: time IP core additionally needs to setup SCL in ns
566 * @sda_fall_ns: time SDA signal takes to fall in ns; t(f) in the I2C specification 566 * @sda_fall_ns: time SDA signal takes to fall in ns; t(f) in the I2C specification
567 * @sda_hold_ns: time IP core additionally needs to hold SDA in ns
567 */ 568 */
568struct i2c_timings { 569struct i2c_timings {
569 u32 bus_freq_hz; 570 u32 bus_freq_hz;
@@ -571,6 +572,7 @@ struct i2c_timings {
571 u32 scl_fall_ns; 572 u32 scl_fall_ns;
572 u32 scl_int_delay_ns; 573 u32 scl_int_delay_ns;
573 u32 sda_fall_ns; 574 u32 sda_fall_ns;
575 u32 sda_hold_ns;
574}; 576};
575 577
576/** 578/**
@@ -581,12 +583,14 @@ struct i2c_timings {
581 * recovery. Populated internally for generic GPIO recovery. 583 * recovery. Populated internally for generic GPIO recovery.
582 * @set_scl: This sets/clears the SCL line. Mandatory for generic SCL recovery. 584 * @set_scl: This sets/clears the SCL line. Mandatory for generic SCL recovery.
583 * Populated internally for generic GPIO recovery. 585 * Populated internally for generic GPIO recovery.
584 * @get_sda: This gets current value of SDA line. Optional for generic SCL 586 * @get_sda: This gets current value of SDA line. This or set_sda() is mandatory
585 * recovery. Populated internally, if sda_gpio is a valid GPIO, for generic 587 * for generic SCL recovery. Populated internally, if sda_gpio is a valid
586 * GPIO recovery. 588 * GPIO, for generic GPIO recovery.
587 * @set_sda: This sets/clears the SDA line. Optional for generic SCL recovery. 589 * @set_sda: This sets/clears the SDA line. This or get_sda() is mandatory for
588 * Populated internally, if sda_gpio is a valid GPIO, for generic GPIO 590 * generic SCL recovery. Populated internally, if sda_gpio is a valid GPIO,
589 * recovery. 591 * for generic GPIO recovery.
592 * @get_bus_free: Returns the bus free state as seen from the IP core in case it
593 * has a more complex internal logic than just reading SDA. Optional.
590 * @prepare_recovery: This will be called before starting recovery. Platform may 594 * @prepare_recovery: This will be called before starting recovery. Platform may
591 * configure padmux here for SDA/SCL line or something else they want. 595 * configure padmux here for SDA/SCL line or something else they want.
592 * @unprepare_recovery: This will be called after completing recovery. Platform 596 * @unprepare_recovery: This will be called after completing recovery. Platform
@@ -601,6 +605,7 @@ struct i2c_bus_recovery_info {
601 void (*set_scl)(struct i2c_adapter *adap, int val); 605 void (*set_scl)(struct i2c_adapter *adap, int val);
602 int (*get_sda)(struct i2c_adapter *adap); 606 int (*get_sda)(struct i2c_adapter *adap);
603 void (*set_sda)(struct i2c_adapter *adap, int val); 607 void (*set_sda)(struct i2c_adapter *adap, int val);
608 int (*get_bus_free)(struct i2c_adapter *adap);
604 609
605 void (*prepare_recovery)(struct i2c_adapter *adap); 610 void (*prepare_recovery)(struct i2c_adapter *adap);
606 void (*unprepare_recovery)(struct i2c_adapter *adap); 611 void (*unprepare_recovery)(struct i2c_adapter *adap);
@@ -658,6 +663,10 @@ struct i2c_adapter_quirks {
658 I2C_AQ_COMB_READ_SECOND | I2C_AQ_COMB_SAME_ADDR) 663 I2C_AQ_COMB_READ_SECOND | I2C_AQ_COMB_SAME_ADDR)
659/* clock stretching is not supported */ 664/* clock stretching is not supported */
660#define I2C_AQ_NO_CLK_STRETCH BIT(4) 665#define I2C_AQ_NO_CLK_STRETCH BIT(4)
666/* message cannot have length of 0 */
667#define I2C_AQ_NO_ZERO_LEN_READ BIT(5)
668#define I2C_AQ_NO_ZERO_LEN_WRITE BIT(6)
669#define I2C_AQ_NO_ZERO_LEN (I2C_AQ_NO_ZERO_LEN_READ | I2C_AQ_NO_ZERO_LEN_WRITE)
661 670
662/* 671/*
663 * i2c_adapter is the structure used to identify a physical i2c bus along 672 * i2c_adapter is the structure used to identify a physical i2c bus along
@@ -759,18 +768,6 @@ i2c_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
759 adapter->lock_ops->unlock_bus(adapter, flags); 768 adapter->lock_ops->unlock_bus(adapter, flags);
760} 769}
761 770
762static inline void
763i2c_lock_adapter(struct i2c_adapter *adapter)
764{
765 i2c_lock_bus(adapter, I2C_LOCK_ROOT_ADAPTER);
766}
767
768static inline void
769i2c_unlock_adapter(struct i2c_adapter *adapter)
770{
771 i2c_unlock_bus(adapter, I2C_LOCK_ROOT_ADAPTER);
772}
773
774/*flags for the client struct: */ 771/*flags for the client struct: */
775#define I2C_CLIENT_PEC 0x04 /* Use Packet Error Checking */ 772#define I2C_CLIENT_PEC 0x04 /* Use Packet Error Checking */
776#define I2C_CLIENT_TEN 0x10 /* we have a ten bit chip address */ 773#define I2C_CLIENT_TEN 0x10 /* we have a ten bit chip address */