core.c 11.1 KB
Newer Older
M
Magnus Damm 已提交
1
/*
2
 * SuperH Pin Function Controller support.
M
Magnus Damm 已提交
3 4
 *
 * Copyright (C) 2008 Magnus Damm
5
 * Copyright (C) 2009 - 2012 Paul Mundt
M
Magnus Damm 已提交
6 7 8 9 10
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
11 12

#define DRV_NAME "sh-pfc"
13

14
#include <linux/bitops.h>
M
Magnus Damm 已提交
15
#include <linux/err.h>
16
#include <linux/errno.h>
M
Magnus Damm 已提交
17
#include <linux/io.h>
M
Magnus Damm 已提交
18
#include <linux/ioport.h>
19 20
#include <linux/kernel.h>
#include <linux/module.h>
21
#include <linux/pinctrl/machine.h>
22
#include <linux/platform_device.h>
23
#include <linux/slab.h>
M
Magnus Damm 已提交
24

25 26
#include "core.h"

27
static int sh_pfc_ioremap(struct sh_pfc *pfc, struct platform_device *pdev)
M
Magnus Damm 已提交
28 29 30 31
{
	struct resource *res;
	int k;

32 33
	if (pdev->num_resources == 0)
		return -EINVAL;
M
Magnus Damm 已提交
34

35
	pfc->window = devm_kzalloc(pfc->dev, pdev->num_resources *
L
Laurent Pinchart 已提交
36
				   sizeof(*pfc->window), GFP_NOWAIT);
37
	if (!pfc->window)
L
Laurent Pinchart 已提交
38
		return -ENOMEM;
M
Magnus Damm 已提交
39

40
	pfc->num_windows = pdev->num_resources;
41

42
	for (k = 0, res = pdev->resource; k < pdev->num_resources; k++, res++) {
M
Magnus Damm 已提交
43
		WARN_ON(resource_type(res) != IORESOURCE_MEM);
44 45
		pfc->window[k].phys = res->start;
		pfc->window[k].size = resource_size(res);
46 47 48
		pfc->window[k].virt = devm_ioremap_nocache(pfc->dev, res->start,
							   resource_size(res));
		if (!pfc->window[k].virt)
L
Laurent Pinchart 已提交
49
			return -ENOMEM;
M
Magnus Damm 已提交
50 51 52 53 54
	}

	return 0;
}

55 56
static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc,
					 unsigned long address)
M
Magnus Damm 已提交
57
{
58
	struct sh_pfc_window *window;
59
	unsigned int i;
M
Magnus Damm 已提交
60 61

	/* scan through physical windows and convert address */
62 63
	for (i = 0; i < pfc->num_windows; i++) {
		window = pfc->window + i;
M
Magnus Damm 已提交
64 65 66 67 68 69 70 71 72 73

		if (address < window->phys)
			continue;

		if (address >= (window->phys + window->size))
			continue;

		return window->virt + (address - window->phys);
	}

74
	BUG();
75
	return NULL;
M
Magnus Damm 已提交
76
}
M
Magnus Damm 已提交
77

78
int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
79
{
80 81 82 83
	unsigned int offset;
	unsigned int i;

	if (pfc->info->ranges == NULL)
84
		return pin;
85 86 87 88 89 90

	for (i = 0, offset = 0; i < pfc->info->nr_ranges; ++i) {
		const struct pinmux_range *range = &pfc->info->ranges[i];

		if (pin <= range->end)
			return pin >= range->begin
91
			     ? offset + pin - range->begin : -1;
92 93 94 95

		offset += range->end - range->begin + 1;
	}

96
	return -EINVAL;
97 98
}

L
Laurent Pinchart 已提交
99 100
static int sh_pfc_enum_in_range(pinmux_enum_t enum_id,
				const struct pinmux_range *r)
M
Magnus Damm 已提交
101 102 103 104 105 106 107 108 109 110
{
	if (enum_id < r->begin)
		return 0;

	if (enum_id > r->end)
		return 0;

	return 1;
}

111 112
unsigned long sh_pfc_read_raw_reg(void __iomem *mapped_reg,
				  unsigned long reg_width)
M
Magnus Damm 已提交
113 114 115
{
	switch (reg_width) {
	case 8:
M
Magnus Damm 已提交
116
		return ioread8(mapped_reg);
M
Magnus Damm 已提交
117
	case 16:
M
Magnus Damm 已提交
118
		return ioread16(mapped_reg);
M
Magnus Damm 已提交
119
	case 32:
M
Magnus Damm 已提交
120
		return ioread32(mapped_reg);
M
Magnus Damm 已提交
121 122 123 124 125 126
	}

	BUG();
	return 0;
}

