Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Bitbanging I2C bus driver using the GPIO API
  4 *
  5 * Copyright (C) 2007 Atmel Corporation
 
 
 
 
  6 */
  7#include <linux/completion.h>
  8#include <linux/debugfs.h>
  9#include <linux/delay.h>
 10#include <linux/gpio/consumer.h>
 11#include <linux/i2c-algo-bit.h>
 12#include <linux/i2c.h>
 
 
 13#include <linux/init.h>
 14#include <linux/interrupt.h>
 15#include <linux/module.h>
 16#include <linux/platform_data/i2c-gpio.h>
 17#include <linux/platform_device.h>
 18#include <linux/property.h>
 19#include <linux/slab.h>
 
 
 
 
 20
 21struct i2c_gpio_private_data {
 22	struct gpio_desc *sda;
 23	struct gpio_desc *scl;
 24	struct i2c_adapter adap;
 25	struct i2c_algo_bit_data bit_data;
 26	struct i2c_gpio_platform_data pdata;
 27#ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
 28	/* these must be protected by bus lock */
 29	struct completion scl_irq_completion;
 30	u64 scl_irq_data;
 31#endif
 32};
 33
 
 
 
 
 
 
 
 
 
 
 
 34/*
 35 * Toggle SDA by changing the output value of the pin. This is only
 36 * valid for pins configured as open drain (i.e. setting the value
 37 * high effectively turns off the output driver.)
 38 */
 39static void i2c_gpio_setsda_val(void *data, int state)
 40{
 41	struct i2c_gpio_private_data *priv = data;
 42
 43	gpiod_set_value_cansleep(priv->sda, state);
 
 
 
 
 
 
 
 
 
 
 
 44}
 45
 46/*
 47 * Toggle SCL by changing the output value of the pin. This is used
 48 * for pins that are configured as open drain and for output-only
 49 * pins. The latter case will break the i2c protocol, but it will
 50 * often work in practice.
 51 */
 52static void i2c_gpio_setscl_val(void *data, int state)
 53{
 54	struct i2c_gpio_private_data *priv = data;
 55
 56	gpiod_set_value_cansleep(priv->scl, state);
 57}
 58
 59static int i2c_gpio_getsda(void *data)
 60{
 61	struct i2c_gpio_private_data *priv = data;
 62
 63	return gpiod_get_value_cansleep(priv->sda);
 64}
 65
 66static int i2c_gpio_getscl(void *data)
 67{
 68	struct i2c_gpio_private_data *priv = data;
 69
 70	return gpiod_get_value_cansleep(priv->scl);
 71}
 72
 73#ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
 74
 75#define setsda(bd, val)	((bd)->setsda((bd)->data, val))
 76#define setscl(bd, val)	((bd)->setscl((bd)->data, val))
 77#define getsda(bd)	((bd)->getsda((bd)->data))
 78#define getscl(bd)	((bd)->getscl((bd)->data))
 79
 80#define WIRE_ATTRIBUTE(wire) \
 81static int fops_##wire##_get(void *data, u64 *val)		\
 82{								\
 83	struct i2c_gpio_private_data *priv = data;		\
 84								\
 85	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
 86	*val = get##wire(&priv->bit_data);			\
 87	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
 88	return 0;						\
 89}								\
 90static int fops_##wire##_set(void *data, u64 val)		\
 91{								\
 92	struct i2c_gpio_private_data *priv = data;		\
 93								\
 94	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
 95	set##wire(&priv->bit_data, val);			\
 96	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
 97	return 0;						\
 98}								\
 99DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
100
101WIRE_ATTRIBUTE(scl);
102WIRE_ATTRIBUTE(sda);
103
104static void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data *priv,
105					u32 pattern, u8 pattern_size)
106{
107	struct i2c_algo_bit_data *bit_data = &priv->bit_data;
108	int i;
109
110	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
111
112	/* START condition */
113	setsda(bit_data, 0);
114	udelay(bit_data->udelay);
115
116	/* Send pattern, request ACK, don't send STOP */
117	for (i = pattern_size - 1; i >= 0; i--) {
118		setscl(bit_data, 0);
119		udelay(bit_data->udelay / 2);
120		setsda(bit_data, (pattern >> i) & 1);
121		udelay((bit_data->udelay + 1) / 2);
122		setscl(bit_data, 1);
123		udelay(bit_data->udelay);
124	}
125
126	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
127}
128
129static int fops_incomplete_addr_phase_set(void *data, u64 addr)
130{
131	struct i2c_gpio_private_data *priv = data;
132	u32 pattern;
133
134	if (addr > 0x7f)
135		return -EINVAL;
136
137	/* ADDR (7 bit) + RD (1 bit) + Client ACK, keep SDA hi (1 bit) */
138	pattern = (addr << 2) | 3;
139
140	i2c_gpio_incomplete_transfer(priv, pattern, 9);
141
142	return 0;
143}
144DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase, NULL, fops_incomplete_addr_phase_set, "%llu\n");
145
146static int fops_incomplete_write_byte_set(void *data, u64 addr)
 
