1 /* Touch screen driver for the TI something-or-other
3 * Copyright © 2005 SDG Systems, LLC
5 * Based on code that was based on the SAMCOP driver.
6 * Copyright © 2003, 2004 Compaq Computer Corporation.
8 * Use consistent with the GNU GPL is permitted,
9 * provided that this copyright notice is
10 * preserved in its entirety in all copies and derived works.
12 * COMPAQ COMPUTER CORPORATION MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
13 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
14 * FITNESS FOR ANY PARTICULAR PURPOSE.
16 * Author: Keith Packard <keith.packard@hp.com>
21 * 2004-02-11 Michael Opdenacker Renamed names from samcop to shamcop,
22 * Goal:support HAMCOP and SAMCOP.
23 * 2004-02-14 Michael Opdenacker Temporary fix for device id handling
25 * 2005-02-18 Aric Blumer Converted basic structure to support hx4700
27 * 2005-06-07 Aric Blumer Added tssim device handling so we can
28 * hook in the fbvncserver.
31 #include <linux/module.h>
32 #include <linux/version.h>
34 #include <linux/init.h>
36 #include <linux/cdev.h>
37 #include <linux/interrupt.h>
38 #include <linux/irq.h>
39 #include <linux/sched.h>
41 #include <linux/delay.h>
42 #include <linux/input.h>
43 #include <linux/platform_device.h>
44 //#include <linux/battery.h>
46 #include <asm/arch/hardware.h>
49 #include <asm/arch/pxa-regs.h>
50 #include <asm/arch/magician.h>
52 enum touchscreen_state
{
53 STATE_WAIT_FOR_TOUCH
, /* Waiting for a PEN interrupt */
54 STATE_SAMPLING_X
, /* Actively sampling ADC */
55 STATE_SAMPLING_Y
, /* Actively sampling ADC */
56 STATE_SAMPLING_Z
, /* Actively sampling ADC */
59 struct touchscreen_data
{
60 enum touchscreen_state state
;
61 struct timer_list timer
;
63 struct input_dev
*input
;
66 static unsigned long poll_sample_time
= 10; /* Sample every 10 milliseconds */
68 static struct touchscreen_data
*ts_data
;
72 module_param(poll_sample_time
, ulong
, 0644);
73 MODULE_PARM_DESC(poll_sample_time
, "Poll sample time");
76 report_touchpanel(struct touchscreen_data
*ts
, int pressure
, int x
, int y
)
78 input_report_abs(ts
->input
, ABS_PRESSURE
, pressure
);
79 input_report_abs(ts
->input
, ABS_X
, x
);
80 input_report_abs(ts
->input
, ABS_Y
, y
);
81 input_sync(ts
->input
);
84 static struct work_struct serial_work
;
86 static irqreturn_t
pen_isr(int irq
, void *irq_desc
)
88 /* struct touchscreen_data *ts = dev_id->data; */
89 struct touchscreen_data
*ts
= ts_data
;
91 if (irq
== MAGICIAN_IRQ(TOUCHPANEL_IRQ_N
)) {
93 if (ts
->state
== STATE_WAIT_FOR_TOUCH
) {
95 * There is ground bounce or noise or something going on here:
96 * when you write to the SSP port to get the X and Y values, it
97 * causes a TOUCHPANEL_IRQ_N interrupt to occur. So if that
98 * happens, we can check to make sure the pen is actually down and
99 * disregard the interrupt if it's not.
101 if (GET_MAGICIAN_GPIO(TOUCHPANEL_IRQ_N
) == 0) {
103 * Disable the pen interrupt. It's reenabled when the user lifts the
106 disable_irq(MAGICIAN_IRQ(TOUCHPANEL_IRQ_N
));
108 ts
->state
= STATE_SAMPLING_X
;
109 schedule_work(&serial_work
);
112 /* Shouldn't happen */
113 printk(KERN_ERR
"Unexpected ts interrupt\n");
120 static void ssp_init(void)
123 pxa_set_cken(CKEN3_SSP2
, 1);
125 /* *** Set up the SPI Registers *** */
126 SSCR0_P2
= (1 << 20) /* Extended Data Size Select */
127 |(6 << 8) /* Serial Clock Rate */
128 |(0 << 7) /* Synchronous Serial Enable (Disable for now) */
129 |(0 << 4) /* Motorola SPI Interface */
130 |(7 << 0) /* Data Size Select (24-bit) */
135 /* Clear the Status */
136 SSSR_P2
= SSSR_P2
& 0x00fcfffc;
139 SSCR0_P2
= (1 << 20) /* Extended Data Size Select */
140 |(6 << 8) /* Serial Clock Rate */
141 |(1 << 7) /* Synchronous Serial Enable */
142 |(0 << 4) /* Motorola SPI Interface */
143 |(7 << 0) /* Data Size Select (24-bit) */
145 /* enable_irq(MAGICIAN_IRQ(TOUCHPANEL_IRQ_N)); */
148 DECLARE_MUTEX(serial_mutex
);
150 static void start_read(void *in
)
152 struct touchscreen_data
*touch
= in
;
153 unsigned long inc
= (poll_sample_time
* HZ
) / 1000;
155 int command
, samples
;
159 /* Write here to the serial port.
160 * Then we have to wait for poll_sample_time before we read out the serial
161 * port. Then, when we read it out, we check to see if the pen is still
162 * down. If so, then we issue another request here.
165 switch (touch
->state
) {
166 case STATE_SAMPLING_X
:
170 case STATE_SAMPLING_Y
:
174 case STATE_SAMPLING_Z
:
179 for (i
= 0; i
< (samples
- 1); i
++) {
180 while (!(SSSR_P2
& (1 << 2))) ;
181 /* It's not full. Write the command for X/Y/Z */
184 while (!(SSSR_P2
& (1 << 2))) ;
185 /* Write the command for X/Y/Z, turn off ADC */
186 SSDR_P2
= command
& ~(0x10000);
189 * Enable the timer. We should get an interrupt, but we want keep a timer
190 * to ensure that we can detect missing data
192 mod_timer(&touch
->timer
, jiffies
+ inc
);
195 static int do_delta_calc(int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
197 /* This is based on Jamey Hicks' description on IRC. */
198 int dx2_a
, dy2_a
, dx2_b
, dy2_b
;
201 dx2_a
= dx2_a
* dx2_a
; /* If dx2_a was negative, it's not now */
203 dy2_a
= dy2_a
* dy2_a
; /* If dy2_a was negative, it's not now */
206 dx2_b
= dx2_b
* dx2_b
; /* If dx2_b was negative, it's not now */
208 dy2_b
= dy2_b
* dy2_b
; /* If dy2_b was negative, it's not now */
211 /* This was described in the algorithm by Jamey, but it doesn't do much
214 if (dx2_a
+ dy2_a
< dx2_b
+ dy2_b
)
218 /* dx2_a + dy2_a is the distance squared */
219 if (((dx2_a
+ dy2_a
) > 8000)
220 || ((dx2_b
+ dy2_b
) > 8000)
227 if ((dx2_b
+ dy2_b
) > 5000) {
234 static void ts_timer_callback(unsigned long data
)
236 struct touchscreen_data
*ts
= (struct touchscreen_data
*)data
;
242 case STATE_SAMPLING_X
:
245 case STATE_SAMPLING_Y
:
248 case STATE_SAMPLING_Z
:
253 * Check here to see if there is anything in the SPI FIFO. If so,
254 * return it if there has been a change. If not, then we have a
255 * timeout. Generate an erro somehow.
258 if (ssrval
& (1 << 3)) { /* Look at Rx Not Empty bit */
259 int number_of_entries_in_fifo
;
261 /* The FIFO is not emtpy. Good! Now make sure there are at least two
264 number_of_entries_in_fifo
= ((ssrval
>> 12) & 0xf) + 1;
266 if (number_of_entries_in_fifo
< samples
) {
267 /* Not ready yet. Come back later. */
268 unsigned long inc
= (poll_sample_time
* HZ
) / 1000;
269 mod_timer(&ts
->timer
, jiffies
+ inc
);
273 if (number_of_entries_in_fifo
== samples
) {
275 int X
[14], Y
[12], Z2
;
278 case STATE_SAMPLING_X
:
279 for (i
= 0; i
< 14; i
++)
280 X
[13 - i
] = SSDR_P2
& 0xffff;
281 x
= (2 * X
[0] + X
[1] + X
[2]) / 4;
282 ts
->state
= STATE_SAMPLING_Y
;
284 case STATE_SAMPLING_Y
:
285 for (i
= 0; i
< 12; i
++)
286 Y
[11 - i
] = SSDR_P2
& 0xffff;
287 y
= (2 * Y
[0] + Y
[1] + Y
[2]) / 4;
288 ts
->state
= STATE_SAMPLING_Z
;
290 case STATE_SAMPLING_Z
:
291 Z2
= SSDR_P2
& 0xffff;
292 ts
->state
= STATE_SAMPLING_X
;
303 if (do_delta_calc(X
[0], Y
[0], X
[1], Y
[1], X
[2], Y
[2])) {
306 if (do_delta_calc(X
[1], Y
[1], X
[2], Y
[2], X
[3], Y
[3])) {
309 if (do_delta_calc(X
[2], Y
[2], X
[3], Y
[3], X
[4], Y
[4])) {
314 /* Take average of point 0 and point 3 */
315 X
[2] = (X
[1] + X
[3]) / 2;
316 Y
[2] = (Y
[1] + Y
[3]) / 2;
320 /* Just ignore this one */
325 /* keep this sample */
326 x
= (X
[1] + (2 * X
[2]) + X
[3]) / 4;
327 y
= (Y
[1] + (2 * Y
[2]) + Y
[3]) / 4;
331 X
[1] = (X
[0] + X
[2]) / 2;
332 Y
[1] = (Y
[0] + Y
[2]) / 2;
337 x
= (X
[0] + (4 * X
[1]) + (6 * X
[2]) +
338 (4 * X
[3]) + X
[4]) >> 4;
339 y
= (Y
[0] + (4 * Y
[1]) + (6 * Y
[2]) +
340 (4 * Y
[3]) + Y
[4]) >> 4;
345 if (GET_MAGICIAN_GPIO(TOUCHPANEL_IRQ_N
) == 0) {
348 report_touchpanel(ts
, 1, x
, y
);
353 report_touchpanel(ts
, 0, 0, 0);
354 ts
->state
= STATE_WAIT_FOR_TOUCH
;
355 /* Re-enable pen down interrupt */
356 enable_irq(MAGICIAN_IRQ(TOUCHPANEL_IRQ_N
));
360 /* We have an error! Too many entries. */
361 printk(KERN_ERR
"TS: Expected %d entries. Got %d\n", 2,
362 number_of_entries_in_fifo
);
363 /* Try to clear the FIFO */
364 while (number_of_entries_in_fifo
--) {
368 if (GET_MAGICIAN_GPIO(TOUCHPANEL_IRQ_N
) == 0) {
374 /* Not ready yet. Come back later. */
375 unsigned long inc
= (poll_sample_time
* HZ
) / 1000;
376 printk("wait some more\n");
377 mod_timer(&ts
->timer
, jiffies
+ inc
);
382 static int ts_probe(struct platform_device
*dev
)
385 struct touchscreen_data
*ts
;
389 ts
= ts_data
= kmalloc(sizeof(*ts
), GFP_KERNEL
);
391 printk(KERN_NOTICE
"magician_ts: unable to allocate memory\n");
394 memset(ts
, 0, sizeof(*ts
));
396 /* *** Set up the input subsystem stuff *** */
397 // memset(ts->input, 0, sizeof(struct input_dev));
398 ts
->input
= input_allocate_device();
399 if (ts
->input
== NULL
) {
401 "magician_ts: unable to allocation touchscreen input\n");
405 ts
->input
->evbit
[0] = BIT(EV_ABS
);
406 ts
->input
->absbit
[0] = BIT(ABS_X
) | BIT(ABS_Y
) | BIT(ABS_PRESSURE
);
407 ts
->input
->absmin
[ABS_X
] = 0;
408 ts
->input
->absmax
[ABS_X
] = 32767;
409 ts
->input
->absmin
[ABS_Y
] = 0;
410 ts
->input
->absmax
[ABS_Y
] = 32767;
411 ts
->input
->absmin
[ABS_PRESSURE
] = 0;
412 ts
->input
->absmax
[ABS_PRESSURE
] = 1;
414 ts
->input
->name
= "magician_ts";
415 ts
->input
->private = ts
;
417 input_register_device(ts
->input
);
419 ts
->timer
.function
= ts_timer_callback
;
420 ts
->timer
.data
= (unsigned long)ts
;
421 ts
->state
= STATE_WAIT_FOR_TOUCH
;
422 init_timer(&ts
->timer
);
424 INIT_WORK(&serial_work
, start_read
, ts
);
426 platform_set_drvdata(dev
, ts
);
428 /* *** Initialize the SSP interface *** */
432 /* Make sure the device is in such a state that it can give pen
434 while (!(SSSR_P2
& (1 << 2))) ;
437 for (retval
= 0; retval
< 100; retval
++) {
438 if (SSSR_P2
& (1 << 3)) {
439 while (SSSR_P2
& (1 << 3)) {
448 ts
->irq
= MAGICIAN_IRQ(TOUCHPANEL_IRQ_N
);
449 retval
= request_irq(ts
->irq
, pen_isr
, SA_INTERRUPT
, "magician_ts", ts
);
451 printk("Unable to get interrupt\n");
452 input_unregister_device(ts
->input
);
455 set_irq_type(ts
->irq
, IRQT_FALLING
);
460 static int ts_remove(struct platform_device
*dev
)
463 struct touchscreen_data
*ts
= platform_get_drvdata(dev
);
465 input_unregister_device(ts
->input
);
466 del_timer_sync(&ts
->timer
);
467 free_irq(ts
->irq
, ts
);
470 pxa_set_cken(CKEN3_SSP2
, 0);
474 static int ts_suspend(struct platform_device
*dev
, pm_message_t state
)
476 disable_irq(MAGICIAN_IRQ(TOUCHPANEL_IRQ_N
));
480 static int ts_resume(struct platform_device
*dev
)
482 struct touchscreen_data
*ts
= platform_get_drvdata(dev
);
484 ts
->state
= STATE_WAIT_FOR_TOUCH
;
486 enable_irq(MAGICIAN_IRQ(TOUCHPANEL_IRQ_N
));
491 static struct platform_driver ts_driver
= {
494 .suspend
= ts_suspend
,
497 .name
= "magician-ts",
501 static int tssim_init(void);
503 static int ts_module_init(void)
505 printk(KERN_NOTICE
"HTC Magician Touch Screen Driver\n");
507 printk(KERN_NOTICE
" TS Simulator Not Installed\n");
509 printk(KERN_NOTICE
" TS Simulator Installed\n");
511 return platform_driver_register(&ts_driver
);
514 static void tssim_exit(void);
516 static void ts_module_cleanup(void)
519 platform_driver_unregister(&ts_driver
);
522 /************* Code for Touch Screen Simulation for FBVNC Server **********/
524 static struct cdev
*cdev
;
526 static long a0
= -1122, a2
= 33588528, a4
= 1452, a5
= -2970720, a6
= 65536;
528 /* The input into the input subsystem is prior to correction from calibration.
529 * So we have to undo the effects of the calibration. It's actually a
530 * complicated equation where the calibrated value of X depends on the
531 * uncalibrated values of X and Y. Fortunately, at least on the magician, the
532 * multiplier for the Y value is zero, so I assume that here. It is a shame
533 * that the tslib does not allow multiple inputs. Then we could do another
534 * driver for this (as it was originally) that give input that does not
535 * require calibration.
538 tssim_ioctl(struct inode
*inode
, struct file
*fp
, unsigned int ioctlnum
,
561 "a0 = %ld, a2 = %ld, a4 = %ld, a5 = %ld, a6 = %ld\n", a0
,
570 static int tssim_open(struct inode
*inode
, struct file
*fp
)
572 /* Nothing to do here */
577 tssim_write(struct file
*fp
, const char __user
* data
, size_t bytes
,
580 unsigned long pressure
;
584 x
= data
[0] | (data
[1] << 8) | (data
[2] << 16) | (data
[3] << 24);
586 y
= data
[0] | (data
[1] << 8) | (data
[2] << 16) | (data
[3] << 24);
588 pressure
= data
[0] | (data
[1] << 8) | (data
[2] << 16) | (data
[3] << 24);
591 input_report_abs(ts_data
->input
, ABS_PRESSURE
, pressure
? 1 : 0);
592 input_report_abs(ts_data
->input
, ABS_X
, ((x
* a6
) - a2
) / a0
);
593 input_report_abs(ts_data
->input
, ABS_Y
, ((y
* a6
) - a5
) / a4
);
594 input_sync(ts_data
->input
);
599 int tssim_close(struct inode
*inode
, struct file
*fp
)
604 struct file_operations fops
= {
606 .write
= tssim_write
,
608 .release
= tssim_close
,
609 .ioctl
= tssim_ioctl
,
613 static int battery_class
;
615 static int get_min_voltage(struct battery
*b
)
619 static int get_max_voltage(struct battery
*b
)
621 return 1400; /* mV */
623 static int get_max_charge(struct battery
*b
)
627 static int get_voltage(struct battery
*b
)
629 static int battery_sample
;
631 if (!down_interruptible(&serial_mutex
)) {
635 while (!(SSSR_P2
& (1 << 2))) ;
637 while (!(SSSR_P2
& (1 << 2))) ;
639 while (!(SSSR_P2
& (1 << 2))) ;
640 SSDR_P2
= 0xd00000; /* Dummy command to allow pen interrupts again */
642 for (i
= 0; i
< 10; i
++) {
644 if (ssrval
& (1 << 3)) { /* Look at Rx Not Empty bit */
645 int number_of_entries_in_fifo
;
647 number_of_entries_in_fifo
=
648 ((ssrval
>> 12) & 0xf) + 1;
649 if (number_of_entries_in_fifo
== 3) {
658 battery_sample
= SSDR_P2
& 0xfff;
661 /* Make sure the FIFO is empty */
662 while (SSSR_P2
& (1 << 3)) {
670 return battery_sample
;
672 static int get_charge(struct battery
*b
)
675 static int battery_sample
;
676 static int temperature_sample
;
677 static int temperature_sample2
;
679 if (!down_interruptible(&serial_mutex
)) {
683 while (!(SSSR_P2
& (1 << 2))) ;
684 SSDR_P2
= 0xa70000; // e is AUX 1(110), a is BAT 1(010)
685 while (!(SSSR_P2
& (1 << 2))) ;
687 while (!(SSSR_P2
& (1 << 2))) ;
688 SSDR_P2
= 0xd00000; /* Dummy command to allow pen interrupts again */
690 for (i
= 0; i
< 10; i
++) {
692 if (ssrval
& (1 << 3)) { /* Look at Rx Not Empty bit */
693 int number_of_entries_in_fifo
;
695 number_of_entries_in_fifo
=
696 ((ssrval
>> 12) & 0xf) + 1;
697 if (number_of_entries_in_fifo
== 3) {
706 battery_sample
= SSDR_P2
& 0xfff;
709 /* Make sure the FIFO is empty */
710 while (SSSR_P2
& (1 << 3)) {
718 printk("VBAT: %d\n", battery_sample
);
720 if (!down_interruptible(&serial_mutex
)) {
724 while (!(SSSR_P2
& (1 << 2))) ;
725 SSDR_P2
= 0x870000; // e is AUX 1(110), 8 is TEMP0 1(000)
726 while (!(SSSR_P2
& (1 << 2))) ;
728 while (!(SSSR_P2
& (1 << 2))) ;
729 SSDR_P2
= 0xd00000; /* Dummy command to allow pen interrupts again */
731 for (i
= 0; i
< 10; i
++) {
733 if (ssrval
& (1 << 3)) { /* Look at Rx Not Empty bit */
734 int number_of_entries_in_fifo
;
736 number_of_entries_in_fifo
=
737 ((ssrval
>> 12) & 0xf) + 1;
738 if (number_of_entries_in_fifo
== 3) {
747 temperature_sample
= SSDR_P2
& 0xfff;
750 /* Make sure the FIFO is empty */
751 while (SSSR_P2
& (1 << 3)) {
754 temperature_sample
= -1;
759 printk("TEMP0: %d\n", temperature_sample
);
761 if (!down_interruptible(&serial_mutex
)) {
765 while (!(SSSR_P2
& (1 << 2))) ;
766 SSDR_P2
= 0x870000; // e is AUX 1(110), 8 is TEMP0 1(000)
767 while (!(SSSR_P2
& (1 << 2))) ;
769 while (!(SSSR_P2
& (1 << 2))) ;
770 SSDR_P2
= 0xd00000; /* Dummy command to allow pen interrupts again */
772 for (i
= 0; i
< 10; i
++) {
774 if (ssrval
& (1 << 3)) { /* Look at Rx Not Empty bit */
775 int number_of_entries_in_fifo
;
777 number_of_entries_in_fifo
=
778 ((ssrval
>> 12) & 0xf) + 1;
779 if (number_of_entries_in_fifo
== 3) {
788 temperature_sample
= SSDR_P2
& 0xfff;
791 /* Make sure the FIFO is empty */
792 while (SSSR_P2
& (1 << 3)) {
795 temperature_sample
= -1;
800 printk("TEMP1: %d\n", temperature_sample2
);
804 static int get_status(struct battery
*b
)
809 static struct battery magician_power
= {
810 .name
= "magician_backup",
812 .get_min_voltage
= get_min_voltage
,
813 .get_min_current
= 0,
815 .get_max_voltage
= get_max_voltage
,
816 .get_max_current
= 0,
817 .get_max_charge
= get_max_charge
,
819 .get_voltage
= get_voltage
,
821 .get_charge
= get_charge
,
822 .get_status
= get_status
,
826 battery_class_uevent(struct class_device
*dev
, char **envp
, int num_envp
,
827 char *buffer
, int buffer_size
)
832 static void battery_class_release(struct class_device
*dev
)
836 static void battery_class_class_release(struct class *class)
841 static int tssim_init(void)
845 retval
= alloc_chrdev_region(&dev
, 0, 1, "tssim");
847 printk(KERN_ERR
"TSSIM Unable to allocate device numbers\n");
852 cdev
->owner
= THIS_MODULE
;
854 retval
= cdev_add(cdev
, dev
, 1);
856 printk(KERN_ERR
"Unable to add cdev\n");
857 unregister_chrdev_region(dev
, 1);
863 if (battery_class_register(&magician_power
)) {
865 "magician_ts: Could not register battery class\n");
868 magician_power
.class_dev
.class->uevent
= battery_class_uevent
;
869 magician_power
.class_dev
.class->release
= battery_class_release
;
870 magician_power
.class_dev
.class->class_release
=
871 battery_class_class_release
;
878 static void tssim_exit(void)
881 unregister_chrdev_region(dev
, 1);
884 battery_class_unregister(&magician_power
);
890 module_init(ts_module_init
);
891 module_exit(ts_module_cleanup
);
893 MODULE_LICENSE("GPL");
894 MODULE_AUTHOR("Aric Blumer, SDG Systems, LLC");
895 MODULE_DESCRIPTION("HTC Magician Touch Screen Driver");