127 128
void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned long reg_width,
			  unsigned long data)
M
Magnus Damm 已提交
129 130 131
{
	switch (reg_width) {
	case 8:
M
Magnus Damm 已提交
132
		iowrite8(data, mapped_reg);
M
Magnus Damm 已提交
133 134
		return;
	case 16:
M
Magnus Damm 已提交
135
		iowrite16(data, mapped_reg);
M
Magnus Damm 已提交
136 137
		return;
	case 32:
M
Magnus Damm 已提交
138
		iowrite32(data, mapped_reg);
M
Magnus Damm 已提交
139 140 141 142 143 144
		return;
	}

	BUG();
}

145
static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
L
Laurent Pinchart 已提交
146
				     const struct pinmux_cfg_reg *crp,
147 148 149 150
				     unsigned long in_pos,
				     void __iomem **mapped_regp,
				     unsigned long *maskp,
				     unsigned long *posp)
M
Magnus Damm 已提交
151
{
152 153
	int k;

154
	*mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
M
Magnus Damm 已提交
155

156 157 158 159 160 161 162 163 164
	if (crp->field_width) {
		*maskp = (1 << crp->field_width) - 1;
		*posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
	} else {
		*maskp = (1 << crp->var_field_width[in_pos]) - 1;
		*posp = crp->reg_width;
		for (k = 0; k <= in_pos; k++)
			*posp -= crp->var_field_width[k];
	}
165 166
}

167
static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
L
Laurent Pinchart 已提交
168
				    const struct pinmux_cfg_reg *crp,
169
				    unsigned long field, unsigned long value)
M
Magnus Damm 已提交
170
{
171
	void __iomem *mapped_reg;
M
Magnus Damm 已提交
172
	unsigned long mask, pos, data;
M
Magnus Damm 已提交
173

174
	sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
M
Magnus Damm 已提交
175

176 177 178
	dev_dbg(pfc->dev, "write_reg addr = %lx, value = %ld, field = %ld, "
		"r_width = %ld, f_width = %ld\n",
		crp->reg, value, field, crp->reg_width, crp->field_width);
M
Magnus Damm 已提交
179 180 181

	mask = ~(mask << pos);
	value = value << pos;
M
Magnus Damm 已提交
182

183
	data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
M
Magnus Damm 已提交
184 185 186
	data &= mask;
	data |= value;

187
	if (pfc->info->unlock_reg)
188
		sh_pfc_write_raw_reg(
189
			sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
190
			~data);
M
Magnus Damm 已提交
191

192
	sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
M
Magnus Damm 已提交
193 194
}

195
static int sh_pfc_get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
L
Laurent Pinchart 已提交
196
				 const struct pinmux_cfg_reg **crp, int *fieldp,
197
				 int *valuep)
M
Magnus Damm 已提交
198
{
L
Laurent Pinchart 已提交
199
	const struct pinmux_cfg_reg *config_reg;
200 201
	unsigned long r_width, f_width, curr_width, ncomb;
	int k, m, n, pos, bit_pos;
M
Magnus Damm 已提交
202 203 204

	k = 0;
	while (1) {
205
		config_reg = pfc->info->cfg_regs + k;
M
Magnus Damm 已提交
206 207 208 209 210 211

		r_width = config_reg->reg_width;
		f_width = config_reg->field_width;

		if (!r_width)
			break;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

		pos = 0;
		m = 0;
		for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
			if (f_width)
				curr_width = f_width;
			else
				curr_width = config_reg->var_field_width[m];

			ncomb = 1 << curr_width;
			for (n = 0; n < ncomb; n++) {
				if (config_reg->enum_ids[pos + n] == enum_id) {
					*crp = config_reg;
					*fieldp = m;
					*valuep = n;
					return 0;
				}
M
Magnus Damm 已提交
229
			}
230 231
			pos += ncomb;
			m++;
M
Magnus Damm 已提交
232 233 234 235
		}
		k++;
	}

236
	return -EINVAL;
M
Magnus Damm 已提交
237 238
}

239 240
static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, pinmux_enum_t mark, int pos,
			      pinmux_enum_t *enum_idp)
