mini6410一条线触摸屏驱动(注释)

#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/serio.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/miscdevice.h>
#include <linux/timer.h>
#include <linux/param.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <plat/regs-timer.h>
    
#include <plat/gpio-cfg.h>
#include <mach/regs-clock.h>
#include <mach/regs-gpio.h>

#include <mach/gpio-bank-e.h>
#include <mach/gpio-bank-f.h>

#include <linux/cdev.h>

#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/err.h>
#include <linux/io.h>

#include <asm/system.h>
#include <asm/leds.h>
#include <asm/mach-types.h>

#include <asm/irq.h>
#include <mach/map.h>
#include <mach/regs-irq.h>
#include <asm/mach/time.h>

#include <plat/clock.h>
#include <plat/cpu.h>

#undef DEBUG
#define DEBUG
#ifdef DEBUG
#define DPRINTK(x...) {printk("%s(%d): ",__FUNCTION__ ,__LINE__);printk(x);}
#else
#define DPRINTK(x...) (void)(0)
#endif

#define TOUCH_DEVICE_NAME    "touchscreen-1wire"
#define BACKLIGHT_DEVICE_NAME    "backlight-1wire"
#define SAMPLE_BPS 9600

#define SLOW_LOOP_FEQ 25
#define FAST_LOOP_FEQ 60

#define REQ_TS   0x40U
#define REQ_INFO 0x60U

/***************Touch Screen driver user interface*******begin**********************/
//
static DECLARE_WAIT_QUEUE_HEAD(ts_waitq);
static int ts_ready;
static unsigned ts_status;

static inline void notify_ts_data(unsigned x, unsigned y, unsigned down)
{
    if (!down && !(ts_status &(1U << 31))) {
        // up repeat, give it up
        return;
    }

    ts_status = ((x << 16) | (y)) | (down << 31); //把x y 坐标整合在一个变量中
    ts_ready = 1;
    wake_up_interruptible(&ts_waitq); //唤醒read中的等待队列
}

static ssize_t ts_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
    unsigned long err;

    if (!ts_ready) {
        if (filp->f_flags & O_NONBLOCK)
            return -EAGAIN;
        else
            wait_event_interruptible(ts_waitq, ts_ready);/ /在notify_ts_data唤醒
    }

    ts_ready = 0;

    if (count < sizeof ts_status) {
        return -EINVAL;
    } else {
        count = sizeof ts_status;
    }

    err = copy_to_user((void *)buffer, (const void *)(&ts_status), sizeof ts_status);
    return err ? -EFAULT : sizeof ts_status;
}

static unsigned int ts_poll( struct file *file, struct poll_table_struct *wait)
{
    unsigned int mask = 0;
    poll_wait(file, &ts_waitq, wait);
    if (ts_ready)
        mask |= POLLIN | POLLRDNORM;
    return mask;
}

static struct file_operations ts_fops = {
    owner:    THIS_MODULE,
    read:    ts_read,    
    poll:   ts_poll,
};

static struct miscdevice ts_misc = {
    .minor = 181,
    .name = TOUCH_DEVICE_NAME,
    .fops = &ts_fops,
};
/***************Touch Screen driver user interface*******end**********************/


/***************backlight driver user interface*******begin**********************/

static DECLARE_WAIT_QUEUE_HEAD(bl_waitq);
static int bl_ready;
static unsigned char backlight_req = 0;
static unsigned char backlight_init_success;

static inline void notify_bl_data(unsigned char a, unsigned char b, unsigned char c)
{
    bl_ready = 1;
    backlight_init_success = 1;
    wake_up_interruptible(&bl_waitq);
}

static ssize_t bl_write(struct file *file, const char *buffer, size_t count, loff_t * ppos)
{
    int ret;
    char buf[4] = {0, 0, 0, 0};
    unsigned v;
    unsigned len;

    if (count == 0) {
        return -EINVAL;
    }

    if (count > sizeof buf - 1) {
        len = sizeof buf - 1;
    } else {
        len = count;
    }
    ret = copy_from_user(buf, buffer, len);
    if (ret) {
        return -EFAULT;
    }
    if (sscanf(buf, "%u", &v) != 1) {
        return -EINVAL;
    }
    
    if (v > 127) {
        v = 127;
    }

    bl_ready = 0;
    backlight_req = v + 0x80U;

    ret = wait_event_interruptible_timeout(bl_waitq, bl_ready, HZ / 10); //背光等待队列,在notify_bl_data中wake_up
    if (ret < 0) {
        return ret;
    }
    if (ret == 0) {
        return -ETIMEDOUT;
    }

    return count;
}

