Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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/gpio/generic.h>
10#include <linux/module.h>
11#include <linux/platform_device.h>
12#include <linux/property.h>
13
14#define DEFAULT_PIN_NUMBER 16
15#define INPUT_REG_OFFSET 0x00
16#define OUTPUT_REG_OFFSET 0x02
17#define DIRECTION_REG_OFFSET 0x04
18
19static int ts4800_gpio_probe(struct platform_device *pdev)
20{
21 struct gpio_generic_chip_config config;
22 struct device *dev = &pdev->dev;
23 struct gpio_generic_chip *chip;
24 void __iomem *base_addr;
25 int retval;
26 u32 ngpios;
27
28 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
29 if (!chip)
30 return -ENOMEM;
31
32 base_addr = devm_platform_ioremap_resource(pdev, 0);
33 if (IS_ERR(base_addr))
34 return PTR_ERR(base_addr);
35
36 retval = device_property_read_u32(dev, "ngpios", &ngpios);
37 if (retval == -EINVAL)
38 ngpios = DEFAULT_PIN_NUMBER;
39 else if (retval)
40 return retval;
41
42 config = (struct gpio_generic_chip_config) {
43 .dev = dev,
44 .sz = 2,
45 .dat = base_addr + INPUT_REG_OFFSET,
46 .set = base_addr + OUTPUT_REG_OFFSET,
47 .dirout = base_addr + DIRECTION_REG_OFFSET,
48 };
49
50 retval = gpio_generic_chip_init(chip, &config);
51 if (retval)
52 return dev_err_probe(dev, retval,
53 "failed to initialize the generic GPIO chip\n");
54
55 chip->gc.ngpio = ngpios;
56
57 return devm_gpiochip_add_data(dev, &chip->gc, NULL);
58}
59
60static const struct of_device_id ts4800_gpio_of_match[] = {
61 { .compatible = "technologic,ts4800-gpio", },
62 {},
63};
64MODULE_DEVICE_TABLE(of, ts4800_gpio_of_match);
65
66static struct platform_driver ts4800_gpio_driver = {
67 .driver = {
68 .name = "ts4800-gpio",
69 .of_match_table = ts4800_gpio_of_match,
70 },
71 .probe = ts4800_gpio_probe,
72};
73
74module_platform_driver_probe(ts4800_gpio_driver, ts4800_gpio_probe);
75
76MODULE_AUTHOR("Julien Grossholtz <julien.grossholtz@savoirfairelinux.com>");
77MODULE_DESCRIPTION("TS4800 FPGA GPIO driver");
78MODULE_LICENSE("GPL v2");