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