1 /* drivers/input/ts/ts-auto.c - handle all touchscreen in this file
\r
3 * Copyright (C) 2012-2015 ROCKCHIP.
\r
4 * Author: luowei <lw@rock-chips.com>
\r
6 * This software is licensed under the terms of the GNU General Public
\r
7 * License version 2, as published by the Free Software Foundation, and
\r
8 * may be copied, distributed, and modified under those terms.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
17 #include <linux/interrupt.h>
\r
18 #include <linux/i2c.h>
\r
19 #include <linux/slab.h>
\r
20 #include <linux/irq.h>
\r
21 #include <linux/miscdevice.h>
\r
22 #include <linux/gpio.h>
\r
23 #include <asm/uaccess.h>
\r
24 #include <asm/atomic.h>
\r
25 #include <linux/delay.h>
\r
26 #include <linux/input.h>
\r
27 #include <linux/workqueue.h>
\r
28 #include <linux/freezer.h>
\r
29 #include <linux/input/mt.h>
\r
30 #include <mach/gpio.h>
\r
31 #include <mach/board.h>
\r
32 #ifdef CONFIG_HAS_EARLYSUSPEND
\r
33 #include <linux/earlysuspend.h>
\r
36 #include <linux/ts-auto.h>
\r
40 #define DBG(x...) printk(x)
\r
45 struct ts_private_data *g_ts;
\r
46 static struct class *g_ts_class;
\r
47 static struct ts_operate *g_ts_ops[TS_NUM_ID];
\r
51 * ts_reg_read: Read a single ts register.
\r
53 * @ts: Device to read from.
\r
54 * @reg: Register to read.
\r
56 int ts_reg_read(struct ts_private_data *ts, unsigned short reg)
\r
61 mutex_lock(&ts->io_lock);
\r
63 ret = ts->read_dev(ts, reg, ts->ops->reg_size, &val, ts->ops->reg_size);
\r
65 mutex_unlock(&ts->io_lock);
\r
72 EXPORT_SYMBOL_GPL(ts_reg_read);
\r
75 * ts_bulk_read: Read multiple ts registers
\r
77 * @ts: Device to read from
\r
78 * @reg: First register
\r
79 * @count: Number of registers
\r
80 * @buf: Buffer to fill.
\r
82 int ts_bulk_read(struct ts_private_data *ts, unsigned short reg,
\r
83 int count, unsigned char *buf)
\r
87 mutex_lock(&ts->io_lock);
\r
89 ret = ts->read_dev(ts, reg, count, buf, ts->ops->reg_size);
\r
91 mutex_unlock(&ts->io_lock);
\r
95 EXPORT_SYMBOL_GPL(ts_bulk_read);
\r
99 * ts_reg_write: Write a single ts register.
\r
101 * @ts: Device to write to.
\r
102 * @reg: Register to write to.
\r
103 * @val: Value to write.
\r
105 int ts_reg_write(struct ts_private_data *ts, unsigned short reg,
\r
106 unsigned short val)
\r
110 mutex_lock(&ts->io_lock);
\r
112 ret = ts->write_dev(ts, reg, ts->ops->reg_size, &val, ts->ops->reg_size);
\r
114 mutex_unlock(&ts->io_lock);
\r
118 EXPORT_SYMBOL_GPL(ts_reg_write);
\r
121 int ts_bulk_write(struct ts_private_data *ts, unsigned short reg,
\r
122 int count, unsigned char *buf)
\r
126 mutex_lock(&ts->io_lock);
\r
128 ret = ts->write_dev(ts, reg, count, buf, ts->ops->reg_size);
\r
130 mutex_unlock(&ts->io_lock);
\r
134 EXPORT_SYMBOL_GPL(ts_bulk_write);
\r
139 * ts_set_bits: Set the value of a bitfield in a ts register
\r
141 * @ts: Device to write to.
\r
142 * @reg: Register to write to.
\r
143 * @mask: Mask of bits to set.
\r
144 * @val: Value to set (unshifted)
\r
146 int ts_set_bits(struct ts_private_data *ts, unsigned short reg,
\r
147 unsigned short mask, unsigned short val)
\r
152 mutex_lock(&ts->io_lock);
\r
154 ret = ts->read_dev(ts, reg, ts->ops->reg_size, &r, ts->ops->reg_size);
\r
161 ret = ts->write_dev(ts, reg, ts->ops->reg_size, &r, ts->ops->reg_size);
\r
164 mutex_unlock(&ts->io_lock);
\r
168 EXPORT_SYMBOL_GPL(ts_set_bits);
\r
170 static int ts_get_id(struct ts_operate *ops, struct ts_private_data *ts, int *value)
\r
173 char temp[4] = {ops->id_reg & 0xff};
\r
176 DBG("%s:start\n",__func__);
\r
177 if(ops->id_reg >= 0)
\r
181 if(ops->reg_size == 2)
\r
183 temp[0] = ops->id_reg >> 8;
\r
184 temp[1] = ops->id_reg & 0xff;
\r
185 result = ts->read_dev(ts, ops->id_reg, 2, temp, ops->reg_size);
\r
186 *value = (temp[0] << 8) | temp[1];
\r
190 result = ts->read_dev(ts, ops->id_reg, 1, temp, ops->reg_size);
\r
201 if((ops->id_data != TS_UNKNOW_DATA)&&(ops->id_data != *value))
\r
203 printk("%s:id=0x%x is not 0x%x\n",__func__,*value, ops->id_data);
\r
207 DBG("%s:devid=0x%x\n",__func__,*value);
\r
214 static int ts_get_version(struct ts_operate *ops, struct ts_private_data *ts)
\r
217 char temp[TS_MAX_VER_LEN + 1] = {0};
\r
220 DBG("%s:start\n",__func__);
\r
222 if(ops->version_reg >= 0)
\r
224 if((ops->version_len < 0) || (ops->version_len > TS_MAX_VER_LEN))
\r
226 printk("%s:version_len %d is error\n",__func__,ops->version_len);
\r
227 ops->version_len = TS_MAX_VER_LEN;
\r
230 result = ts->read_dev(ts, ops->version_reg, ops->version_len, temp, ops->reg_size);
\r
234 if(ops->version_data)
\r
236 for(i=0; i<ops->version_len; i++)
\r
238 if(temp[i] == ops->version_data[i])
\r
240 printk("%s:version %s is not %s\n",__func__,temp, ops->version_data);
\r
245 DBG("%s:%s version: %s\n",__func__,ops->name, temp);
\r
252 static int ts_chip_init(struct ts_private_data *ts, int type)
\r
254 struct ts_operate *ops = NULL;
\r
258 if((type <= TS_BUS_TYPE_INVALID) || (type >= TS_BUS_TYPE_NUM_ID))
\r
260 printk("%s:type=%d is error\n",__func__,type);
\r
264 if(ts->pdata->init_platform_hw)
\r
265 ts->pdata->init_platform_hw();
\r
267 for(i=TS_ID_INVALID+1; i<TS_NUM_ID; i++)
\r
272 printk("%s:error:%p\n",__func__,ops);
\r
277 if(ops->bus_type == type)
\r
280 if(!ops->init || !ops->report)
\r
282 printk("%s:error:%p,%p\n",__func__,ops->init,ops->report);
\r
287 ts->ops = ops; //save ops
\r
289 result = ts_get_id(ops, ts, &ts->devid);//get id
\r
292 printk("%s:fail to read %s devid:0x%x\n",__func__, ops->name, ts->devid);
\r
296 result = ts_get_version(ops, ts); //get version
\r
299 printk("%s:fail to read %s version\n",__func__, ops->name);
\r
303 result = ops->init(ts);
\r
306 printk("%s:fail to init ts\n",__func__);
\r
312 result = ops->firmware(ts);
\r
315 printk("%s:fail to updata firmware ts\n",__func__);
\r
320 printk("%s:%s devid:0x%x\n",__func__, ts->ops->name, ts->devid);
\r
332 static int ts_get_data(struct ts_private_data *ts)
\r
334 return ts->ops->report(ts);
\r
338 static void ts_delaywork_func(struct work_struct *work)
\r
340 struct delayed_work *delaywork = container_of(work, struct delayed_work, work);
\r
341 struct ts_private_data *ts = container_of(delaywork, struct ts_private_data, delaywork);
\r
343 mutex_lock(&ts->ts_lock);
\r
344 if (ts_get_data(ts) < 0)
\r
345 DBG(KERN_ERR "%s: Get data failed\n",__func__);
\r
347 if(!ts->ops->irq_enable)//restart work while polling
\r
348 schedule_delayed_work(&ts->delaywork, msecs_to_jiffies(ts->ops->poll_delay_ms));
\r
351 if(ts->ops->check_irq)
\r
353 ts->ops->check_irq(ts);
\r
357 if((ts->ops->trig & IRQF_TRIGGER_LOW) || (ts->ops->trig & IRQF_TRIGGER_HIGH))
\r
358 enable_irq(ts->irq);
\r
361 mutex_unlock(&ts->ts_lock);
\r
363 DBG("%s:%s\n",__func__,ts->i2c_id->name);
\r
367 * This is a threaded IRQ handler so can access I2C/SPI. Since all
\r
368 * interrupts are clear on read the IRQ line will be reasserted and
\r
369 * the physical IRQ will be handled again if another interrupt is
\r
370 * asserted while we run - in the normal course of events this is a
\r
371 * rare occurrence so we save I2C/SPI reads. We're also assuming that
\r
372 * it's rare to get lots of interrupts firing simultaneously so try to
\r
375 static irqreturn_t ts_interrupt(int irq, void *dev_id)
\r
377 struct ts_private_data *ts = (struct ts_private_data *)dev_id;
\r
380 //if (ts_get_data(ts->client) < 0)
\r
381 // DBG(KERN_ERR "%s: Get data failed\n",__func__);
\r
382 //msleep(ts->ops->poll_delay_ms);
\r
383 if(ts->ops->check_irq)
\r
385 disable_irq_nosync(irq);
\r
389 if((ts->ops->trig & IRQF_TRIGGER_LOW) || (ts->ops->trig & IRQF_TRIGGER_HIGH))
\r
390 disable_irq_nosync(irq);
\r
392 schedule_delayed_work(&ts->delaywork, msecs_to_jiffies(ts->ops->poll_delay_ms));
\r
393 DBG("%s:irq=%d\n",__func__,irq);
\r
394 return IRQ_HANDLED;
\r
398 static int ts_irq_init(struct ts_private_data *ts)
\r
402 if((ts->ops->irq_enable)&&(ts->ops->trig != TS_UNKNOW_DATA))
\r
404 INIT_DELAYED_WORK(&ts->delaywork, ts_delaywork_func);
\r
405 if(ts->ops->poll_delay_ms < 0)
\r
406 ts->ops->poll_delay_ms = 30;
\r
408 //result = gpio_request(ts->irq, ts->i2c_id->name);
\r
411 // printk("%s:fail to request gpio :%d\n",__func__,ts->irq);
\r
414 gpio_pull_updown(ts->irq, PullEnable);
\r
415 irq = gpio_to_irq(ts->irq);
\r
416 result = request_irq(irq, ts_interrupt, ts->ops->trig, ts->ops->name, ts);
\r
417 //result = request_threaded_irq(irq, NULL, ts_interrupt, ts->ops->trig, ts->ops->name, ts);
\r
419 printk(KERN_ERR "%s:fail to request irq = %d, ret = 0x%x\n",__func__, irq, result);
\r
423 printk("%s:use irq=%d\n",__func__,irq);
\r
425 else if(!ts->ops->irq_enable)
\r
427 INIT_DELAYED_WORK(&ts->delaywork, ts_delaywork_func);
\r
428 if(ts->ops->poll_delay_ms < 0)
\r
429 ts->ops->poll_delay_ms = 30;
\r
431 schedule_delayed_work(&ts->delaywork, msecs_to_jiffies(ts->ops->poll_delay_ms));
\r
432 printk("%s:use polling,delay=%d ms\n",__func__,ts->ops->poll_delay_ms);
\r
441 int ts_device_init(struct ts_private_data *ts, int type, int irq)
\r
443 struct ts_platform_data *pdata = ts->dev->platform_data;
\r
444 int result = -1, i;
\r
446 mutex_init(&ts->io_lock);
\r
447 mutex_init(&ts->ts_lock);
\r
448 dev_set_drvdata(ts->dev, ts);
\r
451 result = ts_chip_init(ts, type);
\r
454 printk("%s:touch screen with bus type %d is not exist\n",__func__,type);
\r
455 goto out_free_memory;
\r
458 ts->input_dev = input_allocate_device();
\r
459 if (!ts->input_dev) {
\r
462 "Failed to allocate input device %s\n", ts->input_dev->name);
\r
463 goto out_free_memory;
\r
466 ts->input_dev->dev.parent = ts->dev;
\r
467 ts->input_dev->name = ts->ops->name;
\r
469 result = input_register_device(ts->input_dev);
\r
472 "Unable to register input device %s\n", ts->input_dev->name);
\r
473 goto out_input_register_device_failed;
\r
476 result = ts_irq_init(ts);
\r
479 "fail to init ts irq,ret=%d\n",result);
\r
480 goto out_input_register_device_failed;
\r
483 __set_bit(EV_ABS, ts->input_dev->evbit);
\r
484 __set_bit(EV_KEY, ts->input_dev->evbit);
\r
485 __set_bit(EV_REP, ts->input_dev->evbit);
\r
486 __set_bit(INPUT_PROP_DIRECT, ts->input_dev->propbit);
\r
487 set_bit(ABS_MT_POSITION_X, ts->input_dev->absbit);
\r
488 set_bit(ABS_MT_POSITION_Y, ts->input_dev->absbit);
\r
489 set_bit(ABS_MT_TOUCH_MAJOR, ts->input_dev->absbit);
\r
490 set_bit(ABS_MT_WIDTH_MAJOR, ts->input_dev->absbit);
\r
492 if(ts->ops->max_point <= 0)
\r
493 ts->ops->max_point = 1;
\r
495 input_mt_init_slots(ts->input_dev, ts->ops->max_point);
\r
497 if((ts->ops->range[0] <= 0) || (ts->ops->range[1] <= 0))
\r
499 ts->ops->range[0] = 1024;
\r
500 ts->ops->range[1] = 600;
\r
503 input_set_abs_params(ts->input_dev,ABS_MT_POSITION_X, 0, ts->ops->range[0], 0, 0);
\r
504 input_set_abs_params(ts->input_dev,ABS_MT_POSITION_Y, 0, ts->ops->range[1], 0, 0);
\r
505 input_set_abs_params(ts->input_dev,ABS_MT_TOUCH_MAJOR, 0, 10, 0, 0);
\r
506 input_set_abs_params(ts->input_dev,ABS_MT_WIDTH_MAJOR, 0, 10, 0, 0);
\r
510 printk("%s:initialized ok,ts name:%s,devid=%d\n\n",__func__,ts->ops->name,ts->devid);
\r
514 out_misc_device_register_device_failed:
\r
515 input_unregister_device(ts->input_dev);
\r
516 out_input_register_device_failed:
\r
517 input_free_device(ts->input_dev);
\r
521 printk("%s:line=%d\n",__func__,__LINE__);
\r
526 void ts_device_exit(struct ts_private_data *ts)
\r
528 if(!ts->ops->irq_enable)
\r
529 cancel_delayed_work_sync(&ts->delaywork);
\r
530 input_unregister_device(ts->input_dev);
\r
531 input_free_device(ts->input_dev);
\r
532 #ifdef CONFIG_HAS_EARLYSUSPEND
\r
533 if((ts->ops->suspend) && (ts->ops->resume))
\r
534 unregister_early_suspend(&ts->early_suspend);
\r
540 int ts_device_suspend(struct ts_private_data *ts)
\r
542 if(ts->ops->suspend)
\r
543 ts->ops->suspend(ts);
\r
545 if(ts->ops->irq_enable)
\r
546 disable_irq_nosync(ts->irq);
\r
548 cancel_delayed_work_sync(&ts->delaywork);
\r
554 int ts_device_resume(struct ts_private_data *ts)
\r
556 if(ts->ops->resume)
\r
557 ts->ops->resume(ts);
\r
559 if(ts->ops->irq_enable)
\r
560 enable_irq(ts->irq);
\r
563 PREPARE_DELAYED_WORK(&ts->delaywork, ts_delaywork_func);
\r
564 schedule_delayed_work(&ts->delaywork, msecs_to_jiffies(ts->ops->poll_delay_ms));
\r
572 int ts_register_slave(struct ts_private_data *ts,
\r
573 struct ts_platform_data *slave_pdata,
\r
574 struct ts_operate *(*get_ts_ops)(void))
\r
577 struct ts_operate *ops = get_ts_ops();
\r
578 if((ops->ts_id >= TS_NUM_ID) || (ops->ts_id <= TS_ID_INVALID))
\r
580 printk("%s:%s id is error %d\n", __func__, ops->name, ops->ts_id);
\r
583 g_ts_ops[ops->ts_id] = ops;
\r
584 printk("%s:%s,id=%d\n",__func__,g_ts_ops[ops->ts_id]->name, ops->ts_id);
\r
589 int ts_unregister_slave(struct ts_private_data *ts,
\r
590 struct ts_platform_data *slave_pdata,
\r
591 struct ts_operate *(*get_ts_ops)(void))
\r
594 struct ts_operate *ops = get_ts_ops();
\r
595 if((ops->ts_id >= TS_NUM_ID) || (ops->ts_id <= TS_ID_INVALID))
\r
597 printk("%s:%s id is error %d\n", __func__, ops->name, ops->ts_id);
\r
600 printk("%s:%s,id=%d\n",__func__,g_ts_ops[ops->ts_id]->name, ops->ts_id);
\r
601 g_ts_ops[ops->ts_id] = NULL;
\r
606 MODULE_AUTHOR("ROCKCHIP Corporation:lw@rock-chips.com");
\r
607 MODULE_DESCRIPTION("device interface for auto touch screen");
\r
608 MODULE_LICENSE("GPL");
\r