blob: 0182ce1fc4fb848742e0e5790079fe0a19abaefe [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
2 * File: arch/blackfin/kernel/bfin_gpio.c
3 * Based on:
4 * Author: Michael Hennerich (hennerich@blackfin.uclinux.org)
5 *
6 * Created:
7 * Description: GPIO Abstraction Layer
8 *
9 * Modified:
10 * Copyright 2006 Analog Devices Inc.
11 *
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */
29
30/*
31* Number BF537/6/4 BF561 BF533/2/1
32*
33* GPIO_0 PF0 PF0 PF0
34* GPIO_1 PF1 PF1 PF1
35* GPIO_2 PF2 PF2 PF2
36* GPIO_3 PF3 PF3 PF3
37* GPIO_4 PF4 PF4 PF4
38* GPIO_5 PF5 PF5 PF5
39* GPIO_6 PF6 PF6 PF6
40* GPIO_7 PF7 PF7 PF7
41* GPIO_8 PF8 PF8 PF8
42* GPIO_9 PF9 PF9 PF9
43* GPIO_10 PF10 PF10 PF10
44* GPIO_11 PF11 PF11 PF11
45* GPIO_12 PF12 PF12 PF12
46* GPIO_13 PF13 PF13 PF13
47* GPIO_14 PF14 PF14 PF14
48* GPIO_15 PF15 PF15 PF15
49* GPIO_16 PG0 PF16
50* GPIO_17 PG1 PF17
51* GPIO_18 PG2 PF18
52* GPIO_19 PG3 PF19
53* GPIO_20 PG4 PF20
54* GPIO_21 PG5 PF21
55* GPIO_22 PG6 PF22
56* GPIO_23 PG7 PF23
57* GPIO_24 PG8 PF24
58* GPIO_25 PG9 PF25
59* GPIO_26 PG10 PF26
60* GPIO_27 PG11 PF27
61* GPIO_28 PG12 PF28
62* GPIO_29 PG13 PF29
63* GPIO_30 PG14 PF30
64* GPIO_31 PG15 PF31
65* GPIO_32 PH0 PF32
66* GPIO_33 PH1 PF33
67* GPIO_34 PH2 PF34
68* GPIO_35 PH3 PF35
69* GPIO_36 PH4 PF36
70* GPIO_37 PH5 PF37
71* GPIO_38 PH6 PF38
72* GPIO_39 PH7 PF39
73* GPIO_40 PH8 PF40
74* GPIO_41 PH9 PF41
75* GPIO_42 PH10 PF42
76* GPIO_43 PH11 PF43
77* GPIO_44 PH12 PF44
78* GPIO_45 PH13 PF45
79* GPIO_46 PH14 PF46
80* GPIO_47 PH15 PF47
81*/
82
83#include <linux/module.h>
84#include <linux/err.h>
85#include <asm/blackfin.h>
86#include <asm/gpio.h>
Michael Hennerichc58c2142007-10-04 00:35:05 +080087#include <asm/portmux.h>
Bryan Wu1394f032007-05-06 14:50:22 -070088#include <linux/irq.h>
89
90#ifdef BF533_FAMILY
91static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
92 (struct gpio_port_t *) FIO_FLAG_D,
93};
94#endif
95
96#ifdef BF537_FAMILY
97static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
98 (struct gpio_port_t *) PORTFIO,
99 (struct gpio_port_t *) PORTGIO,
100 (struct gpio_port_t *) PORTHIO,
101};
102
103static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
104 (unsigned short *) PORTF_FER,
105 (unsigned short *) PORTG_FER,
106 (unsigned short *) PORTH_FER,
107};
108
109#endif
110
111#ifdef BF561_FAMILY
112static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
113 (struct gpio_port_t *) FIO0_FLAG_D,
114 (struct gpio_port_t *) FIO1_FLAG_D,
115 (struct gpio_port_t *) FIO2_FLAG_D,
116};
117#endif
118
Michael Hennerichc58c2142007-10-04 00:35:05 +0800119static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
120static unsigned short reserved_peri_map[gpio_bank(MAX_BLACKFIN_GPIOS + 16)];
Michael Hennerichc58c2142007-10-04 00:35:05 +0800121
Michael Hennerich8c613622007-08-03 17:48:09 +0800122#define MAX_RESOURCES 256
123#define RESOURCE_LABEL_SIZE 16
124
125struct str_ident {
126 char name[RESOURCE_LABEL_SIZE];
127} *str_ident;
128
Bryan Wu1394f032007-05-06 14:50:22 -0700129
130#ifdef CONFIG_PM
131static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
132static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
133static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
134
135#ifdef BF533_FAMILY
136static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
137#endif
138
139#ifdef BF537_FAMILY
140static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
141#endif
142
143#ifdef BF561_FAMILY
144static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
145#endif
146
147#endif /* CONFIG_PM */
148
149inline int check_gpio(unsigned short gpio)
150{
Michael Henneriche7613aa2007-06-11 16:37:57 +0800151 if (gpio >= MAX_BLACKFIN_GPIOS)
Bryan Wu1394f032007-05-06 14:50:22 -0700152 return -EINVAL;
153 return 0;
154}
155
Michael Hennerichc58c2142007-10-04 00:35:05 +0800156static void set_label(unsigned short ident, const char *label)
157{
158
159 if (label && str_ident) {
Michael Hennerich8c613622007-08-03 17:48:09 +0800160 strncpy(str_ident[ident].name, label,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800161 RESOURCE_LABEL_SIZE);
Michael Hennerich8c613622007-08-03 17:48:09 +0800162 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800163 }
164}
165
166static char *get_label(unsigned short ident)
167{
168 if (!str_ident)
169 return "UNKNOWN";
170
Michael Hennerich8c613622007-08-03 17:48:09 +0800171 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
Michael Hennerichc58c2142007-10-04 00:35:05 +0800172}
173
174static int cmp_label(unsigned short ident, const char *label)
175{
176 if (label && str_ident)
Michael Hennerich8c613622007-08-03 17:48:09 +0800177 return strncmp(str_ident[ident].name,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800178 label, strlen(label));
179 else
180 return -EINVAL;
181}
182
Bryan Wu1394f032007-05-06 14:50:22 -0700183#ifdef BF537_FAMILY
Mike Frysingera161bb02007-05-21 18:09:14 +0800184static void port_setup(unsigned short gpio, unsigned short usage)
Bryan Wu1394f032007-05-06 14:50:22 -0700185{
Michael Hennerichcda6a202007-10-04 00:36:18 +0800186 if (!check_gpio(gpio)) {
187 if (usage == GPIO_USAGE) {
188 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
189 } else
190 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
191 SSYNC();
192 }
Bryan Wu1394f032007-05-06 14:50:22 -0700193}
194#else
195# define port_setup(...) do { } while (0)
196#endif
197
Michael Hennerichc58c2142007-10-04 00:35:05 +0800198#ifdef BF537_FAMILY
199
Michael Hennerich8c613622007-08-03 17:48:09 +0800200static struct {
201 unsigned short res;
202 unsigned short offset;
203} port_mux_lut[] = {
204 {.res = P_PPI0_D13, .offset = 11},
205 {.res = P_PPI0_D14, .offset = 11},
206 {.res = P_PPI0_D15, .offset = 11},
207 {.res = P_SPORT1_TFS, .offset = 11},
208 {.res = P_SPORT1_TSCLK, .offset = 11},
209 {.res = P_SPORT1_DTPRI, .offset = 11},
210 {.res = P_PPI0_D10, .offset = 10},
211 {.res = P_PPI0_D11, .offset = 10},
212 {.res = P_PPI0_D12, .offset = 10},
213 {.res = P_SPORT1_RSCLK, .offset = 10},
214 {.res = P_SPORT1_RFS, .offset = 10},
215 {.res = P_SPORT1_DRPRI, .offset = 10},
216 {.res = P_PPI0_D8, .offset = 9},
217 {.res = P_PPI0_D9, .offset = 9},
218 {.res = P_SPORT1_DRSEC, .offset = 9},
219 {.res = P_SPORT1_DTSEC, .offset = 9},
220 {.res = P_TMR2, .offset = 8},
221 {.res = P_PPI0_FS3, .offset = 8},
222 {.res = P_TMR3, .offset = 7},
223 {.res = P_SPI0_SSEL4, .offset = 7},
224 {.res = P_TMR4, .offset = 6},
225 {.res = P_SPI0_SSEL5, .offset = 6},
226 {.res = P_TMR5, .offset = 5},
227 {.res = P_SPI0_SSEL6, .offset = 5},
228 {.res = P_UART1_RX, .offset = 4},
229 {.res = P_UART1_TX, .offset = 4},
230 {.res = P_TMR6, .offset = 4},
231 {.res = P_TMR7, .offset = 4},
232 {.res = P_UART0_RX, .offset = 3},
233 {.res = P_UART0_TX, .offset = 3},
234 {.res = P_DMAR0, .offset = 3},
235 {.res = P_DMAR1, .offset = 3},
236 {.res = P_SPORT0_DTSEC, .offset = 1},
237 {.res = P_SPORT0_DRSEC, .offset = 1},
238 {.res = P_CAN0_RX, .offset = 1},
239 {.res = P_CAN0_TX, .offset = 1},
240 {.res = P_SPI0_SSEL7, .offset = 1},
241 {.res = P_SPORT0_TFS, .offset = 0},
242 {.res = P_SPORT0_DTPRI, .offset = 0},
243 {.res = P_SPI0_SSEL2, .offset = 0},
244 {.res = P_SPI0_SSEL3, .offset = 0},
Michael Hennerichc58c2142007-10-04 00:35:05 +0800245};
246
247static void portmux_setup(unsigned short per, unsigned short function)
248{
Michael Hennerich8c613622007-08-03 17:48:09 +0800249 u16 y, offset, muxreg;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800250
Michael Hennerich8c613622007-08-03 17:48:09 +0800251 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
252 if (port_mux_lut[y].res == per) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800253
254 /* SET PORTMUX REG */
255
Michael Hennerich8c613622007-08-03 17:48:09 +0800256 offset = port_mux_lut[y].offset;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800257 muxreg = bfin_read_PORT_MUX();
258
259 if (offset != 1) {
260 muxreg &= ~(1 << offset);
261 } else {
262 muxreg &= ~(3 << 1);
263 }
264
265 muxreg |= (function << offset);
266 bfin_write_PORT_MUX(muxreg);
267 }
268 }
269}
270
271#else
272# define portmux_setup(...) do { } while (0)
273#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700274
Mike Frysingera161bb02007-05-21 18:09:14 +0800275static void default_gpio(unsigned short gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700276{
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800277 unsigned short bank, bitmask;
Bryan Wu1394f032007-05-06 14:50:22 -0700278
279 bank = gpio_bank(gpio);
280 bitmask = gpio_bit(gpio);
281
282 gpio_bankb[bank]->maska_clear = bitmask;
283 gpio_bankb[bank]->maskb_clear = bitmask;
284 SSYNC();
285 gpio_bankb[bank]->inen &= ~bitmask;
286 gpio_bankb[bank]->dir &= ~bitmask;
287 gpio_bankb[bank]->polar &= ~bitmask;
288 gpio_bankb[bank]->both &= ~bitmask;
289 gpio_bankb[bank]->edge &= ~bitmask;
290}
291
Mike Frysingera161bb02007-05-21 18:09:14 +0800292static int __init bfin_gpio_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700293{
Michael Hennerich8c613622007-08-03 17:48:09 +0800294 str_ident = kcalloc(MAX_RESOURCES,
295 sizeof(struct str_ident), GFP_KERNEL);
296 if (str_ident == NULL)
Michael Hennerichc58c2142007-10-04 00:35:05 +0800297 return -ENOMEM;
Bryan Wu1394f032007-05-06 14:50:22 -0700298
Michael Hennerich8c613622007-08-03 17:48:09 +0800299 memset(str_ident, 0, MAX_RESOURCES * sizeof(struct str_ident));
300
Bryan Wu1394f032007-05-06 14:50:22 -0700301 printk(KERN_INFO "Blackfin GPIO Controller\n");
302
Bryan Wu1394f032007-05-06 14:50:22 -0700303 return 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800304
Bryan Wu1394f032007-05-06 14:50:22 -0700305}
Bryan Wu1394f032007-05-06 14:50:22 -0700306arch_initcall(bfin_gpio_init);
307
308
309/***********************************************************
310*
311* FUNCTIONS: Blackfin General Purpose Ports Access Functions
312*
313* INPUTS/OUTPUTS:
314* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
315*
316*
317* DESCRIPTION: These functions abstract direct register access
318* to Blackfin processor General Purpose
319* Ports Regsiters
320*
321* CAUTION: These functions do not belong to the GPIO Driver API
322*************************************************************
323* MODIFICATION HISTORY :
324**************************************************************/
325
326/* Set a specific bit */
327
328#define SET_GPIO(name) \
329void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
330{ \
331 unsigned long flags; \
Michael Hennerichc58c2142007-10-04 00:35:05 +0800332 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
Bryan Wu1394f032007-05-06 14:50:22 -0700333 local_irq_save(flags); \
334 if (arg) \
335 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
336 else \
337 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
338 local_irq_restore(flags); \
339} \
340EXPORT_SYMBOL(set_gpio_ ## name);
341
342SET_GPIO(dir)
343SET_GPIO(inen)
344SET_GPIO(polar)
345SET_GPIO(edge)
346SET_GPIO(both)
347
348
349#define SET_GPIO_SC(name) \
350void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
351{ \
Michael Hennerichc58c2142007-10-04 00:35:05 +0800352 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
Bryan Wu1394f032007-05-06 14:50:22 -0700353 if (arg) \
354 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
355 else \
356 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
357} \
358EXPORT_SYMBOL(set_gpio_ ## name);
359
360SET_GPIO_SC(maska)
361SET_GPIO_SC(maskb)
362
Mike Frysinger1aafd902007-07-25 11:19:14 +0800363#if ANOMALY_05000311
Bryan Wu1394f032007-05-06 14:50:22 -0700364void set_gpio_data(unsigned short gpio, unsigned short arg)
365{
366 unsigned long flags;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800367 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700368 local_irq_save(flags);
369 if (arg)
370 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
371 else
372 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
373 bfin_read_CHIPID();
374 local_irq_restore(flags);
375}
376EXPORT_SYMBOL(set_gpio_data);
377#else
378SET_GPIO_SC(data)
379#endif
380
381
Mike Frysinger1aafd902007-07-25 11:19:14 +0800382#if ANOMALY_05000311
Bryan Wu1394f032007-05-06 14:50:22 -0700383void set_gpio_toggle(unsigned short gpio)
384{
385 unsigned long flags;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800386 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700387 local_irq_save(flags);
388 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
389 bfin_read_CHIPID();
390 local_irq_restore(flags);
391}
392#else
393void set_gpio_toggle(unsigned short gpio)
394{
Michael Hennerichc58c2142007-10-04 00:35:05 +0800395 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700396 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
397}
398#endif
399EXPORT_SYMBOL(set_gpio_toggle);
400
401
402/*Set current PORT date (16-bit word)*/
403
404#define SET_GPIO_P(name) \
405void set_gpiop_ ## name(unsigned short gpio, unsigned short arg) \
406{ \
407 gpio_bankb[gpio_bank(gpio)]->name = arg; \
408} \
409EXPORT_SYMBOL(set_gpiop_ ## name);
410
411SET_GPIO_P(dir)
412SET_GPIO_P(inen)
413SET_GPIO_P(polar)
414SET_GPIO_P(edge)
415SET_GPIO_P(both)
416SET_GPIO_P(maska)
417SET_GPIO_P(maskb)
418
419
Mike Frysinger1aafd902007-07-25 11:19:14 +0800420#if ANOMALY_05000311
Bryan Wu1394f032007-05-06 14:50:22 -0700421void set_gpiop_data(unsigned short gpio, unsigned short arg)
422{
423 unsigned long flags;
424 local_irq_save(flags);
425 gpio_bankb[gpio_bank(gpio)]->data = arg;
426 bfin_read_CHIPID();
427 local_irq_restore(flags);
428}
429EXPORT_SYMBOL(set_gpiop_data);
430#else
431SET_GPIO_P(data)
432#endif
433
434
435
436/* Get a specific bit */
437
438#define GET_GPIO(name) \
439unsigned short get_gpio_ ## name(unsigned short gpio) \
440{ \
441 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
442} \
443EXPORT_SYMBOL(get_gpio_ ## name);
444
445GET_GPIO(dir)
446GET_GPIO(inen)
447GET_GPIO(polar)
448GET_GPIO(edge)
449GET_GPIO(both)
450GET_GPIO(maska)
451GET_GPIO(maskb)
452
453
Mike Frysinger1aafd902007-07-25 11:19:14 +0800454#if ANOMALY_05000311
Bryan Wu1394f032007-05-06 14:50:22 -0700455unsigned short get_gpio_data(unsigned short gpio)
456{
457 unsigned long flags;
458 unsigned short ret;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800459 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700460 local_irq_save(flags);
461 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->data >> gpio_sub_n(gpio));
462 bfin_read_CHIPID();
463 local_irq_restore(flags);
464 return ret;
465}
466EXPORT_SYMBOL(get_gpio_data);
467#else
468GET_GPIO(data)
469#endif
470
471/*Get current PORT date (16-bit word)*/
472
473#define GET_GPIO_P(name) \
474unsigned short get_gpiop_ ## name(unsigned short gpio) \
475{ \
476 return (gpio_bankb[gpio_bank(gpio)]->name);\
477} \
478EXPORT_SYMBOL(get_gpiop_ ## name);
479
480GET_GPIO_P(dir)
481GET_GPIO_P(inen)
482GET_GPIO_P(polar)
483GET_GPIO_P(edge)
484GET_GPIO_P(both)
485GET_GPIO_P(maska)
486GET_GPIO_P(maskb)
487
Mike Frysinger1aafd902007-07-25 11:19:14 +0800488#if ANOMALY_05000311
Bryan Wu1394f032007-05-06 14:50:22 -0700489unsigned short get_gpiop_data(unsigned short gpio)
490{
491 unsigned long flags;
492 unsigned short ret;
493 local_irq_save(flags);
494 ret = gpio_bankb[gpio_bank(gpio)]->data;
495 bfin_read_CHIPID();
496 local_irq_restore(flags);
497 return ret;
498}
499EXPORT_SYMBOL(get_gpiop_data);
500#else
501GET_GPIO_P(data)
502#endif
503
504#ifdef CONFIG_PM
505/***********************************************************
506*
507* FUNCTIONS: Blackfin PM Setup API
508*
509* INPUTS/OUTPUTS:
510* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
511* type -
512* PM_WAKE_RISING
513* PM_WAKE_FALLING
514* PM_WAKE_HIGH
515* PM_WAKE_LOW
516* PM_WAKE_BOTH_EDGES
517*
518* DESCRIPTION: Blackfin PM Driver API
519*
520* CAUTION:
521*************************************************************
522* MODIFICATION HISTORY :
523**************************************************************/
524int gpio_pm_wakeup_request(unsigned short gpio, unsigned char type)
525{
526 unsigned long flags;
527
528 if ((check_gpio(gpio) < 0) || !type)
529 return -EINVAL;
530
531 local_irq_save(flags);
532
533 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
534 wakeup_flags_map[gpio] = type;
535 local_irq_restore(flags);
536
537 return 0;
538}
539EXPORT_SYMBOL(gpio_pm_wakeup_request);
540
541void gpio_pm_wakeup_free(unsigned short gpio)
542{
543 unsigned long flags;
544
545 if (check_gpio(gpio) < 0)
546 return;
547
548 local_irq_save(flags);
549
550 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
551
552 local_irq_restore(flags);
553}
554EXPORT_SYMBOL(gpio_pm_wakeup_free);
555
556static int bfin_gpio_wakeup_type(unsigned short gpio, unsigned char type)
557{
558 port_setup(gpio, GPIO_USAGE);
559 set_gpio_dir(gpio, 0);
560 set_gpio_inen(gpio, 1);
561
562 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
563 set_gpio_edge(gpio, 1);
564 else
565 set_gpio_edge(gpio, 0);
566
567 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
568 set_gpio_both(gpio, 1);
569 else
570 set_gpio_both(gpio, 0);
571
572 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
573 set_gpio_polar(gpio, 1);
574 else
575 set_gpio_polar(gpio, 0);
576
577 SSYNC();
578
579 return 0;
580}
581
582u32 gpio_pm_setup(void)
583{
584 u32 sic_iwr = 0;
585 u16 bank, mask, i, gpio;
586
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800587 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700588 mask = wakeup_map[gpio_bank(i)];
589 bank = gpio_bank(i);
590
591 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
592 gpio_bankb[bank]->maskb = 0;
593
594 if (mask) {
595#ifdef BF537_FAMILY
596 gpio_bank_saved[bank].fer = *port_fer[bank];
597#endif
598 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
599 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
600 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
601 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
602 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800603 gpio_bank_saved[bank].reserved =
604 reserved_gpio_map[bank];
Bryan Wu1394f032007-05-06 14:50:22 -0700605
606 gpio = i;
607
608 while (mask) {
609 if (mask & 1) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800610 reserved_gpio_map[gpio_bank(gpio)] |=
Michael Hennerich581d62a2007-06-14 13:30:23 +0800611 gpio_bit(gpio);
612 bfin_gpio_wakeup_type(gpio,
613 wakeup_flags_map[gpio]);
Bryan Wu1394f032007-05-06 14:50:22 -0700614 set_gpio_data(gpio, 0); /*Clear*/
615 }
616 gpio++;
617 mask >>= 1;
618 }
619
Michael Hennerich581d62a2007-06-14 13:30:23 +0800620 sic_iwr |= 1 <<
621 (sic_iwr_irqs[bank] - (IRQ_CORETMR + 1));
Bryan Wu1394f032007-05-06 14:50:22 -0700622 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
623 }
624 }
625
626 if (sic_iwr)
627 return sic_iwr;
628 else
629 return IWR_ENABLE_ALL;
630}
631
Bryan Wu1394f032007-05-06 14:50:22 -0700632void gpio_pm_restore(void)
633{
634 u16 bank, mask, i;
635
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800636 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700637 mask = wakeup_map[gpio_bank(i)];
638 bank = gpio_bank(i);
639
640 if (mask) {
641#ifdef BF537_FAMILY
642 *port_fer[bank] = gpio_bank_saved[bank].fer;
643#endif
644 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
645 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
646 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
647 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
648 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
Michael Hennerich581d62a2007-06-14 13:30:23 +0800649
Michael Hennerichc58c2142007-10-04 00:35:05 +0800650 reserved_gpio_map[bank] =
651 gpio_bank_saved[bank].reserved;
Michael Hennerich581d62a2007-06-14 13:30:23 +0800652
Bryan Wu1394f032007-05-06 14:50:22 -0700653 }
654
655 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
656 }
657}
658
659#endif
660
Michael Hennerichc58c2142007-10-04 00:35:05 +0800661
662
663
664int peripheral_request(unsigned short per, const char *label)
665{
666 unsigned long flags;
667 unsigned short ident = P_IDENT(per);
668
669 /*
670 * Don't cares are pins with only one dedicated function
671 */
672
673 if (per & P_DONTCARE)
674 return 0;
675
676 if (!(per & P_DEFINED))
677 return -ENODEV;
678
Michael Hennerichc58c2142007-10-04 00:35:05 +0800679 local_irq_save(flags);
680
Michael Hennerichcda6a202007-10-04 00:36:18 +0800681 if (!check_gpio(ident)) {
682
Michael Hennerichc58c2142007-10-04 00:35:05 +0800683 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
684 printk(KERN_ERR
685 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
686 __FUNCTION__, ident, get_label(ident));
687 dump_stack();
688 local_irq_restore(flags);
689 return -EBUSY;
690 }
691
Michael Hennerichcda6a202007-10-04 00:36:18 +0800692 }
693
Michael Hennerichc58c2142007-10-04 00:35:05 +0800694 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
695
696 /*
697 * Pin functions like AMC address strobes my
698 * be requested and used by several drivers
699 */
700
701 if (!(per & P_MAYSHARE)) {
702
703 /*
704 * Allow that the identical pin function can
705 * be requested from the same driver twice
706 */
707
708 if (cmp_label(ident, label) == 0)
709 goto anyway;
710
711 printk(KERN_ERR
712 "%s: Peripheral %d function %d is already"
Michael Hennerich8c613622007-08-03 17:48:09 +0800713 " reserved by %s !\n",
Michael Hennerichc58c2142007-10-04 00:35:05 +0800714 __FUNCTION__, ident, P_FUNCT2MUX(per),
715 get_label(ident));
716 dump_stack();
717 local_irq_restore(flags);
718 return -EBUSY;
719 }
720
721 }
722
723anyway:
724
725
726 portmux_setup(per, P_FUNCT2MUX(per));
727
728 port_setup(ident, PERIPHERAL_USAGE);
729
730 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
731 local_irq_restore(flags);
732 set_label(ident, label);
733
734 return 0;
735}
736EXPORT_SYMBOL(peripheral_request);
737
738int peripheral_request_list(unsigned short per[], const char *label)
739{
740 u16 cnt;
741 int ret;
742
743 for (cnt = 0; per[cnt] != 0; cnt++) {
Michael Hennerich314c98d2007-07-24 18:03:45 +0800744
Michael Hennerichc58c2142007-10-04 00:35:05 +0800745 ret = peripheral_request(per[cnt], label);
Michael Hennerich314c98d2007-07-24 18:03:45 +0800746
747 if (ret < 0) {
748 for ( ; cnt > 0; cnt--) {
749 peripheral_free(per[cnt - 1]);
750 }
751 return ret;
752 }
Michael Hennerichc58c2142007-10-04 00:35:05 +0800753 }
754
755 return 0;
756}
757EXPORT_SYMBOL(peripheral_request_list);
758
759void peripheral_free(unsigned short per)
760{
761 unsigned long flags;
762 unsigned short ident = P_IDENT(per);
763
764 if (per & P_DONTCARE)
765 return;
766
767 if (!(per & P_DEFINED))
768 return;
769
770 if (check_gpio(ident) < 0)
771 return;
772
773 local_irq_save(flags);
774
775 if (unlikely(!(reserved_peri_map[gpio_bank(ident)]
776 & gpio_bit(ident)))) {
777 local_irq_restore(flags);
778 return;
779 }
780
781 if (!(per & P_MAYSHARE)) {
782 port_setup(ident, GPIO_USAGE);
783 }
784
785 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
786
787 local_irq_restore(flags);
788}
789EXPORT_SYMBOL(peripheral_free);
790
791void peripheral_free_list(unsigned short per[])
792{
793 u16 cnt;
794
795 for (cnt = 0; per[cnt] != 0; cnt++) {
796 peripheral_free(per[cnt]);
797 }
798
799}
800EXPORT_SYMBOL(peripheral_free_list);
801
Bryan Wu1394f032007-05-06 14:50:22 -0700802/***********************************************************
803*
804* FUNCTIONS: Blackfin GPIO Driver
805*
806* INPUTS/OUTPUTS:
807* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
808*
809*
810* DESCRIPTION: Blackfin GPIO Driver API
811*
812* CAUTION:
813*************************************************************
814* MODIFICATION HISTORY :
815**************************************************************/
816
817int gpio_request(unsigned short gpio, const char *label)
818{
819 unsigned long flags;
820
821 if (check_gpio(gpio) < 0)
822 return -EINVAL;
823
824 local_irq_save(flags);
825
Michael Hennerichc58c2142007-10-04 00:35:05 +0800826 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Bryan Wu1394f032007-05-06 14:50:22 -0700827 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio);
828 dump_stack();
829 local_irq_restore(flags);
830 return -EBUSY;
831 }
Michael Hennerichc58c2142007-10-04 00:35:05 +0800832 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -0700833
834 local_irq_restore(flags);
835
836 port_setup(gpio, GPIO_USAGE);
837
838 return 0;
839}
840EXPORT_SYMBOL(gpio_request);
841
Bryan Wu1394f032007-05-06 14:50:22 -0700842void gpio_free(unsigned short gpio)
843{
844 unsigned long flags;
845
846 if (check_gpio(gpio) < 0)
847 return;
848
849 local_irq_save(flags);
850
Michael Hennerichc58c2142007-10-04 00:35:05 +0800851 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
Bryan Wu1394f032007-05-06 14:50:22 -0700852 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
853 dump_stack();
854 local_irq_restore(flags);
855 return;
856 }
857
858 default_gpio(gpio);
859
Michael Hennerichc58c2142007-10-04 00:35:05 +0800860 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -0700861
862 local_irq_restore(flags);
863}
864EXPORT_SYMBOL(gpio_free);
865
Bryan Wu1394f032007-05-06 14:50:22 -0700866void gpio_direction_input(unsigned short gpio)
867{
868 unsigned long flags;
869
Michael Hennerichc58c2142007-10-04 00:35:05 +0800870 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700871
872 local_irq_save(flags);
873 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
874 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
875 local_irq_restore(flags);
876}
877EXPORT_SYMBOL(gpio_direction_input);
878
879void gpio_direction_output(unsigned short gpio)
880{
881 unsigned long flags;
882
Michael Hennerichc58c2142007-10-04 00:35:05 +0800883 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700884
885 local_irq_save(flags);
886 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
887 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
888 local_irq_restore(flags);
889}
890EXPORT_SYMBOL(gpio_direction_output);