blob: 8a10bdf42a8906b9f212fe82c3d42afc79d5c028 [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>
12#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",
73 i++, map_to_sysmem(dev),
74 map_to_sysmem(dev->driver->ops),
75 map_to_sysmem(dev_get_platdata(dev)));
76 }
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);