Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*	$NetBSD: j6x0lcd.c,v 1.14 2009/04/05 02:23:00 uwe Exp $ */

/*
 * Copyright (c) 2004, 2005 Valeriy E. Ushakov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: j6x0lcd.c,v 1.14 2009/04/05 02:23:00 uwe Exp $");

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/systm.h>

#include <machine/platid.h>
#include <machine/platid_mask.h>

#include <machine/config_hook.h>

#include <sh3/dacreg.h>
#include <hpcsh/dev/hd64461/hd64461var.h> /* XXX: for hd64461_reg_read_2 &c */
#include <hpcsh/dev/hd64461/hd64461reg.h>
#include <hpcsh/dev/hd64461/hd64461gpioreg.h>

#define arraysize(ary) (sizeof(ary) / sizeof(ary[0]))


/*
 * LCD power: controlled by pin 0 in HD64461 GPIO port B.
 *   0 - power on
 *   1 - power off
 */
#define HD64461_GPBDR_J6X0_LCD_OFF	0x01

#define HD64461_GPBCR_J6X0_LCD_OFF_MASK	0xfffc
#define HD64461_GPBCR_J6X0_LCD_OFF_BITS	0x0001


/*
 * LCD brightness: controlled by DAC channel 0.  Larger channel values
 * mean dimmer.  Values smaller (i.e. brighter) then 0x5e seems to
 * result in no visible changes.
 */
#define J6X0LCD_BRIGHTNESS_DA_MAX	0x5e
#define J6X0LCD_BRIGHTNESS_DA_MIN	0xff

#define J6X0LCD_DA_TO_BRIGHTNESS(da) \
	(J6X0LCD_BRIGHTNESS_DA_MIN - (da))

#define J6X0LCD_BRIGHTNESS_TO_DA(br) \
	(J6X0LCD_BRIGHTNESS_DA_MIN - (br))

#define J6X0LCD_BRIGHTNESS_MAX \
	J6X0LCD_DA_TO_BRIGHTNESS(J6X0LCD_BRIGHTNESS_DA_MAX)

/* convenience macro to accesses DAC registers */
#define DAC_(x)    (*((volatile uint8_t *)SH7709_DA ## x))


/*
 * LCD contrast in 680 is controlled by pins 6..3 of HD64461 GPIO
 * port B.  6th pin is the least significant bit, 3rd pin is the most
 * significant.  The bits are inverted: 0 = .1111...; 1 = .0111...;
 * etc.  Larger values mean "blacker".
 *
 * The contrast value is programmed by setting bits in the data
 * register to all ones, and changing the mode of the pins in the
 * control register, setting logical "ones" to GPIO output mode (1),
 * and switching "zeroes" to input mode (3).
 */
#define HD64461_GPBDR_J680_CONTRAST_BITS	0x78	/* set */
#define HD64461_GPBCR_J680_CONTRAST_MASK	0xc03f

static const uint8_t j6x0lcd_contrast680_pins[] = { 6, 5, 4, 3 };

static const uint16_t j6x0lcd_contrast680_control_bits[] = {
	0x1540, 0x3540, 0x1d40, 0x3d40, 0x1740, 0x3740, 0x1f40, 0x3f40,
	0x15c0, 0x35c0, 0x1dc0, 0x3dc0, 0x17c0, 0x37c0, 0x1fc0, 0x3fc0
};


/*
 * LCD contrast in 620lx is controlled by pins 7,6,3,4,5 of HD64461
 * GPIO port B (in the order from the least significant to the most
 * significant).  The bits are inverted: 0 = 11111...; 5 = 01110...;
 * etc.  Larger values mean "whiter".
 *
 * The contrast value is programmed by setting bits in the data
 * register to all zeroes, and changing the mode of the pins in the
 * control register, setting logical "ones" to GPIO output mode (1),
 * and switching "zeroes" to input mode (3).
 */
#define HD64461_GPBDR_J620LX_CONTRAST_BITS	0xf8	/* clear */
#define HD64461_GPBCR_J620LX_CONTRAST_MASK	0x003f

static const uint8_t j6x0lcd_contrast620lx_pins[] = { 7, 6, 3, 4, 5 };

static const uint16_t j6x0lcd_contrast620lx_control_bits[] = {
	0xffc0, 0x7fc0, 0xdfc0, 0x5fc0, 0xff40, 0x7f40, 0xdf40, 0x5f40,
	0xfdc0, 0x7dc0, 0xddc0, 0x5dc0, 0xfd40, 0x7d40, 0xdd40, 0x5d40,
	0xf7c0, 0x77c0, 0xd7c0, 0x57c0, 0xf740, 0x7740, 0xd740, 0x5740,
	0xf5c0, 0x75c0, 0xd5c0, 0x55c0, 0xf540, 0x7540, 0xd540, 0x5540
};



