| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * pps-gpio.c -- PPS client driver using GPIO |
|---|
| 3 | 4 | * |
|---|
| 4 | | - * |
|---|
| 5 | 5 | * Copyright (C) 2010 Ricardo Martins <rasm@fe.up.pt> |
|---|
| 6 | 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 | 7 | */ |
|---|
| 22 | 8 | |
|---|
| 23 | 9 | #define PPS_GPIO_NAME "pps-gpio" |
|---|
| .. | .. |
|---|
| 31 | 17 | #include <linux/slab.h> |
|---|
| 32 | 18 | #include <linux/pps_kernel.h> |
|---|
| 33 | 19 | #include <linux/pps-gpio.h> |
|---|
| 34 | | -#include <linux/gpio.h> |
|---|
| 20 | +#include <linux/gpio/consumer.h> |
|---|
| 35 | 21 | #include <linux/list.h> |
|---|
| 36 | 22 | #include <linux/of_device.h> |
|---|
| 37 | 23 | #include <linux/of_gpio.h> |
|---|
| 24 | +#include <linux/timer.h> |
|---|
| 25 | +#include <linux/jiffies.h> |
|---|
| 38 | 26 | |
|---|
| 39 | 27 | /* Info for each registered platform device */ |
|---|
| 40 | 28 | struct pps_gpio_device_data { |
|---|
| 41 | 29 | int irq; /* IRQ used as PPS source */ |
|---|
| 42 | 30 | struct pps_device *pps; /* PPS source device */ |
|---|
| 43 | 31 | struct pps_source_info info; /* PPS source information */ |
|---|
| 32 | + struct gpio_desc *gpio_pin; /* GPIO port descriptors */ |
|---|
| 33 | + struct gpio_desc *echo_pin; |
|---|
| 34 | + struct timer_list echo_timer; /* timer to reset echo active state */ |
|---|
| 44 | 35 | bool assert_falling_edge; |
|---|
| 45 | 36 | bool capture_clear; |
|---|
| 46 | | - unsigned int gpio_pin; |
|---|
| 37 | + unsigned int echo_active_ms; /* PPS echo active duration */ |
|---|
| 38 | + unsigned long echo_timeout; /* timer timeout value in jiffies */ |
|---|
| 47 | 39 | }; |
|---|
| 48 | 40 | |
|---|
| 49 | 41 | /* |
|---|
| .. | .. |
|---|
| 61 | 53 | |
|---|
| 62 | 54 | info = data; |
|---|
| 63 | 55 | |
|---|
| 64 | | - rising_edge = gpio_get_value(info->gpio_pin); |
|---|
| 56 | + rising_edge = gpiod_get_value(info->gpio_pin); |
|---|
| 65 | 57 | if ((rising_edge && !info->assert_falling_edge) || |
|---|
| 66 | 58 | (!rising_edge && info->assert_falling_edge)) |
|---|
| 67 | | - pps_event(info->pps, &ts, PPS_CAPTUREASSERT, NULL); |
|---|
| 59 | + pps_event(info->pps, &ts, PPS_CAPTUREASSERT, data); |
|---|
| 68 | 60 | else if (info->capture_clear && |
|---|
| 69 | 61 | ((rising_edge && info->assert_falling_edge) || |
|---|
| 70 | | - (!rising_edge && !info->assert_falling_edge))) |
|---|
| 71 | | - pps_event(info->pps, &ts, PPS_CAPTURECLEAR, NULL); |
|---|
| 62 | + (!rising_edge && !info->assert_falling_edge))) |
|---|
| 63 | + pps_event(info->pps, &ts, PPS_CAPTURECLEAR, data); |
|---|
| 72 | 64 | |
|---|
| 73 | 65 | return IRQ_HANDLED; |
|---|
| 66 | +} |
|---|
| 67 | + |
|---|
| 68 | +/* This function will only be called when an ECHO GPIO is defined */ |
|---|
| 69 | +static void pps_gpio_echo(struct pps_device *pps, int event, void *data) |
|---|
| 70 | +{ |
|---|
| 71 | + /* add_timer() needs to write into info->echo_timer */ |
|---|
| 72 | + struct pps_gpio_device_data *info = data; |
|---|
| 73 | + |
|---|
| 74 | + switch (event) { |
|---|
| 75 | + case PPS_CAPTUREASSERT: |
|---|
| 76 | + if (pps->params.mode & PPS_ECHOASSERT) |
|---|
| 77 | + gpiod_set_value(info->echo_pin, 1); |
|---|
| 78 | + break; |
|---|
| 79 | + |
|---|
| 80 | + case PPS_CAPTURECLEAR: |
|---|
| 81 | + if (pps->params.mode & PPS_ECHOCLEAR) |
|---|
| 82 | + gpiod_set_value(info->echo_pin, 1); |
|---|
| 83 | + break; |
|---|
| 84 | + } |
|---|
| 85 | + |
|---|
| 86 | + /* fire the timer */ |
|---|
| 87 | + if (info->pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) { |
|---|
| 88 | + info->echo_timer.expires = jiffies + info->echo_timeout; |
|---|
| 89 | + add_timer(&info->echo_timer); |
|---|
| 90 | + } |
|---|
| 91 | +} |
|---|
| 92 | + |
|---|
| 93 | +/* Timer callback to reset the echo pin to the inactive state */ |
|---|
| 94 | +static void pps_gpio_echo_timer_callback(struct timer_list *t) |
|---|
| 95 | +{ |
|---|
| 96 | + const struct pps_gpio_device_data *info; |
|---|
| 97 | + |
|---|
| 98 | + info = from_timer(info, t, echo_timer); |
|---|
| 99 | + |
|---|
| 100 | + gpiod_set_value(info->echo_pin, 0); |
|---|
| 101 | +} |
|---|
| 102 | + |
|---|
| 103 | +static int pps_gpio_setup(struct platform_device *pdev) |
|---|
| 104 | +{ |
|---|
| 105 | + struct pps_gpio_device_data *data = platform_get_drvdata(pdev); |
|---|
| 106 | + struct device_node *np = pdev->dev.of_node; |
|---|
| 107 | + int ret; |
|---|
| 108 | + u32 value; |
|---|
| 109 | + |
|---|
| 110 | + data->gpio_pin = devm_gpiod_get(&pdev->dev, |
|---|
| 111 | + NULL, /* request "gpios" */ |
|---|
| 112 | + GPIOD_IN); |
|---|
| 113 | + if (IS_ERR(data->gpio_pin)) { |
|---|
| 114 | + dev_err(&pdev->dev, |
|---|
| 115 | + "failed to request PPS GPIO\n"); |
|---|
| 116 | + return PTR_ERR(data->gpio_pin); |
|---|
| 117 | + } |
|---|
| 118 | + |
|---|
| 119 | + data->echo_pin = devm_gpiod_get_optional(&pdev->dev, |
|---|
| 120 | + "echo", |
|---|
| 121 | + GPIOD_OUT_LOW); |
|---|
| 122 | + if (data->echo_pin) { |
|---|
| 123 | + if (IS_ERR(data->echo_pin)) { |
|---|
| 124 | + dev_err(&pdev->dev, "failed to request ECHO GPIO\n"); |
|---|
| 125 | + return PTR_ERR(data->echo_pin); |
|---|
| 126 | + } |
|---|
| 127 | + |
|---|
| 128 | + ret = of_property_read_u32(np, |
|---|
| 129 | + "echo-active-ms", |
|---|
| 130 | + &value); |
|---|
| 131 | + if (ret) { |
|---|
| 132 | + dev_err(&pdev->dev, |
|---|
| 133 | + "failed to get echo-active-ms from OF\n"); |
|---|
| 134 | + return ret; |
|---|
| 135 | + } |
|---|
| 136 | + data->echo_active_ms = value; |
|---|
| 137 | + /* sanity check on echo_active_ms */ |
|---|
| 138 | + if (!data->echo_active_ms || data->echo_active_ms > 999) { |
|---|
| 139 | + dev_err(&pdev->dev, |
|---|
| 140 | + "echo-active-ms: %u - bad value from OF\n", |
|---|
| 141 | + data->echo_active_ms); |
|---|
| 142 | + return -EINVAL; |
|---|
| 143 | + } |
|---|
| 144 | + } |
|---|
| 145 | + |
|---|
| 146 | + if (of_property_read_bool(np, "assert-falling-edge")) |
|---|
| 147 | + data->assert_falling_edge = true; |
|---|
| 148 | + return 0; |
|---|
| 74 | 149 | } |
|---|
| 75 | 150 | |
|---|
| 76 | 151 | static unsigned long |
|---|
| .. | .. |
|---|
| 90 | 165 | static int pps_gpio_probe(struct platform_device *pdev) |
|---|
| 91 | 166 | { |
|---|
| 92 | 167 | struct pps_gpio_device_data *data; |
|---|
| 93 | | - const char *gpio_label; |
|---|
| 94 | 168 | int ret; |
|---|
| 95 | 169 | int pps_default_params; |
|---|
| 96 | 170 | const struct pps_gpio_platform_data *pdata = pdev->dev.platform_data; |
|---|
| 97 | | - struct device_node *np = pdev->dev.of_node; |
|---|
| 98 | 171 | |
|---|
| 99 | 172 | /* allocate space for device info */ |
|---|
| 100 | | - data = devm_kzalloc(&pdev->dev, sizeof(struct pps_gpio_device_data), |
|---|
| 101 | | - GFP_KERNEL); |
|---|
| 173 | + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
|---|
| 102 | 174 | if (!data) |
|---|
| 103 | 175 | return -ENOMEM; |
|---|
| 176 | + platform_set_drvdata(pdev, data); |
|---|
| 104 | 177 | |
|---|
| 178 | + /* GPIO setup */ |
|---|
| 105 | 179 | if (pdata) { |
|---|
| 106 | 180 | data->gpio_pin = pdata->gpio_pin; |
|---|
| 107 | | - gpio_label = pdata->gpio_label; |
|---|
| 181 | + data->echo_pin = pdata->echo_pin; |
|---|
| 108 | 182 | |
|---|
| 109 | 183 | data->assert_falling_edge = pdata->assert_falling_edge; |
|---|
| 110 | 184 | data->capture_clear = pdata->capture_clear; |
|---|
| 185 | + data->echo_active_ms = pdata->echo_active_ms; |
|---|
| 111 | 186 | } else { |
|---|
| 112 | | - ret = of_get_gpio(np, 0); |
|---|
| 113 | | - if (ret < 0) { |
|---|
| 114 | | - dev_err(&pdev->dev, "failed to get GPIO from device tree\n"); |
|---|
| 115 | | - return ret; |
|---|
| 116 | | - } |
|---|
| 117 | | - data->gpio_pin = ret; |
|---|
| 118 | | - gpio_label = PPS_GPIO_NAME; |
|---|
| 119 | | - |
|---|
| 120 | | - if (of_get_property(np, "assert-falling-edge", NULL)) |
|---|
| 121 | | - data->assert_falling_edge = true; |
|---|
| 122 | | - } |
|---|
| 123 | | - |
|---|
| 124 | | - /* GPIO setup */ |
|---|
| 125 | | - ret = devm_gpio_request(&pdev->dev, data->gpio_pin, gpio_label); |
|---|
| 126 | | - if (ret) { |
|---|
| 127 | | - dev_err(&pdev->dev, "failed to request GPIO %u\n", |
|---|
| 128 | | - data->gpio_pin); |
|---|
| 129 | | - return ret; |
|---|
| 130 | | - } |
|---|
| 131 | | - |
|---|
| 132 | | - ret = gpio_direction_input(data->gpio_pin); |
|---|
| 133 | | - if (ret) { |
|---|
| 134 | | - dev_err(&pdev->dev, "failed to set pin direction\n"); |
|---|
| 135 | | - return -EINVAL; |
|---|
| 187 | + ret = pps_gpio_setup(pdev); |
|---|
| 188 | + if (ret) |
|---|
| 189 | + return -EINVAL; |
|---|
| 136 | 190 | } |
|---|
| 137 | 191 | |
|---|
| 138 | 192 | /* IRQ setup */ |
|---|
| 139 | | - ret = gpio_to_irq(data->gpio_pin); |
|---|
| 193 | + ret = gpiod_to_irq(data->gpio_pin); |
|---|
| 140 | 194 | if (ret < 0) { |
|---|
| 141 | 195 | dev_err(&pdev->dev, "failed to map GPIO to IRQ: %d\n", ret); |
|---|
| 142 | 196 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 152 | 206 | data->info.owner = THIS_MODULE; |
|---|
| 153 | 207 | snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d", |
|---|
| 154 | 208 | pdev->name, pdev->id); |
|---|
| 209 | + if (data->echo_pin) { |
|---|
| 210 | + data->info.echo = pps_gpio_echo; |
|---|
| 211 | + data->echo_timeout = msecs_to_jiffies(data->echo_active_ms); |
|---|
| 212 | + timer_setup(&data->echo_timer, pps_gpio_echo_timer_callback, 0); |
|---|
| 213 | + } |
|---|
| 155 | 214 | |
|---|
| 156 | 215 | /* register PPS source */ |
|---|
| 157 | 216 | pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT; |
|---|
| 158 | 217 | if (data->capture_clear) |
|---|
| 159 | 218 | pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR; |
|---|
| 160 | 219 | data->pps = pps_register_source(&data->info, pps_default_params); |
|---|
| 161 | | - if (data->pps == NULL) { |
|---|
| 220 | + if (IS_ERR(data->pps)) { |
|---|
| 162 | 221 | dev_err(&pdev->dev, "failed to register IRQ %d as PPS source\n", |
|---|
| 163 | 222 | data->irq); |
|---|
| 164 | | - return -EINVAL; |
|---|
| 223 | + return PTR_ERR(data->pps); |
|---|
| 165 | 224 | } |
|---|
| 166 | 225 | |
|---|
| 167 | 226 | /* register IRQ interrupt handler */ |
|---|
| .. | .. |
|---|
| 173 | 232 | return -EINVAL; |
|---|
| 174 | 233 | } |
|---|
| 175 | 234 | |
|---|
| 176 | | - platform_set_drvdata(pdev, data); |
|---|
| 177 | 235 | dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n", |
|---|
| 178 | 236 | data->irq); |
|---|
| 179 | 237 | |
|---|
| .. | .. |
|---|
| 185 | 243 | struct pps_gpio_device_data *data = platform_get_drvdata(pdev); |
|---|
| 186 | 244 | |
|---|
| 187 | 245 | pps_unregister_source(data->pps); |
|---|
| 246 | + if (data->echo_pin) { |
|---|
| 247 | + del_timer_sync(&data->echo_timer); |
|---|
| 248 | + /* reset echo pin in any case */ |
|---|
| 249 | + gpiod_set_value(data->echo_pin, 0); |
|---|
| 250 | + } |
|---|
| 188 | 251 | dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq); |
|---|
| 189 | 252 | return 0; |
|---|
| 190 | 253 | } |
|---|
| .. | .. |
|---|
| 209 | 272 | MODULE_AUTHOR("James Nuss <jamesnuss@nanometrics.ca>"); |
|---|
| 210 | 273 | MODULE_DESCRIPTION("Use GPIO pin as PPS source"); |
|---|
| 211 | 274 | MODULE_LICENSE("GPL"); |
|---|
| 212 | | -MODULE_VERSION("1.0.0"); |
|---|
| 275 | +MODULE_VERSION("1.2.0"); |
|---|