xref: /netbsd-src/sys/dev/i2c/pcf8563.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: pcf8563.c,v 1.3 2012/01/07 21:02:15 phx Exp $	*/
2 
3 /*
4  * Copyright (c) 2011 Jonathan A. Kollasch
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: pcf8563.c,v 1.3 2012/01/07 21:02:15 phx Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/kernel.h>
36 
37 #include <dev/clock_subr.h>
38 
39 #include <dev/i2c/i2cvar.h>
40 #include <dev/i2c/pcf8563reg.h>
41 
42 struct pcf8563rtc_softc {
43 	device_t sc_dev;
44 	i2c_tag_t sc_tag;
45 	int sc_addr;
46 	struct todr_chip_handle sc_todr;
47 };
48 
49 static int pcf8563rtc_match(device_t, cfdata_t, void *);
50 static void pcf8563rtc_attach(device_t, device_t, void *);
51 
52 CFATTACH_DECL_NEW(pcf8563rtc, sizeof(struct pcf8563rtc_softc),
53     pcf8563rtc_match, pcf8563rtc_attach, NULL, NULL);
54 
55 static int pcf8563rtc_clock_read(struct pcf8563rtc_softc *, struct clock_ymdhms *);
56 static int pcf8563rtc_clock_write(struct pcf8563rtc_softc *, struct clock_ymdhms *);
57 static int pcf8563rtc_gettime(struct todr_chip_handle *, struct clock_ymdhms *);
58 static int pcf8563rtc_settime(struct todr_chip_handle *, struct clock_ymdhms *);
59 
60 static int
61 pcf8563rtc_match(device_t parent, cfdata_t cf, void *aux)
62 {
63 	struct i2c_attach_args *ia = aux;
64 
65 	if (ia->ia_name) {
66 		/* direct config - check name */
67 		if (strcmp(ia->ia_name, "pcf8563rtc") == 0)
68 			return 1;
69 	} else {
70 		/* indirect config - check typical address */
71 		if (ia->ia_addr == PCF8563_ADDR)
72 			return 1;
73 	}
74 	return 0;
75 }
76 
77 static void
78 pcf8563rtc_attach(device_t parent, device_t self, void *aux)
79 {
80 	struct pcf8563rtc_softc *sc = device_private(self);
81 	struct i2c_attach_args *ia = aux;
82 
83 	aprint_naive(": Real-time Clock\n");
84 	aprint_normal(": NXP PCF8563 Real-time Clock\n");
85 
86 	sc->sc_dev = self;
87 	sc->sc_tag = ia->ia_tag;
88 	sc->sc_addr = ia->ia_addr;
89 	sc->sc_todr.cookie = sc;
90 	sc->sc_todr.todr_gettime_ymdhms = pcf8563rtc_gettime;
91 	sc->sc_todr.todr_settime_ymdhms = pcf8563rtc_settime;
92 	sc->sc_todr.todr_setwen = NULL;
93 
94 	todr_attach(&sc->sc_todr);
95 }
96 
97 static int
98 pcf8563rtc_gettime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
99 {
100 	struct pcf8563rtc_softc *sc = ch->cookie;
101 
102 	if (pcf8563rtc_clock_read(sc, dt) == 0)
103 		return -1;
104 
105 	return 0;
106 }
107 
108 static int
109 pcf8563rtc_settime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
110 {
111         struct pcf8563rtc_softc *sc = ch->cookie;
112 
113 	if (pcf8563rtc_clock_write(sc, dt) == 0)
114 		return -1;
115 
116 	return 0;
117 }
118 
119 static int
120 pcf8563rtc_clock_read(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
121 {
122 	uint8_t bcd[PCF8563_NREGS];
123 	uint8_t reg = PCF8563_R_CS1;
124 
125 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
126 		device_printf(sc->sc_dev, "acquire bus for read failed\n");
127 		return 0;
128 	}
129 
130 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, &reg, 1,
131 		     &bcd[reg], PCF8563_R_YEAR - reg + 1, I2C_F_POLL)) {
132 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
133 		device_printf(sc->sc_dev, "read failed\n");
134 		return 0;
135 	}
136 
137 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
138 
139 	dt->dt_sec = FROMBCD(bcd[PCF8563_R_SECOND] & PCF8563_M_SECOND);
140 	dt->dt_min = FROMBCD(bcd[PCF8563_R_MINUTE] & PCF8563_M_MINUTE);
141 	dt->dt_hour = FROMBCD(bcd[PCF8563_R_HOUR] & PCF8563_M_HOUR);
142 	dt->dt_day = FROMBCD(bcd[PCF8563_R_DAY] & PCF8563_M_DAY);
143 	dt->dt_mon = FROMBCD(bcd[PCF8563_R_MONTH] & PCF8563_M_MONTH);
144 	dt->dt_year = FROMBCD(bcd[PCF8563_R_YEAR] & PCF8563_M_YEAR);
145 	dt->dt_year += 2000;
146 
147 	return 1;
148 }
149 
150 static int
151 pcf8563rtc_clock_write(struct pcf8563rtc_softc *sc, struct clock_ymdhms *dt)
152 {
153 	uint8_t bcd[PCF8563_NREGS];
154 	uint8_t reg = PCF8563_R_SECOND;
155 
156 	bcd[PCF8563_R_SECOND] = TOBCD(dt->dt_sec);
157 	bcd[PCF8563_R_MINUTE] = TOBCD(dt->dt_min);
158 	bcd[PCF8563_R_HOUR] = TOBCD(dt->dt_hour);
159 	bcd[PCF8563_R_DAY] = TOBCD(dt->dt_day);
160 	bcd[PCF8563_R_WEEKDAY] = TOBCD(dt->dt_wday);
161 	bcd[PCF8563_R_MONTH] = TOBCD(dt->dt_mon);
162 	bcd[PCF8563_R_YEAR] = TOBCD(dt->dt_year % 100);
163 
164 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
165 		device_printf(sc->sc_dev, "acquire bus for write failed\n");
166 		return 0;
167 	}
168 
169 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, &reg, 1,
170 		     &bcd[reg], PCF8563_R_YEAR - reg + 1, I2C_F_POLL)) {
171 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
172 		device_printf(sc->sc_dev, "write failed\n");
173 		return 0;
174 	}
175 
176 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
177 
178 	return 1;
179 }
180