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. return -ENOMEM;
  82. platform_set_drvdata(pdev, priv);
  83. priv->dev = &pdev->dev;
  84. spin_lock_init(&priv->lock);
  85. /* Ioremap the registers */
  86. res_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  87. priv->reg_base = devm_ioremap_resource(&pdev->dev, res_regs);
  88. if (IS_ERR(priv->reg_base))
  89. return PTR_ERR(priv->reg_base);
  90. /* Get core clock */
  91. priv->clk = devm_clk_get(&pdev->dev, "core");
  92. if (IS_ERR(priv->clk))
  93. dev_warn(&pdev->dev, "cannot get core clock resource\n");
  94. /* Get sys clock */
  95. priv->sys_clk = devm_clk_get(&pdev->dev, "sys");
  96. if (IS_ERR(priv->sys_clk))
  97. dev_warn(&pdev->dev, "cannot get sys clock resource\n");
  98. /*
  99. * Enabling the system clock before the register interface is
  100. * accessed. ISR shouldn't get called with Sys Clock disabled,
  101. * hence exiting probe with an error.
  102. */
  103. if (!IS_ERR(priv->sys_clk)) {
  104. error = clk_prepare_enable(priv->sys_clk);
  105. if (error) {
  106. dev_err(&pdev->dev, "cannot enable sys clock\n");
  107. return error;
  108. }
  109. }
  110. /* Set up raw & hw decoder */
  111. error = img_ir_probe_raw(priv);
  112. error2 = img_ir_probe_hw(priv);
  113. if (error && error2) {
  114. if (error == -ENODEV)
  115. error = error2;
  116. goto err_probe;
  117. }
  118. /* Get the IRQ */
  119. priv->irq = irq;
  120. error = request_irq(priv->irq, img_ir_isr, 0, "img-ir", priv);
  121. if (error) {
  122. dev_err(&pdev->dev, "cannot register IRQ %u\n",
  123. priv->irq);
  124. error = -EIO;
  125. goto err_irq;
  126. }
  127. img_ir_ident(priv);
  128. img_ir_setup(priv);
  129. return 0;
  130. err_irq:
  131. img_ir_remove_hw(priv);
  132. img_ir_remove_raw(priv);
  133. err_probe:
  134. if (!IS_ERR(priv->sys_clk))
  135. clk_disable_unprepare(priv->sys_clk);
  136. return error;
  137. }
  138. static int img_ir_remove(struct platform_device *pdev)
  139. {
  140. struct img_ir_priv *priv = platform_get_drvdata(pdev);
  141. free_irq(priv->irq, priv);
  142. img_ir_remove_hw(priv);
  143. img_ir_remove_raw(priv);
  144. if (!IS_ERR(priv->clk))
  145. clk_disable_unprepare(priv->clk);
  146. if (!IS_ERR(priv->sys_clk))
  147. clk_disable_unprepare(priv->sys_clk);
  148. return 0;
  149. }
  150. static SIMPLE_DEV_PM_OPS(img_ir_pmops, img_ir_suspend, img_ir_resume);
  151. static const struct of_device_id img_ir_match[] = {
  152. { .compatible = "img,ir-rev1" },
  153. {}
  154. };
  155. MODULE_DEVICE_TABLE(of, img_ir_match);
  156. static struct platform_driver img_ir_driver = {
  157. .driver = {
  158. .name = "img-ir",
  159. .of_match_table = img_ir_match,
  160. .pm = &img_ir_pmops,
  161. },
  162. .probe = img_ir_probe,
  163. .remove = img_ir_remove,
  164. };
  165. module_platform_driver(img_ir_driver);
  166. MODULE_AUTHOR("Imagination Technologies Ltd.");
  167. MODULE_DESCRIPTION("ImgTec IR");
  168. MODULE_LICENSE("GPL");