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