1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/errno.h>
4 #include <linux/kernel.h>
5 #include <adc.h>
6 #include <env.h>
7 
8 #define HW_ID_CHANNEL	1
9 
10 struct board_model {
11 	unsigned int low;
12 	unsigned int high;
13 	const char *fdtfile;
14 };
15 
16 static const struct board_model board_models[] = {
17 	{ 230, 270, "rockchip/rk3566-radxa-zero-3w.dtb" },
18 	{ 400, 450, "rockchip/rk3566-radxa-zero-3e.dtb" },
19 };
20 
get_board_model(void)21 static const struct board_model *get_board_model(void)
22 {
23 	unsigned int val;
24 	int i, ret;
25 
26 	ret = adc_channel_single_shot("saradc@fe720000", HW_ID_CHANNEL, &val);
27 	if (ret)
28 		return NULL;
29 
30 	for (i = 0; i < ARRAY_SIZE(board_models); i++) {
31 		unsigned int min = board_models[i].low;
32 		unsigned int max = board_models[i].high;
33 
34 		if (min <= val && val <= max)
35 			return &board_models[i];
36 	}
37 
38 	return NULL;
39 }
40 
rk_board_late_init(void)41 int rk_board_late_init(void)
42 {
43 	const struct board_model *model = get_board_model();
44 
45 	if (model)
46 		env_set("fdtfile", model->fdtfile);
47 
48 	return 0;
49 }
50 
board_fit_config_name_match(const char * name)51 int board_fit_config_name_match(const char *name)
52 {
53 	const struct board_model *model = get_board_model();
54 
55 	if (model && !strcmp(name, model->fdtfile))
56 		return 0;
57 
58 	return -EINVAL;
59 }
60