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