1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f48e699dSBrendan Higgins /*
3f48e699dSBrendan Higgins * Aspeed 24XX/25XX I2C Interrupt Controller.
4f48e699dSBrendan Higgins *
5f48e699dSBrendan Higgins * Copyright (C) 2012-2017 ASPEED Technology Inc.
6f48e699dSBrendan Higgins * Copyright 2017 IBM Corporation
7f48e699dSBrendan Higgins * Copyright 2017 Google, Inc.
8f48e699dSBrendan Higgins */
9f48e699dSBrendan Higgins
10f48e699dSBrendan Higgins #include <linux/irq.h>
11f48e699dSBrendan Higgins #include <linux/irqchip.h>
12f48e699dSBrendan Higgins #include <linux/irqchip/chained_irq.h>
13f48e699dSBrendan Higgins #include <linux/irqdomain.h>
14f48e699dSBrendan Higgins #include <linux/of_address.h>
15f48e699dSBrendan Higgins #include <linux/of_irq.h>
16f48e699dSBrendan Higgins #include <linux/io.h>
17f48e699dSBrendan Higgins
18f48e699dSBrendan Higgins
19f48e699dSBrendan Higgins #define ASPEED_I2C_IC_NUM_BUS 14
20f48e699dSBrendan Higgins
21f48e699dSBrendan Higgins struct aspeed_i2c_ic {
22f48e699dSBrendan Higgins void __iomem *base;
23f48e699dSBrendan Higgins int parent_irq;
24f48e699dSBrendan Higgins struct irq_domain *irq_domain;
25f48e699dSBrendan Higgins };
26f48e699dSBrendan Higgins
27f48e699dSBrendan Higgins /*
28f48e699dSBrendan Higgins * The aspeed chip provides a single hardware interrupt for all of the I2C
29f48e699dSBrendan Higgins * busses, so we use a dummy interrupt chip to translate this single interrupt
30f48e699dSBrendan Higgins * into multiple interrupts, each associated with a single I2C bus.
31f48e699dSBrendan Higgins */
aspeed_i2c_ic_irq_handler(struct irq_desc * desc)32f48e699dSBrendan Higgins static void aspeed_i2c_ic_irq_handler(struct irq_desc *desc)
33f48e699dSBrendan Higgins {
34f48e699dSBrendan Higgins struct aspeed_i2c_ic *i2c_ic = irq_desc_get_handler_data(desc);
35f48e699dSBrendan Higgins struct irq_chip *chip = irq_desc_get_chip(desc);
36f48e699dSBrendan Higgins unsigned long bit, status;
37f48e699dSBrendan Higgins
38f48e699dSBrendan Higgins chained_irq_enter(chip, desc);
39f48e699dSBrendan Higgins status = readl(i2c_ic->base);
40046a6ee2SMarc Zyngier for_each_set_bit(bit, &status, ASPEED_I2C_IC_NUM_BUS)
41046a6ee2SMarc Zyngier generic_handle_domain_irq(i2c_ic->irq_domain, bit);
42046a6ee2SMarc Zyngier
43f48e699dSBrendan Higgins chained_irq_exit(chip, desc);
44f48e699dSBrendan Higgins }
45f48e699dSBrendan Higgins
46f48e699dSBrendan Higgins /*
47f48e699dSBrendan Higgins * Set simple handler and mark IRQ as valid. Nothing interesting to do here
48f48e699dSBrendan Higgins * since we are using a dummy interrupt chip.
49f48e699dSBrendan Higgins */
aspeed_i2c_ic_map_irq_domain(struct irq_domain * domain,unsigned int irq,irq_hw_number_t hwirq)50f48e699dSBrendan Higgins static int aspeed_i2c_ic_map_irq_domain(struct irq_domain *domain,
51f48e699dSBrendan Higgins unsigned int irq, irq_hw_number_t hwirq)
52f48e699dSBrendan Higgins {
53f48e699dSBrendan Higgins irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
54f48e699dSBrendan Higgins irq_set_chip_data(irq, domain->host_data);
55f48e699dSBrendan Higgins
56f48e699dSBrendan Higgins return 0;
57f48e699dSBrendan Higgins }
58f48e699dSBrendan Higgins
59f48e699dSBrendan Higgins static const struct irq_domain_ops aspeed_i2c_ic_irq_domain_ops = {
60f48e699dSBrendan Higgins .map = aspeed_i2c_ic_map_irq_domain,
61f48e699dSBrendan Higgins };
62f48e699dSBrendan Higgins
aspeed_i2c_ic_of_init(struct device_node * node,struct device_node * parent)63f48e699dSBrendan Higgins static int __init aspeed_i2c_ic_of_init(struct device_node *node,
64f48e699dSBrendan Higgins struct device_node *parent)
65f48e699dSBrendan Higgins {
66f48e699dSBrendan Higgins struct aspeed_i2c_ic *i2c_ic;
67f48e699dSBrendan Higgins int ret = 0;
68f48e699dSBrendan Higgins
69f48e699dSBrendan Higgins i2c_ic = kzalloc(sizeof(*i2c_ic), GFP_KERNEL);
70f48e699dSBrendan Higgins if (!i2c_ic)
71f48e699dSBrendan Higgins return -ENOMEM;
72f48e699dSBrendan Higgins
73f48e699dSBrendan Higgins i2c_ic->base = of_iomap(node, 0);
747bdeb7f5SWei Yongjun if (!i2c_ic->base) {
757bdeb7f5SWei Yongjun ret = -ENOMEM;
76f48e699dSBrendan Higgins goto err_free_ic;
77f48e699dSBrendan Higgins }
78f48e699dSBrendan Higgins
79f48e699dSBrendan Higgins i2c_ic->parent_irq = irq_of_parse_and_map(node, 0);
80*50f0f26eSKrzysztof Kozlowski if (!i2c_ic->parent_irq) {
81*50f0f26eSKrzysztof Kozlowski ret = -EINVAL;
82f48e699dSBrendan Higgins goto err_iounmap;
83f48e699dSBrendan Higgins }
84f48e699dSBrendan Higgins
85f48e699dSBrendan Higgins i2c_ic->irq_domain = irq_domain_add_linear(node, ASPEED_I2C_IC_NUM_BUS,
86f48e699dSBrendan Higgins &aspeed_i2c_ic_irq_domain_ops,
87f48e699dSBrendan Higgins NULL);
88f48e699dSBrendan Higgins if (!i2c_ic->irq_domain) {
89f48e699dSBrendan Higgins ret = -ENOMEM;
90f48e699dSBrendan Higgins goto err_iounmap;
91f48e699dSBrendan Higgins }
92f48e699dSBrendan Higgins
93f48e699dSBrendan Higgins i2c_ic->irq_domain->name = "aspeed-i2c-domain";
94f48e699dSBrendan Higgins
95f48e699dSBrendan Higgins irq_set_chained_handler_and_data(i2c_ic->parent_irq,
96f48e699dSBrendan Higgins aspeed_i2c_ic_irq_handler, i2c_ic);
97f48e699dSBrendan Higgins
98f48e699dSBrendan Higgins pr_info("i2c controller registered, irq %d\n", i2c_ic->parent_irq);
99f48e699dSBrendan Higgins
100f48e699dSBrendan Higgins return 0;
101f48e699dSBrendan Higgins
102f48e699dSBrendan Higgins err_iounmap:
103f48e699dSBrendan Higgins iounmap(i2c_ic->base);
104f48e699dSBrendan Higgins err_free_ic:
105f48e699dSBrendan Higgins kfree(i2c_ic);
106f48e699dSBrendan Higgins return ret;
107f48e699dSBrendan Higgins }
108f48e699dSBrendan Higgins
109f48e699dSBrendan Higgins IRQCHIP_DECLARE(ast2400_i2c_ic, "aspeed,ast2400-i2c-ic", aspeed_i2c_ic_of_init);
110f48e699dSBrendan Higgins IRQCHIP_DECLARE(ast2500_i2c_ic, "aspeed,ast2500-i2c-ic", aspeed_i2c_ic_of_init);
111