summaryrefslogtreecommitdiffstats
path: root/uClinux-2.4.20-uc1/drivers/char/w90n745_keypad.c
blob: 9f6586255b0b6ed16214a128c295ab2ef72add90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/****************************************************************************
 *                                                                          *
 * Copyright (c) 2004 - 2006 Winbond Electronics Corp. All rights reserved. *
 *                                                                          *
 ****************************************************************************/
 
/****************************************************************************
 * 
 * FILENAME
 *     w90n745_keypad.c
 *
 * VERSION
 *     1.0
 *
 * DESCRIPTION
 *     The Winbond 64-keyboard driver
 *
 * DATA STRUCTURES
 *     None
 *
 * FUNCTIONS
 *     None
 *
 * HISTORY
 *     2005/09/09		 Ver 1.0 Created by PC34 YHan
 *
 * REMARK
 *     None
 *************************************************************************/

#include <linux/init.h>
#include <linux/slab.h>
#include <asm/errno.h>
#include <asm/delay.h>
//#include <asm/fcntl.h>
#include <asm/arch/irqs.h>
#include <linux/mm.h>
#include <linux/poll.h>
#include <linux/module.h>
#include <asm/hardware.h>
#include <asm/io.h>
#include "w90n745_keypad.h"

#define MAJOR_NUM 192

static int volatile kpd_get=0;
static int volatile kpd_block=1;
unsigned char DEV_NAME[10] = "Keypad";

typedef struct _keymap 
{
	short row;
	short col;	
}keymap;

keymap key __attribute__ ((aligned (4)));

static DECLARE_WAIT_QUEUE_HEAD(read_wait_a);

static void read_task_block()
{	
	DECLARE_WAITQUEUE(wait, current);
	add_wait_queue(&read_wait_a, &wait);
	set_current_state(TASK_INTERRUPTIBLE);

	schedule();
		
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&read_wait_a, &wait);
		
	return ;
}

static void read_task_wake_up(void)
{
	wake_up_interruptible(&read_wait_a);
	
	return ;
}

void keypad745_irq(int irq, void *dev_id, struct pt_regs *regs)
{
	
	volatile unsigned int status;	
	kpd_get=1;
	
	key.row = 0;
	key.col = 0;
	
	status = readl(KPISTATUS);
	#ifdef KPI_DEBUG
	printk("KPI ISR KPISTATUS=0x%08x\n",status);
	#endif
    if(status&0x00210000)
	{		
	    key.row = (status&0x00000078)>>3;
	    key.col = (status&0x00000007);		    
	}
	
	if(kpd_block)
		read_task_wake_up();
	
	return;
	
}

int keypad745_open(struct inode* i,struct file* f)
{
	int result;
	int irq;
	int old_cfg;
	kpd_block=1;

	if(f->f_flags & 0x800)	//0x800:04000
		kpd_block=0;
	
	
	irq = INT_KEYPAD;
	
	MOD_INC_USE_COUNT;
		
	result = request_irq(irq,keypad745_irq,0,DEV_NAME,NULL);
	if(result!=0)
		printk("register the keypad_irq failed!\n");
	
	old_cfg=readl(GPIO_CFG);
	old_cfg=old_cfg&GPIO_CFG_MASK;
	old_cfg=old_cfg|GPIO_CFG_VALUE;
	writel(old_cfg,GPIO_CFG);
    
    #ifdef KPI_DEBUG
    printk("KPI OPEN:\nKPICONF=0x%08x\nGPIO_CFG=0x%08x\n",readl(KPICONF),readl(GPIO_CFG));
    #endif

	return 0;
}

int keypad745_close(struct inode* i,struct file* f)
{
	MOD_DEC_USE_COUNT;
	free_irq(INT_KEYPAD,NULL);
	
	return 0;
}


ssize_t keypad745_read(struct file *filp, char *buff, size_t read_mode, loff_t *offp)
{
	kpd_block = read_mode ;
	
	if(kpd_block)
	{
		read_task_block();
	}
	
	if(kpd_get)
	{
		kpd_get=0;
		copy_to_user(buff,(char*)&key,sizeof(keymap));
		key.row = 0;
		key.col = 0;
		return 1;
	}
	else
		return -1;	
}

static int keypad745_ioctl(struct inode *inode, struct file *flip, 
													 		unsigned int cmd, unsigned long arg)
{
	int err = 0;
	
	if(_IOC_TYPE(cmd) != KEYPAD_MAGIC) return -ENOTTY;
	if(_IOC_NR(cmd) > KEYPAD_MAXNR) return -ENOTTY;

	if(_IOC_DIR(cmd) & _IOC_READ)
		err = !access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd));
	else if(_IOC_DIR(cmd) & _IOC_WRITE)
		err = !access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd));
		
	if(err) 
		return -EFAULT;
	
	switch (cmd)
	{
		case KPD_BLOCK:	
			kpd_block=1;
			flip->f_flags &= ~0x800;
			break;
		case KPD_NONBLOCK:	
			kpd_block=0;
			flip->f_flags |= ~0x800;
			
			break;
		default:
			break;
	}	
	
	return 0;		
}

struct file_operations keypad745_fops = 
{
	owner: THIS_MODULE,
	open: keypad745_open,
	read: keypad745_read,
	ioctl:keypad745_ioctl,
	release: keypad745_close,
};

static int __init keypad_745_reg(void)
{		
	int result;
	int old_cfg;
	result = register_chrdev(MAJOR_NUM,DEV_NAME,&keypad745_fops);
	if(result<0)
	{
		printk("initial the device error!\n");
		return (result);	
	}	
	
	old_cfg=readl(GPIO_CFG);
	old_cfg=old_cfg&GPIO_CFG_MASK;
	old_cfg=old_cfg|GPIO_CFG_VALUE;
	writel(old_cfg,GPIO_CFG);
	
	writel(0,KPICONF);
	writel(0,KPI3KCONF);
	writel(0,KPISTATUS);	
#ifdef __WB_EVB__
	old_cfg=readl(GPIO_DIR);
	old_cfg |= 0x3FF0000;
	writel(old_cfg,GPIO_DIR);
#endif	
	writel(KPICONF_VALUE,KPICONF);	
	
	init_waitqueue_head(&read_wait_a);
	
	printk("W90N745 Keypad initialized successful\n");
	
	return (result);
	
}

static void keypad_745_exit(void)
{
	unregister_chrdev(MAJOR_NUM,DEV_NAME);
	
	return;
}

module_init(keypad_745_reg);
module_exit(keypad_745_exit);