blob: c3ab46e595dafc4ac3c3b84d7161e56a4a1feb71 [file] [log] [blame]
Jonas Jensen0299b772013-11-29 12:11:34 +01001/*
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>
Kamlakant Patel3c01b9a2014-12-01 17:39:34 +053026#include <linux/basic_mmio_gpio.h>
Jonas Jensen0299b772013-11-29 12:11:34 +010027
28#define GPIO_DATA_OUT 0x00
29#define GPIO_DATA_IN 0x04
30#define GPIO_PIN_DIRECTION 0x08
31
Jonas Jensen0299b772013-11-29 12:11:34 +010032static int moxart_gpio_request(struct gpio_chip *chip, unsigned offset)
33{
34 return pinctrl_request_gpio(offset);
35}
36
37static void moxart_gpio_free(struct gpio_chip *chip, unsigned offset)
38{
39 pinctrl_free_gpio(offset);
40}
41
Axel Linc9e8dba2014-03-25 10:43:33 +080042static int moxart_gpio_get(struct gpio_chip *chip, unsigned offset)
43{
Kamlakant Patel3c01b9a2014-12-01 17:39:34 +053044 struct bgpio_chip *bgc = to_bgpio_chip(chip);
45 u32 ret = bgc->read_reg(bgc->reg_dir);
Axel Linc9e8dba2014-03-25 10:43:33 +080046
47 if (ret & BIT(offset))
Kamlakant Patel3c01b9a2014-12-01 17:39:34 +053048 return !!(bgc->read_reg(bgc->reg_set) & BIT(offset));
Axel Linc9e8dba2014-03-25 10:43:33 +080049 else
Kamlakant Patel3c01b9a2014-12-01 17:39:34 +053050 return !!(bgc->read_reg(bgc->reg_dat) & BIT(offset));
Axel Linc9e8dba2014-03-25 10:43:33 +080051}
52
Jonas Jensen0299b772013-11-29 12:11:34 +010053static int moxart_gpio_probe(struct platform_device *pdev)
54{
55 struct device *dev = &pdev->dev;
56 struct resource *res;
Kamlakant Patel3c01b9a2014-12-01 17:39:34 +053057 struct bgpio_chip *bgc;
58 void __iomem *base;
Jonas Jensen0299b772013-11-29 12:11:34 +010059 int ret;
60
Kamlakant Patel3c01b9a2014-12-01 17:39:34 +053061 bgc = devm_kzalloc(dev, sizeof(*bgc), GFP_KERNEL);
62 if (!bgc)
Jonas Jensen0299b772013-11-29 12:11:34 +010063 return -ENOMEM;
Jonas Jensen0299b772013-11-29 12:11:34 +010064
65 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kamlakant Patel3c01b9a2014-12-01 17:39:34 +053066 base = devm_ioremap_resource(dev, res);
67 if (IS_ERR(base))
68 return PTR_ERR(base);
Jonas Jensen0299b772013-11-29 12:11:34 +010069
Kamlakant Patel3c01b9a2014-12-01 17:39:34 +053070 ret = bgpio_init(bgc, dev, 4, base + GPIO_DATA_IN,
71 base + GPIO_DATA_OUT, NULL,
72 base + GPIO_PIN_DIRECTION, NULL, 0);
73 if (ret) {
74 dev_err(&pdev->dev, "bgpio_init failed\n");
75 return ret;
76 }
Jonas Jensen0299b772013-11-29 12:11:34 +010077
Kamlakant Patel3c01b9a2014-12-01 17:39:34 +053078 bgc->gc.label = "moxart-gpio";
79 bgc->gc.request = moxart_gpio_request;
80 bgc->gc.free = moxart_gpio_free;
81 bgc->gc.get = moxart_gpio_get;
82 bgc->data = bgc->read_reg(bgc->reg_set);
83 bgc->gc.base = 0;
84 bgc->gc.ngpio = 32;
85 bgc->gc.dev = dev;
86 bgc->gc.owner = THIS_MODULE;
87
88 ret = gpiochip_add(&bgc->gc);
Jonas Jensen0299b772013-11-29 12:11:34 +010089 if (ret) {
90 dev_err(dev, "%s: gpiochip_add failed\n",
91 dev->of_node->full_name);
92 return ret;
93 }
94
Kamlakant Patel3c01b9a2014-12-01 17:39:34 +053095 return ret;
Jonas Jensen0299b772013-11-29 12:11:34 +010096}
97
98static const struct of_device_id moxart_gpio_match[] = {
99 { .compatible = "moxa,moxart-gpio" },
100 { }
101};
102
103static struct platform_driver moxart_gpio_driver = {
104 .driver = {
105 .name = "moxart-gpio",
Jonas Jensen0299b772013-11-29 12:11:34 +0100106 .of_match_table = moxart_gpio_match,
107 },
108 .probe = moxart_gpio_probe,
109};
110module_platform_driver(moxart_gpio_driver);
111
112MODULE_DESCRIPTION("MOXART GPIO chip driver");
113MODULE_LICENSE("GPL");
114MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");