xref: /freebsd/sys/dev/pwm/ofw_pwmbus.c (revision 559de8eedbc5cc18982bb254527ed7fe2f8655b2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Ian Lepore <ian@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/libkern.h>
35 #include <sys/lock.h>
36 #include <sys/module.h>
37 
38 #include <dev/fdt/fdt_common.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/ofw/openfirm.h>
42 #include <dev/pwm/pwmbus.h>
43 
44 #include "pwmbus_if.h"
45 
46 struct ofw_pwmbus_ivars {
47 	struct pwmbus_ivars	base;
48 	struct ofw_bus_devinfo	devinfo;
49 };
50 
51 struct ofw_pwmbus_softc {
52 	struct pwmbus_softc	base;
53 };
54 
55 /*
56  * bus_if methods...
57  */
58 
59 static device_t
60 ofw_pwmbus_add_child(device_t dev, u_int order, const char *name, int unit)
61 {
62 	device_t child;
63 	struct ofw_pwmbus_ivars *ivars;
64 
65 	if ((ivars = malloc(sizeof(struct ofw_pwmbus_ivars), M_DEVBUF,
66 	    M_NOWAIT | M_ZERO)) == NULL) {
67 		return (NULL);
68 	}
69 
70 	if ((child = device_add_child_ordered(dev, order, name, unit)) == NULL) {
71 		free(ivars, M_DEVBUF);
72 		return (NULL);
73 	}
74 
75 	ivars->devinfo.obd_node = -1;
76 	device_set_ivars(child, ivars);
77 
78 	return (child);
79 }
80 
81 static void
82 ofw_pwmbus_child_deleted(device_t dev, device_t child)
83 {
84 	struct ofw_pwmbus_ivars *ivars;
85 
86 	ivars = device_get_ivars(child);
87 	if (ivars != NULL) {
88 		ofw_bus_gen_destroy_devinfo(&ivars->devinfo);
89 		free(ivars, M_DEVBUF);
90 	}
91 }
92 
93 static const struct ofw_bus_devinfo *
94 ofw_pwmbus_get_devinfo(device_t bus, device_t dev)
95 {
96 	struct ofw_pwmbus_ivars *ivars;
97 
98 	ivars = device_get_ivars(dev);
99 	return (&ivars->devinfo);
100 }
101 
102 /*
103  * device_if methods...
104  */
105 
106 static int
107 ofw_pwmbus_probe(device_t dev)
108 {
109 
110 	if (ofw_bus_get_node(dev) == -1) {
111 		return (ENXIO);
112 	}
113 	device_set_desc(dev, "OFW PWM bus");
114 
115 	return (BUS_PROBE_DEFAULT);
116 }
117 
118 static int
119 ofw_pwmbus_attach(device_t dev)
120 {
121 	struct ofw_pwmbus_softc *sc;
122 	struct ofw_pwmbus_ivars *ivars;
123 	phandle_t node;
124 	device_t child, parent;
125 	pcell_t  chan;
126 	bool any_children;
127 
128 	sc = device_get_softc(dev);
129 	sc->base.dev = dev;
130 	parent = device_get_parent(dev);
131 
132 	if (PWMBUS_CHANNEL_COUNT(parent, &sc->base.nchannels) != 0 ||
133 	    sc->base.nchannels == 0) {
134 		device_printf(dev, "No channels on parent %s\n",
135 		    device_get_nameunit(parent));
136 		return (ENXIO);
137 	}
138 
139 	/*
140 	 * Attach the children found in the fdt node of the hardware controller.
141 	 * Hardware controllers must implement the ofw_bus_get_node method so
142 	 * that our call to ofw_bus_get_node() gets back the controller's node.
143 	 */
144 	any_children = false;
145 	node = ofw_bus_get_node(dev);
146 	for (node = OF_child(node); node != 0; node = OF_peer(node)) {
147 		/*
148 		 * The child has to have a reg property; its value is the
149 		 * channel number so range-check it.
150 		 */
151 		if (OF_getencprop(node, "reg", &chan, sizeof(chan)) == -1)
152 			continue;
153 		if (chan >= sc->base.nchannels)
154 			continue;
155 
156 		if ((child = ofw_pwmbus_add_child(dev, 0, NULL, -1)) == NULL)
157 			continue;
158 
159 		ivars = device_get_ivars(child);
160 		ivars->base.pi_channel = chan;
161 
162 		/* Set up the standard ofw devinfo. */
163 		if (ofw_bus_gen_setup_devinfo(&ivars->devinfo, node) != 0) {
164 			device_delete_child(dev, child);
165 			continue;
166 		}
167 		any_children = true;
168 	}
169 
170 	/*
171 	 * If we didn't find any children in the fdt data, add a pwmc(4) child
172 	 * for each channel, like the base pwmbus does.  The idea is that if
173 	 * there is any fdt data, then we do exactly what it says and nothing
174 	 * more, otherwise we just provide generic userland access to all the
175 	 * pwm channels that exist like the base pwmbus's attach code does.
176 	 */
177 	if (!any_children) {
178 		for (chan = 0; chan < sc->base.nchannels; ++chan) {
179 			child = ofw_pwmbus_add_child(dev, 0, "pwmc", -1);
180 			if (child == NULL) {
181 				device_printf(dev, "failed to add pwmc child "
182 				    " device for channel %u\n", chan);
183 				continue;
184 			}
185 			ivars = device_get_ivars(child);
186 			ivars->base.pi_channel = chan;
187 		}
188 	}
189 	bus_enumerate_hinted_children(dev);
190 	bus_generic_probe(dev);
191 
192 	return (bus_generic_attach(dev));
193 }
194 
195 static device_method_t ofw_pwmbus_methods[] = {
196 	/* Device interface */
197 	DEVMETHOD(device_probe,           ofw_pwmbus_probe),
198 	DEVMETHOD(device_attach,          ofw_pwmbus_attach),
199 
200 	/* Bus interface */
201 	DEVMETHOD(bus_child_pnpinfo,	  ofw_bus_gen_child_pnpinfo),
202 	DEVMETHOD(bus_add_child,          ofw_pwmbus_add_child),
203 	DEVMETHOD(bus_child_deleted,      ofw_pwmbus_child_deleted),
204 
205 	/* ofw_bus interface */
206 	DEVMETHOD(ofw_bus_get_devinfo,    ofw_pwmbus_get_devinfo),
207 	DEVMETHOD(ofw_bus_get_compat,     ofw_bus_gen_get_compat),
208 	DEVMETHOD(ofw_bus_get_model,      ofw_bus_gen_get_model),
209 	DEVMETHOD(ofw_bus_get_name,       ofw_bus_gen_get_name),
210 	DEVMETHOD(ofw_bus_get_node,       ofw_bus_gen_get_node),
211 	DEVMETHOD(ofw_bus_get_type,       ofw_bus_gen_get_type),
212 
213 	DEVMETHOD_END
214 };
215 
216 DEFINE_CLASS_1(pwmbus, ofw_pwmbus_driver, ofw_pwmbus_methods,
217     sizeof(struct pwmbus_softc), pwmbus_driver);
218 EARLY_DRIVER_MODULE(ofw_pwmbus, pwm, ofw_pwmbus_driver, 0, 0,
219     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
220 MODULE_VERSION(ofw_pwmbus, 1);
221 MODULE_DEPEND(ofw_pwmbus, pwmbus, 1, 1, 1);
222