No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / i2c / x1226.c
blob3c56a763368eb3885f2859eebc4ca703954d2c4c
1 /* $NetBSD: x1226.c,v 1.13 2008/06/08 03:49:26 tsutsui Exp $ */
3 /*
4 * Copyright (c) 2003 Shigeyuki Fukushima.
5 * All rights reserved.
7 * Written by Shigeyuki Fukushima for the NetBSD Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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 * Shigeyuki Fukushima.
21 * 4. The name of Shigeyuki Fukushima may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
25 * THIS SOFTWARE IS PROVIDED BY SHIGEYUKI FUKUSHIMA ``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 SHIGEYUKI FUKUSHIMA
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.
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: x1226.c,v 1.13 2008/06/08 03:49:26 tsutsui Exp $");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/kernel.h>
45 #include <sys/fcntl.h>
46 #include <sys/uio.h>
47 #include <sys/conf.h>
48 #include <sys/event.h>
50 #include <dev/clock_subr.h>
52 #include <dev/i2c/i2cvar.h>
53 #include <dev/i2c/x1226reg.h>
55 struct xrtc_softc {
56 device_t sc_dev;
57 i2c_tag_t sc_tag;
58 int sc_address;
59 int sc_open;
60 struct todr_chip_handle sc_todr;
63 static void xrtc_attach(device_t, device_t, void *);
64 static int xrtc_match(device_t, cfdata_t, void *);
66 CFATTACH_DECL_NEW(xrtc, sizeof(struct xrtc_softc),
67 xrtc_match, xrtc_attach, NULL, NULL);
68 extern struct cfdriver xrtc_cd;
70 dev_type_open(xrtc_open);
71 dev_type_close(xrtc_close);
72 dev_type_read(xrtc_read);
73 dev_type_write(xrtc_write);
75 const struct cdevsw xrtc_cdevsw = {
76 xrtc_open, xrtc_close, xrtc_read, xrtc_write,
77 noioctl, nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
80 static int xrtc_clock_read(struct xrtc_softc *, struct clock_ymdhms *);
81 static int xrtc_clock_write(struct xrtc_softc *, struct clock_ymdhms *);
82 static int xrtc_gettime(struct todr_chip_handle *, struct timeval *);
83 static int xrtc_settime(struct todr_chip_handle *, struct timeval *);
86 * xrtc_match()
88 static int
89 xrtc_match(device_t parent, cfdata_t cf, void *arg)
91 struct i2c_attach_args *ia = arg;
93 /* match only this RTC devices */
94 if (ia->ia_addr == X1226_ADDR)
95 return (1);
97 return (0);
101 * xrtc_attach()
103 static void
104 xrtc_attach(device_t parent, device_t self, void *arg)
106 struct xrtc_softc *sc = device_private(self);
107 struct i2c_attach_args *ia = arg;
109 aprint_naive(": Real-time Clock/NVRAM\n");
110 aprint_normal(": Xicor X1226 Real-time Clock/NVRAM\n");
112 sc->sc_tag = ia->ia_tag;
113 sc->sc_address = ia->ia_addr;
114 sc->sc_dev = self;
115 sc->sc_open = 0;
116 sc->sc_todr.cookie = sc;
117 sc->sc_todr.todr_gettime = xrtc_gettime;
118 sc->sc_todr.todr_settime = xrtc_settime;
119 sc->sc_todr.todr_setwen = NULL;
121 todr_attach(&sc->sc_todr);
125 /*ARGSUSED*/
127 xrtc_open(dev_t dev, int flag, int fmt, struct lwp *l)
129 struct xrtc_softc *sc;
131 if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
132 return (ENXIO);
134 /* XXX: Locking */
136 if (sc->sc_open)
137 return (EBUSY);
139 sc->sc_open = 1;
140 return (0);
143 /*ARGSUSED*/
145 xrtc_close(dev_t dev, int flag, int fmt, struct lwp *l)
147 struct xrtc_softc *sc;
149 if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
150 return (ENXIO);
152 sc->sc_open = 0;
153 return (0);
156 /*ARGSUSED*/
158 xrtc_read(dev_t dev, struct uio *uio, int flags)
160 struct xrtc_softc *sc;
161 u_int8_t ch, cmdbuf[2];
162 int addr, error;
164 if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
165 return (ENXIO);
167 if (uio->uio_offset >= X1226_NVRAM_SIZE)
168 return (EINVAL);
170 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
171 return (error);
173 while (uio->uio_resid && uio->uio_offset < X1226_NVRAM_SIZE) {
174 addr = (int)uio->uio_offset + X1226_NVRAM_START;
175 cmdbuf[0] = (addr >> 8) && 0xff;
176 cmdbuf[1] = addr && 0xff;
177 if ((error = iic_exec(sc->sc_tag,
178 I2C_OP_READ_WITH_STOP,
179 sc->sc_address, cmdbuf, 2, &ch, 1, 0)) != 0) {
180 iic_release_bus(sc->sc_tag, 0);
181 aprint_error_dev(sc->sc_dev,
182 "xrtc_read: read failed at 0x%x\n",
183 (int)uio->uio_offset);
184 return (error);
186 if ((error = uiomove(&ch, 1, uio)) != 0) {
187 iic_release_bus(sc->sc_tag, 0);
188 return (error);
192 iic_release_bus(sc->sc_tag, 0);
194 return (0);
197 /*ARGSUSED*/
199 xrtc_write(dev_t dev, struct uio *uio, int flags)
201 struct xrtc_softc *sc;
202 u_int8_t cmdbuf[3];
203 int addr, error;
205 if ((sc = device_lookup_private(&xrtc_cd, minor(dev))) == NULL)
206 return (ENXIO);
208 if (uio->uio_offset >= X1226_NVRAM_SIZE)
209 return (EINVAL);
211 if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
212 return (error);
214 while (uio->uio_resid && uio->uio_offset < X1226_NVRAM_SIZE) {
215 addr = (int)uio->uio_offset + X1226_NVRAM_START;
216 cmdbuf[0] = (addr >> 8) && 0xff;
217 cmdbuf[1] = addr && 0xff;
218 if ((error = uiomove(&cmdbuf[2], 1, uio)) != 0) {
219 break;
221 if ((error = iic_exec(sc->sc_tag,
222 uio->uio_resid ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
223 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
224 iic_release_bus(sc->sc_tag, 0);
225 aprint_error_dev(sc->sc_dev,
226 "xrtc_write: write failed at 0x%x\n",
227 (int)uio->uio_offset);
228 return (error);
232 iic_release_bus(sc->sc_tag, 0);
234 return (0);
238 static int
239 xrtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
241 struct xrtc_softc *sc = ch->cookie;
242 struct clock_ymdhms dt, check;
243 int retries;
245 memset(&dt, 0, sizeof(dt));
246 memset(&check, 0, sizeof(check));
248 retries = 5;
249 do {
250 xrtc_clock_read(sc, &dt);
251 xrtc_clock_read(sc, &check);
252 } while (memcmp(&dt, &check, sizeof(check)) != 0 && --retries);
254 tv->tv_sec = clock_ymdhms_to_secs(&dt);
255 tv->tv_usec = 0;
257 return (0);
260 static int
261 xrtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
263 struct xrtc_softc *sc = ch->cookie;
264 struct clock_ymdhms dt;
266 clock_secs_to_ymdhms(tv->tv_sec, &dt);
268 if (xrtc_clock_write(sc, &dt) == 0)
269 return (-1);
271 return (0);
274 static int
275 xrtc_clock_read(struct xrtc_softc *sc, struct clock_ymdhms *dt)
277 int i = 0;
278 u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[2];
280 if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
281 aprint_error_dev(sc->sc_dev,
282 "xrtc_clock_read: failed to acquire I2C bus\n");
283 return (0);
286 /* Read each RTC register in order */
287 for (i = 0 ; i < X1226_REG_RTC_SIZE ; i++) {
288 int addr = i + X1226_REG_RTC_BASE;
289 cmdbuf[0] = (addr >> 8) & 0xff;
290 cmdbuf[1] = addr & 0xff;
292 if (iic_exec(sc->sc_tag,
293 I2C_OP_READ_WITH_STOP,
294 sc->sc_address, cmdbuf, 2,
295 &bcd[i], 1, I2C_F_POLL)) {
296 iic_release_bus(sc->sc_tag, I2C_F_POLL);
297 aprint_error_dev(sc->sc_dev,
298 "xrtc_clock_read: failed to read rtc "
299 "at 0x%x\n", i);
300 return (0);
304 /* Done with I2C */
305 iic_release_bus(sc->sc_tag, I2C_F_POLL);
308 * Convert the X1226's register bcd values
310 dt->dt_sec = FROMBCD(bcd[X1226_REG_SC - X1226_REG_RTC_BASE]
311 & X1226_REG_SC_MASK);
312 dt->dt_min = FROMBCD(bcd[X1226_REG_MN - X1226_REG_RTC_BASE]
313 & X1226_REG_MN_MASK);
314 if (!(bcd[X1226_REG_HR - X1226_REG_RTC_BASE] & X1226_FLAG_HR_24H)) {
315 dt->dt_hour = FROMBCD(bcd[X1226_REG_HR - X1226_REG_RTC_BASE]
316 & X1226_REG_HR12_MASK);
317 if (bcd[X1226_REG_HR - X1226_REG_RTC_BASE] & X1226_FLAG_HR_12HPM) {
318 dt->dt_hour += 12;
320 } else {
321 dt->dt_hour = FROMBCD(bcd[X1226_REG_HR - X1226_REG_RTC_BASE]
322 & X1226_REG_HR24_MASK);
324 dt->dt_wday = FROMBCD(bcd[X1226_REG_DW - X1226_REG_RTC_BASE]
325 & X1226_REG_DT_MASK);
326 dt->dt_day = FROMBCD(bcd[X1226_REG_DT - X1226_REG_RTC_BASE]
327 & X1226_REG_DT_MASK);
328 dt->dt_mon = FROMBCD(bcd[X1226_REG_MO - X1226_REG_RTC_BASE]
329 & X1226_REG_MO_MASK);
330 dt->dt_year = FROMBCD(bcd[X1226_REG_YR - X1226_REG_RTC_BASE]
331 & X1226_REG_YR_MASK);
332 dt->dt_year += FROMBCD(bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE]
333 & X1226_REG_Y2K_MASK) * 100;
335 return (1);
338 static int
339 xrtc_clock_write(struct xrtc_softc *sc, struct clock_ymdhms *dt)
341 int i = 0, addr;
342 u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[3];
345 * Convert our time to bcd values
347 bcd[X1226_REG_SC - X1226_REG_RTC_BASE] = TOBCD(dt->dt_sec);
348 bcd[X1226_REG_MN - X1226_REG_RTC_BASE] = TOBCD(dt->dt_min);
349 bcd[X1226_REG_HR - X1226_REG_RTC_BASE] = TOBCD(dt->dt_hour)
350 | X1226_FLAG_HR_24H;
351 bcd[X1226_REG_DW - X1226_REG_RTC_BASE] = TOBCD(dt->dt_wday);
352 bcd[X1226_REG_DT - X1226_REG_RTC_BASE] = TOBCD(dt->dt_day);
353 bcd[X1226_REG_MO - X1226_REG_RTC_BASE] = TOBCD(dt->dt_mon);
354 bcd[X1226_REG_YR - X1226_REG_RTC_BASE] = TOBCD(dt->dt_year % 100);
355 bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE] = TOBCD(dt->dt_year / 100);
357 if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
358 aprint_error_dev(sc->sc_dev,
359 "xrtc_clock_write: failed to acquire I2C bus\n");
360 return (0);
363 /* Unlock register: Write Enable Latch */
364 addr = X1226_REG_SR;
365 cmdbuf[0] = ((addr >> 8) & 0xff);
366 cmdbuf[1] = (addr & 0xff);
367 cmdbuf[2] = X1226_FLAG_SR_WEL;
368 if (iic_exec(sc->sc_tag,
369 I2C_OP_WRITE_WITH_STOP,
370 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) {
371 iic_release_bus(sc->sc_tag, I2C_F_POLL);
372 aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
373 "failed to write-unlock status register(WEL=1)\n");
374 return (0);
377 /* Unlock register: Register Write Enable Latch */
378 addr = X1226_REG_SR;
379 cmdbuf[0] = ((addr >> 8) & 0xff);
380 cmdbuf[1] = (addr & 0xff);
381 cmdbuf[2] = X1226_FLAG_SR_WEL | X1226_FLAG_SR_RWEL;
382 if (iic_exec(sc->sc_tag,
383 I2C_OP_WRITE_WITH_STOP,
384 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) {
385 iic_release_bus(sc->sc_tag, I2C_F_POLL);
386 aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
387 "failed to write-unlock status register(RWEL=1)\n");
388 return (0);
391 /* Write each RTC register in reverse order */
392 for (i = (X1226_REG_RTC_SIZE - 1) ; i >= 0; i--) {
393 addr = i + X1226_REG_RTC_BASE;
394 cmdbuf[0] = ((addr >> 8) & 0xff);
395 cmdbuf[1] = (addr & 0xff);
396 if (iic_exec(sc->sc_tag,
397 I2C_OP_WRITE_WITH_STOP,
398 sc->sc_address, cmdbuf, 2,
399 &bcd[i], 1, I2C_F_POLL)) {
401 /* Lock register: WEL/RWEL off */
402 addr = X1226_REG_SR;
403 cmdbuf[0] = ((addr >> 8) & 0xff);
404 cmdbuf[1] = (addr & 0xff);
405 cmdbuf[2] = 0;
406 iic_exec(sc->sc_tag,
407 I2C_OP_WRITE_WITH_STOP,
408 sc->sc_address, cmdbuf, 2,
409 &cmdbuf[2], 1, 0);
411 iic_release_bus(sc->sc_tag, I2C_F_POLL);
412 aprint_error_dev(sc->sc_dev, "xrtc_clock_write: failed to write rtc "
413 "at 0x%x\n", i);
414 return (0);
418 /* Lock register: WEL/RWEL off */
419 addr = X1226_REG_SR;
420 cmdbuf[0] = ((addr >> 8) & 0xff);
421 cmdbuf[1] = (addr & 0xff);
422 cmdbuf[2] = 0;
423 if (iic_exec(sc->sc_tag,
424 I2C_OP_WRITE_WITH_STOP,
425 sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) {
426 iic_release_bus(sc->sc_tag, I2C_F_POLL);
427 aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
428 "failed to write-lock status register\n");
429 return (0);
432 iic_release_bus(sc->sc_tag, I2C_F_POLL);
433 return (1);