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/malloc.h>
41 #include <sys/module.h>
43 #include <dev/pwm/pwmbus.h>
45 #include "pwmbus_if.h"
52 pwmbus_add_child(device_t dev, u_int order, const char *name, int unit)
55 struct pwmbus_ivars *ivars;
57 child = device_add_child_ordered(dev, order, name, unit);
61 ivars = malloc(sizeof(struct pwmbus_ivars), M_DEVBUF, M_NOWAIT | M_ZERO);
63 device_delete_child(dev, child);
66 device_set_ivars(child, ivars);
72 pwmbus_child_location_str(device_t dev, device_t child, char *buf, size_t blen)
74 struct pwmbus_ivars *ivars;
76 ivars = device_get_ivars(child);
77 snprintf(buf, blen, "hwdev=%s channel=%u",
78 device_get_nameunit(device_get_parent(dev)), ivars->pi_channel);
84 pwmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf,
92 pwmbus_hinted_child(device_t dev, const char *dname, int dunit)
94 struct pwmbus_ivars *ivars;
97 child = pwmbus_add_child(dev, 0, dname, dunit);
100 * If there is a channel hint, use it. Otherwise pi_channel was
101 * initialized to zero, so that's the channel we'll use.
103 ivars = device_get_ivars(child);
104 resource_int_value(dname, dunit, "channel", &ivars->pi_channel);
108 pwmbus_print_child(device_t dev, device_t child)
110 struct pwmbus_ivars *ivars;
113 ivars = device_get_ivars(child);
115 rv = bus_print_child_header(dev, child);
116 rv += printf(" channel %u", ivars->pi_channel);
117 rv += bus_print_child_footer(dev, child);
123 pwmbus_probe_nomatch(device_t dev, device_t child)
125 struct pwmbus_ivars *ivars;
127 ivars = device_get_ivars(child);
129 device_printf(dev, "<unknown> on channel %u\n",
136 pwmbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
138 struct pwmbus_ivars *ivars;
140 ivars = device_get_ivars(child);
143 case PWMBUS_IVAR_CHANNEL:
144 *(u_int *)result = ivars->pi_channel;
154 * device_if methods...
158 pwmbus_probe(device_t dev)
160 device_set_desc(dev, "PWM bus");
161 return (BUS_PROBE_GENERIC);
165 pwmbus_attach(device_t dev)
167 struct pwmbus_softc *sc;
168 struct pwmbus_ivars *ivars;
169 device_t child, parent;
172 sc = device_get_softc(dev);
174 parent = device_get_parent(dev);
176 if (PWMBUS_CHANNEL_COUNT(parent, &sc->nchannels) != 0 ||
177 sc->nchannels == 0) {
178 device_printf(sc->dev, "No channels on parent %s\n",
179 device_get_nameunit(parent));
183 /* Add a pwmc(4) child for each channel. */
184 for (chan = 0; chan < sc->nchannels; ++chan) {
185 if ((child = pwmbus_add_child(sc->dev, 0, "pwmc", -1)) == NULL) {
186 device_printf(dev, "failed to add pwmc child device "
187 "for channel %u\n", chan);
190 ivars = device_get_ivars(child);
191 ivars->pi_channel = chan;
194 bus_enumerate_hinted_children(dev);
195 bus_generic_probe(dev);
197 return (bus_generic_attach(dev));
201 pwmbus_detach(device_t dev)
205 if ((rv = bus_generic_detach(dev)) == 0)
206 rv = device_delete_children(dev);
212 * pwmbus_if methods...
216 pwmbus_channel_config(device_t dev, u_int chan, u_int period, u_int duty)
218 return (PWMBUS_CHANNEL_CONFIG(device_get_parent(dev), chan, period, duty));
222 pwmbus_channel_get_config(device_t dev, u_int chan, u_int *period, u_int *duty)
224 return (PWMBUS_CHANNEL_GET_CONFIG(device_get_parent(dev), chan, period, duty));
228 pwmbus_channel_get_flags(device_t dev, u_int chan, uint32_t *flags)
230 return (PWMBUS_CHANNEL_GET_FLAGS(device_get_parent(dev), chan, flags));
234 pwmbus_channel_enable(device_t dev, u_int chan, bool enable)
236 return (PWMBUS_CHANNEL_ENABLE(device_get_parent(dev), chan, enable));
240 pwmbus_channel_set_flags(device_t dev, u_int chan, uint32_t flags)
242 return (PWMBUS_CHANNEL_SET_FLAGS(device_get_parent(dev), chan, flags));
246 pwmbus_channel_is_enabled(device_t dev, u_int chan, bool *enable)
248 return (PWMBUS_CHANNEL_IS_ENABLED(device_get_parent(dev), chan, enable));
252 pwmbus_channel_count(device_t dev, u_int *nchannel)
254 return (PWMBUS_CHANNEL_COUNT(device_get_parent(dev), nchannel));
257 static device_method_t pwmbus_methods[] = {
259 DEVMETHOD(device_probe, pwmbus_probe),
260 DEVMETHOD(device_attach, pwmbus_attach),
261 DEVMETHOD(device_detach, pwmbus_detach),
264 DEVMETHOD(bus_add_child, pwmbus_add_child),
265 DEVMETHOD(bus_child_location_str, pwmbus_child_location_str),
266 DEVMETHOD(bus_child_pnpinfo_str, pwmbus_child_pnpinfo_str),
267 DEVMETHOD(bus_hinted_child, pwmbus_hinted_child),
268 DEVMETHOD(bus_print_child, pwmbus_print_child),
269 DEVMETHOD(bus_probe_nomatch, pwmbus_probe_nomatch),
270 DEVMETHOD(bus_read_ivar, pwmbus_read_ivar),
273 DEVMETHOD(pwmbus_channel_count, pwmbus_channel_count),
274 DEVMETHOD(pwmbus_channel_config, pwmbus_channel_config),
275 DEVMETHOD(pwmbus_channel_get_config, pwmbus_channel_get_config),
276 DEVMETHOD(pwmbus_channel_set_flags, pwmbus_channel_set_flags),
277 DEVMETHOD(pwmbus_channel_get_flags, pwmbus_channel_get_flags),
278 DEVMETHOD(pwmbus_channel_enable, pwmbus_channel_enable),
279 DEVMETHOD(pwmbus_channel_is_enabled, pwmbus_channel_is_enabled),
284 driver_t pwmbus_driver = {
287 sizeof(struct pwmbus_softc),
289 devclass_t pwmbus_devclass;
291 EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, pwmbus_devclass, 0, 0,
292 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
293 MODULE_VERSION(pwmbus, 1);