aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/ABI/testing/sysfs-platform-i2c-demux-pinctrl23
-rw-r--r--Documentation/devicetree/bindings/i2c/i2c-demux-pinctrl.txt135
-rw-r--r--Documentation/devicetree/bindings/i2c/i2c-imx.txt2
-rw-r--r--Documentation/devicetree/bindings/i2c/i2c-rcar.txt2
-rw-r--r--Documentation/devicetree/bindings/i2c/i2c-sirf.txt2
-rw-r--r--Documentation/devicetree/bindings/i2c/i2c-xiic.txt3
-rw-r--r--MAINTAINERS14
-rw-r--r--drivers/i2c/busses/Kconfig10
-rw-r--r--drivers/i2c/busses/i2c-bcm-iproc.c177
-rw-r--r--drivers/i2c/busses/i2c-cadence.c14
-rw-r--r--drivers/i2c/busses/i2c-designware-baytrail.c5
-rw-r--r--drivers/i2c/busses/i2c-designware-core.c22
-rw-r--r--drivers/i2c/busses/i2c-designware-core.h2
-rw-r--r--drivers/i2c/busses/i2c-i801.c8
-rw-r--r--drivers/i2c/busses/i2c-imx.c12
-rw-r--r--drivers/i2c/busses/i2c-mt65xx.c42
-rw-r--r--drivers/i2c/busses/i2c-octeon.c307
-rw-r--r--drivers/i2c/busses/i2c-piix4.c48
-rw-r--r--drivers/i2c/busses/i2c-qup.c977
-rw-r--r--drivers/i2c/busses/i2c-rcar.c2
-rw-r--r--drivers/i2c/busses/i2c-tegra.c4
-rw-r--r--drivers/i2c/busses/i2c-xiic.c88
-rw-r--r--drivers/i2c/i2c-boardinfo.c6
-rw-r--r--drivers/i2c/i2c-core.c39
-rw-r--r--drivers/i2c/i2c-dev.c14
-rw-r--r--drivers/i2c/i2c-mux.c8
-rw-r--r--drivers/i2c/i2c-smbus.c8
-rw-r--r--drivers/i2c/i2c-stub.c8
-rw-r--r--drivers/i2c/muxes/Kconfig9
-rw-r--r--drivers/i2c/muxes/Makefile2
-rw-r--r--drivers/i2c/muxes/i2c-demux-pinctrl.c272
31 files changed, 1893 insertions, 372 deletions
diff --git a/Documentation/ABI/testing/sysfs-platform-i2c-demux-pinctrl b/Documentation/ABI/testing/sysfs-platform-i2c-demux-pinctrl
new file mode 100644
index 000000000000..7ac7d7262bb7
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-platform-i2c-demux-pinctrl
@@ -0,0 +1,23 @@
1What: /sys/devices/platform/<i2c-demux-name>/cur_master
2Date: January 2016
3KernelVersion: 4.6
4Contact: Wolfram Sang <wsa@the-dreams.de>
5Description:
6
7This file selects the active I2C master for a demultiplexed bus.
8
9Write 0 there for the first master, 1 for the second etc. Reading the file will
10give you a list with the active master marked. Example from a Renesas Lager
11board:
12
13root@Lager:~# cat /sys/devices/platform/i2c@8/cur_master
14* 0 - /i2c@9
15 1 - /i2c@e6520000
16 2 - /i2c@e6530000
17
18root@Lager:~# echo 2 > /sys/devices/platform/i2c@8/cur_master
19
20root@Lager:~# cat /sys/devices/platform/i2c@8/cur_master
21 0 - /i2c@9
22 1 - /i2c@e6520000
23* 2 - /i2c@e6530000
diff --git a/Documentation/devicetree/bindings/i2c/i2c-demux-pinctrl.txt b/Documentation/devicetree/bindings/i2c/i2c-demux-pinctrl.txt
new file mode 100644
index 000000000000..6078aefe7ed4
--- /dev/null
+++ b/Documentation/devicetree/bindings/i2c/i2c-demux-pinctrl.txt
@@ -0,0 +1,135 @@
1Pinctrl-based I2C Bus DeMux
2
3This binding describes an I2C bus demultiplexer that uses pin multiplexing to
4route the I2C signals, and represents the pin multiplexing configuration using
5the pinctrl device tree bindings. This may be used to select one I2C IP core at
6runtime which may have a better feature set for a given task than another I2C
7IP core on the SoC. The most simple example is to fall back to GPIO bitbanging
8if your current runtime configuration hits an errata of the internal IP core.
9
10 +-------------------------------+
11 | SoC |
12 | | +-----+ +-----+
13 | +------------+ | | dev | | dev |
14 | |I2C IP Core1|--\ | +-----+ +-----+
15 | +------------+ \-------+ | | |
16 | |Pinctrl|--|------+--------+
17 | +------------+ +-------+ |
18 | |I2C IP Core2|--/ |
19 | +------------+ |
20 | |
21 +-------------------------------+
22
23Required properties:
24- compatible: "i2c-demux-pinctrl"
25- i2c-parent: List of phandles of I2C masters available for selection. The first
26 one will be used as default.
27- i2c-bus-name: The name of this bus. Also needed as pinctrl-name for the I2C
28 parents.
29
30Furthermore, I2C mux properties and child nodes. See mux.txt in this directory.
31
32Example:
33
34Here is a snipplet for a bus to be demuxed. It contains various i2c clients for
35HDMI, so the bus is named "i2c-hdmi":
36
37 i2chdmi: i2c@8 {
38
39 compatible = "i2c-demux-pinctrl";
40 i2c-parent = <&gpioi2c>, <&iic2>, <&i2c2>;
41 i2c-bus-name = "i2c-hdmi";
42 #address-cells = <1>;
43 #size-cells = <0>;
44
45 ak4643: sound-codec@12 {
46 compatible = "asahi-kasei,ak4643";
47
48 #sound-dai-cells = <0>;
49 reg = <0x12>;
50 };
51
52 composite-in@20 {
53 compatible = "adi,adv7180";
54 reg = <0x20>;
55 remote = <&vin1>;
56
57 port {
58 adv7180: endpoint {
59 bus-width = <8>;
60 remote-endpoint = <&vin1ep0>;
61 };
62 };
63 };
64
65 hdmi@39 {
66 compatible = "adi,adv7511w";
67 reg = <0x39>;
68 interrupt-parent = <&gpio1>;
69 interrupts = <15 IRQ_TYPE_LEVEL_LOW>;
70
71 adi,input-depth = <8>;
72 adi,input-colorspace = "rgb";
73 adi,input-clock = "1x";
74 adi,input-style = <1>;
75 adi,input-justification = "evenly";
76
77 ports {
78 #address-cells = <1>;
79 #size-cells = <0>;
80
81 port@0 {
82 reg = <0>;
83 adv7511_in: endpoint {
84 remote-endpoint = <&du_out_lvds0>;
85 };
86 };
87
88 port@1 {
89 reg = <1>;
90 adv7511_out: endpoint {
91 remote-endpoint = <&hdmi_con>;
92 };
93 };
94 };
95 };
96 };
97
98And for clarification, here are the snipplets for the i2c-parents:
99
100 gpioi2c: i2c@9 {
101 #address-cells = <1>;
102 #size-cells = <0>;
103 compatible = "i2c-gpio";
104 status = "disabled";
105 gpios = <&gpio5 6 GPIO_ACTIVE_HIGH /* sda */
106 &gpio5 5 GPIO_ACTIVE_HIGH /* scl */
107 >;
108 i2c-gpio,delay-us = <5>;
109 };
110
111...
112
113&i2c2 {
114 pinctrl-0 = <&i2c2_pins>;
115 pinctrl-names = "i2c-hdmi";
116
117 clock-frequency = <100000>;
118};
119
120...
121
122&iic2 {
123 pinctrl-0 = <&iic2_pins>;
124 pinctrl-names = "i2c-hdmi";
125
126 clock-frequency = <100000>;
127};
128
129Please note:
130
131- pinctrl properties for the parent I2C controllers need a pinctrl state
132 with the same name as i2c-bus-name, not "default"!
133
134- the i2c masters must have their status "disabled". This driver will
135 enable them at runtime when needed.
diff --git a/Documentation/devicetree/bindings/i2c/i2c-imx.txt b/Documentation/devicetree/bindings/i2c/i2c-imx.txt
index eab5836ba7f9..b967544590e8 100644
--- a/Documentation/devicetree/bindings/i2c/i2c-imx.txt
+++ b/Documentation/devicetree/bindings/i2c/i2c-imx.txt
@@ -11,7 +11,7 @@ Required properties:
11 11
12Optional properties: 12Optional properties:
13- clock-frequency : Constains desired I2C/HS-I2C bus clock frequency in Hz. 13- clock-frequency : Constains desired I2C/HS-I2C bus clock frequency in Hz.
14 The absence of the propoerty indicates the default frequency 100 kHz. 14 The absence of the property indicates the default frequency 100 kHz.
15- dmas: A list of two dma specifiers, one for each entry in dma-names. 15- dmas: A list of two dma specifiers, one for each entry in dma-names.
16- dma-names: should contain "tx" and "rx". 16- dma-names: should contain "tx" and "rx".
17- scl-gpios: specify the gpio related to SCL pin 17- scl-gpios: specify the gpio related to SCL pin
diff --git a/Documentation/devicetree/bindings/i2c/i2c-rcar.txt b/Documentation/devicetree/bindings/i2c/i2c-rcar.txt
index 95e97223a71c..cf8bfc956cdc 100644
--- a/Documentation/devicetree/bindings/i2c/i2c-rcar.txt
+++ b/Documentation/devicetree/bindings/i2c/i2c-rcar.txt
@@ -17,7 +17,7 @@ Required properties:
17 17
18Optional properties: 18Optional properties:
19- clock-frequency: desired I2C bus clock frequency in Hz. The absence of this 19- clock-frequency: desired I2C bus clock frequency in Hz. The absence of this
20 propoerty indicates the default frequency 100 kHz. 20 property indicates the default frequency 100 kHz.
21- clocks: clock specifier. 21- clocks: clock specifier.
22 22
23- i2c-scl-falling-time-ns: see i2c.txt 23- i2c-scl-falling-time-ns: see i2c.txt
diff --git a/Documentation/devicetree/bindings/i2c/i2c-sirf.txt b/Documentation/devicetree/bindings/i2c/i2c-sirf.txt
index 7baf9e133fa8..2701eefb00f7 100644
--- a/Documentation/devicetree/bindings/i2c/i2c-sirf.txt
+++ b/Documentation/devicetree/bindings/i2c/i2c-sirf.txt
@@ -8,7 +8,7 @@ Required properties :
8 8
9Optional properties: 9Optional properties:
10- clock-frequency : Constains desired I2C/HS-I2C bus clock frequency in Hz. 10- clock-frequency : Constains desired I2C/HS-I2C bus clock frequency in Hz.
11 The absence of the propoerty indicates the default frequency 100 kHz. 11 The absence of the property indicates the default frequency 100 kHz.
12 12
13Examples : 13Examples :
14 14
diff --git a/Documentation/devicetree/bindings/i2c/i2c-xiic.txt b/Documentation/devicetree/bindings/i2c/i2c-xiic.txt
index ceabbe91ae44..caf42e989462 100644
--- a/Documentation/devicetree/bindings/i2c/i2c-xiic.txt
+++ b/Documentation/devicetree/bindings/i2c/i2c-xiic.txt
@@ -6,14 +6,17 @@ Required properties:
6- interrupts : IIC controller unterrupt 6- interrupts : IIC controller unterrupt
7- #address-cells = <1> 7- #address-cells = <1>
8- #size-cells = <0> 8- #size-cells = <0>
9- clocks: Input clock specifier. Refer to common clock bindings.
9 10
10Optional properties: 11Optional properties:
11- Child nodes conforming to i2c bus binding 12- Child nodes conforming to i2c bus binding
13- clock-names: Input clock name, should be 'pclk'.
12 14
13Example: 15Example:
14 16
15 axi_iic_0: i2c@40800000 { 17 axi_iic_0: i2c@40800000 {
16 compatible = "xlnx,xps-iic-2.00.a"; 18 compatible = "xlnx,xps-iic-2.00.a";
19 clocks = <&clkc 15>;
17 interrupts = < 1 2 >; 20 interrupts = < 1 2 >;
18 reg = < 0x40800000 0x10000 >; 21 reg = < 0x40800000 0x10000 >;
19 22
diff --git a/MAINTAINERS b/MAINTAINERS
index cc637cd31f3b..0f3063cce44c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5270,6 +5270,16 @@ F: include/linux/hyperv.h
5270F: tools/hv/ 5270F: tools/hv/
5271F: Documentation/ABI/stable/sysfs-bus-vmbus 5271F: Documentation/ABI/stable/sysfs-bus-vmbus
5272 5272
5273I2C MUXES
5274M: Peter Rosin <peda@axentia.se>
5275L: linux-i2c@vger.kernel.org
5276S: Maintained
5277F: Documentation/i2c/muxes/
5278F: Documentation/devicetree/bindings/i2c/i2c-mux*
5279F: drivers/i2c/i2c-mux.c
5280F: drivers/i2c/muxes/
5281F: include/linux/i2c-mux.h
5282
5273I2C OVER PARALLEL PORT 5283I2C OVER PARALLEL PORT
5274M: Jean Delvare <jdelvare@suse.com> 5284M: Jean Delvare <jdelvare@suse.com>
5275L: linux-i2c@vger.kernel.org 5285L: linux-i2c@vger.kernel.org
@@ -9676,9 +9686,9 @@ F: Documentation/devicetree/bindings/net/snps,dwc-qos-ethernet.txt
9676F: drivers/net/ethernet/synopsys/dwc_eth_qos.c 9686F: drivers/net/ethernet/synopsys/dwc_eth_qos.c
9677 9687
9678SYNOPSYS DESIGNWARE I2C DRIVER 9688SYNOPSYS DESIGNWARE I2C DRIVER
9679M: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
9680M: Jarkko Nikula <jarkko.nikula@linux.intel.com> 9689M: Jarkko Nikula <jarkko.nikula@linux.intel.com>
9681M: Mika Westerberg <mika.westerberg@linux.intel.com> 9690R: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
9691R: Mika Westerberg <mika.westerberg@linux.intel.com>
9682L: linux-i2c@vger.kernel.org 9692L: linux-i2c@vger.kernel.org
9683S: Maintained 9693S: Maintained
9684F: drivers/i2c/busses/i2c-designware-* 9694F: drivers/i2c/busses/i2c-designware-*
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index 0299dfa746a3..faa8e6821fea 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -789,7 +789,7 @@ config I2C_QUP
789 789
790config I2C_RIIC 790config I2C_RIIC
791 tristate "Renesas RIIC adapter" 791 tristate "Renesas RIIC adapter"
792 depends on ARCH_SHMOBILE || COMPILE_TEST 792 depends on ARCH_RENESAS || COMPILE_TEST
793 help 793 help
794 If you say yes to this option, support will be included for the 794 If you say yes to this option, support will be included for the
795 Renesas RIIC I2C interface. 795 Renesas RIIC I2C interface.
@@ -833,7 +833,7 @@ config I2C_SH7760
833config I2C_SH_MOBILE 833config I2C_SH_MOBILE
834 tristate "SuperH Mobile I2C Controller" 834 tristate "SuperH Mobile I2C Controller"
835 depends on HAS_DMA 835 depends on HAS_DMA
836 depends on SUPERH || ARCH_SHMOBILE || COMPILE_TEST 836 depends on SUPERH || ARCH_RENESAS || COMPILE_TEST
837 help 837 help
838 If you say yes to this option, support will be included for the 838 If you say yes to this option, support will be included for the
839 built-in I2C interface on the Renesas SH-Mobile processor. 839 built-in I2C interface on the Renesas SH-Mobile processor.
@@ -908,7 +908,7 @@ config I2C_TEGRA
908 908
909config I2C_UNIPHIER 909config I2C_UNIPHIER
910 tristate "UniPhier FIFO-less I2C controller" 910 tristate "UniPhier FIFO-less I2C controller"
911 depends on ARCH_UNIPHIER 911 depends on ARCH_UNIPHIER || COMPILE_TEST
912 help 912 help
913 If you say yes to this option, support will be included for 913 If you say yes to this option, support will be included for
914 the UniPhier FIFO-less I2C interface embedded in PH1-LD4, PH1-sLD8, 914 the UniPhier FIFO-less I2C interface embedded in PH1-LD4, PH1-sLD8,
@@ -916,7 +916,7 @@ config I2C_UNIPHIER
916 916
917config I2C_UNIPHIER_F 917config I2C_UNIPHIER_F
918 tristate "UniPhier FIFO-builtin I2C controller" 918 tristate "UniPhier FIFO-builtin I2C controller"
919 depends on ARCH_UNIPHIER 919 depends on ARCH_UNIPHIER || COMPILE_TEST
920 help 920 help
921 If you say yes to this option, support will be included for 921 If you say yes to this option, support will be included for
922 the UniPhier FIFO-builtin I2C interface embedded in PH1-Pro4, 922 the UniPhier FIFO-builtin I2C interface embedded in PH1-Pro4,
@@ -985,7 +985,7 @@ config I2C_XLP9XX
985 985
986config I2C_RCAR 986config I2C_RCAR
987 tristate "Renesas R-Car I2C Controller" 987 tristate "Renesas R-Car I2C Controller"
988 depends on ARCH_SHMOBILE || COMPILE_TEST 988 depends on ARCH_RENESAS || COMPILE_TEST
989 select I2C_SLAVE 989 select I2C_SLAVE
990 help 990 help
991 If you say yes to this option, support will be included for the 991 If you say yes to this option, support will be included for the
diff --git a/drivers/i2c/busses/i2c-bcm-iproc.c b/drivers/i2c/busses/i2c-bcm-iproc.c
index 0419f5284609..b9f0fff4e723 100644
--- a/drivers/i2c/busses/i2c-bcm-iproc.c
+++ b/drivers/i2c/busses/i2c-bcm-iproc.c
@@ -58,11 +58,13 @@
58#define IE_M_RX_FIFO_FULL_SHIFT 31 58#define IE_M_RX_FIFO_FULL_SHIFT 31
59#define IE_M_RX_THLD_SHIFT 30 59#define IE_M_RX_THLD_SHIFT 30
60#define IE_M_START_BUSY_SHIFT 28 60#define IE_M_START_BUSY_SHIFT 28
61#define IE_M_TX_UNDERRUN_SHIFT 27
61 62
62#define IS_OFFSET 0x3c 63#define IS_OFFSET 0x3c
63#define IS_M_RX_FIFO_FULL_SHIFT 31 64#define IS_M_RX_FIFO_FULL_SHIFT 31
64#define IS_M_RX_THLD_SHIFT 30 65#define IS_M_RX_THLD_SHIFT 30
65#define IS_M_START_BUSY_SHIFT 28 66#define IS_M_START_BUSY_SHIFT 28
67#define IS_M_TX_UNDERRUN_SHIFT 27
66 68
67#define M_TX_OFFSET 0x40 69#define M_TX_OFFSET 0x40
68#define M_TX_WR_STATUS_SHIFT 31 70#define M_TX_WR_STATUS_SHIFT 31
@@ -76,7 +78,7 @@
76#define M_RX_DATA_SHIFT 0 78#define M_RX_DATA_SHIFT 0
77#define M_RX_DATA_MASK 0xff 79#define M_RX_DATA_MASK 0xff
78 80
79#define I2C_TIMEOUT_MESC 100 81#define I2C_TIMEOUT_MSEC 50000
80#define M_TX_RX_FIFO_SIZE 64 82#define M_TX_RX_FIFO_SIZE 64
81 83
82enum bus_speed_index { 84enum bus_speed_index {
@@ -95,12 +97,17 @@ struct bcm_iproc_i2c_dev {
95 97
96 struct completion done; 98 struct completion done;
97 int xfer_is_done; 99 int xfer_is_done;
100
101 struct i2c_msg *msg;
102
103 /* bytes that have been transferred */
104 unsigned int tx_bytes;
98}; 105};
99 106
100/* 107/*
101 * Can be expanded in the future if more interrupt status bits are utilized 108 * Can be expanded in the future if more interrupt status bits are utilized
102 */ 109 */
103#define ISR_MASK (1 << IS_M_START_BUSY_SHIFT) 110#define ISR_MASK (BIT(IS_M_START_BUSY_SHIFT) | BIT(IS_M_TX_UNDERRUN_SHIFT))
104 111
105static irqreturn_t bcm_iproc_i2c_isr(int irq, void *data) 112static irqreturn_t bcm_iproc_i2c_isr(int irq, void *data)
106{ 113{
@@ -112,13 +119,95 @@ static irqreturn_t bcm_iproc_i2c_isr(int irq, void *data)
112 if (!status) 119 if (!status)
113 return IRQ_NONE; 120 return IRQ_NONE;
114 121
122 /* TX FIFO is empty and we have more data to send */
123 if (status & BIT(IS_M_TX_UNDERRUN_SHIFT)) {
124 struct i2c_msg *msg = iproc_i2c->msg;
125 unsigned int tx_bytes = msg->len - iproc_i2c->tx_bytes;
126 unsigned int i;
127 u32 val;
128
129 /* can only fill up to the FIFO size */
130 tx_bytes = min_t(unsigned int, tx_bytes, M_TX_RX_FIFO_SIZE);
131 for (i = 0; i < tx_bytes; i++) {
132 /* start from where we left over */
133 unsigned int idx = iproc_i2c->tx_bytes + i;
134
135 val = msg->buf[idx];
136
137 /* mark the last byte */
138 if (idx == msg->len - 1) {
139 u32 tmp;
140
141 val |= BIT(M_TX_WR_STATUS_SHIFT);
142
143 /*
144 * Since this is the last byte, we should
145 * now disable TX FIFO underrun interrupt
146 */
147 tmp = readl(iproc_i2c->base + IE_OFFSET);
148 tmp &= ~BIT(IE_M_TX_UNDERRUN_SHIFT);
149 writel(tmp, iproc_i2c->base + IE_OFFSET);
150 }
151
152 /* load data into TX FIFO */
153 writel(val, iproc_i2c->base + M_TX_OFFSET);
154 }
155 /* update number of transferred bytes */
156 iproc_i2c->tx_bytes += tx_bytes;
157 }
158
159 if (status & BIT(IS_M_START_BUSY_SHIFT)) {
160 iproc_i2c->xfer_is_done = 1;
161 complete_all(&iproc_i2c->done);
162 }
163
115 writel(status, iproc_i2c->base + IS_OFFSET); 164 writel(status, iproc_i2c->base + IS_OFFSET);
116 iproc_i2c->xfer_is_done = 1;
117 complete_all(&iproc_i2c->done);
118 165
119 return IRQ_HANDLED; 166 return IRQ_HANDLED;
120} 167}
121 168
169static int bcm_iproc_i2c_init(struct bcm_iproc_i2c_dev *iproc_i2c)
170{
171 u32 val;
172
173 /* put controller in reset */
174 val = readl(iproc_i2c->base + CFG_OFFSET);
175 val |= 1 << CFG_RESET_SHIFT;
176 val &= ~(1 << CFG_EN_SHIFT);
177 writel(val, iproc_i2c->base + CFG_OFFSET);
178
179 /* wait 100 usec per spec */
180 udelay(100);
181
182 /* bring controller out of reset */
183 val &= ~(1 << CFG_RESET_SHIFT);
184 writel(val, iproc_i2c->base + CFG_OFFSET);
185
186 /* flush TX/RX FIFOs and set RX FIFO threshold to zero */
187 val = (1 << M_FIFO_RX_FLUSH_SHIFT) | (1 << M_FIFO_TX_FLUSH_SHIFT);
188 writel(val, iproc_i2c->base + M_FIFO_CTRL_OFFSET);
189 /* disable all interrupts */
190 writel(0, iproc_i2c->base + IE_OFFSET);
191
192 /* clear all pending interrupts */
193 writel(0xffffffff, iproc_i2c->base + IS_OFFSET);
194
195 return 0;
196}
197
198static void bcm_iproc_i2c_enable_disable(struct bcm_iproc_i2c_dev *iproc_i2c,
199 bool enable)
200{
201 u32 val;
202
203 val = readl(iproc_i2c->base + CFG_OFFSET);
204 if (enable)
205 val |= BIT(CFG_EN_SHIFT);
206 else
207 val &= ~BIT(CFG_EN_SHIFT);
208 writel(val, iproc_i2c->base + CFG_OFFSET);
209}
210
122static int bcm_iproc_i2c_check_status(struct bcm_iproc_i2c_dev *iproc_i2c, 211static int bcm_iproc_i2c_check_status(struct bcm_iproc_i2c_dev *iproc_i2c,
123 struct i2c_msg *msg) 212 struct i2c_msg *msg)
124{ 213{
@@ -149,6 +238,12 @@ static int bcm_iproc_i2c_check_status(struct bcm_iproc_i2c_dev *iproc_i2c,
149 238
150 default: 239 default:
151 dev_dbg(iproc_i2c->device, "unknown error code=%d\n", val); 240 dev_dbg(iproc_i2c->device, "unknown error code=%d\n", val);
241
242 /* re-initialize i2c for recovery */
243 bcm_iproc_i2c_enable_disable(iproc_i2c, false);
244 bcm_iproc_i2c_init(iproc_i2c);
245 bcm_iproc_i2c_enable_disable(iproc_i2c, true);
246
152 return -EIO; 247 return -EIO;
153 } 248 }
154} 249}
@@ -159,7 +254,8 @@ static int bcm_iproc_i2c_xfer_single_msg(struct bcm_iproc_i2c_dev *iproc_i2c,
159 int ret, i; 254 int ret, i;
160 u8 addr; 255 u8 addr;
161 u32 val; 256 u32 val;
162 unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT_MESC); 257 unsigned int tx_bytes;
258 unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT_MSEC);
163 259
164 /* check if bus is busy */ 260 /* check if bus is busy */
165 if (!!(readl(iproc_i2c->base + M_CMD_OFFSET) & 261 if (!!(readl(iproc_i2c->base + M_CMD_OFFSET) &
@@ -168,13 +264,20 @@ static int bcm_iproc_i2c_xfer_single_msg(struct bcm_iproc_i2c_dev *iproc_i2c,
168 return -EBUSY; 264 return -EBUSY;
169 } 265 }
170 266
267 iproc_i2c->msg = msg;
268
171 /* format and load slave address into the TX FIFO */ 269 /* format and load slave address into the TX FIFO */
172 addr = msg->addr << 1 | (msg->flags & I2C_M_RD ? 1 : 0); 270 addr = msg->addr << 1 | (msg->flags & I2C_M_RD ? 1 : 0);
173 writel(addr, iproc_i2c->base + M_TX_OFFSET); 271 writel(addr, iproc_i2c->base + M_TX_OFFSET);
174 272
175 /* for a write transaction, load data into the TX FIFO */ 273 /*
274 * For a write transaction, load data into the TX FIFO. Only allow
275 * loading up to TX FIFO size - 1 bytes of data since the first byte
276 * has been used up by the slave address
277 */
278 tx_bytes = min_t(unsigned int, msg->len, M_TX_RX_FIFO_SIZE - 1);
176 if (!(msg->flags & I2C_M_RD)) { 279 if (!(msg->flags & I2C_M_RD)) {
177 for (i = 0; i < msg->len; i++) { 280 for (i = 0; i < tx_bytes; i++) {
178 val = msg->buf[i]; 281 val = msg->buf[i];
179 282
180 /* mark the last byte */ 283 /* mark the last byte */
@@ -183,6 +286,7 @@ static int bcm_iproc_i2c_xfer_single_msg(struct bcm_iproc_i2c_dev *iproc_i2c,
183 286
184 writel(val, iproc_i2c->base + M_TX_OFFSET); 287 writel(val, iproc_i2c->base + M_TX_OFFSET);
185 } 288 }
289 iproc_i2c->tx_bytes = tx_bytes;
186 } 290 }
187 291
188 /* mark as incomplete before starting the transaction */ 292 /* mark as incomplete before starting the transaction */
@@ -194,13 +298,24 @@ static int bcm_iproc_i2c_xfer_single_msg(struct bcm_iproc_i2c_dev *iproc_i2c,
194 * transaction is done, i.e., the internal start_busy bit, transitions 298 * transaction is done, i.e., the internal start_busy bit, transitions
195 * from 1 to 0. 299 * from 1 to 0.
196 */ 300 */
197 writel(1 << IE_M_START_BUSY_SHIFT, iproc_i2c->base + IE_OFFSET); 301 val = BIT(IE_M_START_BUSY_SHIFT);
302
303 /*
304 * If TX data size is larger than the TX FIFO, need to enable TX
305 * underrun interrupt, which will be triggerred when the TX FIFO is
306 * empty. When that happens we can then pump more data into the FIFO
307 */
308 if (!(msg->flags & I2C_M_RD) &&
309 msg->len > iproc_i2c->tx_bytes)
310 val |= BIT(IE_M_TX_UNDERRUN_SHIFT);
311
312 writel(val, iproc_i2c->base + IE_OFFSET);
198 313
199 /* 314 /*
200 * Now we can activate the transfer. For a read operation, specify the 315 * Now we can activate the transfer. For a read operation, specify the
201 * number of bytes to read 316 * number of bytes to read
202 */ 317 */
203 val = 1 << M_CMD_START_BUSY_SHIFT; 318 val = BIT(M_CMD_START_BUSY_SHIFT);
204 if (msg->flags & I2C_M_RD) { 319 if (msg->flags & I2C_M_RD) {
205 val |= (M_CMD_PROTOCOL_BLK_RD << M_CMD_PROTOCOL_SHIFT) | 320 val |= (M_CMD_PROTOCOL_BLK_RD << M_CMD_PROTOCOL_SHIFT) |
206 (msg->len << M_CMD_RD_CNT_SHIFT); 321 (msg->len << M_CMD_RD_CNT_SHIFT);
@@ -283,7 +398,6 @@ static const struct i2c_algorithm bcm_iproc_algo = {
283static struct i2c_adapter_quirks bcm_iproc_i2c_quirks = { 398static struct i2c_adapter_quirks bcm_iproc_i2c_quirks = {
284 /* need to reserve one byte in the FIFO for the slave address */ 399 /* need to reserve one byte in the FIFO for the slave address */
285 .max_read_len = M_TX_RX_FIFO_SIZE - 1, 400 .max_read_len = M_TX_RX_FIFO_SIZE - 1,
286 .max_write_len = M_TX_RX_FIFO_SIZE - 1,
287}; 401};
288 402
289static int bcm_iproc_i2c_cfg_speed(struct bcm_iproc_i2c_dev *iproc_i2c) 403static int bcm_iproc_i2c_cfg_speed(struct bcm_iproc_i2c_dev *iproc_i2c)
@@ -321,49 +435,6 @@ static int bcm_iproc_i2c_cfg_speed(struct bcm_iproc_i2c_dev *iproc_i2c)
321 return 0; 435 return 0;
322} 436}
323 437
324static int bcm_iproc_i2c_init(struct bcm_iproc_i2c_dev *iproc_i2c)
325{
326 u32 val;
327
328 /* put controller in reset */
329 val = readl(iproc_i2c->base + CFG_OFFSET);
330 val |= 1 << CFG_RESET_SHIFT;
331 val &= ~(1 << CFG_EN_SHIFT);
332 writel(val, iproc_i2c->base + CFG_OFFSET);
333
334 /* wait 100 usec per spec */
335 udelay(100);
336
337 /* bring controller out of reset */
338 val &= ~(1 << CFG_RESET_SHIFT);
339 writel(val, iproc_i2c->base + CFG_OFFSET);
340
341 /* flush TX/RX FIFOs and set RX FIFO threshold to zero */
342 val = (1 << M_FIFO_RX_FLUSH_SHIFT) | (1 << M_FIFO_TX_FLUSH_SHIFT);
343 writel(val, iproc_i2c->base + M_FIFO_CTRL_OFFSET);
344
345 /* disable all interrupts */
346 writel(0, iproc_i2c->base + IE_OFFSET);
347
348 /* clear all pending interrupts */
349 writel(0xffffffff, iproc_i2c->base + IS_OFFSET);
350
351 return 0;
352}
353
354static void bcm_iproc_i2c_enable_disable(struct bcm_iproc_i2c_dev *iproc_i2c,
355 bool enable)
356{
357 u32 val;
358
359 val = readl(iproc_i2c->base + CFG_OFFSET);
360 if (enable)
361 val |= BIT(CFG_EN_SHIFT);
362 else
363 val &= ~BIT(CFG_EN_SHIFT);
364 writel(val, iproc_i2c->base + CFG_OFFSET);
365}
366
367static int bcm_iproc_i2c_probe(struct platform_device *pdev) 438static int bcm_iproc_i2c_probe(struct platform_device *pdev)
368{ 439{
369 int irq, ret = 0; 440 int irq, ret = 0;
diff --git a/drivers/i2c/busses/i2c-cadence.c b/drivers/i2c/busses/i2c-cadence.c
index 6b08d1607b7a..90bbd9f9dd8f 100644
--- a/drivers/i2c/busses/i2c-cadence.c
+++ b/drivers/i2c/busses/i2c-cadence.c
@@ -124,6 +124,8 @@
124 124
125/** 125/**
126 * struct cdns_i2c - I2C device private data structure 126 * struct cdns_i2c - I2C device private data structure
127 *
128 * @dev: Pointer to device structure
127 * @membase: Base address of the I2C device 129 * @membase: Base address of the I2C device
128 * @adap: I2C adapter instance 130 * @adap: I2C adapter instance
129 * @p_msg: Message pointer 131 * @p_msg: Message pointer
@@ -171,7 +173,7 @@ struct cdns_platform_data {
171 clk_rate_change_nb) 173 clk_rate_change_nb)
172 174
173/** 175/**
174 * cdns_i2c_clear_bus_hold() - Clear bus hold bit 176 * cdns_i2c_clear_bus_hold - Clear bus hold bit
175 * @id: Pointer to driver data struct 177 * @id: Pointer to driver data struct
176 * 178 *
177 * Helper to clear the controller's bus hold bit. 179 * Helper to clear the controller's bus hold bit.
@@ -815,8 +817,8 @@ static int cdns_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
815} 817}
816 818
817/** 819/**
818 * cdns_i2c_suspend - Suspend method for the driver 820 * cdns_i2c_runtime_suspend - Runtime suspend method for the driver
819 * @_dev: Address of the platform_device structure 821 * @dev: Address of the platform_device structure
820 * 822 *
821 * Put the driver into low power mode. 823 * Put the driver into low power mode.
822 * 824 *
@@ -833,10 +835,10 @@ static int __maybe_unused cdns_i2c_runtime_suspend(struct device *dev)
833} 835}
834 836
835/** 837/**
836 * cdns_i2c_resume - Resume from suspend 838 * cdns_i2c_runtime_resume - Runtime resume
837 * @_dev: Address of the platform_device structure 839 * @dev: Address of the platform_device structure
838 * 840 *
839 * Resume operation after suspend. 841 * Runtime resume callback.
840 * 842 *
841 * Return: 0 on success and error value on error 843 * Return: 0 on success and error value on error
842 */ 844 */
diff --git a/drivers/i2c/busses/i2c-designware-baytrail.c b/drivers/i2c/busses/i2c-designware-baytrail.c
index e38c2bbba940..1590ad0a8081 100644
--- a/drivers/i2c/busses/i2c-designware-baytrail.c
+++ b/drivers/i2c/busses/i2c-designware-baytrail.c
@@ -11,7 +11,6 @@
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details. 12 * more details.
13 */ 13 */
14#include <linux/module.h>
15#include <linux/delay.h> 14#include <linux/delay.h>
16#include <linux/device.h> 15#include <linux/device.h>
17#include <linux/acpi.h> 16#include <linux/acpi.h>
@@ -151,7 +150,3 @@ int i2c_dw_eval_lock_support(struct dw_i2c_dev *dev)
151 150
152 return 0; 151 return 0;
153} 152}
154
155MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
156MODULE_DESCRIPTION("Baytrail I2C Semaphore driver");
157MODULE_LICENSE("GPL v2");
diff --git a/drivers/i2c/busses/i2c-designware-core.c b/drivers/i2c/busses/i2c-designware-core.c
index 10fbd6d841e0..99b54be6ba73 100644
--- a/drivers/i2c/busses/i2c-designware-core.c
+++ b/drivers/i2c/busses/i2c-designware-core.c
@@ -634,7 +634,6 @@ i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
634 634
635 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); 635 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
636 636
637 mutex_lock(&dev->lock);
638 pm_runtime_get_sync(dev->dev); 637 pm_runtime_get_sync(dev->dev);
639 638
640 reinit_completion(&dev->cmd_complete); 639 reinit_completion(&dev->cmd_complete);
@@ -673,11 +672,12 @@ i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
673 } 672 }
674 673
675 /* 674 /*
676 * We must disable the adapter before unlocking the &dev->lock mutex 675 * We must disable the adapter before returning and signaling the end
677 * below. Otherwise the hardware might continue generating interrupts 676 * of the current transfer. Otherwise the hardware might continue
678 * which in turn causes a race condition with the following transfer. 677 * generating interrupts which in turn causes a race condition with
679 * Needs some more investigation if the additional interrupts are 678 * the following transfer. Needs some more investigation if the
680 * a hardware bug or this driver doesn't handle them correctly yet. 679 * additional interrupts are a hardware bug or this driver doesn't
680 * handle them correctly yet.
681 */ 681 */
682 __i2c_dw_enable(dev, false); 682 __i2c_dw_enable(dev, false);
683 683
@@ -706,7 +706,6 @@ done:
706done_nolock: 706done_nolock:
707 pm_runtime_mark_last_busy(dev->dev); 707 pm_runtime_mark_last_busy(dev->dev);
708 pm_runtime_put_autosuspend(dev->dev); 708 pm_runtime_put_autosuspend(dev->dev);
709 mutex_unlock(&dev->lock);
710 709
711 return ret; 710 return ret;
712} 711}
@@ -860,7 +859,6 @@ int i2c_dw_probe(struct dw_i2c_dev *dev)
860 int r; 859 int r;
861 860
862 init_completion(&dev->cmd_complete); 861 init_completion(&dev->cmd_complete);
863 mutex_init(&dev->lock);
864 862
865 r = i2c_dw_init(dev); 863 r = i2c_dw_init(dev);
866 if (r) 864 if (r)
@@ -883,9 +881,17 @@ int i2c_dw_probe(struct dw_i2c_dev *dev)
883 return r; 881 return r;
884 } 882 }
885 883
884 /*
885 * Increment PM usage count during adapter registration in order to
886 * avoid possible spurious runtime suspend when adapter device is
887 * registered to the device core and immediate resume in case bus has
888 * registered I2C slaves that do I2C transfers in their probe.
889 */
890 pm_runtime_get_noresume(dev->dev);
886 r = i2c_add_numbered_adapter(adap); 891 r = i2c_add_numbered_adapter(adap);
887 if (r) 892 if (r)
888 dev_err(dev->dev, "failure adding adapter: %d\n", r); 893 dev_err(dev->dev, "failure adding adapter: %d\n", r);
894 pm_runtime_put_noidle(dev->dev);
889 895
890 return r; 896 return r;
891} 897}
diff --git a/drivers/i2c/busses/i2c-designware-core.h b/drivers/i2c/busses/i2c-designware-core.h
index 9ffb63a60f95..cd409e7fbc71 100644
--- a/drivers/i2c/busses/i2c-designware-core.h
+++ b/drivers/i2c/busses/i2c-designware-core.h
@@ -36,7 +36,6 @@
36 * @dev: driver model device node 36 * @dev: driver model device node
37 * @base: IO registers pointer 37 * @base: IO registers pointer
38 * @cmd_complete: tx completion indicator 38 * @cmd_complete: tx completion indicator
39 * @lock: protect this struct and IO registers
40 * @clk: input reference clock 39 * @clk: input reference clock
41 * @cmd_err: run time hadware error code 40 * @cmd_err: run time hadware error code
42 * @msgs: points to an array of messages currently being transfered 41 * @msgs: points to an array of messages currently being transfered
@@ -73,7 +72,6 @@ struct dw_i2c_dev {
73 struct device *dev; 72 struct device *dev;
74 void __iomem *base; 73 void __iomem *base;
75 struct completion cmd_complete; 74 struct completion cmd_complete;
76 struct mutex lock;
77 struct clk *clk; 75 struct clk *clk;
78 u32 (*get_clk_rate_khz) (struct dw_i2c_dev *dev); 76 u32 (*get_clk_rate_khz) (struct dw_i2c_dev *dev);
79 struct dw_pci_controller *controller; 77 struct dw_pci_controller *controller;
diff --git a/drivers/i2c/busses/i2c-i801.c b/drivers/i2c/busses/i2c-i801.c
index 27fa0cb09538..585a3b7915bd 100644
--- a/drivers/i2c/busses/i2c-i801.c
+++ b/drivers/i2c/busses/i2c-i801.c
@@ -184,7 +184,7 @@
184 184
185/* Older devices have their ID defined in <linux/pci_ids.h> */ 185/* Older devices have their ID defined in <linux/pci_ids.h> */
186#define PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS 0x0f12 186#define PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS 0x0f12
187#define PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS 0x2292 187#define PCI_DEVICE_ID_INTEL_DNV_SMBUS 0x19df
188#define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22 188#define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
189#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22 189#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
190/* Patsburg also has three 'Integrated Device Function' SMBus controllers */ 190/* Patsburg also has three 'Integrated Device Function' SMBus controllers */
@@ -193,9 +193,11 @@
193#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72 193#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS_IDF2 0x1d72
194#define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22 194#define PCI_DEVICE_ID_INTEL_PANTHERPOINT_SMBUS 0x1e22
195#define PCI_DEVICE_ID_INTEL_AVOTON_SMBUS 0x1f3c 195#define PCI_DEVICE_ID_INTEL_AVOTON_SMBUS 0x1f3c
196#define PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS 0x2292
196#define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330 197#define PCI_DEVICE_ID_INTEL_DH89XXCC_SMBUS 0x2330
197#define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0 198#define PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS 0x23b0
198#define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30 199#define PCI_DEVICE_ID_INTEL_5_3400_SERIES_SMBUS 0x3b30
200#define PCI_DEVICE_ID_INTEL_BROXTON_SMBUS 0x5ad4
199#define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22 201#define PCI_DEVICE_ID_INTEL_LYNXPOINT_SMBUS 0x8c22
200#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS 0x8ca2 202#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS 0x8ca2
201#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS 0x8d22 203#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS 0x8d22
@@ -204,10 +206,8 @@
204#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f 206#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f
205#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22 207#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
206#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS 0x9ca2 208#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS 0x9ca2
207#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS 0xa123
208#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS 0x9d23 209#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS 0x9d23
209#define PCI_DEVICE_ID_INTEL_DNV_SMBUS 0x19df 210#define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS 0xa123
210#define PCI_DEVICE_ID_INTEL_BROXTON_SMBUS 0x5ad4
211#define PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS 0xa1a3 211#define PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS 0xa1a3
212#define PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS 0xa223 212#define PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS 0xa223
213 213
diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index a2b132cef717..1ca7ef2314f7 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -212,7 +212,7 @@ struct imx_i2c_struct {
212 struct imx_i2c_dma *dma; 212 struct imx_i2c_dma *dma;
213}; 213};
214 214
215static const struct imx_i2c_hwdata imx1_i2c_hwdata = { 215static const struct imx_i2c_hwdata imx1_i2c_hwdata = {
216 .devtype = IMX1_I2C, 216 .devtype = IMX1_I2C,
217 .regshift = IMX_I2C_REGSHIFT, 217 .regshift = IMX_I2C_REGSHIFT,
218 .clk_div = imx_i2c_clk_div, 218 .clk_div = imx_i2c_clk_div,
@@ -222,7 +222,7 @@ static const struct imx_i2c_hwdata imx1_i2c_hwdata = {
222 222
223}; 223};
224 224
225static const struct imx_i2c_hwdata imx21_i2c_hwdata = { 225static const struct imx_i2c_hwdata imx21_i2c_hwdata = {
226 .devtype = IMX21_I2C, 226 .devtype = IMX21_I2C,
227 .regshift = IMX_I2C_REGSHIFT, 227 .regshift = IMX_I2C_REGSHIFT,
228 .clk_div = imx_i2c_clk_div, 228 .clk_div = imx_i2c_clk_div,
@@ -871,7 +871,7 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bo
871 if ((!i) && block_data) 871 if ((!i) && block_data)
872 msgs->buf[0] = len; 872 msgs->buf[0] = len;
873 else 873 else
874 msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); 874 msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
875 dev_dbg(&i2c_imx->adapter.dev, 875 dev_dbg(&i2c_imx->adapter.dev,
876 "<%s> read byte: B%d=0x%X\n", 876 "<%s> read byte: B%d=0x%X\n",
877 __func__, i, msgs->buf[i]); 877 __func__, i, msgs->buf[i]);
@@ -916,7 +916,7 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter,
916 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); 916 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
917 temp |= I2CR_RSTA; 917 temp |= I2CR_RSTA;
918 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); 918 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
919 result = i2c_imx_bus_busy(i2c_imx, 1); 919 result = i2c_imx_bus_busy(i2c_imx, 1);
920 if (result) 920 if (result)
921 goto fail0; 921 goto fail0;
922 } 922 }
@@ -1192,7 +1192,7 @@ static int i2c_imx_remove(struct platform_device *pdev)
1192#ifdef CONFIG_PM 1192#ifdef CONFIG_PM
1193static int i2c_imx_runtime_suspend(struct device *dev) 1193static int i2c_imx_runtime_suspend(struct device *dev)
1194{ 1194{
1195 struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev); 1195 struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev);
1196 1196
1197 clk_disable_unprepare(i2c_imx->clk); 1197 clk_disable_unprepare(i2c_imx->clk);
1198 1198
@@ -1201,7 +1201,7 @@ static int i2c_imx_runtime_suspend(struct device *dev)
1201 1201
1202static int i2c_imx_runtime_resume(struct device *dev) 1202static int i2c_imx_runtime_resume(struct device *dev)
1203{ 1203{
1204 struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev); 1204 struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev);
1205 int ret; 1205 int ret;
1206 1206
1207 ret = clk_prepare_enable(i2c_imx->clk); 1207 ret = clk_prepare_enable(i2c_imx->clk);
diff --git a/drivers/i2c/busses/i2c-mt65xx.c b/drivers/i2c/busses/i2c-mt65xx.c
index aec8e6ce38a4..453358b4d9ca 100644
--- a/drivers/i2c/busses/i2c-mt65xx.c
+++ b/drivers/i2c/busses/i2c-mt65xx.c
@@ -60,6 +60,7 @@
60#define I2C_DMA_INT_FLAG_NONE 0x0000 60#define I2C_DMA_INT_FLAG_NONE 0x0000
61#define I2C_DMA_CLR_FLAG 0x0000 61#define I2C_DMA_CLR_FLAG 0x0000
62#define I2C_DMA_HARD_RST 0x0002 62#define I2C_DMA_HARD_RST 0x0002
63#define I2C_DMA_4G_MODE 0x0001
63 64
64#define I2C_DEFAULT_SPEED 100000 /* hz */ 65#define I2C_DEFAULT_SPEED 100000 /* hz */
65#define MAX_FS_MODE_SPEED 400000 66#define MAX_FS_MODE_SPEED 400000
@@ -88,6 +89,8 @@ enum DMA_REGS_OFFSET {
88 OFFSET_RX_MEM_ADDR = 0x20, 89 OFFSET_RX_MEM_ADDR = 0x20,
89 OFFSET_TX_LEN = 0x24, 90 OFFSET_TX_LEN = 0x24,
90 OFFSET_RX_LEN = 0x28, 91 OFFSET_RX_LEN = 0x28,
92 OFFSET_TX_4G_MODE = 0x54,
93 OFFSET_RX_4G_MODE = 0x58,
91}; 94};
92 95
93enum i2c_trans_st_rs { 96enum i2c_trans_st_rs {
@@ -133,6 +136,7 @@ struct mtk_i2c_compatible {
133 unsigned char dcm: 1; 136 unsigned char dcm: 1;
134 unsigned char auto_restart: 1; 137 unsigned char auto_restart: 1;
135 unsigned char aux_len_reg: 1; 138 unsigned char aux_len_reg: 1;
139 unsigned char support_33bits: 1;
136}; 140};
137 141
138struct mtk_i2c { 142struct mtk_i2c {
@@ -182,6 +186,7 @@ static const struct mtk_i2c_compatible mt6577_compat = {
182 .dcm = 1, 186 .dcm = 1,
183 .auto_restart = 0, 187 .auto_restart = 0,
184 .aux_len_reg = 0, 188 .aux_len_reg = 0,
189 .support_33bits = 0,
185}; 190};
186 191
187static const struct mtk_i2c_compatible mt6589_compat = { 192static const struct mtk_i2c_compatible mt6589_compat = {
@@ -190,6 +195,7 @@ static const struct mtk_i2c_compatible mt6589_compat = {
190 .dcm = 0, 195 .dcm = 0,
191 .auto_restart = 0, 196 .auto_restart = 0,
192 .aux_len_reg = 0, 197 .aux_len_reg = 0,
198 .support_33bits = 0,
193}; 199};
194 200
195static const struct mtk_i2c_compatible mt8173_compat = { 201static const struct mtk_i2c_compatible mt8173_compat = {
@@ -198,6 +204,7 @@ static const struct mtk_i2c_compatible mt8173_compat = {
198 .dcm = 1, 204 .dcm = 1,
199 .auto_restart = 1, 205 .auto_restart = 1,
200 .aux_len_reg = 1, 206 .aux_len_reg = 1,
207 .support_33bits = 1,
201}; 208};
202 209
203static const struct of_device_id mtk_i2c_of_match[] = { 210static const struct of_device_id mtk_i2c_of_match[] = {
@@ -366,6 +373,11 @@ static int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk,
366 return 0; 373 return 0;
367} 374}
368 375
376static inline u32 mtk_i2c_set_4g_mode(dma_addr_t addr)
377{
378 return (addr & BIT_ULL(32)) ? I2C_DMA_4G_MODE : I2C_DMA_CLR_FLAG;
379}
380
369static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, 381static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs,
370 int num, int left_num) 382 int num, int left_num)
371{ 383{
@@ -373,6 +385,7 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs,
373 u16 start_reg; 385 u16 start_reg;
374 u16 control_reg; 386 u16 control_reg;
375 u16 restart_flag = 0; 387 u16 restart_flag = 0;
388 u32 reg_4g_mode;
376 dma_addr_t rpaddr = 0; 389 dma_addr_t rpaddr = 0;
377 dma_addr_t wpaddr = 0; 390 dma_addr_t wpaddr = 0;
378 int ret; 391 int ret;
@@ -439,6 +452,12 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs,
439 msgs->len, DMA_FROM_DEVICE); 452 msgs->len, DMA_FROM_DEVICE);
440 if (dma_mapping_error(i2c->dev, rpaddr)) 453 if (dma_mapping_error(i2c->dev, rpaddr))
441 return -ENOMEM; 454 return -ENOMEM;
455
456 if (i2c->dev_comp->support_33bits) {
457 reg_4g_mode = mtk_i2c_set_4g_mode(rpaddr);
458 writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE);
459 }
460
442 writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 461 writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR);
443 writel(msgs->len, i2c->pdmabase + OFFSET_RX_LEN); 462 writel(msgs->len, i2c->pdmabase + OFFSET_RX_LEN);
444 } else if (i2c->op == I2C_MASTER_WR) { 463 } else if (i2c->op == I2C_MASTER_WR) {
@@ -448,6 +467,12 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs,
448 msgs->len, DMA_TO_DEVICE); 467 msgs->len, DMA_TO_DEVICE);
449 if (dma_mapping_error(i2c->dev, wpaddr)) 468 if (dma_mapping_error(i2c->dev, wpaddr))
450 return -ENOMEM; 469 return -ENOMEM;
470
471 if (i2c->dev_comp->support_33bits) {
472 reg_4g_mode = mtk_i2c_set_4g_mode(wpaddr);
473 writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE);
474 }
475
451 writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 476 writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR);
452 writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 477 writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN);
453 } else { 478 } else {
@@ -465,6 +490,15 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs,
465 msgs->len, DMA_TO_DEVICE); 490 msgs->len, DMA_TO_DEVICE);
466 return -ENOMEM; 491 return -ENOMEM;
467 } 492 }
493
494 if (i2c->dev_comp->support_33bits) {
495 reg_4g_mode = mtk_i2c_set_4g_mode(wpaddr);
496 writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE);
497
498 reg_4g_mode = mtk_i2c_set_4g_mode(rpaddr);
499 writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE);
500 }
501
468 writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR); 502 writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR);
469 writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR); 503 writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR);
470 writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN); 504 writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN);
@@ -729,6 +763,14 @@ static int mtk_i2c_probe(struct platform_device *pdev)
729 return -EINVAL; 763 return -EINVAL;
730 } 764 }
731 765
766 if (i2c->dev_comp->support_33bits) {
767 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(33));
768 if (ret) {
769 dev_err(&pdev->dev, "dma_set_mask return error.\n");
770 return ret;
771 }
772 }
773
732 ret = mtk_i2c_clock_enable(i2c); 774 ret = mtk_i2c_clock_enable(i2c);
733 if (ret) { 775 if (ret) {
734 dev_err(&pdev->dev, "clock enable failed!\n"); 776 dev_err(&pdev->dev, "clock enable failed!\n");
diff --git a/drivers/i2c/busses/i2c-octeon.c b/drivers/i2c/busses/i2c-octeon.c
index 32914ab42a19..46fb6c42934f 100644
--- a/drivers/i2c/busses/i2c-octeon.c
+++ b/drivers/i2c/busses/i2c-octeon.c
@@ -2,7 +2,7 @@
2 * (C) Copyright 2009-2010 2 * (C) Copyright 2009-2010
3 * Nokia Siemens Networks, michael.lawnick.ext@nsn.com 3 * Nokia Siemens Networks, michael.lawnick.ext@nsn.com
4 * 4 *
5 * Portions Copyright (C) 2010, 2011 Cavium Networks, Inc. 5 * Portions Copyright (C) 2010 - 2016 Cavium, Inc.
6 * 6 *
7 * This is a driver for the i2c adapter in Cavium Networks' OCTEON processors. 7 * This is a driver for the i2c adapter in Cavium Networks' OCTEON processors.
8 * 8 *
@@ -26,39 +26,48 @@
26 26
27#define DRV_NAME "i2c-octeon" 27#define DRV_NAME "i2c-octeon"
28 28
29/* The previous out-of-tree version was implicitly version 1.0. */ 29/* Register offsets */
30#define DRV_VERSION "2.0" 30#define SW_TWSI 0x00
31 31#define TWSI_INT 0x10
32/* register offsets */
33#define SW_TWSI 0x00
34#define TWSI_INT 0x10
35 32
36/* Controller command patterns */ 33/* Controller command patterns */
37#define SW_TWSI_V 0x8000000000000000ull 34#define SW_TWSI_V BIT_ULL(63) /* Valid bit */
38#define SW_TWSI_EOP_TWSI_DATA 0x0C00000100000000ull 35#define SW_TWSI_R BIT_ULL(56) /* Result or read bit */
39#define SW_TWSI_EOP_TWSI_CTL 0x0C00000200000000ull 36
40#define SW_TWSI_EOP_TWSI_CLKCTL 0x0C00000300000000ull 37/* Controller opcode word (bits 60:57) */
41#define SW_TWSI_EOP_TWSI_STAT 0x0C00000300000000ull 38#define SW_TWSI_OP_SHIFT 57
42#define SW_TWSI_EOP_TWSI_RST 0x0C00000700000000ull 39#define SW_TWSI_OP_TWSI_CLK (4ULL << SW_TWSI_OP_SHIFT)
43#define SW_TWSI_OP_TWSI_CLK 0x0800000000000000ull 40#define SW_TWSI_OP_EOP (6ULL << SW_TWSI_OP_SHIFT) /* Extended opcode */
44#define SW_TWSI_R 0x0100000000000000ull 41
42/* Controller extended opcode word (bits 34:32) */
43#define SW_TWSI_EOP_SHIFT 32
44#define SW_TWSI_EOP_TWSI_DATA (SW_TWSI_OP_EOP | 1ULL << SW_TWSI_EOP_SHIFT)
45#define SW_TWSI_EOP_TWSI_CTL (SW_TWSI_OP_EOP | 2ULL << SW_TWSI_EOP_SHIFT)
46#define SW_TWSI_EOP_TWSI_CLKCTL (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
47#define SW_TWSI_EOP_TWSI_STAT (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
48#define SW_TWSI_EOP_TWSI_RST (SW_TWSI_OP_EOP | 7ULL << SW_TWSI_EOP_SHIFT)
45 49
46/* Controller command and status bits */ 50/* Controller command and status bits */
47#define TWSI_CTL_CE 0x80 51#define TWSI_CTL_CE 0x80
48#define TWSI_CTL_ENAB 0x40 52#define TWSI_CTL_ENAB 0x40 /* Bus enable */
49#define TWSI_CTL_STA 0x20 53#define TWSI_CTL_STA 0x20 /* Master-mode start, HW clears when done */
50#define TWSI_CTL_STP 0x10 54#define TWSI_CTL_STP 0x10 /* Master-mode stop, HW clears when done */
51#define TWSI_CTL_IFLG 0x08 55#define TWSI_CTL_IFLG 0x08 /* HW event, SW writes 0 to ACK */
52#define TWSI_CTL_AAK 0x04 56#define TWSI_CTL_AAK 0x04 /* Assert ACK */
53 57
54/* Some status values */ 58/* Some status values */
55#define STAT_START 0x08 59#define STAT_START 0x08
56#define STAT_RSTART 0x10 60#define STAT_RSTART 0x10
57#define STAT_TXADDR_ACK 0x18 61#define STAT_TXADDR_ACK 0x18
58#define STAT_TXDATA_ACK 0x28 62#define STAT_TXDATA_ACK 0x28
59#define STAT_RXADDR_ACK 0x40 63#define STAT_RXADDR_ACK 0x40
60#define STAT_RXDATA_ACK 0x50 64#define STAT_RXDATA_ACK 0x50
61#define STAT_IDLE 0xF8 65#define STAT_IDLE 0xF8
66
67/* TWSI_INT values */
68#define TWSI_INT_CORE_EN BIT_ULL(6)
69#define TWSI_INT_SDA_OVR BIT_ULL(8)
70#define TWSI_INT_SCL_OVR BIT_ULL(9)
62 71
63struct octeon_i2c { 72struct octeon_i2c {
64 wait_queue_head_t queue; 73 wait_queue_head_t queue;
@@ -66,23 +75,19 @@ struct octeon_i2c {
66 int irq; 75 int irq;
67 u32 twsi_freq; 76 u32 twsi_freq;
68 int sys_freq; 77 int sys_freq;
69 resource_size_t twsi_phys;
70 void __iomem *twsi_base; 78 void __iomem *twsi_base;
71 resource_size_t regsize;
72 struct device *dev; 79 struct device *dev;
73}; 80};
74 81
75/** 82/**
76 * octeon_i2c_write_sw - write an I2C core register. 83 * octeon_i2c_write_sw - write an I2C core register
77 * @i2c: The struct octeon_i2c. 84 * @i2c: The struct octeon_i2c
78 * @eop_reg: Register selector. 85 * @eop_reg: Register selector
79 * @data: Value to be written. 86 * @data: Value to be written
80 * 87 *
81 * The I2C core registers are accessed indirectly via the SW_TWSI CSR. 88 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
82 */ 89 */
83static void octeon_i2c_write_sw(struct octeon_i2c *i2c, 90static void octeon_i2c_write_sw(struct octeon_i2c *i2c, u64 eop_reg, u8 data)
84 u64 eop_reg,
85 u8 data)
86{ 91{
87 u64 tmp; 92 u64 tmp;
88 93
@@ -93,9 +98,9 @@ static void octeon_i2c_write_sw(struct octeon_i2c *i2c,
93} 98}
94 99
95/** 100/**
96 * octeon_i2c_read_sw - write an I2C core register. 101 * octeon_i2c_read_sw - read lower bits of an I2C core register
97 * @i2c: The struct octeon_i2c. 102 * @i2c: The struct octeon_i2c
98 * @eop_reg: Register selector. 103 * @eop_reg: Register selector
99 * 104 *
100 * Returns the data. 105 * Returns the data.
101 * 106 *
@@ -115,8 +120,8 @@ static u8 octeon_i2c_read_sw(struct octeon_i2c *i2c, u64 eop_reg)
115 120
116/** 121/**
117 * octeon_i2c_write_int - write the TWSI_INT register 122 * octeon_i2c_write_int - write the TWSI_INT register
118 * @i2c: The struct octeon_i2c. 123 * @i2c: The struct octeon_i2c
119 * @data: Value to be written. 124 * @data: Value to be written
120 */ 125 */
121static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data) 126static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
122{ 127{
@@ -125,57 +130,52 @@ static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
125} 130}
126 131
127/** 132/**
128 * octeon_i2c_int_enable - enable the TS interrupt. 133 * octeon_i2c_int_enable - enable the CORE interrupt
129 * @i2c: The struct octeon_i2c. 134 * @i2c: The struct octeon_i2c
130 * 135 *
131 * The interrupt will be asserted when there is non-STAT_IDLE state in 136 * The interrupt will be asserted when there is non-STAT_IDLE state in
132 * the SW_TWSI_EOP_TWSI_STAT register. 137 * the SW_TWSI_EOP_TWSI_STAT register.
133 */ 138 */
134static void octeon_i2c_int_enable(struct octeon_i2c *i2c) 139static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
135{ 140{
136 octeon_i2c_write_int(i2c, 0x40); 141 octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
137} 142}
138 143
139/** 144/* disable the CORE interrupt */
140 * octeon_i2c_int_disable - disable the TS interrupt.
141 * @i2c: The struct octeon_i2c.
142 */
143static void octeon_i2c_int_disable(struct octeon_i2c *i2c) 145static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
144{ 146{
147 /* clear TS/ST/IFLG events */
145 octeon_i2c_write_int(i2c, 0); 148 octeon_i2c_write_int(i2c, 0);
146} 149}
147 150
148/** 151/**
149 * octeon_i2c_unblock - unblock the bus. 152 * octeon_i2c_unblock - unblock the bus
150 * @i2c: The struct octeon_i2c. 153 * @i2c: The struct octeon_i2c
151 * 154 *
152 * If there was a reset while a device was driving 0 to bus, 155 * If there was a reset while a device was driving 0 to bus, bus is blocked.
153 * bus is blocked. We toggle it free manually by some clock 156 * We toggle it free manually by some clock cycles and send a stop.
154 * cycles and send a stop.
155 */ 157 */
156static void octeon_i2c_unblock(struct octeon_i2c *i2c) 158static void octeon_i2c_unblock(struct octeon_i2c *i2c)
157{ 159{
158 int i; 160 int i;
159 161
160 dev_dbg(i2c->dev, "%s\n", __func__); 162 dev_dbg(i2c->dev, "%s\n", __func__);
163
161 for (i = 0; i < 9; i++) { 164 for (i = 0; i < 9; i++) {
162 octeon_i2c_write_int(i2c, 0x0); 165 octeon_i2c_write_int(i2c, 0);
163 udelay(5); 166 udelay(5);
164 octeon_i2c_write_int(i2c, 0x200); 167 octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
165 udelay(5); 168 udelay(5);
166 } 169 }
167 octeon_i2c_write_int(i2c, 0x300); 170 /* hand-crank a STOP */
171 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR | TWSI_INT_SCL_OVR);
168 udelay(5); 172 udelay(5);
169 octeon_i2c_write_int(i2c, 0x100); 173 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR);
170 udelay(5); 174 udelay(5);
171 octeon_i2c_write_int(i2c, 0x0); 175 octeon_i2c_write_int(i2c, 0);
172} 176}
173 177
174/** 178/* interrupt service routine */
175 * octeon_i2c_isr - the interrupt service routine.
176 * @int: The irq, unused.
177 * @dev_id: Our struct octeon_i2c.
178 */
179static irqreturn_t octeon_i2c_isr(int irq, void *dev_id) 179static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
180{ 180{
181 struct octeon_i2c *i2c = dev_id; 181 struct octeon_i2c *i2c = dev_id;
@@ -193,24 +193,20 @@ static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
193} 193}
194 194
195/** 195/**
196 * octeon_i2c_wait - wait for the IFLG to be set. 196 * octeon_i2c_wait - wait for the IFLG to be set
197 * @i2c: The struct octeon_i2c. 197 * @i2c: The struct octeon_i2c
198 * 198 *
199 * Returns 0 on success, otherwise a negative errno. 199 * Returns 0 on success, otherwise a negative errno.
200 */ 200 */
201static int octeon_i2c_wait(struct octeon_i2c *i2c) 201static int octeon_i2c_wait(struct octeon_i2c *i2c)
202{ 202{
203 long result; 203 long time_left;
204 204
205 octeon_i2c_int_enable(i2c); 205 octeon_i2c_int_enable(i2c);
206 206 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
207 result = wait_event_timeout(i2c->queue, 207 i2c->adap.timeout);
208 octeon_i2c_test_iflg(i2c),
209 i2c->adap.timeout);
210
211 octeon_i2c_int_disable(i2c); 208 octeon_i2c_int_disable(i2c);
212 209 if (!time_left) {
213 if (result == 0) {
214 dev_dbg(i2c->dev, "%s: timeout\n", __func__); 210 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
215 return -ETIMEDOUT; 211 return -ETIMEDOUT;
216 } 212 }
@@ -219,18 +215,18 @@ static int octeon_i2c_wait(struct octeon_i2c *i2c)
219} 215}
220 216
221/** 217/**
222 * octeon_i2c_start - send START to the bus. 218 * octeon_i2c_start - send START to the bus
223 * @i2c: The struct octeon_i2c. 219 * @i2c: The struct octeon_i2c
224 * 220 *
225 * Returns 0 on success, otherwise a negative errno. 221 * Returns 0 on success, otherwise a negative errno.
226 */ 222 */
227static int octeon_i2c_start(struct octeon_i2c *i2c) 223static int octeon_i2c_start(struct octeon_i2c *i2c)
228{ 224{
229 u8 data;
230 int result; 225 int result;
226 u8 data;
231 227
232 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, 228 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
233 TWSI_CTL_ENAB | TWSI_CTL_STA); 229 TWSI_CTL_ENAB | TWSI_CTL_STA);
234 230
235 result = octeon_i2c_wait(i2c); 231 result = octeon_i2c_wait(i2c);
236 if (result) { 232 if (result) {
@@ -243,7 +239,6 @@ static int octeon_i2c_start(struct octeon_i2c *i2c)
243 octeon_i2c_unblock(i2c); 239 octeon_i2c_unblock(i2c);
244 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, 240 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
245 TWSI_CTL_ENAB | TWSI_CTL_STA); 241 TWSI_CTL_ENAB | TWSI_CTL_STA);
246
247 result = octeon_i2c_wait(i2c); 242 result = octeon_i2c_wait(i2c);
248 } 243 }
249 if (result) 244 if (result)
@@ -259,34 +254,19 @@ static int octeon_i2c_start(struct octeon_i2c *i2c)
259 return 0; 254 return 0;
260} 255}
261 256
262/** 257/* send STOP to the bus */
263 * octeon_i2c_stop - send STOP to the bus. 258static void octeon_i2c_stop(struct octeon_i2c *i2c)
264 * @i2c: The struct octeon_i2c.
265 *
266 * Returns 0 on success, otherwise a negative errno.
267 */
268static int octeon_i2c_stop(struct octeon_i2c *i2c)
269{ 259{
270 u8 data;
271
272 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, 260 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
273 TWSI_CTL_ENAB | TWSI_CTL_STP); 261 TWSI_CTL_ENAB | TWSI_CTL_STP);
274
275 data = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
276
277 if (data != STAT_IDLE) {
278 dev_err(i2c->dev, "%s: bad status(0x%x)\n", __func__, data);
279 return -EIO;
280 }
281 return 0;
282} 262}
283 263
284/** 264/**
285 * octeon_i2c_write - send data to the bus. 265 * octeon_i2c_write - send data to the bus via low-level controller
286 * @i2c: The struct octeon_i2c. 266 * @i2c: The struct octeon_i2c
287 * @target: Target address. 267 * @target: Target address
288 * @data: Pointer to the data to be sent. 268 * @data: Pointer to the data to be sent
289 * @length: Length of the data. 269 * @length: Length of the data
290 * 270 *
291 * The address is sent over the bus, then the data. 271 * The address is sent over the bus, then the data.
292 * 272 *
@@ -311,6 +291,7 @@ static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
311 291
312 for (i = 0; i < length; i++) { 292 for (i = 0; i < length; i++) {
313 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT); 293 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
294
314 if ((tmp != STAT_TXADDR_ACK) && (tmp != STAT_TXDATA_ACK)) { 295 if ((tmp != STAT_TXADDR_ACK) && (tmp != STAT_TXDATA_ACK)) {
315 dev_err(i2c->dev, 296 dev_err(i2c->dev,
316 "%s: bad status before write (0x%x)\n", 297 "%s: bad status before write (0x%x)\n",
@@ -330,20 +311,21 @@ static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
330} 311}
331 312
332/** 313/**
333 * octeon_i2c_read - receive data from the bus. 314 * octeon_i2c_read - receive data from the bus via low-level controller
334 * @i2c: The struct octeon_i2c. 315 * @i2c: The struct octeon_i2c
335 * @target: Target address. 316 * @target: Target address
336 * @data: Pointer to the location to store the datae . 317 * @data: Pointer to the location to store the data
337 * @length: Length of the data. 318 * @rlength: Length of the data
319 * @recv_len: flag for length byte
338 * 320 *
339 * The address is sent over the bus, then the data is read. 321 * The address is sent over the bus, then the data is read.
340 * 322 *
341 * Returns 0 on success, otherwise a negative errno. 323 * Returns 0 on success, otherwise a negative errno.
342 */ 324 */
343static int octeon_i2c_read(struct octeon_i2c *i2c, int target, 325static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
344 u8 *data, int length) 326 u8 *data, u16 *rlength, bool recv_len)
345{ 327{
346 int i, result; 328 int i, result, length = *rlength;
347 u8 tmp; 329 u8 tmp;
348 330
349 if (length < 1) 331 if (length < 1)
@@ -353,7 +335,7 @@ static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
353 if (result) 335 if (result)
354 return result; 336 return result;
355 337
356 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, (target<<1) | 1); 338 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, (target << 1) | 1);
357 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB); 339 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
358 340
359 result = octeon_i2c_wait(i2c); 341 result = octeon_i2c_wait(i2c);
@@ -362,6 +344,7 @@ static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
362 344
363 for (i = 0; i < length; i++) { 345 for (i = 0; i < length; i++) {
364 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT); 346 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
347
365 if ((tmp != STAT_RXDATA_ACK) && (tmp != STAT_RXADDR_ACK)) { 348 if ((tmp != STAT_RXDATA_ACK) && (tmp != STAT_RXADDR_ACK)) {
366 dev_err(i2c->dev, 349 dev_err(i2c->dev,
367 "%s: bad status before read (0x%x)\n", 350 "%s: bad status before read (0x%x)\n",
@@ -369,52 +352,59 @@ static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
369 return -EIO; 352 return -EIO;
370 } 353 }
371 354
372 if (i+1 < length) 355 if (i + 1 < length)
373 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, 356 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
374 TWSI_CTL_ENAB | TWSI_CTL_AAK); 357 TWSI_CTL_ENAB | TWSI_CTL_AAK);
375 else 358 else
376 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, 359 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
377 TWSI_CTL_ENAB); 360 TWSI_CTL_ENAB);
378 361
379 result = octeon_i2c_wait(i2c); 362 result = octeon_i2c_wait(i2c);
380 if (result) 363 if (result)
381 return result; 364 return result;
382 365
383 data[i] = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_DATA); 366 data[i] = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_DATA);
367 if (recv_len && i == 0) {
368 if (data[i] > I2C_SMBUS_BLOCK_MAX + 1) {
369 dev_err(i2c->dev,
370 "%s: read len > I2C_SMBUS_BLOCK_MAX %d\n",
371 __func__, data[i]);
372 return -EPROTO;
373 }
374 length += data[i];
375 }
384 } 376 }
377 *rlength = length;
385 return 0; 378 return 0;
386} 379}
387 380
388/** 381/**
389 * octeon_i2c_xfer - The driver's master_xfer function. 382 * octeon_i2c_xfer - The driver's master_xfer function
390 * @adap: Pointer to the i2c_adapter structure. 383 * @adap: Pointer to the i2c_adapter structure
391 * @msgs: Pointer to the messages to be processed. 384 * @msgs: Pointer to the messages to be processed
392 * @num: Length of the MSGS array. 385 * @num: Length of the MSGS array
393 * 386 *
394 * Returns the number of messages processed, or a negative errno on 387 * Returns the number of messages processed, or a negative errno on failure.
395 * failure.
396 */ 388 */
397static int octeon_i2c_xfer(struct i2c_adapter *adap, 389static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
398 struct i2c_msg *msgs,
399 int num) 390 int num)
400{ 391{
401 struct i2c_msg *pmsg;
402 int i;
403 int ret = 0;
404 struct octeon_i2c *i2c = i2c_get_adapdata(adap); 392 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
393 int i, ret = 0;
405 394
406 for (i = 0; ret == 0 && i < num; i++) { 395 for (i = 0; ret == 0 && i < num; i++) {
407 pmsg = &msgs[i]; 396 struct i2c_msg *pmsg = &msgs[i];
397
408 dev_dbg(i2c->dev, 398 dev_dbg(i2c->dev,
409 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n", 399 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
410 pmsg->flags & I2C_M_RD ? "read" : "write", 400 pmsg->flags & I2C_M_RD ? "read" : "write",
411 pmsg->len, pmsg->addr, i + 1, num); 401 pmsg->len, pmsg->addr, i + 1, num);
412 if (pmsg->flags & I2C_M_RD) 402 if (pmsg->flags & I2C_M_RD)
413 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf, 403 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
414 pmsg->len); 404 &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
415 else 405 else
416 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf, 406 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
417 pmsg->len); 407 pmsg->len);
418 } 408 }
419 octeon_i2c_stop(i2c); 409 octeon_i2c_stop(i2c);
420 410
@@ -423,7 +413,8 @@ static int octeon_i2c_xfer(struct i2c_adapter *adap,
423 413
424static u32 octeon_i2c_functionality(struct i2c_adapter *adap) 414static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
425{ 415{
426 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 416 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
417 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
427} 418}
428 419
429static const struct i2c_algorithm octeon_i2c_algo = { 420static const struct i2c_algorithm octeon_i2c_algo = {
@@ -438,10 +429,8 @@ static struct i2c_adapter octeon_i2c_ops = {
438 .timeout = HZ / 50, 429 .timeout = HZ / 50,
439}; 430};
440 431
441/** 432/* calculate and set clock divisors */
442 * octeon_i2c_setclock - Calculate and set clock divisors. 433static void octeon_i2c_set_clock(struct octeon_i2c *i2c)
443 */
444static int octeon_i2c_setclock(struct octeon_i2c *i2c)
445{ 434{
446 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff; 435 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
447 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000; 436 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
@@ -449,8 +438,7 @@ static int octeon_i2c_setclock(struct octeon_i2c *i2c)
449 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) { 438 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
450 /* 439 /*
451 * An mdiv value of less than 2 seems to not work well 440 * An mdiv value of less than 2 seems to not work well
452 * with ds1337 RTCs, so we constrain it to larger 441 * with ds1337 RTCs, so we constrain it to larger values.
453 * values.
454 */ 442 */
455 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) { 443 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
456 /* 444 /*
@@ -460,6 +448,7 @@ static int octeon_i2c_setclock(struct octeon_i2c *i2c)
460 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10; 448 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
461 tclk *= (1 << ndiv_idx); 449 tclk *= (1 << ndiv_idx);
462 thp_base = (i2c->sys_freq / (tclk * 2)) - 1; 450 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
451
463 for (inc = 0; inc <= 1; inc++) { 452 for (inc = 0; inc <= 1; inc++) {
464 thp_idx = thp_base + inc; 453 thp_idx = thp_base + inc;
465 if (thp_idx < 5 || thp_idx > 0xff) 454 if (thp_idx < 5 || thp_idx > 0xff)
@@ -480,11 +469,9 @@ static int octeon_i2c_setclock(struct octeon_i2c *i2c)
480 } 469 }
481 octeon_i2c_write_sw(i2c, SW_TWSI_OP_TWSI_CLK, thp); 470 octeon_i2c_write_sw(i2c, SW_TWSI_OP_TWSI_CLK, thp);
482 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv); 471 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
483
484 return 0;
485} 472}
486 473
487static int octeon_i2c_initlowlevel(struct octeon_i2c *i2c) 474static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
488{ 475{
489 u8 status; 476 u8 status;
490 int tries; 477 int tries;
@@ -507,9 +494,10 @@ static int octeon_i2c_initlowlevel(struct octeon_i2c *i2c)
507 494
508static int octeon_i2c_probe(struct platform_device *pdev) 495static int octeon_i2c_probe(struct platform_device *pdev)
509{ 496{
510 int irq, result = 0; 497 struct device_node *node = pdev->dev.of_node;
511 struct octeon_i2c *i2c;
512 struct resource *res_mem; 498 struct resource *res_mem;
499 struct octeon_i2c *i2c;
500 int irq, result = 0;
513 501
514 /* All adaptors have an irq. */ 502 /* All adaptors have an irq. */
515 irq = platform_get_irq(pdev, 0); 503 irq = platform_get_irq(pdev, 0);
@@ -518,31 +506,25 @@ static int octeon_i2c_probe(struct platform_device *pdev)
518 506
519 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); 507 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
520 if (!i2c) { 508 if (!i2c) {
521 dev_err(&pdev->dev, "kzalloc failed\n");
522 result = -ENOMEM; 509 result = -ENOMEM;
523 goto out; 510 goto out;
524 } 511 }
525 i2c->dev = &pdev->dev; 512 i2c->dev = &pdev->dev;
526 513
527 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 514 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
528 515 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
529 if (res_mem == NULL) { 516 if (IS_ERR(i2c->twsi_base)) {
530 dev_err(i2c->dev, "found no memory resource\n"); 517 result = PTR_ERR(i2c->twsi_base);
531 result = -ENXIO;
532 goto out; 518 goto out;
533 } 519 }
534 i2c->twsi_phys = res_mem->start;
535 i2c->regsize = resource_size(res_mem);
536 520
537 /* 521 /*
538 * "clock-rate" is a legacy binding, the official binding is 522 * "clock-rate" is a legacy binding, the official binding is
539 * "clock-frequency". Try the official one first and then 523 * "clock-frequency". Try the official one first and then
540 * fall back if it doesn't exist. 524 * fall back if it doesn't exist.
541 */ 525 */
542 if (of_property_read_u32(pdev->dev.of_node, 526 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
543 "clock-frequency", &i2c->twsi_freq) && 527 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
544 of_property_read_u32(pdev->dev.of_node,
545 "clock-rate", &i2c->twsi_freq)) {
546 dev_err(i2c->dev, 528 dev_err(i2c->dev,
547 "no I2C 'clock-rate' or 'clock-frequency' property\n"); 529 "no I2C 'clock-rate' or 'clock-frequency' property\n");
548 result = -ENXIO; 530 result = -ENXIO;
@@ -551,13 +533,6 @@ static int octeon_i2c_probe(struct platform_device *pdev)
551 533
552 i2c->sys_freq = octeon_get_io_clock_rate(); 534 i2c->sys_freq = octeon_get_io_clock_rate();
553 535
554 if (!devm_request_mem_region(&pdev->dev, i2c->twsi_phys, i2c->regsize,
555 res_mem->name)) {
556 dev_err(i2c->dev, "request_mem_region failed\n");
557 goto out;
558 }
559 i2c->twsi_base = devm_ioremap(&pdev->dev, i2c->twsi_phys, i2c->regsize);
560
561 init_waitqueue_head(&i2c->queue); 536 init_waitqueue_head(&i2c->queue);
562 537
563 i2c->irq = irq; 538 i2c->irq = irq;
@@ -569,21 +544,17 @@ static int octeon_i2c_probe(struct platform_device *pdev)
569 goto out; 544 goto out;
570 } 545 }
571 546
572 result = octeon_i2c_initlowlevel(i2c); 547 result = octeon_i2c_init_lowlevel(i2c);
573 if (result) { 548 if (result) {
574 dev_err(i2c->dev, "init low level failed\n"); 549 dev_err(i2c->dev, "init low level failed\n");
575 goto out; 550 goto out;
576 } 551 }
577 552
578 result = octeon_i2c_setclock(i2c); 553 octeon_i2c_set_clock(i2c);
579 if (result) {
580 dev_err(i2c->dev, "clock init failed\n");
581 goto out;
582 }
583 554
584 i2c->adap = octeon_i2c_ops; 555 i2c->adap = octeon_i2c_ops;
585 i2c->adap.dev.parent = &pdev->dev; 556 i2c->adap.dev.parent = &pdev->dev;
586 i2c->adap.dev.of_node = pdev->dev.of_node; 557 i2c->adap.dev.of_node = node;
587 i2c_set_adapdata(&i2c->adap, i2c); 558 i2c_set_adapdata(&i2c->adap, i2c);
588 platform_set_drvdata(pdev, i2c); 559 platform_set_drvdata(pdev, i2c);
589 560
@@ -592,8 +563,7 @@ static int octeon_i2c_probe(struct platform_device *pdev)
592 dev_err(i2c->dev, "failed to add adapter\n"); 563 dev_err(i2c->dev, "failed to add adapter\n");
593 goto out; 564 goto out;
594 } 565 }
595 dev_info(i2c->dev, "version %s\n", DRV_VERSION); 566 dev_info(i2c->dev, "probed\n");
596
597 return 0; 567 return 0;
598 568
599out: 569out:
@@ -608,10 +578,8 @@ static int octeon_i2c_remove(struct platform_device *pdev)
608 return 0; 578 return 0;
609}; 579};
610 580
611static struct of_device_id octeon_i2c_match[] = { 581static const struct of_device_id octeon_i2c_match[] = {
612 { 582 { .compatible = "cavium,octeon-3860-twsi", },
613 .compatible = "cavium,octeon-3860-twsi",
614 },
615 {}, 583 {},
616}; 584};
617MODULE_DEVICE_TABLE(of, octeon_i2c_match); 585MODULE_DEVICE_TABLE(of, octeon_i2c_match);
@@ -630,4 +598,3 @@ module_platform_driver(octeon_i2c_driver);
630MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>"); 598MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
631MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors"); 599MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
632MODULE_LICENSE("GPL"); 600MODULE_LICENSE("GPL");
633MODULE_VERSION(DRV_VERSION);
diff --git a/drivers/i2c/busses/i2c-piix4.c b/drivers/i2c/busses/i2c-piix4.c
index 93f2895383ee..23d1c167b5d7 100644
--- a/drivers/i2c/busses/i2c-piix4.c
+++ b/drivers/i2c/busses/i2c-piix4.c
@@ -85,8 +85,14 @@
85/* SB800 constants */ 85/* SB800 constants */
86#define SB800_PIIX4_SMB_IDX 0xcd6 86#define SB800_PIIX4_SMB_IDX 0xcd6
87 87
88/* SB800 port is selected by bits 2:1 of the smb_en register (0x2c) */ 88/*
89 * SB800 port is selected by bits 2:1 of the smb_en register (0x2c)
90 * or the smb_sel register (0x2e), depending on bit 0 of register 0x2f.
91 * Hudson-2/Bolton port is always selected by bits 2:1 of register 0x2f.
92 */
89#define SB800_PIIX4_PORT_IDX 0x2c 93#define SB800_PIIX4_PORT_IDX 0x2c
94#define SB800_PIIX4_PORT_IDX_ALT 0x2e
95#define SB800_PIIX4_PORT_IDX_SEL 0x2f
90#define SB800_PIIX4_PORT_IDX_MASK 0x06 96#define SB800_PIIX4_PORT_IDX_MASK 0x06
91 97
92/* insmod parameters */ 98/* insmod parameters */
@@ -136,8 +142,13 @@ static const struct dmi_system_id piix4_dmi_ibm[] = {
136 { }, 142 { },
137}; 143};
138 144
139/* SB800 globals */ 145/*
146 * SB800 globals
147 * piix4_mutex_sb800 protects piix4_port_sel_sb800 and the pair
148 * of I/O ports at SB800_PIIX4_SMB_IDX.
149 */
140static DEFINE_MUTEX(piix4_mutex_sb800); 150static DEFINE_MUTEX(piix4_mutex_sb800);
151static u8 piix4_port_sel_sb800;
141static const char *piix4_main_port_names_sb800[PIIX4_MAX_ADAPTERS] = { 152static const char *piix4_main_port_names_sb800[PIIX4_MAX_ADAPTERS] = {
142 " port 0", " port 2", " port 3", " port 4" 153 " port 0", " port 2", " port 3", " port 4"
143}; 154};
@@ -148,7 +159,7 @@ struct i2c_piix4_adapdata {
148 159
149 /* SB800 */ 160 /* SB800 */
150 bool sb800_main; 161 bool sb800_main;
151 unsigned short port; 162 u8 port; /* Port number, shifted */
152}; 163};
153 164
154static int piix4_setup(struct pci_dev *PIIX4_dev, 165static int piix4_setup(struct pci_dev *PIIX4_dev,
@@ -254,7 +265,7 @@ static int piix4_setup_sb800(struct pci_dev *PIIX4_dev,
254 const struct pci_device_id *id, u8 aux) 265 const struct pci_device_id *id, u8 aux)
255{ 266{
256 unsigned short piix4_smba; 267 unsigned short piix4_smba;
257 u8 smba_en_lo, smba_en_hi, smb_en, smb_en_status; 268 u8 smba_en_lo, smba_en_hi, smb_en, smb_en_status, port_sel;
258 u8 i2ccfg, i2ccfg_offset = 0x10; 269 u8 i2ccfg, i2ccfg_offset = 0x10;
259 270
260 /* SB800 and later SMBus does not support forcing address */ 271 /* SB800 and later SMBus does not support forcing address */
@@ -334,6 +345,23 @@ static int piix4_setup_sb800(struct pci_dev *PIIX4_dev,
334 "SMBus Host Controller at 0x%x, revision %d\n", 345 "SMBus Host Controller at 0x%x, revision %d\n",
335 piix4_smba, i2ccfg >> 4); 346 piix4_smba, i2ccfg >> 4);
336 347
348 /* Find which register is used for port selection */
349 if (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD) {
350 piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_ALT;
351 } else {
352 mutex_lock(&piix4_mutex_sb800);
353 outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX);
354 port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1);
355 piix4_port_sel_sb800 = (port_sel & 0x01) ?
356 SB800_PIIX4_PORT_IDX_ALT :
357 SB800_PIIX4_PORT_IDX;
358 mutex_unlock(&piix4_mutex_sb800);
359 }
360
361 dev_info(&PIIX4_dev->dev,
362 "Using register 0x%02x for SMBus port selection\n",
363 (unsigned int)piix4_port_sel_sb800);
364
337 return piix4_smba; 365 return piix4_smba;
338} 366}
339 367
@@ -563,12 +591,12 @@ static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
563 591
564 mutex_lock(&piix4_mutex_sb800); 592 mutex_lock(&piix4_mutex_sb800);
565 593
566 outb_p(SB800_PIIX4_PORT_IDX, SB800_PIIX4_SMB_IDX); 594 outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
567 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1); 595 smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
568 596
569 port = adapdata->port; 597 port = adapdata->port;
570 if ((smba_en_lo & SB800_PIIX4_PORT_IDX_MASK) != (port << 1)) 598 if ((smba_en_lo & SB800_PIIX4_PORT_IDX_MASK) != port)
571 outb_p((smba_en_lo & ~SB800_PIIX4_PORT_IDX_MASK) | (port << 1), 599 outb_p((smba_en_lo & ~SB800_PIIX4_PORT_IDX_MASK) | port,
572 SB800_PIIX4_SMB_IDX + 1); 600 SB800_PIIX4_SMB_IDX + 1);
573 601
574 retval = piix4_access(adap, addr, flags, read_write, 602 retval = piix4_access(adap, addr, flags, read_write,
@@ -627,7 +655,7 @@ static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS];
627static struct i2c_adapter *piix4_aux_adapter; 655static struct i2c_adapter *piix4_aux_adapter;
628 656
629static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba, 657static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
630 bool sb800_main, unsigned short port, 658 bool sb800_main, u8 port,
631 const char *name, struct i2c_adapter **padap) 659 const char *name, struct i2c_adapter **padap)
632{ 660{
633 struct i2c_adapter *adap; 661 struct i2c_adapter *adap;
@@ -654,7 +682,7 @@ static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
654 682
655 adapdata->smba = smba; 683 adapdata->smba = smba;
656 adapdata->sb800_main = sb800_main; 684 adapdata->sb800_main = sb800_main;
657 adapdata->port = port; 685 adapdata->port = port << 1;
658 686
659 /* set up the sysfs linkage to our parent device */ 687 /* set up the sysfs linkage to our parent device */
660 adap->dev.parent = &dev->dev; 688 adap->dev.parent = &dev->dev;
@@ -790,7 +818,7 @@ static void piix4_adap_remove(struct i2c_adapter *adap)
790 818
791 if (adapdata->smba) { 819 if (adapdata->smba) {
792 i2c_del_adapter(adap); 820 i2c_del_adapter(adap);
793 if (adapdata->port == 0) { 821 if (adapdata->port == (0 << 1)) {
794 release_region(adapdata->smba, SMBIOSIZE); 822 release_region(adapdata->smba, SMBIOSIZE);
795 if (adapdata->sb800_main) 823 if (adapdata->sb800_main)
796 release_region(SB800_PIIX4_SMB_IDX, 2); 824 release_region(SB800_PIIX4_SMB_IDX, 2);
diff --git a/drivers/i2c/busses/i2c-qup.c b/drivers/i2c/busses/i2c-qup.c
index fdcbdab808e9..23eaabb19f96 100644
--- a/drivers/i2c/busses/i2c-qup.c
+++ b/drivers/i2c/busses/i2c-qup.c
@@ -14,8 +14,12 @@
14 * 14 *
15 */ 15 */
16 16
17#include <linux/atomic.h>
17#include <linux/clk.h> 18#include <linux/clk.h>
18#include <linux/delay.h> 19#include <linux/delay.h>
20#include <linux/dmaengine.h>
21#include <linux/dmapool.h>
22#include <linux/dma-mapping.h>
19#include <linux/err.h> 23#include <linux/err.h>
20#include <linux/i2c.h> 24#include <linux/i2c.h>
21#include <linux/interrupt.h> 25#include <linux/interrupt.h>
@@ -24,6 +28,7 @@
24#include <linux/of.h> 28#include <linux/of.h>
25#include <linux/platform_device.h> 29#include <linux/platform_device.h>
26#include <linux/pm_runtime.h> 30#include <linux/pm_runtime.h>
31#include <linux/scatterlist.h>
27 32
28/* QUP Registers */ 33/* QUP Registers */
29#define QUP_CONFIG 0x000 34#define QUP_CONFIG 0x000
@@ -33,6 +38,7 @@
33#define QUP_OPERATIONAL 0x018 38#define QUP_OPERATIONAL 0x018
34#define QUP_ERROR_FLAGS 0x01c 39#define QUP_ERROR_FLAGS 0x01c
35#define QUP_ERROR_FLAGS_EN 0x020 40#define QUP_ERROR_FLAGS_EN 0x020
41#define QUP_OPERATIONAL_MASK 0x028
36#define QUP_HW_VERSION 0x030 42#define QUP_HW_VERSION 0x030
37#define QUP_MX_OUTPUT_CNT 0x100 43#define QUP_MX_OUTPUT_CNT 0x100
38#define QUP_OUT_FIFO_BASE 0x110 44#define QUP_OUT_FIFO_BASE 0x110
@@ -42,6 +48,7 @@
42#define QUP_IN_FIFO_BASE 0x218 48#define QUP_IN_FIFO_BASE 0x218
43#define QUP_I2C_CLK_CTL 0x400 49#define QUP_I2C_CLK_CTL 0x400
44#define QUP_I2C_STATUS 0x404 50#define QUP_I2C_STATUS 0x404
51#define QUP_I2C_MASTER_GEN 0x408
45 52
46/* QUP States and reset values */ 53/* QUP States and reset values */
47#define QUP_RESET_STATE 0 54#define QUP_RESET_STATE 0
@@ -51,6 +58,7 @@
51 58
52#define QUP_STATE_VALID BIT(2) 59#define QUP_STATE_VALID BIT(2)
53#define QUP_I2C_MAST_GEN BIT(4) 60#define QUP_I2C_MAST_GEN BIT(4)
61#define QUP_I2C_FLUSH BIT(6)
54 62
55#define QUP_OPERATIONAL_RESET 0x000ff0 63#define QUP_OPERATIONAL_RESET 0x000ff0
56#define QUP_I2C_STATUS_RESET 0xfffffc 64#define QUP_I2C_STATUS_RESET 0xfffffc
@@ -69,16 +77,22 @@
69#define QUP_CLOCK_AUTO_GATE BIT(13) 77#define QUP_CLOCK_AUTO_GATE BIT(13)
70#define I2C_MINI_CORE (2 << 8) 78#define I2C_MINI_CORE (2 << 8)
71#define I2C_N_VAL 15 79#define I2C_N_VAL 15
80#define I2C_N_VAL_V2 7
81
72/* Most significant word offset in FIFO port */ 82/* Most significant word offset in FIFO port */
73#define QUP_MSW_SHIFT (I2C_N_VAL + 1) 83#define QUP_MSW_SHIFT (I2C_N_VAL + 1)
74 84
75/* Packing/Unpacking words in FIFOs, and IO modes */ 85/* Packing/Unpacking words in FIFOs, and IO modes */
76#define QUP_OUTPUT_BLK_MODE (1 << 10) 86#define QUP_OUTPUT_BLK_MODE (1 << 10)
87#define QUP_OUTPUT_BAM_MODE (3 << 10)
77#define QUP_INPUT_BLK_MODE (1 << 12) 88#define QUP_INPUT_BLK_MODE (1 << 12)
89#define QUP_INPUT_BAM_MODE (3 << 12)
90#define QUP_BAM_MODE (QUP_OUTPUT_BAM_MODE | QUP_INPUT_BAM_MODE)
78#define QUP_UNPACK_EN BIT(14) 91#define QUP_UNPACK_EN BIT(14)
79#define QUP_PACK_EN BIT(15) 92#define QUP_PACK_EN BIT(15)
80 93
81#define QUP_REPACK_EN (QUP_UNPACK_EN | QUP_PACK_EN) 94#define QUP_REPACK_EN (QUP_UNPACK_EN | QUP_PACK_EN)
95#define QUP_V2_TAGS_EN 1
82 96
83#define QUP_OUTPUT_BLOCK_SIZE(x)(((x) >> 0) & 0x03) 97#define QUP_OUTPUT_BLOCK_SIZE(x)(((x) >> 0) & 0x03)
84#define QUP_OUTPUT_FIFO_SIZE(x) (((x) >> 2) & 0x07) 98#define QUP_OUTPUT_FIFO_SIZE(x) (((x) >> 2) & 0x07)
@@ -90,6 +104,15 @@
90#define QUP_TAG_DATA (2 << 8) 104#define QUP_TAG_DATA (2 << 8)
91#define QUP_TAG_STOP (3 << 8) 105#define QUP_TAG_STOP (3 << 8)
92#define QUP_TAG_REC (4 << 8) 106#define QUP_TAG_REC (4 << 8)
107#define QUP_BAM_INPUT_EOT 0x93
108#define QUP_BAM_FLUSH_STOP 0x96
109
110/* QUP v2 tags */
111#define QUP_TAG_V2_START 0x81
112#define QUP_TAG_V2_DATAWR 0x82
113#define QUP_TAG_V2_DATAWR_STOP 0x83
114#define QUP_TAG_V2_DATARD 0x85
115#define QUP_TAG_V2_DATARD_STOP 0x87
93 116
94/* Status, Error flags */ 117/* Status, Error flags */
95#define I2C_STATUS_WR_BUFFER_FULL BIT(0) 118#define I2C_STATUS_WR_BUFFER_FULL BIT(0)
@@ -98,6 +121,36 @@
98#define QUP_STATUS_ERROR_FLAGS 0x7c 121#define QUP_STATUS_ERROR_FLAGS 0x7c
99 122
100#define QUP_READ_LIMIT 256 123#define QUP_READ_LIMIT 256
124#define SET_BIT 0x1
125#define RESET_BIT 0x0
126#define ONE_BYTE 0x1
127#define QUP_I2C_MX_CONFIG_DURING_RUN BIT(31)
128
129#define MX_TX_RX_LEN SZ_64K
130#define MX_BLOCKS (MX_TX_RX_LEN / QUP_READ_LIMIT)
131
132/* Max timeout in ms for 32k bytes */
133#define TOUT_MAX 300
134
135struct qup_i2c_block {
136 int count;
137 int pos;
138 int tx_tag_len;
139 int rx_tag_len;
140 int data_len;
141 u8 tags[6];
142};
143
144struct qup_i2c_tag {
145 u8 *start;
146 dma_addr_t addr;
147};
148
149struct qup_i2c_bam {
150 struct qup_i2c_tag tag;
151 struct dma_chan *dma;
152 struct scatterlist *sg;
153};
101 154
102struct qup_i2c_dev { 155struct qup_i2c_dev {
103 struct device *dev; 156 struct device *dev;
@@ -114,6 +167,7 @@ struct qup_i2c_dev {
114 int in_blk_sz; 167 int in_blk_sz;
115 168
116 unsigned long one_byte_t; 169 unsigned long one_byte_t;
170 struct qup_i2c_block blk;
117 171
118 struct i2c_msg *msg; 172 struct i2c_msg *msg;
119 /* Current posion in user message buffer */ 173 /* Current posion in user message buffer */
@@ -123,6 +177,19 @@ struct qup_i2c_dev {
123 /* QUP core errors */ 177 /* QUP core errors */
124 u32 qup_err; 178 u32 qup_err;
125 179
180 /* To check if this is the last msg */
181 bool is_last;
182
183 /* To configure when bus is in run state */
184 int config_run;
185
186 /* dma parameters */
187 bool is_dma;
188 struct dma_pool *dpool;
189 struct qup_i2c_tag start_tag;
190 struct qup_i2c_bam brx;
191 struct qup_i2c_bam btx;
192
126 struct completion xfer; 193 struct completion xfer;
127}; 194};
128 195
@@ -199,6 +266,14 @@ static int qup_i2c_poll_state(struct qup_i2c_dev *qup, u32 req_state)
199 return qup_i2c_poll_state_mask(qup, req_state, QUP_STATE_MASK); 266 return qup_i2c_poll_state_mask(qup, req_state, QUP_STATE_MASK);
200} 267}
201 268
269static void qup_i2c_flush(struct qup_i2c_dev *qup)
270{
271 u32 val = readl(qup->base + QUP_STATE);
272
273 val |= QUP_I2C_FLUSH;
274 writel(val, qup->base + QUP_STATE);
275}
276
202static int qup_i2c_poll_state_valid(struct qup_i2c_dev *qup) 277static int qup_i2c_poll_state_valid(struct qup_i2c_dev *qup)
203{ 278{
204 return qup_i2c_poll_state_mask(qup, 0, 0); 279 return qup_i2c_poll_state_mask(qup, 0, 0);
@@ -221,26 +296,62 @@ static int qup_i2c_change_state(struct qup_i2c_dev *qup, u32 state)
221 return 0; 296 return 0;
222} 297}
223 298
224static int qup_i2c_wait_writeready(struct qup_i2c_dev *qup) 299/**
300 * qup_i2c_wait_ready - wait for a give number of bytes in tx/rx path
301 * @qup: The qup_i2c_dev device
302 * @op: The bit/event to wait on
303 * @val: value of the bit to wait on, 0 or 1
304 * @len: The length the bytes to be transferred
305 */
306static int qup_i2c_wait_ready(struct qup_i2c_dev *qup, int op, bool val,
307 int len)
225{ 308{
226 unsigned long timeout; 309 unsigned long timeout;
227 u32 opflags; 310 u32 opflags;
228 u32 status; 311 u32 status;
312 u32 shift = __ffs(op);
229 313
230 timeout = jiffies + HZ; 314 len *= qup->one_byte_t;
315 /* timeout after a wait of twice the max time */
316 timeout = jiffies + len * 4;
231 317
232 for (;;) { 318 for (;;) {
233 opflags = readl(qup->base + QUP_OPERATIONAL); 319 opflags = readl(qup->base + QUP_OPERATIONAL);
234 status = readl(qup->base + QUP_I2C_STATUS); 320 status = readl(qup->base + QUP_I2C_STATUS);
235 321
236 if (!(opflags & QUP_OUT_NOT_EMPTY) && 322 if (((opflags & op) >> shift) == val) {
237 !(status & I2C_STATUS_BUS_ACTIVE)) 323 if ((op == QUP_OUT_NOT_EMPTY) && qup->is_last) {
238 return 0; 324 if (!(status & I2C_STATUS_BUS_ACTIVE))
325 return 0;
326 } else {
327 return 0;
328 }
329 }
239 330
240 if (time_after(jiffies, timeout)) 331 if (time_after(jiffies, timeout))
241 return -ETIMEDOUT; 332 return -ETIMEDOUT;
242 333
243 usleep_range(qup->one_byte_t, qup->one_byte_t * 2); 334 usleep_range(len, len * 2);
335 }
336}
337
338static void qup_i2c_set_write_mode_v2(struct qup_i2c_dev *qup,
339 struct i2c_msg *msg)
340{
341 /* Number of entries to shift out, including the tags */
342 int total = msg->len + qup->blk.tx_tag_len;
343
344 total |= qup->config_run;
345
346 if (total < qup->out_fifo_sz) {
347 /* FIFO mode */
348 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
349 writel(total, qup->base + QUP_MX_WRITE_CNT);
350 } else {
351 /* BLOCK mode (transfer data on chunks) */
352 writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
353 qup->base + QUP_IO_MODE);
354 writel(total, qup->base + QUP_MX_OUTPUT_CNT);
244 } 355 }
245} 356}
246 357
@@ -261,13 +372,45 @@ static void qup_i2c_set_write_mode(struct qup_i2c_dev *qup, struct i2c_msg *msg)
261 } 372 }
262} 373}
263 374
264static void qup_i2c_issue_write(struct qup_i2c_dev *qup, struct i2c_msg *msg) 375static int check_for_fifo_space(struct qup_i2c_dev *qup)
376{
377 int ret;
378
379 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
380 if (ret)
381 goto out;
382
383 ret = qup_i2c_wait_ready(qup, QUP_OUT_FULL,
384 RESET_BIT, 4 * ONE_BYTE);
385 if (ret) {
386 /* Fifo is full. Drain out the fifo */
387 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
388 if (ret)
389 goto out;
390
391 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY,
392 RESET_BIT, 256 * ONE_BYTE);
393 if (ret) {
394 dev_err(qup->dev, "timeout for fifo out full");
395 goto out;
396 }
397
398 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
399 if (ret)
400 goto out;
401 }
402
403out:
404 return ret;
405}
406
407static int qup_i2c_issue_write(struct qup_i2c_dev *qup, struct i2c_msg *msg)
265{ 408{
266 u32 addr = msg->addr << 1; 409 u32 addr = msg->addr << 1;
267 u32 qup_tag; 410 u32 qup_tag;
268 u32 opflags;
269 int idx; 411 int idx;
270 u32 val; 412 u32 val;
413 int ret = 0;
271 414
272 if (qup->pos == 0) { 415 if (qup->pos == 0) {
273 val = QUP_TAG_START | addr; 416 val = QUP_TAG_START | addr;
@@ -279,9 +422,9 @@ static void qup_i2c_issue_write(struct qup_i2c_dev *qup, struct i2c_msg *msg)
279 422
280 while (qup->pos < msg->len) { 423 while (qup->pos < msg->len) {
281 /* Check that there's space in the FIFO for our pair */ 424 /* Check that there's space in the FIFO for our pair */
282 opflags = readl(qup->base + QUP_OPERATIONAL); 425 ret = check_for_fifo_space(qup);
283 if (opflags & QUP_OUT_FULL) 426 if (ret)
284 break; 427 return ret;
285 428
286 if (qup->pos == msg->len - 1) 429 if (qup->pos == msg->len - 1)
287 qup_tag = QUP_TAG_STOP; 430 qup_tag = QUP_TAG_STOP;
@@ -300,11 +443,501 @@ static void qup_i2c_issue_write(struct qup_i2c_dev *qup, struct i2c_msg *msg)
300 qup->pos++; 443 qup->pos++;
301 idx++; 444 idx++;
302 } 445 }
446
447 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
448
449 return ret;
303} 450}
304 451
305static int qup_i2c_write_one(struct qup_i2c_dev *qup, struct i2c_msg *msg) 452static void qup_i2c_set_blk_data(struct qup_i2c_dev *qup,
453 struct i2c_msg *msg)
454{
455 memset(&qup->blk, 0, sizeof(qup->blk));
456
457 qup->blk.data_len = msg->len;
458 qup->blk.count = (msg->len + QUP_READ_LIMIT - 1) / QUP_READ_LIMIT;
459
460 /* 4 bytes for first block and 2 writes for rest */
461 qup->blk.tx_tag_len = 4 + (qup->blk.count - 1) * 2;
462
463 /* There are 2 tag bytes that are read in to fifo for every block */
464 if (msg->flags & I2C_M_RD)
465 qup->blk.rx_tag_len = qup->blk.count * 2;
466}
467
468static int qup_i2c_send_data(struct qup_i2c_dev *qup, int tlen, u8 *tbuf,
469 int dlen, u8 *dbuf)
470{
471 u32 val = 0, idx = 0, pos = 0, i = 0, t;
472 int len = tlen + dlen;
473 u8 *buf = tbuf;
474 int ret = 0;
475
476 while (len > 0) {
477 ret = check_for_fifo_space(qup);
478 if (ret)
479 return ret;
480
481 t = (len >= 4) ? 4 : len;
482
483 while (idx < t) {
484 if (!i && (pos >= tlen)) {
485 buf = dbuf;
486 pos = 0;
487 i = 1;
488 }
489 val |= buf[pos++] << (idx++ * 8);
490 }
491
492 writel(val, qup->base + QUP_OUT_FIFO_BASE);
493 idx = 0;
494 val = 0;
495 len -= 4;
496 }
497
498 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
499
500 return ret;
501}
502
503static int qup_i2c_get_data_len(struct qup_i2c_dev *qup)
504{
505 int data_len;
506
507 if (qup->blk.data_len > QUP_READ_LIMIT)
508 data_len = QUP_READ_LIMIT;
509 else
510 data_len = qup->blk.data_len;
511
512 return data_len;
513}
514
515static int qup_i2c_set_tags(u8 *tags, struct qup_i2c_dev *qup,
516 struct i2c_msg *msg, int is_dma)
517{
518 u16 addr = (msg->addr << 1) | ((msg->flags & I2C_M_RD) == I2C_M_RD);
519 int len = 0;
520 int data_len;
521
522 int last = (qup->blk.pos == (qup->blk.count - 1)) && (qup->is_last);
523
524 if (qup->blk.pos == 0) {
525 tags[len++] = QUP_TAG_V2_START;
526 tags[len++] = addr & 0xff;
527
528 if (msg->flags & I2C_M_TEN)
529 tags[len++] = addr >> 8;
530 }
531
532 /* Send _STOP commands for the last block */
533 if (last) {
534 if (msg->flags & I2C_M_RD)
535 tags[len++] = QUP_TAG_V2_DATARD_STOP;
536 else
537 tags[len++] = QUP_TAG_V2_DATAWR_STOP;
538 } else {
539 if (msg->flags & I2C_M_RD)
540 tags[len++] = QUP_TAG_V2_DATARD;
541 else
542 tags[len++] = QUP_TAG_V2_DATAWR;
543 }
544
545 data_len = qup_i2c_get_data_len(qup);
546
547 /* 0 implies 256 bytes */
548 if (data_len == QUP_READ_LIMIT)
549 tags[len++] = 0;
550 else
551 tags[len++] = data_len;
552
553 if ((msg->flags & I2C_M_RD) && last && is_dma) {
554 tags[len++] = QUP_BAM_INPUT_EOT;
555 tags[len++] = QUP_BAM_FLUSH_STOP;
556 }
557
558 return len;
559}
560
561static int qup_i2c_issue_xfer_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
562{
563 int data_len = 0, tag_len, index;
564 int ret;
565
566 tag_len = qup_i2c_set_tags(qup->blk.tags, qup, msg, 0);
567 index = msg->len - qup->blk.data_len;
568
569 /* only tags are written for read */
570 if (!(msg->flags & I2C_M_RD))
571 data_len = qup_i2c_get_data_len(qup);
572
573 ret = qup_i2c_send_data(qup, tag_len, qup->blk.tags,
574 data_len, &msg->buf[index]);
575 qup->blk.data_len -= data_len;
576
577 return ret;
578}
579
580static void qup_i2c_bam_cb(void *data)
581{
582 struct qup_i2c_dev *qup = data;
583
584 complete(&qup->xfer);
585}
586
587static int qup_sg_set_buf(struct scatterlist *sg, void *buf,
588 struct qup_i2c_tag *tg, unsigned int buflen,
589 struct qup_i2c_dev *qup, int map, int dir)
590{
591 int ret;
592
593 sg_set_buf(sg, buf, buflen);
594 ret = dma_map_sg(qup->dev, sg, 1, dir);
595 if (!ret)
596 return -EINVAL;
597
598 if (!map)
599 sg_dma_address(sg) = tg->addr + ((u8 *)buf - tg->start);
600
601 return 0;
602}
603
604static void qup_i2c_rel_dma(struct qup_i2c_dev *qup)
605{
606 if (qup->btx.dma)
607 dma_release_channel(qup->btx.dma);
608 if (qup->brx.dma)
609 dma_release_channel(qup->brx.dma);
610 qup->btx.dma = NULL;
611 qup->brx.dma = NULL;
612}
613
614static int qup_i2c_req_dma(struct qup_i2c_dev *qup)
615{
616 int err;
617
618 if (!qup->btx.dma) {
619 qup->btx.dma = dma_request_slave_channel_reason(qup->dev, "tx");
620 if (IS_ERR(qup->btx.dma)) {
621 err = PTR_ERR(qup->btx.dma);
622 qup->btx.dma = NULL;
623 dev_err(qup->dev, "\n tx channel not available");
624 return err;
625 }
626 }
627
628 if (!qup->brx.dma) {
629 qup->brx.dma = dma_request_slave_channel_reason(qup->dev, "rx");
630 if (IS_ERR(qup->brx.dma)) {
631 dev_err(qup->dev, "\n rx channel not available");
632 err = PTR_ERR(qup->brx.dma);
633 qup->brx.dma = NULL;
634 qup_i2c_rel_dma(qup);
635 return err;
636 }
637 }
638 return 0;
639}
640
641static int qup_i2c_bam_do_xfer(struct qup_i2c_dev *qup, struct i2c_msg *msg,
642 int num)
643{
644 struct dma_async_tx_descriptor *txd, *rxd = NULL;
645 int ret = 0, idx = 0, limit = QUP_READ_LIMIT;
646 dma_cookie_t cookie_rx, cookie_tx;
647 u32 rx_nents = 0, tx_nents = 0, len, blocks, rem;
648 u32 i, tlen, tx_len, tx_buf = 0, rx_buf = 0, off = 0;
649 u8 *tags;
650
651 while (idx < num) {
652 blocks = (msg->len + limit) / limit;
653 rem = msg->len % limit;
654 tx_len = 0, len = 0, i = 0;
655
656 qup->is_last = (idx == (num - 1));
657
658 qup_i2c_set_blk_data(qup, msg);
659
660 if (msg->flags & I2C_M_RD) {
661 rx_nents += (blocks * 2) + 1;
662 tx_nents += 1;
663
664 while (qup->blk.pos < blocks) {
665 /* length set to '0' implies 256 bytes */
666 tlen = (i == (blocks - 1)) ? rem : 0;
667 tags = &qup->start_tag.start[off + len];
668 len += qup_i2c_set_tags(tags, qup, msg, 1);
669
670 /* scratch buf to read the start and len tags */
671 ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++],
672 &qup->brx.tag.start[0],
673 &qup->brx.tag,
674 2, qup, 0, 0);
675
676 if (ret)
677 return ret;
678
679 ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++],
680 &msg->buf[limit * i],
681 NULL, tlen, qup,
682 1, DMA_FROM_DEVICE);
683 if (ret)
684 return ret;
685
686 i++;
687 qup->blk.pos = i;
688 }
689 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
690 &qup->start_tag.start[off],
691 &qup->start_tag, len, qup, 0, 0);
692 if (ret)
693 return ret;
694
695 off += len;
696 /* scratch buf to read the BAM EOT and FLUSH tags */
697 ret = qup_sg_set_buf(&qup->brx.sg[rx_buf++],
698 &qup->brx.tag.start[0],
699 &qup->brx.tag, 2,
700 qup, 0, 0);
701 if (ret)
702 return ret;
703 } else {
704 tx_nents += (blocks * 2);
705
706 while (qup->blk.pos < blocks) {
707 tlen = (i == (blocks - 1)) ? rem : 0;
708 tags = &qup->start_tag.start[off + tx_len];
709 len = qup_i2c_set_tags(tags, qup, msg, 1);
710
711 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
712 tags,
713 &qup->start_tag, len,
714 qup, 0, 0);
715 if (ret)
716 return ret;
717
718 tx_len += len;
719 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
720 &msg->buf[limit * i],
721 NULL, tlen, qup, 1,
722 DMA_TO_DEVICE);
723 if (ret)
724 return ret;
725 i++;
726 qup->blk.pos = i;
727 }
728 off += tx_len;
729
730 if (idx == (num - 1)) {
731 len = 1;
732 if (rx_nents) {
733 qup->btx.tag.start[0] =
734 QUP_BAM_INPUT_EOT;
735 len++;
736 }
737 qup->btx.tag.start[len - 1] =
738 QUP_BAM_FLUSH_STOP;
739 ret = qup_sg_set_buf(&qup->btx.sg[tx_buf++],
740 &qup->btx.tag.start[0],
741 &qup->btx.tag, len,
742 qup, 0, 0);
743 if (ret)
744 return ret;
745 tx_nents += 1;
746 }
747 }
748 idx++;
749 msg++;
750 }
751
752 txd = dmaengine_prep_slave_sg(qup->btx.dma, qup->btx.sg, tx_nents,
753 DMA_MEM_TO_DEV,
754 DMA_PREP_INTERRUPT | DMA_PREP_FENCE);
755 if (!txd) {
756 dev_err(qup->dev, "failed to get tx desc\n");
757 ret = -EINVAL;
758 goto desc_err;
759 }
760
761 if (!rx_nents) {
762 txd->callback = qup_i2c_bam_cb;
763 txd->callback_param = qup;
764 }
765
766 cookie_tx = dmaengine_submit(txd);
767 if (dma_submit_error(cookie_tx)) {
768 ret = -EINVAL;
769 goto desc_err;
770 }
771
772 dma_async_issue_pending(qup->btx.dma);
773
774 if (rx_nents) {
775 rxd = dmaengine_prep_slave_sg(qup->brx.dma, qup->brx.sg,
776 rx_nents, DMA_DEV_TO_MEM,
777 DMA_PREP_INTERRUPT);
778 if (!rxd) {
779 dev_err(qup->dev, "failed to get rx desc\n");
780 ret = -EINVAL;
781
782 /* abort TX descriptors */
783 dmaengine_terminate_all(qup->btx.dma);
784 goto desc_err;
785 }
786
787 rxd->callback = qup_i2c_bam_cb;
788 rxd->callback_param = qup;
789 cookie_rx = dmaengine_submit(rxd);
790 if (dma_submit_error(cookie_rx)) {
791 ret = -EINVAL;
792 goto desc_err;
793 }
794
795 dma_async_issue_pending(qup->brx.dma);
796 }
797
798 if (!wait_for_completion_timeout(&qup->xfer, TOUT_MAX * HZ)) {
799 dev_err(qup->dev, "normal trans timed out\n");
800 ret = -ETIMEDOUT;
801 }
802
803 if (ret || qup->bus_err || qup->qup_err) {
804 if (qup->bus_err & QUP_I2C_NACK_FLAG) {
805 msg--;
806 dev_err(qup->dev, "NACK from %x\n", msg->addr);
807 ret = -EIO;
808
809 if (qup_i2c_change_state(qup, QUP_RUN_STATE)) {
810 dev_err(qup->dev, "change to run state timed out");
811 return ret;
812 }
813
814 if (rx_nents)
815 writel(QUP_BAM_INPUT_EOT,
816 qup->base + QUP_OUT_FIFO_BASE);
817
818 writel(QUP_BAM_FLUSH_STOP,
819 qup->base + QUP_OUT_FIFO_BASE);
820
821 qup_i2c_flush(qup);
822
823 /* wait for remaining interrupts to occur */
824 if (!wait_for_completion_timeout(&qup->xfer, HZ))
825 dev_err(qup->dev, "flush timed out\n");
826
827 qup_i2c_rel_dma(qup);
828 }
829 }
830
831 dma_unmap_sg(qup->dev, qup->btx.sg, tx_nents, DMA_TO_DEVICE);
832
833 if (rx_nents)
834 dma_unmap_sg(qup->dev, qup->brx.sg, rx_nents,
835 DMA_FROM_DEVICE);
836desc_err:
837 return ret;
838}
839
840static int qup_i2c_bam_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
841 int num)
842{
843 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
844 int ret = 0;
845
846 enable_irq(qup->irq);
847 ret = qup_i2c_req_dma(qup);
848
849 if (ret)
850 goto out;
851
852 qup->bus_err = 0;
853 qup->qup_err = 0;
854
855 writel(0, qup->base + QUP_MX_INPUT_CNT);
856 writel(0, qup->base + QUP_MX_OUTPUT_CNT);
857
858 /* set BAM mode */
859 writel(QUP_REPACK_EN | QUP_BAM_MODE, qup->base + QUP_IO_MODE);
860
861 /* mask fifo irqs */
862 writel((0x3 << 8), qup->base + QUP_OPERATIONAL_MASK);
863
864 /* set RUN STATE */
865 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
866 if (ret)
867 goto out;
868
869 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
870
871 qup->msg = msg;
872 ret = qup_i2c_bam_do_xfer(qup, qup->msg, num);
873out:
874 disable_irq(qup->irq);
875
876 qup->msg = NULL;
877 return ret;
878}
879
880static int qup_i2c_wait_for_complete(struct qup_i2c_dev *qup,
881 struct i2c_msg *msg)
306{ 882{
307 unsigned long left; 883 unsigned long left;
884 int ret = 0;
885
886 left = wait_for_completion_timeout(&qup->xfer, HZ);
887 if (!left) {
888 writel(1, qup->base + QUP_SW_RESET);
889 ret = -ETIMEDOUT;
890 }
891
892 if (qup->bus_err || qup->qup_err) {
893 if (qup->bus_err & QUP_I2C_NACK_FLAG) {
894 dev_err(qup->dev, "NACK from %x\n", msg->addr);
895 ret = -EIO;
896 }
897 }
898
899 return ret;
900}
901
902static int qup_i2c_write_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
903{
904 int ret = 0;
905
906 qup->msg = msg;
907 qup->pos = 0;
908 enable_irq(qup->irq);
909 qup_i2c_set_blk_data(qup, msg);
910 qup_i2c_set_write_mode_v2(qup, msg);
911
912 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
913 if (ret)
914 goto err;
915
916 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
917
918 do {
919 ret = qup_i2c_issue_xfer_v2(qup, msg);
920 if (ret)
921 goto err;
922
923 ret = qup_i2c_wait_for_complete(qup, msg);
924 if (ret)
925 goto err;
926
927 qup->blk.pos++;
928 } while (qup->blk.pos < qup->blk.count);
929
930 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
931
932err:
933 disable_irq(qup->irq);
934 qup->msg = NULL;
935
936 return ret;
937}
938
939static int qup_i2c_write_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
940{
308 int ret; 941 int ret;
309 942
310 qup->msg = msg; 943 qup->msg = msg;
@@ -325,30 +958,21 @@ static int qup_i2c_write_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
325 if (ret) 958 if (ret)
326 goto err; 959 goto err;
327 960
328 qup_i2c_issue_write(qup, msg); 961 ret = qup_i2c_issue_write(qup, msg);
329
330 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
331 if (ret) 962 if (ret)
332 goto err; 963 goto err;
333 964
334 left = wait_for_completion_timeout(&qup->xfer, HZ); 965 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
335 if (!left) { 966 if (ret)
336 writel(1, qup->base + QUP_SW_RESET);
337 ret = -ETIMEDOUT;
338 goto err; 967 goto err;
339 }
340 968
341 if (qup->bus_err || qup->qup_err) { 969 ret = qup_i2c_wait_for_complete(qup, msg);
342 if (qup->bus_err & QUP_I2C_NACK_FLAG) 970 if (ret)
343 dev_err(qup->dev, "NACK from %x\n", msg->addr);
344 ret = -EIO;
345 goto err; 971 goto err;
346 }
347 } while (qup->pos < msg->len); 972 } while (qup->pos < msg->len);
348 973
349 /* Wait for the outstanding data in the fifo to drain */ 974 /* Wait for the outstanding data in the fifo to drain */
350 ret = qup_i2c_wait_writeready(qup); 975 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
351
352err: 976err:
353 disable_irq(qup->irq); 977 disable_irq(qup->irq);
354 qup->msg = NULL; 978 qup->msg = NULL;
@@ -370,6 +994,28 @@ static void qup_i2c_set_read_mode(struct qup_i2c_dev *qup, int len)
370 } 994 }
371} 995}
372 996
997static void qup_i2c_set_read_mode_v2(struct qup_i2c_dev *qup, int len)
998{
999 int tx_len = qup->blk.tx_tag_len;
1000
1001 len += qup->blk.rx_tag_len;
1002 len |= qup->config_run;
1003 tx_len |= qup->config_run;
1004
1005 if (len < qup->in_fifo_sz) {
1006 /* FIFO mode */
1007 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
1008 writel(tx_len, qup->base + QUP_MX_WRITE_CNT);
1009 writel(len, qup->base + QUP_MX_READ_CNT);
1010 } else {
1011 /* BLOCK mode (transfer data on chunks) */
1012 writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
1013 qup->base + QUP_IO_MODE);
1014 writel(tx_len, qup->base + QUP_MX_OUTPUT_CNT);
1015 writel(len, qup->base + QUP_MX_INPUT_CNT);
1016 }
1017}
1018
373static void qup_i2c_issue_read(struct qup_i2c_dev *qup, struct i2c_msg *msg) 1019static void qup_i2c_issue_read(struct qup_i2c_dev *qup, struct i2c_msg *msg)
374{ 1020{
375 u32 addr, len, val; 1021 u32 addr, len, val;
@@ -384,18 +1030,19 @@ static void qup_i2c_issue_read(struct qup_i2c_dev *qup, struct i2c_msg *msg)
384} 1030}
385 1031
386 1032
387static void qup_i2c_read_fifo(struct qup_i2c_dev *qup, struct i2c_msg *msg) 1033static int qup_i2c_read_fifo(struct qup_i2c_dev *qup, struct i2c_msg *msg)
388{ 1034{
389 u32 opflags;
390 u32 val = 0; 1035 u32 val = 0;
391 int idx; 1036 int idx;
1037 int ret = 0;
392 1038
393 for (idx = 0; qup->pos < msg->len; idx++) { 1039 for (idx = 0; qup->pos < msg->len; idx++) {
394 if ((idx & 1) == 0) { 1040 if ((idx & 1) == 0) {
395 /* Check that FIFO have data */ 1041 /* Check that FIFO have data */
396 opflags = readl(qup->base + QUP_OPERATIONAL); 1042 ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
397 if (!(opflags & QUP_IN_NOT_EMPTY)) 1043 SET_BIT, 4 * ONE_BYTE);
398 break; 1044 if (ret)
1045 return ret;
399 1046
400 /* Reading 2 words at time */ 1047 /* Reading 2 words at time */
401 val = readl(qup->base + QUP_IN_FIFO_BASE); 1048 val = readl(qup->base + QUP_IN_FIFO_BASE);
@@ -405,18 +1052,94 @@ static void qup_i2c_read_fifo(struct qup_i2c_dev *qup, struct i2c_msg *msg)
405 msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT; 1052 msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT;
406 } 1053 }
407 } 1054 }
1055
1056 return ret;
1057}
1058
1059static int qup_i2c_read_fifo_v2(struct qup_i2c_dev *qup,
1060 struct i2c_msg *msg)
1061{
1062 u32 val;
1063 int idx, pos = 0, ret = 0, total;
1064
1065 total = qup_i2c_get_data_len(qup);
1066
1067 /* 2 extra bytes for read tags */
1068 while (pos < (total + 2)) {
1069 /* Check that FIFO have data */
1070 ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
1071 SET_BIT, 4 * ONE_BYTE);
1072 if (ret) {
1073 dev_err(qup->dev, "timeout for fifo not empty");
1074 return ret;
1075 }
1076 val = readl(qup->base + QUP_IN_FIFO_BASE);
1077
1078 for (idx = 0; idx < 4; idx++, val >>= 8, pos++) {
1079 /* first 2 bytes are tag bytes */
1080 if (pos < 2)
1081 continue;
1082
1083 if (pos >= (total + 2))
1084 goto out;
1085
1086 msg->buf[qup->pos++] = val & 0xff;
1087 }
1088 }
1089
1090out:
1091 qup->blk.data_len -= total;
1092
1093 return ret;
1094}
1095
1096static int qup_i2c_read_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
1097{
1098 int ret = 0;
1099
1100 qup->msg = msg;
1101 qup->pos = 0;
1102 enable_irq(qup->irq);
1103 qup_i2c_set_blk_data(qup, msg);
1104 qup_i2c_set_read_mode_v2(qup, msg->len);
1105
1106 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1107 if (ret)
1108 goto err;
1109
1110 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
1111
1112 do {
1113 ret = qup_i2c_issue_xfer_v2(qup, msg);
1114 if (ret)
1115 goto err;
1116
1117 ret = qup_i2c_wait_for_complete(qup, msg);
1118 if (ret)
1119 goto err;
1120
1121 ret = qup_i2c_read_fifo_v2(qup, msg);
1122 if (ret)
1123 goto err;
1124
1125 qup->blk.pos++;
1126 } while (qup->blk.pos < qup->blk.count);
1127
1128err:
1129 disable_irq(qup->irq);
1130 qup->msg = NULL;
1131
1132 return ret;
408} 1133}
409 1134
410static int qup_i2c_read_one(struct qup_i2c_dev *qup, struct i2c_msg *msg) 1135static int qup_i2c_read_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
411{ 1136{
412 unsigned long left;
413 int ret; 1137 int ret;
414 1138
415 qup->msg = msg; 1139 qup->msg = msg;
416 qup->pos = 0; 1140 qup->pos = 0;
417 1141
418 enable_irq(qup->irq); 1142 enable_irq(qup->irq);
419
420 qup_i2c_set_read_mode(qup, msg->len); 1143 qup_i2c_set_read_mode(qup, msg->len);
421 1144
422 ret = qup_i2c_change_state(qup, QUP_RUN_STATE); 1145 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
@@ -436,21 +1159,13 @@ static int qup_i2c_read_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
436 goto err; 1159 goto err;
437 1160
438 do { 1161 do {
439 left = wait_for_completion_timeout(&qup->xfer, HZ); 1162 ret = qup_i2c_wait_for_complete(qup, msg);
440 if (!left) { 1163 if (ret)
441 writel(1, qup->base + QUP_SW_RESET);
442 ret = -ETIMEDOUT;
443 goto err; 1164 goto err;
444 }
445 1165
446 if (qup->bus_err || qup->qup_err) { 1166 ret = qup_i2c_read_fifo(qup, msg);
447 if (qup->bus_err & QUP_I2C_NACK_FLAG) 1167 if (ret)
448 dev_err(qup->dev, "NACK from %x\n", msg->addr);
449 ret = -EIO;
450 goto err; 1168 goto err;
451 }
452
453 qup_i2c_read_fifo(qup, msg);
454 } while (qup->pos < msg->len); 1169 } while (qup->pos < msg->len);
455 1170
456err: 1171err:
@@ -513,6 +1228,87 @@ out:
513 return ret; 1228 return ret;
514} 1229}
515 1230
1231static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
1232 struct i2c_msg msgs[],
1233 int num)
1234{
1235 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
1236 int ret, len, idx = 0, use_dma = 0;
1237
1238 ret = pm_runtime_get_sync(qup->dev);
1239 if (ret < 0)
1240 goto out;
1241
1242 writel(1, qup->base + QUP_SW_RESET);
1243 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
1244 if (ret)
1245 goto out;
1246
1247 /* Configure QUP as I2C mini core */
1248 writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG);
1249 writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN);
1250
1251 if ((qup->is_dma)) {
1252 /* All i2c_msgs should be transferred using either dma or cpu */
1253 for (idx = 0; idx < num; idx++) {
1254 if (msgs[idx].len == 0) {
1255 ret = -EINVAL;
1256 goto out;
1257 }
1258
1259 len = (msgs[idx].len > qup->out_fifo_sz) ||
1260 (msgs[idx].len > qup->in_fifo_sz);
1261
1262 if ((!is_vmalloc_addr(msgs[idx].buf)) && len) {
1263 use_dma = 1;
1264 } else {
1265 use_dma = 0;
1266 break;
1267 }
1268 }
1269 }
1270
1271 do {
1272 if (msgs[idx].len == 0) {
1273 ret = -EINVAL;
1274 goto out;
1275 }
1276
1277 if (qup_i2c_poll_state_i2c_master(qup)) {
1278 ret = -EIO;
1279 goto out;
1280 }
1281
1282 qup->is_last = (idx == (num - 1));
1283 if (idx)
1284 qup->config_run = QUP_I2C_MX_CONFIG_DURING_RUN;
1285 else
1286 qup->config_run = 0;
1287
1288 reinit_completion(&qup->xfer);
1289
1290 if (use_dma) {
1291 ret = qup_i2c_bam_xfer(adap, &msgs[idx], num);
1292 } else {
1293 if (msgs[idx].flags & I2C_M_RD)
1294 ret = qup_i2c_read_one_v2(qup, &msgs[idx]);
1295 else
1296 ret = qup_i2c_write_one_v2(qup, &msgs[idx]);
1297 }
1298 } while ((idx++ < (num - 1)) && !use_dma && !ret);
1299
1300 if (!ret)
1301 ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
1302
1303 if (ret == 0)
1304 ret = num;
1305out:
1306 pm_runtime_mark_last_busy(qup->dev);
1307 pm_runtime_put_autosuspend(qup->dev);
1308
1309 return ret;
1310}
1311
516static u32 qup_i2c_func(struct i2c_adapter *adap) 1312static u32 qup_i2c_func(struct i2c_adapter *adap)
517{ 1313{
518 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); 1314 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
@@ -523,6 +1319,11 @@ static const struct i2c_algorithm qup_i2c_algo = {
523 .functionality = qup_i2c_func, 1319 .functionality = qup_i2c_func,
524}; 1320};
525 1321
1322static const struct i2c_algorithm qup_i2c_algo_v2 = {
1323 .master_xfer = qup_i2c_xfer_v2,
1324 .functionality = qup_i2c_func,
1325};
1326
526/* 1327/*
527 * The QUP block will issue a NACK and STOP on the bus when reaching 1328 * The QUP block will issue a NACK and STOP on the bus when reaching
528 * the end of the read, the length of the read is specified as one byte 1329 * the end of the read, the length of the read is specified as one byte
@@ -561,6 +1362,7 @@ static int qup_i2c_probe(struct platform_device *pdev)
561 int ret, fs_div, hs_div; 1362 int ret, fs_div, hs_div;
562 int src_clk_freq; 1363 int src_clk_freq;
563 u32 clk_freq = 100000; 1364 u32 clk_freq = 100000;
1365 int blocks;
564 1366
565 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL); 1367 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
566 if (!qup) 1368 if (!qup)
@@ -572,6 +1374,68 @@ static int qup_i2c_probe(struct platform_device *pdev)
572 1374
573 of_property_read_u32(node, "clock-frequency", &clk_freq); 1375 of_property_read_u32(node, "clock-frequency", &clk_freq);
574 1376
1377 if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) {
1378 qup->adap.algo = &qup_i2c_algo;
1379 qup->adap.quirks = &qup_i2c_quirks;
1380 } else {
1381 qup->adap.algo = &qup_i2c_algo_v2;
1382 ret = qup_i2c_req_dma(qup);
1383
1384 if (ret == -EPROBE_DEFER)
1385 goto fail_dma;
1386 else if (ret != 0)
1387 goto nodma;
1388
1389 blocks = (MX_BLOCKS << 1) + 1;
1390 qup->btx.sg = devm_kzalloc(&pdev->dev,
1391 sizeof(*qup->btx.sg) * blocks,
1392 GFP_KERNEL);
1393 if (!qup->btx.sg) {
1394 ret = -ENOMEM;
1395 goto fail_dma;
1396 }
1397 sg_init_table(qup->btx.sg, blocks);
1398
1399 qup->brx.sg = devm_kzalloc(&pdev->dev,
1400 sizeof(*qup->brx.sg) * blocks,
1401 GFP_KERNEL);
1402 if (!qup->brx.sg) {
1403 ret = -ENOMEM;
1404 goto fail_dma;
1405 }
1406 sg_init_table(qup->brx.sg, blocks);
1407
1408 /* 2 tag bytes for each block + 5 for start, stop tags */
1409 size = blocks * 2 + 5;
1410 qup->dpool = dma_pool_create("qup_i2c-dma-pool", &pdev->dev,
1411 size, 4, 0);
1412
1413 qup->start_tag.start = dma_pool_alloc(qup->dpool, GFP_KERNEL,
1414 &qup->start_tag.addr);
1415 if (!qup->start_tag.start) {
1416 ret = -ENOMEM;
1417 goto fail_dma;
1418 }
1419
1420 qup->brx.tag.start = dma_pool_alloc(qup->dpool,
1421 GFP_KERNEL,
1422 &qup->brx.tag.addr);
1423 if (!qup->brx.tag.start) {
1424 ret = -ENOMEM;
1425 goto fail_dma;
1426 }
1427
1428 qup->btx.tag.start = dma_pool_alloc(qup->dpool,
1429 GFP_KERNEL,
1430 &qup->btx.tag.addr);
1431 if (!qup->btx.tag.start) {
1432 ret = -ENOMEM;
1433 goto fail_dma;
1434 }
1435 qup->is_dma = true;
1436 }
1437
1438nodma:
575 /* We support frequencies up to FAST Mode (400KHz) */ 1439 /* We support frequencies up to FAST Mode (400KHz) */
576 if (!clk_freq || clk_freq > 400000) { 1440 if (!clk_freq || clk_freq > 400000) {
577 dev_err(qup->dev, "clock frequency not supported %d\n", 1441 dev_err(qup->dev, "clock frequency not supported %d\n",
@@ -667,10 +1531,10 @@ static int qup_i2c_probe(struct platform_device *pdev)
667 qup->out_blk_sz, qup->out_fifo_sz); 1531 qup->out_blk_sz, qup->out_fifo_sz);
668 1532
669 i2c_set_adapdata(&qup->adap, qup); 1533 i2c_set_adapdata(&qup->adap, qup);
670 qup->adap.algo = &qup_i2c_algo;
671 qup->adap.quirks = &qup_i2c_quirks;
672 qup->adap.dev.parent = qup->dev; 1534 qup->adap.dev.parent = qup->dev;
673 qup->adap.dev.of_node = pdev->dev.of_node; 1535 qup->adap.dev.of_node = pdev->dev.of_node;
1536 qup->is_last = true;
1537
674 strlcpy(qup->adap.name, "QUP I2C adapter", sizeof(qup->adap.name)); 1538 strlcpy(qup->adap.name, "QUP I2C adapter", sizeof(qup->adap.name));
675 1539
676 pm_runtime_set_autosuspend_delay(qup->dev, MSEC_PER_SEC); 1540 pm_runtime_set_autosuspend_delay(qup->dev, MSEC_PER_SEC);
@@ -689,6 +1553,11 @@ fail_runtime:
689 pm_runtime_set_suspended(qup->dev); 1553 pm_runtime_set_suspended(qup->dev);
690fail: 1554fail:
691 qup_i2c_disable_clocks(qup); 1555 qup_i2c_disable_clocks(qup);
1556fail_dma:
1557 if (qup->btx.dma)
1558 dma_release_channel(qup->btx.dma);
1559 if (qup->brx.dma)
1560 dma_release_channel(qup->brx.dma);
692 return ret; 1561 return ret;
693} 1562}
694 1563
@@ -696,6 +1565,18 @@ static int qup_i2c_remove(struct platform_device *pdev)
696{ 1565{
697 struct qup_i2c_dev *qup = platform_get_drvdata(pdev); 1566 struct qup_i2c_dev *qup = platform_get_drvdata(pdev);
698 1567
1568 if (qup->is_dma) {
1569 dma_pool_free(qup->dpool, qup->start_tag.start,
1570 qup->start_tag.addr);
1571 dma_pool_free(qup->dpool, qup->brx.tag.start,
1572 qup->brx.tag.addr);
1573 dma_pool_free(qup->dpool, qup->btx.tag.start,
1574 qup->btx.tag.addr);
1575 dma_pool_destroy(qup->dpool);
1576 dma_release_channel(qup->btx.dma);
1577 dma_release_channel(qup->brx.dma);
1578 }
1579
699 disable_irq(qup->irq); 1580 disable_irq(qup->irq);
700 qup_i2c_disable_clocks(qup); 1581 qup_i2c_disable_clocks(qup);
701 i2c_del_adapter(&qup->adap); 1582 i2c_del_adapter(&qup->adap);
diff --git a/drivers/i2c/busses/i2c-rcar.c b/drivers/i2c/busses/i2c-rcar.c
index 1abeadc8ab79..68ecb5630ad5 100644
--- a/drivers/i2c/busses/i2c-rcar.c
+++ b/drivers/i2c/busses/i2c-rcar.c
@@ -611,7 +611,7 @@ static int rcar_i2c_probe(struct platform_device *pdev)
611 if (IS_ERR(priv->io)) 611 if (IS_ERR(priv->io))
612 return PTR_ERR(priv->io); 612 return PTR_ERR(priv->io);
613 613
614 priv->devtype = (enum rcar_i2c_type)of_match_device(rcar_i2c_dt_ids, dev)->data; 614 priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev);
615 init_waitqueue_head(&priv->wait); 615 init_waitqueue_head(&priv->wait);
616 616
617 adap = &priv->adap; 617 adap = &priv->adap;
diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
index a0522fcc4ff8..929185a7296c 100644
--- a/drivers/i2c/busses/i2c-tegra.c
+++ b/drivers/i2c/busses/i2c-tegra.c
@@ -805,9 +805,7 @@ static int tegra_i2c_probe(struct platform_device *pdev)
805 i2c_dev->hw = &tegra20_i2c_hw; 805 i2c_dev->hw = &tegra20_i2c_hw;
806 806
807 if (pdev->dev.of_node) { 807 if (pdev->dev.of_node) {
808 const struct of_device_id *match; 808 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
809 match = of_match_device(tegra_i2c_of_match, &pdev->dev);
810 i2c_dev->hw = match->data;
811 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node, 809 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
812 "nvidia,tegra20-i2c-dvc"); 810 "nvidia,tegra20-i2c-dvc");
813 } else if (pdev->id == 3) { 811 } else if (pdev->id == 3) {
diff --git a/drivers/i2c/busses/i2c-xiic.c b/drivers/i2c/busses/i2c-xiic.c
index 6efd20095d5d..74f54f2f471f 100644
--- a/drivers/i2c/busses/i2c-xiic.c
+++ b/drivers/i2c/busses/i2c-xiic.c
@@ -37,6 +37,8 @@
37#include <linux/io.h> 37#include <linux/io.h>
38#include <linux/slab.h> 38#include <linux/slab.h>
39#include <linux/of.h> 39#include <linux/of.h>
40#include <linux/clk.h>
41#include <linux/pm_runtime.h>
40 42
41#define DRIVER_NAME "xiic-i2c" 43#define DRIVER_NAME "xiic-i2c"
42 44
@@ -66,6 +68,7 @@ enum xiic_endian {
66 * @endianness: big/little-endian byte order 68 * @endianness: big/little-endian byte order
67 */ 69 */
68struct xiic_i2c { 70struct xiic_i2c {
71 struct device *dev;
69 void __iomem *base; 72 void __iomem *base;
70 wait_queue_head_t wait; 73 wait_queue_head_t wait;
71 struct i2c_adapter adap; 74 struct i2c_adapter adap;
@@ -77,6 +80,7 @@ struct xiic_i2c {
77 struct i2c_msg *rx_msg; 80 struct i2c_msg *rx_msg;
78 int rx_pos; 81 int rx_pos;
79 enum xiic_endian endianness; 82 enum xiic_endian endianness;
83 struct clk *clk;
80}; 84};
81 85
82 86
@@ -164,6 +168,7 @@ struct xiic_i2c {
164 168
165#define XIIC_RESET_MASK 0xAUL 169#define XIIC_RESET_MASK 0xAUL
166 170
171#define XIIC_PM_TIMEOUT 1000 /* ms */
167/* 172/*
168 * The following constant is used for the device global interrupt enable 173 * The following constant is used for the device global interrupt enable
169 * register, to enable all interrupts for the device, this is the only bit 174 * register, to enable all interrupts for the device, this is the only bit
@@ -676,9 +681,13 @@ static int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
676 dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__, 681 dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__,
677 xiic_getreg8(i2c, XIIC_SR_REG_OFFSET)); 682 xiic_getreg8(i2c, XIIC_SR_REG_OFFSET));
678 683
684 err = pm_runtime_get_sync(i2c->dev);
685 if (err < 0)
686 return err;
687
679 err = xiic_busy(i2c); 688 err = xiic_busy(i2c);
680 if (err) 689 if (err)
681 return err; 690 goto out;
682 691
683 i2c->tx_msg = msgs; 692 i2c->tx_msg = msgs;
684 i2c->nmsgs = num; 693 i2c->nmsgs = num;
@@ -686,14 +695,20 @@ static int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
686 xiic_start_xfer(i2c); 695 xiic_start_xfer(i2c);
687 696
688 if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) || 697 if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
689 (i2c->state == STATE_DONE), HZ)) 698 (i2c->state == STATE_DONE), HZ)) {
690 return (i2c->state == STATE_DONE) ? num : -EIO; 699 err = (i2c->state == STATE_DONE) ? num : -EIO;
691 else { 700 goto out;
701 } else {
692 i2c->tx_msg = NULL; 702 i2c->tx_msg = NULL;
693 i2c->rx_msg = NULL; 703 i2c->rx_msg = NULL;
694 i2c->nmsgs = 0; 704 i2c->nmsgs = 0;
695 return -ETIMEDOUT; 705 err = -ETIMEDOUT;
706 goto out;
696 } 707 }
708out:
709 pm_runtime_mark_last_busy(i2c->dev);
710 pm_runtime_put_autosuspend(i2c->dev);
711 return err;
697} 712}
698 713
699static u32 xiic_func(struct i2c_adapter *adap) 714static u32 xiic_func(struct i2c_adapter *adap)
@@ -748,13 +763,28 @@ static int xiic_i2c_probe(struct platform_device *pdev)
748 mutex_init(&i2c->lock); 763 mutex_init(&i2c->lock);
749 init_waitqueue_head(&i2c->wait); 764 init_waitqueue_head(&i2c->wait);
750 765
766 i2c->clk = devm_clk_get(&pdev->dev, NULL);
767 if (IS_ERR(i2c->clk)) {
768 dev_err(&pdev->dev, "input clock not found.\n");
769 return PTR_ERR(i2c->clk);
770 }
771 ret = clk_prepare_enable(i2c->clk);
772 if (ret) {
773 dev_err(&pdev->dev, "Unable to enable clock.\n");
774 return ret;
775 }
776 i2c->dev = &pdev->dev;
777 pm_runtime_enable(i2c->dev);
778 pm_runtime_set_autosuspend_delay(i2c->dev, XIIC_PM_TIMEOUT);
779 pm_runtime_use_autosuspend(i2c->dev);
780 pm_runtime_set_active(i2c->dev);
751 ret = devm_request_threaded_irq(&pdev->dev, irq, xiic_isr, 781 ret = devm_request_threaded_irq(&pdev->dev, irq, xiic_isr,
752 xiic_process, IRQF_ONESHOT, 782 xiic_process, IRQF_ONESHOT,
753 pdev->name, i2c); 783 pdev->name, i2c);
754 784
755 if (ret < 0) { 785 if (ret < 0) {
756 dev_err(&pdev->dev, "Cannot claim IRQ\n"); 786 dev_err(&pdev->dev, "Cannot claim IRQ\n");
757 return ret; 787 goto err_clk_dis;
758 } 788 }
759 789
760 /* 790 /*
@@ -776,7 +806,7 @@ static int xiic_i2c_probe(struct platform_device *pdev)
776 if (ret) { 806 if (ret) {
777 dev_err(&pdev->dev, "Failed to add adapter\n"); 807 dev_err(&pdev->dev, "Failed to add adapter\n");
778 xiic_deinit(i2c); 808 xiic_deinit(i2c);
779 return ret; 809 goto err_clk_dis;
780 } 810 }
781 811
782 if (pdata) { 812 if (pdata) {
@@ -786,16 +816,30 @@ static int xiic_i2c_probe(struct platform_device *pdev)
786 } 816 }
787 817
788 return 0; 818 return 0;
819
820err_clk_dis:
821 pm_runtime_set_suspended(&pdev->dev);
822 pm_runtime_disable(&pdev->dev);
823 clk_disable_unprepare(i2c->clk);
824 return ret;
789} 825}
790 826
791static int xiic_i2c_remove(struct platform_device *pdev) 827static int xiic_i2c_remove(struct platform_device *pdev)
792{ 828{
793 struct xiic_i2c *i2c = platform_get_drvdata(pdev); 829 struct xiic_i2c *i2c = platform_get_drvdata(pdev);
830 int ret;
794 831
795 /* remove adapter & data */ 832 /* remove adapter & data */
796 i2c_del_adapter(&i2c->adap); 833 i2c_del_adapter(&i2c->adap);
797 834
835 ret = clk_prepare_enable(i2c->clk);
836 if (ret) {
837 dev_err(&pdev->dev, "Unable to enable clock.\n");
838 return ret;
839 }
798 xiic_deinit(i2c); 840 xiic_deinit(i2c);
841 clk_disable_unprepare(i2c->clk);
842 pm_runtime_disable(&pdev->dev);
799 843
800 return 0; 844 return 0;
801} 845}
@@ -808,12 +852,42 @@ static const struct of_device_id xiic_of_match[] = {
808MODULE_DEVICE_TABLE(of, xiic_of_match); 852MODULE_DEVICE_TABLE(of, xiic_of_match);
809#endif 853#endif
810 854
855static int __maybe_unused cdns_i2c_runtime_suspend(struct device *dev)
856{
857 struct platform_device *pdev = to_platform_device(dev);
858 struct xiic_i2c *i2c = platform_get_drvdata(pdev);
859
860 clk_disable(i2c->clk);
861
862 return 0;
863}
864
865static int __maybe_unused cdns_i2c_runtime_resume(struct device *dev)
866{
867 struct platform_device *pdev = to_platform_device(dev);
868 struct xiic_i2c *i2c = platform_get_drvdata(pdev);
869 int ret;
870
871 ret = clk_enable(i2c->clk);
872 if (ret) {
873 dev_err(dev, "Cannot enable clock.\n");
874 return ret;
875 }
876
877 return 0;
878}
879
880static const struct dev_pm_ops xiic_dev_pm_ops = {
881 SET_RUNTIME_PM_OPS(cdns_i2c_runtime_suspend,
882 cdns_i2c_runtime_resume, NULL)
883};
811static struct platform_driver xiic_i2c_driver = { 884static struct platform_driver xiic_i2c_driver = {
812 .probe = xiic_i2c_probe, 885 .probe = xiic_i2c_probe,
813 .remove = xiic_i2c_remove, 886 .remove = xiic_i2c_remove,
814 .driver = { 887 .driver = {
815 .name = DRIVER_NAME, 888 .name = DRIVER_NAME,
816 .of_match_table = of_match_ptr(xiic_of_match), 889 .of_match_table = of_match_ptr(xiic_of_match),
890 .pm = &xiic_dev_pm_ops,
817 }, 891 },
818}; 892};
819 893
diff --git a/drivers/i2c/i2c-boardinfo.c b/drivers/i2c/i2c-boardinfo.c
index 90e322959303..e33022e2d459 100644
--- a/drivers/i2c/i2c-boardinfo.c
+++ b/drivers/i2c/i2c-boardinfo.c
@@ -12,11 +12,11 @@
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 */ 13 */
14 14
15#include <linux/kernel.h>
16#include <linux/i2c.h>
17#include <linux/slab.h>
18#include <linux/export.h> 15#include <linux/export.h>
16#include <linux/i2c.h>
17#include <linux/kernel.h>
19#include <linux/rwsem.h> 18#include <linux/rwsem.h>
19#include <linux/slab.h>
20 20
21#include "i2c-core.h" 21#include "i2c-core.h"
22 22
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index ffe715d346d8..0f2f8484e8ec 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -28,32 +28,32 @@
28 */ 28 */
29 29
30#include <dt-bindings/i2c/i2c.h> 30#include <dt-bindings/i2c/i2c.h>
31#include <linux/module.h> 31#include <asm/uaccess.h>
32#include <linux/kernel.h> 32#include <linux/acpi.h>
33#include <linux/clk/clk-conf.h>
34#include <linux/completion.h>
33#include <linux/delay.h> 35#include <linux/delay.h>
36#include <linux/err.h>
34#include <linux/errno.h> 37#include <linux/errno.h>
35#include <linux/gpio.h> 38#include <linux/gpio.h>
36#include <linux/slab.h> 39#include <linux/hardirq.h>
37#include <linux/i2c.h> 40#include <linux/i2c.h>
38#include <linux/init.h>
39#include <linux/idr.h> 41#include <linux/idr.h>
42#include <linux/init.h>
43#include <linux/irqflags.h>
44#include <linux/jump_label.h>
45#include <linux/kernel.h>
46#include <linux/module.h>
40#include <linux/mutex.h> 47#include <linux/mutex.h>
41#include <linux/of.h>
42#include <linux/of_device.h> 48#include <linux/of_device.h>
49#include <linux/of.h>
43#include <linux/of_irq.h> 50#include <linux/of_irq.h>
44#include <linux/clk/clk-conf.h>
45#include <linux/completion.h>
46#include <linux/hardirq.h>
47#include <linux/irqflags.h>
48#include <linux/rwsem.h>
49#include <linux/pm_runtime.h>
50#include <linux/pm_domain.h> 51#include <linux/pm_domain.h>
52#include <linux/pm_runtime.h>
51#include <linux/pm_wakeirq.h> 53#include <linux/pm_wakeirq.h>
52#include <linux/acpi.h>
53#include <linux/jump_label.h>
54#include <asm/uaccess.h>
55#include <linux/err.h>
56#include <linux/property.h> 54#include <linux/property.h>
55#include <linux/rwsem.h>
56#include <linux/slab.h>
57 57
58#include "i2c-core.h" 58#include "i2c-core.h"
59 59
@@ -73,6 +73,7 @@ static struct device_type i2c_client_type;
73static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); 73static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
74 74
75static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE; 75static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
76static bool is_registered;
76 77
77void i2c_transfer_trace_reg(void) 78void i2c_transfer_trace_reg(void)
78{ 79{
@@ -1529,7 +1530,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
1529 int res = 0; 1530 int res = 0;
1530 1531
1531 /* Can't register until after driver model init */ 1532 /* Can't register until after driver model init */
1532 if (unlikely(WARN_ON(!i2c_bus_type.p))) { 1533 if (WARN_ON(!is_registered)) {
1533 res = -EAGAIN; 1534 res = -EAGAIN;
1534 goto out_list; 1535 goto out_list;
1535 } 1536 }
@@ -1926,7 +1927,7 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1926 int res; 1927 int res;
1927 1928
1928 /* Can't register until after driver model init */ 1929 /* Can't register until after driver model init */
1929 if (unlikely(WARN_ON(!i2c_bus_type.p))) 1930 if (WARN_ON(!is_registered))
1930 return -EAGAIN; 1931 return -EAGAIN;
1931 1932
1932 /* add the driver to the list of i2c drivers in the driver core */ 1933 /* add the driver to the list of i2c drivers in the driver core */
@@ -2104,6 +2105,9 @@ static int __init i2c_init(void)
2104 retval = bus_register(&i2c_bus_type); 2105 retval = bus_register(&i2c_bus_type);
2105 if (retval) 2106 if (retval)
2106 return retval; 2107 return retval;
2108
2109 is_registered = true;
2110
2107#ifdef CONFIG_I2C_COMPAT 2111#ifdef CONFIG_I2C_COMPAT
2108 i2c_adapter_compat_class = class_compat_register("i2c-adapter"); 2112 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2109 if (!i2c_adapter_compat_class) { 2113 if (!i2c_adapter_compat_class) {
@@ -2125,6 +2129,7 @@ class_err:
2125 class_compat_unregister(i2c_adapter_compat_class); 2129 class_compat_unregister(i2c_adapter_compat_class);
2126bus_err: 2130bus_err:
2127#endif 2131#endif
2132 is_registered = false;
2128 bus_unregister(&i2c_bus_type); 2133 bus_unregister(&i2c_bus_type);
2129 return retval; 2134 return retval;
2130} 2135}
diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
index 2413ec9f8207..0b1108d3c2f3 100644
--- a/drivers/i2c/i2c-dev.c
+++ b/drivers/i2c/i2c-dev.c
@@ -22,17 +22,17 @@
22 22
23/* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */ 23/* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
24 24
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/device.h> 25#include <linux/device.h>
28#include <linux/notifier.h>
29#include <linux/fs.h> 26#include <linux/fs.h>
30#include <linux/slab.h>
31#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/i2c.h>
34#include <linux/i2c-dev.h> 27#include <linux/i2c-dev.h>
28#include <linux/i2c.h>
29#include <linux/init.h>
35#include <linux/jiffies.h> 30#include <linux/jiffies.h>
31#include <linux/kernel.h>
32#include <linux/list.h>
33#include <linux/module.h>
34#include <linux/notifier.h>
35#include <linux/slab.h>
36#include <linux/uaccess.h> 36#include <linux/uaccess.h>
37 37
38/* 38/*
diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c
index 00fc5b1c7b66..d4022878b2f0 100644
--- a/drivers/i2c/i2c-mux.c
+++ b/drivers/i2c/i2c-mux.c
@@ -19,13 +19,13 @@
19 * warranty of any kind, whether express or implied. 19 * warranty of any kind, whether express or implied.
20 */ 20 */
21 21
22#include <linux/kernel.h> 22#include <linux/acpi.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/i2c.h> 23#include <linux/i2c.h>
26#include <linux/i2c-mux.h> 24#include <linux/i2c-mux.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/of.h> 27#include <linux/of.h>
28#include <linux/acpi.h> 28#include <linux/slab.h>
29 29
30/* multiplexer per channel data */ 30/* multiplexer per channel data */
31struct i2c_mux_priv { 31struct i2c_mux_priv {
diff --git a/drivers/i2c/i2c-smbus.c b/drivers/i2c/i2c-smbus.c
index 94765a81970d..abb55d3e76f3 100644
--- a/drivers/i2c/i2c-smbus.c
+++ b/drivers/i2c/i2c-smbus.c
@@ -15,14 +15,14 @@
15 * GNU General Public License for more details. 15 * GNU General Public License for more details.
16 */ 16 */
17 17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/device.h> 18#include <linux/device.h>
21#include <linux/interrupt.h>
22#include <linux/workqueue.h>
23#include <linux/i2c.h> 19#include <linux/i2c.h>
24#include <linux/i2c-smbus.h> 20#include <linux/i2c-smbus.h>
21#include <linux/interrupt.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
25#include <linux/slab.h> 24#include <linux/slab.h>
25#include <linux/workqueue.h>
26 26
27struct i2c_smbus_alert { 27struct i2c_smbus_alert {
28 unsigned int alert_edge_triggered:1; 28 unsigned int alert_edge_triggered:1;
diff --git a/drivers/i2c/i2c-stub.c b/drivers/i2c/i2c-stub.c
index af2a94e1140b..06af583d5101 100644
--- a/drivers/i2c/i2c-stub.c
+++ b/drivers/i2c/i2c-stub.c
@@ -17,13 +17,13 @@
17 17
18#define DEBUG 1 18#define DEBUG 1
19 19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/slab.h>
24#include <linux/errno.h> 20#include <linux/errno.h>
25#include <linux/i2c.h> 21#include <linux/i2c.h>
22#include <linux/init.h>
23#include <linux/kernel.h>
26#include <linux/list.h> 24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/slab.h>
27 27
28#define MAX_CHIPS 10 28#define MAX_CHIPS 10
29 29
diff --git a/drivers/i2c/muxes/Kconfig b/drivers/i2c/muxes/Kconfig
index f06b0e24673b..e280c8ecc0b5 100644
--- a/drivers/i2c/muxes/Kconfig
+++ b/drivers/i2c/muxes/Kconfig
@@ -72,4 +72,13 @@ config I2C_MUX_REG
72 This driver can also be built as a module. If so, the module 72 This driver can also be built as a module. If so, the module
73 will be called i2c-mux-reg. 73 will be called i2c-mux-reg.
74 74
75config I2C_DEMUX_PINCTRL
76 tristate "pinctrl-based I2C demultiplexer"
77 depends on PINCTRL && OF
78 select OF_DYNAMIC
79 help
80 If you say yes to this option, support will be included for an I2C
81 demultiplexer that uses the pinctrl subsystem. This is useful if you
82 want to change the I2C master at run-time depending on features.
83
75endmenu 84endmenu
diff --git a/drivers/i2c/muxes/Makefile b/drivers/i2c/muxes/Makefile
index e89799b76a92..7c267c29b191 100644
--- a/drivers/i2c/muxes/Makefile
+++ b/drivers/i2c/muxes/Makefile
@@ -3,6 +3,8 @@
3 3
4obj-$(CONFIG_I2C_ARB_GPIO_CHALLENGE) += i2c-arb-gpio-challenge.o 4obj-$(CONFIG_I2C_ARB_GPIO_CHALLENGE) += i2c-arb-gpio-challenge.o
5 5
6obj-$(CONFIG_I2C_DEMUX_PINCTRL) += i2c-demux-pinctrl.o
7
6obj-$(CONFIG_I2C_MUX_GPIO) += i2c-mux-gpio.o 8obj-$(CONFIG_I2C_MUX_GPIO) += i2c-mux-gpio.o
7obj-$(CONFIG_I2C_MUX_PCA9541) += i2c-mux-pca9541.o 9obj-$(CONFIG_I2C_MUX_PCA9541) += i2c-mux-pca9541.o
8obj-$(CONFIG_I2C_MUX_PCA954x) += i2c-mux-pca954x.o 10obj-$(CONFIG_I2C_MUX_PCA954x) += i2c-mux-pca954x.o
diff --git a/drivers/i2c/muxes/i2c-demux-pinctrl.c b/drivers/i2c/muxes/i2c-demux-pinctrl.c
new file mode 100644
index 000000000000..7748a0a5ddb9
--- /dev/null
+++ b/drivers/i2c/muxes/i2c-demux-pinctrl.c
@@ -0,0 +1,272 @@
1/*
2 * Pinctrl based I2C DeMultiplexer
3 *
4 * Copyright (C) 2015-16 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
5 * Copyright (C) 2015-16 by Renesas Electronics Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; version 2 of the License.
10 *
11 * See the bindings doc for DTS setup and the sysfs doc for usage information.
12 * (look for filenames containing 'i2c-demux-pinctrl' in Documentation/)
13 */
14
15#include <linux/i2c.h>
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/pinctrl/consumer.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <linux/sysfs.h>
23
24struct i2c_demux_pinctrl_chan {
25 struct device_node *parent_np;
26 struct i2c_adapter *parent_adap;
27 struct of_changeset chgset;
28};
29
30struct i2c_demux_pinctrl_priv {
31 int cur_chan;
32 int num_chan;
33 struct device *dev;
34 const char *bus_name;
35 struct i2c_adapter cur_adap;
36 struct i2c_algorithm algo;
37 struct i2c_demux_pinctrl_chan chan[];
38};
39
40static struct property status_okay = { .name = "status", .length = 3, .value = "ok" };
41
42static int i2c_demux_master_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
43{
44 struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
45 struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
46
47 return __i2c_transfer(parent, msgs, num);
48}
49
50static u32 i2c_demux_functionality(struct i2c_adapter *adap)
51{
52 struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
53 struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
54
55 return parent->algo->functionality(parent);
56}
57
58static int i2c_demux_activate_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
59{
60 struct i2c_adapter *adap;
61 struct pinctrl *p;
62 int ret;
63
64 ret = of_changeset_apply(&priv->chan[new_chan].chgset);
65 if (ret)
66 goto err;
67
68 adap = of_find_i2c_adapter_by_node(priv->chan[new_chan].parent_np);
69 if (!adap) {
70 ret = -ENODEV;
71 goto err;
72 }
73
74 p = devm_pinctrl_get_select(adap->dev.parent, priv->bus_name);
75 if (IS_ERR(p)) {
76 ret = PTR_ERR(p);
77 goto err_with_put;
78 }
79
80 priv->chan[new_chan].parent_adap = adap;
81 priv->cur_chan = new_chan;
82
83 /* Now fill out current adapter structure. cur_chan must be up to date */
84 priv->algo.master_xfer = i2c_demux_master_xfer;
85 priv->algo.functionality = i2c_demux_functionality;
86
87 snprintf(priv->cur_adap.name, sizeof(priv->cur_adap.name),
88 "i2c-demux (master i2c-%d)", i2c_adapter_id(adap));
89 priv->cur_adap.owner = THIS_MODULE;
90 priv->cur_adap.algo = &priv->algo;
91 priv->cur_adap.algo_data = priv;
92 priv->cur_adap.dev.parent = priv->dev;
93 priv->cur_adap.class = adap->class;
94 priv->cur_adap.retries = adap->retries;
95 priv->cur_adap.timeout = adap->timeout;
96 priv->cur_adap.quirks = adap->quirks;
97 priv->cur_adap.dev.of_node = priv->dev->of_node;
98 ret = i2c_add_adapter(&priv->cur_adap);
99 if (ret < 0)
100 goto err_with_put;
101
102 return 0;
103
104 err_with_put:
105 i2c_put_adapter(adap);
106 err:
107 dev_err(priv->dev, "failed to setup demux-adapter %d (%d)\n", new_chan, ret);
108 return ret;
109}
110
111static int i2c_demux_deactivate_master(struct i2c_demux_pinctrl_priv *priv)
112{
113 int ret, cur = priv->cur_chan;
114
115 if (cur < 0)
116 return 0;
117
118 i2c_del_adapter(&priv->cur_adap);
119 i2c_put_adapter(priv->chan[cur].parent_adap);
120
121 ret = of_changeset_revert(&priv->chan[cur].chgset);
122
123 priv->chan[cur].parent_adap = NULL;
124 priv->cur_chan = -EINVAL;
125
126 return ret;
127}
128
129static int i2c_demux_change_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
130{
131 int ret;
132
133 if (new_chan == priv->cur_chan)
134 return 0;
135
136 ret = i2c_demux_deactivate_master(priv);
137 if (ret)
138 return ret;
139
140 return i2c_demux_activate_master(priv, new_chan);
141}
142
143static ssize_t cur_master_show(struct device *dev, struct device_attribute *attr,
144 char *buf)
145{
146 struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
147 int count = 0, i;
148
149 for (i = 0; i < priv->num_chan && count < PAGE_SIZE; i++)
150 count += scnprintf(buf + count, PAGE_SIZE - count, "%c %d - %s\n",
151 i == priv->cur_chan ? '*' : ' ', i,
152 priv->chan[i].parent_np->full_name);
153
154 return count;
155}
156
157static ssize_t cur_master_store(struct device *dev, struct device_attribute *attr,
158 const char *buf, size_t count)
159{
160 struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
161 unsigned int val;
162 int ret;
163
164 ret = kstrtouint(buf, 0, &val);
165 if (ret < 0)
166 return ret;
167
168 if (val >= priv->num_chan)
169 return -EINVAL;
170
171 ret = i2c_demux_change_master(priv, val);
172
173 return ret < 0 ? ret : count;
174}
175static DEVICE_ATTR_RW(cur_master);
176
177static int i2c_demux_pinctrl_probe(struct platform_device *pdev)
178{
179 struct device_node *np = pdev->dev.of_node;
180 struct i2c_demux_pinctrl_priv *priv;
181 int num_chan, i, j, err;
182
183 num_chan = of_count_phandle_with_args(np, "i2c-parent", NULL);
184 if (num_chan < 2) {
185 dev_err(&pdev->dev, "Need at least two I2C masters to switch\n");
186 return -EINVAL;
187 }
188
189 priv = devm_kzalloc(&pdev->dev, sizeof(*priv)
190 + num_chan * sizeof(struct i2c_demux_pinctrl_chan), GFP_KERNEL);
191 if (!priv)
192 return -ENOMEM;
193
194 err = of_property_read_string(np, "i2c-bus-name", &priv->bus_name);
195 if (err)
196 return err;
197
198 for (i = 0; i < num_chan; i++) {
199 struct device_node *adap_np;
200
201 adap_np = of_parse_phandle(np, "i2c-parent", i);
202 if (!adap_np) {
203 dev_err(&pdev->dev, "can't get phandle for parent %d\n", i);
204 err = -ENOENT;
205 goto err_rollback;
206 }
207 priv->chan[i].parent_np = adap_np;
208
209 of_changeset_init(&priv->chan[i].chgset);
210 of_changeset_update_property(&priv->chan[i].chgset, adap_np, &status_okay);
211 }
212
213 priv->num_chan = num_chan;
214 priv->dev = &pdev->dev;
215
216 platform_set_drvdata(pdev, priv);
217
218 /* switch to first parent as active master */
219 i2c_demux_activate_master(priv, 0);
220
221 err = device_create_file(&pdev->dev, &dev_attr_cur_master);
222 if (err)
223 goto err_rollback;
224
225 return 0;
226
227err_rollback:
228 for (j = 0; j < i; j++) {
229 of_node_put(priv->chan[j].parent_np);
230 of_changeset_destroy(&priv->chan[j].chgset);
231 }
232
233 return err;
234}
235
236static int i2c_demux_pinctrl_remove(struct platform_device *pdev)
237{
238 struct i2c_demux_pinctrl_priv *priv = platform_get_drvdata(pdev);
239 int i;
240
241 device_remove_file(&pdev->dev, &dev_attr_cur_master);
242
243 i2c_demux_deactivate_master(priv);
244
245 for (i = 0; i < priv->num_chan; i++) {
246 of_node_put(priv->chan[i].parent_np);
247 of_changeset_destroy(&priv->chan[i].chgset);
248 }
249
250 return 0;
251}
252
253static const struct of_device_id i2c_demux_pinctrl_of_match[] = {
254 { .compatible = "i2c-demux-pinctrl", },
255 {},
256};
257MODULE_DEVICE_TABLE(of, i2c_demux_pinctrl_of_match);
258
259static struct platform_driver i2c_demux_pinctrl_driver = {
260 .driver = {
261 .name = "i2c-demux-pinctrl",
262 .of_match_table = i2c_demux_pinctrl_of_match,
263 },
264 .probe = i2c_demux_pinctrl_probe,
265 .remove = i2c_demux_pinctrl_remove,
266};
267module_platform_driver(i2c_demux_pinctrl_driver);
268
269MODULE_DESCRIPTION("pinctrl-based I2C demux driver");
270MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
271MODULE_LICENSE("GPL v2");
272MODULE_ALIAS("platform:i2c-demux-pinctrl");