rk_pwm.c revision 1.4 1 1.4 thorpej /* $NetBSD: rk_pwm.c,v 1.4 2021/01/18 02:35:49 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 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.4 thorpej __KERNEL_RCSID(1, "$NetBSD: rk_pwm.c,v 1.4 2021/01/18 02:35:49 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 PWM0_CNT 0x00
45 1.1 jmcneill #define PWM0_PERIOD_HPR 0x04
46 1.1 jmcneill #define PWM0_DUTY_LPR 0x08
47 1.1 jmcneill #define PWM0_CTRL 0x0c
48 1.1 jmcneill #define CTRL_RPT __BITS(31,24)
49 1.1 jmcneill #define CTRL_SCALE __BITS(23,16)
50 1.1 jmcneill #define CTRL_PRESCALE __BITS(14,12)
51 1.1 jmcneill #define CTRL_CLK_SEL __BIT(9)
52 1.1 jmcneill #define CTRL_LP_EN __BIT(8)
53 1.1 jmcneill #define CTRL_OUTPUT_MODE __BIT(5)
54 1.1 jmcneill #define CTRL_INACTIVE_POL __BIT(4)
55 1.1 jmcneill #define CTRL_DUTY_POL __BIT(3)
56 1.1 jmcneill #define CTRL_PWM_MODE __BITS(2,1)
57 1.1 jmcneill #define CTRL_PWM_MODE_ONESHOT 0
58 1.1 jmcneill #define CTRL_PWM_MODE_CONTINUOUS 1
59 1.1 jmcneill #define CTRL_PWM_MODE_CAPTURE 2
60 1.1 jmcneill #define CTRL_PWM_EN __BIT(0)
61 1.1 jmcneill
62 1.1 jmcneill enum rk_pwm_type {
63 1.1 jmcneill PWM_RK3288 = 1,
64 1.1 jmcneill };
65 1.1 jmcneill
66 1.4 thorpej static const struct device_compatible_entry compat_data[] = {
67 1.4 thorpej { .compat = "rockchip,rk3288-pwm", .value = PWM_RK3288 },
68 1.4 thorpej
69 1.4 thorpej { 0 }
70 1.1 jmcneill };
71 1.1 jmcneill
72 1.1 jmcneill struct rk_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 pwm_controller sc_pwm;
78 1.1 jmcneill struct pwm_config sc_conf;
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 pwm_tag_t
89 1.1 jmcneill rk_pwm_get_tag(device_t dev, const void *data, size_t len)
90 1.1 jmcneill {
91 1.1 jmcneill struct rk_pwm_softc * const sc = device_private(dev);
92 1.1 jmcneill const u_int *pwm = data;
93 1.1 jmcneill
94 1.1 jmcneill if (len != 16)
95 1.1 jmcneill return NULL;
96 1.1 jmcneill
97 1.1 jmcneill const u_int index = be32toh(pwm[1]);
98 1.1 jmcneill if (index != 0)
99 1.1 jmcneill return NULL;
100 1.1 jmcneill
101 1.1 jmcneill const u_int period = be32toh(pwm[2]);
102 1.1 jmcneill const u_int polarity = be32toh(pwm[3]);
103 1.1 jmcneill
104 1.1 jmcneill sc->sc_conf.period = period;
105 1.1 jmcneill sc->sc_conf.polarity = polarity ? PWM_ACTIVE_LOW : PWM_ACTIVE_HIGH;
106 1.1 jmcneill
107 1.1 jmcneill return &sc->sc_pwm;
108 1.1 jmcneill }
109 1.1 jmcneill
110 1.1 jmcneill static struct fdtbus_pwm_controller_func rk_pwm_funcs = {
111 1.1 jmcneill .get_tag = rk_pwm_get_tag
112 1.1 jmcneill };
113 1.1 jmcneill
114 1.1 jmcneill static int
115 1.1 jmcneill rk_pwm_enable(pwm_tag_t pwm, bool enable)
116 1.1 jmcneill {
117 1.1 jmcneill struct rk_pwm_softc * const sc = device_private(pwm->pwm_dev);
118 1.1 jmcneill uint32_t ctrl, octrl;
119 1.1 jmcneill
120 1.1 jmcneill octrl = ctrl = PWM_READ(sc, PWM0_CTRL);
121 1.1 jmcneill if (enable)
122 1.1 jmcneill ctrl |= CTRL_PWM_EN;
123 1.1 jmcneill else
124 1.1 jmcneill ctrl &= ~CTRL_PWM_EN;
125 1.1 jmcneill
126 1.1 jmcneill if (ctrl != octrl)
127 1.1 jmcneill PWM_WRITE(sc, PWM0_CTRL, ctrl);
128 1.1 jmcneill
129 1.1 jmcneill return 0;
130 1.1 jmcneill }
131 1.1 jmcneill
132 1.1 jmcneill static int
133 1.1 jmcneill rk_pwm_get_config(pwm_tag_t pwm, struct pwm_config *conf)
134 1.1 jmcneill {
135 1.1 jmcneill struct rk_pwm_softc * const sc = device_private(pwm->pwm_dev);
136 1.3 jakllsch
137 1.3 jakllsch #if 0
138 1.3 jakllsch /* XXX may be useful someday */
139 1.3 jakllsch
140 1.1 jmcneill uint32_t ctrl, period, duty;
141 1.1 jmcneill u_int div;
142 1.1 jmcneill
143 1.1 jmcneill ctrl = PWM_READ(sc, PWM0_CTRL);
144 1.1 jmcneill period = PWM_READ(sc, PWM0_PERIOD_HPR);
145 1.1 jmcneill duty = PWM_READ(sc, PWM0_DUTY_LPR);
146 1.1 jmcneill
147 1.1 jmcneill if (ctrl & CTRL_CLK_SEL) {
148 1.1 jmcneill div = __SHIFTOUT(ctrl, CTRL_SCALE) * 2;
149 1.1 jmcneill if (div == 0)
150 1.1 jmcneill div = 512;
151 1.1 jmcneill } else {
152 1.1 jmcneill div = 1;
153 1.1 jmcneill }
154 1.1 jmcneill div /= (1 << __SHIFTOUT(ctrl, CTRL_PRESCALE));
155 1.1 jmcneill
156 1.1 jmcneill const uint64_t rate = sc->sc_clkfreq / div;
157 1.1 jmcneill
158 1.1 jmcneill conf->polarity = (ctrl & CTRL_DUTY_POL) ? PWM_ACTIVE_HIGH : PWM_ACTIVE_LOW;
159 1.1 jmcneill conf->period = (u_int)(((uint64_t)period * 1000000000) / rate);
160 1.1 jmcneill conf->duty_cycle = (u_int)(((uint64_t)duty * 1000000000) / rate);
161 1.3 jakllsch #else
162 1.3 jakllsch *conf = sc->sc_conf;
163 1.3 jakllsch #endif
164 1.1 jmcneill
165 1.1 jmcneill return 0;
166 1.1 jmcneill }
167 1.1 jmcneill
168 1.1 jmcneill static int
169 1.1 jmcneill rk_pwm_set_config(pwm_tag_t pwm, const struct pwm_config *conf)
170 1.1 jmcneill {
171 1.1 jmcneill struct rk_pwm_softc * const sc = device_private(pwm->pwm_dev);
172 1.1 jmcneill uint32_t ctrl;
173 1.1 jmcneill
174 1.1 jmcneill const uint64_t rate = sc->sc_clkfreq;
175 1.1 jmcneill const uint32_t period = (u_int)((conf->period * rate) / 1000000000);
176 1.1 jmcneill const uint32_t duty = (u_int)((conf->duty_cycle * rate) / 1000000000);
177 1.1 jmcneill
178 1.1 jmcneill /* Preserve PWM_EN flag */
179 1.1 jmcneill ctrl = PWM_READ(sc, PWM0_CTRL) & CTRL_PWM_EN;
180 1.1 jmcneill
181 1.1 jmcneill ctrl |= __SHIFTIN(CTRL_PWM_MODE_CONTINUOUS, CTRL_PWM_MODE);
182 1.1 jmcneill if (conf->polarity == PWM_ACTIVE_HIGH)
183 1.1 jmcneill ctrl |= CTRL_DUTY_POL;
184 1.1 jmcneill else
185 1.1 jmcneill ctrl |= CTRL_INACTIVE_POL;
186 1.1 jmcneill
187 1.1 jmcneill PWM_WRITE(sc, PWM0_CTRL, 0);
188 1.1 jmcneill PWM_WRITE(sc, PWM0_PERIOD_HPR, period);
189 1.1 jmcneill PWM_WRITE(sc, PWM0_DUTY_LPR, duty);
190 1.1 jmcneill PWM_WRITE(sc, PWM0_CTRL, ctrl);
191 1.1 jmcneill
192 1.1 jmcneill sc->sc_conf = *conf;
193 1.1 jmcneill
194 1.1 jmcneill return 0;
195 1.1 jmcneill }
196 1.1 jmcneill
197 1.1 jmcneill static int
198 1.1 jmcneill rk_pwm_match(device_t parent, cfdata_t cf, void *aux)
199 1.1 jmcneill {
200 1.1 jmcneill struct fdt_attach_args * const faa = aux;
201 1.1 jmcneill
202 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
203 1.1 jmcneill }
204 1.1 jmcneill
205 1.1 jmcneill static void
206 1.1 jmcneill rk_pwm_attach(device_t parent, device_t self, void *aux)
207 1.1 jmcneill {
208 1.1 jmcneill struct rk_pwm_softc * const sc = device_private(self);
209 1.1 jmcneill struct fdt_attach_args * const faa = aux;
210 1.1 jmcneill const int phandle = faa->faa_phandle;
211 1.1 jmcneill struct clk *clk;
212 1.1 jmcneill bus_addr_t addr;
213 1.1 jmcneill bus_size_t size;
214 1.1 jmcneill int error;
215 1.1 jmcneill
216 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
217 1.1 jmcneill aprint_error(": couldn't get registers\n");
218 1.1 jmcneill return;
219 1.1 jmcneill }
220 1.1 jmcneill
221 1.1 jmcneill clk = fdtbus_clock_get_index(phandle, 0);
222 1.1 jmcneill if (clk == NULL) {
223 1.1 jmcneill aprint_error(": couldn't get clock\n");
224 1.1 jmcneill return;
225 1.1 jmcneill }
226 1.1 jmcneill
227 1.1 jmcneill sc->sc_dev = self;
228 1.1 jmcneill sc->sc_clkfreq = clk_get_rate(clk);
229 1.1 jmcneill sc->sc_bst = faa->faa_bst;
230 1.1 jmcneill error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
231 1.1 jmcneill if (error) {
232 1.2 skrll aprint_error(": couldn't map %#" PRIxBUSADDR ": %d",
233 1.2 skrll addr, error);
234 1.1 jmcneill return;
235 1.1 jmcneill }
236 1.1 jmcneill
237 1.1 jmcneill aprint_naive("\n");
238 1.1 jmcneill aprint_normal(": PWM\n");
239 1.1 jmcneill
240 1.1 jmcneill sc->sc_pwm.pwm_enable = rk_pwm_enable;
241 1.1 jmcneill sc->sc_pwm.pwm_get_config = rk_pwm_get_config;
242 1.1 jmcneill sc->sc_pwm.pwm_set_config = rk_pwm_set_config;
243 1.1 jmcneill sc->sc_pwm.pwm_dev = self;
244 1.1 jmcneill
245 1.1 jmcneill fdtbus_register_pwm_controller(self, phandle,
246 1.1 jmcneill &rk_pwm_funcs);
247 1.1 jmcneill }
248 1.1 jmcneill
249 1.1 jmcneill CFATTACH_DECL_NEW(rk_pwm, sizeof(struct rk_pwm_softc),
250 1.1 jmcneill rk_pwm_match, rk_pwm_attach, NULL, NULL);
251