19312900fSEmmanuel Vadot /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
39312900fSEmmanuel Vadot *
49312900fSEmmanuel Vadot * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
59312900fSEmmanuel Vadot *
69312900fSEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
79312900fSEmmanuel Vadot * modification, are permitted provided that the following conditions
89312900fSEmmanuel Vadot * are met:
99312900fSEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
109312900fSEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
119312900fSEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
129312900fSEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
139312900fSEmmanuel Vadot * documentation and/or other materials provided with the distribution.
149312900fSEmmanuel Vadot *
159312900fSEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
169312900fSEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
179312900fSEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
189312900fSEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199312900fSEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
209312900fSEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
219312900fSEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
229312900fSEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
239312900fSEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
249312900fSEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
259312900fSEmmanuel Vadot * SUCH DAMAGE.
269312900fSEmmanuel Vadot */
279312900fSEmmanuel Vadot
289312900fSEmmanuel Vadot #include <sys/cdefs.h>
29b43e2c8bSIan Lepore #include "opt_platform.h"
30b43e2c8bSIan Lepore
319312900fSEmmanuel Vadot #include <sys/param.h>
329312900fSEmmanuel Vadot #include <sys/systm.h>
339312900fSEmmanuel Vadot #include <sys/bus.h>
349312900fSEmmanuel Vadot #include <sys/conf.h>
359312900fSEmmanuel Vadot #include <sys/kernel.h>
369312900fSEmmanuel Vadot #include <sys/module.h>
379312900fSEmmanuel Vadot #include <sys/time.h>
389312900fSEmmanuel Vadot
390af7a9a4SIan Lepore #include <dev/pwm/pwmbus.h>
4071fb3739SIan Lepore #include <dev/pwm/pwmc.h>
419312900fSEmmanuel Vadot
429312900fSEmmanuel Vadot #include "pwmbus_if.h"
439312900fSEmmanuel Vadot
44b43e2c8bSIan Lepore #ifdef FDT
45b43e2c8bSIan Lepore #include <dev/ofw/openfirm.h>
46b43e2c8bSIan Lepore #include <dev/ofw/ofw_bus.h>
47b43e2c8bSIan Lepore #include <dev/ofw/ofw_bus_subr.h>
48b43e2c8bSIan Lepore
49b43e2c8bSIan Lepore static struct ofw_compat_data compat_data[] = {
50b43e2c8bSIan Lepore {"freebsd,pwmc", true},
51b43e2c8bSIan Lepore {NULL, false},
52b43e2c8bSIan Lepore };
53519b64e2SMark Johnston
54519b64e2SMark Johnston PWMBUS_FDT_PNP_INFO(compat_data);
55519b64e2SMark Johnston
56b43e2c8bSIan Lepore #endif
57b43e2c8bSIan Lepore
589312900fSEmmanuel Vadot struct pwmc_softc {
599312900fSEmmanuel Vadot device_t dev;
600af7a9a4SIan Lepore struct cdev *cdev;
610af7a9a4SIan Lepore u_int chan;
629312900fSEmmanuel Vadot };
639312900fSEmmanuel Vadot
649312900fSEmmanuel Vadot static int
pwm_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)659312900fSEmmanuel Vadot pwm_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
669312900fSEmmanuel Vadot int fflag, struct thread *td)
679312900fSEmmanuel Vadot {
689312900fSEmmanuel Vadot struct pwmc_softc *sc;
699312900fSEmmanuel Vadot struct pwm_state state;
709312900fSEmmanuel Vadot device_t bus;
719312900fSEmmanuel Vadot int rv = 0;
729312900fSEmmanuel Vadot
739312900fSEmmanuel Vadot sc = dev->si_drv1;
74f8f8d87cSIan Lepore bus = device_get_parent(sc->dev);
759312900fSEmmanuel Vadot
769312900fSEmmanuel Vadot switch (cmd) {
779312900fSEmmanuel Vadot case PWMSETSTATE:
789312900fSEmmanuel Vadot bcopy(data, &state, sizeof(state));
790af7a9a4SIan Lepore rv = PWMBUS_CHANNEL_CONFIG(bus, sc->chan,
809312900fSEmmanuel Vadot state.period, state.duty);
8117b14d8fSOskar Holmund if (rv != 0)
8217b14d8fSOskar Holmund return (rv);
8317b14d8fSOskar Holmund
8417b14d8fSOskar Holmund rv = PWMBUS_CHANNEL_SET_FLAGS(bus,
8517b14d8fSOskar Holmund sc->chan, state.flags);
8617b14d8fSOskar Holmund if (rv != 0 && rv != EOPNOTSUPP)
8717b14d8fSOskar Holmund return (rv);
8817b14d8fSOskar Holmund
890af7a9a4SIan Lepore rv = PWMBUS_CHANNEL_ENABLE(bus, sc->chan,
909312900fSEmmanuel Vadot state.enable);
919312900fSEmmanuel Vadot break;
929312900fSEmmanuel Vadot case PWMGETSTATE:
939312900fSEmmanuel Vadot bcopy(data, &state, sizeof(state));
940af7a9a4SIan Lepore rv = PWMBUS_CHANNEL_GET_CONFIG(bus, sc->chan,
959312900fSEmmanuel Vadot &state.period, &state.duty);
969312900fSEmmanuel Vadot if (rv != 0)
979312900fSEmmanuel Vadot return (rv);
9817b14d8fSOskar Holmund
9917b14d8fSOskar Holmund rv = PWMBUS_CHANNEL_GET_FLAGS(bus, sc->chan,
10017b14d8fSOskar Holmund &state.flags);
10117b14d8fSOskar Holmund if (rv != 0)
10217b14d8fSOskar Holmund return (rv);
10317b14d8fSOskar Holmund
1040af7a9a4SIan Lepore rv = PWMBUS_CHANNEL_IS_ENABLED(bus, sc->chan,
1059312900fSEmmanuel Vadot &state.enable);
1069312900fSEmmanuel Vadot if (rv != 0)
1079312900fSEmmanuel Vadot return (rv);
1089312900fSEmmanuel Vadot bcopy(&state, data, sizeof(state));
1099312900fSEmmanuel Vadot break;
1109312900fSEmmanuel Vadot }
1119312900fSEmmanuel Vadot
1129312900fSEmmanuel Vadot return (rv);
1139312900fSEmmanuel Vadot }
1149312900fSEmmanuel Vadot
1159312900fSEmmanuel Vadot static struct cdevsw pwm_cdevsw = {
1169312900fSEmmanuel Vadot .d_version = D_VERSION,
1170af7a9a4SIan Lepore .d_name = "pwmc",
1189312900fSEmmanuel Vadot .d_ioctl = pwm_ioctl
1199312900fSEmmanuel Vadot };
1209312900fSEmmanuel Vadot
121b43e2c8bSIan Lepore static void
pwmc_setup_label(struct pwmc_softc * sc)122b43e2c8bSIan Lepore pwmc_setup_label(struct pwmc_softc *sc)
123b43e2c8bSIan Lepore {
124edd96b9fSIan Lepore const char *hintlabel;
125edd96b9fSIan Lepore #ifdef FDT
126b43e2c8bSIan Lepore void *label;
127b43e2c8bSIan Lepore
128b43e2c8bSIan Lepore if (OF_getprop_alloc(ofw_bus_get_node(sc->dev), "label", &label) > 0) {
129b43e2c8bSIan Lepore make_dev_alias(sc->cdev, "pwm/%s", (char *)label);
130b43e2c8bSIan Lepore OF_prop_free(label);
131b43e2c8bSIan Lepore }
132edd96b9fSIan Lepore #endif
133b43e2c8bSIan Lepore
134b43e2c8bSIan Lepore if (resource_string_value(device_get_name(sc->dev),
135edd96b9fSIan Lepore device_get_unit(sc->dev), "label", &hintlabel) == 0) {
136edd96b9fSIan Lepore make_dev_alias(sc->cdev, "pwm/%s", hintlabel);
137b43e2c8bSIan Lepore }
138b43e2c8bSIan Lepore }
139b43e2c8bSIan Lepore
1409312900fSEmmanuel Vadot static int
pwmc_probe(device_t dev)1419312900fSEmmanuel Vadot pwmc_probe(device_t dev)
1429312900fSEmmanuel Vadot {
143b43e2c8bSIan Lepore int rv;
144b43e2c8bSIan Lepore
145b43e2c8bSIan Lepore rv = BUS_PROBE_NOWILDCARD;
146b43e2c8bSIan Lepore
147b43e2c8bSIan Lepore #ifdef FDT
148b43e2c8bSIan Lepore if (!ofw_bus_status_okay(dev))
149b43e2c8bSIan Lepore return (ENXIO);
150b43e2c8bSIan Lepore
151b43e2c8bSIan Lepore if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
152b43e2c8bSIan Lepore rv = BUS_PROBE_DEFAULT;
153b43e2c8bSIan Lepore }
154b43e2c8bSIan Lepore #endif
1559312900fSEmmanuel Vadot
1560af7a9a4SIan Lepore device_set_desc(dev, "PWM Control");
157b43e2c8bSIan Lepore return (rv);
1589312900fSEmmanuel Vadot }
1599312900fSEmmanuel Vadot
1609312900fSEmmanuel Vadot static int
pwmc_attach(device_t dev)1619312900fSEmmanuel Vadot pwmc_attach(device_t dev)
1629312900fSEmmanuel Vadot {
1639312900fSEmmanuel Vadot struct pwmc_softc *sc;
1649312900fSEmmanuel Vadot struct make_dev_args args;
1650af7a9a4SIan Lepore int error;
1669312900fSEmmanuel Vadot
1679312900fSEmmanuel Vadot sc = device_get_softc(dev);
1689312900fSEmmanuel Vadot sc->dev = dev;
1699312900fSEmmanuel Vadot
1700af7a9a4SIan Lepore if ((error = pwmbus_get_channel(dev, &sc->chan)) != 0)
1710af7a9a4SIan Lepore return (error);
1720af7a9a4SIan Lepore
1739312900fSEmmanuel Vadot make_dev_args_init(&args);
1749312900fSEmmanuel Vadot args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
1759312900fSEmmanuel Vadot args.mda_devsw = &pwm_cdevsw;
1769312900fSEmmanuel Vadot args.mda_uid = UID_ROOT;
1779312900fSEmmanuel Vadot args.mda_gid = GID_OPERATOR;
1780af7a9a4SIan Lepore args.mda_mode = 0660;
1799312900fSEmmanuel Vadot args.mda_si_drv1 = sc;
180b5d67730SIan Lepore error = make_dev_s(&args, &sc->cdev, "pwm/pwmc%d.%d",
1810af7a9a4SIan Lepore device_get_unit(device_get_parent(dev)), sc->chan);
1820af7a9a4SIan Lepore if (error != 0) {
1839312900fSEmmanuel Vadot device_printf(dev, "Failed to make PWM device\n");
1840af7a9a4SIan Lepore return (error);
1859312900fSEmmanuel Vadot }
1860af7a9a4SIan Lepore
187b43e2c8bSIan Lepore pwmc_setup_label(sc);
1880af7a9a4SIan Lepore
1899312900fSEmmanuel Vadot return (0);
1909312900fSEmmanuel Vadot }
1919312900fSEmmanuel Vadot
1929312900fSEmmanuel Vadot static int
pwmc_detach(device_t dev)1939312900fSEmmanuel Vadot pwmc_detach(device_t dev)
1949312900fSEmmanuel Vadot {
1956bb80425SIan Lepore struct pwmc_softc *sc;
1966bb80425SIan Lepore
1976bb80425SIan Lepore sc = device_get_softc(dev);
1980af7a9a4SIan Lepore destroy_dev(sc->cdev);
1999312900fSEmmanuel Vadot
2009312900fSEmmanuel Vadot return (0);
2019312900fSEmmanuel Vadot }
2029312900fSEmmanuel Vadot
2039312900fSEmmanuel Vadot static device_method_t pwmc_methods[] = {
2049312900fSEmmanuel Vadot /* device_if */
2059312900fSEmmanuel Vadot DEVMETHOD(device_probe, pwmc_probe),
2069312900fSEmmanuel Vadot DEVMETHOD(device_attach, pwmc_attach),
2079312900fSEmmanuel Vadot DEVMETHOD(device_detach, pwmc_detach),
2089312900fSEmmanuel Vadot
2099312900fSEmmanuel Vadot DEVMETHOD_END
2109312900fSEmmanuel Vadot };
2119312900fSEmmanuel Vadot
2126bb80425SIan Lepore static driver_t pwmc_driver = {
2139312900fSEmmanuel Vadot "pwmc",
2149312900fSEmmanuel Vadot pwmc_methods,
2159312900fSEmmanuel Vadot sizeof(struct pwmc_softc),
2169312900fSEmmanuel Vadot };
2179312900fSEmmanuel Vadot
218024d9473SJohn Baldwin DRIVER_MODULE(pwmc, pwmbus, pwmc_driver, 0, 0);
219f8f8d87cSIan Lepore MODULE_DEPEND(pwmc, pwmbus, 1, 1, 1);
2209312900fSEmmanuel Vadot MODULE_VERSION(pwmc, 1);
221