static struct file_operations bl_fops = {
    owner:    THIS_MODULE,
    write:    bl_write,
};

static struct miscdevice bl_misc = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = BACKLIGHT_DEVICE_NAME,
    .fops = &bl_fops,
};
/***************backlight driver user interface*******end**********************/


// for query base info
//
static unsigned lcd_type, firmware_ver;

static inline void notify_info_data(unsigned char _lcd_type, unsigned char ver_year, unsigned char week)
{
    if (_lcd_type != 0xFF) {
        lcd_type = _lcd_type;
        firmware_ver = ver_year * 100 + week;
    }
}

// Pin access
//
static inline void set_pin_up(void)
{
    unsigned long tmp;
    tmp = readl(S3C64XX_GPFPUD);
    tmp &= ~(3U <<30);
    tmp |= (2U << 30);
    writel(tmp, S3C64XX_GPFPUD);
}

static inline void set_pin_as_input(void)
{
    unsigned long tmp;
    tmp = readl(S3C64XX_GPFCON);
    tmp &= ~(3 << 30);
    writel(tmp, S3C64XX_GPFCON);
}

static inline void set_pin_as_output(void)
{
    unsigned long tmp;
    tmp = readl(S3C64XX_GPFCON);
    tmp = (tmp & ~(3U << 30)) | (1U << 30);
    writel(tmp, S3C64XX_GPFCON);
}

static inline void set_pin_value(int v)
{
    unsigned long tmp;
    tmp = readl(S3C64XX_GPFDAT);
    if (v) {
        tmp |= (1 << 15);
    } else {
        tmp &= ~(1<<15);
    }
    writel(tmp, S3C64XX_GPFDAT);
}

static inline int get_pin_value(void)
{
    int v;
    unsigned long tmp;
    tmp = readl(S3C64XX_GPFDAT);
    v = !!(tmp & (1<<15));
    return v;
}

// CRC
//
static const unsigned char crc8_tab[] = {
0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15,
0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65,
0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
0xE0, 0xE7, 0xEE, 0xE9, 0xFC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, 0x1A, 0x1D, 0x14, 0x13,
0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB,
0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB,
0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3,
};

#define crc8_init(crc) ((crc) = 0XACU)
#define crc8(crc, v) ( (crc) = crc8_tab[(crc) ^(v)])

// once a session complete
static unsigned total_received, total_error;
static unsigned last_req, last_res;
//每次状态机完成一次都会执行这个函数,提交装态机采集的数据
static void one_wire_session_complete(unsigned char req, unsigned int res)
{
    unsigned char crc;
    const unsigned char *p = (const unsigned char*)&res;
    total_received ++;

    last_res = res;

    crc8_init(crc);
    crc8(crc, p[3]);
    crc8(crc, p[2]);
    crc8(crc, p[1]);
    if (crc != p[0]) {
        // CRC dismatch
        if (total_received > 100) {
            total_error++;
        }
        return;
    }
    switch(req) {
    case REQ_TS:
        {
            unsigned short x,y;
            unsigned pressed;
            x =  ((p[3] >>   4U) << 8U) + p[2];
            y =  ((p[3] &  0xFU) << 8U) + p[1];
            pressed = (x != 0xFFFU) && (y != 0xFFFU);
            notify_ts_data(x, y, pressed); //提交触摸屏数据  x y 和是否是按下
        }
        break;
    
    case REQ_INFO:
        notify_info_data(p[3], p[2], p[1]); //提交lcd信息等数据。传过去的是lcd_type和firmware_ver信息
        break;
    default:
        notify_bl_data(p[3], p[2], p[1]) ;//提交背光数据,这里传过去的数据对于背光设置来说是没有意义的。丢弃
        break;
    }
}


