blob: bcf8cf6fe4126d6608a560b39e2b92ade8b89a7a [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Robin Getz96f10502009-09-24 14:11:24 +00002 * GPIO Abstraction Layer
Bryan Wu1394f032007-05-06 14:50:22 -07003 *
steven miao05bbec32010-09-17 03:03:17 +00004 * Copyright 2006-2010 Analog Devices Inc.
Bryan Wu1394f032007-05-06 14:50:22 -07005 *
Robin Getz96f10502009-09-24 14:11:24 +00006 * Licensed under the GPL-2 or later
Bryan Wu1394f032007-05-06 14:50:22 -07007 */
8
Mike Frysinger168f1212007-10-11 00:22:35 +08009#include <linux/delay.h>
Bryan Wu1394f032007-05-06 14:50:22 -070010#include <linux/module.h>
11#include <linux/err.h>
Mike Frysinger1545a112007-12-24 16:54:48 +080012#include <linux/proc_fs.h>
Alexey Dobriyan6362ec22011-05-14 19:48:46 +030013#include <linux/seq_file.h>
Bryan Wu1394f032007-05-06 14:50:22 -070014#include <asm/blackfin.h>
15#include <asm/gpio.h>
Michael Hennerichc58c2142007-10-04 00:35:05 +080016#include <asm/portmux.h>
Bryan Wu1394f032007-05-06 14:50:22 -070017#include <linux/irq.h>
Mike Frysinger6327a572011-04-15 03:06:59 -040018#include <asm/irq_handler.h>
Bryan Wu1394f032007-05-06 14:50:22 -070019
Michael Hennerich2b393312007-10-10 16:58:49 +080020#if ANOMALY_05000311 || ANOMALY_05000323
21enum {
22 AWA_data = SYSCR,
23 AWA_data_clear = SYSCR,
24 AWA_data_set = SYSCR,
25 AWA_toggle = SYSCR,
Graf Yang6ed83942008-04-24 04:43:14 +080026 AWA_maska = BFIN_UART_SCR,
27 AWA_maska_clear = BFIN_UART_SCR,
28 AWA_maska_set = BFIN_UART_SCR,
29 AWA_maska_toggle = BFIN_UART_SCR,
30 AWA_maskb = BFIN_UART_GCTL,
31 AWA_maskb_clear = BFIN_UART_GCTL,
32 AWA_maskb_set = BFIN_UART_GCTL,
33 AWA_maskb_toggle = BFIN_UART_GCTL,
Michael Hennerich2b393312007-10-10 16:58:49 +080034 AWA_dir = SPORT1_STAT,
35 AWA_polar = SPORT1_STAT,
36 AWA_edge = SPORT1_STAT,
37 AWA_both = SPORT1_STAT,
38#if ANOMALY_05000311
39 AWA_inen = TIMER_ENABLE,
40#elif ANOMALY_05000323
41 AWA_inen = DMA1_1_CONFIG,
42#endif
43};
44 /* Anomaly Workaround */
45#define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
46#else
47#define AWA_DUMMY_READ(...) do { } while (0)
48#endif
49
Mike Frysingerf5563092009-02-04 16:49:45 +080050static struct gpio_port_t * const gpio_array[] = {
Michael Hennerichdc26aec2008-11-18 17:48:22 +080051#if defined(BF533_FAMILY) || defined(BF538_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -070052 (struct gpio_port_t *) FIO_FLAG_D,
Mike Frysinger269647d2009-03-28 20:32:57 +080053#elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
Bryan Wu1394f032007-05-06 14:50:22 -070054 (struct gpio_port_t *) PORTFIO,
55 (struct gpio_port_t *) PORTGIO,
56 (struct gpio_port_t *) PORTHIO,
Mike Frysingerf5563092009-02-04 16:49:45 +080057#elif defined(BF561_FAMILY)
58 (struct gpio_port_t *) FIO0_FLAG_D,
59 (struct gpio_port_t *) FIO1_FLAG_D,
60 (struct gpio_port_t *) FIO2_FLAG_D,
Mike Frysinger269647d2009-03-28 20:32:57 +080061#elif defined(CONFIG_BF54x)
Mike Frysingerf5563092009-02-04 16:49:45 +080062 (struct gpio_port_t *)PORTA_FER,
63 (struct gpio_port_t *)PORTB_FER,
64 (struct gpio_port_t *)PORTC_FER,
65 (struct gpio_port_t *)PORTD_FER,
66 (struct gpio_port_t *)PORTE_FER,
67 (struct gpio_port_t *)PORTF_FER,
68 (struct gpio_port_t *)PORTG_FER,
69 (struct gpio_port_t *)PORTH_FER,
70 (struct gpio_port_t *)PORTI_FER,
71 (struct gpio_port_t *)PORTJ_FER,
72#else
73# error no gpio arrays defined
74#endif
Bryan Wu1394f032007-05-06 14:50:22 -070075};
76
Mike Frysinger269647d2009-03-28 20:32:57 +080077#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
Mike Frysingerf5563092009-02-04 16:49:45 +080078static unsigned short * const port_fer[] = {
Bryan Wu1394f032007-05-06 14:50:22 -070079 (unsigned short *) PORTF_FER,
80 (unsigned short *) PORTG_FER,
81 (unsigned short *) PORTH_FER,
82};
Bryan Wu1394f032007-05-06 14:50:22 -070083
Mike Frysingerf5563092009-02-04 16:49:45 +080084# if !defined(BF537_FAMILY)
85static unsigned short * const port_mux[] = {
Michael Hennerich59003142007-10-21 16:54:27 +080086 (unsigned short *) PORTF_MUX,
87 (unsigned short *) PORTG_MUX,
88 (unsigned short *) PORTH_MUX,
89};
90
91static const
Graf Yang0ce5eaf2009-02-04 16:49:45 +080092u8 pmux_offset[][16] = {
Mike Frysinger269647d2009-03-28 20:32:57 +080093# if defined(CONFIG_BF52x)
Graf Yang0ce5eaf2009-02-04 16:49:45 +080094 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
95 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
96 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
Mike Frysinger269647d2009-03-28 20:32:57 +080097# elif defined(CONFIG_BF51x)
Graf Yang0ce5eaf2009-02-04 16:49:45 +080098 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
99 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
100 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
101# endif
102};
Mike Frysingerf5563092009-02-04 16:49:45 +0800103# endif
Graf Yang0ce5eaf2009-02-04 16:49:45 +0800104
Michael Hennerich621dd242009-09-28 12:23:41 +0000105#elif defined(BF538_FAMILY)
106static unsigned short * const port_fer[] = {
107 (unsigned short *) PORTCIO_FER,
108 (unsigned short *) PORTDIO_FER,
109 (unsigned short *) PORTEIO_FER,
110};
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800111#endif
112
Mike Frysinger812ae982010-07-05 08:40:41 +0000113#define RESOURCE_LABEL_SIZE 16
Michael Hennerich8c613622007-08-03 17:48:09 +0800114
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800115static struct str_ident {
Michael Hennerich8c613622007-08-03 17:48:09 +0800116 char name[RESOURCE_LABEL_SIZE];
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800117} str_ident[MAX_RESOURCES];
Bryan Wu1394f032007-05-06 14:50:22 -0700118
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800119#if defined(CONFIG_PM)
Bryan Wu397861c2008-11-18 17:48:22 +0800120static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
Bryan Wu1394f032007-05-06 14:50:22 -0700121#endif
122
Mike Frysinger74c04502008-10-08 16:13:17 +0800123static void gpio_error(unsigned gpio)
Michael Hennerichacbcd262008-01-22 18:36:20 +0800124{
125 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
126}
127
Michael Hennerichc58c2142007-10-04 00:35:05 +0800128static void set_label(unsigned short ident, const char *label)
129{
Michael Henneriche9fae182008-10-13 11:35:22 +0800130 if (label) {
Michael Hennerich8c613622007-08-03 17:48:09 +0800131 strncpy(str_ident[ident].name, label,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800132 RESOURCE_LABEL_SIZE);
Michael Hennerich8c613622007-08-03 17:48:09 +0800133 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800134 }
135}
136
137static char *get_label(unsigned short ident)
138{
Michael Hennerich8c613622007-08-03 17:48:09 +0800139 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
Michael Hennerichc58c2142007-10-04 00:35:05 +0800140}
141
142static int cmp_label(unsigned short ident, const char *label)
143{
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800144 if (label == NULL) {
145 dump_stack();
146 printk(KERN_ERR "Please provide none-null label\n");
147 }
148
Michael Henneriche9fae182008-10-13 11:35:22 +0800149 if (label)
Mike Frysinger1f7d3732008-10-28 15:47:11 +0800150 return strcmp(str_ident[ident].name, label);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800151 else
152 return -EINVAL;
153}
154
Mike Frysinger332824b2010-06-02 04:22:01 +0000155#define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
156#define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
157#define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
158#define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
159#define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
160
161DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
Michael Hennerich382dbe52010-07-13 08:26:10 +0000162DECLARE_RESERVED_MAP(peri, DIV_ROUND_UP(MAX_RESOURCES, GPIO_BANKSIZE));
Mike Frysinger332824b2010-06-02 04:22:01 +0000163DECLARE_RESERVED_MAP(gpio_irq, GPIO_BANK_NUM);
164
165inline int check_gpio(unsigned gpio)
166{
167#if defined(CONFIG_BF54x)
168 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
169 || gpio == GPIO_PH14 || gpio == GPIO_PH15
170 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
171 return -EINVAL;
172#endif
173 if (gpio >= MAX_BLACKFIN_GPIOS)
174 return -EINVAL;
175 return 0;
176}
177
Mike Frysingera2d03a12008-10-28 15:53:37 +0800178static void port_setup(unsigned gpio, unsigned short usage)
179{
Michael Hennerich621dd242009-09-28 12:23:41 +0000180#if defined(BF538_FAMILY)
181 /*
182 * BF538/9 Port C,D and E are special.
183 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
184 * Regular PORT F GPIOs are handled here, CDE are exclusively
185 * managed by GPIOLIB
186 */
187
188 if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
189 return;
190
191 gpio -= MAX_BLACKFIN_GPIOS;
192
193 if (usage == GPIO_USAGE)
194 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
195 else
196 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
197 SSYNC();
198 return;
199#endif
200
Mike Frysingera2d03a12008-10-28 15:53:37 +0800201 if (check_gpio(gpio))
202 return;
203
Mike Frysinger269647d2009-03-28 20:32:57 +0800204#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
Mike Frysingera2d03a12008-10-28 15:53:37 +0800205 if (usage == GPIO_USAGE)
206 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
207 else
208 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
209 SSYNC();
Mike Frysinger269647d2009-03-28 20:32:57 +0800210#elif defined(CONFIG_BF54x)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800211 if (usage == GPIO_USAGE)
212 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
213 else
214 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
215 SSYNC();
Bryan Wu1394f032007-05-06 14:50:22 -0700216#endif
Mike Frysingera2d03a12008-10-28 15:53:37 +0800217}
Bryan Wu1394f032007-05-06 14:50:22 -0700218
Michael Hennerichc58c2142007-10-04 00:35:05 +0800219#ifdef BF537_FAMILY
steven miao05bbec32010-09-17 03:03:17 +0000220static const s8 port_mux[] = {
221 [GPIO_PF0] = 3,
222 [GPIO_PF1] = 3,
223 [GPIO_PF2] = 4,
224 [GPIO_PF3] = 4,
225 [GPIO_PF4] = 5,
226 [GPIO_PF5] = 6,
227 [GPIO_PF6] = 7,
228 [GPIO_PF7] = 8,
229 [GPIO_PF8 ... GPIO_PF15] = -1,
230 [GPIO_PG0 ... GPIO_PG7] = -1,
231 [GPIO_PG8] = 9,
232 [GPIO_PG9] = 9,
233 [GPIO_PG10] = 10,
234 [GPIO_PG11] = 10,
235 [GPIO_PG12] = 10,
236 [GPIO_PG13] = 11,
237 [GPIO_PG14] = 11,
238 [GPIO_PG15] = 11,
239 [GPIO_PH0 ... GPIO_PH15] = -1,
240 [PORT_PJ0 ... PORT_PJ3] = -1,
241 [PORT_PJ4] = 1,
242 [PORT_PJ5] = 1,
243 [PORT_PJ6 ... PORT_PJ9] = -1,
244 [PORT_PJ10] = 0,
245 [PORT_PJ11] = 0,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800246};
247
steven miao05bbec32010-09-17 03:03:17 +0000248static int portmux_group_check(unsigned short per)
249{
250 u16 ident = P_IDENT(per);
251 u16 function = P_FUNCT2MUX(per);
252 s8 offset = port_mux[ident];
253 u16 m, pmux, pfunc;
254
255 if (offset < 0)
256 return 0;
257
258 pmux = bfin_read_PORT_MUX();
259 for (m = 0; m < ARRAY_SIZE(port_mux); ++m) {
260 if (m == ident)
261 continue;
262 if (port_mux[m] != offset)
263 continue;
264 if (!is_reserved(peri, m, 1))
265 continue;
266
267 if (offset == 1)
268 pfunc = (pmux >> offset) & 3;
269 else
270 pfunc = (pmux >> offset) & 1;
271 if (pfunc != function) {
272 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
273 ident, function, m, pfunc);
274 return -EINVAL;
275 }
276 }
277
278 return 0;
279}
280
Mike Frysingerf5563092009-02-04 16:49:45 +0800281static void portmux_setup(unsigned short per)
Michael Hennerichc58c2142007-10-04 00:35:05 +0800282{
steven miao05bbec32010-09-17 03:03:17 +0000283 u16 ident = P_IDENT(per);
Mike Frysingerf5563092009-02-04 16:49:45 +0800284 u16 function = P_FUNCT2MUX(per);
steven miao05bbec32010-09-17 03:03:17 +0000285 s8 offset = port_mux[ident];
286 u16 pmux;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800287
steven miao05bbec32010-09-17 03:03:17 +0000288 if (offset == -1)
289 return;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800290
steven miao05bbec32010-09-17 03:03:17 +0000291 pmux = bfin_read_PORT_MUX();
292 if (offset != 1)
293 pmux &= ~(1 << offset);
294 else
295 pmux &= ~(3 << 1);
296 pmux |= (function << offset);
297 bfin_write_PORT_MUX(pmux);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800298}
Mike Frysinger269647d2009-03-28 20:32:57 +0800299#elif defined(CONFIG_BF54x)
Mike Frysingerf5563092009-02-04 16:49:45 +0800300inline void portmux_setup(unsigned short per)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800301{
Mike Frysingerf5563092009-02-04 16:49:45 +0800302 u16 ident = P_IDENT(per);
303 u16 function = P_FUNCT2MUX(per);
steven miao05bbec32010-09-17 03:03:17 +0000304 u32 pmux;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800305
Mike Frysingerf5563092009-02-04 16:49:45 +0800306 pmux = gpio_array[gpio_bank(ident)]->port_mux;
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800307
Mike Frysingerf5563092009-02-04 16:49:45 +0800308 pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
309 pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800310
Mike Frysingerf5563092009-02-04 16:49:45 +0800311 gpio_array[gpio_bank(ident)]->port_mux = pmux;
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800312}
313
Mike Frysingerf5563092009-02-04 16:49:45 +0800314inline u16 get_portmux(unsigned short per)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800315{
Mike Frysingerf5563092009-02-04 16:49:45 +0800316 u16 ident = P_IDENT(per);
steven miao05bbec32010-09-17 03:03:17 +0000317 u32 pmux = gpio_array[gpio_bank(ident)]->port_mux;
Mike Frysingerf5563092009-02-04 16:49:45 +0800318 return (pmux >> (2 * gpio_sub_n(ident)) & 0x3);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800319}
steven miao05bbec32010-09-17 03:03:17 +0000320static int portmux_group_check(unsigned short per)
321{
322 return 0;
323}
Mike Frysinger269647d2009-03-28 20:32:57 +0800324#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
steven miao05bbec32010-09-17 03:03:17 +0000325static int portmux_group_check(unsigned short per)
326{
327 u16 ident = P_IDENT(per);
328 u16 function = P_FUNCT2MUX(per);
329 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
330 u16 pin, gpiopin, pfunc;
331
332 for (pin = 0; pin < GPIO_BANKSIZE; ++pin) {
333 if (offset != pmux_offset[gpio_bank(ident)][pin])
334 continue;
335
336 gpiopin = gpio_bank(ident) * GPIO_BANKSIZE + pin;
337 if (gpiopin == ident)
338 continue;
339 if (!is_reserved(peri, gpiopin, 1))
340 continue;
341
342 pfunc = *port_mux[gpio_bank(ident)];
343 pfunc = (pfunc >> offset) & 3;
344 if (pfunc != function) {
345 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
346 ident, function, gpiopin, pfunc);
347 return -EINVAL;
348 }
349 }
350
351 return 0;
352}
353
Mike Frysingerf5563092009-02-04 16:49:45 +0800354inline void portmux_setup(unsigned short per)
Michael Hennerich59003142007-10-21 16:54:27 +0800355{
steven miao05bbec32010-09-17 03:03:17 +0000356 u16 ident = P_IDENT(per);
357 u16 function = P_FUNCT2MUX(per);
Michael Hennerich59003142007-10-21 16:54:27 +0800358 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
steven miao05bbec32010-09-17 03:03:17 +0000359 u16 pmux;
Michael Hennerich59003142007-10-21 16:54:27 +0800360
361 pmux = *port_mux[gpio_bank(ident)];
steven miao05bbec32010-09-17 03:03:17 +0000362 if (((pmux >> offset) & 3) == function)
363 return;
Michael Hennerich59003142007-10-21 16:54:27 +0800364 pmux &= ~(3 << offset);
365 pmux |= (function & 3) << offset;
366 *port_mux[gpio_bank(ident)] = pmux;
367 SSYNC();
368}
Michael Hennerichc58c2142007-10-04 00:35:05 +0800369#else
370# define portmux_setup(...) do { } while (0)
steven miao05bbec32010-09-17 03:03:17 +0000371static int portmux_group_check(unsigned short per)
372{
373 return 0;
374}
Michael Hennerichc58c2142007-10-04 00:35:05 +0800375#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700376
Mike Frysinger269647d2009-03-28 20:32:57 +0800377#ifndef CONFIG_BF54x
Bryan Wu1394f032007-05-06 14:50:22 -0700378/***********************************************************
379*
380* FUNCTIONS: Blackfin General Purpose Ports Access Functions
381*
382* INPUTS/OUTPUTS:
383* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
384*
385*
386* DESCRIPTION: These functions abstract direct register access
387* to Blackfin processor General Purpose
388* Ports Regsiters
389*
390* CAUTION: These functions do not belong to the GPIO Driver API
391*************************************************************
392* MODIFICATION HISTORY :
393**************************************************************/
394
395/* Set a specific bit */
396
397#define SET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800398void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700399{ \
400 unsigned long flags; \
David Howells3b139cd2010-10-07 14:08:52 +0100401 flags = hard_local_irq_save(); \
Bryan Wu1394f032007-05-06 14:50:22 -0700402 if (arg) \
Mike Frysingerf5563092009-02-04 16:49:45 +0800403 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
Bryan Wu1394f032007-05-06 14:50:22 -0700404 else \
Mike Frysingerf5563092009-02-04 16:49:45 +0800405 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800406 AWA_DUMMY_READ(name); \
David Howells3b139cd2010-10-07 14:08:52 +0100407 hard_local_irq_restore(flags); \
Bryan Wu1394f032007-05-06 14:50:22 -0700408} \
409EXPORT_SYMBOL(set_gpio_ ## name);
410
Mike Frysingerf5563092009-02-04 16:49:45 +0800411SET_GPIO(dir) /* set_gpio_dir() */
412SET_GPIO(inen) /* set_gpio_inen() */
413SET_GPIO(polar) /* set_gpio_polar() */
414SET_GPIO(edge) /* set_gpio_edge() */
415SET_GPIO(both) /* set_gpio_both() */
Bryan Wu1394f032007-05-06 14:50:22 -0700416
417
Michael Hennerich2b393312007-10-10 16:58:49 +0800418#define SET_GPIO_SC(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800419void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800420{ \
421 unsigned long flags; \
Mike Frysingerf5563092009-02-04 16:49:45 +0800422 if (ANOMALY_05000311 || ANOMALY_05000323) \
David Howells3b139cd2010-10-07 14:08:52 +0100423 flags = hard_local_irq_save(); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800424 if (arg) \
Mike Frysingerf5563092009-02-04 16:49:45 +0800425 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800426 else \
Mike Frysingerf5563092009-02-04 16:49:45 +0800427 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
428 if (ANOMALY_05000311 || ANOMALY_05000323) { \
429 AWA_DUMMY_READ(name); \
David Howells3b139cd2010-10-07 14:08:52 +0100430 hard_local_irq_restore(flags); \
Mike Frysingerf5563092009-02-04 16:49:45 +0800431 } \
Michael Hennerich2b393312007-10-10 16:58:49 +0800432} \
433EXPORT_SYMBOL(set_gpio_ ## name);
Bryan Wu1394f032007-05-06 14:50:22 -0700434
435SET_GPIO_SC(maska)
436SET_GPIO_SC(maskb)
Bryan Wu1394f032007-05-06 14:50:22 -0700437SET_GPIO_SC(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700438
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800439void set_gpio_toggle(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700440{
441 unsigned long flags;
Mike Frysingerf5563092009-02-04 16:49:45 +0800442 if (ANOMALY_05000311 || ANOMALY_05000323)
David Howells3b139cd2010-10-07 14:08:52 +0100443 flags = hard_local_irq_save();
Mike Frysingerf5563092009-02-04 16:49:45 +0800444 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
445 if (ANOMALY_05000311 || ANOMALY_05000323) {
446 AWA_DUMMY_READ(toggle);
David Howells3b139cd2010-10-07 14:08:52 +0100447 hard_local_irq_restore(flags);
Mike Frysingerf5563092009-02-04 16:49:45 +0800448 }
Bryan Wu1394f032007-05-06 14:50:22 -0700449}
Bryan Wu1394f032007-05-06 14:50:22 -0700450EXPORT_SYMBOL(set_gpio_toggle);
451
452
453/*Set current PORT date (16-bit word)*/
454
Michael Hennerich2b393312007-10-10 16:58:49 +0800455#define SET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800456void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800457{ \
458 unsigned long flags; \
Mike Frysingerf5563092009-02-04 16:49:45 +0800459 if (ANOMALY_05000311 || ANOMALY_05000323) \
David Howells3b139cd2010-10-07 14:08:52 +0100460 flags = hard_local_irq_save(); \
Mike Frysingerf5563092009-02-04 16:49:45 +0800461 gpio_array[gpio_bank(gpio)]->name = arg; \
462 if (ANOMALY_05000311 || ANOMALY_05000323) { \
463 AWA_DUMMY_READ(name); \
David Howells3b139cd2010-10-07 14:08:52 +0100464 hard_local_irq_restore(flags); \
Mike Frysingerf5563092009-02-04 16:49:45 +0800465 } \
Michael Hennerich2b393312007-10-10 16:58:49 +0800466} \
467EXPORT_SYMBOL(set_gpiop_ ## name);
Bryan Wu1394f032007-05-06 14:50:22 -0700468
Michael Hennerich2b393312007-10-10 16:58:49 +0800469SET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700470SET_GPIO_P(dir)
471SET_GPIO_P(inen)
472SET_GPIO_P(polar)
473SET_GPIO_P(edge)
474SET_GPIO_P(both)
475SET_GPIO_P(maska)
476SET_GPIO_P(maskb)
477
Bryan Wu1394f032007-05-06 14:50:22 -0700478/* Get a specific bit */
Michael Hennerich2b393312007-10-10 16:58:49 +0800479#define GET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800480unsigned short get_gpio_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800481{ \
482 unsigned long flags; \
483 unsigned short ret; \
Mike Frysingerf5563092009-02-04 16:49:45 +0800484 if (ANOMALY_05000311 || ANOMALY_05000323) \
David Howells3b139cd2010-10-07 14:08:52 +0100485 flags = hard_local_irq_save(); \
Mike Frysingerf5563092009-02-04 16:49:45 +0800486 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
487 if (ANOMALY_05000311 || ANOMALY_05000323) { \
488 AWA_DUMMY_READ(name); \
David Howells3b139cd2010-10-07 14:08:52 +0100489 hard_local_irq_restore(flags); \
Mike Frysingerf5563092009-02-04 16:49:45 +0800490 } \
Michael Hennerich2b393312007-10-10 16:58:49 +0800491 return ret; \
492} \
493EXPORT_SYMBOL(get_gpio_ ## name);
Bryan Wu1394f032007-05-06 14:50:22 -0700494
Michael Hennerich2b393312007-10-10 16:58:49 +0800495GET_GPIO(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700496GET_GPIO(dir)
497GET_GPIO(inen)
498GET_GPIO(polar)
499GET_GPIO(edge)
500GET_GPIO(both)
501GET_GPIO(maska)
502GET_GPIO(maskb)
503
Bryan Wu1394f032007-05-06 14:50:22 -0700504/*Get current PORT date (16-bit word)*/
505
Michael Hennerich2b393312007-10-10 16:58:49 +0800506#define GET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800507unsigned short get_gpiop_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800508{ \
509 unsigned long flags; \
510 unsigned short ret; \
Mike Frysingerf5563092009-02-04 16:49:45 +0800511 if (ANOMALY_05000311 || ANOMALY_05000323) \
David Howells3b139cd2010-10-07 14:08:52 +0100512 flags = hard_local_irq_save(); \
Mike Frysingerf5563092009-02-04 16:49:45 +0800513 ret = (gpio_array[gpio_bank(gpio)]->name); \
514 if (ANOMALY_05000311 || ANOMALY_05000323) { \
515 AWA_DUMMY_READ(name); \
David Howells3b139cd2010-10-07 14:08:52 +0100516 hard_local_irq_restore(flags); \
Mike Frysingerf5563092009-02-04 16:49:45 +0800517 } \
Michael Hennerich2b393312007-10-10 16:58:49 +0800518 return ret; \
519} \
520EXPORT_SYMBOL(get_gpiop_ ## name);
Bryan Wu1394f032007-05-06 14:50:22 -0700521
Michael Hennerich2b393312007-10-10 16:58:49 +0800522GET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700523GET_GPIO_P(dir)
524GET_GPIO_P(inen)
525GET_GPIO_P(polar)
526GET_GPIO_P(edge)
527GET_GPIO_P(both)
528GET_GPIO_P(maska)
529GET_GPIO_P(maskb)
530
Bryan Wu1394f032007-05-06 14:50:22 -0700531
532#ifdef CONFIG_PM
Mike Frysinger332824b2010-06-02 04:22:01 +0000533DECLARE_RESERVED_MAP(wakeup, GPIO_BANK_NUM);
Mike Frysingerf5563092009-02-04 16:49:45 +0800534
535static const unsigned int sic_iwr_irqs[] = {
536#if defined(BF533_FAMILY)
537 IRQ_PROG_INTB
538#elif defined(BF537_FAMILY)
Mike Frysinger8c054102011-04-15 13:04:59 -0400539 IRQ_PF_INTB_WATCH, IRQ_PORTG_INTB, IRQ_PH_INTB_MAC_TX
Mike Frysingerf5563092009-02-04 16:49:45 +0800540#elif defined(BF538_FAMILY)
541 IRQ_PORTF_INTB
Mike Frysinger269647d2009-03-28 20:32:57 +0800542#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
Mike Frysingerf5563092009-02-04 16:49:45 +0800543 IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
544#elif defined(BF561_FAMILY)
545 IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
546#else
547# error no SIC_IWR defined
548#endif
549};
550
Bryan Wu1394f032007-05-06 14:50:22 -0700551/***********************************************************
552*
553* FUNCTIONS: Blackfin PM Setup API
554*
555* INPUTS/OUTPUTS:
556* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
557* type -
558* PM_WAKE_RISING
559* PM_WAKE_FALLING
560* PM_WAKE_HIGH
561* PM_WAKE_LOW
562* PM_WAKE_BOTH_EDGES
563*
564* DESCRIPTION: Blackfin PM Driver API
565*
566* CAUTION:
567*************************************************************
568* MODIFICATION HISTORY :
569**************************************************************/
Michael Hennerichbb84dbf2010-03-10 14:26:06 +0000570int gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
Bryan Wu1394f032007-05-06 14:50:22 -0700571{
572 unsigned long flags;
573
574 if (check_gpio(gpio) < 0)
Michael Hennerichbb84dbf2010-03-10 14:26:06 +0000575 return -EINVAL;
Bryan Wu1394f032007-05-06 14:50:22 -0700576
David Howells3b139cd2010-10-07 14:08:52 +0100577 flags = hard_local_irq_save();
Michael Hennerichbb84dbf2010-03-10 14:26:06 +0000578 if (ctrl)
Mike Frysinger332824b2010-06-02 04:22:01 +0000579 reserve(wakeup, gpio);
Michael Hennerichbb84dbf2010-03-10 14:26:06 +0000580 else
Mike Frysinger332824b2010-06-02 04:22:01 +0000581 unreserve(wakeup, gpio);
Bryan Wu1394f032007-05-06 14:50:22 -0700582
Michael Hennerichbb84dbf2010-03-10 14:26:06 +0000583 set_gpio_maskb(gpio, ctrl);
David Howells3b139cd2010-10-07 14:08:52 +0100584 hard_local_irq_restore(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700585
586 return 0;
587}
588
Michael Hennerichbb84dbf2010-03-10 14:26:06 +0000589int bfin_pm_standby_ctrl(unsigned ctrl)
Bryan Wu1394f032007-05-06 14:50:22 -0700590{
591 u16 bank, mask, i;
592
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800593 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Mike Frysinger332824b2010-06-02 04:22:01 +0000594 mask = map_entry(wakeup, i);
Bryan Wu1394f032007-05-06 14:50:22 -0700595 bank = gpio_bank(i);
596
Michael Hennerichbb84dbf2010-03-10 14:26:06 +0000597 if (mask)
598 bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
Bryan Wu1394f032007-05-06 14:50:22 -0700599 }
Michael Hennerichbb84dbf2010-03-10 14:26:06 +0000600 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700601}
602
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800603void bfin_gpio_pm_hibernate_suspend(void)
604{
605 int i, bank;
606
607 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
608 bank = gpio_bank(i);
609
Mike Frysinger269647d2009-03-28 20:32:57 +0800610#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
Mike Frysingerf5563092009-02-04 16:49:45 +0800611 gpio_bank_saved[bank].fer = *port_fer[bank];
Mike Frysinger269647d2009-03-28 20:32:57 +0800612#if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
Mike Frysingerf5563092009-02-04 16:49:45 +0800613 gpio_bank_saved[bank].mux = *port_mux[bank];
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800614#else
Mike Frysingerf5563092009-02-04 16:49:45 +0800615 if (bank == 0)
616 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800617#endif
618#endif
Mike Frysingerf5563092009-02-04 16:49:45 +0800619 gpio_bank_saved[bank].data = gpio_array[bank]->data;
620 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
621 gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
622 gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
623 gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
624 gpio_bank_saved[bank].both = gpio_array[bank]->both;
625 gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800626 }
627
628 AWA_DUMMY_READ(maska);
629}
630
631void bfin_gpio_pm_hibernate_restore(void)
632{
633 int i, bank;
634
635 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Mike Frysingerf5563092009-02-04 16:49:45 +0800636 bank = gpio_bank(i);
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800637
Mike Frysinger269647d2009-03-28 20:32:57 +0800638#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
639#if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
Mike Frysingerf5563092009-02-04 16:49:45 +0800640 *port_mux[bank] = gpio_bank_saved[bank].mux;
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800641#else
Mike Frysingerf5563092009-02-04 16:49:45 +0800642 if (bank == 0)
643 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800644#endif
Mike Frysingerf5563092009-02-04 16:49:45 +0800645 *port_fer[bank] = gpio_bank_saved[bank].fer;
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800646#endif
Mike Frysingerf5563092009-02-04 16:49:45 +0800647 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
Michael Hennerichc03c2a82009-07-08 12:04:43 +0000648 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
649 & gpio_bank_saved[bank].dir;
Mike Frysingerf5563092009-02-04 16:49:45 +0800650 gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
651 gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
652 gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
653 gpio_array[bank]->both = gpio_bank_saved[bank].both;
Mike Frysingerf5563092009-02-04 16:49:45 +0800654 gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800655 }
656 AWA_DUMMY_READ(maska);
657}
658
659
Bryan Wu1394f032007-05-06 14:50:22 -0700660#endif
Mike Frysinger269647d2009-03-28 20:32:57 +0800661#else /* CONFIG_BF54x */
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800662#ifdef CONFIG_PM
663
Michael Hennerichbb84dbf2010-03-10 14:26:06 +0000664int bfin_pm_standby_ctrl(unsigned ctrl)
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800665{
666 return 0;
667}
668
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800669void bfin_gpio_pm_hibernate_suspend(void)
670{
671 int i, bank;
672
673 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
674 bank = gpio_bank(i);
675
Mike Frysingerf5563092009-02-04 16:49:45 +0800676 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
677 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
678 gpio_bank_saved[bank].data = gpio_array[bank]->data;
Mike Frysingerf5563092009-02-04 16:49:45 +0800679 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
680 gpio_bank_saved[bank].dir = gpio_array[bank]->dir_set;
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800681 }
682}
683
684void bfin_gpio_pm_hibernate_restore(void)
685{
686 int i, bank;
687
688 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Mike Frysingerf5563092009-02-04 16:49:45 +0800689 bank = gpio_bank(i);
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800690
Mike Frysingerf5563092009-02-04 16:49:45 +0800691 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
692 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
693 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
694 gpio_array[bank]->dir_set = gpio_bank_saved[bank].dir;
695 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
696 | gpio_bank_saved[bank].dir;
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800697 }
698}
699#endif
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800700
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800701unsigned short get_gpio_dir(unsigned gpio)
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800702{
Mike Frysingerf5563092009-02-04 16:49:45 +0800703 return (0x01 & (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio)));
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800704}
705EXPORT_SYMBOL(get_gpio_dir);
706
Mike Frysinger269647d2009-03-28 20:32:57 +0800707#endif /* CONFIG_BF54x */
Bryan Wu1394f032007-05-06 14:50:22 -0700708
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800709/***********************************************************
710*
Mike Frysinger812ae982010-07-05 08:40:41 +0000711* FUNCTIONS: Blackfin Peripheral Resource Allocation
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800712* and PortMux Setup
713*
714* INPUTS/OUTPUTS:
715* per Peripheral Identifier
716* label String
717*
718* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
719*
720* CAUTION:
721*************************************************************
722* MODIFICATION HISTORY :
723**************************************************************/
Michael Hennerichc58c2142007-10-04 00:35:05 +0800724
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800725int peripheral_request(unsigned short per, const char *label)
726{
727 unsigned long flags;
728 unsigned short ident = P_IDENT(per);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800729
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800730 /*
731 * Don't cares are pins with only one dedicated function
732 */
733
734 if (per & P_DONTCARE)
735 return 0;
736
737 if (!(per & P_DEFINED))
738 return -ENODEV;
739
Barry Song89e84ee2009-09-16 04:36:29 +0000740 BUG_ON(ident >= MAX_RESOURCES);
741
David Howells3b139cd2010-10-07 14:08:52 +0100742 flags = hard_local_irq_save();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800743
Mike Frysinger6a87d292008-10-28 16:16:29 +0800744 /* If a pin can be muxed as either GPIO or peripheral, make
745 * sure it is not already a GPIO pin when we request it.
746 */
Mike Frysinger332824b2010-06-02 04:22:01 +0000747 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
Robin Getzd6879c52009-03-29 01:10:30 +0800748 if (system_state == SYSTEM_BOOTING)
749 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800750 printk(KERN_ERR
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800751 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800752 __func__, ident, get_label(ident));
David Howells3b139cd2010-10-07 14:08:52 +0100753 hard_local_irq_restore(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800754 return -EBUSY;
755 }
756
Mike Frysinger332824b2010-06-02 04:22:01 +0000757 if (unlikely(is_reserved(peri, ident, 1))) {
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800758
Mike Frysingerd171c232008-03-26 08:35:46 +0800759 /*
760 * Pin functions like AMC address strobes my
761 * be requested and used by several drivers
762 */
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800763
Mike Frysinger269647d2009-03-28 20:32:57 +0800764#ifdef CONFIG_BF54x
Mike Frysingerf5563092009-02-04 16:49:45 +0800765 if (!((per & P_MAYSHARE) && get_portmux(per) == P_FUNCT2MUX(per))) {
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800766#else
767 if (!(per & P_MAYSHARE)) {
768#endif
Mike Frysingerd171c232008-03-26 08:35:46 +0800769 /*
770 * Allow that the identical pin function can
771 * be requested from the same driver twice
772 */
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800773
Mike Frysingerd171c232008-03-26 08:35:46 +0800774 if (cmp_label(ident, label) == 0)
775 goto anyway;
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800776
Robin Getzd6879c52009-03-29 01:10:30 +0800777 if (system_state == SYSTEM_BOOTING)
778 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800779 printk(KERN_ERR
780 "%s: Peripheral %d function %d is already reserved by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800781 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
David Howells3b139cd2010-10-07 14:08:52 +0100782 hard_local_irq_restore(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800783 return -EBUSY;
784 }
785 }
786
steven miao05bbec32010-09-17 03:03:17 +0000787 if (unlikely(portmux_group_check(per))) {
788 hard_local_irq_restore(flags);
789 return -EBUSY;
790 }
Mike Frysingerd171c232008-03-26 08:35:46 +0800791 anyway:
Mike Frysinger332824b2010-06-02 04:22:01 +0000792 reserve(peri, ident);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800793
Mike Frysingerf5563092009-02-04 16:49:45 +0800794 portmux_setup(per);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800795 port_setup(ident, PERIPHERAL_USAGE);
796
David Howells3b139cd2010-10-07 14:08:52 +0100797 hard_local_irq_restore(flags);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800798 set_label(ident, label);
799
800 return 0;
801}
802EXPORT_SYMBOL(peripheral_request);
803
Mike Frysinger68179372008-04-24 05:04:24 +0800804int peripheral_request_list(const unsigned short per[], const char *label)
Michael Hennerichc58c2142007-10-04 00:35:05 +0800805{
806 u16 cnt;
807 int ret;
808
809 for (cnt = 0; per[cnt] != 0; cnt++) {
Michael Hennerich314c98d2007-07-24 18:03:45 +0800810
Michael Hennerichc58c2142007-10-04 00:35:05 +0800811 ret = peripheral_request(per[cnt], label);
Michael Hennerich314c98d2007-07-24 18:03:45 +0800812
813 if (ret < 0) {
Mike Frysingerd171c232008-03-26 08:35:46 +0800814 for ( ; cnt > 0; cnt--)
Michael Hennerich314c98d2007-07-24 18:03:45 +0800815 peripheral_free(per[cnt - 1]);
Mike Frysingerd171c232008-03-26 08:35:46 +0800816
817 return ret;
Michael Hennerich314c98d2007-07-24 18:03:45 +0800818 }
Michael Hennerichc58c2142007-10-04 00:35:05 +0800819 }
820
821 return 0;
822}
823EXPORT_SYMBOL(peripheral_request_list);
824
825void peripheral_free(unsigned short per)
826{
827 unsigned long flags;
828 unsigned short ident = P_IDENT(per);
829
830 if (per & P_DONTCARE)
831 return;
832
833 if (!(per & P_DEFINED))
834 return;
835
David Howells3b139cd2010-10-07 14:08:52 +0100836 flags = hard_local_irq_save();
Michael Hennerichc58c2142007-10-04 00:35:05 +0800837
Mike Frysinger332824b2010-06-02 04:22:01 +0000838 if (unlikely(!is_reserved(peri, ident, 0))) {
David Howells3b139cd2010-10-07 14:08:52 +0100839 hard_local_irq_restore(flags);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800840 return;
841 }
842
Mike Frysingerd171c232008-03-26 08:35:46 +0800843 if (!(per & P_MAYSHARE))
Michael Hennerichc58c2142007-10-04 00:35:05 +0800844 port_setup(ident, GPIO_USAGE);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800845
Mike Frysinger332824b2010-06-02 04:22:01 +0000846 unreserve(peri, ident);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800847
Michael Hennerich2acde902007-10-11 00:24:40 +0800848 set_label(ident, "free");
849
David Howells3b139cd2010-10-07 14:08:52 +0100850 hard_local_irq_restore(flags);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800851}
852EXPORT_SYMBOL(peripheral_free);
853
Mike Frysinger68179372008-04-24 05:04:24 +0800854void peripheral_free_list(const unsigned short per[])
Michael Hennerichc58c2142007-10-04 00:35:05 +0800855{
856 u16 cnt;
Mike Frysingerd171c232008-03-26 08:35:46 +0800857 for (cnt = 0; per[cnt] != 0; cnt++)
Michael Hennerichc58c2142007-10-04 00:35:05 +0800858 peripheral_free(per[cnt]);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800859}
860EXPORT_SYMBOL(peripheral_free_list);
861
Bryan Wu1394f032007-05-06 14:50:22 -0700862/***********************************************************
863*
864* FUNCTIONS: Blackfin GPIO Driver
865*
866* INPUTS/OUTPUTS:
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800867* gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
868* label String
Bryan Wu1394f032007-05-06 14:50:22 -0700869*
870* DESCRIPTION: Blackfin GPIO Driver API
871*
872* CAUTION:
873*************************************************************
874* MODIFICATION HISTORY :
875**************************************************************/
876
Michael Hennericha4f0b322008-11-18 17:48:22 +0800877int bfin_gpio_request(unsigned gpio, const char *label)
Bryan Wu1394f032007-05-06 14:50:22 -0700878{
879 unsigned long flags;
880
881 if (check_gpio(gpio) < 0)
882 return -EINVAL;
883
David Howells3b139cd2010-10-07 14:08:52 +0100884 flags = hard_local_irq_save();
Bryan Wu1394f032007-05-06 14:50:22 -0700885
Michael Hennerich2acde902007-10-11 00:24:40 +0800886 /*
887 * Allow that the identical GPIO can
888 * be requested from the same driver twice
889 * Do nothing and return -
890 */
891
892 if (cmp_label(gpio, label) == 0) {
David Howells3b139cd2010-10-07 14:08:52 +0100893 hard_local_irq_restore(flags);
Michael Hennerich2acde902007-10-11 00:24:40 +0800894 return 0;
895 }
896
Mike Frysinger332824b2010-06-02 04:22:01 +0000897 if (unlikely(is_reserved(gpio, gpio, 1))) {
Robin Getzd6879c52009-03-29 01:10:30 +0800898 if (system_state == SYSTEM_BOOTING)
899 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800900 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
Graf Yang9570ff42009-01-07 23:14:38 +0800901 gpio, get_label(gpio));
David Howells3b139cd2010-10-07 14:08:52 +0100902 hard_local_irq_restore(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700903 return -EBUSY;
904 }
Mike Frysinger332824b2010-06-02 04:22:01 +0000905 if (unlikely(is_reserved(peri, gpio, 1))) {
Robin Getzd6879c52009-03-29 01:10:30 +0800906 if (system_state == SYSTEM_BOOTING)
907 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800908 printk(KERN_ERR
909 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
910 gpio, get_label(gpio));
David Howells3b139cd2010-10-07 14:08:52 +0100911 hard_local_irq_restore(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800912 return -EBUSY;
913 }
Mike Frysinger332824b2010-06-02 04:22:01 +0000914 if (unlikely(is_reserved(gpio_irq, gpio, 1))) {
Graf Yang9570ff42009-01-07 23:14:38 +0800915 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
916 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
Michael Hennericha2be3932009-02-04 16:49:45 +0800917 }
Mike Frysinger269647d2009-03-28 20:32:57 +0800918#ifndef CONFIG_BF54x
Michael Hennericha2be3932009-02-04 16:49:45 +0800919 else { /* Reset POLAR setting when acquiring a gpio for the first time */
920 set_gpio_polar(gpio, 0);
921 }
922#endif
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800923
Mike Frysinger332824b2010-06-02 04:22:01 +0000924 reserve(gpio, gpio);
Graf Yang9570ff42009-01-07 23:14:38 +0800925 set_label(gpio, label);
Bryan Wu1394f032007-05-06 14:50:22 -0700926
David Howells3b139cd2010-10-07 14:08:52 +0100927 hard_local_irq_restore(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700928
929 port_setup(gpio, GPIO_USAGE);
930
931 return 0;
932}
Michael Hennericha4f0b322008-11-18 17:48:22 +0800933EXPORT_SYMBOL(bfin_gpio_request);
Bryan Wu1394f032007-05-06 14:50:22 -0700934
Michael Hennericha4f0b322008-11-18 17:48:22 +0800935void bfin_gpio_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700936{
937 unsigned long flags;
938
939 if (check_gpio(gpio) < 0)
940 return;
941
Uwe Kleine-Koenig45c4f2a2009-02-04 17:02:30 +0800942 might_sleep();
943
David Howells3b139cd2010-10-07 14:08:52 +0100944 flags = hard_local_irq_save();
Bryan Wu1394f032007-05-06 14:50:22 -0700945
Mike Frysinger332824b2010-06-02 04:22:01 +0000946 if (unlikely(!is_reserved(gpio, gpio, 0))) {
Robin Getzd6879c52009-03-29 01:10:30 +0800947 if (system_state == SYSTEM_BOOTING)
948 dump_stack();
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800949 gpio_error(gpio);
David Howells3b139cd2010-10-07 14:08:52 +0100950 hard_local_irq_restore(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700951 return;
952 }
953
Mike Frysinger332824b2010-06-02 04:22:01 +0000954 unreserve(gpio, gpio);
Bryan Wu1394f032007-05-06 14:50:22 -0700955
Michael Hennerich2acde902007-10-11 00:24:40 +0800956 set_label(gpio, "free");
957
David Howells3b139cd2010-10-07 14:08:52 +0100958 hard_local_irq_restore(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700959}
Michael Hennericha4f0b322008-11-18 17:48:22 +0800960EXPORT_SYMBOL(bfin_gpio_free);
Bryan Wu1394f032007-05-06 14:50:22 -0700961
Michael Hennerich621dd242009-09-28 12:23:41 +0000962#ifdef BFIN_SPECIAL_GPIO_BANKS
Mike Frysinger332824b2010-06-02 04:22:01 +0000963DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
Michael Hennerich621dd242009-09-28 12:23:41 +0000964
965int bfin_special_gpio_request(unsigned gpio, const char *label)
966{
967 unsigned long flags;
968
David Howells3b139cd2010-10-07 14:08:52 +0100969 flags = hard_local_irq_save();
Michael Hennerich621dd242009-09-28 12:23:41 +0000970
971 /*
972 * Allow that the identical GPIO can
973 * be requested from the same driver twice
974 * Do nothing and return -
975 */
976
977 if (cmp_label(gpio, label) == 0) {
David Howells3b139cd2010-10-07 14:08:52 +0100978 hard_local_irq_restore(flags);
Michael Hennerich621dd242009-09-28 12:23:41 +0000979 return 0;
980 }
981
Mike Frysinger332824b2010-06-02 04:22:01 +0000982 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
David Howells3b139cd2010-10-07 14:08:52 +0100983 hard_local_irq_restore(flags);
Michael Hennerich621dd242009-09-28 12:23:41 +0000984 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
985 gpio, get_label(gpio));
986
987 return -EBUSY;
988 }
Mike Frysinger332824b2010-06-02 04:22:01 +0000989 if (unlikely(is_reserved(peri, gpio, 1))) {
David Howells3b139cd2010-10-07 14:08:52 +0100990 hard_local_irq_restore(flags);
Michael Hennerich621dd242009-09-28 12:23:41 +0000991 printk(KERN_ERR
992 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
993 gpio, get_label(gpio));
994
995 return -EBUSY;
996 }
997
Mike Frysinger332824b2010-06-02 04:22:01 +0000998 reserve(special_gpio, gpio);
999 reserve(peri, gpio);
Michael Hennerich621dd242009-09-28 12:23:41 +00001000
1001 set_label(gpio, label);
David Howells3b139cd2010-10-07 14:08:52 +01001002 hard_local_irq_restore(flags);
Michael Hennerich621dd242009-09-28 12:23:41 +00001003 port_setup(gpio, GPIO_USAGE);
1004
1005 return 0;
1006}
1007EXPORT_SYMBOL(bfin_special_gpio_request);
1008
1009void bfin_special_gpio_free(unsigned gpio)
1010{
1011 unsigned long flags;
1012
1013 might_sleep();
1014
David Howells3b139cd2010-10-07 14:08:52 +01001015 flags = hard_local_irq_save();
Michael Hennerich621dd242009-09-28 12:23:41 +00001016
Mike Frysinger332824b2010-06-02 04:22:01 +00001017 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
Michael Hennerich621dd242009-09-28 12:23:41 +00001018 gpio_error(gpio);
David Howells3b139cd2010-10-07 14:08:52 +01001019 hard_local_irq_restore(flags);
Michael Hennerich621dd242009-09-28 12:23:41 +00001020 return;
1021 }
1022
Mike Frysinger332824b2010-06-02 04:22:01 +00001023 unreserve(special_gpio, gpio);
1024 unreserve(peri, gpio);
Michael Hennerich621dd242009-09-28 12:23:41 +00001025 set_label(gpio, "free");
David Howells3b139cd2010-10-07 14:08:52 +01001026 hard_local_irq_restore(flags);
Michael Hennerich621dd242009-09-28 12:23:41 +00001027}
1028EXPORT_SYMBOL(bfin_special_gpio_free);
1029#endif
1030
1031
Graf Yang9570ff42009-01-07 23:14:38 +08001032int bfin_gpio_irq_request(unsigned gpio, const char *label)
1033{
1034 unsigned long flags;
1035
1036 if (check_gpio(gpio) < 0)
1037 return -EINVAL;
1038
David Howells3b139cd2010-10-07 14:08:52 +01001039 flags = hard_local_irq_save();
Graf Yang9570ff42009-01-07 23:14:38 +08001040
Mike Frysinger332824b2010-06-02 04:22:01 +00001041 if (unlikely(is_reserved(peri, gpio, 1))) {
Robin Getzd6879c52009-03-29 01:10:30 +08001042 if (system_state == SYSTEM_BOOTING)
1043 dump_stack();
Graf Yang9570ff42009-01-07 23:14:38 +08001044 printk(KERN_ERR
1045 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1046 gpio, get_label(gpio));
David Howells3b139cd2010-10-07 14:08:52 +01001047 hard_local_irq_restore(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001048 return -EBUSY;
1049 }
Mike Frysinger332824b2010-06-02 04:22:01 +00001050 if (unlikely(is_reserved(gpio, gpio, 1)))
Graf Yang9570ff42009-01-07 23:14:38 +08001051 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
1052 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1053 gpio, get_label(gpio));
1054
Mike Frysinger332824b2010-06-02 04:22:01 +00001055 reserve(gpio_irq, gpio);
Graf Yang9570ff42009-01-07 23:14:38 +08001056 set_label(gpio, label);
1057
David Howells3b139cd2010-10-07 14:08:52 +01001058 hard_local_irq_restore(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001059
1060 port_setup(gpio, GPIO_USAGE);
1061
1062 return 0;
1063}
1064
1065void bfin_gpio_irq_free(unsigned gpio)
1066{
1067 unsigned long flags;
1068
1069 if (check_gpio(gpio) < 0)
1070 return;
1071
David Howells3b139cd2010-10-07 14:08:52 +01001072 flags = hard_local_irq_save();
Graf Yang9570ff42009-01-07 23:14:38 +08001073
Mike Frysinger332824b2010-06-02 04:22:01 +00001074 if (unlikely(!is_reserved(gpio_irq, gpio, 0))) {
Robin Getzd6879c52009-03-29 01:10:30 +08001075 if (system_state == SYSTEM_BOOTING)
1076 dump_stack();
Graf Yang9570ff42009-01-07 23:14:38 +08001077 gpio_error(gpio);
David Howells3b139cd2010-10-07 14:08:52 +01001078 hard_local_irq_restore(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001079 return;
1080 }
1081
Mike Frysinger332824b2010-06-02 04:22:01 +00001082 unreserve(gpio_irq, gpio);
Graf Yang9570ff42009-01-07 23:14:38 +08001083
1084 set_label(gpio, "free");
1085
David Howells3b139cd2010-10-07 14:08:52 +01001086 hard_local_irq_restore(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001087}
1088
Mike Frysingerf5563092009-02-04 16:49:45 +08001089static inline void __bfin_gpio_direction_input(unsigned gpio)
1090{
Mike Frysinger269647d2009-03-28 20:32:57 +08001091#ifdef CONFIG_BF54x
Mike Frysingerf5563092009-02-04 16:49:45 +08001092 gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001093#else
Mike Frysingerf5563092009-02-04 16:49:45 +08001094 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1095#endif
1096 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
Michael Hennerich803a8d22008-05-17 16:01:51 +08001097}
Michael Hennerich803a8d22008-05-17 16:01:51 +08001098
Michael Hennericha4f0b322008-11-18 17:48:22 +08001099int bfin_gpio_direction_input(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001100{
1101 unsigned long flags;
1102
Mike Frysinger332824b2010-06-02 04:22:01 +00001103 if (unlikely(!is_reserved(gpio, gpio, 0))) {
Michael Hennerichacbcd262008-01-22 18:36:20 +08001104 gpio_error(gpio);
1105 return -EINVAL;
1106 }
Bryan Wu1394f032007-05-06 14:50:22 -07001107
David Howells3b139cd2010-10-07 14:08:52 +01001108 flags = hard_local_irq_save();
Mike Frysingerf5563092009-02-04 16:49:45 +08001109 __bfin_gpio_direction_input(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001110 AWA_DUMMY_READ(inen);
David Howells3b139cd2010-10-07 14:08:52 +01001111 hard_local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001112
1113 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001114}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001115EXPORT_SYMBOL(bfin_gpio_direction_input);
Bryan Wu1394f032007-05-06 14:50:22 -07001116
Mike Frysingerf5563092009-02-04 16:49:45 +08001117void bfin_gpio_irq_prepare(unsigned gpio)
1118{
Mike Frysinger269647d2009-03-28 20:32:57 +08001119#ifdef CONFIG_BF54x
Mike Frysingerf5563092009-02-04 16:49:45 +08001120 unsigned long flags;
1121#endif
1122
1123 port_setup(gpio, GPIO_USAGE);
1124
Mike Frysinger269647d2009-03-28 20:32:57 +08001125#ifdef CONFIG_BF54x
David Howells3b139cd2010-10-07 14:08:52 +01001126 flags = hard_local_irq_save();
Mike Frysingerf5563092009-02-04 16:49:45 +08001127 __bfin_gpio_direction_input(gpio);
David Howells3b139cd2010-10-07 14:08:52 +01001128 hard_local_irq_restore(flags);
Mike Frysingerf5563092009-02-04 16:49:45 +08001129#endif
1130}
1131
1132void bfin_gpio_set_value(unsigned gpio, int arg)
1133{
1134 if (arg)
1135 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1136 else
1137 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1138}
1139EXPORT_SYMBOL(bfin_gpio_set_value);
1140
Michael Hennericha4f0b322008-11-18 17:48:22 +08001141int bfin_gpio_direction_output(unsigned gpio, int value)
Bryan Wu1394f032007-05-06 14:50:22 -07001142{
1143 unsigned long flags;
1144
Mike Frysinger332824b2010-06-02 04:22:01 +00001145 if (unlikely(!is_reserved(gpio, gpio, 0))) {
Michael Hennerichacbcd262008-01-22 18:36:20 +08001146 gpio_error(gpio);
1147 return -EINVAL;
1148 }
Bryan Wu1394f032007-05-06 14:50:22 -07001149
David Howells3b139cd2010-10-07 14:08:52 +01001150 flags = hard_local_irq_save();
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001151
Mike Frysingerf5563092009-02-04 16:49:45 +08001152 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1153 gpio_set_value(gpio, value);
Mike Frysinger269647d2009-03-28 20:32:57 +08001154#ifdef CONFIG_BF54x
Mike Frysingerf5563092009-02-04 16:49:45 +08001155 gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
1156#else
1157 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1158#endif
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001159
Michael Hennerich2b393312007-10-10 16:58:49 +08001160 AWA_DUMMY_READ(dir);
David Howells3b139cd2010-10-07 14:08:52 +01001161 hard_local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001162
1163 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001164}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001165EXPORT_SYMBOL(bfin_gpio_direction_output);
Mike Frysinger168f1212007-10-11 00:22:35 +08001166
Mike Frysingerf5563092009-02-04 16:49:45 +08001167int bfin_gpio_get_value(unsigned gpio)
1168{
Mike Frysinger269647d2009-03-28 20:32:57 +08001169#ifdef CONFIG_BF54x
Mike Frysingerf5563092009-02-04 16:49:45 +08001170 return (1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio)));
1171#else
1172 unsigned long flags;
1173
1174 if (unlikely(get_gpio_edge(gpio))) {
1175 int ret;
David Howells3b139cd2010-10-07 14:08:52 +01001176 flags = hard_local_irq_save();
Mike Frysingerf5563092009-02-04 16:49:45 +08001177 set_gpio_edge(gpio, 0);
1178 ret = get_gpio_data(gpio);
1179 set_gpio_edge(gpio, 1);
David Howells3b139cd2010-10-07 14:08:52 +01001180 hard_local_irq_restore(flags);
Mike Frysingerf5563092009-02-04 16:49:45 +08001181 return ret;
1182 } else
1183 return get_gpio_data(gpio);
1184#endif
1185}
1186EXPORT_SYMBOL(bfin_gpio_get_value);
1187
Mike Frysinger168f1212007-10-11 00:22:35 +08001188/* If we are booting from SPI and our board lacks a strong enough pull up,
1189 * the core can reset and execute the bootrom faster than the resistor can
1190 * pull the signal logically high. To work around this (common) error in
1191 * board design, we explicitly set the pin back to GPIO mode, force /CS
1192 * high, and wait for the electrons to do their thing.
1193 *
1194 * This function only makes sense to be called from reset code, but it
1195 * lives here as we need to force all the GPIO states w/out going through
1196 * BUG() checks and such.
1197 */
Sonic Zhangb52dae32009-02-04 16:49:45 +08001198void bfin_reset_boot_spi_cs(unsigned short pin)
Mike Frysinger168f1212007-10-11 00:22:35 +08001199{
Sonic Zhangb52dae32009-02-04 16:49:45 +08001200 unsigned short gpio = P_IDENT(pin);
Michael Hennerich4d5f4ed2007-08-27 16:46:17 +08001201 port_setup(gpio, GPIO_USAGE);
Mike Frysingerf5563092009-02-04 16:49:45 +08001202 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001203 AWA_DUMMY_READ(data_set);
Mike Frysinger168f1212007-10-11 00:22:35 +08001204 udelay(1);
1205}
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001206
Mike Frysinger1545a112007-12-24 16:54:48 +08001207#if defined(CONFIG_PROC_FS)
Alexey Dobriyan6362ec22011-05-14 19:48:46 +03001208static int gpio_proc_show(struct seq_file *m, void *v)
Mike Frysinger1545a112007-12-24 16:54:48 +08001209{
Alexey Dobriyan6362ec22011-05-14 19:48:46 +03001210 int c, irq, gpio;
Mike Frysinger1545a112007-12-24 16:54:48 +08001211
1212 for (c = 0; c < MAX_RESOURCES; c++) {
Mike Frysinger332824b2010-06-02 04:22:01 +00001213 irq = is_reserved(gpio_irq, c, 1);
1214 gpio = is_reserved(gpio, c, 1);
Graf Yang9570ff42009-01-07 23:14:38 +08001215 if (!check_gpio(c) && (gpio || irq))
Alexey Dobriyan6362ec22011-05-14 19:48:46 +03001216 seq_printf(m, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
Graf Yang9570ff42009-01-07 23:14:38 +08001217 get_label(c), (gpio && irq) ? " *" : "",
1218 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
Mike Frysinger332824b2010-06-02 04:22:01 +00001219 else if (is_reserved(peri, c, 1))
Alexey Dobriyan6362ec22011-05-14 19:48:46 +03001220 seq_printf(m, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
Mike Frysinger1545a112007-12-24 16:54:48 +08001221 else
1222 continue;
Mike Frysinger1545a112007-12-24 16:54:48 +08001223 }
Alexey Dobriyan6362ec22011-05-14 19:48:46 +03001224
1225 return 0;
Mike Frysinger1545a112007-12-24 16:54:48 +08001226}
1227
Alexey Dobriyan6362ec22011-05-14 19:48:46 +03001228static int gpio_proc_open(struct inode *inode, struct file *file)
1229{
1230 return single_open(file, gpio_proc_show, NULL);
1231}
1232
1233static const struct file_operations gpio_proc_ops = {
1234 .open = gpio_proc_open,
1235 .read = seq_read,
1236 .llseek = seq_lseek,
1237 .release = single_release,
1238};
1239
Mike Frysinger1545a112007-12-24 16:54:48 +08001240static __init int gpio_register_proc(void)
1241{
1242 struct proc_dir_entry *proc_gpio;
1243
Alexey Dobriyan6362ec22011-05-14 19:48:46 +03001244 proc_gpio = proc_create("gpio", S_IRUGO, NULL, &gpio_proc_ops);
Mike Frysinger1545a112007-12-24 16:54:48 +08001245 return proc_gpio != NULL;
1246}
Mike Frysinger1545a112007-12-24 16:54:48 +08001247__initcall(gpio_register_proc);
1248#endif
Michael Hennericha4f0b322008-11-18 17:48:22 +08001249
1250#ifdef CONFIG_GPIOLIB
Joachim Eastwoodf9c29e82010-02-11 12:41:11 +01001251static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
Michael Hennericha4f0b322008-11-18 17:48:22 +08001252{
1253 return bfin_gpio_direction_input(gpio);
1254}
1255
Joachim Eastwoodf9c29e82010-02-11 12:41:11 +01001256static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
Michael Hennericha4f0b322008-11-18 17:48:22 +08001257{
1258 return bfin_gpio_direction_output(gpio, level);
1259}
1260
Joachim Eastwoodf9c29e82010-02-11 12:41:11 +01001261static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
Michael Hennericha4f0b322008-11-18 17:48:22 +08001262{
1263 return bfin_gpio_get_value(gpio);
1264}
1265
Joachim Eastwoodf9c29e82010-02-11 12:41:11 +01001266static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
Michael Hennericha4f0b322008-11-18 17:48:22 +08001267{
Michael Hennericha4f0b322008-11-18 17:48:22 +08001268 return bfin_gpio_set_value(gpio, value);
Michael Hennericha4f0b322008-11-18 17:48:22 +08001269}
1270
Joachim Eastwoodf9c29e82010-02-11 12:41:11 +01001271static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
Michael Hennericha4f0b322008-11-18 17:48:22 +08001272{
1273 return bfin_gpio_request(gpio, chip->label);
1274}
1275
Joachim Eastwoodf9c29e82010-02-11 12:41:11 +01001276static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
Michael Hennericha4f0b322008-11-18 17:48:22 +08001277{
1278 return bfin_gpio_free(gpio);
1279}
1280
Joachim Eastwoodf9c29e82010-02-11 12:41:11 +01001281static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
Joachim Eastwood7f4f69f2010-02-10 12:31:41 +01001282{
1283 return gpio + GPIO_IRQ_BASE;
1284}
1285
Michael Hennericha4f0b322008-11-18 17:48:22 +08001286static struct gpio_chip bfin_chip = {
Michael Hennerichedd07992009-12-16 08:45:17 +00001287 .label = "BFIN-GPIO",
Michael Hennericha4f0b322008-11-18 17:48:22 +08001288 .direction_input = bfin_gpiolib_direction_input,
1289 .get = bfin_gpiolib_get_value,
1290 .direction_output = bfin_gpiolib_direction_output,
1291 .set = bfin_gpiolib_set_value,
1292 .request = bfin_gpiolib_gpio_request,
1293 .free = bfin_gpiolib_gpio_free,
Joachim Eastwood7f4f69f2010-02-10 12:31:41 +01001294 .to_irq = bfin_gpiolib_gpio_to_irq,
Michael Hennericha4f0b322008-11-18 17:48:22 +08001295 .base = 0,
1296 .ngpio = MAX_BLACKFIN_GPIOS,
1297};
1298
1299static int __init bfin_gpiolib_setup(void)
1300{
1301 return gpiochip_add(&bfin_chip);
1302}
1303arch_initcall(bfin_gpiolib_setup);
1304#endif