Paul Sokolovsky a écrit :
> Hello Pierre,
>
> Monday, January 29, 2007, 9:26:04 PM, you wrote:
>
> []
>   
>>>   Yes, I refactored it exactly for your use, and I'd appreciate if you
>>> give it try (and win ;-) ). Please see h4000 code fro how to use it
>>> (pretty simple actually, you just need to pass in pen down IRQ).
>>>
>>>   I also confirmed that ADS7846 and TSC2046 are exact clones. Quoting
>>> doc:
>>> "The TSC2046 is 100% pin-compatible with the existing
>>> ADS7846, and will drop into the same socket."
>>> They talk about pin compatibility here, but looking at command bits,
>>> programmatical interface is the same too. So, this is great news,
>>> meaning that the whole dozen of PDAs can use the same driver. I will
>>> try ads7846ts_ssp on hx4700 as the next thing to do too.
>>>   
>>>       
>> ok : go for it, then :-)
>> Just a question : do you have any idea of the transfer laws to be used ?
>> I get some really non-linear measurements...
>>     
Something goes wrong with the device-independent 7846 driver.
It seems that the SPI communication is faulty. As long as the stylus is
down, it prints :

ads7846_ssp_putget: warning: timeout while waiting for SSSR_RNE_MSK
ads7846_ssp_putget: warning: timeout while waiting for SSSR_TNF_MSK
ads7846_ssp_putget: warning: timeout while waiting for SSSR_RNE_MSK
ads7846_ssp_putget: warning: timeout while waiting for SSSR_TNF_MSK

I do not understand why...
Attached is the touchscreen module I hacked, if it can help.

Pierre


/* Touch screen driver for the TI something-or-other
 *
 * Copyright © 2005 SDG Systems, LLC
 *
 * Based on code that was based on the SAMCOP driver.
 *     Copyright © 2003, 2004 Compaq Computer Corporation.
 *
 * Use consistent with the GNU GPL is permitted,
 * provided that this copyright notice is
 * preserved in its entirety in all copies and derived works.
 *
 * COMPAQ COMPUTER CORPORATION MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
 * FITNESS FOR ANY PARTICULAR PURPOSE.
 *
 * Author:  Keith Packard <[EMAIL PROTECTED]>
 *          May 2003
 *
 * Updates:
 *
 * 2004-02-11   Michael Opdenacker      Renamed names from samcop to shamcop,
 *                                      Goal:support HAMCOP and SAMCOP.
 * 2004-02-14   Michael Opdenacker      Temporary fix for device id handling
 *
 * 2005-02-18   Aric Blumer             Converted  basic structure to support hx4700
 *
 * 2005-06-07   Aric Blumer             Added tssim device handling so we can
 *                                      hook in the fbvncserver.
 * 2007-01-27   Pierre Gaufillet        Adaptation to Axim X51(v)
 */

#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/input_pda.h>
#include <linux/platform_device.h>
#include <linux/battery.h>

#include <asm/arch/hardware.h>
#include <asm/irq.h>
#include <asm/mach/irq.h>
#include <asm/io.h>
#include <asm/semaphore.h>

#include <asm/arch/pxa-regs.h>
#include <asm/arch-pxa/aximx50-gpio.h>

/* SSP port to use */
#define SSP_ID 1

#if SSP_ID == 1

#define enable_ssp_clock()   pxa_set_cken(CKEN23_SSP1, 1);
#define disable_ssp_clock()  pxa_set_cken(CKEN23_SSP1, 0);

#elif SSP_ID == 2

#define enable_ssp_clock()   pxa_set_cken(CKEN3_SSP2, 1);
#define disable_ssp_clock()  pxa_set_cken(CKEN3_SSP2, 0);

#elif SSP_ID == 3

#define enable_ssp_clock()   pxa_set_cken(CKEN4_SSP3, 1);
#define disable_ssp_clock()  pxa_set_cken(CKEN4_SSP3, 0);

#endif

enum touchscreen_state {
    STATE_WAIT_FOR_TOUCH,   /* Waiting for a PEN interrupt */
    STATE_SAMPLING          /* Actively sampling ADC */
};

