Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
| 4 | * |
| 5 | * (C) Copyright 2012 |
| 6 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 11 | #include <dm-demo.h> |
Joe Hershberger | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 12 | #include <mapmem.h> |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 15 | struct udevice *demo_dev; |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 16 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 17 | static int do_demo_hello(struct cmd_tbl *cmdtp, int flag, int argc, |
| 18 | char *const argv[]) |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 19 | { |
| 20 | int ch = 0; |
| 21 | |
| 22 | if (argc) |
| 23 | ch = *argv[0]; |
| 24 | |
| 25 | return demo_hello(demo_dev, ch); |
| 26 | } |
| 27 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 28 | static int do_demo_status(struct cmd_tbl *cmdtp, int flag, int argc, |
| 29 | char *const argv[]) |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 30 | { |
| 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 43 | static int do_demo_light(struct cmd_tbl *cmdtp, int flag, int argc, |
| 44 | char *const argv[]) |
Simon Glass | 120834d | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 45 | { |
| 46 | int light; |
| 47 | int ret; |
| 48 | |
| 49 | if (argc) { |
| 50 | light = simple_strtoul(argv[0], NULL, 16); |
| 51 | 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 63 | int do_demo_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 64 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 65 | struct udevice *dev; |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 66 | int i, ret; |
| 67 | |
| 68 | puts("Demo uclass entries:\n"); |
| 69 | |
| 70 | for (i = 0, ret = uclass_first_device(UCLASS_DEMO, &dev); |
| 71 | dev; |
| 72 | ret = uclass_next_device(&dev)) { |
| 73 | printf("entry %d - instance %08x, ops %08x, platdata %08x\n", |
Mario Six | 61efece | 2018-02-12 08:05:57 +0100 | [diff] [blame] | 74 | i++, (uint)map_to_sysmem(dev), |
| 75 | (uint)map_to_sysmem(dev->driver->ops), |
| 76 | (uint)map_to_sysmem(dev_get_platdata(dev))); |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return cmd_process_error(cmdtp, ret); |
| 80 | } |
| 81 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 82 | static struct cmd_tbl demo_commands[] = { |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 83 | U_BOOT_CMD_MKENT(list, 0, 1, do_demo_list, "", ""), |
| 84 | U_BOOT_CMD_MKENT(hello, 2, 1, do_demo_hello, "", ""), |
Simon Glass | 120834d | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 85 | U_BOOT_CMD_MKENT(light, 2, 1, do_demo_light, "", ""), |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 86 | U_BOOT_CMD_MKENT(status, 1, 1, do_demo_status, "", ""), |
| 87 | }; |
| 88 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 89 | static int do_demo(struct cmd_tbl *cmdtp, int flag, int argc, |
| 90 | char *const argv[]) |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 91 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 92 | struct cmd_tbl *demo_cmd; |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 93 | int devnum = 0; |
| 94 | int ret; |
| 95 | |
| 96 | if (argc < 2) |
| 97 | return CMD_RET_USAGE; |
| 98 | demo_cmd = find_cmd_tbl(argv[1], demo_commands, |
| 99 | ARRAY_SIZE(demo_commands)); |
| 100 | argc -= 2; |
| 101 | argv += 2; |
Peter Tyser | 9001fa1 | 2015-02-03 13:18:49 -0600 | [diff] [blame] | 102 | |
| 103 | if ((!demo_cmd || argc > demo_cmd->maxargs) || |
| 104 | ((demo_cmd->name[0] != 'l') && (argc < 1))) |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 105 | return CMD_RET_USAGE; |
| 106 | |
| 107 | if (argc) { |
| 108 | devnum = simple_strtoul(argv[0], NULL, 10); |
| 109 | ret = uclass_get_device(UCLASS_DEMO, devnum, &demo_dev); |
| 110 | if (ret) |
| 111 | return cmd_process_error(cmdtp, ret); |
| 112 | argc--; |
| 113 | argv++; |
Simon Glass | 120834d | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 114 | } else { |
| 115 | demo_dev = NULL; |
| 116 | if (demo_cmd->cmd != do_demo_list) |
| 117 | return CMD_RET_USAGE; |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | ret = demo_cmd->cmd(demo_cmd, flag, argc, argv); |
| 121 | |
| 122 | return cmd_process_error(demo_cmd, ret); |
| 123 | } |
| 124 | |
| 125 | U_BOOT_CMD( |
| 126 | demo, 4, 1, do_demo, |
| 127 | "Driver model (dm) demo operations", |
| 128 | "list List available demo devices\n" |
| 129 | "demo hello <num> [<char>] Say hello\n" |
Simon Glass | 120834d | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 130 | "demo light [<num>] Set or get the lights\n" |
| 131 | "demo status <num> Get demo device status\n" |
| 132 | "demo list List available demo devices" |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 133 | ); |