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