diff options
author | Wolfram Sang <wsa@the-dreams.de> | 2017-11-01 18:44:52 -0400 |
---|---|---|
committer | Wolfram Sang <wsa@the-dreams.de> | 2017-11-01 18:45:46 -0400 |
commit | 4ee045f4e9b73c4635ceedbb1ee40e0b3ecbdbcc (patch) | |
tree | f75e20907a3dc01e780ca75b1696f7d30c179944 /drivers/i2c | |
parent | 93367bfca98f36cece57c01dbce6ea1b4ac58245 (diff) | |
parent | 05c74778858d7d9907d607172fcc9646b70b6364 (diff) |
Merge branch 'for-wolfram' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio into i2c/for-4.15
Refactor i2c-gpio and its users to use gpiod. Done by GPIO maintainer
LinusW.
Diffstat (limited to 'drivers/i2c')
-rw-r--r-- | drivers/i2c/busses/i2c-gpio.c | 212 |
1 files changed, 104 insertions, 108 deletions
diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c index 0ef8fcc6ac3a..d80ea6ce91bb 100644 --- a/drivers/i2c/busses/i2c-gpio.c +++ b/drivers/i2c/busses/i2c-gpio.c | |||
@@ -14,27 +14,17 @@ | |||
14 | #include <linux/module.h> | 14 | #include <linux/module.h> |
15 | #include <linux/slab.h> | 15 | #include <linux/slab.h> |
16 | #include <linux/platform_device.h> | 16 | #include <linux/platform_device.h> |
17 | #include <linux/gpio.h> | 17 | #include <linux/gpio/consumer.h> |
18 | #include <linux/of.h> | 18 | #include <linux/of.h> |
19 | #include <linux/of_gpio.h> | ||
20 | 19 | ||
21 | struct i2c_gpio_private_data { | 20 | struct i2c_gpio_private_data { |
21 | struct gpio_desc *sda; | ||
22 | struct gpio_desc *scl; | ||
22 | struct i2c_adapter adap; | 23 | struct i2c_adapter adap; |
23 | struct i2c_algo_bit_data bit_data; | 24 | struct i2c_algo_bit_data bit_data; |
24 | struct i2c_gpio_platform_data pdata; | 25 | struct i2c_gpio_platform_data pdata; |
25 | }; | 26 | }; |
26 | 27 | ||
27 | /* Toggle SDA by changing the direction of the pin */ | ||
28 | static void i2c_gpio_setsda_dir(void *data, int state) | ||
29 | { | ||
30 | struct i2c_gpio_platform_data *pdata = data; | ||
31 | |||
32 | if (state) | ||
33 | gpio_direction_input(pdata->sda_pin); | ||
34 | else | ||
35 | gpio_direction_output(pdata->sda_pin, 0); | ||
36 | } | ||
37 | |||
38 | /* | 28 | /* |
39 | * 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 |
40 | * 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 |
@@ -42,20 +32,9 @@ static void i2c_gpio_setsda_dir(void *data, int state) | |||
42 | */ | 32 | */ |
43 | static void i2c_gpio_setsda_val(void *data, int state) | 33 | static void i2c_gpio_setsda_val(void *data, int state) |
44 | { | 34 | { |
45 | struct i2c_gpio_platform_data *pdata = data; | 35 | struct i2c_gpio_private_data *priv = data; |
46 | |||
47 | gpio_set_value(pdata->sda_pin, state); | ||
48 | } | ||
49 | |||
50 | /* Toggle SCL by changing the direction of the pin. */ | ||
51 | static void i2c_gpio_setscl_dir(void *data, int state) | ||
52 | { | ||
53 | struct i2c_gpio_platform_data *pdata = data; | ||
54 | 36 | ||
55 | if (state) | 37 | gpiod_set_value(priv->sda, state); |
56 | gpio_direction_input(pdata->scl_pin); | ||
57 | else | ||
58 | gpio_direction_output(pdata->scl_pin, 0); | ||
59 | } | 38 | } |
60 | 39 | ||
61 | /* | 40 | /* |
@@ -66,44 +45,23 @@ static void i2c_gpio_setscl_dir(void *data, int state) | |||
66 | */ | 45 | */ |
67 | static void i2c_gpio_setscl_val(void *data, int state) | 46 | static void i2c_gpio_setscl_val(void *data, int state) |
68 | { | 47 | { |
69 | struct i2c_gpio_platform_data *pdata = data; | 48 | struct i2c_gpio_private_data *priv = data; |
70 | 49 | ||
71 | gpio_set_value(pdata->scl_pin, state); | 50 | gpiod_set_value(priv->scl, state); |
72 | } | 51 | } |
73 | 52 | ||
74 | static int i2c_gpio_getsda(void *data) | 53 | static int i2c_gpio_getsda(void *data) |
75 | { | 54 | { |
76 | struct i2c_gpio_platform_data *pdata = data; | 55 | struct i2c_gpio_private_data *priv = data; |
77 | 56 | ||
78 | return gpio_get_value(pdata->sda_pin); | 57 | return gpiod_get_value(priv->sda); |
79 | } | 58 | } |
80 | 59 | ||
81 | static int i2c_gpio_getscl(void *data) | 60 | static int i2c_gpio_getscl(void *data) |
82 | { | 61 | { |
83 | struct i2c_gpio_platform_data *pdata = data; | 62 | struct i2c_gpio_private_data *priv = data; |
84 | |||
85 | return gpio_get_value(pdata->scl_pin); | ||
86 | } | ||
87 | |||
88 | static int of_i2c_gpio_get_pins(struct device_node *np, | ||
89 | unsigned int *sda_pin, unsigned int *scl_pin) | ||
90 | { | ||
91 | if (of_gpio_count(np) < 2) | ||
92 | return -ENODEV; | ||
93 | |||
94 | *sda_pin = of_get_gpio(np, 0); | ||
95 | *scl_pin = of_get_gpio(np, 1); | ||
96 | |||
97 | if (*sda_pin == -EPROBE_DEFER || *scl_pin == -EPROBE_DEFER) | ||
98 | return -EPROBE_DEFER; | ||
99 | |||
100 | if (!gpio_is_valid(*sda_pin) || !gpio_is_valid(*scl_pin)) { | ||
101 | pr_err("%pOF: invalid GPIO pins, sda=%d/scl=%d\n", | ||
102 | np, *sda_pin, *scl_pin); | ||
103 | return -ENODEV; | ||
104 | } | ||
105 | 63 | ||
106 | return 0; | 64 | return gpiod_get_value(priv->scl); |
107 | } | 65 | } |
108 | 66 | ||
109 | static void of_i2c_gpio_get_props(struct device_node *np, | 67 | static void of_i2c_gpio_get_props(struct device_node *np, |
@@ -124,72 +82,105 @@ static void of_i2c_gpio_get_props(struct device_node *np, | |||
124 | of_property_read_bool(np, "i2c-gpio,scl-output-only"); | 82 | of_property_read_bool(np, "i2c-gpio,scl-output-only"); |
125 | } | 83 | } |
126 | 84 | ||
85 | static struct gpio_desc *i2c_gpio_get_desc(struct device *dev, | ||
86 | const char *con_id, | ||
87 | unsigned int index, | ||
88 | enum gpiod_flags gflags) | ||
89 | { | ||
90 | struct gpio_desc *retdesc; | ||
91 | int ret; | ||
92 | |||
93 | retdesc = devm_gpiod_get(dev, con_id, gflags); | ||
94 | if (!IS_ERR(retdesc)) { | ||
95 | dev_dbg(dev, "got GPIO from name %s\n", con_id); | ||
96 | return retdesc; | ||
97 | } | ||
98 | |||
99 | retdesc = devm_gpiod_get_index(dev, NULL, index, gflags); | ||
100 | if (!IS_ERR(retdesc)) { | ||
101 | dev_dbg(dev, "got GPIO from index %u\n", index); | ||
102 | return retdesc; | ||
103 | } | ||
104 | |||
105 | ret = PTR_ERR(retdesc); | ||
106 | |||
107 | /* FIXME: hack in the old code, is this really necessary? */ | ||
108 | if (ret == -EINVAL) | ||
109 | retdesc = ERR_PTR(-EPROBE_DEFER); | ||
110 | |||
111 | /* This happens if the GPIO driver is not yet probed, let's defer */ | ||
112 | if (ret == -ENOENT) | ||
113 | retdesc = ERR_PTR(-EPROBE_DEFER); | ||
114 | |||
115 | if (ret != -EPROBE_DEFER) | ||
116 | dev_err(dev, "error trying to get descriptor: %d\n", ret); | ||
117 | |||
118 | return retdesc; | ||
119 | } | ||
120 | |||
127 | static int i2c_gpio_probe(struct platform_device *pdev) | 121 | static int i2c_gpio_probe(struct platform_device *pdev) |
128 | { | 122 | { |
129 | struct i2c_gpio_private_data *priv; | 123 | struct i2c_gpio_private_data *priv; |
130 | struct i2c_gpio_platform_data *pdata; | 124 | struct i2c_gpio_platform_data *pdata; |
131 | struct i2c_algo_bit_data *bit_data; | 125 | struct i2c_algo_bit_data *bit_data; |
132 | struct i2c_adapter *adap; | 126 | struct i2c_adapter *adap; |
133 | unsigned int sda_pin, scl_pin; | 127 | struct device *dev = &pdev->dev; |
128 | struct device_node *np = dev->of_node; | ||
129 | enum gpiod_flags gflags; | ||
134 | int ret; | 130 | int ret; |
135 | 131 | ||
136 | /* First get the GPIO pins; if it fails, we'll defer the probe. */ | 132 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
137 | if (pdev->dev.of_node) { | ||
138 | ret = of_i2c_gpio_get_pins(pdev->dev.of_node, | ||
139 | &sda_pin, &scl_pin); | ||
140 | if (ret) | ||
141 | return ret; | ||
142 | } else { | ||
143 | if (!dev_get_platdata(&pdev->dev)) | ||
144 | return -ENXIO; | ||
145 | pdata = dev_get_platdata(&pdev->dev); | ||
146 | sda_pin = pdata->sda_pin; | ||
147 | scl_pin = pdata->scl_pin; | ||
148 | } | ||
149 | |||
150 | ret = devm_gpio_request(&pdev->dev, sda_pin, "sda"); | ||
151 | if (ret) { | ||
152 | if (ret == -EINVAL) | ||
153 | ret = -EPROBE_DEFER; /* Try again later */ | ||
154 | return ret; | ||
155 | } | ||
156 | ret = devm_gpio_request(&pdev->dev, scl_pin, "scl"); | ||
157 | if (ret) { | ||
158 | if (ret == -EINVAL) | ||
159 | ret = -EPROBE_DEFER; /* Try again later */ | ||
160 | return ret; | ||
161 | } | ||
162 | |||
163 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); | ||
164 | if (!priv) | 133 | if (!priv) |
165 | return -ENOMEM; | 134 | return -ENOMEM; |
135 | |||
166 | adap = &priv->adap; | 136 | adap = &priv->adap; |
167 | bit_data = &priv->bit_data; | 137 | bit_data = &priv->bit_data; |
168 | pdata = &priv->pdata; | 138 | pdata = &priv->pdata; |
169 | 139 | ||
170 | if (pdev->dev.of_node) { | 140 | if (np) { |
171 | pdata->sda_pin = sda_pin; | 141 | of_i2c_gpio_get_props(np, pdata); |
172 | pdata->scl_pin = scl_pin; | ||
173 | of_i2c_gpio_get_props(pdev->dev.of_node, pdata); | ||
174 | } else { | 142 | } else { |
175 | memcpy(pdata, dev_get_platdata(&pdev->dev), sizeof(*pdata)); | 143 | /* |
144 | * If all platform data settings are zero it is OK | ||
145 | * to not provide any platform data from the board. | ||
146 | */ | ||
147 | if (dev_get_platdata(dev)) | ||
148 | memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata)); | ||
176 | } | 149 | } |
177 | 150 | ||
178 | if (pdata->sda_is_open_drain) { | 151 | /* |
179 | gpio_direction_output(pdata->sda_pin, 1); | 152 | * First get the GPIO pins; if it fails, we'll defer the probe. |
180 | bit_data->setsda = i2c_gpio_setsda_val; | 153 | * If the SDA line is marked from platform data or device tree as |
181 | } else { | 154 | * "open drain" it means something outside of our control is making |
182 | gpio_direction_input(pdata->sda_pin); | 155 | * this line being handled as open drain, and we should just handle |
183 | bit_data->setsda = i2c_gpio_setsda_dir; | 156 | * it as any other output. Else we enforce open drain as this is |
184 | } | 157 | * required for an I2C bus. |
158 | */ | ||
159 | if (pdata->sda_is_open_drain) | ||
160 | gflags = GPIOD_OUT_HIGH; | ||
161 | else | ||
162 | gflags = GPIOD_OUT_HIGH_OPEN_DRAIN; | ||
163 | priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags); | ||
164 | if (IS_ERR(priv->sda)) | ||
165 | return PTR_ERR(priv->sda); | ||
166 | |||
167 | /* | ||
168 | * If the SCL line is marked from platform data or device tree as | ||
169 | * "open drain" it means something outside of our control is making | ||
170 | * this line being handled as open drain, and we should just handle | ||
171 | * it as any other output. Else we enforce open drain as this is | ||
172 | * required for an I2C bus. | ||
173 | */ | ||
174 | if (pdata->scl_is_open_drain) | ||
175 | gflags = GPIOD_OUT_LOW; | ||
176 | else | ||
177 | gflags = GPIOD_OUT_LOW_OPEN_DRAIN; | ||
178 | priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags); | ||
179 | if (IS_ERR(priv->scl)) | ||
180 | return PTR_ERR(priv->scl); | ||
185 | 181 | ||
186 | if (pdata->scl_is_open_drain || pdata->scl_is_output_only) { | 182 | bit_data->setsda = i2c_gpio_setsda_val; |
187 | gpio_direction_output(pdata->scl_pin, 1); | 183 | bit_data->setscl = i2c_gpio_setscl_val; |
188 | bit_data->setscl = i2c_gpio_setscl_val; | ||
189 | } else { | ||
190 | gpio_direction_input(pdata->scl_pin); | ||
191 | bit_data->setscl = i2c_gpio_setscl_dir; | ||
192 | } | ||
193 | 184 | ||
194 | if (!pdata->scl_is_output_only) | 185 | if (!pdata->scl_is_output_only) |
195 | bit_data->getscl = i2c_gpio_getscl; | 186 | bit_data->getscl = i2c_gpio_getscl; |
@@ -207,18 +198,18 @@ static int i2c_gpio_probe(struct platform_device *pdev) | |||
207 | else | 198 | else |
208 | bit_data->timeout = HZ / 10; /* 100 ms */ | 199 | bit_data->timeout = HZ / 10; /* 100 ms */ |
209 | 200 | ||
210 | bit_data->data = pdata; | 201 | bit_data->data = priv; |
211 | 202 | ||
212 | adap->owner = THIS_MODULE; | 203 | adap->owner = THIS_MODULE; |
213 | if (pdev->dev.of_node) | 204 | if (np) |
214 | strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name)); | 205 | strlcpy(adap->name, dev_name(dev), sizeof(adap->name)); |
215 | else | 206 | else |
216 | snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id); | 207 | snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id); |
217 | 208 | ||
218 | adap->algo_data = bit_data; | 209 | adap->algo_data = bit_data; |
219 | adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; | 210 | adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
220 | adap->dev.parent = &pdev->dev; | 211 | adap->dev.parent = dev; |
221 | adap->dev.of_node = pdev->dev.of_node; | 212 | adap->dev.of_node = np; |
222 | 213 | ||
223 | adap->nr = pdev->id; | 214 | adap->nr = pdev->id; |
224 | ret = i2c_bit_add_numbered_bus(adap); | 215 | ret = i2c_bit_add_numbered_bus(adap); |
@@ -227,8 +218,13 @@ static int i2c_gpio_probe(struct platform_device *pdev) | |||
227 | 218 | ||
228 | platform_set_drvdata(pdev, priv); | 219 | platform_set_drvdata(pdev, priv); |
229 | 220 | ||
230 | dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n", | 221 | /* |
231 | pdata->sda_pin, pdata->scl_pin, | 222 | * FIXME: using global GPIO numbers is not helpful. If/when we |
223 | * get accessors to get the actual name of the GPIO line, | ||
224 | * from the descriptor, then provide that instead. | ||
225 | */ | ||
226 | dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n", | ||
227 | desc_to_gpio(priv->sda), desc_to_gpio(priv->scl), | ||
232 | pdata->scl_is_output_only | 228 | pdata->scl_is_output_only |
233 | ? ", no clock stretching" : ""); | 229 | ? ", no clock stretching" : ""); |
234 | 230 | ||