1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/conf.h> 36 #include <sys/kernel.h> 37 #include <sys/module.h> 38 #include <sys/time.h> 39 40 #include <dev/pwm/pwmc.h> 41 42 #include "pwmbus_if.h" 43 44 struct pwmc_softc { 45 device_t dev; 46 struct cdev *pwm_dev; 47 char name[32]; 48 }; 49 50 static int 51 pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 52 int fflag, struct thread *td) 53 { 54 struct pwmc_softc *sc; 55 struct pwm_state state; 56 device_t bus; 57 u_int nchannel; 58 int rv = 0; 59 60 sc = dev->si_drv1; 61 bus = device_get_parent(sc->dev); 62 63 switch (cmd) { 64 case PWMMAXCHANNEL: 65 nchannel = 0; 66 rv = PWMBUS_CHANNEL_COUNT(bus, &nchannel); 67 bcopy(&nchannel, data, sizeof(nchannel)); 68 break; 69 case PWMSETSTATE: 70 bcopy(data, &state, sizeof(state)); 71 rv = PWMBUS_CHANNEL_CONFIG(bus, state.channel, 72 state.period, state.duty); 73 if (rv == 0) 74 rv = PWMBUS_CHANNEL_ENABLE(bus, state.channel, 75 state.enable); 76 break; 77 case PWMGETSTATE: 78 bcopy(data, &state, sizeof(state)); 79 rv = PWMBUS_CHANNEL_GET_CONFIG(bus, state.channel, 80 &state.period, &state.duty); 81 if (rv != 0) 82 return (rv); 83 rv = PWMBUS_CHANNEL_IS_ENABLED(bus, state.channel, 84 &state.enable); 85 if (rv != 0) 86 return (rv); 87 bcopy(&state, data, sizeof(state)); 88 break; 89 } 90 91 return (rv); 92 } 93 94 static struct cdevsw pwm_cdevsw = { 95 .d_version = D_VERSION, 96 .d_name = "pwm", 97 .d_ioctl = pwm_ioctl 98 }; 99 100 static int 101 pwmc_probe(device_t dev) 102 { 103 104 device_set_desc(dev, "PWM Controller"); 105 return (BUS_PROBE_NOWILDCARD); 106 } 107 108 static int 109 pwmc_attach(device_t dev) 110 { 111 struct pwmc_softc *sc; 112 struct make_dev_args args; 113 114 sc = device_get_softc(dev); 115 sc->dev = dev; 116 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"); 127 return (ENXIO); 128 } 129 return (0); 130 } 131 132 static int 133 pwmc_detach(device_t dev) 134 { 135 struct pwmc_softc *sc; 136 137 sc = device_get_softc(dev); 138 destroy_dev(sc->pwm_dev); 139 140 return (0); 141 } 142 143 static device_method_t pwmc_methods[] = { 144 /* device_if */ 145 DEVMETHOD(device_probe, pwmc_probe), 146 DEVMETHOD(device_attach, pwmc_attach), 147 DEVMETHOD(device_detach, pwmc_detach), 148 149 DEVMETHOD_END 150 }; 151 152 static driver_t pwmc_driver = { 153 "pwmc", 154 pwmc_methods, 155 sizeof(struct pwmc_softc), 156 }; 157 static devclass_t pwmc_devclass; 158 159 DRIVER_MODULE(pwmc, pwmbus, pwmc_driver, pwmc_devclass, 0, 0); 160 MODULE_DEPEND(pwmc, pwmbus, 1, 1, 1); 161 MODULE_VERSION(pwmc, 1); 162