Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Michal Simek <monstr@monstr.eu> |
| 3 | * Copyright (C) 2009 PetaLogix |
| 4 | * |
| 5 | * This file is subject to the terms and conditions of the GNU General Public |
| 6 | * License. See the file "COPYING" in the main directory of this archive |
| 7 | * for more details. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/of_platform.h> |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 12 | |
| 13 | /* Trigger specific functions */ |
| 14 | #ifdef CONFIG_GPIOLIB |
| 15 | |
| 16 | #include <linux/of_gpio.h> |
| 17 | |
| 18 | static int handle; /* reset pin handle */ |
Michal Simek | 67bf876 | 2009-10-29 10:12:59 +0100 | [diff] [blame] | 19 | static unsigned int reset_val; |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 20 | |
Rob Herring | 45df561 | 2018-06-19 15:36:20 -0600 | [diff] [blame] | 21 | static int of_platform_reset_gpio_probe(void) |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 22 | { |
| 23 | int ret; |
Grant Likely | fe9f684 | 2011-12-12 09:25:56 -0700 | [diff] [blame] | 24 | handle = of_get_named_gpio(of_find_node_by_path("/"), |
| 25 | "hard-reset-gpios", 0); |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 26 | |
| 27 | if (!gpio_is_valid(handle)) { |
Michal Simek | aaa5241 | 2012-10-04 14:24:58 +0200 | [diff] [blame] | 28 | pr_info("Skipping unavailable RESET gpio %d (%s)\n", |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 29 | handle, "reset"); |
Rob Herring | 45df561 | 2018-06-19 15:36:20 -0600 | [diff] [blame] | 30 | return -ENODEV; |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | ret = gpio_request(handle, "reset"); |
| 34 | if (ret < 0) { |
Michal Simek | aaa5241 | 2012-10-04 14:24:58 +0200 | [diff] [blame] | 35 | pr_info("GPIO pin is already allocated\n"); |
Rob Herring | 45df561 | 2018-06-19 15:36:20 -0600 | [diff] [blame] | 36 | return ret; |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /* get current setup value */ |
Michal Simek | 67bf876 | 2009-10-29 10:12:59 +0100 | [diff] [blame] | 40 | reset_val = gpio_get_value(handle); |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 41 | /* FIXME maybe worth to perform any action */ |
Michal Simek | 67bf876 | 2009-10-29 10:12:59 +0100 | [diff] [blame] | 42 | pr_debug("Reset: Gpio output state: 0x%x\n", reset_val); |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 43 | |
| 44 | /* Setup GPIO as output */ |
| 45 | ret = gpio_direction_output(handle, 0); |
| 46 | if (ret < 0) |
| 47 | goto err; |
| 48 | |
| 49 | /* Setup output direction */ |
| 50 | gpio_set_value(handle, 0); |
| 51 | |
Michal Simek | aaa5241 | 2012-10-04 14:24:58 +0200 | [diff] [blame] | 52 | pr_info("RESET: Registered gpio device: %d, current val: %d\n", |
Michal Simek | 67bf876 | 2009-10-29 10:12:59 +0100 | [diff] [blame] | 53 | handle, reset_val); |
Rob Herring | 45df561 | 2018-06-19 15:36:20 -0600 | [diff] [blame] | 54 | return 0; |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 55 | err: |
| 56 | gpio_free(handle); |
Rob Herring | 45df561 | 2018-06-19 15:36:20 -0600 | [diff] [blame] | 57 | return ret; |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 58 | } |
Rob Herring | 45df561 | 2018-06-19 15:36:20 -0600 | [diff] [blame] | 59 | device_initcall(of_platform_reset_gpio_probe); |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 60 | |
| 61 | |
| 62 | static void gpio_system_reset(void) |
| 63 | { |
Stephan Linz | 191d5ec | 2012-06-20 22:36:37 +0200 | [diff] [blame] | 64 | if (gpio_is_valid(handle)) |
| 65 | gpio_set_value(handle, 1 - reset_val); |
| 66 | else |
| 67 | pr_notice("Reset GPIO unavailable - halting!\n"); |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 68 | } |
| 69 | #else |
Michal Simek | 54ea21f | 2012-05-28 09:56:40 +0200 | [diff] [blame] | 70 | static void gpio_system_reset(void) |
| 71 | { |
| 72 | pr_notice("No reset GPIO present - halting!\n"); |
| 73 | } |
| 74 | |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 75 | void of_platform_reset_gpio_probe(void) |
| 76 | { |
| 77 | return; |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | void machine_restart(char *cmd) |
| 82 | { |
Michal Simek | aaa5241 | 2012-10-04 14:24:58 +0200 | [diff] [blame] | 83 | pr_notice("Machine restart...\n"); |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 84 | gpio_system_reset(); |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 85 | while (1) |
| 86 | ; |
| 87 | } |
| 88 | |
| 89 | void machine_shutdown(void) |
| 90 | { |
Michal Simek | aaa5241 | 2012-10-04 14:24:58 +0200 | [diff] [blame] | 91 | pr_notice("Machine shutdown...\n"); |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 92 | while (1) |
| 93 | ; |
| 94 | } |
| 95 | |
| 96 | void machine_halt(void) |
| 97 | { |
Michal Simek | aaa5241 | 2012-10-04 14:24:58 +0200 | [diff] [blame] | 98 | pr_notice("Machine halt...\n"); |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 99 | while (1) |
| 100 | ; |
| 101 | } |
| 102 | |
| 103 | void machine_power_off(void) |
| 104 | { |
Michal Simek | aaa5241 | 2012-10-04 14:24:58 +0200 | [diff] [blame] | 105 | pr_notice("Machine power off...\n"); |
Michal Simek | 42a2478 | 2009-10-02 12:48:47 +0200 | [diff] [blame] | 106 | while (1) |
| 107 | ; |
| 108 | } |