Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * pps-gpio.c -- PPS client driver using GPIO
0004  *
0005  * Copyright (C) 2010 Ricardo Martins <rasm@fe.up.pt>
0006  * Copyright (C) 2011 James Nuss <jamesnuss@nanometrics.ca>
0007  */
0008 
0009 #define PPS_GPIO_NAME "pps-gpio"
0010 #define pr_fmt(fmt) PPS_GPIO_NAME ": " fmt
0011 
0012 #include <linux/init.h>
0013 #include <linux/kernel.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/mod_devicetable.h>
0016 #include <linux/module.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/slab.h>
0019 #include <linux/pps_kernel.h>
0020 #include <linux/gpio/consumer.h>
0021 #include <linux/list.h>
0022 #include <linux/property.h>
0023 #include <linux/timer.h>
0024 #include <linux/jiffies.h>
0025 
0026 /* Info for each registered platform device */
0027 struct pps_gpio_device_data {
0028     int irq;            /* IRQ used as PPS source */
0029     struct pps_device *pps;     /* PPS source device */
0030     struct pps_source_info info;    /* PPS source information */
0031     struct gpio_desc *gpio_pin; /* GPIO port descriptors */
0032     struct gpio_desc *echo_pin;
0033     struct timer_list echo_timer;   /* timer to reset echo active state */
0034     bool assert_falling_edge;
0035     bool capture_clear;
0036     unsigned int echo_active_ms;    /* PPS echo active duration */
0037     unsigned long echo_timeout; /* timer timeout value in jiffies */
0038 };
0039 
0040 /*
0041  * Report the PPS event
0042  */
0043 
0044 static irqreturn_t pps_gpio_irq_handler(int irq, void *data)
0045 {
0046     const struct pps_gpio_device_data *info;
0047     struct pps_event_time ts;
0048     int rising_edge;
0049 
0050     /* Get the time stamp first */
0051     pps_get_ts(&ts);
0052 
0053     info = data;
0054 
0055     rising_edge = gpiod_get_value(info->gpio_pin);
0056     if ((rising_edge && !info->assert_falling_edge) ||
0057             (!rising_edge && info->assert_falling_edge))
0058         pps_event(info->pps, &ts, PPS_CAPTUREASSERT, data);
0059     else if (info->capture_clear &&
0060             ((rising_edge && info->assert_falling_edge) ||
0061             (!rising_edge && !info->assert_falling_edge)))
0062         pps_event(info->pps, &ts, PPS_CAPTURECLEAR, data);
0063 
0064     return IRQ_HANDLED;
0065 }
0066 
0067 /* This function will only be called when an ECHO GPIO is defined */
0068 static void pps_gpio_echo(struct pps_device *pps, int event, void *data)
0069 {
0070     /* add_timer() needs to write into info->echo_timer */
0071     struct pps_gpio_device_data *info = data;
0072 
0073     switch (event) {
0074     case PPS_CAPTUREASSERT:
0075         if (pps->params.mode & PPS_ECHOASSERT)
0076             gpiod_set_value(info->echo_pin, 1);
0077         break;
0078 
0079     case PPS_CAPTURECLEAR:
0080         if (pps->params.mode & PPS_ECHOCLEAR)
0081             gpiod_set_value(info->echo_pin, 1);
0082         break;
0083     }
0084 
0085     /* fire the timer */
0086     if (info->pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) {
0087         info->echo_timer.expires = jiffies + info->echo_timeout;
0088         add_timer(&info->echo_timer);
0089     }
0090 }
0091 
0092 /* Timer callback to reset the echo pin to the inactive state */
0093 static void pps_gpio_echo_timer_callback(struct timer_list *t)
0094 {
0095     const struct pps_gpio_device_data *info;
0096 
0097     info = from_timer(info, t, echo_timer);
0098 
0099     gpiod_set_value(info->echo_pin, 0);
0100 }
0101 
0102 static int pps_gpio_setup(struct device *dev)
0103 {
0104     struct pps_gpio_device_data *data = dev_get_drvdata(dev);
0105     int ret;
0106     u32 value;
0107 
0108     data->gpio_pin = devm_gpiod_get(dev, NULL, GPIOD_IN);
0109     if (IS_ERR(data->gpio_pin))
0110         return dev_err_probe(dev, PTR_ERR(data->gpio_pin),
0111                      "failed to request PPS GPIO\n");
0112 
0113     data->assert_falling_edge =
0114         device_property_read_bool(dev, "assert-falling-edge");
0115 
0116     data->echo_pin = devm_gpiod_get_optional(dev, "echo", GPIOD_OUT_LOW);
0117     if (IS_ERR(data->echo_pin))
0118         return dev_err_probe(dev, PTR_ERR(data->echo_pin),
0119                      "failed to request ECHO GPIO\n");
0120 
0121     if (!data->echo_pin)
0122         return 0;
0123 
0124     ret = device_property_read_u32(dev, "echo-active-ms", &value);
0125     if (ret) {
0126         dev_err(dev, "failed to get echo-active-ms from FW\n");
0127         return ret;
0128     }
0129 
0130     /* sanity check on echo_active_ms */
0131     if (!value || value > 999) {
0132         dev_err(dev, "echo-active-ms: %u - bad value from FW\n", value);
0133         return -EINVAL;
0134     }
0135 
0136     data->echo_active_ms = value;
0137 
0138     return 0;
0139 }
0140 
0141 static unsigned long
0142 get_irqf_trigger_flags(const struct pps_gpio_device_data *data)
0143 {
0144     unsigned long flags = data->assert_falling_edge ?
0145         IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
0146 
0147     if (data->capture_clear) {
0148         flags |= ((flags & IRQF_TRIGGER_RISING) ?
0149                 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
0150     }
0151 
0152     return flags;
0153 }
0154 
0155 static int pps_gpio_probe(struct platform_device *pdev)
0156 {
0157     struct pps_gpio_device_data *data;
0158     struct device *dev = &pdev->dev;
0159     int ret;
0160     int pps_default_params;
0161 
0162     /* allocate space for device info */
0163     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0164     if (!data)
0165         return -ENOMEM;
0166 
0167     dev_set_drvdata(dev, data);
0168 
0169     /* GPIO setup */
0170     ret = pps_gpio_setup(dev);
0171     if (ret)
0172         return ret;
0173 
0174     /* IRQ setup */
0175     ret = gpiod_to_irq(data->gpio_pin);
0176     if (ret < 0) {
0177         dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
0178         return -EINVAL;
0179     }
0180     data->irq = ret;
0181 
0182     /* initialize PPS specific parts of the bookkeeping data structure. */
0183     data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
0184         PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC;
0185     if (data->capture_clear)
0186         data->info.mode |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR |
0187             PPS_ECHOCLEAR;
0188     data->info.owner = THIS_MODULE;
0189     snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d",
0190          pdev->name, pdev->id);
0191     if (data->echo_pin) {
0192         data->info.echo = pps_gpio_echo;
0193         data->echo_timeout = msecs_to_jiffies(data->echo_active_ms);
0194         timer_setup(&data->echo_timer, pps_gpio_echo_timer_callback, 0);
0195     }
0196 
0197     /* register PPS source */
0198     pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
0199     if (data->capture_clear)
0200         pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR;
0201     data->pps = pps_register_source(&data->info, pps_default_params);
0202     if (IS_ERR(data->pps)) {
0203         dev_err(dev, "failed to register IRQ %d as PPS source\n",
0204             data->irq);
0205         return PTR_ERR(data->pps);
0206     }
0207 
0208     /* register IRQ interrupt handler */
0209     ret = devm_request_irq(dev, data->irq, pps_gpio_irq_handler,
0210             get_irqf_trigger_flags(data), data->info.name, data);
0211     if (ret) {
0212         pps_unregister_source(data->pps);
0213         dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
0214         return -EINVAL;
0215     }
0216 
0217     dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n",
0218          data->irq);
0219 
0220     return 0;
0221 }
0222 
0223 static int pps_gpio_remove(struct platform_device *pdev)
0224 {
0225     struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
0226 
0227     pps_unregister_source(data->pps);
0228     del_timer_sync(&data->echo_timer);
0229     /* reset echo pin in any case */
0230     gpiod_set_value(data->echo_pin, 0);
0231     dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
0232     return 0;
0233 }
0234 
0235 static const struct of_device_id pps_gpio_dt_ids[] = {
0236     { .compatible = "pps-gpio", },
0237     { /* sentinel */ }
0238 };
0239 MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
0240 
0241 static struct platform_driver pps_gpio_driver = {
0242     .probe      = pps_gpio_probe,
0243     .remove     = pps_gpio_remove,
0244     .driver     = {
0245         .name   = PPS_GPIO_NAME,
0246         .of_match_table = pps_gpio_dt_ids,
0247     },
0248 };
0249 
0250 module_platform_driver(pps_gpio_driver);
0251 MODULE_AUTHOR("Ricardo Martins <rasm@fe.up.pt>");
0252 MODULE_AUTHOR("James Nuss <jamesnuss@nanometrics.ca>");
0253 MODULE_DESCRIPTION("Use GPIO pin as PPS source");
0254 MODULE_LICENSE("GPL");
0255 MODULE_VERSION("1.2.0");