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 <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
40 #include <dev/pwm/pwmbus.h>
41 #include <dev/pwm/pwmc.h>
43 #include "pwmbus_if.h"
52 pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
53 int fflag, struct thread *td)
55 struct pwmc_softc *sc;
56 struct pwm_state state;
62 bus = device_get_parent(sc->dev);
67 rv = PWMBUS_CHANNEL_COUNT(bus, &nchannel);
68 bcopy(&nchannel, data, sizeof(nchannel));
71 bcopy(data, &state, sizeof(state));
72 rv = PWMBUS_CHANNEL_CONFIG(bus, sc->chan,
73 state.period, state.duty);
75 rv = PWMBUS_CHANNEL_ENABLE(bus, sc->chan,
79 bcopy(data, &state, sizeof(state));
80 rv = PWMBUS_CHANNEL_GET_CONFIG(bus, sc->chan,
81 &state.period, &state.duty);
84 rv = PWMBUS_CHANNEL_IS_ENABLED(bus, sc->chan,
88 bcopy(&state, data, sizeof(state));
95 static struct cdevsw pwm_cdevsw = {
96 .d_version = D_VERSION,
102 pwmc_probe(device_t dev)
105 device_set_desc(dev, "PWM Control");
106 return (BUS_PROBE_NOWILDCARD);
110 pwmc_attach(device_t dev)
112 struct pwmc_softc *sc;
113 struct make_dev_args args;
117 sc = device_get_softc(dev);
120 if ((error = pwmbus_get_channel(dev, &sc->chan)) != 0)
123 make_dev_args_init(&args);
124 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
125 args.mda_devsw = &pwm_cdevsw;
126 args.mda_uid = UID_ROOT;
127 args.mda_gid = GID_OPERATOR;
128 args.mda_mode = 0660;
129 args.mda_si_drv1 = sc;
130 error = make_dev_s(&args, &sc->cdev, "pwmc%d.%d",
131 device_get_unit(device_get_parent(dev)), sc->chan);
133 device_printf(dev, "Failed to make PWM device\n");
137 /* If there is a label hint, create an alias with that name. */
138 if (resource_string_value(device_get_name(dev), device_get_unit(dev),
139 "label", &label) == 0) {
140 make_dev_alias(sc->cdev, "pwm/%s", label);
147 pwmc_detach(device_t dev)
149 struct pwmc_softc *sc;
151 sc = device_get_softc(dev);
152 destroy_dev(sc->cdev);
157 static device_method_t pwmc_methods[] = {
159 DEVMETHOD(device_probe, pwmc_probe),
160 DEVMETHOD(device_attach, pwmc_attach),
161 DEVMETHOD(device_detach, pwmc_detach),
166 static driver_t pwmc_driver = {
169 sizeof(struct pwmc_softc),
171 static devclass_t pwmc_devclass;
173 DRIVER_MODULE(pwmc, pwmbus, pwmc_driver, pwmc_devclass, 0, 0);
174 MODULE_DEPEND(pwmc, pwmbus, 1, 1, 1);
175 MODULE_VERSION(pwmc, 1);