freebsd-dev/sys/dev/pwm/pwmc.c

176 lines
4.3 KiB
C
Raw Normal View History

/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/time.h>
#include <dev/pwm/pwmbus.h>
#include <dev/pwm/pwmc.h>
#include "pwmbus_if.h"
struct pwmc_softc {
device_t dev;
struct cdev *cdev;
u_int chan;
};
static int
pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
int fflag, struct thread *td)
{
struct pwmc_softc *sc;
struct pwm_state state;
device_t bus;
2019-06-15 23:02:09 +00:00
u_int nchannel;
int rv = 0;
sc = dev->si_drv1;
bus = device_get_parent(sc->dev);
switch (cmd) {
case PWMMAXCHANNEL:
2019-06-15 23:02:09 +00:00
nchannel = 0;
rv = PWMBUS_CHANNEL_COUNT(bus, &nchannel);
bcopy(&nchannel, data, sizeof(nchannel));
break;
case PWMSETSTATE:
bcopy(data, &state, sizeof(state));
rv = PWMBUS_CHANNEL_CONFIG(bus, sc->chan,
state.period, state.duty);
if (rv == 0)
rv = PWMBUS_CHANNEL_ENABLE(bus, sc->chan,
state.enable);
break;
case PWMGETSTATE:
bcopy(data, &state, sizeof(state));
rv = PWMBUS_CHANNEL_GET_CONFIG(bus, sc->chan,
&state.period, &state.duty);
if (rv != 0)
return (rv);
rv = PWMBUS_CHANNEL_IS_ENABLED(bus, sc->chan,
&state.enable);
if (rv != 0)
return (rv);
bcopy(&state, data, sizeof(state));
break;
}
return (rv);
}
static struct cdevsw pwm_cdevsw = {
.d_version = D_VERSION,
.d_name = "pwmc",
.d_ioctl = pwm_ioctl
};
static int
pwmc_probe(device_t dev)
{
device_set_desc(dev, "PWM Control");
return (BUS_PROBE_NOWILDCARD);
}
static int
pwmc_attach(device_t dev)
{
struct pwmc_softc *sc;
struct make_dev_args args;
const char *label;
int error;
sc = device_get_softc(dev);
sc->dev = dev;
if ((error = pwmbus_get_channel(dev, &sc->chan)) != 0)
return (error);
make_dev_args_init(&args);
args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
args.mda_devsw = &pwm_cdevsw;
args.mda_uid = UID_ROOT;
args.mda_gid = GID_OPERATOR;
args.mda_mode = 0660;
args.mda_si_drv1 = sc;
error = make_dev_s(&args, &sc->cdev, "pwmc%d.%d",
device_get_unit(device_get_parent(dev)), sc->chan);
if (error != 0) {
device_printf(dev, "Failed to make PWM device\n");
return (error);
}
/* If there is a label hint, create an alias with that name. */
if (resource_string_value(device_get_name(dev), device_get_unit(dev),
"label", &label) == 0) {
make_dev_alias(sc->cdev, "pwm/%s", label);
}
return (0);
}
static int
pwmc_detach(device_t dev)
{
struct pwmc_softc *sc;
sc = device_get_softc(dev);
destroy_dev(sc->cdev);
return (0);
}
static device_method_t pwmc_methods[] = {
/* device_if */
DEVMETHOD(device_probe, pwmc_probe),
DEVMETHOD(device_attach, pwmc_attach),
DEVMETHOD(device_detach, pwmc_detach),
DEVMETHOD_END
};
static driver_t pwmc_driver = {
"pwmc",
pwmc_methods,
sizeof(struct pwmc_softc),
};
static devclass_t pwmc_devclass;
DRIVER_MODULE(pwmc, pwmbus, pwmc_driver, pwmc_devclass, 0, 0);
MODULE_DEPEND(pwmc, pwmbus, 1, 1, 1);
MODULE_VERSION(pwmc, 1);