img-ir-raw.c 4.1 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, IR_SPACE);
  39. else /* high */
  40. ir_raw_event_store_edge(rc_dev, IR_PULSE);
  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(unsigned long arg)
  61. {
  62. struct img_ir_priv *priv = (struct img_ir_priv *)arg;
  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. setup_timer(&raw->timer, img_ir_echo_timer, (unsigned long)priv);
  93. /* Allocate raw decoder */
  94. raw->rdev = rdev = rc_allocate_device();
  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->input_name = "IMG Infrared Decoder Raw";
  102. rdev->driver_type = RC_DRIVER_IR_RAW;
  103. /* Register raw decoder */
  104. error = rc_register_device(rdev);
  105. if (error) {
  106. dev_err(priv->dev, "failed to register raw IR input device\n");
  107. rc_free_device(rdev);
  108. raw->rdev = NULL;
  109. return error;
  110. }
  111. return 0;
  112. }
  113. void img_ir_remove_raw(struct img_ir_priv *priv)
  114. {
  115. struct img_ir_priv_raw *raw = &priv->raw;
  116. struct rc_dev *rdev = raw->rdev;
  117. u32 irq_en;
  118. if (!rdev)
  119. return;
  120. /* switch off and disable raw (edge) interrupts */
  121. spin_lock_irq(&priv->lock);
  122. raw->rdev = NULL;
  123. irq_en = img_ir_read(priv, IMG_IR_IRQ_ENABLE);
  124. irq_en &= ~IMG_IR_IRQ_EDGE;
  125. img_ir_write(priv, IMG_IR_IRQ_ENABLE, irq_en);
  126. img_ir_write(priv, IMG_IR_IRQ_CLEAR, IMG_IR_IRQ_EDGE);
  127. spin_unlock_irq(&priv->lock);
  128. rc_unregister_device(rdev);
  129. del_timer_sync(&raw->timer);
  130. }