gpio-moxart.c 3.8 KB
Newer Older
J
Jonas Jensen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * MOXA ART SoCs GPIO driver.
 *
 * Copyright (C) 2013 Jonas Jensen
 *
 * Jonas Jensen <jonas.jensen@gmail.com>
 *
 * This file is licensed under the terms of the GNU General Public
 * License version 2.  This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

#include <linux/err.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/pinctrl/consumer.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/bitops.h>

#define GPIO_DATA_OUT		0x00
#define GPIO_DATA_IN		0x04
#define GPIO_PIN_DIRECTION	0x08

struct moxart_gpio_chip {
	struct gpio_chip gpio;
33
	void __iomem *base;
J
Jonas Jensen 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
};

static inline struct moxart_gpio_chip *to_moxart_gpio(struct gpio_chip *chip)
{
	return container_of(chip, struct moxart_gpio_chip, gpio);
}

static int moxart_gpio_request(struct gpio_chip *chip, unsigned offset)
{
	return pinctrl_request_gpio(offset);
}

static void moxart_gpio_free(struct gpio_chip *chip, unsigned offset)
{
	pinctrl_free_gpio(offset);
}

static void moxart_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
	struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
54
	void __iomem *ioaddr = gc->base + GPIO_DATA_OUT;
J
Jonas Jensen 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67
	u32 reg = readl(ioaddr);

	if (value)
		reg = reg | BIT(offset);
	else
		reg = reg & ~BIT(offset);

	writel(reg, ioaddr);
}

static int moxart_gpio_get(struct gpio_chip *chip, unsigned offset)
{
	struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
68
	u32 ret = readl(gc->base + GPIO_PIN_DIRECTION);
J
Jonas Jensen 已提交
69 70

	if (ret & BIT(offset))
71
		return !!(readl(gc->base + GPIO_DATA_OUT) & BIT(offset));
J
Jonas Jensen 已提交
72
	else
73
		return !!(readl(gc->base + GPIO_DATA_IN) & BIT(offset));
J
Jonas Jensen 已提交
74 75
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static int moxart_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
{
	struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
	void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION;

	writel(readl(ioaddr) & ~BIT(offset), ioaddr);
	return 0;
}

static int moxart_gpio_direction_output(struct gpio_chip *chip,
					unsigned offset, int value)
{
	struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
	void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION;

91
	moxart_gpio_set(chip, offset, value);
92 93 94 95
	writel(readl(ioaddr) | BIT(offset), ioaddr);
	return 0;
}

J
Jonas Jensen 已提交
96 97 98 99 100 101 102 103 104
static struct gpio_chip moxart_template_chip = {
	.label			= "moxart-gpio",
	.request		= moxart_gpio_request,
	.free			= moxart_gpio_free,
	.direction_input	= moxart_gpio_direction_input,
	.direction_output	= moxart_gpio_direction_output,
	.set			= moxart_gpio_set,
	.get			= moxart_gpio_get,
	.ngpio			= 32,
105
	.owner			= THIS_MODULE,
J
Jonas Jensen 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
};

static int moxart_gpio_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct resource *res;
	struct moxart_gpio_chip *mgc;
	int ret;

	mgc = devm_kzalloc(dev, sizeof(*mgc), GFP_KERNEL);
	if (!mgc) {
		dev_err(dev, "can't allocate GPIO chip container\n");
		return -ENOMEM;
	}
	mgc->gpio = moxart_template_chip;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
123
	mgc->base = devm_ioremap_resource(dev, res);
124
	if (IS_ERR(mgc->base))
125
		return PTR_ERR(mgc->base);
J
Jonas Jensen 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

	mgc->gpio.dev = dev;

	ret = gpiochip_add(&mgc->gpio);
	if (ret) {
		dev_err(dev, "%s: gpiochip_add failed\n",
			dev->of_node->full_name);
		return ret;
	}

	return 0;
}

static const struct of_device_id moxart_gpio_match[] = {
	{ .compatible = "moxa,moxart-gpio" },
	{ }
};

static struct platform_driver moxart_gpio_driver = {
	.driver	= {
		.name		= "moxart-gpio",
		.owner		= THIS_MODULE,
		.of_match_table	= moxart_gpio_match,
	},
	.probe	= moxart_gpio_probe,
};
module_platform_driver(moxart_gpio_driver);

MODULE_DESCRIPTION("MOXART GPIO chip driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");