blob: e800309de5e6bb7e9ae675d44dd57c31b4aec0d1 [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>
Zelong Dong9bf3ccf2024-09-06 08:35:50 +000012#include <asm/amlogic/arch/mailbox.h>
Zelong Donge6ef8152023-02-14 08:52:45 +000013
14#define KEY_PARAMS_NUM 10
15#define OTHER_PARAMS_NUM 1
16
Zelong Dong9bf3ccf2024-09-06 08:35:50 +000017#define MAX_KEY_NUM 16
18
19enum IrMboxCmdType {
20 IR_MBOX_CMD_SET_WAKEUP_LIST,
21 IR_MBOX_CMD_SET_DEBUG_LOG,
22 IR_MBOX_CMD_GET_WAKEUP_KEY,
23 IR_MBOX_CMD_SET_STATUS,
24 IR_MBOX_CMD_GET_PREBOOT_KEY
25};
26
Zelong Donge6ef8152023-02-14 08:52:45 +000027extern u32 get_time(void);
28
29static int do_irkey(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
30{
31 u32 key_buf[KEY_PARAMS_NUM];
32 u32 time_out = 0;
33 u32 time_base = 0;
34 struct udevice *ir_devp;
35 u32 key;
36 char *endp;
37 char str[16] = {0};
38 u8 i;
39
40 /*at least set a key*/
41 if (argc < 3)
42 return -EINVAL;
43
44 /*obtain timeout time*/
45 time_out = simple_strtoul(argv[1], &endp, 0);
46 printf("time_out = %d\n", time_out);
47 if (*argv[1] == 0 || *endp != 0)
48 return -EINVAL;
49
50 /*obtain IR keys value which need to detect*/
51 for (i = 2; i < argc; i++) {
52 key_buf[i - 2] = simple_strtoul(argv[i], &endp, 0);
53 printf("key[%d] = %x\n", i - 2, key_buf[i - 2]);
54 if (*argv[i] == 0 || *endp != 0)
55 return -EINVAL;
56 }
57
58 if (uclass_get_device_by_name(UCLASS_MISC, "meson-ir", &ir_devp)) {
59 printf("failed to get ir udevice\n");
60 return -EINVAL;
61 }
62
63 time_base = get_time();
64
65 while ((get_time() - time_base) < time_out) {
66 if (misc_read(ir_devp, 0, &key, 1))
67 continue;
68
69 printf("keycode = %x\n", key);
70 for (i = 2; i < argc; i++) {
71 if (key == key_buf[i - 2]) {
72 snprintf(str, sizeof(str), "0x%x", key);
73 env_set("irkey_value", str);
74 return 0;
75 }
76 }
77 }
78
79 return -EINVAL;
80}
81
Zelong Dong9bf3ccf2024-09-06 08:35:50 +000082static int do_irkey_wakeup(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
83{
84 char *endp;
85 u32 data[MAX_KEY_NUM + 3];
86 u32 framecode, ret;
87 char str[16] = {0};
88 u8 i;
89
90 if (argc < 2)
91 return CMD_RET_USAGE;
92
93 for (i = 1; i < argc; i++) {
94 data[i - 1] = simple_strtoul(argv[i], &endp, 0);
95 if (*argv[i] == 0 || *endp != 0)
96 return -EINVAL;
97 }
98
99 ret = scpi_send_data(AOCPU_REE_CHANNEL, CMD_SET_IR_STATE, data,
100 sizeof(data), &framecode, sizeof(framecode));
101 if (ret != 0)
102 printf("CMD_SET_IR_STATE failed\n");
103
104 snprintf(str, sizeof(str), "0x%x", framecode);
105 if (data[0] == IR_MBOX_CMD_GET_WAKEUP_KEY)
106 env_set("ir_wakeup_key", str);
107 else if (data[0] == IR_MBOX_CMD_GET_PREBOOT_KEY)
108 env_set("ir_preboot_key", str);
109
110 return 0;
111}
112
Zelong Donge6ef8152023-02-14 08:52:45 +0000113/*Max key arguments: 10*/
114U_BOOT_CMD(irkey, (KEY_PARAMS_NUM + OTHER_PARAMS_NUM + 1), 0, do_irkey,
115 "irkey <timeout> <key1> ...<keyN> - maximum value of N: 10",
116 NULL
117);
Zelong Dong9bf3ccf2024-09-06 08:35:50 +0000118
119U_BOOT_CMD(irkey_wakeup, (MAX_KEY_NUM + 3 + 1), 0, do_irkey_wakeup,
120 "irkey_wakeup",
121 "0 <key_num> <reason_bitmap> <key0> <key1> ... <key15>\n"
122 " - set wakeup table\n"
123 "irkey_wakeup 1 <1|0> - enable/disable bl30 ir debug log\n"
124 "irkey_wakeup 2 - get ir_wakeup_key\n"
125 "irkey_wakeup 3 <1|0> - enable/disable bl30 ir\n"
126 "irkey_wakeup 4 - get ir_preboot_key\n"
127);