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/kernel.h>
39 #include <sys/module.h>
42 #include <dev/pwm/pwmbus.h>
43 #include <dev/pwm/pwmc.h>
45 #include "pwmbus_if.h"
48 #include <dev/ofw/openfirm.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
52 static struct ofw_compat_data compat_data[] = {
53 {"freebsd,pwmc", true},
57 PWMBUS_FDT_PNP_INFO(compat_data);
68 pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
69 int fflag, struct thread *td)
71 struct pwmc_softc *sc;
72 struct pwm_state state;
77 bus = device_get_parent(sc->dev);
81 bcopy(data, &state, sizeof(state));
82 rv = PWMBUS_CHANNEL_CONFIG(bus, sc->chan,
83 state.period, state.duty);
85 rv = PWMBUS_CHANNEL_ENABLE(bus, sc->chan,
89 bcopy(data, &state, sizeof(state));
90 rv = PWMBUS_CHANNEL_GET_CONFIG(bus, sc->chan,
91 &state.period, &state.duty);
94 rv = PWMBUS_CHANNEL_IS_ENABLED(bus, sc->chan,
98 bcopy(&state, data, sizeof(state));
105 static struct cdevsw pwm_cdevsw = {
106 .d_version = D_VERSION,
112 pwmc_setup_label(struct pwmc_softc *sc)
114 const char *hintlabel;
118 if (OF_getprop_alloc(ofw_bus_get_node(sc->dev), "label", &label) > 0) {
119 make_dev_alias(sc->cdev, "pwm/%s", (char *)label);
124 if (resource_string_value(device_get_name(sc->dev),
125 device_get_unit(sc->dev), "label", &hintlabel) == 0) {
126 make_dev_alias(sc->cdev, "pwm/%s", hintlabel);
131 pwmc_probe(device_t dev)
135 rv = BUS_PROBE_NOWILDCARD;
138 if (!ofw_bus_status_okay(dev))
141 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
142 rv = BUS_PROBE_DEFAULT;
146 device_set_desc(dev, "PWM Control");
151 pwmc_attach(device_t dev)
153 struct pwmc_softc *sc;
154 struct make_dev_args args;
157 sc = device_get_softc(dev);
160 if ((error = pwmbus_get_channel(dev, &sc->chan)) != 0)
163 make_dev_args_init(&args);
164 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
165 args.mda_devsw = &pwm_cdevsw;
166 args.mda_uid = UID_ROOT;
167 args.mda_gid = GID_OPERATOR;
168 args.mda_mode = 0660;
169 args.mda_si_drv1 = sc;
170 error = make_dev_s(&args, &sc->cdev, "pwm/pwmc%d.%d",
171 device_get_unit(device_get_parent(dev)), sc->chan);
173 device_printf(dev, "Failed to make PWM device\n");
177 pwmc_setup_label(sc);
183 pwmc_detach(device_t dev)
185 struct pwmc_softc *sc;
187 sc = device_get_softc(dev);
188 destroy_dev(sc->cdev);
193 static device_method_t pwmc_methods[] = {
195 DEVMETHOD(device_probe, pwmc_probe),
196 DEVMETHOD(device_attach, pwmc_attach),
197 DEVMETHOD(device_detach, pwmc_detach),
202 static driver_t pwmc_driver = {
205 sizeof(struct pwmc_softc),
207 static devclass_t pwmc_devclass;
209 DRIVER_MODULE(pwmc, pwmbus, pwmc_driver, pwmc_devclass, 0, 0);
210 MODULE_DEPEND(pwmc, pwmbus, 1, 1, 1);
211 MODULE_VERSION(pwmc, 1);