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