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/spinlock.h>
18 #include <linux/string.h>
20 #define ASPEED_SGPIO_CTRL 0x54
22 #define ASPEED_SGPIO_CLK_DIV_MASK GENMASK(31, 16)
23 #define ASPEED_SGPIO_ENABLE BIT(0)
24 #define ASPEED_SGPIO_PINS_SHIFT 6
26 struct aspeed_sgpio_pdata {
31 struct gpio_chip chip;
39 struct aspeed_sgpio_bank {
44 const char names[4][3];
48 * Note: The "value" register returns the input value when the GPIO is
49 * configured as an input.
51 * The "rdata" register returns the output value when the GPIO is
52 * configured as an output.
54 static const struct aspeed_sgpio_bank aspeed_sgpio_banks[] = {
59 .tolerance_regs = 0x0018,
60 .names = { "A", "B", "C", "D" },
66 .tolerance_regs = 0x0034,
67 .names = { "E", "F", "G", "H" },
73 .tolerance_regs = 0x0050,
74 .names = { "I", "J", "K", "L" },
80 .tolerance_regs = 0x00A8,
81 .names = { "M", "N", "O", "P" },
85 enum aspeed_sgpio_reg {
96 #define GPIO_VAL_VALUE 0x00
97 #define GPIO_IRQ_ENABLE 0x00
98 #define GPIO_IRQ_TYPE0 0x04
99 #define GPIO_IRQ_TYPE1 0x08
100 #define GPIO_IRQ_TYPE2 0x0C
101 #define GPIO_IRQ_STATUS 0x10
103 static void __iomem *bank_reg(struct aspeed_sgpio *gpio,
104 const struct aspeed_sgpio_bank *bank,
105 const enum aspeed_sgpio_reg reg)
109 return gpio->base + bank->val_regs + GPIO_VAL_VALUE;
111 return gpio->base + bank->rdata_reg;
113 return gpio->base + bank->irq_regs + GPIO_IRQ_ENABLE;
115 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE0;
117 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE1;
119 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE2;
121 return gpio->base + bank->irq_regs + GPIO_IRQ_STATUS;
123 return gpio->base + bank->tolerance_regs;
125 /* acturally if code runs to here, it's an error case */
130 #define GPIO_BANK(x) ((x) >> 6)
131 #define GPIO_OFFSET(x) ((x) & GENMASK(5, 0))
132 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x) >> 1)
134 static const struct aspeed_sgpio_bank *to_bank(unsigned int offset)
138 bank = GPIO_BANK(offset);
140 WARN_ON(bank >= ARRAY_SIZE(aspeed_sgpio_banks));
141 return &aspeed_sgpio_banks[bank];
144 static int aspeed_sgpio_init_valid_mask(struct gpio_chip *gc,
145 unsigned long *valid_mask, unsigned int ngpios)
147 bitmap_set(valid_mask, 0, ngpios);
151 static void aspeed_sgpio_irq_init_valid_mask(struct gpio_chip *gc,
152 unsigned long *valid_mask, unsigned int ngpios)
156 /* input GPIOs are even bits */
157 for (i = 0; i < ngpios; i++) {
159 clear_bit(i, valid_mask);
163 static bool aspeed_sgpio_is_input(unsigned int offset)
165 return !(offset % 2);
168 static int aspeed_sgpio_get(struct gpio_chip *gc, unsigned int offset)
170 struct aspeed_sgpio *gpio = gpiochip_get_data(gc);
171 const struct aspeed_sgpio_bank *bank = to_bank(offset);
173 enum aspeed_sgpio_reg reg;
176 raw_spin_lock_irqsave(&gpio->lock, flags);
178 reg = aspeed_sgpio_is_input(offset) ? reg_val : reg_rdata;
179 rc = !!(ioread32(bank_reg(gpio, bank, reg)) & GPIO_BIT(offset));
181 raw_spin_unlock_irqrestore(&gpio->lock, flags);
186 static int sgpio_set_value(struct gpio_chip *gc, unsigned int offset, int val)
188 struct aspeed_sgpio *gpio = gpiochip_get_data(gc);
189 const struct aspeed_sgpio_bank *bank = to_bank(offset);
190 void __iomem *addr_r, *addr_w;
193 if (aspeed_sgpio_is_input(offset))
196 /* Since this is an output, read the cached value from rdata, then
198 addr_r = bank_reg(gpio, bank, reg_rdata);
199 addr_w = bank_reg(gpio, bank, reg_val);
201 reg = ioread32(addr_r);
204 reg |= GPIO_BIT(offset);
206 reg &= ~GPIO_BIT(offset);
208 iowrite32(reg, addr_w);
213 static void aspeed_sgpio_set(struct gpio_chip *gc, unsigned int offset, int val)
215 struct aspeed_sgpio *gpio = gpiochip_get_data(gc);
218 raw_spin_lock_irqsave(&gpio->lock, flags);
220 sgpio_set_value(gc, offset, val);
222 raw_spin_unlock_irqrestore(&gpio->lock, flags);
225 static int aspeed_sgpio_dir_in(struct gpio_chip *gc, unsigned int offset)
227 return aspeed_sgpio_is_input(offset) ? 0 : -EINVAL;
230 static int aspeed_sgpio_dir_out(struct gpio_chip *gc, unsigned int offset, int val)
232 struct aspeed_sgpio *gpio = gpiochip_get_data(gc);
236 /* No special action is required for setting the direction; we'll
237 * error-out in sgpio_set_value if this isn't an output GPIO */
239 raw_spin_lock_irqsave(&gpio->lock, flags);
240 rc = sgpio_set_value(gc, offset, val);
241 raw_spin_unlock_irqrestore(&gpio->lock, flags);
246 static int aspeed_sgpio_get_direction(struct gpio_chip *gc, unsigned int offset)
248 return !!aspeed_sgpio_is_input(offset);
251 static void irqd_to_aspeed_sgpio_data(struct irq_data *d,
252 struct aspeed_sgpio **gpio,
253 const struct aspeed_sgpio_bank **bank,
254 u32 *bit, int *offset)
256 struct aspeed_sgpio *internal;
258 *offset = irqd_to_hwirq(d);
259 internal = irq_data_get_irq_chip_data(d);
263 *bank = to_bank(*offset);
264 *bit = GPIO_BIT(*offset);
267 static void aspeed_sgpio_irq_ack(struct irq_data *d)
269 const struct aspeed_sgpio_bank *bank;
270 struct aspeed_sgpio *gpio;
272 void __iomem *status_addr;
276 irqd_to_aspeed_sgpio_data(d, &gpio, &bank, &bit, &offset);
278 status_addr = bank_reg(gpio, bank, reg_irq_status);
280 raw_spin_lock_irqsave(&gpio->lock, flags);
282 iowrite32(bit, status_addr);
284 raw_spin_unlock_irqrestore(&gpio->lock, flags);
287 static void aspeed_sgpio_irq_set_mask(struct irq_data *d, bool set)
289 const struct aspeed_sgpio_bank *bank;
290 struct aspeed_sgpio *gpio;
296 irqd_to_aspeed_sgpio_data(d, &gpio, &bank, &bit, &offset);
297 addr = bank_reg(gpio, bank, reg_irq_enable);
299 raw_spin_lock_irqsave(&gpio->lock, flags);
301 reg = ioread32(addr);
307 iowrite32(reg, addr);
309 raw_spin_unlock_irqrestore(&gpio->lock, flags);
312 static void aspeed_sgpio_irq_mask(struct irq_data *d)
314 aspeed_sgpio_irq_set_mask(d, false);
317 static void aspeed_sgpio_irq_unmask(struct irq_data *d)
319 aspeed_sgpio_irq_set_mask(d, true);
322 static int aspeed_sgpio_set_type(struct irq_data *d, unsigned int type)
328 const struct aspeed_sgpio_bank *bank;
329 irq_flow_handler_t handler;
330 struct aspeed_sgpio *gpio;
335 irqd_to_aspeed_sgpio_data(d, &gpio, &bank, &bit, &offset);
337 switch (type & IRQ_TYPE_SENSE_MASK) {
338 case IRQ_TYPE_EDGE_BOTH:
341 case IRQ_TYPE_EDGE_RISING:
344 case IRQ_TYPE_EDGE_FALLING:
345 handler = handle_edge_irq;
347 case IRQ_TYPE_LEVEL_HIGH:
350 case IRQ_TYPE_LEVEL_LOW:
352 handler = handle_level_irq;
358 raw_spin_lock_irqsave(&gpio->lock, flags);
360 addr = bank_reg(gpio, bank, reg_irq_type0);
361 reg = ioread32(addr);
362 reg = (reg & ~bit) | type0;
363 iowrite32(reg, addr);
365 addr = bank_reg(gpio, bank, reg_irq_type1);
366 reg = ioread32(addr);
367 reg = (reg & ~bit) | type1;
368 iowrite32(reg, addr);
370 addr = bank_reg(gpio, bank, reg_irq_type2);
371 reg = ioread32(addr);
372 reg = (reg & ~bit) | type2;
373 iowrite32(reg, addr);
375 raw_spin_unlock_irqrestore(&gpio->lock, flags);
377 irq_set_handler_locked(d, handler);
382 static void aspeed_sgpio_irq_handler(struct irq_desc *desc)
384 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
385 struct irq_chip *ic = irq_desc_get_chip(desc);
386 struct aspeed_sgpio *data = gpiochip_get_data(gc);
390 chained_irq_enter(ic, desc);
392 for (i = 0; i < ARRAY_SIZE(aspeed_sgpio_banks); i++) {
393 const struct aspeed_sgpio_bank *bank = &aspeed_sgpio_banks[i];
395 reg = ioread32(bank_reg(data, bank, reg_irq_status));
397 for_each_set_bit(p, ®, 32)
398 generic_handle_domain_irq(gc->irq.domain, (i * 32 + p) * 2);
401 chained_irq_exit(ic, desc);
404 static int aspeed_sgpio_setup_irqs(struct aspeed_sgpio *gpio,
405 struct platform_device *pdev)
408 const struct aspeed_sgpio_bank *bank;
409 struct gpio_irq_chip *irq;
411 rc = platform_get_irq(pdev, 0);
417 /* Disable IRQ and clear Interrupt status registers for all SGPIO Pins. */
418 for (i = 0; i < ARRAY_SIZE(aspeed_sgpio_banks); i++) {
419 bank = &aspeed_sgpio_banks[i];
420 /* disable irq enable bits */
421 iowrite32(0x00000000, bank_reg(gpio, bank, reg_irq_enable));
422 /* clear status bits */
423 iowrite32(0xffffffff, bank_reg(gpio, bank, reg_irq_status));
426 gpio->intc.name = dev_name(&pdev->dev);
427 gpio->intc.irq_ack = aspeed_sgpio_irq_ack;
428 gpio->intc.irq_mask = aspeed_sgpio_irq_mask;
429 gpio->intc.irq_unmask = aspeed_sgpio_irq_unmask;
430 gpio->intc.irq_set_type = aspeed_sgpio_set_type;
432 irq = &gpio->chip.irq;
433 irq->chip = &gpio->intc;
434 irq->init_valid_mask = aspeed_sgpio_irq_init_valid_mask;
435 irq->handler = handle_bad_irq;
436 irq->default_type = IRQ_TYPE_NONE;
437 irq->parent_handler = aspeed_sgpio_irq_handler;
438 irq->parent_handler_data = gpio;
439 irq->parents = &gpio->irq;
440 irq->num_parents = 1;
442 /* Apply default IRQ settings */
443 for (i = 0; i < ARRAY_SIZE(aspeed_sgpio_banks); i++) {
444 bank = &aspeed_sgpio_banks[i];
445 /* set falling or level-low irq */
446 iowrite32(0x00000000, bank_reg(gpio, bank, reg_irq_type0));
447 /* trigger type is edge */
448 iowrite32(0x00000000, bank_reg(gpio, bank, reg_irq_type1));
449 /* single edge trigger */
450 iowrite32(0x00000000, bank_reg(gpio, bank, reg_irq_type2));
456 static const struct aspeed_sgpio_pdata ast2400_sgpio_pdata = {
457 .pin_mask = GENMASK(9, 6),
460 static int aspeed_sgpio_reset_tolerance(struct gpio_chip *chip,
461 unsigned int offset, bool enable)
463 struct aspeed_sgpio *gpio = gpiochip_get_data(chip);
468 reg = bank_reg(gpio, to_bank(offset), reg_tolerance);
470 raw_spin_lock_irqsave(&gpio->lock, flags);
475 val |= GPIO_BIT(offset);
477 val &= ~GPIO_BIT(offset);
481 raw_spin_unlock_irqrestore(&gpio->lock, flags);
486 static int aspeed_sgpio_set_config(struct gpio_chip *chip, unsigned int offset,
487 unsigned long config)
489 unsigned long param = pinconf_to_config_param(config);
490 u32 arg = pinconf_to_config_argument(config);
492 if (param == PIN_CONFIG_PERSIST_STATE)
493 return aspeed_sgpio_reset_tolerance(chip, offset, arg);
498 static const struct aspeed_sgpio_pdata ast2600_sgpiom_pdata = {
499 .pin_mask = GENMASK(10, 6),
502 static const struct of_device_id aspeed_sgpio_of_table[] = {
503 { .compatible = "aspeed,ast2400-sgpio", .data = &ast2400_sgpio_pdata, },
504 { .compatible = "aspeed,ast2500-sgpio", .data = &ast2400_sgpio_pdata, },
505 { .compatible = "aspeed,ast2600-sgpiom", .data = &ast2600_sgpiom_pdata, },
509 MODULE_DEVICE_TABLE(of, aspeed_sgpio_of_table);
511 static int __init aspeed_sgpio_probe(struct platform_device *pdev)
513 u32 nr_gpios, sgpio_freq, sgpio_clk_div, gpio_cnt_regval, pin_mask;
514 const struct aspeed_sgpio_pdata *pdata;
515 struct aspeed_sgpio *gpio;
516 unsigned long apb_freq;
519 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
523 gpio->base = devm_platform_ioremap_resource(pdev, 0);
524 if (IS_ERR(gpio->base))
525 return PTR_ERR(gpio->base);
527 pdata = device_get_match_data(&pdev->dev);
531 pin_mask = pdata->pin_mask;
533 rc = device_property_read_u32(&pdev->dev, "ngpios", &nr_gpios);
535 dev_err(&pdev->dev, "Could not read ngpios property\n");
537 } else if (nr_gpios % 8) {
538 dev_err(&pdev->dev, "Number of GPIOs not multiple of 8: %d\n",
543 rc = device_property_read_u32(&pdev->dev, "bus-frequency", &sgpio_freq);
545 dev_err(&pdev->dev, "Could not read bus-frequency property\n");
549 gpio->pclk = devm_clk_get(&pdev->dev, NULL);
550 if (IS_ERR(gpio->pclk)) {
551 dev_err(&pdev->dev, "devm_clk_get failed\n");
552 return PTR_ERR(gpio->pclk);
555 apb_freq = clk_get_rate(gpio->pclk);
558 * From the datasheet,
559 * SGPIO period = 1/PCLK * 2 * (GPIO254[31:16] + 1)
560 * period = 2 * (GPIO254[31:16] + 1) / PCLK
561 * frequency = 1 / (2 * (GPIO254[31:16] + 1) / PCLK)
562 * frequency = PCLK / (2 * (GPIO254[31:16] + 1))
563 * frequency * 2 * (GPIO254[31:16] + 1) = PCLK
564 * GPIO254[31:16] = PCLK / (frequency * 2) - 1
569 sgpio_clk_div = (apb_freq / (sgpio_freq * 2)) - 1;
571 if (sgpio_clk_div > (1 << 16) - 1)
574 gpio_cnt_regval = ((nr_gpios / 8) << ASPEED_SGPIO_PINS_SHIFT) & pin_mask;
575 iowrite32(FIELD_PREP(ASPEED_SGPIO_CLK_DIV_MASK, sgpio_clk_div) | gpio_cnt_regval |
576 ASPEED_SGPIO_ENABLE, gpio->base + ASPEED_SGPIO_CTRL);
578 raw_spin_lock_init(&gpio->lock);
580 gpio->chip.parent = &pdev->dev;
581 gpio->chip.ngpio = nr_gpios * 2;
582 gpio->chip.init_valid_mask = aspeed_sgpio_init_valid_mask;
583 gpio->chip.direction_input = aspeed_sgpio_dir_in;
584 gpio->chip.direction_output = aspeed_sgpio_dir_out;
585 gpio->chip.get_direction = aspeed_sgpio_get_direction;
586 gpio->chip.request = NULL;
587 gpio->chip.free = NULL;
588 gpio->chip.get = aspeed_sgpio_get;
589 gpio->chip.set = aspeed_sgpio_set;
590 gpio->chip.set_config = aspeed_sgpio_set_config;
591 gpio->chip.label = dev_name(&pdev->dev);
592 gpio->chip.base = -1;
594 aspeed_sgpio_setup_irqs(gpio, pdev);
596 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
603 static struct platform_driver aspeed_sgpio_driver = {
605 .name = KBUILD_MODNAME,
606 .of_match_table = aspeed_sgpio_of_table,
610 module_platform_driver_probe(aspeed_sgpio_driver, aspeed_sgpio_probe);
611 MODULE_DESCRIPTION("Aspeed Serial GPIO Driver");
612 MODULE_LICENSE("GPL");