Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 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 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 14 | struct udevice *demo_dev; |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 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 | |
Simon Glass | 120834d | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 42 | static 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 Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 62 | int do_demo_list(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 63 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 64 | struct udevice *dev; |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 65 | 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 | |
| 81 | static 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 Glass | 120834d | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 84 | U_BOOT_CMD_MKENT(light, 2, 1, do_demo_light, "", ""), |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 85 | U_BOOT_CMD_MKENT(status, 1, 1, do_demo_status, "", ""), |
| 86 | }; |
| 87 | |
| 88 | static 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; |
| 100 | if (!demo_cmd || argc > demo_cmd->maxargs) |
| 101 | return CMD_RET_USAGE; |
| 102 | |
| 103 | if (argc) { |
| 104 | devnum = simple_strtoul(argv[0], NULL, 10); |
| 105 | ret = uclass_get_device(UCLASS_DEMO, devnum, &demo_dev); |
| 106 | if (ret) |
| 107 | return cmd_process_error(cmdtp, ret); |
| 108 | argc--; |
| 109 | argv++; |
Simon Glass | 120834d | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 110 | } else { |
| 111 | demo_dev = NULL; |
| 112 | if (demo_cmd->cmd != do_demo_list) |
| 113 | return CMD_RET_USAGE; |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | ret = demo_cmd->cmd(demo_cmd, flag, argc, argv); |
| 117 | |
| 118 | return cmd_process_error(demo_cmd, ret); |
| 119 | } |
| 120 | |
| 121 | U_BOOT_CMD( |
| 122 | demo, 4, 1, do_demo, |
| 123 | "Driver model (dm) demo operations", |
| 124 | "list List available demo devices\n" |
| 125 | "demo hello <num> [<char>] Say hello\n" |
Simon Glass | 120834d | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 126 | "demo light [<num>] Set or get the lights\n" |
| 127 | "demo status <num> Get demo device status\n" |
| 128 | "demo list List available demo devices" |
Simon Glass | 63b1757 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 129 | ); |