Merge remote-tracking branch 'stable/linux-3.0.y' into develop-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / video / display / transmitter / tc358768.c
1 /*
2  * Copyright (C) 2012 ROCKCHIP, Inc.
3  *
4  * author: hhb@rock-chips.com
5  * create date: 2012-10-26
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15  
16 #include "tc358768.h"
17
18 #if 0
19 #define dsi_debug   printk
20 #else
21 #define dsi_debug(fmt...)   do { } while (0)
22 #endif
23
24 #ifdef CONFIG_TC358768_I2C
25 struct tc358768_t *tc358768 = NULL;
26 struct i2c_client *tc358768_client = NULL;
27 struct mipi_dsi_t *dsi;
28
29
30 u32 i2c_write_32bits(u32 value) 
31 {
32         struct i2c_msg msgs;
33         int ret = -1;
34         char buf[4];
35         buf[0] = value>>24;
36         buf[1] = value>>16;
37         buf[2] = value>>8;
38         buf[3] = value;
39         
40         msgs.addr = tc358768_client->addr;
41         msgs.flags = tc358768_client->flags;
42         msgs.len = 4;
43         msgs.buf = buf;
44         msgs.scl_rate = CONFIG_TC358768_I2C_CLK;
45         msgs.udelay = tc358768_client->udelay;
46
47         ret = i2c_transfer(tc358768_client->adapter, &msgs, 1);
48         if(ret < 0)
49                 printk("%s:i2c_transfer fail =%d\n",__func__, ret);
50         return ret;
51 }
52
53 u32 i2c_read_32bits(u32 value) 
54 {
55         struct i2c_msg msgs[2];
56         int ret = -1;
57         char buf[4];
58         buf[0] = value>>8;
59         buf[1] = value;
60         
61         msgs[0].addr = tc358768_client->addr;
62         msgs[0].flags = tc358768_client->flags;
63         msgs[0].len = 2;
64         msgs[0].buf = buf;
65         msgs[0].scl_rate = CONFIG_TC358768_I2C_CLK;
66         msgs[0].udelay = tc358768_client->udelay;
67
68         msgs[1].addr = tc358768_client->addr;
69         msgs[1].flags = tc358768_client->flags | I2C_M_RD;
70         msgs[1].len = 2;
71         msgs[1].buf = buf;
72         msgs[1].scl_rate = CONFIG_TC358768_I2C_CLK;
73         msgs[1].udelay = tc358768_client->udelay;
74
75         ret = i2c_transfer(tc358768_client->adapter, msgs, 2);
76         if(ret < 0)
77                 printk("%s:i2c_transfer fail =%d\n",__func__, ret);
78         else
79                 ret = (buf[0]<<8) | buf[1];     
80         
81         return ret;
82 }
83
84
85 int tc358768_gpio_init(void *data) {
86         int ret = 0;
87         struct reset_t *reset = &tc358768->reset;
88         struct power_t *vdd = &tc358768->vddc;
89         if(reset->reset_pin > INVALID_GPIO) {
90                 ret = gpio_request(reset->reset_pin, "tc358768_reset");
91                 if (ret != 0) {
92                         gpio_free(reset->reset_pin);
93                         printk("%s: request TC358768_RST_PIN error\n", __func__);
94                 } else {
95                         if(reset->mux_name)
96                                 rk30_mux_api_set(reset->mux_name, reset->mux_mode);
97                         gpio_direction_output(reset->reset_pin, reset->effect_value);
98                 }
99         }
100         
101         if(vdd->enable_pin > INVALID_GPIO) {
102                 ret = gpio_request(vdd->enable_pin, "tc358768_vddc");
103                 if (ret != 0) {
104                         gpio_free(vdd->enable_pin);
105                         printk("%s: request TC358768_vddc_PIN error\n", __func__);
106                 } else {
107                         if(vdd->mux_name)
108                                 rk30_mux_api_set(vdd->mux_name, vdd->mux_mode); 
109                         gpio_direction_output(vdd->enable_pin, !vdd->effect_value);
110                 }
111         }
112         
113         vdd = &tc358768->vddio;
114         if(vdd->enable_pin > INVALID_GPIO) {
115                 ret = gpio_request(vdd->enable_pin, "tc358768_vddio");
116                 if (ret != 0) {
117                         gpio_free(vdd->enable_pin);
118                         printk("%s: request TC358768_vddio_PIN error\n", __func__);
119                 } else {
120                         if(vdd->mux_name)
121                                 rk30_mux_api_set(vdd->mux_name, vdd->mux_mode); 
122                         gpio_direction_output(vdd->enable_pin, !vdd->effect_value);     
123                 }
124         }
125         
126         vdd = &tc358768->vdd_mipi;
127         if(vdd->enable_pin > INVALID_GPIO) {
128                 ret = gpio_request(vdd->enable_pin, "tc358768_vdd_mipi");
129                 if (ret != 0) {
130                         gpio_free(vdd->enable_pin);
131                         printk("%s: request TC358768_vdd_mipi_PIN error\n", __func__);
132                 } else {
133                         if(vdd->mux_name)
134                                 rk30_mux_api_set(vdd->mux_name, vdd->mux_mode); 
135                         gpio_direction_output(vdd->enable_pin, !vdd->effect_value);     
136                 }
137         }
138         return 0;
139
140 }
141
142 int tc358768_gpio_deinit(void *data) {
143         struct reset_t *reset = &tc358768->reset;
144         struct power_t *vdd = &tc358768->vddc;
145         gpio_free(reset->reset_pin);
146         
147         vdd = &tc358768->vddio;
148         gpio_free(vdd->enable_pin);
149         
150         vdd = &tc358768->vdd_mipi;
151         gpio_free(vdd->enable_pin);
152         return 0;
153 }
154
155 int tc358768_reset(void *data) {
156         int ret = 0;
157         struct reset_t *reset = &tc358768->reset;
158         if(reset->reset_pin <= INVALID_GPIO)
159                 return -1;
160         gpio_set_value(reset->reset_pin, reset->effect_value);
161         if(reset->time_before_reset <= 0)
162                 msleep(1);
163         else
164                 msleep(reset->time_before_reset);
165         
166         gpio_set_value(reset->reset_pin, !reset->effect_value);
167         if(reset->time_after_reset <= 0)
168                 msleep(5);
169         else
170                 msleep(reset->time_after_reset);
171         return ret;     
172 }
173
174 int tc358768_vdd_enable(void *data) {
175         int ret = 0;
176         struct power_t *vdd = (struct power_t *)data;
177         if(vdd->enable_pin > INVALID_GPIO) {
178                 gpio_set_value(vdd->enable_pin, vdd->effect_value);
179         } else {
180                 //for other control
181         }
182         return ret;
183 }
184
185 int tc358768_vdd_disable(void *data) {
186         int ret = 0;
187         struct power_t *vdd = (struct power_t *)data;
188         
189         if(vdd->enable_pin > INVALID_GPIO) {
190                 gpio_set_value(vdd->enable_pin, !vdd->effect_value);
191         } else {
192                 //for other control
193         }
194         return ret;
195 }
196
197
198 int tc358768_power_up(void *data) {
199         int ret = 0;
200         struct tc358768_t *tc = (struct tc358768_t *)tc358768;
201         
202         tc->vddc.enable(&tc->vddc);
203         tc->vdd_mipi.enable(&tc->vdd_mipi);
204         tc->vddio.enable(&tc->vddio);
205         tc->reset.do_reset(&tc->reset);
206         
207         return ret;
208 }
209
210 int tc358768_power_down(void *data) {
211         int ret = 0;
212         struct tc358768_t *tc = (struct tc358768_t *)tc358768;
213         
214         tc->vddio.disable(&tc->vddio);
215         tc->vdd_mipi.disable(&tc->vdd_mipi);
216         tc->vddc.disable(&tc->vddc);
217         
218         return ret;
219 }
220
221 static int tc358768_probe(struct i2c_client *client,
222                          const struct i2c_device_id *did) 
223 {
224     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
225     
226     int ret = 0;
227
228     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
229         dev_warn(&adapter->dev,
230                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
231         return -EIO;
232     }
233     tc358768 = (struct tc358768_t *)client->dev.platform_data;
234     if(!tc358768) {
235         ret = -1;
236         printk("%s:%d tc358768 is null\n", __func__, __LINE__);
237         return ret;
238     }   
239     
240     tc358768_client = client;
241     if(!tc358768_client) {
242         ret = -1;
243         printk("%s:%d tc358768_client is null\n", __func__, __LINE__);
244         return ret;
245     }
246     
247     if(!tc358768->gpio_init)
248         tc358768->gpio_init = tc358768_gpio_init;
249         
250     if(!tc358768->gpio_deinit)
251         tc358768->gpio_deinit = tc358768_gpio_deinit;    
252     
253     if(!tc358768->power_up)
254         tc358768->power_up = tc358768_power_up;  
255     if(!tc358768->power_down)
256         tc358768->power_down = tc358768_power_down;     
257     
258     if(!tc358768->reset.do_reset)
259         tc358768->reset.do_reset = tc358768_reset;
260     
261     if(!tc358768->vddc.enable)
262         tc358768->vddc.enable = tc358768_vdd_enable;    
263     if(!tc358768->vddc.disable)
264         tc358768->vddc.disable = tc358768_vdd_disable;
265     
266     if(!tc358768->vddio.enable)
267         tc358768->vddio.enable = tc358768_vdd_enable;    
268     if(!tc358768->vddio.disable)
269         tc358768->vddio.disable = tc358768_vdd_disable;
270     
271     if(!tc358768->vdd_mipi.enable)
272         tc358768->vdd_mipi.enable = tc358768_vdd_enable;    
273     if(!tc358768->vdd_mipi.disable)
274         tc358768->vdd_mipi.disable = tc358768_vdd_disable;
275         
276     dsi->chip = tc358768;       
277         
278     return ret;
279 }
280 static int tc358768_remove(struct i2c_client *client)
281 {
282     tc358768_client = NULL;
283     tc358768 = NULL;
284     return 0;
285 }
286
287 static const struct i2c_device_id tc358768_id[] = {
288         {"tc358768", 0 },
289         { }
290 };
291 MODULE_DEVICE_TABLE(i2c, tc358768_id);
292
293 static struct i2c_driver tc358768_driver = {
294         .probe          = tc358768_probe,
295         .remove         = tc358768_remove,
296         .id_table       = tc358768_id,
297         .driver = {
298                 .name   = "tc358768",
299         },
300 };
301 #else
302
303 u32 spi_read_32bits(u32 addr)
304 {
305         unsigned int i = 32;
306         //a frame starts
307         CS_CLR();
308         CLK_SET();
309
310         addr <<= 16;
311         addr &= 0xfffe0000;
312         addr |= 0x00010000;
313
314         udelay(2);
315         while(i--) {
316                 CLK_CLR();
317                 if(addr & 0x80000000)
318                         TXD_SET();
319         else
320                         TXD_CLR();
321                 addr <<= 1;
322                 udelay(2);
323                 CLK_SET();
324                 udelay(2);
325         }
326         //a frame ends
327     CS_SET();
328
329
330     udelay(2);
331     CS_CLR();
332     addr = 0xfffe0000;
333     i = 16;
334         while(i--) {
335                 CLK_CLR();
336                 if(addr & 0x80000000)
337                         TXD_SET();
338         else
339                         TXD_CLR();
340                 addr <<= 1;
341                 udelay(2);
342                 CLK_SET();
343                 udelay(2);
344         }
345
346         TXD_SET();
347
348         addr = 0;
349     i = 16;
350         while(i--) {
351                 CLK_CLR();
352                 udelay(1);
353                 CLK_SET();
354                 udelay(1);
355                 if (gpio_get_value(gLcd_info->rxd_pin) == 1)
356                         addr |= 1 << i;
357                 udelay(1);
358         }
359     CS_SET();
360
361     return addr;
362 }
363
364
365 //32 bits per frame
366 u32 spi_write_32bits(u32 value)
367 {
368         int i = 32;
369
370     //a frame starts
371         CS_CLR();
372         CLK_SET();
373
374         while(i--) {
375                 CLK_CLR();
376                 if(value & 0x80000000)
377                         TXD_SET();
378         else
379                         TXD_CLR();
380                 value <<= 1;
381                 CLK_SET();
382         }
383         //a frame ends
384     CS_SET();
385
386     return 0;
387 }
388
389 #endif
390
391 u32 tc358768_wr_reg_32bits(u32 data) {
392 #ifdef CONFIG_TC358768_I2C
393         i2c_write_32bits(data);
394 #else
395         spi_write_32bits(data);
396 #endif
397         return 0;
398 }
399
400
401 u32 tc358768_wr_reg_32bits_delay(u32 delay, u32 data) {
402         //wait a minute  according to the source format
403     if(delay < 20000)
404         udelay(delay);
405     else {
406         mdelay(delay/1000);
407     }
408
409 #ifdef CONFIG_TC358768_I2C
410         i2c_write_32bits(data);
411 #else
412         spi_write_32bits(data);
413 #endif
414         return 0;
415 }
416
417
418
419 u32 tc358768_rd_reg_32bits(u32 addr) {
420 #ifdef CONFIG_TC358768_I2C
421         return i2c_read_32bits(addr);
422 #else
423         return spi_read_32bits(addr);
424 #endif
425 }
426
427
428
429 void tc_print(u32 addr) {
430         dsi_debug("+++++++++++addr->%04x: %04x\n", addr, tc358768_rd_reg_32bits(addr));
431 }
432
433 #define tc358768_wr_regs_32bits(reg_array)  _tc358768_wr_regs_32bits(reg_array, ARRAY_SIZE(reg_array))
434 int _tc358768_wr_regs_32bits(unsigned int reg_array[], int n) {
435
436         int i = 0;
437         dsi_debug("%s:%d\n", __func__, n);
438         for(i = 0; i < n; i++) {
439                 if(reg_array[i] < 0x00020000) {
440                     if(reg_array[i] < 20000)
441                         udelay(reg_array[i]);
442                     else {
443                         mdelay(reg_array[i]/1000);
444                     }
445                 } else {
446                         tc358768_wr_reg_32bits(reg_array[i]);
447                 }
448         }
449         return 0;
450 }
451
452 int tc358768_command_tx_less8bytes(unsigned char type, unsigned char *regs, int n) {
453         int i = 0;
454         unsigned int command[] = {
455                         0x06020000,
456                         0x06040000,
457                         0x06100000,
458                         0x06120000,
459                         0x06140000,
460                         0x06160000,
461         };
462
463         if(n <= 2)
464                 command[0] |= 0x1000;   //short packet
465         else {
466                 command[0] |= 0x4000;   //long packet
467                 command[1] |= n;                //word count byte
468         }
469         command[0] |= type;         //data type
470
471         //dsi_debug("*cmd:\n");
472         //dsi_debug("0x%08x\n", command[0]);
473         //dsi_debug("0x%08x\n", command[1]);
474
475         for(i = 0; i < (n + 1)/2; i++) {
476                 command[i+2] |= regs[i*2];
477                 if((i*2 + 1) < n)
478                         command[i+2] |= regs[i*2 + 1] << 8;
479                 dsi_debug("0x%08x\n", command[i+2]);
480         }
481
482         _tc358768_wr_regs_32bits(command, (n + 1)/2 + 2);
483         tc358768_wr_reg_32bits(0x06000001);   //Packet Transfer
484         //wait until packet is out
485         i = 100;
486         while(tc358768_rd_reg_32bits(0x0600) & 0x01) {
487                 if(i-- == 0)
488                         break;
489                 tc_print(0x0600);
490         }
491         //udelay(50);
492         return 0;
493 }
494
495 int tc358768_command_tx_more8bytes_hs(unsigned char type, unsigned char regs[], int n) {
496
497         int i = 0;
498         unsigned int dbg_data = 0x00E80000, temp = 0;
499         unsigned int command[] = {
500                         0x05000080,    //HS data 4 lane, EOT is added
501                         0x0502A300,
502                         0x00080001,
503                         0x00500000,    //Data ID setting
504                         0x00220000,    //Transmission byte count= byte
505                         0x00E08000,        //Enable I2C/SPI write to VB
506                         0x00E20048,    //Total word count = 0x48 (max 0xFFF). This value should be adjusted considering trade off between transmission time and transmission start/stop time delay
507                         0x00E4007F,    //Vertical blank line = 0x7F
508         };
509
510
511         command[3] |= type;        //data type
512         command[4] |= n & 0xffff;           //Transmission byte count
513
514         tc358768_wr_regs_32bits(command);
515
516         for(i = 0; i < (n + 1)/2; i++) {
517                 temp = dbg_data | regs[i*2];
518                 if((i*2 + 1) < n)
519                         temp |= (regs[i*2 + 1] << 8);
520                 //dsi_debug("0x%08x\n", temp);
521                 tc358768_wr_reg_32bits(temp);
522         }
523         if((n % 4 == 1) ||  (n % 4 == 2))     //4 bytes align
524                 tc358768_wr_reg_32bits(dbg_data);
525
526         tc358768_wr_reg_32bits(0x00E0C000);     //Start command transmisison
527         tc358768_wr_reg_32bits(0x00E00000);      //Stop command transmission. This setting should be done just after above setting to prevent multiple output
528         udelay(200);
529         //Re-Initialize
530         //tc358768_wr_regs_32bits(re_initialize);
531         return 0;
532 }
533
534 //low power mode only for tc358768a
535 int tc358768_command_tx_more8bytes_lp(unsigned char type, unsigned char regs[], int n) {
536
537         int i = 0;
538         unsigned int dbg_data = 0x00E80000, temp = 0;
539         unsigned int command[] = {
540                         0x00080001,
541                         0x00500000,    //Data ID setting
542                         0x00220000,    //Transmission byte count= byte
543                         0x00E08000,        //Enable I2C/SPI write to VB
544         };
545
546         command[1] |= type;        //data type
547         command[2] |= n & 0xffff;           //Transmission byte count
548
549         tc358768_wr_regs_32bits(command);
550
551         for(i = 0; i < (n + 1)/2; i++) {
552                 temp = dbg_data | regs[i*2];
553                 if((i*2 + 1) < n)
554                         temp |= (regs[i*2 + 1] << 8);
555                 //dsi_debug("0x%08x\n", temp);
556                 tc358768_wr_reg_32bits(temp);
557
558         }
559         if((n % 4 == 1) ||  (n % 4 == 2))     //4 bytes align
560                 tc358768_wr_reg_32bits(dbg_data);
561
562         tc358768_wr_reg_32bits(0x00E0E000);     //Start command transmisison
563         udelay(1000);
564         tc358768_wr_reg_32bits(0x00E02000);      //Keep Mask High to prevent short packets send out
565         tc358768_wr_reg_32bits(0x00E00000);      //Stop command transmission. This setting should be done just after above setting to prevent multiple output
566         udelay(10);
567         return 0;
568 }
569
570 int _tc358768_send_packet(unsigned char type, unsigned char regs[], int n) {
571
572         if(n <= 8) {
573                 tc358768_command_tx_less8bytes(type, regs, n);
574         } else {
575                 //tc358768_command_tx_more8bytes_hs(type, regs, n);
576                 tc358768_command_tx_more8bytes_lp(type, regs, n);
577         }
578         return 0;
579 }
580
581 int tc358768_send_packet(unsigned char type, unsigned char regs[], int n) {
582         return _tc358768_send_packet(type, regs, n);
583 }
584
585
586 /*
587 The DCS is separated into two functional areas: the User Command Set and the Manufacturer Command
588 Set. Each command is an eight-bit code with 00h to AFh assigned to the User Command Set and all other
589 codes assigned to the Manufacturer Command Set.
590 */
591 int _mipi_dsi_send_dcs_packet(unsigned char regs[], int n) {
592
593         unsigned char type = 0;
594         if(n == 1) {
595                 type = DTYPE_DCS_SWRITE_0P;
596         } else if (n == 2) {
597                 type = DTYPE_DCS_SWRITE_1P;
598         } else if (n > 2) {
599                 type = DTYPE_DCS_LWRITE;
600         } 
601         _tc358768_send_packet(type, regs, n);
602         return 0;
603 }
604
605 int mipi_dsi_send_dcs_packet(unsigned char regs[], int n) {
606         return _mipi_dsi_send_dcs_packet(regs, n);
607 }
608
609
610 int _tc358768_rd_lcd_regs(unsigned char type, char comd, int size, unsigned char* buf) {
611
612         unsigned char regs[8];
613         u32 count = 0, data30, data32;
614         regs[0] = size;
615         regs[1] = 0;
616         tc358768_command_tx_less8bytes(0x37, regs, 2);
617         tc358768_wr_reg_32bits(0x05040010);
618         tc358768_wr_reg_32bits(0x05060000);
619         regs[0] = comd;
620         tc358768_command_tx_less8bytes(type, regs, 1);
621
622         while (!(tc358768_rd_reg_32bits(0x0410) & 0x20)){
623                 printk("error 0x0410:%04x\n", tc358768_rd_reg_32bits(0x0410));
624                 msleep(1);
625                 if(count++ > 10) {
626                         break;
627                 }
628         }
629         
630         data30 = tc358768_rd_reg_32bits(0x0430);          //data id , word count[0:7]
631         //printk("0x0430:%04x\n", data30);
632         data32 = tc358768_rd_reg_32bits(0x0432);          //word count[8:15]  ECC
633         //printk("0x0432:%04x\n", data32);
634         
635         while(size > 0) {
636                 data30 = tc358768_rd_reg_32bits(0x0430);          
637                 //printk("0x0430:%04x\n", data30);
638                 data32 = tc358768_rd_reg_32bits(0x0432);          
639                 //printk("0x0432:%04x\n", data32);
640         
641                 if(size-- > 0)
642                         *buf++ = (u8)data30;
643                 else
644                         break;
645                 if(size-- > 0)
646                         *buf++ = (u8)(data30 >> 8);
647                 else
648                         break;
649                 if(size-- > 0) {
650                         *buf++ = (u8)data32;
651                         if(size-- > 0)
652                                 *buf++ = (u8)(data32 >> 8);
653                 }
654         }       
655         
656         data30 = tc358768_rd_reg_32bits(0x0430);          
657         //printk("0x0430:%04x\n", data30);
658         data32 = tc358768_rd_reg_32bits(0x0432);          
659         //printk("0x0432:%04x\n", data32);
660         return 0;
661 }
662
663 int mipi_dsi_read_dcs_packet(unsigned char *data, int n) {
664         //DCS READ 
665         _tc358768_rd_lcd_regs(0x06, *data, n, data);
666         return 0;
667 }
668
669
670 int tc358768_init(struct mipi_dsi_t *pram) {
671         int ret = 0;
672         dsi = pram;
673         if(!dsi)
674                 return -1;
675         dsi->id = 0x4401;
676         dsi->dsi_init = _tc358768_wr_regs_32bits;
677         dsi->dsi_hs_start = _tc358768_wr_regs_32bits;
678         dsi->dsi_send_dcs_packet = mipi_dsi_send_dcs_packet;
679         dsi->dsi_read_dcs_packet = mipi_dsi_read_dcs_packet;
680 #ifdef CONFIG_TC358768_I2C    
681     i2c_add_driver(&tc358768_driver);
682 #endif    
683         tc358768_gpio_init(NULL);
684         return 0;
685 exit_init:
686         //tc358768_power_down(NULL);
687         tc358768_gpio_deinit(NULL);
688         return -1;
689 }