]> rtime.felk.cvut.cz Git - mx1ts.git/blobdiff - mx1_ts-driver.c
change to litle more universal - ASP_TOUCH_IRQ, ASP_PENDATA_IRQ, ASP_BASE_ADDR are...
[mx1ts.git] / mx1_ts-driver.c
index d4e9f3d3816a649b257658ffee27e8e2df72728e..8a65d4212e3c7c9b27bf40c073b13051e79d5e8f 100644 (file)
@@ -1,55 +1,32 @@
 /*
- *  MX1 touchscreen driver
+ * MX1 touchscreen driver
  *
- *  Author:    Nicolas Pitre
- *  Created:   September 25, 2006
- *  Copyright: MontaVista Software, Inc.
+ * Copyright (C) 2008
+ * Radek Pupak (pupakr1@fel.cvut.cz)
+ * Czech Technical University, FEE, DCE
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  *
- * This code is heavily based on ucb1x00-*.c copyrighted by Russell King
- * covering the UCB1100, UCB1200 and UCB1300..  Support for the UCB1400 has
- * been made separate from ucb1x00-core/ucb1x00-ts on Russell's request.
+ * This code is heavily based on ucb*.c copyrighted by Russell King
+ * and Nicolas Pitre covering the UCB1100, UCB1200, UCB1300 and UCB1400.
+ *
+ * Hardware specific properties are based on mx1ts.h and mx1ts.c copyrated
+ * by Blue Mug, Inc. for Motorola, Inc.
+ *
  */
 
+
 #include <linux/module.h>
-#include <linux/moduleparam.h>
-#include <linux/init.h>
-#include <linux/completion.h>
 #include <linux/delay.h>
 #include <linux/input.h>
-#include <linux/device.h>
 #include <linux/interrupt.h>
-#include <linux/suspend.h>
-#include <linux/slab.h>
-#include <linux/kthread.h>
-#include <linux/freezer.h>
-#include <linux/io.h>
-
 #include <sound/driver.h>
-#include <sound/core.h>
-#include <sound/ac97_codec.h>
-
-#include <linux/gameport.h>            //bonus XXX
-
+#include <asm/io.h>
 #include <linux/platform_device.h>
 
 
-
-/*
- * originaly from
- * linux/drivers/misc/mx1ts.h
- *
- * Copyright (C) 2003 Blue Mug, Inc. for Motorola, Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- */
-
 /* Interrupt numbers */
 #define ASP_COMPARE_IRQ         5
 #define ASP_PENDATA_IRQ         33
@@ -57,6 +34,7 @@
 
 /* Analog signal processor (ASP) control registers */
 #define ASP_BASE_ADDR          0x00215000
+#define ASP_BASE_ADDR_LEN      0x38
 
 #define ASP_ACNTLCR            (0x10)     /* Control register */
 #define ASP_PSMPLRG            (0x14)     /* Pen A/D sampe rate control */
 
 struct mx1ts {
        struct input_dev        *ts_idev;
-       struct resource         pamet;
-       int                     irq;
+       /*struct resource       pamet;*/
+       unsigned int            irq_touch;
+       unsigned int            irq_pen_data;
 
        wait_queue_head_t       ts_wait;
        struct task_struct      *ts_task;
 
-       unsigned int            irq_pending;    
-       unsigned int            ts_restart:1;
-       unsigned int            adcsync:1;
-       void __iomem*           mx1ts_mem;
-       
+       void __iomem*           mx1ts_mem;      /* remaped memmory */
+       unsigned int            baseaddr;
+       unsigned int            baseaddr_len;           
        u16                     x_res;          
        u16                     y_res;
        u16                     x_akt;
        u16                     y_akt;
        u16                     u_akt;
-       unsigned int            stav;
+       u16                     cal_auto_zero;
+       u16                     cal_range_x;
+       u16                     cal_range_y;
+       /*unsigned int          stav; */
        u8                      auto_calibration;
-}; 
+       u8                      is_open;
 
+}; 
 
-struct mx1ts mx1_ts;
 
 static inline void mx1ts_reg_set_mask(struct mx1ts *mts , unsigned int reg, u32 mask)
 {      
        u32 val;
-       val = __raw_readl(mts->mx1ts_mem+reg);
+       val = __raw_readl(mts->mx1ts_mem+reg);   /* TODO zvazit pouziti ioread32 */
        val |= mask;
        __raw_writel(val, mts->mx1ts_mem+reg );
 }
