Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 2 | /* |
| 3 | * AMD Promontory GPIO driver |
| 4 | * |
| 5 | * Copyright (C) 2015 ASMedia Technology Inc. |
| 6 | * Author: YD Tseng <yd_tseng@asmedia.com.tw> |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/gpio/driver.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/acpi.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | |
| 16 | #define PT_TOTAL_GPIO 8 |
Hsu Yuchang | 2ac5eb8 | 2021-12-10 17:03:15 +0800 | [diff] [blame] | 17 | #define PT_TOTAL_GPIO_EX 24 |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 18 | |
| 19 | /* PCI-E MMIO register offsets */ |
| 20 | #define PT_DIRECTION_REG 0x00 |
| 21 | #define PT_INPUTDATA_REG 0x04 |
| 22 | #define PT_OUTPUTDATA_REG 0x08 |
| 23 | #define PT_CLOCKRATE_REG 0x0C |
| 24 | #define PT_SYNC_REG 0x28 |
| 25 | |
| 26 | struct pt_gpio_chip { |
| 27 | struct gpio_chip gc; |
| 28 | void __iomem *reg_base; |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 29 | }; |
| 30 | |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 31 | static int pt_gpio_request(struct gpio_chip *gc, unsigned offset) |
| 32 | { |
Linus Walleij | fb72288 | 2015-12-04 15:29:41 +0100 | [diff] [blame] | 33 | struct pt_gpio_chip *pt_gpio = gpiochip_get_data(gc); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 34 | unsigned long flags; |
| 35 | u32 using_pins; |
| 36 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 37 | dev_dbg(gc->parent, "pt_gpio_request offset=%x\n", offset); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 38 | |
Axel Lin | 574b782 | 2016-02-29 22:00:01 +0800 | [diff] [blame] | 39 | spin_lock_irqsave(&gc->bgpio_lock, flags); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 40 | |
| 41 | using_pins = readl(pt_gpio->reg_base + PT_SYNC_REG); |
| 42 | if (using_pins & BIT(offset)) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 43 | dev_warn(gc->parent, "PT GPIO pin %x reconfigured\n", |
| 44 | offset); |
Axel Lin | 574b782 | 2016-02-29 22:00:01 +0800 | [diff] [blame] | 45 | spin_unlock_irqrestore(&gc->bgpio_lock, flags); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 46 | return -EINVAL; |
| 47 | } |
| 48 | |
| 49 | writel(using_pins | BIT(offset), pt_gpio->reg_base + PT_SYNC_REG); |
| 50 | |
Axel Lin | 574b782 | 2016-02-29 22:00:01 +0800 | [diff] [blame] | 51 | spin_unlock_irqrestore(&gc->bgpio_lock, flags); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static void pt_gpio_free(struct gpio_chip *gc, unsigned offset) |
| 57 | { |
Linus Walleij | fb72288 | 2015-12-04 15:29:41 +0100 | [diff] [blame] | 58 | struct pt_gpio_chip *pt_gpio = gpiochip_get_data(gc); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 59 | unsigned long flags; |
| 60 | u32 using_pins; |
| 61 | |
Axel Lin | 574b782 | 2016-02-29 22:00:01 +0800 | [diff] [blame] | 62 | spin_lock_irqsave(&gc->bgpio_lock, flags); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 63 | |
| 64 | using_pins = readl(pt_gpio->reg_base + PT_SYNC_REG); |
| 65 | using_pins &= ~BIT(offset); |
| 66 | writel(using_pins, pt_gpio->reg_base + PT_SYNC_REG); |
| 67 | |
Axel Lin | 574b782 | 2016-02-29 22:00:01 +0800 | [diff] [blame] | 68 | spin_unlock_irqrestore(&gc->bgpio_lock, flags); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 69 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 70 | dev_dbg(gc->parent, "pt_gpio_free offset=%x\n", offset); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 71 | } |
| 72 | |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 73 | static int pt_gpio_probe(struct platform_device *pdev) |
| 74 | { |
| 75 | struct device *dev = &pdev->dev; |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 76 | struct pt_gpio_chip *pt_gpio; |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 77 | int ret = 0; |
| 78 | |
Rafael J. Wysocki | f0b2731 | 2021-10-13 18:06:40 +0200 | [diff] [blame] | 79 | if (!ACPI_COMPANION(dev)) { |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 80 | dev_err(dev, "PT GPIO device node not found\n"); |
| 81 | return -ENODEV; |
| 82 | } |
| 83 | |
| 84 | pt_gpio = devm_kzalloc(dev, sizeof(struct pt_gpio_chip), GFP_KERNEL); |
| 85 | if (!pt_gpio) |
| 86 | return -ENOMEM; |
| 87 | |
Enrico Weigelt, metux IT consult | bb17a27 | 2019-03-11 19:54:41 +0100 | [diff] [blame] | 88 | pt_gpio->reg_base = devm_platform_ioremap_resource(pdev, 0); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 89 | if (IS_ERR(pt_gpio->reg_base)) { |
Enrico Weigelt, metux IT consult | 2b3fee3 | 2019-06-17 18:49:15 +0200 | [diff] [blame] | 90 | dev_err(dev, "Failed to map MMIO resource for PT GPIO.\n"); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 91 | return PTR_ERR(pt_gpio->reg_base); |
| 92 | } |
| 93 | |
Axel Lin | 574b782 | 2016-02-29 22:00:01 +0800 | [diff] [blame] | 94 | ret = bgpio_init(&pt_gpio->gc, dev, 4, |
| 95 | pt_gpio->reg_base + PT_INPUTDATA_REG, |
| 96 | pt_gpio->reg_base + PT_OUTPUTDATA_REG, NULL, |
| 97 | pt_gpio->reg_base + PT_DIRECTION_REG, NULL, |
| 98 | BGPIOF_READ_OUTPUT_REG_SET); |
| 99 | if (ret) { |
Enrico Weigelt, metux IT consult | 2b3fee3 | 2019-06-17 18:49:15 +0200 | [diff] [blame] | 100 | dev_err(dev, "bgpio_init failed\n"); |
Axel Lin | 574b782 | 2016-02-29 22:00:01 +0800 | [diff] [blame] | 101 | return ret; |
| 102 | } |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 103 | |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 104 | pt_gpio->gc.owner = THIS_MODULE; |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 105 | pt_gpio->gc.request = pt_gpio_request; |
| 106 | pt_gpio->gc.free = pt_gpio_free; |
Hsu Yuchang | 2ac5eb8 | 2021-12-10 17:03:15 +0800 | [diff] [blame] | 107 | pt_gpio->gc.ngpio = (uintptr_t)device_get_match_data(dev); |
Andy Shevchenko | 448cf90 | 2021-12-17 17:39:35 +0100 | [diff] [blame] | 108 | |
Linus Walleij | fb72288 | 2015-12-04 15:29:41 +0100 | [diff] [blame] | 109 | ret = gpiochip_add_data(&pt_gpio->gc, pt_gpio); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 110 | if (ret) { |
Enrico Weigelt, metux IT consult | 2b3fee3 | 2019-06-17 18:49:15 +0200 | [diff] [blame] | 111 | dev_err(dev, "Failed to register GPIO lib\n"); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | platform_set_drvdata(pdev, pt_gpio); |
| 116 | |
| 117 | /* initialize register setting */ |
| 118 | writel(0, pt_gpio->reg_base + PT_SYNC_REG); |
| 119 | writel(0, pt_gpio->reg_base + PT_CLOCKRATE_REG); |
| 120 | |
Enrico Weigelt, metux IT consult | 2b3fee3 | 2019-06-17 18:49:15 +0200 | [diff] [blame] | 121 | dev_dbg(dev, "PT GPIO driver loaded\n"); |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | static int pt_gpio_remove(struct platform_device *pdev) |
| 126 | { |
| 127 | struct pt_gpio_chip *pt_gpio = platform_get_drvdata(pdev); |
| 128 | |
| 129 | gpiochip_remove(&pt_gpio->gc); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static const struct acpi_device_id pt_gpio_acpi_match[] = { |
Hsu Yuchang | 2ac5eb8 | 2021-12-10 17:03:15 +0800 | [diff] [blame] | 135 | { "AMDF030", PT_TOTAL_GPIO }, |
| 136 | { "AMDIF030", PT_TOTAL_GPIO }, |
| 137 | { "AMDIF031", PT_TOTAL_GPIO_EX }, |
YD Tseng | 6057d40f | 2015-10-19 11:07:37 +0800 | [diff] [blame] | 138 | { }, |
| 139 | }; |
| 140 | MODULE_DEVICE_TABLE(acpi, pt_gpio_acpi_match); |
| 141 | |
| 142 | static struct platform_driver pt_gpio_driver = { |
| 143 | .driver = { |
| 144 | .name = "pt-gpio", |
| 145 | .acpi_match_table = ACPI_PTR(pt_gpio_acpi_match), |
| 146 | }, |
| 147 | .probe = pt_gpio_probe, |
| 148 | .remove = pt_gpio_remove, |
| 149 | }; |
| 150 | |
| 151 | module_platform_driver(pt_gpio_driver); |
| 152 | |
| 153 | MODULE_LICENSE("GPL"); |
| 154 | MODULE_AUTHOR("YD Tseng <yd_tseng@asmedia.com.tw>"); |
| 155 | MODULE_DESCRIPTION("AMD Promontory GPIO Driver"); |