1 // SPDX-License-Identifier: GPL-2.0-only
3 * GPIO driver for Exar XR17V35X chip
8 #include <linux/bitops.h>
9 #include <linux/device.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/idr.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
19 #define EXAR_OFFSET_MPIOLVL_LO 0x90
20 #define EXAR_OFFSET_MPIOSEL_LO 0x93
21 #define EXAR_OFFSET_MPIOLVL_HI 0x96
22 #define EXAR_OFFSET_MPIOSEL_HI 0x99
24 #define DRIVER_NAME "gpio_exar"
26 static DEFINE_IDA(ida_index);
28 struct exar_gpio_chip {
29 struct gpio_chip gpio_chip;
30 struct regmap *regmap;
33 unsigned int first_pin;
37 exar_offset_to_sel_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
39 return (offset + exar_gpio->first_pin) / 8 ? EXAR_OFFSET_MPIOSEL_HI
40 : EXAR_OFFSET_MPIOSEL_LO;
44 exar_offset_to_lvl_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
46 return (offset + exar_gpio->first_pin) / 8 ? EXAR_OFFSET_MPIOLVL_HI
47 : EXAR_OFFSET_MPIOLVL_LO;
51 exar_offset_to_bit(struct exar_gpio_chip *exar_gpio, unsigned int offset)
53 return (offset + exar_gpio->first_pin) % 8;
56 static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
58 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
59 unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
60 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
62 if (regmap_test_bits(exar_gpio->regmap, addr, BIT(bit)))
63 return GPIO_LINE_DIRECTION_IN;
65 return GPIO_LINE_DIRECTION_OUT;
68 static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
70 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
71 unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
72 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
74 return !!(regmap_test_bits(exar_gpio->regmap, addr, BIT(bit)));
77 static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
80 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
81 unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
82 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
85 regmap_set_bits(exar_gpio->regmap, addr, BIT(bit));
87 regmap_clear_bits(exar_gpio->regmap, addr, BIT(bit));
90 static int exar_direction_output(struct gpio_chip *chip, unsigned int offset,
93 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
94 unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
95 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
97 exar_set_value(chip, offset, value);
98 regmap_clear_bits(exar_gpio->regmap, addr, BIT(bit));
103 static int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
105 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
106 unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
107 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
109 regmap_set_bits(exar_gpio->regmap, addr, BIT(bit));
114 static void exar_devm_ida_free(void *data)
116 struct exar_gpio_chip *exar_gpio = data;
118 ida_free(&ida_index, exar_gpio->index);
121 static const struct regmap_config exar_regmap_config = {
127 static int gpio_exar_probe(struct platform_device *pdev)
129 struct device *dev = &pdev->dev;
130 struct pci_dev *pcidev = to_pci_dev(dev->parent);
131 struct exar_gpio_chip *exar_gpio;
132 u32 first_pin, ngpios;
137 * The UART driver must have mapped region 0 prior to registering this
140 p = pcim_iomap_table(pcidev)[0];
144 ret = device_property_read_u32(dev, "exar,first-pin", &first_pin);
148 ret = device_property_read_u32(dev, "ngpios", &ngpios);
152 exar_gpio = devm_kzalloc(dev, sizeof(*exar_gpio), GFP_KERNEL);
157 * We don't need to check the return values of mmio regmap operations (unless
158 * the regmap has a clock attached which is not the case here).
160 exar_gpio->regmap = devm_regmap_init_mmio(dev, p, &exar_regmap_config);
161 if (IS_ERR(exar_gpio->regmap))
162 return PTR_ERR(exar_gpio->regmap);
164 index = ida_alloc(&ida_index, GFP_KERNEL);
168 ret = devm_add_action_or_reset(dev, exar_devm_ida_free, exar_gpio);
172 sprintf(exar_gpio->name, "exar_gpio%d", index);
173 exar_gpio->gpio_chip.label = exar_gpio->name;
174 exar_gpio->gpio_chip.parent = dev;
175 exar_gpio->gpio_chip.direction_output = exar_direction_output;
176 exar_gpio->gpio_chip.direction_input = exar_direction_input;
177 exar_gpio->gpio_chip.get_direction = exar_get_direction;
178 exar_gpio->gpio_chip.get = exar_get_value;
179 exar_gpio->gpio_chip.set = exar_set_value;
180 exar_gpio->gpio_chip.base = -1;
181 exar_gpio->gpio_chip.ngpio = ngpios;
182 exar_gpio->index = index;
183 exar_gpio->first_pin = first_pin;
185 ret = devm_gpiochip_add_data(dev, &exar_gpio->gpio_chip, exar_gpio);
189 platform_set_drvdata(pdev, exar_gpio);
194 static struct platform_driver gpio_exar_driver = {
195 .probe = gpio_exar_probe,
201 module_platform_driver(gpio_exar_driver);
203 MODULE_ALIAS("platform:" DRIVER_NAME);
204 MODULE_DESCRIPTION("Exar GPIO driver");
206 MODULE_LICENSE("GPL");