Home | History | Annotate | Line # | Download | only in i2c
rs5c372.c revision 1.16
      1  1.16   thorpej /*	$NetBSD: rs5c372.c,v 1.16 2020/01/02 17:17:36 thorpej 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.16   thorpej __KERNEL_RCSID(0, "$NetBSD: rs5c372.c,v 1.16 2020/01/02 17:17:36 thorpej 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.16   thorpej static int 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.15   thorpej 	int match_result;
     69  1.15   thorpej 
     70  1.15   thorpej 	if (iic_use_direct_match(ia, cf, NULL, &match_result))
     71  1.15   thorpej 		return match_result;
     72  1.15   thorpej 
     73  1.15   thorpej 	/* indirect config - check typical address */
     74  1.15   thorpej 	if (ia->ia_addr == RS5C372_ADDR)
     75  1.15   thorpej 		return I2C_MATCH_ADDRESS_ONLY;
     76   1.1    nonaka 
     77  1.11       phx 	return 0;
     78   1.1    nonaka }
     79   1.1    nonaka 
     80   1.1    nonaka static void
     81   1.9   xtraeme rs5c372rtc_attach(device_t parent, device_t self, void *arg)
     82   1.1    nonaka {
     83   1.5   thorpej 	struct rs5c372rtc_softc *sc = device_private(self);
     84   1.1    nonaka 	struct i2c_attach_args *ia = arg;
     85   1.1    nonaka 
     86   1.1    nonaka 	aprint_naive(": Real-time Clock\n");
     87   1.1    nonaka 	aprint_normal(": RICOH RS5C372[AB] Real-time Clock\n");
     88   1.1    nonaka 
     89   1.1    nonaka 	sc->sc_tag = ia->ia_tag;
     90   1.1    nonaka 	sc->sc_address = ia->ia_addr;
     91   1.9   xtraeme 	sc->sc_dev = self;
     92   1.1    nonaka 	sc->sc_todr.cookie = sc;
     93  1.13   tsutsui 	sc->sc_todr.todr_gettime_ymdhms = rs5c372rtc_gettime_ymdhms;
     94  1.13   tsutsui 	sc->sc_todr.todr_settime_ymdhms = rs5c372rtc_settime_ymdhms;
     95   1.1    nonaka 	sc->sc_todr.todr_setwen = NULL;
     96   1.1    nonaka 
     97   1.1    nonaka 	todr_attach(&sc->sc_todr);
     98   1.1    nonaka 
     99   1.2    nonaka 	/* Initialize RTC */
    100   1.2    nonaka 	rs5c372rtc_reg_write(sc, RS5C372_CONTROL2, RS5C372_CONTROL2_24HRS);
    101   1.2    nonaka 	rs5c372rtc_reg_write(sc, RS5C372_CONTROL1, 0);
    102   1.1    nonaka }
    103   1.1    nonaka 
    104   1.1    nonaka static int
    105  1.13   tsutsui rs5c372rtc_gettime_ymdhms(todr_chip_handle_t ch, struct clock_ymdhms *dt)
    106   1.1    nonaka {
    107   1.1    nonaka 	struct rs5c372rtc_softc *sc = ch->cookie;
    108   1.1    nonaka 
    109  1.16   thorpej 	return rs5c372rtc_clock_read(sc, dt);
    110   1.1    nonaka }
    111   1.1    nonaka 
    112   1.1    nonaka static int
    113  1.13   tsutsui rs5c372rtc_settime_ymdhms(todr_chip_handle_t ch, struct clock_ymdhms *dt)
    114   1.1    nonaka {
    115   1.1    nonaka 	struct rs5c372rtc_softc *sc = ch->cookie;
    116   1.1    nonaka 
    117  1.16   thorpej 	return rs5c372rtc_clock_write(sc, dt);
    118   1.1    nonaka }
    119   1.1    nonaka 
    120  1.16   thorpej static int
    121   1.2    nonaka rs5c372rtc_reg_write(struct rs5c372rtc_softc *sc, int reg, uint8_t val)
    122   1.1    nonaka {
    123   1.1    nonaka 	uint8_t cmdbuf[2];
    124  1.16   thorpej 	int error;
    125   1.1    nonaka 
    126  1.16   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
    127   1.9   xtraeme 		aprint_error_dev(sc->sc_dev,
    128   1.9   xtraeme 		    "rs5c372rtc_reg_write: failed to acquire I2C bus\n");
    129  1.16   thorpej 		return error;
    130   1.1    nonaka 	}
    131   1.1    nonaka 
    132   1.2    nonaka 	reg &= 0xf;
    133   1.2    nonaka 	cmdbuf[0] = (reg << 4);
    134   1.2    nonaka 	cmdbuf[1] = val;
    135  1.16   thorpej 	if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    136  1.16   thorpej 			      sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1,
    137  1.16   thorpej 			      0)) != 0) {
    138  1.16   thorpej 		iic_release_bus(sc->sc_tag, 0);
    139   1.9   xtraeme 		aprint_error_dev(sc->sc_dev,
    140   1.9   xtraeme 		    "rs5c372rtc_reg_write: failed to write reg%d\n", reg);
    141  1.16   thorpej 		return error;
    142   1.1    nonaka 	}
    143   1.1    nonaka 
    144  1.16   thorpej 	iic_release_bus(sc->sc_tag, 0);
    145  1.16   thorpej 
    146  1.16   thorpej 	return 0;
    147   1.1    nonaka }
    148   1.1    nonaka 
    149   1.1    nonaka static int
    150   1.1    nonaka rs5c372rtc_clock_read(struct rs5c372rtc_softc *sc, struct clock_ymdhms *dt)
    151   1.1    nonaka {
    152   1.1    nonaka 	uint8_t bcd[RS5C372_NRTC_REGS];
    153   1.1    nonaka 	uint8_t cmdbuf[1];
    154  1.16   thorpej 	int error;
    155   1.1    nonaka 
    156  1.16   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
    157   1.9   xtraeme 		aprint_error_dev(sc->sc_dev,
    158   1.9   xtraeme 		    "rs5c372rtc_clock_read: failed to acquire I2C bus\n");
    159  1.16   thorpej 		return (error);
    160   1.1    nonaka 	}
    161   1.1    nonaka 
    162   1.1    nonaka 	cmdbuf[0] = (RS5C372_SECONDS << 4);
    163  1.16   thorpej 	if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
    164  1.16   thorpej 	             cmdbuf, 1, bcd, RS5C372_NRTC_REGS, 0)) != 0) {
    165  1.16   thorpej 		iic_release_bus(sc->sc_tag, 0);
    166   1.9   xtraeme 		aprint_error_dev(sc->sc_dev,
    167   1.9   xtraeme 		    "rs5c372rtc_clock_read: failed to read rtc\n");
    168  1.16   thorpej 		return (error);
    169   1.1    nonaka 	}
    170   1.1    nonaka 
    171  1.16   thorpej 	iic_release_bus(sc->sc_tag, 0);
    172   1.1    nonaka 
    173   1.1    nonaka 	/*
    174   1.1    nonaka 	 * Convert the RS5C372's register values into something useable
    175   1.1    nonaka 	 */
    176  1.14  christos 	dt->dt_sec = bcdtobin(bcd[RS5C372_SECONDS] & RS5C372_SECONDS_MASK);
    177  1.14  christos 	dt->dt_min = bcdtobin(bcd[RS5C372_MINUTES] & RS5C372_MINUTES_MASK);
    178  1.14  christos 	dt->dt_hour = bcdtobin(bcd[RS5C372_HOURS] & RS5C372_HOURS_24MASK);
    179  1.14  christos 	dt->dt_day = bcdtobin(bcd[RS5C372_DATE] & RS5C372_DATE_MASK);
    180  1.14  christos 	dt->dt_mon = bcdtobin(bcd[RS5C372_MONTH] & RS5C372_MONTH_MASK);
    181  1.14  christos 	dt->dt_year = bcdtobin(bcd[RS5C372_YEAR]) + 2000;
    182   1.1    nonaka 
    183  1.16   thorpej 	return (0);
    184   1.1    nonaka }
    185   1.1    nonaka 
    186   1.1    nonaka static int
    187   1.1    nonaka rs5c372rtc_clock_write(struct rs5c372rtc_softc *sc, struct clock_ymdhms *dt)
    188   1.1    nonaka {
    189   1.1    nonaka 	uint8_t bcd[RS5C372_NRTC_REGS];
    190   1.1    nonaka 	uint8_t cmdbuf[1];
    191  1.16   thorpej 	int error;
    192   1.1    nonaka 
    193   1.1    nonaka 	/*
    194   1.1    nonaka 	 * Convert our time representation into something the RS5C372
    195   1.1    nonaka 	 * can understand.
    196   1.1    nonaka 	 */
    197  1.14  christos 	bcd[RS5C372_SECONDS] = bintobcd(dt->dt_sec);
    198  1.14  christos 	bcd[RS5C372_MINUTES] = bintobcd(dt->dt_min);
    199  1.14  christos 	bcd[RS5C372_HOURS] = bintobcd(dt->dt_hour);
    200  1.14  christos 	bcd[RS5C372_DATE] = bintobcd(dt->dt_day);
    201  1.14  christos 	bcd[RS5C372_DAY] = bintobcd(dt->dt_wday);
    202  1.14  christos 	bcd[RS5C372_MONTH] = bintobcd(dt->dt_mon);
    203  1.14  christos 	bcd[RS5C372_YEAR] = bintobcd(dt->dt_year % 100);
    204   1.1    nonaka 
    205  1.16   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
    206   1.9   xtraeme 		aprint_error_dev(sc->sc_dev, "rs5c372rtc_clock_write: failed to "
    207   1.8    cegger 		    "acquire I2C bus\n");
    208  1.16   thorpej 		return (error);
    209   1.1    nonaka 	}
    210   1.1    nonaka 
    211   1.1    nonaka 	cmdbuf[0] = (RS5C372_SECONDS << 4);
    212  1.16   thorpej 	if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    213  1.16   thorpej 			      sc->sc_address, cmdbuf, 1, bcd,
    214  1.16   thorpej 			      RS5C372_NRTC_REGS, 0)) != 0) {
    215  1.16   thorpej 		iic_release_bus(sc->sc_tag, 0);
    216   1.9   xtraeme 		aprint_error_dev(sc->sc_dev,
    217   1.9   xtraeme 		    "rs5c372rtc_clock_write: failed to write rtc\n");
    218  1.16   thorpej 		return (error);
    219   1.1    nonaka 	}
    220   1.1    nonaka 
    221  1.16   thorpej 	iic_release_bus(sc->sc_tag, 0);
    222   1.1    nonaka 
    223  1.16   thorpej 	return (0);
    224   1.1    nonaka }
    225