xref: /freebsd/sys/arm/ti/am335x/am335x_ecap.c (revision 38a52bd3)
1 /*-
2  * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/lock.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/resource.h>
39 #include <sys/rman.h>
40 #include <sys/sysctl.h>
41 
42 #include <machine/bus.h>
43 
44 #include <dev/ofw/openfirm.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 
48 #include "am335x_pwm.h"
49 
50 #define	ECAP_TSCTR		0x00
51 #define	ECAP_CAP1		0x08
52 #define	ECAP_CAP2		0x0C
53 #define	ECAP_CAP3		0x10
54 #define	ECAP_CAP4		0x14
55 #define	ECAP_ECCTL2		0x2A
56 #define		ECCTL2_MODE_APWM		(1 << 9)
57 #define		ECCTL2_SYNCO_SEL		(3 << 6)
58 #define		ECCTL2_TSCTRSTOP_FREERUN	(1 << 4)
59 
60 #define	ECAP_READ2(_sc, reg)	bus_read_2((_sc)->sc_mem_res, reg);
61 #define	ECAP_WRITE2(_sc, reg, value)	\
62     bus_write_2((_sc)->sc_mem_res, reg, value);
63 #define	ECAP_READ4(_sc, reg)	bus_read_4((_sc)->sc_mem_res, reg);
64 #define	ECAP_WRITE4(_sc, reg, value)	\
65     bus_write_4((_sc)->sc_mem_res, reg, value);
66 
67 #define	PWM_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
68 #define	PWM_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
69 #define	PWM_LOCK_INIT(_sc)	mtx_init(&(_sc)->sc_mtx, \
70     device_get_nameunit(_sc->sc_dev), "am335x_ecap softc", MTX_DEF)
71 #define	PWM_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->sc_mtx)
72 
73 static device_probe_t am335x_ecap_probe;
74 static device_attach_t am335x_ecap_attach;
75 static device_detach_t am335x_ecap_detach;
76 
77 struct am335x_ecap_softc {
78 	device_t		sc_dev;
79 	struct mtx		sc_mtx;
80 	struct resource		*sc_mem_res;
81 	int			sc_mem_rid;
82 };
83 
84 static device_method_t am335x_ecap_methods[] = {
85 	DEVMETHOD(device_probe,		am335x_ecap_probe),
86 	DEVMETHOD(device_attach,	am335x_ecap_attach),
87 	DEVMETHOD(device_detach,	am335x_ecap_detach),
88 
89 	DEVMETHOD_END
90 };
91 
92 static driver_t am335x_ecap_driver = {
93 	"am335x_ecap",
94 	am335x_ecap_methods,
95 	sizeof(struct am335x_ecap_softc),
96 };
97 
98 /*
99  * API function to set period/duty cycles for ECAPx
100  */
101 int
102 am335x_pwm_config_ecap(int unit, int period, int duty)
103 {
104 	device_t dev;
105 	struct am335x_ecap_softc *sc;
106 	uint16_t reg;
107 
108 	dev = devclass_get_device(devclass_find(am335x_ecap_driver.name), unit);
109 	if (dev == NULL)
110 		return (ENXIO);
111 
112 	if (duty > period)
113 		return (EINVAL);
114 
115 	if (period == 0)
116 		return (EINVAL);
117 
118 	sc = device_get_softc(dev);
119 	PWM_LOCK(sc);
120 
121 	reg = ECAP_READ2(sc, ECAP_ECCTL2);
122 	reg |= ECCTL2_MODE_APWM | ECCTL2_TSCTRSTOP_FREERUN | ECCTL2_SYNCO_SEL;
123 	ECAP_WRITE2(sc, ECAP_ECCTL2, reg);
124 
125 	/* CAP3 in APWM mode is APRD shadow register */
126 	ECAP_WRITE4(sc, ECAP_CAP3, period - 1);
127 
128 	/* CAP4 in APWM mode is ACMP shadow register */
129 	ECAP_WRITE4(sc, ECAP_CAP4, duty);
130 	/* Restart counter */
131 	ECAP_WRITE4(sc, ECAP_TSCTR, 0);
132 
133 	PWM_UNLOCK(sc);
134 
135 	return (0);
136 }
137 
138 static int
139 am335x_ecap_probe(device_t dev)
140 {
141 
142 	if (!ofw_bus_status_okay(dev))
143 		return (ENXIO);
144 
145 	if (!ofw_bus_is_compatible(dev, "ti,am33xx-ecap"))
146 		return (ENXIO);
147 
148 	device_set_desc(dev, "AM335x eCAP");
149 
150 	return (BUS_PROBE_DEFAULT);
151 }
152 
153 static int
154 am335x_ecap_attach(device_t dev)
155 {
156 	struct am335x_ecap_softc *sc;
157 
158 	sc = device_get_softc(dev);
159 	sc->sc_dev = dev;
160 
161 	PWM_LOCK_INIT(sc);
162 
163 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
164 	    &sc->sc_mem_rid, RF_ACTIVE);
165 	if (sc->sc_mem_res == NULL) {
166 		device_printf(dev, "cannot allocate memory resources\n");
167 		goto fail;
168 	}
169 
170 	return (0);
171 
172 fail:
173 	PWM_LOCK_DESTROY(sc);
174 	return (ENXIO);
175 }
176 
177 static int
178 am335x_ecap_detach(device_t dev)
179 {
180 	struct am335x_ecap_softc *sc;
181 
182 	sc = device_get_softc(dev);
183 
184 	PWM_LOCK(sc);
185 	if (sc->sc_mem_res)
186 		bus_release_resource(dev, SYS_RES_MEMORY,
187 		    sc->sc_mem_rid, sc->sc_mem_res);
188 	PWM_UNLOCK(sc);
189 
190 	PWM_LOCK_DESTROY(sc);
191 
192 	return (0);
193 }
194 
195 DRIVER_MODULE(am335x_ecap, am335x_pwmss, am335x_ecap_driver, 0, 0);
196 MODULE_VERSION(am335x_ecap, 1);
197 MODULE_DEPEND(am335x_ecap, am335x_pwmss, 1, 1, 1);
198