summaryrefslogtreecommitdiff
path: root/drivers/irqchip/irq-imx-intmux.c
blob: a585003eec729a0eea446ffd006af5a3d0696adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Copyright 2017 NXP
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/clk.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/irqchip/chained_irq.h>
#include <linux/irqdomain.h>
#include <linux/of_platform.h>
#include <linux/spinlock.h>
#include <linux/pm_runtime.h>

#define CHANCSR(n)	(0x0 + 0x40 * n)
#define CHANVEC(n)	(0x4 + 0x40 * n)
#define CHANIER(n)	(0x10 + (0x40 * n))
#define CHANIPR(n)	(0x20 + (0x40 * n))

struct intmux_irqchip_data {
	struct irq_chip chip;
	int chanidx;
	int irq;
	struct irq_domain *domain;
	unsigned long irqstat;
};


struct intmux_data {
	spinlock_t lock;
	struct platform_device	*pdev;
	void __iomem *regs;
	struct clk *ipg_clk;
	int channum;
#ifdef CONFIG_PM
	u32 *saved_reg;
#endif
	struct intmux_irqchip_data irqchip_data[];
};

static void imx_intmux_irq_unmask(struct irq_data *d)
{
	struct intmux_irqchip_data *irqchip_data = d->chip_data;
	u32 idx = irqchip_data->chanidx;
	struct intmux_data *intmux_data = container_of(irqchip_data, struct intmux_data, irqchip_data[idx]);
	void __iomem *reg;
	u32 val;

	spin_lock(&intmux_data->lock);
	reg = intmux_data->regs + CHANIER(idx);
	val = readl_relaxed(reg);
	val |= 1 << d->hwirq;
	writel_relaxed(val, reg);
	spin_unlock(&intmux_data->lock);
}

static void imx_intmux_irq_mask(struct irq_data *d)
{
	struct intmux_irqchip_data *irqchip_data = d->chip_data;
	u32 idx = irqchip_data->chanidx;
	struct intmux_data *intmux_data = container_of(irqchip_data, struct intmux_data, irqchip_data[idx]);
	void __iomem *reg;
	u32 val;

	spin_lock(&intmux_data->lock);
	reg = intmux_data->regs + CHANIER(idx);
	val = readl_relaxed(reg);
	val &= ~(1 << d->hwirq);
	writel_relaxed(val, reg);
	spin_unlock(&intmux_data->lock);
}

static void imx_intmux_irq_ack(struct irq_data *d)
{
	/* the irqchip has no ack */
}

static struct irq_chip imx_intmux_irq_chip = {
	.name		= "intmux",
	.irq_eoi	= irq_chip_eoi_parent,
	.irq_mask	= imx_intmux_irq_mask,
	.irq_unmask	= imx_intmux_irq_unmask,
	.irq_ack	= imx_intmux_irq_ack,
};

static int imx_intmux_irq_map(struct irq_domain *h, unsigned int irq,
				irq_hw_number_t hwirq)
{
	struct intmux_irqchip_data *irqchip_data = h->host_data;

	irq_set_chip_data(irq, h->host_data);
	irq_set_chip_and_handler(irq, &irqchip_data->chip, handle_edge_irq);

	return 0;
}

static const struct irq_domain_ops imx_intmux_domain_ops = {
	.map		= imx_intmux_irq_map,
	.xlate		= irq_domain_xlate_twocell,
};

static void imx_intmux_update_irqstat(struct intmux_irqchip_data *irqchip_data)
{
	int i = irqchip_data->chanidx;
	struct intmux_data *intmux_data = container_of(irqchip_data, struct intmux_data, irqchip_data[i]);

	irqchip_data->irqstat = readl_relaxed(intmux_data->regs + CHANIPR(i));
}

static void imx_intmux_irq_handler(struct irq_desc *desc)
{
	struct intmux_irqchip_data *irqchip_data = irq_desc_get_handler_data(desc);
	int pos, virq;

	chained_irq_enter(irq_desc_get_chip(desc), desc);

	imx_intmux_update_irqstat(irqchip_data);

	for_each_set_bit(pos, &irqchip_data->irqstat, 32) {
		virq = irq_find_mapping(irqchip_data->domain, pos);
		if (virq)
			generic_handle_irq(virq);
	}

	chained_irq_exit(irq_desc_get_chip(desc), desc);
}

