xref: /openbmc/linux/drivers/i2c/busses/i2c-gpio.c (revision 9dae47aba0a055f761176d9297371d5bb24289ec)
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/consumer.h>
18 #include <linux/of.h>
19 
20 struct i2c_gpio_private_data {
21 	struct gpio_desc *sda;
22 	struct gpio_desc *scl;
23 	struct i2c_adapter adap;
24 	struct i2c_algo_bit_data bit_data;
25 	struct i2c_gpio_platform_data pdata;
26 };
27 
28 /*
29  * Toggle SDA by changing the output value of the pin. This is only
30  * valid for pins configured as open drain (i.e. setting the value
31  * high effectively turns off the output driver.)
32  */
33 static void i2c_gpio_setsda_val(void *data, int state)
34 {
35 	struct i2c_gpio_private_data *priv = data;
36 
37 	gpiod_set_value(priv->sda, state);
38 }
39 
40 /*
41  * Toggle SCL by changing the output value of the pin. This is used
42  * for pins that are configured as open drain and for output-only
43  * pins. The latter case will break the i2c protocol, but it will
44  * often work in practice.
45  */
46 static void i2c_gpio_setscl_val(void *data, int state)
47 {
48 	struct i2c_gpio_private_data *priv = data;
49 
50 	gpiod_set_value(priv->scl, state);
51 }
52 
53 static int i2c_gpio_getsda(void *data)
54 {
55 	struct i2c_gpio_private_data *priv = data;
56 
57 	return gpiod_get_value(priv->sda);
58 }
59 
60 static int i2c_gpio_getscl(void *data)
61 {
62 	struct i2c_gpio_private_data *priv = data;
63 
64 	return gpiod_get_value(priv->scl);
65 }
66 
67 static void of_i2c_gpio_get_props(struct device_node *np,
68 				  struct i2c_gpio_platform_data *pdata)
69 {
70 	u32 reg;
71 
72 	of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
73 
74 	if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
75 		pdata->timeout = msecs_to_jiffies(reg);
76 
77 	pdata->sda_is_open_drain =
78 		of_property_read_bool(np, "i2c-gpio,sda-open-drain");
79 	pdata->scl_is_open_drain =
80 		of_property_read_bool(np, "i2c-gpio,scl-open-drain");
81 	pdata->scl_is_output_only =
82 		of_property_read_bool(np, "i2c-gpio,scl-output-only");
83 }
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 
121 static int i2c_gpio_probe(struct platform_device *pdev)
122 {
123 	struct i2c_gpio_private_data *priv;
124 	struct i2c_gpio_platform_data *pdata;
125 	struct i2c_algo_bit_data *bit_data;
126 	struct i2c_adapter *adap;
127 	struct device *dev = &pdev->dev;
128 	struct device_node *np = dev->of_node;
129 	enum gpiod_flags gflags;
130 	int ret;
131 
132 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
133 	if (!priv)
134 		return -ENOMEM;
135 
136 	adap = &priv->adap;
137 	bit_data = &priv->bit_data;
138 	pdata = &priv->pdata;
139 
140 	if (np) {
141 		of_i2c_gpio_get_props(np, pdata);
142 	} else {
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));
149 	}
150 
151 	/*
152 	 * First get the GPIO pins; if it fails, we'll defer the probe.
153 	 * If the SDA line is marked from platform data or device tree as
154 	 * "open drain" it means something outside of our control is making
155 	 * this line being handled as open drain, and we should just handle
156 	 * it as any other output. Else we enforce open drain as this is
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);
181 
182 	bit_data->setsda = i2c_gpio_setsda_val;
183 	bit_data->setscl = i2c_gpio_setscl_val;
184 
185 	if (!pdata->scl_is_output_only)
186 		bit_data->getscl = i2c_gpio_getscl;
187 	bit_data->getsda = i2c_gpio_getsda;
188 
189 	if (pdata->udelay)
190 		bit_data->udelay = pdata->udelay;
191 	else if (pdata->scl_is_output_only)
192 		bit_data->udelay = 50;			/* 10 kHz */
193 	else
194 		bit_data->udelay = 5;			/* 100 kHz */
195 
196 	if (pdata->timeout)
197 		bit_data->timeout = pdata->timeout;
198 	else
199 		bit_data->timeout = HZ / 10;		/* 100 ms */
200 
201 	bit_data->data = priv;
202 
203 	adap->owner = THIS_MODULE;
204 	if (np)
205 		strlcpy(adap->name, dev_name(dev), sizeof(adap->name));
206 	else
207 		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
208 
209 	adap->algo_data = bit_data;
210 	adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
211 	adap->dev.parent = dev;
212 	adap->dev.of_node = np;
213 
214 	adap->nr = pdev->id;
215 	ret = i2c_bit_add_numbered_bus(adap);
216 	if (ret)
217 		return ret;
218 
219 	platform_set_drvdata(pdev, priv);
220 
221 	/*
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),
228 		 pdata->scl_is_output_only
229 		 ? ", no clock stretching" : "");
230 
231 	return 0;
232 }
233 
234 static int i2c_gpio_remove(struct platform_device *pdev)
235 {
236 	struct i2c_gpio_private_data *priv;
237 	struct i2c_adapter *adap;
238 
239 	priv = platform_get_drvdata(pdev);
240 	adap = &priv->adap;
241 
242 	i2c_del_adapter(adap);
243 
244 	return 0;
245 }
246 
247 #if defined(CONFIG_OF)
248 static const struct of_device_id i2c_gpio_dt_ids[] = {
249 	{ .compatible = "i2c-gpio", },
250 	{ /* sentinel */ }
251 };
252 
253 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
254 #endif
255 
256 static struct platform_driver i2c_gpio_driver = {
257 	.driver		= {
258 		.name	= "i2c-gpio",
259 		.of_match_table	= of_match_ptr(i2c_gpio_dt_ids),
260 	},
261 	.probe		= i2c_gpio_probe,
262 	.remove		= i2c_gpio_remove,
263 };
264 
265 static int __init i2c_gpio_init(void)
266 {
267 	int ret;
268 
269 	ret = platform_driver_register(&i2c_gpio_driver);
270 	if (ret)
271 		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
272 
273 	return ret;
274 }
275 subsys_initcall(i2c_gpio_init);
276 
277 static void __exit i2c_gpio_exit(void)
278 {
279 	platform_driver_unregister(&i2c_gpio_driver);
280 }
281 module_exit(i2c_gpio_exit);
282 
283 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
284 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
285 MODULE_LICENSE("GPL");
286 MODULE_ALIAS("platform:i2c-gpio");
287