|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
| 2 | + |
| 3 | +/* |
| 4 | + * GPIO driver for the AMD G series FCH (eg. GX-412TC) |
| 5 | + * |
| 6 | + * Copyright (C) 2018 metux IT consult |
| 7 | + * Author: Enrico Weigelt, metux IT consult <info@metux.net> |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +#include <linux/err.h> |
| 12 | +#include <linux/io.h> |
| 13 | +#include <linux/kernel.h> |
| 14 | +#include <linux/module.h> |
| 15 | +#include <linux/platform_device.h> |
| 16 | +#include <linux/gpio/driver.h> |
| 17 | +#include <linux/platform_data/gpio/gpio-amd-fch.h> |
| 18 | +#include <linux/spinlock.h> |
| 19 | + |
| 20 | +#define AMD_FCH_MMIO_BASE 0xFED80000 |
| 21 | +#define AMD_FCH_GPIO_BANK0_BASE 0x1500 |
| 22 | +#define AMD_FCH_GPIO_SIZE 0x0300 |
| 23 | + |
| 24 | +#define AMD_FCH_GPIO_FLAG_DIRECTION BIT(23) |
| 25 | +#define AMD_FCH_GPIO_FLAG_WRITE BIT(22) |
| 26 | +#define AMD_FCH_GPIO_FLAG_READ BIT(16) |
| 27 | + |
| 28 | +static const struct resource amd_fch_gpio_iores = |
| 29 | + DEFINE_RES_MEM_NAMED( |
| 30 | + AMD_FCH_MMIO_BASE + AMD_FCH_GPIO_BANK0_BASE, |
| 31 | + AMD_FCH_GPIO_SIZE, |
| 32 | + "amd-fch-gpio-iomem"); |
| 33 | + |
| 34 | +struct amd_fch_gpio_priv { |
| 35 | + struct platform_device *pdev; |
| 36 | + struct gpio_chip gc; |
| 37 | + void __iomem *base; |
| 38 | + struct amd_fch_gpio_pdata *pdata; |
| 39 | + spinlock_t lock; |
| 40 | +}; |
| 41 | + |
| 42 | +static void *amd_fch_gpio_addr(struct amd_fch_gpio_priv *priv, |
| 43 | + unsigned int gpio) |
| 44 | +{ |
| 45 | + return priv->base + priv->pdata->gpio_reg[gpio]*sizeof(u32); |
| 46 | +} |
| 47 | + |
| 48 | +static int amd_fch_gpio_direction_input(struct gpio_chip *gc, |
| 49 | + unsigned int offset) |
| 50 | +{ |
| 51 | + unsigned long flags; |
| 52 | + struct amd_fch_gpio_priv *priv = gpiochip_get_data(gc); |
| 53 | + void *ptr = amd_fch_gpio_addr(priv, offset); |
| 54 | + |
| 55 | + spin_lock_irqsave(&priv->lock, flags); |
| 56 | + writel_relaxed(readl_relaxed(ptr) & ~AMD_FCH_GPIO_FLAG_DIRECTION, ptr); |
| 57 | + spin_unlock_irqrestore(&priv->lock, flags); |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +static int amd_fch_gpio_direction_output(struct gpio_chip *gc, |
| 63 | + unsigned int gpio, int value) |
| 64 | +{ |
| 65 | + unsigned long flags; |
| 66 | + struct amd_fch_gpio_priv *priv = gpiochip_get_data(gc); |
| 67 | + void *ptr = amd_fch_gpio_addr(priv, gpio); |
| 68 | + |
| 69 | + spin_lock_irqsave(&priv->lock, flags); |
| 70 | + writel_relaxed(readl_relaxed(ptr) | AMD_FCH_GPIO_FLAG_DIRECTION, ptr); |
| 71 | + spin_unlock_irqrestore(&priv->lock, flags); |
| 72 | + |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +static int amd_fch_gpio_get_direction(struct gpio_chip *gc, unsigned int gpio) |
| 77 | +{ |
| 78 | + int ret; |
| 79 | + unsigned long flags; |
| 80 | + struct amd_fch_gpio_priv *priv = gpiochip_get_data(gc); |
| 81 | + void *ptr = amd_fch_gpio_addr(priv, gpio); |
| 82 | + |
| 83 | + spin_lock_irqsave(&priv->lock, flags); |
| 84 | + ret = (readl_relaxed(ptr) & AMD_FCH_GPIO_FLAG_DIRECTION); |
| 85 | + spin_unlock_irqrestore(&priv->lock, flags); |
| 86 | + |
| 87 | + return ret; |
| 88 | +} |
| 89 | + |
| 90 | +static void amd_fch_gpio_set(struct gpio_chip *gc, |
| 91 | + unsigned int gpio, int value) |
| 92 | +{ |
| 93 | + unsigned long flags; |
| 94 | + struct amd_fch_gpio_priv *priv = gpiochip_get_data(gc); |
| 95 | + void *ptr = amd_fch_gpio_addr(priv, gpio); |
| 96 | + u32 mask; |
| 97 | + |
| 98 | + spin_lock_irqsave(&priv->lock, flags); |
| 99 | + |
| 100 | + mask = readl_relaxed(ptr); |
| 101 | + if (value) |
| 102 | + mask |= AMD_FCH_GPIO_FLAG_WRITE; |
| 103 | + else |
| 104 | + mask &= ~AMD_FCH_GPIO_FLAG_WRITE; |
| 105 | + writel_relaxed(mask, ptr); |
| 106 | + |
| 107 | + spin_unlock_irqrestore(&priv->lock, flags); |
| 108 | +} |
| 109 | + |
| 110 | +static int amd_fch_gpio_get(struct gpio_chip *gc, |
| 111 | + unsigned int offset) |
| 112 | +{ |
| 113 | + unsigned long flags; |
| 114 | + int ret; |
| 115 | + struct amd_fch_gpio_priv *priv = gpiochip_get_data(gc); |
| 116 | + void *ptr = amd_fch_gpio_addr(priv, offset); |
| 117 | + |
| 118 | + spin_lock_irqsave(&priv->lock, flags); |
| 119 | + ret = (readl_relaxed(ptr) & AMD_FCH_GPIO_FLAG_READ); |
| 120 | + spin_unlock_irqrestore(&priv->lock, flags); |
| 121 | + |
| 122 | + return ret; |
| 123 | +} |
| 124 | + |
| 125 | +static int amd_fch_gpio_request(struct gpio_chip *chip, |
| 126 | + unsigned int gpio_pin) |
| 127 | +{ |
| 128 | + return 0; |
| 129 | +} |
| 130 | + |
| 131 | +static int amd_fch_gpio_probe(struct platform_device *pdev) |
| 132 | +{ |
| 133 | + struct amd_fch_gpio_priv *priv; |
| 134 | + struct amd_fch_gpio_pdata *pdata; |
| 135 | + |
| 136 | + pdata = dev_get_platdata(&pdev->dev); |
| 137 | + if (!pdata) { |
| 138 | + dev_err(&pdev->dev, "no platform_data\n"); |
| 139 | + return -ENOENT; |
| 140 | + } |
| 141 | + |
| 142 | + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 143 | + if (!priv) |
| 144 | + return -ENOMEM; |
| 145 | + |
| 146 | + priv->pdata = pdata; |
| 147 | + priv->pdev = pdev; |
| 148 | + |
| 149 | + priv->gc.owner = THIS_MODULE; |
| 150 | + priv->gc.parent = &pdev->dev; |
| 151 | + priv->gc.label = dev_name(&pdev->dev); |
| 152 | + priv->gc.ngpio = priv->pdata->gpio_num; |
| 153 | + priv->gc.names = priv->pdata->gpio_names; |
| 154 | + priv->gc.base = -1; |
| 155 | + priv->gc.request = amd_fch_gpio_request; |
| 156 | + priv->gc.direction_input = amd_fch_gpio_direction_input; |
| 157 | + priv->gc.direction_output = amd_fch_gpio_direction_output; |
| 158 | + priv->gc.get_direction = amd_fch_gpio_get_direction; |
| 159 | + priv->gc.get = amd_fch_gpio_get; |
| 160 | + priv->gc.set = amd_fch_gpio_set; |
| 161 | + |
| 162 | + spin_lock_init(&priv->lock); |
| 163 | + |
| 164 | + priv->base = devm_ioremap_resource(&pdev->dev, &amd_fch_gpio_iores); |
| 165 | + if (IS_ERR(priv->base)) |
| 166 | + return PTR_ERR(priv->base); |
| 167 | + |
| 168 | + platform_set_drvdata(pdev, priv); |
| 169 | + |
| 170 | + return devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv); |
| 171 | +} |
| 172 | + |
| 173 | +static struct platform_driver amd_fch_gpio_driver = { |
| 174 | + .driver = { |
| 175 | + .name = AMD_FCH_GPIO_DRIVER_NAME, |
| 176 | + }, |
| 177 | + .probe = amd_fch_gpio_probe, |
| 178 | +}; |
| 179 | + |
| 180 | +module_platform_driver(amd_fch_gpio_driver); |
| 181 | + |
| 182 | +MODULE_AUTHOR("Enrico Weigelt, metux IT consult <info@metux.net>"); |
| 183 | +MODULE_DESCRIPTION("AMD G-series FCH GPIO driver"); |
| 184 | +MODULE_LICENSE("GPL"); |
| 185 | +MODULE_ALIAS("platform:" AMD_FCH_GPIO_DRIVER_NAME); |
0 commit comments