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 unsigned int echo_active_ms; /* PPS echo active duration */ 35 unsigned long echo_timeout; /* timer timeout value in jiffies */ 36 struct pps_event_time ts; /* timestamp captured in hardirq */ 37 }; 38 39 /* 40 * Report the PPS event 41 */ 42 43 /* 44 * Primary hardirq handler -- runs in hardirq context even on PREEMPT_RT. 45 * Only captures the timestamp; all other work is deferred to the thread. 46 */ 47 static irqreturn_t pps_gpio_irq_hardirq(int irq, void *data) 48 { 49 struct pps_gpio_device_data *info = data; 50 51 pps_get_ts(&info->ts); 52 53 return IRQ_WAKE_THREAD; 54 } 55 56 /* 57 * Threaded handler -- processes the PPS event using the timestamp 58 * captured in hardirq context above. 59 */ 60 static irqreturn_t pps_gpio_irq_thread(int irq, void *data) 61 { 62 struct pps_gpio_device_data *info = data; 63 64 pps_event(info->pps, &info->ts, PPS_CAPTUREASSERT, data); 65 66 return IRQ_HANDLED; 67 } 68 69 /* This function will only be called when an ECHO GPIO is defined */ 70 static void pps_gpio_echo(struct pps_device *pps, int event, void *data) 71 { 72 /* add_timer() needs to write into info->echo_timer */ 73 struct pps_gpio_device_data *info = data; 74 75 switch (event) { 76 case PPS_CAPTUREASSERT: 77 if (pps->params.mode & PPS_ECHOASSERT) 78 gpiod_set_value(info->echo_pin, 1); 79 break; 80 } 81 82 /* fire the timer */ 83 if (info->pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) { 84 info->echo_timer.expires = jiffies + info->echo_timeout; 85 add_timer(&info->echo_timer); 86 } 87 } 88 89 /* Timer callback to reset the echo pin to the inactive state */ 90 static void pps_gpio_echo_timer_callback(struct timer_list *t) 91 { 92 const struct pps_gpio_device_data *info; 93 94 info = timer_container_of(info, t, echo_timer); 95 96 gpiod_set_value(info->echo_pin, 0); 97 } 98 99 static int pps_gpio_setup(struct device *dev) 100 { 101 struct pps_gpio_device_data *data = dev_get_drvdata(dev); 102 int ret; 103 u32 value; 104 105 data->gpio_pin = devm_gpiod_get(dev, NULL, GPIOD_IN); 106 if (IS_ERR(data->gpio_pin)) 107 return dev_err_probe(dev, PTR_ERR(data->gpio_pin), 108 "failed to request PPS GPIO\n"); 109 110 data->assert_falling_edge = 111 device_property_read_bool(dev, "assert-falling-edge"); 112 113 data->echo_pin = devm_gpiod_get_optional(dev, "echo", GPIOD_OUT_LOW); 114 if (IS_ERR(data->echo_pin)) 115 return dev_err_probe(dev, PTR_ERR(data->echo_pin), 116 "failed to request ECHO GPIO\n"); 117 118 if (!data->echo_pin) 119 return 0; 120 121 ret = device_property_read_u32(dev, "echo-active-ms", &value); 122 if (ret) { 123 dev_err(dev, "failed to get echo-active-ms from FW\n"); 124 return ret; 125 } 126 127 /* sanity check on echo_active_ms */ 128 if (!value || value > 999) { 129 dev_err(dev, "echo-active-ms: %u - bad value from FW\n", value); 130 return -EINVAL; 131 } 132 133 data->echo_active_ms = value; 134 135 return 0; 136 } 137 138 static unsigned long 139 get_irqf_trigger_flags(const struct pps_gpio_device_data *data) 140 { 141 return data->assert_falling_edge ? IRQF_TRIGGER_FALLING : 142 IRQF_TRIGGER_RISING; 143 } 144 145 static int pps_gpio_probe(struct platform_device *pdev) 146 { 147 struct pps_gpio_device_data *data; 148 struct device *dev = &pdev->dev; 149 int ret; 150 int pps_default_params; 151 152 /* allocate space for device info */ 153 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 154 if (!data) 155 return -ENOMEM; 156 157 dev_set_drvdata(dev, data); 158 159 /* GPIO setup */ 160 ret = pps_gpio_setup(dev); 161 if (ret) 162 return ret; 163 164 /* IRQ setup */ 165 ret = gpiod_to_irq(data->gpio_pin); 166 if (ret < 0) { 167 dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret); 168 return -EINVAL; 169 } 170 data->irq = ret; 171 172 /* initialize PPS specific parts of the bookkeeping data structure. */ 173 data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT | 174 PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC; 175 data->info.owner = THIS_MODULE; 176 snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d", 177 pdev->name, pdev->id); 178 if (data->echo_pin) { 179 data->info.echo = pps_gpio_echo; 180 data->echo_timeout = msecs_to_jiffies(data->echo_active_ms); 181 timer_setup(&data->echo_timer, pps_gpio_echo_timer_callback, 0); 182 } 183 184 /* register PPS source */ 185 pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT; 186 data->pps = pps_register_source(&data->info, pps_default_params); 187 if (IS_ERR(data->pps)) { 188 dev_err(dev, "failed to register IRQ %d as PPS source\n", 189 data->irq); 190 return PTR_ERR(data->pps); 191 } 192 193 /* register IRQ interrupt handler */ 194 ret = request_threaded_irq(data->irq, 195 pps_gpio_irq_hardirq, pps_gpio_irq_thread, 196 get_irqf_trigger_flags(data) | IRQF_ONESHOT, 197 data->info.name, data); 198 if (ret) { 199 pps_unregister_source(data->pps); 200 dev_err(dev, "failed to acquire IRQ %d\n", data->irq); 201 return -EINVAL; 202 } 203 204 dev_dbg(&data->pps->dev, "Registered IRQ %d as PPS source\n", 205 data->irq); 206 207 return 0; 208 } 209 210 static void pps_gpio_remove(struct platform_device *pdev) 211 { 212 struct pps_gpio_device_data *data = platform_get_drvdata(pdev); 213 214 free_irq(data->irq, data); 215 pps_unregister_source(data->pps); 216 timer_delete_sync(&data->echo_timer); 217 /* reset echo pin in any case */ 218 gpiod_set_value(data->echo_pin, 0); 219 dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq); 220 } 221 222 static const struct of_device_id pps_gpio_dt_ids[] = { 223 { .compatible = "pps-gpio", }, 224 { /* sentinel */ } 225 }; 226 MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids); 227 228 static struct platform_driver pps_gpio_driver = { 229 .probe = pps_gpio_probe, 230 .remove = pps_gpio_remove, 231 .driver = { 232 .name = PPS_GPIO_NAME, 233 .of_match_table = pps_gpio_dt_ids, 234 }, 235 }; 236 237 module_platform_driver(pps_gpio_driver); 238 MODULE_AUTHOR("Ricardo Martins <rasm@fe.up.pt>"); 239 MODULE_AUTHOR("James Nuss <jamesnuss@nanometrics.ca>"); 240 MODULE_DESCRIPTION("Use GPIO pin as PPS source"); 241 MODULE_LICENSE("GPL"); 242 MODULE_VERSION("1.2.0"); 243