1 /* $NetBSD: axp22x.c,v 1.9 2021/01/27 02:29:48 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2014 Jared D. McNeill <jmcneill@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.9 2021/01/27 02:29:48 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 struct device_compatible_entry compat_data[] = {
44 { .compat = "x-powers,axp221" },
45 DEVICE_COMPAT_EOL
46 };
47
48 #define AXP_TEMP_MON_REG 0x56 /* 2 bytes */
49
50 struct axp22x_softc {
51 device_t sc_dev;
52 i2c_tag_t sc_i2c;
53 i2c_addr_t sc_addr;
54
55 struct sysmon_envsys *sc_sme;
56 envsys_data_t sc_sensor_temp;
57 };
58
59 static int axp22x_match(device_t, cfdata_t, void *);
60 static void axp22x_attach(device_t, device_t, void *);
61
62 static void axp22x_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
63 static int axp22x_read(struct axp22x_softc *, uint8_t, uint8_t *, size_t);
64
65 CFATTACH_DECL_NEW(axp22x, sizeof(struct axp22x_softc),
66 axp22x_match, axp22x_attach, NULL, NULL);
67
68 static int
axp22x_match(device_t parent,cfdata_t match,void * aux)69 axp22x_match(device_t parent, cfdata_t match, void *aux)
70 {
71 struct i2c_attach_args *ia = aux;
72 int match_result;
73
74 if (iic_use_direct_match(ia, match, compat_data, &match_result))
75 return match_result;
76
77 /* This device is direct-config only. */
78
79 return 0;
80 }
81
82 static void
axp22x_attach(device_t parent,device_t self,void * aux)83 axp22x_attach(device_t parent, device_t self, void *aux)
84 {
85 struct axp22x_softc *sc = device_private(self);
86 struct i2c_attach_args *ia = aux;
87
88 sc->sc_dev = self;
89 sc->sc_i2c = ia->ia_tag;
90 sc->sc_addr = ia->ia_addr;
91
92 aprint_naive("\n");
93 aprint_normal("\n");
94
95 sc->sc_sme = sysmon_envsys_create();
96 sc->sc_sme->sme_name = device_xname(self);
97 sc->sc_sme->sme_cookie = sc;
98 sc->sc_sme->sme_refresh = axp22x_sensors_refresh;
99
100 sc->sc_sensor_temp.units = ENVSYS_STEMP;
101 sc->sc_sensor_temp.state = ENVSYS_SINVALID;
102 sc->sc_sensor_temp.flags = ENVSYS_FHAS_ENTROPY;
103 snprintf(sc->sc_sensor_temp.desc, sizeof(sc->sc_sensor_temp.desc),
104 "internal temperature");
105 sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_temp);
106
107 sysmon_envsys_register(sc->sc_sme);
108 }
109
110 static void
axp22x_sensors_refresh(struct sysmon_envsys * sme,envsys_data_t * edata)111 axp22x_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
112 {
113 struct axp22x_softc *sc = sme->sme_cookie;
114 uint8_t buf[2];
115 int error;
116
117 iic_acquire_bus(sc->sc_i2c, 0);
118 error = axp22x_read(sc, AXP_TEMP_MON_REG, buf, sizeof(buf));
119 iic_release_bus(sc->sc_i2c, 0);
120
121 if (error) {
122 edata->state = ENVSYS_SINVALID;
123 } else {
124 /* between -243.7C and 165.8C, step +0.1C */
125 edata->value_cur = (((buf[0] << 4) | (buf[1] & 0xf)) - 2437)
126 * 100000 + 273150000;
127 edata->state = ENVSYS_SVALID;
128 }
129 }
130
131 static int
axp22x_read(struct axp22x_softc * sc,uint8_t reg,uint8_t * val,size_t len)132 axp22x_read(struct axp22x_softc *sc, uint8_t reg, uint8_t *val, size_t len)
133 {
134 size_t i;
135 int error;
136
137 for (i = 0; i < len; i++) {
138 error = iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr,
139 reg + i, &val[i], 0);
140 if (error)
141 return error;
142 }
143
144 return 0;
145 }
146