img-ir-core.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /*
  97. * The driver doesn't need to know about the system ("sys") or power
  98. * modulation ("mod") clocks yet
  99. */
  100. /* Set up raw & hw decoder */
  101. error = img_ir_probe_raw(priv);
  102. error2 = img_ir_probe_hw(priv);
  103. if (error && error2)
  104. return (error == -ENODEV) ? error2 : error;
  105. /* Get the IRQ */
  106. priv->irq = irq;
  107. error = request_irq(priv->irq, img_ir_isr, 0, "img-ir", priv);
  108. if (error) {
  109. dev_err(&pdev->dev, "cannot register IRQ %u\n",
  110. priv->irq);
  111. error = -EIO;
  112. goto err_irq;
  113. }
  114. img_ir_ident(priv);
  115. img_ir_setup(priv);
  116. return 0;
  117. err_irq:
  118. img_ir_remove_hw(priv);
  119. img_ir_remove_raw(priv);
  120. return error;
  121. }
  122. static int img_ir_remove(struct platform_device *pdev)
  123. {
  124. struct img_ir_priv *priv = platform_get_drvdata(pdev);
  125. free_irq(priv->irq, priv);
  126. img_ir_remove_hw(priv);
  127. img_ir_remove_raw(priv);
  128. if (!IS_ERR(priv->clk))
  129. clk_disable_unprepare(priv->clk);
  130. return 0;
  131. }
  132. static SIMPLE_DEV_PM_OPS(img_ir_pmops, img_ir_suspend, img_ir_resume);
  133. static const struct of_device_id img_ir_match[] = {
  134. { .compatible = "img,ir-rev1" },
  135. {}
  136. };
  137. MODULE_DEVICE_TABLE(of, img_ir_match);
  138. static struct platform_driver img_ir_driver = {
  139. .driver = {
  140. .name = "img-ir",
  141. .owner = THIS_MODULE,
  142. .of_match_table = img_ir_match,
  143. .pm = &img_ir_pmops,
  144. },
  145. .probe = img_ir_probe,
  146. .remove = img_ir_remove,
  147. };
  148. module_platform_driver(img_ir_driver);
  149. MODULE_AUTHOR("Imagination Technologies Ltd.");
  150. MODULE_DESCRIPTION("ImgTec IR");
  151. MODULE_LICENSE("GPL");