1/*	$OpenBSD: rkiovd.c,v 1.1 2023/04/01 08:39:05 kettenis Exp $	*/
2/*
3 * Copyright (c) 2023 Mark Kettenis <kettenis@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/param.h>
19#include <sys/systm.h>
20#include <sys/device.h>
21
22#include <machine/intr.h>
23#include <machine/bus.h>
24#include <machine/fdt.h>
25
26#include <dev/ofw/openfirm.h>
27#include <dev/ofw/ofw_misc.h>
28#include <dev/ofw/ofw_regulator.h>
29#include <dev/ofw/fdt.h>
30
31#define RK3568_PMU_GRF_IO_VSEL0		0x0140
32#define RK3568_PMU_GRF_IO_VSEL1		0x0144
33#define RK3568_PMU_GRF_IO_VSEL2		0x0148
34
35#define RKIOVD_MAX_DOMAINS	9
36
37const char *rkiovd_rk3568_domains[] = {
38	"pmuio1-supply",
39	"pmuio2-supply",
40	"vccio1-supply",
41	"vccio2-supply",
42	"vccio3-supply",
43	"vccio4-supply",
44	"vccio5-supply",
45	"vccio6-supply",
46	"vccio7-supply",
47	NULL
48};
49
50struct rkiovd_domain {
51	struct rkiovd_softc	*rd_sc;
52	int			rd_idx;
53	struct regulator_notifier rd_rn;
54};
55
56struct rkiovd_softc {
57	struct device		sc_dv;
58	struct regmap		*sc_rm;
59	struct rkiovd_domain	sc_rd[RKIOVD_MAX_DOMAINS];
60};
61
62int	rkiovd_match(struct device *, void *, void *);
63void	rkiovd_attach(struct device *, struct device *, void *);
64
65const struct cfattach rkiovd_ca = {
66	sizeof (struct rkiovd_softc), rkiovd_match, rkiovd_attach
67};
68
69struct cfdriver rkiovd_cd = {
70	NULL, "rkiovd", DV_DULL
71};
72
73void	rkiovd_rk3568_notify(void *, uint32_t);
74
75int
76rkiovd_match(struct device *parent, void *match, void *aux)
77{
78	struct fdt_attach_args *faa = aux;
79
80	return OF_is_compatible(faa->fa_node,
81	    "rockchip,rk3568-pmu-io-voltage-domain");
82}
83
84void
85rkiovd_attach(struct device *parent, struct device *self, void *aux)
86{
87	struct rkiovd_softc *sc = (struct rkiovd_softc *)self;
88	struct fdt_attach_args *faa = aux;
89	int idx;
90
91	printf("\n");
92
93	sc->sc_rm = regmap_bynode(OF_parent(faa->fa_node));
94	if (sc->sc_rm == NULL)
95		return;
96
97	for (idx = 0; rkiovd_rk3568_domains[idx]; idx++) {
98		struct rkiovd_domain *rd = &sc->sc_rd[idx];
99		char *name = (char *)rkiovd_rk3568_domains[idx];
100		uint32_t phandle;
101
102		phandle = OF_getpropint(faa->fa_node, name, 0);
103		if (phandle == 0)
104			continue;
105
106		rd->rd_sc = sc;
107		rd->rd_idx = idx;
108		rd->rd_rn.rn_phandle = phandle;
109		rd->rd_rn.rn_cookie = rd;
110		rd->rd_rn.rn_notify = rkiovd_rk3568_notify;
111		regulator_notify(&rd->rd_rn);
112	}
113}
114
115void
116rkiovd_rk3568_notify(void *cookie, uint32_t voltage)
117{
118	struct rkiovd_domain *rd = cookie;
119	struct rkiovd_softc *sc = rd->rd_sc;
120	uint32_t current_voltage;
121	uint32_t vsel0, vsel1;
122	int bit;
123
124	/*
125	 * If the new voltage is higher than the current voltage we
126	 * need to configure the I/O voltage domain before changing
127	 * the voltage.  If the new voltage is lower we need to
128	 * configure the domain after changing the voltage.  Using the
129	 * maximum of the new and current voltage makes sure this is
130	 * always the case.
131	 */
132	current_voltage = regulator_get_voltage(rd->rd_rn.rn_phandle);
133	if (voltage < current_voltage)
134		voltage = current_voltage;
135
136	switch (rd->rd_idx) {
137	case 1:			/* PMUIO2 */
138		if (voltage > 1980000) {
139			vsel0 = (1 << 1) << 16 | (0 << 1);
140			vsel1 = (1 << 5) << 16 | (1 << 5);
141		} else {
142			vsel0 = (1 << 1) << 16 | (1 << 1);
143			vsel1 = (1 << 5) << 16 | (0 << 5);
144		}
145		regmap_write_4(sc->sc_rm, RK3568_PMU_GRF_IO_VSEL2, vsel0);
146		regmap_write_4(sc->sc_rm, RK3568_PMU_GRF_IO_VSEL2, vsel1);
147		break;
148	case 2:			/* VCCIO1 */
149	case 4:			/* VCCIO3 */
150	case 5:			/* VCCIO4 */
151	case 6:			/* VCCIO5 */
152	case 7:			/* VCCIO6 */
153	case 8:			/* VCCIO7 */
154		bit = rd->rd_idx - 1;
155		if (voltage > 1980000) {
156			vsel0 = (1 << bit) << 16 | (0 << bit);
157			vsel1 = (1 << bit) << 16 | (1 << bit);
158		} else {
159			vsel0 = (1 << bit) << 16 | (1 << bit);
160			vsel1 = (1 << bit) << 16 | (0 << bit);
161		}
162		regmap_write_4(sc->sc_rm, RK3568_PMU_GRF_IO_VSEL0, vsel0);
163		regmap_write_4(sc->sc_rm, RK3568_PMU_GRF_IO_VSEL1, vsel1);
164		break;
165	case 0:			/* PMUIO1 */
166	case 3:			/* VCCIO2 */
167		/* Ignore */
168		break;
169	}
170}
171