@@ -196,17 +176,15 @@ static inline void mx1ts_reg_clear_mask(struct mx1ts *mts , unsigned int reg, u3
 static inline void mx1ts_reg_write(struct mx1ts *mts, unsigned int reg, unsigned int val)
 {      
        __raw_writel(val, mts->mx1ts_mem+reg);
-       printk("<1> mx1_touchscreen: structura %p\n", mts);
-       printk("mx1_touchscreen: zapisuji do : %p  hodnotu %x\n",mts->mx1ts_mem+reg, val);
+       /*printk(KERN_DEBUG "mx1_touchscreen: writing into : %p value %x\n",mts->mx1ts_mem+reg, val);*/
        
 }
 
 static inline unsigned int mx1ts_reg_read(struct mx1ts *mts, unsigned int reg)
 {
        unsigned int out;
-
        out =  __raw_readl( mts->mx1ts_mem + reg );     
-       /*printk("mx1_touchscreen: ctu z %p : %d \n",mts->mx1ts_mem + reg, out);*/
+       /*printk(KERN_DEBUG "mx1_touchscreen: reading from %p : %d \n",mts->mx1ts_mem + reg, out);*/
                
        return out;
 }
@@ -219,10 +197,9 @@ static inline void mx1ts_flush_fifo(struct mx1ts *mts)
                        mx1ts_reg_read(mts, ASP_PADFIFO);
 }
 
-static void mx1_ts_enable_auto_sample(struct mx1ts *mts)
+static void mx1ts_enable_auto_sample(struct mx1ts *mts)
 {
        unsigned int value;
-
        mx1ts_flush_fifo(mts);
 
        value = mx1ts_reg_read(mts, ASP_ACNTLCR);
@@ -279,8 +256,10 @@ static void mx1ts_enable_pen_touch_interrupt(struct mx1ts *mts)
 
        /* Enable pen touch interrupt. */
        value = mx1ts_reg_read(mts, ASP_ICNTLR);
-       value |= ASP_EDGE | ASP_PIRQE;
+       value |= ASP_EDGE | /*ASP_POL |*/ ASP_PIRQE;
        mx1ts_reg_write(mts, ASP_ICNTLR, value);
+       
+       printk(KERN_DEBUG "mx1_touchscreen: pen_touch: activating pen_down interrupt \n");
 }
 
 static void mx1ts_disable_pen_touch_interrupt(struct mx1ts *mts)
@@ -291,16 +270,17 @@ static void mx1ts_disable_pen_touch_interrupt(struct mx1ts *mts)
        value = mx1ts_reg_read(mts, ASP_ICNTLR);
        value &= ~ASP_PIRQE;
        mx1ts_reg_write(mts, ASP_ICNTLR, value);
+       printk(KERN_DEBUG "mx1_touchscreen: pen_touch: deaktivating pen_down interrupt \n");
 }
 
 static void mx1ts_enable_pen_up_interrupt(struct mx1ts *mts)
 {
        unsigned int value;
 
-       /* Enable pen up interrupt. XXX: This feature is undocumented. */
        value = mx1ts_reg_read(mts, ASP_ICNTLR);
-       value |= ASP_PUIE;
+       value |= ASP_PUIE; 
        mx1ts_reg_write(mts, ASP_ICNTLR, value);
+       printk(KERN_DEBUG "mx1_touchscreen: pen_up: activating pen_up interrupt \n");
 }
 
 static void mx1ts_disable_pen_up_interrupt(struct mx1ts *mts)
@@ -311,6 +291,7 @@ static void mx1ts_disable_pen_up_interrupt(struct mx1ts *mts)
        value = mx1ts_reg_read(mts, ASP_ICNTLR);
        value &= ~ASP_PUIE;
        mx1ts_reg_write(mts, ASP_ICNTLR, value);
+       printk(KERN_DEBUG "mx1_touchscreen: pen_up: deaktivating pen_up interrupt \n");
 }
 
 
