Sat Oct 7 20:18:16 2017 UTC ()
Add FDT support


(jmcneill)
diff -r1.7 -r1.8 src/sys/dev/i2c/pcf8563.c

cvs diff -r1.7 -r1.8 src/sys/dev/i2c/pcf8563.c (expand / switch to unified diff)

--- src/sys/dev/i2c/pcf8563.c 2015/04/11 20:05:44 1.7
+++ src/sys/dev/i2c/pcf8563.c 2017/10/07 20:18:16 1.8
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: pcf8563.c,v 1.7 2015/04/11 20:05:44 joerg Exp $ */ 1/* $NetBSD: pcf8563.c,v 1.8 2017/10/07 20:18:16 jmcneill Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2011 Jonathan A. Kollasch 4 * Copyright (c) 2011 Jonathan A. Kollasch
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -16,66 +16,80 @@ @@ -16,66 +16,80 @@
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 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 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 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, 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 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 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
 29/* XXX */
 30#if defined(__arm__) || defined(__aarch64__)
 31#include "opt_fdt.h"
 32#endif
 33
29#include <sys/cdefs.h> 34#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: pcf8563.c,v 1.7 2015/04/11 20:05:44 joerg Exp $"); 35__KERNEL_RCSID(0, "$NetBSD: pcf8563.c,v 1.8 2017/10/07 20:18:16 jmcneill Exp $");
31 36
32#include <sys/param.h> 37#include <sys/param.h>
33#include <sys/systm.h> 38#include <sys/systm.h>
34#include <sys/device.h> 39#include <sys/device.h>
35#include <sys/kernel.h> 40#include <sys/kernel.h>
36 41
37#include <dev/clock_subr.h> 42#include <dev/clock_subr.h>
38 43
39#include <dev/i2c/i2cvar.h> 44#include <dev/i2c/i2cvar.h>
40#include <dev/i2c/pcf8563reg.h> 45#include <dev/i2c/pcf8563reg.h>
41 46
 47#ifdef FDT
 48#include <dev/fdt/fdtvar.h>
 49#endif
 50
 51static const char *compatible[] = {
 52 "nxp,pcf8563",
 53 "pcf8563rtc",
 54 NULL
 55};
 56
