blob: 743dc7d17660d1c3fca008d601a26b028a0e119b [file] [log] [blame]
Zelong Donge6ef8152023-02-14 08:52:45 +00001// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2/*
3 * Copyright (c) 2019 Amlogic, Inc. All rights reserved.
4 */
5
6#include <dm.h>
7#include <asm/io.h>
8#include <common.h>
9#include <command.h>
10#include <errno.h>
11#include <misc.h>
12
13#define KEY_PARAMS_NUM 10
14#define OTHER_PARAMS_NUM 1
15
16extern u32 get_time(void);
17
18static int do_irkey(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
19{
20 u32 key_buf[KEY_PARAMS_NUM];
21 u32 time_out = 0;
22 u32 time_base = 0;
23 struct udevice *ir_devp;
24 u32 key;
25 char *endp;
26 char str[16] = {0};
27 u8 i;
28
29 /*at least set a key*/
30 if (argc < 3)
31 return -EINVAL;
32
33 /*obtain timeout time*/
34 time_out = simple_strtoul(argv[1], &endp, 0);
35 printf("time_out = %d\n", time_out);
36 if (*argv[1] == 0 || *endp != 0)
37 return -EINVAL;
38
39 /*obtain IR keys value which need to detect*/
40 for (i = 2; i < argc; i++) {
41 key_buf[i - 2] = simple_strtoul(argv[i], &endp, 0);
42 printf("key[%d] = %x\n", i - 2, key_buf[i - 2]);
43 if (*argv[i] == 0 || *endp != 0)
44 return -EINVAL;
45 }
46
47 if (uclass_get_device_by_name(UCLASS_MISC, "meson-ir", &ir_devp)) {
48 printf("failed to get ir udevice\n");
49 return -EINVAL;
50 }
51
52 time_base = get_time();
53
54 while ((get_time() - time_base) < time_out) {
55 if (misc_read(ir_devp, 0, &key, 1))
56 continue;
57
58 printf("keycode = %x\n", key);
59 for (i = 2; i < argc; i++) {
60 if (key == key_buf[i - 2]) {
61 snprintf(str, sizeof(str), "0x%x", key);
62 env_set("irkey_value", str);
63 return 0;
64 }
65 }
66 }
67
68 return -EINVAL;
69}
70
71/*Max key arguments: 10*/
72U_BOOT_CMD(irkey, (KEY_PARAMS_NUM + OTHER_PARAMS_NUM + 1), 0, do_irkey,
73 "irkey <timeout> <key1> ...<keyN> - maximum value of N: 10",
74 NULL
75);