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