vrc4172pwm.c revision 1.2 1 /* $NetBSD: vrc4172pwm.c,v 1.2 2000/11/11 10:08:12 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((struct vr4172pwm_softc *));
66 void vrc4172pwm_light __P((struct vr4172pwm_softc *, int));
67 int vrc4172pwm_get_brightness __P((struct vr4172pwm_softc *));
68 void vrc4172pwm_set_brightness __P((struct vr4172pwm_softc *), int);
69
70
71 struct cfattach vrc4172pwm_ca = {
72 sizeof(struct vrc4172pwm_softc), vrc4172pwmmatch, vrc4172pwmattach
73 };
74
75 struct vrc4172pwm_softc *this_pwm;
76
77 static inline void
78 vrc4172pwm_write(sc, port, val)
79 struct vrc4172pwm_softc *sc;
80 int port;
81 unsigned short val;
82 {
83 bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
84 }
85
86 static inline unsigned short
87 vrc4172pwm_read(sc, port)
88 struct vrc4172pwm_softc *sc;
89 int port;
90 {
91 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
92 }
93
94 static int
95 vrc4172pwmmatch(parent, cf, aux)
96 struct device *parent;
97 struct cfdata *cf;
98 void *aux;
99 {
100 return 1;
101 }
102
103 static void
104 vrc4172pwmattach(parent, self, aux)
105 struct device *parent;
106 struct device *self;
107 void *aux;
108 {
109 struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)self;
110 struct vrip_attach_args *va = aux;
111
112 bus_space_tag_t iot = va->va_iot;
113 bus_space_handle_t ioh;
114
115 if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
116 printf(": can't map bus space\n");
117 return;
118 }
119
120 sc->sc_iot = iot;
121 sc->sc_ioh = ioh;
122
123 printf("\n");
124
125 VDUMPREG(sc);
126 /* basic setup */
127 sc->sc_pmhook = config_hook(CONFIG_HOOK_PMEVENT,
128 CONFIG_PMEVENT_HARDPOWER,
129 CONFIG_HOOK_SHARE,
130 vrc4172pwm_pmevent, sc);
131 sc->sc_hook = config_hook(CONFIG_HOOK_POWERCONTROL,
132 CONFIG_POWERCONTROL_LCDLIGHT,
133 CONFIG_HOOK_SHARE,
134 vrc4172pwm_event, sc);
135 sc->sc_sethook = config_hook(CONFIG_HOOK_SET,
136 CONFIG_SET_BRIGHTNESS,
137 CONFIG_HOOK_SHARE,
138 vrc4172pwm_event, sc);
139 sc->sc_gethook = config_hook(CONFIG_HOOK_GET,
140 CONFIG_GET_BRIGHTNESS,
141 CONFIG_HOOK_SHARE,
142 vrc4172pwm_event, sc);
143
144 vr4172pwm_init_brightness(sc);
145 this_pwm = sc;
146 }
147
148 /*
149 *
150 * Initialize PWM brightness parameters
151 *
152 */
153 void
154 vrc4172pwm_init_brightness(sc)
155 {
156 sc->sc_raw_freq = vrc4172pwm_read(sc, VRC2_PWM_LCDFREQ);
157 sc->sc_raw_duty = vrc4172pwm_read(sc, VRC2_PWM_LCDDUTY);
158 sc->sc_param = vrc4172pwm_getparam();
159 sc->sc_brightness = vrc4172pwm_raw_duty2brightness(sc);
160 }
161
162 /*
163 * PWM config hook events
164 *
165 */
166 int
167 vrc4172pwm_event(ctx, type, id, msg)
168 void *ctx;
169 int type;
170 long id;
171 void *msg;
172 {
173 struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)ctx;
174 int why =(int)msg;
175
176 if (type == CONFIG_HOOK_POWERCONTROL
177 && id == CONFIG_HOOK_POWERCONTROL_LCDLIGHT) {
178 vrc4172pwm_light(sc, why);
179 } else if (type == CONFIG_HOOK_GET
180 && id == CONFIG_HOOK_GET_BRIGHTNESS) {
181 *(int *)msg = vr4172pwm_get_brightness(sc);
182 } else if (type == CONFIG_HOOK_SET
183 && id == CONFIG_HOOK_GET_BRIGHTNESS) {
184 vr4172pwm_set_brightness(sc, *(int *)msg);
185 } else
186 return 1;
187
188 return (0);
189 }
190
191
192 /*
193 * PWM config hook events
194 *
195 */
196 int
197 vrc4172pwm_pmevent(ctx, type, id, msg)
198 void *ctx;
199 int type;
200 long id;
201 void *msg;
202 {
203 struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)ctx;
204 int why =(int)msg;
205
206 if (type != CONFIG_HOOK_PMEVENT)
207 return 1;
208
209 switch (why) {
210 case PWR_STANBY:
211 case PWR_SUSPEND:
212 vrc4172pwm_light(sc, 0);
213 break;
214 case PWR_RESUME:
215 vrc4172pwm_light(sc, 1);
216 break;
217 default:
218 return 1;
219 }
220
221 return (0);
222 }
223
224 /*
225 *
226 */
227 void
228 vrc4172pwm_light(sc, on)
229 struct vrc4172pwm_softc *sc;
230 int on;
231 {
232 if (on)
233 vrc4172pwm_write(sc, VRC2_PWM_LCDDUTYEN, VRC2_PWM_LCDEN);
234 else
235 vrc4172pwm_write(sc, VRC2_PWM_LCDDUTYEN, VRC2_PWM_LCDDIS);
236 }
237
238 /*
239 * set brightness
240 */
241 void
242 vrc4172pwm_set_brightness(sc, val)
243 struct vrc4172pwm_softc *sc;
244 int val;
245 {
246 int raw;
247
248 if (val > VRC2_PWM_MAX_BRIGHTNESS)
249 val = VRC2_PWM_MAX_BRIGHTNESS;
250 if (val > sc->sc_param->max_brightness)
251 val = sc->sc_param->max_brightness;
252 raw = vrc4172pwm_brightness2rawduty(sc, val);
253 vrc4172pwm_write(sc, VRC2_PWM_LCDDUTY, raw);
254 sc->sc_brightness = val;
255 }
256
257 /*
258 * get brightness
259 */
260 int
261 vrc4172pwm_set_brightness(sc)
262 struct vrc4172pwm_softc *sc;
263 {
264 return sc->sc_brightness;
265 }
266
267 /*
268 * dump pwm registers
269 */
270 void
271 vrc4172pwm_dumpreg(sc)
272 struct vrc4172pwm_softc *sc;
273 {
274 int en, freq, duty;
275
276 en = vrc4172pwm_read(sc, VRC2_PWM_LCDDUTYEN);
277 freq = vrc4172pwm_read(sc, VRC2_PWM_LCDFREQ);
278 duty = vrc4172pwm_read(sc, VRC2_PWM_LCDDUTY);
279
280 printf("vrc4172pwm: lightenable = %d, freq = 0x%x, duty = 0x%x\n",
281 en, freq, duty);
282 }
283
284 /* end */
285