2 * Copyright 2015 IBM Corp.
4 * Joel Stanley <joel@jms.id.au>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
16 #include <linux/spinlock.h>
17 #include <linux/platform_device.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/pinctrl/consumer.h>
21 struct aspeed_bank_props
{
27 struct aspeed_gpio_config
{
28 unsigned int nr_gpios
;
29 const struct aspeed_bank_props
*props
;
33 struct gpio_chip chip
;
37 const struct aspeed_gpio_config
*config
;
40 struct aspeed_gpio_bank
{
43 const char names
[4][3];
46 static const struct aspeed_gpio_bank aspeed_gpio_banks
[] = {
50 .names
= { "A", "B", "C", "D" },
55 .names
= { "E", "F", "G", "H" },
60 .names
= { "I", "J", "K", "L" },
65 .names
= { "M", "N", "O", "P" },
70 .names
= { "Q", "R", "S", "T" },
75 .names
= { "U", "V", "W", "X" },
80 .names
= { "Y", "Z", "AA", "AB" },
85 .names
= { "AC", "", "", "" },
89 #define GPIO_BANK(x) ((x) >> 5)
90 #define GPIO_OFFSET(x) ((x) & 0x1f)
91 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
93 #define GPIO_DATA 0x00
96 #define GPIO_IRQ_ENABLE 0x00
97 #define GPIO_IRQ_TYPE0 0x04
98 #define GPIO_IRQ_TYPE1 0x08
99 #define GPIO_IRQ_TYPE2 0x0c
100 #define GPIO_IRQ_STATUS 0x10
102 static const struct aspeed_gpio_bank
*to_bank(unsigned int offset
)
104 unsigned int bank
= GPIO_BANK(offset
);
106 WARN_ON(bank
> ARRAY_SIZE(aspeed_gpio_banks
));
107 return &aspeed_gpio_banks
[bank
];
110 static inline bool is_bank_props_sentinel(const struct aspeed_bank_props
*props
)
112 return !(props
->input
|| props
->output
);
115 static inline const struct aspeed_bank_props
*find_bank_props(
116 struct aspeed_gpio
*gpio
, unsigned int offset
)
118 const struct aspeed_bank_props
*props
= gpio
->config
->props
;
120 while (!is_bank_props_sentinel(props
)) {
121 if (props
->bank
== GPIO_BANK(offset
))
129 static inline bool have_gpio(struct aspeed_gpio
*gpio
, unsigned int offset
)
131 const struct aspeed_bank_props
*props
= find_bank_props(gpio
, offset
);
132 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
133 unsigned int group
= GPIO_OFFSET(offset
) / 8;
135 return bank
->names
[group
][0] != '\0' &&
136 (!props
|| ((props
->input
| props
->output
) & GPIO_BIT(offset
)));
139 static inline bool have_input(struct aspeed_gpio
*gpio
, unsigned int offset
)
141 const struct aspeed_bank_props
*props
= find_bank_props(gpio
, offset
);
143 return !props
|| (props
->input
& GPIO_BIT(offset
));
146 #define have_irq(g, o) have_input((g), (o))
148 static inline bool have_output(struct aspeed_gpio
*gpio
, unsigned int offset
)
150 const struct aspeed_bank_props
*props
= find_bank_props(gpio
, offset
);
152 return !props
|| (props
->output
& GPIO_BIT(offset
));
155 static void __iomem
*bank_val_reg(struct aspeed_gpio
*gpio
,
156 const struct aspeed_gpio_bank
*bank
,
159 return gpio
->base
+ bank
->val_regs
+ reg
;
162 static void __iomem
*bank_irq_reg(struct aspeed_gpio
*gpio
,
163 const struct aspeed_gpio_bank
*bank
,
166 return gpio
->base
+ bank
->irq_regs
+ reg
;
169 static int aspeed_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
171 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
172 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
174 return !!(ioread32(bank_val_reg(gpio
, bank
, GPIO_DATA
))
178 static void __aspeed_gpio_set(struct gpio_chip
*gc
, unsigned int offset
,
181 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
182 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
186 addr
= bank_val_reg(gpio
, bank
, GPIO_DATA
);
187 reg
= ioread32(addr
);
190 reg
|= GPIO_BIT(offset
);
192 reg
&= ~GPIO_BIT(offset
);
194 iowrite32(reg
, addr
);
197 static void aspeed_gpio_set(struct gpio_chip
*gc
, unsigned int offset
,
200 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
203 spin_lock_irqsave(&gpio
->lock
, flags
);
205 __aspeed_gpio_set(gc
, offset
, val
);
207 spin_unlock_irqrestore(&gpio
->lock
, flags
);
210 static int aspeed_gpio_dir_in(struct gpio_chip
*gc
, unsigned int offset
)
212 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
213 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
217 if (!have_input(gpio
, offset
))
220 spin_lock_irqsave(&gpio
->lock
, flags
);
222 reg
= ioread32(bank_val_reg(gpio
, bank
, GPIO_DIR
));
223 iowrite32(reg
& ~GPIO_BIT(offset
), bank_val_reg(gpio
, bank
, GPIO_DIR
));
225 spin_unlock_irqrestore(&gpio
->lock
, flags
);
230 static int aspeed_gpio_dir_out(struct gpio_chip
*gc
,
231 unsigned int offset
, int val
)
233 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
234 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
238 if (!have_output(gpio
, offset
))
241 spin_lock_irqsave(&gpio
->lock
, flags
);
243 reg
= ioread32(bank_val_reg(gpio
, bank
, GPIO_DIR
));
244 iowrite32(reg
| GPIO_BIT(offset
), bank_val_reg(gpio
, bank
, GPIO_DIR
));
246 __aspeed_gpio_set(gc
, offset
, val
);
248 spin_unlock_irqrestore(&gpio
->lock
, flags
);
253 static int aspeed_gpio_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
255 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
256 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
260 if (!have_input(gpio
, offset
))
263 if (!have_output(gpio
, offset
))
266 spin_lock_irqsave(&gpio
->lock
, flags
);
268 val
= ioread32(bank_val_reg(gpio
, bank
, GPIO_DIR
)) & GPIO_BIT(offset
);
270 spin_unlock_irqrestore(&gpio
->lock
, flags
);
276 static inline int irqd_to_aspeed_gpio_data(struct irq_data
*d
,
277 struct aspeed_gpio
**gpio
,
278 const struct aspeed_gpio_bank
**bank
,
282 struct aspeed_gpio
*internal
;
284 offset
= irqd_to_hwirq(d
);
286 internal
= irq_data_get_irq_chip_data(d
);
288 /* This might be a bit of a questionable place to check */
289 if (!have_irq(internal
, offset
))
293 *bank
= to_bank(offset
);
294 *bit
= GPIO_BIT(offset
);
299 static void aspeed_gpio_irq_ack(struct irq_data
*d
)
301 const struct aspeed_gpio_bank
*bank
;
302 struct aspeed_gpio
*gpio
;
304 void __iomem
*status_addr
;
308 rc
= irqd_to_aspeed_gpio_data(d
, &gpio
, &bank
, &bit
);
312 status_addr
= bank_irq_reg(gpio
, bank
, GPIO_IRQ_STATUS
);
314 spin_lock_irqsave(&gpio
->lock
, flags
);
315 iowrite32(bit
, status_addr
);
316 spin_unlock_irqrestore(&gpio
->lock
, flags
);
319 static void aspeed_gpio_irq_set_mask(struct irq_data
*d
, bool set
)
321 const struct aspeed_gpio_bank
*bank
;
322 struct aspeed_gpio
*gpio
;
328 rc
= irqd_to_aspeed_gpio_data(d
, &gpio
, &bank
, &bit
);
332 addr
= bank_irq_reg(gpio
, bank
, GPIO_IRQ_ENABLE
);
334 spin_lock_irqsave(&gpio
->lock
, flags
);
336 reg
= ioread32(addr
);
341 iowrite32(reg
, addr
);
343 spin_unlock_irqrestore(&gpio
->lock
, flags
);
346 static void aspeed_gpio_irq_mask(struct irq_data
*d
)
348 aspeed_gpio_irq_set_mask(d
, false);
351 static void aspeed_gpio_irq_unmask(struct irq_data
*d
)
353 aspeed_gpio_irq_set_mask(d
, true);
356 static int aspeed_gpio_set_type(struct irq_data
*d
, unsigned int type
)
362 const struct aspeed_gpio_bank
*bank
;
363 irq_flow_handler_t handler
;
364 struct aspeed_gpio
*gpio
;
369 rc
= irqd_to_aspeed_gpio_data(d
, &gpio
, &bank
, &bit
);
373 switch (type
& IRQ_TYPE_SENSE_MASK
) {
374 case IRQ_TYPE_EDGE_BOTH
:
376 case IRQ_TYPE_EDGE_RISING
:
378 case IRQ_TYPE_EDGE_FALLING
:
379 handler
= handle_edge_irq
;
381 case IRQ_TYPE_LEVEL_HIGH
:
383 case IRQ_TYPE_LEVEL_LOW
:
385 handler
= handle_level_irq
;
391 spin_lock_irqsave(&gpio
->lock
, flags
);
393 addr
= bank_irq_reg(gpio
, bank
, GPIO_IRQ_TYPE0
);
394 reg
= ioread32(addr
);
395 reg
= (reg
& ~bit
) | type0
;
396 iowrite32(reg
, addr
);
398 addr
= bank_irq_reg(gpio
, bank
, GPIO_IRQ_TYPE1
);
399 reg
= ioread32(addr
);
400 reg
= (reg
& ~bit
) | type1
;
401 iowrite32(reg
, addr
);
403 addr
= bank_irq_reg(gpio
, bank
, GPIO_IRQ_TYPE2
);
404 reg
= ioread32(addr
);
405 reg
= (reg
& ~bit
) | type2
;
406 iowrite32(reg
, addr
);
408 spin_unlock_irqrestore(&gpio
->lock
, flags
);
410 irq_set_handler_locked(d
, handler
);
415 static void aspeed_gpio_irq_handler(struct irq_desc
*desc
)
417 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
418 struct irq_chip
*ic
= irq_desc_get_chip(desc
);
419 struct aspeed_gpio
*data
= gpiochip_get_data(gc
);
420 unsigned int i
, p
, girq
;
423 chained_irq_enter(ic
, desc
);
425 for (i
= 0; i
< ARRAY_SIZE(aspeed_gpio_banks
); i
++) {
426 const struct aspeed_gpio_bank
*bank
= &aspeed_gpio_banks
[i
];
428 reg
= ioread32(bank_irq_reg(data
, bank
, GPIO_IRQ_STATUS
));
430 for_each_set_bit(p
, ®
, 32) {
431 girq
= irq_find_mapping(gc
->irqdomain
, i
* 32 + p
);
432 generic_handle_irq(girq
);
437 chained_irq_exit(ic
, desc
);
440 static struct irq_chip aspeed_gpio_irqchip
= {
441 .name
= "aspeed-gpio",
442 .irq_ack
= aspeed_gpio_irq_ack
,
443 .irq_mask
= aspeed_gpio_irq_mask
,
444 .irq_unmask
= aspeed_gpio_irq_unmask
,
445 .irq_set_type
= aspeed_gpio_set_type
,
448 static void set_irq_valid_mask(struct aspeed_gpio
*gpio
)
450 const struct aspeed_bank_props
*props
= gpio
->config
->props
;
452 while (!is_bank_props_sentinel(props
)) {
454 const unsigned long int input
= props
->input
;
456 /* Pretty crummy approach, but similar to GPIO core */
457 for_each_clear_bit(offset
, &input
, 32) {
458 unsigned int i
= props
->bank
* 32 + offset
;
460 if (i
>= gpio
->config
->nr_gpios
)
463 clear_bit(i
, gpio
->chip
.irq_valid_mask
);
470 static int aspeed_gpio_setup_irqs(struct aspeed_gpio
*gpio
,
471 struct platform_device
*pdev
)
475 rc
= platform_get_irq(pdev
, 0);
481 set_irq_valid_mask(gpio
);
483 rc
= gpiochip_irqchip_add(&gpio
->chip
, &aspeed_gpio_irqchip
,
484 0, handle_bad_irq
, IRQ_TYPE_NONE
);
486 dev_info(&pdev
->dev
, "Could not add irqchip\n");
490 gpiochip_set_chained_irqchip(&gpio
->chip
, &aspeed_gpio_irqchip
,
491 gpio
->irq
, aspeed_gpio_irq_handler
);
496 static int aspeed_gpio_request(struct gpio_chip
*chip
, unsigned int offset
)
498 if (!have_gpio(gpiochip_get_data(chip
), offset
))
501 return pinctrl_request_gpio(chip
->base
+ offset
);
504 static void aspeed_gpio_free(struct gpio_chip
*chip
, unsigned int offset
)
506 pinctrl_free_gpio(chip
->base
+ offset
);
510 * Any banks not specified in a struct aspeed_bank_props array are assumed to
511 * have the properties:
513 * { .input = 0xffffffff, .output = 0xffffffff }
516 static const struct aspeed_bank_props ast2400_bank_props
[] = {
518 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
519 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
523 static const struct aspeed_gpio_config ast2400_config
=
524 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
525 { .nr_gpios
= 220, .props
= ast2400_bank_props
, };
527 static const struct aspeed_bank_props ast2500_bank_props
[] = {
529 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
530 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
531 { 7, 0x000000ff, 0x000000ff }, /* AC */
535 static const struct aspeed_gpio_config ast2500_config
=
536 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
537 { .nr_gpios
= 232, .props
= ast2500_bank_props
, };
539 static const struct of_device_id aspeed_gpio_of_table
[] = {
540 { .compatible
= "aspeed,ast2400-gpio", .data
= &ast2400_config
, },
541 { .compatible
= "aspeed,ast2500-gpio", .data
= &ast2500_config
, },
544 MODULE_DEVICE_TABLE(of
, aspeed_gpio_of_table
);
546 static int __init
aspeed_gpio_probe(struct platform_device
*pdev
)
548 const struct of_device_id
*gpio_id
;
549 struct aspeed_gpio
*gpio
;
550 struct resource
*res
;
553 gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*gpio
), GFP_KERNEL
);
557 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
558 gpio
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
559 if (IS_ERR(gpio
->base
))
560 return PTR_ERR(gpio
->base
);
562 spin_lock_init(&gpio
->lock
);
564 gpio_id
= of_match_node(aspeed_gpio_of_table
, pdev
->dev
.of_node
);
568 gpio
->config
= gpio_id
->data
;
570 gpio
->chip
.ngpio
= gpio
->config
->nr_gpios
;
571 gpio
->chip
.parent
= &pdev
->dev
;
572 gpio
->chip
.direction_input
= aspeed_gpio_dir_in
;
573 gpio
->chip
.direction_output
= aspeed_gpio_dir_out
;
574 gpio
->chip
.get_direction
= aspeed_gpio_get_direction
;
575 gpio
->chip
.request
= aspeed_gpio_request
;
576 gpio
->chip
.free
= aspeed_gpio_free
;
577 gpio
->chip
.get
= aspeed_gpio_get
;
578 gpio
->chip
.set
= aspeed_gpio_set
;
579 gpio
->chip
.label
= dev_name(&pdev
->dev
);
580 gpio
->chip
.base
= -1;
581 gpio
->chip
.irq_need_valid_mask
= true;
583 rc
= devm_gpiochip_add_data(&pdev
->dev
, &gpio
->chip
, gpio
);
587 return aspeed_gpio_setup_irqs(gpio
, pdev
);
590 static struct platform_driver aspeed_gpio_driver
= {
592 .name
= KBUILD_MODNAME
,
593 .of_match_table
= aspeed_gpio_of_table
,
597 module_platform_driver_probe(aspeed_gpio_driver
, aspeed_gpio_probe
);
599 MODULE_DESCRIPTION("Aspeed GPIO Driver");
600 MODULE_LICENSE("GPL");