ead6675e0b1eb65225894f3dc412a873311264b2
[openwrt/staging/zorun.git] /
1 From f11a04464ae57e8db1bb7634547842b43e36a898 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Jan=20Kundr=C3=A1t?= <jan.kundrat@cesnet.cz>
3 Date: Fri, 22 Dec 2017 22:47:16 +0100
4 Subject: i2c: gpio: Enable working over slow can_sleep GPIOs
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 "Slow" GPIOs (usually those connected over an SPI or an I2C bus) are,
10 well, slow in their operation. It is generally a good idea to avoid
11 using them for time-critical operation, but sometimes the hardware just
12 sucks, and the software has to cope. In addition to that, the I2C bus
13 itself does not actually define any strict timing limits; the bus is
14 free to go all the way down to DC. The timeouts (and therefore the
15 slowest acceptable frequency) are present only in SMBus.
16
17 The `can_sleep` is IMHO a wrong concept to use here. My SPI-to-quad-UART
18 chip (MAX14830) is connected via a 26MHz SPI bus, and it happily drives
19 SCL at 200kHz (5µs pulses) during my benchmarks. That's faster than the
20 maximal allowed speed of the traditional I2C.
21
22 The previous version of this code did not really block operation over
23 slow GPIO pins, anyway. Instead, it just resorted to printing a warning
24 with a backtrace each time a GPIO pin was accessed, thereby slowing
25 things down even more.
26
27 Finally, it's not just me. A similar patch was originally submitted in
28 2015 [1].
29
30 [1] https://patchwork.ozlabs.org/patch/450956/
31
32 Signed-off-by: Jan Kundrát <jan.kundrat@cesnet.cz>
33 Acked-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
34 Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
35 ---
36 drivers/i2c/busses/i2c-gpio.c | 11 +++++++----
37 1 file changed, 7 insertions(+), 4 deletions(-)
38
39 --- a/drivers/i2c/busses/i2c-gpio.c
40 +++ b/drivers/i2c/busses/i2c-gpio.c
41 @@ -44,7 +44,7 @@ static void i2c_gpio_setsda_val(void *da
42 {
43 struct i2c_gpio_platform_data *pdata = data;
44
45 - gpio_set_value(pdata->sda_pin, state);
46 + gpio_set_value_cansleep(pdata->sda_pin, state);
47 }
48
49 /* Toggle SCL by changing the direction of the pin. */
50 @@ -68,21 +68,21 @@ static void i2c_gpio_setscl_val(void *da
51 {
52 struct i2c_gpio_platform_data *pdata = data;
53
54 - gpio_set_value(pdata->scl_pin, state);
55 + gpio_set_value_cansleep(pdata->scl_pin, state);
56 }
57
58 static int i2c_gpio_getsda(void *data)
59 {
60 struct i2c_gpio_platform_data *pdata = data;
61
62 - return gpio_get_value(pdata->sda_pin);
63 + return gpio_get_value_cansleep(pdata->sda_pin);
64 }
65
66 static int i2c_gpio_getscl(void *data)
67 {
68 struct i2c_gpio_platform_data *pdata = data;
69
70 - return gpio_get_value(pdata->scl_pin);
71 + return gpio_get_value_cansleep(pdata->scl_pin);
72 }
73
74 static int of_i2c_gpio_get_pins(struct device_node *np,
75 @@ -175,6 +175,9 @@ static int i2c_gpio_probe(struct platfor
76 memcpy(pdata, dev_get_platdata(&pdev->dev), sizeof(*pdata));
77 }
78
79 + if (gpiod_cansleep(gpio_to_desc(pdata->sda_pin)) || gpiod_cansleep(gpio_to_desc(pdata->scl_pin)))
80 + dev_warn(&pdev->dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
81 +
82 if (pdata->sda_is_open_drain) {
83 gpio_direction_output(pdata->sda_pin, 1);
84 bit_data->setsda = i2c_gpio_setsda_val;