xref: /freebsd/sys/dev/pwm/pwmbus.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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
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
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
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
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
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
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
148 pwmbus_probe(device_t dev)
149 {
150 	device_set_desc(dev, "PWM bus");
151 	return (BUS_PROBE_GENERIC);
152 }
153 
154 static int
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_generic_probe(dev);
186 
187 	return (bus_generic_attach(dev));
188 }
189 
190 static int
191 pwmbus_detach(device_t dev)
192 {
193 	int rv;
194 
195 	if ((rv = bus_generic_detach(dev)) == 0)
196 		rv = device_delete_children(dev);
197 
198 	return (rv);
199 }
200 
201 /*
202  * pwmbus_if methods...
203  */
204 
205 static int
206 pwmbus_channel_config(device_t dev, u_int chan, u_int period, u_int duty)
207 {
208 	return (PWMBUS_CHANNEL_CONFIG(device_get_parent(dev), chan, period, duty));
209 }
210 
211 static int
212 pwmbus_channel_get_config(device_t dev, u_int chan, u_int *period, u_int *duty)
213 {
214 	return (PWMBUS_CHANNEL_GET_CONFIG(device_get_parent(dev), chan, period, duty));
215 }
216 
217 static int
218 pwmbus_channel_get_flags(device_t dev, u_int chan, uint32_t *flags)
219 {
220 	return (PWMBUS_CHANNEL_GET_FLAGS(device_get_parent(dev), chan, flags));
221 }
222 
223 static int
224 pwmbus_channel_enable(device_t dev, u_int chan, bool enable)
225 {
226 	return (PWMBUS_CHANNEL_ENABLE(device_get_parent(dev), chan, enable));
227 }
228 
229 static int
230 pwmbus_channel_set_flags(device_t dev, u_int chan, uint32_t flags)
231 {
232 	return (PWMBUS_CHANNEL_SET_FLAGS(device_get_parent(dev), chan, flags));
233 }
234 
235 static int
236 pwmbus_channel_is_enabled(device_t dev, u_int chan, bool *enable)
237 {
238 	return (PWMBUS_CHANNEL_IS_ENABLED(device_get_parent(dev), chan, enable));
239 }
240 
241 static int
242 pwmbus_channel_count(device_t dev, u_int *nchannel)
243 {
244 	return (PWMBUS_CHANNEL_COUNT(device_get_parent(dev), nchannel));
245 }
246 
247 static device_method_t pwmbus_methods[] = {
248 	/* device_if */
249 	DEVMETHOD(device_probe,  pwmbus_probe),
250 	DEVMETHOD(device_attach, pwmbus_attach),
251 	DEVMETHOD(device_detach, pwmbus_detach),
252 
253         /* bus_if */
254 	DEVMETHOD(bus_add_child,		pwmbus_add_child),
255 	DEVMETHOD(bus_child_location,		pwmbus_child_location),
256 	DEVMETHOD(bus_hinted_child,		pwmbus_hinted_child),
257 	DEVMETHOD(bus_print_child,		pwmbus_print_child),
258 	DEVMETHOD(bus_probe_nomatch,		pwmbus_probe_nomatch),
259 	DEVMETHOD(bus_read_ivar,		pwmbus_read_ivar),
260 
261         /* pwmbus_if  */
262 	DEVMETHOD(pwmbus_channel_count,		pwmbus_channel_count),
263 	DEVMETHOD(pwmbus_channel_config,	pwmbus_channel_config),
264 	DEVMETHOD(pwmbus_channel_get_config,	pwmbus_channel_get_config),
265 	DEVMETHOD(pwmbus_channel_set_flags,	pwmbus_channel_set_flags),
266 	DEVMETHOD(pwmbus_channel_get_flags,	pwmbus_channel_get_flags),
267 	DEVMETHOD(pwmbus_channel_enable,	pwmbus_channel_enable),
268 	DEVMETHOD(pwmbus_channel_is_enabled,	pwmbus_channel_is_enabled),
269 
270 	DEVMETHOD_END
271 };
272 
273 driver_t pwmbus_driver = {
274 	"pwmbus",
275 	pwmbus_methods,
276 	sizeof(struct pwmbus_softc),
277 };
278 
279 EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, 0, 0,
280     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
281 MODULE_VERSION(pwmbus, 1);
282