xref: /netbsd-src/sys/dev/i2c/axp20x.c (revision 6cf6fe02a981b55727c49c3d37b0d8191a98c0ee)
1 /* $NetBSD: axp20x.c,v 1.2 2014/09/09 23:39:16 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: axp20x.c,v 1.2 2014/09/09 23:39:16 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 #define AXP_TEMP_MON_REG	0x5e	/* 2 bytes */
44 
45 struct axp20x_softc {
46 	device_t	sc_dev;
47 	i2c_tag_t	sc_i2c;
48 	i2c_addr_t	sc_addr;
49 
50 	struct sysmon_envsys *sc_sme;
51 	envsys_data_t	sc_sensor_temp;
52 };
53 
54 static int	axp20x_match(device_t, cfdata_t, void *);
55 static void	axp20x_attach(device_t, device_t, void *);
56 
57 static void	axp20x_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
58 static int	axp20x_read(struct axp20x_softc *, uint8_t, uint8_t *, size_t);
59 
60 CFATTACH_DECL_NEW(axp20x, sizeof(struct axp20x_softc),
61     axp20x_match, axp20x_attach, NULL, NULL);
62 
63 static int
64 axp20x_match(device_t parent, cfdata_t match, void *aux)
65 {
66 	return 1;
67 }
68 
69 static void
70 axp20x_attach(device_t parent, device_t self, void *aux)
71 {
72 	struct axp20x_softc *sc = device_private(self);
73 	struct i2c_attach_args *ia = aux;
74 
75 	sc->sc_dev = self;
76 	sc->sc_i2c = ia->ia_tag;
77 	sc->sc_addr = ia->ia_addr;
78 
79 	aprint_naive("\n");
80 	aprint_normal("\n");
81 
82 	sc->sc_sme = sysmon_envsys_create();
83 	sc->sc_sme->sme_name = device_xname(self);
84 	sc->sc_sme->sme_cookie = sc;
85 	sc->sc_sme->sme_refresh = axp20x_sensors_refresh;
86 
87 	sc->sc_sensor_temp.units = ENVSYS_STEMP;
88 	sc->sc_sensor_temp.state = ENVSYS_SINVALID;
89 	sc->sc_sensor_temp.flags = ENVSYS_FHAS_ENTROPY;
90 	snprintf(sc->sc_sensor_temp.desc, sizeof(sc->sc_sensor_temp.desc),
91 	    "internal temperature");
92 	sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_temp);
93 
94 	sysmon_envsys_register(sc->sc_sme);
95 }
96 
97 static void
98 axp20x_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
99 {
100 	struct axp20x_softc *sc = sme->sme_cookie;
101 	uint8_t buf[2];
102 	int error;
103 
104 	iic_acquire_bus(sc->sc_i2c, 0);
105 	error = axp20x_read(sc, AXP_TEMP_MON_REG, buf, sizeof(buf));
106 	iic_release_bus(sc->sc_i2c, 0);
107 
108 	if (error) {
109 		edata->state = ENVSYS_SINVALID;
110 	} else {
111 		/* between -144.7C and 264.8C, step +0.1C */
112 		edata->value_cur = (((buf[0] << 4) | (buf[1] & 0xf)) - 1447)
113 				   * 100000 + 273150000;
114 		edata->state = ENVSYS_SVALID;
115 	}
116 }
117 
118 static int
119 axp20x_read(struct axp20x_softc *sc, uint8_t reg, uint8_t *val, size_t len)
120 {
121 	return iic_smbus_block_read(sc->sc_i2c, sc->sc_addr,
122 	    reg, val, len, 0);
123 }
124