Deleted Added
full compact
gpioc.c (233767) gpioc.c (274670)
1/*-
2 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/gpio/gpioc.c 233767 2012-04-02 00:11:26Z gonzo $");
28__FBSDID("$FreeBSD: head/sys/dev/gpio/gpioc.c 274670 2014-11-18 17:22:08Z loos $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/types.h>
33
34#include <sys/bus.h>
35#include <sys/conf.h>
32#include <sys/bus.h>
33#include <sys/conf.h>
34#include <sys/gpio.h>
36#include <sys/ioccom.h>
37#include <sys/kernel.h>
38#include <sys/malloc.h>
39#include <sys/module.h>
35#include <sys/ioccom.h>
36#include <sys/kernel.h>
37#include <sys/malloc.h>
38#include <sys/module.h>
40#include <sys/queue.h>
41#include <machine/bus.h>
42#include <machine/resource.h>
43
39
44#include <sys/gpio.h>
40#include <dev/gpio/gpiobusvar.h>
41
45#include "gpio_if.h"
46
47#undef GPIOC_DEBUG
48#ifdef GPIOC_DEBUG
49#define dprintf printf
50#else
51#define dprintf(x, arg...)
52#endif
53
54static int gpioc_probe(device_t dev);
55static int gpioc_attach(device_t dev);
56static int gpioc_detach(device_t dev);
57
58static d_ioctl_t gpioc_ioctl;
59
60static struct cdevsw gpioc_cdevsw = {
61 .d_version = D_VERSION,
62 .d_ioctl = gpioc_ioctl,
63 .d_name = "gpioc",
64};
65
66struct gpioc_softc {
67 device_t sc_dev; /* gpiocX dev */
68 device_t sc_pdev; /* gpioX dev */
69 struct cdev *sc_ctl_dev; /* controller device */
70 int sc_unit;
71};
72
73static int
74gpioc_probe(device_t dev)
75{
76 device_set_desc(dev, "GPIO controller");
77 return (0);
78}
79
80static int
81gpioc_attach(device_t dev)
82{
83 struct gpioc_softc *sc = device_get_softc(dev);
84
85 sc->sc_dev = dev;
86 sc->sc_pdev = device_get_parent(dev);
87 sc->sc_unit = device_get_unit(dev);
88 sc->sc_ctl_dev = make_dev(&gpioc_cdevsw, sc->sc_unit,
89 UID_ROOT, GID_WHEEL, 0600, "gpioc%d", sc->sc_unit);
90 if (!sc->sc_ctl_dev) {
91 printf("Failed to create gpioc%d", sc->sc_unit);
92 return (ENXIO);
93 }
94 sc->sc_ctl_dev->si_drv1 = sc;
95
96 return (0);
97}
98
99static int
100gpioc_detach(device_t dev)
101{
102 struct gpioc_softc *sc = device_get_softc(dev);
103 int err;
104
105 if (sc->sc_ctl_dev)
106 destroy_dev(sc->sc_ctl_dev);
107
108 if ((err = bus_generic_detach(dev)) != 0)
109 return (err);
110
111 return (0);
112}
113
114static int
115gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag,
116 struct thread *td)
117{
118 int max_pin, res;
119 struct gpioc_softc *sc = cdev->si_drv1;
120 struct gpio_pin pin;
121 struct gpio_req req;
42#include "gpio_if.h"
43
44#undef GPIOC_DEBUG
45#ifdef GPIOC_DEBUG
46#define dprintf printf
47#else
48#define dprintf(x, arg...)
49#endif
50
51static int gpioc_probe(device_t dev);
52static int gpioc_attach(device_t dev);
53static int gpioc_detach(device_t dev);
54
55static d_ioctl_t gpioc_ioctl;
56
57static struct cdevsw gpioc_cdevsw = {
58 .d_version = D_VERSION,
59 .d_ioctl = gpioc_ioctl,
60 .d_name = "gpioc",
61};
62
63struct gpioc_softc {
64 device_t sc_dev; /* gpiocX dev */
65 device_t sc_pdev; /* gpioX dev */
66 struct cdev *sc_ctl_dev; /* controller device */
67 int sc_unit;
68};
69
70static int
71gpioc_probe(device_t dev)
72{
73 device_set_desc(dev, "GPIO controller");
74 return (0);
75}
76
77static int
78gpioc_attach(device_t dev)
79{
80 struct gpioc_softc *sc = device_get_softc(dev);
81
82 sc->sc_dev = dev;
83 sc->sc_pdev = device_get_parent(dev);
84 sc->sc_unit = device_get_unit(dev);
85 sc->sc_ctl_dev = make_dev(&gpioc_cdevsw, sc->sc_unit,
86 UID_ROOT, GID_WHEEL, 0600, "gpioc%d", sc->sc_unit);
87 if (!sc->sc_ctl_dev) {
88 printf("Failed to create gpioc%d", sc->sc_unit);
89 return (ENXIO);
90 }
91 sc->sc_ctl_dev->si_drv1 = sc;
92
93 return (0);
94}
95
96static int
97gpioc_detach(device_t dev)
98{
99 struct gpioc_softc *sc = device_get_softc(dev);
100 int err;
101
102 if (sc->sc_ctl_dev)
103 destroy_dev(sc->sc_ctl_dev);
104
105 if ((err = bus_generic_detach(dev)) != 0)
106 return (err);
107
108 return (0);
109}
110
111static int
112gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag,
113 struct thread *td)
114{
115 int max_pin, res;
116 struct gpioc_softc *sc = cdev->si_drv1;
117 struct gpio_pin pin;
118 struct gpio_req req;
119 uint32_t caps;
122
123 switch (cmd) {
124 case GPIOMAXPIN:
125 max_pin = -1;
126 res = GPIO_PIN_MAX(sc->sc_pdev, &max_pin);
127 bcopy(&max_pin, arg, sizeof(max_pin));
128 break;
129 case GPIOGETCONFIG:
130 bcopy(arg, &pin, sizeof(pin));
131 dprintf("get config pin %d\n", pin.gp_pin);
132 res = GPIO_PIN_GETFLAGS(sc->sc_pdev, pin.gp_pin,
133 &pin.gp_flags);
134 /* Fail early */
135 if (res)
136 break;
137 GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &pin.gp_caps);
138 GPIO_PIN_GETNAME(sc->sc_pdev, pin.gp_pin, pin.gp_name);
139 bcopy(&pin, arg, sizeof(pin));
140 break;
141 case GPIOSETCONFIG:
142 bcopy(arg, &pin, sizeof(pin));
143 dprintf("set config pin %d\n", pin.gp_pin);
120
121 switch (cmd) {
122 case GPIOMAXPIN:
123 max_pin = -1;
124 res = GPIO_PIN_MAX(sc->sc_pdev, &max_pin);
125 bcopy(&max_pin, arg, sizeof(max_pin));
126 break;
127 case GPIOGETCONFIG:
128 bcopy(arg, &pin, sizeof(pin));
129 dprintf("get config pin %d\n", pin.gp_pin);
130 res = GPIO_PIN_GETFLAGS(sc->sc_pdev, pin.gp_pin,
131 &pin.gp_flags);
132 /* Fail early */
133 if (res)
134 break;
135 GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &pin.gp_caps);
136 GPIO_PIN_GETNAME(sc->sc_pdev, pin.gp_pin, pin.gp_name);
137 bcopy(&pin, arg, sizeof(pin));
138 break;
139 case GPIOSETCONFIG:
140 bcopy(arg, &pin, sizeof(pin));
141 dprintf("set config pin %d\n", pin.gp_pin);
144 res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin,
145 pin.gp_flags);
142 res = GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &caps);
143 if (res == 0)
144 res = gpio_check_flags(caps, pin.gp_flags);
145 if (res == 0)
146 res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin,
147 pin.gp_flags);
146 break;
147 case GPIOGET:
148 bcopy(arg, &req, sizeof(req));
149 res = GPIO_PIN_GET(sc->sc_pdev, req.gp_pin,
150 &req.gp_value);
151 dprintf("read pin %d -> %d\n",
152 req.gp_pin, req.gp_value);
153 bcopy(&req, arg, sizeof(req));
154 break;
155 case GPIOSET:
156 bcopy(arg, &req, sizeof(req));
157 res = GPIO_PIN_SET(sc->sc_pdev, req.gp_pin,
158 req.gp_value);
159 dprintf("write pin %d -> %d\n",
160 req.gp_pin, req.gp_value);
161 break;
162 case GPIOTOGGLE:
163 bcopy(arg, &req, sizeof(req));
164 dprintf("toggle pin %d\n",
165 req.gp_pin);
166 res = GPIO_PIN_TOGGLE(sc->sc_pdev, req.gp_pin);
167 break;
168 default:
169 return (ENOTTY);
170 break;
171 }
172
173 return (res);
174}
175
176static device_method_t gpioc_methods[] = {
177 /* Device interface */
178 DEVMETHOD(device_probe, gpioc_probe),
179 DEVMETHOD(device_attach, gpioc_attach),
180 DEVMETHOD(device_detach, gpioc_detach),
181 DEVMETHOD(device_shutdown, bus_generic_shutdown),
182 DEVMETHOD(device_suspend, bus_generic_suspend),
183 DEVMETHOD(device_resume, bus_generic_resume),
184
185 { 0, 0 }
186};
187
188driver_t gpioc_driver = {
189 "gpioc",
190 gpioc_methods,
191 sizeof(struct gpioc_softc)
192};
193
194devclass_t gpioc_devclass;
195
196DRIVER_MODULE(gpioc, gpio, gpioc_driver, gpioc_devclass, 0, 0);
197MODULE_VERSION(gpioc, 1);
148 break;
149 case GPIOGET:
150 bcopy(arg, &req, sizeof(req));
151 res = GPIO_PIN_GET(sc->sc_pdev, req.gp_pin,
152 &req.gp_value);
153 dprintf("read pin %d -> %d\n",
154 req.gp_pin, req.gp_value);
155 bcopy(&req, arg, sizeof(req));
156 break;
157 case GPIOSET:
158 bcopy(arg, &req, sizeof(req));
159 res = GPIO_PIN_SET(sc->sc_pdev, req.gp_pin,
160 req.gp_value);
161 dprintf("write pin %d -> %d\n",
162 req.gp_pin, req.gp_value);
163 break;
164 case GPIOTOGGLE:
165 bcopy(arg, &req, sizeof(req));
166 dprintf("toggle pin %d\n",
167 req.gp_pin);
168 res = GPIO_PIN_TOGGLE(sc->sc_pdev, req.gp_pin);
169 break;
170 default:
171 return (ENOTTY);
172 break;
173 }
174
175 return (res);
176}
177
178static device_method_t gpioc_methods[] = {
179 /* Device interface */
180 DEVMETHOD(device_probe, gpioc_probe),
181 DEVMETHOD(device_attach, gpioc_attach),
182 DEVMETHOD(device_detach, gpioc_detach),
183 DEVMETHOD(device_shutdown, bus_generic_shutdown),
184 DEVMETHOD(device_suspend, bus_generic_suspend),
185 DEVMETHOD(device_resume, bus_generic_resume),
186
187 { 0, 0 }
188};
189
190driver_t gpioc_driver = {
191 "gpioc",
192 gpioc_methods,
193 sizeof(struct gpioc_softc)
194};
195
196devclass_t gpioc_devclass;
197
198DRIVER_MODULE(gpioc, gpio, gpioc_driver, gpioc_devclass, 0, 0);
199MODULE_VERSION(gpioc, 1);