summaryrefslogtreecommitdiff
path: root/drivers/gpio/altera_pio.c
blob: 7ceb80e9510f3320956a1253603b0e6e9717a0c6 (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
/*
 * Copyright (C) 2015  Thomas Chou <thomas@wytron.com.tw>
 * Copyright (C) 2011  Missing Link Electronics
 *                     Joachim Foerster <joachim@missinglinkelectronics.com>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */
#include <common.h>
#include <dm.h>
#include <errno.h>
#include <malloc.h>
#include <fdtdec.h>
#include <asm/io.h>
#include <asm/gpio.h>

DECLARE_GLOBAL_DATA_PTR;

struct altera_pio_regs {
	u32	data;			/* Data register */
	u32	direction;		/* Direction register */
};

struct altera_pio_platdata {
	struct altera_pio_regs *regs;
	int gpio_count;
	const char *bank_name;
};

static int altera_pio_direction_input(struct udevice *dev, unsigned pin)
{
	struct altera_pio_platdata *plat = dev_get_platdata(dev);
	struct altera_pio_regs *const regs = plat->regs;

	clrbits_le32(&regs->direction, 1 << pin);

	return 0;
}

static int altera_pio_direction_output(struct udevice *dev, unsigned pin,
				     int val)
{
	struct altera_pio_platdata *plat = dev_get_platdata(dev);
	struct altera_pio_regs *const regs = plat->regs;

	if (val)
		setbits_le32(&regs->data, 1 << pin);
	else
		clrbits_le32(&regs->data, 1 << pin);
	/* change the data first, then the direction. to avoid glitch */
	setbits_le32(&regs->direction, 1 << pin);

	return 0;
}

static int altera_pio_get_value(struct udevice *dev, unsigned pin)
{
	struct altera_pio_platdata *plat = dev_get_platdata(dev);
	struct altera_pio_regs *const regs = plat->regs;

	return readl(&regs->data) & (1 << pin);
}


static int altera_pio_set_value(struct udevice *dev, unsigned pin, int val)
{
	struct altera_pio_platdata *plat = dev_get_platdata(dev);
	struct altera_pio_regs *const regs = plat->regs;

	if (val)
		setbits_le32(&regs->data, 1 << pin);
	else
		clrbits_le32(&regs->data, 1 << pin);

	return 0;
}

static int altera_pio_probe(struct udevice *dev)
{
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
	struct altera_pio_platdata *plat = dev_get_platdata(dev);

	uc_priv->gpio_count = plat->gpio_count;
	uc_priv->bank_name = plat->bank_name;

	return 0;
}

static int altera_pio_ofdata_to_platdata(struct udevice *dev)
{
	struct altera_pio_platdata *plat = dev_get_platdata(dev);

	plat->regs = ioremap(dev_get_addr(dev),
		sizeof(struct altera_pio_regs));
	plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
		"altr,gpio-bank-width", 32);
	plat->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
		"gpio-bank-name", NULL);

	return 0;
}

static const struct dm_gpio_ops altera_pio_ops = {
	.direction_input	= altera_pio_direction_input,
	.direction_output	= altera_pio_direction_output,
	.get_value		= altera_pio_get_value,
	.set_value		= altera_pio_set_value,
};

static const struct udevice_id altera_pio_ids[] = {
	{ .compatible = "altr,pio-1.0" },
	{ }
};

U_BOOT_DRIVER(altera_pio) = {
	.name		= "altera_pio",
	.id		= UCLASS_GPIO,
	.of_match	= altera_pio_ids,
	.ops		= &altera_pio_ops,
	.ofdata_to_platdata = altera_pio_ofdata_to_platdata,
	.platdata_auto_alloc_size = sizeof(struct altera_pio_platdata),
	.probe		= altera_pio_probe,
};