aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-u300/padmux.c
blob: 4c93c6cefd372df8ae6c6bef44c9d5aa9e9de838 (plain) (blame)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 *
 * arch/arm/mach-u300/padmux.c
 *
 *
 * Copyright (C) 2009 ST-Ericsson AB
 * License terms: GNU General Public License (GPL) version 2
 * U300 PADMUX functions
 * Author: Martin Persson <martin.persson@stericsson.com>
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/io.h>
#include <linux/mutex.h>
#include <linux/string.h>
#include <linux/bug.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <mach/u300-regs.h>
#include <mach/syscon.h>
#include "padmux.h"

static DEFINE_MUTEX(pmx_mutex);

const u32 pmx_registers[] = {
	(U300_SYSCON_VBASE + U300_SYSCON_PMC1LR),
	(U300_SYSCON_VBASE + U300_SYSCON_PMC1HR),
	(U300_SYSCON_VBASE + U300_SYSCON_PMC2R),
	(U300_SYSCON_VBASE + U300_SYSCON_PMC3R),
	(U300_SYSCON_VBASE + U300_SYSCON_PMC4R)
};

/* High level functionality */

/* Lazy dog:
 * onmask = {
 *   {"PMC1LR" mask, "PMC1LR" value},
 *   {"PMC1HR" mask, "PMC1HR" value},
 *   {"PMC2R"  mask, "PMC2R"  value},
 *   {"PMC3R"  mask, "PMC3R"  value},
 *   {"PMC4R"  mask, "PMC4R"  value}
 * }
 */
static struct pmx mmc_setting = {
	.setting = U300_APP_PMX_MMC_SETTING,
	.default_on = false,
	.activated = false,
	.name = "MMC",
	.onmask = {
		   {U300_SYSCON_PMC1LR_MMCSD_MASK,
		    U300_SYSCON_PMC1LR_MMCSD_MMCSD},
		   {0, 0},
		   {0, 0},
		   {0, 0},
		   {U300_SYSCON_PMC4R_APP_MISC_12_MASK,
		    U300_SYSCON_PMC4R_APP_MISC_12_APP_GPIO}
		   },
};

static struct pmx spi_setting = {
	.setting = U300_APP_PMX_SPI_SETTING,
	.default_on = false,
	.activated = false,
	.name = "SPI",
	.onmask = {{0, 0},
		   {U300_SYSCON_PMC1HR_APP_SPI_2_MASK |
		    U300_SYSCON_PMC1HR_APP_SPI_CS_1_MASK |
		    U300_SYSCON_PMC1HR_APP_SPI_CS_2_MASK,
		    U300_SYSCON_PMC1HR_APP_SPI_2_SPI |
		    U300_SYSCON_PMC1HR_APP_SPI_CS_1_SPI |
		    U300_SYSCON_PMC1HR_APP_SPI_CS_2_SPI},
		   {0, 0},
		   {0, 0},
		   {0, 0}
		   },
};

/* Available padmux settings */
static struct pmx *pmx_settings[] = {
	&mmc_setting,
	&spi_setting,
};

static void update_registers(struct pmx *pmx, bool activate)
{
	u16 regval, val, mask;
	int i;

	for (i = 0; i < ARRAY_SIZE(pmx_registers); i++) {
		if (activate)
			val = pmx->onmask[i].val;
		else
			val = 0;

		mask = pmx->onmask[i].mask;
		if (mask != 0) {
			regval = readw(pmx_registers[i]);
			regval &= ~mask;
			regval |= val;
			writew(regval, pmx_registers[i]);
		}
	}
}

struct pmx *pmx_get(struct device *dev, enum pmx_settings setting)
{
	int i;
	struct pmx *pmx = ERR_PTR(-ENOENT);

	if (dev == NULL)
		return ERR_PTR(-EINVAL);

	mutex_lock(&pmx_mutex);
	for (i = 0; i < ARRAY_SIZE(pmx_settings); i++) {

		if (setting == pmx_settings[i]->setting) {

			if (pmx_settings[i]->dev != NULL) {
				WARN(1, "padmux: required setting "
				     "in use by another consumer\n");
			} else {
				pmx = pmx_settings[i];
				pmx->dev = dev;
				dev_dbg(dev, "padmux: setting nr %d is now "
					"bound to %s and ready to use\n",
					setting, dev_name(dev));
				break;
			}
		}
	}
	mutex_unlock(&pmx_mutex);

	return pmx;
}
EXPORT_SYMBOL(pmx_get);

int pmx_put(struct device *dev, struct pmx *pmx)
{
	int i;
	int ret = -ENOENT;

	if (pmx == NULL || dev == NULL)
		return -EINVAL;

	mutex_lock(&pmx_mutex);
	for (i = 0; i < ARRAY_SIZE(pmx_settings); i++) {

		if (pmx->setting == pmx_settings[i]->setting) {

			if (dev != pmx->dev) {
				WARN(1, "padmux: cannot release handle as "
					"it is bound to another consumer\n");
				ret = -EINVAL;
				break;
			} else {
				pmx_settings[i]->dev = NULL;
				ret = 0;
				break;
			}
		}
	}
	mutex_unlock(&pmx_mutex);

	return ret;
}
EXPORT_SYMBOL(pmx_put);

