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 #endif 56 57 struct pwmc_softc { 58 device_t dev; 59 struct cdev *cdev; 60 u_int chan; 61 }; 62 63 static int 64 pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 65 int fflag, struct thread *td) 66 { 67 struct pwmc_softc *sc; 68 struct pwm_state state; 69 device_t bus; 70 int rv = 0; 71 72 sc = dev->si_drv1; 73 bus = device_get_parent(sc->dev); 74 75 switch (cmd) { 76 case PWMSETSTATE: 77 bcopy(data, &state, sizeof(state)); 78 rv = PWMBUS_CHANNEL_CONFIG(bus, sc->chan, 79 state.period, state.duty); 80 if (rv == 0) 81 rv = PWMBUS_CHANNEL_ENABLE(bus, sc->chan, 82 state.enable); 83 break; 84 case PWMGETSTATE: 85 bcopy(data, &state, sizeof(state)); 86 rv = PWMBUS_CHANNEL_GET_CONFIG(bus, sc->chan, 87 &state.period, &state.duty); 88 if (rv != 0) 89 return (rv); 90 rv = PWMBUS_CHANNEL_IS_ENABLED(bus, sc->chan, 91 &state.enable); 92 if (rv != 0) 93 return (rv); 94 bcopy(&state, data, sizeof(state)); 95 break; 96 } 97 98 return (rv); 99 } 100 101 static struct cdevsw pwm_cdevsw = { 102 .d_version = D_VERSION, 103 .d_name = "pwmc", 104 .d_ioctl = pwm_ioctl 105 }; 106 107 static void 108 pwmc_setup_label(struct pwmc_softc *sc) 109 { 110 const char *hintlabel; 111 #ifdef FDT 112 void *label; 113 114 if (OF_getprop_alloc(ofw_bus_get_node(sc->dev), "label", &label) > 0) { 115 make_dev_alias(sc->cdev, "pwm/%s", (char *)label); 116 OF_prop_free(label); 117 } 118 #endif 119 120 if (resource_string_value(device_get_name(sc->dev), 121 device_get_unit(sc->dev), "label", &hintlabel) == 0) { 122 make_dev_alias(sc->cdev, "pwm/%s", hintlabel); 123 } 124 } 125 126 static int 127 pwmc_probe(device_t dev) 128 { 129 int rv; 130 131 rv = BUS_PROBE_NOWILDCARD; 132 133 #ifdef FDT 134 if (!ofw_bus_status_okay(dev)) 135 return (ENXIO); 136 137 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) { 138 rv = BUS_PROBE_DEFAULT; 139 } 140 #endif 141 142 device_set_desc(dev, "PWM Control"); 143 return (rv); 144 } 145 146 static int 147 pwmc_attach(device_t dev) 148 { 149 struct pwmc_softc *sc; 150 struct make_dev_args args; 151 int error; 152 153 sc = device_get_softc(dev); 154 sc->dev = dev; 155 156 if ((error = pwmbus_get_channel(dev, &sc->chan)) != 0) 157 return (error); 158 159 make_dev_args_init(&args); 160 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK; 161 args.mda_devsw = &pwm_cdevsw; 162 args.mda_uid = UID_ROOT; 163 args.mda_gid = GID_OPERATOR; 164 args.mda_mode = 0660; 165 args.mda_si_drv1 = sc; 166 error = make_dev_s(&args, &sc->cdev, "pwm/pwmc%d.%d", 167 device_get_unit(device_get_parent(dev)), sc->chan); 168 if (error != 0) { 169 device_printf(dev, "Failed to make PWM device\n"); 170 return (error); 171 } 172 173 pwmc_setup_label(sc); 174 175 return (0); 176 } 177 178 static int 179 pwmc_detach(device_t dev) 180 { 181 struct pwmc_softc *sc; 182 183 sc = device_get_softc(dev); 184 destroy_dev(sc->cdev); 185 186 return (0); 187 } 188 189 static device_method_t pwmc_methods[] = { 190 /* device_if */ 191 DEVMETHOD(device_probe, pwmc_probe), 192 DEVMETHOD(device_attach, pwmc_attach), 193 DEVMETHOD(device_detach, pwmc_detach), 194 195 DEVMETHOD_END 196 }; 197 198 static driver_t pwmc_driver = { 199 "pwmc", 200 pwmc_methods, 201 sizeof(struct pwmc_softc), 202 }; 203 static devclass_t pwmc_devclass; 204 205 DRIVER_MODULE(pwmc, pwmbus, pwmc_driver, pwmc_devclass, 0, 0); 206 #ifdef FDT 207 PWMBUS_FDT_PNP_INFO(compat_data); 208 #endif 209 MODULE_DEPEND(pwmc, pwmbus, 1, 1, 1); 210 MODULE_VERSION(pwmc, 1); 211