summaryrefslogtreecommitdiff
path: root/drivers/pinctrl/sunxi/pinctrl-sunxi.c
blob: dc1a1f74090a7c3773c5ce7d03ef6b5dcdba54b6 (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
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
/*
 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
 *
 * In parts based on linux/drivers/pinctrl/pinctrl-sunxi.c, which is
 *   Copyright (C) 2012 Maxime Ripard
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */



#include <common.h>
#include <dm.h>
#include <errno.h>
#include <syscon.h>
#include <asm/io.h>
#include <asm/arch/clock.h>
#include <asm/gpio.h>
#if defined(CONFIG_DM_GPIO) && !defined(CONFIG_DM_ALLOW_MULTIPLE_DRIVERS)
#include <asm/arch/gpio-internal.h>
#endif
#include <dm/device-internal.h>
#include <dm/lists.h>
#include <dm/pinctrl.h>
#include <dt-bindings/pinctrl/sun4i-a10.h>
#include <dt-bindings/gpio/gpio.h>
#include <linux/kernel.h>
#include "pinctrl-sunxi.h"

DECLARE_GLOBAL_DATA_PTR;

struct sunxi_pctrl_priv {
	void *base;
#if defined(CONFIG_DM_GPIO) && !defined(CONFIG_DM_ALLOW_MULTIPLE_DRIVERS)
	struct sunxi_gpio_soc_data gpio_soc_data;
	struct udevice *gpio_dev;
#endif
};

static int sunxi_pctrl_parse_drive_prop(const void *blob, int node)
{
	int val;

	/* Try the new style binding */
	val = fdtdec_get_int(blob, node, "drive-strength", -EINVAL);
	if (val >= 0) {
		/* We can't go below 10mA ... */
		if (val < 10)
			return -EINVAL;

		/* ... and only up to 40 mA ... */
		if (val > 40)
			val = 40;

		/* by steps of 10 mA */
		return rounddown(val, 10);
	}

	/* And then fall back to the old binding */
	val = fdtdec_get_int(blob, node, "allwinner,drive", -EINVAL);
	if (val < 0)
		return -EINVAL;

	return (val + 1) * 10;
}

static int sunxi_pctrl_parse_bias_prop(const void *blob, int node)
{
	/* Try the new style binding */
	if (fdtdec_get_bool(blob, node, "bias-pull-up"))
		return SUN4I_PINCTRL_PULL_UP;

	if (fdtdec_get_bool(blob, node, "bias-pull-down"))
		return SUN4I_PINCTRL_PULL_DOWN;

	if (fdtdec_get_bool(blob, node, "bias-disable"))
		return SUN4I_PINCTRL_NO_PULL;

	/* And fall back to the old binding */
	return fdtdec_get_int(blob, node, "allwinner,pull", -EINVAL);
}

static const struct sunxi_desc_pin *sunxi_pctrl_pin_by_name(struct udevice *dev,
							    const char *name)
{
	const struct sunxi_pinctrl_desc *data =
		(struct sunxi_pinctrl_desc *)dev_get_driver_data(dev);
	int i;

	for (i = 0; i < data->npins; ++i)
		if (!strcmp(data->pins[i].pin.name, name))
			return &data->pins[i];

	return NULL;
}

static int sunxi_pctrl_muxval_by_name(const struct sunxi_desc_pin *pin,
				      const char *name)
{
	const struct sunxi_desc_function *func;

	if (!pin)
		return -EINVAL;

	for (func = pin->functions; func->name; func++)
		if (!strcmp(func->name, name))
			return func->muxval;

	return -ENOENT;
}

static void sunxi_pctrl_set_function(struct udevice *dev,
				     unsigned pin, int function)
{
	struct sunxi_pctrl_priv *priv = dev_get_priv(dev);
	const struct sunxi_pinctrl_desc *data =
		(struct sunxi_pinctrl_desc *)dev_get_driver_data(dev);
	u32 val, mask;

	if (function < 0)
		return;

	pin -= data->pin_base;
	mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
	val = function << sunxi_mux_offset(pin);
	clrsetbits_le32(priv->base + sunxi_mux_reg(pin), mask, val);
}

static void sunxi_pctrl_set_dlevel(struct udevice *dev,
				   unsigned pin, int dlevel)
{
	struct sunxi_pctrl_priv *priv = dev_get_priv(dev);
	const struct sunxi_pinctrl_desc *data =
		(struct sunxi_pinctrl_desc *)dev_get_driver_data(dev);
	u32 val, mask;

	if (dlevel < 0)
		return;

	pin -= data->pin_base;
	mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(pin);
	val = dlevel << sunxi_dlevel_offset(pin);
	clrsetbits_le32(priv->base + sunxi_dlevel_reg(pin), mask, val);
}

static void sunxi_pctrl_set_bias(struct udevice *dev,
				 unsigned pin, int bias)
{
	struct sunxi_pctrl_priv *priv = dev_get_priv(dev);
	const struct sunxi_pinctrl_desc *data =
		(struct sunxi_pinctrl_desc *)dev_get_driver_data(dev);
	u32 val, mask;

	if (bias < 0)
		return;

	pin -= data->pin_base;
	mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
	val = bias << sunxi_pull_offset(pin);
	clrsetbits_le32(priv->base + sunxi_pull_reg(pin), mask, val);
}

static const char *sunxi_pctrl_find_pins_prop(const char *blob,
					      int node,
					      int *npins)
{
	int count;

	/* Try the generic binding */
	count = fdt_stringlist_count(blob, node, "pins");
	if (count > 0) {
		*npins = count;
		return "pins";
	}

	/* And fall back to our legacy one */
	count = fdt_stringlist_count(blob, node, "allwinner,pins");
	if (count > 0) {
		*npins = count;
		return "allwinner,pins";
	}

	return NULL;
}

static const char* sunxi_pctrl_parse_function(const char *blob, int node)
{
	const char *function = NULL;

	/* Try the generic binding */
	function = fdt_getprop(blob, node, "function", NULL);

	/* And fall back to our legacy one */
	if (!function)
		function = fdt_getprop(blob, node, "allwinner,function", NULL);

	return function;
}

static int sunxi_pctrl_set_state(struct udevice *dev, struct udevice *config)
{
	const void *blob = gd->fdt_blob;
	int node = config->of_offset;
	const char *pin_prop;
	const char *function;
	int drive, bias;
	int i, npins;

	debug("%s: %s %s\n", __func__, dev->name, config->name);

	pin_prop = sunxi_pctrl_find_pins_prop(blob, node, &npins);
	if (!pin_prop) {
		debug("%s: missing pins property in node %s\n",
		      dev->name, config->name);
		return -EINVAL;
	}

	function = sunxi_pctrl_parse_function(blob, node);
	if (!function) {
		debug("%s: missing allwinner,function property in node %s\n",
		      dev->name, config->name);
		return -EINVAL;
	}

	drive = sunxi_pctrl_parse_drive_prop(blob, node);
	bias = sunxi_pctrl_parse_bias_prop(blob, node);

	debug("%s: function %s, drive %d, bias %d\n",
	      config->name, function, drive, bias);

	for (i = 0; i < npins; ++i) {
		const struct sunxi_desc_pin *pin;
		const char *pin_name =
			fdt_stringlist_get(blob, node, pin_prop, i, NULL);
		int muxval;

		if (!pin_name)
			continue;

		pin = sunxi_pctrl_pin_by_name(dev, pin_name);
		if (!pin) {
			debug("%s: unknown pin %s\n", dev->name, pin_name);
			continue;
		}

		muxval = sunxi_pctrl_muxval_by_name(pin, function);

		sunxi_pctrl_set_function(dev, pin->pin.number, muxval);
		sunxi_pctrl_set_dlevel(dev, pin->pin.number, drive);
		sunxi_pctrl_set_bias(dev, pin->pin.number, bias);
	}

	return 0;
}

#if defined(CONFIG_DM_GPIO) && !defined(CONFIG_DM_ALLOW_MULTIPLE_DRIVERS)
static inline void soc_data_from_desc(const struct sunxi_pinctrl_desc *data,
				      struct sunxi_gpio_soc_data *soc_data)
{
	int i;
	unsigned pinnum;
	unsigned low = data->pin_base / PINS_PER_BANK;
	unsigned high = data->pin_base;

	for (i = 0; i < data->npins; ++i) {
		pinnum = data->pins[i].pin.number;
		high = max(high, pinnum);
	}

	/* convert pin-numbers to bank numbers */
	high /= PINS_PER_BANK;

	soc_data->start = low;
	soc_data->no_banks = high - low + 1;
}
#endif

static int sunxi_pctrl_bind_gpio(struct udevice *dev)
{
#if defined(CONFIG_DM_GPIO) && !defined(CONFIG_DM_ALLOW_MULTIPLE_DRIVERS)
	struct sunxi_pctrl_priv *priv = dev_get_priv(dev);
	const struct sunxi_pinctrl_desc *data =
		(struct sunxi_pinctrl_desc *)dev_get_driver_data(dev);
	struct driver *gpio_driver;
	char name[20];
	int ret;

	/* Fill the soc_data for the gpio driver from the pinctrl_desc */
	soc_data_from_desc(data, &priv->gpio_soc_data);

	gpio_driver = lists_driver_lookup_name("gpio_sunxi");
	if (!gpio_driver)
		return -ENOENT;

	ret = device_bind_with_driver_data(dev, gpio_driver, "sunxi_gpio",
					   (ulong)&priv->gpio_soc_data,
					   dev->of_offset, &priv->gpio_dev);

	if (ret < 0)
		return ret;

	snprintf(name, sizeof(name), "sunxi_gpio@%x", (uint32_t)priv->base);
	device_set_name(priv->gpio_dev, name);
#endif

	return 0;
}

static int sunxi_pctrl_probe(struct udevice *dev)
{
	struct sunxi_pctrl_priv *priv = dev_get_priv(dev);
	fdt_addr_t addr_base;
	fdt_size_t size;

	addr_base = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob,
						       dev->of_offset,
						       "reg", 0, &size,
						       false);
	if (addr_base == FDT_ADDR_T_NONE)
		return -EINVAL;

	priv->base = (void *)addr_base;

	return sunxi_pctrl_bind_gpio(dev);
}

static struct pinctrl_ops sunxi_pctrl_ops = {
	.set_state	  = sunxi_pctrl_set_state,
};

#if defined(CONFIG_MACH_SUN50I)
extern const struct sunxi_pinctrl_desc a64_pinctrl_data;
extern const struct sunxi_pinctrl_desc a64_r_pinctrl_data;
#endif

static const struct udevice_id sunxi_pctrl_ids[] = {
#if defined(CONFIG_MACH_SUN50I)
	{ .compatible = "allwinner,sun50i-a64-pinctrl",
	  .data = (ulong)&a64_pinctrl_data },
	{ .compatible = "allwinner,sun50i-a64-r-pinctrl",
	  .data = (ulong)&a64_r_pinctrl_data },
#endif
	{ }
};

U_BOOT_DRIVER(pinctrl_sunxi) = {
	.name		= "sunxi_pctrl",
	.id		= UCLASS_PINCTRL,
	.of_match	= sunxi_pctrl_ids,
	.priv_auto_alloc_size = sizeof(struct sunxi_pctrl_priv),
	.ops		= &sunxi_pctrl_ops,
	.bind		= dm_scan_fdt_dev,
	.probe		= sunxi_pctrl_probe,
};