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