Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Andy Shevchenko and committed by
Bartosz Golaszewski
667630ed 6b3c1791

+14 -26
+14 -26
drivers/gpio/gpiolib-sysfs.c
··· 13 13 #include "gpiolib.h" 14 14 #include "gpiolib-sysfs.h" 15 15 16 + #define GPIO_IRQF_TRIGGER_NONE 0 16 17 #define GPIO_IRQF_TRIGGER_FALLING BIT(0) 17 18 #define GPIO_IRQF_TRIGGER_RISING BIT(1) 18 19 #define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \ ··· 219 218 sysfs_put(data->value_kn); 220 219 } 221 220 222 - static const struct { 223 - const char *name; 224 - unsigned char flags; 225 - } trigger_types[] = { 226 - { "none", 0 }, 227 - { "falling", GPIO_IRQF_TRIGGER_FALLING }, 228 - { "rising", GPIO_IRQF_TRIGGER_RISING }, 229 - { "both", GPIO_IRQF_TRIGGER_BOTH }, 221 + static const char * const trigger_names[] = { 222 + [GPIO_IRQF_TRIGGER_NONE] = "none", 223 + [GPIO_IRQF_TRIGGER_FALLING] = "falling", 224 + [GPIO_IRQF_TRIGGER_RISING] = "rising", 225 + [GPIO_IRQF_TRIGGER_BOTH] = "both", 230 226 }; 231 227 232 228 static ssize_t edge_show(struct device *dev, 233 229 struct device_attribute *attr, char *buf) 234 230 { 235 231 struct gpiod_data *data = dev_get_drvdata(dev); 236 - int i; 232 + int flags; 237 233 238 234 mutex_lock(&data->mutex); 239 235 240 - for (i = 0; i < ARRAY_SIZE(trigger_types); i++) { 241 - if (data->irq_flags == trigger_types[i].flags) 242 - break; 243 - } 236 + flags = data->irq_flags; 244 237 245 238 mutex_unlock(&data->mutex); 246 239 247 - if (i >= ARRAY_SIZE(trigger_types)) 240 + if (flags >= ARRAY_SIZE(trigger_names)) 248 241 return 0; 249 242 250 - return sysfs_emit(buf, "%s\n", trigger_types[i].name); 243 + return sysfs_emit(buf, "%s\n", trigger_names[flags]); 251 244 } 252 245 253 246 static ssize_t edge_store(struct device *dev, 254 247 struct device_attribute *attr, const char *buf, size_t size) 255 248 { 256 249 struct gpiod_data *data = dev_get_drvdata(dev); 257 - unsigned char flags; 258 250 ssize_t status = size; 259 - int i; 251 + int flags; 260 252 261 - for (i = 0; i < ARRAY_SIZE(trigger_types); i++) { 262 - if (sysfs_streq(trigger_types[i].name, buf)) 263 - break; 264 - } 265 - 266 - if (i == ARRAY_SIZE(trigger_types)) 267 - return -EINVAL; 268 - 269 - flags = trigger_types[i].flags; 253 + flags = sysfs_match_string(trigger_names, buf); 254 + if (flags < 0) 255 + return flags; 270 256 271 257 mutex_lock(&data->mutex); 272 258