From 0b64f07fd56503a2c60c61054930df3b9907295b Mon Sep 17 00:00:00 2001 From: Sultan Qasim Khan Date: Sun, 18 Feb 2024 10:25:42 -0500 Subject: [PATCH] r82xx: avoid redundant register writes for speed --- src/tuner_r82xx.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/tuner_r82xx.c b/src/tuner_r82xx.c index a6d28d2..79612d2 100644 --- a/src/tuner_r82xx.c +++ b/src/tuner_r82xx.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "rtlsdr_i2c.h" #include "tuner_r82xx.h" @@ -243,6 +244,7 @@ static void shadow_store(struct r82xx_priv *priv, uint8_t reg, const uint8_t *va if (r < 0) { len += r; + val -= r; r = 0; } if (len <= 0) @@ -253,11 +255,29 @@ static void shadow_store(struct r82xx_priv *priv, uint8_t reg, const uint8_t *va memcpy(&priv->regs[r], val, len); } +static bool shadow_equal(struct r82xx_priv *priv, uint8_t reg, const uint8_t *val, + int len) +{ + int r = reg - REG_SHADOW_START; + + if (r < 0 || len < 0 || len > NUM_REGS - r) + return false; + + if (memcmp(&priv->regs[r], val, len) == 0) + return true; + + return false; +} + static int r82xx_write(struct r82xx_priv *priv, uint8_t reg, const uint8_t *val, unsigned int len) { int rc, size, pos = 0; + /* Avoid setting registers unnecessarily since it's slow */ + if (shadow_equal(priv, reg, val, len)) + return 0; + /* Store the shadow registers */ shadow_store(priv, reg, val, len); @@ -1320,6 +1340,7 @@ int r82xx_init(struct r82xx_priv *priv) priv->xtal_cap_sel = XTAL_HIGH_CAP_0P; /* Initialize registers */ + memset(priv->regs, 0, NUM_REGS); rc = r82xx_write(priv, 0x05, r82xx_init_array, sizeof(r82xx_init_array));