All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lukas Senger <lukas@fridolin.com>
To: giometti@enneenne.com
Cc: linux-kernel@vger.kernel.org, Lukas Senger <lukas@fridolin.com>
Subject: [PATCH] pps-gpio: implement echo pulses
Date: Tue,  6 Feb 2018 16:58:56 +0100	[thread overview]
Message-ID: <20180206155856.14562-1-lukas@fridolin.com> (raw)

pps-gpio reports as having echo capability via sysfs, which is not
actually the case. This patch implements it.

The output pin is hardcoded as 17. This should probably be configurable
via the dtoverlay in the same way as the input pin.
---
 drivers/pps/clients/pps-gpio.c | 55 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 50 insertions(+), 5 deletions(-)

diff --git a/drivers/pps/clients/pps-gpio.c b/drivers/pps/clients/pps-gpio.c
index 333ad7d5..dd7624f1 100644
--- a/drivers/pps/clients/pps-gpio.c
+++ b/drivers/pps/clients/pps-gpio.c
@@ -35,6 +35,12 @@
 #include <linux/list.h>
 #include <linux/of_device.h>
 #include <linux/of_gpio.h>
+#include <linux/delay.h>
+
+/* TODO: this should work like gpio_pin below but I don't know how to work with
+ * devicetree overlays.
+ */
+#define PPS_GPIO_ECHO_PIN 17
 
 /* Info for each registered platform device */
 struct pps_gpio_device_data {
@@ -63,16 +69,41 @@ static irqreturn_t pps_gpio_irq_handler(int irq, void *data)
 
 	rising_edge = gpio_get_value(info->gpio_pin);
 	if ((rising_edge && !info->assert_falling_edge) ||
-			(!rising_edge && info->assert_falling_edge))
+			(!rising_edge && info->assert_falling_edge)) {
+		if (info->pps->params.mode & PPS_ECHOASSERT) {
+			gpio_set_value(PPS_GPIO_ECHO_PIN, 1);
+		}
 		pps_event(info->pps, &ts, PPS_CAPTUREASSERT, NULL);
-	else if (info->capture_clear &&
+	} else if (info->capture_clear &&
 			((rising_edge && info->assert_falling_edge) ||
-			 (!rising_edge && !info->assert_falling_edge)))
+			 (!rising_edge && !info->assert_falling_edge))) {
+		if (info->pps->params.mode & PPS_ECHOCLEAR) {
+			gpio_set_value(PPS_GPIO_ECHO_PIN, 1);
+		}
 		pps_event(info->pps, &ts, PPS_CAPTURECLEAR, NULL);
+	}
 
+	if (info->pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR))
+		return IRQ_WAKE_THREAD;
 	return IRQ_HANDLED;
 }
 
+/* If we sent an echo pulse, we set the output pin back to low. This results in
+ * an echo pulse of roughly 100ms length.
+ */
+static irqreturn_t pps_gpio_irq_threaded(int irq, void *data)
+{
+	const struct pps_gpio_device_data *info;
+
+	info = data;
+
+	msleep(100);
+	gpio_set_value(PPS_GPIO_ECHO_PIN, 0);
+
+	return IRQ_HANDLED;
+}
+
+
 static unsigned long
 get_irqf_trigger_flags(const struct pps_gpio_device_data *data)
 {
@@ -131,7 +162,20 @@ static int pps_gpio_probe(struct platform_device *pdev)
 
 	ret = gpio_direction_input(data->gpio_pin);
 	if (ret) {
-		dev_err(&pdev->dev, "failed to set pin direction\n");
+		dev_err(&pdev->dev, "failed to set pin as input\n");
+		return -EINVAL;
+	}
+
+	ret = devm_gpio_request(&pdev->dev, PPS_GPIO_ECHO_PIN, gpio_label);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to request GPIO %u\n",
+			PPS_GPIO_ECHO_PIN);
+		return ret;
+	}
+
+	ret = gpio_direction_output(PPS_GPIO_ECHO_PIN, 0);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to set pin as output\n");
 		return -EINVAL;
 	}
 
@@ -165,7 +209,8 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	}
 
 	/* register IRQ interrupt handler */
-	ret = devm_request_irq(&pdev->dev, data->irq, pps_gpio_irq_handler,
+	ret = devm_request_threaded_irq(&pdev->dev, data->irq,
+			pps_gpio_irq_handler, pps_gpio_irq_threaded,
 			get_irqf_trigger_flags(data), data->info.name, data);
 	if (ret) {
 		pps_unregister_source(data->pps);
-- 
2.16.1

             reply	other threads:[~2018-02-06 15:59 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-06 15:58 Lukas Senger [this message]
2018-02-07  9:09 ` [PATCH] pps-gpio: implement echo pulses Rodolfo Giometti
2018-02-15 12:59   ` Lukas Senger
2018-02-15 12:59     ` [PATCH 1/2] pps-gpio: Avoid flooding syslog Lukas Senger
2018-02-15 13:34       ` Rodolfo Giometti
2018-02-15 15:12         ` Lukas Senger
2018-02-15 15:36           ` Rodolfo Giometti
2018-02-15 19:48             ` Lukas Senger
2018-02-15 12:59     ` [PATCH 2/2] pps-gpio: Set echo GPIO pin via devicetree Lukas Senger
2018-02-15 13:43       ` Rodolfo Giometti
2018-02-15 15:08         ` Lukas Senger
2018-02-15 15:33           ` Rodolfo Giometti

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180206155856.14562-1-lukas@fridolin.com \
    --to=lukas@fridolin.com \
    --cc=giometti@enneenne.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.