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