misc: panel: Add lcd_home() helper
[linux-block.git] / drivers / misc / panel.c
CommitLineData
7005b584 1/*
698b1515
WT
2 * Front panel driver for Linux
3 * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
7005b584 4 *
698b1515
WT
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
7005b584 9 *
698b1515
WT
10 * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
11 * connected to a parallel printer port.
7005b584 12 *
698b1515
WT
13 * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit
14 * serial module compatible with Samsung's KS0074. The pins may be connected in
15 * any combination, everything is programmable.
7005b584 16 *
698b1515
WT
17 * The keypad consists in a matrix of push buttons connecting input pins to
18 * data output pins or to the ground. The combinations have to be hard-coded
19 * in the driver, though several profiles exist and adding new ones is easy.
7005b584 20 *
698b1515
WT
21 * Several profiles are provided for commonly found LCD+keypad modules on the
22 * market, such as those found in Nexcom's appliances.
7005b584
WT
23 *
24 * FIXME:
25 * - the initialization/deinitialization process is very dirty and should
26 * be rewritten. It may even be buggy.
27 *
28 * TODO:
29 * - document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
30 * - make the LCD a part of a virtual screen of Vx*Vy
31 * - make the inputs list smp-safe
32 * - change the keyboard to a double mapping : signals -> key_id -> values
33 * so that applications can change values without knowing signals
34 *
35 */
36
493aa896
TY
37#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
7005b584
WT
39#include <linux/module.h>
40
41#include <linux/types.h>
42#include <linux/errno.h>
43#include <linux/signal.h>
44#include <linux/sched.h>
45#include <linux/spinlock.h>
7005b584
WT
46#include <linux/interrupt.h>
47#include <linux/miscdevice.h>
698b1515 48#include <linux/slab.h>
7005b584
WT
49#include <linux/ioport.h>
50#include <linux/fcntl.h>
51#include <linux/init.h>
52#include <linux/delay.h>
d85170ed 53#include <linux/kernel.h>
7005b584
WT
54#include <linux/ctype.h>
55#include <linux/parport.h>
7005b584
WT
56#include <linux/list.h>
57#include <linux/notifier.h>
58#include <linux/reboot.h>
273b281f 59#include <generated/utsrelease.h>
7005b584 60
698b1515 61#include <linux/io.h>
48f658bb 62#include <linux/uaccess.h>
7005b584 63
7005b584
WT
64#define LCD_MINOR 156
65#define KEYPAD_MINOR 185
7005b584 66
7005b584
WT
67#define LCD_MAXBYTES 256 /* max burst write */
68
7005b584 69#define KEYPAD_BUFFER 64
7005b584 70
429ccf05 71/* poll the keyboard this every second */
2b3c9eb2 72#define INPUT_POLL_TIME (HZ / 50)
429ccf05
HH
73/* a key starts to repeat after this times INPUT_POLL_TIME */
74#define KEYPAD_REP_START (10)
75/* a key repeats this times INPUT_POLL_TIME */
76#define KEYPAD_REP_DELAY (2)
77
78/* keep the light on this times INPUT_POLL_TIME for each flash */
79#define FLASH_LIGHT_TEMPO (200)
7005b584
WT
80
81/* converts an r_str() input to an active high, bits string : 000BAOSE */
82#define PNL_PINPUT(a) ((((unsigned char)(a)) ^ 0x7F) >> 3)
83
698b1515
WT
84#define PNL_PBUSY 0x80 /* inverted input, active low */
85#define PNL_PACK 0x40 /* direct input, active low */
86#define PNL_POUTPA 0x20 /* direct input, active high */
87#define PNL_PSELECD 0x10 /* direct input, active high */
88#define PNL_PERRORP 0x08 /* direct input, active low */
7005b584 89
698b1515 90#define PNL_PBIDIR 0x20 /* bi-directional ports */
429ccf05
HH
91/* high to read data in or-ed with data out */
92#define PNL_PINTEN 0x10
698b1515
WT
93#define PNL_PSELECP 0x08 /* inverted output, active low */
94#define PNL_PINITP 0x04 /* direct output, active low */
95#define PNL_PAUTOLF 0x02 /* inverted output, active low */
96#define PNL_PSTROBE 0x01 /* inverted output */
7005b584
WT
97
98#define PNL_PD0 0x01
99#define PNL_PD1 0x02
100#define PNL_PD2 0x04
101#define PNL_PD3 0x08
102#define PNL_PD4 0x10
103#define PNL_PD5 0x20
104#define PNL_PD6 0x40
105#define PNL_PD7 0x80
106
107#define PIN_NONE 0
108#define PIN_STROBE 1
109#define PIN_D0 2
110#define PIN_D1 3
111#define PIN_D2 4
112#define PIN_D3 5
113#define PIN_D4 6
114#define PIN_D5 7
115#define PIN_D6 8
116#define PIN_D7 9
117#define PIN_AUTOLF 14
118#define PIN_INITP 16
119#define PIN_SELECP 17
120#define PIN_NOT_SET 127
121
7005b584
WT
122#define LCD_FLAG_B 0x0004 /* blink on */
123#define LCD_FLAG_C 0x0008 /* cursor on */
124#define LCD_FLAG_D 0x0010 /* display on */
125#define LCD_FLAG_F 0x0020 /* large font mode */
126#define LCD_FLAG_N 0x0040 /* 2-rows mode */
127#define LCD_FLAG_L 0x0080 /* backlight enabled */
128
2114924a
MG
129/* LCD commands */
130#define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
131
132#define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
133#define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
134
135#define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */
136#define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */
137#define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */
138#define LCD_CMD_BLINK_ON 0x01 /* Set blink on */
139
140#define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */
141#define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */
142#define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */
143
144#define LCD_CMD_FUNCTION_SET 0x20 /* Set function */
145#define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */
146#define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */
147#define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */
148
149#define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
150
151#define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
152
429ccf05 153#define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */
7005b584
WT
154#define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
155
36277d4a
MG
156#define NOT_SET -1
157
7005b584
WT
158/* macros to simplify use of the parallel port */
159#define r_ctr(x) (parport_read_control((x)->port))
160#define r_dtr(x) (parport_read_data((x)->port))
161#define r_str(x) (parport_read_status((x)->port))
6ebb56d9
TY
162#define w_ctr(x, y) (parport_write_control((x)->port, (y)))
163#define w_dtr(x, y) (parport_write_data((x)->port, (y)))
7005b584
WT
164
165/* this defines which bits are to be used and which ones to be ignored */
429ccf05
HH
166/* logical or of the output bits involved in the scan matrix */
167static __u8 scan_mask_o;
168/* logical or of the input bits involved in the scan matrix */
169static __u8 scan_mask_i;
7005b584 170
7005b584 171enum input_type {
698b1515
WT
172 INPUT_TYPE_STD,
173 INPUT_TYPE_KBD,
7005b584
WT
174};
175
176enum input_state {
698b1515
WT
177 INPUT_ST_LOW,
178 INPUT_ST_RISING,
179 INPUT_ST_HIGH,
180 INPUT_ST_FALLING,
7005b584
WT
181};
182
183struct logical_input {
698b1515 184 struct list_head list;
35fe0872
KS
185 __u64 mask;
186 __u64 value;
698b1515
WT
187 enum input_type type;
188 enum input_state state;
189 __u8 rise_time, fall_time;
190 __u8 rise_timer, fall_timer, high_timer;
191
192 union {
429ccf05 193 struct { /* valid when type == INPUT_TYPE_STD */
68d386bf
MA
194 void (*press_fct)(int);
195 void (*release_fct)(int);
698b1515
WT
196 int press_data;
197 int release_data;
198 } std;
429ccf05
HH
199 struct { /* valid when type == INPUT_TYPE_KBD */
200 /* strings can be non null-terminated */
698b1515
WT
201 char press_str[sizeof(void *) + sizeof(int)];
202 char repeat_str[sizeof(void *) + sizeof(int)];
203 char release_str[sizeof(void *) + sizeof(int)];
204 } kbd;
205 } u;
7005b584
WT
206};
207
36d2041a 208static LIST_HEAD(logical_inputs); /* list of all defined logical inputs */
7005b584
WT
209
210/* physical contacts history
211 * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
212 * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
213 * corresponds to the ground.
214 * Within each group, bits are stored in the same order as read on the port :
215 * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
35fe0872 216 * So, each __u64 is represented like this :
7005b584
WT
217 * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
218 * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
219 */
429ccf05
HH
220
221/* what has just been read from the I/O ports */
35fe0872 222static __u64 phys_read;
429ccf05 223/* previous phys_read */
35fe0872 224static __u64 phys_read_prev;
429ccf05 225/* stabilized phys_read (phys_read|phys_read_prev) */
35fe0872 226static __u64 phys_curr;
429ccf05 227/* previous phys_curr */
35fe0872 228static __u64 phys_prev;
429ccf05
HH
229/* 0 means that at least one logical signal needs be computed */
230static char inputs_stable;
7005b584 231
7005b584 232/* these variables are specific to the keypad */
a8b2580b
MG
233static struct {
234 bool enabled;
235} keypad;
236
7005b584 237static char keypad_buffer[KEYPAD_BUFFER];
698b1515
WT
238static int keypad_buflen;
239static int keypad_start;
240static char keypressed;
7005b584 241static wait_queue_head_t keypad_read_wait;
7005b584
WT
242
243/* lcd-specific variables */
a8b2580b
MG
244static struct {
245 bool enabled;
6d8b588c
MG
246 bool initialized;
247 bool must_clear;
248
8037e2a3
MG
249 int height;
250 int width;
251 int bwidth;
252 int hwidth;
253 int charset;
254 int proto;
6d8b588c
MG
255 int light_tempo;
256
8037e2a3
MG
257 /* TODO: use union here? */
258 struct {
259 int e;
260 int rs;
261 int rw;
262 int cl;
263 int da;
264 int bl;
265 } pins;
6d8b588c
MG
266
267 /* contains the LCD config state */
268 unsigned long int flags;
269
270 /* Contains the LCD X and Y offset */
271 struct {
272 unsigned long int x;
273 unsigned long int y;
274 } addr;
275
276 /* Current escape sequence and it's length or -1 if outside */
277 struct {
278 char buf[LCD_ESCAPE_LEN + 1];
279 int len;
280 } esc_seq;
a8b2580b 281} lcd;
429ccf05 282
87b8e0c8
MG
283/* Needed only for init */
284static int selected_lcd_type = NOT_SET;
285
7005b584
WT
286/*
287 * Bit masks to convert LCD signals to parallel port outputs.
288 * _d_ are values for data port, _c_ are for control port.
289 * [0] = signal OFF, [1] = signal ON, [2] = mask
290 */
698b1515
WT
291#define BIT_CLR 0
292#define BIT_SET 1
293#define BIT_MSK 2
7005b584
WT
294#define BIT_STATES 3
295/*
296 * one entry for each bit on the LCD
297 */
298#define LCD_BIT_E 0
299#define LCD_BIT_RS 1
300#define LCD_BIT_RW 2
301#define LCD_BIT_BL 3
302#define LCD_BIT_CL 4
303#define LCD_BIT_DA 5
304#define LCD_BITS 6
305
306/*
307 * each bit can be either connected to a DATA or CTRL port
308 */
309#define LCD_PORT_C 0
310#define LCD_PORT_D 1
311#define LCD_PORTS 2
312
313static unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
314
315/*
316 * LCD protocols
317 */
318#define LCD_PROTO_PARALLEL 0
319#define LCD_PROTO_SERIAL 1
77943d31 320#define LCD_PROTO_TI_DA8XX_LCD 2
7005b584
WT
321
322/*
323 * LCD character sets
324 */
325#define LCD_CHARSET_NORMAL 0
326#define LCD_CHARSET_KS0074 1
327
328/*
329 * LCD types
330 */
331#define LCD_TYPE_NONE 0
2c20d92d
SM
332#define LCD_TYPE_CUSTOM 1
333#define LCD_TYPE_OLD 2
334#define LCD_TYPE_KS0074 3
335#define LCD_TYPE_HANTRONIX 4
336#define LCD_TYPE_NEXCOM 5
7005b584
WT
337
338/*
339 * keypad types
340 */
341#define KEYPAD_TYPE_NONE 0
342#define KEYPAD_TYPE_OLD 1
343#define KEYPAD_TYPE_NEW 2
344#define KEYPAD_TYPE_NEXCOM 3
345
346/*
347 * panel profiles
348 */
349#define PANEL_PROFILE_CUSTOM 0
350#define PANEL_PROFILE_OLD 1
351#define PANEL_PROFILE_NEW 2
352#define PANEL_PROFILE_HANTRONIX 3
353#define PANEL_PROFILE_NEXCOM 4
354#define PANEL_PROFILE_LARGE 5
355
356/*
357 * Construct custom config from the kernel's configuration
358 */
7005b584 359#define DEFAULT_PARPORT 0
fe4d7e2c 360#define DEFAULT_PROFILE PANEL_PROFILE_LARGE
98fac3d3
MG
361#define DEFAULT_KEYPAD_TYPE KEYPAD_TYPE_OLD
362#define DEFAULT_LCD_TYPE LCD_TYPE_OLD
fe4d7e2c 363#define DEFAULT_LCD_HEIGHT 2
7005b584
WT
364#define DEFAULT_LCD_WIDTH 40
365#define DEFAULT_LCD_BWIDTH 40
366#define DEFAULT_LCD_HWIDTH 64
fe4d7e2c 367#define DEFAULT_LCD_CHARSET LCD_CHARSET_NORMAL
7005b584
WT
368#define DEFAULT_LCD_PROTO LCD_PROTO_PARALLEL
369
370#define DEFAULT_LCD_PIN_E PIN_AUTOLF
371#define DEFAULT_LCD_PIN_RS PIN_SELECP
372#define DEFAULT_LCD_PIN_RW PIN_INITP
373#define DEFAULT_LCD_PIN_SCL PIN_STROBE
374#define DEFAULT_LCD_PIN_SDA PIN_D0
375#define DEFAULT_LCD_PIN_BL PIN_NOT_SET
7005b584 376
7005b584
WT
377#ifdef CONFIG_PANEL_PARPORT
378#undef DEFAULT_PARPORT
379#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
380#endif
381
1e13e8aa
MG
382#ifdef CONFIG_PANEL_PROFILE
383#undef DEFAULT_PROFILE
384#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
385#endif
386
698b1515 387#if DEFAULT_PROFILE == 0 /* custom */
7005b584 388#ifdef CONFIG_PANEL_KEYPAD
98fac3d3
MG
389#undef DEFAULT_KEYPAD_TYPE
390#define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
7005b584
WT
391#endif
392
7005b584 393#ifdef CONFIG_PANEL_LCD
98fac3d3
MG
394#undef DEFAULT_LCD_TYPE
395#define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
7005b584
WT
396#endif
397
1e13e8aa
MG
398#ifdef CONFIG_PANEL_LCD_HEIGHT
399#undef DEFAULT_LCD_HEIGHT
400#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
401#endif
402
7005b584
WT
403#ifdef CONFIG_PANEL_LCD_WIDTH
404#undef DEFAULT_LCD_WIDTH
405#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
406#endif
407
408#ifdef CONFIG_PANEL_LCD_BWIDTH
409#undef DEFAULT_LCD_BWIDTH
410#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
411#endif
412
413#ifdef CONFIG_PANEL_LCD_HWIDTH
414#undef DEFAULT_LCD_HWIDTH
415#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
416#endif
417
1e13e8aa
MG
418#ifdef CONFIG_PANEL_LCD_CHARSET
419#undef DEFAULT_LCD_CHARSET
420#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
7005b584
WT
421#endif
422
423#ifdef CONFIG_PANEL_LCD_PROTO
424#undef DEFAULT_LCD_PROTO
425#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
426#endif
427
428#ifdef CONFIG_PANEL_LCD_PIN_E
429#undef DEFAULT_LCD_PIN_E
430#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
431#endif
432
433#ifdef CONFIG_PANEL_LCD_PIN_RS
434#undef DEFAULT_LCD_PIN_RS
435#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
436#endif
437
438#ifdef CONFIG_PANEL_LCD_PIN_RW
439#undef DEFAULT_LCD_PIN_RW
440#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
441#endif
442
443#ifdef CONFIG_PANEL_LCD_PIN_SCL
444#undef DEFAULT_LCD_PIN_SCL
445#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
446#endif
447
448#ifdef CONFIG_PANEL_LCD_PIN_SDA
449#undef DEFAULT_LCD_PIN_SDA
450#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
451#endif
452
453#ifdef CONFIG_PANEL_LCD_PIN_BL
454#undef DEFAULT_LCD_PIN_BL
455#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
456#endif
457
7005b584
WT
458#endif /* DEFAULT_PROFILE == 0 */
459
460/* global variables */
f4757af8
MG
461
462/* Device single-open policy control */
463static atomic_t lcd_available = ATOMIC_INIT(1);
464static atomic_t keypad_available = ATOMIC_INIT(1);
465
698b1515 466static struct pardevice *pprt;
7005b584 467
f6d1fcfe 468static int keypad_initialized;
7005b584 469
68d386bf
MA
470static void (*lcd_write_cmd)(int);
471static void (*lcd_write_data)(int);
472static void (*lcd_clear_fast)(void);
7005b584 473
698b1515 474static DEFINE_SPINLOCK(pprt_lock);
7005b584
WT
475static struct timer_list scan_timer;
476
63023177 477MODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
f6d1fcfe 478
59a66a24 479static int parport = DEFAULT_PARPORT;
698b1515
WT
480module_param(parport, int, 0000);
481MODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
f6d1fcfe 482
98e0e762
MG
483static int profile = DEFAULT_PROFILE;
484module_param(profile, int, 0000);
485MODULE_PARM_DESC(profile,
486 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
487 "4=16x2 nexcom; default=40x2, old kp");
488
36277d4a 489static int keypad_type = NOT_SET;
98e0e762
MG
490module_param(keypad_type, int, 0000);
491MODULE_PARM_DESC(keypad_type,
492 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
493
36277d4a 494static int lcd_type = NOT_SET;
98e0e762
MG
495module_param(lcd_type, int, 0000);
496MODULE_PARM_DESC(lcd_type,
2c20d92d 497 "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom");
98e0e762 498
36277d4a 499static int lcd_height = NOT_SET;
698b1515
WT
500module_param(lcd_height, int, 0000);
501MODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
f6d1fcfe 502
36277d4a 503static int lcd_width = NOT_SET;
698b1515
WT
504module_param(lcd_width, int, 0000);
505MODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
f6d1fcfe 506
36277d4a 507static int lcd_bwidth = NOT_SET; /* internal buffer width (usually 40) */
698b1515
WT
508module_param(lcd_bwidth, int, 0000);
509MODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
f6d1fcfe 510
36277d4a 511static int lcd_hwidth = NOT_SET; /* hardware buffer width (usually 64) */
698b1515
WT
512module_param(lcd_hwidth, int, 0000);
513MODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
f6d1fcfe 514
36277d4a 515static int lcd_charset = NOT_SET;
98e0e762
MG
516module_param(lcd_charset, int, 0000);
517MODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
f6d1fcfe 518
36277d4a 519static int lcd_proto = NOT_SET;
698b1515 520module_param(lcd_proto, int, 0000);
429ccf05 521MODULE_PARM_DESC(lcd_proto,
fdf4a494 522 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
f6d1fcfe 523
f6d1fcfe
WT
524/*
525 * These are the parallel port pins the LCD control signals are connected to.
526 * Set this to 0 if the signal is not used. Set it to its opposite value
527 * (negative) if the signal is negated. -MAXINT is used to indicate that the
528 * pin has not been explicitly specified.
529 *
63023177 530 * WARNING! no check will be performed about collisions with keypad !
f6d1fcfe
WT
531 */
532
533static int lcd_e_pin = PIN_NOT_SET;
698b1515
WT
534module_param(lcd_e_pin, int, 0000);
535MODULE_PARM_DESC(lcd_e_pin,
fe5d2e01 536 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
f6d1fcfe
WT
537
538static int lcd_rs_pin = PIN_NOT_SET;
698b1515
WT
539module_param(lcd_rs_pin, int, 0000);
540MODULE_PARM_DESC(lcd_rs_pin,
fe5d2e01 541 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
f6d1fcfe
WT
542
543static int lcd_rw_pin = PIN_NOT_SET;
698b1515
WT
544module_param(lcd_rw_pin, int, 0000);
545MODULE_PARM_DESC(lcd_rw_pin,
fe5d2e01 546 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
f6d1fcfe 547
98e0e762
MG
548static int lcd_cl_pin = PIN_NOT_SET;
549module_param(lcd_cl_pin, int, 0000);
550MODULE_PARM_DESC(lcd_cl_pin,
551 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
f6d1fcfe
WT
552
553static int lcd_da_pin = PIN_NOT_SET;
698b1515
WT
554module_param(lcd_da_pin, int, 0000);
555MODULE_PARM_DESC(lcd_da_pin,
fe5d2e01 556 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
f6d1fcfe 557
98e0e762
MG
558static int lcd_bl_pin = PIN_NOT_SET;
559module_param(lcd_bl_pin, int, 0000);
560MODULE_PARM_DESC(lcd_bl_pin,
561 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
562
563/* Deprecated module parameters - consider not using them anymore */
564
36277d4a 565static int lcd_enabled = NOT_SET;
98e0e762
MG
566module_param(lcd_enabled, int, 0000);
567MODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
568
36277d4a 569static int keypad_enabled = NOT_SET;
98e0e762
MG
570module_param(keypad_enabled, int, 0000);
571MODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
572
36d2041a 573static const unsigned char *lcd_char_conv;
7005b584
WT
574
575/* for some LCD drivers (ks0074) we need a charset conversion table. */
36d2041a 576static const unsigned char lcd_char_conv_ks0074[256] = {
698b1515
WT
577 /* 0|8 1|9 2|A 3|B 4|C 5|D 6|E 7|F */
578 /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
579 /* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
580 /* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
581 /* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
582 /* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
583 /* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
584 /* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
585 /* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
586 /* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
587 /* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
588 /* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
589 /* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
590 /* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
591 /* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
592 /* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
593 /* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
594 /* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
595 /* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
596 /* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
597 /* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
598 /* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
599 /* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
600 /* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
601 /* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
602 /* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
603 /* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
604 /* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
605 /* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
606 /* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
607 /* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
608 /* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
609 /* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
7005b584
WT
610};
611
36d2041a 612static const char old_keypad_profile[][4][9] = {
698b1515
WT
613 {"S0", "Left\n", "Left\n", ""},
614 {"S1", "Down\n", "Down\n", ""},
615 {"S2", "Up\n", "Up\n", ""},
616 {"S3", "Right\n", "Right\n", ""},
617 {"S4", "Esc\n", "Esc\n", ""},
618 {"S5", "Ret\n", "Ret\n", ""},
619 {"", "", "", ""}
7005b584
WT
620};
621
622/* signals, press, repeat, release */
36d2041a 623static const char new_keypad_profile[][4][9] = {
698b1515
WT
624 {"S0", "Left\n", "Left\n", ""},
625 {"S1", "Down\n", "Down\n", ""},
626 {"S2", "Up\n", "Up\n", ""},
627 {"S3", "Right\n", "Right\n", ""},
628 {"S4s5", "", "Esc\n", "Esc\n"},
629 {"s4S5", "", "Ret\n", "Ret\n"},
630 {"S4S5", "Help\n", "", ""},
631 /* add new signals above this line */
632 {"", "", "", ""}
7005b584
WT
633};
634
635/* signals, press, repeat, release */
36d2041a 636static const char nexcom_keypad_profile[][4][9] = {
698b1515
WT
637 {"a-p-e-", "Down\n", "Down\n", ""},
638 {"a-p-E-", "Ret\n", "Ret\n", ""},
639 {"a-P-E-", "Esc\n", "Esc\n", ""},
640 {"a-P-e-", "Up\n", "Up\n", ""},
641 /* add new signals above this line */
642 {"", "", "", ""}
7005b584
WT
643};
644
36d2041a 645static const char (*keypad_profile)[4][9] = old_keypad_profile;
7005b584 646
bea7433b
DC
647static DECLARE_BITMAP(bits, LCD_BITS);
648
649static void lcd_get_bits(unsigned int port, int *val)
650{
651 unsigned int bit, state;
652
653 for (bit = 0; bit < LCD_BITS; bit++) {
654 state = test_bit(bit, bits) ? BIT_SET : BIT_CLR;
655 *val &= lcd_bits[port][bit][BIT_MSK];
656 *val |= lcd_bits[port][bit][state];
657 }
658}
7005b584
WT
659
660static void init_scan_timer(void);
661
662/* sets data port bits according to current signals values */
698b1515
WT
663static int set_data_bits(void)
664{
bea7433b 665 int val;
698b1515
WT
666
667 val = r_dtr(pprt);
bea7433b 668 lcd_get_bits(LCD_PORT_D, &val);
698b1515
WT
669 w_dtr(pprt, val);
670 return val;
7005b584
WT
671}
672
673/* sets ctrl port bits according to current signals values */
698b1515
WT
674static int set_ctrl_bits(void)
675{
bea7433b 676 int val;
698b1515
WT
677
678 val = r_ctr(pprt);
bea7433b 679 lcd_get_bits(LCD_PORT_C, &val);
698b1515
WT
680 w_ctr(pprt, val);
681 return val;
7005b584
WT
682}
683
684/* sets ctrl & data port bits according to current signals values */
6136ac86 685static void panel_set_bits(void)
698b1515
WT
686{
687 set_data_bits();
688 set_ctrl_bits();
7005b584
WT
689}
690
691/*
692 * Converts a parallel port pin (from -25 to 25) to data and control ports
693 * masks, and data and control port bits. The signal will be considered
694 * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
695 *
696 * Result will be used this way :
697 * out(dport, in(dport) & d_val[2] | d_val[signal_state])
698 * out(cport, in(cport) & c_val[2] | c_val[signal_state])
699 */
36d2041a 700static void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
698b1515
WT
701{
702 int d_bit, c_bit, inv;
703
2d53426b
DB
704 d_val[0] = 0;
705 c_val[0] = 0;
706 d_val[1] = 0;
707 c_val[1] = 0;
708 d_val[2] = 0xFF;
709 c_val[2] = 0xFF;
698b1515
WT
710
711 if (pin == 0)
712 return;
713
714 inv = (pin < 0);
715 if (inv)
716 pin = -pin;
717
2d53426b
DB
718 d_bit = 0;
719 c_bit = 0;
698b1515
WT
720
721 switch (pin) {
722 case PIN_STROBE: /* strobe, inverted */
723 c_bit = PNL_PSTROBE;
724 inv = !inv;
725 break;
726 case PIN_D0...PIN_D7: /* D0 - D7 = 2 - 9 */
727 d_bit = 1 << (pin - 2);
728 break;
729 case PIN_AUTOLF: /* autofeed, inverted */
730 c_bit = PNL_PAUTOLF;
731 inv = !inv;
732 break;
429ccf05 733 case PIN_INITP: /* init, direct */
698b1515
WT
734 c_bit = PNL_PINITP;
735 break;
736 case PIN_SELECP: /* select_in, inverted */
737 c_bit = PNL_PSELECP;
738 inv = !inv;
739 break;
740 default: /* unknown pin, ignore */
741 break;
742 }
743
744 if (c_bit) {
745 c_val[2] &= ~c_bit;
746 c_val[!inv] = c_bit;
747 } else if (d_bit) {
748 d_val[2] &= ~d_bit;
749 d_val[!inv] = d_bit;
750 }
7005b584
WT
751}
752
753/* sleeps that many milliseconds with a reschedule */
698b1515
WT
754static void long_sleep(int ms)
755{
895875a3 756 if (in_interrupt())
698b1515 757 mdelay(ms);
895875a3
NMG
758 else
759 schedule_timeout_interruptible(msecs_to_jiffies(ms));
698b1515 760}
7005b584 761
881bf281
AW
762/*
763 * send a serial byte to the LCD panel. The caller is responsible for locking
764 * if needed.
765 */
698b1515
WT
766static void lcd_send_serial(int byte)
767{
768 int bit;
769
881bf281
AW
770 /*
771 * the data bit is set on D0, and the clock on STROBE.
772 * LCD reads D0 on STROBE's rising edge.
773 */
698b1515 774 for (bit = 0; bit < 8; bit++) {
bea7433b 775 clear_bit(LCD_BIT_CL, bits); /* CLK low */
6136ac86 776 panel_set_bits();
bea7433b
DC
777 if (byte & 1) {
778 set_bit(LCD_BIT_DA, bits);
779 } else {
780 clear_bit(LCD_BIT_DA, bits);
781 }
782
6136ac86 783 panel_set_bits();
429ccf05 784 udelay(2); /* maintain the data during 2 us before CLK up */
bea7433b 785 set_bit(LCD_BIT_CL, bits); /* CLK high */
6136ac86 786 panel_set_bits();
429ccf05 787 udelay(1); /* maintain the strobe during 1 us */
698b1515
WT
788 byte >>= 1;
789 }
7005b584
WT
790}
791
792/* turn the backlight on or off */
698b1515
WT
793static void lcd_backlight(int on)
794{
8037e2a3 795 if (lcd.pins.bl == PIN_NONE)
698b1515
WT
796 return;
797
6975e183 798 /* The backlight is activated by setting the AUTOFEED line to +5V */
d4d2dbca 799 spin_lock_irq(&pprt_lock);
bea7433b
DC
800 if (on)
801 set_bit(LCD_BIT_BL, bits);
802 else
803 clear_bit(LCD_BIT_BL, bits);
6136ac86 804 panel_set_bits();
d4d2dbca 805 spin_unlock_irq(&pprt_lock);
7005b584
WT
806}
807
808/* send a command to the LCD panel in serial mode */
698b1515
WT
809static void lcd_write_cmd_s(int cmd)
810{
d4d2dbca 811 spin_lock_irq(&pprt_lock);
698b1515
WT
812 lcd_send_serial(0x1F); /* R/W=W, RS=0 */
813 lcd_send_serial(cmd & 0x0F);
814 lcd_send_serial((cmd >> 4) & 0x0F);
b64a1cbe 815 udelay(40); /* the shortest command takes at least 40 us */
d4d2dbca 816 spin_unlock_irq(&pprt_lock);
7005b584
WT
817}
818
819/* send data to the LCD panel in serial mode */
698b1515
WT
820static void lcd_write_data_s(int data)
821{
d4d2dbca 822 spin_lock_irq(&pprt_lock);
698b1515
WT
823 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
824 lcd_send_serial(data & 0x0F);
825 lcd_send_serial((data >> 4) & 0x0F);
b64a1cbe 826 udelay(40); /* the shortest data takes at least 40 us */
d4d2dbca 827 spin_unlock_irq(&pprt_lock);
7005b584
WT
828}
829
830/* send a command to the LCD panel in 8 bits parallel mode */
698b1515
WT
831static void lcd_write_cmd_p8(int cmd)
832{
d4d2dbca 833 spin_lock_irq(&pprt_lock);
698b1515
WT
834 /* present the data to the data port */
835 w_dtr(pprt, cmd);
b64a1cbe 836 udelay(20); /* maintain the data during 20 us before the strobe */
7005b584 837
bea7433b
DC
838 set_bit(LCD_BIT_E, bits);
839 clear_bit(LCD_BIT_RS, bits);
840 clear_bit(LCD_BIT_RW, bits);
698b1515 841 set_ctrl_bits();
7005b584 842
b64a1cbe 843 udelay(40); /* maintain the strobe during 40 us */
7005b584 844
bea7433b 845 clear_bit(LCD_BIT_E, bits);
698b1515 846 set_ctrl_bits();
7005b584 847
b64a1cbe 848 udelay(120); /* the shortest command takes at least 120 us */
d4d2dbca 849 spin_unlock_irq(&pprt_lock);
7005b584
WT
850}
851
852/* send data to the LCD panel in 8 bits parallel mode */
698b1515
WT
853static void lcd_write_data_p8(int data)
854{
d4d2dbca 855 spin_lock_irq(&pprt_lock);
698b1515
WT
856 /* present the data to the data port */
857 w_dtr(pprt, data);
b64a1cbe 858 udelay(20); /* maintain the data during 20 us before the strobe */
7005b584 859
bea7433b
DC
860 set_bit(LCD_BIT_E, bits);
861 set_bit(LCD_BIT_RS, bits);
862 clear_bit(LCD_BIT_RW, bits);
698b1515 863 set_ctrl_bits();
7005b584 864
b64a1cbe 865 udelay(40); /* maintain the strobe during 40 us */
7005b584 866
bea7433b 867 clear_bit(LCD_BIT_E, bits);
698b1515 868 set_ctrl_bits();
7005b584 869
b64a1cbe 870 udelay(45); /* the shortest data takes at least 45 us */
d4d2dbca 871 spin_unlock_irq(&pprt_lock);
7005b584
WT
872}
873
77943d31
SR
874/* send a command to the TI LCD panel */
875static void lcd_write_cmd_tilcd(int cmd)
876{
d4d2dbca 877 spin_lock_irq(&pprt_lock);
77943d31
SR
878 /* present the data to the control port */
879 w_ctr(pprt, cmd);
b64a1cbe 880 udelay(60);
d4d2dbca 881 spin_unlock_irq(&pprt_lock);
77943d31
SR
882}
883
884/* send data to the TI LCD panel */
885static void lcd_write_data_tilcd(int data)
886{
d4d2dbca 887 spin_lock_irq(&pprt_lock);
77943d31
SR
888 /* present the data to the data port */
889 w_dtr(pprt, data);
b64a1cbe 890 udelay(60);
d4d2dbca 891 spin_unlock_irq(&pprt_lock);
77943d31
SR
892}
893
698b1515
WT
894static void lcd_gotoxy(void)
895{
2114924a 896 lcd_write_cmd(LCD_CMD_SET_DDRAM_ADDR
6d8b588c 897 | (lcd.addr.y ? lcd.hwidth : 0)
8c17893c
NB
898 /*
899 * we force the cursor to stay at the end of the
900 * line if it wants to go farther
901 */
6d8b588c 902 | ((lcd.addr.x < lcd.bwidth) ? lcd.addr.x &
8037e2a3 903 (lcd.hwidth - 1) : lcd.bwidth - 1));
7005b584
WT
904}
905
204a4f6d
GU
906static void lcd_home(void)
907{
908 lcd.addr.x = 0;
909 lcd.addr.y = 0;
910 lcd_gotoxy();
911}
912
698b1515
WT
913static void lcd_print(char c)
914{
6d8b588c 915 if (lcd.addr.x < lcd.bwidth) {
b565b3fb 916 if (lcd_char_conv)
698b1515
WT
917 c = lcd_char_conv[(unsigned char)c];
918 lcd_write_data(c);
6d8b588c 919 lcd.addr.x++;
698b1515
WT
920 }
921 /* prevents the cursor from wrapping onto the next line */
6d8b588c 922 if (lcd.addr.x == lcd.bwidth)
698b1515 923 lcd_gotoxy();
7005b584
WT
924}
925
926/* fills the display with spaces and resets X/Y */
698b1515
WT
927static void lcd_clear_fast_s(void)
928{
929 int pos;
c3ed0afc 930
204a4f6d 931 lcd_home();
698b1515 932
d4d2dbca 933 spin_lock_irq(&pprt_lock);
8037e2a3 934 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
698b1515
WT
935 lcd_send_serial(0x5F); /* R/W=W, RS=1 */
936 lcd_send_serial(' ' & 0x0F);
937 lcd_send_serial((' ' >> 4) & 0x0F);
df44f150 938 /* the shortest data takes at least 40 us */
4cff7adb 939 udelay(40);
698b1515 940 }
d4d2dbca 941 spin_unlock_irq(&pprt_lock);
698b1515 942
204a4f6d 943 lcd_home();
7005b584
WT
944}
945
946/* fills the display with spaces and resets X/Y */
698b1515
WT
947static void lcd_clear_fast_p8(void)
948{
949 int pos;
c3ed0afc 950
204a4f6d 951 lcd_home();
7005b584 952
d4d2dbca 953 spin_lock_irq(&pprt_lock);
8037e2a3 954 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
698b1515
WT
955 /* present the data to the data port */
956 w_dtr(pprt, ' ');
429ccf05
HH
957
958 /* maintain the data during 20 us before the strobe */
b64a1cbe 959 udelay(20);
7005b584 960
bea7433b
DC
961 set_bit(LCD_BIT_E, bits);
962 set_bit(LCD_BIT_RS, bits);
963 clear_bit(LCD_BIT_RW, bits);
698b1515 964 set_ctrl_bits();
7005b584 965
429ccf05 966 /* maintain the strobe during 40 us */
b64a1cbe 967 udelay(40);
7005b584 968
bea7433b 969 clear_bit(LCD_BIT_E, bits);
698b1515 970 set_ctrl_bits();
7005b584 971
429ccf05 972 /* the shortest data takes at least 45 us */
b64a1cbe 973 udelay(45);
698b1515 974 }
d4d2dbca 975 spin_unlock_irq(&pprt_lock);
7005b584 976
204a4f6d 977 lcd_home();
7005b584
WT
978}
979
77943d31
SR
980/* fills the display with spaces and resets X/Y */
981static void lcd_clear_fast_tilcd(void)
982{
983 int pos;
c3ed0afc 984
204a4f6d 985 lcd_home();
77943d31 986
d4d2dbca 987 spin_lock_irq(&pprt_lock);
8037e2a3 988 for (pos = 0; pos < lcd.height * lcd.hwidth; pos++) {
77943d31
SR
989 /* present the data to the data port */
990 w_dtr(pprt, ' ');
b64a1cbe 991 udelay(60);
77943d31
SR
992 }
993
d4d2dbca 994 spin_unlock_irq(&pprt_lock);
77943d31 995
204a4f6d 996 lcd_home();
77943d31
SR
997}
998
7005b584 999/* clears the display and resets X/Y */
698b1515
WT
1000static void lcd_clear_display(void)
1001{
2114924a 1002 lcd_write_cmd(LCD_CMD_DISPLAY_CLEAR);
6d8b588c
MG
1003 lcd.addr.x = 0;
1004 lcd.addr.y = 0;
698b1515
WT
1005 /* we must wait a few milliseconds (15) */
1006 long_sleep(15);
7005b584
WT
1007}
1008
698b1515
WT
1009static void lcd_init_display(void)
1010{
6d8b588c 1011 lcd.flags = ((lcd.height > 1) ? LCD_FLAG_N : 0)
698b1515 1012 | LCD_FLAG_D | LCD_FLAG_C | LCD_FLAG_B;
7005b584 1013
698b1515 1014 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
7005b584 1015
2114924a
MG
1016 /* 8bits, 1 line, small fonts; let's do it 3 times */
1017 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
698b1515 1018 long_sleep(10);
2114924a 1019 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
698b1515 1020 long_sleep(10);
2114924a 1021 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
698b1515 1022 long_sleep(10);
7005b584 1023
2114924a
MG
1024 /* set font height and lines number */
1025 lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS
1026 | ((lcd.flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0)
1027 | ((lcd.flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0)
698b1515
WT
1028 );
1029 long_sleep(10);
7005b584 1030
2114924a
MG
1031 /* display off, cursor off, blink off */
1032 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL);
698b1515 1033 long_sleep(10);
7005b584 1034
2114924a
MG
1035 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL /* set display mode */
1036 | ((lcd.flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0)
1037 | ((lcd.flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0)
1038 | ((lcd.flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0)
698b1515 1039 );
7005b584 1040
6d8b588c 1041 lcd_backlight((lcd.flags & LCD_FLAG_L) ? 1 : 0);
7005b584 1042
698b1515 1043 long_sleep(10);
7005b584 1044
429ccf05 1045 /* entry mode set : increment, cursor shifting */
2114924a 1046 lcd_write_cmd(LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
7005b584 1047
698b1515 1048 lcd_clear_display();
7005b584
WT
1049}
1050
1051/*
1052 * These are the file operation function for user access to /dev/lcd
1053 * This function can also be called from inside the kernel, by
1054 * setting file and ppos to NULL.
1055 *
1056 */
1057
429ccf05
HH
1058static inline int handle_lcd_special_code(void)
1059{
1060 /* LCD special codes */
1061
1062 int processed = 0;
1063
6d8b588c
MG
1064 char *esc = lcd.esc_seq.buf + 2;
1065 int oldflags = lcd.flags;
429ccf05
HH
1066
1067 /* check for display mode flags */
1068 switch (*esc) {
1069 case 'D': /* Display ON */
6d8b588c 1070 lcd.flags |= LCD_FLAG_D;
429ccf05
HH
1071 processed = 1;
1072 break;
1073 case 'd': /* Display OFF */
6d8b588c 1074 lcd.flags &= ~LCD_FLAG_D;
429ccf05
HH
1075 processed = 1;
1076 break;
1077 case 'C': /* Cursor ON */
6d8b588c 1078 lcd.flags |= LCD_FLAG_C;
429ccf05
HH
1079 processed = 1;
1080 break;
1081 case 'c': /* Cursor OFF */
6d8b588c 1082 lcd.flags &= ~LCD_FLAG_C;
429ccf05
HH
1083 processed = 1;
1084 break;
1085 case 'B': /* Blink ON */
6d8b588c 1086 lcd.flags |= LCD_FLAG_B;
429ccf05
HH
1087 processed = 1;
1088 break;
1089 case 'b': /* Blink OFF */
6d8b588c 1090 lcd.flags &= ~LCD_FLAG_B;
429ccf05
HH
1091 processed = 1;
1092 break;
1093 case '+': /* Back light ON */
6d8b588c 1094 lcd.flags |= LCD_FLAG_L;
429ccf05
HH
1095 processed = 1;
1096 break;
1097 case '-': /* Back light OFF */
6d8b588c 1098 lcd.flags &= ~LCD_FLAG_L;
429ccf05
HH
1099 processed = 1;
1100 break;
1101 case '*':
1102 /* flash back light using the keypad timer */
b565b3fb 1103 if (scan_timer.function) {
832bf28c
SS
1104 if (lcd.light_tempo == 0 &&
1105 ((lcd.flags & LCD_FLAG_L) == 0))
429ccf05 1106 lcd_backlight(1);
6d8b588c 1107 lcd.light_tempo = FLASH_LIGHT_TEMPO;
429ccf05
HH
1108 }
1109 processed = 1;
1110 break;
1111 case 'f': /* Small Font */
6d8b588c 1112 lcd.flags &= ~LCD_FLAG_F;
429ccf05
HH
1113 processed = 1;
1114 break;
1115 case 'F': /* Large Font */
6d8b588c 1116 lcd.flags |= LCD_FLAG_F;
429ccf05
HH
1117 processed = 1;
1118 break;
1119 case 'n': /* One Line */
6d8b588c 1120 lcd.flags &= ~LCD_FLAG_N;
429ccf05
HH
1121 processed = 1;
1122 break;
1123 case 'N': /* Two Lines */
6d8b588c 1124 lcd.flags |= LCD_FLAG_N;
429ccf05
HH
1125 break;
1126 case 'l': /* Shift Cursor Left */
6d8b588c 1127 if (lcd.addr.x > 0) {
429ccf05 1128 /* back one char if not at end of line */
6d8b588c 1129 if (lcd.addr.x < lcd.bwidth)
2114924a 1130 lcd_write_cmd(LCD_CMD_SHIFT);
6d8b588c 1131 lcd.addr.x--;
429ccf05
HH
1132 }
1133 processed = 1;
1134 break;
1135 case 'r': /* shift cursor right */
6d8b588c 1136 if (lcd.addr.x < lcd.width) {
429ccf05 1137 /* allow the cursor to pass the end of the line */
2114924a
MG
1138 if (lcd.addr.x < (lcd.bwidth - 1))
1139 lcd_write_cmd(LCD_CMD_SHIFT |
1140 LCD_CMD_SHIFT_RIGHT);
6d8b588c 1141 lcd.addr.x++;
429ccf05
HH
1142 }
1143 processed = 1;
1144 break;
1145 case 'L': /* shift display left */
2114924a 1146 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
429ccf05
HH
1147 processed = 1;
1148 break;
1149 case 'R': /* shift display right */
2114924a
MG
1150 lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
1151 LCD_CMD_SHIFT_RIGHT);
429ccf05
HH
1152 processed = 1;
1153 break;
1154 case 'k': { /* kill end of line */
1155 int x;
c3ed0afc 1156
6d8b588c 1157 for (x = lcd.addr.x; x < lcd.bwidth; x++)
429ccf05
HH
1158 lcd_write_data(' ');
1159
1160 /* restore cursor position */
1161 lcd_gotoxy();
1162 processed = 1;
1163 break;
1164 }
1165 case 'I': /* reinitialize display */
1166 lcd_init_display();
429ccf05
HH
1167 processed = 1;
1168 break;
1169 case 'G': {
1170 /* Generator : LGcxxxxx...xx; must have <c> between '0'
1171 * and '7', representing the numerical ASCII code of the
1172 * redefined character, and <xx...xx> a sequence of 16
1173 * hex digits representing 8 bytes for each character.
1174 * Most LCDs will only use 5 lower bits of the 7 first
1175 * bytes.
1176 */
1177
1178 unsigned char cgbytes[8];
1179 unsigned char cgaddr;
1180 int cgoffset;
1181 int shift;
1182 char value;
1183 int addr;
1184
b565b3fb 1185 if (!strchr(esc, ';'))
429ccf05
HH
1186 break;
1187
1188 esc++;
1189
1190 cgaddr = *(esc++) - '0';
1191 if (cgaddr > 7) {
1192 processed = 1;
1193 break;
1194 }
1195
1196 cgoffset = 0;
1197 shift = 0;
1198 value = 0;
1199 while (*esc && cgoffset < 8) {
1200 shift ^= 4;
3ac76904 1201 if (*esc >= '0' && *esc <= '9') {
429ccf05 1202 value |= (*esc - '0') << shift;
3ac76904 1203 } else if (*esc >= 'A' && *esc <= 'Z') {
429ccf05 1204 value |= (*esc - 'A' + 10) << shift;
3ac76904 1205 } else if (*esc >= 'a' && *esc <= 'z') {
429ccf05 1206 value |= (*esc - 'a' + 10) << shift;
3ac76904 1207 } else {
429ccf05
HH
1208 esc++;
1209 continue;
1210 }
1211
1212 if (shift == 0) {
1213 cgbytes[cgoffset++] = value;
1214 value = 0;
1215 }
1216
1217 esc++;
1218 }
1219
2114924a 1220 lcd_write_cmd(LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
429ccf05
HH
1221 for (addr = 0; addr < cgoffset; addr++)
1222 lcd_write_data(cgbytes[addr]);
1223
1224 /* ensures that we stop writing to CGRAM */
1225 lcd_gotoxy();
1226 processed = 1;
1227 break;
1228 }
1229 case 'x': /* gotoxy : LxXXX[yYYY]; */
1230 case 'y': /* gotoxy : LyYYY[xXXX]; */
b565b3fb 1231 if (!strchr(esc, ';'))
429ccf05
HH
1232 break;
1233
1234 while (*esc) {
1235 if (*esc == 'x') {
1236 esc++;
6d8b588c 1237 if (kstrtoul(esc, 10, &lcd.addr.x) < 0)
12995706 1238 break;
429ccf05
HH
1239 } else if (*esc == 'y') {
1240 esc++;
6d8b588c 1241 if (kstrtoul(esc, 10, &lcd.addr.y) < 0)
12995706 1242 break;
3ac76904 1243 } else {
429ccf05 1244 break;
3ac76904 1245 }
429ccf05
HH
1246 }
1247
1248 lcd_gotoxy();
1249 processed = 1;
1250 break;
1251 }
1252
2114924a 1253 /* TODO: This indent party here got ugly, clean it! */
f2635894 1254 /* Check whether one flag was changed */
6d8b588c 1255 if (oldflags != lcd.flags) {
429ccf05 1256 /* check whether one of B,C,D flags were changed */
6d8b588c 1257 if ((oldflags ^ lcd.flags) &
429ccf05
HH
1258 (LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
1259 /* set display mode */
2114924a
MG
1260 lcd_write_cmd(LCD_CMD_DISPLAY_CTRL
1261 | ((lcd.flags & LCD_FLAG_D)
1262 ? LCD_CMD_DISPLAY_ON : 0)
1263 | ((lcd.flags & LCD_FLAG_C)
1264 ? LCD_CMD_CURSOR_ON : 0)
1265 | ((lcd.flags & LCD_FLAG_B)
1266 ? LCD_CMD_BLINK_ON : 0));
429ccf05 1267 /* check whether one of F,N flags was changed */
6d8b588c 1268 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_F | LCD_FLAG_N))
2114924a
MG
1269 lcd_write_cmd(LCD_CMD_FUNCTION_SET
1270 | LCD_CMD_DATA_LEN_8BITS
1271 | ((lcd.flags & LCD_FLAG_F)
2114924a 1272 ? LCD_CMD_FONT_5X10_DOTS
84a1ed04
GU
1273 : 0)
1274 | ((lcd.flags & LCD_FLAG_N)
1275 ? LCD_CMD_TWO_LINES
2114924a 1276 : 0));
f2635894 1277 /* check whether L flag was changed */
6d8b588c
MG
1278 else if ((oldflags ^ lcd.flags) & (LCD_FLAG_L)) {
1279 if (lcd.flags & (LCD_FLAG_L))
429ccf05 1280 lcd_backlight(1);
6d8b588c 1281 else if (lcd.light_tempo == 0)
8c17893c
NB
1282 /*
1283 * switch off the light only when the tempo
1284 * lighting is gone
1285 */
429ccf05
HH
1286 lcd_backlight(0);
1287 }
1288 }
1289
1290 return processed;
1291}
1292
70a8c3eb
BA
1293static void lcd_write_char(char c)
1294{
1295 /* first, we'll test if we're in escape mode */
6d8b588c 1296 if ((c != '\n') && lcd.esc_seq.len >= 0) {
70a8c3eb 1297 /* yes, let's add this char to the buffer */
6d8b588c
MG
1298 lcd.esc_seq.buf[lcd.esc_seq.len++] = c;
1299 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
70a8c3eb
BA
1300 } else {
1301 /* aborts any previous escape sequence */
6d8b588c 1302 lcd.esc_seq.len = -1;
70a8c3eb
BA
1303
1304 switch (c) {
1305 case LCD_ESCAPE_CHAR:
1306 /* start of an escape sequence */
6d8b588c
MG
1307 lcd.esc_seq.len = 0;
1308 lcd.esc_seq.buf[lcd.esc_seq.len] = 0;
70a8c3eb
BA
1309 break;
1310 case '\b':
1311 /* go back one char and clear it */
6d8b588c 1312 if (lcd.addr.x > 0) {
8c17893c
NB
1313 /*
1314 * check if we're not at the
1315 * end of the line
1316 */
6d8b588c 1317 if (lcd.addr.x < lcd.bwidth)
70a8c3eb 1318 /* back one char */
2114924a 1319 lcd_write_cmd(LCD_CMD_SHIFT);
6d8b588c 1320 lcd.addr.x--;
70a8c3eb
BA
1321 }
1322 /* replace with a space */
1323 lcd_write_data(' ');
1324 /* back one char again */
2114924a 1325 lcd_write_cmd(LCD_CMD_SHIFT);
70a8c3eb
BA
1326 break;
1327 case '\014':
1328 /* quickly clear the display */
1329 lcd_clear_fast();
1330 break;
1331 case '\n':
8c17893c
NB
1332 /*
1333 * flush the remainder of the current line and
1334 * go to the beginning of the next line
1335 */
6d8b588c 1336 for (; lcd.addr.x < lcd.bwidth; lcd.addr.x++)
70a8c3eb 1337 lcd_write_data(' ');
6d8b588c
MG
1338 lcd.addr.x = 0;
1339 lcd.addr.y = (lcd.addr.y + 1) % lcd.height;
70a8c3eb
BA
1340 lcd_gotoxy();
1341 break;
1342 case '\r':
1343 /* go to the beginning of the same line */
6d8b588c 1344 lcd.addr.x = 0;
70a8c3eb
BA
1345 lcd_gotoxy();
1346 break;
1347 case '\t':
1348 /* print a space instead of the tab */
1349 lcd_print(' ');
1350 break;
1351 default:
1352 /* simply print this char */
1353 lcd_print(c);
1354 break;
1355 }
1356 }
1357
8c17893c
NB
1358 /*
1359 * now we'll see if we're in an escape mode and if the current
1360 * escape sequence can be understood.
1361 */
6d8b588c 1362 if (lcd.esc_seq.len >= 2) {
70a8c3eb
BA
1363 int processed = 0;
1364
6d8b588c 1365 if (!strcmp(lcd.esc_seq.buf, "[2J")) {
70a8c3eb
BA
1366 /* clear the display */
1367 lcd_clear_fast();
1368 processed = 1;
6d8b588c 1369 } else if (!strcmp(lcd.esc_seq.buf, "[H")) {
70a8c3eb 1370 /* cursor to home */
204a4f6d 1371 lcd_home();
70a8c3eb
BA
1372 processed = 1;
1373 }
1374 /* codes starting with ^[[L */
6d8b588c
MG
1375 else if ((lcd.esc_seq.len >= 3) &&
1376 (lcd.esc_seq.buf[0] == '[') &&
1377 (lcd.esc_seq.buf[1] == 'L')) {
70a8c3eb
BA
1378 processed = handle_lcd_special_code();
1379 }
1380
1381 /* LCD special escape codes */
8c17893c
NB
1382 /*
1383 * flush the escape sequence if it's been processed
1384 * or if it is getting too long.
1385 */
6d8b588c
MG
1386 if (processed || (lcd.esc_seq.len >= LCD_ESCAPE_LEN))
1387 lcd.esc_seq.len = -1;
70a8c3eb
BA
1388 } /* escape codes */
1389}
1390
698b1515 1391static ssize_t lcd_write(struct file *file,
fdf4a494 1392 const char __user *buf, size_t count, loff_t *ppos)
698b1515 1393{
70a8c3eb 1394 const char __user *tmp = buf;
698b1515
WT
1395 char c;
1396
70a8c3eb 1397 for (; count-- > 0; (*ppos)++, tmp++) {
698b1515 1398 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
8c17893c
NB
1399 /*
1400 * let's be a little nice with other processes
1401 * that need some CPU
1402 */
429ccf05 1403 schedule();
698b1515 1404
6a4193a2 1405 if (get_user(c, tmp))
698b1515
WT
1406 return -EFAULT;
1407
70a8c3eb 1408 lcd_write_char(c);
698b1515 1409 }
7005b584 1410
698b1515 1411 return tmp - buf;
7005b584
WT
1412}
1413
698b1515
WT
1414static int lcd_open(struct inode *inode, struct file *file)
1415{
f4757af8 1416 if (!atomic_dec_and_test(&lcd_available))
698b1515 1417 return -EBUSY; /* open only once at a time */
7005b584 1418
698b1515
WT
1419 if (file->f_mode & FMODE_READ) /* device is write-only */
1420 return -EPERM;
7005b584 1421
6d8b588c 1422 if (lcd.must_clear) {
698b1515 1423 lcd_clear_display();
6d8b588c 1424 lcd.must_clear = false;
698b1515 1425 }
3ff81013 1426 return nonseekable_open(inode, file);
7005b584
WT
1427}
1428
698b1515
WT
1429static int lcd_release(struct inode *inode, struct file *file)
1430{
f4757af8 1431 atomic_inc(&lcd_available);
698b1515 1432 return 0;
7005b584
WT
1433}
1434
429ccf05 1435static const struct file_operations lcd_fops = {
698b1515
WT
1436 .write = lcd_write,
1437 .open = lcd_open,
1438 .release = lcd_release,
3ff81013 1439 .llseek = no_llseek,
7005b584
WT
1440};
1441
1442static struct miscdevice lcd_dev = {
6c3773de
MG
1443 .minor = LCD_MINOR,
1444 .name = "lcd",
1445 .fops = &lcd_fops,
7005b584
WT
1446};
1447
7005b584 1448/* public function usable from the kernel for any purpose */
36d2041a 1449static void panel_lcd_print(const char *s)
698b1515 1450{
70a8c3eb
BA
1451 const char *tmp = s;
1452 int count = strlen(s);
1453
6d8b588c 1454 if (lcd.enabled && lcd.initialized) {
70a8c3eb
BA
1455 for (; count-- > 0; tmp++) {
1456 if (!in_interrupt() && (((count + 1) & 0x1f) == 0))
8c17893c
NB
1457 /*
1458 * let's be a little nice with other processes
1459 * that need some CPU
1460 */
70a8c3eb
BA
1461 schedule();
1462
1463 lcd_write_char(*tmp);
1464 }
1465 }
7005b584
WT
1466}
1467
7005b584 1468/* initialize the LCD driver */
36d2041a 1469static void lcd_init(void)
698b1515 1470{
87b8e0c8 1471 switch (selected_lcd_type) {
429ccf05
HH
1472 case LCD_TYPE_OLD:
1473 /* parallel mode, 8 bits */
8037e2a3
MG
1474 lcd.proto = LCD_PROTO_PARALLEL;
1475 lcd.charset = LCD_CHARSET_NORMAL;
1476 lcd.pins.e = PIN_STROBE;
1477 lcd.pins.rs = PIN_AUTOLF;
1478
1479 lcd.width = 40;
1480 lcd.bwidth = 40;
1481 lcd.hwidth = 64;
1482 lcd.height = 2;
7005b584 1483 break;
429ccf05
HH
1484 case LCD_TYPE_KS0074:
1485 /* serial mode, ks0074 */
8037e2a3
MG
1486 lcd.proto = LCD_PROTO_SERIAL;
1487 lcd.charset = LCD_CHARSET_KS0074;
1488 lcd.pins.bl = PIN_AUTOLF;
1489 lcd.pins.cl = PIN_STROBE;
1490 lcd.pins.da = PIN_D0;
1491
1492 lcd.width = 16;
1493 lcd.bwidth = 40;
1494 lcd.hwidth = 16;
1495 lcd.height = 2;
7005b584 1496 break;
429ccf05
HH
1497 case LCD_TYPE_NEXCOM:
1498 /* parallel mode, 8 bits, generic */
8037e2a3
MG
1499 lcd.proto = LCD_PROTO_PARALLEL;
1500 lcd.charset = LCD_CHARSET_NORMAL;
1501 lcd.pins.e = PIN_AUTOLF;
1502 lcd.pins.rs = PIN_SELECP;
1503 lcd.pins.rw = PIN_INITP;
1504
1505 lcd.width = 16;
1506 lcd.bwidth = 40;
1507 lcd.hwidth = 64;
1508 lcd.height = 2;
7005b584 1509 break;
429ccf05
HH
1510 case LCD_TYPE_CUSTOM:
1511 /* customer-defined */
8037e2a3
MG
1512 lcd.proto = DEFAULT_LCD_PROTO;
1513 lcd.charset = DEFAULT_LCD_CHARSET;
7005b584
WT
1514 /* default geometry will be set later */
1515 break;
429ccf05
HH
1516 case LCD_TYPE_HANTRONIX:
1517 /* parallel mode, 8 bits, hantronix-like */
698b1515 1518 default:
8037e2a3
MG
1519 lcd.proto = LCD_PROTO_PARALLEL;
1520 lcd.charset = LCD_CHARSET_NORMAL;
1521 lcd.pins.e = PIN_STROBE;
1522 lcd.pins.rs = PIN_SELECP;
1523
1524 lcd.width = 16;
1525 lcd.bwidth = 40;
1526 lcd.hwidth = 64;
1527 lcd.height = 2;
7005b584 1528 break;
698b1515 1529 }
7005b584 1530
8037e2a3 1531 /* Overwrite with module params set on loading */
1a4b2e3e 1532 if (lcd_height != NOT_SET)
8037e2a3 1533 lcd.height = lcd_height;
1a4b2e3e 1534 if (lcd_width != NOT_SET)
8037e2a3 1535 lcd.width = lcd_width;
1a4b2e3e 1536 if (lcd_bwidth != NOT_SET)
8037e2a3 1537 lcd.bwidth = lcd_bwidth;
1a4b2e3e 1538 if (lcd_hwidth != NOT_SET)
8037e2a3 1539 lcd.hwidth = lcd_hwidth;
1a4b2e3e 1540 if (lcd_charset != NOT_SET)
8037e2a3 1541 lcd.charset = lcd_charset;
1a4b2e3e 1542 if (lcd_proto != NOT_SET)
8037e2a3
MG
1543 lcd.proto = lcd_proto;
1544 if (lcd_e_pin != PIN_NOT_SET)
1545 lcd.pins.e = lcd_e_pin;
1546 if (lcd_rs_pin != PIN_NOT_SET)
1547 lcd.pins.rs = lcd_rs_pin;
1548 if (lcd_rw_pin != PIN_NOT_SET)
1549 lcd.pins.rw = lcd_rw_pin;
1550 if (lcd_cl_pin != PIN_NOT_SET)
1551 lcd.pins.cl = lcd_cl_pin;
1552 if (lcd_da_pin != PIN_NOT_SET)
1553 lcd.pins.da = lcd_da_pin;
1554 if (lcd_bl_pin != PIN_NOT_SET)
1555 lcd.pins.bl = lcd_bl_pin;
1556
698b1515 1557 /* this is used to catch wrong and default values */
8037e2a3
MG
1558 if (lcd.width <= 0)
1559 lcd.width = DEFAULT_LCD_WIDTH;
1560 if (lcd.bwidth <= 0)
1561 lcd.bwidth = DEFAULT_LCD_BWIDTH;
1562 if (lcd.hwidth <= 0)
1563 lcd.hwidth = DEFAULT_LCD_HWIDTH;
1564 if (lcd.height <= 0)
1565 lcd.height = DEFAULT_LCD_HEIGHT;
1566
1567 if (lcd.proto == LCD_PROTO_SERIAL) { /* SERIAL */
698b1515
WT
1568 lcd_write_cmd = lcd_write_cmd_s;
1569 lcd_write_data = lcd_write_data_s;
1570 lcd_clear_fast = lcd_clear_fast_s;
1571
8037e2a3
MG
1572 if (lcd.pins.cl == PIN_NOT_SET)
1573 lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
1574 if (lcd.pins.da == PIN_NOT_SET)
1575 lcd.pins.da = DEFAULT_LCD_PIN_SDA;
698b1515 1576
8037e2a3 1577 } else if (lcd.proto == LCD_PROTO_PARALLEL) { /* PARALLEL */
698b1515
WT
1578 lcd_write_cmd = lcd_write_cmd_p8;
1579 lcd_write_data = lcd_write_data_p8;
1580 lcd_clear_fast = lcd_clear_fast_p8;
1581
8037e2a3
MG
1582 if (lcd.pins.e == PIN_NOT_SET)
1583 lcd.pins.e = DEFAULT_LCD_PIN_E;
1584 if (lcd.pins.rs == PIN_NOT_SET)
1585 lcd.pins.rs = DEFAULT_LCD_PIN_RS;
1586 if (lcd.pins.rw == PIN_NOT_SET)
1587 lcd.pins.rw = DEFAULT_LCD_PIN_RW;
77943d31
SR
1588 } else {
1589 lcd_write_cmd = lcd_write_cmd_tilcd;
1590 lcd_write_data = lcd_write_data_tilcd;
1591 lcd_clear_fast = lcd_clear_fast_tilcd;
698b1515 1592 }
7005b584 1593
8037e2a3
MG
1594 if (lcd.pins.bl == PIN_NOT_SET)
1595 lcd.pins.bl = DEFAULT_LCD_PIN_BL;
1596
1597 if (lcd.pins.e == PIN_NOT_SET)
1598 lcd.pins.e = PIN_NONE;
1599 if (lcd.pins.rs == PIN_NOT_SET)
1600 lcd.pins.rs = PIN_NONE;
1601 if (lcd.pins.rw == PIN_NOT_SET)
1602 lcd.pins.rw = PIN_NONE;
1603 if (lcd.pins.bl == PIN_NOT_SET)
1604 lcd.pins.bl = PIN_NONE;
1605 if (lcd.pins.cl == PIN_NOT_SET)
1606 lcd.pins.cl = PIN_NONE;
1607 if (lcd.pins.da == PIN_NOT_SET)
1608 lcd.pins.da = PIN_NONE;
1609
1610 if (lcd.charset == NOT_SET)
1611 lcd.charset = DEFAULT_LCD_CHARSET;
1612
1613 if (lcd.charset == LCD_CHARSET_KS0074)
698b1515
WT
1614 lcd_char_conv = lcd_char_conv_ks0074;
1615 else
1616 lcd_char_conv = NULL;
1617
8037e2a3 1618 if (lcd.pins.bl != PIN_NONE)
698b1515
WT
1619 init_scan_timer();
1620
8037e2a3 1621 pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
698b1515 1622 lcd_bits[LCD_PORT_C][LCD_BIT_E]);
8037e2a3 1623 pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
698b1515 1624 lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
8037e2a3 1625 pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
698b1515 1626 lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
8037e2a3 1627 pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
698b1515 1628 lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
8037e2a3 1629 pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
698b1515 1630 lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
8037e2a3 1631 pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
698b1515
WT
1632 lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
1633
8c17893c
NB
1634 /*
1635 * before this line, we must NOT send anything to the display.
698b1515 1636 * Since lcd_init_display() needs to write data, we have to
8c17893c
NB
1637 * enable mark the LCD initialized just before.
1638 */
6d8b588c 1639 lcd.initialized = true;
698b1515 1640 lcd_init_display();
7005b584 1641
698b1515 1642 /* display a short message */
7005b584
WT
1643#ifdef CONFIG_PANEL_CHANGE_MESSAGE
1644#ifdef CONFIG_PANEL_BOOT_MESSAGE
698b1515 1645 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*" CONFIG_PANEL_BOOT_MESSAGE);
7005b584
WT
1646#endif
1647#else
30f468b2 1648 panel_lcd_print("\x1b[Lc\x1b[Lb\x1b[L*Linux-" UTS_RELEASE);
7005b584 1649#endif
429ccf05 1650 /* clear the display on the next device opening */
6d8b588c 1651 lcd.must_clear = true;
204a4f6d 1652 lcd_home();
7005b584
WT
1653}
1654
7005b584
WT
1655/*
1656 * These are the file operation function for user access to /dev/keypad
1657 */
1658
698b1515 1659static ssize_t keypad_read(struct file *file,
cce75f41 1660 char __user *buf, size_t count, loff_t *ppos)
698b1515 1661{
698b1515 1662 unsigned i = *ppos;
cce75f41 1663 char __user *tmp = buf;
7005b584 1664
698b1515
WT
1665 if (keypad_buflen == 0) {
1666 if (file->f_flags & O_NONBLOCK)
1667 return -EAGAIN;
7005b584 1668
310df69c
AB
1669 if (wait_event_interruptible(keypad_read_wait,
1670 keypad_buflen != 0))
698b1515
WT
1671 return -EINTR;
1672 }
7005b584 1673
429ccf05
HH
1674 for (; count-- > 0 && (keypad_buflen > 0);
1675 ++i, ++tmp, --keypad_buflen) {
698b1515
WT
1676 put_user(keypad_buffer[keypad_start], tmp);
1677 keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
1678 }
1679 *ppos = i;
7005b584 1680
698b1515 1681 return tmp - buf;
7005b584
WT
1682}
1683
698b1515
WT
1684static int keypad_open(struct inode *inode, struct file *file)
1685{
f4757af8 1686 if (!atomic_dec_and_test(&keypad_available))
698b1515 1687 return -EBUSY; /* open only once at a time */
7005b584 1688
698b1515
WT
1689 if (file->f_mode & FMODE_WRITE) /* device is read-only */
1690 return -EPERM;
7005b584 1691
698b1515 1692 keypad_buflen = 0; /* flush the buffer on opening */
698b1515 1693 return 0;
7005b584
WT
1694}
1695
698b1515
WT
1696static int keypad_release(struct inode *inode, struct file *file)
1697{
f4757af8 1698 atomic_inc(&keypad_available);
698b1515 1699 return 0;
7005b584
WT
1700}
1701
429ccf05 1702static const struct file_operations keypad_fops = {
698b1515
WT
1703 .read = keypad_read, /* read */
1704 .open = keypad_open, /* open */
1705 .release = keypad_release, /* close */
6038f373 1706 .llseek = default_llseek,
7005b584
WT
1707};
1708
1709static struct miscdevice keypad_dev = {
6c3773de
MG
1710 .minor = KEYPAD_MINOR,
1711 .name = "keypad",
1712 .fops = &keypad_fops,
7005b584
WT
1713};
1714
36d2041a 1715static void keypad_send_key(const char *string, int max_len)
698b1515 1716{
698b1515 1717 /* send the key to the device only if a process is attached to it. */
f4757af8 1718 if (!atomic_read(&keypad_available)) {
698b1515
WT
1719 while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
1720 keypad_buffer[(keypad_start + keypad_buflen++) %
1721 KEYPAD_BUFFER] = *string++;
1722 }
1723 wake_up_interruptible(&keypad_read_wait);
7005b584 1724 }
7005b584
WT
1725}
1726
429ccf05
HH
1727/* this function scans all the bits involving at least one logical signal,
1728 * and puts the results in the bitfield "phys_read" (one bit per established
1729 * contact), and sets "phys_read_prev" to "phys_read".
7005b584 1730 *
429ccf05
HH
1731 * Note: to debounce input signals, we will only consider as switched a signal
1732 * which is stable across 2 measures. Signals which are different between two
1733 * reads will be kept as they previously were in their logical form (phys_prev).
1734 * A signal which has just switched will have a 1 in
1735 * (phys_read ^ phys_read_prev).
7005b584 1736 */
698b1515
WT
1737static void phys_scan_contacts(void)
1738{
1739 int bit, bitval;
1740 char oldval;
1741 char bitmask;
1742 char gndmask;
1743
1744 phys_prev = phys_curr;
1745 phys_read_prev = phys_read;
1746 phys_read = 0; /* flush all signals */
1747
429ccf05
HH
1748 /* keep track of old value, with all outputs disabled */
1749 oldval = r_dtr(pprt) | scan_mask_o;
1750 /* activate all keyboard outputs (active low) */
1751 w_dtr(pprt, oldval & ~scan_mask_o);
1752
1753 /* will have a 1 for each bit set to gnd */
1754 bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1755 /* disable all matrix signals */
1756 w_dtr(pprt, oldval);
698b1515
WT
1757
1758 /* now that all outputs are cleared, the only active input bits are
1759 * directly connected to the ground
7005b584 1760 */
698b1515 1761
429ccf05
HH
1762 /* 1 for each grounded input */
1763 gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
1764
1765 /* grounded inputs are signals 40-44 */
35fe0872 1766 phys_read |= (__u64)gndmask << 40;
7005b584 1767
698b1515 1768 if (bitmask != gndmask) {
8c17893c
NB
1769 /*
1770 * since clearing the outputs changed some inputs, we know
429ccf05
HH
1771 * that some input signals are currently tied to some outputs.
1772 * So we'll scan them.
698b1515
WT
1773 */
1774 for (bit = 0; bit < 8; bit++) {
79f2af62 1775 bitval = BIT(bit);
7005b584 1776
698b1515
WT
1777 if (!(scan_mask_o & bitval)) /* skip unused bits */
1778 continue;
1779
1780 w_dtr(pprt, oldval & ~bitval); /* enable this output */
1781 bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
35fe0872 1782 phys_read |= (__u64)bitmask << (5 * bit);
698b1515
WT
1783 }
1784 w_dtr(pprt, oldval); /* disable all outputs */
7005b584 1785 }
8c17893c
NB
1786 /*
1787 * this is easy: use old bits when they are flapping,
1788 * use new ones when stable
1789 */
429ccf05
HH
1790 phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
1791 (phys_read & ~(phys_read ^ phys_read_prev));
1792}
1793
1794static inline int input_state_high(struct logical_input *input)
1795{
1796#if 0
1797 /* FIXME:
1798 * this is an invalid test. It tries to catch
1799 * transitions from single-key to multiple-key, but
1800 * doesn't take into account the contacts polarity.
1801 * The only solution to the problem is to parse keys
1802 * from the most complex to the simplest combinations,
1803 * and mark them as 'caught' once a combination
1804 * matches, then unmatch it for all other ones.
1805 */
1806
1807 /* try to catch dangerous transitions cases :
1808 * someone adds a bit, so this signal was a false
1809 * positive resulting from a transition. We should
1810 * invalidate the signal immediately and not call the
1811 * release function.
1812 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
1813 */
fdf4a494
DB
1814 if (((phys_prev & input->mask) == input->value) &&
1815 ((phys_curr & input->mask) > input->value)) {
429ccf05
HH
1816 input->state = INPUT_ST_LOW; /* invalidate */
1817 return 1;
1818 }
1819#endif
1820
1821 if ((phys_curr & input->mask) == input->value) {
1822 if ((input->type == INPUT_TYPE_STD) &&
1823 (input->high_timer == 0)) {
1824 input->high_timer++;
b565b3fb 1825 if (input->u.std.press_fct)
429ccf05
HH
1826 input->u.std.press_fct(input->u.std.press_data);
1827 } else if (input->type == INPUT_TYPE_KBD) {
1828 /* will turn on the light */
1829 keypressed = 1;
1830
1831 if (input->high_timer == 0) {
1832 char *press_str = input->u.kbd.press_str;
c3ed0afc 1833
e6626de5
JC
1834 if (press_str[0]) {
1835 int s = sizeof(input->u.kbd.press_str);
c3ed0afc 1836
e6626de5
JC
1837 keypad_send_key(press_str, s);
1838 }
429ccf05
HH
1839 }
1840
1841 if (input->u.kbd.repeat_str[0]) {
1842 char *repeat_str = input->u.kbd.repeat_str;
c3ed0afc 1843
429ccf05 1844 if (input->high_timer >= KEYPAD_REP_START) {
e6626de5 1845 int s = sizeof(input->u.kbd.repeat_str);
c3ed0afc 1846
429ccf05 1847 input->high_timer -= KEYPAD_REP_DELAY;
e6626de5 1848 keypad_send_key(repeat_str, s);
429ccf05
HH
1849 }
1850 /* we will need to come back here soon */
1851 inputs_stable = 0;
1852 }
1853
1854 if (input->high_timer < 255)
1855 input->high_timer++;
1856 }
1857 return 1;
429ccf05 1858 }
083b3638
VH
1859
1860 /* else signal falling down. Let's fall through. */
1861 input->state = INPUT_ST_FALLING;
1862 input->fall_timer = 0;
1863
429ccf05
HH
1864 return 0;
1865}
1866
1867static inline void input_state_falling(struct logical_input *input)
1868{
1869#if 0
1870 /* FIXME !!! same comment as in input_state_high */
fdf4a494
DB
1871 if (((phys_prev & input->mask) == input->value) &&
1872 ((phys_curr & input->mask) > input->value)) {
429ccf05
HH
1873 input->state = INPUT_ST_LOW; /* invalidate */
1874 return;
1875 }
1876#endif
1877
1878 if ((phys_curr & input->mask) == input->value) {
1879 if (input->type == INPUT_TYPE_KBD) {
1880 /* will turn on the light */
1881 keypressed = 1;
1882
1883 if (input->u.kbd.repeat_str[0]) {
1884 char *repeat_str = input->u.kbd.repeat_str;
c3ed0afc 1885
e6626de5
JC
1886 if (input->high_timer >= KEYPAD_REP_START) {
1887 int s = sizeof(input->u.kbd.repeat_str);
c3ed0afc 1888
429ccf05 1889 input->high_timer -= KEYPAD_REP_DELAY;
e6626de5
JC
1890 keypad_send_key(repeat_str, s);
1891 }
429ccf05
HH
1892 /* we will need to come back here soon */
1893 inputs_stable = 0;
1894 }
1895
1896 if (input->high_timer < 255)
1897 input->high_timer++;
1898 }
1899 input->state = INPUT_ST_HIGH;
1900 } else if (input->fall_timer >= input->fall_time) {
1901 /* call release event */
1902 if (input->type == INPUT_TYPE_STD) {
1903 void (*release_fct)(int) = input->u.std.release_fct;
c3ed0afc 1904
b565b3fb 1905 if (release_fct)
429ccf05
HH
1906 release_fct(input->u.std.release_data);
1907 } else if (input->type == INPUT_TYPE_KBD) {
1908 char *release_str = input->u.kbd.release_str;
c3ed0afc 1909
e6626de5
JC
1910 if (release_str[0]) {
1911 int s = sizeof(input->u.kbd.release_str);
c3ed0afc 1912
e6626de5
JC
1913 keypad_send_key(release_str, s);
1914 }
429ccf05
HH
1915 }
1916
1917 input->state = INPUT_ST_LOW;
1918 } else {
1919 input->fall_timer++;
1920 inputs_stable = 0;
1921 }
7005b584
WT
1922}
1923
698b1515
WT
1924static void panel_process_inputs(void)
1925{
1926 struct list_head *item;
1927 struct logical_input *input;
7005b584 1928
698b1515
WT
1929 keypressed = 0;
1930 inputs_stable = 1;
1931 list_for_each(item, &logical_inputs) {
1932 input = list_entry(item, struct logical_input, list);
1933
1934 switch (input->state) {
1935 case INPUT_ST_LOW:
1936 if ((phys_curr & input->mask) != input->value)
1937 break;
429ccf05
HH
1938 /* if all needed ones were already set previously,
1939 * this means that this logical signal has been
1940 * activated by the releasing of another combined
1941 * signal, so we don't want to match.
1942 * eg: AB -(release B)-> A -(release A)-> 0 :
1943 * don't match A.
698b1515
WT
1944 */
1945 if ((phys_prev & input->mask) == input->value)
1946 break;
1947 input->rise_timer = 0;
1948 input->state = INPUT_ST_RISING;
1949 /* no break here, fall through */
1950 case INPUT_ST_RISING:
1951 if ((phys_curr & input->mask) != input->value) {
1952 input->state = INPUT_ST_LOW;
1953 break;
1954 }
1955 if (input->rise_timer < input->rise_time) {
1956 inputs_stable = 0;
1957 input->rise_timer++;
1958 break;
1959 }
1960 input->high_timer = 0;
1961 input->state = INPUT_ST_HIGH;
1962 /* no break here, fall through */
1963 case INPUT_ST_HIGH:
429ccf05 1964 if (input_state_high(input))
698b1515 1965 break;
698b1515
WT
1966 /* no break here, fall through */
1967 case INPUT_ST_FALLING:
429ccf05 1968 input_state_falling(input);
698b1515
WT
1969 }
1970 }
1971}
7005b584 1972
698b1515
WT
1973static void panel_scan_timer(void)
1974{
a8b2580b 1975 if (keypad.enabled && keypad_initialized) {
d4d2dbca 1976 if (spin_trylock_irq(&pprt_lock)) {
698b1515 1977 phys_scan_contacts();
429ccf05
HH
1978
1979 /* no need for the parport anymore */
d4d2dbca 1980 spin_unlock_irq(&pprt_lock);
7005b584
WT
1981 }
1982
698b1515
WT
1983 if (!inputs_stable || phys_curr != phys_prev)
1984 panel_process_inputs();
7005b584 1985 }
7005b584 1986
6d8b588c 1987 if (lcd.enabled && lcd.initialized) {
698b1515 1988 if (keypressed) {
832bf28c
SS
1989 if (lcd.light_tempo == 0 &&
1990 ((lcd.flags & LCD_FLAG_L) == 0))
698b1515 1991 lcd_backlight(1);
6d8b588c
MG
1992 lcd.light_tempo = FLASH_LIGHT_TEMPO;
1993 } else if (lcd.light_tempo > 0) {
1994 lcd.light_tempo--;
832bf28c
SS
1995 if (lcd.light_tempo == 0 &&
1996 ((lcd.flags & LCD_FLAG_L) == 0))
698b1515
WT
1997 lcd_backlight(0);
1998 }
1999 }
2000
2001 mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
7005b584
WT
2002}
2003
698b1515
WT
2004static void init_scan_timer(void)
2005{
b565b3fb 2006 if (scan_timer.function)
698b1515
WT
2007 return; /* already started */
2008
8f6e36c5 2009 setup_timer(&scan_timer, (void *)&panel_scan_timer, 0);
698b1515 2010 scan_timer.expires = jiffies + INPUT_POLL_TIME;
698b1515 2011 add_timer(&scan_timer);
7005b584
WT
2012}
2013
2014/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
429ccf05
HH
2015 * if <omask> or <imask> are non-null, they will be or'ed with the bits
2016 * corresponding to out and in bits respectively.
7005b584
WT
2017 * returns 1 if ok, 0 if error (in which case, nothing is written).
2018 */
35fe0872 2019static u8 input_name2mask(const char *name, __u64 *mask, __u64 *value,
d938e1eb 2020 u8 *imask, u8 *omask)
698b1515 2021{
8aa7307b 2022 const char sigtab[] = "EeSsPpAaBb";
d938e1eb 2023 u8 im, om;
35fe0872 2024 __u64 m, v;
698b1515 2025
d12f27e8
KS
2026 om = 0;
2027 im = 0;
2d53426b
DB
2028 m = 0ULL;
2029 v = 0ULL;
698b1515
WT
2030 while (*name) {
2031 int in, out, bit, neg;
8aa7307b 2032 const char *idx;
c3ed0afc 2033
8aa7307b
KS
2034 idx = strchr(sigtab, *name);
2035 if (!idx)
698b1515 2036 return 0; /* input name not found */
8aa7307b
KS
2037
2038 in = idx - sigtab;
698b1515
WT
2039 neg = (in & 1); /* odd (lower) names are negated */
2040 in >>= 1;
79f2af62 2041 im |= BIT(in);
698b1515
WT
2042
2043 name++;
52ebf93f 2044 if (*name >= '0' && *name <= '7') {
698b1515 2045 out = *name - '0';
79f2af62 2046 om |= BIT(out);
3ac76904 2047 } else if (*name == '-') {
698b1515 2048 out = 8;
3ac76904 2049 } else {
698b1515 2050 return 0; /* unknown bit name */
3ac76904 2051 }
698b1515
WT
2052
2053 bit = (out * 5) + in;
2054
2055 m |= 1ULL << bit;
2056 if (!neg)
2057 v |= 1ULL << bit;
2058 name++;
7005b584 2059 }
698b1515
WT
2060 *mask = m;
2061 *value = v;
2062 if (imask)
2063 *imask |= im;
2064 if (omask)
2065 *omask |= om;
2066 return 1;
7005b584
WT
2067}
2068
2069/* tries to bind a key to the signal name <name>. The key will send the
2070 * strings <press>, <repeat>, <release> for these respective events.
2071 * Returns the pointer to the new key if ok, NULL if the key could not be bound.
2072 */
36d2041a
PH
2073static struct logical_input *panel_bind_key(const char *name, const char *press,
2074 const char *repeat,
2075 const char *release)
698b1515
WT
2076{
2077 struct logical_input *key;
2078
fdf4a494 2079 key = kzalloc(sizeof(*key), GFP_KERNEL);
eb073a9b 2080 if (!key)
698b1515 2081 return NULL;
eb073a9b 2082
698b1515 2083 if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
cb46f472
KV
2084 &scan_mask_o)) {
2085 kfree(key);
698b1515 2086 return NULL;
cb46f472 2087 }
698b1515
WT
2088
2089 key->type = INPUT_TYPE_KBD;
2090 key->state = INPUT_ST_LOW;
2091 key->rise_time = 1;
2092 key->fall_time = 1;
7005b584 2093
698b1515
WT
2094 strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
2095 strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
2096 strncpy(key->u.kbd.release_str, release,
2097 sizeof(key->u.kbd.release_str));
2098 list_add(&key->list, &logical_inputs);
2099 return key;
7005b584
WT
2100}
2101
63023177 2102#if 0
7005b584
WT
2103/* tries to bind a callback function to the signal name <name>. The function
2104 * <press_fct> will be called with the <press_data> arg when the signal is
2105 * activated, and so on for <release_fct>/<release_data>
429ccf05
HH
2106 * Returns the pointer to the new signal if ok, NULL if the signal could not
2107 * be bound.
7005b584
WT
2108 */
2109static struct logical_input *panel_bind_callback(char *name,
68d386bf 2110 void (*press_fct)(int),
698b1515 2111 int press_data,
68d386bf 2112 void (*release_fct)(int),
698b1515
WT
2113 int release_data)
2114{
2115 struct logical_input *callback;
2116
fdf4a494 2117 callback = kmalloc(sizeof(*callback), GFP_KERNEL);
eb073a9b 2118 if (!callback)
698b1515 2119 return NULL;
eb073a9b 2120
698b1515
WT
2121 memset(callback, 0, sizeof(struct logical_input));
2122 if (!input_name2mask(name, &callback->mask, &callback->value,
2123 &scan_mask_i, &scan_mask_o))
2124 return NULL;
2125
2126 callback->type = INPUT_TYPE_STD;
2127 callback->state = INPUT_ST_LOW;
2128 callback->rise_time = 1;
2129 callback->fall_time = 1;
2130 callback->u.std.press_fct = press_fct;
2131 callback->u.std.press_data = press_data;
2132 callback->u.std.release_fct = release_fct;
2133 callback->u.std.release_data = release_data;
2134 list_add(&callback->list, &logical_inputs);
2135 return callback;
7005b584 2136}
63023177 2137#endif
7005b584 2138
698b1515
WT
2139static void keypad_init(void)
2140{
2141 int keynum;
c3ed0afc 2142
698b1515
WT
2143 init_waitqueue_head(&keypad_read_wait);
2144 keypad_buflen = 0; /* flushes any eventual noisy keystroke */
7005b584 2145
698b1515 2146 /* Let's create all known keys */
7005b584 2147
698b1515
WT
2148 for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
2149 panel_bind_key(keypad_profile[keynum][0],
2150 keypad_profile[keynum][1],
2151 keypad_profile[keynum][2],
2152 keypad_profile[keynum][3]);
2153 }
7005b584 2154
698b1515
WT
2155 init_scan_timer();
2156 keypad_initialized = 1;
7005b584
WT
2157}
2158
7005b584
WT
2159/**************************************************/
2160/* device initialization */
2161/**************************************************/
2162
698b1515
WT
2163static int panel_notify_sys(struct notifier_block *this, unsigned long code,
2164 void *unused)
2165{
6d8b588c 2166 if (lcd.enabled && lcd.initialized) {
698b1515
WT
2167 switch (code) {
2168 case SYS_DOWN:
2169 panel_lcd_print
2170 ("\x0cReloading\nSystem...\x1b[Lc\x1b[Lb\x1b[L+");
2171 break;
2172 case SYS_HALT:
2173 panel_lcd_print
2174 ("\x0cSystem Halted.\x1b[Lc\x1b[Lb\x1b[L+");
2175 break;
2176 case SYS_POWER_OFF:
2177 panel_lcd_print("\x0cPower off.\x1b[Lc\x1b[Lb\x1b[L+");
2178 break;
2179 default:
2180 break;
2181 }
7005b584 2182 }
698b1515 2183 return NOTIFY_DONE;
7005b584
WT
2184}
2185
2186static struct notifier_block panel_notifier = {
2187 panel_notify_sys,
2188 NULL,
2189 0
2190};
2191
698b1515 2192static void panel_attach(struct parport *port)
7005b584 2193{
9be83c0a
SM
2194 struct pardev_cb panel_cb;
2195
698b1515
WT
2196 if (port->number != parport)
2197 return;
2198
2199 if (pprt) {
eb073a9b
TY
2200 pr_err("%s: port->number=%d parport=%d, already registered!\n",
2201 __func__, port->number, parport);
698b1515
WT
2202 return;
2203 }
2204
9be83c0a
SM
2205 memset(&panel_cb, 0, sizeof(panel_cb));
2206 panel_cb.private = &pprt;
2207 /* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */
2208
2209 pprt = parport_register_dev_model(port, "panel", &panel_cb, 0);
b565b3fb 2210 if (!pprt) {
eb073a9b
TY
2211 pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
2212 __func__, port->number, parport);
10f3f5b7
KV
2213 return;
2214 }
698b1515
WT
2215
2216 if (parport_claim(pprt)) {
eb073a9b
TY
2217 pr_err("could not claim access to parport%d. Aborting.\n",
2218 parport);
10f3f5b7 2219 goto err_unreg_device;
698b1515
WT
2220 }
2221
429ccf05
HH
2222 /* must init LCD first, just in case an IRQ from the keypad is
2223 * generated at keypad init
2224 */
a8b2580b 2225 if (lcd.enabled) {
698b1515 2226 lcd_init();
10f3f5b7
KV
2227 if (misc_register(&lcd_dev))
2228 goto err_unreg_device;
698b1515
WT
2229 }
2230
a8b2580b 2231 if (keypad.enabled) {
698b1515 2232 keypad_init();
10f3f5b7
KV
2233 if (misc_register(&keypad_dev))
2234 goto err_lcd_unreg;
698b1515 2235 }
bb046fef 2236 register_reboot_notifier(&panel_notifier);
10f3f5b7
KV
2237 return;
2238
2239err_lcd_unreg:
a8b2580b 2240 if (lcd.enabled)
10f3f5b7
KV
2241 misc_deregister(&lcd_dev);
2242err_unreg_device:
2243 parport_unregister_device(pprt);
2244 pprt = NULL;
7005b584
WT
2245}
2246
698b1515 2247static void panel_detach(struct parport *port)
7005b584 2248{
698b1515
WT
2249 if (port->number != parport)
2250 return;
2251
2252 if (!pprt) {
eb073a9b
TY
2253 pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
2254 __func__, port->number, parport);
698b1515
WT
2255 return;
2256 }
b565b3fb 2257 if (scan_timer.function)
7d98c63e 2258 del_timer_sync(&scan_timer);
698b1515 2259
3f77b439
GU
2260 if (keypad.enabled) {
2261 misc_deregister(&keypad_dev);
2262 keypad_initialized = 0;
2263 }
698b1515 2264
3f77b439
GU
2265 if (lcd.enabled) {
2266 panel_lcd_print("\x0cLCD driver unloaded.\x1b[Lc\x1b[Lb\x1b[L-");
2267 misc_deregister(&lcd_dev);
2268 lcd.initialized = false;
0b0595bf 2269 }
3f77b439
GU
2270
2271 /* TODO: free all input signals */
2272 parport_release(pprt);
2273 parport_unregister_device(pprt);
2274 pprt = NULL;
2275 unregister_reboot_notifier(&panel_notifier);
7005b584
WT
2276}
2277
2278static struct parport_driver panel_driver = {
698b1515 2279 .name = "panel",
9be83c0a 2280 .match_port = panel_attach,
698b1515 2281 .detach = panel_detach,
9be83c0a 2282 .devmodel = true,
7005b584
WT
2283};
2284
2285/* init function */
d9114767 2286static int __init panel_init_module(void)
698b1515 2287{
e134201b 2288 int selected_keypad_type = NOT_SET, err;
698b1515 2289
698b1515
WT
2290 /* take care of an eventual profile */
2291 switch (profile) {
429ccf05
HH
2292 case PANEL_PROFILE_CUSTOM:
2293 /* custom profile */
87b8e0c8
MG
2294 selected_keypad_type = DEFAULT_KEYPAD_TYPE;
2295 selected_lcd_type = DEFAULT_LCD_TYPE;
698b1515 2296 break;
429ccf05
HH
2297 case PANEL_PROFILE_OLD:
2298 /* 8 bits, 2*16, old keypad */
87b8e0c8
MG
2299 selected_keypad_type = KEYPAD_TYPE_OLD;
2300 selected_lcd_type = LCD_TYPE_OLD;
2301
2302 /* TODO: This two are a little hacky, sort it out later */
2d35bcf6 2303 if (lcd_width == NOT_SET)
698b1515 2304 lcd_width = 16;
2d35bcf6 2305 if (lcd_hwidth == NOT_SET)
698b1515
WT
2306 lcd_hwidth = 16;
2307 break;
429ccf05
HH
2308 case PANEL_PROFILE_NEW:
2309 /* serial, 2*16, new keypad */
87b8e0c8
MG
2310 selected_keypad_type = KEYPAD_TYPE_NEW;
2311 selected_lcd_type = LCD_TYPE_KS0074;
698b1515 2312 break;
429ccf05
HH
2313 case PANEL_PROFILE_HANTRONIX:
2314 /* 8 bits, 2*16 hantronix-like, no keypad */
87b8e0c8
MG
2315 selected_keypad_type = KEYPAD_TYPE_NONE;
2316 selected_lcd_type = LCD_TYPE_HANTRONIX;
698b1515 2317 break;
429ccf05
HH
2318 case PANEL_PROFILE_NEXCOM:
2319 /* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
87b8e0c8
MG
2320 selected_keypad_type = KEYPAD_TYPE_NEXCOM;
2321 selected_lcd_type = LCD_TYPE_NEXCOM;
698b1515 2322 break;
429ccf05
HH
2323 case PANEL_PROFILE_LARGE:
2324 /* 8 bits, 2*40, old keypad */
87b8e0c8
MG
2325 selected_keypad_type = KEYPAD_TYPE_OLD;
2326 selected_lcd_type = LCD_TYPE_OLD;
698b1515
WT
2327 break;
2328 }
2329
87b8e0c8
MG
2330 /*
2331 * Overwrite selection with module param values (both keypad and lcd),
2332 * where the deprecated params have lower prio.
2333 */
1a4b2e3e 2334 if (keypad_enabled != NOT_SET)
87b8e0c8 2335 selected_keypad_type = keypad_enabled;
1a4b2e3e 2336 if (keypad_type != NOT_SET)
87b8e0c8
MG
2337 selected_keypad_type = keypad_type;
2338
2339 keypad.enabled = (selected_keypad_type > 0);
2340
1a4b2e3e 2341 if (lcd_enabled != NOT_SET)
87b8e0c8 2342 selected_lcd_type = lcd_enabled;
1a4b2e3e 2343 if (lcd_type != NOT_SET)
87b8e0c8
MG
2344 selected_lcd_type = lcd_type;
2345
2346 lcd.enabled = (selected_lcd_type > 0);
698b1515 2347
733345ec
SM
2348 if (lcd.enabled) {
2349 /*
2350 * Init lcd struct with load-time values to preserve exact
2351 * current functionality (at least for now).
2352 */
2353 lcd.height = lcd_height;
2354 lcd.width = lcd_width;
2355 lcd.bwidth = lcd_bwidth;
2356 lcd.hwidth = lcd_hwidth;
2357 lcd.charset = lcd_charset;
2358 lcd.proto = lcd_proto;
2359 lcd.pins.e = lcd_e_pin;
2360 lcd.pins.rs = lcd_rs_pin;
2361 lcd.pins.rw = lcd_rw_pin;
2362 lcd.pins.cl = lcd_cl_pin;
2363 lcd.pins.da = lcd_da_pin;
2364 lcd.pins.bl = lcd_bl_pin;
2365
2366 /* Leave it for now, just in case */
2367 lcd.esc_seq.len = -1;
2368 }
2369
87b8e0c8 2370 switch (selected_keypad_type) {
698b1515
WT
2371 case KEYPAD_TYPE_OLD:
2372 keypad_profile = old_keypad_profile;
2373 break;
2374 case KEYPAD_TYPE_NEW:
2375 keypad_profile = new_keypad_profile;
2376 break;
2377 case KEYPAD_TYPE_NEXCOM:
2378 keypad_profile = nexcom_keypad_profile;
2379 break;
2380 default:
2381 keypad_profile = NULL;
2382 break;
2383 }
2384
a8b2580b 2385 if (!lcd.enabled && !keypad.enabled) {
f43de77c 2386 /* no device enabled, let's exit */
30f468b2 2387 pr_err("panel driver disabled.\n");
698b1515 2388 return -ENODEV;
7005b584 2389 }
7005b584 2390
e134201b
SM
2391 err = parport_register_driver(&panel_driver);
2392 if (err) {
f43de77c 2393 pr_err("could not register with parport. Aborting.\n");
e134201b 2394 return err;
f43de77c
SM
2395 }
2396
698b1515 2397 if (pprt)
30f468b2
GU
2398 pr_info("panel driver registered on parport%d (io=0x%lx).\n",
2399 parport, pprt->port->base);
698b1515 2400 else
30f468b2 2401 pr_info("panel driver not yet registered\n");
698b1515
WT
2402 return 0;
2403}
7005b584 2404
f6d1fcfe 2405static void __exit panel_cleanup_module(void)
698b1515 2406{
698b1515 2407 parport_unregister_driver(&panel_driver);
7005b584 2408}
7005b584 2409
7005b584
WT
2410module_init(panel_init_module);
2411module_exit(panel_cleanup_module);
2412MODULE_AUTHOR("Willy Tarreau");
2413MODULE_LICENSE("GPL");
7005b584
WT
2414
2415/*
2416 * Local variables:
2417 * c-indent-level: 4
2418 * tab-width: 8
2419 * End:
2420 */