147{
148	struct i2c_gpio_private_data *priv = data;
149	u32 pattern;
150
151	if (addr > 0x7f)
152		return -EINVAL;
153
154	/* ADDR (7 bit) + WR (1 bit) + Client ACK (1 bit) */
155	pattern = (addr << 2) | 1;
156	/* 0x00 (8 bit) + Client ACK, keep SDA hi (1 bit) */
157	pattern = (pattern << 9) | 1;
158
159	i2c_gpio_incomplete_transfer(priv, pattern, 18);
 
 
 
 
160
161	return 0;
162}
163DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n");
164
165static int i2c_gpio_fi_act_on_scl_irq(struct i2c_gpio_private_data *priv,
166				       irqreturn_t handler(int, void*))
167{
168	int ret, irq = gpiod_to_irq(priv->scl);
169
170	if (irq < 0)
171		return irq;
172
173	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
174
175	ret = gpiod_direction_input(priv->scl);
176	if (ret)
177		goto unlock;
178
179	reinit_completion(&priv->scl_irq_completion);
180
181	ret = request_irq(irq, handler, IRQF_TRIGGER_FALLING,
182			  "i2c_gpio_fault_injector_scl_irq", priv);
183	if (ret)
184		goto output;
185
186	wait_for_completion_interruptible(&priv->scl_irq_completion);
187
188	free_irq(irq, priv);
189 output:
190	ret = gpiod_direction_output(priv->scl, 1) ?: ret;
191 unlock:
192	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
193
194	return ret;
195}
196
197static irqreturn_t lose_arbitration_irq(int irq, void *dev_id)
198{
199	struct i2c_gpio_private_data *priv = dev_id;
200
201	setsda(&priv->bit_data, 0);
202	udelay(priv->scl_irq_data);
203	setsda(&priv->bit_data, 1);
204
205	complete(&priv->scl_irq_completion);
206
207	return IRQ_HANDLED;
208}
209
210static int fops_lose_arbitration_set(void *data, u64 duration)
211{
212	struct i2c_gpio_private_data *priv = data;
213
214	if (duration > 100 * 1000)
215		return -EINVAL;
216
217	priv->scl_irq_data = duration;
218	/*
219	 * Interrupt on falling SCL. This ensures that the controller under test
220	 * has really started the transfer. Interrupt on falling SDA did only
221	 * exercise 'bus busy' detection on some HW but not 'arbitration lost'.
222	 * Note that the interrupt latency may cause the first bits to be
223	 * transmitted correctly.
224	 */
225	return i2c_gpio_fi_act_on_scl_irq(priv, lose_arbitration_irq);
226}
227DEFINE_DEBUGFS_ATTRIBUTE(fops_lose_arbitration, NULL, fops_lose_arbitration_set, "%llu\n");
228
229static irqreturn_t inject_panic_irq(int irq, void *dev_id)
230{
231	struct i2c_gpio_private_data *priv = dev_id;
232
233	udelay(priv->scl_irq_data);
234	panic("I2C fault injector induced panic");
235
236	return IRQ_HANDLED;
237}
238
239static int fops_inject_panic_set(void *data, u64 duration)
240{
241	struct i2c_gpio_private_data *priv = data;
242
243	if (duration > 100 * 1000)
244		return -EINVAL;
245
246	priv->scl_irq_data = duration;
247	/*
248	 * Interrupt on falling SCL. This ensures that the controller under test
249	 * has really started the transfer.
250	 */
251	return i2c_gpio_fi_act_on_scl_irq(priv, inject_panic_irq);
252}
253DEFINE_DEBUGFS_ATTRIBUTE(fops_inject_panic, NULL, fops_inject_panic_set, "%llu\n");
254
255static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
256{
257	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
258
259	init_completion(&priv->scl_irq_completion);
260
261	debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->adap.debugfs,
262				   priv, &fops_incomplete_addr_phase);
263	debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->adap.debugfs,
264				   priv, &fops_incomplete_write_byte);
265	if (priv->bit_data.getscl) {
266		debugfs_create_file_unsafe("inject_panic", 0200, priv->adap.debugfs,
267					   priv, &fops_inject_panic);
268		debugfs_create_file_unsafe("lose_arbitration", 0200, priv->adap.debugfs,
269					   priv, &fops_lose_arbitration);
270	}
271	debugfs_create_file_unsafe("scl", 0600, priv->adap.debugfs, priv, &fops_scl);
272	debugfs_create_file_unsafe("sda", 0600, priv->adap.debugfs, priv, &fops_sda);
273}
274#else
275static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
276#endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
277
278/* Get i2c-gpio properties from DT or ACPI table */
279static void i2c_gpio_get_properties(struct device *dev,
280				    struct i2c_gpio_platform_data *pdata)
281{
282	u32 reg;
283
284	device_property_read_u32(dev, "i2c-gpio,delay-us", &pdata->udelay);
285
286	if (!device_property_read_u32(dev, "i2c-gpio,timeout-ms", &reg))
287		pdata->timeout = msecs_to_jiffies(reg);
288
289	pdata->sda_is_open_drain =
290		device_property_read_bool(dev, "i2c-gpio,sda-open-drain");
291	pdata->scl_is_open_drain =
292		device_property_read_bool(dev, "i2c-gpio,scl-open-drain");
293	pdata->scl_is_output_only =
294		device_property_read_bool(dev, "i2c-gpio,scl-output-only");
295	pdata->sda_is_output_only =
296		device_property_read_bool(dev, "i2c-gpio,sda-output-only");
297	pdata->sda_has_no_pullup =
298		device_property_read_bool(dev, "i2c-gpio,sda-has-no-pullup");
299	pdata->scl_has_no_pullup =
300		device_property_read_bool(dev, "i2c-gpio,scl-has-no-pullup");
301}
302
303static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
304					   const char *con_id,
305					   unsigned int index,
306					   enum gpiod_flags gflags)
307{
308	struct gpio_desc *retdesc;
309	int ret;
310
311	retdesc = devm_gpiod_get(dev, con_id, gflags);
312	if (!IS_ERR(retdesc)) {
313		dev_dbg(dev, "got GPIO from name %s\n", con_id);
314		return retdesc;
315	}
316
317	retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);
318	if (!IS_ERR(retdesc)) {
319		dev_dbg(dev, "got GPIO from index %u\n", index);
320		return retdesc;
321	}
322
323	ret = PTR_ERR(retdesc);
324
325	/* FIXME: hack in the old code, is this really necessary? */
326	if (ret == -EINVAL)
327		retdesc = ERR_PTR(-EPROBE_DEFER);
328
329	/* This happens if the GPIO driver is not yet probed, let's defer */
330	if (ret == -ENOENT)
331		retdesc = ERR_PTR(-EPROBE_DEFER);
332
333	if (PTR_ERR(retdesc) != -EPROBE_DEFER)
334		dev_err(dev, "error trying to get descriptor: %d\n", ret);
335
336	return retdesc;
337}
338
339static int i2c_gpio_probe(struct platform_device *pdev)
340{
341	struct i2c_gpio_private_data *priv;
342	struct i2c_gpio_platform_data *pdata;
343	struct i2c_algo_bit_data *bit_data;
344	struct i2c_adapter *adap;
345	struct device *dev = &pdev->dev;
346	struct fwnode_handle *fwnode = dev_fwnode(dev);
347	enum gpiod_flags gflags;
348	int ret;
349
350	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
351	if (!priv)
352		return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
 
 
 
 
 
354	adap = &priv->adap;
355	bit_data = &priv->bit_data;
356	pdata = &priv->pdata;
357
358	if (fwnode) {
359		i2c_gpio_get_properties(dev, pdata);
 
 
360	} else {
361		/*
362		 * If all platform data settings are zero it is OK
363		 * to not provide any platform data from the board.
364		 */
365		if (dev_get_platdata(dev))
366			memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
367	}
368
369	/*
370	 * First get the GPIO pins; if it fails, we'll defer the probe.
371	 * If the SCL/SDA lines are marked "open drain" by platform data or
372	 * device tree then this means that something outside of our control is
373	 * marking these lines to be handled as open drain, and we should just
374	 * handle them as we handle any other output. Else we enforce open
375	 * drain as this is required for an I2C bus.
376	 */
377	if (pdata->sda_is_open_drain || pdata->sda_has_no_pullup)
378		gflags = GPIOD_OUT_HIGH;
379	else
380		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
381	priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
382	if (IS_ERR(priv->sda))
383		return PTR_ERR(priv->sda);
384
385	if (pdata->scl_is_open_drain || pdata->scl_has_no_pullup)
386		gflags = GPIOD_OUT_HIGH;
387	else
388		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
389	priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);
390	if (IS_ERR(priv->scl))
391		return PTR_ERR(priv->scl);
392
393	if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
394		dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
395	else
396		bit_data->can_do_atomic = true;
397
398	bit_data->setsda = i2c_gpio_setsda_val;
399	bit_data->setscl = i2c_gpio_setscl_val;
 
 
 
 
 
