^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * pps-gpio.c -- PPS client driver using GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2010 Ricardo Martins <rasm@fe.up.pt>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2011 James Nuss <jamesnuss@nanometrics.ca>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define PPS_GPIO_NAME "pps-gpio"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define pr_fmt(fmt) PPS_GPIO_NAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pps_kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/pps-gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/of_gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* Info for each registered platform device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct pps_gpio_device_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int irq; /* IRQ used as PPS source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct pps_device *pps; /* PPS source device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct pps_source_info info; /* PPS source information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct gpio_desc *gpio_pin; /* GPIO port descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct gpio_desc *echo_pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct timer_list echo_timer; /* timer to reset echo active state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) bool assert_falling_edge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) bool capture_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned int echo_active_ms; /* PPS echo active duration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned long echo_timeout; /* timer timeout value in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Report the PPS event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static irqreturn_t pps_gpio_irq_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) const struct pps_gpio_device_data *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct pps_event_time ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int rising_edge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* Get the time stamp first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) pps_get_ts(&ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) rising_edge = gpiod_get_value(info->gpio_pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if ((rising_edge && !info->assert_falling_edge) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) (!rising_edge && info->assert_falling_edge))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) pps_event(info->pps, &ts, PPS_CAPTUREASSERT, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) else if (info->capture_clear &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ((rising_edge && info->assert_falling_edge) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) (!rising_edge && !info->assert_falling_edge)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) pps_event(info->pps, &ts, PPS_CAPTURECLEAR, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* This function will only be called when an ECHO GPIO is defined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static void pps_gpio_echo(struct pps_device *pps, int event, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* add_timer() needs to write into info->echo_timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct pps_gpio_device_data *info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) case PPS_CAPTUREASSERT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (pps->params.mode & PPS_ECHOASSERT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) gpiod_set_value(info->echo_pin, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) case PPS_CAPTURECLEAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (pps->params.mode & PPS_ECHOCLEAR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) gpiod_set_value(info->echo_pin, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* fire the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (info->pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) info->echo_timer.expires = jiffies + info->echo_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) add_timer(&info->echo_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* Timer callback to reset the echo pin to the inactive state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static void pps_gpio_echo_timer_callback(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) const struct pps_gpio_device_data *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) info = from_timer(info, t, echo_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) gpiod_set_value(info->echo_pin, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int pps_gpio_setup(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct device_node *np = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) data->gpio_pin = devm_gpiod_get(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) NULL, /* request "gpios" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (IS_ERR(data->gpio_pin)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) "failed to request PPS GPIO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return PTR_ERR(data->gpio_pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) data->echo_pin = devm_gpiod_get_optional(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) "echo",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (data->echo_pin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (IS_ERR(data->echo_pin)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) dev_err(&pdev->dev, "failed to request ECHO GPIO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return PTR_ERR(data->echo_pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ret = of_property_read_u32(np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) "echo-active-ms",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) "failed to get echo-active-ms from OF\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) data->echo_active_ms = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* sanity check on echo_active_ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!data->echo_active_ms || data->echo_active_ms > 999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) "echo-active-ms: %u - bad value from OF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) data->echo_active_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (of_property_read_bool(np, "assert-falling-edge"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) data->assert_falling_edge = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) get_irqf_trigger_flags(const struct pps_gpio_device_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned long flags = data->assert_falling_edge ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (data->capture_clear) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) flags |= ((flags & IRQF_TRIGGER_RISING) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static int pps_gpio_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct pps_gpio_device_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int pps_default_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) const struct pps_gpio_platform_data *pdata = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* allocate space for device info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) platform_set_drvdata(pdev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /* GPIO setup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) data->gpio_pin = pdata->gpio_pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) data->echo_pin = pdata->echo_pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) data->assert_falling_edge = pdata->assert_falling_edge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) data->capture_clear = pdata->capture_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) data->echo_active_ms = pdata->echo_active_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) ret = pps_gpio_setup(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* IRQ setup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ret = gpiod_to_irq(data->gpio_pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) dev_err(&pdev->dev, "failed to map GPIO to IRQ: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) data->irq = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* initialize PPS specific parts of the bookkeeping data structure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (data->capture_clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) data->info.mode |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) PPS_ECHOCLEAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) data->info.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) pdev->name, pdev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (data->echo_pin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) data->info.echo = pps_gpio_echo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) data->echo_timeout = msecs_to_jiffies(data->echo_active_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) timer_setup(&data->echo_timer, pps_gpio_echo_timer_callback, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* register PPS source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (data->capture_clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) data->pps = pps_register_source(&data->info, pps_default_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (IS_ERR(data->pps)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) dev_err(&pdev->dev, "failed to register IRQ %d as PPS source\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) data->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return PTR_ERR(data->pps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /* register IRQ interrupt handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) ret = devm_request_irq(&pdev->dev, data->irq, pps_gpio_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) get_irqf_trigger_flags(data), data->info.name, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) pps_unregister_source(data->pps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) dev_err(&pdev->dev, "failed to acquire IRQ %d\n", data->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) data->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static int pps_gpio_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) pps_unregister_source(data->pps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (data->echo_pin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) del_timer_sync(&data->echo_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /* reset echo pin in any case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) gpiod_set_value(data->echo_pin, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static const struct of_device_id pps_gpio_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) { .compatible = "pps-gpio", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static struct platform_driver pps_gpio_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) .probe = pps_gpio_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) .remove = pps_gpio_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) .name = PPS_GPIO_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) .of_match_table = pps_gpio_dt_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) module_platform_driver(pps_gpio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) MODULE_AUTHOR("Ricardo Martins <rasm@fe.up.pt>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) MODULE_AUTHOR("James Nuss <jamesnuss@nanometrics.ca>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) MODULE_DESCRIPTION("Use GPIO pin as PPS source");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) MODULE_VERSION("1.2.0");