1 /*
2  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
4  * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #ifndef __MT76X0U_EEPROM_H
17 #define __MT76X0U_EEPROM_H
18 
19 #include "../mt76x02_eeprom.h"
20 
21 struct mt76x0_dev;
22 
23 #define MT76X0U_EE_MAX_VER		0x0c
24 #define MT76X0_EEPROM_SIZE		512
25 
26 enum mt76x0_eeprom_access_modes {
27 	MT_EE_READ = 0,
28 	MT_EE_PHYSICAL_READ = 1,
29 };
30 
31 struct reg_channel_bounds {
32 	u8 start;
33 	u8 num;
34 };
35 
36 struct mt76x0_eeprom_params {
37 	u8 rf_freq_off;
38 	s16 temp_off;
39 	s8 rssi_offset_2ghz[2];
40 	s8 rssi_offset_5ghz[3];
41 	s8 lna_gain_2ghz;
42 	s8 lna_gain_5ghz[3];
43 	u8 pa_type;
44 
45 	/* TX_PWR_CFG_* values from EEPROM for 20 and 40 Mhz bandwidths. */
46 	u32 tx_pwr_cfg_2g[5][2];
47 	u32 tx_pwr_cfg_5g[5][2];
48 
49 	u8 tx_pwr_per_chan[58];
50 
51 	struct reg_channel_bounds reg;
52 };
53 
54 int mt76x0_eeprom_init(struct mt76x0_dev *dev);
55 
56 static inline u32 s6_validate(u32 reg)
57 {
58 	WARN_ON(reg & ~GENMASK(5, 0));
59 	return reg & GENMASK(5, 0);
60 }
61 
62 static inline int s6_to_int(u32 reg)
63 {
64 	int s6;
65 
66 	s6 = s6_validate(reg);
67 	if (s6 & BIT(5))
68 		s6 -= BIT(6);
69 
70 	return s6;
71 }
72 
73 static inline u32 int_to_s6(int val)
74 {
75 	if (val < -0x20)
76 		return 0x20;
77 	if (val > 0x1f)
78 		return 0x1f;
79 
80 	return val & 0x3f;
81 }
82 
83 #endif
84