img-ir-raw.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * ImgTec IR Raw Decoder found in PowerDown Controller.
  3. *
  4. * Copyright 2010-2014 Imagination Technologies Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This ties into the input subsystem using the RC-core in raw mode. Raw IR
  12. * signal edges are reported and decoded by generic software decoders.
  13. */
  14. #include <linux/spinlock.h>
  15. #include <media/rc-core.h>
  16. #include "img-ir.h"
  17. #define ECHO_TIMEOUT_MS 150 /* ms between echos */
  18. /* must be called with priv->lock held */
  19. static void img_ir_refresh_raw(struct img_ir_priv *priv, u32 irq_status)
  20. {
  21. struct img_ir_priv_raw *raw = &priv->raw;
  22. struct rc_dev *rc_dev = priv->raw.rdev;
  23. int multiple;
  24. u32 ir_status;
  25. /* find whether both rise and fall was detected */
  26. multiple = ((irq_status & IMG_IR_IRQ_EDGE) == IMG_IR_IRQ_EDGE);
  27. /*
  28. * If so, we need to see if the level has actually changed.
  29. * If it's just noise that we didn't have time to process,
  30. * there's no point reporting it.
  31. */
  32. ir_status = img_ir_read(priv, IMG_IR_STATUS) & IMG_IR_IRRXD;
  33. if (multiple && ir_status == raw->last_status)
  34. return;
  35. raw->last_status = ir_status;
  36. /* report the edge to the IR raw decoders */
  37. if (ir_status) /* low */
  38. ir_raw_event_store_edge(rc_dev, false);
  39. else /* high */
  40. ir_raw_event_store_edge(rc_dev, true);
  41. ir_raw_event_handle(rc_dev);
  42. }
  43. /* called with priv->lock held */
  44. void img_ir_isr_raw(struct img_ir_priv *priv, u32 irq_status)
  45. {
  46. struct img_ir_priv_raw *raw = &priv->raw;
  47. /* check not removing */
  48. if (!raw->rdev)
  49. return;
  50. img_ir_refresh_raw(priv, irq_status);
  51. /* start / push back the echo timer */
  52. mod_timer(&raw->timer, jiffies + msecs_to_jiffies(ECHO_TIMEOUT_MS));
  53. }
  54. /*
  55. * Echo timer callback function.
  56. * The raw decoders expect to get a final sample even if there are no edges, in
  57. * order to be assured of the final space. If there are no edges for a certain
  58. * time we use this timer to emit a final sample to satisfy them.
  59. */
  60. static void img_ir_echo_timer(struct timer_list *t)
  61. {
  62. struct img_ir_priv *priv = from_timer(priv, t, raw.timer);
  63. spin_lock_irq(&priv->lock);
  64. /* check not removing */
  65. if (priv->raw.rdev)
  66. /*
  67. * It's safe to pass irq_status=0 since it's only used to check
  68. * for double edges.
  69. */
  70. img_ir_refresh_raw(priv, 0);
  71. spin_unlock_irq(&priv->lock);
  72. }
  73. void img_ir_setup_raw(struct img_ir_priv *priv)
  74. {
  75. u32 irq_en;
  76. if (!priv->raw.rdev)
  77. return;
  78. /* clear and enable edge interrupts */
  79. spin_lock_irq(&priv->lock);
  80. irq_en = img_ir_read(priv, IMG_IR_IRQ_ENABLE);
  81. irq_en |= IMG_IR_IRQ_EDGE;
  82. img_ir_write(priv, IMG_IR_IRQ_CLEAR, IMG_IR_IRQ_EDGE);
  83. img_ir_write(priv, IMG_IR_IRQ_ENABLE, irq_en);
  84. spin_unlock_irq(&priv->lock);
  85. }
  86. int img_ir_probe_raw(struct img_ir_priv *priv)
  87. {
  88. struct img_ir_priv_raw *raw = &priv->raw;
  89. struct rc_dev *rdev;
  90. int error;
  91. /* Set up the echo timer */
  92. timer_setup(&raw->timer, img_ir_echo_timer, 0);
  93. /* Allocate raw decoder */
  94. raw->rdev = rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
  95. if (!rdev) {
  96. dev_err(priv->dev, "cannot allocate raw input device\n");
  97. return -ENOMEM;
  98. }
  99. rdev->priv = priv;
  100. rdev->map_name = RC_MAP_EMPTY;
  101. rdev->device_name = "IMG Infrared Decoder Raw";
  102. /* Register raw decoder */
  103. error = rc_register_device(rdev);
  104. if (error) {
  105. dev_err(priv->dev, "failed to register raw IR input device\n");
  106. rc_free_device(rdev);
  107. raw->rdev = NULL;
  108. return error;
  109. }
  110. return 0;
  111. }
  112. void img_ir_remove_raw(struct img_ir_priv *priv)
  113. {
  114. struct img_ir_priv_raw *raw = &priv->raw;
  115. struct rc_dev *rdev = raw->rdev;
  116. u32 irq_en;
  117. if (!rdev)
  118. return;
  119. /* switch off and disable raw (edge) interrupts */
  120. spin_lock_irq(&priv->lock);
  121. raw->rdev = NULL;
  122. irq_en = img_ir_read(priv, IMG_IR_IRQ_ENABLE);
  123. irq_en &= ~IMG_IR_IRQ_EDGE;
  124. img_ir_write(priv, IMG_IR_IRQ_ENABLE, irq_en);
  125. img_ir_write(priv, IMG_IR_IRQ_CLEAR, IMG_IR_IRQ_EDGE);
  126. spin_unlock_irq(&priv->lock);
  127. rc_unregister_device(rdev);
  128. del_timer_sync(&raw->timer);
  129. }