mirror of
https://github.com/opnsense/src.git
synced 2026-04-22 14:49:36 -04:00
Add a driver for the Dallas/Maxim DS1307, another common i2c RTC.
Many thanks to ian who gently provided me the DS1307 breakout board. Tested on: Raspberry pi Differential Revision: https://reviews.freebsd.org/D2022 Reviewed by: rpaulo
This commit is contained in:
parent
25c1155771
commit
d7eb38c008
5 changed files with 603 additions and 0 deletions
|
|
@ -119,6 +119,7 @@ MAN= aac.4 \
|
|||
divert.4 \
|
||||
${_dpms.4} \
|
||||
dpt.4 \
|
||||
ds1307.4 \
|
||||
ds3231.4 \
|
||||
dummynet.4 \
|
||||
ed.4 \
|
||||
|
|
|
|||
131
share/man/man4/ds1307.4
Normal file
131
share/man/man4/ds1307.4
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
.\"
|
||||
.\" Copyright (c) 2015 Luiz Otavio O Souza <loos@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 ``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 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.
|
||||
.\"
|
||||
.\" $FreeBSD$
|
||||
.\"
|
||||
.Dd March 7, 2015
|
||||
.Dt DS1307 4
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm ds1307
|
||||
.Nd 64 x 8, serial, i2c real-time clock
|
||||
.Sh SYNOPSIS
|
||||
.Cd "device iic"
|
||||
.Cd "device iicbus"
|
||||
.Cd "device ds1307"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Nm
|
||||
serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD)
|
||||
clock/calendar plus 56 bytes of NV SRAM.
|
||||
.Pp
|
||||
The
|
||||
.Nm
|
||||
has a built-in power-sense circuit that detects power failures and
|
||||
automatically switches to the backup supply.
|
||||
Timekeeping operation continues while the part operates from the backup supply.
|
||||
.Pp
|
||||
Access to
|
||||
.Nm
|
||||
settings is made with the
|
||||
.Xr sysctl 8
|
||||
interface:
|
||||
.Bd -literal
|
||||
dev.ds1307.0.%desc: Maxim DS1307 RTC
|
||||
dev.ds1307.0.%driver: ds1307
|
||||
dev.ds1307.0.%location: addr=0xd0
|
||||
dev.ds1307.0.%pnpinfo: name=rtc compat=maxim,ds1307
|
||||
dev.ds1307.0.%parent: iicbus1
|
||||
dev.ds1307.0.sqwe: 1
|
||||
dev.ds1307.0.sqw_freq: 32768
|
||||
dev.ds1307.0.sqw_out: 0
|
||||
.Ed
|
||||
.Bl -tag -width ".Va dev.ds1307.%d.sqw_freq"
|
||||
.It Va dev.ds1307.%d.sqwe
|
||||
If set to 1, the SQW pin drives a square-wave of
|
||||
.Va dev.ds1307.%d.sqw_freq
|
||||
frequency.
|
||||
If set to 0, the output level of SQW pin is controlled by
|
||||
.Va dev.ds1307.%d.sqw_out .
|
||||
.It Va dev.ds1307.%d.sqw_freq
|
||||
Select the frequency of the SQW pin when the square-wave output is enabled on
|
||||
.Va dev.ds1307.%d.sqwe .
|
||||
It can be set to 1, 4096, 8192 and 32768.
|
||||
.It Va dev.ds1307.%d.sqw_out
|
||||
Set the output level of the SQW pin when
|
||||
.Va dev.ds1307.%d.sqwe
|
||||
is set to 0.
|
||||
.El
|
||||
.Pp
|
||||
Please check the
|
||||
.Nm
|
||||
datasheet for more details.
|
||||
.Pp
|
||||
On a
|
||||
.Xr device.hints 5
|
||||
based system, such as
|
||||
.Li MIPS ,
|
||||
these values are configurable for
|
||||
.Nm :
|
||||
.Bl -tag -width ".Va hint.ds1307.%d.addr"
|
||||
.It Va hint.ds1307.%d.at
|
||||
The
|
||||
.Xr iicbus 4
|
||||
that the
|
||||
.Nm
|
||||
is connected to.
|
||||
.It Va hint.ds1307.%d.addr
|
||||
The i2c address of
|
||||
.Nm .
|
||||
.El
|
||||
.Pp
|
||||
On a
|
||||
.Xr FDT 4
|
||||
based system the following properties must be set:
|
||||
.Bl -tag -width ".Va compatible"
|
||||
.It Va compatible
|
||||
Must always be set to "dallas,ds1307" or "maxim,ds1307".
|
||||
.It Va reg
|
||||
The i2c address of
|
||||
.Nm .
|
||||
The default address for
|
||||
.Nm
|
||||
is 0xd0.
|
||||
.El
|
||||
.Sh SEE ALSO
|
||||
.Xr fdt 4 ,
|
||||
.Xr iic 4 ,
|
||||
.Xr iicbus 4 ,
|
||||
.Xr sysctl 8
|
||||
.Sh HISTORY
|
||||
The
|
||||
.Nm
|
||||
driver first appeared in
|
||||
.Fx 11.0 .
|
||||
.Sh AUTHORS
|
||||
.An -nosplit
|
||||
The
|
||||
.Nm
|
||||
driver and this manual page were written by
|
||||
.An Luiz Otavio O Souza Aq Mt loos@FreeBSD.org .
|
||||
|
|
@ -1449,6 +1449,7 @@ dev/ida/ida_pci.c optional ida pci
|
|||
dev/ie/if_ie.c optional ie isa nowerror
|
||||
dev/ie/if_ie_isa.c optional ie isa
|
||||
dev/iicbus/ad7418.c optional ad7418
|
||||
dev/iicbus/ds1307.c optional ds1307
|
||||
dev/iicbus/ds133x.c optional ds133x
|
||||
dev/iicbus/ds1374.c optional ds1374
|
||||
dev/iicbus/ds1672.c optional ds1672
|
||||
|
|
|
|||
411
sys/dev/iicbus/ds1307.c
Normal file
411
sys/dev/iicbus/ds1307.c
Normal file
|
|
@ -0,0 +1,411 @@
|
|||
/*-
|
||||
* Copyright (c) 2015 Luiz Otavio O Souza <loos@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 Maxim DS1307 I2C real-time clock/calendar.
|
||||
*/
|
||||
|
||||
#include "opt_platform.h"
|
||||
|
||||
#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 <sys/sysctl.h>
|
||||
|
||||
#include <dev/iicbus/iicbus.h>
|
||||
#include <dev/iicbus/iiconf.h>
|
||||
#ifdef FDT
|
||||
#include <dev/ofw/openfirm.h>
|
||||
#include <dev/ofw/ofw_bus.h>
|
||||
#include <dev/ofw/ofw_bus_subr.h>
|
||||
#endif
|
||||
|
||||
#include <dev/iicbus/ds1307reg.h>
|
||||
|
||||
#include "clock_if.h"
|
||||
#include "iicbus_if.h"
|
||||
|
||||
struct ds1307_softc {
|
||||
device_t sc_dev;
|
||||
int sc_year0;
|
||||
struct intr_config_hook enum_hook;
|
||||
uint16_t sc_addr; /* DS1307 slave address. */
|
||||
uint8_t sc_ctrl;
|
||||
};
|
||||
|
||||
static void ds1307_start(void *);
|
||||
|
||||
static int
|
||||
ds1307_read(device_t dev, uint16_t addr, uint8_t reg, uint8_t *data, size_t len)
|
||||
{
|
||||
struct iic_msg msg[2] = {
|
||||
{ addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
|
||||
{ addr, IIC_M_RD, len, data },
|
||||
};
|
||||
|
||||
return (iicbus_transfer(dev, msg, nitems(msg)));
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_write(device_t dev, uint16_t addr, uint8_t *data, size_t len)
|
||||
{
|
||||
struct iic_msg msg[1] = {
|
||||
{ addr, IIC_M_WR, len, data },
|
||||
};
|
||||
|
||||
return (iicbus_transfer(dev, msg, nitems(msg)));
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_ctrl_read(struct ds1307_softc *sc)
|
||||
{
|
||||
int error;
|
||||
|
||||
sc->sc_ctrl = 0;
|
||||
error = ds1307_read(sc->sc_dev, sc->sc_addr, DS1307_CONTROL,
|
||||
&sc->sc_ctrl, sizeof(sc->sc_ctrl));
|
||||
if (error) {
|
||||
device_printf(sc->sc_dev, "cannot read from RTC.\n");
|
||||
return (error);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_ctrl_write(struct ds1307_softc *sc)
|
||||
{
|
||||
int error;
|
||||
uint8_t data[2];
|
||||
|
||||
data[0] = DS1307_CONTROL;
|
||||
data[1] = sc->sc_ctrl & DS1307_CTRL_MASK;
|
||||
error = ds1307_write(sc->sc_dev, sc->sc_addr, data, sizeof(data));
|
||||
if (error != 0)
|
||||
device_printf(sc->sc_dev, "cannot write to RTC.\n");
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_osc_enable(struct ds1307_softc *sc)
|
||||
{
|
||||
int error;
|
||||
uint8_t data[2], secs;
|
||||
|
||||
secs = 0;
|
||||
error = ds1307_read(sc->sc_dev, sc->sc_addr, DS1307_SECS,
|
||||
&secs, sizeof(secs));
|
||||
if (error) {
|
||||
device_printf(sc->sc_dev, "cannot read from RTC.\n");
|
||||
return (error);
|
||||
}
|
||||
/* Check if the oscillator is disabled. */
|
||||
if ((secs & DS1307_SECS_CH) == 0)
|
||||
return (0);
|
||||
device_printf(sc->sc_dev, "clock was halted, check the battery.\n");
|
||||
data[0] = DS1307_SECS;
|
||||
data[1] = secs & DS1307_SECS_MASK;
|
||||
error = ds1307_write(sc->sc_dev, sc->sc_addr, data, sizeof(data));
|
||||
if (error != 0)
|
||||
device_printf(sc->sc_dev, "cannot write to RTC.\n");
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_set_24hrs_mode(struct ds1307_softc *sc)
|
||||
{
|
||||
int error;
|
||||
uint8_t data[2], hour;
|
||||
|
||||
hour = 0;
|
||||
error = ds1307_read(sc->sc_dev, sc->sc_addr, DS1307_HOUR,
|
||||
&hour, sizeof(hour));
|
||||
if (error) {
|
||||
device_printf(sc->sc_dev, "cannot read from RTC.\n");
|
||||
return (error);
|
||||
}
|
||||
data[0] = DS1307_HOUR;
|
||||
data[1] = hour & DS1307_HOUR_MASK;
|
||||
error = ds1307_write(sc->sc_dev, sc->sc_addr, data, sizeof(data));
|
||||
if (error != 0)
|
||||
device_printf(sc->sc_dev, "cannot write to RTC.\n");
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_sqwe_sysctl(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
int sqwe, error, newv;
|
||||
struct ds1307_softc *sc;
|
||||
|
||||
sc = (struct ds1307_softc *)arg1;
|
||||
error = ds1307_ctrl_read(sc);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
sqwe = newv = (sc->sc_ctrl & DS1307_CTRL_SQWE) ? 1 : 0;
|
||||
error = sysctl_handle_int(oidp, &newv, 0, req);
|
||||
if (error != 0 || req->newptr == NULL)
|
||||
return (error);
|
||||
if (sqwe != newv) {
|
||||
sc->sc_ctrl &= ~DS1307_CTRL_SQWE;
|
||||
if (newv)
|
||||
sc->sc_ctrl |= DS1307_CTRL_SQWE;
|
||||
error = ds1307_ctrl_write(sc);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
}
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_sqw_freq_sysctl(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
int ds1307_sqw_freq[] = { 1, 4096, 8192, 32768 };
|
||||
int error, freq, i, newf, tmp;
|
||||
struct ds1307_softc *sc;
|
||||
|
||||
sc = (struct ds1307_softc *)arg1;
|
||||
error = ds1307_ctrl_read(sc);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
tmp = (sc->sc_ctrl & DS1307_CTRL_RS_MASK);
|
||||
if (tmp >= nitems(ds1307_sqw_freq))
|
||||
tmp = nitems(ds1307_sqw_freq) - 1;
|
||||
freq = ds1307_sqw_freq[tmp];
|
||||
error = sysctl_handle_int(oidp, &freq, 0, req);
|
||||
if (error != 0 || req->newptr == NULL)
|
||||
return (error);
|
||||
if (freq != ds1307_sqw_freq[tmp]) {
|
||||
newf = 0;
|
||||
for (i = 0; i < nitems(ds1307_sqw_freq); i++)
|
||||
if (freq >= ds1307_sqw_freq[i])
|
||||
newf = i;
|
||||
sc->sc_ctrl &= ~DS1307_CTRL_RS_MASK;
|
||||
sc->sc_ctrl |= newf;
|
||||
error = ds1307_ctrl_write(sc);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
}
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_sqw_out_sysctl(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
int sqwe, error, newv;
|
||||
struct ds1307_softc *sc;
|
||||
|
||||
sc = (struct ds1307_softc *)arg1;
|
||||
error = ds1307_ctrl_read(sc);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
sqwe = newv = (sc->sc_ctrl & DS1307_CTRL_OUT) ? 1 : 0;
|
||||
error = sysctl_handle_int(oidp, &newv, 0, req);
|
||||
if (error != 0 || req->newptr == NULL)
|
||||
return (error);
|
||||
if (sqwe != newv) {
|
||||
sc->sc_ctrl &= ~DS1307_CTRL_OUT;
|
||||
if (newv)
|
||||
sc->sc_ctrl |= DS1307_CTRL_OUT;
|
||||
error = ds1307_ctrl_write(sc);
|
||||
if (error != 0)
|
||||
return (error);
|
||||
}
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_probe(device_t dev)
|
||||
{
|
||||
|
||||
#ifdef FDT
|
||||
if (!ofw_bus_status_okay(dev))
|
||||
return (ENXIO);
|
||||
if (!ofw_bus_is_compatible(dev, "dallas,ds1307") &&
|
||||
!ofw_bus_is_compatible(dev, "maxim,ds1307"))
|
||||
return (ENXIO);
|
||||
#endif
|
||||
device_set_desc(dev, "Maxim DS1307 RTC");
|
||||
|
||||
return (BUS_PROBE_DEFAULT);
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_attach(device_t dev)
|
||||
{
|
||||
struct ds1307_softc *sc;
|
||||
|
||||
sc = device_get_softc(dev);
|
||||
sc->sc_dev = dev;
|
||||
sc->sc_addr = iicbus_get_addr(dev);
|
||||
sc->sc_year0 = 1900;
|
||||
sc->enum_hook.ich_func = ds1307_start;
|
||||
sc->enum_hook.ich_arg = dev;
|
||||
|
||||
/*
|
||||
* We have to wait until interrupts are enabled. Usually I2C read
|
||||
* and write only works when the interrupts are available.
|
||||
*/
|
||||
if (config_intrhook_establish(&sc->enum_hook) != 0)
|
||||
return (ENOMEM);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
ds1307_start(void *xdev)
|
||||
{
|
||||
device_t dev;
|
||||
struct ds1307_softc *sc;
|
||||
struct sysctl_ctx_list *ctx;
|
||||
struct sysctl_oid *tree_node;
|
||||
struct sysctl_oid_list *tree;
|
||||
|
||||
dev = (device_t)xdev;
|
||||
sc = device_get_softc(dev);
|
||||
ctx = device_get_sysctl_ctx(dev);
|
||||
tree_node = device_get_sysctl_tree(dev);
|
||||
tree = SYSCTL_CHILDREN(tree_node);
|
||||
|
||||
config_intrhook_disestablish(&sc->enum_hook);
|
||||
/* Set the 24 hours mode. */
|
||||
if (ds1307_set_24hrs_mode(sc) != 0)
|
||||
return;
|
||||
/* Enable the oscillator if halted. */
|
||||
if (ds1307_osc_enable(sc) != 0)
|
||||
return;
|
||||
|
||||
/* Configuration parameters. */
|
||||
SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "sqwe",
|
||||
CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,
|
||||
ds1307_sqwe_sysctl, "IU", "DS1307 square-wave enable");
|
||||
SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "sqw_freq",
|
||||
CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,
|
||||
ds1307_sqw_freq_sysctl, "IU",
|
||||
"DS1307 square-wave output frequency");
|
||||
SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "sqw_out",
|
||||
CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,
|
||||
ds1307_sqw_out_sysctl, "IU", "DS1307 square-wave output state");
|
||||
|
||||
/* 1 second resolution. */
|
||||
clock_register(dev, 1000000);
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_gettime(device_t dev, struct timespec *ts)
|
||||
{
|
||||
int error;
|
||||
struct clocktime ct;
|
||||
struct ds1307_softc *sc;
|
||||
uint8_t data[7];
|
||||
|
||||
sc = device_get_softc(dev);
|
||||
memset(data, 0, sizeof(data));
|
||||
error = ds1307_read(sc->sc_dev, sc->sc_addr, DS1307_SECS,
|
||||
data, sizeof(data));
|
||||
if (error != 0) {
|
||||
device_printf(dev, "cannot read from RTC.\n");
|
||||
return (error);
|
||||
}
|
||||
ct.nsec = 0;
|
||||
ct.sec = FROMBCD(data[DS1307_SECS] & DS1307_SECS_MASK);
|
||||
ct.min = FROMBCD(data[DS1307_MINS] & DS1307_MINS_MASK);
|
||||
ct.hour = FROMBCD(data[DS1307_HOUR] & DS1307_HOUR_MASK);
|
||||
ct.day = FROMBCD(data[DS1307_DATE] & DS1307_DATE_MASK);
|
||||
ct.dow = data[DS1307_WEEKDAY] & DS1307_WEEKDAY_MASK;
|
||||
ct.mon = FROMBCD(data[DS1307_MONTH] & DS1307_MONTH_MASK);
|
||||
ct.year = FROMBCD(data[DS1307_YEAR] & DS1307_YEAR_MASK);
|
||||
ct.year += sc->sc_year0;
|
||||
if (ct.year < POSIX_BASE_YEAR)
|
||||
ct.year += 100; /* assume [1970, 2069] */
|
||||
|
||||
return (clock_ct_to_ts(&ct, ts));
|
||||
}
|
||||
|
||||
static int
|
||||
ds1307_settime(device_t dev, struct timespec *ts)
|
||||
{
|
||||
int error;
|
||||
struct clocktime ct;
|
||||
struct ds1307_softc *sc;
|
||||
uint8_t data[8];
|
||||
|
||||
sc = device_get_softc(dev);
|
||||
/* Accuracy is only one second. */
|
||||
if (ts->tv_nsec >= 500000000)
|
||||
ts->tv_sec++;
|
||||
ts->tv_nsec = 0;
|
||||
clock_ts_to_ct(ts, &ct);
|
||||
memset(data, 0, sizeof(data));
|
||||
data[0] = DS1307_SECS;
|
||||
data[DS1307_SECS + 1] = TOBCD(ct.sec);
|
||||
data[DS1307_MINS + 1] = TOBCD(ct.min);
|
||||
data[DS1307_HOUR + 1] = TOBCD(ct.hour);
|
||||
data[DS1307_DATE + 1] = TOBCD(ct.day);
|
||||
data[DS1307_WEEKDAY + 1] = ct.dow;
|
||||
data[DS1307_MONTH + 1] = TOBCD(ct.mon);
|
||||
data[DS1307_YEAR + 1] = TOBCD(ct.year % 100);
|
||||
/* Write the time back to RTC. */
|
||||
error = ds1307_write(dev, sc->sc_addr, data, sizeof(data));
|
||||
if (error != 0)
|
||||
device_printf(dev, "cannot write to RTC.\n");
|
||||
|
||||
return (error);
|
||||
}
|
||||
|
||||
static device_method_t ds1307_methods[] = {
|
||||
DEVMETHOD(device_probe, ds1307_probe),
|
||||
DEVMETHOD(device_attach, ds1307_attach),
|
||||
|
||||
DEVMETHOD(clock_gettime, ds1307_gettime),
|
||||
DEVMETHOD(clock_settime, ds1307_settime),
|
||||
|
||||
DEVMETHOD_END
|
||||
};
|
||||
|
||||
static driver_t ds1307_driver = {
|
||||
"ds1307",
|
||||
ds1307_methods,
|
||||
sizeof(struct ds1307_softc),
|
||||
};
|
||||
|
||||
static devclass_t ds1307_devclass;
|
||||
|
||||
DRIVER_MODULE(ds1307, iicbus, ds1307_driver, ds1307_devclass, NULL, NULL);
|
||||
MODULE_VERSION(ds1307, 1);
|
||||
MODULE_DEPEND(ds1307, iicbus, 1, 1, 1);
|
||||
59
sys/dev/iicbus/ds1307reg.h
Normal file
59
sys/dev/iicbus/ds1307reg.h
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/*-
|
||||
* Copyright (c) 2015 Luiz Otavio O Souza <loos@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.
|
||||
*
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
/*
|
||||
* Maxim DS1307 RTC registers.
|
||||
*/
|
||||
|
||||
#ifndef _DS1307REG_H_
|
||||
#define _DS1307REG_H_
|
||||
|
||||
#define DS1307_SECS 0x00
|
||||
#define DS1307_SECS_MASK 0x7f
|
||||
#define DS1307_SECS_CH 0x80
|
||||
#define DS1307_MINS 0x01
|
||||
#define DS1307_MINS_MASK 0x7f
|
||||
#define DS1307_HOUR 0x02
|
||||
#define DS1307_HOUR_MASK 0x3f
|
||||
#define DS1307_WEEKDAY 0x03
|
||||
#define DS1307_WEEKDAY_MASK 0x07
|
||||
#define DS1307_DATE 0x04
|
||||
#define DS1307_DATE_MASK 0x3f
|
||||
#define DS1307_MONTH 0x05
|
||||
#define DS1307_MONTH_MASK 0x1f
|
||||
#define DS1307_YEAR 0x06
|
||||
#define DS1307_YEAR_MASK 0xff
|
||||
#define DS1307_CONTROL 0x07
|
||||
#define DS1307_CTRL_OUT (1 << 7)
|
||||
#define DS1307_CTRL_SQWE (1 << 4)
|
||||
#define DS1307_CTRL_RS1 (1 << 1)
|
||||
#define DS1307_CTRL_RS0 (1 << 0)
|
||||
#define DS1307_CTRL_RS_MASK (DS1307_CTRL_RS1 | DS1307_CTRL_RS0)
|
||||
#define DS1307_CTRL_MASK 0x93
|
||||
|
||||
#endif /* _DS1307REG_H_ */
|
||||
Loading…
Reference in a new issue