exynos_pwm.c revision 1.1.6.3 1 1.1.6.3 martin /* $NetBSD: exynos_pwm.c,v 1.1.6.3 2020/04/13 08:03:37 martin Exp $ */
2 1.1.6.2 christos
3 1.1.6.2 christos /*-
4 1.1.6.2 christos * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1.6.2 christos * All rights reserved.
6 1.1.6.2 christos *
7 1.1.6.2 christos * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 christos * modification, are permitted provided that the following conditions
9 1.1.6.2 christos * are met:
10 1.1.6.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 christos * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 christos * documentation and/or other materials provided with the distribution.
15 1.1.6.2 christos *
16 1.1.6.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.6.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.6.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.6.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.6.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.6.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.6.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.6.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.6.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.6.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.6.2 christos * POSSIBILITY OF SUCH DAMAGE.
27 1.1.6.2 christos */
28 1.1.6.2 christos
29 1.1.6.2 christos #include <sys/cdefs.h>
30 1.1.6.2 christos
31 1.1.6.3 martin __KERNEL_RCSID(1, "$NetBSD: exynos_pwm.c,v 1.1.6.3 2020/04/13 08:03:37 martin Exp $");
32 1.1.6.2 christos
33 1.1.6.2 christos #include <sys/param.h>
34 1.1.6.2 christos #include <sys/bus.h>
35 1.1.6.2 christos #include <sys/device.h>
36 1.1.6.2 christos #include <sys/intr.h>
37 1.1.6.2 christos #include <sys/systm.h>
38 1.1.6.2 christos #include <sys/time.h>
39 1.1.6.2 christos
40 1.1.6.2 christos #include <dev/pwm/pwmvar.h>
41 1.1.6.2 christos
42 1.1.6.2 christos #include <dev/fdt/fdtvar.h>
43 1.1.6.2 christos
44 1.1.6.2 christos #define TCFG0 0x00
45 1.1.6.2 christos #define TCFG1 0x04
46 1.1.6.2 christos #define TCON 0x08
47 1.1.6.2 christos #define _TCON_OFF(n) ((n) == 0 ? 0 : (((n) + 1) * 4))
48 1.1.6.2 christos #define TCON_START(n) __BIT(_TCON_OFF(n) + 0)
49 1.1.6.2 christos #define TCON_UPDATE(n) __BIT(_TCON_OFF(n) + 1)
50 1.1.6.2 christos #define TCON_OUTINV(n) __BIT(_TCON_OFF(n) + 2)
51 1.1.6.2 christos #define TCON_AUTO_RELOAD(n) __BIT(_TCON_OFF(n) + 3)
52 1.1.6.2 christos #define TCON_DEADZONE_EN(n) __BIT(_TCON_OFF(n) + 4)
53 1.1.6.2 christos #define TCNTB(n) (0x0c + (n) * 12)
54 1.1.6.2 christos #define TCMPB(n) (0x10 + (n) * 12)
55 1.1.6.2 christos #define TCNTO(n) (0x14 + (n) * 12)
56 1.1.6.2 christos #define TINT_CSTAT 0x44
57 1.1.6.2 christos
58 1.1.6.2 christos #define PWM_NTIMERS 5
59 1.1.6.2 christos
60 1.1.6.2 christos static const char * const compatible[] = {
61 1.1.6.2 christos "samsung,exynos4210-pwm",
62 1.1.6.2 christos NULL
63 1.1.6.2 christos };
64 1.1.6.2 christos
65 1.1.6.2 christos struct exynos_pwm_softc;
66 1.1.6.2 christos
67 1.1.6.2 christos struct exynos_pwm_timer {
68 1.1.6.2 christos u_int timer_index;
69 1.1.6.2 christos struct pwm_controller timer_pwm;
70 1.1.6.2 christos };
71 1.1.6.2 christos
72 1.1.6.2 christos struct exynos_pwm_softc {
73 1.1.6.2 christos device_t sc_dev;
74 1.1.6.2 christos bus_space_tag_t sc_bst;
75 1.1.6.2 christos bus_space_handle_t sc_bsh;
76 1.1.6.2 christos
77 1.1.6.2 christos struct exynos_pwm_timer sc_timer[PWM_NTIMERS];
78 1.1.6.2 christos u_int sc_timermask;
79 1.1.6.2 christos
80 1.1.6.2 christos u_int sc_clkfreq;
81 1.1.6.2 christos };
82 1.1.6.2 christos
83 1.1.6.2 christos #define PWM_READ(sc, reg) \
84 1.1.6.2 christos bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
85 1.1.6.2 christos #define PWM_WRITE(sc, reg, val) \
86 1.1.6.2 christos bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
87 1.1.6.2 christos
88 1.1.6.2 christos static int
89 1.1.6.2 christos exynos_pwm_enable(pwm_tag_t pwm, bool enable)
90 1.1.6.2 christos {
91 1.1.6.2 christos struct exynos_pwm_timer * const timer = pwm->pwm_priv;
92 1.1.6.2 christos struct exynos_pwm_softc * const sc = device_private(pwm->pwm_dev);
93 1.1.6.2 christos uint32_t tcon;
94 1.1.6.2 christos
95 1.1.6.2 christos if (enable) {
96 1.1.6.2 christos tcon = PWM_READ(sc, TCON);
97 1.1.6.2 christos tcon &= ~TCON_START(timer->timer_index);
98 1.1.6.2 christos tcon |= TCON_UPDATE(timer->timer_index);
99 1.1.6.2 christos PWM_WRITE(sc, TCON, tcon);
100 1.1.6.2 christos tcon &= ~TCON_UPDATE(timer->timer_index);
101 1.1.6.2 christos tcon |= TCON_START(timer->timer_index);
102 1.1.6.2 christos tcon |= TCON_AUTO_RELOAD(timer->timer_index);
103 1.1.6.2 christos PWM_WRITE(sc, TCON, tcon);
104 1.1.6.2 christos } else {
105 1.1.6.2 christos tcon = PWM_READ(sc, TCON);
106 1.1.6.2 christos tcon &= ~TCON_AUTO_RELOAD(timer->timer_index);
107 1.1.6.2 christos PWM_WRITE(sc, TCON, tcon);
108 1.1.6.2 christos }
109 1.1.6.2 christos
110 1.1.6.2 christos return 0;
111 1.1.6.2 christos }
112 1.1.6.2 christos
113 1.1.6.2 christos static int
114 1.1.6.2 christos exynos_pwm_get_config(pwm_tag_t pwm, struct pwm_config *conf)
115 1.1.6.2 christos {
116 1.1.6.2 christos struct exynos_pwm_timer * const timer = pwm->pwm_priv;
117 1.1.6.2 christos struct exynos_pwm_softc * const sc = device_private(pwm->pwm_dev);
118 1.1.6.2 christos uint32_t tcon, tcntb, tcmpb;
119 1.1.6.2 christos
120 1.1.6.2 christos tcon = PWM_READ(sc, TCON);
121 1.1.6.2 christos tcntb = PWM_READ(sc, TCNTB(timer->timer_index));
122 1.1.6.2 christos tcmpb = PWM_READ(sc, TCMPB(timer->timer_index));
123 1.1.6.2 christos
124 1.1.6.2 christos conf->polarity = (tcon & TCON_OUTINV(timer->timer_index)) ? PWM_ACTIVE_HIGH : PWM_ACTIVE_LOW;
125 1.1.6.2 christos conf->period = (u_int)(((uint64_t)tcntb * 1000000000) / sc->sc_clkfreq);
126 1.1.6.2 christos conf->duty_cycle = (u_int)(((uint64_t)tcmpb * 1000000000) / sc->sc_clkfreq);
127 1.1.6.2 christos
128 1.1.6.2 christos return 0;
129 1.1.6.2 christos }
130 1.1.6.2 christos
131 1.1.6.2 christos static int
132 1.1.6.2 christos exynos_pwm_set_config(pwm_tag_t pwm, const struct pwm_config *conf)
133 1.1.6.2 christos {
134 1.1.6.2 christos struct exynos_pwm_timer * const timer = pwm->pwm_priv;
135 1.1.6.2 christos struct exynos_pwm_softc * const sc = device_private(pwm->pwm_dev);
136 1.1.6.2 christos uint32_t tcon, tcntb, tcmpb;
137 1.1.6.2 christos
138 1.1.6.2 christos tcon = PWM_READ(sc, TCON);
139 1.1.6.2 christos if (conf->polarity == PWM_ACTIVE_HIGH)
140 1.1.6.2 christos tcon |= TCON_OUTINV(timer->timer_index);
141 1.1.6.2 christos else
142 1.1.6.2 christos tcon &= ~TCON_OUTINV(timer->timer_index);
143 1.1.6.2 christos PWM_WRITE(sc, TCON, tcon);
144 1.1.6.2 christos
145 1.1.6.2 christos tcntb = conf->period / (1000000000 / sc->sc_clkfreq);
146 1.1.6.2 christos tcmpb = conf->duty_cycle / (1000000000 / sc->sc_clkfreq);
147 1.1.6.2 christos if (tcmpb == 0)
148 1.1.6.2 christos tcmpb = 1;
149 1.1.6.2 christos tcmpb = tcntb - tcmpb;
150 1.1.6.2 christos
151 1.1.6.2 christos PWM_WRITE(sc, TCNTB(timer->timer_index), tcntb - 1);
152 1.1.6.2 christos PWM_WRITE(sc, TCMPB(timer->timer_index), tcmpb - 1);
153 1.1.6.2 christos
154 1.1.6.2 christos tcon = PWM_READ(sc, TCON);
155 1.1.6.2 christos tcon |= TCON_UPDATE(timer->timer_index);
156 1.1.6.2 christos tcon |= TCON_AUTO_RELOAD(timer->timer_index);
157 1.1.6.2 christos PWM_WRITE(sc, TCON, tcon);
158 1.1.6.2 christos
159 1.1.6.2 christos tcon &= ~TCON_UPDATE(timer->timer_index);
160 1.1.6.2 christos PWM_WRITE(sc, TCON, tcon);
161 1.1.6.2 christos
162 1.1.6.2 christos return 0;
163 1.1.6.2 christos }
164 1.1.6.2 christos
165 1.1.6.2 christos static pwm_tag_t
166 1.1.6.2 christos exynos_pwm_get_tag(device_t dev, const void *data, size_t len)
167 1.1.6.2 christos {
168 1.1.6.2 christos struct exynos_pwm_softc * const sc = device_private(dev);
169 1.1.6.2 christos struct exynos_pwm_timer *timer;
170 1.1.6.2 christos const u_int *pwm = data;
171 1.1.6.2 christos struct pwm_config conf;
172 1.1.6.2 christos
173 1.1.6.2 christos if (len != 16)
174 1.1.6.2 christos return NULL;
175 1.1.6.2 christos
176 1.1.6.2 christos const u_int index = be32toh(pwm[1]);
177 1.1.6.2 christos if (index >= 32 || (sc->sc_timermask & __BIT(index)) == 0)
178 1.1.6.2 christos return NULL;
179 1.1.6.2 christos
180 1.1.6.2 christos const u_int period = be32toh(pwm[2]);
181 1.1.6.2 christos const u_int polarity = be32toh(pwm[3]);
182 1.1.6.2 christos
183 1.1.6.2 christos timer = &sc->sc_timer[index];
184 1.1.6.2 christos
185 1.1.6.2 christos /* Set initial timer polarity and period from specifier */
186 1.1.6.2 christos exynos_pwm_get_config(&timer->timer_pwm, &conf);
187 1.1.6.2 christos conf.period = period;
188 1.1.6.2 christos conf.polarity = polarity ? PWM_ACTIVE_LOW : PWM_ACTIVE_HIGH;
189 1.1.6.2 christos exynos_pwm_set_config(&timer->timer_pwm, &conf);
190 1.1.6.2 christos
191 1.1.6.2 christos return &timer->timer_pwm;
192 1.1.6.2 christos }
193 1.1.6.2 christos
194 1.1.6.2 christos static struct fdtbus_pwm_controller_func exynos_pwm_funcs = {
195 1.1.6.2 christos .get_tag = exynos_pwm_get_tag
196 1.1.6.2 christos };
197 1.1.6.2 christos
198 1.1.6.2 christos static int
199 1.1.6.2 christos exynos_pwm_match(device_t parent, cfdata_t cf, void *aux)
200 1.1.6.2 christos {
201 1.1.6.2 christos struct fdt_attach_args * const faa = aux;
202 1.1.6.2 christos
203 1.1.6.2 christos return of_match_compatible(faa->faa_phandle, compatible);
204 1.1.6.2 christos }
205 1.1.6.2 christos
206 1.1.6.2 christos static void
207 1.1.6.2 christos exynos_pwm_attach(device_t parent, device_t self, void *aux)
208 1.1.6.2 christos {
209 1.1.6.2 christos struct exynos_pwm_softc * const sc = device_private(self);
210 1.1.6.2 christos struct fdt_attach_args * const faa = aux;
211 1.1.6.2 christos const int phandle = faa->faa_phandle;
212 1.1.6.2 christos const u_int *data;
213 1.1.6.2 christos struct clk *clk;
214 1.1.6.2 christos bus_addr_t addr;
215 1.1.6.2 christos bus_size_t size;
216 1.1.6.2 christos int error, len, n;
217 1.1.6.2 christos
218 1.1.6.2 christos if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
219 1.1.6.2 christos aprint_error(": couldn't get registers\n");
220 1.1.6.2 christos return;
221 1.1.6.2 christos }
222 1.1.6.2 christos
223 1.1.6.2 christos clk = fdtbus_clock_get(phandle, "timers");
224 1.1.6.2 christos if (clk == NULL || clk_enable(clk) != 0) {
225 1.1.6.2 christos aprint_error(": couldn't enable timers clock\n");
226 1.1.6.2 christos return;
227 1.1.6.2 christos }
228 1.1.6.2 christos
229 1.1.6.2 christos sc->sc_dev = self;
230 1.1.6.2 christos sc->sc_clkfreq = clk_get_rate(clk);
231 1.1.6.2 christos sc->sc_bst = faa->faa_bst;
232 1.1.6.2 christos error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
233 1.1.6.2 christos if (error) {
234 1.1.6.3 martin aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
235 1.1.6.2 christos return;
236 1.1.6.2 christos }
237 1.1.6.2 christos for (n = 0; n < PWM_NTIMERS; n++) {
238 1.1.6.2 christos sc->sc_timer[n].timer_index = n;
239 1.1.6.2 christos sc->sc_timer[n].timer_pwm.pwm_enable = exynos_pwm_enable;
240 1.1.6.2 christos sc->sc_timer[n].timer_pwm.pwm_get_config = exynos_pwm_get_config;
241 1.1.6.2 christos sc->sc_timer[n].timer_pwm.pwm_set_config = exynos_pwm_set_config;
242 1.1.6.2 christos sc->sc_timer[n].timer_pwm.pwm_dev = self;
243 1.1.6.2 christos sc->sc_timer[n].timer_pwm.pwm_priv = &sc->sc_timer[n];
244 1.1.6.2 christos sc->sc_timermask |= __BIT(n);
245 1.1.6.2 christos }
246 1.1.6.2 christos
247 1.1.6.2 christos data = fdtbus_get_prop(phandle, "samsung,pwm-outputs", &len);
248 1.1.6.2 christos if (data) {
249 1.1.6.2 christos sc->sc_timermask = 0;
250 1.1.6.2 christos for (n = 0; n < len / 4; n++) {
251 1.1.6.2 christos sc->sc_timermask |= __BIT(be32toh(data[n]));
252 1.1.6.2 christos }
253 1.1.6.2 christos }
254 1.1.6.2 christos
255 1.1.6.2 christos aprint_naive("\n");
256 1.1.6.2 christos aprint_normal(": PWM\n");
257 1.1.6.2 christos
258 1.1.6.2 christos fdtbus_register_pwm_controller(self, phandle,
259 1.1.6.2 christos &exynos_pwm_funcs);
260 1.1.6.2 christos }
261 1.1.6.2 christos
262 1.1.6.2 christos CFATTACH_DECL_NEW(exynos_pwm, sizeof(struct exynos_pwm_softc),
263 1.1.6.2 christos exynos_pwm_match, exynos_pwm_attach, NULL, NULL);
264