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