Linux Audio

Check our new training course

Loading...
v6.2
  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/mod_devicetable.h>
 16#include <linux/module.h>
 17#include <linux/platform_device.h>
 18#include <linux/slab.h>
 19#include <linux/pps_kernel.h>
 20#include <linux/gpio/consumer.h>
 
 21#include <linux/list.h>
 22#include <linux/property.h>
 23#include <linux/timer.h>
 24#include <linux/jiffies.h>
 25
 26/* Info for each registered platform device */
 27struct pps_gpio_device_data {
 28	int irq;			/* IRQ used as PPS source */
 29	struct pps_device *pps;		/* PPS source device */
 30	struct pps_source_info info;	/* PPS source information */
 31	struct gpio_desc *gpio_pin;	/* GPIO port descriptors */
 32	struct gpio_desc *echo_pin;
 33	struct timer_list echo_timer;	/* timer to reset echo active state */
 34	bool assert_falling_edge;
 35	bool capture_clear;
 36	unsigned int echo_active_ms;	/* PPS echo active duration */
 37	unsigned long echo_timeout;	/* timer timeout value in jiffies */
 38};
 39
 40/*
 41 * Report the PPS event
 42 */
 43
 44static irqreturn_t pps_gpio_irq_handler(int irq, void *data)
 45{
 46	const struct pps_gpio_device_data *info;
 47	struct pps_event_time ts;
 48	int rising_edge;
 49
 50	/* Get the time stamp first */
 51	pps_get_ts(&ts);
 52
 53	info = data;
 54
 55	rising_edge = gpiod_get_value(info->gpio_pin);
 56	if ((rising_edge && !info->assert_falling_edge) ||
 57			(!rising_edge && info->assert_falling_edge))
 58		pps_event(info->pps, &ts, PPS_CAPTUREASSERT, data);
 59	else if (info->capture_clear &&
 60			((rising_edge && info->assert_falling_edge) ||
 61			(!rising_edge && !info->assert_falling_edge)))
 62		pps_event(info->pps, &ts, PPS_CAPTURECLEAR, data);
 63
 64	return IRQ_HANDLED;
 65}
 66
 67/* This function will only be called when an ECHO GPIO is defined */
 68static void pps_gpio_echo(struct pps_device *pps, int event, void *data)
 69{
 70	/* add_timer() needs to write into info->echo_timer */
 71	struct pps_gpio_device_data *info = data;
 72
 73	switch (event) {
 74	case PPS_CAPTUREASSERT:
 75		if (pps->params.mode & PPS_ECHOASSERT)
 76			gpiod_set_value(info->echo_pin, 1);
 77		break;
 78
 79	case PPS_CAPTURECLEAR:
 80		if (pps->params.mode & PPS_ECHOCLEAR)
 81			gpiod_set_value(info->echo_pin, 1);
 82		break;
 83	}
 84
 85	/* fire the timer */
 86	if (info->pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) {
 87		info->echo_timer.expires = jiffies + info->echo_timeout;
 88		add_timer(&info->echo_timer);
 89	}
 90}
 91
 92/* Timer callback to reset the echo pin to the inactive state */
 93static void pps_gpio_echo_timer_callback(struct timer_list *t)
 94{
 95	const struct pps_gpio_device_data *info;
 96
 97	info = from_timer(info, t, echo_timer);
 98
 99	gpiod_set_value(info->echo_pin, 0);
100}
101
102static int pps_gpio_setup(struct device *dev)
103{
104	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
105	int ret;
106	u32 value;
107
108	data->gpio_pin = devm_gpiod_get(dev, NULL, GPIOD_IN);
109	if (IS_ERR(data->gpio_pin))
110		return dev_err_probe(dev, PTR_ERR(data->gpio_pin),
111				     "failed to request PPS GPIO\n");
112
113	data->assert_falling_edge =
114		device_property_read_bool(dev, "assert-falling-edge");
115
116	data->echo_pin = devm_gpiod_get_optional(dev, "echo", GPIOD_OUT_LOW);
117	if (IS_ERR(data->echo_pin))
118		return dev_err_probe(dev, PTR_ERR(data->echo_pin),
119				     "failed to request ECHO GPIO\n");
120
121	if (!data->echo_pin)
122		return 0;
123
124	ret = device_property_read_u32(dev, "echo-active-ms", &value);
125	if (ret) {
126		dev_err(dev, "failed to get echo-active-ms from FW\n");
127		return ret;
128	}
129
130	/* sanity check on echo_active_ms */
131	if (!value || value > 999) {
132		dev_err(dev, "echo-active-ms: %u - bad value from FW\n", value);
133		return -EINVAL;
134	}
135
136	data->echo_active_ms = value;
137
138	return 0;
139}
140
141static unsigned long
142get_irqf_trigger_flags(const struct pps_gpio_device_data *data)
143{
144	unsigned long flags = data->assert_falling_edge ?
145		IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
146
147	if (data->capture_clear) {
148		flags |= ((flags & IRQF_TRIGGER_RISING) ?
149				IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
150	}
151
152	return flags;
153}
154
155static int pps_gpio_probe(struct platform_device *pdev)
156{
157	struct pps_gpio_device_data *data;
158	struct device *dev = &pdev->dev;
159	int ret;
160	int pps_default_params;
 
 
161
162	/* allocate space for device info */
163	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
 
164	if (!data)
165		return -ENOMEM;
166
167	dev_set_drvdata(dev, data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
169	/* GPIO setup */
170	ret = pps_gpio_setup(dev);
171	if (ret)
 
 
172		return ret;
 
 
 
 
 
 
 
173
174	/* IRQ setup */
175	ret = gpiod_to_irq(data->gpio_pin);
176	if (ret < 0) {
177		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
178		return -EINVAL;
179	}
180	data->irq = ret;
181
182	/* initialize PPS specific parts of the bookkeeping data structure. */
183	data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
184		PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC;
185	if (data->capture_clear)
186		data->info.mode |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR |
187			PPS_ECHOCLEAR;
188	data->info.owner = THIS_MODULE;
189	snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d",
190		 pdev->name, pdev->id);
191	if (data->echo_pin) {
192		data->info.echo = pps_gpio_echo;
193		data->echo_timeout = msecs_to_jiffies(data->echo_active_ms);
194		timer_setup(&data->echo_timer, pps_gpio_echo_timer_callback, 0);
195	}
196
197	/* register PPS source */
198	pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
199	if (data->capture_clear)
200		pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR;
201	data->pps = pps_register_source(&data->info, pps_default_params);
202	if (IS_ERR(data->pps)) {
203		dev_err(dev, "failed to register IRQ %d as PPS source\n",
204			data->irq);
205		return PTR_ERR(data->pps);
206	}
207
208	/* register IRQ interrupt handler */
209	ret = devm_request_irq(dev, data->irq, pps_gpio_irq_handler,
210			get_irqf_trigger_flags(data), data->info.name, data);
211	if (ret) {
212		pps_unregister_source(data->pps);
213		dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
214		return -EINVAL;
215	}
216
 
217	dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n",
218		 data->irq);
219
220	return 0;
221}
222
223static int pps_gpio_remove(struct platform_device *pdev)
224{
225	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
226
227	pps_unregister_source(data->pps);
228	del_timer_sync(&data->echo_timer);
229	/* reset echo pin in any case */
230	gpiod_set_value(data->echo_pin, 0);
231	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
232	return 0;
233}
234
235static const struct of_device_id pps_gpio_dt_ids[] = {
236	{ .compatible = "pps-gpio", },
237	{ /* sentinel */ }
238};
239MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
240
241static struct platform_driver pps_gpio_driver = {
242	.probe		= pps_gpio_probe,
243	.remove		= pps_gpio_remove,
244	.driver		= {
245		.name	= PPS_GPIO_NAME,
 
246		.of_match_table	= pps_gpio_dt_ids,
247	},
248};
249
250module_platform_driver(pps_gpio_driver);
251MODULE_AUTHOR("Ricardo Martins <rasm@fe.up.pt>");
252MODULE_AUTHOR("James Nuss <jamesnuss@nanometrics.ca>");
253MODULE_DESCRIPTION("Use GPIO pin as PPS source");
254MODULE_LICENSE("GPL");
255MODULE_VERSION("1.2.0");
v3.15
 
  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>
 36#include <linux/of_device.h>
 37#include <linux/of_gpio.h>
 
 38
 39/* Info for each registered platform device */
 40struct 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 */
 
 
 
 44	bool assert_falling_edge;
 45	bool capture_clear;
 46	unsigned int gpio_pin;
 
 47};
 48
 49/*
 50 * Report the PPS event
 51 */
 52
 53static 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
 64	rising_edge = gpio_get_value(info->gpio_pin);
 65	if ((rising_edge && !info->assert_falling_edge) ||
 66			(!rising_edge && info->assert_falling_edge))
 67		pps_event(info->pps, &ts, PPS_CAPTUREASSERT, NULL);
 68	else if (info->capture_clear &&
 69			((rising_edge && info->assert_falling_edge) ||
 70			 (!rising_edge && !info->assert_falling_edge)))
 71		pps_event(info->pps, &ts, PPS_CAPTURECLEAR, NULL);
 72
 73	return IRQ_HANDLED;
 74}
 75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 76static unsigned long
 77get_irqf_trigger_flags(const struct pps_gpio_device_data *data)
 78{
 79	unsigned long flags = data->assert_falling_edge ?
 80		IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
 81
 82	if (data->capture_clear) {
 83		flags |= ((flags & IRQF_TRIGGER_RISING) ?
 84				IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
 85	}
 86
 87	return flags;
 88}
 89
 90static int pps_gpio_probe(struct platform_device *pdev)
 91{
 92	struct pps_gpio_device_data *data;
 93	const char *gpio_label;
 94	int ret;
 95	int pps_default_params;
 96	const struct pps_gpio_platform_data *pdata = pdev->dev.platform_data;
 97	struct device_node *np = pdev->dev.of_node;
 98
 99	/* allocate space for device info */
100	data = devm_kzalloc(&pdev->dev, sizeof(struct pps_gpio_device_data),
101			GFP_KERNEL);
102	if (!data)
103		return -ENOMEM;
104
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
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;
149	if (data->capture_clear)
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;
158	if (data->capture_clear)
159		pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR;
160	data->pps = pps_register_source(&data->info, pps_default_params);
161	if (data->pps == NULL) {
162		dev_err(&pdev->dev, "failed to register IRQ %d as PPS source\n",
163			data->irq);
164		return -EINVAL;
165	}
166
167	/* register IRQ interrupt handler */
168	ret = devm_request_irq(&pdev->dev, data->irq, pps_gpio_irq_handler,
169			get_irqf_trigger_flags(data), data->info.name, data);
170	if (ret) {
171		pps_unregister_source(data->pps);
172		dev_err(&pdev->dev, "failed to acquire IRQ %d\n", data->irq);
173		return -EINVAL;
174	}
175
176	platform_set_drvdata(pdev, data);
177	dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n",
178		 data->irq);
179
180	return 0;
181}
182
183static int pps_gpio_remove(struct platform_device *pdev)
184{
185	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
186
187	pps_unregister_source(data->pps);
 
 
 
188	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
189	return 0;
190}
191
192static const struct of_device_id pps_gpio_dt_ids[] = {
193	{ .compatible = "pps-gpio", },
194	{ /* sentinel */ }
195};
196MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
197
198static struct platform_driver pps_gpio_driver = {
199	.probe		= pps_gpio_probe,
200	.remove		= pps_gpio_remove,
201	.driver		= {
202		.name	= PPS_GPIO_NAME,
203		.owner	= THIS_MODULE,
204		.of_match_table	= pps_gpio_dt_ids,
205	},
206};
207
208module_platform_driver(pps_gpio_driver);
209MODULE_AUTHOR("Ricardo Martins <rasm@fe.up.pt>");
210MODULE_AUTHOR("James Nuss <jamesnuss@nanometrics.ca>");
211MODULE_DESCRIPTION("Use GPIO pin as PPS source");
212MODULE_LICENSE("GPL");
213MODULE_VERSION("1.0.0");