blob: 1b106633ff112eaf9489b18460a12a293a316930 [file] [log] [blame]
developer3abe1ad2022-01-24 11:13:32 +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",
14 get_band_val(an, 0, phy_idx));
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",
33 get_band_val(an, 0, phy_idx));
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;
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",
66 get_band_val(an, 0, phy_idx));
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",
85 get_band_val(an, 0, phy_idx));
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;
101out:
102 close(fd);
103
104 return ret;
105}