Skip to content

Commit 5a2a46a

Browse files
andy-shevbrgl
authored andcommitted
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>
1 parent 9fe5fcd commit 5a2a46a

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

drivers/gpio/gpio-wcove.c

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
enum ctrl_register {
7474
CTRL_IN,
7575
CTRL_OUT,
76+
IRQ_STATUS,
77+
IRQ_MASK,
7678
};
7779

7880
/*
@@ -112,18 +114,25 @@ static inline int to_reg(int gpio, enum ctrl_register reg_type)
112114
return reg;
113115
}
114116

115-
static void wcove_update_irq_mask(struct wcove_gpio *wg, int gpio)
117+
static inline int to_ireg(int gpio, enum ctrl_register type, unsigned int *mask)
116118
{
117-
unsigned int reg, mask;
119+
unsigned int reg = type == IRQ_STATUS ? IRQ_STATUS_BASE : IRQ_MASK_BASE;
118120

119121
if (gpio < GROUP0_NR_IRQS) {
120-
reg = IRQ_MASK_BASE;
121-
mask = BIT(gpio % GROUP0_NR_IRQS);
122+
reg += 0;
123+
*mask = BIT(gpio);
122124
} else {
123-
reg = IRQ_MASK_BASE + 1;
124-
mask = BIT((gpio - GROUP0_NR_IRQS) % GROUP1_NR_IRQS);
125+
reg += 1;
126+
*mask = BIT(gpio - GROUP0_NR_IRQS);
125127
}
126128

129+
return reg;
130+
}
131+
132+
static void wcove_update_irq_mask(struct wcove_gpio *wg, int gpio)
133+
{
134+
unsigned int mask, reg = to_ireg(gpio, IRQ_MASK, &mask);
135+
127136
if (wg->set_irq_mask)
128137
regmap_set_bits(wg->regmap, reg, mask);
129138
else
@@ -324,7 +333,7 @@ static struct irq_chip wcove_irqchip = {
324333
static irqreturn_t wcove_gpio_irq_handler(int irq, void *data)
325334
{
326335
struct wcove_gpio *wg = (struct wcove_gpio *)data;
327-
unsigned int virq, gpio, mask, offset;
336+
unsigned int virq, gpio;
328337
unsigned long pending;
329338
u8 p[2];
330339

@@ -341,12 +350,11 @@ static irqreturn_t wcove_gpio_irq_handler(int irq, void *data)
341350
while (pending) {
342351
/* One iteration is for all pending bits */
343352
for_each_set_bit(gpio, &pending, WCOVE_GPIO_NUM) {
344-
offset = (gpio > GROUP0_NR_IRQS) ? 1 : 0;
345-
mask = (offset == 1) ? BIT(gpio - GROUP0_NR_IRQS) :
346-
BIT(gpio);
353+
unsigned int mask, reg = to_ireg(gpio, IRQ_STATUS, &mask);
354+
347355
virq = irq_find_mapping(wg->chip.irq.domain, gpio);
348356
handle_nested_irq(virq);
349-
regmap_set_bits(wg->regmap, IRQ_STATUS_BASE + offset, mask);
357+
regmap_set_bits(wg->regmap, reg, mask);
350358
}
351359

352360
/* Next iteration */
@@ -366,30 +374,26 @@ static void wcove_gpio_dbg_show(struct seq_file *s,
366374
{
367375
unsigned int ctlo, ctli, irq_mask, irq_status;
368376
struct wcove_gpio *wg = gpiochip_get_data(chip);
369-
int gpio, offset, group, ret = 0;
377+
int gpio, mask, ret = 0;
370378

371379
for (gpio = 0; gpio < WCOVE_GPIO_NUM; gpio++) {
372-
group = gpio < GROUP0_NR_IRQS ? 0 : 1;
373380
ret += regmap_read(wg->regmap, to_reg(gpio, CTRL_OUT), &ctlo);
374381
ret += regmap_read(wg->regmap, to_reg(gpio, CTRL_IN), &ctli);
375-
ret += regmap_read(wg->regmap, IRQ_MASK_BASE + group,
376-
&irq_mask);
377-
ret += regmap_read(wg->regmap, IRQ_STATUS_BASE + group,
378-
&irq_status);
382+
ret += regmap_read(wg->regmap, to_ireg(gpio, IRQ_MASK, &mask), &irq_mask);
383+
ret += regmap_read(wg->regmap, to_ireg(gpio, IRQ_STATUS, &mask), &irq_status);
379384
if (ret) {
380385
pr_err("Failed to read registers: ctrl out/in or irq status/mask\n");
381386
break;
382387
}
383388

384-
offset = gpio % 8;
385389
seq_printf(s, " gpio-%-2d %s %s %s %s ctlo=%2x,%s %s\n",
386390
gpio, ctlo & CTLO_DIR_OUT ? "out" : "in ",
387391
ctli & 0x1 ? "hi" : "lo",
388392
ctli & CTLI_INTCNT_NE ? "fall" : " ",
389393
ctli & CTLI_INTCNT_PE ? "rise" : " ",
390394
ctlo,
391-
irq_mask & BIT(offset) ? "mask " : "unmask",
392-
irq_status & BIT(offset) ? "pending" : " ");
395+
irq_mask & mask ? "mask " : "unmask",
396+
irq_status & mask ? "pending" : " ");
393397
}
394398
}
395399

0 commit comments

Comments
 (0)