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> |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of_address.h> |
| 20 | #include <linux/of_gpio.h> |
| 21 | #include <linux/pinctrl/consumer.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/timer.h> |
| 24 | #include <linux/bitops.h> |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 25 | #include <linux/gpio/driver.h> |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 26 | |
| 27 | #define GPIO_DATA_OUT 0x00 |
| 28 | #define GPIO_DATA_IN 0x04 |
| 29 | #define GPIO_PIN_DIRECTION 0x08 |
| 30 | |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 31 | static int moxart_gpio_probe(struct platform_device *pdev) |
| 32 | { |
| 33 | struct device *dev = &pdev->dev; |
| 34 | struct resource *res; |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 35 | struct gpio_chip *gc; |
Kamlakant Patel | 3c01b9a | 2014-12-01 17:39:34 +0530 | [diff] [blame] | 36 | void __iomem *base; |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 37 | int ret; |
| 38 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 39 | gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL); |
| 40 | if (!gc) |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 41 | return -ENOMEM; |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 42 | |
| 43 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Kamlakant Patel | 3c01b9a | 2014-12-01 17:39:34 +0530 | [diff] [blame] | 44 | base = devm_ioremap_resource(dev, res); |
| 45 | if (IS_ERR(base)) |
| 46 | return PTR_ERR(base); |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 47 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 48 | ret = bgpio_init(gc, dev, 4, base + GPIO_DATA_IN, |
Vladimir Zapolskiy | f306633 | 2015-04-29 18:35:00 +0300 | [diff] [blame] | 49 | base + GPIO_DATA_OUT, NULL, |
| 50 | base + GPIO_PIN_DIRECTION, NULL, |
| 51 | BGPIOF_READ_OUTPUT_REG_SET); |
Kamlakant Patel | 3c01b9a | 2014-12-01 17:39:34 +0530 | [diff] [blame] | 52 | if (ret) { |
| 53 | dev_err(&pdev->dev, "bgpio_init failed\n"); |
| 54 | return ret; |
| 55 | } |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 56 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 57 | gc->label = "moxart-gpio"; |
| 58 | gc->request = gpiochip_generic_request; |
| 59 | gc->free = gpiochip_generic_free; |
Arnd Bergmann | 780c43d | 2016-01-07 14:55:34 +0100 | [diff] [blame^] | 60 | gc->bgpio_data = gc->read_reg(gc->reg_set); |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 61 | gc->base = 0; |
| 62 | gc->ngpio = 32; |
| 63 | gc->parent = dev; |
| 64 | gc->owner = THIS_MODULE; |
Kamlakant Patel | 3c01b9a | 2014-12-01 17:39:34 +0530 | [diff] [blame] | 65 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 66 | ret = gpiochip_add_data(gc, NULL); |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 67 | if (ret) { |
| 68 | dev_err(dev, "%s: gpiochip_add failed\n", |
| 69 | dev->of_node->full_name); |
| 70 | return ret; |
| 71 | } |
| 72 | |
Kamlakant Patel | 3c01b9a | 2014-12-01 17:39:34 +0530 | [diff] [blame] | 73 | return ret; |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static const struct of_device_id moxart_gpio_match[] = { |
| 77 | { .compatible = "moxa,moxart-gpio" }, |
| 78 | { } |
| 79 | }; |
| 80 | |
| 81 | static struct platform_driver moxart_gpio_driver = { |
| 82 | .driver = { |
| 83 | .name = "moxart-gpio", |
Jonas Jensen | 0299b77 | 2013-11-29 12:11:34 +0100 | [diff] [blame] | 84 | .of_match_table = moxart_gpio_match, |
| 85 | }, |
| 86 | .probe = moxart_gpio_probe, |
| 87 | }; |
| 88 | module_platform_driver(moxart_gpio_driver); |
| 89 | |
| 90 | MODULE_DESCRIPTION("MOXART GPIO chip driver"); |
| 91 | MODULE_LICENSE("GPL"); |
| 92 | MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>"); |