Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * GPIO driver for the TS-4800 board
4 *
5 * Copyright (c) 2016 - Savoir-faire Linux
6 */
7
8#include <linux/gpio/driver.h>
9#include <linux/module.h>
10#include <linux/of.h>
11#include <linux/platform_device.h>
12
13#define DEFAULT_PIN_NUMBER 16
14#define INPUT_REG_OFFSET 0x00
15#define OUTPUT_REG_OFFSET 0x02
16#define DIRECTION_REG_OFFSET 0x04
17
18static int ts4800_gpio_probe(struct platform_device *pdev)
19{
20 struct device_node *node;
21 struct gpio_chip *chip;
22 void __iomem *base_addr;
23 int retval;
24 u32 ngpios;
25
26 chip = devm_kzalloc(&pdev->dev, sizeof(struct gpio_chip), GFP_KERNEL);
27 if (!chip)
28 return -ENOMEM;
29
30 base_addr = devm_platform_ioremap_resource(pdev, 0);
31 if (IS_ERR(base_addr))
32 return PTR_ERR(base_addr);
33
34 node = pdev->dev.of_node;
35 if (!node)
36 return -EINVAL;
37
38 retval = of_property_read_u32(node, "ngpios", &ngpios);
39 if (retval == -EINVAL)
40 ngpios = DEFAULT_PIN_NUMBER;
41 else if (retval)
42 return retval;
43
44 retval = bgpio_init(chip, &pdev->dev, 2, base_addr + INPUT_REG_OFFSET,
45 base_addr + OUTPUT_REG_OFFSET, NULL,
46 base_addr + DIRECTION_REG_OFFSET, NULL, 0);
47 if (retval) {
48 dev_err(&pdev->dev, "bgpio_init failed\n");
49 return retval;
50 }
51
52 chip->ngpio = ngpios;
53
54 platform_set_drvdata(pdev, chip);
55
56 return devm_gpiochip_add_data(&pdev->dev, chip, NULL);
57}
58
59static const struct of_device_id ts4800_gpio_of_match[] = {
60 { .compatible = "technologic,ts4800-gpio", },
61 {},
62};
63MODULE_DEVICE_TABLE(of, ts4800_gpio_of_match);
64
65static struct platform_driver ts4800_gpio_driver = {
66 .driver = {
67 .name = "ts4800-gpio",
68 .of_match_table = ts4800_gpio_of_match,
69 },
70 .probe = ts4800_gpio_probe,
71};
72
73module_platform_driver_probe(ts4800_gpio_driver, ts4800_gpio_probe);
74
75MODULE_AUTHOR("Julien Grossholtz <julien.grossholtz@savoirfairelinux.com>");
76MODULE_DESCRIPTION("TS4800 FPGA GPIO driver");
77MODULE_LICENSE("GPL v2");
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>
12#include <linux/of_address.h>
13#include <linux/of_device.h>
14#include <linux/platform_device.h>
15
16#define DEFAULT_PIN_NUMBER 16
17#define INPUT_REG_OFFSET 0x00
18#define OUTPUT_REG_OFFSET 0x02
19#define DIRECTION_REG_OFFSET 0x04
20
21static int ts4800_gpio_probe(struct platform_device *pdev)
22{
23 struct device_node *node;
24 struct gpio_chip *chip;
25 struct resource *res;
26 void __iomem *base_addr;
27 int retval;
28 u32 ngpios;
29
30 chip = devm_kzalloc(&pdev->dev, sizeof(struct gpio_chip), GFP_KERNEL);
31 if (!chip)
32 return -ENOMEM;
33
34 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
35 base_addr = devm_ioremap_resource(&pdev->dev, res);
36 if (IS_ERR(base_addr))
37 return PTR_ERR(base_addr);
38
39 node = pdev->dev.of_node;
40 if (!node)
41 return -EINVAL;
42
43 retval = of_property_read_u32(node, "ngpios", &ngpios);
44 if (retval == -EINVAL)
45 ngpios = DEFAULT_PIN_NUMBER;
46 else if (retval)
47 return retval;
48
49 retval = bgpio_init(chip, &pdev->dev, 2, base_addr + INPUT_REG_OFFSET,
50 base_addr + OUTPUT_REG_OFFSET, NULL,
51 base_addr + DIRECTION_REG_OFFSET, NULL, 0);
52 if (retval) {
53 dev_err(&pdev->dev, "bgpio_init failed\n");
54 return retval;
55 }
56
57 chip->ngpio = ngpios;
58
59 platform_set_drvdata(pdev, chip);
60
61 return devm_gpiochip_add_data(&pdev->dev, chip, NULL);
62}
63
64static const struct of_device_id ts4800_gpio_of_match[] = {
65 { .compatible = "technologic,ts4800-gpio", },
66 {},
67};
68
69static struct platform_driver ts4800_gpio_driver = {
70 .driver = {
71 .name = "ts4800-gpio",
72 .of_match_table = ts4800_gpio_of_match,
73 },
74 .probe = ts4800_gpio_probe,
75};
76
77module_platform_driver_probe(ts4800_gpio_driver, ts4800_gpio_probe);
78
79MODULE_AUTHOR("Julien Grossholtz <julien.grossholtz@savoirfairelinux.com>");
80MODULE_DESCRIPTION("TS4800 FPGA GPIO driver");
81MODULE_LICENSE("GPL v2");