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_probe(device_t dev)
68 device_set_desc(dev, "PWM bus");
69 return (BUS_PROBE_GENERIC);
73 pwmbus_attach(device_t dev)
75 struct pwmbus_softc *sc;
77 sc = device_get_softc(dev);
79 sc->dev = device_get_parent(dev);
81 if (PWM_CHANNEL_MAX(sc->dev, &sc->nchannels) != 0 ||
86 device_printf(dev, "Registering %d channel(s)\n", sc->nchannels);
87 bus_generic_probe(dev);
89 return (bus_generic_attach(dev));
93 pwmbus_detach(device_t dev)
98 rv = bus_generic_detach(dev);
102 rv = device_get_children(dev, &devlist, &ndevs);
105 for (i = 0; i < ndevs; i++)
106 device_delete_child(dev, devlist[i]);
112 pwmbus_channel_config(device_t bus, int channel, unsigned int period, unsigned int duty)
114 struct pwmbus_softc *sc;
116 sc = device_get_softc(bus);
118 if (channel > sc->nchannels)
121 return (PWM_CHANNEL_CONFIG(sc->dev, channel, period, duty));
125 pwmbus_channel_get_config(device_t bus, int channel, unsigned int *period, unsigned int *duty)
127 struct pwmbus_softc *sc;
129 sc = device_get_softc(bus);
131 if (channel > sc->nchannels)
134 return (PWM_CHANNEL_GET_CONFIG(sc->dev, channel, period, duty));
138 pwmbus_channel_set_flags(device_t bus, int channel, uint32_t flags)
140 struct pwmbus_softc *sc;
142 sc = device_get_softc(bus);
144 if (channel > sc->nchannels)
147 return (PWM_CHANNEL_SET_FLAGS(sc->dev, channel, flags));
151 pwmbus_channel_get_flags(device_t bus, int channel, uint32_t *flags)
153 struct pwmbus_softc *sc;
155 sc = device_get_softc(bus);
157 if (channel > sc->nchannels)
160 return (PWM_CHANNEL_GET_FLAGS(sc->dev, channel, flags));
164 pwmbus_channel_enable(device_t bus, int channel, bool enable)
166 struct pwmbus_softc *sc;
168 sc = device_get_softc(bus);
170 if (channel > sc->nchannels)
173 return (PWM_CHANNEL_ENABLE(sc->dev, channel, enable));
177 pwmbus_channel_is_enabled(device_t bus, int channel, bool *enable)
179 struct pwmbus_softc *sc;
181 sc = device_get_softc(bus);
183 if (channel > sc->nchannels)
186 return (PWM_CHANNEL_IS_ENABLED(sc->dev, channel, enable));
189 static device_method_t pwmbus_methods[] = {
191 DEVMETHOD(device_probe, pwmbus_probe),
192 DEVMETHOD(device_attach, pwmbus_attach),
193 DEVMETHOD(device_detach, pwmbus_detach),
196 DEVMETHOD(pwmbus_channel_config, pwmbus_channel_config),
197 DEVMETHOD(pwmbus_channel_get_config, pwmbus_channel_get_config),
198 DEVMETHOD(pwmbus_channel_set_flags, pwmbus_channel_set_flags),
199 DEVMETHOD(pwmbus_channel_get_flags, pwmbus_channel_get_flags),
200 DEVMETHOD(pwmbus_channel_enable, pwmbus_channel_enable),
201 DEVMETHOD(pwmbus_channel_is_enabled, pwmbus_channel_is_enabled),
206 driver_t pwmbus_driver = {
209 sizeof(struct pwmbus_softc),
211 devclass_t pwmbus_devclass;
213 EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, pwmbus_devclass, 0, 0,
214 BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);
215 MODULE_VERSION(pwmbus, 1);