diff --git a/sys/conf/files.powerpc b/sys/conf/files.powerpc index 8900197e6f48..f00c62b1ab0e 100644 --- a/sys/conf/files.powerpc +++ b/sys/conf/files.powerpc @@ -1,235 +1,236 @@ # This file tells config what files go into building a kernel, # files marked standard are always included. # # $FreeBSD$ # # The long compile-with and dependency lines are required because of # limitations in config: backslash-newline doesn't work in strings, and # dependency lines other than the first are silently ignored. # # font.h optional sc \ compile-with "uudecode < /usr/share/syscons/fonts/${SC_DFLT_FONT}-8x16.fnt && file2c 'u_char dflt_font_16[16*256] = {' '};' < ${SC_DFLT_FONT}-8x16 > font.h && uudecode < /usr/share/syscons/fonts/${SC_DFLT_FONT}-8x14.fnt && file2c 'u_char dflt_font_14[14*256] = {' '};' < ${SC_DFLT_FONT}-8x14 >> font.h && uudecode < /usr/share/syscons/fonts/${SC_DFLT_FONT}-8x8.fnt && file2c 'u_char dflt_font_8[8*256] = {' '};' < ${SC_DFLT_FONT}-8x8 >> font.h" \ no-obj no-implicit-rule before-depend \ clean "font.h ${SC_DFLT_FONT}-8x14 ${SC_DFLT_FONT}-8x16 ${SC_DFLT_FONT}-8x8" # # There is only an asm version on ppc64. cddl/compat/opensolaris/kern/opensolaris_atomic.c optional zfs powerpc compile-with "${ZFS_C}" cddl/contrib/opensolaris/common/atomic/powerpc64/opensolaris_atomic.S optional zfs powerpc64 compile-with "${ZFS_S}" crypto/blowfish/bf_enc.c optional crypto | ipsec crypto/des/des_enc.c optional crypto | ipsec | netsmb dev/bm/if_bm.c optional bm powermac dev/adb/adb_bus.c optional adb dev/adb/adb_kbd.c optional adb dev/adb/adb_mouse.c optional adb dev/adb/adb_hb_if.m optional adb dev/adb/adb_if.m optional adb dev/adb/adb_buttons.c optional adb dev/agp/agp_apple.c optional agp powermac dev/cfi/cfi_bus_fdt.c optional cfi fdt dev/fb/fb.c optional sc dev/fdt/fdt_powerpc.c optional fdt dev/hwpmc/hwpmc_powerpc.c optional hwpmc dev/iicbus/ad7417.c optional ad7417 powermac +dev/iicbus/ds1631.c optional ds1631 powermac dev/iicbus/ds1775.c optional ds1775 powermac dev/iicbus/max6690.c optional max6690 powermac dev/kbd/kbd.c optional sc dev/nand/nfc_fsl.c optional nand mpc85xx dev/ofw/openfirm.c optional aim | fdt dev/ofw/openfirmio.c optional aim | fdt dev/ofw/ofw_bus_if.m optional aim | fdt dev/ofw/ofw_if.m optional aim | fdt dev/ofw/ofw_bus_subr.c optional aim | fdt dev/ofw/ofw_console.c optional aim dev/ofw/ofw_disk.c optional ofwd aim dev/ofw/ofw_fdt.c optional fdt dev/ofw/ofw_iicbus.c optional iicbus aim dev/ofw/ofw_standard.c optional aim powerpc dev/powermac_nvram/powermac_nvram.c optional powermac_nvram powermac dev/quicc/quicc_bfe_fdt.c optional quicc mpc85xx dev/scc/scc_bfe_macio.c optional scc powermac dev/sec/sec.c optional sec mpc85xx dev/sound/macio/aoa.c optional snd_davbus | snd_ai2s powermac dev/sound/macio/davbus.c optional snd_davbus powermac dev/sound/macio/i2s.c optional snd_ai2s powermac dev/sound/macio/snapper.c optional snd_ai2s iicbus powermac dev/sound/macio/tumbler.c optional snd_ai2s iicbus powermac dev/syscons/scgfbrndr.c optional sc dev/syscons/scterm-teken.c optional sc dev/syscons/scvtb.c optional sc dev/tsec/if_tsec.c optional tsec dev/tsec/if_tsec_fdt.c optional tsec fdt dev/uart/uart_cpu_powerpc.c optional uart aim kern/kern_clocksource.c standard kern/subr_dummy_vdso_tc.c standard kern/syscalls.c optional ktr libkern/ashldi3.c optional powerpc libkern/ashrdi3.c optional powerpc libkern/bcmp.c standard libkern/cmpdi2.c optional powerpc libkern/divdi3.c optional powerpc libkern/ffs.c standard libkern/ffsl.c standard libkern/fls.c standard libkern/flsl.c standard libkern/lshrdi3.c optional powerpc libkern/memchr.c optional fdt libkern/memmove.c standard libkern/memset.c standard libkern/moddi3.c optional powerpc libkern/qdivrem.c optional powerpc libkern/ucmpdi2.c optional powerpc libkern/udivdi3.c optional powerpc libkern/umoddi3.c optional powerpc powerpc/aim/clock.c optional aim powerpc/aim/copyinout.c optional aim powerpc/aim/interrupt.c optional aim powerpc/aim/locore.S optional aim no-obj powerpc/aim/machdep.c optional aim powerpc/aim/mmu_oea.c optional aim powerpc powerpc/aim/mmu_oea64.c optional aim powerpc/aim/moea64_if.m optional aim powerpc/aim/moea64_native.c optional aim powerpc/aim/mp_cpudep.c optional aim smp powerpc/aim/nexus.c optional aim powerpc/aim/slb.c optional aim powerpc64 powerpc/aim/swtch32.S optional aim powerpc powerpc/aim/swtch64.S optional aim powerpc64 powerpc/aim/trap.c optional aim powerpc/aim/uma_machdep.c optional aim powerpc/aim/vm_machdep.c optional aim powerpc/booke/clock.c optional booke powerpc/booke/copyinout.c optional booke powerpc/booke/interrupt.c optional booke powerpc/booke/locore.S optional booke no-obj powerpc/booke/machdep.c optional booke powerpc/booke/machdep_e500.c optional booke_e500 powerpc/booke/mp_cpudep.c optional booke smp powerpc/booke/platform_bare.c optional mpc85xx powerpc/booke/pmap.c optional booke powerpc/booke/swtch.S optional booke powerpc/booke/trap.c optional booke powerpc/booke/vm_machdep.c optional booke powerpc/cpufreq/dfs.c optional cpufreq powerpc/cpufreq/pcr.c optional cpufreq aim powerpc/fpu/fpu_add.c optional fpu_emu powerpc powerpc/fpu/fpu_compare.c optional fpu_emu powerpc powerpc/fpu/fpu_div.c optional fpu_emu powerpc powerpc/fpu/fpu_emu.c optional fpu_emu powerpc powerpc/fpu/fpu_explode.c optional fpu_emu powerpc powerpc/fpu/fpu_implode.c optional fpu_emu powerpc powerpc/fpu/fpu_mul.c optional fpu_emu powerpc powerpc/fpu/fpu_sqrt.c optional fpu_emu powerpc powerpc/fpu/fpu_subr.c optional fpu_emu powerpc powerpc/mambo/mambocall.S optional mambo powerpc/mambo/mambo.c optional mambo powerpc/mambo/mambo_console.c optional mambo powerpc/mambo/mambo_disk.c optional mambo powerpc/mambo/mambo_openpic.c optional mambo powerpc/mpc85xx/atpic.c optional mpc85xx isa powerpc/mpc85xx/ds1553_bus_fdt.c optional ds1553 fdt powerpc/mpc85xx/ds1553_core.c optional ds1553 powerpc/mpc85xx/i2c.c optional iicbus fdt powerpc/mpc85xx/isa.c optional mpc85xx isa powerpc/mpc85xx/lbc.c optional mpc85xx powerpc/mpc85xx/mpc85xx.c optional mpc85xx powerpc/mpc85xx/nexus.c optional mpc85xx powerpc/mpc85xx/pci_fdt.c optional pci mpc85xx powerpc/ofw/ofw_cpu.c optional aim powerpc/ofw/ofw_machdep.c optional aim powerpc/ofw/ofw_pci.c optional pci aim powerpc/ofw/ofw_pcibus.c optional pci aim powerpc/ofw/ofw_pcib_pci.c optional pci aim powerpc/ofw/ofw_real.c optional aim powerpc/ofw/ofw_syscons.c optional sc aim powerpc/ofw/ofwcall32.S optional aim powerpc powerpc/ofw/ofwcall64.S optional aim powerpc64 powerpc/ofw/ofwmagic.S optional aim powerpc/ofw/rtas.c optional aim powerpc/powermac/ata_kauai.c optional powermac ata | powermac atamacio powerpc/powermac/ata_macio.c optional powermac ata | powermac atamacio powerpc/powermac/ata_dbdma.c optional powermac ata | powermac atamacio powerpc/powermac/atibl.c optional powermac atibl powerpc/powermac/cuda.c optional powermac cuda powerpc/powermac/cpcht.c optional powermac pci powerpc/powermac/dbdma.c optional powermac pci powerpc/powermac/fcu.c optional powermac fcu powerpc/powermac/grackle.c optional powermac pci powerpc/powermac/hrowpic.c optional powermac pci powerpc/powermac/kiic.c optional powermac kiic powerpc/powermac/macgpio.c optional powermac pci powerpc/powermac/macio.c optional powermac pci powerpc/powermac/nvbl.c optional powermac nvbl powerpc/powermac/openpic_macio.c optional powermac pci powerpc/powermac/platform_powermac.c optional powermac powerpc/powermac/powermac_thermal.c optional powermac powerpc/powermac/pswitch.c optional powermac pswitch powerpc/powermac/pmu.c optional powermac pmu powerpc/powermac/smu.c optional powermac smu powerpc/powermac/smusat.c optional powermac smu powerpc/powermac/uninorth.c optional powermac powerpc/powermac/uninorthpci.c optional powermac pci powerpc/powermac/vcoregpio.c optional powermac powerpc/powermac/windtunnel.c optional powermac windtunnel powerpc/powerpc/altivec.c optional aim powerpc/powerpc/autoconf.c standard powerpc/powerpc/bcopy.c standard powerpc/powerpc/bus_machdep.c standard powerpc/powerpc/busdma_machdep.c standard powerpc/powerpc/copystr.c standard powerpc/powerpc/cpu.c standard powerpc/powerpc/db_disasm.c optional ddb powerpc/powerpc/db_hwwatch.c optional ddb powerpc/powerpc/db_interface.c optional ddb powerpc/powerpc/db_trace.c optional ddb powerpc/powerpc/dump_machdep.c standard powerpc/powerpc/elf32_machdep.c optional powerpc | compat_freebsd32 powerpc/powerpc/elf64_machdep.c optional powerpc64 powerpc/powerpc/exec_machdep.c standard powerpc/powerpc/fpu.c optional aim powerpc/powerpc/fuswintr.c standard powerpc/powerpc/gdb_machdep.c optional gdb powerpc/powerpc/in_cksum.c optional inet | inet6 powerpc/powerpc/intr_machdep.c standard powerpc/powerpc/iommu_if.m standard powerpc/powerpc/mem.c optional mem powerpc/powerpc/mmu_if.m standard powerpc/powerpc/mp_machdep.c optional smp powerpc/powerpc/openpic.c standard powerpc/powerpc/openpic_fdt.c optional fdt powerpc/powerpc/pic_if.m standard powerpc/powerpc/pmap_dispatch.c standard powerpc/powerpc/platform.c standard powerpc/powerpc/platform_if.m standard powerpc/powerpc/sc_machdep.c optional sc powerpc/powerpc/setjmp.S standard powerpc/powerpc/sigcode32.S optional powerpc | compat_freebsd32 powerpc/powerpc/sigcode64.S optional powerpc64 powerpc/powerpc/stack_machdep.c optional ddb | stack powerpc/powerpc/suswintr.c standard powerpc/powerpc/syncicache.c standard powerpc/powerpc/sys_machdep.c standard powerpc/powerpc/uio_machdep.c standard powerpc/ps3/ehci_ps3.c optional ps3 ehci powerpc/ps3/ohci_ps3.c optional ps3 ohci powerpc/ps3/if_glc.c optional ps3 glc powerpc/ps3/mmu_ps3.c optional ps3 powerpc/ps3/platform_ps3.c optional ps3 powerpc/ps3/ps3bus.c optional ps3 powerpc/ps3/ps3cdrom.c optional ps3 scbus powerpc/ps3/ps3disk.c optional ps3 powerpc/ps3/ps3pic.c optional ps3 powerpc/ps3/ps3_syscons.c optional ps3 sc powerpc/ps3/ps3-hvcall.S optional ps3 sc powerpc/psim/iobus.c optional psim powerpc/psim/ata_iobus.c optional ata psim powerpc/psim/openpic_iobus.c optional psim powerpc/psim/uart_iobus.c optional uart psim compat/freebsd32/freebsd32_ioctl.c optional compat_freebsd32 compat/freebsd32/freebsd32_misc.c optional compat_freebsd32 compat/freebsd32/freebsd32_syscalls.c optional compat_freebsd32 compat/freebsd32/freebsd32_sysent.c optional compat_freebsd32 kern/imgact_elf32.c optional compat_freebsd32 diff --git a/sys/dev/iicbus/ds1631.c b/sys/dev/iicbus/ds1631.c new file mode 100644 index 000000000000..51531ef8dbc1 --- /dev/null +++ b/sys/dev/iicbus/ds1631.c @@ -0,0 +1,413 @@ +/*- + * Copyright (c) 2012 Andreas Tobler + * All rights reserved. + * + * 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 AUTHOR ``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 AUTHOR 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include + +/* Sensor: Maxim DS1631 */ + +#define DS1631_STOP 0x22 +#define DS1631_START 0x51 +#define DS1631_RESET 0x54 +#define DS1631_TEMP 0xAA +#define DS1631_CONTROL 0xAC +#define DS1631_CONTROL_1SHOT 0x01 +#define DS1631_CONTROL_9BIT 0x00 +#define DS1631_CONTROL_10BIT 0x04 +#define DS1631_CONTROL_11BIT 0x08 +#define DS1631_CONTROL_12BIT 0x0C + + + +/* Regular bus attachment functions */ +static int ds1631_probe(device_t); +static int ds1631_attach(device_t); + +struct ds1631_softc { + struct pmac_therm sc_sensor; + device_t sc_dev; + struct intr_config_hook enum_hook; + uint32_t sc_addr; + uint32_t init_done; +}; + +struct write_data { + uint8_t reg; + uint8_t val; +}; + +struct read_data { + uint8_t reg; + uint16_t val; +}; + +/* Utility functions */ +static int ds1631_sensor_read(struct ds1631_softc *sc); +static int ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS); +static void ds1631_start(void *xdev); +static int ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg, + uint8_t *data); +static int ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg, + uint16_t *data); +static int ds1631_write(device_t dev, uint32_t addr, uint8_t reg, + uint8_t *buff, int len); + +static device_method_t ds1631_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, ds1631_probe), + DEVMETHOD(device_attach, ds1631_attach), + { 0, 0 }, +}; + +static driver_t ds1631_driver = { + "ds1631", + ds1631_methods, + sizeof(struct ds1631_softc) +}; + +static devclass_t ds1631_devclass; + +DRIVER_MODULE(ds1631, iicbus, ds1631_driver, ds1631_devclass, 0, 0); + +static int +ds1631_write(device_t dev, uint32_t addr, uint8_t reg, uint8_t *buff, int len) +{ + uint8_t buf[4]; + int try = 0; + + struct iic_msg msg[] = { + { addr, IIC_M_WR, 0, buf } + }; + + /* Prepare the write msg. */ + msg[0].len = len + 1; + buf[0] = reg; + memcpy(buf + 1, buff, len); + + for (;;) + { + if (iicbus_transfer(dev, msg, 1) == 0) + return (0); + if (++try > 5) { + device_printf(dev, "iicbus write failed\n"); + return (-1); + } + pause("ds1631_write", hz); + } +} + +static int +ds1631_read_1(device_t dev, uint32_t addr, uint8_t reg, uint8_t *data) +{ + uint8_t buf[4]; + int err, try = 0; + + struct iic_msg msg[2] = { + { addr, IIC_M_WR, 1, ® }, + { addr, IIC_M_RD, 1, buf }, + }; + + for (;;) + { + err = iicbus_transfer(dev, msg, 2); + if (err != 0) + goto retry; + + *data = *((uint8_t*)buf); + return (0); + retry: + if (++try > 5) { + device_printf(dev, "iicbus read failed\n"); + return (-1); + } + pause("ds1631_read_1", hz); + } +} + +static int +ds1631_read_2(device_t dev, uint32_t addr, uint8_t reg, uint16_t *data) +{ + uint8_t buf[4]; + int err, try = 0; + + struct iic_msg msg[2] = { + { addr, IIC_M_WR, 1, ® }, + { addr, IIC_M_RD, 2, buf }, + }; + + for (;;) + { + err = iicbus_transfer(dev, msg, 2); + if (err != 0) + goto retry; + + *data = *((uint16_t*)buf); + return (0); + retry: + if (++try > 5) { + device_printf(dev, "iicbus read failed\n"); + return (-1); + } + pause("ds1631_read_2", hz); + } +} + +static int +ds1631_probe(device_t dev) +{ + const char *name, *compatible; + struct ds1631_softc *sc; + + name = ofw_bus_get_name(dev); + compatible = ofw_bus_get_compat(dev); + + if (!name) + return (ENXIO); + + if (strcmp(name, "temp-monitor") != 0 || + strcmp(compatible, "ds1631") != 0 ) + return (ENXIO); + + sc = device_get_softc(dev); + sc->sc_dev = dev; + sc->sc_addr = iicbus_get_addr(dev); + + device_set_desc(dev, "Temp-Monitor DS1631"); + + return (0); +} + +static int +ds1631_attach(device_t dev) +{ + struct ds1631_softc *sc; + + sc = device_get_softc(dev); + + sc->enum_hook.ich_func = ds1631_start; + sc->enum_hook.ich_arg = dev; + + /* + * We have to wait until interrupts are enabled. I2C read and write + * only works if the interrupts are available. + * The unin/i2c is controlled by the htpic on unin. But this is not + * the master. The openpic on mac-io is controlling the htpic. + * This one gets attached after the mac-io probing and then the + * interrupts will be available. + */ + + if (config_intrhook_establish(&sc->enum_hook) != 0) + return (ENOMEM); + + return (0); +} +static int +ds1631_init(device_t dev, uint32_t addr) +{ + uint8_t conf; + int err; + struct ds1631_softc *sc; + + sc = device_get_softc(dev); + + err = ds1631_read_1(dev, addr, DS1631_CONTROL, &conf); + if (err < 0) { + device_printf(dev, "ds1631 read config failed: %x\n", err); + return (-1); + } + + /* Stop the conversion if not in 1SHOT mode. */ + if (conf & ~DS1631_CONTROL_1SHOT) + err = ds1631_write(dev, addr, DS1631_STOP, &conf, 0); + + /* + * Setup the resolution, 10-bit is enough. Each bit increase in + * resolution doubles the conversion time. + */ + conf = DS1631_CONTROL_10BIT; + + err = ds1631_write(dev, addr, DS1631_CONTROL, &conf, 1); + if (err < 0) { + device_printf(dev, "ds1631 write config failed: %x\n", err); + return (-1); + } + + /* And now start....*/ + err = ds1631_write(dev, addr, DS1631_START, &conf, 0); + + if (err < 0) { + device_printf(dev, "ds1631 write start failed: %x\n", err); + return (-1); + } + + sc->init_done = 1; + + return (0); + +} +static void +ds1631_start(void *xdev) +{ + phandle_t child, node; + struct ds1631_softc *sc; + struct sysctl_oid *oid, *sensroot_oid; + struct sysctl_ctx_list *ctx; + ssize_t plen; + int i; + char sysctl_desc[40], sysctl_name[40]; + + device_t dev = (device_t)xdev; + + sc = device_get_softc(dev); + + child = ofw_bus_get_node(dev); + + ctx = device_get_sysctl_ctx(dev); + sensroot_oid = SYSCTL_ADD_NODE(ctx, + SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor", + CTLFLAG_RD, 0, "DS1631 Sensor Information"); + + if (OF_getprop(child, "hwsensor-zone", &sc->sc_sensor.zone, + sizeof(int)) < 0) + sc->sc_sensor.zone = 0; + + plen = OF_getprop(child, "hwsensor-location", sc->sc_sensor.name, + sizeof(sc->sc_sensor.name)); + if (plen == -1) { + /* + * Ok, no hwsensor-location property, so let's look for a + * location property on a sub node. + */ + for (node = OF_child(child); node; node = OF_peer(node)) + plen = OF_getprop(node, "location", sc->sc_sensor.name, + sizeof(sc->sc_sensor.name)); + } + + if (plen == -1) { + strcpy(sysctl_name, "sensor"); + } else { + for (i = 0; i < strlen(sc->sc_sensor.name); i++) { + sysctl_name[i] = tolower(sc->sc_sensor.name[i]); + if (isspace(sysctl_name[i])) + sysctl_name[i] = '_'; + } + sysctl_name[i] = 0; + } + + /* Make up target temperatures. These are low, for the drive bay. */ + if (sc->sc_sensor.zone == 0) { + sc->sc_sensor.target_temp = 400 + ZERO_C_TO_K; + sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K; + } else { + sc->sc_sensor.target_temp = 300 + ZERO_C_TO_K; + sc->sc_sensor.max_temp = 500 + ZERO_C_TO_K; + } + + sc->sc_sensor.read = + (int (*)(struct pmac_therm *sc))(ds1631_sensor_read); + pmac_thermal_sensor_register(&sc->sc_sensor); + + sprintf(sysctl_desc,"%s %s", sc->sc_sensor.name, "(C)"); + oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sensroot_oid), + OID_AUTO, sysctl_name, CTLFLAG_RD, 0, + "Sensor Information"); + SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "temp", + CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, + 0, ds1631_sensor_sysctl, "IK", sysctl_desc); + + config_intrhook_disestablish(&sc->enum_hook); +} + +static int +ds1631_sensor_read(struct ds1631_softc *sc) +{ + uint16_t buf[2]; + uint16_t read; + int err; + + if (!sc->init_done) + ds1631_init(sc->sc_dev, sc->sc_addr); + + err = ds1631_read_2(sc->sc_dev, sc->sc_addr, DS1631_TEMP, buf); + if (err < 0) { + device_printf(sc->sc_dev, "ds1631 read TEMP failed: %x\n", err); + return (-1); + } + + read = *((int16_t *)buf); + + /* + * The default mode of the ADC is 12-bit, the resolution is 0.0625 C + * per bit. The temperature is in tenth kelvin. + * We use 10-bit resolution which seems enough, resolution is 0.25 C. + */ + + return (((int16_t)(read) >> 6) * 25 / 10 + ZERO_C_TO_K); +} + +static int +ds1631_sensor_sysctl(SYSCTL_HANDLER_ARGS) +{ + device_t dev; + struct ds1631_softc *sc; + int error; + unsigned int temp; + + dev = arg1; + sc = device_get_softc(dev); + + temp = ds1631_sensor_read(sc); + if (temp < 0) + return (EIO); + + error = sysctl_handle_int(oidp, &temp, 0, req); + + return (error); +}