1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (c) 2024, The Linux Foundation. All rights reserved. 4 */ 5 6 #include <linux/types.h> 7 8 #include "dp_utils.h" 9 10 u8 dp_utils_get_g0_value(u8 data) 11 { 12 u8 c[4]; 13 u8 g[4]; 14 u8 ret_data = 0; 15 u8 i; 16 17 for (i = 0; i < 4; i++) 18 c[i] = (data >> i) & 0x01; 19 20 g[0] = c[3]; 21 g[1] = c[0] ^ c[3]; 22 g[2] = c[1]; 23 g[3] = c[2]; 24 25 for (i = 0; i < 4; i++) 26 ret_data = ((g[i] & 0x01) << i) | ret_data; 27 28 return ret_data; 29 } 30 31 u8 dp_utils_get_g1_value(u8 data) 32 { 33 u8 c[4]; 34 u8 g[4]; 35 u8 ret_data = 0; 36 u8 i; 37 38 for (i = 0; i < 4; i++) 39 c[i] = (data >> i) & 0x01; 40 41 g[0] = c[0] ^ c[3]; 42 g[1] = c[0] ^ c[1] ^ c[3]; 43 g[2] = c[1] ^ c[2]; 44 g[3] = c[2] ^ c[3]; 45 46 for (i = 0; i < 4; i++) 47 ret_data = ((g[i] & 0x01) << i) | ret_data; 48 49 return ret_data; 50 } 51 52 u8 dp_utils_calculate_parity(u32 data) 53 { 54 u8 x0 = 0; 55 u8 x1 = 0; 56 u8 ci = 0; 57 u8 iData = 0; 58 u8 i = 0; 59 u8 parity_byte; 60 u8 num_byte = (data & 0xFF00) > 0 ? 8 : 2; 61 62 for (i = 0; i < num_byte; i++) { 63 iData = (data >> i * 4) & 0xF; 64 65 ci = iData ^ x1; 66 x1 = x0 ^ dp_utils_get_g1_value(ci); 67 x0 = dp_utils_get_g0_value(ci); 68 } 69 70 parity_byte = x1 | (x0 << 4); 71 72 return parity_byte; 73 } 74