xref: /freebsd/sys/dev/pwm/ofw_pwmbus.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
29 #include <sys/bus.h>
30 #include <sys/kernel.h>
31 #include <sys/libkern.h>
32 #include <sys/lock.h>
33 #include <sys/module.h>
34 
35 #include <dev/fdt/fdt_common.h>
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/ofw_bus_subr.h>
38 #include <dev/ofw/openfirm.h>
39 #include <dev/pwm/pwmbus.h>
40 
41 #include "pwmbus_if.h"
42 
43 struct ofw_pwmbus_ivars {
44 	struct pwmbus_ivars	base;
45 	struct ofw_bus_devinfo	devinfo;
46 };
47 
48 struct ofw_pwmbus_softc {
49 	struct pwmbus_softc	base;
50 };
51 
52 /*
53  * bus_if methods...
54  */
55 
56 static device_t
57 ofw_pwmbus_add_child(device_t dev, u_int order, const char *name, int unit)
58 {
59 	device_t child;
60 	struct ofw_pwmbus_ivars *ivars;
61 
62 	if ((ivars = malloc(sizeof(struct ofw_pwmbus_ivars), M_DEVBUF,
63 	    M_NOWAIT | M_ZERO)) == NULL) {
64 		return (NULL);
65 	}
66 
67 	if ((child = device_add_child_ordered(dev, order, name, unit)) == NULL) {
68 		free(ivars, M_DEVBUF);
69 		return (NULL);
70 	}
71 
72 	ivars->devinfo.obd_node = -1;
73 	device_set_ivars(child, ivars);
74 
75 	return (child);
76 }
77 
78 static void
79 ofw_pwmbus_child_deleted(device_t dev, device_t child)
80 {
81 	struct ofw_pwmbus_ivars *ivars;
82 
83 	ivars = device_get_ivars(child);
84 	if (ivars != NULL) {
85 		ofw_bus_gen_destroy_devinfo(&ivars->devinfo);
86 		free(ivars, M_DEVBUF);
87 	}
88 }
89 
90 static const struct ofw_bus_devinfo *
91 ofw_pwmbus_get_devinfo(device_t bus, device_t dev)
92 {
93 	struct ofw_pwmbus_ivars *ivars;
94 
95 	ivars = device_get_ivars(dev);
96 	return (&ivars->devinfo);
97 }
98 
99 /*
100  * device_if methods...
101  */
102 
103 static int
104 ofw_pwmbus_probe(device_t dev)
105 {
106 
107 	if (ofw_bus_get_node(dev) == -1) {
108 		return (ENXIO);
109 	}
110 	device_set_desc(dev, "OFW PWM bus");
111 
112 	return (BUS_PROBE_DEFAULT);
113 }
114 
115 static int
116 ofw_pwmbus_attach(device_t dev)
117 {
118 	struct ofw_pwmbus_softc *sc;
119 	struct ofw_pwmbus_ivars *ivars;
120 	phandle_t node;
121 	device_t child, parent;
122 	pcell_t  chan;
123 	bool any_children;
124 
125 	sc = device_get_softc(dev);
126 	sc->base.dev = dev;
127 	parent = device_get_parent(dev);
128 
129 	if (PWMBUS_CHANNEL_COUNT(parent, &sc->base.nchannels) != 0 ||
130 	    sc->base.nchannels == 0) {
131 		device_printf(dev, "No channels on parent %s\n",
132 		    device_get_nameunit(parent));
133 		return (ENXIO);
134 	}
135 
136 	/*
137 	 * Attach the children found in the fdt node of the hardware controller.
138 	 * Hardware controllers must implement the ofw_bus_get_node method so
139 	 * that our call to ofw_bus_get_node() gets back the controller's node.
140 	 */
141 	any_children = false;
142 	node = ofw_bus_get_node(dev);
143 	for (node = OF_child(node); node != 0; node = OF_peer(node)) {
144 		/*
145 		 * The child has to have a reg property; its value is the
146 		 * channel number so range-check it.
147 		 */
148 		if (OF_getencprop(node, "reg", &chan, sizeof(chan)) == -1)
149 			continue;
150 		if (chan >= sc->base.nchannels)
151 			continue;
152 
153 		if ((child = ofw_pwmbus_add_child(dev, 0, NULL, -1)) == NULL)
154 			continue;
155 
156 		ivars = device_get_ivars(child);
157 		ivars->base.pi_channel = chan;
158 
159 		/* Set up the standard ofw devinfo. */
160 		if (ofw_bus_gen_setup_devinfo(&ivars->devinfo, node) != 0) {
161 			device_delete_child(dev, child);
162 			continue;
163 		}
164 		any_children = true;
165 	}
166 
167 	/*
168 	 * If we didn't find any children in the fdt data, add a pwmc(4) child
169 	 * for each channel, like the base pwmbus does.  The idea is that if
170 	 * there is any fdt data, then we do exactly what it says and nothing
171 	 * more, otherwise we just provide generic userland access to all the
172 	 * pwm channels that exist like the base pwmbus's attach code does.
173 	 */
174 	if (!any_children) {
175 		for (chan = 0; chan < sc->base.nchannels; ++chan) {
176 			child = ofw_pwmbus_add_child(dev, 0, "pwmc", -1);
177 			if (child == NULL) {
178 				device_printf(dev, "failed to add pwmc child "
179 				    " device for channel %u\n", chan);
180 				continue;
181 			}
182 			ivars = device_get_ivars(child);
183 			ivars->base.pi_channel = chan;
184 		}
185 	}
186 	bus_enumerate_hinted_children(dev);
187 	bus_generic_probe(dev);
188 
189 	return (bus_generic_attach(dev));
190 }
191 
192 static device_method_t ofw_pwmbus_methods[] = {
193 	/* Device interface */
194 	DEVMETHOD(device_probe,           ofw_pwmbus_probe),
195 	DEVMETHOD(device_attach,          ofw_pwmbus_attach),
196 
197 	/* Bus interface */
198 	DEVMETHOD(bus_child_pnpinfo,	  ofw_bus_gen_child_pnpinfo),
199 	DEVMETHOD(bus_add_child,          ofw_pwmbus_add_child),
200 	DEVMETHOD(bus_child_deleted,      ofw_pwmbus_child_deleted),
201 
202 	/* ofw_bus interface */
203 	DEVMETHOD(ofw_bus_get_devinfo,    ofw_pwmbus_get_devinfo),
204 	DEVMETHOD(ofw_bus_get_compat,     ofw_bus_gen_get_compat),
205 	DEVMETHOD(ofw_bus_get_model,      ofw_bus_gen_get_model),
206 	DEVMETHOD(ofw_bus_get_name,       ofw_bus_gen_get_name),
207 	DEVMETHOD(ofw_bus_get_node,       ofw_bus_gen_get_node),
208 	DEVMETHOD(ofw_bus_get_type,       ofw_bus_gen_get_type),
209 
210 	DEVMETHOD_END
211 };
212 
213 DEFINE_CLASS_1(pwmbus, ofw_pwmbus_driver, ofw_pwmbus_methods,
214     sizeof(struct pwmbus_softc), pwmbus_driver);
215 EARLY_DRIVER_MODULE(ofw_pwmbus, pwm, ofw_pwmbus_driver, 0, 0,
216     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
217 MODULE_VERSION(ofw_pwmbus, 1);
218 MODULE_DEPEND(ofw_pwmbus, pwmbus, 1, 1, 1);
219