/*
* Copyright (c) 2003 Wasabi Systems, Inc.
* All rights reserved.
*
* Written by Jason R. Thorpe for Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project by
* Wasabi Systems, Inc.
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
static int
lmtemp_match(device_t parent, cfdata_t cf, void *aux)
{
struct i2c_attach_args *ia = aux;
int i, match_result;
if (iic_use_direct_match(ia, cf, compat_data, &match_result))
return match_result;
/*
* Indirect config - not much we can do!
*/
for (i = 0; i < __arraycount(lmtemptbl); i++) {
if (i == cf->cf_flags) {
break;
}
}
if (i == __arraycount(lmtemptbl)) {
return 0;
}
if ((ia->ia_addr & lmtemptbl[i].lmtemp_addrmask) ==
lmtemptbl[i].lmtemp_addr)
return I2C_MATCH_ADDRESS_ONLY;
if (iic_acquire_bus(sc->sc_tag, 0)) {
aprint_error_dev(self,
"unable to acquire I2C bus\n");
return;
}
/* Read temperature limit(s) and remember initial value(s). */
if (i == lmtemp_lm77) {
if (lmtemp_temp_read(sc, LM77_REG_TCRIT_SET_POINT,
&sc->sc_scrit, 1) != 0) {
aprint_error_dev(self,
"unable to read low register\n");
iic_release_bus(sc->sc_tag, 0);
return;
}
if (lmtemp_temp_read(sc, LM77_REG_TLOW_SET_POINT,
&sc->sc_smin, 1) != 0) {
aprint_error_dev(self,
"unable to read low register\n");
iic_release_bus(sc->sc_tag, 0);
return;
}
if (lmtemp_temp_read(sc, LM77_REG_THIGH_SET_POINT,
&sc->sc_smax, 1) != 0) {
aprint_error_dev(self,
"unable to read high register\n");
iic_release_bus(sc->sc_tag, 0);
return;
}
} else { /* LM75 or compatible */
if (lmtemp_temp_read(sc, LM75_REG_TOS_SET_POINT,
&sc->sc_smax, 1) != 0) {
aprint_error_dev(self, "unable to read Tos register\n");
iic_release_bus(sc->sc_tag, 0);
return;
}
}
sc->sc_tmax = sc->sc_smax;
if (i == lmtemp_lm75)
lmtemp_setup_sysctl(sc);
/* Set the configuration of the LM75 to defaults. */
if (lmtemp_config_write(sc, LM75_CONFIG_FAULT_QUEUE_4) != 0) {
aprint_error_dev(self, "unable to write config register\n");
iic_release_bus(sc->sc_tag, 0);
return;
}
iic_release_bus(sc->sc_tag, 0);
static uint32_t
lmtemp_decode_lm75(const uint8_t *buf, int degc)
{
int temp;
uint32_t val;
/*
* LM75 temps are the most-significant 9 bits of a 16-bit reg.
* sign-extend the MSB and add in the 0.5 from the LSB
*/
temp = (int8_t) buf[0];
temp = (temp << 1) + ((buf[1] >> 7) & 0x1);
/* Temp is given in 1/2 deg. C, we convert to C or uK. */
if (degc)
val = temp / 2;
else
val = temp * 500000 + 273150000;
return val;
}
static uint32_t
lmtemp_decode_ds75(const uint8_t *buf, int degc)
{
int temp;
/*
* Sign-extend the MSB byte, and add in the fractions of a
* degree contained in the LSB (precision 1/16th DegC).
*/
temp = (int8_t)buf[0];
temp = (temp << 4) | ((buf[1] >> 4) & 0xf);
/*
* Conversion to C or uK is simple.
*/
if (degc)
return temp / 16;
else
return (temp * 62500 + 273150000);
}
static uint32_t
lmtemp_decode_lm77(const uint8_t *buf, int degc)
{
int temp;
uint32_t val;
/*
* Describe each bits of temperature registers on LM77.
* D15 - D12: Sign
* D11 - D3 : Bit8(MSB) - Bit0
*/
temp = (int8_t)buf[0];
temp = (temp << 5) | ((buf[1] >> 3) & 0x1f);
/* Temp is given in 1/2 deg. C, we convert to C or uK. */
if (degc)
val = temp / 2;
else
val = temp * 500000 + 273150000;
return val;
}
static void lmtemp_encode_lm75(const uint32_t val, uint8_t *buf, int degc)
{
int temp;
/* Convert from C or uK to register format */
if (degc)
temp = val * 2;
else
temp = (val - 273150000) / 500000;
buf[0] = (temp >> 1) & 0xff;
buf[1] = (temp & 1) << 7;
}
static void lmtemp_encode_ds75(const uint32_t val, uint8_t *buf, int degc)
{
int temp;
/* Convert from C or uK to register format */
if (degc)
temp = val * 16;
else
temp = (val - 273150000) / 62500;
buf[0] = (temp >> 4) & 0xff;
buf[1] = (temp & 0xf) << 4;
}
static void lmtemp_encode_lm77(const uint32_t val, uint8_t *buf, int degc)
{
int temp;
/* Convert from C or uK to register format */
if (degc)
temp = val * 2;
else
temp = (val - 273150000) / 500000;
buf[0] = (temp >> 5) & 0xff;
buf[1] = (temp & 0x1f) << 3;
}