From: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
To: linux-arm-kernel@lists.infradead.org
Cc: devicetree-discuss@lists.ozlabs.org,
Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>,
linux-i2c@vger.kernel.org
Subject: [PATCH 1/4] i2c/gpio-i2c add: add DT support
Date: Sun, 5 Feb 2012 11:38:53 +0100 [thread overview]
Message-ID: <1328438337-21185-1-git-send-email-plagnioj@jcrosoft.com> (raw)
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Cc: linux-i2c@vger.kernel.org
Cc: devicetree-discuss@lists.ozlabs.org
---
.../devicetree/bindings/gpio/gpio_i2c.txt | 33 ++++++++++++
drivers/i2c/busses/i2c-gpio.c | 55 +++++++++++++++++++-
2 files changed, 86 insertions(+), 2 deletions(-)
create mode 100644 Documentation/devicetree/bindings/gpio/gpio_i2c.txt
diff --git a/Documentation/devicetree/bindings/gpio/gpio_i2c.txt b/Documentation/devicetree/bindings/gpio/gpio_i2c.txt
new file mode 100644
index 0000000..15f288da
--- /dev/null
+++ b/Documentation/devicetree/bindings/gpio/gpio_i2c.txt
@@ -0,0 +1,33 @@
+Device-Tree bindings for i2c gpio driver
+
+Required properties:
+ - compatible = "gpio-i2c";
+ - gpios: sda and scl gpio
+
+
+Optional properties:
+ - gpio-i2c,sda_is_open_drain: sda as open drain
+ - gpio-i2c,scl_is_open_drain: scl as open drain
+ - gpio-i2c,scl_is_output_only: scl as output only
+ - udelay: half clock cycle time in us (may depend on each platform)
+ - timeout: timeout to get data
+
+
+Example nodes:
+
+i2c-gpio@0 {
+ compatible = "gpio-i2c";
+ gpios = <&pioA 23 0 /* sda */
+ &pioA 24 0 /* scl */
+ >;
+ gpio-i2c,sda_is_open_drain;
+ gpio-i2c,scl_is_open_drain;
+ udelay = <2>; /* ~100 kHz */
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ rv3029c2@56 {
+ compatible = "rv3029c2";
+ reg = <0x56>;
+ };
+};
diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c
index a651779..6b5d794 100644
--- a/drivers/i2c/busses/i2c-gpio.c
+++ b/drivers/i2c/busses/i2c-gpio.c
@@ -14,6 +14,8 @@
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
+#include <linux/of_gpio.h>
+#include <linux/of_i2c.h>
#include <asm/gpio.h>
@@ -78,16 +80,51 @@ static int i2c_gpio_getscl(void *data)
return gpio_get_value(pdata->scl_pin);
}
+static int of_i2c_gpio_probe(struct device_node *np,
+ struct i2c_gpio_platform_data *pdata)
+{
+ u32 reg;
+
+ pdata->sda_pin = of_get_gpio(np, 0);
+ pdata->scl_pin = of_get_gpio(np, 1);
+
+ if (of_property_read_u32(np, "udelay", ®))
+ pdata->udelay = reg;
+
+ if (of_property_read_u32(np, "timeout", ®))
+ pdata->timeout = reg;
+
+ pdata->sda_is_open_drain =
+ !!of_get_property(np, "gpio-i2c,sda_is_open_drain", NULL);
+ pdata->scl_is_open_drain =
+ !!of_get_property(np, "gpio-i2c,scl_is_open_drain", NULL);
+ pdata->scl_is_output_only =
+ !!of_get_property(np, "gpio-i2c,scl_is_output_only", NULL);
+
+ return 0;
+}
+
static int __devinit i2c_gpio_probe(struct platform_device *pdev)
{
struct i2c_gpio_platform_data *pdata;
struct i2c_algo_bit_data *bit_data;
struct i2c_adapter *adap;
int ret;
+ int len = sizeof(struct i2c_gpio_platform_data);
- pdata = pdev->dev.platform_data;
+ pdata = kzalloc(len, GFP_KERNEL);
if (!pdata)
- return -ENXIO;
+ return -ENOMEM;
+
+ if (pdev->dev.of_node) {
+ of_i2c_gpio_probe(pdev->dev.of_node, pdata);
+ } else {
+ if (!pdev->dev.platform_data) {
+ ret = -ENXIO;
+ goto err_alloc_adap;
+ }
+ memcpy(pdata, pdev->dev.platform_data, len);
+ }
ret = -ENOMEM;
adap = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
@@ -143,6 +180,7 @@ static int __devinit i2c_gpio_probe(struct platform_device *pdev)
adap->algo_data = bit_data;
adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
adap->dev.parent = &pdev->dev;
+ adap->dev.of_node = pdev->dev.of_node;
/*
* If "dev->id" is negative we consider it as zero.
@@ -154,6 +192,8 @@ static int __devinit i2c_gpio_probe(struct platform_device *pdev)
if (ret)
goto err_add_bus;
+ of_i2c_register_devices(adap);
+
platform_set_drvdata(pdev, adap);
dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n",
@@ -172,6 +212,7 @@ err_request_sda:
err_alloc_bit_data:
kfree(adap);
err_alloc_adap:
+ kfree(pdata);
return ret;
}
@@ -192,10 +233,20 @@ static int __devexit i2c_gpio_remove(struct platform_device *pdev)
return 0;
}
+#if defined(CONFIG_OF)
+static const struct of_device_id gpio_i2c_dt_ids[] = {
+ { .compatible = "gpio-i2c", },
+ { /* sentinel */ }
+};
+
+MODULE_DEVICE_TABLE(of, gpio_i2c_dt_ids);
+#endif
+
static struct platform_driver i2c_gpio_driver = {
.driver = {
.name = "i2c-gpio",
.owner = THIS_MODULE,
+ .of_match_table = of_match_ptr(gpio_i2c_dt_ids),
},
.probe = i2c_gpio_probe,
.remove = __devexit_p(i2c_gpio_remove),
--
1.7.7
next reply other threads:[~2012-02-05 10:38 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-05 10:38 Jean-Christophe PLAGNIOL-VILLARD [this message]
[not found] ` <1328438337-21185-1-git-send-email-plagnioj-sclMFOaUSTBWk0Htik3J/w@public.gmane.org>
2012-02-06 16:09 ` [PATCH 1/4] i2c/gpio-i2c add: add DT support Mark Brown
[not found] ` <20120206160907.GG10173-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
2012-02-07 2:56 ` Jean-Christophe PLAGNIOL-VILLARD
[not found] ` <20120207025624.GB15647-RQcB7r2h9QmfDR2tN2SG5Ni2O/JbrIOy@public.gmane.org>
2012-02-07 11:25 ` Mark Brown
2012-02-06 18:38 ` Karol Lewandowski
[not found] ` <4F301E25.5060507-Sze3O3UU22JBDgjK7y7TUQ@public.gmane.org>
2012-02-06 19:15 ` Jean Delvare
2012-02-07 3:25 ` Jean-Christophe PLAGNIOL-VILLARD
[not found] ` <20120207032533.GC15647-RQcB7r2h9QmfDR2tN2SG5Ni2O/JbrIOy@public.gmane.org>
2012-02-07 15:35 ` Karol Lewandowski
2012-02-13 23:14 ` Ben Dooks
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1328438337-21185-1-git-send-email-plagnioj@jcrosoft.com \
--to=plagnioj@jcrosoft.com \
--cc=devicetree-discuss@lists.ozlabs.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-i2c@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).