Mike Frysinger | b52c2d1 | 2010-06-02 04:34:49 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Control GPIO pins on the fly |
| 3 | * |
| 4 | * Copyright (c) 2008-2010 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | |
| 12 | #include <asm/blackfin.h> |
| 13 | #include <asm/gpio.h> |
| 14 | |
| 15 | enum { |
| 16 | GPIO_INPUT, |
| 17 | GPIO_SET, |
| 18 | GPIO_CLEAR, |
| 19 | GPIO_TOGGLE, |
| 20 | }; |
| 21 | |
| 22 | int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 23 | { |
| 24 | if (argc == 2 && !strcmp(argv[1], "status")) { |
| 25 | bfin_gpio_labels(); |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | if (argc != 3) { |
| 30 | show_usage: |
| 31 | printf("Usage:\n%s\n", cmdtp->usage); |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | /* parse the behavior */ |
| 36 | ulong sub_cmd; |
| 37 | switch (argv[1][0]) { |
| 38 | case 'i': sub_cmd = GPIO_INPUT; break; |
| 39 | case 's': sub_cmd = GPIO_SET; break; |
| 40 | case 'c': sub_cmd = GPIO_CLEAR; break; |
| 41 | case 't': sub_cmd = GPIO_TOGGLE; break; |
| 42 | default: goto show_usage; |
| 43 | } |
| 44 | |
| 45 | /* parse the pin with format: [p][port]<#> */ |
| 46 | const char *str_pin = argv[2]; |
| 47 | |
| 48 | /* grab the [p]<port> portion */ |
| 49 | ulong port_base; |
| 50 | if (*str_pin == 'p') ++str_pin; |
| 51 | switch (*str_pin) { |
| 52 | #ifdef GPIO_PA0 |
| 53 | case 'a': port_base = GPIO_PA0; break; |
| 54 | #endif |
| 55 | #ifdef GPIO_PB0 |
| 56 | case 'b': port_base = GPIO_PB0; break; |
| 57 | #endif |
| 58 | #ifdef GPIO_PC0 |
| 59 | case 'c': port_base = GPIO_PC0; break; |
| 60 | #endif |
| 61 | #ifdef GPIO_PD0 |
| 62 | case 'd': port_base = GPIO_PD0; break; |
| 63 | #endif |
| 64 | #ifdef GPIO_PE0 |
| 65 | case 'e': port_base = GPIO_PE0; break; |
| 66 | #endif |
| 67 | #ifdef GPIO_PF0 |
| 68 | case 'f': port_base = GPIO_PF0; break; |
| 69 | #endif |
| 70 | #ifdef GPIO_PG0 |
| 71 | case 'g': port_base = GPIO_PG0; break; |
| 72 | #endif |
| 73 | #ifdef GPIO_PH0 |
| 74 | case 'h': port_base = GPIO_PH0; break; |
| 75 | #endif |
| 76 | #ifdef GPIO_PI0 |
| 77 | case 'i': port_base = GPIO_PI0; break; |
| 78 | #endif |
| 79 | #ifdef GPIO_PJ |
| 80 | case 'j': port_base = GPIO_PJ0; break; |
| 81 | #endif |
| 82 | default: goto show_usage; |
| 83 | } |
| 84 | |
| 85 | /* grab the <#> portion */ |
| 86 | ulong pin = simple_strtoul(str_pin + 1, NULL, 10); |
| 87 | if (pin > 15) |
| 88 | goto show_usage; |
| 89 | |
| 90 | /* grab the pin before we tweak it */ |
| 91 | ulong gpio = port_base + pin; |
| 92 | gpio_request(gpio, "cmd_gpio"); |
| 93 | |
| 94 | /* finally, let's do it: set direction and exec command */ |
| 95 | if (sub_cmd == GPIO_INPUT) { |
| 96 | gpio_direction_input(gpio); |
| 97 | printf("gpio: pin %lu on port %c set to input\n", pin, *str_pin); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | ulong value; |
| 102 | switch (sub_cmd) { |
| 103 | case GPIO_SET: value = 1; break; |
| 104 | case GPIO_CLEAR: value = 0; break; |
| 105 | case GPIO_TOGGLE: value = !gpio_get_value(gpio); break; |
| 106 | default: goto show_usage; |
| 107 | } |
| 108 | gpio_direction_output(gpio, value); |
| 109 | printf("gpio: pin %lu on port %c (gpio %lu) value is %lu\n", |
| 110 | pin, *str_pin, gpio, value); |
| 111 | |
| 112 | gpio_free(gpio); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | U_BOOT_CMD(gpio, 3, 0, do_gpio, |
| 118 | "set/clear/toggle gpio output pins", |
| 119 | "<set|clear|toggle> <port><pin>\n" |
| 120 | " - set/clear/toggle the specified pin (e.g. PF10)"); |