LinkIt Smart MT7688 >> C/C++

2.0" IPS 320x240(ILI9335)(GPIO)


driver/gpio/ili9335_gpio/main.c

#include <linux/init.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/gpio.h>
#include <asm/io.h>
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Steward_Fu");
MODULE_DESCRIPTION("ILI9335 SLCD Driver for MT7688");

#define do_output(val, pin) \
  if(val){ \
    gpio_set_value(pin, 1); \
  } \
  else{ \
    gpio_set_value(pin, 0); \
  }

#define do_request(pin, name) \
  if(gpio_request(pin, name) < 0){ \
    printk("failed to request gpio: %s\n", name); \
  } \
  else{ \
    printk("request successfully for gpio: %s\n", name); \
    gpio_direction_output(pin, 1); \
  }

#define GPIO1_MODE         0x10000060
#define GPIO2_MODE         0x10000064

#define ILI9335_SLCD_D17   16
#define ILI9335_SLCD_D16   17
#define ILI9335_SLCD_D15   19
#define ILI9335_SLCD_D14   18
#define ILI9335_SLCD_D13   6
#define ILI9335_SLCD_D12   4
#define ILI9335_SLCD_D11   5
#define ILI9335_SLCD_D10   12

#define ILI9335_SLCD_RS    13
#define ILI9335_SLCD_RST   45
#define ILI9335_SLCD_CS    46
#define ILI9335_SLCD_RD    44
#define ILI9335_SLCD_WR    37

static int g_blink_period=1000;
static struct timer_list g_blink_timer;

static void ili9335_reset(void)
{
  gpio_set_value(ILI9335_SLCD_RST, 1);  
  mdelay(100);
  gpio_set_value(ILI9335_SLCD_RST, 0);
  mdelay(100);  
  gpio_set_value(ILI9335_SLCD_RST, 1);  
  mdelay(100);
}

static void ili9335_send_command(unsigned int val)
{
  unsigned char i, cmd=0;

  gpio_set_value(ILI9335_SLCD_RS, 0);
  gpio_set_value(ILI9335_SLCD_RD, 1);
  gpio_set_value(ILI9335_SLCD_CS, 0);
  for(i=0; i<2; i++){
    cmd = (i == 0) ? (val >> 8) : val;
    do_output(cmd & 0x01, ILI9335_SLCD_D10);
    do_output(cmd & 0x02, ILI9335_SLCD_D11);
    do_output(cmd & 0x04, ILI9335_SLCD_D12);
    do_output(cmd & 0x08, ILI9335_SLCD_D13);
    do_output(cmd & 0x10, ILI9335_SLCD_D14);
    do_output(cmd & 0x20, ILI9335_SLCD_D15);
    do_output(cmd & 0x40, ILI9335_SLCD_D16);
    do_output(cmd & 0x80, ILI9335_SLCD_D17);
    gpio_set_value(ILI9335_SLCD_WR, 0);
    gpio_set_value(ILI9335_SLCD_WR, 1);
  }
  gpio_set_value(ILI9335_SLCD_CS, 1);
}

static void ili9335_send_data(unsigned int val)
{
  unsigned char i, dat=0;

  gpio_set_value(ILI9335_SLCD_RS, 1);
  gpio_set_value(ILI9335_SLCD_RD, 1);
  gpio_set_value(ILI9335_SLCD_CS, 0);
  for(i=0; i<2; i++){
    dat = (i == 0) ? (val >> 8) : val;
    do_output(dat & 0x01, ILI9335_SLCD_D10);
    do_output(dat & 0x02, ILI9335_SLCD_D11);
    do_output(dat & 0x04, ILI9335_SLCD_D12);
    do_output(dat & 0x08, ILI9335_SLCD_D13);
    do_output(dat & 0x10, ILI9335_SLCD_D14);
    do_output(dat & 0x20, ILI9335_SLCD_D15);
    do_output(dat & 0x40, ILI9335_SLCD_D16);
    do_output(dat & 0x80, ILI9335_SLCD_D17);
    gpio_set_value(ILI9335_SLCD_WR, 0);
    gpio_set_value(ILI9335_SLCD_WR, 1);
  }
  gpio_set_value(ILI9335_SLCD_CS, 1);
}

static void ili9335_send_register(unsigned int cmd, unsigned int data)
{
  ili9335_send_command(cmd);
  ili9335_send_data(data);
}

