mio283qt2.c 32 KB

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