]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/pwm/pwmc.c
Rework pwmbus and pwmc so that each child will handle a single PWM channel.
[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 <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/time.h>
39
40 #include <dev/pwm/pwmbus.h>
41 #include <dev/pwm/pwmc.h>
42
43 #include "pwmbus_if.h"
44
45 struct pwmc_softc {
46         device_t        dev;
47         struct cdev     *cdev;
48         u_int           chan;
49 };
50
51 static int
52 pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
53     int fflag, struct thread *td)
54 {
55         struct pwmc_softc *sc;
56         struct pwm_state state;
57         device_t bus;
58         u_int nchannel;
59         int rv = 0;
60
61         sc = dev->si_drv1;
62         bus = device_get_parent(sc->dev);
63
64         switch (cmd) {
65         case PWMMAXCHANNEL:
66                 nchannel = 0;
67                 rv = PWMBUS_CHANNEL_COUNT(bus, &nchannel);
68                 bcopy(&nchannel, data, sizeof(nchannel));
69                 break;
70         case PWMSETSTATE:
71                 bcopy(data, &state, sizeof(state));
72                 rv = PWMBUS_CHANNEL_CONFIG(bus, sc->chan,
73                     state.period, state.duty);
74                 if (rv == 0)
75                         rv = PWMBUS_CHANNEL_ENABLE(bus, sc->chan,
76                             state.enable);
77                 break;
78         case PWMGETSTATE:
79                 bcopy(data, &state, sizeof(state));
80                 rv = PWMBUS_CHANNEL_GET_CONFIG(bus, sc->chan,
81                     &state.period, &state.duty);
82                 if (rv != 0)
83                         return (rv);
84                 rv = PWMBUS_CHANNEL_IS_ENABLED(bus, sc->chan,
85                     &state.enable);
86                 if (rv != 0)
87                         return (rv);
88                 bcopy(&state, data, sizeof(state));
89                 break;
90         }
91
92         return (rv);
93 }
94
95 static struct cdevsw pwm_cdevsw = {
96         .d_version      = D_VERSION,
97         .d_name         = "pwmc",
98         .d_ioctl        = pwm_ioctl
99 };
100
101 static int
102 pwmc_probe(device_t dev)
103 {
104
105         device_set_desc(dev, "PWM Control");
106         return (BUS_PROBE_NOWILDCARD);
107 }
108
109 static int
110 pwmc_attach(device_t dev)
111 {
112         struct pwmc_softc *sc;
113         struct make_dev_args args;
114         const char *label;
115         int error;
116
117         sc = device_get_softc(dev);
118         sc->dev = dev;
119
120         if ((error = pwmbus_get_channel(dev, &sc->chan)) != 0)
121                 return (error);
122
123         make_dev_args_init(&args);
124         args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
125         args.mda_devsw = &pwm_cdevsw;
126         args.mda_uid = UID_ROOT;
127         args.mda_gid = GID_OPERATOR;
128         args.mda_mode = 0660;
129         args.mda_si_drv1 = sc;
130         error = make_dev_s(&args, &sc->cdev, "pwmc%d.%d",
131             device_get_unit(device_get_parent(dev)), sc->chan);
132         if (error != 0) {
133                 device_printf(dev, "Failed to make PWM device\n");
134                 return (error);
135         }
136
137         /* If there is a label hint, create an alias with that name. */
138         if (resource_string_value(device_get_name(dev), device_get_unit(dev),
139             "label", &label) == 0) {
140                 make_dev_alias(sc->cdev, "pwm/%s", label);
141         }
142
143         return (0);
144 }
145
146 static int
147 pwmc_detach(device_t dev)
148 {
149         struct pwmc_softc *sc;
150  
151         sc = device_get_softc(dev);
152         destroy_dev(sc->cdev);
153
154         return (0);
155 }
156
157 static device_method_t pwmc_methods[] = {
158         /* device_if */
159         DEVMETHOD(device_probe, pwmc_probe),
160         DEVMETHOD(device_attach, pwmc_attach),
161         DEVMETHOD(device_detach, pwmc_detach),
162
163         DEVMETHOD_END
164 };
165
166 static driver_t pwmc_driver = {
167         "pwmc",
168         pwmc_methods,
169         sizeof(struct pwmc_softc),
170 };
171 static devclass_t pwmc_devclass;
172
173 DRIVER_MODULE(pwmc, pwmbus, pwmc_driver, pwmc_devclass, 0, 0);
174 MODULE_DEPEND(pwmc, pwmbus, 1, 1, 1);
175 MODULE_VERSION(pwmc, 1);