vrc4172pwm.c revision 1.1 1 /* $NetBSD: vrc4172pwm.c,v 1.1 2000/11/11 04:42:09 sato Exp $ */
2
3 /*
4 * Copyright (c) 2000 SATO Kazumi. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/device.h>
31 #include <sys/reboot.h>
32
33 #include <machine/bus.h>
34 #include <machine/config_hook.h>
35 #include <machine/platid.h>
36
37 #include <hpcmips/vr/vrc4172pwmvar.h>
38 #include <hpcmips/vr/vrc4172pwmreg.h>
39
40
41 #ifdef VRC2PWMDEBUG
42 #ifndef VRC2PWMDEBUG_CONF
43 #define VRC2PWMDEBUG_CONF 0
44 #endif /* VRC2PWMDEBUG_CONF */
45 int vrc4172pwmdebug = VRC2PWMDEBUG_CONF;
46 #define DPRINTF(arg) if (vrc4172pwmdebug) printf arg;
47 #define VPRINTF(arg) if (bootverbose||vrc4172pwmdebug) printf arg;
48 #define VDUMPREG(arg) if (bootverbose||vrc4172pwmdebug) vrc4172_dumpreg arg;
49 #else /* VRC2PWMDEBUG */
50 #define DPRINTF(arg)
51 #define VPRINTF(arg) if (bootverbose) printf arg;
52 #define VDUMPREG(arg) if (bootverbose) vrc4172_dumpreg arg;
53 #endif /* VRC2PWMDEBUG */
54
55 static int vrc4172pwmmatch __P((struct device *, struct cfdata *, void *));
56 static void vrc4172pwmattach __P((struct device *, struct device *, void *));
57
58 static void vrc4172pwm_write __P((struct vrc4172pwm_softc *, int, unsigned short));
59 static unsigned short vrc4172pwm_read __P((struct vrc4172pwm_softc *, int));
60
61 static int vrc4172pwm_event __P((void *, int, long, void *));
62 static int vrc4172pwm_pmevent __P((void *, int, long, void *));
63
64 static void vrc4172pwm_dumpreg __P((struct vrc4172pwm_softc *));
65 static void vrc4172pwm_init_brightness __P((void));
66
67
68 struct cfattach vrc4172pwm_ca = {
69 sizeof(struct vrc4172pwm_softc), vrc4172pwmmatch, vrc4172pwmattach
70 };
71
72 struct vrc4172pwm_softc *this_pwm;
73
74 static inline void
75 vrc4172pwm_write(sc, port, val)
76 struct vrc4172pwm_softc *sc;
77 int port;
78 unsigned short val;
79 {
80 bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
81 }
82
83 static inline unsigned short
84 vrc4172pwm_read(sc, port)
85 struct vrc4172pwm_softc *sc;
86 int port;
87 {
88 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
89 }
90
91 static int
92 vrc4172pwmmatch(parent, cf, aux)
93 struct device *parent;
94 struct cfdata *cf;
95 void *aux;
96 {
97 return 1;
98 }
99
100 static void
101 vrc4172pwmattach(parent, self, aux)
102 struct device *parent;
103 struct device *self;
104 void *aux;
105 {
106 struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)self;
107 struct vrip_attach_args *va = aux;
108
109 bus_space_tag_t iot = va->va_iot;
110 bus_space_handle_t ioh;
111
112 if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
113 printf(": can't map bus space\n");
114 return;
115 }
116
117 sc->sc_iot = iot;
118 sc->sc_ioh = ioh;
119
120 printf("\n");
121
122 VDUMPREG(sc);
123 /* basic setup */
124 sc->sc_pmhook = config_hook(CONFIG_HOOK_PMEVENT,
125 CONFIG_PMEVENT_HARDPOWER,
126 CONFIG_HOOK_SHARE,
127 vrc4172pwm_pmevent, sc);
128 sc->sc_hook = config_hook(CONFIG_HOOK_POWERCONTROL,
129 CONFIG_POWERCONTROL_LCDLIGHT,
130 CONFIG_HOOK_SHARE,
131 vrc4172pwm_event, sc);
132 sc->sc_sethook = config_hook(CONFIG_HOOK_SET,
133 CONFIG_SET_BRIGHTNESS,
134 CONFIG_HOOK_SHARE,
135 vrc4172pwm_event, sc);
136 sc->sc_gethook = config_hook(CONFIG_HOOK_GET,
137 CONFIG_GET_BRIGHTNESS,
138 CONFIG_HOOK_SHARE,
139 vrc4172pwm_event, sc);
140
141 vr4172pwm_init_brightness();
142 this_pwm = sc;
143 }
144
145
146 /*
147 * PWM config hook events
148 *
149 */
150 int
151 vrc4172pwm_event(ctx, type, id, msg)
152 void *ctx;
153 int type;
154 long id;
155 void *msg;
156 {
157 struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)ctx;
158 int why =(int)msg;
159
160 if (type == CONFIG_HOOK_POWERCONTROL
161 && id == CONFIG_HOOK_POWERCONTROL_LCDLIGHT) {
162 vrc4172pwm_light(why);
163 } else if (type == CONFIG_HOOK_GET
164 && id == CONFIG_HOOL_GET_BRIGHTNESS) {
165 *(int *)msg = vr4172pwm_get_brightness(sc);
166 } else if (type == CONFIG_HOOK_SET
167 && id == CONFIG_HOOL_GET_BRIGHTNESS) {
168 vr4172pwm_set_brightness(sc, *(int *)msg);
169 } else
170 return 1;
171
172 return (0);
173 }
174
175
176 /*
177 * PWM config hook events
178 *
179 */
180 int
181 vrc4172pwm_pmevent(ctx, type, id, msg)
182 void *ctx;
183 int type;
184 long id;
185 void *msg;
186 {
187 struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)ctx;
188 int why =(int)msg;
189
190 if (type != CONFIG_HOOK_PMEVENT)
191 return 1;
192
193 switch (why) {
194 }
195 return (0);
196 }
197
198 /*
199 * dump pwm registers
200 */
201 void
202 vrc4172pwm_dumpreg(sc)
203 struct vrc4172pwm_softc *sc;
204 {
205 int en, freq, duty;
206
207 en = vrc4172pwm_read(sc, VRC2_PWM_LCDDUTYEN);
208 freq = vrc4172pwm_read(sc, VRC2_PWM_LCDFREQ);
209 duty = vrc4172pwm_read(sc, VRC2_PWM_LCDDUTY);
210
211 printf("vrc4172pwm: lightenable = %d, freq = 0x%x, duty = 0x%x\n",
212 en, freq, duty);
213 }
214 /* end */
215