File Index
Files and directories
Sign in
FILE

pps-gpio.c

File preview with syntax highlighting.

linux-6.19.14/drivers/pps/clients/pps-gpio.c C
  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	/* Small trick to bypass the check on edge's direction when capture_clear is unset */
 56	rising_edge = info->capture_clear ?
 57		      gpiod_get_value(info->gpio_pin) : !info->assert_falling_edge;
 58	if ((rising_edge && !info->assert_falling_edge) ||
 59			(!rising_edge && info->assert_falling_edge))
 60		pps_event(info->pps, &ts, PPS_CAPTUREASSERT, data);
 61	else if (info->capture_clear &&
 62			((rising_edge && info->assert_falling_edge) ||
 63			(!rising_edge && !info->assert_falling_edge)))
 64		pps_event(info->pps, &ts, PPS_CAPTURECLEAR, data);
 65	else
 66		dev_warn_ratelimited(&info->pps->dev, "IRQ did not trigger any PPS event\n");
 67
 68	return IRQ_HANDLED;
 69}
 70
 71/* This function will only be called when an ECHO GPIO is defined */
 72static void pps_gpio_echo(struct pps_device *pps, int event, void *data)
 73{
 74	/* add_timer() needs to write into info->echo_timer */
 75	struct pps_gpio_device_data *info = data;
 76
 77	switch (event) {
 78	case PPS_CAPTUREASSERT:
 79		if (pps->params.mode & PPS_ECHOASSERT)
 80			gpiod_set_value(info->echo_pin, 1);
 81		break;
 82
 83	case PPS_CAPTURECLEAR:
 84		if (pps->params.mode & PPS_ECHOCLEAR)
 85			gpiod_set_value(info->echo_pin, 1);
 86		break;
 87	}
 88
 89	/* fire the timer */
 90	if (info->pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) {
 91		info->echo_timer.expires = jiffies + info->echo_timeout;
 92		add_timer(&info->echo_timer);
 93	}
 94}
 95
 96/* Timer callback to reset the echo pin to the inactive state */
 97static void pps_gpio_echo_timer_callback(struct timer_list *t)
 98{
 99	const struct pps_gpio_device_data *info;
100
101	info = timer_container_of(info, t, echo_timer);
102
103	gpiod_set_value(info->echo_pin, 0);
104}
105
106static int pps_gpio_setup(struct device *dev)
107{
108	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
109	int ret;
110	u32 value;
111
112	data->gpio_pin = devm_gpiod_get(dev, NULL, GPIOD_IN);
113	if (IS_ERR(data->gpio_pin))
114		return dev_err_probe(dev, PTR_ERR(data->gpio_pin),
115				     "failed to request PPS GPIO\n");
116
117	data->assert_falling_edge =
118		device_property_read_bool(dev, "assert-falling-edge");
119
120	data->echo_pin = devm_gpiod_get_optional(dev, "echo", GPIOD_OUT_LOW);
121	if (IS_ERR(data->echo_pin))
122		return dev_err_probe(dev, PTR_ERR(data->echo_pin),
123				     "failed to request ECHO GPIO\n");
124
125	if (!data->echo_pin)
126		return 0;
127
128	ret = device_property_read_u32(dev, "echo-active-ms", &value);
129	if (ret) {
130		dev_err(dev, "failed to get echo-active-ms from FW\n");
131		return ret;
132	}
133
134	/* sanity check on echo_active_ms */
135	if (!value || value > 999) {
136		dev_err(dev, "echo-active-ms: %u - bad value from FW\n", value);
137		return -EINVAL;
138	}
139
140	data->echo_active_ms = value;
141
142	return 0;
143}
144
145static unsigned long
146get_irqf_trigger_flags(const struct pps_gpio_device_data *data)
147{
148	unsigned long flags = data->assert_falling_edge ?
149		IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
150
151	if (data->capture_clear) {
152		flags |= ((flags & IRQF_TRIGGER_RISING) ?
153				IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
154	}
155
156	return flags;
157}
158
159static int pps_gpio_probe(struct platform_device *pdev)
160{
161	struct pps_gpio_device_data *data;
162	struct device *dev = &pdev->dev;
163	int ret;
164	int pps_default_params;
165
166	/* allocate space for device info */
167	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
168	if (!data)
169		return -ENOMEM;
170
171	dev_set_drvdata(dev, data);
172
173	/* GPIO setup */
174	ret = pps_gpio_setup(dev);
175	if (ret)
176		return ret;
177
178	/* IRQ setup */
179	ret = gpiod_to_irq(data->gpio_pin);
180	if (ret < 0) {
181		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
182		return -EINVAL;
183	}
184	data->irq = ret;
185
186	/* initialize PPS specific parts of the bookkeeping data structure. */
187	data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
188		PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC;
189	if (data->capture_clear)
190		data->info.mode |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR |
191			PPS_ECHOCLEAR;
192	data->info.owner = THIS_MODULE;
193	snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d",
194		 pdev->name, pdev->id);
195	if (data->echo_pin) {
196		data->info.echo = pps_gpio_echo;
197		data->echo_timeout = msecs_to_jiffies(data->echo_active_ms);
198		timer_setup(&data->echo_timer, pps_gpio_echo_timer_callback, 0);
199	}
200
201	/* register PPS source */
202	pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
203	if (data->capture_clear)
204		pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR;
205	data->pps = pps_register_source(&data->info, pps_default_params);
206	if (IS_ERR(data->pps)) {
207		dev_err(dev, "failed to register IRQ %d as PPS source\n",
208			data->irq);
209		return PTR_ERR(data->pps);
210	}
211
212	/* register IRQ interrupt handler */
213	ret = request_irq(data->irq, pps_gpio_irq_handler,
214			  get_irqf_trigger_flags(data), data->info.name, data);
215	if (ret) {
216		pps_unregister_source(data->pps);
217		dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
218		return -EINVAL;
219	}
220
221	dev_dbg(&data->pps->dev, "Registered IRQ %d as PPS source\n",
222		data->irq);
223
224	return 0;
225}
226
227static void pps_gpio_remove(struct platform_device *pdev)
228{
229	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
230
231	free_irq(data->irq, data);
232	pps_unregister_source(data->pps);
233	timer_delete_sync(&data->echo_timer);
234	/* reset echo pin in any case */
235	gpiod_set_value(data->echo_pin, 0);
236	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
237}
238
239static const struct of_device_id pps_gpio_dt_ids[] = {
240	{ .compatible = "pps-gpio", },
241	{ /* sentinel */ }
242};
243MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
244
245static struct platform_driver pps_gpio_driver = {
246	.probe		= pps_gpio_probe,
247	.remove		= pps_gpio_remove,
248	.driver		= {
249		.name	= PPS_GPIO_NAME,
250		.of_match_table	= pps_gpio_dt_ids,
251	},
252};
253
254module_platform_driver(pps_gpio_driver);
255MODULE_AUTHOR("Ricardo Martins <rasm@fe.up.pt>");
256MODULE_AUTHOR("James Nuss <jamesnuss@nanometrics.ca>");
257MODULE_DESCRIPTION("Use GPIO pin as PPS source");
258MODULE_LICENSE("GPL");
259MODULE_VERSION("1.2.0");
~/ Read-only file preview.