source: src/linux/pb42/linux-2.6.34.6/arch/mips/ar7100/gpio_driver.c @ 18012

Last change on this file since 18012 was 18012, checked in by BrainSlayer, 18 months ago

keep default state

File size: 9.1 KB
Line 
1/*
2 *  Atheros AR7XXX/AR9XXX SoC GPIO API support
3 *
4 *  Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
5 *  Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6 *
7 *  This program is free software; you can redistribute it and/or modify it
8 *  under the terms of the GNU General Public License version 2 as published
9 *  by the Free Software Foundation.
10 */
11
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/spinlock.h>
17#include <linux/io.h>
18#include <linux/ioport.h>
19#include <linux/gpio.h>
20
21#include <asm/mach-ar71xx/ar71xx.h>
22#include "dev-leds-gpio.h"
23
24static DEFINE_SPINLOCK(ar71xx_gpio_lock);
25
26void set_wl0_gpio(int gpio,int val);
27void set_wl1_gpio(int gpio,int val);
28
29unsigned long ar71xx_gpio_count;
30EXPORT_SYMBOL(ar71xx_gpio_count);
31
32void __ar71xx_gpio_set_value(unsigned gpio, int value)
33{
34        void __iomem *base = ar71xx_gpio_base;
35        if (gpio>=48)
36            {
37            set_wl1_gpio(gpio-48,value);
38            return;
39            }
40        if (gpio>=32)
41            {
42            set_wl0_gpio(gpio-32,value);
43            return;
44            }
45        if (value)
46                __raw_writel(1 << gpio, base + GPIO_REG_SET);
47        else
48                __raw_writel(1 << gpio, base + GPIO_REG_CLEAR);
49}
50EXPORT_SYMBOL(__ar71xx_gpio_set_value);
51
52int __ar71xx_gpio_get_value(unsigned gpio)
53{
54        if (gpio>=32)
55            return 0;
56        return (__raw_readl(ar71xx_gpio_base + GPIO_REG_IN) >> gpio) & 1;
57}
58EXPORT_SYMBOL(__ar71xx_gpio_get_value);
59
60static int ar71xx_gpio_get_value(struct gpio_chip *chip, unsigned offset)
61{
62        return __ar71xx_gpio_get_value(offset);
63}
64
65static void ar71xx_gpio_set_value(struct gpio_chip *chip,
66                                  unsigned offset, int value)
67{
68        __ar71xx_gpio_set_value(offset, value);
69}
70
71static int ar71xx_gpio_direction_input(struct gpio_chip *chip,
72                                       unsigned offset)
73{
74        void __iomem *base = ar71xx_gpio_base;
75        unsigned long flags;
76        if (offset>=32)
77            return 0;
78
79        spin_lock_irqsave(&ar71xx_gpio_lock, flags);
80
81        __raw_writel(__raw_readl(base + GPIO_REG_OE) & ~(1 << offset),
82                     base + GPIO_REG_OE);
83
84        spin_unlock_irqrestore(&ar71xx_gpio_lock, flags);
85
86        return 0;
87}
88
89static int ar71xx_gpio_direction_output(struct gpio_chip *chip,
90                                        unsigned offset, int value)
91{
92        void __iomem *base = ar71xx_gpio_base;
93        unsigned long flags;
94        if (offset>=32)
95            return 0;
96
97        spin_lock_irqsave(&ar71xx_gpio_lock, flags);
98
99        if (value)
100                __raw_writel(1 << offset, base + GPIO_REG_SET);
101        else
102                __raw_writel(1 << offset, base + GPIO_REG_CLEAR);
103
104        __raw_writel(__raw_readl(base + GPIO_REG_OE) | (1 << offset),
105                     base + GPIO_REG_OE);
106
107        spin_unlock_irqrestore(&ar71xx_gpio_lock, flags);
108
109        return 0;
110}
111
112static struct gpio_chip ar71xx_gpio_chip = {
113        .label                  = "ar71xx",
114        .get                    = ar71xx_gpio_get_value,
115        .set                    = ar71xx_gpio_set_value,
116        .direction_input        = ar71xx_gpio_direction_input,
117        .direction_output       = ar71xx_gpio_direction_output,
118        .base                   = 0,
119        .ngpio                  = AR71XX_GPIO_COUNT,
120};
121
122void ar71xx_gpio_function_enable(u32 mask)
123{
124        void __iomem *base = ar71xx_gpio_base;
125        unsigned long flags;
126
127        spin_lock_irqsave(&ar71xx_gpio_lock, flags);
128
129        __raw_writel(__raw_readl(base + GPIO_REG_FUNC) | mask,
130                     base + GPIO_REG_FUNC);
131        /* flush write */
132        (void) __raw_readl(base + GPIO_REG_FUNC);
133
134        spin_unlock_irqrestore(&ar71xx_gpio_lock, flags);
135}
136
137void ar71xx_gpio_function_disable(u32 mask)
138{
139        void __iomem *base = ar71xx_gpio_base;
140        unsigned long flags;
141
142        spin_lock_irqsave(&ar71xx_gpio_lock, flags);
143
144        __raw_writel(__raw_readl(base + GPIO_REG_FUNC) & ~mask,
145                     base + GPIO_REG_FUNC);
146        /* flush write */
147        (void) __raw_readl(base + GPIO_REG_FUNC);
148
149        spin_unlock_irqrestore(&ar71xx_gpio_lock, flags);
150}
151
152void ar71xx_gpio_function_setup(u32 set, u32 clear)
153{
154        void __iomem *base = ar71xx_gpio_base;
155        unsigned long flags;
156
157        spin_lock_irqsave(&ar71xx_gpio_lock, flags);
158
159        __raw_writel((__raw_readl(base + GPIO_REG_FUNC) & ~clear) | set,
160                     base + GPIO_REG_FUNC);
161        /* flush write */
162        (void) __raw_readl(base + GPIO_REG_FUNC);
163
164        spin_unlock_irqrestore(&ar71xx_gpio_lock, flags);
165}
166EXPORT_SYMBOL(ar71xx_gpio_function_setup);
167
168static struct gpio_led generic_leds_gpio[] __initdata = {
169        {
170                .name           = "generic_0",
171                .gpio           = 0,
172                .active_low     = 0,
173        },
174        {
175                .name           = "generic_1",
176                .gpio           = 1,
177                .active_low     = 0,
178               
179        },
180        {
181                .name           = "generic_2",
182                .gpio           = 2,
183                .active_low     = 0,
184        },
185        {
186                .name           = "generic_3",
187                .gpio           = 3,
188                .active_low     = 0,
189        },
190        {
191                .name           = "generic_4",
192                .gpio           = 4,
193                .active_low     = 0,
194        },
195        {
196                .name           = "generic_5",
197                .gpio           = 5,
198                .active_low     = 0,
199        },
200        {
201                .name           = "generic_6",
202                .gpio           = 6,
203                .active_low     = 0,
204        },
205        {
206                .name           = "generic_7",
207                .gpio           = 7,
208                .active_low     = 0,
209        },
210        {
211                .name           = "generic_8",
212                .gpio           = 8,
213                .active_low     = 0,
214        },
215        {
216                .name           = "generic_9",
217                .gpio           = 9,
218                .active_low     = 0,
219        },
220        {
221                .name           = "generic_10",
222                .gpio           = 10,
223                .active_low     = 0,
224        },
225        {
226                .name           = "generic_11",
227                .gpio           = 11,
228                .active_low     = 0,
229        },
230        {
231                .name           = "generic_12",
232                .gpio           = 12,
233                .active_low     = 0,
234        },
235        {
236                .name           = "generic_13",
237                .gpio           = 13,
238                .active_low     = 0,
239        },
240        {
241                .name           = "generic_14",
242                .gpio           = 14,
243                .active_low     = 0,
244        },
245        {
246                .name           = "generic_15",
247                .gpio           = 15,
248                .active_low     = 0,
249        },
250        {
251                .name           = "generic_16",
252                .gpio           = 16,
253                .active_low     = 0,
254        },
255        {
256                .name           = "generic_17",
257                .gpio           = 17,
258                .active_low     = 0,
259        },
260        {
261                .name           = "generic_18",
262                .gpio           = 18,
263                .active_low     = 0,
264        },
265        {
266                .name           = "generic_19",
267                .gpio           = 19,
268                .active_low     = 0,
269        },
270        {
271                .name           = "generic_20",
272                .gpio           = 20,
273                .active_low     = 0,
274        },
275        {
276                .name           = "generic_21",
277                .gpio           = 21,
278                .active_low     = 0,
279        },
280        {
281                .name           = "generic_22",
282                .gpio           = 22,
283                .active_low     = 0,
284        },
285        {
286                .name           = "generic_23",
287                .gpio           = 23,
288                .active_low     = 0,
289        },
290        {
291                .name           = "generic_24",
292                .gpio           = 24,
293                .active_low     = 0,
294        },
295        {
296                .name           = "generic_25",
297                .gpio           = 25,
298                .active_low     = 0,
299        },
300        {
301                .name           = "generic_26",
302                .gpio           = 26,
303                .active_low     = 0,
304        },
305        {
306                .name           = "generic_27",
307                .gpio           = 27,
308                .active_low     = 0,
309        },
310        {
311                .name           = "generic_28",
312                .gpio           = 28,
313                .active_low     = 0,
314        },
315        {
316                .name           = "generic_29",
317                .gpio           = 29,
318                .active_low     = 0,
319        },
320        {
321                .name           = "generic_30",
322                .gpio           = 30,
323                .active_low     = 0,
324        },
325        {
326                .name           = "generic_31",
327                .gpio           = 31,
328                .active_low     = 0,
329        },
330
331//wl gpios
332#ifndef CONFIG_AR9100
333        {
334                .name           = "wireless_generic_0",
335                .gpio           = 32,
336                .active_low     = 1,
337        },
338        {
339                .name           = "wireless_generic_1",
340                .gpio           = 33,
341                .active_low     = 1,
342        },
343        {
344                .name           = "wireless_generic_2",
345                .gpio           = 34,
346                .active_low     = 1,
347        },
348        {
349                .name           = "wireless_generic_3",
350                .gpio           = 35,
351                .active_low     = 1,
352        },
353        {
354                .name           = "wireless_generic_4",
355                .gpio           = 36,
356                .active_low     = 1,
357        },
358        {
359                .name           = "wireless_generic_5",
360                .gpio           = 37,
361                .active_low     = 1,
362        },
363        {
364                .name           = "wireless_generic_6",
365                .gpio           = 38,
366                .active_low     = 1,
367        },
368        {
369                .name           = "wireless_generic_7",
370                .gpio           = 39,
371                .active_low     = 1,
372        },
373        {
374                .name           = "wireless_generic_8",
375                .gpio           = 40,
376                .active_low     = 1,
377        },
378        {
379                .name           = "wireless_generic_9",
380                .gpio           = 41,
381                .active_low     = 1,
382        },
383        {
384                .name           = "wireless_generic_10",
385                .gpio           = 42,
386                .active_low     = 1,
387        },
388        {
389                .name           = "wireless_generic_11",
390                .gpio           = 43,
391                .active_low     = 1,
392        },
393        {
394                .name           = "wireless_generic_12",
395                .gpio           = 44,
396                .active_low     = 1,
397        },
398        {
399                .name           = "wireless_generic_13",
400                .gpio           = 45,
401                .active_low     = 1,
402        },
403        {
404                .name           = "wireless_generic_14",
405                .gpio           = 46,
406                .active_low     = 1,
407        },
408        {
409                .name           = "wireless_generic_15",
410                .gpio           = 47,
411                .active_low     = 1,
412        },
413        {
414                .name           = "wireless_generic_16",
415                .gpio           = 48,
416                .active_low     = 1,
417        },
418        {
419                .name           = "wireless_generic_17",
420                .gpio           = 49,
421                .active_low     = 1,
422        },
423        {
424                .name           = "wireless_generic_18",
425                .gpio           = 50,
426                .active_low     = 1,
427        },
428        {
429                .name           = "wireless_generic_19",
430                .gpio           = 51,
431                .active_low     = 1,
432        },
433        {
434                .name           = "wireless_generic_20",
435                .gpio           = 52,
436                .active_low     = 1,
437        },
438        {
439                .name           = "wireless_generic_21",
440                .gpio           = 53,
441                .active_low     = 1,
442        },
443        {
444                .name           = "wireless_generic_22",
445                .gpio           = 54,
446                .active_low     = 1,
447        },
448        {
449                .name           = "wireless_generic_23",
450                .gpio           = 55,
451                .active_low     = 1,
452        },
453        {
454                .name           = "wireless_generic_24",
455                .gpio           = 56,
456                .active_low     = 1,
457        },
458        {
459                .name           = "wireless_generic_25",
460                .gpio           = 57,
461                .active_low     = 1,
462        },
463        {
464                .name           = "wireless_generic_26",
465                .gpio           = 58,
466                .active_low     = 1,
467        },
468        {
469                .name           = "wireless_generic_27",
470                .gpio           = 59,
471                .active_low     = 1,
472        },
473        {
474                .name           = "wireless_generic_28",
475                .gpio           = 60,
476                .active_low     = 1,
477        },
478        {
479                .name           = "wireless_generic_29",
480                .gpio           = 61,
481                .active_low     = 1,
482        },
483        {
484                .name           = "wireless_generic_30",
485                .gpio           = 62,
486                .active_low     = 1,
487        },
488        {
489                .name           = "wireless_generic_31",
490                .gpio           = 63,
491                .active_low     = 1,
492        },
493#endif
494};
495
496
497void __init ar71xx_gpio_init(void)
498{
499        int err;
500        printk(KERN_INFO "Register LED Device\n");
501        if (!request_mem_region(AR71XX_GPIO_BASE, AR71XX_GPIO_SIZE,
502                                "AR71xx GPIO controller"))
503                panic("cannot allocate AR71xx GPIO registers page");
504
505        ar71xx_gpio_chip.ngpio = 64;
506
507        err = gpiochip_add(&ar71xx_gpio_chip);
508        if (err)
509                panic("cannot add AR71xx GPIO chip, error=%d", err);
510        int i;
511        for (i=0;i<sizeof(generic_leds_gpio)/sizeof(struct gpio_led);i++) {
512                generic_leds_gpio[i].default_state = LEDS_GPIO_DEFSTATE_KEEP;
513        }
514
515        ar71xx_add_device_leds_gpio(-1,sizeof(generic_leds_gpio)/sizeof(struct gpio_led),generic_leds_gpio);
516}
Note: See TracBrowser for help on using the repository browser.