developer | b9b4cd1 | 2022-10-11 13:18:59 +0800 | [diff] [blame] | 1 | /* Copyright (C) 2021-2022 Mediatek Inc. */ |
| 2 | |
| 3 | #include "atenl.h" |
| 4 | |
| 5 | int atenl_reg_read(struct atenl *an, u32 offset, u32 *res) |
| 6 | { |
| 7 | char dir[64], buf[16]; |
| 8 | unsigned long val; |
| 9 | int fd, ret; |
| 10 | |
| 11 | /* write offset into regidx */ |
| 12 | ret = snprintf(dir, sizeof(dir), |
| 13 | "/sys/kernel/debug/ieee80211/phy%d/mt76/regidx", |
developer | 9237f44 | 2024-06-14 17:13:04 +0800 | [diff] [blame] | 14 | an->main_phy_idx); |
developer | b9b4cd1 | 2022-10-11 13:18:59 +0800 | [diff] [blame] | 15 | if (snprintf_error(sizeof(dir), ret)) |
| 16 | return ret; |
| 17 | |
| 18 | fd = open(dir, O_WRONLY); |
| 19 | if (fd < 0) |
| 20 | return fd; |
| 21 | |
| 22 | ret = snprintf(buf, sizeof(buf), "0x%x", offset); |
| 23 | if (snprintf_error(sizeof(buf), ret)) |
| 24 | goto out; |
| 25 | |
| 26 | lseek(fd, 0, SEEK_SET); |
| 27 | write(fd, buf, sizeof(buf)); |
| 28 | close(fd); |
| 29 | |
| 30 | /* read value from regval */ |
| 31 | ret = snprintf(dir, sizeof(dir), |
| 32 | "/sys/kernel/debug/ieee80211/phy%d/mt76/regval", |
developer | 9237f44 | 2024-06-14 17:13:04 +0800 | [diff] [blame] | 33 | an->main_phy_idx); |
developer | b9b4cd1 | 2022-10-11 13:18:59 +0800 | [diff] [blame] | 34 | if (snprintf_error(sizeof(dir), ret)) |
| 35 | return ret; |
| 36 | |
| 37 | fd = open(dir, O_RDONLY); |
| 38 | if (fd < 0) |
| 39 | return fd; |
| 40 | |
| 41 | ret = read(fd, buf, sizeof(buf) - 1); |
| 42 | if (ret < 0) |
| 43 | goto out; |
| 44 | buf[ret] = 0; |
| 45 | |
| 46 | val = strtoul(buf, NULL, 16); |
| 47 | if (val > (u32) -1) |
| 48 | return -EINVAL; |
| 49 | |
| 50 | *res = val; |
| 51 | ret = 0; |
| 52 | out: |
| 53 | close(fd); |
| 54 | |
| 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | int atenl_reg_write(struct atenl *an, u32 offset, u32 val) |
| 59 | { |
| 60 | char dir[64], buf[16]; |
| 61 | int fd, ret; |
| 62 | |
| 63 | /* write offset into regidx */ |
| 64 | ret = snprintf(dir, sizeof(dir), |
| 65 | "/sys/kernel/debug/ieee80211/phy%d/mt76/regidx", |
developer | 9237f44 | 2024-06-14 17:13:04 +0800 | [diff] [blame] | 66 | an->main_phy_idx); |
developer | b9b4cd1 | 2022-10-11 13:18:59 +0800 | [diff] [blame] | 67 | if (snprintf_error(sizeof(dir), ret)) |
| 68 | return ret; |
| 69 | |
| 70 | fd = open(dir, O_WRONLY); |
| 71 | if (fd < 0) |
| 72 | return fd; |
| 73 | |
| 74 | ret = snprintf(buf, sizeof(buf), "0x%x", offset); |
| 75 | if (snprintf_error(sizeof(buf), ret)) |
| 76 | goto out; |
| 77 | |
| 78 | lseek(fd, 0, SEEK_SET); |
| 79 | write(fd, buf, sizeof(buf)); |
| 80 | close(fd); |
| 81 | |
| 82 | /* write value into regval */ |
| 83 | ret = snprintf(dir, sizeof(dir), |
| 84 | "/sys/kernel/debug/ieee80211/phy%d/mt76/regval", |
developer | 9237f44 | 2024-06-14 17:13:04 +0800 | [diff] [blame] | 85 | an->main_phy_idx); |
developer | b9b4cd1 | 2022-10-11 13:18:59 +0800 | [diff] [blame] | 86 | if (snprintf_error(sizeof(dir), ret)) |
| 87 | return ret; |
| 88 | |
| 89 | fd = open(dir, O_WRONLY); |
| 90 | if (fd < 0) |
| 91 | return fd; |
| 92 | |
| 93 | ret = snprintf(buf, sizeof(buf), "0x%x", val); |
| 94 | if (snprintf_error(sizeof(buf), ret)) |
| 95 | goto out; |
| 96 | buf[ret] = 0; |
| 97 | |
| 98 | lseek(fd, 0, SEEK_SET); |
| 99 | write(fd, buf, sizeof(buf)); |
| 100 | ret = 0; |
| 101 | out: |
| 102 | close(fd); |
| 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | int atenl_rf_read(struct atenl *an, u32 wf_sel, u32 offset, u32 *res) |
| 108 | { |
| 109 | char dir[64], buf[16]; |
| 110 | unsigned long val; |
| 111 | int fd, ret; |
| 112 | u32 regidx; |
| 113 | |
| 114 | /* merge wf_sel and offset into regidx */ |
| 115 | regidx = FIELD_PREP(GENMASK(31, 28), wf_sel) | |
| 116 | FIELD_PREP(GENMASK(27, 0), offset); |
| 117 | |
| 118 | /* write regidx */ |
| 119 | ret = snprintf(dir, sizeof(dir), |
| 120 | "/sys/kernel/debug/ieee80211/phy%d/mt76/regidx", |
developer | 9237f44 | 2024-06-14 17:13:04 +0800 | [diff] [blame] | 121 | an->main_phy_idx); |
developer | b9b4cd1 | 2022-10-11 13:18:59 +0800 | [diff] [blame] | 122 | if (snprintf_error(sizeof(dir), ret)) |
| 123 | return ret; |
| 124 | |
| 125 | fd = open(dir, O_WRONLY); |
| 126 | if (fd < 0) |
| 127 | return fd; |
| 128 | |
| 129 | ret = snprintf(buf, sizeof(buf), "0x%x", regidx); |
| 130 | if (snprintf_error(sizeof(buf), ret)) |
| 131 | goto out; |
| 132 | |
| 133 | lseek(fd, 0, SEEK_SET); |
| 134 | write(fd, buf, sizeof(buf)); |
| 135 | close(fd); |
| 136 | |
| 137 | /* read from rf_regval */ |
| 138 | ret = snprintf(dir, sizeof(dir), |
| 139 | "/sys/kernel/debug/ieee80211/phy%d/mt76/rf_regval", |
developer | 9237f44 | 2024-06-14 17:13:04 +0800 | [diff] [blame] | 140 | an->main_phy_idx); |
developer | b9b4cd1 | 2022-10-11 13:18:59 +0800 | [diff] [blame] | 141 | if (snprintf_error(sizeof(dir), ret)) |
| 142 | return ret; |
| 143 | |
| 144 | fd = open(dir, O_RDONLY); |
| 145 | if (fd < 0) |
| 146 | return fd; |
| 147 | |
| 148 | ret = read(fd, buf, sizeof(buf) - 1); |
| 149 | if (ret < 0) |
| 150 | goto out; |
| 151 | buf[ret] = 0; |
| 152 | |
| 153 | val = strtoul(buf, NULL, 16); |
| 154 | if (val > (u32) -1) |
| 155 | return -EINVAL; |
| 156 | |
| 157 | *res = val; |
| 158 | ret = 0; |
| 159 | out: |
| 160 | close(fd); |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | int atenl_rf_write(struct atenl *an, u32 wf_sel, u32 offset, u32 val) |
| 166 | { |
| 167 | char dir[64], buf[16]; |
| 168 | int fd, ret; |
| 169 | u32 regidx; |
| 170 | |
| 171 | /* merge wf_sel and offset into regidx */ |
| 172 | regidx = FIELD_PREP(GENMASK(31, 28), wf_sel) | |
| 173 | FIELD_PREP(GENMASK(27, 0), offset); |
| 174 | |
| 175 | /* write regidx */ |
| 176 | ret = snprintf(dir, sizeof(dir), |
| 177 | "/sys/kernel/debug/ieee80211/phy%d/mt76/regidx", |
developer | 9237f44 | 2024-06-14 17:13:04 +0800 | [diff] [blame] | 178 | an->main_phy_idx); |
developer | b9b4cd1 | 2022-10-11 13:18:59 +0800 | [diff] [blame] | 179 | if (snprintf_error(sizeof(dir), ret)) |
| 180 | return ret; |
| 181 | |
| 182 | fd = open(dir, O_WRONLY); |
| 183 | if (fd < 0) |
| 184 | return fd; |
| 185 | |
| 186 | ret = snprintf(buf, sizeof(buf), "0x%x", regidx); |
| 187 | if (snprintf_error(sizeof(buf), ret)) |
| 188 | goto out; |
| 189 | |
| 190 | lseek(fd, 0, SEEK_SET); |
| 191 | write(fd, buf, sizeof(buf)); |
| 192 | close(fd); |
| 193 | |
| 194 | /* write value into rf_val */ |
| 195 | ret = snprintf(dir, sizeof(dir), |
| 196 | "/sys/kernel/debug/ieee80211/phy%d/mt76/rf_regval", |
developer | 9237f44 | 2024-06-14 17:13:04 +0800 | [diff] [blame] | 197 | an->main_phy_idx); |
developer | b9b4cd1 | 2022-10-11 13:18:59 +0800 | [diff] [blame] | 198 | if (snprintf_error(sizeof(dir), ret)) |
| 199 | return ret; |
| 200 | |
| 201 | fd = open(dir, O_WRONLY); |
| 202 | if (fd < 0) |
| 203 | return fd; |
| 204 | |
| 205 | ret = snprintf(buf, sizeof(buf), "0x%x", val); |
| 206 | if (snprintf_error(sizeof(buf), ret)) |
| 207 | goto out; |
| 208 | buf[ret] = 0; |
| 209 | |
| 210 | lseek(fd, 0, SEEK_SET); |
| 211 | write(fd, buf, sizeof(buf)); |
| 212 | ret = 0; |
| 213 | out: |
| 214 | close(fd); |
| 215 | |
| 216 | return ret; |
| 217 | } |