400
401	if (!pdata->scl_is_output_only)
402		bit_data->getscl = i2c_gpio_getscl;
403	if (!pdata->sda_is_output_only)
404		bit_data->getsda = i2c_gpio_getsda;
405
406	if (pdata->udelay)
407		bit_data->udelay = pdata->udelay;
408	else if (pdata->scl_is_output_only)
409		bit_data->udelay = 50;			/* 10 kHz */
410	else
411		bit_data->udelay = 5;			/* 100 kHz */
412
413	if (pdata->timeout)
414		bit_data->timeout = pdata->timeout;
415	else
416		bit_data->timeout = HZ / 10;		/* 100 ms */
417
418	bit_data->data = priv;
419
420	adap->owner = THIS_MODULE;
421	if (fwnode)
422		strscpy(adap->name, dev_name(dev), sizeof(adap->name));
423	else
424		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
425
426	adap->algo_data = bit_data;
427	adap->class = I2C_CLASS_HWMON;
428	adap->dev.parent = dev;
429	device_set_node(&adap->dev, fwnode);
430
431	adap->nr = pdev->id;
432	ret = i2c_bit_add_numbered_bus(adap);
433	if (ret)
434		return ret;
435
436	platform_set_drvdata(pdev, priv);
437
438	/*
439	 * FIXME: using global GPIO numbers is not helpful. If/when we
440	 * get accessors to get the actual name of the GPIO line,
441	 * from the descriptor, then provide that instead.
442	 */
443	dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
444		 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
445		 pdata->scl_is_output_only
446		 ? ", no clock stretching" : "");
447
448	i2c_gpio_fault_injector_init(pdev);
449
450	return 0;
 
 
 
 
 
 
 
