2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
5 * Copyright (c) 2019 Brandon Bergren <git@bdragon.rtk0.net>
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 ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * 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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/resource.h>
41 #include <machine/bus.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/extres/clk/clk.h>
48 #include "pwmbus_if.h"
50 /* Register offsets. */
51 #define RK_PWM_COUNTER 0x00
52 #define RK_PWM_PERIOD 0x04
53 #define RK_PWM_DUTY 0x08
54 #define RK_PWM_CTRL 0x0c
56 #define SET(reg,mask,val) reg = ((reg & ~mask) | val)
58 #define RK_PWM_CTRL_ENABLE_MASK (1 << 0)
59 #define RK_PWM_CTRL_ENABLED (1 << 0)
60 #define RK_PWM_CTRL_DISABLED (0)
62 #define RK_PWM_CTRL_MODE_MASK (3 << 1)
63 #define RK_PWM_CTRL_MODE_ONESHOT (0)
64 #define RK_PWM_CTRL_MODE_CONTINUOUS (1 << 1)
65 #define RK_PWM_CTRL_MODE_CAPTURE (1 << 2)
67 #define RK_PWM_CTRL_DUTY_MASK (1 << 3)
68 #define RK_PWM_CTRL_DUTY_POSITIVE (1 << 3)
69 #define RK_PWM_CTRL_DUTY_NEGATIVE (0)
71 #define RK_PWM_CTRL_INACTIVE_MASK (1 << 4)
72 #define RK_PWM_CTRL_INACTIVE_POSITIVE (1 << 4)
73 #define RK_PWM_CTRL_INACTIVE_NEGATIVE (0)
75 /* PWM Output Alignment */
76 #define RK_PWM_CTRL_ALIGN_MASK (1 << 5)
77 #define RK_PWM_CTRL_ALIGN_CENTER (1 << 5)
78 #define RK_PWM_CTRL_ALIGN_LEFT (0)
80 /* Low power mode: disable prescaler when inactive */
81 #define RK_PWM_CTRL_LP_MASK (1 << 8)
82 #define RK_PWM_CTRL_LP_ENABLE (1 << 8)
83 #define RK_PWM_CTRL_LP_DISABLE (0)
85 /* Clock source: bypass the scaler or not */
86 #define RK_PWM_CTRL_CLOCKSRC_MASK (1 << 9)
87 #define RK_PWM_CTRL_CLOCKSRC_NONSCALED (0)
88 #define RK_PWM_CTRL_CLOCKSRC_SCALED (1 << 9)
90 #define RK_PWM_CTRL_PRESCALE_MASK (7 << 12)
91 #define RK_PWM_CTRL_PRESCALE_SHIFT 12
93 #define RK_PWM_CTRL_SCALE_MASK (0xFF << 16)
94 #define RK_PWM_CTRL_SCALE_SHIFT 16
96 #define RK_PWM_CTRL_REPEAT_MASK (0xFF << 24)
97 #define RK_PWM_CTRL_REPEAT_SHIFT 24
99 #define NS_PER_SEC 1000000000
101 static struct ofw_compat_data compat_data[] = {
102 { "rockchip,rk3399-pwm", 1 },
106 static struct resource_spec rk_pwm_spec[] = {
107 { SYS_RES_MEMORY, 0, RF_ACTIVE },
111 struct rk_pwm_softc {
115 struct resource *res;
127 #define RK_PWM_READ(sc, reg) bus_read_4((sc)->res, (reg))
128 #define RK_PWM_WRITE(sc, reg, val) bus_write_4((sc)->res, (reg), (val))
130 static int rk_pwm_probe(device_t dev);
131 static int rk_pwm_attach(device_t dev);
132 static int rk_pwm_detach(device_t dev);
135 rk_pwm_probe(device_t dev)
137 if (!ofw_bus_status_okay(dev))
140 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
143 device_set_desc(dev, "Rockchip PWM");
144 return (BUS_PROBE_DEFAULT);
148 rk_pwm_attach(device_t dev)
150 struct rk_pwm_softc *sc;
156 sc = device_get_softc(dev);
159 error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
161 device_printf(dev, "cannot get clock\n");
164 error = clk_enable(sc->clk);
166 device_printf(dev, "cannot enable clock\n");
169 error = clk_get_freq(sc->clk, &sc->clk_freq);
171 device_printf(dev, "cannot get base frequency\n");
175 if (bus_alloc_resources(dev, rk_pwm_spec, &sc->res) != 0) {
176 device_printf(dev, "cannot allocate resources for device\n");
181 /* Read the configuration left by U-Boot */
182 reg = RK_PWM_READ(sc, RK_PWM_CTRL);
183 if ((reg & RK_PWM_CTRL_ENABLE_MASK) == RK_PWM_CTRL_ENABLED)
186 reg = RK_PWM_READ(sc, RK_PWM_CTRL);
187 reg &= RK_PWM_CTRL_PRESCALE_MASK;
188 sc->prescaler = reg >> RK_PWM_CTRL_PRESCALE_SHIFT;
190 reg = RK_PWM_READ(sc, RK_PWM_CTRL);
191 reg &= RK_PWM_CTRL_SCALE_MASK;
192 sc->scaler = reg >> RK_PWM_CTRL_SCALE_SHIFT;
194 reg = RK_PWM_READ(sc, RK_PWM_CTRL);
195 if ((reg & RK_PWM_CTRL_CLOCKSRC_MASK) == RK_PWM_CTRL_CLOCKSRC_SCALED)
196 sc->using_scaler = true;
198 sc->using_scaler = false;
200 clk_freq = sc->clk_freq / (2 ^ sc->prescaler);
202 if (sc->using_scaler) {
206 clk_freq /= (sc->scaler * 2);
209 reg = RK_PWM_READ(sc, RK_PWM_PERIOD);
210 sc->period = NS_PER_SEC /
212 reg = RK_PWM_READ(sc, RK_PWM_DUTY);
213 sc->duty = NS_PER_SEC /
216 node = ofw_bus_get_node(dev);
217 OF_device_register_xref(OF_xref_from_node(node), dev);
219 sc->busdev = device_add_child(dev, "pwmbus", -1);
221 return (bus_generic_attach(dev));
229 rk_pwm_detach(device_t dev)
231 struct rk_pwm_softc *sc;
233 sc = device_get_softc(dev);
235 bus_generic_detach(sc->dev);
237 bus_release_resources(dev, rk_pwm_spec, &sc->res);
243 aw_pwm_get_node(device_t bus, device_t dev)
247 * Share our controller node with our pwmbus child; it instantiates
248 * devices by walking the children contained within our node.
250 return ofw_bus_get_node(bus);
254 rk_pwm_channel_count(device_t dev, u_int *nchannel)
256 /* The device supports 4 channels, but attaches multiple times in the
257 * device tree. This interferes with advanced usage though, as
258 * the interrupt capability and channel 3 FIFO register offsets
259 * don't work right in this situation.
260 * But since we don't support those yet, pretend we are singlechannel.
268 rk_pwm_channel_config(device_t dev, u_int channel, u_int period, u_int duty)
270 struct rk_pwm_softc *sc;
271 uint64_t period_freq, duty_freq;
279 sc = device_get_softc(dev);
281 period_freq = NS_PER_SEC / period;
282 /* Datasheet doesn't define, so use Nyquist frequency. */
283 if (period_freq > (sc->clk_freq / 2))
285 duty_freq = NS_PER_SEC / duty;
286 if (duty_freq < period_freq) {
287 device_printf(sc->dev, "duty < period\n");
291 /* Assuming 24 MHz reference, we should never actually have
292 to use the divider due to pwm API limitations. */
295 using_scaler = false;
297 /* XXX Expand API to allow for 64 bit period/duty. */
298 period_out = (sc->clk_freq * period) / NS_PER_SEC;
299 duty_out = (sc->clk_freq * duty) / NS_PER_SEC;
301 reg = RK_PWM_READ(sc, RK_PWM_CTRL);
303 if ((reg & RK_PWM_CTRL_MODE_MASK) != RK_PWM_CTRL_MODE_CONTINUOUS) {
304 /* Switching modes, disable just in case. */
305 SET(reg, RK_PWM_CTRL_ENABLE_MASK, RK_PWM_CTRL_DISABLED);
306 RK_PWM_WRITE(sc, RK_PWM_CTRL, reg);
309 RK_PWM_WRITE(sc, RK_PWM_PERIOD, period_out);
310 RK_PWM_WRITE(sc, RK_PWM_DUTY, duty_out);
312 SET(reg, RK_PWM_CTRL_ENABLE_MASK, RK_PWM_CTRL_ENABLED);
313 SET(reg, RK_PWM_CTRL_MODE_MASK, RK_PWM_CTRL_MODE_CONTINUOUS);
314 SET(reg, RK_PWM_CTRL_ALIGN_MASK, RK_PWM_CTRL_ALIGN_LEFT);
315 SET(reg, RK_PWM_CTRL_CLOCKSRC_MASK, using_scaler);
316 SET(reg, RK_PWM_CTRL_PRESCALE_MASK,
317 prescaler << RK_PWM_CTRL_PRESCALE_SHIFT);
318 SET(reg, RK_PWM_CTRL_SCALE_MASK,
319 scaler << RK_PWM_CTRL_SCALE_SHIFT);
321 RK_PWM_WRITE(sc, RK_PWM_CTRL, reg);
330 rk_pwm_channel_get_config(device_t dev, u_int channel, u_int *period, u_int *duty)
332 struct rk_pwm_softc *sc;
334 sc = device_get_softc(dev);
336 *period = sc->period;
343 rk_pwm_channel_enable(device_t dev, u_int channel, bool enable)
345 struct rk_pwm_softc *sc;
348 sc = device_get_softc(dev);
350 if (enable && sc->enabled)
353 reg = RK_PWM_READ(sc, RK_PWM_CTRL);
354 SET(reg, RK_PWM_CTRL_ENABLE_MASK, enable);
356 RK_PWM_WRITE(sc, RK_PWM_CTRL, reg);
358 sc->enabled = enable;
364 rk_pwm_channel_is_enabled(device_t dev, u_int channel, bool *enabled)
366 struct rk_pwm_softc *sc;
368 sc = device_get_softc(dev);
370 *enabled = sc->enabled;
375 static device_method_t rk_pwm_methods[] = {
376 /* Device interface */
377 DEVMETHOD(device_probe, rk_pwm_probe),
378 DEVMETHOD(device_attach, rk_pwm_attach),
379 DEVMETHOD(device_detach, rk_pwm_detach),
381 /* ofw_bus interface */
382 DEVMETHOD(ofw_bus_get_node, aw_pwm_get_node),
385 DEVMETHOD(pwmbus_channel_count, rk_pwm_channel_count),
386 DEVMETHOD(pwmbus_channel_config, rk_pwm_channel_config),
387 DEVMETHOD(pwmbus_channel_get_config, rk_pwm_channel_get_config),
388 DEVMETHOD(pwmbus_channel_enable, rk_pwm_channel_enable),
389 DEVMETHOD(pwmbus_channel_is_enabled, rk_pwm_channel_is_enabled),
394 static driver_t rk_pwm_driver = {
397 sizeof(struct rk_pwm_softc),
400 static devclass_t rk_pwm_devclass;
402 DRIVER_MODULE(rk_pwm, simplebus, rk_pwm_driver, rk_pwm_devclass, 0, 0);
403 SIMPLEBUS_PNP_INFO(compat_data);