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
|
/* $NetBSD: psh3lcd.c,v 1.8 2021/10/24 20:00:11 andvar Exp $ */
/*
* Copyright (c) 2005 KIYOHARA Takashi
* 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.
*
* 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>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <machine/platid.h>
#include <machine/platid_mask.h>
#include <machine/config_hook.h>
#include <sh3/pfcreg.h>
#include <hpcsh/dev/hd64461/hd64461var.h>
#include <hpcsh/dev/hd64461/hd64461reg.h>
#include <hpcsh/dev/hd64461/hd64461gpioreg.h>
/*
* LCD contrast INC#: controlled by pin 0 in HD64461 GPIO port A.
* LCD contrast CS#: controlled by pin 1 in HD64461 GPIO port A.
* LCD contrast U/D#: controlled by pin 0 in SH7709 GPIO port D.
* 0 - down
* 1 - up
*/
#define PSH3LCD_CONTRAST_INC 0x01
#define PSH3LCD_CONTRAST_CS 0x02
#define PSH3LCD_CONTRAST_UD 0x01
/*
* LCD brightness: controlled by HG71C105FE at AREA2.
* XXXX: That is custom IC. We don't know spec. X-<
*/
#define PSH3LCD_BRIGHTNESS_REG0 0xaa000072
#define PSH3LCD_BRIGHTNESS_REG1 0xaa000150
#define PSH3LCD_BRIGHTNESS_REG2 0xaa000152
/* brightness control data */
static const struct psh3lcd_x0_bcd { /* 50PA, 30PA */
uint8_t reg0;
uint8_t reg1;
uint8_t reg2;
} psh3lcd_x0_bcd[] = {
{ 0x05, 0x08, 0x1e },
{ 0x05, 0x1d, 0x09 },
{ 0x04, 0x1e, 0x1e },
{ 0x06, 0x1e, 0x1e },
{ 0x07, 0x1e, 0x1e },
{ 0x00, 0x00, 0x00 }
};
static const struct psh3lcd_xx0_bcd { /* 200JC */
uint8_t reg1;
uint8_t reg2;
} psh3lcd_xx0_bcd[] = {
{ 0x33, 0x0c },
{ 0x2d, 0x12 },
{ 0x26, 0x19 },
{ 0x20, 0x1f },
{ 0x1a, 0x25 },
{ 0x13, 0x2c },
{ 0x0d, 0x32 },
{ 0x07, 0x38 },
{ 0x01, 0x00 },
{ 0x00, 0x00 }
};
#define PSH3LCD_XX0_BRIGHTNESS_MAX 8
#define PSH3LCD_X0_BRIGHTNESS_MAX 4
#define PSH3LCD_CONTRAST_MAX 2 /* XXX */
#define PSH3LCD_CONTRAST 1
#define PSH3LCD_CONTRAST_UP 2
#define PSH3LCD_CONTRAST_DOWN 0
#define BCD_NO_MATCH (-1)
struct psh3lcd_softc {
device_t sc_dev;
int sc_brightness;
int sc_brightness_max;
void (*sc_set_brightness)(int);
};
static int psh3lcd_match(device_t, struct cfdata *, void *);
static void psh3lcd_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(psh3lcd, sizeof(struct psh3lcd_softc),
psh3lcd_match, psh3lcd_attach, NULL, NULL);
static inline int psh3lcd_x0_bcd_get(void);
static inline int psh3lcd_xx0_bcd_get(void);
static void psh3lcd_x0_set_brightness(int);
static void psh3lcd_xx0_set_brightness(int);
static void psh3lcd_set_contrast(int);
static int psh3lcd_param(void *, int, long, void *);
static int psh3lcd_power(void *, int, long, void *);
static inline int
psh3lcd_x0_bcd_get(void)
{
int i;
uint8_t bcr0, bcr1, bcr2;
bcr0 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG0);
bcr1 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG1);
bcr2 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG2);
for (i = 0; psh3lcd_x0_bcd[i].reg0 != 0; i++)
if (bcr0 == psh3lcd_x0_bcd[i].reg0 &&
bcr1 == psh3lcd_x0_bcd[i].reg1 &&
bcr2 == psh3lcd_x0_bcd[i].reg2)
break;
if (psh3lcd_x0_bcd[i].reg0 == 0)
return BCD_NO_MATCH;
return i;
}
static inline int
psh3lcd_xx0_bcd_get(void)
{
int i;
uint8_t bcr1, bcr2;
bcr1 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG1);
bcr2 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG2);
for (i = 0; psh3lcd_xx0_bcd[i].reg1 != 0; i++)
if (bcr1 == psh3lcd_xx0_bcd[i].reg1 &&
bcr2 == psh3lcd_xx0_bcd[i].reg2)
break;
if (psh3lcd_xx0_bcd[i].reg1 == 0)
return BCD_NO_MATCH;
return i;
}
static void
psh3lcd_xx0_set_brightness(int index)
{
_reg_write_1(PSH3LCD_BRIGHTNESS_REG1, psh3lcd_xx0_bcd[index].reg1);
_reg_write_1(PSH3LCD_BRIGHTNESS_REG2, psh3lcd_xx0_bcd[index].reg2);
}
static void
psh3lcd_x0_set_brightness(int index)
{
_reg_write_1(PSH3LCD_BRIGHTNESS_REG0, psh3lcd_x0_bcd[index].reg0);
_reg_write_1(PSH3LCD_BRIGHTNESS_REG1, psh3lcd_x0_bcd[index].reg1);
_reg_write_1(PSH3LCD_BRIGHTNESS_REG2, psh3lcd_x0_bcd[index].reg2);
}
/*
* contrast control function. controlled by IC (X9313W).
*/
inline void
psh3lcd_set_contrast(int value)
{
uint16_t gpadr;
uint8_t pddr;
/* CS assert */
gpadr = hd64461_reg_read_2(HD64461_GPADR_REG16);
gpadr &= ~PSH3LCD_CONTRAST_CS;
hd64461_reg_write_2(HD64461_GPADR_REG16, gpadr);
delay(1);
/* set U/D# */
pddr = _reg_read_1(SH7709_PDDR);
if (value == PSH3LCD_CONTRAST_UP)
pddr |= PSH3LCD_CONTRAST_UD;
else if (value == PSH3LCD_CONTRAST_DOWN)
pddr &= ~PSH3LCD_CONTRAST_UD;
_reg_write_1(SH7709_PDDR, pddr);
delay(3);
/* INCrement */
hd64461_reg_write_2(HD64461_GPADR_REG16, gpadr & ~PSH3LCD_CONTRAST_INC);
delay(1);
hd64461_reg_write_2(HD64461_GPADR_REG16, gpadr);
delay(1);
/* CS deassert */
gpadr |= PSH3LCD_CONTRAST_CS;
hd64461_reg_write_2(HD64461_GPADR_REG16, gpadr);
delay(1);
}
/* ARGSUSED */
static int
psh3lcd_match(device_t parent __unused, struct cfdata *cfp, void *aux __unused)
{
uint8_t bcr0;
if (!platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
return 0;
if (strcmp(cfp->cf_name, "psh3lcd") != 0)
return 0;
bcr0 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG0);
if (bcr0 == 0) {
if (psh3lcd_xx0_bcd_get() == BCD_NO_MATCH)
return 0;
} else {
if (psh3lcd_x0_bcd_get() == BCD_NO_MATCH)
return 0;
}
return 1;
}
/* ARGSUSED */
static void
psh3lcd_attach(device_t parent __unused, device_t self, void *aux __unused)
{
struct psh3lcd_softc *sc = device_private(self);
uint8_t bcr0;
sc->sc_dev = self;
bcr0 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG0);
(void)_reg_read_1(PSH3LCD_BRIGHTNESS_REG1);
(void)_reg_read_1(PSH3LCD_BRIGHTNESS_REG2);
if (bcr0 == 0) {
sc->sc_set_brightness = psh3lcd_xx0_set_brightness;
sc->sc_brightness = psh3lcd_xx0_bcd_get();
sc->sc_brightness_max = PSH3LCD_XX0_BRIGHTNESS_MAX;
} else {
sc->sc_set_brightness = psh3lcd_x0_set_brightness;
sc->sc_brightness = psh3lcd_x0_bcd_get();
sc->sc_brightness_max = PSH3LCD_X0_BRIGHTNESS_MAX;
}
aprint_naive("\n");
aprint_normal(": brightness %d\n", sc->sc_brightness);
/* LCD contrast hooks */
config_hook(CONFIG_HOOK_GET,
CONFIG_HOOK_CONTRAST_MAX, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
config_hook(CONFIG_HOOK_GET,
CONFIG_HOOK_CONTRAST, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
config_hook(CONFIG_HOOK_SET,
CONFIG_HOOK_CONTRAST, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
/* LCD brightness hooks */
config_hook(CONFIG_HOOK_GET,
CONFIG_HOOK_BRIGHTNESS_MAX, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
config_hook(CONFIG_HOOK_GET,
CONFIG_HOOK_BRIGHTNESS, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
config_hook(CONFIG_HOOK_SET,
CONFIG_HOOK_BRIGHTNESS, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
/* LCD on/off hook */
config_hook(CONFIG_HOOK_POWERCONTROL,
CONFIG_HOOK_POWERCONTROL_LCD, CONFIG_HOOK_SHARE, psh3lcd_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");
}
static int
psh3lcd_param(void *ctx, int type, long id, void *msg)
{
struct psh3lcd_softc *sc = ctx;
int value;
switch (type) {
case CONFIG_HOOK_GET:
switch (id) {
case CONFIG_HOOK_CONTRAST:
*(int *)msg = PSH3LCD_CONTRAST;
return 0;
case CONFIG_HOOK_CONTRAST_MAX:
*(int *)msg = PSH3LCD_CONTRAST_MAX;
return 0;
case CONFIG_HOOK_BRIGHTNESS:
*(int *)msg = sc->sc_brightness;
return 0;
case CONFIG_HOOK_BRIGHTNESS_MAX:
*(int *)msg = sc->sc_brightness_max;
return 0;
}
break;
case CONFIG_HOOK_SET:
value = *(int *)msg;
switch (id) {
case CONFIG_HOOK_CONTRAST:
if (value != PSH3LCD_CONTRAST_UP &&
value != PSH3LCD_CONTRAST_DOWN)
return EINVAL;
psh3lcd_set_contrast(value);
return 0;
case CONFIG_HOOK_BRIGHTNESS:
if (value < 0)
value = 0;
if (value > sc->sc_brightness_max)
value = sc->sc_brightness_max;
sc->sc_brightness = value;
sc->sc_set_brightness(sc->sc_brightness);
return 0;
}
break;
}
return EINVAL;
}
static int
psh3lcd_power(void *ctx, int type, long id, void *msg)
{
struct psh3lcd_softc *sc = ctx;
int on;
if (type != CONFIG_HOOK_POWERCONTROL ||
id != CONFIG_HOOK_POWERCONTROL_LCD)
return EINVAL;
on = (int)msg;
if (on)
sc->sc_set_brightness(sc->sc_brightness);
else
sc->sc_set_brightness(sc->sc_brightness_max + 1);
return 0;
}
|