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 <common.h> |
| 10 | #include <dm.h> |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 11 | #include <dt-structs.h> |
Simon Glass | ff418d9 | 2019-12-06 21:41:58 -0700 | [diff] [blame] | 12 | #include <irq.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | a847b27 | 2020-02-06 09:54:57 -0700 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
Simon Glass | ff418d9 | 2019-12-06 21:41:58 -0700 | [diff] [blame] | 15 | |
| 16 | int 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 | |
| 26 | int 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 | |
| 36 | int 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 | |
| 46 | int 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 Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 56 | int 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) |
Simon Glass | e7995f7 | 2021-08-07 07:24:11 -0600 | [diff] [blame] | 67 | int irq_get_by_phandle(struct udevice *dev, const struct phandle_2_arg *cells, |
| 68 | struct irq *irq) |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 69 | { |
| 70 | int ret; |
| 71 | |
Simon Glass | 0000e0d | 2021-03-15 17:25:28 +1300 | [diff] [blame] | 72 | ret = device_get_by_ofplat_idx(cells->idx, &irq->dev); |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 73 | if (ret) |
| 74 | return ret; |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 75 | irq->id = cells->arg[0]; |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 76 | |
Simon Glass | e7995f7 | 2021-08-07 07:24:11 -0600 | [diff] [blame] | 77 | /* |
| 78 | * Note: we could call irq_of_xlate_default() here to do this properly. |
| 79 | * For now, this is good enough for existing cases. |
| 80 | */ |
| 81 | irq->flags = cells->arg[1]; |
| 82 | |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | #else |
| 86 | static int irq_of_xlate_default(struct irq *irq, |
| 87 | struct ofnode_phandle_args *args) |
| 88 | { |
| 89 | log_debug("(irq=%p)\n", irq); |
| 90 | |
| 91 | if (args->args_count > 1) { |
| 92 | log_debug("Invaild args_count: %d\n", args->args_count); |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
| 96 | if (args->args_count) |
| 97 | irq->id = args->args[0]; |
| 98 | else |
| 99 | irq->id = 0; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int irq_get_by_index_tail(int ret, ofnode node, |
| 105 | struct ofnode_phandle_args *args, |
| 106 | const char *list_name, int index, |
| 107 | struct irq *irq) |
| 108 | { |
| 109 | struct udevice *dev_irq; |
| 110 | const struct irq_ops *ops; |
| 111 | |
| 112 | assert(irq); |
| 113 | irq->dev = NULL; |
| 114 | if (ret) |
| 115 | goto err; |
| 116 | |
| 117 | ret = uclass_get_device_by_ofnode(UCLASS_IRQ, args->node, &dev_irq); |
| 118 | if (ret) { |
| 119 | log_debug("uclass_get_device_by_ofnode failed: err=%d\n", ret); |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | irq->dev = dev_irq; |
| 124 | |
| 125 | ops = irq_get_ops(dev_irq); |
| 126 | |
| 127 | if (ops->of_xlate) |
| 128 | ret = ops->of_xlate(irq, args); |
| 129 | else |
| 130 | ret = irq_of_xlate_default(irq, args); |
| 131 | if (ret) { |
| 132 | log_debug("of_xlate() failed: %d\n", ret); |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | return irq_request(dev_irq, irq); |
| 137 | err: |
| 138 | log_debug("Node '%s', property '%s', failed to request IRQ index %d: %d\n", |
| 139 | ofnode_get_name(node), list_name, index, ret); |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | int irq_get_by_index(struct udevice *dev, int index, struct irq *irq) |
| 144 | { |
| 145 | struct ofnode_phandle_args args; |
| 146 | int ret; |
| 147 | |
| 148 | ret = dev_read_phandle_with_args(dev, "interrupts-extended", |
| 149 | "#interrupt-cells", 0, index, &args); |
| 150 | |
| 151 | return irq_get_by_index_tail(ret, dev_ofnode(dev), &args, |
| 152 | "interrupts-extended", index > 0, irq); |
| 153 | } |
| 154 | #endif /* OF_PLATDATA */ |
| 155 | |
| 156 | int irq_request(struct udevice *dev, struct irq *irq) |
| 157 | { |
| 158 | const struct irq_ops *ops; |
| 159 | |
| 160 | log_debug("(dev=%p, irq=%p)\n", dev, irq); |
Simon Glass | 515dcff | 2020-02-06 09:55:00 -0700 | [diff] [blame] | 161 | ops = irq_get_ops(dev); |
| 162 | |
| 163 | irq->dev = dev; |
| 164 | |
| 165 | if (!ops->request) |
| 166 | return 0; |
| 167 | |
| 168 | return ops->request(irq); |
| 169 | } |
| 170 | |
Simon Glass | a847b27 | 2020-02-06 09:54:57 -0700 | [diff] [blame] | 171 | int irq_first_device_type(enum irq_dev_t type, struct udevice **devp) |
| 172 | { |
| 173 | int ret; |
| 174 | |
| 175 | ret = uclass_first_device_drvdata(UCLASS_IRQ, type, devp); |
| 176 | if (ret) |
Simon Glass | d89f193 | 2020-07-16 21:22:30 -0600 | [diff] [blame] | 177 | return ret; |
Simon Glass | a847b27 | 2020-02-06 09:54:57 -0700 | [diff] [blame] | 178 | |
| 179 | return 0; |
| 180 | } |
Simon Glass | b24cbf4 | 2020-07-07 13:11:41 -0600 | [diff] [blame] | 181 | |
| 182 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 183 | int irq_get_acpi(const struct irq *irq, struct acpi_irq *acpi_irq) |
| 184 | { |
| 185 | struct irq_ops *ops; |
| 186 | |
| 187 | if (!irq_is_valid(irq)) |
| 188 | return -EINVAL; |
| 189 | |
| 190 | ops = irq_get_ops(irq->dev); |
| 191 | if (!ops->get_acpi) |
| 192 | return -ENOSYS; |
| 193 | |
| 194 | return ops->get_acpi(irq, acpi_irq); |
| 195 | } |
| 196 | #endif |
Simon Glass | a847b27 | 2020-02-06 09:54:57 -0700 | [diff] [blame] | 197 | |
Simon Glass | ff418d9 | 2019-12-06 21:41:58 -0700 | [diff] [blame] | 198 | UCLASS_DRIVER(irq) = { |
| 199 | .id = UCLASS_IRQ, |
| 200 | .name = "irq", |
| 201 | }; |