xref: /openbmc/linux/drivers/pps/clients/pps-gpio.c (revision 8adfd166)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
216152045SJames Nuss /*
316152045SJames Nuss  * pps-gpio.c -- PPS client driver using GPIO
416152045SJames Nuss  *
516152045SJames Nuss  * Copyright (C) 2010 Ricardo Martins <rasm@fe.up.pt>
616152045SJames Nuss  * Copyright (C) 2011 James Nuss <jamesnuss@nanometrics.ca>
716152045SJames Nuss  */
816152045SJames Nuss 
916152045SJames Nuss #define PPS_GPIO_NAME "pps-gpio"
1016152045SJames Nuss #define pr_fmt(fmt) PPS_GPIO_NAME ": " fmt
1116152045SJames Nuss 
1216152045SJames Nuss #include <linux/init.h>
1316152045SJames Nuss #include <linux/kernel.h>
1416152045SJames Nuss #include <linux/interrupt.h>
1528d03251SAndy Shevchenko #include <linux/mod_devicetable.h>
1616152045SJames Nuss #include <linux/module.h>
1716152045SJames Nuss #include <linux/platform_device.h>
1816152045SJames Nuss #include <linux/slab.h>
1916152045SJames Nuss #include <linux/pps_kernel.h>
204461d651STom Burkart #include <linux/gpio/consumer.h>
2116152045SJames Nuss #include <linux/list.h>
2228d03251SAndy Shevchenko #include <linux/property.h>
234c69add4STom Burkart #include <linux/timer.h>
244c69add4STom Burkart #include <linux/jiffies.h>
2516152045SJames Nuss 
2616152045SJames Nuss /* Info for each registered platform device */
2716152045SJames Nuss struct pps_gpio_device_data {
2816152045SJames Nuss 	int irq;			/* IRQ used as PPS source */
2916152045SJames Nuss 	struct pps_device *pps;		/* PPS source device */
3016152045SJames Nuss 	struct pps_source_info info;	/* PPS source information */
314461d651STom Burkart 	struct gpio_desc *gpio_pin;	/* GPIO port descriptors */
324c69add4STom Burkart 	struct gpio_desc *echo_pin;
334c69add4STom Burkart 	struct timer_list echo_timer;	/* timer to reset echo active state */
34c5dbcf8bSJan Luebbe 	bool assert_falling_edge;
35c5dbcf8bSJan Luebbe 	bool capture_clear;
364c69add4STom Burkart 	unsigned int echo_active_ms;	/* PPS echo active duration */
374c69add4STom Burkart 	unsigned long echo_timeout;	/* timer timeout value in jiffies */
3816152045SJames Nuss };
3916152045SJames Nuss 
4016152045SJames Nuss /*
4116152045SJames Nuss  * Report the PPS event
4216152045SJames Nuss  */
4316152045SJames Nuss 
pps_gpio_irq_handler(int irq,void * data)4416152045SJames Nuss static irqreturn_t pps_gpio_irq_handler(int irq, void *data)
4516152045SJames Nuss {
4616152045SJames Nuss 	const struct pps_gpio_device_data *info;
4716152045SJames Nuss 	struct pps_event_time ts;
4816152045SJames Nuss 	int rising_edge;
4916152045SJames Nuss 
5016152045SJames Nuss 	/* Get the time stamp first */
5116152045SJames Nuss 	pps_get_ts(&ts);
5216152045SJames Nuss 
5316152045SJames Nuss 	info = data;
5416152045SJames Nuss 
554461d651STom Burkart 	rising_edge = gpiod_get_value(info->gpio_pin);
56c5dbcf8bSJan Luebbe 	if ((rising_edge && !info->assert_falling_edge) ||
57c5dbcf8bSJan Luebbe 			(!rising_edge && info->assert_falling_edge))
584c69add4STom Burkart 		pps_event(info->pps, &ts, PPS_CAPTUREASSERT, data);
59c5dbcf8bSJan Luebbe 	else if (info->capture_clear &&
60c5dbcf8bSJan Luebbe 			((rising_edge && info->assert_falling_edge) ||
61c5dbcf8bSJan Luebbe 			(!rising_edge && !info->assert_falling_edge)))
624c69add4STom Burkart 		pps_event(info->pps, &ts, PPS_CAPTURECLEAR, data);
6316152045SJames Nuss 
6416152045SJames Nuss 	return IRQ_HANDLED;
6516152045SJames Nuss }
6616152045SJames Nuss 
674c69add4STom Burkart /* This function will only be called when an ECHO GPIO is defined */
pps_gpio_echo(struct pps_device * pps,int event,void * data)684c69add4STom Burkart static void pps_gpio_echo(struct pps_device *pps, int event, void *data)
694c69add4STom Burkart {
704c69add4STom Burkart 	/* add_timer() needs to write into info->echo_timer */
714c69add4STom Burkart 	struct pps_gpio_device_data *info = data;
724c69add4STom Burkart 
734c69add4STom Burkart 	switch (event) {
744c69add4STom Burkart 	case PPS_CAPTUREASSERT:
754c69add4STom Burkart 		if (pps->params.mode & PPS_ECHOASSERT)
764c69add4STom Burkart 			gpiod_set_value(info->echo_pin, 1);
774c69add4STom Burkart 		break;
784c69add4STom Burkart 
794c69add4STom Burkart 	case PPS_CAPTURECLEAR:
804c69add4STom Burkart 		if (pps->params.mode & PPS_ECHOCLEAR)
814c69add4STom Burkart 			gpiod_set_value(info->echo_pin, 1);
824c69add4STom Burkart 		break;
834c69add4STom Burkart 	}
844c69add4STom Burkart 
854c69add4STom Burkart 	/* fire the timer */
864c69add4STom Burkart 	if (info->pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) {
874c69add4STom Burkart 		info->echo_timer.expires = jiffies + info->echo_timeout;
884c69add4STom Burkart 		add_timer(&info->echo_timer);
894c69add4STom Burkart 	}
904c69add4STom Burkart }
914c69add4STom Burkart 
924c69add4STom Burkart /* Timer callback to reset the echo pin to the inactive state */
pps_gpio_echo_timer_callback(struct timer_list * t)934c69add4STom Burkart static void pps_gpio_echo_timer_callback(struct timer_list *t)
944c69add4STom Burkart {
954c69add4STom Burkart 	const struct pps_gpio_device_data *info;
964c69add4STom Burkart 
974c69add4STom Burkart 	info = from_timer(info, t, echo_timer);
984c69add4STom Burkart 
994c69add4STom Burkart 	gpiod_set_value(info->echo_pin, 0);
1004c69add4STom Burkart }
1014c69add4STom Burkart 
pps_gpio_setup(struct device * dev)102162a5deaSAndy Shevchenko static int pps_gpio_setup(struct device *dev)
1034461d651STom Burkart {
104162a5deaSAndy Shevchenko 	struct pps_gpio_device_data *data = dev_get_drvdata(dev);
1054c69add4STom Burkart 	int ret;
1064c69add4STom Burkart 	u32 value;
1074461d651STom Burkart 
108162a5deaSAndy Shevchenko 	data->gpio_pin = devm_gpiod_get(dev, NULL, GPIOD_IN);
1091a8fc4f7SAndy Shevchenko 	if (IS_ERR(data->gpio_pin))
110162a5deaSAndy Shevchenko 		return dev_err_probe(dev, PTR_ERR(data->gpio_pin),
1114461d651STom Burkart 				     "failed to request PPS GPIO\n");
1124461d651STom Burkart 
1136b3bc828SAndy Shevchenko 	data->assert_falling_edge =
1146b3bc828SAndy Shevchenko 		device_property_read_bool(dev, "assert-falling-edge");
1156b3bc828SAndy Shevchenko 
116162a5deaSAndy Shevchenko 	data->echo_pin = devm_gpiod_get_optional(dev, "echo", GPIOD_OUT_LOW);
1171a8fc4f7SAndy Shevchenko 	if (IS_ERR(data->echo_pin))
118162a5deaSAndy Shevchenko 		return dev_err_probe(dev, PTR_ERR(data->echo_pin),
1191a8fc4f7SAndy Shevchenko 				     "failed to request ECHO GPIO\n");
1204c69add4STom Burkart 
1216b3bc828SAndy Shevchenko 	if (!data->echo_pin)
1226b3bc828SAndy Shevchenko 		return 0;
1236b3bc828SAndy Shevchenko 
124162a5deaSAndy Shevchenko 	ret = device_property_read_u32(dev, "echo-active-ms", &value);
1254c69add4STom Burkart 	if (ret) {
126162a5deaSAndy Shevchenko 		dev_err(dev, "failed to get echo-active-ms from FW\n");
1274c69add4STom Burkart 		return ret;
1284c69add4STom Burkart 	}
1296b3bc828SAndy Shevchenko 
1304c69add4STom Burkart 	/* sanity check on echo_active_ms */
1316b3bc828SAndy Shevchenko 	if (!value || value > 999) {
1326b3bc828SAndy Shevchenko 		dev_err(dev, "echo-active-ms: %u - bad value from FW\n", value);
1334c69add4STom Burkart 		return -EINVAL;
1344c69add4STom Burkart 	}
1354c69add4STom Burkart 
1366b3bc828SAndy Shevchenko 	data->echo_active_ms = value;
1376b3bc828SAndy Shevchenko 
1384461d651STom Burkart 	return 0;
1394461d651STom Burkart }
1404461d651STom Burkart 
14116152045SJames Nuss static unsigned long
get_irqf_trigger_flags(const struct pps_gpio_device_data * data)142c5dbcf8bSJan Luebbe get_irqf_trigger_flags(const struct pps_gpio_device_data *data)
14316152045SJames Nuss {
144c5dbcf8bSJan Luebbe 	unsigned long flags = data->assert_falling_edge ?
14516152045SJames Nuss 		IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
14616152045SJames Nuss 
147c5dbcf8bSJan Luebbe 	if (data->capture_clear) {
14816152045SJames Nuss 		flags |= ((flags & IRQF_TRIGGER_RISING) ?
14916152045SJames Nuss 				IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
15016152045SJames Nuss 	}
15116152045SJames Nuss 
15216152045SJames Nuss 	return flags;
15316152045SJames Nuss }
15416152045SJames Nuss 
pps_gpio_probe(struct platform_device * pdev)15516152045SJames Nuss static int pps_gpio_probe(struct platform_device *pdev)
15616152045SJames Nuss {
15716152045SJames Nuss 	struct pps_gpio_device_data *data;
158162a5deaSAndy Shevchenko 	struct device *dev = &pdev->dev;
15916152045SJames Nuss 	int ret;
16016152045SJames Nuss 	int pps_default_params;
16116152045SJames Nuss 
16216152045SJames Nuss 	/* allocate space for device info */
163162a5deaSAndy Shevchenko 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
164c5dbcf8bSJan Luebbe 	if (!data)
1652a651822SJan Luebbe 		return -ENOMEM;
166162a5deaSAndy Shevchenko 
167162a5deaSAndy Shevchenko 	dev_set_drvdata(dev, data);
16816152045SJames Nuss 
1694461d651STom Burkart 	/* GPIO setup */
170162a5deaSAndy Shevchenko 	ret = pps_gpio_setup(dev);
1714461d651STom Burkart 	if (ret)
172*8adfd166SRobert Hancock 		return ret;
173c5dbcf8bSJan Luebbe 
174c5dbcf8bSJan Luebbe 	/* IRQ setup */
1754461d651STom Burkart 	ret = gpiod_to_irq(data->gpio_pin);
176c5dbcf8bSJan Luebbe 	if (ret < 0) {
177162a5deaSAndy Shevchenko 		dev_err(dev, "failed to map GPIO to IRQ: %d\n", ret);
178c5dbcf8bSJan Luebbe 		return -EINVAL;
179c5dbcf8bSJan Luebbe 	}
180c5dbcf8bSJan Luebbe 	data->irq = ret;
181c5dbcf8bSJan Luebbe 
18216152045SJames Nuss 	/* initialize PPS specific parts of the bookkeeping data structure. */
18316152045SJames Nuss 	data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
18416152045SJames Nuss 		PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC;
185c5dbcf8bSJan Luebbe 	if (data->capture_clear)
18616152045SJames Nuss 		data->info.mode |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR |
18716152045SJames Nuss 			PPS_ECHOCLEAR;
18816152045SJames Nuss 	data->info.owner = THIS_MODULE;
18916152045SJames Nuss 	snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d",
19016152045SJames Nuss 		 pdev->name, pdev->id);
1914c69add4STom Burkart 	if (data->echo_pin) {
1924c69add4STom Burkart 		data->info.echo = pps_gpio_echo;
1934c69add4STom Burkart 		data->echo_timeout = msecs_to_jiffies(data->echo_active_ms);
1944c69add4STom Burkart 		timer_setup(&data->echo_timer, pps_gpio_echo_timer_callback, 0);
1954c69add4STom Burkart 	}
19616152045SJames Nuss 
19716152045SJames Nuss 	/* register PPS source */
19816152045SJames Nuss 	pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
199c5dbcf8bSJan Luebbe 	if (data->capture_clear)
20016152045SJames Nuss 		pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR;
20116152045SJames Nuss 	data->pps = pps_register_source(&data->info, pps_default_params);
2023b1ad360SYueHaibing 	if (IS_ERR(data->pps)) {
203162a5deaSAndy Shevchenko 		dev_err(dev, "failed to register IRQ %d as PPS source\n",
204c5dbcf8bSJan Luebbe 			data->irq);
2053b1ad360SYueHaibing 		return PTR_ERR(data->pps);
20616152045SJames Nuss 	}
20716152045SJames Nuss 
20816152045SJames Nuss 	/* register IRQ interrupt handler */
209162a5deaSAndy Shevchenko 	ret = devm_request_irq(dev, data->irq, pps_gpio_irq_handler,
210c5dbcf8bSJan Luebbe 			get_irqf_trigger_flags(data), data->info.name, data);
21116152045SJames Nuss 	if (ret) {
21216152045SJames Nuss 		pps_unregister_source(data->pps);
213162a5deaSAndy Shevchenko 		dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
2142a651822SJan Luebbe 		return -EINVAL;
21516152045SJames Nuss 	}
21616152045SJames Nuss 
217c5dbcf8bSJan Luebbe 	dev_info(data->pps->dev, "Registered IRQ %d as PPS source\n",
218c5dbcf8bSJan Luebbe 		 data->irq);
21916152045SJames Nuss 
22016152045SJames Nuss 	return 0;
22116152045SJames Nuss }
22216152045SJames Nuss 
pps_gpio_remove(struct platform_device * pdev)22316152045SJames Nuss static int pps_gpio_remove(struct platform_device *pdev)
22416152045SJames Nuss {
22516152045SJames Nuss 	struct pps_gpio_device_data *data = platform_get_drvdata(pdev);
22616152045SJames Nuss 
22716152045SJames Nuss 	pps_unregister_source(data->pps);
2284c69add4STom Burkart 	del_timer_sync(&data->echo_timer);
2294c69add4STom Burkart 	/* reset echo pin in any case */
2304c69add4STom Burkart 	gpiod_set_value(data->echo_pin, 0);
231c5dbcf8bSJan Luebbe 	dev_info(&pdev->dev, "removed IRQ %d as PPS source\n", data->irq);
23216152045SJames Nuss 	return 0;
23316152045SJames Nuss }
23416152045SJames Nuss 
235c5dbcf8bSJan Luebbe static const struct of_device_id pps_gpio_dt_ids[] = {
236c5dbcf8bSJan Luebbe 	{ .compatible = "pps-gpio", },
237c5dbcf8bSJan Luebbe 	{ /* sentinel */ }
238c5dbcf8bSJan Luebbe };
239c5dbcf8bSJan Luebbe MODULE_DEVICE_TABLE(of, pps_gpio_dt_ids);
240c5dbcf8bSJan Luebbe 
24116152045SJames Nuss static struct platform_driver pps_gpio_driver = {
24216152045SJames Nuss 	.probe		= pps_gpio_probe,
2430fe763c5SGreg Kroah-Hartman 	.remove		= pps_gpio_remove,
24416152045SJames Nuss 	.driver		= {
24516152045SJames Nuss 		.name	= PPS_GPIO_NAME,
2461a10bd94SSachin Kamat 		.of_match_table	= pps_gpio_dt_ids,
24716152045SJames Nuss 	},
24816152045SJames Nuss };
24916152045SJames Nuss 
25005212be3SJan Luebbe module_platform_driver(pps_gpio_driver);
25116152045SJames Nuss MODULE_AUTHOR("Ricardo Martins <rasm@fe.up.pt>");
25216152045SJames Nuss MODULE_AUTHOR("James Nuss <jamesnuss@nanometrics.ca>");
25316152045SJames Nuss MODULE_DESCRIPTION("Use GPIO pin as PPS source");
25416152045SJames Nuss MODULE_LICENSE("GPL");
2554c69add4STom Burkart MODULE_VERSION("1.2.0");
256