linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: minyard@acm.org
To: Wolfram Sang <wsa@the-dreams.de>, linux-i2c@vger.kernel.org
Cc: Corey Minyard <cminyard@mvista.com>
Subject: [PATCH 3/3] i2c-smbus: Allow parms to be passed in from sysfs new_device
Date: Tue, 19 Jan 2016 11:14:14 -0600	[thread overview]
Message-ID: <1453223654-20724-4-git-send-email-minyard@acm.org> (raw)
In-Reply-To: <1453223654-20724-1-git-send-email-minyard@acm.org>

From: Corey Minyard <cminyard@mvista.com>

This allows "irq=<num>" and "edge" to be passed in to the i2c new_device
after the name and i2c address.  This way the alert can be configured
without having to have platform data.

Signed-off-by: Corey Minyard <cminyard@mvista.com>
---
 drivers/i2c/i2c-smbus.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/drivers/i2c/i2c-smbus.c b/drivers/i2c/i2c-smbus.c
index cecd423..a3b7781 100644
--- a/drivers/i2c/i2c-smbus.c
+++ b/drivers/i2c/i2c-smbus.c
@@ -23,6 +23,7 @@
 #include <linux/i2c.h>
 #include <linux/i2c-smbus.h>
 #include <linux/slab.h>
+#include <linux/ctype.h>
 
 struct i2c_smbus_alert {
 	unsigned int		alert_edge_triggered:1;
@@ -123,15 +124,86 @@ static irqreturn_t smbalert_irq(int irq, void *d)
 	return IRQ_HANDLED;
 }
 
+static struct i2c_smbus_alert_setup *smbalert_parse_parms(struct device *dev,
+		const char *parms,
+		struct i2c_smbus_alert_setup *data)
+{
+	int rv;
+	char end;
+
+	if (!parms)
+		return NULL;
+
+	data->alert_edge_triggered = false;
+	while (*parms) {
+		const char *next = parms;
+		const char *val;
+		int parmlen;
+
+		while (*next && !isspace(*next) && *next != '=')
+			next++;
+
+		parmlen = next - parms;
+
+		if (*next == '=') {
+			next++;
+			val = next;
+			while (*next && !isspace(*next))
+				next++;
+		} else {
+			val = NULL;
+		}
+
+		if (strncmp(parms, "irq", parmlen) == 0) {
+			if (!val) {
+				dev_err(dev, "no irq parm value given\n");
+				return NULL;
+			}
+			rv = sscanf(val, "%d%c", &data->irq, &end);
+			if ((rv < 1) || ((rv > 1) && !isspace(end))) {
+				dev_err(dev, "Invalid irq parm: %s\n", val);
+				return NULL;
+			}
+		} else if (strncmp(parms, "edge", parmlen) == 0) {
+			if (val) {
+				dev_err(dev, "Value given with edge parm: %s\n",
+					val);
+				return NULL;
+			}
+			data->alert_edge_triggered = true;
+		} else {
+			dev_err(dev, "Invalid parameter: %s\n", parms);
+			return NULL;
+		}
+
+		while (*next && isspace(*next))
+			next++;
+		parms = next;
+	}
+
+	return data;
+}
+
 /* Setup SMBALERT# infrastructure */
 static int smbalert_probe(struct i2c_client *ara,
 			  const struct i2c_device_id *id)
 {
 	struct i2c_smbus_alert_setup *setup = dev_get_platdata(&ara->dev);
+	struct i2c_smbus_alert_setup dummy_setup;
 	struct i2c_smbus_alert *alert;
 	struct i2c_adapter *adapter = ara->adapter;
 	int res;
 
+	if (ara->parms)
+		/* Came in through sysfs. */
+		setup = smbalert_parse_parms(&ara->dev, ara->parms,
+					     &dummy_setup);
+
+	if (!setup) {
+		dev_err(&ara->dev, "SMBus alert without setup\n");
+		return -ENODEV;
+	}
+
 	alert = devm_kzalloc(&ara->dev, sizeof(struct i2c_smbus_alert),
 			     GFP_KERNEL);
 	if (!alert)
-- 
2.5.0

      parent reply	other threads:[~2016-01-19 17:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-19 17:14 [PATCH 0/3] Add parameters to sysfs added i2c devices minyard
2016-01-19 17:14 ` [PATCH 1/3] i2c: Add parameters to sysfs-added " minyard
2016-01-19 17:14 ` [PATCH 2/3] ipmi: Handle I2C parms in the SSIF driver minyard
2016-01-19 17:14 ` minyard [this message]

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=1453223654-20724-4-git-send-email-minyard@acm.org \
    --to=minyard@acm.org \
    --cc=cminyard@mvista.com \
    --cc=linux-i2c@vger.kernel.org \
    --cc=wsa@the-dreams.de \
    /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).