int pmx_activate(struct device *dev, struct pmx *pmx)
{
	int i, j, ret;
	ret = 0;

	if (pmx == NULL || dev == NULL)
		return -EINVAL;

	mutex_lock(&pmx_mutex);

	/* Make sure the required bits are not used */
	for (i = 0; i < ARRAY_SIZE(pmx_settings); i++) {

		if (pmx_settings[i]->dev == NULL || pmx_settings[i] == pmx)
			continue;

		for (j = 0; j < ARRAY_SIZE(pmx_registers); j++) {

			if (pmx_settings[i]->onmask[j].mask & pmx->
				onmask[j].mask) {
				/* More than one entry on the same bits */
				WARN(1, "padmux: cannot activate "
					"setting. Bit conflict with "
					"an active setting\n");

				ret = -EUSERS;
				goto exit;
			}
		}
	}
	update_registers(pmx, true);
	pmx->activated = true;
	dev_dbg(dev, "padmux: setting nr %d is activated\n",
		pmx->setting);

exit:
	mutex_unlock(&pmx_mutex);
	return ret;
}
EXPORT_SYMBOL(pmx_activate);

int pmx_deactivate(struct device *dev, struct pmx *pmx)
{
	int i;
	int ret = -ENOENT;

	if (pmx == NULL || dev == NULL)
		return -EINVAL;

	mutex_lock(&pmx_mutex);
	for (i = 0; i < ARRAY_SIZE(pmx_settings); i++) {

		if (pmx_settings[i]->dev == NULL)
			continue;

		if (pmx->setting == pmx_settings[i]->setting) {

			if (dev != pmx->dev) {
				WARN(1, "padmux: cannot deactivate "
				     "pmx setting as it was activated "
				     "by another consumer\n");

				ret = -EBUSY;
				continue;
			} else {
				update_registers(pmx, false);
				pmx_settings[i]->dev = NULL;
				pmx->activated = false;
				ret = 0;
				dev_dbg(dev, "padmux: setting nr %d is deactivated",
					pmx->setting);
				break;
			}
		}
	}
	mutex_unlock(&pmx_mutex);

	return ret;
}
EXPORT_SYMBOL(pmx_deactivate);

/*
 * For internal use only. If it is to be exported,
 * it should be reentrant. Notice that pmx_activate
 * (i.e. runtime settings) always override default settings.
 */
static int pmx_set_default(void)
{
	/* Used to identify several entries on the same bits */
	u16 modbits[ARRAY_SIZE(pmx_registers)];

	int i, j;

	memset(modbits, 0, ARRAY_SIZE(pmx_registers) * sizeof(u16));

	for (i = 0; i < ARRAY_SIZE(pmx_settings); i++) {

		if (!pmx_settings[i]->default_on)
			continue;

		for (j = 0; j < ARRAY_SIZE(pmx_registers); j++) {

			/* Make sure there is only one entry on the same bits */
			if (modbits[j] & pmx_settings[i]->onmask[j].mask) {
				BUG();
				return -EUSERS;
			}
			modbits[j] |= pmx_settings[i]->onmask[j].mask;
		}
		update_registers(pmx_settings[i], true);
	}
	return 0;
}

#if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_U300_DEBUG))
static int pmx_show(struct seq_file *s, void *data)
{
	int i;
	seq_printf(s, "-------------------------------------------------\n");
	seq_printf(s, "SETTING     BOUND TO DEVICE               STATE\n");
	seq_printf(s, "-------------------------------------------------\n");
	mutex_lock(&pmx_mutex);
	for (i = 0; i < ARRAY_SIZE(pmx_settings); i++) {
		/* Format pmx and device name nicely */
		char cdp[33];
		int chars;

		chars = snprintf(&cdp[0], 17, "%s", pmx_settings[i]->name);
		while (chars < 16) {
			cdp[chars] = ' ';
			chars++;
		}
		chars = snprintf(&cdp[16], 17, "%s", pmx_settings[i]->dev ?
				dev_name(pmx_settings[i]->dev) : "N/A");
		while (chars < 16) {
			cdp[chars+16] = ' ';
			chars++;
		}
		cdp[32] = '\0';

		seq_printf(s,
			"%s\t%s\n",
			&cdp[0],
			pmx_settings[i]->activated ?
			"ACTIVATED" : "DEACTIVATED"
			);

	}
	mutex_unlock(&pmx_mutex);
	return 0;
}

static int pmx_open(struct inode *inode, struct file *file)
{
	return single_open(file, pmx_show, NULL);
}

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

static int __init init_pmx_read_debugfs(void)
{
	/* Expose a simple debugfs interface to view pmx settings */
	(void) debugfs_create_file("padmux", S_IFREG | S_IRUGO,
				   NULL, NULL,
				   &pmx_operations);
	return 0;
}

/*
 * This needs to come in after the core_initcall(),
 * because debugfs is not available until
 * the subsystems come up.
 */
module_init(init_pmx_read_debugfs);
#endif

static int __init pmx_init(void)
{
	int ret;

	ret = pmx_set_default();

	if (IS_ERR_VALUE(ret))
		pr_crit("padmux: default settings could not be set\n");

	return 0;
}

/* Should be initialized before consumers */
core_initcall(pmx_init);