Index: head/sys/dev/pwm/pwmc.c =================================================================== --- head/sys/dev/pwm/pwmc.c (revision 349163) +++ head/sys/dev/pwm/pwmc.c (revision 349164) @@ -1,226 +1,220 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2018 Emmanuel Vadot * 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 __FBSDID("$FreeBSD$"); #include "opt_platform.h" #include #include #include #include #include #include #include #include #include #include "pwmbus_if.h" #ifdef FDT #include #include #include static struct ofw_compat_data compat_data[] = { {"freebsd,pwmc", true}, {NULL, false}, }; PWMBUS_FDT_PNP_INFO(compat_data); #endif 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; - u_int nchannel; int rv = 0; sc = dev->si_drv1; bus = device_get_parent(sc->dev); switch (cmd) { - case PWMMAXCHANNEL: - 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 }; #ifdef FDT static void pwmc_setup_label(struct pwmc_softc *sc) { void *label; if (OF_getprop_alloc(ofw_bus_get_node(sc->dev), "label", &label) > 0) { make_dev_alias(sc->cdev, "pwm/%s", (char *)label); OF_prop_free(label); } } #else /* FDT */ static void pwmc_setup_label(struct pwmc_softc *sc) { const char *label; if (resource_string_value(device_get_name(sc->dev), device_get_unit(sc->dev), "label", &label) == 0) { make_dev_alias(sc->cdev, "pwm/%s", label); } } #endif /* FDT */ static int pwmc_probe(device_t dev) { int rv; rv = BUS_PROBE_NOWILDCARD; #ifdef FDT if (!ofw_bus_status_okay(dev)) return (ENXIO); if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) { rv = BUS_PROBE_DEFAULT; } #endif device_set_desc(dev, "PWM Control"); return (rv); } static int pwmc_attach(device_t dev) { struct pwmc_softc *sc; struct make_dev_args args; 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, "pwm/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); } pwmc_setup_label(sc); 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); Index: head/sys/dev/pwm/pwmc.h =================================================================== --- head/sys/dev/pwm/pwmc.h (revision 349163) +++ head/sys/dev/pwm/pwmc.h (revision 349164) @@ -1,53 +1,51 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2018 Emmanuel Vadot * 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. * * $FreeBSD$ */ #ifndef _PWM_H_ #define _PWM_H_ #define PWM_POLARITY_INVERTED (1 << 0) struct pwm_state { - u_int channel; u_int period; u_int duty; uint32_t flags; bool enable; }; /* * ioctls */ -#define PWMMAXCHANNEL _IOWR('G', 0, int) -#define PWMGETSTATE _IOWR('G', 1, struct pwm_state) -#define PWMSETSTATE _IOWR('G', 2, struct pwm_state) +#define PWMGETSTATE _IOWR('G', 0, struct pwm_state) +#define PWMSETSTATE _IOWR('G', 1, struct pwm_state) #endif /* _PWM_H_ */ Index: head/usr.sbin/pwm/pwm.8 =================================================================== --- head/usr.sbin/pwm/pwm.8 (revision 349163) +++ head/usr.sbin/pwm/pwm.8 (revision 349164) @@ -1,118 +1,111 @@ .\" Copyright (c) 2018 Emmanuel Vadot .\" .\" 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 DEVELOPERS ``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 DEVELOPERS 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. .\" .\" $FreeBSD$ .\" .Dd June 17, 2019 .Dt PWM 8 .Os .Sh NAME .Nm pwm .Nd configure pwm controller .Sh SYNOPSIS .Nm .Op Fl f Ar device -.Fl c Ar channel .Fl E .Nm .Op Fl f Ar device -.Fl c Ar channel .Fl D .Nm .Op Fl f Ar device -.Fl c Ar channel .Fl C .Nm .Op Fl f Ar device -.Fl c Ar channel .Fl p Ar period .Nm .Op Fl f Ar device -.Fl c Ar channel .Fl d Ar duty .Sh DESCRIPTION The .Nm utility can be used to configure pwm controllers. .Pp The options are as follow: -.Bl -tag -width "-c channel" -.It Fl c Ar channel -Channel number to operate on. +.Bl -tag -width "-f device" .It Fl f Ar device Device to operate on. If not specified, .Pa /dev/pwm/pwmc0.0 is used. If an unqualified name is provided, .Pa /dev/pwm is automatically prepended. .It Fl E Enable the pwm channel. .It Fl D Disable the pwm channel. .It Fl C Show the configuration of the pwm channel. .It Fl p Ar period Configure the period (in nanoseconds) of the pwm channel .It Fl d Ar duty Configure the duty (in nanoseconds or percentage) of the pwm channel. Duty is the portion of the .Ar period during which the signal is asserted. .El .Sh EXAMPLES .Bl -bullet .It Show the configuration of the pwm channel: .Bd -literal pwm -f /dev/pwm/pwmc0.1 -C .Ed .It Configure a 50000 ns period and a 25000 ns duty cycle: .Bd -literal pwm -f pwmc1.1 -p 50000 -d 25000 .Ed .It Configure a 50% duty cycle on the device and channel which were configured in .Xr pwmc 4 to have the label .Pa backlight : .Bd -literal pwm -f backlight -d 50% .Ed .El .Sh SEE ALSO .Xr pwm 9 , .Xr pwmbus 9 .Sh HISTORY The .Nm utility appeared in .Fx 13.0 . .Sh AUTHORS .An -nosplit The .Nm utility and this manual page were written by .An Emmanuel Vadot Aq Mt manu@FreeBSD.org . Index: head/usr.sbin/pwm/pwm.c =================================================================== --- head/usr.sbin/pwm/pwm.c (revision 349163) +++ head/usr.sbin/pwm/pwm.c (revision 349164) @@ -1,239 +1,223 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2018 Emmanuel Vadot * 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. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PWM_ENABLE 0x0001 #define PWM_DISABLE 0x0002 #define PWM_SHOW_CONFIG 0x0004 #define PWM_PERIOD 0x0008 #define PWM_DUTY 0x0010 static char device_name[PATH_MAX] = "/dev/pwm/pwmc0.0"; static void set_device_name(const char *name) { if (name[0] == '/') strlcpy(device_name, name, sizeof(device_name)); else snprintf(device_name, sizeof(device_name), "/dev/pwm/%s", name); } static void usage(void) { fprintf(stderr, "Usage:\n"); - fprintf(stderr, "\tpwm [-f dev] -c channel -E\n"); - fprintf(stderr, "\tpwm [-f dev] -c channel -D\n"); - fprintf(stderr, "\tpwm [-f dev] -c channel -C\n"); - fprintf(stderr, "\tpwm [-f dev] -c channel -p period\n"); - fprintf(stderr, "\tpwm [-f dev] -c channel -d duty\n"); + fprintf(stderr, "\tpwm [-f dev] -E\n"); + fprintf(stderr, "\tpwm [-f dev] -D\n"); + fprintf(stderr, "\tpwm [-f dev] -C\n"); + fprintf(stderr, "\tpwm [-f dev] -p period\n"); + fprintf(stderr, "\tpwm [-f dev] -d duty\n"); exit(1); } int main(int argc, char *argv[]) { struct pwm_state state; int fd; - u_int channel, nchannels; int period, duty; int action, ch; cap_rights_t right_ioctl; - const unsigned long pwm_ioctls[] = {PWMGETSTATE, PWMSETSTATE, PWMMAXCHANNEL}; + const unsigned long pwm_ioctls[] = {PWMGETSTATE, PWMSETSTATE}; char *percent; bool setname; action = 0; setname = false; fd = -1; - channel = -1u; period = duty = -1; - while ((ch = getopt(argc, argv, "f:c:EDCp:d:")) != -1) { + while ((ch = getopt(argc, argv, "f:EDCp:d:")) != -1) { switch (ch) { case 'E': if (action) usage(); action = PWM_ENABLE; break; case 'D': if (action) usage(); action = PWM_DISABLE; break; case 'C': if (action) usage(); action = PWM_SHOW_CONFIG; break; case 'p': if (action & ~(PWM_PERIOD | PWM_DUTY)) usage(); action = PWM_PERIOD; period = strtol(optarg, NULL, 10); break; case 'd': if (action & ~(PWM_PERIOD | PWM_DUTY)) usage(); action = PWM_DUTY; duty = strtol(optarg, &percent, 10); if (*percent != '\0' && *percent != '%') usage(); break; - case 'c': - if (channel != -1u) - usage(); - channel = strtoul(optarg, NULL, 10); - break; case 'f': setname = true; set_device_name(optarg); break; + case '?': + usage(); + break; } } if (action == 0) usage(); if ((fd = open(device_name, O_RDWR)) == -1) { fprintf(stderr, "pwm: cannot open %s: %s\n", device_name, strerror(errno)); if (setname) exit(1); else usage(); } if (caph_limit_stdio() < 0) { fprintf(stderr, "can't limit stdio rights"); goto fail; } caph_cache_catpages(); cap_rights_init(&right_ioctl, CAP_IOCTL); if (caph_rights_limit(fd, &right_ioctl) < 0) { fprintf(stderr, "cap_right_limit() failed\n"); goto fail; } if (caph_ioctls_limit(fd, pwm_ioctls, nitems(pwm_ioctls)) < 0) { fprintf(stderr, "caph_ioctls_limit() failed\n"); goto fail; } if (caph_enter() < 0) { fprintf(stderr, "failed to enter capability mode\n"); goto fail; } - /* Check if the channel is correct */ - if (ioctl(fd, PWMMAXCHANNEL, &nchannels) == -1) { - fprintf(stderr, "ioctl: %s\n", strerror(errno)); - goto fail; - } - if (channel > nchannels) { - fprintf(stderr, "pwm controller only support %d channels\n", - nchannels); - goto fail; - } - /* Fill the common args */ - state.channel = channel; if (ioctl(fd, PWMGETSTATE, &state) == -1) { fprintf(stderr, "Cannot get current state of the pwm controller\n"); goto fail; } switch (action) { case PWM_ENABLE: if (state.enable == false) { state.enable = true; if (ioctl(fd, PWMSETSTATE, &state) == -1) { fprintf(stderr, "Cannot enable the pwm controller\n"); goto fail; } } break; case PWM_DISABLE: if (state.enable == true) { state.enable = false; if (ioctl(fd, PWMSETSTATE, &state) == -1) { fprintf(stderr, "Cannot disable the pwm controller\n"); goto fail; } } break; case PWM_SHOW_CONFIG: printf("period: %u\nduty: %u\nenabled:%d\n", state.period, state.duty, state.enable); break; case PWM_PERIOD: case PWM_DUTY: if (period != -1) state.period = period; if (duty != -1) { if (*percent != '\0') state.duty = state.period * duty / 100; else state.duty = duty; } if (ioctl(fd, PWMSETSTATE, &state) == -1) { fprintf(stderr, "Cannot configure the pwm controller\n"); goto fail; } break; } close(fd); return (0); fail: close(fd); return (1); }