blob: bbf53e2891412a2e5dc87696a898bc01a3372cdc [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
YD Tseng6057d40f2015-10-19 11:07:37 +08002/*
3 * AMD Promontory GPIO driver
4 *
5 * Copyright (C) 2015 ASMedia Technology Inc.
6 * Author: YD Tseng <yd_tseng@asmedia.com.tw>
YD Tseng6057d40f2015-10-19 11:07:37 +08007 */
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
17
18/* PCI-E MMIO register offsets */
19#define PT_DIRECTION_REG 0x00
20#define PT_INPUTDATA_REG 0x04
21#define PT_OUTPUTDATA_REG 0x08
22#define PT_CLOCKRATE_REG 0x0C
23#define PT_SYNC_REG 0x28
24
25struct pt_gpio_chip {
26 struct gpio_chip gc;
27 void __iomem *reg_base;
YD Tseng6057d40f2015-10-19 11:07:37 +080028};
29
YD Tseng6057d40f2015-10-19 11:07:37 +080030static int pt_gpio_request(struct gpio_chip *gc, unsigned offset)
31{
Linus Walleijfb722882015-12-04 15:29:41 +010032 struct pt_gpio_chip *pt_gpio = gpiochip_get_data(gc);
YD Tseng6057d40f2015-10-19 11:07:37 +080033 unsigned long flags;
34 u32 using_pins;
35
Linus Walleij58383c782015-11-04 09:56:26 +010036 dev_dbg(gc->parent, "pt_gpio_request offset=%x\n", offset);
YD Tseng6057d40f2015-10-19 11:07:37 +080037
Axel Lin574b7822016-02-29 22:00:01 +080038 spin_lock_irqsave(&gc->bgpio_lock, flags);
YD Tseng6057d40f2015-10-19 11:07:37 +080039
40 using_pins = readl(pt_gpio->reg_base + PT_SYNC_REG);
41 if (using_pins & BIT(offset)) {
Linus Walleij58383c782015-11-04 09:56:26 +010042 dev_warn(gc->parent, "PT GPIO pin %x reconfigured\n",
43 offset);
Axel Lin574b7822016-02-29 22:00:01 +080044 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
YD Tseng6057d40f2015-10-19 11:07:37 +080045 return -EINVAL;
46 }
47
48 writel(using_pins | BIT(offset), pt_gpio->reg_base + PT_SYNC_REG);
49
Axel Lin574b7822016-02-29 22:00:01 +080050 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
YD Tseng6057d40f2015-10-19 11:07:37 +080051
52 return 0;
53}
54
55static void pt_gpio_free(struct gpio_chip *gc, unsigned offset)
56{
Linus Walleijfb722882015-12-04 15:29:41 +010057 struct pt_gpio_chip *pt_gpio = gpiochip_get_data(gc);
YD Tseng6057d40f2015-10-19 11:07:37 +080058 unsigned long flags;
59 u32 using_pins;
60
Axel Lin574b7822016-02-29 22:00:01 +080061 spin_lock_irqsave(&gc->bgpio_lock, flags);
YD Tseng6057d40f2015-10-19 11:07:37 +080062
63 using_pins = readl(pt_gpio->reg_base + PT_SYNC_REG);
64 using_pins &= ~BIT(offset);
65 writel(using_pins, pt_gpio->reg_base + PT_SYNC_REG);
66
Axel Lin574b7822016-02-29 22:00:01 +080067 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
YD Tseng6057d40f2015-10-19 11:07:37 +080068
Linus Walleij58383c782015-11-04 09:56:26 +010069 dev_dbg(gc->parent, "pt_gpio_free offset=%x\n", offset);
YD Tseng6057d40f2015-10-19 11:07:37 +080070}
71
YD Tseng6057d40f2015-10-19 11:07:37 +080072static int pt_gpio_probe(struct platform_device *pdev)
73{
74 struct device *dev = &pdev->dev;
YD Tseng6057d40f2015-10-19 11:07:37 +080075 struct pt_gpio_chip *pt_gpio;
YD Tseng6057d40f2015-10-19 11:07:37 +080076 int ret = 0;
77
Rafael J. Wysockif0b27312021-10-13 18:06:40 +020078 if (!ACPI_COMPANION(dev)) {
YD Tseng6057d40f2015-10-19 11:07:37 +080079 dev_err(dev, "PT GPIO device node not found\n");
80 return -ENODEV;
81 }
82
83 pt_gpio = devm_kzalloc(dev, sizeof(struct pt_gpio_chip), GFP_KERNEL);
84 if (!pt_gpio)
85 return -ENOMEM;
86
Enrico Weigelt, metux IT consultbb17a272019-03-11 19:54:41 +010087 pt_gpio->reg_base = devm_platform_ioremap_resource(pdev, 0);
YD Tseng6057d40f2015-10-19 11:07:37 +080088 if (IS_ERR(pt_gpio->reg_base)) {
Enrico Weigelt, metux IT consult2b3fee32019-06-17 18:49:15 +020089 dev_err(dev, "Failed to map MMIO resource for PT GPIO.\n");
YD Tseng6057d40f2015-10-19 11:07:37 +080090 return PTR_ERR(pt_gpio->reg_base);
91 }
92
Axel Lin574b7822016-02-29 22:00:01 +080093 ret = bgpio_init(&pt_gpio->gc, dev, 4,
94 pt_gpio->reg_base + PT_INPUTDATA_REG,
95 pt_gpio->reg_base + PT_OUTPUTDATA_REG, NULL,
96 pt_gpio->reg_base + PT_DIRECTION_REG, NULL,
97 BGPIOF_READ_OUTPUT_REG_SET);
98 if (ret) {
Enrico Weigelt, metux IT consult2b3fee32019-06-17 18:49:15 +020099 dev_err(dev, "bgpio_init failed\n");
Axel Lin574b7822016-02-29 22:00:01 +0800100 return ret;
101 }
YD Tseng6057d40f2015-10-19 11:07:37 +0800102
YD Tseng6057d40f2015-10-19 11:07:37 +0800103 pt_gpio->gc.owner = THIS_MODULE;
YD Tseng6057d40f2015-10-19 11:07:37 +0800104 pt_gpio->gc.request = pt_gpio_request;
105 pt_gpio->gc.free = pt_gpio_free;
YD Tseng6057d40f2015-10-19 11:07:37 +0800106 pt_gpio->gc.ngpio = PT_TOTAL_GPIO;
107#if defined(CONFIG_OF_GPIO)
Enrico Weigelt, metux IT consult2b3fee32019-06-17 18:49:15 +0200108 pt_gpio->gc.of_node = dev->of_node;
YD Tseng6057d40f2015-10-19 11:07:37 +0800109#endif
Linus Walleijfb722882015-12-04 15:29:41 +0100110 ret = gpiochip_add_data(&pt_gpio->gc, pt_gpio);
YD Tseng6057d40f2015-10-19 11:07:37 +0800111 if (ret) {
Enrico Weigelt, metux IT consult2b3fee32019-06-17 18:49:15 +0200112 dev_err(dev, "Failed to register GPIO lib\n");
YD Tseng6057d40f2015-10-19 11:07:37 +0800113 return ret;
114 }
115
116 platform_set_drvdata(pdev, pt_gpio);
117
118 /* initialize register setting */
119 writel(0, pt_gpio->reg_base + PT_SYNC_REG);
120 writel(0, pt_gpio->reg_base + PT_CLOCKRATE_REG);
121
Enrico Weigelt, metux IT consult2b3fee32019-06-17 18:49:15 +0200122 dev_dbg(dev, "PT GPIO driver loaded\n");
YD Tseng6057d40f2015-10-19 11:07:37 +0800123 return ret;
124}
125
126static int pt_gpio_remove(struct platform_device *pdev)
127{
128 struct pt_gpio_chip *pt_gpio = platform_get_drvdata(pdev);
129
130 gpiochip_remove(&pt_gpio->gc);
131
132 return 0;
133}
134
135static const struct acpi_device_id pt_gpio_acpi_match[] = {
136 { "AMDF030", 0 },
YD Tsengca273792016-03-17 11:35:57 +0800137 { "AMDIF030", 0 },
YD Tseng6057d40f2015-10-19 11:07:37 +0800138 { },
139};
140MODULE_DEVICE_TABLE(acpi, pt_gpio_acpi_match);
141
142static 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
151module_platform_driver(pt_gpio_driver);
152
153MODULE_LICENSE("GPL");
154MODULE_AUTHOR("YD Tseng <yd_tseng@asmedia.com.tw>");
155MODULE_DESCRIPTION("AMD Promontory GPIO Driver");