Home | History | Annotate | Line # | Download | only in i2c
rs5c372.c revision 1.14
      1  1.14  christos /*	$NetBSD: rs5c372.c,v 1.14 2014/11/20 16:34:26 christos Exp $	*/
      2   1.1    nonaka 
      3  1.12    nonaka /*-
      4  1.12    nonaka  * Copyright (C) 2005 NONAKA Kimihiro <nonaka (at) netbsd.org>
      5   1.1    nonaka  * All rights reserved.
      6   1.1    nonaka  *
      7   1.1    nonaka  * Redistribution and use in source and binary forms, with or without
      8   1.1    nonaka  * modification, are permitted provided that the following conditions
      9   1.1    nonaka  * are met:
     10   1.1    nonaka  * 1. Redistributions of source code must retain the above copyright
     11   1.1    nonaka  *    notice, this list of conditions and the following disclaimer.
     12   1.1    nonaka  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    nonaka  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    nonaka  *    documentation and/or other materials provided with the distribution.
     15   1.1    nonaka  *
     16  1.12    nonaka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.12    nonaka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.12    nonaka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.12    nonaka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.12    nonaka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.12    nonaka  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.12    nonaka  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.12    nonaka  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.12    nonaka  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.12    nonaka  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1    nonaka  */
     27   1.1    nonaka 
     28   1.7     lukem #include <sys/cdefs.h>
     29  1.14  christos __KERNEL_RCSID(0, "$NetBSD: rs5c372.c,v 1.14 2014/11/20 16:34:26 christos Exp $");
     30   1.7     lukem 
     31   1.1    nonaka #include <sys/param.h>
     32   1.1    nonaka #include <sys/systm.h>
     33   1.1    nonaka #include <sys/device.h>
     34   1.1    nonaka #include <sys/kernel.h>
     35   1.1    nonaka #include <sys/fcntl.h>
     36   1.1    nonaka #include <sys/uio.h>
     37   1.1    nonaka #include <sys/conf.h>
     38   1.1    nonaka #include <sys/event.h>
     39   1.1    nonaka 
     40   1.1    nonaka #include <dev/clock_subr.h>
     41   1.1    nonaka 
     42   1.1    nonaka #include <dev/i2c/i2cvar.h>
     43   1.1    nonaka #include <dev/i2c/rs5c372reg.h>
     44   1.1    nonaka 
     45   1.1    nonaka struct rs5c372rtc_softc {
     46   1.9   xtraeme 	device_t sc_dev;
     47   1.1    nonaka 	i2c_tag_t sc_tag;
     48   1.1    nonaka 	int sc_address;
     49   1.1    nonaka 	struct todr_chip_handle sc_todr;
     50   1.1    nonaka };
     51   1.1    nonaka 
     52   1.9   xtraeme static int rs5c372rtc_match(device_t, cfdata_t, void *);
     53   1.9   xtraeme static void rs5c372rtc_attach(device_t, device_t, void *);
     54   1.1    nonaka 
     55   1.9   xtraeme CFATTACH_DECL_NEW(rs5c372rtc, sizeof(struct rs5c372rtc_softc),
     56   1.1    nonaka     rs5c372rtc_match, rs5c372rtc_attach, NULL, NULL);
     57   1.1    nonaka 
     58   1.2    nonaka static void rs5c372rtc_reg_write(struct rs5c372rtc_softc *, int, uint8_t);
     59   1.1    nonaka static int rs5c372rtc_clock_read(struct rs5c372rtc_softc *, struct clock_ymdhms *);
     60   1.1    nonaka static int rs5c372rtc_clock_write(struct rs5c372rtc_softc *, struct clock_ymdhms *);
     61  1.13   tsutsui static int rs5c372rtc_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     62  1.13   tsutsui static int rs5c372rtc_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     63   1.1    nonaka 
     64   1.1    nonaka static int
     65   1.9   xtraeme rs5c372rtc_match(device_t parent, cfdata_t cf, void *arg)
     66   1.1    nonaka {
     67   1.1    nonaka 	struct i2c_attach_args *ia = arg;
     68   1.1    nonaka 
     69  1.11       phx 	if (ia->ia_name) {
     70  1.11       phx 		/* direct config - check name */
     71  1.11       phx 		if (strcmp(ia->ia_name, "rs5c372rtc") == 0)
     72  1.11       phx 			return 1;
     73  1.11       phx 	} else {
     74  1.11       phx 		/* indirect config - check typical address */
     75  1.11       phx 		if (ia->ia_addr == RS5C372_ADDR)
     76  1.11       phx 			return 1;
     77  1.11       phx 	}
     78  1.11       phx 	return 0;
     79   1.1    nonaka }
     80   1.1    nonaka 
     81   1.1    nonaka static void
     82   1.9   xtraeme rs5c372rtc_attach(device_t parent, device_t self, void *arg)
     83   1.1    nonaka {
     84   1.5   thorpej 	struct rs5c372rtc_softc *sc = device_private(self);
     85   1.1    nonaka 	struct i2c_attach_args *ia = arg;
     86   1.1    nonaka 
     87   1.1    nonaka 	aprint_naive(": Real-time Clock\n");
     88   1.1    nonaka 	aprint_normal(": RICOH RS5C372[AB] Real-time Clock\n");
     89   1.1    nonaka 
     90   1.1    nonaka 	sc->sc_tag = ia->ia_tag;
     91   1.1    nonaka 	sc->sc_address = ia->ia_addr;
     92   1.9   xtraeme 	sc->sc_dev = self;
     93   1.1    nonaka 	sc->sc_todr.cookie = sc;
     94  1.13   tsutsui 	sc->sc_todr.todr_gettime_ymdhms = rs5c372rtc_gettime_ymdhms;
     95  1.13   tsutsui 	sc->sc_todr.todr_settime_ymdhms = rs5c372rtc_settime_ymdhms;
     96   1.1    nonaka 	sc->sc_todr.todr_setwen = NULL;
     97   1.1    nonaka 
     98   1.1    nonaka 	todr_attach(&sc->sc_todr);
     99   1.1    nonaka 
    100   1.2    nonaka 	/* Initialize RTC */
    101   1.2    nonaka 	rs5c372rtc_reg_write(sc, RS5C372_CONTROL2, RS5C372_CONTROL2_24HRS);
    102   1.2    nonaka 	rs5c372rtc_reg_write(sc, RS5C372_CONTROL1, 0);
    103   1.1    nonaka }
    104   1.1    nonaka 
    105   1.1    nonaka static int
    106  1.13   tsutsui rs5c372rtc_gettime_ymdhms(todr_chip_handle_t ch, struct clock_ymdhms *dt)
    107   1.1    nonaka {
    108   1.1    nonaka 	struct rs5c372rtc_softc *sc = ch->cookie;
    109   1.1    nonaka 
    110  1.13   tsutsui 	if (rs5c372rtc_clock_read(sc, dt) == 0)
    111   1.3    nonaka 		return (-1);
    112   1.1    nonaka 
    113   1.1    nonaka 	return (0);
    114   1.1    nonaka }
    115   1.1    nonaka 
    116   1.1    nonaka static int
    117  1.13   tsutsui rs5c372rtc_settime_ymdhms(todr_chip_handle_t ch, struct clock_ymdhms *dt)
    118   1.1    nonaka {
    119   1.1    nonaka 	struct rs5c372rtc_softc *sc = ch->cookie;
    120   1.1    nonaka 
    121  1.13   tsutsui 	if (rs5c372rtc_clock_write(sc, dt) == 0)
    122   1.1    nonaka 		return (-1);
    123   1.1    nonaka 
    124   1.1    nonaka 	return (0);
    125   1.1    nonaka }
    126   1.1    nonaka 
    127   1.1    nonaka static void
    128   1.2    nonaka rs5c372rtc_reg_write(struct rs5c372rtc_softc *sc, int reg, uint8_t val)
    129   1.1    nonaka {
    130   1.1    nonaka 	uint8_t cmdbuf[2];
    131   1.1    nonaka 
    132   1.1    nonaka 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    133   1.9   xtraeme 		aprint_error_dev(sc->sc_dev,
    134   1.9   xtraeme 		    "rs5c372rtc_reg_write: failed to acquire I2C bus\n");
    135   1.1    nonaka 		return;
    136   1.1    nonaka 	}
    137   1.1    nonaka 
    138   1.2    nonaka 	reg &= 0xf;
    139   1.2    nonaka 	cmdbuf[0] = (reg << 4);
    140   1.2    nonaka 	cmdbuf[1] = val;
    141   1.1    nonaka 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
    142   1.1    nonaka 	             cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) {
    143   1.1    nonaka 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    144   1.9   xtraeme 		aprint_error_dev(sc->sc_dev,
    145   1.9   xtraeme 		    "rs5c372rtc_reg_write: failed to write reg%d\n", reg);
    146   1.1    nonaka 		return;
    147   1.1    nonaka 	}
    148   1.1    nonaka 
    149   1.1    nonaka 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    150   1.1    nonaka }
    151   1.1    nonaka 
    152   1.1    nonaka static int
    153   1.1    nonaka rs5c372rtc_clock_read(struct rs5c372rtc_softc *sc, struct clock_ymdhms *dt)
    154   1.1    nonaka {
    155   1.1    nonaka 	uint8_t bcd[RS5C372_NRTC_REGS];
    156   1.1    nonaka 	uint8_t cmdbuf[1];
    157   1.1    nonaka 
    158   1.1    nonaka 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    159   1.9   xtraeme 		aprint_error_dev(sc->sc_dev,
    160   1.9   xtraeme 		    "rs5c372rtc_clock_read: failed to acquire I2C bus\n");
    161   1.1    nonaka 		return (0);
    162   1.1    nonaka 	}
    163   1.1    nonaka 
    164   1.1    nonaka 	cmdbuf[0] = (RS5C372_SECONDS << 4);
    165   1.3    nonaka 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
    166   1.1    nonaka 	             cmdbuf, 1, bcd, RS5C372_NRTC_REGS, I2C_F_POLL)) {
    167   1.1    nonaka 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    168   1.9   xtraeme 		aprint_error_dev(sc->sc_dev,
    169   1.9   xtraeme 		    "rs5c372rtc_clock_read: failed to read rtc\n");
    170   1.1    nonaka 		return (0);
    171   1.1    nonaka 	}
    172   1.1    nonaka 
    173   1.1    nonaka 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    174   1.1    nonaka 
    175   1.1    nonaka 	/*
    176   1.1    nonaka 	 * Convert the RS5C372's register values into something useable
    177   1.1    nonaka 	 */
    178  1.14  christos 	dt->dt_sec = bcdtobin(bcd[RS5C372_SECONDS] & RS5C372_SECONDS_MASK);
    179  1.14  christos 	dt->dt_min = bcdtobin(bcd[RS5C372_MINUTES] & RS5C372_MINUTES_MASK);
    180  1.14  christos 	dt->dt_hour = bcdtobin(bcd[RS5C372_HOURS] & RS5C372_HOURS_24MASK);
    181  1.14  christos 	dt->dt_day = bcdtobin(bcd[RS5C372_DATE] & RS5C372_DATE_MASK);
    182  1.14  christos 	dt->dt_mon = bcdtobin(bcd[RS5C372_MONTH] & RS5C372_MONTH_MASK);
    183  1.14  christos 	dt->dt_year = bcdtobin(bcd[RS5C372_YEAR]) + 2000;
    184   1.1    nonaka 
    185   1.1    nonaka 	return (1);
    186   1.1    nonaka }
    187   1.1    nonaka 
    188   1.1    nonaka static int
    189   1.1    nonaka rs5c372rtc_clock_write(struct rs5c372rtc_softc *sc, struct clock_ymdhms *dt)
    190   1.1    nonaka {
    191   1.1    nonaka 	uint8_t bcd[RS5C372_NRTC_REGS];
    192   1.1    nonaka 	uint8_t cmdbuf[1];
    193   1.1    nonaka 
    194   1.1    nonaka 	/*
    195   1.1    nonaka 	 * Convert our time representation into something the RS5C372
    196   1.1    nonaka 	 * can understand.
    197   1.1    nonaka 	 */
    198  1.14  christos 	bcd[RS5C372_SECONDS] = bintobcd(dt->dt_sec);
    199  1.14  christos 	bcd[RS5C372_MINUTES] = bintobcd(dt->dt_min);
    200  1.14  christos 	bcd[RS5C372_HOURS] = bintobcd(dt->dt_hour);
    201  1.14  christos 	bcd[RS5C372_DATE] = bintobcd(dt->dt_day);
    202  1.14  christos 	bcd[RS5C372_DAY] = bintobcd(dt->dt_wday);
    203  1.14  christos 	bcd[RS5C372_MONTH] = bintobcd(dt->dt_mon);
    204  1.14  christos 	bcd[RS5C372_YEAR] = bintobcd(dt->dt_year % 100);
    205   1.1    nonaka 
    206   1.1    nonaka 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    207   1.9   xtraeme 		aprint_error_dev(sc->sc_dev, "rs5c372rtc_clock_write: failed to "
    208   1.8    cegger 		    "acquire I2C bus\n");
    209   1.1    nonaka 		return (0);
    210   1.1    nonaka 	}
    211   1.1    nonaka 
    212   1.1    nonaka 	cmdbuf[0] = (RS5C372_SECONDS << 4);
    213   1.3    nonaka 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
    214   1.1    nonaka 	             cmdbuf, 1, bcd, RS5C372_NRTC_REGS, I2C_F_POLL)) {
    215   1.1    nonaka 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    216   1.9   xtraeme 		aprint_error_dev(sc->sc_dev,
    217   1.9   xtraeme 		    "rs5c372rtc_clock_write: failed to write rtc\n");
    218   1.1    nonaka 		return (0);
    219   1.1    nonaka 	}
    220   1.1    nonaka 
    221   1.1    nonaka 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    222   1.1    nonaka 
    223   1.1    nonaka 	return (1);
    224   1.1    nonaka }
    225