blob: 78a55f72b604352409d26b140dbbd0ba4e77f670 [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>
Simon Glassed38aef2020-05-10 11:40:03 -060010#include <command.h>
Simon Glassf3cc7422020-07-19 10:15:36 -060011#include <dm.h>
Simon Glass63b17572014-02-26 15:59:23 -070012#include <dm-demo.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050013#include <mapmem.h>
Simon Glass63b17572014-02-26 15:59:23 -070014#include <asm/io.h>
15
Heiko Schocherb74fcb42014-05-22 12:43:05 +020016struct udevice *demo_dev;
Simon Glass63b17572014-02-26 15:59:23 -070017
Simon Glassed38aef2020-05-10 11:40:03 -060018static int do_demo_hello(struct cmd_tbl *cmdtp, int flag, int argc,
19 char *const argv[])
Simon Glass63b17572014-02-26 15:59:23 -070020{
21 int ch = 0;
22
23 if (argc)
24 ch = *argv[0];
25
26 return demo_hello(demo_dev, ch);
27}
28
Simon Glassed38aef2020-05-10 11:40:03 -060029static int do_demo_status(struct cmd_tbl *cmdtp, int flag, int argc,
30 char *const argv[])
Simon Glass63b17572014-02-26 15:59:23 -070031{
32 int status;
33 int ret;
34
35 ret = demo_status(demo_dev, &status);
36 if (ret)
37 return ret;
38
39 printf("Status: %d\n", status);
40
41 return 0;
42}
43
Simon Glassed38aef2020-05-10 11:40:03 -060044static int do_demo_light(struct cmd_tbl *cmdtp, int flag, int argc,
45 char *const argv[])
Simon Glass120834d2015-01-05 20:05:31 -070046{
47 int light;
48 int ret;
49
50 if (argc) {
51 light = simple_strtoul(argv[0], NULL, 16);
52 ret = demo_set_light(demo_dev, light);
53 } else {
54 ret = demo_get_light(demo_dev);
55 if (ret >= 0) {
56 printf("Light: %x\n", ret);
57 ret = 0;
58 }
59 }
60
61 return ret;
62}
63
Simon Glassed38aef2020-05-10 11:40:03 -060064int do_demo_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glass63b17572014-02-26 15:59:23 -070065{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020066 struct udevice *dev;
Simon Glass63b17572014-02-26 15:59:23 -070067 int i, ret;
68
69 puts("Demo uclass entries:\n");
70
71 for (i = 0, ret = uclass_first_device(UCLASS_DEMO, &dev);
72 dev;
73 ret = uclass_next_device(&dev)) {
Simon Glass71fa5b42020-12-03 16:55:18 -070074 printf("entry %d - instance %08x, ops %08x, plat %08x\n",
Mario Six61efece2018-02-12 08:05:57 +010075 i++, (uint)map_to_sysmem(dev),
76 (uint)map_to_sysmem(dev->driver->ops),
Simon Glassfa20e932020-12-03 16:55:20 -070077 (uint)map_to_sysmem(dev_get_plat(dev)));
Simon Glass63b17572014-02-26 15:59:23 -070078 }
79
80 return cmd_process_error(cmdtp, ret);
81}
82
Simon Glassed38aef2020-05-10 11:40:03 -060083static struct cmd_tbl demo_commands[] = {
Simon Glass63b17572014-02-26 15:59:23 -070084 U_BOOT_CMD_MKENT(list, 0, 1, do_demo_list, "", ""),
85 U_BOOT_CMD_MKENT(hello, 2, 1, do_demo_hello, "", ""),
Simon Glass120834d2015-01-05 20:05:31 -070086 U_BOOT_CMD_MKENT(light, 2, 1, do_demo_light, "", ""),
Simon Glass63b17572014-02-26 15:59:23 -070087 U_BOOT_CMD_MKENT(status, 1, 1, do_demo_status, "", ""),
88};
89
Simon Glassed38aef2020-05-10 11:40:03 -060090static int do_demo(struct cmd_tbl *cmdtp, int flag, int argc,
91 char *const argv[])
Simon Glass63b17572014-02-26 15:59:23 -070092{
Simon Glassed38aef2020-05-10 11:40:03 -060093 struct cmd_tbl *demo_cmd;
Simon Glass63b17572014-02-26 15:59:23 -070094 int devnum = 0;
95 int ret;
96
97 if (argc < 2)
98 return CMD_RET_USAGE;
99 demo_cmd = find_cmd_tbl(argv[1], demo_commands,
100 ARRAY_SIZE(demo_commands));
101 argc -= 2;
102 argv += 2;
Peter Tyser9001fa12015-02-03 13:18:49 -0600103
104 if ((!demo_cmd || argc > demo_cmd->maxargs) ||
105 ((demo_cmd->name[0] != 'l') && (argc < 1)))
Simon Glass63b17572014-02-26 15:59:23 -0700106 return CMD_RET_USAGE;
107
108 if (argc) {
109 devnum = simple_strtoul(argv[0], NULL, 10);
110 ret = uclass_get_device(UCLASS_DEMO, devnum, &demo_dev);
111 if (ret)
112 return cmd_process_error(cmdtp, ret);
113 argc--;
114 argv++;
Simon Glass120834d2015-01-05 20:05:31 -0700115 } else {
116 demo_dev = NULL;
117 if (demo_cmd->cmd != do_demo_list)
118 return CMD_RET_USAGE;
Simon Glass63b17572014-02-26 15:59:23 -0700119 }
120
121 ret = demo_cmd->cmd(demo_cmd, flag, argc, argv);
122
123 return cmd_process_error(demo_cmd, ret);
124}
125
126U_BOOT_CMD(
127 demo, 4, 1, do_demo,
128 "Driver model (dm) demo operations",
129 "list List available demo devices\n"
130 "demo hello <num> [<char>] Say hello\n"
Simon Glass120834d2015-01-05 20:05:31 -0700131 "demo light [<num>] Set or get the lights\n"
132 "demo status <num> Get demo device status\n"
Simon Glass63b17572014-02-26 15:59:23 -0700133);