[PATCH rtw-next v7 3/3] wifi: rtw88: rtw8822c: Replace custom DAC IQ sorting with kernel sort

From: Arsenii Pashchenko

Date: Fri Aug 28 2026 - 01:42:09 EST


Replace the custom, nested bubble-sort logic in the calibration loop
with direct calls to the kernel's optimized heapsort implementation
inside rtw8822c_dac_cal_iq_search().

Implement a safe, overflow-proof s32 comparator using the subtraction
of relationals pattern. Since the input values are already properly
sign-extended into signed arrays, the custom circular index sorting
rules and helper functions completely disappear, reducing lines of code.

Signed-off-by: Arsenii Pashchenko <ulijg308@xxxxxxxxx>
---
drivers/net/wireless/realtek/rtw88/rtw8822c.c | 22 ++++++-------------
1 file changed, 7 insertions(+), 15 deletions(-)

diff --git a/drivers/net/wireless/realtek/rtw88/rtw8822c.c b/drivers/net/wireless/realtek/rtw88/rtw8822c.c
index b010e17c9..bec1bb376 100644
--- a/drivers/net/wireless/realtek/rtw88/rtw8822c.c
+++ b/drivers/net/wireless/realtek/rtw88/rtw8822c.c
@@ -3,6 +3,7 @@
*/

#include <linux/module.h>
+#include <linux/sort.h>
#include "main.h"
#include "coex.h"
#include "fw.h"
@@ -153,24 +154,14 @@ static void rtw8822c_dac_restore_reg(struct rtw_dev *rtwdev,
}
}

-static void __rtw8822c_dac_iq_sort(struct rtw_dev *rtwdev, s32 *v1, s32 *v2)
+static int rtw8822c_dac_iq_cmp_s32(const void *a, const void *b)
{
- if (*v1 > *v2)
- swap(*v1, *v2);
+ s32 val_a = *(const s32 *)a;
+ s32 val_b = *(const s32 *)b;

+ return (val_a > val_b) - (val_a < val_b);
}

-static void rtw8822c_dac_iq_sort(struct rtw_dev *rtwdev, s32 *iv, s32 *qv)
-{
- u32 i, j;
-
- for (i = 0; i < DACK_SN_8822C - 1; i++) {
- for (j = 0; j < (DACK_SN_8822C - 1 - i) ; j++) {
- __rtw8822c_dac_iq_sort(rtwdev, &iv[j], &iv[j + 1]);
- __rtw8822c_dac_iq_sort(rtwdev, &qv[j], &qv[j + 1]);
- }
- }
-}

static u32 rtw8822c_dac_iq_offset(struct rtw_dev *rtwdev, s32 *vec)
{
@@ -291,7 +282,8 @@ static void rtw8822c_dac_cal_iq_search(struct rtw_dev *rtwdev,
"[DACK] q: min=%d, max=%d, delta=%d",
q_min, q_max, q_delta);

- rtw8822c_dac_iq_sort(rtwdev, iv, qv);
+ sort(iv, DACK_SN_8822C, sizeof(s32), rtw8822c_dac_iq_cmp_s32, NULL);
+ sort(qv, DACK_SN_8822C, sizeof(s32), rtw8822c_dac_iq_cmp_s32, NULL);

if (i_delta > 5 || q_delta > 5) {
temp = rtw_read32(rtwdev, 0x2dbc);
--
2.55.0