Home | History | Annotate | Line # | Download | only in i2c
r2025.c revision 1.1
      1 /* $NetBSD: r2025.c,v 1.1 2006/03/06 19:55:08 shige 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.1 2006/03/06 19:55:08 shige 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 	struct device		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(struct device *, struct device *, void *);
     61 static int	r2025rtc_match(struct device *, struct cfdata *, void *);
     62 
     63 CFATTACH_DECL(r2025rtc, sizeof(struct r2025rtc_softc),
     64 	r2025rtc_match, r2025rtc_attach, NULL, NULL);
     65 
     66 static int	r2025rtc_gettime(struct todr_chip_handle *,
     67 				volatile struct timeval *);
     68 static int	r2025rtc_settime(struct todr_chip_handle *,
     69 				volatile struct timeval *);
     70 static int	r2025rtc_getcal(struct todr_chip_handle *, int *);
     71 static int	r2025rtc_setcal(struct todr_chip_handle *, int);
     72 
     73 static int	r2025rtc_reg_write(struct r2025rtc_softc *, int, uint8_t*, int);
     74 static int	r2025rtc_reg_read(struct r2025rtc_softc *, int, uint8_t*, int);
     75 
     76 
     77 static int
     78 r2025rtc_match(struct device *parent, struct cfdata *cf, void *arg)
     79 {
     80 	struct i2c_attach_args *ia = arg;
     81 
     82 	/* match only R2025 RTC devices */
     83 	if (ia->ia_addr == R2025_ADDR)
     84 		return 1;
     85 
     86 	return 0;
     87 }
     88 
     89 static void
     90 r2025rtc_attach(struct device *parent, struct device *self, void *arg)
     91 {
     92 	struct r2025rtc_softc *sc = (struct r2025rtc_softc *)self;
     93 	struct i2c_attach_args *ia = arg;
     94 
     95 	aprint_normal(": RICOH R2025S/D Real-time Clock\n");
     96 
     97 	sc->sc_tag = ia->ia_tag;
     98 	sc->sc_address = ia->ia_addr;
     99 	sc->sc_open = 0;
    100 	sc->sc_todr.cookie = sc;
    101 	sc->sc_todr.todr_gettime = r2025rtc_gettime;
    102 	sc->sc_todr.todr_settime = r2025rtc_settime;
    103 	sc->sc_todr.todr_getcal = r2025rtc_getcal;
    104 	sc->sc_todr.todr_setcal = r2025rtc_setcal;
    105 	sc->sc_todr.todr_setwen = NULL;
    106 
    107 	todr_attach(&sc->sc_todr);
    108 }
    109 
    110 static int
    111 r2025rtc_gettime(struct todr_chip_handle *ch, volatile struct timeval *tv)
    112 {
    113 	struct r2025rtc_softc *sc = ch->cookie;
    114 	struct clock_ymdhms dt;
    115 	uint8_t rctrl;
    116 	uint8_t bcd[R2025_CLK_SIZE];
    117 	int hour;
    118 
    119 	memset(&dt, 0, sizeof(dt));
    120 
    121 	if (r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1) != 0) {
    122 		printf("%s: r2025rtc_gettime: failed to read registers.\n",
    123 			sc->sc_dev.dv_xname);
    124 		return -1;
    125 	}
    126 
    127 	if (r2025rtc_reg_read(sc, R2025_REG_SEC, &bcd[0], R2025_CLK_SIZE)
    128 		!= 0) {
    129 		printf("%s: r2025rtc_gettime: failed to read registers.\n",
    130 			sc->sc_dev.dv_xname);
    131 		return -1;
    132 	}
    133 
    134 	dt.dt_sec = FROMBCD(bcd[R2025_REG_SEC] & R2025_REG_SEC_MASK);
    135 	dt.dt_min = FROMBCD(bcd[R2025_REG_MIN] & R2025_REG_MIN_MASK);
    136 	hour = FROMBCD(bcd[R2025_REG_HOUR] & R2025_REG_HOUR_MASK);
    137 	if (rctrl & R2025_REG_CTRL1_H1224) {
    138 		dt.dt_hour = hour;
    139 	} else {
    140 		if (hour == 12) {
    141 			dt.dt_hour = 0;
    142 		} else if (hour == 32) {
    143 			dt.dt_hour = 12;
    144 		} else if (hour > 13) {
    145 			dt.dt_hour = (hour - 8);
    146 		} else { /* (hour < 12) */
    147 			dt.dt_hour = hour;
    148 		}
    149 	}
    150 	dt.dt_wday = FROMBCD(bcd[R2025_REG_WDAY] & R2025_REG_WDAY_MASK);
    151 	dt.dt_day = FROMBCD(bcd[R2025_REG_DAY] & R2025_REG_DAY_MASK);
    152 	dt.dt_mon = FROMBCD(bcd[R2025_REG_MON] & R2025_REG_MON_MASK);
    153 	dt.dt_year = FROMBCD(bcd[R2025_REG_YEAR] & R2025_REG_YEAR_MASK)
    154 		+ ((bcd[R2025_REG_MON] & R2025_REG_MON_Y1920) ? 2000 : 1900);
    155 
    156 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
    157 	tv->tv_usec = 0;
    158 
    159 	return 0;
    160 }
    161 
    162 static int
    163 r2025rtc_settime(struct todr_chip_handle *ch, volatile struct timeval *tv)
    164 {
    165 	struct r2025rtc_softc *sc = ch->cookie;
    166 	struct clock_ymdhms dt;
    167 	uint8_t rctrl;
    168 	uint8_t bcd[R2025_CLK_SIZE];
    169 
    170 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
    171 
    172 	/* Y3K problem */
    173 	if (dt.dt_year >= 3000) {
    174 		printf("%s: r2025rtc_settime: "
    175 			"RTC does not support year 3000 or over.\n",
    176 			sc->sc_dev.dv_xname);
    177 		return -1;
    178 	}
    179 
    180 	if (r2025rtc_reg_read(sc, R2025_REG_CTRL1, &rctrl, 1) != 0) {
    181 		printf("%s: r2025rtc_settime: failed to read register.\n",
    182 			sc->sc_dev.dv_xname);
    183 		return -1;
    184 	}
    185 	rctrl |= R2025_REG_CTRL1_H1224;
    186 
    187 	/* setup registers 0x00-0x06 (7 byte) */
    188 	bcd[R2025_REG_SEC] = TOBCD(dt.dt_sec) & R2025_REG_SEC_MASK;
    189 	bcd[R2025_REG_MIN] = TOBCD(dt.dt_min) & R2025_REG_MIN_MASK;
    190 	bcd[R2025_REG_HOUR] = TOBCD(dt.dt_hour) & R2025_REG_HOUR_MASK;
    191 	bcd[R2025_REG_WDAY] = TOBCD(dt.dt_wday) & R2025_REG_WDAY_MASK;
    192 	bcd[R2025_REG_DAY] = TOBCD(dt.dt_day) & R2025_REG_DAY_MASK;
    193 	bcd[R2025_REG_MON] = (TOBCD(dt.dt_mon) & R2025_REG_MON_MASK)
    194 		| ((dt.dt_year >= 2000) ? R2025_REG_MON_Y1920 : 0);
    195 	bcd[R2025_REG_YEAR] = TOBCD(dt.dt_year % 100) & R2025_REG_YEAR_MASK;
    196 
    197 	/* Write RTC register */
    198 	if (r2025rtc_reg_write(sc, R2025_REG_CTRL1, &rctrl, 1) != 0) {
    199 		printf("%s: r2025rtc_settime: failed to write registers.\n",
    200 			sc->sc_dev.dv_xname);
    201 		return -1;
    202 	}
    203 	if (r2025rtc_reg_write(sc, R2025_REG_SEC, bcd, R2025_CLK_SIZE) != 0) {
    204 		printf("%s: r2025rtc_settime: failed to write registers.\n",
    205 			sc->sc_dev.dv_xname);
    206 		return -1;
    207 	}
    208 
    209 	return 0;
    210 }
    211 
    212 static int
    213 r2025rtc_setcal(struct todr_chip_handle *ch, int cal)
    214 {
    215 	return EOPNOTSUPP;
    216 }
    217 
    218 static int
    219 r2025rtc_getcal(struct todr_chip_handle *ch, int *cal)
    220 {
    221 	return EOPNOTSUPP;
    222 }
    223 
    224 static int
    225 r2025rtc_reg_write(struct r2025rtc_softc *sc, int reg, uint8_t *val, int len)
    226 {
    227 	int i;
    228 	uint8_t buf[1];
    229 	uint8_t cmdbuf[1];
    230 
    231 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    232 		printf("%s: r2025rtc_clock_write: failed to acquire I2C bus\n",
    233 			sc->sc_dev.dv_xname);
    234 		return -1;
    235 	}
    236 
    237 	for (i = 0 ; i < len ; i++) {
    238 		cmdbuf[0] = (((reg + i) << 4) & 0xf0);
    239 		buf[0] = val[i];
    240 		if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
    241 				cmdbuf, 1, buf, 1, I2C_F_POLL)) {
    242 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    243 			printf("%s: r2025rtc_reg_write: "
    244 				"failed to write registers\n",
    245 				sc->sc_dev.dv_xname);
    246 			return -1;
    247 		}
    248 	}
    249 
    250 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    251 
    252 	return 0;
    253 }
    254 
    255 static int
    256 r2025rtc_reg_read(struct r2025rtc_softc *sc, int reg, uint8_t *val, int len)
    257 {
    258 	int i;
    259 	uint8_t buf[1];
    260 	uint8_t cmdbuf[1];
    261 
    262 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    263 		printf("%s: r2025rtc_clock_read: failed to acquire I2C bus\n",
    264 			sc->sc_dev.dv_xname);
    265 		return -1;
    266 	}
    267 
    268 	for (i = 0 ; i < len ; i++) {
    269 		cmdbuf[0] = (((reg + i) << 4) & 0xf0);
    270 		buf[0] = 0;
    271 		if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
    272 				cmdbuf, 1, buf, 1, I2C_F_POLL)) {
    273 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    274 			printf("%s: r2025rtc_reg_read: "
    275 				"failed to write registers\n",
    276 				sc->sc_dev.dv_xname);
    277 			return -1;
    278 		}
    279 
    280 		*(val + i) = buf[0];
    281 	}
    282 
    283 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    284 
    285 	return 0;
    286 }
    287