img-ir-core.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * ImgTec IR 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 contains core img-ir code for setting up the driver. The two interfaces
  12. * (raw and hardware decode) are handled separately.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include "img-ir.h"
  23. static irqreturn_t img_ir_isr(int irq, void *dev_id)
  24. {
  25. struct img_ir_priv *priv = dev_id;
  26. u32 irq_status;
  27. spin_lock(&priv->lock);
  28. /* we have to clear irqs before reading */
  29. irq_status = img_ir_read(priv, IMG_IR_IRQ_STATUS);
  30. img_ir_write(priv, IMG_IR_IRQ_CLEAR, irq_status);
  31. /* don't handle valid data irqs if we're only interested in matches */
  32. irq_status &= img_ir_read(priv, IMG_IR_IRQ_ENABLE);
  33. /* hand off edge interrupts to raw decode handler */
  34. if (irq_status & IMG_IR_IRQ_EDGE && img_ir_raw_enabled(&priv->raw))
  35. img_ir_isr_raw(priv, irq_status);
  36. /* hand off hardware match interrupts to hardware decode handler */
  37. if (irq_status & (IMG_IR_IRQ_DATA_MATCH |
  38. IMG_IR_IRQ_DATA_VALID |
  39. IMG_IR_IRQ_DATA2_VALID) &&
  40. img_ir_hw_enabled(&priv->hw))
  41. img_ir_isr_hw(priv, irq_status);
  42. spin_unlock(&priv->lock);
  43. return IRQ_HANDLED;
  44. }
  45. static void img_ir_setup(struct img_ir_priv *priv)
  46. {
  47. /* start off with interrupts disabled */
  48. img_ir_write(priv, IMG_IR_IRQ_ENABLE, 0);
  49. img_ir_setup_raw(priv);
  50. img_ir_setup_hw(priv);
  51. if (!IS_ERR(priv->clk))
  52. clk_prepare_enable(priv->clk);
  53. }
  54. static void img_ir_ident(struct img_ir_priv *priv)
  55. {
  56. u32 core_rev = img_ir_read(priv, IMG_IR_CORE_REV);
  57. dev_info(priv->dev,
  58. "IMG IR Decoder (%d.%d.%d.%d) probed successfully\n",
  59. (core_rev & IMG_IR_DESIGNER) >> IMG_IR_DESIGNER_SHIFT,
  60. (core_rev & IMG_IR_MAJOR_REV) >> IMG_IR_MAJOR_REV_SHIFT,
  61. (core_rev & IMG_IR_MINOR_REV) >> IMG_IR_MINOR_REV_SHIFT,
  62. (core_rev & IMG_IR_MAINT_REV) >> IMG_IR_MAINT_REV_SHIFT);
  63. dev_info(priv->dev, "Modes:%s%s\n",
  64. img_ir_hw_enabled(&priv->hw) ? " hardware" : "",
  65. img_ir_raw_enabled(&priv->raw) ? " raw" : "");
  66. }
  67. static int img_ir_probe(struct platform_device *pdev)
  68. {
  69. struct img_ir_priv *priv;
  70. struct resource *res_regs;
  71. int irq, error, error2;
  72. /* Get resources from platform device */
  73. irq = platform_get_irq(pdev, 0);
  74. if (irq < 0) {
  75. dev_err(&pdev->dev, "cannot find IRQ resource\n");
  76. return irq;
  77. }
  78. /* Private driver data */
  79. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  80. if (!priv) {
  81. dev_err(&pdev->dev, "cannot allocate device data\n");
  82. return -ENOMEM;
  83. }
  84. platform_set_drvdata(pdev, priv);
  85. priv->dev = &pdev->dev;
  86. spin_lock_init(&priv->lock);
  87. /* Ioremap the registers */
  88. res_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  89. priv->reg_base = devm_ioremap_resource(&pdev->dev, res_regs);
  90. if (IS_ERR(priv->reg_base))
  91. return PTR_ERR(priv->reg_base);
  92. /* Get core clock */
  93. priv->clk = devm_clk_get(&pdev->dev, "core");
  94. if (IS_ERR(priv->clk))
  95. dev_warn(&pdev->dev, "cannot get core clock resource\n");
  96. /* Get sys clock */
  97. priv->sys_clk = devm_clk_get(&pdev->dev, "sys");
  98. if (IS_ERR(priv->sys_clk))
  99. dev_warn(&pdev->dev, "cannot get sys clock resource\n");
  100. /*
  101. * Enabling the system clock before the register interface is
  102. * accessed. ISR shouldn't get called with Sys Clock disabled,
  103. * hence exiting probe with an error.
  104. */
  105. if (!IS_ERR(priv->sys_clk)) {
  106. error = clk_prepare_enable(priv->sys_clk);
  107. if (error) {
  108. dev_err(&pdev->dev, "cannot enable sys clock\n");
  109. return error;
  110. }
  111. }
  112. /* Set up raw & hw decoder */
  113. error = img_ir_probe_raw(priv);
  114. error2 = img_ir_probe_hw(priv);
  115. if (error && error2) {
  116. if (error == -ENODEV)
  117. error = error2;
  118. goto err_probe;
  119. }
  120. /* Get the IRQ */
  121. priv->irq = irq;
  122. error = request_irq(priv->irq, img_ir_isr, 0, "img-ir", priv);
  123. if (error) {
  124. dev_err(&pdev->dev, "cannot register IRQ %u\n",
  125. priv->irq);
  126. error = -EIO;
  127. goto err_irq;
  128. }
  129. img_ir_ident(priv);
  130. img_ir_setup(priv);
  131. return 0;
  132. err_irq:
  133. img_ir_remove_hw(priv);
  134. img_ir_remove_raw(priv);
  135. err_probe:
  136. if (!IS_ERR(priv->sys_clk))
  137. clk_disable_unprepare(priv->sys_clk);
  138. return error;
  139. }
  140. static int img_ir_remove(struct platform_device *pdev)
  141. {
  142. struct img_ir_priv *priv = platform_get_drvdata(pdev);
  143. free_irq(priv->irq, priv);
  144. img_ir_remove_hw(priv);
  145. img_ir_remove_raw(priv);
  146. if (!IS_ERR(priv->clk))
  147. clk_disable_unprepare(priv->clk);
  148. if (!IS_ERR(priv->sys_clk))
  149. clk_disable_unprepare(priv->sys_clk);
  150. return 0;
  151. }
  152. static SIMPLE_DEV_PM_OPS(img_ir_pmops, img_ir_suspend, img_ir_resume);
  153. static const struct of_device_id img_ir_match[] = {
  154. { .compatible = "img,ir-rev1" },
  155. {}
  156. };
  157. MODULE_DEVICE_TABLE(of, img_ir_match);
  158. static struct platform_driver img_ir_driver = {
  159. .driver = {
  160. .name = "img-ir",
  161. .of_match_table = img_ir_match,
  162. .pm = &img_ir_pmops,
  163. },
  164. .probe = img_ir_probe,
  165. .remove = img_ir_remove,
  166. };
  167. module_platform_driver(img_ir_driver);
  168. MODULE_AUTHOR("Imagination Technologies Ltd.");
  169. MODULE_DESCRIPTION("ImgTec IR");
  170. MODULE_LICENSE("GPL");