aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Walleij <linus.walleij@linaro.org>2017-09-10 13:54:21 -0400
committerLinus Walleij <linus.walleij@linaro.org>2017-10-30 03:42:37 -0400
commit7bb75029ef34838604357350b4f24d6535e9d01f (patch)
tree308c8bd845eacf62c4fbc29b03783cd569f83fdd
parentf926dfc112bc6cf41d7068ee5e3f261e13a5bec8 (diff)
i2c: gpio: Enforce open drain through gpiolib
The I2C GPIO bitbang driver currently emulates open drain behaviour by implementing what the gpiolib already does: not actively driving the line high, instead setting it to input. This makes no sense. Use the new facility in gpiolib to request the lines enforced into open drain mode, and let the open drain emulation already present in the gpiolib kick in and handle this. As a bonus: if the GPIO driver in the back-end actually supports open drain in hardware using the .set_config() callback, it will be utilized. That's correct: we never used that hardware feature before, instead relying on emulating open drain even if the GPIO controller could actually handle this for us. Users will sometimes get messages like this: gpio-485 (?): enforced open drain please flag it properly in DT/ACPI DSDT/board file gpio-486 (?): enforced open drain please flag it properly in DT/ACPI DSDT/board file i2c-gpio gpio-i2c: using lines 485 (SDA) and 486 (SCL) Which is completely proper: since the line is used as open drain, it should actually be flagged properly with e.g. gpios = <&gpio0 5 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>, <&gpio0 6 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>; Or similar facilities in board file descriptor tables or ACPI DSDT. Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
-rw-r--r--drivers/i2c/busses/i2c-gpio.c102
1 files changed, 39 insertions, 63 deletions
diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c
index b4664037eded..97b9c29e9429 100644
--- a/drivers/i2c/busses/i2c-gpio.c
+++ b/drivers/i2c/busses/i2c-gpio.c
@@ -25,23 +25,6 @@ struct i2c_gpio_private_data {
25 struct i2c_gpio_platform_data pdata; 25 struct i2c_gpio_platform_data pdata;
26}; 26};
27 27
28/* Toggle SDA by changing the direction of the pin */
29static void i2c_gpio_setsda_dir(void *data, int state)
30{
31 struct i2c_gpio_private_data *priv = data;
32
33 /*
34 * This is a way of saying "do not drive
35 * me actively high" which means emulating open drain.
36 * The right way to do this is for gpiolib to
37 * handle this, by the function below.
38 */
39 if (state)
40 gpiod_direction_input(priv->sda);
41 else
42 gpiod_direction_output(priv->sda, 0);
43}
44
45/* 28/*
46 * Toggle SDA by changing the output value of the pin. This is only 29 * Toggle SDA by changing the output value of the pin. This is only
47 * valid for pins configured as open drain (i.e. setting the value 30 * valid for pins configured as open drain (i.e. setting the value
@@ -54,17 +37,6 @@ static void i2c_gpio_setsda_val(void *data, int state)
54 gpiod_set_value(priv->sda, state); 37 gpiod_set_value(priv->sda, state);
55} 38}
56 39
57/* Toggle SCL by changing the direction of the pin. */
58static void i2c_gpio_setscl_dir(void *data, int state)
59{
60 struct i2c_gpio_private_data *priv = data;
61
62 if (state)
63 gpiod_direction_input(priv->scl);
64 else
65 gpiod_direction_output(priv->scl, 0);
66}
67
68/* 40/*
69 * Toggle SCL by changing the output value of the pin. This is used 41 * Toggle SCL by changing the output value of the pin. This is used
70 * for pins that are configured as open drain and for output-only 42 * for pins that are configured as open drain and for output-only
@@ -116,30 +88,13 @@ static int i2c_gpio_probe(struct platform_device *pdev)
116 struct i2c_gpio_platform_data *pdata; 88 struct i2c_gpio_platform_data *pdata;
117 struct i2c_algo_bit_data *bit_data; 89 struct i2c_algo_bit_data *bit_data;
118 struct i2c_adapter *adap; 90 struct i2c_adapter *adap;
91 enum gpiod_flags gflags;
119 int ret; 92 int ret;
120 93
121 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 94 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
122 if (!priv) 95 if (!priv)
123 return -ENOMEM; 96 return -ENOMEM;
124 97
125 /* First get the GPIO pins; if it fails, we'll defer the probe. */
126 priv->sda = devm_gpiod_get_index(&pdev->dev, NULL, 0, GPIOD_OUT_HIGH);
127 if (IS_ERR(priv->sda)) {
128 ret = PTR_ERR(priv->sda);
129 /* FIXME: hack in the old code, is this really necessary? */
130 if (ret == -EINVAL)
131 ret = -EPROBE_DEFER;
132 return ret;
133 }
134 priv->scl = devm_gpiod_get_index(&pdev->dev, NULL, 1, GPIOD_OUT_LOW);
135 if (IS_ERR(priv->scl)) {
136 ret = PTR_ERR(priv->scl);
137 /* FIXME: hack in the old code, is this really necessary? */
138 if (ret == -EINVAL)
139 ret = -EPROBE_DEFER;
140 return ret;
141 }
142
143 adap = &priv->adap; 98 adap = &priv->adap;
144 bit_data = &priv->bit_data; 99 bit_data = &priv->bit_data;
145 pdata = &priv->pdata; 100 pdata = &priv->pdata;
@@ -157,27 +112,48 @@ static int i2c_gpio_probe(struct platform_device *pdev)
157 } 112 }
158 113
159 /* 114 /*
160 * FIXME: this is a hack emulating the open drain emulation 115 * First get the GPIO pins; if it fails, we'll defer the probe.
161 * that gpiolib can already do for us. Make all clients properly 116 * If the SDA line is marked from platform data or device tree as
162 * flag their lines as open drain and get rid of this property 117 * "open drain" it means something outside of our control is making
163 * and the special callback. 118 * this line being handled as open drain, and we should just handle
119 * it as any other output. Else we enforce open drain as this is
120 * required for an I2C bus.
164 */ 121 */
165 if (pdata->sda_is_open_drain) { 122 if (pdata->sda_is_open_drain)
166 gpiod_direction_output(priv->sda, 1); 123 gflags = GPIOD_OUT_HIGH;
167 bit_data->setsda = i2c_gpio_setsda_val; 124 else
168 } else { 125 gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
169 gpiod_direction_input(priv->sda); 126 priv->sda = devm_gpiod_get_index(&pdev->dev, NULL, 0, gflags);
170 bit_data->setsda = i2c_gpio_setsda_dir; 127 if (IS_ERR(priv->sda)) {
128 ret = PTR_ERR(priv->sda);
129 /* FIXME: hack in the old code, is this really necessary? */
130 if (ret == -EINVAL)
131 ret = -EPROBE_DEFER;
132 return ret;
171 } 133 }
172 134 /*
173 if (pdata->scl_is_open_drain || pdata->scl_is_output_only) { 135 * If the SCL line is marked from platform data or device tree as
174 gpiod_direction_output(priv->scl, 1); 136 * "open drain" it means something outside of our control is making
175 bit_data->setscl = i2c_gpio_setscl_val; 137 * this line being handled as open drain, and we should just handle
176 } else { 138 * it as any other output. Else we enforce open drain as this is
177 gpiod_direction_input(priv->scl); 139 * required for an I2C bus.
178 bit_data->setscl = i2c_gpio_setscl_dir; 140 */
141 if (pdata->scl_is_open_drain)
142 gflags = GPIOD_OUT_LOW;
143 else
144 gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
145 priv->scl = devm_gpiod_get_index(&pdev->dev, NULL, 1, gflags);
146 if (IS_ERR(priv->scl)) {
147 ret = PTR_ERR(priv->scl);
148 /* FIXME: hack in the old code, is this really necessary? */
149 if (ret == -EINVAL)
150 ret = -EPROBE_DEFER;
151 return ret;
179 } 152 }
180 153
154 bit_data->setsda = i2c_gpio_setsda_val;
155 bit_data->setscl = i2c_gpio_setscl_val;
156
181 if (!pdata->scl_is_output_only) 157 if (!pdata->scl_is_output_only)
182 bit_data->getscl = i2c_gpio_getscl; 158 bit_data->getscl = i2c_gpio_getscl;
183 bit_data->getsda = i2c_gpio_getsda; 159 bit_data->getsda = i2c_gpio_getsda;