Prepare v2024.10
[platform/kernel/u-boot.git] / drivers / misc / irq-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2019 Google, LLC
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #define LOG_CATEGORY UCLASS_IRQ
8
9 #include <dm.h>
10 #include <dt-structs.h>
11 #include <irq.h>
12 #include <log.h>
13 #include <dm/device-internal.h>
14
15 int irq_route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
16 {
17         const struct irq_ops *ops = irq_get_ops(dev);
18
19         if (!ops->route_pmc_gpio_gpe)
20                 return -ENOSYS;
21
22         return ops->route_pmc_gpio_gpe(dev, pmc_gpe_num);
23 }
24
25 int irq_set_polarity(struct udevice *dev, uint irq, bool active_low)
26 {
27         const struct irq_ops *ops = irq_get_ops(dev);
28
29         if (!ops->set_polarity)
30                 return -ENOSYS;
31
32         return ops->set_polarity(dev, irq, active_low);
33 }
34
35 int irq_snapshot_polarities(struct udevice *dev)
36 {
37         const struct irq_ops *ops = irq_get_ops(dev);
38
39         if (!ops->snapshot_polarities)
40                 return -ENOSYS;
41
42         return ops->snapshot_polarities(dev);
43 }
44
45 int irq_restore_polarities(struct udevice *dev)
46 {
47         const struct irq_ops *ops = irq_get_ops(dev);
48
49         if (!ops->restore_polarities)
50                 return -ENOSYS;
51
52         return ops->restore_polarities(dev);
53 }
54
55 int irq_read_and_clear(struct irq *irq)
56 {
57         const struct irq_ops *ops = irq_get_ops(irq->dev);
58
59         if (!ops->read_and_clear)
60                 return -ENOSYS;
61
62         return ops->read_and_clear(irq);
63 }
64
65 #if CONFIG_IS_ENABLED(OF_PLATDATA)
66 int irq_get_by_phandle(struct udevice *dev, const struct phandle_2_arg *cells,
67                        struct irq *irq)
68 {
69         int ret;
70
71         ret = device_get_by_ofplat_idx(cells->idx, &irq->dev);
72         if (ret)
73                 return ret;
74         irq->id = cells->arg[0];
75
76         /*
77          * Note: we could call irq_of_xlate_default() here to do this properly.
78          * For now, this is good enough for existing cases.
79          */
80         irq->flags = cells->arg[1];
81
82         return 0;
83 }
84 #else
85 static int irq_of_xlate_default(struct irq *irq,
86                                 struct ofnode_phandle_args *args)
87 {
88         log_debug("(irq=%p)\n", irq);
89
90         if (args->args_count > 1) {
91                 log_debug("Invalid args_count: %d\n", args->args_count);
92                 return -EINVAL;
93         }
94
95         if (args->args_count)
96                 irq->id = args->args[0];
97         else
98                 irq->id = 0;
99
100         return 0;
101 }
102
103 static int irq_get_by_index_tail(int ret, ofnode node,
104                                  struct ofnode_phandle_args *args,
105                                  const char *list_name, int index,
106                                  struct irq *irq)
107 {
108         struct udevice *dev_irq;
109         const struct irq_ops *ops;
110
111         assert(irq);
112         irq->dev = NULL;
113         if (ret)
114                 goto err;
115
116         ret = uclass_get_device_by_ofnode(UCLASS_IRQ, args->node, &dev_irq);
117         if (ret) {
118                 log_debug("uclass_get_device_by_ofnode failed: err=%d\n", ret);
119                 return ret;
120         }
121
122         irq->dev = dev_irq;
123
124         ops = irq_get_ops(dev_irq);
125
126         if (ops->of_xlate)
127                 ret = ops->of_xlate(irq, args);
128         else
129                 ret = irq_of_xlate_default(irq, args);
130         if (ret) {
131                 log_debug("of_xlate() failed: %d\n", ret);
132                 return ret;
133         }
134
135         return irq_request(dev_irq, irq);
136 err:
137         log_debug("Node '%s', property '%s', failed to request IRQ index %d: %d\n",
138                   ofnode_get_name(node), list_name, index, ret);
139         return ret;
140 }
141
142 int irq_get_by_index(struct udevice *dev, int index, struct irq *irq)
143 {
144         struct ofnode_phandle_args args;
145         int ret;
146
147         ret = dev_read_phandle_with_args(dev, "interrupts-extended",
148                                          "#interrupt-cells", 0, index, &args);
149
150         return irq_get_by_index_tail(ret, dev_ofnode(dev), &args,
151                                      "interrupts-extended", index > 0, irq);
152 }
153 #endif /* OF_PLATDATA */
154
155 int irq_request(struct udevice *dev, struct irq *irq)
156 {
157         const struct irq_ops *ops;
158
159         log_debug("(dev=%p, irq=%p)\n", dev, irq);
160         ops = irq_get_ops(dev);
161
162         irq->dev = dev;
163
164         if (!ops->request)
165                 return 0;
166
167         return ops->request(irq);
168 }
169
170 int irq_first_device_type(enum irq_dev_t type, struct udevice **devp)
171 {
172         int ret;
173
174         ret = uclass_first_device_drvdata(UCLASS_IRQ, type, devp);
175         if (ret)
176                 return ret;
177
178         return 0;
179 }
180
181 #if CONFIG_IS_ENABLED(ACPIGEN)
182 int irq_get_acpi(const struct irq *irq, struct acpi_irq *acpi_irq)
183 {
184         struct irq_ops *ops;
185
186         if (!irq_is_valid(irq))
187                 return -EINVAL;
188
189         ops = irq_get_ops(irq->dev);
190         if (!ops->get_acpi)
191                 return -ENOSYS;
192
193         return ops->get_acpi(irq, acpi_irq);
194 }
195 #endif
196
197 UCLASS_DRIVER(irq) = {
198         .id             = UCLASS_IRQ,
199         .name           = "irq",
200 };