struct j6x0lcd_softc {
	device_t sc_dev;

	int sc_brightness;
	int sc_contrast;

	int sc_contrast_max;
	uint16_t sc_contrast_mask;
	const uint16_t *sc_contrast_control_bits;
};

static int	j6x0lcd_match(device_t, cfdata_t, void *);
static void	j6x0lcd_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(j6x0lcd, sizeof(struct j6x0lcd_softc),
    j6x0lcd_match, j6x0lcd_attach, NULL, NULL);


static int	j6x0lcd_param(void *, int, long, void *);
static int	j6x0lcd_power(void *, int, long, void *);

static int	j6x0lcd_contrast_raw(uint16_t, int, const uint8_t *);
static void	j6x0lcd_contrast_set(struct j6x0lcd_softc *, int);



static int
j6x0lcd_match(device_t parent, cfdata_t cf, void *aux)
{

	/*
	 * XXX: platid_mask_MACH_HP_LX also matches 360LX.  It's not
	 * confirmed whether touch panel in 360LX is connected this
	 * way.  We may need to regroup platid masks.
	 */
	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX)
	    && !platid_match(&platid, &platid_mask_MACH_HP_LX))
		return (0);

	if (strcmp(cf->cf_name, "j6x0lcd") != 0)
		return (0);

	return (1);
}


static void
j6x0lcd_attach(device_t parent, device_t self, void *aux)
{
	struct j6x0lcd_softc *sc;
	uint16_t bcr, bdr;
	uint8_t dcr, ddr;

	aprint_naive("\n");

	sc = device_private(self);
	sc->sc_dev = self;

	/*
	 * Brightness is controlled by DAC channel 0.
	 */
	dcr = DAC_(CR);
	dcr &= ~SH7709_DACR_DAE; /* want to control each channel separately */
	dcr |= SH7709_DACR_DAOE0; /* enable channel 0 */
	DAC_(CR) = dcr;

	ddr = DAC_(DR0);
	sc->sc_brightness = J6X0LCD_DA_TO_BRIGHTNESS(ddr);

	/*
	 * Contrast and power are controlled by HD64461 GPIO port B.
	 */
	bcr = hd64461_reg_read_2(HD64461_GPBCR_REG16);
	bdr = hd64461_reg_read_2(HD64461_GPBDR_REG16);

	/*
	 * Make sure LCD is turned on.
	 */
	bcr &= HD64461_GPBCR_J6X0_LCD_OFF_MASK;
	bcr |= HD64461_GPBCR_J6X0_LCD_OFF_BITS; /* output mode */

	bdr &= ~HD64461_GPBDR_J6X0_LCD_OFF;

	/*
	 * 620LX and 680 have different contrast control.
	 */
	if (platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX)) {
		bdr |= HD64461_GPBDR_J680_CONTRAST_BITS;

		sc->sc_contrast_mask =
			HD64461_GPBCR_J680_CONTRAST_MASK;
		sc->sc_contrast_control_bits =
			j6x0lcd_contrast680_control_bits;
		sc->sc_contrast_max =
			arraysize(j6x0lcd_contrast680_control_bits) - 1;

		sc->sc_contrast = sc->sc_contrast_max
			- j6x0lcd_contrast_raw(bcr,
				arraysize(j6x0lcd_contrast680_pins),
				j6x0lcd_contrast680_pins);
	} else {
		bdr &= ~HD64461_GPBDR_J620LX_CONTRAST_BITS;

		sc->sc_contrast_mask =
			HD64461_GPBCR_J620LX_CONTRAST_MASK;
		sc->sc_contrast_control_bits =
			j6x0lcd_contrast620lx_control_bits;
		sc->sc_contrast_max =
			arraysize(j6x0lcd_contrast620lx_control_bits) - 1;

		sc->sc_contrast =
			j6x0lcd_contrast_raw(bcr,
				arraysize(j6x0lcd_contrast620lx_pins),
				j6x0lcd_contrast620lx_pins);
	}

	hd64461_reg_write_2(HD64461_GPBCR_REG16, bcr);
	hd64461_reg_write_2(HD64461_GPBDR_REG16, bdr);

	aprint_normal(": brightness %d, contrast %d\n",
		      sc->sc_brightness, sc->sc_contrast);


	/* LCD brightness hooks */
	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS_MAX,
		    CONFIG_HOOK_SHARE,
		    j6x0lcd_param, sc);
	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS,
		    CONFIG_HOOK_SHARE,
		    j6x0lcd_param, sc);
	config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS,
		    CONFIG_HOOK_SHARE,
		    j6x0lcd_param, sc);

	/* LCD contrast hooks */
	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST_MAX,
		    CONFIG_HOOK_SHARE,
		    j6x0lcd_param, sc);
	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST,
		    CONFIG_HOOK_SHARE,
		    j6x0lcd_param, sc);
	config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST,
		    CONFIG_HOOK_SHARE,
		    j6x0lcd_param, sc);

	/* LCD on/off hook */
	config_hook(CONFIG_HOOK_POWERCONTROL,
		    CONFIG_HOOK_POWERCONTROL_LCD,
		    CONFIG_HOOK_SHARE,
		    j6x0lcd_power, sc);

	/* XXX: TODO: don't rely on CONFIG_HOOK_POWERCONTROL_LCD */
	if (!pmf_device_register(self, NULL, NULL))
		aprint_error_dev(self, "unable to establish power handler\n");
}


