blob: 109e7db3442062a8639cc8febf721db08909084c [file] [log] [blame]
developerb9b4cd12022-10-11 13:18:59 +08001/* Copyright (C) 2021-2022 Mediatek Inc. */
2
3#include "atenl.h"
4
5int 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",
developer9237f442024-06-14 17:13:04 +080014 an->main_phy_idx);
developerb9b4cd12022-10-11 13:18:59 +080015 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",
developer9237f442024-06-14 17:13:04 +080033 an->main_phy_idx);
developerb9b4cd12022-10-11 13:18:59 +080034 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;
52out:
53 close(fd);
54
55 return ret;
56}
57
58int 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",
developer9237f442024-06-14 17:13:04 +080066 an->main_phy_idx);
developerb9b4cd12022-10-11 13:18:59 +080067 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",
developer9237f442024-06-14 17:13:04 +080085 an->main_phy_idx);
developerb9b4cd12022-10-11 13:18:59 +080086 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;
101out:
102 close(fd);
103
104 return ret;
105}
106
107int 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",
developer9237f442024-06-14 17:13:04 +0800121 an->main_phy_idx);
developerb9b4cd12022-10-11 13:18:59 +0800122 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",
developer9237f442024-06-14 17:13:04 +0800140 an->main_phy_idx);
developerb9b4cd12022-10-11 13:18:59 +0800141 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;
159out:
160 close(fd);
161
162 return ret;
163}
164
165int 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",
developer9237f442024-06-14 17:13:04 +0800178 an->main_phy_idx);
developerb9b4cd12022-10-11 13:18:59 +0800179 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",
developer9237f442024-06-14 17:13:04 +0800197 an->main_phy_idx);
developerb9b4cd12022-10-11 13:18:59 +0800198 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;
213out:
214 close(fd);
215
216 return ret;
217}