451}
452
453static void i2c_gpio_remove(struct platform_device *pdev)
454{
455	struct i2c_gpio_private_data *priv;
 
456	struct i2c_adapter *adap;
457
458	priv = platform_get_drvdata(pdev);
459	adap = &priv->adap;
 
460
461	i2c_del_adapter(adap);
 
 
 
 
462}
463
 
464static const struct of_device_id i2c_gpio_dt_ids[] = {
465	{ .compatible = "i2c-gpio", },
466	{ /* sentinel */ }
467};
468
469MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
470
471static const struct acpi_device_id i2c_gpio_acpi_match[] = {
472	{ "LOON0005" }, /* LoongArch */
473	{ }
474};
475MODULE_DEVICE_TABLE(acpi, i2c_gpio_acpi_match);
476
477static struct platform_driver i2c_gpio_driver = {
478	.driver		= {
479		.name	= "i2c-gpio",
480		.of_match_table	= i2c_gpio_dt_ids,
481		.acpi_match_table = i2c_gpio_acpi_match,
482	},
483	.probe		= i2c_gpio_probe,
484	.remove		= i2c_gpio_remove,
485};
486
487static int __init i2c_gpio_init(void)
488{
489	int ret;
490
491	ret = platform_driver_register(&i2c_gpio_driver);
492	if (ret)
493		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
494
495	return ret;
496}
497subsys_initcall(i2c_gpio_init);
498
499static void __exit i2c_gpio_exit(void)
500{
501	platform_driver_unregister(&i2c_gpio_driver);
502}
503module_exit(i2c_gpio_exit);
504
505MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
506MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
507MODULE_LICENSE("GPL v2");
508MODULE_ALIAS("platform:i2c-gpio");
v3.15
 
  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.h>
 19#include <linux/of_gpio.h>
 20
 21struct i2c_gpio_private_data {
 
 
 22	struct i2c_adapter adap;
 23	struct i2c_algo_bit_data bit_data;
 24	struct i2c_gpio_platform_data pdata;
 
 
 
 
 
 25};
 26
 27/* Toggle SDA by changing the direction of the pin */
 28static 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/*
 39 * 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
 41 * high effectively turns off the output driver.)
 42 */
 43static void i2c_gpio_setsda_val(void *data, int state)
 44{
 45	struct i2c_gpio_platform_data *pdata = data;
 46
 47	gpio_set_value(pdata->sda_pin, state);
 48}
 49
 50/* Toggle SCL by changing the direction of the pin. */
 51static void i2c_gpio_setscl_dir(void *data, int state)
 52{
 53	struct i2c_gpio_platform_data *pdata = data;
 54
 55	if (state)
 56		gpio_direction_input(pdata->scl_pin);
 57	else
 58		gpio_direction_output(pdata->scl_pin, 0);
 59}
 60
 61/*
 62 * Toggle SCL by changing the output value of the pin. This is used
 63 * for pins that are configured as open drain and for output-only
 64 * pins. The latter case will break the i2c protocol, but it will
 65 * often work in practice.
 66 */
 67static void i2c_gpio_setscl_val(void *data, int state)
 68{
 69	struct i2c_gpio_platform_data *pdata = data;
 70
 71	gpio_set_value(pdata->scl_pin, state);
 72}
 73
 74static int i2c_gpio_getsda(void *data)
 75{
 76	struct i2c_gpio_platform_data *pdata = data;
 77
 78	return gpio_get_value(pdata->sda_pin);
 79}
 80
 81static int i2c_gpio_getscl(void *data)
 82{
 83	struct i2c_gpio_platform_data *pdata = data;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 84
 85	return gpio_get_value(pdata->scl_pin);
 
 
 86}
 
 87
 88static 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("%s: invalid GPIO pins, sda=%d/scl=%d\n",
102		       np->full_name, *sda_pin, *scl_pin);
103		return -ENODEV;
104	}
105
106	return 0;
107}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
109static void of_i2c_gpio_get_props(struct device_node *np,
110				  struct i2c_gpio_platform_data *pdata)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111{
112	u32 reg;
113
114	of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
115
116	if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
117		pdata->timeout = msecs_to_jiffies(reg);
118
119	pdata->sda_is_open_drain =
120		of_property_read_bool(np, "i2c-gpio,sda-open-drain");
121	pdata->scl_is_open_drain =
122		of_property_read_bool(np, "i2c-gpio,scl-open-drain");
123	pdata->scl_is_output_only =
124		of_property_read_bool(np, "i2c-gpio,scl-output-only");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125}
126
127static int i2c_gpio_probe(struct platform_device *pdev)
128{
129	struct i2c_gpio_private_data *priv;
130	struct i2c_gpio_platform_data *pdata;
131	struct i2c_algo_bit_data *bit_data;
132	struct i2c_adapter *adap;
133	unsigned int sda_pin, scl_pin;
 
 
134	int ret;
135
136	/* First get the GPIO pins; if it fails, we'll defer the probe. */
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 = gpio_request(sda_pin, "sda");
151	if (ret) {
152		if (ret == -EINVAL)
153			ret = -EPROBE_DEFER;	/* Try again later */
154		goto err_request_sda;
155	}
156	ret = gpio_request(scl_pin, "scl");
157	if (ret) {
158		if (ret == -EINVAL)
159			ret = -EPROBE_DEFER;	/* Try again later */
160		goto err_request_scl;
161	}
162
163	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
164	if (!priv) {
165		ret = -ENOMEM;
166		goto err_add_bus;
167	}
168	adap = &priv->adap;
169	bit_data = &priv->bit_data;
170	pdata = &priv->pdata;
171
172	if (pdev->dev.of_node) {
173		pdata->sda_pin = sda_pin;
174		pdata->scl_pin = scl_pin;
175		of_i2c_gpio_get_props(pdev->dev.of_node, pdata);
176	} else {
177		memcpy(pdata, dev_get_platdata(&pdev->dev), sizeof(*pdata));
 
 
 
 
 
178	}
179
180	if (pdata->sda_is_open_drain) {
181		gpio_direction_output(pdata->sda_pin, 1);
182		bit_data->setsda = i2c_gpio_setsda_val;
183	} else {
184		gpio_direction_input(pdata->sda_pin);
185		bit_data->setsda = i2c_gpio_setsda_dir;
186	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
188	if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
189		gpio_direction_output(pdata->scl_pin, 1);
190		bit_data->setscl = i2c_gpio_setscl_val;
191	} else {
192		gpio_direction_input(pdata->scl_pin);
193		bit_data->setscl = i2c_gpio_setscl_dir;
194	}
195
196	if (!pdata->scl_is_output_only)
197		bit_data->getscl = i2c_gpio_getscl;
198	bit_data->getsda = i2c_gpio_getsda;
 
199
200	if (pdata->udelay)
201		bit_data->udelay = pdata->udelay;
202	else if (pdata->scl_is_output_only)
203		bit_data->udelay = 50;			/* 10 kHz */
204	else
205		bit_data->udelay = 5;			/* 100 kHz */
206
207	if (pdata->timeout)
208		bit_data->timeout = pdata->timeout;
209	else
210		bit_data->timeout = HZ / 10;		/* 100 ms */
211
212	bit_data->data = pdata;
213
214	adap->owner = THIS_MODULE;
215	if (pdev->dev.of_node)
216		strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
217	else
218		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
219
220	adap->algo_data = bit_data;
221	adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
222	adap->dev.parent = &pdev->dev;
223	adap->dev.of_node = pdev->dev.of_node;
224
225	adap->nr = pdev->id;
226	ret = i2c_bit_add_numbered_bus(adap);
227	if (ret)
228		goto err_add_bus;
229
230	platform_set_drvdata(pdev, priv);
231
232	dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
233		 pdata->sda_pin, pdata->scl_pin,
 
 
 
 
 
234		 pdata->scl_is_output_only
235		 ? ", no clock stretching" : "");
236
 
 
237	return 0;
238
239err_add_bus:
240	gpio_free(scl_pin);
241err_request_scl:
242	gpio_free(sda_pin);
243err_request_sda:
244	return ret;
245}
246
247static int i2c_gpio_remove(struct platform_device *pdev)
248{
249	struct i2c_gpio_private_data *priv;
250	struct i2c_gpio_platform_data *pdata;
251	struct i2c_adapter *adap;
252
253	priv = platform_get_drvdata(pdev);
254	adap = &priv->adap;
255	pdata = &priv->pdata;
256
257	i2c_del_adapter(adap);
258	gpio_free(pdata->scl_pin);
259	gpio_free(pdata->sda_pin);
260
261	return 0;
262}
263
264#if defined(CONFIG_OF)
265static const struct of_device_id i2c_gpio_dt_ids[] = {
266	{ .compatible = "i2c-gpio", },
267	{ /* sentinel */ }
268};
269
270MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
271#endif
 
 
 
 
 
272
273static struct platform_driver i2c_gpio_driver = {
274	.driver		= {
275		.name	= "i2c-gpio",
276		.owner	= THIS_MODULE,
277		.of_match_table	= of_match_ptr(i2c_gpio_dt_ids),
278	},
279	.probe		= i2c_gpio_probe,
280	.remove		= i2c_gpio_remove,
281};
282
283static int __init i2c_gpio_init(void)
284{
285	int ret;
286
287	ret = platform_driver_register(&i2c_gpio_driver);
288	if (ret)
289		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
290
291	return ret;
292}
293subsys_initcall(i2c_gpio_init);
294
295static void __exit i2c_gpio_exit(void)
296{
297	platform_driver_unregister(&i2c_gpio_driver);
298}
299module_exit(i2c_gpio_exit);
300
301MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
302MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
303MODULE_LICENSE("GPL");
304MODULE_ALIAS("platform:i2c-gpio");