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, 2017 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 "jzlcd.h" 24 #include "sdram.h" 25 #include "board.h" 26 27 extern vidinfo_t panel_info; 28 static unsigned long lcd_base; 29 30 static unsigned int get_line_length() 31 { 32 return ALIGN((panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8, sizeof(uint32_t)); 33 } 34 35 static uint32_t *get_pixel32(unsigned short h, unsigned short v) 36 { 37 return (uint32_t *) (lcd_base + v * get_line_length()) + h; 38 } 39 40 static uint16_t *get_pixel16(unsigned short h, unsigned short v) 41 { 42 return (uint16_t *) (lcd_base + v * get_line_length()) + h; 43 } 44 45 static uint8_t *get_pixel8(unsigned short h, unsigned short v) 46 { 47 return (uint8_t *) (lcd_base + v * get_line_length()) + h; 48 } 49 50 static uint8_t *get_pixel4(unsigned short h, unsigned short v) 51 { 52 return (uint8_t *) (lcd_base + v * get_line_length()) + h / 2; 53 } 54 55 static inline unsigned short div(unsigned short num, unsigned short denom, unsigned short scale) 56 { 57 return (scale * num) / denom; 58 } 59 60 static uint32_t pixel(uint8_t r, uint8_t g, uint8_t b, uint8_t rmax, uint8_t gmax, uint8_t bmax, uint8_t rshift, uint8_t gshift, uint8_t bshift) 61 { 62 return (div(r, 255, rmax) << rshift) | (div(g, 255, gmax) << gshift) | (div(b, 255, bmax) << bshift); 63 } 64 65 uint32_t get_bitmap_value(unsigned short x, uint32_t value) 66 { 67 return (value >> (((panel_info.vl_col - 1 - x) * 32) / panel_info.vl_col)) % 2 ? 0xffffffff : 0; 68 } 69 70 static void get_colour(unsigned short h, unsigned short v, uint8_t rgb[], unsigned short pixel_type) 71 { 72 unsigned short v_max = panel_info.vl_row - 1; 73 unsigned short h_max = panel_info.vl_col - 1; 74 75 rgb[(pixel_type - 1) % 3] = div(h, h_max, 255); 76 rgb[pixel_type % 3] = div(v, v_max, 255); 77 rgb[(pixel_type + 1) % 3] = (rgb[0] + rgb[1]) / 2; 78 } 79 80 static void set_pixel32(unsigned short h, unsigned short v, uint32_t value) 81 { 82 uint32_t *pix = get_pixel32(h, v); 83 *pix = value; 84 } 85 86 static void set_pixel16_565(unsigned short h, unsigned short v, uint32_t value) 87 { 88 uint16_t *pix = get_pixel16(h, v); 89 *pix = (uint16_t) value; 90 } 91 92 static void set_pixel8(unsigned short h, unsigned short v, uint32_t value) 93 { 94 uint8_t *pix = get_pixel8(h, v); 95 *pix = (uint8_t) value; 96 } 97 98 static void set_pixel4(unsigned short h, unsigned short v, uint32_t value) 99 { 100 uint8_t *pix = get_pixel4(h, v); 101 uint8_t mask = h & 1 ? 0xf0 : 0x0f; 102 *pix = (*pix & mask) | ((uint8_t) value); 103 } 104 105 void set_pixel(unsigned short h, unsigned short v, uint32_t value) 106 { 107 switch (panel_info.vl_bpix) 108 { 109 case LCD_COLOR32: 110 set_pixel32(h, v, value); 111 break; 112 113 case LCD_COLOR8: 114 set_pixel8(h, v, value); 115 break; 116 117 case LCD_COLOR4: 118 set_pixel4(h, v, value); 119 break; 120 121 case LCD_COLOR16: 122 default: 123 set_pixel16_565(h, v, value); 124 break; 125 } 126 } 127 static void test_pixel32(unsigned short h, unsigned short v, unsigned short pixel_type) 128 { 129 uint8_t rgb[3]; 130 131 get_colour(h, v, rgb, pixel_type); 132 set_pixel32(h, v, pixel(rgb[0], rgb[1], rgb[2], 255, 255, 255, 16, 8, 0) | get_bitmap_value(h, pixel_type)); 133 } 134 135 static void test_pixel16_565(unsigned short h, unsigned short v, unsigned short pixel_type) 136 { 137 uint8_t rgb[3]; 138 139 get_colour(h, v, rgb, pixel_type); 140 set_pixel16_565(h, v, pixel(rgb[0], rgb[1], rgb[2], 31, 63, 31, 11, 5, 0) | get_bitmap_value(h, pixel_type)); 141 } 142 143 static void test_pixel8(unsigned short h, unsigned short v, unsigned short pixel_type) 144 { 145 uint8_t rgb[3]; 146 147 get_colour(h, v, rgb, pixel_type); 148 set_pixel8(h, v, pixel(rgb[0], rgb[1], rgb[2], 7, 7, 3, 5, 2, 0) | get_bitmap_value(h, pixel_type)); 149 } 150 151 static void test_pixel4(unsigned short h, unsigned short v, unsigned short pixel_type) 152 { 153 uint8_t rgb[3]; 154 155 get_colour(h, v, rgb, pixel_type); 156 set_pixel4(h, v, pixel(rgb[0], rgb[1], rgb[2], 1, 2, 1, 3, 1, 0) | get_bitmap_value(h, pixel_type)); 157 } 158 159 void test_pixel(unsigned short h, unsigned short v, unsigned short pixel_type) 160 { 161 switch (panel_info.vl_bpix) 162 { 163 case LCD_COLOR32: 164 test_pixel32(h, v, pixel_type); 165 break; 166 167 case LCD_COLOR8: 168 test_pixel8(h, v, pixel_type); 169 break; 170 171 case LCD_COLOR4: 172 test_pixel4(h, v, pixel_type); 173 break; 174 175 case LCD_COLOR16: 176 default: 177 test_pixel16_565(h, v, pixel_type); 178 break; 179 } 180 } 181 182 void clear_pixel32(unsigned short h, unsigned short v) 183 { 184 uint32_t *pix = get_pixel32(h, v); 185 *pix = 0; 186 } 187 188 void clear_pixel16(unsigned short h, unsigned short v) 189 { 190 uint16_t *pix = get_pixel16(h, v); 191 *pix = 0; 192 } 193 194 void clear_pixel8(unsigned short h, unsigned short v) 195 { 196 uint8_t *pix = get_pixel8(h, v); 197 *pix = 0; 198 } 199 200 void clear_pixel4(unsigned short h, unsigned short v) 201 { 202 uint8_t *pix = get_pixel4(h, v); 203 uint8_t mask = h & 1 ? 0xf0 : 0x0f; 204 *pix = *pix & mask; 205 } 206 207 void clear_pixel(unsigned short h, unsigned short v) 208 { 209 switch (panel_info.vl_bpix) 210 { 211 case LCD_COLOR32: 212 clear_pixel32(h, v); 213 break; 214 215 case LCD_COLOR8: 216 clear_pixel8(h, v); 217 break; 218 219 case LCD_COLOR4: 220 clear_pixel4(h, v); 221 break; 222 223 case LCD_COLOR16: 224 default: 225 clear_pixel16(h, v); 226 break; 227 } 228 } 229 230 void test_pattern() 231 { 232 unsigned short v_max = panel_info.vl_row; 233 unsigned short h_max = panel_info.vl_col; 234 unsigned short v, h; 235 236 for (v = 0; v < v_max; v += 1) { 237 for (h = 0; h < h_max; h += 1) { 238 test_pixel(h, v, 1); 239 } 240 } 241 } 242 243 void lcd_clear(unsigned long lcd_base) 244 { 245 unsigned short v_max = panel_info.vl_row; 246 unsigned short h_max = panel_info.vl_col; 247 unsigned short v, h; 248 unsigned long *pix = (unsigned long *)lcd_base; 249 250 for (v = 0; v < v_max; v += 1) { 251 for (h = 0; h < h_max; h += 1) { 252 *pix++ = 0; 253 } 254 } 255 } 256 257 /* LCD initialisation. */ 258 259 void lcd_init() 260 { 261 __lcd_display_pin_init(); 262 __lcd_display_on(); 263 264 lcd_base = lcd_ctrl_init(); 265 lcd_clear(lcd_base); 266 lcd_enable(); 267 }