Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * MOXA ART SoCs GPIO driver. |
| 3 | * |
| 4 | * Copyright (C) 2013 Jonas Jensen |
| 5 | * |
| 6 | * Jonas Jensen <jonas.jensen@gmail.com> |
| 7 | * |
| 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. |
| 11 | */ |
| 12 | |
| 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> |
| 26 | |
| 27 | #define GPIO_DATA_OUT 0x00 |
| 28 | #define GPIO_DATA_IN 0x04 |
| 29 | #define GPIO_PIN_DIRECTION 0x08 |
| 30 | |
| 31 | struct moxart_gpio_chip { |
| 32 | struct gpio_chip gpio; |
Jonas Jensen | a87854d | 2013-12-02 11:27:59 +0100 | [diff] [blame^] | 33 | void __iomem *base; |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static inline struct moxart_gpio_chip *to_moxart_gpio(struct gpio_chip *chip) |
| 37 | { |
| 38 | return container_of(chip, struct moxart_gpio_chip, gpio); |
| 39 | } |
| 40 | |
| 41 | static int moxart_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 42 | { |
| 43 | return pinctrl_request_gpio(offset); |
| 44 | } |
| 45 | |
| 46 | static void moxart_gpio_free(struct gpio_chip *chip, unsigned offset) |
| 47 | { |
| 48 | pinctrl_free_gpio(offset); |
| 49 | } |
| 50 | |
| 51 | static int moxart_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 52 | { |
| 53 | struct moxart_gpio_chip *gc = to_moxart_gpio(chip); |
Jonas Jensen | a87854d | 2013-12-02 11:27:59 +0100 | [diff] [blame^] | 54 | void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION; |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 55 | |
| 56 | writel(readl(ioaddr) & ~BIT(offset), ioaddr); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int moxart_gpio_direction_output(struct gpio_chip *chip, |
| 61 | unsigned offset, int value) |
| 62 | { |
| 63 | struct moxart_gpio_chip *gc = to_moxart_gpio(chip); |
Jonas Jensen | a87854d | 2013-12-02 11:27:59 +0100 | [diff] [blame^] | 64 | void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION; |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 65 | |
| 66 | writel(readl(ioaddr) | BIT(offset), ioaddr); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static void moxart_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 71 | { |
| 72 | struct moxart_gpio_chip *gc = to_moxart_gpio(chip); |
Jonas Jensen | a87854d | 2013-12-02 11:27:59 +0100 | [diff] [blame^] | 73 | void __iomem *ioaddr = gc->base + GPIO_DATA_OUT; |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 74 | u32 reg = readl(ioaddr); |
| 75 | |
| 76 | if (value) |
| 77 | reg = reg | BIT(offset); |
| 78 | else |
| 79 | reg = reg & ~BIT(offset); |
| 80 | |
| 81 | |
| 82 | writel(reg, ioaddr); |
| 83 | } |
| 84 | |
| 85 | static int moxart_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 86 | { |
| 87 | struct moxart_gpio_chip *gc = to_moxart_gpio(chip); |
Jonas Jensen | a87854d | 2013-12-02 11:27:59 +0100 | [diff] [blame^] | 88 | u32 ret = readl(gc->base + GPIO_PIN_DIRECTION); |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 89 | |
| 90 | if (ret & BIT(offset)) |
Jonas Jensen | a87854d | 2013-12-02 11:27:59 +0100 | [diff] [blame^] | 91 | return !!(readl(gc->base + GPIO_DATA_OUT) & BIT(offset)); |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 92 | else |
Jonas Jensen | a87854d | 2013-12-02 11:27:59 +0100 | [diff] [blame^] | 93 | return !!(readl(gc->base + GPIO_DATA_IN) & BIT(offset)); |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 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, |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 104 | .ngpio = 32, |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | static int moxart_gpio_probe(struct platform_device *pdev) |
| 108 | { |
| 109 | struct device *dev = &pdev->dev; |
| 110 | struct resource *res; |
| 111 | struct moxart_gpio_chip *mgc; |
| 112 | int ret; |
| 113 | |
| 114 | mgc = devm_kzalloc(dev, sizeof(*mgc), GFP_KERNEL); |
| 115 | if (!mgc) { |
| 116 | dev_err(dev, "can't allocate GPIO chip container\n"); |
| 117 | return -ENOMEM; |
| 118 | } |
| 119 | mgc->gpio = moxart_template_chip; |
| 120 | |
| 121 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Jonas Jensen | a87854d | 2013-12-02 11:27:59 +0100 | [diff] [blame^] | 122 | mgc->base = devm_ioremap_resource(dev, res); |
| 123 | if (IS_ERR(mgc->base)) { |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 124 | dev_err(dev, "%s: devm_ioremap_resource res_gpio failed\n", |
| 125 | dev->of_node->full_name); |
Jonas Jensen | a87854d | 2013-12-02 11:27:59 +0100 | [diff] [blame^] | 126 | return PTR_ERR(mgc->base); |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | mgc->gpio.dev = dev; |
| 130 | |
| 131 | ret = gpiochip_add(&mgc->gpio); |
| 132 | if (ret) { |
| 133 | dev_err(dev, "%s: gpiochip_add failed\n", |
| 134 | dev->of_node->full_name); |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static const struct of_device_id moxart_gpio_match[] = { |
| 142 | { .compatible = "moxa,moxart-gpio" }, |
| 143 | { } |
| 144 | }; |
| 145 | |
| 146 | static struct platform_driver moxart_gpio_driver = { |
| 147 | .driver = { |
| 148 | .name = "moxart-gpio", |
| 149 | .owner = THIS_MODULE, |
| 150 | .of_match_table = moxart_gpio_match, |
| 151 | }, |
| 152 | .probe = moxart_gpio_probe, |
| 153 | }; |
| 154 | module_platform_driver(moxart_gpio_driver); |
| 155 | |
| 156 | MODULE_DESCRIPTION("MOXART GPIO chip driver"); |
| 157 | MODULE_LICENSE("GPL"); |
| 158 | MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>"); |