1 /* $NetBSD: imx_i2c.c,v 1.3 2022/07/22 23:43:24 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2019 Genetec Corporation. All rights reserved.
5 * Written by Hashimoto Kenichi for Genetec Corporation.
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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: imx_i2c.c,v 1.3 2022/07/22 23:43:24 thorpej Exp $");
31
32 #include <sys/bus.h>
33
34 #include <arm/imx/imxi2cvar.h>
35
36 #include <dev/fdt/fdtvar.h>
37
38 static const struct device_compatible_entry compat_data[] = {
39 { .compat = "fsl,imx21-i2c" },
40 DEVICE_COMPAT_EOL
41 };
42
43 int
imxi2c_match(device_t parent,cfdata_t cf,void * aux)44 imxi2c_match(device_t parent, cfdata_t cf, void *aux)
45 {
46 struct fdt_attach_args * const faa = aux;
47
48 return of_compatible_match(faa->faa_phandle, compat_data);
49 }
50
51 void
imxi2c_attach(device_t parent __unused,device_t self,void * aux)52 imxi2c_attach(device_t parent __unused, device_t self, void *aux)
53 {
54 struct imxi2c_softc *imxsc = device_private(self);
55 struct fdt_attach_args * const faa = aux;
56 const int phandle = faa->faa_phandle;
57 bus_space_tag_t bst = faa->faa_bst;
58 bus_addr_t addr;
59 bus_size_t size;
60 int error;
61
62 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
63 aprint_error(": couldn't get registers\n");
64 return;
65 }
66
67 imxsc->sc_clk = fdtbus_clock_get_index(phandle, 0);
68 if (imxsc->sc_clk == NULL) {
69 aprint_error(": couldn't get clock\n");
70 return;
71 }
72
73 error = clk_enable(imxsc->sc_clk);
74 if (error) {
75 aprint_error_dev(self, "couldn't enable: %d\n", error);
76 return;
77 }
78
79 u_int freq;
80 error = of_getprop_uint32(phandle, "clock-frequency", &freq);
81 if (error)
82 freq = 100000;
83
84 imxsc->sc_motoi2c.sc_phandle = phandle;
85 imxi2c_attach_common(self, bst, addr, size,
86 clk_get_rate(imxsc->sc_clk), freq);
87 }
88