gpio-mb86s7x.c 5.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8
/*
 *  linux/drivers/gpio/gpio-mb86s7x.c
 *
 *  Copyright (C) 2015 Fujitsu Semiconductor Limited
 *  Copyright (C) 2015 Linaro Ltd.
 */

9
#include <linux/acpi.h>
10 11 12
#include <linux/io.h>
#include <linux/init.h>
#include <linux/clk.h>
13
#include <linux/module.h>
14 15 16 17 18 19 20 21 22
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/of_device.h>
#include <linux/gpio/driver.h>
#include <linux/platform_device.h>
#include <linux/spinlock.h>
#include <linux/slab.h>

23
#include "gpiolib.h"
24
#include "gpiolib-acpi.h"
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
 * Only first 8bits of a register correspond to each pin,
 * so there are 4 registers for 32 pins.
 */
#define PDR(x)	(0x0 + x / 8 * 4)
#define DDR(x)	(0x10 + x / 8 * 4)
#define PFR(x)	(0x20 + x / 8 * 4)

#define OFFSET(x)	BIT((x) % 8)

struct mb86s70_gpio_chip {
	struct gpio_chip gc;
	void __iomem *base;
	struct clk *clk;
	spinlock_t lock;
};

static int mb86s70_gpio_request(struct gpio_chip *gc, unsigned gpio)
{
45
	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	unsigned long flags;
	u32 val;

	spin_lock_irqsave(&gchip->lock, flags);

	val = readl(gchip->base + PFR(gpio));
	val &= ~OFFSET(gpio);
	writel(val, gchip->base + PFR(gpio));

	spin_unlock_irqrestore(&gchip->lock, flags);

	return 0;
}

static void mb86s70_gpio_free(struct gpio_chip *gc, unsigned gpio)
{
62
	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
63 64 65 66 67 68 69 70 71 72 73 74 75 76
	unsigned long flags;
	u32 val;

	spin_lock_irqsave(&gchip->lock, flags);

	val = readl(gchip->base + PFR(gpio));
	val |= OFFSET(gpio);
	writel(val, gchip->base + PFR(gpio));

	spin_unlock_irqrestore(&gchip->lock, flags);
}

static int mb86s70_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
{
77
	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	unsigned long flags;
	unsigned char val;

	spin_lock_irqsave(&gchip->lock, flags);

	val = readl(gchip->base + DDR(gpio));
	val &= ~OFFSET(gpio);
	writel(val, gchip->base + DDR(gpio));

	spin_unlock_irqrestore(&gchip->lock, flags);

	return 0;
}

static int mb86s70_gpio_direction_output(struct gpio_chip *gc,
					 unsigned gpio, int value)
{
95
	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	unsigned long flags;
	unsigned char val;

	spin_lock_irqsave(&gchip->lock, flags);

	val = readl(gchip->base + PDR(gpio));
	if (value)
		val |= OFFSET(gpio);
	else
		val &= ~OFFSET(gpio);
	writel(val, gchip->base + PDR(gpio));

	val = readl(gchip->base + DDR(gpio));
	val |= OFFSET(gpio);
	writel(val, gchip->base + DDR(gpio));

	spin_unlock_irqrestore(&gchip->lock, flags);

	return 0;
}

static int mb86s70_gpio_get(struct gpio_chip *gc, unsigned gpio)
{
119
	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
120 121 122 123 124 125

	return !!(readl(gchip->base + PDR(gpio)) & OFFSET(gpio));
}