/*
 * Get raw contrast value programmed in GPIO port B control register.
 * Used only at attach time to get initial contrast.
 */
static int
j6x0lcd_contrast_raw(uint16_t bcr, int width, const uint8_t *pin)
{
	int contrast;
	int bit;

	contrast = 0;
	for (bit = 0; bit < width; ++bit) {
		unsigned int c;

		c = (bcr >> (pin[bit] << 1)) & 0x3;
		if (c == 1)	/* pin in output mode? */
			contrast |= (1 << bit);
	}

	return contrast;
}


/*
 * Set contrast by programming GPIO port B control register.
 * Data register has been initialized at attach time.
 */
static void
j6x0lcd_contrast_set(struct j6x0lcd_softc *sc, int contrast)
{
	uint16_t bcr;

	sc->sc_contrast = contrast;

	bcr = hd64461_reg_read_2(HD64461_GPBCR_REG16);

	bcr &= sc->sc_contrast_mask;
	bcr |= sc->sc_contrast_control_bits[contrast];

	hd64461_reg_write_2(HD64461_GPBCR_REG16, bcr);
}


static int
j6x0lcd_param(void *ctx, int type, long id, void *msg)
{
	struct j6x0lcd_softc *sc = ctx;
	int value;
	uint8_t dr;

	switch (type) {
	case CONFIG_HOOK_GET:
		switch (id) {
		case CONFIG_HOOK_CONTRAST:
			*(int *)msg = sc->sc_contrast;
			return (0);

		case CONFIG_HOOK_CONTRAST_MAX:
			*(int *)msg = sc->sc_contrast_max;
			return (0);

		case CONFIG_HOOK_BRIGHTNESS:
			*(int *)msg = sc->sc_brightness;
			return (0);

		case CONFIG_HOOK_BRIGHTNESS_MAX:
			*(int *)msg = J6X0LCD_BRIGHTNESS_MAX;
			return (0);
		}
		break;

	case CONFIG_HOOK_SET:
		value = *(int *)msg;
		if (value < 0)
			value = 0;

		switch (id) {
		case CONFIG_HOOK_CONTRAST:
			if (value > sc->sc_contrast_max)
				value = sc->sc_contrast_max;
			j6x0lcd_contrast_set(sc, value);
			return (0);

		case CONFIG_HOOK_BRIGHTNESS:
			if (value > J6X0LCD_BRIGHTNESS_MAX)
				value = J6X0LCD_BRIGHTNESS_MAX;
			sc->sc_brightness = value;

			dr = J6X0LCD_BRIGHTNESS_TO_DA(value);
			DAC_(DR0) = dr;
			return (0);
		}
		break;
	}

	return (EINVAL);
}


static int
j6x0lcd_power(void *ctx, int type, long id, void *msg)
{
	int on;
	uint16_t r;

	if (type != CONFIG_HOOK_POWERCONTROL
	    || id != CONFIG_HOOK_POWERCONTROL_LCD)
		return (EINVAL);

	on = (int)msg;

	r = hd64461_reg_read_2(HD64461_GPBDR_REG16);
	if (on)
		r &= ~HD64461_GPBDR_J6X0_LCD_OFF;
	else
		r |= HD64461_GPBDR_J6X0_LCD_OFF;
	hd64461_reg_write_2(HD64461_GPBDR_REG16, r);

	return (0);
}