2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Poul-Henning Kamp <phk@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <sys/sysctl.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include <arm/broadcom/bcm2835/bcm2835_clkman.h>
51 static struct ofw_compat_data compat_data[] = {
52 {"broadcom,bcm2835-pwm", 1},
53 {"brcm,bcm2835-pwm", 1},
57 struct bcm_pwm_softc {
60 struct resource * sc_mem_res;
61 bus_space_tag_t sc_m_bst;
62 bus_space_handle_t sc_m_bsh;
73 #define BCM_PWM_MEM_WRITE(_sc, _off, _val) \
74 bus_space_write_4(_sc->sc_m_bst, _sc->sc_m_bsh, _off, _val)
75 #define BCM_PWM_MEM_READ(_sc, _off) \
76 bus_space_read_4(_sc->sc_m_bst, _sc->sc_m_bsh, _off)
77 #define BCM_PWM_CLK_WRITE(_sc, _off, _val) \
78 bus_space_write_4(_sc->sc_c_bst, _sc->sc_c_bsh, _off, _val)
79 #define BCM_PWM_CLK_READ(_sc, _off) \
80 bus_space_read_4(_sc->sc_c_bst, _sc->sc_c_bsh, _off)
82 #define W_CTL(_sc, _val) BCM_PWM_MEM_WRITE(_sc, 0x00, _val)
83 #define R_CTL(_sc) BCM_PWM_MEM_READ(_sc, 0x00)
84 #define W_STA(_sc, _val) BCM_PWM_MEM_WRITE(_sc, 0x04, _val)
85 #define R_STA(_sc) BCM_PWM_MEM_READ(_sc, 0x04)
86 #define W_RNG(_sc, _val) BCM_PWM_MEM_WRITE(_sc, 0x10, _val)
87 #define R_RNG(_sc) BCM_PWM_MEM_READ(_sc, 0x10)
88 #define W_DAT(_sc, _val) BCM_PWM_MEM_WRITE(_sc, 0x14, _val)
89 #define R_DAT(_sc) BCM_PWM_MEM_READ(_sc, 0x14)
92 bcm_pwm_reconf(struct bcm_pwm_softc *sc)
100 (void)bcm2835_clkman_set_frequency(sc->clkman, BCM_PWM_CLKSRC, 0);
105 u = bcm2835_clkman_set_frequency(sc->clkman, BCM_PWM_CLKSRC, sc->freq);
111 W_RNG(sc, sc->period);
112 if (sc->ratio > sc->period)
113 sc->ratio = sc->period;
114 W_DAT(sc, sc->ratio);
126 bcm_pwm_pwm_freq_proc(SYSCTL_HANDLER_ARGS)
128 struct bcm_pwm_softc *sc;
132 sc = (struct bcm_pwm_softc *)arg1;
134 r = sc->freq / sc->period;
137 error = sysctl_handle_int(oidp, &r, sizeof(r), req);
143 bcm_pwm_mode_proc(SYSCTL_HANDLER_ARGS)
145 struct bcm_pwm_softc *sc;
149 sc = (struct bcm_pwm_softc *)arg1;
151 error = sysctl_handle_int(oidp, &r, sizeof(r), req);
152 if (error != 0 || req->newptr == NULL)
157 return (bcm_pwm_reconf(sc));
161 bcm_pwm_freq_proc(SYSCTL_HANDLER_ARGS)
163 struct bcm_pwm_softc *sc;
167 sc = (struct bcm_pwm_softc *)arg1;
169 error = sysctl_handle_int(oidp, &r, sizeof(r), req);
170 if (error != 0 || req->newptr == NULL)
175 return (bcm_pwm_reconf(sc));
179 bcm_pwm_period_proc(SYSCTL_HANDLER_ARGS)
181 struct bcm_pwm_softc *sc;
184 sc = (struct bcm_pwm_softc *)arg1;
185 error = sysctl_handle_int(oidp, &sc->period, sizeof(sc->period), req);
186 if (error != 0 || req->newptr == NULL)
188 return (bcm_pwm_reconf(sc));
192 bcm_pwm_ratio_proc(SYSCTL_HANDLER_ARGS)
194 struct bcm_pwm_softc *sc;
198 sc = (struct bcm_pwm_softc *)arg1;
200 error = sysctl_handle_int(oidp, &r, sizeof(r), req);
201 if (error != 0 || req->newptr == NULL)
203 if (r > sc->period) // XXX >= ?
206 BCM_PWM_MEM_WRITE(sc, 0x14, sc->ratio);
211 bcm_pwm_reg_proc(SYSCTL_HANDLER_ARGS)
213 struct bcm_pwm_softc *sc;
217 sc = (struct bcm_pwm_softc *)arg1;
218 reg = BCM_PWM_MEM_READ(sc, arg2 & 0xff);
220 error = sysctl_handle_int(oidp, ®, sizeof(reg), req);
221 if (error != 0 || req->newptr == NULL)
224 BCM_PWM_MEM_WRITE(sc, arg2, reg);
229 bcm_pwm_sysctl_init(struct bcm_pwm_softc *sc)
231 struct sysctl_ctx_list *ctx;
232 struct sysctl_oid *tree_node;
233 struct sysctl_oid_list *tree;
236 * Add system sysctl tree/handlers.
238 ctx = device_get_sysctl_ctx(sc->sc_dev);
239 tree_node = device_get_sysctl_tree(sc->sc_dev);
240 tree = SYSCTL_CHILDREN(tree_node);
243 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, y, \
244 CTLFLAG_RW | CTLTYPE_UINT, sc, 0x##x, \
245 bcm_pwm_reg_proc, "IU", "Register 0x" #x " " y);
258 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "pwm_freq",
259 CTLFLAG_RD | CTLTYPE_UINT, sc, 0,
260 bcm_pwm_pwm_freq_proc, "IU", "PWM frequency (Hz)");
261 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "period",
262 CTLFLAG_RW | CTLTYPE_UINT, sc, 0,
263 bcm_pwm_period_proc, "IU", "PWM period (#clocks)");
264 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "ratio",
265 CTLFLAG_RW | CTLTYPE_UINT, sc, 0,
266 bcm_pwm_ratio_proc, "IU", "PWM ratio (0...period)");
267 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "freq",
268 CTLFLAG_RW | CTLTYPE_UINT, sc, 0,
269 bcm_pwm_freq_proc, "IU", "PWM clock (Hz)");
270 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "mode",
271 CTLFLAG_RW | CTLTYPE_UINT, sc, 0,
272 bcm_pwm_mode_proc, "IU", "PWM mode (0=off, 1=pwm, 2=dither)");
276 bcm_pwm_probe(device_t dev)
279 if (!ofw_bus_status_okay(dev))
282 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
285 device_set_desc(dev, "BCM2708/2835 PWM controller");
287 return (BUS_PROBE_DEFAULT);
291 bcm_pwm_attach(device_t dev)
293 struct bcm_pwm_softc *sc;
296 if (device_get_unit(dev) != 0) {
297 device_printf(dev, "only one PWM controller supported\n");
301 sc = device_get_softc(dev);
304 sc->clkman = devclass_get_device(devclass_find("bcm2835_clkman"), 0);
305 if (sc->clkman == NULL) {
306 device_printf(dev, "cannot find Clock Manager\n");
311 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
313 if (!sc->sc_mem_res) {
314 device_printf(dev, "cannot allocate memory window\n");
318 sc->sc_m_bst = rman_get_bustag(sc->sc_mem_res);
319 sc->sc_m_bsh = rman_get_bushandle(sc->sc_mem_res);
321 /* Add sysctl nodes. */
322 bcm_pwm_sysctl_init(sc);
324 sc->freq = 125000000;
329 return (bus_generic_attach(dev));
333 bcm_pwm_detach(device_t dev)
335 struct bcm_pwm_softc *sc;
337 bus_generic_detach(dev);
339 sc = device_get_softc(dev);
341 (void)bcm_pwm_reconf(sc);
343 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
349 bcm_pwm_get_node(device_t bus, device_t dev)
352 return (ofw_bus_get_node(bus));
356 static device_method_t bcm_pwm_methods[] = {
357 /* Device interface */
358 DEVMETHOD(device_probe, bcm_pwm_probe),
359 DEVMETHOD(device_attach, bcm_pwm_attach),
360 DEVMETHOD(device_detach, bcm_pwm_detach),
361 DEVMETHOD(ofw_bus_get_node, bcm_pwm_get_node),
366 static devclass_t bcm_pwm_devclass;
368 static driver_t bcm_pwm_driver = {
371 sizeof(struct bcm_pwm_softc),
374 DRIVER_MODULE(bcm2835_pwm, simplebus, bcm_pwm_driver, bcm_pwm_devclass, 0, 0);
375 MODULE_DEPEND(bcm2835_pwm, bcm2835_clkman, 1, 1, 1);