@@ -318,6 +299,8 @@ static void mx1ts_disable_pen_up_interrupt(struct mx1ts *mts)
 static void mx1ts_start_auto_calibration(struct mx1ts *mts)
 {
        unsigned int value;
+       printk(KERN_DEBUG "mx1_touchscreen: start_auto_calibration \n");
+
 
        mts->auto_calibration = 1;
 
@@ -325,8 +308,7 @@ static void mx1ts_start_auto_calibration(struct mx1ts *mts)
 
        /* Set the mode to X then Y */
        value &= ~ASP_MODE_MASK;
-       /*value |= ASP_MODE_ONLY_X;*/
-       value |= ASP_MODE_ONLY_Y;
+       value |= ASP_MODE_ONLY_X;
 
        /* Enable auto zero. */
        value |= ASP_AZE;
@@ -367,28 +349,43 @@ static void mx1ts_reset_asp(struct mx1ts *mts)
        value = ASP_CLKEN | ASP_NM | ASP_SW6 | ASP_BGE;
         mx1ts_reg_write(mts, ASP_ACNTLCR, value);
 
-       /* Set the clock divide ratio to 2. */
-       mx1ts_reg_write(mts, ASP_CLKDIV, 0x01);
+       /* 0x01 Set the clock divide ratio to 2. */
+       mx1ts_reg_write(mts, ASP_CLKDIV, 0x1f);
 
        /* Set the sample rate control register. These values should yield
          * about 150 samples per second, which seems to give good smooth
          * lines. */
-       value = (0x2 << ASP_DMCNT_SCALE) |      /* Decimation ratio is 3 */
-               (0x1 << ASP_IDLECNT_SCALE) |    /* Idle count is 1 clock */
-               (0x2 << ASP_DSCNT_SCALE);       /* Data setup is 2 clocks */
+       value = (0x7 << ASP_DMCNT_SCALE) |      /* 0x2 - Decimation ratio is 3 */
+               (0x3F << ASP_IDLECNT_SCALE) |   /* 0x1 - Idle count is 1 clock */
+               (0xF << ASP_DSCNT_SCALE);       /* 0x2 - Data setup is 2 clocks */
        mx1ts_reg_write(mts, ASP_PSMPLRG, value);
 
        /* Disable the compare function. */
        mx1ts_reg_write(mts, ASP_CMPCNTL, 0);
 }
 
-static void mx1ts_evt_add(struct input_dev *idev, u8 p , u16 x, u16 y)
+static void mx1ts_evt_add_touch(struct input_dev *idev, u8 p , u16 x, u16 y)
 {
-       input_report_key(idev, BTN_TOUCH, p);
+       struct mx1ts *mts = dev_get_drvdata(idev->dev.parent);
+       mts->x_akt = x;
+       mts->y_akt = y;
+
+       input_report_key(idev, BTN_TOUCH, 1);
        input_report_abs(idev, ABS_X, x);
        input_report_abs(idev, ABS_Y, y);
-       /* input_report_abs(idev, ABS_PRESSURE, pressure);*/
-       printk(KERN_ERR "ukladam hodnoty vystupu %i, %i, %i.\n", p, x, y);
+       /*input_report_abs(idev, ABS_PRESSURE, p);*/
+       input_report_abs(idev, ABS_PRESSURE, 1);
+       input_sync(idev);
+}
+
+static void mx1ts_evt_add_up(struct input_dev *idev)
+{
+       struct mx1ts *mts = dev_get_drvdata(idev->dev.parent);
+       
+       input_report_abs(idev, ABS_X, mts->x_akt);
+       input_report_abs(idev, ABS_Y, mts->y_akt);
+       input_report_abs(idev, ABS_PRESSURE, 0);
+       /*input_report_key(idev, BTN_TOUCH, 0);*/
        input_sync(idev);
 }
 
@@ -396,109 +393,96 @@ static void mx1ts_evt_add(struct input_dev *idev, u8 p , u16 x, u16 y)
  * Handle the pen data ready interrupt, generated when pen data is
  * in the FIFO.
  */
