Simon Glass | 39f7611 | 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 | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 14 | struct udevice *demo_dev; |
Simon Glass | 39f7611 | 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 | a02af4a | 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 | 39f7611 | 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 | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 64 | struct udevice *dev; |
Simon Glass | 39f7611 | 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 | a02af4a | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 84 | U_BOOT_CMD_MKENT(light, 2, 1, do_demo_light, "", ""), |
Simon Glass | 39f7611 | 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; |
Peter Tyser | b922a5f | 2015-02-03 13:18:49 -0600 | [diff] [blame] | 100 | |
| 101 | if ((!demo_cmd || argc > demo_cmd->maxargs) || |
| 102 | ((demo_cmd->name[0] != 'l') && (argc < 1))) |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 103 | return CMD_RET_USAGE; |
| 104 | |
| 105 | if (argc) { |
| 106 | devnum = simple_strtoul(argv[0], NULL, 10); |
| 107 | ret = uclass_get_device(UCLASS_DEMO, devnum, &demo_dev); |
| 108 | if (ret) |
| 109 | return cmd_process_error(cmdtp, ret); |
| 110 | argc--; |
| 111 | argv++; |
Simon Glass | a02af4a | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 112 | } else { |
| 113 | demo_dev = NULL; |
| 114 | if (demo_cmd->cmd != do_demo_list) |
| 115 | return CMD_RET_USAGE; |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | ret = demo_cmd->cmd(demo_cmd, flag, argc, argv); |
| 119 | |
| 120 | return cmd_process_error(demo_cmd, ret); |
| 121 | } |
| 122 | |
| 123 | U_BOOT_CMD( |
| 124 | demo, 4, 1, do_demo, |
| 125 | "Driver model (dm) demo operations", |
| 126 | "list List available demo devices\n" |
| 127 | "demo hello <num> [<char>] Say hello\n" |
Simon Glass | a02af4a | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 128 | "demo light [<num>] Set or get the lights\n" |
| 129 | "demo status <num> Get demo device status\n" |
| 130 | "demo list List available demo devices" |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 131 | ); |