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