/*****************协议的核心部分*******begin*******************/
// one-wire protocol core
static unsigned long TCNT_FOR_SAMPLE_BIT;
static unsigned long TCNT_FOR_FAST_LOOP;
static unsigned long TCNT_FOR_SLOW_LOOP;
//HW timer的初始化
static int init_timer_for_1wire(void)
{
        unsigned long tcfg1;
        unsigned long tcfg0;

    unsigned prescale1_value;

    unsigned long pclk;
    struct clk *clk;

    // get pclk
    clk = clk_get(NULL, "timers") ;//获取时钟
    if (IS_ERR(clk)) {
        DPRINTK("ERROR to get PCLK\n");
        return -EIO;
    }
    pclk = clk_get_rate(clk);

    // get prescaler
        tcfg0 = __raw_readl(S3C2410_TCFG0);
    // we use system prescaler value because timer 4 uses same one
    prescale1_value = (tcfg0 >> 8) & 0xFF;

    // calc the TCNT_FOR_SAMPLE_BIT, that is one of the goal
    TCNT_FOR_SAMPLE_BIT = pclk / (prescale1_value + 1) / SAMPLE_BPS - 1;
    TCNT_FOR_FAST_LOOP  = pclk / (prescale1_value + 1) / FAST_LOOP_FEQ - 1;
    TCNT_FOR_SLOW_LOOP  = pclk / (prescale1_value + 1) / SLOW_LOOP_FEQ - 1;
    
    // select timer 3, the 2rd goal
        tcfg1 = __raw_readl(S3C2410_TCFG1);
    tcfg1 &= ~S3C2410_TCFG1_MUX3_MASK;
    writel(tcfg1, S3C2410_TCFG1);


    return 0;
}
//关闭Hw timer
static inline void stop_timer_for_1wire(void)
{
    unsigned long tcon;
        tcon = __raw_readl(S3C2410_TCON);
    tcon &= ~S3C2410_TCON_T3START;
    writel(tcon, S3C2410_TCON);
}
//HW timer状态机 状态值
enum {
    IDLE,
    START,
    REQUEST,
    WAITING,
    RESPONSE,
    STOPING,
} one_wire_status = IDLE;

static volatile unsigned int io_bit_count;
static volatile unsigned int io_data;
static volatile unsigned char one_wire_request;
//HW timer中断处理函数
static irqreturn_t timer_for_1wire_interrupt(int irq, void *dev_id)
{
    io_bit_count--;
    switch(one_wire_status) {
    case START:
        if (io_bit_count == 0) {
            io_bit_count = 16;
            one_wire_status = REQUEST;
        }
        break;

    case REQUEST:
        // Send a bit
        set_pin_value(io_data & (1U << 31)) ;//发送数据,16为数据。先发送高位字节
        io_data <<= 1;
        if (io_bit_count == 0) {
            io_bit_count = 2;
            one_wire_status = WAITING;
        }
        break;
        
    case WAITING:
        if (io_bit_count == 0) {//等待应答
            io_bit_count = 32;
            one_wire_status = RESPONSE;
        }
        if (io_bit_count == 1) {
            set_pin_as_input();
            set_pin_value(1);
        }
        break;
        
    case RESPONSE:
        // Get a bit
        io_data = (io_data << 1) | get_pin_value() ;//读取函32位数据,放在最低位开始放
        if (io_bit_count == 0) {
            io_bit_count = 2;
            one_wire_status = STOPING;
            set_pin_value(1);
            set_pin_as_output();
            one_wire_session_complete(one_wire_request, io_data) ;//提交数据
        }
        break;

    case STOPING:
        if (io_bit_count == 0) {
            one_wire_status = IDLE;
            stop_timer_for_1wire();/ /停止定时器中断。下次定时器定时时间到又开启
        }
        break;
        
    default:
        stop_timer_for_1wire();
    }
    return IRQ_HANDLED;
}
//HW timer结构体

static struct irqaction timer_for_1wire_irq = {
    .name    = "1-wire Timer Tick",
    .flags   = IRQF_DISABLED | IRQF_IRQPOLL,
    .handler = timer_for_1wire_interrupt,
    .dev_id  = &timer_for_1wire_irq,
};

//这个函数会被Linux timer调用,用于初始化HW timer状态机状态和开启HW timer

