James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 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.h> |
| 35 | #include <linux/list.h> |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 36 | #include <linux/of_device.h> |
| 37 | #include <linux/of_gpio.h> |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 38 | |
| 39 | /* Info for each registered platform device */ |
| 40 | struct pps_gpio_device_data { |
| 41 | int irq; /* IRQ used as PPS source */ |
| 42 | struct pps_device *pps; /* PPS source device */ |
| 43 | struct pps_source_info info; /* PPS source information */ |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 44 | bool assert_falling_edge; |
| 45 | bool capture_clear; |
| 46 | unsigned int gpio_pin; |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * Report the PPS event |
| 51 | */ |
| 52 | |
| 53 | static irqreturn_t pps_gpio_irq_handler(int irq, void *data) |
| 54 | { |
| 55 | const struct pps_gpio_device_data *info; |
| 56 | struct pps_event_time ts; |
| 57 | int rising_edge; |
| 58 | |
| 59 | /* Get the time stamp first */ |
| 60 | pps_get_ts(&ts); |
| 61 | |
| 62 | info = data; |
| 63 | |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 64 | rising_edge = gpio_get_value(info->gpio_pin); |
| 65 | if ((rising_edge && !info->assert_falling_edge) || |
| 66 | (!rising_edge && info->assert_falling_edge)) |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 67 | pps_event(info->pps, &ts, PPS_CAPTUREASSERT, NULL); |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 68 | else if (info->capture_clear && |
| 69 | ((rising_edge && info->assert_falling_edge) || |
| 70 | (!rising_edge && !info->assert_falling_edge))) |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 71 | pps_event(info->pps, &ts, PPS_CAPTURECLEAR, NULL); |
| 72 | |
| 73 | return IRQ_HANDLED; |
| 74 | } |
| 75 | |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 76 | static unsigned long |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 77 | get_irqf_trigger_flags(const struct pps_gpio_device_data *data) |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 78 | { |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 79 | unsigned long flags = data->assert_falling_edge ? |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 80 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; |
| 81 | |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 82 | if (data->capture_clear) { |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 83 | flags |= ((flags & IRQF_TRIGGER_RISING) ? |
| 84 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING); |
| 85 | } |
| 86 | |
| 87 | return flags; |
| 88 | } |
| 89 | |
| 90 | static int pps_gpio_probe(struct platform_device *pdev) |
| 91 | { |
| 92 | struct pps_gpio_device_data *data; |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 93 | const char *gpio_label; |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 94 | int ret; |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 95 | int pps_default_params; |
| 96 | const struct pps_gpio_platform_data *pdata = pdev->dev.platform_data; |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 97 | struct device_node *np = pdev->dev.of_node; |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 98 | |
| 99 | /* allocate space for device info */ |
Julia Lawall | 507063b | 2013-02-27 17:05:44 -0800 | [diff] [blame] | 100 | data = devm_kzalloc(&pdev->dev, sizeof(struct pps_gpio_device_data), |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 101 | GFP_KERNEL); |
| 102 | if (!data) |
Jan Luebbe | 2a65182 | 2013-07-03 15:09:10 -0700 | [diff] [blame] | 103 | return -ENOMEM; |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 104 | |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 105 | if (pdata) { |
| 106 | data->gpio_pin = pdata->gpio_pin; |
| 107 | gpio_label = pdata->gpio_label; |
| 108 | |
| 109 | data->assert_falling_edge = pdata->assert_falling_edge; |
| 110 | data->capture_clear = pdata->capture_clear; |
| 111 | } 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; |
| 136 | } |
| 137 | |
| 138 | /* IRQ setup */ |
| 139 | ret = gpio_to_irq(data->gpio_pin); |
| 140 | if (ret < 0) { |
| 141 | dev_err(&pdev->dev, "failed to map GPIO to IRQ: %d\n", ret); |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | data->irq = ret; |
| 145 | |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 146 | /* initialize PPS specific parts of the bookkeeping data structure. */ |
| 147 | data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT | |
| 148 | PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC; |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 149 | if (data->capture_clear) |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 150 | data->info.mode |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR | |
| 151 | PPS_ECHOCLEAR; |
| 152 | data->info.owner = THIS_MODULE; |
| 153 | snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d", |
| 154 | pdev->name, pdev->id); |
| 155 | |
| 156 | /* register PPS source */ |
| 157 | pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT; |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 158 | if (data->capture_clear) |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 159 | pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR; |
| 160 | data->pps = pps_register_source(&data->info, pps_default_params); |
| 161 | if (data->pps == NULL) { |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 162 | dev_err(&pdev->dev, "failed to register IRQ %d as PPS source\n", |
| 163 | data->irq); |
Jan Luebbe | 2a65182 | 2013-07-03 15:09:10 -0700 | [diff] [blame] | 164 | return -EINVAL; |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 165 | } |
| 166 | |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 167 | /* register IRQ interrupt handler */ |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 168 | ret = devm_request_irq(&pdev->dev, data->irq, pps_gpio_irq_handler, |
| 169 | get_irqf_trigger_flags(data), data->info.name, data); |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 170 | if (ret) { |
| 171 | pps_unregister_source(data->pps); |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 172 | dev_err(&pdev->dev, "failed to acquire IRQ %d\n", data->irq); |
Jan Luebbe | 2a65182 | 2013-07-03 15:09:10 -0700 | [diff] [blame] | 173 | return -EINVAL; |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | platform_set_drvdata(pdev, data); |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 177 | dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n", |
| 178 | data->irq); |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 179 | |
| 180 | return 0; |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static int pps_gpio_remove(struct platform_device *pdev) |
| 184 | { |
| 185 | struct pps_gpio_device_data *data = platform_get_drvdata(pdev); |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 186 | |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 187 | pps_unregister_source(data->pps); |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 188 | dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq); |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 192 | static const struct of_device_id pps_gpio_dt_ids[] = { |
| 193 | { .compatible = "pps-gpio", }, |
| 194 | { /* sentinel */ } |
| 195 | }; |
| 196 | MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids); |
| 197 | |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 198 | static struct platform_driver pps_gpio_driver = { |
| 199 | .probe = pps_gpio_probe, |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 200 | .remove = pps_gpio_remove, |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 201 | .driver = { |
| 202 | .name = PPS_GPIO_NAME, |
Jan Luebbe | c5dbcf8 | 2013-07-03 15:09:12 -0700 | [diff] [blame] | 203 | .owner = THIS_MODULE, |
Sachin Kamat | 1a10bd9 | 2013-11-12 15:11:33 -0800 | [diff] [blame] | 204 | .of_match_table = pps_gpio_dt_ids, |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 205 | }, |
| 206 | }; |
| 207 | |
Jan Luebbe | 05212be | 2013-07-03 15:09:10 -0700 | [diff] [blame] | 208 | module_platform_driver(pps_gpio_driver); |
James Nuss | 16152045 | 2011-11-02 13:39:38 -0700 | [diff] [blame] | 209 | MODULE_AUTHOR("Ricardo Martins <rasm@fe.up.pt>"); |
| 210 | MODULE_AUTHOR("James Nuss <jamesnuss@nanometrics.ca>"); |
| 211 | MODULE_DESCRIPTION("Use GPIO pin as PPS source"); |
| 212 | MODULE_LICENSE("GPL"); |
| 213 | MODULE_VERSION("1.0.0"); |