blob: 13f4e2af3800749c07be1d5a125c88472935ad30 [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
Hsu Yuchang2ac5eb82021-12-10 17:03:15 +080017#define PT_TOTAL_GPIO_EX 24
YD Tseng6057d40f2015-10-19 11:07:37 +080018
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
26struct pt_gpio_chip {
27 struct gpio_chip gc;
28 void __iomem *reg_base;
YD Tseng6057d40f2015-10-19 11:07:37 +080029};
30
YD Tseng6057d40f2015-10-19 11:07:37 +080031static int pt_gpio_request(struct gpio_chip *gc, unsigned offset)
32{
Linus Walleijfb722882015-12-04 15:29:41 +010033 struct pt_gpio_chip *pt_gpio = gpiochip_get_data(gc);
YD Tseng6057d40f2015-10-19 11:07:37 +080034 unsigned long flags;
35 u32 using_pins;
36
Linus Walleij58383c782015-11-04 09:56:26 +010037 dev_dbg(gc->parent, "pt_gpio_request offset=%x\n", offset);
YD Tseng6057d40f2015-10-19 11:07:37 +080038
Axel Lin574b7822016-02-29 22:00:01 +080039 spin_lock_irqsave(&gc->bgpio_lock, flags);
YD Tseng6057d40f2015-10-19 11:07:37 +080040
41 using_pins = readl(pt_gpio->reg_base + PT_SYNC_REG);
42 if (using_pins & BIT(offset)) {
Linus Walleij58383c782015-11-04 09:56:26 +010043 dev_warn(gc->parent, "PT GPIO pin %x reconfigured\n",
44 offset);
Axel Lin574b7822016-02-29 22:00:01 +080045 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
YD Tseng6057d40f2015-10-19 11:07:37 +080046 return -EINVAL;
47 }
48
49 writel(using_pins | BIT(offset), pt_gpio->reg_base + PT_SYNC_REG);
50
Axel Lin574b7822016-02-29 22:00:01 +080051 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
YD Tseng6057d40f2015-10-19 11:07:37 +080052
53 return 0;
54}
55
56static void pt_gpio_free(struct gpio_chip *gc, unsigned offset)
57{
Linus Walleijfb722882015-12-04 15:29:41 +010058 struct pt_gpio_chip *pt_gpio = gpiochip_get_data(gc);
YD Tseng6057d40f2015-10-19 11:07:37 +080059 unsigned long flags;
60 u32 using_pins;
61
Axel Lin574b7822016-02-29 22:00:01 +080062 spin_lock_irqsave(&gc->bgpio_lock, flags);
YD Tseng6057d40f2015-10-19 11:07:37 +080063
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 Lin574b7822016-02-29 22:00:01 +080068 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
YD Tseng6057d40f2015-10-19 11:07:37 +080069
Linus Walleij58383c782015-11-04 09:56:26 +010070 dev_dbg(gc->parent, "pt_gpio_free offset=%x\n", offset);
YD Tseng6057d40f2015-10-19 11:07:37 +080071}
72
YD Tseng6057d40f2015-10-19 11:07:37 +080073static int pt_gpio_probe(struct platform_device *pdev)
74{
75 struct device *dev = &pdev->dev;
YD Tseng6057d40f2015-10-19 11:07:37 +080076 struct pt_gpio_chip *pt_gpio;
YD Tseng6057d40f2015-10-19 11:07:37 +080077 int ret = 0;
78
Rafael J. Wysockif0b27312021-10-13 18:06:40 +020079 if (!ACPI_COMPANION(dev)) {
YD Tseng6057d40f2015-10-19 11:07:37 +080080 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 consultbb17a272019-03-11 19:54:41 +010088 pt_gpio->reg_base = devm_platform_ioremap_resource(pdev, 0);
YD Tseng6057d40f2015-10-19 11:07:37 +080089 if (IS_ERR(pt_gpio->reg_base)) {
Enrico Weigelt, metux IT consult2b3fee32019-06-17 18:49:15 +020090 dev_err(dev, "Failed to map MMIO resource for PT GPIO.\n");
YD Tseng6057d40f2015-10-19 11:07:37 +080091 return PTR_ERR(pt_gpio->reg_base);
92 }
93
Axel Lin574b7822016-02-29 22:00:01 +080094 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 consult2b3fee32019-06-17 18:49:15 +0200100 dev_err(dev, "bgpio_init failed\n");
Axel Lin574b7822016-02-29 22:00:01 +0800101 return ret;
102 }
YD Tseng6057d40f2015-10-19 11:07:37 +0800103
YD Tseng6057d40f2015-10-19 11:07:37 +0800104 pt_gpio->gc.owner = THIS_MODULE;
YD Tseng6057d40f2015-10-19 11:07:37 +0800105 pt_gpio->gc.request = pt_gpio_request;
106 pt_gpio->gc.free = pt_gpio_free;
Hsu Yuchang2ac5eb82021-12-10 17:03:15 +0800107 pt_gpio->gc.ngpio = (uintptr_t)device_get_match_data(dev);
YD Tseng6057d40f2015-10-19 11:07:37 +0800108#if defined(CONFIG_OF_GPIO)
Enrico Weigelt, metux IT consult2b3fee32019-06-17 18:49:15 +0200109 pt_gpio->gc.of_node = dev->of_node;
YD Tseng6057d40f2015-10-19 11:07:37 +0800110#endif
Linus Walleijfb722882015-12-04 15:29:41 +0100111 ret = gpiochip_add_data(&pt_gpio->gc, pt_gpio);
YD Tseng6057d40f2015-10-19 11:07:37 +0800112 if (ret) {
Enrico Weigelt, metux IT consult2b3fee32019-06-17 18:49:15 +0200113 dev_err(dev, "Failed to register GPIO lib\n");
YD Tseng6057d40f2015-10-19 11:07:37 +0800114 return ret;
115 }
116
117 platform_set_drvdata(pdev, pt_gpio);
118
119 /* initialize register setting */
120 writel(0, pt_gpio->reg_base + PT_SYNC_REG);
121 writel(0, pt_gpio->reg_base + PT_CLOCKRATE_REG);
122
Enrico Weigelt, metux IT consult2b3fee32019-06-17 18:49:15 +0200123 dev_dbg(dev, "PT GPIO driver loaded\n");
YD Tseng6057d40f2015-10-19 11:07:37 +0800124 return ret;
125}
126
127static int pt_gpio_remove(struct platform_device *pdev)
128{
129 struct pt_gpio_chip *pt_gpio = platform_get_drvdata(pdev);
130
131 gpiochip_remove(&pt_gpio->gc);
132
133 return 0;
134}
135
136static const struct acpi_device_id pt_gpio_acpi_match[] = {
Hsu Yuchang2ac5eb82021-12-10 17:03:15 +0800137 { "AMDF030", PT_TOTAL_GPIO },
138 { "AMDIF030", PT_TOTAL_GPIO },
139 { "AMDIF031", PT_TOTAL_GPIO_EX },
YD Tseng6057d40f2015-10-19 11:07:37 +0800140 { },
141};
142MODULE_DEVICE_TABLE(acpi, pt_gpio_acpi_match);
143
144static struct platform_driver pt_gpio_driver = {
145 .driver = {
146 .name = "pt-gpio",
147 .acpi_match_table = ACPI_PTR(pt_gpio_acpi_match),
148 },
149 .probe = pt_gpio_probe,
150 .remove = pt_gpio_remove,
151};
152
153module_platform_driver(pt_gpio_driver);
154
155MODULE_LICENSE("GPL");
156MODULE_AUTHOR("YD Tseng <yd_tseng@asmedia.com.tw>");
157MODULE_DESCRIPTION("AMD Promontory GPIO Driver");