static void start_one_wire_session(unsigned char req)
{
    unsigned long tcon;
    unsigned long flags;
    if (one_wire_status != IDLE) {
        printk("one_wire_status: %d\n", one_wire_status);
        return;
    }

    one_wire_status = START;// IRQ_TIMER3中断处理函数状态及开始设置为START

    set_pin_value(1);
    set_pin_as_output();
    // IDLE to START
    {
        unsigned char crc;
        crc8_init(crc);
        crc8(crc, req);
        io_data = (req << 8) + crc;
        io_data <<= 16;
    }
    last_req = (io_data >> 16);
    one_wire_request = req; //这里只是保存一下值,one_wire_session_complete中会用到
    io_bit_count = 1;
    set_pin_as_output();

    writel(TCNT_FOR_SAMPLE_BIT, S3C2410_TCNTB(3));
    // init tranfer and start timer
        tcon = __raw_readl(S3C2410_TCON );//打开IRQ_TIMER3中断,因为每次提交一次数据后,STOPING状态是都会关闭IRQ_TIMER3中断
    tcon &= ~(0xF << 16);
    tcon |= S3C2410_TCON_T3MANUALUPD;
    writel(tcon, S3C2410_TCON);


    tcon |= S3C2410_TCON_T3START;
    tcon |= S3C2410_TCON_T3RELOAD;
    tcon &= ~S3C2410_TCON_T3MANUALUPD;

    local_irq_save(flags);
    writel(tcon, S3C2410_TCON);
    set_pin_value(0);
    local_irq_restore(flags);
}
/*****************协议的核心部分*******end*******************/


// poll the device
// following is Linux timer not HW timer
static int exitting;
static struct timer_list one_wire_timer;

//每次linux timer 时间到都会执行这个函数
void one_wire_timer_proc(unsigned long v)
{
    unsigned char req;
    if (exitting) {
        return;
    }
    one_wire_timer.expires = jiffies + HZ / 50; //定时时间设置 20ms,确保能发生几十次IRQ_TIMER3中断
    add_timer(&one_wire_timer); //添加定时器
    if (lcd_type == 0) {// lcd_type初始化为0,所以第一个定时时间到会进这个if ,以后都不会进这个if了
        req = REQ_INFO;
    } else if (!backlight_init_success) {// 第二个定时器时间会会进这个if,以后都不会进这个if了
        req = 127;
    } else if (backlight_req) {// 当在用户空间想改变背光的时候调用write,bl_write被调用,backlight_req被置为不为0,下一个定时时间到进这个if
        req = backlight_req;
        backlight_req = 0; //对标志清零
    } else {/ /其他时候都进这里,触摸屏数据
        req = REQ_TS;
    }
    start_one_wire_session(req);

}

static struct timer_list one_wire_timer = {/ /linux定时器结构体
    .function = one_wire_timer_proc,
};

//用于写proc文件系统
static int read_proc(char *buf, char **start, off_t offset, int count, int *eof, void *data)
{
    int len;
    len = sprintf(buf, "%u %u %u %u %04X %08X\n", lcd_type, firmware_ver, total_received, total_error, last_req, last_res);
    *eof = 1;
    return len;
}

static int __init dev_init(void)
{
    int ret;
    ret = misc_register(&ts_misc) | misc_register(&bl_misc) ; //注册两个字符设备,一个是用于触摸屏数据读写,一个是用于控制背光
    set_pin_up();// 上拉
    set_pin_value(1);// 置为高点平
    set_pin_as_output();// 设置为输出模式

    if (ret == 0) {
        setup_irq(IRQ_TIMER3, &timer_for_1wire_irq);// 初始化一个定时器中断,用的中断线是IRQ_TIMER3  硬件时钟
        ret = init_timer_for_1wire();// 对这个中断进行初始化和时钟配置,到现在这个中断应该还没有打开
        init_timer(&one_wire_timer);// 初始化一个定时器
        one_wire_timer_proc(0);// 对这个定时器进行时间设置,start_one_wire_session函数会打开IRQ_TIMER3中断线
        create_proc_read_entry("driver/one-wire-info", 0, NULL, read_proc, NULL);
    }
    
    if (ret == 0) {
        printk (TOUCH_DEVICE_NAME"\tinitialized\n");
        printk (BACKLIGHT_DEVICE_NAME"\tinitialized\n");
    }
    return ret;
}


static void __exit dev_exit(void)
{
    exitting = 1;
    remove_proc_entry("driver/one-wire-info", NULL);
    del_timer_sync(&one_wire_timer);
    free_irq(IRQ_TIMER3, &timer_for_1wire_irq);
    misc_deregister(&ts_misc);
    misc_deregister(&bl_misc);
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
MODULE_DESCRIPTION("Mini6410 one-wire host and Touch Screen Driver");


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值