import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / input / touchscreen / mediatek / S7020 / rmi_fw_update.c
1 /*
2 * Synaptics DSX touchscreen driver
3 *
4 * Copyright (C) 2012 Synaptics Incorporated
5 *
6 * Copyright (C) 2012 Alexandra Chin <alexandra.chin@tw.synaptics.com>
7 * Copyright (C) 2012 Scott Lin <scott.lin@tw.synaptics.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/input.h>
30 #include <linux/firmware.h>
31 #include <cust_eint.h>
32 //#include <linux/input/synaptics_dsx.h>
33 #include "synaptics_dsx_rmi4_i2c.h"
34 #include "tpd.h"
35 //#include "SynaImage.h"
36
37
38
39
40 #define DEBUG_FW_UPDATE
41 #define SHOW_PROGRESS
42 #define FW_IMAGE_NAME "SynaImage.h"
43
44 #define CHECKSUM_OFFSET 0x00
45 #define BOOTLOADER_VERSION_OFFSET 0x07
46 #define IMAGE_SIZE_OFFSET 0x08
47 #define CONFIG_SIZE_OFFSET 0x0C
48 #define PRODUCT_ID_OFFSET 0x10
49 #define PRODUCT_INFO_OFFSET 0x1E
50 #define FW_IMAGE_OFFSET 0x100
51 #define PRODUCT_ID_SIZE 10
52
53 #define BOOTLOADER_ID_OFFSET 0
54 #define FLASH_PROPERTIES_OFFSET 2
55 #define BLOCK_SIZE_OFFSET 3
56 #define FW_BLOCK_COUNT_OFFSET 5
57
58 #define SYN_I2C_RETRY_TIMES 10
59 #define F01_STD_QUERY_LEN 21
60
61
62 #define REG_MAP (1 << 0)
63 #define UNLOCKED (1 << 1)
64 #define HAS_CONFIG_ID (1 << 2)
65 #define HAS_PERM_CONFIG (1 << 3)
66 #define HAS_BL_CONFIG (1 << 4)
67 #define HAS_DISP_CONFIG (1 << 5)
68 #define HAS_CTRL1 (1 << 6)
69
70 #define BLOCK_NUMBER_OFFSET 0
71 #define BLOCK_DATA_OFFSET 2
72
73 #define UI_CONFIG_AREA 0x00
74 #define PERM_CONFIG_AREA 0x01
75 #define BL_CONFIG_AREA 0x02
76 #define DISP_CONFIG_AREA 0x03
77
78 enum flash_command
79 {
80 CMD_WRITE_FW_BLOCK = 0x2,
81 CMD_ERASE_ALL = 0x3,
82 CMD_READ_CONFIG_BLOCK = 0x5,
83 CMD_WRITE_CONFIG_BLOCK = 0x6,
84 CMD_ERASE_CONFIG = 0x7,
85 CMD_ERASE_BL_CONFIG = 0x9,
86 CMD_ERASE_DISP_CONFIG = 0xA,
87 CMD_ENABLE_FLASH_PROG = 0xF,
88 };
89
90 #define SLEEP_MODE_NORMAL (0x00)
91 #define SLEEP_MODE_SENSOR_SLEEP (0x01)
92 #define SLEEP_MODE_RESERVED0 (0x02)
93 #define SLEEP_MODE_RESERVED1 (0x03)
94
95 #define ENABLE_WAIT_MS (1 * 1000)
96 #define WRITE_WAIT_MS (3 * 1000)
97 #define ERASE_WAIT_MS (5 * 1000)
98
99 #define MIN_SLEEP_TIME_US 50
100 #define MAX_SLEEP_TIME_US 100
101
102 extern void mt65xx_eint_unmask(unsigned int line);
103 extern void mt65xx_eint_mask(unsigned int line);
104
105
106 extern struct tpd_device *tpd;
107
108 extern struct kobject *properties_kobj_synap;
109 struct kobject *properties_kobj_fwupdate;
110
111
112 static ssize_t fwu_sysfs_show_image(struct file *data_file,
113 struct kobject *kobj, struct bin_attribute *attributes,
114 char *buf, loff_t pos, size_t count);
115
116 static ssize_t fwu_sysfs_store_image(struct file *data_file,
117 struct kobject *kobj, struct bin_attribute *attributes,
118 char *buf, loff_t pos, size_t count);
119
120 static ssize_t fwu_sysfs_do_reflash_store(struct device *dev,
121 struct device_attribute *attr, const char *buf, size_t count);
122
123 static ssize_t fwu_sysfs_write_config_store(struct device *dev,
124 struct device_attribute *attr, const char *buf, size_t count);
125
126 static ssize_t fwu_sysfs_read_config_store(struct device *dev,
127 struct device_attribute *attr, const char *buf, size_t count);
128 #if 0
129 static ssize_t fwu_sysfs_config_area_store(struct device *dev,
130 struct device_attribute *attr, const char *buf, size_t count);
131
132 static ssize_t fwu_sysfs_image_size_store(struct device *dev,
133 struct device_attribute *attr, const char *buf, size_t count);
134 #endif
135 static ssize_t fwu_sysfs_block_size_show(struct device *dev,
136 struct device_attribute *attr, char *buf);
137
138 static ssize_t fwu_sysfs_firmware_block_count_show(struct device *dev,
139 struct device_attribute *attr, char *buf);
140
141 static ssize_t fwu_sysfs_configuration_block_count_show(struct device *dev,
142 struct device_attribute *attr, char *buf);
143
144 static ssize_t fwu_sysfs_perm_config_block_count_show(struct device *dev,
145 struct device_attribute *attr, char *buf);
146
147 static ssize_t fwu_sysfs_bl_config_block_count_show(struct device *dev,
148 struct device_attribute *attr, char *buf);
149
150 static ssize_t fwu_sysfs_disp_config_block_count_show(struct device *dev,
151 struct device_attribute *attr, char *buf);
152
153 static int fwu_wait_for_idle(int timeout_ms);
154
155 extern int tpd_i2c_read_data(struct i2c_client *client, unsigned short addr, unsigned char *data, unsigned short length);
156
157 extern int tpd_i2c_write_data(struct i2c_client *client, unsigned short addr, unsigned char *data, unsigned short length);
158
159
160 struct image_header {
161 unsigned int checksum;
162 unsigned int image_size;
163 unsigned int config_size;
164 unsigned char options;
165 unsigned char bootloader_version;
166 unsigned char product_id[SYNAPTICS_RMI4_PRODUCT_ID_LENGTH + 1];
167 unsigned char product_info[SYNAPTICS_RMI4_PRODUCT_INFO_SIZE];
168 };
169
170 struct pdt_properties {
171 union {
172 struct {
173 unsigned char reserved_1:6;
174 unsigned char has_bsr:1;
175 unsigned char reserved_2:1;
176 } __packed;
177 unsigned char data[1];
178 };
179 };
180
181 struct f01_device_status {
182 union {
183 struct {
184 unsigned char status_code:4;
185 unsigned char reserved:2;
186 unsigned char flash_prog:1;
187 unsigned char unconfigured:1;
188 } __packed;
189 unsigned char data[1];
190 };
191 };
192
193 struct f01_device_control {
194 union {
195 struct {
196 unsigned char sleep_mode:2;
197 unsigned char nosleep:1;
198 unsigned char reserved:2;
199 unsigned char charger_connected:1;
200 unsigned char report_rate:1;
201 unsigned char configured:1;
202 } __packed;
203 unsigned char data[1];
204 };
205 };
206
207 struct f34_flash_control {
208 union {
209 struct {
210 unsigned char command:4;
211 unsigned char status:3;
212 unsigned char program_enabled:1;
213 } __packed;
214 unsigned char data[1];
215 };
216 };
217
218 struct f34_flash_properties {
219 union {
220 struct {
221 unsigned char regmap:1;
222 unsigned char unlocked:1;
223 unsigned char has_configid:1;
224 unsigned char has_perm_config:1;
225 unsigned char has_bl_config:1;
226 unsigned char has_display_config:1;
227 unsigned char has_blob_config:1;
228 unsigned char reserved:1;
229 } __packed;
230 unsigned char data[1];
231 };
232 };
233
234 struct synaptics_rmi4_fwu_handle {
235 bool initialized;
236 char product_id[SYNAPTICS_RMI4_PRODUCT_ID_LENGTH + 1];
237 unsigned int image_size;
238 unsigned int data_pos;
239 unsigned char intr_mask;
240 unsigned char bootloader_id[2];
241 unsigned char productinfo1;
242 unsigned char productinfo2;
243 unsigned char *ext_data_source;
244 unsigned char *read_config_buf;
245 const unsigned char *firmware_data;
246 const unsigned char *config_data;
247 unsigned short block_size;
248 unsigned short fw_block_count;
249 unsigned short config_block_count;
250 unsigned short perm_config_block_count;
251 unsigned short bl_config_block_count;
252 unsigned short disp_config_block_count;
253 unsigned short config_size;
254 unsigned short config_area;
255 unsigned short addr_f34_flash_control;
256 unsigned short addr_f01_interrupt_register;
257 struct synaptics_rmi4_fn_desc f01_fd;
258 struct synaptics_rmi4_fn_desc f34_fd;
259 struct synaptics_rmi4_exp_fn_ptr *fn_ptr;
260 struct synaptics_rmi4_data *rmi4_data;
261 struct f34_flash_control flash_control;
262 struct f34_flash_properties flash_properties;
263 };
264
265 static struct bin_attribute dev_attr_data = {
266 .attr = {
267 .name = "data",
268 .mode = (S_IRUGO | S_IWUSR),
269 },
270 .size = 0,
271 .read = fwu_sysfs_show_image,
272 .write = fwu_sysfs_store_image,
273 };
274
275 static struct device_attribute attrs[] = {
276 __ATTR(doreflash, (S_IRUGO | S_IWUSR),
277 synaptics_rmi4_show_error,
278 fwu_sysfs_do_reflash_store),
279 __ATTR(writeconfig, (S_IRUGO | S_IWUSR),
280 synaptics_rmi4_show_error,
281 fwu_sysfs_write_config_store),
282 __ATTR(readconfig, (S_IRUGO | S_IWUSR),
283 synaptics_rmi4_show_error,
284 fwu_sysfs_read_config_store),
285 #if 0
286 __ATTR(configarea, S_IWUGO,
287 synaptics_rmi4_show_error,
288 fwu_sysfs_config_area_store),
289 __ATTR(imagesize, S_IWUGO,
290 synaptics_rmi4_show_error,
291 fwu_sysfs_image_size_store),
292 #endif
293 __ATTR(blocksize, S_IRUGO,
294 fwu_sysfs_block_size_show,
295 synaptics_rmi4_store_error),
296 __ATTR(fwblockcount, S_IRUGO,
297 fwu_sysfs_firmware_block_count_show,
298 synaptics_rmi4_store_error),
299 __ATTR(configblockcount, S_IRUGO,
300 fwu_sysfs_configuration_block_count_show,
301 synaptics_rmi4_store_error),
302 __ATTR(permconfigblockcount, S_IRUGO,
303 fwu_sysfs_perm_config_block_count_show,
304 synaptics_rmi4_store_error),
305 __ATTR(blconfigblockcount, S_IRUGO,
306 fwu_sysfs_bl_config_block_count_show,
307 synaptics_rmi4_store_error),
308 __ATTR(dispconfigblockcount, S_IRUGO,
309 fwu_sysfs_disp_config_block_count_show,
310 synaptics_rmi4_store_error),
311 };
312 /*
313 static struct kobj_attribute synaptics_flashprog_attr = {
314 .attr = {
315 .name = "prog",
316 .mode = (S_IRUGO | S_IWUGO),
317 },
318 .show = &synaptics_rmi4_f01_flashprog_show,
319 .store = &synaptics_rmi4_store_error,
320 };
321
322 static struct kobj_attribute synaptics_0dbutton_attr = {
323 .attr = {
324 .name = "0dbutton",
325 .mode = (S_IRUGO | S_IWUGO),
326 },
327 .show = &synaptics_rmi4_0dbutton_show,
328 .store = &synaptics_rmi4_0dbutton_store,
329 };
330
331 static struct attribute *syna_attrs[] = {
332
333 &synaptics_pm_cycle_attr.attr,
334 &synaptics_f51_enables_attr.attr,
335 &synaptics_show_error_attr.attr,
336 &synaptics_productinfo_attr.attr,
337 &synaptics_0dbutton_attr.attr,
338 &synaptics_flashprog_attr.attr,
339 NULL
340 };
341
342 static struct attribute_group syna_attr_group = {
343 .attrs = syna_attrs,
344 };
345 */
346
347 static struct synaptics_rmi4_fwu_handle *fwu;
348
349 static struct completion remove_complete;
350
351 static unsigned int extract_uint(const unsigned char *ptr)
352 {
353 return (unsigned int)ptr[0] +
354 (unsigned int)ptr[1] * 0x100 +
355 (unsigned int)ptr[2] * 0x10000 +
356 (unsigned int)ptr[3] * 0x1000000;
357 }
358
359 static void parse_header(struct image_header *header,
360 const unsigned char *fw_image)
361 {
362 header->checksum = extract_uint(&fw_image[CHECKSUM_OFFSET]);
363 header->bootloader_version = fw_image[BOOTLOADER_VERSION_OFFSET];
364 header->image_size = extract_uint(&fw_image[IMAGE_SIZE_OFFSET]);
365 header->config_size = extract_uint(&fw_image[CONFIG_SIZE_OFFSET]);
366 memcpy(header->product_id, &fw_image[PRODUCT_ID_OFFSET],
367 SYNAPTICS_RMI4_PRODUCT_ID_LENGTH);
368 header->product_id[SYNAPTICS_RMI4_PRODUCT_ID_LENGTH] = 0;
369 memcpy(header->product_info, &fw_image[PRODUCT_INFO_OFFSET],
370 SYNAPTICS_RMI4_PRODUCT_INFO_SIZE);
371
372 #ifdef DEBUG_FW_UPDATE
373 dev_info(&fwu->rmi4_data->i2c_client->dev,
374 "Firwmare size %d, config size %d\n",
375 header->image_size,
376 header->config_size);
377 #endif
378 return;
379 }
380
381 static int fwu_check_version(void)
382 {
383 int retval;
384 unsigned char firmware_id[4];
385 unsigned char config_id[4];
386 struct i2c_client *i2c_client = fwu->rmi4_data->i2c_client;
387
388 //device firmware id
389 retval = fwu->fn_ptr->read(fwu->rmi4_data,
390 fwu->f01_fd.query_base_addr + 18,
391 firmware_id,
392 sizeof(firmware_id));
393 if (retval < 0) {
394 TPD_DMESG("Failed to read firmware ID (code %d).\n", retval);
395 return retval;
396 }
397 firmware_id[3] = 0;
398
399 TPD_DMESG("Device firmware ID%d\n",
400 extract_uint(firmware_id));
401
402 //device config id
403 retval = fwu->fn_ptr->read(fwu->rmi4_data,
404 fwu->f34_fd.ctrl_base_addr,
405 config_id,
406 sizeof(config_id));
407 if (retval < 0) {
408 dev_err(&i2c_client->dev,
409 "Failed to read config ID (code %d).\n", retval);
410 return retval;
411 }
412
413 TPD_DMESG("Device config ID 0x%02X, 0x%02X, 0x%02X, 0x%02X\n",
414 config_id[0], config_id[1], config_id[2], config_id[3]);
415
416 //.img config id
417 TPD_DMESG(".img config ID 0x%02X, 0x%02X, 0x%02X, 0x%02X\n",
418 fwu->config_data[0],
419 fwu->config_data[1],
420 fwu->config_data[2],
421 fwu->config_data[3]);
422
423 if(config_id[0]==fwu->config_data[0]&&
424 config_id[1]==fwu->config_data[1]&&
425 config_id[2]==fwu->config_data[2]&&
426 config_id[3]==fwu->config_data[3]){
427 TPD_DMESG("Both the device and .img is the same version, no need to update!\n");
428 return -1;
429 }
430 return 0;
431 }
432
433 static int fwu_read_f01_device_status(struct f01_device_status *status)
434 {
435 int retval;
436
437 retval = fwu->fn_ptr->read(fwu->rmi4_data,
438 fwu->f01_fd.data_base_addr,
439 status->data,
440 sizeof(status->data));
441 if (retval < 0) {
442 TPD_DMESG(
443 "%s: Failed to read F01 device status\n",
444 __func__);
445 return retval;
446 }
447
448 return 0;
449 }
450
451 static int fwu_read_f34_queries(void)
452 {
453 int retval;
454 unsigned char count = 4;
455 unsigned char buf[10];
456 struct i2c_client *i2c_client = fwu->rmi4_data->i2c_client;
457
458 retval = fwu->fn_ptr->read(fwu->rmi4_data,
459 fwu->f34_fd.query_base_addr + BOOTLOADER_ID_OFFSET,
460 fwu->bootloader_id,
461 sizeof(fwu->bootloader_id));
462 if (retval < 0) {
463 TPD_DMESG(
464 "%s: Failed to read bootloader ID\n",
465 __func__);
466 return retval;
467 }
468
469 retval = fwu->fn_ptr->read(fwu->rmi4_data,
470 fwu->f34_fd.query_base_addr + FLASH_PROPERTIES_OFFSET,
471 fwu->flash_properties.data,
472 sizeof(fwu->flash_properties.data));
473 if (retval < 0) {
474 dev_err(&i2c_client->dev,
475 "%s: Failed to read flash properties\n",
476 __func__);
477 return retval;
478 }
479
480 dev_info(&i2c_client->dev, "%s perm:%d, bl%d, display:%d\n",
481 __func__,
482 fwu->flash_properties.has_perm_config,
483 fwu->flash_properties.has_bl_config,
484 fwu->flash_properties.has_display_config);
485
486 if (fwu->flash_properties.has_perm_config)
487 count += 2;
488
489 if (fwu->flash_properties.has_bl_config)
490 count += 2;
491
492 if (fwu->flash_properties.has_display_config)
493 count += 2;
494
495 retval = fwu->fn_ptr->read(fwu->rmi4_data,
496 fwu->f34_fd.query_base_addr + BLOCK_SIZE_OFFSET,
497 buf,
498 2);
499 if (retval < 0) {
500 dev_err(&i2c_client->dev,
501 "%s: Failed to read block size info\n",
502 __func__);
503 return retval;
504 }
505
506 batohs(&fwu->block_size, &(buf[0]));
507
508 retval = fwu->fn_ptr->read(fwu->rmi4_data,
509 fwu->f34_fd.query_base_addr + FW_BLOCK_COUNT_OFFSET,
510 buf,
511 count);
512 if (retval < 0) {
513 dev_err(&i2c_client->dev,
514 "%s: Failed to read block count info\n",
515 __func__);
516 return retval;
517 }
518
519 batohs(&fwu->fw_block_count, &(buf[0]));
520 batohs(&fwu->config_block_count, &(buf[2]));
521
522 count = 4;
523
524 if (fwu->flash_properties.has_perm_config) {
525 batohs(&fwu->perm_config_block_count, &(buf[count]));
526 count += 2;
527 }
528
529 if (fwu->flash_properties.has_bl_config) {
530 batohs(&fwu->bl_config_block_count, &(buf[count]));
531 count += 2;
532 }
533
534 if (fwu->flash_properties.has_display_config)
535 batohs(&fwu->disp_config_block_count, &(buf[count]));
536
537 fwu->addr_f34_flash_control = fwu->f34_fd.data_base_addr +
538 BLOCK_DATA_OFFSET +
539 fwu->block_size;
540 return 0;
541 }
542
543 static int fwu_read_interrupt_status(void)
544 {
545 int retval;
546 unsigned char interrupt_status;
547 retval = fwu->fn_ptr->read(fwu->rmi4_data,
548 fwu->addr_f01_interrupt_register,
549 &interrupt_status,
550 sizeof(interrupt_status));
551 if (retval < 0) {
552 TPD_DMESG(
553 "%s: Failed to read flash status\n",
554 __func__);
555 return retval;
556 }
557 return interrupt_status;
558 }
559
560 static int fwu_read_f34_flash_status(void)
561 {
562 int retval;
563 retval = fwu->fn_ptr->read(fwu->rmi4_data,
564 fwu->addr_f34_flash_control,
565 fwu->flash_control.data,
566 sizeof(fwu->flash_control.data));
567 if (retval < 0) {
568 dev_err(&fwu->rmi4_data->i2c_client->dev,
569 "%s: Failed to read flash status\n",
570 __func__);
571 return retval;
572 }
573 // return 0;
574 //TPD_DMESG("fwu_read_f34_flash_status = 0x%x\n", fwu->flash_control.data[0]);
575 return fwu->flash_control.data[0];
576
577 }
578
579 static int fwu_reset_device(void)
580 {
581 int retval;
582 unsigned char reset = 0x01;
583
584 #ifdef DEBUG_FW_UPDATE
585 dev_info(&fwu->rmi4_data->i2c_client->dev, "Reset device\n");
586 #endif
587
588 retval = fwu->fn_ptr->write(fwu->rmi4_data,
589 fwu->f01_fd.cmd_base_addr,
590 &reset,
591 sizeof(reset));
592 if (retval < 0) {
593 dev_err(&fwu->rmi4_data->i2c_client->dev,
594 "%s: Failed to reset device (addr : 0x%02x)\n",
595 __func__, fwu->f01_fd.cmd_base_addr);
596 return retval;
597 }
598
599 fwu_wait_for_idle(WRITE_WAIT_MS);
600
601 retval = fwu->rmi4_data->reset_device(fwu->rmi4_data);
602 if (retval < 0) {
603 dev_err(&fwu->rmi4_data->i2c_client->dev,
604 "%s: Failed to reset core driver after reflash\n",
605 __func__);
606 return retval;
607 }
608 return 0;
609 }
610
611 static int fwu_write_f34_command(unsigned char cmd)
612 {
613 int retval;
614
615 retval = fwu->fn_ptr->write(fwu->rmi4_data,
616 fwu->addr_f34_flash_control,
617 &cmd,
618 sizeof(cmd));
619 if (retval < 0) {
620 TPD_DMESG(
621 "%s: Failed to write command 0x%02x\n",
622 __func__, cmd);
623 return retval;
624 }
625 return 0;
626 }
627
628 static unsigned char fwu_check_flash_status(void)
629 {
630 fwu_read_f34_flash_status();
631 return fwu->flash_control.status;
632 }
633
634
635 static int fwu_wait_for_idle(int timeout_ms)
636 {
637 int count = 0;
638 int timeout_count = ((timeout_ms * 1000) / MAX_SLEEP_TIME_US) + 1;
639
640 do {
641 if (fwu_read_interrupt_status() > 0)
642 // if( (fwu_read_f34_flash_status())&0x7f==0x00)
643 return 0;
644
645 usleep_range(MIN_SLEEP_TIME_US, MAX_SLEEP_TIME_US);
646 count++;
647 } while (count < timeout_count);
648
649 TPD_DMESG(
650 "%s: Timed out waiting for idle status\n",
651 __func__);
652
653 return -ETIMEDOUT;
654 }
655
656 static int fwu_scan_pdt(void)
657 {
658 int retval;
659 unsigned char ii;
660 unsigned char intr_count = 0;
661 unsigned char intr_off;
662 unsigned char intr_src;
663 unsigned short addr;
664 bool f01found = false;
665 bool f34found = false;
666 struct synaptics_rmi4_fn_desc rmi_fd;
667
668 #ifdef DEBUG_FW_UPDATE
669 dev_info(&fwu->rmi4_data->i2c_client->dev, "Scan PDT\n");
670 #endif
671
672 for (addr = PDT_START; addr > PDT_END; addr -= PDT_ENTRY_SIZE) {
673 retval = fwu->fn_ptr->read(fwu->rmi4_data,
674 addr,
675 (unsigned char *)&rmi_fd,
676 sizeof(rmi_fd));
677 if (retval < 0)
678 return retval;
679
680 if (rmi_fd.fn_number) {
681 TPD_DMESG(
682 "%s: Found F%02x\n",
683 __func__, rmi_fd.fn_number);
684 switch (rmi_fd.fn_number) {
685 case SYNAPTICS_RMI4_F01:
686 f01found = true;
687 fwu->f01_fd = rmi_fd;
688 fwu->addr_f01_interrupt_register =
689 fwu->f01_fd.data_base_addr + 1;
690 break;
691 case SYNAPTICS_RMI4_F34:
692 f34found = true;
693 fwu->f34_fd = rmi_fd;
694 fwu->intr_mask = 0;
695 intr_src = rmi_fd.intr_src_count;
696 intr_off = intr_count % 8;
697 for (ii = intr_off;
698 ii < ((intr_src & MASK_3BIT) +
699 intr_off);
700 ii++)
701 fwu->intr_mask |= 1 << ii;
702 break;
703 }
704 } else
705 break;
706
707 intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
708 }
709
710 if (!f01found || !f34found) {
711 dev_err(&fwu->rmi4_data->i2c_client->dev,
712 "%s: Failed to find both F01 and F34\n",
713 __func__);
714 return -EINVAL;
715 }
716
717 fwu_read_interrupt_status();
718 return 0;
719 }
720
721 static int fwu_write_blocks(unsigned char *block_ptr, unsigned short block_cnt,
722 unsigned char command)
723 {
724 int retval;
725 unsigned char block_offset[] = {0, 0};
726 unsigned short block_num;
727 #ifdef SHOW_PROGRESS
728 unsigned int progress = (command == CMD_WRITE_CONFIG_BLOCK) ?
729 10 : 100;
730 #endif
731 retval = fwu->fn_ptr->write(fwu->rmi4_data,
732 fwu->f34_fd.data_base_addr + BLOCK_NUMBER_OFFSET,
733 block_offset,
734 sizeof(block_offset));
735 // TPD_DMESG("write block number");
736 if (retval < 0) {
737 dev_err(&fwu->rmi4_data->i2c_client->dev,
738 "%s: Failed to write to block number registers\n",
739 __func__);
740 return retval;
741 }
742
743 for (block_num = 0; block_num < block_cnt; block_num++) {
744 #ifdef SHOW_PROGRESS
745 if (block_num % progress == 0){
746 TPD_DMESG(
747 "%s: update %s %3d / %3d \n",
748 __func__,
749 command == CMD_WRITE_CONFIG_BLOCK ?
750 "config" : "firmware",
751 block_num, block_cnt);
752 }
753 #endif
754 // TPD_DMESG("before write BLOCK_DATA_OFFSET");
755 retval = fwu->fn_ptr->write(fwu->rmi4_data,
756 fwu->f34_fd.data_base_addr + BLOCK_DATA_OFFSET,
757 block_ptr,
758 fwu->block_size);
759 if (retval < 0) {
760 TPD_DMESG("%s: Failed to write block data (block %d)\n",
761 __func__, block_num);
762 return retval;
763 }
764
765 retval = fwu_write_f34_command(command);
766 if (retval < 0) {
767 dev_err(&fwu->rmi4_data->i2c_client->dev,
768 "%s: Failed to write command for block %d\n",
769 __func__, block_num);
770 return retval;
771 }
772
773 retval = fwu_wait_for_idle(WRITE_WAIT_MS);
774 if (retval < 0) {
775 dev_err(&fwu->rmi4_data->i2c_client->dev,
776 "%s: Failed to wait for idle status (block %d)\n",
777 __func__, block_num);
778 return retval;
779 }
780
781 retval = fwu_check_flash_status();
782 if (retval!= 0) {
783 dev_err(&fwu->rmi4_data->i2c_client->dev,
784 "%s: Flash block %d status %d\n",
785 __func__, block_num, retval);
786 return -1;
787 }
788 block_ptr += fwu->block_size;
789 }
790 #ifdef SHOW_PROGRESS
791 dev_info(&fwu->rmi4_data->i2c_client->dev,
792 "%s: update %s %3d / %3d \n",
793 __func__,
794 command == CMD_WRITE_CONFIG_BLOCK ?
795 "config" : "firmware",
796 block_cnt, block_cnt);
797 #endif
798 return 0;
799 }
800
801 static int fwu_write_firmware(void)
802 {
803 return fwu_write_blocks((unsigned char *)fwu->firmware_data,
804 fwu->fw_block_count, CMD_WRITE_FW_BLOCK);
805 }
806
807 static int fwu_write_configuration(void)
808 {
809 return fwu_write_blocks((unsigned char *)fwu->config_data,
810 fwu->config_block_count, CMD_WRITE_CONFIG_BLOCK);
811 }
812
813 static int fwu_write_bootloader_id(void)
814 {
815 int retval;
816
817 #ifdef DEBUG_FW_UPDATE
818 dev_info(&fwu->rmi4_data->i2c_client->dev, "Write bootloader ID\n");
819 #endif
820 retval = fwu->fn_ptr->write(fwu->rmi4_data,
821 fwu->f34_fd.data_base_addr + BLOCK_DATA_OFFSET,
822 fwu->bootloader_id,
823 sizeof(fwu->bootloader_id));
824 TPD_DMESG("bootloader ID=%s\n",fwu->bootloader_id);
825 TPD_DMESG("f34_data_addr=%x\n",fwu->f34_fd.data_base_addr + BLOCK_DATA_OFFSET);
826
827 if (retval < 0) {
828 TPD_DMESG(
829 "%s: Failed to write bootloader ID\n",
830 __func__);
831 return retval;
832 }
833
834 return 0;
835 }
836
837 static int fwu_enter_flash_prog(void)
838 {
839 int retval;
840 struct f01_device_status f01_device_status;
841 struct f01_device_control f01_device_control;
842
843 #ifdef DEBUG_FW_UPDATE
844 dev_info(&fwu->rmi4_data->i2c_client->dev, "Enter bootloader mode\n");
845 #endif
846 retval = fwu_read_f01_device_status(&f01_device_status);
847 if (retval < 0)
848 return retval;
849
850 if (f01_device_status.flash_prog) {
851 TPD_DMESG(
852 "%s: Already in flash prog mode\n",
853 __func__);
854 return 0;
855 }
856
857 retval = fwu_write_bootloader_id();
858 if (retval < 0)
859 return retval;
860
861 retval = fwu_write_f34_command(CMD_ENABLE_FLASH_PROG);
862 if (retval < 0)
863 return retval;
864
865 retval = fwu_wait_for_idle(ENABLE_WAIT_MS);
866 if (retval < 0)
867 return retval;
868
869 retval = fwu_read_f01_device_status(&f01_device_status);
870 if (retval < 0)
871 return retval;
872
873 if (!f01_device_status.flash_prog) {
874 dev_err(&fwu->rmi4_data->i2c_client->dev,
875 "%s: Program enabled bit not set\n",
876 __func__);
877 return -EINVAL;
878 }
879
880 retval = fwu_scan_pdt();
881 if (retval < 0)
882 return retval;
883
884 retval = fwu_read_f01_device_status(&f01_device_status);
885 if (retval < 0)
886 return retval;
887
888 if (!f01_device_status.flash_prog) {
889 dev_err(&fwu->rmi4_data->i2c_client->dev,
890 "%s: Not in flash prog mode\n",
891 __func__);
892 return -EINVAL;
893 }
894
895 retval = fwu_read_f34_queries();
896 if (retval < 0)
897 return retval;
898
899 retval = fwu->fn_ptr->read(fwu->rmi4_data,
900 fwu->f01_fd.ctrl_base_addr,
901 f01_device_control.data,
902 sizeof(f01_device_control.data));
903 if (retval < 0) {
904 TPD_DMESG(
905 "%s: Failed to read F01 device control\n",
906 __func__);
907 return retval;
908 }
909
910 f01_device_control.nosleep = true;
911 f01_device_control.sleep_mode = SLEEP_MODE_NORMAL;
912
913 retval = fwu->fn_ptr->write(fwu->rmi4_data,
914 fwu->f01_fd.ctrl_base_addr,
915 f01_device_control.data,
916 sizeof(f01_device_control.data));
917 if (retval < 0) {
918 TPD_DMESG(
919 "%s: Failed to write F01 device control\n",
920 __func__);
921 return retval;
922 }
923
924 return retval;
925 }
926
927 static int fwu_do_reflash(void)
928 {
929 int retval;
930
931 retval = fwu_enter_flash_prog();
932 if (retval < 0)
933 return retval;
934
935 TPD_DMESG(
936 "%s: Entered flash prog mode\n",
937 __func__);
938
939 retval = fwu_write_bootloader_id();
940 if (retval < 0)
941 return retval;
942
943 TPD_DMESG(
944 "%s: Bootloader ID written\n",
945 __func__);
946
947 retval = fwu_write_f34_command(CMD_ERASE_ALL);
948 if (retval < 0)
949 return retval;
950
951 TPD_DMESG("%s: Erase all command written\n",
952 __func__);
953
954
955 //while (1);
956
957 retval = fwu_wait_for_idle(ERASE_WAIT_MS);
958 if (retval < 0)
959 return retval;
960
961 TPD_DMESG(
962 "%s: Idle status detected\n",
963 __func__);
964
965 if (fwu->firmware_data) {
966 retval = fwu_write_firmware();
967 mdelay(100);
968 if (retval < 0)
969 return retval;
970 pr_notice("%s: Firmware programmed\n", __func__);
971 }
972
973 if (fwu->config_data) {
974 retval = fwu_write_configuration();
975 if (retval < 0)
976 return retval;
977 pr_notice("%s: Configuration programmed\n", __func__);
978 }
979
980 return retval;
981 }
982
983 char buf[] = {
984 // #inlcude ".h"
985 };
986
987 static int fwu_start_reflash(void)
988 {
989 int retval;
990 struct image_header header;
991 const unsigned char *fw_image;
992 const struct firmware *fw_entry = NULL;
993 struct f01_device_status f01_device_status;
994
995 TPD_DMESG("%s: Start of reflash process\n", __func__);
996 //mt65xx_eint_mask(CUST_EINT_TOUCH_PANEL_NUM);
997
998 if (fwu->ext_data_source)
999 fw_image = fwu->ext_data_source;
1000 else {
1001 TPD_DMESG(
1002 "%s: Requesting firmware image %s\n",
1003 __func__, FW_IMAGE_NAME);
1004
1005 retval = request_firmware(&fw_entry, FW_IMAGE_NAME,
1006 &fwu->rmi4_data->i2c_client->dev);
1007 if (retval != 0) {
1008 TPD_DMESG(
1009 "%s: Firmware image %s not available\n",
1010 __func__, FW_IMAGE_NAME);
1011 retval = -EINVAL;
1012 goto exit;
1013 }
1014
1015 TPD_DMESG(
1016 "%s: Firmware image size = %d\n",
1017 __func__, fw_entry->size);
1018
1019 fw_image = fw_entry->data;
1020 }
1021
1022 parse_header(&header, fw_image);
1023
1024 if (header.image_size)
1025 fwu->firmware_data = fw_image + FW_IMAGE_OFFSET;
1026 if (header.config_size) {
1027 fwu->config_data = fw_image + FW_IMAGE_OFFSET +
1028 header.image_size;
1029 }
1030
1031 fwu->fn_ptr->enable(fwu->rmi4_data, false);
1032
1033 retval = fwu_check_version();
1034 #if 1
1035 if (retval < 0) {
1036 goto exit;
1037 }
1038 #endif
1039 retval = fwu_do_reflash();
1040 if (retval < 0) {
1041 TPD_DMESG(
1042 "%s: Failed to do reflash\n",
1043 __func__);
1044 }
1045 msleep(50);
1046 //reset device
1047 fwu_reset_device();
1048
1049 //check device status
1050 retval = fwu_read_f01_device_status(&f01_device_status);
1051 if (retval < 0)
1052 goto exit;
1053
1054 dev_info(&fwu->rmi4_data->i2c_client->dev, "Device is in %s mode\n",
1055 f01_device_status.flash_prog == 1 ? "bootloader" : "UI");
1056 if (f01_device_status.flash_prog )
1057 dev_info(&fwu->rmi4_data->i2c_client->dev, "Flash status %d\n",
1058 f01_device_status.status_code);
1059
1060 if (f01_device_status.flash_prog) {
1061 dev_info(&fwu->rmi4_data->i2c_client->dev,
1062 "%s: Device is in flash prog mode 0x%02X\n",
1063 __func__, f01_device_status.status_code);
1064 retval = 0;
1065 goto exit;
1066 }
1067 fwu->fn_ptr->enable(fwu->rmi4_data, true);
1068 if (fw_entry)
1069 release_firmware(fw_entry);
1070
1071 pr_notice("%s: End of reflash process\n", __func__);
1072 exit:
1073 //mt65xx_eint_unmask(CUST_EINT_TOUCH_PANEL_NUM);
1074 return retval;
1075 }
1076
1077 static int fwu_do_write_config(void)
1078 {
1079 int retval;
1080
1081 retval = fwu_enter_flash_prog();
1082 if (retval < 0)
1083 return retval;
1084
1085 TPD_DMESG(
1086 "%s: Entered flash prog mode\n",
1087 __func__);
1088
1089 if (fwu->config_area == PERM_CONFIG_AREA) {
1090 fwu->config_block_count = fwu->perm_config_block_count;
1091 goto write_config;
1092 }
1093
1094 retval = fwu_write_bootloader_id();
1095 if (retval < 0)
1096 return retval;
1097
1098 TPD_DMESG(
1099 "%s: Bootloader ID written\n",
1100 __func__);
1101
1102 switch (fwu->config_area) {
1103 case UI_CONFIG_AREA:
1104 retval = fwu_write_f34_command(CMD_ERASE_CONFIG);
1105 break;
1106 case BL_CONFIG_AREA:
1107 retval = fwu_write_f34_command(CMD_ERASE_BL_CONFIG);
1108 fwu->config_block_count = fwu->bl_config_block_count;
1109 break;
1110 case DISP_CONFIG_AREA:
1111 retval = fwu_write_f34_command(CMD_ERASE_DISP_CONFIG);
1112 fwu->config_block_count = fwu->disp_config_block_count;
1113 break;
1114 }
1115 if (retval < 0)
1116 return retval;
1117
1118 TPD_DMESG(
1119 "%s: Erase command written\n",
1120 __func__);
1121
1122 retval = fwu_wait_for_idle(ERASE_WAIT_MS);
1123 if (retval < 0)
1124 return retval;
1125
1126 TPD_DMESG(
1127 "%s: Idle status detected\n",
1128 __func__);
1129
1130 write_config:
1131 retval = fwu_write_configuration();
1132 if (retval < 0)
1133 return retval;
1134
1135 pr_notice("%s: Config written\n", __func__);
1136
1137 return retval;
1138 }
1139
1140 static int fwu_start_write_config(void)
1141 {
1142 int retval;
1143 struct image_header header;
1144
1145 switch (fwu->config_area) {
1146 case UI_CONFIG_AREA:
1147 break;
1148 case PERM_CONFIG_AREA:
1149 if (!fwu->flash_properties.has_perm_config)
1150 return -EINVAL;
1151 break;
1152 case BL_CONFIG_AREA:
1153 if (!fwu->flash_properties.has_bl_config)
1154 return -EINVAL;
1155 break;
1156 case DISP_CONFIG_AREA:
1157 if (!fwu->flash_properties.has_display_config)
1158 return -EINVAL;
1159 break;
1160 default:
1161 return -EINVAL;
1162 }
1163
1164 if (fwu->ext_data_source)
1165 fwu->config_data = fwu->ext_data_source;
1166 else
1167 return -EINVAL;
1168
1169 if (fwu->config_area == UI_CONFIG_AREA) {
1170 parse_header(&header, fwu->ext_data_source);
1171
1172 if (header.config_size) {
1173 fwu->config_data = fwu->ext_data_source +
1174 FW_IMAGE_OFFSET +
1175 header.image_size;
1176 } else {
1177 return -EINVAL;
1178 }
1179 }
1180
1181 pr_notice("%s: Start of write config process\n", __func__);
1182
1183 retval = fwu_do_write_config();
1184 if (retval < 0) {
1185 dev_err(&fwu->rmi4_data->i2c_client->dev,
1186 "%s: Failed to write config\n",
1187 __func__);
1188 }
1189
1190 fwu->rmi4_data->reset_device(fwu->rmi4_data);
1191
1192 pr_notice("%s: End of write config process\n", __func__);
1193
1194 return retval;
1195 }
1196
1197 static int fwu_do_read_config(void)
1198 {
1199 int retval;
1200 unsigned char block_offset[] = {0, 0};
1201 unsigned short block_num;
1202 unsigned short block_count;
1203 unsigned short index = 0;
1204
1205 retval = fwu_enter_flash_prog();
1206 if (retval < 0)
1207 goto exit;
1208
1209 TPD_DMESG(
1210 "%s: Entered flash prog mode\n",
1211 __func__);
1212
1213 switch (fwu->config_area) {
1214 case UI_CONFIG_AREA:
1215 block_count = fwu->config_block_count;
1216 break;
1217 case PERM_CONFIG_AREA:
1218 if (!fwu->flash_properties.has_perm_config) {
1219 retval = -EINVAL;
1220 goto exit;
1221 }
1222 block_count = fwu->perm_config_block_count;
1223 break;
1224 case BL_CONFIG_AREA:
1225 if (!fwu->flash_properties.has_bl_config) {
1226 retval = -EINVAL;
1227 goto exit;
1228 }
1229 block_count = fwu->bl_config_block_count;
1230 break;
1231 case DISP_CONFIG_AREA:
1232 if (!fwu->flash_properties.has_display_config) {
1233 retval = -EINVAL;
1234 goto exit;
1235 }
1236 block_count = fwu->disp_config_block_count;
1237 break;
1238 default:
1239 retval = -EINVAL;
1240 goto exit;
1241 }
1242
1243 fwu->config_size = fwu->block_size * block_count;
1244
1245 kfree(fwu->read_config_buf);
1246 fwu->read_config_buf = kzalloc(fwu->config_size, GFP_KERNEL);
1247
1248 block_offset[1] |= (fwu->config_area << 5);
1249
1250 retval = fwu->fn_ptr->write(fwu->rmi4_data,
1251 fwu->f34_fd.data_base_addr + BLOCK_NUMBER_OFFSET,
1252 block_offset,
1253 sizeof(block_offset));
1254 if (retval < 0) {
1255 dev_err(&fwu->rmi4_data->i2c_client->dev,
1256 "%s: Failed to write to block number registers\n",
1257 __func__);
1258 goto exit;
1259 }
1260
1261 for (block_num = 0; block_num < block_count; block_num++) {
1262 retval = fwu_write_f34_command(CMD_READ_CONFIG_BLOCK);
1263 if (retval < 0) {
1264 dev_err(&fwu->rmi4_data->i2c_client->dev,
1265 "%s: Failed to write read config command\n",
1266 __func__);
1267 goto exit;
1268 }
1269
1270 retval = fwu_wait_for_idle(WRITE_WAIT_MS);
1271 if (retval < 0) {
1272 dev_err(&fwu->rmi4_data->i2c_client->dev,
1273 "%s: Failed to wait for idle status\n",
1274 __func__);
1275 goto exit;
1276 }
1277
1278 retval = fwu->fn_ptr->read(fwu->rmi4_data,
1279 fwu->f34_fd.data_base_addr + BLOCK_DATA_OFFSET,
1280 &fwu->read_config_buf[index],
1281 fwu->block_size);
1282 if (retval < 0) {
1283 dev_err(&fwu->rmi4_data->i2c_client->dev,
1284 "%s: Failed to read block data (block %d)\n",
1285 __func__, block_num);
1286 goto exit;
1287 }
1288
1289 index += fwu->block_size;
1290 }
1291
1292 exit:
1293 fwu->rmi4_data->reset_device(fwu->rmi4_data);
1294
1295 return retval;
1296 }
1297
1298 int synaptics_fw_updater(unsigned char *fw_data)
1299 {
1300 int retval;
1301
1302 if (!fwu)
1303 return -ENODEV;
1304
1305 if (!fwu->initialized)
1306 return -ENODEV;
1307
1308 fwu->ext_data_source = fw_data;
1309 fwu->config_area = UI_CONFIG_AREA;
1310
1311 retval = fwu_start_reflash();
1312
1313 return retval;
1314 }
1315 EXPORT_SYMBOL(synaptics_fw_updater);
1316
1317 static ssize_t fwu_sysfs_show_image(struct file *data_file,
1318 struct kobject *kobj, struct bin_attribute *attributes,
1319 char *buf, loff_t pos, size_t count)
1320 {
1321 struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
1322
1323 if (count < fwu->config_size) {
1324 dev_err(&rmi4_data->i2c_client->dev,
1325 "%s: Not enough space (%d bytes) in buffer\n",
1326 __func__, count);
1327 return -EINVAL;
1328 }
1329
1330 memcpy(buf, fwu->read_config_buf, fwu->config_size);
1331
1332 return fwu->config_size;
1333 }
1334
1335 static ssize_t fwu_sysfs_store_image(struct file *data_file,
1336 struct kobject *kobj, struct bin_attribute *attributes,
1337 char *buf, loff_t pos, size_t count)
1338 {
1339 memcpy((void *)(&fwu->ext_data_source[fwu->data_pos]),
1340 (const void *)buf,
1341 count);
1342
1343 fwu->data_pos += count;
1344
1345 return count;
1346 }
1347
1348 static ssize_t fwu_sysfs_do_reflash_store(struct device *dev,
1349 struct device_attribute *attr, const char *buf, size_t count)
1350 {
1351 int retval;
1352 unsigned int input;
1353 struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
1354
1355 if (sscanf(buf, "%u", &input) != 1) {
1356 retval = -EINVAL;
1357 goto exit;
1358 }
1359
1360 if (input != 1) {
1361 retval = -EINVAL;
1362 goto exit;
1363 }
1364
1365 retval = synaptics_fw_updater(fwu->ext_data_source);
1366 if (retval < 0) {
1367 dev_err(&rmi4_data->i2c_client->dev,
1368 "%s: Failed to do reflash\n",
1369 __func__);
1370 goto exit;
1371 }
1372
1373 retval = count;
1374
1375 exit:
1376 kfree(fwu->ext_data_source);
1377 fwu->ext_data_source = NULL;
1378 return retval;
1379 }
1380
1381 static ssize_t fwu_sysfs_write_config_store(struct device *dev,
1382 struct device_attribute *attr, const char *buf, size_t count)
1383 {
1384 int retval;
1385 unsigned int input;
1386 struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
1387
1388 if (sscanf(buf, "%u", &input) != 1) {
1389 retval = -EINVAL;
1390 goto exit;
1391 }
1392
1393 if (input != 1) {
1394 retval = -EINVAL;
1395 goto exit;
1396 }
1397
1398 retval = fwu_start_write_config();
1399 if (retval < 0) {
1400 dev_err(&rmi4_data->i2c_client->dev,
1401 "%s: Failed to write config\n",
1402 __func__);
1403 goto exit;
1404 }
1405
1406 retval = count;
1407
1408 exit:
1409 kfree(fwu->ext_data_source);
1410 fwu->ext_data_source = NULL;
1411 return retval;
1412 }
1413
1414 static ssize_t fwu_sysfs_read_config_store(struct device *dev,
1415 struct device_attribute *attr, const char *buf, size_t count)
1416 {
1417 int retval;
1418 unsigned int input;
1419 struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
1420
1421 if (sscanf(buf, "%u", &input) != 1)
1422 return -EINVAL;
1423
1424 if (input != 1)
1425 return -EINVAL;
1426
1427 retval = fwu_do_read_config();
1428 if (retval < 0) {
1429 dev_err(&rmi4_data->i2c_client->dev,
1430 "%s: Failed to read config\n",
1431 __func__);
1432 return retval;
1433 }
1434
1435 return count;
1436 }
1437 #if 0
1438 static ssize_t fwu_sysfs_config_area_store(struct device *dev,
1439 struct device_attribute *attr, const char *buf, size_t count)
1440 {
1441 int retval;
1442 unsigned long config_area;
1443
1444 retval = sstrtoul(buf, 10, &config_area);
1445 if (retval)
1446 return retval;
1447
1448 fwu->config_area = config_area;
1449
1450 return count;
1451 }
1452
1453 static ssize_t fwu_sysfs_image_size_store(struct device *dev,
1454 struct device_attribute *attr, const char *buf, size_t count)
1455 {
1456 int retval;
1457 unsigned long size;
1458 struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
1459
1460 retval = sstrtoul(buf, 10, &size);
1461 if (retval)
1462 return retval;
1463
1464 fwu->image_size = size;
1465 fwu->data_pos = 0;
1466
1467 kfree(fwu->ext_data_source);
1468 fwu->ext_data_source = kzalloc(fwu->image_size, GFP_KERNEL);
1469 if (!fwu->ext_data_source) {
1470 dev_err(&rmi4_data->i2c_client->dev,
1471 "%s: Failed to alloc mem for image data\n",
1472 __func__);
1473 return -ENOMEM;
1474 }
1475
1476 return count;
1477 }
1478 #endif
1479 static ssize_t fwu_sysfs_block_size_show(struct device *dev,
1480 struct device_attribute *attr, char *buf)
1481 {
1482 return snprintf(buf, PAGE_SIZE, "%u\n", fwu->block_size);
1483 }
1484
1485 static ssize_t fwu_sysfs_firmware_block_count_show(struct device *dev,
1486 struct device_attribute *attr, char *buf)
1487 {
1488 return snprintf(buf, PAGE_SIZE, "%u\n", fwu->fw_block_count);
1489 }
1490
1491 static ssize_t fwu_sysfs_configuration_block_count_show(struct device *dev,
1492 struct device_attribute *attr, char *buf)
1493 {
1494 return snprintf(buf, PAGE_SIZE, "%u\n", fwu->config_block_count);
1495 }
1496
1497 static ssize_t fwu_sysfs_perm_config_block_count_show(struct device *dev,
1498 struct device_attribute *attr, char *buf)
1499 {
1500 return snprintf(buf, PAGE_SIZE, "%u\n", fwu->perm_config_block_count);
1501 }
1502
1503 static ssize_t fwu_sysfs_bl_config_block_count_show(struct device *dev,
1504 struct device_attribute *attr, char *buf)
1505 {
1506 return snprintf(buf, PAGE_SIZE, "%u\n", fwu->bl_config_block_count);
1507 }
1508
1509 static ssize_t fwu_sysfs_disp_config_block_count_show(struct device *dev,
1510 struct device_attribute *attr, char *buf)
1511 {
1512 return snprintf(buf, PAGE_SIZE, "%u\n", fwu->disp_config_block_count);
1513 }
1514
1515 static void synaptics_rmi4_fwu_attn(struct i2c_client *client,
1516 unsigned char intr_mask)
1517 {
1518 if (fwu->intr_mask & intr_mask)
1519 fwu_read_f34_flash_status();
1520
1521 return;
1522 }
1523
1524
1525 /**
1526 * synaptics_rmi4_set_page()
1527 *
1528 * Called by synaptics_rmi4_i2c_read() and synaptics_rmi4_i2c_write().
1529 *
1530 * This function writes to the page select register to switch to the
1531 * assigned page.
1532 */
1533 #if 0
1534 static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *rmi4_data,
1535 unsigned int address)
1536 {
1537 int retval = 0;
1538 unsigned char retry;
1539 unsigned char buf[PAGE_SELECT_LEN];
1540 unsigned char page;
1541 struct i2c_client *i2c = rmi4_data->i2c_client;
1542
1543 page = ((address >> 8) & MASK_8BIT);
1544 if (page != rmi4_data->current_page) {
1545 buf[0] = MASK_8BIT;
1546 buf[1] = page;
1547 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
1548 retval = i2c_master_send(i2c, buf, PAGE_SELECT_LEN);
1549 if (retval != PAGE_SELECT_LEN) {
1550 dev_err(&i2c->dev,
1551 "%s: I2C retry %d\n",
1552 __func__, retry + 1);
1553 msleep(20);
1554 } else {
1555 rmi4_data->current_page = page;
1556 break;
1557 }
1558 }
1559 } else {
1560 retval = PAGE_SELECT_LEN;
1561 }
1562
1563 return retval;
1564 }
1565 #endif
1566 /**
1567 * synaptics_rmi4_i2c_read()
1568 *
1569 * Called by various functions in this driver, and also exported to
1570 * other expansion Function modules such as rmi_dev.
1571 *
1572 * This function reads data of an arbitrary length from the sensor,
1573 * starting from an assigned register address of the sensor, via I2C
1574 * with a retry mechanism.
1575 */
1576 static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data,
1577 unsigned short addr, unsigned char *data, unsigned short length)
1578 {
1579 return tpd_i2c_read_data(rmi4_data->i2c_client, addr, data, length);
1580
1581 /*
1582 int retval;
1583 unsigned char retry;
1584 unsigned char buf;
1585 struct i2c_msg msg[] = {
1586 {
1587 .addr = rmi4_data->i2c_client->addr,
1588 .flags = 0,
1589 .len = 1,
1590 .buf = &buf,
1591 },
1592 {
1593 .addr = rmi4_data->i2c_client->addr,
1594 .flags = I2C_M_RD,
1595 .len = length,
1596 .buf = data,
1597 },
1598 };
1599
1600 buf = addr & MASK_8BIT;
1601
1602 mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
1603
1604 retval = synaptics_rmi4_set_page(rmi4_data, addr);
1605 if (retval != PAGE_SELECT_LEN)
1606 goto exit;
1607
1608 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
1609 if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 2) == 2) {
1610 retval = length;
1611 break;
1612 }
1613 dev_err(&rmi4_data->i2c_client->dev,
1614 "%s: I2C retry %d\n",
1615 __func__, retry + 1);
1616 msleep(20);
1617 }
1618
1619 if (retry == SYN_I2C_RETRY_TIMES) {
1620 dev_err(&rmi4_data->i2c_client->dev,
1621 "%s: I2C read over retry limit\n",
1622 __func__);
1623 retval = -EIO;
1624 }
1625
1626 exit:
1627 mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
1628
1629 return retval;*/
1630 }
1631
1632 /**
1633 * synaptics_rmi4_i2c_write()
1634 *
1635 * Called by various functions in this driver, and also exported to
1636 * other expansion Function modules such as rmi_dev.
1637 *
1638 * This function writes data of an arbitrary length to the sensor,
1639 * starting from an assigned register address of the sensor, via I2C with
1640 * a retry mechanism.
1641 */
1642 static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data,
1643 unsigned short addr, unsigned char *data, unsigned short length)
1644 {
1645 return tpd_i2c_write_data(rmi4_data->i2c_client, addr, data, length);
1646
1647 /* int retval;
1648 unsigned char retry;
1649 unsigned char buf[length + 1];
1650 struct i2c_msg msg[] = {
1651 {
1652 .addr = rmi4_data->i2c_client->addr,
1653 .flags = 0,
1654 .len = length + 1,
1655 .buf = buf,
1656 }
1657 };
1658
1659 mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
1660
1661 retval = synaptics_rmi4_set_page(rmi4_data, addr);
1662 if (retval != PAGE_SELECT_LEN)
1663 goto exit;
1664
1665 buf[0] = addr & MASK_8BIT;
1666 memcpy(&buf[1], &data[0], length);
1667
1668 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
1669 if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 1) == 1) {
1670 retval = length;
1671 break;
1672 }
1673 dev_err(&rmi4_data->i2c_client->dev,
1674 "%s: I2C retry %d\n",
1675 __func__, retry + 1);
1676 msleep(20);
1677 }
1678
1679 if (retry == SYN_I2C_RETRY_TIMES) {
1680 dev_err(&rmi4_data->i2c_client->dev,
1681 "%s: I2C write over retry limit\n",
1682 __func__);
1683 retval = -EIO;
1684 }
1685
1686 exit:
1687 mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
1688
1689 return retval;*/
1690 }
1691
1692 /**
1693 * synaptics_rmi4_irq_enable()
1694 *
1695 * Called by synaptics_rmi4_probe() and the power management functions
1696 * in this driver and also exported to other expansion Function modules
1697 * such as rmi_dev.
1698 *
1699 * This function handles the enabling and disabling of the attention
1700 * irq including the setting up of the ISR thread.
1701 */
1702 static int synaptics_rmi4_irq_enable(struct synaptics_rmi4_data *rmi4_data,
1703 bool enable)
1704 {
1705 int retval = 0;
1706 unsigned char intr_status;
1707 //const struct synaptics_dsx_platform_data *platform_data = rmi4_data->i2c_client->dev.platform_data;
1708
1709 if (enable) {
1710 if (rmi4_data->irq_enabled)
1711 return retval;
1712
1713 /* Clear interrupts first */
1714 retval = synaptics_rmi4_i2c_read(rmi4_data,
1715 rmi4_data->f01_data_base_addr + 1,
1716 &intr_status,
1717 rmi4_data->num_of_intr_regs);
1718 if (retval < 0)
1719 return retval;
1720
1721 /* retval = request_threaded_irq(rmi4_data->irq, NULL,
1722 synaptics_rmi4_irq, platform_data->irq_type,
1723 DRIVER_NAME, rmi4_data);
1724 if (retval < 0) {
1725 dev_err(&rmi4_data->i2c_client->dev,
1726 "%s: Failed to create irq thread\n",
1727 __func__);
1728 return retval;
1729 }*/
1730
1731 rmi4_data->irq_enabled = true;
1732 } else {
1733 if (rmi4_data->irq_enabled) {
1734 disable_irq(rmi4_data->irq);
1735 free_irq(rmi4_data->irq, rmi4_data);
1736 rmi4_data->irq_enabled = false;
1737 }
1738 }
1739
1740 return retval;
1741 }
1742
1743
1744 static int synaptics_rmi4_alloc_fh(struct synaptics_rmi4_fn **fhandler,
1745 struct synaptics_rmi4_fn_desc *rmi_fd, int page_number)
1746 {
1747 if (!(*fhandler))
1748 *fhandler = kmalloc(sizeof(**fhandler), GFP_KERNEL);
1749
1750 if (!(*fhandler))
1751 return -ENOMEM;
1752
1753 (*fhandler)->full_addr.data_base =
1754 (rmi_fd->data_base_addr |
1755 (page_number << 8));
1756 (*fhandler)->full_addr.ctrl_base =
1757 (rmi_fd->ctrl_base_addr |
1758 (page_number << 8));
1759 (*fhandler)->full_addr.cmd_base =
1760 (rmi_fd->cmd_base_addr |
1761 (page_number << 8));
1762 (*fhandler)->full_addr.query_base =
1763 (rmi_fd->query_base_addr |
1764 (page_number << 8));
1765
1766 return 0;
1767 }
1768
1769 static int synaptics_rmi4_f11_init(struct synaptics_rmi4_data *rmi4_data,
1770 struct synaptics_rmi4_fn *fhandler,
1771 struct synaptics_rmi4_fn_desc *fd,
1772 unsigned int intr_count)
1773 {
1774 int retval;
1775 unsigned char ii;
1776 unsigned char intr_offset;
1777 unsigned char abs_data_size;
1778 unsigned char abs_data_blk_size;
1779 unsigned char query[F11_STD_QUERY_LEN];
1780 unsigned char control[F11_STD_CTRL_LEN];
1781
1782 fhandler->fn_number = fd->fn_number;
1783 fhandler->num_of_data_sources = fd->intr_src_count;
1784
1785 retval = synaptics_rmi4_i2c_read(rmi4_data,
1786 fhandler->full_addr.query_base,
1787 query,
1788 sizeof(query));
1789 if (retval < 0)
1790 return retval;
1791
1792 /* Maximum number of fingers supported */
1793 if ((query[1] & MASK_3BIT) <= 4)
1794 fhandler->num_of_data_points = (query[1] & MASK_3BIT) + 1;
1795 else if ((query[1] & MASK_3BIT) == 5)
1796 fhandler->num_of_data_points = 10;
1797
1798 retval = synaptics_rmi4_i2c_read(rmi4_data,
1799 fhandler->full_addr.ctrl_base,
1800 control,
1801 sizeof(control));
1802 if (retval < 0)
1803 return retval;
1804
1805 /* Maximum x and y */
1806 rmi4_data->sensor_max_x = ((control[6] & MASK_8BIT) << 0) |
1807 ((control[7] & MASK_4BIT) << 8);
1808 rmi4_data->sensor_max_y = ((control[8] & MASK_8BIT) << 0) |
1809 ((control[9] & MASK_4BIT) << 8);
1810 dev_dbg(&rmi4_data->i2c_client->dev,
1811 "%s: Function %02x max x = %d max y = %d\n",
1812 __func__, fhandler->fn_number,
1813 rmi4_data->sensor_max_x,
1814 rmi4_data->sensor_max_y);
1815
1816 fhandler->intr_reg_num = (intr_count + 7) / 8;
1817 if (fhandler->intr_reg_num != 0)
1818 fhandler->intr_reg_num -= 1;
1819
1820 /* Set an enable bit for each data source */
1821 intr_offset = intr_count % 8;
1822 fhandler->intr_mask = 0;
1823 for (ii = intr_offset;
1824 ii < ((fd->intr_src_count & MASK_3BIT) +
1825 intr_offset);
1826 ii++)
1827 fhandler->intr_mask |= 1 << ii;
1828
1829 abs_data_size = query[5] & MASK_2BIT;
1830 abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0));
1831 fhandler->size_of_data_register_block = abs_data_blk_size;
1832
1833 return retval;
1834 }
1835
1836 /**
1837 * synaptics_rmi4_f12_init()
1838 *
1839 * Called by synaptics_rmi4_query_device().
1840 *
1841 * This funtion parses information from the Function 12 registers and
1842 * determines the number of fingers supported, offset to the data1
1843 * register, x and y data ranges, offset to the associated interrupt
1844 * status register, interrupt bit mask, and allocates memory resources
1845 * for finger data acquisition.
1846 */
1847 static int synaptics_rmi4_f12_init(struct synaptics_rmi4_data *rmi4_data,
1848 struct synaptics_rmi4_fn *fhandler,
1849 struct synaptics_rmi4_fn_desc *fd,
1850 unsigned int intr_count)
1851 {
1852 int retval;
1853 unsigned char ii;
1854 unsigned char intr_offset;
1855 unsigned char ctrl_8_offset;
1856 unsigned char ctrl_23_offset;
1857 struct synaptics_rmi4_f12_query_5 query_5;
1858 struct synaptics_rmi4_f12_query_8 query_8;
1859 struct synaptics_rmi4_f12_ctrl_8 ctrl_8;
1860 struct synaptics_rmi4_f12_ctrl_23 ctrl_23;
1861 struct synaptics_rmi4_f12_finger_data *finger_data_list;
1862
1863 fhandler->fn_number = fd->fn_number;
1864 fhandler->num_of_data_sources = fd->intr_src_count;
1865
1866 retval = synaptics_rmi4_i2c_read(rmi4_data,
1867 fhandler->full_addr.query_base + 5,
1868 query_5.data,
1869 sizeof(query_5.data));
1870 if (retval < 0)
1871 return retval;
1872
1873 ctrl_8_offset = query_5.ctrl0_is_present +
1874 query_5.ctrl1_is_present +
1875 query_5.ctrl2_is_present +
1876 query_5.ctrl3_is_present +
1877 query_5.ctrl4_is_present +
1878 query_5.ctrl5_is_present +
1879 query_5.ctrl6_is_present +
1880 query_5.ctrl7_is_present;
1881
1882 ctrl_23_offset = ctrl_8_offset +
1883 query_5.ctrl8_is_present +
1884 query_5.ctrl9_is_present +
1885 query_5.ctrl10_is_present +
1886 query_5.ctrl11_is_present +
1887 query_5.ctrl12_is_present +
1888 query_5.ctrl13_is_present +
1889 query_5.ctrl14_is_present +
1890 query_5.ctrl15_is_present +
1891 query_5.ctrl16_is_present +
1892 query_5.ctrl17_is_present +
1893 query_5.ctrl18_is_present +
1894 query_5.ctrl19_is_present +
1895 query_5.ctrl20_is_present +
1896 query_5.ctrl21_is_present +
1897 query_5.ctrl22_is_present;
1898
1899 retval = synaptics_rmi4_i2c_read(rmi4_data,
1900 fhandler->full_addr.ctrl_base + ctrl_23_offset,
1901 ctrl_23.data,
1902 sizeof(ctrl_23.data));
1903 if (retval < 0)
1904 return retval;
1905
1906 /* Maximum number of fingers supported */
1907 fhandler->num_of_data_points = ctrl_23.max_reported_objects;
1908
1909 retval = synaptics_rmi4_i2c_read(rmi4_data,
1910 fhandler->full_addr.query_base + 8,
1911 query_8.data,
1912 sizeof(query_8.data));
1913 if (retval < 0)
1914 return retval;
1915
1916 /* Determine the presence of the Data0 register */
1917 fhandler->data1_offset = query_8.data0_is_present;
1918
1919 retval = synaptics_rmi4_i2c_read(rmi4_data,
1920 fhandler->full_addr.ctrl_base + ctrl_8_offset,
1921 ctrl_8.data,
1922 sizeof(ctrl_8.data));
1923 if (retval < 0)
1924 return retval;
1925
1926 /* Maximum x and y */
1927 rmi4_data->sensor_max_x =
1928 ((unsigned short)ctrl_8.max_x_coord_lsb << 0) |
1929 ((unsigned short)ctrl_8.max_x_coord_msb << 8);
1930 rmi4_data->sensor_max_y =
1931 ((unsigned short)ctrl_8.max_y_coord_lsb << 0) |
1932 ((unsigned short)ctrl_8.max_y_coord_msb << 8);
1933 dev_dbg(&rmi4_data->i2c_client->dev,
1934 "%s: Function %02x max x = %d max y = %d\n",
1935 __func__, fhandler->fn_number,
1936 rmi4_data->sensor_max_x,
1937 rmi4_data->sensor_max_y);
1938
1939 rmi4_data->num_of_rx = ctrl_8.num_of_rx;
1940 rmi4_data->num_of_tx = ctrl_8.num_of_tx;
1941
1942 fhandler->intr_reg_num = (intr_count + 7) / 8;
1943 if (fhandler->intr_reg_num != 0)
1944 fhandler->intr_reg_num -= 1;
1945
1946 /* Set an enable bit for each data source */
1947 intr_offset = intr_count % 8;
1948 fhandler->intr_mask = 0;
1949 for (ii = intr_offset;
1950 ii < ((fd->intr_src_count & MASK_3BIT) +
1951 intr_offset);
1952 ii++)
1953 fhandler->intr_mask |= 1 << ii;
1954
1955 /* Allocate memory for finger data storage space */
1956 fhandler->data_size = fhandler->num_of_data_points *
1957 sizeof(struct synaptics_rmi4_f12_finger_data);
1958 finger_data_list = kmalloc(fhandler->data_size, GFP_KERNEL);
1959 fhandler->data = (void *)finger_data_list;
1960
1961 return retval;
1962 }
1963
1964
1965 static void synaptics_rmi4_f1a_kfree(struct synaptics_rmi4_fn *fhandler)
1966 {
1967 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
1968
1969 if (f1a) {
1970 kfree(f1a->button_data_buffer);
1971 kfree(f1a->button_map);
1972 kfree(f1a);
1973 fhandler->data = NULL;
1974 }
1975
1976 return;
1977 }
1978
1979 static int synaptics_rmi4_f1a_alloc_mem(struct synaptics_rmi4_data *rmi4_data,
1980 struct synaptics_rmi4_fn *fhandler)
1981 {
1982 int retval;
1983 struct synaptics_rmi4_f1a_handle *f1a;
1984
1985 f1a = kzalloc(sizeof(*f1a), GFP_KERNEL);
1986 if (!f1a) {
1987 dev_err(&rmi4_data->i2c_client->dev,
1988 "%s: Failed to alloc mem for function handle\n",
1989 __func__);
1990 return -ENOMEM;
1991 }
1992
1993 fhandler->data = (void *)f1a;
1994
1995 retval = synaptics_rmi4_i2c_read(rmi4_data,
1996 fhandler->full_addr.query_base,
1997 f1a->button_query.data,
1998 sizeof(f1a->button_query.data));
1999 if (retval < 0) {
2000 dev_err(&rmi4_data->i2c_client->dev,
2001 "%s: Failed to read query registers\n",
2002 __func__);
2003 return retval;
2004 }
2005
2006 f1a->button_count = f1a->button_query.max_button_count + 1;
2007 f1a->button_bitmask_size = (f1a->button_count + 7) / 8;
2008
2009 f1a->button_data_buffer = kcalloc(f1a->button_bitmask_size,
2010 sizeof(*(f1a->button_data_buffer)), GFP_KERNEL);
2011 if (!f1a->button_data_buffer) {
2012 dev_err(&rmi4_data->i2c_client->dev,
2013 "%s: Failed to alloc mem for data buffer\n",
2014 __func__);
2015 return -ENOMEM;
2016 }
2017
2018 f1a->button_map = kcalloc(f1a->button_count,
2019 sizeof(*(f1a->button_map)), GFP_KERNEL);
2020 if (!f1a->button_map) {
2021 dev_err(&rmi4_data->i2c_client->dev,
2022 "%s: Failed to alloc mem for button map\n",
2023 __func__);
2024 return -ENOMEM;
2025 }
2026
2027 return 0;
2028 }
2029
2030 static int synaptics_rmi4_f1a_button_map(struct synaptics_rmi4_data *rmi4_data,
2031 struct synaptics_rmi4_fn *fhandler)
2032 {
2033 //unsigned char ii;
2034 //struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
2035 /* const struct synaptics_dsx_platform_data *pdata = rmi4_data->board;
2036
2037 if (!pdata->f1a_button_map) {
2038 dev_err(&rmi4_data->i2c_client->dev,
2039 "%s: f1a_button_map is NULL in board file\n",
2040 __func__);
2041 return -ENODEV;
2042 } else if (!pdata->f1a_button_map->map) {
2043 dev_err(&rmi4_data->i2c_client->dev,
2044 "%s: Button map is missing in board file\n",
2045 __func__);
2046 return -ENODEV;
2047 } else {
2048 if (pdata->f1a_button_map->nbuttons != f1a->button_count) {
2049 f1a->valid_button_count = min(f1a->button_count,
2050 pdata->f1a_button_map->nbuttons);
2051 } else {
2052 f1a->valid_button_count = f1a->button_count;
2053 }
2054
2055 for (ii = 0; ii < f1a->valid_button_count; ii++)
2056 f1a->button_map[ii] = pdata->f1a_button_map->map[ii];
2057 }*/
2058
2059 return 0;
2060 }
2061
2062
2063 static int synaptics_rmi4_f1a_init(struct synaptics_rmi4_data *rmi4_data,
2064 struct synaptics_rmi4_fn *fhandler,
2065 struct synaptics_rmi4_fn_desc *fd,
2066 unsigned int intr_count)
2067 {
2068 int retval;
2069 unsigned char ii;
2070 unsigned short intr_offset;
2071
2072 fhandler->fn_number = fd->fn_number;
2073 fhandler->num_of_data_sources = fd->intr_src_count;
2074
2075 fhandler->intr_reg_num = (intr_count + 7) / 8;
2076 if (fhandler->intr_reg_num != 0)
2077 fhandler->intr_reg_num -= 1;
2078
2079 /* Set an enable bit for each data source */
2080 intr_offset = intr_count % 8;
2081 fhandler->intr_mask = 0;
2082 for (ii = intr_offset;
2083 ii < ((fd->intr_src_count & MASK_3BIT) +
2084 intr_offset);
2085 ii++)
2086 fhandler->intr_mask |= 1 << ii;
2087
2088 retval = synaptics_rmi4_f1a_alloc_mem(rmi4_data, fhandler);
2089 if (retval < 0)
2090 goto error_exit;
2091
2092 retval = synaptics_rmi4_f1a_button_map(rmi4_data, fhandler);
2093 if (retval < 0)
2094 goto error_exit;
2095
2096 rmi4_data->button_0d_enabled = 1;
2097
2098 return 0;
2099
2100 error_exit:
2101 synaptics_rmi4_f1a_kfree(fhandler);
2102
2103 return retval;
2104 }
2105
2106
2107
2108 /**
2109 * synaptics_rmi4_query_device()
2110 *
2111 * Called by synaptics_rmi4_probe().
2112 *
2113 * This funtion scans the page description table, records the offsets
2114 * to the register types of Function $01, sets up the function handlers
2115 * for Function $11 and Function $12, determines the number of interrupt
2116 * sources from the sensor, adds valid Functions with data inputs to the
2117 * Function linked list, parses information from the query registers of
2118 * Function $01, and enables the interrupt sources from the valid Functions
2119 * with data inputs.
2120 */
2121 static int synaptics_rmi4_query_device(struct synaptics_rmi4_data *rmi4_data)
2122 {
2123 int retval;
2124 unsigned char ii;
2125 unsigned char page_number;
2126 unsigned char intr_count = 0;
2127 unsigned char data_sources = 0;
2128 unsigned char f01_query[F01_STD_QUERY_LEN];
2129 unsigned short pdt_entry_addr;
2130 unsigned short intr_addr;
2131 struct synaptics_rmi4_f01_device_status status;
2132 struct synaptics_rmi4_fn_desc rmi_fd;
2133 struct synaptics_rmi4_fn *fhandler;
2134 struct synaptics_rmi4_device_info *rmi;
2135
2136 rmi = &(rmi4_data->rmi4_mod_info);
2137
2138 INIT_LIST_HEAD(&rmi->support_fn_list);
2139
2140 /* Scan the page description tables of the pages to service */
2141 for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
2142 for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
2143 pdt_entry_addr -= PDT_ENTRY_SIZE) {
2144 pdt_entry_addr |= (page_number << 8);
2145
2146 retval = synaptics_rmi4_i2c_read(rmi4_data,
2147 pdt_entry_addr,
2148 (unsigned char *)&rmi_fd,
2149 sizeof(rmi_fd));
2150 if (retval < 0)
2151 return retval;
2152
2153 fhandler = NULL;
2154
2155 if (rmi_fd.fn_number == 0) {
2156 TPD_DMESG(
2157 "%s: Reached end of PDT\n",
2158 __func__);
2159 break;
2160 }
2161
2162 TPD_DMESG(
2163 "%s: F%02x found (page %d)\n",
2164 __func__, rmi_fd.fn_number,
2165 page_number);
2166
2167 switch (rmi_fd.fn_number) {
2168 case SYNAPTICS_RMI4_F01:
2169 rmi4_data->f01_query_base_addr =
2170 rmi_fd.query_base_addr;
2171 rmi4_data->f01_ctrl_base_addr =
2172 rmi_fd.ctrl_base_addr;
2173 rmi4_data->f01_data_base_addr =
2174 rmi_fd.data_base_addr;
2175 rmi4_data->f01_cmd_base_addr =
2176 rmi_fd.cmd_base_addr;
2177
2178 retval = synaptics_rmi4_i2c_read(rmi4_data,
2179 rmi4_data->f01_data_base_addr,
2180 status.data,
2181 sizeof(status.data));
2182 if (retval < 0)
2183 return retval;
2184
2185 if (status.flash_prog == 1) {
2186 TPD_DMESG("%s: In flash prog mode, status = 0x%02x\n",
2187 __func__,
2188 status.status_code);
2189 goto flash_prog_mode;
2190 }
2191 break;
2192 case SYNAPTICS_RMI4_F11:
2193 if (rmi_fd.intr_src_count == 0)
2194 break;
2195
2196 retval = synaptics_rmi4_alloc_fh(&fhandler,
2197 &rmi_fd, page_number);
2198 if (retval < 0) {
2199 dev_err(&rmi4_data->i2c_client->dev,
2200 "%s: Failed to alloc for F%d\n",
2201 __func__,
2202 rmi_fd.fn_number);
2203 return retval;
2204 }
2205
2206 retval = synaptics_rmi4_f11_init(rmi4_data,
2207 fhandler, &rmi_fd, intr_count);
2208 if (retval < 0)
2209 return retval;
2210 break;
2211 case SYNAPTICS_RMI4_F12:
2212 if (rmi_fd.intr_src_count == 0)
2213 break;
2214
2215 retval = synaptics_rmi4_alloc_fh(&fhandler,
2216 &rmi_fd, page_number);
2217 if (retval < 0) {
2218 dev_err(&rmi4_data->i2c_client->dev,
2219 "%s: Failed to alloc for F%d\n",
2220 __func__,
2221 rmi_fd.fn_number);
2222 return retval;
2223 }
2224
2225 retval = synaptics_rmi4_f12_init(rmi4_data,
2226 fhandler, &rmi_fd, intr_count);
2227 if (retval < 0)
2228 return retval;
2229 break;
2230 case SYNAPTICS_RMI4_F1A:
2231 if (rmi_fd.intr_src_count == 0)
2232 break;
2233
2234 retval = synaptics_rmi4_alloc_fh(&fhandler,
2235 &rmi_fd, page_number);
2236 if (retval < 0) {
2237 dev_err(&rmi4_data->i2c_client->dev,
2238 "%s: Failed to alloc for F%d\n",
2239 __func__,
2240 rmi_fd.fn_number);
2241 return retval;
2242 }
2243
2244 retval = synaptics_rmi4_f1a_init(rmi4_data,
2245 fhandler, &rmi_fd, intr_count);
2246 if (retval < 0)
2247 return retval;
2248 break;
2249 #if PROXIMITY
2250 case SYNAPTICS_RMI4_F51:
2251 if (rmi_fd.intr_src_count == 0)
2252 break;
2253
2254 retval = synaptics_rmi4_alloc_fh(&fhandler,
2255 &rmi_fd, page_number);
2256 if (retval < 0) {
2257 dev_err(&rmi4_data->i2c_client->dev,
2258 "%s: Failed to alloc for F%d\n",
2259 __func__,
2260 rmi_fd.fn_number);
2261 return retval;
2262 }
2263
2264 retval = synaptics_rmi4_f51_init(rmi4_data,
2265 fhandler, &rmi_fd, intr_count);
2266 if (retval < 0)
2267 return retval;
2268 break;
2269 #endif
2270 }
2271
2272 /* Accumulate the interrupt count */
2273 intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
2274
2275 if (fhandler && rmi_fd.intr_src_count) {
2276 list_add_tail(&fhandler->link,
2277 &rmi->support_fn_list);
2278 }
2279 }
2280 }
2281
2282 flash_prog_mode:
2283 rmi4_data->num_of_intr_regs = (intr_count + 7) / 8;
2284 TPD_DMESG(
2285 "%s: Number of interrupt registers = %d\n",
2286 __func__, rmi4_data->num_of_intr_regs);
2287
2288 retval = synaptics_rmi4_i2c_read(rmi4_data,
2289 rmi4_data->f01_query_base_addr,
2290 f01_query,
2291 sizeof(f01_query));
2292 if (retval < 0)
2293 return retval;
2294
2295 /* RMI Version 4.0 currently supported */
2296 rmi->version_major = 4;
2297 rmi->version_minor = 0;
2298
2299 rmi->manufacturer_id = f01_query[0];
2300 rmi->product_props = f01_query[1];
2301 rmi->product_info[0] = f01_query[2] & MASK_7BIT;
2302 rmi->product_info[1] = f01_query[3] & MASK_7BIT;
2303 rmi->date_code[0] = f01_query[4] & MASK_5BIT;
2304 rmi->date_code[1] = f01_query[5] & MASK_4BIT;
2305 rmi->date_code[2] = f01_query[6] & MASK_5BIT;
2306 rmi->tester_id = ((f01_query[7] & MASK_7BIT) << 8) |
2307 (f01_query[8] & MASK_7BIT);
2308 rmi->serial_number = ((f01_query[9] & MASK_7BIT) << 8) |
2309 (f01_query[10] & MASK_7BIT);
2310 memcpy(rmi->product_id_string, &f01_query[11], 10);
2311
2312 if (rmi->manufacturer_id != 1) {
2313 dev_err(&rmi4_data->i2c_client->dev,
2314 "%s: Non-Synaptics device found, manufacturer ID = %d\n",
2315 __func__, rmi->manufacturer_id);
2316 }
2317
2318 memset(rmi4_data->intr_mask, 0x00, sizeof(rmi4_data->intr_mask));
2319
2320 /*
2321 * Map out the interrupt bit masks for the interrupt sources
2322 * from the registered function handlers.
2323 */
2324 list_for_each_entry(fhandler, &rmi->support_fn_list, link)
2325 data_sources += fhandler->num_of_data_sources;
2326 if (data_sources) {
2327 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
2328 if (fhandler->num_of_data_sources) {
2329 rmi4_data->intr_mask[fhandler->intr_reg_num] |=
2330 fhandler->intr_mask;
2331 }
2332 }
2333 }
2334
2335 /* Enable the interrupt sources */
2336 for (ii = 0; ii < rmi4_data->num_of_intr_regs; ii++) {
2337 if (rmi4_data->intr_mask[ii] != 0x00) {
2338 TPD_DMESG("%s: Interrupt enable mask %d = 0x%02x\n",
2339 __func__, ii, rmi4_data->intr_mask[ii]);
2340 intr_addr = rmi4_data->f01_ctrl_base_addr + 1 + ii;
2341 retval = synaptics_rmi4_i2c_write(rmi4_data,
2342 intr_addr,
2343 &(rmi4_data->intr_mask[ii]),
2344 sizeof(rmi4_data->intr_mask[ii]));
2345 if (retval < 0)
2346 return retval;
2347 }
2348 }
2349
2350 return 0;
2351 }
2352
2353 static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data)
2354 {
2355 int retval;
2356 unsigned char command = 0x01;
2357 //struct synaptics_rmi4_fn *fhandler;
2358 struct synaptics_rmi4_device_info *rmi;
2359
2360 rmi = &(rmi4_data->rmi4_mod_info);
2361
2362 retval = synaptics_rmi4_i2c_write(rmi4_data,
2363 rmi4_data->f01_cmd_base_addr,
2364 &command,
2365 sizeof(command));
2366 if (retval < 0) {
2367 dev_err(&rmi4_data->i2c_client->dev,
2368 "%s: Failed to issue reset command, error = %d\n",
2369 __func__, retval);
2370 return retval;
2371 }
2372
2373 msleep(100);
2374
2375 /* list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
2376 printk("kfree handler = 0x%x\n", fhandler);
2377 if (fhandler) {
2378 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
2379 synaptics_rmi4_f1a_kfree(fhandler);
2380 else
2381 kfree(fhandler->data);
2382 kfree(fhandler);
2383 }
2384 }*/
2385
2386 retval = synaptics_rmi4_query_device(rmi4_data);
2387 if (retval < 0) {
2388 dev_err(&rmi4_data->i2c_client->dev,
2389 "%s: Failed to query device\n",
2390 __func__);
2391 return retval;
2392 }
2393
2394 return 0;
2395 }
2396
2397 static int synaptics_rmi4_fwu_init(struct i2c_client *client)
2398 {
2399 int retval;
2400 unsigned char attr_count;
2401 struct pdt_properties pdt_props;
2402
2403 TPD_DMESG("%s:enter \n", __func__);
2404
2405 fwu = kzalloc(sizeof(*fwu), GFP_KERNEL);
2406 if (!fwu) {
2407 dev_err(&client->dev,
2408 "%s: Failed to alloc mem for fwu\n",
2409 __func__);
2410 goto exit;
2411 }
2412
2413 fwu->rmi4_data = kzalloc(sizeof(struct synaptics_rmi4_data), GFP_KERNEL);
2414 if (!fwu->rmi4_data) {
2415 dev_err(&client->dev,
2416 "%s: Failed to alloc mem for rmi4_data\n",
2417 __func__);
2418 retval = -ENOMEM;
2419 goto exit_free_fwu;
2420 }
2421
2422 fwu->fn_ptr = kzalloc(sizeof(*(fwu->fn_ptr)), GFP_KERNEL);
2423 if (!fwu->fn_ptr) {
2424 dev_err(&client->dev,
2425 "%s: Failed to alloc mem for fn_ptr\n",
2426 __func__);
2427 retval = -ENOMEM;
2428 goto exit_free_rmi4;
2429 }
2430
2431 fwu->rmi4_data->input_dev = tpd->dev;
2432 fwu->rmi4_data->i2c_client = client;
2433 fwu->fn_ptr->read = synaptics_rmi4_i2c_read;
2434 fwu->fn_ptr->write = synaptics_rmi4_i2c_write;
2435 fwu->fn_ptr->enable = synaptics_rmi4_irq_enable;
2436 fwu->rmi4_data->reset_device = synaptics_rmi4_reset_device;
2437
2438 synaptics_rmi4_query_device(fwu->rmi4_data);
2439
2440 mutex_init(&(fwu->rmi4_data->rmi4_io_ctrl_mutex));
2441
2442 retval = fwu->fn_ptr->read(fwu->rmi4_data,
2443 PDT_PROPS,
2444 pdt_props.data,
2445 sizeof(pdt_props.data));
2446 if (retval < 0) {
2447 TPD_DMESG("%s: Failed to read PDT properties, assuming 0x00\n",
2448 __func__);
2449 } else if (pdt_props.has_bsr) {
2450 TPD_DMESG(
2451 "%s: Reflash for LTS not currently supported\n",
2452 __func__);
2453 goto exit_free_mem;
2454 }
2455
2456 retval = fwu_scan_pdt();
2457 if (retval < 0)
2458 goto exit_free_mem;
2459
2460 /* fwu->productinfo1 = rmi4_data->rmi4_mod_info.product_info[0];
2461 fwu->productinfo2 = rmi4_data->rmi4_mod_info.product_info[1];
2462
2463 memcpy(fwu->product_id, rmi4_data->rmi4_mod_info.product_id_string,
2464 SYNAPTICS_RMI4_PRODUCT_ID_SIZE);
2465 fwu->product_id[SYNAPTICS_RMI4_PRODUCT_ID_SIZE] = 0;
2466
2467 dev_dbg(&rmi4_data->i2c_client->dev,
2468 "%s: F01 product info: 0x%04x 0x%04x\n",
2469 __func__, fwu->productinfo1, fwu->productinfo2);
2470 dev_dbg(&rmi4_data->i2c_client->dev,
2471 "%s: F01 product ID: %s\n",
2472 __func__, fwu->product_id);
2473 */
2474 retval = fwu_read_f34_queries();
2475 if (retval < 0)
2476 goto exit_free_mem;
2477
2478 TPD_DMESG("query_base_addr=0x%x, data_base_addr=0x%x, addr_f34_flash_control=0x%x, bootloader_id=%s\n",
2479 fwu->f34_fd.query_base_addr, fwu->f34_fd.data_base_addr,
2480 fwu->addr_f34_flash_control, fwu->bootloader_id);
2481
2482 fwu->initialized = true;
2483 // fwu->ext_data_source = synaImage;
2484 // fwu->config_area = UI_CONFIG_AREA;
2485
2486 properties_kobj_fwupdate = kobject_create_and_add("fwupdate", properties_kobj_synap);
2487
2488 if (!properties_kobj_fwupdate) {
2489 dev_err(&client->dev,
2490 "%s: Failed to create sysfs directory\n",
2491 __func__);
2492 goto err_sysfs_dir;
2493 }
2494 retval = sysfs_create_bin_file(properties_kobj_fwupdate, &dev_attr_data);
2495 if (retval < 0) {
2496 dev_err(&client->dev,
2497 "%s: Failed to create sysfs bin file\n",
2498 __func__);
2499 goto exit_free_mem;
2500 }
2501
2502 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
2503 //retval = sysfs_create_file(&tpd->dev->dev.kobj, &attrs[attr_count].attr);
2504 retval = sysfs_create_file(properties_kobj_fwupdate, &attrs[attr_count].attr);
2505 if (retval < 0) {
2506 dev_err(&client->dev,
2507 "%s: Failed to create sysfs attributes\n",
2508 __func__);
2509 retval = -ENODEV;
2510 goto exit_remove_attrs;
2511 }
2512 }
2513 /*
2514 attr_kobj = kobject_create_and_add("syna_reflash", NULL);
2515 retval = sysfs_create_group(attr_kobj,&syna_attr_group);
2516 if(!attr_kobj|| retval){
2517 printk("failed to create board_properties\n");
2518 goto err_sysfs;
2519 */
2520 // fwu_start_reflash();
2521
2522 return 0;
2523
2524 exit_remove_attrs:
2525 for (attr_count--; attr_count >= 0; attr_count--) {
2526 sysfs_remove_file(properties_kobj_fwupdate,&attrs[attr_count].attr);
2527 }
2528
2529 sysfs_remove_bin_file(properties_kobj_fwupdate, &dev_attr_data);
2530
2531 err_sysfs_dir:
2532
2533 exit_free_mem:
2534 kfree(fwu->fn_ptr);
2535
2536 exit_free_rmi4:
2537 kfree(fwu->rmi4_data);
2538
2539
2540 exit_free_fwu:
2541 kfree(fwu);
2542
2543 exit:
2544 return 0;
2545 }
2546
2547 static void synaptics_rmi4_fwu_remove(struct i2c_client *client)
2548 {
2549 unsigned char attr_count;
2550
2551 sysfs_remove_bin_file(&fwu->rmi4_data->input_dev->dev.kobj, &dev_attr_data);
2552
2553 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
2554 sysfs_remove_file(&fwu->rmi4_data->input_dev->dev.kobj,
2555 &attrs[attr_count].attr);
2556 }
2557
2558 kfree(fwu->fn_ptr);
2559 kfree(fwu);
2560
2561 complete(&remove_complete);
2562
2563 return;
2564 }
2565
2566 static int __init rmi4_fw_update_module_init(void)
2567 {
2568 synaptics_rmi4_new_function(RMI_FW_UPDATER, true,
2569 synaptics_rmi4_fwu_init,
2570 synaptics_rmi4_fwu_remove,
2571 synaptics_rmi4_fwu_attn);
2572 return 0;
2573 }
2574
2575 static void __exit rmi4_fw_update_module_exit(void)
2576 {
2577 init_completion(&remove_complete);
2578 synaptics_rmi4_new_function(RMI_FW_UPDATER, false,
2579 synaptics_rmi4_fwu_init,
2580 synaptics_rmi4_fwu_remove,
2581 synaptics_rmi4_fwu_attn);
2582 wait_for_completion(&remove_complete);
2583 return;
2584 }
2585
2586 module_init(rmi4_fw_update_module_init);
2587 module_exit(rmi4_fw_update_module_exit);
2588
2589 MODULE_AUTHOR("Synaptics, Inc.");
2590 MODULE_DESCRIPTION("RMI4 FW Update Module");
2591 MODULE_LICENSE("GPL");
2592 MODULE_VERSION(SYNAPTICS_RMI4_DRIVER_VERSION);