1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_platform.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/bus.h> 36 #include <sys/conf.h> 37 #include <sys/kernel.h> 38 #include <sys/module.h> 39 #include <sys/time.h> 40 41 #include <dev/pwm/pwmbus.h> 42 #include <dev/pwm/pwmc.h> 43 44 #include "pwmbus_if.h" 45 46 #ifdef FDT 47 #include <dev/ofw/openfirm.h> 48 #include <dev/ofw/ofw_bus.h> 49 #include <dev/ofw/ofw_bus_subr.h> 50 51 static struct ofw_compat_data compat_data[] = { 52 {"freebsd,pwmc", true}, 53 {NULL, false}, 54 }; 55 56 PWMBUS_FDT_PNP_INFO(compat_data); 57 58 #endif 59 60 struct pwmc_softc { 61 device_t dev; 62 struct cdev *cdev; 63 u_int chan; 64 }; 65 66 static int 67 pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 68 int fflag, struct thread *td) 69 { 70 struct pwmc_softc *sc; 71 struct pwm_state state; 72 device_t bus; 73 int rv = 0; 74 75 sc = dev->si_drv1; 76 bus = device_get_parent(sc->dev); 77 78 switch (cmd) { 79 case PWMSETSTATE: 80 bcopy(data, &state, sizeof(state)); 81 rv = PWMBUS_CHANNEL_CONFIG(bus, sc->chan, 82 state.period, state.duty); 83 if (rv == 0) 84 rv = PWMBUS_CHANNEL_ENABLE(bus, sc->chan, 85 state.enable); 86 break; 87 case PWMGETSTATE: 88 bcopy(data, &state, sizeof(state)); 89 rv = PWMBUS_CHANNEL_GET_CONFIG(bus, sc->chan, 90 &state.period, &state.duty); 91 if (rv != 0) 92 return (rv); 93 rv = PWMBUS_CHANNEL_IS_ENABLED(bus, sc->chan, 94 &state.enable); 95 if (rv != 0) 96 return (rv); 97 bcopy(&state, data, sizeof(state)); 98 break; 99 } 100 101 return (rv); 102 } 103 104 static struct cdevsw pwm_cdevsw = { 105 .d_version = D_VERSION, 106 .d_name = "pwmc", 107 .d_ioctl = pwm_ioctl 108 }; 109 110 static void 111 pwmc_setup_label(struct pwmc_softc *sc) 112 { 113 const char *hintlabel; 114 #ifdef FDT 115 void *label; 116 117 if (OF_getprop_alloc(ofw_bus_get_node(sc->dev), "label", &label) > 0) { 118 make_dev_alias(sc->cdev, "pwm/%s", (char *)label); 119 OF_prop_free(label); 120 } 121 #endif 122 123 if (resource_string_value(device_get_name(sc->dev), 124 device_get_unit(sc->dev), "label", &hintlabel) == 0) { 125 make_dev_alias(sc->cdev, "pwm/%s", hintlabel); 126 } 127 } 128 129 static int 130 pwmc_probe(device_t dev) 131 { 132 int rv; 133 134 rv = BUS_PROBE_NOWILDCARD; 135 136 #ifdef FDT 137 if (!ofw_bus_status_okay(dev)) 138 return (ENXIO); 139 140 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) { 141 rv = BUS_PROBE_DEFAULT; 142 } 143 #endif 144 145 device_set_desc(dev, "PWM Control"); 146 return (rv); 147 } 148 149 static int 150 pwmc_attach(device_t dev) 151 { 152 struct pwmc_softc *sc; 153 struct make_dev_args args; 154 int error; 155 156 sc = device_get_softc(dev); 157 sc->dev = dev; 158 159 if ((error = pwmbus_get_channel(dev, &sc->chan)) != 0) 160 return (error); 161 162 make_dev_args_init(&args); 163 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK; 164 args.mda_devsw = &pwm_cdevsw; 165 args.mda_uid = UID_ROOT; 166 args.mda_gid = GID_OPERATOR; 167 args.mda_mode = 0660; 168 args.mda_si_drv1 = sc; 169 error = make_dev_s(&args, &sc->cdev, "pwm/pwmc%d.%d", 170 device_get_unit(device_get_parent(dev)), sc->chan); 171 if (error != 0) { 172 device_printf(dev, "Failed to make PWM device\n"); 173 return (error); 174 } 175 176 pwmc_setup_label(sc); 177 178 return (0); 179 } 180 181 static int 182 pwmc_detach(device_t dev) 183 { 184 struct pwmc_softc *sc; 185 186 sc = device_get_softc(dev); 187 destroy_dev(sc->cdev); 188 189 return (0); 190 } 191 192 static device_method_t pwmc_methods[] = { 193 /* device_if */ 194 DEVMETHOD(device_probe, pwmc_probe), 195 DEVMETHOD(device_attach, pwmc_attach), 196 DEVMETHOD(device_detach, pwmc_detach), 197 198 DEVMETHOD_END 199 }; 200 201 static driver_t pwmc_driver = { 202 "pwmc", 203 pwmc_methods, 204 sizeof(struct pwmc_softc), 205 }; 206 static devclass_t pwmc_devclass; 207 208 DRIVER_MODULE(pwmc, pwmbus, pwmc_driver, pwmc_devclass, 0, 0); 209 MODULE_DEPEND(pwmc, pwmbus, 1, 1, 1); 210 MODULE_VERSION(pwmc, 1); 211