blob: 5bd3bf093b5454a3e2fcb472ae903302f4499a0f [file] [log] [blame]
James Nuss161520452011-11-02 13:39:38 -07001/*
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>
36
37/* Info for each registered platform device */
38struct pps_gpio_device_data {
39 int irq; /* IRQ used as PPS source */
40 struct pps_device *pps; /* PPS source device */
41 struct pps_source_info info; /* PPS source information */
42 const struct pps_gpio_platform_data *pdata;
43};
44
45/*
46 * Report the PPS event
47 */
48
49static irqreturn_t pps_gpio_irq_handler(int irq, void *data)
50{
51 const struct pps_gpio_device_data *info;
52 struct pps_event_time ts;
53 int rising_edge;
54
55 /* Get the time stamp first */
56 pps_get_ts(&ts);
57
58 info = data;
59
60 rising_edge = gpio_get_value(info->pdata->gpio_pin);
61 if ((rising_edge && !info->pdata->assert_falling_edge) ||
62 (!rising_edge && info->pdata->assert_falling_edge))
63 pps_event(info->pps, &ts, PPS_CAPTUREASSERT, NULL);
64 else if (info->pdata->capture_clear &&
65 ((rising_edge && info->pdata->assert_falling_edge) ||
66 (!rising_edge && !info->pdata->assert_falling_edge)))
67 pps_event(info->pps, &ts, PPS_CAPTURECLEAR, NULL);
68
69 return IRQ_HANDLED;
70}
71
72static int pps_gpio_setup(struct platform_device *pdev)
73{
74 int ret;
75 const struct pps_gpio_platform_data *pdata = pdev->dev.platform_data;
76
Jan Luebbe2a651822013-07-03 15:09:10 -070077 ret = devm_gpio_request(&pdev->dev, pdata->gpio_pin, pdata->gpio_label);
James Nuss161520452011-11-02 13:39:38 -070078 if (ret) {
79 pr_warning("failed to request GPIO %u\n", pdata->gpio_pin);
80 return -EINVAL;
81 }
82
83 ret = gpio_direction_input(pdata->gpio_pin);
84 if (ret) {
85 pr_warning("failed to set pin direction\n");
James Nuss161520452011-11-02 13:39:38 -070086 return -EINVAL;
87 }
88
89 return 0;
90}
91
92static unsigned long
93get_irqf_trigger_flags(const struct pps_gpio_platform_data *pdata)
94{
95 unsigned long flags = pdata->assert_falling_edge ?
96 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
97
98 if (pdata->capture_clear) {
99 flags |= ((flags & IRQF_TRIGGER_RISING) ?
100 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
101 }
102
103 return flags;
104}
105
106static int pps_gpio_probe(struct platform_device *pdev)
107{
108 struct pps_gpio_device_data *data;
109 int irq;
110 int ret;
James Nuss161520452011-11-02 13:39:38 -0700111 int pps_default_params;
112 const struct pps_gpio_platform_data *pdata = pdev->dev.platform_data;
113
114
115 /* GPIO setup */
116 ret = pps_gpio_setup(pdev);
117 if (ret)
118 return -EINVAL;
119
120 /* IRQ setup */
121 irq = gpio_to_irq(pdata->gpio_pin);
122 if (irq < 0) {
123 pr_err("failed to map GPIO to IRQ: %d\n", irq);
Jan Luebbe2a651822013-07-03 15:09:10 -0700124 return -EINVAL;
James Nuss161520452011-11-02 13:39:38 -0700125 }
126
127 /* allocate space for device info */
Julia Lawall507063b2013-02-27 17:05:44 -0800128 data = devm_kzalloc(&pdev->dev, sizeof(struct pps_gpio_device_data),
129 GFP_KERNEL);
Jan Luebbe2a651822013-07-03 15:09:10 -0700130 if (data == NULL)
131 return -ENOMEM;
James Nuss161520452011-11-02 13:39:38 -0700132
133 /* initialize PPS specific parts of the bookkeeping data structure. */
134 data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
135 PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC;
136 if (pdata->capture_clear)
137 data->info.mode |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR |
138 PPS_ECHOCLEAR;
139 data->info.owner = THIS_MODULE;
140 snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d",
141 pdev->name, pdev->id);
142
143 /* register PPS source */
144 pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
145 if (pdata->capture_clear)
146 pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR;
147 data->pps = pps_register_source(&data->info, pps_default_params);
148 if (data->pps == NULL) {
James Nuss161520452011-11-02 13:39:38 -0700149 pr_err("failed to register IRQ %d as PPS source\n", irq);
Jan Luebbe2a651822013-07-03 15:09:10 -0700150 return -EINVAL;
James Nuss161520452011-11-02 13:39:38 -0700151 }
152
153 data->irq = irq;
154 data->pdata = pdata;
155
156 /* register IRQ interrupt handler */
Jan Luebbe2a651822013-07-03 15:09:10 -0700157 ret = devm_request_irq(&pdev->dev, irq, pps_gpio_irq_handler,
James Nuss161520452011-11-02 13:39:38 -0700158 get_irqf_trigger_flags(pdata), data->info.name, data);
159 if (ret) {
160 pps_unregister_source(data->pps);
James Nuss161520452011-11-02 13:39:38 -0700161 pr_err("failed to acquire IRQ %d\n", irq);
Jan Luebbe2a651822013-07-03 15:09:10 -0700162 return -EINVAL;
James Nuss161520452011-11-02 13:39:38 -0700163 }
164
165 platform_set_drvdata(pdev, data);
166 dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n", irq);
167
168 return 0;
James Nuss161520452011-11-02 13:39:38 -0700169}
170
171static int pps_gpio_remove(struct platform_device *pdev)
172{
173 struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
James Nuss161520452011-11-02 13:39:38 -0700174
175 platform_set_drvdata(pdev, NULL);
James Nuss161520452011-11-02 13:39:38 -0700176 pps_unregister_source(data->pps);
177 pr_info("removed IRQ %d as PPS source\n", data->irq);
James Nuss161520452011-11-02 13:39:38 -0700178 return 0;
179}
180
181static struct platform_driver pps_gpio_driver = {
182 .probe = pps_gpio_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800183 .remove = pps_gpio_remove,
James Nuss161520452011-11-02 13:39:38 -0700184 .driver = {
185 .name = PPS_GPIO_NAME,
186 .owner = THIS_MODULE
187 },
188};
189
Jan Luebbe05212be2013-07-03 15:09:10 -0700190module_platform_driver(pps_gpio_driver);
James Nuss161520452011-11-02 13:39:38 -0700191MODULE_AUTHOR("Ricardo Martins <rasm@fe.up.pt>");
192MODULE_AUTHOR("James Nuss <jamesnuss@nanometrics.ca>");
193MODULE_DESCRIPTION("Use GPIO pin as PPS source");
194MODULE_LICENSE("GPL");
195MODULE_VERSION("1.0.0");