blob: 52c6e7ec15be701bdc21e74cb0f3f3643eeaa55d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass63b17572014-02-26 15:59:23 -07002/*
3 * Copyright (c) 2013 Google, Inc
4 *
5 * (C) Copyright 2012
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glass63b17572014-02-26 15:59:23 -07007 */
8
9#include <common.h>
10#include <dm-demo.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050011#include <mapmem.h>
Simon Glass63b17572014-02-26 15:59:23 -070012#include <asm/io.h>
13
Heiko Schocherb74fcb42014-05-22 12:43:05 +020014struct udevice *demo_dev;
Simon Glass63b17572014-02-26 15:59:23 -070015
16static 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
27static 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
Simon Glass120834d2015-01-05 20:05:31 -070042static 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
Simon Glass63b17572014-02-26 15:59:23 -070062int do_demo_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
63{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020064 struct udevice *dev;
Simon Glass63b17572014-02-26 15:59:23 -070065 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",
Mario Six61efece2018-02-12 08:05:57 +010073 i++, (uint)map_to_sysmem(dev),
74 (uint)map_to_sysmem(dev->driver->ops),
75 (uint)map_to_sysmem(dev_get_platdata(dev)));
Simon Glass63b17572014-02-26 15:59:23 -070076 }
77
78 return cmd_process_error(cmdtp, ret);
79}
80
81static 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, "", ""),
Simon Glass120834d2015-01-05 20:05:31 -070084 U_BOOT_CMD_MKENT(light, 2, 1, do_demo_light, "", ""),
Simon Glass63b17572014-02-26 15:59:23 -070085 U_BOOT_CMD_MKENT(status, 1, 1, do_demo_status, "", ""),
86};
87
88static 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;
Peter Tyser9001fa12015-02-03 13:18:49 -0600100
101 if ((!demo_cmd || argc > demo_cmd->maxargs) ||
102 ((demo_cmd->name[0] != 'l') && (argc < 1)))
Simon Glass63b17572014-02-26 15:59:23 -0700103 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++;
Simon Glass120834d2015-01-05 20:05:31 -0700112 } else {
113 demo_dev = NULL;
114 if (demo_cmd->cmd != do_demo_list)
115 return CMD_RET_USAGE;
Simon Glass63b17572014-02-26 15:59:23 -0700116 }
117
118 ret = demo_cmd->cmd(demo_cmd, flag, argc, argv);
119
120 return cmd_process_error(demo_cmd, ret);
121}
122
123U_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"
Simon Glass120834d2015-01-05 20:05:31 -0700128 "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"
Simon Glass63b17572014-02-26 15:59:23 -0700131);