xref: /openbmc/u-boot/cmd/demo.c (revision d66ff4ba)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  *
5  * (C) Copyright 2012
6  * Pavel Herrmann <morpheus.ibis@gmail.com>
7  */
8 
9 #include <common.h>
10 #include <dm-demo.h>
11 #include <mapmem.h>
12 #include <asm/io.h>
13 
14 struct udevice *demo_dev;
15 
16 static int do_demo_hello(cmd_tbl_t *cmdtp, int flag, int argc,
17 			 char * const argv[])
18 {
19 	int ch = 0;
20 
21 	if (argc)
22 		ch = *argv[0];
23 
24 	return demo_hello(demo_dev, ch);
25 }
26 
27 static int do_demo_status(cmd_tbl_t *cmdtp, int flag, int argc,
28 			  char * const argv[])
29 {
30 	int status;
31 	int ret;
32 
33 	ret = demo_status(demo_dev, &status);
34 	if (ret)
35 		return ret;
36 
37 	printf("Status: %d\n", status);
38 
39 	return 0;
40 }
41 
42 static int do_demo_light(cmd_tbl_t *cmdtp, int flag, int argc,
43 			 char * const argv[])
44 {
45 	int light;
46 	int ret;
47 
48 	if (argc) {
49 		light = simple_strtoul(argv[0], NULL, 16);
50 		ret = demo_set_light(demo_dev, light);
51 	} else {
52 		ret = demo_get_light(demo_dev);
53 		if (ret >= 0) {
54 			printf("Light: %x\n", ret);
55 			ret = 0;
56 		}
57 	}
58 
59 	return ret;
60 }
61 
62 int do_demo_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
63 {
64 	struct udevice *dev;
65 	int i, ret;
66 
67 	puts("Demo uclass entries:\n");
68 
69 	for (i = 0, ret = uclass_first_device(UCLASS_DEMO, &dev);
70 	     dev;
71 	     ret = uclass_next_device(&dev)) {
72 		printf("entry %d - instance %08x, ops %08x, platdata %08x\n",
73 		       i++, (uint)map_to_sysmem(dev),
74 		       (uint)map_to_sysmem(dev->driver->ops),
75 		       (uint)map_to_sysmem(dev_get_platdata(dev)));
76 	}
77 
78 	return cmd_process_error(cmdtp, ret);
79 }
80 
81 static cmd_tbl_t demo_commands[] = {
82 	U_BOOT_CMD_MKENT(list, 0, 1, do_demo_list, "", ""),
83 	U_BOOT_CMD_MKENT(hello, 2, 1, do_demo_hello, "", ""),
84 	U_BOOT_CMD_MKENT(light, 2, 1, do_demo_light, "", ""),
85 	U_BOOT_CMD_MKENT(status, 1, 1, do_demo_status, "", ""),
86 };
87 
88 static int do_demo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
89 {
90 	cmd_tbl_t *demo_cmd;
91 	int devnum = 0;
92 	int ret;
93 
94 	if (argc < 2)
95 		return CMD_RET_USAGE;
96 	demo_cmd = find_cmd_tbl(argv[1], demo_commands,
97 				ARRAY_SIZE(demo_commands));
98 	argc -= 2;
99 	argv += 2;
100 
101 	if ((!demo_cmd || argc > demo_cmd->maxargs) ||
102 	    ((demo_cmd->name[0] != 'l') && (argc < 1)))
103 		return CMD_RET_USAGE;
104 
105 	if (argc) {
106 		devnum = simple_strtoul(argv[0], NULL, 10);
107 		ret = uclass_get_device(UCLASS_DEMO, devnum, &demo_dev);
108 		if (ret)
109 			return cmd_process_error(cmdtp, ret);
110 		argc--;
111 		argv++;
112 	} else {
113 		demo_dev = NULL;
114 		if (demo_cmd->cmd != do_demo_list)
115 			return CMD_RET_USAGE;
116 	}
117 
118 	ret = demo_cmd->cmd(demo_cmd, flag, argc, argv);
119 
120 	return cmd_process_error(demo_cmd, ret);
121 }
122 
123 U_BOOT_CMD(
124 	demo,   4,      1,      do_demo,
125 	"Driver model (dm) demo operations",
126 	"list                     List available demo devices\n"
127 	"demo hello <num> [<char>]     Say hello\n"
128 	"demo light [<num>]            Set or get the lights\n"
129 	"demo status <num>             Get demo device status\n"
130 	"demo list                     List available demo devices"
131 );
132