sunxi_pwm.c revision 1.4 1 1.4 thorpej /* $NetBSD: sunxi_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) 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.4 thorpej __KERNEL_RCSID(1, "$NetBSD: sunxi_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 PWM_CH_CTRL 0x00
45 1.1 jmcneill #define PWM0_RDY __BIT(28)
46 1.1 jmcneill #define PWM0_BYPASS __BIT(9)
47 1.1 jmcneill #define PWM_CH0_PUL_START __BIT(8)
48 1.1 jmcneill #define PWM_CHANNEL0_MODE __BIT(7)
49 1.1 jmcneill #define SCLK_CH0_GATING __BIT(6)
50 1.1 jmcneill #define PWM_CH0_ACT_STA __BIT(5)
51 1.1 jmcneill #define PWM_CH0_EN __BIT(4)
52 1.1 jmcneill #define PWM_CH0_PRESCAL __BITS(3,0)
53 1.1 jmcneill #define PWM_CH0_PERIOD 0x04
54 1.1 jmcneill #define PWM_CH0_ENTIRE_CYS __BITS(31,16)
55 1.1 jmcneill #define PWM_CH0_ENTIRE_ACT_CYS __BITS(15,0)
56 1.1 jmcneill
57 1.1 jmcneill enum sunxi_pwm_type {
58 1.1 jmcneill PWM_A64 = 1,
59 1.1 jmcneill };
60 1.1 jmcneill
61 1.4 thorpej static const struct device_compatible_entry compat_data[] = {
62 1.4 thorpej { .compat = "allwinner,sun50i-a64-pwm", .value = PWM_A64 },
63 1.4 thorpej
64 1.4 thorpej { 0 }
65 1.1 jmcneill };
66 1.1 jmcneill
67 1.1 jmcneill struct sunxi_pwm_softc {
68 1.1 jmcneill device_t sc_dev;
69 1.1 jmcneill bus_space_tag_t sc_bst;
70 1.1 jmcneill bus_space_handle_t sc_bsh;
71 1.1 jmcneill
72 1.1 jmcneill struct pwm_controller sc_pwm;
73 1.1 jmcneill struct pwm_config sc_conf;
74 1.1 jmcneill
75 1.1 jmcneill u_int sc_clkfreq;
76 1.1 jmcneill };
77 1.1 jmcneill
78 1.1 jmcneill #define PWM_READ(sc, reg) \
79 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
80 1.1 jmcneill #define PWM_WRITE(sc, reg, val) \
81 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
82 1.1 jmcneill
83 1.1 jmcneill static pwm_tag_t
84 1.1 jmcneill sunxi_pwm_get_tag(device_t dev, const void *data, size_t len)
85 1.1 jmcneill {
86 1.1 jmcneill struct sunxi_pwm_softc * const sc = device_private(dev);
87 1.1 jmcneill const u_int *pwm = data;
88 1.1 jmcneill
89 1.1 jmcneill if (len != 16)
90 1.1 jmcneill return NULL;
91 1.1 jmcneill
92 1.1 jmcneill const u_int index = be32toh(pwm[1]);
93 1.1 jmcneill if (index != 0)
94 1.1 jmcneill return NULL;
95 1.1 jmcneill
96 1.1 jmcneill const u_int period = be32toh(pwm[2]);
97 1.1 jmcneill const u_int polarity = be32toh(pwm[3]);
98 1.1 jmcneill
99 1.1 jmcneill sc->sc_conf.period = period;
100 1.1 jmcneill sc->sc_conf.polarity = polarity ? PWM_ACTIVE_LOW : PWM_ACTIVE_HIGH;
101 1.1 jmcneill
102 1.1 jmcneill return &sc->sc_pwm;
103 1.1 jmcneill }
104 1.1 jmcneill
105 1.1 jmcneill static struct fdtbus_pwm_controller_func sunxi_pwm_funcs = {
106 1.1 jmcneill .get_tag = sunxi_pwm_get_tag
107 1.1 jmcneill };
108 1.1 jmcneill
109 1.1 jmcneill static int
110 1.1 jmcneill sunxi_pwm_enable(pwm_tag_t pwm, bool enable)
111 1.1 jmcneill {
112 1.1 jmcneill struct sunxi_pwm_softc * const sc = device_private(pwm->pwm_dev);
113 1.1 jmcneill uint32_t ctrl, octrl;
114 1.1 jmcneill
115 1.1 jmcneill octrl = ctrl = PWM_READ(sc, PWM_CH_CTRL);
116 1.1 jmcneill if (enable)
117 1.1 jmcneill ctrl |= (PWM_CH0_EN | SCLK_CH0_GATING);
118 1.1 jmcneill else
119 1.1 jmcneill ctrl &= ~(PWM_CH0_EN | SCLK_CH0_GATING);
120 1.1 jmcneill
121 1.1 jmcneill if (ctrl != octrl)
122 1.1 jmcneill PWM_WRITE(sc, PWM_CH_CTRL, ctrl);
123 1.1 jmcneill
124 1.1 jmcneill return 0;
125 1.1 jmcneill }
126 1.1 jmcneill
127 1.1 jmcneill static int
128 1.1 jmcneill sunxi_pwm_get_config(pwm_tag_t pwm, struct pwm_config *conf)
129 1.1 jmcneill {
130 1.1 jmcneill struct sunxi_pwm_softc * const sc = device_private(pwm->pwm_dev);
131 1.1 jmcneill uint32_t ctrl, ch_period;
132 1.1 jmcneill
133 1.1 jmcneill ctrl = PWM_READ(sc, PWM_CH_CTRL);
134 1.1 jmcneill ch_period = PWM_READ(sc, PWM_CH0_PERIOD);
135 1.1 jmcneill
136 1.1 jmcneill const uint64_t rate = sc->sc_clkfreq / 120;
137 1.1 jmcneill const u_int cycles = __SHIFTOUT(ch_period, PWM_CH0_ENTIRE_CYS) + 1;
138 1.1 jmcneill const u_int act_cycles = __SHIFTOUT(ch_period, PWM_CH0_ENTIRE_ACT_CYS);
139 1.1 jmcneill
140 1.1 jmcneill conf->polarity = (ctrl & PWM_CH0_ACT_STA) ? PWM_ACTIVE_HIGH : PWM_ACTIVE_LOW;
141 1.1 jmcneill conf->period = (u_int)(((uint64_t)cycles * 1000000000) / rate);
142 1.1 jmcneill conf->duty_cycle = (u_int)(((uint64_t)act_cycles * 1000000000) / rate);
143 1.1 jmcneill
144 1.1 jmcneill return 0;
145 1.1 jmcneill }
146 1.1 jmcneill
147 1.1 jmcneill static int
148 1.1 jmcneill sunxi_pwm_set_config(pwm_tag_t pwm, const struct pwm_config *conf)
149 1.1 jmcneill {
150 1.1 jmcneill struct sunxi_pwm_softc * const sc = device_private(pwm->pwm_dev);
151 1.1 jmcneill uint32_t ctrl, ch_period;
152 1.1 jmcneill
153 1.1 jmcneill ctrl = PWM_READ(sc, PWM_CH_CTRL);
154 1.1 jmcneill if (ctrl & PWM0_RDY)
155 1.1 jmcneill return EBUSY;
156 1.1 jmcneill
157 1.1 jmcneill ctrl &= ~PWM0_BYPASS; /* Prescaler /120 = 200 kHz */
158 1.1 jmcneill ctrl &= ~PWM_CH0_PRESCAL;
159 1.1 jmcneill ctrl |= __SHIFTIN(0, PWM_CH0_PRESCAL);
160 1.1 jmcneill
161 1.1 jmcneill ctrl &= ~PWM_CHANNEL0_MODE; /* Cycle mode */
162 1.1 jmcneill if (conf->polarity == PWM_ACTIVE_HIGH)
163 1.1 jmcneill ctrl |= PWM_CH0_ACT_STA;
164 1.1 jmcneill else
165 1.1 jmcneill ctrl &= ~PWM_CH0_ACT_STA;
166 1.1 jmcneill
167 1.1 jmcneill const uint64_t rate = sc->sc_clkfreq / 120;
168 1.1 jmcneill const u_int cycles = (u_int)((conf->period * rate) / 1000000000);
169 1.1 jmcneill const u_int act_cycles = (u_int)((conf->duty_cycle * rate) / 1000000000);
170 1.1 jmcneill
171 1.1 jmcneill ch_period = __SHIFTIN(cycles - 1, PWM_CH0_ENTIRE_CYS);
172 1.1 jmcneill ch_period |= __SHIFTIN(act_cycles, PWM_CH0_ENTIRE_ACT_CYS);
173 1.1 jmcneill
174 1.1 jmcneill PWM_WRITE(sc, PWM_CH0_PERIOD, ch_period);
175 1.1 jmcneill PWM_WRITE(sc, PWM_CH_CTRL, ctrl);
176 1.1 jmcneill
177 1.1 jmcneill sc->sc_conf = *conf;
178 1.1 jmcneill
179 1.1 jmcneill return 0;
180 1.1 jmcneill }
181 1.1 jmcneill
182 1.1 jmcneill static int
183 1.1 jmcneill sunxi_pwm_match(device_t parent, cfdata_t cf, void *aux)
184 1.1 jmcneill {
185 1.1 jmcneill struct fdt_attach_args * const faa = aux;
186 1.1 jmcneill
187 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
188 1.1 jmcneill }
189 1.1 jmcneill
190 1.1 jmcneill static void
191 1.1 jmcneill sunxi_pwm_attach(device_t parent, device_t self, void *aux)
192 1.1 jmcneill {
193 1.1 jmcneill struct sunxi_pwm_softc * const sc = device_private(self);
194 1.1 jmcneill struct fdt_attach_args * const faa = aux;
195 1.1 jmcneill const int phandle = faa->faa_phandle;
196 1.1 jmcneill struct clk *clk;
197 1.1 jmcneill bus_addr_t addr;
198 1.1 jmcneill bus_size_t size;
199 1.1 jmcneill int error;
200 1.1 jmcneill
201 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
202 1.1 jmcneill aprint_error(": couldn't get registers\n");
203 1.1 jmcneill return;
204 1.1 jmcneill }
205 1.1 jmcneill
206 1.1 jmcneill clk = fdtbus_clock_get_index(phandle, 0);
207 1.1 jmcneill if (clk == NULL) {
208 1.1 jmcneill aprint_error(": couldn't get clock\n");
209 1.1 jmcneill return;
210 1.1 jmcneill }
211 1.1 jmcneill
212 1.1 jmcneill sc->sc_dev = self;
213 1.1 jmcneill sc->sc_clkfreq = clk_get_rate(clk);
214 1.1 jmcneill sc->sc_bst = faa->faa_bst;
215 1.1 jmcneill error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
216 1.1 jmcneill if (error) {
217 1.3 skrll aprint_error(": couldn't map %#" PRIxBUSADDR ": %d",
218 1.3 skrll addr, error);
219 1.1 jmcneill return;
220 1.1 jmcneill }
221 1.1 jmcneill
222 1.1 jmcneill aprint_naive("\n");
223 1.1 jmcneill aprint_normal(": PWM\n");
224 1.1 jmcneill
225 1.1 jmcneill sc->sc_pwm.pwm_enable = sunxi_pwm_enable;
226 1.1 jmcneill sc->sc_pwm.pwm_get_config = sunxi_pwm_get_config;
227 1.1 jmcneill sc->sc_pwm.pwm_set_config = sunxi_pwm_set_config;
228 1.1 jmcneill sc->sc_pwm.pwm_dev = self;
229 1.1 jmcneill
230 1.1 jmcneill fdtbus_register_pwm_controller(self, phandle,
231 1.1 jmcneill &sunxi_pwm_funcs);
232 1.1 jmcneill }
233 1.1 jmcneill
234 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_pwm, sizeof(struct sunxi_pwm_softc),
235 1.1 jmcneill sunxi_pwm_match, sunxi_pwm_attach, NULL, NULL);
236