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/gpio/aspeed.h>
16 #include <linux/hashtable.h>
17 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/platform_device.h>
23 #include <linux/spinlock.h>
24 #include <linux/string.h>
27 * These two headers aren't meant to be used by GPIO drivers. We need
28 * them in order to access gpio_chip_hwgpio() which we need to implement
29 * the aspeed specific API which allows the coprocessor to request
30 * access to some GPIOs and to arbitrate between coprocessor and ARM.
32 #include <linux/gpio/consumer.h>
35 struct aspeed_bank_props
{
41 struct aspeed_gpio_config
{
42 unsigned int nr_gpios
;
43 const struct aspeed_bank_props
*props
;
47 * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled
48 * @timer_users: Tracks the number of users for each timer
50 * The @timer_users has four elements but the first element is unused. This is
51 * to simplify accounting and indexing, as a zero value in @offset_timer
52 * represents disabled debouncing for the GPIO. Any other value for an element
53 * of @offset_timer is used as an index into @timer_users. This behaviour of
54 * the zero value aligns with the behaviour of zero built from the timer
55 * configuration registers (i.e. debouncing is disabled).
58 struct gpio_chip chip
;
62 const struct aspeed_gpio_config
*config
;
65 unsigned int timer_users
[4];
72 struct aspeed_gpio_bank
{
73 uint16_t val_regs
; /* +0: Rd: read input value, Wr: set write latch
74 * +4: Rd/Wr: Direction (0=in, 1=out)
76 uint16_t rdata_reg
; /* Rd: read write latch, Wr: <none> */
78 uint16_t debounce_regs
;
79 uint16_t tolerance_regs
;
81 const char names
[4][3];
85 * Note: The "value" register returns the input value sampled on the
86 * line even when the GPIO is configured as an output. Since
87 * that input goes through synchronizers, writing, then reading
88 * back may not return the written value right away.
90 * The "rdata" register returns the content of the write latch
91 * and thus can be used to read back what was last written
95 static const int debounce_timers
[4] = { 0x00, 0x50, 0x54, 0x58 };
97 static const struct aspeed_gpio_copro_ops
*copro_ops
;
98 static void *copro_data
;
100 static const struct aspeed_gpio_bank aspeed_gpio_banks
[] = {
105 .debounce_regs
= 0x0040,
106 .tolerance_regs
= 0x001c,
107 .cmdsrc_regs
= 0x0060,
108 .names
= { "A", "B", "C", "D" },
114 .debounce_regs
= 0x0048,
115 .tolerance_regs
= 0x003c,
116 .cmdsrc_regs
= 0x0068,
117 .names
= { "E", "F", "G", "H" },
123 .debounce_regs
= 0x00b0,
124 .tolerance_regs
= 0x00ac,
125 .cmdsrc_regs
= 0x0090,
126 .names
= { "I", "J", "K", "L" },
132 .debounce_regs
= 0x0100,
133 .tolerance_regs
= 0x00fc,
134 .cmdsrc_regs
= 0x00e0,
135 .names
= { "M", "N", "O", "P" },
141 .debounce_regs
= 0x0130,
142 .tolerance_regs
= 0x012c,
143 .cmdsrc_regs
= 0x0110,
144 .names
= { "Q", "R", "S", "T" },
150 .debounce_regs
= 0x0160,
151 .tolerance_regs
= 0x015c,
152 .cmdsrc_regs
= 0x0140,
153 .names
= { "U", "V", "W", "X" },
159 .debounce_regs
= 0x0190,
160 .tolerance_regs
= 0x018c,
161 .cmdsrc_regs
= 0x0170,
162 .names
= { "Y", "Z", "AA", "AB" },
168 .debounce_regs
= 0x01c0,
169 .tolerance_regs
= 0x01bc,
170 .cmdsrc_regs
= 0x01a0,
171 .names
= { "AC", "", "", "" },
175 enum aspeed_gpio_reg
{
191 #define GPIO_VAL_VALUE 0x00
192 #define GPIO_VAL_DIR 0x04
194 #define GPIO_IRQ_ENABLE 0x00
195 #define GPIO_IRQ_TYPE0 0x04
196 #define GPIO_IRQ_TYPE1 0x08
197 #define GPIO_IRQ_TYPE2 0x0c
198 #define GPIO_IRQ_STATUS 0x10
200 #define GPIO_DEBOUNCE_SEL1 0x00
201 #define GPIO_DEBOUNCE_SEL2 0x04
203 #define GPIO_CMDSRC_0 0x00
204 #define GPIO_CMDSRC_1 0x04
205 #define GPIO_CMDSRC_ARM 0
206 #define GPIO_CMDSRC_LPC 1
207 #define GPIO_CMDSRC_COLDFIRE 2
208 #define GPIO_CMDSRC_RESERVED 3
210 /* This will be resolved at compile time */
211 static inline void __iomem
*bank_reg(struct aspeed_gpio
*gpio
,
212 const struct aspeed_gpio_bank
*bank
,
213 const enum aspeed_gpio_reg reg
)
217 return gpio
->base
+ bank
->val_regs
+ GPIO_VAL_VALUE
;
219 return gpio
->base
+ bank
->rdata_reg
;
221 return gpio
->base
+ bank
->val_regs
+ GPIO_VAL_DIR
;
223 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_ENABLE
;
225 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_TYPE0
;
227 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_TYPE1
;
229 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_TYPE2
;
231 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_STATUS
;
232 case reg_debounce_sel1
:
233 return gpio
->base
+ bank
->debounce_regs
+ GPIO_DEBOUNCE_SEL1
;
234 case reg_debounce_sel2
:
235 return gpio
->base
+ bank
->debounce_regs
+ GPIO_DEBOUNCE_SEL2
;
237 return gpio
->base
+ bank
->tolerance_regs
;
239 return gpio
->base
+ bank
->cmdsrc_regs
+ GPIO_CMDSRC_0
;
241 return gpio
->base
+ bank
->cmdsrc_regs
+ GPIO_CMDSRC_1
;
246 #define GPIO_BANK(x) ((x) >> 5)
247 #define GPIO_OFFSET(x) ((x) & 0x1f)
248 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
250 #define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
251 #define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
252 #define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
254 static const struct aspeed_gpio_bank
*to_bank(unsigned int offset
)
256 unsigned int bank
= GPIO_BANK(offset
);
258 WARN_ON(bank
>= ARRAY_SIZE(aspeed_gpio_banks
));
259 return &aspeed_gpio_banks
[bank
];
262 static inline bool is_bank_props_sentinel(const struct aspeed_bank_props
*props
)
264 return !(props
->input
|| props
->output
);
267 static inline const struct aspeed_bank_props
*find_bank_props(
268 struct aspeed_gpio
*gpio
, unsigned int offset
)
270 const struct aspeed_bank_props
*props
= gpio
->config
->props
;
272 while (!is_bank_props_sentinel(props
)) {
273 if (props
->bank
== GPIO_BANK(offset
))
281 static inline bool have_gpio(struct aspeed_gpio
*gpio
, unsigned int offset
)
283 const struct aspeed_bank_props
*props
= find_bank_props(gpio
, offset
);
284 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
285 unsigned int group
= GPIO_OFFSET(offset
) / 8;
287 return bank
->names
[group
][0] != '\0' &&
288 (!props
|| ((props
->input
| props
->output
) & GPIO_BIT(offset
)));
291 static inline bool have_input(struct aspeed_gpio
*gpio
, unsigned int offset
)
293 const struct aspeed_bank_props
*props
= find_bank_props(gpio
, offset
);
295 return !props
|| (props
->input
& GPIO_BIT(offset
));
298 #define have_irq(g, o) have_input((g), (o))
299 #define have_debounce(g, o) have_input((g), (o))
301 static inline bool have_output(struct aspeed_gpio
*gpio
, unsigned int offset
)
303 const struct aspeed_bank_props
*props
= find_bank_props(gpio
, offset
);
305 return !props
|| (props
->output
& GPIO_BIT(offset
));
308 static void aspeed_gpio_change_cmd_source(struct aspeed_gpio
*gpio
,
309 const struct aspeed_gpio_bank
*bank
,
310 int bindex
, int cmdsrc
)
312 void __iomem
*c0
= bank_reg(gpio
, bank
, reg_cmdsrc0
);
313 void __iomem
*c1
= bank_reg(gpio
, bank
, reg_cmdsrc1
);
317 * Each register controls 4 banks, so take the bottom 2
318 * bits of the bank index, and use them to select the
319 * right control bit (0, 8, 16 or 24).
321 bit
= BIT((bindex
& 3) << 3);
323 /* Source 1 first to avoid illegal 11 combination */
340 static bool aspeed_gpio_copro_request(struct aspeed_gpio
*gpio
,
343 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
345 if (!copro_ops
|| !gpio
->cf_copro_bankmap
)
347 if (!gpio
->cf_copro_bankmap
[offset
>> 3])
349 if (!copro_ops
->request_access
)
352 /* Pause the coprocessor */
353 copro_ops
->request_access(copro_data
);
355 /* Change command source back to ARM */
356 aspeed_gpio_change_cmd_source(gpio
, bank
, offset
>> 3, GPIO_CMDSRC_ARM
);
359 gpio
->dcache
[GPIO_BANK(offset
)] = ioread32(bank_reg(gpio
, bank
, reg_rdata
));
364 static void aspeed_gpio_copro_release(struct aspeed_gpio
*gpio
,
367 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
369 if (!copro_ops
|| !gpio
->cf_copro_bankmap
)
371 if (!gpio
->cf_copro_bankmap
[offset
>> 3])
373 if (!copro_ops
->release_access
)
376 /* Change command source back to ColdFire */
377 aspeed_gpio_change_cmd_source(gpio
, bank
, offset
>> 3,
378 GPIO_CMDSRC_COLDFIRE
);
380 /* Restart the coprocessor */
381 copro_ops
->release_access(copro_data
);
384 static int aspeed_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
386 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
387 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
389 return !!(ioread32(bank_reg(gpio
, bank
, reg_val
)) & GPIO_BIT(offset
));
392 static void __aspeed_gpio_set(struct gpio_chip
*gc
, unsigned int offset
,
395 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
396 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
400 addr
= bank_reg(gpio
, bank
, reg_val
);
401 reg
= gpio
->dcache
[GPIO_BANK(offset
)];
404 reg
|= GPIO_BIT(offset
);
406 reg
&= ~GPIO_BIT(offset
);
407 gpio
->dcache
[GPIO_BANK(offset
)] = reg
;
409 iowrite32(reg
, addr
);
412 static void aspeed_gpio_set(struct gpio_chip
*gc
, unsigned int offset
,
415 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
419 spin_lock_irqsave(&gpio
->lock
, flags
);
420 copro
= aspeed_gpio_copro_request(gpio
, offset
);
422 __aspeed_gpio_set(gc
, offset
, val
);
425 aspeed_gpio_copro_release(gpio
, offset
);
426 spin_unlock_irqrestore(&gpio
->lock
, flags
);
429 static int aspeed_gpio_dir_in(struct gpio_chip
*gc
, unsigned int offset
)
431 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
432 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
433 void __iomem
*addr
= bank_reg(gpio
, bank
, reg_dir
);
438 if (!have_input(gpio
, offset
))
441 spin_lock_irqsave(&gpio
->lock
, flags
);
443 reg
= ioread32(addr
);
444 reg
&= ~GPIO_BIT(offset
);
446 copro
= aspeed_gpio_copro_request(gpio
, offset
);
447 iowrite32(reg
, addr
);
449 aspeed_gpio_copro_release(gpio
, offset
);
451 spin_unlock_irqrestore(&gpio
->lock
, flags
);
456 static int aspeed_gpio_dir_out(struct gpio_chip
*gc
,
457 unsigned int offset
, int val
)
459 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
460 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
461 void __iomem
*addr
= bank_reg(gpio
, bank
, reg_dir
);
466 if (!have_output(gpio
, offset
))
469 spin_lock_irqsave(&gpio
->lock
, flags
);
471 reg
= ioread32(addr
);
472 reg
|= GPIO_BIT(offset
);
474 copro
= aspeed_gpio_copro_request(gpio
, offset
);
475 __aspeed_gpio_set(gc
, offset
, val
);
476 iowrite32(reg
, addr
);
479 aspeed_gpio_copro_release(gpio
, offset
);
480 spin_unlock_irqrestore(&gpio
->lock
, flags
);
485 static int aspeed_gpio_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
487 struct aspeed_gpio
*gpio
= gpiochip_get_data(gc
);
488 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
492 if (!have_input(gpio
, offset
))
495 if (!have_output(gpio
, offset
))
498 spin_lock_irqsave(&gpio
->lock
, flags
);
500 val
= ioread32(bank_reg(gpio
, bank
, reg_dir
)) & GPIO_BIT(offset
);
502 spin_unlock_irqrestore(&gpio
->lock
, flags
);
508 static inline int irqd_to_aspeed_gpio_data(struct irq_data
*d
,
509 struct aspeed_gpio
**gpio
,
510 const struct aspeed_gpio_bank
**bank
,
511 u32
*bit
, int *offset
)
513 struct aspeed_gpio
*internal
;
515 *offset
= irqd_to_hwirq(d
);
517 internal
= irq_data_get_irq_chip_data(d
);
519 /* This might be a bit of a questionable place to check */
520 if (!have_irq(internal
, *offset
))
524 *bank
= to_bank(*offset
);
525 *bit
= GPIO_BIT(*offset
);
530 static void aspeed_gpio_irq_ack(struct irq_data
*d
)
532 const struct aspeed_gpio_bank
*bank
;
533 struct aspeed_gpio
*gpio
;
535 void __iomem
*status_addr
;
540 rc
= irqd_to_aspeed_gpio_data(d
, &gpio
, &bank
, &bit
, &offset
);
544 status_addr
= bank_reg(gpio
, bank
, reg_irq_status
);
546 spin_lock_irqsave(&gpio
->lock
, flags
);
547 copro
= aspeed_gpio_copro_request(gpio
, offset
);
549 iowrite32(bit
, status_addr
);
552 aspeed_gpio_copro_release(gpio
, offset
);
553 spin_unlock_irqrestore(&gpio
->lock
, flags
);
556 static void aspeed_gpio_irq_set_mask(struct irq_data
*d
, bool set
)
558 const struct aspeed_gpio_bank
*bank
;
559 struct aspeed_gpio
*gpio
;
566 rc
= irqd_to_aspeed_gpio_data(d
, &gpio
, &bank
, &bit
, &offset
);
570 addr
= bank_reg(gpio
, bank
, reg_irq_enable
);
572 spin_lock_irqsave(&gpio
->lock
, flags
);
573 copro
= aspeed_gpio_copro_request(gpio
, offset
);
575 reg
= ioread32(addr
);
580 iowrite32(reg
, addr
);
583 aspeed_gpio_copro_release(gpio
, offset
);
584 spin_unlock_irqrestore(&gpio
->lock
, flags
);
587 static void aspeed_gpio_irq_mask(struct irq_data
*d
)
589 aspeed_gpio_irq_set_mask(d
, false);
592 static void aspeed_gpio_irq_unmask(struct irq_data
*d
)
594 aspeed_gpio_irq_set_mask(d
, true);
597 static int aspeed_gpio_set_type(struct irq_data
*d
, unsigned int type
)
603 const struct aspeed_gpio_bank
*bank
;
604 irq_flow_handler_t handler
;
605 struct aspeed_gpio
*gpio
;
611 rc
= irqd_to_aspeed_gpio_data(d
, &gpio
, &bank
, &bit
, &offset
);
615 switch (type
& IRQ_TYPE_SENSE_MASK
) {
616 case IRQ_TYPE_EDGE_BOTH
:
619 case IRQ_TYPE_EDGE_RISING
:
622 case IRQ_TYPE_EDGE_FALLING
:
623 handler
= handle_edge_irq
;
625 case IRQ_TYPE_LEVEL_HIGH
:
628 case IRQ_TYPE_LEVEL_LOW
:
630 handler
= handle_level_irq
;
636 spin_lock_irqsave(&gpio
->lock
, flags
);
637 copro
= aspeed_gpio_copro_request(gpio
, offset
);
639 addr
= bank_reg(gpio
, bank
, reg_irq_type0
);
640 reg
= ioread32(addr
);
641 reg
= (reg
& ~bit
) | type0
;
642 iowrite32(reg
, addr
);
644 addr
= bank_reg(gpio
, bank
, reg_irq_type1
);
645 reg
= ioread32(addr
);
646 reg
= (reg
& ~bit
) | type1
;
647 iowrite32(reg
, addr
);
649 addr
= bank_reg(gpio
, bank
, reg_irq_type2
);
650 reg
= ioread32(addr
);
651 reg
= (reg
& ~bit
) | type2
;
652 iowrite32(reg
, addr
);
655 aspeed_gpio_copro_release(gpio
, offset
);
656 spin_unlock_irqrestore(&gpio
->lock
, flags
);
658 irq_set_handler_locked(d
, handler
);
663 static void aspeed_gpio_irq_handler(struct irq_desc
*desc
)
665 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
666 struct irq_chip
*ic
= irq_desc_get_chip(desc
);
667 struct aspeed_gpio
*data
= gpiochip_get_data(gc
);
668 unsigned int i
, p
, girq
;
671 chained_irq_enter(ic
, desc
);
673 for (i
= 0; i
< ARRAY_SIZE(aspeed_gpio_banks
); i
++) {
674 const struct aspeed_gpio_bank
*bank
= &aspeed_gpio_banks
[i
];
676 reg
= ioread32(bank_reg(data
, bank
, reg_irq_status
));
678 for_each_set_bit(p
, ®
, 32) {
679 girq
= irq_find_mapping(gc
->irq
.domain
, i
* 32 + p
);
680 generic_handle_irq(girq
);
685 chained_irq_exit(ic
, desc
);
688 static struct irq_chip aspeed_gpio_irqchip
= {
689 .name
= "aspeed-gpio",
690 .irq_ack
= aspeed_gpio_irq_ack
,
691 .irq_mask
= aspeed_gpio_irq_mask
,
692 .irq_unmask
= aspeed_gpio_irq_unmask
,
693 .irq_set_type
= aspeed_gpio_set_type
,
696 static void set_irq_valid_mask(struct aspeed_gpio
*gpio
)
698 const struct aspeed_bank_props
*props
= gpio
->config
->props
;
700 while (!is_bank_props_sentinel(props
)) {
702 const unsigned long int input
= props
->input
;
704 /* Pretty crummy approach, but similar to GPIO core */
705 for_each_clear_bit(offset
, &input
, 32) {
706 unsigned int i
= props
->bank
* 32 + offset
;
708 if (i
>= gpio
->config
->nr_gpios
)
711 clear_bit(i
, gpio
->chip
.irq
.valid_mask
);
718 static int aspeed_gpio_setup_irqs(struct aspeed_gpio
*gpio
,
719 struct platform_device
*pdev
)
723 rc
= platform_get_irq(pdev
, 0);
729 set_irq_valid_mask(gpio
);
731 rc
= gpiochip_irqchip_add(&gpio
->chip
, &aspeed_gpio_irqchip
,
732 0, handle_bad_irq
, IRQ_TYPE_NONE
);
734 dev_info(&pdev
->dev
, "Could not add irqchip\n");
738 gpiochip_set_chained_irqchip(&gpio
->chip
, &aspeed_gpio_irqchip
,
739 gpio
->irq
, aspeed_gpio_irq_handler
);
744 static int aspeed_gpio_reset_tolerance(struct gpio_chip
*chip
,
745 unsigned int offset
, bool enable
)
747 struct aspeed_gpio
*gpio
= gpiochip_get_data(chip
);
753 treg
= bank_reg(gpio
, to_bank(offset
), reg_tolerance
);
755 spin_lock_irqsave(&gpio
->lock
, flags
);
756 copro
= aspeed_gpio_copro_request(gpio
, offset
);
761 val
|= GPIO_BIT(offset
);
763 val
&= ~GPIO_BIT(offset
);
768 aspeed_gpio_copro_release(gpio
, offset
);
769 spin_unlock_irqrestore(&gpio
->lock
, flags
);
774 static int aspeed_gpio_request(struct gpio_chip
*chip
, unsigned int offset
)
776 if (!have_gpio(gpiochip_get_data(chip
), offset
))
779 return pinctrl_gpio_request(chip
->base
+ offset
);
782 static void aspeed_gpio_free(struct gpio_chip
*chip
, unsigned int offset
)
784 pinctrl_gpio_free(chip
->base
+ offset
);
787 static int usecs_to_cycles(struct aspeed_gpio
*gpio
, unsigned long usecs
,
794 rate
= clk_get_rate(gpio
->clk
);
799 r
= do_div(n
, 1000000);
804 /* At least as long as the requested time */
810 /* Call under gpio->lock */
811 static int register_allocated_timer(struct aspeed_gpio
*gpio
,
812 unsigned int offset
, unsigned int timer
)
814 if (WARN(gpio
->offset_timer
[offset
] != 0,
815 "Offset %d already allocated timer %d\n",
816 offset
, gpio
->offset_timer
[offset
]))
819 if (WARN(gpio
->timer_users
[timer
] == UINT_MAX
,
820 "Timer user count would overflow\n"))
823 gpio
->offset_timer
[offset
] = timer
;
824 gpio
->timer_users
[timer
]++;
829 /* Call under gpio->lock */
830 static int unregister_allocated_timer(struct aspeed_gpio
*gpio
,
833 if (WARN(gpio
->offset_timer
[offset
] == 0,
834 "No timer allocated to offset %d\n", offset
))
837 if (WARN(gpio
->timer_users
[gpio
->offset_timer
[offset
]] == 0,
838 "No users recorded for timer %d\n",
839 gpio
->offset_timer
[offset
]))
842 gpio
->timer_users
[gpio
->offset_timer
[offset
]]--;
843 gpio
->offset_timer
[offset
] = 0;
848 /* Call under gpio->lock */
849 static inline bool timer_allocation_registered(struct aspeed_gpio
*gpio
,
852 return gpio
->offset_timer
[offset
] > 0;
855 /* Call under gpio->lock */
856 static void configure_timer(struct aspeed_gpio
*gpio
, unsigned int offset
,
859 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
860 const u32 mask
= GPIO_BIT(offset
);
864 /* Note: Debounce timer isn't under control of the command
865 * source registers, so no need to sync with the coprocessor
867 addr
= bank_reg(gpio
, bank
, reg_debounce_sel1
);
868 val
= ioread32(addr
);
869 iowrite32((val
& ~mask
) | GPIO_SET_DEBOUNCE1(timer
, offset
), addr
);
871 addr
= bank_reg(gpio
, bank
, reg_debounce_sel2
);
872 val
= ioread32(addr
);
873 iowrite32((val
& ~mask
) | GPIO_SET_DEBOUNCE2(timer
, offset
), addr
);
876 static int enable_debounce(struct gpio_chip
*chip
, unsigned int offset
,
879 struct aspeed_gpio
*gpio
= gpiochip_get_data(chip
);
880 u32 requested_cycles
;
888 rc
= usecs_to_cycles(gpio
, usecs
, &requested_cycles
);
890 dev_warn(chip
->parent
, "Failed to convert %luus to cycles at %luHz: %d\n",
891 usecs
, clk_get_rate(gpio
->clk
), rc
);
895 spin_lock_irqsave(&gpio
->lock
, flags
);
897 if (timer_allocation_registered(gpio
, offset
)) {
898 rc
= unregister_allocated_timer(gpio
, offset
);
903 /* Try to find a timer already configured for the debounce period */
904 for (i
= 1; i
< ARRAY_SIZE(debounce_timers
); i
++) {
907 cycles
= ioread32(gpio
->base
+ debounce_timers
[i
]);
908 if (requested_cycles
== cycles
)
912 if (i
== ARRAY_SIZE(debounce_timers
)) {
916 * As there are no timers configured for the requested debounce
917 * period, find an unused timer instead
919 for (j
= 1; j
< ARRAY_SIZE(gpio
->timer_users
); j
++) {
920 if (gpio
->timer_users
[j
] == 0)
924 if (j
== ARRAY_SIZE(gpio
->timer_users
)) {
925 dev_warn(chip
->parent
,
926 "Debounce timers exhausted, cannot debounce for period %luus\n",
932 * We already adjusted the accounting to remove @offset
933 * as a user of its previous timer, so also configure
934 * the hardware so @offset has timers disabled for
937 configure_timer(gpio
, offset
, 0);
943 iowrite32(requested_cycles
, gpio
->base
+ debounce_timers
[i
]);
946 if (WARN(i
== 0, "Cannot register index of disabled timer\n")) {
951 register_allocated_timer(gpio
, offset
, i
);
952 configure_timer(gpio
, offset
, i
);
955 spin_unlock_irqrestore(&gpio
->lock
, flags
);
960 static int disable_debounce(struct gpio_chip
*chip
, unsigned int offset
)
962 struct aspeed_gpio
*gpio
= gpiochip_get_data(chip
);
966 spin_lock_irqsave(&gpio
->lock
, flags
);
968 rc
= unregister_allocated_timer(gpio
, offset
);
970 configure_timer(gpio
, offset
, 0);
972 spin_unlock_irqrestore(&gpio
->lock
, flags
);
977 static int set_debounce(struct gpio_chip
*chip
, unsigned int offset
,
980 struct aspeed_gpio
*gpio
= gpiochip_get_data(chip
);
982 if (!have_debounce(gpio
, offset
))
986 return enable_debounce(chip
, offset
, usecs
);
988 return disable_debounce(chip
, offset
);
991 static int aspeed_gpio_set_config(struct gpio_chip
*chip
, unsigned int offset
,
992 unsigned long config
)
994 unsigned long param
= pinconf_to_config_param(config
);
995 u32 arg
= pinconf_to_config_argument(config
);
997 if (param
== PIN_CONFIG_INPUT_DEBOUNCE
)
998 return set_debounce(chip
, offset
, arg
);
999 else if (param
== PIN_CONFIG_BIAS_DISABLE
||
1000 param
== PIN_CONFIG_BIAS_PULL_DOWN
||
1001 param
== PIN_CONFIG_DRIVE_STRENGTH
)
1002 return pinctrl_gpio_set_config(offset
, config
);
1003 else if (param
== PIN_CONFIG_DRIVE_OPEN_DRAIN
||
1004 param
== PIN_CONFIG_DRIVE_OPEN_SOURCE
)
1005 /* Return -ENOTSUPP to trigger emulation, as per datasheet */
1007 else if (param
== PIN_CONFIG_PERSIST_STATE
)
1008 return aspeed_gpio_reset_tolerance(chip
, offset
, arg
);
1014 * aspeed_gpio_copro_set_ops - Sets the callbacks used for handhsaking with
1015 * the coprocessor for shared GPIO banks
1016 * @ops: The callbacks
1017 * @data: Pointer passed back to the callbacks
1019 int aspeed_gpio_copro_set_ops(const struct aspeed_gpio_copro_ops
*ops
, void *data
)
1026 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_set_ops
);
1029 * aspeed_gpio_copro_grab_gpio - Mark a GPIO used by the coprocessor. The entire
1030 * bank gets marked and any access from the ARM will
1031 * result in handshaking via callbacks.
1032 * @desc: The GPIO to be marked
1033 * @vreg_offset: If non-NULL, returns the value register offset in the GPIO space
1034 * @dreg_offset: If non-NULL, returns the data latch register offset in the GPIO space
1035 * @bit: If non-NULL, returns the bit number of the GPIO in the registers
1037 int aspeed_gpio_copro_grab_gpio(struct gpio_desc
*desc
,
1038 u16
*vreg_offset
, u16
*dreg_offset
, u8
*bit
)
1040 struct gpio_chip
*chip
= gpiod_to_chip(desc
);
1041 struct aspeed_gpio
*gpio
= gpiochip_get_data(chip
);
1042 int rc
= 0, bindex
, offset
= gpio_chip_hwgpio(desc
);
1043 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
1044 unsigned long flags
;
1046 if (!gpio
->cf_copro_bankmap
)
1047 gpio
->cf_copro_bankmap
= kzalloc(gpio
->config
->nr_gpios
>> 3, GFP_KERNEL
);
1048 if (!gpio
->cf_copro_bankmap
)
1050 if (offset
< 0 || offset
> gpio
->config
->nr_gpios
)
1052 bindex
= offset
>> 3;
1054 spin_lock_irqsave(&gpio
->lock
, flags
);
1056 /* Sanity check, this shouldn't happen */
1057 if (gpio
->cf_copro_bankmap
[bindex
] == 0xff) {
1061 gpio
->cf_copro_bankmap
[bindex
]++;
1063 /* Switch command source */
1064 if (gpio
->cf_copro_bankmap
[bindex
] == 1)
1065 aspeed_gpio_change_cmd_source(gpio
, bank
, bindex
,
1066 GPIO_CMDSRC_COLDFIRE
);
1069 *vreg_offset
= bank
->val_regs
;
1071 *dreg_offset
= bank
->rdata_reg
;
1073 *bit
= GPIO_OFFSET(offset
);
1075 spin_unlock_irqrestore(&gpio
->lock
, flags
);
1078 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_grab_gpio
);
1081 * aspeed_gpio_copro_release_gpio - Unmark a GPIO used by the coprocessor.
1082 * @desc: The GPIO to be marked
1084 int aspeed_gpio_copro_release_gpio(struct gpio_desc
*desc
)
1086 struct gpio_chip
*chip
= gpiod_to_chip(desc
);
1087 struct aspeed_gpio
*gpio
= gpiochip_get_data(chip
);
1088 int rc
= 0, bindex
, offset
= gpio_chip_hwgpio(desc
);
1089 const struct aspeed_gpio_bank
*bank
= to_bank(offset
);
1090 unsigned long flags
;
1092 if (!gpio
->cf_copro_bankmap
)
1095 if (offset
< 0 || offset
> gpio
->config
->nr_gpios
)
1097 bindex
= offset
>> 3;
1099 spin_lock_irqsave(&gpio
->lock
, flags
);
1101 /* Sanity check, this shouldn't happen */
1102 if (gpio
->cf_copro_bankmap
[bindex
] == 0) {
1106 gpio
->cf_copro_bankmap
[bindex
]--;
1108 /* Switch command source */
1109 if (gpio
->cf_copro_bankmap
[bindex
] == 0)
1110 aspeed_gpio_change_cmd_source(gpio
, bank
, bindex
,
1113 spin_unlock_irqrestore(&gpio
->lock
, flags
);
1116 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_release_gpio
);
1119 * Any banks not specified in a struct aspeed_bank_props array are assumed to
1120 * have the properties:
1122 * { .input = 0xffffffff, .output = 0xffffffff }
1125 static const struct aspeed_bank_props ast2400_bank_props
[] = {
1127 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1128 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
1132 static const struct aspeed_gpio_config ast2400_config
=
1133 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
1134 { .nr_gpios
= 220, .props
= ast2400_bank_props
, };
1136 static const struct aspeed_bank_props ast2500_bank_props
[] = {
1138 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1139 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
1140 { 7, 0x000000ff, 0x000000ff }, /* AC */
1144 static const struct aspeed_gpio_config ast2500_config
=
1145 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
1146 { .nr_gpios
= 232, .props
= ast2500_bank_props
, };
1148 static const struct of_device_id aspeed_gpio_of_table
[] = {
1149 { .compatible
= "aspeed,ast2400-gpio", .data
= &ast2400_config
, },
1150 { .compatible
= "aspeed,ast2500-gpio", .data
= &ast2500_config
, },
1153 MODULE_DEVICE_TABLE(of
, aspeed_gpio_of_table
);
1155 static int __init
aspeed_gpio_probe(struct platform_device
*pdev
)
1157 const struct of_device_id
*gpio_id
;
1158 struct aspeed_gpio
*gpio
;
1159 struct resource
*res
;
1162 gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*gpio
), GFP_KERNEL
);
1166 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1167 gpio
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
1168 if (IS_ERR(gpio
->base
))
1169 return PTR_ERR(gpio
->base
);
1171 spin_lock_init(&gpio
->lock
);
1173 gpio_id
= of_match_node(aspeed_gpio_of_table
, pdev
->dev
.of_node
);
1177 gpio
->clk
= of_clk_get(pdev
->dev
.of_node
, 0);
1178 if (IS_ERR(gpio
->clk
)) {
1179 dev_warn(&pdev
->dev
,
1180 "Failed to get clock from devicetree, debouncing disabled\n");
1184 gpio
->config
= gpio_id
->data
;
1186 gpio
->chip
.parent
= &pdev
->dev
;
1187 gpio
->chip
.ngpio
= gpio
->config
->nr_gpios
;
1188 gpio
->chip
.parent
= &pdev
->dev
;
1189 gpio
->chip
.direction_input
= aspeed_gpio_dir_in
;
1190 gpio
->chip
.direction_output
= aspeed_gpio_dir_out
;
1191 gpio
->chip
.get_direction
= aspeed_gpio_get_direction
;
1192 gpio
->chip
.request
= aspeed_gpio_request
;
1193 gpio
->chip
.free
= aspeed_gpio_free
;
1194 gpio
->chip
.get
= aspeed_gpio_get
;
1195 gpio
->chip
.set
= aspeed_gpio_set
;
1196 gpio
->chip
.set_config
= aspeed_gpio_set_config
;
1197 gpio
->chip
.label
= dev_name(&pdev
->dev
);
1198 gpio
->chip
.base
= -1;
1199 gpio
->chip
.irq
.need_valid_mask
= true;
1201 /* Allocate a cache of the output registers */
1202 banks
= DIV_ROUND_UP(gpio
->config
->nr_gpios
, 32);
1203 gpio
->dcache
= devm_kcalloc(&pdev
->dev
,
1204 banks
, sizeof(u32
), GFP_KERNEL
);
1209 * Populate it with initial values read from the HW and switch
1210 * all command sources to the ARM by default
1212 for (i
= 0; i
< banks
; i
++) {
1213 const struct aspeed_gpio_bank
*bank
= &aspeed_gpio_banks
[i
];
1214 void __iomem
*addr
= bank_reg(gpio
, bank
, reg_rdata
);
1215 gpio
->dcache
[i
] = ioread32(addr
);
1216 aspeed_gpio_change_cmd_source(gpio
, bank
, 0, GPIO_CMDSRC_ARM
);
1217 aspeed_gpio_change_cmd_source(gpio
, bank
, 1, GPIO_CMDSRC_ARM
);
1218 aspeed_gpio_change_cmd_source(gpio
, bank
, 2, GPIO_CMDSRC_ARM
);
1219 aspeed_gpio_change_cmd_source(gpio
, bank
, 3, GPIO_CMDSRC_ARM
);
1222 rc
= devm_gpiochip_add_data(&pdev
->dev
, &gpio
->chip
, gpio
);
1226 gpio
->offset_timer
=
1227 devm_kzalloc(&pdev
->dev
, gpio
->chip
.ngpio
, GFP_KERNEL
);
1228 if (!gpio
->offset_timer
)
1231 return aspeed_gpio_setup_irqs(gpio
, pdev
);
1234 static struct platform_driver aspeed_gpio_driver
= {
1236 .name
= KBUILD_MODNAME
,
1237 .of_match_table
= aspeed_gpio_of_table
,
1241 module_platform_driver_probe(aspeed_gpio_driver
, aspeed_gpio_probe
);
1243 MODULE_DESCRIPTION("Aspeed GPIO Driver");
1244 MODULE_LICENSE("GPL");