42struct pcf8563rtc_softc { 57struct pcf8563rtc_softc {
43 device_t sc_dev; 58 device_t sc_dev;
44 i2c_tag_t sc_tag; 59 i2c_tag_t sc_tag;
45 int sc_addr; 60 int sc_addr;
46 struct todr_chip_handle sc_todr; 61 struct todr_chip_handle sc_todr;
47}; 62};
48 63
49static int pcf8563rtc_match(device_t, cfdata_t, void *); 64static int pcf8563rtc_match(device_t, cfdata_t, void *);
50static void pcf8563rtc_attach(device_t, device_t, void *); 65static void pcf8563rtc_attach(device_t, device_t, void *);
51 66
52CFATTACH_DECL_NEW(pcf8563rtc, sizeof(struct pcf8563rtc_softc), 67CFATTACH_DECL_NEW(pcf8563rtc, sizeof(struct pcf8563rtc_softc),
53 pcf8563rtc_match, pcf8563rtc_attach, NULL, NULL); 68 pcf8563rtc_match, pcf8563rtc_attach, NULL, NULL);
54 69
55static int pcf8563rtc_clock_read(struct pcf8563rtc_softc *, struct clock_ymdhms *); 70static int pcf8563rtc_clock_read(struct pcf8563rtc_softc *, struct clock_ymdhms *);
56static int pcf8563rtc_clock_write(struct pcf8563rtc_softc *, struct clock_ymdhms *); 71static int pcf8563rtc_clock_write(struct pcf8563rtc_softc *, struct clock_ymdhms *);
57static int pcf8563rtc_gettime(struct todr_chip_handle *, struct clock_ymdhms *); 72static int pcf8563rtc_gettime(struct todr_chip_handle *, struct clock_ymdhms *);
58static int pcf8563rtc_settime(struct todr_chip_handle *, struct clock_ymdhms *); 73static int pcf8563rtc_settime(struct todr_chip_handle *, struct clock_ymdhms *);
59 74
60static int 75static int
61pcf8563rtc_match(device_t parent, cfdata_t cf, void *aux) 76pcf8563rtc_match(device_t parent, cfdata_t cf, void *aux)
62{ 77{
63 struct i2c_attach_args *ia = aux; 78 struct i2c_attach_args *ia = aux;
64 79
65 if (ia->ia_name) { 80 if (ia->ia_name) {
66 /* direct config - check name */ 81 /* direct config - check name */
67 if (strcmp(ia->ia_name, "pcf8563rtc") == 0) 82 return iic_compat_match(ia, compatible);
68 return 1; 
69 } else { 83 } else {
70 /* indirect config - check typical address */ 84 /* indirect config - check typical address */
71 if (ia->ia_addr == PCF8563_ADDR) 85 if (ia->ia_addr == PCF8563_ADDR)
72 return 1; 86 return 1;
73 } 87 }
74 return 0; 88 return 0;
75} 89}
76 90
77static void 91static void
78pcf8563rtc_attach(device_t parent, device_t self, void *aux) 92pcf8563rtc_attach(device_t parent, device_t self, void *aux)
79{ 93{
80 struct pcf8563rtc_softc *sc = device_private(self); 94 struct pcf8563rtc_softc *sc = device_private(self);
81 struct i2c_attach_args *ia = aux; 95 struct i2c_attach_args *ia = aux;
@@ -88,27 +102,31 @@ pcf8563rtc_attach(device_t parent, devic @@ -88,27 +102,31 @@ pcf8563rtc_attach(device_t parent, devic
88 sc->sc_addr = ia->ia_addr; 102 sc->sc_addr = ia->ia_addr;
89 sc->sc_todr.cookie = sc; 103 sc->sc_todr.cookie = sc;
90 sc->sc_todr.todr_gettime_ymdhms = pcf8563rtc_gettime; 104 sc->sc_todr.todr_gettime_ymdhms = pcf8563rtc_gettime;
91 sc->sc_todr.todr_settime_ymdhms = pcf8563rtc_settime; 105 sc->sc_todr.todr_settime_ymdhms = pcf8563rtc_settime;
92 sc->sc_todr.todr_setwen = NULL; 106 sc->sc_todr.todr_setwen = NULL;
93 107
94 iic_acquire_bus(sc->sc_tag, I2C_F_POLL); 108 iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
95 iic_smbus_write_byte(sc->sc_tag, sc->sc_addr, PCF8563_R_CS1, 0, 109 iic_smbus_write_byte(sc->sc_tag, sc->sc_addr, PCF8563_R_CS1, 0,
96 I2C_F_POLL); 110 I2C_F_POLL);
97 iic_smbus_write_byte(sc->sc_tag, sc->sc_addr, PCF8563_R_CS2, 0, 111 iic_smbus_write_byte(sc->sc_tag, sc->sc_addr, PCF8563_R_CS2, 0,
98 I2C_F_POLL); 112 I2C_F_POLL);
99 iic_release_bus(sc->sc_tag, I2C_F_POLL); 113 iic_release_bus(sc->sc_tag, I2C_F_POLL);
100 114
 115#ifdef FDT
 116 fdtbus_todr_attach(self, ia->ia_cookie, &sc->sc_todr);
 117#else
101 todr_attach(&sc->sc_todr); 118 todr_attach(&sc->sc_todr);
 119#endif
102} 120}
103 121
104static int 122static int
105pcf8563rtc_gettime(struct todr_chip_handle *ch, struct clock_ymdhms *dt) 123pcf8563rtc_gettime(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
106{ 124{
107 struct pcf8563rtc_softc *sc = ch->cookie; 125 struct pcf8563rtc_softc *sc = ch->cookie;
108  126
109 if (pcf8563rtc_clock_read(sc, dt) == 0) 127 if (pcf8563rtc_clock_read(sc, dt) == 0)
110 return -1; 128 return -1;
111 129
112 return 0; 130 return 0;
113} 131}
114 132