1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2015 IBM Corp.
10 #include <linux/gpio/driver.h>
11 #include <linux/gpio/aspeed.h>
12 #include <linux/hashtable.h>
13 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
19 #include <linux/spinlock.h>
20 #include <linux/string.h>
23 * These two headers aren't meant to be used by GPIO drivers. We need
24 * them in order to access gpio_chip_hwgpio() which we need to implement
25 * the aspeed specific API which allows the coprocessor to request
26 * access to some GPIOs and to arbitrate between coprocessor and ARM.
28 #include <linux/gpio/consumer.h>
31 struct aspeed_bank_props {
37 struct aspeed_gpio_config {
38 unsigned int nr_gpios;
39 const struct aspeed_bank_props *props;
43 * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled
44 * @timer_users: Tracks the number of users for each timer
46 * The @timer_users has four elements but the first element is unused. This is
47 * to simplify accounting and indexing, as a zero value in @offset_timer
48 * represents disabled debouncing for the GPIO. Any other value for an element
49 * of @offset_timer is used as an index into @timer_users. This behaviour of
50 * the zero value aligns with the behaviour of zero built from the timer
51 * configuration registers (i.e. debouncing is disabled).
54 struct gpio_chip chip;
58 const struct aspeed_gpio_config *config;
61 unsigned int timer_users[4];
68 struct aspeed_gpio_bank {
69 uint16_t val_regs; /* +0: Rd: read input value, Wr: set write latch
70 * +4: Rd/Wr: Direction (0=in, 1=out)
72 uint16_t rdata_reg; /* Rd: read write latch, Wr: <none> */
74 uint16_t debounce_regs;
75 uint16_t tolerance_regs;
77 const char names[4][3];
81 * Note: The "value" register returns the input value sampled on the
82 * line even when the GPIO is configured as an output. Since
83 * that input goes through synchronizers, writing, then reading
84 * back may not return the written value right away.
86 * The "rdata" register returns the content of the write latch
87 * and thus can be used to read back what was last written
91 static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 };
93 static const struct aspeed_gpio_copro_ops *copro_ops;
94 static void *copro_data;
96 static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
101 .debounce_regs = 0x0040,
102 .tolerance_regs = 0x001c,
103 .cmdsrc_regs = 0x0060,
104 .names = { "A", "B", "C", "D" },
110 .debounce_regs = 0x0048,
111 .tolerance_regs = 0x003c,
112 .cmdsrc_regs = 0x0068,
113 .names = { "E", "F", "G", "H" },
119 .debounce_regs = 0x00b0,
120 .tolerance_regs = 0x00ac,
121 .cmdsrc_regs = 0x0090,
122 .names = { "I", "J", "K", "L" },
128 .debounce_regs = 0x0100,
129 .tolerance_regs = 0x00fc,
130 .cmdsrc_regs = 0x00e0,
131 .names = { "M", "N", "O", "P" },
137 .debounce_regs = 0x0130,
138 .tolerance_regs = 0x012c,
139 .cmdsrc_regs = 0x0110,
140 .names = { "Q", "R", "S", "T" },
146 .debounce_regs = 0x0160,
147 .tolerance_regs = 0x015c,
148 .cmdsrc_regs = 0x0140,
149 .names = { "U", "V", "W", "X" },
155 .debounce_regs = 0x0190,
156 .tolerance_regs = 0x018c,
157 .cmdsrc_regs = 0x0170,
158 .names = { "Y", "Z", "AA", "AB" },
164 .debounce_regs = 0x01c0,
165 .tolerance_regs = 0x01bc,
166 .cmdsrc_regs = 0x01a0,
167 .names = { "AC", "", "", "" },
171 enum aspeed_gpio_reg {
187 #define GPIO_VAL_VALUE 0x00
188 #define GPIO_VAL_DIR 0x04
190 #define GPIO_IRQ_ENABLE 0x00
191 #define GPIO_IRQ_TYPE0 0x04
192 #define GPIO_IRQ_TYPE1 0x08
193 #define GPIO_IRQ_TYPE2 0x0c
194 #define GPIO_IRQ_STATUS 0x10
196 #define GPIO_DEBOUNCE_SEL1 0x00
197 #define GPIO_DEBOUNCE_SEL2 0x04
199 #define GPIO_CMDSRC_0 0x00
200 #define GPIO_CMDSRC_1 0x04
201 #define GPIO_CMDSRC_ARM 0
202 #define GPIO_CMDSRC_LPC 1
203 #define GPIO_CMDSRC_COLDFIRE 2
204 #define GPIO_CMDSRC_RESERVED 3
206 /* This will be resolved at compile time */
207 static inline void __iomem *bank_reg(struct aspeed_gpio *gpio,
208 const struct aspeed_gpio_bank *bank,
209 const enum aspeed_gpio_reg reg)
213 return gpio->base + bank->val_regs + GPIO_VAL_VALUE;
215 return gpio->base + bank->rdata_reg;
217 return gpio->base + bank->val_regs + GPIO_VAL_DIR;
219 return gpio->base + bank->irq_regs + GPIO_IRQ_ENABLE;
221 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE0;
223 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE1;
225 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE2;
227 return gpio->base + bank->irq_regs + GPIO_IRQ_STATUS;
228 case reg_debounce_sel1:
229 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL1;
230 case reg_debounce_sel2:
231 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL2;
233 return gpio->base + bank->tolerance_regs;
235 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_0;
237 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_1;
242 #define GPIO_BANK(x) ((x) >> 5)
243 #define GPIO_OFFSET(x) ((x) & 0x1f)
244 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
246 #define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
247 #define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
248 #define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
250 static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
252 unsigned int bank = GPIO_BANK(offset);
254 WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks));
255 return &aspeed_gpio_banks[bank];
258 static inline bool is_bank_props_sentinel(const struct aspeed_bank_props *props)
260 return !(props->input || props->output);
263 static inline const struct aspeed_bank_props *find_bank_props(
264 struct aspeed_gpio *gpio, unsigned int offset)
266 const struct aspeed_bank_props *props = gpio->config->props;
268 while (!is_bank_props_sentinel(props)) {
269 if (props->bank == GPIO_BANK(offset))
277 static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset)
279 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
280 const struct aspeed_gpio_bank *bank = to_bank(offset);
281 unsigned int group = GPIO_OFFSET(offset) / 8;
283 return bank->names[group][0] != '\0' &&
284 (!props || ((props->input | props->output) & GPIO_BIT(offset)));
287 static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset)
289 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
291 return !props || (props->input & GPIO_BIT(offset));
294 #define have_irq(g, o) have_input((g), (o))
295 #define have_debounce(g, o) have_input((g), (o))
297 static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset)
299 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
301 return !props || (props->output & GPIO_BIT(offset));
304 static void aspeed_gpio_change_cmd_source(struct aspeed_gpio *gpio,
305 const struct aspeed_gpio_bank *bank,
306 int bindex, int cmdsrc)
308 void __iomem *c0 = bank_reg(gpio, bank, reg_cmdsrc0);
309 void __iomem *c1 = bank_reg(gpio, bank, reg_cmdsrc1);
313 * Each register controls 4 banks, so take the bottom 2
314 * bits of the bank index, and use them to select the
315 * right control bit (0, 8, 16 or 24).
317 bit = BIT((bindex & 3) << 3);
319 /* Source 1 first to avoid illegal 11 combination */
336 static bool aspeed_gpio_copro_request(struct aspeed_gpio *gpio,
339 const struct aspeed_gpio_bank *bank = to_bank(offset);
341 if (!copro_ops || !gpio->cf_copro_bankmap)
343 if (!gpio->cf_copro_bankmap[offset >> 3])
345 if (!copro_ops->request_access)
348 /* Pause the coprocessor */
349 copro_ops->request_access(copro_data);
351 /* Change command source back to ARM */
352 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3, GPIO_CMDSRC_ARM);
355 gpio->dcache[GPIO_BANK(offset)] = ioread32(bank_reg(gpio, bank, reg_rdata));
360 static void aspeed_gpio_copro_release(struct aspeed_gpio *gpio,
363 const struct aspeed_gpio_bank *bank = to_bank(offset);
365 if (!copro_ops || !gpio->cf_copro_bankmap)
367 if (!gpio->cf_copro_bankmap[offset >> 3])
369 if (!copro_ops->release_access)
372 /* Change command source back to ColdFire */
373 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3,
374 GPIO_CMDSRC_COLDFIRE);
376 /* Restart the coprocessor */
377 copro_ops->release_access(copro_data);
380 static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
382 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
383 const struct aspeed_gpio_bank *bank = to_bank(offset);
385 return !!(ioread32(bank_reg(gpio, bank, reg_val)) & GPIO_BIT(offset));
388 static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
391 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
392 const struct aspeed_gpio_bank *bank = to_bank(offset);
396 addr = bank_reg(gpio, bank, reg_val);
397 reg = gpio->dcache[GPIO_BANK(offset)];
400 reg |= GPIO_BIT(offset);
402 reg &= ~GPIO_BIT(offset);
403 gpio->dcache[GPIO_BANK(offset)] = reg;
405 iowrite32(reg, addr);
408 static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
411 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
415 spin_lock_irqsave(&gpio->lock, flags);
416 copro = aspeed_gpio_copro_request(gpio, offset);
418 __aspeed_gpio_set(gc, offset, val);
421 aspeed_gpio_copro_release(gpio, offset);
422 spin_unlock_irqrestore(&gpio->lock, flags);
425 static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
427 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
428 const struct aspeed_gpio_bank *bank = to_bank(offset);
429 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
434 if (!have_input(gpio, offset))
437 spin_lock_irqsave(&gpio->lock, flags);
439 reg = ioread32(addr);
440 reg &= ~GPIO_BIT(offset);
442 copro = aspeed_gpio_copro_request(gpio, offset);
443 iowrite32(reg, addr);
445 aspeed_gpio_copro_release(gpio, offset);
447 spin_unlock_irqrestore(&gpio->lock, flags);
452 static int aspeed_gpio_dir_out(struct gpio_chip *gc,
453 unsigned int offset, int val)
455 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
456 const struct aspeed_gpio_bank *bank = to_bank(offset);
457 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
462 if (!have_output(gpio, offset))
465 spin_lock_irqsave(&gpio->lock, flags);
467 reg = ioread32(addr);
468 reg |= GPIO_BIT(offset);
470 copro = aspeed_gpio_copro_request(gpio, offset);
471 __aspeed_gpio_set(gc, offset, val);
472 iowrite32(reg, addr);
475 aspeed_gpio_copro_release(gpio, offset);
476 spin_unlock_irqrestore(&gpio->lock, flags);
481 static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
483 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
484 const struct aspeed_gpio_bank *bank = to_bank(offset);
488 if (!have_input(gpio, offset))
491 if (!have_output(gpio, offset))
494 spin_lock_irqsave(&gpio->lock, flags);
496 val = ioread32(bank_reg(gpio, bank, reg_dir)) & GPIO_BIT(offset);
498 spin_unlock_irqrestore(&gpio->lock, flags);
504 static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
505 struct aspeed_gpio **gpio,
506 const struct aspeed_gpio_bank **bank,
507 u32 *bit, int *offset)
509 struct aspeed_gpio *internal;
511 *offset = irqd_to_hwirq(d);
513 internal = irq_data_get_irq_chip_data(d);
515 /* This might be a bit of a questionable place to check */
516 if (!have_irq(internal, *offset))
520 *bank = to_bank(*offset);
521 *bit = GPIO_BIT(*offset);
526 static void aspeed_gpio_irq_ack(struct irq_data *d)
528 const struct aspeed_gpio_bank *bank;
529 struct aspeed_gpio *gpio;
531 void __iomem *status_addr;
536 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
540 status_addr = bank_reg(gpio, bank, reg_irq_status);
542 spin_lock_irqsave(&gpio->lock, flags);
543 copro = aspeed_gpio_copro_request(gpio, offset);
545 iowrite32(bit, status_addr);
548 aspeed_gpio_copro_release(gpio, offset);
549 spin_unlock_irqrestore(&gpio->lock, flags);
552 static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
554 const struct aspeed_gpio_bank *bank;
555 struct aspeed_gpio *gpio;
562 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
566 addr = bank_reg(gpio, bank, reg_irq_enable);
568 spin_lock_irqsave(&gpio->lock, flags);
569 copro = aspeed_gpio_copro_request(gpio, offset);
571 reg = ioread32(addr);
576 iowrite32(reg, addr);
579 aspeed_gpio_copro_release(gpio, offset);
580 spin_unlock_irqrestore(&gpio->lock, flags);
583 static void aspeed_gpio_irq_mask(struct irq_data *d)
585 aspeed_gpio_irq_set_mask(d, false);
588 static void aspeed_gpio_irq_unmask(struct irq_data *d)
590 aspeed_gpio_irq_set_mask(d, true);
593 static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
599 const struct aspeed_gpio_bank *bank;
600 irq_flow_handler_t handler;
601 struct aspeed_gpio *gpio;
607 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
611 switch (type & IRQ_TYPE_SENSE_MASK) {
612 case IRQ_TYPE_EDGE_BOTH:
615 case IRQ_TYPE_EDGE_RISING:
618 case IRQ_TYPE_EDGE_FALLING:
619 handler = handle_edge_irq;
621 case IRQ_TYPE_LEVEL_HIGH:
624 case IRQ_TYPE_LEVEL_LOW:
626 handler = handle_level_irq;
632 spin_lock_irqsave(&gpio->lock, flags);
633 copro = aspeed_gpio_copro_request(gpio, offset);
635 addr = bank_reg(gpio, bank, reg_irq_type0);
636 reg = ioread32(addr);
637 reg = (reg & ~bit) | type0;
638 iowrite32(reg, addr);
640 addr = bank_reg(gpio, bank, reg_irq_type1);
641 reg = ioread32(addr);
642 reg = (reg & ~bit) | type1;
643 iowrite32(reg, addr);
645 addr = bank_reg(gpio, bank, reg_irq_type2);
646 reg = ioread32(addr);
647 reg = (reg & ~bit) | type2;
648 iowrite32(reg, addr);
651 aspeed_gpio_copro_release(gpio, offset);
652 spin_unlock_irqrestore(&gpio->lock, flags);
654 irq_set_handler_locked(d, handler);
659 static void aspeed_gpio_irq_handler(struct irq_desc *desc)
661 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
662 struct irq_chip *ic = irq_desc_get_chip(desc);
663 struct aspeed_gpio *data = gpiochip_get_data(gc);
664 unsigned int i, p, girq;
667 chained_irq_enter(ic, desc);
669 for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) {
670 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
672 reg = ioread32(bank_reg(data, bank, reg_irq_status));
674 for_each_set_bit(p, ®, 32) {
675 girq = irq_find_mapping(gc->irq.domain, i * 32 + p);
676 generic_handle_irq(girq);
681 chained_irq_exit(ic, desc);
684 static struct irq_chip aspeed_gpio_irqchip = {
685 .name = "aspeed-gpio",
686 .irq_ack = aspeed_gpio_irq_ack,
687 .irq_mask = aspeed_gpio_irq_mask,
688 .irq_unmask = aspeed_gpio_irq_unmask,
689 .irq_set_type = aspeed_gpio_set_type,
692 static void set_irq_valid_mask(struct aspeed_gpio *gpio)
694 const struct aspeed_bank_props *props = gpio->config->props;
696 while (!is_bank_props_sentinel(props)) {
698 const unsigned long int input = props->input;
700 /* Pretty crummy approach, but similar to GPIO core */
701 for_each_clear_bit(offset, &input, 32) {
702 unsigned int i = props->bank * 32 + offset;
704 if (i >= gpio->config->nr_gpios)
707 clear_bit(i, gpio->chip.irq.valid_mask);
714 static int aspeed_gpio_setup_irqs(struct aspeed_gpio *gpio,
715 struct platform_device *pdev)
719 rc = platform_get_irq(pdev, 0);
725 set_irq_valid_mask(gpio);
727 rc = gpiochip_irqchip_add(&gpio->chip, &aspeed_gpio_irqchip,
728 0, handle_bad_irq, IRQ_TYPE_NONE);
730 dev_info(&pdev->dev, "Could not add irqchip\n");
734 gpiochip_set_chained_irqchip(&gpio->chip, &aspeed_gpio_irqchip,
735 gpio->irq, aspeed_gpio_irq_handler);
740 static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip,
741 unsigned int offset, bool enable)
743 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
749 treg = bank_reg(gpio, to_bank(offset), reg_tolerance);
751 spin_lock_irqsave(&gpio->lock, flags);
752 copro = aspeed_gpio_copro_request(gpio, offset);
757 val |= GPIO_BIT(offset);
759 val &= ~GPIO_BIT(offset);
764 aspeed_gpio_copro_release(gpio, offset);
765 spin_unlock_irqrestore(&gpio->lock, flags);
770 static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
772 if (!have_gpio(gpiochip_get_data(chip), offset))
775 return pinctrl_gpio_request(chip->base + offset);
778 static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
780 pinctrl_gpio_free(chip->base + offset);
783 static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs,
790 rate = clk_get_rate(gpio->clk);
795 r = do_div(n, 1000000);
800 /* At least as long as the requested time */
806 /* Call under gpio->lock */
807 static int register_allocated_timer(struct aspeed_gpio *gpio,
808 unsigned int offset, unsigned int timer)
810 if (WARN(gpio->offset_timer[offset] != 0,
811 "Offset %d already allocated timer %d\n",
812 offset, gpio->offset_timer[offset]))
815 if (WARN(gpio->timer_users[timer] == UINT_MAX,
816 "Timer user count would overflow\n"))
819 gpio->offset_timer[offset] = timer;
820 gpio->timer_users[timer]++;
825 /* Call under gpio->lock */
826 static int unregister_allocated_timer(struct aspeed_gpio *gpio,
829 if (WARN(gpio->offset_timer[offset] == 0,
830 "No timer allocated to offset %d\n", offset))
833 if (WARN(gpio->timer_users[gpio->offset_timer[offset]] == 0,
834 "No users recorded for timer %d\n",
835 gpio->offset_timer[offset]))
838 gpio->timer_users[gpio->offset_timer[offset]]--;
839 gpio->offset_timer[offset] = 0;
844 /* Call under gpio->lock */
845 static inline bool timer_allocation_registered(struct aspeed_gpio *gpio,
848 return gpio->offset_timer[offset] > 0;
851 /* Call under gpio->lock */
852 static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset,
855 const struct aspeed_gpio_bank *bank = to_bank(offset);
856 const u32 mask = GPIO_BIT(offset);
860 /* Note: Debounce timer isn't under control of the command
861 * source registers, so no need to sync with the coprocessor
863 addr = bank_reg(gpio, bank, reg_debounce_sel1);
864 val = ioread32(addr);
865 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr);
867 addr = bank_reg(gpio, bank, reg_debounce_sel2);
868 val = ioread32(addr);
869 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr);
872 static int enable_debounce(struct gpio_chip *chip, unsigned int offset,
875 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
876 u32 requested_cycles;
884 rc = usecs_to_cycles(gpio, usecs, &requested_cycles);
886 dev_warn(chip->parent, "Failed to convert %luus to cycles at %luHz: %d\n",
887 usecs, clk_get_rate(gpio->clk), rc);
891 spin_lock_irqsave(&gpio->lock, flags);
893 if (timer_allocation_registered(gpio, offset)) {
894 rc = unregister_allocated_timer(gpio, offset);
899 /* Try to find a timer already configured for the debounce period */
900 for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) {
903 cycles = ioread32(gpio->base + debounce_timers[i]);
904 if (requested_cycles == cycles)
908 if (i == ARRAY_SIZE(debounce_timers)) {
912 * As there are no timers configured for the requested debounce
913 * period, find an unused timer instead
915 for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) {
916 if (gpio->timer_users[j] == 0)
920 if (j == ARRAY_SIZE(gpio->timer_users)) {
921 dev_warn(chip->parent,
922 "Debounce timers exhausted, cannot debounce for period %luus\n",
928 * We already adjusted the accounting to remove @offset
929 * as a user of its previous timer, so also configure
930 * the hardware so @offset has timers disabled for
933 configure_timer(gpio, offset, 0);
939 iowrite32(requested_cycles, gpio->base + debounce_timers[i]);
942 if (WARN(i == 0, "Cannot register index of disabled timer\n")) {
947 register_allocated_timer(gpio, offset, i);
948 configure_timer(gpio, offset, i);
951 spin_unlock_irqrestore(&gpio->lock, flags);
956 static int disable_debounce(struct gpio_chip *chip, unsigned int offset)
958 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
962 spin_lock_irqsave(&gpio->lock, flags);
964 rc = unregister_allocated_timer(gpio, offset);
966 configure_timer(gpio, offset, 0);
968 spin_unlock_irqrestore(&gpio->lock, flags);
973 static int set_debounce(struct gpio_chip *chip, unsigned int offset,
976 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
978 if (!have_debounce(gpio, offset))
982 return enable_debounce(chip, offset, usecs);
984 return disable_debounce(chip, offset);
987 static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
988 unsigned long config)
990 unsigned long param = pinconf_to_config_param(config);
991 u32 arg = pinconf_to_config_argument(config);
993 if (param == PIN_CONFIG_INPUT_DEBOUNCE)
994 return set_debounce(chip, offset, arg);
995 else if (param == PIN_CONFIG_BIAS_DISABLE ||
996 param == PIN_CONFIG_BIAS_PULL_DOWN ||
997 param == PIN_CONFIG_DRIVE_STRENGTH)
998 return pinctrl_gpio_set_config(offset, config);
999 else if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN ||
1000 param == PIN_CONFIG_DRIVE_OPEN_SOURCE)
1001 /* Return -ENOTSUPP to trigger emulation, as per datasheet */
1003 else if (param == PIN_CONFIG_PERSIST_STATE)
1004 return aspeed_gpio_reset_tolerance(chip, offset, arg);
1010 * aspeed_gpio_copro_set_ops - Sets the callbacks used for handhsaking with
1011 * the coprocessor for shared GPIO banks
1012 * @ops: The callbacks
1013 * @data: Pointer passed back to the callbacks
1015 int aspeed_gpio_copro_set_ops(const struct aspeed_gpio_copro_ops *ops, void *data)
1022 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_set_ops);
1025 * aspeed_gpio_copro_grab_gpio - Mark a GPIO used by the coprocessor. The entire
1026 * bank gets marked and any access from the ARM will
1027 * result in handshaking via callbacks.
1028 * @desc: The GPIO to be marked
1029 * @vreg_offset: If non-NULL, returns the value register offset in the GPIO space
1030 * @dreg_offset: If non-NULL, returns the data latch register offset in the GPIO space
1031 * @bit: If non-NULL, returns the bit number of the GPIO in the registers
1033 int aspeed_gpio_copro_grab_gpio(struct gpio_desc *desc,
1034 u16 *vreg_offset, u16 *dreg_offset, u8 *bit)
1036 struct gpio_chip *chip = gpiod_to_chip(desc);
1037 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
1038 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
1039 const struct aspeed_gpio_bank *bank = to_bank(offset);
1040 unsigned long flags;
1042 if (!gpio->cf_copro_bankmap)
1043 gpio->cf_copro_bankmap = kzalloc(gpio->config->nr_gpios >> 3, GFP_KERNEL);
1044 if (!gpio->cf_copro_bankmap)
1046 if (offset < 0 || offset > gpio->config->nr_gpios)
1048 bindex = offset >> 3;
1050 spin_lock_irqsave(&gpio->lock, flags);
1052 /* Sanity check, this shouldn't happen */
1053 if (gpio->cf_copro_bankmap[bindex] == 0xff) {
1057 gpio->cf_copro_bankmap[bindex]++;
1059 /* Switch command source */
1060 if (gpio->cf_copro_bankmap[bindex] == 1)
1061 aspeed_gpio_change_cmd_source(gpio, bank, bindex,
1062 GPIO_CMDSRC_COLDFIRE);
1065 *vreg_offset = bank->val_regs;
1067 *dreg_offset = bank->rdata_reg;
1069 *bit = GPIO_OFFSET(offset);
1071 spin_unlock_irqrestore(&gpio->lock, flags);
1074 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_grab_gpio);
1077 * aspeed_gpio_copro_release_gpio - Unmark a GPIO used by the coprocessor.
1078 * @desc: The GPIO to be marked
1080 int aspeed_gpio_copro_release_gpio(struct gpio_desc *desc)
1082 struct gpio_chip *chip = gpiod_to_chip(desc);
1083 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
1084 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
1085 const struct aspeed_gpio_bank *bank = to_bank(offset);
1086 unsigned long flags;
1088 if (!gpio->cf_copro_bankmap)
1091 if (offset < 0 || offset > gpio->config->nr_gpios)
1093 bindex = offset >> 3;
1095 spin_lock_irqsave(&gpio->lock, flags);
1097 /* Sanity check, this shouldn't happen */
1098 if (gpio->cf_copro_bankmap[bindex] == 0) {
1102 gpio->cf_copro_bankmap[bindex]--;
1104 /* Switch command source */
1105 if (gpio->cf_copro_bankmap[bindex] == 0)
1106 aspeed_gpio_change_cmd_source(gpio, bank, bindex,
1109 spin_unlock_irqrestore(&gpio->lock, flags);
1112 EXPORT_SYMBOL_GPL(aspeed_gpio_copro_release_gpio);
1115 * Any banks not specified in a struct aspeed_bank_props array are assumed to
1116 * have the properties:
1118 * { .input = 0xffffffff, .output = 0xffffffff }
1121 static const struct aspeed_bank_props ast2400_bank_props[] = {
1123 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1124 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
1128 static const struct aspeed_gpio_config ast2400_config =
1129 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
1130 { .nr_gpios = 220, .props = ast2400_bank_props, };
1132 static const struct aspeed_bank_props ast2500_bank_props[] = {
1134 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1135 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
1136 { 7, 0x000000ff, 0x000000ff }, /* AC */
1140 static const struct aspeed_gpio_config ast2500_config =
1141 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
1142 { .nr_gpios = 232, .props = ast2500_bank_props, };
1144 static const struct of_device_id aspeed_gpio_of_table[] = {
1145 { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
1146 { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
1149 MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
1151 static int __init aspeed_gpio_probe(struct platform_device *pdev)
1153 const struct of_device_id *gpio_id;
1154 struct aspeed_gpio *gpio;
1157 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
1161 gpio->base = devm_platform_ioremap_resource(pdev, 0);
1162 if (IS_ERR(gpio->base))
1163 return PTR_ERR(gpio->base);
1165 spin_lock_init(&gpio->lock);
1167 gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node);
1171 gpio->clk = of_clk_get(pdev->dev.of_node, 0);
1172 if (IS_ERR(gpio->clk)) {
1173 dev_warn(&pdev->dev,
1174 "Failed to get clock from devicetree, debouncing disabled\n");
1178 gpio->config = gpio_id->data;
1180 gpio->chip.parent = &pdev->dev;
1181 gpio->chip.ngpio = gpio->config->nr_gpios;
1182 gpio->chip.direction_input = aspeed_gpio_dir_in;
1183 gpio->chip.direction_output = aspeed_gpio_dir_out;
1184 gpio->chip.get_direction = aspeed_gpio_get_direction;
1185 gpio->chip.request = aspeed_gpio_request;
1186 gpio->chip.free = aspeed_gpio_free;
1187 gpio->chip.get = aspeed_gpio_get;
1188 gpio->chip.set = aspeed_gpio_set;
1189 gpio->chip.set_config = aspeed_gpio_set_config;
1190 gpio->chip.label = dev_name(&pdev->dev);
1191 gpio->chip.base = -1;
1192 gpio->chip.irq.need_valid_mask = true;
1194 /* Allocate a cache of the output registers */
1195 banks = gpio->config->nr_gpios >> 5;
1196 gpio->dcache = devm_kcalloc(&pdev->dev,
1197 banks, sizeof(u32), GFP_KERNEL);
1202 * Populate it with initial values read from the HW and switch
1203 * all command sources to the ARM by default
1205 for (i = 0; i < banks; i++) {
1206 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
1207 void __iomem *addr = bank_reg(gpio, bank, reg_rdata);
1208 gpio->dcache[i] = ioread32(addr);
1209 aspeed_gpio_change_cmd_source(gpio, bank, 0, GPIO_CMDSRC_ARM);
1210 aspeed_gpio_change_cmd_source(gpio, bank, 1, GPIO_CMDSRC_ARM);
1211 aspeed_gpio_change_cmd_source(gpio, bank, 2, GPIO_CMDSRC_ARM);
1212 aspeed_gpio_change_cmd_source(gpio, bank, 3, GPIO_CMDSRC_ARM);
1215 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
1219 gpio->offset_timer =
1220 devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL);
1221 if (!gpio->offset_timer)
1224 return aspeed_gpio_setup_irqs(gpio, pdev);
1227 static struct platform_driver aspeed_gpio_driver = {
1229 .name = KBUILD_MODNAME,
1230 .of_match_table = aspeed_gpio_of_table,
1234 module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
1236 MODULE_DESCRIPTION("Aspeed GPIO Driver");
1237 MODULE_LICENSE("GPL");