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