2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Ian Lepore <ian@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/libkern.h>
36 #include <sys/module.h>
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>
44 #include "pwmbus_if.h"
46 struct ofw_pwmbus_ivars {
47 struct pwmbus_ivars base;
48 struct ofw_bus_devinfo devinfo;
51 struct ofw_pwmbus_softc {
52 struct pwmbus_softc base;
60 ofw_pwmbus_add_child(device_t dev, u_int order, const char *name, int unit)
63 struct ofw_pwmbus_ivars *ivars;
65 if ((ivars = malloc(sizeof(struct ofw_pwmbus_ivars), M_DEVBUF,
66 M_NOWAIT | M_ZERO)) == NULL) {
70 if ((child = device_add_child_ordered(dev, order, name, unit)) == NULL) {
71 free(ivars, M_DEVBUF);
75 ivars->devinfo.obd_node = -1;
76 device_set_ivars(child, ivars);
82 ofw_pwmbus_child_deleted(device_t dev, device_t child)
84 struct ofw_pwmbus_ivars *ivars;
86 ivars = device_get_ivars(child);
88 ofw_bus_gen_destroy_devinfo(&ivars->devinfo);
89 free(ivars, M_DEVBUF);
93 static const struct ofw_bus_devinfo *
94 ofw_pwmbus_get_devinfo(device_t bus, device_t dev)
96 struct ofw_pwmbus_ivars *ivars;
98 ivars = device_get_ivars(dev);
99 return (&ivars->devinfo);
103 * device_if methods...
107 ofw_pwmbus_probe(device_t dev)
110 if (ofw_bus_get_node(dev) == -1) {
113 device_set_desc(dev, "OFW PWM bus");
115 return (BUS_PROBE_DEFAULT);
119 ofw_pwmbus_attach(device_t dev)
121 struct ofw_pwmbus_softc *sc;
122 struct ofw_pwmbus_ivars *ivars;
124 device_t child, parent;
128 sc = device_get_softc(dev);
130 parent = device_get_parent(dev);
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));
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.
144 any_children = false;
145 node = ofw_bus_get_node(dev);
146 for (node = OF_child(node); node != 0; node = OF_peer(node)) {
148 * The child has to have a reg property; its value is the
149 * channel number so range-check it.
151 if (OF_getencprop(node, "reg", &chan, sizeof(chan)) == -1)
153 if (chan >= sc->base.nchannels)
156 if ((child = ofw_pwmbus_add_child(dev, 0, NULL, -1)) == NULL)
159 ivars = device_get_ivars(child);
160 ivars->base.pi_channel = chan;
162 /* Set up the standard ofw devinfo. */
163 if (ofw_bus_gen_setup_devinfo(&ivars->devinfo, node) != 0) {
164 device_delete_child(dev, child);
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.
178 for (chan = 0; chan < sc->base.nchannels; ++chan) {
179 child = ofw_pwmbus_add_child(dev, 0, "pwmc", -1);
181 device_printf(dev, "failed to add pwmc child "
182 " device for channel %u\n", chan);
185 ivars = device_get_ivars(child);
186 ivars->base.pi_channel = chan;
189 bus_enumerate_hinted_children(dev);
190 bus_generic_probe(dev);
192 return (bus_generic_attach(dev));
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),
201 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
202 DEVMETHOD(bus_add_child, ofw_pwmbus_add_child),
203 DEVMETHOD(bus_child_deleted, ofw_pwmbus_child_deleted),
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),
216 devclass_t ofw_pwmbus_devclass;
218 DEFINE_CLASS_1(pwmbus, ofw_pwmbus_driver, ofw_pwmbus_methods,
219 sizeof(struct pwmbus_softc), pwmbus_driver);
220 EARLY_DRIVER_MODULE(ofw_pwmbus, pwm, ofw_pwmbus_driver, ofw_pwmbus_devclass,
221 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
222 MODULE_VERSION(ofw_pwmbus, 1);
223 MODULE_DEPEND(ofw_pwmbus, pwmbus, 1, 1, 1);