Home | History | Annotate | Line # | Download | only in i2c
ds1307.c revision 1.27
      1  1.27   thorpej /*	$NetBSD: ds1307.c,v 1.27 2018/06/18 17:07:07 thorpej Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4   1.1   thorpej  * Copyright (c) 2003 Wasabi Systems, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
      8   1.1   thorpej  *
      9   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10   1.1   thorpej  * modification, are permitted provided that the following conditions
     11   1.1   thorpej  * are met:
     12   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18   1.1   thorpej  *    must display the following acknowledgement:
     19   1.1   thorpej  *      This product includes software developed for the NetBSD Project by
     20   1.1   thorpej  *      Wasabi Systems, Inc.
     21   1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22   1.1   thorpej  *    or promote products derived from this software without specific prior
     23   1.1   thorpej  *    written permission.
     24   1.1   thorpej  *
     25   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26   1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1   thorpej  */
     37   1.1   thorpej 
     38   1.9     lukem #include <sys/cdefs.h>
     39  1.27   thorpej __KERNEL_RCSID(0, "$NetBSD: ds1307.c,v 1.27 2018/06/18 17:07:07 thorpej Exp $");
     40   1.9     lukem 
     41   1.1   thorpej #include <sys/param.h>
     42   1.1   thorpej #include <sys/systm.h>
     43   1.1   thorpej #include <sys/device.h>
     44   1.1   thorpej #include <sys/kernel.h>
     45   1.1   thorpej #include <sys/fcntl.h>
     46   1.1   thorpej #include <sys/uio.h>
     47   1.1   thorpej #include <sys/conf.h>
     48   1.1   thorpej #include <sys/event.h>
     49   1.1   thorpej 
     50   1.1   thorpej #include <dev/clock_subr.h>
     51   1.1   thorpej 
     52   1.1   thorpej #include <dev/i2c/i2cvar.h>
     53   1.1   thorpej #include <dev/i2c/ds1307reg.h>
     54  1.19  macallan #include <dev/sysmon/sysmonvar.h>
     55   1.1   thorpej 
     56  1.25  riastrad #include "ioconf.h"
     57  1.25  riastrad 
     58  1.15      matt struct dsrtc_model {
     59  1.26   thorpej 	const i2c_addr_t *dm_valid_addrs;
     60  1.15      matt 	uint16_t dm_model;
     61  1.15      matt 	uint8_t dm_ch_reg;
     62  1.15      matt 	uint8_t dm_ch_value;
     63  1.24   aymeric 	uint8_t dm_vbaten_reg;
     64  1.24   aymeric 	uint8_t dm_vbaten_value;
     65  1.15      matt 	uint8_t dm_rtc_start;
     66  1.15      matt 	uint8_t dm_rtc_size;
     67  1.15      matt 	uint8_t dm_nvram_start;
     68  1.15      matt 	uint8_t dm_nvram_size;
     69  1.15      matt 	uint8_t dm_flags;
     70  1.24   aymeric #define	DSRTC_FLAG_CLOCK_HOLD		0x01
     71  1.24   aymeric #define	DSRTC_FLAG_BCD			0x02
     72  1.24   aymeric #define	DSRTC_FLAG_TEMP			0x04
     73  1.24   aymeric #define DSRTC_FLAG_VBATEN		0x08
     74  1.24   aymeric #define	DSRTC_FLAG_YEAR_START_2K	0x10
     75  1.24   aymeric #define	DSRTC_FLAG_CLOCK_HOLD_REVERSED	0x20
     76  1.15      matt };
     77  1.15      matt 
     78  1.26   thorpej static const char *ds1307_compats[] = { "dallas,ds1307", "maxim,ds1307", NULL };
     79  1.27   thorpej static const i2c_addr_t ds1307_valid_addrs[] = { DS1307_ADDR, 0 };
     80  1.27   thorpej static const struct dsrtc_model ds1307_model = {
     81  1.27   thorpej 	.dm_valid_addrs = ds1307_valid_addrs,
     82  1.27   thorpej 	.dm_model = 1307,
     83  1.27   thorpej 	.dm_ch_reg = DSXXXX_SECONDS,
     84  1.27   thorpej 	.dm_ch_value = DS1307_SECONDS_CH,
     85  1.27   thorpej 	.dm_rtc_start = DS1307_RTC_START,
     86  1.27   thorpej 	.dm_rtc_size = DS1307_RTC_SIZE,
     87  1.27   thorpej 	.dm_nvram_start = DS1307_NVRAM_START,
     88  1.27   thorpej 	.dm_nvram_size = DS1307_NVRAM_SIZE,
     89  1.27   thorpej 	.dm_flags = DSRTC_FLAG_BCD | DSRTC_FLAG_CLOCK_HOLD,
     90  1.27   thorpej };
     91  1.27   thorpej 
     92  1.26   thorpej static const char *ds1339_compats[] = { "dallas,ds1339", "maxim,ds1339", NULL };
     93  1.27   thorpej static const struct dsrtc_model ds1339_model = {
     94  1.27   thorpej 	.dm_valid_addrs = ds1307_valid_addrs,
     95  1.27   thorpej 	.dm_model = 1339,
     96  1.27   thorpej 	.dm_rtc_start = DS1339_RTC_START,
     97  1.27   thorpej 	.dm_rtc_size = DS1339_RTC_SIZE,
     98  1.27   thorpej 	.dm_flags = DSRTC_FLAG_BCD,
     99  1.27   thorpej };
    100  1.27   thorpej 
    101  1.26   thorpej static const char *ds1340_compats[] = { "dallas,ds1340", "maxim,ds1340", NULL };
    102  1.27   thorpej static const struct dsrtc_model ds1340_model = {
    103  1.27   thorpej 	.dm_valid_addrs = ds1307_valid_addrs,
    104  1.27   thorpej 	.dm_model = 1340,
    105  1.27   thorpej 	.dm_ch_reg = DSXXXX_SECONDS,
    106  1.27   thorpej 	.dm_ch_value = DS1340_SECONDS_EOSC,
    107  1.27   thorpej 	.dm_rtc_start = DS1340_RTC_START,
    108  1.27   thorpej 	.dm_rtc_size = DS1340_RTC_SIZE,
    109  1.27   thorpej 	.dm_flags = DSRTC_FLAG_BCD,
    110  1.27   thorpej };
    111  1.27   thorpej 
    112  1.26   thorpej static const char *ds1672_compats[] = { "dallas,ds1672", "maxim,ds1672", NULL };
    113  1.27   thorpej static const struct dsrtc_model ds1672_model = {
    114  1.27   thorpej 	.dm_valid_addrs = ds1307_valid_addrs,
    115  1.27   thorpej 	.dm_model = 1672,
    116  1.27   thorpej 	.dm_rtc_start = DS1672_RTC_START,
    117  1.27   thorpej 	.dm_rtc_size = DS1672_RTC_SIZE,
    118  1.27   thorpej 	.dm_ch_reg = DS1672_CONTROL,
    119  1.27   thorpej 	.dm_ch_value = DS1672_CONTROL_CH,
    120  1.27   thorpej 	.dm_flags = 0,
    121  1.27   thorpej };
    122  1.27   thorpej 
    123  1.26   thorpej static const char *ds3231_compats[] = { "dallas,ds3231", "maxim,ds3231", NULL };
    124  1.27   thorpej static const struct dsrtc_model ds3231_model = {
    125  1.27   thorpej 	.dm_valid_addrs = ds1307_valid_addrs,
    126  1.27   thorpej 	.dm_model = 3231,
    127  1.27   thorpej 	.dm_rtc_start = DS3232_RTC_START,
    128  1.27   thorpej 	.dm_rtc_size = DS3232_RTC_SIZE,
    129  1.27   thorpej 	.dm_flags = DSRTC_FLAG_BCD | DSRTC_FLAG_TEMP,
    130  1.27   thorpej };
    131  1.27   thorpej 
    132  1.26   thorpej static const char *ds3232_compats[] = { "dallas,ds3232", "maxim,ds3232", NULL };
    133  1.27   thorpej static const struct dsrtc_model ds3232_model = {
    134  1.27   thorpej 	.dm_valid_addrs = ds1307_valid_addrs,
    135  1.27   thorpej 	.dm_model = 3232,
    136  1.27   thorpej 	.dm_rtc_start = DS3232_RTC_START,
    137  1.27   thorpej 	.dm_rtc_size = DS3232_RTC_SIZE,
    138  1.27   thorpej 	.dm_nvram_start = DS3232_NVRAM_START,
    139  1.27   thorpej 	.dm_nvram_size = DS3232_NVRAM_SIZE,
    140  1.27   thorpej 	/*
    141  1.27   thorpej 	 * XXX
    142  1.27   thorpej 	 * the DS3232 likely has the temperature sensor too but I can't
    143  1.27   thorpej 	 * easily verify or test that right now
    144  1.27   thorpej 	 */
    145  1.27   thorpej 	.dm_flags = DSRTC_FLAG_BCD,
    146  1.27   thorpej };
    147  1.26   thorpej 
    148  1.26   thorpej 				/* XXX vendor prefix */
    149  1.26   thorpej static const char *mcp7940_compats[] = { "microchip,mcp7940", NULL };
    150  1.26   thorpej static const i2c_addr_t mcp7940_valid_addrs[] = { MCP7940_ADDR, 0 };
    151  1.27   thorpej static const struct dsrtc_model mcp7940_model = {
    152  1.27   thorpej 	.dm_valid_addrs = mcp7940_valid_addrs,
    153  1.27   thorpej 	.dm_model = 7940,
    154  1.27   thorpej 	.dm_rtc_start = DS1307_RTC_START,
    155  1.27   thorpej 	.dm_rtc_size = DS1307_RTC_SIZE,
    156  1.27   thorpej 	.dm_ch_reg = DSXXXX_SECONDS,
    157  1.27   thorpej 	.dm_ch_value = DS1307_SECONDS_CH,
    158  1.27   thorpej 	.dm_vbaten_reg = DSXXXX_DAY,
    159  1.27   thorpej 	.dm_vbaten_value = MCP7940_TOD_DAY_VBATEN,
    160  1.27   thorpej 	.dm_nvram_start = MCP7940_NVRAM_START,
    161  1.27   thorpej 	.dm_nvram_size = MCP7940_NVRAM_SIZE,
    162  1.27   thorpej 	.dm_flags = DSRTC_FLAG_BCD | DSRTC_FLAG_CLOCK_HOLD |
    163  1.27   thorpej 		DSRTC_FLAG_VBATEN | DSRTC_FLAG_CLOCK_HOLD_REVERSED,
    164  1.27   thorpej };
    165  1.26   thorpej 
    166  1.27   thorpej static const struct device_compatible_entry dsrtc_compat_data[] = {
    167  1.27   thorpej 	DEVICE_COMPAT_ENTRY_WITH_DATA(ds1307_compats,  &ds1307_model),
    168  1.27   thorpej 	DEVICE_COMPAT_ENTRY_WITH_DATA(ds1339_compats,  &ds1339_model),
    169  1.27   thorpej 	DEVICE_COMPAT_ENTRY_WITH_DATA(ds1340_compats,  &ds1340_model),
    170  1.27   thorpej 	DEVICE_COMPAT_ENTRY_WITH_DATA(ds1672_compats,  &ds1672_model),
    171  1.27   thorpej 	DEVICE_COMPAT_ENTRY_WITH_DATA(ds3231_compats,  &ds3231_model),
    172  1.27   thorpej 	DEVICE_COMPAT_ENTRY_WITH_DATA(ds3232_compats,  &ds3232_model),
    173  1.27   thorpej 	DEVICE_COMPAT_ENTRY_WITH_DATA(mcp7940_compats, &mcp7940_model),
    174  1.27   thorpej 	DEVICE_COMPAT_TERMINATOR
    175  1.15      matt };
    176  1.15      matt 
    177   1.1   thorpej struct dsrtc_softc {
    178  1.11   xtraeme 	device_t sc_dev;
    179   1.1   thorpej 	i2c_tag_t sc_tag;
    180  1.15      matt 	uint8_t sc_address;
    181  1.15      matt 	bool sc_open;
    182  1.15      matt 	struct dsrtc_model sc_model;
    183   1.1   thorpej 	struct todr_chip_handle sc_todr;
    184  1.19  macallan 	struct sysmon_envsys *sc_sme;
    185  1.19  macallan 	envsys_data_t sc_sensor;
    186   1.1   thorpej };
    187   1.1   thorpej 
    188  1.11   xtraeme static void	dsrtc_attach(device_t, device_t, void *);
    189  1.11   xtraeme static int	dsrtc_match(device_t, cfdata_t, void *);
    190   1.1   thorpej 
    191  1.11   xtraeme CFATTACH_DECL_NEW(dsrtc, sizeof(struct dsrtc_softc),
    192   1.1   thorpej     dsrtc_match, dsrtc_attach, NULL, NULL);
    193   1.1   thorpej 
    194   1.1   thorpej dev_type_open(dsrtc_open);
    195   1.1   thorpej dev_type_close(dsrtc_close);
    196   1.1   thorpej dev_type_read(dsrtc_read);
    197   1.1   thorpej dev_type_write(dsrtc_write);
    198   1.1   thorpej 
    199   1.1   thorpej const struct cdevsw dsrtc_cdevsw = {
    200  1.17  dholland 	.d_open = dsrtc_open,
    201  1.17  dholland 	.d_close = dsrtc_close,
    202  1.17  dholland 	.d_read = dsrtc_read,
    203  1.17  dholland 	.d_write = dsrtc_write,
    204  1.17  dholland 	.d_ioctl = noioctl,
    205  1.17  dholland 	.d_stop = nostop,
    206  1.17  dholland 	.d_tty = notty,
    207  1.17  dholland 	.d_poll = nopoll,
    208  1.17  dholland 	.d_mmap = nommap,
    209  1.17  dholland 	.d_kqfilter = nokqfilter,
    210  1.18  dholland 	.d_discard = nodiscard,
    211  1.17  dholland 	.d_flag = D_OTHER
    212   1.1   thorpej };
    213   1.1   thorpej 
    214  1.15      matt static int dsrtc_gettime_ymdhms(struct todr_chip_handle *, struct clock_ymdhms *);
    215  1.15      matt static int dsrtc_settime_ymdhms(struct todr_chip_handle *, struct clock_ymdhms *);
    216  1.15      matt static int dsrtc_clock_read_ymdhms(struct dsrtc_softc *, struct clock_ymdhms *);
    217  1.15      matt static int dsrtc_clock_write_ymdhms(struct dsrtc_softc *, struct clock_ymdhms *);
    218  1.15      matt 
    219  1.15      matt static int dsrtc_gettime_timeval(struct todr_chip_handle *, struct timeval *);
    220  1.15      matt static int dsrtc_settime_timeval(struct todr_chip_handle *, struct timeval *);
    221  1.15      matt static int dsrtc_clock_read_timeval(struct dsrtc_softc *, time_t *);
    222  1.15      matt static int dsrtc_clock_write_timeval(struct dsrtc_softc *, time_t);
    223  1.15      matt 
    224  1.19  macallan static int dsrtc_read_temp(struct dsrtc_softc *, uint32_t *);
    225  1.19  macallan static void dsrtc_refresh(struct sysmon_envsys *, envsys_data_t *);
    226  1.19  macallan 
    227  1.15      matt static const struct dsrtc_model *
    228  1.26   thorpej dsrtc_model_by_number(u_int model)
    229  1.15      matt {
    230  1.27   thorpej 	const struct device_compatible_entry *dce;
    231  1.27   thorpej 	const struct dsrtc_model *dm;
    232  1.27   thorpej 
    233  1.27   thorpej 	/* no model given, assume it's a DS1307 */
    234  1.15      matt 	if (model == 0)
    235  1.27   thorpej 		return &ds1307_model;
    236  1.15      matt 
    237  1.27   thorpej 	for (dce = dsrtc_compat_data;
    238  1.27   thorpej 	     DEVICE_COMPAT_ENTRY_IS_TERMINATOR(dce) == false; dce++) {
    239  1.27   thorpej 		dm = DEVICE_COMPAT_ENTRY_GET_PTR(dce);
    240  1.15      matt 		if (dm->dm_model == model)
    241  1.15      matt 			return dm;
    242  1.15      matt 	}
    243  1.15      matt 	return NULL;
    244  1.15      matt }
    245   1.1   thorpej 
    246  1.26   thorpej static const struct dsrtc_model *
    247  1.26   thorpej dsrtc_model_by_compat(const struct i2c_attach_args *ia)
    248  1.26   thorpej {
    249  1.27   thorpej 	const struct dsrtc_model *dm = NULL;
    250  1.27   thorpej 	const struct device_compatible_entry *dce;
    251  1.26   thorpej 
    252  1.27   thorpej 	dce = iic_compatible_match(ia, dsrtc_compat_data, NULL);
    253  1.27   thorpej 	if (dce != NULL)
    254  1.27   thorpej 		dm = DEVICE_COMPAT_ENTRY_GET_PTR(dce);
    255  1.26   thorpej 
    256  1.27   thorpej 	return dm;
    257  1.26   thorpej }
    258  1.26   thorpej 
    259  1.26   thorpej static bool
    260  1.26   thorpej dsrtc_is_valid_addr_for_model(const struct dsrtc_model *dm, i2c_addr_t addr)
    261  1.26   thorpej {
    262  1.26   thorpej 
    263  1.26   thorpej 	for (int i = 0; dm->dm_valid_addrs[i] != 0; i++) {
    264  1.26   thorpej 		if (addr == dm->dm_valid_addrs[i])
    265  1.26   thorpej 			return true;
    266  1.26   thorpej 	}
    267  1.26   thorpej 	return false;
    268  1.26   thorpej }
    269  1.26   thorpej 
    270   1.1   thorpej static int
    271  1.11   xtraeme dsrtc_match(device_t parent, cfdata_t cf, void *arg)
    272   1.1   thorpej {
    273   1.1   thorpej 	struct i2c_attach_args *ia = arg;
    274  1.26   thorpej 	const struct dsrtc_model *dm;
    275  1.26   thorpej 	int match_result;
    276  1.26   thorpej 
    277  1.27   thorpej 	if (iic_use_direct_match(ia, cf, dsrtc_compat_data, &match_result))
    278  1.26   thorpej 		return match_result;
    279  1.26   thorpej 
    280  1.26   thorpej 	dm = dsrtc_model_by_number(cf->cf_flags & 0xffff);
    281  1.26   thorpej 	if (dm == NULL)
    282  1.26   thorpej 		return 0;
    283  1.26   thorpej 
    284  1.26   thorpej 	if (dsrtc_is_valid_addr_for_model(dm, ia->ia_addr))
    285  1.26   thorpej 		return I2C_MATCH_ADDRESS_ONLY;
    286   1.1   thorpej 
    287  1.13       phx 	return 0;
    288   1.1   thorpej }
    289   1.1   thorpej 
    290   1.1   thorpej static void
    291  1.11   xtraeme dsrtc_attach(device_t parent, device_t self, void *arg)
    292   1.1   thorpej {
    293   1.5   thorpej 	struct dsrtc_softc *sc = device_private(self);
    294   1.1   thorpej 	struct i2c_attach_args *ia = arg;
    295  1.26   thorpej 	const struct dsrtc_model *dm;
    296  1.26   thorpej 
    297  1.26   thorpej 	if ((dm = dsrtc_model_by_compat(ia)) == NULL)
    298  1.26   thorpej 		dm = dsrtc_model_by_number(device_cfdata(self)->cf_flags);
    299  1.26   thorpej 
    300  1.26   thorpej 	if (dm == NULL) {
    301  1.26   thorpej 		aprint_error(": unable to determine model!\n");
    302  1.26   thorpej 		return;
    303  1.26   thorpej 	}
    304   1.1   thorpej 
    305  1.15      matt 	aprint_naive(": Real-time Clock%s\n",
    306  1.15      matt 	    dm->dm_nvram_size > 0 ? "/NVRAM" : "");
    307  1.15      matt 	aprint_normal(": DS%u Real-time Clock%s\n", dm->dm_model,
    308  1.15      matt 	    dm->dm_nvram_size > 0 ? "/NVRAM" : "");
    309   1.1   thorpej 
    310   1.1   thorpej 	sc->sc_tag = ia->ia_tag;
    311   1.1   thorpej 	sc->sc_address = ia->ia_addr;
    312  1.15      matt 	sc->sc_model = *dm;
    313  1.11   xtraeme 	sc->sc_dev = self;
    314   1.1   thorpej 	sc->sc_open = 0;
    315   1.1   thorpej 	sc->sc_todr.cookie = sc;
    316  1.15      matt 	if (dm->dm_flags & DSRTC_FLAG_BCD) {
    317  1.15      matt 		sc->sc_todr.todr_gettime_ymdhms = dsrtc_gettime_ymdhms;
    318  1.15      matt 		sc->sc_todr.todr_settime_ymdhms = dsrtc_settime_ymdhms;
    319  1.15      matt 	} else {
    320  1.15      matt 		sc->sc_todr.todr_gettime = dsrtc_gettime_timeval;
    321  1.15      matt 		sc->sc_todr.todr_settime = dsrtc_settime_timeval;
    322  1.15      matt 	}
    323   1.1   thorpej 	sc->sc_todr.todr_setwen = NULL;
    324   1.1   thorpej 
    325   1.1   thorpej 	todr_attach(&sc->sc_todr);
    326  1.19  macallan 	if ((sc->sc_model.dm_flags & DSRTC_FLAG_TEMP) != 0) {
    327  1.19  macallan 		int error;
    328  1.19  macallan 
    329  1.19  macallan 		sc->sc_sme = sysmon_envsys_create();
    330  1.19  macallan 		sc->sc_sme->sme_name = device_xname(self);
    331  1.19  macallan 		sc->sc_sme->sme_cookie = sc;
    332  1.19  macallan 		sc->sc_sme->sme_refresh = dsrtc_refresh;
    333  1.19  macallan 
    334  1.19  macallan 		sc->sc_sensor.units =  ENVSYS_STEMP;
    335  1.19  macallan 		sc->sc_sensor.state = ENVSYS_SINVALID;
    336  1.19  macallan 		sc->sc_sensor.flags = 0;
    337  1.19  macallan 		(void)strlcpy(sc->sc_sensor.desc, "temperature",
    338  1.19  macallan 		    sizeof(sc->sc_sensor.desc));
    339  1.19  macallan 
    340  1.19  macallan 		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
    341  1.19  macallan 			aprint_error_dev(self, "unable to attach sensor\n");
    342  1.19  macallan 			goto bad;
    343  1.19  macallan 		}
    344  1.19  macallan 
    345  1.19  macallan 		error = sysmon_envsys_register(sc->sc_sme);
    346  1.19  macallan 		if (error) {
    347  1.19  macallan 			aprint_error_dev(self,
    348  1.19  macallan 			    "error %d registering with sysmon\n", error);
    349  1.19  macallan 			goto bad;
    350  1.19  macallan 		}
    351  1.19  macallan 	}
    352  1.19  macallan 	return;
    353  1.19  macallan bad:
    354  1.19  macallan 	sysmon_envsys_destroy(sc->sc_sme);
    355   1.1   thorpej }
    356   1.1   thorpej 
    357   1.1   thorpej /*ARGSUSED*/
    358   1.1   thorpej int
    359   1.4       abs dsrtc_open(dev_t dev, int flag, int fmt, struct lwp *l)
    360   1.1   thorpej {
    361   1.1   thorpej 	struct dsrtc_softc *sc;
    362   1.1   thorpej 
    363  1.12   tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    364  1.14       phx 		return ENXIO;
    365   1.1   thorpej 
    366   1.1   thorpej 	/* XXX: Locking */
    367   1.1   thorpej 	if (sc->sc_open)
    368  1.14       phx 		return EBUSY;
    369   1.1   thorpej 
    370  1.15      matt 	sc->sc_open = true;
    371  1.14       phx 	return 0;
    372   1.1   thorpej }
    373   1.1   thorpej 
    374   1.1   thorpej /*ARGSUSED*/
    375   1.1   thorpej int
    376   1.4       abs dsrtc_close(dev_t dev, int flag, int fmt, struct lwp *l)
    377   1.1   thorpej {
    378   1.1   thorpej 	struct dsrtc_softc *sc;
    379   1.1   thorpej 
    380  1.12   tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    381  1.14       phx 		return ENXIO;
    382   1.1   thorpej 
    383  1.15      matt 	sc->sc_open = false;
    384  1.14       phx 	return 0;
    385   1.1   thorpej }
    386   1.1   thorpej 
    387   1.1   thorpej /*ARGSUSED*/
    388   1.1   thorpej int
    389   1.1   thorpej dsrtc_read(dev_t dev, struct uio *uio, int flags)
    390   1.1   thorpej {
    391   1.1   thorpej 	struct dsrtc_softc *sc;
    392  1.15      matt 	int error;
    393   1.1   thorpej 
    394  1.12   tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    395  1.14       phx 		return ENXIO;
    396   1.1   thorpej 
    397  1.15      matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    398  1.15      matt 	if (uio->uio_offset >= dm->dm_nvram_size)
    399  1.14       phx 		return EINVAL;
    400   1.1   thorpej 
    401   1.1   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    402  1.14       phx 		return error;
    403   1.1   thorpej 
    404  1.15      matt 	KASSERT(uio->uio_offset >= 0);
    405  1.15      matt 	while (uio->uio_resid && uio->uio_offset < dm->dm_nvram_size) {
    406  1.15      matt 		uint8_t ch, cmd;
    407  1.15      matt 		const u_int a = uio->uio_offset;
    408  1.15      matt 		cmd = a + dm->dm_nvram_start;
    409  1.15      matt 		if ((error = iic_exec(sc->sc_tag,
    410  1.15      matt 		    uio->uio_resid > 1 ? I2C_OP_READ : I2C_OP_READ_WITH_STOP,
    411  1.15      matt 		    sc->sc_address, &cmd, 1, &ch, 1, 0)) != 0) {
    412   1.1   thorpej 			iic_release_bus(sc->sc_tag, 0);
    413  1.11   xtraeme 			aprint_error_dev(sc->sc_dev,
    414  1.16      matt 			    "%s: read failed at 0x%x: %d\n",
    415  1.16      matt 			    __func__, a, error);
    416  1.14       phx 			return error;
    417   1.1   thorpej 		}
    418   1.1   thorpej 		if ((error = uiomove(&ch, 1, uio)) != 0) {
    419   1.1   thorpej 			iic_release_bus(sc->sc_tag, 0);
    420  1.14       phx 			return error;
    421   1.1   thorpej 		}
    422   1.1   thorpej 	}
    423   1.1   thorpej 
    424   1.1   thorpej 	iic_release_bus(sc->sc_tag, 0);
    425   1.1   thorpej 
    426  1.14       phx 	return 0;
    427   1.1   thorpej }
    428   1.1   thorpej 
    429   1.1   thorpej /*ARGSUSED*/
    430   1.1   thorpej int
    431   1.1   thorpej dsrtc_write(dev_t dev, struct uio *uio, int flags)
    432   1.1   thorpej {
    433   1.1   thorpej 	struct dsrtc_softc *sc;
    434  1.15      matt 	int error;
    435   1.1   thorpej 
    436  1.12   tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    437  1.14       phx 		return ENXIO;
    438   1.1   thorpej 
    439  1.15      matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    440  1.15      matt 	if (uio->uio_offset >= dm->dm_nvram_size)
    441  1.14       phx 		return EINVAL;
    442   1.1   thorpej 
    443   1.1   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    444  1.14       phx 		return error;
    445   1.1   thorpej 
    446  1.15      matt 	while (uio->uio_resid && uio->uio_offset < dm->dm_nvram_size) {
    447  1.15      matt 		uint8_t cmdbuf[2];
    448  1.15      matt 		const u_int a = (int)uio->uio_offset;
    449  1.15      matt 		cmdbuf[0] = a + dm->dm_nvram_start;
    450   1.1   thorpej 		if ((error = uiomove(&cmdbuf[1], 1, uio)) != 0)
    451   1.1   thorpej 			break;
    452   1.1   thorpej 
    453   1.1   thorpej 		if ((error = iic_exec(sc->sc_tag,
    454   1.1   thorpej 		    uio->uio_resid ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
    455   1.1   thorpej 		    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, 0)) != 0) {
    456  1.11   xtraeme 			aprint_error_dev(sc->sc_dev,
    457  1.16      matt 			    "%s: write failed at 0x%x: %d\n",
    458  1.16      matt 			    __func__, a, error);
    459   1.1   thorpej 			break;
    460   1.1   thorpej 		}
    461   1.1   thorpej 	}
    462   1.1   thorpej 
    463   1.1   thorpej 	iic_release_bus(sc->sc_tag, 0);
    464   1.1   thorpej 
    465  1.14       phx 	return error;
    466   1.1   thorpej }
    467   1.1   thorpej 
    468   1.1   thorpej static int
    469  1.15      matt dsrtc_gettime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    470   1.1   thorpej {
    471   1.1   thorpej 	struct dsrtc_softc *sc = ch->cookie;
    472   1.7   gdamore 	struct clock_ymdhms check;
    473   1.1   thorpej 	int retries;
    474   1.1   thorpej 
    475   1.7   gdamore 	memset(dt, 0, sizeof(*dt));
    476   1.1   thorpej 	memset(&check, 0, sizeof(check));
    477   1.1   thorpej 
    478   1.1   thorpej 	/*
    479   1.1   thorpej 	 * Since we don't support Burst Read, we have to read the clock twice
    480   1.1   thorpej 	 * until we get two consecutive identical results.
    481   1.1   thorpej 	 */
    482   1.1   thorpej 	retries = 5;
    483   1.1   thorpej 	do {
    484  1.15      matt 		dsrtc_clock_read_ymdhms(sc, dt);
    485  1.15      matt 		dsrtc_clock_read_ymdhms(sc, &check);
    486   1.7   gdamore 	} while (memcmp(dt, &check, sizeof(check)) != 0 && --retries);
    487   1.1   thorpej 
    488  1.14       phx 	return 0;
    489   1.1   thorpej }
    490   1.1   thorpej 
    491   1.1   thorpej static int
    492  1.15      matt dsrtc_settime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    493   1.1   thorpej {
    494   1.1   thorpej 	struct dsrtc_softc *sc = ch->cookie;
    495   1.1   thorpej 
    496  1.15      matt 	if (dsrtc_clock_write_ymdhms(sc, dt) == 0)
    497  1.14       phx 		return -1;
    498   1.1   thorpej 
    499  1.14       phx 	return 0;
    500   1.1   thorpej }
    501   1.1   thorpej 
    502   1.1   thorpej static int
    503  1.15      matt dsrtc_clock_read_ymdhms(struct dsrtc_softc *sc, struct clock_ymdhms *dt)
    504   1.1   thorpej {
    505  1.15      matt 	struct dsrtc_model * const dm = &sc->sc_model;
    506  1.15      matt 	uint8_t bcd[DSXXXX_RTC_SIZE], cmdbuf[1];
    507  1.16      matt 	int error;
    508  1.15      matt 
    509  1.15      matt 	KASSERT(DSXXXX_RTC_SIZE >= dm->dm_rtc_size);
    510   1.1   thorpej 
    511  1.16      matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    512  1.11   xtraeme 		aprint_error_dev(sc->sc_dev,
    513  1.16      matt 		    "%s: failed to acquire I2C bus: %d\n",
    514  1.16      matt 		    __func__, error);
    515  1.14       phx 		return 0;
    516   1.1   thorpej 	}
    517   1.1   thorpej 
    518   1.1   thorpej 	/* Read each RTC register in order. */
    519  1.16      matt 	for (u_int i = 0; !error && i < dm->dm_rtc_size; i++) {
    520  1.15      matt 		cmdbuf[0] = dm->dm_rtc_start + i;
    521   1.1   thorpej 
    522  1.16      matt 		error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    523  1.16      matt 		    sc->sc_address, cmdbuf, 1, &bcd[i], 1, I2C_F_POLL);
    524   1.1   thorpej 	}
    525   1.1   thorpej 
    526   1.1   thorpej 	/* Done with I2C */
    527   1.1   thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    528   1.1   thorpej 
    529  1.16      matt 	if (error != 0) {
    530  1.16      matt 		aprint_error_dev(sc->sc_dev,
    531  1.16      matt 		    "%s: failed to read rtc at 0x%x: %d\n",
    532  1.16      matt 		    __func__, cmdbuf[0], error);
    533  1.16      matt 		return 0;
    534  1.16      matt 	}
    535  1.16      matt 
    536   1.1   thorpej 	/*
    537  1.15      matt 	 * Convert the RTC's register values into something useable
    538   1.1   thorpej 	 */
    539  1.21  christos 	dt->dt_sec = bcdtobin(bcd[DSXXXX_SECONDS] & DSXXXX_SECONDS_MASK);
    540  1.21  christos 	dt->dt_min = bcdtobin(bcd[DSXXXX_MINUTES] & DSXXXX_MINUTES_MASK);
    541   1.1   thorpej 
    542  1.15      matt 	if ((bcd[DSXXXX_HOURS] & DSXXXX_HOURS_12HRS_MODE) != 0) {
    543  1.21  christos 		dt->dt_hour = bcdtobin(bcd[DSXXXX_HOURS] &
    544  1.15      matt 		    DSXXXX_HOURS_12MASK) % 12; /* 12AM -> 0, 12PM -> 12 */
    545  1.15      matt 		if (bcd[DSXXXX_HOURS] & DSXXXX_HOURS_12HRS_PM)
    546   1.1   thorpej 			dt->dt_hour += 12;
    547  1.14       phx 	} else
    548  1.21  christos 		dt->dt_hour = bcdtobin(bcd[DSXXXX_HOURS] &
    549  1.15      matt 		    DSXXXX_HOURS_24MASK);
    550   1.1   thorpej 
    551  1.21  christos 	dt->dt_day = bcdtobin(bcd[DSXXXX_DATE] & DSXXXX_DATE_MASK);
    552  1.21  christos 	dt->dt_mon = bcdtobin(bcd[DSXXXX_MONTH] & DSXXXX_MONTH_MASK);
    553   1.1   thorpej 
    554   1.1   thorpej 	/* XXX: Should be an MD way to specify EPOCH used by BIOS/Firmware */
    555  1.24   aymeric 	if (sc->sc_model.dm_flags & DSRTC_FLAG_YEAR_START_2K)
    556  1.24   aymeric 		dt->dt_year = bcdtobin(bcd[DSXXXX_YEAR]) + 2000;
    557  1.24   aymeric 	else {
    558  1.24   aymeric 		dt->dt_year = bcdtobin(bcd[DSXXXX_YEAR]) + POSIX_BASE_YEAR;
    559  1.24   aymeric 		if (bcd[DSXXXX_MONTH] & DSXXXX_MONTH_CENTURY)
    560  1.24   aymeric 			dt->dt_year += 100;
    561  1.24   aymeric 	}
    562   1.1   thorpej 
    563  1.14       phx 	return 1;
    564   1.1   thorpej }
    565   1.1   thorpej 
    566   1.1   thorpej static int
    567  1.15      matt dsrtc_clock_write_ymdhms(struct dsrtc_softc *sc, struct clock_ymdhms *dt)
    568   1.1   thorpej {
    569  1.15      matt 	struct dsrtc_model * const dm = &sc->sc_model;
    570  1.15      matt 	uint8_t bcd[DSXXXX_RTC_SIZE], cmdbuf[2];
    571  1.16      matt 	int error;
    572  1.15      matt 
    573  1.15      matt 	KASSERT(DSXXXX_RTC_SIZE >= dm->dm_rtc_size);
    574   1.1   thorpej 
    575   1.1   thorpej 	/*
    576  1.15      matt 	 * Convert our time representation into something the DSXXXX
    577   1.1   thorpej 	 * can understand.
    578   1.1   thorpej 	 */
    579  1.21  christos 	bcd[DSXXXX_SECONDS] = bintobcd(dt->dt_sec);
    580  1.21  christos 	bcd[DSXXXX_MINUTES] = bintobcd(dt->dt_min);
    581  1.21  christos 	bcd[DSXXXX_HOURS] = bintobcd(dt->dt_hour); /* DSXXXX_HOURS_12HRS_MODE=0 */
    582  1.21  christos 	bcd[DSXXXX_DATE] = bintobcd(dt->dt_day);
    583  1.21  christos 	bcd[DSXXXX_DAY] = bintobcd(dt->dt_wday);
    584  1.21  christos 	bcd[DSXXXX_MONTH] = bintobcd(dt->dt_mon);
    585  1.21  christos 	bcd[DSXXXX_YEAR] = bintobcd((dt->dt_year - POSIX_BASE_YEAR) % 100);
    586  1.15      matt 	if (dt->dt_year - POSIX_BASE_YEAR >= 100)
    587  1.15      matt 		bcd[DSXXXX_MONTH] |= DSXXXX_MONTH_CENTURY;
    588   1.1   thorpej 
    589  1.16      matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    590  1.11   xtraeme 		aprint_error_dev(sc->sc_dev,
    591  1.16      matt 		    "%s: failed to acquire I2C bus: %d\n",
    592  1.16      matt 		    __func__, error);
    593  1.14       phx 		return 0;
    594   1.1   thorpej 	}
    595   1.1   thorpej 
    596   1.1   thorpej 	/* Stop the clock */
    597  1.15      matt 	cmdbuf[0] = dm->dm_ch_reg;
    598  1.15      matt 
    599  1.16      matt 	if ((error = iic_exec(sc->sc_tag, I2C_OP_READ, sc->sc_address,
    600  1.16      matt 	    cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) != 0) {
    601  1.15      matt 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    602  1.15      matt 		aprint_error_dev(sc->sc_dev,
    603  1.16      matt 		    "%s: failed to read Hold Clock: %d\n",
    604  1.16      matt 		    __func__, error);
    605  1.15      matt 		return 0;
    606  1.15      matt 	}
    607  1.15      matt 
    608  1.24   aymeric 	if (sc->sc_model.dm_flags & DSRTC_FLAG_CLOCK_HOLD_REVERSED)
    609  1.24   aymeric 		cmdbuf[1] &= ~dm->dm_ch_value;
    610  1.24   aymeric 	else
    611  1.24   aymeric 		cmdbuf[1] |= dm->dm_ch_value;
    612   1.1   thorpej 
    613  1.16      matt 	if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE, sc->sc_address,
    614  1.16      matt 	    cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) != 0) {
    615   1.1   thorpej 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    616  1.11   xtraeme 		aprint_error_dev(sc->sc_dev,
    617  1.16      matt 		    "%s: failed to write Hold Clock: %d\n",
    618  1.16      matt 		    __func__, error);
    619  1.14       phx 		return 0;
    620   1.1   thorpej 	}
    621   1.1   thorpej 
    622   1.1   thorpej 	/*
    623   1.1   thorpej 	 * Write registers in reverse order. The last write (to the Seconds
    624   1.1   thorpej 	 * register) will undo the Clock Hold, above.
    625   1.1   thorpej 	 */
    626  1.15      matt 	uint8_t op = I2C_OP_WRITE;
    627  1.15      matt 	for (signed int i = dm->dm_rtc_size - 1; i >= 0; i--) {
    628  1.15      matt 		cmdbuf[0] = dm->dm_rtc_start + i;
    629  1.24   aymeric 		if ((dm->dm_flags & DSRTC_FLAG_VBATEN) &&
    630  1.24   aymeric 				dm->dm_rtc_start + i == dm->dm_vbaten_reg)
    631  1.24   aymeric 			bcd[i] |= dm->dm_vbaten_value;
    632  1.15      matt 		if (dm->dm_rtc_start + i == dm->dm_ch_reg) {
    633  1.15      matt 			op = I2C_OP_WRITE_WITH_STOP;
    634  1.24   aymeric 			if (dm->dm_flags & DSRTC_FLAG_CLOCK_HOLD_REVERSED)
    635  1.24   aymeric 				bcd[i] |= dm->dm_ch_value;
    636  1.15      matt 		}
    637  1.16      matt 		if ((error = iic_exec(sc->sc_tag, op, sc->sc_address,
    638  1.16      matt 		    cmdbuf, 1, &bcd[i], 1, I2C_F_POLL)) != 0) {
    639   1.1   thorpej 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    640  1.11   xtraeme 			aprint_error_dev(sc->sc_dev,
    641  1.16      matt 			    "%s: failed to write rtc at 0x%x: %d\n",
    642  1.16      matt 			    __func__, i, error);
    643   1.1   thorpej 			/* XXX: Clock Hold is likely still asserted! */
    644  1.14       phx 			return 0;
    645   1.1   thorpej 		}
    646   1.1   thorpej 	}
    647  1.15      matt 	/*
    648  1.15      matt 	 * If the clock hold register isn't the same register as seconds,
    649  1.15      matt 	 * we need to reeanble the clock.
    650  1.15      matt 	 */
    651  1.15      matt 	if (op != I2C_OP_WRITE_WITH_STOP) {
    652  1.15      matt 		cmdbuf[0] = dm->dm_ch_reg;
    653  1.24   aymeric 		if (dm->dm_flags & DSRTC_FLAG_CLOCK_HOLD_REVERSED)
    654  1.24   aymeric 			cmdbuf[1] |= dm->dm_ch_value;
    655  1.24   aymeric 		else
    656  1.24   aymeric 			cmdbuf[1] &= ~dm->dm_ch_value;
    657  1.15      matt 
    658  1.16      matt 		if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    659  1.16      matt 		    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1,
    660  1.16      matt 		    I2C_F_POLL)) != 0) {
    661  1.15      matt 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    662  1.15      matt 			aprint_error_dev(sc->sc_dev,
    663  1.16      matt 			    "%s: failed to Hold Clock: %d\n",
    664  1.16      matt 			    __func__, error);
    665  1.15      matt 			return 0;
    666  1.15      matt 		}
    667  1.15      matt 	}
    668   1.1   thorpej 
    669   1.1   thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    670   1.1   thorpej 
    671  1.14       phx 	return 1;
    672   1.1   thorpej }
    673  1.15      matt 
    674  1.15      matt static int
    675  1.15      matt dsrtc_gettime_timeval(struct todr_chip_handle *ch, struct timeval *tv)
    676  1.15      matt {
    677  1.15      matt 	struct dsrtc_softc *sc = ch->cookie;
    678  1.15      matt 	struct timeval check;
    679  1.15      matt 	int retries;
    680  1.15      matt 
    681  1.15      matt 	memset(tv, 0, sizeof(*tv));
    682  1.15      matt 	memset(&check, 0, sizeof(check));
    683  1.15      matt 
    684  1.15      matt 	/*
    685  1.15      matt 	 * Since we don't support Burst Read, we have to read the clock twice
    686  1.15      matt 	 * until we get two consecutive identical results.
    687  1.15      matt 	 */
    688  1.15      matt 	retries = 5;
    689  1.15      matt 	do {
    690  1.15      matt 		dsrtc_clock_read_timeval(sc, &tv->tv_sec);
    691  1.15      matt 		dsrtc_clock_read_timeval(sc, &check.tv_sec);
    692  1.15      matt 	} while (memcmp(tv, &check, sizeof(check)) != 0 && --retries);
    693  1.15      matt 
    694  1.15      matt 	return 0;
    695  1.15      matt }
    696  1.15      matt 
    697  1.15      matt static int
    698  1.15      matt dsrtc_settime_timeval(struct todr_chip_handle *ch, struct timeval *tv)
    699  1.15      matt {
    700  1.15      matt 	struct dsrtc_softc *sc = ch->cookie;
    701  1.15      matt 
    702  1.15      matt 	if (dsrtc_clock_write_timeval(sc, tv->tv_sec) == 0)
    703  1.15      matt 		return -1;
    704  1.15      matt 
    705  1.15      matt 	return 0;
    706  1.15      matt }
    707  1.15      matt 
    708  1.15      matt /*
    709  1.15      matt  * The RTC probably has a nice Clock Burst Read/Write command, but we can't use
    710  1.15      matt  * it, since some I2C controllers don't support anything other than single-byte
    711  1.15      matt  * transfers.
    712  1.15      matt  */
    713  1.15      matt static int
    714  1.15      matt dsrtc_clock_read_timeval(struct dsrtc_softc *sc, time_t *tp)
    715  1.15      matt {
    716  1.15      matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    717  1.15      matt 	uint8_t buf[4];
    718  1.16      matt 	int error;
    719  1.15      matt 
    720  1.16      matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    721  1.16      matt 		aprint_error_dev(sc->sc_dev,
    722  1.16      matt 		    "%s: failed to acquire I2C bus: %d\n",
    723  1.16      matt 		    __func__, error);
    724  1.16      matt 		return 0;
    725  1.15      matt 	}
    726  1.15      matt 
    727  1.15      matt 	/* read all registers: */
    728  1.15      matt 	uint8_t reg = dm->dm_rtc_start;
    729  1.16      matt 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
    730  1.16      matt 	     &reg, 1, buf, 4, I2C_F_POLL);
    731  1.15      matt 
    732  1.15      matt 	/* Done with I2C */
    733  1.15      matt 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    734  1.15      matt 
    735  1.16      matt 	if (error != 0) {
    736  1.16      matt 		aprint_error_dev(sc->sc_dev,
    737  1.16      matt 		    "%s: failed to read rtc at 0x%x: %d\n",
    738  1.16      matt 		    __func__, reg, error);
    739  1.16      matt 		return 0;
    740  1.16      matt 	}
    741  1.16      matt 
    742  1.15      matt 	uint32_t v = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
    743  1.15      matt 	*tp = v;
    744  1.15      matt 
    745  1.15      matt 	aprint_debug_dev(sc->sc_dev, "%s: cntr=0x%08"PRIx32"\n",
    746  1.15      matt 	    __func__, v);
    747  1.15      matt 
    748  1.16      matt 	return 1;
    749  1.15      matt }
    750  1.15      matt 
    751  1.15      matt static int
    752  1.15      matt dsrtc_clock_write_timeval(struct dsrtc_softc *sc, time_t t)
    753  1.15      matt {
    754  1.15      matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    755  1.15      matt 	size_t buflen = dm->dm_rtc_size + 2;
    756  1.15      matt 	uint8_t buf[buflen];
    757  1.16      matt 	int error;
    758  1.15      matt 
    759  1.15      matt 	KASSERT((dm->dm_flags & DSRTC_FLAG_CLOCK_HOLD) == 0);
    760  1.15      matt 	KASSERT(dm->dm_ch_reg == dm->dm_rtc_start + 4);
    761  1.15      matt 
    762  1.15      matt 	buf[0] = dm->dm_rtc_start;
    763  1.15      matt 	buf[1] = (t >> 0) & 0xff;
    764  1.15      matt 	buf[2] = (t >> 8) & 0xff;
    765  1.15      matt 	buf[3] = (t >> 16) & 0xff;
    766  1.15      matt 	buf[4] = (t >> 24) & 0xff;
    767  1.15      matt 	buf[5] = 0;
    768  1.15      matt 
    769  1.16      matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    770  1.16      matt 		aprint_error_dev(sc->sc_dev,
    771  1.16      matt 		    "%s: failed to acquire I2C bus: %d\n",
    772  1.16      matt 		    __func__, error);
    773  1.16      matt 		return 0;
    774  1.15      matt 	}
    775  1.15      matt 
    776  1.16      matt 	error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
    777  1.16      matt 	    &buf, buflen, NULL, 0, I2C_F_POLL);
    778  1.16      matt 
    779  1.16      matt 	/* Done with I2C */
    780  1.16      matt 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    781  1.16      matt 
    782  1.15      matt 	/* send data */
    783  1.16      matt 	if (error != 0) {
    784  1.16      matt 		aprint_error_dev(sc->sc_dev,
    785  1.16      matt 		    "%s: failed to set time: %d\n",
    786  1.16      matt 		    __func__, error);
    787  1.16      matt 		return 0;
    788  1.15      matt 	}
    789  1.15      matt 
    790  1.16      matt 	return 1;
    791  1.15      matt }
    792  1.19  macallan 
    793  1.19  macallan static int
    794  1.19  macallan dsrtc_read_temp(struct dsrtc_softc *sc, uint32_t *temp)
    795  1.19  macallan {
    796  1.19  macallan 	int error, tc;
    797  1.19  macallan 	uint8_t reg = DS3232_TEMP_MSB;
    798  1.19  macallan 	uint8_t buf[2];
    799  1.19  macallan 
    800  1.19  macallan 	if ((sc->sc_model.dm_flags & DSRTC_FLAG_TEMP) == 0)
    801  1.19  macallan 		return ENOTSUP;
    802  1.19  macallan 
    803  1.19  macallan 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    804  1.19  macallan 		aprint_error_dev(sc->sc_dev,
    805  1.19  macallan 		    "%s: failed to acquire I2C bus: %d\n",
    806  1.19  macallan 		    __func__, error);
    807  1.19  macallan 		return 0;
    808  1.19  macallan 	}
    809  1.19  macallan 
    810  1.19  macallan 	/* read temperature registers: */
    811  1.19  macallan 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
    812  1.19  macallan 	     &reg, 1, buf, 2, I2C_F_POLL);
    813  1.19  macallan 
    814  1.19  macallan 	/* Done with I2C */
    815  1.19  macallan 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    816  1.19  macallan 
    817  1.19  macallan 	if (error != 0) {
    818  1.19  macallan 		aprint_error_dev(sc->sc_dev,
    819  1.19  macallan 		    "%s: failed to read temperature: %d\n",
    820  1.19  macallan 		    __func__, error);
    821  1.19  macallan 		return 0;
    822  1.19  macallan 	}
    823  1.19  macallan 
    824  1.19  macallan 	/* convert to microkelvin */
    825  1.19  macallan 	tc = buf[0] * 1000000 + (buf[1] >> 6) * 250000;
    826  1.19  macallan 	*temp = tc + 273150000;
    827  1.19  macallan 	return 1;
    828  1.19  macallan }
    829  1.19  macallan 
    830  1.19  macallan static void
    831  1.19  macallan dsrtc_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    832  1.19  macallan {
    833  1.19  macallan 	struct dsrtc_softc *sc = sme->sme_cookie;
    834  1.20    martin 	uint32_t temp = 0;	/* XXX gcc */
    835  1.19  macallan 
    836  1.19  macallan 	if (dsrtc_read_temp(sc, &temp) == 0) {
    837  1.19  macallan 		edata->state = ENVSYS_SINVALID;
    838  1.19  macallan 		return;
    839  1.19  macallan 	}
    840  1.19  macallan 
    841  1.19  macallan 	edata->value_cur = temp;
    842  1.19  macallan 
    843  1.19  macallan 	edata->state = ENVSYS_SVALID;
    844  1.19  macallan }
    845