summaryrefslogtreecommitdiff
path: root/common/cmd_irkey.c
blob: 82bdf995f75f4079a5b19ea8772a4984278b5772 (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
#include <asm/io.h>
#include <common.h>
#include <command.h>
#include <asm/arch/secure_apb.h>
#include <asm/cpu_id.h>

#define KEY_PARAMS_NUM  10
#define OTHER_PARAMS_NUM 1

extern uint32_t get_time(void);

void init_custom_trigger(void)
{
	if (get_cpu_id().family_id == MESON_CPU_MAJOR_ID_GXTVBB) {
		setbits_le32(P_AO_RTI_PIN_MUX_REG, 1 << 12); // SET IR_DEC_INPUT
	} else {
		setbits_le32(P_AO_RTI_PIN_MUX_REG, 1 << 0);  // SET IR_DEC_INPUT
		clrbits_le32(P_AO_RTI_PIN_MUX_REG, 1 << 21); //CLEAR IR_REMOTE_OUTPUT
	}
	writel((readl(P_AO_MF_IR_DEC_REG1) | (1 << 15)), P_AO_MF_IR_DEC_REG1);
}

void ir_release(void)
{
	if (get_cpu_id().family_id == MESON_CPU_MAJOR_ID_GXTVBB) {
		clrbits_le32(P_AO_RTI_PIN_MUX_REG, 1 << 12);
	} else {
	    clrbits_le32(P_AO_RTI_PIN_MUX_REG, 1 << 0);
	}
}

uint32_t read_key(void)
{
	unsigned int keycode;

	if (!(readl(P_AO_MF_IR_DEC_STATUS) & (1<<3))) {
		return 0;
	}

	keycode = readl(P_AO_MF_IR_DEC_FRAME);

	printf("keycode = %x\n",keycode);
	return keycode;
}

static int do_irkey(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	uint32_t key_buf[KEY_PARAMS_NUM];
	uint32_t time_out = 0;
	uint32_t time_base = 0;
	uint32_t key;
	char *endp;
	char str[8];
	u8 i;

	/*at least set a key*/
	if (argc < 3)
		return -1;

	/*obtain timeout time*/
	time_out = simple_strtoul(argv[1], &endp, 0);
	printf("time_out = %x\n",time_out);
	if (*argv[1] == 0 || *endp != 0)
		return -1;

	/*obtain IR keys value which need to detect*/
	for (i=2; i<argc; i++) {
		key_buf[i-2] = simple_strtoul(argv[i], &endp, 0);
		printf("key[%d] = %x\n",i-2, key_buf[i-2]);
		if (*argv[i] == 0 || *endp != 0)
			return -1;
	}

	init_custom_trigger();

	time_base = get_time();

	while ((get_time() - time_base) < time_out)
	{
		key = read_key();
		for (i=2; i<argc; i++) {
			if (key == key_buf[i-2]) {
				sprintf(str, "0x%x", key);
				setenv("irkey_value",str);
				ir_release();
				return 0;
			}
		}
	}
	ir_release();
	return -1;
}
/*Maxium key arguments: 10*/
U_BOOT_CMD(
	irkey, (KEY_PARAMS_NUM + OTHER_PARAMS_NUM + 1), 0, do_irkey,
	"irkey <timeout> <key1> ...<keyN> - maximum value of N: 10",
	NULL
);