diff options
author | Denis Kalashnikov <denis281089@gmail.com> | 2022-01-19 13:25:05 +0300 |
---|---|---|
committer | Koen Vandeputte <koen.vandeputte@ncentric.com> | 2022-01-19 16:40:12 +0100 |
commit | ec85e48a113514502563a06c5d0278a57a8b6b86 (patch) | |
tree | c25f6fa403e8fa36adb342fa74aca77fe09e7ab7 /target/linux/ath79/files | |
parent | 522e414dcbe478fa150a49b743e15999026bf774 (diff) | |
download | upstream-ec85e48a113514502563a06c5d0278a57a8b6b86.tar.gz upstream-ec85e48a113514502563a06c5d0278a57a8b6b86.tar.bz2 upstream-ec85e48a113514502563a06c5d0278a57a8b6b86.zip |
ath79: add support for reset key on MikroTik RB912UAG-2HPnD
On MikroTik RB91x board series a reset key shares SoC gpio
line #15 with NAND ALE and NAND IO7. So we need a custom
gpio driver to manage this non-trivial connection schema.
Also rb91x-nand needs to have an ability to disable a polling
of the key while it works with NAND.
While we've been integrating rb91x-key into a firmware, we've
figured out that:
* In the gpio-latch driver we need to add a "cansleep" suffix to
several gpiolib calls,
* When gpio-latch and rb91x-nand fail to get a gpio and an error
is -EPROBE_DEFER, they shouldn't report about this, since this
actually is not an error and occurs when the gpio-latch probe
function is called before the rb91x-key probe.
We fix these related things here too.
Signed-off-by: Denis Kalashnikov <denis281089@gmail.com>
Reviewed-by: Sergey Ryazanov <ryazanov.s.a@gmail.com>
Tested-by: Koen Vandeputte <koen.vandeputte@ncentric.com>
Diffstat (limited to 'target/linux/ath79/files')
3 files changed, 240 insertions, 6 deletions
diff --git a/target/linux/ath79/files/drivers/gpio/gpio-latch.c b/target/linux/ath79/files/drivers/gpio/gpio-latch.c index f3545a663e..976e683230 100644 --- a/target/linux/ath79/files/drivers/gpio/gpio-latch.c +++ b/target/linux/ath79/files/drivers/gpio/gpio-latch.c @@ -62,7 +62,7 @@ gpio_latch_get(struct gpio_chip *gc, unsigned offset) int ret; gpio_latch_lock(glc, false); - ret = gpiod_get_value(glc->gpios[offset]); + ret = gpiod_get_raw_value_cansleep(glc->gpios[offset]); gpio_latch_unlock(glc, false); return ret; @@ -81,7 +81,7 @@ gpio_latch_set(struct gpio_chip *gc, unsigned offset, int value) } gpio_latch_lock(glc, enable_latch); - gpiod_set_raw_value(glc->gpios[offset], value); + gpiod_set_raw_value_cansleep(glc->gpios[offset], value); gpio_latch_unlock(glc, disable_latch); } @@ -133,8 +133,10 @@ static int gpio_latch_probe(struct platform_device *pdev) glc->gpios[i] = devm_gpiod_get_index_optional(dev, NULL, i, GPIOD_OUT_LOW); if (IS_ERR(glc->gpios[i])) { - dev_err(dev, "failed to get gpio %d: %d\n", i, - PTR_ERR(glc->gpios[i])); + if (PTR_ERR(glc->gpios[i]) != -EPROBE_DEFER) { + dev_err(dev, "failed to get gpio %d: %d\n", i, + PTR_ERR(glc->gpios[i])); + } return PTR_ERR(glc->gpios[i]); } } diff --git a/target/linux/ath79/files/drivers/gpio/gpio-rb91x-key.c b/target/linux/ath79/files/drivers/gpio/gpio-rb91x-key.c new file mode 100644 index 0000000000..ee8359e774 --- /dev/null +++ b/target/linux/ath79/files/drivers/gpio/gpio-rb91x-key.c @@ -0,0 +1,218 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Driver for reset key gpio line on MikroTik RB91x board series. + * This line is shared between NAND ALE (goes through a latch), + * NAND IO7 and reset key. We make 3 virtual gpio lines from the + * single physical one: + * 1) Capable output one for NAND, + * 2) Capable input one for reset key, + * 3) And capable output one, aka "key-poll-disable", + * for NAND -- to syncronise NAND operation and key polling. + * + * Copyright (C) 2021 Denis Kalashnikov <denis281089@gmail.com> + */ + +#include <linux/kernel.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/types.h> +#include <linux/gpio/consumer.h> +#include <linux/gpio/driver.h> +#include <linux/platform_device.h> +#include <linux/of_platform.h> +#include <linux/of_gpio.h> +#include <linux/delay.h> + +#define GPIO_RB91X_KEY_DRIVER_NAME "gpio-rb91x-key" + +enum gpio_rb91x_key_gpios { + GPIO_RB91X_KEY_NAND, + GPIO_RB91X_KEY_POLL, + GPIO_RB91X_KEY_PDIS, + + GPIO_RB91X_KEY_NGPIOS, +}; + +struct gpio_rb91x_key { + struct gpio_chip gc; + struct mutex mutex; + struct mutex poll_mutex; + int polling_disabled; + struct gpio_desc *gpio; +}; + +static inline struct gpio_rb91x_key *to_gpio_rb91x_key(struct gpio_chip *gc) +{ + return container_of(gc, struct gpio_rb91x_key, gc); +} + +static int gpio_rb91x_key_get(struct gpio_chip *gc, unsigned offset) +{ + struct gpio_rb91x_key *drvdata = to_gpio_rb91x_key(gc); + struct gpio_desc *gpio = drvdata->gpio; + int val, bak_val; + + switch (offset) { + case GPIO_RB91X_KEY_NAND: + mutex_lock(&drvdata->mutex); + val = gpiod_get_value_cansleep(gpio); + mutex_unlock(&drvdata->mutex); + break; + case GPIO_RB91X_KEY_PDIS: + mutex_lock(&drvdata->mutex); + val = drvdata->polling_disabled; + mutex_unlock(&drvdata->mutex); + break; + case GPIO_RB91X_KEY_POLL: + mutex_lock(&drvdata->poll_mutex); + mutex_lock(&drvdata->mutex); + bak_val = gpiod_get_raw_value_cansleep(gpio); + gpiod_direction_input(gpio); + /* + * Without this delay nothing works. Get it + * from mikrotik RouterOS linux kernel patches. + */ + udelay(200); + val = gpiod_get_raw_value_cansleep(gpio); + gpiod_direction_output_raw(gpio, bak_val); + mutex_unlock(&drvdata->mutex); + mutex_unlock(&drvdata->poll_mutex); + break; + default: + return -EINVAL; + } + + return val; +} + +static int gpio_rb91x_key_direction_input(struct gpio_chip *gc, unsigned offset) +{ + switch (offset) { + case GPIO_RB91X_KEY_POLL: + return 0; + default: + return -EINVAL; + } +} + +static void gpio_rb91x_key_set(struct gpio_chip *gc, unsigned offset, int value) +{ + struct gpio_rb91x_key *drvdata = to_gpio_rb91x_key(gc); + struct gpio_desc *gpio = drvdata->gpio; + + mutex_lock(&drvdata->mutex); + + switch (offset) { + case GPIO_RB91X_KEY_NAND: + gpiod_set_raw_value_cansleep(gpio, value); + break; + case GPIO_RB91X_KEY_PDIS: + if (value) { + if (!drvdata->polling_disabled) { + mutex_lock(&drvdata->poll_mutex); + drvdata->polling_disabled = 1; + } + } else { + if (drvdata->polling_disabled) { + mutex_unlock(&drvdata->poll_mutex); + drvdata->polling_disabled = 0; + } + } + break; + default: + break; + } + + mutex_unlock(&drvdata->mutex); +} + +static int gpio_rb91x_key_direction_output(struct gpio_chip *gc, unsigned offset, + int value) +{ + switch (offset) { + case GPIO_RB91X_KEY_NAND: + case GPIO_RB91X_KEY_PDIS: + gpio_rb91x_key_set(gc, offset, value); + return 0; + default: + return -EINVAL; + } +} + +static int gpio_rb91x_key_probe(struct platform_device *pdev) +{ + struct gpio_rb91x_key *drvdata; + struct gpio_chip *gc; + struct device *dev = &pdev->dev; + struct device_node *of_node = dev->of_node; + int r; + + drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); + if (!drvdata) + return -ENOMEM; + + mutex_init(&drvdata->mutex); + mutex_init(&drvdata->poll_mutex); + + drvdata->gpio = devm_gpiod_get(dev, NULL, GPIOD_OUT_LOW); + if (IS_ERR(drvdata->gpio)) { + if (PTR_ERR(drvdata->gpio) != -EPROBE_DEFER) { + dev_err(dev, "failed to get gpio: %ld\n", + PTR_ERR(drvdata->gpio)); + } + return PTR_ERR(drvdata->gpio); + } + + gc = &drvdata->gc; + gc->label = GPIO_RB91X_KEY_DRIVER_NAME; + gc->can_sleep = 1; + gc->base = -1; + gc->ngpio = GPIO_RB91X_KEY_NGPIOS; + gc->get = gpio_rb91x_key_get; + gc->set = gpio_rb91x_key_set; + gc->direction_output = gpio_rb91x_key_direction_output; + gc->direction_input = gpio_rb91x_key_direction_input; + gc->of_node = of_node; + + platform_set_drvdata(pdev, drvdata); + + r = gpiochip_add(&drvdata->gc); + if (r) { + dev_err(dev, "gpiochip_add() failed: %d\n", r); + return r; + } + + return 0; +} + +static int gpio_rb91x_key_remove(struct platform_device *pdev) +{ + struct gpio_rb91x_key *drvdata = platform_get_drvdata(pdev); + + gpiochip_remove(&drvdata->gc); + return 0; +} + +static const struct of_device_id gpio_rb91x_key_match[] = { + { .compatible = "mikrotik,"GPIO_RB91X_KEY_DRIVER_NAME }, + {}, +}; + +MODULE_DEVICE_TABLE(of, gpio_rb91x_key_match); + +static struct platform_driver gpio_rb91x_key_driver = { + .probe = gpio_rb91x_key_probe, + .remove = gpio_rb91x_key_remove, + .driver = { + .name = GPIO_RB91X_KEY_DRIVER_NAME, + .owner = THIS_MODULE, + .of_match_table = gpio_rb91x_key_match, + }, +}; + +module_platform_driver(gpio_rb91x_key_driver); + +MODULE_DESCRIPTION("Driver for reset key gpio line shared with NAND for MikroTik RB91x board series."); +MODULE_AUTHOR("Denis Kalashnikov <denis281089@gmail.com>"); +MODULE_LICENSE("GPL v2"); +MODULE_ALIAS("platform:" GPIO_RB91X_KEY_DRIVER_NAME); diff --git a/target/linux/ath79/files/drivers/mtd/nand/raw/rb91x_nand.c b/target/linux/ath79/files/drivers/mtd/nand/raw/rb91x_nand.c index ac9a2ac8ac..244fd27590 100644 --- a/target/linux/ath79/files/drivers/mtd/nand/raw/rb91x_nand.c +++ b/target/linux/ath79/files/drivers/mtd/nand/raw/rb91x_nand.c @@ -40,6 +40,7 @@ enum rb91x_nand_gpios { RB91X_NAND_ALE, /* Address Latch Enable */ RB91X_NAND_NRW, /* Read/Write. Active low */ RB91X_NAND_NLE, /* Latch Enable. Active low */ + RB91X_NAND_PDIS, /* Reset Key Poll Disable. Active high */ RB91X_NAND_GPIOS, }; @@ -57,6 +58,12 @@ static inline void rb91x_nand_latch_lock(struct rb91x_nand_drvdata *drvdata, gpiod_set_value_cansleep(drvdata->gpio[RB91X_NAND_NLE], lock); } +static inline void rb91x_nand_rst_key_poll_disable(struct rb91x_nand_drvdata *drvdata, + int disable) +{ + gpiod_set_value_cansleep(drvdata->gpio[RB91X_NAND_PDIS], disable); +} + static int rb91x_ooblayout_ecc(struct mtd_info *mtd, int section, struct mtd_oob_region *oobregion) { @@ -115,6 +122,7 @@ static void rb91x_nand_write(struct rb91x_nand_drvdata *drvdata, unsigned i; rb91x_nand_latch_lock(drvdata, 1); + rb91x_nand_rst_key_poll_disable(drvdata, 1); oe_reg = __raw_readl(base + AR71XX_GPIO_REG_OE); out_reg = __raw_readl(base + AR71XX_GPIO_REG_OUT); @@ -146,6 +154,7 @@ static void rb91x_nand_write(struct rb91x_nand_drvdata *drvdata, /* Flush write */ __raw_readl(base + AR71XX_GPIO_REG_OUT); + rb91x_nand_rst_key_poll_disable(drvdata, 0); rb91x_nand_latch_lock(drvdata, 0); } @@ -162,6 +171,7 @@ static void rb91x_nand_read(struct rb91x_nand_drvdata *drvdata, gpiod_set_value_cansleep(drvdata->gpio[RB91X_NAND_READ], 1); rb91x_nand_latch_lock(drvdata, 1); + rb91x_nand_rst_key_poll_disable(drvdata, 1); /* Save registers */ oe_reg = __raw_readl(base + AR71XX_GPIO_REG_OE); @@ -199,6 +209,7 @@ static void rb91x_nand_read(struct rb91x_nand_drvdata *drvdata, /* Flush write */ __raw_readl(base + AR71XX_GPIO_REG_OUT); + rb91x_nand_rst_key_poll_disable(drvdata, 0); rb91x_nand_latch_lock(drvdata, 0); /* Disable read mode */ @@ -274,8 +285,11 @@ static int rb91x_nand_probe(struct platform_device *pdev) gpios = gpiod_get_array(dev, NULL, GPIOD_OUT_LOW); if (IS_ERR(gpios)) { - dev_err(dev, "failed to get gpios: %d\n", (int)gpios); - return -EINVAL; + if (PTR_ERR(gpios) != -EPROBE_DEFER) { + dev_err(dev, "failed to get gpios: %d\n", + PTR_ERR(gpios)); + } + return PTR_ERR(gpios); } if (gpios->ndescs != RB91X_NAND_GPIOS) { |