sync hh.org
[hh.org.git] / arch / arm / mach-pxa / himalaya / himalaya_kp.c
blob41a2c4f44ed3f6a75474de141d7cf5c038c61304
1 /*
2 * Driver interface to the touchscreen on the HTC Himalaya
4 * Copyright (C) 2004 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
5 * Copyright (C) 2004 w4xy@xanadux.org
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
11 * HAL code based on h5400_asic_io.c, which is
12 * Copyright (C) 2003 Compaq Computer Corporation.
14 * Driver based on the h1900_ts.c, which is
15 * Copyright (C) 2003, Joshua Wise <joshua at joshuawise.com>
19 #include <linux/module.h>
20 #include <linux/version.h>
21 #include <linux/config.h>
23 #include <linux/init.h>
24 #include <linux/fs.h>
25 #include <linux/interrupt.h>
26 #include <linux/sched.h>
27 #include <linux/pm.h>
28 #include <linux/sysctl.h>
29 #include <linux/proc_fs.h>
30 #include <linux/delay.h>
31 #include <linux/input.h>
33 #if 0
34 #include <asm/arch/hardware.h>
35 //#include <asm/arch-sa1100/h3600.h>
36 //#include <asm/arch-sa1100/SA-1100.h>
38 #include <asm/mach/irq.h>
39 /*#include <asm/arch-pxa/himalaya-gpio.h>*/
40 #include <asm/hardware/ipaq-asic3.h>
41 #endif
43 #include <asm/irq.h>
44 #include <asm/arch/hardware.h>
46 #include <asm/mach-types.h>
47 #include <asm/mach/irq.h>
48 #include <asm/mach/arch.h>
49 #include <asm/mach/map.h>
50 #include <asm/arch/udc.h>
52 #include <asm/arch/himalaya-gpio.h>
53 #include <asm/hardware/ipaq-asic3.h>
54 #include <asm/arch-pxa/h3900-asic.h>
55 #include <linux/soc/asic3_base.h>
56 #include "tsc2200.h"
57 #include "pxa_nssp.h"
58 #include <asm/arch/pxa-regs.h>
59 #include <asm/apm.h>
61 static int gp = 74;
62 MODULE_PARM(gp, "i");
63 MODULE_PARM_DESC(gp,"GPIO # (while testing for ASIC3 GPIO)");
65 static char kp_name[] = "tsc2200_kp";
66 static char kp_phys[] = "touchscreen/tsc2200";
68 extern struct platform_device himalaya_asic3;
69 struct input_dev kp_input_dev;
70 static struct work_struct kp_workqueue;
71 static struct work_struct kp_readworkqueue;
72 static int protected;
74 #define SAMPLE_TIMEOUT 8 /* sample every 10ms */
75 #define CONVERSION_TIMEOUT 2 /* wait 1ms for a conversion */
76 #undef UBERDEBUGGY
78 #define GPIO_NR_HIMALAYA_KP_IRQ_N 8
79 #define HIMALAYA_KP_IRQ IRQ_GPIO(GPIO_NR_HIMALAYA_KP_IRQ_N)
81 //static struct timer_list kp_timer;
83 typedef enum
85 TSC_DO_NOTHING = 0,
86 TSC_PEN_DOWN_EXPECT_CONVERT,
87 TSC_PEN_DOWN_CONVERT_DONE,
88 TSC_PEN_UP,
90 } TSC_PEN_STATE;
92 static int tsc_state = TSC_DO_NOTHING;
94 static void kp_tsc2200_start_conv(void)
96 tsc2200_write(TSC2200_CTRLREG_ADC,
97 // TSC2200_CTRLREG_ADC_PSM_TSC2200 |
98 TSC2200_CTRLREG_ADC_AD0 |
99 TSC2200_CTRLREG_ADC_RES (TSC2200_CTRLREG_ADC_RES_12BITP) |
100 TSC2200_CTRLREG_ADC_AVG (TSC2200_CTRLREG_ADC_8AVG) |
101 TSC2200_CTRLREG_ADC_CL (TSC2200_CTRLREG_ADC_CL_2MHZ_12BIT) |
102 TSC2200_CTRLREG_ADC_PV (TSC2200_CTRLREG_ADC_PV_500uS) );
103 printk("%s: %X.\n", __FUNCTION__, tsc2200_read(TSC2200_CTRLREG_ADC));
106 static void kp_read(struct input_dev *dev)
108 unsigned int bytes[4];
110 /* read positions. */
111 bytes[0] = tsc2200_read(TSC2200_DATAREG_X);
112 bytes[1] = tsc2200_read(TSC2200_DATAREG_Y);
113 bytes[2] = tsc2200_read(TSC2200_DATAREG_Z1);
114 bytes[3] = tsc2200_read(TSC2200_DATAREG_Z2);
116 input_report_abs(dev, ABS_X, bytes[0] & 0xfff);
117 input_report_abs(dev, ABS_Y, bytes[1] & 0xfff);
118 input_report_abs(dev, ABS_PRESSURE, 0xfff);
119 // input_report_abs(dev, ABS_PRESSURE, bytes[2] & 0xfff);
120 input_sync(dev);
122 #if 0
123 printk("%s: %03X-%03x-%03x-%03x / %i\n", __FUNCTION__, bytes[0], bytes[1], bytes[2], bytes[3], tsc2200_dav());
124 printk("%s: BAT1: %03X\nBAT2: %03x\nAUX1: %03x\nAUX2: %03x\nTEMP1: %03x\nTEMP2: %03x\n", __FUNCTION__, tsc2200_read(TSC2200_DATAREG_BAT1), tsc2200_read(TSC2200_DATAREG_BAT2), tsc2200_read(TSC2200_DATAREG_AUX1), tsc2200_read(TSC2200_DATAREG_AUX2), tsc2200_read(TSC2200_DATAREG_TEMP1), tsc2200_read(TSC2200_DATAREG_TEMP2));
125 #endif
128 static void kp_up(struct input_dev *dev)
130 input_report_abs(dev, ABS_PRESSURE, 0x0);
131 input_sync(dev);
134 static int kp_pressed(void)
136 return ( GPLR(GPIO_NR_HIMALAYA_KP_IRQ_N) & GPIO_bit(GPIO_NR_HIMALAYA_KP_IRQ_N) ) ? 0 : 1;
139 static irqreturn_t kp_irq(int irq, void* data, struct pt_regs *regs)
141 printk("%s: got interrupt...\n", __FUNCTION__);
142 printk("ASIC3 int status:\n %X\n %X\n %X\n %X\n",
143 asic3_get_gpio_intstatus_a(&himalaya_asic3.dev),
144 asic3_get_gpio_intstatus_b(&himalaya_asic3.dev),
145 asic3_get_gpio_intstatus_c(&himalaya_asic3.dev),
146 asic3_get_gpio_intstatus_d(&himalaya_asic3.dev));
148 asic3_set_gpio_intstatus_c(&himalaya_asic3.dev, 0xfe00, 0x0);
150 asic3_set_gpio_mask_c(&himalaya_asic3.dev, 0xfe00, 0x0);
151 asic3_set_gpio_dir_c(&himalaya_asic3.dev, 0xfe00, 0x0);
152 asic3_set_gpio_trigtype_c(&himalaya_asic3.dev, 0xfe00, 0xfe00);
153 asic3_set_gpio_rising_c(&himalaya_asic3.dev, 0xfe00, 0x0);
154 asic3_set_gpio_triglevel_c(&himalaya_asic3.dev, 0xfe00, 0x0);
155 asic3_set_gpio_sleepmask_c(&himalaya_asic3.dev, 0xfe00, 0x0);
156 // asic3_set_gpio_intstatus_c(&himalaya_asic3.dev, 0xfe00, 0x0800);
157 asic3_set_gpio_alt_fn_c(&himalaya_asic3.dev, 0xfe00, 0x0);
159 // disable_irq(HIMALAYA_KP_IRQ);
160 // schedule_work(&kp_workqueue);
162 return IRQ_HANDLED;
165 static void kp_work(void* data)
167 struct input_dev *dev = (struct input_dev *)data;
169 // printk("%i: workqueue fired...\n", jiffies);
170 if ( !protected ) {
171 down_interruptible(&tsc2200_sem);
172 printk("protected.\n");
173 protected++;
174 // kp_tsc2200_initialise();
175 // printk("initialised.\n");
176 // tsc2200_write(TSC2200_CTRLREG_ADC, 0x8506);
177 kp_tsc2200_start_conv();
179 // printk("0: %X\n", tsc2200_read(TSC2200_CTRLREG_ADC));
180 printk("%s: DAV=%i, pressed=%i.\n", __FUNCTION__, tsc2200_dav(), kp_pressed());
181 // msleep(3);
182 while ( !(tsc2200_read(TSC2200_CTRLREG_ADC) & 0x4000) && !tsc2200_dav()) {
183 // printk("1: %X\n", tsc2200_read(TSC2200_CTRLREG_ADC));
184 msleep(1);
186 #if 0
187 while ( !tsc2200_dav() ) {
188 // printk("2: %X\n", tsc2200_read(TSC2200_CTRLREG_ADC));
190 #endif
191 if ( kp_pressed() ) {
192 // printk("3: %X\n", tsc2200_read(TSC2200_CTRLREG_ADC));
193 schedule_work(&kp_workqueue);
194 kp_read(dev);
195 } else {
196 // printk("4: %X\n", tsc2200_read(TSC2200_CTRLREG_ADC));
197 protected--;
198 kp_up(dev);
199 printk("unprotecting.\n");
200 up(&tsc2200_sem);
201 enable_irq(HIMALAYA_KP_IRQ);
205 int kp_tsc2200_open( struct input_dev *dev )
207 if ( !machine_is_himalaya() ) {
208 printk("%s: unknown iPAQ model %s\n", __FUNCTION__, h3600_generic_name() );
209 return -ENODEV;
212 printk("%s: init work queue...\n", __FUNCTION__);
213 // INIT_WORK(&kp_workqueue, kp_work, dev);
214 // INIT_WORK(&kp_readworkqueue, kp_readwork, dev);
216 printk("%s: setup ASIC3...\n", __FUNCTION__);
217 asic3_set_gpio_mask_c(&himalaya_asic3.dev, 0xfe00, 0x0);
218 asic3_set_gpio_dir_c(&himalaya_asic3.dev, 0xfe00, 0x0);
219 asic3_set_gpio_trigtype_c(&himalaya_asic3.dev, 0xfe00, 0xfe00);
220 asic3_set_gpio_rising_c(&himalaya_asic3.dev, 0xfe00, 0x0);
221 asic3_set_gpio_triglevel_c(&himalaya_asic3.dev, 0xfe00, 0x0);
222 asic3_set_gpio_sleepmask_c(&himalaya_asic3.dev, 0xfe00, 0x0);
223 // asic3_set_gpio_intstatus_c(&himalaya_asic3.dev, 0xfe00, 0x0800);
224 asic3_set_gpio_alt_fn_c(&himalaya_asic3.dev, 0xfe00, 0x0);
226 printk("%s: set irq...\n", __FUNCTION__);
227 /* now set up the pen action GPIO */
229 pxa_gpio_mode(gp | GPIO_IN);
230 set_irq_type(IRQ_GPIO(gp), IRQT_BOTHEDGE);
231 request_irq (IRQ_GPIO(gp), kp_irq, SA_SAMPLE_RANDOM, "himalaya_kp", (void*)&kp_input_dev);
233 udelay(300);
234 printk("%s: %X\n", __FUNCTION__, tsc2200_read(TSC2200_CTRLREG_ADC) );
235 udelay(300);
236 // enable_irq(IRQ_GPIO(gp));
238 return 0;
241 void kp_tsc2200_close( struct input_dev *dev )
243 printk("%s: closing tsc2200.\n", __FUNCTION__);
245 disable_irq(IRQ_GPIO(gp));
247 /* make sure to free the IRQ... */
248 free_irq(IRQ_GPIO(gp), dev);
250 /* now free the timer... */
251 // del_timer_sync (&kp_timer);
254 static void kp_release(struct device *dev)
259 struct device kp_tsc2200_dev = {
260 .parent = &nssp_bus,
261 .bus = &nssp_bus_type,
262 .bus_id = "kp_tsc2200",
263 .release = kp_release,
266 static int kp_setup(struct input_dev *dev)
268 memset(dev, 0x0, sizeof(*dev));
270 init_input_dev(dev);
272 dev->private = dev;
274 dev->name = kp_name;
275 dev->phys = kp_phys;
276 dev->open = kp_tsc2200_open;
277 dev->close = kp_tsc2200_close;
279 set_bit(EV_ABS, dev->evbit);
281 set_bit(ABS_X, dev->absbit);
282 set_bit(ABS_Y, dev->absbit);
283 set_bit(ABS_PRESSURE, dev->absbit);
284 dev->absmin[ABS_PRESSURE] = 0;
285 dev->absmax[ABS_PRESSURE] = 0xfff;
286 dev->absmax[ABS_X] = 0xfff;
287 dev->absmax[ABS_Y] = 0xfff;
288 dev->absfuzz[ABS_X] = 2;
289 dev->absfuzz[ABS_Y] = 2;
290 dev->absflat[ABS_X] = 4;
291 dev->absflat[ABS_Y] = 4;
293 dev->id.bustype = BUS_PXA_NSSP;
294 dev->id.vendor = 0xffff;
295 dev->id.product = 0xffff;
296 dev->id.version = 0xffff;
298 dev->dev = &kp_tsc2200_dev;
300 device_register(dev->dev);
301 input_register_device(dev);
303 return 0;
306 static int __init kp_init(void)
308 kp_setup (&kp_input_dev);
310 return 0;
313 static void __exit kp_exit(void)
315 // cancel_delayed_work(&kp_workqueue);
316 flush_scheduled_work();
317 device_unregister(&kp_tsc2200_dev);
318 input_unregister_device(&kp_input_dev);
321 module_init(kp_init)
322 module_exit(kp_exit)
324 MODULE_AUTHOR("Matthias Burghardt");
325 MODULE_DESCRIPTION("Touchscreen (TI TSC2200) support for the HTC Himalaya");