i2c-gpio.c 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Bitbanging I2C bus driver using the GPIO API
 *
 * Copyright (C) 2007 Atmel Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
W
Wolfram Sang 已提交
10 11
#include <linux/debugfs.h>
#include <linux/delay.h>
12 13
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>
14
#include <linux/platform_data/i2c-gpio.h>
15 16
#include <linux/init.h>
#include <linux/module.h>
17
#include <linux/slab.h>
18
#include <linux/platform_device.h>
19
#include <linux/gpio/consumer.h>
S
Sachin Kamat 已提交
20
#include <linux/of.h>
21 22

struct i2c_gpio_private_data {
23 24
	struct gpio_desc *sda;
	struct gpio_desc *scl;
25 26 27
	struct i2c_adapter adap;
	struct i2c_algo_bit_data bit_data;
	struct i2c_gpio_platform_data pdata;
W
Wolfram Sang 已提交
28 29 30
#ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
	struct dentry *debug_dir;
#endif
31
};
32 33 34 35 36 37 38 39

/*
 * Toggle SDA by changing the output value of the pin. This is only
 * valid for pins configured as open drain (i.e. setting the value
 * high effectively turns off the output driver.)
 */
static void i2c_gpio_setsda_val(void *data, int state)
{
40
	struct i2c_gpio_private_data *priv = data;
41

42
	gpiod_set_value_cansleep(priv->sda, state);
43 44 45 46 47 48 49 50 51 52
}

/*
 * Toggle SCL by changing the output value of the pin. This is used
 * for pins that are configured as open drain and for output-only
 * pins. The latter case will break the i2c protocol, but it will
 * often work in practice.
 */
static void i2c_gpio_setscl_val(void *data, int state)
{
53
	struct i2c_gpio_private_data *priv = data;
54

55
	gpiod_set_value_cansleep(priv->scl, state);
56 57
}

58
static int i2c_gpio_getsda(void *data)
59
{
60
	struct i2c_gpio_private_data *priv = data;
61

62
	return gpiod_get_value_cansleep(priv->sda);
63 64
}

65
static int i2c_gpio_getscl(void *data)
66
{
67
	struct i2c_gpio_private_data *priv = data;
68

69
	return gpiod_get_value_cansleep(priv->scl);
70 71
}

W
Wolfram Sang 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
static struct dentry *i2c_gpio_debug_dir;

#define setsda(bd, val)	((bd)->setsda((bd)->data, val))
#define setscl(bd, val)	((bd)->setscl((bd)->data, val))
#define getsda(bd)	((bd)->getsda((bd)->data))
#define getscl(bd)	((bd)->getscl((bd)->data))

#define WIRE_ATTRIBUTE(wire) \
static int fops_##wire##_get(void *data, u64 *val)	\
{							\
	struct i2c_gpio_private_data *priv = data;	\
							\
	i2c_lock_adapter(&priv->adap);			\
	*val = get##wire(&priv->bit_data);		\
	i2c_unlock_adapter(&priv->adap);		\
	return 0;					\
}							\
static int fops_##wire##_set(void *data, u64 val)	\
{							\
	struct i2c_gpio_private_data *priv = data;	\
							\
	i2c_lock_adapter(&priv->adap);			\
	set##wire(&priv->bit_data, val);		\
	i2c_unlock_adapter(&priv->adap);		\
	return 0;					\
}							\
DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")

WIRE_ATTRIBUTE(scl);
WIRE_ATTRIBUTE(sda);

static int fops_incomplete_transfer_set(void *data, u64 addr)
{
	struct i2c_gpio_private_data *priv = data;
	struct i2c_algo_bit_data *bit_data = &priv->bit_data;
	int i, pattern;

	if (addr > 0x7f)
		return -EINVAL;

	/* ADDR (7 bit) + RD (1 bit) + SDA hi (1 bit) */
	pattern = (addr << 2) | 3;

	i2c_lock_adapter(&priv->adap);

	/* START condition */
	setsda(bit_data, 0);
	udelay(bit_data->udelay);

	/* Send ADDR+RD, request ACK, don't send STOP */
	for (i = 8; i >= 0; i--) {
		setscl(bit_data, 0);
		udelay(bit_data->udelay / 2);
		setsda(bit_data, (pattern >> i) & 1);
		udelay((bit_data->udelay + 1) / 2);
		setscl(bit_data, 1);
		udelay(bit_data->udelay);
	}

	i2c_unlock_adapter(&priv->adap);

	return 0;
}
DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_transfer, NULL, fops_incomplete_transfer_set, "%llu\n");

