2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_platform.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <machine/bus.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/pwm/pwmbus.h>
49 #include "pwmbus_if.h"
52 struct pwmbus_channel_data {
65 pwmbus_attach_bus(device_t dev)
72 busdev = device_add_child(dev, "pwmbus", -1);
74 device_printf(dev, "Cannot add child pwmbus\n");
77 if (device_add_child(dev, "pwmc", -1) == NULL) {
78 device_printf(dev, "Cannot add pwmc\n");
79 device_delete_child(dev, busdev);
84 node = ofw_bus_get_node(dev);
85 OF_device_register_xref(OF_xref_from_node(node), dev);
88 bus_generic_attach(dev);
94 pwmbus_probe(device_t dev)
97 device_set_desc(dev, "PWM bus");
98 return (BUS_PROBE_GENERIC);
102 pwmbus_attach(device_t dev)
104 struct pwmbus_softc *sc;
106 sc = device_get_softc(dev);
108 sc->dev = device_get_parent(dev);
110 if (PWM_CHANNEL_MAX(sc->dev, &sc->nchannels) != 0 ||
115 device_printf(dev, "Registering %d channel(s)\n", sc->nchannels);
116 bus_generic_probe(dev);
118 return (bus_generic_attach(dev));
122 pwmbus_detach(device_t dev)
127 rv = bus_generic_detach(dev);
131 rv = device_get_children(dev, &devlist, &ndevs);
134 for (i = 0; i < ndevs; i++)
135 device_delete_child(dev, devlist[i]);
141 pwmbus_channel_config(device_t bus, int channel, unsigned int period, unsigned int duty)
143 struct pwmbus_softc *sc;
145 sc = device_get_softc(bus);
147 if (channel > sc->nchannels)
150 return (PWM_CHANNEL_CONFIG(sc->dev, channel, period, duty));
154 pwmbus_channel_get_config(device_t bus, int channel, unsigned int *period, unsigned int *duty)
156 struct pwmbus_softc *sc;
158 sc = device_get_softc(bus);
160 if (channel > sc->nchannels)
163 return (PWM_CHANNEL_GET_CONFIG(sc->dev, channel, period, duty));
167 pwmbus_channel_set_flags(device_t bus, int channel, uint32_t flags)
169 struct pwmbus_softc *sc;
171 sc = device_get_softc(bus);
173 if (channel > sc->nchannels)
176 return (PWM_CHANNEL_SET_FLAGS(sc->dev, channel, flags));
180 pwmbus_channel_get_flags(device_t bus, int channel, uint32_t *flags)
182 struct pwmbus_softc *sc;
184 sc = device_get_softc(bus);
186 if (channel > sc->nchannels)
189 return (PWM_CHANNEL_GET_FLAGS(sc->dev, channel, flags));
193 pwmbus_channel_enable(device_t bus, int channel, bool enable)
195 struct pwmbus_softc *sc;
197 sc = device_get_softc(bus);
199 if (channel > sc->nchannels)
202 return (PWM_CHANNEL_ENABLE(sc->dev, channel, enable));
206 pwmbus_channel_is_enabled(device_t bus, int channel, bool *enable)
208 struct pwmbus_softc *sc;
210 sc = device_get_softc(bus);
212 if (channel > sc->nchannels)
215 return (PWM_CHANNEL_IS_ENABLED(sc->dev, channel, enable));
218 static device_method_t pwmbus_methods[] = {
220 DEVMETHOD(device_probe, pwmbus_probe),
221 DEVMETHOD(device_attach, pwmbus_attach),
222 DEVMETHOD(device_detach, pwmbus_detach),
225 DEVMETHOD(pwmbus_channel_config, pwmbus_channel_config),
226 DEVMETHOD(pwmbus_channel_get_config, pwmbus_channel_get_config),
227 DEVMETHOD(pwmbus_channel_set_flags, pwmbus_channel_set_flags),
228 DEVMETHOD(pwmbus_channel_get_flags, pwmbus_channel_get_flags),
229 DEVMETHOD(pwmbus_channel_enable, pwmbus_channel_enable),
230 DEVMETHOD(pwmbus_channel_is_enabled, pwmbus_channel_is_enabled),
235 driver_t pwmbus_driver = {
238 sizeof(struct pwmbus_softc),
240 devclass_t pwmbus_devclass;
242 EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, pwmbus_devclass, 0, 0,
243 BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);
244 MODULE_VERSION(pwmbus, 1);