1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2015 IBM Corp.
9 #include <linux/gpio/aspeed.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/hashtable.h>
12 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/platform_device.h>
18 #include <linux/spinlock.h>
19 #include <linux/string.h>
21 #include <asm/div64.h>
24 * These two headers aren't meant to be used by GPIO drivers. We need
25 * them in order to access gpio_chip_hwgpio() which we need to implement
26 * the aspeed specific API which allows the coprocessor to request
27 * access to some GPIOs and to arbitrate between coprocessor and ARM.
29 #include <linux/gpio/consumer.h>
32 struct aspeed_bank_props {
38 struct aspeed_gpio_config {
39 unsigned int nr_gpios;
40 const struct aspeed_bank_props *props;
44 * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled
45 * @timer_users: Tracks the number of users for each timer
47 * The @timer_users has four elements but the first element is unused. This is
48 * to simplify accounting and indexing, as a zero value in @offset_timer
49 * represents disabled debouncing for the GPIO. Any other value for an element
50 * of @offset_timer is used as an index into @timer_users. This behaviour of
51 * the zero value aligns with the behaviour of zero built from the timer
52 * configuration registers (i.e. debouncing is disabled).
55 struct gpio_chip chip;
60 const struct aspeed_gpio_config *config;
63 unsigned int timer_users[4];
70 struct aspeed_gpio_bank {
71 uint16_t val_regs; /* +0: Rd: read input value, Wr: set write latch
72 * +4: Rd/Wr: Direction (0=in, 1=out)
74 uint16_t rdata_reg; /* Rd: read write latch, Wr: <none> */
76 uint16_t debounce_regs;
77 uint16_t tolerance_regs;
79 const char names[4][3];
83 * Note: The "value" register returns the input value sampled on the
84 * line even when the GPIO is configured as an output. Since
85 * that input goes through synchronizers, writing, then reading
86 * back may not return the written value right away.
88 * The "rdata" register returns the content of the write latch
89 * and thus can be used to read back what was last written
93 static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 };
95 static const struct aspeed_gpio_copro_ops *copro_ops;
96 static void *copro_data;
98 static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
103 .debounce_regs = 0x0040,
104 .tolerance_regs = 0x001c,
105 .cmdsrc_regs = 0x0060,
106 .names = { "A", "B", "C", "D" },
112 .debounce_regs = 0x0048,
113 .tolerance_regs = 0x003c,
114 .cmdsrc_regs = 0x0068,
115 .names = { "E", "F", "G", "H" },
121 .debounce_regs = 0x00b0,
122 .tolerance_regs = 0x00ac,
123 .cmdsrc_regs = 0x0090,
124 .names = { "I", "J", "K", "L" },
130 .debounce_regs = 0x0100,
131 .tolerance_regs = 0x00fc,
132 .cmdsrc_regs = 0x00e0,
133 .names = { "M", "N", "O", "P" },
139 .debounce_regs = 0x0130,
140 .tolerance_regs = 0x012c,
141 .cmdsrc_regs = 0x0110,
142 .names = { "Q", "R", "S", "T" },
148 .debounce_regs = 0x0160,
149 .tolerance_regs = 0x015c,
150 .cmdsrc_regs = 0x0140,
151 .names = { "U", "V", "W", "X" },
157 .debounce_regs = 0x0190,
158 .tolerance_regs = 0x018c,
159 .cmdsrc_regs = 0x0170,
160 .names = { "Y", "Z", "AA", "AB" },
166 .debounce_regs = 0x01c0,
167 .tolerance_regs = 0x01bc,
168 .cmdsrc_regs = 0x01a0,
169 .names = { "AC", "", "", "" },
173 enum aspeed_gpio_reg {
189 #define GPIO_VAL_VALUE 0x00
190 #define GPIO_VAL_DIR 0x04
192 #define GPIO_IRQ_ENABLE 0x00
193 #define GPIO_IRQ_TYPE0 0x04
194 #define GPIO_IRQ_TYPE1 0x08
195 #define GPIO_IRQ_TYPE2 0x0c
196 #define GPIO_IRQ_STATUS 0x10
198 #define GPIO_DEBOUNCE_SEL1 0x00
199 #define GPIO_DEBOUNCE_SEL2 0x04
201 #define GPIO_CMDSRC_0 0x00
202 #define GPIO_CMDSRC_1 0x04
203 #define GPIO_CMDSRC_ARM 0
204 #define GPIO_CMDSRC_LPC 1
205 #define GPIO_CMDSRC_COLDFIRE 2
206 #define GPIO_CMDSRC_RESERVED 3
208 /* This will be resolved at compile time */
209 static inline void __iomem *bank_reg(struct aspeed_gpio *gpio,
210 const struct aspeed_gpio_bank *bank,
211 const enum aspeed_gpio_reg reg)
215 return gpio->base + bank->val_regs + GPIO_VAL_VALUE;
217 return gpio->base + bank->rdata_reg;
219 return gpio->base + bank->val_regs + GPIO_VAL_DIR;
221 return gpio->base + bank->irq_regs + GPIO_IRQ_ENABLE;
223 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE0;
225 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE1;
227 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE2;
229 return gpio->base + bank->irq_regs + GPIO_IRQ_STATUS;
230 case reg_debounce_sel1:
231 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL1;
232 case reg_debounce_sel2:
233 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL2;
235 return gpio->base + bank->tolerance_regs;
237 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_0;
239 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_1;
244 #define GPIO_BANK(x) ((x) >> 5)
245 #define GPIO_OFFSET(x) ((x) & 0x1f)
246 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
248 #define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
249 #define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
250 #define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
252 static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
254 unsigned int bank = GPIO_BANK(offset);
256 WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks));
257 return &aspeed_gpio_banks[bank];
260 static inline bool is_bank_props_sentinel(const struct aspeed_bank_props *props)
262 return !(props->input || props->output);
265 static inline const struct aspeed_bank_props *find_bank_props(
266 struct aspeed_gpio *gpio, unsigned int offset)
268 const struct aspeed_bank_props *props = gpio->config->props;
270 while (!is_bank_props_sentinel(props)) {
271 if (props->bank == GPIO_BANK(offset))
279 static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset)
281 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
282 const struct aspeed_gpio_bank *bank = to_bank(offset);
283 unsigned int group = GPIO_OFFSET(offset) / 8;
285 return bank->names[group][0] != '\0' &&
286 (!props || ((props->input | props->output) & GPIO_BIT(offset)));
289 static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset)
291 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
293 return !props || (props->input & GPIO_BIT(offset));
296 #define have_irq(g, o) have_input((g), (o))
297 #define have_debounce(g, o) have_input((g), (o))
299 static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset)
301 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
303 return !props || (props->output & GPIO_BIT(offset));
306 static void aspeed_gpio_change_cmd_source(struct aspeed_gpio *gpio,
307 const struct aspeed_gpio_bank *bank,
308 int bindex, int cmdsrc)
310 void __iomem *c0 = bank_reg(gpio, bank, reg_cmdsrc0);
311 void __iomem *c1 = bank_reg(gpio, bank, reg_cmdsrc1);
315 * Each register controls 4 banks, so take the bottom 2
316 * bits of the bank index, and use them to select the
317 * right control bit (0, 8, 16 or 24).
319 bit = BIT((bindex & 3) << 3);
321 /* Source 1 first to avoid illegal 11 combination */
338 static bool aspeed_gpio_copro_request(struct aspeed_gpio *gpio,
341 const struct aspeed_gpio_bank *bank = to_bank(offset);
343 if (!copro_ops || !gpio->cf_copro_bankmap)
345 if (!gpio->cf_copro_bankmap[offset >> 3])
347 if (!copro_ops->request_access)
350 /* Pause the coprocessor */
351 copro_ops->request_access(copro_data);
353 /* Change command source back to ARM */
354 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3, GPIO_CMDSRC_ARM);
357 gpio->dcache[GPIO_BANK(offset)] = ioread32(bank_reg(gpio, bank, reg_rdata));
362 static void aspeed_gpio_copro_release(struct aspeed_gpio *gpio,
365 const struct aspeed_gpio_bank *bank = to_bank(offset);
367 if (!copro_ops || !gpio->cf_copro_bankmap)
369 if (!gpio->cf_copro_bankmap[offset >> 3])
371 if (!copro_ops->release_access)
374 /* Change command source back to ColdFire */
375 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3,
376 GPIO_CMDSRC_COLDFIRE);
378 /* Restart the coprocessor */
379 copro_ops->release_access(copro_data);
382 static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
384 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
385 const struct aspeed_gpio_bank *bank = to_bank(offset);
387 return !!(ioread32(bank_reg(gpio, bank, reg_val)) & GPIO_BIT(offset));
390 static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
393 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
394 const struct aspeed_gpio_bank *bank = to_bank(offset);
398 addr = bank_reg(gpio, bank, reg_val);
399 reg = gpio->dcache[GPIO_BANK(offset)];
402 reg |= GPIO_BIT(offset);
404 reg &= ~GPIO_BIT(offset);
405 gpio->dcache[GPIO_BANK(offset)] = reg;
407 iowrite32(reg, addr);
410 static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
413 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
417 raw_spin_lock_irqsave(&gpio->lock, flags);
418 copro = aspeed_gpio_copro_request(gpio, offset);
420 __aspeed_gpio_set(gc, offset, val);
423 aspeed_gpio_copro_release(gpio, offset);
424 raw_spin_unlock_irqrestore(&gpio->lock, flags);
427 static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
429 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
430 const struct aspeed_gpio_bank *bank = to_bank(offset);
431 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
436 if (!have_input(gpio, offset))
439 raw_spin_lock_irqsave(&gpio->lock, flags);
441 reg = ioread32(addr);
442 reg &= ~GPIO_BIT(offset);
444 copro = aspeed_gpio_copro_request(gpio, offset);
445 iowrite32(reg, addr);
447 aspeed_gpio_copro_release(gpio, offset);
449 raw_spin_unlock_irqrestore(&gpio->lock, flags);
454 static int aspeed_gpio_dir_out(struct gpio_chip *gc,
455 unsigned int offset, int val)
457 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
458 const struct aspeed_gpio_bank *bank = to_bank(offset);
459 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
464 if (!have_output(gpio, offset))
467 raw_spin_lock_irqsave(&gpio->lock, flags);
469 reg = ioread32(addr);
470 reg |= GPIO_BIT(offset);
472 copro = aspeed_gpio_copro_request(gpio, offset);
473 __aspeed_gpio_set(gc, offset, val);
474 iowrite32(reg, addr);
477 aspeed_gpio_copro_release(gpio, offset);
478 raw_spin_unlock_irqrestore(&gpio->lock, flags);
483 static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
485 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
486 const struct aspeed_gpio_bank *bank = to_bank(offset);
490 if (!have_input(gpio, offset))
491 return GPIO_LINE_DIRECTION_OUT;
493 if (!have_output(gpio, offset))
494 return GPIO_LINE_DIRECTION_IN;
496 raw_spin_lock_irqsave(&gpio->lock, flags);
498 val = ioread32(bank_reg(gpio, bank, reg_dir)) & GPIO_BIT(offset);
500 raw_spin_unlock_irqrestore(&gpio->lock, flags);
502 return val ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
505 static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
506 struct aspeed_gpio **gpio,
507 const struct aspeed_gpio_bank **bank,
508 u32 *bit, int *offset)
510 struct aspeed_gpio *internal;
512 *offset = irqd_to_hwirq(d);
514 internal = irq_data_get_irq_chip_data(d);
516 /* This might be a bit of a questionable place to check */
517 if (!have_irq(internal, *offset))
521 *bank = to_bank(*offset);
522 *bit = GPIO_BIT(*offset);
527 static void aspeed_gpio_irq_ack(struct irq_data *d)
529 const struct aspeed_gpio_bank *bank;
530 struct aspeed_gpio *gpio;
532 void __iomem *status_addr;
537 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
541 status_addr = bank_reg(gpio, bank, reg_irq_status);
543 raw_spin_lock_irqsave(&gpio->lock, flags);
544 copro = aspeed_gpio_copro_request(gpio, offset);
546 iowrite32(bit, status_addr);
549 aspeed_gpio_copro_release(gpio, offset);
550 raw_spin_unlock_irqrestore(&gpio->lock, flags);
553 static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
555 const struct aspeed_gpio_bank *bank;
556 struct aspeed_gpio *gpio;
563 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
567 addr = bank_reg(gpio, bank, reg_irq_enable);
569 raw_spin_lock_irqsave(&gpio->lock, flags);
570 copro = aspeed_gpio_copro_request(gpio, offset);
572 reg = ioread32(addr);
577 iowrite32(reg, addr);
580 aspeed_gpio_copro_release(gpio, offset);
581 raw_spin_unlock_irqrestore(&gpio->lock, flags);
584 static void aspeed_gpio_irq_mask(struct irq_data *d)
586 aspeed_gpio_irq_set_mask(d, false);
589 static void aspeed_gpio_irq_unmask(struct irq_data *d)
591 aspeed_gpio_irq_set_mask(d, true);
594 static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
600 const struct aspeed_gpio_bank *bank;
601 irq_flow_handler_t handler;
602 struct aspeed_gpio *gpio;
608 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
612 switch (type & IRQ_TYPE_SENSE_MASK) {
613 case IRQ_TYPE_EDGE_BOTH:
616 case IRQ_TYPE_EDGE_RISING:
619 case IRQ_TYPE_EDGE_FALLING:
620 handler = handle_edge_irq;
622 case IRQ_TYPE_LEVEL_HIGH:
625 case IRQ_TYPE_LEVEL_LOW:
627 handler = handle_level_irq;
633 raw_spin_lock_irqsave(&gpio->lock, flags);
634 copro = aspeed_gpio_copro_request(gpio, offset);
636 addr = bank_reg(gpio, bank, reg_irq_type0);
637 reg = ioread32(addr);
638 reg = (reg & ~bit) | type0;
639 iowrite32(reg, addr);
641 addr = bank_reg(gpio, bank, reg_irq_type1);
642 reg = ioread32(addr);
643 reg = (reg & ~bit) | type1;
644 iowrite32(reg, addr);
646 addr = bank_reg(gpio, bank, reg_irq_type2);
647 reg = ioread32(addr);
648 reg = (reg & ~bit) | type2;
649 iowrite32(reg, addr);
652 aspeed_gpio_copro_release(gpio, offset);
653 raw_spin_unlock_irqrestore(&gpio->lock, flags);
655 irq_set_handler_locked(d, handler);
660 static void aspeed_gpio_irq_handler(struct irq_desc *desc)
662 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
663 struct irq_chip *ic = irq_desc_get_chip(desc);
664 struct aspeed_gpio *data = gpiochip_get_data(gc);
665 unsigned int i, p, banks;
667 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
669 chained_irq_enter(ic, desc);
671 banks = DIV_ROUND_UP(gpio->chip.ngpio, 32);
672 for (i = 0; i < banks; i++) {
673 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
675 reg = ioread32(bank_reg(data, bank, reg_irq_status));
677 for_each_set_bit(p, ®, 32)
678 generic_handle_domain_irq(gc->irq.domain, i * 32 + p);
681 chained_irq_exit(ic, desc);
684 static void aspeed_init_irq_valid_mask(struct gpio_chip *gc,
685 unsigned long *valid_mask,
688 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
689 const struct aspeed_bank_props *props = gpio->config->props;
691 while (!is_bank_props_sentinel(props)) {
693 const unsigned long int input = props->input;
695 /* Pretty crummy approach, but similar to GPIO core */
696 for_each_clear_bit(offset, &input, 32) {
697 unsigned int i = props->bank * 32 + offset;
699 if (i >= gpio->chip.ngpio)
702 clear_bit(i, valid_mask);
709 static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip,
710 unsigned int offset, bool enable)
712 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
718 treg = bank_reg(gpio, to_bank(offset), reg_tolerance);
720 raw_spin_lock_irqsave(&gpio->lock, flags);
721 copro = aspeed_gpio_copro_request(gpio, offset);
726 val |= GPIO_BIT(offset);
728 val &= ~GPIO_BIT(offset);
733 aspeed_gpio_copro_release(gpio, offset);
734 raw_spin_unlock_irqrestore(&gpio->lock, flags);
739 static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
741 if (!have_gpio(gpiochip_get_data(chip), offset))
744 return pinctrl_gpio_request(chip->base + offset);
747 static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
749 pinctrl_gpio_free(chip->base + offset);
752 static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs,
759 rate = clk_get_rate(gpio->clk);
764 r = do_div(n, 1000000);
769 /* At least as long as the requested time */
775 /* Call under gpio->lock */
776 static int register_allocated_timer(struct aspeed_gpio *gpio,
777 unsigned int offset, unsigned int timer)
779 if (WARN(gpio->offset_timer[offset] != 0,
780 "Offset %d already allocated timer %d\n",
781 offset, gpio->offset_timer[offset]))
784 if (WARN(gpio->timer_users[timer] == UINT_MAX,
785 "Timer user count would overflow\n"))
788 gpio->offset_timer[offset] = timer;
789 gpio->timer_users[timer]++;
794 /* Call under gpio->lock */
795 static int unregister_allocated_timer(struct aspeed_gpio *gpio,
798 if (WARN(gpio->offset_timer[offset] == 0,
799 "No timer allocated to offset %d\n", offset))
802 if (WARN(gpio->timer_users[gpio->offset_timer[offset]] == 0,
803 "No users recorded for timer %d\n",
804 gpio->offset_timer[offset]))
807 gpio->timer_users[gpio->offset_timer[offset]]--;
808 gpio->offset_timer[offset] = 0;
813 /* Call under gpio->lock */
814 static inline bool timer_allocation_registered(struct aspeed_gpio *gpio,
817 return gpio->offset_timer[offset] > 0;
820 /* Call under gpio->lock */
821 static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset,
824 const struct aspeed_gpio_bank *bank = to_bank(offset);
825 const u32 mask = GPIO_BIT(offset);
829 /* Note: Debounce timer isn't under control of the command
830 * source registers, so no need to sync with the coprocessor
832 addr = bank_reg(gpio, bank, reg_debounce_sel1);
833 val = ioread32(addr);
834 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr);
836 addr = bank_reg(gpio, bank, reg_debounce_sel2);
837 val = ioread32(addr);
838 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr);
841 static int enable_debounce(struct gpio_chip *chip, unsigned int offset,
844 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
845 u32 requested_cycles;
853 rc = usecs_to_cycles(gpio, usecs, &requested_cycles);
855 dev_warn(chip->parent, "Failed to convert %luus to cycles at %luHz: %d\n",
856 usecs, clk_get_rate(gpio->clk), rc);
860 raw_spin_lock_irqsave(&gpio->lock, flags);
862 if (timer_allocation_registered(gpio, offset)) {
863 rc = unregister_allocated_timer(gpio, offset);
868 /* Try to find a timer already configured for the debounce period */
869 for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) {
872 cycles = ioread32(gpio->base + debounce_timers[i]);
873 if (requested_cycles == cycles)
877 if (i == ARRAY_SIZE(debounce_timers)) {
881 * As there are no timers configured for the requested debounce
882 * period, find an unused timer instead
884 for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) {
885 if (gpio->timer_users[j] == 0)
889 if (j == ARRAY_SIZE(gpio->timer_users)) {
890 dev_warn(chip->parent,
891 "Debounce timers exhausted, cannot debounce for period %luus\n",
897 * We already adjusted the accounting to remove @offset
898 * as a user of its previous timer, so also configure
899 * the hardware so @offset has timers disabled for
902 configure_timer(gpio, offset, 0);
908 iowrite32(requested_cycles, gpio->base + debounce_timers[i]);
911 if (WARN(i == 0, "Cannot register index of disabled timer\n")) {
916 register_allocated_timer(gpio, offset, i);
917 configure_timer(gpio, offset, i);
920 raw_spin_unlock_irqrestore(&gpio->lock, flags);
925 static int disable_debounce(struct gpio_chip *chip, unsigned int offset)
927 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
931 raw_spin_lock_irqsave(&gpio->lock, flags);
933 rc = unregister_allocated_timer(gpio, offset);
935 configure_timer(gpio, offset, 0);
937 raw_spin_unlock_irqrestore(&gpio->lock, flags);
942 static int set_debounce(struct gpio_chip *chip, unsigned int offset,
945 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
947 if (!have_debounce(gpio, offset))
951 return enable_debounce(chip, offset, usecs);
953 return disable_debounce(chip, offset);
956 static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
957 unsigned long config)
959 unsigned long param = pinconf_to_config_param(config);
960 u32 arg = pinconf_to_config_argument(config);
962 if (param == PIN_CONFIG_INPUT_DEBOUNCE)
963 return set_debounce(chip, offset, arg);
964 else if (param == PIN_CONFIG_BIAS_DISABLE ||
965 param == PIN_CONFIG_BIAS_PULL_DOWN ||
966 param == PIN_CONFIG_DRIVE_STRENGTH)
967 return pinctrl_gpio_set_config(offset, config);
968 else if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN ||
969 param == PIN_CONFIG_DRIVE_OPEN_SOURCE)
970 /* Return -ENOTSUPP to trigger emulation, as per datasheet */
972 else if (param == PIN_CONFIG_PERSIST_STATE)
973 return aspeed_gpio_reset_tolerance(chip, offset, arg);
979 * aspeed_gpio_copro_set_ops - Sets the callbacks used for handshaking with
980 * the coprocessor for shared GPIO banks
981 * @ops: The callbacks
982 * @data: Pointer passed back to the callbacks
984 int aspeed_gpio_copro_set_ops(const struct aspeed_gpio_copro_ops *ops, void *data)
991 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_set_ops);
994 * aspeed_gpio_copro_grab_gpio - Mark a GPIO used by the coprocessor. The entire
995 * bank gets marked and any access from the ARM will
996 * result in handshaking via callbacks.
997 * @desc: The GPIO to be marked
998 * @vreg_offset: If non-NULL, returns the value register offset in the GPIO space
999 * @dreg_offset: If non-NULL, returns the data latch register offset in the GPIO space
1000 * @bit: If non-NULL, returns the bit number of the GPIO in the registers
1002 int aspeed_gpio_copro_grab_gpio(struct gpio_desc *desc,
1003 u16 *vreg_offset, u16 *dreg_offset, u8 *bit)
1005 struct gpio_chip *chip = gpiod_to_chip(desc);
1006 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
1007 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
1008 const struct aspeed_gpio_bank *bank = to_bank(offset);
1009 unsigned long flags;
1011 if (!gpio->cf_copro_bankmap)
1012 gpio->cf_copro_bankmap = kzalloc(gpio->chip.ngpio >> 3, GFP_KERNEL);
1013 if (!gpio->cf_copro_bankmap)
1015 if (offset < 0 || offset > gpio->chip.ngpio)
1017 bindex = offset >> 3;
1019 raw_spin_lock_irqsave(&gpio->lock, flags);
1021 /* Sanity check, this shouldn't happen */
1022 if (gpio->cf_copro_bankmap[bindex] == 0xff) {
1026 gpio->cf_copro_bankmap[bindex]++;
1028 /* Switch command source */
1029 if (gpio->cf_copro_bankmap[bindex] == 1)
1030 aspeed_gpio_change_cmd_source(gpio, bank, bindex,
1031 GPIO_CMDSRC_COLDFIRE);
1034 *vreg_offset = bank->val_regs;
1036 *dreg_offset = bank->rdata_reg;
1038 *bit = GPIO_OFFSET(offset);
1040 raw_spin_unlock_irqrestore(&gpio->lock, flags);
1043 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_grab_gpio);
1046 * aspeed_gpio_copro_release_gpio - Unmark a GPIO used by the coprocessor.
1047 * @desc: The GPIO to be marked
1049 int aspeed_gpio_copro_release_gpio(struct gpio_desc *desc)
1051 struct gpio_chip *chip = gpiod_to_chip(desc);
1052 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
1053 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
1054 const struct aspeed_gpio_bank *bank = to_bank(offset);
1055 unsigned long flags;
1057 if (!gpio->cf_copro_bankmap)
1060 if (offset < 0 || offset > gpio->chip.ngpio)
1062 bindex = offset >> 3;
1064 raw_spin_lock_irqsave(&gpio->lock, flags);
1066 /* Sanity check, this shouldn't happen */
1067 if (gpio->cf_copro_bankmap[bindex] == 0) {
1071 gpio->cf_copro_bankmap[bindex]--;
1073 /* Switch command source */
1074 if (gpio->cf_copro_bankmap[bindex] == 0)
1075 aspeed_gpio_change_cmd_source(gpio, bank, bindex,
1078 raw_spin_unlock_irqrestore(&gpio->lock, flags);
1081 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_release_gpio);
1084 * Any banks not specified in a struct aspeed_bank_props array are assumed to
1085 * have the properties:
1087 * { .input = 0xffffffff, .output = 0xffffffff }
1090 static const struct aspeed_bank_props ast2400_bank_props[] = {
1092 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1093 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
1097 static const struct aspeed_gpio_config ast2400_config =
1098 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
1099 { .nr_gpios = 220, .props = ast2400_bank_props, };
1101 static const struct aspeed_bank_props ast2500_bank_props[] = {
1103 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1104 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
1105 { 7, 0x000000ff, 0x000000ff }, /* AC */
1109 static const struct aspeed_gpio_config ast2500_config =
1110 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
1111 { .nr_gpios = 232, .props = ast2500_bank_props, };
1113 static const struct aspeed_bank_props ast2600_bank_props[] = {
1115 {4, 0xffffffff, 0x00ffffff}, /* Q/R/S/T */
1116 {5, 0xffffffff, 0xffffff00}, /* U/V/W/X */
1117 {6, 0x0000ffff, 0x0000ffff}, /* Y/Z */
1121 static const struct aspeed_gpio_config ast2600_config =
1123 * ast2600 has two controllers one with 208 GPIOs and one with 36 GPIOs.
1124 * We expect ngpio being set in the device tree and this is a fallback
1127 { .nr_gpios = 208, .props = ast2600_bank_props, };
1129 static const struct of_device_id aspeed_gpio_of_table[] = {
1130 { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
1131 { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
1132 { .compatible = "aspeed,ast2600-gpio", .data = &ast2600_config, },
1135 MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
1137 static int __init aspeed_gpio_probe(struct platform_device *pdev)
1139 const struct of_device_id *gpio_id;
1140 struct aspeed_gpio *gpio;
1141 int rc, i, banks, err;
1144 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
1148 gpio->base = devm_platform_ioremap_resource(pdev, 0);
1149 if (IS_ERR(gpio->base))
1150 return PTR_ERR(gpio->base);
1152 raw_spin_lock_init(&gpio->lock);
1154 gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node);
1158 gpio->clk = of_clk_get(pdev->dev.of_node, 0);
1159 if (IS_ERR(gpio->clk)) {
1160 dev_warn(&pdev->dev,
1161 "Failed to get clock from devicetree, debouncing disabled\n");
1165 gpio->config = gpio_id->data;
1167 gpio->chip.parent = &pdev->dev;
1168 err = of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpio);
1169 gpio->chip.ngpio = (u16) ngpio;
1171 gpio->chip.ngpio = gpio->config->nr_gpios;
1172 gpio->chip.direction_input = aspeed_gpio_dir_in;
1173 gpio->chip.direction_output = aspeed_gpio_dir_out;
1174 gpio->chip.get_direction = aspeed_gpio_get_direction;
1175 gpio->chip.request = aspeed_gpio_request;
1176 gpio->chip.free = aspeed_gpio_free;
1177 gpio->chip.get = aspeed_gpio_get;
1178 gpio->chip.set = aspeed_gpio_set;
1179 gpio->chip.set_config = aspeed_gpio_set_config;
1180 gpio->chip.label = dev_name(&pdev->dev);
1181 gpio->chip.base = -1;
1183 /* Allocate a cache of the output registers */
1184 banks = DIV_ROUND_UP(gpio->chip.ngpio, 32);
1185 gpio->dcache = devm_kcalloc(&pdev->dev,
1186 banks, sizeof(u32), GFP_KERNEL);
1191 * Populate it with initial values read from the HW and switch
1192 * all command sources to the ARM by default
1194 for (i = 0; i < banks; i++) {
1195 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
1196 void __iomem *addr = bank_reg(gpio, bank, reg_rdata);
1197 gpio->dcache[i] = ioread32(addr);
1198 aspeed_gpio_change_cmd_source(gpio, bank, 0, GPIO_CMDSRC_ARM);
1199 aspeed_gpio_change_cmd_source(gpio, bank, 1, GPIO_CMDSRC_ARM);
1200 aspeed_gpio_change_cmd_source(gpio, bank, 2, GPIO_CMDSRC_ARM);
1201 aspeed_gpio_change_cmd_source(gpio, bank, 3, GPIO_CMDSRC_ARM);
1204 /* Optionally set up an irqchip if there is an IRQ */
1205 rc = platform_get_irq(pdev, 0);
1207 struct gpio_irq_chip *girq;
1210 girq = &gpio->chip.irq;
1211 girq->chip = &gpio->irqc;
1212 girq->chip->name = dev_name(&pdev->dev);
1213 girq->chip->irq_ack = aspeed_gpio_irq_ack;
1214 girq->chip->irq_mask = aspeed_gpio_irq_mask;
1215 girq->chip->irq_unmask = aspeed_gpio_irq_unmask;
1216 girq->chip->irq_set_type = aspeed_gpio_set_type;
1217 girq->parent_handler = aspeed_gpio_irq_handler;
1218 girq->num_parents = 1;
1219 girq->parents = devm_kcalloc(&pdev->dev, 1,
1220 sizeof(*girq->parents),
1224 girq->parents[0] = gpio->irq;
1225 girq->default_type = IRQ_TYPE_NONE;
1226 girq->handler = handle_bad_irq;
1227 girq->init_valid_mask = aspeed_init_irq_valid_mask;
1230 gpio->offset_timer =
1231 devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL);
1232 if (!gpio->offset_timer)
1235 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
1242 static struct platform_driver aspeed_gpio_driver = {
1244 .name = KBUILD_MODNAME,
1245 .of_match_table = aspeed_gpio_of_table,
1249 module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
1251 MODULE_DESCRIPTION("Aspeed GPIO Driver");
1252 MODULE_LICENSE("GPL");