gsi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * ACPI GSI IRQ layer
  3. *
  4. * Copyright (C) 2015 ARM Ltd.
  5. * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/acpi.h>
  12. #include <linux/irq.h>
  13. #include <linux/irqdomain.h>
  14. enum acpi_irq_model_id acpi_irq_model;
  15. static unsigned int acpi_gsi_get_irq_type(int trigger, int polarity)
  16. {
  17. switch (polarity) {
  18. case ACPI_ACTIVE_LOW:
  19. return trigger == ACPI_EDGE_SENSITIVE ?
  20. IRQ_TYPE_EDGE_FALLING :
  21. IRQ_TYPE_LEVEL_LOW;
  22. case ACPI_ACTIVE_HIGH:
  23. return trigger == ACPI_EDGE_SENSITIVE ?
  24. IRQ_TYPE_EDGE_RISING :
  25. IRQ_TYPE_LEVEL_HIGH;
  26. case ACPI_ACTIVE_BOTH:
  27. if (trigger == ACPI_EDGE_SENSITIVE)
  28. return IRQ_TYPE_EDGE_BOTH;
  29. default:
  30. return IRQ_TYPE_NONE;
  31. }
  32. }
  33. /**
  34. * acpi_gsi_to_irq() - Retrieve the linux irq number for a given GSI
  35. * @gsi: GSI IRQ number to map
  36. * @irq: pointer where linux IRQ number is stored
  37. *
  38. * irq location updated with irq value [>0 on success, 0 on failure]
  39. *
  40. * Returns: linux IRQ number on success (>0)
  41. * -EINVAL on failure
  42. */
  43. int acpi_gsi_to_irq(u32 gsi, unsigned int *irq)
  44. {
  45. /*
  46. * Only default domain is supported at present, always find
  47. * the mapping corresponding to default domain by passing NULL
  48. * as irq_domain parameter
  49. */
  50. *irq = irq_find_mapping(NULL, gsi);
  51. /*
  52. * *irq == 0 means no mapping, that should
  53. * be reported as a failure
  54. */
  55. return (*irq > 0) ? *irq : -EINVAL;
  56. }
  57. EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);
  58. /**
  59. * acpi_register_gsi() - Map a GSI to a linux IRQ number
  60. * @dev: device for which IRQ has to be mapped
  61. * @gsi: GSI IRQ number
  62. * @trigger: trigger type of the GSI number to be mapped
  63. * @polarity: polarity of the GSI to be mapped
  64. *
  65. * Returns: a valid linux IRQ number on success
  66. * -EINVAL on failure
  67. */
  68. int acpi_register_gsi(struct device *dev, u32 gsi, int trigger,
  69. int polarity)
  70. {
  71. unsigned int irq;
  72. unsigned int irq_type = acpi_gsi_get_irq_type(trigger, polarity);
  73. /*
  74. * There is no way at present to look-up the IRQ domain on ACPI,
  75. * hence always create mapping referring to the default domain
  76. * by passing NULL as irq_domain parameter
  77. */
  78. irq = irq_create_mapping(NULL, gsi);
  79. if (!irq)
  80. return -EINVAL;
  81. /* Set irq type if specified and different than the current one */
  82. if (irq_type != IRQ_TYPE_NONE &&
  83. irq_type != irq_get_trigger_type(irq))
  84. irq_set_irq_type(irq, irq_type);
  85. return irq;
  86. }
  87. EXPORT_SYMBOL_GPL(acpi_register_gsi);
  88. /**
  89. * acpi_unregister_gsi() - Free a GSI<->linux IRQ number mapping
  90. * @gsi: GSI IRQ number
  91. */
  92. void acpi_unregister_gsi(u32 gsi)
  93. {
  94. int irq = irq_find_mapping(NULL, gsi);
  95. irq_dispose_mapping(irq);
  96. }
  97. EXPORT_SYMBOL_GPL(acpi_unregister_gsi);