mio283qt2.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /**************************************************************************************
  2. * drivers/lcd/mio283qt2.c
  3. *
  4. * This is a driver for the MI0283QT-2 LCD from Multi-Inno Technology Co., Ltd. This
  5. * LCD is based on the Himax HX8347-D LCD controller.
  6. *
  7. * Copyright (C) 2012 Gregory Nutt. All rights reserved.
  8. * Authors: Gregory Nutt <gnutt@nuttx.org>
  9. *
  10. * References:
  11. * 1) LCD Module Specification, Model : MI0283QT-2, Multi-Inno Technology Co.,
  12. * Ltd., Revision 1.0
  13. * 2) Data Sheet: HX8347-D(T), 240RGB x 320 dot, 262K color, with internal GRAM, TFT
  14. * Mobile Single Chip Driver Version 02 March, Doc No. HX8347-D(T)-DS, Himax
  15. * Technologies, Inc., 2009,
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. *
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in
  25. * the documentation and/or other materials provided with the
  26. * distribution.
  27. * 3. Neither the name NuttX nor the names of its contributors may be
  28. * used to endorse or promote products derived from this software
  29. * without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  34. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  35. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  36. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  37. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  38. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  39. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  41. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. * POSSIBILITY OF SUCH DAMAGE.
  43. *
  44. **************************************************************************************/
  45. /**************************************************************************************
  46. * Included Files
  47. **************************************************************************************/
  48. #include <nuttx/config.h>
  49. #include <sys/types.h>
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include <string.h>
  53. #include <errno.h>
  54. #include <debug.h>
  55. #include <nuttx/arch.h>
  56. #include <nuttx/spi/spi.h>
  57. #include <nuttx/lcd/lcd.h>
  58. #include <nuttx/lcd/mio283qt2.h>
  59. #ifdef CONFIG_LCD_MIO283QT2
  60. /**************************************************************************************
  61. * Pre-processor Definitions
  62. **************************************************************************************/
  63. /* Configuration **********************************************************************/
  64. /* Check contrast selection */
  65. #if !defined(CONFIG_LCD_MAXCONTRAST)
  66. # define CONFIG_LCD_MAXCONTRAST 1
  67. #endif
  68. /* Check power setting */
  69. #if !defined(CONFIG_LCD_MAXPOWER) || CONFIG_LCD_MAXPOWER < 1
  70. # define CONFIG_LCD_MAXPOWER 1
  71. #endif
  72. #if CONFIG_LCD_MAXPOWER > 255
  73. # error "CONFIG_LCD_MAXPOWER must be less than 256 to fit in uint8_t"
  74. #endif
  75. /* Check orientation */
  76. #if defined(CONFIG_LCD_PORTRAIT)
  77. # if defined(CONFIG_LCD_LANDSCAPE) || defined(CONFIG_LCD_RLANDSCAPE) || defined(CONFIG_LCD_RPORTRAIT)
  78. # error "Cannot define both portrait and any other orientations"
  79. # endif
  80. #elif defined(CONFIG_LCD_RPORTRAIT)
  81. # if defined(CONFIG_LCD_LANDSCAPE) || defined(CONFIG_LCD_RLANDSCAPE)
  82. # error "Cannot define both rportrait and any other orientations"
  83. # endif
  84. #elif defined(CONFIG_LCD_LANDSCAPE)
  85. # ifdef CONFIG_LCD_RLANDSCAPE
  86. # error "Cannot define both landscape and any other orientations"
  87. # endif
  88. #elif !defined(CONFIG_LCD_RLANDSCAPE)
  89. # define CONFIG_LCD_LANDSCAPE 1
  90. #endif
  91. /* Display/Color Properties ***********************************************************/
  92. /* Display Resolution */
  93. #if defined(CONFIG_LCD_LANDSCAPE) || defined(CONFIG_LCD_RLANDSCAPE)
  94. # define MIO283QT2_XRES 320
  95. # define MIO283QT2_YRES 240
  96. #else
  97. # define MIO283QT2_XRES 240
  98. # define MIO283QT2_YRES 320
  99. #endif
  100. /* Color depth and format */
  101. #define MIO283QT2_BPP 16
  102. #define MIO283QT2_COLORFMT FB_FMT_RGB16_565
  103. /* Hardware LCD/LCD controller definitions ********************************************/
  104. /* In this driver, I chose to use all literal constants for register address and
  105. * values. Some recent experiences have shown me that during LCD bringup, it is more
  106. * important to know the binary values rather than nice, people friendly names. Sad,
  107. * but true.
  108. */
  109. #define HIMAX_ID 0x0047
  110. /* LCD Profiles ***********************************************************************/
  111. /* Many details of the controller initialization must, unfortunately, vary from LCD to
  112. * LCD. I have looked at the spec and at three different drivers for LCDs that have
  113. * MIO283QT2 controllers. I have tried to summarize these differences as "LCD profiles"
  114. *
  115. * Most of the differences between LCDs are nothing more than a few minor bit
  116. * settings. The most significant difference betwen LCD drivers in is the
  117. * manner in which the LCD is powered up and in how the power controls are set.
  118. * My suggestion is that if you have working LCD initialization code, you should
  119. * simply replace the code in mio283qt2_hwinitialize with your working code.
  120. */
  121. #if defined (CONFIG_MIO283QT2_PROFILE2)
  122. # undef MIO283QT2_USE_SIMPLE_INIT
  123. /* PWRCTRL1: AP=smalll-to-medium, DC=Flinex24, BT=+5/-4, DCT=Flinex24 */
  124. # define PWRCTRL1_SETTING \
  125. (MIO283QT2_PWRCTRL1_AP_SMMED | MIO283QT2_PWRCTRL1_DC_FLINEx24 | \
  126. MIO283QT2_PWRCTRL1_BT_p5m4 | MIO283QT2_PWRCTRL1_DCT_FLINEx24)
  127. /* PWRCTRL2: 5.1v */
  128. # define PWRCTRL2_SETTING MIO283QT2_PWRCTRL2_VRC_5p1V
  129. /* PWRCTRL3: x 2.165
  130. * NOTE: Many drivers have bit 8 set which is not defined in the MIO283QT2 spec.
  131. */
  132. # define PWRCTRL3_SETTING MIO283QT2_PWRCTRL3_VRH_x2p165
  133. /* PWRCTRL4: VDV=9 + VCOMG */
  134. # define PWRCTRL4_SETTING (MIO283QT2_PWRCTRL4_VDV(9) | MIO283QT2_PWRCTRL4_VCOMG)
  135. /* PWRCTRL5: VCM=56 + NOTP */
  136. # define PWRCTRL5_SETTING (MIO283QT2_PWRCTRL5_VCM(56) | MIO283QT2_PWRCTRL5_NOTP)
  137. #elif defined (CONFIG_MIO283QT2_PROFILE3)
  138. # undef MIO283QT2_USE_SIMPLE_INIT
  139. /* PWRCTRL1: AP=smalll-to-medium, DC=Flinex24, BT=+5/-4, DCT=Flinex24 */
  140. # define PWRCTRL1_SETTING \
  141. (MIO283QT2_PWRCTRL1_AP_SMMED | MIO283QT2_PWRCTRL1_DC_FLINEx24 | \
  142. MIO283QT2_PWRCTRL1_BT_p5m4 | MIO283QT2_PWRCTRL1_DCT_FLINEx24)
  143. /* PWRCTRL2: 5.1v */
  144. # define PWRCTRL2_SETTING MIO283QT2_PWRCTRL2_VRC_5p1V
  145. /* PWRCTRL3: x 2.165
  146. * NOTE: Many drivers have bit 8 set which is not defined in the MIO283QT2 spec.
  147. */
  148. # define PWRCTRL3_SETTING MIO283QT2_PWRCTRL3_VRH_x2p165
  149. /* PWRCTRL4: VDV=9 + VCOMG */
  150. # define PWRCTRL4_SETTING (MIO283QT2_PWRCTRL4_VDV(9) | MIO283QT2_PWRCTRL4_VCOMG)
  151. /* PWRCTRL5: VCM=56 + NOTP */
  152. # define PWRCTRL5_SETTING (MIO283QT2_PWRCTRL5_VCM(56) | MIO283QT2_PWRCTRL5_NOTP)
  153. #else /* if defined (CONFIG_MIO283QT2_PROFILE1) */
  154. # undef MIO283QT2_USE_SIMPLE_INIT
  155. # define MIO283QT2_USE_SIMPLE_INIT 1
  156. /* PWRCTRL1: AP=medium-to-large, DC=Fosc/4, BT=+5/-4, DCT=Fosc/4 */
  157. # define PWRCTRL1_SETTING \
  158. (MIO283QT2_PWRCTRL1_AP_MEDLG | MIO283QT2_PWRCTRL1_DC_FOSd4 | \
  159. MIO283QT2_PWRCTRL1_BT_p5m4 | MIO283QT2_PWRCTRL1_DCT_FOSd4)
  160. /* PWRCTRL2: 5.3v */
  161. # define PWRCTRL2_SETTING MIO283QT2_PWRCTRL2_VRC_5p3V
  162. /* PWRCTRL3: x 2.570
  163. * NOTE: Many drivers have bit 8 set which is not defined in the MIO283QT2 spec.
  164. */
  165. # define PWRCTRL3_SETTING MIO283QT2_PWRCTRL3_VRH_x2p570
  166. /* PWRCTRL4: VDV=12 + VCOMG */
  167. # define PWRCTRL4_SETTING (MIO283QT2_PWRCTRL4_VDV(12) | MIO283QT2_PWRCTRL4_VCOMG)
  168. /* PWRCTRL5: VCM=60 + NOTP */
  169. # define PWRCTRL5_SETTING (MIO283QT2_PWRCTRL5_VCM(60) | MIO283QT2_PWRCTRL5_NOTP)
  170. #endif
  171. /**************************************************************************************
  172. * Private Type Definition
  173. **************************************************************************************/
  174. /* This structure describes the state of this driver */
  175. struct mio283qt2_dev_s
  176. {
  177. /* Publically visible device structure */
  178. struct lcd_dev_s dev;
  179. /* Private LCD-specific information follows */
  180. FAR struct mio283qt2_lcd_s *lcd; /* The contained platform-specific, LCD interface */
  181. uint8_t power; /* Current power setting */
  182. /* This is working memory allocated by the LCD driver for each LCD device
  183. * and for each color plane. This memory will hold one raster line of data.
  184. * The size of the allocated run buffer must therefore be at least
  185. * (bpp * xres / 8). Actual alignment of the buffer must conform to the
  186. * bitwidth of the underlying pixel type.
  187. *
  188. * If there are multiple planes, they may share the same working buffer
  189. * because different planes will not be operate on concurrently. However,
  190. * if there are multiple LCD devices, they must each have unique run buffers.
  191. */
  192. uint16_t runbuffer[MIO283QT2_XRES];
  193. };
  194. /**************************************************************************************
  195. * Private Function Protototypes
  196. **************************************************************************************/
  197. /* Low Level LCD access */
  198. static void mio283qt2_putreg(FAR struct mio283qt2_lcd_s *lcd, uint8_t regaddr,
  199. uint16_t regval);
  200. #ifndef CONFIG_LCD_NOGETRUN
  201. static uint16_t mio283qt2_readreg(FAR struct mio283qt2_lcd_s *lcd, uint8_t regaddr);
  202. #endif
  203. static inline void mio283qt2_gramwrite(FAR struct mio283qt2_lcd_s *lcd,
  204. uint16_t rgbcolor);
  205. #ifndef CONFIG_LCD_NOGETRUN
  206. static inline void mio283qt2_readsetup(FAR struct mio283qt2_lcd_s *lcd,
  207. FAR uint16_t *accum);
  208. static inline uint16_t mio283qt2_gramread(FAR struct mio283qt2_lcd_s *lcd,
  209. FAR uint16_t *accum);
  210. #endif
  211. static void mio283qt2_setarea(FAR struct mio283qt2_lcd_s *lcd,
  212. uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
  213. /* LCD Data Transfer Methods */
  214. static int mio283qt2_putrun(fb_coord_t row, fb_coord_t col, FAR const uint8_t *buffer,
  215. size_t npixels);
  216. static int mio283qt2_getrun(fb_coord_t row, fb_coord_t col, FAR uint8_t *buffer,
  217. size_t npixels);
  218. /* LCD Configuration */
  219. static int mio283qt2_getvideoinfo(FAR struct lcd_dev_s *dev,
  220. FAR struct fb_videoinfo_s *vinfo);
  221. static int mio283qt2_getplaneinfo(FAR struct lcd_dev_s *dev, unsigned int planeno,
  222. FAR struct lcd_planeinfo_s *pinfo);
  223. /* LCD RGB Mapping */
  224. #ifdef CONFIG_FB_CMAP
  225. # error "RGB color mapping not supported by this driver"
  226. #endif
  227. /* Cursor Controls */
  228. #ifdef CONFIG_FB_HWCURSOR
  229. # error "Cursor control not supported by this driver"
  230. #endif
  231. /* LCD Specific Controls */
  232. static int mio283qt2_getpower(FAR struct lcd_dev_s *dev);
  233. static int mio283qt2_setpower(FAR struct lcd_dev_s *dev, int power);
  234. static int mio283qt2_getcontrast(FAR struct lcd_dev_s *dev);
  235. static int mio283qt2_setcontrast(FAR struct lcd_dev_s *dev, unsigned int contrast);
  236. /* Initialization */
  237. static inline int mio283qt2_hwinitialize(FAR struct mio283qt2_dev_s *priv);
  238. /**************************************************************************************
  239. * Private Data
  240. **************************************************************************************/
  241. /* This driver can support only a signal MIO283QT2 device. This is due to an
  242. * unfortunate decision made whent he getrun and putrun methods were designed. The
  243. * following is the single MIO283QT2 driver state instance:
  244. */
  245. static struct mio283qt2_dev_s g_lcddev;
  246. /**************************************************************************************
  247. * Private Functions
  248. **************************************************************************************/
  249. /**************************************************************************************
  250. * Name: mio283qt2_putreg(lcd,
  251. *
  252. * Description:
  253. * Write to an LCD register
  254. *
  255. **************************************************************************************/
  256. static void mio283qt2_putreg(FAR struct mio283qt2_lcd_s *lcd,
  257. uint8_t regaddr, uint16_t regval)
  258. {
  259. /* Set the index register to the register address and write the register contents */
  260. lcd->index(lcd, regaddr);
  261. lcd->write(lcd, regval);
  262. }
  263. /**************************************************************************************
  264. * Name: mio283qt2_readreg
  265. *
  266. * Description:
  267. * Read from an LCD register
  268. *
  269. **************************************************************************************/
  270. #ifndef CONFIG_LCD_NOGETRUN
  271. static uint16_t mio283qt2_readreg(FAR struct mio283qt2_lcd_s *lcd, uint8_t regaddr)
  272. {
  273. /* Set the index register to the register address and read the register contents. */
  274. lcd->index(lcd, regaddr);
  275. return lcd->read(lcd);
  276. }
  277. #endif
  278. /**************************************************************************************
  279. * Name: mio283qt2_gramselect
  280. *
  281. * Description:
  282. * Setup to read or write multiple pixels to the GRAM memory
  283. *
  284. **************************************************************************************/
  285. static inline void mio283qt2_gramselect(FAR struct mio283qt2_lcd_s *lcd)
  286. {
  287. lcd->index(lcd, 0x22);
  288. }
  289. /**************************************************************************************
  290. * Name: mio283qt2_gramwrite
  291. *
  292. * Description:
  293. * Setup to read or write multiple pixels to the GRAM memory
  294. *
  295. **************************************************************************************/
  296. static inline void mio283qt2_gramwrite(FAR struct mio283qt2_lcd_s *lcd, uint16_t data)
  297. {
  298. lcd->write(lcd, data);
  299. }
  300. /**************************************************************************************
  301. * Name: mio283qt2_readsetup
  302. *
  303. * Description:
  304. * Prime the operation by reading one pixel from the GRAM memory if necessary for
  305. * this LCD type. When reading 16-bit gram data, there may be some shifts in the
  306. * returned data:
  307. *
  308. * - ILI932x: Discard first dummy read; no shift in the return data
  309. *
  310. **************************************************************************************/
  311. #ifndef CONFIG_LCD_NOGETRUN
  312. static inline void mio283qt2_readsetup(FAR struct mio283qt2_lcd_s *lcd,
  313. FAR uint16_t *accum)
  314. {
  315. #if 0 /* Probably not necessary... untested */
  316. /* Read-ahead one pixel */
  317. *accum = lcd->read(lcd);
  318. #endif
  319. }
  320. #endif
  321. /**************************************************************************************
  322. * Name: mio283qt2_gramread
  323. *
  324. * Description:
  325. * Read one correctly aligned pixel from the GRAM memory. Possibly shifting the
  326. * data and possibly swapping red and green components.
  327. *
  328. * - ILI932x: Unknown -- assuming colors are in the color order
  329. *
  330. **************************************************************************************/
  331. #ifndef CONFIG_LCD_NOGETRUN
  332. static inline uint16_t mio283qt2_gramread(FAR struct mio283qt2_lcd_s *lcd,
  333. FAR uint16_t *accum)
  334. {
  335. /* Read the value (GRAM register already selected) */
  336. return lcd->read(lcd);
  337. }
  338. #endif
  339. /**************************************************************************************
  340. * Name: mio283qt2_setarea
  341. *
  342. * Description:
  343. * Set the cursor position. In landscape mode, the "column" is actually the physical
  344. * Y position and the "row" is the physical X position.
  345. *
  346. **************************************************************************************/
  347. static void mio283qt2_setarea(FAR struct mio283qt2_lcd_s *lcd,
  348. uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
  349. {
  350. mio283qt2_putreg(lcd, 0x03, (x0 & 0x00ff)); /* set x0 */
  351. mio283qt2_putreg(lcd, 0x02, (x0 >> 8)); /* set x0 */
  352. mio283qt2_putreg(lcd, 0x05, (x1 & 0x00ff)); /* set x1 */
  353. mio283qt2_putreg(lcd, 0x04, (x1 >> 8)); /* set x1 */
  354. mio283qt2_putreg(lcd, 0x07, (y0 & 0x00ff)); /* set y0 */
  355. mio283qt2_putreg(lcd, 0x06, (y0 >> 8)); /* set y0 */
  356. mio283qt2_putreg(lcd, 0x09, (y1 & 0x00ff)); /* set y1 */
  357. mio283qt2_putreg(lcd, 0x08, (y1 >> 8)); /* set y1 */
  358. }
  359. /**************************************************************************************
  360. * Name: mio283qt2_dumprun
  361. *
  362. * Description:
  363. * Dump the contexts of the run buffer:
  364. *
  365. * run - The buffer in containing the run read to be dumped
  366. * npixels - The number of pixels to dump
  367. *
  368. **************************************************************************************/
  369. #if 0 /* Sometimes useful */
  370. static void mio283qt2_dumprun(FAR const char *msg, FAR uint16_t *run, size_t npixels)
  371. {
  372. int i, j;
  373. syslog(LOG_INFO, "\n%s:\n", msg);
  374. for (i = 0; i < npixels; i += 16)
  375. {
  376. up_putc(' ');
  377. syslog(LOG_INFO, " ");
  378. for (j = 0; j < 16; j++)
  379. {
  380. syslog(LOG_INFO, " %04x", *run++);
  381. }
  382. up_putc('\n');
  383. }
  384. }
  385. #endif
  386. /**************************************************************************************
  387. * Name: mio283qt2_putrun
  388. *
  389. * Description:
  390. * This method can be used to write a partial raster line to the LCD:
  391. *
  392. * row - Starting row to write to (range: 0 <= row < yres)
  393. * col - Starting column to write to (range: 0 <= col <= xres-npixels)
  394. * buffer - The buffer containing the run to be written to the LCD
  395. * npixels - The number of pixels to write to the LCD
  396. * (range: 0 < npixels <= xres-col)
  397. *
  398. **************************************************************************************/
  399. static int mio283qt2_putrun(fb_coord_t row, fb_coord_t col, FAR const uint8_t *buffer,
  400. size_t npixels)
  401. {
  402. FAR struct mio283qt2_dev_s *priv = &g_lcddev;
  403. FAR struct mio283qt2_lcd_s *lcd = priv->lcd;
  404. FAR const uint16_t *src = (FAR const uint16_t *)buffer;
  405. int i;
  406. /* Buffer must be provided and aligned to a 16-bit address boundary */
  407. lcdinfo("row: %d col: %d npixels: %d\n", row, col, npixels);
  408. DEBUGASSERT(buffer && ((uintptr_t)buffer & 1) == 0);
  409. /* Select the LCD */
  410. lcd->select(lcd);
  411. /* Write the run to GRAM. */
  412. mio283qt2_setarea(lcd, col, row, col + npixels - 1, row);
  413. mio283qt2_gramselect(lcd);
  414. for (i = 0; i < npixels; i++)
  415. {
  416. mio283qt2_gramwrite(lcd, *src);
  417. src++;
  418. }
  419. /* De-select the LCD */
  420. lcd->deselect(lcd);
  421. return OK;
  422. }
  423. /**************************************************************************************
  424. * Name: mio283qt2_getrun
  425. *
  426. * Description:
  427. * This method can be used to read a partial raster line from the LCD:
  428. *
  429. * row - Starting row to read from (range: 0 <= row < yres)
  430. * col - Starting column to read read (range: 0 <= col <= xres-npixels)
  431. * buffer - The buffer in which to return the run read from the LCD
  432. * npixels - The number of pixels to read from the LCD
  433. * (range: 0 < npixels <= xres-col)
  434. *
  435. **************************************************************************************/
  436. static int mio283qt2_getrun(fb_coord_t row, fb_coord_t col, FAR uint8_t *buffer,
  437. size_t npixels)
  438. {
  439. #ifndef CONFIG_LCD_NOGETRUN
  440. FAR struct mio283qt2_dev_s *priv = &g_lcddev;
  441. FAR struct mio283qt2_lcd_s *lcd = priv->lcd;
  442. FAR uint16_t *dest = (FAR uint16_t *)buffer;
  443. uint16_t accum;
  444. int i;
  445. /* Buffer must be provided and aligned to a 16-bit address boundary */
  446. lcdinfo("row: %d col: %d npixels: %d\n", row, col, npixels);
  447. DEBUGASSERT(buffer && ((uintptr_t)buffer & 1) == 0);
  448. /* Select the LCD */
  449. lcd->select(lcd);
  450. /* Read the run from GRAM. */
  451. /* Select the LCD */
  452. lcd->select(lcd);
  453. /* Red the run fram GRAM. */
  454. mio283qt2_setarea(lcd, col, row, col + npixels - 1, row);
  455. mio283qt2_gramselect(lcd);
  456. /* Prime the pump for unaligned read data */
  457. mio283qt2_readsetup(lcd, &accum);
  458. for (i = 0; i < npixels; i++)
  459. {
  460. *dest++ = mio283qt2_gramread(lcd, &accum);
  461. }
  462. /* De-select the LCD */
  463. lcd->deselect(lcd);
  464. return OK;
  465. #else
  466. return -ENOSYS;
  467. #endif
  468. }
  469. /**************************************************************************************
  470. * Name: mio283qt2_getvideoinfo
  471. *
  472. * Description:
  473. * Get information about the LCD video controller configuration.
  474. *
  475. **************************************************************************************/
  476. static int mio283qt2_getvideoinfo(FAR struct lcd_dev_s *dev,
  477. FAR struct fb_videoinfo_s *vinfo)
  478. {
  479. DEBUGASSERT(dev && vinfo);
  480. lcdinfo("fmt: %d xres: %d yres: %d nplanes: 1\n",
  481. MIO283QT2_COLORFMT, MIO283QT2_XRES, MIO283QT2_XRES);
  482. vinfo->fmt = MIO283QT2_COLORFMT; /* Color format: RGB16-565: RRRR RGGG GGGB BBBB */
  483. vinfo->xres = MIO283QT2_XRES; /* Horizontal resolution in pixel columns */
  484. vinfo->yres = MIO283QT2_YRES; /* Vertical resolution in pixel rows */
  485. vinfo->nplanes = 1; /* Number of color planes supported */
  486. return OK;
  487. }
  488. /**************************************************************************************
  489. * Name: mio283qt2_getplaneinfo
  490. *
  491. * Description:
  492. * Get information about the configuration of each LCD color plane.
  493. *
  494. **************************************************************************************/
  495. static int mio283qt2_getplaneinfo(FAR struct lcd_dev_s *dev, unsigned int planeno,
  496. FAR struct lcd_planeinfo_s *pinfo)
  497. {
  498. FAR struct mio283qt2_dev_s *priv = (FAR struct mio283qt2_dev_s *)dev;
  499. DEBUGASSERT(dev && pinfo && planeno == 0);
  500. lcdinfo("planeno: %d bpp: %d\n", planeno, MIO283QT2_BPP);
  501. pinfo->putrun = mio283qt2_putrun; /* Put a run into LCD memory */
  502. pinfo->getrun = mio283qt2_getrun; /* Get a run from LCD memory */
  503. pinfo->buffer = (FAR uint8_t *)priv->runbuffer; /* Run scratch buffer */
  504. pinfo->bpp = MIO283QT2_BPP; /* Bits-per-pixel */
  505. return OK;
  506. }
  507. /**************************************************************************************
  508. * Name: mio283qt2_getpower
  509. *
  510. * Description:
  511. * Get the LCD panel power status (0: full off - CONFIG_LCD_MAXPOWER: full on). On
  512. * backlit LCDs, this setting may correspond to the backlight setting.
  513. *
  514. **************************************************************************************/
  515. static int mio283qt2_getpower(FAR struct lcd_dev_s *dev)
  516. {
  517. lcdinfo("power: %d\n", 0);
  518. return g_lcddev.power;
  519. }
  520. /**************************************************************************************
  521. * Name: mio283qt2_poweroff
  522. *
  523. * Description:
  524. * Enable/disable LCD panel power (0: full off - CONFIG_LCD_MAXPOWER: full on). On
  525. * backlit LCDs, this setting may correspond to the backlight setting.
  526. *
  527. **************************************************************************************/
  528. static int mio283qt2_poweroff(FAR struct mio283qt2_lcd_s *lcd)
  529. {
  530. /* Select the LCD */
  531. lcd->select(lcd);
  532. /* Set the backlight off */
  533. lcd->backlight(lcd, 0);
  534. /* Turn the display off */
  535. mio283qt2_putreg(lcd, 0x28, 0x0000); /* GON=0, DTE=0, D=0 */
  536. /* Deselect the LCD */
  537. lcd->deselect(lcd);
  538. /* Remember the power off state */
  539. g_lcddev.power = 0;
  540. return OK;
  541. }
  542. /**************************************************************************************
  543. * Name: mio283qt2_setpower
  544. *
  545. * Description:
  546. * Enable/disable LCD panel power (0: full off - CONFIG_LCD_MAXPOWER: full on). On
  547. * backlit LCDs, this setting may correspond to the backlight setting.
  548. *
  549. **************************************************************************************/
  550. static int mio283qt2_setpower(FAR struct lcd_dev_s *dev, int power)
  551. {
  552. FAR struct mio283qt2_dev_s *priv = (FAR struct mio283qt2_dev_s *)dev;
  553. FAR struct mio283qt2_lcd_s *lcd = priv->lcd;
  554. lcdinfo("power: %d\n", power);
  555. DEBUGASSERT((unsigned)power <= CONFIG_LCD_MAXPOWER);
  556. /* Set new power level */
  557. if (power > 0)
  558. {
  559. /* Select the LCD */
  560. lcd->select(lcd);
  561. /* Set the backlight level */
  562. lcd->backlight(lcd, power);
  563. /* Then turn the display on:
  564. * D=ON(3) CM=0 DTE=1 GON=1 SPT=0 VLE=0 PT=0
  565. */
  566. mio283qt2_putreg(lcd, 0x28, 0x0038); /* GON=1, DTE=1, D=2 */
  567. up_mdelay(40);
  568. mio283qt2_putreg(lcd, 0x28, 0x003c); /* GON=1, DTE=1, D=3 */
  569. /* Deselect the LCD */
  570. lcd->deselect(lcd);
  571. /* Remember the power on state */
  572. g_lcddev.power = power;
  573. }
  574. else
  575. {
  576. /* Turn the display off */
  577. mio283qt2_poweroff(lcd);
  578. }
  579. return OK;
  580. }
  581. /**************************************************************************************
  582. * Name: mio283qt2_getcontrast
  583. *
  584. * Description:
  585. * Get the current contrast setting (0-CONFIG_LCD_MAXCONTRAST).
  586. *
  587. **************************************************************************************/
  588. static int mio283qt2_getcontrast(FAR struct lcd_dev_s *dev)
  589. {
  590. lcdinfo("Not implemented\n");
  591. return -ENOSYS;
  592. }
  593. /**************************************************************************************
  594. * Name: mio283qt2_setcontrast
  595. *
  596. * Description:
  597. * Set LCD panel contrast (0-CONFIG_LCD_MAXCONTRAST).
  598. *
  599. **************************************************************************************/
  600. static int mio283qt2_setcontrast(FAR struct lcd_dev_s *dev, unsigned int contrast)
  601. {
  602. lcdinfo("contrast: %d\n", contrast);
  603. return -ENOSYS;
  604. }
  605. /**************************************************************************************
  606. * Name: mio283qt2_hwinitialize
  607. *
  608. * Description:
  609. * Initialize the LCD hardware.
  610. *
  611. **************************************************************************************/
  612. static inline int mio283qt2_hwinitialize(FAR struct mio283qt2_dev_s *priv)
  613. {
  614. FAR struct mio283qt2_lcd_s *lcd = priv->lcd;
  615. #ifndef CONFIG_LCD_NOGETRUN
  616. uint16_t id;
  617. #endif
  618. int ret;
  619. /* Select the LCD */
  620. lcd->select(lcd);
  621. /* Read the HIMAX ID registger (0x00) */
  622. #ifndef CONFIG_LCD_NOGETRUN
  623. id = mio283qt2_readreg(lcd, 0x00);
  624. lcdinfo("LCD ID: %04x\n", id);
  625. /* Check if the ID is for the MIO283QT2 */
  626. if (id == HIMAX_ID)
  627. #endif
  628. {
  629. /* Driving ability */
  630. mio283qt2_putreg(lcd, 0xea, 0x0000); /* PTBA[15:8] */
  631. mio283qt2_putreg(lcd, 0xeb, 0x0020); /* PTBA[7:0] */
  632. mio283qt2_putreg(lcd, 0xec, 0x000c); /* STBA[15:8] */
  633. mio283qt2_putreg(lcd, 0xed, 0x00c4); /* STBA[7:0] */
  634. mio283qt2_putreg(lcd, 0xe8, 0x0040); /* OPON[7:0] */
  635. mio283qt2_putreg(lcd, 0xe9, 0x0038); /* OPON1[7:0] */
  636. mio283qt2_putreg(lcd, 0xf1, 0x0001); /* OTPS1B */
  637. mio283qt2_putreg(lcd, 0xf2, 0x0010); /* GEN */
  638. mio283qt2_putreg(lcd, 0x27, 0x00a3);
  639. /* Power voltage */
  640. mio283qt2_putreg(lcd, 0x1b, 0x001b); /* VRH = 4.65 */
  641. mio283qt2_putreg(lcd, 0x1a, 0x0001); /* BT */
  642. mio283qt2_putreg(lcd, 0x24, 0x002f); /* VMH */
  643. mio283qt2_putreg(lcd, 0x25, 0x0057); /* VML */
  644. /* Vcom offset */
  645. mio283qt2_putreg(lcd, 0x23, 0x008d); /* For flicker adjust */
  646. /* Power on */
  647. mio283qt2_putreg(lcd, 0x18, 0x0036);
  648. mio283qt2_putreg(lcd, 0x19, 0x0001); /* Start oscillator */
  649. mio283qt2_putreg(lcd, 0x01, 0x0000); /* Wakeup */
  650. mio283qt2_putreg(lcd, 0x1f, 0x0088);
  651. up_mdelay(5);
  652. mio283qt2_putreg(lcd, 0x1f, 0x0080);
  653. up_mdelay(5);
  654. mio283qt2_putreg(lcd, 0x1f, 0x0090);
  655. up_mdelay(5);
  656. mio283qt2_putreg(lcd, 0x1f, 0x00d0);
  657. up_mdelay(5);
  658. /* Gamma 2.8 setting */
  659. mio283qt2_putreg(lcd, 0x40, 0x0000);
  660. mio283qt2_putreg(lcd, 0x41, 0x0000);
  661. mio283qt2_putreg(lcd, 0x42, 0x0001);
  662. mio283qt2_putreg(lcd, 0x43, 0x0013);
  663. mio283qt2_putreg(lcd, 0x44, 0x0010);
  664. mio283qt2_putreg(lcd, 0x45, 0x0026);
  665. mio283qt2_putreg(lcd, 0x46, 0x0008);
  666. mio283qt2_putreg(lcd, 0x47, 0x0051);
  667. mio283qt2_putreg(lcd, 0x48, 0x0002);
  668. mio283qt2_putreg(lcd, 0x49, 0x0012);
  669. mio283qt2_putreg(lcd, 0x4a, 0x0018);
  670. mio283qt2_putreg(lcd, 0x4b, 0x0019);
  671. mio283qt2_putreg(lcd, 0x4c, 0x0014);
  672. mio283qt2_putreg(lcd, 0x50, 0x0019);
  673. mio283qt2_putreg(lcd, 0x51, 0x002f);
  674. mio283qt2_putreg(lcd, 0x52, 0x002c);
  675. mio283qt2_putreg(lcd, 0x53, 0x003e);
  676. mio283qt2_putreg(lcd, 0x54, 0x003f);
  677. mio283qt2_putreg(lcd, 0x55, 0x003f);
  678. mio283qt2_putreg(lcd, 0x56, 0x002e);
  679. mio283qt2_putreg(lcd, 0x57, 0x0077);
  680. mio283qt2_putreg(lcd, 0x58, 0x000b);
  681. mio283qt2_putreg(lcd, 0x59, 0x0006);
  682. mio283qt2_putreg(lcd, 0x5a, 0x0007);
  683. mio283qt2_putreg(lcd, 0x5b, 0x000d);
  684. mio283qt2_putreg(lcd, 0x5c, 0x001d);
  685. mio283qt2_putreg(lcd, 0x5d, 0x00cc);
  686. /* 4K Color Selection */
  687. mio283qt2_putreg(lcd, 0x17, 0x0003);
  688. mio283qt2_putreg(lcd, 0x17, 0x0005); /* 0x0005=65k, 0x0006=262k */
  689. /* Panel characteristics */
  690. mio283qt2_putreg(lcd, 0x36, 0x0000);
  691. /* Display Setting */
  692. mio283qt2_putreg(lcd, 0x01, 0x0000); /* IDMON=0, INVON=0, NORON=0, PTLON=0 */
  693. #if defined(CONFIG_LCD_LANDSCAPE)
  694. mio283qt2_putreg(lcd, 0x16, 0x00a8); /* MY=1, MX=0, MV=1, ML=0, BGR=1 */
  695. #elif defined(CONFIG_LCD_PORTRAIT)
  696. mio283qt2_putreg(lcd, 0x16, 0x0008); /* MY=0, MX=0, MV=0, ML=0, BGR=1 */
  697. #elif defined(CONFIG_LCD_RLANDSCAPE)
  698. mio283qt2_putreg(lcd, 0x16, 0x0068); /* MY=0, MX=1, MV=1, ML=0, BGR=1 */
  699. #elif defined(CONFIG_LCD_RPORTRAIT)
  700. mio283qt2_putreg(lcd, 0x16, 0x00c8); /* MY=1, MX=0, MV=1, ML=0, BGR=1 */
  701. #endif
  702. /* Window setting */
  703. mio283qt2_setarea(lcd, 0, 0, (MIO283QT2_XRES-1), (MIO283QT2_YRES-1));
  704. ret = OK;
  705. }
  706. #ifndef CONFIG_LCD_NOGETRUN
  707. else
  708. {
  709. lcderr("ERROR: Unsupported LCD type\n");
  710. ret = -ENODEV;
  711. }
  712. #endif
  713. /* De-select the LCD */
  714. lcd->deselect(lcd);
  715. return ret;
  716. }
  717. /**************************************************************************************
  718. * Public Functions
  719. **************************************************************************************/
  720. /**************************************************************************************
  721. * Name: mio283qt2_lcdinitialize
  722. *
  723. * Description:
  724. * Initialize the LCD video hardware. The initial state of the LCD is fully
  725. * initialized, display memory cleared, and the LCD ready to use, but with the power
  726. * setting at 0 (full off).
  727. *
  728. **************************************************************************************/
  729. FAR struct lcd_dev_s *mio283qt2_lcdinitialize(FAR struct mio283qt2_lcd_s *lcd)
  730. {
  731. int ret;
  732. lcdinfo("Initializing\n");
  733. /* If we ccould support multiple MIO283QT2 devices, this is where we would allocate
  734. * a new driver data structure... but we can't. Why not? Because of a bad should
  735. * the form of the getrun() and putrun methods.
  736. */
  737. FAR struct mio283qt2_dev_s *priv = &g_lcddev;
  738. /* Initialize the driver data structure */
  739. priv->dev.getvideoinfo = mio283qt2_getvideoinfo;
  740. priv->dev.getplaneinfo = mio283qt2_getplaneinfo;
  741. priv->dev.getpower = mio283qt2_getpower;
  742. priv->dev.setpower = mio283qt2_setpower;
  743. priv->dev.getcontrast = mio283qt2_getcontrast;
  744. priv->dev.setcontrast = mio283qt2_setcontrast;
  745. priv->lcd = lcd;
  746. /* Configure and enable LCD */
  747. ret = mio283qt2_hwinitialize(priv);
  748. if (ret == OK)
  749. {
  750. /* Clear the display (setting it to the color 0=black) */
  751. mio283qt2_clear(&priv->dev, 0);
  752. /* Turn the display off */
  753. mio283qt2_poweroff(lcd);
  754. return &g_lcddev.dev;
  755. }
  756. return NULL;
  757. }
  758. /**************************************************************************************
  759. * Name: mio283qt2_clear
  760. *
  761. * Description:
  762. * This is a non-standard LCD interface just for the stm3240g-EVAL board. Because
  763. * of the various rotations, clearing the display in the normal way by writing a
  764. * sequences of runs that covers the entire display can be very slow. Here the
  765. * display is cleared by simply setting all GRAM memory to the specified color.
  766. *
  767. **************************************************************************************/
  768. void mio283qt2_clear(FAR struct lcd_dev_s *dev, uint16_t color)
  769. {
  770. FAR struct mio283qt2_dev_s *priv = (FAR struct mio283qt2_dev_s *)dev;
  771. FAR struct mio283qt2_lcd_s *lcd = priv->lcd;
  772. uint32_t i;
  773. /* Select the LCD and set the drawring area */
  774. lcd->select(lcd);
  775. mio283qt2_setarea(lcd, 0, 0, (MIO283QT2_XRES-1), (MIO283QT2_YRES-1));
  776. /* Prepare to write GRAM data */
  777. mio283qt2_gramselect(lcd);
  778. /* Copy color into all of GRAM. Orientation does not matter in this case. */
  779. for (i = 0; i < MIO283QT2_XRES * MIO283QT2_YRES; i++)
  780. {
  781. mio283qt2_gramwrite(lcd, color);
  782. }
  783. /* De-select the LCD */
  784. lcd->deselect(lcd);
  785. }
  786. #endif /* CONFIG_LCD_MIO283QT2 */