summaryrefslogtreecommitdiff
path: root/common/gpio_commands.c
diff options
context:
space:
mode:
authorAnton Staaf <robotboy@chromium.org>2016-03-02 08:53:12 -0800
committerchrome-bot <chrome-bot@chromium.org>2016-03-02 18:03:43 -0800
commit325fe5ac6079e28d21d78de75c3151db10614ebd (patch)
tree88d8d5356ee5843670b7fd682781cb7f2a58e4cc /common/gpio_commands.c
parent6877eee247250cb0e962ef8ddf1544aba4b75e6b (diff)
downloadchrome-ec-325fe5ac6079e28d21d78de75c3151db10614ebd.tar.gz
GPIO: Move host and console commands to new file
These commands, like other users of GPIOs should be able to use the public GPIO API, and thus do not need to be coupled directly to the GPIO common code. Signed-off-by: Anton Staaf <robotboy@chromium.org> BRANCH=None BUG=None TEST=make buildall -j Manually verified console commands on discovery board Change-Id: I6e38b9d103590d4f7c72813a33437067716a858c Reviewed-on: https://chromium-review.googlesource.com/329992 Commit-Ready: Anton Staaf <robotboy@chromium.org> Tested-by: Anton Staaf <robotboy@chromium.org> Reviewed-by: Randall Spangler <rspangler@chromium.org>
Diffstat (limited to 'common/gpio_commands.c')
-rw-r--r--common/gpio_commands.c208
1 files changed, 208 insertions, 0 deletions
diff --git a/common/gpio_commands.c b/common/gpio_commands.c
new file mode 100644
index 0000000000..25185597ea
--- /dev/null
+++ b/common/gpio_commands.c
@@ -0,0 +1,208 @@
+/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* GPIO common functionality for Chrome EC */
+
+#include "common.h"
+#include "console.h"
+#include "gpio.h"
+#include "host_command.h"
+#include "system.h"
+#include "util.h"
+
+static uint8_t last_val[(GPIO_COUNT + 7) / 8];
+
+/**
+ * Find a GPIO signal by name.
+ *
+ * @param name Signal name to find
+ *
+ * @return the signal index, or GPIO_COUNT if no match.
+ */
+static enum gpio_signal find_signal_by_name(const char *name)
+{
+ int i;
+
+ if (!name || !*name)
+ return GPIO_COUNT;
+
+ for (i = 0; i < GPIO_COUNT; i++)
+ if (gpio_is_implemented(i) &&
+ !strcasecmp(name, gpio_get_name(i)))
+ return i;
+
+ return GPIO_COUNT;
+}
+
+/**
+ * Update last_val
+ *
+ * @param i Index of last_val[] to update
+ * @param v New value for last_val[i]
+ *
+ * @return 1 if last_val[i] was updated, 0 if last_val[i]==v.
+ */
+static int last_val_changed(int i, int v)
+{
+ if (v && !(last_val[i / 8] & (1 << (i % 8)))) {
+ last_val[i / 8] |= 1 << (i % 8);
+ return 1;
+ } else if (!v && last_val[i / 8] & (1 << (i % 8))) {
+ last_val[i / 8] &= ~(1 << (i % 8));
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+static enum ec_error_list set(const char *name, int value)
+{
+ enum gpio_signal signal = find_signal_by_name(name);
+
+ if (signal == GPIO_COUNT)
+ return EC_ERROR_INVAL;
+
+ if (!gpio_is_implemented(signal))
+ return EC_ERROR_INVAL;
+
+ if (!(gpio_get_default_flags(signal) & GPIO_OUTPUT))
+ return EC_ERROR_INVAL;
+
+ gpio_set_level(signal, value);
+
+ return EC_SUCCESS;
+}
+/*****************************************************************************/
+/* Console commands */
+
+static int command_gpio_get(int argc, char **argv)
+{
+ int changed, v, i;
+
+ /* If a signal is specified, print only that one */
+ if (argc == 2) {
+ i = find_signal_by_name(argv[1]);
+ if (i == GPIO_COUNT)
+ return EC_ERROR_PARAM1;
+ v = gpio_get_level(i);
+ changed = last_val_changed(i, v);
+ ccprintf(" %d%c %s\n", v, (changed ? '*' : ' '),
+ gpio_get_name(i));
+
+ return EC_SUCCESS;
+ }
+
+ /* Otherwise print them all */
+ for (i = 0; i < GPIO_COUNT; i++) {
+ if (!gpio_is_implemented(i))
+ continue; /* Skip unsupported signals */
+
+ v = gpio_get_level(i);
+ changed = last_val_changed(i, v);
+ ccprintf(" %d%c %s\n", v, (changed ? '*' : ' '),
+ gpio_get_name(i));
+
+ /* Flush console to avoid truncating output */
+ cflush();
+ }
+
+ return EC_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(gpioget, command_gpio_get,
+ "[name]",
+ "Read GPIO value(s)",
+ NULL);
+
+static int command_gpio_set(int argc, char **argv)
+{
+ char *e;
+ int v;
+
+ if (argc < 3)
+ return EC_ERROR_PARAM_COUNT;
+
+ v = strtoi(argv[2], &e, 0);
+ if (*e)
+ return EC_ERROR_PARAM2;
+
+ if (set(argv[1], v) != EC_SUCCESS)
+ return EC_ERROR_PARAM1;
+
+ return EC_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(gpioset, command_gpio_set,
+ "name <0 | 1>",
+ "Set a GPIO",
+ NULL);
+
+/*****************************************************************************/
+/* Host commands */
+
+static int gpio_command_get(struct host_cmd_handler_args *args)
+{
+ const struct ec_params_gpio_get_v1 *p_v1 = args->params;
+ struct ec_response_gpio_get_v1 *r_v1 = args->response;
+ int i, len;
+
+ if (args->version == 0) {
+ const struct ec_params_gpio_get *p = args->params;
+ struct ec_response_gpio_get *r = args->response;
+
+ i = find_signal_by_name(p->name);
+ if (i == GPIO_COUNT)
+ return EC_RES_ERROR;
+
+ r->val = gpio_get_level(i);
+ args->response_size = sizeof(struct ec_response_gpio_get);
+ return EC_RES_SUCCESS;
+ }
+
+ switch (p_v1->subcmd) {
+ case EC_GPIO_GET_BY_NAME:
+ i = find_signal_by_name(p_v1->get_value_by_name.name);
+ if (i == GPIO_COUNT)
+ return EC_RES_ERROR;
+
+ r_v1->get_value_by_name.val = gpio_get_level(i);
+ args->response_size = sizeof(r_v1->get_value_by_name);
+ break;
+ case EC_GPIO_GET_COUNT:
+ r_v1->get_count.val = GPIO_COUNT;
+ args->response_size = sizeof(r_v1->get_count);
+ break;
+ case EC_GPIO_GET_INFO:
+ if (p_v1->get_info.index >= GPIO_COUNT)
+ return EC_RES_ERROR;
+
+ i = p_v1->get_info.index;
+ len = strlen(gpio_get_name(i));
+ memcpy(r_v1->get_info.name, gpio_get_name(i), len+1);
+ r_v1->get_info.val = gpio_get_level(i);
+ r_v1->get_info.flags = gpio_get_default_flags(i);
+ args->response_size = sizeof(r_v1->get_info);
+ break;
+ default:
+ return EC_RES_INVALID_PARAM;
+ }
+
+ return EC_RES_SUCCESS;
+
+}
+DECLARE_HOST_COMMAND(EC_CMD_GPIO_GET, gpio_command_get,
+ EC_VER_MASK(0) | EC_VER_MASK(1));
+
+static int gpio_command_set(struct host_cmd_handler_args *args)
+{
+ const struct ec_params_gpio_set *p = args->params;
+
+ if (system_is_locked())
+ return EC_RES_ACCESS_DENIED;
+
+ if (set(p->name, p->val) != EC_SUCCESS)
+ return EC_RES_ERROR;
+
+ return EC_RES_SUCCESS;
+}
+DECLARE_HOST_COMMAND(EC_CMD_GPIO_SET, gpio_command_set, EC_VER_MASK(0));