struct touchscreen_data {
    enum touchscreen_state state;
    struct timer_list      timer;
    int                    irq;
    struct input_dev       *input;
};

static unsigned long poll_sample_time   = 10; /* Sample every 10 milliseconds */

static struct touchscreen_data *ts_data;

#define TS_SAMPLES 5

module_param(poll_sample_time, ulong, 0644);
MODULE_PARM_DESC(poll_sample_time, "Poll sample time");

static inline void
report_touchpanel(struct touchscreen_data *ts, int pressure, int x, int y)
{
    input_report_abs(ts->input, ABS_PRESSURE, pressure);
    input_report_abs(ts->input, ABS_X, x);
    input_report_abs(ts->input, ABS_Y, y);
    input_sync(ts->input);
}

static struct work_struct serial_work;

static irqreturn_t
pen_isr(int irq, void *irq_desc)
{
    /* struct touchscreen_data *ts = dev_id->data; */
    struct touchscreen_data *ts = ts_data;

    if(irq == X50_IRQ(PEN_IRQ_N)) {

        if (ts->state == STATE_WAIT_FOR_TOUCH) {
            /*
             * There is ground bounce or noise or something going on here:
             * when you write to the SSP port to get the X and Y values, it
             * causes a TOUCHPANEL_IRQ_N interrupt to occur.  So if that
             * happens, we can check to make sure the pen is actually down and
             * disregard the interrupt if it's not.
             */
            if(GET_X50_GPIO(PEN_IRQ_N) == 0) {
                /*
                * Disable the pen interrupt.  It's reenabled when the user lifts the
                * pen.
                */
                disable_irq(IRQ_GPIO(GPIO_NR_X50_PEN_IRQ_N));
                ts->state = STATE_SAMPLING;
                schedule_work(&serial_work);
            }
        } else {
            /* Shouldn't happen */
            printk(KERN_ERR "Unexpected ts interrupt\n");
        }

    }
    return IRQ_HANDLED;
}

static void
ssp_init(void)
{
	/* now to set up GPIOs... */
	GPDR(GPIO23_SCLK) |=  GPIO_bit(GPIO23_SCLK);
	GPDR(GPIO24_SFRM) |=  GPIO_bit(GPIO24_SFRM);
	GPDR(GPIO25_STXD) |=  GPIO_bit(GPIO25_STXD);
	GPDR(GPIO26_SRXD) &= ~GPIO_bit(GPIO26_SRXD);
	pxa_gpio_mode(GPIO23_SCLK_MD);
	pxa_gpio_mode(GPIO24_SFRM_MD);
	pxa_gpio_mode(GPIO25_STXD_MD);
	pxa_gpio_mode(GPIO26_SRXD_MD);

    enable_ssp_clock();

    /* *** Set up the SPI Registers *** */
    SSCR0_P(SSP_ID) =
          (1 << 20)     /* Extended Data Size Select */
        | (6 << 8)      /* Serial Clock Rate */
        | (0 << 7)      /* Synchronous Serial Enable (Disable for now) */
        | (0 << 4)      /* Motorola SPI Interface */
        | (7 << 0)      /* Data Size Select  (24-bit) */
        ;
    SSCR1_P(SSP_ID) = 0;
    SSPSP_P(SSP_ID) = 0;

    /* Clear the Status */
    SSSR_P(SSP_ID)  = SSSR_P(SSP_ID) & 0x00fcfffc;

    /* Now enable it */
    SSCR0_P(SSP_ID) =
          (1 << 20)     /* Extended Data Size Select */
        | (6 << 8)      /* Serial Clock Rate */
        | (1 << 7)      /* Synchronous Serial Enable */
        | (0 << 4)      /* Motorola SPI Interface */
        | (7 << 0)      /* Data Size Select  (24-bit) */
        ;
}

DECLARE_MUTEX(serial_mutex);

