/*
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Paul Goyette.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 THE FOUNDATION OR CONTRIBUTORS
* 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.
*/
if ((ia->ia_addr & SDTEMP_ADDRMASK) != SDTEMP_ADDR)
return 0;
/*
* Verify that we can read the manufacturer ID, Device ID and the
* capability
*/
error = iic_acquire_bus(sc.sc_tag, 0);
if (error)
return 0;
error = sdtemp_read_16(&sc, SDTEMP_REG_MFG_ID, &mfgid) |
sdtemp_read_16(&sc, SDTEMP_REG_DEV_REV, &devid) |
sdtemp_read_16(&sc, SDTEMP_REG_CAPABILITY, &cap);
iic_release_bus(sc.sc_tag, 0);
if (error)
return 0;
i = sdtemp_lookup(mfgid, devid);
if ((sdtemp_dev_table[i].sdtemp_mfg_id == 0) &&
(sdtemp_dev_table[i].sdtemp_devrev == 0)) {
aprint_debug("sdtemp: No match for mfg 0x%04x dev 0x%02x "
"rev 0x%02x at address 0x%02x\n", mfgid, devid >> 8,
devid & 0xff, sc.sc_address);
return 0;
}
/*
* Check by SDTEMP_IS_TSE2004AV() might not be enough, so check the
* alarm capability, too.
*/
if ((cap & SDTEMP_CAP_HAS_ALARM) == 0)
return 0;
if (sdtemp_dev_table[i].sdtemp_mfg_id == 0) {
if (SDTEMP_IS_TSE2004AV(devid))
aprint_normal_dev(self, "TSE2004av compliant. "
"Manufacturer ID 0x%04hx, Device revision 0x%02x\n",
mfgid, devid & TSE2004AV_REV);
else {
aprint_error_dev(self,
"mfg 0x%04x dev 0x%02x rev 0x%02x at addr 0x%02x\n",
mfgid, devid >> 8, devid & 0xff, ia->ia_addr);
iic_release_bus(sc->sc_tag, 0);
aprint_error_dev(self, "It should no happen. "
"Why attach() found me?\n");
return;
}
}
error = sdtemp_read_16(sc, SDTEMP_REG_CAPABILITY, &sc->sc_capability);
aprint_debug_dev(self, "capability reg = %04x\n", sc->sc_capability);
sc->sc_resolution
= __SHIFTOUT(sc->sc_capability, SDTEMP_CAP_RESOLUTION);
/*
* Call device dependent function here. Currently, it's used for
* the resolution.
*
* IDT's devices and some Microchip's devices have the resolution
* register in the vendor specific registers area. The devices'
* resolution bits in the capability register are not the maximum
* resolution but the current value of the setting.
*/
if (sdtemp_dev_table[i].sdtemp_config != NULL)
sdtemp_dev_table[i].sdtemp_config(sc);
aprint_normal_dev(self, "%s accuracy",
(sc->sc_capability & SDTEMP_CAP_ACCURACY_1C) ? "high" : "default");
if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) != 0)
aprint_normal(", wider range");
aprint_normal(", %s resolution", temp_resl[sc->sc_resolution]);
if ((sc->sc_capability & SDTEMP_CAP_VHV) != 0)
aprint_debug(", high voltage standoff");
aprint_debug(", %s timeout",
(sc->sc_capability & SDTEMP_CAP_TMOUT) ? "25-35ms" : "10-60ms");
if ((sc->sc_capability & SDTEMP_CAP_EVSD) != 0)
aprint_normal(", event with shutdown");
aprint_normal("\n");
/*
* Alarm capability is required; if not present, this is likely
* not a real sdtemp device.
*/
if (error != 0 || (sc->sc_capability & SDTEMP_CAP_HAS_ALARM) == 0) {
iic_release_bus(sc->sc_tag, 0);
aprint_error_dev(self,
"required alarm capability not present!\n");
return;
}
/* Set the configuration to defaults. */
error = sdtemp_write_16(sc, SDTEMP_REG_CONFIG, 0);
if (error != 0) {
iic_release_bus(sc->sc_tag, 0);
aprint_error_dev(self, "error %d writing config register\n",
error);
return;
}
iic_release_bus(sc->sc_tag, 0);
/* Hook us into the sysmon_envsys subsystem */
sc->sc_sme = sysmon_envsys_create();
sc->sc_sme->sme_name = device_xname(self);
sc->sc_sme->sme_cookie = sc;
sc->sc_sme->sme_refresh = sdtemp_refresh;
sc->sc_sme->sme_get_limits = sdtemp_get_limits;
sc->sc_sme->sme_set_limits = sdtemp_set_limits;
/* Now attach the sensor */
if (sysmon_envsys_sensor_attach(sc->sc_sme, sc->sc_sensor)) {
aprint_error_dev(self, "unable to attach sensor\n");
goto bad;
}
/* Register the device */
error = sysmon_envsys_register(sc->sc_sme);
if (error) {
aprint_error_dev(self, "error %d registering with sysmon\n",
error);
goto bad;
}
if (!pmf_device_register(self, sdtemp_pmf_suspend, sdtemp_pmf_resume))
aprint_error_dev(self, "couldn't establish power handler\n");
/* Send current limit values to the device */
static void
sdtemp_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
sysmon_envsys_lim_t *limits, uint32_t *props)
{
uint16_t val;
struct sdtemp_softc *sc = sme->sme_cookie;
if (limits == NULL) {
limits = &sc->sc_deflims;
props = &sc->sc_defprops;
}
if (iic_acquire_bus(sc->sc_tag, 0) != 0)
return;
if (*props & PROP_WARNMIN) {
val = __UK2C(limits->sel_warnmin);
(void)sdtemp_write_16(sc, SDTEMP_REG_LOWER_LIM,
(val << 4) & SDTEMP_TEMP_MASK);
}
if (*props & PROP_WARNMAX) {
val = __UK2C(limits->sel_warnmax);
(void)sdtemp_write_16(sc, SDTEMP_REG_UPPER_LIM,
(val << 4) & SDTEMP_TEMP_MASK);
}
if (*props & PROP_CRITMAX) {
val = __UK2C(limits->sel_critmax);
(void)sdtemp_write_16(sc, SDTEMP_REG_CRIT_LIM,
(val << 4) & SDTEMP_TEMP_MASK);
}
iic_release_bus(sc->sc_tag, 0);
/*
* If at least one limit is set that we can handle, and no
* limits are set that we cannot handle, tell sysmon that
* the driver will take care of monitoring the limits!
*/
if (*props & (PROP_CRITMIN | PROP_BATTCAP | PROP_BATTWARN))
*props &= ~PROP_DRIVER_LIMITS;
else if (*props & PROP_LIMITS)
*props |= PROP_DRIVER_LIMITS;
else
*props &= ~PROP_DRIVER_LIMITS;
}
#ifdef NOT_YET /* All registers on these sensors are 16-bits */
/* Read a 8-bit value from a register */
static int
sdtemp_read_8(struct sdtemp_softc *sc, uint8_t reg, uint8_t *valp)
{
int error;
/* Get only the temperature bits */
temp &= SDTEMP_TEMP_MASK;
/* If necessary, extend the sign bit */
if ((sc->sc_capability & SDTEMP_CAP_WIDER_RANGE) &&
(temp & SDTEMP_TEMP_NEGATIVE))
temp |= SDTEMP_TEMP_SIGN_EXT;
/* Mask off only bits valid within current resolution */
temp &= ~(0x7 >> sc->sc_resolution);
/* Treat as signed and extend to 32-bits */
stemp = (int16_t)temp;
/* Now convert from 0.0625 (1/16) deg C increments to microKelvins */
val = (stemp * 62500) + 273150000;
if (error) {
edata->state = ENVSYS_SINVALID;
return;
}
edata->value_cur = sdtemp_decode_temp(sc, val);
/* Now check for limits */
if ((edata->upropset & PROP_DRIVER_LIMITS) == 0)
edata->state = ENVSYS_SVALID;
else if ((val & SDTEMP_ABOVE_CRIT) &&
(edata->upropset & PROP_CRITMAX))
edata->state = ENVSYS_SCRITOVER;
else if ((val & SDTEMP_ABOVE_UPPER) &&
(edata->upropset & PROP_WARNMAX))
edata->state = ENVSYS_SWARNOVER;
else if ((val & SDTEMP_BELOW_LOWER) &&
(edata->upropset & PROP_WARNMIN))
edata->state = ENVSYS_SWARNUNDER;
else
edata->state = ENVSYS_SVALID;
}
/*
* Power management functions
*
* We go into "shutdown" mode at suspend time, and return to normal
* mode upon resume. This reduces power consumption by disabling
* the A/D converter.
*/
static void
sdtemp_config_mcp(struct sdtemp_softc *sc)
{
int rv;
uint8_t resolreg;
/* Note that MCP9805 has no resolution register */
switch (sc->sc_devid_masked) {
case MCP_9804_DEVICE_ID:
case MCP_98242_DEVICE_ID:
case MCP_98243_DEVICE_ID:
resolreg = SDTEMP_REG_MCP_RESOLUTION_9804;
break;
case MCP_98244_DEVICE_ID:
resolreg = SDTEMP_REG_MCP_RESOLUTION_98244;
break;
default:
aprint_error("%s: %s: unknown device ID (%04hx)\n",
device_xname(sc->sc_dev), __func__, sc->sc_devid_masked);
return;
}
/*
* Set resolution to the max.
*
* Even if it fails, the resolution will be the default. It's not a
* fatal error.
*/
rv = sdtemp_write_16(sc, resolreg, SDTEMP_CAP_RESOLUTION_MAX);
if (rv == 0)
sc->sc_resolution = SDTEMP_CAP_RESOLUTION_MAX;
else
aprint_debug_dev(sc->sc_dev,
"error %d writing resolution register\n", rv);
}
static void
sdtemp_config_idt(struct sdtemp_softc *sc)
{
int rv;
/*
* Set resolution to the max.
*
* Even if it fails, the resolution will be the default. It's not a
* fatal error.
*/
rv = sdtemp_write_16(sc, SDTEMP_REG_IDT_RESOLUTION,
__SHIFTIN(SDTEMP_CAP_RESOLUTION_MAX, SDTEMP_CAP_RESOLUTION));
if (rv == 0)
sc->sc_resolution = SDTEMP_CAP_RESOLUTION_MAX;
else
aprint_debug_dev(sc->sc_dev,
"error %d writing resolution register\n", rv);
}