xref: /freebsd/sys/dev/pwm/pwmbus.c (revision c6e475ff5ab177d1bfa2fe8eaf9678fd165649fa)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_platform.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 
42 #include <machine/bus.h>
43 
44 #include <dev/pwm/pwmbus.h>
45 
46 #include "pwmbus_if.h"
47 
48 struct pwmbus_channel_data {
49 	int	reserved;
50 	char	*name;
51 };
52 
53 struct pwmbus_softc {
54 	device_t	dev;
55 	device_t	parent;
56 
57 	u_int		nchannels;
58 };
59 
60 static int
61 pwmbus_probe(device_t dev)
62 {
63 
64 	device_set_desc(dev, "PWM bus");
65 	return (BUS_PROBE_GENERIC);
66 }
67 
68 static int
69 pwmbus_attach(device_t dev)
70 {
71 	struct pwmbus_softc *sc;
72 
73 	sc = device_get_softc(dev);
74 	sc->dev = dev;
75 	sc->parent = device_get_parent(dev);
76 
77 	if (PWMBUS_CHANNEL_COUNT(sc->parent, &sc->nchannels) != 0 ||
78 	    sc->nchannels == 0) {
79 		device_printf(sc->dev, "No channels on parent %s\n",
80 		    device_get_nameunit(sc->parent));
81 		return (ENXIO);
82 	}
83 
84 	device_add_child(sc->dev, "pwmc", -1);
85 
86 	bus_generic_probe(dev);
87 
88 	return (bus_generic_attach(dev));
89 }
90 
91 static int
92 pwmbus_detach(device_t dev)
93 {
94 	int rv;
95 
96 	if ((rv = bus_generic_detach(dev)) == 0)
97 		rv = device_delete_children(dev);
98 
99 	return (rv);
100 }
101 
102 static int
103 pwmbus_channel_config(device_t dev, u_int chan, u_int period, u_int duty)
104 {
105 	return (PWMBUS_CHANNEL_CONFIG(device_get_parent(dev), chan, period, duty));
106 }
107 
108 static int
109 pwmbus_channel_get_config(device_t dev, u_int chan, u_int *period, u_int *duty)
110 {
111 	return (PWMBUS_CHANNEL_GET_CONFIG(device_get_parent(dev), chan, period, duty));
112 }
113 
114 static int
115 pwmbus_channel_get_flags(device_t dev, u_int chan, uint32_t *flags)
116 {
117 	return (PWMBUS_CHANNEL_GET_FLAGS(device_get_parent(dev), chan, flags));
118 }
119 
120 static int
121 pwmbus_channel_enable(device_t dev, u_int chan, bool enable)
122 {
123 	return (PWMBUS_CHANNEL_ENABLE(device_get_parent(dev), chan, enable));
124 }
125 
126 static int
127 pwmbus_channel_set_flags(device_t dev, u_int chan, uint32_t flags)
128 {
129 	return (PWMBUS_CHANNEL_SET_FLAGS(device_get_parent(dev), chan, flags));
130 }
131 
132 static int
133 pwmbus_channel_is_enabled(device_t dev, u_int chan, bool *enable)
134 {
135 	return (PWMBUS_CHANNEL_IS_ENABLED(device_get_parent(dev), chan, enable));
136 }
137 
138 static int
139 pwmbus_channel_count(device_t dev, u_int *nchannel)
140 {
141 	return (PWMBUS_CHANNEL_COUNT(device_get_parent(dev), nchannel));
142 }
143 
144 static device_method_t pwmbus_methods[] = {
145 	/* device_if */
146 	DEVMETHOD(device_probe,  pwmbus_probe),
147 	DEVMETHOD(device_attach, pwmbus_attach),
148 	DEVMETHOD(device_detach, pwmbus_detach),
149 
150         /* pwmbus_if  */
151 	DEVMETHOD(pwmbus_channel_count,		pwmbus_channel_count),
152 	DEVMETHOD(pwmbus_channel_config,	pwmbus_channel_config),
153 	DEVMETHOD(pwmbus_channel_get_config,	pwmbus_channel_get_config),
154 	DEVMETHOD(pwmbus_channel_set_flags,	pwmbus_channel_set_flags),
155 	DEVMETHOD(pwmbus_channel_get_flags,	pwmbus_channel_get_flags),
156 	DEVMETHOD(pwmbus_channel_enable,	pwmbus_channel_enable),
157 	DEVMETHOD(pwmbus_channel_is_enabled,	pwmbus_channel_is_enabled),
158 
159 	DEVMETHOD_END
160 };
161 
162 static driver_t pwmbus_driver = {
163 	"pwmbus",
164 	pwmbus_methods,
165 	sizeof(struct pwmbus_softc),
166 };
167 static devclass_t pwmbus_devclass;
168 
169 EARLY_DRIVER_MODULE(pwmbus, pwm, pwmbus_driver, pwmbus_devclass, 0, 0,
170   BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);
171 MODULE_VERSION(pwmbus, 1);
172