1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012 Robert N. M. Watson
5 * All rights reserved.
6 *
7 * This software was developed by SRI International and the University of
8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9 * ("CTSRD"), as part of the DARPA CRASH research programme.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36#include <sys/param.h>
37#include <sys/bus.h>
38#include <sys/lock.h>
39#include <sys/mutex.h>
40#include <sys/rman.h>
41
42#include <machine/bus.h>
43#include <machine/resource.h>
44
45#include <dev/led/led.h>
46#include <dev/terasic/de4led/terasic_de4led.h>
47
48devclass_t	terasic_de4led_devclass;
49
50static void
51terasic_de4led_update(struct terasic_de4led_softc *sc)
52{
53
54	TERASIC_DE4LED_LOCK_ASSERT(sc);
55
56	bus_write_1(sc->tdl_res, TERASIC_DE4LED_OFF_LED, sc->tdl_bits);
57}
58
59static void
60led_update(struct terasic_de4led_softc *sc, int bit, int onoff)
61{
62
63	TERASIC_DE4LED_LOCK(sc);
64	TERASIC_DE4LED_SETLED(sc, bit, onoff);
65	terasic_de4led_update(sc);
66	TERASIC_DE4LED_UNLOCK(sc);
67}
68
69static void
70led_0(void *arg, int onoff)
71{
72
73	led_update(arg, 0, onoff);
74}
75
76static void
77led_1(void *arg, int onoff)
78{
79
80	led_update(arg, 1, onoff);
81}
82
83static void
84led_2(void *arg, int onoff)
85{
86
87	led_update(arg, 2, onoff);
88}
89
90static void
91led_3(void *arg, int onoff)
92{
93
94	led_update(arg, 3, onoff);
95}
96
97static void
98led_4(void *arg, int onoff)
99{
100
101	led_update(arg, 4, onoff);
102}
103
104static void
105led_5(void *arg, int onoff)
106{
107
108	led_update(arg, 5, onoff);
109}
110
111static void
112led_6(void *arg, int onoff)
113{
114
115	led_update(arg, 6, onoff);
116}
117
118static void
119led_7(void *arg, int onoff)
120{
121
122	led_update(arg, 7, onoff);
123}
124
125void
126terasic_de4led_attach(struct terasic_de4led_softc *sc)
127{
128	const char *cmd;
129
130	TERASIC_DE4LED_LOCK_INIT(sc);
131
132	/*
133	 * Clear the LED array before we start.
134	 */
135	TERASIC_DE4LED_LOCK(sc);
136	TERASIC_DE4LED_CLEARBAR(sc);
137	terasic_de4led_update(sc);
138	TERASIC_DE4LED_UNLOCK(sc);
139
140	/*
141	 * Register the LED array with led(4).
142	 */
143	sc->tdl_leds[0] = led_create(led_0, sc, "de4led_0");
144	sc->tdl_leds[1] = led_create(led_1, sc, "de4led_1");
145	sc->tdl_leds[2] = led_create(led_2, sc, "de4led_2");
146	sc->tdl_leds[3] = led_create(led_3, sc, "de4led_3");
147	sc->tdl_leds[4] = led_create(led_4, sc, "de4led_4");
148	sc->tdl_leds[5] = led_create(led_5, sc, "de4led_5");
149	sc->tdl_leds[6] = led_create(led_6, sc, "de4led_6");
150	sc->tdl_leds[7] = led_create(led_7, sc, "de4led_7");
151
152	if (resource_string_value(device_get_name(sc->tdl_dev),
153	    sc->tdl_unit, "de4led_0_cmd", &cmd) == 0)
154		led_set("de4led_0", cmd);
155	if (resource_string_value(device_get_name(sc->tdl_dev),
156	    sc->tdl_unit, "de4led_1_cmd", &cmd) == 0)
157		led_set("de4led_1", cmd);
158	if (resource_string_value(device_get_name(sc->tdl_dev),
159	    sc->tdl_unit, "de4led_2_cmd", &cmd) == 0)
160		led_set("de4led_2", cmd);
161	if (resource_string_value(device_get_name(sc->tdl_dev),
162	    sc->tdl_unit, "de4led_3_cmd", &cmd) == 0)
163		led_set("de4led_3", cmd);
164	if (resource_string_value(device_get_name(sc->tdl_dev),
165	    sc->tdl_unit, "de4led_4_cmd", &cmd) == 0)
166		led_set("de4led_4", cmd);
167	if (resource_string_value(device_get_name(sc->tdl_dev),
168	    sc->tdl_unit, "de4led_5_cmd", &cmd) == 0)
169		led_set("de4led_5", cmd);
170	if (resource_string_value(device_get_name(sc->tdl_dev),
171	    sc->tdl_unit, "de4led_6_cmd", &cmd) == 0)
172		led_set("de4led_6", cmd);
173	if (resource_string_value(device_get_name(sc->tdl_dev),
174	    sc->tdl_unit, "de4led_7_cmd", &cmd) == 0)
175		led_set("de4led_7", cmd);
176}
177
178void
179terasic_de4led_detach(struct terasic_de4led_softc *sc)
180{
181	int i;
182
183	for (i = 0; i < 8; i++) {
184		led_destroy(sc->tdl_leds[i]);
185		sc->tdl_leds[i] = NULL;
186	}
187	TERASIC_DE4LED_LOCK(sc);
188	TERASIC_DE4LED_CLEARBAR(sc);
189	terasic_de4led_update(sc);
190	TERASIC_DE4LED_UNLOCK(sc);
191	TERASIC_DE4LED_LOCK_DESTROY(sc);
192}
193