gptimers-example.c 2.0 KB

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