1 // SPDX-License-Identifier: GPL-2.0-only
3 * GPIO driver for Exar XR17V35X chip
5 * Copyright (C) 2015 Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>
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>
18 #define EXAR_OFFSET_MPIOLVL_LO 0x90
19 #define EXAR_OFFSET_MPIOSEL_LO 0x93
20 #define EXAR_OFFSET_MPIOLVL_HI 0x96
21 #define EXAR_OFFSET_MPIOSEL_HI 0x99
23 #define DRIVER_NAME "gpio_exar"
25 static DEFINE_IDA(ida_index);
27 struct exar_gpio_chip {
28 struct gpio_chip gpio_chip;
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 void exar_update(struct gpio_chip *chip, unsigned int reg, int val,
59 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
62 mutex_lock(&exar_gpio->lock);
63 temp = readb(exar_gpio->regs + reg);
67 writeb(temp, exar_gpio->regs + reg);
68 mutex_unlock(&exar_gpio->lock);
71 static int exar_set_direction(struct gpio_chip *chip, int direction,
74 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
75 unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
76 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
78 exar_update(chip, addr, direction, bit);
82 static int exar_get(struct gpio_chip *chip, unsigned int reg)
84 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
87 mutex_lock(&exar_gpio->lock);
88 value = readb(exar_gpio->regs + reg);
89 mutex_unlock(&exar_gpio->lock);
94 static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
96 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
97 unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
98 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
100 if (exar_get(chip, addr) & BIT(bit))
101 return GPIO_LINE_DIRECTION_IN;
103 return GPIO_LINE_DIRECTION_OUT;
106 static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
108 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
109 unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
110 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
112 return !!(exar_get(chip, addr) & BIT(bit));
115 static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
118 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
119 unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
120 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
122 exar_update(chip, addr, value, bit);
125 static int exar_direction_output(struct gpio_chip *chip, unsigned int offset,
128 exar_set_value(chip, offset, value);
129 return exar_set_direction(chip, 0, offset);
132 static int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
134 return exar_set_direction(chip, 1, offset);
137 static int gpio_exar_probe(struct platform_device *pdev)
139 struct device *dev = &pdev->dev;
140 struct pci_dev *pcidev = to_pci_dev(dev->parent);
141 struct exar_gpio_chip *exar_gpio;
142 u32 first_pin, ngpios;
147 * The UART driver must have mapped region 0 prior to registering this
150 p = pcim_iomap_table(pcidev)[0];
154 ret = device_property_read_u32(dev, "exar,first-pin", &first_pin);
158 ret = device_property_read_u32(dev, "ngpios", &ngpios);
162 exar_gpio = devm_kzalloc(dev, sizeof(*exar_gpio), GFP_KERNEL);
166 mutex_init(&exar_gpio->lock);
168 index = ida_alloc(&ida_index, GFP_KERNEL);
171 goto err_mutex_destroy;
174 sprintf(exar_gpio->name, "exar_gpio%d", index);
175 exar_gpio->gpio_chip.label = exar_gpio->name;
176 exar_gpio->gpio_chip.parent = dev;
177 exar_gpio->gpio_chip.direction_output = exar_direction_output;
178 exar_gpio->gpio_chip.direction_input = exar_direction_input;
179 exar_gpio->gpio_chip.get_direction = exar_get_direction;
180 exar_gpio->gpio_chip.get = exar_get_value;
181 exar_gpio->gpio_chip.set = exar_set_value;
182 exar_gpio->gpio_chip.base = -1;
183 exar_gpio->gpio_chip.ngpio = ngpios;
185 exar_gpio->index = index;
186 exar_gpio->first_pin = first_pin;
188 ret = devm_gpiochip_add_data(dev, &exar_gpio->gpio_chip, exar_gpio);
192 platform_set_drvdata(pdev, exar_gpio);
197 ida_free(&ida_index, index);
199 mutex_destroy(&exar_gpio->lock);
203 static int gpio_exar_remove(struct platform_device *pdev)
205 struct exar_gpio_chip *exar_gpio = platform_get_drvdata(pdev);
207 ida_free(&ida_index, exar_gpio->index);
208 mutex_destroy(&exar_gpio->lock);
213 static struct platform_driver gpio_exar_driver = {
214 .probe = gpio_exar_probe,
215 .remove = gpio_exar_remove,
221 module_platform_driver(gpio_exar_driver);
223 MODULE_ALIAS("platform:" DRIVER_NAME);
224 MODULE_DESCRIPTION("Exar GPIO driver");
225 MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
226 MODULE_LICENSE("GPL");