3 #include <linux/delay.h>
12 #define CT36X_CHIP_FLASH_SECTOR_NUM 256
13 #define CT36X_CHIP_FLASH_SECTOR_SIZE 128
14 #define CT36X_CHIP_FLASH_SOURCE_SIZE 8
16 static unsigned char binary_data[] = {
17 //#include "CT365Five3020D_V42120523A.dat"
18 //#include "CT365_THSD_40X28_V05_120827_I2C0X01.dat"
22 static void ct36x_chip_set_idle(struct i2c_client *client, unsigned char *buf)
24 if ( CT36X_TS_CHIP_DEBUG )
25 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
29 ct36x_ts_reg_write(client, 0x7F, buf, 2);
33 static void ct36x_chip_rst_offset(struct i2c_client *client, unsigned char *buf)
35 if ( CT36X_TS_CHIP_DEBUG )
36 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
39 ct36x_ts_reg_write(client, 0x7F, buf, 1);
43 static int ct36x_chip_get_busstatus(struct i2c_client *client, unsigned char *buf)
45 if ( CT36X_TS_CHIP_DEBUG )
46 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
48 ct36x_ts_reg_read(client, 0x7F, buf, 1);
54 static int ct36x_chip_erase_flash(struct i2c_client *client, unsigned char *buf)
58 if ( CT36X_TS_CHIP_DEBUG )
59 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
65 ct36x_ts_reg_write(client, 0x7F, buf, 3);
68 // Reset I2C offset address
69 ct36x_chip_rst_offset(client, buf);
71 // Read I2C bus status
72 ret = ct36x_chip_get_busstatus(client, buf);
81 ** Prepare code segment
83 static int ct36x_chip_set_code(unsigned int flash_addr, unsigned char *buf)
85 unsigned char cod_chksum;
87 //if ( CT36X_TS_CHIP_DEBUG )
88 //printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
92 buf[2] = (char)(flash_addr >> 8);
93 buf[3] = (char)(flash_addr & 0xFF);
96 // Fill firmware source data
97 //if ( (sec == 1 && cod == 4) || (sec == 1 && cod == 5) ) {
98 //if ( flash_addr == (CT36X_CHIP_FLASH_SECTOR_SIZE + 32) ||
99 //flash_addr == (CT36X_CHIP_FLASH_SECTOR_SIZE + 40) ) {
100 if ( flash_addr == (160) || flash_addr == (168) ) {
101 buf[6] = ~binary_data[flash_addr + 0];
102 buf[7] = ~binary_data[flash_addr + 1];
103 buf[8] = ~binary_data[flash_addr + 2];
104 buf[9] = ~binary_data[flash_addr + 3];
105 buf[10] = ~binary_data[flash_addr + 4];
106 buf[11] = ~binary_data[flash_addr + 5];
107 buf[12] = ~binary_data[flash_addr + 6];
108 buf[13] = ~binary_data[flash_addr + 7];
110 buf[6] = binary_data[flash_addr + 0];
111 buf[7] = binary_data[flash_addr + 1];
112 buf[8] = binary_data[flash_addr + 2];
113 buf[9] = binary_data[flash_addr + 3];
114 buf[10] = binary_data[flash_addr + 4];
115 buf[11] = binary_data[flash_addr + 5];
116 buf[12] = binary_data[flash_addr + 6];
117 buf[13] = binary_data[flash_addr + 7];
120 /* Calculate a checksum by Host controller.
121 ** Checksum = ~(FLASH_ADRH+FLASH_ADRL+LENGTH+
122 ** Binary_Data1+Binary_Data2+Binary_Data3+Binary_Data4+
123 ** Binary_Data5+Binary_Data6+Binary_Data7+Binary_Data8) + 1
125 cod_chksum = ~(buf[2]+buf[3]+buf[4]+
126 buf[6]+buf[7]+buf[8]+buf[9]+
127 buf[10]+buf[11]+buf[12]+buf[13]) + 1;
133 static int ct36x_chip_write_firmware(struct i2c_client *client, unsigned char *buf)
137 unsigned char cod_chksum;
138 unsigned int fin_chksum;
139 unsigned int flash_addr;
141 if ( CT36X_TS_CHIP_DEBUG )
142 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
144 // Code checksum, final checksum
145 cod_chksum = 0x00; fin_chksum = 0x00;
147 // Flash write command
151 // 256 sectors, 128 bytes per sectors
152 for ( sec = 0; sec < CT36X_CHIP_FLASH_SECTOR_NUM; sec++ ) {
153 flash_addr = sec * CT36X_CHIP_FLASH_SECTOR_SIZE;
154 // 16 segments, 8 bytes per segment
155 for ( cod = 0; cod < (CT36X_CHIP_FLASH_SECTOR_SIZE/CT36X_CHIP_FLASH_SOURCE_SIZE); cod++ ) {
157 cod_chksum = ct36x_chip_set_code(flash_addr, buf);
158 fin_chksum += cod_chksum;
160 // Write firmware source data
161 ct36x_ts_reg_write(client, 0x7F, buf, 14);
166 // Increase flash address 8bytes for each write command
167 flash_addr += CT36X_CHIP_FLASH_SOURCE_SIZE;
176 static int ct36x_chip_read_infoblk(struct i2c_client *client, unsigned char *buf)
178 if ( CT36X_TS_CHIP_DEBUG )
179 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
184 static int ct36x_chip_erase_infoblk(struct i2c_client *client, unsigned char *buf)
188 if ( CT36X_TS_CHIP_DEBUG )
189 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
191 // info block erase command
195 ct36x_ts_reg_write(client, 0x7F, buf, 3);
198 // Reset I2C offset address
199 ct36x_chip_rst_offset(client, buf);
201 // Read I2C bus status
202 ret = ct36x_chip_get_busstatus(client, buf);
204 printk("trim data erase error!!! \n");
211 static int ct36x_chip_write_infoblk(struct i2c_client *client, unsigned char *buf)
215 unsigned int flash_addr;
217 if ( CT36X_TS_CHIP_DEBUG )
218 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
222 // write info block 0
226 for ( cod = 0; cod < 16; cod++ ) {
229 buf[2] = (char)(flash_addr >> 8);
230 buf[3] = (char)(flash_addr & 0xFF);
232 if ( flash_addr == 0x0000 )
245 buf[5] = (~(buf[2]+buf[3]+buf[4]+buf[6]+buf[7]+buf[8]+buf[9]+buf[10]+buf[11]+buf[12]+buf[13]))+1;
247 ct36x_ts_reg_write(client, 0x7F, buf, 14);
256 int ct36x_chip_get_binchksum(unsigned char *buf)
259 unsigned char cod_chksum;
260 unsigned int fin_chksum = 0;
261 unsigned int flash_addr;
263 if ( CT36X_TS_CHIP_DEBUG )
264 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
266 // 256 sectors, 128 bytes per sectors
267 for ( sec = 0; sec < CT36X_CHIP_FLASH_SECTOR_NUM; sec++ ) {
268 flash_addr = sec * CT36X_CHIP_FLASH_SECTOR_SIZE;
269 // 16 segments, 8 bytes per segment
270 for ( cod = 0; cod < (CT36X_CHIP_FLASH_SECTOR_SIZE/CT36X_CHIP_FLASH_SOURCE_SIZE); cod++ ) {
272 cod_chksum = ct36x_chip_set_code(flash_addr, buf);
273 fin_chksum += cod_chksum;
275 // Increase flash address 8bytes for each write command
276 flash_addr += CT36X_CHIP_FLASH_SOURCE_SIZE;
280 return (unsigned short)fin_chksum;
283 int ct36x_chip_get_fwchksum(struct i2c_client *client, unsigned char *buf)
287 if ( CT36X_TS_CHIP_DEBUG )
288 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
293 ct36x_ts_reg_write(client, client->addr, buf, 3);
298 ct36x_ts_reg_write(client, client->addr, buf, 2);
304 ct36x_ts_reg_write(client, client->addr, buf, 3);
307 ct36x_chip_rst_offset(client, buf);
309 ct36x_ts_reg_read(client, client->addr, buf, 3);
312 fwchksum = ((buf[0]<<8) | buf[1]);
317 int ct36x_chip_get_ver(struct i2c_client *client, unsigned char *buf)
319 if ( CT36X_TS_CHIP_DEBUG )
320 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
322 // Read version command
327 ct36x_ts_reg_write(client, client->addr, buf, 3);
331 ct36x_ts_reg_write(client, client->addr, buf, 1);
335 ct36x_ts_reg_read(client, client->addr, buf, 1);
341 int ct36x_chip_get_vendor(struct i2c_client *client, unsigned char *buf)
343 if ( CT36X_TS_CHIP_DEBUG )
344 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
349 void ct36x_chip_go_sleep(struct i2c_client *client, unsigned char *buf)
351 if ( CT36X_TS_CHIP_DEBUG )
352 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
357 ct36x_ts_reg_write(client, client->addr, buf, 3);
362 ct36x_ts_reg_write(client, client->addr, buf, 2);
368 int ct36x_chip_go_bootloader(struct i2c_client *client, unsigned char *buf)
372 if ( CT36X_TS_CHIP_DEBUG )
373 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
376 ct36x_chip_set_idle(client, buf);
378 // Reset I2C offset address
379 ct36x_chip_rst_offset(client, buf);
381 // Get I2C bus status
382 ret = ct36x_chip_get_busstatus(client, buf);
384 printk("I2C bus status: 0x%x.\n", ret);
389 ct36x_chip_read_infoblk(client, buf);
390 ct36x_chip_erase_infoblk(client, buf);
391 ct36x_chip_write_infoblk(client, buf);
394 //ret = ct36x_chip_erase_flash(client, buf);
396 // printk("Erase flash failed.\n");
401 //ct36x_chip_write_firmware(client, buf);
406 void ct36x_chip_set_adapter_on(struct i2c_client *client, unsigned char *buf)
408 if ( CT36X_TS_CHIP_DEBUG )
409 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
414 ct36x_ts_reg_write(client, client->addr, buf, 3);
419 ct36x_ts_reg_write(client, client->addr, buf, 2);
423 void ct36x_chip_set_adapter_off(struct i2c_client *client, unsigned char *buf)
425 if ( CT36X_TS_CHIP_DEBUG )
426 printk(">>>>> %s() called <<<<< \n", __FUNCTION__);
431 ct36x_ts_reg_write(client, client->addr, buf, 3);
436 ct36x_ts_reg_write(client, client->addr, buf, 2);