18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Front panel driver for Linux
48c2ecf20Sopenharmony_ci * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu>
58c2ecf20Sopenharmony_ci * Copyright (C) 2016-2017 Glider bvba
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This code drives an LCD module (/dev/lcd), and a keypad (/dev/keypad)
88c2ecf20Sopenharmony_ci * connected to a parallel printer port.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * The LCD module may either be an HD44780-like 8-bit parallel LCD, or a 1-bit
118c2ecf20Sopenharmony_ci * serial module compatible with Samsung's KS0074. The pins may be connected in
128c2ecf20Sopenharmony_ci * any combination, everything is programmable.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The keypad consists in a matrix of push buttons connecting input pins to
158c2ecf20Sopenharmony_ci * data output pins or to the ground. The combinations have to be hard-coded
168c2ecf20Sopenharmony_ci * in the driver, though several profiles exist and adding new ones is easy.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * Several profiles are provided for commonly found LCD+keypad modules on the
198c2ecf20Sopenharmony_ci * market, such as those found in Nexcom's appliances.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * FIXME:
228c2ecf20Sopenharmony_ci *      - the initialization/deinitialization process is very dirty and should
238c2ecf20Sopenharmony_ci *        be rewritten. It may even be buggy.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * TODO:
268c2ecf20Sopenharmony_ci *	- document 24 keys keyboard (3 rows of 8 cols, 32 diodes + 2 inputs)
278c2ecf20Sopenharmony_ci *      - make the LCD a part of a virtual screen of Vx*Vy
288c2ecf20Sopenharmony_ci *	- make the inputs list smp-safe
298c2ecf20Sopenharmony_ci *      - change the keyboard to a double mapping : signals -> key_id -> values
308c2ecf20Sopenharmony_ci *        so that applications can change values without knowing signals
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include <linux/module.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include <linux/types.h>
398c2ecf20Sopenharmony_ci#include <linux/errno.h>
408c2ecf20Sopenharmony_ci#include <linux/signal.h>
418c2ecf20Sopenharmony_ci#include <linux/sched.h>
428c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
438c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
448c2ecf20Sopenharmony_ci#include <linux/miscdevice.h>
458c2ecf20Sopenharmony_ci#include <linux/slab.h>
468c2ecf20Sopenharmony_ci#include <linux/ioport.h>
478c2ecf20Sopenharmony_ci#include <linux/fcntl.h>
488c2ecf20Sopenharmony_ci#include <linux/init.h>
498c2ecf20Sopenharmony_ci#include <linux/delay.h>
508c2ecf20Sopenharmony_ci#include <linux/kernel.h>
518c2ecf20Sopenharmony_ci#include <linux/ctype.h>
528c2ecf20Sopenharmony_ci#include <linux/parport.h>
538c2ecf20Sopenharmony_ci#include <linux/list.h>
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#include <linux/io.h>
568c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#include "charlcd.h"
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define LCD_MAXBYTES		256	/* max burst write */
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define KEYPAD_BUFFER		64
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/* poll the keyboard this every second */
658c2ecf20Sopenharmony_ci#define INPUT_POLL_TIME		(HZ / 50)
668c2ecf20Sopenharmony_ci/* a key starts to repeat after this times INPUT_POLL_TIME */
678c2ecf20Sopenharmony_ci#define KEYPAD_REP_START	(10)
688c2ecf20Sopenharmony_ci/* a key repeats this times INPUT_POLL_TIME */
698c2ecf20Sopenharmony_ci#define KEYPAD_REP_DELAY	(2)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* converts an r_str() input to an active high, bits string : 000BAOSE */
728c2ecf20Sopenharmony_ci#define PNL_PINPUT(a)		((((unsigned char)(a)) ^ 0x7F) >> 3)
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define PNL_PBUSY		0x80	/* inverted input, active low */
758c2ecf20Sopenharmony_ci#define PNL_PACK		0x40	/* direct input, active low */
768c2ecf20Sopenharmony_ci#define PNL_POUTPA		0x20	/* direct input, active high */
778c2ecf20Sopenharmony_ci#define PNL_PSELECD		0x10	/* direct input, active high */
788c2ecf20Sopenharmony_ci#define PNL_PERRORP		0x08	/* direct input, active low */
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define PNL_PBIDIR		0x20	/* bi-directional ports */
818c2ecf20Sopenharmony_ci/* high to read data in or-ed with data out */
828c2ecf20Sopenharmony_ci#define PNL_PINTEN		0x10
838c2ecf20Sopenharmony_ci#define PNL_PSELECP		0x08	/* inverted output, active low */
848c2ecf20Sopenharmony_ci#define PNL_PINITP		0x04	/* direct output, active low */
858c2ecf20Sopenharmony_ci#define PNL_PAUTOLF		0x02	/* inverted output, active low */
868c2ecf20Sopenharmony_ci#define PNL_PSTROBE		0x01	/* inverted output */
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define PNL_PD0			0x01
898c2ecf20Sopenharmony_ci#define PNL_PD1			0x02
908c2ecf20Sopenharmony_ci#define PNL_PD2			0x04
918c2ecf20Sopenharmony_ci#define PNL_PD3			0x08
928c2ecf20Sopenharmony_ci#define PNL_PD4			0x10
938c2ecf20Sopenharmony_ci#define PNL_PD5			0x20
948c2ecf20Sopenharmony_ci#define PNL_PD6			0x40
958c2ecf20Sopenharmony_ci#define PNL_PD7			0x80
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci#define PIN_NONE		0
988c2ecf20Sopenharmony_ci#define PIN_STROBE		1
998c2ecf20Sopenharmony_ci#define PIN_D0			2
1008c2ecf20Sopenharmony_ci#define PIN_D1			3
1018c2ecf20Sopenharmony_ci#define PIN_D2			4
1028c2ecf20Sopenharmony_ci#define PIN_D3			5
1038c2ecf20Sopenharmony_ci#define PIN_D4			6
1048c2ecf20Sopenharmony_ci#define PIN_D5			7
1058c2ecf20Sopenharmony_ci#define PIN_D6			8
1068c2ecf20Sopenharmony_ci#define PIN_D7			9
1078c2ecf20Sopenharmony_ci#define PIN_AUTOLF		14
1088c2ecf20Sopenharmony_ci#define PIN_INITP		16
1098c2ecf20Sopenharmony_ci#define PIN_SELECP		17
1108c2ecf20Sopenharmony_ci#define PIN_NOT_SET		127
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define NOT_SET			-1
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/* macros to simplify use of the parallel port */
1158c2ecf20Sopenharmony_ci#define r_ctr(x)        (parport_read_control((x)->port))
1168c2ecf20Sopenharmony_ci#define r_dtr(x)        (parport_read_data((x)->port))
1178c2ecf20Sopenharmony_ci#define r_str(x)        (parport_read_status((x)->port))
1188c2ecf20Sopenharmony_ci#define w_ctr(x, y)     (parport_write_control((x)->port, (y)))
1198c2ecf20Sopenharmony_ci#define w_dtr(x, y)     (parport_write_data((x)->port, (y)))
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/* this defines which bits are to be used and which ones to be ignored */
1228c2ecf20Sopenharmony_ci/* logical or of the output bits involved in the scan matrix */
1238c2ecf20Sopenharmony_cistatic __u8 scan_mask_o;
1248c2ecf20Sopenharmony_ci/* logical or of the input bits involved in the scan matrix */
1258c2ecf20Sopenharmony_cistatic __u8 scan_mask_i;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cienum input_type {
1288c2ecf20Sopenharmony_ci	INPUT_TYPE_STD,
1298c2ecf20Sopenharmony_ci	INPUT_TYPE_KBD,
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cienum input_state {
1338c2ecf20Sopenharmony_ci	INPUT_ST_LOW,
1348c2ecf20Sopenharmony_ci	INPUT_ST_RISING,
1358c2ecf20Sopenharmony_ci	INPUT_ST_HIGH,
1368c2ecf20Sopenharmony_ci	INPUT_ST_FALLING,
1378c2ecf20Sopenharmony_ci};
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistruct logical_input {
1408c2ecf20Sopenharmony_ci	struct list_head list;
1418c2ecf20Sopenharmony_ci	__u64 mask;
1428c2ecf20Sopenharmony_ci	__u64 value;
1438c2ecf20Sopenharmony_ci	enum input_type type;
1448c2ecf20Sopenharmony_ci	enum input_state state;
1458c2ecf20Sopenharmony_ci	__u8 rise_time, fall_time;
1468c2ecf20Sopenharmony_ci	__u8 rise_timer, fall_timer, high_timer;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	union {
1498c2ecf20Sopenharmony_ci		struct {	/* valid when type == INPUT_TYPE_STD */
1508c2ecf20Sopenharmony_ci			void (*press_fct)(int);
1518c2ecf20Sopenharmony_ci			void (*release_fct)(int);
1528c2ecf20Sopenharmony_ci			int press_data;
1538c2ecf20Sopenharmony_ci			int release_data;
1548c2ecf20Sopenharmony_ci		} std;
1558c2ecf20Sopenharmony_ci		struct {	/* valid when type == INPUT_TYPE_KBD */
1568c2ecf20Sopenharmony_ci			char press_str[sizeof(void *) + sizeof(int)] __nonstring;
1578c2ecf20Sopenharmony_ci			char repeat_str[sizeof(void *) + sizeof(int)] __nonstring;
1588c2ecf20Sopenharmony_ci			char release_str[sizeof(void *) + sizeof(int)] __nonstring;
1598c2ecf20Sopenharmony_ci		} kbd;
1608c2ecf20Sopenharmony_ci	} u;
1618c2ecf20Sopenharmony_ci};
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic LIST_HEAD(logical_inputs);	/* list of all defined logical inputs */
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/* physical contacts history
1668c2ecf20Sopenharmony_ci * Physical contacts are a 45 bits string of 9 groups of 5 bits each.
1678c2ecf20Sopenharmony_ci * The 8 lower groups correspond to output bits 0 to 7, and the 9th group
1688c2ecf20Sopenharmony_ci * corresponds to the ground.
1698c2ecf20Sopenharmony_ci * Within each group, bits are stored in the same order as read on the port :
1708c2ecf20Sopenharmony_ci * BAPSE (busy=4, ack=3, paper empty=2, select=1, error=0).
1718c2ecf20Sopenharmony_ci * So, each __u64 is represented like this :
1728c2ecf20Sopenharmony_ci * 0000000000000000000BAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSEBAPSE
1738c2ecf20Sopenharmony_ci * <-----unused------><gnd><d07><d06><d05><d04><d03><d02><d01><d00>
1748c2ecf20Sopenharmony_ci */
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci/* what has just been read from the I/O ports */
1778c2ecf20Sopenharmony_cistatic __u64 phys_read;
1788c2ecf20Sopenharmony_ci/* previous phys_read */
1798c2ecf20Sopenharmony_cistatic __u64 phys_read_prev;
1808c2ecf20Sopenharmony_ci/* stabilized phys_read (phys_read|phys_read_prev) */
1818c2ecf20Sopenharmony_cistatic __u64 phys_curr;
1828c2ecf20Sopenharmony_ci/* previous phys_curr */
1838c2ecf20Sopenharmony_cistatic __u64 phys_prev;
1848c2ecf20Sopenharmony_ci/* 0 means that at least one logical signal needs be computed */
1858c2ecf20Sopenharmony_cistatic char inputs_stable;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/* these variables are specific to the keypad */
1888c2ecf20Sopenharmony_cistatic struct {
1898c2ecf20Sopenharmony_ci	bool enabled;
1908c2ecf20Sopenharmony_ci} keypad;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic char keypad_buffer[KEYPAD_BUFFER];
1938c2ecf20Sopenharmony_cistatic int keypad_buflen;
1948c2ecf20Sopenharmony_cistatic int keypad_start;
1958c2ecf20Sopenharmony_cistatic char keypressed;
1968c2ecf20Sopenharmony_cistatic wait_queue_head_t keypad_read_wait;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci/* lcd-specific variables */
1998c2ecf20Sopenharmony_cistatic struct {
2008c2ecf20Sopenharmony_ci	bool enabled;
2018c2ecf20Sopenharmony_ci	bool initialized;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	int charset;
2048c2ecf20Sopenharmony_ci	int proto;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	/* TODO: use union here? */
2078c2ecf20Sopenharmony_ci	struct {
2088c2ecf20Sopenharmony_ci		int e;
2098c2ecf20Sopenharmony_ci		int rs;
2108c2ecf20Sopenharmony_ci		int rw;
2118c2ecf20Sopenharmony_ci		int cl;
2128c2ecf20Sopenharmony_ci		int da;
2138c2ecf20Sopenharmony_ci		int bl;
2148c2ecf20Sopenharmony_ci	} pins;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	struct charlcd *charlcd;
2178c2ecf20Sopenharmony_ci} lcd;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci/* Needed only for init */
2208c2ecf20Sopenharmony_cistatic int selected_lcd_type = NOT_SET;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci/*
2238c2ecf20Sopenharmony_ci * Bit masks to convert LCD signals to parallel port outputs.
2248c2ecf20Sopenharmony_ci * _d_ are values for data port, _c_ are for control port.
2258c2ecf20Sopenharmony_ci * [0] = signal OFF, [1] = signal ON, [2] = mask
2268c2ecf20Sopenharmony_ci */
2278c2ecf20Sopenharmony_ci#define BIT_CLR		0
2288c2ecf20Sopenharmony_ci#define BIT_SET		1
2298c2ecf20Sopenharmony_ci#define BIT_MSK		2
2308c2ecf20Sopenharmony_ci#define BIT_STATES	3
2318c2ecf20Sopenharmony_ci/*
2328c2ecf20Sopenharmony_ci * one entry for each bit on the LCD
2338c2ecf20Sopenharmony_ci */
2348c2ecf20Sopenharmony_ci#define LCD_BIT_E	0
2358c2ecf20Sopenharmony_ci#define LCD_BIT_RS	1
2368c2ecf20Sopenharmony_ci#define LCD_BIT_RW	2
2378c2ecf20Sopenharmony_ci#define LCD_BIT_BL	3
2388c2ecf20Sopenharmony_ci#define LCD_BIT_CL	4
2398c2ecf20Sopenharmony_ci#define LCD_BIT_DA	5
2408c2ecf20Sopenharmony_ci#define LCD_BITS	6
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci/*
2438c2ecf20Sopenharmony_ci * each bit can be either connected to a DATA or CTRL port
2448c2ecf20Sopenharmony_ci */
2458c2ecf20Sopenharmony_ci#define LCD_PORT_C	0
2468c2ecf20Sopenharmony_ci#define LCD_PORT_D	1
2478c2ecf20Sopenharmony_ci#define LCD_PORTS	2
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic unsigned char lcd_bits[LCD_PORTS][LCD_BITS][BIT_STATES];
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci/*
2528c2ecf20Sopenharmony_ci * LCD protocols
2538c2ecf20Sopenharmony_ci */
2548c2ecf20Sopenharmony_ci#define LCD_PROTO_PARALLEL      0
2558c2ecf20Sopenharmony_ci#define LCD_PROTO_SERIAL        1
2568c2ecf20Sopenharmony_ci#define LCD_PROTO_TI_DA8XX_LCD	2
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci/*
2598c2ecf20Sopenharmony_ci * LCD character sets
2608c2ecf20Sopenharmony_ci */
2618c2ecf20Sopenharmony_ci#define LCD_CHARSET_NORMAL      0
2628c2ecf20Sopenharmony_ci#define LCD_CHARSET_KS0074      1
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci/*
2658c2ecf20Sopenharmony_ci * LCD types
2668c2ecf20Sopenharmony_ci */
2678c2ecf20Sopenharmony_ci#define LCD_TYPE_NONE		0
2688c2ecf20Sopenharmony_ci#define LCD_TYPE_CUSTOM		1
2698c2ecf20Sopenharmony_ci#define LCD_TYPE_OLD		2
2708c2ecf20Sopenharmony_ci#define LCD_TYPE_KS0074		3
2718c2ecf20Sopenharmony_ci#define LCD_TYPE_HANTRONIX	4
2728c2ecf20Sopenharmony_ci#define LCD_TYPE_NEXCOM		5
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci/*
2758c2ecf20Sopenharmony_ci * keypad types
2768c2ecf20Sopenharmony_ci */
2778c2ecf20Sopenharmony_ci#define KEYPAD_TYPE_NONE	0
2788c2ecf20Sopenharmony_ci#define KEYPAD_TYPE_OLD		1
2798c2ecf20Sopenharmony_ci#define KEYPAD_TYPE_NEW		2
2808c2ecf20Sopenharmony_ci#define KEYPAD_TYPE_NEXCOM	3
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci/*
2838c2ecf20Sopenharmony_ci * panel profiles
2848c2ecf20Sopenharmony_ci */
2858c2ecf20Sopenharmony_ci#define PANEL_PROFILE_CUSTOM	0
2868c2ecf20Sopenharmony_ci#define PANEL_PROFILE_OLD	1
2878c2ecf20Sopenharmony_ci#define PANEL_PROFILE_NEW	2
2888c2ecf20Sopenharmony_ci#define PANEL_PROFILE_HANTRONIX	3
2898c2ecf20Sopenharmony_ci#define PANEL_PROFILE_NEXCOM	4
2908c2ecf20Sopenharmony_ci#define PANEL_PROFILE_LARGE	5
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci/*
2938c2ecf20Sopenharmony_ci * Construct custom config from the kernel's configuration
2948c2ecf20Sopenharmony_ci */
2958c2ecf20Sopenharmony_ci#define DEFAULT_PARPORT         0
2968c2ecf20Sopenharmony_ci#define DEFAULT_PROFILE         PANEL_PROFILE_LARGE
2978c2ecf20Sopenharmony_ci#define DEFAULT_KEYPAD_TYPE     KEYPAD_TYPE_OLD
2988c2ecf20Sopenharmony_ci#define DEFAULT_LCD_TYPE        LCD_TYPE_OLD
2998c2ecf20Sopenharmony_ci#define DEFAULT_LCD_HEIGHT      2
3008c2ecf20Sopenharmony_ci#define DEFAULT_LCD_WIDTH       40
3018c2ecf20Sopenharmony_ci#define DEFAULT_LCD_BWIDTH      40
3028c2ecf20Sopenharmony_ci#define DEFAULT_LCD_HWIDTH      64
3038c2ecf20Sopenharmony_ci#define DEFAULT_LCD_CHARSET     LCD_CHARSET_NORMAL
3048c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PROTO       LCD_PROTO_PARALLEL
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_E       PIN_AUTOLF
3078c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_RS      PIN_SELECP
3088c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_RW      PIN_INITP
3098c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_SCL     PIN_STROBE
3108c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_SDA     PIN_D0
3118c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_BL      PIN_NOT_SET
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_PARPORT
3148c2ecf20Sopenharmony_ci#undef DEFAULT_PARPORT
3158c2ecf20Sopenharmony_ci#define DEFAULT_PARPORT CONFIG_PANEL_PARPORT
3168c2ecf20Sopenharmony_ci#endif
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_PROFILE
3198c2ecf20Sopenharmony_ci#undef DEFAULT_PROFILE
3208c2ecf20Sopenharmony_ci#define DEFAULT_PROFILE CONFIG_PANEL_PROFILE
3218c2ecf20Sopenharmony_ci#endif
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci#if DEFAULT_PROFILE == 0	/* custom */
3248c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_KEYPAD
3258c2ecf20Sopenharmony_ci#undef DEFAULT_KEYPAD_TYPE
3268c2ecf20Sopenharmony_ci#define DEFAULT_KEYPAD_TYPE CONFIG_PANEL_KEYPAD
3278c2ecf20Sopenharmony_ci#endif
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD
3308c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_TYPE
3318c2ecf20Sopenharmony_ci#define DEFAULT_LCD_TYPE CONFIG_PANEL_LCD
3328c2ecf20Sopenharmony_ci#endif
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_HEIGHT
3358c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_HEIGHT
3368c2ecf20Sopenharmony_ci#define DEFAULT_LCD_HEIGHT CONFIG_PANEL_LCD_HEIGHT
3378c2ecf20Sopenharmony_ci#endif
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_WIDTH
3408c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_WIDTH
3418c2ecf20Sopenharmony_ci#define DEFAULT_LCD_WIDTH CONFIG_PANEL_LCD_WIDTH
3428c2ecf20Sopenharmony_ci#endif
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_BWIDTH
3458c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_BWIDTH
3468c2ecf20Sopenharmony_ci#define DEFAULT_LCD_BWIDTH CONFIG_PANEL_LCD_BWIDTH
3478c2ecf20Sopenharmony_ci#endif
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_HWIDTH
3508c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_HWIDTH
3518c2ecf20Sopenharmony_ci#define DEFAULT_LCD_HWIDTH CONFIG_PANEL_LCD_HWIDTH
3528c2ecf20Sopenharmony_ci#endif
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_CHARSET
3558c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_CHARSET
3568c2ecf20Sopenharmony_ci#define DEFAULT_LCD_CHARSET CONFIG_PANEL_LCD_CHARSET
3578c2ecf20Sopenharmony_ci#endif
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_PROTO
3608c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_PROTO
3618c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PROTO CONFIG_PANEL_LCD_PROTO
3628c2ecf20Sopenharmony_ci#endif
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_PIN_E
3658c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_PIN_E
3668c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_E CONFIG_PANEL_LCD_PIN_E
3678c2ecf20Sopenharmony_ci#endif
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_PIN_RS
3708c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_PIN_RS
3718c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_RS CONFIG_PANEL_LCD_PIN_RS
3728c2ecf20Sopenharmony_ci#endif
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_PIN_RW
3758c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_PIN_RW
3768c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_RW CONFIG_PANEL_LCD_PIN_RW
3778c2ecf20Sopenharmony_ci#endif
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_PIN_SCL
3808c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_PIN_SCL
3818c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_SCL CONFIG_PANEL_LCD_PIN_SCL
3828c2ecf20Sopenharmony_ci#endif
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_PIN_SDA
3858c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_PIN_SDA
3868c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_SDA CONFIG_PANEL_LCD_PIN_SDA
3878c2ecf20Sopenharmony_ci#endif
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci#ifdef CONFIG_PANEL_LCD_PIN_BL
3908c2ecf20Sopenharmony_ci#undef DEFAULT_LCD_PIN_BL
3918c2ecf20Sopenharmony_ci#define DEFAULT_LCD_PIN_BL CONFIG_PANEL_LCD_PIN_BL
3928c2ecf20Sopenharmony_ci#endif
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci#endif /* DEFAULT_PROFILE == 0 */
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci/* global variables */
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci/* Device single-open policy control */
3998c2ecf20Sopenharmony_cistatic atomic_t keypad_available = ATOMIC_INIT(1);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic struct pardevice *pprt;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic int keypad_initialized;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(pprt_lock);
4068c2ecf20Sopenharmony_cistatic struct timer_list scan_timer;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Generic parallel port LCD/Keypad driver");
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cistatic int parport = DEFAULT_PARPORT;
4118c2ecf20Sopenharmony_cimodule_param(parport, int, 0000);
4128c2ecf20Sopenharmony_ciMODULE_PARM_DESC(parport, "Parallel port index (0=lpt1, 1=lpt2, ...)");
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic int profile = DEFAULT_PROFILE;
4158c2ecf20Sopenharmony_cimodule_param(profile, int, 0000);
4168c2ecf20Sopenharmony_ciMODULE_PARM_DESC(profile,
4178c2ecf20Sopenharmony_ci		 "1=16x2 old kp; 2=serial 16x2, new kp; 3=16x2 hantronix; "
4188c2ecf20Sopenharmony_ci		 "4=16x2 nexcom; default=40x2, old kp");
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_cistatic int keypad_type = NOT_SET;
4218c2ecf20Sopenharmony_cimodule_param(keypad_type, int, 0000);
4228c2ecf20Sopenharmony_ciMODULE_PARM_DESC(keypad_type,
4238c2ecf20Sopenharmony_ci		 "Keypad type: 0=none, 1=old 6 keys, 2=new 6+1 keys, 3=nexcom 4 keys");
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cistatic int lcd_type = NOT_SET;
4268c2ecf20Sopenharmony_cimodule_param(lcd_type, int, 0000);
4278c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_type,
4288c2ecf20Sopenharmony_ci		 "LCD type: 0=none, 1=compiled-in, 2=old, 3=serial ks0074, 4=hantronix, 5=nexcom");
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic int lcd_height = NOT_SET;
4318c2ecf20Sopenharmony_cimodule_param(lcd_height, int, 0000);
4328c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_height, "Number of lines on the LCD");
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_cistatic int lcd_width = NOT_SET;
4358c2ecf20Sopenharmony_cimodule_param(lcd_width, int, 0000);
4368c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_width, "Number of columns on the LCD");
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_cistatic int lcd_bwidth = NOT_SET;	/* internal buffer width (usually 40) */
4398c2ecf20Sopenharmony_cimodule_param(lcd_bwidth, int, 0000);
4408c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_bwidth, "Internal LCD line width (40)");
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_cistatic int lcd_hwidth = NOT_SET;	/* hardware buffer width (usually 64) */
4438c2ecf20Sopenharmony_cimodule_param(lcd_hwidth, int, 0000);
4448c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_hwidth, "LCD line hardware address (64)");
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistatic int lcd_charset = NOT_SET;
4478c2ecf20Sopenharmony_cimodule_param(lcd_charset, int, 0000);
4488c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_charset, "LCD character set: 0=standard, 1=KS0074");
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_cistatic int lcd_proto = NOT_SET;
4518c2ecf20Sopenharmony_cimodule_param(lcd_proto, int, 0000);
4528c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_proto,
4538c2ecf20Sopenharmony_ci		 "LCD communication: 0=parallel (//), 1=serial, 2=TI LCD Interface");
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci/*
4568c2ecf20Sopenharmony_ci * These are the parallel port pins the LCD control signals are connected to.
4578c2ecf20Sopenharmony_ci * Set this to 0 if the signal is not used. Set it to its opposite value
4588c2ecf20Sopenharmony_ci * (negative) if the signal is negated. -MAXINT is used to indicate that the
4598c2ecf20Sopenharmony_ci * pin has not been explicitly specified.
4608c2ecf20Sopenharmony_ci *
4618c2ecf20Sopenharmony_ci * WARNING! no check will be performed about collisions with keypad !
4628c2ecf20Sopenharmony_ci */
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistatic int lcd_e_pin  = PIN_NOT_SET;
4658c2ecf20Sopenharmony_cimodule_param(lcd_e_pin, int, 0000);
4668c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_e_pin,
4678c2ecf20Sopenharmony_ci		 "# of the // port pin connected to LCD 'E' signal, with polarity (-17..17)");
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_cistatic int lcd_rs_pin = PIN_NOT_SET;
4708c2ecf20Sopenharmony_cimodule_param(lcd_rs_pin, int, 0000);
4718c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_rs_pin,
4728c2ecf20Sopenharmony_ci		 "# of the // port pin connected to LCD 'RS' signal, with polarity (-17..17)");
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cistatic int lcd_rw_pin = PIN_NOT_SET;
4758c2ecf20Sopenharmony_cimodule_param(lcd_rw_pin, int, 0000);
4768c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_rw_pin,
4778c2ecf20Sopenharmony_ci		 "# of the // port pin connected to LCD 'RW' signal, with polarity (-17..17)");
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_cistatic int lcd_cl_pin = PIN_NOT_SET;
4808c2ecf20Sopenharmony_cimodule_param(lcd_cl_pin, int, 0000);
4818c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_cl_pin,
4828c2ecf20Sopenharmony_ci		 "# of the // port pin connected to serial LCD 'SCL' signal, with polarity (-17..17)");
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_cistatic int lcd_da_pin = PIN_NOT_SET;
4858c2ecf20Sopenharmony_cimodule_param(lcd_da_pin, int, 0000);
4868c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_da_pin,
4878c2ecf20Sopenharmony_ci		 "# of the // port pin connected to serial LCD 'SDA' signal, with polarity (-17..17)");
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cistatic int lcd_bl_pin = PIN_NOT_SET;
4908c2ecf20Sopenharmony_cimodule_param(lcd_bl_pin, int, 0000);
4918c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_bl_pin,
4928c2ecf20Sopenharmony_ci		 "# of the // port pin connected to LCD backlight, with polarity (-17..17)");
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci/* Deprecated module parameters - consider not using them anymore */
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic int lcd_enabled = NOT_SET;
4978c2ecf20Sopenharmony_cimodule_param(lcd_enabled, int, 0000);
4988c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lcd_enabled, "Deprecated option, use lcd_type instead");
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic int keypad_enabled = NOT_SET;
5018c2ecf20Sopenharmony_cimodule_param(keypad_enabled, int, 0000);
5028c2ecf20Sopenharmony_ciMODULE_PARM_DESC(keypad_enabled, "Deprecated option, use keypad_type instead");
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci/* for some LCD drivers (ks0074) we need a charset conversion table. */
5058c2ecf20Sopenharmony_cistatic const unsigned char lcd_char_conv_ks0074[256] = {
5068c2ecf20Sopenharmony_ci	/*          0|8   1|9   2|A   3|B   4|C   5|D   6|E   7|F */
5078c2ecf20Sopenharmony_ci	/* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
5088c2ecf20Sopenharmony_ci	/* 0x08 */ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
5098c2ecf20Sopenharmony_ci	/* 0x10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
5108c2ecf20Sopenharmony_ci	/* 0x18 */ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
5118c2ecf20Sopenharmony_ci	/* 0x20 */ 0x20, 0x21, 0x22, 0x23, 0xa2, 0x25, 0x26, 0x27,
5128c2ecf20Sopenharmony_ci	/* 0x28 */ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
5138c2ecf20Sopenharmony_ci	/* 0x30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
5148c2ecf20Sopenharmony_ci	/* 0x38 */ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
5158c2ecf20Sopenharmony_ci	/* 0x40 */ 0xa0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
5168c2ecf20Sopenharmony_ci	/* 0x48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
5178c2ecf20Sopenharmony_ci	/* 0x50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
5188c2ecf20Sopenharmony_ci	/* 0x58 */ 0x58, 0x59, 0x5a, 0xfa, 0xfb, 0xfc, 0x1d, 0xc4,
5198c2ecf20Sopenharmony_ci	/* 0x60 */ 0x96, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
5208c2ecf20Sopenharmony_ci	/* 0x68 */ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
5218c2ecf20Sopenharmony_ci	/* 0x70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
5228c2ecf20Sopenharmony_ci	/* 0x78 */ 0x78, 0x79, 0x7a, 0xfd, 0xfe, 0xff, 0xce, 0x20,
5238c2ecf20Sopenharmony_ci	/* 0x80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
5248c2ecf20Sopenharmony_ci	/* 0x88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
5258c2ecf20Sopenharmony_ci	/* 0x90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
5268c2ecf20Sopenharmony_ci	/* 0x98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
5278c2ecf20Sopenharmony_ci	/* 0xA0 */ 0x20, 0x40, 0xb1, 0xa1, 0x24, 0xa3, 0xfe, 0x5f,
5288c2ecf20Sopenharmony_ci	/* 0xA8 */ 0x22, 0xc8, 0x61, 0x14, 0x97, 0x2d, 0xad, 0x96,
5298c2ecf20Sopenharmony_ci	/* 0xB0 */ 0x80, 0x8c, 0x82, 0x83, 0x27, 0x8f, 0x86, 0xdd,
5308c2ecf20Sopenharmony_ci	/* 0xB8 */ 0x2c, 0x81, 0x6f, 0x15, 0x8b, 0x8a, 0x84, 0x60,
5318c2ecf20Sopenharmony_ci	/* 0xC0 */ 0xe2, 0xe2, 0xe2, 0x5b, 0x5b, 0xae, 0xbc, 0xa9,
5328c2ecf20Sopenharmony_ci	/* 0xC8 */ 0xc5, 0xbf, 0xc6, 0xf1, 0xe3, 0xe3, 0xe3, 0xe3,
5338c2ecf20Sopenharmony_ci	/* 0xD0 */ 0x44, 0x5d, 0xa8, 0xe4, 0xec, 0xec, 0x5c, 0x78,
5348c2ecf20Sopenharmony_ci	/* 0xD8 */ 0xab, 0xa6, 0xe5, 0x5e, 0x5e, 0xe6, 0xaa, 0xbe,
5358c2ecf20Sopenharmony_ci	/* 0xE0 */ 0x7f, 0xe7, 0xaf, 0x7b, 0x7b, 0xaf, 0xbd, 0xc8,
5368c2ecf20Sopenharmony_ci	/* 0xE8 */ 0xa4, 0xa5, 0xc7, 0xf6, 0xa7, 0xe8, 0x69, 0x69,
5378c2ecf20Sopenharmony_ci	/* 0xF0 */ 0xed, 0x7d, 0xa8, 0xe4, 0xec, 0x5c, 0x5c, 0x25,
5388c2ecf20Sopenharmony_ci	/* 0xF8 */ 0xac, 0xa6, 0xea, 0xef, 0x7e, 0xeb, 0xb2, 0x79,
5398c2ecf20Sopenharmony_ci};
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cistatic const char old_keypad_profile[][4][9] = {
5428c2ecf20Sopenharmony_ci	{"S0", "Left\n", "Left\n", ""},
5438c2ecf20Sopenharmony_ci	{"S1", "Down\n", "Down\n", ""},
5448c2ecf20Sopenharmony_ci	{"S2", "Up\n", "Up\n", ""},
5458c2ecf20Sopenharmony_ci	{"S3", "Right\n", "Right\n", ""},
5468c2ecf20Sopenharmony_ci	{"S4", "Esc\n", "Esc\n", ""},
5478c2ecf20Sopenharmony_ci	{"S5", "Ret\n", "Ret\n", ""},
5488c2ecf20Sopenharmony_ci	{"", "", "", ""}
5498c2ecf20Sopenharmony_ci};
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci/* signals, press, repeat, release */
5528c2ecf20Sopenharmony_cistatic const char new_keypad_profile[][4][9] = {
5538c2ecf20Sopenharmony_ci	{"S0", "Left\n", "Left\n", ""},
5548c2ecf20Sopenharmony_ci	{"S1", "Down\n", "Down\n", ""},
5558c2ecf20Sopenharmony_ci	{"S2", "Up\n", "Up\n", ""},
5568c2ecf20Sopenharmony_ci	{"S3", "Right\n", "Right\n", ""},
5578c2ecf20Sopenharmony_ci	{"S4s5", "", "Esc\n", "Esc\n"},
5588c2ecf20Sopenharmony_ci	{"s4S5", "", "Ret\n", "Ret\n"},
5598c2ecf20Sopenharmony_ci	{"S4S5", "Help\n", "", ""},
5608c2ecf20Sopenharmony_ci	/* add new signals above this line */
5618c2ecf20Sopenharmony_ci	{"", "", "", ""}
5628c2ecf20Sopenharmony_ci};
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci/* signals, press, repeat, release */
5658c2ecf20Sopenharmony_cistatic const char nexcom_keypad_profile[][4][9] = {
5668c2ecf20Sopenharmony_ci	{"a-p-e-", "Down\n", "Down\n", ""},
5678c2ecf20Sopenharmony_ci	{"a-p-E-", "Ret\n", "Ret\n", ""},
5688c2ecf20Sopenharmony_ci	{"a-P-E-", "Esc\n", "Esc\n", ""},
5698c2ecf20Sopenharmony_ci	{"a-P-e-", "Up\n", "Up\n", ""},
5708c2ecf20Sopenharmony_ci	/* add new signals above this line */
5718c2ecf20Sopenharmony_ci	{"", "", "", ""}
5728c2ecf20Sopenharmony_ci};
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic const char (*keypad_profile)[4][9] = old_keypad_profile;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_cistatic DECLARE_BITMAP(bits, LCD_BITS);
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cistatic void lcd_get_bits(unsigned int port, int *val)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	unsigned int bit, state;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	for (bit = 0; bit < LCD_BITS; bit++) {
5838c2ecf20Sopenharmony_ci		state = test_bit(bit, bits) ? BIT_SET : BIT_CLR;
5848c2ecf20Sopenharmony_ci		*val &= lcd_bits[port][bit][BIT_MSK];
5858c2ecf20Sopenharmony_ci		*val |= lcd_bits[port][bit][state];
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci}
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci/* sets data port bits according to current signals values */
5908c2ecf20Sopenharmony_cistatic int set_data_bits(void)
5918c2ecf20Sopenharmony_ci{
5928c2ecf20Sopenharmony_ci	int val;
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	val = r_dtr(pprt);
5958c2ecf20Sopenharmony_ci	lcd_get_bits(LCD_PORT_D, &val);
5968c2ecf20Sopenharmony_ci	w_dtr(pprt, val);
5978c2ecf20Sopenharmony_ci	return val;
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci/* sets ctrl port bits according to current signals values */
6018c2ecf20Sopenharmony_cistatic int set_ctrl_bits(void)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	int val;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	val = r_ctr(pprt);
6068c2ecf20Sopenharmony_ci	lcd_get_bits(LCD_PORT_C, &val);
6078c2ecf20Sopenharmony_ci	w_ctr(pprt, val);
6088c2ecf20Sopenharmony_ci	return val;
6098c2ecf20Sopenharmony_ci}
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci/* sets ctrl & data port bits according to current signals values */
6128c2ecf20Sopenharmony_cistatic void panel_set_bits(void)
6138c2ecf20Sopenharmony_ci{
6148c2ecf20Sopenharmony_ci	set_data_bits();
6158c2ecf20Sopenharmony_ci	set_ctrl_bits();
6168c2ecf20Sopenharmony_ci}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci/*
6198c2ecf20Sopenharmony_ci * Converts a parallel port pin (from -25 to 25) to data and control ports
6208c2ecf20Sopenharmony_ci * masks, and data and control port bits. The signal will be considered
6218c2ecf20Sopenharmony_ci * unconnected if it's on pin 0 or an invalid pin (<-25 or >25).
6228c2ecf20Sopenharmony_ci *
6238c2ecf20Sopenharmony_ci * Result will be used this way :
6248c2ecf20Sopenharmony_ci *   out(dport, in(dport) & d_val[2] | d_val[signal_state])
6258c2ecf20Sopenharmony_ci *   out(cport, in(cport) & c_val[2] | c_val[signal_state])
6268c2ecf20Sopenharmony_ci */
6278c2ecf20Sopenharmony_cistatic void pin_to_bits(int pin, unsigned char *d_val, unsigned char *c_val)
6288c2ecf20Sopenharmony_ci{
6298c2ecf20Sopenharmony_ci	int d_bit, c_bit, inv;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	d_val[0] = 0;
6328c2ecf20Sopenharmony_ci	c_val[0] = 0;
6338c2ecf20Sopenharmony_ci	d_val[1] = 0;
6348c2ecf20Sopenharmony_ci	c_val[1] = 0;
6358c2ecf20Sopenharmony_ci	d_val[2] = 0xFF;
6368c2ecf20Sopenharmony_ci	c_val[2] = 0xFF;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	if (pin == 0)
6398c2ecf20Sopenharmony_ci		return;
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	inv = (pin < 0);
6428c2ecf20Sopenharmony_ci	if (inv)
6438c2ecf20Sopenharmony_ci		pin = -pin;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	d_bit = 0;
6468c2ecf20Sopenharmony_ci	c_bit = 0;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	switch (pin) {
6498c2ecf20Sopenharmony_ci	case PIN_STROBE:	/* strobe, inverted */
6508c2ecf20Sopenharmony_ci		c_bit = PNL_PSTROBE;
6518c2ecf20Sopenharmony_ci		inv = !inv;
6528c2ecf20Sopenharmony_ci		break;
6538c2ecf20Sopenharmony_ci	case PIN_D0...PIN_D7:	/* D0 - D7 = 2 - 9 */
6548c2ecf20Sopenharmony_ci		d_bit = 1 << (pin - 2);
6558c2ecf20Sopenharmony_ci		break;
6568c2ecf20Sopenharmony_ci	case PIN_AUTOLF:	/* autofeed, inverted */
6578c2ecf20Sopenharmony_ci		c_bit = PNL_PAUTOLF;
6588c2ecf20Sopenharmony_ci		inv = !inv;
6598c2ecf20Sopenharmony_ci		break;
6608c2ecf20Sopenharmony_ci	case PIN_INITP:		/* init, direct */
6618c2ecf20Sopenharmony_ci		c_bit = PNL_PINITP;
6628c2ecf20Sopenharmony_ci		break;
6638c2ecf20Sopenharmony_ci	case PIN_SELECP:	/* select_in, inverted */
6648c2ecf20Sopenharmony_ci		c_bit = PNL_PSELECP;
6658c2ecf20Sopenharmony_ci		inv = !inv;
6668c2ecf20Sopenharmony_ci		break;
6678c2ecf20Sopenharmony_ci	default:		/* unknown pin, ignore */
6688c2ecf20Sopenharmony_ci		break;
6698c2ecf20Sopenharmony_ci	}
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	if (c_bit) {
6728c2ecf20Sopenharmony_ci		c_val[2] &= ~c_bit;
6738c2ecf20Sopenharmony_ci		c_val[!inv] = c_bit;
6748c2ecf20Sopenharmony_ci	} else if (d_bit) {
6758c2ecf20Sopenharmony_ci		d_val[2] &= ~d_bit;
6768c2ecf20Sopenharmony_ci		d_val[!inv] = d_bit;
6778c2ecf20Sopenharmony_ci	}
6788c2ecf20Sopenharmony_ci}
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci/*
6818c2ecf20Sopenharmony_ci * send a serial byte to the LCD panel. The caller is responsible for locking
6828c2ecf20Sopenharmony_ci * if needed.
6838c2ecf20Sopenharmony_ci */
6848c2ecf20Sopenharmony_cistatic void lcd_send_serial(int byte)
6858c2ecf20Sopenharmony_ci{
6868c2ecf20Sopenharmony_ci	int bit;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	/*
6898c2ecf20Sopenharmony_ci	 * the data bit is set on D0, and the clock on STROBE.
6908c2ecf20Sopenharmony_ci	 * LCD reads D0 on STROBE's rising edge.
6918c2ecf20Sopenharmony_ci	 */
6928c2ecf20Sopenharmony_ci	for (bit = 0; bit < 8; bit++) {
6938c2ecf20Sopenharmony_ci		clear_bit(LCD_BIT_CL, bits);	/* CLK low */
6948c2ecf20Sopenharmony_ci		panel_set_bits();
6958c2ecf20Sopenharmony_ci		if (byte & 1) {
6968c2ecf20Sopenharmony_ci			set_bit(LCD_BIT_DA, bits);
6978c2ecf20Sopenharmony_ci		} else {
6988c2ecf20Sopenharmony_ci			clear_bit(LCD_BIT_DA, bits);
6998c2ecf20Sopenharmony_ci		}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci		panel_set_bits();
7028c2ecf20Sopenharmony_ci		udelay(2);  /* maintain the data during 2 us before CLK up */
7038c2ecf20Sopenharmony_ci		set_bit(LCD_BIT_CL, bits);	/* CLK high */
7048c2ecf20Sopenharmony_ci		panel_set_bits();
7058c2ecf20Sopenharmony_ci		udelay(1);  /* maintain the strobe during 1 us */
7068c2ecf20Sopenharmony_ci		byte >>= 1;
7078c2ecf20Sopenharmony_ci	}
7088c2ecf20Sopenharmony_ci}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci/* turn the backlight on or off */
7118c2ecf20Sopenharmony_cistatic void lcd_backlight(struct charlcd *charlcd, int on)
7128c2ecf20Sopenharmony_ci{
7138c2ecf20Sopenharmony_ci	if (lcd.pins.bl == PIN_NONE)
7148c2ecf20Sopenharmony_ci		return;
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	/* The backlight is activated by setting the AUTOFEED line to +5V  */
7178c2ecf20Sopenharmony_ci	spin_lock_irq(&pprt_lock);
7188c2ecf20Sopenharmony_ci	if (on)
7198c2ecf20Sopenharmony_ci		set_bit(LCD_BIT_BL, bits);
7208c2ecf20Sopenharmony_ci	else
7218c2ecf20Sopenharmony_ci		clear_bit(LCD_BIT_BL, bits);
7228c2ecf20Sopenharmony_ci	panel_set_bits();
7238c2ecf20Sopenharmony_ci	spin_unlock_irq(&pprt_lock);
7248c2ecf20Sopenharmony_ci}
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci/* send a command to the LCD panel in serial mode */
7278c2ecf20Sopenharmony_cistatic void lcd_write_cmd_s(struct charlcd *charlcd, int cmd)
7288c2ecf20Sopenharmony_ci{
7298c2ecf20Sopenharmony_ci	spin_lock_irq(&pprt_lock);
7308c2ecf20Sopenharmony_ci	lcd_send_serial(0x1F);	/* R/W=W, RS=0 */
7318c2ecf20Sopenharmony_ci	lcd_send_serial(cmd & 0x0F);
7328c2ecf20Sopenharmony_ci	lcd_send_serial((cmd >> 4) & 0x0F);
7338c2ecf20Sopenharmony_ci	udelay(40);		/* the shortest command takes at least 40 us */
7348c2ecf20Sopenharmony_ci	spin_unlock_irq(&pprt_lock);
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci/* send data to the LCD panel in serial mode */
7388c2ecf20Sopenharmony_cistatic void lcd_write_data_s(struct charlcd *charlcd, int data)
7398c2ecf20Sopenharmony_ci{
7408c2ecf20Sopenharmony_ci	spin_lock_irq(&pprt_lock);
7418c2ecf20Sopenharmony_ci	lcd_send_serial(0x5F);	/* R/W=W, RS=1 */
7428c2ecf20Sopenharmony_ci	lcd_send_serial(data & 0x0F);
7438c2ecf20Sopenharmony_ci	lcd_send_serial((data >> 4) & 0x0F);
7448c2ecf20Sopenharmony_ci	udelay(40);		/* the shortest data takes at least 40 us */
7458c2ecf20Sopenharmony_ci	spin_unlock_irq(&pprt_lock);
7468c2ecf20Sopenharmony_ci}
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci/* send a command to the LCD panel in 8 bits parallel mode */
7498c2ecf20Sopenharmony_cistatic void lcd_write_cmd_p8(struct charlcd *charlcd, int cmd)
7508c2ecf20Sopenharmony_ci{
7518c2ecf20Sopenharmony_ci	spin_lock_irq(&pprt_lock);
7528c2ecf20Sopenharmony_ci	/* present the data to the data port */
7538c2ecf20Sopenharmony_ci	w_dtr(pprt, cmd);
7548c2ecf20Sopenharmony_ci	udelay(20);	/* maintain the data during 20 us before the strobe */
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	set_bit(LCD_BIT_E, bits);
7578c2ecf20Sopenharmony_ci	clear_bit(LCD_BIT_RS, bits);
7588c2ecf20Sopenharmony_ci	clear_bit(LCD_BIT_RW, bits);
7598c2ecf20Sopenharmony_ci	set_ctrl_bits();
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	udelay(40);	/* maintain the strobe during 40 us */
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	clear_bit(LCD_BIT_E, bits);
7648c2ecf20Sopenharmony_ci	set_ctrl_bits();
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	udelay(120);	/* the shortest command takes at least 120 us */
7678c2ecf20Sopenharmony_ci	spin_unlock_irq(&pprt_lock);
7688c2ecf20Sopenharmony_ci}
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci/* send data to the LCD panel in 8 bits parallel mode */
7718c2ecf20Sopenharmony_cistatic void lcd_write_data_p8(struct charlcd *charlcd, int data)
7728c2ecf20Sopenharmony_ci{
7738c2ecf20Sopenharmony_ci	spin_lock_irq(&pprt_lock);
7748c2ecf20Sopenharmony_ci	/* present the data to the data port */
7758c2ecf20Sopenharmony_ci	w_dtr(pprt, data);
7768c2ecf20Sopenharmony_ci	udelay(20);	/* maintain the data during 20 us before the strobe */
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	set_bit(LCD_BIT_E, bits);
7798c2ecf20Sopenharmony_ci	set_bit(LCD_BIT_RS, bits);
7808c2ecf20Sopenharmony_ci	clear_bit(LCD_BIT_RW, bits);
7818c2ecf20Sopenharmony_ci	set_ctrl_bits();
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	udelay(40);	/* maintain the strobe during 40 us */
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	clear_bit(LCD_BIT_E, bits);
7868c2ecf20Sopenharmony_ci	set_ctrl_bits();
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	udelay(45);	/* the shortest data takes at least 45 us */
7898c2ecf20Sopenharmony_ci	spin_unlock_irq(&pprt_lock);
7908c2ecf20Sopenharmony_ci}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci/* send a command to the TI LCD panel */
7938c2ecf20Sopenharmony_cistatic void lcd_write_cmd_tilcd(struct charlcd *charlcd, int cmd)
7948c2ecf20Sopenharmony_ci{
7958c2ecf20Sopenharmony_ci	spin_lock_irq(&pprt_lock);
7968c2ecf20Sopenharmony_ci	/* present the data to the control port */
7978c2ecf20Sopenharmony_ci	w_ctr(pprt, cmd);
7988c2ecf20Sopenharmony_ci	udelay(60);
7998c2ecf20Sopenharmony_ci	spin_unlock_irq(&pprt_lock);
8008c2ecf20Sopenharmony_ci}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci/* send data to the TI LCD panel */
8038c2ecf20Sopenharmony_cistatic void lcd_write_data_tilcd(struct charlcd *charlcd, int data)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	spin_lock_irq(&pprt_lock);
8068c2ecf20Sopenharmony_ci	/* present the data to the data port */
8078c2ecf20Sopenharmony_ci	w_dtr(pprt, data);
8088c2ecf20Sopenharmony_ci	udelay(60);
8098c2ecf20Sopenharmony_ci	spin_unlock_irq(&pprt_lock);
8108c2ecf20Sopenharmony_ci}
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci/* fills the display with spaces and resets X/Y */
8138c2ecf20Sopenharmony_cistatic void lcd_clear_fast_s(struct charlcd *charlcd)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	int pos;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	spin_lock_irq(&pprt_lock);
8188c2ecf20Sopenharmony_ci	for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
8198c2ecf20Sopenharmony_ci		lcd_send_serial(0x5F);	/* R/W=W, RS=1 */
8208c2ecf20Sopenharmony_ci		lcd_send_serial(' ' & 0x0F);
8218c2ecf20Sopenharmony_ci		lcd_send_serial((' ' >> 4) & 0x0F);
8228c2ecf20Sopenharmony_ci		/* the shortest data takes at least 40 us */
8238c2ecf20Sopenharmony_ci		udelay(40);
8248c2ecf20Sopenharmony_ci	}
8258c2ecf20Sopenharmony_ci	spin_unlock_irq(&pprt_lock);
8268c2ecf20Sopenharmony_ci}
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci/* fills the display with spaces and resets X/Y */
8298c2ecf20Sopenharmony_cistatic void lcd_clear_fast_p8(struct charlcd *charlcd)
8308c2ecf20Sopenharmony_ci{
8318c2ecf20Sopenharmony_ci	int pos;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	spin_lock_irq(&pprt_lock);
8348c2ecf20Sopenharmony_ci	for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
8358c2ecf20Sopenharmony_ci		/* present the data to the data port */
8368c2ecf20Sopenharmony_ci		w_dtr(pprt, ' ');
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci		/* maintain the data during 20 us before the strobe */
8398c2ecf20Sopenharmony_ci		udelay(20);
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci		set_bit(LCD_BIT_E, bits);
8428c2ecf20Sopenharmony_ci		set_bit(LCD_BIT_RS, bits);
8438c2ecf20Sopenharmony_ci		clear_bit(LCD_BIT_RW, bits);
8448c2ecf20Sopenharmony_ci		set_ctrl_bits();
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci		/* maintain the strobe during 40 us */
8478c2ecf20Sopenharmony_ci		udelay(40);
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci		clear_bit(LCD_BIT_E, bits);
8508c2ecf20Sopenharmony_ci		set_ctrl_bits();
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci		/* the shortest data takes at least 45 us */
8538c2ecf20Sopenharmony_ci		udelay(45);
8548c2ecf20Sopenharmony_ci	}
8558c2ecf20Sopenharmony_ci	spin_unlock_irq(&pprt_lock);
8568c2ecf20Sopenharmony_ci}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci/* fills the display with spaces and resets X/Y */
8598c2ecf20Sopenharmony_cistatic void lcd_clear_fast_tilcd(struct charlcd *charlcd)
8608c2ecf20Sopenharmony_ci{
8618c2ecf20Sopenharmony_ci	int pos;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	spin_lock_irq(&pprt_lock);
8648c2ecf20Sopenharmony_ci	for (pos = 0; pos < charlcd->height * charlcd->hwidth; pos++) {
8658c2ecf20Sopenharmony_ci		/* present the data to the data port */
8668c2ecf20Sopenharmony_ci		w_dtr(pprt, ' ');
8678c2ecf20Sopenharmony_ci		udelay(60);
8688c2ecf20Sopenharmony_ci	}
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	spin_unlock_irq(&pprt_lock);
8718c2ecf20Sopenharmony_ci}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_cistatic const struct charlcd_ops charlcd_serial_ops = {
8748c2ecf20Sopenharmony_ci	.write_cmd	= lcd_write_cmd_s,
8758c2ecf20Sopenharmony_ci	.write_data	= lcd_write_data_s,
8768c2ecf20Sopenharmony_ci	.clear_fast	= lcd_clear_fast_s,
8778c2ecf20Sopenharmony_ci	.backlight	= lcd_backlight,
8788c2ecf20Sopenharmony_ci};
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_cistatic const struct charlcd_ops charlcd_parallel_ops = {
8818c2ecf20Sopenharmony_ci	.write_cmd	= lcd_write_cmd_p8,
8828c2ecf20Sopenharmony_ci	.write_data	= lcd_write_data_p8,
8838c2ecf20Sopenharmony_ci	.clear_fast	= lcd_clear_fast_p8,
8848c2ecf20Sopenharmony_ci	.backlight	= lcd_backlight,
8858c2ecf20Sopenharmony_ci};
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_cistatic const struct charlcd_ops charlcd_tilcd_ops = {
8888c2ecf20Sopenharmony_ci	.write_cmd	= lcd_write_cmd_tilcd,
8898c2ecf20Sopenharmony_ci	.write_data	= lcd_write_data_tilcd,
8908c2ecf20Sopenharmony_ci	.clear_fast	= lcd_clear_fast_tilcd,
8918c2ecf20Sopenharmony_ci	.backlight	= lcd_backlight,
8928c2ecf20Sopenharmony_ci};
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci/* initialize the LCD driver */
8958c2ecf20Sopenharmony_cistatic void lcd_init(void)
8968c2ecf20Sopenharmony_ci{
8978c2ecf20Sopenharmony_ci	struct charlcd *charlcd;
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	charlcd = charlcd_alloc(0);
9008c2ecf20Sopenharmony_ci	if (!charlcd)
9018c2ecf20Sopenharmony_ci		return;
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	/*
9048c2ecf20Sopenharmony_ci	 * Init lcd struct with load-time values to preserve exact
9058c2ecf20Sopenharmony_ci	 * current functionality (at least for now).
9068c2ecf20Sopenharmony_ci	 */
9078c2ecf20Sopenharmony_ci	charlcd->height = lcd_height;
9088c2ecf20Sopenharmony_ci	charlcd->width = lcd_width;
9098c2ecf20Sopenharmony_ci	charlcd->bwidth = lcd_bwidth;
9108c2ecf20Sopenharmony_ci	charlcd->hwidth = lcd_hwidth;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	switch (selected_lcd_type) {
9138c2ecf20Sopenharmony_ci	case LCD_TYPE_OLD:
9148c2ecf20Sopenharmony_ci		/* parallel mode, 8 bits */
9158c2ecf20Sopenharmony_ci		lcd.proto = LCD_PROTO_PARALLEL;
9168c2ecf20Sopenharmony_ci		lcd.charset = LCD_CHARSET_NORMAL;
9178c2ecf20Sopenharmony_ci		lcd.pins.e = PIN_STROBE;
9188c2ecf20Sopenharmony_ci		lcd.pins.rs = PIN_AUTOLF;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci		charlcd->width = 40;
9218c2ecf20Sopenharmony_ci		charlcd->bwidth = 40;
9228c2ecf20Sopenharmony_ci		charlcd->hwidth = 64;
9238c2ecf20Sopenharmony_ci		charlcd->height = 2;
9248c2ecf20Sopenharmony_ci		break;
9258c2ecf20Sopenharmony_ci	case LCD_TYPE_KS0074:
9268c2ecf20Sopenharmony_ci		/* serial mode, ks0074 */
9278c2ecf20Sopenharmony_ci		lcd.proto = LCD_PROTO_SERIAL;
9288c2ecf20Sopenharmony_ci		lcd.charset = LCD_CHARSET_KS0074;
9298c2ecf20Sopenharmony_ci		lcd.pins.bl = PIN_AUTOLF;
9308c2ecf20Sopenharmony_ci		lcd.pins.cl = PIN_STROBE;
9318c2ecf20Sopenharmony_ci		lcd.pins.da = PIN_D0;
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci		charlcd->width = 16;
9348c2ecf20Sopenharmony_ci		charlcd->bwidth = 40;
9358c2ecf20Sopenharmony_ci		charlcd->hwidth = 16;
9368c2ecf20Sopenharmony_ci		charlcd->height = 2;
9378c2ecf20Sopenharmony_ci		break;
9388c2ecf20Sopenharmony_ci	case LCD_TYPE_NEXCOM:
9398c2ecf20Sopenharmony_ci		/* parallel mode, 8 bits, generic */
9408c2ecf20Sopenharmony_ci		lcd.proto = LCD_PROTO_PARALLEL;
9418c2ecf20Sopenharmony_ci		lcd.charset = LCD_CHARSET_NORMAL;
9428c2ecf20Sopenharmony_ci		lcd.pins.e = PIN_AUTOLF;
9438c2ecf20Sopenharmony_ci		lcd.pins.rs = PIN_SELECP;
9448c2ecf20Sopenharmony_ci		lcd.pins.rw = PIN_INITP;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci		charlcd->width = 16;
9478c2ecf20Sopenharmony_ci		charlcd->bwidth = 40;
9488c2ecf20Sopenharmony_ci		charlcd->hwidth = 64;
9498c2ecf20Sopenharmony_ci		charlcd->height = 2;
9508c2ecf20Sopenharmony_ci		break;
9518c2ecf20Sopenharmony_ci	case LCD_TYPE_CUSTOM:
9528c2ecf20Sopenharmony_ci		/* customer-defined */
9538c2ecf20Sopenharmony_ci		lcd.proto = DEFAULT_LCD_PROTO;
9548c2ecf20Sopenharmony_ci		lcd.charset = DEFAULT_LCD_CHARSET;
9558c2ecf20Sopenharmony_ci		/* default geometry will be set later */
9568c2ecf20Sopenharmony_ci		break;
9578c2ecf20Sopenharmony_ci	case LCD_TYPE_HANTRONIX:
9588c2ecf20Sopenharmony_ci		/* parallel mode, 8 bits, hantronix-like */
9598c2ecf20Sopenharmony_ci	default:
9608c2ecf20Sopenharmony_ci		lcd.proto = LCD_PROTO_PARALLEL;
9618c2ecf20Sopenharmony_ci		lcd.charset = LCD_CHARSET_NORMAL;
9628c2ecf20Sopenharmony_ci		lcd.pins.e = PIN_STROBE;
9638c2ecf20Sopenharmony_ci		lcd.pins.rs = PIN_SELECP;
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci		charlcd->width = 16;
9668c2ecf20Sopenharmony_ci		charlcd->bwidth = 40;
9678c2ecf20Sopenharmony_ci		charlcd->hwidth = 64;
9688c2ecf20Sopenharmony_ci		charlcd->height = 2;
9698c2ecf20Sopenharmony_ci		break;
9708c2ecf20Sopenharmony_ci	}
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	/* Overwrite with module params set on loading */
9738c2ecf20Sopenharmony_ci	if (lcd_height != NOT_SET)
9748c2ecf20Sopenharmony_ci		charlcd->height = lcd_height;
9758c2ecf20Sopenharmony_ci	if (lcd_width != NOT_SET)
9768c2ecf20Sopenharmony_ci		charlcd->width = lcd_width;
9778c2ecf20Sopenharmony_ci	if (lcd_bwidth != NOT_SET)
9788c2ecf20Sopenharmony_ci		charlcd->bwidth = lcd_bwidth;
9798c2ecf20Sopenharmony_ci	if (lcd_hwidth != NOT_SET)
9808c2ecf20Sopenharmony_ci		charlcd->hwidth = lcd_hwidth;
9818c2ecf20Sopenharmony_ci	if (lcd_charset != NOT_SET)
9828c2ecf20Sopenharmony_ci		lcd.charset = lcd_charset;
9838c2ecf20Sopenharmony_ci	if (lcd_proto != NOT_SET)
9848c2ecf20Sopenharmony_ci		lcd.proto = lcd_proto;
9858c2ecf20Sopenharmony_ci	if (lcd_e_pin != PIN_NOT_SET)
9868c2ecf20Sopenharmony_ci		lcd.pins.e = lcd_e_pin;
9878c2ecf20Sopenharmony_ci	if (lcd_rs_pin != PIN_NOT_SET)
9888c2ecf20Sopenharmony_ci		lcd.pins.rs = lcd_rs_pin;
9898c2ecf20Sopenharmony_ci	if (lcd_rw_pin != PIN_NOT_SET)
9908c2ecf20Sopenharmony_ci		lcd.pins.rw = lcd_rw_pin;
9918c2ecf20Sopenharmony_ci	if (lcd_cl_pin != PIN_NOT_SET)
9928c2ecf20Sopenharmony_ci		lcd.pins.cl = lcd_cl_pin;
9938c2ecf20Sopenharmony_ci	if (lcd_da_pin != PIN_NOT_SET)
9948c2ecf20Sopenharmony_ci		lcd.pins.da = lcd_da_pin;
9958c2ecf20Sopenharmony_ci	if (lcd_bl_pin != PIN_NOT_SET)
9968c2ecf20Sopenharmony_ci		lcd.pins.bl = lcd_bl_pin;
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	/* this is used to catch wrong and default values */
9998c2ecf20Sopenharmony_ci	if (charlcd->width <= 0)
10008c2ecf20Sopenharmony_ci		charlcd->width = DEFAULT_LCD_WIDTH;
10018c2ecf20Sopenharmony_ci	if (charlcd->bwidth <= 0)
10028c2ecf20Sopenharmony_ci		charlcd->bwidth = DEFAULT_LCD_BWIDTH;
10038c2ecf20Sopenharmony_ci	if (charlcd->hwidth <= 0)
10048c2ecf20Sopenharmony_ci		charlcd->hwidth = DEFAULT_LCD_HWIDTH;
10058c2ecf20Sopenharmony_ci	if (charlcd->height <= 0)
10068c2ecf20Sopenharmony_ci		charlcd->height = DEFAULT_LCD_HEIGHT;
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	if (lcd.proto == LCD_PROTO_SERIAL) {	/* SERIAL */
10098c2ecf20Sopenharmony_ci		charlcd->ops = &charlcd_serial_ops;
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci		if (lcd.pins.cl == PIN_NOT_SET)
10128c2ecf20Sopenharmony_ci			lcd.pins.cl = DEFAULT_LCD_PIN_SCL;
10138c2ecf20Sopenharmony_ci		if (lcd.pins.da == PIN_NOT_SET)
10148c2ecf20Sopenharmony_ci			lcd.pins.da = DEFAULT_LCD_PIN_SDA;
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	} else if (lcd.proto == LCD_PROTO_PARALLEL) {	/* PARALLEL */
10178c2ecf20Sopenharmony_ci		charlcd->ops = &charlcd_parallel_ops;
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci		if (lcd.pins.e == PIN_NOT_SET)
10208c2ecf20Sopenharmony_ci			lcd.pins.e = DEFAULT_LCD_PIN_E;
10218c2ecf20Sopenharmony_ci		if (lcd.pins.rs == PIN_NOT_SET)
10228c2ecf20Sopenharmony_ci			lcd.pins.rs = DEFAULT_LCD_PIN_RS;
10238c2ecf20Sopenharmony_ci		if (lcd.pins.rw == PIN_NOT_SET)
10248c2ecf20Sopenharmony_ci			lcd.pins.rw = DEFAULT_LCD_PIN_RW;
10258c2ecf20Sopenharmony_ci	} else {
10268c2ecf20Sopenharmony_ci		charlcd->ops = &charlcd_tilcd_ops;
10278c2ecf20Sopenharmony_ci	}
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	if (lcd.pins.bl == PIN_NOT_SET)
10308c2ecf20Sopenharmony_ci		lcd.pins.bl = DEFAULT_LCD_PIN_BL;
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci	if (lcd.pins.e == PIN_NOT_SET)
10338c2ecf20Sopenharmony_ci		lcd.pins.e = PIN_NONE;
10348c2ecf20Sopenharmony_ci	if (lcd.pins.rs == PIN_NOT_SET)
10358c2ecf20Sopenharmony_ci		lcd.pins.rs = PIN_NONE;
10368c2ecf20Sopenharmony_ci	if (lcd.pins.rw == PIN_NOT_SET)
10378c2ecf20Sopenharmony_ci		lcd.pins.rw = PIN_NONE;
10388c2ecf20Sopenharmony_ci	if (lcd.pins.bl == PIN_NOT_SET)
10398c2ecf20Sopenharmony_ci		lcd.pins.bl = PIN_NONE;
10408c2ecf20Sopenharmony_ci	if (lcd.pins.cl == PIN_NOT_SET)
10418c2ecf20Sopenharmony_ci		lcd.pins.cl = PIN_NONE;
10428c2ecf20Sopenharmony_ci	if (lcd.pins.da == PIN_NOT_SET)
10438c2ecf20Sopenharmony_ci		lcd.pins.da = PIN_NONE;
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	if (lcd.charset == NOT_SET)
10468c2ecf20Sopenharmony_ci		lcd.charset = DEFAULT_LCD_CHARSET;
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	if (lcd.charset == LCD_CHARSET_KS0074)
10498c2ecf20Sopenharmony_ci		charlcd->char_conv = lcd_char_conv_ks0074;
10508c2ecf20Sopenharmony_ci	else
10518c2ecf20Sopenharmony_ci		charlcd->char_conv = NULL;
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	pin_to_bits(lcd.pins.e, lcd_bits[LCD_PORT_D][LCD_BIT_E],
10548c2ecf20Sopenharmony_ci		    lcd_bits[LCD_PORT_C][LCD_BIT_E]);
10558c2ecf20Sopenharmony_ci	pin_to_bits(lcd.pins.rs, lcd_bits[LCD_PORT_D][LCD_BIT_RS],
10568c2ecf20Sopenharmony_ci		    lcd_bits[LCD_PORT_C][LCD_BIT_RS]);
10578c2ecf20Sopenharmony_ci	pin_to_bits(lcd.pins.rw, lcd_bits[LCD_PORT_D][LCD_BIT_RW],
10588c2ecf20Sopenharmony_ci		    lcd_bits[LCD_PORT_C][LCD_BIT_RW]);
10598c2ecf20Sopenharmony_ci	pin_to_bits(lcd.pins.bl, lcd_bits[LCD_PORT_D][LCD_BIT_BL],
10608c2ecf20Sopenharmony_ci		    lcd_bits[LCD_PORT_C][LCD_BIT_BL]);
10618c2ecf20Sopenharmony_ci	pin_to_bits(lcd.pins.cl, lcd_bits[LCD_PORT_D][LCD_BIT_CL],
10628c2ecf20Sopenharmony_ci		    lcd_bits[LCD_PORT_C][LCD_BIT_CL]);
10638c2ecf20Sopenharmony_ci	pin_to_bits(lcd.pins.da, lcd_bits[LCD_PORT_D][LCD_BIT_DA],
10648c2ecf20Sopenharmony_ci		    lcd_bits[LCD_PORT_C][LCD_BIT_DA]);
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	lcd.charlcd = charlcd;
10678c2ecf20Sopenharmony_ci	lcd.initialized = true;
10688c2ecf20Sopenharmony_ci}
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci/*
10718c2ecf20Sopenharmony_ci * These are the file operation function for user access to /dev/keypad
10728c2ecf20Sopenharmony_ci */
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_cistatic ssize_t keypad_read(struct file *file,
10758c2ecf20Sopenharmony_ci			   char __user *buf, size_t count, loff_t *ppos)
10768c2ecf20Sopenharmony_ci{
10778c2ecf20Sopenharmony_ci	unsigned i = *ppos;
10788c2ecf20Sopenharmony_ci	char __user *tmp = buf;
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci	if (keypad_buflen == 0) {
10818c2ecf20Sopenharmony_ci		if (file->f_flags & O_NONBLOCK)
10828c2ecf20Sopenharmony_ci			return -EAGAIN;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci		if (wait_event_interruptible(keypad_read_wait,
10858c2ecf20Sopenharmony_ci					     keypad_buflen != 0))
10868c2ecf20Sopenharmony_ci			return -EINTR;
10878c2ecf20Sopenharmony_ci	}
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	for (; count-- > 0 && (keypad_buflen > 0);
10908c2ecf20Sopenharmony_ci	     ++i, ++tmp, --keypad_buflen) {
10918c2ecf20Sopenharmony_ci		put_user(keypad_buffer[keypad_start], tmp);
10928c2ecf20Sopenharmony_ci		keypad_start = (keypad_start + 1) % KEYPAD_BUFFER;
10938c2ecf20Sopenharmony_ci	}
10948c2ecf20Sopenharmony_ci	*ppos = i;
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci	return tmp - buf;
10978c2ecf20Sopenharmony_ci}
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_cistatic int keypad_open(struct inode *inode, struct file *file)
11008c2ecf20Sopenharmony_ci{
11018c2ecf20Sopenharmony_ci	int ret;
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	ret = -EBUSY;
11048c2ecf20Sopenharmony_ci	if (!atomic_dec_and_test(&keypad_available))
11058c2ecf20Sopenharmony_ci		goto fail;	/* open only once at a time */
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	ret = -EPERM;
11088c2ecf20Sopenharmony_ci	if (file->f_mode & FMODE_WRITE)	/* device is read-only */
11098c2ecf20Sopenharmony_ci		goto fail;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	keypad_buflen = 0;	/* flush the buffer on opening */
11128c2ecf20Sopenharmony_ci	return 0;
11138c2ecf20Sopenharmony_ci fail:
11148c2ecf20Sopenharmony_ci	atomic_inc(&keypad_available);
11158c2ecf20Sopenharmony_ci	return ret;
11168c2ecf20Sopenharmony_ci}
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_cistatic int keypad_release(struct inode *inode, struct file *file)
11198c2ecf20Sopenharmony_ci{
11208c2ecf20Sopenharmony_ci	atomic_inc(&keypad_available);
11218c2ecf20Sopenharmony_ci	return 0;
11228c2ecf20Sopenharmony_ci}
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_cistatic const struct file_operations keypad_fops = {
11258c2ecf20Sopenharmony_ci	.read    = keypad_read,		/* read */
11268c2ecf20Sopenharmony_ci	.open    = keypad_open,		/* open */
11278c2ecf20Sopenharmony_ci	.release = keypad_release,	/* close */
11288c2ecf20Sopenharmony_ci	.llseek  = default_llseek,
11298c2ecf20Sopenharmony_ci};
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_cistatic struct miscdevice keypad_dev = {
11328c2ecf20Sopenharmony_ci	.minor	= KEYPAD_MINOR,
11338c2ecf20Sopenharmony_ci	.name	= "keypad",
11348c2ecf20Sopenharmony_ci	.fops	= &keypad_fops,
11358c2ecf20Sopenharmony_ci};
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_cistatic void keypad_send_key(const char *string, int max_len)
11388c2ecf20Sopenharmony_ci{
11398c2ecf20Sopenharmony_ci	/* send the key to the device only if a process is attached to it. */
11408c2ecf20Sopenharmony_ci	if (!atomic_read(&keypad_available)) {
11418c2ecf20Sopenharmony_ci		while (max_len-- && keypad_buflen < KEYPAD_BUFFER && *string) {
11428c2ecf20Sopenharmony_ci			keypad_buffer[(keypad_start + keypad_buflen++) %
11438c2ecf20Sopenharmony_ci				      KEYPAD_BUFFER] = *string++;
11448c2ecf20Sopenharmony_ci		}
11458c2ecf20Sopenharmony_ci		wake_up_interruptible(&keypad_read_wait);
11468c2ecf20Sopenharmony_ci	}
11478c2ecf20Sopenharmony_ci}
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci/* this function scans all the bits involving at least one logical signal,
11508c2ecf20Sopenharmony_ci * and puts the results in the bitfield "phys_read" (one bit per established
11518c2ecf20Sopenharmony_ci * contact), and sets "phys_read_prev" to "phys_read".
11528c2ecf20Sopenharmony_ci *
11538c2ecf20Sopenharmony_ci * Note: to debounce input signals, we will only consider as switched a signal
11548c2ecf20Sopenharmony_ci * which is stable across 2 measures. Signals which are different between two
11558c2ecf20Sopenharmony_ci * reads will be kept as they previously were in their logical form (phys_prev).
11568c2ecf20Sopenharmony_ci * A signal which has just switched will have a 1 in
11578c2ecf20Sopenharmony_ci * (phys_read ^ phys_read_prev).
11588c2ecf20Sopenharmony_ci */
11598c2ecf20Sopenharmony_cistatic void phys_scan_contacts(void)
11608c2ecf20Sopenharmony_ci{
11618c2ecf20Sopenharmony_ci	int bit, bitval;
11628c2ecf20Sopenharmony_ci	char oldval;
11638c2ecf20Sopenharmony_ci	char bitmask;
11648c2ecf20Sopenharmony_ci	char gndmask;
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	phys_prev = phys_curr;
11678c2ecf20Sopenharmony_ci	phys_read_prev = phys_read;
11688c2ecf20Sopenharmony_ci	phys_read = 0;		/* flush all signals */
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	/* keep track of old value, with all outputs disabled */
11718c2ecf20Sopenharmony_ci	oldval = r_dtr(pprt) | scan_mask_o;
11728c2ecf20Sopenharmony_ci	/* activate all keyboard outputs (active low) */
11738c2ecf20Sopenharmony_ci	w_dtr(pprt, oldval & ~scan_mask_o);
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	/* will have a 1 for each bit set to gnd */
11768c2ecf20Sopenharmony_ci	bitmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
11778c2ecf20Sopenharmony_ci	/* disable all matrix signals */
11788c2ecf20Sopenharmony_ci	w_dtr(pprt, oldval);
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_ci	/* now that all outputs are cleared, the only active input bits are
11818c2ecf20Sopenharmony_ci	 * directly connected to the ground
11828c2ecf20Sopenharmony_ci	 */
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	/* 1 for each grounded input */
11858c2ecf20Sopenharmony_ci	gndmask = PNL_PINPUT(r_str(pprt)) & scan_mask_i;
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci	/* grounded inputs are signals 40-44 */
11888c2ecf20Sopenharmony_ci	phys_read |= (__u64)gndmask << 40;
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	if (bitmask != gndmask) {
11918c2ecf20Sopenharmony_ci		/*
11928c2ecf20Sopenharmony_ci		 * since clearing the outputs changed some inputs, we know
11938c2ecf20Sopenharmony_ci		 * that some input signals are currently tied to some outputs.
11948c2ecf20Sopenharmony_ci		 * So we'll scan them.
11958c2ecf20Sopenharmony_ci		 */
11968c2ecf20Sopenharmony_ci		for (bit = 0; bit < 8; bit++) {
11978c2ecf20Sopenharmony_ci			bitval = BIT(bit);
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci			if (!(scan_mask_o & bitval))	/* skip unused bits */
12008c2ecf20Sopenharmony_ci				continue;
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci			w_dtr(pprt, oldval & ~bitval);	/* enable this output */
12038c2ecf20Sopenharmony_ci			bitmask = PNL_PINPUT(r_str(pprt)) & ~gndmask;
12048c2ecf20Sopenharmony_ci			phys_read |= (__u64)bitmask << (5 * bit);
12058c2ecf20Sopenharmony_ci		}
12068c2ecf20Sopenharmony_ci		w_dtr(pprt, oldval);	/* disable all outputs */
12078c2ecf20Sopenharmony_ci	}
12088c2ecf20Sopenharmony_ci	/*
12098c2ecf20Sopenharmony_ci	 * this is easy: use old bits when they are flapping,
12108c2ecf20Sopenharmony_ci	 * use new ones when stable
12118c2ecf20Sopenharmony_ci	 */
12128c2ecf20Sopenharmony_ci	phys_curr = (phys_prev & (phys_read ^ phys_read_prev)) |
12138c2ecf20Sopenharmony_ci		    (phys_read & ~(phys_read ^ phys_read_prev));
12148c2ecf20Sopenharmony_ci}
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_cistatic inline int input_state_high(struct logical_input *input)
12178c2ecf20Sopenharmony_ci{
12188c2ecf20Sopenharmony_ci#if 0
12198c2ecf20Sopenharmony_ci	/* FIXME:
12208c2ecf20Sopenharmony_ci	 * this is an invalid test. It tries to catch
12218c2ecf20Sopenharmony_ci	 * transitions from single-key to multiple-key, but
12228c2ecf20Sopenharmony_ci	 * doesn't take into account the contacts polarity.
12238c2ecf20Sopenharmony_ci	 * The only solution to the problem is to parse keys
12248c2ecf20Sopenharmony_ci	 * from the most complex to the simplest combinations,
12258c2ecf20Sopenharmony_ci	 * and mark them as 'caught' once a combination
12268c2ecf20Sopenharmony_ci	 * matches, then unmatch it for all other ones.
12278c2ecf20Sopenharmony_ci	 */
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci	/* try to catch dangerous transitions cases :
12308c2ecf20Sopenharmony_ci	 * someone adds a bit, so this signal was a false
12318c2ecf20Sopenharmony_ci	 * positive resulting from a transition. We should
12328c2ecf20Sopenharmony_ci	 * invalidate the signal immediately and not call the
12338c2ecf20Sopenharmony_ci	 * release function.
12348c2ecf20Sopenharmony_ci	 * eg: 0 -(press A)-> A -(press B)-> AB : don't match A's release.
12358c2ecf20Sopenharmony_ci	 */
12368c2ecf20Sopenharmony_ci	if (((phys_prev & input->mask) == input->value) &&
12378c2ecf20Sopenharmony_ci	    ((phys_curr & input->mask) >  input->value)) {
12388c2ecf20Sopenharmony_ci		input->state = INPUT_ST_LOW; /* invalidate */
12398c2ecf20Sopenharmony_ci		return 1;
12408c2ecf20Sopenharmony_ci	}
12418c2ecf20Sopenharmony_ci#endif
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	if ((phys_curr & input->mask) == input->value) {
12448c2ecf20Sopenharmony_ci		if ((input->type == INPUT_TYPE_STD) &&
12458c2ecf20Sopenharmony_ci		    (input->high_timer == 0)) {
12468c2ecf20Sopenharmony_ci			input->high_timer++;
12478c2ecf20Sopenharmony_ci			if (input->u.std.press_fct)
12488c2ecf20Sopenharmony_ci				input->u.std.press_fct(input->u.std.press_data);
12498c2ecf20Sopenharmony_ci		} else if (input->type == INPUT_TYPE_KBD) {
12508c2ecf20Sopenharmony_ci			/* will turn on the light */
12518c2ecf20Sopenharmony_ci			keypressed = 1;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci			if (input->high_timer == 0) {
12548c2ecf20Sopenharmony_ci				char *press_str = input->u.kbd.press_str;
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci				if (press_str[0]) {
12578c2ecf20Sopenharmony_ci					int s = sizeof(input->u.kbd.press_str);
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci					keypad_send_key(press_str, s);
12608c2ecf20Sopenharmony_ci				}
12618c2ecf20Sopenharmony_ci			}
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci			if (input->u.kbd.repeat_str[0]) {
12648c2ecf20Sopenharmony_ci				char *repeat_str = input->u.kbd.repeat_str;
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci				if (input->high_timer >= KEYPAD_REP_START) {
12678c2ecf20Sopenharmony_ci					int s = sizeof(input->u.kbd.repeat_str);
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci					input->high_timer -= KEYPAD_REP_DELAY;
12708c2ecf20Sopenharmony_ci					keypad_send_key(repeat_str, s);
12718c2ecf20Sopenharmony_ci				}
12728c2ecf20Sopenharmony_ci				/* we will need to come back here soon */
12738c2ecf20Sopenharmony_ci				inputs_stable = 0;
12748c2ecf20Sopenharmony_ci			}
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci			if (input->high_timer < 255)
12778c2ecf20Sopenharmony_ci				input->high_timer++;
12788c2ecf20Sopenharmony_ci		}
12798c2ecf20Sopenharmony_ci		return 1;
12808c2ecf20Sopenharmony_ci	}
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci	/* else signal falling down. Let's fall through. */
12838c2ecf20Sopenharmony_ci	input->state = INPUT_ST_FALLING;
12848c2ecf20Sopenharmony_ci	input->fall_timer = 0;
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	return 0;
12878c2ecf20Sopenharmony_ci}
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_cistatic inline void input_state_falling(struct logical_input *input)
12908c2ecf20Sopenharmony_ci{
12918c2ecf20Sopenharmony_ci#if 0
12928c2ecf20Sopenharmony_ci	/* FIXME !!! same comment as in input_state_high */
12938c2ecf20Sopenharmony_ci	if (((phys_prev & input->mask) == input->value) &&
12948c2ecf20Sopenharmony_ci	    ((phys_curr & input->mask) >  input->value)) {
12958c2ecf20Sopenharmony_ci		input->state = INPUT_ST_LOW;	/* invalidate */
12968c2ecf20Sopenharmony_ci		return;
12978c2ecf20Sopenharmony_ci	}
12988c2ecf20Sopenharmony_ci#endif
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	if ((phys_curr & input->mask) == input->value) {
13018c2ecf20Sopenharmony_ci		if (input->type == INPUT_TYPE_KBD) {
13028c2ecf20Sopenharmony_ci			/* will turn on the light */
13038c2ecf20Sopenharmony_ci			keypressed = 1;
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci			if (input->u.kbd.repeat_str[0]) {
13068c2ecf20Sopenharmony_ci				char *repeat_str = input->u.kbd.repeat_str;
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci				if (input->high_timer >= KEYPAD_REP_START) {
13098c2ecf20Sopenharmony_ci					int s = sizeof(input->u.kbd.repeat_str);
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci					input->high_timer -= KEYPAD_REP_DELAY;
13128c2ecf20Sopenharmony_ci					keypad_send_key(repeat_str, s);
13138c2ecf20Sopenharmony_ci				}
13148c2ecf20Sopenharmony_ci				/* we will need to come back here soon */
13158c2ecf20Sopenharmony_ci				inputs_stable = 0;
13168c2ecf20Sopenharmony_ci			}
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci			if (input->high_timer < 255)
13198c2ecf20Sopenharmony_ci				input->high_timer++;
13208c2ecf20Sopenharmony_ci		}
13218c2ecf20Sopenharmony_ci		input->state = INPUT_ST_HIGH;
13228c2ecf20Sopenharmony_ci	} else if (input->fall_timer >= input->fall_time) {
13238c2ecf20Sopenharmony_ci		/* call release event */
13248c2ecf20Sopenharmony_ci		if (input->type == INPUT_TYPE_STD) {
13258c2ecf20Sopenharmony_ci			void (*release_fct)(int) = input->u.std.release_fct;
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci			if (release_fct)
13288c2ecf20Sopenharmony_ci				release_fct(input->u.std.release_data);
13298c2ecf20Sopenharmony_ci		} else if (input->type == INPUT_TYPE_KBD) {
13308c2ecf20Sopenharmony_ci			char *release_str = input->u.kbd.release_str;
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci			if (release_str[0]) {
13338c2ecf20Sopenharmony_ci				int s = sizeof(input->u.kbd.release_str);
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_ci				keypad_send_key(release_str, s);
13368c2ecf20Sopenharmony_ci			}
13378c2ecf20Sopenharmony_ci		}
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci		input->state = INPUT_ST_LOW;
13408c2ecf20Sopenharmony_ci	} else {
13418c2ecf20Sopenharmony_ci		input->fall_timer++;
13428c2ecf20Sopenharmony_ci		inputs_stable = 0;
13438c2ecf20Sopenharmony_ci	}
13448c2ecf20Sopenharmony_ci}
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_cistatic void panel_process_inputs(void)
13478c2ecf20Sopenharmony_ci{
13488c2ecf20Sopenharmony_ci	struct logical_input *input;
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci	keypressed = 0;
13518c2ecf20Sopenharmony_ci	inputs_stable = 1;
13528c2ecf20Sopenharmony_ci	list_for_each_entry(input, &logical_inputs, list) {
13538c2ecf20Sopenharmony_ci		switch (input->state) {
13548c2ecf20Sopenharmony_ci		case INPUT_ST_LOW:
13558c2ecf20Sopenharmony_ci			if ((phys_curr & input->mask) != input->value)
13568c2ecf20Sopenharmony_ci				break;
13578c2ecf20Sopenharmony_ci			/* if all needed ones were already set previously,
13588c2ecf20Sopenharmony_ci			 * this means that this logical signal has been
13598c2ecf20Sopenharmony_ci			 * activated by the releasing of another combined
13608c2ecf20Sopenharmony_ci			 * signal, so we don't want to match.
13618c2ecf20Sopenharmony_ci			 * eg: AB -(release B)-> A -(release A)-> 0 :
13628c2ecf20Sopenharmony_ci			 *     don't match A.
13638c2ecf20Sopenharmony_ci			 */
13648c2ecf20Sopenharmony_ci			if ((phys_prev & input->mask) == input->value)
13658c2ecf20Sopenharmony_ci				break;
13668c2ecf20Sopenharmony_ci			input->rise_timer = 0;
13678c2ecf20Sopenharmony_ci			input->state = INPUT_ST_RISING;
13688c2ecf20Sopenharmony_ci			fallthrough;
13698c2ecf20Sopenharmony_ci		case INPUT_ST_RISING:
13708c2ecf20Sopenharmony_ci			if ((phys_curr & input->mask) != input->value) {
13718c2ecf20Sopenharmony_ci				input->state = INPUT_ST_LOW;
13728c2ecf20Sopenharmony_ci				break;
13738c2ecf20Sopenharmony_ci			}
13748c2ecf20Sopenharmony_ci			if (input->rise_timer < input->rise_time) {
13758c2ecf20Sopenharmony_ci				inputs_stable = 0;
13768c2ecf20Sopenharmony_ci				input->rise_timer++;
13778c2ecf20Sopenharmony_ci				break;
13788c2ecf20Sopenharmony_ci			}
13798c2ecf20Sopenharmony_ci			input->high_timer = 0;
13808c2ecf20Sopenharmony_ci			input->state = INPUT_ST_HIGH;
13818c2ecf20Sopenharmony_ci			fallthrough;
13828c2ecf20Sopenharmony_ci		case INPUT_ST_HIGH:
13838c2ecf20Sopenharmony_ci			if (input_state_high(input))
13848c2ecf20Sopenharmony_ci				break;
13858c2ecf20Sopenharmony_ci			fallthrough;
13868c2ecf20Sopenharmony_ci		case INPUT_ST_FALLING:
13878c2ecf20Sopenharmony_ci			input_state_falling(input);
13888c2ecf20Sopenharmony_ci		}
13898c2ecf20Sopenharmony_ci	}
13908c2ecf20Sopenharmony_ci}
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_cistatic void panel_scan_timer(struct timer_list *unused)
13938c2ecf20Sopenharmony_ci{
13948c2ecf20Sopenharmony_ci	if (keypad.enabled && keypad_initialized) {
13958c2ecf20Sopenharmony_ci		if (spin_trylock_irq(&pprt_lock)) {
13968c2ecf20Sopenharmony_ci			phys_scan_contacts();
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci			/* no need for the parport anymore */
13998c2ecf20Sopenharmony_ci			spin_unlock_irq(&pprt_lock);
14008c2ecf20Sopenharmony_ci		}
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci		if (!inputs_stable || phys_curr != phys_prev)
14038c2ecf20Sopenharmony_ci			panel_process_inputs();
14048c2ecf20Sopenharmony_ci	}
14058c2ecf20Sopenharmony_ci
14068c2ecf20Sopenharmony_ci	if (keypressed && lcd.enabled && lcd.initialized)
14078c2ecf20Sopenharmony_ci		charlcd_poke(lcd.charlcd);
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_ci	mod_timer(&scan_timer, jiffies + INPUT_POLL_TIME);
14108c2ecf20Sopenharmony_ci}
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_cistatic void init_scan_timer(void)
14138c2ecf20Sopenharmony_ci{
14148c2ecf20Sopenharmony_ci	if (scan_timer.function)
14158c2ecf20Sopenharmony_ci		return;		/* already started */
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci	timer_setup(&scan_timer, panel_scan_timer, 0);
14188c2ecf20Sopenharmony_ci	scan_timer.expires = jiffies + INPUT_POLL_TIME;
14198c2ecf20Sopenharmony_ci	add_timer(&scan_timer);
14208c2ecf20Sopenharmony_ci}
14218c2ecf20Sopenharmony_ci
14228c2ecf20Sopenharmony_ci/* converts a name of the form "({BbAaPpSsEe}{01234567-})*" to a series of bits.
14238c2ecf20Sopenharmony_ci * if <omask> or <imask> are non-null, they will be or'ed with the bits
14248c2ecf20Sopenharmony_ci * corresponding to out and in bits respectively.
14258c2ecf20Sopenharmony_ci * returns 1 if ok, 0 if error (in which case, nothing is written).
14268c2ecf20Sopenharmony_ci */
14278c2ecf20Sopenharmony_cistatic u8 input_name2mask(const char *name, __u64 *mask, __u64 *value,
14288c2ecf20Sopenharmony_ci			  u8 *imask, u8 *omask)
14298c2ecf20Sopenharmony_ci{
14308c2ecf20Sopenharmony_ci	const char sigtab[] = "EeSsPpAaBb";
14318c2ecf20Sopenharmony_ci	u8 im, om;
14328c2ecf20Sopenharmony_ci	__u64 m, v;
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci	om = 0;
14358c2ecf20Sopenharmony_ci	im = 0;
14368c2ecf20Sopenharmony_ci	m = 0ULL;
14378c2ecf20Sopenharmony_ci	v = 0ULL;
14388c2ecf20Sopenharmony_ci	while (*name) {
14398c2ecf20Sopenharmony_ci		int in, out, bit, neg;
14408c2ecf20Sopenharmony_ci		const char *idx;
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci		idx = strchr(sigtab, *name);
14438c2ecf20Sopenharmony_ci		if (!idx)
14448c2ecf20Sopenharmony_ci			return 0;	/* input name not found */
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci		in = idx - sigtab;
14478c2ecf20Sopenharmony_ci		neg = (in & 1);	/* odd (lower) names are negated */
14488c2ecf20Sopenharmony_ci		in >>= 1;
14498c2ecf20Sopenharmony_ci		im |= BIT(in);
14508c2ecf20Sopenharmony_ci
14518c2ecf20Sopenharmony_ci		name++;
14528c2ecf20Sopenharmony_ci		if (*name >= '0' && *name <= '7') {
14538c2ecf20Sopenharmony_ci			out = *name - '0';
14548c2ecf20Sopenharmony_ci			om |= BIT(out);
14558c2ecf20Sopenharmony_ci		} else if (*name == '-') {
14568c2ecf20Sopenharmony_ci			out = 8;
14578c2ecf20Sopenharmony_ci		} else {
14588c2ecf20Sopenharmony_ci			return 0;	/* unknown bit name */
14598c2ecf20Sopenharmony_ci		}
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci		bit = (out * 5) + in;
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci		m |= 1ULL << bit;
14648c2ecf20Sopenharmony_ci		if (!neg)
14658c2ecf20Sopenharmony_ci			v |= 1ULL << bit;
14668c2ecf20Sopenharmony_ci		name++;
14678c2ecf20Sopenharmony_ci	}
14688c2ecf20Sopenharmony_ci	*mask = m;
14698c2ecf20Sopenharmony_ci	*value = v;
14708c2ecf20Sopenharmony_ci	if (imask)
14718c2ecf20Sopenharmony_ci		*imask |= im;
14728c2ecf20Sopenharmony_ci	if (omask)
14738c2ecf20Sopenharmony_ci		*omask |= om;
14748c2ecf20Sopenharmony_ci	return 1;
14758c2ecf20Sopenharmony_ci}
14768c2ecf20Sopenharmony_ci
14778c2ecf20Sopenharmony_ci/* tries to bind a key to the signal name <name>. The key will send the
14788c2ecf20Sopenharmony_ci * strings <press>, <repeat>, <release> for these respective events.
14798c2ecf20Sopenharmony_ci * Returns the pointer to the new key if ok, NULL if the key could not be bound.
14808c2ecf20Sopenharmony_ci */
14818c2ecf20Sopenharmony_cistatic struct logical_input *panel_bind_key(const char *name, const char *press,
14828c2ecf20Sopenharmony_ci					    const char *repeat,
14838c2ecf20Sopenharmony_ci					    const char *release)
14848c2ecf20Sopenharmony_ci{
14858c2ecf20Sopenharmony_ci	struct logical_input *key;
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci	key = kzalloc(sizeof(*key), GFP_KERNEL);
14888c2ecf20Sopenharmony_ci	if (!key)
14898c2ecf20Sopenharmony_ci		return NULL;
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci	if (!input_name2mask(name, &key->mask, &key->value, &scan_mask_i,
14928c2ecf20Sopenharmony_ci			     &scan_mask_o)) {
14938c2ecf20Sopenharmony_ci		kfree(key);
14948c2ecf20Sopenharmony_ci		return NULL;
14958c2ecf20Sopenharmony_ci	}
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_ci	key->type = INPUT_TYPE_KBD;
14988c2ecf20Sopenharmony_ci	key->state = INPUT_ST_LOW;
14998c2ecf20Sopenharmony_ci	key->rise_time = 1;
15008c2ecf20Sopenharmony_ci	key->fall_time = 1;
15018c2ecf20Sopenharmony_ci
15028c2ecf20Sopenharmony_ci	strncpy(key->u.kbd.press_str, press, sizeof(key->u.kbd.press_str));
15038c2ecf20Sopenharmony_ci	strncpy(key->u.kbd.repeat_str, repeat, sizeof(key->u.kbd.repeat_str));
15048c2ecf20Sopenharmony_ci	strncpy(key->u.kbd.release_str, release,
15058c2ecf20Sopenharmony_ci		sizeof(key->u.kbd.release_str));
15068c2ecf20Sopenharmony_ci	list_add(&key->list, &logical_inputs);
15078c2ecf20Sopenharmony_ci	return key;
15088c2ecf20Sopenharmony_ci}
15098c2ecf20Sopenharmony_ci
15108c2ecf20Sopenharmony_ci#if 0
15118c2ecf20Sopenharmony_ci/* tries to bind a callback function to the signal name <name>. The function
15128c2ecf20Sopenharmony_ci * <press_fct> will be called with the <press_data> arg when the signal is
15138c2ecf20Sopenharmony_ci * activated, and so on for <release_fct>/<release_data>
15148c2ecf20Sopenharmony_ci * Returns the pointer to the new signal if ok, NULL if the signal could not
15158c2ecf20Sopenharmony_ci * be bound.
15168c2ecf20Sopenharmony_ci */
15178c2ecf20Sopenharmony_cistatic struct logical_input *panel_bind_callback(char *name,
15188c2ecf20Sopenharmony_ci						 void (*press_fct)(int),
15198c2ecf20Sopenharmony_ci						 int press_data,
15208c2ecf20Sopenharmony_ci						 void (*release_fct)(int),
15218c2ecf20Sopenharmony_ci						 int release_data)
15228c2ecf20Sopenharmony_ci{
15238c2ecf20Sopenharmony_ci	struct logical_input *callback;
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_ci	callback = kmalloc(sizeof(*callback), GFP_KERNEL);
15268c2ecf20Sopenharmony_ci	if (!callback)
15278c2ecf20Sopenharmony_ci		return NULL;
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_ci	memset(callback, 0, sizeof(struct logical_input));
15308c2ecf20Sopenharmony_ci	if (!input_name2mask(name, &callback->mask, &callback->value,
15318c2ecf20Sopenharmony_ci			     &scan_mask_i, &scan_mask_o))
15328c2ecf20Sopenharmony_ci		return NULL;
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci	callback->type = INPUT_TYPE_STD;
15358c2ecf20Sopenharmony_ci	callback->state = INPUT_ST_LOW;
15368c2ecf20Sopenharmony_ci	callback->rise_time = 1;
15378c2ecf20Sopenharmony_ci	callback->fall_time = 1;
15388c2ecf20Sopenharmony_ci	callback->u.std.press_fct = press_fct;
15398c2ecf20Sopenharmony_ci	callback->u.std.press_data = press_data;
15408c2ecf20Sopenharmony_ci	callback->u.std.release_fct = release_fct;
15418c2ecf20Sopenharmony_ci	callback->u.std.release_data = release_data;
15428c2ecf20Sopenharmony_ci	list_add(&callback->list, &logical_inputs);
15438c2ecf20Sopenharmony_ci	return callback;
15448c2ecf20Sopenharmony_ci}
15458c2ecf20Sopenharmony_ci#endif
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_cistatic void keypad_init(void)
15488c2ecf20Sopenharmony_ci{
15498c2ecf20Sopenharmony_ci	int keynum;
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci	init_waitqueue_head(&keypad_read_wait);
15528c2ecf20Sopenharmony_ci	keypad_buflen = 0;	/* flushes any eventual noisy keystroke */
15538c2ecf20Sopenharmony_ci
15548c2ecf20Sopenharmony_ci	/* Let's create all known keys */
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	for (keynum = 0; keypad_profile[keynum][0][0]; keynum++) {
15578c2ecf20Sopenharmony_ci		panel_bind_key(keypad_profile[keynum][0],
15588c2ecf20Sopenharmony_ci			       keypad_profile[keynum][1],
15598c2ecf20Sopenharmony_ci			       keypad_profile[keynum][2],
15608c2ecf20Sopenharmony_ci			       keypad_profile[keynum][3]);
15618c2ecf20Sopenharmony_ci	}
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci	init_scan_timer();
15648c2ecf20Sopenharmony_ci	keypad_initialized = 1;
15658c2ecf20Sopenharmony_ci}
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci/**************************************************/
15688c2ecf20Sopenharmony_ci/* device initialization                          */
15698c2ecf20Sopenharmony_ci/**************************************************/
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_cistatic void panel_attach(struct parport *port)
15728c2ecf20Sopenharmony_ci{
15738c2ecf20Sopenharmony_ci	struct pardev_cb panel_cb;
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_ci	if (port->number != parport)
15768c2ecf20Sopenharmony_ci		return;
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_ci	if (pprt) {
15798c2ecf20Sopenharmony_ci		pr_err("%s: port->number=%d parport=%d, already registered!\n",
15808c2ecf20Sopenharmony_ci		       __func__, port->number, parport);
15818c2ecf20Sopenharmony_ci		return;
15828c2ecf20Sopenharmony_ci	}
15838c2ecf20Sopenharmony_ci
15848c2ecf20Sopenharmony_ci	memset(&panel_cb, 0, sizeof(panel_cb));
15858c2ecf20Sopenharmony_ci	panel_cb.private = &pprt;
15868c2ecf20Sopenharmony_ci	/* panel_cb.flags = 0 should be PARPORT_DEV_EXCL? */
15878c2ecf20Sopenharmony_ci
15888c2ecf20Sopenharmony_ci	pprt = parport_register_dev_model(port, "panel", &panel_cb, 0);
15898c2ecf20Sopenharmony_ci	if (!pprt) {
15908c2ecf20Sopenharmony_ci		pr_err("%s: port->number=%d parport=%d, parport_register_device() failed\n",
15918c2ecf20Sopenharmony_ci		       __func__, port->number, parport);
15928c2ecf20Sopenharmony_ci		return;
15938c2ecf20Sopenharmony_ci	}
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci	if (parport_claim(pprt)) {
15968c2ecf20Sopenharmony_ci		pr_err("could not claim access to parport%d. Aborting.\n",
15978c2ecf20Sopenharmony_ci		       parport);
15988c2ecf20Sopenharmony_ci		goto err_unreg_device;
15998c2ecf20Sopenharmony_ci	}
16008c2ecf20Sopenharmony_ci
16018c2ecf20Sopenharmony_ci	/* must init LCD first, just in case an IRQ from the keypad is
16028c2ecf20Sopenharmony_ci	 * generated at keypad init
16038c2ecf20Sopenharmony_ci	 */
16048c2ecf20Sopenharmony_ci	if (lcd.enabled) {
16058c2ecf20Sopenharmony_ci		lcd_init();
16068c2ecf20Sopenharmony_ci		if (!lcd.charlcd || charlcd_register(lcd.charlcd))
16078c2ecf20Sopenharmony_ci			goto err_unreg_device;
16088c2ecf20Sopenharmony_ci	}
16098c2ecf20Sopenharmony_ci
16108c2ecf20Sopenharmony_ci	if (keypad.enabled) {
16118c2ecf20Sopenharmony_ci		keypad_init();
16128c2ecf20Sopenharmony_ci		if (misc_register(&keypad_dev))
16138c2ecf20Sopenharmony_ci			goto err_lcd_unreg;
16148c2ecf20Sopenharmony_ci	}
16158c2ecf20Sopenharmony_ci	return;
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_cierr_lcd_unreg:
16188c2ecf20Sopenharmony_ci	if (scan_timer.function)
16198c2ecf20Sopenharmony_ci		del_timer_sync(&scan_timer);
16208c2ecf20Sopenharmony_ci	if (lcd.enabled)
16218c2ecf20Sopenharmony_ci		charlcd_unregister(lcd.charlcd);
16228c2ecf20Sopenharmony_cierr_unreg_device:
16238c2ecf20Sopenharmony_ci	charlcd_free(lcd.charlcd);
16248c2ecf20Sopenharmony_ci	lcd.charlcd = NULL;
16258c2ecf20Sopenharmony_ci	parport_unregister_device(pprt);
16268c2ecf20Sopenharmony_ci	pprt = NULL;
16278c2ecf20Sopenharmony_ci}
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_cistatic void panel_detach(struct parport *port)
16308c2ecf20Sopenharmony_ci{
16318c2ecf20Sopenharmony_ci	if (port->number != parport)
16328c2ecf20Sopenharmony_ci		return;
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci	if (!pprt) {
16358c2ecf20Sopenharmony_ci		pr_err("%s: port->number=%d parport=%d, nothing to unregister.\n",
16368c2ecf20Sopenharmony_ci		       __func__, port->number, parport);
16378c2ecf20Sopenharmony_ci		return;
16388c2ecf20Sopenharmony_ci	}
16398c2ecf20Sopenharmony_ci	if (scan_timer.function)
16408c2ecf20Sopenharmony_ci		del_timer_sync(&scan_timer);
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ci	if (keypad.enabled) {
16438c2ecf20Sopenharmony_ci		misc_deregister(&keypad_dev);
16448c2ecf20Sopenharmony_ci		keypad_initialized = 0;
16458c2ecf20Sopenharmony_ci	}
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci	if (lcd.enabled) {
16488c2ecf20Sopenharmony_ci		charlcd_unregister(lcd.charlcd);
16498c2ecf20Sopenharmony_ci		lcd.initialized = false;
16508c2ecf20Sopenharmony_ci		charlcd_free(lcd.charlcd);
16518c2ecf20Sopenharmony_ci		lcd.charlcd = NULL;
16528c2ecf20Sopenharmony_ci	}
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_ci	/* TODO: free all input signals */
16558c2ecf20Sopenharmony_ci	parport_release(pprt);
16568c2ecf20Sopenharmony_ci	parport_unregister_device(pprt);
16578c2ecf20Sopenharmony_ci	pprt = NULL;
16588c2ecf20Sopenharmony_ci}
16598c2ecf20Sopenharmony_ci
16608c2ecf20Sopenharmony_cistatic struct parport_driver panel_driver = {
16618c2ecf20Sopenharmony_ci	.name = "panel",
16628c2ecf20Sopenharmony_ci	.match_port = panel_attach,
16638c2ecf20Sopenharmony_ci	.detach = panel_detach,
16648c2ecf20Sopenharmony_ci	.devmodel = true,
16658c2ecf20Sopenharmony_ci};
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ci/* init function */
16688c2ecf20Sopenharmony_cistatic int __init panel_init_module(void)
16698c2ecf20Sopenharmony_ci{
16708c2ecf20Sopenharmony_ci	int selected_keypad_type = NOT_SET, err;
16718c2ecf20Sopenharmony_ci
16728c2ecf20Sopenharmony_ci	/* take care of an eventual profile */
16738c2ecf20Sopenharmony_ci	switch (profile) {
16748c2ecf20Sopenharmony_ci	case PANEL_PROFILE_CUSTOM:
16758c2ecf20Sopenharmony_ci		/* custom profile */
16768c2ecf20Sopenharmony_ci		selected_keypad_type = DEFAULT_KEYPAD_TYPE;
16778c2ecf20Sopenharmony_ci		selected_lcd_type = DEFAULT_LCD_TYPE;
16788c2ecf20Sopenharmony_ci		break;
16798c2ecf20Sopenharmony_ci	case PANEL_PROFILE_OLD:
16808c2ecf20Sopenharmony_ci		/* 8 bits, 2*16, old keypad */
16818c2ecf20Sopenharmony_ci		selected_keypad_type = KEYPAD_TYPE_OLD;
16828c2ecf20Sopenharmony_ci		selected_lcd_type = LCD_TYPE_OLD;
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_ci		/* TODO: This two are a little hacky, sort it out later */
16858c2ecf20Sopenharmony_ci		if (lcd_width == NOT_SET)
16868c2ecf20Sopenharmony_ci			lcd_width = 16;
16878c2ecf20Sopenharmony_ci		if (lcd_hwidth == NOT_SET)
16888c2ecf20Sopenharmony_ci			lcd_hwidth = 16;
16898c2ecf20Sopenharmony_ci		break;
16908c2ecf20Sopenharmony_ci	case PANEL_PROFILE_NEW:
16918c2ecf20Sopenharmony_ci		/* serial, 2*16, new keypad */
16928c2ecf20Sopenharmony_ci		selected_keypad_type = KEYPAD_TYPE_NEW;
16938c2ecf20Sopenharmony_ci		selected_lcd_type = LCD_TYPE_KS0074;
16948c2ecf20Sopenharmony_ci		break;
16958c2ecf20Sopenharmony_ci	case PANEL_PROFILE_HANTRONIX:
16968c2ecf20Sopenharmony_ci		/* 8 bits, 2*16 hantronix-like, no keypad */
16978c2ecf20Sopenharmony_ci		selected_keypad_type = KEYPAD_TYPE_NONE;
16988c2ecf20Sopenharmony_ci		selected_lcd_type = LCD_TYPE_HANTRONIX;
16998c2ecf20Sopenharmony_ci		break;
17008c2ecf20Sopenharmony_ci	case PANEL_PROFILE_NEXCOM:
17018c2ecf20Sopenharmony_ci		/* generic 8 bits, 2*16, nexcom keypad, eg. Nexcom. */
17028c2ecf20Sopenharmony_ci		selected_keypad_type = KEYPAD_TYPE_NEXCOM;
17038c2ecf20Sopenharmony_ci		selected_lcd_type = LCD_TYPE_NEXCOM;
17048c2ecf20Sopenharmony_ci		break;
17058c2ecf20Sopenharmony_ci	case PANEL_PROFILE_LARGE:
17068c2ecf20Sopenharmony_ci		/* 8 bits, 2*40, old keypad */
17078c2ecf20Sopenharmony_ci		selected_keypad_type = KEYPAD_TYPE_OLD;
17088c2ecf20Sopenharmony_ci		selected_lcd_type = LCD_TYPE_OLD;
17098c2ecf20Sopenharmony_ci		break;
17108c2ecf20Sopenharmony_ci	}
17118c2ecf20Sopenharmony_ci
17128c2ecf20Sopenharmony_ci	/*
17138c2ecf20Sopenharmony_ci	 * Overwrite selection with module param values (both keypad and lcd),
17148c2ecf20Sopenharmony_ci	 * where the deprecated params have lower prio.
17158c2ecf20Sopenharmony_ci	 */
17168c2ecf20Sopenharmony_ci	if (keypad_enabled != NOT_SET)
17178c2ecf20Sopenharmony_ci		selected_keypad_type = keypad_enabled;
17188c2ecf20Sopenharmony_ci	if (keypad_type != NOT_SET)
17198c2ecf20Sopenharmony_ci		selected_keypad_type = keypad_type;
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci	keypad.enabled = (selected_keypad_type > 0);
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci	if (lcd_enabled != NOT_SET)
17248c2ecf20Sopenharmony_ci		selected_lcd_type = lcd_enabled;
17258c2ecf20Sopenharmony_ci	if (lcd_type != NOT_SET)
17268c2ecf20Sopenharmony_ci		selected_lcd_type = lcd_type;
17278c2ecf20Sopenharmony_ci
17288c2ecf20Sopenharmony_ci	lcd.enabled = (selected_lcd_type > 0);
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_ci	if (lcd.enabled) {
17318c2ecf20Sopenharmony_ci		/*
17328c2ecf20Sopenharmony_ci		 * Init lcd struct with load-time values to preserve exact
17338c2ecf20Sopenharmony_ci		 * current functionality (at least for now).
17348c2ecf20Sopenharmony_ci		 */
17358c2ecf20Sopenharmony_ci		lcd.charset = lcd_charset;
17368c2ecf20Sopenharmony_ci		lcd.proto = lcd_proto;
17378c2ecf20Sopenharmony_ci		lcd.pins.e = lcd_e_pin;
17388c2ecf20Sopenharmony_ci		lcd.pins.rs = lcd_rs_pin;
17398c2ecf20Sopenharmony_ci		lcd.pins.rw = lcd_rw_pin;
17408c2ecf20Sopenharmony_ci		lcd.pins.cl = lcd_cl_pin;
17418c2ecf20Sopenharmony_ci		lcd.pins.da = lcd_da_pin;
17428c2ecf20Sopenharmony_ci		lcd.pins.bl = lcd_bl_pin;
17438c2ecf20Sopenharmony_ci	}
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_ci	switch (selected_keypad_type) {
17468c2ecf20Sopenharmony_ci	case KEYPAD_TYPE_OLD:
17478c2ecf20Sopenharmony_ci		keypad_profile = old_keypad_profile;
17488c2ecf20Sopenharmony_ci		break;
17498c2ecf20Sopenharmony_ci	case KEYPAD_TYPE_NEW:
17508c2ecf20Sopenharmony_ci		keypad_profile = new_keypad_profile;
17518c2ecf20Sopenharmony_ci		break;
17528c2ecf20Sopenharmony_ci	case KEYPAD_TYPE_NEXCOM:
17538c2ecf20Sopenharmony_ci		keypad_profile = nexcom_keypad_profile;
17548c2ecf20Sopenharmony_ci		break;
17558c2ecf20Sopenharmony_ci	default:
17568c2ecf20Sopenharmony_ci		keypad_profile = NULL;
17578c2ecf20Sopenharmony_ci		break;
17588c2ecf20Sopenharmony_ci	}
17598c2ecf20Sopenharmony_ci
17608c2ecf20Sopenharmony_ci	if (!lcd.enabled && !keypad.enabled) {
17618c2ecf20Sopenharmony_ci		/* no device enabled, let's exit */
17628c2ecf20Sopenharmony_ci		pr_err("panel driver disabled.\n");
17638c2ecf20Sopenharmony_ci		return -ENODEV;
17648c2ecf20Sopenharmony_ci	}
17658c2ecf20Sopenharmony_ci
17668c2ecf20Sopenharmony_ci	err = parport_register_driver(&panel_driver);
17678c2ecf20Sopenharmony_ci	if (err) {
17688c2ecf20Sopenharmony_ci		pr_err("could not register with parport. Aborting.\n");
17698c2ecf20Sopenharmony_ci		return err;
17708c2ecf20Sopenharmony_ci	}
17718c2ecf20Sopenharmony_ci
17728c2ecf20Sopenharmony_ci	if (pprt)
17738c2ecf20Sopenharmony_ci		pr_info("panel driver registered on parport%d (io=0x%lx).\n",
17748c2ecf20Sopenharmony_ci			parport, pprt->port->base);
17758c2ecf20Sopenharmony_ci	else
17768c2ecf20Sopenharmony_ci		pr_info("panel driver not yet registered\n");
17778c2ecf20Sopenharmony_ci	return 0;
17788c2ecf20Sopenharmony_ci}
17798c2ecf20Sopenharmony_ci
17808c2ecf20Sopenharmony_cistatic void __exit panel_cleanup_module(void)
17818c2ecf20Sopenharmony_ci{
17828c2ecf20Sopenharmony_ci	parport_unregister_driver(&panel_driver);
17838c2ecf20Sopenharmony_ci}
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_cimodule_init(panel_init_module);
17868c2ecf20Sopenharmony_cimodule_exit(panel_cleanup_module);
17878c2ecf20Sopenharmony_ciMODULE_AUTHOR("Willy Tarreau");
17888c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci/*
17918c2ecf20Sopenharmony_ci * Local variables:
17928c2ecf20Sopenharmony_ci *  c-indent-level: 4
17938c2ecf20Sopenharmony_ci *  tab-width: 8
17948c2ecf20Sopenharmony_ci * End:
17958c2ecf20Sopenharmony_ci */
1796