j720lcd.c revision 1.3.64.1 1 /* $NetBSD: j720lcd.c,v 1.3.64.1 2008/05/16 02:22:27 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by IWAMOTO Toshihiro.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /* Jornada 720 LCD screen driver. */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: j720lcd.c,v 1.3.64.1 2008/05/16 02:22:27 yamt Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/kernel.h>
41
42 #include <machine/config_hook.h>
43 #include <machine/platid.h>
44 #include <machine/platid_mask.h>
45
46 #include <arm/sa11x0/sa11x0_var.h>
47 #include <arm/sa11x0/sa11x0_gpioreg.h>
48 #include <arm/sa11x0/sa11x0_ppcreg.h>
49 #include <arm/sa11x0/sa11x0_sspreg.h>
50
51 #include <hpcarm/dev/j720sspvar.h>
52 #include <hpcarm/dev/sed1356var.h>
53
54 #ifdef DEBUG
55 #define DPRINTF(arg) printf arg
56 #else
57 #define DPRINTF(arg) /* nothing */
58 #endif
59
60 struct j720lcd_softc {
61 struct device sc_dev;
62
63 struct j720ssp_softc *sc_ssp;
64 };
65
66 static int j720lcd_match(struct device *, struct cfdata *, void *);
67 static void j720lcd_attach(struct device *, struct device *, void *);
68
69 static int j720lcd_param(void *, int, long, void *);
70 int j720lcd_power(void *, int, long, void *);
71
72 CFATTACH_DECL(j720lcd, sizeof(struct j720lcd_softc),
73 j720lcd_match, j720lcd_attach, NULL, NULL);
74
75
76 static int
77 j720lcd_match(struct device *parent, struct cfdata *cf, void *aux)
78 {
79
80 if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
81 return 0;
82 if (strcmp(cf->cf_name, "j720lcd") != 0)
83 return 0;
84
85 return 1;
86 }
87
88 static void
89 j720lcd_attach(struct device *parent, struct device *self, void *aux)
90 {
91 struct j720lcd_softc *sc = (struct j720lcd_softc *)self;
92 int brightness, contrast;
93
94 sc->sc_ssp = (struct j720ssp_softc *)parent;
95
96 /* LCD brightness hooks. */
97 config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS,
98 CONFIG_HOOK_SHARE, j720lcd_param, sc);
99 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS,
100 CONFIG_HOOK_SHARE, j720lcd_param, sc);
101 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS_MAX,
102 CONFIG_HOOK_SHARE, j720lcd_param, sc);
103
104 /* LCD contrast hooks. */
105 config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST,
106 CONFIG_HOOK_SHARE, j720lcd_param, sc);
107 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST,
108 CONFIG_HOOK_SHARE, j720lcd_param, sc);
109 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST_MAX,
110 CONFIG_HOOK_SHARE, j720lcd_param, sc);
111
112 /* LCD power hook. */
113 #if 0
114 config_hook(CONFIG_HOOK_POWERCONTROL,
115 CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
116 CONFIG_HOOK_SHARE, j720lcd_power, sc);
117 #endif
118
119 /* Get default brightness/contrast values. */
120 config_hook_call(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS, &brightness);
121 config_hook_call(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST, &contrast);
122
123 printf(": brightness %d, contrast %d\n", brightness, contrast);
124 }
125
126 static int
127 j720lcd_param(void *ctx, int type, long id, void *msg)
128 {
129 struct j720lcd_softc *sc = ctx;
130 struct j720ssp_softc *ssp = sc->sc_ssp;
131 uint32_t data[2], len;
132 const int maxval = 255;
133 int i, s;
134
135 switch (type) {
136 case CONFIG_HOOK_GET:
137 switch (id) {
138 case CONFIG_HOOK_BRIGHTNESS_MAX:
139 case CONFIG_HOOK_CONTRAST_MAX:
140 *(int *)msg = maxval;
141 return 1;
142 case CONFIG_HOOK_BRIGHTNESS:
143 data[0] = 0xd6;
144 data[1] = 0x11;
145 len = 2;
146 break;
147 case CONFIG_HOOK_CONTRAST:
148 data[0] = 0xd4;
149 data[1] = 0x11;
150 len = 2;
151 break;
152 default:
153 return 0;
154 }
155 break;
156
157 case CONFIG_HOOK_SET:
158 switch (id) {
159 case CONFIG_HOOK_BRIGHTNESS:
160 if (*(int *)msg >= 0) {
161 data[0] = 0xd3;
162 data[1] = maxval - *(int *)msg;
163 len = 2;
164 } else {
165 /* XXX hack */
166 data[0] = 0xdf;
167 len = 1;
168 }
169 break;
170 case CONFIG_HOOK_CONTRAST:
171 data[0] = 0xd1;
172 data[1] = maxval - *(int *)msg;
173 len = 2;
174 break;
175 default:
176 return 0;
177 }
178 break;
179
180 default:
181 return 0;
182 }
183
184 s = splbio();
185 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
186
187 for (i = 0; i < len; i++) {
188 if (j720ssp_readwrite(ssp, 1, data[i], &data[i], 500) < 0)
189 goto out;
190 }
191 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
192 splx(s);
193
194 if (type == CONFIG_HOOK_SET)
195 return 1;
196
197 *(int *)msg = maxval - data[1];
198
199 return 1;
200
201 out:
202 bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
203
204 /* reset SSP */
205 bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
206 delay(100);
207 bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
208
209 splx(s);
210
211 DPRINTF(("j720lcd_param: error %x %x\n", data[0], data[1]));
212 return 0;
213
214 }
215
216 int
217 j720lcd_power(void *ctx, int type, long id, void *msg)
218 {
219 struct sed1356_softc *sc = ctx;
220 struct sa11x0_softc *psc = sc->sc_parent;
221 uint32_t reg;
222 int val;
223
224 if (type != CONFIG_HOOK_POWERCONTROL ||
225 id != CONFIG_HOOK_POWERCONTROL_LCDLIGHT)
226 return 0;
227
228 sed1356_init_brightness(sc, 0);
229 sed1356_init_contrast(sc, 0);
230
231 if (msg) {
232 bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 0);
233
234 reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
235 reg |= 0x1;
236 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
237 delay(50000);
238
239 val = sc->sc_contrast;
240 config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
241 delay(100000);
242
243 reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
244 reg |= 0x4;
245 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
246
247 val = sc->sc_brightness;
248 config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
249
250 reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
251 reg |= 0x2;
252 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
253 } else {
254 reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
255 reg &= ~0x2;
256 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
257 reg &= ~0x4;
258 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
259 delay(100000);
260
261 val = -2;
262 config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
263
264 bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 1);
265
266 delay(100000);
267 reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
268 reg &= ~0x1;
269 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
270 }
271
272 return 1;
273 }
274