blob: 5c422ac165bd7cb3bd3beb5971cc046f931878aa [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
Simon Glassed38aef2020-05-10 11:40:03 -06009#include <command.h>
Simon Glassf3cc7422020-07-19 10:15:36 -060010#include <dm.h>
Simon Glass63b17572014-02-26 15:59:23 -070011#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
Simon Glassed38aef2020-05-10 11:40:03 -060017static int do_demo_hello(struct cmd_tbl *cmdtp, int flag, int argc,
18 char *const argv[])
Simon Glass63b17572014-02-26 15:59:23 -070019{
20 int ch = 0;
21
22 if (argc)
23 ch = *argv[0];
24
25 return demo_hello(demo_dev, ch);
26}
27
Simon Glassed38aef2020-05-10 11:40:03 -060028static int do_demo_status(struct cmd_tbl *cmdtp, int flag, int argc,
29 char *const argv[])
Simon Glass63b17572014-02-26 15:59:23 -070030{
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 Glassed38aef2020-05-10 11:40:03 -060043static int do_demo_light(struct cmd_tbl *cmdtp, int flag, int argc,
44 char *const argv[])
Simon Glass120834d2015-01-05 20:05:31 -070045{
46 int light;
47 int ret;
48
49 if (argc) {
Simon Glass3ff49ec2021-07-24 09:03:29 -060050 light = hextoul(argv[0], NULL);
Simon Glass120834d2015-01-05 20:05:31 -070051 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 Glassed38aef2020-05-10 11:40:03 -060063int do_demo_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glass63b17572014-02-26 15:59:23 -070064{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020065 struct udevice *dev;
Michal Suchanek1d524ae2022-10-12 21:57:58 +020066 int i, ret, err = 0;
Simon Glass63b17572014-02-26 15:59:23 -070067
68 puts("Demo uclass entries:\n");
69
Michal Suchanek1d524ae2022-10-12 21:57:58 +020070 for (i = 0, ret = uclass_first_device_check(UCLASS_DEMO, &dev);
Simon Glass63b17572014-02-26 15:59:23 -070071 dev;
Michal Suchanek1d524ae2022-10-12 21:57:58 +020072 ret = uclass_next_device_check(&dev)) {
73 printf("entry %d - instance %08x, ops %08x, plat %08x, status %i\n",
Mario Six61efece2018-02-12 08:05:57 +010074 i++, (uint)map_to_sysmem(dev),
75 (uint)map_to_sysmem(dev->driver->ops),
Michal Suchanek1d524ae2022-10-12 21:57:58 +020076 (uint)map_to_sysmem(dev_get_plat(dev)),
77 ret);
78 if (ret)
79 err = ret;
Simon Glass63b17572014-02-26 15:59:23 -070080 }
81
Michal Suchanek1d524ae2022-10-12 21:57:58 +020082 return cmd_process_error(cmdtp, err);
Simon Glass63b17572014-02-26 15:59:23 -070083}
84
Simon Glassed38aef2020-05-10 11:40:03 -060085static struct cmd_tbl demo_commands[] = {
Simon Glass63b17572014-02-26 15:59:23 -070086 U_BOOT_CMD_MKENT(list, 0, 1, do_demo_list, "", ""),
87 U_BOOT_CMD_MKENT(hello, 2, 1, do_demo_hello, "", ""),
Simon Glass120834d2015-01-05 20:05:31 -070088 U_BOOT_CMD_MKENT(light, 2, 1, do_demo_light, "", ""),
Simon Glass63b17572014-02-26 15:59:23 -070089 U_BOOT_CMD_MKENT(status, 1, 1, do_demo_status, "", ""),
90};
91
Simon Glassed38aef2020-05-10 11:40:03 -060092static int do_demo(struct cmd_tbl *cmdtp, int flag, int argc,
93 char *const argv[])
Simon Glass63b17572014-02-26 15:59:23 -070094{
Simon Glassed38aef2020-05-10 11:40:03 -060095 struct cmd_tbl *demo_cmd;
Simon Glass63b17572014-02-26 15:59:23 -070096 int devnum = 0;
97 int ret;
98
99 if (argc < 2)
100 return CMD_RET_USAGE;
101 demo_cmd = find_cmd_tbl(argv[1], demo_commands,
102 ARRAY_SIZE(demo_commands));
103 argc -= 2;
104 argv += 2;
Peter Tyser9001fa12015-02-03 13:18:49 -0600105
106 if ((!demo_cmd || argc > demo_cmd->maxargs) ||
107 ((demo_cmd->name[0] != 'l') && (argc < 1)))
Simon Glass63b17572014-02-26 15:59:23 -0700108 return CMD_RET_USAGE;
109
110 if (argc) {
Simon Glassff9b9032021-07-24 09:03:30 -0600111 devnum = dectoul(argv[0], NULL);
Simon Glass63b17572014-02-26 15:59:23 -0700112 ret = uclass_get_device(UCLASS_DEMO, devnum, &demo_dev);
113 if (ret)
114 return cmd_process_error(cmdtp, ret);
115 argc--;
116 argv++;
Simon Glass120834d2015-01-05 20:05:31 -0700117 } else {
118 demo_dev = NULL;
119 if (demo_cmd->cmd != do_demo_list)
120 return CMD_RET_USAGE;
Simon Glass63b17572014-02-26 15:59:23 -0700121 }
122
123 ret = demo_cmd->cmd(demo_cmd, flag, argc, argv);
124
125 return cmd_process_error(demo_cmd, ret);
126}
127
128U_BOOT_CMD(
129 demo, 4, 1, do_demo,
130 "Driver model (dm) demo operations",
131 "list List available demo devices\n"
132 "demo hello <num> [<char>] Say hello\n"
Simon Glass120834d2015-01-05 20:05:31 -0700133 "demo light [<num>] Set or get the lights\n"
134 "demo status <num> Get demo device status\n"
Simon Glass63b17572014-02-26 15:59:23 -0700135);