Home | History | Annotate | Line # | Download | only in i2c
      1  1.25   thorpej /*	$NetBSD: x1226.c,v 1.25 2025/09/07 21:45:16 thorpej Exp $	*/
      2   1.1     shige 
      3   1.1     shige /*
      4   1.1     shige  * Copyright (c) 2003 Shigeyuki Fukushima.
      5   1.1     shige  * All rights reserved.
      6   1.1     shige  *
      7   1.1     shige  * Written by Shigeyuki Fukushima for the NetBSD Project.
      8   1.1     shige  *
      9   1.1     shige  * Redistribution and use in source and binary forms, with or without
     10   1.1     shige  * modification, are permitted provided that the following conditions
     11   1.1     shige  * are met:
     12   1.1     shige  * 1. Redistributions of source code must retain the above copyright
     13   1.1     shige  *    notice, this list of conditions and the following disclaimer.
     14   1.1     shige  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1     shige  *    notice, this list of conditions and the following disclaimer in the
     16   1.1     shige  *    documentation and/or other materials provided with the distribution.
     17   1.1     shige  * 3. All advertising materials mentioning features or use of this software
     18   1.1     shige  *    must display the following acknowledgement:
     19   1.1     shige  *      This product includes software developed for the NetBSD Project by
     20   1.1     shige  *      Shigeyuki Fukushima.
     21   1.1     shige  * 4. The name of Shigeyuki Fukushima may not be used to endorse
     22   1.1     shige  *    or promote products derived from this software without specific prior
     23   1.1     shige  *    written permission.
     24   1.1     shige  *
     25   1.1     shige  * THIS SOFTWARE IS PROVIDED BY SHIGEYUKI FUKUSHIMA ``AS IS'' AND
     26   1.1     shige  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1     shige  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1     shige  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SHIGEYUKI FUKUSHIMA
     29   1.1     shige  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1     shige  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1     shige  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1     shige  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1     shige  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1     shige  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1     shige  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1     shige  */
     37   1.1     shige 
     38   1.1     shige #include <sys/cdefs.h>
     39  1.25   thorpej __KERNEL_RCSID(0, "$NetBSD: x1226.c,v 1.25 2025/09/07 21:45:16 thorpej Exp $");
     40   1.1     shige 
     41   1.1     shige #include <sys/param.h>
     42   1.1     shige #include <sys/systm.h>
     43   1.1     shige #include <sys/device.h>
     44   1.1     shige #include <sys/kernel.h>
     45   1.1     shige #include <sys/fcntl.h>
     46   1.1     shige #include <sys/uio.h>
     47   1.1     shige #include <sys/conf.h>
     48   1.1     shige #include <sys/event.h>
     49   1.1     shige 
     50   1.1     shige #include <dev/clock_subr.h>
     51   1.1     shige 
     52   1.1     shige #include <dev/i2c/i2cvar.h>
     53   1.1     shige #include <dev/i2c/x1226reg.h>
     54   1.1     shige 
     55  1.20  riastrad #include "ioconf.h"
     56  1.20  riastrad 
     57   1.1     shige struct xrtc_softc {
     58  1.12   xtraeme 	device_t		sc_dev;
     59   1.1     shige 	i2c_tag_t		sc_tag;
     60   1.1     shige 	int			sc_address;
     61   1.1     shige 	int			sc_open;
     62   1.1     shige 	struct todr_chip_handle	sc_todr;
     63   1.1     shige };
     64   1.1     shige 
     65  1.12   xtraeme static void	xrtc_attach(device_t, device_t, void *);
     66  1.12   xtraeme static int	xrtc_match(device_t, cfdata_t, void *);
     67   1.1     shige 
     68  1.12   xtraeme CFATTACH_DECL_NEW(xrtc, sizeof(struct xrtc_softc),
     69   1.1     shige     xrtc_match, xrtc_attach, NULL, NULL);
     70   1.1     shige 
     71   1.1     shige dev_type_open(xrtc_open);
     72   1.1     shige dev_type_close(xrtc_close);
     73   1.1     shige dev_type_read(xrtc_read);
     74   1.1     shige dev_type_write(xrtc_write);
     75   1.1     shige 
     76   1.1     shige const struct cdevsw xrtc_cdevsw = {
     77  1.15  dholland 	.d_open = xrtc_open,
     78  1.15  dholland 	.d_close = xrtc_close,
     79  1.15  dholland 	.d_read = xrtc_read,
     80  1.15  dholland 	.d_write = xrtc_write,
     81  1.16     skrll 	.d_ioctl = noioctl,
     82  1.16     skrll 	.d_stop = nostop,
     83  1.16     skrll 	.d_tty = notty,
     84  1.16     skrll 	.d_poll = nopoll,
     85  1.16     skrll 	.d_mmap = nommap,
     86  1.16     skrll 	.d_kqfilter = nokqfilter,
     87  1.17  dholland 	.d_discard = nodiscard,
     88  1.16     skrll 	.d_flag = D_OTHER
     89   1.1     shige };
     90   1.1     shige 
     91   1.1     shige static int xrtc_clock_read(struct xrtc_softc *, struct clock_ymdhms *);
     92  1.23   thorpej static int xrtc_gettime_ymdhms(struct todr_chip_handle *,
     93  1.23   thorpej 			       struct clock_ymdhms *);
     94  1.23   thorpej static int xrtc_settime_ymdhms(struct todr_chip_handle *,
     95  1.23   thorpej 			       struct clock_ymdhms *);
     96   1.1     shige 
     97   1.1     shige /*
     98   1.1     shige  * xrtc_match()
     99   1.1     shige  */
    100   1.1     shige static int
    101  1.12   xtraeme xrtc_match(device_t parent, cfdata_t cf, void *arg)
    102   1.1     shige {
    103   1.1     shige 	struct i2c_attach_args *ia = arg;
    104   1.1     shige 
    105   1.1     shige 	/* match only this RTC devices */
    106   1.1     shige 	if (ia->ia_addr == X1226_ADDR)
    107  1.21   thorpej 		return (I2C_MATCH_ADDRESS_ONLY);
    108   1.1     shige 
    109   1.1     shige 	return (0);
    110   1.1     shige }
    111   1.1     shige 
    112   1.1     shige /*
    113   1.1     shige  * xrtc_attach()
    114   1.1     shige  */
    115   1.1     shige static void
    116  1.12   xtraeme xrtc_attach(device_t parent, device_t self, void *arg)
    117   1.1     shige {
    118   1.8   thorpej 	struct xrtc_softc *sc = device_private(self);
    119   1.1     shige 	struct i2c_attach_args *ia = arg;
    120   1.1     shige 
    121   1.1     shige 	aprint_naive(": Real-time Clock/NVRAM\n");
    122   1.1     shige 	aprint_normal(": Xicor X1226 Real-time Clock/NVRAM\n");
    123   1.1     shige 
    124   1.1     shige 	sc->sc_tag = ia->ia_tag;
    125   1.1     shige 	sc->sc_address = ia->ia_addr;
    126  1.12   xtraeme 	sc->sc_dev = self;
    127   1.1     shige 	sc->sc_open = 0;
    128  1.25   thorpej 	sc->sc_todr.todr_dev = self;
    129  1.23   thorpej 	sc->sc_todr.todr_gettime_ymdhms = xrtc_gettime_ymdhms;
    130  1.23   thorpej 	sc->sc_todr.todr_settime_ymdhms = xrtc_settime_ymdhms;
    131   1.1     shige 
    132   1.1     shige 	todr_attach(&sc->sc_todr);
    133   1.1     shige }
    134   1.1     shige 
    135   1.1     shige 
    136   1.1     shige /*ARGSUSED*/
    137   1.1     shige int
    138   1.7  christos xrtc_open(dev_t dev, int flag, int fmt, struct lwp *l)
    139   1.1     shige {
    140   1.1     shige 	struct xrtc_softc *sc;
    141   1.1     shige 
    142  1.13   tsutsui 	if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
    143   1.1     shige 		return (ENXIO);
    144   1.1     shige 
    145   1.1     shige 	/* XXX: Locking */
    146   1.1     shige 
    147   1.1     shige 	if (sc->sc_open)
    148   1.1     shige 		return (EBUSY);
    149   1.1     shige 
    150   1.1     shige 	sc->sc_open = 1;
    151   1.1     shige 	return (0);
    152   1.1     shige }
    153   1.1     shige 
    154   1.1     shige /*ARGSUSED*/
    155   1.1     shige int
    156   1.7  christos xrtc_close(dev_t dev, int flag, int fmt, struct lwp *l)
    157   1.1     shige {
    158   1.1     shige 	struct xrtc_softc *sc;
    159   1.1     shige 
    160  1.13   tsutsui 	if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
    161   1.1     shige 		return (ENXIO);
    162   1.1     shige 
    163   1.1     shige 	sc->sc_open = 0;
    164   1.1     shige 	return (0);
    165   1.1     shige }
    166   1.1     shige 
    167   1.1     shige /*ARGSUSED*/
    168   1.1     shige int
    169   1.1     shige xrtc_read(dev_t dev, struct uio *uio, int flags)
    170   1.1     shige {
    171   1.1     shige 	struct xrtc_softc *sc;
    172   1.1     shige 	u_int8_t ch, cmdbuf[2];
    173   1.1     shige 	int addr, error;
    174   1.1     shige 
    175  1.13   tsutsui 	if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
    176   1.1     shige 		return (ENXIO);
    177   1.1     shige 
    178   1.1     shige 	if (uio->uio_offset >= X1226_NVRAM_SIZE)
    179   1.1     shige 		return (EINVAL);
    180   1.1     shige 
    181   1.1     shige 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    182   1.1     shige 		return (error);
    183   1.1     shige 
    184   1.1     shige 	while (uio->uio_resid && uio->uio_offset < X1226_NVRAM_SIZE) {
    185   1.1     shige 		addr = (int)uio->uio_offset + X1226_NVRAM_START;
    186  1.18     joerg 		cmdbuf[0] = (addr >> 8) & 0xff;
    187  1.18     joerg 		cmdbuf[1] = addr & 0xff;
    188   1.1     shige 		if ((error = iic_exec(sc->sc_tag,
    189   1.1     shige 			I2C_OP_READ_WITH_STOP,
    190   1.1     shige 			sc->sc_address, cmdbuf, 2, &ch, 1, 0)) != 0) {
    191   1.1     shige 			iic_release_bus(sc->sc_tag, 0);
    192  1.12   xtraeme 			aprint_error_dev(sc->sc_dev,
    193  1.12   xtraeme 			    "xrtc_read: read failed at 0x%x\n",
    194  1.11    cegger 				(int)uio->uio_offset);
    195   1.1     shige 			return (error);
    196   1.1     shige 		}
    197   1.1     shige 		if ((error = uiomove(&ch, 1, uio)) != 0) {
    198   1.1     shige 			iic_release_bus(sc->sc_tag, 0);
    199   1.1     shige 			return (error);
    200   1.1     shige 		}
    201   1.1     shige 	}
    202   1.1     shige 
    203   1.1     shige 	iic_release_bus(sc->sc_tag, 0);
    204   1.1     shige 
    205   1.1     shige 	return (0);
    206   1.1     shige }
    207   1.1     shige 
    208   1.1     shige /*ARGSUSED*/
    209   1.1     shige int
    210   1.1     shige xrtc_write(dev_t dev, struct uio *uio, int flags)
    211   1.1     shige {
    212   1.1     shige 	struct xrtc_softc *sc;
    213   1.1     shige 	u_int8_t cmdbuf[3];
    214   1.1     shige 	int addr, error;
    215   1.1     shige 
    216  1.13   tsutsui 	if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
    217   1.1     shige 		return (ENXIO);
    218   1.1     shige 
    219   1.1     shige 	if (uio->uio_offset >= X1226_NVRAM_SIZE)
    220   1.1     shige 		return (EINVAL);
    221   1.1     shige 
    222   1.1     shige 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    223   1.1     shige 		return (error);
    224   1.1     shige 
    225   1.1     shige 	while (uio->uio_resid && uio->uio_offset < X1226_NVRAM_SIZE) {
    226   1.1     shige 		addr = (int)uio->uio_offset + X1226_NVRAM_START;
    227  1.18     joerg 		cmdbuf[0] = (addr >> 8) & 0xff;
    228  1.18     joerg 		cmdbuf[1] = addr & 0xff;
    229   1.1     shige 		if ((error = uiomove(&cmdbuf[2], 1, uio)) != 0) {
    230   1.1     shige 			break;
    231   1.1     shige 		}
    232   1.1     shige 		if ((error = iic_exec(sc->sc_tag,
    233   1.1     shige 			uio->uio_resid ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
    234   1.1     shige 			sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
    235   1.1     shige 			iic_release_bus(sc->sc_tag, 0);
    236  1.12   xtraeme 			aprint_error_dev(sc->sc_dev,
    237  1.12   xtraeme 			    "xrtc_write: write failed at 0x%x\n",
    238  1.11    cegger 				(int)uio->uio_offset);
    239   1.1     shige 			return (error);
    240   1.1     shige 		}
    241   1.1     shige 	}
    242   1.1     shige 
    243   1.1     shige 	iic_release_bus(sc->sc_tag, 0);
    244   1.1     shige 
    245   1.1     shige 	return (0);
    246   1.1     shige }
    247   1.1     shige 
    248   1.1     shige 
    249   1.1     shige static int
    250  1.23   thorpej xrtc_gettime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    251   1.1     shige {
    252  1.25   thorpej 	struct xrtc_softc *sc = device_private(ch->todr_dev);
    253  1.23   thorpej 	struct clock_ymdhms check;
    254   1.1     shige 	int retries;
    255  1.22   thorpej 	int error;
    256   1.1     shige 
    257  1.23   thorpej 	memset(dt, 0, sizeof(*dt));
    258   1.1     shige 	memset(&check, 0, sizeof(check));
    259   1.1     shige 
    260   1.1     shige 	retries = 5;
    261   1.1     shige 	do {
    262  1.23   thorpej 		if ((error = xrtc_clock_read(sc, dt)) == 0)
    263  1.22   thorpej 			error = xrtc_clock_read(sc, &check);
    264  1.22   thorpej 		if (error)
    265  1.22   thorpej 			return error;
    266  1.23   thorpej 	} while (memcmp(dt, &check, sizeof(check)) != 0 && --retries);
    267   1.1     shige 
    268   1.1     shige 	return (0);
    269   1.1     shige }
    270   1.1     shige 
    271   1.1     shige static int
    272   1.1     shige xrtc_clock_read(struct xrtc_softc *sc, struct clock_ymdhms *dt)
    273   1.1     shige {
    274   1.1     shige 	int i = 0;
    275   1.1     shige 	u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[2];
    276  1.22   thorpej 	int error;
    277   1.1     shige 
    278  1.22   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
    279  1.12   xtraeme 		aprint_error_dev(sc->sc_dev,
    280  1.12   xtraeme 		    "xrtc_clock_read: failed to acquire I2C bus\n");
    281  1.22   thorpej 		return (error);
    282   1.1     shige 	}
    283   1.1     shige 
    284   1.1     shige 	/* Read each RTC register in order */
    285   1.1     shige 	for (i = 0 ; i < X1226_REG_RTC_SIZE ; i++) {
    286   1.1     shige 		int addr = i + X1226_REG_RTC_BASE;
    287   1.2     shige 		cmdbuf[0] = (addr >> 8) & 0xff;
    288   1.2     shige 		cmdbuf[1] = addr & 0xff;
    289   1.1     shige 
    290  1.22   thorpej 		if ((error = iic_exec(sc->sc_tag,
    291   1.1     shige 			I2C_OP_READ_WITH_STOP,
    292   1.1     shige 			sc->sc_address, cmdbuf, 2,
    293  1.22   thorpej 			&bcd[i], 1, 0)) != 0) {
    294  1.22   thorpej 			iic_release_bus(sc->sc_tag, 0);
    295  1.12   xtraeme 			aprint_error_dev(sc->sc_dev,
    296  1.12   xtraeme 			    "xrtc_clock_read: failed to read rtc "
    297  1.11    cegger 				"at 0x%x\n", i);
    298  1.22   thorpej 			return (error);
    299   1.1     shige 		}
    300   1.1     shige 	}
    301   1.1     shige 
    302   1.1     shige 	/* Done with I2C */
    303  1.22   thorpej 	iic_release_bus(sc->sc_tag, 0);
    304   1.1     shige 
    305   1.1     shige 	/*
    306   1.1     shige 	 * Convert the X1226's register bcd values
    307   1.1     shige 	 */
    308  1.19  christos 	dt->dt_sec = bcdtobin(bcd[X1226_REG_SC - X1226_REG_RTC_BASE]
    309   1.1     shige 			& X1226_REG_SC_MASK);
    310  1.19  christos 	dt->dt_min = bcdtobin(bcd[X1226_REG_MN - X1226_REG_RTC_BASE]
    311   1.1     shige 			& X1226_REG_MN_MASK);
    312   1.3     shige 	if (!(bcd[X1226_REG_HR - X1226_REG_RTC_BASE] & X1226_FLAG_HR_24H)) {
    313  1.19  christos 		dt->dt_hour = bcdtobin(bcd[X1226_REG_HR - X1226_REG_RTC_BASE]
    314   1.1     shige 				& X1226_REG_HR12_MASK);
    315   1.1     shige 		if (bcd[X1226_REG_HR - X1226_REG_RTC_BASE] & X1226_FLAG_HR_12HPM) {
    316   1.1     shige 			dt->dt_hour += 12;
    317   1.1     shige 		}
    318   1.1     shige 	} else {
    319  1.19  christos 		dt->dt_hour = bcdtobin(bcd[X1226_REG_HR - X1226_REG_RTC_BASE]
    320   1.1     shige 			& X1226_REG_HR24_MASK);
    321   1.1     shige 	}
    322  1.19  christos 	dt->dt_wday = bcdtobin(bcd[X1226_REG_DW - X1226_REG_RTC_BASE]
    323   1.2     shige 			& X1226_REG_DT_MASK);
    324  1.19  christos 	dt->dt_day = bcdtobin(bcd[X1226_REG_DT - X1226_REG_RTC_BASE]
    325   1.1     shige 			& X1226_REG_DT_MASK);
    326  1.19  christos 	dt->dt_mon = bcdtobin(bcd[X1226_REG_MO - X1226_REG_RTC_BASE]
    327   1.1     shige 			& X1226_REG_MO_MASK);
    328  1.19  christos 	dt->dt_year = bcdtobin(bcd[X1226_REG_YR - X1226_REG_RTC_BASE]
    329   1.1     shige 			& X1226_REG_YR_MASK);
    330  1.19  christos 	dt->dt_year += bcdtobin(bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE]
    331   1.1     shige 			& X1226_REG_Y2K_MASK) * 100;
    332   1.1     shige 
    333  1.22   thorpej 	return (0);
    334   1.1     shige }
    335   1.1     shige 
    336   1.1     shige static int
    337  1.23   thorpej xrtc_settime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    338   1.1     shige {
    339  1.25   thorpej 	struct xrtc_softc *sc = device_private(ch->todr_dev);
    340   1.1     shige 	int i = 0, addr;
    341   1.1     shige 	u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[3];
    342  1.22   thorpej 	int error, error2;
    343   1.1     shige 
    344   1.1     shige 	/*
    345   1.1     shige 	 * Convert our time to bcd values
    346   1.1     shige 	 */
    347  1.19  christos 	bcd[X1226_REG_SC - X1226_REG_RTC_BASE] = bintobcd(dt->dt_sec);
    348  1.19  christos 	bcd[X1226_REG_MN - X1226_REG_RTC_BASE] = bintobcd(dt->dt_min);
    349  1.19  christos 	bcd[X1226_REG_HR - X1226_REG_RTC_BASE] = bintobcd(dt->dt_hour)
    350   1.1     shige 						| X1226_FLAG_HR_24H;
    351  1.19  christos 	bcd[X1226_REG_DW - X1226_REG_RTC_BASE] = bintobcd(dt->dt_wday);
    352  1.19  christos 	bcd[X1226_REG_DT - X1226_REG_RTC_BASE] = bintobcd(dt->dt_day);
    353  1.19  christos 	bcd[X1226_REG_MO - X1226_REG_RTC_BASE] = bintobcd(dt->dt_mon);
    354  1.19  christos 	bcd[X1226_REG_YR - X1226_REG_RTC_BASE] = bintobcd(dt->dt_year % 100);
    355  1.19  christos 	bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE] = bintobcd(dt->dt_year / 100);
    356   1.1     shige 
    357  1.22   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
    358  1.12   xtraeme 		aprint_error_dev(sc->sc_dev,
    359  1.12   xtraeme 		    "xrtc_clock_write: failed to acquire I2C bus\n");
    360  1.22   thorpej 		return (error);
    361   1.1     shige 	}
    362   1.1     shige 
    363   1.1     shige 	/* Unlock register: Write Enable Latch */
    364   1.1     shige 	addr = X1226_REG_SR;
    365   1.2     shige 	cmdbuf[0] = ((addr >> 8) & 0xff);
    366   1.2     shige 	cmdbuf[1] = (addr & 0xff);
    367   1.1     shige 	cmdbuf[2] = X1226_FLAG_SR_WEL;
    368  1.22   thorpej 	if ((error = iic_exec(sc->sc_tag,
    369   1.1     shige 		I2C_OP_WRITE_WITH_STOP,
    370  1.22   thorpej 		sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
    371  1.22   thorpej 		iic_release_bus(sc->sc_tag, 0);
    372  1.12   xtraeme 		aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
    373  1.11    cegger 			"failed to write-unlock status register(WEL=1)\n");
    374  1.22   thorpej 		return (error);
    375   1.1     shige 	}
    376   1.1     shige 
    377   1.1     shige 	/* Unlock register: Register Write Enable Latch */
    378   1.1     shige 	addr = X1226_REG_SR;
    379   1.2     shige 	cmdbuf[0] = ((addr >> 8) & 0xff);
    380   1.2     shige 	cmdbuf[1] = (addr & 0xff);
    381   1.4     shige 	cmdbuf[2] = X1226_FLAG_SR_WEL | X1226_FLAG_SR_RWEL;
    382  1.22   thorpej 	if ((error = iic_exec(sc->sc_tag,
    383   1.1     shige 		I2C_OP_WRITE_WITH_STOP,
    384  1.22   thorpej 		sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
    385  1.22   thorpej 		iic_release_bus(sc->sc_tag, 0);
    386  1.12   xtraeme 		aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
    387  1.11    cegger 			"failed to write-unlock status register(RWEL=1)\n");
    388  1.22   thorpej 		return (error);
    389   1.1     shige 	}
    390   1.1     shige 
    391   1.1     shige 	/* Write each RTC register in reverse order */
    392   1.1     shige 	for (i = (X1226_REG_RTC_SIZE - 1) ; i >= 0; i--) {
    393   1.5       scw 		addr = i + X1226_REG_RTC_BASE;
    394   1.2     shige 		cmdbuf[0] = ((addr >> 8) & 0xff);
    395   1.2     shige 		cmdbuf[1] = (addr & 0xff);
    396  1.22   thorpej 		if ((error = iic_exec(sc->sc_tag,
    397   1.1     shige 			I2C_OP_WRITE_WITH_STOP,
    398   1.1     shige 			sc->sc_address, cmdbuf, 2,
    399  1.22   thorpej 			&bcd[i], 1, 0)) != 0) {
    400   1.1     shige 
    401  1.22   thorpej 			aprint_error_dev(sc->sc_dev,
    402  1.22   thorpej 			    "xrtc_clock_write: failed to write rtc at 0x%x\n",
    403  1.22   thorpej 			    i);
    404   1.1     shige 
    405  1.22   thorpej 			goto write_lock_rtc;
    406   1.1     shige 		}
    407   1.1     shige 	}
    408   1.1     shige 
    409  1.22   thorpej  write_lock_rtc:
    410   1.1     shige 	/* Lock register: WEL/RWEL off */
    411   1.1     shige 	addr = X1226_REG_SR;
    412   1.2     shige 	cmdbuf[0] = ((addr >> 8) & 0xff);
    413   1.2     shige 	cmdbuf[1] = (addr & 0xff);
    414   1.2     shige 	cmdbuf[2] = 0;
    415  1.22   thorpej 	if ((error2 = iic_exec(sc->sc_tag,
    416  1.22   thorpej 			I2C_OP_WRITE_WITH_STOP,
    417  1.22   thorpej 			sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
    418  1.22   thorpej 		iic_release_bus(sc->sc_tag, 0);
    419  1.12   xtraeme 		aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
    420  1.11    cegger 			"failed to write-lock status register\n");
    421  1.22   thorpej 		return (error ? error : error2);
    422   1.1     shige 	}
    423   1.1     shige 
    424  1.22   thorpej 	iic_release_bus(sc->sc_tag, 0);
    425  1.22   thorpej 	return (error);
    426   1.1     shige }
    427