Date: Fri, 13 Apr 2012 23:07:32 +0000 (UTC) From: Marius Strobl <marius@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r234248 - in head/sys: conf dev/iicbus Message-ID: <201204132307.q3DN7WNM001811@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: marius Date: Fri Apr 13 23:07:32 2012 New Revision: 234248 URL: http://svn.freebsd.org/changeset/base/234248 Log: Add a driver for the NXP (Philips) PCF8563 RTC. Obtained from: NetBSD (pcf8563reg.h) Added: head/sys/dev/iicbus/pcf8563.c (contents, props changed) head/sys/dev/iicbus/pcf8563reg.h (contents, props changed) Modified: head/sys/conf/files Modified: head/sys/conf/files ============================================================================== --- head/sys/conf/files Fri Apr 13 22:58:23 2012 (r234247) +++ head/sys/conf/files Fri Apr 13 23:07:32 2012 (r234248) @@ -1288,6 +1288,7 @@ dev/iicbus/iiconf.c optional iicbus dev/iicbus/iicsmb.c optional iicsmb \ dependency "iicbus_if.h" dev/iicbus/iicoc.c optional iicoc +dev/iicbus/pcf8563.c optional pcf8563 dev/iir/iir.c optional iir dev/iir/iir_ctrl.c optional iir dev/iir/iir_pci.c optional iir pci Added: head/sys/dev/iicbus/pcf8563.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/iicbus/pcf8563.c Fri Apr 13 23:07:32 2012 (r234248) @@ -0,0 +1,202 @@ +/*- + * Copyright (c) 2012 Marius Strobl <marius@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +/* + * Driver for NXP PCF8563 real-time clock/calendar + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/bus.h> +#include <sys/clock.h> +#include <sys/kernel.h> +#include <sys/module.h> + +#include <dev/iicbus/iicbus.h> +#include <dev/iicbus/iiconf.h> +#include <dev/iicbus/pcf8563reg.h> + +#include "clock_if.h" +#include "iicbus_if.h" + +#define PCF8563_NCLOCKREGS (PCF8563_R_YEAR - PCF8563_R_CS1 + 1) + +struct pcf8563_softc { + uint32_t sc_flags; +#define PCF8563_CPOL (1 << 0) /* PCF8563_R_MONTH_C means 19xx */ + uint16_t sc_addr; /* PCF8563 slave address */ + uint16_t sc_year0; /* TOD clock year 0 */ +}; + +static device_attach_t pcf8563_attach; +static device_probe_t pcf8563_probe; +static clock_gettime_t pcf8563_gettime; +static clock_settime_t pcf8563_settime; + +static int +pcf8563_probe(device_t dev) +{ + + device_set_desc(dev, "NXP PCF8563 RTC"); + return (BUS_PROBE_NOWILDCARD); +} + +static int +pcf8563_attach(device_t dev) +{ + uint8_t reg = PCF8563_R_SECOND, val; + struct iic_msg msgs[] = { + { 0, IIC_M_WR, sizeof(reg), ® }, + { 0, IIC_M_RD, sizeof(val), &val } + }; + struct pcf8563_softc *sc; + int error; + + sc = device_get_softc(dev); + sc->sc_addr = iicbus_get_addr(dev); + if (sc->sc_addr == 0) + sc->sc_addr = PCF8563_ADDR; + + msgs[0].slave = msgs[1].slave = sc->sc_addr; + error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) / + sizeof(*msgs)); + if (error != 0) { + device_printf(dev, "%s: cannot read RTC\n", __func__); + return (error); + } + if ((val & PCF8563_R_SECOND_VL) != 0) { + device_printf(dev, "%s: battery low\n", __func__); + return (ENXIO); + } + + sc->sc_year0 = 1900; + clock_register(dev, 1000000); /* 1 second resolution */ + return (0); +} + +static int +pcf8563_gettime(device_t dev, struct timespec *ts) +{ + struct clocktime ct; + uint8_t reg = PCF8563_R_SECOND, val[PCF8563_NCLOCKREGS]; + struct iic_msg msgs[] = { + { 0, IIC_M_WR, sizeof(reg), ® }, + { 0, IIC_M_RD, PCF8563_NCLOCKREGS, &val[PCF8563_R_SECOND] } + }; + struct pcf8563_softc *sc; + int error; + + sc = device_get_softc(dev); + msgs[0].slave = msgs[1].slave = sc->sc_addr; + error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) / + sizeof(*msgs)); + if (error != 0) { + device_printf(dev, "%s: cannot read RTC\n", __func__); + return (error); + } + + ct.nsec = 0; + ct.sec = FROMBCD(val[PCF8563_R_SECOND] & PCF8563_M_SECOND); + ct.min = FROMBCD(val[PCF8563_R_MINUTE] & PCF8563_M_MINUTE); + ct.hour = FROMBCD(val[PCF8563_R_HOUR] & PCF8563_M_HOUR); + ct.day = FROMBCD(val[PCF8563_R_DAY] & PCF8563_M_DAY); + ct.dow = val[PCF8563_R_WEEKDAY] & PCF8563_M_WEEKDAY; + ct.mon = FROMBCD(val[PCF8563_R_MONTH] & PCF8563_M_MONTH); + ct.year = FROMBCD(val[PCF8563_R_YEAR] & PCF8563_M_YEAR); + ct.year += sc->sc_year0; + if (ct.year < POSIX_BASE_YEAR) + ct.year += 100; /* assume [1970, 2069] */ + if ((val[PCF8563_R_MONTH] & PCF8563_R_MONTH_C) != 0) { + if (ct.year >= 100 + sc->sc_year0) + sc->sc_flags |= PCF8563_CPOL; + } else if (ct.year < 100 + sc->sc_year0) + sc->sc_flags |= PCF8563_CPOL; + return (clock_ct_to_ts(&ct, ts)); +} + +static int +pcf8563_settime(device_t dev, struct timespec *ts) +{ + struct clocktime ct; + uint8_t val[PCF8563_NCLOCKREGS]; + struct iic_msg msgs[] = { + { 0, IIC_M_WR, PCF8563_NCLOCKREGS - 1, &val[PCF8563_R_CS2] } + }; + struct pcf8563_softc *sc; + int error; + + sc = device_get_softc(dev); + val[PCF8563_R_CS2] = PCF8563_R_SECOND; /* abuse */ + /* Accuracy is only one second. */ + if (ts->tv_nsec >= 500000000) + ts->tv_sec++; + ts->tv_nsec = 0; + clock_ts_to_ct(ts, &ct); + val[PCF8563_R_SECOND] = TOBCD(ct.sec); + val[PCF8563_R_MINUTE] = TOBCD(ct.min); + val[PCF8563_R_HOUR] = TOBCD(ct.hour); + val[PCF8563_R_DAY] = TOBCD(ct.day); + val[PCF8563_R_WEEKDAY] = ct.dow; + val[PCF8563_R_MONTH] = TOBCD(ct.mon); + val[PCF8563_R_YEAR] = TOBCD(ct.year % 100); + if ((sc->sc_flags & PCF8563_CPOL) != 0) { + if (ct.year >= 100 + sc->sc_year0) + val[PCF8563_R_MONTH] |= PCF8563_R_MONTH_C; + } else if (ct.year < 100 + sc->sc_year0) + val[PCF8563_R_MONTH] |= PCF8563_R_MONTH_C; + + msgs[0].slave = sc->sc_addr; + error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) / + sizeof(*msgs)); + if (error != 0) + device_printf(dev, "%s: cannot write RTC\n", __func__); + return (error); +} + +static device_method_t pcf8563_methods[] = { + DEVMETHOD(device_probe, pcf8563_probe), + DEVMETHOD(device_attach, pcf8563_attach), + + DEVMETHOD(clock_gettime, pcf8563_gettime), + DEVMETHOD(clock_settime, pcf8563_settime), + + DEVMETHOD_END +}; + +static driver_t pcf8563_driver = { + "pcf8563_rtc", + pcf8563_methods, + sizeof(struct pcf8563_softc), +}; + +static devclass_t pcf8563_devclass; + +DRIVER_MODULE(pcf8563, iicbus, pcf8563_driver, pcf8563_devclass, NULL, NULL); +MODULE_VERSION(pcf8563, 1); +MODULE_DEPEND(pcf8563, iicbus, 1, 1, 1); Added: head/sys/dev/iicbus/pcf8563reg.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/iicbus/pcf8563reg.h Fri Apr 13 23:07:32 2012 (r234248) @@ -0,0 +1,58 @@ +/* $NetBSD: pcf8563reg.h,v 1.1 2011/01/21 19:11:47 jakllsch Exp $ */ + +/*- + * Jonathan Kollasch, 2011 + * + * This file is in the public domain. + * + * $FreeBSD$ + */ + +/* + * NXP (Philips) PCF8563 RTC registers + */ + +/* We only have clock mode registers here. */ + +#ifndef _PCF8563REG_H_ +#define _PCF8563REG_H_ + +/* + * PCF8563 RTC I2C address: + * + * 101 0001 + */ +#define PCF8563_ADDR 0xa2 + +#define PCF8563_R_CS1 0x00 +#define PCF8563_R_CS2 0x01 +#define PCF8563_R_SECOND 0x02 +#define PCF8563_R_MINUTE 0x03 +#define PCF8563_R_HOUR 0x04 +#define PCF8563_R_DAY 0x05 +#define PCF8563_R_WEEKDAY 0x06 +#define PCF8563_R_MONTH 0x07 +#define PCF8563_R_YEAR 0x08 +#define PCF8563_R_MINUTE_ALARM 0x09 +#define PCF8563_R_HOUR_ALARM 0x0a +#define PCF8563_R_DAY_ALARM 0x0b +#define PCF8563_R_WEEKDAY_ALARM 0x0c +#define PCF8563_R_CLKOUT_CNTRL 0x0d +#define PCF8563_R_TIMER_CNTRL 0x0e +#define PCF8563_R_TIMER 0x0f + +#define PCF8563_R_SECOND_VL 0x80 +#define PCF8563_R_MONTH_C 0x80 + +#define PCF8563_NREGS 0x10 + +#define PCF8563_M_SECOND 0x7f +#define PCF8563_M_MINUTE 0x7f +#define PCF8563_M_HOUR 0x3f +#define PCF8563_M_DAY 0x3f +#define PCF8563_M_WEEKDAY 0x07 +#define PCF8563_M_MONTH 0x1f +#define PCF8563_M_CENTURY 0x80 +#define PCF8563_M_YEAR 0xff + +#endif /* _PCF8563REG_H_ */
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201204132307.q3DN7WNM001811>