Add driver for Himax HX8347 LCD controller.
[bertos.git] / bertos / drv / lcd_hx8347.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Himax HX8347 graphic driver
34  *
35  * \author Stefano Fedrigo <aleph@develer.com>
36  *
37  * Display initialization sequence is based on Atmel's softpack library
38  * implementation, see license below.
39  */
40
41 /* ----------------------------------------------------------------------------
42  *         ATMEL Microcontroller Software Support
43  * ----------------------------------------------------------------------------
44  * Copyright (c) 2010, Atmel Corporation
45  *
46  * All rights reserved.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions are met:
50  *
51  * - Redistributions of source code must retain the above copyright notice,
52  * this list of conditions and the disclaimer below.
53  *
54  * Atmel's name may not be used to endorse or promote products derived from
55  * this software without specific prior written permission.
56  *
57  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
59  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
60  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
62  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
63  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
64  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
65  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
66  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  * ----------------------------------------------------------------------------
68  */
69
70 #include "lcd_hx8347.h"
71
72 #include "hw/hw_hx8347.h"
73 #include <cfg/debug.h>
74 #include <drv/timer.h>
75
76 // Himax HX8347 chip id
77 #define HX8347_ID_HIMAX  0x47
78
79
80 static uint16_t lcd_row[LCD_WIDTH];
81
82
83 struct lcd_hx8347_reg
84 {
85         uint8_t cmd;    // Register index, if 0xFF wait for value ms
86         uint8_t data;   // Register value
87 };
88
89 static const struct lcd_hx8347_reg init_seq[] =
90 {
91         // Start internal OSC
92         { 0x19, 0x49 },   // OSCADJ=10 0000, OSD_EN=1 //60Hz
93         { 0x93, 0x0C },   // RADJ=1100
94
95         // Power on flow
96         { 0x44, 0x4D },   // VCM=100 1101
97         { 0x45, 0x11 },   // VDV=1 0001
98         { 0x20, 0x40 },   // BT=0100
99         { 0x1D, 0x07 },   // VC1=111
100         { 0x1E, 0x00 },   // VC3=000
101         { 0x1F, 0x04 },   // VRH=0100
102
103         { 0x1C, 0x04 },   // AP=100
104         { 0x1B, 0x10 },   // GASENB=0, PON=1, DK=0, XDK=0, DDVDH_TRI=0, STB=0
105         { 0xFF, 50   },   // 50 ms delay
106
107         { 0x43, 0x80 },   // Set VCOMG=1
108         { 0xFF, 50   },   // 50 ms delay
109
110 #if 0
111         // Gamma for CMO 2.8
112         { 0x46, 0x95 },
113         { 0x47, 0x51 },
114         { 0x48, 0x00 },
115         { 0x49, 0x36 },
116         { 0x4A, 0x11 },
117         { 0x4B, 0x66 },
118         { 0x4C, 0x14 },
119         { 0x4D, 0x77 },
120         { 0x4E, 0x13 },
121         { 0x4F, 0x4C },
122         { 0x50, 0x46 },
123         { 0x51, 0x46 },
124 #endif
125
126         // 240x320 window setting
127         { 0x02, 0x00 },   // Column address start2
128         { 0x03, 0x00 },   // Column address start1
129         { 0x04, 0x00 },   // Column address end2
130         { 0x05, 0xEF },   // Column address end1
131         { 0x06, 0x00 },   // Row address start2
132         { 0x07, 0x00 },   // Row address start1
133         { 0x08, 0x01 },   // Row address end2
134         { 0x09, 0x3F },   // Row address end1
135
136         // Display Setting
137         { 0x01, 0x06 },   // IDMON=0, INVON=1, NORON=1, PTLON=0
138 //      { 0x16, 0xC8 },   // MY=1, MX=1, MV=0, BGR=1
139         { 0x16, 0x68 },   // MY=0, MX=1, MV=1, RGB XY exchange X mirror
140         { 0x23, 0x95 },   // N_DC=1001 0101
141         { 0x24, 0x95 },   // P_DC=1001 0101
142         { 0x25, 0xFF },   // I_DC=1111 1111
143         { 0x27, 0x06 },   // N_BP=0000 0110
144         { 0x28, 0x06 },   // N_FP=0000 0110
145         { 0x29, 0x06 },   // P_BP=0000 0110
146         { 0x2A, 0x06 },   // P_FP=0000 0110
147         { 0x2C, 0x06 },   // I_BP=0000 0110
148         { 0x2D, 0x06 },   // I_FP=0000 0110
149         { 0x3A, 0x01 },   // N_RTN=0000, N_NW=001
150         { 0x3B, 0x01 },   // P_RTN=0000, P_NW=001
151         { 0x3C, 0xF0 },   // I_RTN=1111, I_NW=000
152         { 0x3D, 0x00 },   // DIV=00
153         { 0x3E, 0x38 },   // SON=38h
154         { 0x40, 0x0F },   // GDON=0Fh
155         { 0x41, 0xF0 },   // GDOF=F0h
156 };
157
158 /*
159  * Write to an LCD register.
160  */
161 static void regWrite(uint8_t reg, uint16_t val)
162 {
163         hx8347_cmd(reg);
164         hx8347_write(val);
165 }
166
167 /*
168  * Read data from a LCD register.
169  */
170 static uint16_t regRead(uint8_t reg)
171 {
172         hx8347_cmd(reg);
173         return hx8347_read();
174 }
175
176 /*
177  * Write data in a buffer to the LCD controller.
178  */
179 static void bufferWrite(const uint16_t *buf, uint16_t size)
180 {
181         uint16_t i;
182         for (i = 0 ; i < size; ++i)
183                 hx8347_write(buf[i]);
184 }
185
186 static void lcd_startBlit(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
187 {
188         ASSERT((x + width) <= LCD_WIDTH);
189         ASSERT((y + height) <= LCD_HEIGHT);
190
191         regWrite(0x02, x >> 8);
192         regWrite(0x03, x & 0xff);
193         regWrite(0x06, y >> 8);
194         regWrite(0x07, y & 0xff);
195
196         regWrite(0x04, (x + width) >> 8);
197         regWrite(0x05, (x + width) & 0xff);
198         regWrite(0x08, (y + height) >> 8);
199         regWrite(0x09, (y + height) & 0xff);
200 }
201
202 /*
203  * Refresh a bitmap on screen
204  */
205 void lcd_hx8347_blitBitmap(const Bitmap *bm)
206 {
207         uint8_t mask;
208         int i, l, r;
209
210         lcd_startBlit(0, 0, bm->width, bm->height);
211
212         for (l = 0; l < bm->height / 8; l++)
213         {
214                 for (mask = 1; mask; mask <<= 1)
215                 {
216                         for (i = 0; i < bm->width; i++)
217                         {
218                                 if (bm->raster[l * bm->width + i] & mask)
219                                         lcd_row[i] = 0x0000;
220                                 else
221                                         lcd_row[i] = 0xFFFF;
222                         }
223                         hx8347_cmd(0x22);
224                         bufferWrite(lcd_row, bm->width);
225                 }
226         }
227
228         for (r = 0, mask = 1; r < bm->height % 8; r++, mask <<= 1)
229         {
230                 for (i = 0; i < bm->width; i++)
231                 {
232                         if (bm->raster[l * bm->width + i] & mask)
233                                 lcd_row[i] = 0x0000;
234                         else
235                                 lcd_row[i] = 0xFFFF;
236                 }
237                 hx8347_cmd(0x22);
238                 bufferWrite(lcd_row, bm->width);
239         }
240 }
241 #if 0
242 /*
243  * Blit a 24 bit color raw raster directly on screen
244  */
245 void lcd_hx8347_blitBitmap24(int x, int y, int width, int height, const char *bmp)
246 {
247         int l, r;
248
249         lcd_startBlit(x, y, width, height);
250
251         for (l = 0; l < height; l++)
252         {
253                 for (r = 0; r < width; r++)
254                 {
255                         lcd_row[r] =
256                                 (((uint16_t)bmp[1] << 11) & 0xE000) |
257                                 (((uint16_t)bmp[2] <<  5) & 0x1F00) |
258                                 (((uint16_t)bmp[0] <<  0) & 0x00F8) |
259                                 (((uint16_t)bmp[1] >>  5) & 0x0007);
260                         bmp += 3;
261                 }
262
263                 lcd_cmd(0x22);
264                 lcd_data(lcd_row, width);
265         }
266 }
267 #endif
268 /**
269  * Turn off display.
270  */
271 void lcd_hx8347_off(void)
272 {
273         regWrite(0x90, 0);  // SAP=0000 0000
274         regWrite(0x26, 0);  // GON=0, DTE=0, D=00
275 }
276
277 /**
278  * Turn on display.
279  */
280 void lcd_hx8347_on(void)
281 {
282         regWrite(0x90, 0x7F);  // SAP=0111 1111
283         regWrite(0x26, 0x04);  // GON=0, DTE=0, D=01
284         timer_delay(100);
285         regWrite(0x26, 0x24);  // GON=1, DTE=0, D=01
286         regWrite(0x26, 0x2C);  // GON=1, DTE=0, D=11
287         timer_delay(100);
288         regWrite(0x26, 0x3C);  // GON=1, DTE=1, D=11
289 }
290
291 /**
292  * Display initialization.
293  */
294 void lcd_hx8347_init(void)
295 {
296         unsigned i;
297         uint16_t chip_id;
298
299         hx8347_busInit();
300         lcd_hx8347_off();
301
302         // Check chip id
303         if ((chip_id = regRead(0x67)) != HX8347_ID_HIMAX)
304         {
305                 kprintf("HX8347 chip id read error or wrong id (0x%x), skipping initialization.\n", chip_id);
306                 return;
307         }
308
309         for (i = 0; i < countof(init_seq); i++)
310         {
311                 if (init_seq[i].cmd != 0xFF)
312                         regWrite(init_seq[i].cmd, init_seq[i].data);
313                 else
314                         timer_delay(init_seq[i].data);
315         }
316
317         lcd_hx8347_on();
318 }