xref: /netbsd-src/sys/arch/arm/marvell/mvsocts.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: mvsocts.c,v 1.1 2012/08/01 10:34:42 kiyohara Exp $	*/
2 /*
3  * Copyright (c) 2012 KIYOHARA Takashi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: mvsocts.c,v 1.1 2012/08/01 10:34:42 kiyohara Exp $");
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/device.h>
33 #include <sys/errno.h>
34 
35 #include <dev/sysmon/sysmonvar.h>
36 
37 #include <dev/marvell/marvellvar.h>
38 
39 #define TS_STATUS		0x0
40 #define   STATUS_VALID			(1 << 9)
41 #define   STATUS_VAL(v)			(((v) >> 10) & 0x1ff)
42 
43 #define VAL2MCELSIUS(v)		(((322 - (v)) * 10000) / 13625)
44 
45 struct mvsocts_softc {
46 	device_t sc_dev;
47 
48 	struct sysmon_envsys *sc_sme;
49 	envsys_data_t sc_sensor;
50 
51 	bus_space_tag_t sc_iot;
52 	bus_space_handle_t sc_ioh;
53 };
54 
55 
56 static int mvsocts_match(device_t, struct cfdata *, void *);
57 static void mvsocts_attach(device_t, device_t, void *);
58 
59 static void mvsocts_refresh(struct sysmon_envsys *, envsys_data_t *);
60 
61 CFATTACH_DECL_NEW(mvsocts, sizeof(struct mvsocts_softc),
62     mvsocts_match, mvsocts_attach, NULL, NULL);
63 
64 
65 /* ARGSUSED */
66 static int
67 mvsocts_match(device_t parent, struct cfdata *match, void *aux)
68 {
69 	struct marvell_attach_args *mva = aux;
70 
71 	if (strcmp(mva->mva_name, match->cf_name) != 0)
72 		return 0;
73 	return 1;
74 }
75 
76 /* ARGSUSED */
77 static void
78 mvsocts_attach(device_t parent, device_t self, void *aux)
79 {
80         struct mvsocts_softc *sc = device_private(self);
81 	struct marvell_attach_args *mva = aux;
82 
83 	aprint_naive("\n");
84 	aprint_normal(": Marvell SoC Thermal Sensor\n");
85 
86 	sc->sc_dev = self;
87 	sc->sc_iot = mva->mva_iot;
88 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
89 	    mva->mva_offset, sizeof(uint32_t), &sc->sc_ioh))
90 		panic("%s: Cannot map registers", device_xname(self));
91 
92 	sc->sc_sme = sysmon_envsys_create();
93 	/* Initialize sensor data. */
94 	sc->sc_sensor.units =  ENVSYS_STEMP;
95 	sc->sc_sensor.state =  ENVSYS_SINVALID;
96 	strlcpy(sc->sc_sensor.desc, device_xname(self),
97 	    sizeof(sc->sc_sensor.desc));
98 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
99 		aprint_error_dev(self, "Unable to attach sysmon\n");
100 		sysmon_envsys_destroy(sc->sc_sme);
101 		return;
102 	}
103 
104 	/* Hook into system monitor. */
105 	sc->sc_sme->sme_name = device_xname(self);
106 	sc->sc_sme->sme_cookie = sc;
107 	sc->sc_sme->sme_refresh = mvsocts_refresh;
108 	if (sysmon_envsys_register(sc->sc_sme)) {
109 		aprint_error_dev(self, "Unable to register with sysmon\n");
110 		sysmon_envsys_destroy(sc->sc_sme);
111 	}
112 }
113 
114 static void
115 mvsocts_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
116 {
117 	struct mvsocts_softc *sc = sme->sme_cookie;
118 	uint32_t val, uc, uk;
119 
120 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, TS_STATUS);
121 	if (!(val & STATUS_VALID)) {
122 		aprint_error_dev(sc->sc_dev, "status value is invalid\n");
123 		return;
124 	}
125 	uc = VAL2MCELSIUS(STATUS_VAL(val)) * 1000000;	/* uC */
126 	uk = uc + 273150000;				/* convert to uKelvin */
127 	sc->sc_sensor.value_cur = uk;
128 	sc->sc_sensor.state = ENVSYS_SVALID;
129 }
130