[media] rc: img-ir: pass toggle bit to the rc driver
[linux-2.6/btrfs-unstable.git] / drivers / gpio / gpio-moxart.c
blob31e2551ed9034b4681ca723f3cbeafcb8774be89
1 /*
2 * MOXA ART SoCs GPIO driver.
4 * Copyright (C) 2013 Jonas Jensen
6 * Jonas Jensen <jonas.jensen@gmail.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/irq.h>
16 #include <linux/io.h>
17 #include <linux/gpio.h>
18 #include <linux/platform_device.h>
19 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/of_gpio.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/delay.h>
24 #include <linux/timer.h>
25 #include <linux/bitops.h>
27 #define GPIO_DATA_OUT 0x00
28 #define GPIO_DATA_IN 0x04
29 #define GPIO_PIN_DIRECTION 0x08
31 struct moxart_gpio_chip {
32 struct gpio_chip gpio;
33 void __iomem *base;
36 static inline struct moxart_gpio_chip *to_moxart_gpio(struct gpio_chip *chip)
38 return container_of(chip, struct moxart_gpio_chip, gpio);
41 static int moxart_gpio_request(struct gpio_chip *chip, unsigned offset)
43 return pinctrl_request_gpio(offset);
46 static void moxart_gpio_free(struct gpio_chip *chip, unsigned offset)
48 pinctrl_free_gpio(offset);
51 static void moxart_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
53 struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
54 void __iomem *ioaddr = gc->base + GPIO_DATA_OUT;
55 u32 reg = readl(ioaddr);
57 if (value)
58 reg = reg | BIT(offset);
59 else
60 reg = reg & ~BIT(offset);
62 writel(reg, ioaddr);
65 static int moxart_gpio_get(struct gpio_chip *chip, unsigned offset)
67 struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
68 u32 ret = readl(gc->base + GPIO_PIN_DIRECTION);
70 if (ret & BIT(offset))
71 return !!(readl(gc->base + GPIO_DATA_OUT) & BIT(offset));
72 else
73 return !!(readl(gc->base + GPIO_DATA_IN) & BIT(offset));
76 static int moxart_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
78 struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
79 void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION;
81 writel(readl(ioaddr) & ~BIT(offset), ioaddr);
82 return 0;
85 static int moxart_gpio_direction_output(struct gpio_chip *chip,
86 unsigned offset, int value)
88 struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
89 void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION;
91 moxart_gpio_set(chip, offset, value);
92 writel(readl(ioaddr) | BIT(offset), ioaddr);
93 return 0;
96 static struct gpio_chip moxart_template_chip = {
97 .label = "moxart-gpio",
98 .request = moxart_gpio_request,
99 .free = moxart_gpio_free,
100 .direction_input = moxart_gpio_direction_input,
101 .direction_output = moxart_gpio_direction_output,
102 .set = moxart_gpio_set,
103 .get = moxart_gpio_get,
104 .ngpio = 32,
105 .owner = THIS_MODULE,
108 static int moxart_gpio_probe(struct platform_device *pdev)
110 struct device *dev = &pdev->dev;
111 struct resource *res;
112 struct moxart_gpio_chip *mgc;
113 int ret;
115 mgc = devm_kzalloc(dev, sizeof(*mgc), GFP_KERNEL);
116 if (!mgc)
117 return -ENOMEM;
118 mgc->gpio = moxart_template_chip;
120 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
121 mgc->base = devm_ioremap_resource(dev, res);
122 if (IS_ERR(mgc->base))
123 return PTR_ERR(mgc->base);
125 mgc->gpio.dev = dev;
127 ret = gpiochip_add(&mgc->gpio);
128 if (ret) {
129 dev_err(dev, "%s: gpiochip_add failed\n",
130 dev->of_node->full_name);
131 return ret;
134 return 0;
137 static const struct of_device_id moxart_gpio_match[] = {
138 { .compatible = "moxa,moxart-gpio" },
142 static struct platform_driver moxart_gpio_driver = {
143 .driver = {
144 .name = "moxart-gpio",
145 .of_match_table = moxart_gpio_match,
147 .probe = moxart_gpio_probe,
149 module_platform_driver(moxart_gpio_driver);
151 MODULE_DESCRIPTION("MOXART GPIO chip driver");
152 MODULE_LICENSE("GPL");
153 MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");