aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-litecell15/misc/lc15bts_bid.c
blob: 1fb585142ffde86def2f73a069788616f5d20573 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Copyright (C) 2015 by Yves Godin <support@nuranwireless.com>
 * 
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#include "lc15bts_bid.h"

#define BOARD_REV_SYSFS		"/sys/devices/0.lc15/revision"
#define BOARD_OPT_SYSFS		"/sys/devices/0.lc15/option"
 
static const int option_type_mask[_NUM_OPTION_TYPES] = {
        [LC15BTS_OPTION_OCXO]		= 0x07,
	[LC15BTS_OPTION_FPGA]		= 0x03,
	[LC15BTS_OPTION_PA]		= 0x01,
	[LC15BTS_OPTION_BAND]		= 0x03,
	[LC15BTS_OPTION_TX_ISO_BYP]	= 0x01,
	[LC15BTS_OPTION_RX_DUP_BYP]	= 0x01,
	[LC15BTS_OPTION_RX_PB_BYP]	= 0x01,
	[LC15BTS_OPTION_RX_DIV]		= 0x01,
	[LC15BTS_OPTION_RX1A]		= 0x01,
	[LC15BTS_OPTION_RX1B]		= 0x01,
	[LC15BTS_OPTION_RX2A]		= 0x01,
	[LC15BTS_OPTION_RX2B]		= 0x01,
	[LC15BTS_OPTION_DDR_32B]	= 0x01,
	[LC15BTS_OPTION_DDR_ECC]	= 0x01,
	[LC15BTS_OPTION_LOG_DET]	= 0x01,
	[LC15BTS_OPTION_DUAL_LOG_DET]	= 0x01,
};

static const int option_type_shift[_NUM_OPTION_TYPES] = {
        [LC15BTS_OPTION_OCXO]		= 0,
	[LC15BTS_OPTION_FPGA]		= 3,
	[LC15BTS_OPTION_PA]		= 5,
	[LC15BTS_OPTION_BAND]		= 6,
	[LC15BTS_OPTION_TX_ISO_BYP]	= 8,
	[LC15BTS_OPTION_RX_DUP_BYP]	= 9,
	[LC15BTS_OPTION_RX_PB_BYP]	= 10,
	[LC15BTS_OPTION_RX_DIV]		= 11,
	[LC15BTS_OPTION_RX1A]		= 12,
	[LC15BTS_OPTION_RX1B]		= 13,
	[LC15BTS_OPTION_RX2A]		= 14,
	[LC15BTS_OPTION_RX2B]		= 15,
	[LC15BTS_OPTION_DDR_32B]	= 16,
	[LC15BTS_OPTION_DDR_ECC]	= 17,
	[LC15BTS_OPTION_LOG_DET]	= 18,
	[LC15BTS_OPTION_DUAL_LOG_DET]	= 19,
};


static int board_rev = -1;
static int board_option = -1;


int lc15bts_rev_get(void) 
{
	FILE *fp;
	char rev;

	if (board_rev != -1) {
		return board_rev;
	}

        fp = fopen(BOARD_REV_SYSFS, "r");
        if (fp == NULL) return -1;

        if (fscanf(fp, "%c", &rev) != 1) {
                fclose( fp );
                return -1;
        }
        fclose(fp);

	board_rev = rev;
	return board_rev;
}

int lc15bts_model_get(void)
{
        FILE *fp;
        int opt;


        if (board_option == -1) {
        	fp = fopen(BOARD_OPT_SYSFS, "r");
	        if (fp == NULL) {
			return -1;
		}

	        if (fscanf(fp, "%X", &opt) != 1) {
	                fclose( fp );
	                return -1;
	        }
	        fclose(fp);
	
	        board_option = opt;
	}
        return board_option;
}

int lc15bts_option_get(enum lc15bts_option_type type)
{
	int rc;
	int option;

	if (type >= _NUM_OPTION_TYPES) {
		return -EINVAL;
	}

	if (board_option == -1) {
		rc = lc15bts_model_get();
		if (rc < 0) return rc;
	}

	option = (board_option >> option_type_shift[type])
		& option_type_mask[type];

	return option;	
}