static void
start_read(void *in)
{
    struct touchscreen_data *touch = in;
    unsigned long inc = (poll_sample_time * HZ) / 1000;
    int i;

    down(&serial_mutex);

    /* Write here to the serial port.
     * Then we have to wait for poll_sample_time before we read out the serial
     * port.  Then, when we read it out, we check to see if the pen is still
     * down.  If so, then we issue another request here.
     */

    for(i = 0; i < TS_SAMPLES; i++) {
        while(!(SSSR_P(SSP_ID) & (1 << 2)))
            ;
        /* It's not full. Write the command for X */
        SSDR_P(SSP_ID) = 0xd00000;
        while(!(SSSR_P(SSP_ID) & (1 << 2)))
            ;
        /* It's not full. Write the command for Y */
        SSDR_P(SSP_ID) = 0x900000;
    }

    /*
     * Enable the timer. We should get an interrupt, but we want keep a timer
     * to ensure that we can detect missing data
     */
    mod_timer(&touch->timer, jiffies + inc);
}

static void
ts_timer_callback(unsigned long data)
{
    struct touchscreen_data *ts = (struct touchscreen_data *)data;
    int x, y; // resulting measure
    int ssrval;
    
    
    /*
     * Check here to see if there is anything in the SPI FIFO.  If so,
     * return it if there has been a change.  If not, then we have a
     * timeout.  Generate an erro somehow.
     */
    ssrval = SSSR_P(SSP_ID);
    if(ssrval & (1 << 3)) { /* Look at Rx Not Empty bit */
        int number_of_entries_in_fifo;

        /* The FIFO is not emtpy. Good! Now make sure there are at least two
         * entries. */

        number_of_entries_in_fifo = ((ssrval >> 12) & 0xf) + 1;

        if(number_of_entries_in_fifo < (TS_SAMPLES * 2)) {
            /* Not ready yet. Come back later. */
            unsigned long inc = (poll_sample_time * HZ) / 1000;
            mod_timer(&ts->timer, jiffies + inc);
            return;
        }
        
        if(number_of_entries_in_fifo == TS_SAMPLES * 2) {
            int i/*, result*/;
            int X[TS_SAMPLES], Y[TS_SAMPLES];
            int num;
            int keep;
            long x_av, y_av;        // average measure
            long xtmp, ytmp;        // temp result  
 
            for(i = 0; i < TS_SAMPLES; i++) {
                X[i] = SSDR_P(SSP_ID);
                Y[i] = SSDR_P(SSP_ID);
                //printk("pen_isr: X=%d, Y=%d\n", X[i], Y[i]);
            }

            up(&serial_mutex);

            x = y = xtmp = ytmp = 0;
            num = 0;
            keep = 0;
            x_av = (X[0] + X[1] + X[2] + X[3] + X[4]) / 5;
            y_av = (Y[0] + Y[1] + Y[2] + Y[3] + Y[4]) / 5;
            //printk(KERN_NOTICE "x_av=%ld, y_av=%ld\n", x_av, y_av);

            for(i = 0; i < TS_SAMPLES; i++) {
            	long dx, dy;
            	
            	dx = (X[i] - x_av);
            	dx *= dx;
            	dy = (Y[i] - y_av);
            	dy *= dy;
                //printk(KERN_NOTICE "%d : dx=%ld, dy=%ld\n", i, dx, dy);
           		if(dx + dy < 15000000)
                {
                    xtmp += X[i];
                    ytmp += Y[i];
                    num++;
                    //printk(KERN_NOTICE "%d kept\n", i);
                }
            }
            if(num)
            {
                x = xtmp / num;
                y = ytmp / num;
                keep =1;
            }
           
            if(GET_X50_GPIO(PEN_IRQ_N) == 0) {
                /* Still down */
                if(keep) {
                    report_touchpanel(ts, 1, x, y);
                    printk(KERN_NOTICE " x=%d, y=%d\n", x, y);
                }
                start_read(ts);
            } else {
                /* Up */
                report_touchpanel(ts, 0, 0, 0);
                ts->state = STATE_WAIT_FOR_TOUCH;
                /* Re-enable pen down interrupt */
                enable_irq(X50_IRQ(PEN_IRQ_N));
            }

        } else {
            /* We have an error! Too many entries. */
            printk(KERN_ERR "TS: Expected %d entries. Got %d\n", 2, number_of_entries_in_fifo);
            /* Try to clear the FIFO */
            while(number_of_entries_in_fifo--) {
                (void)SSDR_P(SSP_ID);
            }
            up(&serial_mutex);
            if(GET_X50_GPIO(PEN_IRQ_N) == 0) {
                start_read(ts);
            }
            return;
        }
    } else {
        /* Not ready yet. Come back later. */
        unsigned long inc = (poll_sample_time * HZ) / 1000;
        mod_timer(&ts->timer, jiffies + inc);
        return;
    }
}

