summaryrefslogtreecommitdiff
path: root/arch/arm/mach-ux500/pins.c
blob: 4738103646d4266eab7488e8398af4e789dca5be (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
 * License terms: GNU General Public License (GPL), version 2
 */

#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/err.h>
#include <plat/pincfg.h>

#include "pins.h"

static LIST_HEAD(pin_lookups);
static DEFINE_MUTEX(pin_lookups_mutex);
static DEFINE_SPINLOCK(pins_lock);

void __init ux500_pins_add(struct ux500_pin_lookup *pl, size_t num)
{
	mutex_lock(&pin_lookups_mutex);

	while (num--) {
		list_add_tail(&pl->node, &pin_lookups);
		pl++;
	}

	mutex_unlock(&pin_lookups_mutex);
}

struct ux500_pins *ux500_pins_get(const char *name)
{
	struct ux500_pins *pins = NULL;
	struct ux500_pin_lookup *pl;

	mutex_lock(&pin_lookups_mutex);

	list_for_each_entry(pl, &pin_lookups, node) {
		if (!strcmp(pl->name, name)) {
			pins = pl->pins;
			goto out;
		}
	}

out:
	mutex_unlock(&pin_lookups_mutex);
	return pins;
}

int ux500_pins_enable(struct ux500_pins *pins)
{
	unsigned long flags;
	int ret = 0;

	spin_lock_irqsave(&pins_lock, flags);

	if (pins->usage++ == 0)
		ret = nmk_config_pins(pins->cfg, pins->num);

	spin_unlock_irqrestore(&pins_lock, flags);
	return ret;
}

int ux500_pins_disable(struct ux500_pins *pins)
{
	unsigned long flags;
	int ret = 0;

	spin_lock_irqsave(&pins_lock, flags);

	if (WARN_ON(pins->usage == 0))
		goto out;

	if (--pins->usage == 0)
		ret = nmk_config_pins_sleep(pins->cfg, pins->num);

out:
	spin_unlock_irqrestore(&pins_lock, flags);
	return ret;
}

void ux500_pins_put(struct ux500_pins *pins)
{
	WARN_ON(!pins);
}

#ifdef CONFIG_DEBUG_FS
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/gpio/nomadik.h>

#include <mach/gpio.h>

static void show_pin(struct seq_file *s, pin_cfg_t pin)
{
	static const char *afnames[] = {
		[NMK_GPIO_ALT_GPIO]	= "GPIO",
		[NMK_GPIO_ALT_A]	= "A",
		[NMK_GPIO_ALT_B]	= "B",
		[NMK_GPIO_ALT_C]	= "C"
	};
	static const char *pullnames[] = {
		[NMK_GPIO_PULL_NONE]	= "none",
		[NMK_GPIO_PULL_UP]	= "up",
		[NMK_GPIO_PULL_DOWN]	= "down",
		[3] /* illegal */	= "??"
	};

	int pin_num = PIN_NUM(pin);
	int pull = PIN_PULL(pin);
	int af = PIN_ALT(pin);
	int slpm = PIN_SLPM(pin);
	int output = PIN_DIR(pin);
	int val = PIN_VAL(pin);
	int slpm_pull = PIN_SLPM_PULL(pin);
	int slpm_dir = PIN_SLPM_DIR(pin);
	int slpm_val = PIN_SLPM_VAL(pin);

	seq_printf(s,
		   "  pin %d [%#lx]: af %s, pull %s (%s%s) - slpm: %s%s%s%s\n",
		   pin_num, pin, afnames[af],
		   pullnames[pull],
		   output ? "output " : "input",
		   output ? (val ? "high" : "low") : "",
		   slpm ? "no-change/no-wakeup " : "input/wakeup ",
		   slpm_dir ? (slpm_dir == 1 ? "input " : "output " ) : "",
		   slpm_dir == 1 ? (slpm_pull == 0 ? "pull: none ":
				    (slpm_pull == NMK_GPIO_PULL_UP ?
				     "pull: up " : "pull: down ") ): "",
		   slpm_dir == 2 ? (slpm_val == 1 ? "low " : "high " ) : "");
}

static int pins_dbg_show(struct seq_file *s, void *iter)
{
	struct ux500_pin_lookup *pl;
	int i;
	bool *pins;
	int prev = -2;
	int first = 0;

	pins = kzalloc(sizeof(bool) * NOMADIK_NR_GPIO, GFP_KERNEL);

	mutex_lock(&pin_lookups_mutex);

	list_for_each_entry(pl, &pin_lookups, node) {
		seq_printf(s, "\n%s (%d) usage: %d\n",
			   pl->name, pl->pins->num, pl->pins->usage);
		for (i = 0; i < pl->pins->num; i++) {
			show_pin(s, pl->pins->cfg[i]);
			pins[PIN_NUM(pl->pins->cfg[i])] = true;
		}
	}
	mutex_unlock(&pin_lookups_mutex);

	seq_printf(s, "\nSummary allocated pins:\n");
	for (i = 0; i < NOMADIK_NR_GPIO; i++) {
		if (prev == i - 1) {
			if (pins[i])
				prev = i;
			else
				if (prev > 0) {
					if  (first != prev)
						seq_printf(s, "-%d, ", prev);
					else
						seq_printf(s, ", ");
				}
			continue;
		}
		if (pins[i]) {
			seq_printf(s, "%d", i);
			prev = i;
			first = i;
		}
	}
	if (prev == i - 1 && first != prev)
		seq_printf(s, "-%d", prev);

	seq_printf(s, "\n");

	kfree(pins);

	return 0;
}

static int pins_dbg_open(struct inode *inode,
			  struct file *file)
{
	return single_open(file, pins_dbg_show, inode->i_private);
}

static const struct file_operations pins_fops = {
	.open = pins_dbg_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static int __init pins_dbg_init(void)
{
	(void) debugfs_create_file("pins", S_IRUGO,
				   NULL,
				   NULL,
				   &pins_fops);
	return 0;
}
late_initcall(pins_dbg_init);
#endif