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 <asm/div64.h>
13 #include <linux/clk.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/hashtable.h>
16 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/platform_device.h>
22 #include <linux/spinlock.h>
23 #include <linux/string.h>
25 struct aspeed_bank_props
{
31 struct aspeed_gpio_config
{
32 unsigned int nr_gpios
;
33 const struct aspeed_bank_props
*props
;
37 * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled
38 * @timer_users: Tracks the number of users for each timer
40 * The @timer_users has four elements but the first element is unused. This is
41 * to simplify accounting and indexing, as a zero value in @offset_timer
42 * represents disabled debouncing for the GPIO. Any other value for an element
43 * of @offset_timer is used as an index into @timer_users. This behaviour of
44 * the zero value aligns with the behaviour of zero built from the timer
45 * configuration registers (i.e. debouncing is disabled).
48 struct gpio_chip chip
;
52 const struct aspeed_gpio_config
*config
;
55 unsigned int timer_users
[4];
59 struct aspeed_gpio_bank
{
62 uint16_t debounce_regs
;
63 uint16_t tolerance_regs
;
64 const char names
[4][3];
67 static const int debounce_timers
[4] = { 0x00, 0x50, 0x54, 0x58 };
69 static const struct aspeed_gpio_bank aspeed_gpio_banks
[] = {
73 .debounce_regs
= 0x0040,
74 .tolerance_regs
= 0x001c,
75 .names
= { "A", "B", "C", "D" },
80 .debounce_regs
= 0x0048,
81 .tolerance_regs
= 0x003c,
82 .names
= { "E", "F", "G", "H" },
87 .debounce_regs
= 0x00b0,
88 .tolerance_regs
= 0x00ac,
89 .names
= { "I", "J", "K", "L" },
94 .debounce_regs
= 0x0100,
95 .tolerance_regs
= 0x00fc,
96 .names
= { "M", "N", "O", "P" },
101 .debounce_regs
= 0x0130,
102 .tolerance_regs
= 0x012c,
103 .names
= { "Q", "R", "S", "T" },
108 .debounce_regs
= 0x0160,
109 .tolerance_regs
= 0x015c,
110 .names
= { "U", "V", "W", "X" },
115 .debounce_regs
= 0x0190,
116 .tolerance_regs
= 0x018c,
117 .names
= { "Y", "Z", "AA", "AB" },
122 .debounce_regs
= 0x01c0,
123 .tolerance_regs
= 0x01bc,
124 .names
= { "AC", "", "", "" },
128 #define GPIO_BANK(x) ((x) >> 5)
129 #define GPIO_OFFSET(x) ((x) & 0x1f)
130 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
132 #define GPIO_DATA 0x00
133 #define GPIO_DIR 0x04
135 #define GPIO_IRQ_ENABLE 0x00
136 #define GPIO_IRQ_TYPE0 0x04
137 #define GPIO_IRQ_TYPE1 0x08
138 #define GPIO_IRQ_TYPE2 0x0c
139 #define GPIO_IRQ_STATUS 0x10
141 #define GPIO_DEBOUNCE_SEL1 0x00
142 #define GPIO_DEBOUNCE_SEL2 0x04
144 #define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
145 #define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
146 #define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
148 static const struct aspeed_gpio_bank
*to_bank(unsigned int offset
)
150 unsigned int bank
= GPIO_BANK(offset
);
152 WARN_ON(bank
>= ARRAY_SIZE(aspeed_gpio_banks
));
153 return &aspeed_gpio_banks
[bank
];
156 static inline bool is_bank_props_sentinel(const struct aspeed_bank_props
*props
)
158 return !(props
->input
|| props
->output
);
161 static inline const struct aspeed_bank_props
*find_bank_props(
162 struct aspeed_gpio
*gpio
, unsigned int offset
)
164 const struct aspeed_bank_props
*props
= gpio
->config
->props
;
166 while (!is_bank_props_sentinel(props
)) {
167 if (props
->bank
== GPIO_BANK(offset
))
175 static inline bool have_gpio(struct aspeed_gpio
*gpio
, unsigned int offset
)
177 const struct aspeed_bank_props
*props
= find_bank_props(gpio
, offset
);
178 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
179 unsigned int group
= GPIO_OFFSET(offset
) / 8;
181 return bank
->names
[group
][0] != '\0' &&
182 (!props
|| ((props
->input
| props
->output
) & GPIO_BIT(offset
)));
185 static inline bool have_input(struct aspeed_gpio
*gpio
, unsigned int offset
)
187 const struct aspeed_bank_props
*props
= find_bank_props(gpio
, offset
);
189 return !props
|| (props
->input
& GPIO_BIT(offset
));
192 #define have_irq(g, o) have_input((g), (o))
193 #define have_debounce(g, o) have_input((g), (o))
195 static inline bool have_output(struct aspeed_gpio
*gpio
, unsigned int offset
)
197 const struct aspeed_bank_props
*props
= find_bank_props(gpio
, offset
);
199 return !props
|| (props
->output
& GPIO_BIT(offset
));
202 static void __iomem
*bank_val_reg(struct aspeed_gpio
*gpio
,
203 const struct aspeed_gpio_bank
*bank
,
206 return gpio
->base
+ bank
->val_regs
+ reg
;
209 static void __iomem
*bank_irq_reg(struct aspeed_gpio
*gpio
,
210 const struct aspeed_gpio_bank
*bank
,
213 return gpio
->base
+ bank
->irq_regs
+ reg
;
216 static int aspeed_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
218 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
219 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
221 return !!(ioread32(bank_val_reg(gpio
, bank
, GPIO_DATA
))
225 static void __aspeed_gpio_set(struct gpio_chip
*gc
, unsigned int offset
,
228 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
229 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
233 addr
= bank_val_reg(gpio
, bank
, GPIO_DATA
);
234 reg
= ioread32(addr
);
237 reg
|= GPIO_BIT(offset
);
239 reg
&= ~GPIO_BIT(offset
);
241 iowrite32(reg
, addr
);
244 static void aspeed_gpio_set(struct gpio_chip
*gc
, unsigned int offset
,
247 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
250 spin_lock_irqsave(&gpio
->lock
, flags
);
252 __aspeed_gpio_set(gc
, offset
, val
);
254 spin_unlock_irqrestore(&gpio
->lock
, flags
);
257 static int aspeed_gpio_dir_in(struct gpio_chip
*gc
, unsigned int offset
)
259 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
260 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
264 if (!have_input(gpio
, offset
))
267 spin_lock_irqsave(&gpio
->lock
, flags
);
269 reg
= ioread32(bank_val_reg(gpio
, bank
, GPIO_DIR
));
270 iowrite32(reg
& ~GPIO_BIT(offset
), bank_val_reg(gpio
, bank
, GPIO_DIR
));
272 spin_unlock_irqrestore(&gpio
->lock
, flags
);
277 static int aspeed_gpio_dir_out(struct gpio_chip
*gc
,
278 unsigned int offset
, int val
)
280 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
281 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
285 if (!have_output(gpio
, offset
))
288 spin_lock_irqsave(&gpio
->lock
, flags
);
290 reg
= ioread32(bank_val_reg(gpio
, bank
, GPIO_DIR
));
291 iowrite32(reg
| GPIO_BIT(offset
), bank_val_reg(gpio
, bank
, GPIO_DIR
));
293 __aspeed_gpio_set(gc
, offset
, val
);
295 spin_unlock_irqrestore(&gpio
->lock
, flags
);
300 static int aspeed_gpio_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
302 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
303 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
307 if (!have_input(gpio
, offset
))
310 if (!have_output(gpio
, offset
))
313 spin_lock_irqsave(&gpio
->lock
, flags
);
315 val
= ioread32(bank_val_reg(gpio
, bank
, GPIO_DIR
)) & GPIO_BIT(offset
);
317 spin_unlock_irqrestore(&gpio
->lock
, flags
);
323 static inline int irqd_to_aspeed_gpio_data(struct irq_data
*d
,
324 struct aspeed_gpio
**gpio
,
325 const struct aspeed_gpio_bank
**bank
,
329 struct aspeed_gpio
*internal
;
331 offset
= irqd_to_hwirq(d
);
333 internal
= irq_data_get_irq_chip_data(d
);
335 /* This might be a bit of a questionable place to check */
336 if (!have_irq(internal
, offset
))
340 *bank
= to_bank(offset
);
341 *bit
= GPIO_BIT(offset
);
346 static void aspeed_gpio_irq_ack(struct irq_data
*d
)
348 const struct aspeed_gpio_bank
*bank
;
349 struct aspeed_gpio
*gpio
;
351 void __iomem
*status_addr
;
355 rc
= irqd_to_aspeed_gpio_data(d
, &gpio
, &bank
, &bit
);
359 status_addr
= bank_irq_reg(gpio
, bank
, GPIO_IRQ_STATUS
);
361 spin_lock_irqsave(&gpio
->lock
, flags
);
362 iowrite32(bit
, status_addr
);
363 spin_unlock_irqrestore(&gpio
->lock
, flags
);
366 static void aspeed_gpio_irq_set_mask(struct irq_data
*d
, bool set
)
368 const struct aspeed_gpio_bank
*bank
;
369 struct aspeed_gpio
*gpio
;
375 rc
= irqd_to_aspeed_gpio_data(d
, &gpio
, &bank
, &bit
);
379 addr
= bank_irq_reg(gpio
, bank
, GPIO_IRQ_ENABLE
);
381 spin_lock_irqsave(&gpio
->lock
, flags
);
383 reg
= ioread32(addr
);
388 iowrite32(reg
, addr
);
390 spin_unlock_irqrestore(&gpio
->lock
, flags
);
393 static void aspeed_gpio_irq_mask(struct irq_data
*d
)
395 aspeed_gpio_irq_set_mask(d
, false);
398 static void aspeed_gpio_irq_unmask(struct irq_data
*d
)
400 aspeed_gpio_irq_set_mask(d
, true);
403 static int aspeed_gpio_set_type(struct irq_data
*d
, unsigned int type
)
409 const struct aspeed_gpio_bank
*bank
;
410 irq_flow_handler_t handler
;
411 struct aspeed_gpio
*gpio
;
416 rc
= irqd_to_aspeed_gpio_data(d
, &gpio
, &bank
, &bit
);
420 switch (type
& IRQ_TYPE_SENSE_MASK
) {
421 case IRQ_TYPE_EDGE_BOTH
:
424 case IRQ_TYPE_EDGE_RISING
:
427 case IRQ_TYPE_EDGE_FALLING
:
428 handler
= handle_edge_irq
;
430 case IRQ_TYPE_LEVEL_HIGH
:
433 case IRQ_TYPE_LEVEL_LOW
:
435 handler
= handle_level_irq
;
441 spin_lock_irqsave(&gpio
->lock
, flags
);
443 addr
= bank_irq_reg(gpio
, bank
, GPIO_IRQ_TYPE0
);
444 reg
= ioread32(addr
);
445 reg
= (reg
& ~bit
) | type0
;
446 iowrite32(reg
, addr
);
448 addr
= bank_irq_reg(gpio
, bank
, GPIO_IRQ_TYPE1
);
449 reg
= ioread32(addr
);
450 reg
= (reg
& ~bit
) | type1
;
451 iowrite32(reg
, addr
);
453 addr
= bank_irq_reg(gpio
, bank
, GPIO_IRQ_TYPE2
);
454 reg
= ioread32(addr
);
455 reg
= (reg
& ~bit
) | type2
;
456 iowrite32(reg
, addr
);
458 spin_unlock_irqrestore(&gpio
->lock
, flags
);
460 irq_set_handler_locked(d
, handler
);
465 static void aspeed_gpio_irq_handler(struct irq_desc
*desc
)
467 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
468 struct irq_chip
*ic
= irq_desc_get_chip(desc
);
469 struct aspeed_gpio
*data
= gpiochip_get_data(gc
);
470 unsigned int i
, p
, girq
;
473 chained_irq_enter(ic
, desc
);
475 for (i
= 0; i
< ARRAY_SIZE(aspeed_gpio_banks
); i
++) {
476 const struct aspeed_gpio_bank
*bank
= &aspeed_gpio_banks
[i
];
478 reg
= ioread32(bank_irq_reg(data
, bank
, GPIO_IRQ_STATUS
));
480 for_each_set_bit(p
, ®
, 32) {
481 girq
= irq_find_mapping(gc
->irq
.domain
, i
* 32 + p
);
482 generic_handle_irq(girq
);
487 chained_irq_exit(ic
, desc
);
490 static struct irq_chip aspeed_gpio_irqchip
= {
491 .name
= "aspeed-gpio",
492 .irq_ack
= aspeed_gpio_irq_ack
,
493 .irq_mask
= aspeed_gpio_irq_mask
,
494 .irq_unmask
= aspeed_gpio_irq_unmask
,
495 .irq_set_type
= aspeed_gpio_set_type
,
498 static void set_irq_valid_mask(struct aspeed_gpio
*gpio
)
500 const struct aspeed_bank_props
*props
= gpio
->config
->props
;
502 while (!is_bank_props_sentinel(props
)) {
504 const unsigned long int input
= props
->input
;
506 /* Pretty crummy approach, but similar to GPIO core */
507 for_each_clear_bit(offset
, &input
, 32) {
508 unsigned int i
= props
->bank
* 32 + offset
;
510 if (i
>= gpio
->config
->nr_gpios
)
513 clear_bit(i
, gpio
->chip
.irq
.valid_mask
);
520 static int aspeed_gpio_setup_irqs(struct aspeed_gpio
*gpio
,
521 struct platform_device
*pdev
)
525 rc
= platform_get_irq(pdev
, 0);
531 set_irq_valid_mask(gpio
);
533 rc
= gpiochip_irqchip_add(&gpio
->chip
, &aspeed_gpio_irqchip
,
534 0, handle_bad_irq
, IRQ_TYPE_NONE
);
536 dev_info(&pdev
->dev
, "Could not add irqchip\n");
540 gpiochip_set_chained_irqchip(&gpio
->chip
, &aspeed_gpio_irqchip
,
541 gpio
->irq
, aspeed_gpio_irq_handler
);
546 static int aspeed_gpio_reset_tolerance(struct gpio_chip
*chip
,
547 unsigned int offset
, bool enable
)
549 struct aspeed_gpio
*gpio
= gpiochip_get_data(chip
);
550 const struct aspeed_gpio_bank
*bank
;
554 bank
= to_bank(offset
);
556 spin_lock_irqsave(&gpio
->lock
, flags
);
557 val
= readl(gpio
->base
+ bank
->tolerance_regs
);
560 val
|= GPIO_BIT(offset
);
562 val
&= ~GPIO_BIT(offset
);
564 writel(val
, gpio
->base
+ bank
->tolerance_regs
);
565 spin_unlock_irqrestore(&gpio
->lock
, flags
);
570 static int aspeed_gpio_request(struct gpio_chip
*chip
, unsigned int offset
)
572 if (!have_gpio(gpiochip_get_data(chip
), offset
))
575 return pinctrl_gpio_request(chip
->base
+ offset
);
578 static void aspeed_gpio_free(struct gpio_chip
*chip
, unsigned int offset
)
580 pinctrl_gpio_free(chip
->base
+ offset
);
583 static inline void __iomem
*bank_debounce_reg(struct aspeed_gpio
*gpio
,
584 const struct aspeed_gpio_bank
*bank
,
587 return gpio
->base
+ bank
->debounce_regs
+ reg
;
590 static int usecs_to_cycles(struct aspeed_gpio
*gpio
, unsigned long usecs
,
597 rate
= clk_get_rate(gpio
->clk
);
602 r
= do_div(n
, 1000000);
607 /* At least as long as the requested time */
613 /* Call under gpio->lock */
614 static int register_allocated_timer(struct aspeed_gpio
*gpio
,
615 unsigned int offset
, unsigned int timer
)
617 if (WARN(gpio
->offset_timer
[offset
] != 0,
618 "Offset %d already allocated timer %d\n",
619 offset
, gpio
->offset_timer
[offset
]))
622 if (WARN(gpio
->timer_users
[timer
] == UINT_MAX
,
623 "Timer user count would overflow\n"))
626 gpio
->offset_timer
[offset
] = timer
;
627 gpio
->timer_users
[timer
]++;
632 /* Call under gpio->lock */
633 static int unregister_allocated_timer(struct aspeed_gpio
*gpio
,
636 if (WARN(gpio
->offset_timer
[offset
] == 0,
637 "No timer allocated to offset %d\n", offset
))
640 if (WARN(gpio
->timer_users
[gpio
->offset_timer
[offset
]] == 0,
641 "No users recorded for timer %d\n",
642 gpio
->offset_timer
[offset
]))
645 gpio
->timer_users
[gpio
->offset_timer
[offset
]]--;
646 gpio
->offset_timer
[offset
] = 0;
651 /* Call under gpio->lock */
652 static inline bool timer_allocation_registered(struct aspeed_gpio
*gpio
,
655 return gpio
->offset_timer
[offset
] > 0;
658 /* Call under gpio->lock */
659 static void configure_timer(struct aspeed_gpio
*gpio
, unsigned int offset
,
662 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
663 const u32 mask
= GPIO_BIT(offset
);
667 addr
= bank_debounce_reg(gpio
, bank
, GPIO_DEBOUNCE_SEL1
);
668 val
= ioread32(addr
);
669 iowrite32((val
& ~mask
) | GPIO_SET_DEBOUNCE1(timer
, offset
), addr
);
671 addr
= bank_debounce_reg(gpio
, bank
, GPIO_DEBOUNCE_SEL2
);
672 val
= ioread32(addr
);
673 iowrite32((val
& ~mask
) | GPIO_SET_DEBOUNCE2(timer
, offset
), addr
);
676 static int enable_debounce(struct gpio_chip
*chip
, unsigned int offset
,
679 struct aspeed_gpio
*gpio
= gpiochip_get_data(chip
);
680 u32 requested_cycles
;
688 rc
= usecs_to_cycles(gpio
, usecs
, &requested_cycles
);
690 dev_warn(chip
->parent
, "Failed to convert %luus to cycles at %luHz: %d\n",
691 usecs
, clk_get_rate(gpio
->clk
), rc
);
695 spin_lock_irqsave(&gpio
->lock
, flags
);
697 if (timer_allocation_registered(gpio
, offset
)) {
698 rc
= unregister_allocated_timer(gpio
, offset
);
703 /* Try to find a timer already configured for the debounce period */
704 for (i
= 1; i
< ARRAY_SIZE(debounce_timers
); i
++) {
707 cycles
= ioread32(gpio
->base
+ debounce_timers
[i
]);
708 if (requested_cycles
== cycles
)
712 if (i
== ARRAY_SIZE(debounce_timers
)) {
716 * As there are no timers configured for the requested debounce
717 * period, find an unused timer instead
719 for (j
= 1; j
< ARRAY_SIZE(gpio
->timer_users
); j
++) {
720 if (gpio
->timer_users
[j
] == 0)
724 if (j
== ARRAY_SIZE(gpio
->timer_users
)) {
725 dev_warn(chip
->parent
,
726 "Debounce timers exhausted, cannot debounce for period %luus\n",
732 * We already adjusted the accounting to remove @offset
733 * as a user of its previous timer, so also configure
734 * the hardware so @offset has timers disabled for
737 configure_timer(gpio
, offset
, 0);
743 iowrite32(requested_cycles
, gpio
->base
+ debounce_timers
[i
]);
746 if (WARN(i
== 0, "Cannot register index of disabled timer\n")) {
751 register_allocated_timer(gpio
, offset
, i
);
752 configure_timer(gpio
, offset
, i
);
755 spin_unlock_irqrestore(&gpio
->lock
, flags
);
760 static int disable_debounce(struct gpio_chip
*chip
, unsigned int offset
)
762 struct aspeed_gpio
*gpio
= gpiochip_get_data(chip
);
766 spin_lock_irqsave(&gpio
->lock
, flags
);
768 rc
= unregister_allocated_timer(gpio
, offset
);
770 configure_timer(gpio
, offset
, 0);
772 spin_unlock_irqrestore(&gpio
->lock
, flags
);
777 static int set_debounce(struct gpio_chip
*chip
, unsigned int offset
,
780 struct aspeed_gpio
*gpio
= gpiochip_get_data(chip
);
782 if (!have_debounce(gpio
, offset
))
786 return enable_debounce(chip
, offset
, usecs
);
788 return disable_debounce(chip
, offset
);
791 static int aspeed_gpio_set_config(struct gpio_chip
*chip
, unsigned int offset
,
792 unsigned long config
)
794 unsigned long param
= pinconf_to_config_param(config
);
795 u32 arg
= pinconf_to_config_argument(config
);
797 if (param
== PIN_CONFIG_INPUT_DEBOUNCE
)
798 return set_debounce(chip
, offset
, arg
);
799 else if (param
== PIN_CONFIG_BIAS_DISABLE
||
800 param
== PIN_CONFIG_BIAS_PULL_DOWN
||
801 param
== PIN_CONFIG_DRIVE_STRENGTH
)
802 return pinctrl_gpio_set_config(offset
, config
);
803 else if (param
== PIN_CONFIG_DRIVE_OPEN_DRAIN
||
804 param
== PIN_CONFIG_DRIVE_OPEN_SOURCE
)
805 /* Return -ENOTSUPP to trigger emulation, as per datasheet */
807 else if (param
== PIN_CONFIG_PERSIST_STATE
)
808 return aspeed_gpio_reset_tolerance(chip
, offset
, arg
);
814 * Any banks not specified in a struct aspeed_bank_props array are assumed to
815 * have the properties:
817 * { .input = 0xffffffff, .output = 0xffffffff }
820 static const struct aspeed_bank_props ast2400_bank_props
[] = {
822 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
823 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
827 static const struct aspeed_gpio_config ast2400_config
=
828 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
829 { .nr_gpios
= 220, .props
= ast2400_bank_props
, };
831 static const struct aspeed_bank_props ast2500_bank_props
[] = {
833 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
834 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
835 { 7, 0x000000ff, 0x000000ff }, /* AC */
839 static const struct aspeed_gpio_config ast2500_config
=
840 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
841 { .nr_gpios
= 232, .props
= ast2500_bank_props
, };
843 static const struct of_device_id aspeed_gpio_of_table
[] = {
844 { .compatible
= "aspeed,ast2400-gpio", .data
= &ast2400_config
, },
845 { .compatible
= "aspeed,ast2500-gpio", .data
= &ast2500_config
, },
848 MODULE_DEVICE_TABLE(of
, aspeed_gpio_of_table
);
850 static int __init
aspeed_gpio_probe(struct platform_device
*pdev
)
852 const struct of_device_id
*gpio_id
;
853 struct aspeed_gpio
*gpio
;
854 struct resource
*res
;
857 gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*gpio
), GFP_KERNEL
);
861 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
862 gpio
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
863 if (IS_ERR(gpio
->base
))
864 return PTR_ERR(gpio
->base
);
866 spin_lock_init(&gpio
->lock
);
868 gpio_id
= of_match_node(aspeed_gpio_of_table
, pdev
->dev
.of_node
);
872 gpio
->clk
= of_clk_get(pdev
->dev
.of_node
, 0);
873 if (IS_ERR(gpio
->clk
)) {
875 "Failed to get clock from devicetree, debouncing disabled\n");
879 gpio
->config
= gpio_id
->data
;
881 gpio
->chip
.parent
= &pdev
->dev
;
882 gpio
->chip
.ngpio
= gpio
->config
->nr_gpios
;
883 gpio
->chip
.parent
= &pdev
->dev
;
884 gpio
->chip
.direction_input
= aspeed_gpio_dir_in
;
885 gpio
->chip
.direction_output
= aspeed_gpio_dir_out
;
886 gpio
->chip
.get_direction
= aspeed_gpio_get_direction
;
887 gpio
->chip
.request
= aspeed_gpio_request
;
888 gpio
->chip
.free
= aspeed_gpio_free
;
889 gpio
->chip
.get
= aspeed_gpio_get
;
890 gpio
->chip
.set
= aspeed_gpio_set
;
891 gpio
->chip
.set_config
= aspeed_gpio_set_config
;
892 gpio
->chip
.label
= dev_name(&pdev
->dev
);
893 gpio
->chip
.base
= -1;
894 gpio
->chip
.irq
.need_valid_mask
= true;
896 rc
= devm_gpiochip_add_data(&pdev
->dev
, &gpio
->chip
, gpio
);
901 devm_kzalloc(&pdev
->dev
, gpio
->chip
.ngpio
, GFP_KERNEL
);
903 return aspeed_gpio_setup_irqs(gpio
, pdev
);
906 static struct platform_driver aspeed_gpio_driver
= {
908 .name
= KBUILD_MODNAME
,
909 .of_match_table
= aspeed_gpio_of_table
,
913 module_platform_driver_probe(aspeed_gpio_driver
, aspeed_gpio_probe
);
915 MODULE_DESCRIPTION("Aspeed GPIO Driver");
916 MODULE_LICENSE("GPL");