About Kernel Documentation Linux Kernel Contact Linux Resources Linux Blog

Documentation / blackfin / gptimers-example.c


Based on kernel version 4.8. Page generated on 2016-10-06 23:10 EST.

1	/*
2	 * Simple gptimers example
3	 *	http://docs.blackfin.uclinux.org/doku.php?id=linux-kernel:drivers:gptimers
4	 *
5	 * Copyright 2007-2009 Analog Devices Inc.
6	 *
7	 * Licensed under the GPL-2 or later.
8	 */
9	
10	#include <linux/interrupt.h>
11	#include <linux/module.h>
12	
13	#include <asm/gptimers.h>
14	#include <asm/portmux.h>
15	
16	/* ... random driver includes ... */
17	
18	#define DRIVER_NAME "gptimer_example"
19	
20	#ifdef IRQ_TIMER5
21	#define SAMPLE_IRQ_TIMER IRQ_TIMER5
22	#else
23	#define SAMPLE_IRQ_TIMER IRQ_TIMER2
24	#endif
25	
26	struct gptimer_data {
27		uint32_t period, width;
28	};
29	static struct gptimer_data data;
30	
31	/* ... random driver state ... */
32	
33	static irqreturn_t gptimer_example_irq(int irq, void *dev_id)
34	{
35		struct gptimer_data *data = dev_id;
36	
37		/* make sure it was our timer which caused the interrupt */
38		if (!get_gptimer_intr(TIMER5_id))
39			return IRQ_NONE;
40	
41		/* read the width/period values that were captured for the waveform */
42		data->width = get_gptimer_pwidth(TIMER5_id);
43		data->period = get_gptimer_period(TIMER5_id);
44	
45		/* acknowledge the interrupt */
46		clear_gptimer_intr(TIMER5_id);
47	
48		/* tell the upper layers we took care of things */
49		return IRQ_HANDLED;
50	}
51	
52	/* ... random driver code ... */
53	
54	static int __init gptimer_example_init(void)
55	{
56		int ret;
57	
58		/* grab the peripheral pins */
59		ret = peripheral_request(P_TMR5, DRIVER_NAME);
60		if (ret) {
61			printk(KERN_NOTICE DRIVER_NAME ": peripheral request failed\n");
62			return ret;
63		}
64	
65		/* grab the IRQ for the timer */
66		ret = request_irq(SAMPLE_IRQ_TIMER, gptimer_example_irq,
67				IRQF_SHARED, DRIVER_NAME, &data);
68		if (ret) {
69			printk(KERN_NOTICE DRIVER_NAME ": IRQ request failed\n");
70			peripheral_free(P_TMR5);
71			return ret;
72		}
73	
74		/* setup the timer and enable it */
75		set_gptimer_config(TIMER5_id,
76				WDTH_CAP | PULSE_HI | PERIOD_CNT | IRQ_ENA);
77		enable_gptimers(TIMER5bit);
78	
79		return 0;
80	}
81	module_init(gptimer_example_init);
82	
83	static void __exit gptimer_example_exit(void)
84	{
85		disable_gptimers(TIMER5bit);
86		free_irq(SAMPLE_IRQ_TIMER, &data);
87		peripheral_free(P_TMR5);
88	}
89	module_exit(gptimer_example_exit);
90	
91	MODULE_LICENSE("BSD");
Hide Line Numbers


About Kernel Documentation Linux Kernel Contact Linux Resources Linux Blog