dwiic_fdt.c revision 1.3 1 /* $NetBSD: dwiic_fdt.c,v 1.3 2021/01/15 22:35:39 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Manuel Bouyer.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * Synopsys DesignWare I2C controller, FDT front-end
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: dwiic_fdt.c,v 1.3 2021/01/15 22:35:39 jmcneill Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40
41 #include <dev/fdt/fdtvar.h>
42 #include <dev/ic/dwiic_var.h>
43
44 static const char * const compatible[] = {
45 "snps,designware-i2c",
46 NULL
47 };
48
49 struct dwiic_fdt_softc {
50 struct dwiic_softc sc_dwiic;
51 };
52
53 static int dwiic_fdt_match(device_t, cfdata_t, void *);
54 static void dwiic_fdt_attach(device_t, device_t, void *);
55
56 CFATTACH_DECL_NEW(dwiic_fdt, sizeof(struct dwiic_fdt_softc),
57 dwiic_fdt_match, dwiic_fdt_attach, dwiic_detach, NULL);
58
59 int
60 dwiic_fdt_match(device_t parent, cfdata_t match, void *aux)
61 {
62 struct fdt_attach_args * const faa = aux;
63
64 return of_match_compatible(faa->faa_phandle, compatible);
65 }
66
67 void
68 dwiic_fdt_attach(device_t parent, device_t self, void *aux)
69 {
70 struct dwiic_fdt_softc * const sc = device_private(self);
71 struct fdt_attach_args * const faa = aux;
72 const int phandle = faa->faa_phandle;
73 bus_addr_t addr;
74 bus_size_t size;
75 char intrstr[128];
76
77 sc->sc_dwiic.sc_dev = self;
78 sc->sc_dwiic.sc_power = NULL;
79 sc->sc_dwiic.sc_type = dwiic_type_generic;
80
81 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
82 aprint_error(": couldn't get registers\n");
83 return;
84 }
85
86 sc->sc_dwiic.sc_iot = faa->faa_bst;
87 if (bus_space_map(sc->sc_dwiic.sc_iot, addr, size, 0, &sc->sc_dwiic.sc_ioh) != 0) {
88 aprint_error(": couldn't map registers\n");
89 return;
90 }
91
92 aprint_naive(": I2C controller\n");
93 aprint_normal(": I2C controller\n");
94
95 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
96 aprint_error_dev(self, "failed to decode interrupt\n");
97 return;
98 }
99 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
100
101 sc->sc_dwiic.sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
102 dwiic_intr, &sc->sc_dwiic, device_xname(self));
103 if (sc->sc_dwiic.sc_ih == NULL) {
104 aprint_error_dev(self, "couldn't establish interrupt\n");
105 goto out;
106 }
107
108 dwiic_attach(&sc->sc_dwiic);
109
110 pmf_device_register(self, dwiic_suspend, dwiic_resume);
111
112 fdtbus_register_i2c_controller(&sc->sc_dwiic.sc_i2c_tag, phandle);
113
114 fdtbus_attach_i2cbus(self, phandle, &sc->sc_dwiic.sc_i2c_tag, iicbus_print);
115
116 out:
117 return;
118 }
119