-/*static irqreturn_t mx1ts_pendata_irq(int irq, void *dev_id)*/
-static irqreturn_t mx1ts_pendata_irq(int irq, struct mx1ts *mts)
+
+static irqreturn_t mx1ts_pendata_irq(int irq, void *dev_id)
 {      
+       
+       struct mx1ts *mts = (struct mx1ts *)dev_id;
        u16 mx1_cal_range_x;
        u16 mx1_cal_range_y;
        static unsigned int auto_zero, pen_x, pen_y, pen_u;
 
-       /*struct mx1ts *mts = (struct mx1_ts *) dev_id;*/
-
        mx1_cal_range_x = 1;
        mx1_cal_range_y = 1;
-       
-/*     printk("<1> mx1_touchscreen: mx1_pendata_irq interrupt recived from struct %p\n", mts);*/
+
+       /*printk(KERN_DEBUG "mx1_touchscreen: mx1_pendata_irq interrupt recived from struct %p\n", mts);*/
                
-       if (mx1ts_reg_read(mts, ASP_ISTATR) & ASP_PUIS) {
-               mx1ts_reg_set_mask(mts, ASP_ISTATR, ASP_PUIS);
+       if (mx1ts_reg_read(mts, ASP_ISTATR) & ASP_PUIS) {       /*pen up interupt pending*/
+               mx1ts_reg_set_mask(mts, ASP_ISTATR, ASP_PUIS);  /*clearing pen up interupt*/
 
-               mx1ts_disable_auto_sample(mts);
+               mx1ts_disable_auto_sample(mts);                         
                mx1ts_disable_pen_up_interrupt(mts);
                mx1ts_enable_pen_touch_interrupt(mts);
 
-               mx1ts_evt_add(mts, 0, 0, 0);
-               printk(KERN_ERR "Zaplnena fifo.\n");
+               if(mts->is_open)                                        /*report last known touch*/
+                       mx1ts_evt_add_up(mts->ts_idev);
+               printk(KERN_DEBUG "Pen up interrupt.\n");                       
                mx1ts_flush_fifo(mts);
 
                return IRQ_HANDLED;
        }
        
-       /*mts->stav = 1;*/
-/*     printk("mx1_touchscreen: moje pamet je : %p  \n", mts->mx1ts_mem);*/
-
-        
-       /*if (pmts->auto_calibration) {
-               unsigned int value;
+       if (mts->auto_calibration) {
+/*             unsigned int value;*/
 
-               mx1_cal_auto_zero = mx1_reg_read(ASP_PADFIFO) & 0xFFFF;
-               mx1_cal_range_x = mx1_reg_read(ASP_PADFIFO) & 0xFFFF;
-               mx1_cal_range_y = mx1_reg_read(ASP_PADFIFO) & 0xFFFF;
+               mts->cal_auto_zero = mx1ts_reg_read(mts, ASP_PADFIFO) & 0xFFFF;
+               mts->cal_range_x = mx1ts_reg_read(mts, ASP_PADFIFO) & 0xFFFF;
+               mts->cal_range_y = mx1ts_reg_read(mts, ASP_PADFIFO) & 0xFFFF;
 
-               if ((mx1_cal_auto_zero >= mx1_cal_range_x) ||
-                   (mx1_cal_auto_zero >= mx1_cal_range_y)) {
+               if ((mts->cal_auto_zero >= mts->cal_range_x) ||
+                   (mts->cal_auto_zero >= mts->cal_range_y)) {
                        // Invalid data. 
-                       mx1ts_start_auto_calibration();
-                       return IRQ_HANDLED;
+                       printk(KERN_INFO "Invalid calibration data. Recalibrating. \n");
+                       mx1ts_flush_fifo(mts);                  /*if there are some older data*/
+                       mx1ts_start_auto_calibration(mts);
+                       return IRQ_HANDLED; /*return IRQ_NONE;*/
                }
 
-               mx1_cal_range_x -= mx1_cal_auto_zero;
-               mx1_cal_range_y -= mx1_cal_auto_zero;
-
-               value = mx1_reg_read(ASP_ACNTLCR);
-               value &= ~0x04000000; // Undocumented. 
-               mx1_reg_write(ASP_ACNTLCR, value);
-
-               mx1_performing_auto_calibration = 0;
-
-               mx1ts_enable_auto_sample();
-       } else {*/
+               printk(KERN_DEBUG "mx1 touchscreen: calibration cal_auto_zero %i.\n",mts->cal_auto_zero);
+               printk(KERN_DEBUG "mx1 touchscreen: calibration cal_range_x %i.\n",mts->cal_range_x);
+               printk(KERN_DEBUG "mx1 touchscreen: calibration cal_range_y %i.\n",mts->cal_range_y);
+               
+               mts->cal_range_x -= mts->cal_auto_zero;
+               mts->cal_range_y -= mts->cal_auto_zero;
+               
+               
+               /*
+               value = mx1ts_reg_read(mts, ASP_ACNTLCR);  podle me duplicita
+               value &= ~0x04000000; 
+               mx1ts_reg_write(mts, ASP_ACNTLCR, value);
+               */
+               mts->auto_calibration = 0;
+
+               mx1ts_enable_auto_sample(mts);
+       } else {
                // There could be more than one sample in the FIFO, but we're
                // only going to read one per call. The interrupt will be
                // generated as long as there is data in the FIFO. 
 
                if ((mx1ts_reg_read(mts, ASP_ISTATR) & ASP_PDR) != ASP_PDR) {
-                       return IRQ_NONE; /* TODO je to tak? */
+                       return IRQ_NONE; 
                }
 
                auto_zero = mx1ts_reg_read(mts, ASP_PADFIFO);
-               /*if (auto_zero > (mx1ts_cal_auto_zero + 0x200)) {
-                       return IRQ_HANDLED;
-               }*/
+               if (auto_zero > (mts->cal_auto_zero + 0x200)) {
+                       return IRQ_HANDLED; /* TODO asi se nic nedeje */
+               }
 
                pen_x = mx1ts_reg_read(mts, ASP_PADFIFO);
                pen_y = mx1ts_reg_read(mts, ASP_PADFIFO);
                pen_u = mx1ts_reg_read(mts, ASP_PADFIFO);
 
-               /*pen_x = (u32)(((pen_x - auto_zero) << 16) /
-                             mx1_cal_range_x);
-               pen_y = (u32)(((pen_y - auto_zero) << 16) /
-                             mx1_cal_range_y);*/
-
-               mx1ts_evt_add(mts, 1, pen_x, pen_y);
-       /*}*/
+               pen_x = mts->x_res - (u32)(((pen_x - auto_zero) << 16) /
+                             mts->cal_range_x);
+               pen_y = mts->y_res - (u32)(((pen_y - auto_zero) << 16) /
+                             mts->cal_range_y);
+               
+               if(mts->is_open)
+                       mx1ts_evt_add_touch(mts->ts_idev, 200 , pen_x, pen_y);
+       }
        return IRQ_HANDLED;
 }
 
-static void mx1ts_pokus_zapnout(struct mx1ts *mts) {
-       mx1ts_reg_write(mts, ASP_CLKDIV, 0x1F); //clok divide register
-       mx1ts_reg_write(mts, ASP_ICNTLR , ASP_PUIE | ASP_PDRE | ASP_PFFE | ASP_EDGE | ASP_PIRQE);       //interupt control register //0X00000833
-       mx1ts_reg_write(mts, ASP_PSMPLRG, 0x0000487F);  //pen a/d sample rate control register 
-       mx1ts_reg_write(mts,ASP_ACNTLCR, ASP_CLKEN | ASP_AZE | ASP_AUTO | (10<<12) | 3);        //control register
-
-
-}
-
-static void mx1ts_enable_pen_up_irq(struct mx1ts *mts)
-{
-       unsigned int value;
-       printk(KERN_ERR "Pen up irq.\n");       
-
-       value = mx1ts_reg_read(mts, ASP_ICNTLR);
-       value |= ASP_PUIE;
-       mx1ts_reg_write(mts, ASP_ICNTLR, value);
-}
-
 /*
  * Handle the touch interrupt, generated when the pen is pressed/
  * released.
@@ -508,12 +492,13 @@ static irqreturn_t mx1ts_touch_irq(int irq, void *dev_id)
 {
        struct mx1ts *mts = (struct mx1ts *) dev_id;
        
-       printk(KERN_ERR "Prijat touch irq \n"); 
+       printk(KERN_DEBUG "mx1 touchscreen: Touch down interrupt \n");  
 
        /* Clear the interrupt. */
        mx1ts_reg_set_mask(mts, ASP_ISTATR, ASP_PEN);
 
        mx1ts_disable_pen_touch_interrupt(mts);
+       mx1ts_flush_fifo(mts);
        mx1ts_start_auto_calibration(mts);
        mx1ts_enable_pen_up_interrupt(mts);
 
@@ -523,27 +508,25 @@ static irqreturn_t mx1ts_touch_irq(int irq, void *dev_id)
 static inline int mx1ts_enable_irqs(struct mx1ts *mts) //zaregistruje preruseni
 {
        int result;
-// int request_irq(unsigned int irq,  void (*handler)(int, struct pt_regs *),  unsigned long flags,  const char *device); 
-//static void mx1ts_pendata_int(int irq, void *dev_id, struct pt_regs *regs);
-//request_irq (irq, soc_intr, IRQF_SHARED, "SOC", (void *)s))
-       result = request_irq(ASP_PENDATA_IRQ,
+
+       result = request_irq(mts->irq_pen_data,
                             mx1ts_pendata_irq,
-                            /*IRQF_*/ 0,
+                                0,
                             DEV_IRQ_ID,
                             mts);
        if (result) {
-               printk(KERN_ERR "Couldn't request pen data IRQ.\n");
+               printk(KERN_ERR "mx1 touchscreen: Couldn't request pen data IRQ.\n");
                return result;
        }
-
-       result = request_irq(ASP_TOUCH_IRQ,
+       
+       result = request_irq(mts->irq_touch,
                             mx1ts_touch_irq,
                             /*IRQF_*/ 0,
                             DEV_IRQ_ID,
                             mts);
        if (result) {
-               printk(KERN_ERR "Couldn't request pen touch IRQ.\n");
-               free_irq(ASP_PENDATA_IRQ, mts);
+               printk(KERN_ERR "mx1 touchscreen: Couldn't request pen touch IRQ.\n");
+               free_irq(mts->irq_pen_data, mts);
                return result;
        }
        return result;
@@ -555,131 +538,129 @@ static int mx1ts_on(struct mx1ts *mts)
 {
        int ret = 0;
        
-       if(!request_mem_region(ASP_BASE_ADDR, 0x38 , "mx1ts")) {
-               printk(KERN_ERR "mx1ts: request_mem_region \tFAILED\n");
-               return -1;      
+       if(!request_mem_region(mts->baseaddr, mts->baseaddr_len , "mx1ts")) {
+               printk(KERN_ERR "mx1 touchscreen: request_mem_region \tFAILED\n");
+               return -ENOMEM; 
        }
 
-       printk("<1>mx1ts: request_mem_region \tOK\n");  
+       printk(KERN_DEBUG "mx1 touchscreen: request_mem_region \tOK\n");        
 
-       mts->mx1ts_mem = ioremap ( ASP_BASE_ADDR, 0x38);
+       mts->mx1ts_mem = ioremap ( mts->baseaddr, mts->baseaddr_len);
        
-       printk("<1>mx1ts: memory remaped on %p \n", mts->mx1ts_mem);
+       if(!mts->mx1ts_mem) {
+               release_mem_region(mts->baseaddr, mts->baseaddr_len);
+               printk(KERN_ERR "mx1 touchscreen: ioremap  \tFAILED\n");
+               return -1;      
+       }
+
+       printk(KERN_DEBUG "mx1 touchscreen: memory remaped on %p \n", mts->mx1ts_mem);
        
 
 
-       printk("<1>mx1ts: enabling irqs\n");
+       /*printk(KERN_DEBUG "mx1 touchscreen: enabling irqs\n");*/
        if ((ret = mx1ts_enable_irqs(mts)))
                return ret;
-       printk("<1>mx1ts: irqs enabled \tOK\n");        
+       printk(KERN_DEBUG "mx1 touchscreen: irqs enabled \tOK\n");      
 
        
        mx1ts_reset_asp(mts);
-       printk("<1>mx1ts: reset\tOK\n");
+       printk(KERN_DEBUG "mx1 touchscreen: reset\tOK\n");
        
-       mx1ts_enable_pen_touch_interrupt(mts);
-       printk("<1>mx1ts: zapnuti touch interrupt\n");  
        return 0;
 }
 
-static int mx1ts_close(struct platform_device *dev)
+static void mx1ts_close(struct input_dev *idev)
 {
-       return 0;
+       struct mx1ts *mts = dev_get_drvdata(idev->dev.parent);
+       mts->is_open = 0;
+       mx1ts_flush_fifo(mts);
+       mx1ts_reset_asp(mts);
+       printk(KERN_DEBUG "mx1 touchscreen: reset\tOK\n");
+       mx1ts_disable_pen_touch_interrupt(mts);
+       mx1ts_disable_pen_up_interrupt(mts);
+
 }
 
-static int mx1ts_open(struct platform_device *dev)
+static int mx1ts_open(struct input_dev *idev)
 {
+       struct mx1ts *mts = dev_get_drvdata(idev->dev.parent);
+       mts->is_open = 1;
+       mx1ts_flush_fifo(mts);                  /*if there are some older data*/
+       mx1ts_enable_pen_touch_interrupt(mts);
+       printk(KERN_DEBUG "mx1 touchscreen: touch_down interupt enabled\n");    
        return 0;
 }
 
 static int mx1ts_probe(struct platform_device *dev)
 {
-               
-       //struct mx1ts *mts= &mx1_ts;
-       struct mx1ts *mts;              
+       struct mx1ts *mts;
        struct input_dev *idev;
-       int error, id, x_res, y_res;
+       int error;
 
-//     mx1ts_disable_pen_touch_interrupt();
-//     mx1ts_start_auto_calibration();
-//     mx1ts_enable_pen_up_interrupt();
+       /* TODO jak poznat ze zarizeni je pritomne ? */
 
-       mts = kzalloc(sizeof(struct mx1ts), GFP_KERNEL);        //alokuje pamet
-       idev = input_allocate_device();                         
+       mts = kzalloc(sizeof(struct mx1ts), GFP_KERNEL);
+       idev = input_allocate_device();
        if (!mts || !idev) {
                error = -ENOMEM;
+               printk(KERN_ERR "mx1 touchscreen: failed allocate memory for struct mx1ts or idev\n");  
                goto err_free_devs;
-               return error;   //XXX
+               return error;   
        }
-       mx1_ts = *mts;
-
-       
-       mx1ts_on(mts);  
-       
-       return 0;
-
-       mts->ts_idev = idev;
-       //ucb->adcsync = adcsync;
-       //ucb->ac97 = to_ac97_t(dev);
-       init_waitqueue_head(&mts->ts_wait);
-
-/*     id = mx1ts_reg_read(ucb, UCB_ID);
-       if (id != UCB_ID_1400) {
-               error = -ENODEV;
-               goto err_free_devs;
-       }
-
-       error = mx1ts_detect_irq(ucb);
-       if (error) {
-               printk(KERN_ERR "UCB1400: IRQ probe failed\n");
-               goto err_free_devs;
-       }
-
-       error = request_irq(ucb->irq, mx1ts_hard_irq, IRQF_TRIGGER_RISING,
-                               "UCB1400", ucb);
-       if (error) {
-               printk(KERN_ERR "mx1ts: unable to grab irq%d: %d\n",
-                               ucb->irq, error);
+       mts->baseaddr = dev->resource[0].start;
+       mts->baseaddr_len = dev->resource[0].end - dev->resource[0].start;
+       printk(KERN_ERR "mx1 touchscreen: memorry base addr %d \n", mts->baseaddr);     
+       printk(KERN_ERR "mx1 touchscreen: memorry base addr_len %d \n", mts->baseaddr_len);     
+       mts->irq_pen_data = dev->resource[1].start;
+       mts->irq_touch = dev->resource[2].start;
+
+       error = mx1ts_on(mts);
+       if(error<0) {                   /* remaping registers, reseting device */
+               printk(KERN_ERR "mx1 touchscreen: failed mx1ts_on - remaping registers, reseting device\n");    
                goto err_free_devs;
+               return error;   
        }
-       printk(KERN_DEBUG "UCB1400: found IRQ %d\n", ucb->irq);*/
 
-//     input_set_drvdata(idev, mx1_ts);
+       mts->ts_idev = idev;
+       init_waitqueue_head(&mts->ts_wait);
 
+       //input_set_drvdata(idev, mts);         /*moznost ulozit ukazatel na trukturu do input_driver*/
+       platform_set_drvdata(dev, mts);
+       
        idev->dev.parent        = &dev->dev;
        idev->name              = "MX1 touchscreen interface";
-       idev->id.vendor         = (unsigned int) 345;//mx1ts_reg_read(mx1_ts, AC97_VENDOR_ID1);
-       idev->id.product        = id;
+       idev->phys              = "mx1ts/input0";       
+       idev->id.vendor         = (unsigned int) 345;   /*mx1ts_reg_read(mx1_ts, AC97_VENDOR_ID1);*/
+       idev->id.product        = (unsigned int) 354;   
+       idev->id.version        = 0x0100;               
+       idev->id.bustype        = BUS_HOST;
        idev->open              = mx1ts_open;
        idev->close             = mx1ts_close;
        idev->evbit[0]          = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
        idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
 
-       //idev->bustype         = BUS_HOST;
-
-       printk("<1> mx1ts: setting idev struct \tOK\t");
+       printk(KERN_DEBUG "mx1ts: setting idev struct \tOK\n");
        
-/*     mx1ts_adc_enable(ucb);
-       x_res = mx1ts_read_xres(ucb);
-       y_res = mx1ts_read_yres(ucb);
-       mx1ts_adc_disable(ucb);
-       printk(KERN_DEBUG "UCB1400: x/y = %d/%d\n", x_res, y_res);*/
+       mts->x_res = 60000;             /*TODO neni mozne zmerit ? */
+       mts->y_res = 60000;
+       printk(KERN_DEBUG "mx1ts: x/y = %d/%d\n", mts->x_res, mts->y_res);
 
-       input_set_abs_params(idev, ABS_X, 0, x_res, 0, 0);
-       input_set_abs_params(idev, ABS_Y, 0, y_res, 0, 0);
-       input_set_abs_params(idev, ABS_PRESSURE, 0, 0, 0, 0);
+       input_set_abs_params(idev, ABS_X, 0, mts->x_res, 0, 0);
+       input_set_abs_params(idev, ABS_Y, 0, mts->y_res, 0, 0);
+       input_set_abs_params(idev, ABS_PRESSURE, 0, 1, 0, 0);
 
-//     error = input_register_device(idev);
-//     if (error)
-//             goto err_free_irq;
+       error = input_register_device(idev);
+       if (error) {
+               printk(KERN_ERR "mx1 touchscreen: error while register input device\n");
+               goto err_free_devs;
+               }
+
+       printk(KERN_DEBUG "mx1ts: input device registered \tOK\n");
 
-//     dev_set_drvdata(dev, ucb);
        return 0;
 
-// err_free_irq:
-//     free_irq(ucb->irq, ucb);
- err_free_devs:
-       printk("<1> mx1ts: chyba pri nastavovani irq  \n");
+  err_free_devs:
+       printk(KERN_ERR "mx1 touchscreen: error in device probe  \n");
        input_free_device(idev);
        kfree(mts);
        return error;
@@ -688,20 +669,26 @@ static int mx1ts_probe(struct platform_device *dev)
 
 
 
-
 static int mx1ts_remove(struct platform_device *dev)
 {
-       struct mx1_ts *mts = &mx1_ts;
+       struct mx1ts *mts = platform_get_drvdata(dev);
+
+       //BUG_ON(mts == NULL);
        
-       release_mem_region(ASP_BASE_ADDR, 0x40);
-       iounmap(mx1_ts.mx1ts_mem);
-       free_irq(ASP_PENDATA_IRQ, mts);
-       free_irq(ASP_TOUCH_IRQ, mts);
+       free_irq(mts->irq_pen_data, mts);
+       free_irq(mts->irq_touch, mts);
 
-       //input_free_device(mts->ts_idev);
+       /*input_free_device(mts->ts_idev);
+       printk(KERN_DEBUG "Free device \tOK\n");*/
+       
+       input_unregister_device(mts->ts_idev);
+       printk(KERN_INFO "mx1 touchscreen: Unregister device \tOK\n");
+       iounmap(mts->mx1ts_mem);
+       release_mem_region(mts->baseaddr, mts->baseaddr_len);
+               
        kfree(mts);
 
-       printk("<1> Removing driver \tOK\n");
+       printk(KERN_INFO "mx1 touchscreen: Removing driver \tOK\n");
        return 0;
 }
 
@@ -712,8 +699,6 @@ static int mx1ts_resume(struct platform_device *dev)
        return 0;
 }
 
-/* inicializace ovladace a driveru (insmod, rmmmod ) */
-
 static struct platform_driver mx1ts_driver = {
        .probe = mx1ts_probe,
        .remove = mx1ts_remove,
@@ -727,7 +712,7 @@ static struct platform_driver mx1ts_driver = {
 static struct resource mx1ts_resources[] = {
        [0] = {
                .start  = ASP_BASE_ADDR + 0,
-               .end    = ASP_BASE_ADDR + 0x37,
+               .end    = ASP_BASE_ADDR + ASP_BASE_ADDR_LEN,
                .flags  = IORESOURCE_MEM,
        },
        [1] = {
@@ -742,11 +727,21 @@ static struct resource mx1ts_resources[] = {
        },
 };
 
+void mx1ts_device_release(struct device *dev) {
+       /*struct platform_device *pdev = to_platform_device(dev);
+       release_resource(&dev->res);
+       kfree(pdev);
+       }*/
+}
+
 static struct platform_device mx1ts_device = {
        .name           = "mx1ts",
        .id             = 0,
        .num_resources  = ARRAY_SIZE(mx1ts_resources),
        .resource       = mx1ts_resources,
+       .dev = {
+       .release = mx1ts_device_release,
+       },
 };
 
 
@@ -758,7 +753,7 @@ static int __init mx1ts_init(void)
        int error;
        error = platform_device_register(&mx1ts_device);
        if(error) {
-               printk(KERN_ERR "PiMX1_touchsceen: device registration failed\n");
+               printk(KERN_ERR "mx1 touchscreen: device registration failed\n");
                return error;
        }
 
@@ -783,16 +778,9 @@ static void __exit mx1ts_exit(void)
        return;
 }                           
 
-//module_param(adcsync, bool, 0444);
+//module_param(adcsync, bool, 0444);   /* priklad parametru modulu*/
 //MODULE_PARM_DESC(adcsync, "Synchronize touch readings with ADCSYNC pin.");
 
-//module_param(ts_delay, int, 0444);
-//MODULE_PARM_DESC(ts_delay, "Delay between panel setup and position read. Default = 55us.");
-
-//module_param(ts_delay_pressure, int, 0444);
-//MODULE_PARM_DESC(ts_delay_pressure,
-//               "delay between panel setup and pressure read.  Default = 0us.");
-
 module_init(mx1ts_init);
 module_exit(mx1ts_exit);