1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ELAN HID-I2C TouchScreen driver.
4 *
5 * Copyright (C) 2014 Elan Microelectronics Corporation.
6 *
7 * Author: Chuming Zhang <chuming.zhang@elanic.com.cn>
8 */
9
10 #include "elan_ts.h"
11 #include <linux/of_gpio.h>
12 #include <linux/buffer_head.h>
13 #include <linux/types.h>
14 #include <linux/irq.h>
15 #include <linux/interrupt.h>
16
17 #define IntToASCII(c) (c)>9?((c)+0x37):((c)+0x30)
18
store_disable_irq(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)19 static ssize_t store_disable_irq(struct device *dev,
20 struct device_attribute *attr,
21 const char *buf, size_t count)
22 {
23 struct i2c_client *client = to_i2c_client(dev);
24 struct elan_ts_data *ts = i2c_get_clientdata(client);
25 /*
26 if (ts->irq_lock_flag == 0) {
27 disable_irq(ts->hw_info.irq_num);
28 ts->irq_lock_flag = 1;
29 }
30 */
31 elan_switch_irq(ts, 0);
32 dev_info(&client->dev, "Disable IRQ.\n");
33 return count;
34 }
35 static DEVICE_ATTR(disable_irq, S_IWUSR, NULL, store_disable_irq);
36
store_enable_irq(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)37 static ssize_t store_enable_irq(struct device *dev,
38 struct device_attribute *attr,
39 const char *buf, size_t count)
40 {
41 struct i2c_client *client = to_i2c_client(dev);
42 struct elan_ts_data *ts = i2c_get_clientdata(client);
43 /*
44 if (ts->irq_lock_flag == 1) {
45 enable_irq(ts->hw_info.irq_num);
46 ts->irq_lock_flag = 0;
47 }
48 */
49 elan_switch_irq(ts, 1);
50 dev_info(&client->dev, "Enable IRQ.\n");
51 return count;
52 }
53 static DEVICE_ATTR(enable_irq, S_IWUSR, NULL, store_enable_irq);
54
55
store_reset(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)56 static ssize_t store_reset(struct device *dev,
57 struct device_attribute *attr,
58 const char *buf, size_t count)
59 {
60 struct i2c_client *client = to_i2c_client(dev);
61 struct elan_ts_data *ts = i2c_get_clientdata(client);
62
63 elan_ts_hw_reset(&ts->hw_info);
64 dev_info(&client->dev,
65 "Reset Touch Screen Controller!\n");
66 return count;
67 }
68 static DEVICE_ATTR(reset, S_IWUSR, NULL, store_reset);
69
show_gpio_int(struct device * dev,struct device_attribute * attr,char * buf)70 static ssize_t show_gpio_int(struct device *dev,
71 struct device_attribute *attr, char *buf)
72 {
73 struct i2c_client *client = to_i2c_client(dev);
74 struct elan_ts_data *ts = i2c_get_clientdata(client);
75
76 return sprintf(buf, "%d\n",
77 gpio_get_value(ts->hw_info.intr_gpio));
78 }
79 static DEVICE_ATTR(gpio_int, S_IRUGO, show_gpio_int, NULL);
80
store_calibrate(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)81 static ssize_t store_calibrate(struct device *dev,
82 struct device_attribute *attr,
83 const char *buf, size_t count)
84 {
85 struct i2c_client *client = to_i2c_client(dev);
86 struct elan_ts_data *ts = i2c_get_clientdata(client);
87 int ret = 0;
88 /*
89 if (ts->irq_lock_flag == 0) {
90 disable_irq(ts->hw_info.irq_num);
91 ts->irq_lock_flag = 1;
92 }
93 */
94 elan_switch_irq(ts, 0);
95 ret = elan_ts_calibrate(client);
96
97 if (ret == 0)
98 dev_info(&client->dev, "ELAN CALIBRATE Success\n");
99 else
100 dev_err(&client->dev, "ELAN CALIBRATE Fail\n");
101 /*
102 if (ts->irq_lock_flag == 1) {
103 enable_irq(ts->hw_info.irq_num);
104 ts->irq_lock_flag = 0;
105 }
106 */
107 elan_switch_irq(ts, 1);
108 return count;
109 }
110 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, store_calibrate);
111
112
store_check_rek(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)113 static ssize_t store_check_rek(struct device *dev,
114 struct device_attribute *attr,
115 const char *buf, size_t count)
116 {
117 struct i2c_client *client = to_i2c_client(dev);
118 struct elan_ts_data *ts = i2c_get_clientdata(client);
119 int ret = 0;
120
121 /*
122 if (ts->irq_lock_flag == 0) {
123 disable_irq(ts->hw_info.irq_num);
124 ts->irq_lock_flag = 1;
125 }
126 */
127 elan_switch_irq(ts, 0);
128 ret = elan_ts_check_calibrate(client);
129
130 if (ret)
131 dev_err(&client->dev, "ELAN CALIBRATE CHECK Fail\n");
132 else
133 dev_info(&client->dev, "ELAN CHECK CALIBRATE Success\n");
134 /*
135 if (ts->irq_lock_flag == 1) {
136 enable_irq(ts->hw_info.irq_num);
137 ts->irq_lock_flag = 0;
138 }
139 */
140 elan_switch_irq(ts, 1);
141 return count;
142 }
143 static DEVICE_ATTR(check_rek, S_IWUSR, NULL, store_check_rek);
144
show_fw_info(struct device * dev,struct device_attribute * attr,char * buf)145 static ssize_t show_fw_info(struct device *dev,
146 struct device_attribute *attr, char *buf)
147 {
148 struct i2c_client *client = to_i2c_client(dev);
149 struct elan_ts_data *ts = i2c_get_clientdata(client);
150
151 return sprintf(buf, "FW VER = 0x%04x, FW ID = 0x%04x, BC VER = 0x%04x, tx:rx = %d:%d\n",\
152 ts->fw_info.fw_ver, ts->fw_info.fw_id, ts->fw_info.fw_bcv, ts->fw_info.tx, ts->fw_info.rx);
153 }
store_fw_info(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)154 static ssize_t store_fw_info(struct device *dev,
155 struct device_attribute *attr,
156 const char *buf, size_t count)
157 {
158 struct i2c_client *client = to_i2c_client(dev);
159 struct elan_ts_data *ts = i2c_get_clientdata(client);
160 /*
161 if (ts->irq_lock_flag == 0) {
162 disable_irq(ts->hw_info.irq_num);
163 ts->irq_lock_flag = 1;
164 }
165 */
166 elan_switch_irq(ts, 0);
167
168 elan__fw_packet_handler(ts->client);
169 /*
170 if (ts->irq_lock_flag == 1) {
171 enable_irq(ts->hw_info.irq_num);
172 ts->irq_lock_flag = 0;
173 }
174 */
175 elan_switch_irq(ts, 1);
176
177 return count;
178 }
179 static DEVICE_ATTR(fw_info, S_IWUSR | S_IRUSR, show_fw_info, store_fw_info);
180
store_iap_status(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)181 static ssize_t store_iap_status(struct device *dev,
182 struct device_attribute *attr,
183 const char *buf, size_t count)
184 {
185 struct i2c_client *client = to_i2c_client(dev);
186 struct elan_ts_data *ts = i2c_get_clientdata(client);
187 /*
188 if (ts->irq_lock_flag == 0) {
189 disable_irq(ts->hw_info.irq_num);
190 ts->irq_lock_flag = 1;
191 }
192 */
193 elan_switch_irq(ts, 0);
194 elan_ts_hw_reset(&ts->hw_info);
195 mdelay(200);
196 elan__hello_packet_handler(client, ts->chip_type);
197
198 /*
199 if (ts->irq_lock_flag == 1) {
200 enable_irq(ts->hw_info.irq_num);
201 ts->irq_lock_flag = 0;
202 }
203 */
204 elan_switch_irq(ts, 1);
205 return count;
206 }
207
show_iap_status(struct device * dev,struct device_attribute * attr,char * buf)208 static ssize_t show_iap_status(struct device *dev,
209 struct device_attribute *attr, char *buf)
210 {
211
212 struct i2c_client *client = to_i2c_client(dev);
213 struct elan_ts_data *ts = i2c_get_clientdata(client);
214
215 return sprintf(buf, "IAP STATUS = %s\n",(ts->recover < 0 ? "UNKNOW":(ts->recover == 0x01 ? "RECOVERY":"NORMAL")));
216 }
217 static DEVICE_ATTR(iap_status, S_IWUSR | S_IRUSR, show_iap_status, store_iap_status);
218
store_fw_upgrade(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)219 static ssize_t store_fw_upgrade(struct device *dev,
220 struct device_attribute *attr,
221 const char *buf, size_t count)
222 {
223 struct i2c_client *client = to_i2c_client(dev);
224 struct elan_ts_data *ts = i2c_get_clientdata(client);
225 /*
226 if (ts->irq_lock_flag == 0) {
227 disable_irq(ts->hw_info.irq_num);
228 ts->irq_lock_flag = 1;
229 }
230 */
231 elan_switch_irq(ts, 0);
232 // elan_get_vendor_fw(ts,ts->fw_store_type);
233 // elan_FW_Update(ts->client);
234 elan_check_update_flage(ts);
235 /*
236 if (ts->irq_lock_flag == 1) {
237 enable_irq(ts->hw_info.irq_num);
238 ts->irq_lock_flag = 0;
239 }
240 */
241 elan_switch_irq(ts, 1);
242 return count;
243 }
244 static DEVICE_ATTR(fw_update, S_IWUSR, NULL, store_fw_upgrade);
245
246
show_fw_store(struct device * dev,struct device_attribute * attr,char * buf)247 static ssize_t show_fw_store(struct device *dev,
248 struct device_attribute *attr, char *buf)
249 {
250 struct i2c_client *client = to_i2c_client(dev);
251 struct elan_ts_data *ts = i2c_get_clientdata(client);
252
253 if (ts->fw_store_type > -1 && ts->fw_store_type < 3) {
254 return sprintf(buf, "FW STORE = %s\n",\
255 (ts->fw_store_type == FROM_SYS_ETC_FIRMWARE ? "/system/etc/firmware/elants_i2c.ekt":\
256 (ts->fw_store_type == FROM_SDCARD_FIRMWARE ? "/data/local/tmp/elants_i2c.ekt":"build in driver")));
257 } else {
258 return sprintf(buf, "FW STORE TYPE OUT OF RANGE\n");
259 }
260 }
261
262
store_fw_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)263 static ssize_t store_fw_store(struct device *dev,
264 struct device_attribute *attr,
265 const char *buf, size_t count)
266 {
267
268 struct i2c_client *client = to_i2c_client(dev);
269 struct elan_ts_data *ts = i2c_get_clientdata(client);
270 int type;
271
272 sscanf(buf,"%d",&type);
273 if (type > -1 && type < 3) {
274 ts->fw_store_type = type;
275 } else {
276 dev_info(&client->dev, "[elan] fw store type out of range!!!\n");
277 }
278 return count;
279
280 }
281 static DEVICE_ATTR(fw_store, S_IWUSR | S_IRUSR, show_fw_store, store_fw_store);
282
283 #if 0
284 static ssize_t store_tp_module_test(struct device *dev,
285 struct device_attribute *attr,
286 const char *buf, size_t count)
287 {
288
289 struct i2c_client *client = to_i2c_client(dev);
290 struct elan_ts_data *ts = i2c_get_clientdata(client);
291 int ret = 0;
292
293 ret = elan_tp_module_test(ts);
294 return count;
295 }
296 static DEVICE_ATTR(elan_tp_module_test, S_IWUSR, NULL, store_tp_module_test);
297 #endif
store_tp_print_level(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)298 static ssize_t store_tp_print_level(struct device *dev,
299 struct device_attribute *attr,
300 const char *buf, size_t count)
301 {
302 int level = 0;
303 struct i2c_client *client = to_i2c_client(dev);
304 struct elan_ts_data *ts = i2c_get_clientdata(client);
305
306 sscanf(buf, "%x", &level);
307 ts->level = level;
308 return count;
309 }
310
311
show_tp_print_level(struct device * dev,struct device_attribute * attr,char * buf)312 static ssize_t show_tp_print_level(struct device *dev,
313 struct device_attribute *attr, char *buf)
314 {
315 struct i2c_client *client = to_i2c_client(dev);
316 struct elan_ts_data *ts = i2c_get_clientdata(client);
317
318 return sprintf(buf, "PRINT LEVEL = %s\n",\
319 (ts->level == TP_DEBUG ? "DEBUG":(ts->level == TP_INFO ? "INFO" : (ts->level == TP_WARNING ?"WARNING":"ERROR"))));
320 }
321 static DEVICE_ATTR(tp_print_level, S_IWUSR | S_IRUSR, show_tp_print_level, store_tp_print_level);
322
store_tp_cmd_send(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)323 static ssize_t store_tp_cmd_send(struct device *dev,
324 struct device_attribute *attr,
325 const char *buf, size_t count)
326 {
327
328 int valid_size = 0;
329 struct i2c_client *client = to_i2c_client(dev);
330 struct elan_ts_data *ts = i2c_get_clientdata(client);
331 char cmd[37] = {0x04, 0x00, 0x23, 0x00, 0x03};
332 int ret = 0;
333
334 sscanf(buf, "%d:%x:%x:%x:%x:%x:%x:%x:%x", &valid_size,\
335 (int *)&cmd[5], (int *)&cmd[6], (int *)&cmd[7], (int *)&cmd[8],\
336 (int *)&cmd[9], (int *)&cmd[10], (int *)&cmd[11], (int *)&cmd[12]);
337 /*
338 if (ts->irq_lock_flag == 0) {
339 disable_irq(ts->hw_info.irq_num);
340 ts->irq_lock_flag = 1;
341 }
342 */
343 elan_switch_irq(ts, 0);
344 dev_info(&client->dev, "cmd: %x:%x:%x:%x:%x:%x:%x:%x\n",cmd[5],cmd[6],cmd[7],cmd[8],cmd[9],cmd[10],cmd[11],cmd[12]);
345
346
347 if (ts->chip_type == HID_TYPE_PROTOCOL) {
348 ret = ts->ops->send(cmd, sizeof(cmd));
349 if (ret != sizeof(cmd))
350 dev_err(&client->dev, "send cmd failed %d:%ld\n", ret, sizeof(cmd));
351 } else {
352 ret = ts->ops->send(&cmd[7], valid_size);
353 if (ret != valid_size)
354 dev_err(&client->dev, "send cmd failed %d:%d\n",ret, valid_size);
355 }
356 return count;
357 }
358
show_tp_cmd_recv(struct device * dev,struct device_attribute * attr,char * buf)359 static ssize_t show_tp_cmd_recv(struct device *dev,
360 struct device_attribute *attr, char *buf)
361 {
362 struct i2c_client *client = to_i2c_client(dev);
363 struct elan_ts_data *ts = i2c_get_clientdata(client);
364 uint8_t rbuf[67] = {0x00};
365
366 int i = 0;
367 char c1;
368 char out[1024];
369 int shift = 0;
370
371 while(!ts->ops->poll()) {
372 ts->ops->recv(rbuf, 67);
373 dev_info(&client->dev, "rbuf: %x:%x:%x:%x:%x:%x:%x:%x\n",
374 rbuf[1],rbuf[2],rbuf[3],rbuf[4],rbuf[5],rbuf[6],rbuf[7],rbuf[8]);
375
376 for(i = 0; i < sizeof(rbuf); i++) {
377 c1 = rbuf[i] & 0xF0;
378 out[3 * i + 0 + shift] = IntToASCII(c1 >> 4);
379
380 c1 = rbuf[i]&0x0F;
381 out[3 * i + 1 + shift] = IntToASCII(c1);
382
383 if((i %7 ==0) && (i !=0)) {
384 out[3 * i + 2 + shift] = 0x0A;//'\n' = 0x0A;
385 } else {
386 out[3 * i + 2 + shift] = 0x20;//space = 0x20;
387 }
388 }
389 printk("%s\n", out);
390 sprintf(buf + strlen(buf), "%s\n", out+shift);
391 shift += 3 * i;
392 }
393 /*
394 if (ts->irq_lock_flag == 1) {
395 enable_irq(ts->hw_info.irq_num);
396 ts->irq_lock_flag = 0;
397 }
398 */
399 elan_switch_irq(ts, 1);
400 return strlen(buf);
401 }
402
403
404 static DEVICE_ATTR(raw_cmd, S_IWUSR | S_IRUSR, show_tp_cmd_recv, store_tp_cmd_send);
405
406 static struct attribute *elan_default_attributes[] = {
407 &dev_attr_enable_irq.attr,
408 &dev_attr_disable_irq.attr,
409 &dev_attr_gpio_int.attr,
410 &dev_attr_reset.attr,
411 &dev_attr_calibrate.attr,
412 &dev_attr_check_rek.attr,
413 &dev_attr_fw_info.attr,
414 &dev_attr_iap_status.attr,
415 &dev_attr_fw_update.attr,
416 &dev_attr_fw_store.attr,
417 // &dev_attr_tp_module_test.attr,
418 &dev_attr_tp_print_level.attr,
419 &dev_attr_raw_cmd.attr,
420 NULL
421 };
422
423 static struct attribute_group elan_default_attribute_group = {
424 .name = "elan_ktf",
425 .attrs = elan_default_attributes,
426 };
427
elan_sysfs_attri_file(struct elan_ts_data * ts)428 int elan_sysfs_attri_file(struct elan_ts_data *ts)
429 {
430 int err = 0;
431
432 err = sysfs_create_group(&ts->client->dev.kobj, &elan_default_attribute_group);
433 if ( err ) {
434 dev_err(&ts->client->dev, "[elan] %s sysfs create group error\n",__func__);
435 } else {
436 dev_err(&ts->client->dev,"[elan] %s sysfs create group success\n",__func__);
437 }
438
439 return err;
440 }
441
elan_sysfs_attri_file_remove(struct elan_ts_data * ts)442 void elan_sysfs_attri_file_remove(struct elan_ts_data *ts)
443 {
444 sysfs_remove_group(&ts->client->dev.kobj, &elan_default_attribute_group);
445 }
446
447
448
449