blob: ec70866cc3ef0e9a514fb6765759c67e0d8b48ad [file] [log] [blame]
Simon Glassff418d92019-12-06 21:41:58 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
Simon Glassa847b272020-02-06 09:54:57 -07003 * Copyright 2019 Google, LLC
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassff418d92019-12-06 21:41:58 -07005 */
6
Simon Glass515dcff2020-02-06 09:55:00 -07007#define LOG_CATEGORY UCLASS_IRQ
8
Simon Glassff418d92019-12-06 21:41:58 -07009#include <common.h>
10#include <dm.h>
Simon Glass515dcff2020-02-06 09:55:00 -070011#include <dt-structs.h>
Simon Glassff418d92019-12-06 21:41:58 -070012#include <irq.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glassa847b272020-02-06 09:54:57 -070014#include <dm/device-internal.h>
Simon Glassff418d92019-12-06 21:41:58 -070015
16int irq_route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
17{
18 const struct irq_ops *ops = irq_get_ops(dev);
19
20 if (!ops->route_pmc_gpio_gpe)
21 return -ENOSYS;
22
23 return ops->route_pmc_gpio_gpe(dev, pmc_gpe_num);
24}
25
26int irq_set_polarity(struct udevice *dev, uint irq, bool active_low)
27{
28 const struct irq_ops *ops = irq_get_ops(dev);
29
30 if (!ops->set_polarity)
31 return -ENOSYS;
32
33 return ops->set_polarity(dev, irq, active_low);
34}
35
36int irq_snapshot_polarities(struct udevice *dev)
37{
38 const struct irq_ops *ops = irq_get_ops(dev);
39
40 if (!ops->snapshot_polarities)
41 return -ENOSYS;
42
43 return ops->snapshot_polarities(dev);
44}
45
46int irq_restore_polarities(struct udevice *dev)
47{
48 const struct irq_ops *ops = irq_get_ops(dev);
49
50 if (!ops->restore_polarities)
51 return -ENOSYS;
52
53 return ops->restore_polarities(dev);
54}
55
Simon Glass515dcff2020-02-06 09:55:00 -070056int irq_read_and_clear(struct irq *irq)
57{
58 const struct irq_ops *ops = irq_get_ops(irq->dev);
59
60 if (!ops->read_and_clear)
61 return -ENOSYS;
62
63 return ops->read_and_clear(irq);
64}
65
66#if CONFIG_IS_ENABLED(OF_PLATDATA)
Walter Lozanodc5b4372020-06-25 01:10:13 -030067int irq_get_by_driver_info(struct udevice *dev,
68 struct phandle_1_arg *cells, struct irq *irq)
Simon Glass515dcff2020-02-06 09:55:00 -070069{
70 int ret;
71
Walter Lozanodc5b4372020-06-25 01:10:13 -030072 ret = device_get_by_driver_info(cells->node, &irq->dev);
Simon Glass515dcff2020-02-06 09:55:00 -070073 if (ret)
74 return ret;
Walter Lozanodc5b4372020-06-25 01:10:13 -030075 irq->id = cells->arg[0];
Simon Glass515dcff2020-02-06 09:55:00 -070076
77 return 0;
78}
79#else
80static int irq_of_xlate_default(struct irq *irq,
81 struct ofnode_phandle_args *args)
82{
83 log_debug("(irq=%p)\n", irq);
84
85 if (args->args_count > 1) {
86 log_debug("Invaild args_count: %d\n", args->args_count);
87 return -EINVAL;
88 }
89
90 if (args->args_count)
91 irq->id = args->args[0];
92 else
93 irq->id = 0;
94
95 return 0;
96}
97
98static int irq_get_by_index_tail(int ret, ofnode node,
99 struct ofnode_phandle_args *args,
100 const char *list_name, int index,
101 struct irq *irq)
102{
103 struct udevice *dev_irq;
104 const struct irq_ops *ops;
105
106 assert(irq);
107 irq->dev = NULL;
108 if (ret)
109 goto err;
110
111 ret = uclass_get_device_by_ofnode(UCLASS_IRQ, args->node, &dev_irq);
112 if (ret) {
113 log_debug("uclass_get_device_by_ofnode failed: err=%d\n", ret);
114 return ret;
115 }
116
117 irq->dev = dev_irq;
118
119 ops = irq_get_ops(dev_irq);
120
121 if (ops->of_xlate)
122 ret = ops->of_xlate(irq, args);
123 else
124 ret = irq_of_xlate_default(irq, args);
125 if (ret) {
126 log_debug("of_xlate() failed: %d\n", ret);
127 return ret;
128 }
129
130 return irq_request(dev_irq, irq);
131err:
132 log_debug("Node '%s', property '%s', failed to request IRQ index %d: %d\n",
133 ofnode_get_name(node), list_name, index, ret);
134 return ret;
135}
136
137int irq_get_by_index(struct udevice *dev, int index, struct irq *irq)
138{
139 struct ofnode_phandle_args args;
140 int ret;
141
142 ret = dev_read_phandle_with_args(dev, "interrupts-extended",
143 "#interrupt-cells", 0, index, &args);
144
145 return irq_get_by_index_tail(ret, dev_ofnode(dev), &args,
146 "interrupts-extended", index > 0, irq);
147}
148#endif /* OF_PLATDATA */
149
150int irq_request(struct udevice *dev, struct irq *irq)
151{
152 const struct irq_ops *ops;
153
154 log_debug("(dev=%p, irq=%p)\n", dev, irq);
155 if (!irq)
156 return 0;
157 ops = irq_get_ops(dev);
158
159 irq->dev = dev;
160
161 if (!ops->request)
162 return 0;
163
164 return ops->request(irq);
165}
166
Simon Glassa847b272020-02-06 09:54:57 -0700167int irq_first_device_type(enum irq_dev_t type, struct udevice **devp)
168{
169 int ret;
170
171 ret = uclass_first_device_drvdata(UCLASS_IRQ, type, devp);
172 if (ret)
173 return log_msg_ret("find", ret);
174
175 return 0;
176}
177
Simon Glassff418d92019-12-06 21:41:58 -0700178UCLASS_DRIVER(irq) = {
179 .id = UCLASS_IRQ,
180 .name = "irq",
181};