summaryrefslogtreecommitdiff
path: root/sys/dev/ofw/ofw_pinctrl.c
blob: f186b8afbc5293f38e8ca4ed080ff7d301af262e (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
/*	$OpenBSD: ofw_pinctrl.c,v 1.3 2020/06/06 16:59:43 patrick Exp $	*/
/*
 * Copyright (c) 2016 Mark Kettenis
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/systm.h>
#include <sys/malloc.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_pinctrl.h>

struct pinctrl {
	int	pc_phandle;
	int	(*pc_pinctrl)(uint32_t, void *);
	void	*pc_cookie;

	LIST_ENTRY(pinctrl) pc_list;
};

void pinctrl_register_child(int, int (*)(uint32_t, void *), void *);

LIST_HEAD(, pinctrl) pinctrls = LIST_HEAD_INITIALIZER(pinctrl);

void
pinctrl_register(int node, int (*pinctrl)(uint32_t, void *), void *cookie)
{
	for (node = OF_child(node); node; node = OF_peer(node))
		pinctrl_register_child(node, pinctrl, cookie);
}

void
pinctrl_register_child(int node, int (*pinctrl)(uint32_t, void *), void *cookie)
{
	struct pinctrl *pc;
	uint32_t phandle;

	phandle = OF_getpropint(node, "phandle", 0);
	if (phandle) {
		pc = malloc(sizeof(struct pinctrl), M_DEVBUF, M_WAITOK);
		pc->pc_phandle = phandle;
		pc->pc_pinctrl = pinctrl;
		pc->pc_cookie = cookie;
		LIST_INSERT_HEAD(&pinctrls, pc, pc_list);
	}

	for (node = OF_child(node); node; node = OF_peer(node))
		pinctrl_register_child(node, pinctrl, cookie);
}

int
pinctrl_byphandle(uint32_t phandle)
{
	struct pinctrl *pc;

	if (phandle == 0)
		return -1;

	LIST_FOREACH(pc, &pinctrls, pc_list) {
		if (pc->pc_phandle == phandle)
			return pc->pc_pinctrl(pc->pc_phandle, pc->pc_cookie);
	}

	return -1;
}

int
pinctrl_byid(int node, int id)
{
	char pinctrl[32];
	uint32_t *phandles;
	int len, i;

	snprintf(pinctrl, sizeof(pinctrl), "pinctrl-%d", id);
	len = OF_getproplen(node, pinctrl);
	if (len <= 0)
		return -1;

	phandles = malloc(len, M_TEMP, M_WAITOK);
	OF_getpropintarray(node, pinctrl, phandles, len);
	for (i = 0; i < len / sizeof(uint32_t); i++)
		pinctrl_byphandle(phandles[i]);
	free(phandles, M_TEMP, len);
	return 0;
}

int
pinctrl_byname(int node, const char *config)
{
	int id;

	id = OF_getindex(node, config, "pinctrl-names");
	if (id < 0)
		return -1;

	return pinctrl_byid(node, id);
}