dm: Add a demonstration/example driver
[karo-tx-uboot.git] / common / cmd_demo.c
1 /*
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
14 struct device *demo_dev;
15
16 static 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
27 static 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
42 int do_demo_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
43 {
44         struct device *dev;
45         int i, ret;
46
47         puts("Demo uclass entries:\n");
48
49         for (i = 0, ret = uclass_first_device(UCLASS_DEMO, &dev);
50              dev;
51              ret = uclass_next_device(&dev)) {
52                 printf("entry %d - instance %08x, ops %08x, platdata %08x\n",
53                        i++, map_to_sysmem(dev),
54                        map_to_sysmem(dev->driver->ops),
55                        map_to_sysmem(dev_get_platdata(dev)));
56         }
57
58         return cmd_process_error(cmdtp, ret);
59 }
60
61 static cmd_tbl_t demo_commands[] = {
62         U_BOOT_CMD_MKENT(list, 0, 1, do_demo_list, "", ""),
63         U_BOOT_CMD_MKENT(hello, 2, 1, do_demo_hello, "", ""),
64         U_BOOT_CMD_MKENT(status, 1, 1, do_demo_status, "", ""),
65 };
66
67 static int do_demo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
68 {
69         cmd_tbl_t *demo_cmd;
70         int devnum = 0;
71         int ret;
72
73         if (argc < 2)
74                 return CMD_RET_USAGE;
75         demo_cmd = find_cmd_tbl(argv[1], demo_commands,
76                                 ARRAY_SIZE(demo_commands));
77         argc -= 2;
78         argv += 2;
79         if (!demo_cmd || argc > demo_cmd->maxargs)
80                 return CMD_RET_USAGE;
81
82         if (argc) {
83                 devnum = simple_strtoul(argv[0], NULL, 10);
84                 ret = uclass_get_device(UCLASS_DEMO, devnum, &demo_dev);
85                 if (ret)
86                         return cmd_process_error(cmdtp, ret);
87                 argc--;
88                 argv++;
89         }
90
91         ret = demo_cmd->cmd(demo_cmd, flag, argc, argv);
92
93         return cmd_process_error(demo_cmd, ret);
94 }
95
96 U_BOOT_CMD(
97         demo,   4,      1,      do_demo,
98         "Driver model (dm) demo operations",
99         "list                     List available demo devices\n"
100         "demo hello <num> [<char>]     Say hello\n"
101         "demo status <num>             Get demo device status"
102 );