1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/slab.h>
5 #include <linux/proc_fs.h>
6 #include <linux/fs.h>
7 #include <linux/uaccess.h>
8 #include <linux/i2c.h>
9 #include <linux/interrupt.h>
10
11 #include "vtl_ts.h"
12 #include "chip.h"
13
14 #define APK_ITOUCH 0x01
15
16 #define INVALID 0x00
17 #define HW_RESET_CMD 0x01
18 #define CHIP_INFO_CMD 0x02
19 #define DRIVER_INFO_CMD 0x03
20 #define CHIP_ID_CMD 0x04
21 #define WRITE_CHIP_CMD 0x05
22 #define READ_CHIP_CMD 0x06
23 #define RECOVERY_CMD 0x07
24 #define INTERRUPT_CMD 0x08
25 #define READ_CHECKSUM_CMD 0x09
26 #define READ_FWCHECKSUM_CMD 0x0a
27 #define XY_DEBUG_CMD 0x0b
28
29
30 #define LINUX_SHELL 'c'
31 #define SHELL_CHECKSUM_CMD '1'
32 #define SHELL_UPDATE_CMD '2'
33 #define SHELL_DEBUG_CMD '3'
34
35
36
37 static struct ts_info * ts_object = NULL;
38
39 /*****************************************************************************
40 ** Function define
41 *****************************************************************************/
apk_i2c_transfer(struct i2c_client * client,unsigned char i2c_addr,unsigned char len,unsigned char * buf,unsigned char rw)42 static int apk_i2c_transfer(struct i2c_client *client,unsigned char i2c_addr,unsigned char len,unsigned char *buf,unsigned char rw)
43 {
44 struct i2c_msg msgs[1];
45
46 DEBUG();
47
48 msgs[0].flags = rw;
49 msgs[0].addr = i2c_addr;
50 msgs[0].len = len;
51 msgs[0].buf = buf;
52 //msgs[0].scl_rate = TS_I2C_SPEED; //only for rockchip
53
54 if(i2c_transfer(client->adapter, msgs, 1)!= 1)
55 {
56 return -1;
57 }
58 return 0;
59 }
60
61
62
apk_open(struct inode * inode,struct file * file)63 static int apk_open(struct inode *inode, struct file *file)
64 {
65 printk("___%s___\n",__func__);
66 DEBUG();
67
68 ts_object = vtl_ts_get_object();
69 if(ts_object == NULL)
70 {
71 return -1;
72 }
73 return 0;
74 }
75
apk_close(struct inode * inode,struct file * file)76 static int apk_close(struct inode *inode, struct file *file)
77 {
78 printk("___%s___\n",__func__);
79 DEBUG();
80
81 return 0;
82 }
apk_write(struct file * file,const char __user * buff,size_t count,loff_t * offp)83 static ssize_t apk_write(struct file *file, const char __user *buff, size_t count, loff_t *offp)
84 {
85 struct i2c_client *client = ts_object->driver->client;
86 unsigned char frame_data[255] = {0};
87 int bin_checksum = 0;
88 int fw_checksum = 0;
89 int ret = 0;
90
91 DEBUG();
92
93 if(copy_from_user(frame_data, buff, count)){
94 return -EFAULT;
95 }
96
97 if(frame_data[0]==APK_ITOUCH){
98
99 switch(frame_data[1]){
100
101 case HW_RESET_CMD :{
102 vtl_ts_hw_reset();
103 }break;
104
105 case INTERRUPT_CMD :{
106 if(frame_data[4]){
107 enable_irq(ts_object->config_info.irq_number);
108 }else{
109 disable_irq(ts_object->config_info.irq_number);
110 }
111 }break;
112
113 case RECOVERY_CMD :{
114 ret = update(client);
115 }break;
116
117 case WRITE_CHIP_CMD:{
118 ret = apk_i2c_transfer(client,frame_data[2],frame_data[3],&frame_data[4],0);
119 }break;
120
121 case XY_DEBUG_CMD :{
122 if(ts_object->debug){
123 ts_object->debug = 0x00;
124 }else{
125 ts_object->debug = 0x01;
126 }
127 }break;
128
129 default :{
130
131 }break;
132 }
133 }else if(frame_data[0]==LINUX_SHELL){
134 printk("CMD: %s,count = %zu\n",frame_data,count);
135 switch(frame_data[1]){
136 case SHELL_CHECKSUM_CMD :{
137 chip_get_checksum(client,&bin_checksum,&fw_checksum);
138 printk("bin_checksum = 0x%x,fw_checksum = 0x%x\n",bin_checksum,fw_checksum);
139 }break;
140
141 case SHELL_UPDATE_CMD :{
142 chip_update(client);
143 }break;
144
145 case SHELL_DEBUG_CMD :{
146 if(ts_object->debug){
147 ts_object->debug = 0x00;
148 }else{
149 ts_object->debug = 0x01;
150 }
151
152 }break;
153
154 default :{
155
156 }break;
157 }
158 }else{
159 return -1;
160 }
161 if(ret<0){
162 return -1;
163 }
164
165 return count;
166 }
167
apk_read(struct file * file,char __user * buff,size_t count,loff_t * offp)168 static ssize_t apk_read(struct file *file, char __user *buff, size_t count, loff_t *offp)
169 {
170 struct i2c_client *client = ts_object->driver->client;
171 unsigned char frame_data[255];
172 int bin_checksum = 0;
173 int fw_checksum = 0;
174 int len = 0;
175 int ret = 0;
176
177 DEBUG();
178 if(copy_from_user(frame_data, buff, count))
179 {
180 return -EFAULT;
181 }
182
183 len = frame_data[3];
184 if(frame_data[0]==APK_ITOUCH){
185
186 switch(frame_data[1]){
187 case DRIVER_INFO_CMD :{
188 frame_data[0] = client->addr;
189 }break;
190
191 case READ_CHIP_CMD :{
192
193 ret = apk_i2c_transfer(client,frame_data[2],frame_data[3],frame_data,1);
194 }break;
195
196 case READ_CHECKSUM_CMD :{
197 ret = chip_get_checksum(client,&bin_checksum,&fw_checksum);
198 frame_data[0] = bin_checksum & 0x00ff;
199 frame_data[1] = bin_checksum >> 8;
200 frame_data[2] = fw_checksum & 0x00ff;
201 frame_data[3] = fw_checksum >> 8;
202 }break;
203 case READ_FWCHECKSUM_CMD :{
204 ret = chip_get_fwchksum(client,&fw_checksum);
205 frame_data[0] = fw_checksum & 0x00ff;
206 frame_data[1] = fw_checksum >> 8;
207 }break;
208
209 default :{
210
211 }break;
212 }
213 }
214
215 if(copy_to_user(buff, frame_data,len)){
216 return -EFAULT;
217 }
218 if(ret<0){
219 return -1;
220 }
221
222 return count;
223 }
224
225 struct file_operations apk_fops = {
226 .owner = THIS_MODULE,
227 .open = apk_open,
228 .release = apk_close,
229 .write = apk_write,
230 .read = apk_read,
231 };
232
233