Home | History | Annotate | Line # | Download | only in i2c
r2025.c revision 1.10
      1  1.10   thorpej /* $NetBSD: r2025.c,v 1.10 2020/01/02 19:00:34 thorpej Exp $ */
      2   1.1     shige 
      3   1.1     shige /*-
      4   1.1     shige  * Copyright (c) 2006 Shigeyuki Fukushima.
      5   1.1     shige  * All rights reserved.
      6   1.1     shige  *
      7   1.1     shige  * Written by Shigeyuki Fukushima.
      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
     15   1.1     shige  *    copyright notice, this list of conditions and the following
     16   1.1     shige  *    disclaimer in the documentation and/or other materials provided
     17   1.1     shige  *    with the distribution.
     18   1.1     shige  * 3. The name of the author may not be used to endorse or promote
     19   1.1     shige  *    products derived from this software without specific prior
     20   1.1     shige  *    written permission.
     21   1.1     shige  *
     22   1.1     shige  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     23   1.1     shige  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24   1.1     shige  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1     shige  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     26   1.1     shige  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1     shige  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     28   1.1     shige  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29   1.1     shige  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30   1.1     shige  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     31   1.1     shige  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     32   1.1     shige  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33   1.1     shige  */
     34   1.1     shige 
     35   1.1     shige #include <sys/cdefs.h>
     36  1.10   thorpej __KERNEL_RCSID(0, "$NetBSD: r2025.c,v 1.10 2020/01/02 19:00:34 thorpej Exp $");
     37   1.1     shige 
     38   1.1     shige #include <sys/param.h>
     39   1.1     shige #include <sys/systm.h>
     40   1.1     shige #include <sys/device.h>
     41   1.1     shige #include <sys/kernel.h>
     42   1.1     shige #include <sys/fcntl.h>
     43   1.1     shige #include <sys/uio.h>
     44   1.1     shige #include <sys/conf.h>
     45   1.1     shige #include <sys/event.h>
     46   1.1     shige 
     47   1.1     shige #include <dev/clock_subr.h>
     48   1.1     shige 
     49   1.1     shige #include <dev/i2c/i2cvar.h>
     50   1.1     shige #include <dev/i2c/r2025reg.h>
     51   1.1     shige 
     52   1.1     shige struct r2025rtc_softc {
     53   1.5   xtraeme 	device_t		sc_dev;
     54   1.1     shige 	i2c_tag_t		sc_tag;
     55   1.1     shige 	int			sc_address;
     56   1.1     shige 	int			sc_open;
     57   1.1     shige 	struct todr_chip_handle	sc_todr;
     58   1.1     shige };
     59   1.1     shige 
     60   1.5   xtraeme static void	r2025rtc_attach(device_t, device_t, void *);
     61   1.5   xtraeme static int	r2025rtc_match(device_t, cfdata_t, void *);
     62   1.1     shige 
     63   1.5   xtraeme CFATTACH_DECL_NEW(r2025rtc, sizeof(struct r2025rtc_softc),
     64   1.1     shige 	r2025rtc_match, r2025rtc_attach, NULL, NULL);
     65   1.1     shige 
     66  1.10   thorpej static int	r2025rtc_gettime_ymdhms(struct todr_chip_handle *,
     67  1.10   thorpej 					struct clock_ymdhms *);
     68  1.10   thorpej static int	r2025rtc_settime_ymdhms(struct todr_chip_handle *,
     69  1.10   thorpej 					struct clock_ymdhms *);
     70   1.1     shige static int	r2025rtc_reg_write(struct r2025rtc_softc *, int, uint8_t*, int);
     71   1.1     shige static int	r2025rtc_reg_read(struct r2025rtc_softc *, int, uint8_t*, int);
     72   1.1     shige 
     73   1.1     shige 
     74   1.1     shige static int
     75   1.5   xtraeme r2025rtc_match(device_t parent, cfdata_t cf, void *arg)
     76   1.1     shige {
     77   1.1     shige 	struct i2c_attach_args *ia = arg;
     78   1.1     shige 
     79   1.1     shige 	/* match only R2025 RTC devices */
     80   1.1     shige 	if (ia->ia_addr == R2025_ADDR)
     81   1.8   thorpej 		return I2C_MATCH_ADDRESS_ONLY;
     82   1.1     shige 
     83   1.1     shige 	return 0;
     84   1.1     shige }
     85   1.1     shige 
     86   1.1     shige static void
     87   1.5   xtraeme r2025rtc_attach(device_t parent, device_t self, void *arg)
     88   1.1     shige {
     89   1.2   thorpej 	struct r2025rtc_softc *sc = device_private(self);
     90   1.1     shige 	struct i2c_attach_args *ia = arg;
     91   1.1     shige 
     92   1.1     shige 	aprint_normal(": RICOH R2025S/D Real-time Clock\n");
     93   1.1     shige 
     94   1.1     shige 	sc->sc_tag = ia->ia_tag;
     95   1.1     shige 	sc->sc_address = ia->ia_addr;
     96   1.5   xtraeme 	sc->sc_dev = self;
     97   1.1     shige 	sc->sc_open = 0;
     98   1.1     shige 	sc->sc_todr.cookie = sc;
     99  1.10   thorpej 	sc->sc_todr.todr_gettime = NULL;
    100  1.10   thorpej 	sc->sc_todr.todr_settime = NULL;
    101  1.10   thorpej 	sc->sc_todr.todr_gettime_ymdhms = r2025rtc_gettime_ymdhms;
    102  1.10   thorpej 	sc->sc_todr.todr_settime_ymdhms = r2025rtc_settime_ymdhms;
    103   1.1     shige 	sc->sc_todr.todr_setwen = NULL;
    104   1.1     shige 
    105   1.1     shige 	todr_attach(&sc->sc_todr);
    106   1.1     shige }
    107   1.1     shige 
    108   1.1     shige static int
    109  1.10   thorpej r2025rtc_gettime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    110   1.1     shige {
    111   1.1     shige 	struct r2025rtc_softc *sc = ch->cookie;
    112   1.1     shige 	uint8_t rctrl;
    113   1.1     shige 	uint8_t bcd[R2025_CLK_SIZE];
    114   1.1     shige 	int hour;
    115   1.9   thorpej 	int error;
    116   1.1     shige 
    117  1.10   thorpej 	memset(dt, 0, sizeof(*dt));
    118   1.1     shige 
    119   1.9   thorpej 	if ((error = r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) {
    120   1.5   xtraeme 		aprint_error_dev(sc->sc_dev,
    121   1.5   xtraeme 		    "r2025rtc_gettime: failed to read registers.\n");
    122   1.9   thorpej 		return error;
    123   1.1     shige 	}
    124   1.1     shige 
    125   1.9   thorpej 	if ((error = r2025rtc_reg_read(sc, R2025_REG_SEC, &bcd[0],
    126   1.9   thorpej 				       R2025_CLK_SIZE)) != 0) {
    127   1.5   xtraeme 		aprint_error_dev(sc->sc_dev,
    128   1.5   xtraeme 		    "r2025rtc_gettime: failed to read registers.\n");
    129   1.9   thorpej 		return error;
    130   1.1     shige 	}
    131   1.1     shige 
    132  1.10   thorpej 	dt->dt_sec = bcdtobin(bcd[R2025_REG_SEC] & R2025_REG_SEC_MASK);
    133  1.10   thorpej 	dt->dt_min = bcdtobin(bcd[R2025_REG_MIN] & R2025_REG_MIN_MASK);
    134   1.7  christos 	hour = bcdtobin(bcd[R2025_REG_HOUR] & R2025_REG_HOUR_MASK);
    135   1.1     shige 	if (rctrl & R2025_REG_CTRL1_H1224) {
    136  1.10   thorpej 		dt->dt_hour = hour;
    137   1.1     shige 	} else {
    138   1.1     shige 		if (hour == 12) {
    139  1.10   thorpej 			dt->dt_hour = 0;
    140   1.1     shige 		} else if (hour == 32) {
    141  1.10   thorpej 			dt->dt_hour = 12;
    142   1.1     shige 		} else if (hour > 13) {
    143  1.10   thorpej 			dt->dt_hour = (hour - 8);
    144   1.1     shige 		} else { /* (hour < 12) */
    145  1.10   thorpej 			dt->dt_hour = hour;
    146   1.1     shige 		}
    147   1.1     shige 	}
    148  1.10   thorpej 	dt->dt_wday = bcdtobin(bcd[R2025_REG_WDAY] & R2025_REG_WDAY_MASK);
    149  1.10   thorpej 	dt->dt_day = bcdtobin(bcd[R2025_REG_DAY] & R2025_REG_DAY_MASK);
    150  1.10   thorpej 	dt->dt_mon = bcdtobin(bcd[R2025_REG_MON] & R2025_REG_MON_MASK);
    151  1.10   thorpej 	dt->dt_year = bcdtobin(bcd[R2025_REG_YEAR] & R2025_REG_YEAR_MASK)
    152   1.1     shige 		+ ((bcd[R2025_REG_MON] & R2025_REG_MON_Y1920) ? 2000 : 1900);
    153   1.1     shige 
    154   1.1     shige 	return 0;
    155   1.1     shige }
    156   1.1     shige 
    157   1.1     shige static int
    158  1.10   thorpej r2025rtc_settime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    159   1.1     shige {
    160   1.1     shige 	struct r2025rtc_softc *sc = ch->cookie;
    161   1.1     shige 	uint8_t rctrl;
    162   1.1     shige 	uint8_t bcd[R2025_CLK_SIZE];
    163   1.9   thorpej 	int error;
    164   1.1     shige 
    165   1.1     shige 	/* Y3K problem */
    166  1.10   thorpej 	if (dt->dt_year >= 3000) {
    167   1.5   xtraeme 		aprint_error_dev(sc->sc_dev,
    168   1.5   xtraeme 		    "r2025rtc_settime: "
    169   1.5   xtraeme 		    "RTC does not support year 3000 or over.\n");
    170   1.9   thorpej 		return EINVAL;
    171   1.1     shige 	}
    172   1.1     shige 
    173   1.9   thorpej 	if ((error = r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) {
    174   1.5   xtraeme 		aprint_error_dev(sc->sc_dev,
    175   1.5   xtraeme 		    "r2025rtc_settime: failed to read register.\n");
    176   1.9   thorpej 		return error;
    177   1.1     shige 	}
    178   1.1     shige 	rctrl |= R2025_REG_CTRL1_H1224;
    179   1.1     shige 
    180   1.1     shige 	/* setup registers 0x00-0x06 (7 byte) */
    181  1.10   thorpej 	bcd[R2025_REG_SEC] = bintobcd(dt->dt_sec) & R2025_REG_SEC_MASK;
    182  1.10   thorpej 	bcd[R2025_REG_MIN] = bintobcd(dt->dt_min) & R2025_REG_MIN_MASK;
    183  1.10   thorpej 	bcd[R2025_REG_HOUR] = bintobcd(dt->dt_hour) & R2025_REG_HOUR_MASK;
    184  1.10   thorpej 	bcd[R2025_REG_WDAY] = bintobcd(dt->dt_wday) & R2025_REG_WDAY_MASK;
    185  1.10   thorpej 	bcd[R2025_REG_DAY] = bintobcd(dt->dt_day) & R2025_REG_DAY_MASK;
    186  1.10   thorpej 	bcd[R2025_REG_MON] = (bintobcd(dt->dt_mon) & R2025_REG_MON_MASK)
    187  1.10   thorpej 		| ((dt->dt_year >= 2000) ? R2025_REG_MON_Y1920 : 0);
    188  1.10   thorpej 	bcd[R2025_REG_YEAR] = bintobcd(dt->dt_year % 100) & R2025_REG_YEAR_MASK;
    189   1.1     shige 
    190   1.1     shige 	/* Write RTC register */
    191   1.9   thorpej 	if ((error = r2025rtc_reg_write(sc, R2025_REG_CTRL1, &rctrl, 1)) != 0) {
    192   1.5   xtraeme 		aprint_error_dev(sc->sc_dev,
    193   1.5   xtraeme 		    "r2025rtc_settime: failed to write registers.\n");
    194   1.9   thorpej 		return error;
    195   1.1     shige 	}
    196   1.9   thorpej 	if ((error = r2025rtc_reg_write(sc, R2025_REG_SEC, bcd,
    197   1.9   thorpej 					R2025_CLK_SIZE)) != 0) {
    198   1.5   xtraeme 		aprint_error_dev(sc->sc_dev,
    199   1.5   xtraeme 		    "r2025rtc_settime: failed to write registers.\n");
    200   1.9   thorpej 		return error;
    201   1.1     shige 	}
    202   1.1     shige 
    203   1.1     shige 	return 0;
    204   1.1     shige }
    205   1.1     shige 
    206   1.1     shige static int
    207   1.1     shige r2025rtc_reg_write(struct r2025rtc_softc *sc, int reg, uint8_t *val, int len)
    208   1.1     shige {
    209   1.1     shige 	int i;
    210   1.1     shige 	uint8_t buf[1];
    211   1.1     shige 	uint8_t cmdbuf[1];
    212   1.9   thorpej 	int error;
    213   1.1     shige 
    214   1.9   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
    215   1.5   xtraeme 		aprint_error_dev(sc->sc_dev,
    216   1.9   thorpej 		    "r2025rtc_reg_write: failed to acquire I2C bus\n");
    217   1.9   thorpej 		return error;
    218   1.1     shige 	}
    219   1.1     shige 
    220   1.1     shige 	for (i = 0 ; i < len ; i++) {
    221   1.1     shige 		cmdbuf[0] = (((reg + i) << 4) & 0xf0);
    222   1.1     shige 		buf[0] = val[i];
    223   1.9   thorpej 		if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    224   1.9   thorpej 				      sc->sc_address, cmdbuf, 1, buf, 1,
    225   1.9   thorpej 				      0)) != 0) {
    226   1.9   thorpej 			iic_release_bus(sc->sc_tag, 0);
    227   1.5   xtraeme 			aprint_error_dev(sc->sc_dev, "r2025rtc_reg_write: "
    228   1.4    cegger 				"failed to write registers\n");
    229   1.9   thorpej 			return error;
    230   1.1     shige 		}
    231   1.1     shige 	}
    232   1.1     shige 
    233   1.9   thorpej 	iic_release_bus(sc->sc_tag, 0);
    234   1.1     shige 
    235   1.1     shige 	return 0;
    236   1.1     shige }
    237   1.1     shige 
    238   1.1     shige static int
    239   1.1     shige r2025rtc_reg_read(struct r2025rtc_softc *sc, int reg, uint8_t *val, int len)
    240   1.1     shige {
    241   1.1     shige 	int i;
    242   1.1     shige 	uint8_t buf[1];
    243   1.1     shige 	uint8_t cmdbuf[1];
    244   1.9   thorpej 	int error;
    245   1.1     shige 
    246   1.9   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
    247   1.5   xtraeme 		aprint_error_dev(sc->sc_dev,
    248   1.9   thorpej 		    "r2025rtc_reg_read: failed to acquire I2C bus\n");
    249   1.9   thorpej 		return error;
    250   1.1     shige 	}
    251   1.1     shige 
    252   1.1     shige 	for (i = 0 ; i < len ; i++) {
    253   1.1     shige 		cmdbuf[0] = (((reg + i) << 4) & 0xf0);
    254   1.1     shige 		buf[0] = 0;
    255   1.9   thorpej 		if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    256   1.9   thorpej 				      sc->sc_address, cmdbuf, 1, buf, 1,
    257   1.9   thorpej 				      0)) != 0) {
    258   1.9   thorpej 			iic_release_bus(sc->sc_tag, 0);
    259   1.5   xtraeme 			aprint_error_dev(sc->sc_dev, "r2025rtc_reg_read: "
    260   1.9   thorpej 				"failed to read registers\n");
    261   1.9   thorpej 			return error;
    262   1.1     shige 		}
    263   1.1     shige 
    264   1.1     shige 		*(val + i) = buf[0];
    265   1.1     shige 	}
    266   1.1     shige 
    267   1.9   thorpej 	iic_release_bus(sc->sc_tag, 0);
    268   1.1     shige 
    269   1.1     shige 	return 0;
    270   1.1     shige }
    271