Linux Audio

Check our new training course

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