gsi.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include <linux/of.h>
  15. enum acpi_irq_model_id acpi_irq_model;
  16. static struct fwnode_handle *acpi_gsi_domain_id;
  17. static unsigned int acpi_gsi_get_irq_type(int trigger, int polarity)
  18. {
  19. switch (polarity) {
  20. case ACPI_ACTIVE_LOW:
  21. return trigger == ACPI_EDGE_SENSITIVE ?
  22. IRQ_TYPE_EDGE_FALLING :
  23. IRQ_TYPE_LEVEL_LOW;
  24. case ACPI_ACTIVE_HIGH:
  25. return trigger == ACPI_EDGE_SENSITIVE ?
  26. IRQ_TYPE_EDGE_RISING :
  27. IRQ_TYPE_LEVEL_HIGH;
  28. case ACPI_ACTIVE_BOTH:
  29. if (trigger == ACPI_EDGE_SENSITIVE)
  30. return IRQ_TYPE_EDGE_BOTH;
  31. default:
  32. return IRQ_TYPE_NONE;
  33. }
  34. }
  35. /**
  36. * acpi_gsi_to_irq() - Retrieve the linux irq number for a given GSI
  37. * @gsi: GSI IRQ number to map
  38. * @irq: pointer where linux IRQ number is stored
  39. *
  40. * irq location updated with irq value [>0 on success, 0 on failure]
  41. *
  42. * Returns: linux IRQ number on success (>0)
  43. * -EINVAL on failure
  44. */
  45. int acpi_gsi_to_irq(u32 gsi, unsigned int *irq)
  46. {
  47. struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
  48. DOMAIN_BUS_ANY);
  49. *irq = irq_find_mapping(d, gsi);
  50. /*
  51. * *irq == 0 means no mapping, that should
  52. * be reported as a failure
  53. */
  54. return (*irq > 0) ? *irq : -EINVAL;
  55. }
  56. EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);
  57. /**
  58. * acpi_register_gsi() - Map a GSI to a linux IRQ number
  59. * @dev: device for which IRQ has to be mapped
  60. * @gsi: GSI IRQ number
  61. * @trigger: trigger type of the GSI number to be mapped
  62. * @polarity: polarity of the GSI to be mapped
  63. *
  64. * Returns: a valid linux IRQ number on success
  65. * -EINVAL on failure
  66. */
  67. int acpi_register_gsi(struct device *dev, u32 gsi, int trigger,
  68. int polarity)
  69. {
  70. struct irq_fwspec fwspec;
  71. if (WARN_ON(!acpi_gsi_domain_id)) {
  72. pr_warn("GSI: No registered irqchip, giving up\n");
  73. return -EINVAL;
  74. }
  75. fwspec.fwnode = acpi_gsi_domain_id;
  76. fwspec.param[0] = gsi;
  77. fwspec.param[1] = acpi_gsi_get_irq_type(trigger, polarity);
  78. fwspec.param_count = 2;
  79. return irq_create_fwspec_mapping(&fwspec);
  80. }
  81. EXPORT_SYMBOL_GPL(acpi_register_gsi);
  82. /**
  83. * acpi_unregister_gsi() - Free a GSI<->linux IRQ number mapping
  84. * @gsi: GSI IRQ number
  85. */
  86. void acpi_unregister_gsi(u32 gsi)
  87. {
  88. struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
  89. DOMAIN_BUS_ANY);
  90. int irq = irq_find_mapping(d, gsi);
  91. irq_dispose_mapping(irq);
  92. }
  93. EXPORT_SYMBOL_GPL(acpi_unregister_gsi);
  94. /**
  95. * acpi_set_irq_model - Setup the GSI irqdomain information
  96. * @model: the value assigned to acpi_irq_model
  97. * @fwnode: the irq_domain identifier for mapping and looking up
  98. * GSI interrupts
  99. */
  100. void __init acpi_set_irq_model(enum acpi_irq_model_id model,
  101. struct fwnode_handle *fwnode)
  102. {
  103. acpi_irq_model = model;
  104. acpi_gsi_domain_id = fwnode;
  105. }