Re: [PATCH 1/3] ath79: add gpio-latch driver for MikroTik RouterBOARDs

2021-06-15 Thread Sergey Ryazanov
On Thu, May 27, 2021 at 11:18 AM Denis Kalashnikov
 wrote:
> From: Denis Kalashnikov 
>
> This is a slighty modified version of ar71xx gpio-latch driver
> written by Gabor Juhos .
>
> Changes:
> * DTS support,
> * New gpio API (gpiod_*).
>
> Signed-off-by: Denis Kalashnikov 

Reviewed-by: Sergey Ryazanov 

___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


[PATCH 1/3] ath79: add gpio-latch driver for MikroTik RouterBOARDs

2021-05-27 Thread Denis Kalashnikov
From: Denis Kalashnikov 

This is a slighty modified version of ar71xx gpio-latch driver
written by Gabor Juhos .

Changes:
* DTS support,
* New gpio API (gpiod_*).

Signed-off-by: Denis Kalashnikov 
---

Changelog:

RFC v1 --> RFC v2
- Don't use MFD driver API anymore,
- Use the new gpio API (gpiod_*),
- Add support of kernel 5.10.

RFC v2 --> v1
- Change DTS node params -- remove redundant latch-enable-gpios param,
- Delete unneeded direction input callback (latch is output-only),
- Add support of pin holes (unconnected lines of the latch).

---
 .../ath79/files/drivers/gpio/gpio-latch.c | 203 ++
 .../patches-5.10/939-mikrotik-rb91x.patch |  26 +++
 .../patches-5.4/939-mikrotik-rb91x.patch  |  23 ++
 3 files changed, 252 insertions(+)
 create mode 100644 target/linux/ath79/files/drivers/gpio/gpio-latch.c
 create mode 100644 target/linux/ath79/patches-5.10/939-mikrotik-rb91x.patch
 create mode 100644 target/linux/ath79/patches-5.4/939-mikrotik-rb91x.patch

diff --git a/target/linux/ath79/files/drivers/gpio/gpio-latch.c 
b/target/linux/ath79/files/drivers/gpio/gpio-latch.c
new file mode 100644
index 00..f3545a663e
--- /dev/null
+++ b/target/linux/ath79/files/drivers/gpio/gpio-latch.c
@@ -0,0 +1,203 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ *  GPIO latch driver
+ *
+ *  Copyright (C) 2014 Gabor Juhos 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define GPIO_LATCH_DRIVER_NAME  "gpio-latch"
+#define GPIO_LATCH_LINES 9
+
+struct gpio_latch_chip {
+   struct gpio_chip gc;
+   struct mutex mutex;
+   struct mutex latch_mutex;
+   bool latch_enabled;
+   int le_gpio;
+   bool le_active_low;
+   struct gpio_desc *gpios[GPIO_LATCH_LINES];
+};
+
+static inline struct gpio_latch_chip *to_gpio_latch_chip(struct gpio_chip *gc)
+{
+   return container_of(gc, struct gpio_latch_chip, gc);
+}
+
+static void gpio_latch_lock(struct gpio_latch_chip *glc, bool enable)
+{
+   mutex_lock(>mutex);
+
+   if (enable)
+   glc->latch_enabled = true;
+
+   if (glc->latch_enabled)
+   mutex_lock(>latch_mutex);
+}
+
+static void gpio_latch_unlock(struct gpio_latch_chip *glc, bool disable)
+{
+   if (glc->latch_enabled)
+   mutex_unlock(>latch_mutex);
+
+   if (disable)
+   glc->latch_enabled = true;
+
+   mutex_unlock(>mutex);
+}
+
+static int
+gpio_latch_get(struct gpio_chip *gc, unsigned offset)
+{
+   struct gpio_latch_chip *glc = to_gpio_latch_chip(gc);
+   int ret;
+
+   gpio_latch_lock(glc, false);
+   ret = gpiod_get_value(glc->gpios[offset]);
+   gpio_latch_unlock(glc, false);
+
+   return ret;
+}
+
+static void
+gpio_latch_set(struct gpio_chip *gc, unsigned offset, int value)
+{
+   struct gpio_latch_chip *glc = to_gpio_latch_chip(gc);
+   bool enable_latch = false;
+   bool disable_latch = false;
+
+   if (offset == glc->le_gpio) {
+   enable_latch = value ^ glc->le_active_low;
+   disable_latch = !enable_latch;
+   }
+
+   gpio_latch_lock(glc, enable_latch);
+   gpiod_set_raw_value(glc->gpios[offset], value);
+   gpio_latch_unlock(glc, disable_latch);
+}
+
+static int
+gpio_latch_direction_output(struct gpio_chip *gc, unsigned offset, int value)
+{
+   struct gpio_latch_chip *glc = to_gpio_latch_chip(gc);
+   bool enable_latch = false;
+   bool disable_latch = false;
+   int ret;
+
+   if (offset == glc->le_gpio) {
+   enable_latch = value ^ glc->le_active_low;
+   disable_latch = !enable_latch;
+   }
+
+   gpio_latch_lock(glc, enable_latch);
+   ret = gpiod_direction_output_raw(glc->gpios[offset], value);
+   gpio_latch_unlock(glc, disable_latch);
+
+   return ret;
+}
+
+static int gpio_latch_probe(struct platform_device *pdev)
+{
+   struct gpio_latch_chip *glc;
+   struct gpio_chip *gc;
+   struct device *dev = >dev;
+   struct device_node *of_node = dev->of_node;
+   int i, n;
+
+   glc = devm_kzalloc(dev, sizeof(*glc), GFP_KERNEL);
+   if (!glc)
+   return -ENOMEM;
+
+   mutex_init(>mutex);
+   mutex_init(>latch_mutex);
+
+   n = gpiod_count(dev, NULL);
+   if (n <= 0) {
+   dev_err(dev, "failed to get gpios: %d\n", n);
+   return n;
+   } else if (n != GPIO_LATCH_LINES) {
+   dev_err(dev, "expected %d gpios\n", GPIO_LATCH_LINES);
+   return -EINVAL;
+   }
+
+   for (i = 0; i < n; i++) {
+   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]));
+   return PTR_ERR(glc->gpios[i]);
+   }
+