realtek: use automatic GPIO numbering for RTL8231
[openwrt/openwrt.git] / target / linux / realtek / files-5.10 / drivers / gpio / gpio-rtl8231.c
1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <linux/gpio/driver.h>
4 #include <linux/module.h>
5 #include <linux/platform_device.h>
6 #include <linux/delay.h>
7 #include <asm/mach-rtl838x/mach-rtl83xx.h>
8
9 /* RTL8231 registers for LED control */
10 #define RTL8231_LED_FUNC0 0x0000
11 #define RTL8231_GPIO_PIN_SEL(gpio) ((0x0002) + ((gpio) >> 4))
12 #define RTL8231_GPIO_DIR(gpio) ((0x0005) + ((gpio) >> 4))
13 #define RTL8231_GPIO_DATA(gpio) ((0x001C) + ((gpio) >> 4))
14
15 #define USEC_TIMEOUT 5000
16
17 struct rtl8231_gpios {
18 struct gpio_chip gc;
19 struct device *dev;
20 u32 id;
21 int smi_bus_id;
22 u16 reg_shadow[0x20];
23 u32 reg_cached;
24 int ext_gpio_indrt_access;
25 };
26
27 extern struct mutex smi_lock;
28 extern struct rtl83xx_soc_info soc_info;
29
30 static u32 rtl8231_read(struct rtl8231_gpios *gpios, u32 reg)
31 {
32 u32 t = 0, n = 0;
33 u8 bus_id = gpios->smi_bus_id;
34
35 reg &= 0x1f;
36 bus_id &= 0x1f;
37
38 /* Calculate read register address */
39 t = (bus_id << 2) | (reg << 7);
40
41 /* Set execution bit: cleared when operation completed */
42 t |= 1;
43
44 // Start execution
45 sw_w32(t, gpios->ext_gpio_indrt_access);
46 do {
47 udelay(1);
48 t = sw_r32(gpios->ext_gpio_indrt_access);
49 n++;
50 } while ((t & 1) && (n < USEC_TIMEOUT));
51
52 if (n >= USEC_TIMEOUT)
53 return 0x80000000;
54
55 pr_debug("%s: %x, %x, %x\n", __func__, bus_id, reg, (t & 0xffff0000) >> 16);
56
57 return (t & 0xffff0000) >> 16;
58 }
59
60 static int rtl8231_write(struct rtl8231_gpios *gpios, u32 reg, u32 data)
61 {
62 u32 t = 0, n = 0;
63 u8 bus_id = gpios->smi_bus_id;
64
65 pr_debug("%s: %x, %x, %x\n", __func__, bus_id, reg, data);
66 reg &= 0x1f;
67 bus_id &= 0x1f;
68
69 t = (bus_id << 2) | (reg << 7) | (data << 16);
70 /* Set write bit */
71 t |= 2;
72
73 /* Set execution bit: cleared when operation completed */
74 t |= 1;
75
76 // Start execution
77 sw_w32(t, gpios->ext_gpio_indrt_access);
78 do {
79 udelay(1);
80 t = sw_r32(gpios->ext_gpio_indrt_access);
81 } while ((t & 1) && (n < USEC_TIMEOUT));
82
83 if (n >= USEC_TIMEOUT)
84 return -1;
85
86 return 0;
87 }
88
89 static u32 rtl8231_read_cached(struct rtl8231_gpios *gpios, u32 reg)
90 {
91 if (reg > 0x1f)
92 return 0;
93
94 if (gpios->reg_cached & (1 << reg))
95 return gpios->reg_shadow[reg];
96
97 return rtl8231_read(gpios, reg);
98 }
99
100 /* Set Direction of the RTL8231 pin:
101 * dir 1: input
102 * dir 0: output
103 */
104 static int rtl8231_pin_dir(struct rtl8231_gpios *gpios, u32 gpio, u32 dir)
105 {
106 u32 v;
107 int pin_sel_addr = RTL8231_GPIO_PIN_SEL(gpio);
108 int pin_dir_addr = RTL8231_GPIO_DIR(gpio);
109 int dpin = gpio % 16;
110
111 if (gpio > 31) {
112 pr_debug("WARNING: HIGH pin\n");
113 dpin += 5;
114 pin_dir_addr = pin_sel_addr;
115 }
116
117 v = rtl8231_read_cached(gpios, pin_dir_addr);
118 if (v & 0x80000000) {
119 pr_err("Error reading RTL8231\n");
120 return -1;
121 }
122
123 v = (v & ~(1 << dpin)) | (dir << dpin);
124 rtl8231_write(gpios, pin_dir_addr, v);
125 gpios->reg_shadow[pin_dir_addr] = v;
126 gpios->reg_cached |= 1 << pin_dir_addr;
127 return 0;
128 }
129
130 static int rtl8231_pin_dir_get(struct rtl8231_gpios *gpios, u32 gpio, u32 *dir)
131 {
132 /* dir 1: input
133 * dir 0: output
134 */
135
136 u32 v;
137 int pin_dir_addr = RTL8231_GPIO_DIR(gpio);
138 int pin = gpio % 16;
139
140 if (gpio > 31) {
141 pin_dir_addr = RTL8231_GPIO_PIN_SEL(gpio);
142 pin += 5;
143 }
144
145 v = rtl8231_read(gpios, pin_dir_addr);
146 if (v & (1 << pin))
147 *dir = 1;
148 else
149 *dir = 0;
150 return 0;
151 }
152
153 static int rtl8231_pin_set(struct rtl8231_gpios *gpios, u32 gpio, u32 data)
154 {
155 u32 v = rtl8231_read(gpios, RTL8231_GPIO_DATA(gpio));
156
157 pr_debug("%s: %d to %d\n", __func__, gpio, data);
158 if (v & 0x80000000) {
159 pr_err("Error reading RTL8231\n");
160 return -1;
161 }
162 v = (v & ~(1 << (gpio % 16))) | (data << (gpio % 16));
163 rtl8231_write(gpios, RTL8231_GPIO_DATA(gpio), v);
164 gpios->reg_shadow[RTL8231_GPIO_DATA(gpio)] = v;
165 gpios->reg_cached |= 1 << RTL8231_GPIO_DATA(gpio);
166 return 0;
167 }
168
169 static int rtl8231_pin_get(struct rtl8231_gpios *gpios, u32 gpio, u16 *state)
170 {
171 u32 v = rtl8231_read(gpios, RTL8231_GPIO_DATA(gpio));
172
173 if (v & 0x80000000) {
174 pr_err("Error reading RTL8231\n");
175 return -1;
176 }
177
178 *state = v & 0xffff;
179 return 0;
180 }
181
182 static int rtl8231_direction_input(struct gpio_chip *gc, unsigned int offset)
183 {
184 int err;
185 struct rtl8231_gpios *gpios = gpiochip_get_data(gc);
186
187 pr_debug("%s: %d\n", __func__, offset);
188 mutex_lock(&smi_lock);
189 err = rtl8231_pin_dir(gpios, offset, 1);
190 mutex_unlock(&smi_lock);
191 return err;
192 }
193
194 static int rtl8231_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
195 {
196 int err;
197 struct rtl8231_gpios *gpios = gpiochip_get_data(gc);
198
199 pr_debug("%s: %d\n", __func__, offset);
200 mutex_lock(&smi_lock);
201 err = rtl8231_pin_dir(gpios, offset, 0);
202 mutex_unlock(&smi_lock);
203 if (!err)
204 err = rtl8231_pin_set(gpios, offset, value);
205 return err;
206 }
207
208 static int rtl8231_get_direction(struct gpio_chip *gc, unsigned int offset)
209 {
210 u32 v = 0;
211 struct rtl8231_gpios *gpios = gpiochip_get_data(gc);
212
213 pr_debug("%s: %d\n", __func__, offset);
214 mutex_lock(&smi_lock);
215 rtl8231_pin_dir_get(gpios, offset, &v);
216 mutex_unlock(&smi_lock);
217 return v;
218 }
219
220 static int rtl8231_gpio_get(struct gpio_chip *gc, unsigned int offset)
221 {
222 u16 state = 0;
223 struct rtl8231_gpios *gpios = gpiochip_get_data(gc);
224
225 mutex_lock(&smi_lock);
226 rtl8231_pin_get(gpios, offset, &state);
227 mutex_unlock(&smi_lock);
228 if (state & (1 << (offset % 16)))
229 return 1;
230 return 0;
231 }
232
233 void rtl8231_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
234 {
235 struct rtl8231_gpios *gpios = gpiochip_get_data(gc);
236
237 rtl8231_pin_set(gpios, offset, value);
238 }
239
240 int rtl8231_init(struct rtl8231_gpios *gpios)
241 {
242 pr_info("%s called, MDIO bus ID: %d\n", __func__, gpios->smi_bus_id);
243
244 gpios->reg_cached = 0;
245
246 if (soc_info.family == RTL8390_FAMILY_ID) {
247 // RTL8390: Enable external gpio in global led control register
248 sw_w32_mask(0x7 << 18, 0x4 << 18, RTL839X_LED_GLB_CTRL);
249 } else if (soc_info.family == RTL8380_FAMILY_ID) {
250 // RTL8380: Enable RTL8231 indirect access mode
251 sw_w32_mask(0, 1, RTL838X_EXTRA_GPIO_CTRL);
252 sw_w32_mask(3, 1, RTL838X_DMY_REG5);
253 }
254
255 /* Select GPIO functionality and force input direction for pins 0-36 */
256 rtl8231_write(gpios, RTL8231_GPIO_PIN_SEL(0), 0xffff);
257 rtl8231_write(gpios, RTL8231_GPIO_DIR(0), 0xffff);
258 rtl8231_write(gpios, RTL8231_GPIO_PIN_SEL(16), 0xffff);
259 rtl8231_write(gpios, RTL8231_GPIO_DIR(16), 0xffff);
260 rtl8231_write(gpios, RTL8231_GPIO_PIN_SEL(32), 0x03ff);
261
262 /* Set LED_Start to enable drivers for output mode */
263 rtl8231_write(gpios, RTL8231_LED_FUNC0, 1 << 1);
264
265 return 0;
266 }
267
268 static const struct of_device_id rtl8231_gpio_of_match[] = {
269 { .compatible = "realtek,rtl8231-gpio" },
270 {},
271 };
272
273 MODULE_DEVICE_TABLE(of, rtl8231_gpio_of_match);
274
275 static int rtl8231_gpio_probe(struct platform_device *pdev)
276 {
277 struct device *dev = &pdev->dev;
278 struct device_node *np = dev->of_node;
279 struct rtl8231_gpios *gpios;
280 int err;
281 u32 indirect_bus_id;
282
283 pr_info("Probing RTL8231 GPIOs\n");
284
285 if (!np) {
286 dev_err(&pdev->dev, "No DT found\n");
287 return -EINVAL;
288 }
289
290 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
291 if (!gpios)
292 return -ENOMEM;
293
294 gpios->id = soc_info.id;
295 if (soc_info.family == RTL8380_FAMILY_ID) {
296 gpios->ext_gpio_indrt_access = RTL838X_EXT_GPIO_INDRT_ACCESS;
297 }
298
299 if (soc_info.family == RTL8390_FAMILY_ID) {
300 gpios->ext_gpio_indrt_access = RTL839X_EXT_GPIO_INDRT_ACCESS;
301 }
302
303 /*
304 * We use a default MDIO bus ID for the 8231 of 0, which can be overriden
305 * by the indirect-access-bus-id property in the dts.
306 */
307 gpios->smi_bus_id = 0;
308 of_property_read_u32(np, "indirect-access-bus-id", &indirect_bus_id);
309 gpios->smi_bus_id = indirect_bus_id;
310
311 rtl8231_init(gpios);
312
313 gpios->dev = dev;
314 gpios->gc.base = -1;
315 gpios->gc.ngpio = 37;
316 gpios->gc.label = "rtl8231";
317 gpios->gc.parent = dev;
318 gpios->gc.owner = THIS_MODULE;
319 gpios->gc.can_sleep = true;
320
321 gpios->gc.direction_input = rtl8231_direction_input;
322 gpios->gc.direction_output = rtl8231_direction_output;
323 gpios->gc.set = rtl8231_gpio_set;
324 gpios->gc.get = rtl8231_gpio_get;
325 gpios->gc.get_direction = rtl8231_get_direction;
326
327 err = devm_gpiochip_add_data(dev, &gpios->gc, gpios);
328 return err;
329 }
330
331 static struct platform_driver rtl8231_gpio_driver = {
332 .driver = {
333 .name = "rtl8231-gpio",
334 .of_match_table = rtl8231_gpio_of_match,
335 },
336 .probe = rtl8231_gpio_probe,
337 };
338
339 module_platform_driver(rtl8231_gpio_driver);
340
341 MODULE_DESCRIPTION("Realtek RTL8231 GPIO expansion chip support");
342 MODULE_LICENSE("GPL v2");