Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / hpcmips / dev / teliosio.c
blobe0ca506b7b149e3d68749ffba7c6975c06bb4bd6
1 /* $NetBSD: teliosio.c,v 1.2 2005/12/11 12:17:33 christos Exp $ */
3 /*
4 * Copyright (c) 2005 Takeshi Nakayama.
5 * All rights reserved.
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Sharp Telios machine dependent I/O (LCD backlight/Battery unit) driver
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: teliosio.c,v 1.2 2005/12/11 12:17:33 christos Exp $");
35 #include "apmdev.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
41 #include <machine/bus.h>
42 #include <machine/config_hook.h>
44 #include <hpcmips/tx/tx39var.h>
45 #include <hpcmips/tx/txcsbusvar.h>
47 #if NAPMDEV > 0
48 #include <dev/apm/apmbios.h>
49 #define __TX39IO_PRIVATE
50 #include <hpcmips/tx/tx39ioreg.h>
51 #include <hpcmips/tx/tx39timerreg.h>
52 #endif
54 #include <hpcmips/dev/teliosioreg.h>
56 struct teliosio_softc {
57 struct device sc_dev;
58 tx_chipset_tag_t sc_tc;
59 bus_space_tag_t sc_regt;
60 bus_space_handle_t sc_regh;
62 int sc_backlight;
63 int sc_brightness;
64 #define MAX_BRIGHTNESS 9
65 #define BRIGHTNESSVAL(n) \
66 ((n) <= 0 ? 0 : ((n) <= MAX_BRIGHTNESS ? (n) : MAX_BRIGHTNESS) * 9 - 1)
69 static int teliosio_match(struct device *, struct cfdata *, void *);
70 static void teliosio_attach(struct device *, struct device *, void *);
71 static int teliosio_event(void *, int, long, void *);
73 /* LCD backlight control */
74 static void teliosio_backlight(struct teliosio_softc *, int);
75 static void teliosio_brightness(struct teliosio_softc *, int);
77 /* Battery unit control */
78 #if NAPMDEV > 0
79 static int teliosio_ac_state(struct teliosio_softc *);
80 static int teliosio_mbu_state(struct teliosio_softc *);
81 static void teliosio_mbu_write(tx_chipset_tag_t, int);
82 static int teliosio_mbu_read(tx_chipset_tag_t);
83 #endif
85 CFATTACH_DECL(teliosio, sizeof(struct teliosio_softc),
86 teliosio_match, teliosio_attach, NULL, NULL);
88 static int
89 teliosio_match(struct device *parent, struct cfdata *cf, void *aux)
92 return 2; /* attach before plumvideo */
95 static void
96 teliosio_attach(struct device *parent, struct device *self, void *aux)
98 struct cs_attach_args *ca = aux;
99 struct teliosio_softc *sc = (void *)self;
101 sc->sc_tc = ca->ca_tc;
102 sc->sc_regt = ca->ca_csreg.cstag;
104 if (bus_space_map(sc->sc_regt, TELIOSIO_REGBASE, TELIOSIO_REGSIZE,
105 0, &sc->sc_regh)) {
106 printf(": register map failed\n");
107 return;
109 printf("\n");
111 sc->sc_backlight = 1;
112 sc->sc_brightness = MAX_BRIGHTNESS;
114 config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_POWER_LCDLIGHT,
115 CONFIG_HOOK_SHARE, teliosio_event, sc);
116 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_POWER_LCDLIGHT,
117 CONFIG_HOOK_SHARE, teliosio_event, sc);
118 config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS,
119 CONFIG_HOOK_SHARE, teliosio_event, sc);
120 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS,
121 CONFIG_HOOK_SHARE, teliosio_event, sc);
122 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS_MAX,
123 CONFIG_HOOK_SHARE, teliosio_event, sc);
124 #if NAPMDEV > 0
125 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_ACADAPTER,
126 CONFIG_HOOK_SHARE, teliosio_event, sc);
127 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BATTERYVAL,
128 CONFIG_HOOK_SHARE, teliosio_event, sc);
129 #endif
132 static int
133 teliosio_event(void *ctx, int type, long id, void *msg)
135 struct teliosio_softc *sc = ctx;
137 switch (type) {
138 case CONFIG_HOOK_SET:
139 switch (id) {
140 case CONFIG_HOOK_POWER_LCDLIGHT:
141 teliosio_backlight(sc, (int)msg);
142 break;
143 case CONFIG_HOOK_BRIGHTNESS:
144 teliosio_brightness(sc, *(int *)msg + 1);
145 break;
146 default:
147 return 1;
149 break;
150 case CONFIG_HOOK_GET:
151 switch (id) {
152 case CONFIG_HOOK_POWER_LCDLIGHT:
153 *(int *)msg = sc->sc_backlight;
154 break;
155 case CONFIG_HOOK_BRIGHTNESS:
156 *(int *)msg = sc->sc_brightness - 1;
157 break;
158 case CONFIG_HOOK_BRIGHTNESS_MAX:
159 *(int *)msg = MAX_BRIGHTNESS - 1;
160 break;
161 #if NAPMDEV > 0
162 case CONFIG_HOOK_ACADAPTER:
163 *(int *)msg = teliosio_ac_state(sc);
164 break;
165 case CONFIG_HOOK_BATTERYVAL:
166 *(int *)msg = teliosio_mbu_state(sc) / 2;
167 break;
168 #endif
169 default:
170 return 1;
172 break;
173 default:
174 return 1;
177 return 0;
181 * LCD backlight control
183 static void
184 teliosio_backlight(struct teliosio_softc *sc, int on)
186 int brightness;
188 sc->sc_backlight = on;
189 if (on) {
190 teliosio_brightness(sc, sc->sc_brightness);
191 } else {
192 brightness = sc->sc_brightness;
193 teliosio_brightness(sc, 0);
194 sc->sc_brightness = brightness;
198 static void
199 teliosio_brightness(struct teliosio_softc *sc, int val)
201 bus_space_tag_t regt = sc->sc_regt;
202 bus_space_handle_t regh = sc->sc_regh;
203 u_int16_t period;
205 if (val < 0)
206 val = 0;
207 else if (val > MAX_BRIGHTNESS)
208 val = MAX_BRIGHTNESS;
209 sc->sc_brightness = val;
211 period = bus_space_read_2(regt, regh, TELIOSIO_BACKLIGHT_PERIOD);
212 if (val)
213 period |= TELIOSIO_BACKLIGHT_PERIOD_EN;
214 else
215 period &= ~TELIOSIO_BACKLIGHT_PERIOD_EN;
216 bus_space_write_2(regt, regh, TELIOSIO_BACKLIGHT_PERIOD, period);
217 bus_space_write_2(regt, regh, TELIOSIO_BACKLIGHT_RESET,
218 BRIGHTNESSVAL(val));
222 * Battery unit control
224 #if NAPMDEV > 0
225 static int
226 teliosio_ac_state(struct teliosio_softc *sc)
228 tx_chipset_tag_t tc = sc->sc_tc;
229 txreg_t reg;
231 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
232 return (reg & TELIOSIO_AC_STATE) ? APM_AC_OFF : APM_AC_ON;
235 static int
236 teliosio_mbu_state(struct teliosio_softc *sc)
238 tx_chipset_tag_t tc = sc->sc_tc;
239 int val;
241 teliosio_mbu_write(tc, 0x00);
242 delay(7000);
243 val = teliosio_mbu_read(tc);
244 delay(7000);
246 return val;
249 #define WAIT (1000000 / TELIOSIO_BMU_CLOCK_FREQ / 2)
251 static void
252 teliosio_mbu_write(tx_chipset_tag_t tc, int val)
254 txreg_t reg;
255 int s;
256 int mask;
258 for (mask = 0x80; mask != 0; mask >>= 1) {
259 s = splhigh();
260 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
261 reg |= TELIOSIO_BMU_CLOCK;
262 if (val & mask)
263 reg |= TELIOSIO_BMU_DATAOUT;
264 else
265 reg &= ~TELIOSIO_BMU_DATAOUT;
266 tx_conf_write(tc, TX392X_IODATAINOUT_REG, reg);
267 splx(s);
268 delay(WAIT);
270 s = splhigh();
271 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
272 reg &= ~TELIOSIO_BMU_CLOCK;
273 tx_conf_write(tc, TX392X_IODATAINOUT_REG, reg);
274 splx(s);
275 delay(WAIT);
279 static int
280 teliosio_mbu_read(tx_chipset_tag_t tc)
282 txreg_t reg;
283 int s;
284 int mask, val;
286 val = 0;
287 for (mask = 0x80; mask != 0; mask >>= 1) {
288 s = splhigh();
289 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
290 reg |= TELIOSIO_BMU_CLOCK;
291 tx_conf_write(tc, TX392X_IODATAINOUT_REG, reg);
292 splx(s);
293 delay(WAIT);
295 s = splhigh();
296 reg = tx_conf_read(tc, TX392X_IODATAINOUT_REG);
297 if (!(reg & TELIOSIO_BMU_DATAIN))
298 val |= mask;
299 reg &= ~TELIOSIO_BMU_CLOCK;
300 tx_conf_write(tc, TX392X_IODATAINOUT_REG, reg);
301 splx(s);
302 delay(WAIT);
305 return val;
307 #endif /* NAPMDEV */