summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/qcsdam.c
blob: 309c889b35e046ecc75d9d855fcf622e517f1525 (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
/*	$OpenBSD: qcsdam.c,v 1.1 2023/07/22 22:43:53 patrick Exp $	*/
/*
 * Copyright (c) 2023 Patrick Wildt <patrick@blueri.se>
 *
 * 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/param.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <machine/bus.h>
#include <machine/fdt.h>

#include <dev/fdt/spmivar.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_misc.h>
#include <dev/ofw/fdt.h>

struct qcsdam_softc {
	struct device		sc_dev;
	spmi_tag_t		sc_tag;
	int8_t			sc_sid;
	uint16_t		sc_addr;

	struct nvmem_device	sc_nd;
};

int qcsdam_match(struct device *, void *, void *);
void qcsdam_attach(struct device *, struct device *, void *);

const struct cfattach	qcsdam_ca = {
	sizeof (struct qcsdam_softc), qcsdam_match, qcsdam_attach
};

struct cfdriver qcsdam_cd = {
	NULL, "qcsdam", DV_DULL
};

int	qcsdam_nvmem_read(void *, bus_addr_t, void *, bus_size_t);
int	qcsdam_nvmem_write(void *, bus_addr_t, const void *, bus_size_t);

int
qcsdam_match(struct device *parent, void *match, void *aux)
{
	struct spmi_attach_args *saa = aux;

	return OF_is_compatible(saa->sa_node, "qcom,spmi-sdam");
}

void
qcsdam_attach(struct device *parent, struct device *self, void *aux)
{
	struct qcsdam_softc *sc = (struct qcsdam_softc *)self;
	struct spmi_attach_args *saa = aux;
	uint32_t reg;

	if (OF_getpropintarray(saa->sa_node, "reg",
	    &reg, sizeof(reg)) != sizeof(reg)) {
		printf(": can't find registers\n");
		return;
	}

	sc->sc_tag = saa->sa_tag;
	sc->sc_sid = saa->sa_sid;
	sc->sc_addr = reg;

	printf("\n");

	sc->sc_nd.nd_node = saa->sa_node;
	sc->sc_nd.nd_cookie = sc;
	sc->sc_nd.nd_read = qcsdam_nvmem_read;
	sc->sc_nd.nd_write = qcsdam_nvmem_write;
	nvmem_register(&sc->sc_nd);
}

int
qcsdam_nvmem_read(void *cookie, bus_addr_t addr, void *data, bus_size_t size)
{
	struct qcsdam_softc *sc = cookie;
	int error;

	error = spmi_cmd_read(sc->sc_tag, sc->sc_sid, SPMI_CMD_EXT_READL,
	    sc->sc_addr + addr, data, size);
	if (error) {
		printf("%s: error reading NVMEM\n", sc->sc_dev.dv_xname);
		return error;
	}

	return 0;
}

int
qcsdam_nvmem_write(void *cookie, bus_addr_t addr, const void *data,
    bus_size_t size)
{
	struct qcsdam_softc *sc = cookie;
	int error;

	error = spmi_cmd_write(sc->sc_tag, sc->sc_sid, SPMI_CMD_EXT_WRITEL,
	    sc->sc_addr + addr, data, size);
	if (error) {
		printf("%s: error reading NVMEM\n", sc->sc_dev.dv_xname);
		return error;
	}

	return 0;
}