]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/pwm/pwmbus.c
Restructure the pwm device hirearchy and interfaces.
[FreeBSD/FreeBSD.git] / sys / dev / pwm / pwmbus.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_platform.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41
42 #include <machine/bus.h>
43
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46
47 #include <dev/pwm/pwmbus.h>
48
49 #include "pwmbus_if.h"
50
51 struct pwmbus_channel_data {
52         int     reserved;
53         char    *name;
54 };
55
56 struct pwmbus_softc {
57         device_t        dev;
58         device_t        parent;
59
60         int             nchannels;
61 };
62
63 static int
64 pwmbus_probe(device_t dev)
65 {
66
67         device_set_desc(dev, "PWM bus");
68         return (BUS_PROBE_GENERIC);
69 }
70
71 static int
72 pwmbus_attach(device_t dev)
73 {
74         struct pwmbus_softc *sc;
75
76         sc = device_get_softc(dev);
77         sc->dev = dev;
78         sc->parent = device_get_parent(dev);
79
80         if (PWMBUS_CHANNEL_COUNT(sc->parent, &sc->nchannels) != 0 ||
81             sc->nchannels == 0) {
82                 device_printf(sc->dev, "No channels on parent %s\n",
83                     device_get_nameunit(sc->parent));
84                 return (ENXIO);
85         }
86
87         device_add_child(sc->dev, "pwmc", -1);
88
89         bus_generic_probe(dev);
90
91         return (bus_generic_attach(dev));
92 }
93
94 static int
95 pwmbus_detach(device_t dev)
96 {
97         int rv;
98
99         if ((rv = bus_generic_detach(dev)) == 0)
100                 rv = device_delete_children(dev);
101
102         return (rv);
103 }
104
105 static int
106 pwmbus_channel_config(device_t dev, int chan, u_int period, u_int duty)
107 {
108         return (PWMBUS_CHANNEL_CONFIG(device_get_parent(dev), chan, period, duty));
109 }
110
111 static int
112 pwmbus_channel_get_config(device_t dev, int chan, u_int *period, u_int *duty)
113 {
114         return (PWMBUS_CHANNEL_GET_CONFIG(device_get_parent(dev), chan, period, duty));
115 }
116
117 static int
118 pwmbus_channel_get_flags(device_t dev, int chan, uint32_t *flags)
119 {
120         return (PWMBUS_CHANNEL_GET_FLAGS(device_get_parent(dev), chan, flags));
121 }
122
123 static int
124 pwmbus_channel_enable(device_t dev, int chan, bool enable)
125 {
126         return (PWMBUS_CHANNEL_ENABLE(device_get_parent(dev), chan, enable));
127 }
128
129 static int
130 pwmbus_channel_set_flags(device_t dev, int chan, uint32_t flags)
131 {
132         return (PWMBUS_CHANNEL_SET_FLAGS(device_get_parent(dev), chan, flags));
133 }
134
135 static int
136 pwmbus_channel_is_enabled(device_t dev, int chan, bool *enable)
137 {
138         return (PWMBUS_CHANNEL_IS_ENABLED(device_get_parent(dev), chan, enable));
139 }
140
141 static int
142 pwmbus_channel_count(device_t dev, int *nchannel)
143 {
144         return (PWMBUS_CHANNEL_COUNT(device_get_parent(dev), nchannel));
145 }
146
147 static device_method_t pwmbus_methods[] = {
148         /* device_if */
149         DEVMETHOD(device_probe,  pwmbus_probe),
150         DEVMETHOD(device_attach, pwmbus_attach),
151         DEVMETHOD(device_detach, pwmbus_detach),
152
153         /* pwmbus_if  */
154         DEVMETHOD(pwmbus_channel_count,         pwmbus_channel_count),
155         DEVMETHOD(pwmbus_channel_config,        pwmbus_channel_config),
156         DEVMETHOD(pwmbus_channel_get_config,    pwmbus_channel_get_config),
157         DEVMETHOD(pwmbus_channel_set_flags,     pwmbus_channel_set_flags),
158         DEVMETHOD(pwmbus_channel_get_flags,     pwmbus_channel_get_flags),
159         DEVMETHOD(pwmbus_channel_enable,        pwmbus_channel_enable),
160         DEVMETHOD(pwmbus_channel_is_enabled,    pwmbus_channel_is_enabled),
161
162         DEVMETHOD_END
163 };
164
165 static driver_t pwmbus_driver = {
166         "pwmbus",
167         pwmbus_methods,
168         sizeof(struct pwmbus_softc),
169 };
170 static devclass_t pwmbus_devclass;
171
172 EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, pwmbus_devclass, 0, 0,
173   BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);
174 MODULE_VERSION(pwmbus, 1);