sama_lcdc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file SAMA5D2x/sama_lcdc.c
  15. * @brief SAMA LCDC support code.
  16. *
  17. * @addtogroup SAMA5D2x_LCDC
  18. * @{
  19. */
  20. #include "hal.h"
  21. #if (SAMA_USE_LCDC) || defined(__DOXYGEN__)
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. /*
  26. * @brief NO CACHE attribute
  27. */
  28. #if !defined(NO_CACHE)
  29. #define NO_CACHE __attribute__((section (".nocache")))
  30. #endif
  31. /*===========================================================================*/
  32. /* Driver local macros. */
  33. /*===========================================================================*/
  34. /*
  35. * @name Configuration Macros
  36. * @{
  37. */
  38. /*
  39. * @brief Transfer Descriptor Fetch Enable
  40. */
  41. #define LCDC_CTRL_DFETCH (0x1u << 0)
  42. /*
  43. * @brief Channel Enable Register
  44. */
  45. #define LCDC_CHER_CHEN (0x1u << 0)
  46. /*
  47. * @brief Channel Disable Register
  48. */
  49. #define LCDC_CHDR_CHDIS (0x1u << 0)
  50. /*
  51. * @brief Update Overlay Attributes Enable Register
  52. */
  53. #define LCDC_CHER_UPDATEEN (0x1u << 1)
  54. /*
  55. * @brief Blender DMA Layer Enable
  56. */
  57. #define LCDC_CFG_DMA (0x1u << 8)
  58. /*
  59. * @brief Blender Overlay Layer Enable
  60. */
  61. #define LCDC_CFG_OVR (0x1u << 7)
  62. /*
  63. * @brief Pixel Stride
  64. */
  65. #define LCDC_CFG_PSTRIDE_Pos 0
  66. #define LCDC_CFG_PSTRIDE_Msk (0xffffffffu << LCDC_CFG_PSTRIDE_Pos)
  67. #define LCDC_CFG_PSTRIDE(value) ((LCDC_CFG_PSTRIDE_Msk & ((value) << \
  68. LCDC_CFG_PSTRIDE_Pos)))
  69. /*
  70. * @brief Horizontal Stride
  71. */
  72. #define LCDC_CFG_XSTRIDE_Pos 0
  73. #define LCDC_CFG_XSTRIDE_Msk (0xffffffffu << LCDC_CFG_XSTRIDE_Pos)
  74. #define LCDC_CFG_XSTRIDE(value) ((LCDC_CFG_XSTRIDE_Msk & ((value) << \
  75. LCDC_CFG_XSTRIDE_Pos)))
  76. /*
  77. * @brief Hardware Rotation Optimization Disable
  78. */
  79. #define LCDC_CFG_ROTDIS (0x1u << 12)
  80. /*
  81. * @brief Horizontal Window Position
  82. */
  83. #define LCDC_CFG_XPOS_Pos 0
  84. #define LCDC_CFG_XPOS_Msk (0x7ffu << LCDC_CFG_XPOS_Pos)
  85. #define LCDC_CFG_XPOS(value) ((LCDC_CFG_XPOS_Msk & ((value) << LCDC_CFG_XPOS_Pos)))
  86. /*
  87. * @brief Vertical Window Position
  88. */
  89. #define LCDC_CFG_YPOS_Pos 16
  90. #define LCDC_CFG_YPOS_Msk (0x7ffu << LCDC_CFG_YPOS_Pos)
  91. #define LCDC_CFG_YPOS(value) ((LCDC_CFG_YPOS_Msk & ((value) << LCDC_CFG_YPOS_Pos)))
  92. /*
  93. * @brief Horizontal Window Size
  94. */
  95. #define LCDC_CFG_XSIZE_Pos 0
  96. #define LCDC_CFG_XSIZE_Msk (0x7ffu << LCDC_CFG_XSIZE_Pos)
  97. #define LCDC_CFG_XSIZE(value) ((LCDC_CFG_XSIZE_Msk & ((value) << LCDC_CFG_XSIZE_Pos)))
  98. /*
  99. * @brief Vertical Window Size
  100. */
  101. #define LCDC_CFG_YSIZE_Pos 16
  102. #define LCDC_CFG_YSIZE_Msk (0x7ffu << LCDC_CFG_YSIZE_Pos)
  103. #define LCDC_CFG_YSIZE(value) ((LCDC_CFG_YSIZE_Msk & ((value) << LCDC_CFG_YSIZE_Pos)))
  104. /*
  105. * @brief Horizontal image Size in Memory
  106. */
  107. #define LCDC_CFG_XMEMSIZE_Pos 0
  108. #define LCDC_CFG_XMEMSIZE_Msk (0x7ffu << LCDC_CFG_XMEMSIZE_Pos)
  109. #define LCDC_CFG_XMEMSIZE(value) ((LCDC_CFG_XMEMSIZE_Msk & ((value) << LCDC_CFG_XMEMSIZE_Pos)))
  110. /*
  111. * @brief Vertical image Size in Memory
  112. */
  113. #define LCDC_CFG_YMEMSIZE_Pos 16
  114. #define LCDC_CFG_YMEMSIZE_Msk (0x7ffu << LCDC_CFG_YMEMSIZE_Pos)
  115. #define LCDC_CFG_YMEMSIZE(value) ((LCDC_CFG_YMEMSIZE_Msk & ((value) << LCDC_CFG_YMEMSIZE_Pos)))
  116. /** @} */
  117. /*===========================================================================*/
  118. /* Driver exported variables. */
  119. /*===========================================================================*/
  120. LCDCDriver LCDCD0;
  121. /*===========================================================================*/
  122. /* Driver local variables. */
  123. /*===========================================================================*/
  124. /**
  125. * @brief DMA Channel Descriptor.
  126. */
  127. typedef struct {
  128. /**
  129. * @brief Frame Buffer base address register.
  130. */
  131. uint32_t addr;
  132. /**
  133. * @brief Transfer Control register.
  134. */
  135. uint32_t ctrl;
  136. /**
  137. * @brief Next Descriptor Address register.
  138. */
  139. uint32_t next;
  140. } lcdc_dma_descriptor_t;
  141. /* Variable layer data */
  142. typedef struct {
  143. lcdc_dma_descriptor_t *dma_desc;
  144. lcdc_dma_descriptor_t *dma_u_desc;
  145. lcdc_dma_descriptor_t *dma_v_desc;
  146. void *buffer;
  147. uint8_t bpp;
  148. uint8_t num_colors;
  149. } layerdata_t;
  150. /*
  151. * @brief Hardware info about the layers
  152. */
  153. typedef struct {
  154. layerdata_t *data;
  155. bool stride_supported;
  156. /* regs: _ER, _DR, _SR, _IER, _IDR, _IMR, _ISR */
  157. volatile uint32_t *reg_enable;
  158. /* regs: blender */
  159. volatile uint32_t *reg_blender;
  160. /* _HEAD, _ADDRESS, _CONTROL, _NEXT */
  161. volatile uint32_t *reg_dma_head;
  162. /* _HEAD, _ADDRESS, _CONTROL, _NEXT */
  163. volatile uint32_t *reg_dma_u_head;
  164. /* _HEAD, _ADDRESS, _CONTROL, _NEXT */
  165. volatile uint32_t *reg_dma_v_head;
  166. /* regs: _CFG0, _CFG1 (RGB mode...) */
  167. volatile uint32_t *reg_cfg;
  168. /* X Y register, W H register */
  169. volatile uint32_t *reg_win;
  170. /* regs: stride */
  171. volatile uint32_t *reg_stride;
  172. /* regs: RGB Default, RGB Key, RGB Mask */
  173. volatile uint32_t *reg_color;
  174. /* regs: scale */
  175. volatile uint32_t *reg_scale;
  176. /* regs: CLUT */
  177. volatile uint32_t *reg_clut;
  178. } layerinfo_t;
  179. /* Base Layer */
  180. static layerdata_t lcdd_base;
  181. /* OVR1 Layer */
  182. static layerdata_t lcdd_ovr1;
  183. /* OVR2 Layer */
  184. static layerdata_t lcdd_ovr2;
  185. /* HEO Layer */
  186. static layerdata_t lcdd_heo;
  187. /* HCC Layer */
  188. static layerdata_t lcdd_hcc;
  189. /*
  190. * @brief DMA descriptor
  191. * @note The DMA Channel Descriptor (DSCR) must be aligned on a 64-bit boundary.
  192. */
  193. ALIGNED_VAR(8)
  194. /* DMA descriptor for Base Layer */
  195. NO_CACHE static lcdc_dma_descriptor_t base_dma_desc;
  196. ALIGNED_VAR(8)
  197. /* DMA descriptor for OVR1 Layer */
  198. NO_CACHE static lcdc_dma_descriptor_t ovr1_dma_desc;
  199. ALIGNED_VAR(8)
  200. /* DMA descriptor for OVR2 Layer */
  201. NO_CACHE static lcdc_dma_descriptor_t ovr2_dma_desc;
  202. ALIGNED_VAR(8)
  203. /* DMA descriptor for HEO Layer */
  204. NO_CACHE static lcdc_dma_descriptor_t heo_dma_desc;
  205. ALIGNED_VAR(8)
  206. /* DMA descriptor for HEO U-UV Layer */
  207. NO_CACHE static lcdc_dma_descriptor_t heo_dma_u_desc;
  208. ALIGNED_VAR(8)
  209. /* DMA descriptor for HEO V Layer */
  210. NO_CACHE static lcdc_dma_descriptor_t heo_dma_v_desc;
  211. ALIGNED_VAR(8)
  212. /* DMA descriptor for HCC Layer */
  213. NO_CACHE static lcdc_dma_descriptor_t hcc_dma_desc;
  214. /**
  215. * @brief Information about layers
  216. */
  217. static const layerinfo_t lcdd_layers[] = {
  218. /* 0: LCDD_CONTROLLER */
  219. {
  220. .stride_supported = false,
  221. .reg_enable = &LCDC->LCDC_LCDEN,
  222. },
  223. /* 1: LCDD_BASE */
  224. {
  225. .data = &lcdd_base,
  226. .stride_supported = false,
  227. .reg_enable = &LCDC->LCDC_BASECHER,
  228. .reg_blender = &LCDC->LCDC_BASECFG4,
  229. .reg_dma_head = &LCDC->LCDC_BASEHEAD,
  230. .reg_cfg = &LCDC->LCDC_BASECFG0,
  231. .reg_stride = &LCDC->LCDC_BASECFG2,
  232. .reg_color = &LCDC->LCDC_BASECFG3,
  233. .reg_clut = &LCDC->LCDC_BASECLUT[0]
  234. },
  235. /* 2: LCDD_OVR1 */
  236. {
  237. .data = &lcdd_ovr1,
  238. .stride_supported = true,
  239. .reg_enable = &LCDC->LCDC_OVR1CHER,
  240. .reg_blender = &LCDC->LCDC_OVR1CFG9,
  241. .reg_dma_head = &LCDC->LCDC_OVR1HEAD,
  242. .reg_cfg = &LCDC->LCDC_OVR1CFG0,
  243. .reg_win = &LCDC->LCDC_OVR1CFG2,
  244. .reg_stride = &LCDC->LCDC_OVR1CFG4,
  245. .reg_color = &LCDC->LCDC_OVR1CFG6,
  246. .reg_clut = &LCDC->LCDC_OVR1CLUT[0],
  247. },
  248. /* 3: LCDD_HEO */
  249. {
  250. .data = &lcdd_heo,
  251. .stride_supported = true,
  252. .reg_enable = &LCDC->LCDC_HEOCHER,
  253. .reg_blender = &LCDC->LCDC_HEOCFG12,
  254. .reg_dma_head = &LCDC->LCDC_HEOHEAD,
  255. .reg_dma_u_head = &LCDC->LCDC_HEOUHEAD,
  256. .reg_dma_v_head = &LCDC->LCDC_HEOVHEAD,
  257. .reg_cfg = &LCDC->LCDC_HEOCFG0,
  258. .reg_win = &LCDC->LCDC_HEOCFG2,
  259. .reg_stride = &LCDC->LCDC_HEOCFG5,
  260. .reg_color = &LCDC->LCDC_HEOCFG9,
  261. .reg_scale = &LCDC->LCDC_HEOCFG13,
  262. .reg_clut = &LCDC->LCDC_HEOCLUT[0],
  263. },
  264. /* 4: LCDD_OVR2 */
  265. {
  266. .data = &lcdd_ovr2,
  267. .stride_supported = true,
  268. .reg_enable = &LCDC->LCDC_OVR2CHER,
  269. .reg_blender = &LCDC->LCDC_OVR2CFG9,
  270. .reg_dma_head = &LCDC->LCDC_OVR2HEAD,
  271. .reg_cfg = &LCDC->LCDC_OVR2CFG0,
  272. .reg_win = &LCDC->LCDC_OVR2CFG2,
  273. .reg_stride = &LCDC->LCDC_OVR2CFG4,
  274. .reg_color = &LCDC->LCDC_OVR2CFG6,
  275. .reg_clut = &LCDC->LCDC_OVR2CLUT[0],
  276. }
  277. ,
  278. /* 5: N/A */
  279. {
  280. .data = NULL,
  281. },
  282. /* 6: LCDD_CUR */
  283. {
  284. .data = &lcdd_hcc,
  285. .stride_supported = false,
  286. },
  287. };
  288. /*===========================================================================*/
  289. /* Driver local functions. */
  290. /*===========================================================================*/
  291. /*
  292. * @brief Clear DMA channel descriptor
  293. *
  294. * @param[in] descp pointer to lcdc_dma_descriptor
  295. * @param[in] dma_regp pointer to LCDC leyer register
  296. *
  297. * @notapi
  298. */
  299. static void clear_dma_desc(lcdc_dma_descriptor_t *descp, volatile uint32_t *dma_regp) {
  300. /* Modify descriptor */
  301. if (descp) {
  302. descp->ctrl &= ~LCDC_CTRL_DFETCH;
  303. descp->next = (uint32_t)descp;
  304. //cacheCleanRegion(descp, sizeof(lcdc_dma_descriptor_t));
  305. }
  306. /* Modify registers */
  307. dma_regp[2] &= ~LCDC_CTRL_DFETCH;
  308. dma_regp[3] = (uint32_t)descp;
  309. }
  310. /**
  311. * @brief Computing scaling factor.
  312. *
  313. * @param[in] layerp pointer to a layerinfo_t struct
  314. * @param[out] xfactorp pointer to xfactor scaling factor
  315. * @param[out] yfactorp pointer to yfactor scaling factor
  316. *
  317. * @notapi
  318. */
  319. static void compute_scaling_factors(const layerinfo_t *layerp,
  320. uint16_t* xfactorp, uint16_t* yfactorp)
  321. {
  322. uint16_t xmemsize, ymemsize;
  323. uint16_t xsize, ysize;
  324. #ifdef LCDC_HEOCFG41_XPHIDEF
  325. uint16_t xfactor_1st, yfactor_1st;
  326. #endif
  327. xmemsize = (layerp->reg_win[2] & LCDC_CFG_XMEMSIZE_Msk) >> LCDC_CFG_XMEMSIZE_Pos;
  328. ymemsize = (layerp->reg_win[2] & LCDC_CFG_YMEMSIZE_Msk) >> LCDC_CFG_YMEMSIZE_Pos;
  329. xsize = (layerp->reg_win[1] & LCDC_CFG_XSIZE_Msk) >> LCDC_CFG_XSIZE_Pos;
  330. ysize = (layerp->reg_win[1] & LCDC_CFG_YSIZE_Msk) >> LCDC_CFG_YSIZE_Pos;
  331. #ifdef LCDC_HEOCFG41_XPHIDEF
  332. /* we assume that XPHIDEF & YPHIDEF are 0 */
  333. xfactor_1st = (2048 * xmemsize / xsize) + 1;
  334. yfactor_1st = (2048 * ymemsize / ysize) + 1;
  335. if ((xfactor_1st * xsize / 2048) > xmemsize)
  336. *xfactorp = xfactor_1st - 1;
  337. else
  338. *xfactorp = xfactor_1st;
  339. if ((yfactor_1st * ysize / 2048) > ymemsize)
  340. *yfactorp = yfactor_1st - 1;
  341. else
  342. *yfactorp = yfactor_1st;
  343. #else
  344. *xfactorp = 1024 * (xmemsize + 1) / (xsize + 1);
  345. *yfactorp = 1024 * (ymemsize + 1) / (ysize + 1);
  346. #endif
  347. }
  348. /**
  349. * @brief Configures LCDC layers according to configuration struct.
  350. *
  351. * @param[in] listp pointer to a LCDCLayerConfig array
  352. * @param[in] length length of array
  353. *
  354. * @notapi
  355. */
  356. void layer_config(LCDCLayerConfig *listp, size_t length) {
  357. uint8_t i;
  358. uint8_t bpp_bit;
  359. uint8_t bpp;
  360. uint32_t index;
  361. uint32_t padding = 0;
  362. uint32_t src_w, src_h, img_w, img_h;
  363. uint32_t bits_per_row, bytes_per_row;
  364. LCDCLayerConfig *layerp;
  365. for (i = 0; i < length; i++) {
  366. index = listp[i].layer_id;
  367. osalDbgAssert((index != LCDD_CONTROLLER) || (index != LCDD_CUR), "This is not a real layer");
  368. layerp = &listp[i];
  369. uint16_t w, h, x, y;
  370. bpp = layerp->bpp;
  371. w = layerp->width;
  372. h = layerp->height;
  373. x = layerp->x_pos;
  374. y = layerp->y_pos;
  375. img_w = layerp->w_img;
  376. img_h = layerp->h_img;
  377. /* Bpp settings */
  378. lcdd_layers[index].reg_cfg[1] = layerp->bpp;
  379. bpp = bpp >> 4;
  380. if (bpp == 1 || bpp < 5) {
  381. bpp_bit = 16;
  382. }
  383. else if (bpp == 5 || bpp == 6) {
  384. bpp_bit = 18;
  385. }
  386. else if (bpp == 7 || bpp == 8) {
  387. bpp_bit = 19;
  388. }
  389. else if (bpp == 9 || bpp == 10) {
  390. bpp_bit = 24;
  391. }
  392. else if (bpp == 11) {
  393. bpp_bit = 25;
  394. }
  395. else if (bpp == 12 || bpp == 13) {
  396. bpp_bit = 32;
  397. }
  398. else {
  399. bpp_bit = 12;
  400. }
  401. /* Set display buffer & mode */
  402. bits_per_row = img_w * bpp_bit;
  403. bytes_per_row = bits_per_row >> 3;
  404. if (bits_per_row & 0x7) {
  405. bytes_per_row++;
  406. }
  407. if (bytes_per_row & 0x3) {
  408. padding = 4 - (bytes_per_row & 0x3);
  409. }
  410. /* No rotation optimization */
  411. lcdd_layers[index].reg_cfg[0] |= LCDC_CFG_ROTDIS;
  412. /* Configure PSTRIDE if supported */
  413. if (lcdd_layers[index].stride_supported)
  414. lcdd_layers[index].reg_stride[1] = LCDC_CFG_PSTRIDE(0);
  415. /* Configure XSTRIDE if supported */
  416. lcdd_layers[index].reg_stride[0] = LCDC_CFG_XSTRIDE(padding);
  417. /* Set window & position */
  418. if (lcdd_layers[index].reg_win) {
  419. /* Re - calculate to eliminate hardware overflow */
  420. if (x + w > LCDCD0.config->width) {
  421. w = LCDCD0.config->width - x;
  422. }
  423. if (y + h > LCDCD0.config->height) {
  424. h = LCDCD0.config->height - y;
  425. }
  426. if (w == 0)
  427. w++;
  428. if (h == 0)
  429. h++;
  430. lcdd_layers[index].reg_win[0] = LCDC_CFG_XPOS(x) | LCDC_CFG_YPOS(y);
  431. lcdd_layers[index].reg_win[1] = LCDC_CFG_XSIZE(w - 1) | LCDC_CFG_YSIZE(h - 1);
  432. }
  433. /* Scaling setup, only HEO layer has scaling register */
  434. if (lcdd_layers[index].reg_win && lcdd_layers[index].reg_scale) {
  435. src_w = img_w;
  436. src_h = img_h;
  437. lcdd_layers[index].reg_win[2] = LCDC_CFG_XMEMSIZE(src_w - 1) |
  438. LCDC_CFG_YMEMSIZE(src_h - 1);
  439. /* Scaled */
  440. if (w != src_w || h != src_h) {
  441. uint16_t scale_w, scale_h;
  442. compute_scaling_factors(&lcdd_layers[index], &scale_w, &scale_h);
  443. lcdd_layers[index].reg_scale[0] = LCDC_HEOCFG13_YFACTOR(scale_h) |
  444. LCDC_HEOCFG13_XFACTOR(scale_w) |
  445. LCDC_HEOCFG13_SCALEN;
  446. }
  447. /* Disable scaling */
  448. else {
  449. lcdd_layers[index].reg_scale[0] = 0;
  450. }
  451. }
  452. /* Configure Descriptor */
  453. lcdd_layers[index].data->dma_desc->addr = (uint32_t)layerp->buffer;
  454. lcdd_layers[index].data->dma_desc->ctrl = LCDC_CTRL_DFETCH;
  455. lcdd_layers[index].data->dma_desc->next = (uint32_t)lcdd_layers[index].data->dma_desc;
  456. lcdd_layers[index].reg_dma_head[1] = (uint32_t)lcdd_layers[index].data->dma_desc->addr;
  457. lcdd_layers[index].reg_dma_head[2] = LCDC_CTRL_DFETCH;
  458. lcdd_layers[index].reg_dma_head[3] = (uint32_t)lcdd_layers[index].data->dma_desc;
  459. /* Configure layer */
  460. lcdd_layers[index].reg_enable[0] = LCDC_CHER_UPDATEEN;
  461. lcdd_layers[index].reg_blender[0] |= LCDC_CFG_DMA | LCDC_CFG_OVR;
  462. }
  463. }
  464. /**
  465. * @brief Enable Display.
  466. *
  467. * @param[in] lcdcp pointer to the @p LCDCDriver object
  468. */
  469. static void lcdc_on(LCDCDriver *lcdcp) {
  470. uint32_t pixel_clock = lcdcp->config->framerate;
  471. pixel_clock *= lcdcp->config->timing_hpw + lcdcp->config->timing_hbp +
  472. lcdcp->config->width + lcdcp->config->timing_hfp;
  473. pixel_clock *= lcdcp->config->timing_vpw + lcdcp->config->timing_vbp +
  474. lcdcp->config->height + lcdcp->config->timing_vfp;
  475. /* Wait for clock domain synchronization to be complete. */
  476. while ((LCDC->LCDC_LCDSR & LCDC_LCDSR_SIPSTS));
  477. /* Configure LCD timing parameters, signal polarity and clock period. */
  478. if( LCDC->LCDC_LCDCFG0 & LCDC_LCDCFG0_CLKSEL) {
  479. LCDC->LCDC_LCDCFG0 = LCDC_LCDCFG0_CLKDIV((SAMA_MCK * 2) / pixel_clock - 2) |
  480. LCDC_LCDCFG0_CGDISHEO | LCDC_LCDCFG0_CGDISOVR1 |
  481. LCDC_LCDCFG0_CGDISOVR2 | LCDC_LCDCFG0_CGDISBASE |
  482. LCDC_LCDCFG0_CLKPWMSEL | LCDC_LCDCFG0_CLKSEL;
  483. }
  484. else {
  485. LCDC->LCDC_LCDCFG0 = LCDC_LCDCFG0_CLKDIV(SAMA_MCK / pixel_clock - 2) |
  486. LCDC_LCDCFG0_CGDISBASE | LCDC_LCDCFG0_CGDISHEO |
  487. LCDC_LCDCFG0_CLKPWMSEL;
  488. }
  489. /* Wait for clock domain synchronization to be complete. */
  490. while ((LCDC->LCDC_LCDSR & LCDC_LCDSR_SIPSTS));
  491. LCDC->LCDC_LCDCFG1 = LCDC_LCDCFG1_VSPW(lcdcp->config->timing_vpw - 1) |
  492. LCDC_LCDCFG1_HSPW(lcdcp->config->timing_hpw - 1);
  493. /* Wait for clock domain synchronization to be complete. */
  494. while ((LCDC->LCDC_LCDSR & LCDC_LCDSR_SIPSTS));
  495. LCDC->LCDC_LCDCFG2 = LCDC_LCDCFG2_VBPW(lcdcp->config->timing_vbp) |
  496. LCDC_LCDCFG2_VFPW(lcdcp->config->timing_vfp - 1);
  497. /* Wait for clock domain synchronization to be complete. */
  498. while ((LCDC->LCDC_LCDSR & LCDC_LCDSR_SIPSTS));
  499. LCDC->LCDC_LCDCFG3 = LCDC_LCDCFG3_HBPW(lcdcp->config->timing_hbp - 1) |
  500. LCDC_LCDCFG3_HFPW(lcdcp->config->timing_hfp - 1);
  501. /* Wait for clock domain synchronization to be complete. */
  502. while ((LCDC->LCDC_LCDSR & LCDC_LCDSR_SIPSTS));
  503. LCDC->LCDC_LCDCFG4 = LCDC_LCDCFG4_RPF(lcdcp->config->height - 1) |
  504. LCDC_LCDCFG4_PPL(lcdcp->config->width - 1);
  505. /* Wait for clock domain synchronization to be complete. */
  506. while ((LCDC->LCDC_LCDSR & LCDC_LCDSR_SIPSTS));
  507. LCDC->LCDC_LCDCFG5 = LCDC_LCDCFG5_GUARDTIME(30) | LCDC_LCDCFG5_MODE_OUTPUT_24BPP |
  508. LCDC_LCDCFG5_DISPDLY | LCDC_LCDCFG5_VSPDLYS | LCDC_LCDCFG5_VSPOL |
  509. LCDC_LCDCFG5_HSPOL;
  510. /* Wait for clock domain synchronization to be complete. */
  511. while ((LCDC->LCDC_LCDSR & LCDC_LCDSR_SIPSTS));
  512. LCDC->LCDC_LCDCFG6 = LCDC_LCDCFG6_PWMCVAL(0xF0) | LCDC_LCDCFG6_PWMPOL |
  513. LCDC_LCDCFG6_PWMPS(6);
  514. /* Wait for clock domain synchronization to be complete. */
  515. while ((LCDC->LCDC_LCDSR & LCDC_LCDSR_SIPSTS));
  516. /* Enable the Pixel Clock. */
  517. LCDC->LCDC_LCDEN = LCDC_LCDEN_CLKEN;
  518. /* Poll to check that clock is running. */
  519. while (!(LCDC->LCDC_LCDSR & LCDC_LCDSR_CLKSTS));
  520. /* Wait for clock domain synchronization to be complete. */
  521. while ((LCDC->LCDC_LCDSR & LCDC_LCDSR_SIPSTS));
  522. /* Enable Horizontal and Vertical Synchronization. */
  523. LCDC->LCDC_LCDEN = LCDC_LCDEN_SYNCEN;
  524. /* Poll to check that the synchronization is up. */
  525. while (!(LCDC->LCDC_LCDSR & LCDC_LCDSR_LCDSTS));
  526. /* Wait for clock domain synchronization to be complete. */
  527. while ((LCDC->LCDC_LCDSR & LCDC_LCDSR_SIPSTS));
  528. /* Enable the display power signal. */
  529. LCDC->LCDC_LCDEN = LCDC_LCDEN_DISPEN;
  530. /* Poll to check that the power signal is activated. */
  531. while (!(LCDC->LCDC_LCDSR & LCDC_LCDSR_DISPSTS));
  532. /* Wait for clock domain synchronization to be complete. */
  533. while ((LCDC->LCDC_LCDSR & LCDC_LCDSR_SIPSTS));
  534. /* Enable backlight */
  535. LCDC->LCDC_LCDEN = LCDC_LCDEN_PWMEN;
  536. }
  537. /**
  538. * @brief Disable Display.
  539. *
  540. * @param[in] lcdcp pointer to the @p LCDCDriver object
  541. */
  542. static void lcdc_off(void) {
  543. /* Disable all DMA channel descriptors */
  544. clear_dma_desc(&base_dma_desc, &LCDC->LCDC_BASEADDR);
  545. clear_dma_desc(&ovr1_dma_desc, &LCDC->LCDC_OVR1ADDR);
  546. clear_dma_desc(&ovr2_dma_desc, &LCDC->LCDC_OVR2ADDR);
  547. clear_dma_desc(&heo_dma_desc, &LCDC->LCDC_HEOADDR);
  548. clear_dma_desc(&heo_dma_u_desc, &LCDC->LCDC_HEOUADDR);
  549. clear_dma_desc(&heo_dma_v_desc, &LCDC->LCDC_HEOVADDR);
  550. /* Disable DMA channels */
  551. LCDC->LCDC_BASECHDR = LCDC_BASECHDR_CHDIS;
  552. LCDC->LCDC_OVR1CHDR = LCDC_OVR1CHDR_CHDIS;
  553. LCDC->LCDC_OVR2CHDR = LCDC_OVR2CHDR_CHDIS;
  554. LCDC->LCDC_HEOCHDR = LCDC_HEOCHDR_CHDIS;
  555. LCDC->LCDC_BASECFG4 = 0;
  556. /* Poll CHSR until the channel is successfully disabled. */
  557. while (LCDC->LCDC_BASECHSR & LCDC_BASECHSR_CHSR);
  558. while (LCDC->LCDC_OVR1CHSR & LCDC_OVR1CHSR_CHSR);
  559. while (LCDC->LCDC_OVR2CHSR & LCDC_OVR1CHSR_CHSR);
  560. while (LCDC->LCDC_HEOCHSR & LCDC_HEOCHSR_CHSR);
  561. /* Disable backlight */
  562. LCDC->LCDC_LCDDIS = LCDC_LCDDIS_PWMDIS;
  563. /* Poll PWMSTS: field of the LCDC_LCDSR register to verify that the PWM
  564. is no activated. */
  565. while (LCDC->LCDC_LCDSR & LCDC_LCDSR_PWMSTS);
  566. /* Disable the DISP signal. */
  567. LCDC->LCDC_LCDDIS = LCDC_LCDDIS_DISPDIS;
  568. /* Poll DISPSTS field of the LCDC_LCDSR register to verify that the DISP
  569. is no longer activated. */
  570. while (LCDC->LCDC_LCDSR & LCDC_LCDSR_DISPSTS);
  571. /* Disable the hsync and vsync signals. */
  572. LCDC->LCDC_LCDDIS = LCDC_LCDDIS_SYNCDIS;
  573. /* Poll LCDSTS field of the LCDC_LCDSR register to check that the
  574. synchronization is off. */
  575. while (LCDC->LCDC_LCDSR & LCDC_LCDSR_LCDSTS);
  576. /* Disable the Pixel clock. */
  577. LCDC->LCDC_LCDDIS = LCDC_LCDDIS_CLKDIS;
  578. /* Poll CLKSTS field of the LCDC_LCDSR register to check that Pixel Clock
  579. is disabled. */
  580. while (LCDC->LCDC_LCDSR & LCDC_LCDSR_CLKSTS);
  581. }
  582. /*===========================================================================*/
  583. /* Driver interrupt handlers. */
  584. /*===========================================================================*/
  585. /*===========================================================================*/
  586. /* Driver exported functions. */
  587. /*===========================================================================*/
  588. /**
  589. * @brief Low level LCDC driver initialization.
  590. *
  591. * @notapi
  592. */
  593. void lcdc_lld_init(void) {
  594. #if SAMA_HAL_IS_SECURE
  595. mtxConfigPeriphSecurity(MATRIX0, ID_LCDC, SECURE_PER);
  596. #endif /* SAMA_HAL_IS_SECURE */
  597. /* Driver initialization.*/
  598. lcdcObjectInit(&LCDCD0);
  599. LCDCD0.lcdc = LCDC;
  600. /* Reset layer information */
  601. lcdd_base.bpp = 0;
  602. lcdd_base.buffer = NULL;
  603. lcdd_base.dma_desc = &base_dma_desc;
  604. lcdd_ovr1.bpp = 0;
  605. lcdd_ovr1.buffer = NULL;
  606. lcdd_ovr1.dma_desc = &ovr1_dma_desc;
  607. lcdd_ovr2.bpp = 0;
  608. lcdd_ovr2.buffer = NULL;
  609. lcdd_ovr2.dma_desc = &ovr2_dma_desc;
  610. lcdd_heo.bpp = 0;
  611. lcdd_heo.buffer = NULL;
  612. lcdd_heo.dma_desc = &heo_dma_desc;
  613. lcdd_heo.dma_u_desc = &heo_dma_u_desc;
  614. lcdd_heo.dma_v_desc = &heo_dma_v_desc;
  615. lcdd_hcc.bpp = 0;
  616. lcdd_base.buffer = NULL;
  617. lcdd_hcc.dma_desc = &hcc_dma_desc;
  618. /* Disable LCD controller */
  619. lcdc_off();
  620. /* Timing Engine Configuration */
  621. /* Disable interrupt */
  622. LCDC->LCDC_LCDIDR = 0xFFFFFFFF;
  623. /* Base */
  624. LCDC->LCDC_BASECFG0 = LCDC_BASECFG0_DLBO | LCDC_BASECFG0_BLEN_AHB_INCR16;
  625. /* Overlay 1, GA 0xFF */
  626. LCDC->LCDC_OVR1CFG0 = LCDC_OVR1CFG0_DLBO | LCDC_OVR1CFG0_BLEN_AHB_BLEN_INCR16 |
  627. LCDC_OVR1CFG0_ROTDIS;
  628. LCDC->LCDC_OVR1CFG9 = LCDC_OVR1CFG9_GA(0xFF) | LCDC_OVR1CFG9_GAEN;
  629. /* Overlay 2, GA 0xFF */
  630. LCDC->LCDC_OVR2CFG0 = LCDC_OVR2CFG0_DLBO | LCDC_OVR2CFG0_BLEN_AHB_INCR16 |
  631. LCDC_OVR2CFG0_ROTDIS;
  632. LCDC->LCDC_OVR2CFG9 = LCDC_OVR2CFG9_GA(0xFF) | LCDC_OVR2CFG9_GAEN;
  633. /* High End Overlay, GA 0xFF */
  634. LCDC->LCDC_HEOCFG0 = LCDC_HEOCFG0_DLBO | LCDC_HEOCFG0_BLEN_AHB_BLEN_INCR16 |
  635. LCDC_HEOCFG0_ROTDIS;
  636. LCDC->LCDC_HEOCFG12 = LCDC_HEOCFG12_GA(0xFF) | LCDC_HEOCFG12_GAEN;
  637. LCDC->LCDC_HEOCFG14 = LCDC_HEOCFG14_CSCRY(0x94) | LCDC_HEOCFG14_CSCRU(0xCC) |
  638. LCDC_HEOCFG14_CSCRV(0) | LCDC_HEOCFG14_CSCYOFF;
  639. LCDC->LCDC_HEOCFG15 = LCDC_HEOCFG15_CSCGY(0x94) | LCDC_HEOCFG15_CSCGU(0x387) |
  640. LCDC_HEOCFG15_CSCGV(0x3CD) | LCDC_HEOCFG15_CSCUOFF;
  641. LCDC->LCDC_HEOCFG16 = LCDC_HEOCFG16_CSCBY(0x94)| LCDC_HEOCFG16_CSCBU(0) |
  642. LCDC_HEOCFG16_CSCBV(0x102) | LCDC_HEOCFG16_CSCVOFF;
  643. }
  644. /**
  645. * @brief Configures and activates the LCDC peripheral.
  646. *
  647. * @param[in] lcdcp pointer to the @p LCDCDriver object
  648. *
  649. * @notapi
  650. */
  651. void lcdc_lld_start(LCDCDriver *lcdcp) {
  652. /* Enable the LCDC peripheral clock. */
  653. pmcEnableLCDC();
  654. /* Configure overlays */
  655. layer_config(lcdcp->config->listp, lcdcp->config->length);
  656. }
  657. /**
  658. * @brief Deactivates the LCDC peripheral.
  659. *
  660. * @param[in] lcdcp pointer to the @p LCDCDriver object
  661. *
  662. * @notapi
  663. */
  664. void lcdc_lld_stop(LCDCDriver *lcdcp) {
  665. if (lcdcp->state == LCDC_READY) {
  666. /* Disable display. */
  667. lcdc_off();
  668. /* Disable the LCDC clock. */
  669. pmcDisableLCDC();
  670. }
  671. }
  672. /**
  673. *
  674. * @brief Initializes the standard part of a @p LCDCDriver structure.
  675. *
  676. * @param[out] lcdcp pointer to a @p LCDCDriver object
  677. *
  678. * @init
  679. */
  680. void lcdcObjectInit(LCDCDriver *lcdcp) {
  681. lcdcp->state = LCDC_STOP;
  682. lcdcp->config = NULL;
  683. }
  684. /**
  685. * @brief LCDC driver initialization.
  686. *
  687. * @notapi
  688. */
  689. void lcdcInit(void) {
  690. lcdc_lld_init();
  691. }
  692. /**
  693. * @brief Configures and activates the LCDC peripheral.
  694. *
  695. * @param[in] lcdcp pointer to the @p LCDCDriver object
  696. * @param[in] configp pointer to the LCDCConfig struct
  697. *
  698. * @api
  699. */
  700. void lcdcStart(LCDCDriver *lcdcp, const LCDCConfig *configp) {
  701. osalDbgCheck(lcdcp != NULL);
  702. osalSysLock();
  703. osalDbgAssert((lcdcp->state == LCDC_STOP) , "invalid state");
  704. lcdcp->config = configp;
  705. lcdc_lld_start(lcdcp);
  706. lcdcp->state = LCDC_READY;
  707. osalSysUnlock();
  708. /* Enable display. */
  709. lcdc_on(lcdcp);
  710. }
  711. /**
  712. * @brief Deactivates the LCDC peripheral.
  713. *
  714. * @param[in] lcdcp pointer to the @p LCDCDriver object
  715. *
  716. * @api
  717. */
  718. void lcdcStop(LCDCDriver *lcdcp) {
  719. osalDbgCheck(lcdcp != NULL);
  720. osalSysLock();
  721. osalDbgAssert((lcdcp->state == LCDC_READY), "invalid state");
  722. lcdc_lld_stop(lcdcp);
  723. lcdcp->state = LCDC_STOP;
  724. osalSysUnlock();
  725. }
  726. void lcdcShowLayer(LCDCDriver *lcdcp, uint8_t id, bool enable) {
  727. (void)lcdcp;
  728. if(enable) {
  729. lcdd_layers[id].reg_enable[0] = LCDC_CHER_CHEN;
  730. }
  731. else {
  732. lcdd_layers[id].reg_enable[1] = LCDC_CHDR_CHDIS;
  733. }
  734. }
  735. /*
  736. * @brief brief Set the backlight of the LCD.
  737. *
  738. * param[in] level Backlight brightness level [1..255],
  739. * 255 means maximum brightness.
  740. *
  741. * @api
  742. */
  743. void lcdcSetBacklight(uint32_t level) {
  744. uint32_t cfg = LCDC->LCDC_LCDCFG6 & ~LCDC_LCDCFG6_PWMCVAL_Msk;
  745. LCDC->LCDC_LCDCFG6 = cfg | LCDC_LCDCFG6_PWMCVAL(level);
  746. }
  747. #if (TRUE == LCDC_USE_MUTUAL_EXCLUSION)
  748. /**
  749. * @brief Gains exclusive access to the LCDC module.
  750. * @details This function tries to gain ownership to the LCDC module, if the
  751. * module is already being used then the invoking thread is queued.
  752. * @pre In order to use this function the option
  753. * @p LCDC_USE_MUTUAL_EXCLUSION must be enabled.
  754. *
  755. * @param[in] lcdcp pointer to the @p LCDCDriver object
  756. *
  757. * @sclass
  758. */
  759. void lcdcAcquireBusS(LCDCDriver *lcdcp) {
  760. osalDbgCheckClassS();
  761. osalDbgCheck(lcdcp == &LCDCD0);
  762. #if (TRUE == CH_CFG_USE_MUTEXES)
  763. chMtxLockS(&lcdcp->lock);
  764. #else
  765. chSemWaitS(&lcdcp->lock);
  766. #endif
  767. }
  768. /**
  769. * @brief Gains exclusive access to the LCDC module.
  770. * @details This function tries to gain ownership to the LTDC module, if the
  771. * module is already being used then the invoking thread is queued.
  772. * @pre In order to use this function the option
  773. * @p LCDC_USE_MUTUAL_EXCLUSION must be enabled.
  774. *
  775. * @param[in] lcdcp pointer to the @p LCDCDriver object
  776. *
  777. * @api
  778. */
  779. void lcdcAcquireBus(LCDCDriver *lcdcp) {
  780. osalSysLock();
  781. lcdcAcquireBusS(lcdcp);
  782. osalSysUnlock();
  783. }
  784. /**
  785. * @brief Releases exclusive access to the LCDC module.
  786. * @pre In order to use this function the option
  787. * @p LCDC_USE_MUTUAL_EXCLUSION must be enabled.
  788. *
  789. * @param[in] lcdcp pointer to the @p LCDCDriver object
  790. *
  791. * @sclass
  792. */
  793. void lcdcReleaseBusS(LCDCDriver *lcdcp) {
  794. osalDbgCheckClassS();
  795. osalDbgCheck(lcdcp == &LCDCD0);
  796. #if (TRUE == CH_CFG_USE_MUTEXES)
  797. chMtxUnlockS(&lcdcp->lock);
  798. #else
  799. chSemSignalI(&lcdcp->lock);
  800. #endif
  801. }
  802. /**
  803. * @brief Releases exclusive access to the LCDC module.
  804. * @pre In order to use this function the option
  805. * @p LCDC_USE_MUTUAL_EXCLUSION must be enabled.
  806. *
  807. * @param[in] lcdcp pointer to the @p LCDCDriver object
  808. *
  809. * @api
  810. */
  811. void lcdcReleaseBus(LCDCDriver *lcdcp) {
  812. osalSysLock();
  813. lcdcReleaseBusS(lcdcp);
  814. osalSysUnlock();
  815. }
  816. #endif /* LCDC_USE_MUTUAL_EXCLUSION */
  817. #endif /* SAMA_USE_LCDC == TRUE */
  818. /** @} */