Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 1 | /* |
Bo Shen | 39b787e | 2013-08-13 14:38:32 +0800 | [diff] [blame] | 2 | * Copyright (C) 2013 Bo Shen <voice.shen@atmel.com> |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009 Jens Scharsig (js_at_ng@scharsoft.de) |
| 5 | * |
| 6 | * Copyright (C) 2005 HP Labs |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <config.h> |
| 12 | #include <common.h> |
Reinhard Meyer | 86592f6 | 2010-11-07 13:26:14 +0100 | [diff] [blame] | 13 | #include <asm/io.h> |
Alexey Brodkin | 1ace402 | 2014-02-26 17:47:58 +0400 | [diff] [blame] | 14 | #include <linux/sizes.h> |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 15 | #include <asm/arch/hardware.h> |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 16 | #include <asm/arch/at91_pio.h> |
Andreas Bießmann | 9ecc922 | 2013-11-29 12:13:43 +0100 | [diff] [blame] | 17 | #include <asm/arch/gpio.h> |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 18 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 19 | static struct at91_port *at91_pio_get_port(unsigned port) |
| 20 | { |
| 21 | switch (port) { |
| 22 | case AT91_PIO_PORTA: |
| 23 | return (struct at91_port *)ATMEL_BASE_PIOA; |
| 24 | case AT91_PIO_PORTB: |
| 25 | return (struct at91_port *)ATMEL_BASE_PIOB; |
| 26 | case AT91_PIO_PORTC: |
| 27 | return (struct at91_port *)ATMEL_BASE_PIOC; |
| 28 | #if (ATMEL_PIO_PORTS > 3) |
| 29 | case AT91_PIO_PORTD: |
| 30 | return (struct at91_port *)ATMEL_BASE_PIOD; |
| 31 | #if (ATMEL_PIO_PORTS > 4) |
| 32 | case AT91_PIO_PORTE: |
| 33 | return (struct at91_port *)ATMEL_BASE_PIOE; |
| 34 | #endif |
| 35 | #endif |
| 36 | default: |
Wu, Josh | 7d82d89 | 2014-05-07 16:50:45 +0800 | [diff] [blame] | 37 | printf("Error: at91_gpio: Fail to get PIO base!\n"); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 38 | return NULL; |
| 39 | } |
| 40 | } |
| 41 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 42 | int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup) |
| 43 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 44 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 45 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 46 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 47 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 48 | mask = 1 << pin; |
| 49 | if (use_pullup) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 50 | writel(1 << pin, &at91_port->puer); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 51 | else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 52 | writel(1 << pin, &at91_port->pudr); |
| 53 | writel(mask, &at91_port->per); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 54 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 55 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * mux the pin to the "GPIO" peripheral role. |
| 61 | */ |
| 62 | int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup) |
| 63 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 64 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 65 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 66 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 67 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 68 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 69 | writel(mask, &at91_port->idr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 70 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 71 | writel(mask, &at91_port->per); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 72 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 73 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * mux the pin to the "A" internal peripheral role. |
| 79 | */ |
| 80 | int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup) |
| 81 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 82 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 83 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 84 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 85 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 86 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 87 | writel(mask, &at91_port->idr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 88 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 89 | #if defined(CPU_HAS_PIO3) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 90 | writel(readl(&at91_port->abcdsr1) & ~mask, |
| 91 | &at91_port->abcdsr1); |
| 92 | writel(readl(&at91_port->abcdsr2) & ~mask, |
| 93 | &at91_port->abcdsr2); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 94 | #else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 95 | writel(mask, &at91_port->asr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 96 | #endif |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 97 | writel(mask, &at91_port->pdr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 98 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 99 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * mux the pin to the "B" internal peripheral role. |
| 105 | */ |
| 106 | int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup) |
| 107 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 108 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 109 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 110 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 111 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 112 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 113 | writel(mask, &at91_port->idr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 114 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 115 | #if defined(CPU_HAS_PIO3) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 116 | writel(readl(&at91_port->abcdsr1) | mask, |
| 117 | &at91_port->abcdsr1); |
| 118 | writel(readl(&at91_port->abcdsr2) & ~mask, |
| 119 | &at91_port->abcdsr2); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 120 | #else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 121 | writel(mask, &at91_port->bsr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 122 | #endif |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 123 | writel(mask, &at91_port->pdr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 124 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 125 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 129 | #if defined(CPU_HAS_PIO3) |
| 130 | /* |
| 131 | * mux the pin to the "C" internal peripheral role. |
| 132 | */ |
| 133 | int at91_set_c_periph(unsigned port, unsigned pin, int use_pullup) |
| 134 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 135 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 136 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 137 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 138 | if (at91_port && (pin < 32)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 139 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 140 | writel(mask, &at91_port->idr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 141 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 142 | writel(readl(&at91_port->abcdsr1) & ~mask, |
| 143 | &at91_port->abcdsr1); |
| 144 | writel(readl(&at91_port->abcdsr2) | mask, |
| 145 | &at91_port->abcdsr2); |
| 146 | writel(mask, &at91_port->pdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 147 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 148 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * mux the pin to the "D" internal peripheral role. |
| 154 | */ |
| 155 | int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup) |
| 156 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 157 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 158 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 159 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 160 | if (at91_port && (pin < 32)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 161 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 162 | writel(mask, &at91_port->idr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 163 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 164 | writel(readl(&at91_port->abcdsr1) | mask, |
| 165 | &at91_port->abcdsr1); |
| 166 | writel(readl(&at91_port->abcdsr2) | mask, |
| 167 | &at91_port->abcdsr2); |
| 168 | writel(mask, &at91_port->pdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 169 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 170 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | #endif |
| 174 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 175 | /* |
| 176 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and |
| 177 | * configure it for an input. |
| 178 | */ |
| 179 | int at91_set_pio_input(unsigned port, u32 pin, int use_pullup) |
| 180 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 181 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 182 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 183 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 184 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 185 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 186 | writel(mask, &at91_port->idr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 187 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 188 | writel(mask, &at91_port->odr); |
| 189 | writel(mask, &at91_port->per); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 190 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 191 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), |
| 197 | * and configure it for an output. |
| 198 | */ |
| 199 | int at91_set_pio_output(unsigned port, u32 pin, int value) |
| 200 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 201 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 202 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 203 | |
Wu, Josh | 7d82d89 | 2014-05-07 16:50:45 +0800 | [diff] [blame] | 204 | if (at91_port && (port < ATMEL_PIO_PORTS) && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 205 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 206 | writel(mask, &at91_port->idr); |
| 207 | writel(mask, &at91_port->pudr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 208 | if (value) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 209 | writel(mask, &at91_port->sodr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 210 | else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 211 | writel(mask, &at91_port->codr); |
| 212 | writel(mask, &at91_port->oer); |
| 213 | writel(mask, &at91_port->per); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 214 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 215 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * enable/disable the glitch filter. mostly used with IRQ handling. |
| 221 | */ |
| 222 | int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on) |
| 223 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 224 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 225 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 226 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 227 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 228 | mask = 1 << pin; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 229 | if (is_on) { |
| 230 | #if defined(CPU_HAS_PIO3) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 231 | writel(mask, &at91_port->ifscdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 232 | #endif |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 233 | writel(mask, &at91_port->ifer); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 234 | } else { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 235 | writel(mask, &at91_port->ifdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 236 | } |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 237 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 238 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 242 | #if defined(CPU_HAS_PIO3) |
| 243 | /* |
| 244 | * enable/disable the debounce filter. |
| 245 | */ |
| 246 | int at91_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div) |
| 247 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 248 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 249 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 250 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 251 | if (at91_port && (pin < 32)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 252 | mask = 1 << pin; |
| 253 | if (is_on) { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 254 | writel(mask, &at91_port->ifscer); |
| 255 | writel(div & PIO_SCDR_DIV, &at91_port->scdr); |
| 256 | writel(mask, &at91_port->ifer); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 257 | } else { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 258 | writel(mask, &at91_port->ifdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 261 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * enable/disable the pull-down. |
| 267 | * If pull-up already enabled while calling the function, we disable it. |
| 268 | */ |
| 269 | int at91_set_pio_pulldown(unsigned port, unsigned pin, int is_on) |
| 270 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 271 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 272 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 273 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 274 | if (at91_port && (pin < 32)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 275 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 276 | writel(mask, &at91_port->pudr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 277 | if (is_on) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 278 | writel(mask, &at91_port->ppder); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 279 | else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 280 | writel(mask, &at91_port->ppddr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 281 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 282 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * disable Schmitt trigger |
| 288 | */ |
| 289 | int at91_set_pio_disable_schmitt_trig(unsigned port, unsigned pin) |
| 290 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 291 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 292 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 293 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 294 | if (at91_port && (pin < 32)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 295 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 296 | writel(readl(&at91_port->schmitt) | mask, |
| 297 | &at91_port->schmitt); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 298 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 299 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | #endif |
| 303 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 304 | /* |
| 305 | * enable/disable the multi-driver. This is only valid for output and |
| 306 | * allows the output pin to run as an open collector output. |
| 307 | */ |
| 308 | int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on) |
| 309 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 310 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 311 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 312 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 313 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 314 | mask = 1 << pin; |
| 315 | if (is_on) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 316 | writel(mask, &at91_port->mder); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 317 | else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 318 | writel(mask, &at91_port->mddr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 319 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 320 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * assuming the pin is muxed as a gpio output, set its value. |
| 326 | */ |
| 327 | int at91_set_pio_value(unsigned port, unsigned pin, int value) |
| 328 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 329 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 330 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 331 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 332 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 333 | mask = 1 << pin; |
| 334 | if (value) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 335 | writel(mask, &at91_port->sodr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 336 | else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 337 | writel(mask, &at91_port->codr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 338 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 339 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * read the pin's value (works even if it's not muxed as a gpio). |
| 345 | */ |
| 346 | int at91_get_pio_value(unsigned port, unsigned pin) |
| 347 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 348 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 349 | u32 pdsr = 0, mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 350 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 351 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 352 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 353 | pdsr = readl(&at91_port->pdsr) & mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 354 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 355 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 356 | return pdsr != 0; |
| 357 | } |
Bo Shen | 6edaea8 | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 358 | |
| 359 | /* Common GPIO API */ |
| 360 | |
Bo Shen | 6edaea8 | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 361 | int gpio_request(unsigned gpio, const char *label) |
| 362 | { |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | int gpio_free(unsigned gpio) |
| 367 | { |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | int gpio_direction_input(unsigned gpio) |
| 372 | { |
| 373 | at91_set_pio_input(at91_gpio_to_port(gpio), |
| 374 | at91_gpio_to_pin(gpio), 0); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | int gpio_direction_output(unsigned gpio, int value) |
| 379 | { |
| 380 | at91_set_pio_output(at91_gpio_to_port(gpio), |
| 381 | at91_gpio_to_pin(gpio), value); |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | int gpio_get_value(unsigned gpio) |
| 386 | { |
| 387 | return at91_get_pio_value(at91_gpio_to_port(gpio), |
| 388 | at91_gpio_to_pin(gpio)); |
| 389 | } |
| 390 | |
| 391 | int gpio_set_value(unsigned gpio, int value) |
| 392 | { |
| 393 | at91_set_pio_value(at91_gpio_to_port(gpio), |
| 394 | at91_gpio_to_pin(gpio), value); |
| 395 | |
| 396 | return 0; |
| 397 | } |