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
40 #include <dev/pwm/pwmbus.h>
41
42 #include "pwmbus_if.h"
43
44 /*
45 * bus_if methods...
46 */
47
48 static device_t
pwmbus_add_child(device_t dev,u_int order,const char * name,int unit)49 pwmbus_add_child(device_t dev, u_int order, const char *name, int unit)
50 {
51 device_t child;
52 struct pwmbus_ivars *ivars;
53
54 child = device_add_child_ordered(dev, order, name, unit);
55 if (child == NULL)
56 return (child);
57
58 ivars = malloc(sizeof(struct pwmbus_ivars), M_DEVBUF, M_NOWAIT | M_ZERO);
59 if (ivars == NULL) {
60 device_delete_child(dev, child);
61 return (NULL);
62 }
63 device_set_ivars(child, ivars);
64
65 return (child);
66 }
67
68 static int
pwmbus_child_location_str(device_t dev,device_t child,char * buf,size_t blen)69 pwmbus_child_location_str(device_t dev, device_t child, char *buf, size_t blen)
70 {
71 struct pwmbus_ivars *ivars;
72
73 ivars = device_get_ivars(child);
74 snprintf(buf, blen, "hwdev=%s channel=%u",
75 device_get_nameunit(device_get_parent(dev)), ivars->pi_channel);
76
77 return (0);
78 }
79
80 static int
pwmbus_child_pnpinfo_str(device_t dev,device_t child,char * buf,size_t buflen)81 pwmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf,
82 size_t buflen)
83 {
84 *buf = '\0';
85 return (0);
86 }
87
88 static void
pwmbus_hinted_child(device_t dev,const char * dname,int dunit)89 pwmbus_hinted_child(device_t dev, const char *dname, int dunit)
90 {
91 struct pwmbus_ivars *ivars;
92 device_t child;
93
94 child = pwmbus_add_child(dev, 0, dname, dunit);
95
96 /*
97 * If there is a channel hint, use it. Otherwise pi_channel was
98 * initialized to zero, so that's the channel we'll use.
99 */
100 ivars = device_get_ivars(child);
101 resource_int_value(dname, dunit, "channel", &ivars->pi_channel);
102 }
103
104 static int
pwmbus_print_child(device_t dev,device_t child)105 pwmbus_print_child(device_t dev, device_t child)
106 {
107 struct pwmbus_ivars *ivars;
108 int rv;
109
110 ivars = device_get_ivars(child);
111
112 rv = bus_print_child_header(dev, child);
113 rv += printf(" channel %u", ivars->pi_channel);
114 rv += bus_print_child_footer(dev, child);
115
116 return (rv);
117 }
118
119 static void
pwmbus_probe_nomatch(device_t dev,device_t child)120 pwmbus_probe_nomatch(device_t dev, device_t child)
121 {
122 struct pwmbus_ivars *ivars;
123
124 ivars = device_get_ivars(child);
125 if (ivars != NULL)
126 device_printf(dev, "<unknown> on channel %u\n",
127 ivars->pi_channel);
128
129 return;
130 }
131
132 static int
pwmbus_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)133 pwmbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
134 {
135 struct pwmbus_ivars *ivars;
136
137 ivars = device_get_ivars(child);
138
139 switch (which) {
140 case PWMBUS_IVAR_CHANNEL:
141 *(u_int *)result = ivars->pi_channel;
142 break;
143 default:
144 return (EINVAL);
145 }
146
147 return (0);
148 }
149
150 /*
151 * device_if methods...
152 */
153
154 static int
pwmbus_probe(device_t dev)155 pwmbus_probe(device_t dev)
156 {
157 device_set_desc(dev, "PWM bus");
158 return (BUS_PROBE_GENERIC);
159 }
160
161 static int
pwmbus_attach(device_t dev)162 pwmbus_attach(device_t dev)
163 {
164 struct pwmbus_softc *sc;
165 struct pwmbus_ivars *ivars;
166 device_t child, parent;
167 u_int chan;
168
169 sc = device_get_softc(dev);
170 sc->dev = dev;
171 parent = device_get_parent(dev);
172
173 if (PWMBUS_CHANNEL_COUNT(parent, &sc->nchannels) != 0 ||
174 sc->nchannels == 0) {
175 device_printf(sc->dev, "No channels on parent %s\n",
176 device_get_nameunit(parent));
177 return (ENXIO);
178 }
179
180 /* Add a pwmc(4) child for each channel. */
181 for (chan = 0; chan < sc->nchannels; ++chan) {
182 if ((child = pwmbus_add_child(sc->dev, 0, "pwmc", -1)) == NULL) {
183 device_printf(dev, "failed to add pwmc child device "
184 "for channel %u\n", chan);
185 continue;
186 }
187 ivars = device_get_ivars(child);
188 ivars->pi_channel = chan;
189 }
190
191 bus_enumerate_hinted_children(dev);
192 bus_generic_probe(dev);
193
194 return (bus_generic_attach(dev));
195 }
196
197 static int
pwmbus_detach(device_t dev)198 pwmbus_detach(device_t dev)
199 {
200 int rv;
201
202 if ((rv = bus_generic_detach(dev)) == 0)
203 rv = device_delete_children(dev);
204
205 return (rv);
206 }
207
208 /*
209 * pwmbus_if methods...
210 */
211
212 static int
pwmbus_channel_config(device_t dev,u_int chan,u_int period,u_int duty)213 pwmbus_channel_config(device_t dev, u_int chan, u_int period, u_int duty)
214 {
215 return (PWMBUS_CHANNEL_CONFIG(device_get_parent(dev), chan, period, duty));
216 }
217
218 static int
pwmbus_channel_get_config(device_t dev,u_int chan,u_int * period,u_int * duty)219 pwmbus_channel_get_config(device_t dev, u_int chan, u_int *period, u_int *duty)
220 {
221 return (PWMBUS_CHANNEL_GET_CONFIG(device_get_parent(dev), chan, period, duty));
222 }
223
224 static int
pwmbus_channel_get_flags(device_t dev,u_int chan,uint32_t * flags)225 pwmbus_channel_get_flags(device_t dev, u_int chan, uint32_t *flags)
226 {
227 return (PWMBUS_CHANNEL_GET_FLAGS(device_get_parent(dev), chan, flags));
228 }
229
230 static int
pwmbus_channel_enable(device_t dev,u_int chan,bool enable)231 pwmbus_channel_enable(device_t dev, u_int chan, bool enable)
232 {
233 return (PWMBUS_CHANNEL_ENABLE(device_get_parent(dev), chan, enable));
234 }
235
236 static int
pwmbus_channel_set_flags(device_t dev,u_int chan,uint32_t flags)237 pwmbus_channel_set_flags(device_t dev, u_int chan, uint32_t flags)
238 {
239 return (PWMBUS_CHANNEL_SET_FLAGS(device_get_parent(dev), chan, flags));
240 }
241
242 static int
pwmbus_channel_is_enabled(device_t dev,u_int chan,bool * enable)243 pwmbus_channel_is_enabled(device_t dev, u_int chan, bool *enable)
244 {
245 return (PWMBUS_CHANNEL_IS_ENABLED(device_get_parent(dev), chan, enable));
246 }
247
248 static int
pwmbus_channel_count(device_t dev,u_int * nchannel)249 pwmbus_channel_count(device_t dev, u_int *nchannel)
250 {
251 return (PWMBUS_CHANNEL_COUNT(device_get_parent(dev), nchannel));
252 }
253
254 static device_method_t pwmbus_methods[] = {
255 /* device_if */
256 DEVMETHOD(device_probe, pwmbus_probe),
257 DEVMETHOD(device_attach, pwmbus_attach),
258 DEVMETHOD(device_detach, pwmbus_detach),
259
260 /* bus_if */
261 DEVMETHOD(bus_add_child, pwmbus_add_child),
262 DEVMETHOD(bus_child_location_str, pwmbus_child_location_str),
263 DEVMETHOD(bus_child_pnpinfo_str, pwmbus_child_pnpinfo_str),
264 DEVMETHOD(bus_hinted_child, pwmbus_hinted_child),
265 DEVMETHOD(bus_print_child, pwmbus_print_child),
266 DEVMETHOD(bus_probe_nomatch, pwmbus_probe_nomatch),
267 DEVMETHOD(bus_read_ivar, pwmbus_read_ivar),
268
269 /* pwmbus_if */
270 DEVMETHOD(pwmbus_channel_count, pwmbus_channel_count),
271 DEVMETHOD(pwmbus_channel_config, pwmbus_channel_config),
272 DEVMETHOD(pwmbus_channel_get_config, pwmbus_channel_get_config),
273 DEVMETHOD(pwmbus_channel_set_flags, pwmbus_channel_set_flags),
274 DEVMETHOD(pwmbus_channel_get_flags, pwmbus_channel_get_flags),
275 DEVMETHOD(pwmbus_channel_enable, pwmbus_channel_enable),
276 DEVMETHOD(pwmbus_channel_is_enabled, pwmbus_channel_is_enabled),
277
278 DEVMETHOD_END
279 };
280
281 driver_t pwmbus_driver = {
282 "pwmbus",
283 pwmbus_methods,
284 sizeof(struct pwmbus_softc),
285 };
286 devclass_t pwmbus_devclass;
287
288 EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, pwmbus_devclass, 0, 0,
289 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
290 MODULE_VERSION(pwmbus, 1);
291