aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/busses
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2014-06-06 15:26:57 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2014-06-06 15:26:57 -0400
commit7b215de3d0abbc4f6daf2efd19e8809af0564490 (patch)
tree1170758ed12b1db76b3c5891ddda13229f6f96eb /drivers/i2c/busses
parent1fe9eb184721132c7254d76d9ef31c96edad8870 (diff)
parent642653d16a0f8e78b7a25d930b62aa771ebc939c (diff)
Merge branch 'i2c/for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux into next
Pull i2c updates from Wolfram Sang: "I2C has the following updates for 3.16: - major cleanups to the rcar and sh_mobile drivers - removal of nuc900 driver which had a compile error for years - usual bunch of driver updates, bugfixes and cleanups" * 'i2c/for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux: (44 commits) i2c: pca954x: Fix compilation without CONFIG_GPIOLIB i2c: mux: pca954x: Use the descriptor-based GPIO API i2c: mpc: insert DR read in i2c_fixup() i2c: bfin: turn to Resource-managed API in probe function i2c: Make of_device_id array const i2c: remove unnecessary OOM messages i2c: designware-pci: Add Haswell PCI IDs i2c: designware: Add runtime PM hooks i2c: designware: Disable device on system suspend i2c: nuc900: remove driver i2c: imx: update i2c clock divider for each transaction i2c: imx: fix the i2c bus hang issue when do repeat restart i2c: rcar: update copyright and license information i2c: rcar: janitorial cleanup after refactoring i2c: rcar: reuse status bits as enable bits i2c: rcar: remove spinlock i2c: rcar: refactor status bit handling i2c: rcar: refactor setting up msg i2c: rcar: check bus free before first message i2c: rcar: refactor irq state machine ...
Diffstat (limited to 'drivers/i2c/busses')
-rw-r--r--drivers/i2c/busses/Kconfig9
-rw-r--r--drivers/i2c/busses/Makefile1
-rw-r--r--drivers/i2c/busses/i2c-ali1563.c82
-rw-r--r--drivers/i2c/busses/i2c-bcm2835.c4
-rw-r--r--drivers/i2c/busses/i2c-bfin-twi.c44
-rw-r--r--drivers/i2c/busses/i2c-designware-pcidrv.c22
-rw-r--r--drivers/i2c/busses/i2c-designware-platdrv.c13
-rw-r--r--drivers/i2c/busses/i2c-diolan-u2c.c1
-rw-r--r--drivers/i2c/busses/i2c-efm32.c4
-rw-r--r--drivers/i2c/busses/i2c-eg20t.c4
-rw-r--r--drivers/i2c/busses/i2c-exynos5.c71
-rw-r--r--drivers/i2c/busses/i2c-gpio.c25
-rw-r--r--drivers/i2c/busses/i2c-imx.c169
-rw-r--r--drivers/i2c/busses/i2c-mpc.c2
-rw-r--r--drivers/i2c/busses/i2c-mv64xxx.c2
-rw-r--r--drivers/i2c/busses/i2c-nomadik.c14
-rw-r--r--drivers/i2c/busses/i2c-nuc900.c709
-rw-r--r--drivers/i2c/busses/i2c-ocores.c2
-rw-r--r--drivers/i2c/busses/i2c-omap.c4
-rw-r--r--drivers/i2c/busses/i2c-pxa.c2
-rw-r--r--drivers/i2c/busses/i2c-rcar.c259
-rw-r--r--drivers/i2c/busses/i2c-riic.c2
-rw-r--r--drivers/i2c/busses/i2c-s3c2410.c8
-rw-r--r--drivers/i2c/busses/i2c-sh_mobile.c225
-rw-r--r--drivers/i2c/busses/i2c-simtec.c4
-rw-r--r--drivers/i2c/busses/i2c-sirf.c1
-rw-r--r--drivers/i2c/busses/i2c-st.c2
-rw-r--r--drivers/i2c/busses/i2c-stu300.c4
-rw-r--r--drivers/i2c/busses/i2c-tegra.c4
-rw-r--r--drivers/i2c/busses/i2c-wmt.c6
-rw-r--r--drivers/i2c/busses/scx200_acb.c4
31 files changed, 441 insertions, 1262 deletions
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index 9a0a6cc7f4ba..620d1004a1e7 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -449,7 +449,7 @@ config I2C_EFM32
449 449
450config I2C_EG20T 450config I2C_EG20T
451 tristate "Intel EG20T PCH/LAPIS Semicon IOH(ML7213/ML7223/ML7831) I2C" 451 tristate "Intel EG20T PCH/LAPIS Semicon IOH(ML7213/ML7223/ML7831) I2C"
452 depends on PCI 452 depends on PCI && (X86_32 || COMPILE_TEST)
453 help 453 help
454 This driver is for PCH(Platform controller Hub) I2C of EG20T which 454 This driver is for PCH(Platform controller Hub) I2C of EG20T which
455 is an IOH(Input/Output Hub) for x86 embedded processor. 455 is an IOH(Input/Output Hub) for x86 embedded processor.
@@ -570,13 +570,6 @@ config I2C_NOMADIK
570 I2C interface from ST-Ericsson's Nomadik and Ux500 architectures, 570 I2C interface from ST-Ericsson's Nomadik and Ux500 architectures,
571 as well as the STA2X11 PCIe I/O HUB. 571 as well as the STA2X11 PCIe I/O HUB.
572 572
573config I2C_NUC900
574 tristate "NUC900 I2C Driver"
575 depends on ARCH_W90X900
576 help
577 Say Y here to include support for I2C controller in the
578 Winbond/Nuvoton NUC900 based System-on-Chip devices.
579
580config I2C_OCORES 573config I2C_OCORES
581 tristate "OpenCores I2C Controller" 574 tristate "OpenCores I2C Controller"
582 help 575 help
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index e110ca932918..298692cc6000 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -55,7 +55,6 @@ obj-$(CONFIG_I2C_MPC) += i2c-mpc.o
55obj-$(CONFIG_I2C_MV64XXX) += i2c-mv64xxx.o 55obj-$(CONFIG_I2C_MV64XXX) += i2c-mv64xxx.o
56obj-$(CONFIG_I2C_MXS) += i2c-mxs.o 56obj-$(CONFIG_I2C_MXS) += i2c-mxs.o
57obj-$(CONFIG_I2C_NOMADIK) += i2c-nomadik.o 57obj-$(CONFIG_I2C_NOMADIK) += i2c-nomadik.o
58obj-$(CONFIG_I2C_NUC900) += i2c-nuc900.o
59obj-$(CONFIG_I2C_OCORES) += i2c-ocores.o 58obj-$(CONFIG_I2C_OCORES) += i2c-ocores.o
60obj-$(CONFIG_I2C_OMAP) += i2c-omap.o 59obj-$(CONFIG_I2C_OMAP) += i2c-omap.o
61obj-$(CONFIG_I2C_PASEMI) += i2c-pasemi.o 60obj-$(CONFIG_I2C_PASEMI) += i2c-pasemi.o
diff --git a/drivers/i2c/busses/i2c-ali1563.c b/drivers/i2c/busses/i2c-ali1563.c
index 98a1c97739ba..15517d78d5ff 100644
--- a/drivers/i2c/busses/i2c-ali1563.c
+++ b/drivers/i2c/busses/i2c-ali1563.c
@@ -63,7 +63,7 @@
63static struct pci_driver ali1563_pci_driver; 63static struct pci_driver ali1563_pci_driver;
64static unsigned short ali1563_smba; 64static unsigned short ali1563_smba;
65 65
66static int ali1563_transaction(struct i2c_adapter * a, int size) 66static int ali1563_transaction(struct i2c_adapter *a, int size)
67{ 67{
68 u32 data; 68 u32 data;
69 int timeout; 69 int timeout;
@@ -78,7 +78,7 @@ static int ali1563_transaction(struct i2c_adapter * a, int size)
78 data = inb_p(SMB_HST_STS); 78 data = inb_p(SMB_HST_STS);
79 if (data & HST_STS_BAD) { 79 if (data & HST_STS_BAD) {
80 dev_err(&a->dev, "ali1563: Trying to reset busy device\n"); 80 dev_err(&a->dev, "ali1563: Trying to reset busy device\n");
81 outb_p(data | HST_STS_BAD,SMB_HST_STS); 81 outb_p(data | HST_STS_BAD, SMB_HST_STS);
82 data = inb_p(SMB_HST_STS); 82 data = inb_p(SMB_HST_STS);
83 if (data & HST_STS_BAD) 83 if (data & HST_STS_BAD)
84 return -EBUSY; 84 return -EBUSY;
@@ -102,10 +102,10 @@ static int ali1563_transaction(struct i2c_adapter * a, int size)
102 if (!timeout) { 102 if (!timeout) {
103 dev_err(&a->dev, "Timeout - Trying to KILL transaction!\n"); 103 dev_err(&a->dev, "Timeout - Trying to KILL transaction!\n");
104 /* Issue 'kill' to host controller */ 104 /* Issue 'kill' to host controller */
105 outb_p(HST_CNTL2_KILL,SMB_HST_CNTL2); 105 outb_p(HST_CNTL2_KILL, SMB_HST_CNTL2);
106 data = inb_p(SMB_HST_STS); 106 data = inb_p(SMB_HST_STS);
107 status = -ETIMEDOUT; 107 status = -ETIMEDOUT;
108 } 108 }
109 109
110 /* device error - no response, ignore the autodetection case */ 110 /* device error - no response, ignore the autodetection case */
111 if (data & HST_STS_DEVERR) { 111 if (data & HST_STS_DEVERR) {
@@ -117,18 +117,18 @@ static int ali1563_transaction(struct i2c_adapter * a, int size)
117 if (data & HST_STS_BUSERR) { 117 if (data & HST_STS_BUSERR) {
118 dev_err(&a->dev, "Bus collision!\n"); 118 dev_err(&a->dev, "Bus collision!\n");
119 /* Issue timeout, hoping it helps */ 119 /* Issue timeout, hoping it helps */
120 outb_p(HST_CNTL1_TIMEOUT,SMB_HST_CNTL1); 120 outb_p(HST_CNTL1_TIMEOUT, SMB_HST_CNTL1);
121 } 121 }
122 122
123 if (data & HST_STS_FAIL) { 123 if (data & HST_STS_FAIL) {
124 dev_err(&a->dev, "Cleaning fail after KILL!\n"); 124 dev_err(&a->dev, "Cleaning fail after KILL!\n");
125 outb_p(0x0,SMB_HST_CNTL2); 125 outb_p(0x0, SMB_HST_CNTL2);
126 } 126 }
127 127
128 return status; 128 return status;
129} 129}
130 130
131static int ali1563_block_start(struct i2c_adapter * a) 131static int ali1563_block_start(struct i2c_adapter *a)
132{ 132{
133 u32 data; 133 u32 data;
134 int timeout; 134 int timeout;
@@ -142,8 +142,8 @@ static int ali1563_block_start(struct i2c_adapter * a)
142 142
143 data = inb_p(SMB_HST_STS); 143 data = inb_p(SMB_HST_STS);
144 if (data & HST_STS_BAD) { 144 if (data & HST_STS_BAD) {
145 dev_warn(&a->dev,"ali1563: Trying to reset busy device\n"); 145 dev_warn(&a->dev, "ali1563: Trying to reset busy device\n");
146 outb_p(data | HST_STS_BAD,SMB_HST_STS); 146 outb_p(data | HST_STS_BAD, SMB_HST_STS);
147 data = inb_p(SMB_HST_STS); 147 data = inb_p(SMB_HST_STS);
148 if (data & HST_STS_BAD) 148 if (data & HST_STS_BAD)
149 return -EBUSY; 149 return -EBUSY;
@@ -184,13 +184,14 @@ static int ali1563_block_start(struct i2c_adapter * a)
184 return status; 184 return status;
185} 185}
186 186
187static int ali1563_block(struct i2c_adapter * a, union i2c_smbus_data * data, u8 rw) 187static int ali1563_block(struct i2c_adapter *a,
188 union i2c_smbus_data *data, u8 rw)
188{ 189{
189 int i, len; 190 int i, len;
190 int error = 0; 191 int error = 0;
191 192
192 /* Do we need this? */ 193 /* Do we need this? */
193 outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1); 194 outb_p(HST_CNTL1_LAST, SMB_HST_CNTL1);
194 195
195 if (rw == I2C_SMBUS_WRITE) { 196 if (rw == I2C_SMBUS_WRITE) {
196 len = data->block[0]; 197 len = data->block[0];
@@ -198,8 +199,8 @@ static int ali1563_block(struct i2c_adapter * a, union i2c_smbus_data * data, u8
198 len = 1; 199 len = 1;
199 else if (len > 32) 200 else if (len > 32)
200 len = 32; 201 len = 32;
201 outb_p(len,SMB_HST_DAT0); 202 outb_p(len, SMB_HST_DAT0);
202 outb_p(data->block[1],SMB_BLK_DAT); 203 outb_p(data->block[1], SMB_BLK_DAT);
203 } else 204 } else
204 len = 32; 205 len = 32;
205 206
@@ -208,10 +209,12 @@ static int ali1563_block(struct i2c_adapter * a, union i2c_smbus_data * data, u8
208 for (i = 0; i < len; i++) { 209 for (i = 0; i < len; i++) {
209 if (rw == I2C_SMBUS_WRITE) { 210 if (rw == I2C_SMBUS_WRITE) {
210 outb_p(data->block[i + 1], SMB_BLK_DAT); 211 outb_p(data->block[i + 1], SMB_BLK_DAT);
211 if ((error = ali1563_block_start(a))) 212 error = ali1563_block_start(a);
213 if (error)
212 break; 214 break;
213 } else { 215 } else {
214 if ((error = ali1563_block_start(a))) 216 error = ali1563_block_start(a);
217 if (error)
215 break; 218 break;
216 if (i == 0) { 219 if (i == 0) {
217 len = inb_p(SMB_HST_DAT0); 220 len = inb_p(SMB_HST_DAT0);
@@ -224,25 +227,26 @@ static int ali1563_block(struct i2c_adapter * a, union i2c_smbus_data * data, u8
224 } 227 }
225 } 228 }
226 /* Do we need this? */ 229 /* Do we need this? */
227 outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1); 230 outb_p(HST_CNTL1_LAST, SMB_HST_CNTL1);
228 return error; 231 return error;
229} 232}
230 233
231static s32 ali1563_access(struct i2c_adapter * a, u16 addr, 234static s32 ali1563_access(struct i2c_adapter *a, u16 addr,
232 unsigned short flags, char rw, u8 cmd, 235 unsigned short flags, char rw, u8 cmd,
233 int size, union i2c_smbus_data * data) 236 int size, union i2c_smbus_data *data)
234{ 237{
235 int error = 0; 238 int error = 0;
236 int timeout; 239 int timeout;
237 u32 reg; 240 u32 reg;
238 241
239 for (timeout = ALI1563_MAX_TIMEOUT; timeout; timeout--) { 242 for (timeout = ALI1563_MAX_TIMEOUT; timeout; timeout--) {
240 if (!(reg = inb_p(SMB_HST_STS) & HST_STS_BUSY)) 243 reg = inb_p(SMB_HST_STS);
244 if (!(reg & HST_STS_BUSY))
241 break; 245 break;
242 } 246 }
243 if (!timeout) 247 if (!timeout)
244 dev_warn(&a->dev,"SMBus not idle. HST_STS = %02x\n",reg); 248 dev_warn(&a->dev, "SMBus not idle. HST_STS = %02x\n", reg);
245 outb_p(0xff,SMB_HST_STS); 249 outb_p(0xff, SMB_HST_STS);
246 250
247 /* Map the size to what the chip understands */ 251 /* Map the size to what the chip understands */
248 switch (size) { 252 switch (size) {
@@ -268,13 +272,14 @@ static s32 ali1563_access(struct i2c_adapter * a, u16 addr,
268 } 272 }
269 273
270 outb_p(((addr & 0x7f) << 1) | (rw & 0x01), SMB_HST_ADD); 274 outb_p(((addr & 0x7f) << 1) | (rw & 0x01), SMB_HST_ADD);
271 outb_p((inb_p(SMB_HST_CNTL2) & ~HST_CNTL2_SIZEMASK) | (size << 3), SMB_HST_CNTL2); 275 outb_p((inb_p(SMB_HST_CNTL2) & ~HST_CNTL2_SIZEMASK) |
276 (size << 3), SMB_HST_CNTL2);
272 277
273 /* Write the command register */ 278 /* Write the command register */
274 279
275 switch(size) { 280 switch (size) {
276 case HST_CNTL2_BYTE: 281 case HST_CNTL2_BYTE:
277 if (rw== I2C_SMBUS_WRITE) 282 if (rw == I2C_SMBUS_WRITE)
278 /* Beware it uses DAT0 register and not CMD! */ 283 /* Beware it uses DAT0 register and not CMD! */
279 outb_p(cmd, SMB_HST_DAT0); 284 outb_p(cmd, SMB_HST_DAT0);
280 break; 285 break;
@@ -292,11 +297,12 @@ static s32 ali1563_access(struct i2c_adapter * a, u16 addr,
292 break; 297 break;
293 case HST_CNTL2_BLOCK: 298 case HST_CNTL2_BLOCK:
294 outb_p(cmd, SMB_HST_CMD); 299 outb_p(cmd, SMB_HST_CMD);
295 error = ali1563_block(a,data,rw); 300 error = ali1563_block(a, data, rw);
296 goto Done; 301 goto Done;
297 } 302 }
298 303
299 if ((error = ali1563_transaction(a, size))) 304 error = ali1563_transaction(a, size);
305 if (error)
300 goto Done; 306 goto Done;
301 307
302 if ((rw == I2C_SMBUS_WRITE) || (size == HST_CNTL2_QUICK)) 308 if ((rw == I2C_SMBUS_WRITE) || (size == HST_CNTL2_QUICK))
@@ -317,7 +323,7 @@ Done:
317 return error; 323 return error;
318} 324}
319 325
320static u32 ali1563_func(struct i2c_adapter * a) 326static u32 ali1563_func(struct i2c_adapter *a)
321{ 327{
322 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | 328 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
323 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | 329 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
@@ -329,13 +335,13 @@ static int ali1563_setup(struct pci_dev *dev)
329{ 335{
330 u16 ctrl; 336 u16 ctrl;
331 337
332 pci_read_config_word(dev,ALI1563_SMBBA,&ctrl); 338 pci_read_config_word(dev, ALI1563_SMBBA, &ctrl);
333 339
334 /* SMB I/O Base in high 12 bits and must be aligned with the 340 /* SMB I/O Base in high 12 bits and must be aligned with the
335 * size of the I/O space. */ 341 * size of the I/O space. */
336 ali1563_smba = ctrl & ~(ALI1563_SMB_IOSIZE - 1); 342 ali1563_smba = ctrl & ~(ALI1563_SMB_IOSIZE - 1);
337 if (!ali1563_smba) { 343 if (!ali1563_smba) {
338 dev_warn(&dev->dev,"ali1563_smba Uninitialized\n"); 344 dev_warn(&dev->dev, "ali1563_smba Uninitialized\n");
339 goto Err; 345 goto Err;
340 } 346 }
341 347
@@ -350,8 +356,8 @@ static int ali1563_setup(struct pci_dev *dev)
350 ctrl | ALI1563_SMB_IOEN); 356 ctrl | ALI1563_SMB_IOEN);
351 pci_read_config_word(dev, ALI1563_SMBBA, &ctrl); 357 pci_read_config_word(dev, ALI1563_SMBBA, &ctrl);
352 if (!(ctrl & ALI1563_SMB_IOEN)) { 358 if (!(ctrl & ALI1563_SMB_IOEN)) {
353 dev_err(&dev->dev, "I/O space still not enabled, " 359 dev_err(&dev->dev,
354 "giving up\n"); 360 "I/O space still not enabled, giving up\n");
355 goto Err; 361 goto Err;
356 } 362 }
357 } 363 }
@@ -375,7 +381,7 @@ Err:
375 381
376static void ali1563_shutdown(struct pci_dev *dev) 382static void ali1563_shutdown(struct pci_dev *dev)
377{ 383{
378 release_region(ali1563_smba,ALI1563_SMB_IOSIZE); 384 release_region(ali1563_smba, ALI1563_SMB_IOSIZE);
379} 385}
380 386
381static const struct i2c_algorithm ali1563_algorithm = { 387static const struct i2c_algorithm ali1563_algorithm = {
@@ -394,12 +400,14 @@ static int ali1563_probe(struct pci_dev *dev,
394{ 400{
395 int error; 401 int error;
396 402
397 if ((error = ali1563_setup(dev))) 403 error = ali1563_setup(dev);
404 if (error)
398 goto exit; 405 goto exit;
399 ali1563_adapter.dev.parent = &dev->dev; 406 ali1563_adapter.dev.parent = &dev->dev;
400 snprintf(ali1563_adapter.name, sizeof(ali1563_adapter.name), 407 snprintf(ali1563_adapter.name, sizeof(ali1563_adapter.name),
401 "SMBus ALi 1563 Adapter @ %04x", ali1563_smba); 408 "SMBus ALi 1563 Adapter @ %04x", ali1563_smba);
402 if ((error = i2c_add_adapter(&ali1563_adapter))) 409 error = i2c_add_adapter(&ali1563_adapter);
410 if (error)
403 goto exit_shutdown; 411 goto exit_shutdown;
404 return 0; 412 return 0;
405 413
@@ -421,12 +429,12 @@ static const struct pci_device_id ali1563_id_table[] = {
421 {}, 429 {},
422}; 430};
423 431
424MODULE_DEVICE_TABLE (pci, ali1563_id_table); 432MODULE_DEVICE_TABLE(pci, ali1563_id_table);
425 433
426static struct pci_driver ali1563_pci_driver = { 434static struct pci_driver ali1563_pci_driver = {
427 .name = "ali1563_smbus", 435 .name = "ali1563_smbus",
428 .id_table = ali1563_id_table, 436 .id_table = ali1563_id_table,
429 .probe = ali1563_probe, 437 .probe = ali1563_probe,
430 .remove = ali1563_remove, 438 .remove = ali1563_remove,
431}; 439};
432 440
diff --git a/drivers/i2c/busses/i2c-bcm2835.c b/drivers/i2c/busses/i2c-bcm2835.c
index c60719577fc3..214ff9700efe 100644
--- a/drivers/i2c/busses/i2c-bcm2835.c
+++ b/drivers/i2c/busses/i2c-bcm2835.c
@@ -225,10 +225,8 @@ static int bcm2835_i2c_probe(struct platform_device *pdev)
225 struct i2c_adapter *adap; 225 struct i2c_adapter *adap;
226 226
227 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); 227 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
228 if (!i2c_dev) { 228 if (!i2c_dev)
229 dev_err(&pdev->dev, "Cannot allocate i2c_dev\n");
230 return -ENOMEM; 229 return -ENOMEM;
231 }
232 platform_set_drvdata(pdev, i2c_dev); 230 platform_set_drvdata(pdev, i2c_dev);
233 i2c_dev->dev = &pdev->dev; 231 i2c_dev->dev = &pdev->dev;
234 init_completion(&i2c_dev->completion); 232 init_completion(&i2c_dev->completion);
diff --git a/drivers/i2c/busses/i2c-bfin-twi.c b/drivers/i2c/busses/i2c-bfin-twi.c
index e6d5162b6379..3e271e7558d3 100644
--- a/drivers/i2c/busses/i2c-bfin-twi.c
+++ b/drivers/i2c/busses/i2c-bfin-twi.c
@@ -620,35 +620,27 @@ static int i2c_bfin_twi_probe(struct platform_device *pdev)
620 int rc; 620 int rc;
621 unsigned int clkhilow; 621 unsigned int clkhilow;
622 622
623 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL); 623 iface = devm_kzalloc(&pdev->dev, sizeof(struct bfin_twi_iface),
624 GFP_KERNEL);
624 if (!iface) { 625 if (!iface) {
625 dev_err(&pdev->dev, "Cannot allocate memory\n"); 626 dev_err(&pdev->dev, "Cannot allocate memory\n");
626 rc = -ENOMEM; 627 return -ENOMEM;
627 goto out_error_nomem;
628 } 628 }
629 629
630 spin_lock_init(&(iface->lock)); 630 spin_lock_init(&(iface->lock));
631 631
632 /* Find and map our resources */ 632 /* Find and map our resources */
633 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 633 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
634 if (res == NULL) { 634 iface->regs_base = devm_ioremap_resource(&pdev->dev, res);
635 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n"); 635 if (IS_ERR(iface->regs_base)) {
636 rc = -ENOENT;
637 goto out_error_get_res;
638 }
639
640 iface->regs_base = ioremap(res->start, resource_size(res));
641 if (iface->regs_base == NULL) {
642 dev_err(&pdev->dev, "Cannot map IO\n"); 636 dev_err(&pdev->dev, "Cannot map IO\n");
643 rc = -ENXIO; 637 return PTR_ERR(iface->regs_base);
644 goto out_error_ioremap;
645 } 638 }
646 639
647 iface->irq = platform_get_irq(pdev, 0); 640 iface->irq = platform_get_irq(pdev, 0);
648 if (iface->irq < 0) { 641 if (iface->irq < 0) {
649 dev_err(&pdev->dev, "No IRQ specified\n"); 642 dev_err(&pdev->dev, "No IRQ specified\n");
650 rc = -ENOENT; 643 return -ENOENT;
651 goto out_error_no_irq;
652 } 644 }
653 645
654 p_adap = &iface->adap; 646 p_adap = &iface->adap;
@@ -666,15 +658,15 @@ static int i2c_bfin_twi_probe(struct platform_device *pdev)
666 "i2c-bfin-twi"); 658 "i2c-bfin-twi");
667 if (rc) { 659 if (rc) {
668 dev_err(&pdev->dev, "Can't setup pin mux!\n"); 660 dev_err(&pdev->dev, "Can't setup pin mux!\n");
669 goto out_error_pin_mux; 661 return -EBUSY;
670 } 662 }
671 663
672 rc = request_irq(iface->irq, bfin_twi_interrupt_entry, 664 rc = devm_request_irq(&pdev->dev, iface->irq, bfin_twi_interrupt_entry,
673 0, pdev->name, iface); 665 0, pdev->name, iface);
674 if (rc) { 666 if (rc) {
675 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq); 667 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
676 rc = -ENODEV; 668 rc = -ENODEV;
677 goto out_error_req_irq; 669 goto out_error;
678 } 670 }
679 671
680 /* Set TWI internal clock as 10MHz */ 672 /* Set TWI internal clock as 10MHz */
@@ -695,7 +687,7 @@ static int i2c_bfin_twi_probe(struct platform_device *pdev)
695 rc = i2c_add_numbered_adapter(p_adap); 687 rc = i2c_add_numbered_adapter(p_adap);
696 if (rc < 0) { 688 if (rc < 0) {
697 dev_err(&pdev->dev, "Can't add i2c adapter!\n"); 689 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
698 goto out_error_add_adapter; 690 goto out_error;
699 } 691 }
700 692
701 platform_set_drvdata(pdev, iface); 693 platform_set_drvdata(pdev, iface);
@@ -705,17 +697,8 @@ static int i2c_bfin_twi_probe(struct platform_device *pdev)
705 697
706 return 0; 698 return 0;
707 699
708out_error_add_adapter: 700out_error:
709 free_irq(iface->irq, iface);
710out_error_req_irq:
711out_error_no_irq:
712 peripheral_free_list(dev_get_platdata(&pdev->dev)); 701 peripheral_free_list(dev_get_platdata(&pdev->dev));
713out_error_pin_mux:
714 iounmap(iface->regs_base);
715out_error_ioremap:
716out_error_get_res:
717 kfree(iface);
718out_error_nomem:
719 return rc; 702 return rc;
720} 703}
721 704
@@ -724,10 +707,7 @@ static int i2c_bfin_twi_remove(struct platform_device *pdev)
724 struct bfin_twi_iface *iface = platform_get_drvdata(pdev); 707 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
725 708
726 i2c_del_adapter(&(iface->adap)); 709 i2c_del_adapter(&(iface->adap));
727 free_irq(iface->irq, iface);
728 peripheral_free_list(dev_get_platdata(&pdev->dev)); 710 peripheral_free_list(dev_get_platdata(&pdev->dev));
729 iounmap(iface->regs_base);
730 kfree(iface);
731 711
732 return 0; 712 return 0;
733} 713}
diff --git a/drivers/i2c/busses/i2c-designware-pcidrv.c b/drivers/i2c/busses/i2c-designware-pcidrv.c
index 85056c22d21e..3356f7ab9f79 100644
--- a/drivers/i2c/busses/i2c-designware-pcidrv.c
+++ b/drivers/i2c/busses/i2c-designware-pcidrv.c
@@ -56,6 +56,7 @@ enum dw_pci_ctl_id_t {
56 medfield_5, 56 medfield_5,
57 57
58 baytrail, 58 baytrail,
59 haswell,
59}; 60};
60 61
61struct dw_scl_sda_cfg { 62struct dw_scl_sda_cfg {
@@ -95,6 +96,15 @@ static struct dw_scl_sda_cfg byt_config = {
95 .sda_hold = 0x6, 96 .sda_hold = 0x6,
96}; 97};
97 98
99/* Haswell HCNT/LCNT/SDA hold time */
100static struct dw_scl_sda_cfg hsw_config = {
101 .ss_hcnt = 0x01b0,
102 .fs_hcnt = 0x48,
103 .ss_lcnt = 0x01fb,
104 .fs_lcnt = 0xa0,
105 .sda_hold = 0x9,
106};
107
98static struct dw_pci_controller dw_pci_controllers[] = { 108static struct dw_pci_controller dw_pci_controllers[] = {
99 [moorestown_0] = { 109 [moorestown_0] = {
100 .bus_num = 0, 110 .bus_num = 0,
@@ -168,6 +178,15 @@ static struct dw_pci_controller dw_pci_controllers[] = {
168 .functionality = I2C_FUNC_10BIT_ADDR, 178 .functionality = I2C_FUNC_10BIT_ADDR,
169 .scl_sda_cfg = &byt_config, 179 .scl_sda_cfg = &byt_config,
170 }, 180 },
181 [haswell] = {
182 .bus_num = -1,
183 .bus_cfg = INTEL_MID_STD_CFG | DW_IC_CON_SPEED_FAST,
184 .tx_fifo_depth = 32,
185 .rx_fifo_depth = 32,
186 .clk_khz = 100000,
187 .functionality = I2C_FUNC_10BIT_ADDR,
188 .scl_sda_cfg = &hsw_config,
189 },
171}; 190};
172static struct i2c_algorithm i2c_dw_algo = { 191static struct i2c_algorithm i2c_dw_algo = {
173 .master_xfer = i2c_dw_xfer, 192 .master_xfer = i2c_dw_xfer,
@@ -328,6 +347,9 @@ static const struct pci_device_id i2_designware_pci_ids[] = {
328 { PCI_VDEVICE(INTEL, 0x0F45), baytrail }, 347 { PCI_VDEVICE(INTEL, 0x0F45), baytrail },
329 { PCI_VDEVICE(INTEL, 0x0F46), baytrail }, 348 { PCI_VDEVICE(INTEL, 0x0F46), baytrail },
330 { PCI_VDEVICE(INTEL, 0x0F47), baytrail }, 349 { PCI_VDEVICE(INTEL, 0x0F47), baytrail },
350 /* Haswell */
351 { PCI_VDEVICE(INTEL, 0x9c61), haswell },
352 { PCI_VDEVICE(INTEL, 0x9c62), haswell },
331 { 0,} 353 { 0,}
332}; 354};
333MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids); 355MODULE_DEVICE_TABLE(pci, i2_designware_pci_ids);
diff --git a/drivers/i2c/busses/i2c-designware-platdrv.c b/drivers/i2c/busses/i2c-designware-platdrv.c
index 9c7802614342..402ec3970fed 100644
--- a/drivers/i2c/busses/i2c-designware-platdrv.c
+++ b/drivers/i2c/busses/i2c-designware-platdrv.c
@@ -247,12 +247,13 @@ static const struct of_device_id dw_i2c_of_match[] = {
247MODULE_DEVICE_TABLE(of, dw_i2c_of_match); 247MODULE_DEVICE_TABLE(of, dw_i2c_of_match);
248#endif 248#endif
249 249
250#ifdef CONFIG_PM_SLEEP 250#ifdef CONFIG_PM
251static int dw_i2c_suspend(struct device *dev) 251static int dw_i2c_suspend(struct device *dev)
252{ 252{
253 struct platform_device *pdev = to_platform_device(dev); 253 struct platform_device *pdev = to_platform_device(dev);
254 struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev); 254 struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev);
255 255
256 i2c_dw_disable(i_dev);
256 clk_disable_unprepare(i_dev->clk); 257 clk_disable_unprepare(i_dev->clk);
257 258
258 return 0; 259 return 0;
@@ -268,13 +269,11 @@ static int dw_i2c_resume(struct device *dev)
268 269
269 return 0; 270 return 0;
270} 271}
271
272static SIMPLE_DEV_PM_OPS(dw_i2c_dev_pm_ops, dw_i2c_suspend, dw_i2c_resume);
273#define DW_I2C_DEV_PM_OPS (&dw_i2c_dev_pm_ops)
274#else
275#define DW_I2C_DEV_PM_OPS NULL
276#endif 272#endif
277 273
274static UNIVERSAL_DEV_PM_OPS(dw_i2c_dev_pm_ops, dw_i2c_suspend,
275 dw_i2c_resume, NULL);
276
278/* work with hotplug and coldplug */ 277/* work with hotplug and coldplug */
279MODULE_ALIAS("platform:i2c_designware"); 278MODULE_ALIAS("platform:i2c_designware");
280 279
@@ -286,7 +285,7 @@ static struct platform_driver dw_i2c_driver = {
286 .owner = THIS_MODULE, 285 .owner = THIS_MODULE,
287 .of_match_table = of_match_ptr(dw_i2c_of_match), 286 .of_match_table = of_match_ptr(dw_i2c_of_match),
288 .acpi_match_table = ACPI_PTR(dw_i2c_acpi_match), 287 .acpi_match_table = ACPI_PTR(dw_i2c_acpi_match),
289 .pm = DW_I2C_DEV_PM_OPS, 288 .pm = &dw_i2c_dev_pm_ops,
290 }, 289 },
291}; 290};
292 291
diff --git a/drivers/i2c/busses/i2c-diolan-u2c.c b/drivers/i2c/busses/i2c-diolan-u2c.c
index 721f7ebf9a3b..b19a310bf9b3 100644
--- a/drivers/i2c/busses/i2c-diolan-u2c.c
+++ b/drivers/i2c/busses/i2c-diolan-u2c.c
@@ -455,7 +455,6 @@ static int diolan_u2c_probe(struct usb_interface *interface,
455 /* allocate memory for our device state and initialize it */ 455 /* allocate memory for our device state and initialize it */
456 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 456 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
457 if (dev == NULL) { 457 if (dev == NULL) {
458 dev_err(&interface->dev, "no memory for device state\n");
459 ret = -ENOMEM; 458 ret = -ENOMEM;
460 goto error; 459 goto error;
461 } 460 }
diff --git a/drivers/i2c/busses/i2c-efm32.c b/drivers/i2c/busses/i2c-efm32.c
index 777ed409a24a..f7eccd682de9 100644
--- a/drivers/i2c/busses/i2c-efm32.c
+++ b/drivers/i2c/busses/i2c-efm32.c
@@ -320,10 +320,8 @@ static int efm32_i2c_probe(struct platform_device *pdev)
320 return -EINVAL; 320 return -EINVAL;
321 321
322 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); 322 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
323 if (!ddata) { 323 if (!ddata)
324 dev_dbg(&pdev->dev, "failed to allocate private data\n");
325 return -ENOMEM; 324 return -ENOMEM;
326 }
327 platform_set_drvdata(pdev, ddata); 325 platform_set_drvdata(pdev, ddata);
328 326
329 init_completion(&ddata->done); 327 init_completion(&ddata->done);
diff --git a/drivers/i2c/busses/i2c-eg20t.c b/drivers/i2c/busses/i2c-eg20t.c
index ff775ac29e49..a44ea13d1434 100644
--- a/drivers/i2c/busses/i2c-eg20t.c
+++ b/drivers/i2c/busses/i2c-eg20t.c
@@ -751,10 +751,8 @@ static int pch_i2c_probe(struct pci_dev *pdev,
751 pch_pci_dbg(pdev, "Entered.\n"); 751 pch_pci_dbg(pdev, "Entered.\n");
752 752
753 adap_info = kzalloc((sizeof(struct adapter_info)), GFP_KERNEL); 753 adap_info = kzalloc((sizeof(struct adapter_info)), GFP_KERNEL);
754 if (adap_info == NULL) { 754 if (adap_info == NULL)
755 pch_pci_err(pdev, "Memory allocation FAILED\n");
756 return -ENOMEM; 755 return -ENOMEM;
757 }
758 756
759 ret = pci_enable_device(pdev); 757 ret = pci_enable_device(pdev);
760 if (ret) { 758 if (ret) {
diff --git a/drivers/i2c/busses/i2c-exynos5.c b/drivers/i2c/busses/i2c-exynos5.c
index 00af0a0a3361..63d229202854 100644
--- a/drivers/i2c/busses/i2c-exynos5.c
+++ b/drivers/i2c/busses/i2c-exynos5.c
@@ -76,12 +76,6 @@
76#define HSI2C_RXFIFO_TRIGGER_LEVEL(x) ((x) << 4) 76#define HSI2C_RXFIFO_TRIGGER_LEVEL(x) ((x) << 4)
77#define HSI2C_TXFIFO_TRIGGER_LEVEL(x) ((x) << 16) 77#define HSI2C_TXFIFO_TRIGGER_LEVEL(x) ((x) << 16)
78 78
79/* As per user manual FIFO max depth is 64bytes */
80#define HSI2C_FIFO_MAX 0x40
81/* default trigger levels for Tx and Rx FIFOs */
82#define HSI2C_DEF_TXFIFO_LVL (HSI2C_FIFO_MAX - 0x30)
83#define HSI2C_DEF_RXFIFO_LVL (HSI2C_FIFO_MAX - 0x10)
84
85/* I2C_TRAILING_CTL Register bits */ 79/* I2C_TRAILING_CTL Register bits */
86#define HSI2C_TRAILING_COUNT (0xf) 80#define HSI2C_TRAILING_COUNT (0xf)
87 81
@@ -183,14 +177,54 @@ struct exynos5_i2c {
183 * 2. Fast speed upto 1Mbps 177 * 2. Fast speed upto 1Mbps
184 */ 178 */
185 int speed_mode; 179 int speed_mode;
180
181 /* Version of HS-I2C Hardware */
182 struct exynos_hsi2c_variant *variant;
183};
184
185/**
186 * struct exynos_hsi2c_variant - platform specific HSI2C driver data
187 * @fifo_depth: the fifo depth supported by the HSI2C module
188 *
189 * Specifies platform specific configuration of HSI2C module.
190 * Note: A structure for driver specific platform data is used for future
191 * expansion of its usage.
192 */
193struct exynos_hsi2c_variant {
194 unsigned int fifo_depth;
195};
196
197static const struct exynos_hsi2c_variant exynos5250_hsi2c_data = {
198 .fifo_depth = 64,
199};
200
201static const struct exynos_hsi2c_variant exynos5260_hsi2c_data = {
202 .fifo_depth = 16,
186}; 203};
187 204
188static const struct of_device_id exynos5_i2c_match[] = { 205static const struct of_device_id exynos5_i2c_match[] = {
189 { .compatible = "samsung,exynos5-hsi2c" }, 206 {
190 {}, 207 .compatible = "samsung,exynos5-hsi2c",
208 .data = &exynos5250_hsi2c_data
209 }, {
210 .compatible = "samsung,exynos5250-hsi2c",
211 .data = &exynos5250_hsi2c_data
212 }, {
213 .compatible = "samsung,exynos5260-hsi2c",
214 .data = &exynos5260_hsi2c_data
215 }, {},
191}; 216};
192MODULE_DEVICE_TABLE(of, exynos5_i2c_match); 217MODULE_DEVICE_TABLE(of, exynos5_i2c_match);
193 218
219static inline struct exynos_hsi2c_variant *exynos5_i2c_get_variant
220 (struct platform_device *pdev)
221{
222 const struct of_device_id *match;
223
224 match = of_match_node(exynos5_i2c_match, pdev->dev.of_node);
225 return (struct exynos_hsi2c_variant *)match->data;
226}
227
194static void exynos5_i2c_clr_pend_irq(struct exynos5_i2c *i2c) 228static void exynos5_i2c_clr_pend_irq(struct exynos5_i2c *i2c)
195{ 229{
196 writel(readl(i2c->regs + HSI2C_INT_STATUS), 230 writel(readl(i2c->regs + HSI2C_INT_STATUS),
@@ -415,7 +449,7 @@ static irqreturn_t exynos5_i2c_irq(int irqno, void *dev_id)
415 fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS); 449 fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
416 fifo_level = HSI2C_TX_FIFO_LVL(fifo_status); 450 fifo_level = HSI2C_TX_FIFO_LVL(fifo_status);
417 451
418 len = HSI2C_FIFO_MAX - fifo_level; 452 len = i2c->variant->fifo_depth - fifo_level;
419 if (len > (i2c->msg->len - i2c->msg_ptr)) 453 if (len > (i2c->msg->len - i2c->msg_ptr))
420 len = i2c->msg->len - i2c->msg_ptr; 454 len = i2c->msg->len - i2c->msg_ptr;
421 455
@@ -483,6 +517,7 @@ static void exynos5_i2c_message_start(struct exynos5_i2c *i2c, int stop)
483 u32 i2c_auto_conf = 0; 517 u32 i2c_auto_conf = 0;
484 u32 fifo_ctl; 518 u32 fifo_ctl;
485 unsigned long flags; 519 unsigned long flags;
520 unsigned short trig_lvl;
486 521
487 i2c_ctl = readl(i2c->regs + HSI2C_CTL); 522 i2c_ctl = readl(i2c->regs + HSI2C_CTL);
488 i2c_ctl &= ~(HSI2C_TXCHON | HSI2C_RXCHON); 523 i2c_ctl &= ~(HSI2C_TXCHON | HSI2C_RXCHON);
@@ -493,13 +528,19 @@ static void exynos5_i2c_message_start(struct exynos5_i2c *i2c, int stop)
493 528
494 i2c_auto_conf = HSI2C_READ_WRITE; 529 i2c_auto_conf = HSI2C_READ_WRITE;
495 530
496 fifo_ctl |= HSI2C_RXFIFO_TRIGGER_LEVEL(HSI2C_DEF_TXFIFO_LVL); 531 trig_lvl = (i2c->msg->len > i2c->variant->fifo_depth) ?
532 (i2c->variant->fifo_depth * 3 / 4) : i2c->msg->len;
533 fifo_ctl |= HSI2C_RXFIFO_TRIGGER_LEVEL(trig_lvl);
534
497 int_en |= (HSI2C_INT_RX_ALMOSTFULL_EN | 535 int_en |= (HSI2C_INT_RX_ALMOSTFULL_EN |
498 HSI2C_INT_TRAILING_EN); 536 HSI2C_INT_TRAILING_EN);
499 } else { 537 } else {
500 i2c_ctl |= HSI2C_TXCHON; 538 i2c_ctl |= HSI2C_TXCHON;
501 539
502 fifo_ctl |= HSI2C_TXFIFO_TRIGGER_LEVEL(HSI2C_DEF_RXFIFO_LVL); 540 trig_lvl = (i2c->msg->len > i2c->variant->fifo_depth) ?
541 (i2c->variant->fifo_depth * 1 / 4) : i2c->msg->len;
542 fifo_ctl |= HSI2C_TXFIFO_TRIGGER_LEVEL(trig_lvl);
543
503 int_en |= HSI2C_INT_TX_ALMOSTEMPTY_EN; 544 int_en |= HSI2C_INT_TX_ALMOSTEMPTY_EN;
504 } 545 }
505 546
@@ -621,10 +662,8 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
621 int ret; 662 int ret;
622 663
623 i2c = devm_kzalloc(&pdev->dev, sizeof(struct exynos5_i2c), GFP_KERNEL); 664 i2c = devm_kzalloc(&pdev->dev, sizeof(struct exynos5_i2c), GFP_KERNEL);
624 if (!i2c) { 665 if (!i2c)
625 dev_err(&pdev->dev, "no memory for state\n");
626 return -ENOMEM; 666 return -ENOMEM;
627 }
628 667
629 if (of_property_read_u32(np, "clock-frequency", &op_clock)) { 668 if (of_property_read_u32(np, "clock-frequency", &op_clock)) {
630 i2c->speed_mode = HSI2C_FAST_SPD; 669 i2c->speed_mode = HSI2C_FAST_SPD;
@@ -691,7 +730,9 @@ static int exynos5_i2c_probe(struct platform_device *pdev)
691 if (ret) 730 if (ret)
692 goto err_clk; 731 goto err_clk;
693 732
694 exynos5_i2c_init(i2c); 733 i2c->variant = exynos5_i2c_get_variant(pdev);
734
735 exynos5_i2c_reset(i2c);
695 736
696 ret = i2c_add_adapter(&i2c->adap); 737 ret = i2c_add_adapter(&i2c->adap);
697 if (ret < 0) { 738 if (ret < 0) {
diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c
index 02d2d4abb9dd..71a45b210a24 100644
--- a/drivers/i2c/busses/i2c-gpio.c
+++ b/drivers/i2c/busses/i2c-gpio.c
@@ -147,24 +147,22 @@ static int i2c_gpio_probe(struct platform_device *pdev)
147 scl_pin = pdata->scl_pin; 147 scl_pin = pdata->scl_pin;
148 } 148 }
149 149
150 ret = gpio_request(sda_pin, "sda"); 150 ret = devm_gpio_request(&pdev->dev, sda_pin, "sda");
151 if (ret) { 151 if (ret) {
152 if (ret == -EINVAL) 152 if (ret == -EINVAL)
153 ret = -EPROBE_DEFER; /* Try again later */ 153 ret = -EPROBE_DEFER; /* Try again later */
154 goto err_request_sda; 154 return ret;
155 } 155 }
156 ret = gpio_request(scl_pin, "scl"); 156 ret = devm_gpio_request(&pdev->dev, scl_pin, "scl");
157 if (ret) { 157 if (ret) {
158 if (ret == -EINVAL) 158 if (ret == -EINVAL)
159 ret = -EPROBE_DEFER; /* Try again later */ 159 ret = -EPROBE_DEFER; /* Try again later */
160 goto err_request_scl; 160 return ret;
161 } 161 }
162 162
163 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 163 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
164 if (!priv) { 164 if (!priv)
165 ret = -ENOMEM; 165 return -ENOMEM;
166 goto err_add_bus;
167 }
168 adap = &priv->adap; 166 adap = &priv->adap;
169 bit_data = &priv->bit_data; 167 bit_data = &priv->bit_data;
170 pdata = &priv->pdata; 168 pdata = &priv->pdata;
@@ -225,7 +223,7 @@ static int i2c_gpio_probe(struct platform_device *pdev)
225 adap->nr = pdev->id; 223 adap->nr = pdev->id;
226 ret = i2c_bit_add_numbered_bus(adap); 224 ret = i2c_bit_add_numbered_bus(adap);
227 if (ret) 225 if (ret)
228 goto err_add_bus; 226 return ret;
229 227
230 platform_set_drvdata(pdev, priv); 228 platform_set_drvdata(pdev, priv);
231 229
@@ -235,13 +233,6 @@ static int i2c_gpio_probe(struct platform_device *pdev)
235 ? ", no clock stretching" : ""); 233 ? ", no clock stretching" : "");
236 234
237 return 0; 235 return 0;
238
239err_add_bus:
240 gpio_free(scl_pin);
241err_request_scl:
242 gpio_free(sda_pin);
243err_request_sda:
244 return ret;
245} 236}
246 237
247static int i2c_gpio_remove(struct platform_device *pdev) 238static int i2c_gpio_remove(struct platform_device *pdev)
@@ -255,8 +246,6 @@ static int i2c_gpio_remove(struct platform_device *pdev)
255 pdata = &priv->pdata; 246 pdata = &priv->pdata;
256 247
257 i2c_del_adapter(adap); 248 i2c_del_adapter(adap);
258 gpio_free(pdata->scl_pin);
259 gpio_free(pdata->sda_pin);
260 249
261 return 0; 250 return 0;
262} 251}
diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index db895fb22e65..aa8bc146718b 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -183,6 +183,8 @@ struct imx_i2c_struct {
183 unsigned int disable_delay; 183 unsigned int disable_delay;
184 int stopped; 184 int stopped;
185 unsigned int ifdr; /* IMX_I2C_IFDR */ 185 unsigned int ifdr; /* IMX_I2C_IFDR */
186 unsigned int cur_clk;
187 unsigned int bitrate;
186 const struct imx_i2c_hwdata *hwdata; 188 const struct imx_i2c_hwdata *hwdata;
187}; 189};
188 190
@@ -305,6 +307,48 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
305 return 0; 307 return 0;
306} 308}
307 309
310static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx)
311{
312 struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
313 unsigned int i2c_clk_rate;
314 unsigned int div;
315 int i;
316
317 /* Divider value calculation */
318 i2c_clk_rate = clk_get_rate(i2c_imx->clk);
319 if (i2c_imx->cur_clk == i2c_clk_rate)
320 return;
321 else
322 i2c_imx->cur_clk = i2c_clk_rate;
323
324 div = (i2c_clk_rate + i2c_imx->bitrate - 1) / i2c_imx->bitrate;
325 if (div < i2c_clk_div[0].div)
326 i = 0;
327 else if (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div)
328 i = i2c_imx->hwdata->ndivs - 1;
329 else
330 for (i = 0; i2c_clk_div[i].div < div; i++);
331
332 /* Store divider value */
333 i2c_imx->ifdr = i2c_clk_div[i].val;
334
335 /*
336 * There dummy delay is calculated.
337 * It should be about one I2C clock period long.
338 * This delay is used in I2C bus disable function
339 * to fix chip hardware bug.
340 */
341 i2c_imx->disable_delay = (500000U * i2c_clk_div[i].div
342 + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
343
344#ifdef CONFIG_I2C_DEBUG_BUS
345 dev_dbg(&i2c_imx->adapter.dev, "I2C_CLK=%d, REQ DIV=%d\n",
346 i2c_clk_rate, div);
347 dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n",
348 i2c_clk_div[i].val, i2c_clk_div[i].div);
349#endif
350}
351
308static int i2c_imx_start(struct imx_i2c_struct *i2c_imx) 352static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
309{ 353{
310 unsigned int temp = 0; 354 unsigned int temp = 0;
@@ -312,6 +356,8 @@ static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
312 356
313 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); 357 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
314 358
359 i2c_imx_set_clk(i2c_imx);
360
315 result = clk_prepare_enable(i2c_imx->clk); 361 result = clk_prepare_enable(i2c_imx->clk);
316 if (result) 362 if (result)
317 return result; 363 return result;
@@ -367,45 +413,6 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
367 clk_disable_unprepare(i2c_imx->clk); 413 clk_disable_unprepare(i2c_imx->clk);
368} 414}
369 415
370static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
371 unsigned int rate)
372{
373 struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
374 unsigned int i2c_clk_rate;
375 unsigned int div;
376 int i;
377
378 /* Divider value calculation */
379 i2c_clk_rate = clk_get_rate(i2c_imx->clk);
380 div = (i2c_clk_rate + rate - 1) / rate;
381 if (div < i2c_clk_div[0].div)
382 i = 0;
383 else if (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div)
384 i = i2c_imx->hwdata->ndivs - 1;
385 else
386 for (i = 0; i2c_clk_div[i].div < div; i++);
387
388 /* Store divider value */
389 i2c_imx->ifdr = i2c_clk_div[i].val;
390
391 /*
392 * There dummy delay is calculated.
393 * It should be about one I2C clock period long.
394 * This delay is used in I2C bus disable function
395 * to fix chip hardware bug.
396 */
397 i2c_imx->disable_delay = (500000U * i2c_clk_div[i].div
398 + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
399
400 /* dev_dbg() can't be used, because adapter is not yet registered */
401#ifdef CONFIG_I2C_DEBUG_BUS
402 dev_dbg(&i2c_imx->adapter.dev, "<%s> I2C_CLK=%d, REQ DIV=%d\n",
403 __func__, i2c_clk_rate, div);
404 dev_dbg(&i2c_imx->adapter.dev, "<%s> IFDR[IC]=0x%x, REAL DIV=%d\n",
405 __func__, i2c_clk_div[i].val, i2c_clk_div[i].div);
406#endif
407}
408
409static irqreturn_t i2c_imx_isr(int irq, void *dev_id) 416static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
410{ 417{
411 struct imx_i2c_struct *i2c_imx = dev_id; 418 struct imx_i2c_struct *i2c_imx = dev_id;
@@ -458,10 +465,11 @@ static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
458 return 0; 465 return 0;
459} 466}
460 467
461static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs) 468static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg)
462{ 469{
463 int i, result; 470 int i, result;
464 unsigned int temp; 471 unsigned int temp;
472 int block_data = msgs->flags & I2C_M_RECV_LEN;
465 473
466 dev_dbg(&i2c_imx->adapter.dev, 474 dev_dbg(&i2c_imx->adapter.dev,
467 "<%s> write slave address: addr=0x%x\n", 475 "<%s> write slave address: addr=0x%x\n",
@@ -481,7 +489,12 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
481 /* setup bus to read data */ 489 /* setup bus to read data */
482 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); 490 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
483 temp &= ~I2CR_MTX; 491 temp &= ~I2CR_MTX;
484 if (msgs->len - 1) 492
493 /*
494 * Reset the I2CR_TXAK flag initially for SMBus block read since the
495 * length is unknown
496 */
497 if ((msgs->len - 1) || block_data)
485 temp &= ~I2CR_TXAK; 498 temp &= ~I2CR_TXAK;
486 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); 499 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
487 imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */ 500 imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */
@@ -490,19 +503,49 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
490 503
491 /* read data */ 504 /* read data */
492 for (i = 0; i < msgs->len; i++) { 505 for (i = 0; i < msgs->len; i++) {
506 u8 len = 0;
493 result = i2c_imx_trx_complete(i2c_imx); 507 result = i2c_imx_trx_complete(i2c_imx);
494 if (result) 508 if (result)
495 return result; 509 return result;
496 if (i == (msgs->len - 1)) { 510 /*
497 /* It must generate STOP before read I2DR to prevent 511 * First byte is the length of remaining packet
498 controller from generating another clock cycle */ 512 * in the SMBus block data read. Add it to
513 * msgs->len.
514 */
515 if ((!i) && block_data) {
516 len = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
517 if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX))
518 return -EPROTO;
499 dev_dbg(&i2c_imx->adapter.dev, 519 dev_dbg(&i2c_imx->adapter.dev,
500 "<%s> clear MSTA\n", __func__); 520 "<%s> read length: 0x%X\n",
501 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); 521 __func__, len);
502 temp &= ~(I2CR_MSTA | I2CR_MTX); 522 msgs->len += len;
503 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); 523 }
504 i2c_imx_bus_busy(i2c_imx, 0); 524 if (i == (msgs->len - 1)) {
505 i2c_imx->stopped = 1; 525 if (is_lastmsg) {
526 /*
527 * It must generate STOP before read I2DR to prevent
528 * controller from generating another clock cycle
529 */
530 dev_dbg(&i2c_imx->adapter.dev,
531 "<%s> clear MSTA\n", __func__);
532 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
533 temp &= ~(I2CR_MSTA | I2CR_MTX);
534 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
535 i2c_imx_bus_busy(i2c_imx, 0);
536 i2c_imx->stopped = 1;
537 } else {
538 /*
539 * For i2c master receiver repeat restart operation like:
540 * read -> repeat MSTA -> read/write
541 * The controller must set MTX before read the last byte in
542 * the first read operation, otherwise the first read cost
543 * one extra clock cycle.
544 */
545 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
546 temp |= I2CR_MTX;
547 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
548 }
506 } else if (i == (msgs->len - 2)) { 549 } else if (i == (msgs->len - 2)) {
507 dev_dbg(&i2c_imx->adapter.dev, 550 dev_dbg(&i2c_imx->adapter.dev,
508 "<%s> set TXAK\n", __func__); 551 "<%s> set TXAK\n", __func__);
@@ -510,7 +553,10 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
510 temp |= I2CR_TXAK; 553 temp |= I2CR_TXAK;
511 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); 554 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
512 } 555 }
513 msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); 556 if ((!i) && block_data)
557 msgs->buf[0] = len;
558 else
559 msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
514 dev_dbg(&i2c_imx->adapter.dev, 560 dev_dbg(&i2c_imx->adapter.dev,
515 "<%s> read byte: B%d=0x%X\n", 561 "<%s> read byte: B%d=0x%X\n",
516 __func__, i, msgs->buf[i]); 562 __func__, i, msgs->buf[i]);
@@ -523,6 +569,7 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter,
523{ 569{
524 unsigned int i, temp; 570 unsigned int i, temp;
525 int result; 571 int result;
572 bool is_lastmsg = false;
526 struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter); 573 struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
527 574
528 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); 575 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
@@ -534,6 +581,9 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter,
534 581
535 /* read/write data */ 582 /* read/write data */
536 for (i = 0; i < num; i++) { 583 for (i = 0; i < num; i++) {
584 if (i == num - 1)
585 is_lastmsg = true;
586
537 if (i) { 587 if (i) {
538 dev_dbg(&i2c_imx->adapter.dev, 588 dev_dbg(&i2c_imx->adapter.dev,
539 "<%s> repeated start\n", __func__); 589 "<%s> repeated start\n", __func__);
@@ -564,7 +614,7 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter,
564 (temp & I2SR_RXAK ? 1 : 0)); 614 (temp & I2SR_RXAK ? 1 : 0));
565#endif 615#endif
566 if (msgs[i].flags & I2C_M_RD) 616 if (msgs[i].flags & I2C_M_RD)
567 result = i2c_imx_read(i2c_imx, &msgs[i]); 617 result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg);
568 else 618 else
569 result = i2c_imx_write(i2c_imx, &msgs[i]); 619 result = i2c_imx_write(i2c_imx, &msgs[i]);
570 if (result) 620 if (result)
@@ -583,7 +633,8 @@ fail0:
583 633
584static u32 i2c_imx_func(struct i2c_adapter *adapter) 634static u32 i2c_imx_func(struct i2c_adapter *adapter)
585{ 635{
586 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 636 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
637 | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
587} 638}
588 639
589static struct i2c_algorithm i2c_imx_algo = { 640static struct i2c_algorithm i2c_imx_algo = {
@@ -600,7 +651,6 @@ static int i2c_imx_probe(struct platform_device *pdev)
600 struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev); 651 struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
601 void __iomem *base; 652 void __iomem *base;
602 int irq, ret; 653 int irq, ret;
603 u32 bitrate;
604 654
605 dev_dbg(&pdev->dev, "<%s>\n", __func__); 655 dev_dbg(&pdev->dev, "<%s>\n", __func__);
606 656
@@ -617,10 +667,8 @@ static int i2c_imx_probe(struct platform_device *pdev)
617 667
618 i2c_imx = devm_kzalloc(&pdev->dev, sizeof(struct imx_i2c_struct), 668 i2c_imx = devm_kzalloc(&pdev->dev, sizeof(struct imx_i2c_struct),
619 GFP_KERNEL); 669 GFP_KERNEL);
620 if (!i2c_imx) { 670 if (!i2c_imx)
621 dev_err(&pdev->dev, "can't allocate interface\n");
622 return -ENOMEM; 671 return -ENOMEM;
623 }
624 672
625 if (of_id) 673 if (of_id)
626 i2c_imx->hwdata = of_id->data; 674 i2c_imx->hwdata = of_id->data;
@@ -664,12 +712,11 @@ static int i2c_imx_probe(struct platform_device *pdev)
664 i2c_set_adapdata(&i2c_imx->adapter, i2c_imx); 712 i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
665 713
666 /* Set up clock divider */ 714 /* Set up clock divider */
667 bitrate = IMX_I2C_BIT_RATE; 715 i2c_imx->bitrate = IMX_I2C_BIT_RATE;
668 ret = of_property_read_u32(pdev->dev.of_node, 716 ret = of_property_read_u32(pdev->dev.of_node,
669 "clock-frequency", &bitrate); 717 "clock-frequency", &i2c_imx->bitrate);
670 if (ret < 0 && pdata && pdata->bitrate) 718 if (ret < 0 && pdata && pdata->bitrate)
671 bitrate = pdata->bitrate; 719 i2c_imx->bitrate = pdata->bitrate;
672 i2c_imx_set_clk(i2c_imx, bitrate);
673 720
674 /* Set up chip registers to defaults */ 721 /* Set up chip registers to defaults */
675 imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN, 722 imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
diff --git a/drivers/i2c/busses/i2c-mpc.c b/drivers/i2c/busses/i2c-mpc.c
index f5391633b53a..6a32aa095f83 100644
--- a/drivers/i2c/busses/i2c-mpc.c
+++ b/drivers/i2c/busses/i2c-mpc.c
@@ -115,7 +115,7 @@ static void mpc_i2c_fixup(struct mpc_i2c *i2c)
115 for (k = 9; k; k--) { 115 for (k = 9; k; k--) {
116 writeccr(i2c, 0); 116 writeccr(i2c, 0);
117 writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN); 117 writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
118 udelay(delay_val); 118 readb(i2c->base + MPC_I2C_DR);
119 writeccr(i2c, CCR_MEN); 119 writeccr(i2c, CCR_MEN);
120 udelay(delay_val << 1); 120 udelay(delay_val << 1);
121 } 121 }
diff --git a/drivers/i2c/busses/i2c-mv64xxx.c b/drivers/i2c/busses/i2c-mv64xxx.c
index 540ea692bf79..9f4b775e2e39 100644
--- a/drivers/i2c/busses/i2c-mv64xxx.c
+++ b/drivers/i2c/busses/i2c-mv64xxx.c
@@ -681,7 +681,7 @@ static const struct i2c_algorithm mv64xxx_i2c_algo = {
681 ***************************************************************************** 681 *****************************************************************************
682 */ 682 */
683static const struct of_device_id mv64xxx_i2c_of_match_table[] = { 683static const struct of_device_id mv64xxx_i2c_of_match_table[] = {
684 { .compatible = "allwinner,sun4i-i2c", .data = &mv64xxx_i2c_regs_sun4i}, 684 { .compatible = "allwinner,sun4i-a10-i2c", .data = &mv64xxx_i2c_regs_sun4i},
685 { .compatible = "allwinner,sun6i-a31-i2c", .data = &mv64xxx_i2c_regs_sun4i}, 685 { .compatible = "allwinner,sun6i-a31-i2c", .data = &mv64xxx_i2c_regs_sun4i},
686 { .compatible = "marvell,mv64xxx-i2c", .data = &mv64xxx_i2c_regs_mv64xxx}, 686 { .compatible = "marvell,mv64xxx-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
687 { .compatible = "marvell,mv78230-i2c", .data = &mv64xxx_i2c_regs_mv64xxx}, 687 { .compatible = "marvell,mv78230-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
diff --git a/drivers/i2c/busses/i2c-nomadik.c b/drivers/i2c/busses/i2c-nomadik.c
index 32c85e9ecdae..0e55d85fd4ed 100644
--- a/drivers/i2c/busses/i2c-nomadik.c
+++ b/drivers/i2c/busses/i2c-nomadik.c
@@ -879,19 +879,19 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg)
879#ifdef CONFIG_PM_SLEEP 879#ifdef CONFIG_PM_SLEEP
880static int nmk_i2c_suspend_late(struct device *dev) 880static int nmk_i2c_suspend_late(struct device *dev)
881{ 881{
882 pinctrl_pm_select_sleep_state(dev); 882 int ret;
883 883
884 ret = pm_runtime_force_suspend(dev);
885 if (ret)
886 return ret;
887
888 pinctrl_pm_select_sleep_state(dev);
884 return 0; 889 return 0;
885} 890}
886 891
887static int nmk_i2c_resume_early(struct device *dev) 892static int nmk_i2c_resume_early(struct device *dev)
888{ 893{
889 /* First go to the default state */ 894 return pm_runtime_force_resume(dev);
890 pinctrl_pm_select_default_state(dev);
891 /* Then let's idle the pins until the next transfer happens */
892 pinctrl_pm_select_idle_state(dev);
893
894 return 0;
895} 895}
896#endif 896#endif
897 897
diff --git a/drivers/i2c/busses/i2c-nuc900.c b/drivers/i2c/busses/i2c-nuc900.c
deleted file mode 100644
index 36394d737faf..000000000000
--- a/drivers/i2c/busses/i2c-nuc900.c
+++ /dev/null
@@ -1,709 +0,0 @@
1/*
2 * linux/drivers/i2c/busses/i2c-nuc900.c
3 *
4 * Copyright (c) 2010 Nuvoton technology corporation.
5 *
6 * This driver based on S3C2410 I2C driver of Ben Dooks <ben-Y5A6D6n0/KfQXOPxS62xeg@public.gmane.org>.
7 * Written by Wan ZongShun <mcuos.com-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation;version 2 of the License.
12 *
13 */
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17
18#include <linux/i2c.h>
19#include <linux/init.h>
20#include <linux/time.h>
21#include <linux/interrupt.h>
22#include <linux/delay.h>
23#include <linux/errno.h>
24#include <linux/err.h>
25#include <linux/platform_device.h>
26#include <linux/clk.h>
27#include <linux/cpufreq.h>
28#include <linux/slab.h>
29#include <linux/io.h>
30
31#include <mach/mfp.h>
32#include <linux/platform_data/i2c-nuc900.h>
33
34/* nuc900 i2c registers offset */
35
36#define CSR 0x00
37#define DIVIDER 0x04
38#define CMDR 0x08
39#define SWR 0x0C
40#define RXR 0x10
41#define TXR 0x14
42
43/* nuc900 i2c CSR register bits */
44
45#define IRQEN 0x003
46#define I2CBUSY 0x400
47#define I2CSTART 0x018
48#define IRQFLAG 0x004
49#define ARBIT_LOST 0x200
50#define SLAVE_ACK 0x800
51
52/* nuc900 i2c CMDR register bits */
53
54#define I2C_CMD_START 0x10
55#define I2C_CMD_STOP 0x08
56#define I2C_CMD_READ 0x04
57#define I2C_CMD_WRITE 0x02
58#define I2C_CMD_NACK 0x01
59
60/* i2c controller state */
61
62enum nuc900_i2c_state {
63 STATE_IDLE,
64 STATE_START,
65 STATE_READ,
66 STATE_WRITE,
67 STATE_STOP
68};
69
70/* i2c controller private data */
71
72struct nuc900_i2c {
73 spinlock_t lock;
74 wait_queue_head_t wait;
75
76 struct i2c_msg *msg;
77 unsigned int msg_num;
78 unsigned int msg_idx;
79 unsigned int msg_ptr;
80 unsigned int irq;
81
82 enum nuc900_i2c_state state;
83
84 void __iomem *regs;
85 struct clk *clk;
86 struct device *dev;
87 struct resource *ioarea;
88 struct i2c_adapter adap;
89};
90
91/* nuc900_i2c_master_complete
92 *
93 * complete the message and wake up the caller, using the given return code,
94 * or zero to mean ok.
95*/
96
97static inline void nuc900_i2c_master_complete(struct nuc900_i2c *i2c, int ret)
98{
99 dev_dbg(i2c->dev, "master_complete %d\n", ret);
100
101 i2c->msg_ptr = 0;
102 i2c->msg = NULL;
103 i2c->msg_idx++;
104 i2c->msg_num = 0;
105 if (ret)
106 i2c->msg_idx = ret;
107
108 wake_up(&i2c->wait);
109}
110
111/* irq enable/disable functions */
112
113static inline void nuc900_i2c_disable_irq(struct nuc900_i2c *i2c)
114{
115 unsigned long tmp;
116
117 tmp = readl(i2c->regs + CSR);
118 writel(tmp & ~IRQEN, i2c->regs + CSR);
119}
120
121static inline void nuc900_i2c_enable_irq(struct nuc900_i2c *i2c)
122{
123 unsigned long tmp;
124
125 tmp = readl(i2c->regs + CSR);
126 writel(tmp | IRQEN, i2c->regs + CSR);
127}
128
129
130/* nuc900_i2c_message_start
131 *
132 * put the start of a message onto the bus
133*/
134
135static void nuc900_i2c_message_start(struct nuc900_i2c *i2c,
136 struct i2c_msg *msg)
137{
138 unsigned int addr = (msg->addr & 0x7f) << 1;
139
140 if (msg->flags & I2C_M_RD)
141 addr |= 0x1;
142 writel(addr & 0xff, i2c->regs + TXR);
143 writel(I2C_CMD_START | I2C_CMD_WRITE, i2c->regs + CMDR);
144}
145
146static inline void nuc900_i2c_stop(struct nuc900_i2c *i2c, int ret)
147{
148
149 dev_dbg(i2c->dev, "STOP\n");
150
151 /* stop the transfer */
152 i2c->state = STATE_STOP;
153 writel(I2C_CMD_STOP, i2c->regs + CMDR);
154
155 nuc900_i2c_master_complete(i2c, ret);
156 nuc900_i2c_disable_irq(i2c);
157}
158
159/* helper functions to determine the current state in the set of
160 * messages we are sending
161*/
162
163/* is_lastmsg()
164 *
165 * returns TRUE if the current message is the last in the set
166*/
167
168static inline int is_lastmsg(struct nuc900_i2c *i2c)
169{
170 return i2c->msg_idx >= (i2c->msg_num - 1);
171}
172
173/* is_msglast
174 *
175 * returns TRUE if we this is the last byte in the current message
176*/
177
178static inline int is_msglast(struct nuc900_i2c *i2c)
179{
180 return i2c->msg_ptr == i2c->msg->len-1;
181}
182
183/* is_msgend
184 *
185 * returns TRUE if we reached the end of the current message
186*/
187
188static inline int is_msgend(struct nuc900_i2c *i2c)
189{
190 return i2c->msg_ptr >= i2c->msg->len;
191}
192
193/* i2c_nuc900_irq_nextbyte
194 *
195 * process an interrupt and work out what to do
196 */
197
198static void i2c_nuc900_irq_nextbyte(struct nuc900_i2c *i2c,
199 unsigned long iicstat)
200{
201 unsigned char byte;
202
203 switch (i2c->state) {
204
205 case STATE_IDLE:
206 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
207 break;
208
209 case STATE_STOP:
210 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
211 nuc900_i2c_disable_irq(i2c);
212 break;
213
214 case STATE_START:
215 /* last thing we did was send a start condition on the
216 * bus, or started a new i2c message
217 */
218
219 if (iicstat & SLAVE_ACK &&
220 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
221 /* ack was not received... */
222
223 dev_dbg(i2c->dev, "ack was not received\n");
224 nuc900_i2c_stop(i2c, -ENXIO);
225 break;
226 }
227
228 if (i2c->msg->flags & I2C_M_RD)
229 i2c->state = STATE_READ;
230 else
231 i2c->state = STATE_WRITE;
232
233 /* terminate the transfer if there is nothing to do
234 * as this is used by the i2c probe to find devices.
235 */
236
237 if (is_lastmsg(i2c) && i2c->msg->len == 0) {
238 nuc900_i2c_stop(i2c, 0);
239 break;
240 }
241
242 if (i2c->state == STATE_READ)
243 goto prepare_read;
244
245 /* fall through to the write state, as we will need to
246 * send a byte as well
247 */
248
249 case STATE_WRITE:
250 /* we are writing data to the device... check for the
251 * end of the message, and if so, work out what to do
252 */
253
254 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
255 if (iicstat & SLAVE_ACK) {
256 dev_dbg(i2c->dev, "WRITE: No Ack\n");
257
258 nuc900_i2c_stop(i2c, -ECONNREFUSED);
259 break;
260 }
261 }
262
263retry_write:
264
265 if (!is_msgend(i2c)) {
266 byte = i2c->msg->buf[i2c->msg_ptr++];
267 writeb(byte, i2c->regs + TXR);
268 writel(I2C_CMD_WRITE, i2c->regs + CMDR);
269
270 } else if (!is_lastmsg(i2c)) {
271 /* we need to go to the next i2c message */
272
273 dev_dbg(i2c->dev, "WRITE: Next Message\n");
274
275 i2c->msg_ptr = 0;
276 i2c->msg_idx++;
277 i2c->msg++;
278
279 /* check to see if we need to do another message */
280 if (i2c->msg->flags & I2C_M_NOSTART) {
281
282 if (i2c->msg->flags & I2C_M_RD) {
283 /* cannot do this, the controller
284 * forces us to send a new START
285 * when we change direction
286 */
287
288 nuc900_i2c_stop(i2c, -EINVAL);
289 }
290
291 goto retry_write;
292 } else {
293 /* send the new start */
294 nuc900_i2c_message_start(i2c, i2c->msg);
295 i2c->state = STATE_START;
296 }
297
298 } else {
299 /* send stop */
300
301 nuc900_i2c_stop(i2c, 0);
302 }
303 break;
304
305 case STATE_READ:
306 /* we have a byte of data in the data register, do
307 * something with it, and then work out whether we are
308 * going to do any more read/write
309 */
310
311 byte = readb(i2c->regs + RXR);
312 i2c->msg->buf[i2c->msg_ptr++] = byte;
313
314prepare_read:
315 if (is_msglast(i2c)) {
316 /* last byte of buffer */
317
318 if (is_lastmsg(i2c))
319 writel(I2C_CMD_READ | I2C_CMD_NACK,
320 i2c->regs + CMDR);
321
322 } else if (is_msgend(i2c)) {
323 /* ok, we've read the entire buffer, see if there
324 * is anything else we need to do
325 */
326
327 if (is_lastmsg(i2c)) {
328 /* last message, send stop and complete */
329 dev_dbg(i2c->dev, "READ: Send Stop\n");
330
331 nuc900_i2c_stop(i2c, 0);
332 } else {
333 /* go to the next transfer */
334 dev_dbg(i2c->dev, "READ: Next Transfer\n");
335
336 i2c->msg_ptr = 0;
337 i2c->msg_idx++;
338 i2c->msg++;
339
340 writel(I2C_CMD_READ, i2c->regs + CMDR);
341 }
342
343 } else {
344 writel(I2C_CMD_READ, i2c->regs + CMDR);
345 }
346
347 break;
348 }
349}
350
351/* nuc900_i2c_irq
352 *
353 * top level IRQ servicing routine
354*/
355
356static irqreturn_t nuc900_i2c_irq(int irqno, void *dev_id)
357{
358 struct nuc900_i2c *i2c = dev_id;
359 unsigned long status;
360
361 status = readl(i2c->regs + CSR);
362 writel(status | IRQFLAG, i2c->regs + CSR);
363
364 if (status & ARBIT_LOST) {
365 /* deal with arbitration loss */
366 dev_err(i2c->dev, "deal with arbitration loss\n");
367 goto out;
368 }
369
370 if (i2c->state == STATE_IDLE) {
371 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
372 goto out;
373 }
374
375 /* pretty much this leaves us with the fact that we've
376 * transmitted or received whatever byte we last sent
377 */
378
379 i2c_nuc900_irq_nextbyte(i2c, status);
380
381 out:
382 return IRQ_HANDLED;
383}
384
385
386/* nuc900_i2c_set_master
387 *
388 * get the i2c bus for a master transaction
389*/
390
391static int nuc900_i2c_set_master(struct nuc900_i2c *i2c)
392{
393 int timeout = 400;
394
395 while (timeout-- > 0) {
396 if (((readl(i2c->regs + SWR) & I2CSTART) == I2CSTART) &&
397 ((readl(i2c->regs + CSR) & I2CBUSY) == 0)) {
398 return 0;
399 }
400
401 msleep(1);
402 }
403
404 return -ETIMEDOUT;
405}
406
407/* nuc900_i2c_doxfer
408 *
409 * this starts an i2c transfer
410*/
411
412static int nuc900_i2c_doxfer(struct nuc900_i2c *i2c,
413 struct i2c_msg *msgs, int num)
414{
415 unsigned long iicstat, timeout;
416 int spins = 20;
417 int ret;
418
419 ret = nuc900_i2c_set_master(i2c);
420 if (ret != 0) {
421 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
422 ret = -EAGAIN;
423 goto out;
424 }
425
426 spin_lock_irq(&i2c->lock);
427
428 i2c->msg = msgs;
429 i2c->msg_num = num;
430 i2c->msg_ptr = 0;
431 i2c->msg_idx = 0;
432 i2c->state = STATE_START;
433
434 nuc900_i2c_message_start(i2c, msgs);
435 spin_unlock_irq(&i2c->lock);
436
437 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
438
439 ret = i2c->msg_idx;
440
441 /* having these next two as dev_err() makes life very
442 * noisy when doing an i2cdetect
443 */
444
445 if (timeout == 0)
446 dev_dbg(i2c->dev, "timeout\n");
447 else if (ret != num)
448 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
449
450 /* ensure the stop has been through the bus */
451
452 dev_dbg(i2c->dev, "waiting for bus idle\n");
453
454 /* first, try busy waiting briefly */
455 do {
456 iicstat = readl(i2c->regs + CSR);
457 } while ((iicstat & I2CBUSY) && --spins);
458
459 /* if that timed out sleep */
460 if (!spins) {
461 msleep(1);
462 iicstat = readl(i2c->regs + CSR);
463 }
464
465 if (iicstat & I2CBUSY)
466 dev_warn(i2c->dev, "timeout waiting for bus idle\n");
467
468 out:
469 return ret;
470}
471
472/* nuc900_i2c_xfer
473 *
474 * first port of call from the i2c bus code when an message needs
475 * transferring across the i2c bus.
476*/
477
478static int nuc900_i2c_xfer(struct i2c_adapter *adap,
479 struct i2c_msg *msgs, int num)
480{
481 struct nuc900_i2c *i2c = (struct nuc900_i2c *)adap->algo_data;
482 int retry;
483 int ret;
484
485 nuc900_i2c_enable_irq(i2c);
486
487 for (retry = 0; retry < adap->retries; retry++) {
488
489 ret = nuc900_i2c_doxfer(i2c, msgs, num);
490
491 if (ret != -EAGAIN)
492 return ret;
493
494 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
495
496 udelay(100);
497 }
498
499 return -EREMOTEIO;
500}
501
502/* declare our i2c functionality */
503static u32 nuc900_i2c_func(struct i2c_adapter *adap)
504{
505 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
506 I2C_FUNC_PROTOCOL_MANGLING;
507}
508
509/* i2c bus registration info */
510
511static const struct i2c_algorithm nuc900_i2c_algorithm = {
512 .master_xfer = nuc900_i2c_xfer,
513 .functionality = nuc900_i2c_func,
514};
515
516/* nuc900_i2c_probe
517 *
518 * called by the bus driver when a suitable device is found
519*/
520
521static int nuc900_i2c_probe(struct platform_device *pdev)
522{
523 struct nuc900_i2c *i2c;
524 struct nuc900_platform_i2c *pdata;
525 struct resource *res;
526 int ret;
527
528 pdata = dev_get_platdata(&pdev->dev);
529 if (!pdata) {
530 dev_err(&pdev->dev, "no platform data\n");
531 return -EINVAL;
532 }
533
534 i2c = kzalloc(sizeof(struct nuc900_i2c), GFP_KERNEL);
535 if (!i2c) {
536 dev_err(&pdev->dev, "no memory for state\n");
537 return -ENOMEM;
538 }
539
540 strlcpy(i2c->adap.name, "nuc900-i2c0", sizeof(i2c->adap.name));
541 i2c->adap.owner = THIS_MODULE;
542 i2c->adap.algo = &nuc900_i2c_algorithm;
543 i2c->adap.retries = 2;
544 i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
545
546 spin_lock_init(&i2c->lock);
547 init_waitqueue_head(&i2c->wait);
548
549 /* find the clock and enable it */
550
551 i2c->dev = &pdev->dev;
552 i2c->clk = clk_get(&pdev->dev, NULL);
553 if (IS_ERR(i2c->clk)) {
554 dev_err(&pdev->dev, "cannot get clock\n");
555 ret = -ENOENT;
556 goto err_noclk;
557 }
558
559 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
560
561 clk_enable(i2c->clk);
562
563 /* map the registers */
564
565 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
566 if (res == NULL) {
567 dev_err(&pdev->dev, "cannot find IO resource\n");
568 ret = -ENOENT;
569 goto err_clk;
570 }
571
572 i2c->ioarea = request_mem_region(res->start, resource_size(res),
573 pdev->name);
574
575 if (i2c->ioarea == NULL) {
576 dev_err(&pdev->dev, "cannot request IO\n");
577 ret = -ENXIO;
578 goto err_clk;
579 }
580
581 i2c->regs = ioremap(res->start, resource_size(res));
582
583 if (i2c->regs == NULL) {
584 dev_err(&pdev->dev, "cannot map IO\n");
585 ret = -ENXIO;
586 goto err_ioarea;
587 }
588
589 dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
590 i2c->regs, i2c->ioarea, res);
591
592 /* setup info block for the i2c core */
593
594 i2c->adap.algo_data = i2c;
595 i2c->adap.dev.parent = &pdev->dev;
596
597 mfp_set_groupg(&pdev->dev, NULL);
598
599 clk_get_rate(i2c->clk);
600
601 ret = (i2c->clk.apbfreq)/(pdata->bus_freq * 5) - 1;
602 writel(ret & 0xffff, i2c->regs + DIVIDER);
603
604 /* find the IRQ for this unit (note, this relies on the init call to
605 * ensure no current IRQs pending
606 */
607
608 i2c->irq = ret = platform_get_irq(pdev, 0);
609 if (ret <= 0) {
610 dev_err(&pdev->dev, "cannot find IRQ\n");
611 goto err_iomap;
612 }
613
614 ret = request_irq(i2c->irq, nuc900_i2c_irq, IRQF_SHARED,
615 dev_name(&pdev->dev), i2c);
616
617 if (ret != 0) {
618 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
619 goto err_iomap;
620 }
621
622 /* Note, previous versions of the driver used i2c_add_adapter()
623 * to add the bus at any number. We now pass the bus number via
624 * the platform data, so if unset it will now default to always
625 * being bus 0.
626 */
627
628 i2c->adap.nr = pdata->bus_num;
629
630 ret = i2c_add_numbered_adapter(&i2c->adap);
631 if (ret < 0) {
632 dev_err(&pdev->dev, "failed to add bus to i2c core\n");
633 goto err_irq;
634 }
635
636 platform_set_drvdata(pdev, i2c);
637
638 dev_info(&pdev->dev, "%s: NUC900 I2C adapter\n",
639 dev_name(&i2c->adap.dev));
640 return 0;
641
642 err_irq:
643 free_irq(i2c->irq, i2c);
644
645 err_iomap:
646 iounmap(i2c->regs);
647
648 err_ioarea:
649 release_resource(i2c->ioarea);
650 kfree(i2c->ioarea);
651
652 err_clk:
653 clk_disable(i2c->clk);
654 clk_put(i2c->clk);
655
656 err_noclk:
657 kfree(i2c);
658 return ret;
659}
660
661/* nuc900_i2c_remove
662 *
663 * called when device is removed from the bus
664*/
665
666static int nuc900_i2c_remove(struct platform_device *pdev)
667{
668 struct nuc900_i2c *i2c = platform_get_drvdata(pdev);
669
670 i2c_del_adapter(&i2c->adap);
671 free_irq(i2c->irq, i2c);
672
673 clk_disable(i2c->clk);
674 clk_put(i2c->clk);
675
676 iounmap(i2c->regs);
677
678 release_resource(i2c->ioarea);
679 kfree(i2c->ioarea);
680 kfree(i2c);
681
682 return 0;
683}
684
685static struct platform_driver nuc900_i2c_driver = {
686 .probe = nuc900_i2c_probe,
687 .remove = nuc900_i2c_remove,
688 .driver = {
689 .owner = THIS_MODULE,
690 .name = "nuc900-i2c0",
691 },
692};
693
694static int __init i2c_adap_nuc900_init(void)
695{
696 return platform_driver_register(&nuc900_i2c_driver);
697}
698
699static void __exit i2c_adap_nuc900_exit(void)
700{
701 platform_driver_unregister(&nuc900_i2c_driver);
702}
703subsys_initcall(i2c_adap_nuc900_init);
704module_exit(i2c_adap_nuc900_exit);
705
706MODULE_DESCRIPTION("NUC900 I2C Bus driver");
707MODULE_AUTHOR("Wan ZongShun, <mcuos.com-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>");
708MODULE_LICENSE("GPL");
709MODULE_ALIAS("platform:nuc900-i2c0");
diff --git a/drivers/i2c/busses/i2c-ocores.c b/drivers/i2c/busses/i2c-ocores.c
index 1f6369f14fb6..0e10cc6182f0 100644
--- a/drivers/i2c/busses/i2c-ocores.c
+++ b/drivers/i2c/busses/i2c-ocores.c
@@ -250,7 +250,7 @@ static struct i2c_adapter ocores_adapter = {
250 .algo = &ocores_algorithm, 250 .algo = &ocores_algorithm,
251}; 251};
252 252
253static struct of_device_id ocores_i2c_match[] = { 253static const struct of_device_id ocores_i2c_match[] = {
254 { 254 {
255 .compatible = "opencores,i2c-ocores", 255 .compatible = "opencores,i2c-ocores",
256 .data = (void *)TYPE_OCORES, 256 .data = (void *)TYPE_OCORES,
diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
index 85f8eac9ba18..b182793a4051 100644
--- a/drivers/i2c/busses/i2c-omap.c
+++ b/drivers/i2c/busses/i2c-omap.c
@@ -1114,10 +1114,8 @@ omap_i2c_probe(struct platform_device *pdev)
1114 } 1114 }
1115 1115
1116 dev = devm_kzalloc(&pdev->dev, sizeof(struct omap_i2c_dev), GFP_KERNEL); 1116 dev = devm_kzalloc(&pdev->dev, sizeof(struct omap_i2c_dev), GFP_KERNEL);
1117 if (!dev) { 1117 if (!dev)
1118 dev_err(&pdev->dev, "Menory allocation failed\n");
1119 return -ENOMEM; 1118 return -ENOMEM;
1120 }
1121 1119
1122 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1120 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1123 dev->base = devm_ioremap_resource(&pdev->dev, mem); 1121 dev->base = devm_ioremap_resource(&pdev->dev, mem);
diff --git a/drivers/i2c/busses/i2c-pxa.c b/drivers/i2c/busses/i2c-pxa.c
index bbe6dfbc5c05..be671f7a0e06 100644
--- a/drivers/i2c/busses/i2c-pxa.c
+++ b/drivers/i2c/busses/i2c-pxa.c
@@ -1084,7 +1084,7 @@ static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
1084 .functionality = i2c_pxa_functionality, 1084 .functionality = i2c_pxa_functionality,
1085}; 1085};
1086 1086
1087static struct of_device_id i2c_pxa_dt_ids[] = { 1087static const struct of_device_id i2c_pxa_dt_ids[] = {
1088 { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX }, 1088 { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX },
1089 { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX }, 1089 { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX },
1090 { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA2XX }, 1090 { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA2XX },
diff --git a/drivers/i2c/busses/i2c-rcar.c b/drivers/i2c/busses/i2c-rcar.c
index 06d47aafbb79..899405923678 100644
--- a/drivers/i2c/busses/i2c-rcar.c
+++ b/drivers/i2c/busses/i2c-rcar.c
@@ -1,7 +1,9 @@
1/* 1/*
2 * drivers/i2c/busses/i2c-rcar.c 2 * Driver for the Renesas RCar I2C unit
3 * 3 *
4 * Copyright (C) 2012 Renesas Solutions Corp. 4 * Copyright (C) 2014 Wolfram Sang <wsa@sang-engineering.com>
5 *
6 * Copyright (C) 2012-14 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 7 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 * 8 *
7 * This file is based on the drivers/i2c/busses/i2c-sh7760.c 9 * This file is based on the drivers/i2c/busses/i2c-sh7760.c
@@ -12,16 +14,12 @@
12 * 14 *
13 * This program is free software; you can redistribute it and/or modify 15 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by 16 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License 17 * the Free Software Foundation; version 2 of the License.
16 * 18 *
17 * This program is distributed in the hope that it will be useful, 19 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details. 22 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */ 23 */
26#include <linux/clk.h> 24#include <linux/clk.h>
27#include <linux/delay.h> 25#include <linux/delay.h>
@@ -36,7 +34,6 @@
36#include <linux/platform_device.h> 34#include <linux/platform_device.h>
37#include <linux/pm_runtime.h> 35#include <linux/pm_runtime.h>
38#include <linux/slab.h> 36#include <linux/slab.h>
39#include <linux/spinlock.h>
40 37
41/* register offsets */ 38/* register offsets */
42#define ICSCR 0x00 /* slave ctrl */ 39#define ICSCR 0x00 /* slave ctrl */
@@ -60,7 +57,7 @@
60#define FSB (1 << 1) /* force stop bit */ 57#define FSB (1 << 1) /* force stop bit */
61#define ESG (1 << 0) /* en startbit gen */ 58#define ESG (1 << 0) /* en startbit gen */
62 59
63/* ICMSR */ 60/* ICMSR (also for ICMIE) */
64#define MNR (1 << 6) /* nack received */ 61#define MNR (1 << 6) /* nack received */
65#define MAL (1 << 5) /* arbitration lost */ 62#define MAL (1 << 5) /* arbitration lost */
66#define MST (1 << 4) /* sent a stop */ 63#define MST (1 << 4) /* sent a stop */
@@ -69,32 +66,18 @@
69#define MDR (1 << 1) 66#define MDR (1 << 1)
70#define MAT (1 << 0) /* slave addr xfer done */ 67#define MAT (1 << 0) /* slave addr xfer done */
71 68
72/* ICMIE */
73#define MNRE (1 << 6) /* nack irq en */
74#define MALE (1 << 5) /* arblos irq en */
75#define MSTE (1 << 4) /* stop irq en */
76#define MDEE (1 << 3)
77#define MDTE (1 << 2)
78#define MDRE (1 << 1)
79#define MATE (1 << 0) /* address sent irq en */
80 69
70#define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
71#define RCAR_BUS_PHASE_DATA (MDBS | MIE)
72#define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
81 73
82enum { 74#define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
83 RCAR_BUS_PHASE_ADDR, 75#define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
84 RCAR_BUS_PHASE_DATA, 76#define RCAR_IRQ_STOP (MST)
85 RCAR_BUS_PHASE_STOP,
86};
87 77
88enum { 78#define RCAR_IRQ_ACK_SEND (~(MAT | MDE))
89 RCAR_IRQ_CLOSE, 79#define RCAR_IRQ_ACK_RECV (~(MAT | MDR))
90 RCAR_IRQ_OPEN_FOR_SEND,
91 RCAR_IRQ_OPEN_FOR_RECV,
92 RCAR_IRQ_OPEN_FOR_STOP,
93};
94 80
95/*
96 * flags
97 */
98#define ID_LAST_MSG (1 << 0) 81#define ID_LAST_MSG (1 << 0)
99#define ID_IOERROR (1 << 1) 82#define ID_IOERROR (1 << 1)
100#define ID_DONE (1 << 2) 83#define ID_DONE (1 << 2)
@@ -112,14 +95,12 @@ struct rcar_i2c_priv {
112 struct i2c_msg *msg; 95 struct i2c_msg *msg;
113 struct clk *clk; 96 struct clk *clk;
114 97
115 spinlock_t lock;
116 wait_queue_head_t wait; 98 wait_queue_head_t wait;
117 99
118 int pos; 100 int pos;
119 int irq;
120 u32 icccr; 101 u32 icccr;
121 u32 flags; 102 u32 flags;
122 enum rcar_i2c_type devtype; 103 enum rcar_i2c_type devtype;
123}; 104};
124 105
125#define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent) 106#define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
@@ -130,9 +111,7 @@ struct rcar_i2c_priv {
130 111
131#define LOOP_TIMEOUT 1024 112#define LOOP_TIMEOUT 1024
132 113
133/* 114
134 * basic functions
135 */
136static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val) 115static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
137{ 116{
138 writel(val, priv->io + reg); 117 writel(val, priv->io + reg);
@@ -161,36 +140,6 @@ static void rcar_i2c_init(struct rcar_i2c_priv *priv)
161 rcar_i2c_write(priv, ICMAR, 0); 140 rcar_i2c_write(priv, ICMAR, 0);
162} 141}
163 142
164static void rcar_i2c_irq_mask(struct rcar_i2c_priv *priv, int open)
165{
166 u32 val = MNRE | MALE | MSTE | MATE; /* default */
167
168 switch (open) {
169 case RCAR_IRQ_OPEN_FOR_SEND:
170 val |= MDEE; /* default + send */
171 break;
172 case RCAR_IRQ_OPEN_FOR_RECV:
173 val |= MDRE; /* default + read */
174 break;
175 case RCAR_IRQ_OPEN_FOR_STOP:
176 val = MSTE; /* stop irq only */
177 break;
178 case RCAR_IRQ_CLOSE:
179 default:
180 val = 0; /* all close */
181 break;
182 }
183 rcar_i2c_write(priv, ICMIER, val);
184}
185
186static void rcar_i2c_set_addr(struct rcar_i2c_priv *priv, u32 recv)
187{
188 rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | recv);
189}
190
191/*
192 * bus control functions
193 */
194static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv) 143static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
195{ 144{
196 int i; 145 int i;
@@ -205,24 +154,6 @@ static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
205 return -EBUSY; 154 return -EBUSY;
206} 155}
207 156
208static void rcar_i2c_bus_phase(struct rcar_i2c_priv *priv, int phase)
209{
210 switch (phase) {
211 case RCAR_BUS_PHASE_ADDR:
212 rcar_i2c_write(priv, ICMCR, MDBS | MIE | ESG);
213 break;
214 case RCAR_BUS_PHASE_DATA:
215 rcar_i2c_write(priv, ICMCR, MDBS | MIE);
216 break;
217 case RCAR_BUS_PHASE_STOP:
218 rcar_i2c_write(priv, ICMCR, MDBS | MIE | FSB);
219 break;
220 }
221}
222
223/*
224 * clock function
225 */
226static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, 157static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
227 u32 bus_speed, 158 u32 bus_speed,
228 struct device *dev) 159 struct device *dev)
@@ -312,60 +243,18 @@ scgd_find:
312 return 0; 243 return 0;
313} 244}
314 245
315static void rcar_i2c_clock_start(struct rcar_i2c_priv *priv) 246static int rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
316{
317 rcar_i2c_write(priv, ICCCR, priv->icccr);
318}
319
320/*
321 * status functions
322 */
323static u32 rcar_i2c_status_get(struct rcar_i2c_priv *priv)
324{
325 return rcar_i2c_read(priv, ICMSR);
326}
327
328#define rcar_i2c_status_clear(priv) rcar_i2c_status_bit_clear(priv, 0xffffffff)
329static void rcar_i2c_status_bit_clear(struct rcar_i2c_priv *priv, u32 bit)
330{
331 rcar_i2c_write(priv, ICMSR, ~bit);
332}
333
334/*
335 * recv/send functions
336 */
337static int rcar_i2c_recv(struct rcar_i2c_priv *priv)
338{
339 rcar_i2c_set_addr(priv, 1);
340 rcar_i2c_status_clear(priv);
341 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
342 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_RECV);
343
344 return 0;
345}
346
347static int rcar_i2c_send(struct rcar_i2c_priv *priv)
348{ 247{
349 int ret; 248 int read = !!rcar_i2c_is_recv(priv);
350 249
351 /* 250 rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
352 * It should check bus status when send case 251 rcar_i2c_write(priv, ICMSR, 0);
353 */ 252 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
354 ret = rcar_i2c_bus_barrier(priv); 253 rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
355 if (ret < 0)
356 return ret;
357
358 rcar_i2c_set_addr(priv, 0);
359 rcar_i2c_status_clear(priv);
360 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_ADDR);
361 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_SEND);
362 254
363 return 0; 255 return 0;
364} 256}
365 257
366#define rcar_i2c_send_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDE))
367#define rcar_i2c_recv_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDR))
368
369/* 258/*
370 * interrupt functions 259 * interrupt functions
371 */ 260 */
@@ -386,7 +275,7 @@ static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
386 * goto data phase. 275 * goto data phase.
387 */ 276 */
388 if (msr & MAT) 277 if (msr & MAT)
389 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA); 278 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
390 279
391 if (priv->pos < msg->len) { 280 if (priv->pos < msg->len) {
392 /* 281 /*
@@ -414,7 +303,7 @@ static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
414 * prepare stop condition here. 303 * prepare stop condition here.
415 * ID_DONE will be set on STOP irq. 304 * ID_DONE will be set on STOP irq.
416 */ 305 */
417 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP); 306 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
418 else 307 else
419 /* 308 /*
420 * If current msg is _NOT_ last msg, 309 * If current msg is _NOT_ last msg,
@@ -425,7 +314,7 @@ static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
425 return ID_DONE; 314 return ID_DONE;
426 } 315 }
427 316
428 rcar_i2c_send_restart(priv); 317 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
429 318
430 return 0; 319 return 0;
431} 320}
@@ -462,11 +351,11 @@ static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
462 * otherwise, go to DATA phase. 351 * otherwise, go to DATA phase.
463 */ 352 */
464 if (priv->pos + 1 >= msg->len) 353 if (priv->pos + 1 >= msg->len)
465 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP); 354 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
466 else 355 else
467 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_DATA); 356 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
468 357
469 rcar_i2c_recv_restart(priv); 358 rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
470 359
471 return 0; 360 return 0;
472} 361}
@@ -474,53 +363,31 @@ static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
474static irqreturn_t rcar_i2c_irq(int irq, void *ptr) 363static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
475{ 364{
476 struct rcar_i2c_priv *priv = ptr; 365 struct rcar_i2c_priv *priv = ptr;
477 struct device *dev = rcar_i2c_priv_to_dev(priv);
478 u32 msr; 366 u32 msr;
479 367
480 /*-------------- spin lock -----------------*/ 368 msr = rcar_i2c_read(priv, ICMSR);
481 spin_lock(&priv->lock);
482
483 msr = rcar_i2c_status_get(priv);
484 369
485 /* 370 /* Arbitration lost */
486 * Arbitration lost
487 */
488 if (msr & MAL) { 371 if (msr & MAL) {
489 /*
490 * CAUTION
491 *
492 * When arbitration lost, device become _slave_ mode.
493 */
494 dev_dbg(dev, "Arbitration Lost\n");
495 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST)); 372 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
496 goto out; 373 goto out;
497 } 374 }
498 375
499 /* 376 /* Stop */
500 * Stop
501 */
502 if (msr & MST) { 377 if (msr & MST) {
503 dev_dbg(dev, "Stop\n");
504 rcar_i2c_flags_set(priv, ID_DONE); 378 rcar_i2c_flags_set(priv, ID_DONE);
505 goto out; 379 goto out;
506 } 380 }
507 381
508 /* 382 /* Nack */
509 * Nack
510 */
511 if (msr & MNR) { 383 if (msr & MNR) {
512 dev_dbg(dev, "Nack\n");
513
514 /* go to stop phase */ 384 /* go to stop phase */
515 rcar_i2c_bus_phase(priv, RCAR_BUS_PHASE_STOP); 385 rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
516 rcar_i2c_irq_mask(priv, RCAR_IRQ_OPEN_FOR_STOP); 386 rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
517 rcar_i2c_flags_set(priv, ID_NACK); 387 rcar_i2c_flags_set(priv, ID_NACK);
518 goto out; 388 goto out;
519 } 389 }
520 390
521 /*
522 * recv/send
523 */
524 if (rcar_i2c_is_recv(priv)) 391 if (rcar_i2c_is_recv(priv))
525 rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr)); 392 rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
526 else 393 else
@@ -528,14 +395,11 @@ static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
528 395
529out: 396out:
530 if (rcar_i2c_flags_has(priv, ID_DONE)) { 397 if (rcar_i2c_flags_has(priv, ID_DONE)) {
531 rcar_i2c_irq_mask(priv, RCAR_IRQ_CLOSE); 398 rcar_i2c_write(priv, ICMIER, 0);
532 rcar_i2c_status_clear(priv); 399 rcar_i2c_write(priv, ICMSR, 0);
533 wake_up(&priv->wait); 400 wake_up(&priv->wait);
534 } 401 }
535 402
536 spin_unlock(&priv->lock);
537 /*-------------- spin unlock -----------------*/
538
539 return IRQ_HANDLED; 403 return IRQ_HANDLED;
540} 404}
541 405
@@ -545,21 +409,18 @@ static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
545{ 409{
546 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); 410 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
547 struct device *dev = rcar_i2c_priv_to_dev(priv); 411 struct device *dev = rcar_i2c_priv_to_dev(priv);
548 unsigned long flags;
549 int i, ret, timeout; 412 int i, ret, timeout;
550 413
551 pm_runtime_get_sync(dev); 414 pm_runtime_get_sync(dev);
552 415
553 /*-------------- spin lock -----------------*/
554 spin_lock_irqsave(&priv->lock, flags);
555
556 rcar_i2c_init(priv); 416 rcar_i2c_init(priv);
557 rcar_i2c_clock_start(priv); 417 /* start clock */
418 rcar_i2c_write(priv, ICCCR, priv->icccr);
558 419
559 spin_unlock_irqrestore(&priv->lock, flags); 420 ret = rcar_i2c_bus_barrier(priv);
560 /*-------------- spin unlock -----------------*/ 421 if (ret < 0)
422 goto out;
561 423
562 ret = -EINVAL;
563 for (i = 0; i < num; i++) { 424 for (i = 0; i < num; i++) {
564 /* This HW can't send STOP after address phase */ 425 /* This HW can't send STOP after address phase */
565 if (msgs[i].len == 0) { 426 if (msgs[i].len == 0) {
@@ -567,9 +428,6 @@ static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
567 break; 428 break;
568 } 429 }
569 430
570 /*-------------- spin lock -----------------*/
571 spin_lock_irqsave(&priv->lock, flags);
572
573 /* init each data */ 431 /* init each data */
574 priv->msg = &msgs[i]; 432 priv->msg = &msgs[i];
575 priv->pos = 0; 433 priv->pos = 0;
@@ -577,21 +435,11 @@ static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
577 if (priv->msg == &msgs[num - 1]) 435 if (priv->msg == &msgs[num - 1])
578 rcar_i2c_flags_set(priv, ID_LAST_MSG); 436 rcar_i2c_flags_set(priv, ID_LAST_MSG);
579 437
580 /* start send/recv */ 438 ret = rcar_i2c_prepare_msg(priv);
581 if (rcar_i2c_is_recv(priv))
582 ret = rcar_i2c_recv(priv);
583 else
584 ret = rcar_i2c_send(priv);
585
586 spin_unlock_irqrestore(&priv->lock, flags);
587 /*-------------- spin unlock -----------------*/
588 439
589 if (ret < 0) 440 if (ret < 0)
590 break; 441 break;
591 442
592 /*
593 * wait result
594 */
595 timeout = wait_event_timeout(priv->wait, 443 timeout = wait_event_timeout(priv->wait,
596 rcar_i2c_flags_has(priv, ID_DONE), 444 rcar_i2c_flags_has(priv, ID_DONE),
597 5 * HZ); 445 5 * HZ);
@@ -600,9 +448,6 @@ static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
600 break; 448 break;
601 } 449 }
602 450
603 /*
604 * error handling
605 */
606 if (rcar_i2c_flags_has(priv, ID_NACK)) { 451 if (rcar_i2c_flags_has(priv, ID_NACK)) {
607 ret = -ENXIO; 452 ret = -ENXIO;
608 break; 453 break;
@@ -620,7 +465,7 @@ static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
620 465
621 ret = i + 1; /* The number of transfer */ 466 ret = i + 1; /* The number of transfer */
622 } 467 }
623 468out:
624 pm_runtime_put(dev); 469 pm_runtime_put(dev);
625 470
626 if (ret < 0 && ret != -ENXIO) 471 if (ret < 0 && ret != -ENXIO)
@@ -646,6 +491,9 @@ static const struct of_device_id rcar_i2c_dt_ids[] = {
646 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 }, 491 { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
647 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 }, 492 { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
648 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 }, 493 { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
494 { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
495 { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
496 { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
649 {}, 497 {},
650}; 498};
651MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids); 499MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
@@ -658,13 +506,11 @@ static int rcar_i2c_probe(struct platform_device *pdev)
658 struct resource *res; 506 struct resource *res;
659 struct device *dev = &pdev->dev; 507 struct device *dev = &pdev->dev;
660 u32 bus_speed; 508 u32 bus_speed;
661 int ret; 509 int irq, ret;
662 510
663 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL); 511 priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
664 if (!priv) { 512 if (!priv)
665 dev_err(dev, "no mem for private data\n");
666 return -ENOMEM; 513 return -ENOMEM;
667 }
668 514
669 priv->clk = devm_clk_get(dev, NULL); 515 priv->clk = devm_clk_get(dev, NULL);
670 if (IS_ERR(priv->clk)) { 516 if (IS_ERR(priv->clk)) {
@@ -692,9 +538,8 @@ static int rcar_i2c_probe(struct platform_device *pdev)
692 if (IS_ERR(priv->io)) 538 if (IS_ERR(priv->io))
693 return PTR_ERR(priv->io); 539 return PTR_ERR(priv->io);
694 540
695 priv->irq = platform_get_irq(pdev, 0); 541 irq = platform_get_irq(pdev, 0);
696 init_waitqueue_head(&priv->wait); 542 init_waitqueue_head(&priv->wait);
697 spin_lock_init(&priv->lock);
698 543
699 adap = &priv->adap; 544 adap = &priv->adap;
700 adap->nr = pdev->id; 545 adap->nr = pdev->id;
@@ -706,10 +551,10 @@ static int rcar_i2c_probe(struct platform_device *pdev)
706 i2c_set_adapdata(adap, priv); 551 i2c_set_adapdata(adap, priv);
707 strlcpy(adap->name, pdev->name, sizeof(adap->name)); 552 strlcpy(adap->name, pdev->name, sizeof(adap->name));
708 553
709 ret = devm_request_irq(dev, priv->irq, rcar_i2c_irq, 0, 554 ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
710 dev_name(dev), priv); 555 dev_name(dev), priv);
711 if (ret < 0) { 556 if (ret < 0) {
712 dev_err(dev, "cannot get irq %d\n", priv->irq); 557 dev_err(dev, "cannot get irq %d\n", irq);
713 return ret; 558 return ret;
714 } 559 }
715 560
@@ -759,6 +604,6 @@ static struct platform_driver rcar_i2c_driver = {
759 604
760module_platform_driver(rcar_i2c_driver); 605module_platform_driver(rcar_i2c_driver);
761 606
762MODULE_LICENSE("GPL"); 607MODULE_LICENSE("GPL v2");
763MODULE_DESCRIPTION("Renesas R-Car I2C bus driver"); 608MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
764MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 609MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
diff --git a/drivers/i2c/busses/i2c-riic.c b/drivers/i2c/busses/i2c-riic.c
index 9e1f8bacfb39..af3b3d032a9f 100644
--- a/drivers/i2c/busses/i2c-riic.c
+++ b/drivers/i2c/busses/i2c-riic.c
@@ -404,7 +404,7 @@ static int riic_i2c_remove(struct platform_device *pdev)
404 return 0; 404 return 0;
405} 405}
406 406
407static struct of_device_id riic_i2c_dt_ids[] = { 407static const struct of_device_id riic_i2c_dt_ids[] = {
408 { .compatible = "renesas,riic-rz" }, 408 { .compatible = "renesas,riic-rz" },
409 { /* Sentinel */ }, 409 { /* Sentinel */ },
410}; 410};
diff --git a/drivers/i2c/busses/i2c-s3c2410.c b/drivers/i2c/busses/i2c-s3c2410.c
index bb3a9964f7e0..e828a1dba0e5 100644
--- a/drivers/i2c/busses/i2c-s3c2410.c
+++ b/drivers/i2c/busses/i2c-s3c2410.c
@@ -1114,16 +1114,12 @@ static int s3c24xx_i2c_probe(struct platform_device *pdev)
1114 } 1114 }
1115 1115
1116 i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL); 1116 i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
1117 if (!i2c) { 1117 if (!i2c)
1118 dev_err(&pdev->dev, "no memory for state\n");
1119 return -ENOMEM; 1118 return -ENOMEM;
1120 }
1121 1119
1122 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 1120 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1123 if (!i2c->pdata) { 1121 if (!i2c->pdata)
1124 dev_err(&pdev->dev, "no memory for platform data\n");
1125 return -ENOMEM; 1122 return -ENOMEM;
1126 }
1127 1123
1128 i2c->quirks = s3c24xx_get_device_quirks(pdev); 1124 i2c->quirks = s3c24xx_get_device_quirks(pdev);
1129 if (pdata) 1125 if (pdata)
diff --git a/drivers/i2c/busses/i2c-sh_mobile.c b/drivers/i2c/busses/i2c-sh_mobile.c
index 1d79585ba4b3..8b5e79cb4468 100644
--- a/drivers/i2c/busses/i2c-sh_mobile.c
+++ b/drivers/i2c/busses/i2c-sh_mobile.c
@@ -32,6 +32,7 @@
32#include <linux/clk.h> 32#include <linux/clk.h>
33#include <linux/io.h> 33#include <linux/io.h>
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/of_device.h>
35#include <linux/i2c/i2c-sh_mobile.h> 36#include <linux/i2c/i2c-sh_mobile.h>
36 37
37/* Transmit operation: */ 38/* Transmit operation: */
@@ -139,6 +140,10 @@ struct sh_mobile_i2c_data {
139 bool send_stop; 140 bool send_stop;
140}; 141};
141 142
143struct sh_mobile_dt_config {
144 int clks_per_count;
145};
146
142#define IIC_FLAG_HAS_ICIC67 (1 << 0) 147#define IIC_FLAG_HAS_ICIC67 (1 << 0)
143 148
144#define STANDARD_MODE 100000 149#define STANDARD_MODE 100000
@@ -194,7 +199,7 @@ static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
194 iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr); 199 iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
195} 200}
196 201
197static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf, int offset) 202static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf)
198{ 203{
199 /* 204 /*
200 * Conditional expression: 205 * Conditional expression:
@@ -206,10 +211,10 @@ static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf, int off
206 * account the fall time of SCL signal (tf). Default tf value 211 * account the fall time of SCL signal (tf). Default tf value
207 * should be 0.3 us, for safety. 212 * should be 0.3 us, for safety.
208 */ 213 */
209 return (((count_khz * (tLOW + tf)) + 5000) / 10000) + offset; 214 return (((count_khz * (tLOW + tf)) + 5000) / 10000);
210} 215}
211 216
212static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf, int offset) 217static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf)
213{ 218{
214 /* 219 /*
215 * Conditional expression: 220 * Conditional expression:
@@ -225,52 +230,58 @@ static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf, int of
225 * to take into account the fall time of SDA signal (tf) at START 230 * to take into account the fall time of SDA signal (tf) at START
226 * condition, in order to meet both tHIGH and tHD;STA specs. 231 * condition, in order to meet both tHIGH and tHD;STA specs.
227 */ 232 */
228 return (((count_khz * (tHIGH + tf)) + 5000) / 10000) + offset; 233 return (((count_khz * (tHIGH + tf)) + 5000) / 10000);
229} 234}
230 235
231static void sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd) 236static int sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
232{ 237{
233 unsigned long i2c_clk_khz; 238 unsigned long i2c_clk_khz;
234 u32 tHIGH, tLOW, tf; 239 u32 tHIGH, tLOW, tf;
235 int offset; 240 uint16_t max_val;
236 241
237 /* Get clock rate after clock is enabled */ 242 /* Get clock rate after clock is enabled */
238 clk_prepare_enable(pd->clk); 243 clk_prepare_enable(pd->clk);
239 i2c_clk_khz = clk_get_rate(pd->clk) / 1000; 244 i2c_clk_khz = clk_get_rate(pd->clk) / 1000;
245 clk_disable_unprepare(pd->clk);
240 i2c_clk_khz /= pd->clks_per_count; 246 i2c_clk_khz /= pd->clks_per_count;
241 247
242 if (pd->bus_speed == STANDARD_MODE) { 248 if (pd->bus_speed == STANDARD_MODE) {
243 tLOW = 47; /* tLOW = 4.7 us */ 249 tLOW = 47; /* tLOW = 4.7 us */
244 tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */ 250 tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */
245 tf = 3; /* tf = 0.3 us */ 251 tf = 3; /* tf = 0.3 us */
246 offset = 0; /* No offset */
247 } else if (pd->bus_speed == FAST_MODE) { 252 } else if (pd->bus_speed == FAST_MODE) {
248 tLOW = 13; /* tLOW = 1.3 us */ 253 tLOW = 13; /* tLOW = 1.3 us */
249 tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */ 254 tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */
250 tf = 3; /* tf = 0.3 us */ 255 tf = 3; /* tf = 0.3 us */
251 offset = 0; /* No offset */
252 } else { 256 } else {
253 dev_err(pd->dev, "unrecognized bus speed %lu Hz\n", 257 dev_err(pd->dev, "unrecognized bus speed %lu Hz\n",
254 pd->bus_speed); 258 pd->bus_speed);
255 goto out; 259 return -EINVAL;
260 }
261
262 pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf);
263 pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf);
264
265 max_val = pd->flags & IIC_FLAG_HAS_ICIC67 ? 0x1ff : 0xff;
266 if (pd->iccl > max_val || pd->icch > max_val) {
267 dev_err(pd->dev, "timing values out of range: L/H=0x%x/0x%x\n",
268 pd->iccl, pd->icch);
269 return -EINVAL;
256 } 270 }
257 271
258 pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf, offset);
259 /* one more bit of ICCL in ICIC */ 272 /* one more bit of ICCL in ICIC */
260 if ((pd->iccl > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67)) 273 if (pd->iccl & 0x100)
261 pd->icic |= ICIC_ICCLB8; 274 pd->icic |= ICIC_ICCLB8;
262 else 275 else
263 pd->icic &= ~ICIC_ICCLB8; 276 pd->icic &= ~ICIC_ICCLB8;
264 277
265 pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf, offset);
266 /* one more bit of ICCH in ICIC */ 278 /* one more bit of ICCH in ICIC */
267 if ((pd->icch > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67)) 279 if (pd->icch & 0x100)
268 pd->icic |= ICIC_ICCHB8; 280 pd->icic |= ICIC_ICCHB8;
269 else 281 else
270 pd->icic &= ~ICIC_ICCHB8; 282 pd->icic &= ~ICIC_ICCHB8;
271 283
272out: 284 return 0;
273 clk_disable_unprepare(pd->clk);
274} 285}
275 286
276static void activate_ch(struct sh_mobile_i2c_data *pd) 287static void activate_ch(struct sh_mobile_i2c_data *pd)
@@ -316,7 +327,7 @@ static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
316 327
317 switch (op) { 328 switch (op) {
318 case OP_START: /* issue start and trigger DTE interrupt */ 329 case OP_START: /* issue start and trigger DTE interrupt */
319 iic_wr(pd, ICCR, 0x94); 330 iic_wr(pd, ICCR, ICCR_ICE | ICCR_TRS | ICCR_BBSY);
320 break; 331 break;
321 case OP_TX_FIRST: /* disable DTE interrupt and write data */ 332 case OP_TX_FIRST: /* disable DTE interrupt and write data */
322 iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE); 333 iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
@@ -327,10 +338,11 @@ static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
327 break; 338 break;
328 case OP_TX_STOP: /* write data and issue a stop afterwards */ 339 case OP_TX_STOP: /* write data and issue a stop afterwards */
329 iic_wr(pd, ICDR, data); 340 iic_wr(pd, ICDR, data);
330 iic_wr(pd, ICCR, pd->send_stop ? 0x90 : 0x94); 341 iic_wr(pd, ICCR, pd->send_stop ? ICCR_ICE | ICCR_TRS
342 : ICCR_ICE | ICCR_TRS | ICCR_BBSY);
331 break; 343 break;
332 case OP_TX_TO_RX: /* select read mode */ 344 case OP_TX_TO_RX: /* select read mode */
333 iic_wr(pd, ICCR, 0x81); 345 iic_wr(pd, ICCR, ICCR_ICE | ICCR_SCP);
334 break; 346 break;
335 case OP_RX: /* just read data */ 347 case OP_RX: /* just read data */
336 ret = iic_rd(pd, ICDR); 348 ret = iic_rd(pd, ICDR);
@@ -338,13 +350,13 @@ static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
338 case OP_RX_STOP: /* enable DTE interrupt, issue stop */ 350 case OP_RX_STOP: /* enable DTE interrupt, issue stop */
339 iic_wr(pd, ICIC, 351 iic_wr(pd, ICIC,
340 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); 352 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
341 iic_wr(pd, ICCR, 0xc0); 353 iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
342 break; 354 break;
343 case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */ 355 case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
344 iic_wr(pd, ICIC, 356 iic_wr(pd, ICIC,
345 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); 357 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
346 ret = iic_rd(pd, ICDR); 358 ret = iic_rd(pd, ICDR);
347 iic_wr(pd, ICCR, 0xc0); 359 iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
348 break; 360 break;
349 } 361 }
350 362
@@ -479,7 +491,7 @@ static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg,
479{ 491{
480 if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) { 492 if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
481 dev_err(pd->dev, "Unsupported zero length i2c read\n"); 493 dev_err(pd->dev, "Unsupported zero length i2c read\n");
482 return -EIO; 494 return -EOPNOTSUPP;
483 } 495 }
484 496
485 if (do_init) { 497 if (do_init) {
@@ -514,17 +526,12 @@ static int poll_dte(struct sh_mobile_i2c_data *pd)
514 break; 526 break;
515 527
516 if (val & ICSR_TACK) 528 if (val & ICSR_TACK)
517 return -EIO; 529 return -ENXIO;
518 530
519 udelay(10); 531 udelay(10);
520 } 532 }
521 533
522 if (!i) { 534 return i ? 0 : -ETIMEDOUT;
523 dev_warn(pd->dev, "Timeout polling for DTE!\n");
524 return -ETIMEDOUT;
525 }
526
527 return 0;
528} 535}
529 536
530static int poll_busy(struct sh_mobile_i2c_data *pd) 537static int poll_busy(struct sh_mobile_i2c_data *pd)
@@ -542,20 +549,18 @@ static int poll_busy(struct sh_mobile_i2c_data *pd)
542 */ 549 */
543 if (!(val & ICSR_BUSY)) { 550 if (!(val & ICSR_BUSY)) {
544 /* handle missing acknowledge and arbitration lost */ 551 /* handle missing acknowledge and arbitration lost */
545 if ((val | pd->sr) & (ICSR_TACK | ICSR_AL)) 552 val |= pd->sr;
546 return -EIO; 553 if (val & ICSR_TACK)
554 return -ENXIO;
555 if (val & ICSR_AL)
556 return -EAGAIN;
547 break; 557 break;
548 } 558 }
549 559
550 udelay(10); 560 udelay(10);
551 } 561 }
552 562
553 if (!i) { 563 return i ? 0 : -ETIMEDOUT;
554 dev_err(pd->dev, "Polling timed out\n");
555 return -ETIMEDOUT;
556 }
557
558 return 0;
559} 564}
560 565
561static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter, 566static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
@@ -617,42 +622,44 @@ static struct i2c_algorithm sh_mobile_i2c_algorithm = {
617 .master_xfer = sh_mobile_i2c_xfer, 622 .master_xfer = sh_mobile_i2c_xfer,
618}; 623};
619 624
620static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, int hook) 625static const struct sh_mobile_dt_config default_dt_config = {
626 .clks_per_count = 1,
627};
628
629static const struct sh_mobile_dt_config rcar_gen2_dt_config = {
630 .clks_per_count = 2,
631};
632
633static const struct of_device_id sh_mobile_i2c_dt_ids[] = {
634 { .compatible = "renesas,rmobile-iic", .data = &default_dt_config },
635 { .compatible = "renesas,iic-r8a7790", .data = &rcar_gen2_dt_config },
636 { .compatible = "renesas,iic-r8a7791", .data = &rcar_gen2_dt_config },
637 { .compatible = "renesas,iic-r8a7792", .data = &rcar_gen2_dt_config },
638 { .compatible = "renesas,iic-r8a7793", .data = &rcar_gen2_dt_config },
639 { .compatible = "renesas,iic-r8a7794", .data = &rcar_gen2_dt_config },
640 {},
641};
642MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
643
644static int sh_mobile_i2c_hook_irqs(struct platform_device *dev)
621{ 645{
622 struct resource *res; 646 struct resource *res;
623 int ret = -ENXIO; 647 resource_size_t n;
624 int n, k = 0; 648 int k = 0, ret;
625 649
626 while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) { 650 while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
627 for (n = res->start; hook && n <= res->end; n++) { 651 for (n = res->start; n <= res->end; n++) {
628 if (request_irq(n, sh_mobile_i2c_isr, 0, 652 ret = devm_request_irq(&dev->dev, n, sh_mobile_i2c_isr,
629 dev_name(&dev->dev), dev)) { 653 0, dev_name(&dev->dev), dev);
630 for (n--; n >= res->start; n--) 654 if (ret) {
631 free_irq(n, dev); 655 dev_err(&dev->dev, "cannot request IRQ %pa\n", &n);
632 656 return ret;
633 goto rollback;
634 } 657 }
635 } 658 }
636 k++; 659 k++;
637 } 660 }
638 661
639 if (hook) 662 return k > 0 ? 0 : -ENOENT;
640 return k > 0 ? 0 : -ENOENT;
641
642 ret = 0;
643
644 rollback:
645 k--;
646
647 while (k >= 0) {
648 res = platform_get_resource(dev, IORESOURCE_IRQ, k);
649 for (n = res->start; n <= res->end; n++)
650 free_irq(n, dev);
651
652 k--;
653 }
654
655 return ret;
656} 663}
657 664
658static int sh_mobile_i2c_probe(struct platform_device *dev) 665static int sh_mobile_i2c_probe(struct platform_device *dev)
@@ -661,62 +668,64 @@ static int sh_mobile_i2c_probe(struct platform_device *dev)
661 struct sh_mobile_i2c_data *pd; 668 struct sh_mobile_i2c_data *pd;
662 struct i2c_adapter *adap; 669 struct i2c_adapter *adap;
663 struct resource *res; 670 struct resource *res;
664 int size;
665 int ret; 671 int ret;
672 u32 bus_speed;
666 673
667 pd = kzalloc(sizeof(struct sh_mobile_i2c_data), GFP_KERNEL); 674 pd = devm_kzalloc(&dev->dev, sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
668 if (pd == NULL) { 675 if (!pd)
669 dev_err(&dev->dev, "cannot allocate private data\n");
670 return -ENOMEM; 676 return -ENOMEM;
671 }
672 677
673 pd->clk = clk_get(&dev->dev, NULL); 678 pd->clk = devm_clk_get(&dev->dev, NULL);
674 if (IS_ERR(pd->clk)) { 679 if (IS_ERR(pd->clk)) {
675 dev_err(&dev->dev, "cannot get clock\n"); 680 dev_err(&dev->dev, "cannot get clock\n");
676 ret = PTR_ERR(pd->clk); 681 return PTR_ERR(pd->clk);
677 goto err;
678 } 682 }
679 683
680 ret = sh_mobile_i2c_hook_irqs(dev, 1); 684 ret = sh_mobile_i2c_hook_irqs(dev);
681 if (ret) { 685 if (ret)
682 dev_err(&dev->dev, "cannot request IRQ\n"); 686 return ret;
683 goto err_clk;
684 }
685 687
686 pd->dev = &dev->dev; 688 pd->dev = &dev->dev;
687 platform_set_drvdata(dev, pd); 689 platform_set_drvdata(dev, pd);
688 690
689 res = platform_get_resource(dev, IORESOURCE_MEM, 0); 691 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
690 if (res == NULL) {
691 dev_err(&dev->dev, "cannot find IO resource\n");
692 ret = -ENOENT;
693 goto err_irq;
694 }
695
696 size = resource_size(res);
697 692
698 pd->reg = ioremap(res->start, size); 693 pd->reg = devm_ioremap_resource(&dev->dev, res);
699 if (pd->reg == NULL) { 694 if (IS_ERR(pd->reg))
700 dev_err(&dev->dev, "cannot map IO\n"); 695 return PTR_ERR(pd->reg);
701 ret = -ENXIO;
702 goto err_irq;
703 }
704 696
705 /* Use platform data bus speed or STANDARD_MODE */ 697 /* Use platform data bus speed or STANDARD_MODE */
706 pd->bus_speed = STANDARD_MODE; 698 ret = of_property_read_u32(dev->dev.of_node, "clock-frequency", &bus_speed);
707 if (pdata && pdata->bus_speed) 699 pd->bus_speed = ret ? STANDARD_MODE : bus_speed;
708 pd->bus_speed = pdata->bus_speed; 700
709 pd->clks_per_count = 1; 701 pd->clks_per_count = 1;
710 if (pdata && pdata->clks_per_count) 702
711 pd->clks_per_count = pdata->clks_per_count; 703 if (dev->dev.of_node) {
704 const struct of_device_id *match;
705
706 match = of_match_device(sh_mobile_i2c_dt_ids, &dev->dev);
707 if (match) {
708 const struct sh_mobile_dt_config *config;
709
710 config = match->data;
711 pd->clks_per_count = config->clks_per_count;
712 }
713 } else {
714 if (pdata && pdata->bus_speed)
715 pd->bus_speed = pdata->bus_speed;
716 if (pdata && pdata->clks_per_count)
717 pd->clks_per_count = pdata->clks_per_count;
718 }
712 719
713 /* The IIC blocks on SH-Mobile ARM processors 720 /* The IIC blocks on SH-Mobile ARM processors
714 * come with two new bits in ICIC. 721 * come with two new bits in ICIC.
715 */ 722 */
716 if (size > 0x17) 723 if (resource_size(res) > 0x17)
717 pd->flags |= IIC_FLAG_HAS_ICIC67; 724 pd->flags |= IIC_FLAG_HAS_ICIC67;
718 725
719 sh_mobile_i2c_init(pd); 726 ret = sh_mobile_i2c_init(pd);
727 if (ret)
728 return ret;
720 729
721 /* Enable Runtime PM for this device. 730 /* Enable Runtime PM for this device.
722 * 731 *
@@ -750,24 +759,14 @@ static int sh_mobile_i2c_probe(struct platform_device *dev)
750 ret = i2c_add_numbered_adapter(adap); 759 ret = i2c_add_numbered_adapter(adap);
751 if (ret < 0) { 760 if (ret < 0) {
752 dev_err(&dev->dev, "cannot add numbered adapter\n"); 761 dev_err(&dev->dev, "cannot add numbered adapter\n");
753 goto err_all; 762 return ret;
754 } 763 }
755 764
756 dev_info(&dev->dev, 765 dev_info(&dev->dev,
757 "I2C adapter %d with bus speed %lu Hz (L/H=%x/%x)\n", 766 "I2C adapter %d with bus speed %lu Hz (L/H=0x%x/0x%x)\n",
758 adap->nr, pd->bus_speed, pd->iccl, pd->icch); 767 adap->nr, pd->bus_speed, pd->iccl, pd->icch);
759 768
760 return 0; 769 return 0;
761
762 err_all:
763 iounmap(pd->reg);
764 err_irq:
765 sh_mobile_i2c_hook_irqs(dev, 0);
766 err_clk:
767 clk_put(pd->clk);
768 err:
769 kfree(pd);
770 return ret;
771} 770}
772 771
773static int sh_mobile_i2c_remove(struct platform_device *dev) 772static int sh_mobile_i2c_remove(struct platform_device *dev)
@@ -775,11 +774,7 @@ static int sh_mobile_i2c_remove(struct platform_device *dev)
775 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev); 774 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
776 775
777 i2c_del_adapter(&pd->adap); 776 i2c_del_adapter(&pd->adap);
778 iounmap(pd->reg);
779 sh_mobile_i2c_hook_irqs(dev, 0);
780 clk_put(pd->clk);
781 pm_runtime_disable(&dev->dev); 777 pm_runtime_disable(&dev->dev);
782 kfree(pd);
783 return 0; 778 return 0;
784} 779}
785 780
@@ -800,12 +795,6 @@ static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
800 .runtime_resume = sh_mobile_i2c_runtime_nop, 795 .runtime_resume = sh_mobile_i2c_runtime_nop,
801}; 796};
802 797
803static const struct of_device_id sh_mobile_i2c_dt_ids[] = {
804 { .compatible = "renesas,rmobile-iic", },
805 {},
806};
807MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
808
809static struct platform_driver sh_mobile_i2c_driver = { 798static struct platform_driver sh_mobile_i2c_driver = {
810 .driver = { 799 .driver = {
811 .name = "i2c-sh_mobile", 800 .name = "i2c-sh_mobile",
diff --git a/drivers/i2c/busses/i2c-simtec.c b/drivers/i2c/busses/i2c-simtec.c
index 294c80f21d65..964e5c6f84ab 100644
--- a/drivers/i2c/busses/i2c-simtec.c
+++ b/drivers/i2c/busses/i2c-simtec.c
@@ -77,10 +77,8 @@ static int simtec_i2c_probe(struct platform_device *dev)
77 int ret; 77 int ret;
78 78
79 pd = kzalloc(sizeof(struct simtec_i2c_data), GFP_KERNEL); 79 pd = kzalloc(sizeof(struct simtec_i2c_data), GFP_KERNEL);
80 if (pd == NULL) { 80 if (pd == NULL)
81 dev_err(&dev->dev, "cannot allocate private data\n");
82 return -ENOMEM; 81 return -ENOMEM;
83 }
84 82
85 platform_set_drvdata(dev, pd); 83 platform_set_drvdata(dev, pd);
86 84
diff --git a/drivers/i2c/busses/i2c-sirf.c b/drivers/i2c/busses/i2c-sirf.c
index 8e3be7ed0586..a3216defc1d3 100644
--- a/drivers/i2c/busses/i2c-sirf.c
+++ b/drivers/i2c/busses/i2c-sirf.c
@@ -307,7 +307,6 @@ static int i2c_sirfsoc_probe(struct platform_device *pdev)
307 307
308 siic = devm_kzalloc(&pdev->dev, sizeof(*siic), GFP_KERNEL); 308 siic = devm_kzalloc(&pdev->dev, sizeof(*siic), GFP_KERNEL);
309 if (!siic) { 309 if (!siic) {
310 dev_err(&pdev->dev, "Can't allocate driver data\n");
311 err = -ENOMEM; 310 err = -ENOMEM;
312 goto out; 311 goto out;
313 } 312 }
diff --git a/drivers/i2c/busses/i2c-st.c b/drivers/i2c/busses/i2c-st.c
index 872016196ef3..95b947670386 100644
--- a/drivers/i2c/busses/i2c-st.c
+++ b/drivers/i2c/busses/i2c-st.c
@@ -847,7 +847,7 @@ static int st_i2c_remove(struct platform_device *pdev)
847 return 0; 847 return 0;
848} 848}
849 849
850static struct of_device_id st_i2c_match[] = { 850static const struct of_device_id st_i2c_match[] = {
851 { .compatible = "st,comms-ssc-i2c", }, 851 { .compatible = "st,comms-ssc-i2c", },
852 { .compatible = "st,comms-ssc4-i2c", }, 852 { .compatible = "st,comms-ssc4-i2c", },
853 {}, 853 {},
diff --git a/drivers/i2c/busses/i2c-stu300.c b/drivers/i2c/busses/i2c-stu300.c
index 29b1fb778943..fefb1c19ec1d 100644
--- a/drivers/i2c/busses/i2c-stu300.c
+++ b/drivers/i2c/busses/i2c-stu300.c
@@ -868,10 +868,8 @@ static int stu300_probe(struct platform_device *pdev)
868 int ret = 0; 868 int ret = 0;
869 869
870 dev = devm_kzalloc(&pdev->dev, sizeof(struct stu300_dev), GFP_KERNEL); 870 dev = devm_kzalloc(&pdev->dev, sizeof(struct stu300_dev), GFP_KERNEL);
871 if (!dev) { 871 if (!dev)
872 dev_err(&pdev->dev, "could not allocate device struct\n");
873 return -ENOMEM; 872 return -ENOMEM;
874 }
875 873
876 bus_nr = pdev->id; 874 bus_nr = pdev->id;
877 dev->clk = devm_clk_get(&pdev->dev, NULL); 875 dev->clk = devm_clk_get(&pdev->dev, NULL);
diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
index 00f04cb5b4eb..f1bb2fc06791 100644
--- a/drivers/i2c/busses/i2c-tegra.c
+++ b/drivers/i2c/busses/i2c-tegra.c
@@ -732,10 +732,8 @@ static int tegra_i2c_probe(struct platform_device *pdev)
732 } 732 }
733 733
734 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); 734 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
735 if (!i2c_dev) { 735 if (!i2c_dev)
736 dev_err(&pdev->dev, "Could not allocate struct tegra_i2c_dev");
737 return -ENOMEM; 736 return -ENOMEM;
738 }
739 737
740 i2c_dev->base = base; 738 i2c_dev->base = base;
741 i2c_dev->div_clk = div_clk; 739 i2c_dev->div_clk = div_clk;
diff --git a/drivers/i2c/busses/i2c-wmt.c b/drivers/i2c/busses/i2c-wmt.c
index 2c8a3e4f9008..f80a38c2072c 100644
--- a/drivers/i2c/busses/i2c-wmt.c
+++ b/drivers/i2c/busses/i2c-wmt.c
@@ -379,10 +379,8 @@ static int wmt_i2c_probe(struct platform_device *pdev)
379 u32 clk_rate; 379 u32 clk_rate;
380 380
381 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); 381 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
382 if (!i2c_dev) { 382 if (!i2c_dev)
383 dev_err(&pdev->dev, "device memory allocation failed\n");
384 return -ENOMEM; 383 return -ENOMEM;
385 }
386 384
387 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 385 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
388 i2c_dev->base = devm_ioremap_resource(&pdev->dev, res); 386 i2c_dev->base = devm_ioremap_resource(&pdev->dev, res);
@@ -454,7 +452,7 @@ static int wmt_i2c_remove(struct platform_device *pdev)
454 return 0; 452 return 0;
455} 453}
456 454
457static struct of_device_id wmt_i2c_dt_ids[] = { 455static const struct of_device_id wmt_i2c_dt_ids[] = {
458 { .compatible = "wm,wm8505-i2c" }, 456 { .compatible = "wm,wm8505-i2c" },
459 { /* Sentinel */ }, 457 { /* Sentinel */ },
460}; 458};
diff --git a/drivers/i2c/busses/scx200_acb.c b/drivers/i2c/busses/scx200_acb.c
index cb66f9586f76..ff3f5747e43b 100644
--- a/drivers/i2c/busses/scx200_acb.c
+++ b/drivers/i2c/busses/scx200_acb.c
@@ -431,10 +431,8 @@ static struct scx200_acb_iface *scx200_create_iface(const char *text,
431 struct i2c_adapter *adapter; 431 struct i2c_adapter *adapter;
432 432
433 iface = kzalloc(sizeof(*iface), GFP_KERNEL); 433 iface = kzalloc(sizeof(*iface), GFP_KERNEL);
434 if (!iface) { 434 if (!iface)
435 pr_err("can't allocate memory\n");
436 return NULL; 435 return NULL;
437 }
438 436
439 adapter = &iface->adapter; 437 adapter = &iface->adapter;
440 i2c_set_adapdata(adapter, iface); 438 i2c_set_adapdata(adapter, iface);