aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2018-04-28 13:06:16 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2018-04-28 13:06:16 -0400
commita97d8efd9d350bd9c6cf13689c7cc09049b42acd (patch)
tree6159a9faa50938c01cab75fd0bab4313a9c92247
parent6e041ffcc2d0bf8792937e89480b2172a9dd2823 (diff)
parent2a010461207cc96bee5ab81748325dec1972976f (diff)
Merge branch 'i2c/for-current' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux
Pull i2c fixes from Wolfram Sang: - two driver fixes - better parameter check for the core - Documentation updates - part of a tree-wide HAS_DMA cleanup * 'i2c/for-current' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux: i2c: sprd: Fix the i2c count issue i2c: sprd: Prevent i2c accesses after suspend is called i2c: dev: prevent ZERO_SIZE_PTR deref in i2cdev_ioctl_rdwr() Documentation/i2c: adopt kernel commenting style in examples Documentation/i2c: sync docs with current state of i2c-tools Documentation/i2c: whitespace cleanup i2c: Remove depends on HAS_DMA in case of platform dependency
-rw-r--r--Documentation/i2c/dev-interface32
-rw-r--r--drivers/i2c/busses/Kconfig3
-rw-r--r--drivers/i2c/busses/i2c-sprd.c22
-rw-r--r--drivers/i2c/i2c-dev.c2
4 files changed, 33 insertions, 26 deletions
diff --git a/Documentation/i2c/dev-interface b/Documentation/i2c/dev-interface
index d04e6e4964ee..fbed645ccd75 100644
--- a/Documentation/i2c/dev-interface
+++ b/Documentation/i2c/dev-interface
@@ -9,8 +9,8 @@ i2c adapters present on your system at a given time. i2cdetect is part of
9the i2c-tools package. 9the i2c-tools package.
10 10
11I2C device files are character device files with major device number 89 11I2C device files are character device files with major device number 89
12and a minor device number corresponding to the number assigned as 12and a minor device number corresponding to the number assigned as
13explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ..., 13explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
14i2c-10, ...). All 256 minor device numbers are reserved for i2c. 14i2c-10, ...). All 256 minor device numbers are reserved for i2c.
15 15
16 16
@@ -23,11 +23,6 @@ First, you need to include these two headers:
23 #include <linux/i2c-dev.h> 23 #include <linux/i2c-dev.h>
24 #include <i2c/smbus.h> 24 #include <i2c/smbus.h>
25 25
26(Please note that there are two files named "i2c-dev.h" out there. One is
27distributed with the Linux kernel and the other one is included in the
28source tree of i2c-tools. They used to be different in content but since 2012
29they're identical. You should use "linux/i2c-dev.h").
30
31Now, you have to decide which adapter you want to access. You should 26Now, you have to decide which adapter you want to access. You should
32inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this. 27inspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this.
33Adapter numbers are assigned somewhat dynamically, so you can not 28Adapter numbers are assigned somewhat dynamically, so you can not
@@ -38,7 +33,7 @@ Next thing, open the device file, as follows:
38 int file; 33 int file;
39 int adapter_nr = 2; /* probably dynamically determined */ 34 int adapter_nr = 2; /* probably dynamically determined */
40 char filename[20]; 35 char filename[20];
41 36
42 snprintf(filename, 19, "/dev/i2c-%d", adapter_nr); 37 snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
43 file = open(filename, O_RDWR); 38 file = open(filename, O_RDWR);
44 if (file < 0) { 39 if (file < 0) {
@@ -72,8 +67,10 @@ the device supports them. Both are illustrated below.
72 /* res contains the read word */ 67 /* res contains the read word */
73 } 68 }
74 69
75 /* Using I2C Write, equivalent of 70 /*
76 i2c_smbus_write_word_data(file, reg, 0x6543) */ 71 * Using I2C Write, equivalent of
72 * i2c_smbus_write_word_data(file, reg, 0x6543)
73 */
77 buf[0] = reg; 74 buf[0] = reg;
78 buf[1] = 0x43; 75 buf[1] = 0x43;
79 buf[2] = 0x65; 76 buf[2] = 0x65;
@@ -140,14 +137,14 @@ ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)
140 set in each message, overriding the values set with the above ioctl's. 137 set in each message, overriding the values set with the above ioctl's.
141 138
142ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args) 139ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)
143 Not meant to be called directly; instead, use the access functions 140 If possible, use the provided i2c_smbus_* methods described below instead
144 below. 141 of issuing direct ioctls.
145 142
146You can do plain i2c transactions by using read(2) and write(2) calls. 143You can do plain i2c transactions by using read(2) and write(2) calls.
147You do not need to pass the address byte; instead, set it through 144You do not need to pass the address byte; instead, set it through
148ioctl I2C_SLAVE before you try to access the device. 145ioctl I2C_SLAVE before you try to access the device.
149 146
150You can do SMBus level transactions (see documentation file smbus-protocol 147You can do SMBus level transactions (see documentation file smbus-protocol
151for details) through the following functions: 148for details) through the following functions:
152 __s32 i2c_smbus_write_quick(int file, __u8 value); 149 __s32 i2c_smbus_write_quick(int file, __u8 value);
153 __s32 i2c_smbus_read_byte(int file); 150 __s32 i2c_smbus_read_byte(int file);
@@ -158,7 +155,7 @@ for details) through the following functions:
158 __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); 155 __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value);
159 __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); 156 __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value);
160 __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); 157 __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values);
161 __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, 158 __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length,
162 __u8 *values); 159 __u8 *values);
163All these transactions return -1 on failure; you can read errno to see 160All these transactions return -1 on failure; you can read errno to see
164what happened. The 'write' transactions return 0 on success; the 161what happened. The 'write' transactions return 0 on success; the
@@ -166,10 +163,9 @@ what happened. The 'write' transactions return 0 on success; the
166returns the number of values read. The block buffers need not be longer 163returns the number of values read. The block buffers need not be longer
167than 32 bytes. 164than 32 bytes.
168 165
169The above functions are all inline functions, that resolve to calls to 166The above functions are made available by linking against the libi2c library,
170the i2c_smbus_access function, that on its turn calls a specific ioctl 167which is provided by the i2c-tools project. See:
171with the data in a specific format. Read the source code if you 168https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git/.
172want to know what happens behind the screens.
173 169
174 170
175Implementation details 171Implementation details
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index c4865b08d7fb..8d21b9825d71 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -707,7 +707,6 @@ config I2C_MPC
707config I2C_MT65XX 707config I2C_MT65XX
708 tristate "MediaTek I2C adapter" 708 tristate "MediaTek I2C adapter"
709 depends on ARCH_MEDIATEK || COMPILE_TEST 709 depends on ARCH_MEDIATEK || COMPILE_TEST
710 depends on HAS_DMA
711 help 710 help
712 This selects the MediaTek(R) Integrated Inter Circuit bus driver 711 This selects the MediaTek(R) Integrated Inter Circuit bus driver
713 for MT65xx and MT81xx. 712 for MT65xx and MT81xx.
@@ -885,7 +884,6 @@ config I2C_SH7760
885 884
886config I2C_SH_MOBILE 885config I2C_SH_MOBILE
887 tristate "SuperH Mobile I2C Controller" 886 tristate "SuperH Mobile I2C Controller"
888 depends on HAS_DMA
889 depends on ARCH_SHMOBILE || ARCH_RENESAS || COMPILE_TEST 887 depends on ARCH_SHMOBILE || ARCH_RENESAS || COMPILE_TEST
890 help 888 help
891 If you say yes to this option, support will be included for the 889 If you say yes to this option, support will be included for the
@@ -1098,7 +1096,6 @@ config I2C_XLP9XX
1098 1096
1099config I2C_RCAR 1097config I2C_RCAR
1100 tristate "Renesas R-Car I2C Controller" 1098 tristate "Renesas R-Car I2C Controller"
1101 depends on HAS_DMA
1102 depends on ARCH_RENESAS || COMPILE_TEST 1099 depends on ARCH_RENESAS || COMPILE_TEST
1103 select I2C_SLAVE 1100 select I2C_SLAVE
1104 help 1101 help
diff --git a/drivers/i2c/busses/i2c-sprd.c b/drivers/i2c/busses/i2c-sprd.c
index 25fcc3c1e32b..4053259bccb8 100644
--- a/drivers/i2c/busses/i2c-sprd.c
+++ b/drivers/i2c/busses/i2c-sprd.c
@@ -86,6 +86,7 @@ struct sprd_i2c {
86 u32 count; 86 u32 count;
87 int irq; 87 int irq;
88 int err; 88 int err;
89 bool is_suspended;
89}; 90};
90 91
91static void sprd_i2c_set_count(struct sprd_i2c *i2c_dev, u32 count) 92static void sprd_i2c_set_count(struct sprd_i2c *i2c_dev, u32 count)
@@ -283,6 +284,9 @@ static int sprd_i2c_master_xfer(struct i2c_adapter *i2c_adap,
283 struct sprd_i2c *i2c_dev = i2c_adap->algo_data; 284 struct sprd_i2c *i2c_dev = i2c_adap->algo_data;
284 int im, ret; 285 int im, ret;
285 286
287 if (i2c_dev->is_suspended)
288 return -EBUSY;
289
286 ret = pm_runtime_get_sync(i2c_dev->dev); 290 ret = pm_runtime_get_sync(i2c_dev->dev);
287 if (ret < 0) 291 if (ret < 0)
288 return ret; 292 return ret;
@@ -364,13 +368,12 @@ static irqreturn_t sprd_i2c_isr_thread(int irq, void *dev_id)
364 struct sprd_i2c *i2c_dev = dev_id; 368 struct sprd_i2c *i2c_dev = dev_id;
365 struct i2c_msg *msg = i2c_dev->msg; 369 struct i2c_msg *msg = i2c_dev->msg;
366 bool ack = !(readl(i2c_dev->base + I2C_STATUS) & I2C_RX_ACK); 370 bool ack = !(readl(i2c_dev->base + I2C_STATUS) & I2C_RX_ACK);
367 u32 i2c_count = readl(i2c_dev->base + I2C_COUNT);
368 u32 i2c_tran; 371 u32 i2c_tran;
369 372
370 if (msg->flags & I2C_M_RD) 373 if (msg->flags & I2C_M_RD)
371 i2c_tran = i2c_dev->count >= I2C_FIFO_FULL_THLD; 374 i2c_tran = i2c_dev->count >= I2C_FIFO_FULL_THLD;
372 else 375 else
373 i2c_tran = i2c_count; 376 i2c_tran = i2c_dev->count;
374 377
375 /* 378 /*
376 * If we got one ACK from slave when writing data, and we did not 379 * If we got one ACK from slave when writing data, and we did not
@@ -408,14 +411,13 @@ static irqreturn_t sprd_i2c_isr(int irq, void *dev_id)
408{ 411{
409 struct sprd_i2c *i2c_dev = dev_id; 412 struct sprd_i2c *i2c_dev = dev_id;
410 struct i2c_msg *msg = i2c_dev->msg; 413 struct i2c_msg *msg = i2c_dev->msg;
411 u32 i2c_count = readl(i2c_dev->base + I2C_COUNT);
412 bool ack = !(readl(i2c_dev->base + I2C_STATUS) & I2C_RX_ACK); 414 bool ack = !(readl(i2c_dev->base + I2C_STATUS) & I2C_RX_ACK);
413 u32 i2c_tran; 415 u32 i2c_tran;
414 416
415 if (msg->flags & I2C_M_RD) 417 if (msg->flags & I2C_M_RD)
416 i2c_tran = i2c_dev->count >= I2C_FIFO_FULL_THLD; 418 i2c_tran = i2c_dev->count >= I2C_FIFO_FULL_THLD;
417 else 419 else
418 i2c_tran = i2c_count; 420 i2c_tran = i2c_dev->count;
419 421
420 /* 422 /*
421 * If we did not get one ACK from slave when writing data, then we 423 * If we did not get one ACK from slave when writing data, then we
@@ -586,11 +588,23 @@ static int sprd_i2c_remove(struct platform_device *pdev)
586 588
587static int __maybe_unused sprd_i2c_suspend_noirq(struct device *pdev) 589static int __maybe_unused sprd_i2c_suspend_noirq(struct device *pdev)
588{ 590{
591 struct sprd_i2c *i2c_dev = dev_get_drvdata(pdev);
592
593 i2c_lock_adapter(&i2c_dev->adap);
594 i2c_dev->is_suspended = true;
595 i2c_unlock_adapter(&i2c_dev->adap);
596
589 return pm_runtime_force_suspend(pdev); 597 return pm_runtime_force_suspend(pdev);
590} 598}
591 599
592static int __maybe_unused sprd_i2c_resume_noirq(struct device *pdev) 600static int __maybe_unused sprd_i2c_resume_noirq(struct device *pdev)
593{ 601{
602 struct sprd_i2c *i2c_dev = dev_get_drvdata(pdev);
603
604 i2c_lock_adapter(&i2c_dev->adap);
605 i2c_dev->is_suspended = false;
606 i2c_unlock_adapter(&i2c_dev->adap);
607
594 return pm_runtime_force_resume(pdev); 608 return pm_runtime_force_resume(pdev);
595} 609}
596 610
diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
index 036a03f0d0a6..1667b6e7674f 100644
--- a/drivers/i2c/i2c-dev.c
+++ b/drivers/i2c/i2c-dev.c
@@ -280,7 +280,7 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
280 */ 280 */
281 if (msgs[i].flags & I2C_M_RECV_LEN) { 281 if (msgs[i].flags & I2C_M_RECV_LEN) {
282 if (!(msgs[i].flags & I2C_M_RD) || 282 if (!(msgs[i].flags & I2C_M_RD) ||
283 msgs[i].buf[0] < 1 || 283 msgs[i].len < 1 || msgs[i].buf[0] < 1 ||
284 msgs[i].len < msgs[i].buf[0] + 284 msgs[i].len < msgs[i].buf[0] +
285 I2C_SMBUS_BLOCK_MAX) { 285 I2C_SMBUS_BLOCK_MAX) {
286 res = -EINVAL; 286 res = -EINVAL;