aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
authorJiri Kosina <jkosina@suse.cz>2013-12-19 09:08:03 -0500
committerJiri Kosina <jkosina@suse.cz>2013-12-19 09:08:32 -0500
commite23c34bb41da65f354fb7eee04300c56ee48f60c (patch)
tree549fbe449d55273b81ef104a9755109bf4ae7817 /drivers/i2c
parentb481c2cb3534c85dca625973b33eba15f9af3e4c (diff)
parent319e2e3f63c348a9b66db4667efa73178e18b17d (diff)
Merge branch 'master' into for-next
Sync with Linus' tree to be able to apply fixes on top of newer things in tree (efi-stub). Signed-off-by: Jiri Kosina <jkosina@suse.cz>
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/Kconfig1
-rw-r--r--drivers/i2c/busses/Kconfig38
-rw-r--r--drivers/i2c/busses/Makefile3
-rw-r--r--drivers/i2c/busses/i2c-at91.c2
-rw-r--r--drivers/i2c/busses/i2c-bcm-kona.c908
-rw-r--r--drivers/i2c/busses/i2c-bcm2835.c3
-rw-r--r--drivers/i2c/busses/i2c-bfin-twi.c6
-rw-r--r--drivers/i2c/busses/i2c-cbus-gpio.c2
-rw-r--r--drivers/i2c/busses/i2c-cpm.c2
-rw-r--r--drivers/i2c/busses/i2c-davinci.c10
-rw-r--r--drivers/i2c/busses/i2c-designware-core.c28
-rw-r--r--drivers/i2c/busses/i2c-designware-platdrv.c7
-rw-r--r--drivers/i2c/busses/i2c-diolan-u2c.c16
-rw-r--r--drivers/i2c/busses/i2c-eg20t.c26
-rw-r--r--drivers/i2c/busses/i2c-exynos5.c769
-rw-r--r--drivers/i2c/busses/i2c-gpio.c1
-rw-r--r--drivers/i2c/busses/i2c-i801.c3
-rw-r--r--drivers/i2c/busses/i2c-ibm_iic.c2
-rw-r--r--drivers/i2c/busses/i2c-imx.c15
-rw-r--r--drivers/i2c/busses/i2c-ismt.c5
-rw-r--r--drivers/i2c/busses/i2c-mpc.c2
-rw-r--r--drivers/i2c/busses/i2c-mv64xxx.c18
-rw-r--r--drivers/i2c/busses/i2c-mxs.c345
-rw-r--r--drivers/i2c/busses/i2c-omap.c35
-rw-r--r--drivers/i2c/busses/i2c-pnx.c1
-rw-r--r--drivers/i2c/busses/i2c-powermac.c1
-rw-r--r--drivers/i2c/busses/i2c-rcar.c65
-rw-r--r--drivers/i2c/busses/i2c-s3c2410.c3
-rw-r--r--drivers/i2c/busses/i2c-scmi.c6
-rw-r--r--drivers/i2c/busses/i2c-sh_mobile.c8
-rw-r--r--drivers/i2c/busses/i2c-st.c872
-rw-r--r--drivers/i2c/busses/i2c-stu300.c11
-rw-r--r--drivers/i2c/busses/i2c-tegra.c2
-rw-r--r--drivers/i2c/busses/i2c-wmt.c5
-rw-r--r--drivers/i2c/busses/i2c-xiic.c3
-rw-r--r--drivers/i2c/i2c-core.c49
-rw-r--r--drivers/i2c/i2c-dev.c19
-rw-r--r--drivers/i2c/i2c-mux.c2
-rw-r--r--drivers/i2c/i2c-smbus.c10
-rw-r--r--drivers/i2c/muxes/i2c-arb-gpio-challenge.c4
-rw-r--r--drivers/i2c/muxes/i2c-mux-gpio.c24
-rw-r--r--drivers/i2c/muxes/i2c-mux-pinctrl.c5
42 files changed, 3071 insertions, 266 deletions
diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig
index e380c6eef3af..7b7ea320a258 100644
--- a/drivers/i2c/Kconfig
+++ b/drivers/i2c/Kconfig
@@ -75,7 +75,6 @@ config I2C_HELPER_AUTO
75 75
76config I2C_SMBUS 76config I2C_SMBUS
77 tristate "SMBus-specific protocols" if !I2C_HELPER_AUTO 77 tristate "SMBus-specific protocols" if !I2C_HELPER_AUTO
78 depends on GENERIC_HARDIRQS
79 help 78 help
80 Say Y here if you want support for SMBus extensions to the I2C 79 Say Y here if you want support for SMBus extensions to the I2C
81 specification. At the moment, the only supported extension is 80 specification. At the moment, the only supported extension is
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index fcdd321f709e..3b26129f6055 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -109,13 +109,14 @@ config I2C_I801
109 Avoton (SOC) 109 Avoton (SOC)
110 Wellsburg (PCH) 110 Wellsburg (PCH)
111 Coleto Creek (PCH) 111 Coleto Creek (PCH)
112 Wildcat Point-LP (PCH)
112 113
113 This driver can also be built as a module. If so, the module 114 This driver can also be built as a module. If so, the module
114 will be called i2c-i801. 115 will be called i2c-i801.
115 116
116config I2C_ISCH 117config I2C_ISCH
117 tristate "Intel SCH SMBus 1.0" 118 tristate "Intel SCH SMBus 1.0"
118 depends on PCI && GENERIC_HARDIRQS 119 depends on PCI
119 select LPC_SCH 120 select LPC_SCH
120 help 121 help
121 Say Y here if you want to use SMBus controller on the Intel SCH 122 Say Y here if you want to use SMBus controller on the Intel SCH
@@ -345,6 +346,16 @@ config I2C_BCM2835
345 This support is also available as a module. If so, the module 346 This support is also available as a module. If so, the module
346 will be called i2c-bcm2835. 347 will be called i2c-bcm2835.
347 348
349config I2C_BCM_KONA
350 tristate "BCM Kona I2C adapter"
351 depends on ARCH_BCM_MOBILE
352 default y
353 help
354 If you say yes to this option, support will be included for the
355 I2C interface on the Broadcom Kona family of processors.
356
357 If you do not need KONA I2C inteface, say N.
358
348config I2C_BLACKFIN_TWI 359config I2C_BLACKFIN_TWI
349 tristate "Blackfin TWI I2C support" 360 tristate "Blackfin TWI I2C support"
350 depends on BLACKFIN 361 depends on BLACKFIN
@@ -436,6 +447,13 @@ config I2C_EG20T
436 ML7213/ML7223/ML7831 is companion chip for Intel Atom E6xx series. 447 ML7213/ML7223/ML7831 is companion chip for Intel Atom E6xx series.
437 ML7213/ML7223/ML7831 is completely compatible for Intel EG20T PCH. 448 ML7213/ML7223/ML7831 is completely compatible for Intel EG20T PCH.
438 449
450config I2C_EXYNOS5
451 tristate "Exynos5 high-speed I2C driver"
452 depends on ARCH_EXYNOS5 && OF
453 help
454 Say Y here to include support for high-speed I2C controller in the
455 Exynos5 based Samsung SoCs.
456
439config I2C_GPIO 457config I2C_GPIO
440 tristate "GPIO-based bitbanging I2C" 458 tristate "GPIO-based bitbanging I2C"
441 depends on GPIOLIB 459 depends on GPIOLIB
@@ -546,7 +564,6 @@ config I2C_NUC900
546 564
547config I2C_OCORES 565config I2C_OCORES
548 tristate "OpenCores I2C Controller" 566 tristate "OpenCores I2C Controller"
549 depends on GENERIC_HARDIRQS
550 help 567 help
551 If you say yes to this option, support will be included for the 568 If you say yes to this option, support will be included for the
552 OpenCores I2C controller. For details see 569 OpenCores I2C controller. For details see
@@ -666,7 +683,7 @@ config I2C_SH7760
666 683
667config I2C_SH_MOBILE 684config I2C_SH_MOBILE
668 tristate "SuperH Mobile I2C Controller" 685 tristate "SuperH Mobile I2C Controller"
669 depends on SUPERH || ARCH_SHMOBILE 686 depends on SUPERH || ARM || COMPILE_TEST
670 help 687 help
671 If you say yes to this option, support will be included for the 688 If you say yes to this option, support will be included for the
672 built-in I2C interface on the Renesas SH-Mobile processor. 689 built-in I2C interface on the Renesas SH-Mobile processor.
@@ -696,6 +713,16 @@ config I2C_SIRF
696 This driver can also be built as a module. If so, the module 713 This driver can also be built as a module. If so, the module
697 will be called i2c-sirf. 714 will be called i2c-sirf.
698 715
716config I2C_ST
717 tristate "STMicroelectronics SSC I2C support"
718 depends on ARCH_STI
719 help
720 Enable this option to add support for STMicroelectronics SoCs
721 hardware SSC (Synchronous Serial Controller) as an I2C controller.
722
723 This driver can also be built as module. If so, the module
724 will be called i2c-st.
725
699config I2C_STU300 726config I2C_STU300
700 tristate "ST Microelectronics DDC I2C interface" 727 tristate "ST Microelectronics DDC I2C interface"
701 depends on MACH_U300 728 depends on MACH_U300
@@ -769,7 +796,7 @@ config I2C_XLR
769 796
770config I2C_RCAR 797config I2C_RCAR
771 tristate "Renesas R-Car I2C Controller" 798 tristate "Renesas R-Car I2C Controller"
772 depends on ARCH_SHMOBILE && I2C 799 depends on ARM || COMPILE_TEST
773 help 800 help
774 If you say yes to this option, support will be included for the 801 If you say yes to this option, support will be included for the
775 R-Car I2C controller. 802 R-Car I2C controller.
@@ -791,7 +818,7 @@ config I2C_DIOLAN_U2C
791 818
792config I2C_PARPORT 819config I2C_PARPORT
793 tristate "Parallel port adapter" 820 tristate "Parallel port adapter"
794 depends on PARPORT && GENERIC_HARDIRQS 821 depends on PARPORT
795 select I2C_ALGOBIT 822 select I2C_ALGOBIT
796 select I2C_SMBUS 823 select I2C_SMBUS
797 help 824 help
@@ -816,7 +843,6 @@ config I2C_PARPORT
816 843
817config I2C_PARPORT_LIGHT 844config I2C_PARPORT_LIGHT
818 tristate "Parallel port adapter (light)" 845 tristate "Parallel port adapter (light)"
819 depends on GENERIC_HARDIRQS
820 select I2C_ALGOBIT 846 select I2C_ALGOBIT
821 select I2C_SMBUS 847 select I2C_SMBUS
822 help 848 help
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index d00997f3eb3b..c73eb0ea788e 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -42,6 +42,7 @@ i2c-designware-platform-objs := i2c-designware-platdrv.o
42obj-$(CONFIG_I2C_DESIGNWARE_PCI) += i2c-designware-pci.o 42obj-$(CONFIG_I2C_DESIGNWARE_PCI) += i2c-designware-pci.o
43i2c-designware-pci-objs := i2c-designware-pcidrv.o 43i2c-designware-pci-objs := i2c-designware-pcidrv.o
44obj-$(CONFIG_I2C_EG20T) += i2c-eg20t.o 44obj-$(CONFIG_I2C_EG20T) += i2c-eg20t.o
45obj-$(CONFIG_I2C_EXYNOS5) += i2c-exynos5.o
45obj-$(CONFIG_I2C_GPIO) += i2c-gpio.o 46obj-$(CONFIG_I2C_GPIO) += i2c-gpio.o
46obj-$(CONFIG_I2C_HIGHLANDER) += i2c-highlander.o 47obj-$(CONFIG_I2C_HIGHLANDER) += i2c-highlander.o
47obj-$(CONFIG_I2C_IBM_IIC) += i2c-ibm_iic.o 48obj-$(CONFIG_I2C_IBM_IIC) += i2c-ibm_iic.o
@@ -68,6 +69,7 @@ obj-$(CONFIG_I2C_SH7760) += i2c-sh7760.o
68obj-$(CONFIG_I2C_SH_MOBILE) += i2c-sh_mobile.o 69obj-$(CONFIG_I2C_SH_MOBILE) += i2c-sh_mobile.o
69obj-$(CONFIG_I2C_SIMTEC) += i2c-simtec.o 70obj-$(CONFIG_I2C_SIMTEC) += i2c-simtec.o
70obj-$(CONFIG_I2C_SIRF) += i2c-sirf.o 71obj-$(CONFIG_I2C_SIRF) += i2c-sirf.o
72obj-$(CONFIG_I2C_ST) += i2c-st.o
71obj-$(CONFIG_I2C_STU300) += i2c-stu300.o 73obj-$(CONFIG_I2C_STU300) += i2c-stu300.o
72obj-$(CONFIG_I2C_TEGRA) += i2c-tegra.o 74obj-$(CONFIG_I2C_TEGRA) += i2c-tegra.o
73obj-$(CONFIG_I2C_VERSATILE) += i2c-versatile.o 75obj-$(CONFIG_I2C_VERSATILE) += i2c-versatile.o
@@ -87,6 +89,7 @@ obj-$(CONFIG_I2C_VIPERBOARD) += i2c-viperboard.o
87 89
88# Other I2C/SMBus bus drivers 90# Other I2C/SMBus bus drivers
89obj-$(CONFIG_I2C_ACORN) += i2c-acorn.o 91obj-$(CONFIG_I2C_ACORN) += i2c-acorn.o
92obj-$(CONFIG_I2C_BCM_KONA) += i2c-bcm-kona.o
90obj-$(CONFIG_I2C_ELEKTOR) += i2c-elektor.o 93obj-$(CONFIG_I2C_ELEKTOR) += i2c-elektor.o
91obj-$(CONFIG_I2C_PCA_ISA) += i2c-pca-isa.o 94obj-$(CONFIG_I2C_PCA_ISA) += i2c-pca-isa.o
92obj-$(CONFIG_I2C_SIBYTE) += i2c-sibyte.o 95obj-$(CONFIG_I2C_SIBYTE) += i2c-sibyte.o
diff --git a/drivers/i2c/busses/i2c-at91.c b/drivers/i2c/busses/i2c-at91.c
index fd059308affa..8edba9de76df 100644
--- a/drivers/i2c/busses/i2c-at91.c
+++ b/drivers/i2c/busses/i2c-at91.c
@@ -371,7 +371,7 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
371 dev_dbg(dev->dev, "transfer: %s %d bytes.\n", 371 dev_dbg(dev->dev, "transfer: %s %d bytes.\n",
372 (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len); 372 (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
373 373
374 INIT_COMPLETION(dev->cmd_complete); 374 reinit_completion(&dev->cmd_complete);
375 dev->transfer_status = 0; 375 dev->transfer_status = 0;
376 376
377 if (!dev->buf_len) { 377 if (!dev->buf_len) {
diff --git a/drivers/i2c/busses/i2c-bcm-kona.c b/drivers/i2c/busses/i2c-bcm-kona.c
new file mode 100644
index 000000000000..18a74a6751a9
--- /dev/null
+++ b/drivers/i2c/busses/i2c-bcm-kona.c
@@ -0,0 +1,908 @@
1/*
2 * Copyright (C) 2013 Broadcom Corporation
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
7 *
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/device.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/sched.h>
18#include <linux/i2c.h>
19#include <linux/interrupt.h>
20#include <linux/platform_device.h>
21#include <linux/clk.h>
22#include <linux/io.h>
23#include <linux/slab.h>
24
25/* Hardware register offsets and field defintions */
26#define CS_OFFSET 0x00000020
27#define CS_ACK_SHIFT 3
28#define CS_ACK_MASK 0x00000008
29#define CS_ACK_CMD_GEN_START 0x00000000
30#define CS_ACK_CMD_GEN_RESTART 0x00000001
31#define CS_CMD_SHIFT 1
32#define CS_CMD_CMD_NO_ACTION 0x00000000
33#define CS_CMD_CMD_START_RESTART 0x00000001
34#define CS_CMD_CMD_STOP 0x00000002
35#define CS_EN_SHIFT 0
36#define CS_EN_CMD_ENABLE_BSC 0x00000001
37
38#define TIM_OFFSET 0x00000024
39#define TIM_PRESCALE_SHIFT 6
40#define TIM_P_SHIFT 3
41#define TIM_NO_DIV_SHIFT 2
42#define TIM_DIV_SHIFT 0
43
44#define DAT_OFFSET 0x00000028
45
46#define TOUT_OFFSET 0x0000002c
47
48#define TXFCR_OFFSET 0x0000003c
49#define TXFCR_FIFO_FLUSH_MASK 0x00000080
50#define TXFCR_FIFO_EN_MASK 0x00000040
51
52#define IER_OFFSET 0x00000044
53#define IER_READ_COMPLETE_INT_MASK 0x00000010
54#define IER_I2C_INT_EN_MASK 0x00000008
55#define IER_FIFO_INT_EN_MASK 0x00000002
56#define IER_NOACK_EN_MASK 0x00000001
57
58#define ISR_OFFSET 0x00000048
59#define ISR_RESERVED_MASK 0xffffff60
60#define ISR_CMDBUSY_MASK 0x00000080
61#define ISR_READ_COMPLETE_MASK 0x00000010
62#define ISR_SES_DONE_MASK 0x00000008
63#define ISR_ERR_MASK 0x00000004
64#define ISR_TXFIFOEMPTY_MASK 0x00000002
65#define ISR_NOACK_MASK 0x00000001
66
67#define CLKEN_OFFSET 0x0000004C
68#define CLKEN_AUTOSENSE_OFF_MASK 0x00000080
69#define CLKEN_M_SHIFT 4
70#define CLKEN_N_SHIFT 1
71#define CLKEN_CLKEN_MASK 0x00000001
72
73#define FIFO_STATUS_OFFSET 0x00000054
74#define FIFO_STATUS_RXFIFO_EMPTY_MASK 0x00000004
75#define FIFO_STATUS_TXFIFO_EMPTY_MASK 0x00000010
76
77#define HSTIM_OFFSET 0x00000058
78#define HSTIM_HS_MODE_MASK 0x00008000
79#define HSTIM_HS_HOLD_SHIFT 10
80#define HSTIM_HS_HIGH_PHASE_SHIFT 5
81#define HSTIM_HS_SETUP_SHIFT 0
82
83#define PADCTL_OFFSET 0x0000005c
84#define PADCTL_PAD_OUT_EN_MASK 0x00000004
85
86#define RXFCR_OFFSET 0x00000068
87#define RXFCR_NACK_EN_SHIFT 7
88#define RXFCR_READ_COUNT_SHIFT 0
89#define RXFIFORDOUT_OFFSET 0x0000006c
90
91/* Locally used constants */
92#define MAX_RX_FIFO_SIZE 64U /* bytes */
93#define MAX_TX_FIFO_SIZE 64U /* bytes */
94
95#define STD_EXT_CLK_FREQ 13000000UL
96#define HS_EXT_CLK_FREQ 104000000UL
97
98#define MASTERCODE 0x08 /* Mastercodes are 0000_1xxxb */
99
100#define I2C_TIMEOUT 100 /* msecs */
101
102/* Operations that can be commanded to the controller */
103enum bcm_kona_cmd_t {
104 BCM_CMD_NOACTION = 0,
105 BCM_CMD_START,
106 BCM_CMD_RESTART,
107 BCM_CMD_STOP,
108};
109
110enum bus_speed_index {
111 BCM_SPD_100K = 0,
112 BCM_SPD_400K,
113 BCM_SPD_1MHZ,
114};
115
116enum hs_bus_speed_index {
117 BCM_SPD_3P4MHZ = 0,
118};
119
120/* Internal divider settings for standard mode, fast mode and fast mode plus */
121struct bus_speed_cfg {
122 uint8_t time_m; /* Number of cycles for setup time */
123 uint8_t time_n; /* Number of cycles for hold time */
124 uint8_t prescale; /* Prescale divider */
125 uint8_t time_p; /* Timing coefficient */
126 uint8_t no_div; /* Disable clock divider */
127 uint8_t time_div; /* Post-prescale divider */
128};
129
130/* Internal divider settings for high-speed mode */
131struct hs_bus_speed_cfg {
132 uint8_t hs_hold; /* Number of clock cycles SCL stays low until
133 the end of bit period */
134 uint8_t hs_high_phase; /* Number of clock cycles SCL stays high
135 before it falls */
136 uint8_t hs_setup; /* Number of clock cycles SCL stays low
137 before it rises */
138 uint8_t prescale; /* Prescale divider */
139 uint8_t time_p; /* Timing coefficient */
140 uint8_t no_div; /* Disable clock divider */
141 uint8_t time_div; /* Post-prescale divider */
142};
143
144static const struct bus_speed_cfg std_cfg_table[] = {
145 [BCM_SPD_100K] = {0x01, 0x01, 0x03, 0x06, 0x00, 0x02},
146 [BCM_SPD_400K] = {0x05, 0x01, 0x03, 0x05, 0x01, 0x02},
147 [BCM_SPD_1MHZ] = {0x01, 0x01, 0x03, 0x01, 0x01, 0x03},
148};
149
150static const struct hs_bus_speed_cfg hs_cfg_table[] = {
151 [BCM_SPD_3P4MHZ] = {0x01, 0x08, 0x14, 0x00, 0x06, 0x01, 0x00},
152};
153
154struct bcm_kona_i2c_dev {
155 struct device *device;
156
157 void __iomem *base;
158 int irq;
159 struct clk *external_clk;
160
161 struct i2c_adapter adapter;
162
163 struct completion done;
164
165 const struct bus_speed_cfg *std_cfg;
166 const struct hs_bus_speed_cfg *hs_cfg;
167};
168
169static void bcm_kona_i2c_send_cmd_to_ctrl(struct bcm_kona_i2c_dev *dev,
170 enum bcm_kona_cmd_t cmd)
171{
172 dev_dbg(dev->device, "%s, %d\n", __func__, cmd);
173
174 switch (cmd) {
175 case BCM_CMD_NOACTION:
176 writel((CS_CMD_CMD_NO_ACTION << CS_CMD_SHIFT) |
177 (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
178 dev->base + CS_OFFSET);
179 break;
180
181 case BCM_CMD_START:
182 writel((CS_ACK_CMD_GEN_START << CS_ACK_SHIFT) |
183 (CS_CMD_CMD_START_RESTART << CS_CMD_SHIFT) |
184 (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
185 dev->base + CS_OFFSET);
186 break;
187
188 case BCM_CMD_RESTART:
189 writel((CS_ACK_CMD_GEN_RESTART << CS_ACK_SHIFT) |
190 (CS_CMD_CMD_START_RESTART << CS_CMD_SHIFT) |
191 (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
192 dev->base + CS_OFFSET);
193 break;
194
195 case BCM_CMD_STOP:
196 writel((CS_CMD_CMD_STOP << CS_CMD_SHIFT) |
197 (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
198 dev->base + CS_OFFSET);
199 break;
200
201 default:
202 dev_err(dev->device, "Unknown command %d\n", cmd);
203 }
204}
205
206static void bcm_kona_i2c_enable_clock(struct bcm_kona_i2c_dev *dev)
207{
208 writel(readl(dev->base + CLKEN_OFFSET) | CLKEN_CLKEN_MASK,
209 dev->base + CLKEN_OFFSET);
210}
211
212static void bcm_kona_i2c_disable_clock(struct bcm_kona_i2c_dev *dev)
213{
214 writel(readl(dev->base + CLKEN_OFFSET) & ~CLKEN_CLKEN_MASK,
215 dev->base + CLKEN_OFFSET);
216}
217
218static irqreturn_t bcm_kona_i2c_isr(int irq, void *devid)
219{
220 struct bcm_kona_i2c_dev *dev = devid;
221 uint32_t status = readl(dev->base + ISR_OFFSET);
222
223 if ((status & ~ISR_RESERVED_MASK) == 0)
224 return IRQ_NONE;
225
226 /* Must flush the TX FIFO when NAK detected */
227 if (status & ISR_NOACK_MASK)
228 writel(TXFCR_FIFO_FLUSH_MASK | TXFCR_FIFO_EN_MASK,
229 dev->base + TXFCR_OFFSET);
230
231 writel(status & ~ISR_RESERVED_MASK, dev->base + ISR_OFFSET);
232 complete_all(&dev->done);
233
234 return IRQ_HANDLED;
235}
236
237/* Wait for ISR_CMDBUSY_MASK to go low before writing to CS, DAT, or RCD */
238static int bcm_kona_i2c_wait_if_busy(struct bcm_kona_i2c_dev *dev)
239{
240 unsigned long timeout = jiffies + msecs_to_jiffies(I2C_TIMEOUT);
241
242 while (readl(dev->base + ISR_OFFSET) & ISR_CMDBUSY_MASK)
243 if (time_after(jiffies, timeout)) {
244 dev_err(dev->device, "CMDBUSY timeout\n");
245 return -ETIMEDOUT;
246 }
247
248 return 0;
249}
250
251/* Send command to I2C bus */
252static int bcm_kona_send_i2c_cmd(struct bcm_kona_i2c_dev *dev,
253 enum bcm_kona_cmd_t cmd)
254{
255 int rc;
256 unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT);
257
258 /* Make sure the hardware is ready */
259 rc = bcm_kona_i2c_wait_if_busy(dev);
260 if (rc < 0)
261 return rc;
262
263 /* Unmask the session done interrupt */
264 writel(IER_I2C_INT_EN_MASK, dev->base + IER_OFFSET);
265
266 /* Mark as incomplete before sending the command */
267 reinit_completion(&dev->done);
268
269 /* Send the command */
270 bcm_kona_i2c_send_cmd_to_ctrl(dev, cmd);
271
272 /* Wait for transaction to finish or timeout */
273 time_left = wait_for_completion_timeout(&dev->done, time_left);
274
275 /* Mask all interrupts */
276 writel(0, dev->base + IER_OFFSET);
277
278 if (!time_left) {
279 dev_err(dev->device, "controller timed out\n");
280 rc = -ETIMEDOUT;
281 }
282
283 /* Clear command */
284 bcm_kona_i2c_send_cmd_to_ctrl(dev, BCM_CMD_NOACTION);
285
286 return rc;
287}
288
289/* Read a single RX FIFO worth of data from the i2c bus */
290static int bcm_kona_i2c_read_fifo_single(struct bcm_kona_i2c_dev *dev,
291 uint8_t *buf, unsigned int len,
292 unsigned int last_byte_nak)
293{
294 unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT);
295
296 /* Mark as incomplete before starting the RX FIFO */
297 reinit_completion(&dev->done);
298
299 /* Unmask the read complete interrupt */
300 writel(IER_READ_COMPLETE_INT_MASK, dev->base + IER_OFFSET);
301
302 /* Start the RX FIFO */
303 writel((last_byte_nak << RXFCR_NACK_EN_SHIFT) |
304 (len << RXFCR_READ_COUNT_SHIFT),
305 dev->base + RXFCR_OFFSET);
306
307 /* Wait for FIFO read to complete */
308 time_left = wait_for_completion_timeout(&dev->done, time_left);
309
310 /* Mask all interrupts */
311 writel(0, dev->base + IER_OFFSET);
312
313 if (!time_left) {
314 dev_err(dev->device, "RX FIFO time out\n");
315 return -EREMOTEIO;
316 }
317
318 /* Read data from FIFO */
319 for (; len > 0; len--, buf++)
320 *buf = readl(dev->base + RXFIFORDOUT_OFFSET);
321
322 return 0;
323}
324
325/* Read any amount of data using the RX FIFO from the i2c bus */
326static int bcm_kona_i2c_read_fifo(struct bcm_kona_i2c_dev *dev,
327 struct i2c_msg *msg)
328{
329 unsigned int bytes_to_read = MAX_RX_FIFO_SIZE;
330 unsigned int last_byte_nak = 0;
331 unsigned int bytes_read = 0;
332 int rc;
333
334 uint8_t *tmp_buf = msg->buf;
335
336 while (bytes_read < msg->len) {
337 if (msg->len - bytes_read <= MAX_RX_FIFO_SIZE) {
338 last_byte_nak = 1; /* NAK last byte of transfer */
339 bytes_to_read = msg->len - bytes_read;
340 }
341
342 rc = bcm_kona_i2c_read_fifo_single(dev, tmp_buf, bytes_to_read,
343 last_byte_nak);
344 if (rc < 0)
345 return -EREMOTEIO;
346
347 bytes_read += bytes_to_read;
348 tmp_buf += bytes_to_read;
349 }
350
351 return 0;
352}
353
354/* Write a single byte of data to the i2c bus */
355static int bcm_kona_i2c_write_byte(struct bcm_kona_i2c_dev *dev, uint8_t data,
356 unsigned int nak_expected)
357{
358 int rc;
359 unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT);
360 unsigned int nak_received;
361
362 /* Make sure the hardware is ready */
363 rc = bcm_kona_i2c_wait_if_busy(dev);
364 if (rc < 0)
365 return rc;
366
367 /* Clear pending session done interrupt */
368 writel(ISR_SES_DONE_MASK, dev->base + ISR_OFFSET);
369
370 /* Unmask the session done interrupt */
371 writel(IER_I2C_INT_EN_MASK, dev->base + IER_OFFSET);
372
373 /* Mark as incomplete before sending the data */
374 reinit_completion(&dev->done);
375
376 /* Send one byte of data */
377 writel(data, dev->base + DAT_OFFSET);
378
379 /* Wait for byte to be written */
380 time_left = wait_for_completion_timeout(&dev->done, time_left);
381
382 /* Mask all interrupts */
383 writel(0, dev->base + IER_OFFSET);
384
385 if (!time_left) {
386 dev_dbg(dev->device, "controller timed out\n");
387 return -ETIMEDOUT;
388 }
389
390 nak_received = readl(dev->base + CS_OFFSET) & CS_ACK_MASK ? 1 : 0;
391
392 if (nak_received ^ nak_expected) {
393 dev_dbg(dev->device, "unexpected NAK/ACK\n");
394 return -EREMOTEIO;
395 }
396
397 return 0;
398}
399
400/* Write a single TX FIFO worth of data to the i2c bus */
401static int bcm_kona_i2c_write_fifo_single(struct bcm_kona_i2c_dev *dev,
402 uint8_t *buf, unsigned int len)
403{
404 int k;
405 unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT);
406 unsigned int fifo_status;
407
408 /* Mark as incomplete before sending data to the TX FIFO */
409 reinit_completion(&dev->done);
410
411 /* Unmask the fifo empty and nak interrupt */
412 writel(IER_FIFO_INT_EN_MASK | IER_NOACK_EN_MASK,
413 dev->base + IER_OFFSET);
414
415 /* Disable IRQ to load a FIFO worth of data without interruption */
416 disable_irq(dev->irq);
417
418 /* Write data into FIFO */
419 for (k = 0; k < len; k++)
420 writel(buf[k], (dev->base + DAT_OFFSET));
421
422 /* Enable IRQ now that data has been loaded */
423 enable_irq(dev->irq);
424
425 /* Wait for FIFO to empty */
426 do {
427 time_left = wait_for_completion_timeout(&dev->done, time_left);
428 fifo_status = readl(dev->base + FIFO_STATUS_OFFSET);
429 } while (time_left && !(fifo_status & FIFO_STATUS_TXFIFO_EMPTY_MASK));
430
431 /* Mask all interrupts */
432 writel(0, dev->base + IER_OFFSET);
433
434 /* Check if there was a NAK */
435 if (readl(dev->base + CS_OFFSET) & CS_ACK_MASK) {
436 dev_err(dev->device, "unexpected NAK\n");
437 return -EREMOTEIO;
438 }
439
440 /* Check if a timeout occured */
441 if (!time_left) {
442 dev_err(dev->device, "completion timed out\n");
443 return -EREMOTEIO;
444 }
445
446 return 0;
447}
448
449
450/* Write any amount of data using TX FIFO to the i2c bus */
451static int bcm_kona_i2c_write_fifo(struct bcm_kona_i2c_dev *dev,
452 struct i2c_msg *msg)
453{
454 unsigned int bytes_to_write = MAX_TX_FIFO_SIZE;
455 unsigned int bytes_written = 0;
456 int rc;
457
458 uint8_t *tmp_buf = msg->buf;
459
460 while (bytes_written < msg->len) {
461 if (msg->len - bytes_written <= MAX_TX_FIFO_SIZE)
462 bytes_to_write = msg->len - bytes_written;
463
464 rc = bcm_kona_i2c_write_fifo_single(dev, tmp_buf,
465 bytes_to_write);
466 if (rc < 0)
467 return -EREMOTEIO;
468
469 bytes_written += bytes_to_write;
470 tmp_buf += bytes_to_write;
471 }
472
473 return 0;
474}
475
476/* Send i2c address */
477static int bcm_kona_i2c_do_addr(struct bcm_kona_i2c_dev *dev,
478 struct i2c_msg *msg)
479{
480 unsigned char addr;
481
482 if (msg->flags & I2C_M_TEN) {
483 /* First byte is 11110XX0 where XX is upper 2 bits */
484 addr = 0xF0 | ((msg->addr & 0x300) >> 7);
485 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
486 return -EREMOTEIO;
487
488 /* Second byte is the remaining 8 bits */
489 addr = msg->addr & 0xFF;
490 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
491 return -EREMOTEIO;
492
493 if (msg->flags & I2C_M_RD) {
494 /* For read, send restart command */
495 if (bcm_kona_send_i2c_cmd(dev, BCM_CMD_RESTART) < 0)
496 return -EREMOTEIO;
497
498 /* Then re-send the first byte with the read bit set */
499 addr = 0xF0 | ((msg->addr & 0x300) >> 7) | 0x01;
500 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
501 return -EREMOTEIO;
502 }
503 } else {
504 addr = msg->addr << 1;
505
506 if (msg->flags & I2C_M_RD)
507 addr |= 1;
508
509 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
510 return -EREMOTEIO;
511 }
512
513 return 0;
514}
515
516static void bcm_kona_i2c_enable_autosense(struct bcm_kona_i2c_dev *dev)
517{
518 writel(readl(dev->base + CLKEN_OFFSET) & ~CLKEN_AUTOSENSE_OFF_MASK,
519 dev->base + CLKEN_OFFSET);
520}
521
522static void bcm_kona_i2c_config_timing(struct bcm_kona_i2c_dev *dev)
523{
524 writel(readl(dev->base + HSTIM_OFFSET) & ~HSTIM_HS_MODE_MASK,
525 dev->base + HSTIM_OFFSET);
526
527 writel((dev->std_cfg->prescale << TIM_PRESCALE_SHIFT) |
528 (dev->std_cfg->time_p << TIM_P_SHIFT) |
529 (dev->std_cfg->no_div << TIM_NO_DIV_SHIFT) |
530 (dev->std_cfg->time_div << TIM_DIV_SHIFT),
531 dev->base + TIM_OFFSET);
532
533 writel((dev->std_cfg->time_m << CLKEN_M_SHIFT) |
534 (dev->std_cfg->time_n << CLKEN_N_SHIFT) |
535 CLKEN_CLKEN_MASK,
536 dev->base + CLKEN_OFFSET);
537}
538
539static void bcm_kona_i2c_config_timing_hs(struct bcm_kona_i2c_dev *dev)
540{
541 writel((dev->hs_cfg->prescale << TIM_PRESCALE_SHIFT) |
542 (dev->hs_cfg->time_p << TIM_P_SHIFT) |
543 (dev->hs_cfg->no_div << TIM_NO_DIV_SHIFT) |
544 (dev->hs_cfg->time_div << TIM_DIV_SHIFT),
545 dev->base + TIM_OFFSET);
546
547 writel((dev->hs_cfg->hs_hold << HSTIM_HS_HOLD_SHIFT) |
548 (dev->hs_cfg->hs_high_phase << HSTIM_HS_HIGH_PHASE_SHIFT) |
549 (dev->hs_cfg->hs_setup << HSTIM_HS_SETUP_SHIFT),
550 dev->base + HSTIM_OFFSET);
551
552 writel(readl(dev->base + HSTIM_OFFSET) | HSTIM_HS_MODE_MASK,
553 dev->base + HSTIM_OFFSET);
554}
555
556static int bcm_kona_i2c_switch_to_hs(struct bcm_kona_i2c_dev *dev)
557{
558 int rc;
559
560 /* Send mastercode at standard speed */
561 rc = bcm_kona_i2c_write_byte(dev, MASTERCODE, 1);
562 if (rc < 0) {
563 pr_err("High speed handshake failed\n");
564 return rc;
565 }
566
567 /* Configure external clock to higher frequency */
568 rc = clk_set_rate(dev->external_clk, HS_EXT_CLK_FREQ);
569 if (rc) {
570 dev_err(dev->device, "%s: clk_set_rate returned %d\n",
571 __func__, rc);
572 return rc;
573 }
574
575 /* Reconfigure internal dividers */
576 bcm_kona_i2c_config_timing_hs(dev);
577
578 /* Send a restart command */
579 rc = bcm_kona_send_i2c_cmd(dev, BCM_CMD_RESTART);
580 if (rc < 0)
581 dev_err(dev->device, "High speed restart command failed\n");
582
583 return rc;
584}
585
586static int bcm_kona_i2c_switch_to_std(struct bcm_kona_i2c_dev *dev)
587{
588 int rc;
589
590 /* Reconfigure internal dividers */
591 bcm_kona_i2c_config_timing(dev);
592
593 /* Configure external clock to lower frequency */
594 rc = clk_set_rate(dev->external_clk, STD_EXT_CLK_FREQ);
595 if (rc) {
596 dev_err(dev->device, "%s: clk_set_rate returned %d\n",
597 __func__, rc);
598 }
599
600 return rc;
601}
602
603/* Master transfer function */
604static int bcm_kona_i2c_xfer(struct i2c_adapter *adapter,
605 struct i2c_msg msgs[], int num)
606{
607 struct bcm_kona_i2c_dev *dev = i2c_get_adapdata(adapter);
608 struct i2c_msg *pmsg;
609 int rc = 0;
610 int i;
611
612 rc = clk_prepare_enable(dev->external_clk);
613 if (rc) {
614 dev_err(dev->device, "%s: peri clock enable failed. err %d\n",
615 __func__, rc);
616 return rc;
617 }
618
619 /* Enable pad output */
620 writel(0, dev->base + PADCTL_OFFSET);
621
622 /* Enable internal clocks */
623 bcm_kona_i2c_enable_clock(dev);
624
625 /* Send start command */
626 rc = bcm_kona_send_i2c_cmd(dev, BCM_CMD_START);
627 if (rc < 0) {
628 dev_err(dev->device, "Start command failed rc = %d\n", rc);
629 goto xfer_disable_pad;
630 }
631
632 /* Switch to high speed if applicable */
633 if (dev->hs_cfg) {
634 rc = bcm_kona_i2c_switch_to_hs(dev);
635 if (rc < 0)
636 goto xfer_send_stop;
637 }
638
639 /* Loop through all messages */
640 for (i = 0; i < num; i++) {
641 pmsg = &msgs[i];
642
643 /* Send restart for subsequent messages */
644 if ((i != 0) && ((pmsg->flags & I2C_M_NOSTART) == 0)) {
645 rc = bcm_kona_send_i2c_cmd(dev, BCM_CMD_RESTART);
646 if (rc < 0) {
647 dev_err(dev->device,
648 "restart cmd failed rc = %d\n", rc);
649 goto xfer_send_stop;
650 }
651 }
652
653 /* Send slave address */
654 if (!(pmsg->flags & I2C_M_NOSTART)) {
655 rc = bcm_kona_i2c_do_addr(dev, pmsg);
656 if (rc < 0) {
657 dev_err(dev->device,
658 "NAK from addr %2.2x msg#%d rc = %d\n",
659 pmsg->addr, i, rc);
660 goto xfer_send_stop;
661 }
662 }
663
664 /* Perform data transfer */
665 if (pmsg->flags & I2C_M_RD) {
666 rc = bcm_kona_i2c_read_fifo(dev, pmsg);
667 if (rc < 0) {
668 dev_err(dev->device, "read failure\n");
669 goto xfer_send_stop;
670 }
671 } else {
672 rc = bcm_kona_i2c_write_fifo(dev, pmsg);
673 if (rc < 0) {
674 dev_err(dev->device, "write failure");
675 goto xfer_send_stop;
676 }
677 }
678 }
679
680 rc = num;
681
682xfer_send_stop:
683 /* Send a STOP command */
684 bcm_kona_send_i2c_cmd(dev, BCM_CMD_STOP);
685
686 /* Return from high speed if applicable */
687 if (dev->hs_cfg) {
688 int hs_rc = bcm_kona_i2c_switch_to_std(dev);
689
690 if (hs_rc)
691 rc = hs_rc;
692 }
693
694xfer_disable_pad:
695 /* Disable pad output */
696 writel(PADCTL_PAD_OUT_EN_MASK, dev->base + PADCTL_OFFSET);
697
698 /* Stop internal clock */
699 bcm_kona_i2c_disable_clock(dev);
700
701 clk_disable_unprepare(dev->external_clk);
702
703 return rc;
704}
705
706static uint32_t bcm_kona_i2c_functionality(struct i2c_adapter *adap)
707{
708 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
709 I2C_FUNC_NOSTART;
710}
711
712static const struct i2c_algorithm bcm_algo = {
713 .master_xfer = bcm_kona_i2c_xfer,
714 .functionality = bcm_kona_i2c_functionality,
715};
716
717static int bcm_kona_i2c_assign_bus_speed(struct bcm_kona_i2c_dev *dev)
718{
719 unsigned int bus_speed;
720 int ret = of_property_read_u32(dev->device->of_node, "clock-frequency",
721 &bus_speed);
722 if (ret < 0) {
723 dev_err(dev->device, "missing clock-frequency property\n");
724 return -ENODEV;
725 }
726
727 switch (bus_speed) {
728 case 100000:
729 dev->std_cfg = &std_cfg_table[BCM_SPD_100K];
730 break;
731 case 400000:
732 dev->std_cfg = &std_cfg_table[BCM_SPD_400K];
733 break;
734 case 1000000:
735 dev->std_cfg = &std_cfg_table[BCM_SPD_1MHZ];
736 break;
737 case 3400000:
738 /* Send mastercode at 100k */
739 dev->std_cfg = &std_cfg_table[BCM_SPD_100K];
740 dev->hs_cfg = &hs_cfg_table[BCM_SPD_3P4MHZ];
741 break;
742 default:
743 pr_err("%d hz bus speed not supported\n", bus_speed);
744 pr_err("Valid speeds are 100khz, 400khz, 1mhz, and 3.4mhz\n");
745 return -EINVAL;
746 }
747
748 return 0;
749}
750
751static int bcm_kona_i2c_probe(struct platform_device *pdev)
752{
753 int rc = 0;
754 struct bcm_kona_i2c_dev *dev;
755 struct i2c_adapter *adap;
756 struct resource *iomem;
757
758 /* Allocate memory for private data structure */
759 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
760 if (!dev)
761 return -ENOMEM;
762
763 platform_set_drvdata(pdev, dev);
764 dev->device = &pdev->dev;
765 init_completion(&dev->done);
766
767 /* Map hardware registers */
768 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
769 dev->base = devm_ioremap_resource(dev->device, iomem);
770 if (IS_ERR(dev->base))
771 return -ENOMEM;
772
773 /* Get and enable external clock */
774 dev->external_clk = devm_clk_get(dev->device, NULL);
775 if (IS_ERR(dev->external_clk)) {
776 dev_err(dev->device, "couldn't get clock\n");
777 return -ENODEV;
778 }
779
780 rc = clk_set_rate(dev->external_clk, STD_EXT_CLK_FREQ);
781 if (rc) {
782 dev_err(dev->device, "%s: clk_set_rate returned %d\n",
783 __func__, rc);
784 return rc;
785 }
786
787 rc = clk_prepare_enable(dev->external_clk);
788 if (rc) {
789 dev_err(dev->device, "couldn't enable clock\n");
790 return rc;
791 }
792
793 /* Parse bus speed */
794 rc = bcm_kona_i2c_assign_bus_speed(dev);
795 if (rc)
796 goto probe_disable_clk;
797
798 /* Enable internal clocks */
799 bcm_kona_i2c_enable_clock(dev);
800
801 /* Configure internal dividers */
802 bcm_kona_i2c_config_timing(dev);
803
804 /* Disable timeout */
805 writel(0, dev->base + TOUT_OFFSET);
806
807 /* Enable autosense */
808 bcm_kona_i2c_enable_autosense(dev);
809
810 /* Enable TX FIFO */
811 writel(TXFCR_FIFO_FLUSH_MASK | TXFCR_FIFO_EN_MASK,
812 dev->base + TXFCR_OFFSET);
813
814 /* Mask all interrupts */
815 writel(0, dev->base + IER_OFFSET);
816
817 /* Clear all pending interrupts */
818 writel(ISR_CMDBUSY_MASK |
819 ISR_READ_COMPLETE_MASK |
820 ISR_SES_DONE_MASK |
821 ISR_ERR_MASK |
822 ISR_TXFIFOEMPTY_MASK |
823 ISR_NOACK_MASK,
824 dev->base + ISR_OFFSET);
825
826 /* Get the interrupt number */
827 dev->irq = platform_get_irq(pdev, 0);
828 if (dev->irq < 0) {
829 dev_err(dev->device, "no irq resource\n");
830 rc = -ENODEV;
831 goto probe_disable_clk;
832 }
833
834 /* register the ISR handler */
835 rc = devm_request_irq(&pdev->dev, dev->irq, bcm_kona_i2c_isr,
836 IRQF_SHARED, pdev->name, dev);
837 if (rc) {
838 dev_err(dev->device, "failed to request irq %i\n", dev->irq);
839 goto probe_disable_clk;
840 }
841
842 /* Enable the controller but leave it idle */
843 bcm_kona_i2c_send_cmd_to_ctrl(dev, BCM_CMD_NOACTION);
844
845 /* Disable pad output */
846 writel(PADCTL_PAD_OUT_EN_MASK, dev->base + PADCTL_OFFSET);
847
848 /* Disable internal clock */
849 bcm_kona_i2c_disable_clock(dev);
850
851 /* Disable external clock */
852 clk_disable_unprepare(dev->external_clk);
853
854 /* Add the i2c adapter */
855 adap = &dev->adapter;
856 i2c_set_adapdata(adap, dev);
857 adap->owner = THIS_MODULE;
858 strlcpy(adap->name, "Broadcom I2C adapter", sizeof(adap->name));
859 adap->algo = &bcm_algo;
860 adap->dev.parent = &pdev->dev;
861 adap->dev.of_node = pdev->dev.of_node;
862
863 rc = i2c_add_adapter(adap);
864 if (rc) {
865 dev_err(dev->device, "failed to add adapter\n");
866 return rc;
867 }
868
869 dev_info(dev->device, "device registered successfully\n");
870
871 return 0;
872
873probe_disable_clk:
874 bcm_kona_i2c_disable_clock(dev);
875 clk_disable_unprepare(dev->external_clk);
876
877 return rc;
878}
879
880static int bcm_kona_i2c_remove(struct platform_device *pdev)
881{
882 struct bcm_kona_i2c_dev *dev = platform_get_drvdata(pdev);
883
884 i2c_del_adapter(&dev->adapter);
885
886 return 0;
887}
888
889static const struct of_device_id bcm_kona_i2c_of_match[] = {
890 {.compatible = "brcm,kona-i2c",},
891 {},
892};
893MODULE_DEVICE_TABLE(of, bcm_kona_i2c_of_match);
894
895static struct platform_driver bcm_kona_i2c_driver = {
896 .driver = {
897 .name = "bcm-kona-i2c",
898 .owner = THIS_MODULE,
899 .of_match_table = bcm_kona_i2c_of_match,
900 },
901 .probe = bcm_kona_i2c_probe,
902 .remove = bcm_kona_i2c_remove,
903};
904module_platform_driver(bcm_kona_i2c_driver);
905
906MODULE_AUTHOR("Tim Kryger <tkryger@broadcom.com>");
907MODULE_DESCRIPTION("Broadcom Kona I2C Driver");
908MODULE_LICENSE("GPL v2");
diff --git a/drivers/i2c/busses/i2c-bcm2835.c b/drivers/i2c/busses/i2c-bcm2835.c
index ea4b08fc3353..77df97b932af 100644
--- a/drivers/i2c/busses/i2c-bcm2835.c
+++ b/drivers/i2c/busses/i2c-bcm2835.c
@@ -151,7 +151,7 @@ static int bcm2835_i2c_xfer_msg(struct bcm2835_i2c_dev *i2c_dev,
151 151
152 i2c_dev->msg_buf = msg->buf; 152 i2c_dev->msg_buf = msg->buf;
153 i2c_dev->msg_buf_remaining = msg->len; 153 i2c_dev->msg_buf_remaining = msg->len;
154 INIT_COMPLETION(i2c_dev->completion); 154 reinit_completion(&i2c_dev->completion);
155 155
156 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR); 156 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, BCM2835_I2C_C_CLEAR);
157 157
@@ -299,6 +299,7 @@ static int bcm2835_i2c_probe(struct platform_device *pdev)
299 strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name)); 299 strlcpy(adap->name, "bcm2835 I2C adapter", sizeof(adap->name));
300 adap->algo = &bcm2835_i2c_algo; 300 adap->algo = &bcm2835_i2c_algo;
301 adap->dev.parent = &pdev->dev; 301 adap->dev.parent = &pdev->dev;
302 adap->dev.of_node = pdev->dev.of_node;
302 303
303 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0); 304 bcm2835_i2c_writel(i2c_dev, BCM2835_I2C_C, 0);
304 305
diff --git a/drivers/i2c/busses/i2c-bfin-twi.c b/drivers/i2c/busses/i2c-bfin-twi.c
index 35a473ba3d81..3b9bd9a3f2b0 100644
--- a/drivers/i2c/busses/i2c-bfin-twi.c
+++ b/drivers/i2c/busses/i2c-bfin-twi.c
@@ -675,7 +675,7 @@ static int i2c_bfin_twi_probe(struct platform_device *pdev)
675 p_adap->retries = 3; 675 p_adap->retries = 3;
676 676
677 rc = peripheral_request_list( 677 rc = peripheral_request_list(
678 (unsigned short *)dev_get_platdata(&pdev->dev), 678 dev_get_platdata(&pdev->dev),
679 "i2c-bfin-twi"); 679 "i2c-bfin-twi");
680 if (rc) { 680 if (rc) {
681 dev_err(&pdev->dev, "Can't setup pin mux!\n"); 681 dev_err(&pdev->dev, "Can't setup pin mux!\n");
@@ -723,7 +723,7 @@ out_error_add_adapter:
723 free_irq(iface->irq, iface); 723 free_irq(iface->irq, iface);
724out_error_req_irq: 724out_error_req_irq:
725out_error_no_irq: 725out_error_no_irq:
726 peripheral_free_list((unsigned short *)dev_get_platdata(&pdev->dev)); 726 peripheral_free_list(dev_get_platdata(&pdev->dev));
727out_error_pin_mux: 727out_error_pin_mux:
728 iounmap(iface->regs_base); 728 iounmap(iface->regs_base);
729out_error_ioremap: 729out_error_ioremap:
@@ -739,7 +739,7 @@ static int i2c_bfin_twi_remove(struct platform_device *pdev)
739 739
740 i2c_del_adapter(&(iface->adap)); 740 i2c_del_adapter(&(iface->adap));
741 free_irq(iface->irq, iface); 741 free_irq(iface->irq, iface);
742 peripheral_free_list((unsigned short *)dev_get_platdata(&pdev->dev)); 742 peripheral_free_list(dev_get_platdata(&pdev->dev));
743 iounmap(iface->regs_base); 743 iounmap(iface->regs_base);
744 kfree(iface); 744 kfree(iface);
745 745
diff --git a/drivers/i2c/busses/i2c-cbus-gpio.c b/drivers/i2c/busses/i2c-cbus-gpio.c
index 2d46f13adfdf..ce7ffba2b020 100644
--- a/drivers/i2c/busses/i2c-cbus-gpio.c
+++ b/drivers/i2c/busses/i2c-cbus-gpio.c
@@ -246,6 +246,7 @@ static int cbus_i2c_probe(struct platform_device *pdev)
246 adapter->owner = THIS_MODULE; 246 adapter->owner = THIS_MODULE;
247 adapter->class = I2C_CLASS_HWMON; 247 adapter->class = I2C_CLASS_HWMON;
248 adapter->dev.parent = &pdev->dev; 248 adapter->dev.parent = &pdev->dev;
249 adapter->dev.of_node = pdev->dev.of_node;
249 adapter->nr = pdev->id; 250 adapter->nr = pdev->id;
250 adapter->timeout = HZ; 251 adapter->timeout = HZ;
251 adapter->algo = &cbus_i2c_algo; 252 adapter->algo = &cbus_i2c_algo;
@@ -289,6 +290,7 @@ static struct platform_driver cbus_i2c_driver = {
289 .driver = { 290 .driver = {
290 .owner = THIS_MODULE, 291 .owner = THIS_MODULE,
291 .name = "i2c-cbus-gpio", 292 .name = "i2c-cbus-gpio",
293 .of_match_table = of_match_ptr(i2c_cbus_dt_ids),
292 }, 294 },
293}; 295};
294module_platform_driver(cbus_i2c_driver); 296module_platform_driver(cbus_i2c_driver);
diff --git a/drivers/i2c/busses/i2c-cpm.c b/drivers/i2c/busses/i2c-cpm.c
index b2b8aa9adc0e..3e5ea2c87a6e 100644
--- a/drivers/i2c/busses/i2c-cpm.c
+++ b/drivers/i2c/busses/i2c-cpm.c
@@ -447,7 +447,7 @@ static int cpm_i2c_setup(struct cpm_i2c *cpm)
447 447
448 init_waitqueue_head(&cpm->i2c_wait); 448 init_waitqueue_head(&cpm->i2c_wait);
449 449
450 cpm->irq = of_irq_to_resource(ofdev->dev.of_node, 0, NULL); 450 cpm->irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
451 if (!cpm->irq) 451 if (!cpm->irq)
452 return -EINVAL; 452 return -EINVAL;
453 453
diff --git a/drivers/i2c/busses/i2c-davinci.c b/drivers/i2c/busses/i2c-davinci.c
index 57473415be10..af0b5830303d 100644
--- a/drivers/i2c/busses/i2c-davinci.c
+++ b/drivers/i2c/busses/i2c-davinci.c
@@ -125,12 +125,12 @@ static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
125static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev, 125static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
126 int reg, u16 val) 126 int reg, u16 val)
127{ 127{
128 __raw_writew(val, i2c_dev->base + reg); 128 writew_relaxed(val, i2c_dev->base + reg);
129} 129}
130 130
131static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg) 131static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
132{ 132{
133 return __raw_readw(i2c_dev->base + reg); 133 return readw_relaxed(i2c_dev->base + reg);
134} 134}
135 135
136/* Generate a pulse on the i2c clock pin. */ 136/* Generate a pulse on the i2c clock pin. */
@@ -323,7 +323,7 @@ i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
323 323
324 davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len); 324 davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
325 325
326 INIT_COMPLETION(dev->cmd_complete); 326 reinit_completion(&dev->cmd_complete);
327 dev->cmd_err = 0; 327 dev->cmd_err = 0;
328 328
329 /* Take I2C out of reset and configure it as master */ 329 /* Take I2C out of reset and configure it as master */
@@ -662,7 +662,7 @@ static int davinci_i2c_probe(struct platform_device *pdev)
662#endif 662#endif
663 dev->dev = &pdev->dev; 663 dev->dev = &pdev->dev;
664 dev->irq = irq->start; 664 dev->irq = irq->start;
665 dev->pdata = dev_get_platdata(&dev->dev); 665 dev->pdata = dev_get_platdata(&pdev->dev);
666 platform_set_drvdata(pdev, dev); 666 platform_set_drvdata(pdev, dev);
667 667
668 if (!dev->pdata && pdev->dev.of_node) { 668 if (!dev->pdata && pdev->dev.of_node) {
@@ -795,7 +795,7 @@ static struct platform_driver davinci_i2c_driver = {
795 .name = "i2c_davinci", 795 .name = "i2c_davinci",
796 .owner = THIS_MODULE, 796 .owner = THIS_MODULE,
797 .pm = davinci_i2c_pm_ops, 797 .pm = davinci_i2c_pm_ops,
798 .of_match_table = of_match_ptr(davinci_i2c_of_match), 798 .of_match_table = davinci_i2c_of_match,
799 }, 799 },
800}; 800};
801 801
diff --git a/drivers/i2c/busses/i2c-designware-core.c b/drivers/i2c/busses/i2c-designware-core.c
index dbecf08399f8..e89e3e2145e5 100644
--- a/drivers/i2c/busses/i2c-designware-core.c
+++ b/drivers/i2c/busses/i2c-designware-core.c
@@ -98,6 +98,8 @@
98 98
99#define DW_IC_ERR_TX_ABRT 0x1 99#define DW_IC_ERR_TX_ABRT 0x1
100 100
101#define DW_IC_TAR_10BITADDR_MASTER BIT(12)
102
101/* 103/*
102 * status codes 104 * status codes
103 */ 105 */
@@ -388,22 +390,34 @@ static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
388static void i2c_dw_xfer_init(struct dw_i2c_dev *dev) 390static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
389{ 391{
390 struct i2c_msg *msgs = dev->msgs; 392 struct i2c_msg *msgs = dev->msgs;
391 u32 ic_con; 393 u32 ic_con, ic_tar = 0;
392 394
393 /* Disable the adapter */ 395 /* Disable the adapter */
394 __i2c_dw_enable(dev, false); 396 __i2c_dw_enable(dev, false);
395 397
396 /* set the slave (target) address */
397 dw_writel(dev, msgs[dev->msg_write_idx].addr, DW_IC_TAR);
398
399 /* if the slave address is ten bit address, enable 10BITADDR */ 398 /* if the slave address is ten bit address, enable 10BITADDR */
400 ic_con = dw_readl(dev, DW_IC_CON); 399 ic_con = dw_readl(dev, DW_IC_CON);
401 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) 400 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) {
402 ic_con |= DW_IC_CON_10BITADDR_MASTER; 401 ic_con |= DW_IC_CON_10BITADDR_MASTER;
403 else 402 /*
403 * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing
404 * mode has to be enabled via bit 12 of IC_TAR register.
405 * We set it always as I2C_DYNAMIC_TAR_UPDATE can't be
406 * detected from registers.
407 */
408 ic_tar = DW_IC_TAR_10BITADDR_MASTER;
409 } else {
404 ic_con &= ~DW_IC_CON_10BITADDR_MASTER; 410 ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
411 }
412
405 dw_writel(dev, ic_con, DW_IC_CON); 413 dw_writel(dev, ic_con, DW_IC_CON);
406 414
415 /*
416 * Set the slave (target) address and enable 10-bit addressing mode
417 * if applicable.
418 */
419 dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR);
420
407 /* Enable the adapter */ 421 /* Enable the adapter */
408 __i2c_dw_enable(dev, true); 422 __i2c_dw_enable(dev, true);
409 423
@@ -599,7 +613,7 @@ i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
599 mutex_lock(&dev->lock); 613 mutex_lock(&dev->lock);
600 pm_runtime_get_sync(dev->dev); 614 pm_runtime_get_sync(dev->dev);
601 615
602 INIT_COMPLETION(dev->cmd_complete); 616 reinit_completion(&dev->cmd_complete);
603 dev->msgs = msgs; 617 dev->msgs = msgs;
604 dev->msgs_num = num; 618 dev->msgs_num = num;
605 dev->cmd_err = 0; 619 dev->cmd_err = 0;
diff --git a/drivers/i2c/busses/i2c-designware-platdrv.c b/drivers/i2c/busses/i2c-designware-platdrv.c
index 4c1b60539a25..d0bdac0498ce 100644
--- a/drivers/i2c/busses/i2c-designware-platdrv.c
+++ b/drivers/i2c/busses/i2c-designware-platdrv.c
@@ -103,6 +103,8 @@ static int dw_i2c_acpi_configure(struct platform_device *pdev)
103static const struct acpi_device_id dw_i2c_acpi_match[] = { 103static const struct acpi_device_id dw_i2c_acpi_match[] = {
104 { "INT33C2", 0 }, 104 { "INT33C2", 0 },
105 { "INT33C3", 0 }, 105 { "INT33C3", 0 },
106 { "INT3432", 0 },
107 { "INT3433", 0 },
106 { "80860F41", 0 }, 108 { "80860F41", 0 },
107 { } 109 { }
108}; 110};
@@ -270,7 +272,8 @@ static SIMPLE_DEV_PM_OPS(dw_i2c_dev_pm_ops, dw_i2c_suspend, dw_i2c_resume);
270MODULE_ALIAS("platform:i2c_designware"); 272MODULE_ALIAS("platform:i2c_designware");
271 273
272static struct platform_driver dw_i2c_driver = { 274static struct platform_driver dw_i2c_driver = {
273 .remove = dw_i2c_remove, 275 .probe = dw_i2c_probe,
276 .remove = dw_i2c_remove,
274 .driver = { 277 .driver = {
275 .name = "i2c_designware", 278 .name = "i2c_designware",
276 .owner = THIS_MODULE, 279 .owner = THIS_MODULE,
@@ -282,7 +285,7 @@ static struct platform_driver dw_i2c_driver = {
282 285
283static int __init dw_i2c_init_driver(void) 286static int __init dw_i2c_init_driver(void)
284{ 287{
285 return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe); 288 return platform_driver_register(&dw_i2c_driver);
286} 289}
287subsys_initcall(dw_i2c_init_driver); 290subsys_initcall(dw_i2c_init_driver);
288 291
diff --git a/drivers/i2c/busses/i2c-diolan-u2c.c b/drivers/i2c/busses/i2c-diolan-u2c.c
index dae3ddfe7619..721f7ebf9a3b 100644
--- a/drivers/i2c/busses/i2c-diolan-u2c.c
+++ b/drivers/i2c/busses/i2c-diolan-u2c.c
@@ -25,8 +25,6 @@
25#define USB_VENDOR_ID_DIOLAN 0x0abf 25#define USB_VENDOR_ID_DIOLAN 0x0abf
26#define USB_DEVICE_ID_DIOLAN_U2C 0x3370 26#define USB_DEVICE_ID_DIOLAN_U2C 0x3370
27 27
28#define DIOLAN_OUT_EP 0x02
29#define DIOLAN_IN_EP 0x84
30 28
31/* commands via USB, must match command ids in the firmware */ 29/* commands via USB, must match command ids in the firmware */
32#define CMD_I2C_READ 0x01 30#define CMD_I2C_READ 0x01
@@ -84,6 +82,7 @@
84struct i2c_diolan_u2c { 82struct i2c_diolan_u2c {
85 u8 obuffer[DIOLAN_OUTBUF_LEN]; /* output buffer */ 83 u8 obuffer[DIOLAN_OUTBUF_LEN]; /* output buffer */
86 u8 ibuffer[DIOLAN_INBUF_LEN]; /* input buffer */ 84 u8 ibuffer[DIOLAN_INBUF_LEN]; /* input buffer */
85 int ep_in, ep_out; /* Endpoints */
87 struct usb_device *usb_dev; /* the usb device for this device */ 86 struct usb_device *usb_dev; /* the usb device for this device */
88 struct usb_interface *interface;/* the interface for this device */ 87 struct usb_interface *interface;/* the interface for this device */
89 struct i2c_adapter adapter; /* i2c related things */ 88 struct i2c_adapter adapter; /* i2c related things */
@@ -109,7 +108,7 @@ static int diolan_usb_transfer(struct i2c_diolan_u2c *dev)
109 return -EINVAL; 108 return -EINVAL;
110 109
111 ret = usb_bulk_msg(dev->usb_dev, 110 ret = usb_bulk_msg(dev->usb_dev,
112 usb_sndbulkpipe(dev->usb_dev, DIOLAN_OUT_EP), 111 usb_sndbulkpipe(dev->usb_dev, dev->ep_out),
113 dev->obuffer, dev->olen, &actual, 112 dev->obuffer, dev->olen, &actual,
114 DIOLAN_USB_TIMEOUT); 113 DIOLAN_USB_TIMEOUT);
115 if (!ret) { 114 if (!ret) {
@@ -118,7 +117,7 @@ static int diolan_usb_transfer(struct i2c_diolan_u2c *dev)
118 117
119 tmpret = usb_bulk_msg(dev->usb_dev, 118 tmpret = usb_bulk_msg(dev->usb_dev,
120 usb_rcvbulkpipe(dev->usb_dev, 119 usb_rcvbulkpipe(dev->usb_dev,
121 DIOLAN_IN_EP), 120 dev->ep_in),
122 dev->ibuffer, 121 dev->ibuffer,
123 sizeof(dev->ibuffer), &actual, 122 sizeof(dev->ibuffer), &actual,
124 DIOLAN_USB_TIMEOUT); 123 DIOLAN_USB_TIMEOUT);
@@ -210,7 +209,7 @@ static void diolan_flush_input(struct i2c_diolan_u2c *dev)
210 int ret; 209 int ret;
211 210
212 ret = usb_bulk_msg(dev->usb_dev, 211 ret = usb_bulk_msg(dev->usb_dev,
213 usb_rcvbulkpipe(dev->usb_dev, DIOLAN_IN_EP), 212 usb_rcvbulkpipe(dev->usb_dev, dev->ep_in),
214 dev->ibuffer, sizeof(dev->ibuffer), &actual, 213 dev->ibuffer, sizeof(dev->ibuffer), &actual,
215 DIOLAN_USB_TIMEOUT); 214 DIOLAN_USB_TIMEOUT);
216 if (ret < 0 || actual == 0) 215 if (ret < 0 || actual == 0)
@@ -445,9 +444,14 @@ static void diolan_u2c_free(struct i2c_diolan_u2c *dev)
445static int diolan_u2c_probe(struct usb_interface *interface, 444static int diolan_u2c_probe(struct usb_interface *interface,
446 const struct usb_device_id *id) 445 const struct usb_device_id *id)
447{ 446{
447 struct usb_host_interface *hostif = interface->cur_altsetting;
448 struct i2c_diolan_u2c *dev; 448 struct i2c_diolan_u2c *dev;
449 int ret; 449 int ret;
450 450
451 if (hostif->desc.bInterfaceNumber != 0
452 || hostif->desc.bNumEndpoints < 2)
453 return -ENODEV;
454
451 /* allocate memory for our device state and initialize it */ 455 /* allocate memory for our device state and initialize it */
452 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 456 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
453 if (dev == NULL) { 457 if (dev == NULL) {
@@ -455,6 +459,8 @@ static int diolan_u2c_probe(struct usb_interface *interface,
455 ret = -ENOMEM; 459 ret = -ENOMEM;
456 goto error; 460 goto error;
457 } 461 }
462 dev->ep_out = hostif->endpoint[0].desc.bEndpointAddress;
463 dev->ep_in = hostif->endpoint[1].desc.bEndpointAddress;
458 464
459 dev->usb_dev = usb_get_dev(interface_to_usbdev(interface)); 465 dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
460 dev->interface = interface; 466 dev->interface = interface;
diff --git a/drivers/i2c/busses/i2c-eg20t.c b/drivers/i2c/busses/i2c-eg20t.c
index 0f3752967c4b..ff15ae90aaf5 100644
--- a/drivers/i2c/busses/i2c-eg20t.c
+++ b/drivers/i2c/busses/i2c-eg20t.c
@@ -312,24 +312,6 @@ static void pch_i2c_start(struct i2c_algo_pch_data *adap)
312} 312}
313 313
314/** 314/**
315 * pch_i2c_getack() - to confirm ACK/NACK
316 * @adap: Pointer to struct i2c_algo_pch_data.
317 */
318static s32 pch_i2c_getack(struct i2c_algo_pch_data *adap)
319{
320 u32 reg_val;
321 void __iomem *p = adap->pch_base_address;
322 reg_val = ioread32(p + PCH_I2CSR) & PCH_GETACK;
323
324 if (reg_val != 0) {
325 pch_err(adap, "return%d\n", -EPROTO);
326 return -EPROTO;
327 }
328
329 return 0;
330}
331
332/**
333 * pch_i2c_stop() - generate stop condition in normal mode. 315 * pch_i2c_stop() - generate stop condition in normal mode.
334 * @adap: Pointer to struct i2c_algo_pch_data. 316 * @adap: Pointer to struct i2c_algo_pch_data.
335 */ 317 */
@@ -344,6 +326,7 @@ static void pch_i2c_stop(struct i2c_algo_pch_data *adap)
344static int pch_i2c_wait_for_check_xfer(struct i2c_algo_pch_data *adap) 326static int pch_i2c_wait_for_check_xfer(struct i2c_algo_pch_data *adap)
345{ 327{
346 long ret; 328 long ret;
329 void __iomem *p = adap->pch_base_address;
347 330
348 ret = wait_event_timeout(pch_event, 331 ret = wait_event_timeout(pch_event,
349 (adap->pch_event_flag != 0), msecs_to_jiffies(1000)); 332 (adap->pch_event_flag != 0), msecs_to_jiffies(1000));
@@ -366,10 +349,9 @@ static int pch_i2c_wait_for_check_xfer(struct i2c_algo_pch_data *adap)
366 349
367 adap->pch_event_flag = 0; 350 adap->pch_event_flag = 0;
368 351
369 if (pch_i2c_getack(adap)) { 352 if (ioread32(p + PCH_I2CSR) & PCH_GETACK) {
370 pch_dbg(adap, "Receive NACK for slave address" 353 pch_dbg(adap, "Receive NACK for slave address setting\n");
371 "setting\n"); 354 return -ENXIO;
372 return -EIO;
373 } 355 }
374 356
375 return 0; 357 return 0;
diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
new file mode 100644
index 000000000000..c1ef228095b5
--- /dev/null
+++ b/drivers/i2c/busses/i2c-exynos5.c
@@ -0,0 +1,769 @@
1/**
2 * i2c-exynos5.c - Samsung Exynos5 I2C Controller Driver
3 *
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9*/
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13
14#include <linux/i2c.h>
15#include <linux/init.h>
16#include <linux/time.h>
17#include <linux/interrupt.h>
18#include <linux/delay.h>
19#include <linux/errno.h>
20#include <linux/err.h>
21#include <linux/platform_device.h>
22#include <linux/clk.h>
23#include <linux/slab.h>
24#include <linux/io.h>
25#include <linux/of_address.h>
26#include <linux/of_irq.h>
27#include <linux/spinlock.h>
28
29/*
30 * HSI2C controller from Samsung supports 2 modes of operation
31 * 1. Auto mode: Where in master automatically controls the whole transaction
32 * 2. Manual mode: Software controls the transaction by issuing commands
33 * START, READ, WRITE, STOP, RESTART in I2C_MANUAL_CMD register.
34 *
35 * Operation mode can be selected by setting AUTO_MODE bit in I2C_CONF register
36 *
37 * Special bits are available for both modes of operation to set commands
38 * and for checking transfer status
39 */
40
41/* Register Map */
42#define HSI2C_CTL 0x00
43#define HSI2C_FIFO_CTL 0x04
44#define HSI2C_TRAILIG_CTL 0x08
45#define HSI2C_CLK_CTL 0x0C
46#define HSI2C_CLK_SLOT 0x10
47#define HSI2C_INT_ENABLE 0x20
48#define HSI2C_INT_STATUS 0x24
49#define HSI2C_ERR_STATUS 0x2C
50#define HSI2C_FIFO_STATUS 0x30
51#define HSI2C_TX_DATA 0x34
52#define HSI2C_RX_DATA 0x38
53#define HSI2C_CONF 0x40
54#define HSI2C_AUTO_CONF 0x44
55#define HSI2C_TIMEOUT 0x48
56#define HSI2C_MANUAL_CMD 0x4C
57#define HSI2C_TRANS_STATUS 0x50
58#define HSI2C_TIMING_HS1 0x54
59#define HSI2C_TIMING_HS2 0x58
60#define HSI2C_TIMING_HS3 0x5C
61#define HSI2C_TIMING_FS1 0x60
62#define HSI2C_TIMING_FS2 0x64
63#define HSI2C_TIMING_FS3 0x68
64#define HSI2C_TIMING_SLA 0x6C
65#define HSI2C_ADDR 0x70
66
67/* I2C_CTL Register bits */
68#define HSI2C_FUNC_MODE_I2C (1u << 0)
69#define HSI2C_MASTER (1u << 3)
70#define HSI2C_RXCHON (1u << 6)
71#define HSI2C_TXCHON (1u << 7)
72#define HSI2C_SW_RST (1u << 31)
73
74/* I2C_FIFO_CTL Register bits */
75#define HSI2C_RXFIFO_EN (1u << 0)
76#define HSI2C_TXFIFO_EN (1u << 1)
77#define HSI2C_RXFIFO_TRIGGER_LEVEL(x) ((x) << 4)
78#define HSI2C_TXFIFO_TRIGGER_LEVEL(x) ((x) << 16)
79
80/* As per user manual FIFO max depth is 64bytes */
81#define HSI2C_FIFO_MAX 0x40
82/* default trigger levels for Tx and Rx FIFOs */
83#define HSI2C_DEF_TXFIFO_LVL (HSI2C_FIFO_MAX - 0x30)
84#define HSI2C_DEF_RXFIFO_LVL (HSI2C_FIFO_MAX - 0x10)
85
86/* I2C_TRAILING_CTL Register bits */
87#define HSI2C_TRAILING_COUNT (0xf)
88
89/* I2C_INT_EN Register bits */
90#define HSI2C_INT_TX_ALMOSTEMPTY_EN (1u << 0)
91#define HSI2C_INT_RX_ALMOSTFULL_EN (1u << 1)
92#define HSI2C_INT_TRAILING_EN (1u << 6)
93#define HSI2C_INT_I2C_EN (1u << 9)
94
95/* I2C_INT_STAT Register bits */
96#define HSI2C_INT_TX_ALMOSTEMPTY (1u << 0)
97#define HSI2C_INT_RX_ALMOSTFULL (1u << 1)
98#define HSI2C_INT_TX_UNDERRUN (1u << 2)
99#define HSI2C_INT_TX_OVERRUN (1u << 3)
100#define HSI2C_INT_RX_UNDERRUN (1u << 4)
101#define HSI2C_INT_RX_OVERRUN (1u << 5)
102#define HSI2C_INT_TRAILING (1u << 6)
103#define HSI2C_INT_I2C (1u << 9)
104
105/* I2C_FIFO_STAT Register bits */
106#define HSI2C_RX_FIFO_EMPTY (1u << 24)
107#define HSI2C_RX_FIFO_FULL (1u << 23)
108#define HSI2C_RX_FIFO_LVL(x) ((x >> 16) & 0x7f)
109#define HSI2C_TX_FIFO_EMPTY (1u << 8)
110#define HSI2C_TX_FIFO_FULL (1u << 7)
111#define HSI2C_TX_FIFO_LVL(x) ((x >> 0) & 0x7f)
112
113/* I2C_CONF Register bits */
114#define HSI2C_AUTO_MODE (1u << 31)
115#define HSI2C_10BIT_ADDR_MODE (1u << 30)
116#define HSI2C_HS_MODE (1u << 29)
117
118/* I2C_AUTO_CONF Register bits */
119#define HSI2C_READ_WRITE (1u << 16)
120#define HSI2C_STOP_AFTER_TRANS (1u << 17)
121#define HSI2C_MASTER_RUN (1u << 31)
122
123/* I2C_TIMEOUT Register bits */
124#define HSI2C_TIMEOUT_EN (1u << 31)
125#define HSI2C_TIMEOUT_MASK 0xff
126
127/* I2C_TRANS_STATUS register bits */
128#define HSI2C_MASTER_BUSY (1u << 17)
129#define HSI2C_SLAVE_BUSY (1u << 16)
130#define HSI2C_TIMEOUT_AUTO (1u << 4)
131#define HSI2C_NO_DEV (1u << 3)
132#define HSI2C_NO_DEV_ACK (1u << 2)
133#define HSI2C_TRANS_ABORT (1u << 1)
134#define HSI2C_TRANS_DONE (1u << 0)
135
136/* I2C_ADDR register bits */
137#define HSI2C_SLV_ADDR_SLV(x) ((x & 0x3ff) << 0)
138#define HSI2C_SLV_ADDR_MAS(x) ((x & 0x3ff) << 10)
139#define HSI2C_MASTER_ID(x) ((x & 0xff) << 24)
140#define MASTER_ID(x) ((x & 0x7) + 0x08)
141
142/*
143 * Controller operating frequency, timing values for operation
144 * are calculated against this frequency
145 */
146#define HSI2C_HS_TX_CLOCK 1000000
147#define HSI2C_FS_TX_CLOCK 100000
148#define HSI2C_HIGH_SPD 1
149#define HSI2C_FAST_SPD 0
150
151#define EXYNOS5_I2C_TIMEOUT (msecs_to_jiffies(1000))
152
153struct exynos5_i2c {
154 struct i2c_adapter adap;
155 unsigned int suspended:1;
156
157 struct i2c_msg *msg;
158 struct completion msg_complete;
159 unsigned int msg_ptr;
160
161 unsigned int irq;
162
163 void __iomem *regs;
164 struct clk *clk;
165 struct device *dev;
166 int state;
167
168 spinlock_t lock; /* IRQ synchronization */
169
170 /*
171 * Since the TRANS_DONE bit is cleared on read, and we may read it
172 * either during an IRQ or after a transaction, keep track of its
173 * state here.
174 */
175 int trans_done;
176
177 /* Controller operating frequency */
178 unsigned int fs_clock;
179 unsigned int hs_clock;
180
181 /*
182 * HSI2C Controller can operate in
183 * 1. High speed upto 3.4Mbps
184 * 2. Fast speed upto 1Mbps
185 */
186 int speed_mode;
187};
188
189static const struct of_device_id exynos5_i2c_match[] = {
190 { .compatible = "samsung,exynos5-hsi2c" },
191 {},
192};
193MODULE_DEVICE_TABLE(of, exynos5_i2c_match);
194
195static void exynos5_i2c_clr_pend_irq(struct exynos5_i2c *i2c)
196{
197 writel(readl(i2c->regs + HSI2C_INT_STATUS),
198 i2c->regs + HSI2C_INT_STATUS);
199}
200
201/*
202 * exynos5_i2c_set_timing: updates the registers with appropriate
203 * timing values calculated
204 *
205 * Returns 0 on success, -EINVAL if the cycle length cannot
206 * be calculated.
207 */
208static int exynos5_i2c_set_timing(struct exynos5_i2c *i2c, int mode)
209{
210 u32 i2c_timing_s1;
211 u32 i2c_timing_s2;
212 u32 i2c_timing_s3;
213 u32 i2c_timing_sla;
214 unsigned int t_start_su, t_start_hd;
215 unsigned int t_stop_su;
216 unsigned int t_data_su, t_data_hd;
217 unsigned int t_scl_l, t_scl_h;
218 unsigned int t_sr_release;
219 unsigned int t_ftl_cycle;
220 unsigned int clkin = clk_get_rate(i2c->clk);
221 unsigned int div, utemp0 = 0, utemp1 = 0, clk_cycle;
222 unsigned int op_clk = (mode == HSI2C_HIGH_SPD) ?
223 i2c->hs_clock : i2c->fs_clock;
224
225 /*
226 * FPCLK / FI2C =
227 * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
228 * utemp0 = (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2)
229 * utemp1 = (TSCLK_L + TSCLK_H + 2)
230 */
231 t_ftl_cycle = (readl(i2c->regs + HSI2C_CONF) >> 16) & 0x7;
232 utemp0 = (clkin / op_clk) - 8 - 2 * t_ftl_cycle;
233
234 /* CLK_DIV max is 256 */
235 for (div = 0; div < 256; div++) {
236 utemp1 = utemp0 / (div + 1);
237
238 /*
239 * SCL_L and SCL_H each has max value of 255
240 * Hence, For the clk_cycle to the have right value
241 * utemp1 has to be less then 512 and more than 4.
242 */
243 if ((utemp1 < 512) && (utemp1 > 4)) {
244 clk_cycle = utemp1 - 2;
245 break;
246 } else if (div == 255) {
247 dev_warn(i2c->dev, "Failed to calculate divisor");
248 return -EINVAL;
249 }
250 }
251
252 t_scl_l = clk_cycle / 2;
253 t_scl_h = clk_cycle / 2;
254 t_start_su = t_scl_l;
255 t_start_hd = t_scl_l;
256 t_stop_su = t_scl_l;
257 t_data_su = t_scl_l / 2;
258 t_data_hd = t_scl_l / 2;
259 t_sr_release = clk_cycle;
260
261 i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
262 i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
263 i2c_timing_s3 = div << 16 | t_sr_release << 0;
264 i2c_timing_sla = t_data_hd << 0;
265
266 dev_dbg(i2c->dev, "tSTART_SU: %X, tSTART_HD: %X, tSTOP_SU: %X\n",
267 t_start_su, t_start_hd, t_stop_su);
268 dev_dbg(i2c->dev, "tDATA_SU: %X, tSCL_L: %X, tSCL_H: %X\n",
269 t_data_su, t_scl_l, t_scl_h);
270 dev_dbg(i2c->dev, "nClkDiv: %X, tSR_RELEASE: %X\n",
271 div, t_sr_release);
272 dev_dbg(i2c->dev, "tDATA_HD: %X\n", t_data_hd);
273
274 if (mode == HSI2C_HIGH_SPD) {
275 writel(i2c_timing_s1, i2c->regs + HSI2C_TIMING_HS1);
276 writel(i2c_timing_s2, i2c->regs + HSI2C_TIMING_HS2);
277 writel(i2c_timing_s3, i2c->regs + HSI2C_TIMING_HS3);
278 } else {
279 writel(i2c_timing_s1, i2c->regs + HSI2C_TIMING_FS1);
280 writel(i2c_timing_s2, i2c->regs + HSI2C_TIMING_FS2);
281 writel(i2c_timing_s3, i2c->regs + HSI2C_TIMING_FS3);
282 }
283 writel(i2c_timing_sla, i2c->regs + HSI2C_TIMING_SLA);
284
285 return 0;
286}
287
288static int exynos5_hsi2c_clock_setup(struct exynos5_i2c *i2c)
289{
290 /*
291 * Configure the Fast speed timing values
292 * Even the High Speed mode initially starts with Fast mode
293 */
294 if (exynos5_i2c_set_timing(i2c, HSI2C_FAST_SPD)) {
295 dev_err(i2c->dev, "HSI2C FS Clock set up failed\n");
296 return -EINVAL;
297 }
298
299 /* configure the High speed timing values */
300 if (i2c->speed_mode == HSI2C_HIGH_SPD) {
301 if (exynos5_i2c_set_timing(i2c, HSI2C_HIGH_SPD)) {
302 dev_err(i2c->dev, "HSI2C HS Clock set up failed\n");
303 return -EINVAL;
304 }
305 }
306
307 return 0;
308}
309
310/*
311 * exynos5_i2c_init: configures the controller for I2C functionality
312 * Programs I2C controller for Master mode operation
313 */
314static void exynos5_i2c_init(struct exynos5_i2c *i2c)
315{
316 u32 i2c_conf = readl(i2c->regs + HSI2C_CONF);
317 u32 i2c_timeout = readl(i2c->regs + HSI2C_TIMEOUT);
318
319 /* Clear to disable Timeout */
320 i2c_timeout &= ~HSI2C_TIMEOUT_EN;
321 writel(i2c_timeout, i2c->regs + HSI2C_TIMEOUT);
322
323 writel((HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
324 i2c->regs + HSI2C_CTL);
325 writel(HSI2C_TRAILING_COUNT, i2c->regs + HSI2C_TRAILIG_CTL);
326
327 if (i2c->speed_mode == HSI2C_HIGH_SPD) {
328 writel(HSI2C_MASTER_ID(MASTER_ID(i2c->adap.nr)),
329 i2c->regs + HSI2C_ADDR);
330 i2c_conf |= HSI2C_HS_MODE;
331 }
332
333 writel(i2c_conf | HSI2C_AUTO_MODE, i2c->regs + HSI2C_CONF);
334}
335
336static void exynos5_i2c_reset(struct exynos5_i2c *i2c)
337{
338 u32 i2c_ctl;
339
340 /* Set and clear the bit for reset */
341 i2c_ctl = readl(i2c->regs + HSI2C_CTL);
342 i2c_ctl |= HSI2C_SW_RST;
343 writel(i2c_ctl, i2c->regs + HSI2C_CTL);
344
345 i2c_ctl = readl(i2c->regs + HSI2C_CTL);
346 i2c_ctl &= ~HSI2C_SW_RST;
347 writel(i2c_ctl, i2c->regs + HSI2C_CTL);
348
349 /* We don't expect calculations to fail during the run */
350 exynos5_hsi2c_clock_setup(i2c);
351 /* Initialize the configure registers */
352 exynos5_i2c_init(i2c);
353}
354
355/*
356 * exynos5_i2c_irq: top level IRQ servicing routine
357 *
358 * INT_STATUS registers gives the interrupt details. Further,
359 * FIFO_STATUS or TRANS_STATUS registers are to be check for detailed
360 * state of the bus.
361 */
362static irqreturn_t exynos5_i2c_irq(int irqno, void *dev_id)
363{
364 struct exynos5_i2c *i2c = dev_id;
365 u32 fifo_level, int_status, fifo_status, trans_status;
366 unsigned char byte;
367 int len = 0;
368
369 i2c->state = -EINVAL;
370
371 spin_lock(&i2c->lock);
372
373 int_status = readl(i2c->regs + HSI2C_INT_STATUS);
374 writel(int_status, i2c->regs + HSI2C_INT_STATUS);
375 fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
376
377 /* handle interrupt related to the transfer status */
378 if (int_status & HSI2C_INT_I2C) {
379 trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS);
380 if (trans_status & HSI2C_NO_DEV_ACK) {
381 dev_dbg(i2c->dev, "No ACK from device\n");
382 i2c->state = -ENXIO;
383 goto stop;
384 } else if (trans_status & HSI2C_NO_DEV) {
385 dev_dbg(i2c->dev, "No device\n");
386 i2c->state = -ENXIO;
387 goto stop;
388 } else if (trans_status & HSI2C_TRANS_ABORT) {
389 dev_dbg(i2c->dev, "Deal with arbitration lose\n");
390 i2c->state = -EAGAIN;
391 goto stop;
392 } else if (trans_status & HSI2C_TIMEOUT_AUTO) {
393 dev_dbg(i2c->dev, "Accessing device timed out\n");
394 i2c->state = -EAGAIN;
395 goto stop;
396 } else if (trans_status & HSI2C_TRANS_DONE) {
397 i2c->trans_done = 1;
398 i2c->state = 0;
399 }
400 }
401
402 if ((i2c->msg->flags & I2C_M_RD) && (int_status &
403 (HSI2C_INT_TRAILING | HSI2C_INT_RX_ALMOSTFULL))) {
404 fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
405 fifo_level = HSI2C_RX_FIFO_LVL(fifo_status);
406 len = min(fifo_level, i2c->msg->len - i2c->msg_ptr);
407
408 while (len > 0) {
409 byte = (unsigned char)
410 readl(i2c->regs + HSI2C_RX_DATA);
411 i2c->msg->buf[i2c->msg_ptr++] = byte;
412 len--;
413 }
414 i2c->state = 0;
415 } else if (int_status & HSI2C_INT_TX_ALMOSTEMPTY) {
416 fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
417 fifo_level = HSI2C_TX_FIFO_LVL(fifo_status);
418
419 len = HSI2C_FIFO_MAX - fifo_level;
420 if (len > (i2c->msg->len - i2c->msg_ptr))
421 len = i2c->msg->len - i2c->msg_ptr;
422
423 while (len > 0) {
424 byte = i2c->msg->buf[i2c->msg_ptr++];
425 writel(byte, i2c->regs + HSI2C_TX_DATA);
426 len--;
427 }
428 i2c->state = 0;
429 }
430
431 stop:
432 if ((i2c->trans_done && (i2c->msg->len == i2c->msg_ptr)) ||
433 (i2c->state < 0)) {
434 writel(0, i2c->regs + HSI2C_INT_ENABLE);
435 exynos5_i2c_clr_pend_irq(i2c);
436 complete(&i2c->msg_complete);
437 }
438
439 spin_unlock(&i2c->lock);
440
441 return IRQ_HANDLED;
442}
443
444/*
445 * exynos5_i2c_wait_bus_idle
446 *
447 * Wait for the bus to go idle, indicated by the MASTER_BUSY bit being
448 * cleared.
449 *
450 * Returns -EBUSY if the bus cannot be bought to idle
451 */
452static int exynos5_i2c_wait_bus_idle(struct exynos5_i2c *i2c)
453{
454 unsigned long stop_time;
455 u32 trans_status;
456
457 /* wait for 100 milli seconds for the bus to be idle */
458 stop_time = jiffies + msecs_to_jiffies(100) + 1;
459 do {
460 trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS);
461 if (!(trans_status & HSI2C_MASTER_BUSY))
462 return 0;
463
464 usleep_range(50, 200);
465 } while (time_before(jiffies, stop_time));
466
467 return -EBUSY;
468}
469
470/*
471 * exynos5_i2c_message_start: Configures the bus and starts the xfer
472 * i2c: struct exynos5_i2c pointer for the current bus
473 * stop: Enables stop after transfer if set. Set for last transfer of
474 * in the list of messages.
475 *
476 * Configures the bus for read/write function
477 * Sets chip address to talk to, message length to be sent.
478 * Enables appropriate interrupts and sends start xfer command.
479 */
480static void exynos5_i2c_message_start(struct exynos5_i2c *i2c, int stop)
481{
482 u32 i2c_ctl;
483 u32 int_en = HSI2C_INT_I2C_EN;
484 u32 i2c_auto_conf = 0;
485 u32 fifo_ctl;
486 unsigned long flags;
487
488 i2c_ctl = readl(i2c->regs + HSI2C_CTL);
489 i2c_ctl &= ~(HSI2C_TXCHON | HSI2C_RXCHON);
490 fifo_ctl = HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN;
491
492 if (i2c->msg->flags & I2C_M_RD) {
493 i2c_ctl |= HSI2C_RXCHON;
494
495 i2c_auto_conf = HSI2C_READ_WRITE;
496
497 fifo_ctl |= HSI2C_RXFIFO_TRIGGER_LEVEL(HSI2C_DEF_TXFIFO_LVL);
498 int_en |= (HSI2C_INT_RX_ALMOSTFULL_EN |
499 HSI2C_INT_TRAILING_EN);
500 } else {
501 i2c_ctl |= HSI2C_TXCHON;
502
503 fifo_ctl |= HSI2C_TXFIFO_TRIGGER_LEVEL(HSI2C_DEF_RXFIFO_LVL);
504 int_en |= HSI2C_INT_TX_ALMOSTEMPTY_EN;
505 }
506
507 writel(HSI2C_SLV_ADDR_MAS(i2c->msg->addr), i2c->regs + HSI2C_ADDR);
508
509 writel(fifo_ctl, i2c->regs + HSI2C_FIFO_CTL);
510 writel(i2c_ctl, i2c->regs + HSI2C_CTL);
511
512
513 /*
514 * Enable interrupts before starting the transfer so that we don't
515 * miss any INT_I2C interrupts.
516 */
517 spin_lock_irqsave(&i2c->lock, flags);
518 writel(int_en, i2c->regs + HSI2C_INT_ENABLE);
519
520 if (stop == 1)
521 i2c_auto_conf |= HSI2C_STOP_AFTER_TRANS;
522 i2c_auto_conf |= i2c->msg->len;
523 i2c_auto_conf |= HSI2C_MASTER_RUN;
524 writel(i2c_auto_conf, i2c->regs + HSI2C_AUTO_CONF);
525 spin_unlock_irqrestore(&i2c->lock, flags);
526}
527
528static int exynos5_i2c_xfer_msg(struct exynos5_i2c *i2c,
529 struct i2c_msg *msgs, int stop)
530{
531 unsigned long timeout;
532 int ret;
533
534 i2c->msg = msgs;
535 i2c->msg_ptr = 0;
536 i2c->trans_done = 0;
537
538 reinit_completion(&i2c->msg_complete);
539
540 exynos5_i2c_message_start(i2c, stop);
541
542 timeout = wait_for_completion_timeout(&i2c->msg_complete,
543 EXYNOS5_I2C_TIMEOUT);
544 if (timeout == 0)
545 ret = -ETIMEDOUT;
546 else
547 ret = i2c->state;
548
549 /*
550 * If this is the last message to be transfered (stop == 1)
551 * Then check if the bus can be brought back to idle.
552 */
553 if (ret == 0 && stop)
554 ret = exynos5_i2c_wait_bus_idle(i2c);
555
556 if (ret < 0) {
557 exynos5_i2c_reset(i2c);
558 if (ret == -ETIMEDOUT)
559 dev_warn(i2c->dev, "%s timeout\n",
560 (msgs->flags & I2C_M_RD) ? "rx" : "tx");
561 }
562
563 /* Return the state as in interrupt routine */
564 return ret;
565}
566
567static int exynos5_i2c_xfer(struct i2c_adapter *adap,
568 struct i2c_msg *msgs, int num)
569{
570 struct exynos5_i2c *i2c = (struct exynos5_i2c *)adap->algo_data;
571 int i = 0, ret = 0, stop = 0;
572
573 if (i2c->suspended) {
574 dev_err(i2c->dev, "HS-I2C is not initialzed.\n");
575 return -EIO;
576 }
577
578 clk_prepare_enable(i2c->clk);
579
580 for (i = 0; i < num; i++, msgs++) {
581 stop = (i == num - 1);
582
583 ret = exynos5_i2c_xfer_msg(i2c, msgs, stop);
584
585 if (ret < 0)
586 goto out;
587 }
588
589 if (i == num) {
590 ret = num;
591 } else {
592 /* Only one message, cannot access the device */
593 if (i == 1)
594 ret = -EREMOTEIO;
595 else
596 ret = i;
597
598 dev_warn(i2c->dev, "xfer message failed\n");
599 }
600
601 out:
602 clk_disable_unprepare(i2c->clk);
603 return ret;
604}
605
606static u32 exynos5_i2c_func(struct i2c_adapter *adap)
607{
608 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
609}
610
611static const struct i2c_algorithm exynos5_i2c_algorithm = {
612 .master_xfer = exynos5_i2c_xfer,
613 .functionality = exynos5_i2c_func,
614};
615
616static int exynos5_i2c_probe(struct platform_device *pdev)
617{
618 struct device_node *np = pdev->dev.of_node;
619 struct exynos5_i2c *i2c;
620 struct resource *mem;
621 unsigned int op_clock;
622 int ret;
623
624 i2c = devm_kzalloc(&pdev->dev, sizeof(struct exynos5_i2c), GFP_KERNEL);
625 if (!i2c) {
626 dev_err(&pdev->dev, "no memory for state\n");
627 return -ENOMEM;
628 }
629
630 if (of_property_read_u32(np, "clock-frequency", &op_clock)) {
631 i2c->speed_mode = HSI2C_FAST_SPD;
632 i2c->fs_clock = HSI2C_FS_TX_CLOCK;
633 } else {
634 if (op_clock >= HSI2C_HS_TX_CLOCK) {
635 i2c->speed_mode = HSI2C_HIGH_SPD;
636 i2c->fs_clock = HSI2C_FS_TX_CLOCK;
637 i2c->hs_clock = op_clock;
638 } else {
639 i2c->speed_mode = HSI2C_FAST_SPD;
640 i2c->fs_clock = op_clock;
641 }
642 }
643
644 strlcpy(i2c->adap.name, "exynos5-i2c", sizeof(i2c->adap.name));
645 i2c->adap.owner = THIS_MODULE;
646 i2c->adap.algo = &exynos5_i2c_algorithm;
647 i2c->adap.retries = 3;
648
649 i2c->dev = &pdev->dev;
650 i2c->clk = devm_clk_get(&pdev->dev, "hsi2c");
651 if (IS_ERR(i2c->clk)) {
652 dev_err(&pdev->dev, "cannot get clock\n");
653 return -ENOENT;
654 }
655
656 clk_prepare_enable(i2c->clk);
657
658 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
659 i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
660 if (IS_ERR(i2c->regs)) {
661 ret = PTR_ERR(i2c->regs);
662 goto err_clk;
663 }
664
665 i2c->adap.dev.of_node = np;
666 i2c->adap.algo_data = i2c;
667 i2c->adap.dev.parent = &pdev->dev;
668
669 /* Clear pending interrupts from u-boot or misc causes */
670 exynos5_i2c_clr_pend_irq(i2c);
671
672 spin_lock_init(&i2c->lock);
673 init_completion(&i2c->msg_complete);
674
675 i2c->irq = ret = platform_get_irq(pdev, 0);
676 if (ret <= 0) {
677 dev_err(&pdev->dev, "cannot find HS-I2C IRQ\n");
678 ret = -EINVAL;
679 goto err_clk;
680 }
681
682 ret = devm_request_irq(&pdev->dev, i2c->irq, exynos5_i2c_irq,
683 IRQF_NO_SUSPEND | IRQF_ONESHOT,
684 dev_name(&pdev->dev), i2c);
685
686 if (ret != 0) {
687 dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", i2c->irq);
688 goto err_clk;
689 }
690
691 ret = exynos5_hsi2c_clock_setup(i2c);
692 if (ret)
693 goto err_clk;
694
695 exynos5_i2c_init(i2c);
696
697 ret = i2c_add_adapter(&i2c->adap);
698 if (ret < 0) {
699 dev_err(&pdev->dev, "failed to add bus to i2c core\n");
700 goto err_clk;
701 }
702
703 platform_set_drvdata(pdev, i2c);
704
705 err_clk:
706 clk_disable_unprepare(i2c->clk);
707 return ret;
708}
709
710static int exynos5_i2c_remove(struct platform_device *pdev)
711{
712 struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
713
714 i2c_del_adapter(&i2c->adap);
715
716 return 0;
717}
718
719static int exynos5_i2c_suspend_noirq(struct device *dev)
720{
721 struct platform_device *pdev = to_platform_device(dev);
722 struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
723
724 i2c->suspended = 1;
725
726 return 0;
727}
728
729static int exynos5_i2c_resume_noirq(struct device *dev)
730{
731 struct platform_device *pdev = to_platform_device(dev);
732 struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
733 int ret = 0;
734
735 clk_prepare_enable(i2c->clk);
736
737 ret = exynos5_hsi2c_clock_setup(i2c);
738 if (ret) {
739 clk_disable_unprepare(i2c->clk);
740 return ret;
741 }
742
743 exynos5_i2c_init(i2c);
744 clk_disable_unprepare(i2c->clk);
745 i2c->suspended = 0;
746
747 return 0;
748}
749
750static SIMPLE_DEV_PM_OPS(exynos5_i2c_dev_pm_ops, exynos5_i2c_suspend_noirq,
751 exynos5_i2c_resume_noirq);
752
753static struct platform_driver exynos5_i2c_driver = {
754 .probe = exynos5_i2c_probe,
755 .remove = exynos5_i2c_remove,
756 .driver = {
757 .owner = THIS_MODULE,
758 .name = "exynos5-hsi2c",
759 .pm = &exynos5_i2c_dev_pm_ops,
760 .of_match_table = exynos5_i2c_match,
761 },
762};
763
764module_platform_driver(exynos5_i2c_driver);
765
766MODULE_DESCRIPTION("Exynos5 HS-I2C Bus driver");
767MODULE_AUTHOR("Naveen Krishna Chatradhi, <ch.naveen@samsung.com>");
768MODULE_AUTHOR("Taekgyun Ko, <taeggyun.ko@samsung.com>");
769MODULE_LICENSE("GPL v2");
diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c
index bfa02c6c2dda..d9f7e186a4c7 100644
--- a/drivers/i2c/busses/i2c-gpio.c
+++ b/drivers/i2c/busses/i2c-gpio.c
@@ -15,6 +15,7 @@
15#include <linux/slab.h> 15#include <linux/slab.h>
16#include <linux/platform_device.h> 16#include <linux/platform_device.h>
17#include <linux/gpio.h> 17#include <linux/gpio.h>
18#include <linux/of.h>
18#include <linux/of_gpio.h> 19#include <linux/of_gpio.h>
19 20
20struct i2c_gpio_private_data { 21struct i2c_gpio_private_data {
diff --git a/drivers/i2c/busses/i2c-i801.c b/drivers/i2c/busses/i2c-i801.c
index 4296d1721272..737e29866887 100644
--- a/drivers/i2c/busses/i2c-i801.c
+++ b/drivers/i2c/busses/i2c-i801.c
@@ -59,6 +59,7 @@
59 Wellsburg (PCH) MS 0x8d7e 32 hard yes yes yes 59 Wellsburg (PCH) MS 0x8d7e 32 hard yes yes yes
60 Wellsburg (PCH) MS 0x8d7f 32 hard yes yes yes 60 Wellsburg (PCH) MS 0x8d7f 32 hard yes yes yes
61 Coleto Creek (PCH) 0x23b0 32 hard yes yes yes 61 Coleto Creek (PCH) 0x23b0 32 hard yes yes yes
62 Wildcat Point-LP (PCH) 0x9ca2 32 hard yes yes yes
62 63
63 Features supported by this driver: 64 Features supported by this driver:
64 Software PEC no 65 Software PEC no
@@ -177,6 +178,7 @@
177#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1 0x8d7e 178#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1 0x8d7e
178#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f 179#define PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2 0x8d7f
179#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22 180#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22
181#define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS 0x9ca2
180 182
181struct i801_mux_config { 183struct i801_mux_config {
182 char *gpio_chip; 184 char *gpio_chip;
@@ -819,6 +821,7 @@ static DEFINE_PCI_DEVICE_TABLE(i801_ids) = {
819 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1) }, 821 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS1) },
820 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2) }, 822 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WELLSBURG_SMBUS_MS2) },
821 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS) }, 823 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_COLETOCREEK_SMBUS) },
824 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS) },
822 { 0, } 825 { 0, }
823}; 826};
824 827
diff --git a/drivers/i2c/busses/i2c-ibm_iic.c b/drivers/i2c/busses/i2c-ibm_iic.c
index ff3caa0c28cd..f7444100f397 100644
--- a/drivers/i2c/busses/i2c-ibm_iic.c
+++ b/drivers/i2c/busses/i2c-ibm_iic.c
@@ -41,6 +41,8 @@
41#include <asm/irq.h> 41#include <asm/irq.h>
42#include <linux/io.h> 42#include <linux/io.h>
43#include <linux/i2c.h> 43#include <linux/i2c.h>
44#include <linux/of_address.h>
45#include <linux/of_irq.h>
44#include <linux/of_platform.h> 46#include <linux/of_platform.h>
45 47
46#include "i2c-ibm_iic.h" 48#include "i2c-ibm_iic.h"
diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index ccf46656bdad..d0cfbb4cb964 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -312,7 +312,9 @@ static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
312 312
313 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); 313 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
314 314
315 clk_prepare_enable(i2c_imx->clk); 315 result = clk_prepare_enable(i2c_imx->clk);
316 if (result)
317 return result;
316 imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR); 318 imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR);
317 /* Enable I2C controller */ 319 /* Enable I2C controller */
318 imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR); 320 imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
@@ -365,7 +367,7 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
365 clk_disable_unprepare(i2c_imx->clk); 367 clk_disable_unprepare(i2c_imx->clk);
366} 368}
367 369
368static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx, 370static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
369 unsigned int rate) 371 unsigned int rate)
370{ 372{
371 struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div; 373 struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
@@ -589,7 +591,7 @@ static struct i2c_algorithm i2c_imx_algo = {
589 .functionality = i2c_imx_func, 591 .functionality = i2c_imx_func,
590}; 592};
591 593
592static int __init i2c_imx_probe(struct platform_device *pdev) 594static int i2c_imx_probe(struct platform_device *pdev)
593{ 595{
594 const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids, 596 const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids,
595 &pdev->dev); 597 &pdev->dev);
@@ -697,7 +699,7 @@ static int __init i2c_imx_probe(struct platform_device *pdev)
697 return 0; /* Return OK */ 699 return 0; /* Return OK */
698} 700}
699 701
700static int __exit i2c_imx_remove(struct platform_device *pdev) 702static int i2c_imx_remove(struct platform_device *pdev)
701{ 703{
702 struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev); 704 struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
703 705
@@ -715,7 +717,8 @@ static int __exit i2c_imx_remove(struct platform_device *pdev)
715} 717}
716 718
717static struct platform_driver i2c_imx_driver = { 719static struct platform_driver i2c_imx_driver = {
718 .remove = __exit_p(i2c_imx_remove), 720 .probe = i2c_imx_probe,
721 .remove = i2c_imx_remove,
719 .driver = { 722 .driver = {
720 .name = DRIVER_NAME, 723 .name = DRIVER_NAME,
721 .owner = THIS_MODULE, 724 .owner = THIS_MODULE,
@@ -726,7 +729,7 @@ static struct platform_driver i2c_imx_driver = {
726 729
727static int __init i2c_adap_imx_init(void) 730static int __init i2c_adap_imx_init(void)
728{ 731{
729 return platform_driver_probe(&i2c_imx_driver, i2c_imx_probe); 732 return platform_driver_register(&i2c_imx_driver);
730} 733}
731subsys_initcall(i2c_adap_imx_init); 734subsys_initcall(i2c_adap_imx_init);
732 735
diff --git a/drivers/i2c/busses/i2c-ismt.c b/drivers/i2c/busses/i2c-ismt.c
index 8ed79a086f85..0043ede234c2 100644
--- a/drivers/i2c/busses/i2c-ismt.c
+++ b/drivers/i2c/busses/i2c-ismt.c
@@ -393,6 +393,9 @@ static int ismt_access(struct i2c_adapter *adap, u16 addr,
393 393
394 desc = &priv->hw[priv->head]; 394 desc = &priv->hw[priv->head];
395 395
396 /* Initialize the DMA buffer */
397 memset(priv->dma_buffer, 0, sizeof(priv->dma_buffer));
398
396 /* Initialize the descriptor */ 399 /* Initialize the descriptor */
397 memset(desc, 0, sizeof(struct ismt_desc)); 400 memset(desc, 0, sizeof(struct ismt_desc));
398 desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, read_write); 401 desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, read_write);
@@ -538,7 +541,7 @@ static int ismt_access(struct i2c_adapter *adap, u16 addr,
538 desc->dptr_high = upper_32_bits(dma_addr); 541 desc->dptr_high = upper_32_bits(dma_addr);
539 } 542 }
540 543
541 INIT_COMPLETION(priv->cmp); 544 reinit_completion(&priv->cmp);
542 545
543 /* Add the descriptor */ 546 /* Add the descriptor */
544 ismt_submit_desc(priv); 547 ismt_submit_desc(priv);
diff --git a/drivers/i2c/busses/i2c-mpc.c b/drivers/i2c/busses/i2c-mpc.c
index b80c76888cab..b6a741caf4f6 100644
--- a/drivers/i2c/busses/i2c-mpc.c
+++ b/drivers/i2c/busses/i2c-mpc.c
@@ -17,6 +17,8 @@
17#include <linux/module.h> 17#include <linux/module.h>
18#include <linux/sched.h> 18#include <linux/sched.h>
19#include <linux/init.h> 19#include <linux/init.h>
20#include <linux/of_address.h>
21#include <linux/of_irq.h>
20#include <linux/of_platform.h> 22#include <linux/of_platform.h>
21#include <linux/slab.h> 23#include <linux/slab.h>
22 24
diff --git a/drivers/i2c/busses/i2c-mv64xxx.c b/drivers/i2c/busses/i2c-mv64xxx.c
index 7f3a47443494..8be7e42aa4de 100644
--- a/drivers/i2c/busses/i2c-mv64xxx.c
+++ b/drivers/i2c/busses/i2c-mv64xxx.c
@@ -234,9 +234,9 @@ static int mv64xxx_i2c_offload_msg(struct mv64xxx_i2c_data *drv_data)
234 ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_WR | 234 ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_WR |
235 (msg->len - 1) << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT; 235 (msg->len - 1) << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT;
236 236
237 writel_relaxed(data_reg_lo, 237 writel(data_reg_lo,
238 drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_LO); 238 drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_LO);
239 writel_relaxed(data_reg_hi, 239 writel(data_reg_hi,
240 drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_HI); 240 drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_HI);
241 241
242 } else { 242 } else {
@@ -697,6 +697,7 @@ static const struct of_device_id mv64xxx_i2c_of_match_table[] = {
697MODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table); 697MODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table);
698 698
699#ifdef CONFIG_OF 699#ifdef CONFIG_OF
700#ifdef CONFIG_HAVE_CLK
700static int 701static int
701mv64xxx_calc_freq(const int tclk, const int n, const int m) 702mv64xxx_calc_freq(const int tclk, const int n, const int m)
702{ 703{
@@ -726,16 +727,12 @@ mv64xxx_find_baud_factors(const u32 req_freq, const u32 tclk, u32 *best_n,
726 return false; 727 return false;
727 return true; 728 return true;
728} 729}
730#endif /* CONFIG_HAVE_CLK */
729 731
730static int 732static int
731mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data, 733mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
732 struct device *dev) 734 struct device *dev)
733{ 735{
734 const struct of_device_id *device;
735 struct device_node *np = dev->of_node;
736 u32 bus_freq, tclk;
737 int rc = 0;
738
739 /* CLK is mandatory when using DT to describe the i2c bus. We 736 /* CLK is mandatory when using DT to describe the i2c bus. We
740 * need to know tclk in order to calculate bus clock 737 * need to know tclk in order to calculate bus clock
741 * factors. 738 * factors.
@@ -744,6 +741,11 @@ mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
744 /* Have OF but no CLK */ 741 /* Have OF but no CLK */
745 return -ENODEV; 742 return -ENODEV;
746#else 743#else
744 const struct of_device_id *device;
745 struct device_node *np = dev->of_node;
746 u32 bus_freq, tclk;
747 int rc = 0;
748
747 if (IS_ERR(drv_data->clk)) { 749 if (IS_ERR(drv_data->clk)) {
748 rc = -ENODEV; 750 rc = -ENODEV;
749 goto out; 751 goto out;
@@ -909,7 +911,7 @@ static struct platform_driver mv64xxx_i2c_driver = {
909 .driver = { 911 .driver = {
910 .owner = THIS_MODULE, 912 .owner = THIS_MODULE,
911 .name = MV64XXX_I2C_CTLR_NAME, 913 .name = MV64XXX_I2C_CTLR_NAME,
912 .of_match_table = of_match_ptr(mv64xxx_i2c_of_match_table), 914 .of_match_table = mv64xxx_i2c_of_match_table,
913 }, 915 },
914}; 916};
915 917
diff --git a/drivers/i2c/busses/i2c-mxs.c b/drivers/i2c/busses/i2c-mxs.c
index f4a01675fa71..0cde4e6ab2b2 100644
--- a/drivers/i2c/busses/i2c-mxs.c
+++ b/drivers/i2c/busses/i2c-mxs.c
@@ -1,6 +1,7 @@
1/* 1/*
2 * Freescale MXS I2C bus driver 2 * Freescale MXS I2C bus driver
3 * 3 *
4 * Copyright (C) 2012-2013 Marek Vasut <marex@denx.de>
4 * Copyright (C) 2011-2012 Wolfram Sang, Pengutronix e.K. 5 * Copyright (C) 2011-2012 Wolfram Sang, Pengutronix e.K.
5 * 6 *
6 * based on a (non-working) driver which was: 7 * based on a (non-working) driver which was:
@@ -34,10 +35,12 @@
34 35
35#define MXS_I2C_CTRL0 (0x00) 36#define MXS_I2C_CTRL0 (0x00)
36#define MXS_I2C_CTRL0_SET (0x04) 37#define MXS_I2C_CTRL0_SET (0x04)
38#define MXS_I2C_CTRL0_CLR (0x08)
37 39
38#define MXS_I2C_CTRL0_SFTRST 0x80000000 40#define MXS_I2C_CTRL0_SFTRST 0x80000000
39#define MXS_I2C_CTRL0_RUN 0x20000000 41#define MXS_I2C_CTRL0_RUN 0x20000000
40#define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000 42#define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000
43#define MXS_I2C_CTRL0_PIO_MODE 0x01000000
41#define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000 44#define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000
42#define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000 45#define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000
43#define MXS_I2C_CTRL0_PRE_SEND_START 0x00080000 46#define MXS_I2C_CTRL0_PRE_SEND_START 0x00080000
@@ -64,13 +67,13 @@
64#define MXS_I2C_CTRL1_SLAVE_IRQ 0x01 67#define MXS_I2C_CTRL1_SLAVE_IRQ 0x01
65 68
66#define MXS_I2C_STAT (0x50) 69#define MXS_I2C_STAT (0x50)
70#define MXS_I2C_STAT_GOT_A_NAK 0x10000000
67#define MXS_I2C_STAT_BUS_BUSY 0x00000800 71#define MXS_I2C_STAT_BUS_BUSY 0x00000800
68#define MXS_I2C_STAT_CLK_GEN_BUSY 0x00000400 72#define MXS_I2C_STAT_CLK_GEN_BUSY 0x00000400
69 73
70#define MXS_I2C_DATA (0xa0) 74#define MXS_I2C_DATA(i2c) ((i2c->dev_type == MXS_I2C_V1) ? 0x60 : 0xa0)
71 75
72#define MXS_I2C_DEBUG0 (0xb0) 76#define MXS_I2C_DEBUG0_CLR(i2c) ((i2c->dev_type == MXS_I2C_V1) ? 0x78 : 0xb8)
73#define MXS_I2C_DEBUG0_CLR (0xb8)
74 77
75#define MXS_I2C_DEBUG0_DMAREQ 0x80000000 78#define MXS_I2C_DEBUG0_DMAREQ 0x80000000
76 79
@@ -95,10 +98,17 @@
95#define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \ 98#define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \
96 MXS_I2C_CTRL0_MASTER_MODE) 99 MXS_I2C_CTRL0_MASTER_MODE)
97 100
101enum mxs_i2c_devtype {
102 MXS_I2C_UNKNOWN = 0,
103 MXS_I2C_V1,
104 MXS_I2C_V2,
105};
106
98/** 107/**
99 * struct mxs_i2c_dev - per device, private MXS-I2C data 108 * struct mxs_i2c_dev - per device, private MXS-I2C data
100 * 109 *
101 * @dev: driver model device node 110 * @dev: driver model device node
111 * @dev_type: distinguish i.MX23/i.MX28 features
102 * @regs: IO registers pointer 112 * @regs: IO registers pointer
103 * @cmd_complete: completion object for transaction wait 113 * @cmd_complete: completion object for transaction wait
104 * @cmd_err: error code for last transaction 114 * @cmd_err: error code for last transaction
@@ -106,6 +116,7 @@
106 */ 116 */
107struct mxs_i2c_dev { 117struct mxs_i2c_dev {
108 struct device *dev; 118 struct device *dev;
119 enum mxs_i2c_devtype dev_type;
109 void __iomem *regs; 120 void __iomem *regs;
110 struct completion cmd_complete; 121 struct completion cmd_complete;
111 int cmd_err; 122 int cmd_err;
@@ -291,48 +302,11 @@ write_init_pio_fail:
291 return -EINVAL; 302 return -EINVAL;
292} 303}
293 304
294static int mxs_i2c_pio_wait_dmareq(struct mxs_i2c_dev *i2c) 305static int mxs_i2c_pio_wait_xfer_end(struct mxs_i2c_dev *i2c)
295{ 306{
296 unsigned long timeout = jiffies + msecs_to_jiffies(1000); 307 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
297 308
298 while (!(readl(i2c->regs + MXS_I2C_DEBUG0) & 309 while (readl(i2c->regs + MXS_I2C_CTRL0) & MXS_I2C_CTRL0_RUN) {
299 MXS_I2C_DEBUG0_DMAREQ)) {
300 if (time_after(jiffies, timeout))
301 return -ETIMEDOUT;
302 cond_resched();
303 }
304
305 return 0;
306}
307
308static int mxs_i2c_pio_wait_cplt(struct mxs_i2c_dev *i2c, int last)
309{
310 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
311
312 /*
313 * We do not use interrupts in the PIO mode. Due to the
314 * maximum transfer length being 8 bytes in PIO mode, the
315 * overhead of interrupt would be too large and this would
316 * neglect the gain from using the PIO mode.
317 */
318
319 while (!(readl(i2c->regs + MXS_I2C_CTRL1) &
320 MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ)) {
321 if (time_after(jiffies, timeout))
322 return -ETIMEDOUT;
323 cond_resched();
324 }
325
326 writel(MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ,
327 i2c->regs + MXS_I2C_CTRL1_CLR);
328
329 /*
330 * When ending a transfer with a stop, we have to wait for the bus to
331 * go idle before we report the transfer as completed. Otherwise the
332 * start of the next transfer may race with the end of the current one.
333 */
334 while (last && (readl(i2c->regs + MXS_I2C_STAT) &
335 (MXS_I2C_STAT_BUS_BUSY | MXS_I2C_STAT_CLK_GEN_BUSY))) {
336 if (time_after(jiffies, timeout)) 310 if (time_after(jiffies, timeout))
337 return -ETIMEDOUT; 311 return -ETIMEDOUT;
338 cond_resched(); 312 cond_resched();
@@ -370,106 +344,215 @@ static void mxs_i2c_pio_trigger_cmd(struct mxs_i2c_dev *i2c, u32 cmd)
370 writel(reg, i2c->regs + MXS_I2C_CTRL0); 344 writel(reg, i2c->regs + MXS_I2C_CTRL0);
371} 345}
372 346
347/*
348 * Start WRITE transaction on the I2C bus. By studying i.MX23 datasheet,
349 * CTRL0::PIO_MODE bit description clarifies the order in which the registers
350 * must be written during PIO mode operation. First, the CTRL0 register has
351 * to be programmed with all the necessary bits but the RUN bit. Then the
352 * payload has to be written into the DATA register. Finally, the transmission
353 * is executed by setting the RUN bit in CTRL0.
354 */
355static void mxs_i2c_pio_trigger_write_cmd(struct mxs_i2c_dev *i2c, u32 cmd,
356 u32 data)
357{
358 writel(cmd, i2c->regs + MXS_I2C_CTRL0);
359
360 if (i2c->dev_type == MXS_I2C_V1)
361 writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_SET);
362
363 writel(data, i2c->regs + MXS_I2C_DATA(i2c));
364 writel(MXS_I2C_CTRL0_RUN, i2c->regs + MXS_I2C_CTRL0_SET);
365}
366
373static int mxs_i2c_pio_setup_xfer(struct i2c_adapter *adap, 367static int mxs_i2c_pio_setup_xfer(struct i2c_adapter *adap,
374 struct i2c_msg *msg, uint32_t flags) 368 struct i2c_msg *msg, uint32_t flags)
375{ 369{
376 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap); 370 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
377 uint32_t addr_data = msg->addr << 1; 371 uint32_t addr_data = msg->addr << 1;
378 uint32_t data = 0; 372 uint32_t data = 0;
379 int i, shifts_left, ret; 373 int i, ret, xlen = 0, xmit = 0;
374 uint32_t start;
380 375
381 /* Mute IRQs coming from this block. */ 376 /* Mute IRQs coming from this block. */
382 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR); 377 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR);
383 378
379 /*
380 * MX23 idea:
381 * - Enable CTRL0::PIO_MODE (1 << 24)
382 * - Enable CTRL1::ACK_MODE (1 << 27)
383 *
384 * WARNING! The MX23 is broken in some way, even if it claims
385 * to support PIO, when we try to transfer any amount of data
386 * that is not aligned to 4 bytes, the DMA engine will have
387 * bits in DEBUG1::DMA_BYTES_ENABLES still set even after the
388 * transfer. This in turn will mess up the next transfer as
389 * the block it emit one byte write onto the bus terminated
390 * with a NAK+STOP. A possible workaround is to reset the IP
391 * block after every PIO transmission, which might just work.
392 *
393 * NOTE: The CTRL0::PIO_MODE description is important, since
394 * it outlines how the PIO mode is really supposed to work.
395 */
384 if (msg->flags & I2C_M_RD) { 396 if (msg->flags & I2C_M_RD) {
397 /*
398 * PIO READ transfer:
399 *
400 * This transfer MUST be limited to 4 bytes maximum. It is not
401 * possible to transfer more than four bytes via PIO, since we
402 * can not in any way make sure we can read the data from the
403 * DATA register fast enough. Besides, the RX FIFO is only four
404 * bytes deep, thus we can only really read up to four bytes at
405 * time. Finally, there is no bit indicating us that new data
406 * arrived at the FIFO and can thus be fetched from the DATA
407 * register.
408 */
409 BUG_ON(msg->len > 4);
410
385 addr_data |= I2C_SMBUS_READ; 411 addr_data |= I2C_SMBUS_READ;
386 412
387 /* SELECT command. */ 413 /* SELECT command. */
388 mxs_i2c_pio_trigger_cmd(i2c, MXS_CMD_I2C_SELECT); 414 mxs_i2c_pio_trigger_write_cmd(i2c, MXS_CMD_I2C_SELECT,
389 415 addr_data);
390 ret = mxs_i2c_pio_wait_dmareq(i2c);
391 if (ret)
392 return ret;
393
394 writel(addr_data, i2c->regs + MXS_I2C_DATA);
395 writel(MXS_I2C_DEBUG0_DMAREQ, i2c->regs + MXS_I2C_DEBUG0_CLR);
396 416
397 ret = mxs_i2c_pio_wait_cplt(i2c, 0); 417 ret = mxs_i2c_pio_wait_xfer_end(i2c);
398 if (ret) 418 if (ret) {
399 return ret; 419 dev_err(i2c->dev,
400 420 "PIO: Failed to send SELECT command!\n");
401 if (mxs_i2c_pio_check_error_state(i2c))
402 goto cleanup; 421 goto cleanup;
422 }
403 423
404 /* READ command. */ 424 /* READ command. */
405 mxs_i2c_pio_trigger_cmd(i2c, 425 mxs_i2c_pio_trigger_cmd(i2c,
406 MXS_CMD_I2C_READ | flags | 426 MXS_CMD_I2C_READ | flags |
407 MXS_I2C_CTRL0_XFER_COUNT(msg->len)); 427 MXS_I2C_CTRL0_XFER_COUNT(msg->len));
408 428
429 ret = mxs_i2c_pio_wait_xfer_end(i2c);
430 if (ret) {
431 dev_err(i2c->dev,
432 "PIO: Failed to send SELECT command!\n");
433 goto cleanup;
434 }
435
436 data = readl(i2c->regs + MXS_I2C_DATA(i2c));
409 for (i = 0; i < msg->len; i++) { 437 for (i = 0; i < msg->len; i++) {
410 if ((i & 3) == 0) {
411 ret = mxs_i2c_pio_wait_dmareq(i2c);
412 if (ret)
413 return ret;
414 data = readl(i2c->regs + MXS_I2C_DATA);
415 writel(MXS_I2C_DEBUG0_DMAREQ,
416 i2c->regs + MXS_I2C_DEBUG0_CLR);
417 }
418 msg->buf[i] = data & 0xff; 438 msg->buf[i] = data & 0xff;
419 data >>= 8; 439 data >>= 8;
420 } 440 }
421 } else { 441 } else {
442 /*
443 * PIO WRITE transfer:
444 *
445 * The code below implements clock stretching to circumvent
446 * the possibility of kernel not being able to supply data
447 * fast enough. It is possible to transfer arbitrary amount
448 * of data using PIO write.
449 */
422 addr_data |= I2C_SMBUS_WRITE; 450 addr_data |= I2C_SMBUS_WRITE;
423 451
424 /* WRITE command. */
425 mxs_i2c_pio_trigger_cmd(i2c,
426 MXS_CMD_I2C_WRITE | flags |
427 MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1));
428
429 /* 452 /*
430 * The LSB of data buffer is the first byte blasted across 453 * The LSB of data buffer is the first byte blasted across
431 * the bus. Higher order bytes follow. Thus the following 454 * the bus. Higher order bytes follow. Thus the following
432 * filling schematic. 455 * filling schematic.
433 */ 456 */
457
434 data = addr_data << 24; 458 data = addr_data << 24;
459
460 /* Start the transfer with START condition. */
461 start = MXS_I2C_CTRL0_PRE_SEND_START;
462
463 /* If the transfer is long, use clock stretching. */
464 if (msg->len > 3)
465 start |= MXS_I2C_CTRL0_RETAIN_CLOCK;
466
435 for (i = 0; i < msg->len; i++) { 467 for (i = 0; i < msg->len; i++) {
436 data >>= 8; 468 data >>= 8;
437 data |= (msg->buf[i] << 24); 469 data |= (msg->buf[i] << 24);
438 if ((i & 3) == 2) { 470
439 ret = mxs_i2c_pio_wait_dmareq(i2c); 471 xmit = 0;
440 if (ret) 472
441 return ret; 473 /* This is the last transfer of the message. */
442 writel(data, i2c->regs + MXS_I2C_DATA); 474 if (i + 1 == msg->len) {
443 writel(MXS_I2C_DEBUG0_DMAREQ, 475 /* Add optional STOP flag. */
444 i2c->regs + MXS_I2C_DEBUG0_CLR); 476 start |= flags;
477 /* Remove RETAIN_CLOCK bit. */
478 start &= ~MXS_I2C_CTRL0_RETAIN_CLOCK;
479 xmit = 1;
445 } 480 }
446 }
447 481
448 shifts_left = 24 - (i & 3) * 8; 482 /* Four bytes are ready in the "data" variable. */
449 if (shifts_left) { 483 if ((i & 3) == 2)
450 data >>= shifts_left; 484 xmit = 1;
451 ret = mxs_i2c_pio_wait_dmareq(i2c); 485
452 if (ret) 486 /* Nothing interesting happened, continue stuffing. */
453 return ret; 487 if (!xmit)
454 writel(data, i2c->regs + MXS_I2C_DATA); 488 continue;
489
490 /*
491 * Compute the size of the transfer and shift the
492 * data accordingly.
493 *
494 * i = (4k + 0) .... xlen = 2
495 * i = (4k + 1) .... xlen = 3
496 * i = (4k + 2) .... xlen = 4
497 * i = (4k + 3) .... xlen = 1
498 */
499
500 if ((i % 4) == 3)
501 xlen = 1;
502 else
503 xlen = (i % 4) + 2;
504
505 data >>= (4 - xlen) * 8;
506
507 dev_dbg(i2c->dev,
508 "PIO: len=%i pos=%i total=%i [W%s%s%s]\n",
509 xlen, i, msg->len,
510 start & MXS_I2C_CTRL0_PRE_SEND_START ? "S" : "",
511 start & MXS_I2C_CTRL0_POST_SEND_STOP ? "E" : "",
512 start & MXS_I2C_CTRL0_RETAIN_CLOCK ? "C" : "");
513
455 writel(MXS_I2C_DEBUG0_DMAREQ, 514 writel(MXS_I2C_DEBUG0_DMAREQ,
456 i2c->regs + MXS_I2C_DEBUG0_CLR); 515 i2c->regs + MXS_I2C_DEBUG0_CLR(i2c));
516
517 mxs_i2c_pio_trigger_write_cmd(i2c,
518 start | MXS_I2C_CTRL0_MASTER_MODE |
519 MXS_I2C_CTRL0_DIRECTION |
520 MXS_I2C_CTRL0_XFER_COUNT(xlen), data);
521
522 /* The START condition is sent only once. */
523 start &= ~MXS_I2C_CTRL0_PRE_SEND_START;
524
525 /* Wait for the end of the transfer. */
526 ret = mxs_i2c_pio_wait_xfer_end(i2c);
527 if (ret) {
528 dev_err(i2c->dev,
529 "PIO: Failed to finish WRITE cmd!\n");
530 break;
531 }
532
533 /* Check NAK here. */
534 ret = readl(i2c->regs + MXS_I2C_STAT) &
535 MXS_I2C_STAT_GOT_A_NAK;
536 if (ret) {
537 ret = -ENXIO;
538 goto cleanup;
539 }
457 } 540 }
458 } 541 }
459 542
460 ret = mxs_i2c_pio_wait_cplt(i2c, flags & MXS_I2C_CTRL0_POST_SEND_STOP);
461 if (ret)
462 return ret;
463
464 /* make sure we capture any occurred error into cmd_err */ 543 /* make sure we capture any occurred error into cmd_err */
465 mxs_i2c_pio_check_error_state(i2c); 544 ret = mxs_i2c_pio_check_error_state(i2c);
466 545
467cleanup: 546cleanup:
468 /* Clear any dangling IRQs and re-enable interrupts. */ 547 /* Clear any dangling IRQs and re-enable interrupts. */
469 writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR); 548 writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR);
470 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET); 549 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
471 550
472 return 0; 551 /* Clear the PIO_MODE on i.MX23 */
552 if (i2c->dev_type == MXS_I2C_V1)
553 writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_CLR);
554
555 return ret;
473} 556}
474 557
475/* 558/*
@@ -479,8 +562,9 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
479 int stop) 562 int stop)
480{ 563{
481 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap); 564 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
482 int ret, err; 565 int ret;
483 int flags; 566 int flags;
567 int use_pio = 0;
484 568
485 flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0; 569 flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0;
486 570
@@ -491,21 +575,23 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
491 return -EINVAL; 575 return -EINVAL;
492 576
493 /* 577 /*
494 * The current boundary to select between PIO/DMA transfer method 578 * The MX28 I2C IP block can only do PIO READ for transfer of to up
495 * is set to 8 bytes, transfers shorter than 8 bytes are transfered 579 * 4 bytes of length. The write transfer is not limited as it can use
496 * using PIO mode while longer transfers use DMA. The 8 byte border is 580 * clock stretching to avoid FIFO underruns.
497 * based on this empirical measurement and a lot of previous frobbing.
498 */ 581 */
582 if ((msg->flags & I2C_M_RD) && (msg->len <= 4))
583 use_pio = 1;
584 if (!(msg->flags & I2C_M_RD) && (msg->len < 7))
585 use_pio = 1;
586
499 i2c->cmd_err = 0; 587 i2c->cmd_err = 0;
500 if (0) { /* disable PIO mode until a proper fix is made */ 588 if (use_pio) {
501 ret = mxs_i2c_pio_setup_xfer(adap, msg, flags); 589 ret = mxs_i2c_pio_setup_xfer(adap, msg, flags);
502 if (ret) { 590 /* No need to reset the block if NAK was received. */
503 err = mxs_i2c_reset(i2c); 591 if (ret && (ret != -ENXIO))
504 if (err) 592 mxs_i2c_reset(i2c);
505 return err;
506 }
507 } else { 593 } else {
508 INIT_COMPLETION(i2c->cmd_complete); 594 reinit_completion(&i2c->cmd_complete);
509 ret = mxs_i2c_dma_setup_xfer(adap, msg, flags); 595 ret = mxs_i2c_dma_setup_xfer(adap, msg, flags);
510 if (ret) 596 if (ret)
511 return ret; 597 return ret;
@@ -514,9 +600,11 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
514 msecs_to_jiffies(1000)); 600 msecs_to_jiffies(1000));
515 if (ret == 0) 601 if (ret == 0)
516 goto timeout; 602 goto timeout;
603
604 ret = i2c->cmd_err;
517 } 605 }
518 606
519 if (i2c->cmd_err == -ENXIO) { 607 if (ret == -ENXIO) {
520 /* 608 /*
521 * If the transfer fails with a NAK from the slave the 609 * If the transfer fails with a NAK from the slave the
522 * controller halts until it gets told to return to idle state. 610 * controller halts until it gets told to return to idle state.
@@ -525,7 +613,19 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
525 i2c->regs + MXS_I2C_CTRL1_SET); 613 i2c->regs + MXS_I2C_CTRL1_SET);
526 } 614 }
527 615
528 ret = i2c->cmd_err; 616 /*
617 * WARNING!
618 * The i.MX23 is strange. After each and every operation, it's I2C IP
619 * block must be reset, otherwise the IP block will misbehave. This can
620 * be observed on the bus by the block sending out one single byte onto
621 * the bus. In case such an error happens, bit 27 will be set in the
622 * DEBUG0 register. This bit is not documented in the i.MX23 datasheet
623 * and is marked as "TBD" instead. To reset this bit to a correct state,
624 * reset the whole block. Since the block reset does not take long, do
625 * reset the block after every transfer to play safe.
626 */
627 if (i2c->dev_type == MXS_I2C_V1)
628 mxs_i2c_reset(i2c);
529 629
530 dev_dbg(i2c->dev, "Done with err=%d\n", ret); 630 dev_dbg(i2c->dev, "Done with err=%d\n", ret);
531 631
@@ -680,8 +780,28 @@ static int mxs_i2c_get_ofdata(struct mxs_i2c_dev *i2c)
680 return 0; 780 return 0;
681} 781}
682 782
783static struct platform_device_id mxs_i2c_devtype[] = {
784 {
785 .name = "imx23-i2c",
786 .driver_data = MXS_I2C_V1,
787 }, {
788 .name = "imx28-i2c",
789 .driver_data = MXS_I2C_V2,
790 }, { /* sentinel */ }
791};
792MODULE_DEVICE_TABLE(platform, mxs_i2c_devtype);
793
794static const struct of_device_id mxs_i2c_dt_ids[] = {
795 { .compatible = "fsl,imx23-i2c", .data = &mxs_i2c_devtype[0], },
796 { .compatible = "fsl,imx28-i2c", .data = &mxs_i2c_devtype[1], },
797 { /* sentinel */ }
798};
799MODULE_DEVICE_TABLE(of, mxs_i2c_dt_ids);
800
683static int mxs_i2c_probe(struct platform_device *pdev) 801static int mxs_i2c_probe(struct platform_device *pdev)
684{ 802{
803 const struct of_device_id *of_id =
804 of_match_device(mxs_i2c_dt_ids, &pdev->dev);
685 struct device *dev = &pdev->dev; 805 struct device *dev = &pdev->dev;
686 struct mxs_i2c_dev *i2c; 806 struct mxs_i2c_dev *i2c;
687 struct i2c_adapter *adap; 807 struct i2c_adapter *adap;
@@ -693,6 +813,11 @@ static int mxs_i2c_probe(struct platform_device *pdev)
693 if (!i2c) 813 if (!i2c)
694 return -ENOMEM; 814 return -ENOMEM;
695 815
816 if (of_id) {
817 const struct platform_device_id *device_id = of_id->data;
818 i2c->dev_type = device_id->driver_data;
819 }
820
696 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 821 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
697 irq = platform_get_irq(pdev, 0); 822 irq = platform_get_irq(pdev, 0);
698 823
@@ -768,24 +893,19 @@ static int mxs_i2c_remove(struct platform_device *pdev)
768 return 0; 893 return 0;
769} 894}
770 895
771static const struct of_device_id mxs_i2c_dt_ids[] = {
772 { .compatible = "fsl,imx28-i2c", },
773 { /* sentinel */ }
774};
775MODULE_DEVICE_TABLE(of, mxs_i2c_dt_ids);
776
777static struct platform_driver mxs_i2c_driver = { 896static struct platform_driver mxs_i2c_driver = {
778 .driver = { 897 .driver = {
779 .name = DRIVER_NAME, 898 .name = DRIVER_NAME,
780 .owner = THIS_MODULE, 899 .owner = THIS_MODULE,
781 .of_match_table = mxs_i2c_dt_ids, 900 .of_match_table = mxs_i2c_dt_ids,
782 }, 901 },
902 .probe = mxs_i2c_probe,
783 .remove = mxs_i2c_remove, 903 .remove = mxs_i2c_remove,
784}; 904};
785 905
786static int __init mxs_i2c_init(void) 906static int __init mxs_i2c_init(void)
787{ 907{
788 return platform_driver_probe(&mxs_i2c_driver, mxs_i2c_probe); 908 return platform_driver_register(&mxs_i2c_driver);
789} 909}
790subsys_initcall(mxs_i2c_init); 910subsys_initcall(mxs_i2c_init);
791 911
@@ -795,6 +915,7 @@ static void __exit mxs_i2c_exit(void)
795} 915}
796module_exit(mxs_i2c_exit); 916module_exit(mxs_i2c_exit);
797 917
918MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
798MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>"); 919MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
799MODULE_DESCRIPTION("MXS I2C Bus Driver"); 920MODULE_DESCRIPTION("MXS I2C Bus Driver");
800MODULE_LICENSE("GPL"); 921MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
index 6d8308d5dc4e..90dcc2eaac5f 100644
--- a/drivers/i2c/busses/i2c-omap.c
+++ b/drivers/i2c/busses/i2c-omap.c
@@ -266,13 +266,13 @@ static const u8 reg_map_ip_v2[] = {
266static inline void omap_i2c_write_reg(struct omap_i2c_dev *i2c_dev, 266static inline void omap_i2c_write_reg(struct omap_i2c_dev *i2c_dev,
267 int reg, u16 val) 267 int reg, u16 val)
268{ 268{
269 __raw_writew(val, i2c_dev->base + 269 writew_relaxed(val, i2c_dev->base +
270 (i2c_dev->regs[reg] << i2c_dev->reg_shift)); 270 (i2c_dev->regs[reg] << i2c_dev->reg_shift));
271} 271}
272 272
273static inline u16 omap_i2c_read_reg(struct omap_i2c_dev *i2c_dev, int reg) 273static inline u16 omap_i2c_read_reg(struct omap_i2c_dev *i2c_dev, int reg)
274{ 274{
275 return __raw_readw(i2c_dev->base + 275 return readw_relaxed(i2c_dev->base +
276 (i2c_dev->regs[reg] << i2c_dev->reg_shift)); 276 (i2c_dev->regs[reg] << i2c_dev->reg_shift));
277} 277}
278 278
@@ -543,7 +543,7 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
543 w |= OMAP_I2C_BUF_RXFIF_CLR | OMAP_I2C_BUF_TXFIF_CLR; 543 w |= OMAP_I2C_BUF_RXFIF_CLR | OMAP_I2C_BUF_TXFIF_CLR;
544 omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, w); 544 omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, w);
545 545
546 INIT_COMPLETION(dev->cmd_complete); 546 reinit_completion(&dev->cmd_complete);
547 dev->cmd_err = 0; 547 dev->cmd_err = 0;
548 548
549 w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT; 549 w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;
@@ -939,6 +939,9 @@ omap_i2c_isr_thread(int this_irq, void *dev_id)
939 /* 939 /*
940 * ProDB0017052: Clear ARDY bit twice 940 * ProDB0017052: Clear ARDY bit twice
941 */ 941 */
942 if (stat & OMAP_I2C_STAT_ARDY)
943 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_ARDY);
944
942 if (stat & (OMAP_I2C_STAT_ARDY | OMAP_I2C_STAT_NACK | 945 if (stat & (OMAP_I2C_STAT_ARDY | OMAP_I2C_STAT_NACK |
943 OMAP_I2C_STAT_AL)) { 946 OMAP_I2C_STAT_AL)) {
944 omap_i2c_ack_stat(dev, (OMAP_I2C_STAT_RRDY | 947 omap_i2c_ack_stat(dev, (OMAP_I2C_STAT_RRDY |
@@ -1034,6 +1037,20 @@ static const struct i2c_algorithm omap_i2c_algo = {
1034}; 1037};
1035 1038
1036#ifdef CONFIG_OF 1039#ifdef CONFIG_OF
1040static struct omap_i2c_bus_platform_data omap2420_pdata = {
1041 .rev = OMAP_I2C_IP_VERSION_1,
1042 .flags = OMAP_I2C_FLAG_NO_FIFO |
1043 OMAP_I2C_FLAG_SIMPLE_CLOCK |
1044 OMAP_I2C_FLAG_16BIT_DATA_REG |
1045 OMAP_I2C_FLAG_BUS_SHIFT_2,
1046};
1047
1048static struct omap_i2c_bus_platform_data omap2430_pdata = {
1049 .rev = OMAP_I2C_IP_VERSION_1,
1050 .flags = OMAP_I2C_FLAG_BUS_SHIFT_2 |
1051 OMAP_I2C_FLAG_FORCE_19200_INT_CLK,
1052};
1053
1037static struct omap_i2c_bus_platform_data omap3_pdata = { 1054static struct omap_i2c_bus_platform_data omap3_pdata = {
1038 .rev = OMAP_I2C_IP_VERSION_1, 1055 .rev = OMAP_I2C_IP_VERSION_1,
1039 .flags = OMAP_I2C_FLAG_BUS_SHIFT_2, 1056 .flags = OMAP_I2C_FLAG_BUS_SHIFT_2,
@@ -1052,6 +1069,14 @@ static const struct of_device_id omap_i2c_of_match[] = {
1052 .compatible = "ti,omap3-i2c", 1069 .compatible = "ti,omap3-i2c",
1053 .data = &omap3_pdata, 1070 .data = &omap3_pdata,
1054 }, 1071 },
1072 {
1073 .compatible = "ti,omap2430-i2c",
1074 .data = &omap2430_pdata,
1075 },
1076 {
1077 .compatible = "ti,omap2420-i2c",
1078 .data = &omap2420_pdata,
1079 },
1055 { }, 1080 { },
1056}; 1081};
1057MODULE_DEVICE_TABLE(of, omap_i2c_of_match); 1082MODULE_DEVICE_TABLE(of, omap_i2c_of_match);
@@ -1137,9 +1162,9 @@ omap_i2c_probe(struct platform_device *pdev)
1137 * Read the Rev hi bit-[15:14] ie scheme this is 1 indicates ver2. 1162 * Read the Rev hi bit-[15:14] ie scheme this is 1 indicates ver2.
1138 * On omap1/3/2 Offset 4 is IE Reg the bit [15:14] is 0 at reset. 1163 * On omap1/3/2 Offset 4 is IE Reg the bit [15:14] is 0 at reset.
1139 * Also since the omap_i2c_read_reg uses reg_map_ip_* a 1164 * Also since the omap_i2c_read_reg uses reg_map_ip_* a
1140 * raw_readw is done. 1165 * readw_relaxed is done.
1141 */ 1166 */
1142 rev = __raw_readw(dev->base + 0x04); 1167 rev = readw_relaxed(dev->base + 0x04);
1143 1168
1144 dev->scheme = OMAP_I2C_SCHEME(rev); 1169 dev->scheme = OMAP_I2C_SCHEME(rev);
1145 switch (dev->scheme) { 1170 switch (dev->scheme) {
diff --git a/drivers/i2c/busses/i2c-pnx.c b/drivers/i2c/busses/i2c-pnx.c
index 1a9ea25f2314..c9a352f0a9a5 100644
--- a/drivers/i2c/busses/i2c-pnx.c
+++ b/drivers/i2c/busses/i2c-pnx.c
@@ -23,6 +23,7 @@
23#include <linux/err.h> 23#include <linux/err.h>
24#include <linux/clk.h> 24#include <linux/clk.h>
25#include <linux/slab.h> 25#include <linux/slab.h>
26#include <linux/of.h>
26 27
27#define I2C_PNX_TIMEOUT_DEFAULT 10 /* msec */ 28#define I2C_PNX_TIMEOUT_DEFAULT 10 /* msec */
28#define I2C_PNX_SPEED_KHZ_DEFAULT 100 29#define I2C_PNX_SPEED_KHZ_DEFAULT 100
diff --git a/drivers/i2c/busses/i2c-powermac.c b/drivers/i2c/busses/i2c-powermac.c
index 37e8cfad625b..8c87f4a9793b 100644
--- a/drivers/i2c/busses/i2c-powermac.c
+++ b/drivers/i2c/busses/i2c-powermac.c
@@ -27,6 +27,7 @@
27#include <linux/init.h> 27#include <linux/init.h>
28#include <linux/device.h> 28#include <linux/device.h>
29#include <linux/platform_device.h> 29#include <linux/platform_device.h>
30#include <linux/of_irq.h>
30#include <asm/prom.h> 31#include <asm/prom.h>
31#include <asm/pmac_low_i2c.h> 32#include <asm/pmac_low_i2c.h>
32 33
diff --git a/drivers/i2c/busses/i2c-rcar.c b/drivers/i2c/busses/i2c-rcar.c
index d2fe11da5e82..2c2fd7c2b116 100644
--- a/drivers/i2c/busses/i2c-rcar.c
+++ b/drivers/i2c/busses/i2c-rcar.c
@@ -33,6 +33,7 @@
33#include <linux/i2c/i2c-rcar.h> 33#include <linux/i2c/i2c-rcar.h>
34#include <linux/kernel.h> 34#include <linux/kernel.h>
35#include <linux/module.h> 35#include <linux/module.h>
36#include <linux/of_device.h>
36#include <linux/platform_device.h> 37#include <linux/platform_device.h>
37#include <linux/pm_runtime.h> 38#include <linux/pm_runtime.h>
38#include <linux/slab.h> 39#include <linux/slab.h>
@@ -102,8 +103,8 @@ enum {
102#define ID_NACK (1 << 4) 103#define ID_NACK (1 << 4)
103 104
104enum rcar_i2c_type { 105enum rcar_i2c_type {
105 I2C_RCAR_H1, 106 I2C_RCAR_GEN1,
106 I2C_RCAR_H2, 107 I2C_RCAR_GEN2,
107}; 108};
108 109
109struct rcar_i2c_priv { 110struct rcar_i2c_priv {
@@ -226,22 +227,23 @@ static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
226 u32 bus_speed, 227 u32 bus_speed,
227 struct device *dev) 228 struct device *dev)
228{ 229{
229 struct clk *clkp = clk_get(NULL, "peripheral_clk"); 230 struct clk *clkp = clk_get(dev, NULL);
230 u32 scgd, cdf; 231 u32 scgd, cdf;
231 u32 round, ick; 232 u32 round, ick;
232 u32 scl; 233 u32 scl;
233 u32 cdf_width; 234 u32 cdf_width;
235 unsigned long rate;
234 236
235 if (!clkp) { 237 if (IS_ERR(clkp)) {
236 dev_err(dev, "there is no peripheral_clk\n"); 238 dev_err(dev, "couldn't get clock\n");
237 return -EIO; 239 return PTR_ERR(clkp);
238 } 240 }
239 241
240 switch (priv->devtype) { 242 switch (priv->devtype) {
241 case I2C_RCAR_H1: 243 case I2C_RCAR_GEN1:
242 cdf_width = 2; 244 cdf_width = 2;
243 break; 245 break;
244 case I2C_RCAR_H2: 246 case I2C_RCAR_GEN2:
245 cdf_width = 3; 247 cdf_width = 3;
246 break; 248 break;
247 default: 249 default:
@@ -264,15 +266,14 @@ static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
264 * clkp : peripheral_clk 266 * clkp : peripheral_clk
265 * F[] : integer up-valuation 267 * F[] : integer up-valuation
266 */ 268 */
267 for (cdf = 0; cdf < (1 << cdf_width); cdf++) { 269 rate = clk_get_rate(clkp);
268 ick = clk_get_rate(clkp) / (1 + cdf); 270 cdf = rate / 20000000;
269 if (ick < 20000000) 271 if (cdf >= 1 << cdf_width) {
270 goto ick_find; 272 dev_err(dev, "Input clock %lu too high\n", rate);
273 return -EIO;
271 } 274 }
272 dev_err(dev, "there is no best CDF\n"); 275 ick = rate / (cdf + 1);
273 return -EIO;
274 276
275ick_find:
276 /* 277 /*
277 * it is impossible to calculate large scale 278 * it is impossible to calculate large scale
278 * number on u32. separate it 279 * number on u32. separate it
@@ -290,6 +291,12 @@ ick_find:
290 * 291 *
291 * Calculation result (= SCL) should be less than 292 * Calculation result (= SCL) should be less than
292 * bus_speed for hardware safety 293 * bus_speed for hardware safety
294 *
295 * We could use something along the lines of
296 * div = ick / (bus_speed + 1) + 1;
297 * scgd = (div - 20 - round + 7) / 8;
298 * scl = ick / (20 + (scgd * 8) + round);
299 * (not fully verified) but that would get pretty involved
293 */ 300 */
294 for (scgd = 0; scgd < 0x40; scgd++) { 301 for (scgd = 0; scgd < 0x40; scgd++) {
295 scl = ick / (20 + (scgd * 8) + round); 302 scl = ick / (20 + (scgd * 8) + round);
@@ -306,7 +313,7 @@ scgd_find:
306 /* 313 /*
307 * keep icccr value 314 * keep icccr value
308 */ 315 */
309 priv->icccr = (scgd << (cdf_width) | cdf); 316 priv->icccr = scgd << cdf_width | cdf;
310 317
311 return 0; 318 return 0;
312} 319}
@@ -632,6 +639,15 @@ static const struct i2c_algorithm rcar_i2c_algo = {
632 .functionality = rcar_i2c_func, 639 .functionality = rcar_i2c_func,
633}; 640};
634 641
642static const struct of_device_id rcar_i2c_dt_ids[] = {
643 { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
644 { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
645 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
646 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
647 {},
648};
649MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
650
635static int rcar_i2c_probe(struct platform_device *pdev) 651static int rcar_i2c_probe(struct platform_device *pdev)
636{ 652{
637 struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev); 653 struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
@@ -649,10 +665,15 @@ static int rcar_i2c_probe(struct platform_device *pdev)
649 } 665 }
650 666
651 bus_speed = 100000; /* default 100 kHz */ 667 bus_speed = 100000; /* default 100 kHz */
652 if (pdata && pdata->bus_speed) 668 ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
669 if (ret < 0 && pdata && pdata->bus_speed)
653 bus_speed = pdata->bus_speed; 670 bus_speed = pdata->bus_speed;
654 671
655 priv->devtype = platform_get_device_id(pdev)->driver_data; 672 if (pdev->dev.of_node)
673 priv->devtype = (long)of_match_device(rcar_i2c_dt_ids,
674 dev)->data;
675 else
676 priv->devtype = platform_get_device_id(pdev)->driver_data;
656 677
657 ret = rcar_i2c_clock_calculate(priv, bus_speed, dev); 678 ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
658 if (ret < 0) 679 if (ret < 0)
@@ -673,6 +694,7 @@ static int rcar_i2c_probe(struct platform_device *pdev)
673 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; 694 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
674 adap->retries = 3; 695 adap->retries = 3;
675 adap->dev.parent = dev; 696 adap->dev.parent = dev;
697 adap->dev.of_node = dev->of_node;
676 i2c_set_adapdata(adap, priv); 698 i2c_set_adapdata(adap, priv);
677 strlcpy(adap->name, pdev->name, sizeof(adap->name)); 699 strlcpy(adap->name, pdev->name, sizeof(adap->name));
678 700
@@ -709,9 +731,9 @@ static int rcar_i2c_remove(struct platform_device *pdev)
709} 731}
710 732
711static struct platform_device_id rcar_i2c_id_table[] = { 733static struct platform_device_id rcar_i2c_id_table[] = {
712 { "i2c-rcar", I2C_RCAR_H1 }, 734 { "i2c-rcar", I2C_RCAR_GEN1 },
713 { "i2c-rcar_h1", I2C_RCAR_H1 }, 735 { "i2c-rcar_gen1", I2C_RCAR_GEN1 },
714 { "i2c-rcar_h2", I2C_RCAR_H2 }, 736 { "i2c-rcar_gen2", I2C_RCAR_GEN2 },
715 {}, 737 {},
716}; 738};
717MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table); 739MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
@@ -720,6 +742,7 @@ static struct platform_driver rcar_i2c_driver = {
720 .driver = { 742 .driver = {
721 .name = "i2c-rcar", 743 .name = "i2c-rcar",
722 .owner = THIS_MODULE, 744 .owner = THIS_MODULE,
745 .of_match_table = rcar_i2c_dt_ids,
723 }, 746 },
724 .probe = rcar_i2c_probe, 747 .probe = rcar_i2c_probe,
725 .remove = rcar_i2c_remove, 748 .remove = rcar_i2c_remove,
diff --git a/drivers/i2c/busses/i2c-s3c2410.c b/drivers/i2c/busses/i2c-s3c2410.c
index 3535f3c0f7b4..bf8fb94ebc5d 100644
--- a/drivers/i2c/busses/i2c-s3c2410.c
+++ b/drivers/i2c/busses/i2c-s3c2410.c
@@ -36,6 +36,7 @@
36#include <linux/cpufreq.h> 36#include <linux/cpufreq.h>
37#include <linux/slab.h> 37#include <linux/slab.h>
38#include <linux/io.h> 38#include <linux/io.h>
39#include <linux/of.h>
39#include <linux/of_gpio.h> 40#include <linux/of_gpio.h>
40#include <linux/pinctrl/consumer.h> 41#include <linux/pinctrl/consumer.h>
41 42
@@ -1178,8 +1179,6 @@ static int s3c24xx_i2c_remove(struct platform_device *pdev)
1178 1179
1179 i2c_del_adapter(&i2c->adap); 1180 i2c_del_adapter(&i2c->adap);
1180 1181
1181 clk_disable_unprepare(i2c->clk);
1182
1183 if (pdev->dev.of_node && IS_ERR(i2c->pctrl)) 1182 if (pdev->dev.of_node && IS_ERR(i2c->pctrl))
1184 s3c24xx_i2c_dt_gpio_free(i2c); 1183 s3c24xx_i2c_dt_gpio_free(i2c);
1185 1184
diff --git a/drivers/i2c/busses/i2c-scmi.c b/drivers/i2c/busses/i2c-scmi.c
index c447e8d40b78..599235514138 100644
--- a/drivers/i2c/busses/i2c-scmi.c
+++ b/drivers/i2c/busses/i2c-scmi.c
@@ -223,7 +223,7 @@ acpi_smbus_cmi_access(struct i2c_adapter *adap, u16 addr, unsigned short flags,
223 goto out; 223 goto out;
224 224
225 obj = pkg->package.elements + 1; 225 obj = pkg->package.elements + 1;
226 if (obj == NULL || obj->type != ACPI_TYPE_INTEGER) { 226 if (obj->type != ACPI_TYPE_INTEGER) {
227 ACPI_ERROR((AE_INFO, "Invalid argument type")); 227 ACPI_ERROR((AE_INFO, "Invalid argument type"));
228 result = -EIO; 228 result = -EIO;
229 goto out; 229 goto out;
@@ -235,7 +235,7 @@ acpi_smbus_cmi_access(struct i2c_adapter *adap, u16 addr, unsigned short flags,
235 case I2C_SMBUS_BYTE: 235 case I2C_SMBUS_BYTE:
236 case I2C_SMBUS_BYTE_DATA: 236 case I2C_SMBUS_BYTE_DATA:
237 case I2C_SMBUS_WORD_DATA: 237 case I2C_SMBUS_WORD_DATA:
238 if (obj == NULL || obj->type != ACPI_TYPE_INTEGER) { 238 if (obj->type != ACPI_TYPE_INTEGER) {
239 ACPI_ERROR((AE_INFO, "Invalid argument type")); 239 ACPI_ERROR((AE_INFO, "Invalid argument type"));
240 result = -EIO; 240 result = -EIO;
241 goto out; 241 goto out;
@@ -246,7 +246,7 @@ acpi_smbus_cmi_access(struct i2c_adapter *adap, u16 addr, unsigned short flags,
246 data->byte = obj->integer.value; 246 data->byte = obj->integer.value;
247 break; 247 break;
248 case I2C_SMBUS_BLOCK_DATA: 248 case I2C_SMBUS_BLOCK_DATA:
249 if (obj == NULL || obj->type != ACPI_TYPE_BUFFER) { 249 if (obj->type != ACPI_TYPE_BUFFER) {
250 ACPI_ERROR((AE_INFO, "Invalid argument type")); 250 ACPI_ERROR((AE_INFO, "Invalid argument type"));
251 result = -EIO; 251 result = -EIO;
252 goto out; 252 goto out;
diff --git a/drivers/i2c/busses/i2c-sh_mobile.c b/drivers/i2c/busses/i2c-sh_mobile.c
index 55110ddbed1f..1d79585ba4b3 100644
--- a/drivers/i2c/busses/i2c-sh_mobile.c
+++ b/drivers/i2c/busses/i2c-sh_mobile.c
@@ -235,7 +235,7 @@ static void sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
235 int offset; 235 int offset;
236 236
237 /* Get clock rate after clock is enabled */ 237 /* Get clock rate after clock is enabled */
238 clk_enable(pd->clk); 238 clk_prepare_enable(pd->clk);
239 i2c_clk_khz = clk_get_rate(pd->clk) / 1000; 239 i2c_clk_khz = clk_get_rate(pd->clk) / 1000;
240 i2c_clk_khz /= pd->clks_per_count; 240 i2c_clk_khz /= pd->clks_per_count;
241 241
@@ -270,14 +270,14 @@ static void sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
270 pd->icic &= ~ICIC_ICCHB8; 270 pd->icic &= ~ICIC_ICCHB8;
271 271
272out: 272out:
273 clk_disable(pd->clk); 273 clk_disable_unprepare(pd->clk);
274} 274}
275 275
276static void activate_ch(struct sh_mobile_i2c_data *pd) 276static void activate_ch(struct sh_mobile_i2c_data *pd)
277{ 277{
278 /* Wake up device and enable clock */ 278 /* Wake up device and enable clock */
279 pm_runtime_get_sync(pd->dev); 279 pm_runtime_get_sync(pd->dev);
280 clk_enable(pd->clk); 280 clk_prepare_enable(pd->clk);
281 281
282 /* Enable channel and configure rx ack */ 282 /* Enable channel and configure rx ack */
283 iic_set_clr(pd, ICCR, ICCR_ICE, 0); 283 iic_set_clr(pd, ICCR, ICCR_ICE, 0);
@@ -300,7 +300,7 @@ static void deactivate_ch(struct sh_mobile_i2c_data *pd)
300 iic_set_clr(pd, ICCR, 0, ICCR_ICE); 300 iic_set_clr(pd, ICCR, 0, ICCR_ICE);
301 301
302 /* Disable clock and mark device as idle */ 302 /* Disable clock and mark device as idle */
303 clk_disable(pd->clk); 303 clk_disable_unprepare(pd->clk);
304 pm_runtime_put_sync(pd->dev); 304 pm_runtime_put_sync(pd->dev);
305} 305}
306 306
diff --git a/drivers/i2c/busses/i2c-st.c b/drivers/i2c/busses/i2c-st.c
new file mode 100644
index 000000000000..9cf715d69551
--- /dev/null
+++ b/drivers/i2c/busses/i2c-st.c
@@ -0,0 +1,872 @@
1/*
2 * Copyright (C) 2013 STMicroelectronics
3 *
4 * I2C master mode controller driver, used in STMicroelectronics devices.
5 *
6 * Author: Maxime Coquelin <maxime.coquelin@st.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2, as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/platform_device.h>
15#include <linux/i2c.h>
16#include <linux/clk.h>
17#include <linux/io.h>
18#include <linux/delay.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/of.h>
22#include <linux/of_address.h>
23#include <linux/of_irq.h>
24
25/* SSC registers */
26#define SSC_BRG 0x000
27#define SSC_TBUF 0x004
28#define SSC_RBUF 0x008
29#define SSC_CTL 0x00C
30#define SSC_IEN 0x010
31#define SSC_STA 0x014
32#define SSC_I2C 0x018
33#define SSC_SLAD 0x01C
34#define SSC_REP_START_HOLD 0x020
35#define SSC_START_HOLD 0x024
36#define SSC_REP_START_SETUP 0x028
37#define SSC_DATA_SETUP 0x02C
38#define SSC_STOP_SETUP 0x030
39#define SSC_BUS_FREE 0x034
40#define SSC_TX_FSTAT 0x038
41#define SSC_RX_FSTAT 0x03C
42#define SSC_PRE_SCALER_BRG 0x040
43#define SSC_CLR 0x080
44#define SSC_NOISE_SUPP_WIDTH 0x100
45#define SSC_PRSCALER 0x104
46#define SSC_NOISE_SUPP_WIDTH_DATAOUT 0x108
47#define SSC_PRSCALER_DATAOUT 0x10c
48
49/* SSC Control */
50#define SSC_CTL_DATA_WIDTH_9 0x8
51#define SSC_CTL_DATA_WIDTH_MSK 0xf
52#define SSC_CTL_BM 0xf
53#define SSC_CTL_HB BIT(4)
54#define SSC_CTL_PH BIT(5)
55#define SSC_CTL_PO BIT(6)
56#define SSC_CTL_SR BIT(7)
57#define SSC_CTL_MS BIT(8)
58#define SSC_CTL_EN BIT(9)
59#define SSC_CTL_LPB BIT(10)
60#define SSC_CTL_EN_TX_FIFO BIT(11)
61#define SSC_CTL_EN_RX_FIFO BIT(12)
62#define SSC_CTL_EN_CLST_RX BIT(13)
63
64/* SSC Interrupt Enable */
65#define SSC_IEN_RIEN BIT(0)
66#define SSC_IEN_TIEN BIT(1)
67#define SSC_IEN_TEEN BIT(2)
68#define SSC_IEN_REEN BIT(3)
69#define SSC_IEN_PEEN BIT(4)
70#define SSC_IEN_AASEN BIT(6)
71#define SSC_IEN_STOPEN BIT(7)
72#define SSC_IEN_ARBLEN BIT(8)
73#define SSC_IEN_NACKEN BIT(10)
74#define SSC_IEN_REPSTRTEN BIT(11)
75#define SSC_IEN_TX_FIFO_HALF BIT(12)
76#define SSC_IEN_RX_FIFO_HALF_FULL BIT(14)
77
78/* SSC Status */
79#define SSC_STA_RIR BIT(0)
80#define SSC_STA_TIR BIT(1)
81#define SSC_STA_TE BIT(2)
82#define SSC_STA_RE BIT(3)
83#define SSC_STA_PE BIT(4)
84#define SSC_STA_CLST BIT(5)
85#define SSC_STA_AAS BIT(6)
86#define SSC_STA_STOP BIT(7)
87#define SSC_STA_ARBL BIT(8)
88#define SSC_STA_BUSY BIT(9)
89#define SSC_STA_NACK BIT(10)
90#define SSC_STA_REPSTRT BIT(11)
91#define SSC_STA_TX_FIFO_HALF BIT(12)
92#define SSC_STA_TX_FIFO_FULL BIT(13)
93#define SSC_STA_RX_FIFO_HALF BIT(14)
94
95/* SSC I2C Control */
96#define SSC_I2C_I2CM BIT(0)
97#define SSC_I2C_STRTG BIT(1)
98#define SSC_I2C_STOPG BIT(2)
99#define SSC_I2C_ACKG BIT(3)
100#define SSC_I2C_AD10 BIT(4)
101#define SSC_I2C_TXENB BIT(5)
102#define SSC_I2C_REPSTRTG BIT(11)
103#define SSC_I2C_SLAVE_DISABLE BIT(12)
104
105/* SSC Tx FIFO Status */
106#define SSC_TX_FSTAT_STATUS 0x07
107
108/* SSC Rx FIFO Status */
109#define SSC_RX_FSTAT_STATUS 0x07
110
111/* SSC Clear bit operation */
112#define SSC_CLR_SSCAAS BIT(6)
113#define SSC_CLR_SSCSTOP BIT(7)
114#define SSC_CLR_SSCARBL BIT(8)
115#define SSC_CLR_NACK BIT(10)
116#define SSC_CLR_REPSTRT BIT(11)
117
118/* SSC Clock Prescaler */
119#define SSC_PRSC_VALUE 0x0f
120
121
122#define SSC_TXFIFO_SIZE 0x8
123#define SSC_RXFIFO_SIZE 0x8
124
125enum st_i2c_mode {
126 I2C_MODE_STANDARD,
127 I2C_MODE_FAST,
128 I2C_MODE_END,
129};
130
131/**
132 * struct st_i2c_timings - per-Mode tuning parameters
133 * @rate: I2C bus rate
134 * @rep_start_hold: I2C repeated start hold time requirement
135 * @rep_start_setup: I2C repeated start set up time requirement
136 * @start_hold: I2C start hold time requirement
137 * @data_setup_time: I2C data set up time requirement
138 * @stop_setup_time: I2C stop set up time requirement
139 * @bus_free_time: I2C bus free time requirement
140 * @sda_pulse_min_limit: I2C SDA pulse mini width limit
141 */
142struct st_i2c_timings {
143 u32 rate;
144 u32 rep_start_hold;
145 u32 rep_start_setup;
146 u32 start_hold;
147 u32 data_setup_time;
148 u32 stop_setup_time;
149 u32 bus_free_time;
150 u32 sda_pulse_min_limit;
151};
152
153/**
154 * struct st_i2c_client - client specific data
155 * @addr: 8-bit slave addr, including r/w bit
156 * @count: number of bytes to be transfered
157 * @xfered: number of bytes already transferred
158 * @buf: data buffer
159 * @result: result of the transfer
160 * @stop: last I2C msg to be sent, i.e. STOP to be generated
161 */
162struct st_i2c_client {
163 u8 addr;
164 u32 count;
165 u32 xfered;
166 u8 *buf;
167 int result;
168 bool stop;
169};
170
171/**
172 * struct st_i2c_dev - private data of the controller
173 * @adap: I2C adapter for this controller
174 * @dev: device for this controller
175 * @base: virtual memory area
176 * @complete: completion of I2C message
177 * @irq: interrupt line for th controller
178 * @clk: hw ssc block clock
179 * @mode: I2C mode of the controller. Standard or Fast only supported
180 * @scl_min_width_us: SCL line minimum pulse width in us
181 * @sda_min_width_us: SDA line minimum pulse width in us
182 * @client: I2C transfert information
183 * @busy: I2C transfer on-going
184 */
185struct st_i2c_dev {
186 struct i2c_adapter adap;
187 struct device *dev;
188 void __iomem *base;
189 struct completion complete;
190 int irq;
191 struct clk *clk;
192 int mode;
193 u32 scl_min_width_us;
194 u32 sda_min_width_us;
195 struct st_i2c_client client;
196 bool busy;
197};
198
199static inline void st_i2c_set_bits(void __iomem *reg, u32 mask)
200{
201 writel_relaxed(readl_relaxed(reg) | mask, reg);
202}
203
204static inline void st_i2c_clr_bits(void __iomem *reg, u32 mask)
205{
206 writel_relaxed(readl_relaxed(reg) & ~mask, reg);
207}
208
209/* From I2C Specifications v0.5 */
210static struct st_i2c_timings i2c_timings[] = {
211 [I2C_MODE_STANDARD] = {
212 .rate = 100000,
213 .rep_start_hold = 4000,
214 .rep_start_setup = 4700,
215 .start_hold = 4000,
216 .data_setup_time = 250,
217 .stop_setup_time = 4000,
218 .bus_free_time = 4700,
219 },
220 [I2C_MODE_FAST] = {
221 .rate = 400000,
222 .rep_start_hold = 600,
223 .rep_start_setup = 600,
224 .start_hold = 600,
225 .data_setup_time = 100,
226 .stop_setup_time = 600,
227 .bus_free_time = 1300,
228 },
229};
230
231static void st_i2c_flush_rx_fifo(struct st_i2c_dev *i2c_dev)
232{
233 int count, i;
234
235 /*
236 * Counter only counts up to 7 but fifo size is 8...
237 * When fifo is full, counter is 0 and RIR bit of status register is
238 * set
239 */
240 if (readl_relaxed(i2c_dev->base + SSC_STA) & SSC_STA_RIR)
241 count = SSC_RXFIFO_SIZE;
242 else
243 count = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT) &
244 SSC_RX_FSTAT_STATUS;
245
246 for (i = 0; i < count; i++)
247 readl_relaxed(i2c_dev->base + SSC_RBUF);
248}
249
250static void st_i2c_soft_reset(struct st_i2c_dev *i2c_dev)
251{
252 /*
253 * FIFO needs to be emptied before reseting the IP,
254 * else the controller raises a BUSY error.
255 */
256 st_i2c_flush_rx_fifo(i2c_dev);
257
258 st_i2c_set_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR);
259 st_i2c_clr_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR);
260}
261
262/**
263 * st_i2c_hw_config() - Prepare SSC block, calculate and apply tuning timings
264 * @i2c_dev: Controller's private data
265 */
266static void st_i2c_hw_config(struct st_i2c_dev *i2c_dev)
267{
268 unsigned long rate;
269 u32 val, ns_per_clk;
270 struct st_i2c_timings *t = &i2c_timings[i2c_dev->mode];
271
272 st_i2c_soft_reset(i2c_dev);
273
274 val = SSC_CLR_REPSTRT | SSC_CLR_NACK | SSC_CLR_SSCARBL |
275 SSC_CLR_SSCAAS | SSC_CLR_SSCSTOP;
276 writel_relaxed(val, i2c_dev->base + SSC_CLR);
277
278 /* SSC Control register setup */
279 val = SSC_CTL_PO | SSC_CTL_PH | SSC_CTL_HB | SSC_CTL_DATA_WIDTH_9;
280 writel_relaxed(val, i2c_dev->base + SSC_CTL);
281
282 rate = clk_get_rate(i2c_dev->clk);
283 ns_per_clk = 1000000000 / rate;
284
285 /* Baudrate */
286 val = rate / (2 * t->rate);
287 writel_relaxed(val, i2c_dev->base + SSC_BRG);
288
289 /* Pre-scaler baudrate */
290 writel_relaxed(1, i2c_dev->base + SSC_PRE_SCALER_BRG);
291
292 /* Enable I2C mode */
293 writel_relaxed(SSC_I2C_I2CM, i2c_dev->base + SSC_I2C);
294
295 /* Repeated start hold time */
296 val = t->rep_start_hold / ns_per_clk;
297 writel_relaxed(val, i2c_dev->base + SSC_REP_START_HOLD);
298
299 /* Repeated start set up time */
300 val = t->rep_start_setup / ns_per_clk;
301 writel_relaxed(val, i2c_dev->base + SSC_REP_START_SETUP);
302
303 /* Start hold time */
304 val = t->start_hold / ns_per_clk;
305 writel_relaxed(val, i2c_dev->base + SSC_START_HOLD);
306
307 /* Data set up time */
308 val = t->data_setup_time / ns_per_clk;
309 writel_relaxed(val, i2c_dev->base + SSC_DATA_SETUP);
310
311 /* Stop set up time */
312 val = t->stop_setup_time / ns_per_clk;
313 writel_relaxed(val, i2c_dev->base + SSC_STOP_SETUP);
314
315 /* Bus free time */
316 val = t->bus_free_time / ns_per_clk;
317 writel_relaxed(val, i2c_dev->base + SSC_BUS_FREE);
318
319 /* Prescalers set up */
320 val = rate / 10000000;
321 writel_relaxed(val, i2c_dev->base + SSC_PRSCALER);
322 writel_relaxed(val, i2c_dev->base + SSC_PRSCALER_DATAOUT);
323
324 /* Noise suppression witdh */
325 val = i2c_dev->scl_min_width_us * rate / 100000000;
326 writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH);
327
328 /* Noise suppression max output data delay width */
329 val = i2c_dev->sda_min_width_us * rate / 100000000;
330 writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH_DATAOUT);
331}
332
333static int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev)
334{
335 u32 sta;
336 int i;
337
338 for (i = 0; i < 10; i++) {
339 sta = readl_relaxed(i2c_dev->base + SSC_STA);
340 if (!(sta & SSC_STA_BUSY))
341 return 0;
342
343 usleep_range(2000, 4000);
344 }
345
346 dev_err(i2c_dev->dev, "bus not free (status = 0x%08x)\n", sta);
347
348 return -EBUSY;
349}
350
351/**
352 * st_i2c_write_tx_fifo() - Write a byte in the Tx FIFO
353 * @i2c_dev: Controller's private data
354 * @byte: Data to write in the Tx FIFO
355 */
356static inline void st_i2c_write_tx_fifo(struct st_i2c_dev *i2c_dev, u8 byte)
357{
358 u16 tbuf = byte << 1;
359
360 writel_relaxed(tbuf | 1, i2c_dev->base + SSC_TBUF);
361}
362
363/**
364 * st_i2c_wr_fill_tx_fifo() - Fill the Tx FIFO in write mode
365 * @i2c_dev: Controller's private data
366 *
367 * This functions fills the Tx FIFO with I2C transfert buffer when
368 * in write mode.
369 */
370static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev)
371{
372 struct st_i2c_client *c = &i2c_dev->client;
373 u32 tx_fstat, sta;
374 int i;
375
376 sta = readl_relaxed(i2c_dev->base + SSC_STA);
377 if (sta & SSC_STA_TX_FIFO_FULL)
378 return;
379
380 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
381 tx_fstat &= SSC_TX_FSTAT_STATUS;
382
383 if (c->count < (SSC_TXFIFO_SIZE - tx_fstat))
384 i = c->count;
385 else
386 i = SSC_TXFIFO_SIZE - tx_fstat;
387
388 for (; i > 0; i--, c->count--, c->buf++)
389 st_i2c_write_tx_fifo(i2c_dev, *c->buf);
390}
391
392/**
393 * st_i2c_rd_fill_tx_fifo() - Fill the Tx FIFO in read mode
394 * @i2c_dev: Controller's private data
395 *
396 * This functions fills the Tx FIFO with fixed pattern when
397 * in read mode to trigger clock.
398 */
399static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max)
400{
401 struct st_i2c_client *c = &i2c_dev->client;
402 u32 tx_fstat, sta;
403 int i;
404
405 sta = readl_relaxed(i2c_dev->base + SSC_STA);
406 if (sta & SSC_STA_TX_FIFO_FULL)
407 return;
408
409 tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
410 tx_fstat &= SSC_TX_FSTAT_STATUS;
411
412 if (max < (SSC_TXFIFO_SIZE - tx_fstat))
413 i = max;
414 else
415 i = SSC_TXFIFO_SIZE - tx_fstat;
416
417 for (; i > 0; i--, c->xfered++)
418 st_i2c_write_tx_fifo(i2c_dev, 0xff);
419}
420
421static void st_i2c_read_rx_fifo(struct st_i2c_dev *i2c_dev)
422{
423 struct st_i2c_client *c = &i2c_dev->client;
424 u32 i, sta;
425 u16 rbuf;
426
427 sta = readl_relaxed(i2c_dev->base + SSC_STA);
428 if (sta & SSC_STA_RIR) {
429 i = SSC_RXFIFO_SIZE;
430 } else {
431 i = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT);
432 i &= SSC_RX_FSTAT_STATUS;
433 }
434
435 for (; (i > 0) && (c->count > 0); i--, c->count--) {
436 rbuf = readl_relaxed(i2c_dev->base + SSC_RBUF) >> 1;
437 *c->buf++ = (u8)rbuf & 0xff;
438 }
439
440 if (i) {
441 dev_err(i2c_dev->dev, "Unexpected %d bytes in rx fifo\n", i);
442 st_i2c_flush_rx_fifo(i2c_dev);
443 }
444}
445
446/**
447 * st_i2c_terminate_xfer() - Send either STOP or REPSTART condition
448 * @i2c_dev: Controller's private data
449 */
450static void st_i2c_terminate_xfer(struct st_i2c_dev *i2c_dev)
451{
452 struct st_i2c_client *c = &i2c_dev->client;
453
454 st_i2c_clr_bits(i2c_dev->base + SSC_IEN, SSC_IEN_TEEN);
455 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG);
456
457 if (c->stop) {
458 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_STOPEN);
459 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
460 } else {
461 st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_REPSTRTEN);
462 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_REPSTRTG);
463 }
464}
465
466/**
467 * st_i2c_handle_write() - Handle FIFO empty interrupt in case of write
468 * @i2c_dev: Controller's private data
469 */
470static void st_i2c_handle_write(struct st_i2c_dev *i2c_dev)
471{
472 struct st_i2c_client *c = &i2c_dev->client;
473
474 st_i2c_flush_rx_fifo(i2c_dev);
475
476 if (!c->count)
477 /* End of xfer, send stop or repstart */
478 st_i2c_terminate_xfer(i2c_dev);
479 else
480 st_i2c_wr_fill_tx_fifo(i2c_dev);
481}
482
483/**
484 * st_i2c_handle_write() - Handle FIFO enmpty interrupt in case of read
485 * @i2c_dev: Controller's private data
486 */
487static void st_i2c_handle_read(struct st_i2c_dev *i2c_dev)
488{
489 struct st_i2c_client *c = &i2c_dev->client;
490 u32 ien;
491
492 /* Trash the address read back */
493 if (!c->xfered) {
494 readl_relaxed(i2c_dev->base + SSC_RBUF);
495 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_TXENB);
496 } else {
497 st_i2c_read_rx_fifo(i2c_dev);
498 }
499
500 if (!c->count) {
501 /* End of xfer, send stop or repstart */
502 st_i2c_terminate_xfer(i2c_dev);
503 } else if (c->count == 1) {
504 /* Penultimate byte to xfer, disable ACK gen. */
505 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_ACKG);
506
507 /* Last received byte is to be handled by NACK interrupt */
508 ien = SSC_IEN_NACKEN | SSC_IEN_ARBLEN;
509 writel_relaxed(ien, i2c_dev->base + SSC_IEN);
510
511 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count);
512 } else {
513 st_i2c_rd_fill_tx_fifo(i2c_dev, c->count - 1);
514 }
515}
516
517/**
518 * st_i2c_isr() - Interrupt routine
519 * @irq: interrupt number
520 * @data: Controller's private data
521 */
522static irqreturn_t st_i2c_isr_thread(int irq, void *data)
523{
524 struct st_i2c_dev *i2c_dev = data;
525 struct st_i2c_client *c = &i2c_dev->client;
526 u32 sta, ien;
527 int it;
528
529 ien = readl_relaxed(i2c_dev->base + SSC_IEN);
530 sta = readl_relaxed(i2c_dev->base + SSC_STA);
531
532 /* Use __fls() to check error bits first */
533 it = __fls(sta & ien);
534 if (it < 0) {
535 dev_dbg(i2c_dev->dev, "spurious it (sta=0x%04x, ien=0x%04x)\n",
536 sta, ien);
537 return IRQ_NONE;
538 }
539
540 switch (1 << it) {
541 case SSC_STA_TE:
542 if (c->addr & I2C_M_RD)
543 st_i2c_handle_read(i2c_dev);
544 else
545 st_i2c_handle_write(i2c_dev);
546 break;
547
548 case SSC_STA_STOP:
549 case SSC_STA_REPSTRT:
550 writel_relaxed(0, i2c_dev->base + SSC_IEN);
551 complete(&i2c_dev->complete);
552 break;
553
554 case SSC_STA_NACK:
555 writel_relaxed(SSC_CLR_NACK, i2c_dev->base + SSC_CLR);
556
557 /* Last received byte handled by NACK interrupt */
558 if ((c->addr & I2C_M_RD) && (c->count == 1) && (c->xfered)) {
559 st_i2c_handle_read(i2c_dev);
560 break;
561 }
562
563 it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN;
564 writel_relaxed(it, i2c_dev->base + SSC_IEN);
565
566 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
567 c->result = -EIO;
568 break;
569
570 case SSC_STA_ARBL:
571 writel_relaxed(SSC_CLR_SSCARBL, i2c_dev->base + SSC_CLR);
572
573 it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN;
574 writel_relaxed(it, i2c_dev->base + SSC_IEN);
575
576 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
577 c->result = -EIO;
578 break;
579
580 default:
581 dev_err(i2c_dev->dev,
582 "it %d unhandled (sta=0x%04x)\n", it, sta);
583 }
584
585 /*
586 * Read IEN register to ensure interrupt mask write is effective
587 * before re-enabling interrupt at GIC level, and thus avoid spurious
588 * interrupts.
589 */
590 readl(i2c_dev->base + SSC_IEN);
591
592 return IRQ_HANDLED;
593}
594
595/**
596 * st_i2c_xfer_msg() - Transfer a single I2C message
597 * @i2c_dev: Controller's private data
598 * @msg: I2C message to transfer
599 * @is_first: first message of the sequence
600 * @is_last: last message of the sequence
601 */
602static int st_i2c_xfer_msg(struct st_i2c_dev *i2c_dev, struct i2c_msg *msg,
603 bool is_first, bool is_last)
604{
605 struct st_i2c_client *c = &i2c_dev->client;
606 u32 ctl, i2c, it;
607 unsigned long timeout;
608 int ret;
609
610 c->addr = (u8)(msg->addr << 1);
611 c->addr |= (msg->flags & I2C_M_RD);
612 c->buf = msg->buf;
613 c->count = msg->len;
614 c->xfered = 0;
615 c->result = 0;
616 c->stop = is_last;
617
618 reinit_completion(&i2c_dev->complete);
619
620 ctl = SSC_CTL_EN | SSC_CTL_MS | SSC_CTL_EN_RX_FIFO | SSC_CTL_EN_TX_FIFO;
621 st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl);
622
623 i2c = SSC_I2C_TXENB;
624 if (c->addr & I2C_M_RD)
625 i2c |= SSC_I2C_ACKG;
626 st_i2c_set_bits(i2c_dev->base + SSC_I2C, i2c);
627
628 /* Write slave address */
629 st_i2c_write_tx_fifo(i2c_dev, c->addr);
630
631 /* Pre-fill Tx fifo with data in case of write */
632 if (!(c->addr & I2C_M_RD))
633 st_i2c_wr_fill_tx_fifo(i2c_dev);
634
635 it = SSC_IEN_NACKEN | SSC_IEN_TEEN | SSC_IEN_ARBLEN;
636 writel_relaxed(it, i2c_dev->base + SSC_IEN);
637
638 if (is_first) {
639 ret = st_i2c_wait_free_bus(i2c_dev);
640 if (ret)
641 return ret;
642
643 st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG);
644 }
645
646 timeout = wait_for_completion_timeout(&i2c_dev->complete,
647 i2c_dev->adap.timeout);
648 ret = c->result;
649
650 if (!timeout) {
651 dev_err(i2c_dev->dev, "Write to slave 0x%x timed out\n",
652 c->addr);
653 ret = -ETIMEDOUT;
654 }
655
656 i2c = SSC_I2C_STOPG | SSC_I2C_REPSTRTG;
657 st_i2c_clr_bits(i2c_dev->base + SSC_I2C, i2c);
658
659 writel_relaxed(SSC_CLR_SSCSTOP | SSC_CLR_REPSTRT,
660 i2c_dev->base + SSC_CLR);
661
662 return ret;
663}
664
665/**
666 * st_i2c_xfer() - Transfer a single I2C message
667 * @i2c_adap: Adapter pointer to the controller
668 * @msgs: Pointer to data to be written.
669 * @num: Number of messages to be executed
670 */
671static int st_i2c_xfer(struct i2c_adapter *i2c_adap,
672 struct i2c_msg msgs[], int num)
673{
674 struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
675 int ret, i;
676
677 i2c_dev->busy = true;
678
679 ret = clk_prepare_enable(i2c_dev->clk);
680 if (ret) {
681 dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n");
682 return ret;
683 }
684
685 pinctrl_pm_select_default_state(i2c_dev->dev);
686
687 st_i2c_hw_config(i2c_dev);
688
689 for (i = 0; (i < num) && !ret; i++)
690 ret = st_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, i == num - 1);
691
692 pinctrl_pm_select_idle_state(i2c_dev->dev);
693
694 clk_disable_unprepare(i2c_dev->clk);
695
696 i2c_dev->busy = false;
697
698 return (ret < 0) ? ret : i;
699}
700
701#ifdef CONFIG_PM_SLEEP
702static int st_i2c_suspend(struct device *dev)
703{
704 struct platform_device *pdev =
705 container_of(dev, struct platform_device, dev);
706 struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
707
708 if (i2c_dev->busy)
709 return -EBUSY;
710
711 pinctrl_pm_select_sleep_state(dev);
712
713 return 0;
714}
715
716static int st_i2c_resume(struct device *dev)
717{
718 pinctrl_pm_select_default_state(dev);
719 /* Go in idle state if available */
720 pinctrl_pm_select_idle_state(dev);
721
722 return 0;
723}
724
725static SIMPLE_DEV_PM_OPS(st_i2c_pm, st_i2c_suspend, st_i2c_resume);
726#define ST_I2C_PM (&st_i2c_pm)
727#else
728#define ST_I2C_PM NULL
729#endif
730
731static u32 st_i2c_func(struct i2c_adapter *adap)
732{
733 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
734}
735
736static struct i2c_algorithm st_i2c_algo = {
737 .master_xfer = st_i2c_xfer,
738 .functionality = st_i2c_func,
739};
740
741static int st_i2c_of_get_deglitch(struct device_node *np,
742 struct st_i2c_dev *i2c_dev)
743{
744 int ret;
745
746 ret = of_property_read_u32(np, "st,i2c-min-scl-pulse-width-us",
747 &i2c_dev->scl_min_width_us);
748 if ((ret == -ENODATA) || (ret == -EOVERFLOW)) {
749 dev_err(i2c_dev->dev, "st,i2c-min-scl-pulse-width-us invalid\n");
750 return ret;
751 }
752
753 ret = of_property_read_u32(np, "st,i2c-min-sda-pulse-width-us",
754 &i2c_dev->sda_min_width_us);
755 if ((ret == -ENODATA) || (ret == -EOVERFLOW)) {
756 dev_err(i2c_dev->dev, "st,i2c-min-sda-pulse-width-us invalid\n");
757 return ret;
758 }
759
760 return 0;
761}
762
763static int st_i2c_probe(struct platform_device *pdev)
764{
765 struct device_node *np = pdev->dev.of_node;
766 struct st_i2c_dev *i2c_dev;
767 struct resource *res;
768 u32 clk_rate;
769 struct i2c_adapter *adap;
770 int ret;
771
772 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
773 if (!i2c_dev)
774 return -ENOMEM;
775
776 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
777 i2c_dev->base = devm_ioremap_resource(&pdev->dev, res);
778 if (IS_ERR(i2c_dev->base))
779 return PTR_ERR(i2c_dev->base);
780
781 i2c_dev->irq = irq_of_parse_and_map(np, 0);
782 if (!i2c_dev->irq) {
783 dev_err(&pdev->dev, "IRQ missing or invalid\n");
784 return -EINVAL;
785 }
786
787 i2c_dev->clk = of_clk_get_by_name(np, "ssc");
788 if (IS_ERR(i2c_dev->clk)) {
789 dev_err(&pdev->dev, "Unable to request clock\n");
790 return PTR_ERR(i2c_dev->clk);
791 }
792
793 i2c_dev->mode = I2C_MODE_STANDARD;
794 ret = of_property_read_u32(np, "clock-frequency", &clk_rate);
795 if ((!ret) && (clk_rate == 400000))
796 i2c_dev->mode = I2C_MODE_FAST;
797
798 i2c_dev->dev = &pdev->dev;
799
800 ret = devm_request_threaded_irq(&pdev->dev, i2c_dev->irq,
801 NULL, st_i2c_isr_thread,
802 IRQF_ONESHOT, pdev->name, i2c_dev);
803 if (ret) {
804 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
805 return ret;
806 }
807
808 pinctrl_pm_select_default_state(i2c_dev->dev);
809 /* In case idle state available, select it */
810 pinctrl_pm_select_idle_state(i2c_dev->dev);
811
812 ret = st_i2c_of_get_deglitch(np, i2c_dev);
813 if (ret)
814 return ret;
815
816 adap = &i2c_dev->adap;
817 i2c_set_adapdata(adap, i2c_dev);
818 snprintf(adap->name, sizeof(adap->name), "ST I2C(0x%x)", res->start);
819 adap->owner = THIS_MODULE;
820 adap->timeout = 2 * HZ;
821 adap->retries = 0;
822 adap->algo = &st_i2c_algo;
823 adap->dev.parent = &pdev->dev;
824 adap->dev.of_node = pdev->dev.of_node;
825
826 init_completion(&i2c_dev->complete);
827
828 ret = i2c_add_adapter(adap);
829 if (ret) {
830 dev_err(&pdev->dev, "Failed to add adapter\n");
831 return ret;
832 }
833
834 platform_set_drvdata(pdev, i2c_dev);
835
836 dev_info(i2c_dev->dev, "%s initialized\n", adap->name);
837
838 return 0;
839}
840
841static int st_i2c_remove(struct platform_device *pdev)
842{
843 struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
844
845 i2c_del_adapter(&i2c_dev->adap);
846
847 return 0;
848}
849
850static struct of_device_id st_i2c_match[] = {
851 { .compatible = "st,comms-ssc-i2c", },
852 { .compatible = "st,comms-ssc4-i2c", },
853 {},
854};
855MODULE_DEVICE_TABLE(of, st_i2c_match);
856
857static struct platform_driver st_i2c_driver = {
858 .driver = {
859 .name = "st-i2c",
860 .owner = THIS_MODULE,
861 .of_match_table = st_i2c_match,
862 .pm = ST_I2C_PM,
863 },
864 .probe = st_i2c_probe,
865 .remove = st_i2c_remove,
866};
867
868module_platform_driver(st_i2c_driver);
869
870MODULE_AUTHOR("Maxime Coquelin <maxime.coquelin@st.com>");
871MODULE_DESCRIPTION("STMicroelectronics I2C driver");
872MODULE_LICENSE("GPL v2");
diff --git a/drivers/i2c/busses/i2c-stu300.c b/drivers/i2c/busses/i2c-stu300.c
index f8f6f2e552db..04a17b9b38bb 100644
--- a/drivers/i2c/busses/i2c-stu300.c
+++ b/drivers/i2c/busses/i2c-stu300.c
@@ -859,8 +859,7 @@ static const struct i2c_algorithm stu300_algo = {
859 .functionality = stu300_func, 859 .functionality = stu300_func,
860}; 860};
861 861
862static int __init 862static int stu300_probe(struct platform_device *pdev)
863stu300_probe(struct platform_device *pdev)
864{ 863{
865 struct stu300_dev *dev; 864 struct stu300_dev *dev;
866 struct i2c_adapter *adap; 865 struct i2c_adapter *adap;
@@ -966,8 +965,7 @@ static SIMPLE_DEV_PM_OPS(stu300_pm, stu300_suspend, stu300_resume);
966#define STU300_I2C_PM NULL 965#define STU300_I2C_PM NULL
967#endif 966#endif
968 967
969static int __exit 968static int stu300_remove(struct platform_device *pdev)
970stu300_remove(struct platform_device *pdev)
971{ 969{
972 struct stu300_dev *dev = platform_get_drvdata(pdev); 970 struct stu300_dev *dev = platform_get_drvdata(pdev);
973 971
@@ -989,13 +987,14 @@ static struct platform_driver stu300_i2c_driver = {
989 .pm = STU300_I2C_PM, 987 .pm = STU300_I2C_PM,
990 .of_match_table = stu300_dt_match, 988 .of_match_table = stu300_dt_match,
991 }, 989 },
992 .remove = __exit_p(stu300_remove), 990 .probe = stu300_probe,
991 .remove = stu300_remove,
993 992
994}; 993};
995 994
996static int __init stu300_init(void) 995static int __init stu300_init(void)
997{ 996{
998 return platform_driver_probe(&stu300_i2c_driver, stu300_probe); 997 return platform_driver_register(&stu300_i2c_driver);
999} 998}
1000 999
1001static void __exit stu300_exit(void) 1000static void __exit stu300_exit(void)
diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
index c457cb447c66..e661edee4d0c 100644
--- a/drivers/i2c/busses/i2c-tegra.c
+++ b/drivers/i2c/busses/i2c-tegra.c
@@ -544,7 +544,7 @@ static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
544 i2c_dev->msg_buf_remaining = msg->len; 544 i2c_dev->msg_buf_remaining = msg->len;
545 i2c_dev->msg_err = I2C_ERR_NONE; 545 i2c_dev->msg_err = I2C_ERR_NONE;
546 i2c_dev->msg_read = (msg->flags & I2C_M_RD); 546 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
547 INIT_COMPLETION(i2c_dev->msg_complete); 547 reinit_completion(&i2c_dev->msg_complete);
548 548
549 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) | 549 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
550 PACKET_HEADER0_PROTOCOL_I2C | 550 PACKET_HEADER0_PROTOCOL_I2C |
diff --git a/drivers/i2c/busses/i2c-wmt.c b/drivers/i2c/busses/i2c-wmt.c
index c65da3d913a0..2c8a3e4f9008 100644
--- a/drivers/i2c/busses/i2c-wmt.c
+++ b/drivers/i2c/busses/i2c-wmt.c
@@ -158,7 +158,7 @@ static int wmt_i2c_write(struct i2c_adapter *adap, struct i2c_msg *pmsg,
158 writew(val, i2c_dev->base + REG_CR); 158 writew(val, i2c_dev->base + REG_CR);
159 } 159 }
160 160
161 INIT_COMPLETION(i2c_dev->complete); 161 reinit_completion(&i2c_dev->complete);
162 162
163 if (i2c_dev->mode == I2C_MODE_STANDARD) 163 if (i2c_dev->mode == I2C_MODE_STANDARD)
164 tcr_val = TCR_STANDARD_MODE; 164 tcr_val = TCR_STANDARD_MODE;
@@ -247,7 +247,7 @@ static int wmt_i2c_read(struct i2c_adapter *adap, struct i2c_msg *pmsg,
247 writew(val, i2c_dev->base + REG_CR); 247 writew(val, i2c_dev->base + REG_CR);
248 } 248 }
249 249
250 INIT_COMPLETION(i2c_dev->complete); 250 reinit_completion(&i2c_dev->complete);
251 251
252 if (i2c_dev->mode == I2C_MODE_STANDARD) 252 if (i2c_dev->mode == I2C_MODE_STANDARD)
253 tcr_val = TCR_STANDARD_MODE; 253 tcr_val = TCR_STANDARD_MODE;
@@ -349,6 +349,7 @@ static int wmt_i2c_reset_hardware(struct wmt_i2c_dev *i2c_dev)
349 err = clk_set_rate(i2c_dev->clk, 20000000); 349 err = clk_set_rate(i2c_dev->clk, 20000000);
350 if (err) { 350 if (err) {
351 dev_err(i2c_dev->dev, "failed to set clock = 20Mhz\n"); 351 dev_err(i2c_dev->dev, "failed to set clock = 20Mhz\n");
352 clk_disable_unprepare(i2c_dev->clk);
352 return err; 353 return err;
353 } 354 }
354 355
diff --git a/drivers/i2c/busses/i2c-xiic.c b/drivers/i2c/busses/i2c-xiic.c
index 4c8b368d463b..fc2716afdfd9 100644
--- a/drivers/i2c/busses/i2c-xiic.c
+++ b/drivers/i2c/busses/i2c-xiic.c
@@ -40,6 +40,7 @@
40#include <linux/i2c-xiic.h> 40#include <linux/i2c-xiic.h>
41#include <linux/io.h> 41#include <linux/io.h>
42#include <linux/slab.h> 42#include <linux/slab.h>
43#include <linux/of.h>
43 44
44#define DRIVER_NAME "xiic-i2c" 45#define DRIVER_NAME "xiic-i2c"
45 46
@@ -702,7 +703,7 @@ static int xiic_i2c_probe(struct platform_device *pdev)
702 if (irq < 0) 703 if (irq < 0)
703 goto resource_missing; 704 goto resource_missing;
704 705
705 pdata = (struct xiic_i2c_platform_data *)dev_get_platdata(&pdev->dev); 706 pdata = dev_get_platdata(&pdev->dev);
706 707
707 i2c = kzalloc(sizeof(*i2c), GFP_KERNEL); 708 i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
708 if (!i2c) 709 if (!i2c)
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 29d3f045a2bf..d74c0b34248e 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -248,16 +248,17 @@ static int i2c_device_probe(struct device *dev)
248 driver = to_i2c_driver(dev->driver); 248 driver = to_i2c_driver(dev->driver);
249 if (!driver->probe || !driver->id_table) 249 if (!driver->probe || !driver->id_table)
250 return -ENODEV; 250 return -ENODEV;
251 client->driver = driver; 251
252 if (!device_can_wakeup(&client->dev)) 252 if (!device_can_wakeup(&client->dev))
253 device_init_wakeup(&client->dev, 253 device_init_wakeup(&client->dev,
254 client->flags & I2C_CLIENT_WAKE); 254 client->flags & I2C_CLIENT_WAKE);
255 dev_dbg(dev, "probe\n"); 255 dev_dbg(dev, "probe\n");
256 256
257 acpi_dev_pm_attach(&client->dev, true);
257 status = driver->probe(client, i2c_match_id(driver->id_table, client)); 258 status = driver->probe(client, i2c_match_id(driver->id_table, client));
258 if (status) { 259 if (status) {
259 client->driver = NULL;
260 i2c_set_clientdata(client, NULL); 260 i2c_set_clientdata(client, NULL);
261 acpi_dev_pm_detach(&client->dev, true);
261 } 262 }
262 return status; 263 return status;
263} 264}
@@ -279,10 +280,9 @@ static int i2c_device_remove(struct device *dev)
279 dev->driver = NULL; 280 dev->driver = NULL;
280 status = 0; 281 status = 0;
281 } 282 }
282 if (status == 0) { 283 if (status == 0)
283 client->driver = NULL;
284 i2c_set_clientdata(client, NULL); 284 i2c_set_clientdata(client, NULL);
285 } 285 acpi_dev_pm_detach(&client->dev, true);
286 return status; 286 return status;
287} 287}
288 288
@@ -615,6 +615,22 @@ void i2c_unlock_adapter(struct i2c_adapter *adapter)
615} 615}
616EXPORT_SYMBOL_GPL(i2c_unlock_adapter); 616EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
617 617
618static void i2c_dev_set_name(struct i2c_adapter *adap,
619 struct i2c_client *client)
620{
621 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
622
623 if (adev) {
624 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
625 return;
626 }
627
628 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
629 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
630 client->addr | ((client->flags & I2C_CLIENT_TEN)
631 ? 0xa000 : 0));
632}
633
618/** 634/**
619 * i2c_new_device - instantiate an i2c device 635 * i2c_new_device - instantiate an i2c device
620 * @adap: the adapter managing the device 636 * @adap: the adapter managing the device
@@ -671,12 +687,9 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
671 client->dev.bus = &i2c_bus_type; 687 client->dev.bus = &i2c_bus_type;
672 client->dev.type = &i2c_client_type; 688 client->dev.type = &i2c_client_type;
673 client->dev.of_node = info->of_node; 689 client->dev.of_node = info->of_node;
674 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle); 690 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
675 691
676 /* For 10-bit clients, add an arbitrary offset to avoid collisions */ 692 i2c_dev_set_name(adap, client);
677 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
678 client->addr | ((client->flags & I2C_CLIENT_TEN)
679 ? 0xa000 : 0));
680 status = device_register(&client->dev); 693 status = device_register(&client->dev);
681 if (status) 694 if (status)
682 goto out_err; 695 goto out_err;
@@ -1100,7 +1113,7 @@ static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
1100 return AE_OK; 1113 return AE_OK;
1101 1114
1102 memset(&info, 0, sizeof(info)); 1115 memset(&info, 0, sizeof(info));
1103 info.acpi_node.handle = handle; 1116 info.acpi_node.companion = adev;
1104 info.irq = -1; 1117 info.irq = -1;
1105 1118
1106 INIT_LIST_HEAD(&resource_list); 1119 INIT_LIST_HEAD(&resource_list);
@@ -1111,8 +1124,10 @@ static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
1111 if (ret < 0 || !info.addr) 1124 if (ret < 0 || !info.addr)
1112 return AE_OK; 1125 return AE_OK;
1113 1126
1127 adev->power.flags.ignore_parent = true;
1114 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type)); 1128 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
1115 if (!i2c_new_device(adapter, &info)) { 1129 if (!i2c_new_device(adapter, &info)) {
1130 adev->power.flags.ignore_parent = false;
1116 dev_err(&adapter->dev, 1131 dev_err(&adapter->dev,
1117 "failed to add I2C device %s from ACPI\n", 1132 "failed to add I2C device %s from ACPI\n",
1118 dev_name(&adev->dev)); 1133 dev_name(&adev->dev));
@@ -1134,6 +1149,9 @@ static void acpi_i2c_register_devices(struct i2c_adapter *adap)
1134 acpi_handle handle; 1149 acpi_handle handle;
1135 acpi_status status; 1150 acpi_status status;
1136 1151
1152 if (!adap->dev.parent)
1153 return;
1154
1137 handle = ACPI_HANDLE(adap->dev.parent); 1155 handle = ACPI_HANDLE(adap->dev.parent);
1138 if (!handle) 1156 if (!handle)
1139 return; 1157 return;
@@ -1606,9 +1624,14 @@ static int i2c_cmd(struct device *dev, void *_arg)
1606{ 1624{
1607 struct i2c_client *client = i2c_verify_client(dev); 1625 struct i2c_client *client = i2c_verify_client(dev);
1608 struct i2c_cmd_arg *arg = _arg; 1626 struct i2c_cmd_arg *arg = _arg;
1627 struct i2c_driver *driver;
1628
1629 if (!client || !client->dev.driver)
1630 return 0;
1609 1631
1610 if (client && client->driver && client->driver->command) 1632 driver = to_i2c_driver(client->dev.driver);
1611 client->driver->command(client, arg->cmd, arg->arg); 1633 if (driver->command)
1634 driver->command(client, arg->cmd, arg->arg);
1612 return 0; 1635 return 0;
1613} 1636}
1614 1637
diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
index c3ccdea3d180..80b47e8ce030 100644
--- a/drivers/i2c/i2c-dev.c
+++ b/drivers/i2c/i2c-dev.c
@@ -102,8 +102,8 @@ static void return_i2c_dev(struct i2c_dev *i2c_dev)
102 kfree(i2c_dev); 102 kfree(i2c_dev);
103} 103}
104 104
105static ssize_t show_adapter_name(struct device *dev, 105static ssize_t name_show(struct device *dev,
106 struct device_attribute *attr, char *buf) 106 struct device_attribute *attr, char *buf)
107{ 107{
108 struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt)); 108 struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
109 109
@@ -111,7 +111,13 @@ static ssize_t show_adapter_name(struct device *dev,
111 return -ENODEV; 111 return -ENODEV;
112 return sprintf(buf, "%s\n", i2c_dev->adap->name); 112 return sprintf(buf, "%s\n", i2c_dev->adap->name);
113} 113}
114static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL); 114static DEVICE_ATTR_RO(name);
115
116static struct attribute *i2c_attrs[] = {
117 &dev_attr_name.attr,
118 NULL,
119};
120ATTRIBUTE_GROUPS(i2c);
115 121
116/* ------------------------------------------------------------------------- */ 122/* ------------------------------------------------------------------------- */
117 123
@@ -562,15 +568,10 @@ static int i2cdev_attach_adapter(struct device *dev, void *dummy)
562 res = PTR_ERR(i2c_dev->dev); 568 res = PTR_ERR(i2c_dev->dev);
563 goto error; 569 goto error;
564 } 570 }
565 res = device_create_file(i2c_dev->dev, &dev_attr_name);
566 if (res)
567 goto error_destroy;
568 571
569 pr_debug("i2c-dev: adapter [%s] registered as minor %d\n", 572 pr_debug("i2c-dev: adapter [%s] registered as minor %d\n",
570 adap->name, adap->nr); 573 adap->name, adap->nr);
571 return 0; 574 return 0;
572error_destroy:
573 device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr));
574error: 575error:
575 return_i2c_dev(i2c_dev); 576 return_i2c_dev(i2c_dev);
576 return res; 577 return res;
@@ -589,7 +590,6 @@ static int i2cdev_detach_adapter(struct device *dev, void *dummy)
589 if (!i2c_dev) /* attach_adapter must have failed */ 590 if (!i2c_dev) /* attach_adapter must have failed */
590 return 0; 591 return 0;
591 592
592 device_remove_file(i2c_dev->dev, &dev_attr_name);
593 return_i2c_dev(i2c_dev); 593 return_i2c_dev(i2c_dev);
594 device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr)); 594 device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr));
595 595
@@ -637,6 +637,7 @@ static int __init i2c_dev_init(void)
637 res = PTR_ERR(i2c_dev_class); 637 res = PTR_ERR(i2c_dev_class);
638 goto out_unreg_chrdev; 638 goto out_unreg_chrdev;
639 } 639 }
640 i2c_dev_class->dev_groups = i2c_groups;
640 641
641 /* Keep track of adapters which will be added or removed later */ 642 /* Keep track of adapters which will be added or removed later */
642 res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier); 643 res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c
index 797e3117bef7..2d0847b6be62 100644
--- a/drivers/i2c/i2c-mux.c
+++ b/drivers/i2c/i2c-mux.c
@@ -139,6 +139,8 @@ struct i2c_adapter *i2c_add_mux_adapter(struct i2c_adapter *parent,
139 priv->adap.algo = &priv->algo; 139 priv->adap.algo = &priv->algo;
140 priv->adap.algo_data = priv; 140 priv->adap.algo_data = priv;
141 priv->adap.dev.parent = &parent->dev; 141 priv->adap.dev.parent = &parent->dev;
142 priv->adap.retries = parent->retries;
143 priv->adap.timeout = parent->timeout;
142 144
143 /* Sanity check on class */ 145 /* Sanity check on class */
144 if (i2c_mux_parent_classes(parent) & class) 146 if (i2c_mux_parent_classes(parent) & class)
diff --git a/drivers/i2c/i2c-smbus.c b/drivers/i2c/i2c-smbus.c
index 44d4c6071c15..c99b22987366 100644
--- a/drivers/i2c/i2c-smbus.c
+++ b/drivers/i2c/i2c-smbus.c
@@ -46,6 +46,7 @@ static int smbus_do_alert(struct device *dev, void *addrp)
46{ 46{
47 struct i2c_client *client = i2c_verify_client(dev); 47 struct i2c_client *client = i2c_verify_client(dev);
48 struct alert_data *data = addrp; 48 struct alert_data *data = addrp;
49 struct i2c_driver *driver;
49 50
50 if (!client || client->addr != data->addr) 51 if (!client || client->addr != data->addr)
51 return 0; 52 return 0;
@@ -54,12 +55,13 @@ static int smbus_do_alert(struct device *dev, void *addrp)
54 55
55 /* 56 /*
56 * Drivers should either disable alerts, or provide at least 57 * Drivers should either disable alerts, or provide at least
57 * a minimal handler. Lock so client->driver won't change. 58 * a minimal handler. Lock so the driver won't change.
58 */ 59 */
59 device_lock(dev); 60 device_lock(dev);
60 if (client->driver) { 61 if (client->dev.driver) {
61 if (client->driver->alert) 62 driver = to_i2c_driver(client->dev.driver);
62 client->driver->alert(client, data->flag); 63 if (driver->alert)
64 driver->alert(client, data->flag);
63 else 65 else
64 dev_warn(&client->dev, "no driver alert()!\n"); 66 dev_warn(&client->dev, "no driver alert()!\n");
65 } else 67 } else
diff --git a/drivers/i2c/muxes/i2c-arb-gpio-challenge.c b/drivers/i2c/muxes/i2c-arb-gpio-challenge.c
index 74b41ae690f3..c58e093b6032 100644
--- a/drivers/i2c/muxes/i2c-arb-gpio-challenge.c
+++ b/drivers/i2c/muxes/i2c-arb-gpio-challenge.c
@@ -200,7 +200,7 @@ static int i2c_arbitrator_probe(struct platform_device *pdev)
200 arb->parent = of_find_i2c_adapter_by_node(parent_np); 200 arb->parent = of_find_i2c_adapter_by_node(parent_np);
201 if (!arb->parent) { 201 if (!arb->parent) {
202 dev_err(dev, "Cannot find parent bus\n"); 202 dev_err(dev, "Cannot find parent bus\n");
203 return -EINVAL; 203 return -EPROBE_DEFER;
204 } 204 }
205 205
206 /* Actually add the mux adapter */ 206 /* Actually add the mux adapter */
@@ -238,7 +238,7 @@ static struct platform_driver i2c_arbitrator_driver = {
238 .driver = { 238 .driver = {
239 .owner = THIS_MODULE, 239 .owner = THIS_MODULE,
240 .name = "i2c-arb-gpio-challenge", 240 .name = "i2c-arb-gpio-challenge",
241 .of_match_table = of_match_ptr(i2c_arbitrator_of_match), 241 .of_match_table = i2c_arbitrator_of_match,
242 }, 242 },
243}; 243};
244 244
diff --git a/drivers/i2c/muxes/i2c-mux-gpio.c b/drivers/i2c/muxes/i2c-mux-gpio.c
index 5d4a99ba743e..8a8c56f4b026 100644
--- a/drivers/i2c/muxes/i2c-mux-gpio.c
+++ b/drivers/i2c/muxes/i2c-mux-gpio.c
@@ -30,15 +30,15 @@ static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
30 int i; 30 int i;
31 31
32 for (i = 0; i < mux->data.n_gpios; i++) 32 for (i = 0; i < mux->data.n_gpios; i++)
33 gpio_set_value(mux->gpio_base + mux->data.gpios[i], 33 gpio_set_value_cansleep(mux->gpio_base + mux->data.gpios[i],
34 val & (1 << i)); 34 val & (1 << i));
35} 35}
36 36
37static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan) 37static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan)
38{ 38{
39 struct gpiomux *mux = data; 39 struct gpiomux *mux = data;
40 40
41 i2c_mux_gpio_set(mux, mux->data.values[chan]); 41 i2c_mux_gpio_set(mux, chan);
42 42
43 return 0; 43 return 0;
44} 44}
@@ -66,7 +66,7 @@ static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
66 struct device_node *adapter_np, *child; 66 struct device_node *adapter_np, *child;
67 struct i2c_adapter *adapter; 67 struct i2c_adapter *adapter;
68 unsigned *values, *gpios; 68 unsigned *values, *gpios;
69 int i = 0; 69 int i = 0, ret;
70 70
71 if (!np) 71 if (!np)
72 return -ENODEV; 72 return -ENODEV;
@@ -79,7 +79,7 @@ static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
79 adapter = of_find_i2c_adapter_by_node(adapter_np); 79 adapter = of_find_i2c_adapter_by_node(adapter_np);
80 if (!adapter) { 80 if (!adapter) {
81 dev_err(&pdev->dev, "Cannot find parent bus\n"); 81 dev_err(&pdev->dev, "Cannot find parent bus\n");
82 return -ENODEV; 82 return -EPROBE_DEFER;
83 } 83 }
84 mux->data.parent = i2c_adapter_id(adapter); 84 mux->data.parent = i2c_adapter_id(adapter);
85 put_device(&adapter->dev); 85 put_device(&adapter->dev);
@@ -116,8 +116,12 @@ static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
116 return -ENOMEM; 116 return -ENOMEM;
117 } 117 }
118 118
119 for (i = 0; i < mux->data.n_gpios; i++) 119 for (i = 0; i < mux->data.n_gpios; i++) {
120 gpios[i] = of_get_named_gpio(np, "mux-gpios", i); 120 ret = of_get_named_gpio(np, "mux-gpios", i);
121 if (ret < 0)
122 return ret;
123 gpios[i] = ret;
124 }
121 125
122 mux->data.gpios = gpios; 126 mux->data.gpios = gpios;
123 127
@@ -177,7 +181,7 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
177 if (!parent) { 181 if (!parent) {
178 dev_err(&pdev->dev, "Parent adapter (%d) not found\n", 182 dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
179 mux->data.parent); 183 mux->data.parent);
180 return -ENODEV; 184 return -EPROBE_DEFER;
181 } 185 }
182 186
183 mux->parent = parent; 187 mux->parent = parent;
@@ -224,7 +228,7 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
224 unsigned int class = mux->data.classes ? mux->data.classes[i] : 0; 228 unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
225 229
226 mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr, 230 mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr,
227 i, class, 231 mux->data.values[i], class,
228 i2c_mux_gpio_select, deselect); 232 i2c_mux_gpio_select, deselect);
229 if (!mux->adap[i]) { 233 if (!mux->adap[i]) {
230 ret = -ENODEV; 234 ret = -ENODEV;
@@ -279,7 +283,7 @@ static struct platform_driver i2c_mux_gpio_driver = {
279 .driver = { 283 .driver = {
280 .owner = THIS_MODULE, 284 .owner = THIS_MODULE,
281 .name = "i2c-mux-gpio", 285 .name = "i2c-mux-gpio",
282 .of_match_table = of_match_ptr(i2c_mux_gpio_of_match), 286 .of_match_table = i2c_mux_gpio_of_match,
283 }, 287 },
284}; 288};
285 289
diff --git a/drivers/i2c/muxes/i2c-mux-pinctrl.c b/drivers/i2c/muxes/i2c-mux-pinctrl.c
index 69a91732ae65..d7978dc4ad0b 100644
--- a/drivers/i2c/muxes/i2c-mux-pinctrl.c
+++ b/drivers/i2c/muxes/i2c-mux-pinctrl.c
@@ -24,6 +24,7 @@
24#include <linux/i2c-mux-pinctrl.h> 24#include <linux/i2c-mux-pinctrl.h>
25#include <linux/platform_device.h> 25#include <linux/platform_device.h>
26#include <linux/slab.h> 26#include <linux/slab.h>
27#include <linux/of.h>
27 28
28struct i2c_mux_pinctrl { 29struct i2c_mux_pinctrl {
29 struct device *dev; 30 struct device *dev;
@@ -113,7 +114,7 @@ static int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux,
113 adapter = of_find_i2c_adapter_by_node(adapter_np); 114 adapter = of_find_i2c_adapter_by_node(adapter_np);
114 if (!adapter) { 115 if (!adapter) {
115 dev_err(mux->dev, "Cannot find parent bus\n"); 116 dev_err(mux->dev, "Cannot find parent bus\n");
116 return -ENODEV; 117 return -EPROBE_DEFER;
117 } 118 }
118 mux->pdata->parent_bus_num = i2c_adapter_id(adapter); 119 mux->pdata->parent_bus_num = i2c_adapter_id(adapter);
119 put_device(&adapter->dev); 120 put_device(&adapter->dev);
@@ -211,7 +212,7 @@ static int i2c_mux_pinctrl_probe(struct platform_device *pdev)
211 if (!mux->parent) { 212 if (!mux->parent) {
212 dev_err(&pdev->dev, "Parent adapter (%d) not found\n", 213 dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
213 mux->pdata->parent_bus_num); 214 mux->pdata->parent_bus_num);
214 ret = -ENODEV; 215 ret = -EPROBE_DEFER;
215 goto err; 216 goto err;
216 } 217 }
217 218