2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Emmanuel Vadot <manu@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 "opt_platform.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/endian.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
42 #include <dev/pwm/pwmbus.h>
44 #include "pwmbus_if.h"
51 pwmbus_add_child(device_t dev, u_int order, const char *name, int unit)
54 struct pwmbus_ivars *ivars;
56 child = device_add_child_ordered(dev, order, name, unit);
60 ivars = malloc(sizeof(struct pwmbus_ivars), M_DEVBUF, M_NOWAIT | M_ZERO);
62 device_delete_child(dev, child);
65 device_set_ivars(child, ivars);
71 pwmbus_child_location_str(device_t dev, device_t child, char *buf, size_t blen)
73 struct pwmbus_ivars *ivars;
75 ivars = device_get_ivars(child);
76 snprintf(buf, blen, "hwdev=%s channel=%u",
77 device_get_nameunit(device_get_parent(dev)), ivars->pi_channel);
83 pwmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf,
91 pwmbus_hinted_child(device_t dev, const char *dname, int dunit)
93 struct pwmbus_ivars *ivars;
96 child = pwmbus_add_child(dev, 0, dname, dunit);
99 * If there is a channel hint, use it. Otherwise pi_channel was
100 * initialized to zero, so that's the channel we'll use.
102 ivars = device_get_ivars(child);
103 resource_int_value(dname, dunit, "channel", &ivars->pi_channel);
107 pwmbus_print_child(device_t dev, device_t child)
109 struct pwmbus_ivars *ivars;
112 ivars = device_get_ivars(child);
114 rv = bus_print_child_header(dev, child);
115 rv += printf(" channel %u", ivars->pi_channel);
116 rv += bus_print_child_footer(dev, child);
122 pwmbus_probe_nomatch(device_t dev, device_t child)
124 struct pwmbus_ivars *ivars;
126 ivars = device_get_ivars(child);
128 device_printf(dev, "<unknown> on channel %u\n",
135 pwmbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
137 struct pwmbus_ivars *ivars;
139 ivars = device_get_ivars(child);
142 case PWMBUS_IVAR_CHANNEL:
143 *(u_int *)result = ivars->pi_channel;
153 * device_if methods...
157 pwmbus_probe(device_t dev)
159 device_set_desc(dev, "PWM bus");
160 return (BUS_PROBE_GENERIC);
164 pwmbus_attach(device_t dev)
166 struct pwmbus_softc *sc;
167 struct pwmbus_ivars *ivars;
168 device_t child, parent;
171 sc = device_get_softc(dev);
173 parent = device_get_parent(dev);
175 if (PWMBUS_CHANNEL_COUNT(parent, &sc->nchannels) != 0 ||
176 sc->nchannels == 0) {
177 device_printf(sc->dev, "No channels on parent %s\n",
178 device_get_nameunit(parent));
182 /* Add a pwmc(4) child for each channel. */
183 for (chan = 0; chan < sc->nchannels; ++chan) {
184 if ((child = pwmbus_add_child(sc->dev, 0, "pwmc", -1)) == NULL) {
185 device_printf(dev, "failed to add pwmc child device "
186 "for channel %u\n", chan);
189 ivars = device_get_ivars(child);
190 ivars->pi_channel = chan;
193 bus_enumerate_hinted_children(dev);
194 bus_generic_probe(dev);
196 return (bus_generic_attach(dev));
200 pwmbus_detach(device_t dev)
204 if ((rv = bus_generic_detach(dev)) == 0)
205 rv = device_delete_children(dev);
211 * pwmbus_if methods...
215 pwmbus_channel_config(device_t dev, u_int chan, u_int period, u_int duty)
217 return (PWMBUS_CHANNEL_CONFIG(device_get_parent(dev), chan, period, duty));
221 pwmbus_channel_get_config(device_t dev, u_int chan, u_int *period, u_int *duty)
223 return (PWMBUS_CHANNEL_GET_CONFIG(device_get_parent(dev), chan, period, duty));
227 pwmbus_channel_get_flags(device_t dev, u_int chan, uint32_t *flags)
229 return (PWMBUS_CHANNEL_GET_FLAGS(device_get_parent(dev), chan, flags));
233 pwmbus_channel_enable(device_t dev, u_int chan, bool enable)
235 return (PWMBUS_CHANNEL_ENABLE(device_get_parent(dev), chan, enable));
239 pwmbus_channel_set_flags(device_t dev, u_int chan, uint32_t flags)
241 return (PWMBUS_CHANNEL_SET_FLAGS(device_get_parent(dev), chan, flags));
245 pwmbus_channel_is_enabled(device_t dev, u_int chan, bool *enable)
247 return (PWMBUS_CHANNEL_IS_ENABLED(device_get_parent(dev), chan, enable));
251 pwmbus_channel_count(device_t dev, u_int *nchannel)
253 return (PWMBUS_CHANNEL_COUNT(device_get_parent(dev), nchannel));
256 static device_method_t pwmbus_methods[] = {
258 DEVMETHOD(device_probe, pwmbus_probe),
259 DEVMETHOD(device_attach, pwmbus_attach),
260 DEVMETHOD(device_detach, pwmbus_detach),
263 DEVMETHOD(bus_add_child, pwmbus_add_child),
264 DEVMETHOD(bus_child_location_str, pwmbus_child_location_str),
265 DEVMETHOD(bus_child_pnpinfo_str, pwmbus_child_pnpinfo_str),
266 DEVMETHOD(bus_hinted_child, pwmbus_hinted_child),
267 DEVMETHOD(bus_print_child, pwmbus_print_child),
268 DEVMETHOD(bus_probe_nomatch, pwmbus_probe_nomatch),
269 DEVMETHOD(bus_read_ivar, pwmbus_read_ivar),
272 DEVMETHOD(pwmbus_channel_count, pwmbus_channel_count),
273 DEVMETHOD(pwmbus_channel_config, pwmbus_channel_config),
274 DEVMETHOD(pwmbus_channel_get_config, pwmbus_channel_get_config),
275 DEVMETHOD(pwmbus_channel_set_flags, pwmbus_channel_set_flags),
276 DEVMETHOD(pwmbus_channel_get_flags, pwmbus_channel_get_flags),
277 DEVMETHOD(pwmbus_channel_enable, pwmbus_channel_enable),
278 DEVMETHOD(pwmbus_channel_is_enabled, pwmbus_channel_is_enabled),
283 driver_t pwmbus_driver = {
286 sizeof(struct pwmbus_softc),
288 devclass_t pwmbus_devclass;
290 EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, pwmbus_devclass, 0, 0,
291 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
292 MODULE_VERSION(pwmbus, 1);