fbcon.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2008, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  27. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <debug.h>
  31. #include <err.h>
  32. #include <stdlib.h>
  33. #include <dev/fbcon.h>
  34. #include <splash.h>
  35. #include <platform.h>
  36. #include <string.h>
  37. #include "font5x12.h"
  38. struct pos {
  39. int x;
  40. int y;
  41. };
  42. static struct fbcon_config *config = NULL;
  43. #define RGB565_BLACK 0x0000
  44. #define RGB565_WHITE 0xffff
  45. #define RGB888_BLACK 0x000000
  46. #define RGB888_WHITE 0xffffff
  47. #define FONT_WIDTH 5
  48. #define FONT_HEIGHT 12
  49. static uint16_t BGCOLOR;
  50. static uint16_t FGCOLOR;
  51. static struct pos cur_pos;
  52. static struct pos max_pos;
  53. static void fbcon_drawglyph(uint16_t *pixels, uint16_t paint, unsigned stride,
  54. unsigned *glyph)
  55. {
  56. unsigned x, y, data;
  57. stride -= FONT_WIDTH;
  58. data = glyph[0];
  59. for (y = 0; y < (FONT_HEIGHT / 2); ++y) {
  60. for (x = 0; x < FONT_WIDTH; ++x) {
  61. if (data & 1)
  62. *pixels = paint;
  63. data >>= 1;
  64. pixels++;
  65. }
  66. pixels += stride;
  67. }
  68. data = glyph[1];
  69. for (y = 0; y < (FONT_HEIGHT / 2); y++) {
  70. for (x = 0; x < FONT_WIDTH; x++) {
  71. if (data & 1)
  72. *pixels = paint;
  73. data >>= 1;
  74. pixels++;
  75. }
  76. pixels += stride;
  77. }
  78. }
  79. static void fbcon_flush(void)
  80. {
  81. if (config->update_start)
  82. config->update_start();
  83. if (config->update_done)
  84. while (!config->update_done());
  85. }
  86. /* TODO: Take stride into account */
  87. static void fbcon_scroll_up(void)
  88. {
  89. unsigned short *dst = config->base;
  90. unsigned short *src = dst + (config->width * FONT_HEIGHT);
  91. unsigned count = config->width * (config->height - FONT_HEIGHT);
  92. while(count--) {
  93. *dst++ = *src++;
  94. }
  95. count = config->width * FONT_HEIGHT;
  96. while(count--) {
  97. *dst++ = BGCOLOR;
  98. }
  99. fbcon_flush();
  100. }
  101. /* TODO: take stride into account */
  102. void fbcon_clear(void)
  103. {
  104. unsigned count = config->width * config->height;
  105. memset(config->base, BGCOLOR, count * ((config->bpp) / 8));
  106. }
  107. static void fbcon_set_colors(unsigned bg, unsigned fg)
  108. {
  109. BGCOLOR = bg;
  110. FGCOLOR = fg;
  111. }
  112. void fbcon_putc(char c)
  113. {
  114. uint16_t *pixels;
  115. /* ignore anything that happens before fbcon is initialized */
  116. if (!config)
  117. return;
  118. if((unsigned char)c > 127)
  119. return;
  120. if((unsigned char)c < 32) {
  121. if(c == '\n')
  122. goto newline;
  123. else if (c == '\r')
  124. cur_pos.x = 0;
  125. return;
  126. }
  127. pixels = config->base;
  128. pixels += cur_pos.y * FONT_HEIGHT * config->width;
  129. pixels += cur_pos.x * (FONT_WIDTH + 1);
  130. fbcon_drawglyph(pixels, FGCOLOR, config->stride,
  131. font5x12 + (c - 32) * 2);
  132. cur_pos.x++;
  133. if (cur_pos.x < max_pos.x)
  134. return;
  135. newline:
  136. cur_pos.y++;
  137. cur_pos.x = 0;
  138. if(cur_pos.y >= max_pos.y) {
  139. cur_pos.y = max_pos.y - 1;
  140. fbcon_scroll_up();
  141. } else
  142. fbcon_flush();
  143. }
  144. void fbcon_setup(struct fbcon_config *_config)
  145. {
  146. uint32_t bg;
  147. uint32_t fg;
  148. ASSERT(_config);
  149. config = _config;
  150. switch (config->format) {
  151. case FB_FORMAT_RGB565:
  152. fg = RGB565_WHITE;
  153. bg = RGB565_BLACK;
  154. break;
  155. case FB_FORMAT_RGB888:
  156. fg = RGB888_WHITE;
  157. bg = RGB888_BLACK;
  158. break;
  159. default:
  160. dprintf(CRITICAL, "unknown framebuffer pixel format\n");
  161. ASSERT(0);
  162. break;
  163. }
  164. fbcon_set_colors(bg, fg);
  165. cur_pos.x = 0;
  166. cur_pos.y = 0;
  167. max_pos.x = config->width / (FONT_WIDTH+1);
  168. max_pos.y = (config->height - 1) / FONT_HEIGHT;
  169. #if !DISPLAY_SPLASH_SCREEN
  170. fbcon_clear();
  171. #endif
  172. }
  173. struct fbcon_config* fbcon_display(void)
  174. {
  175. return config;
  176. }
  177. void display_image_on_screen(void)
  178. {
  179. unsigned i = 0;
  180. unsigned total_x;
  181. unsigned total_y;
  182. unsigned bytes_per_bpp;
  183. unsigned image_base;
  184. if (!config) {
  185. dprintf(CRITICAL,"NULL configuration, image cannot be displayed\n");
  186. return;
  187. }
  188. fbcon_clear();
  189. total_x = config->width;
  190. total_y = config->height;
  191. bytes_per_bpp = ((config->bpp) / 8);
  192. image_base = ((((total_y/2) - (SPLASH_IMAGE_WIDTH / 2) - 1) *
  193. (config->width)) + (total_x/2 - (SPLASH_IMAGE_HEIGHT / 2)));
  194. #if DISPLAY_TYPE_MIPI
  195. if (bytes_per_bpp == 3)
  196. {
  197. for (i = 0; i < SPLASH_IMAGE_WIDTH; i++)
  198. {
  199. memcpy (config->base + ((image_base + (i * (config->width))) * bytes_per_bpp),
  200. imageBuffer_rgb888 + (i * SPLASH_IMAGE_HEIGHT * bytes_per_bpp),
  201. SPLASH_IMAGE_HEIGHT * bytes_per_bpp);
  202. }
  203. }
  204. fbcon_flush();
  205. #if DISPLAY_MIPI_PANEL_NOVATEK_BLUE
  206. if(is_cmd_mode_enabled())
  207. mipi_dsi_cmd_mode_trigger();
  208. #endif
  209. #else
  210. if (bytes_per_bpp == 2)
  211. {
  212. for (i = 0; i < SPLASH_IMAGE_WIDTH; i++)
  213. {
  214. memcpy (config->base + ((image_base + (i * (config->width))) * bytes_per_bpp),
  215. imageBuffer + (i * SPLASH_IMAGE_HEIGHT * bytes_per_bpp),
  216. SPLASH_IMAGE_HEIGHT * bytes_per_bpp);
  217. }
  218. }
  219. fbcon_flush();
  220. #endif
  221. }