xref: /freebsd/sys/dev/iicbus/controller/vybrid/vf_i2c_fdt.c (revision 44847114bd4c61ed3dfcc879fa0d7deb452e12d3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
5  * Copyright (c) 2024 Pierre-Luc Drouin <pldrouin@pldrouin.net>
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, 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 /*
30  * Vybrid Family Inter-Integrated Circuit (I2C)
31  * Originally based on Chapter 48, Vybrid Reference Manual, Rev. 5, 07/2013
32  * Currently based on Chapter 21, LX2160A Reference Manual, Rev. 1, 10/2021
33  *
34  * The current implementation is based on the original driver by Ruslan Bukin,
35  * later modified by Dawid Górecki, and split into FDT and ACPI drivers by Val
36  * Packett.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/malloc.h>
45 #include <sys/rman.h>
46 
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include <dev/iicbus/iicbus.h>
52 
53 #include <dev/iicbus/controller/vybrid/vf_i2c.h>
54 
55 static const struct ofw_compat_data vf_i2c_compat_data[] = {
56 	{"fsl,mvf600-i2c",    HW_MVF600},
57 	{"fsl,vf610-i2c",     HW_VF610},
58 	{NULL,                0}
59 };
60 
61 static int
vf_i2c_fdt_probe(device_t dev)62 vf_i2c_fdt_probe(device_t dev)
63 {
64 
65 	if (!ofw_bus_status_okay(dev))
66 		return (ENXIO);
67 
68 	if (ofw_bus_search_compatible(dev, vf_i2c_compat_data)->ocd_data == 0)
69 		return (ENXIO);
70 
71 	device_set_desc(dev, VF_I2C_DEVSTR);
72 	return (BUS_PROBE_DEFAULT);
73 }
74 
75 static int
vf_i2c_fdt_attach(device_t dev)76 vf_i2c_fdt_attach(device_t dev)
77 {
78 	struct vf_i2c_softc *sc;
79 	phandle_t node;
80 	int error;
81 
82 	sc = device_get_softc(dev);
83 	sc->dev = dev;
84 	sc->hwtype = ofw_bus_search_compatible(dev, vf_i2c_compat_data)->ocd_data;
85 	node = ofw_bus_get_node(dev);
86 
87 	error = clk_get_by_ofw_index(dev, node, 0, &sc->clock);
88 	if (error != 0) {
89 		sc->freq = 0;
90 		device_printf(dev, "Parent clock not found.\n");
91 	} else {
92 		if (OF_hasprop(node, "clock-frequency"))
93 			OF_getencprop(node, "clock-frequency", &sc->freq,
94 					sizeof(sc->freq));
95 		else
96 			sc->freq = VF_I2C_DEFAULT_BUS_SPEED;
97 	}
98 	return (vf_i2c_attach_common(dev));
99 }
100 
101 static phandle_t
vf_i2c_get_node(device_t bus,device_t dev)102 vf_i2c_get_node(device_t bus, device_t dev)
103 {
104 	return (ofw_bus_get_node(bus));
105 }
106 
107 static device_method_t vf_i2c_fdt_methods[] = {
108 	/* Device interface */
109 	DEVMETHOD(device_probe,                 vf_i2c_fdt_probe),
110 	DEVMETHOD(device_attach,                vf_i2c_fdt_attach),
111 
112 	/* ofw_bus interface */
113 	DEVMETHOD(ofw_bus_get_node,             vf_i2c_get_node),
114 	DEVMETHOD_END
115 };
116 
117 DEFINE_CLASS_1(vf_i2c_fdt, vf_i2c_fdt_driver, vf_i2c_fdt_methods,
118 		sizeof(struct vf_i2c_softc), vf_i2c_driver);
119 
120 DRIVER_MODULE(vf_i2c_fdt, simplebus, vf_i2c_fdt_driver, 0, 0);
121 DRIVER_MODULE(iicbus, vf_i2c_fdt, iicbus_driver, 0, 0);
122 DRIVER_MODULE(ofw_iicbus, vf_i2c_fdt, ofw_iicbus_driver, 0, 0);
123 
124