2 * Copyright (C) 2012 ROCKCHIP, Inc.
3 * drivers/video/display/transmitter/tc358768.c
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.
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.
16 #include <linux/delay.h>
17 #include <linux/rk_fb.h>
18 #include <mach/gpio.h>
19 #include <mach/iomux.h>
20 #include <mach/board.h>
21 #include <linux/rk_screen.h>
22 #include <linux/ktime.h>
25 #define CONFIG_TC358768_I2C 1
26 #define CONFIG_TC358768_I2C_CLK 400*1000
30 #define dsi_debug printk
32 #define dsi_debug(fmt...) do { } while (0)
35 #ifdef CONFIG_TC358768_I2C
36 static struct tc358768_t *tc358768 = NULL;
37 static struct i2c_client *tc358768_client = NULL;
38 static struct mipi_dsi_ops tc358768_ops;
41 u32 i2c_write_32bits(u32 value)
51 msgs.addr = tc358768_client->addr;
52 msgs.flags = tc358768_client->flags;
55 msgs.scl_rate = CONFIG_TC358768_I2C_CLK;
56 msgs.udelay = tc358768_client->udelay;
58 ret = i2c_transfer(tc358768_client->adapter, &msgs, 1);
60 printk("%s:i2c_transfer fail =%d\n",__func__, ret);
64 u32 i2c_read_32bits(u32 value)
66 struct i2c_msg msgs[2];
72 msgs[0].addr = tc358768_client->addr;
73 msgs[0].flags = tc358768_client->flags;
76 msgs[0].scl_rate = CONFIG_TC358768_I2C_CLK;
77 msgs[0].udelay = tc358768_client->udelay;
79 msgs[1].addr = tc358768_client->addr;
80 msgs[1].flags = tc358768_client->flags | I2C_M_RD;
83 msgs[1].scl_rate = CONFIG_TC358768_I2C_CLK;
84 msgs[1].udelay = tc358768_client->udelay;
86 ret = i2c_transfer(tc358768_client->adapter, msgs, 2);
88 printk("%s:i2c_transfer fail =%d\n",__func__, ret);
90 ret = (buf[0]<<8) | buf[1];
96 int tc358768_gpio_init(void *data) {
98 struct reset_t *reset = &tc358768->reset;
99 struct power_t *vdd = &tc358768->vddc;
100 if(reset->reset_pin > INVALID_GPIO) {
101 ret = gpio_request(reset->reset_pin, "tc358768_reset");
103 //gpio_free(reset->reset_pin);
104 printk("%s: request TC358768_RST_PIN error\n", __func__);
108 rk30_mux_api_set(reset->mux_name, reset->mux_mode);
110 gpio_direction_output(reset->reset_pin, !reset->effect_value);
114 if(vdd->enable_pin > INVALID_GPIO) {
115 ret = gpio_request(vdd->enable_pin, "tc358768_vddc");
117 //gpio_free(vdd->enable_pin);
118 printk("%s: request TC358768_vddc_PIN error\n", __func__);
122 rk30_mux_api_set(vdd->mux_name, vdd->mux_mode);
124 gpio_direction_output(vdd->enable_pin, !vdd->effect_value);
128 vdd = &tc358768->vddio;
129 if(vdd->enable_pin > INVALID_GPIO) {
130 ret = gpio_request(vdd->enable_pin, "tc358768_vddio");
132 //gpio_free(vdd->enable_pin);
133 printk("%s: request TC358768_vddio_PIN error\n", __func__);
137 rk30_mux_api_set(vdd->mux_name, vdd->mux_mode);
139 gpio_direction_output(vdd->enable_pin, !vdd->effect_value);
143 vdd = &tc358768->vdd_mipi;
144 if(vdd->enable_pin > INVALID_GPIO) {
145 ret = gpio_request(vdd->enable_pin, "tc358768_vdd_mipi");
147 //gpio_free(vdd->enable_pin);
148 printk("%s: request TC358768_vdd_mipi_PIN error\n", __func__);
152 rk30_mux_api_set(vdd->mux_name, vdd->mux_mode);
154 gpio_direction_output(vdd->enable_pin, !vdd->effect_value);
161 int tc358768_gpio_deinit(void *data) {
162 struct reset_t *reset = &tc358768->reset;
163 struct power_t *vdd = &tc358768->vddc;
164 gpio_direction_input(reset->reset_pin);
165 gpio_free(reset->reset_pin);
167 gpio_direction_input(vdd->enable_pin);
168 gpio_free(vdd->enable_pin);
170 vdd = &tc358768->vddio;
171 gpio_direction_input(vdd->enable_pin);
172 gpio_free(vdd->enable_pin);
174 vdd = &tc358768->vdd_mipi;
175 gpio_direction_input(vdd->enable_pin);
176 gpio_free(vdd->enable_pin);
180 int tc358768_reset(void *data) {
182 struct reset_t *reset = &tc358768->reset;
183 if(reset->reset_pin <= INVALID_GPIO)
185 gpio_set_value(reset->reset_pin, reset->effect_value);
186 if(reset->time_before_reset <= 0)
189 msleep(reset->time_before_reset);
191 gpio_set_value(reset->reset_pin, !reset->effect_value);
192 if(reset->time_after_reset <= 0)
195 msleep(reset->time_after_reset);
199 int tc358768_vdd_enable(void *data) {
201 struct power_t *vdd = (struct power_t *)data;
202 if(vdd->enable_pin > INVALID_GPIO) {
203 gpio_set_value(vdd->enable_pin, vdd->effect_value);
210 int tc358768_vdd_disable(void *data) {
212 struct power_t *vdd = (struct power_t *)data;
214 if(vdd->enable_pin > INVALID_GPIO) {
215 gpio_set_value(vdd->enable_pin, !vdd->effect_value);
223 int tc358768_power_up(void) {
226 struct tc358768_t *tc = (struct tc358768_t *)tc358768;
228 tc->vddc.enable(&tc->vddc);
229 tc->vdd_mipi.enable(&tc->vdd_mipi);
230 tc->vddio.enable(&tc->vddio);
231 tc->reset.do_reset(&tc->reset);
236 int tc358768_power_down(void) {
239 struct tc358768_t *tc = (struct tc358768_t *)tc358768;
241 tc->vddio.disable(&tc->vddio);
242 tc->vdd_mipi.disable(&tc->vdd_mipi);
243 tc->vddc.disable(&tc->vddc);
248 static int tc358768_probe(struct i2c_client *client,
249 const struct i2c_device_id *did)
251 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
254 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
255 dev_warn(&adapter->dev,
256 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
260 tc358768 = (struct tc358768_t *)client->dev.platform_data;
263 printk("%s:%d tc358768 is null\n", __func__, __LINE__);
267 tc358768_client = client;
268 if(!tc358768_client) {
270 printk("%s:%d tc358768_client is null\n", __func__, __LINE__);
274 if(!tc358768->gpio_init)
275 tc358768->gpio_init = tc358768_gpio_init;
277 if(!tc358768->gpio_deinit)
278 tc358768->gpio_deinit = tc358768_gpio_deinit;
280 if(!tc358768->power_up)
281 tc358768->power_up = tc358768_power_up;
282 if(!tc358768->power_down)
283 tc358768->power_down = tc358768_power_down;
285 if(!tc358768->reset.do_reset)
286 tc358768->reset.do_reset = tc358768_reset;
288 if(!tc358768->vddc.enable)
289 tc358768->vddc.enable = tc358768_vdd_enable;
290 if(!tc358768->vddc.disable)
291 tc358768->vddc.disable = tc358768_vdd_disable;
293 if(!tc358768->vddio.enable)
294 tc358768->vddio.enable = tc358768_vdd_enable;
295 if(!tc358768->vddio.disable)
296 tc358768->vddio.disable = tc358768_vdd_disable;
298 if(!tc358768->vdd_mipi.enable)
299 tc358768->vdd_mipi.enable = tc358768_vdd_enable;
300 if(!tc358768->vdd_mipi.disable)
301 tc358768->vdd_mipi.disable = tc358768_vdd_disable;
303 tc358768_gpio_init(NULL);
307 static int tc358768_remove(struct i2c_client *client)
309 tc358768_gpio_deinit(NULL);
310 tc358768_client = NULL;
315 static const struct i2c_device_id tc358768_id[] = {
319 MODULE_DEVICE_TABLE(i2c, tc358768_id);
321 static struct i2c_driver tc358768_driver = {
322 .probe = tc358768_probe,
323 .remove = tc358768_remove,
324 .id_table = tc358768_id,
331 u32 spi_read_32bits(u32 addr)
345 if(addr & 0x80000000)
364 if(addr & 0x80000000)
383 if (gpio_get_value(gLcd_info->rxd_pin) == 1)
394 u32 spi_write_32bits(u32 value)
404 if(value & 0x80000000)
419 u32 tc358768_wr_reg_32bits(u32 data) {
420 #ifdef CONFIG_TC358768_I2C
421 i2c_write_32bits(data);
423 spi_write_32bits(data);
429 u32 tc358768_wr_reg_32bits_delay(u32 delay, u32 data) {
430 //wait a minute according to the source format
437 #ifdef CONFIG_TC358768_I2C
438 i2c_write_32bits(data);
440 spi_write_32bits(data);
447 u32 tc358768_rd_reg_32bits(u32 addr) {
448 #ifdef CONFIG_TC358768_I2C
449 return i2c_read_32bits(addr);
451 return spi_read_32bits(addr);
457 void tc_print(u32 addr) {
458 dsi_debug("+++++++++++addr->%04x: %04x\n", addr, tc358768_rd_reg_32bits(addr));
461 #define tc358768_wr_regs_32bits(reg_array) _tc358768_wr_regs_32bits(reg_array, ARRAY_SIZE(reg_array))
462 int _tc358768_wr_regs_32bits(unsigned int reg_array[], int n) {
465 dsi_debug("%s:%d\n", __func__, n);
466 for(i = 0; i < n; i++) {
467 if(reg_array[i] < 0x00020000) {
468 if(reg_array[i] < 20000)
469 udelay(reg_array[i]);
471 mdelay(reg_array[i]/1000);
474 tc358768_wr_reg_32bits(reg_array[i]);
480 int tc358768_command_tx_less8bytes(unsigned char type, unsigned char *regs, int n) {
482 unsigned int command[] = {
492 command[0] |= 0x1000; //short packet
494 command[0] |= 0x4000; //long packet
495 command[1] |= n; //word count byte
497 command[0] |= type; //data type
499 //dsi_debug("*cmd:\n");
500 //dsi_debug("0x%08x\n", command[0]);
501 //dsi_debug("0x%08x\n", command[1]);
503 for(i = 0; i < (n + 1)/2; i++) {
504 command[i+2] |= regs[i*2];
506 command[i+2] |= regs[i*2 + 1] << 8;
507 dsi_debug("0x%08x\n", command[i+2]);
510 _tc358768_wr_regs_32bits(command, (n + 1)/2 + 2);
511 tc358768_wr_reg_32bits(0x06000001); //Packet Transfer
512 //wait until packet is out
514 while(tc358768_rd_reg_32bits(0x0600) & 0x01) {
523 int tc358768_command_tx_more8bytes_hs(unsigned char type, unsigned char regs[], int n) {
526 unsigned int dbg_data = 0x00E80000, temp = 0;
527 unsigned int command[] = {
528 0x05000080, //HS data 4 lane, EOT is added
531 0x00500000, //Data ID setting
532 0x00220000, //Transmission byte count= byte
533 0x00E08000, //Enable I2C/SPI write to VB
534 0x00E20048, //Total word count = 0x48 (max 0xFFF). This value should be adjusted considering trade off between transmission time and transmission start/stop time delay
535 0x00E4007F, //Vertical blank line = 0x7F
539 command[3] |= type; //data type
540 command[4] |= n & 0xffff; //Transmission byte count
542 tc358768_wr_regs_32bits(command);
544 for(i = 0; i < (n + 1)/2; i++) {
545 temp = dbg_data | regs[i*2];
547 temp |= (regs[i*2 + 1] << 8);
548 //dsi_debug("0x%08x\n", temp);
549 tc358768_wr_reg_32bits(temp);
551 if((n % 4 == 1) || (n % 4 == 2)) //4 bytes align
552 tc358768_wr_reg_32bits(dbg_data);
554 tc358768_wr_reg_32bits(0x00E0C000); //Start command transmisison
555 tc358768_wr_reg_32bits(0x00E00000); //Stop command transmission. This setting should be done just after above setting to prevent multiple output
558 //tc358768_wr_regs_32bits(re_initialize);
562 //low power mode only for tc358768a
563 int tc358768_command_tx_more8bytes_lp(unsigned char type, unsigned char regs[], int n) {
566 unsigned int dbg_data = 0x00E80000, temp = 0;
567 unsigned int command[] = {
569 0x00500000, //Data ID setting
570 0x00220000, //Transmission byte count= byte
571 0x00E08000, //Enable I2C/SPI write to VB
574 command[1] |= type; //data type
575 command[2] |= n & 0xffff; //Transmission byte count
577 tc358768_wr_regs_32bits(command);
579 for(i = 0; i < (n + 1)/2; i++) {
580 temp = dbg_data | regs[i*2];
582 temp |= (regs[i*2 + 1] << 8);
583 //dsi_debug("0x%08x\n", temp);
584 tc358768_wr_reg_32bits(temp);
587 if((n % 4 == 1) || (n % 4 == 2)) //4 bytes align
588 tc358768_wr_reg_32bits(dbg_data);
590 tc358768_wr_reg_32bits(0x00E0E000); //Start command transmisison
592 tc358768_wr_reg_32bits(0x00E02000); //Keep Mask High to prevent short packets send out
593 tc358768_wr_reg_32bits(0x00E00000); //Stop command transmission. This setting should be done just after above setting to prevent multiple output
598 int _tc358768_send_packet(unsigned char type, unsigned char regs[], int n) {
601 tc358768_command_tx_less8bytes(type, regs, n);
603 //tc358768_command_tx_more8bytes_hs(type, regs, n);
604 tc358768_command_tx_more8bytes_lp(type, regs, n);
609 int tc358768_send_packet(unsigned char type, unsigned char regs[], int n) {
610 return _tc358768_send_packet(type, regs, n);
615 The DCS is separated into two functional areas: the User Command Set and the Manufacturer Command
616 Set. Each command is an eight-bit code with 00h to AFh assigned to the User Command Set and all other
617 codes assigned to the Manufacturer Command Set.
619 int _mipi_dsi_send_dcs_packet(unsigned char regs[], int n) {
621 unsigned char type = 0;
623 type = DTYPE_DCS_SWRITE_0P;
625 type = DTYPE_DCS_SWRITE_1P;
627 type = DTYPE_DCS_LWRITE;
629 _tc358768_send_packet(type, regs, n);
633 int mipi_dsi_send_dcs_packet(unsigned char regs[], int n) {
634 return _mipi_dsi_send_dcs_packet(regs, n);
638 int _tc358768_rd_lcd_regs(unsigned char type, char comd, int size, unsigned char* buf) {
640 unsigned char regs[8];
641 u32 count = 0, data30, data32;
644 tc358768_command_tx_less8bytes(0x37, regs, 2);
645 tc358768_wr_reg_32bits(0x05040010);
646 tc358768_wr_reg_32bits(0x05060000);
648 tc358768_command_tx_less8bytes(type, regs, 1);
650 while (!(tc358768_rd_reg_32bits(0x0410) & 0x20)){
651 printk("error 0x0410:%04x\n", tc358768_rd_reg_32bits(0x0410));
658 data30 = tc358768_rd_reg_32bits(0x0430); //data id , word count[0:7]
659 //printk("0x0430:%04x\n", data30);
660 data32 = tc358768_rd_reg_32bits(0x0432); //word count[8:15] ECC
661 //printk("0x0432:%04x\n", data32);
664 data30 = tc358768_rd_reg_32bits(0x0430);
665 //printk("0x0430:%04x\n", data30);
666 data32 = tc358768_rd_reg_32bits(0x0432);
667 //printk("0x0432:%04x\n", data32);
674 *buf++ = (u8)(data30 >> 8);
680 *buf++ = (u8)(data32 >> 8);
684 data30 = tc358768_rd_reg_32bits(0x0430);
685 //printk("0x0430:%04x\n", data30);
686 data32 = tc358768_rd_reg_32bits(0x0432);
687 //printk("0x0432:%04x\n", data32);
691 int mipi_dsi_read_dcs_packet(unsigned char *data, int n) {
693 _tc358768_rd_lcd_regs(0x06, *data, n, data);
697 int tc358768_get_id(void) {
702 id = tc358768_rd_reg_32bits(0);
706 static struct mipi_dsi_ops tc358768_ops = {
709 .get_id = tc358768_get_id,
710 .dsi_set_regs = _tc358768_wr_regs_32bits,
711 .dsi_send_dcs_packet = mipi_dsi_send_dcs_packet,
712 .dsi_read_dcs_packet = mipi_dsi_read_dcs_packet,
713 .power_up = tc358768_power_up,
714 .power_down = tc358768_power_down,
718 static int __init tc358768_module_init(void)
720 #ifdef CONFIG_TC358768_I2C
721 i2c_add_driver(&tc358768_driver);
723 if(!tc358768 || !tc358768_client)
727 register_dsi_ops(&tc358768_ops);
729 tc358768_ops.id = tc358768->id;
733 static void __exit tc358768_module_exit(void)
735 del_dsi_ops(&tc358768_ops);
736 #ifdef CONFIG_TC358768_I2C
737 i2c_del_driver(&tc358768_driver);
741 subsys_initcall_sync(tc358768_module_init);
742 //module_exit(tc358768_module_init);
743 module_exit(tc358768_module_exit);