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