acpi/gsi: Always perform an irq domain lookup
[linux-2.6-block.git] / drivers / acpi / gsi.c
CommitLineData
d8f4f161
LP
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>
d7f8504d 14#include <linux/of.h>
d8f4f161
LP
15
16enum acpi_irq_model_id acpi_irq_model;
17
d7f8504d
MZ
18static struct fwnode_handle *acpi_gsi_domain_id;
19
d8f4f161
LP
20static unsigned int acpi_gsi_get_irq_type(int trigger, int polarity)
21{
22 switch (polarity) {
23 case ACPI_ACTIVE_LOW:
24 return trigger == ACPI_EDGE_SENSITIVE ?
25 IRQ_TYPE_EDGE_FALLING :
26 IRQ_TYPE_LEVEL_LOW;
27 case ACPI_ACTIVE_HIGH:
28 return trigger == ACPI_EDGE_SENSITIVE ?
29 IRQ_TYPE_EDGE_RISING :
30 IRQ_TYPE_LEVEL_HIGH;
31 case ACPI_ACTIVE_BOTH:
32 if (trigger == ACPI_EDGE_SENSITIVE)
33 return IRQ_TYPE_EDGE_BOTH;
34 default:
35 return IRQ_TYPE_NONE;
36 }
37}
38
39/**
40 * acpi_gsi_to_irq() - Retrieve the linux irq number for a given GSI
41 * @gsi: GSI IRQ number to map
42 * @irq: pointer where linux IRQ number is stored
43 *
44 * irq location updated with irq value [>0 on success, 0 on failure]
45 *
46 * Returns: linux IRQ number on success (>0)
47 * -EINVAL on failure
48 */
49int acpi_gsi_to_irq(u32 gsi, unsigned int *irq)
50{
d7f8504d
MZ
51 struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
52 DOMAIN_BUS_ANY);
53
54 *irq = irq_find_mapping(d, gsi);
d8f4f161
LP
55 /*
56 * *irq == 0 means no mapping, that should
57 * be reported as a failure
58 */
59 return (*irq > 0) ? *irq : -EINVAL;
60}
61EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);
62
63/**
64 * acpi_register_gsi() - Map a GSI to a linux IRQ number
65 * @dev: device for which IRQ has to be mapped
66 * @gsi: GSI IRQ number
67 * @trigger: trigger type of the GSI number to be mapped
68 * @polarity: polarity of the GSI to be mapped
69 *
70 * Returns: a valid linux IRQ number on success
71 * -EINVAL on failure
72 */
73int acpi_register_gsi(struct device *dev, u32 gsi, int trigger,
74 int polarity)
75{
76 unsigned int irq;
77 unsigned int irq_type = acpi_gsi_get_irq_type(trigger, polarity);
d7f8504d
MZ
78 struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
79 DOMAIN_BUS_ANY);
d8f4f161 80
d7f8504d 81 irq = irq_create_mapping(d, gsi);
d8f4f161
LP
82 if (!irq)
83 return -EINVAL;
84
85 /* Set irq type if specified and different than the current one */
86 if (irq_type != IRQ_TYPE_NONE &&
87 irq_type != irq_get_trigger_type(irq))
88 irq_set_irq_type(irq, irq_type);
89 return irq;
90}
91EXPORT_SYMBOL_GPL(acpi_register_gsi);
92
93/**
94 * acpi_unregister_gsi() - Free a GSI<->linux IRQ number mapping
95 * @gsi: GSI IRQ number
96 */
97void acpi_unregister_gsi(u32 gsi)
98{
d7f8504d
MZ
99 struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
100 DOMAIN_BUS_ANY);
101 int irq = irq_find_mapping(d, gsi);
d8f4f161
LP
102
103 irq_dispose_mapping(irq);
104}
105EXPORT_SYMBOL_GPL(acpi_unregister_gsi);