xref: /openbmc/linux/drivers/i2c/busses/i2c-gpio.c (revision 089a49b6)
1 /*
2  * Bitbanging I2C bus driver using the GPIO API
3  *
4  * Copyright (C) 2007 Atmel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/i2c.h>
11 #include <linux/i2c-algo-bit.h>
12 #include <linux/i2c-gpio.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/gpio.h>
18 #include <linux/of_gpio.h>
19 
20 struct i2c_gpio_private_data {
21 	struct i2c_adapter adap;
22 	struct i2c_algo_bit_data bit_data;
23 	struct i2c_gpio_platform_data pdata;
24 };
25 
26 /* Toggle SDA by changing the direction of the pin */
27 static void i2c_gpio_setsda_dir(void *data, int state)
28 {
29 	struct i2c_gpio_platform_data *pdata = data;
30 
31 	if (state)
32 		gpio_direction_input(pdata->sda_pin);
33 	else
34 		gpio_direction_output(pdata->sda_pin, 0);
35 }
36 
37 /*
38  * Toggle SDA by changing the output value of the pin. This is only
39  * valid for pins configured as open drain (i.e. setting the value
40  * high effectively turns off the output driver.)
41  */
42 static void i2c_gpio_setsda_val(void *data, int state)
43 {
44 	struct i2c_gpio_platform_data *pdata = data;
45 
46 	gpio_set_value(pdata->sda_pin, state);
47 }
48 
49 /* Toggle SCL by changing the direction of the pin. */
50 static void i2c_gpio_setscl_dir(void *data, int state)
51 {
52 	struct i2c_gpio_platform_data *pdata = data;
53 
54 	if (state)
55 		gpio_direction_input(pdata->scl_pin);
56 	else
57 		gpio_direction_output(pdata->scl_pin, 0);
58 }
59 
60 /*
61  * Toggle SCL by changing the output value of the pin. This is used
62  * for pins that are configured as open drain and for output-only
63  * pins. The latter case will break the i2c protocol, but it will
64  * often work in practice.
65  */
66 static void i2c_gpio_setscl_val(void *data, int state)
67 {
68 	struct i2c_gpio_platform_data *pdata = data;
69 
70 	gpio_set_value(pdata->scl_pin, state);
71 }
72 
73 static int i2c_gpio_getsda(void *data)
74 {
75 	struct i2c_gpio_platform_data *pdata = data;
76 
77 	return gpio_get_value(pdata->sda_pin);
78 }
79 
80 static int i2c_gpio_getscl(void *data)
81 {
82 	struct i2c_gpio_platform_data *pdata = data;
83 
84 	return gpio_get_value(pdata->scl_pin);
85 }
86 
87 static int of_i2c_gpio_get_pins(struct device_node *np,
88 				unsigned int *sda_pin, unsigned int *scl_pin)
89 {
90 	if (of_gpio_count(np) < 2)
91 		return -ENODEV;
92 
93 	*sda_pin = of_get_gpio(np, 0);
94 	*scl_pin = of_get_gpio(np, 1);
95 
96 	if (!gpio_is_valid(*sda_pin) || !gpio_is_valid(*scl_pin)) {
97 		pr_err("%s: invalid GPIO pins, sda=%d/scl=%d\n",
98 		       np->full_name, *sda_pin, *scl_pin);
99 		return -ENODEV;
100 	}
101 
102 	return 0;
103 }
104 
105 static void of_i2c_gpio_get_props(struct device_node *np,
106 				  struct i2c_gpio_platform_data *pdata)
107 {
108 	u32 reg;
109 
110 	of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
111 
112 	if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
113 		pdata->timeout = msecs_to_jiffies(reg);
114 
115 	pdata->sda_is_open_drain =
116 		of_property_read_bool(np, "i2c-gpio,sda-open-drain");
117 	pdata->scl_is_open_drain =
118 		of_property_read_bool(np, "i2c-gpio,scl-open-drain");
119 	pdata->scl_is_output_only =
120 		of_property_read_bool(np, "i2c-gpio,scl-output-only");
121 }
122 
123 static int i2c_gpio_probe(struct platform_device *pdev)
124 {
125 	struct i2c_gpio_private_data *priv;
126 	struct i2c_gpio_platform_data *pdata;
127 	struct i2c_algo_bit_data *bit_data;
128 	struct i2c_adapter *adap;
129 	unsigned int sda_pin, scl_pin;
130 	int ret;
131 
132 	/* First get the GPIO pins; if it fails, we'll defer the probe. */
133 	if (pdev->dev.of_node) {
134 		ret = of_i2c_gpio_get_pins(pdev->dev.of_node,
135 					   &sda_pin, &scl_pin);
136 		if (ret)
137 			return ret;
138 	} else {
139 		if (!dev_get_platdata(&pdev->dev))
140 			return -ENXIO;
141 		pdata = dev_get_platdata(&pdev->dev);
142 		sda_pin = pdata->sda_pin;
143 		scl_pin = pdata->scl_pin;
144 	}
145 
146 	ret = gpio_request(sda_pin, "sda");
147 	if (ret) {
148 		if (ret == -EINVAL)
149 			ret = -EPROBE_DEFER;	/* Try again later */
150 		goto err_request_sda;
151 	}
152 	ret = gpio_request(scl_pin, "scl");
153 	if (ret) {
154 		if (ret == -EINVAL)
155 			ret = -EPROBE_DEFER;	/* Try again later */
156 		goto err_request_scl;
157 	}
158 
159 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
160 	if (!priv) {
161 		ret = -ENOMEM;
162 		goto err_add_bus;
163 	}
164 	adap = &priv->adap;
165 	bit_data = &priv->bit_data;
166 	pdata = &priv->pdata;
167 
168 	if (pdev->dev.of_node) {
169 		pdata->sda_pin = sda_pin;
170 		pdata->scl_pin = scl_pin;
171 		of_i2c_gpio_get_props(pdev->dev.of_node, pdata);
172 	} else {
173 		memcpy(pdata, dev_get_platdata(&pdev->dev), sizeof(*pdata));
174 	}
175 
176 	if (pdata->sda_is_open_drain) {
177 		gpio_direction_output(pdata->sda_pin, 1);
178 		bit_data->setsda = i2c_gpio_setsda_val;
179 	} else {
180 		gpio_direction_input(pdata->sda_pin);
181 		bit_data->setsda = i2c_gpio_setsda_dir;
182 	}
183 
184 	if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
185 		gpio_direction_output(pdata->scl_pin, 1);
186 		bit_data->setscl = i2c_gpio_setscl_val;
187 	} else {
188 		gpio_direction_input(pdata->scl_pin);
189 		bit_data->setscl = i2c_gpio_setscl_dir;
190 	}
191 
192 	if (!pdata->scl_is_output_only)
193 		bit_data->getscl = i2c_gpio_getscl;
194 	bit_data->getsda = i2c_gpio_getsda;
195 
196 	if (pdata->udelay)
197 		bit_data->udelay = pdata->udelay;
198 	else if (pdata->scl_is_output_only)
199 		bit_data->udelay = 50;			/* 10 kHz */
200 	else
201 		bit_data->udelay = 5;			/* 100 kHz */
202 
203 	if (pdata->timeout)
204 		bit_data->timeout = pdata->timeout;
205 	else
206 		bit_data->timeout = HZ / 10;		/* 100 ms */
207 
208 	bit_data->data = pdata;
209 
210 	adap->owner = THIS_MODULE;
211 	if (pdev->dev.of_node)
212 		strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
213 	else
214 		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
215 
216 	adap->algo_data = bit_data;
217 	adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
218 	adap->dev.parent = &pdev->dev;
219 	adap->dev.of_node = pdev->dev.of_node;
220 
221 	adap->nr = pdev->id;
222 	ret = i2c_bit_add_numbered_bus(adap);
223 	if (ret)
224 		goto err_add_bus;
225 
226 	platform_set_drvdata(pdev, priv);
227 
228 	dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
229 		 pdata->sda_pin, pdata->scl_pin,
230 		 pdata->scl_is_output_only
231 		 ? ", no clock stretching" : "");
232 
233 	return 0;
234 
235 err_add_bus:
236 	gpio_free(scl_pin);
237 err_request_scl:
238 	gpio_free(sda_pin);
239 err_request_sda:
240 	return ret;
241 }
242 
243 static int i2c_gpio_remove(struct platform_device *pdev)
244 {
245 	struct i2c_gpio_private_data *priv;
246 	struct i2c_gpio_platform_data *pdata;
247 	struct i2c_adapter *adap;
248 
249 	priv = platform_get_drvdata(pdev);
250 	adap = &priv->adap;
251 	pdata = &priv->pdata;
252 
253 	i2c_del_adapter(adap);
254 	gpio_free(pdata->scl_pin);
255 	gpio_free(pdata->sda_pin);
256 
257 	return 0;
258 }
259 
260 #if defined(CONFIG_OF)
261 static const struct of_device_id i2c_gpio_dt_ids[] = {
262 	{ .compatible = "i2c-gpio", },
263 	{ /* sentinel */ }
264 };
265 
266 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
267 #endif
268 
269 static struct platform_driver i2c_gpio_driver = {
270 	.driver		= {
271 		.name	= "i2c-gpio",
272 		.owner	= THIS_MODULE,
273 		.of_match_table	= of_match_ptr(i2c_gpio_dt_ids),
274 	},
275 	.probe		= i2c_gpio_probe,
276 	.remove		= i2c_gpio_remove,
277 };
278 
279 static int __init i2c_gpio_init(void)
280 {
281 	int ret;
282 
283 	ret = platform_driver_register(&i2c_gpio_driver);
284 	if (ret)
285 		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
286 
287 	return ret;
288 }
289 subsys_initcall(i2c_gpio_init);
290 
291 static void __exit i2c_gpio_exit(void)
292 {
293 	platform_driver_unregister(&i2c_gpio_driver);
294 }
295 module_exit(i2c_gpio_exit);
296 
297 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
298 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
299 MODULE_LICENSE("GPL");
300 MODULE_ALIAS("platform:i2c-gpio");
301