ee8359e7742f16100712f20983767cbdde093275
[openwrt/openwrt.git] / target / linux / ath79 / files / drivers / gpio / gpio-rb91x-key.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for reset key gpio line on MikroTik RB91x board series.
4 * This line is shared between NAND ALE (goes through a latch),
5 * NAND IO7 and reset key. We make 3 virtual gpio lines from the
6 * single physical one:
7 * 1) Capable output one for NAND,
8 * 2) Capable input one for reset key,
9 * 3) And capable output one, aka "key-poll-disable",
10 * for NAND -- to syncronise NAND operation and key polling.
11 *
12 * Copyright (C) 2021 Denis Kalashnikov <denis281089@gmail.com>
13 */
14
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/gpio/driver.h>
21 #include <linux/platform_device.h>
22 #include <linux/of_platform.h>
23 #include <linux/of_gpio.h>
24 #include <linux/delay.h>
25
26 #define GPIO_RB91X_KEY_DRIVER_NAME "gpio-rb91x-key"
27
28 enum gpio_rb91x_key_gpios {
29 GPIO_RB91X_KEY_NAND,
30 GPIO_RB91X_KEY_POLL,
31 GPIO_RB91X_KEY_PDIS,
32
33 GPIO_RB91X_KEY_NGPIOS,
34 };
35
36 struct gpio_rb91x_key {
37 struct gpio_chip gc;
38 struct mutex mutex;
39 struct mutex poll_mutex;
40 int polling_disabled;
41 struct gpio_desc *gpio;
42 };
43
44 static inline struct gpio_rb91x_key *to_gpio_rb91x_key(struct gpio_chip *gc)
45 {
46 return container_of(gc, struct gpio_rb91x_key, gc);
47 }
48
49 static int gpio_rb91x_key_get(struct gpio_chip *gc, unsigned offset)
50 {
51 struct gpio_rb91x_key *drvdata = to_gpio_rb91x_key(gc);
52 struct gpio_desc *gpio = drvdata->gpio;
53 int val, bak_val;
54
55 switch (offset) {
56 case GPIO_RB91X_KEY_NAND:
57 mutex_lock(&drvdata->mutex);
58 val = gpiod_get_value_cansleep(gpio);
59 mutex_unlock(&drvdata->mutex);
60 break;
61 case GPIO_RB91X_KEY_PDIS:
62 mutex_lock(&drvdata->mutex);
63 val = drvdata->polling_disabled;
64 mutex_unlock(&drvdata->mutex);
65 break;
66 case GPIO_RB91X_KEY_POLL:
67 mutex_lock(&drvdata->poll_mutex);
68 mutex_lock(&drvdata->mutex);
69 bak_val = gpiod_get_raw_value_cansleep(gpio);
70 gpiod_direction_input(gpio);
71 /*
72 * Without this delay nothing works. Get it
73 * from mikrotik RouterOS linux kernel patches.
74 */
75 udelay(200);
76 val = gpiod_get_raw_value_cansleep(gpio);
77 gpiod_direction_output_raw(gpio, bak_val);
78 mutex_unlock(&drvdata->mutex);
79 mutex_unlock(&drvdata->poll_mutex);
80 break;
81 default:
82 return -EINVAL;
83 }
84
85 return val;
86 }
87
88 static int gpio_rb91x_key_direction_input(struct gpio_chip *gc, unsigned offset)
89 {
90 switch (offset) {
91 case GPIO_RB91X_KEY_POLL:
92 return 0;
93 default:
94 return -EINVAL;
95 }
96 }
97
98 static void gpio_rb91x_key_set(struct gpio_chip *gc, unsigned offset, int value)
99 {
100 struct gpio_rb91x_key *drvdata = to_gpio_rb91x_key(gc);
101 struct gpio_desc *gpio = drvdata->gpio;
102
103 mutex_lock(&drvdata->mutex);
104
105 switch (offset) {
106 case GPIO_RB91X_KEY_NAND:
107 gpiod_set_raw_value_cansleep(gpio, value);
108 break;
109 case GPIO_RB91X_KEY_PDIS:
110 if (value) {
111 if (!drvdata->polling_disabled) {
112 mutex_lock(&drvdata->poll_mutex);
113 drvdata->polling_disabled = 1;
114 }
115 } else {
116 if (drvdata->polling_disabled) {
117 mutex_unlock(&drvdata->poll_mutex);
118 drvdata->polling_disabled = 0;
119 }
120 }
121 break;
122 default:
123 break;
124 }
125
126 mutex_unlock(&drvdata->mutex);
127 }
128
129 static int gpio_rb91x_key_direction_output(struct gpio_chip *gc, unsigned offset,
130 int value)
131 {
132 switch (offset) {
133 case GPIO_RB91X_KEY_NAND:
134 case GPIO_RB91X_KEY_PDIS:
135 gpio_rb91x_key_set(gc, offset, value);
136 return 0;
137 default:
138 return -EINVAL;
139 }
140 }
141
142 static int gpio_rb91x_key_probe(struct platform_device *pdev)
143 {
144 struct gpio_rb91x_key *drvdata;
145 struct gpio_chip *gc;
146 struct device *dev = &pdev->dev;
147 struct device_node *of_node = dev->of_node;
148 int r;
149
150 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
151 if (!drvdata)
152 return -ENOMEM;
153
154 mutex_init(&drvdata->mutex);
155 mutex_init(&drvdata->poll_mutex);
156
157 drvdata->gpio = devm_gpiod_get(dev, NULL, GPIOD_OUT_LOW);
158 if (IS_ERR(drvdata->gpio)) {
159 if (PTR_ERR(drvdata->gpio) != -EPROBE_DEFER) {
160 dev_err(dev, "failed to get gpio: %ld\n",
161 PTR_ERR(drvdata->gpio));
162 }
163 return PTR_ERR(drvdata->gpio);
164 }
165
166 gc = &drvdata->gc;
167 gc->label = GPIO_RB91X_KEY_DRIVER_NAME;
168 gc->can_sleep = 1;
169 gc->base = -1;
170 gc->ngpio = GPIO_RB91X_KEY_NGPIOS;
171 gc->get = gpio_rb91x_key_get;
172 gc->set = gpio_rb91x_key_set;
173 gc->direction_output = gpio_rb91x_key_direction_output;
174 gc->direction_input = gpio_rb91x_key_direction_input;
175 gc->of_node = of_node;
176
177 platform_set_drvdata(pdev, drvdata);
178
179 r = gpiochip_add(&drvdata->gc);
180 if (r) {
181 dev_err(dev, "gpiochip_add() failed: %d\n", r);
182 return r;
183 }
184
185 return 0;
186 }
187
188 static int gpio_rb91x_key_remove(struct platform_device *pdev)
189 {
190 struct gpio_rb91x_key *drvdata = platform_get_drvdata(pdev);
191
192 gpiochip_remove(&drvdata->gc);
193 return 0;
194 }
195
196 static const struct of_device_id gpio_rb91x_key_match[] = {
197 { .compatible = "mikrotik,"GPIO_RB91X_KEY_DRIVER_NAME },
198 {},
199 };
200
201 MODULE_DEVICE_TABLE(of, gpio_rb91x_key_match);
202
203 static struct platform_driver gpio_rb91x_key_driver = {
204 .probe = gpio_rb91x_key_probe,
205 .remove = gpio_rb91x_key_remove,
206 .driver = {
207 .name = GPIO_RB91X_KEY_DRIVER_NAME,
208 .owner = THIS_MODULE,
209 .of_match_table = gpio_rb91x_key_match,
210 },
211 };
212
213 module_platform_driver(gpio_rb91x_key_driver);
214
215 MODULE_DESCRIPTION("Driver for reset key gpio line shared with NAND for MikroTik RB91x board series.");
216 MODULE_AUTHOR("Denis Kalashnikov <denis281089@gmail.com>");
217 MODULE_LICENSE("GPL v2");
218 MODULE_ALIAS("platform:" GPIO_RB91X_KEY_DRIVER_NAME);