linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver
@ 2014-10-16 20:26 Marek Belisko
  2014-10-16 20:26 ` [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps Marek Belisko
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Marek Belisko @ 2014-10-16 20:26 UTC (permalink / raw)
  To: arnd, gregkh
  Cc: robh+dt, pawel.moll, mark.rutland, ijc+devicetree, galak,
	grant.likely, devicetree, linux-kernel, Marek Belisko, NeilBrown,
	H. Nikolaus Schaller

This is a driver for the Wi2Wi GPS modules connected through an UART.
The tricky part is that the module is turned on or off by an impulse
on the control line - but it is only possible to get the real state by monitoring
the UART RX input. Since the kernel can't know in which status the module
is brought e.g. by a boot loader or after suspend, we need some logic to handle
this.

The driver allows two different methods to enable (and power up) GPS:
a) through rfkill
b) as a GPIO

The GPIO enable can be plumbed to other drivers that expect to be able to control
a GPIO. On the GTA04 board this is the DTR-gpio of the connected UART so that
opening the UART device enables the receiver and closing it shuts the receiver down.

Original implementation by Neil Brown, fixes + DT bindings by H. Nikolaus Schaller

Signed-off-by: NeilBrown <neilb@suse.de>
Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
Signed-off-by: Marek Belisko <marek@goldelico.com>
---
 drivers/misc/Kconfig     |  10 +
 drivers/misc/Makefile    |   1 +
 drivers/misc/w2sg0004.c  | 512 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/w2sg0004.h |  23 +++
 4 files changed, 546 insertions(+)
 create mode 100644 drivers/misc/w2sg0004.c
 create mode 100644 include/linux/w2sg0004.h

diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index bbeb451..03af633 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -515,6 +515,16 @@ config VEXPRESS_SYSCFG
 	  bus. System Configuration interface is one of the possible means
 	  of generating transactions on this bus.
 
+config W2SG0004
+	tristate "W2SG0004 on/off control"
+	depends on GPIOLIB
+	help
+	  Enable on/off control of W2SG0004 GPS using a virtual GPIO.
+	  The virtual GPIO can be connected to a DTR line of a serial
+	  interface to allow powering up if the /dev/tty$n is opened.
+	  It also provides a rfkill gps node to control the LNA power.
+	  NOTE: can't currently be compiled as module, so please choose Y.
+
 source "drivers/misc/c2port/Kconfig"
 source "drivers/misc/eeprom/Kconfig"
 source "drivers/misc/cb710/Kconfig"
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index 7d5c4cd..2f83270 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -56,3 +56,4 @@ obj-$(CONFIG_GENWQE)		+= genwqe/
 obj-$(CONFIG_ECHO)		+= echo/
 obj-$(CONFIG_VEXPRESS_SYSCFG)	+= vexpress-syscfg.o
 obj-$(CONFIG_CXL_BASE)		+= cxl/
+obj-${CONFIG_W2SG0004}		+= w2sg0004.o
diff --git a/drivers/misc/w2sg0004.c b/drivers/misc/w2sg0004.c
new file mode 100644
index 0000000..8f0afcf
--- /dev/null
+++ b/drivers/misc/w2sg0004.c
@@ -0,0 +1,512 @@
+/*
+ * w2sg0004.c
+ * Virtual GPIO of controlling the w2sg0004 GPS receiver.
+ *
+ * Copyright (C) 2011 Neil Brown <neil@brown.name>
+ *
+ * This receiver has an ON/OFF pin which must be toggled to
+ * turn the device 'on' or 'off'.  A high->low->high toggle
+ * will switch the device on if it is off, and off if it is on.
+ * It is not possible to directly detect the state of the device.
+ * However when it is on it will send characters on a UART line
+ * regularly.
+ * On the OMAP3, the UART line can also be programmed as a GPIO
+ * on which we can receive interrupts.
+ * So when we want the device to be 'off' we can reprogram
+ * the line, toggle the ON/OFF pin and hope that it is off.
+ * However if an interrupt arrives we know that it is really on
+ * and can toggle again.
+ *
+ * To enable receiving on/off requests we create a gpio_chip
+ * with a single 'output' GPIO.  When it is low, the
+ * GPS is turned off.  When it is high, it is turned on.
+ * This can be configured as the DTR GPIO on the UART which
+ * connects the GPS.  Then whenever the tty is open, the GPS
+ * will be switched on, and whenever it is closed, the GPS will
+ * be switched off.
+ *
+ * In addition we register as a rfkill client so that we can
+ * control the LNA power.
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/interrupt.h>
+#include <linux/sched.h>
+#include <linux/irq.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/delay.h>
+#include <linux/of.h>
+#include <linux/of_irq.h>
+#include <linux/gpio.h>
+#include <linux/of_gpio.h>
+#include <linux/platform_device.h>
+#include <linux/w2sg0004.h>
+#include <linux/workqueue.h>
+#include <linux/rfkill.h>
+#include <linux/pinctrl/consumer.h>
+
+/*
+ * There seems to restrictions on how quickly we can toggle the
+ * on/off line.  data sheets says "two rtc ticks", whatever that means.
+ * If we do it too soon it doesn't work.
+ * So we have a state machine which uses the common work queue to ensure
+ * clean transitions.
+ * When a change is requested we record that request and only act on it
+ * once the previous change has completed.
+ * A change involves a 10ms low pulse, and a 990ms raised level, so only
+ * one change per second.
+ */
+
+enum w2sg_state {
+	W2SG_IDLE,	/* is not changing state */
+	W2SG_PULSE,	/* activate on/off impulse */
+	W2SG_NOPULSE	/* desctivate on/off impulse */
+};
+
+struct gpio_w2sg {
+	struct		rfkill *rf_kill;
+	struct		regulator *lna_regulator;
+	int		lna_blocked;	/* rfkill block gps active */
+	int		lna_is_off;	/* LNA is currently off */
+	int		is_on;		/* current state (0/1) */
+	unsigned long	last_toggle;
+	unsigned long	backoff;	/* time to wait since last_toggle */
+	int		on_off_gpio;
+	int		rx_irq;
+
+	struct pinctrl *p;
+	struct pinctrl_state *default_state;	/* should be UART mode */
+	struct pinctrl_state *monitor_state;	/* monitor RX as GPIO */
+	enum w2sg_state	state;
+	int		requested;	/* requested state (0/1) */
+	int		suspended;
+	int		rx_redirected;
+	spinlock_t	lock;
+#ifdef CONFIG_GPIOLIB
+	struct gpio_chip gpio;
+	const char	*gpio_name[1];
+#endif
+	struct delayed_work work;
+};
+
+static int gpio_w2sg_set_lna_power(struct gpio_w2sg *gw2sg)
+{
+	int ret = 0;
+	int off = gw2sg->suspended || !gw2sg->requested || gw2sg->lna_blocked;
+
+	pr_debug("gpio_w2sg_set_lna_power: %s\n", off ? "off" : "on");
+
+	if (off != gw2sg->lna_is_off) {
+		gw2sg->lna_is_off = off;
+		if (!IS_ERR_OR_NULL(gw2sg->lna_regulator)) {
+			if (off)
+				regulator_disable(gw2sg->lna_regulator);
+			else
+				ret = regulator_enable(gw2sg->lna_regulator);
+		}
+	}
+
+	return ret;
+}
+
+static void toggle_work(struct work_struct *work)
+{
+	struct gpio_w2sg *gw2sg = container_of(work, struct gpio_w2sg,
+						work.work);
+
+	gpio_w2sg_set_lna_power(gw2sg);	/* update LNA power state */
+
+	switch (gw2sg->state) {
+	case W2SG_NOPULSE:
+		gw2sg->state = W2SG_IDLE;
+		pr_debug("GPS idle\n");
+		break;
+
+	case W2SG_IDLE:
+		spin_lock_irq(&gw2sg->lock);
+		if (gw2sg->requested == gw2sg->is_on) {
+			if (!gw2sg->is_on && !gw2sg->rx_redirected) {
+				/* not yet redirected in off state */
+				gw2sg->rx_redirected = 1;
+				pinctrl_select_state(gw2sg->p,
+							gw2sg->monitor_state);
+				enable_irq(gw2sg->rx_irq);
+			}
+			spin_unlock_irq(&gw2sg->lock);
+			return;
+		}
+		spin_unlock_irq(&gw2sg->lock);
+		gpio_set_value_cansleep(gw2sg->on_off_gpio, 0);
+		gw2sg->state = W2SG_PULSE;
+
+		pr_debug("GPS pulse\n");
+
+		schedule_delayed_work(&gw2sg->work,
+				      msecs_to_jiffies(10));
+		break;
+
+	case W2SG_PULSE:
+		gpio_set_value_cansleep(gw2sg->on_off_gpio, 1);
+		gw2sg->last_toggle = jiffies;
+		gw2sg->state = W2SG_NOPULSE;
+
+		pr_debug("GPS nopulse\n");
+
+		gw2sg->is_on = !gw2sg->is_on;
+		schedule_delayed_work(&gw2sg->work,
+				      msecs_to_jiffies(10));
+		break;
+	}
+}
+
+static irqreturn_t gpio_w2sg_isr(int irq, void *dev_id)
+{
+	struct gpio_w2sg *gw2sg = dev_id;
+	unsigned long flags;
+
+	/* we have received a RX signal while GPS should be off */
+	pr_debug("!");
+
+	if (!gw2sg->requested && !gw2sg->is_on &&
+	    (gw2sg->state == W2SG_IDLE) &&
+	    time_after(jiffies,
+		       gw2sg->last_toggle + gw2sg->backoff)) {
+		/* Should be off by now, time to toggle again */
+		gw2sg->is_on = 1;
+		gw2sg->backoff *= 2;
+		spin_lock_irqsave(&gw2sg->lock, flags);
+		if (!gw2sg->suspended)
+			schedule_delayed_work(&gw2sg->work, 0);
+		spin_unlock_irqrestore(&gw2sg->lock, flags);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static void gpio_w2sg_set_power(struct gpio_w2sg *gw2sg, int val)
+{
+	unsigned long flags;
+
+	pr_debug("GPS SET to %d\n", val);
+
+	spin_lock_irqsave(&gw2sg->lock, flags);
+
+	if (val && !gw2sg->requested) {
+		if (gw2sg->rx_redirected) {
+			gw2sg->rx_redirected = 0;
+			disable_irq(gw2sg->rx_irq);
+			pinctrl_select_state(gw2sg->p, gw2sg->default_state);
+		}
+		gw2sg->requested = 1;
+	} else if (!val && gw2sg->requested) {
+		gw2sg->backoff = HZ;
+		gw2sg->requested = 0;
+	} else
+		goto unlock;
+
+	if (!gw2sg->suspended)
+		schedule_delayed_work(&gw2sg->work, 0);
+unlock:
+	spin_unlock_irqrestore(&gw2sg->lock, flags);
+}
+
+static int gpio_w2sg_get_value(struct gpio_chip *gc, unsigned offset)
+{
+	struct gpio_w2sg *gw2sg = container_of(gc, struct gpio_w2sg, gpio);
+
+	return gw2sg->is_on;
+}
+
+static void gpio_w2sg_set_value(struct gpio_chip *gc, unsigned offset, int val)
+{
+	struct gpio_w2sg *gw2sg = container_of(gc, struct gpio_w2sg, gpio);
+
+	gpio_w2sg_set_power(gw2sg, val);
+}
+
+static int gpio_w2sg_direction_output(struct gpio_chip *gc,
+					unsigned offset, int val)
+{
+	gpio_w2sg_set_value(gc, offset, val);
+
+	return 0;
+}
+
+static int gpio_w2sg_rfkill_set_block(void *data, bool blocked)
+{
+	struct gpio_w2sg *gw2sg = data;
+
+	pr_debug("%s: blocked: %d\n", __func__, blocked);
+
+	gw2sg->lna_blocked = blocked;
+
+	return gpio_w2sg_set_lna_power(gw2sg);
+}
+
+static struct rfkill_ops gpio_w2sg0004_rfkill_ops = {
+	.set_block = gpio_w2sg_rfkill_set_block,
+};
+
+static int gpio_w2sg_probe(struct platform_device *pdev)
+{
+	struct gpio_w2sg_data *pdata = dev_get_platdata(&pdev->dev);
+	struct gpio_w2sg *gw2sg;
+	struct rfkill *rf_kill;
+	int err;
+
+	pr_debug("gpio_w2sg_probe()\n");
+
+#ifdef CONFIG_OF
+	if (pdev->dev.of_node) {
+		struct device *dev = &pdev->dev;
+		enum of_gpio_flags flags;
+
+		pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
+		if (!pdata)
+			return -ENOMEM;
+
+		pdata->on_off_gpio = of_get_named_gpio_flags(dev->of_node,
+						"on-off-gpio", 0, &flags);
+
+		pdata->rx_irq = irq_of_parse_and_map(dev->of_node, 0);
+		if (!pdata->rx_irq) {
+			dev_err(dev, "no IRQ found\n");
+			return -ENODEV;
+		}
+
+		if (pdata->on_off_gpio == -EPROBE_DEFER)
+			return -EPROBE_DEFER;
+
+		pdata->lna_regulator = devm_regulator_get_optional(dev, "lna");
+
+		pr_debug("lna_regulator = %p\n", pdata->lna_regulator);
+
+		if (IS_ERR(pdata->lna_regulator))
+			return PTR_ERR(pdata->lna_regulator);
+
+		pdata->gpio_base = -1;
+		pdev->dev.platform_data = pdata;
+	}
+#endif
+
+	gw2sg = devm_kzalloc(&pdev->dev, sizeof(*gw2sg), GFP_KERNEL);
+	if (gw2sg == NULL)
+		return -ENOMEM;
+
+	gw2sg->lna_regulator = pdata->lna_regulator;
+	gw2sg->lna_blocked = true;
+	gw2sg->lna_is_off = true;
+
+	gw2sg->on_off_gpio = pdata->on_off_gpio;
+
+	gw2sg->is_on = false;
+	gw2sg->requested = true;
+	gw2sg->state = W2SG_IDLE;
+	gw2sg->last_toggle = jiffies;
+	gw2sg->backoff = HZ;
+
+	/* label of controlling GPIO */
+	gw2sg->gpio_name[0] = "gpio-w2sg0004-enable";
+	gw2sg->gpio.label = "w2sg0004";
+	gw2sg->gpio.names = gw2sg->gpio_name;
+	gw2sg->gpio.ngpio = 1;
+	gw2sg->gpio.base = pdata->gpio_base;
+	gw2sg->gpio.owner = THIS_MODULE;
+	gw2sg->gpio.direction_output = gpio_w2sg_direction_output;
+	gw2sg->gpio.get = gpio_w2sg_get_value;
+	gw2sg->gpio.set = gpio_w2sg_set_value;
+	gw2sg->gpio.can_sleep = 0;
+	gw2sg->rx_irq = pdata->rx_irq;
+
+#ifdef CONFIG_OF_GPIO
+	gw2sg->gpio.of_node = pdev->dev.of_node;
+#endif
+
+#ifdef CONFIG_OF
+	if (pdev->dev.of_node) {
+		gw2sg->p = devm_pinctrl_get(&pdev->dev);
+		if (IS_ERR(gw2sg->p)) {
+			err = PTR_ERR(gw2sg->p);
+			dev_err(&pdev->dev, "Cannot get pinctrl: %d\n", err);
+			goto out;
+		}
+
+		gw2sg->default_state = pinctrl_lookup_state(gw2sg->p,
+							PINCTRL_STATE_DEFAULT);
+		if (IS_ERR(gw2sg->default_state)) {
+			err = PTR_ERR(gw2sg->default_state);
+			dev_err(&pdev->dev, "Cannot look up pinctrl state %s: %d\n",
+				PINCTRL_STATE_DEFAULT, err);
+			goto out;
+		}
+
+		gw2sg->monitor_state = pinctrl_lookup_state(gw2sg->p,
+								"monitor");
+		if (IS_ERR(gw2sg->monitor_state)) {
+			err = PTR_ERR(gw2sg->monitor_state);
+			dev_err(&pdev->dev,
+				"Cannot look up pinctrl state %s: %d\n",
+				"monitor", err);
+			goto out;
+		}
+		/* choose UART state as default */
+		err = pinctrl_select_state(gw2sg->p, gw2sg->default_state);
+		if (err < 0)
+			goto out;
+	}
+#endif
+
+	INIT_DELAYED_WORK(&gw2sg->work, toggle_work);
+	spin_lock_init(&gw2sg->lock);
+
+	err = devm_gpio_request(&pdev->dev, gw2sg->on_off_gpio,
+				"w2sg0004-on-off");
+	if (err < 0)
+		goto out;
+
+	gpio_direction_output(gw2sg->on_off_gpio, false);
+
+	err = devm_request_threaded_irq(&pdev->dev, gw2sg->rx_irq, NULL,
+				   gpio_w2sg_isr,
+				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+				   "w2sg0004-rx",
+				   gw2sg);
+	if (err < 0) {
+		dev_err(&pdev->dev, "Unable to claim irq %d; error %d\n",
+			gw2sg->rx_irq, err);
+		goto out;
+	}
+
+	disable_irq(gw2sg->rx_irq);
+
+	err = gpiochip_add(&gw2sg->gpio);
+	if (err)
+		goto out;
+
+	rf_kill = rfkill_alloc("GPS", &pdev->dev, RFKILL_TYPE_GPS,
+				&gpio_w2sg0004_rfkill_ops, gw2sg);
+	if (rf_kill == NULL) {
+		err = -ENOMEM;
+		goto err_rfkill;
+	}
+
+	err = rfkill_register(rf_kill);
+	if (err) {
+		dev_err(&pdev->dev, "Cannot register rfkill device\n");
+		goto err_rfkill;
+	}
+
+	gw2sg->rf_kill = rf_kill;
+
+	platform_set_drvdata(pdev, gw2sg);
+
+	pr_debug("w2sg0004 probed\n");
+
+	return 0;
+
+err_rfkill:
+	rfkill_destroy(rf_kill);
+	gpiochip_remove(&gw2sg->gpio);
+out:
+	return err;
+}
+
+static int gpio_w2sg_remove(struct platform_device *pdev)
+{
+	struct gpio_w2sg *gw2sg = platform_get_drvdata(pdev);
+
+	cancel_delayed_work_sync(&gw2sg->work);
+	gpiochip_remove(&gw2sg->gpio);
+
+	return 0;
+}
+
+static int gpio_w2sg_suspend(struct device *dev)
+{
+	/* Ignore the GPIO and just turn device off.
+	 * we cannot really wait for a separate thread to
+	 * do things, so we disable that and do it all
+	 * here
+	 */
+	struct gpio_w2sg *gw2sg = dev_get_drvdata(dev);
+
+	spin_lock_irq(&gw2sg->lock);
+	gw2sg->suspended = 1;
+	spin_unlock_irq(&gw2sg->lock);
+
+	cancel_delayed_work_sync(&gw2sg->work);
+
+	gpio_w2sg_set_lna_power(gw2sg);	/* shut down if needed */
+
+	if (gw2sg->state == W2SG_PULSE) {
+		usleep_range(10000, 15000);
+		gpio_set_value_cansleep(gw2sg->on_off_gpio, 1);
+		gw2sg->last_toggle = jiffies;
+		gw2sg->is_on = !gw2sg->is_on;
+		gw2sg->state = W2SG_NOPULSE;
+	}
+
+	if (gw2sg->state == W2SG_NOPULSE) {
+		usleep_range(10000, 15000);
+		gw2sg->state = W2SG_IDLE;
+	}
+
+	if (gw2sg->is_on) {
+		pr_info("GPS off for suspend %d %d %d\n",
+			gw2sg->requested, gw2sg->is_on, gw2sg->lna_is_off);
+
+		gpio_set_value_cansleep(gw2sg->on_off_gpio, 0);
+		usleep_range(10000, 15000);
+		gpio_set_value_cansleep(gw2sg->on_off_gpio, 1);
+		gw2sg->is_on = 0;
+	}
+
+	return 0;
+}
+
+static int gpio_w2sg_resume(struct device *dev)
+{
+	struct gpio_w2sg *gw2sg = dev_get_drvdata(dev);
+
+	spin_lock_irq(&gw2sg->lock);
+	gw2sg->suspended = 0;
+	spin_unlock_irq(&gw2sg->lock);
+
+	pr_info("GPS resuming %d %d %d\n",
+		gw2sg->requested, gw2sg->is_on, gw2sg->lna_is_off);
+
+	schedule_delayed_work(&gw2sg->work, 0);	/* enables LNA if needed */
+
+	return 0;
+}
+
+#if defined(CONFIG_OF)
+static const struct of_device_id w2sg0004_of_match[] = {
+	{ .compatible = "wi2wi,w2sg0004" },
+	{},
+};
+MODULE_DEVICE_TABLE(of, w2sg0004_of_match);
+#endif
+
+SIMPLE_DEV_PM_OPS(w2sg_pm_ops, gpio_w2sg_suspend, gpio_w2sg_resume);
+
+static struct platform_driver gpio_w2sg_driver = {
+	.probe		= gpio_w2sg_probe,
+	.remove		= gpio_w2sg_remove,
+	.driver = {
+		.name	= "w2sg0004",
+		.owner	= THIS_MODULE,
+		.pm	= &w2sg_pm_ops,
+		.of_match_table = of_match_ptr(w2sg0004_of_match)
+	},
+};
+
+module_platform_driver(gpio_w2sg_driver);
+
+MODULE_ALIAS("w2sg0004");
+
+MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
+MODULE_DESCRIPTION("w2sg0004 GPS virtual GPIO driver");
+MODULE_LICENSE("GPL v2");
diff --git a/include/linux/w2sg0004.h b/include/linux/w2sg0004.h
new file mode 100644
index 0000000..f20e90c
--- /dev/null
+++ b/include/linux/w2sg0004.h
@@ -0,0 +1,23 @@
+/*
+ * Virtual gpio to allow ON/OFF control of w2sg0004 GPS receiver.
+ *
+ * Copyright (C) 2011 Neil Brown <neil@brown.name>
+ *
+ */
+
+
+#ifndef __LINUX_W2SG0004_H
+#define __LINUX_W2SG0004_H
+
+#include <linux/regulator/consumer.h>
+
+struct gpio_w2sg_data {
+	int	gpio_base;	/* (not used by DT) - defines the gpio.base */
+	struct regulator *lna_regulator;	/* enable LNA power */
+	int	on_off_gpio;	/* connected to the on-off input of the GPS module */
+	int	rx_irq;		/* the rx irq - we track to check for module activity */
+	unsigned short	on_state;  /* Mux state when GPS is on */
+	unsigned short	off_state; /* Mux state when GPS is off */
+};
+
+#endif /* __LINUX_W2SG0004_H */
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps
  2014-10-16 20:26 [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver Marek Belisko
@ 2014-10-16 20:26 ` Marek Belisko
  2014-10-17  9:37   ` Mark Rutland
  2014-10-19 19:51 ` [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver Arnd Bergmann
  2014-10-21 10:49 ` Pavel Machek
  2 siblings, 1 reply; 15+ messages in thread
From: Marek Belisko @ 2014-10-16 20:26 UTC (permalink / raw)
  To: arnd, gregkh
  Cc: robh+dt, pawel.moll, mark.rutland, ijc+devicetree, galak,
	grant.likely, devicetree, linux-kernel, Marek Belisko,
	H. Nikolaus Schaller

Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
Signed-off-by: Marek Belisko <marek@goldelico.com>
---
 .../devicetree/bindings/misc/wi2wi,w2sg0004.txt    | 44 ++++++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt

diff --git a/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
new file mode 100644
index 0000000..e144441
--- /dev/null
+++ b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
@@ -0,0 +1,44 @@
+Wi2Wi GPS module connected through UART
+
+Required properties:
+- compatible: wi2wi,w2sg0004 or wi2wi,w2sg0084
+- pinctrl: specify two states (default and monitor). One is the default (UART) mode
+  and the other is for monitoring the RX line by an interrupt
+- on-off-gpio: the GPIO that controls the module's on-off toggle input
+
+Optional properties:
+- lna-suppy: an (optional) LNA regulator that is enabled together with the GPS receiver
+
+example:
+
+        gps_receiver: w2sg0004 {
+                compatible = "wi2wi,w2sg0004";
+                gpio-controller;
+                #gpio-cells = <2>;
+
+                pinctrl-names = "default", "monitor";
+                pinctrl-0 = <&uart2_pins>;
+                pinctrl-1 = <&uart2_rx_irq_pins>;
+
+                interrupt-parent = <&gpio5>;
+                interrupts = <19 IRQ_TYPE_EDGE_FALLING>;  /* GPIO_147: RX - trigger on arrival of start bit */
+                lna-supply = <&vsim>;	/* LNA regulator */
+                on-off-gpio = <&gpio5 17 0>;	/* GPIO_145: trigger for turning on/off w2sg0004 */
+
+&pinmux {
+
+	uart2_pins: pinmux_uart2_pins {
+		pinctrl-single,pins = <
+			0x14a (PIN_INPUT | MUX_MODE0)		/* uart2_tx.uart2_rx */
+			0x148 (PIN_OUTPUT | MUX_MODE0)		/* uart2_tx.uart2_tx */
+		>;
+	};
+
+	uart2_rx_irq_pins: pinmux_uart2_rx_irq_pins {
+		pinctrl-single,pins = <
+			/* switch RX to GPIO so that we can get interrupts by the start bit */
+			0x14a (PIN_INPUT | MUX_MODE4)		/* uart2_tx.uart2_rx */
+		>;
+	};
+
+}
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps
  2014-10-16 20:26 ` [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps Marek Belisko
@ 2014-10-17  9:37   ` Mark Rutland
  2014-10-17 10:16     ` Dr. H. Nikolaus Schaller
  0 siblings, 1 reply; 15+ messages in thread
From: Mark Rutland @ 2014-10-17  9:37 UTC (permalink / raw)
  To: Marek Belisko
  Cc: arnd, gregkh, robh+dt, Pawel Moll, ijc+devicetree, galak,
	grant.likely, devicetree, linux-kernel, H. Nikolaus Schaller

On Thu, Oct 16, 2014 at 09:26:23PM +0100, Marek Belisko wrote:
> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
> Signed-off-by: Marek Belisko <marek@goldelico.com>
> ---
>  .../devicetree/bindings/misc/wi2wi,w2sg0004.txt    | 44 ++++++++++++++++++++++
>  1 file changed, 44 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
> 
> diff --git a/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
> new file mode 100644
> index 0000000..e144441
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
> @@ -0,0 +1,44 @@
> +Wi2Wi GPS module connected through UART
> +
> +Required properties:
> +- compatible: wi2wi,w2sg0004 or wi2wi,w2sg0084
> +- pinctrl: specify two states (default and monitor). One is the default (UART) mode
> +  and the other is for monitoring the RX line by an interrupt
> +- on-off-gpio: the GPIO that controls the module's on-off toggle input
> +
> +Optional properties:
> +- lna-suppy: an (optional) LNA regulator that is enabled together with the GPS receiver
> +
> +example:
> +
> +        gps_receiver: w2sg0004 {
> +                compatible = "wi2wi,w2sg0004";

I couldn't spot "wi2wi" in
Documentation/devicetree/bindings/vendor-prefixes.txt (in mainline).

Could you please add it?

> +                gpio-controller;
> +                #gpio-cells = <2>;

As far as I can see, these properties aren't necessary. This only
consumes a GPIO, it doesn't provide any.

> +
> +                pinctrl-names = "default", "monitor";
> +                pinctrl-0 = <&uart2_pins>;
> +                pinctrl-1 = <&uart2_rx_irq_pins>;
> +
> +                interrupt-parent = <&gpio5>;
> +                interrupts = <19 IRQ_TYPE_EDGE_FALLING>;  /* GPIO_147: RX - trigger on arrival of start bit */

While interrupts is a standard property, please describe above how many
you expect and what their logical function is.

The only part I'm confused about is how the link to the UART is
described. I assume I'm just ignorant of some existing pattern.

Otherwise this looks ok.

Thanks,
Mark.

> +                lna-supply = <&vsim>;	/* LNA regulator */
> +                on-off-gpio = <&gpio5 17 0>;	/* GPIO_145: trigger for turning on/off w2sg0004 */
> +
> +&pinmux {
> +
> +	uart2_pins: pinmux_uart2_pins {
> +		pinctrl-single,pins = <
> +			0x14a (PIN_INPUT | MUX_MODE0)		/* uart2_tx.uart2_rx */
> +			0x148 (PIN_OUTPUT | MUX_MODE0)		/* uart2_tx.uart2_tx */
> +		>;
> +	};
> +
> +	uart2_rx_irq_pins: pinmux_uart2_rx_irq_pins {
> +		pinctrl-single,pins = <
> +			/* switch RX to GPIO so that we can get interrupts by the start bit */
> +			0x14a (PIN_INPUT | MUX_MODE4)		/* uart2_tx.uart2_rx */
> +		>;
> +	};
> +
> +}
> -- 
> 1.9.1
> 
> 

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps
  2014-10-17  9:37   ` Mark Rutland
@ 2014-10-17 10:16     ` Dr. H. Nikolaus Schaller
  2014-10-17 11:00       ` Mark Rutland
  0 siblings, 1 reply; 15+ messages in thread
From: Dr. H. Nikolaus Schaller @ 2014-10-17 10:16 UTC (permalink / raw)
  To: Mark Rutland
  Cc: Marek Belisko, arnd, gregkh, robh+dt, Pawel Moll, ijc+devicetree,
	galak, grant.likely, devicetree, linux-kernel


Am 17.10.2014 um 11:37 schrieb Mark Rutland <mark.rutland@arm.com>:

> On Thu, Oct 16, 2014 at 09:26:23PM +0100, Marek Belisko wrote:
>> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
>> Signed-off-by: Marek Belisko <marek@goldelico.com>
>> ---
>> .../devicetree/bindings/misc/wi2wi,w2sg0004.txt    | 44 ++++++++++++++++++++++
>> 1 file changed, 44 insertions(+)
>> create mode 100644 Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>> 
>> diff --git a/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>> new file mode 100644
>> index 0000000..e144441
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>> @@ -0,0 +1,44 @@
>> +Wi2Wi GPS module connected through UART
>> +
>> +Required properties:
>> +- compatible: wi2wi,w2sg0004 or wi2wi,w2sg0084
>> +- pinctrl: specify two states (default and monitor). One is the default (UART) mode
>> +  and the other is for monitoring the RX line by an interrupt
>> +- on-off-gpio: the GPIO that controls the module's on-off toggle input
>> +
>> +Optional properties:
>> +- lna-suppy: an (optional) LNA regulator that is enabled together with the GPS receiver
>> +
>> +example:
>> +
>> +        gps_receiver: w2sg0004 {
>> +                compatible = "wi2wi,w2sg0004";
> 
> I couldn't spot "wi2wi" in
> Documentation/devicetree/bindings/vendor-prefixes.txt (in mainline).
> 
> Could you please add it?
> 
>> +                gpio-controller;
>> +                #gpio-cells = <2>;
> 
> As far as I can see, these properties aren't necessary. This only
> consumes a GPIO, it doesn't provide any.

Well, it provides one GPIO. Sort of a "virtual“ GPIO. It is needed so that 
it can be wired up to the DTR gpio of the UART driver (unfortunately this
patch was reverted some months ago from mainline and we will reintroduce
it soon).

The reason to solve it that way is that we did not want to have a direct link
between this driver and any serial drivers or other mechanisms how drivers
can detect that their serial port (/dev/tty*) is opened.

It is used to power down the w2sg GPS chip if no user space process is
accessing its serial port (or de-asserts DTR through tcsetattr/ioctl).

> 
>> +
>> +                pinctrl-names = "default", "monitor";
>> +                pinctrl-0 = <&uart2_pins>;
>> +                pinctrl-1 = <&uart2_rx_irq_pins>;
>> +
>> +                interrupt-parent = <&gpio5>;
>> +                interrupts = <19 IRQ_TYPE_EDGE_FALLING>;  /* GPIO_147: RX - trigger on arrival of start bit */
> 
> While interrupts is a standard property, please describe above how many
> you expect and what their logical function is.
> 
> The only part I'm confused about is how the link to the UART is
> described. I assume I'm just ignorant of some existing pattern.

The serial link itself is not described at all because it is assumed to be a „must have“.
The driver only needs to monitor the RX line and needs to switch it between UART and
GPIO/IRQ mode. So this monitoring switch is described (with two different pinctrl states).

We know that it is a little tricky to control this chip correctly - and we think this solution
is the most general (no direct dependency on the serial line, and just to pinmux states
and an interrupt).

> 
> Otherwise this looks ok.
> 
> Thanks,
> Mark.

Thanks as well,
Nikolaus

> 
>> +                lna-supply = <&vsim>;	/* LNA regulator */
>> +                on-off-gpio = <&gpio5 17 0>;	/* GPIO_145: trigger for turning on/off w2sg0004 */
>> +
>> +&pinmux {
>> +
>> +	uart2_pins: pinmux_uart2_pins {
>> +		pinctrl-single,pins = <
>> +			0x14a (PIN_INPUT | MUX_MODE0)		/* uart2_tx.uart2_rx */
>> +			0x148 (PIN_OUTPUT | MUX_MODE0)		/* uart2_tx.uart2_tx */
>> +		>;
>> +	};
>> +
>> +	uart2_rx_irq_pins: pinmux_uart2_rx_irq_pins {
>> +		pinctrl-single,pins = <
>> +			/* switch RX to GPIO so that we can get interrupts by the start bit */
>> +			0x14a (PIN_INPUT | MUX_MODE4)		/* uart2_tx.uart2_rx */
>> +		>;
>> +	};
>> +
>> +}
>> -- 
>> 1.9.1


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps
  2014-10-17 10:16     ` Dr. H. Nikolaus Schaller
@ 2014-10-17 11:00       ` Mark Rutland
  2014-10-17 19:55         ` Dr. H. Nikolaus Schaller
  0 siblings, 1 reply; 15+ messages in thread
From: Mark Rutland @ 2014-10-17 11:00 UTC (permalink / raw)
  To: Dr. H. Nikolaus Schaller
  Cc: Marek Belisko, arnd, gregkh, robh+dt, Pawel Moll, ijc+devicetree,
	galak, grant.likely, devicetree, linux-kernel

On Fri, Oct 17, 2014 at 11:16:42AM +0100, Dr. H. Nikolaus Schaller wrote:
> 
> Am 17.10.2014 um 11:37 schrieb Mark Rutland <mark.rutland@arm.com>:
> 
> > On Thu, Oct 16, 2014 at 09:26:23PM +0100, Marek Belisko wrote:
> >> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
> >> Signed-off-by: Marek Belisko <marek@goldelico.com>
> >> ---
> >> .../devicetree/bindings/misc/wi2wi,w2sg0004.txt    | 44 ++++++++++++++++++++++
> >> 1 file changed, 44 insertions(+)
> >> create mode 100644 Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
> >> 
> >> diff --git a/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
> >> new file mode 100644
> >> index 0000000..e144441
> >> --- /dev/null
> >> +++ b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
> >> @@ -0,0 +1,44 @@
> >> +Wi2Wi GPS module connected through UART
> >> +
> >> +Required properties:
> >> +- compatible: wi2wi,w2sg0004 or wi2wi,w2sg0084
> >> +- pinctrl: specify two states (default and monitor). One is the default (UART) mode
> >> +  and the other is for monitoring the RX line by an interrupt
> >> +- on-off-gpio: the GPIO that controls the module's on-off toggle input
> >> +
> >> +Optional properties:
> >> +- lna-suppy: an (optional) LNA regulator that is enabled together with the GPS receiver
> >> +
> >> +example:
> >> +
> >> +        gps_receiver: w2sg0004 {
> >> +                compatible = "wi2wi,w2sg0004";
> > 
> > I couldn't spot "wi2wi" in
> > Documentation/devicetree/bindings/vendor-prefixes.txt (in mainline).
> > 
> > Could you please add it?
> > 
> >> +                gpio-controller;
> >> +                #gpio-cells = <2>;
> > 
> > As far as I can see, these properties aren't necessary. This only
> > consumes a GPIO, it doesn't provide any.
> 
> Well, it provides one GPIO. Sort of a "virtual“ GPIO. It is needed so that 
> it can be wired up to the DTR gpio of the UART driver (unfortunately this
> patch was reverted some months ago from mainline and we will reintroduce
> it soon).

If this GPIO doesn't really exist, then it's a Linux internal
implementation detail rather than a description of the hardware, and
doesn't really belong in the DT.

It sounds like what we actually need is the ability to describe devices
attached to UARTs. Then you could have a mechanism whereby the UART
driver can notify the other device driver regarding events (e.g. the
UART being opened for access), or the other driver could claim ownership
of the UART and expose its own interface to userspace.

That would be independent of the particular UART or other device, and
the only description necessary in the DT would be an accurate
representation of the way the hardware is wired.

There are a few ways that could be done, but I suspect the simplest is
to just have the device as a sub-node of the UART, like we do for SPI or
I2C buses:

	serial@f00 {
		compatible = "vendor,uart";
		reg = <0xf00 0x100>;
		...

		gps {
			compatible = "wi2wi,w2sg0004";
			...
		};
	};

That wouldn't work for devices with multiple UART connections. Do those
exist, and are they common in configurations where out-of-band
management is necessary (e.g. regulators, clocks)?

> The reason to solve it that way is that we did not want to have a direct link
> between this driver and any serial drivers or other mechanisms how drivers
> can detect that their serial port (/dev/tty*) is opened.
>
> It is used to power down the w2sg GPS chip if no user space process is
> accessing its serial port (or de-asserts DTR through tcsetattr/ioctl).
> 
> > 
> >> +
> >> +                pinctrl-names = "default", "monitor";
> >> +                pinctrl-0 = <&uart2_pins>;
> >> +                pinctrl-1 = <&uart2_rx_irq_pins>;
> >> +
> >> +                interrupt-parent = <&gpio5>;
> >> +                interrupts = <19 IRQ_TYPE_EDGE_FALLING>;  /* GPIO_147: RX - trigger on arrival of start bit */
> > 
> > While interrupts is a standard property, please describe above how many
> > you expect and what their logical function is.
> > 
> > The only part I'm confused about is how the link to the UART is
> > described. I assume I'm just ignorant of some existing pattern.
> 
> The serial link itself is not described at all because it is assumed to be a „must have“.

Huh? So it's a "must have" that you "don't have" in the DT?

I think that the relationship is being described incorrectly in the DT,
and I think that there is a more general problem that needs to be
addressed in order to make this case work.

> The driver only needs to monitor the RX line and needs to switch it between UART and
> GPIO/IRQ mode. So this monitoring switch is described (with two different pinctrl states).

While this particular driver only needs that at this point in time,
that's not necessarily true of drivers for similar devices, nor is it
necessarily true if we need to add additional features to this driver.

Describing the relationship leaves a lot more freedom to improve things
without having to update every DTB.
 
> We know that it is a little tricky to control this chip correctly - and we think this solution
> is the most general (no direct dependency on the serial line, and just to pinmux states
> and an interrupt).

I think that the rough approach I sketched out above is more general,
and I think that you must describe the relationship with the serial
line.

It's not clear to me whether the interrupt you describe is attached to
the GPS, or if this is logically part of the UART.

Thanks,
Mark.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps
  2014-10-17 11:00       ` Mark Rutland
@ 2014-10-17 19:55         ` Dr. H. Nikolaus Schaller
  2014-10-20  9:35           ` Mark Rutland
  0 siblings, 1 reply; 15+ messages in thread
From: Dr. H. Nikolaus Schaller @ 2014-10-17 19:55 UTC (permalink / raw)
  To: Mark Rutland
  Cc: Marek Belisko, arnd, gregkh, robh+dt, Pawel Moll, ijc+devicetree,
	galak, grant.likely, devicetree, linux-kernel, NeilBrown


Am 17.10.2014 um 13:00 schrieb Mark Rutland <mark.rutland@arm.com>:

> On Fri, Oct 17, 2014 at 11:16:42AM +0100, Dr. H. Nikolaus Schaller wrote:
>> 
>> Am 17.10.2014 um 11:37 schrieb Mark Rutland <mark.rutland@arm.com>:
>> 
>>> On Thu, Oct 16, 2014 at 09:26:23PM +0100, Marek Belisko wrote:
>>>> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
>>>> Signed-off-by: Marek Belisko <marek@goldelico.com>
>>>> ---
>>>> .../devicetree/bindings/misc/wi2wi,w2sg0004.txt    | 44 ++++++++++++++++++++++
>>>> 1 file changed, 44 insertions(+)
>>>> create mode 100644 Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>> 
>>>> diff --git a/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>> new file mode 100644
>>>> index 0000000..e144441
>>>> --- /dev/null
>>>> +++ b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>> @@ -0,0 +1,44 @@
>>>> +Wi2Wi GPS module connected through UART
>>>> +
>>>> +Required properties:
>>>> +- compatible: wi2wi,w2sg0004 or wi2wi,w2sg0084
>>>> +- pinctrl: specify two states (default and monitor). One is the default (UART) mode
>>>> +  and the other is for monitoring the RX line by an interrupt
>>>> +- on-off-gpio: the GPIO that controls the module's on-off toggle input
>>>> +
>>>> +Optional properties:
>>>> +- lna-suppy: an (optional) LNA regulator that is enabled together with the GPS receiver
>>>> +
>>>> +example:
>>>> +
>>>> +        gps_receiver: w2sg0004 {
>>>> +                compatible = "wi2wi,w2sg0004";
>>> 
>>> I couldn't spot "wi2wi" in
>>> Documentation/devicetree/bindings/vendor-prefixes.txt (in mainline).
>>> 
>>> Could you please add it?
>>> 
>>>> +                gpio-controller;
>>>> +                #gpio-cells = <2>;
>>> 
>>> As far as I can see, these properties aren't necessary. This only
>>> consumes a GPIO, it doesn't provide any.
>> 
>> Well, it provides one GPIO. Sort of a "virtual“ GPIO. It is needed so that 
>> it can be wired up to the DTR gpio of the UART driver (unfortunately this
>> patch was reverted some months ago from mainline and we will reintroduce
>> it soon).
> 
> If this GPIO doesn't really exist, then it's a Linux internal
> implementation detail rather than a description of the hardware, and
> doesn't really belong in the DT.

Hm. 

Let’s describe it differently.

I can see the Linux driver module as a simple software simulation for a
piece of hardware that could have been connected between the UART
and the GPS chip.

Basically it is a pulse-generator and a flip-flop to detect data flow on the RX
wire. This could be implemented by an external FPGA or uController. Or as
it is done on our board for saving hardware by a mix of the main CPU hardware
(Pinmux + GPIO + IRQ) and a kernel driver.

The best of course would have been if the w2sg0004 would have a physical
„enable“ GPIO (instead of that on-off control input).

Then we would hook up that enable to some physical GPIO of the CPU
and simply refer to it as e.g. <&gpio4 12>. And would not even need a driver
for it (unless we want to make rfkill gps work).

Therefore the driver we suggest provides an additional gpio controller with a
single GPIO so that we can write <&w2sg 0> to refer to this virtual gpio.

So in fact we try to wrap a non-optimal chip design by the driver and make it
appear as a standard GPIO interface to the DT and user space and whoever
needs simply to enable/disable the GPS chip.

> 
> It sounds like what we actually need is the ability to describe devices
> attached to UARTs.

Hm. The purpose of the driver is power control of the chip. Not the serial
interface.

> Then you could have a mechanism whereby the UART
> driver can notify the other device driver regarding events (e.g. the
> UART being opened for access), or the other driver could claim ownership
> of the UART and expose its own interface to userspace.
> 
> That would be independent of the particular UART or other device, and
> the only description necessary in the DT would be an accurate
> representation of the way the hardware is wired.
> 
> There are a few ways that could be done, but I suspect the simplest is
> to just have the device as a sub-node of the UART, like we do for SPI or
> I2C buses:
> 
> 	serial@f00 {
> 		compatible = "vendor,uart";
> 		reg = <0xf00 0x100>;
> 		...
> 
> 		gps {
> 			compatible = "wi2wi,w2sg0004";
> 			...
> 		};
> 	};
> 
> That wouldn't work for devices with multiple UART connections. Do those
> exist, and are they common in configurations where out-of-band
> management is necessary (e.g. regulators, clocks)?

UARTs are usually point to point interfaces and not busses. So there is
no need to describe the interface. And I would speculate that in most cases
they simply go to some connector and therefore no connected chip that needs
to be described in the DT at all. Because it has a user-space driver (e.g. AT
commands) and no kernel driver.

But we have no idea how such a solution could be implemented or tested.

If someone adds that to the serial drivers, we would be happy to use it,
but unless such a thing exists, I think our solution is quite simple and isolated
into this single driver and also uses existing standard interfaces (gpios, pinmux).

> 
>> The reason to solve it that way is that we did not want to have a direct link
>> between this driver and any serial drivers or other mechanisms how drivers
>> can detect that their serial port (/dev/tty*) is opened.
>> 
>> It is used to power down the w2sg GPS chip if no user space process is
>> accessing its serial port (or de-asserts DTR through tcsetattr/ioctl).
>> 
>>> 
>>>> +
>>>> +                pinctrl-names = "default", "monitor";
>>>> +                pinctrl-0 = <&uart2_pins>;
>>>> +                pinctrl-1 = <&uart2_rx_irq_pins>;
>>>> +
>>>> +                interrupt-parent = <&gpio5>;
>>>> +                interrupts = <19 IRQ_TYPE_EDGE_FALLING>;  /* GPIO_147: RX - trigger on arrival of start bit */
>>> 
>>> While interrupts is a standard property, please describe above how many
>>> you expect and what their logical function is.
>>> 
>>> The only part I'm confused about is how the link to the UART is
>>> described. I assume I'm just ignorant of some existing pattern.
>> 
>> The serial link itself is not described at all because it is assumed to be a „must have“.
> 
> Huh? So it's a "must have" that you "don't have" in the DT?

Yes. The DT does not describe everything. Only those things that need
a kernel driver. And UARTs usually have user-space drivers (e.g. gpsd,
gsmd, pppd) and ioctl/tcsetattr.

> 
> I think that the relationship is being described incorrectly in the DT,
> and I think that there is a more general problem that needs to be
> addressed in order to make this case work.
> 
>> The driver only needs to monitor the RX line and needs to switch it between UART and
>> GPIO/IRQ mode. So this monitoring switch is described (with two different pinctrl states).
> 
> While this particular driver only needs that at this point in time,
> that's not necessarily true of drivers for similar devices, nor is it
> necessarily true if we need to add additional features to this driver.

Which features are you thinking of to add to this driver? And do
similar devices exist at all? Since we have not found any, we have
declared it as a "misc“ driver.

> 
> Describing the relationship leaves a lot more freedom to improve things
> without having to update every DTB.
> 
>> We know that it is a little tricky to control this chip correctly - and we think this solution
>> is the most general (no direct dependency on the serial line, and just to pinmux states
>> and an interrupt).
> 
> I think that the rough approach I sketched out above is more general,
> and I think that you must describe the relationship with the serial
> line.
> 
> It's not clear to me whether the interrupt you describe is attached to
> the GPS, or if this is logically part of the UART.

The interrupt is needed to simulate the glue logic connected between
UART and GPS.

The output signal comes from the GPS module and goes to some pad
of the OMAP3 SoC. This pad can be either multiplexed into the UART RX
input or into a GPIO bank of the OMAP. That GPIO controller can generate
the interrupt on incoming data (when none is expected).

Therefore it is a GPS-generated interrupt and has nothing to do with
the UART.

> 
> Thanks,
> Mark.

BR,
Nikolaus


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver
  2014-10-16 20:26 [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver Marek Belisko
  2014-10-16 20:26 ` [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps Marek Belisko
@ 2014-10-19 19:51 ` Arnd Bergmann
  2014-10-19 20:29   ` Dr. H. Nikolaus Schaller
  2014-10-21 10:49 ` Pavel Machek
  2 siblings, 1 reply; 15+ messages in thread
From: Arnd Bergmann @ 2014-10-19 19:51 UTC (permalink / raw)
  To: Marek Belisko
  Cc: gregkh, robh+dt, pawel.moll, mark.rutland, ijc+devicetree, galak,
	grant.likely, devicetree, linux-kernel, NeilBrown,
	H. Nikolaus Schaller

On Thursday 16 October 2014 22:26:22 Marek Belisko wrote:
> This is a driver for the Wi2Wi GPS modules connected through an UART.
> The tricky part is that the module is turned on or off by an impulse
> on the control line - but it is only possible to get the real state by monitoring
> the UART RX input. Since the kernel can't know in which status the module
> is brought e.g. by a boot loader or after suspend, we need some logic to handle
> this.
> 
> The driver allows two different methods to enable (and power up) GPS:
> a) through rfkill
> b) as a GPIO
> 
> The GPIO enable can be plumbed to other drivers that expect to be able to control
> a GPIO. On the GTA04 board this is the DTR-gpio of the connected UART so that
> opening the UART device enables the receiver and closing it shuts the receiver down.
> 
> Original implementation by Neil Brown, fixes + DT bindings by H. Nikolaus Schaller

I'm not sure if this is a good way to model the device. You say that it's
connected to a UART, but the code itself has no reference to the tty layer
at all. I assume the actual driver is in user space and it would open the
UART and this control device as separate instances handles and then try
to coordinate them. Is that right?

What is the protocol for the GPS driver itself? Is it standardized?
Would it help to have a TTY line discipline for the GPS mode instead
so the power management and startup could be integrated into the serial
port management instead?

	Arnd

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver
  2014-10-19 19:51 ` [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver Arnd Bergmann
@ 2014-10-19 20:29   ` Dr. H. Nikolaus Schaller
  0 siblings, 0 replies; 15+ messages in thread
From: Dr. H. Nikolaus Schaller @ 2014-10-19 20:29 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Marek Belisko, gregkh, robh+dt, pawel.moll, mark.rutland,
	ijc+devicetree, galak, grant.likely, devicetree, linux-kernel,
	NeilBrown

Hi,

Am 19.10.2014 um 21:51 schrieb Arnd Bergmann <arnd@arndb.de>:

> On Thursday 16 October 2014 22:26:22 Marek Belisko wrote:
>> This is a driver for the Wi2Wi GPS modules connected through an UART.
>> The tricky part is that the module is turned on or off by an impulse
>> on the control line - but it is only possible to get the real state by monitoring
>> the UART RX input. Since the kernel can't know in which status the module
>> is brought e.g. by a boot loader or after suspend, we need some logic to handle
>> this.
>> 
>> The driver allows two different methods to enable (and power up) GPS:
>> a) through rfkill
>> b) as a GPIO
>> 
>> The GPIO enable can be plumbed to other drivers that expect to be able to control
>> a GPIO. On the GTA04 board this is the DTR-gpio of the connected UART so that
>> opening the UART device enables the receiver and closing it shuts the receiver down.
>> 
>> Original implementation by Neil Brown, fixes + DT bindings by H. Nikolaus Schaller
> 
> I’m not sure if this is a good way to model the device.

It is the easiest way we have found after ca. 2 years of working on it :)

> You say that it's
> connected to a UART, but the code itself has no reference to the tty layer
> at all.

Yes.

> I assume the actual driver is in user space and it would open the
> UART and this control device as separate instances handles and then try
> to coordinate them. Is that right?

Yes, it is called gpsd.

> 
> What is the protocol for the GPS driver itself? Is it standardized?

Yes, NMEA records. Like most GPS receivers provide them.

> Would it help to have a TTY line discipline for the GPS mode instead
> so the power management and startup could be integrated into the serial
> port management instead?

I don’t think line disciplines (as far as I understand them) are helpful and
this chip is not special at all regarding the serial interface data. So it needs no
“GPS mode”.

It is very similar to connecting some external handheld GPS receiver by a
RS232 cable or RS232-USB adapter. Or GPS “mouse” receivers through
bluetooth.

They all create/show some /dev/tty that you simply open/read/close. And
there is no special processing of the serial data involved.

The only thing this driver needs to solve is to properly power up/down
the chip on demand.

The DTR line of the tty can enable/disable power of a connected device
(being an external modem or this GPS chip). This is what we have made
the driver compatible to by providing a virtual GPIO to enable/disable.

This is done by a patch to the tty driver that already was in the kernel
but removed in 3.15 because the w2sg driver wasn’t submitted at that
time.

Commit: 985bfd54c826c0ba873ca0adfd5589263e0c6ee2

Basically, if our CPU would have a real RS232 interface and we would
put the chip into an external device, it should look exactly the same if
we look at the serial interface part. Since soldering it on the same PCB
or connecting through a connector should not need different serial drivers.
Just different device tree files.

Therefore we have decided not to touch any serial driver code at all,
because it is not needed (except allowing DTR to control some GPIO)
and keeps it simple and manageable.

BR,
Nikolaus



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps
  2014-10-17 19:55         ` Dr. H. Nikolaus Schaller
@ 2014-10-20  9:35           ` Mark Rutland
  2014-10-20 17:26             ` Dr. H. Nikolaus Schaller
  0 siblings, 1 reply; 15+ messages in thread
From: Mark Rutland @ 2014-10-20  9:35 UTC (permalink / raw)
  To: Dr. H. Nikolaus Schaller
  Cc: Marek Belisko, arnd, gregkh, robh+dt, Pawel Moll, ijc+devicetree,
	galak, grant.likely, devicetree, linux-kernel, NeilBrown

Hi,

On Fri, Oct 17, 2014 at 08:55:50PM +0100, Dr. H. Nikolaus Schaller wrote:
> 
> Am 17.10.2014 um 13:00 schrieb Mark Rutland <mark.rutland@arm.com>:
> 
> > On Fri, Oct 17, 2014 at 11:16:42AM +0100, Dr. H. Nikolaus Schaller wrote:
> >> 
> >> Am 17.10.2014 um 11:37 schrieb Mark Rutland <mark.rutland@arm.com>:
> >> 
> >>> On Thu, Oct 16, 2014 at 09:26:23PM +0100, Marek Belisko wrote:
> >>>> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
> >>>> Signed-off-by: Marek Belisko <marek@goldelico.com>
> >>>> ---
> >>>> .../devicetree/bindings/misc/wi2wi,w2sg0004.txt    | 44 ++++++++++++++++++++++
> >>>> 1 file changed, 44 insertions(+)
> >>>> create mode 100644 Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
> >>>> 
> >>>> diff --git a/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
> >>>> new file mode 100644
> >>>> index 0000000..e144441
> >>>> --- /dev/null
> >>>> +++ b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
> >>>> @@ -0,0 +1,44 @@
> >>>> +Wi2Wi GPS module connected through UART
> >>>> +
> >>>> +Required properties:
> >>>> +- compatible: wi2wi,w2sg0004 or wi2wi,w2sg0084
> >>>> +- pinctrl: specify two states (default and monitor). One is the default (UART) mode
> >>>> +  and the other is for monitoring the RX line by an interrupt
> >>>> +- on-off-gpio: the GPIO that controls the module's on-off toggle input
> >>>> +
> >>>> +Optional properties:
> >>>> +- lna-suppy: an (optional) LNA regulator that is enabled together with the GPS receiver
> >>>> +
> >>>> +example:
> >>>> +
> >>>> +        gps_receiver: w2sg0004 {
> >>>> +                compatible = "wi2wi,w2sg0004";
> >>> 
> >>> I couldn't spot "wi2wi" in
> >>> Documentation/devicetree/bindings/vendor-prefixes.txt (in mainline).
> >>> 
> >>> Could you please add it?
> >>> 
> >>>> +                gpio-controller;
> >>>> +                #gpio-cells = <2>;
> >>> 
> >>> As far as I can see, these properties aren't necessary. This only
> >>> consumes a GPIO, it doesn't provide any.
> >> 
> >> Well, it provides one GPIO. Sort of a "virtual“ GPIO. It is needed so that 
> >> it can be wired up to the DTR gpio of the UART driver (unfortunately this
> >> patch was reverted some months ago from mainline and we will reintroduce
> >> it soon).
> > 
> > If this GPIO doesn't really exist, then it's a Linux internal
> > implementation detail rather than a description of the hardware, and
> > doesn't really belong in the DT.
> 
> Hm. 
> 
> Let’s describe it differently.
> 
> I can see the Linux driver module as a simple software simulation for a
> piece of hardware that could have been connected between the UART
> and the GPS chip.
> 
> Basically it is a pulse-generator and a flip-flop to detect data flow on the RX
> wire. This could be implemented by an external FPGA or uController. Or as
> it is done on our board for saving hardware by a mix of the main CPU hardware
> (Pinmux + GPIO + IRQ) and a kernel driver.
> 
> The best of course would have been if the w2sg0004 would have a physical
> „enable“ GPIO (instead of that on-off control input).
> 
> Then we would hook up that enable to some physical GPIO of the CPU
> and simply refer to it as e.g. <&gpio4 12>. And would not even need a driver
> for it (unless we want to make rfkill gps work).
> 
> Therefore the driver we suggest provides an additional gpio controller with a
> single GPIO so that we can write <&w2sg 0> to refer to this virtual gpio.
> 
> So in fact we try to wrap a non-optimal chip design by the driver and make it
> appear as a standard GPIO interface to the DT and user space and whoever
> needs simply to enable/disable the GPS chip.

The fact remains that this does not accurately represent the hardware,
and is unnecessarily strongly tied to a particular UART design (where
the DTR line is a separate UART).

> > It sounds like what we actually need is the ability to describe devices
> > attached to UARTs.
> 
> Hm. The purpose of the driver is power control of the chip. Not the serial
> interface.

I'm not sure I follow your point. By describing the device as attached
to the UART, the kernel can figure out that when said UART is accessed
the attached device needs to be on (and must be poked as necessary).

The power management logic for the device can stay in the device driver,
and the power management logic for the UART can stay in the UART driver.
Neither would need to know about each other's internal details
(e.g.GPIOs, for DTR or otherwise).

> > Then you could have a mechanism whereby the UART
> > driver can notify the other device driver regarding events (e.g. the
> > UART being opened for access), or the other driver could claim ownership
> > of the UART and expose its own interface to userspace.
> > 
> > That would be independent of the particular UART or other device, and
> > the only description necessary in the DT would be an accurate
> > representation of the way the hardware is wired.
> > 
> > There are a few ways that could be done, but I suspect the simplest is
> > to just have the device as a sub-node of the UART, like we do for SPI or
> > I2C buses:
> > 
> > 	serial@f00 {
> > 		compatible = "vendor,uart";
> > 		reg = <0xf00 0x100>;
> > 		...
> > 
> > 		gps {
> > 			compatible = "wi2wi,w2sg0004";
> > 			...
> > 		};
> > 	};
> > 
> > That wouldn't work for devices with multiple UART connections. Do those
> > exist, and are they common in configurations where out-of-band
> > management is necessary (e.g. regulators, clocks)?
> 
> UARTs are usually point to point interfaces and not busses. So there is
> no need to describe the interface.

I don't follow. You have a device which seems to require management
kernel-side. Rather than describing the interface, you've described a
fictitious relationship between the GPS device and the UART's DTR GPIO,
and you've described a fictitious GPIO to hand to the UART driver. This
is how you have linked the two in order to get the behaviour you want.

So it _is_ necessary to describe the interface. Rather than describing
that interface at a high level you've chosen to hack together a set of
fake relationships to work around the lack of ability to describe said
interface.

> And I would speculate that in most cases they simply go to some
> connector and therefore no connected chip that needs to be described
> in the DT at all. Because it has a user-space driver (e.g. AT
> commands) and no kernel driver.

In the case where no driver is necessary I agree that no description is
necessary, though the description could be exposed in a helpful way to
userspace to describe what's attached to which UARTs.

However, in this case you do have a kernel driver (even if basic), and
it requires some knowledge of the relationship between the device and
the UART in order to function.

> But we have no idea how such a solution could be implemented or tested.

I would disagree on that point, given I provided a high level
description of how this could be implemented.

> If someone adds that to the serial drivers, we would be happy to use it,
> but unless such a thing exists, I think our solution is quite simple and isolated
> into this single driver and also uses existing standard interfaces (gpios, pinmux).

I would argue that this _abuses_ standard interfaces, as you have one
device driver fiddling with resources logically owned by another.

> >> The reason to solve it that way is that we did not want to have a direct link
> >> between this driver and any serial drivers or other mechanisms how drivers
> >> can detect that their serial port (/dev/tty*) is opened.
> >> 
> >> It is used to power down the w2sg GPS chip if no user space process is
> >> accessing its serial port (or de-asserts DTR through tcsetattr/ioctl).
> >> 
> >>> 
> >>>> +
> >>>> +                pinctrl-names = "default", "monitor";
> >>>> +                pinctrl-0 = <&uart2_pins>;
> >>>> +                pinctrl-1 = <&uart2_rx_irq_pins>;
> >>>> +
> >>>> +                interrupt-parent = <&gpio5>;
> >>>> +                interrupts = <19 IRQ_TYPE_EDGE_FALLING>;  /* GPIO_147: RX - trigger on arrival of start bit */
> >>> 
> >>> While interrupts is a standard property, please describe above how many
> >>> you expect and what their logical function is.
> >>> 
> >>> The only part I'm confused about is how the link to the UART is
> >>> described. I assume I'm just ignorant of some existing pattern.
> >> 
> >> The serial link itself is not described at all because it is assumed to be a „must have“.
> > 
> > Huh? So it's a "must have" that you "don't have" in the DT?
> 
> Yes. The DT does not describe everything. Only those things that need
> a kernel driver. And UARTs usually have user-space drivers (e.g. gpsd,
> gsmd, pppd) and ioctl/tcsetattr.

The DT should describe the static portions of the hardware. Typically we
only have devices with kernelspace drivers described simply because
that's the way people have built DTs. Whether or not you have a
kernelspace driver can change over time, the organisation of the
hardware cannot.

> > I think that the relationship is being described incorrectly in the DT,
> > and I think that there is a more general problem that needs to be
> > addressed in order to make this case work.
> > 
> >> The driver only needs to monitor the RX line and needs to switch it between UART and
> >> GPIO/IRQ mode. So this monitoring switch is described (with two different pinctrl states).
> > 
> > While this particular driver only needs that at this point in time,
> > that's not necessarily true of drivers for similar devices, nor is it
> > necessarily true if we need to add additional features to this driver.
> 
> Which features are you thinking of to add to this driver? And do
> similar devices exist at all? Since we have not found any, we have
> declared it as a "misc“ driver.

I don't have any particular feature in mind.

I am not immediately aware of other serial devices which require
out-of-band management in the same way, though we have a vaguely similar
case with SDIO devices which must be powered up before they appear on
the bus. In that case I believe the intent is to describe them in the DT
under the bus.

> > Describing the relationship leaves a lot more freedom to improve things
> > without having to update every DTB.
> > 
> >> We know that it is a little tricky to control this chip correctly - and we think this solution
> >> is the most general (no direct dependency on the serial line, and just to pinmux states
> >> and an interrupt).
> > 
> > I think that the rough approach I sketched out above is more general,
> > and I think that you must describe the relationship with the serial
> > line.
> > 
> > It's not clear to me whether the interrupt you describe is attached to
> > the GPS, or if this is logically part of the UART.
> 
> The interrupt is needed to simulate the glue logic connected between
> UART and GPS.
> 
> The output signal comes from the GPS module and goes to some pad
> of the OMAP3 SoC. This pad can be either multiplexed into the UART RX
> input or into a GPIO bank of the OMAP. That GPIO controller can generate
> the interrupt on incoming data (when none is expected).
> 
> Therefore it is a GPS-generated interrupt and has nothing to do with
> the UART.

Ok. When does the GPS device raise this interrupt?

Thanks,
Mark.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps
  2014-10-20  9:35           ` Mark Rutland
@ 2014-10-20 17:26             ` Dr. H. Nikolaus Schaller
  2014-10-24  9:32               ` Dr. H. Nikolaus Schaller
  0 siblings, 1 reply; 15+ messages in thread
From: Dr. H. Nikolaus Schaller @ 2014-10-20 17:26 UTC (permalink / raw)
  To: Mark Rutland
  Cc: Marek Belisko, arnd, gregkh, robh+dt, Pawel Moll, ijc+devicetree,
	galak, grant.likely, devicetree, linux-kernel, NeilBrown

Hi,

Am 20.10.2014 um 11:35 schrieb Mark Rutland <mark.rutland@arm.com>:

> Hi,
> 
> On Fri, Oct 17, 2014 at 08:55:50PM +0100, Dr. H. Nikolaus Schaller wrote:
>> 
>> Am 17.10.2014 um 13:00 schrieb Mark Rutland <mark.rutland@arm.com>:
>> 
>>> On Fri, Oct 17, 2014 at 11:16:42AM +0100, Dr. H. Nikolaus Schaller wrote:
>>>> 
>>>> Am 17.10.2014 um 11:37 schrieb Mark Rutland <mark.rutland@arm.com>:
>>>> 
>>>>> On Thu, Oct 16, 2014 at 09:26:23PM +0100, Marek Belisko wrote:
>>>>>> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
>>>>>> Signed-off-by: Marek Belisko <marek@goldelico.com>
>>>>>> ---
>>>>>> .../devicetree/bindings/misc/wi2wi,w2sg0004.txt    | 44 ++++++++++++++++++++++
>>>>>> 1 file changed, 44 insertions(+)
>>>>>> create mode 100644 Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>>>> 
>>>>>> diff --git a/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>>>> new file mode 100644
>>>>>> index 0000000..e144441
>>>>>> --- /dev/null
>>>>>> +++ b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>>>> @@ -0,0 +1,44 @@
>>>>>> +Wi2Wi GPS module connected through UART
>>>>>> +
>>>>>> +Required properties:
>>>>>> +- compatible: wi2wi,w2sg0004 or wi2wi,w2sg0084
>>>>>> +- pinctrl: specify two states (default and monitor). One is the default (UART) mode
>>>>>> +  and the other is for monitoring the RX line by an interrupt
>>>>>> +- on-off-gpio: the GPIO that controls the module's on-off toggle input
>>>>>> +
>>>>>> +Optional properties:
>>>>>> +- lna-suppy: an (optional) LNA regulator that is enabled together with the GPS receiver
>>>>>> +
>>>>>> +example:
>>>>>> +
>>>>>> +        gps_receiver: w2sg0004 {
>>>>>> +                compatible = "wi2wi,w2sg0004";
>>>>> 
>>>>> I couldn't spot "wi2wi" in
>>>>> Documentation/devicetree/bindings/vendor-prefixes.txt (in mainline).
>>>>> 
>>>>> Could you please add it?
>>>>> 
>>>>>> +                gpio-controller;
>>>>>> +                #gpio-cells = <2>;
>>>>> 
>>>>> As far as I can see, these properties aren't necessary. This only
>>>>> consumes a GPIO, it doesn't provide any.
>>>> 
>>>> Well, it provides one GPIO. Sort of a "virtual“ GPIO. It is needed so that 
>>>> it can be wired up to the DTR gpio of the UART driver (unfortunately this
>>>> patch was reverted some months ago from mainline and we will reintroduce
>>>> it soon).
>>> 
>>> If this GPIO doesn't really exist, then it's a Linux internal
>>> implementation detail rather than a description of the hardware, and
>>> doesn't really belong in the DT.
>> 
>> Hm. 
>> 
>> Let’s describe it differently.
>> 
>> I can see the Linux driver module as a simple software simulation for a
>> piece of hardware that could have been connected between the UART
>> and the GPS chip.
>> 
>> Basically it is a pulse-generator and a flip-flop to detect data flow on the RX
>> wire. This could be implemented by an external FPGA or uController. Or as
>> it is done on our board for saving hardware by a mix of the main CPU hardware
>> (Pinmux + GPIO + IRQ) and a kernel driver.
>> 
>> The best of course would have been if the w2sg0004 would have a physical
>> „enable“ GPIO (instead of that on-off control input).
>> 
>> Then we would hook up that enable to some physical GPIO of the CPU
>> and simply refer to it as e.g. <&gpio4 12>. And would not even need a driver
>> for it (unless we want to make rfkill gps work).
>> 
>> Therefore the driver we suggest provides an additional gpio controller with a
>> single GPIO so that we can write <&w2sg 0> to refer to this virtual gpio.
>> 
>> So in fact we try to wrap a non-optimal chip design by the driver and make it
>> appear as a standard GPIO interface to the DT and user space and whoever
>> needs simply to enable/disable the GPS chip.
> 
> The fact remains that this does not accurately represent the hardware,
> and is unnecessarily strongly tied to a particular UART design (where
> the DTR line is a separate UART).

I don’t get that it is tied to a particular UART design (except that our DTR
patch affects to omap-serial only).

Let’s describe the facts:

The chip has this interface:

GPS-TX (output)
GPS-RX (input)
ON/OFF (input) - to toggle the power state of the chip

They are connected to an OMAP3 UART2 as

UART2-TX > GPS-RX
UART2-RX <- GPS-TX
GPIO145 -> ON/OFF

That’s it.

If the chip (or any other serial GPS receiver like a Garmin) were connected
through real RS232 we would have

UART2-TX -> level shifter -> cable -> level shifter -> GPS-RX
UART2-RX <- level shifter <- cable <- level shifter <- GPS-TX
DTR-GPIO -> level shifter (DTR line) -> cable -> level shifter -> power-management logic -> ON/OFF

But because it is connected directly, we need to implement the power-management
logic between the DTR-GPIO and GPIO145:

DTR-GPIO -> driver -> GPIO145 -> ON/OFF

To correctly determine the state we need to tap the RX signal before
it enters the UART2-RX (it is pinmuxed with GPIO147):

UART2-RX <——+
                               OMAP3 pinmux <- OMAP3 pad <- GPS-TX
   GPIO147 <——+

So if we describe the driver as a box we have

inputs from user space:
* rfkill for GPS
* a control that is activated by opening /dev/tty

outputs to system:
* a control to switch the pinmux between RX and GPIO (interrupt)
* a control to external hardware

> 
>>> It sounds like what we actually need is the ability to describe devices
>>> attached to UARTs.
>> 
>> Hm. The purpose of the driver is power control of the chip. Not the serial
>> interface.
> 
> I'm not sure I follow your point. By describing the device as attached
> to the UART, the kernel can figure out that when said UART is accessed
> the attached device needs to be on (and must be poked as necessary).

Why do we need to describe this? It is all about controlling the GPIO145,
GPIO147 and pinmux. But not RX or TX.

So the driver does not need to know anything about UARTs (and if we
connect it to UART3 we only have to specify different GPIOs).

Only our board specific dtb connects the UART to the “virtual” GPIO
provided by the driver.

This also brings up a minor problem: on OMAP3 some UART RX lines can
be pinmuxed with different GPIOs. So putting the driver under some UART2
node doesn’t uniquely define the GPIO number to monitor RX and might
become a mess.

> 
> The power management logic for the device can stay in the device driver,
> and the power management logic for the UART can stay in the UART driver.

Here I can’t follow you. What power management does an UART driver provide?

> Neither would need to know about each other's internal details
> (e.g.GPIOs, for DTR or otherwise).

Yes, that is our goal and in our solution they don’t need to make assumptions
about each other. We just define in the DT: this GTA04 board has UART2-DTR
connected to the W2SG-GPIO# - instead of OMAP3-GPIO145.

In our solution the UART driver knows that there could be a DTR GPIO (similar to
a RTS GPIO which is already provided by the omap-serial RS484 bindings). Which
could be connected to some GPIO which drives the real DTR wire of a RS232 level
shifter.

> 
>>> Then you could have a mechanism whereby the UART
>>> driver can notify the other device driver regarding events (e.g. the
>>> UART being opened for access), or the other driver could claim ownership
>>> of the UART and expose its own interface to userspace.
>>> 
>>> That would be independent of the particular UART or other device, and
>>> the only description necessary in the DT would be an accurate
>>> representation of the way the hardware is wired.
>>> 
>>> There are a few ways that could be done, but I suspect the simplest is
>>> to just have the device as a sub-node of the UART, like we do for SPI or
>>> I2C buses:
>>> 
>>> 	serial@f00 {
>>> 		compatible = "vendor,uart";
>>> 		reg = <0xf00 0x100>;
>>> 		...
>>> 
>>> 		gps {
>>> 			compatible = "wi2wi,w2sg0004";
>>> 			...
>>> 		};
>>> 	};
>>> 
>>> That wouldn't work for devices with multiple UART connections. Do those
>>> exist, and are they common in configurations where out-of-band
>>> management is necessary (e.g. regulators, clocks)?
>> 
>> UARTs are usually point to point interfaces and not busses. So there is
>> no need to describe the interface.
> 
> I don't follow. You have a device which seems to require management
> kernel-side.

Yes, power on/off.

> Rather than describing the interface, you've described a
> fictitious relationship between the GPS device and the UART's DTR GPIO,
> and you’ve described a fictitious GPIO to hand to the UART driver.

Yes. Because the UART driver would generally expect a GPIO (if a GPIO driven
DTR hardware line is available on the connector).

> This
> is how you have linked the two in order to get the behaviour you want.

Exactly.

> 
> So it _is_ necessary to describe the interface. Rather than describing
> that interface at a high level you've chosen to hack together a set of
> fake relationships to work around the lack of ability to describe said
> interface.

The power control interface is a single GPIO from UART to the driver.
The other end is an interface from the driver to the pinmux of the OMAP
and the chip. So it is a “middleman”. But I think this is already clear.

> 
>> And I would speculate that in most cases they simply go to some
>> connector and therefore no connected chip that needs to be described
>> in the DT at all. Because it has a user-space driver (e.g. AT
>> commands) and no kernel driver.
> 
> In the case where no driver is necessary I agree that no description is
> necessary, though the description could be exposed in a helpful way to
> userspace to describe what’s attached to which UARTs.

That is usually done by configuring the gpsd process.

> 
> However, in this case you do have a kernel driver (even if basic), and
> it requires some knowledge of the relationship between the device and
> the UART in order to function.

Yes. And that is what we want to provide by the dtb file. That one should describe
the relationship.

> 
>> But we have no idea how such a solution could be implemented or tested.
> 
> I would disagree on that point, given I provided a high level
> description of how this could be implemented.

We do understand how you suggest the bindings should look like, but we have
no idea how that translates into code.

And we do not want to touch the general serial drivers, just the omap-serial
(because the solutions does not work without an UART behind a pinmux with a
GPIO with interrupt capabilities).

We simply have no experience modifying serial drivers (Linux is too big that anyone
can know all areas).

Our experience is limited to re-submitting the DTR-GPIO-control patch by Neil Brown
and making it read DT properties instead of board file.

So your approach needs a lot of help to really implement it. The question is who
will be doing it.

> 
>> If someone adds that to the serial drivers, we would be happy to use it,
>> but unless such a thing exists, I think our solution is quite simple and isolated
>> into this single driver and also uses existing standard interfaces (gpios, pinmux).
> 
> I would argue that this _abuses_ standard interfaces, as you have one
> device driver fiddling with resources logically owned by another.
> 
>>>> The reason to solve it that way is that we did not want to have a direct link
>>>> between this driver and any serial drivers or other mechanisms how drivers
>>>> can detect that their serial port (/dev/tty*) is opened.
>>>> 
>>>> It is used to power down the w2sg GPS chip if no user space process is
>>>> accessing its serial port (or de-asserts DTR through tcsetattr/ioctl).
>>>> 
>>>>> 
>>>>>> +
>>>>>> +                pinctrl-names = "default", "monitor";
>>>>>> +                pinctrl-0 = <&uart2_pins>;
>>>>>> +                pinctrl-1 = <&uart2_rx_irq_pins>;
>>>>>> +
>>>>>> +                interrupt-parent = <&gpio5>;
>>>>>> +                interrupts = <19 IRQ_TYPE_EDGE_FALLING>;  /* GPIO_147: RX - trigger on arrival of start bit */
>>>>> 
>>>>> While interrupts is a standard property, please describe above how many
>>>>> you expect and what their logical function is.
>>>>> 
>>>>> The only part I'm confused about is how the link to the UART is
>>>>> described. I assume I'm just ignorant of some existing pattern.
>>>> 
>>>> The serial link itself is not described at all because it is assumed to be a „must have“.
>>> 
>>> Huh? So it's a "must have" that you "don't have" in the DT?
>> 
>> Yes. The DT does not describe everything. Only those things that need
>> a kernel driver. And UARTs usually have user-space drivers (e.g. gpsd,
>> gsmd, pppd) and ioctl/tcsetattr.
> 
> The DT should describe the static portions of the hardware. Typically we
> only have devices with kernelspace drivers described simply because
> that's the way people have built DTs. Whether or not you have a
> kernelspace driver can change over time, the organisation of the
> hardware cannot.

So far none of the DT I have seen describe what is connected to the UART.
Even for boards which use HCI to communicate with a Bluetooth module.

> 
>>> I think that the relationship is being described incorrectly in the DT,
>>> and I think that there is a more general problem that needs to be
>>> addressed in order to make this case work.
>>> 
>>>> The driver only needs to monitor the RX line and needs to switch it between UART and
>>>> GPIO/IRQ mode. So this monitoring switch is described (with two different pinctrl states).
>>> 
>>> While this particular driver only needs that at this point in time,
>>> that's not necessarily true of drivers for similar devices, nor is it
>>> necessarily true if we need to add additional features to this driver.
>> 
>> Which features are you thinking of to add to this driver? And do
>> similar devices exist at all? Since we have not found any, we have
>> declared it as a "misc“ driver.
> 
> I don't have any particular feature in mind.
> 
> I am not immediately aware of other serial devices which require
> out-of-band management in the same way, though we have a vaguely similar
> case with SDIO devices which must be powered up before they appear on
> the bus.

AFAIK, they are usually descibed by a regulator that is enabled/disabled by
the SDIO driver. And the regulator is usually defined as a gpio-regulator to
drive an GPIO.

And I think the SDIO driver has a reset-gpio (which can also be interpreted
as a disable/power-down).

So such interface drivers simply expect that they can power-control dependent
devices through a regulator or a simple gpio.

> In that case I believe the intent is to describe them in the DT
> under the bus.

Or would it be ok to allow a regulator property for the serial interface? Then
our driver would become a w2sg0004-regulator driver similar to a gpio-regulator
but with a state machine that knows how to pulse the gpio until power is applied
to the GPS receiver internals.

> 
>>> Describing the relationship leaves a lot more freedom to improve things
>>> without having to update every DTB.
>>> 
>>>> We know that it is a little tricky to control this chip correctly - and we think this solution
>>>> is the most general (no direct dependency on the serial line, and just to pinmux states
>>>> and an interrupt).
>>> 
>>> I think that the rough approach I sketched out above is more general,
>>> and I think that you must describe the relationship with the serial
>>> line.
>>> 
>>> It's not clear to me whether the interrupt you describe is attached to
>>> the GPS, or if this is logically part of the UART.
>> 
>> The interrupt is needed to simulate the glue logic connected between
>> UART and GPS.
>> 
>> The output signal comes from the GPS module and goes to some pad
>> of the OMAP3 SoC. This pad can be either multiplexed into the UART RX
>> input or into a GPIO bank of the OMAP. That GPIO controller can generate
>> the interrupt on incoming data (when none is expected).
>> 
>> Therefore it is a GPS-generated interrupt and has nothing to do with
>> the UART.
> 
> Ok. When does the GPS device raise this interrupt?

If the driver assumes the GPS receiver is turned off, it disconnedts the RX
wire from the UART to a GPIO by using two different pinmux states.

If the GPIO raises an interrupt, the driver knows that the GPS module is not
turned off, because the driver has lost knowledge about its state. This is not
an UART related function but OMAP pinmux and GPIO.

I am not sure if that could even be implemented at all by a UART dependent
driver (since the UART is shut down and does not interrupt).

> 
> Thanks,
> Mark.

Thanks as well - it is a fruitful discussion (even if it becomes lengthy because
I might have repeated a lot of things that are already clear. Please accept an apology).

I think we just disagree about implementing a version that “works” in a quite specific
setup (there are necessary assumptions about OMAP pinmux and omap-serial) with
existing APIs versus a general one that might need a lot of changes outside the driver
and introduce new APIs.

BR,
Nikolaus


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver
  2014-10-16 20:26 [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver Marek Belisko
  2014-10-16 20:26 ` [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps Marek Belisko
  2014-10-19 19:51 ` [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver Arnd Bergmann
@ 2014-10-21 10:49 ` Pavel Machek
  2014-10-22 22:35   ` Dr. H. Nikolaus Schaller
  2 siblings, 1 reply; 15+ messages in thread
From: Pavel Machek @ 2014-10-21 10:49 UTC (permalink / raw)
  To: Marek Belisko
  Cc: arnd, gregkh, robh+dt, pawel.moll, mark.rutland, ijc+devicetree,
	galak, grant.likely, devicetree, linux-kernel, NeilBrown,
	H. Nikolaus Schaller

Hi!

> --- a/drivers/misc/Kconfig
> +++ b/drivers/misc/Kconfig
> @@ -515,6 +515,16 @@ config VEXPRESS_SYSCFG
>  	  bus. System Configuration interface is one of the possible means
>  	  of generating transactions on this bus.
>  
> +config W2SG0004
> +	tristate "W2SG0004 on/off control"

                           ~~ insert GPS here.
And make it bool if it can't be a module.

> +	depends on GPIOLIB
> +	help
> +	  Enable on/off control of W2SG0004 GPS using a virtual GPIO.
> +	  The virtual GPIO can be connected to a DTR line of a serial
> +	  interface to allow powering up if the /dev/tty$n is opened.
> +	  It also provides a rfkill gps node to control the LNA power.
> +	  NOTE: can't currently be compiled as module, so please choose Y.
> +

> +++ b/drivers/misc/w2sg0004.c
> @@ -0,0 +1,512 @@
> +/*
> + * w2sg0004.c
> + * Virtual GPIO of controlling the w2sg0004 GPS receiver.
> + *
> + * Copyright (C) 2011 Neil Brown <neil@brown.name>
> + *
> + * This receiver has an ON/OFF pin which must be toggled to
> + * turn the device 'on' or 'off'.  A high->low->high toggle
> + * will switch the device on if it is off, and off if it is on.
> + * It is not possible to directly detect the state of the device.
> + * However when it is on it will send characters on a UART line
> + * regularly.
> + * On the OMAP3, the UART line can also be programmed as a GPIO
> + * on which we can receive interrupts.
> + * So when we want the device to be 'off' we can reprogram
> + * the line, toggle the ON/OFF pin and hope that it is off.
> + * However if an interrupt arrives we know that it is really on
> + * and can toggle again.
> + *
> + * To enable receiving on/off requests we create a gpio_chip
> + * with a single 'output' GPIO.  When it is low, the
> + * GPS is turned off.  When it is high, it is turned on.
> + * This can be configured as the DTR GPIO on the UART which
> + * connects the GPS.  Then whenever the tty is open, the GPS
> + * will be switched on, and whenever it is closed, the GPS will
> + * be switched off.
> + *
> + * In addition we register as a rfkill client so that we can
> + * control the LNA power.
> + *
> + */

GPL?

> +/*
> + * There seems to restrictions on how quickly we can toggle the
> + * on/off line.  data sheets says "two rtc ticks", whatever that means.

"seems to"? data -> Data?

> +enum w2sg_state {
> +	W2SG_IDLE,	/* is not changing state */
> +	W2SG_PULSE,	/* activate on/off impulse */
> +	W2SG_NOPULSE	/* desctivate on/off impulse */
> +};

deactivate.

> +
> +struct gpio_w2sg {
> +	struct		rfkill *rf_kill;
> +	struct		regulator *lna_regulator;
> +	int		lna_blocked;	/* rfkill block gps active */
> +	int		lna_is_off;	/* LNA is currently off */
> +	int		is_on;		/* current state (0/1) */
> +	unsigned long	last_toggle;
> +	unsigned long	backoff;	/* time to wait since last_toggle */
> +	int		on_off_gpio;
> +	int		rx_irq;
> +
> +	struct pinctrl *p;
> +	struct pinctrl_state *default_state;	/* should be UART mode */
> +	struct pinctrl_state *monitor_state;	/* monitor RX as GPIO */
> +	enum w2sg_state	state;
> +	int		requested;	/* requested state (0/1) */
> +	int		suspended;
> +	int		rx_redirected;
> +	spinlock_t	lock;
> +#ifdef CONFIG_GPIOLIB
> +	struct gpio_chip gpio;
> +	const char	*gpio_name[1];
> +#endif

Depends on gpiolib, why ifdef?

Array of names?


> +	rf_kill = rfkill_alloc("GPS", &pdev->dev, RFKILL_TYPE_GPS,
> +				&gpio_w2sg0004_rfkill_ops, gw2sg);

Actually, is rfkill interface right one on GPS? GPS is not supposed to
transmit...

> +	int	gpio_base;	/* (not used by DT) - defines the  gpio.base */


Is non-device tree path still usefull?

									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver
  2014-10-21 10:49 ` Pavel Machek
@ 2014-10-22 22:35   ` Dr. H. Nikolaus Schaller
  0 siblings, 0 replies; 15+ messages in thread
From: Dr. H. Nikolaus Schaller @ 2014-10-22 22:35 UTC (permalink / raw)
  To: Pavel Machek
  Cc: Marek Belisko, arnd, gregkh, robh+dt, pawel.moll, mark.rutland,
	ijc+devicetree, galak, grant.likely, devicetree, linux-kernel,
	NeilBrown

Hi,

Am 21.10.2014 um 12:49 schrieb Pavel Machek <pavel@ucw.cz>:

> Hi!
> 
>> --- a/drivers/misc/Kconfig
>> +++ b/drivers/misc/Kconfig
>> @@ -515,6 +515,16 @@ config VEXPRESS_SYSCFG
>> 	  bus. System Configuration interface is one of the possible means
>> 	  of generating transactions on this bus.
>> 
>> +config W2SG0004
>> +	tristate "W2SG0004 on/off control"
> 
>                           ~~ insert GPS here.

ok!

> And make it bool if it can’t be a module.
> 
>> +	depends on GPIOLIB
>> +	help
>> +	  Enable on/off control of W2SG0004 GPS using a virtual GPIO.
>> +	  The virtual GPIO can be connected to a DTR line of a serial
>> +	  interface to allow powering up if the /dev/tty$n is opened.
>> +	  It also provides a rfkill gps node to control the LNA power.
>> +	  NOTE: can’t currently be compiled as module, so please choose Y.

Sorry, this is a bug in the description that was coorect before using the pinctrl
framework. The driver has been tested to work compiled as a module.

>> +
> 
>> +++ b/drivers/misc/w2sg0004.c
>> @@ -0,0 +1,512 @@
>> +/*
>> + * w2sg0004.c
>> + * Virtual GPIO of controlling the w2sg0004 GPS receiver.
>> + *
>> + * Copyright (C) 2011 Neil Brown <neil@brown.name>
>> + *
>> + * This receiver has an ON/OFF pin which must be toggled to
>> + * turn the device 'on' or 'off'.  A high->low->high toggle
>> + * will switch the device on if it is off, and off if it is on.
>> + * It is not possible to directly detect the state of the device.
>> + * However when it is on it will send characters on a UART line
>> + * regularly.
>> + * On the OMAP3, the UART line can also be programmed as a GPIO
>> + * on which we can receive interrupts.
>> + * So when we want the device to be 'off' we can reprogram
>> + * the line, toggle the ON/OFF pin and hope that it is off.
>> + * However if an interrupt arrives we know that it is really on
>> + * and can toggle again.
>> + *
>> + * To enable receiving on/off requests we create a gpio_chip
>> + * with a single 'output' GPIO.  When it is low, the
>> + * GPS is turned off.  When it is high, it is turned on.
>> + * This can be configured as the DTR GPIO on the UART which
>> + * connects the GPS.  Then whenever the tty is open, the GPS
>> + * will be switched on, and whenever it is closed, the GPS will
>> + * be switched off.
>> + *
>> + * In addition we register as a rfkill client so that we can
>> + * control the LNA power.
>> + *
>> + */
> 
> GPL?
> 
>> +/*
>> + * There seems to restrictions on how quickly we can toggle the
>> + * on/off line.  data sheets says "two rtc ticks", whatever that means.
> 
> “seems to"?

-> “seems to be”

> data -> Data?

ok!

> 
>> +enum w2sg_state {
>> +	W2SG_IDLE,	/* is not changing state */
>> +	W2SG_PULSE,	/* activate on/off impulse */
>> +	W2SG_NOPULSE	/* desctivate on/off impulse */
>> +};
> 
> deactivate.

ok.

> 
>> +
>> +struct gpio_w2sg {
>> +	struct		rfkill *rf_kill;
>> +	struct		regulator *lna_regulator;
>> +	int		lna_blocked;	/* rfkill block gps active */
>> +	int		lna_is_off;	/* LNA is currently off */
>> +	int		is_on;		/* current state (0/1) */
>> +	unsigned long	last_toggle;
>> +	unsigned long	backoff;	/* time to wait since last_toggle */
>> +	int		on_off_gpio;
>> +	int		rx_irq;
>> +
>> +	struct pinctrl *p;
>> +	struct pinctrl_state *default_state;	/* should be UART mode */
>> +	struct pinctrl_state *monitor_state;	/* monitor RX as GPIO */
>> +	enum w2sg_state	state;
>> +	int		requested;	/* requested state (0/1) */
>> +	int		suspended;
>> +	int		rx_redirected;
>> +	spinlock_t	lock;
>> +#ifdef CONFIG_GPIOLIB
>> +	struct gpio_chip gpio;
>> +	const char	*gpio_name[1];
>> +#endif
> 
> Depends on gpiolib, why ifdef?

historic…

> 
> Array of names?

Yes, that is how it should be defined (usually gpio_chips have more than 1 entry).
Otherwise we have to provide some & operators when passing to the gpiolib functions
where all other similar drivers simply use the array name. So it is a matter of taste
where we need to introduce confusion. AFAIK the compiler generated binary code
should be identical.

> 
> 
>> +	rf_kill = rfkill_alloc("GPS", &pdev->dev, RFKILL_TYPE_GPS,
>> +				&gpio_w2sg0004_rfkill_ops, gw2sg);
> 
> Actually, is rfkill interface right one on GPS? GPS is not supposed to
> transmit…

We have not invented RFKILL_TYPE_GPS. We just use it.
And AFAIK no system that transmits GPS runs Linux.

The function here is to stop it from receiving (and allows to power down
any RF amplifiers and active antennas)…

> 
>> +	int	gpio_base;	/* (not used by DT) - defines the  gpio.base */
> 
> 
> Is non-device tree path still usefull?

Probably not. At least we don’t need it in our projects any more.

> 
> 									Pavel

BR,
Nikolaus



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps
  2014-10-20 17:26             ` Dr. H. Nikolaus Schaller
@ 2014-10-24  9:32               ` Dr. H. Nikolaus Schaller
  2014-10-27  9:31                 ` Pavel Machek
  2014-11-02 10:15                 ` Dr. H. Nikolaus Schaller
  0 siblings, 2 replies; 15+ messages in thread
From: Dr. H. Nikolaus Schaller @ 2014-10-24  9:32 UTC (permalink / raw)
  To: Mark Rutland
  Cc: Marek Belisko, arnd, gregkh, robh+dt, Pawel Moll, ijc+devicetree,
	galak, grant.likely, devicetree, linux-kernel, NeilBrown,
	Nikolaus Schaller, linux-serial


Am 20.10.2014 um 19:26 schrieb Dr. H. Nikolaus Schaller <hns@goldelico.com>:

> Hi,
> 
> Am 20.10.2014 um 11:35 schrieb Mark Rutland <mark.rutland@arm.com>:
> 
>> Hi,
>> 
>> On Fri, Oct 17, 2014 at 08:55:50PM +0100, Dr. H. Nikolaus Schaller wrote:
>>> 
>>> Am 17.10.2014 um 13:00 schrieb Mark Rutland <mark.rutland@arm.com>:
>>> 
>>>> On Fri, Oct 17, 2014 at 11:16:42AM +0100, Dr. H. Nikolaus Schaller wrote:
>>>>> 
>>>>> Am 17.10.2014 um 11:37 schrieb Mark Rutland <mark.rutland@arm.com>:
>>>>> 
>>>>>> On Thu, Oct 16, 2014 at 09:26:23PM +0100, Marek Belisko wrote:
>>>>>>> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
>>>>>>> Signed-off-by: Marek Belisko <marek@goldelico.com>
>>>>>>> ---
>>>>>>> .../devicetree/bindings/misc/wi2wi,w2sg0004.txt    | 44 ++++++++++++++++++++++
>>>>>>> 1 file changed, 44 insertions(+)
>>>>>>> create mode 100644 Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>>>>> 
>>>>>>> diff --git a/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>>>>> new file mode 100644
>>>>>>> index 0000000..e144441
>>>>>>> --- /dev/null
>>>>>>> +++ b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>>>>> @@ -0,0 +1,44 @@
>>>>>>> +Wi2Wi GPS module connected through UART
>>>>>>> +
>>>>>>> +Required properties:
>>>>>>> +- compatible: wi2wi,w2sg0004 or wi2wi,w2sg0084
>>>>>>> +- pinctrl: specify two states (default and monitor). One is the default (UART) mode
>>>>>>> +  and the other is for monitoring the RX line by an interrupt
>>>>>>> +- on-off-gpio: the GPIO that controls the module's on-off toggle input
>>>>>>> +
>>>>>>> +Optional properties:
>>>>>>> +- lna-suppy: an (optional) LNA regulator that is enabled together with the GPS receiver
>>>>>>> +
>>>>>>> +example:
>>>>>>> +
>>>>>>> +        gps_receiver: w2sg0004 {
>>>>>>> +                compatible = "wi2wi,w2sg0004";
>>>>>> 
>>>>>> I couldn't spot "wi2wi" in
>>>>>> Documentation/devicetree/bindings/vendor-prefixes.txt (in mainline).
>>>>>> 
>>>>>> Could you please add it?
>>>>>> 
>>>>>>> +                gpio-controller;
>>>>>>> +                #gpio-cells = <2>;
>>>>>> 
>>>>>> As far as I can see, these properties aren't necessary. This only
>>>>>> consumes a GPIO, it doesn't provide any.
>>>>> 
>>>>> Well, it provides one GPIO. Sort of a "virtual“ GPIO. It is needed so that 
>>>>> it can be wired up to the DTR gpio of the UART driver (unfortunately this
>>>>> patch was reverted some months ago from mainline and we will reintroduce
>>>>> it soon).
>>>> 
>>>> If this GPIO doesn't really exist, then it's a Linux internal
>>>> implementation detail rather than a description of the hardware, and
>>>> doesn't really belong in the DT.
>>> 
>>> Hm. 
>>> 
>>> Let’s describe it differently.
>>> 
>>> I can see the Linux driver module as a simple software simulation for a
>>> piece of hardware that could have been connected between the UART
>>> and the GPS chip.
>>> 
>>> Basically it is a pulse-generator and a flip-flop to detect data flow on the RX
>>> wire. This could be implemented by an external FPGA or uController. Or as
>>> it is done on our board for saving hardware by a mix of the main CPU hardware
>>> (Pinmux + GPIO + IRQ) and a kernel driver.
>>> 
>>> The best of course would have been if the w2sg0004 would have a physical
>>> „enable“ GPIO (instead of that on-off control input).
>>> 
>>> Then we would hook up that enable to some physical GPIO of the CPU
>>> and simply refer to it as e.g. <&gpio4 12>. And would not even need a driver
>>> for it (unless we want to make rfkill gps work).
>>> 
>>> Therefore the driver we suggest provides an additional gpio controller with a
>>> single GPIO so that we can write <&w2sg 0> to refer to this virtual gpio.
>>> 
>>> So in fact we try to wrap a non-optimal chip design by the driver and make it
>>> appear as a standard GPIO interface to the DT and user space and whoever
>>> needs simply to enable/disable the GPS chip.
>> 
>> The fact remains that this does not accurately represent the hardware,
>> and is unnecessarily strongly tied to a particular UART design (where
>> the DTR line is a separate UART).
> 
> I don’t get that it is tied to a particular UART design (except that our DTR
> patch affects to omap-serial only).
> 
> Let’s describe the facts:
> 
> The chip has this interface:
> 
> GPS-TX (output)
> GPS-RX (input)
> ON/OFF (input) - to toggle the power state of the chip
> 
> They are connected to an OMAP3 UART2 as
> 
> UART2-TX > GPS-RX
> UART2-RX <- GPS-TX
> GPIO145 -> ON/OFF
> 
> That’s it.
> 
> If the chip (or any other serial GPS receiver like a Garmin) were connected
> through real RS232 we would have
> 
> UART2-TX -> level shifter -> cable -> level shifter -> GPS-RX
> UART2-RX <- level shifter <- cable <- level shifter <- GPS-TX
> DTR-GPIO -> level shifter (DTR line) -> cable -> level shifter -> power-management logic -> ON/OFF
> 
> But because it is connected directly, we need to implement the power-management
> logic between the DTR-GPIO and GPIO145:
> 
> DTR-GPIO -> driver -> GPIO145 -> ON/OFF
> 
> To correctly determine the state we need to tap the RX signal before
> it enters the UART2-RX (it is pinmuxed with GPIO147):
> 
> UART2-RX <——+
>                               OMAP3 pinmux <- OMAP3 pad <- GPS-TX
>   GPIO147 <——+
> 
> So if we describe the driver as a box we have
> 
> inputs from user space:
> * rfkill for GPS
> * a control that is activated by opening /dev/tty
> 
> outputs to system:
> * a control to switch the pinmux between RX and GPIO (interrupt)
> * a control to external hardware
> 
>> 
>>>> It sounds like what we actually need is the ability to describe devices
>>>> attached to UARTs.
>>> 
>>> Hm. The purpose of the driver is power control of the chip. Not the serial
>>> interface.
>> 
>> I'm not sure I follow your point. By describing the device as attached
>> to the UART, the kernel can figure out that when said UART is accessed
>> the attached device needs to be on (and must be poked as necessary).
> 
> Why do we need to describe this? It is all about controlling the GPIO145,
> GPIO147 and pinmux. But not RX or TX.
> 
> So the driver does not need to know anything about UARTs (and if we
> connect it to UART3 we only have to specify different GPIOs).
> 
> Only our board specific dtb connects the UART to the “virtual” GPIO
> provided by the driver.
> 
> This also brings up a minor problem: on OMAP3 some UART RX lines can
> be pinmuxed with different GPIOs. So putting the driver under some UART2
> node doesn’t uniquely define the GPIO number to monitor RX and might
> become a mess.
> 
>> 
>> The power management logic for the device can stay in the device driver,
>> and the power management logic for the UART can stay in the UART driver.
> 
> Here I can’t follow you. What power management does an UART driver provide?
> 
>> Neither would need to know about each other's internal details
>> (e.g.GPIOs, for DTR or otherwise).
> 
> Yes, that is our goal and in our solution they don’t need to make assumptions
> about each other. We just define in the DT: this GTA04 board has UART2-DTR
> connected to the W2SG-GPIO# - instead of OMAP3-GPIO145.
> 
> In our solution the UART driver knows that there could be a DTR GPIO (similar to
> a RTS GPIO which is already provided by the omap-serial RS484 bindings). Which
> could be connected to some GPIO which drives the real DTR wire of a RS232 level
> shifter.
> 
>> 
>>>> Then you could have a mechanism whereby the UART
>>>> driver can notify the other device driver regarding events (e.g. the
>>>> UART being opened for access), or the other driver could claim ownership
>>>> of the UART and expose its own interface to userspace.
>>>> 
>>>> That would be independent of the particular UART or other device, and
>>>> the only description necessary in the DT would be an accurate
>>>> representation of the way the hardware is wired.
>>>> 
>>>> There are a few ways that could be done, but I suspect the simplest is
>>>> to just have the device as a sub-node of the UART, like we do for SPI or
>>>> I2C buses:
>>>> 
>>>> 	serial@f00 {
>>>> 		compatible = "vendor,uart";
>>>> 		reg = <0xf00 0x100>;
>>>> 		...
>>>> 
>>>> 		gps {
>>>> 			compatible = "wi2wi,w2sg0004";
>>>> 			...
>>>> 		};
>>>> 	};
>>>> 
>>>> That wouldn't work for devices with multiple UART connections. Do those
>>>> exist, and are they common in configurations where out-of-band
>>>> management is necessary (e.g. regulators, clocks)?
>>> 
>>> UARTs are usually point to point interfaces and not busses. So there is
>>> no need to describe the interface.
>> 
>> I don't follow. You have a device which seems to require management
>> kernel-side.
> 
> Yes, power on/off.
> 
>> Rather than describing the interface, you've described a
>> fictitious relationship between the GPS device and the UART's DTR GPIO,
>> and you’ve described a fictitious GPIO to hand to the UART driver.
> 
> Yes. Because the UART driver would generally expect a GPIO (if a GPIO driven
> DTR hardware line is available on the connector).
> 
>> This
>> is how you have linked the two in order to get the behaviour you want.
> 
> Exactly.
> 
>> 
>> So it _is_ necessary to describe the interface. Rather than describing
>> that interface at a high level you've chosen to hack together a set of
>> fake relationships to work around the lack of ability to describe said
>> interface.
> 
> The power control interface is a single GPIO from UART to the driver.
> The other end is an interface from the driver to the pinmux of the OMAP
> and the chip. So it is a “middleman”. But I think this is already clear.
> 
>> 
>>> And I would speculate that in most cases they simply go to some
>>> connector and therefore no connected chip that needs to be described
>>> in the DT at all. Because it has a user-space driver (e.g. AT
>>> commands) and no kernel driver.
>> 
>> In the case where no driver is necessary I agree that no description is
>> necessary, though the description could be exposed in a helpful way to
>> userspace to describe what’s attached to which UARTs.
> 
> That is usually done by configuring the gpsd process.
> 
>> 
>> However, in this case you do have a kernel driver (even if basic), and
>> it requires some knowledge of the relationship between the device and
>> the UART in order to function.
> 
> Yes. And that is what we want to provide by the dtb file. That one should describe
> the relationship.
> 
>> 
>>> But we have no idea how such a solution could be implemented or tested.
>> 
>> I would disagree on that point, given I provided a high level
>> description of how this could be implemented.
> 
> We do understand how you suggest the bindings should look like, but we have
> no idea how that translates into code.
> 
> And we do not want to touch the general serial drivers, just the omap-serial
> (because the solutions does not work without an UART behind a pinmux with a
> GPIO with interrupt capabilities).
> 
> We simply have no experience modifying serial drivers (Linux is too big that anyone
> can know all areas).
> 
> Our experience is limited to re-submitting the DTR-GPIO-control patch by Neil Brown
> and making it read DT properties instead of board file.
> 
> So your approach needs a lot of help to really implement it. The question is who
> will be doing it.
> 
>> 
>>> If someone adds that to the serial drivers, we would be happy to use it,
>>> but unless such a thing exists, I think our solution is quite simple and isolated
>>> into this single driver and also uses existing standard interfaces (gpios, pinmux).
>> 
>> I would argue that this _abuses_ standard interfaces, as you have one
>> device driver fiddling with resources logically owned by another.
>> 
>>>>> The reason to solve it that way is that we did not want to have a direct link
>>>>> between this driver and any serial drivers or other mechanisms how drivers
>>>>> can detect that their serial port (/dev/tty*) is opened.
>>>>> 
>>>>> It is used to power down the w2sg GPS chip if no user space process is
>>>>> accessing its serial port (or de-asserts DTR through tcsetattr/ioctl).
>>>>> 
>>>>>> 
>>>>>>> +
>>>>>>> +                pinctrl-names = "default", "monitor";
>>>>>>> +                pinctrl-0 = <&uart2_pins>;
>>>>>>> +                pinctrl-1 = <&uart2_rx_irq_pins>;
>>>>>>> +
>>>>>>> +                interrupt-parent = <&gpio5>;
>>>>>>> +                interrupts = <19 IRQ_TYPE_EDGE_FALLING>;  /* GPIO_147: RX - trigger on arrival of start bit */
>>>>>> 
>>>>>> While interrupts is a standard property, please describe above how many
>>>>>> you expect and what their logical function is.
>>>>>> 
>>>>>> The only part I'm confused about is how the link to the UART is
>>>>>> described. I assume I'm just ignorant of some existing pattern.
>>>>> 
>>>>> The serial link itself is not described at all because it is assumed to be a „must have“.
>>>> 
>>>> Huh? So it's a "must have" that you "don't have" in the DT?
>>> 
>>> Yes. The DT does not describe everything. Only those things that need
>>> a kernel driver. And UARTs usually have user-space drivers (e.g. gpsd,
>>> gsmd, pppd) and ioctl/tcsetattr.
>> 
>> The DT should describe the static portions of the hardware. Typically we
>> only have devices with kernelspace drivers described simply because
>> that's the way people have built DTs. Whether or not you have a
>> kernelspace driver can change over time, the organisation of the
>> hardware cannot.
> 
> So far none of the DT I have seen describe what is connected to the UART.
> Even for boards which use HCI to communicate with a Bluetooth module.
> 
>> 
>>>> I think that the relationship is being described incorrectly in the DT,
>>>> and I think that there is a more general problem that needs to be
>>>> addressed in order to make this case work.
>>>> 
>>>>> The driver only needs to monitor the RX line and needs to switch it between UART and
>>>>> GPIO/IRQ mode. So this monitoring switch is described (with two different pinctrl states).
>>>> 
>>>> While this particular driver only needs that at this point in time,
>>>> that's not necessarily true of drivers for similar devices, nor is it
>>>> necessarily true if we need to add additional features to this driver.
>>> 
>>> Which features are you thinking of to add to this driver? And do
>>> similar devices exist at all? Since we have not found any, we have
>>> declared it as a "misc“ driver.
>> 
>> I don't have any particular feature in mind.
>> 
>> I am not immediately aware of other serial devices which require
>> out-of-band management in the same way, though we have a vaguely similar
>> case with SDIO devices which must be powered up before they appear on
>> the bus.
> 
> AFAIK, they are usually descibed by a regulator that is enabled/disabled by
> the SDIO driver. And the regulator is usually defined as a gpio-regulator to
> drive an GPIO.
> 
> And I think the SDIO driver has a reset-gpio (which can also be interpreted
> as a disable/power-down).
> 
> So such interface drivers simply expect that they can power-control dependent
> devices through a regulator or a simple gpio.
> 
>> In that case I believe the intent is to describe them in the DT
>> under the bus.
> 
> Or would it be ok to allow a regulator property for the serial interface? Then
> our driver would become a w2sg0004-regulator driver similar to a gpio-regulator
> but with a state machine that knows how to pulse the gpio until power is applied
> to the GPS receiver internals.
> 
>> 
>>>> Describing the relationship leaves a lot more freedom to improve things
>>>> without having to update every DTB.
>>>> 
>>>>> We know that it is a little tricky to control this chip correctly - and we think this solution
>>>>> is the most general (no direct dependency on the serial line, and just to pinmux states
>>>>> and an interrupt).
>>>> 
>>>> I think that the rough approach I sketched out above is more general,
>>>> and I think that you must describe the relationship with the serial
>>>> line.
>>>> 
>>>> It's not clear to me whether the interrupt you describe is attached to
>>>> the GPS, or if this is logically part of the UART.
>>> 
>>> The interrupt is needed to simulate the glue logic connected between
>>> UART and GPS.
>>> 
>>> The output signal comes from the GPS module and goes to some pad
>>> of the OMAP3 SoC. This pad can be either multiplexed into the UART RX
>>> input or into a GPIO bank of the OMAP. That GPIO controller can generate
>>> the interrupt on incoming data (when none is expected).
>>> 
>>> Therefore it is a GPS-generated interrupt and has nothing to do with
>>> the UART.
>> 
>> Ok. When does the GPS device raise this interrupt?
> 
> If the driver assumes the GPS receiver is turned off, it disconnedts the RX
> wire from the UART to a GPIO by using two different pinmux states.
> 
> If the GPIO raises an interrupt, the driver knows that the GPS module is not
> turned off, because the driver has lost knowledge about its state. This is not
> an UART related function but OMAP pinmux and GPIO.
> 
> I am not sure if that could even be implemented at all by a UART dependent
> driver (since the UART is shut down and does not interrupt).
> 
>> 
>> Thanks,
>> Mark.
> 
> Thanks as well - it is a fruitful discussion (even if it becomes lengthy because
> I might have repeated a lot of things that are already clear. Please accept an apology).
> 
> I think we just disagree about implementing a version that “works” in a quite specific
> setup (there are necessary assumptions about OMAP pinmux and omap-serial) with
> existing APIs versus a general one that might need a lot of changes outside the driver
> and introduce new APIs.
> 
> BR,
> Nikolaus
> 

After re-thinking what we have discussed so far (and considering other recommendations
I have received off-list) I currently see these options/questions:

* who could modify the serial drivers and APIs so that this driver can become
  a subnode (“bus client”) of an UART? How long does it take to become available?

* would it be an option to rename it to “gpio-w2sg0004” to better describe that it
  provides a (virtual) gpio?

* would it be an option to simply rename the driver to “w2sg0004-power”
  to make clear that it is not at all related to the UART data communication
  channel but only controlling the power of the w2sg0004 chip?

* both?

* or would it be acceptable if this is a regulator driver that controls the LDO
  inside this chip? I.e. describe it as a “w2sg0004-regulator”?

 This would be very similar to the function of a “gpio-regulator”: convert
 “regulator state” into a gpio state. But here convert “on/off” into some impulses.

 And to resolve uncertainty about the real LDO state it would be able to monitor
 some feedback interrupt and handle an (optional) pinmux toggle.

 This would mean that we do not even need to mention anything about UARTs
 in the driver bindings.

 Rather it would just say: the driver can monitor a (gpio) interrupt line to
 know if the attached device is active (when it is not expected, e.g. after boot).

 Since this monitor gpio is sometimes multiplexed with other features of the SoC,
 the driver can also switch between two pinctrl states (default and monitor).

BR,
Nikolaus


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps
  2014-10-24  9:32               ` Dr. H. Nikolaus Schaller
@ 2014-10-27  9:31                 ` Pavel Machek
  2014-11-02 10:15                 ` Dr. H. Nikolaus Schaller
  1 sibling, 0 replies; 15+ messages in thread
From: Pavel Machek @ 2014-10-27  9:31 UTC (permalink / raw)
  To: Dr. H. Nikolaus Schaller
  Cc: Mark Rutland, Marek Belisko, arnd, gregkh, robh+dt, Pawel Moll,
	ijc+devicetree, galak, grant.likely, devicetree, linux-kernel,
	NeilBrown, linux-serial


> > I think we just disagree about implementing a version that “works” in a quite specific
> > setup (there are necessary assumptions about OMAP pinmux and omap-serial) with
> > existing APIs versus a general one that might need a lot of changes outside the driver
> > and introduce new APIs.
> 
> After re-thinking what we have discussed so far (and considering other recommendations
> I have received off-list) I currently see these options/questions:
> 
> * who could modify the serial drivers and APIs so that this driver can become
>   a subnode (“bus client”) of an UART? How long does it take to
> become available?

I believe that is called "tty line discipline".

> * would it be an option to simply rename the driver to “w2sg0004-power”
>   to make clear that it is not at all related to the UART data communication
>   channel but only controlling the power of the w2sg0004 chip?

Sounds sane.
									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps
  2014-10-24  9:32               ` Dr. H. Nikolaus Schaller
  2014-10-27  9:31                 ` Pavel Machek
@ 2014-11-02 10:15                 ` Dr. H. Nikolaus Schaller
  1 sibling, 0 replies; 15+ messages in thread
From: Dr. H. Nikolaus Schaller @ 2014-11-02 10:15 UTC (permalink / raw)
  To: Mark Rutland
  Cc: Marek Belisko, arnd, gregkh, robh+dt, Pawel Moll, ijc+devicetree,
	galak, grant.likely, devicetree, linux-kernel, NeilBrown,
	linux-serial

ping (questions for directions at the end of the mail).

Am 24.10.2014 um 11:32 schrieb Dr. H. Nikolaus Schaller <hns@goldelico.com>:

> 
> Am 20.10.2014 um 19:26 schrieb Dr. H. Nikolaus Schaller <hns@goldelico.com>:
> 
>> Hi,
>> 
>> Am 20.10.2014 um 11:35 schrieb Mark Rutland <mark.rutland@arm.com>:
>> 
>>> Hi,
>>> 
>>> On Fri, Oct 17, 2014 at 08:55:50PM +0100, Dr. H. Nikolaus Schaller wrote:
>>>> 
>>>> Am 17.10.2014 um 13:00 schrieb Mark Rutland <mark.rutland@arm.com>:
>>>> 
>>>>> On Fri, Oct 17, 2014 at 11:16:42AM +0100, Dr. H. Nikolaus Schaller wrote:
>>>>>> 
>>>>>> Am 17.10.2014 um 11:37 schrieb Mark Rutland <mark.rutland@arm.com>:
>>>>>> 
>>>>>>> On Thu, Oct 16, 2014 at 09:26:23PM +0100, Marek Belisko wrote:
>>>>>>>> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
>>>>>>>> Signed-off-by: Marek Belisko <marek@goldelico.com>
>>>>>>>> ---
>>>>>>>> .../devicetree/bindings/misc/wi2wi,w2sg0004.txt    | 44 ++++++++++++++++++++++
>>>>>>>> 1 file changed, 44 insertions(+)
>>>>>>>> create mode 100644 Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>>>>>> 
>>>>>>>> diff --git a/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>>>>>> new file mode 100644
>>>>>>>> index 0000000..e144441
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/Documentation/devicetree/bindings/misc/wi2wi,w2sg0004.txt
>>>>>>>> @@ -0,0 +1,44 @@
>>>>>>>> +Wi2Wi GPS module connected through UART
>>>>>>>> +
>>>>>>>> +Required properties:
>>>>>>>> +- compatible: wi2wi,w2sg0004 or wi2wi,w2sg0084
>>>>>>>> +- pinctrl: specify two states (default and monitor). One is the default (UART) mode
>>>>>>>> +  and the other is for monitoring the RX line by an interrupt
>>>>>>>> +- on-off-gpio: the GPIO that controls the module's on-off toggle input
>>>>>>>> +
>>>>>>>> +Optional properties:
>>>>>>>> +- lna-suppy: an (optional) LNA regulator that is enabled together with the GPS receiver
>>>>>>>> +
>>>>>>>> +example:
>>>>>>>> +
>>>>>>>> +        gps_receiver: w2sg0004 {
>>>>>>>> +                compatible = "wi2wi,w2sg0004";
>>>>>>> 
>>>>>>> I couldn't spot "wi2wi" in
>>>>>>> Documentation/devicetree/bindings/vendor-prefixes.txt (in mainline).
>>>>>>> 
>>>>>>> Could you please add it?
>>>>>>> 
>>>>>>>> +                gpio-controller;
>>>>>>>> +                #gpio-cells = <2>;
>>>>>>> 
>>>>>>> As far as I can see, these properties aren't necessary. This only
>>>>>>> consumes a GPIO, it doesn't provide any.
>>>>>> 
>>>>>> Well, it provides one GPIO. Sort of a "virtual“ GPIO. It is needed so that 
>>>>>> it can be wired up to the DTR gpio of the UART driver (unfortunately this
>>>>>> patch was reverted some months ago from mainline and we will reintroduce
>>>>>> it soon).
>>>>> 
>>>>> If this GPIO doesn't really exist, then it's a Linux internal
>>>>> implementation detail rather than a description of the hardware, and
>>>>> doesn't really belong in the DT.
>>>> 
>>>> Hm. 
>>>> 
>>>> Let’s describe it differently.
>>>> 
>>>> I can see the Linux driver module as a simple software simulation for a
>>>> piece of hardware that could have been connected between the UART
>>>> and the GPS chip.
>>>> 
>>>> Basically it is a pulse-generator and a flip-flop to detect data flow on the RX
>>>> wire. This could be implemented by an external FPGA or uController. Or as
>>>> it is done on our board for saving hardware by a mix of the main CPU hardware
>>>> (Pinmux + GPIO + IRQ) and a kernel driver.
>>>> 
>>>> The best of course would have been if the w2sg0004 would have a physical
>>>> „enable“ GPIO (instead of that on-off control input).
>>>> 
>>>> Then we would hook up that enable to some physical GPIO of the CPU
>>>> and simply refer to it as e.g. <&gpio4 12>. And would not even need a driver
>>>> for it (unless we want to make rfkill gps work).
>>>> 
>>>> Therefore the driver we suggest provides an additional gpio controller with a
>>>> single GPIO so that we can write <&w2sg 0> to refer to this virtual gpio.
>>>> 
>>>> So in fact we try to wrap a non-optimal chip design by the driver and make it
>>>> appear as a standard GPIO interface to the DT and user space and whoever
>>>> needs simply to enable/disable the GPS chip.
>>> 
>>> The fact remains that this does not accurately represent the hardware,
>>> and is unnecessarily strongly tied to a particular UART design (where
>>> the DTR line is a separate UART).
>> 
>> I don’t get that it is tied to a particular UART design (except that our DTR
>> patch affects to omap-serial only).
>> 
>> Let’s describe the facts:
>> 
>> The chip has this interface:
>> 
>> GPS-TX (output)
>> GPS-RX (input)
>> ON/OFF (input) - to toggle the power state of the chip
>> 
>> They are connected to an OMAP3 UART2 as
>> 
>> UART2-TX > GPS-RX
>> UART2-RX <- GPS-TX
>> GPIO145 -> ON/OFF
>> 
>> That’s it.
>> 
>> If the chip (or any other serial GPS receiver like a Garmin) were connected
>> through real RS232 we would have
>> 
>> UART2-TX -> level shifter -> cable -> level shifter -> GPS-RX
>> UART2-RX <- level shifter <- cable <- level shifter <- GPS-TX
>> DTR-GPIO -> level shifter (DTR line) -> cable -> level shifter -> power-management logic -> ON/OFF
>> 
>> But because it is connected directly, we need to implement the power-management
>> logic between the DTR-GPIO and GPIO145:
>> 
>> DTR-GPIO -> driver -> GPIO145 -> ON/OFF
>> 
>> To correctly determine the state we need to tap the RX signal before
>> it enters the UART2-RX (it is pinmuxed with GPIO147):
>> 
>> UART2-RX <——+
>>                              OMAP3 pinmux <- OMAP3 pad <- GPS-TX
>>  GPIO147 <——+
>> 
>> So if we describe the driver as a box we have
>> 
>> inputs from user space:
>> * rfkill for GPS
>> * a control that is activated by opening /dev/tty
>> 
>> outputs to system:
>> * a control to switch the pinmux between RX and GPIO (interrupt)
>> * a control to external hardware
>> 
>>> 
>>>>> It sounds like what we actually need is the ability to describe devices
>>>>> attached to UARTs.
>>>> 
>>>> Hm. The purpose of the driver is power control of the chip. Not the serial
>>>> interface.
>>> 
>>> I'm not sure I follow your point. By describing the device as attached
>>> to the UART, the kernel can figure out that when said UART is accessed
>>> the attached device needs to be on (and must be poked as necessary).
>> 
>> Why do we need to describe this? It is all about controlling the GPIO145,
>> GPIO147 and pinmux. But not RX or TX.
>> 
>> So the driver does not need to know anything about UARTs (and if we
>> connect it to UART3 we only have to specify different GPIOs).
>> 
>> Only our board specific dtb connects the UART to the “virtual” GPIO
>> provided by the driver.
>> 
>> This also brings up a minor problem: on OMAP3 some UART RX lines can
>> be pinmuxed with different GPIOs. So putting the driver under some UART2
>> node doesn’t uniquely define the GPIO number to monitor RX and might
>> become a mess.
>> 
>>> 
>>> The power management logic for the device can stay in the device driver,
>>> and the power management logic for the UART can stay in the UART driver.
>> 
>> Here I can’t follow you. What power management does an UART driver provide?
>> 
>>> Neither would need to know about each other's internal details
>>> (e.g.GPIOs, for DTR or otherwise).
>> 
>> Yes, that is our goal and in our solution they don’t need to make assumptions
>> about each other. We just define in the DT: this GTA04 board has UART2-DTR
>> connected to the W2SG-GPIO# - instead of OMAP3-GPIO145.
>> 
>> In our solution the UART driver knows that there could be a DTR GPIO (similar to
>> a RTS GPIO which is already provided by the omap-serial RS484 bindings). Which
>> could be connected to some GPIO which drives the real DTR wire of a RS232 level
>> shifter.
>> 
>>> 
>>>>> Then you could have a mechanism whereby the UART
>>>>> driver can notify the other device driver regarding events (e.g. the
>>>>> UART being opened for access), or the other driver could claim ownership
>>>>> of the UART and expose its own interface to userspace.
>>>>> 
>>>>> That would be independent of the particular UART or other device, and
>>>>> the only description necessary in the DT would be an accurate
>>>>> representation of the way the hardware is wired.
>>>>> 
>>>>> There are a few ways that could be done, but I suspect the simplest is
>>>>> to just have the device as a sub-node of the UART, like we do for SPI or
>>>>> I2C buses:
>>>>> 
>>>>> 	serial@f00 {
>>>>> 		compatible = "vendor,uart";
>>>>> 		reg = <0xf00 0x100>;
>>>>> 		...
>>>>> 
>>>>> 		gps {
>>>>> 			compatible = "wi2wi,w2sg0004";
>>>>> 			...
>>>>> 		};
>>>>> 	};
>>>>> 
>>>>> That wouldn't work for devices with multiple UART connections. Do those
>>>>> exist, and are they common in configurations where out-of-band
>>>>> management is necessary (e.g. regulators, clocks)?
>>>> 
>>>> UARTs are usually point to point interfaces and not busses. So there is
>>>> no need to describe the interface.
>>> 
>>> I don't follow. You have a device which seems to require management
>>> kernel-side.
>> 
>> Yes, power on/off.
>> 
>>> Rather than describing the interface, you've described a
>>> fictitious relationship between the GPS device and the UART's DTR GPIO,
>>> and you’ve described a fictitious GPIO to hand to the UART driver.
>> 
>> Yes. Because the UART driver would generally expect a GPIO (if a GPIO driven
>> DTR hardware line is available on the connector).
>> 
>>> This
>>> is how you have linked the two in order to get the behaviour you want.
>> 
>> Exactly.
>> 
>>> 
>>> So it _is_ necessary to describe the interface. Rather than describing
>>> that interface at a high level you've chosen to hack together a set of
>>> fake relationships to work around the lack of ability to describe said
>>> interface.
>> 
>> The power control interface is a single GPIO from UART to the driver.
>> The other end is an interface from the driver to the pinmux of the OMAP
>> and the chip. So it is a “middleman”. But I think this is already clear.
>> 
>>> 
>>>> And I would speculate that in most cases they simply go to some
>>>> connector and therefore no connected chip that needs to be described
>>>> in the DT at all. Because it has a user-space driver (e.g. AT
>>>> commands) and no kernel driver.
>>> 
>>> In the case where no driver is necessary I agree that no description is
>>> necessary, though the description could be exposed in a helpful way to
>>> userspace to describe what’s attached to which UARTs.
>> 
>> That is usually done by configuring the gpsd process.
>> 
>>> 
>>> However, in this case you do have a kernel driver (even if basic), and
>>> it requires some knowledge of the relationship between the device and
>>> the UART in order to function.
>> 
>> Yes. And that is what we want to provide by the dtb file. That one should describe
>> the relationship.
>> 
>>> 
>>>> But we have no idea how such a solution could be implemented or tested.
>>> 
>>> I would disagree on that point, given I provided a high level
>>> description of how this could be implemented.
>> 
>> We do understand how you suggest the bindings should look like, but we have
>> no idea how that translates into code.
>> 
>> And we do not want to touch the general serial drivers, just the omap-serial
>> (because the solutions does not work without an UART behind a pinmux with a
>> GPIO with interrupt capabilities).
>> 
>> We simply have no experience modifying serial drivers (Linux is too big that anyone
>> can know all areas).
>> 
>> Our experience is limited to re-submitting the DTR-GPIO-control patch by Neil Brown
>> and making it read DT properties instead of board file.
>> 
>> So your approach needs a lot of help to really implement it. The question is who
>> will be doing it.
>> 
>>> 
>>>> If someone adds that to the serial drivers, we would be happy to use it,
>>>> but unless such a thing exists, I think our solution is quite simple and isolated
>>>> into this single driver and also uses existing standard interfaces (gpios, pinmux).
>>> 
>>> I would argue that this _abuses_ standard interfaces, as you have one
>>> device driver fiddling with resources logically owned by another.
>>> 
>>>>>> The reason to solve it that way is that we did not want to have a direct link
>>>>>> between this driver and any serial drivers or other mechanisms how drivers
>>>>>> can detect that their serial port (/dev/tty*) is opened.
>>>>>> 
>>>>>> It is used to power down the w2sg GPS chip if no user space process is
>>>>>> accessing its serial port (or de-asserts DTR through tcsetattr/ioctl).
>>>>>> 
>>>>>>> 
>>>>>>>> +
>>>>>>>> +                pinctrl-names = "default", "monitor";
>>>>>>>> +                pinctrl-0 = <&uart2_pins>;
>>>>>>>> +                pinctrl-1 = <&uart2_rx_irq_pins>;
>>>>>>>> +
>>>>>>>> +                interrupt-parent = <&gpio5>;
>>>>>>>> +                interrupts = <19 IRQ_TYPE_EDGE_FALLING>;  /* GPIO_147: RX - trigger on arrival of start bit */
>>>>>>> 
>>>>>>> While interrupts is a standard property, please describe above how many
>>>>>>> you expect and what their logical function is.
>>>>>>> 
>>>>>>> The only part I'm confused about is how the link to the UART is
>>>>>>> described. I assume I'm just ignorant of some existing pattern.
>>>>>> 
>>>>>> The serial link itself is not described at all because it is assumed to be a „must have“.
>>>>> 
>>>>> Huh? So it's a "must have" that you "don't have" in the DT?
>>>> 
>>>> Yes. The DT does not describe everything. Only those things that need
>>>> a kernel driver. And UARTs usually have user-space drivers (e.g. gpsd,
>>>> gsmd, pppd) and ioctl/tcsetattr.
>>> 
>>> The DT should describe the static portions of the hardware. Typically we
>>> only have devices with kernelspace drivers described simply because
>>> that's the way people have built DTs. Whether or not you have a
>>> kernelspace driver can change over time, the organisation of the
>>> hardware cannot.
>> 
>> So far none of the DT I have seen describe what is connected to the UART.
>> Even for boards which use HCI to communicate with a Bluetooth module.
>> 
>>> 
>>>>> I think that the relationship is being described incorrectly in the DT,
>>>>> and I think that there is a more general problem that needs to be
>>>>> addressed in order to make this case work.
>>>>> 
>>>>>> The driver only needs to monitor the RX line and needs to switch it between UART and
>>>>>> GPIO/IRQ mode. So this monitoring switch is described (with two different pinctrl states).
>>>>> 
>>>>> While this particular driver only needs that at this point in time,
>>>>> that's not necessarily true of drivers for similar devices, nor is it
>>>>> necessarily true if we need to add additional features to this driver.
>>>> 
>>>> Which features are you thinking of to add to this driver? And do
>>>> similar devices exist at all? Since we have not found any, we have
>>>> declared it as a "misc“ driver.
>>> 
>>> I don't have any particular feature in mind.
>>> 
>>> I am not immediately aware of other serial devices which require
>>> out-of-band management in the same way, though we have a vaguely similar
>>> case with SDIO devices which must be powered up before they appear on
>>> the bus.
>> 
>> AFAIK, they are usually descibed by a regulator that is enabled/disabled by
>> the SDIO driver. And the regulator is usually defined as a gpio-regulator to
>> drive an GPIO.
>> 
>> And I think the SDIO driver has a reset-gpio (which can also be interpreted
>> as a disable/power-down).
>> 
>> So such interface drivers simply expect that they can power-control dependent
>> devices through a regulator or a simple gpio.
>> 
>>> In that case I believe the intent is to describe them in the DT
>>> under the bus.
>> 
>> Or would it be ok to allow a regulator property for the serial interface? Then
>> our driver would become a w2sg0004-regulator driver similar to a gpio-regulator
>> but with a state machine that knows how to pulse the gpio until power is applied
>> to the GPS receiver internals.
>> 
>>> 
>>>>> Describing the relationship leaves a lot more freedom to improve things
>>>>> without having to update every DTB.
>>>>> 
>>>>>> We know that it is a little tricky to control this chip correctly - and we think this solution
>>>>>> is the most general (no direct dependency on the serial line, and just to pinmux states
>>>>>> and an interrupt).
>>>>> 
>>>>> I think that the rough approach I sketched out above is more general,
>>>>> and I think that you must describe the relationship with the serial
>>>>> line.
>>>>> 
>>>>> It's not clear to me whether the interrupt you describe is attached to
>>>>> the GPS, or if this is logically part of the UART.
>>>> 
>>>> The interrupt is needed to simulate the glue logic connected between
>>>> UART and GPS.
>>>> 
>>>> The output signal comes from the GPS module and goes to some pad
>>>> of the OMAP3 SoC. This pad can be either multiplexed into the UART RX
>>>> input or into a GPIO bank of the OMAP. That GPIO controller can generate
>>>> the interrupt on incoming data (when none is expected).
>>>> 
>>>> Therefore it is a GPS-generated interrupt and has nothing to do with
>>>> the UART.
>>> 
>>> Ok. When does the GPS device raise this interrupt?
>> 
>> If the driver assumes the GPS receiver is turned off, it disconnedts the RX
>> wire from the UART to a GPIO by using two different pinmux states.
>> 
>> If the GPIO raises an interrupt, the driver knows that the GPS module is not
>> turned off, because the driver has lost knowledge about its state. This is not
>> an UART related function but OMAP pinmux and GPIO.
>> 
>> I am not sure if that could even be implemented at all by a UART dependent
>> driver (since the UART is shut down and does not interrupt).
>> 
>>> 
>>> Thanks,
>>> Mark.
>> 
>> Thanks as well - it is a fruitful discussion (even if it becomes lengthy because
>> I might have repeated a lot of things that are already clear. Please accept an apology).
>> 
>> I think we just disagree about implementing a version that “works” in a quite specific
>> setup (there are necessary assumptions about OMAP pinmux and omap-serial) with
>> existing APIs versus a general one that might need a lot of changes outside the driver
>> and introduce new APIs.
>> 
>> BR,
>> Nikolaus
>> 
> 
> After re-thinking what we have discussed so far (and considering other recommendations
> I have received off-list) I currently see these options/questions:
> 
> * who could modify the serial drivers and APIs so that this driver can become
>  a subnode (“bus client”) of an UART? How long does it take to become available?
> 
> * would it be an option to rename it to “gpio-w2sg0004” to better describe that it
>  provides a (virtual) gpio?
> 
> * would it be an option to simply rename the driver to “w2sg0004-power”
>  to make clear that it is not at all related to the UART data communication
>  channel but only controlling the power of the w2sg0004 chip?
> 
> * both?
> 
> * or would it be acceptable if this is a regulator driver that controls the LDO
>  inside this chip? I.e. describe it as a “w2sg0004-regulator”?
> 
> This would be very similar to the function of a “gpio-regulator”: convert
> “regulator state” into a gpio state. But here convert “on/off” into some impulses.
> 
> And to resolve uncertainty about the real LDO state it would be able to monitor
> some feedback interrupt and handle an (optional) pinmux toggle.
> 
> This would mean that we do not even need to mention anything about UARTs
> in the driver bindings.
> 
> Rather it would just say: the driver can monitor a (gpio) interrupt line to
> know if the attached device is active (when it is not expected, e.g. after boot).
> 
> Since this monitor gpio is sometimes multiplexed with other features of the SoC,
> the driver can also switch between two pinctrl states (default and monitor).
> 
> BR,
> Nikolaus
> 


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2014-11-02 10:15 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-16 20:26 [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver Marek Belisko
2014-10-16 20:26 ` [PATCH 2/2] Documentation: devicetree: Add bindings for Wi2Wi w2sg0004 gps Marek Belisko
2014-10-17  9:37   ` Mark Rutland
2014-10-17 10:16     ` Dr. H. Nikolaus Schaller
2014-10-17 11:00       ` Mark Rutland
2014-10-17 19:55         ` Dr. H. Nikolaus Schaller
2014-10-20  9:35           ` Mark Rutland
2014-10-20 17:26             ` Dr. H. Nikolaus Schaller
2014-10-24  9:32               ` Dr. H. Nikolaus Schaller
2014-10-27  9:31                 ` Pavel Machek
2014-11-02 10:15                 ` Dr. H. Nikolaus Schaller
2014-10-19 19:51 ` [PATCH 1/2] misc: Add Wi2Wi w2sc0004 gps driver Arnd Bergmann
2014-10-19 20:29   ` Dr. H. Nikolaus Schaller
2014-10-21 10:49 ` Pavel Machek
2014-10-22 22:35   ` Dr. H. Nikolaus Schaller

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).