xref: /netbsd-src/sys/dev/fdt/dwiic_fdt.c (revision d90047b5d07facf36e6c01dcc0bded8997ce9cc2)
1 /* $NetBSD: dwiic_fdt.c,v 1.1 2018/09/26 19:06:33 jakllsch 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.1 2018/09/26 19:06:33 jakllsch 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 static i2c_tag_t dwiic_fdt_i2c_get_tag(device_t);
56 
57 struct fdtbus_i2c_controller_func dwiic_fdt_i2c_funcs = {
58 	.get_tag = dwiic_fdt_i2c_get_tag,
59 };
60 
61 CFATTACH_DECL_NEW(dwiic_fdt, sizeof(struct dwiic_fdt_softc),
62     dwiic_fdt_match, dwiic_fdt_attach, dwiic_detach, NULL);
63 
64 int
65 dwiic_fdt_match(device_t parent, cfdata_t match, void *aux)
66 {
67 	struct fdt_attach_args * const faa = aux;
68 
69 	return of_match_compatible(faa->faa_phandle, compatible);
70 }
71 
72 void
73 dwiic_fdt_attach(device_t parent, device_t self, void *aux)
74 {
75 	struct dwiic_fdt_softc * const sc = device_private(self);
76 	struct fdt_attach_args * const faa = aux;
77 	const int phandle = faa->faa_phandle;
78 	bus_addr_t addr;
79 	bus_size_t size;
80 	char intrstr[128];
81 
82 	sc->sc_dwiic.sc_dev = self;
83 	sc->sc_dwiic.sc_power = NULL;
84 	sc->sc_dwiic.sc_type = dwiic_type_generic;
85 
86 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
87 		aprint_error(": couldn't get registers\n");
88 		return;
89 	}
90 
91 	sc->sc_dwiic.sc_iot = faa->faa_bst;
92 	if (bus_space_map(sc->sc_dwiic.sc_iot, addr, size, 0, &sc->sc_dwiic.sc_ioh) != 0) {
93 		aprint_error(": couldn't map registers\n");
94 		return;
95 	}
96 
97 	aprint_naive(": I2C controller\n");
98 	aprint_normal(": I2C controller\n");
99 
100 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
101 		aprint_error_dev(self, "failed to decode interrupt\n");
102 		return;
103 	}
104 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
105 
106 	sc->sc_dwiic.sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM, 0,
107 		dwiic_intr, &sc->sc_dwiic);
108 	if (sc->sc_dwiic.sc_ih == NULL) {
109 		aprint_error_dev(self, "couldn't establish interrupt\n");
110 		goto out;
111 	}
112 
113 	dwiic_attach(&sc->sc_dwiic);
114 
115 	pmf_device_register(self, dwiic_suspend, dwiic_resume);
116 
117 	fdtbus_register_i2c_controller(self, phandle, &dwiic_fdt_i2c_funcs);
118 
119 	fdtbus_attach_i2cbus(self, phandle, &sc->sc_dwiic.sc_i2c_tag, iicbus_print);
120 
121 out:
122 	return;
123 }
124 
125 static i2c_tag_t
126 dwiic_fdt_i2c_get_tag(device_t dev)
127 {
128 	struct dwiic_fdt_softc * const sc = device_private(dev);
129 
130 	return &sc->sc_dwiic.sc_i2c_tag;
131 }
132