| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (c) 2015 Endless Mobile, Inc. |
| 4 |
* Author: Carlo Caione <[email protected]> |
| 5 |
* Copyright (c) 2016 BayLibre, SAS. |
| 6 |
* Author: Jerome Brunet <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 |
|
| 11 |
#include <linux/io.h> |
| 12 |
#include <linux/module.h> |
| 13 |
#include <linux/irq.h> |
| 14 |
#include <linux/irqdomain.h> |
| 15 |
#include <linux/irqchip.h> |
| 16 |
#include <linux/of.h> |
| 17 |
#include <linux/of_address.h> |
| 18 |
|
| 19 |
#define MAX_NUM_CHANNEL 64 |
| 20 |
#define MAX_INPUT_MUX 256 |
| 21 |
|
| 22 |
#define REG_EDGE_POL 0x00 |
| 23 |
#define REG_PIN_03_SEL 0x04 |
| 24 |
#define REG_PIN_47_SEL 0x08 |
| 25 |
#define REG_FILTER_SEL 0x0c |
| 26 |
|
| 27 |
/* use for A1 like chips */ |
| 28 |
#define REG_PIN_A1_SEL 0x04 |
| 29 |
|
| 30 |
/* |
| 31 |
* Note: The S905X3 datasheet reports that BOTH_EDGE is controlled by |
| 32 |
* bits 24 to 31. Tests on the actual HW show that these bits are |
| 33 |
* stuck at 0. Bits 8 to 15 are responsive and have the expected |
| 34 |
* effect. |
| 35 |
*/ |
| 36 |
#define REG_EDGE_POL_EDGE(params, x) BIT((params)->edge_single_offset + (x)) |
| 37 |
#define REG_EDGE_POL_LOW(params, x) BIT((params)->pol_low_offset + (x)) |
| 38 |
#define REG_BOTH_EDGE(params, x) BIT((params)->edge_both_offset + (x)) |
| 39 |
#define REG_EDGE_POL_MASK(params, x) ( \ |
| 40 |
REG_EDGE_POL_EDGE(params, x) | \ |
| 41 |
REG_EDGE_POL_LOW(params, x) | \ |
| 42 |
REG_BOTH_EDGE(params, x)) |
| 43 |
#define REG_PIN_SEL_SHIFT(x) (((x) % 4) * 8) |
| 44 |
#define REG_FILTER_SEL_SHIFT(x) ((x) * 4) |
| 45 |
|
| 46 |
struct meson_gpio_irq_controller; |
| 47 |
static void meson8_gpio_irq_sel_pin(struct meson_gpio_irq_controller *ctl, |
| 48 |
unsigned int channel, unsigned long hwirq); |
| 49 |
static void meson_gpio_irq_init_dummy(struct meson_gpio_irq_controller *ctl); |
| 50 |
static void meson_a1_gpio_irq_sel_pin(struct meson_gpio_irq_controller *ctl, |
| 51 |
unsigned int channel, |
| 52 |
unsigned long hwirq); |
| 53 |
static void meson_a1_gpio_irq_init(struct meson_gpio_irq_controller *ctl); |
| 54 |
static int meson8_gpio_irq_set_type(struct meson_gpio_irq_controller *ctl, |
| 55 |
unsigned int type, u32 *channel_hwirq); |
| 56 |
static int meson_s4_gpio_irq_set_type(struct meson_gpio_irq_controller *ctl, |
| 57 |
unsigned int type, u32 *channel_hwirq); |
| 58 |
|
| 59 |
struct irq_ctl_ops { |
| 60 |
void (*gpio_irq_sel_pin)(struct meson_gpio_irq_controller *ctl, |
| 61 |
unsigned int channel, unsigned long hwirq); |
| 62 |
void (*gpio_irq_init)(struct meson_gpio_irq_controller *ctl); |
| 63 |
int (*gpio_irq_set_type)(struct meson_gpio_irq_controller *ctl, |
| 64 |
unsigned int type, u32 *channel_hwirq); |
| 65 |
}; |
| 66 |
|
| 67 |
struct meson_gpio_irq_params { |
| 68 |
unsigned int nr_hwirq; |
| 69 |
unsigned int nr_channels; |
| 70 |
bool support_edge_both; |
| 71 |
unsigned int edge_both_offset; |
| 72 |
unsigned int edge_single_offset; |
| 73 |
unsigned int edge_pol_reg; |
| 74 |
unsigned int pol_low_offset; |
| 75 |
unsigned int pin_sel_mask; |
| 76 |
struct irq_ctl_ops ops; |
| 77 |
}; |
| 78 |
|
| 79 |
#define INIT_MESON_COMMON(irqs, init, sel, type) \ |
| 80 |
.nr_hwirq = irqs, \ |
| 81 |
.ops = { \ |
| 82 |
.gpio_irq_init = init, \ |
| 83 |
.gpio_irq_sel_pin = sel, \ |
| 84 |
.gpio_irq_set_type = type, \ |
| 85 |
}, |
| 86 |
|
| 87 |
#define INIT_MESON8_COMMON_DATA(irqs) \ |
| 88 |
INIT_MESON_COMMON(irqs, meson_gpio_irq_init_dummy, \ |
| 89 |
meson8_gpio_irq_sel_pin, \ |
| 90 |
meson8_gpio_irq_set_type) \ |
| 91 |
.edge_single_offset = 0, \ |
| 92 |
.pol_low_offset = 16, \ |
| 93 |
.pin_sel_mask = 0xff, \ |
| 94 |
.nr_channels = 8, \ |
| 95 |
|
| 96 |
#define INIT_MESON_A1_COMMON_DATA(irqs) \ |
| 97 |
INIT_MESON_COMMON(irqs, meson_a1_gpio_irq_init, \ |
| 98 |
meson_a1_gpio_irq_sel_pin, \ |
| 99 |
meson8_gpio_irq_set_type) \ |
| 100 |
.support_edge_both = true, \ |
| 101 |
.edge_both_offset = 16, \ |
| 102 |
.edge_single_offset = 8, \ |
| 103 |
.pol_low_offset = 0, \ |
| 104 |
.pin_sel_mask = 0x7f, \ |
| 105 |
.nr_channels = 8, \ |
| 106 |
|
| 107 |
#define INIT_MESON_A4_AO_COMMON_DATA(irqs) \ |
| 108 |
INIT_MESON_COMMON(irqs, meson_a1_gpio_irq_init, \ |
| 109 |
meson_a1_gpio_irq_sel_pin, \ |
| 110 |
meson_s4_gpio_irq_set_type) \ |
| 111 |
.support_edge_both = true, \ |
| 112 |
.edge_both_offset = 0, \ |
| 113 |
.edge_single_offset = 12, \ |
| 114 |
.edge_pol_reg = 0x8, \ |
| 115 |
.pol_low_offset = 0, \ |
| 116 |
.pin_sel_mask = 0xff, \ |
| 117 |
.nr_channels = 2, \ |
| 118 |
|
| 119 |
#define INIT_MESON_S4_COMMON_DATA(irqs) \ |
| 120 |
INIT_MESON_COMMON(irqs, meson_a1_gpio_irq_init, \ |
| 121 |
meson_a1_gpio_irq_sel_pin, \ |
| 122 |
meson_s4_gpio_irq_set_type) \ |
| 123 |
.support_edge_both = true, \ |
| 124 |
.edge_both_offset = 0, \ |
| 125 |
.edge_single_offset = 12, \ |
| 126 |
.edge_pol_reg = 0x1c, \ |
| 127 |
.pol_low_offset = 0, \ |
| 128 |
.pin_sel_mask = 0xff, \ |
| 129 |
.nr_channels = 12, \ |
| 130 |
|
| 131 |
static const struct meson_gpio_irq_params meson8_params = { |
| 132 |
INIT_MESON8_COMMON_DATA(134) |
| 133 |
}; |
| 134 |
|
| 135 |
static const struct meson_gpio_irq_params meson8b_params = { |
| 136 |
INIT_MESON8_COMMON_DATA(119) |
| 137 |
}; |
| 138 |
|
| 139 |
static const struct meson_gpio_irq_params gxbb_params = { |
| 140 |
INIT_MESON8_COMMON_DATA(133) |
| 141 |
}; |
| 142 |
|
| 143 |
static const struct meson_gpio_irq_params gxl_params = { |
| 144 |
INIT_MESON8_COMMON_DATA(110) |
| 145 |
}; |
| 146 |
|
| 147 |
static const struct meson_gpio_irq_params axg_params = { |
| 148 |
INIT_MESON8_COMMON_DATA(100) |
| 149 |
}; |
| 150 |
|
| 151 |
static const struct meson_gpio_irq_params sm1_params = { |
| 152 |
INIT_MESON8_COMMON_DATA(100) |
| 153 |
.support_edge_both = true, |
| 154 |
.edge_both_offset = 8, |
| 155 |
}; |
| 156 |
|
| 157 |
static const struct meson_gpio_irq_params a1_params = { |
| 158 |
INIT_MESON_A1_COMMON_DATA(62) |
| 159 |
}; |
| 160 |
|
| 161 |
static const struct meson_gpio_irq_params a4_params = { |
| 162 |
INIT_MESON_S4_COMMON_DATA(81) |
| 163 |
}; |
| 164 |
|
| 165 |
static const struct meson_gpio_irq_params a4_ao_params = { |
| 166 |
INIT_MESON_A4_AO_COMMON_DATA(8) |
| 167 |
}; |
| 168 |
|
| 169 |
static const struct meson_gpio_irq_params a5_params = { |
| 170 |
INIT_MESON_S4_COMMON_DATA(99) |
| 171 |
}; |
| 172 |
|
| 173 |
static const struct meson_gpio_irq_params s4_params = { |
| 174 |
INIT_MESON_S4_COMMON_DATA(82) |
| 175 |
}; |
| 176 |
|
| 177 |
static const struct meson_gpio_irq_params s6_params = { |
| 178 |
INIT_MESON_S4_COMMON_DATA(100) |
| 179 |
}; |
| 180 |
|
| 181 |
static const struct meson_gpio_irq_params s7_params = { |
| 182 |
INIT_MESON_S4_COMMON_DATA(84) |
| 183 |
}; |
| 184 |
|
| 185 |
static const struct meson_gpio_irq_params c3_params = { |
| 186 |
INIT_MESON_S4_COMMON_DATA(55) |
| 187 |
}; |
| 188 |
|
| 189 |
static const struct meson_gpio_irq_params t7_params = { |
| 190 |
INIT_MESON_S4_COMMON_DATA(157) |
| 191 |
}; |
| 192 |
|
| 193 |
static const struct of_device_id meson_irq_gpio_matches[] __maybe_unused = { |
| 194 |
{ .compatible = "amlogic,meson8-gpio-intc", .data = &meson8_params }, |
| 195 |
{ .compatible = "amlogic,meson8b-gpio-intc", .data = &meson8b_params }, |
| 196 |
{ .compatible = "amlogic,meson-gxbb-gpio-intc", .data = &gxbb_params }, |
| 197 |
{ .compatible = "amlogic,meson-gxl-gpio-intc", .data = &gxl_params }, |
| 198 |
{ .compatible = "amlogic,meson-axg-gpio-intc", .data = &axg_params }, |
| 199 |
{ .compatible = "amlogic,meson-g12a-gpio-intc", .data = &axg_params }, |
| 200 |
{ .compatible = "amlogic,meson-sm1-gpio-intc", .data = &sm1_params }, |
| 201 |
{ .compatible = "amlogic,meson-a1-gpio-intc", .data = &a1_params }, |
| 202 |
{ .compatible = "amlogic,meson-s4-gpio-intc", .data = &s4_params }, |
| 203 |
{ .compatible = "amlogic,a4-gpio-ao-intc", .data = &a4_ao_params }, |
| 204 |
{ .compatible = "amlogic,a4-gpio-intc", .data = &a4_params }, |
| 205 |
{ .compatible = "amlogic,a5-gpio-intc", .data = &a5_params }, |
| 206 |
{ .compatible = "amlogic,s6-gpio-intc", .data = &s6_params }, |
| 207 |
{ .compatible = "amlogic,s7-gpio-intc", .data = &s7_params }, |
| 208 |
{ .compatible = "amlogic,s7d-gpio-intc", .data = &s7_params }, |
| 209 |
{ .compatible = "amlogic,c3-gpio-intc", .data = &c3_params }, |
| 210 |
{ .compatible = "amlogic,t7-gpio-intc", .data = &t7_params }, |
| 211 |
{ } |
| 212 |
}; |
| 213 |
|
| 214 |
struct meson_gpio_irq_controller { |
| 215 |
const struct meson_gpio_irq_params *params; |
| 216 |
void __iomem *base; |
| 217 |
u32 channel_irqs[MAX_NUM_CHANNEL]; |
| 218 |
DECLARE_BITMAP(channel_map, MAX_NUM_CHANNEL); |
| 219 |
raw_spinlock_t lock; |
| 220 |
}; |
| 221 |
|
| 222 |
static void meson_gpio_irq_update_bits(struct meson_gpio_irq_controller *ctl, |
| 223 |
unsigned int reg, u32 mask, u32 val) |
| 224 |
{ |
| 225 |
unsigned long flags; |
| 226 |
u32 tmp; |
| 227 |
|
| 228 |
raw_spin_lock_irqsave(&ctl->lock, flags); |
| 229 |
|
| 230 |
tmp = readl_relaxed(ctl->base + reg); |
| 231 |
tmp &= ~mask; |
| 232 |
tmp |= val; |
| 233 |
writel_relaxed(tmp, ctl->base + reg); |
| 234 |
|
| 235 |
raw_spin_unlock_irqrestore(&ctl->lock, flags); |
| 236 |
} |
| 237 |
|
| 238 |
static void meson_gpio_irq_init_dummy(struct meson_gpio_irq_controller *ctl) |
| 239 |
{ |
| 240 |
} |
| 241 |
|
| 242 |
static void meson8_gpio_irq_sel_pin(struct meson_gpio_irq_controller *ctl, |
| 243 |
unsigned int channel, unsigned long hwirq) |
| 244 |
{ |
| 245 |
unsigned int reg_offset; |
| 246 |
unsigned int bit_offset; |
| 247 |
|
| 248 |
reg_offset = (channel < 4) ? REG_PIN_03_SEL : REG_PIN_47_SEL; |
| 249 |
bit_offset = REG_PIN_SEL_SHIFT(channel); |
| 250 |
|
| 251 |
meson_gpio_irq_update_bits(ctl, reg_offset, |
| 252 |
ctl->params->pin_sel_mask << bit_offset, |
| 253 |
hwirq << bit_offset); |
| 254 |
} |
| 255 |
|
| 256 |
static void meson_a1_gpio_irq_sel_pin(struct meson_gpio_irq_controller *ctl, |
| 257 |
unsigned int channel, |
| 258 |
unsigned long hwirq) |
| 259 |
{ |
| 260 |
unsigned int reg_offset; |
| 261 |
unsigned int bit_offset; |
| 262 |
|
| 263 |
bit_offset = ((channel % 2) == 0) ? 0 : 16; |
| 264 |
reg_offset = REG_PIN_A1_SEL + ((channel / 2) << 2); |
| 265 |
|
| 266 |
meson_gpio_irq_update_bits(ctl, reg_offset, |
| 267 |
ctl->params->pin_sel_mask << bit_offset, |
| 268 |
hwirq << bit_offset); |
| 269 |
} |
| 270 |
|
| 271 |
/* For a1 or later chips like a1 there is a switch to enable/disable irq */ |
| 272 |
static void meson_a1_gpio_irq_init(struct meson_gpio_irq_controller *ctl) |
| 273 |
{ |
| 274 |
meson_gpio_irq_update_bits(ctl, REG_EDGE_POL, BIT(31), BIT(31)); |
| 275 |
} |
| 276 |
|
| 277 |
static int |
| 278 |
meson_gpio_irq_request_channel(struct meson_gpio_irq_controller *ctl, |
| 279 |
unsigned long hwirq, |
| 280 |
u32 **channel_hwirq) |
| 281 |
{ |
| 282 |
unsigned long flags; |
| 283 |
unsigned int idx; |
| 284 |
|
| 285 |
raw_spin_lock_irqsave(&ctl->lock, flags); |
| 286 |
|
| 287 |
/* Find a free channel */ |
| 288 |
idx = find_first_zero_bit(ctl->channel_map, ctl->params->nr_channels); |
| 289 |
if (idx >= ctl->params->nr_channels) { |
| 290 |
raw_spin_unlock_irqrestore(&ctl->lock, flags); |
| 291 |
pr_err("No channel available\n"); |
| 292 |
return -ENOSPC; |
| 293 |
} |
| 294 |
|
| 295 |
/* Mark the channel as used */ |
| 296 |
set_bit(idx, ctl->channel_map); |
| 297 |
|
| 298 |
raw_spin_unlock_irqrestore(&ctl->lock, flags); |
| 299 |
|
| 300 |
/* |
| 301 |
* Setup the mux of the channel to route the signal of the pad |
| 302 |
* to the appropriate input of the GIC |
| 303 |
*/ |
| 304 |
ctl->params->ops.gpio_irq_sel_pin(ctl, idx, hwirq); |
| 305 |
|
| 306 |
/* |
| 307 |
* Get the hwirq number assigned to this channel through |
| 308 |
* a pointer the channel_irq table. The added benefit of this |
| 309 |
* method is that we can also retrieve the channel index with |
| 310 |
* it, using the table base. |
| 311 |
*/ |
| 312 |
*channel_hwirq = &(ctl->channel_irqs[idx]); |
| 313 |
|
| 314 |
pr_debug("hwirq %lu assigned to channel %d - irq %u\n", |
| 315 |
hwirq, idx, **channel_hwirq); |
| 316 |
|
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
static unsigned int |
| 321 |
meson_gpio_irq_get_channel_idx(struct meson_gpio_irq_controller *ctl, |
| 322 |
u32 *channel_hwirq) |
| 323 |
{ |
| 324 |
return channel_hwirq - ctl->channel_irqs; |
| 325 |
} |
| 326 |
|
| 327 |
static void |
| 328 |
meson_gpio_irq_release_channel(struct meson_gpio_irq_controller *ctl, |
| 329 |
u32 *channel_hwirq) |
| 330 |
{ |
| 331 |
unsigned int idx; |
| 332 |
|
| 333 |
idx = meson_gpio_irq_get_channel_idx(ctl, channel_hwirq); |
| 334 |
clear_bit(idx, ctl->channel_map); |
| 335 |
} |
| 336 |
|
| 337 |
static int meson8_gpio_irq_set_type(struct meson_gpio_irq_controller *ctl, |
| 338 |
unsigned int type, u32 *channel_hwirq) |
| 339 |
{ |
| 340 |
const struct meson_gpio_irq_params *params = ctl->params; |
| 341 |
unsigned int idx; |
| 342 |
u32 val = 0; |
| 343 |
|
| 344 |
idx = meson_gpio_irq_get_channel_idx(ctl, channel_hwirq); |
| 345 |
|
| 346 |
/* |
| 347 |
* The controller has a filter block to operate in either LEVEL or |
| 348 |
* EDGE mode, then signal is sent to the GIC. To enable LEVEL_LOW and |
| 349 |
* EDGE_FALLING support (which the GIC does not support), the filter |
| 350 |
* block is also able to invert the input signal it gets before |
| 351 |
* providing it to the GIC. |
| 352 |
*/ |
| 353 |
type &= IRQ_TYPE_SENSE_MASK; |
| 354 |
|
| 355 |
/* |
| 356 |
* New controller support EDGE_BOTH trigger. This setting takes |
| 357 |
* precedence over the other edge/polarity settings |
| 358 |
*/ |
| 359 |
if (type == IRQ_TYPE_EDGE_BOTH) { |
| 360 |
if (!params->support_edge_both) |
| 361 |
return -EINVAL; |
| 362 |
|
| 363 |
val |= REG_BOTH_EDGE(params, idx); |
| 364 |
} else { |
| 365 |
if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) |
| 366 |
val |= REG_EDGE_POL_EDGE(params, idx); |
| 367 |
|
| 368 |
if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING)) |
| 369 |
val |= REG_EDGE_POL_LOW(params, idx); |
| 370 |
} |
| 371 |
|
| 372 |
meson_gpio_irq_update_bits(ctl, REG_EDGE_POL, |
| 373 |
REG_EDGE_POL_MASK(params, idx), val); |
| 374 |
|
| 375 |
return 0; |
| 376 |
} |
| 377 |
|
| 378 |
/* |
| 379 |
* gpio irq relative registers for s4 |
| 380 |
* -PADCTRL_GPIO_IRQ_CTRL0 |
| 381 |
* bit[31]: enable/disable all the irq lines |
| 382 |
* bit[12-23]: single edge trigger |
| 383 |
* bit[0-11]: polarity trigger |
| 384 |
* |
| 385 |
* -PADCTRL_GPIO_IRQ_CTRL[X] |
| 386 |
* bit[0-16]: 7 bits to choose gpio source for irq line 2*[X] - 2 |
| 387 |
* bit[16-22]:7 bits to choose gpio source for irq line 2*[X] - 1 |
| 388 |
* where X = 1-6 |
| 389 |
* |
| 390 |
* -PADCTRL_GPIO_IRQ_CTRL[7] |
| 391 |
* bit[0-11]: both edge trigger |
| 392 |
*/ |
| 393 |
static int meson_s4_gpio_irq_set_type(struct meson_gpio_irq_controller *ctl, |
| 394 |
unsigned int type, u32 *channel_hwirq) |
| 395 |
{ |
| 396 |
const struct meson_gpio_irq_params *params = ctl->params; |
| 397 |
unsigned int idx; |
| 398 |
u32 val = 0; |
| 399 |
|
| 400 |
idx = meson_gpio_irq_get_channel_idx(ctl, channel_hwirq); |
| 401 |
|
| 402 |
type &= IRQ_TYPE_SENSE_MASK; |
| 403 |
|
| 404 |
meson_gpio_irq_update_bits(ctl, params->edge_pol_reg, BIT(idx), 0); |
| 405 |
|
| 406 |
if (type == IRQ_TYPE_EDGE_BOTH) { |
| 407 |
val = BIT(ctl->params->edge_both_offset + idx); |
| 408 |
meson_gpio_irq_update_bits(ctl, params->edge_pol_reg, val, val); |
| 409 |
return 0; |
| 410 |
} |
| 411 |
|
| 412 |
if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING)) |
| 413 |
val |= BIT(ctl->params->pol_low_offset + idx); |
| 414 |
|
| 415 |
if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) |
| 416 |
val |= BIT(ctl->params->edge_single_offset + idx); |
| 417 |
|
| 418 |
meson_gpio_irq_update_bits(ctl, REG_EDGE_POL, BIT(idx) | BIT(12 + idx), val); |
| 419 |
return 0; |
| 420 |
}; |
| 421 |
|
| 422 |
static unsigned int meson_gpio_irq_type_output(unsigned int type) |
| 423 |
{ |
| 424 |
unsigned int sense = type & IRQ_TYPE_SENSE_MASK; |
| 425 |
|
| 426 |
type &= ~IRQ_TYPE_SENSE_MASK; |
| 427 |
|
| 428 |
/* |
| 429 |
* The polarity of the signal provided to the GIC should always |
| 430 |
* be high. |
| 431 |
*/ |
| 432 |
if (sense & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) |
| 433 |
type |= IRQ_TYPE_LEVEL_HIGH; |
| 434 |
else |
| 435 |
type |= IRQ_TYPE_EDGE_RISING; |
| 436 |
|
| 437 |
return type; |
| 438 |
} |
| 439 |
|
| 440 |
static int meson_gpio_irq_set_type(struct irq_data *data, unsigned int type) |
| 441 |
{ |
| 442 |
struct meson_gpio_irq_controller *ctl = data->domain->host_data; |
| 443 |
u32 *channel_hwirq = irq_data_get_irq_chip_data(data); |
| 444 |
int ret; |
| 445 |
|
| 446 |
ret = ctl->params->ops.gpio_irq_set_type(ctl, type, channel_hwirq); |
| 447 |
if (ret) |
| 448 |
return ret; |
| 449 |
|
| 450 |
return irq_chip_set_type_parent(data, |
| 451 |
meson_gpio_irq_type_output(type)); |
| 452 |
} |
| 453 |
|
| 454 |
static struct irq_chip meson_gpio_irq_chip = { |
| 455 |
.name = "meson-gpio-irqchip", |
| 456 |
.irq_mask = irq_chip_mask_parent, |
| 457 |
.irq_unmask = irq_chip_unmask_parent, |
| 458 |
.irq_eoi = irq_chip_eoi_parent, |
| 459 |
.irq_set_type = meson_gpio_irq_set_type, |
| 460 |
.irq_retrigger = irq_chip_retrigger_hierarchy, |
| 461 |
#ifdef CONFIG_SMP |
| 462 |
.irq_set_affinity = irq_chip_set_affinity_parent, |
| 463 |
#endif |
| 464 |
.flags = IRQCHIP_SET_TYPE_MASKED, |
| 465 |
}; |
| 466 |
|
| 467 |
static int meson_gpio_irq_domain_translate(struct irq_domain *domain, |
| 468 |
struct irq_fwspec *fwspec, |
| 469 |
unsigned long *hwirq, |
| 470 |
unsigned int *type) |
| 471 |
{ |
| 472 |
if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) { |
| 473 |
*hwirq = fwspec->param[0]; |
| 474 |
*type = fwspec->param[1]; |
| 475 |
return 0; |
| 476 |
} |
| 477 |
|
| 478 |
return -EINVAL; |
| 479 |
} |
| 480 |
|
| 481 |
static int meson_gpio_irq_allocate_gic_irq(struct irq_domain *domain, |
| 482 |
unsigned int virq, |
| 483 |
u32 hwirq, |
| 484 |
unsigned int type) |
| 485 |
{ |
| 486 |
struct irq_fwspec fwspec; |
| 487 |
|
| 488 |
fwspec.fwnode = domain->parent->fwnode; |
| 489 |
fwspec.param_count = 3; |
| 490 |
fwspec.param[0] = 0; /* SPI */ |
| 491 |
fwspec.param[1] = hwirq; |
| 492 |
fwspec.param[2] = meson_gpio_irq_type_output(type); |
| 493 |
|
| 494 |
return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec); |
| 495 |
} |
| 496 |
|
| 497 |
static int meson_gpio_irq_domain_alloc(struct irq_domain *domain, |
| 498 |
unsigned int virq, |
| 499 |
unsigned int nr_irqs, |
| 500 |
void *data) |
| 501 |
{ |
| 502 |
struct irq_fwspec *fwspec = data; |
| 503 |
struct meson_gpio_irq_controller *ctl = domain->host_data; |
| 504 |
unsigned long hwirq; |
| 505 |
u32 *channel_hwirq; |
| 506 |
unsigned int type; |
| 507 |
int ret; |
| 508 |
|
| 509 |
if (WARN_ON(nr_irqs != 1)) |
| 510 |
return -EINVAL; |
| 511 |
|
| 512 |
ret = meson_gpio_irq_domain_translate(domain, fwspec, &hwirq, &type); |
| 513 |
if (ret) |
| 514 |
return ret; |
| 515 |
|
| 516 |
ret = meson_gpio_irq_request_channel(ctl, hwirq, &channel_hwirq); |
| 517 |
if (ret) |
| 518 |
return ret; |
| 519 |
|
| 520 |
ret = meson_gpio_irq_allocate_gic_irq(domain, virq, |
| 521 |
*channel_hwirq, type); |
| 522 |
if (ret < 0) { |
| 523 |
pr_err("failed to allocate gic irq %u\n", *channel_hwirq); |
| 524 |
meson_gpio_irq_release_channel(ctl, channel_hwirq); |
| 525 |
return ret; |
| 526 |
} |
| 527 |
|
| 528 |
irq_domain_set_hwirq_and_chip(domain, virq, hwirq, |
| 529 |
&meson_gpio_irq_chip, channel_hwirq); |
| 530 |
|
| 531 |
return 0; |
| 532 |
} |
| 533 |
|
| 534 |
static void meson_gpio_irq_domain_free(struct irq_domain *domain, |
| 535 |
unsigned int virq, |
| 536 |
unsigned int nr_irqs) |
| 537 |
{ |
| 538 |
struct meson_gpio_irq_controller *ctl = domain->host_data; |
| 539 |
struct irq_data *irq_data; |
| 540 |
u32 *channel_hwirq; |
| 541 |
|
| 542 |
if (WARN_ON(nr_irqs != 1)) |
| 543 |
return; |
| 544 |
|
| 545 |
irq_domain_free_irqs_parent(domain, virq, 1); |
| 546 |
|
| 547 |
irq_data = irq_domain_get_irq_data(domain, virq); |
| 548 |
channel_hwirq = irq_data_get_irq_chip_data(irq_data); |
| 549 |
|
| 550 |
meson_gpio_irq_release_channel(ctl, channel_hwirq); |
| 551 |
} |
| 552 |
|
| 553 |
static const struct irq_domain_ops meson_gpio_irq_domain_ops = { |
| 554 |
.alloc = meson_gpio_irq_domain_alloc, |
| 555 |
.free = meson_gpio_irq_domain_free, |
| 556 |
.translate = meson_gpio_irq_domain_translate, |
| 557 |
}; |
| 558 |
|
| 559 |
static int meson_gpio_irq_parse_dt(struct device_node *node, struct meson_gpio_irq_controller *ctl) |
| 560 |
{ |
| 561 |
const struct of_device_id *match; |
| 562 |
int ret; |
| 563 |
|
| 564 |
match = of_match_node(meson_irq_gpio_matches, node); |
| 565 |
if (!match) |
| 566 |
return -ENODEV; |
| 567 |
|
| 568 |
ctl->params = match->data; |
| 569 |
|
| 570 |
ret = of_property_read_variable_u32_array(node, |
| 571 |
"amlogic,channel-interrupts", |
| 572 |
ctl->channel_irqs, |
| 573 |
ctl->params->nr_channels, |
| 574 |
ctl->params->nr_channels); |
| 575 |
if (ret < 0) { |
| 576 |
pr_err("can't get %d channel interrupts\n", ctl->params->nr_channels); |
| 577 |
return ret; |
| 578 |
} |
| 579 |
|
| 580 |
ctl->params->ops.gpio_irq_init(ctl); |
| 581 |
|
| 582 |
return 0; |
| 583 |
} |
| 584 |
|
| 585 |
static int meson_gpio_irq_probe(struct platform_device *pdev, struct device_node *parent) |
| 586 |
{ |
| 587 |
struct device_node *node = pdev->dev.of_node; |
| 588 |
struct irq_domain *domain, *parent_domain; |
| 589 |
struct meson_gpio_irq_controller *ctl; |
| 590 |
int ret; |
| 591 |
|
| 592 |
if (!parent) { |
| 593 |
pr_err("missing parent interrupt node\n"); |
| 594 |
return -ENODEV; |
| 595 |
} |
| 596 |
|
| 597 |
parent_domain = irq_find_host(parent); |
| 598 |
if (!parent_domain) { |
| 599 |
pr_err("unable to obtain parent domain\n"); |
| 600 |
return -ENXIO; |
| 601 |
} |
| 602 |
|
| 603 |
ctl = kzalloc_obj(*ctl); |
| 604 |
if (!ctl) |
| 605 |
return -ENOMEM; |
| 606 |
|
| 607 |
raw_spin_lock_init(&ctl->lock); |
| 608 |
|
| 609 |
ctl->base = of_iomap(node, 0); |
| 610 |
if (!ctl->base) { |
| 611 |
ret = -ENOMEM; |
| 612 |
goto free_ctl; |
| 613 |
} |
| 614 |
|
| 615 |
ret = meson_gpio_irq_parse_dt(node, ctl); |
| 616 |
if (ret) |
| 617 |
goto free_channel_irqs; |
| 618 |
|
| 619 |
domain = irq_domain_create_hierarchy(parent_domain, 0, |
| 620 |
ctl->params->nr_hwirq, |
| 621 |
of_fwnode_handle(node), |
| 622 |
&meson_gpio_irq_domain_ops, |
| 623 |
ctl); |
| 624 |
if (!domain) { |
| 625 |
pr_err("failed to add domain\n"); |
| 626 |
ret = -ENODEV; |
| 627 |
goto free_channel_irqs; |
| 628 |
} |
| 629 |
|
| 630 |
pr_info("%d to %d gpio interrupt mux initialized\n", |
| 631 |
ctl->params->nr_hwirq, ctl->params->nr_channels); |
| 632 |
|
| 633 |
return 0; |
| 634 |
|
| 635 |
free_channel_irqs: |
| 636 |
iounmap(ctl->base); |
| 637 |
free_ctl: |
| 638 |
kfree(ctl); |
| 639 |
|
| 640 |
return ret; |
| 641 |
} |
| 642 |
|
| 643 |
IRQCHIP_PLATFORM_DRIVER_BEGIN(meson_gpio_intc) |
| 644 |
IRQCHIP_MATCH("amlogic,meson-gpio-intc", meson_gpio_irq_probe) |
| 645 |
IRQCHIP_PLATFORM_DRIVER_END(meson_gpio_intc) |
| 646 |
|
| 647 |
MODULE_AUTHOR("Jerome Brunet <[email protected]>"); |
| 648 |
MODULE_DESCRIPTION("Meson GPIO Interrupt Multiplexer driver"); |
| 649 |
MODULE_LICENSE("GPL v2"); |