gpio.c 2.9 KB
Newer Older
1 2 3
/*
 * arch/arm/mach-ns9xxx/gpio.c
 *
4
 * Copyright (C) 2006,2007 by Digi International Inc.
5 6 7 8 9 10 11 12 13 14
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */
#include <linux/compiler.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/module.h>
15
#include <linux/bitops.h>
16

17 18 19
#include <mach/gpio.h>
#include <mach/processor.h>
#include <mach/processor-ns9360.h>
20 21 22
#include <asm/bug.h>
#include <asm/types.h>

23 24
#include "gpio-ns9360.h"

25 26 27 28 29 30 31 32 33 34
#if defined(CONFIG_PROCESSOR_NS9360)
#define GPIO_MAX 72
#elif defined(CONFIG_PROCESSOR_NS9750)
#define GPIO_MAX 49
#endif

/* protects BBU_GCONFx and BBU_GCTRLx */
static spinlock_t gpio_lock = __SPIN_LOCK_UNLOCKED(gpio_lock);

/* only access gpiores with atomic ops */
35
static DECLARE_BITMAP(gpiores, GPIO_MAX + 1);
36 37 38 39 40 41 42 43 44 45 46 47 48

static inline int ns9xxx_valid_gpio(unsigned gpio)
{
#if defined(CONFIG_PROCESSOR_NS9360)
	if (processor_is_ns9360())
		return gpio <= 72;
	else
#endif
#if defined(CONFIG_PROCESSOR_NS9750)
	if (processor_is_ns9750())
		return gpio <= 49;
	else
#endif
49
	{
50
		BUG();
51 52
		return 0;
	}
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

int gpio_request(unsigned gpio, const char *label)
{
	if (likely(ns9xxx_valid_gpio(gpio)))
		return test_and_set_bit(gpio, gpiores) ? -EBUSY : 0;
	else
		return -EINVAL;
}
EXPORT_SYMBOL(gpio_request);

void gpio_free(unsigned gpio)
{
	clear_bit(gpio, gpiores);
	return;
}
EXPORT_SYMBOL(gpio_free);

71
int gpio_direction_input(unsigned gpio)
72
{
73 74 75
	if (likely(ns9xxx_valid_gpio(gpio))) {
		int ret = -EINVAL;
		unsigned long flags;
76

77 78 79 80 81 82 83
		spin_lock_irqsave(&gpio_lock, flags);
#if defined(CONFIG_PROCESSOR_NS9360)
		if (processor_is_ns9360())
			ret = __ns9360_gpio_configure(gpio, 0, 0, 3);
		else
#endif
			BUG();
84

85
		spin_unlock_irqrestore(&gpio_lock, flags);
86

87
		return ret;
88 89 90 91 92 93 94 95 96

	} else
		return -EINVAL;
}
EXPORT_SYMBOL(gpio_direction_input);

int gpio_direction_output(unsigned gpio, int value)
{
	if (likely(ns9xxx_valid_gpio(gpio))) {
97 98 99
		int ret = -EINVAL;
		unsigned long flags;

100 101
		gpio_set_value(gpio, value);

102 103 104 105 106 107 108 109 110 111 112
		spin_lock_irqsave(&gpio_lock, flags);
#if defined(CONFIG_PROCESSOR_NS9360)
		if (processor_is_ns9360())
			ret = __ns9360_gpio_configure(gpio, 1, 0, 3);
		else
#endif
			BUG();

		spin_unlock_irqrestore(&gpio_lock, flags);

		return ret;
113 114 115 116 117 118 119
	} else
		return -EINVAL;
}
EXPORT_SYMBOL(gpio_direction_output);

int gpio_get_value(unsigned gpio)
{
120 121 122 123 124 125 126 127 128
#if defined(CONFIG_PROCESSOR_NS9360)
	if (processor_is_ns9360())
		return ns9360_gpio_get_value(gpio);
	else
#endif
	{
		BUG();
		return -EINVAL;
	}
129 130 131 132 133 134 135
}
EXPORT_SYMBOL(gpio_get_value);

void gpio_set_value(unsigned gpio, int value)
{
	unsigned long flags;
	spin_lock_irqsave(&gpio_lock, flags);
136 137 138
#if defined(CONFIG_PROCESSOR_NS9360)
	if (processor_is_ns9360())
		ns9360_gpio_set_value(gpio, value);
139
	else
140 141
#endif
		BUG();
142 143 144 145

	spin_unlock_irqrestore(&gpio_lock, flags);
}
EXPORT_SYMBOL(gpio_set_value);