static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
{
	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);

	/*
	 * If there will be a debugfs-dir per i2c adapter somewhen, put the
	 * 'fault-injector' dir there. Until then, we have a global dir with
	 * all adapters as subdirs.
	 */
	if (!i2c_gpio_debug_dir) {
		i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL);
		if (!i2c_gpio_debug_dir)
			return;
	}

	priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir);
	if (!priv->debug_dir)
		return;

	debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
	debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
	debugfs_create_file_unsafe("incomplete_transfer", 0200, priv->debug_dir,
				   priv, &fops_incomplete_transfer);
}

static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
{
	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);

	debugfs_remove_recursive(priv->debug_dir);
}
#else
static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
#endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/

174 175 176 177 178
static void of_i2c_gpio_get_props(struct device_node *np,
				  struct i2c_gpio_platform_data *pdata)
{
	u32 reg;

179 180 181 182 183 184 185 186 187 188 189 190 191
	of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);

	if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
		pdata->timeout = msecs_to_jiffies(reg);

	pdata->sda_is_open_drain =
		of_property_read_bool(np, "i2c-gpio,sda-open-drain");
	pdata->scl_is_open_drain =
		of_property_read_bool(np, "i2c-gpio,scl-open-drain");
	pdata->scl_is_output_only =
		of_property_read_bool(np, "i2c-gpio,scl-output-only");
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
					   const char *con_id,
					   unsigned int index,
					   enum gpiod_flags gflags)
{
	struct gpio_desc *retdesc;
	int ret;

	retdesc = devm_gpiod_get(dev, con_id, gflags);
	if (!IS_ERR(retdesc)) {
		dev_dbg(dev, "got GPIO from name %s\n", con_id);
		return retdesc;
	}

	retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);
	if (!IS_ERR(retdesc)) {
		dev_dbg(dev, "got GPIO from index %u\n", index);
		return retdesc;
	}

	ret = PTR_ERR(retdesc);

	/* FIXME: hack in the old code, is this really necessary? */
	if (ret == -EINVAL)
		retdesc = ERR_PTR(-EPROBE_DEFER);

	/* This happens if the GPIO driver is not yet probed, let's defer */
	if (ret == -ENOENT)
		retdesc = ERR_PTR(-EPROBE_DEFER);

	if (ret != -EPROBE_DEFER)
		dev_err(dev, "error trying to get descriptor: %d\n", ret);

	return retdesc;
}

228
static int i2c_gpio_probe(struct platform_device *pdev)
229
{
230
	struct i2c_gpio_private_data *priv;
231 232 233
	struct i2c_gpio_platform_data *pdata;
	struct i2c_algo_bit_data *bit_data;
	struct i2c_adapter *adap;
L
Linus Walleij 已提交
234 235
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
236
	enum gpiod_flags gflags;
237 238
	int ret;

L
Linus Walleij 已提交
239
	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
240 241
	if (!priv)
		return -ENOMEM;
242

243 244 245 246
	adap = &priv->adap;
	bit_data = &priv->bit_data;
	pdata = &priv->pdata;

L
Linus Walleij 已提交
247 248
	if (np) {
		of_i2c_gpio_get_props(np, pdata);
249
	} else {
250 251 252 253
		/*
		 * If all platform data settings are zero it is OK
		 * to not provide any platform data from the board.
		 */
L
Linus Walleij 已提交
254 255
		if (dev_get_platdata(dev))
			memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
256
	}
257

258
	/*
259 260 261 262 263 264
	 * First get the GPIO pins; if it fails, we'll defer the probe.
	 * If the SDA line is marked from platform data or device tree as
	 * "open drain" it means something outside of our control is making
	 * this line being handled as open drain, and we should just handle
	 * it as any other output. Else we enforce open drain as this is
	 * required for an I2C bus.
265
	 */
266 267 268 269
	if (pdata->sda_is_open_drain)
		gflags = GPIOD_OUT_HIGH;
	else
		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
270 271 272 273
	priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
	if (IS_ERR(priv->sda))
		return PTR_ERR(priv->sda);

274 275 276 277 278 279 280 281
	/*
	 * If the SCL line is marked from platform data or device tree as
	 * "open drain" it means something outside of our control is making
	 * this line being handled as open drain, and we should just handle
	 * it as any other output. Else we enforce open drain as this is
	 * required for an I2C bus.
	 */
	if (pdata->scl_is_open_drain)
282
		gflags = GPIOD_OUT_HIGH;
283
	else
284
		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
285 286 287
	priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);
	if (IS_ERR(priv->scl))
		return PTR_ERR(priv->scl);
