2 * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/resource.h>
40 #include <sys/sysctl.h>
42 #include <machine/bus.h>
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include "am335x_pwm.h"
52 #define DEFAULT_PWM_PERIOD 1000
53 #define PWM_CLOCK 100000000UL
55 #define PWM_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
56 #define PWM_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
57 #define PWM_LOCK_INIT(_sc) mtx_init(&(_sc)->sc_mtx, \
58 device_get_nameunit(_sc->sc_dev), "am335x_ehrpwm softc", MTX_DEF)
59 #define PWM_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx)
61 #define EPWM_READ2(_sc, reg) bus_read_2((_sc)->sc_mem_res, reg);
62 #define EPWM_WRITE2(_sc, reg, value) \
63 bus_write_2((_sc)->sc_mem_res, reg, value);
65 #define EPWM_TBCTL 0x00
66 #define TBCTL_FREERUN (2 << 14)
67 #define TBCTL_PHDIR_UP (1 << 13)
68 #define TBCTL_PHDIR_DOWN (0 << 13)
69 #define TBCTL_CLKDIV(x) ((x) << 10)
70 #define TBCTL_CLKDIV_MASK (3 << 10)
71 #define TBCTL_HSPCLKDIV(x) ((x) << 7)
72 #define TBCTL_HSPCLKDIV_MASK (3 << 7)
73 #define TBCTL_SYNCOSEL_DISABLED (3 << 4)
74 #define TBCTL_PRDLD_SHADOW (0 << 3)
75 #define TBCTL_PRDLD_IMMEDIATE (0 << 3)
76 #define TBCTL_PHSEN_ENABLED (1 << 2)
77 #define TBCTL_PHSEN_DISABLED (0 << 2)
78 #define TBCTL_CTRMODE_MASK (3)
79 #define TBCTL_CTRMODE_UP (0 << 0)
80 #define TBCTL_CTRMODE_DOWN (1 << 0)
81 #define TBCTL_CTRMODE_UPDOWN (2 << 0)
82 #define TBCTL_CTRMODE_FREEZE (3 << 0)
84 #define EPWM_TBSTS 0x02
85 #define EPWM_TBPHSHR 0x04
86 #define EPWM_TBPHS 0x06
87 #define EPWM_TBCNT 0x08
88 #define EPWM_TBPRD 0x0a
90 #define EPWM_CMPCTL 0x0e
91 #define CMPCTL_SHDWBMODE_SHADOW (1 << 6)
92 #define CMPCTL_SHDWBMODE_IMMEDIATE (0 << 6)
93 #define CMPCTL_SHDWAMODE_SHADOW (1 << 4)
94 #define CMPCTL_SHDWAMODE_IMMEDIATE (0 << 4)
95 #define CMPCTL_LOADBMODE_ZERO (0 << 2)
96 #define CMPCTL_LOADBMODE_PRD (1 << 2)
97 #define CMPCTL_LOADBMODE_EITHER (2 << 2)
98 #define CMPCTL_LOADBMODE_FREEZE (3 << 2)
99 #define CMPCTL_LOADAMODE_ZERO (0 << 0)
100 #define CMPCTL_LOADAMODE_PRD (1 << 0)
101 #define CMPCTL_LOADAMODE_EITHER (2 << 0)
102 #define CMPCTL_LOADAMODE_FREEZE (3 << 0)
103 #define EPWM_CMPAHR 0x10
104 #define EPWM_CMPA 0x12
105 #define EPWM_CMPB 0x14
106 /* CMPCTL_LOADAMODE_ZERO */
107 #define EPWM_AQCTLA 0x16
108 #define EPWM_AQCTLB 0x18
109 #define AQCTL_CBU_NONE (0 << 8)
110 #define AQCTL_CBU_CLEAR (1 << 8)
111 #define AQCTL_CBU_SET (2 << 8)
112 #define AQCTL_CBU_TOGGLE (3 << 8)
113 #define AQCTL_CAU_NONE (0 << 4)
114 #define AQCTL_CAU_CLEAR (1 << 4)
115 #define AQCTL_CAU_SET (2 << 4)
116 #define AQCTL_CAU_TOGGLE (3 << 4)
117 #define AQCTL_ZRO_NONE (0 << 0)
118 #define AQCTL_ZRO_CLEAR (1 << 0)
119 #define AQCTL_ZRO_SET (2 << 0)
120 #define AQCTL_ZRO_TOGGLE (3 << 0)
121 #define EPWM_AQSFRC 0x1a
122 #define EPWM_AQCSFRC 0x1c
124 /* Trip-Zone module */
125 #define EPWM_TZCTL 0x28
126 #define EPWM_TZFLG 0x2C
127 /* High-Resolution PWM */
128 #define EPWM_HRCTL 0x40
129 #define HRCTL_DELMODE_BOTH 3
130 #define HRCTL_DELMODE_FALL 2
131 #define HRCTL_DELMODE_RISE 1
133 static device_probe_t am335x_ehrpwm_probe;
134 static device_attach_t am335x_ehrpwm_attach;
135 static device_detach_t am335x_ehrpwm_detach;
137 static int am335x_ehrpwm_clkdiv[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
139 struct am335x_ehrpwm_softc {
142 struct resource *sc_mem_res;
144 /* sysctl for configuration */
147 struct sysctl_oid *sc_clkdiv_oid;
148 struct sysctl_oid *sc_freq_oid;
149 struct sysctl_oid *sc_period_oid;
150 struct sysctl_oid *sc_chanA_oid;
151 struct sysctl_oid *sc_chanB_oid;
152 uint32_t sc_pwm_period;
153 uint32_t sc_pwm_dutyA;
154 uint32_t sc_pwm_dutyB;
157 static device_method_t am335x_ehrpwm_methods[] = {
158 DEVMETHOD(device_probe, am335x_ehrpwm_probe),
159 DEVMETHOD(device_attach, am335x_ehrpwm_attach),
160 DEVMETHOD(device_detach, am335x_ehrpwm_detach),
165 static driver_t am335x_ehrpwm_driver = {
167 am335x_ehrpwm_methods,
168 sizeof(struct am335x_ehrpwm_softc),
171 static devclass_t am335x_ehrpwm_devclass;
174 am335x_ehrpwm_freq(struct am335x_ehrpwm_softc *sc)
178 clkdiv = am335x_ehrpwm_clkdiv[sc->sc_pwm_clkdiv];
179 sc->sc_pwm_freq = PWM_CLOCK / (1 * clkdiv) / sc->sc_pwm_period;
183 am335x_ehrpwm_sysctl_freq(SYSCTL_HANDLER_ARGS)
185 int clkdiv, error, freq, i, period;
186 struct am335x_ehrpwm_softc *sc;
189 sc = (struct am335x_ehrpwm_softc *)arg1;
192 freq = sc->sc_pwm_freq;
195 error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
196 if (error != 0 || req->newptr == NULL)
199 if (freq > PWM_CLOCK)
203 if (freq != sc->sc_pwm_freq) {
204 for (i = nitems(am335x_ehrpwm_clkdiv) - 1; i >= 0; i--) {
205 clkdiv = am335x_ehrpwm_clkdiv[i];
206 period = PWM_CLOCK / clkdiv / freq;
207 if (period > USHRT_MAX)
209 sc->sc_pwm_clkdiv = i;
210 sc->sc_pwm_period = period;
212 /* Reset the duty cycle settings. */
213 sc->sc_pwm_dutyA = 0;
214 sc->sc_pwm_dutyB = 0;
215 EPWM_WRITE2(sc, EPWM_CMPA, sc->sc_pwm_dutyA);
216 EPWM_WRITE2(sc, EPWM_CMPB, sc->sc_pwm_dutyB);
217 /* Update the clkdiv settings. */
218 reg = EPWM_READ2(sc, EPWM_TBCTL);
219 reg &= ~TBCTL_CLKDIV_MASK;
220 reg |= TBCTL_CLKDIV(sc->sc_pwm_clkdiv);
221 EPWM_WRITE2(sc, EPWM_TBCTL, reg);
222 /* Update the period settings. */
223 EPWM_WRITE2(sc, EPWM_TBPRD, sc->sc_pwm_period - 1);
224 am335x_ehrpwm_freq(sc);
232 am335x_ehrpwm_sysctl_clkdiv(SYSCTL_HANDLER_ARGS)
234 int error, i, clkdiv;
235 struct am335x_ehrpwm_softc *sc;
238 sc = (struct am335x_ehrpwm_softc *)arg1;
241 clkdiv = am335x_ehrpwm_clkdiv[sc->sc_pwm_clkdiv];
244 error = sysctl_handle_int(oidp, &clkdiv, sizeof(clkdiv), req);
245 if (error != 0 || req->newptr == NULL)
249 if (clkdiv != am335x_ehrpwm_clkdiv[sc->sc_pwm_clkdiv]) {
250 for (i = 0; i < nitems(am335x_ehrpwm_clkdiv); i++)
251 if (clkdiv >= am335x_ehrpwm_clkdiv[i])
252 sc->sc_pwm_clkdiv = i;
254 reg = EPWM_READ2(sc, EPWM_TBCTL);
255 reg &= ~TBCTL_CLKDIV_MASK;
256 reg |= TBCTL_CLKDIV(sc->sc_pwm_clkdiv);
257 EPWM_WRITE2(sc, EPWM_TBCTL, reg);
258 am335x_ehrpwm_freq(sc);
266 am335x_ehrpwm_sysctl_duty(SYSCTL_HANDLER_ARGS)
268 struct am335x_ehrpwm_softc *sc = (struct am335x_ehrpwm_softc*)arg1;
272 if (oidp == sc->sc_chanA_oid)
273 duty = sc->sc_pwm_dutyA;
275 duty = sc->sc_pwm_dutyB;
276 error = sysctl_handle_int(oidp, &duty, 0, req);
278 if (error != 0 || req->newptr == NULL)
281 if (duty > sc->sc_pwm_period) {
282 device_printf(sc->sc_dev, "Duty cycle can't be greater then period\n");
287 if (oidp == sc->sc_chanA_oid) {
288 sc->sc_pwm_dutyA = duty;
289 EPWM_WRITE2(sc, EPWM_CMPA, sc->sc_pwm_dutyA);
292 sc->sc_pwm_dutyB = duty;
293 EPWM_WRITE2(sc, EPWM_CMPB, sc->sc_pwm_dutyB);
301 am335x_ehrpwm_sysctl_period(SYSCTL_HANDLER_ARGS)
303 struct am335x_ehrpwm_softc *sc = (struct am335x_ehrpwm_softc*)arg1;
307 period = sc->sc_pwm_period;
308 error = sysctl_handle_int(oidp, &period, 0, req);
310 if (error != 0 || req->newptr == NULL)
316 if (period > USHRT_MAX)
320 /* Reset the duty cycle settings. */
321 sc->sc_pwm_dutyA = 0;
322 sc->sc_pwm_dutyB = 0;
323 EPWM_WRITE2(sc, EPWM_CMPA, sc->sc_pwm_dutyA);
324 EPWM_WRITE2(sc, EPWM_CMPB, sc->sc_pwm_dutyB);
325 /* Update the period settings. */
326 sc->sc_pwm_period = period;
327 EPWM_WRITE2(sc, EPWM_TBPRD, period - 1);
328 am335x_ehrpwm_freq(sc);
335 am335x_ehrpwm_probe(device_t dev)
338 if (!ofw_bus_status_okay(dev))
341 if (!ofw_bus_is_compatible(dev, "ti,am33xx-ehrpwm"))
344 device_set_desc(dev, "AM335x EHRPWM");
346 return (BUS_PROBE_DEFAULT);
350 am335x_ehrpwm_attach(device_t dev)
352 struct am335x_ehrpwm_softc *sc;
354 struct sysctl_ctx_list *ctx;
355 struct sysctl_oid *tree;
357 sc = device_get_softc(dev);
362 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
363 &sc->sc_mem_rid, RF_ACTIVE);
364 if (sc->sc_mem_res == NULL) {
365 device_printf(dev, "cannot allocate memory resources\n");
369 /* Init backlight interface */
370 ctx = device_get_sysctl_ctx(sc->sc_dev);
371 tree = device_get_sysctl_tree(sc->sc_dev);
373 sc->sc_clkdiv_oid = SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
374 "clkdiv", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
375 am335x_ehrpwm_sysctl_clkdiv, "I", "PWM clock prescaler");
377 sc->sc_freq_oid = SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
378 "freq", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
379 am335x_ehrpwm_sysctl_freq, "I", "PWM frequency");
381 sc->sc_period_oid = SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
382 "period", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
383 am335x_ehrpwm_sysctl_period, "I", "PWM period");
385 sc->sc_chanA_oid = SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
386 "dutyA", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
387 am335x_ehrpwm_sysctl_duty, "I", "Channel A duty cycles");
389 sc->sc_chanB_oid = SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
390 "dutyB", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
391 am335x_ehrpwm_sysctl_duty, "I", "Channel B duty cycles");
393 /* CONFIGURE EPWM1 */
394 reg = EPWM_READ2(sc, EPWM_TBCTL);
395 reg &= ~(TBCTL_CLKDIV_MASK | TBCTL_HSPCLKDIV_MASK);
396 EPWM_WRITE2(sc, EPWM_TBCTL, reg);
398 sc->sc_pwm_period = DEFAULT_PWM_PERIOD;
399 sc->sc_pwm_dutyA = 0;
400 sc->sc_pwm_dutyB = 0;
401 am335x_ehrpwm_freq(sc);
403 EPWM_WRITE2(sc, EPWM_TBPRD, sc->sc_pwm_period - 1);
404 EPWM_WRITE2(sc, EPWM_CMPA, sc->sc_pwm_dutyA);
405 EPWM_WRITE2(sc, EPWM_CMPB, sc->sc_pwm_dutyB);
407 EPWM_WRITE2(sc, EPWM_AQCTLA, (AQCTL_ZRO_SET | AQCTL_CAU_CLEAR));
408 EPWM_WRITE2(sc, EPWM_AQCTLB, (AQCTL_ZRO_SET | AQCTL_CBU_CLEAR));
411 reg &= ~TBCTL_CTRMODE_MASK;
412 reg |= TBCTL_CTRMODE_UP | TBCTL_FREERUN;
413 EPWM_WRITE2(sc, EPWM_TBCTL, reg);
415 EPWM_WRITE2(sc, EPWM_TZCTL, 0xf);
416 reg = EPWM_READ2(sc, EPWM_TZFLG);
420 PWM_LOCK_DESTROY(sc);
422 bus_release_resource(dev, SYS_RES_MEMORY,
423 sc->sc_mem_rid, sc->sc_mem_res);
429 am335x_ehrpwm_detach(device_t dev)
431 struct am335x_ehrpwm_softc *sc;
433 sc = device_get_softc(dev);
437 bus_release_resource(dev, SYS_RES_MEMORY,
438 sc->sc_mem_rid, sc->sc_mem_res);
441 PWM_LOCK_DESTROY(sc);
446 DRIVER_MODULE(am335x_ehrpwm, am335x_pwmss, am335x_ehrpwm_driver, am335x_ehrpwm_devclass, 0, 0);
447 MODULE_VERSION(am335x_ehrpwm, 1);
448 MODULE_DEPEND(am335x_ehrpwm, am335x_pwmss, 1, 1, 1);