xref: /netbsd-src/sys/arch/hpcarm/dev/j720lcd.c (revision 4c494f7678a94d301a0b4643623bcfe63b8f2cbb)
1 /*	$NetBSD: j720lcd.c,v 1.5 2009/05/29 14:15:45 rjs 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.5 2009/05/29 14:15:45 rjs 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)	aprint_normal arg
56 #else
57 #define DPRINTF(arg)	/* nothing */
58 #endif
59 
60 struct j720lcd_softc {
61 	device_t		sc_dev;
62 
63 	struct j720ssp_softc	*sc_ssp;
64 };
65 
66 static int	j720lcd_match(device_t, cfdata_t, void *);
67 static void	j720lcd_attach(device_t, device_t, void *);
68 
69 static int	j720lcd_param(void *, int, long, void *);
70 int		j720lcd_power(void *, int, long, void *);
71 
72 CFATTACH_DECL_NEW(j720lcd, sizeof(struct j720lcd_softc),
73     j720lcd_match, j720lcd_attach, NULL, NULL);
74 
75 
76 static int
j720lcd_match(device_t parent,cfdata_t cf,void * aux)77 j720lcd_match(device_t parent, cfdata_t 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
j720lcd_attach(device_t parent,device_t self,void * aux)89 j720lcd_attach(device_t parent, device_t self, void *aux)
90 {
91 	struct j720lcd_softc *sc = device_private(self);
92 	int brightness, contrast;
93 
94 	sc->sc_dev = self;
95 	sc->sc_ssp = device_private(parent);
96 
97 	/* LCD brightness hooks. */
98 	config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS,
99 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
100 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS,
101 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
102 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS_MAX,
103 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
104 
105 	/* LCD contrast hooks. */
106 	config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST,
107 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
108 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST,
109 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
110 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST_MAX,
111 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
112 
113 	/* LCD power hook. */
114 #if 0
115 	config_hook(CONFIG_HOOK_POWERCONTROL,
116 		    CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
117 		    CONFIG_HOOK_SHARE, j720lcd_power, sc);
118 #endif
119 
120 	/* Get default brightness/contrast values. */
121 	config_hook_call(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS, &brightness);
122 	config_hook_call(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST, &contrast);
123 
124 	aprint_normal(": brightness %d, contrast %d\n", brightness, contrast);
125 }
126 
127 static int
j720lcd_param(void * ctx,int type,long id,void * msg)128 j720lcd_param(void *ctx, int type, long id, void *msg)
129 {
130 	struct j720lcd_softc *sc = ctx;
131 	struct j720ssp_softc *ssp = sc->sc_ssp;
132 	uint32_t data[2], len;
133 	const int maxval = 255;
134 	int i, s;
135 
136 	switch (type) {
137 	case CONFIG_HOOK_GET:
138 		switch (id) {
139 		case CONFIG_HOOK_BRIGHTNESS_MAX:
140 		case CONFIG_HOOK_CONTRAST_MAX:
141 			*(int *)msg = maxval;
142 			return 1;
143 		case CONFIG_HOOK_BRIGHTNESS:
144 			data[0] = 0xd6;
145 			data[1] = 0x11;
146 			len = 2;
147 			break;
148 		case CONFIG_HOOK_CONTRAST:
149 			data[0] = 0xd4;
150 			data[1] = 0x11;
151 			len = 2;
152 			break;
153 		default:
154 			return 0;
155 		}
156 		break;
157 
158 	case CONFIG_HOOK_SET:
159 		switch (id) {
160 		case CONFIG_HOOK_BRIGHTNESS:
161 			if (*(int *)msg >= 0) {
162 				data[0] = 0xd3;
163 				data[1] = maxval - *(int *)msg;
164 				len = 2;
165 			} else {
166 				/* XXX hack */
167 				data[0] = 0xdf;
168 				len = 1;
169 			}
170 			break;
171 		case CONFIG_HOOK_CONTRAST:
172 			data[0] = 0xd1;
173 			data[1] = maxval - *(int *)msg;
174 			len = 2;
175 			break;
176 		default:
177 			return 0;
178 		}
179 		break;
180 
181 	default:
182 		return 0;
183 	}
184 
185 	s = splbio();
186 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
187 
188 	for (i = 0; i < len; i++) {
189 		if (j720ssp_readwrite(ssp, 1, data[i], &data[i], 500) < 0)
190 			goto out;
191 	}
192 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
193 	splx(s);
194 
195 	if (type == CONFIG_HOOK_SET)
196 		return 1;
197 
198 	*(int *)msg = maxval - data[1];
199 
200 	return 1;
201 
202 out:
203 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
204 
205 	/* reset SSP */
206 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
207 	delay(100);
208 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
209 
210 	splx(s);
211 
212 	DPRINTF(("j720lcd_param: error %x %x\n", data[0], data[1]));
213 	return 0;
214 
215 }
216 
217 int
j720lcd_power(void * ctx,int type,long id,void * msg)218 j720lcd_power(void *ctx, int type, long id, void *msg)
219 {
220 	struct sed1356_softc *sc = ctx;
221 	struct sa11x0_softc *psc = sc->sc_parent;
222 	uint32_t reg;
223 	int val;
224 
225 	if (type != CONFIG_HOOK_POWERCONTROL ||
226 	    id != CONFIG_HOOK_POWERCONTROL_LCDLIGHT)
227 		return 0;
228 
229 	sed1356_init_brightness(sc, 0);
230 	sed1356_init_contrast(sc, 0);
231 
232 	if (msg) {
233 		bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 0);
234 
235 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
236 		reg |= 0x1;
237 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
238 		delay(50000);
239 
240 		val = sc->sc_contrast;
241 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
242 		delay(100000);
243 
244 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
245 		reg |= 0x4;
246 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
247 
248 		val = sc->sc_brightness;
249 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
250 
251 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
252 		reg |= 0x2;
253 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
254 	} else {
255 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
256 		reg &= ~0x2;
257 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
258 		reg &= ~0x4;
259 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
260 		delay(100000);
261 
262 		val = -2;
263 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
264 
265 		bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 1);
266 
267 		delay(100000);
268 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
269 		reg &= ~0x1;
270 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
271 	}
272 
273 	return 1;
274 }
275