axp22x.c revision 1.5 1 /* $NetBSD: axp22x.c,v 1.5 2018/06/18 17:07:07 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2014 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: axp22x.c,v 1.5 2018/06/18 17:07:07 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/conf.h>
36 #include <sys/bus.h>
37 #include <sys/kmem.h>
38
39 #include <dev/i2c/i2cvar.h>
40
41 #include <dev/sysmon/sysmonvar.h>
42
43 static const char *compatible[] = {
44 "x-powers,axp221",
45 NULL
46 };
47
48 static const struct device_compatible_entry axp22x_compat_data[] = {
49 DEVICE_COMPAT_ENTRY(compatible),
50 DEVICE_COMPAT_TERMINATOR
51 };
52
53 #define AXP_TEMP_MON_REG 0x56 /* 2 bytes */
54
55 struct axp22x_softc {
56 device_t sc_dev;
57 i2c_tag_t sc_i2c;
58 i2c_addr_t sc_addr;
59
60 struct sysmon_envsys *sc_sme;
61 envsys_data_t sc_sensor_temp;
62 };
63
64 static int axp22x_match(device_t, cfdata_t, void *);
65 static void axp22x_attach(device_t, device_t, void *);
66
67 static void axp22x_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
68 static int axp22x_read(struct axp22x_softc *, uint8_t, uint8_t *, size_t);
69
70 CFATTACH_DECL_NEW(axp22x, sizeof(struct axp22x_softc),
71 axp22x_match, axp22x_attach, NULL, NULL);
72
73 static int
74 axp22x_match(device_t parent, cfdata_t match, void *aux)
75 {
76 struct i2c_attach_args *ia = aux;
77 int match_result;
78
79 if (iic_use_direct_match(ia, match, axp22x_compat_data, &match_result))
80 return match_result;
81
82 /* This device is direct-config only. */
83
84 return 0;
85 }
86
87 static void
88 axp22x_attach(device_t parent, device_t self, void *aux)
89 {
90 struct axp22x_softc *sc = device_private(self);
91 struct i2c_attach_args *ia = aux;
92
93 sc->sc_dev = self;
94 sc->sc_i2c = ia->ia_tag;
95 sc->sc_addr = ia->ia_addr;
96
97 aprint_naive("\n");
98 aprint_normal("\n");
99
100 sc->sc_sme = sysmon_envsys_create();
101 sc->sc_sme->sme_name = device_xname(self);
102 sc->sc_sme->sme_cookie = sc;
103 sc->sc_sme->sme_refresh = axp22x_sensors_refresh;
104
105 sc->sc_sensor_temp.units = ENVSYS_STEMP;
106 sc->sc_sensor_temp.state = ENVSYS_SINVALID;
107 sc->sc_sensor_temp.flags = ENVSYS_FHAS_ENTROPY;
108 snprintf(sc->sc_sensor_temp.desc, sizeof(sc->sc_sensor_temp.desc),
109 "internal temperature");
110 sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_temp);
111
112 sysmon_envsys_register(sc->sc_sme);
113 }
114
115 static void
116 axp22x_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
117 {
118 struct axp22x_softc *sc = sme->sme_cookie;
119 uint8_t buf[2];
120 int error;
121
122 iic_acquire_bus(sc->sc_i2c, 0);
123 error = axp22x_read(sc, AXP_TEMP_MON_REG, buf, sizeof(buf));
124 iic_release_bus(sc->sc_i2c, 0);
125
126 if (error) {
127 edata->state = ENVSYS_SINVALID;
128 } else {
129 /* between -243.7C and 165.8C, step +0.1C */
130 edata->value_cur = (((buf[0] << 4) | (buf[1] & 0xf)) - 2437)
131 * 100000 + 273150000;
132 edata->state = ENVSYS_SVALID;
133 }
134 }
135
136 static int
137 axp22x_read(struct axp22x_softc *sc, uint8_t reg, uint8_t *val, size_t len)
138 {
139 size_t i;
140 int error;
141
142 for (i = 0; i < len; i++) {
143 error = iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr,
144 reg + i, &val[i], 0);
145 if (error)
146 return error;
147 }
148
149 return 0;
150 }
151