2021-05-27 08:16:44 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
/*
|
|
|
|
* GPIO latch driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Gabor Juhos <juhosg@openwrt.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
#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(&glc->mutex);
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
glc->latch_enabled = true;
|
|
|
|
|
|
|
|
if (glc->latch_enabled)
|
|
|
|
mutex_lock(&glc->latch_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gpio_latch_unlock(struct gpio_latch_chip *glc, bool disable)
|
|
|
|
{
|
|
|
|
if (glc->latch_enabled)
|
|
|
|
mutex_unlock(&glc->latch_mutex);
|
|
|
|
|
|
|
|
if (disable)
|
|
|
|
glc->latch_enabled = true;
|
|
|
|
|
|
|
|
mutex_unlock(&glc->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);
|
2022-01-19 10:25:05 +00:00
|
|
|
ret = gpiod_get_raw_value_cansleep(glc->gpios[offset]);
|
2021-05-27 08:16:44 +00:00
|
|
|
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);
|
2022-01-19 10:25:05 +00:00
|
|
|
gpiod_set_raw_value_cansleep(glc->gpios[offset], value);
|
2021-05-27 08:16:44 +00:00
|
|
|
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 = &pdev->dev;
|
2024-04-29 20:26:17 +00:00
|
|
|
struct fwnode_handle *fwnode = dev->fwnode;
|
2021-05-27 08:16:44 +00:00
|
|
|
int i, n;
|
|
|
|
|
|
|
|
glc = devm_kzalloc(dev, sizeof(*glc), GFP_KERNEL);
|
|
|
|
if (!glc)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mutex_init(&glc->mutex);
|
|
|
|
mutex_init(&glc->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])) {
|
2022-01-19 10:25:05 +00:00
|
|
|
if (PTR_ERR(glc->gpios[i]) != -EPROBE_DEFER) {
|
ath79: gpio-latch: fix compilation warning for wrong dev_err %
Fix compilation warning for using %d instead of %ld for gpio-latch in
dev_err.
Fix compilation warning:
In file included from ./include/linux/device.h:15,
from ./include/linux/gpio/driver.h:5,
from drivers/gpio/gpio-latch.c:13:
drivers/gpio/gpio-latch.c: In function 'gpio_latch_probe':
drivers/gpio/gpio-latch.c:137:46: error: format '%d' expects argument of type 'int', but argument 4 has type 'long int' [-Werror=format=]
137 | dev_err(dev, "failed to get gpio %d: %d\n", i,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./include/linux/dev_printk.h:110:30: note: in definition of macro 'dev_printk_index_wrap'
110 | _p_func(dev, fmt, ##__VA_ARGS__); \
| ^~~
./include/linux/dev_printk.h:144:56: note: in expansion of macro 'dev_fmt'
144 | dev_printk_index_wrap(_dev_err, KERN_ERR, dev, dev_fmt(fmt), ##__VA_ARGS__)
| ^~~~~~~
drivers/gpio/gpio-latch.c:137:33: note: in expansion of macro 'dev_err'
137 | dev_err(dev, "failed to get gpio %d: %d\n", i,
| ^~~~~~~
drivers/gpio/gpio-latch.c:137:71: note: format string is defined here
137 | dev_err(dev, "failed to get gpio %d: %d\n", i,
| ~^
| |
| int
| %ld
cc1: all warnings being treated as errors
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
2023-05-09 11:53:25 +00:00
|
|
|
dev_err(dev, "failed to get gpio %d: %ld\n", i,
|
2022-01-19 10:25:05 +00:00
|
|
|
PTR_ERR(glc->gpios[i]));
|
|
|
|
}
|
2021-05-27 08:16:44 +00:00
|
|
|
return PTR_ERR(glc->gpios[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
glc->le_gpio = 8;
|
|
|
|
glc->le_active_low = gpiod_is_active_low(glc->gpios[glc->le_gpio]);
|
|
|
|
|
|
|
|
if (!glc->gpios[glc->le_gpio]) {
|
|
|
|
dev_err(dev, "missing required latch-enable gpio %d\n",
|
|
|
|
glc->le_gpio);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gc = &glc->gc;
|
|
|
|
gc->label = GPIO_LATCH_DRIVER_NAME;
|
|
|
|
gc->can_sleep = true;
|
|
|
|
gc->base = -1;
|
|
|
|
gc->ngpio = GPIO_LATCH_LINES;
|
|
|
|
gc->get = gpio_latch_get;
|
|
|
|
gc->set = gpio_latch_set;
|
|
|
|
gc->direction_output = gpio_latch_direction_output;
|
2024-04-29 20:26:17 +00:00
|
|
|
gc->fwnode = fwnode;
|
2021-05-27 08:16:44 +00:00
|
|
|
|
|
|
|
platform_set_drvdata(pdev, glc);
|
|
|
|
|
|
|
|
i = gpiochip_add(&glc->gc);
|
|
|
|
if (i) {
|
|
|
|
dev_err(dev, "gpiochip_add() failed: %d\n", i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gpio_latch_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct gpio_latch_chip *glc = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
gpiochip_remove(&glc->gc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id gpio_latch_match[] = {
|
|
|
|
{ .compatible = GPIO_LATCH_DRIVER_NAME },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(of, gpio_latch_match);
|
|
|
|
|
|
|
|
static struct platform_driver gpio_latch_driver = {
|
|
|
|
.probe = gpio_latch_probe,
|
|
|
|
.remove = gpio_latch_remove,
|
|
|
|
.driver = {
|
|
|
|
.name = GPIO_LATCH_DRIVER_NAME,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.of_match_table = gpio_latch_match,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(gpio_latch_driver);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("GPIO latch driver");
|
|
|
|
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
|
|
|
|
MODULE_AUTHOR("Denis Kalashnikov <denis281089@gmail.com>");
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
MODULE_ALIAS("platform:" GPIO_LATCH_DRIVER_NAME);
|