Julien Grossholtz | 5041e79 | 2016-01-13 11:57:43 -0500 | [diff] [blame] | 1 | /* |
| 2 | * GPIO driver for the TS-4800 board |
| 3 | * |
| 4 | * Copyright (c) 2016 - Savoir-faire Linux |
| 5 | * |
| 6 | * This file is licensed under the terms of the GNU General Public |
| 7 | * License version 2. This program is licensed "as is" without any |
| 8 | * warranty of any kind, whether express or implied. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/gpio/driver.h> |
Paul Gortmaker | 7de9a6c | 2016-09-12 18:16:26 -0400 | [diff] [blame] | 12 | #include <linux/module.h> |
Julien Grossholtz | 5041e79 | 2016-01-13 11:57:43 -0500 | [diff] [blame] | 13 | #include <linux/of_address.h> |
| 14 | #include <linux/of_device.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | |
| 17 | #define DEFAULT_PIN_NUMBER 16 |
| 18 | #define INPUT_REG_OFFSET 0x00 |
| 19 | #define OUTPUT_REG_OFFSET 0x02 |
| 20 | #define DIRECTION_REG_OFFSET 0x04 |
| 21 | |
| 22 | static int ts4800_gpio_probe(struct platform_device *pdev) |
| 23 | { |
| 24 | struct device_node *node; |
| 25 | struct gpio_chip *chip; |
| 26 | struct resource *res; |
| 27 | void __iomem *base_addr; |
| 28 | int retval; |
| 29 | u32 ngpios; |
| 30 | |
| 31 | chip = devm_kzalloc(&pdev->dev, sizeof(struct gpio_chip), GFP_KERNEL); |
| 32 | if (!chip) |
| 33 | return -ENOMEM; |
| 34 | |
| 35 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 36 | base_addr = devm_ioremap_resource(&pdev->dev, res); |
| 37 | if (IS_ERR(base_addr)) |
| 38 | return PTR_ERR(base_addr); |
| 39 | |
| 40 | node = pdev->dev.of_node; |
| 41 | if (!node) |
| 42 | return -EINVAL; |
| 43 | |
| 44 | retval = of_property_read_u32(node, "ngpios", &ngpios); |
| 45 | if (retval == -EINVAL) |
| 46 | ngpios = DEFAULT_PIN_NUMBER; |
| 47 | else if (retval) |
| 48 | return retval; |
| 49 | |
| 50 | retval = bgpio_init(chip, &pdev->dev, 2, base_addr + INPUT_REG_OFFSET, |
| 51 | base_addr + OUTPUT_REG_OFFSET, NULL, |
Julien Grossholtz | 047b2f62 | 2016-02-10 12:20:34 -0500 | [diff] [blame] | 52 | base_addr + DIRECTION_REG_OFFSET, NULL, 0); |
Julien Grossholtz | 5041e79 | 2016-01-13 11:57:43 -0500 | [diff] [blame] | 53 | if (retval) { |
| 54 | dev_err(&pdev->dev, "bgpio_init failed\n"); |
| 55 | return retval; |
| 56 | } |
| 57 | |
Julien Grossholtz | 5041e79 | 2016-01-13 11:57:43 -0500 | [diff] [blame] | 58 | chip->ngpio = ngpios; |
| 59 | |
| 60 | platform_set_drvdata(pdev, chip); |
| 61 | |
Laxman Dewangan | 33ba54e | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 62 | return devm_gpiochip_add_data(&pdev->dev, chip, NULL); |
Julien Grossholtz | 5041e79 | 2016-01-13 11:57:43 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static const struct of_device_id ts4800_gpio_of_match[] = { |
| 66 | { .compatible = "technologic,ts4800-gpio", }, |
| 67 | {}, |
| 68 | }; |
Javier Martinez Canillas | 91f1551 | 2016-10-18 17:44:02 -0300 | [diff] [blame] | 69 | MODULE_DEVICE_TABLE(of, ts4800_gpio_of_match); |
Julien Grossholtz | 5041e79 | 2016-01-13 11:57:43 -0500 | [diff] [blame] | 70 | |
| 71 | static struct platform_driver ts4800_gpio_driver = { |
| 72 | .driver = { |
| 73 | .name = "ts4800-gpio", |
| 74 | .of_match_table = ts4800_gpio_of_match, |
| 75 | }, |
| 76 | .probe = ts4800_gpio_probe, |
Julien Grossholtz | 5041e79 | 2016-01-13 11:57:43 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | module_platform_driver_probe(ts4800_gpio_driver, ts4800_gpio_probe); |
| 80 | |
| 81 | MODULE_AUTHOR("Julien Grossholtz <julien.grossholtz@savoirfairelinux.com>"); |
| 82 | MODULE_DESCRIPTION("TS4800 FPGA GPIO driver"); |
| 83 | MODULE_LICENSE("GPL v2"); |