Simon Glass | ff418d9 | 2019-12-06 21:41:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Simon Glass | a847b27 | 2020-02-06 09:54:57 -0700 | [diff] [blame] | 3 | * Copyright 2019 Google, LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | ff418d9 | 2019-12-06 21:41:58 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_IRQ |
| 8 | |
Simon Glass | ff418d9 | 2019-12-06 21:41:58 -0700 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 10 | #include <dt-structs.h> |
Simon Glass | ff418d9 | 2019-12-06 21:41:58 -0700 | [diff] [blame] | 11 | #include <irq.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | a847b27 | 2020-02-06 09:54:57 -0700 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
Simon Glass | ff418d9 | 2019-12-06 21:41:58 -0700 | [diff] [blame] | 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 | |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 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) |
Simon Glass | e7995f7 | 2021-08-07 07:24:11 -0600 | [diff] [blame] | 66 | int irq_get_by_phandle(struct udevice *dev, const struct phandle_2_arg *cells, |
| 67 | struct irq *irq) |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 68 | { |
| 69 | int ret; |
| 70 | |
Simon Glass | 0000e0d | 2021-03-15 17:25:28 +1300 | [diff] [blame] | 71 | ret = device_get_by_ofplat_idx(cells->idx, &irq->dev); |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 72 | if (ret) |
| 73 | return ret; |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 74 | irq->id = cells->arg[0]; |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 75 | |
Simon Glass | e7995f7 | 2021-08-07 07:24:11 -0600 | [diff] [blame] | 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 | |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 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) { |
Sean Anderson | a1b654b | 2021-12-01 14:26:53 -0500 | [diff] [blame] | 91 | log_debug("Invalid args_count: %d\n", args->args_count); |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 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); |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 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 | |
Simon Glass | a847b27 | 2020-02-06 09:54:57 -0700 | [diff] [blame] | 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) |
Simon Glass | d89f193 | 2020-07-16 21:22:30 -0600 | [diff] [blame] | 176 | return ret; |
Simon Glass | a847b27 | 2020-02-06 09:54:57 -0700 | [diff] [blame] | 177 | |
| 178 | return 0; |
| 179 | } |
Simon Glass | b24cbf4 | 2020-07-07 13:11:41 -0600 | [diff] [blame] | 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 |
Simon Glass | a847b27 | 2020-02-06 09:54:57 -0700 | [diff] [blame] | 196 | |
Simon Glass | ff418d9 | 2019-12-06 21:41:58 -0700 | [diff] [blame] | 197 | UCLASS_DRIVER(irq) = { |
| 198 | .id = UCLASS_IRQ, |
| 199 | .name = "irq", |
| 200 | }; |