static int
ts_probe (struct platform_device *pdev)
{
    int retval;
    struct touchscreen_data *ts;

	ts = ts_data = kmalloc (sizeof (*ts), GFP_KERNEL);
    if (ts == NULL) {
        printk( KERN_NOTICE "aximx50_tsc2046_ts: unable to allocate memory\n" );
        return -ENOMEM;
    }
    memset (ts, 0, sizeof (*ts));


    /* *** Set up the input subsystem stuff *** */
    // memset(ts->input, 0, sizeof(struct input_dev));
    ts->input = input_allocate_device();
    if (ts->input == NULL) {
        printk( KERN_NOTICE "aximx50_tsc2046_ts: unable to allocation touchscreen input\n" );
        kfree(ts);
        return -ENOMEM;
    }
    ts->input->evbit[0]             = BIT(EV_ABS);
    ts->input->absbit[0]            = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
    ts->input->absmin[ABS_X]        = 0;
    ts->input->absmax[ABS_X]        = 32767;
    ts->input->absmin[ABS_Y]        = 0;
    ts->input->absmax[ABS_Y]        = 32767;
    ts->input->absmin[ABS_PRESSURE] = 0;
    ts->input->absmax[ABS_PRESSURE] = 1;

    ts->input->name = "x50-ts";
    ts->input->private = ts;

    input_register_device(ts->input);

    ts->timer.function = ts_timer_callback;
    ts->timer.data = (unsigned long)ts;
    ts->state = STATE_WAIT_FOR_TOUCH;
    init_timer (&ts->timer);

    INIT_WORK(&serial_work, start_read, ts);

    platform_set_drvdata(pdev, ts);

    /* *** Initialize the SSP interface *** */
    ssp_init();

    down(&serial_mutex);
    /* Make sure the device is in such a state that it can give pen
     * interrupts. */
    while(!(SSSR_P(SSP_ID) & (1 << 2)))
        ;
    SSDR_P(SSP_ID) = 0xd00000;

    for(retval = 0; retval < 100; retval++) {
        if(SSSR_P(SSP_ID) & (1 << 3)) {
            while(SSSR_P(SSP_ID) & (1 << 3)) {
                (void)SSDR_P(SSP_ID);
            }
            break;
        }
        mdelay(1);
    }
    up(&serial_mutex);


    ts->irq = X50_IRQ(PEN_IRQ_N);
    retval = request_irq(ts->irq, pen_isr, SA_INTERRUPT, "x50-ts", ts);
    if(retval) {
        printk("Unable to get interrupt\n");
        input_unregister_device (ts->input);
        return -ENODEV;
    }
    set_irq_type(ts->irq, IRQT_FALLING);

    return 0;
}

static int
ts_remove (struct platform_device *pdev)
{
    struct touchscreen_data *ts = platform_get_drvdata(pdev);

    input_unregister_device (ts->input);
    del_timer_sync (&ts->timer);
    free_irq (ts->irq, ts);

    kfree(ts);
    disable_ssp_clock()
    return 0;
}

static struct platform_driver ts_driver = {
    .probe    = ts_probe,
    .remove   = ts_remove,
    .driver = {
        .name = "x50-ts",
    },
};

static int
ts_module_init (void)
{
    printk(KERN_NOTICE "Axim X51(v) Touch Screen Driver\n");
    return platform_driver_register(&ts_driver);
}

static void
ts_module_cleanup (void)
{
    platform_driver_unregister (&ts_driver);
}

module_init(ts_module_init);
module_exit(ts_module_cleanup);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Aric Blumer, SDG Systems, LLC");
MODULE_DESCRIPTION("Axim X51(v) Touch Screen Driver");

-- 
_______________________________________________
Aximx50-port mailing list
[email protected]
https://www.handhelds.org/mailman/listinfo/aximx50-port

Reply via email to