xref: /netbsd-src/sys/dev/i2c/axp22x.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /* $NetBSD: axp22x.c,v 1.3 2017/10/07 20:31:48 jmcneill 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.3 2017/10/07 20:31:48 jmcneill 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 #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
69 axp22x_match(device_t parent, cfdata_t match, void *aux)
70 {
71 	struct i2c_attach_args *ia = aux;
72 
73 	if (ia->ia_name != NULL)
74 		return iic_compat_match(ia, compatible);
75 
76 	return 1;
77 }
78 
79 static void
80 axp22x_attach(device_t parent, device_t self, void *aux)
81 {
82 	struct axp22x_softc *sc = device_private(self);
83 	struct i2c_attach_args *ia = aux;
84 
85 	sc->sc_dev = self;
86 	sc->sc_i2c = ia->ia_tag;
87 	sc->sc_addr = ia->ia_addr;
88 
89 	aprint_naive("\n");
90 	aprint_normal("\n");
91 
92 	sc->sc_sme = sysmon_envsys_create();
93 	sc->sc_sme->sme_name = device_xname(self);
94 	sc->sc_sme->sme_cookie = sc;
95 	sc->sc_sme->sme_refresh = axp22x_sensors_refresh;
96 
97 	sc->sc_sensor_temp.units = ENVSYS_STEMP;
98 	sc->sc_sensor_temp.state = ENVSYS_SINVALID;
99 	sc->sc_sensor_temp.flags = ENVSYS_FHAS_ENTROPY;
100 	snprintf(sc->sc_sensor_temp.desc, sizeof(sc->sc_sensor_temp.desc),
101 	    "internal temperature");
102 	sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_temp);
103 
104 	sysmon_envsys_register(sc->sc_sme);
105 }
106 
107 static void
108 axp22x_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
109 {
110 	struct axp22x_softc *sc = sme->sme_cookie;
111 	uint8_t buf[2];
112 	int error;
113 
114 	iic_acquire_bus(sc->sc_i2c, 0);
115 	error = axp22x_read(sc, AXP_TEMP_MON_REG, buf, sizeof(buf));
116 	iic_release_bus(sc->sc_i2c, 0);
117 
118 	if (error) {
119 		edata->state = ENVSYS_SINVALID;
120 	} else {
121 		/* between -243.7C and 165.8C, step +0.1C */
122 		edata->value_cur = (((buf[0] << 4) | (buf[1] & 0xf)) - 2437)
123 				   * 100000 + 273150000;
124 		edata->state = ENVSYS_SVALID;
125 	}
126 }
127 
128 static int
129 axp22x_read(struct axp22x_softc *sc, uint8_t reg, uint8_t *val, size_t len)
130 {
131 	size_t i;
132 	int error;
133 
134 	for (i = 0; i < len; i++) {
135 		error = iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr,
136 		    reg + i, &val[i], 0);
137 		if (error)
138 			return error;
139 	}
140 
141 	return 0;
142 }
143