gptimers-example.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include <linux/interrupt.h>
  10. #include <linux/module.h>
  11. #include <asm/gptimers.h>
  12. #include <asm/portmux.h>
  13. /* ... random driver includes ... */
  14. #define DRIVER_NAME "gptimer_example"
  15. struct gptimer_data {
  16. uint32_t period, width;
  17. };
  18. static struct gptimer_data data;
  19. /* ... random driver state ... */
  20. static irqreturn_t gptimer_example_irq(int irq, void *dev_id)
  21. {
  22. struct gptimer_data *data = dev_id;
  23. /* make sure it was our timer which caused the interrupt */
  24. if (!get_gptimer_intr(TIMER5_id))
  25. return IRQ_NONE;
  26. /* read the width/period values that were captured for the waveform */
  27. data->width = get_gptimer_pwidth(TIMER5_id);
  28. data->period = get_gptimer_period(TIMER5_id);
  29. /* acknowledge the interrupt */
  30. clear_gptimer_intr(TIMER5_id);
  31. /* tell the upper layers we took care of things */
  32. return IRQ_HANDLED;
  33. }
  34. /* ... random driver code ... */
  35. static int __init gptimer_example_init(void)
  36. {
  37. int ret;
  38. /* grab the peripheral pins */
  39. ret = peripheral_request(P_TMR5, DRIVER_NAME);
  40. if (ret) {
  41. printk(KERN_NOTICE DRIVER_NAME ": peripheral request failed\n");
  42. return ret;
  43. }
  44. /* grab the IRQ for the timer */
  45. ret = request_irq(IRQ_TIMER5, gptimer_example_irq, IRQF_SHARED, DRIVER_NAME, &data);
  46. if (ret) {
  47. printk(KERN_NOTICE DRIVER_NAME ": IRQ request failed\n");
  48. peripheral_free(P_TMR5);
  49. return ret;
  50. }
  51. /* setup the timer and enable it */
  52. set_gptimer_config(TIMER5_id, WDTH_CAP | PULSE_HI | PERIOD_CNT | IRQ_ENA);
  53. enable_gptimers(TIMER5bit);
  54. return 0;
  55. }
  56. module_init(gptimer_example_init);
  57. static void __exit gptimer_example_exit(void)
  58. {
  59. disable_gptimers(TIMER5bit);
  60. free_irq(IRQ_TIMER5, &data);
  61. peripheral_free(P_TMR5);
  62. }
  63. module_exit(gptimer_example_exit);
  64. MODULE_LICENSE("BSD");