18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Bitbanging I2C bus driver using the GPIO API
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2007 Atmel Corporation
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/completion.h>
88c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
118c2ecf20Sopenharmony_ci#include <linux/i2c-algo-bit.h>
128c2ecf20Sopenharmony_ci#include <linux/i2c.h>
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/of.h>
178c2ecf20Sopenharmony_ci#include <linux/platform_data/i2c-gpio.h>
188c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistruct i2c_gpio_private_data {
228c2ecf20Sopenharmony_ci	struct gpio_desc *sda;
238c2ecf20Sopenharmony_ci	struct gpio_desc *scl;
248c2ecf20Sopenharmony_ci	struct i2c_adapter adap;
258c2ecf20Sopenharmony_ci	struct i2c_algo_bit_data bit_data;
268c2ecf20Sopenharmony_ci	struct i2c_gpio_platform_data pdata;
278c2ecf20Sopenharmony_ci#ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
288c2ecf20Sopenharmony_ci	struct dentry *debug_dir;
298c2ecf20Sopenharmony_ci	/* these must be protected by bus lock */
308c2ecf20Sopenharmony_ci	struct completion scl_irq_completion;
318c2ecf20Sopenharmony_ci	u64 scl_irq_data;
328c2ecf20Sopenharmony_ci#endif
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/*
368c2ecf20Sopenharmony_ci * Toggle SDA by changing the output value of the pin. This is only
378c2ecf20Sopenharmony_ci * valid for pins configured as open drain (i.e. setting the value
388c2ecf20Sopenharmony_ci * high effectively turns off the output driver.)
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_cistatic void i2c_gpio_setsda_val(void *data, int state)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = data;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(priv->sda, state);
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/*
488c2ecf20Sopenharmony_ci * Toggle SCL by changing the output value of the pin. This is used
498c2ecf20Sopenharmony_ci * for pins that are configured as open drain and for output-only
508c2ecf20Sopenharmony_ci * pins. The latter case will break the i2c protocol, but it will
518c2ecf20Sopenharmony_ci * often work in practice.
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_cistatic void i2c_gpio_setscl_val(void *data, int state)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = data;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(priv->scl, state);
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic int i2c_gpio_getsda(void *data)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = data;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	return gpiod_get_value_cansleep(priv->sda);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int i2c_gpio_getscl(void *data)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = data;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	return gpiod_get_value_cansleep(priv->scl);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
758c2ecf20Sopenharmony_cistatic struct dentry *i2c_gpio_debug_dir;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define setsda(bd, val)	((bd)->setsda((bd)->data, val))
788c2ecf20Sopenharmony_ci#define setscl(bd, val)	((bd)->setscl((bd)->data, val))
798c2ecf20Sopenharmony_ci#define getsda(bd)	((bd)->getsda((bd)->data))
808c2ecf20Sopenharmony_ci#define getscl(bd)	((bd)->getscl((bd)->data))
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define WIRE_ATTRIBUTE(wire) \
838c2ecf20Sopenharmony_cistatic int fops_##wire##_get(void *data, u64 *val)		\
848c2ecf20Sopenharmony_ci{								\
858c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = data;		\
868c2ecf20Sopenharmony_ci								\
878c2ecf20Sopenharmony_ci	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
888c2ecf20Sopenharmony_ci	*val = get##wire(&priv->bit_data);			\
898c2ecf20Sopenharmony_ci	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
908c2ecf20Sopenharmony_ci	return 0;						\
918c2ecf20Sopenharmony_ci}								\
928c2ecf20Sopenharmony_cistatic int fops_##wire##_set(void *data, u64 val)		\
938c2ecf20Sopenharmony_ci{								\
948c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = data;		\
958c2ecf20Sopenharmony_ci								\
968c2ecf20Sopenharmony_ci	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
978c2ecf20Sopenharmony_ci	set##wire(&priv->bit_data, val);			\
988c2ecf20Sopenharmony_ci	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
998c2ecf20Sopenharmony_ci	return 0;						\
1008c2ecf20Sopenharmony_ci}								\
1018c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciWIRE_ATTRIBUTE(scl);
1048c2ecf20Sopenharmony_ciWIRE_ATTRIBUTE(sda);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data *priv,
1078c2ecf20Sopenharmony_ci					u32 pattern, u8 pattern_size)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct i2c_algo_bit_data *bit_data = &priv->bit_data;
1108c2ecf20Sopenharmony_ci	int i;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	/* START condition */
1158c2ecf20Sopenharmony_ci	setsda(bit_data, 0);
1168c2ecf20Sopenharmony_ci	udelay(bit_data->udelay);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/* Send pattern, request ACK, don't send STOP */
1198c2ecf20Sopenharmony_ci	for (i = pattern_size - 1; i >= 0; i--) {
1208c2ecf20Sopenharmony_ci		setscl(bit_data, 0);
1218c2ecf20Sopenharmony_ci		udelay(bit_data->udelay / 2);
1228c2ecf20Sopenharmony_ci		setsda(bit_data, (pattern >> i) & 1);
1238c2ecf20Sopenharmony_ci		udelay((bit_data->udelay + 1) / 2);
1248c2ecf20Sopenharmony_ci		setscl(bit_data, 1);
1258c2ecf20Sopenharmony_ci		udelay(bit_data->udelay);
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic int fops_incomplete_addr_phase_set(void *data, u64 addr)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = data;
1348c2ecf20Sopenharmony_ci	u32 pattern;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	if (addr > 0x7f)
1378c2ecf20Sopenharmony_ci		return -EINVAL;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/* ADDR (7 bit) + RD (1 bit) + Client ACK, keep SDA hi (1 bit) */
1408c2ecf20Sopenharmony_ci	pattern = (addr << 2) | 3;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	i2c_gpio_incomplete_transfer(priv, pattern, 9);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	return 0;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase, NULL, fops_incomplete_addr_phase_set, "%llu\n");
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic int fops_incomplete_write_byte_set(void *data, u64 addr)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = data;
1518c2ecf20Sopenharmony_ci	u32 pattern;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	if (addr > 0x7f)
1548c2ecf20Sopenharmony_ci		return -EINVAL;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* ADDR (7 bit) + WR (1 bit) + Client ACK (1 bit) */
1578c2ecf20Sopenharmony_ci	pattern = (addr << 2) | 1;
1588c2ecf20Sopenharmony_ci	/* 0x00 (8 bit) + Client ACK, keep SDA hi (1 bit) */
1598c2ecf20Sopenharmony_ci	pattern = (pattern << 9) | 1;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	i2c_gpio_incomplete_transfer(priv, pattern, 18);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	return 0;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n");
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int i2c_gpio_fi_act_on_scl_irq(struct i2c_gpio_private_data *priv,
1688c2ecf20Sopenharmony_ci				       irqreturn_t handler(int, void*))
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	int ret, irq = gpiod_to_irq(priv->scl);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	if (irq < 0)
1738c2ecf20Sopenharmony_ci		return irq;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	ret = gpiod_direction_input(priv->scl);
1788c2ecf20Sopenharmony_ci	if (ret)
1798c2ecf20Sopenharmony_ci		goto unlock;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	reinit_completion(&priv->scl_irq_completion);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	ret = request_irq(irq, handler, IRQF_TRIGGER_FALLING,
1848c2ecf20Sopenharmony_ci			  "i2c_gpio_fault_injector_scl_irq", priv);
1858c2ecf20Sopenharmony_ci	if (ret)
1868c2ecf20Sopenharmony_ci		goto output;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	wait_for_completion_interruptible(&priv->scl_irq_completion);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	free_irq(irq, priv);
1918c2ecf20Sopenharmony_ci output:
1928c2ecf20Sopenharmony_ci	ret = gpiod_direction_output(priv->scl, 1) ?: ret;
1938c2ecf20Sopenharmony_ci unlock:
1948c2ecf20Sopenharmony_ci	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	return ret;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic irqreturn_t lose_arbitration_irq(int irq, void *dev_id)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = dev_id;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	setsda(&priv->bit_data, 0);
2048c2ecf20Sopenharmony_ci	udelay(priv->scl_irq_data);
2058c2ecf20Sopenharmony_ci	setsda(&priv->bit_data, 1);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	complete(&priv->scl_irq_completion);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic int fops_lose_arbitration_set(void *data, u64 duration)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = data;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	if (duration > 100 * 1000)
2178c2ecf20Sopenharmony_ci		return -EINVAL;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	priv->scl_irq_data = duration;
2208c2ecf20Sopenharmony_ci	/*
2218c2ecf20Sopenharmony_ci	 * Interrupt on falling SCL. This ensures that the master under test has
2228c2ecf20Sopenharmony_ci	 * really started the transfer. Interrupt on falling SDA did only
2238c2ecf20Sopenharmony_ci	 * exercise 'bus busy' detection on some HW but not 'arbitration lost'.
2248c2ecf20Sopenharmony_ci	 * Note that the interrupt latency may cause the first bits to be
2258c2ecf20Sopenharmony_ci	 * transmitted correctly.
2268c2ecf20Sopenharmony_ci	 */
2278c2ecf20Sopenharmony_ci	return i2c_gpio_fi_act_on_scl_irq(priv, lose_arbitration_irq);
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_lose_arbitration, NULL, fops_lose_arbitration_set, "%llu\n");
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic irqreturn_t inject_panic_irq(int irq, void *dev_id)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = dev_id;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	udelay(priv->scl_irq_data);
2368c2ecf20Sopenharmony_ci	panic("I2C fault injector induced panic");
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic int fops_inject_panic_set(void *data, u64 duration)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = data;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	if (duration > 100 * 1000)
2468c2ecf20Sopenharmony_ci		return -EINVAL;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	priv->scl_irq_data = duration;
2498c2ecf20Sopenharmony_ci	/*
2508c2ecf20Sopenharmony_ci	 * Interrupt on falling SCL. This ensures that the master under test has
2518c2ecf20Sopenharmony_ci	 * really started the transfer.
2528c2ecf20Sopenharmony_ci	 */
2538c2ecf20Sopenharmony_ci	return i2c_gpio_fi_act_on_scl_irq(priv, inject_panic_irq);
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_inject_panic, NULL, fops_inject_panic_set, "%llu\n");
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic void i2c_gpio_fault_injector_init(struct platform_device *pdev)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/*
2628c2ecf20Sopenharmony_ci	 * If there will be a debugfs-dir per i2c adapter somewhen, put the
2638c2ecf20Sopenharmony_ci	 * 'fault-injector' dir there. Until then, we have a global dir with
2648c2ecf20Sopenharmony_ci	 * all adapters as subdirs.
2658c2ecf20Sopenharmony_ci	 */
2668c2ecf20Sopenharmony_ci	if (!i2c_gpio_debug_dir) {
2678c2ecf20Sopenharmony_ci		i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL);
2688c2ecf20Sopenharmony_ci		if (!i2c_gpio_debug_dir)
2698c2ecf20Sopenharmony_ci			return;
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir);
2738c2ecf20Sopenharmony_ci	if (!priv->debug_dir)
2748c2ecf20Sopenharmony_ci		return;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	init_completion(&priv->scl_irq_completion);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->debug_dir,
2798c2ecf20Sopenharmony_ci				   priv, &fops_incomplete_addr_phase);
2808c2ecf20Sopenharmony_ci	debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->debug_dir,
2818c2ecf20Sopenharmony_ci				   priv, &fops_incomplete_write_byte);
2828c2ecf20Sopenharmony_ci	if (priv->bit_data.getscl) {
2838c2ecf20Sopenharmony_ci		debugfs_create_file_unsafe("inject_panic", 0200, priv->debug_dir,
2848c2ecf20Sopenharmony_ci					   priv, &fops_inject_panic);
2858c2ecf20Sopenharmony_ci		debugfs_create_file_unsafe("lose_arbitration", 0200, priv->debug_dir,
2868c2ecf20Sopenharmony_ci					   priv, &fops_lose_arbitration);
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci	debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
2898c2ecf20Sopenharmony_ci	debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	debugfs_remove_recursive(priv->debug_dir);
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci#else
2998c2ecf20Sopenharmony_cistatic inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
3008c2ecf20Sopenharmony_cistatic inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
3018c2ecf20Sopenharmony_ci#endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic void of_i2c_gpio_get_props(struct device_node *np,
3048c2ecf20Sopenharmony_ci				  struct i2c_gpio_platform_data *pdata)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	u32 reg;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
3118c2ecf20Sopenharmony_ci		pdata->timeout = msecs_to_jiffies(reg);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	pdata->sda_is_open_drain =
3148c2ecf20Sopenharmony_ci		of_property_read_bool(np, "i2c-gpio,sda-open-drain");
3158c2ecf20Sopenharmony_ci	pdata->scl_is_open_drain =
3168c2ecf20Sopenharmony_ci		of_property_read_bool(np, "i2c-gpio,scl-open-drain");
3178c2ecf20Sopenharmony_ci	pdata->scl_is_output_only =
3188c2ecf20Sopenharmony_ci		of_property_read_bool(np, "i2c-gpio,scl-output-only");
3198c2ecf20Sopenharmony_ci}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_cistatic struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
3228c2ecf20Sopenharmony_ci					   const char *con_id,
3238c2ecf20Sopenharmony_ci					   unsigned int index,
3248c2ecf20Sopenharmony_ci					   enum gpiod_flags gflags)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct gpio_desc *retdesc;
3278c2ecf20Sopenharmony_ci	int ret;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	retdesc = devm_gpiod_get(dev, con_id, gflags);
3308c2ecf20Sopenharmony_ci	if (!IS_ERR(retdesc)) {
3318c2ecf20Sopenharmony_ci		dev_dbg(dev, "got GPIO from name %s\n", con_id);
3328c2ecf20Sopenharmony_ci		return retdesc;
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);
3368c2ecf20Sopenharmony_ci	if (!IS_ERR(retdesc)) {
3378c2ecf20Sopenharmony_ci		dev_dbg(dev, "got GPIO from index %u\n", index);
3388c2ecf20Sopenharmony_ci		return retdesc;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	ret = PTR_ERR(retdesc);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	/* FIXME: hack in the old code, is this really necessary? */
3448c2ecf20Sopenharmony_ci	if (ret == -EINVAL)
3458c2ecf20Sopenharmony_ci		retdesc = ERR_PTR(-EPROBE_DEFER);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	/* This happens if the GPIO driver is not yet probed, let's defer */
3488c2ecf20Sopenharmony_ci	if (ret == -ENOENT)
3498c2ecf20Sopenharmony_ci		retdesc = ERR_PTR(-EPROBE_DEFER);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	if (PTR_ERR(retdesc) != -EPROBE_DEFER)
3528c2ecf20Sopenharmony_ci		dev_err(dev, "error trying to get descriptor: %d\n", ret);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	return retdesc;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic int i2c_gpio_probe(struct platform_device *pdev)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv;
3608c2ecf20Sopenharmony_ci	struct i2c_gpio_platform_data *pdata;
3618c2ecf20Sopenharmony_ci	struct i2c_algo_bit_data *bit_data;
3628c2ecf20Sopenharmony_ci	struct i2c_adapter *adap;
3638c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
3648c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
3658c2ecf20Sopenharmony_ci	enum gpiod_flags gflags;
3668c2ecf20Sopenharmony_ci	int ret;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
3698c2ecf20Sopenharmony_ci	if (!priv)
3708c2ecf20Sopenharmony_ci		return -ENOMEM;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	adap = &priv->adap;
3738c2ecf20Sopenharmony_ci	bit_data = &priv->bit_data;
3748c2ecf20Sopenharmony_ci	pdata = &priv->pdata;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	if (np) {
3778c2ecf20Sopenharmony_ci		of_i2c_gpio_get_props(np, pdata);
3788c2ecf20Sopenharmony_ci	} else {
3798c2ecf20Sopenharmony_ci		/*
3808c2ecf20Sopenharmony_ci		 * If all platform data settings are zero it is OK
3818c2ecf20Sopenharmony_ci		 * to not provide any platform data from the board.
3828c2ecf20Sopenharmony_ci		 */
3838c2ecf20Sopenharmony_ci		if (dev_get_platdata(dev))
3848c2ecf20Sopenharmony_ci			memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
3858c2ecf20Sopenharmony_ci	}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	/*
3888c2ecf20Sopenharmony_ci	 * First get the GPIO pins; if it fails, we'll defer the probe.
3898c2ecf20Sopenharmony_ci	 * If the SCL/SDA lines are marked "open drain" by platform data or
3908c2ecf20Sopenharmony_ci	 * device tree then this means that something outside of our control is
3918c2ecf20Sopenharmony_ci	 * marking these lines to be handled as open drain, and we should just
3928c2ecf20Sopenharmony_ci	 * handle them as we handle any other output. Else we enforce open
3938c2ecf20Sopenharmony_ci	 * drain as this is required for an I2C bus.
3948c2ecf20Sopenharmony_ci	 */
3958c2ecf20Sopenharmony_ci	if (pdata->sda_is_open_drain)
3968c2ecf20Sopenharmony_ci		gflags = GPIOD_OUT_HIGH;
3978c2ecf20Sopenharmony_ci	else
3988c2ecf20Sopenharmony_ci		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
3998c2ecf20Sopenharmony_ci	priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
4008c2ecf20Sopenharmony_ci	if (IS_ERR(priv->sda))
4018c2ecf20Sopenharmony_ci		return PTR_ERR(priv->sda);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	if (pdata->scl_is_open_drain)
4048c2ecf20Sopenharmony_ci		gflags = GPIOD_OUT_HIGH;
4058c2ecf20Sopenharmony_ci	else
4068c2ecf20Sopenharmony_ci		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
4078c2ecf20Sopenharmony_ci	priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);
4088c2ecf20Sopenharmony_ci	if (IS_ERR(priv->scl))
4098c2ecf20Sopenharmony_ci		return PTR_ERR(priv->scl);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
4128c2ecf20Sopenharmony_ci		dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
4138c2ecf20Sopenharmony_ci	else
4148c2ecf20Sopenharmony_ci		bit_data->can_do_atomic = true;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	bit_data->setsda = i2c_gpio_setsda_val;
4178c2ecf20Sopenharmony_ci	bit_data->setscl = i2c_gpio_setscl_val;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	if (!pdata->scl_is_output_only)
4208c2ecf20Sopenharmony_ci		bit_data->getscl = i2c_gpio_getscl;
4218c2ecf20Sopenharmony_ci	bit_data->getsda = i2c_gpio_getsda;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	if (pdata->udelay)
4248c2ecf20Sopenharmony_ci		bit_data->udelay = pdata->udelay;
4258c2ecf20Sopenharmony_ci	else if (pdata->scl_is_output_only)
4268c2ecf20Sopenharmony_ci		bit_data->udelay = 50;			/* 10 kHz */
4278c2ecf20Sopenharmony_ci	else
4288c2ecf20Sopenharmony_ci		bit_data->udelay = 5;			/* 100 kHz */
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	if (pdata->timeout)
4318c2ecf20Sopenharmony_ci		bit_data->timeout = pdata->timeout;
4328c2ecf20Sopenharmony_ci	else
4338c2ecf20Sopenharmony_ci		bit_data->timeout = HZ / 10;		/* 100 ms */
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	bit_data->data = priv;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	adap->owner = THIS_MODULE;
4388c2ecf20Sopenharmony_ci	if (np)
4398c2ecf20Sopenharmony_ci		strlcpy(adap->name, dev_name(dev), sizeof(adap->name));
4408c2ecf20Sopenharmony_ci	else
4418c2ecf20Sopenharmony_ci		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	adap->algo_data = bit_data;
4448c2ecf20Sopenharmony_ci	adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
4458c2ecf20Sopenharmony_ci	adap->dev.parent = dev;
4468c2ecf20Sopenharmony_ci	adap->dev.of_node = np;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	adap->nr = pdev->id;
4498c2ecf20Sopenharmony_ci	ret = i2c_bit_add_numbered_bus(adap);
4508c2ecf20Sopenharmony_ci	if (ret)
4518c2ecf20Sopenharmony_ci		return ret;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, priv);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	/*
4568c2ecf20Sopenharmony_ci	 * FIXME: using global GPIO numbers is not helpful. If/when we
4578c2ecf20Sopenharmony_ci	 * get accessors to get the actual name of the GPIO line,
4588c2ecf20Sopenharmony_ci	 * from the descriptor, then provide that instead.
4598c2ecf20Sopenharmony_ci	 */
4608c2ecf20Sopenharmony_ci	dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
4618c2ecf20Sopenharmony_ci		 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
4628c2ecf20Sopenharmony_ci		 pdata->scl_is_output_only
4638c2ecf20Sopenharmony_ci		 ? ", no clock stretching" : "");
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	i2c_gpio_fault_injector_init(pdev);
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	return 0;
4688c2ecf20Sopenharmony_ci}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_cistatic int i2c_gpio_remove(struct platform_device *pdev)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	struct i2c_gpio_private_data *priv;
4738c2ecf20Sopenharmony_ci	struct i2c_adapter *adap;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	i2c_gpio_fault_injector_exit(pdev);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	priv = platform_get_drvdata(pdev);
4788c2ecf20Sopenharmony_ci	adap = &priv->adap;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	i2c_del_adapter(adap);
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	return 0;
4838c2ecf20Sopenharmony_ci}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci#if defined(CONFIG_OF)
4868c2ecf20Sopenharmony_cistatic const struct of_device_id i2c_gpio_dt_ids[] = {
4878c2ecf20Sopenharmony_ci	{ .compatible = "i2c-gpio", },
4888c2ecf20Sopenharmony_ci	{ /* sentinel */ }
4898c2ecf20Sopenharmony_ci};
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
4928c2ecf20Sopenharmony_ci#endif
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cistatic struct platform_driver i2c_gpio_driver = {
4958c2ecf20Sopenharmony_ci	.driver		= {
4968c2ecf20Sopenharmony_ci		.name	= "i2c-gpio",
4978c2ecf20Sopenharmony_ci		.of_match_table	= of_match_ptr(i2c_gpio_dt_ids),
4988c2ecf20Sopenharmony_ci	},
4998c2ecf20Sopenharmony_ci	.probe		= i2c_gpio_probe,
5008c2ecf20Sopenharmony_ci	.remove		= i2c_gpio_remove,
5018c2ecf20Sopenharmony_ci};
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_cistatic int __init i2c_gpio_init(void)
5048c2ecf20Sopenharmony_ci{
5058c2ecf20Sopenharmony_ci	int ret;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	ret = platform_driver_register(&i2c_gpio_driver);
5088c2ecf20Sopenharmony_ci	if (ret)
5098c2ecf20Sopenharmony_ci		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	return ret;
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_cisubsys_initcall(i2c_gpio_init);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_cistatic void __exit i2c_gpio_exit(void)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	platform_driver_unregister(&i2c_gpio_driver);
5188c2ecf20Sopenharmony_ci}
5198c2ecf20Sopenharmony_cimodule_exit(i2c_gpio_exit);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ciMODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
5228c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
5238c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
5248c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:i2c-gpio");
525