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/pwmc.h>
42 #include "pwmbus_if.h"
51 pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
52 int fflag, struct thread *td)
54 struct pwmc_softc *sc;
55 struct pwm_state state;
61 bus = device_get_parent(sc->dev);
66 rv = PWMBUS_CHANNEL_COUNT(bus, &nchannel);
67 bcopy(&nchannel, data, sizeof(nchannel));
70 bcopy(data, &state, sizeof(state));
71 rv = PWMBUS_CHANNEL_CONFIG(bus, state.channel,
72 state.period, state.duty);
74 rv = PWMBUS_CHANNEL_ENABLE(bus, state.channel,
78 bcopy(data, &state, sizeof(state));
79 rv = PWMBUS_CHANNEL_GET_CONFIG(bus, state.channel,
80 &state.period, &state.duty);
83 rv = PWMBUS_CHANNEL_IS_ENABLED(bus, state.channel,
87 bcopy(&state, data, sizeof(state));
94 static struct cdevsw pwm_cdevsw = {
95 .d_version = D_VERSION,
101 pwmc_probe(device_t dev)
104 device_set_desc(dev, "PWM Controller");
105 return (BUS_PROBE_NOWILDCARD);
109 pwmc_attach(device_t dev)
111 struct pwmc_softc *sc;
112 struct make_dev_args args;
114 sc = device_get_softc(dev);
117 snprintf(sc->name, sizeof(sc->name), "pwmc%d", device_get_unit(dev));
118 make_dev_args_init(&args);
119 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
120 args.mda_devsw = &pwm_cdevsw;
121 args.mda_uid = UID_ROOT;
122 args.mda_gid = GID_OPERATOR;
123 args.mda_mode = 0600;
124 args.mda_si_drv1 = sc;
125 if (make_dev_s(&args, &sc->pwm_dev, "%s", sc->name) != 0) {
126 device_printf(dev, "Failed to make PWM device\n");
133 pwmc_detach(device_t dev)
135 struct pwmc_softc *sc;
137 sc = device_get_softc(dev);
138 destroy_dev(sc->pwm_dev);
143 static device_method_t pwmc_methods[] = {
145 DEVMETHOD(device_probe, pwmc_probe),
146 DEVMETHOD(device_attach, pwmc_attach),
147 DEVMETHOD(device_detach, pwmc_detach),
152 static driver_t pwmc_driver = {
155 sizeof(struct pwmc_softc),
157 static devclass_t pwmc_devclass;
159 DRIVER_MODULE(pwmc, pwmbus, pwmc_driver, pwmc_devclass, 0, 0);
160 MODULE_DEPEND(pwmc, pwmbus, 1, 1, 1);
161 MODULE_VERSION(pwmc, 1);