M
Magnus Damm 已提交
241
{
L
Laurent Pinchart 已提交
242
	const pinmux_enum_t *data = pfc->info->gpio_data;
M
Magnus Damm 已提交
243 244 245 246 247 248 249
	int k;

	if (pos) {
		*enum_idp = data[pos + 1];
		return pos + 1;
	}

250
	for (k = 0; k < pfc->info->gpio_data_size; k++) {
251
		if (data[k] == mark) {
M
Magnus Damm 已提交
252 253 254 255 256
			*enum_idp = data[k + 1];
			return k + 1;
		}
	}

257 258
	dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
		mark);
259
	return -EINVAL;
M
Magnus Damm 已提交
260 261
}

262
int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
M
Magnus Damm 已提交
263
{
L
Laurent Pinchart 已提交
264
	const struct pinmux_cfg_reg *cr = NULL;
M
Magnus Damm 已提交
265
	pinmux_enum_t enum_id;
L
Laurent Pinchart 已提交
266
	const struct pinmux_range *range;
267
	int in_range, pos, field, value;
268
	int ret;
M
Magnus Damm 已提交
269 270

	switch (pinmux_type) {
271
	case PINMUX_TYPE_GPIO:
M
Magnus Damm 已提交
272 273 274 275 276
	case PINMUX_TYPE_FUNCTION:
		range = NULL;
		break;

	case PINMUX_TYPE_OUTPUT:
277
		range = &pfc->info->output;
M
Magnus Damm 已提交
278 279 280
		break;

	case PINMUX_TYPE_INPUT:
281
		range = &pfc->info->input;
M
Magnus Damm 已提交
282 283 284
		break;

	case PINMUX_TYPE_INPUT_PULLUP:
285
		range = &pfc->info->input_pu;
M
Magnus Damm 已提交
286 287 288
		break;

	case PINMUX_TYPE_INPUT_PULLDOWN:
289
		range = &pfc->info->input_pd;
M
Magnus Damm 已提交
290 291 292
		break;

	default:
293
		return -EINVAL;
M
Magnus Damm 已提交
294 295 296 297
	}

	pos = 0;
	enum_id = 0;
298 299
	field = 0;
	value = 0;
300 301

	/* Iterate over all the configuration fields we need to update. */
M
Magnus Damm 已提交
302
	while (1) {
303
		pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
304 305
		if (pos < 0)
			return pos;
M
Magnus Damm 已提交
306 307 308 309

		if (!enum_id)
			break;

310 311 312 313
		/* Check if the configuration field selects a function. If it
		 * doesn't, skip the field if it's not applicable to the
		 * requested pinmux type.
		 */
314
		in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
315
		if (!in_range) {
316 317 318 319 320 321 322 323
			if (pinmux_type == PINMUX_TYPE_FUNCTION) {
				/* Functions are allowed to modify all
				 * fields.
				 */
				in_range = 1;
			} else if (pinmux_type != PINMUX_TYPE_GPIO) {
				/* Input/output types can only modify fields
				 * that correspond to their respective ranges.
324
				 */
325
				in_range = sh_pfc_enum_in_range(enum_id, range);
326 327 328 329 330 331 332 333 334

				/*
				 * special case pass through for fixed
				 * input-only or output-only pins without
				 * function enum register association.
				 */
				if (in_range && enum_id == range->force)
					continue;
			}
335
			/* GPIOs are only allowed to modify function fields. */
336 337
		}

M
Magnus Damm 已提交
338 339 340
		if (!in_range)
			continue;

341 342 343
		ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
		if (ret < 0)
			return ret;
M
Magnus Damm 已提交
344

345
		sh_pfc_write_config_reg(pfc, cr, field, value);
M
Magnus Damm 已提交
346 347 348 349 350
	}

	return 0;
}