static int imx_intmux_probe(struct platform_device *pdev)
{
	struct device_node *np = pdev->dev.of_node;
	struct intmux_data *intmux_data;
	struct resource *res;
	int i;
	int channum;
	int ret;

	ret = of_property_read_u32(np, "nxp,intmux_chans", &channum);
	if (ret)
		channum = 1;

	intmux_data = devm_kzalloc(&pdev->dev, sizeof(*intmux_data) +
				     channum *
				     sizeof(intmux_data->irqchip_data[0]),
				     GFP_KERNEL);
	if (!intmux_data)
		return -ENOMEM;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	intmux_data->regs = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(intmux_data->regs)) {
		dev_err(&pdev->dev, "failed to initialize reg\n");
		return PTR_ERR(intmux_data->regs);
	}

	intmux_data->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
	if (IS_ERR(intmux_data->ipg_clk)) {
		ret = PTR_ERR(intmux_data->ipg_clk);
		dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
		return ret;
	}

	intmux_data->channum = channum;
	intmux_data->pdev = pdev;
	spin_lock_init(&intmux_data->lock);

	if (IS_ENABLED(CONFIG_PM)) {
		/* save CHANIER register */
		intmux_data->saved_reg = devm_kzalloc(&pdev->dev,
						      sizeof(u32) * channum,
						      GFP_KERNEL);
		if (!intmux_data->saved_reg)
			return -ENOMEM;
	}

	for (i = 0; i < channum; i++) {
		intmux_data->irqchip_data[i].chip = imx_intmux_irq_chip;
		intmux_data->irqchip_data[i].chip.parent_device = &pdev->dev;
		intmux_data->irqchip_data[i].chanidx = i;
		intmux_data->irqchip_data[i].irq = platform_get_irq(pdev, i);
		if (intmux_data->irqchip_data[i].irq <= 0) {
			dev_err(&pdev->dev, "failed to get irq\n");
			return -ENODEV;
		}

		intmux_data->irqchip_data[i].domain = irq_domain_add_linear(np,
						 32,
						 &imx_intmux_domain_ops,
						 &intmux_data->irqchip_data[i]);
		if (!intmux_data->irqchip_data[i].domain) {
			dev_err(&intmux_data->pdev->dev,
				"failed to create IRQ domain\n");
			return -ENOMEM;
		}

		irq_set_chained_handler_and_data(intmux_data->irqchip_data[i].irq,
					 imx_intmux_irq_handler,
					 &intmux_data->irqchip_data[i]);
	}

	platform_set_drvdata(pdev, intmux_data);

	pm_runtime_get_noresume(&pdev->dev);
	pm_runtime_set_active(&pdev->dev);
	pm_runtime_enable(&pdev->dev);

	ret = clk_prepare_enable(intmux_data->ipg_clk);
	if (ret) {
		dev_err(&pdev->dev, "failed to enable ipg clk: %d\n", ret);
		return ret;
	}
	/*
	 * Let pm_runtime_put() disable clock.
	 * If CONFIG_PM is not enabled, the clock will stay powered.
	 */
	pm_runtime_put(&pdev->dev);

	return 0;
}

static int imx_intmux_remove(struct platform_device *pdev)
{
	struct intmux_data *intmux_data = platform_get_drvdata(pdev);
	int i;

	for (i = 0; i < intmux_data->channum; i++) {
		irq_set_chained_handler_and_data(intmux_data->irqchip_data[i].irq, NULL, NULL);

		irq_domain_remove(intmux_data->irqchip_data[i].domain);
	}

	platform_set_drvdata(pdev, NULL);
	pm_runtime_disable(&pdev->dev);

	return 0;
}

#ifdef CONFIG_PM
static void imx_intmux_save_regs(struct intmux_data *intmux_data)
{
	int i;

	for (i = 0; i < intmux_data->channum; i++)
		intmux_data->saved_reg[i] = readl_relaxed(intmux_data->regs
							  + CHANIER(i));
}

static void imx_intmux_restore_regs(struct intmux_data *intmux_data)
{
	int i;

	for (i = 0; i < intmux_data->channum; i++)
		writel_relaxed(intmux_data->saved_reg[i], intmux_data->regs
			       + CHANIER(i));
}

static int imx_intmux_runtime_suspend(struct device *dev)
{
	struct intmux_data *intmux_data = dev_get_drvdata(dev);

	imx_intmux_save_regs(intmux_data);
	clk_disable_unprepare(intmux_data->ipg_clk);

	return 0;
}

static int imx_intmux_runtime_resume(struct device *dev)
{
	struct intmux_data *intmux_data = dev_get_drvdata(dev);
	int ret;

	ret = clk_prepare_enable(intmux_data->ipg_clk);
	if (ret) {
		dev_err(dev, "failed to enable ipg clk: %d\n", ret);
		return ret;
	}
	imx_intmux_restore_regs(intmux_data);

	return 0;
}
#endif

static const struct dev_pm_ops imx_intmux_pm_ops = {
	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
				      pm_runtime_force_resume)
	SET_RUNTIME_PM_OPS(imx_intmux_runtime_suspend,
			   imx_intmux_runtime_resume, NULL)
};

static const struct of_device_id imx_intmux_id[] = {
	{ .compatible = "nxp,imx-intmux", },
	{},
};

static struct platform_driver imx_intmux_driver = {
	.driver = {
		.name = "imx-intmux",
		.of_match_table = imx_intmux_id,
		.pm = &imx_intmux_pm_ops,
	},
	.probe = imx_intmux_probe,
	.remove = imx_intmux_remove,
};

static int __init irq_imx_intmux_init(void)
{
	return platform_driver_register(&imx_intmux_driver);
}
arch_initcall(irq_imx_intmux_init);

MODULE_AUTHOR("NXP Semiconductor");
MODULE_DESCRIPTION("NXP i.MX8 irq steering driver");
MODULE_LICENSE("GPL v2");