linux/Documentation/blackfin/gptimers-example.c
<<
>>
Prefs
   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
  20struct gptimer_data {
  21        uint32_t period, width;
  22};
  23static struct gptimer_data data;
  24
  25/* ... random driver state ... */
  26
  27static irqreturn_t gptimer_example_irq(int irq, void *dev_id)
  28{
  29        struct gptimer_data *data = dev_id;
  30
  31        /* make sure it was our timer which caused the interrupt */
  32        if (!get_gptimer_intr(TIMER5_id))
  33                return IRQ_NONE;
  34
  35        /* read the width/period values that were captured for the waveform */
  36        data->width = get_gptimer_pwidth(TIMER5_id);
  37        data->period = get_gptimer_period(TIMER5_id);
  38
  39        /* acknowledge the interrupt */
  40        clear_gptimer_intr(TIMER5_id);
  41
  42        /* tell the upper layers we took care of things */
  43        return IRQ_HANDLED;
  44}
  45
  46/* ... random driver code ... */
  47
  48static int __init gptimer_example_init(void)
  49{
  50        int ret;
  51
  52        /* grab the peripheral pins */
  53        ret = peripheral_request(P_TMR5, DRIVER_NAME);
  54        if (ret) {
  55                printk(KERN_NOTICE DRIVER_NAME ": peripheral request failed\n");
  56                return ret;
  57        }
  58
  59        /* grab the IRQ for the timer */
  60        ret = request_irq(IRQ_TIMER5, gptimer_example_irq, IRQF_SHARED, DRIVER_NAME, &data);
  61        if (ret) {
  62                printk(KERN_NOTICE DRIVER_NAME ": IRQ request failed\n");
  63                peripheral_free(P_TMR5);
  64                return ret;
  65        }
  66
  67        /* setup the timer and enable it */
  68        set_gptimer_config(TIMER5_id, WDTH_CAP | PULSE_HI | PERIOD_CNT | IRQ_ENA);
  69        enable_gptimers(TIMER5bit);
  70
  71        return 0;
  72}
  73module_init(gptimer_example_init);
  74
  75static void __exit gptimer_example_exit(void)
  76{
  77        disable_gptimers(TIMER5bit);
  78        free_irq(IRQ_TIMER5, &data);
  79        peripheral_free(P_TMR5);
  80}
  81module_exit(gptimer_example_exit);
  82
  83MODULE_LICENSE("BSD");
  84