Advertisement
Guest User

ssd1289.c

a guest
Feb 15th, 2013
1,550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 25.10 KB | None | 0 0
  1. /*
  2.  * SSD1289 Framebuffer
  3.  *
  4.  *
  5.  * Original: Copyright (c) 2009 Jean-Christian de Rivaz
  6.  *
  7.  * SPI mods, console support, 320x240 instead of 240x320:
  8.  * Copyright (c) 2012 Jeroen Domburg <jeroen@spritesmods.com>
  9.  *
  10.  * Bits and pieces borrowed from the fsl-ssd1289.c:
  11.  * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
  12.  * Author: Alison Wang <b18965@freescale.com>
  13.  *         Jason Jin <Jason.jin@freescale.com>
  14.  *
  15.  * This file is subject to the terms and conditions of the GNU General Public
  16.  * License.  See the file "COPYING" in the main directory of this archive
  17.  * for more details.
  18.  *
  19.  * The Solomon Systech SSD1289 chip drive TFT screen up to 240x320.
  20.  *
  21.  * For direct I/O-mode:
  22.  *
  23.  * This driver expect the SSD1286 to be connected to a 16 bits local bus
  24.  * and to be set in the 16 bits parallel interface mode. To use it you must
  25.  * define in your board file a struct platform_device with a name set to
  26.  * "ssd1289" and a struct resource array with two IORESOURCE_MEM: the first
  27.  * for the control register; the second for the data register.
  28.  *
  29.  * For SPI mode:
  30.  *
  31.  * A SPI port with modalias 'ssd1289' should exist. The LCD should be in 16-bit
  32.  * mode (which they usually are hardwired to be...) and connected to three
  33.  * CD4094s and a CD4020. For schematic, check out
  34.  * http://spritesmods.com/?art=spitft
  35.  *
  36.  * LCDs in their own, native SPI mode aren't supported yet, mostly because I
  37.  * can't get my hands on a cheap one.
  38.  */
  39.  
  40. #include <linux/kernel.h>
  41. #include <linux/device.h>
  42. #include <linux/module.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/mm.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/fb.h>
  47. #include <asm/io.h>
  48. #include <linux/spi/spi.h>
  49.  
  50. #define SSD1289_REG_OSCILLATION      0x00
  51. #define SSD1289_REG_DRIVER_OUT_CTRL  0x01
  52. #define SSD1289_REG_LCD_DRIVE_AC     0x02
  53. #define SSD1289_REG_POWER_CTRL_1     0x03
  54. #define SSD1289_REG_DISPLAY_CTRL     0x07
  55. #define SSD1289_REG_FRAME_CYCLE      0x0b
  56. #define SSD1289_REG_POWER_CTRL_2     0x0c
  57. #define SSD1289_REG_POWER_CTRL_3     0x0d
  58. #define SSD1289_REG_POWER_CTRL_4     0x0e
  59. #define SSD1289_REG_GATE_SCAN_START  0x0f
  60. #define SSD1289_REG_SLEEP_MODE       0x10
  61. #define SSD1289_REG_ENTRY_MODE       0x11
  62. #define SSD1289_REG_POWER_CTRL_5     0x1e
  63. #define SSD1289_REG_GDDRAM_DATA      0x22
  64. #define SSD1289_REG_WR_DATA_MASK_1   0x23
  65. #define SSD1289_REG_WR_DATA_MASK_2   0x24
  66. #define SSD1289_REG_FRAME_FREQUENCY  0x25
  67. #define SSD1289_REG_GAMMA_CTRL_1     0x30
  68. #define SSD1289_REG_GAMME_CTRL_2     0x31
  69. #define SSD1289_REG_GAMMA_CTRL_3     0x32
  70. #define SSD1289_REG_GAMMA_CTRL_4     0x33
  71. #define SSD1289_REG_GAMMA_CTRL_5     0x34
  72. #define SSD1289_REG_GAMMA_CTRL_6     0x35
  73. #define SSD1289_REG_GAMMA_CTRL_7     0x36
  74. #define SSD1289_REG_GAMMA_CTRL_8     0x37
  75. #define SSD1289_REG_GAMMA_CTRL_9     0x3a
  76. #define SSD1289_REG_GAMMA_CTRL_10    0x3b
  77. #define SSD1289_REG_V_SCROLL_CTRL_1  0x41
  78. #define SSD1289_REG_V_SCROLL_CTRL_2  0x42
  79. #define SSD1289_REG_H_RAM_ADR_POS    0x44
  80. #define SSD1289_REG_V_RAM_ADR_START  0x45
  81. #define SSD1289_REG_V_RAM_ADR_END    0x46
  82. #define SSD1289_REG_FIRST_WIN_START  0x48
  83. #define SSD1289_REG_FIRST_WIN_END    0x49
  84. #define SSD1289_REG_SECND_WIN_START  0x4a
  85. #define SSD1289_REG_SECND_WIN_END    0x4b
  86. #define SSD1289_REG_GDDRAM_X_ADDR    0x4e
  87. #define SSD1289_REG_GDDRAM_Y_ADDR    0x4f
  88.  
  89. struct ssd1289_page {
  90.     unsigned short x;
  91.     unsigned short y;
  92.     unsigned short *buffer;
  93.     unsigned short len;
  94.     int must_update;
  95. };
  96.  
  97. struct ssd1289 {
  98.     struct device *dev;
  99. #if defined(CONFIG_SSD1289_DIRECTIO_MODE)
  100.     volatile unsigned short *ctrl_io;
  101.     volatile unsigned short *data_io;
  102. #elif defined(CONFIG_SSD1289_SPI_MODE)
  103.     struct spi_device *spidev;
  104. #endif
  105.     struct fb_info *info;
  106.     unsigned int pages_count;
  107.     struct ssd1289_page *pages;
  108.     unsigned long pseudo_palette[17];
  109.     int backlight;
  110. };
  111.  
  112.  
  113. #if defined(CONFIG_SSD1289_SPI_MODE)
  114. /*
  115. These two routines will write to the SPI-port, shifting data into the 4094 shift
  116. registers.
  117. */
  118. #define CD4094_RD (1<<0)
  119. #define CD4094_WR (1<<1)
  120. #define CD4094_BCNT (1<<2)
  121. #define CD4094_RST (1<<3)
  122. #define CD4094_DC (1<<4)
  123. #define CD4094_INVERT (CD4094_RD|CD4094_WR|CD4094_RST)
  124.  
  125. /*
  126. This routine will write a single 16-bit value, either as data or as a command
  127. (depends on isdata). The LCD will clock this in because the SPIs /CS goes high.
  128. */
  129. static int ssd1289_spi_write(struct ssd1289 *item, unsigned short value,
  130.                 unsigned int isdata)
  131. {
  132.     u8 buf[3];
  133.     buf[0]=((isdata?CD4094_DC:0)|CD4094_WR|(item->backlight?CD4094_BCNT:0))^CD4094_INVERT;
  134.     buf[1]=(value>>8)&0xff;
  135.     buf[2]=(value)&0xff;
  136.     spi_write(item->spidev, buf, 3);
  137.     return 0;
  138. }
  139.  
  140. #define BLOCKLEN (4096)
  141. static u8 ssd1289_spiblock[BLOCKLEN*4];
  142.  
  143. /*
  144. This routine will clock in len words of data. The LCD will clock this in every 4th byte written
  145. because a 4020 will pull down the /cs at that moment.
  146. */
  147. static int ssd1289_spi_write_datablock(struct ssd1289 *item,
  148.                     unsigned short *block, int len)
  149. {
  150.     int x;
  151.     unsigned short value;
  152.  
  153.     //ToDo: send in parts if needed
  154.     if (len>BLOCKLEN) {
  155.         dev_err(item->dev, "%s: len > blocklen (%i > %i)\n",
  156.             __func__, len, BLOCKLEN);
  157.         len=BLOCKLEN;
  158.     }
  159.  
  160.     for (x=0; x<len; x++) {
  161.         value=block[x];
  162.         ssd1289_spiblock[(x*4)]=0; //dummy
  163.         ssd1289_spiblock[(x*4)+1]=(CD4094_DC|CD4094_WR|(item->backlight?CD4094_BCNT:0))^CD4094_INVERT;
  164.         ssd1289_spiblock[(x*4)+2]=(value>>8)&0xff;
  165.         ssd1289_spiblock[(x*4)+3]=(value)&0xff;
  166.     }
  167.     spi_write(item->spidev, ssd1289_spiblock, len*4);
  168.     return 0;
  169. }
  170. #endif
  171.  
  172.  
  173. static inline void ssd1289_reg_set(struct ssd1289 *item, unsigned char reg,
  174.                    unsigned short value)
  175. {
  176. #if defined(CONFIG_SSD1289_DIRECTIO_MODE)
  177.     unsigned short ctrl = reg & 0x00ff;
  178.     writew(ctrl, item->ctrl_io);
  179.     writew(value, item->data_io);
  180. #elif defined(CONFIG_SSD1289_SPI_MODE)
  181.     ssd1289_spi_write(item, reg&0xff, 0);
  182.     ssd1289_spi_write(item, value, 1);
  183. #endif
  184. }
  185.  
  186. static void ssd1289_copy(struct ssd1289 *item, unsigned int index)
  187. {
  188.     unsigned short x;
  189.     unsigned short y;
  190.     unsigned short *buffer;
  191.     unsigned int len;
  192. #if defined(CONFIG_SSD1289_DIRECTIO_MODE)
  193.     unsigned int count;
  194. #endif
  195.     x = item->pages[index].x;
  196.     y = item->pages[index].y;
  197.     buffer = item->pages[index].buffer;
  198.     len = item->pages[index].len;
  199.     dev_dbg(item->dev,
  200.         "%s: page[%u]: x=%3hu y=%3hu buffer=0x%p len=%3hu\n",
  201.         __func__, index, x, y, buffer, len);
  202.  
  203.     ssd1289_reg_set(item, SSD1289_REG_GDDRAM_X_ADDR, (item->info->var.yres - 1)-y);
  204.     ssd1289_reg_set(item, SSD1289_REG_GDDRAM_Y_ADDR, x);
  205. #if defined(CONFIG_SSD1289_DIRECTIO_MODE)
  206.     writew(SSD1289_REG_GDDRAM_DATA, item->ctrl_io);
  207.     for (count = 0; count < len; count++) {
  208.         writew(buffer[count], item->data_io);
  209.     }
  210. #elif defined(CONFIG_SSD1289_SPI_MODE)
  211.     ssd1289_spi_write(item, SSD1289_REG_GDDRAM_DATA, 0);
  212.  
  213.     ssd1289_spi_write_datablock(item, buffer, len);
  214. /*
  215. //The write_datablock can also be exchanged with this code, which is slower but
  216. //doesn't require the CD4020 counter IC.
  217.     for (count = 0; count < len; count++) {
  218.         ssd1289_spi_write(item, buffer[count], 1);
  219.     }
  220. */
  221. #endif
  222. }
  223.  
  224. static void ssd1289_update_all(struct ssd1289 *item)
  225. {
  226.     unsigned short i;
  227.     struct fb_deferred_io *fbdefio = item->info->fbdefio;
  228.     for (i = 0; i < item->pages_count; i++) {
  229.         item->pages[i].must_update=1;
  230.     }
  231.     schedule_delayed_work(&item->info->deferred_work, fbdefio->delay);
  232. }
  233.  
  234. static void ssd1289_update(struct fb_info *info, struct list_head *pagelist)
  235. {
  236.     struct ssd1289 *item = (struct ssd1289 *)info->par;
  237.     struct page *page;
  238.     int i;
  239.  
  240.     //We can be called because of pagefaults (mmap'ed framebuffer, pages
  241.     //returned in *pagelist) or because of kernel activity
  242.     //(pages[i]/must_update!=0). Add the former to the list of the latter.
  243.     list_for_each_entry(page, pagelist, lru) {
  244.         item->pages[page->index].must_update=1;
  245.     }
  246.  
  247.     //Copy changed pages.
  248.     for (i=0; i<item->pages_count; i++) {
  249.         //ToDo: Small race here between checking and setting must_update,
  250.         //maybe lock?
  251.         if (item->pages[i].must_update) {
  252.             item->pages[i].must_update=0;
  253.             ssd1289_copy(item, i);
  254.         }
  255.     }
  256.  
  257. }
  258.  
  259.  
  260. static void __init ssd1289_setup(struct ssd1289 *item)
  261. {
  262.     dev_dbg(item->dev, "%s: item=0x%p\n", __func__, (void *)item);
  263.     // OSCEN=1
  264.     ssd1289_reg_set(item, SSD1289_REG_OSCILLATION, 0x0001);
  265.     // DCT=b1010=fosc/4 BT=b001=VGH:+6,VGL:-4
  266.     // DC=b1010=fosc/4 AP=b010=small to medium
  267.     ssd1289_reg_set(item, SSD1289_REG_POWER_CTRL_1, 0xa2a4);
  268.     // VRC=b100:5.5V
  269.     ssd1289_reg_set(item, SSD1289_REG_POWER_CTRL_2, 0x0004);
  270.     // VRH=b1000:Vref*2.165
  271.     ssd1289_reg_set(item, SSD1289_REG_POWER_CTRL_3, 0x0308);
  272.     // VCOMG=1 VDV=b1000:VLCD63*1.05
  273.     ssd1289_reg_set(item, SSD1289_REG_POWER_CTRL_4, 0x3000);
  274.     // nOTP=1 VCM=0x2a:VLCD63*0.77
  275.     ssd1289_reg_set(item, SSD1289_REG_POWER_CTRL_5, 0x00ea);
  276.     // RL=0 REV=1 CAD=0 BGR=1 SM=0 TB=1 MUX=0x13f=319
  277.     ssd1289_reg_set(item, SSD1289_REG_DRIVER_OUT_CTRL, 0x2b3f);
  278.     // FLD=0 ENWS=0 D/C=1 EOR=1 WSMD=0 NW=0x00=0
  279.     ssd1289_reg_set(item, SSD1289_REG_LCD_DRIVE_AC, 0x0600);
  280.     // SLP=0
  281.     ssd1289_reg_set(item, SSD1289_REG_SLEEP_MODE, 0x0000);
  282.     // VSMode=0 DFM=3:65k TRAMS=0 OEDef=0 WMode=0 Dmode=0
  283.     // TY=0 ID=2 AM=1 LG=0 (orig: ID=3, AM=0)
  284. //  ssd1289_reg_set(item, SSD1289_REG_ENTRY_MODE, 0x6030);
  285.     ssd1289_reg_set(item, SSD1289_REG_ENTRY_MODE, 0x6028);
  286.     // PT=0 VLE=1 SPT=0 GON=1 DTE=1 CM=0 D=3
  287.     ssd1289_reg_set(item, SSD1289_REG_DISPLAY_CTRL, 0x0233);
  288.     // NO=0 SDT=0 EQ=0 DIV=0 SDIV=1 SRTN=1 RTN=9:25 clock
  289.     ssd1289_reg_set(item, SSD1289_REG_FRAME_CYCLE, 0x0039);
  290.     // SCN=0
  291.     ssd1289_reg_set(item, SSD1289_REG_GATE_SCAN_START, 0x0000);
  292.  
  293.     // PKP1=7 PKP0=7
  294.     ssd1289_reg_set(item, SSD1289_REG_GAMMA_CTRL_1, 0x0707);
  295.     // PKP3=2 PKP2=4
  296.     ssd1289_reg_set(item, SSD1289_REG_GAMME_CTRL_2, 0x0204);
  297.     // PKP5=2 PKP4=2
  298.     ssd1289_reg_set(item, SSD1289_REG_GAMMA_CTRL_3, 0x0204);
  299.     // PRP1=5 PRP0=2
  300.     ssd1289_reg_set(item, SSD1289_REG_GAMMA_CTRL_4, 0x0502);
  301.     // PKN1=5 PKN0=7
  302.     ssd1289_reg_set(item, SSD1289_REG_GAMMA_CTRL_5, 0x0507);
  303.     // PKN3=2 PNN2=4
  304.     ssd1289_reg_set(item, SSD1289_REG_GAMMA_CTRL_6, 0x0204);
  305.     // PKN5=2 PKN4=4
  306.     ssd1289_reg_set(item, SSD1289_REG_GAMMA_CTRL_7, 0x0204);
  307.     // PRN1=5 PRN0=2
  308.     ssd1289_reg_set(item, SSD1289_REG_GAMMA_CTRL_8, 0x0502);
  309.     // VRP1=3 VRP0=2
  310.     ssd1289_reg_set(item, SSD1289_REG_GAMMA_CTRL_9, 0x0302);
  311.     // VRN1=3 VRN0=2
  312.     ssd1289_reg_set(item, SSD1289_REG_GAMMA_CTRL_10, 0x0302);
  313.  
  314.     // WMR=0 WMG=0
  315.     ssd1289_reg_set(item, SSD1289_REG_WR_DATA_MASK_1, 0x0000);
  316.     // WMB=0
  317.     ssd1289_reg_set(item, SSD1289_REG_WR_DATA_MASK_2, 0x0000);
  318.     // OSC=b1010:548k
  319.     ssd1289_reg_set(item, SSD1289_REG_FRAME_FREQUENCY, 0xa000);
  320.     // SS1=0
  321.     ssd1289_reg_set(item, SSD1289_REG_FIRST_WIN_START, 0x0000);
  322.     // SE1=319
  323.     ssd1289_reg_set(item, SSD1289_REG_FIRST_WIN_END,
  324.             (item->info->var.xres - 1));
  325.     // SS2=0
  326.     ssd1289_reg_set(item, SSD1289_REG_SECND_WIN_START, 0x0000);
  327.     // SE2=0
  328.     ssd1289_reg_set(item, SSD1289_REG_SECND_WIN_END, 0x0000);
  329.     // VL1=0
  330.     ssd1289_reg_set(item, SSD1289_REG_V_SCROLL_CTRL_1, 0x0000);
  331.     // VL2=0
  332.     ssd1289_reg_set(item, SSD1289_REG_V_SCROLL_CTRL_2, 0x0000);
  333.     // HEA=0xef=239 HSA=0
  334.     ssd1289_reg_set(item, SSD1289_REG_H_RAM_ADR_POS,
  335.             (item->info->var.yres - 1) << 8);
  336.     // VSA=0
  337.     ssd1289_reg_set(item, SSD1289_REG_V_RAM_ADR_START, 0x0000);
  338.     // VEA=0x13f=319
  339.     ssd1289_reg_set(item, SSD1289_REG_V_RAM_ADR_END,
  340.             (item->info->var.xres - 1));
  341. }
  342.  
  343. //This routine will allocate the buffer for the complete framebuffer. This
  344. //is one continuous chunk of 16-bit pixel values; userspace programs
  345. //will write here.
  346. static int __init ssd1289_video_alloc(struct ssd1289 *item)
  347. {
  348.     unsigned int frame_size;
  349.  
  350.     dev_dbg(item->dev, "%s: item=0x%p\n", __func__, (void *)item);
  351.  
  352.     frame_size = item->info->fix.line_length * item->info->var.yres;
  353.     dev_dbg(item->dev, "%s: item=0x%p frame_size=%u\n",
  354.         __func__, (void *)item, frame_size);
  355.  
  356.     item->pages_count = frame_size / PAGE_SIZE;
  357.     if ((item->pages_count * PAGE_SIZE) < frame_size) {
  358.         item->pages_count++;
  359.     }
  360.     dev_dbg(item->dev, "%s: item=0x%p pages_count=%u\n",
  361.         __func__, (void *)item, item->pages_count);
  362.  
  363.     item->info->fix.smem_len = item->pages_count * PAGE_SIZE;
  364.     item->info->fix.smem_start =
  365.         (unsigned long)vmalloc(item->info->fix.smem_len);
  366.     if (!item->info->fix.smem_start) {
  367.         dev_err(item->dev, "%s: unable to vmalloc\n", __func__);
  368.         return -ENOMEM;
  369.     }
  370.     memset((void *)item->info->fix.smem_start, 0, item->info->fix.smem_len);
  371.  
  372.     return 0;
  373. }
  374.  
  375. static void ssd1289_video_free(struct ssd1289 *item)
  376. {
  377.     dev_dbg(item->dev, "%s: item=0x%p\n", __func__, (void *)item);
  378.     vfree((void *)item->info->fix.smem_start);
  379. }
  380.  
  381. //This routine will allocate a ssd1289_page struct for each vm page in the
  382. //main framebuffer memory. Each struct will contain a pointer to the page
  383. //start, an x- and y-offset, and the length of the pagebuffer which is in the framebuffer.
  384. static int __init ssd1289_pages_alloc(struct ssd1289 *item)
  385. {
  386.     unsigned short pixels_per_page;
  387.     unsigned short yoffset_per_page;
  388.     unsigned short xoffset_per_page;
  389.     unsigned short index;
  390.     unsigned short x = 0;
  391.     unsigned short y = 0;
  392.     unsigned short *buffer;
  393.     unsigned int len;
  394.  
  395.     dev_dbg(item->dev, "%s: item=0x%p\n", __func__, (void *)item);
  396.  
  397.     item->pages = kmalloc(item->pages_count * sizeof(struct ssd1289_page),
  398.                   GFP_KERNEL);
  399.     if (!item->pages) {
  400.         dev_err(item->dev, "%s: unable to kmalloc for ssd1289_page\n",
  401.             __func__);
  402.         return -ENOMEM;
  403.     }
  404.  
  405.     pixels_per_page = PAGE_SIZE / (item->info->var.bits_per_pixel / 8);
  406.     yoffset_per_page = pixels_per_page / item->info->var.xres;
  407.     xoffset_per_page = pixels_per_page -
  408.         (yoffset_per_page * item->info->var.xres);
  409.     dev_dbg(item->dev, "%s: item=0x%p pixels_per_page=%hu "
  410.         "yoffset_per_page=%hu xoffset_per_page=%hu\n",
  411.         __func__, (void *)item, pixels_per_page,
  412.         yoffset_per_page, xoffset_per_page);
  413.  
  414.     buffer = (unsigned short *)item->info->fix.smem_start;
  415.     for (index = 0; index < item->pages_count; index++) {
  416.         len = (item->info->var.xres * item->info->var.yres) -
  417.             (index * pixels_per_page);
  418.         if (len > pixels_per_page) {
  419.             len = pixels_per_page;
  420.         }
  421.         dev_dbg(item->dev,
  422.             "%s: page[%d]: x=%3hu y=%3hu buffer=0x%p len=%3hu\n",
  423.             __func__, index, x, y, buffer, len);
  424.         item->pages[index].x = x;
  425.         item->pages[index].y = y;
  426.         item->pages[index].buffer = buffer;
  427.         item->pages[index].len = len;
  428.  
  429.         x += xoffset_per_page;
  430.         if (x >= item->info->var.xres) {
  431.             y++;
  432.             x -= item->info->var.xres;
  433.         }
  434.         y += yoffset_per_page;
  435.         buffer += pixels_per_page;
  436.     }
  437.  
  438.     return 0;
  439. }
  440.  
  441. static void ssd1289_pages_free(struct ssd1289 *item)
  442. {
  443.     dev_dbg(item->dev, "%s: item=0x%p\n", __func__, (void *)item);
  444.  
  445.     kfree(item->pages);
  446. }
  447.  
  448. static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
  449. {
  450.     return ((val<<width) + 0x7FFF - val)>>16;
  451. }
  452.  
  453. //This routine is needed because the console driver won't work without it.
  454. static int ssd1289_setcolreg(unsigned regno,
  455.                    unsigned red, unsigned green, unsigned blue,
  456.                    unsigned transp, struct fb_info *info)
  457. {
  458.     int ret = 1;
  459.  
  460.     /*
  461.      * If greyscale is true, then we convert the RGB value
  462.      * to greyscale no matter what visual we are using.
  463.      */
  464.     if (info->var.grayscale)
  465.         red = green = blue = (19595 * red + 38470 * green +
  466.                       7471 * blue) >> 16;
  467.     switch (info->fix.visual) {
  468.     case FB_VISUAL_TRUECOLOR:
  469.         if (regno < 16) {
  470.             u32 *pal = info->pseudo_palette;
  471.             u32 value;
  472.  
  473.             red = CNVT_TOHW(red, info->var.red.length);
  474.             green = CNVT_TOHW(green, info->var.green.length);
  475.             blue = CNVT_TOHW(blue, info->var.blue.length);
  476.             transp = CNVT_TOHW(transp, info->var.transp.length);
  477.  
  478.             value = (red << info->var.red.offset) |
  479.                 (green << info->var.green.offset) |
  480.                 (blue << info->var.blue.offset) |
  481.                 (transp << info->var.transp.offset);
  482.  
  483.             pal[regno] = value;
  484.             ret = 0;
  485.         }
  486.         break;
  487.     case FB_VISUAL_STATIC_PSEUDOCOLOR:
  488.     case FB_VISUAL_PSEUDOCOLOR:
  489.         break;
  490.     }
  491.     return ret;
  492. }
  493.  
  494. static int ssd1289_blank(int blank_mode, struct fb_info *info)
  495. {
  496.     struct ssd1289 *item = (struct ssd1289 *)info->par;
  497.     if (blank_mode == FB_BLANK_UNBLANK)
  498.         item->backlight=1;
  499.     else
  500.         item->backlight=0;
  501.     //Item->backlight won't take effect until the LCD is written to. Force that
  502.     //by dirty'ing a page.
  503.     item->pages[0].must_update=1;
  504.     schedule_delayed_work(&info->deferred_work, 0);
  505.     return 0;
  506. }
  507.  
  508. static void ssd1289_touch(struct fb_info *info, int x, int y, int w, int h)
  509. {
  510.     struct fb_deferred_io *fbdefio = info->fbdefio;
  511.     struct ssd1289 *item = (struct ssd1289 *)info->par;
  512.     int i, ystart, yend;
  513.     if (fbdefio) {
  514.         //Touch the pages the y-range hits, so the deferred io will update them.
  515.         for (i=0; i<item->pages_count; i++) {
  516.             ystart=item->pages[i].y;
  517.             yend=item->pages[i].y+(item->pages[i].len/info->fix.line_length)+1;
  518.             if (!((y+h)<ystart || y>yend)) {
  519.                 item->pages[i].must_update=1;
  520.             }
  521.         }
  522.         //Schedule the deferred IO to kick in after a delay.
  523.         schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  524.     }
  525. }
  526.  
  527. static void ssd1289_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
  528. {
  529.     sys_fillrect(p, rect);
  530.     ssd1289_touch(p, rect->dx, rect->dy, rect->width, rect->height);
  531. }
  532.  
  533. static void ssd1289_imageblit(struct fb_info *p, const struct fb_image *image)
  534. {
  535.     sys_imageblit(p, image);
  536.     ssd1289_touch(p, image->dx, image->dy, image->width, image->height);
  537. }
  538.  
  539. static void ssd1289_copyarea(struct fb_info *p, const struct fb_copyarea *area)
  540. {
  541.     sys_copyarea(p, area);
  542.     ssd1289_touch(p, area->dx, area->dy, area->width, area->height);
  543. }
  544.  
  545. static ssize_t ssd1289_write(struct fb_info *p, const char __user *buf,
  546.                 size_t count, loff_t *ppos)
  547. {
  548.     ssize_t res;
  549.     res = fb_sys_write(p, buf, count, ppos);
  550.     ssd1289_touch(p, 0, 0, p->var.xres, p->var.yres);
  551.     return res;
  552. }
  553.  
  554. static struct fb_ops ssd1289_fbops = {
  555.     .owner        = THIS_MODULE,
  556.     .fb_read      = fb_sys_read,
  557.     .fb_write     = ssd1289_write,
  558.     .fb_fillrect  = ssd1289_fillrect,
  559.     .fb_copyarea  = ssd1289_copyarea,
  560.     .fb_imageblit = ssd1289_imageblit,
  561.     .fb_setcolreg   = ssd1289_setcolreg,
  562.     .fb_blank   = ssd1289_blank,
  563. };
  564.  
  565. static struct fb_fix_screeninfo ssd1289_fix __initdata = {
  566.     .id          = "SSD1289",
  567.     .type        = FB_TYPE_PACKED_PIXELS,
  568.     .visual      = FB_VISUAL_TRUECOLOR,
  569.     .accel       = FB_ACCEL_NONE,
  570.     .line_length = 320 * 2,
  571. };
  572.  
  573. static struct fb_var_screeninfo ssd1289_var __initdata = {
  574.     .xres       = 320,
  575.     .yres       = 240,
  576.     .xres_virtual   = 320,
  577.     .yres_virtual   = 240,
  578.     .width      = 320,
  579.     .height     = 240,
  580.     .bits_per_pixel = 16,
  581.     .red        = {11, 5, 0},
  582.     .green      = {5, 6, 0},
  583.     .blue       = {0, 5, 0},
  584.     .activate   = FB_ACTIVATE_NOW,
  585.     .vmode      = FB_VMODE_NONINTERLACED,
  586. };
  587.  
  588. static struct fb_deferred_io ssd1289_defio = {
  589.         .delay          = HZ / 20,
  590.         .deferred_io    = &ssd1289_update,
  591. };
  592.  
  593. #if defined(CONFIG_SSD1289_DIRECTIO_MODE)
  594. static int __devinit ssd1289_probe(struct platform_device *dev)
  595. #elif defined(CONFIG_SSD1289_SPI_MODE)
  596. static int __devinit ssd1289_probe(struct spi_device *dev)
  597. #endif
  598. {
  599.     int ret = 0;
  600.     struct ssd1289 *item;
  601. #if defined(CONFIG_SSD1289_DIRECTIO_MODE)
  602.     struct resource *ctrl_res;
  603.     struct resource *data_res;
  604.     unsigned int ctrl_res_size;
  605.     unsigned int data_res_size;
  606.     struct resource *ctrl_req;
  607.     struct resource *data_req;
  608.     unsigned short signature;
  609. #endif
  610.     struct fb_info *info;
  611.  
  612.     dev_dbg(&dev->dev, "%s\n", __func__);
  613.  
  614.     item = kzalloc(sizeof(struct ssd1289), GFP_KERNEL);
  615.     if (!item) {
  616.         dev_err(&dev->dev,
  617.             "%s: unable to kzalloc for ssd1289\n", __func__);
  618.         ret = -ENOMEM;
  619.         goto out;
  620.     }
  621.     item->dev = &dev->dev;
  622.     dev_set_drvdata(&dev->dev, item);
  623.     item->backlight=1;
  624.  
  625. #if defined(CONFIG_SSD1289_DIRECTIO_MODE)
  626.     ctrl_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  627.     if (!ctrl_res) {
  628.         dev_err(&dev->dev,
  629.             "%s: unable to platform_get_resource for ctrl_res\n",
  630.             __func__);
  631.         ret = -ENOENT;
  632.         goto out_item;
  633.     }
  634.     ctrl_res_size = ctrl_res->end - ctrl_res->start + 1;
  635.     ctrl_req = request_mem_region(ctrl_res->start, ctrl_res_size,
  636.                       dev->name);
  637.     if (!ctrl_req) {
  638.         dev_err(&dev->dev,
  639.             "%s: unable to request_mem_region for ctrl_req\n",
  640.             __func__);
  641.         ret = -EIO;
  642.         goto out_item;
  643.     }
  644.     item->ctrl_io = ioremap(ctrl_res->start, ctrl_res_size);
  645.     if (!item->ctrl_io) {
  646.         ret = -EINVAL;
  647.         dev_err(&dev->dev,
  648.             "%s: unable to ioremap for ctrl_io\n", __func__);
  649.         goto out_item;
  650.     }
  651.  
  652.     data_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
  653.     if (!data_res) {
  654.         dev_err(&dev->dev,
  655.             "%s: unable to platform_get_resource for data_res\n",
  656.             __func__);
  657.         ret = -ENOENT;
  658.         goto out_item;
  659.     }
  660.     data_res_size = data_res->end - data_res->start + 1;
  661.     data_req = request_mem_region(data_res->start,
  662.                       data_res_size, dev->name);
  663.     if (!data_req) {
  664.         dev_err(&dev->dev,
  665.             "%s: unable to request_mem_region for data_req\n",
  666.             __func__);
  667.         ret = -EIO;
  668.         goto out_item;
  669.     }
  670.     item->data_io = ioremap(data_res->start, data_res_size);
  671.     if (!item->data_io) {
  672.         ret = -EINVAL;
  673.         dev_err(&dev->dev,
  674.             "%s: unable to ioremap for data_io\n", __func__);
  675.         goto out_item;
  676.     }
  677.  
  678.     dev_dbg(&dev->dev, "%s: ctrl_io=%p data_io=%p\n",
  679.         __func__, item->ctrl_io, item->data_io);
  680.  
  681.     signature = readw(item->ctrl_io);
  682.     dev_dbg(&dev->dev, "%s: signature=0x%04x\n", __func__, signature);
  683.     if (signature != 0x8989) {
  684.         ret = -ENODEV;
  685.         dev_err(&dev->dev,
  686.             "%s: unknown signature 0x%04x\n", __func__, signature);
  687.         goto out_item;
  688.     }
  689.  
  690.     dev_info(&dev->dev, "item=0x%p ctrl=0x%p data=0x%p\n", (void *)item,
  691.          (void *)ctrl_res->start, (void *)data_res->start);
  692. #elif defined(CONFIG_SSD1289_SPI_MODE)
  693.     item->spidev=dev;
  694.     item->dev=&dev->dev;
  695.     dev_set_drvdata(&dev->dev, item);
  696.     dev_info(&dev->dev, "spi registered, item=0x%p\n", (void *)item);
  697. #endif
  698.  
  699.     info = framebuffer_alloc(sizeof(struct ssd1289), &dev->dev);
  700.     if (!info) {
  701.         ret = -ENOMEM;
  702.         dev_err(&dev->dev,
  703.             "%s: unable to framebuffer_alloc\n", __func__);
  704.         goto out_item;
  705.     }
  706.     info->pseudo_palette = &item->pseudo_palette;
  707.     item->info = info;
  708.     info->par = item;
  709.     info->dev = &dev->dev;
  710.     info->fbops = &ssd1289_fbops;
  711.     info->flags = FBINFO_FLAG_DEFAULT|FBINFO_VIRTFB;
  712.     info->fix = ssd1289_fix;
  713.     info->var = ssd1289_var;
  714.  
  715.     ret = ssd1289_video_alloc(item);
  716.     if (ret) {
  717.         dev_err(&dev->dev,
  718.             "%s: unable to ssd1289_video_alloc\n", __func__);
  719.         goto out_info;
  720.     }
  721.     info->screen_base = (char __iomem *)item->info->fix.smem_start;
  722.  
  723.     ret = ssd1289_pages_alloc(item);
  724.     if (ret < 0) {
  725.         dev_err(&dev->dev,
  726.             "%s: unable to ssd1289_pages_init\n", __func__);
  727.         goto out_video;
  728.     }
  729.  
  730.     info->fbdefio = &ssd1289_defio;
  731.     fb_deferred_io_init(info);
  732.  
  733.     ret = register_framebuffer(info);
  734.     if (ret < 0) {
  735.         dev_err(&dev->dev,
  736.             "%s: unable to register_frambuffer\n", __func__);
  737.         goto out_pages;
  738.     }
  739.  
  740.  
  741.     ssd1289_setup(item);
  742.     ssd1289_update_all(item);
  743.  
  744.     return ret;
  745.  
  746. out_pages:
  747.     ssd1289_pages_free(item);
  748. out_video:
  749.     ssd1289_video_free(item);
  750. out_info:
  751.     framebuffer_release(info);
  752. out_item:
  753.     kfree(item);
  754. out:
  755.     return ret;
  756. }
  757.  
  758. #if defined(CONFIG_SSD1289_DIRECTIO_MODE)
  759. static int __devexit ssd1289_remove(struct platform_device *dev)
  760. #elif defined(CONFIG_SSD1289_SPI_MODE)
  761. static int __devexit ssd1289_remove(struct spi_device *dev)
  762. #endif
  763. {
  764.     struct ssd1289 *item = dev_get_drvdata(&dev->dev);
  765.     struct fb_info *info;
  766.  
  767.     dev_dbg(&dev->dev, "%s\n", __func__);
  768.  
  769.     dev_set_drvdata(&dev->dev, NULL);
  770.     if (item) {
  771.         info = item->info;
  772.         if (info)
  773.             unregister_framebuffer(info);
  774.         ssd1289_pages_free(item);
  775.         ssd1289_video_free(item);
  776.         kfree(item);
  777.         if (info)
  778.             framebuffer_release(info);
  779.     }
  780.     return 0;
  781. }
  782.  
  783. #ifdef CONFIG_PM
  784. static int ssd1289_suspend(struct spi_device *spi, pm_message_t state)
  785. {
  786.     struct fb_info *info = dev_get_drvdata(&spi->dev);
  787.     struct ssd1289 *item = (struct ssd1289 *)info->par;
  788.     /* enter into sleep mode */
  789.     ssd1289_reg_set(item, SSD1289_REG_SLEEP_MODE, 0x0001);
  790.     return 0;
  791. }
  792.  
  793. static int ssd1289_resume(struct spi_device *spi)
  794. {
  795.     struct fb_info *info = dev_get_drvdata(&spi->dev);
  796.     struct ssd1289 *item = (struct ssd1289 *)info->par;
  797.     /* leave sleep mode */
  798.     ssd1289_reg_set(item, SSD1289_REG_SLEEP_MODE, 0x0000);
  799.     return 0;
  800. }
  801. #else
  802. #define ssd1289_suspend NULL
  803. #define ssd1289_resume NULL
  804. #endif
  805.  
  806. #if defined(CONFIG_SSD1289_DIRECTIO_MODE)
  807. static struct platform_driver ssd1289_driver = {
  808.     .probe = ssd1289_probe,
  809.     .driver = {
  810.            .name = "ssd1289",
  811.            },
  812. };
  813. #elif defined(CONFIG_SSD1289_SPI_MODE)
  814. static struct spi_driver spi_ssd1289_driver = {
  815.     .driver = {
  816.         .name   = "spi-ssd1289",
  817.         .bus    = &spi_bus_type,
  818.         .owner  = THIS_MODULE,
  819.     },
  820.     .probe = ssd1289_probe,
  821.     .remove = ssd1289_remove,
  822.     .suspend = ssd1289_suspend,
  823.     .resume = ssd1289_resume,
  824. };
  825. #endif
  826.  
  827. static int __init ssd1289_init(void)
  828. {
  829.     int ret = 0;
  830.  
  831.     pr_debug("%s\n", __func__);
  832.  
  833. #if defined(CONFIG_SSD1289_DIRECTIO_MODE)
  834.     ret = platform_driver_register(&ssd1289_driver);
  835. #elif defined(CONFIG_SSD1289_SPI_MODE)
  836.     ret = spi_register_driver(&spi_ssd1289_driver);
  837. #endif
  838.     if (ret) {
  839.         pr_err("%s: unable to platform_driver_register\n", __func__);
  840.     }
  841.  
  842.     return ret;
  843. }
  844.  
  845. static void __exit ssd1289_exit(void)
  846. {
  847.     pr_debug("%s\n", __func__);
  848.  
  849. #if defined(CONFIG_SSD1289_DIRECTIO_MODE)
  850.     platform_driver_unregister(&ssd1289_driver);
  851. #elif defined(CONFIG_SSD1289_SPI_MODE)
  852.     spi_unregister_driver(&spi_ssd1289_driver);
  853. #endif
  854.  
  855. }
  856.  
  857. module_init(ssd1289_init);
  858. module_exit(ssd1289_exit);
  859.  
  860. MODULE_DESCRIPTION("SSD1289 LCD Driver");
  861. MODULE_AUTHOR("Jeroen Domburg <jeroen@spritesmods.com>");
  862. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement