1 /* 2 * Ben NanoNote LCD initialisation, based on uboot-xburst and xburst-tools. 3 * 4 * Copyright (C) 2001-2002 Wolfgang Denk, DENX Software Engineering, <wd@denx.de> 5 * Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #include "board-display.h" 22 23 #include "xburst_types.h" 24 #include "jzlcd.h" 25 #include "sdram.h" 26 #include "board.h" 27 28 extern vidinfo_t panel_info; 29 static unsigned long lcd_base; 30 31 static unsigned int get_line_length() 32 { 33 return ALIGN((panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8, sizeof(u32)); 34 } 35 36 static u32 *get_pixel32(unsigned short h, unsigned short v) 37 { 38 return (u32 *) (lcd_base + v * get_line_length()) + h; 39 } 40 41 static u16 *get_pixel16(unsigned short h, unsigned short v) 42 { 43 return (u16 *) (lcd_base + v * get_line_length()) + h; 44 } 45 46 static u8 *get_pixel8(unsigned short h, unsigned short v) 47 { 48 return (u8 *) (lcd_base + v * get_line_length()) + h; 49 } 50 51 static u8 *get_pixel4(unsigned short h, unsigned short v) 52 { 53 return (u8 *) (lcd_base + v * get_line_length()) + h / 2; 54 } 55 56 static inline unsigned short div(unsigned short num, unsigned short denom, unsigned short scale) 57 { 58 return (scale * num) / denom; 59 } 60 61 static u32 pixel(u8 r, u8 g, u8 b, u8 rmax, u8 gmax, u8 bmax, u8 rshift, u8 gshift, u8 bshift) 62 { 63 return (div(r, 255, rmax) << rshift) | (div(g, 255, gmax) << gshift) | (div(b, 255, bmax) << bshift); 64 } 65 66 u32 get_bitmap_value(unsigned short x, u32 value) 67 { 68 return (value >> (((panel_info.vl_col - 1 - x) * 32) / panel_info.vl_col)) % 2 ? 0xffffffff : 0; 69 } 70 71 static void get_colour(unsigned short h, unsigned short v, u8 rgb[], unsigned short pixel_type) 72 { 73 unsigned short v_max = panel_info.vl_row - 1; 74 unsigned short h_max = panel_info.vl_col - 1; 75 76 rgb[(pixel_type - 1) % 3] = div(h, h_max, 255); 77 rgb[pixel_type % 3] = div(v, v_max, 255); 78 rgb[(pixel_type + 1) % 3] = (rgb[0] + rgb[1]) / 2; 79 } 80 81 static void set_pixel32(unsigned short h, unsigned short v, u32 value) 82 { 83 u32 *pix = get_pixel32(h, v); 84 *pix = value; 85 } 86 87 static void set_pixel16_565(unsigned short h, unsigned short v, u32 value) 88 { 89 u16 *pix = get_pixel16(h, v); 90 *pix = (u16) value; 91 } 92 93 static void set_pixel8(unsigned short h, unsigned short v, u32 value) 94 { 95 u8 *pix = get_pixel8(h, v); 96 *pix = (u8) value; 97 } 98 99 static void set_pixel4(unsigned short h, unsigned short v, u32 value) 100 { 101 u8 *pix = get_pixel4(h, v); 102 u8 mask = h & 1 ? 0xf0 : 0x0f; 103 *pix = (*pix & mask) | ((u8) value); 104 } 105 106 void set_pixel(unsigned short h, unsigned short v, u32 value) 107 { 108 switch (panel_info.vl_bpix) 109 { 110 case LCD_COLOR32: 111 set_pixel32(h, v, value); 112 break; 113 114 case LCD_COLOR8: 115 set_pixel8(h, v, value); 116 break; 117 118 case LCD_COLOR4: 119 set_pixel4(h, v, value); 120 break; 121 122 case LCD_COLOR16: 123 default: 124 set_pixel16_565(h, v, value); 125 break; 126 } 127 } 128 static void test_pixel32(unsigned short h, unsigned short v, unsigned short pixel_type) 129 { 130 u8 rgb[3]; 131 132 get_colour(h, v, rgb, pixel_type); 133 set_pixel32(h, v, pixel(rgb[0], rgb[1], rgb[2], 255, 255, 255, 16, 8, 0) | get_bitmap_value(h, pixel_type)); 134 } 135 136 static void test_pixel16_565(unsigned short h, unsigned short v, unsigned short pixel_type) 137 { 138 u8 rgb[3]; 139 140 get_colour(h, v, rgb, pixel_type); 141 set_pixel16_565(h, v, pixel(rgb[0], rgb[1], rgb[2], 31, 63, 31, 11, 5, 0) | get_bitmap_value(h, pixel_type)); 142 } 143 144 static void test_pixel8(unsigned short h, unsigned short v, unsigned short pixel_type) 145 { 146 u8 rgb[3]; 147 148 get_colour(h, v, rgb, pixel_type); 149 set_pixel8(h, v, pixel(rgb[0], rgb[1], rgb[2], 7, 7, 3, 5, 2, 0) | get_bitmap_value(h, pixel_type)); 150 } 151 152 static void test_pixel4(unsigned short h, unsigned short v, unsigned short pixel_type) 153 { 154 u8 rgb[3]; 155 156 get_colour(h, v, rgb, pixel_type); 157 set_pixel4(h, v, pixel(rgb[0], rgb[1], rgb[2], 1, 2, 1, 3, 1, 0) | get_bitmap_value(h, pixel_type)); 158 } 159 160 void test_pixel(unsigned short h, unsigned short v, unsigned short pixel_type) 161 { 162 switch (panel_info.vl_bpix) 163 { 164 case LCD_COLOR32: 165 test_pixel32(h, v, pixel_type); 166 break; 167 168 case LCD_COLOR8: 169 test_pixel8(h, v, pixel_type); 170 break; 171 172 case LCD_COLOR4: 173 test_pixel4(h, v, pixel_type); 174 break; 175 176 case LCD_COLOR16: 177 default: 178 test_pixel16_565(h, v, pixel_type); 179 break; 180 } 181 } 182 183 void clear_pixel32(unsigned short h, unsigned short v) 184 { 185 u32 *pix = get_pixel32(h, v); 186 *pix = 0; 187 } 188 189 void clear_pixel16(unsigned short h, unsigned short v) 190 { 191 u16 *pix = get_pixel16(h, v); 192 *pix = 0; 193 } 194 195 void clear_pixel8(unsigned short h, unsigned short v) 196 { 197 u8 *pix = get_pixel8(h, v); 198 *pix = 0; 199 } 200 201 void clear_pixel4(unsigned short h, unsigned short v) 202 { 203 u8 *pix = get_pixel4(h, v); 204 u8 mask = h & 1 ? 0xf0 : 0x0f; 205 *pix = *pix & mask; 206 } 207 208 void clear_pixel(unsigned short h, unsigned short v) 209 { 210 switch (panel_info.vl_bpix) 211 { 212 case LCD_COLOR32: 213 clear_pixel32(h, v); 214 break; 215 216 case LCD_COLOR8: 217 clear_pixel8(h, v); 218 break; 219 220 case LCD_COLOR4: 221 clear_pixel4(h, v); 222 break; 223 224 case LCD_COLOR16: 225 default: 226 clear_pixel16(h, v); 227 break; 228 } 229 } 230 231 void test_pattern() 232 { 233 unsigned short v_max = panel_info.vl_row; 234 unsigned short h_max = panel_info.vl_col; 235 unsigned short v, h; 236 237 for (v = 0; v < v_max; v += 1) { 238 for (h = 0; h < h_max; h += 1) { 239 test_pixel(h, v, 1); 240 } 241 } 242 } 243 244 void lcd_clear(unsigned long lcd_base) 245 { 246 unsigned short v_max = panel_info.vl_row; 247 unsigned short h_max = panel_info.vl_col; 248 unsigned short v, h; 249 unsigned long *pix = (unsigned long *)lcd_base; 250 251 for (v = 0; v < v_max; v += 1) { 252 for (h = 0; h < h_max; h += 1) { 253 *pix++ = 0; 254 } 255 } 256 } 257 258 /* LCD initialisation. */ 259 260 void lcd_init() 261 { 262 __lcd_display_pin_init(); 263 __lcd_display_on(); 264 265 lcd_base = lcd_ctrl_init(); 266 lcd_clear(lcd_base); 267 lcd_enable(); 268 }