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 <sys/pwm.h> 41 42 #include "pwmbus_if.h" 43 #include "pwm_if.h" 44 45 struct pwmc_softc { 46 device_t dev; 47 device_t pdev; 48 struct cdev *pwm_dev; 49 char name[32]; 50 }; 51 52 static int 53 pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 54 int fflag, struct thread *td) 55 { 56 struct pwmc_softc *sc; 57 struct pwm_state state; 58 device_t bus; 59 int nchannel; 60 int rv = 0; 61 62 sc = dev->si_drv1; 63 bus = PWM_GET_BUS(sc->pdev); 64 if (bus == NULL) 65 return (EINVAL); 66 67 switch (cmd) { 68 case PWMMAXCHANNEL: 69 nchannel = -1; 70 rv = PWM_CHANNEL_MAX(sc->pdev, &nchannel); 71 bcopy(&nchannel, data, sizeof(nchannel)); 72 break; 73 case PWMSETSTATE: 74 bcopy(data, &state, sizeof(state)); 75 rv = PWMBUS_CHANNEL_CONFIG(bus, state.channel, 76 state.period, state.duty); 77 if (rv == 0) 78 rv = PWMBUS_CHANNEL_ENABLE(bus, state.channel, 79 state.enable); 80 break; 81 case PWMGETSTATE: 82 bcopy(data, &state, sizeof(state)); 83 rv = PWMBUS_CHANNEL_GET_CONFIG(bus, state.channel, 84 &state.period, &state.duty); 85 if (rv != 0) 86 return (rv); 87 rv = PWMBUS_CHANNEL_IS_ENABLED(bus, state.channel, 88 &state.enable); 89 if (rv != 0) 90 return (rv); 91 bcopy(&state, data, sizeof(state)); 92 break; 93 } 94 95 return (rv); 96 } 97 98 static struct cdevsw pwm_cdevsw = { 99 .d_version = D_VERSION, 100 .d_name = "pwm", 101 .d_ioctl = pwm_ioctl 102 }; 103 104 static int 105 pwmc_probe(device_t dev) 106 { 107 108 device_set_desc(dev, "PWM Controller"); 109 return (0); 110 } 111 112 static int 113 pwmc_attach(device_t dev) 114 { 115 struct pwmc_softc *sc; 116 struct make_dev_args args; 117 118 sc = device_get_softc(dev); 119 sc->dev = dev; 120 sc->pdev = device_get_parent(dev); 121 122 snprintf(sc->name, sizeof(sc->name), "pwmc%d", device_get_unit(dev)); 123 make_dev_args_init(&args); 124 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK; 125 args.mda_devsw = &pwm_cdevsw; 126 args.mda_uid = UID_ROOT; 127 args.mda_gid = GID_OPERATOR; 128 args.mda_mode = 0600; 129 args.mda_si_drv1 = sc; 130 if (make_dev_s(&args, &sc->pwm_dev, "%s", sc->name) != 0) { 131 device_printf(dev, "Failed to make PWM device\n"); 132 return (ENXIO); 133 } 134 return (0); 135 } 136 137 static int 138 pwmc_detach(device_t dev) 139 { 140 141 return (0); 142 } 143 144 static device_method_t pwmc_methods[] = { 145 /* device_if */ 146 DEVMETHOD(device_probe, pwmc_probe), 147 DEVMETHOD(device_attach, pwmc_attach), 148 DEVMETHOD(device_detach, pwmc_detach), 149 150 DEVMETHOD_END 151 }; 152 153 driver_t pwmc_driver = { 154 "pwmc", 155 pwmc_methods, 156 sizeof(struct pwmc_softc), 157 }; 158 devclass_t pwmc_devclass; 159 160 DRIVER_MODULE(pwmc, pwm, pwmc_driver, pwmc_devclass, 0, 0); 161 MODULE_VERSION(pwmc, 1); 162