static void mb86s70_gpio_set(struct gpio_chip *gc, unsigned gpio, int value)
{
126
	struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	unsigned long flags;
	unsigned char val;

	spin_lock_irqsave(&gchip->lock, flags);

	val = readl(gchip->base + PDR(gpio));
	if (value)
		val |= OFFSET(gpio);
	else
		val &= ~OFFSET(gpio);
	writel(val, gchip->base + PDR(gpio));

	spin_unlock_irqrestore(&gchip->lock, flags);
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155
static int mb86s70_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
{
	int irq, index;

	for (index = 0;; index++) {
		irq = platform_get_irq(to_platform_device(gc->parent), index);
		if (irq <= 0)
			break;
		if (irq_get_irq_data(irq)->hwirq == offset)
			return irq;
	}
	return -EINVAL;
}

156 157 158 159 160 161 162 163 164 165 166
static int mb86s70_gpio_probe(struct platform_device *pdev)
{
	struct mb86s70_gpio_chip *gchip;
	int ret;

	gchip = devm_kzalloc(&pdev->dev, sizeof(*gchip), GFP_KERNEL);
	if (gchip == NULL)
		return -ENOMEM;

	platform_set_drvdata(pdev, gchip);

167
	gchip->base = devm_platform_ioremap_resource(pdev, 0);
168 169 170
	if (IS_ERR(gchip->base))
		return PTR_ERR(gchip->base);

171 172 173 174
	if (!has_acpi_companion(&pdev->dev)) {
		gchip->clk = devm_clk_get(&pdev->dev, NULL);
		if (IS_ERR(gchip->clk))
			return PTR_ERR(gchip->clk);
175

176 177 178 179
		ret = clk_prepare_enable(gchip->clk);
		if (ret)
			return ret;
	}
180 181 182 183 184 185 186 187 188 189 190 191

	spin_lock_init(&gchip->lock);

	gchip->gc.direction_output = mb86s70_gpio_direction_output;
	gchip->gc.direction_input = mb86s70_gpio_direction_input;
	gchip->gc.request = mb86s70_gpio_request;
	gchip->gc.free = mb86s70_gpio_free;
	gchip->gc.get = mb86s70_gpio_get;
	gchip->gc.set = mb86s70_gpio_set;
	gchip->gc.label = dev_name(&pdev->dev);
	gchip->gc.ngpio = 32;
	gchip->gc.owner = THIS_MODULE;
192
	gchip->gc.parent = &pdev->dev;
193 194
	gchip->gc.base = -1;

195 196 197
	if (has_acpi_companion(&pdev->dev))
		gchip->gc.to_irq = mb86s70_gpio_to_irq;

198
	ret = gpiochip_add_data(&gchip->gc, gchip);
199 200 201
	if (ret) {
		dev_err(&pdev->dev, "couldn't register gpio driver\n");
		clk_disable_unprepare(gchip->clk);
202
		return ret;
203 204
	}

205 206 207 208
	if (has_acpi_companion(&pdev->dev))
		acpi_gpiochip_request_interrupts(&gchip->gc);

	return 0;
209 210 211 212 213 214
}

static int mb86s70_gpio_remove(struct platform_device *pdev)
{
	struct mb86s70_gpio_chip *gchip = platform_get_drvdata(pdev);

215 216
	if (has_acpi_companion(&pdev->dev))
		acpi_gpiochip_free_interrupts(&gchip->gc);
217 218 219 220 221 222 223 224 225 226
	gpiochip_remove(&gchip->gc);
	clk_disable_unprepare(gchip->clk);

	return 0;
}

static const struct of_device_id mb86s70_gpio_dt_ids[] = {
	{ .compatible = "fujitsu,mb86s70-gpio" },
	{ /* sentinel */ }
};
227
MODULE_DEVICE_TABLE(of, mb86s70_gpio_dt_ids);
228

229 230 231 232 233 234 235 236
#ifdef CONFIG_ACPI
static const struct acpi_device_id mb86s70_gpio_acpi_ids[] = {
	{ "SCX0007" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(acpi, mb86s70_gpio_acpi_ids);
#endif

237 238 239 240
static struct platform_driver mb86s70_gpio_driver = {
	.driver = {
		.name = "mb86s70-gpio",
		.of_match_table = mb86s70_gpio_dt_ids,
241
		.acpi_match_table = ACPI_PTR(mb86s70_gpio_acpi_ids),
242 243 244 245
	},
	.probe = mb86s70_gpio_probe,
	.remove = mb86s70_gpio_remove,
};
246
module_platform_driver(mb86s70_gpio_driver);
247

248 249 250
MODULE_DESCRIPTION("MB86S7x GPIO Driver");
MODULE_ALIAS("platform:mb86s70-gpio");
MODULE_LICENSE("GPL");