1 /* $NetBSD: pcf8583.c,v 1.11 2008/06/08 03:49:26 tsutsui Exp $ */
4 * Copyright (c) 2003 Wasabi Systems, Inc.
7 * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * Driver for the Philips PCF8583 Real Time Clock.
41 * This driver is partially derived from Ben Harris's PCF8583 driver
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: pcf8583.c,v 1.11 2008/06/08 03:49:26 tsutsui Exp $");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 #include <sys/kernel.h>
52 #include <sys/fcntl.h>
55 #include <sys/event.h>
57 #include <dev/clock_subr.h>
59 #include <dev/i2c/i2cvar.h>
60 #include <dev/i2c/pcf8583reg.h>
61 #include <dev/i2c/pcf8583var.h>
68 struct todr_chip_handle sc_todr
;
71 static int pcfrtc_match(device_t
, cfdata_t
, void *);
72 static void pcfrtc_attach(device_t
, device_t
, void *);
74 CFATTACH_DECL_NEW(pcfrtc
, sizeof(struct pcfrtc_softc
),
75 pcfrtc_match
, pcfrtc_attach
, NULL
, NULL
);
76 extern struct cfdriver pcfrtc_cd
;
78 dev_type_open(pcfrtc_open
);
79 dev_type_close(pcfrtc_close
);
80 dev_type_read(pcfrtc_read
);
81 dev_type_write(pcfrtc_write
);
83 const struct cdevsw pcfrtc_cdevsw
= {
84 pcfrtc_open
, pcfrtc_close
, pcfrtc_read
, pcfrtc_write
, noioctl
,
85 nostop
, notty
, nopoll
, nommap
, nokqfilter
, D_OTHER
88 static int pcfrtc_clock_read(struct pcfrtc_softc
*, struct clock_ymdhms
*,
90 static int pcfrtc_clock_write(struct pcfrtc_softc
*, struct clock_ymdhms
*,
92 static int pcfrtc_gettime(struct todr_chip_handle
*, struct timeval
*);
93 static int pcfrtc_settime(struct todr_chip_handle
*, struct timeval
*);
96 pcfrtc_match(device_t parent
, cfdata_t cf
, void *aux
)
98 struct i2c_attach_args
*ia
= aux
;
100 if ((ia
->ia_addr
& PCF8583_ADDRMASK
) == PCF8583_ADDR
)
107 pcfrtc_attach(device_t parent
, device_t self
, void *aux
)
109 struct pcfrtc_softc
*sc
= device_private(self
);
110 struct i2c_attach_args
*ia
= aux
;
111 uint8_t cmdbuf
[1], csr
;
113 sc
->sc_tag
= ia
->ia_tag
;
114 sc
->sc_address
= ia
->ia_addr
;
117 aprint_naive(": Real-time Clock/NVRAM\n");
118 aprint_normal(": PCF8583 Real-time Clock/NVRAM\n");
120 cmdbuf
[0] = PCF8583_REG_CSR
;
121 if (iic_exec(sc
->sc_tag
, I2C_OP_READ_WITH_STOP
, sc
->sc_address
,
122 cmdbuf
, 1, &csr
, 1, 0) != 0) {
123 aprint_error_dev(self
, "unable to read CSR\n");
126 aprint_normal_dev(sc
->sc_dev
, "");
127 switch (csr
& PCF8583_CSR_FN_MASK
) {
128 case PCF8583_CSR_FN_32768HZ
:
129 aprint_normal(" 32.768 kHz clock");
132 case PCF8583_CSR_FN_50HZ
:
133 aprint_normal(" 50 Hz clock");
136 case PCF8583_CSR_FN_EVENT
:
137 aprint_normal(" event counter");
140 case PCF8583_CSR_FN_TEST
:
141 aprint_normal(" test mode");
144 if (csr
& PCF8583_CSR_STOP
)
145 aprint_normal(", stopped");
146 if (csr
& PCF8583_CSR_ALARMENABLE
)
147 aprint_normal(", alarm enabled");
152 sc
->sc_todr
.cookie
= sc
;
153 sc
->sc_todr
.todr_gettime
= pcfrtc_gettime
;
154 sc
->sc_todr
.todr_settime
= pcfrtc_settime
;
155 sc
->sc_todr
.todr_setwen
= NULL
;
157 todr_attach(&sc
->sc_todr
);
162 pcfrtc_open(dev_t dev
, int flag
, int fmt
, struct lwp
*l
)
164 struct pcfrtc_softc
*sc
;
166 if ((sc
= device_lookup_private(&pcfrtc_cd
, minor(dev
))) == NULL
)
180 pcfrtc_close(dev_t dev
, int flag
, int fmt
, struct lwp
*l
)
182 struct pcfrtc_softc
*sc
;
184 if ((sc
= device_lookup_private(&pcfrtc_cd
, minor(dev
))) == NULL
)
193 pcfrtc_read(dev_t dev
, struct uio
*uio
, int flags
)
195 struct pcfrtc_softc
*sc
;
196 u_int8_t ch
, cmdbuf
[1];
199 if ((sc
= device_lookup_private(&pcfrtc_cd
, minor(dev
))) == NULL
)
202 if (uio
->uio_offset
>= PCF8583_NVRAM_SIZE
)
205 if ((error
= iic_acquire_bus(sc
->sc_tag
, 0)) != 0)
208 while (uio
->uio_resid
&& uio
->uio_offset
< PCF8583_NVRAM_SIZE
) {
209 a
= (int)uio
->uio_offset
;
210 cmdbuf
[0] = a
+ PCF8583_NVRAM_START
;
211 if ((error
= iic_exec(sc
->sc_tag
, I2C_OP_READ_WITH_STOP
,
212 sc
->sc_address
, cmdbuf
, 1,
214 iic_release_bus(sc
->sc_tag
, 0);
215 aprint_error_dev(sc
->sc_dev
,
216 "pcfrtc_read: read failed at 0x%x\n", a
);
219 if ((error
= uiomove(&ch
, 1, uio
)) != 0) {
220 iic_release_bus(sc
->sc_tag
, 0);
225 iic_release_bus(sc
->sc_tag
, 0);
232 pcfrtc_write(dev_t dev
, struct uio
*uio
, int flags
)
234 struct pcfrtc_softc
*sc
;
238 if ((sc
= device_lookup_private(&pcfrtc_cd
, minor(dev
))) == NULL
)
241 if (uio
->uio_offset
>= PCF8583_NVRAM_SIZE
)
244 if ((error
= iic_acquire_bus(sc
->sc_tag
, 0)) != 0)
247 while (uio
->uio_resid
&& uio
->uio_offset
< PCF8583_NVRAM_SIZE
) {
248 a
= (int)uio
->uio_offset
;
249 cmdbuf
[0] = a
+ PCF8583_NVRAM_START
;
250 if ((error
= uiomove(&cmdbuf
[1], 1, uio
)) != 0)
253 if ((error
= iic_exec(sc
->sc_tag
,
254 uio
->uio_resid
? I2C_OP_WRITE
: I2C_OP_WRITE_WITH_STOP
,
255 sc
->sc_address
, cmdbuf
, 1, &cmdbuf
[1], 1, 0)) != 0) {
256 aprint_error_dev(sc
->sc_dev
,
257 "pcfrtc_write: write failed at 0x%x\n", a
);
262 iic_release_bus(sc
->sc_tag
, 0);
268 pcfrtc_gettime(struct todr_chip_handle
*ch
, struct timeval
*tv
)
270 struct pcfrtc_softc
*sc
= ch
->cookie
;
271 struct clock_ymdhms dt
;
275 if ((err
= pcfrtc_clock_read(sc
, &dt
, ¢i
)))
278 tv
->tv_sec
= clock_ymdhms_to_secs(&dt
);
279 tv
->tv_usec
= centi
* 10000;
285 pcfrtc_settime(struct todr_chip_handle
*ch
, struct timeval
*tv
)
287 struct pcfrtc_softc
*sc
= ch
->cookie
;
288 struct clock_ymdhms dt
;
291 clock_secs_to_ymdhms(tv
->tv_sec
, &dt
);
293 if ((err
= pcfrtc_clock_write(sc
, &dt
, tv
->tv_usec
/ 10000) == 0))
299 static const int pcf8583_rtc_offset
[] = {
305 PCF8583_REG_YEARDATE
,
308 0xc0, /* NVRAM -- year stored here */
309 0xc1, /* NVRAM -- century stored here */
313 pcfrtc_clock_read(struct pcfrtc_softc
*sc
, struct clock_ymdhms
*dt
,
316 u_int8_t bcd
[10], cmdbuf
[1];
319 if ((err
= iic_acquire_bus(sc
->sc_tag
, I2C_F_POLL
))) {
320 aprint_error_dev(sc
->sc_dev
,
321 "pcfrtc_clock_read: failed to acquire I2C bus\n");
325 /* Read each timekeeping register in order. */
326 for (i
= 0; i
< 10; i
++) {
327 cmdbuf
[0] = pcf8583_rtc_offset
[i
];
329 if ((err
= iic_exec(sc
->sc_tag
, I2C_OP_READ_WITH_STOP
,
330 sc
->sc_address
, cmdbuf
, 1,
331 &bcd
[i
], 1, I2C_F_POLL
))) {
332 iic_release_bus(sc
->sc_tag
, I2C_F_POLL
);
333 aprint_error_dev(sc
->sc_dev
,
334 "pcfrtc_clock_read: failed to read rtc "
336 pcf8583_rtc_offset
[i
]);
342 iic_release_bus(sc
->sc_tag
, I2C_F_POLL
);
345 * Convert the PCF8583's register values into something useable
347 *centi
= FROMBCD(bcd
[PCF8583_REG_CENTI
]);
348 dt
->dt_sec
= FROMBCD(bcd
[PCF8583_REG_SEC
]);
349 dt
->dt_min
= FROMBCD(bcd
[PCF8583_REG_MIN
]);
350 dt
->dt_hour
= FROMBCD(bcd
[PCF8583_REG_HOUR
] & PCF8583_HOUR_MASK
);
351 if (bcd
[PCF8583_REG_HOUR
] & PCF8583_HOUR_12H
) {
352 dt
->dt_hour
%= 12; /* 12AM -> 0, 12PM -> 12 */
353 if (bcd
[PCF8583_REG_HOUR
] & PCF8583_HOUR_PM
)
357 dt
->dt_day
= FROMBCD(bcd
[PCF8583_REG_YEARDATE
] & PCF8583_DATE_MASK
);
358 dt
->dt_mon
= FROMBCD(bcd
[PCF8583_REG_WKDYMON
] & PCF8583_MON_MASK
);
360 dt
->dt_year
= bcd
[8] + (bcd
[9] * 100);
361 /* Try to notice if the year's rolled over. */
362 if (bcd
[PCF8583_REG_CSR
] & PCF8583_CSR_MASK
)
363 aprint_error_dev(sc
->sc_dev
,
364 "cannot check year in mask mode\n");
366 while (dt
->dt_year
% 4 !=
367 (bcd
[PCF8583_REG_YEARDATE
] &
368 PCF8583_YEAR_MASK
) >> PCF8583_YEAR_SHIFT
)
376 pcfrtc_clock_write(struct pcfrtc_softc
*sc
, struct clock_ymdhms
*dt
,
379 uint8_t bcd
[10], cmdbuf
[2];
383 * Convert our time representation into something the PCF8583
386 bcd
[PCF8583_REG_CENTI
] = centi
;
387 bcd
[PCF8583_REG_SEC
] = TOBCD(dt
->dt_sec
);
388 bcd
[PCF8583_REG_MIN
] = TOBCD(dt
->dt_min
);
389 bcd
[PCF8583_REG_HOUR
] = TOBCD(dt
->dt_hour
) & PCF8583_HOUR_MASK
;
390 bcd
[PCF8583_REG_YEARDATE
] = TOBCD(dt
->dt_day
) |
391 ((dt
->dt_year
% 4) << PCF8583_YEAR_SHIFT
);
392 bcd
[PCF8583_REG_WKDYMON
] = TOBCD(dt
->dt_mon
) |
393 ((dt
->dt_wday
% 4) << PCF8583_WKDY_SHIFT
);
394 bcd
[8] = dt
->dt_year
% 100;
395 bcd
[9] = dt
->dt_year
/ 100;
397 if ((err
= iic_acquire_bus(sc
->sc_tag
, I2C_F_POLL
))) {
398 aprint_error_dev(sc
->sc_dev
,
399 "pcfrtc_clock_write: failed to acquire I2C bus\n");
403 for (i
= 1; i
< 10; i
++) {
404 cmdbuf
[0] = pcf8583_rtc_offset
[i
];
405 if ((err
= iic_exec(sc
->sc_tag
,
406 i
!= 9 ? I2C_OP_WRITE
: I2C_OP_WRITE_WITH_STOP
,
407 sc
->sc_address
, cmdbuf
, 1,
408 &bcd
[i
], 1, I2C_F_POLL
))) {
409 iic_release_bus(sc
->sc_tag
, I2C_F_POLL
);
410 aprint_error_dev(sc
->sc_dev
,
411 "pcfrtc_clock_write: failed to write rtc "
413 pcf8583_rtc_offset
[i
]);
418 iic_release_bus(sc
->sc_tag
, I2C_F_POLL
);
424 pcfrtc_bootstrap_read(i2c_tag_t tag
, int i2caddr
, int offset
,
425 u_int8_t
*rvp
, size_t len
)
430 * NOTE: "offset" is an absolute offset into the PCF8583
431 * address space, not relative to the NVRAM.
437 if (iic_acquire_bus(tag
, I2C_F_POLL
) != 0)
441 /* Read a single byte. */
443 if (iic_exec(tag
, I2C_OP_READ_WITH_STOP
, i2caddr
,
444 cmdbuf
, 1, rvp
, 1, I2C_F_POLL
)) {
445 iic_release_bus(tag
, I2C_F_POLL
);
454 iic_release_bus(tag
, I2C_F_POLL
);
459 pcfrtc_bootstrap_write(i2c_tag_t tag
, int i2caddr
, int offset
,
460 u_int8_t
*rvp
, size_t len
)
465 * NOTE: "offset" is an absolute offset into the PCF8583
466 * address space, not relative to the NVRAM.
472 if (iic_acquire_bus(tag
, I2C_F_POLL
) != 0)
476 /* Write a single byte. */
478 if (iic_exec(tag
, I2C_OP_WRITE_WITH_STOP
, i2caddr
,
479 cmdbuf
, 1, rvp
, 1, I2C_F_POLL
)) {
480 iic_release_bus(tag
, I2C_F_POLL
);
489 iic_release_bus(tag
, I2C_F_POLL
);