mirage_ts.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * linux/arch/mips/au1000/db1x00/mirage_ts.c
  3. *
  4. * BRIEF MODULE DESCRIPTION
  5. * Glue between Mirage board-specific touchscreen pieces
  6. * and generic Wolfson Codec touchscreen support.
  7. *
  8. * Based on pb1100_ts.c used in Hydrogen II.
  9. *
  10. * Copyright (c) 2003 Embedded Edge, LLC
  11. * dan@embeddededge.com
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. *
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  24. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <linux/types.h>
  34. #include <linux/module.h>
  35. #include <linux/sched.h>
  36. #include <linux/kernel.h>
  37. #include <linux/init.h>
  38. #include <linux/fs.h>
  39. #include <linux/poll.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/smp.h>
  42. #include <linux/smp_lock.h>
  43. #include <linux/wait.h>
  44. #include <asm/segment.h>
  45. #include <asm/irq.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/delay.h>
  48. #include <asm/au1000.h>
  49. /*
  50. * Imported interface to Wolfson Codec driver.
  51. */
  52. extern void *wm97xx_ts_get_handle(int which);
  53. extern int wm97xx_ts_ready(void* ts_handle);
  54. extern void wm97xx_ts_set_cal(void* ts_handle, int xscale, int xtrans, int yscale, int ytrans);
  55. extern u16 wm97xx_ts_get_ac97(void* ts_handle, u8 reg);
  56. extern void wm97xx_ts_set_ac97(void* ts_handle, u8 reg, u16 val);
  57. extern int wm97xx_ts_read_data(void* ts_handle, long* x, long* y, long* pressure);
  58. extern void wm97xx_ts_send_data(void* ts_handle, long x, long y, long z);
  59. int wm97xx_comodule_present = 1;
  60. #define TS_NAME "mirage_ts"
  61. #define err(format, arg...) printk(KERN_ERR TS_NAME ": " format "\n" , ## arg)
  62. #define info(format, arg...) printk(KERN_INFO TS_NAME ": " format "\n" , ## arg)
  63. #define warn(format, arg...) printk(KERN_WARNING TS_NAME ": " format "\n" , ## arg)
  64. #define DPRINTK(format, arg...) printk("%s: " format "\n", __FUNCTION__ , ## arg)
  65. #define PEN_DOWN_IRQ AU1000_GPIO_7
  66. static struct task_struct *ts_task = 0;
  67. static DECLARE_COMPLETION(ts_complete);
  68. static DECLARE_WAIT_QUEUE_HEAD(pendown_wait);
  69. #ifdef CONFIG_WM97XX_FIVEWIRETS
  70. static int release_pressure = 1;
  71. #else
  72. static int release_pressure = 50;
  73. #endif
  74. typedef struct {
  75. long x;
  76. long y;
  77. } DOWN_EVENT;
  78. #define SAMPLE_RATE 50 /* samples per second */
  79. #define PEN_DEBOUNCE 5 /* samples for settling - fn of SAMPLE_RATE */
  80. #define PEN_UP_TIMEOUT 10 /* in seconds */
  81. #define PEN_UP_SETTLE 5 /* samples per second */
  82. static struct {
  83. int xscale;
  84. int xtrans;
  85. int yscale;
  86. int ytrans;
  87. } mirage_ts_cal =
  88. {
  89. #if 0
  90. .xscale = 84,
  91. .xtrans = -157,
  92. .yscale = 66,
  93. .ytrans = -150,
  94. #else
  95. .xscale = 84,
  96. .xtrans = -150,
  97. .yscale = 66,
  98. .ytrans = -146,
  99. #endif
  100. };
  101. static void pendown_irq(int irqnr, void *devid, struct pt_regs *regs)
  102. {
  103. //DPRINTK("got one 0x%x", au_readl(SYS_PINSTATERD));
  104. wake_up(&pendown_wait);
  105. }
  106. static int ts_thread(void *id)
  107. {
  108. static int pen_was_down = 0;
  109. static DOWN_EVENT pen_xy;
  110. long x, y, z;
  111. void *ts; /* handle */
  112. struct task_struct *tsk = current;
  113. int timeout = HZ / SAMPLE_RATE;
  114. ts_task = tsk;
  115. daemonize();
  116. tsk->tty = NULL;
  117. tsk->policy = SCHED_FIFO;
  118. tsk->rt_priority = 1;
  119. strcpy(tsk->comm, "touchscreen");
  120. /* only want to receive SIGKILL */
  121. spin_lock_irq(&tsk->sigmask_lock);
  122. siginitsetinv(&tsk->blocked, sigmask(SIGKILL));
  123. recalc_sigpending(tsk);
  124. spin_unlock_irq(&tsk->sigmask_lock);
  125. /* get handle for codec */
  126. ts = wm97xx_ts_get_handle(0);
  127. /* proceed only after everybody is ready */
  128. wait_event_timeout(pendown_wait, wm97xx_ts_ready(ts), HZ/4);
  129. /* board-specific calibration */
  130. wm97xx_ts_set_cal(ts,
  131. mirage_ts_cal.xscale,
  132. mirage_ts_cal.xtrans,
  133. mirage_ts_cal.yscale,
  134. mirage_ts_cal.ytrans);
  135. /* route Wolfson pendown interrupts to our GPIO */
  136. au_sync();
  137. wm97xx_ts_set_ac97(ts, 0x4c, wm97xx_ts_get_ac97(ts, 0x4c) & ~0x0008);
  138. au_sync();
  139. wm97xx_ts_set_ac97(ts, 0x56, wm97xx_ts_get_ac97(ts, 0x56) & ~0x0008);
  140. au_sync();
  141. wm97xx_ts_set_ac97(ts, 0x52, wm97xx_ts_get_ac97(ts, 0x52) | 0x2008);
  142. au_sync();
  143. for (;;) {
  144. interruptible_sleep_on_timeout(&pendown_wait, timeout);
  145. disable_irq(PEN_DOWN_IRQ);
  146. if (signal_pending(tsk)) {
  147. break;
  148. }
  149. /* read codec */
  150. if (!wm97xx_ts_read_data(ts, &x, &y, &z))
  151. z = 0; /* treat no-data and pen-up the same */
  152. if (signal_pending(tsk)) {
  153. break;
  154. }
  155. if (z >= release_pressure) {
  156. y = ~y; /* top to bottom */
  157. if (pen_was_down > 1 /*&& pen_was_down < PEN_DEBOUNCE*/) {//THXXX
  158. /* bounce ? */
  159. x = pen_xy.x;
  160. y = pen_xy.y;
  161. --pen_was_down;
  162. } else if (pen_was_down <= 1) {
  163. pen_xy.x = x;
  164. pen_xy.y = y;
  165. if (pen_was_down)
  166. wm97xx_ts_send_data(ts, x, y, z);
  167. pen_was_down = PEN_DEBOUNCE;
  168. }
  169. //wm97xx_ts_send_data(ts, x, y, z);
  170. timeout = HZ / SAMPLE_RATE;
  171. } else {
  172. if (pen_was_down) {
  173. if (--pen_was_down)
  174. z = release_pressure;
  175. else //THXXX
  176. wm97xx_ts_send_data(ts, pen_xy.x, pen_xy.y, z);
  177. }
  178. /* The pendown signal takes some time to settle after
  179. * reading the pen pressure so wait a little
  180. * before enabling the pen.
  181. */
  182. if (! pen_was_down) {
  183. // interruptible_sleep_on_timeout(&pendown_wait, HZ / PEN_UP_SETTLE);
  184. timeout = HZ * PEN_UP_TIMEOUT;
  185. }
  186. }
  187. enable_irq(PEN_DOWN_IRQ);
  188. }
  189. enable_irq(PEN_DOWN_IRQ);
  190. ts_task = NULL;
  191. complete(&ts_complete);
  192. return 0;
  193. }
  194. static int __init ts_mirage_init(void)
  195. {
  196. int ret;
  197. /* pen down signal is connected to GPIO 7 */
  198. ret = request_irq(PEN_DOWN_IRQ, pendown_irq, 0, "ts-pendown", NULL);
  199. if (ret) {
  200. err("unable to get pendown irq%d: [%d]", PEN_DOWN_IRQ, ret);
  201. return ret;
  202. }
  203. lock_kernel();
  204. ret = kernel_thread(ts_thread, NULL, CLONE_FS | CLONE_FILES);
  205. if (ret < 0) {
  206. unlock_kernel();
  207. return ret;
  208. }
  209. unlock_kernel();
  210. info("Mirage touchscreen IRQ initialized.");
  211. return 0;
  212. }
  213. static void __exit ts_mirage_exit(void)
  214. {
  215. if (ts_task) {
  216. send_sig(SIGKILL, ts_task, 1);
  217. wait_for_completion(&ts_complete);
  218. }
  219. free_irq(PEN_DOWN_IRQ, NULL);
  220. }
  221. module_init(ts_mirage_init);
  222. module_exit(ts_mirage_exit);