1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2019 American Megatrends International LLC.
5 * Author: Karthikeyan Mani <karthikeyanm@amiindia.co.in>
8 #include <linux/bitfield.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/platform_device.h>
17 #include <linux/seq_file.h>
18 #include <linux/spinlock.h>
19 #include <linux/string.h>
21 #define ASPEED_SGPIO_CTRL 0x54
23 #define ASPEED_SGPIO_CLK_DIV_MASK GENMASK(31, 16)
24 #define ASPEED_SGPIO_ENABLE BIT(0)
25 #define ASPEED_SGPIO_PINS_SHIFT 6
27 struct aspeed_sgpio_pdata {
32 struct gpio_chip chip;
40 struct aspeed_sgpio_bank {
45 const char names[4][3];
49 * Note: The "value" register returns the input value when the GPIO is
50 * configured as an input.
52 * The "rdata" register returns the output value when the GPIO is
53 * configured as an output.
55 static const struct aspeed_sgpio_bank aspeed_sgpio_banks[] = {
60 .tolerance_regs = 0x0018,
61 .names = { "A", "B", "C", "D" },
67 .tolerance_regs = 0x0034,
68 .names = { "E", "F", "G", "H" },
74 .tolerance_regs = 0x0050,
75 .names = { "I", "J", "K", "L" },
81 .tolerance_regs = 0x00A8,
82 .names = { "M", "N", "O", "P" },
86 enum aspeed_sgpio_reg {
97 #define GPIO_VAL_VALUE 0x00
98 #define GPIO_IRQ_ENABLE 0x00
99 #define GPIO_IRQ_TYPE0 0x04
100 #define GPIO_IRQ_TYPE1 0x08
101 #define GPIO_IRQ_TYPE2 0x0C
102 #define GPIO_IRQ_STATUS 0x10
104 static void __iomem *bank_reg(struct aspeed_sgpio *gpio,
105 const struct aspeed_sgpio_bank *bank,
106 const enum aspeed_sgpio_reg reg)
110 return gpio->base + bank->val_regs + GPIO_VAL_VALUE;
112 return gpio->base + bank->rdata_reg;
114 return gpio->base + bank->irq_regs + GPIO_IRQ_ENABLE;
116 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE0;
118 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE1;
120 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE2;
122 return gpio->base + bank->irq_regs + GPIO_IRQ_STATUS;
124 return gpio->base + bank->tolerance_regs;
126 /* acturally if code runs to here, it's an error case */
131 #define GPIO_BANK(x) ((x) >> 6)
132 #define GPIO_OFFSET(x) ((x) & GENMASK(5, 0))
133 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x) >> 1)
135 static const struct aspeed_sgpio_bank *to_bank(unsigned int offset)
139 bank = GPIO_BANK(offset);
141 WARN_ON(bank >= ARRAY_SIZE(aspeed_sgpio_banks));
142 return &aspeed_sgpio_banks[bank];
145 static int aspeed_sgpio_init_valid_mask(struct gpio_chip *gc,
146 unsigned long *valid_mask, unsigned int ngpios)
148 bitmap_set(valid_mask, 0, ngpios);
152 static void aspeed_sgpio_irq_init_valid_mask(struct gpio_chip *gc,
153 unsigned long *valid_mask, unsigned int ngpios)
157 /* input GPIOs are even bits */
158 for (i = 0; i < ngpios; i++) {
160 clear_bit(i, valid_mask);
164 static bool aspeed_sgpio_is_input(unsigned int offset)
166 return !(offset % 2);
169 static int aspeed_sgpio_get(struct gpio_chip *gc, unsigned int offset)
171 struct aspeed_sgpio *gpio = gpiochip_get_data(gc);
172 const struct aspeed_sgpio_bank *bank = to_bank(offset);
174 enum aspeed_sgpio_reg reg;
177 raw_spin_lock_irqsave(&gpio->lock, flags);
179 reg = aspeed_sgpio_is_input(offset) ? reg_val : reg_rdata;
180 rc = !!(ioread32(bank_reg(gpio, bank, reg)) & GPIO_BIT(offset));
182 raw_spin_unlock_irqrestore(&gpio->lock, flags);
187 static int sgpio_set_value(struct gpio_chip *gc, unsigned int offset, int val)
189 struct aspeed_sgpio *gpio = gpiochip_get_data(gc);
190 const struct aspeed_sgpio_bank *bank = to_bank(offset);
191 void __iomem *addr_r, *addr_w;
194 if (aspeed_sgpio_is_input(offset))
197 /* Since this is an output, read the cached value from rdata, then
199 addr_r = bank_reg(gpio, bank, reg_rdata);
200 addr_w = bank_reg(gpio, bank, reg_val);
202 reg = ioread32(addr_r);
205 reg |= GPIO_BIT(offset);
207 reg &= ~GPIO_BIT(offset);
209 iowrite32(reg, addr_w);
214 static void aspeed_sgpio_set(struct gpio_chip *gc, unsigned int offset, int val)
216 struct aspeed_sgpio *gpio = gpiochip_get_data(gc);
219 raw_spin_lock_irqsave(&gpio->lock, flags);
221 sgpio_set_value(gc, offset, val);
223 raw_spin_unlock_irqrestore(&gpio->lock, flags);
226 static int aspeed_sgpio_dir_in(struct gpio_chip *gc, unsigned int offset)
228 return aspeed_sgpio_is_input(offset) ? 0 : -EINVAL;
231 static int aspeed_sgpio_dir_out(struct gpio_chip *gc, unsigned int offset, int val)
233 struct aspeed_sgpio *gpio = gpiochip_get_data(gc);
237 /* No special action is required for setting the direction; we'll
238 * error-out in sgpio_set_value if this isn't an output GPIO */
240 raw_spin_lock_irqsave(&gpio->lock, flags);
241 rc = sgpio_set_value(gc, offset, val);
242 raw_spin_unlock_irqrestore(&gpio->lock, flags);
247 static int aspeed_sgpio_get_direction(struct gpio_chip *gc, unsigned int offset)
249 return !!aspeed_sgpio_is_input(offset);
252 static void irqd_to_aspeed_sgpio_data(struct irq_data *d,
253 struct aspeed_sgpio **gpio,
254 const struct aspeed_sgpio_bank **bank,
255 u32 *bit, int *offset)
257 struct aspeed_sgpio *internal;
259 *offset = irqd_to_hwirq(d);
260 internal = irq_data_get_irq_chip_data(d);
264 *bank = to_bank(*offset);
265 *bit = GPIO_BIT(*offset);
268 static void aspeed_sgpio_irq_ack(struct irq_data *d)
270 const struct aspeed_sgpio_bank *bank;
271 struct aspeed_sgpio *gpio;
273 void __iomem *status_addr;
277 irqd_to_aspeed_sgpio_data(d, &gpio, &bank, &bit, &offset);
279 status_addr = bank_reg(gpio, bank, reg_irq_status);
281 raw_spin_lock_irqsave(&gpio->lock, flags);
283 iowrite32(bit, status_addr);
285 raw_spin_unlock_irqrestore(&gpio->lock, flags);
288 static void aspeed_sgpio_irq_set_mask(struct irq_data *d, bool set)
290 const struct aspeed_sgpio_bank *bank;
291 struct aspeed_sgpio *gpio;
297 irqd_to_aspeed_sgpio_data(d, &gpio, &bank, &bit, &offset);
298 addr = bank_reg(gpio, bank, reg_irq_enable);
300 /* Unmasking the IRQ */
302 gpiochip_enable_irq(&gpio->chip, irqd_to_hwirq(d));
304 raw_spin_lock_irqsave(&gpio->lock, flags);
306 reg = ioread32(addr);
312 iowrite32(reg, addr);
314 raw_spin_unlock_irqrestore(&gpio->lock, flags);
316 /* Masking the IRQ */
318 gpiochip_disable_irq(&gpio->chip, irqd_to_hwirq(d));
323 static void aspeed_sgpio_irq_mask(struct irq_data *d)
325 aspeed_sgpio_irq_set_mask(d, false);
328 static void aspeed_sgpio_irq_unmask(struct irq_data *d)
330 aspeed_sgpio_irq_set_mask(d, true);
333 static int aspeed_sgpio_set_type(struct irq_data *d, unsigned int type)
339 const struct aspeed_sgpio_bank *bank;
340 irq_flow_handler_t handler;
341 struct aspeed_sgpio *gpio;
346 irqd_to_aspeed_sgpio_data(d, &gpio, &bank, &bit, &offset);
348 switch (type & IRQ_TYPE_SENSE_MASK) {
349 case IRQ_TYPE_EDGE_BOTH:
352 case IRQ_TYPE_EDGE_RISING:
355 case IRQ_TYPE_EDGE_FALLING:
356 handler = handle_edge_irq;
358 case IRQ_TYPE_LEVEL_HIGH:
361 case IRQ_TYPE_LEVEL_LOW:
363 handler = handle_level_irq;
369 raw_spin_lock_irqsave(&gpio->lock, flags);
371 addr = bank_reg(gpio, bank, reg_irq_type0);
372 reg = ioread32(addr);
373 reg = (reg & ~bit) | type0;
374 iowrite32(reg, addr);
376 addr = bank_reg(gpio, bank, reg_irq_type1);
377 reg = ioread32(addr);
378 reg = (reg & ~bit) | type1;
379 iowrite32(reg, addr);
381 addr = bank_reg(gpio, bank, reg_irq_type2);
382 reg = ioread32(addr);
383 reg = (reg & ~bit) | type2;
384 iowrite32(reg, addr);
386 raw_spin_unlock_irqrestore(&gpio->lock, flags);
388 irq_set_handler_locked(d, handler);
393 static void aspeed_sgpio_irq_handler(struct irq_desc *desc)
395 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
396 struct irq_chip *ic = irq_desc_get_chip(desc);
397 struct aspeed_sgpio *data = gpiochip_get_data(gc);
401 chained_irq_enter(ic, desc);
403 for (i = 0; i < ARRAY_SIZE(aspeed_sgpio_banks); i++) {
404 const struct aspeed_sgpio_bank *bank = &aspeed_sgpio_banks[i];
406 reg = ioread32(bank_reg(data, bank, reg_irq_status));
408 for_each_set_bit(p, ®, 32)
409 generic_handle_domain_irq(gc->irq.domain, (i * 32 + p) * 2);
412 chained_irq_exit(ic, desc);
415 static void aspeed_sgpio_irq_print_chip(struct irq_data *d, struct seq_file *p)
417 const struct aspeed_sgpio_bank *bank;
418 struct aspeed_sgpio *gpio;
422 irqd_to_aspeed_sgpio_data(d, &gpio, &bank, &bit, &offset);
423 seq_printf(p, dev_name(gpio->dev));
426 static const struct irq_chip aspeed_sgpio_irq_chip = {
427 .irq_ack = aspeed_sgpio_irq_ack,
428 .irq_mask = aspeed_sgpio_irq_mask,
429 .irq_unmask = aspeed_sgpio_irq_unmask,
430 .irq_set_type = aspeed_sgpio_set_type,
431 .irq_print_chip = aspeed_sgpio_irq_print_chip,
432 .flags = IRQCHIP_IMMUTABLE,
433 GPIOCHIP_IRQ_RESOURCE_HELPERS,
436 static int aspeed_sgpio_setup_irqs(struct aspeed_sgpio *gpio,
437 struct platform_device *pdev)
440 const struct aspeed_sgpio_bank *bank;
441 struct gpio_irq_chip *irq;
443 rc = platform_get_irq(pdev, 0);
449 /* Disable IRQ and clear Interrupt status registers for all SGPIO Pins. */
450 for (i = 0; i < ARRAY_SIZE(aspeed_sgpio_banks); i++) {
451 bank = &aspeed_sgpio_banks[i];
452 /* disable irq enable bits */
453 iowrite32(0x00000000, bank_reg(gpio, bank, reg_irq_enable));
454 /* clear status bits */
455 iowrite32(0xffffffff, bank_reg(gpio, bank, reg_irq_status));
458 irq = &gpio->chip.irq;
459 gpio_irq_chip_set_chip(irq, &aspeed_sgpio_irq_chip);
460 irq->init_valid_mask = aspeed_sgpio_irq_init_valid_mask;
461 irq->handler = handle_bad_irq;
462 irq->default_type = IRQ_TYPE_NONE;
463 irq->parent_handler = aspeed_sgpio_irq_handler;
464 irq->parent_handler_data = gpio;
465 irq->parents = &gpio->irq;
466 irq->num_parents = 1;
468 /* Apply default IRQ settings */
469 for (i = 0; i < ARRAY_SIZE(aspeed_sgpio_banks); i++) {
470 bank = &aspeed_sgpio_banks[i];
471 /* set falling or level-low irq */
472 iowrite32(0x00000000, bank_reg(gpio, bank, reg_irq_type0));
473 /* trigger type is edge */
474 iowrite32(0x00000000, bank_reg(gpio, bank, reg_irq_type1));
475 /* single edge trigger */
476 iowrite32(0x00000000, bank_reg(gpio, bank, reg_irq_type2));
482 static const struct aspeed_sgpio_pdata ast2400_sgpio_pdata = {
483 .pin_mask = GENMASK(9, 6),
486 static int aspeed_sgpio_reset_tolerance(struct gpio_chip *chip,
487 unsigned int offset, bool enable)
489 struct aspeed_sgpio *gpio = gpiochip_get_data(chip);
494 reg = bank_reg(gpio, to_bank(offset), reg_tolerance);
496 raw_spin_lock_irqsave(&gpio->lock, flags);
501 val |= GPIO_BIT(offset);
503 val &= ~GPIO_BIT(offset);
507 raw_spin_unlock_irqrestore(&gpio->lock, flags);
512 static int aspeed_sgpio_set_config(struct gpio_chip *chip, unsigned int offset,
513 unsigned long config)
515 unsigned long param = pinconf_to_config_param(config);
516 u32 arg = pinconf_to_config_argument(config);
518 if (param == PIN_CONFIG_PERSIST_STATE)
519 return aspeed_sgpio_reset_tolerance(chip, offset, arg);
524 static const struct aspeed_sgpio_pdata ast2600_sgpiom_pdata = {
525 .pin_mask = GENMASK(10, 6),
528 static const struct of_device_id aspeed_sgpio_of_table[] = {
529 { .compatible = "aspeed,ast2400-sgpio", .data = &ast2400_sgpio_pdata, },
530 { .compatible = "aspeed,ast2500-sgpio", .data = &ast2400_sgpio_pdata, },
531 { .compatible = "aspeed,ast2600-sgpiom", .data = &ast2600_sgpiom_pdata, },
535 MODULE_DEVICE_TABLE(of, aspeed_sgpio_of_table);
537 static int __init aspeed_sgpio_probe(struct platform_device *pdev)
539 u32 nr_gpios, sgpio_freq, sgpio_clk_div, gpio_cnt_regval, pin_mask;
540 const struct aspeed_sgpio_pdata *pdata;
541 struct aspeed_sgpio *gpio;
542 unsigned long apb_freq;
545 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
549 gpio->base = devm_platform_ioremap_resource(pdev, 0);
550 if (IS_ERR(gpio->base))
551 return PTR_ERR(gpio->base);
553 gpio->dev = &pdev->dev;
555 pdata = device_get_match_data(&pdev->dev);
559 pin_mask = pdata->pin_mask;
561 rc = device_property_read_u32(&pdev->dev, "ngpios", &nr_gpios);
563 dev_err(&pdev->dev, "Could not read ngpios property\n");
565 } else if (nr_gpios % 8) {
566 dev_err(&pdev->dev, "Number of GPIOs not multiple of 8: %d\n",
571 rc = device_property_read_u32(&pdev->dev, "bus-frequency", &sgpio_freq);
573 dev_err(&pdev->dev, "Could not read bus-frequency property\n");
577 gpio->pclk = devm_clk_get(&pdev->dev, NULL);
578 if (IS_ERR(gpio->pclk)) {
579 dev_err(&pdev->dev, "devm_clk_get failed\n");
580 return PTR_ERR(gpio->pclk);
583 apb_freq = clk_get_rate(gpio->pclk);
586 * From the datasheet,
587 * SGPIO period = 1/PCLK * 2 * (GPIO254[31:16] + 1)
588 * period = 2 * (GPIO254[31:16] + 1) / PCLK
589 * frequency = 1 / (2 * (GPIO254[31:16] + 1) / PCLK)
590 * frequency = PCLK / (2 * (GPIO254[31:16] + 1))
591 * frequency * 2 * (GPIO254[31:16] + 1) = PCLK
592 * GPIO254[31:16] = PCLK / (frequency * 2) - 1
597 sgpio_clk_div = (apb_freq / (sgpio_freq * 2)) - 1;
599 if (sgpio_clk_div > (1 << 16) - 1)
602 gpio_cnt_regval = ((nr_gpios / 8) << ASPEED_SGPIO_PINS_SHIFT) & pin_mask;
603 iowrite32(FIELD_PREP(ASPEED_SGPIO_CLK_DIV_MASK, sgpio_clk_div) | gpio_cnt_regval |
604 ASPEED_SGPIO_ENABLE, gpio->base + ASPEED_SGPIO_CTRL);
606 raw_spin_lock_init(&gpio->lock);
608 gpio->chip.parent = &pdev->dev;
609 gpio->chip.ngpio = nr_gpios * 2;
610 gpio->chip.init_valid_mask = aspeed_sgpio_init_valid_mask;
611 gpio->chip.direction_input = aspeed_sgpio_dir_in;
612 gpio->chip.direction_output = aspeed_sgpio_dir_out;
613 gpio->chip.get_direction = aspeed_sgpio_get_direction;
614 gpio->chip.request = NULL;
615 gpio->chip.free = NULL;
616 gpio->chip.get = aspeed_sgpio_get;
617 gpio->chip.set = aspeed_sgpio_set;
618 gpio->chip.set_config = aspeed_sgpio_set_config;
619 gpio->chip.label = dev_name(&pdev->dev);
620 gpio->chip.base = -1;
622 aspeed_sgpio_setup_irqs(gpio, pdev);
624 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
631 static struct platform_driver aspeed_sgpio_driver = {
633 .name = KBUILD_MODNAME,
634 .of_match_table = aspeed_sgpio_of_table,
638 module_platform_driver_probe(aspeed_sgpio_driver, aspeed_sgpio_probe);
639 MODULE_DESCRIPTION("Aspeed Serial GPIO Driver");