]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/pwm/pwmc.c
Remove everything related to channels from the pwmc public interface, now
[FreeBSD/FreeBSD.git] / sys / dev / pwm / pwmc.c
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/kernel.h>
39 #include <sys/module.h>
40 #include <sys/time.h>
41
42 #include <dev/pwm/pwmbus.h>
43 #include <dev/pwm/pwmc.h>
44
45 #include "pwmbus_if.h"
46
47 #ifdef FDT
48 #include <dev/ofw/openfirm.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51
52 static struct ofw_compat_data compat_data[] = {
53         {"freebsd,pwmc", true},
54         {NULL,           false},
55 };
56
57 PWMBUS_FDT_PNP_INFO(compat_data);
58
59 #endif
60
61 struct pwmc_softc {
62         device_t        dev;
63         struct cdev     *cdev;
64         u_int           chan;
65 };
66
67 static int
68 pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
69     int fflag, struct thread *td)
70 {
71         struct pwmc_softc *sc;
72         struct pwm_state state;
73         device_t bus;
74         int rv = 0;
75
76         sc = dev->si_drv1;
77         bus = device_get_parent(sc->dev);
78
79         switch (cmd) {
80         case PWMSETSTATE:
81                 bcopy(data, &state, sizeof(state));
82                 rv = PWMBUS_CHANNEL_CONFIG(bus, sc->chan,
83                     state.period, state.duty);
84                 if (rv == 0)
85                         rv = PWMBUS_CHANNEL_ENABLE(bus, sc->chan,
86                             state.enable);
87                 break;
88         case PWMGETSTATE:
89                 bcopy(data, &state, sizeof(state));
90                 rv = PWMBUS_CHANNEL_GET_CONFIG(bus, sc->chan,
91                     &state.period, &state.duty);
92                 if (rv != 0)
93                         return (rv);
94                 rv = PWMBUS_CHANNEL_IS_ENABLED(bus, sc->chan,
95                     &state.enable);
96                 if (rv != 0)
97                         return (rv);
98                 bcopy(&state, data, sizeof(state));
99                 break;
100         }
101
102         return (rv);
103 }
104
105 static struct cdevsw pwm_cdevsw = {
106         .d_version      = D_VERSION,
107         .d_name         = "pwmc",
108         .d_ioctl        = pwm_ioctl
109 };
110
111 #ifdef FDT
112
113 static void
114 pwmc_setup_label(struct pwmc_softc *sc)
115 {
116         void *label;
117
118         if (OF_getprop_alloc(ofw_bus_get_node(sc->dev), "label", &label) > 0) {
119                 make_dev_alias(sc->cdev, "pwm/%s", (char *)label);
120                 OF_prop_free(label);
121         }
122 }
123
124 #else /* FDT */
125
126 static void
127 pwmc_setup_label(struct pwmc_softc *sc)
128 {
129         const char *label;
130
131         if (resource_string_value(device_get_name(sc->dev),
132             device_get_unit(sc->dev), "label", &label) == 0) {
133                 make_dev_alias(sc->cdev, "pwm/%s", label);
134         }
135 }
136
137 #endif /* FDT */
138
139 static int
140 pwmc_probe(device_t dev)
141 {
142         int rv;
143
144         rv = BUS_PROBE_NOWILDCARD;
145
146 #ifdef FDT
147         if (!ofw_bus_status_okay(dev))
148                 return (ENXIO);
149
150         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
151                 rv = BUS_PROBE_DEFAULT;
152         }
153 #endif
154
155         device_set_desc(dev, "PWM Control");
156         return (rv);
157 }
158
159 static int
160 pwmc_attach(device_t dev)
161 {
162         struct pwmc_softc *sc;
163         struct make_dev_args args;
164         int error;
165
166         sc = device_get_softc(dev);
167         sc->dev = dev;
168
169         if ((error = pwmbus_get_channel(dev, &sc->chan)) != 0)
170                 return (error);
171
172         make_dev_args_init(&args);
173         args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
174         args.mda_devsw = &pwm_cdevsw;
175         args.mda_uid = UID_ROOT;
176         args.mda_gid = GID_OPERATOR;
177         args.mda_mode = 0660;
178         args.mda_si_drv1 = sc;
179         error = make_dev_s(&args, &sc->cdev, "pwm/pwmc%d.%d",
180             device_get_unit(device_get_parent(dev)), sc->chan);
181         if (error != 0) {
182                 device_printf(dev, "Failed to make PWM device\n");
183                 return (error);
184         }
185
186         pwmc_setup_label(sc);
187
188         return (0);
189 }
190
191 static int
192 pwmc_detach(device_t dev)
193 {
194         struct pwmc_softc *sc;
195  
196         sc = device_get_softc(dev);
197         destroy_dev(sc->cdev);
198
199         return (0);
200 }
201
202 static device_method_t pwmc_methods[] = {
203         /* device_if */
204         DEVMETHOD(device_probe, pwmc_probe),
205         DEVMETHOD(device_attach, pwmc_attach),
206         DEVMETHOD(device_detach, pwmc_detach),
207
208         DEVMETHOD_END
209 };
210
211 static driver_t pwmc_driver = {
212         "pwmc",
213         pwmc_methods,
214         sizeof(struct pwmc_softc),
215 };
216 static devclass_t pwmc_devclass;
217
218 DRIVER_MODULE(pwmc, pwmbus, pwmc_driver, pwmc_devclass, 0, 0);
219 MODULE_DEPEND(pwmc, pwmbus, 1, 1, 1);
220 MODULE_VERSION(pwmc, 1);