ckfwq/linux-3.0.4/drivers/input/touchscreen/gsc3280_1wire_host.c

652 lines
16 KiB
C

/*
* GSC3280 SoC ADC(SPI0) Controller Driver
*
* Copyright (C) 2013 BLX IC Design Corp.,Ltd.
* Author: Davied, apple_guet@126.com
*
*/
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <linux/types.h>
#include <linux/mutex.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/irqreturn.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pm.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/serio.h>
#include <linux/clk.h>
#include <linux/timer.h>
#include <linux/param.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/miscdevice.h>
#include <linux/types.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <gsc3280/gpio.h>
#include <gsc3280/timer.h>
#include <gsc3280/sysctl.h>
#include <gsc3280/gsc3280_int.h>
#ifdef CONFIG_GSC3280_TS_DEBUG
#define DBG(msg...) do { \
printk(KERN_INFO msg); \
} while (0)
#else
#define DBG(msg...) do { } while(0)
#endif
//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, 0xFB, 0xF2, 0xF5,
0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85,
0x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,
0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB,
0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3,
};
#define GSC3280_TS_NAME_SIZE 20
#define TS_1WIRE_NAME "ts-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
#define BL_INIT 0x7fU
#define crc8_init(crc) ((crc) = 0XACU)
#define crc8(crc, v) ((crc) = crc8_tab[(crc) ^(v)])
/* Touch screen coordinates */
#define GSC_X_MIN 0
#define GSC_X_MAX 4096
#define GSC_X_FUZZ 5
#define GSC_Y_MIN 0
#define GSC_Y_MAX 4096
#define GSC_Y_FUZZ 5
#define GSC_PRESSURE_MIN 0
#define GSC_PRESSURE_MAX 2000
#define TIMER_DELAY HZ / 40 //25ms
#define TS_1WIRE_DATA_PIN 22
typedef enum {
IDLE,
START,
REQUEST,
WAITING,
RESPONSE,
STOPING,
} one_wire_status_e;
/* Touch screen device structure */
struct ts_1wire_t {
u8 req;
u8 flg;
char phys[32];
char name[20];
volatile unsigned int io_data;
volatile unsigned int io_bit_count;
unsigned lcd_type;
volatile unsigned short x;
volatile unsigned short y;
volatile unsigned short old_x;
volatile unsigned short old_y;
volatile unsigned z;
volatile u8 ts_ready;
volatile u8 bl_ready;
u8 backlight_req;
u8 backlight_init_success;
volatile unsigned data;
one_wire_status_e one_wire_status;
spinlock_t slock;
struct device *dev;
struct input_dev *input;
struct list_head device_entry;
struct gsc3280_hard_timer ts_hard_timer;
};
#ifndef CONFIG_INPUT_TSDEV_SCREEN_X
#define CONFIG_INPUT_TSDEV_SCREEN_X 800
#endif
#ifndef CONFIG_INPUT_TSDEV_SCREEN_Y
#define CONFIG_INPUT_TSDEV_SCREEN_Y 480
#endif
static const int gXres = CONFIG_INPUT_TSDEV_SCREEN_X;
static const int gYres = CONFIG_INPUT_TSDEV_SCREEN_Y;
static LIST_HEAD(wire1_ts_list);
static DEFINE_MUTEX(wire1_ts_list_lock);
static struct timer_list ts_1wire_timer;
/* backlight driver user interface begin */
static DECLARE_WAIT_QUEUE_HEAD(bl_waitq);
/* lcd backlight control function */
int wire1_backlight_control(u8 bl_data)
{
int ret = -ENXIO;
struct ts_1wire_t *ts = NULL;
mutex_lock(&wire1_ts_list_lock);
list_for_each_entry(ts, &wire1_ts_list, device_entry) {
if(strcmp(ts->name, TS_1WIRE_NAME) == 0) {
ret = 0;
break;
}
}
mutex_unlock(&wire1_ts_list_lock);
if (ret != 0) {
return ret;
}
if (bl_data > 127) {
bl_data = 127;
}
ts->bl_ready = 0;
ts->backlight_req = bl_data + 0x80U;
ret = wait_event_interruptible_timeout(bl_waitq, ts->bl_ready, HZ);
if (ret < 0) {
printk(KERN_INFO "wait_event_interruptible_timeout error!\n");
return ret;
} else if (ret == 0) {
printk(KERN_INFO "time out error!\n");
return -ETIMEDOUT;
} else {
return 0;
}
}
EXPORT_SYMBOL(wire1_backlight_control);
static inline void notify_bl_data(struct ts_1wire_t *ts, u8 a, u8 b, u8 c)
{
ts->bl_ready = 1;
wake_up_interruptible(&bl_waitq);
}
static int bl_open(struct inode *inode, struct file *filp)
{
int status = -ENXIO;
struct ts_1wire_t *ts = NULL;
mutex_lock(&wire1_ts_list_lock);
list_for_each_entry(ts, &wire1_ts_list, device_entry) {
if(strcmp(ts->name, TS_1WIRE_NAME) == 0) {
status = 0;
break;
}
}
mutex_unlock(&wire1_ts_list_lock);
if (status == 0) {
filp->private_data = ts;
} else {
return status;
}
return nonseekable_open(inode, filp);
}
static ssize_t bl_write(struct file *filp, const char __user *buffer, size_t count, loff_t *ppos)
{
int ret = 0;
u8 bl_data = 0;
struct ts_1wire_t *ts = filp->private_data;
DBG("bl_write start\n");
if (get_user(bl_data, (u8 __user *)buffer)) {
printk(KERN_INFO "get_user error!\n");
return -EFAULT;
}
if (bl_data > 127) {
bl_data = 127;
}
ts->bl_ready = 0;
ts->backlight_req = bl_data + 0x80U;
ret = wait_event_interruptible_timeout(bl_waitq, ts->bl_ready, HZ);
if (ret < 0) {
printk(KERN_INFO "wait_event_interruptible_timeout error!\n");
return ret;
} else if (ret == 0) {
printk(KERN_INFO "time out error!\n");
return -ETIMEDOUT;
} else {
return count;
}
}
static struct file_operations bl_fops = {
owner : THIS_MODULE,
write : bl_write,
open : bl_open,
};
static struct miscdevice bl_misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = BACKLIGHT_DEVICE_NAME,
.fops = &bl_fops,
};
/* backlight driver user interface end */
static void gsc3280_report_event(struct ts_1wire_t *ts, u32 z)
{
#ifdef CONFIG_GSC3280_POS_PRINT
printk(KERN_INFO "x = %d\n", ts->x);
printk(KERN_INFO "y = %d\n", ts->y);
printk(KERN_INFO "z = %d\n", z);
#endif
input_report_abs(ts->input, ABS_PRESSURE, z);
input_report_abs(ts->input, ABS_X, ts->x);
input_report_abs(ts->input, ABS_Y, ts->y);
if (z > 0)
input_report_key(ts->input, BTN_TOUCH, 1);
else
input_report_key(ts->input, BTN_TOUCH, 0);
input_sync(ts->input);
}
static inline void notify_ts_data(struct ts_1wire_t *ts)
{
if (ts->z == 1) {
ts->x = ((ts->x -285) * gXres) / (3944 - 285);
ts->y = ((3936 - ts->y) * gYres) / (3936 - 102);
if ((ts->x > 0) && (ts->x < gXres) && (ts->y > 0) && (ts->y < gYres)) {
if (ts->flg == 0) {
ts->flg = 1;
gsc3280_report_event(ts, 0);
}
gsc3280_report_event(ts, ts->z);
ts->old_x = ts->x;
ts->old_y = ts->y;
}
} else if (ts->z == 0) {
if (ts->flg == 1) {
ts->flg = 0;
ts->x = ts->old_x;
ts->y = ts->old_y;
gsc3280_report_event(ts, 0);
}
}
}
static inline void notify_info_data(struct ts_1wire_t *ts, unsigned char lcd_type,
unsigned char ver_year, unsigned char week)
{
if (lcd_type != 0xFF) {
ts->lcd_type = lcd_type;
//firmware_ver = ver_year * 100 + week;
}
}
static void one_wire_session_complete(struct ts_1wire_t *ts)
{
u8 crc = 0;
const unsigned char *p = (const u8*)&(ts->io_data);
crc8_init(crc);
crc8(crc, p[3]);
crc8(crc, p[2]);
crc8(crc, p[1]);
if (crc != p[0]) {
DBG("one_wire_session_complete crc error\n");
return;
}
switch(ts->req) {
case REQ_TS:
ts->x = ((p[3] >> 4U) << 8U) + p[2];
ts->y = ((p[3] & 0xFU) << 8U) + p[1];
ts->z = (ts->x != 0xFFFU) && (ts->y != 0xFFFU);
notify_ts_data(ts);
break;
case REQ_INFO:
notify_info_data(ts, p[3], p[2], p[1]);
break;
default:
notify_bl_data(ts, p[3], p[2], p[1]);
break;
}
}
//Pin access
static inline void set_pin_up(void)
{
}
static inline void set_pin_as_input(void)
{
gpio_direction_input(TS_1WIRE_DATA_PIN);
}
static inline void set_pin_as_output(void)
{
gpio_direction_output(TS_1WIRE_DATA_PIN, 1);
}
static inline void set_pin_value(int v)
{
if (v) {
gpio_set_value(TS_1WIRE_DATA_PIN, 1);
} else {
gpio_set_value(TS_1WIRE_DATA_PIN, 0);
}
}
static inline int get_pin_value(void)
{
return gpio_get_value(TS_1WIRE_DATA_PIN);
}
static void ts_1wire_hardtimer_callback(unsigned long data)
{
struct ts_1wire_t *ts = (struct ts_1wire_t *)data;
//DBG("ts_1wire_hardtimer_callback start\n");
ts->io_bit_count--;
switch(ts->one_wire_status) {
case START:
//DBG("START\n");
if (ts->io_bit_count == 0) {
ts->io_bit_count = 16;
ts->one_wire_status = REQUEST;
}
break;
case REQUEST:
//Send a bit
//DBG("REQUEST\n");
set_pin_value(ts->io_data & (1U << 31));
ts->io_data <<= 1;
if (ts->io_bit_count == 0) {
ts->io_bit_count = 2;
ts->one_wire_status = WAITING;
}
break;
case WAITING:
//DBG("WAITING\n");
if (ts->io_bit_count == 0) {
ts->io_bit_count = 32;
ts->one_wire_status = RESPONSE;
}
else if (ts->io_bit_count == 1) {
set_pin_as_input();
set_pin_value(1);
}
break;
case RESPONSE:
//Get a bit
//DBG("RESPONSE\n");
ts->io_data = (ts->io_data << 1) | get_pin_value();
if (ts->io_bit_count == 0) {
ts->io_bit_count = 2;
ts->one_wire_status = STOPING;
set_pin_value(1);
set_pin_as_output();
one_wire_session_complete(ts);
}
break;
case STOPING:
//DBG("STOPING\n");
if (ts->io_bit_count == 0) {
ts->one_wire_status = IDLE;
gsc3280_timer_stop(&ts->ts_hard_timer);
mod_timer(&ts_1wire_timer, jiffies + TIMER_DELAY);
}
break;
default:
//DBG("default\n");
gsc3280_timer_stop(&ts->ts_hard_timer);
mod_timer(&ts_1wire_timer, jiffies + TIMER_DELAY);
break;
}
}
static void init_hard_timer_for_1wire(struct ts_1wire_t *ts)
{
ts->ts_hard_timer.type = LOOP;
ts->ts_hard_timer.value_type = 1;
ts->ts_hard_timer.bps = SAMPLE_BPS;
ts->ts_hard_timer.function = ts_1wire_hardtimer_callback;
ts->ts_hard_timer.data = (unsigned long)ts;
}
static void start_one_wire_session(struct ts_1wire_t *ts)
{
u8 crc = 0;
unsigned long flags = 0;
if (ts->one_wire_status != IDLE) {
DBG("one_wire_status: %d error!!!!\n", ts->one_wire_status);
return;
}
spin_lock_irqsave(&ts->slock, flags);
ts->one_wire_status = START; //IDLE to START
set_pin_value(1);
set_pin_as_output();
crc8_init(crc);
crc8(crc, ts->req);
ts->io_data = (ts->req << 8) + crc;
ts->io_data <<= 16;
ts->io_bit_count = 1;
set_pin_as_output();
spin_unlock_irqrestore(&ts->slock, flags);
local_irq_save(flags);
gsc3280_timer_start(&ts->ts_hard_timer);
set_pin_value(0);
local_irq_restore(flags);
}
static void one_wire_timer_callback(unsigned long data)
{
unsigned long flags = 0;
struct ts_1wire_t *ts = (struct ts_1wire_t *)data;
//mod_timer(&ts_1wire_timer, jiffies + TIMER_DELAY);
spin_lock_irqsave(&ts->slock, flags);
if (ts->lcd_type == 0) {
//DBG("REQ_INFO\n");
ts->req = REQ_INFO;
}
else if (!ts->backlight_init_success) {
//DBG("backlight_init_success\n");
ts->backlight_init_success = 1;
ts->req = BL_INIT;
}
else if (ts->backlight_req) {
//DBG("backlight_req\n");
ts->req = ts->backlight_req;
ts->backlight_req = 0;
} else {
//DBG("REQ_TS\n");
ts->req = REQ_TS;
}
spin_unlock_irqrestore(&ts->slock, flags);
start_one_wire_session(ts);
}
static int ts_1wire_probe(struct platform_device *pdev)
{
int ret = 0;
struct ts_1wire_t *ts = NULL;
struct input_dev *input = NULL;
DBG("############\n");
printk(KERN_INFO "ts 1wire probe start.\n");
ts = kzalloc(sizeof(struct ts_1wire_t), GFP_KERNEL);
if (!ts) {
DBG("kzalloc error\n");
return -ENOMEM;
}
input = input_allocate_device();
if (!input) {
ret = -ENOMEM;
goto err_free_mem;
}
spin_lock_init(&ts->slock);
ts->dev = &pdev->dev;
snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(ts->dev));
input->name = "h3600_ts";
input->phys = ts->phys;
input->dev.parent = ts->dev;
input->id.vendor = 0x00; //tsdev->vendor;
input->id.version = 0x00; //tsdev->rev;
input->id.product = 0x03; //tsdev->rev;
input->id.bustype = BUS_HOST; //should be spi
input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
__set_bit(EV_REP, input->evbit);
__set_bit(EV_KEY, input->evbit);
__set_bit(BTN_TOUCH, input->keybit);
__set_bit(EV_ABS, input->evbit);
__set_bit(ABS_X, input->absbit);
__set_bit(ABS_Y, input->absbit);
__set_bit(ABS_PRESSURE, input->absbit);
ts->input = input;
input_set_abs_params(ts->input, ABS_X, GSC_X_MIN, GSC_X_MAX, GSC_X_FUZZ, 0);
input_set_abs_params(ts->input, ABS_Y, GSC_Y_MIN, GSC_Y_MAX, GSC_Y_FUZZ, 0);
input_set_abs_params(ts->input, ABS_PRESSURE, GSC_PRESSURE_MIN, GSC_PRESSURE_MAX, 0, 0);
ret = input_register_device(ts->input);
if (ret) {
DBG("!!!!input register device error!\n");
goto err_free_input;
}
strlcpy(ts->name, TS_1WIRE_NAME, sizeof(ts->name));
ret = gpio_request(TS_1WIRE_DATA_PIN, "TS_1WIRE_DATA_PIN");
if (ret) {
DBG("gpio request error!\n");
goto err_free_input;
}
set_pin_up();
set_pin_value(1);
set_pin_as_output();
init_hard_timer_for_1wire(ts);
ret = gsc3280_request_hard_timer(&ts->ts_hard_timer);
if (ret) {
DBG("gsc3280 request hard timer error\n");
goto err_gpio_req;
}
ret = misc_register(&bl_misc);
if (ret != 0) {
DBG("misc_register bl_misc error!\n");
goto err_hard_timer_req;
}
ts->one_wire_status = IDLE;
init_timer(&ts_1wire_timer);
ts_1wire_timer.data = (unsigned long)ts;
ts_1wire_timer.expires = jiffies + TIMER_DELAY;
ts_1wire_timer.function = one_wire_timer_callback;
add_timer(&ts_1wire_timer);
mutex_lock(&wire1_ts_list_lock);
list_add(&ts->device_entry, &wire1_ts_list);
mutex_unlock(&wire1_ts_list_lock);
platform_set_drvdata(pdev, ts);
printk(KERN_INFO "ts 1wire probe success.\n");
DBG("############\n");
return 0;
err_hard_timer_req:
gsc3280_free_hard_timer(&ts->ts_hard_timer);
err_gpio_req:
gpio_free(TS_1WIRE_DATA_PIN);
err_free_input:
input_free_device(input);
err_free_mem:
kfree(ts);
printk(KERN_INFO "!!!!ts 1wire probe error!!!!!\n");
return ret;
}
static int ts_1wire_remove(struct platform_device *pdev)
{
struct ts_1wire_t *ts = platform_get_drvdata(pdev);;
platform_set_drvdata(pdev, NULL);
del_timer_sync(&ts_1wire_timer);
gpio_free(TS_1WIRE_DATA_PIN);
misc_deregister(&bl_misc);
kfree(ts);
printk(KERN_INFO "ts 1wire remove.\n");
return 0;
}
static struct platform_driver ts_1wire_device_driver = {
.probe = ts_1wire_probe,
.remove = __devexit_p(ts_1wire_remove),
.driver = {
.name = "ts_1wire_device",
.owner = THIS_MODULE,
}
};
static int __init ts_1wire_init(void)
{
return platform_driver_register(&ts_1wire_device_driver);
}
static void __exit ts_1wire_exit(void)
{
platform_driver_unregister(&ts_1wire_device_driver);
}
module_init(ts_1wire_init);
module_exit(ts_1wire_exit);
MODULE_AUTHOR("Davied<apple_guet@126.com>");
MODULE_DESCRIPTION("GSC3280 one wire ts Driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("gsc3280 one wire ts");