1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, 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
28 #include <sys/cdefs.h>
29 #include "opt_platform.h"
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/sbuf.h>
40
41 #include <dev/pwm/pwmbus.h>
42
43 #include "pwmbus_if.h"
44
45 /*
46 * bus_if methods...
47 */
48
49 static device_t
pwmbus_add_child(device_t dev,u_int order,const char * name,int unit)50 pwmbus_add_child(device_t dev, u_int order, const char *name, int unit)
51 {
52 device_t child;
53 struct pwmbus_ivars *ivars;
54
55 child = device_add_child_ordered(dev, order, name, unit);
56 if (child == NULL)
57 return (child);
58
59 ivars = malloc(sizeof(struct pwmbus_ivars), M_DEVBUF, M_NOWAIT | M_ZERO);
60 if (ivars == NULL) {
61 device_delete_child(dev, child);
62 return (NULL);
63 }
64 device_set_ivars(child, ivars);
65
66 return (child);
67 }
68
69 static int
pwmbus_child_location(device_t dev,device_t child,struct sbuf * sb)70 pwmbus_child_location(device_t dev, device_t child, struct sbuf *sb)
71 {
72 struct pwmbus_ivars *ivars;
73
74 ivars = device_get_ivars(child);
75 sbuf_printf(sb, "hwdev=%s channel=%u",
76 device_get_nameunit(device_get_parent(dev)), ivars->pi_channel);
77
78 return (0);
79 }
80
81 static void
pwmbus_hinted_child(device_t dev,const char * dname,int dunit)82 pwmbus_hinted_child(device_t dev, const char *dname, int dunit)
83 {
84 struct pwmbus_ivars *ivars;
85 device_t child;
86
87 child = pwmbus_add_child(dev, 0, dname, dunit);
88
89 /*
90 * If there is a channel hint, use it. Otherwise pi_channel was
91 * initialized to zero, so that's the channel we'll use.
92 */
93 ivars = device_get_ivars(child);
94 resource_int_value(dname, dunit, "channel", &ivars->pi_channel);
95 }
96
97 static int
pwmbus_print_child(device_t dev,device_t child)98 pwmbus_print_child(device_t dev, device_t child)
99 {
100 struct pwmbus_ivars *ivars;
101 int rv;
102
103 ivars = device_get_ivars(child);
104
105 rv = bus_print_child_header(dev, child);
106 rv += printf(" channel %u", ivars->pi_channel);
107 rv += bus_print_child_footer(dev, child);
108
109 return (rv);
110 }
111
112 static void
pwmbus_probe_nomatch(device_t dev,device_t child)113 pwmbus_probe_nomatch(device_t dev, device_t child)
114 {
115 struct pwmbus_ivars *ivars;
116
117 ivars = device_get_ivars(child);
118 if (ivars != NULL)
119 device_printf(dev, "<unknown> on channel %u\n",
120 ivars->pi_channel);
121
122 return;
123 }
124
125 static int
pwmbus_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)126 pwmbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
127 {
128 struct pwmbus_ivars *ivars;
129
130 ivars = device_get_ivars(child);
131
132 switch (which) {
133 case PWMBUS_IVAR_CHANNEL:
134 *(u_int *)result = ivars->pi_channel;
135 break;
136 default:
137 return (EINVAL);
138 }
139
140 return (0);
141 }
142
143 /*
144 * device_if methods...
145 */
146
147 static int
pwmbus_probe(device_t dev)148 pwmbus_probe(device_t dev)
149 {
150 device_set_desc(dev, "PWM bus");
151 return (BUS_PROBE_GENERIC);
152 }
153
154 static int
pwmbus_attach(device_t dev)155 pwmbus_attach(device_t dev)
156 {
157 struct pwmbus_softc *sc;
158 struct pwmbus_ivars *ivars;
159 device_t child, parent;
160 u_int chan;
161
162 sc = device_get_softc(dev);
163 sc->dev = dev;
164 parent = device_get_parent(dev);
165
166 if (PWMBUS_CHANNEL_COUNT(parent, &sc->nchannels) != 0 ||
167 sc->nchannels == 0) {
168 device_printf(sc->dev, "No channels on parent %s\n",
169 device_get_nameunit(parent));
170 return (ENXIO);
171 }
172
173 /* Add a pwmc(4) child for each channel. */
174 for (chan = 0; chan < sc->nchannels; ++chan) {
175 if ((child = pwmbus_add_child(sc->dev, 0, "pwmc", -1)) == NULL) {
176 device_printf(dev, "failed to add pwmc child device "
177 "for channel %u\n", chan);
178 continue;
179 }
180 ivars = device_get_ivars(child);
181 ivars->pi_channel = chan;
182 }
183
184 bus_enumerate_hinted_children(dev);
185 bus_identify_children(dev);
186 bus_attach_children(dev);
187
188 return (0);
189 }
190
191 /*
192 * pwmbus_if methods...
193 */
194
195 static int
pwmbus_channel_config(device_t dev,u_int chan,u_int period,u_int duty)196 pwmbus_channel_config(device_t dev, u_int chan, u_int period, u_int duty)
197 {
198 return (PWMBUS_CHANNEL_CONFIG(device_get_parent(dev), chan, period, duty));
199 }
200
201 static int
pwmbus_channel_get_config(device_t dev,u_int chan,u_int * period,u_int * duty)202 pwmbus_channel_get_config(device_t dev, u_int chan, u_int *period, u_int *duty)
203 {
204 return (PWMBUS_CHANNEL_GET_CONFIG(device_get_parent(dev), chan, period, duty));
205 }
206
207 static int
pwmbus_channel_get_flags(device_t dev,u_int chan,uint32_t * flags)208 pwmbus_channel_get_flags(device_t dev, u_int chan, uint32_t *flags)
209 {
210 return (PWMBUS_CHANNEL_GET_FLAGS(device_get_parent(dev), chan, flags));
211 }
212
213 static int
pwmbus_channel_enable(device_t dev,u_int chan,bool enable)214 pwmbus_channel_enable(device_t dev, u_int chan, bool enable)
215 {
216 return (PWMBUS_CHANNEL_ENABLE(device_get_parent(dev), chan, enable));
217 }
218
219 static int
pwmbus_channel_set_flags(device_t dev,u_int chan,uint32_t flags)220 pwmbus_channel_set_flags(device_t dev, u_int chan, uint32_t flags)
221 {
222 return (PWMBUS_CHANNEL_SET_FLAGS(device_get_parent(dev), chan, flags));
223 }
224
225 static int
pwmbus_channel_is_enabled(device_t dev,u_int chan,bool * enable)226 pwmbus_channel_is_enabled(device_t dev, u_int chan, bool *enable)
227 {
228 return (PWMBUS_CHANNEL_IS_ENABLED(device_get_parent(dev), chan, enable));
229 }
230
231 static int
pwmbus_channel_count(device_t dev,u_int * nchannel)232 pwmbus_channel_count(device_t dev, u_int *nchannel)
233 {
234 return (PWMBUS_CHANNEL_COUNT(device_get_parent(dev), nchannel));
235 }
236
237 static device_method_t pwmbus_methods[] = {
238 /* device_if */
239 DEVMETHOD(device_probe, pwmbus_probe),
240 DEVMETHOD(device_attach, pwmbus_attach),
241 DEVMETHOD(device_detach, bus_generic_detach),
242
243 /* bus_if */
244 DEVMETHOD(bus_add_child, pwmbus_add_child),
245 DEVMETHOD(bus_child_location, pwmbus_child_location),
246 DEVMETHOD(bus_hinted_child, pwmbus_hinted_child),
247 DEVMETHOD(bus_print_child, pwmbus_print_child),
248 DEVMETHOD(bus_probe_nomatch, pwmbus_probe_nomatch),
249 DEVMETHOD(bus_read_ivar, pwmbus_read_ivar),
250
251 /* pwmbus_if */
252 DEVMETHOD(pwmbus_channel_count, pwmbus_channel_count),
253 DEVMETHOD(pwmbus_channel_config, pwmbus_channel_config),
254 DEVMETHOD(pwmbus_channel_get_config, pwmbus_channel_get_config),
255 DEVMETHOD(pwmbus_channel_set_flags, pwmbus_channel_set_flags),
256 DEVMETHOD(pwmbus_channel_get_flags, pwmbus_channel_get_flags),
257 DEVMETHOD(pwmbus_channel_enable, pwmbus_channel_enable),
258 DEVMETHOD(pwmbus_channel_is_enabled, pwmbus_channel_is_enabled),
259
260 DEVMETHOD_END
261 };
262
263 driver_t pwmbus_driver = {
264 "pwmbus",
265 pwmbus_methods,
266 sizeof(struct pwmbus_softc),
267 };
268
269 EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, 0, 0,
270 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
271 MODULE_VERSION(pwmbus, 1);
272