Skip to content

Commit 667630e

Browse files
andy-shevbrgl
authored andcommitted
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: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
1 parent 6b3c179 commit 667630e

1 file changed

Lines changed: 14 additions & 26 deletions

File tree

drivers/gpio/gpiolib-sysfs.c

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "gpiolib.h"
1414
#include "gpiolib-sysfs.h"
1515

16+
#define GPIO_IRQF_TRIGGER_NONE 0
1617
#define GPIO_IRQF_TRIGGER_FALLING BIT(0)
1718
#define GPIO_IRQF_TRIGGER_RISING BIT(1)
1819
#define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \
@@ -218,54 +219,41 @@ static void gpio_sysfs_free_irq(struct device *dev)
218219
sysfs_put(data->value_kn);
219220
}
220221

221-
static const struct {
222-
const char *name;
223-
unsigned char flags;
224-
} trigger_types[] = {
225-
{ "none", 0 },
226-
{ "falling", GPIO_IRQF_TRIGGER_FALLING },
227-
{ "rising", GPIO_IRQF_TRIGGER_RISING },
228-
{ "both", GPIO_IRQF_TRIGGER_BOTH },
222+
static const char * const trigger_names[] = {
223+
[GPIO_IRQF_TRIGGER_NONE] = "none",
224+
[GPIO_IRQF_TRIGGER_FALLING] = "falling",
225+
[GPIO_IRQF_TRIGGER_RISING] = "rising",
226+
[GPIO_IRQF_TRIGGER_BOTH] = "both",
229227
};
230228

231229
static ssize_t edge_show(struct device *dev,
232230
struct device_attribute *attr, char *buf)
233231
{
234232
struct gpiod_data *data = dev_get_drvdata(dev);
235-
int i;
233+
int flags;
236234

237235
mutex_lock(&data->mutex);
238236

239-
for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
240-
if (data->irq_flags == trigger_types[i].flags)
241-
break;
242-
}
237+
flags = data->irq_flags;
243238

244239
mutex_unlock(&data->mutex);
245240

246-
if (i >= ARRAY_SIZE(trigger_types))
241+
if (flags >= ARRAY_SIZE(trigger_names))
247242
return 0;
248243

249-
return sysfs_emit(buf, "%s\n", trigger_types[i].name);
244+
return sysfs_emit(buf, "%s\n", trigger_names[flags]);
250245
}
251246

252247
static ssize_t edge_store(struct device *dev,
253248
struct device_attribute *attr, const char *buf, size_t size)
254249
{
255250
struct gpiod_data *data = dev_get_drvdata(dev);
256-
unsigned char flags;
257251
ssize_t status = size;
258-
int i;
259-
260-
for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
261-
if (sysfs_streq(trigger_types[i].name, buf))
262-
break;
263-
}
264-
265-
if (i == ARRAY_SIZE(trigger_types))
266-
return -EINVAL;
252+
int flags;
267253

268-
flags = trigger_types[i].flags;
254+
flags = sysfs_match_string(trigger_names, buf);
255+
if (flags < 0)
256+
return flags;
269257

270258
mutex_lock(&data->mutex);
271259

0 commit comments

Comments
 (0)