[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 3/3] misc/pca9552: Only update output GPIOs if state changed
From: |
Glenn Miles |
Subject: |
[PATCH v2 3/3] misc/pca9552: Only update output GPIOs if state changed |
Date: |
Fri, 20 Oct 2023 13:23:21 -0500 |
The pca9552 code was updating output GPIO states whenever
the pin state was updated even if the state did not change.
This commit adds a check so that we only update the GPIO
output when the pin state actually changes.
Signed-off-by: Glenn Miles <milesg@linux.vnet.ibm.com>
---
New commit in v2
hw/misc/pca9552.c | 25 ++++++++++++++++++-------
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/hw/misc/pca9552.c b/hw/misc/pca9552.c
index ed814d1f98..4ed6903404 100644
--- a/hw/misc/pca9552.c
+++ b/hw/misc/pca9552.c
@@ -112,14 +112,15 @@ static void pca955x_update_pin_input(PCA955xState *s)
for (i = 0; i < k->pin_count; i++) {
uint8_t input_reg = PCA9552_INPUT0 + (i / 8);
- uint8_t input_shift = (i % 8);
+ uint8_t bit_mask = 1 << (i % 8);
uint8_t config = pca955x_pin_get_config(s, i);
+ uint8_t old_value = s->regs[input_reg] & bit_mask;
+ uint8_t new_value;
switch (config) {
case PCA9552_LED_ON:
/* Pin is set to 0V to turn on LED */
- qemu_set_irq(s->gpio_out[i], 0);
- s->regs[input_reg] &= ~(1 << input_shift);
+ s->regs[input_reg] &= ~bit_mask;
break;
case PCA9552_LED_OFF:
/*
@@ -128,11 +129,9 @@ static void pca955x_update_pin_input(PCA955xState *s)
* external device drives it low.
*/
if (s->ext_state[i] == PCA9552_PIN_LOW) {
- qemu_set_irq(s->gpio_out[i], 0);
- s->regs[input_reg] &= ~(1 << input_shift);
+ s->regs[input_reg] &= ~bit_mask;
} else {
- qemu_set_irq(s->gpio_out[i], 1);
- s->regs[input_reg] |= 1 << input_shift;
+ s->regs[input_reg] |= bit_mask;
}
break;
case PCA9552_LED_PWM0:
@@ -141,6 +140,18 @@ static void pca955x_update_pin_input(PCA955xState *s)
default:
break;
}
+
+ /* update irq state only if pin state changed */
+ new_value = s->regs[input_reg] & bit_mask;
+ if (new_value != old_value) {
+ if (new_value) {
+ /* changed from 0 to 1 */
+ qemu_set_irq(s->gpio_out[i], 1);
+ } else {
+ /* changed from 1 to 0 */
+ qemu_set_irq(s->gpio_out[i], 0);
+ }
+ }
}
}
--
2.31.1