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