2 * Copyright (C) 2012 ROCKCHIP, Inc.
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.
19 #define dsi_debug printk
21 #define dsi_debug(fmt...) do { } while (0)
24 #ifdef CONFIG_TC358768_I2C
25 struct tc358768_t *tc358768 = NULL;
26 struct i2c_client *tc358768_client = NULL;
27 struct mipi_dsi_t *dsi;
30 u32 i2c_write_32bits(u32 value)
40 msgs.addr = tc358768_client->addr;
41 msgs.flags = tc358768_client->flags;
44 msgs.scl_rate = CONFIG_TC358768_I2C_CLK;
45 msgs.udelay = tc358768_client->udelay;
47 ret = i2c_transfer(tc358768_client->adapter, &msgs, 1);
49 printk("%s:i2c_transfer fail =%d\n",__func__, ret);
53 u32 i2c_read_32bits(u32 value)
55 struct i2c_msg msgs[2];
61 msgs[0].addr = tc358768_client->addr;
62 msgs[0].flags = tc358768_client->flags;
65 msgs[0].scl_rate = CONFIG_TC358768_I2C_CLK;
66 msgs[0].udelay = tc358768_client->udelay;
68 msgs[1].addr = tc358768_client->addr;
69 msgs[1].flags = tc358768_client->flags | I2C_M_RD;
72 msgs[1].scl_rate = CONFIG_TC358768_I2C_CLK;
73 msgs[1].udelay = tc358768_client->udelay;
75 ret = i2c_transfer(tc358768_client->adapter, msgs, 2);
77 printk("%s:i2c_transfer fail =%d\n",__func__, ret);
79 ret = (buf[0]<<8) | buf[1];
85 int tc358768_gpio_init(void *data) {
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");
92 gpio_free(reset->reset_pin);
93 printk("%s: request TC358768_RST_PIN error\n", __func__);
96 rk30_mux_api_set(reset->mux_name, reset->mux_mode);
97 gpio_direction_output(reset->reset_pin, reset->effect_value);
101 if(vdd->enable_pin > INVALID_GPIO) {
102 ret = gpio_request(vdd->enable_pin, "tc358768_vddc");
104 gpio_free(vdd->enable_pin);
105 printk("%s: request TC358768_vddc_PIN error\n", __func__);
108 rk30_mux_api_set(vdd->mux_name, vdd->mux_mode);
109 gpio_direction_output(vdd->enable_pin, !vdd->effect_value);
113 vdd = &tc358768->vddio;
114 if(vdd->enable_pin > INVALID_GPIO) {
115 ret = gpio_request(vdd->enable_pin, "tc358768_vddio");
117 gpio_free(vdd->enable_pin);
118 printk("%s: request TC358768_vddio_PIN error\n", __func__);
121 rk30_mux_api_set(vdd->mux_name, vdd->mux_mode);
122 gpio_direction_output(vdd->enable_pin, !vdd->effect_value);
126 vdd = &tc358768->vdd_mipi;
127 if(vdd->enable_pin > INVALID_GPIO) {
128 ret = gpio_request(vdd->enable_pin, "tc358768_vdd_mipi");
130 gpio_free(vdd->enable_pin);
131 printk("%s: request TC358768_vdd_mipi_PIN error\n", __func__);
134 rk30_mux_api_set(vdd->mux_name, vdd->mux_mode);
135 gpio_direction_output(vdd->enable_pin, !vdd->effect_value);
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);
147 vdd = &tc358768->vddio;
148 gpio_free(vdd->enable_pin);
150 vdd = &tc358768->vdd_mipi;
151 gpio_free(vdd->enable_pin);
155 int tc358768_reset(void *data) {
157 struct reset_t *reset = &tc358768->reset;
158 if(reset->reset_pin <= INVALID_GPIO)
160 gpio_set_value(reset->reset_pin, reset->effect_value);
161 if(reset->time_before_reset <= 0)
164 msleep(reset->time_before_reset);
166 gpio_set_value(reset->reset_pin, !reset->effect_value);
167 if(reset->time_after_reset <= 0)
170 msleep(reset->time_after_reset);
174 int tc358768_vdd_enable(void *data) {
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);
185 int tc358768_vdd_disable(void *data) {
187 struct power_t *vdd = (struct power_t *)data;
189 if(vdd->enable_pin > INVALID_GPIO) {
190 gpio_set_value(vdd->enable_pin, !vdd->effect_value);
198 int tc358768_power_up(void *data) {
200 struct tc358768_t *tc = (struct tc358768_t *)tc358768;
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);
210 int tc358768_power_down(void *data) {
212 struct tc358768_t *tc = (struct tc358768_t *)tc358768;
214 tc->vddio.disable(&tc->vddio);
215 tc->vdd_mipi.disable(&tc->vdd_mipi);
216 tc->vddc.disable(&tc->vddc);
221 static int tc358768_probe(struct i2c_client *client,
222 const struct i2c_device_id *did)
224 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
228 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
229 dev_warn(&adapter->dev,
230 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
233 tc358768 = (struct tc358768_t *)client->dev.platform_data;
236 printk("%s:%d tc358768 is null\n", __func__, __LINE__);
240 tc358768_client = client;
241 if(!tc358768_client) {
243 printk("%s:%d tc358768_client is null\n", __func__, __LINE__);
247 if(!tc358768->gpio_init)
248 tc358768->gpio_init = tc358768_gpio_init;
250 if(!tc358768->gpio_deinit)
251 tc358768->gpio_deinit = tc358768_gpio_deinit;
253 if(!tc358768->power_up)
254 tc358768->power_up = tc358768_power_up;
255 if(!tc358768->power_down)
256 tc358768->power_down = tc358768_power_down;
258 if(!tc358768->reset.do_reset)
259 tc358768->reset.do_reset = tc358768_reset;
261 if(!tc358768->vddc.enable)
262 tc358768->vddc.enable = tc358768_vdd_enable;
263 if(!tc358768->vddc.disable)
264 tc358768->vddc.disable = tc358768_vdd_disable;
266 if(!tc358768->vddio.enable)
267 tc358768->vddio.enable = tc358768_vdd_enable;
268 if(!tc358768->vddio.disable)
269 tc358768->vddio.disable = tc358768_vdd_disable;
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;
276 dsi->chip = tc358768;
280 static int tc358768_remove(struct i2c_client *client)
282 tc358768_client = NULL;
287 static const struct i2c_device_id tc358768_id[] = {
291 MODULE_DEVICE_TABLE(i2c, tc358768_id);
293 static struct i2c_driver tc358768_driver = {
294 .probe = tc358768_probe,
295 .remove = tc358768_remove,
296 .id_table = tc358768_id,
303 u32 spi_read_32bits(u32 addr)
317 if(addr & 0x80000000)
336 if(addr & 0x80000000)
355 if (gpio_get_value(gLcd_info->rxd_pin) == 1)
366 u32 spi_write_32bits(u32 value)
376 if(value & 0x80000000)
391 u32 tc358768_wr_reg_32bits(u32 data) {
392 #ifdef CONFIG_TC358768_I2C
393 i2c_write_32bits(data);
395 spi_write_32bits(data);
401 u32 tc358768_wr_reg_32bits_delay(u32 delay, u32 data) {
402 //wait a minute according to the source format
409 #ifdef CONFIG_TC358768_I2C
410 i2c_write_32bits(data);
412 spi_write_32bits(data);
419 u32 tc358768_rd_reg_32bits(u32 addr) {
420 #ifdef CONFIG_TC358768_I2C
421 return i2c_read_32bits(addr);
423 return spi_read_32bits(addr);
429 void tc_print(u32 addr) {
430 dsi_debug("+++++++++++addr->%04x: %04x\n", addr, tc358768_rd_reg_32bits(addr));
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) {
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]);
443 mdelay(reg_array[i]/1000);
446 tc358768_wr_reg_32bits(reg_array[i]);
452 int tc358768_command_tx_less8bytes(unsigned char type, unsigned char *regs, int n) {
454 unsigned int command[] = {
464 command[0] |= 0x1000; //short packet
466 command[0] |= 0x4000; //long packet
467 command[1] |= n; //word count byte
469 command[0] |= type; //data type
471 //dsi_debug("*cmd:\n");
472 //dsi_debug("0x%08x\n", command[0]);
473 //dsi_debug("0x%08x\n", command[1]);
475 for(i = 0; i < (n + 1)/2; i++) {
476 command[i+2] |= regs[i*2];
478 command[i+2] |= regs[i*2 + 1] << 8;
479 dsi_debug("0x%08x\n", command[i+2]);
482 _tc358768_wr_regs_32bits(command, (n + 1)/2 + 2);
483 tc358768_wr_reg_32bits(0x06000001); //Packet Transfer
484 //wait until packet is out
486 while(tc358768_rd_reg_32bits(0x0600) & 0x01) {
495 int tc358768_command_tx_more8bytes_hs(unsigned char type, unsigned char regs[], int n) {
498 unsigned int dbg_data = 0x00E80000, temp = 0;
499 unsigned int command[] = {
500 0x05000080, //HS data 4 lane, EOT is added
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
511 command[3] |= type; //data type
512 command[4] |= n & 0xffff; //Transmission byte count
514 tc358768_wr_regs_32bits(command);
516 for(i = 0; i < (n + 1)/2; i++) {
517 temp = dbg_data | regs[i*2];
519 temp |= (regs[i*2 + 1] << 8);
520 //dsi_debug("0x%08x\n", temp);
521 tc358768_wr_reg_32bits(temp);
523 if((n % 4 == 1) || (n % 4 == 2)) //4 bytes align
524 tc358768_wr_reg_32bits(dbg_data);
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
530 //tc358768_wr_regs_32bits(re_initialize);
534 //low power mode only for tc358768a
535 int tc358768_command_tx_more8bytes_lp(unsigned char type, unsigned char regs[], int n) {
538 unsigned int dbg_data = 0x00E80000, temp = 0;
539 unsigned int command[] = {
541 0x00500000, //Data ID setting
542 0x00220000, //Transmission byte count= byte
543 0x00E08000, //Enable I2C/SPI write to VB
546 command[1] |= type; //data type
547 command[2] |= n & 0xffff; //Transmission byte count
549 tc358768_wr_regs_32bits(command);
551 for(i = 0; i < (n + 1)/2; i++) {
552 temp = dbg_data | regs[i*2];
554 temp |= (regs[i*2 + 1] << 8);
555 //dsi_debug("0x%08x\n", temp);
556 tc358768_wr_reg_32bits(temp);
559 if((n % 4 == 1) || (n % 4 == 2)) //4 bytes align
560 tc358768_wr_reg_32bits(dbg_data);
562 tc358768_wr_reg_32bits(0x00E0E000); //Start command transmisison
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
570 int _tc358768_send_packet(unsigned char type, unsigned char regs[], int n) {
573 tc358768_command_tx_less8bytes(type, regs, n);
575 //tc358768_command_tx_more8bytes_hs(type, regs, n);
576 tc358768_command_tx_more8bytes_lp(type, regs, n);
581 int tc358768_send_packet(unsigned char type, unsigned char regs[], int n) {
582 return _tc358768_send_packet(type, regs, n);
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.
591 int _mipi_dsi_send_dcs_packet(unsigned char regs[], int n) {
593 unsigned char type = 0;
595 type = DTYPE_DCS_SWRITE_0P;
597 type = DTYPE_DCS_SWRITE_1P;
599 type = DTYPE_DCS_LWRITE;
601 _tc358768_send_packet(type, regs, n);
605 int mipi_dsi_send_dcs_packet(unsigned char regs[], int n) {
606 return _mipi_dsi_send_dcs_packet(regs, n);
610 int _tc358768_rd_lcd_regs(unsigned char type, char comd, int size, unsigned char* buf) {
612 unsigned char regs[8];
613 u32 count = 0, data30, data32;
616 tc358768_command_tx_less8bytes(0x37, regs, 2);
617 tc358768_wr_reg_32bits(0x05040010);
618 tc358768_wr_reg_32bits(0x05060000);
620 tc358768_command_tx_less8bytes(type, regs, 1);
622 while (!(tc358768_rd_reg_32bits(0x0410) & 0x20)){
623 printk("error 0x0410:%04x\n", tc358768_rd_reg_32bits(0x0410));
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);
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);
646 *buf++ = (u8)(data30 >> 8);
652 *buf++ = (u8)(data32 >> 8);
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);
663 int mipi_dsi_read_dcs_packet(unsigned char *data, int n) {
665 _tc358768_rd_lcd_regs(0x06, *data, n, data);
670 int tc358768_init(struct mipi_dsi_t *pram) {
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);
683 tc358768_gpio_init(NULL);
686 //tc358768_power_down(NULL);
687 tc358768_gpio_deinit(NULL);