blob: 61aa10e4658ed251673f4e2b9e2245f06af1f49f [file] [log] [blame]
Simon Glass79d66a62019-12-06 21:41:58 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
Simon Glassba876072020-02-06 09:54:57 -07003 * Copyright 2019 Google, LLC
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass79d66a62019-12-06 21:41:58 -07005 */
6
Simon Glass02554352020-02-06 09:55:00 -07007#define LOG_CATEGORY UCLASS_IRQ
8
Simon Glass79d66a62019-12-06 21:41:58 -07009#include <common.h>
10#include <dm.h>
Simon Glass02554352020-02-06 09:55:00 -070011#include <dt-structs.h>
Simon Glass79d66a62019-12-06 21:41:58 -070012#include <irq.h>
Simon Glassba876072020-02-06 09:54:57 -070013#include <dm/device-internal.h>
Simon Glass79d66a62019-12-06 21:41:58 -070014
15int 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
25int 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
35int 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
45int 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
Simon Glass02554352020-02-06 09:55:00 -070055int 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)
66int irq_get_by_index_platdata(struct udevice *dev, int index,
67 struct phandle_1_arg *cells, struct irq *irq)
68{
69 int ret;
70
71 if (index != 0)
72 return -ENOSYS;
73 ret = uclass_get_device(UCLASS_IRQ, 0, &irq->dev);
74 if (ret)
75 return ret;
76 irq->id = cells[0].arg[0];
77
78 return 0;
79}
80#else
81static int irq_of_xlate_default(struct irq *irq,
82 struct ofnode_phandle_args *args)
83{
84 log_debug("(irq=%p)\n", irq);
85
86 if (args->args_count > 1) {
87 log_debug("Invaild args_count: %d\n", args->args_count);
88 return -EINVAL;
89 }
90
91 if (args->args_count)
92 irq->id = args->args[0];
93 else
94 irq->id = 0;
95
96 return 0;
97}
98
99static int irq_get_by_index_tail(int ret, ofnode node,
100 struct ofnode_phandle_args *args,
101 const char *list_name, int index,
102 struct irq *irq)
103{
104 struct udevice *dev_irq;
105 const struct irq_ops *ops;
106
107 assert(irq);
108 irq->dev = NULL;
109 if (ret)
110 goto err;
111
112 ret = uclass_get_device_by_ofnode(UCLASS_IRQ, args->node, &dev_irq);
113 if (ret) {
114 log_debug("uclass_get_device_by_ofnode failed: err=%d\n", ret);
115 return ret;
116 }
117
118 irq->dev = dev_irq;
119
120 ops = irq_get_ops(dev_irq);
121
122 if (ops->of_xlate)
123 ret = ops->of_xlate(irq, args);
124 else
125 ret = irq_of_xlate_default(irq, args);
126 if (ret) {
127 log_debug("of_xlate() failed: %d\n", ret);
128 return ret;
129 }
130
131 return irq_request(dev_irq, irq);
132err:
133 log_debug("Node '%s', property '%s', failed to request IRQ index %d: %d\n",
134 ofnode_get_name(node), list_name, index, ret);
135 return ret;
136}
137
138int irq_get_by_index(struct udevice *dev, int index, struct irq *irq)
139{
140 struct ofnode_phandle_args args;
141 int ret;
142
143 ret = dev_read_phandle_with_args(dev, "interrupts-extended",
144 "#interrupt-cells", 0, index, &args);
145
146 return irq_get_by_index_tail(ret, dev_ofnode(dev), &args,
147 "interrupts-extended", index > 0, irq);
148}
149#endif /* OF_PLATDATA */
150
151int irq_request(struct udevice *dev, struct irq *irq)
152{
153 const struct irq_ops *ops;
154
155 log_debug("(dev=%p, irq=%p)\n", dev, irq);
156 if (!irq)
157 return 0;
158 ops = irq_get_ops(dev);
159
160 irq->dev = dev;
161
162 if (!ops->request)
163 return 0;
164
165 return ops->request(irq);
166}
167
Simon Glassba876072020-02-06 09:54:57 -0700168int irq_first_device_type(enum irq_dev_t type, struct udevice **devp)
169{
170 int ret;
171
172 ret = uclass_first_device_drvdata(UCLASS_IRQ, type, devp);
173 if (ret)
174 return log_msg_ret("find", ret);
175
176 return 0;
177}
178
Simon Glass79d66a62019-12-06 21:41:58 -0700179UCLASS_DRIVER(irq) = {
180 .id = UCLASS_IRQ,
181 .name = "irq",
182};