]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/allwinner/aw_pwm.c
MFC r343826, r346698, r349057-r349060, r349073-r349077, r349080-r349086, r349088,
[FreeBSD/FreeBSD.git] / sys / arm / allwinner / aw_pwm.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/rman.h>
39 #include <sys/resource.h>
40 #include <machine/bus.h>
41
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44
45 #include <dev/extres/clk/clk.h>
46
47 #include "pwmbus_if.h"
48
49 #define AW_PWM_CTRL                     0x00
50 #define  AW_PWM_CTRL_PRESCALE_MASK      0xF
51 #define  AW_PWM_CTRL_EN                 (1 << 4)
52 #define  AW_PWM_CTRL_ACTIVE_LEVEL_HIGH  (1 << 5)
53 #define  AW_PWM_CTRL_GATE               (1 << 6)
54 #define  AW_PWM_CTRL_MODE_MASK          0x80
55 #define  AW_PWM_CTRL_PULSE_MODE         (1 << 7)
56 #define  AW_PWM_CTRL_CYCLE_MODE         (0 << 7)
57 #define  AW_PWM_CTRL_PULSE_START        (1 << 8)
58 #define  AW_PWM_CTRL_CLK_BYPASS         (1 << 9)
59 #define  AW_PWM_CTRL_PERIOD_BUSY        (1 << 28)
60
61 #define AW_PWM_PERIOD                   0x04
62 #define AW_PWM_PERIOD_TOTAL_MASK        0xFFFF
63 #define AW_PWM_PERIOD_TOTAL_SHIFT       16
64 #define AW_PWM_PERIOD_ACTIVE_MASK       0xFFFF
65 #define AW_PWM_PERIOD_ACTIVE_SHIFT      0
66
67 #define AW_PWM_MAX_FREQ                 24000000
68
69 #define NS_PER_SEC      1000000000
70
71 static struct ofw_compat_data compat_data[] = {
72         { "allwinner,sun5i-a13-pwm",            1 },
73         { NULL,                                 0 }
74 };
75
76 static struct resource_spec aw_pwm_spec[] = {
77         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
78         { -1, 0 }
79 };
80
81 struct aw_pwm_softc {
82         device_t        dev;
83         device_t        busdev;
84         clk_t           clk;
85         struct resource *res;
86
87         uint64_t        clk_freq;
88         unsigned int    period;
89         unsigned int    duty;
90         uint32_t        flags;
91         bool            enabled;
92 };
93
94 static uint32_t aw_pwm_clk_prescaler[] = {
95         120,
96         180,
97         240,
98         360,
99         480,
100         0,
101         0,
102         0,
103         12000,
104         24000,
105         36000,
106         48000,
107         72000,
108         0,
109         0,
110         1,
111 };
112
113 #define AW_PWM_READ(sc, reg)            bus_read_4((sc)->res, (reg))
114 #define AW_PWM_WRITE(sc, reg, val)      bus_write_4((sc)->res, (reg), (val))
115
116 static int aw_pwm_probe(device_t dev);
117 static int aw_pwm_attach(device_t dev);
118 static int aw_pwm_detach(device_t dev);
119
120 static int
121 aw_pwm_probe(device_t dev)
122 {
123         if (!ofw_bus_status_okay(dev))
124                 return (ENXIO);
125
126         if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
127                 return (ENXIO);
128
129         device_set_desc(dev, "Allwinner PWM");
130         return (BUS_PROBE_DEFAULT);
131 }
132
133 static int
134 aw_pwm_attach(device_t dev)
135 {
136         struct aw_pwm_softc *sc;
137         uint64_t clk_freq;
138         uint32_t reg;
139         phandle_t node;
140         int error;
141
142         sc = device_get_softc(dev);
143         sc->dev = dev;
144
145         error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
146         if (error != 0) {
147                 device_printf(dev, "cannot get clock\n");
148                 goto fail;
149         }
150         error = clk_enable(sc->clk);
151         if (error != 0) {
152                 device_printf(dev, "cannot enable clock\n");
153                 goto fail;
154         }
155
156         error = clk_get_freq(sc->clk, &sc->clk_freq);
157         if (error != 0) {
158                 device_printf(dev, "cannot get clock frequency\n");
159                 goto fail;
160         }
161
162         if (bus_alloc_resources(dev, aw_pwm_spec, &sc->res) != 0) {
163                 device_printf(dev, "cannot allocate resources for device\n");
164                 error = ENXIO;
165                 goto fail;
166         }
167
168         /* Read the configuration left by U-Boot */
169         reg = AW_PWM_READ(sc, AW_PWM_CTRL);
170         if (reg & (AW_PWM_CTRL_GATE | AW_PWM_CTRL_EN))
171                 sc->enabled = true;
172
173         reg = AW_PWM_READ(sc, AW_PWM_CTRL);
174         reg &= AW_PWM_CTRL_PRESCALE_MASK;
175         if (reg > nitems(aw_pwm_clk_prescaler)) {
176                 device_printf(dev, "Bad prescaler %x, cannot guess current settings\n", reg);
177                 goto skipcfg;
178         }
179         clk_freq = sc->clk_freq / aw_pwm_clk_prescaler[reg];
180
181         reg = AW_PWM_READ(sc, AW_PWM_PERIOD);
182         sc->period = NS_PER_SEC /
183                 (clk_freq / ((reg >> AW_PWM_PERIOD_TOTAL_SHIFT) & AW_PWM_PERIOD_TOTAL_MASK));
184         sc->duty = NS_PER_SEC /
185                 (clk_freq / ((reg >> AW_PWM_PERIOD_ACTIVE_SHIFT) & AW_PWM_PERIOD_ACTIVE_MASK));
186
187 skipcfg:
188         /*
189          * Note that we don't check for failure to attach pwmbus -- even without
190          * it we can still service clients who connect via fdt xref data.
191          */
192         node = ofw_bus_get_node(dev);
193         OF_device_register_xref(OF_xref_from_node(node), dev);
194
195         sc->busdev = device_add_child(dev, "pwmbus", -1);
196
197         return (bus_generic_attach(dev));
198
199 fail:
200         aw_pwm_detach(dev);
201         return (error);
202 }
203
204 static int
205 aw_pwm_detach(device_t dev)
206 {
207         struct aw_pwm_softc *sc;
208         int error;
209
210         sc = device_get_softc(dev);
211
212         if ((error = bus_generic_detach(sc->dev)) != 0) {
213                 device_printf(sc->dev, "cannot detach child devices\n");
214                 return (error);
215         }
216
217         if (sc->busdev != NULL)
218                 device_delete_child(dev, sc->busdev);
219
220         if (sc->res != NULL)
221                 bus_release_resources(dev, aw_pwm_spec, &sc->res);
222
223         return (0);
224 }
225
226 static phandle_t
227 aw_pwm_get_node(device_t bus, device_t dev)
228 {
229
230         /*
231          * Share our controller node with our pwmbus child; it instantiates
232          * devices by walking the children contained within our node.
233          */
234         return ofw_bus_get_node(bus);
235 }
236
237 static int
238 aw_pwm_channel_count(device_t dev, u_int *nchannel)
239 {
240
241         *nchannel = 1;
242
243         return (0);
244 }
245
246 static int
247 aw_pwm_channel_config(device_t dev, u_int channel, u_int period, u_int duty)
248 {
249         struct aw_pwm_softc *sc;
250         uint64_t period_freq, duty_freq;
251         uint64_t clk_rate, div;
252         uint32_t reg;
253         int prescaler;
254         int i;
255
256         sc = device_get_softc(dev);
257
258         period_freq = NS_PER_SEC / period;
259         if (period_freq > AW_PWM_MAX_FREQ)
260                 return (EINVAL);
261         duty_freq = NS_PER_SEC / duty;
262         if (duty_freq < period_freq) {
263                 device_printf(sc->dev, "duty < period\n");
264                 return (EINVAL);
265         }
266
267         /* First test without prescaler */
268         clk_rate = AW_PWM_MAX_FREQ;
269         prescaler = AW_PWM_CTRL_PRESCALE_MASK;
270         div = AW_PWM_MAX_FREQ / period_freq;
271         if ((div - 1) > AW_PWM_PERIOD_TOTAL_MASK) {
272                 /* Test all prescaler */
273                 for (i = 0; i < nitems(aw_pwm_clk_prescaler); i++) {
274                         if (aw_pwm_clk_prescaler[i] == 0)
275                                 continue;
276                         div = (AW_PWM_MAX_FREQ * aw_pwm_clk_prescaler[i]) / period_freq;
277                         if ((div - 1) < AW_PWM_PERIOD_TOTAL_MASK ) {
278                                 prescaler = i;
279                                 clk_rate = AW_PWM_MAX_FREQ / aw_pwm_clk_prescaler[i];
280                                 break;
281                         }
282                 }
283                 if (prescaler == AW_PWM_CTRL_PRESCALE_MASK)
284                         return (EINVAL);
285         }
286
287         reg = AW_PWM_READ(sc, AW_PWM_CTRL);
288         if (reg & AW_PWM_CTRL_PERIOD_BUSY) {
289                 device_printf(sc->dev, "pwm busy\n");
290                 return (EBUSY);
291         }
292
293         /* Write the prescalar */
294         reg &= ~AW_PWM_CTRL_PRESCALE_MASK;
295         reg |= prescaler;
296         AW_PWM_WRITE(sc, AW_PWM_CTRL, reg);
297
298         /* Write the total/active cycles */
299         reg = ((clk_rate / period_freq) << AW_PWM_PERIOD_TOTAL_SHIFT) |
300           ((clk_rate / duty_freq) << AW_PWM_PERIOD_ACTIVE_SHIFT);
301         AW_PWM_WRITE(sc, AW_PWM_PERIOD, reg);
302
303         sc->period = period;
304         sc->duty = duty;
305
306         return (0);
307 }
308
309 static int
310 aw_pwm_channel_get_config(device_t dev, u_int channel, u_int *period, u_int *duty)
311 {
312         struct aw_pwm_softc *sc;
313
314         sc = device_get_softc(dev);
315
316         *period = sc->period;
317         *duty = sc->duty;
318
319         return (0);
320 }
321
322 static int
323 aw_pwm_channel_enable(device_t dev, u_int channel, bool enable)
324 {
325         struct aw_pwm_softc *sc;
326         uint32_t reg;
327
328         sc = device_get_softc(dev);
329
330         if (enable && sc->enabled)
331                 return (0);
332
333         reg = AW_PWM_READ(sc, AW_PWM_CTRL);
334         if (enable)
335                 reg |= AW_PWM_CTRL_GATE | AW_PWM_CTRL_EN;
336         else
337                 reg &= ~(AW_PWM_CTRL_GATE | AW_PWM_CTRL_EN);
338
339         AW_PWM_WRITE(sc, AW_PWM_CTRL, reg);
340
341         sc->enabled = enable;
342
343         return (0);
344 }
345
346 static int
347 aw_pwm_channel_is_enabled(device_t dev, u_int channel, bool *enabled)
348 {
349         struct aw_pwm_softc *sc;
350
351         sc = device_get_softc(dev);
352
353         *enabled = sc->enabled;
354
355         return (0);
356 }
357
358 static device_method_t aw_pwm_methods[] = {
359         /* Device interface */
360         DEVMETHOD(device_probe,         aw_pwm_probe),
361         DEVMETHOD(device_attach,        aw_pwm_attach),
362         DEVMETHOD(device_detach,        aw_pwm_detach),
363
364         /* ofw_bus interface */
365         DEVMETHOD(ofw_bus_get_node,     aw_pwm_get_node),
366
367         /* pwmbus interface */
368         DEVMETHOD(pwmbus_channel_count,         aw_pwm_channel_count),
369         DEVMETHOD(pwmbus_channel_config,        aw_pwm_channel_config),
370         DEVMETHOD(pwmbus_channel_get_config,    aw_pwm_channel_get_config),
371         DEVMETHOD(pwmbus_channel_enable,        aw_pwm_channel_enable),
372         DEVMETHOD(pwmbus_channel_is_enabled,    aw_pwm_channel_is_enabled),
373
374         DEVMETHOD_END
375 };
376
377 static driver_t aw_pwm_driver = {
378         "pwm",
379         aw_pwm_methods,
380         sizeof(struct aw_pwm_softc),
381 };
382
383 static devclass_t aw_pwm_devclass;
384
385 DRIVER_MODULE(aw_pwm, simplebus, aw_pwm_driver, aw_pwm_devclass, 0, 0);
386 MODULE_VERSION(aw_pwm, 1);
387 SIMPLEBUS_PNP_INFO(compat_data);