288

289 290 291
	if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
		dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");

292 293 294
	bit_data->setsda = i2c_gpio_setsda_val;
	bit_data->setscl = i2c_gpio_setscl_val;

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	if (!pdata->scl_is_output_only)
		bit_data->getscl = i2c_gpio_getscl;
	bit_data->getsda = i2c_gpio_getsda;

	if (pdata->udelay)
		bit_data->udelay = pdata->udelay;
	else if (pdata->scl_is_output_only)
		bit_data->udelay = 50;			/* 10 kHz */
	else
		bit_data->udelay = 5;			/* 100 kHz */

	if (pdata->timeout)
		bit_data->timeout = pdata->timeout;
	else
		bit_data->timeout = HZ / 10;		/* 100 ms */

311
	bit_data->data = priv;
312 313

	adap->owner = THIS_MODULE;
L
Linus Walleij 已提交
314 315
	if (np)
		strlcpy(adap->name, dev_name(dev), sizeof(adap->name));
316 317 318
	else
		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);

319
	adap->algo_data = bit_data;
320
	adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
L
Linus Walleij 已提交
321 322
	adap->dev.parent = dev;
	adap->dev.of_node = np;
323

324
	adap->nr = pdev->id;
325
	ret = i2c_bit_add_numbered_bus(adap);
326
	if (ret)
J
Jingoo Han 已提交
327
		return ret;
328

329
	platform_set_drvdata(pdev, priv);
330

331 332 333 334 335
	/*
	 * FIXME: using global GPIO numbers is not helpful. If/when we
	 * get accessors to get the actual name of the GPIO line,
	 * from the descriptor, then provide that instead.
	 */
L
Linus Walleij 已提交
336
	dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
337
		 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
338 339 340
		 pdata->scl_is_output_only
		 ? ", no clock stretching" : "");

W
Wolfram Sang 已提交
341 342
	i2c_gpio_fault_injector_init(pdev);

343 344 345
	return 0;
}

346
static int i2c_gpio_remove(struct platform_device *pdev)
347
{
348
	struct i2c_gpio_private_data *priv;
349 350
	struct i2c_adapter *adap;

W
Wolfram Sang 已提交
351 352
	i2c_gpio_fault_injector_exit(pdev);

353 354
	priv = platform_get_drvdata(pdev);
	adap = &priv->adap;
355 356 357 358 359 360

	i2c_del_adapter(adap);

	return 0;
}

361 362 363 364 365 366 367 368 369
#if defined(CONFIG_OF)
static const struct of_device_id i2c_gpio_dt_ids[] = {
	{ .compatible = "i2c-gpio", },
	{ /* sentinel */ }
};

MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
#endif

370 371 372
static struct platform_driver i2c_gpio_driver = {
	.driver		= {
		.name	= "i2c-gpio",
373
		.of_match_table	= of_match_ptr(i2c_gpio_dt_ids),
374
	},
375
	.probe		= i2c_gpio_probe,
376
	.remove		= i2c_gpio_remove,
377 378 379 380 381 382
};

static int __init i2c_gpio_init(void)
{
	int ret;

383
	ret = platform_driver_register(&i2c_gpio_driver);
384 385 386 387 388
	if (ret)
		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);

	return ret;
}
389
subsys_initcall(i2c_gpio_init);
390 391 392 393 394 395 396

static void __exit i2c_gpio_exit(void)
{
	platform_driver_unregister(&i2c_gpio_driver);
}
module_exit(i2c_gpio_exit);

J
Jean Delvare 已提交
397
MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
398 399
MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
MODULE_LICENSE("GPL");
400
MODULE_ALIAS("platform:i2c-gpio");