351
static int sh_pfc_probe(struct platform_device *pdev)
M
Magnus Damm 已提交
352
{
L
Laurent Pinchart 已提交
353
	const struct sh_pfc_soc_info *info;
354
	struct sh_pfc *pfc;
M
Magnus Damm 已提交
355
	int ret;
M
Magnus Damm 已提交
356

357 358 359
	info = pdev->id_entry->driver_data
	      ? (void *)pdev->id_entry->driver_data : pdev->dev.platform_data;
	if (info == NULL)
360
		return -ENODEV;
M
Magnus Damm 已提交
361

362
	pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
363 364
	if (pfc == NULL)
		return -ENOMEM;
365

366
	pfc->info = info;
367 368
	pfc->dev = &pdev->dev;

369
	ret = sh_pfc_ioremap(pfc, pdev);
370
	if (unlikely(ret < 0))
M
Magnus Damm 已提交
371 372
		return ret;

373
	spin_lock_init(&pfc->lock);
M
Magnus Damm 已提交
374

375
	pinctrl_provide_dummies();
M
Magnus Damm 已提交
376

377 378 379
	/*
	 * Initialize pinctrl bindings first
	 */
380
	ret = sh_pfc_register_pinctrl(pfc);
381
	if (unlikely(ret != 0))
382
		return ret;
383

384
#ifdef CONFIG_GPIO_SH_PFC
385 386 387
	/*
	 * Then the GPIO chip
	 */
388
	ret = sh_pfc_register_gpiochip(pfc);
389
	if (unlikely(ret != 0)) {
390 391 392 393 394
		/*
		 * If the GPIO chip fails to come up we still leave the
		 * PFC state as it is, given that there are already
		 * extant users of it that have succeeded by this point.
		 */
395
		dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
396
	}
397
#endif
398

399 400
	platform_set_drvdata(pdev, pfc);

401
	dev_info(pfc->dev, "%s support registered\n", info->name);
402

403
	return 0;
404
}
405

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
static int sh_pfc_remove(struct platform_device *pdev)
{
	struct sh_pfc *pfc = platform_get_drvdata(pdev);

#ifdef CONFIG_GPIO_SH_PFC
	sh_pfc_unregister_gpiochip(pfc);
#endif
	sh_pfc_unregister_pinctrl(pfc);

	platform_set_drvdata(pdev, NULL);

	return 0;
}

static const struct platform_device_id sh_pfc_id_table[] = {
M
Magnus Damm 已提交
421 422 423
#ifdef CONFIG_PINCTRL_PFC_R8A73A4
	{ "pfc-r8a73a4", (kernel_ulong_t)&r8a73a4_pinmux_info },
#endif
424 425
#ifdef CONFIG_PINCTRL_PFC_R8A7740
	{ "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
426 427 428
#endif
#ifdef CONFIG_PINCTRL_PFC_R8A7779
	{ "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
429
#endif
430 431 432
#ifdef CONFIG_PINCTRL_PFC_SH7203
	{ "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
#endif
433 434 435
#ifdef CONFIG_PINCTRL_PFC_SH7264
	{ "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
#endif
436 437 438
#ifdef CONFIG_PINCTRL_PFC_SH7269
	{ "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
#endif
439 440
#ifdef CONFIG_PINCTRL_PFC_SH7372
	{ "pfc-sh7372", (kernel_ulong_t)&sh7372_pinmux_info },
441 442 443
#endif
#ifdef CONFIG_PINCTRL_PFC_SH73A0
	{ "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
444 445 446
#endif
#ifdef CONFIG_PINCTRL_PFC_SH7720
	{ "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
447 448 449
#endif
#ifdef CONFIG_PINCTRL_PFC_SH7722
	{ "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
450 451 452
#endif
#ifdef CONFIG_PINCTRL_PFC_SH7723
	{ "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
453 454 455
#endif
#ifdef CONFIG_PINCTRL_PFC_SH7724
	{ "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
456 457 458
#endif
#ifdef CONFIG_PINCTRL_PFC_SH7734
	{ "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
459 460 461
#endif
#ifdef CONFIG_PINCTRL_PFC_SH7757
	{ "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
462 463 464
#endif
#ifdef CONFIG_PINCTRL_PFC_SH7785
	{ "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
465 466 467
#endif
#ifdef CONFIG_PINCTRL_PFC_SH7786
	{ "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
468 469 470
#endif
#ifdef CONFIG_PINCTRL_PFC_SHX3
	{ "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
471
#endif
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	{ "sh-pfc", 0 },
	{ },
};
MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);

static struct platform_driver sh_pfc_driver = {
	.probe		= sh_pfc_probe,
	.remove		= sh_pfc_remove,
	.id_table	= sh_pfc_id_table,
	.driver		= {
		.name	= DRV_NAME,
		.owner	= THIS_MODULE,
	},
};

487 488 489
static int __init sh_pfc_init(void)
{
	return platform_driver_register(&sh_pfc_driver);
490
}
491
postcore_initcall(sh_pfc_init);
492 493 494 495 496 497 498

static void __exit sh_pfc_exit(void)
{
	platform_driver_unregister(&sh_pfc_driver);
}
module_exit(sh_pfc_exit);

499 500 501
MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
MODULE_LICENSE("GPL v2");