1 // SPDX-License-Identifier: GPL-2.0-or-later
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
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.
12 * Copyright (C) 2021 Denis Kalashnikov <denis281089@gmail.com>
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>
26 #define GPIO_RB91X_KEY_DRIVER_NAME "gpio-rb91x-key"
28 enum gpio_rb91x_key_gpios
{
33 GPIO_RB91X_KEY_NGPIOS
,
36 struct gpio_rb91x_key
{
39 struct mutex poll_mutex
;
41 struct gpio_desc
*gpio
;
44 static inline struct gpio_rb91x_key
*to_gpio_rb91x_key(struct gpio_chip
*gc
)
46 return container_of(gc
, struct gpio_rb91x_key
, gc
);
49 static int gpio_rb91x_key_get(struct gpio_chip
*gc
, unsigned offset
)
51 struct gpio_rb91x_key
*drvdata
= to_gpio_rb91x_key(gc
);
52 struct gpio_desc
*gpio
= drvdata
->gpio
;
56 case GPIO_RB91X_KEY_NAND
:
57 mutex_lock(&drvdata
->mutex
);
58 val
= gpiod_get_value_cansleep(gpio
);
59 mutex_unlock(&drvdata
->mutex
);
61 case GPIO_RB91X_KEY_PDIS
:
62 mutex_lock(&drvdata
->mutex
);
63 val
= drvdata
->polling_disabled
;
64 mutex_unlock(&drvdata
->mutex
);
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
);
72 * Without this delay nothing works. Get it
73 * from mikrotik RouterOS linux kernel patches.
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
);
88 static int gpio_rb91x_key_direction_input(struct gpio_chip
*gc
, unsigned offset
)
91 case GPIO_RB91X_KEY_POLL
:
98 static void gpio_rb91x_key_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
100 struct gpio_rb91x_key
*drvdata
= to_gpio_rb91x_key(gc
);
101 struct gpio_desc
*gpio
= drvdata
->gpio
;
103 mutex_lock(&drvdata
->mutex
);
106 case GPIO_RB91X_KEY_NAND
:
107 gpiod_set_raw_value_cansleep(gpio
, value
);
109 case GPIO_RB91X_KEY_PDIS
:
111 if (!drvdata
->polling_disabled
) {
112 mutex_lock(&drvdata
->poll_mutex
);
113 drvdata
->polling_disabled
= 1;
116 if (drvdata
->polling_disabled
) {
117 mutex_unlock(&drvdata
->poll_mutex
);
118 drvdata
->polling_disabled
= 0;
126 mutex_unlock(&drvdata
->mutex
);
129 static int gpio_rb91x_key_direction_output(struct gpio_chip
*gc
, unsigned offset
,
133 case GPIO_RB91X_KEY_NAND
:
134 case GPIO_RB91X_KEY_PDIS
:
135 gpio_rb91x_key_set(gc
, offset
, value
);
142 static int gpio_rb91x_key_probe(struct platform_device
*pdev
)
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
;
150 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
154 mutex_init(&drvdata
->mutex
);
155 mutex_init(&drvdata
->poll_mutex
);
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
));
163 return PTR_ERR(drvdata
->gpio
);
167 gc
->label
= GPIO_RB91X_KEY_DRIVER_NAME
;
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
;
177 platform_set_drvdata(pdev
, drvdata
);
179 r
= gpiochip_add(&drvdata
->gc
);
181 dev_err(dev
, "gpiochip_add() failed: %d\n", r
);
188 static int gpio_rb91x_key_remove(struct platform_device
*pdev
)
190 struct gpio_rb91x_key
*drvdata
= platform_get_drvdata(pdev
);
192 gpiochip_remove(&drvdata
->gc
);
196 static const struct of_device_id gpio_rb91x_key_match
[] = {
197 { .compatible
= "mikrotik,"GPIO_RB91X_KEY_DRIVER_NAME
},
201 MODULE_DEVICE_TABLE(of
, gpio_rb91x_key_match
);
203 static struct platform_driver gpio_rb91x_key_driver
= {
204 .probe
= gpio_rb91x_key_probe
,
205 .remove
= gpio_rb91x_key_remove
,
207 .name
= GPIO_RB91X_KEY_DRIVER_NAME
,
208 .owner
= THIS_MODULE
,
209 .of_match_table
= gpio_rb91x_key_match
,
213 module_platform_driver(gpio_rb91x_key_driver
);
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
);