aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-ux500/gpio.c
blob: 34b5624316de7e21d31ebac8c2bf0b5cbb599610 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2010 ST-Ericsson
 * Copyright (C) 2009 STMicroelectronics
 *
 * This file is licensed under  the terms of the GNU General Public
 * License version 2. This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/gpio.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <mach/hardware.h>

static struct gpio_altfun_data *altfun_table;
static int altfun_table_size;
static DEFINE_MUTEX(altfun_lock);

/**
 * stm_gpio_set_altfunctable - set the alternate function table
 * @table: pointer to the altfunction table
 * @size: number of elements in the table
 */
int stm_gpio_set_altfunctable(struct gpio_altfun_data *table, int size)
{
	int ret = 0;

	mutex_lock(&altfun_lock);

	if (altfun_table) {
		ret = -EBUSY;
		goto out;
	}

	altfun_table = table;
	altfun_table_size = size;

out:
	mutex_unlock(&altfun_lock);
	return ret;
}

/**
 * stm_gpio_altfuncenable - enables alternate functions for
 * 			    the given set of GPIOs
 * @alt_func: GPIO altfunction to disable
 *
 * Enable the given set of alternate functions.  Error will be returned if any
 * of the pins or invalid or busy.
 */
int stm_gpio_altfuncenable(gpio_alt_function alt_func)
{
	int i, j, found = false;
	int ret = -EINVAL;
	int start, end;

	mutex_lock(&altfun_lock);
	for (i = 0; i < altfun_table_size; i++) {
		if (altfun_table[i].altfun != alt_func)
			continue;

		start = altfun_table[i].start;
		end = altfun_table[i].end;
		if (start > end) {
			j = start;
			start = end;
			end = j;
		}

		found = true;

		if (!gpio_is_valid(start) || !gpio_is_valid(end)) {
			printk(KERN_ERR
			       "%s: GPIO range %d-%d for %s is not valid for\n",
			       __func__, start, end, altfun_table[i].dev_name);
			goto out;
		}

		for (j = start; j <= end; j++) {
			if (gpio_request(j, altfun_table[i].dev_name) < 0) {
				printk(KERN_ERR
				       "%s:Failed to set %s, GPIO %d is busy\n",
				       __func__, altfun_table[i].dev_name, j);
				ret = -EBUSY;
				goto out;
			}

			nmk_gpio_set_mode(j, altfun_table[i].type);
		}
	}

	if (found)
		ret = 0;

out:
	mutex_unlock(&altfun_lock);
	return ret;
}
EXPORT_SYMBOL(stm_gpio_altfuncenable);

/**
 * stm_gpio_altfuncdisable - disable the alternate functions for
 * 			     the given set of GPIOs.
 * @alt_func: GPIO altfunction enum
 *
 * Disable the given set of alternate functions.  Error will be returned if any
 * of the pins or invalid.
 **/
int stm_gpio_altfuncdisable(gpio_alt_function alt_func)
{
	bool found = false;
	int ret = -EINVAL;
	int i, j;
	int start, end;

	mutex_lock(&altfun_lock);
	for (i = 0; i < altfun_table_size; i++) {
		if (altfun_table[i].altfun != alt_func)
			continue;
		start = altfun_table[i].start;
		end = altfun_table[i].end;
		if (start > end) {
			j = start;
			start = end;
			end = j;
		}

		found = true;

		if (!gpio_is_valid(start) || !gpio_is_valid(end)) {
			printk(KERN_ERR
			       "%s: GPIO range %d-%d for %s is out of bound\n",
			       __func__, start, end, altfun_table[i].dev_name);
			goto out;
		}

		for (j = start; j <= end; j++) {
			/* Reset the pin - input, gpio */
			gpio_direction_input(j);
			nmk_gpio_set_mode(j, NMK_GPIO_ALT_GPIO);
			gpio_free(j);
		}
	}

	if (!found)
		printk(KERN_ERR
		       "%s: Didn't find any GPIO alt interface named '%s'\n",
		       __func__, altfun_table[i].dev_name);

	if (found)
		ret = 0;

out:
	mutex_unlock(&altfun_lock);
	return ret;
}
EXPORT_SYMBOL(stm_gpio_altfuncdisable);