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