blob: 1ffd7c2d1285ccf04f68c1d382ffc574654c85c3 [file] [log] [blame]
YD Tseng6057d402015-10-19 11:07:37 +08001/*
2 * AMD Promontory GPIO driver
3 *
4 * Copyright (C) 2015 ASMedia Technology Inc.
5 * Author: YD Tseng <yd_tseng@asmedia.com.tw>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/gpio/driver.h>
15#include <linux/spinlock.h>
16#include <linux/acpi.h>
17#include <linux/platform_device.h>
18
19#define PT_TOTAL_GPIO 8
20
21/* PCI-E MMIO register offsets */
22#define PT_DIRECTION_REG 0x00
23#define PT_INPUTDATA_REG 0x04
24#define PT_OUTPUTDATA_REG 0x08
25#define PT_CLOCKRATE_REG 0x0C
26#define PT_SYNC_REG 0x28
27
28struct pt_gpio_chip {
29 struct gpio_chip gc;
30 void __iomem *reg_base;
YD Tseng6057d402015-10-19 11:07:37 +080031};
32
YD Tseng6057d402015-10-19 11:07:37 +080033static int pt_gpio_request(struct gpio_chip *gc, unsigned offset)
34{
Linus Walleijfb722882015-12-04 15:29:41 +010035 struct pt_gpio_chip *pt_gpio = gpiochip_get_data(gc);
YD Tseng6057d402015-10-19 11:07:37 +080036 unsigned long flags;
37 u32 using_pins;
38
Linus Walleij58383c782015-11-04 09:56:26 +010039 dev_dbg(gc->parent, "pt_gpio_request offset=%x\n", offset);
YD Tseng6057d402015-10-19 11:07:37 +080040
Axel Lin574b7822016-02-29 22:00:01 +080041 spin_lock_irqsave(&gc->bgpio_lock, flags);
YD Tseng6057d402015-10-19 11:07:37 +080042
43 using_pins = readl(pt_gpio->reg_base + PT_SYNC_REG);
44 if (using_pins & BIT(offset)) {
Linus Walleij58383c782015-11-04 09:56:26 +010045 dev_warn(gc->parent, "PT GPIO pin %x reconfigured\n",
46 offset);
Axel Lin574b7822016-02-29 22:00:01 +080047 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
YD Tseng6057d402015-10-19 11:07:37 +080048 return -EINVAL;
49 }
50
51 writel(using_pins | BIT(offset), pt_gpio->reg_base + PT_SYNC_REG);
52
Axel Lin574b7822016-02-29 22:00:01 +080053 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
YD Tseng6057d402015-10-19 11:07:37 +080054
55 return 0;
56}
57
58static void pt_gpio_free(struct gpio_chip *gc, unsigned offset)
59{
Linus Walleijfb722882015-12-04 15:29:41 +010060 struct pt_gpio_chip *pt_gpio = gpiochip_get_data(gc);
YD Tseng6057d402015-10-19 11:07:37 +080061 unsigned long flags;
62 u32 using_pins;
63
Axel Lin574b7822016-02-29 22:00:01 +080064 spin_lock_irqsave(&gc->bgpio_lock, flags);
YD Tseng6057d402015-10-19 11:07:37 +080065
66 using_pins = readl(pt_gpio->reg_base + PT_SYNC_REG);
67 using_pins &= ~BIT(offset);
68 writel(using_pins, pt_gpio->reg_base + PT_SYNC_REG);
69
Axel Lin574b7822016-02-29 22:00:01 +080070 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
YD Tseng6057d402015-10-19 11:07:37 +080071
Linus Walleij58383c782015-11-04 09:56:26 +010072 dev_dbg(gc->parent, "pt_gpio_free offset=%x\n", offset);
YD Tseng6057d402015-10-19 11:07:37 +080073}
74
YD Tseng6057d402015-10-19 11:07:37 +080075static int pt_gpio_probe(struct platform_device *pdev)
76{
77 struct device *dev = &pdev->dev;
78 struct acpi_device *acpi_dev;
79 acpi_handle handle = ACPI_HANDLE(dev);
80 struct pt_gpio_chip *pt_gpio;
YD Tseng6057d402015-10-19 11:07:37 +080081 int ret = 0;
82
83 if (acpi_bus_get_device(handle, &acpi_dev)) {
84 dev_err(dev, "PT GPIO device node not found\n");
85 return -ENODEV;
86 }
87
88 pt_gpio = devm_kzalloc(dev, sizeof(struct pt_gpio_chip), GFP_KERNEL);
89 if (!pt_gpio)
90 return -ENOMEM;
91
Enrico Weigelt, metux IT consultbb17a272019-03-11 19:54:41 +010092 pt_gpio->reg_base = devm_platform_ioremap_resource(pdev, 0);
YD Tseng6057d402015-10-19 11:07:37 +080093 if (IS_ERR(pt_gpio->reg_base)) {
94 dev_err(&pdev->dev, "Failed to map MMIO resource for PT GPIO.\n");
95 return PTR_ERR(pt_gpio->reg_base);
96 }
97
Axel Lin574b7822016-02-29 22:00:01 +080098 ret = bgpio_init(&pt_gpio->gc, dev, 4,
99 pt_gpio->reg_base + PT_INPUTDATA_REG,
100 pt_gpio->reg_base + PT_OUTPUTDATA_REG, NULL,
101 pt_gpio->reg_base + PT_DIRECTION_REG, NULL,
102 BGPIOF_READ_OUTPUT_REG_SET);
103 if (ret) {
104 dev_err(&pdev->dev, "bgpio_init failed\n");
105 return ret;
106 }
YD Tseng6057d402015-10-19 11:07:37 +0800107
YD Tseng6057d402015-10-19 11:07:37 +0800108 pt_gpio->gc.owner = THIS_MODULE;
YD Tseng6057d402015-10-19 11:07:37 +0800109 pt_gpio->gc.request = pt_gpio_request;
110 pt_gpio->gc.free = pt_gpio_free;
YD Tseng6057d402015-10-19 11:07:37 +0800111 pt_gpio->gc.ngpio = PT_TOTAL_GPIO;
112#if defined(CONFIG_OF_GPIO)
113 pt_gpio->gc.of_node = pdev->dev.of_node;
114#endif
Linus Walleijfb722882015-12-04 15:29:41 +0100115 ret = gpiochip_add_data(&pt_gpio->gc, pt_gpio);
YD Tseng6057d402015-10-19 11:07:37 +0800116 if (ret) {
117 dev_err(&pdev->dev, "Failed to register GPIO lib\n");
118 return ret;
119 }
120
121 platform_set_drvdata(pdev, pt_gpio);
122
123 /* initialize register setting */
124 writel(0, pt_gpio->reg_base + PT_SYNC_REG);
125 writel(0, pt_gpio->reg_base + PT_CLOCKRATE_REG);
126
127 dev_dbg(&pdev->dev, "PT GPIO driver loaded\n");
128 return ret;
129}
130
131static int pt_gpio_remove(struct platform_device *pdev)
132{
133 struct pt_gpio_chip *pt_gpio = platform_get_drvdata(pdev);
134
135 gpiochip_remove(&pt_gpio->gc);
136
137 return 0;
138}
139
140static const struct acpi_device_id pt_gpio_acpi_match[] = {
141 { "AMDF030", 0 },
YD Tsengca273792016-03-17 11:35:57 +0800142 { "AMDIF030", 0 },
YD Tseng6057d402015-10-19 11:07:37 +0800143 { },
144};
145MODULE_DEVICE_TABLE(acpi, pt_gpio_acpi_match);
146
147static struct platform_driver pt_gpio_driver = {
148 .driver = {
149 .name = "pt-gpio",
150 .acpi_match_table = ACPI_PTR(pt_gpio_acpi_match),
151 },
152 .probe = pt_gpio_probe,
153 .remove = pt_gpio_remove,
154};
155
156module_platform_driver(pt_gpio_driver);
157
158MODULE_LICENSE("GPL");
159MODULE_AUTHOR("YD Tseng <yd_tseng@asmedia.com.tw>");
160MODULE_DESCRIPTION("AMD Promontory GPIO Driver");