Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dieter Kiermaier | e740eb8 | 2009-06-29 14:45:08 +0200 | [diff] [blame] | 2 | /* |
| 3 | * arch/arm/plat-orion/gpio.c |
| 4 | * |
| 5 | * Marvell Orion SoC GPIO handling. |
Dieter Kiermaier | e740eb8 | 2009-06-29 14:45:08 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Based on (mostly copied from) plat-orion based Linux 2.6 kernel driver. |
| 10 | * Removed orion_gpiochip struct and kernel level irq handling. |
| 11 | * |
| 12 | * Dieter Kiermaier dk-arm-linux@gmx.de |
| 13 | */ |
| 14 | |
Masahiro Yamada | 14fecf9 | 2017-11-22 02:38:11 +0900 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Lei Wen | 298ae91 | 2011-10-18 20:11:42 +0530 | [diff] [blame] | 16 | #include <asm/io.h> |
Stefan Roese | c243784 | 2014-10-22 12:13:06 +0200 | [diff] [blame] | 17 | #include <asm/arch/soc.h> |
Dieter Kiermaier | e740eb8 | 2009-06-29 14:45:08 +0200 | [diff] [blame] | 18 | #include <asm/arch/gpio.h> |
| 19 | |
| 20 | static unsigned long gpio_valid_input[BITS_TO_LONGS(GPIO_MAX)]; |
| 21 | static unsigned long gpio_valid_output[BITS_TO_LONGS(GPIO_MAX)]; |
| 22 | |
| 23 | void __set_direction(unsigned pin, int input) |
| 24 | { |
| 25 | u32 u; |
| 26 | |
| 27 | u = readl(GPIO_IO_CONF(pin)); |
| 28 | if (input) |
| 29 | u |= 1 << (pin & 31); |
| 30 | else |
| 31 | u &= ~(1 << (pin & 31)); |
| 32 | writel(u, GPIO_IO_CONF(pin)); |
| 33 | |
| 34 | u = readl(GPIO_IO_CONF(pin)); |
| 35 | } |
| 36 | |
Jeroen Hofstee | 15da974 | 2014-10-08 22:57:37 +0200 | [diff] [blame] | 37 | static void __set_level(unsigned pin, int high) |
Dieter Kiermaier | e740eb8 | 2009-06-29 14:45:08 +0200 | [diff] [blame] | 38 | { |
| 39 | u32 u; |
| 40 | |
| 41 | u = readl(GPIO_OUT(pin)); |
| 42 | if (high) |
| 43 | u |= 1 << (pin & 31); |
| 44 | else |
| 45 | u &= ~(1 << (pin & 31)); |
| 46 | writel(u, GPIO_OUT(pin)); |
| 47 | } |
| 48 | |
Jeroen Hofstee | 15da974 | 2014-10-08 22:57:37 +0200 | [diff] [blame] | 49 | static void __set_blinking(unsigned pin, int blink) |
Dieter Kiermaier | e740eb8 | 2009-06-29 14:45:08 +0200 | [diff] [blame] | 50 | { |
| 51 | u32 u; |
| 52 | |
| 53 | u = readl(GPIO_BLINK_EN(pin)); |
| 54 | if (blink) |
| 55 | u |= 1 << (pin & 31); |
| 56 | else |
| 57 | u &= ~(1 << (pin & 31)); |
| 58 | writel(u, GPIO_BLINK_EN(pin)); |
| 59 | } |
| 60 | |
| 61 | int kw_gpio_is_valid(unsigned pin, int mode) |
| 62 | { |
| 63 | if (pin < GPIO_MAX) { |
| 64 | if ((mode & GPIO_INPUT_OK) && !test_bit(pin, gpio_valid_input)) |
| 65 | goto err_out; |
| 66 | |
| 67 | if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, gpio_valid_output)) |
| 68 | goto err_out; |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | err_out: |
| 73 | printf("%s: invalid GPIO %d\n", __func__, pin); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | void kw_gpio_set_valid(unsigned pin, int mode) |
| 78 | { |
| 79 | if (mode == 1) |
| 80 | mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK; |
| 81 | if (mode & GPIO_INPUT_OK) |
| 82 | __set_bit(pin, gpio_valid_input); |
| 83 | else |
| 84 | __clear_bit(pin, gpio_valid_input); |
| 85 | if (mode & GPIO_OUTPUT_OK) |
| 86 | __set_bit(pin, gpio_valid_output); |
| 87 | else |
| 88 | __clear_bit(pin, gpio_valid_output); |
| 89 | } |
| 90 | /* |
| 91 | * GENERIC_GPIO primitives. |
| 92 | */ |
| 93 | int kw_gpio_direction_input(unsigned pin) |
| 94 | { |
Holger Brunck | 8dd156e | 2011-08-21 23:21:06 +0000 | [diff] [blame] | 95 | if (kw_gpio_is_valid(pin, GPIO_INPUT_OK) != 0) |
Dieter Kiermaier | e740eb8 | 2009-06-29 14:45:08 +0200 | [diff] [blame] | 96 | return 1; |
| 97 | |
| 98 | /* Configure GPIO direction. */ |
| 99 | __set_direction(pin, 1); |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | int kw_gpio_direction_output(unsigned pin, int value) |
| 105 | { |
| 106 | if (kw_gpio_is_valid(pin, GPIO_OUTPUT_OK) != 0) |
| 107 | { |
| 108 | printf("%s: invalid GPIO %d\n", __func__, pin); |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | __set_blinking(pin, 0); |
| 113 | |
| 114 | /* Configure GPIO output value. */ |
| 115 | __set_level(pin, value); |
| 116 | |
| 117 | /* Configure GPIO direction. */ |
| 118 | __set_direction(pin, 0); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | int kw_gpio_get_value(unsigned pin) |
| 124 | { |
| 125 | int val; |
| 126 | |
| 127 | if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31))) |
| 128 | val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin)); |
| 129 | else |
| 130 | val = readl(GPIO_OUT(pin)); |
| 131 | |
| 132 | return (val >> (pin & 31)) & 1; |
| 133 | } |
| 134 | |
| 135 | void kw_gpio_set_value(unsigned pin, int value) |
| 136 | { |
| 137 | /* Configure GPIO output value. */ |
| 138 | __set_level(pin, value); |
| 139 | } |
| 140 | |
| 141 | void kw_gpio_set_blink(unsigned pin, int blink) |
| 142 | { |
| 143 | /* Set output value to zero. */ |
| 144 | __set_level(pin, 0); |
| 145 | |
| 146 | /* Set blinking. */ |
| 147 | __set_blinking(pin, blink); |
| 148 | } |