1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2015 IBM Corp.
5 * Joel Stanley <joel@jms.id.au>
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/seq_file.h>
19 #include <linux/spinlock.h>
20 #include <linux/string.h>
22 #include <asm/div64.h>
25 * These two headers aren't meant to be used by GPIO drivers. We need
26 * them in order to access gpio_chip_hwgpio() which we need to implement
27 * the aspeed specific API which allows the coprocessor to request
28 * access to some GPIOs and to arbitrate between coprocessor and ARM.
30 #include <linux/gpio/consumer.h>
33 struct aspeed_bank_props {
39 struct aspeed_gpio_config {
40 unsigned int nr_gpios;
41 const struct aspeed_bank_props *props;
45 * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled
46 * @timer_users: Tracks the number of users for each timer
48 * The @timer_users has four elements but the first element is unused. This is
49 * to simplify accounting and indexing, as a zero value in @offset_timer
50 * represents disabled debouncing for the GPIO. Any other value for an element
51 * of @offset_timer is used as an index into @timer_users. This behaviour of
52 * the zero value aligns with the behaviour of zero built from the timer
53 * configuration registers (i.e. debouncing is disabled).
56 struct gpio_chip chip;
61 const struct aspeed_gpio_config *config;
64 unsigned int timer_users[4];
71 struct aspeed_gpio_bank {
72 uint16_t val_regs; /* +0: Rd: read input value, Wr: set write latch
73 * +4: Rd/Wr: Direction (0=in, 1=out)
75 uint16_t rdata_reg; /* Rd: read write latch, Wr: <none> */
77 uint16_t debounce_regs;
78 uint16_t tolerance_regs;
80 const char names[4][3];
84 * Note: The "value" register returns the input value sampled on the
85 * line even when the GPIO is configured as an output. Since
86 * that input goes through synchronizers, writing, then reading
87 * back may not return the written value right away.
89 * The "rdata" register returns the content of the write latch
90 * and thus can be used to read back what was last written
94 static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 };
96 static const struct aspeed_gpio_copro_ops *copro_ops;
97 static void *copro_data;
99 static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
104 .debounce_regs = 0x0040,
105 .tolerance_regs = 0x001c,
106 .cmdsrc_regs = 0x0060,
107 .names = { "A", "B", "C", "D" },
113 .debounce_regs = 0x0048,
114 .tolerance_regs = 0x003c,
115 .cmdsrc_regs = 0x0068,
116 .names = { "E", "F", "G", "H" },
122 .debounce_regs = 0x00b0,
123 .tolerance_regs = 0x00ac,
124 .cmdsrc_regs = 0x0090,
125 .names = { "I", "J", "K", "L" },
131 .debounce_regs = 0x0100,
132 .tolerance_regs = 0x00fc,
133 .cmdsrc_regs = 0x00e0,
134 .names = { "M", "N", "O", "P" },
140 .debounce_regs = 0x0130,
141 .tolerance_regs = 0x012c,
142 .cmdsrc_regs = 0x0110,
143 .names = { "Q", "R", "S", "T" },
149 .debounce_regs = 0x0160,
150 .tolerance_regs = 0x015c,
151 .cmdsrc_regs = 0x0140,
152 .names = { "U", "V", "W", "X" },
158 .debounce_regs = 0x0190,
159 .tolerance_regs = 0x018c,
160 .cmdsrc_regs = 0x0170,
161 .names = { "Y", "Z", "AA", "AB" },
167 .debounce_regs = 0x01c0,
168 .tolerance_regs = 0x01bc,
169 .cmdsrc_regs = 0x01a0,
170 .names = { "AC", "", "", "" },
174 enum aspeed_gpio_reg {
190 #define GPIO_VAL_VALUE 0x00
191 #define GPIO_VAL_DIR 0x04
193 #define GPIO_IRQ_ENABLE 0x00
194 #define GPIO_IRQ_TYPE0 0x04
195 #define GPIO_IRQ_TYPE1 0x08
196 #define GPIO_IRQ_TYPE2 0x0c
197 #define GPIO_IRQ_STATUS 0x10
199 #define GPIO_DEBOUNCE_SEL1 0x00
200 #define GPIO_DEBOUNCE_SEL2 0x04
202 #define GPIO_CMDSRC_0 0x00
203 #define GPIO_CMDSRC_1 0x04
204 #define GPIO_CMDSRC_ARM 0
205 #define GPIO_CMDSRC_LPC 1
206 #define GPIO_CMDSRC_COLDFIRE 2
207 #define GPIO_CMDSRC_RESERVED 3
209 /* This will be resolved at compile time */
210 static inline void __iomem *bank_reg(struct aspeed_gpio *gpio,
211 const struct aspeed_gpio_bank *bank,
212 const enum aspeed_gpio_reg reg)
216 return gpio->base + bank->val_regs + GPIO_VAL_VALUE;
218 return gpio->base + bank->rdata_reg;
220 return gpio->base + bank->val_regs + GPIO_VAL_DIR;
222 return gpio->base + bank->irq_regs + GPIO_IRQ_ENABLE;
224 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE0;
226 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE1;
228 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE2;
230 return gpio->base + bank->irq_regs + GPIO_IRQ_STATUS;
231 case reg_debounce_sel1:
232 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL1;
233 case reg_debounce_sel2:
234 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL2;
236 return gpio->base + bank->tolerance_regs;
238 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_0;
240 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_1;
245 #define GPIO_BANK(x) ((x) >> 5)
246 #define GPIO_OFFSET(x) ((x) & 0x1f)
247 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
249 #define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
250 #define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
251 #define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
253 static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
255 unsigned int bank = GPIO_BANK(offset);
257 WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks));
258 return &aspeed_gpio_banks[bank];
261 static inline bool is_bank_props_sentinel(const struct aspeed_bank_props *props)
263 return !(props->input || props->output);
266 static inline const struct aspeed_bank_props *find_bank_props(
267 struct aspeed_gpio *gpio, unsigned int offset)
269 const struct aspeed_bank_props *props = gpio->config->props;
271 while (!is_bank_props_sentinel(props)) {
272 if (props->bank == GPIO_BANK(offset))
280 static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset)
282 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
283 const struct aspeed_gpio_bank *bank = to_bank(offset);
284 unsigned int group = GPIO_OFFSET(offset) / 8;
286 return bank->names[group][0] != '\0' &&
287 (!props || ((props->input | props->output) & GPIO_BIT(offset)));
290 static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset)
292 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
294 return !props || (props->input & GPIO_BIT(offset));
297 #define have_irq(g, o) have_input((g), (o))
298 #define have_debounce(g, o) have_input((g), (o))
300 static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset)
302 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
304 return !props || (props->output & GPIO_BIT(offset));
307 static void aspeed_gpio_change_cmd_source(struct aspeed_gpio *gpio,
308 const struct aspeed_gpio_bank *bank,
309 int bindex, int cmdsrc)
311 void __iomem *c0 = bank_reg(gpio, bank, reg_cmdsrc0);
312 void __iomem *c1 = bank_reg(gpio, bank, reg_cmdsrc1);
316 * Each register controls 4 banks, so take the bottom 2
317 * bits of the bank index, and use them to select the
318 * right control bit (0, 8, 16 or 24).
320 bit = BIT((bindex & 3) << 3);
322 /* Source 1 first to avoid illegal 11 combination */
339 static bool aspeed_gpio_copro_request(struct aspeed_gpio *gpio,
342 const struct aspeed_gpio_bank *bank = to_bank(offset);
344 if (!copro_ops || !gpio->cf_copro_bankmap)
346 if (!gpio->cf_copro_bankmap[offset >> 3])
348 if (!copro_ops->request_access)
351 /* Pause the coprocessor */
352 copro_ops->request_access(copro_data);
354 /* Change command source back to ARM */
355 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3, GPIO_CMDSRC_ARM);
358 gpio->dcache[GPIO_BANK(offset)] = ioread32(bank_reg(gpio, bank, reg_rdata));
363 static void aspeed_gpio_copro_release(struct aspeed_gpio *gpio,
366 const struct aspeed_gpio_bank *bank = to_bank(offset);
368 if (!copro_ops || !gpio->cf_copro_bankmap)
370 if (!gpio->cf_copro_bankmap[offset >> 3])
372 if (!copro_ops->release_access)
375 /* Change command source back to ColdFire */
376 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3,
377 GPIO_CMDSRC_COLDFIRE);
379 /* Restart the coprocessor */
380 copro_ops->release_access(copro_data);
383 static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
385 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
386 const struct aspeed_gpio_bank *bank = to_bank(offset);
388 return !!(ioread32(bank_reg(gpio, bank, reg_val)) & GPIO_BIT(offset));
391 static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
394 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
395 const struct aspeed_gpio_bank *bank = to_bank(offset);
399 addr = bank_reg(gpio, bank, reg_val);
400 reg = gpio->dcache[GPIO_BANK(offset)];
403 reg |= GPIO_BIT(offset);
405 reg &= ~GPIO_BIT(offset);
406 gpio->dcache[GPIO_BANK(offset)] = reg;
408 iowrite32(reg, addr);
411 static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
414 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
418 raw_spin_lock_irqsave(&gpio->lock, flags);
419 copro = aspeed_gpio_copro_request(gpio, offset);
421 __aspeed_gpio_set(gc, offset, val);
424 aspeed_gpio_copro_release(gpio, offset);
425 raw_spin_unlock_irqrestore(&gpio->lock, flags);
428 static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
430 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
431 const struct aspeed_gpio_bank *bank = to_bank(offset);
432 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
437 if (!have_input(gpio, offset))
440 raw_spin_lock_irqsave(&gpio->lock, flags);
442 reg = ioread32(addr);
443 reg &= ~GPIO_BIT(offset);
445 copro = aspeed_gpio_copro_request(gpio, offset);
446 iowrite32(reg, addr);
448 aspeed_gpio_copro_release(gpio, offset);
450 raw_spin_unlock_irqrestore(&gpio->lock, flags);
455 static int aspeed_gpio_dir_out(struct gpio_chip *gc,
456 unsigned int offset, int val)
458 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
459 const struct aspeed_gpio_bank *bank = to_bank(offset);
460 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
465 if (!have_output(gpio, offset))
468 raw_spin_lock_irqsave(&gpio->lock, flags);
470 reg = ioread32(addr);
471 reg |= GPIO_BIT(offset);
473 copro = aspeed_gpio_copro_request(gpio, offset);
474 __aspeed_gpio_set(gc, offset, val);
475 iowrite32(reg, addr);
478 aspeed_gpio_copro_release(gpio, offset);
479 raw_spin_unlock_irqrestore(&gpio->lock, flags);
484 static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
486 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
487 const struct aspeed_gpio_bank *bank = to_bank(offset);
491 if (!have_input(gpio, offset))
492 return GPIO_LINE_DIRECTION_OUT;
494 if (!have_output(gpio, offset))
495 return GPIO_LINE_DIRECTION_IN;
497 raw_spin_lock_irqsave(&gpio->lock, flags);
499 val = ioread32(bank_reg(gpio, bank, reg_dir)) & GPIO_BIT(offset);
501 raw_spin_unlock_irqrestore(&gpio->lock, flags);
503 return val ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
506 static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
507 struct aspeed_gpio **gpio,
508 const struct aspeed_gpio_bank **bank,
509 u32 *bit, int *offset)
511 struct aspeed_gpio *internal;
513 *offset = irqd_to_hwirq(d);
515 internal = irq_data_get_irq_chip_data(d);
517 /* This might be a bit of a questionable place to check */
518 if (!have_irq(internal, *offset))
522 *bank = to_bank(*offset);
523 *bit = GPIO_BIT(*offset);
528 static void aspeed_gpio_irq_ack(struct irq_data *d)
530 const struct aspeed_gpio_bank *bank;
531 struct aspeed_gpio *gpio;
533 void __iomem *status_addr;
538 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
542 status_addr = bank_reg(gpio, bank, reg_irq_status);
544 raw_spin_lock_irqsave(&gpio->lock, flags);
545 copro = aspeed_gpio_copro_request(gpio, offset);
547 iowrite32(bit, status_addr);
550 aspeed_gpio_copro_release(gpio, offset);
551 raw_spin_unlock_irqrestore(&gpio->lock, flags);
554 static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
556 const struct aspeed_gpio_bank *bank;
557 struct aspeed_gpio *gpio;
564 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
568 addr = bank_reg(gpio, bank, reg_irq_enable);
570 /* Unmasking the IRQ */
572 gpiochip_enable_irq(&gpio->chip, irqd_to_hwirq(d));
574 raw_spin_lock_irqsave(&gpio->lock, flags);
575 copro = aspeed_gpio_copro_request(gpio, offset);
577 reg = ioread32(addr);
582 iowrite32(reg, addr);
585 aspeed_gpio_copro_release(gpio, offset);
586 raw_spin_unlock_irqrestore(&gpio->lock, flags);
588 /* Masking the IRQ */
590 gpiochip_disable_irq(&gpio->chip, irqd_to_hwirq(d));
593 static void aspeed_gpio_irq_mask(struct irq_data *d)
595 aspeed_gpio_irq_set_mask(d, false);
598 static void aspeed_gpio_irq_unmask(struct irq_data *d)
600 aspeed_gpio_irq_set_mask(d, true);
603 static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
609 const struct aspeed_gpio_bank *bank;
610 irq_flow_handler_t handler;
611 struct aspeed_gpio *gpio;
617 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
621 switch (type & IRQ_TYPE_SENSE_MASK) {
622 case IRQ_TYPE_EDGE_BOTH:
625 case IRQ_TYPE_EDGE_RISING:
628 case IRQ_TYPE_EDGE_FALLING:
629 handler = handle_edge_irq;
631 case IRQ_TYPE_LEVEL_HIGH:
634 case IRQ_TYPE_LEVEL_LOW:
636 handler = handle_level_irq;
642 raw_spin_lock_irqsave(&gpio->lock, flags);
643 copro = aspeed_gpio_copro_request(gpio, offset);
645 addr = bank_reg(gpio, bank, reg_irq_type0);
646 reg = ioread32(addr);
647 reg = (reg & ~bit) | type0;
648 iowrite32(reg, addr);
650 addr = bank_reg(gpio, bank, reg_irq_type1);
651 reg = ioread32(addr);
652 reg = (reg & ~bit) | type1;
653 iowrite32(reg, addr);
655 addr = bank_reg(gpio, bank, reg_irq_type2);
656 reg = ioread32(addr);
657 reg = (reg & ~bit) | type2;
658 iowrite32(reg, addr);
661 aspeed_gpio_copro_release(gpio, offset);
662 raw_spin_unlock_irqrestore(&gpio->lock, flags);
664 irq_set_handler_locked(d, handler);
669 static void aspeed_gpio_irq_handler(struct irq_desc *desc)
671 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
672 struct irq_chip *ic = irq_desc_get_chip(desc);
673 struct aspeed_gpio *data = gpiochip_get_data(gc);
674 unsigned int i, p, banks;
676 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
678 chained_irq_enter(ic, desc);
680 banks = DIV_ROUND_UP(gpio->chip.ngpio, 32);
681 for (i = 0; i < banks; i++) {
682 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
684 reg = ioread32(bank_reg(data, bank, reg_irq_status));
686 for_each_set_bit(p, ®, 32)
687 generic_handle_domain_irq(gc->irq.domain, i * 32 + p);
690 chained_irq_exit(ic, desc);
693 static void aspeed_init_irq_valid_mask(struct gpio_chip *gc,
694 unsigned long *valid_mask,
697 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
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->chip.ngpio)
711 clear_bit(i, valid_mask);
718 static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip,
719 unsigned int offset, bool enable)
721 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
727 treg = bank_reg(gpio, to_bank(offset), reg_tolerance);
729 raw_spin_lock_irqsave(&gpio->lock, flags);
730 copro = aspeed_gpio_copro_request(gpio, offset);
735 val |= GPIO_BIT(offset);
737 val &= ~GPIO_BIT(offset);
742 aspeed_gpio_copro_release(gpio, offset);
743 raw_spin_unlock_irqrestore(&gpio->lock, flags);
748 static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
750 if (!have_gpio(gpiochip_get_data(chip), offset))
753 return pinctrl_gpio_request(chip->base + offset);
756 static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
758 pinctrl_gpio_free(chip->base + offset);
761 static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs,
768 rate = clk_get_rate(gpio->clk);
773 r = do_div(n, 1000000);
778 /* At least as long as the requested time */
784 /* Call under gpio->lock */
785 static int register_allocated_timer(struct aspeed_gpio *gpio,
786 unsigned int offset, unsigned int timer)
788 if (WARN(gpio->offset_timer[offset] != 0,
789 "Offset %d already allocated timer %d\n",
790 offset, gpio->offset_timer[offset]))
793 if (WARN(gpio->timer_users[timer] == UINT_MAX,
794 "Timer user count would overflow\n"))
797 gpio->offset_timer[offset] = timer;
798 gpio->timer_users[timer]++;
803 /* Call under gpio->lock */
804 static int unregister_allocated_timer(struct aspeed_gpio *gpio,
807 if (WARN(gpio->offset_timer[offset] == 0,
808 "No timer allocated to offset %d\n", offset))
811 if (WARN(gpio->timer_users[gpio->offset_timer[offset]] == 0,
812 "No users recorded for timer %d\n",
813 gpio->offset_timer[offset]))
816 gpio->timer_users[gpio->offset_timer[offset]]--;
817 gpio->offset_timer[offset] = 0;
822 /* Call under gpio->lock */
823 static inline bool timer_allocation_registered(struct aspeed_gpio *gpio,
826 return gpio->offset_timer[offset] > 0;
829 /* Call under gpio->lock */
830 static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset,
833 const struct aspeed_gpio_bank *bank = to_bank(offset);
834 const u32 mask = GPIO_BIT(offset);
838 /* Note: Debounce timer isn't under control of the command
839 * source registers, so no need to sync with the coprocessor
841 addr = bank_reg(gpio, bank, reg_debounce_sel1);
842 val = ioread32(addr);
843 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr);
845 addr = bank_reg(gpio, bank, reg_debounce_sel2);
846 val = ioread32(addr);
847 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr);
850 static int enable_debounce(struct gpio_chip *chip, unsigned int offset,
853 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
854 u32 requested_cycles;
862 rc = usecs_to_cycles(gpio, usecs, &requested_cycles);
864 dev_warn(chip->parent, "Failed to convert %luus to cycles at %luHz: %d\n",
865 usecs, clk_get_rate(gpio->clk), rc);
869 raw_spin_lock_irqsave(&gpio->lock, flags);
871 if (timer_allocation_registered(gpio, offset)) {
872 rc = unregister_allocated_timer(gpio, offset);
877 /* Try to find a timer already configured for the debounce period */
878 for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) {
881 cycles = ioread32(gpio->base + debounce_timers[i]);
882 if (requested_cycles == cycles)
886 if (i == ARRAY_SIZE(debounce_timers)) {
890 * As there are no timers configured for the requested debounce
891 * period, find an unused timer instead
893 for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) {
894 if (gpio->timer_users[j] == 0)
898 if (j == ARRAY_SIZE(gpio->timer_users)) {
899 dev_warn(chip->parent,
900 "Debounce timers exhausted, cannot debounce for period %luus\n",
906 * We already adjusted the accounting to remove @offset
907 * as a user of its previous timer, so also configure
908 * the hardware so @offset has timers disabled for
911 configure_timer(gpio, offset, 0);
917 iowrite32(requested_cycles, gpio->base + debounce_timers[i]);
920 if (WARN(i == 0, "Cannot register index of disabled timer\n")) {
925 register_allocated_timer(gpio, offset, i);
926 configure_timer(gpio, offset, i);
929 raw_spin_unlock_irqrestore(&gpio->lock, flags);
934 static int disable_debounce(struct gpio_chip *chip, unsigned int offset)
936 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
940 raw_spin_lock_irqsave(&gpio->lock, flags);
942 rc = unregister_allocated_timer(gpio, offset);
944 configure_timer(gpio, offset, 0);
946 raw_spin_unlock_irqrestore(&gpio->lock, flags);
951 static int set_debounce(struct gpio_chip *chip, unsigned int offset,
954 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
956 if (!have_debounce(gpio, offset))
960 return enable_debounce(chip, offset, usecs);
962 return disable_debounce(chip, offset);
965 static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
966 unsigned long config)
968 unsigned long param = pinconf_to_config_param(config);
969 u32 arg = pinconf_to_config_argument(config);
971 if (param == PIN_CONFIG_INPUT_DEBOUNCE)
972 return set_debounce(chip, offset, arg);
973 else if (param == PIN_CONFIG_BIAS_DISABLE ||
974 param == PIN_CONFIG_BIAS_PULL_DOWN ||
975 param == PIN_CONFIG_DRIVE_STRENGTH)
976 return pinctrl_gpio_set_config(chip->base + offset, config);
977 else if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN ||
978 param == PIN_CONFIG_DRIVE_OPEN_SOURCE)
979 /* Return -ENOTSUPP to trigger emulation, as per datasheet */
981 else if (param == PIN_CONFIG_PERSIST_STATE)
982 return aspeed_gpio_reset_tolerance(chip, offset, arg);
988 * aspeed_gpio_copro_set_ops - Sets the callbacks used for handshaking with
989 * the coprocessor for shared GPIO banks
990 * @ops: The callbacks
991 * @data: Pointer passed back to the callbacks
993 int aspeed_gpio_copro_set_ops(const struct aspeed_gpio_copro_ops *ops, void *data)
1000 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_set_ops);
1003 * aspeed_gpio_copro_grab_gpio - Mark a GPIO used by the coprocessor. The entire
1004 * bank gets marked and any access from the ARM will
1005 * result in handshaking via callbacks.
1006 * @desc: The GPIO to be marked
1007 * @vreg_offset: If non-NULL, returns the value register offset in the GPIO space
1008 * @dreg_offset: If non-NULL, returns the data latch register offset in the GPIO space
1009 * @bit: If non-NULL, returns the bit number of the GPIO in the registers
1011 int aspeed_gpio_copro_grab_gpio(struct gpio_desc *desc,
1012 u16 *vreg_offset, u16 *dreg_offset, u8 *bit)
1014 struct gpio_chip *chip = gpiod_to_chip(desc);
1015 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
1016 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
1017 const struct aspeed_gpio_bank *bank = to_bank(offset);
1018 unsigned long flags;
1020 if (!gpio->cf_copro_bankmap)
1021 gpio->cf_copro_bankmap = kzalloc(gpio->chip.ngpio >> 3, GFP_KERNEL);
1022 if (!gpio->cf_copro_bankmap)
1024 if (offset < 0 || offset > gpio->chip.ngpio)
1026 bindex = offset >> 3;
1028 raw_spin_lock_irqsave(&gpio->lock, flags);
1030 /* Sanity check, this shouldn't happen */
1031 if (gpio->cf_copro_bankmap[bindex] == 0xff) {
1035 gpio->cf_copro_bankmap[bindex]++;
1037 /* Switch command source */
1038 if (gpio->cf_copro_bankmap[bindex] == 1)
1039 aspeed_gpio_change_cmd_source(gpio, bank, bindex,
1040 GPIO_CMDSRC_COLDFIRE);
1043 *vreg_offset = bank->val_regs;
1045 *dreg_offset = bank->rdata_reg;
1047 *bit = GPIO_OFFSET(offset);
1049 raw_spin_unlock_irqrestore(&gpio->lock, flags);
1052 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_grab_gpio);
1055 * aspeed_gpio_copro_release_gpio - Unmark a GPIO used by the coprocessor.
1056 * @desc: The GPIO to be marked
1058 int aspeed_gpio_copro_release_gpio(struct gpio_desc *desc)
1060 struct gpio_chip *chip = gpiod_to_chip(desc);
1061 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
1062 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
1063 const struct aspeed_gpio_bank *bank = to_bank(offset);
1064 unsigned long flags;
1066 if (!gpio->cf_copro_bankmap)
1069 if (offset < 0 || offset > gpio->chip.ngpio)
1071 bindex = offset >> 3;
1073 raw_spin_lock_irqsave(&gpio->lock, flags);
1075 /* Sanity check, this shouldn't happen */
1076 if (gpio->cf_copro_bankmap[bindex] == 0) {
1080 gpio->cf_copro_bankmap[bindex]--;
1082 /* Switch command source */
1083 if (gpio->cf_copro_bankmap[bindex] == 0)
1084 aspeed_gpio_change_cmd_source(gpio, bank, bindex,
1087 raw_spin_unlock_irqrestore(&gpio->lock, flags);
1090 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_release_gpio);
1092 static void aspeed_gpio_irq_print_chip(struct irq_data *d, struct seq_file *p)
1094 const struct aspeed_gpio_bank *bank;
1095 struct aspeed_gpio *gpio;
1099 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
1103 seq_printf(p, dev_name(gpio->dev));
1106 static const struct irq_chip aspeed_gpio_irq_chip = {
1107 .irq_ack = aspeed_gpio_irq_ack,
1108 .irq_mask = aspeed_gpio_irq_mask,
1109 .irq_unmask = aspeed_gpio_irq_unmask,
1110 .irq_set_type = aspeed_gpio_set_type,
1111 .irq_print_chip = aspeed_gpio_irq_print_chip,
1112 .flags = IRQCHIP_IMMUTABLE,
1113 GPIOCHIP_IRQ_RESOURCE_HELPERS,
1117 * Any banks not specified in a struct aspeed_bank_props array are assumed to
1118 * have the properties:
1120 * { .input = 0xffffffff, .output = 0xffffffff }
1123 static const struct aspeed_bank_props ast2400_bank_props[] = {
1125 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1126 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
1130 static const struct aspeed_gpio_config ast2400_config =
1131 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
1132 { .nr_gpios = 220, .props = ast2400_bank_props, };
1134 static const struct aspeed_bank_props ast2500_bank_props[] = {
1136 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1137 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
1138 { 7, 0x000000ff, 0x000000ff }, /* AC */
1142 static const struct aspeed_gpio_config ast2500_config =
1143 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
1144 { .nr_gpios = 232, .props = ast2500_bank_props, };
1146 static const struct aspeed_bank_props ast2600_bank_props[] = {
1148 {4, 0xffffffff, 0x00ffffff}, /* Q/R/S/T */
1149 {5, 0xffffffff, 0xffffff00}, /* U/V/W/X */
1150 {6, 0x0000ffff, 0x0000ffff}, /* Y/Z */
1154 static const struct aspeed_gpio_config ast2600_config =
1156 * ast2600 has two controllers one with 208 GPIOs and one with 36 GPIOs.
1157 * We expect ngpio being set in the device tree and this is a fallback
1160 { .nr_gpios = 208, .props = ast2600_bank_props, };
1162 static const struct of_device_id aspeed_gpio_of_table[] = {
1163 { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
1164 { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
1165 { .compatible = "aspeed,ast2600-gpio", .data = &ast2600_config, },
1168 MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
1170 static int __init aspeed_gpio_probe(struct platform_device *pdev)
1172 const struct of_device_id *gpio_id;
1173 struct gpio_irq_chip *girq;
1174 struct aspeed_gpio *gpio;
1175 int rc, irq, i, banks, err;
1178 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
1182 gpio->base = devm_platform_ioremap_resource(pdev, 0);
1183 if (IS_ERR(gpio->base))
1184 return PTR_ERR(gpio->base);
1186 gpio->dev = &pdev->dev;
1188 raw_spin_lock_init(&gpio->lock);
1190 gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node);
1194 gpio->clk = of_clk_get(pdev->dev.of_node, 0);
1195 if (IS_ERR(gpio->clk)) {
1196 dev_warn(&pdev->dev,
1197 "Failed to get clock from devicetree, debouncing disabled\n");
1201 gpio->config = gpio_id->data;
1203 gpio->chip.parent = &pdev->dev;
1204 err = of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpio);
1205 gpio->chip.ngpio = (u16) ngpio;
1207 gpio->chip.ngpio = gpio->config->nr_gpios;
1208 gpio->chip.direction_input = aspeed_gpio_dir_in;
1209 gpio->chip.direction_output = aspeed_gpio_dir_out;
1210 gpio->chip.get_direction = aspeed_gpio_get_direction;
1211 gpio->chip.request = aspeed_gpio_request;
1212 gpio->chip.free = aspeed_gpio_free;
1213 gpio->chip.get = aspeed_gpio_get;
1214 gpio->chip.set = aspeed_gpio_set;
1215 gpio->chip.set_config = aspeed_gpio_set_config;
1216 gpio->chip.label = dev_name(&pdev->dev);
1217 gpio->chip.base = -1;
1219 /* Allocate a cache of the output registers */
1220 banks = DIV_ROUND_UP(gpio->chip.ngpio, 32);
1221 gpio->dcache = devm_kcalloc(&pdev->dev,
1222 banks, sizeof(u32), GFP_KERNEL);
1227 * Populate it with initial values read from the HW and switch
1228 * all command sources to the ARM by default
1230 for (i = 0; i < banks; i++) {
1231 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
1232 void __iomem *addr = bank_reg(gpio, bank, reg_rdata);
1233 gpio->dcache[i] = ioread32(addr);
1234 aspeed_gpio_change_cmd_source(gpio, bank, 0, GPIO_CMDSRC_ARM);
1235 aspeed_gpio_change_cmd_source(gpio, bank, 1, GPIO_CMDSRC_ARM);
1236 aspeed_gpio_change_cmd_source(gpio, bank, 2, GPIO_CMDSRC_ARM);
1237 aspeed_gpio_change_cmd_source(gpio, bank, 3, GPIO_CMDSRC_ARM);
1240 /* Set up an irqchip */
1241 irq = platform_get_irq(pdev, 0);
1245 girq = &gpio->chip.irq;
1246 gpio_irq_chip_set_chip(girq, &aspeed_gpio_irq_chip);
1248 girq->parent_handler = aspeed_gpio_irq_handler;
1249 girq->num_parents = 1;
1250 girq->parents = devm_kcalloc(&pdev->dev, 1, sizeof(*girq->parents), GFP_KERNEL);
1253 girq->parents[0] = gpio->irq;
1254 girq->default_type = IRQ_TYPE_NONE;
1255 girq->handler = handle_bad_irq;
1256 girq->init_valid_mask = aspeed_init_irq_valid_mask;
1258 gpio->offset_timer =
1259 devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL);
1260 if (!gpio->offset_timer)
1263 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
1270 static struct platform_driver aspeed_gpio_driver = {
1272 .name = KBUILD_MODNAME,
1273 .of_match_table = aspeed_gpio_of_table,
1277 module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
1279 MODULE_DESCRIPTION("Aspeed GPIO Driver");
1280 MODULE_LICENSE("GPL");