Commit 667630ed authored by Andy Shevchenko's avatar Andy Shevchenko Committed by Bartosz Golaszewski

gpiolib: sysfs: Simplify edge handling in the code

Instead of keeping specific data structure for IRQ trigger types, switch
to array of trigger names and use index as a type.

The code is in maintenance mode and that array is not going to grow.
Signed-off-by: default avatarAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: default avatarBartosz Golaszewski <brgl@bgdev.pl>
parent 6b3c1791
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "gpiolib.h" #include "gpiolib.h"
#include "gpiolib-sysfs.h" #include "gpiolib-sysfs.h"
#define GPIO_IRQF_TRIGGER_NONE 0
#define GPIO_IRQF_TRIGGER_FALLING BIT(0) #define GPIO_IRQF_TRIGGER_FALLING BIT(0)
#define GPIO_IRQF_TRIGGER_RISING BIT(1) #define GPIO_IRQF_TRIGGER_RISING BIT(1)
#define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \ #define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \
...@@ -218,54 +219,41 @@ static void gpio_sysfs_free_irq(struct device *dev) ...@@ -218,54 +219,41 @@ static void gpio_sysfs_free_irq(struct device *dev)
sysfs_put(data->value_kn); sysfs_put(data->value_kn);
} }
static const struct { static const char * const trigger_names[] = {
const char *name; [GPIO_IRQF_TRIGGER_NONE] = "none",
unsigned char flags; [GPIO_IRQF_TRIGGER_FALLING] = "falling",
} trigger_types[] = { [GPIO_IRQF_TRIGGER_RISING] = "rising",
{ "none", 0 }, [GPIO_IRQF_TRIGGER_BOTH] = "both",
{ "falling", GPIO_IRQF_TRIGGER_FALLING },
{ "rising", GPIO_IRQF_TRIGGER_RISING },
{ "both", GPIO_IRQF_TRIGGER_BOTH },
}; };
static ssize_t edge_show(struct device *dev, static ssize_t edge_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct gpiod_data *data = dev_get_drvdata(dev); struct gpiod_data *data = dev_get_drvdata(dev);
int i; int flags;
mutex_lock(&data->mutex); mutex_lock(&data->mutex);
for (i = 0; i < ARRAY_SIZE(trigger_types); i++) { flags = data->irq_flags;
if (data->irq_flags == trigger_types[i].flags)
break;
}
mutex_unlock(&data->mutex); mutex_unlock(&data->mutex);
if (i >= ARRAY_SIZE(trigger_types)) if (flags >= ARRAY_SIZE(trigger_names))
return 0; return 0;
return sysfs_emit(buf, "%s\n", trigger_types[i].name); return sysfs_emit(buf, "%s\n", trigger_names[flags]);
} }
static ssize_t edge_store(struct device *dev, static ssize_t edge_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size) struct device_attribute *attr, const char *buf, size_t size)
{ {
struct gpiod_data *data = dev_get_drvdata(dev); struct gpiod_data *data = dev_get_drvdata(dev);
unsigned char flags;
ssize_t status = size; ssize_t status = size;
int i; int flags;
for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
if (sysfs_streq(trigger_types[i].name, buf))
break;
}
if (i == ARRAY_SIZE(trigger_types))
return -EINVAL;
flags = trigger_types[i].flags; flags = sysfs_match_string(trigger_names, buf);
if (flags < 0)
return flags;
mutex_lock(&data->mutex); mutex_lock(&data->mutex);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment