]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpio/gpio-exar.c
Merge tag 'media/v5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[mirror_ubuntu-hirsute-kernel.git] / drivers / gpio / gpio-exar.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
6596e59e
SM
2/*
3 * GPIO driver for Exar XR17V35X chip
4 *
5 * Copyright (C) 2015 Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>
6596e59e 6 */
1bfaf129 7
6596e59e
SM
8#include <linux/bitops.h>
9#include <linux/device.h>
10#include <linux/gpio/driver.h>
26ced453 11#include <linux/idr.h>
6596e59e
SM
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>
36fb7218 17#include <linux/regmap.h>
6596e59e
SM
18
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
23
24#define DRIVER_NAME "gpio_exar"
25
26static DEFINE_IDA(ida_index);
27
28struct exar_gpio_chip {
29 struct gpio_chip gpio_chip;
36fb7218 30 struct regmap *regmap;
6596e59e 31 int index;
6596e59e 32 char name[20];
380b1e2f 33 unsigned int first_pin;
6596e59e
SM
34};
35
696868d0
BG
36static unsigned int
37exar_offset_to_sel_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
38{
39 return (offset + exar_gpio->first_pin) / 8 ? EXAR_OFFSET_MPIOSEL_HI
40 : EXAR_OFFSET_MPIOSEL_LO;
41}
42
43static unsigned int
44exar_offset_to_lvl_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
45{
46 return (offset + exar_gpio->first_pin) / 8 ? EXAR_OFFSET_MPIOLVL_HI
47 : EXAR_OFFSET_MPIOLVL_LO;
48}
49
50static unsigned int
51exar_offset_to_bit(struct exar_gpio_chip *exar_gpio, unsigned int offset)
52{
53 return (offset + exar_gpio->first_pin) % 8;
54}
55
6596e59e
SM
56static int exar_get_direction(struct gpio_chip *chip, unsigned int offset)
57{
380b1e2f 58 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
696868d0
BG
59 unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset);
60 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
6596e59e 61
36fb7218 62 if (regmap_test_bits(exar_gpio->regmap, addr, BIT(bit)))
e42615ec
MV
63 return GPIO_LINE_DIRECTION_IN;
64
65 return GPIO_LINE_DIRECTION_OUT;
6596e59e
SM
66}
67
68static int exar_get_value(struct gpio_chip *chip, unsigned int offset)
69{
380b1e2f 70 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
696868d0
BG
71 unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
72 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
6596e59e 73
36fb7218 74 return !!(regmap_test_bits(exar_gpio->regmap, addr, BIT(bit)));
6596e59e
SM
75}
76
77static void exar_set_value(struct gpio_chip *chip, unsigned int offset,
78 int value)
79{
380b1e2f 80 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip);
696868d0
BG
81 unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset);
82 unsigned int bit = exar_offset_to_bit(exar_gpio, offset);
6596e59e 83
36fb7218
BG
84 if (value)
85 regmap_set_bits(exar_gpio->regmap, addr, BIT(bit));
86 else
87 regmap_clear_bits(exar_gpio->regmap, addr, BIT(bit));
6596e59e
SM
88}
89
eddeae07
AL
90static int exar_direction_output(struct gpio_chip *chip, unsigned int offset,
91 int value)
92{
36fb7218
BG
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);
96
eddeae07 97 exar_set_value(chip, offset, value);
36fb7218
BG
98 regmap_clear_bits(exar_gpio->regmap, addr, BIT(bit));
99
100 return 0;
eddeae07
AL
101}
102
103static int exar_direction_input(struct gpio_chip *chip, unsigned int offset)
104{
36fb7218
BG
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);
108
109 regmap_set_bits(exar_gpio->regmap, addr, BIT(bit));
110
111 return 0;
eddeae07
AL
112}
113
5300ebb6
BG
114static void exar_devm_ida_free(void *data)
115{
116 struct exar_gpio_chip *exar_gpio = data;
117
118 ida_free(&ida_index, exar_gpio->index);
119}
120
36fb7218
BG
121static const struct regmap_config exar_regmap_config = {
122 .name = "exar-gpio",
123 .reg_bits = 16,
124 .val_bits = 8,
125};
126
6596e59e
SM
127static int gpio_exar_probe(struct platform_device *pdev)
128{
0c2c7e13
BG
129 struct device *dev = &pdev->dev;
130 struct pci_dev *pcidev = to_pci_dev(dev->parent);
6596e59e 131 struct exar_gpio_chip *exar_gpio;
380b1e2f 132 u32 first_pin, ngpios;
6596e59e
SM
133 void __iomem *p;
134 int index, ret;
135
6596e59e 136 /*
8847f5f9
JK
137 * The UART driver must have mapped region 0 prior to registering this
138 * device - use it.
6596e59e 139 */
8847f5f9 140 p = pcim_iomap_table(pcidev)[0];
6596e59e
SM
141 if (!p)
142 return -ENOMEM;
143
0c2c7e13 144 ret = device_property_read_u32(dev, "exar,first-pin", &first_pin);
380b1e2f
JK
145 if (ret)
146 return ret;
147
0c2c7e13 148 ret = device_property_read_u32(dev, "ngpios", &ngpios);
380b1e2f
JK
149 if (ret)
150 return ret;
151
0c2c7e13 152 exar_gpio = devm_kzalloc(dev, sizeof(*exar_gpio), GFP_KERNEL);
6596e59e
SM
153 if (!exar_gpio)
154 return -ENOMEM;
155
36fb7218
BG
156 /*
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).
159 */
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);
6596e59e 163
8e27c2ae 164 index = ida_alloc(&ida_index, GFP_KERNEL);
36fb7218
BG
165 if (index < 0)
166 return index;
6596e59e 167
5300ebb6
BG
168 ret = devm_add_action_or_reset(dev, exar_devm_ida_free, exar_gpio);
169 if (ret)
170 return ret;
171
6596e59e
SM
172 sprintf(exar_gpio->name, "exar_gpio%d", index);
173 exar_gpio->gpio_chip.label = exar_gpio->name;
0c2c7e13 174 exar_gpio->gpio_chip.parent = dev;
6596e59e
SM
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;
380b1e2f 181 exar_gpio->gpio_chip.ngpio = ngpios;
6596e59e 182 exar_gpio->index = index;
380b1e2f 183 exar_gpio->first_pin = first_pin;
6596e59e 184
0c2c7e13 185 ret = devm_gpiochip_add_data(dev, &exar_gpio->gpio_chip, exar_gpio);
6596e59e 186 if (ret)
5300ebb6 187 return ret;
6596e59e
SM
188
189 platform_set_drvdata(pdev, exar_gpio);
190
6596e59e
SM
191 return 0;
192}
193
194static struct platform_driver gpio_exar_driver = {
195 .probe = gpio_exar_probe,
6596e59e
SM
196 .driver = {
197 .name = DRIVER_NAME,
198 },
199};
200
201module_platform_driver(gpio_exar_driver);
202
203MODULE_ALIAS("platform:" DRIVER_NAME);
204MODULE_DESCRIPTION("Exar GPIO driver");
205MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>");
206MODULE_LICENSE("GPL");