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/kernel.h>
38 #include <sys/module.h>
41 #include <dev/pwm/pwmbus.h>
42 #include <dev/pwm/pwmc.h>
44 #include "pwmbus_if.h"
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
51 static struct ofw_compat_data compat_data[] = {
52 {"freebsd,pwmc", true},
56 PWMBUS_FDT_PNP_INFO(compat_data);
67 pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
68 int fflag, struct thread *td)
70 struct pwmc_softc *sc;
71 struct pwm_state state;
76 bus = device_get_parent(sc->dev);
80 bcopy(data, &state, sizeof(state));
81 rv = PWMBUS_CHANNEL_CONFIG(bus, sc->chan,
82 state.period, state.duty);
84 rv = PWMBUS_CHANNEL_ENABLE(bus, sc->chan,
88 bcopy(data, &state, sizeof(state));
89 rv = PWMBUS_CHANNEL_GET_CONFIG(bus, sc->chan,
90 &state.period, &state.duty);
93 rv = PWMBUS_CHANNEL_IS_ENABLED(bus, sc->chan,
97 bcopy(&state, data, sizeof(state));
104 static struct cdevsw pwm_cdevsw = {
105 .d_version = D_VERSION,
111 pwmc_setup_label(struct pwmc_softc *sc)
113 const char *hintlabel;
117 if (OF_getprop_alloc(ofw_bus_get_node(sc->dev), "label", &label) > 0) {
118 make_dev_alias(sc->cdev, "pwm/%s", (char *)label);
123 if (resource_string_value(device_get_name(sc->dev),
124 device_get_unit(sc->dev), "label", &hintlabel) == 0) {
125 make_dev_alias(sc->cdev, "pwm/%s", hintlabel);
130 pwmc_probe(device_t dev)
134 rv = BUS_PROBE_NOWILDCARD;
137 if (!ofw_bus_status_okay(dev))
140 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
141 rv = BUS_PROBE_DEFAULT;
145 device_set_desc(dev, "PWM Control");
150 pwmc_attach(device_t dev)
152 struct pwmc_softc *sc;
153 struct make_dev_args args;
156 sc = device_get_softc(dev);
159 if ((error = pwmbus_get_channel(dev, &sc->chan)) != 0)
162 make_dev_args_init(&args);
163 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
164 args.mda_devsw = &pwm_cdevsw;
165 args.mda_uid = UID_ROOT;
166 args.mda_gid = GID_OPERATOR;
167 args.mda_mode = 0660;
168 args.mda_si_drv1 = sc;
169 error = make_dev_s(&args, &sc->cdev, "pwm/pwmc%d.%d",
170 device_get_unit(device_get_parent(dev)), sc->chan);
172 device_printf(dev, "Failed to make PWM device\n");
176 pwmc_setup_label(sc);
182 pwmc_detach(device_t dev)
184 struct pwmc_softc *sc;
186 sc = device_get_softc(dev);
187 destroy_dev(sc->cdev);
192 static device_method_t pwmc_methods[] = {
194 DEVMETHOD(device_probe, pwmc_probe),
195 DEVMETHOD(device_attach, pwmc_attach),
196 DEVMETHOD(device_detach, pwmc_detach),
201 static driver_t pwmc_driver = {
204 sizeof(struct pwmc_softc),
206 static devclass_t pwmc_devclass;
208 DRIVER_MODULE(pwmc, pwmbus, pwmc_driver, pwmc_devclass, 0, 0);
209 MODULE_DEPEND(pwmc, pwmbus, 1, 1, 1);
210 MODULE_VERSION(pwmc, 1);