Merge branch 'vendor/GREP'
[dragonfly.git] / sys / dev / misc / gpio / gpio_led.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/limits.h>
40 #include <sys/malloc.h>
41 #include <sys/ctype.h>
42 #include <sys/sbuf.h>
43 #include <sys/queue.h>
44 #include <dev/misc/gpio/gpio.h>
45 #include <sys/uio.h>
46 #include <sys/lock.h>
47 #include <sys/devfs.h>
48
49 struct ledsc {
50         LIST_ENTRY(ledsc)       list;
51         struct gpio *gp;
52         int             pin;
53         cdev_t  dev;
54         int             unit;
55         int             opened;
56         char    *name;
57         struct gpio_mapping *gp_map;
58 };
59
60 DEVFS_DECLARE_CLONE_BITMAP(nled);
61 static struct lock led_lock;
62 static LIST_HEAD(, ledsc) led_list = LIST_HEAD_INITIALIZER(&led_list);
63 static MALLOC_DEFINE(M_LED, "LED", "LED driver");
64
65
66 static int
67 led_open(struct dev_open_args *ap)
68 {
69         struct ledsc *sc;
70         cdev_t  dev;
71
72         dev = ap->a_head.a_dev;
73         sc = dev->si_drv1;
74
75         if (sc->opened)
76                 return EBUSY;
77
78         sc->opened = 1;
79
80         return 0;
81 }
82
83 static int
84 led_close(struct dev_close_args *ap)
85 {
86         struct ledsc *sc;
87         cdev_t  dev;
88
89         dev = ap->a_head.a_dev;
90         sc = dev->si_drv1;
91
92         if (sc->opened)
93                 sc->opened = 0;
94
95         return 0;
96 }
97
98 static int
99 led_write(struct dev_write_args *ap)
100 {
101         struct ledsc *sc;
102         cdev_t  dev;
103         int             error;
104         int             data = 0;
105         int             len;
106
107         dev = ap->a_head.a_dev;
108         sc = dev->si_drv1;
109
110         if (ap->a_uio->uio_resid > sizeof(int))
111                 return EINVAL;
112
113         len = ap->a_uio->uio_resid;
114
115         error = uiomove((void *)&data, ap->a_uio->uio_resid, ap->a_uio);
116         if (error)
117                 return error;
118
119         if (len > 1)
120                 data = ((char *)&data)[0];
121
122         if (data >= '0')
123                 data -= '0';
124
125         gpio_pin_write(sc->gp, sc->gp_map, 0, data);
126
127         return 0;
128 }
129
130 static int
131 led_read(struct dev_read_args *ap)
132 {
133         struct ledsc *sc;
134         cdev_t  dev;
135         int             error;
136         int             data = 0;
137
138         dev = ap->a_head.a_dev;
139         sc = dev->si_drv1;
140
141         if (ap->a_uio->uio_resid < sizeof(int))
142                 return EINVAL;
143
144         data = gpio_pin_read(sc->gp, sc->gp_map, 0);
145
146         error = uiomove((void *)&data,
147             (ap->a_uio->uio_resid > sizeof(int))?(sizeof(int)):(ap->a_uio->uio_resid),
148                 ap->a_uio);
149
150         return error;
151 }
152
153 static int
154 led_ioctl(struct dev_ioctl_args *ap)
155 {
156         /* XXX: set a name */
157         return 0;
158 }
159
160 static struct dev_ops nled_ops = {
161         { "gpio", 0, 0 },
162         .d_open  =      led_open,
163         .d_close =      led_close,
164         .d_write =      led_write,
165         .d_read  =      led_read,
166         .d_ioctl =      led_ioctl,
167 };
168
169
170 static int
171 led_attach(struct gpio *gp, void *arg, int pin, u_int32_t mask)
172 {
173         struct ledsc *sc;
174
175         if (arg == NULL)
176                 return 1;
177
178         lockmgr(&led_lock, LK_EXCLUSIVE);
179         sc = kmalloc(sizeof(struct ledsc), M_LED, M_WAITOK);
180
181         /* XXX: check for name collisions */
182         sc->name = kstrdup((char *)arg, M_LED);
183         sc->pin = pin;
184         sc->gp = gp;
185
186         sc->gp_map = gpio_map(gp, NULL, pin, 1);
187         if (sc->gp_map == NULL) {
188                 return 2;
189         }
190
191         sc->unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(nled), 0);
192
193         LIST_INSERT_HEAD(&led_list, sc, list);
194         sc->dev = make_dev(&nled_ops, sc->unit,
195             UID_ROOT, GID_WHEEL, 0600, "led/%s", sc->name);
196         sc->dev->si_drv1 = sc;
197         lockmgr(&led_lock, LK_RELEASE);
198
199         kprintf("gpio_led: Attached led '%s' to gpio %s, pin %d\n",
200             sc->name, sc->gp->driver_name, pin);
201
202         return 0;
203 }
204
205 static int
206 led_detach(struct gpio *gp, void *arg, int pin)
207 {
208         /* XXX: implement */
209         return 0;
210 }
211
212 void
213 led_switch(const char *name, int on_off)
214 {
215         struct ledsc *sc;
216
217         if (name == NULL)
218                 return;
219
220         lockmgr(&led_lock, LK_EXCLUSIVE);
221         LIST_FOREACH(sc, &led_list, list) {
222                 if (strcmp(name, sc->name) != 0)
223                         continue;
224
225                 gpio_pin_write(sc->gp, sc->gp_map, 0, on_off);
226                 break;
227         }
228
229         lockmgr(&led_lock, LK_RELEASE);
230 }
231
232 struct gpio_consumer led_gpio_cons = {
233         .consumer_name = "led",
234         .consumer_attach = led_attach,
235         .consumer_detach = led_detach,
236 };
237
238 static void
239 led_drvinit(void *unused)
240 {
241         lockinit(&led_lock, "led_lock", 0, 0);
242         devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(nled));
243         gpio_consumer_register(&led_gpio_cons);
244 }
245
246 SYSINIT(leddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, led_drvinit, NULL);