1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/gpio/driver.h>
5 #include <linux/of_gpio.h>
7 #include <linux/interrupt.h>
8 #include <linux/platform_device.h>
10 #define ETRAX_FS_rw_pa_dout 0
11 #define ETRAX_FS_r_pa_din 4
12 #define ETRAX_FS_rw_pa_oe 8
13 #define ETRAX_FS_rw_intr_cfg 12
14 #define ETRAX_FS_rw_intr_mask 16
15 #define ETRAX_FS_rw_ack_intr 20
16 #define ETRAX_FS_r_intr 24
17 #define ETRAX_FS_r_masked_intr 28
18 #define ETRAX_FS_rw_pb_dout 32
19 #define ETRAX_FS_r_pb_din 36
20 #define ETRAX_FS_rw_pb_oe 40
21 #define ETRAX_FS_rw_pc_dout 48
22 #define ETRAX_FS_r_pc_din 52
23 #define ETRAX_FS_rw_pc_oe 56
24 #define ETRAX_FS_rw_pd_dout 64
25 #define ETRAX_FS_r_pd_din 68
26 #define ETRAX_FS_rw_pd_oe 72
27 #define ETRAX_FS_rw_pe_dout 80
28 #define ETRAX_FS_r_pe_din 84
29 #define ETRAX_FS_rw_pe_oe 88
31 #define ARTPEC3_r_pa_din 0
32 #define ARTPEC3_rw_pa_dout 4
33 #define ARTPEC3_rw_pa_oe 8
34 #define ARTPEC3_r_pb_din 44
35 #define ARTPEC3_rw_pb_dout 48
36 #define ARTPEC3_rw_pb_oe 52
37 #define ARTPEC3_r_pc_din 88
38 #define ARTPEC3_rw_pc_dout 92
39 #define ARTPEC3_rw_pc_oe 96
40 #define ARTPEC3_r_pd_din 116
41 #define ARTPEC3_rw_intr_cfg 120
42 #define ARTPEC3_rw_intr_pins 124
43 #define ARTPEC3_rw_intr_mask 128
44 #define ARTPEC3_rw_ack_intr 132
45 #define ARTPEC3_r_masked_intr 140
51 #define GIO_CFG_POSEDGE 5
52 #define GIO_CFG_NEGEDGE 6
53 #define GIO_CFG_ANYEDGE 7
55 struct etraxfs_gpio_info
;
57 struct etraxfs_gpio_block
{
62 unsigned int group
[8];
65 const struct etraxfs_gpio_info
*info
;
68 struct etraxfs_gpio_chip
{
70 struct etraxfs_gpio_block
*block
;
73 struct etraxfs_gpio_port
{
81 struct etraxfs_gpio_info
{
82 unsigned int num_ports
;
83 const struct etraxfs_gpio_port
*ports
;
85 unsigned int rw_ack_intr
;
86 unsigned int rw_intr_mask
;
87 unsigned int rw_intr_cfg
;
88 unsigned int rw_intr_pins
;
89 unsigned int r_masked_intr
;
92 static const struct etraxfs_gpio_port etraxfs_gpio_etraxfs_ports
[] = {
96 .oe
= ETRAX_FS_rw_pa_oe
,
97 .dout
= ETRAX_FS_rw_pa_dout
,
98 .din
= ETRAX_FS_r_pa_din
,
103 .oe
= ETRAX_FS_rw_pb_oe
,
104 .dout
= ETRAX_FS_rw_pb_dout
,
105 .din
= ETRAX_FS_r_pb_din
,
110 .oe
= ETRAX_FS_rw_pc_oe
,
111 .dout
= ETRAX_FS_rw_pc_dout
,
112 .din
= ETRAX_FS_r_pc_din
,
117 .oe
= ETRAX_FS_rw_pd_oe
,
118 .dout
= ETRAX_FS_rw_pd_dout
,
119 .din
= ETRAX_FS_r_pd_din
,
124 .oe
= ETRAX_FS_rw_pe_oe
,
125 .dout
= ETRAX_FS_rw_pe_dout
,
126 .din
= ETRAX_FS_r_pe_din
,
130 static const struct etraxfs_gpio_info etraxfs_gpio_etraxfs
= {
131 .num_ports
= ARRAY_SIZE(etraxfs_gpio_etraxfs_ports
),
132 .ports
= etraxfs_gpio_etraxfs_ports
,
133 .rw_ack_intr
= ETRAX_FS_rw_ack_intr
,
134 .rw_intr_mask
= ETRAX_FS_rw_intr_mask
,
135 .rw_intr_cfg
= ETRAX_FS_rw_intr_cfg
,
136 .r_masked_intr
= ETRAX_FS_r_masked_intr
,
139 static const struct etraxfs_gpio_port etraxfs_gpio_artpec3_ports
[] = {
143 .oe
= ARTPEC3_rw_pa_oe
,
144 .dout
= ARTPEC3_rw_pa_dout
,
145 .din
= ARTPEC3_r_pa_din
,
150 .oe
= ARTPEC3_rw_pb_oe
,
151 .dout
= ARTPEC3_rw_pb_dout
,
152 .din
= ARTPEC3_r_pb_din
,
157 .oe
= ARTPEC3_rw_pc_oe
,
158 .dout
= ARTPEC3_rw_pc_dout
,
159 .din
= ARTPEC3_r_pc_din
,
164 .din
= ARTPEC3_r_pd_din
,
168 static const struct etraxfs_gpio_info etraxfs_gpio_artpec3
= {
169 .num_ports
= ARRAY_SIZE(etraxfs_gpio_artpec3_ports
),
170 .ports
= etraxfs_gpio_artpec3_ports
,
171 .rw_ack_intr
= ARTPEC3_rw_ack_intr
,
172 .rw_intr_mask
= ARTPEC3_rw_intr_mask
,
173 .rw_intr_cfg
= ARTPEC3_rw_intr_cfg
,
174 .r_masked_intr
= ARTPEC3_r_masked_intr
,
175 .rw_intr_pins
= ARTPEC3_rw_intr_pins
,
178 static unsigned int etraxfs_gpio_chip_to_port(struct gpio_chip
*gc
)
180 return gc
->label
[0] - 'A';
183 static int etraxfs_gpio_of_xlate(struct gpio_chip
*gc
,
184 const struct of_phandle_args
*gpiospec
,
188 * Port numbers are A to E, and the properties are integers, so we
189 * specify them as 0xA - 0xE.
191 if (etraxfs_gpio_chip_to_port(gc
) + 0xA != gpiospec
->args
[2])
194 return of_gpio_simple_xlate(gc
, gpiospec
, flags
);
197 static const struct of_device_id etraxfs_gpio_of_table
[] = {
199 .compatible
= "axis,etraxfs-gio",
200 .data
= &etraxfs_gpio_etraxfs
,
203 .compatible
= "axis,artpec3-gio",
204 .data
= &etraxfs_gpio_artpec3
,
209 static unsigned int etraxfs_gpio_to_group_irq(unsigned int gpio
)
214 static unsigned int etraxfs_gpio_to_group_pin(struct etraxfs_gpio_chip
*chip
,
217 return 4 * etraxfs_gpio_chip_to_port(&chip
->gc
) + gpio
/ 8;
220 static void etraxfs_gpio_irq_ack(struct irq_data
*d
)
222 struct etraxfs_gpio_chip
*chip
=
223 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
224 struct etraxfs_gpio_block
*block
= chip
->block
;
225 unsigned int grpirq
= etraxfs_gpio_to_group_irq(d
->hwirq
);
227 writel(BIT(grpirq
), block
->regs
+ block
->info
->rw_ack_intr
);
230 static void etraxfs_gpio_irq_mask(struct irq_data
*d
)
232 struct etraxfs_gpio_chip
*chip
=
233 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
234 struct etraxfs_gpio_block
*block
= chip
->block
;
235 unsigned int grpirq
= etraxfs_gpio_to_group_irq(d
->hwirq
);
237 raw_spin_lock(&block
->lock
);
238 block
->mask
&= ~BIT(grpirq
);
239 writel(block
->mask
, block
->regs
+ block
->info
->rw_intr_mask
);
240 raw_spin_unlock(&block
->lock
);
243 static void etraxfs_gpio_irq_unmask(struct irq_data
*d
)
245 struct etraxfs_gpio_chip
*chip
=
246 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
247 struct etraxfs_gpio_block
*block
= chip
->block
;
248 unsigned int grpirq
= etraxfs_gpio_to_group_irq(d
->hwirq
);
250 raw_spin_lock(&block
->lock
);
251 block
->mask
|= BIT(grpirq
);
252 writel(block
->mask
, block
->regs
+ block
->info
->rw_intr_mask
);
253 raw_spin_unlock(&block
->lock
);
256 static int etraxfs_gpio_irq_set_type(struct irq_data
*d
, u32 type
)
258 struct etraxfs_gpio_chip
*chip
=
259 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
260 struct etraxfs_gpio_block
*block
= chip
->block
;
261 unsigned int grpirq
= etraxfs_gpio_to_group_irq(d
->hwirq
);
265 case IRQ_TYPE_EDGE_RISING
:
266 cfg
= GIO_CFG_POSEDGE
;
268 case IRQ_TYPE_EDGE_FALLING
:
269 cfg
= GIO_CFG_NEGEDGE
;
271 case IRQ_TYPE_EDGE_BOTH
:
272 cfg
= GIO_CFG_ANYEDGE
;
274 case IRQ_TYPE_LEVEL_LOW
:
277 case IRQ_TYPE_LEVEL_HIGH
:
284 raw_spin_lock(&block
->lock
);
285 block
->cfg
&= ~(0x7 << (grpirq
* 3));
286 block
->cfg
|= (cfg
<< (grpirq
* 3));
287 writel(block
->cfg
, block
->regs
+ block
->info
->rw_intr_cfg
);
288 raw_spin_unlock(&block
->lock
);
293 static int etraxfs_gpio_irq_request_resources(struct irq_data
*d
)
295 struct etraxfs_gpio_chip
*chip
=
296 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
297 struct etraxfs_gpio_block
*block
= chip
->block
;
298 unsigned int grpirq
= etraxfs_gpio_to_group_irq(d
->hwirq
);
301 raw_spin_lock(&block
->lock
);
302 if (block
->group
[grpirq
])
305 ret
= gpiochip_lock_as_irq(&chip
->gc
, d
->hwirq
);
309 block
->group
[grpirq
] = d
->irq
;
310 if (block
->info
->rw_intr_pins
) {
311 unsigned int pin
= etraxfs_gpio_to_group_pin(chip
, d
->hwirq
);
313 block
->pins
&= ~(0xf << (grpirq
* 4));
314 block
->pins
|= (pin
<< (grpirq
* 4));
316 writel(block
->pins
, block
->regs
+ block
->info
->rw_intr_pins
);
320 raw_spin_unlock(&block
->lock
);
324 static void etraxfs_gpio_irq_release_resources(struct irq_data
*d
)
326 struct etraxfs_gpio_chip
*chip
=
327 gpiochip_get_data(irq_data_get_irq_chip_data(d
));
328 struct etraxfs_gpio_block
*block
= chip
->block
;
329 unsigned int grpirq
= etraxfs_gpio_to_group_irq(d
->hwirq
);
331 raw_spin_lock(&block
->lock
);
332 block
->group
[grpirq
] = 0;
333 gpiochip_unlock_as_irq(&chip
->gc
, d
->hwirq
);
334 raw_spin_unlock(&block
->lock
);
337 static struct irq_chip etraxfs_gpio_irq_chip
= {
338 .name
= "gpio-etraxfs",
339 .irq_ack
= etraxfs_gpio_irq_ack
,
340 .irq_mask
= etraxfs_gpio_irq_mask
,
341 .irq_unmask
= etraxfs_gpio_irq_unmask
,
342 .irq_set_type
= etraxfs_gpio_irq_set_type
,
343 .irq_request_resources
= etraxfs_gpio_irq_request_resources
,
344 .irq_release_resources
= etraxfs_gpio_irq_release_resources
,
347 static irqreturn_t
etraxfs_gpio_interrupt(int irq
, void *dev_id
)
349 struct etraxfs_gpio_block
*block
= dev_id
;
350 unsigned long intr
= readl(block
->regs
+ block
->info
->r_masked_intr
);
353 for_each_set_bit(bit
, &intr
, 8)
354 generic_handle_irq(block
->group
[bit
]);
356 return IRQ_RETVAL(intr
& 0xff);
359 static int etraxfs_gpio_probe(struct platform_device
*pdev
)
361 struct device
*dev
= &pdev
->dev
;
362 const struct etraxfs_gpio_info
*info
;
363 const struct of_device_id
*match
;
364 struct etraxfs_gpio_block
*block
;
365 struct etraxfs_gpio_chip
*chips
;
366 struct resource
*res
, *irq
;
367 bool allportsirq
= false;
372 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
373 regs
= devm_ioremap_resource(dev
, res
);
375 return PTR_ERR(regs
);
377 match
= of_match_node(etraxfs_gpio_of_table
, dev
->of_node
);
383 chips
= devm_kzalloc(dev
, sizeof(*chips
) * info
->num_ports
, GFP_KERNEL
);
387 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
391 block
= devm_kzalloc(dev
, sizeof(*block
), GFP_KERNEL
);
395 raw_spin_lock_init(&block
->lock
);
400 writel(0, block
->regs
+ info
->rw_intr_mask
);
401 writel(0, block
->regs
+ info
->rw_intr_cfg
);
402 if (info
->rw_intr_pins
) {
404 writel(0, block
->regs
+ info
->rw_intr_pins
);
407 ret
= devm_request_irq(dev
, irq
->start
, etraxfs_gpio_interrupt
,
408 IRQF_SHARED
, dev_name(dev
), block
);
410 dev_err(dev
, "Unable to request irq %d\n", ret
);
414 for (i
= 0; i
< info
->num_ports
; i
++) {
415 struct etraxfs_gpio_chip
*chip
= &chips
[i
];
416 struct gpio_chip
*gc
= &chip
->gc
;
417 const struct etraxfs_gpio_port
*port
= &info
->ports
[i
];
418 unsigned long flags
= BGPIOF_READ_OUTPUT_REG_SET
;
419 void __iomem
*dat
= regs
+ port
->din
;
420 void __iomem
*set
= regs
+ port
->dout
;
421 void __iomem
*dirout
= regs
+ port
->oe
;
427 flags
= BGPIOF_NO_OUTPUT
;
430 ret
= bgpio_init(gc
, dev
, 4,
431 dat
, set
, NULL
, dirout
, NULL
,
434 dev_err(dev
, "Unable to init port %s\n",
439 gc
->ngpio
= port
->ngpio
;
440 gc
->label
= port
->label
;
442 gc
->of_node
= dev
->of_node
;
443 gc
->of_gpio_n_cells
= 3;
444 gc
->of_xlate
= etraxfs_gpio_of_xlate
;
446 ret
= gpiochip_add_data(gc
, chip
);
448 dev_err(dev
, "Unable to register port %s\n",
453 if (i
> 0 && !allportsirq
)
456 ret
= gpiochip_irqchip_add(gc
, &etraxfs_gpio_irq_chip
, 0,
457 handle_level_irq
, IRQ_TYPE_NONE
);
459 dev_err(dev
, "Unable to add irqchip to port %s\n",
467 static struct platform_driver etraxfs_gpio_driver
= {
469 .name
= "etraxfs-gpio",
470 .of_match_table
= of_match_ptr(etraxfs_gpio_of_table
),
472 .probe
= etraxfs_gpio_probe
,
475 builtin_platform_driver(etraxfs_gpio_driver
);