xref: /freebsd/sys/arm/ti/am335x/am335x_ecap.c (revision 325151a3)
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/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>
48 
49 #include "am335x_pwm.h"
50 
51 #define	ECAP_TSCTR		0x00
52 #define	ECAP_CAP1		0x08
53 #define	ECAP_CAP2		0x0C
54 #define	ECAP_CAP3		0x10
55 #define	ECAP_CAP4		0x14
56 #define	ECAP_ECCTL2		0x2A
57 #define		ECCTL2_MODE_APWM		(1 << 9)
58 #define		ECCTL2_SYNCO_SEL		(3 << 6)
59 #define		ECCTL2_TSCTRSTOP_FREERUN	(1 << 4)
60 
61 #define	ECAP_READ2(_sc, reg)	bus_read_2((_sc)->sc_mem_res, reg);
62 #define	ECAP_WRITE2(_sc, reg, value)	\
63     bus_write_2((_sc)->sc_mem_res, reg, value);
64 #define	ECAP_READ4(_sc, reg)	bus_read_4((_sc)->sc_mem_res, reg);
65 #define	ECAP_WRITE4(_sc, reg, value)	\
66     bus_write_4((_sc)->sc_mem_res, reg, value);
67 
68 #define	PWM_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
69 #define	PWM_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
70 #define	PWM_LOCK_INIT(_sc)	mtx_init(&(_sc)->sc_mtx, \
71     device_get_nameunit(_sc->sc_dev), "am335x_ecap softc", MTX_DEF)
72 #define	PWM_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->sc_mtx)
73 
74 static device_probe_t am335x_ecap_probe;
75 static device_attach_t am335x_ecap_attach;
76 static device_detach_t am335x_ecap_detach;
77 
78 struct am335x_ecap_softc {
79 	device_t		sc_dev;
80 	struct mtx		sc_mtx;
81 	struct resource		*sc_mem_res;
82 	int			sc_mem_rid;
83 };
84 
85 static device_method_t am335x_ecap_methods[] = {
86 	DEVMETHOD(device_probe,		am335x_ecap_probe),
87 	DEVMETHOD(device_attach,	am335x_ecap_attach),
88 	DEVMETHOD(device_detach,	am335x_ecap_detach),
89 
90 	DEVMETHOD_END
91 };
92 
93 static driver_t am335x_ecap_driver = {
94 	"am335x_ecap",
95 	am335x_ecap_methods,
96 	sizeof(struct am335x_ecap_softc),
97 };
98 
99 static devclass_t am335x_ecap_devclass;
100 
101 /*
102  * API function to set period/duty cycles for ECAPx
103  */
104 int
105 am335x_pwm_config_ecap(int unit, int period, int duty)
106 {
107 	device_t dev;
108 	struct am335x_ecap_softc *sc;
109 	uint16_t reg;
110 
111 	dev = devclass_get_device(am335x_ecap_devclass, unit);
112 	if (dev == NULL)
113 		return (ENXIO);
114 
115 	if (duty > period)
116 		return (EINVAL);
117 
118 	if (period == 0)
119 		return (EINVAL);
120 
121 	sc = device_get_softc(dev);
122 	PWM_LOCK(sc);
123 
124 	reg = ECAP_READ2(sc, ECAP_ECCTL2);
125 	reg |= ECCTL2_MODE_APWM | ECCTL2_TSCTRSTOP_FREERUN | ECCTL2_SYNCO_SEL;
126 	ECAP_WRITE2(sc, ECAP_ECCTL2, reg);
127 
128 	/* CAP3 in APWM mode is APRD shadow register */
129 	ECAP_WRITE4(sc, ECAP_CAP3, period - 1);
130 
131 	/* CAP4 in APWM mode is ACMP shadow register */
132 	ECAP_WRITE4(sc, ECAP_CAP4, duty);
133 	/* Restart counter */
134 	ECAP_WRITE4(sc, ECAP_TSCTR, 0);
135 
136 	PWM_UNLOCK(sc);
137 
138 	return (0);
139 }
140 
141 static int
142 am335x_ecap_probe(device_t dev)
143 {
144 
145 	if (!ofw_bus_status_okay(dev))
146 		return (ENXIO);
147 
148 	if (!ofw_bus_is_compatible(dev, "ti,am33xx-ecap"))
149 		return (ENXIO);
150 
151 	device_set_desc(dev, "AM335x eCAP");
152 
153 	return (BUS_PROBE_DEFAULT);
154 }
155 
156 static int
157 am335x_ecap_attach(device_t dev)
158 {
159 	struct am335x_ecap_softc *sc;
160 
161 	sc = device_get_softc(dev);
162 	sc->sc_dev = dev;
163 
164 	PWM_LOCK_INIT(sc);
165 
166 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
167 	    &sc->sc_mem_rid, RF_ACTIVE);
168 	if (sc->sc_mem_res == NULL) {
169 		device_printf(dev, "cannot allocate memory resources\n");
170 		goto fail;
171 	}
172 
173 	return (0);
174 
175 fail:
176 	PWM_LOCK_DESTROY(sc);
177 	return (ENXIO);
178 }
179 
180 static int
181 am335x_ecap_detach(device_t dev)
182 {
183 	struct am335x_ecap_softc *sc;
184 
185 	sc = device_get_softc(dev);
186 
187 	PWM_LOCK(sc);
188 	if (sc->sc_mem_res)
189 		bus_release_resource(dev, SYS_RES_MEMORY,
190 		    sc->sc_mem_rid, sc->sc_mem_res);
191 	PWM_UNLOCK(sc);
192 
193 	PWM_LOCK_DESTROY(sc);
194 
195 
196 	return (0);
197 }
198 
199 DRIVER_MODULE(am335x_ecap, am335x_pwmss, am335x_ecap_driver, am335x_ecap_devclass, 0, 0);
200 MODULE_VERSION(am335x_ecap, 1);
201 MODULE_DEPEND(am335x_ecap, am335x_pwmss, 1, 1, 1);
202