static void ili9335_init(void)
{
  ili9335_send_register(0x0001, 0x0100);
  ili9335_send_register(0x0002, 0x0200);
  ili9335_send_register(0x0003, 0x1018);
  ili9335_send_register(0x0008, 0x0202);
  ili9335_send_register(0x0009, 0x0000);
  ili9335_send_register(0x000A, 0x0000);
  ili9335_send_register(0x000C, 0x0000);
  ili9335_send_register(0x000D, 0x0000);
  ili9335_send_register(0x0060, 0x2700);  
  ili9335_send_register(0x0061, 0x0000);
  ili9335_send_register(0x006A, 0x0000);
  mdelay(10);
  ili9335_send_register(0x0010, 0x1490);
  ili9335_send_register(0x0011, 0x0227);
  mdelay(80);
  ili9335_send_register(0x0012, 0x000c);
  mdelay(10);
  ili9335_send_register(0x0013, 0x1000);
  ili9335_send_register(0x0029, 0x000b);
  ili9335_send_register(0x002b, 0x000b);
  mdelay(10);
  ili9335_send_register(0x0020, 0x0000);
  ili9335_send_register(0x0021, 0x0000);
  
  ili9335_send_register(0x0030, 0x0000);
  ili9335_send_register(0x0031, 0x0406);
  ili9335_send_register(0x0032, 0x0002);
  ili9335_send_register(0x0035, 0x0402);
  ili9335_send_register(0x0036, 0x0004);
  ili9335_send_register(0x0037, 0x0507);
  ili9335_send_register(0x0038, 0x0103);
  ili9335_send_register(0x0039, 0x0707);
  ili9335_send_register(0x003c, 0x0204);
  ili9335_send_register(0x003d, 0x0004);
  
  ili9335_send_register(0x0050, 0x0000);
  ili9335_send_register(0x0051, 0x00ef);
  ili9335_send_register(0x0052, 0x0000);
  ili9335_send_register(0x0053, 0x013f);

  mdelay(10);
  ili9335_send_register(0x0007, 0x0133);  
}

static void show_color(unsigned int val)
{
  int i, j;
 
  ili9335_send_command(0x0022);
  for(i=0; i<240; i++){
    for(j=0; j<320; j++){
      ili9335_send_data(val);
    }
  }
}

static void print_time(void)
{
  struct timeval t;
  struct tm broken;
 
  do_gettimeofday(&t);
  time_to_tm(t.tv_sec, 0, &broken);
  printk("%d:%d:%d:%ld\n", broken.tm_hour, broken.tm_min, broken.tm_sec, t.tv_usec);
}

void blink_handler(unsigned long unused)
{
  static int i=0;
  unsigned long col[]={0xf800, 0x7e0, 0x1f};

  show_color(col[i++ % 3]);
  mod_timer(&g_blink_timer, jiffies + msecs_to_jiffies(g_blink_period));
}

static int __init main_init(void)
{
  volatile unsigned long *ptr;

  ptr = ioremap(GPIO1_MODE, 4);
  *ptr&= 0xffff0fff;
  *ptr|= 0x51550555;
  iounmap(ptr);
  
  ptr = ioremap(GPIO2_MODE, 4);
  *ptr = 0x55555555;
  iounmap(ptr);

  do_request(ILI9335_SLCD_WR, "lcd_wr");
  do_request(ILI9335_SLCD_RD, "lcd_rd");
  do_request(ILI9335_SLCD_CS, "lcd_cs");
  do_request(ILI9335_SLCD_RS, "lcd_rs");
  do_request(ILI9335_SLCD_RST, "lcd_rst");
  do_request(ILI9335_SLCD_D10, "lcd_d10");
  do_request(ILI9335_SLCD_D11, "lcd_d11");
  do_request(ILI9335_SLCD_D12, "lcd_d12");
  do_request(ILI9335_SLCD_D13, "lcd_d13");
  do_request(ILI9335_SLCD_D14, "lcd_d14");
  do_request(ILI9335_SLCD_D15, "lcd_d15");
  do_request(ILI9335_SLCD_D16, "lcd_d16");
  do_request(ILI9335_SLCD_D17, "lcd_d17");

  ili9335_reset();
  ili9335_init();
  
  setup_timer(&g_blink_timer, blink_handler, 0);
  mod_timer(&g_blink_timer, jiffies + msecs_to_jiffies(g_blink_period));
  return 0;
}
 
static void __exit main_exit(void)
{
  del_timer(&g_blink_timer);
  gpio_free(ILI9335_SLCD_WR);
  gpio_free(ILI9335_SLCD_RD);
  gpio_free(ILI9335_SLCD_CS);
  gpio_free(ILI9335_SLCD_RS);
  gpio_free(ILI9335_SLCD_RST);
  gpio_free(ILI9335_SLCD_D10);
  gpio_free(ILI9335_SLCD_D11);
  gpio_free(ILI9335_SLCD_D12);
  gpio_free(ILI9335_SLCD_D13);
  gpio_free(ILI9335_SLCD_D14);
  gpio_free(ILI9335_SLCD_D15);
  gpio_free(ILI9335_SLCD_D16);
  gpio_free(ILI9335_SLCD_D17);
}
 
module_init(main_init);
module_exit(main_exit);

完成


返回上一頁