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"
51 struct pwmbus_channel_data {
64 pwmbus_probe(device_t dev)
67 device_set_desc(dev, "PWM bus");
68 return (BUS_PROBE_GENERIC);
72 pwmbus_attach(device_t dev)
74 struct pwmbus_softc *sc;
76 sc = device_get_softc(dev);
78 sc->parent = device_get_parent(dev);
80 if (PWMBUS_CHANNEL_COUNT(sc->parent, &sc->nchannels) != 0 ||
82 device_printf(sc->dev, "No channels on parent %s\n",
83 device_get_nameunit(sc->parent));
87 device_add_child(sc->dev, "pwmc", -1);
89 bus_generic_probe(dev);
91 return (bus_generic_attach(dev));
95 pwmbus_detach(device_t dev)
99 if ((rv = bus_generic_detach(dev)) == 0)
100 rv = device_delete_children(dev);
106 pwmbus_channel_config(device_t dev, int chan, u_int period, u_int duty)
108 return (PWMBUS_CHANNEL_CONFIG(device_get_parent(dev), chan, period, duty));
112 pwmbus_channel_get_config(device_t dev, int chan, u_int *period, u_int *duty)
114 return (PWMBUS_CHANNEL_GET_CONFIG(device_get_parent(dev), chan, period, duty));
118 pwmbus_channel_get_flags(device_t dev, int chan, uint32_t *flags)
120 return (PWMBUS_CHANNEL_GET_FLAGS(device_get_parent(dev), chan, flags));
124 pwmbus_channel_enable(device_t dev, int chan, bool enable)
126 return (PWMBUS_CHANNEL_ENABLE(device_get_parent(dev), chan, enable));
130 pwmbus_channel_set_flags(device_t dev, int chan, uint32_t flags)
132 return (PWMBUS_CHANNEL_SET_FLAGS(device_get_parent(dev), chan, flags));
136 pwmbus_channel_is_enabled(device_t dev, int chan, bool *enable)
138 return (PWMBUS_CHANNEL_IS_ENABLED(device_get_parent(dev), chan, enable));
142 pwmbus_channel_count(device_t dev, int *nchannel)
144 return (PWMBUS_CHANNEL_COUNT(device_get_parent(dev), nchannel));
147 static device_method_t pwmbus_methods[] = {
149 DEVMETHOD(device_probe, pwmbus_probe),
150 DEVMETHOD(device_attach, pwmbus_attach),
151 DEVMETHOD(device_detach, pwmbus_detach),
154 DEVMETHOD(pwmbus_channel_count, pwmbus_channel_count),
155 DEVMETHOD(pwmbus_channel_config, pwmbus_channel_config),
156 DEVMETHOD(pwmbus_channel_get_config, pwmbus_channel_get_config),
157 DEVMETHOD(pwmbus_channel_set_flags, pwmbus_channel_set_flags),
158 DEVMETHOD(pwmbus_channel_get_flags, pwmbus_channel_get_flags),
159 DEVMETHOD(pwmbus_channel_enable, pwmbus_channel_enable),
160 DEVMETHOD(pwmbus_channel_is_enabled, pwmbus_channel_is_enabled),
165 static driver_t pwmbus_driver = {
168 sizeof(struct pwmbus_softc),
170 static devclass_t pwmbus_devclass;
172 EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, pwmbus_devclass, 0, 0,
173 BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);
174 MODULE_VERSION(pwmbus, 1);