aboutsummaryrefslogtreecommitdiff
path: root/drivers/pinctrl/pinctrl-apple.c
blob: 62476358c349d5ce894f4ce240b9a4852bc19d41 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2021 Mark Kettenis <kettenis@openbsd.org>
 */

#include <common.h>
#include <dm.h>
#include <dm/device-internal.h>
#include <dm/pinctrl.h>
#include <dt-bindings/pinctrl/apple.h>
#include <asm/io.h>
#include <asm-generic/gpio.h>
#include <linux/bitfield.h>

struct apple_pinctrl_priv {
	void *base;
	int pin_count;
};

#define REG_GPIO(x)	(4 * (x))
#define  REG_GPIO_DATA		BIT(0)
#define  REG_GPIO_MODE		GENMASK(3, 1)
#define  REG_GPIO_OUT		1
#define  REG_GPIO_PERIPH	GENMASK(6, 5)
#define  REG_GPIO_INPUT_ENABLE	BIT(9)

static void apple_pinctrl_config_pin(struct apple_pinctrl_priv *priv,
				     unsigned pin, u32 clr, u32 set)
{
	unsigned reg = REG_GPIO(pin);
	u32 old, new;

	old = readl(priv->base + REG_GPIO(pin));
	new = (old & ~clr) | set;
	writel(new, priv->base + reg);
}

static int apple_gpio_get_value(struct udevice *dev, unsigned offset)
{
	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);

	return !!(readl(priv->base + REG_GPIO(offset)) & REG_GPIO_DATA);
}

static int apple_gpio_set_value(struct udevice *dev, unsigned offset,
				int value)
{
	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);

	apple_pinctrl_config_pin(priv, offset, REG_GPIO_DATA,
				 value ? REG_GPIO_DATA : 0);
	return 0;
}

static int apple_gpio_get_direction(struct udevice *dev, unsigned offset)
{
	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
	u32 reg = readl(priv->base + REG_GPIO(offset));

	if (FIELD_GET(REG_GPIO_MODE, reg) == REG_GPIO_OUT)
		return GPIOF_OUTPUT;
	else
		return GPIOF_INPUT;
}

static int apple_gpio_direction_input(struct udevice *dev, unsigned offset)
{
	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);

	apple_pinctrl_config_pin(priv, offset,
				 REG_GPIO_PERIPH | REG_GPIO_MODE,
				 REG_GPIO_INPUT_ENABLE);
	return 0;
}

static int apple_gpio_direction_output(struct udevice *dev, unsigned offset,
				       int value)
{
	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
	u32 set = (value ? REG_GPIO_DATA : 0);

	apple_pinctrl_config_pin(priv, offset, REG_GPIO_DATA |
				 REG_GPIO_PERIPH | REG_GPIO_MODE,
				 set | FIELD_PREP(REG_GPIO_MODE, REG_GPIO_OUT));
	return 0;
}

static int apple_gpio_probe(struct udevice *dev)
{
	struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
	struct gpio_dev_priv *uc_priv;

	uc_priv = dev_get_uclass_priv(dev);
	uc_priv->bank_name = "gpio";
	uc_priv->gpio_count = priv->pin_count;

	return 0;
}

static struct dm_gpio_ops apple_gpio_ops = {
	.get_value = apple_gpio_get_value,
	.set_value = apple_gpio_set_value,
	.get_function = apple_gpio_get_direction,
	.direction_input = apple_gpio_direction_input,
	.direction_output = apple_gpio_direction_output,
};

static struct driver apple_gpio_driver = {
	.name = "apple_gpio",
	.id = UCLASS_GPIO,
	.probe = apple_gpio_probe,
	.ops = &apple_gpio_ops,
};

static int apple_pinctrl_get_pins_count(struct udevice *dev)
{
	struct apple_pinctrl_priv *priv = dev_get_priv(dev);

	return priv->pin_count;
}

static const char *apple_pinctrl_get_pin_name(struct udevice *dev,
					      unsigned selector)
{
	static char pin_name[PINNAME_SIZE];

	snprintf(pin_name, PINNAME_SIZE, "pin%d", selector);
	return pin_name;
}

static int apple_pinctrl_get_pin_muxing(struct udevice *dev, unsigned selector,
					char *buf, int size)
{
	struct apple_pinctrl_priv *priv = dev_get_priv(dev);

	if (readl(priv->base + REG_GPIO(selector)) & REG_GPIO_PERIPH)
		strncpy(buf, "periph", size);
	else
		strncpy(buf, "gpio", size);
	return 0;
}

static int apple_pinctrl_pinmux_set(struct udevice *dev, unsigned pin_selector,
				    unsigned func_selector)
{
	struct apple_pinctrl_priv *priv = dev_get_priv(dev);

	apple_pinctrl_config_pin(priv, pin_selector,
				 REG_GPIO_DATA | REG_GPIO_MODE,
				 FIELD_PREP(REG_GPIO_PERIPH, func_selector) |
				 REG_GPIO_INPUT_ENABLE);
	return 0;
}

static int apple_pinctrl_pinmux_property_set(struct udevice *dev,
					     u32 pinmux_group)
{
	unsigned pin_selector = APPLE_PIN(pinmux_group);
	unsigned func_selector = APPLE_FUNC(pinmux_group);
	int ret;

	ret = apple_pinctrl_pinmux_set(dev, pin_selector, func_selector);
	return ret ? ret : pin_selector;
}

static int apple_pinctrl_probe(struct udevice *dev)
{
	struct apple_pinctrl_priv *priv = dev_get_priv(dev);
	struct ofnode_phandle_args args;
	struct udevice *child;

	priv->base = dev_read_addr_ptr(dev);
	if (!priv->base)
		return -EINVAL;

	if (!dev_read_phandle_with_args(dev, "gpio-ranges",
					NULL, 3, 0, &args))
		priv->pin_count = args.args[2];

	device_bind(dev, &apple_gpio_driver, "apple_gpio", NULL,
		    dev_ofnode(dev), &child);

	return 0;
}

static struct pinctrl_ops apple_pinctrl_ops = {
	.set_state = pinctrl_generic_set_state,
	.get_pins_count = apple_pinctrl_get_pins_count,
	.get_pin_name = apple_pinctrl_get_pin_name,
	.pinmux_set = apple_pinctrl_pinmux_set,
	.pinmux_property_set = apple_pinctrl_pinmux_property_set,
	.get_pin_muxing = apple_pinctrl_get_pin_muxing,
};

static const struct udevice_id apple_pinctrl_ids[] = {
	{ .compatible = "apple,pinctrl" },
	{ /* sentinel */ }
};

U_BOOT_DRIVER(pinctrl_apple) = {
	.name = "apple_pinctrl",
	.id = UCLASS_PINCTRL,
	.of_match = apple_pinctrl_ids,
	.priv_auto = sizeof(struct apple_pinctrl_priv),
	.ops = &apple_pinctrl_ops,
	.probe = apple_pinctrl_probe,
};