i2c-gpio.c 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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.
 */
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>
#include <linux/i2c-gpio.h>
#include <linux/init.h>
#include <linux/module.h>
15
#include <linux/slab.h>
16
#include <linux/platform_device.h>
17 18 19 20 21 22 23 24
#include <linux/gpio.h>
#include <linux/of_gpio.h>

struct i2c_gpio_private_data {
	struct i2c_adapter adap;
	struct i2c_algo_bit_data bit_data;
	struct i2c_gpio_platform_data pdata;
};
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

/* Toggle SDA by changing the direction of the pin */
static void i2c_gpio_setsda_dir(void *data, int state)
{
	struct i2c_gpio_platform_data *pdata = data;

	if (state)
		gpio_direction_input(pdata->sda_pin);
	else
		gpio_direction_output(pdata->sda_pin, 0);
}

/*
 * 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)
{
	struct i2c_gpio_platform_data *pdata = data;

	gpio_set_value(pdata->sda_pin, state);
}

/* Toggle SCL by changing the direction of the pin. */
static void i2c_gpio_setscl_dir(void *data, int state)
{
	struct i2c_gpio_platform_data *pdata = data;

	if (state)
		gpio_direction_input(pdata->scl_pin);
	else
		gpio_direction_output(pdata->scl_pin, 0);
}

/*
 * 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)
{
	struct i2c_gpio_platform_data *pdata = data;

	gpio_set_value(pdata->scl_pin, state);
}

73
static int i2c_gpio_getsda(void *data)
74 75 76 77 78 79
{
	struct i2c_gpio_platform_data *pdata = data;

	return gpio_get_value(pdata->sda_pin);
}

80
static int i2c_gpio_getscl(void *data)
81 82 83 84 85 86
{
	struct i2c_gpio_platform_data *pdata = data;

	return gpio_get_value(pdata->scl_pin);
}

87 88
static int of_i2c_gpio_get_pins(struct device_node *np,
				unsigned int *sda_pin, unsigned int *scl_pin)
89 90 91 92
{
	if (of_gpio_count(np) < 2)
		return -ENODEV;

93 94
	*sda_pin = of_get_gpio(np, 0);
	*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 100 101
		return -ENODEV;
	}

102 103 104 105 106 107 108 109
	return 0;
}

static void of_i2c_gpio_get_props(struct device_node *np,
				  struct i2c_gpio_platform_data *pdata)
{
	u32 reg;

110 111 112 113 114 115 116 117 118 119 120 121 122
	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");
}

123
static int i2c_gpio_probe(struct platform_device *pdev)
124
{
125
	struct i2c_gpio_private_data *priv;
126 127 128
	struct i2c_gpio_platform_data *pdata;
	struct i2c_algo_bit_data *bit_data;
	struct i2c_adapter *adap;
129
	unsigned int sda_pin, scl_pin;
130 131
	int ret;

132
	/* First get the GPIO pins; if it fails, we'll defer the probe. */
133
	if (pdev->dev.of_node) {
134 135
		ret = of_i2c_gpio_get_pins(pdev->dev.of_node,
					   &sda_pin, &scl_pin);
136 137 138
		if (ret)
			return ret;
	} else {
J
Jingoo Han 已提交
139
		if (!dev_get_platdata(&pdev->dev))
140
			return -ENXIO;
J
Jingoo Han 已提交
141
		pdata = dev_get_platdata(&pdev->dev);
142 143
		sda_pin = pdata->sda_pin;
		scl_pin = pdata->scl_pin;
144
	}
145

146 147 148 149
	ret = gpio_request(sda_pin, "sda");
	if (ret) {
		if (ret == -EINVAL)
			ret = -EPROBE_DEFER;	/* Try again later */
150
		goto err_request_sda;
151 152 153 154 155
	}
	ret = gpio_request(scl_pin, "scl");
	if (ret) {
		if (ret == -EINVAL)
			ret = -EPROBE_DEFER;	/* Try again later */
156
		goto err_request_scl;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	}

	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv) {
		ret = -ENOMEM;
		goto err_add_bus;
	}
	adap = &priv->adap;
	bit_data = &priv->bit_data;
	pdata = &priv->pdata;

	if (pdev->dev.of_node) {
		pdata->sda_pin = sda_pin;
		pdata->scl_pin = scl_pin;
		of_i2c_gpio_get_props(pdev->dev.of_node, pdata);
	} else {
J
Jingoo Han 已提交
173
		memcpy(pdata, dev_get_platdata(&pdev->dev), sizeof(*pdata));
174
	}
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

	if (pdata->sda_is_open_drain) {
		gpio_direction_output(pdata->sda_pin, 1);
		bit_data->setsda = i2c_gpio_setsda_val;
	} else {
		gpio_direction_input(pdata->sda_pin);
		bit_data->setsda = i2c_gpio_setsda_dir;
	}

	if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
		gpio_direction_output(pdata->scl_pin, 1);
		bit_data->setscl = i2c_gpio_setscl_val;
	} else {
		gpio_direction_input(pdata->scl_pin);
		bit_data->setscl = i2c_gpio_setscl_dir;
	}

	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 */

	bit_data->data = pdata;

	adap->owner = THIS_MODULE;
211 212 213 214 215
	if (pdev->dev.of_node)
		strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
	else
		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);

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 224 225
	if (ret)
		goto err_add_bus;

226
	platform_set_drvdata(pdev, priv);
227 228 229 230 231 232 233 234 235

	dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
		 pdata->sda_pin, pdata->scl_pin,
		 pdata->scl_is_output_only
		 ? ", no clock stretching" : "");

	return 0;

err_add_bus:
236
	gpio_free(scl_pin);
237
err_request_scl:
238
	gpio_free(sda_pin);
239 240 241 242
err_request_sda:
	return ret;
}

243
static int i2c_gpio_remove(struct platform_device *pdev)
244
{
245
	struct i2c_gpio_private_data *priv;
246 247 248
	struct i2c_gpio_platform_data *pdata;
	struct i2c_adapter *adap;

249 250 251
	priv = platform_get_drvdata(pdev);
	adap = &priv->adap;
	pdata = &priv->pdata;
252 253 254 255 256 257 258 259

	i2c_del_adapter(adap);
	gpio_free(pdata->scl_pin);
	gpio_free(pdata->sda_pin);

	return 0;
}

260 261 262 263 264 265 266 267 268
#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

269 270 271 272
static struct platform_driver i2c_gpio_driver = {
	.driver		= {
		.name	= "i2c-gpio",
		.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 280 281 282
};

static int __init i2c_gpio_init(void)
{
	int ret;

283
	ret = platform_driver_register(&i2c_gpio_driver);
284 285 286 287 288
	if (ret)
		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);

	return ret;
}
289
subsys_initcall(i2c_gpio_init);
290 291 292 293 294 295 296

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

J
Jean Delvare 已提交
297
MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
298 299
MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
MODULE_LICENSE("GPL");
300
MODULE_ALIAS("platform:i2c-gpio");