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/endian.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 43 #include <dev/pwm/pwmbus.h> 44 45 #include "pwmbus_if.h" 46 47 /* 48 * bus_if methods... 49 */ 50 51 static device_t 52 pwmbus_add_child(device_t dev, u_int order, const char *name, int unit) 53 { 54 device_t child; 55 struct pwmbus_ivars *ivars; 56 57 child = device_add_child_ordered(dev, order, name, unit); 58 if (child == NULL) 59 return (child); 60 61 ivars = malloc(sizeof(struct pwmbus_ivars), M_DEVBUF, M_NOWAIT | M_ZERO); 62 if (ivars == NULL) { 63 device_delete_child(dev, child); 64 return (NULL); 65 } 66 device_set_ivars(child, ivars); 67 68 return (child); 69 } 70 71 static int 72 pwmbus_child_location_str(device_t dev, device_t child, char *buf, size_t blen) 73 { 74 struct pwmbus_ivars *ivars; 75 76 ivars = device_get_ivars(child); 77 snprintf(buf, blen, "hwdev=%s channel=%u", 78 device_get_nameunit(device_get_parent(dev)), ivars->pi_channel); 79 80 return (0); 81 } 82 83 static int 84 pwmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf, 85 size_t buflen) 86 { 87 *buf = '\0'; 88 return (0); 89 } 90 91 static void 92 pwmbus_hinted_child(device_t dev, const char *dname, int dunit) 93 { 94 struct pwmbus_ivars *ivars; 95 device_t child; 96 97 child = pwmbus_add_child(dev, 0, dname, dunit); 98 99 /* 100 * If there is a channel hint, use it. Otherwise pi_channel was 101 * initialized to zero, so that's the channel we'll use. 102 */ 103 ivars = device_get_ivars(child); 104 resource_int_value(dname, dunit, "channel", &ivars->pi_channel); 105 } 106 107 static int 108 pwmbus_print_child(device_t dev, device_t child) 109 { 110 struct pwmbus_ivars *ivars; 111 int rv; 112 113 ivars = device_get_ivars(child); 114 115 rv = bus_print_child_header(dev, child); 116 rv += printf(" channel %u", ivars->pi_channel); 117 rv += bus_print_child_footer(dev, child); 118 119 return (rv); 120 } 121 122 static void 123 pwmbus_probe_nomatch(device_t dev, device_t child) 124 { 125 struct pwmbus_ivars *ivars; 126 127 ivars = device_get_ivars(child); 128 if (ivars != NULL) 129 device_printf(dev, "<unknown> on channel %u\n", 130 ivars->pi_channel); 131 132 return; 133 } 134 135 static int 136 pwmbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 137 { 138 struct pwmbus_ivars *ivars; 139 140 ivars = device_get_ivars(child); 141 142 switch (which) { 143 case PWMBUS_IVAR_CHANNEL: 144 *(u_int *)result = ivars->pi_channel; 145 break; 146 default: 147 return (EINVAL); 148 } 149 150 return (0); 151 } 152 153 /* 154 * device_if methods... 155 */ 156 157 static int 158 pwmbus_probe(device_t dev) 159 { 160 device_set_desc(dev, "PWM bus"); 161 return (BUS_PROBE_GENERIC); 162 } 163 164 static int 165 pwmbus_attach(device_t dev) 166 { 167 struct pwmbus_softc *sc; 168 struct pwmbus_ivars *ivars; 169 device_t child, parent; 170 u_int chan; 171 172 sc = device_get_softc(dev); 173 sc->dev = dev; 174 parent = device_get_parent(dev); 175 176 if (PWMBUS_CHANNEL_COUNT(parent, &sc->nchannels) != 0 || 177 sc->nchannels == 0) { 178 device_printf(sc->dev, "No channels on parent %s\n", 179 device_get_nameunit(parent)); 180 return (ENXIO); 181 } 182 183 /* Add a pwmc(4) child for each channel. */ 184 for (chan = 0; chan < sc->nchannels; ++chan) { 185 if ((child = pwmbus_add_child(sc->dev, 0, "pwmc", -1)) == NULL) { 186 device_printf(dev, "failed to add pwmc child device " 187 "for channel %u\n", chan); 188 continue; 189 } 190 ivars = device_get_ivars(child); 191 ivars->pi_channel = chan; 192 } 193 194 bus_enumerate_hinted_children(dev); 195 bus_generic_probe(dev); 196 197 return (bus_generic_attach(dev)); 198 } 199 200 static int 201 pwmbus_detach(device_t dev) 202 { 203 int rv; 204 205 if ((rv = bus_generic_detach(dev)) == 0) 206 rv = device_delete_children(dev); 207 208 return (rv); 209 } 210 211 /* 212 * pwmbus_if methods... 213 */ 214 215 static int 216 pwmbus_channel_config(device_t dev, u_int chan, u_int period, u_int duty) 217 { 218 return (PWMBUS_CHANNEL_CONFIG(device_get_parent(dev), chan, period, duty)); 219 } 220 221 static int 222 pwmbus_channel_get_config(device_t dev, u_int chan, u_int *period, u_int *duty) 223 { 224 return (PWMBUS_CHANNEL_GET_CONFIG(device_get_parent(dev), chan, period, duty)); 225 } 226 227 static int 228 pwmbus_channel_get_flags(device_t dev, u_int chan, uint32_t *flags) 229 { 230 return (PWMBUS_CHANNEL_GET_FLAGS(device_get_parent(dev), chan, flags)); 231 } 232 233 static int 234 pwmbus_channel_enable(device_t dev, u_int chan, bool enable) 235 { 236 return (PWMBUS_CHANNEL_ENABLE(device_get_parent(dev), chan, enable)); 237 } 238 239 static int 240 pwmbus_channel_set_flags(device_t dev, u_int chan, uint32_t flags) 241 { 242 return (PWMBUS_CHANNEL_SET_FLAGS(device_get_parent(dev), chan, flags)); 243 } 244 245 static int 246 pwmbus_channel_is_enabled(device_t dev, u_int chan, bool *enable) 247 { 248 return (PWMBUS_CHANNEL_IS_ENABLED(device_get_parent(dev), chan, enable)); 249 } 250 251 static int 252 pwmbus_channel_count(device_t dev, u_int *nchannel) 253 { 254 return (PWMBUS_CHANNEL_COUNT(device_get_parent(dev), nchannel)); 255 } 256 257 static device_method_t pwmbus_methods[] = { 258 /* device_if */ 259 DEVMETHOD(device_probe, pwmbus_probe), 260 DEVMETHOD(device_attach, pwmbus_attach), 261 DEVMETHOD(device_detach, pwmbus_detach), 262 263 /* bus_if */ 264 DEVMETHOD(bus_add_child, pwmbus_add_child), 265 DEVMETHOD(bus_child_location_str, pwmbus_child_location_str), 266 DEVMETHOD(bus_child_pnpinfo_str, pwmbus_child_pnpinfo_str), 267 DEVMETHOD(bus_hinted_child, pwmbus_hinted_child), 268 DEVMETHOD(bus_print_child, pwmbus_print_child), 269 DEVMETHOD(bus_probe_nomatch, pwmbus_probe_nomatch), 270 DEVMETHOD(bus_read_ivar, pwmbus_read_ivar), 271 272 /* pwmbus_if */ 273 DEVMETHOD(pwmbus_channel_count, pwmbus_channel_count), 274 DEVMETHOD(pwmbus_channel_config, pwmbus_channel_config), 275 DEVMETHOD(pwmbus_channel_get_config, pwmbus_channel_get_config), 276 DEVMETHOD(pwmbus_channel_set_flags, pwmbus_channel_set_flags), 277 DEVMETHOD(pwmbus_channel_get_flags, pwmbus_channel_get_flags), 278 DEVMETHOD(pwmbus_channel_enable, pwmbus_channel_enable), 279 DEVMETHOD(pwmbus_channel_is_enabled, pwmbus_channel_is_enabled), 280 281 DEVMETHOD_END 282 }; 283 284 driver_t pwmbus_driver = { 285 "pwmbus", 286 pwmbus_methods, 287 sizeof(struct pwmbus_softc), 288 }; 289 devclass_t pwmbus_devclass; 290 291 EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, pwmbus_devclass, 0, 0, 292 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 293 MODULE_VERSION(pwmbus, 1); 294