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

gpio: wcove: Split out to_ireg() helper and deduplicate the code

There are a few places in the code where IRQ status and mask register
values are being updated. Use a new exctracted helper to deduplicate
the code.

While at it, get rid of unnecessary divisions.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

authored by

Andy Shevchenko and committed by
Bartosz Golaszewski
5a2a46ae 9fe5fcd6

+24 -20
+24 -20
drivers/gpio/gpio-wcove.c
··· 73 73 enum ctrl_register { 74 74 CTRL_IN, 75 75 CTRL_OUT, 76 + IRQ_STATUS, 77 + IRQ_MASK, 76 78 }; 77 79 78 80 /* ··· 114 112 return reg; 115 113 } 116 114 117 - static void wcove_update_irq_mask(struct wcove_gpio *wg, int gpio) 115 + static inline int to_ireg(int gpio, enum ctrl_register type, unsigned int *mask) 118 116 { 119 - unsigned int reg, mask; 117 + unsigned int reg = type == IRQ_STATUS ? IRQ_STATUS_BASE : IRQ_MASK_BASE; 120 118 121 119 if (gpio < GROUP0_NR_IRQS) { 122 - reg = IRQ_MASK_BASE; 123 - mask = BIT(gpio % GROUP0_NR_IRQS); 120 + reg += 0; 121 + *mask = BIT(gpio); 124 122 } else { 125 - reg = IRQ_MASK_BASE + 1; 126 - mask = BIT((gpio - GROUP0_NR_IRQS) % GROUP1_NR_IRQS); 123 + reg += 1; 124 + *mask = BIT(gpio - GROUP0_NR_IRQS); 127 125 } 126 + 127 + return reg; 128 + } 129 + 130 + static void wcove_update_irq_mask(struct wcove_gpio *wg, int gpio) 131 + { 132 + unsigned int mask, reg = to_ireg(gpio, IRQ_MASK, &mask); 128 133 129 134 if (wg->set_irq_mask) 130 135 regmap_set_bits(wg->regmap, reg, mask); ··· 333 324 static irqreturn_t wcove_gpio_irq_handler(int irq, void *data) 334 325 { 335 326 struct wcove_gpio *wg = (struct wcove_gpio *)data; 336 - unsigned int virq, gpio, mask, offset; 327 + unsigned int virq, gpio; 337 328 unsigned long pending; 338 329 u8 p[2]; 339 330 ··· 350 341 while (pending) { 351 342 /* One iteration is for all pending bits */ 352 343 for_each_set_bit(gpio, &pending, WCOVE_GPIO_NUM) { 353 - offset = (gpio > GROUP0_NR_IRQS) ? 1 : 0; 354 - mask = (offset == 1) ? BIT(gpio - GROUP0_NR_IRQS) : 355 - BIT(gpio); 344 + unsigned int mask, reg = to_ireg(gpio, IRQ_STATUS, &mask); 345 + 356 346 virq = irq_find_mapping(wg->chip.irq.domain, gpio); 357 347 handle_nested_irq(virq); 358 - regmap_set_bits(wg->regmap, IRQ_STATUS_BASE + offset, mask); 348 + regmap_set_bits(wg->regmap, reg, mask); 359 349 } 360 350 361 351 /* Next iteration */ ··· 374 366 { 375 367 unsigned int ctlo, ctli, irq_mask, irq_status; 376 368 struct wcove_gpio *wg = gpiochip_get_data(chip); 377 - int gpio, offset, group, ret = 0; 369 + int gpio, mask, ret = 0; 378 370 379 371 for (gpio = 0; gpio < WCOVE_GPIO_NUM; gpio++) { 380 - group = gpio < GROUP0_NR_IRQS ? 0 : 1; 381 372 ret += regmap_read(wg->regmap, to_reg(gpio, CTRL_OUT), &ctlo); 382 373 ret += regmap_read(wg->regmap, to_reg(gpio, CTRL_IN), &ctli); 383 - ret += regmap_read(wg->regmap, IRQ_MASK_BASE + group, 384 - &irq_mask); 385 - ret += regmap_read(wg->regmap, IRQ_STATUS_BASE + group, 386 - &irq_status); 374 + ret += regmap_read(wg->regmap, to_ireg(gpio, IRQ_MASK, &mask), &irq_mask); 375 + ret += regmap_read(wg->regmap, to_ireg(gpio, IRQ_STATUS, &mask), &irq_status); 387 376 if (ret) { 388 377 pr_err("Failed to read registers: ctrl out/in or irq status/mask\n"); 389 378 break; 390 379 } 391 380 392 - offset = gpio % 8; 393 381 seq_printf(s, " gpio-%-2d %s %s %s %s ctlo=%2x,%s %s\n", 394 382 gpio, ctlo & CTLO_DIR_OUT ? "out" : "in ", 395 383 ctli & 0x1 ? "hi" : "lo", 396 384 ctli & CTLI_INTCNT_NE ? "fall" : " ", 397 385 ctli & CTLI_INTCNT_PE ? "rise" : " ", 398 386 ctlo, 399 - irq_mask & BIT(offset) ? "mask " : "unmask", 400 - irq_status & BIT(offset) ? "pending" : " "); 387 + irq_mask & mask ? "mask " : "unmask", 388 + irq_status & mask ? "pending" : " "); 401 389 } 402 390 } 403 391