panel_device_ramless.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <device.h>
  7. #include <tracing/tracing.h>
  8. #include "panel_device.h"
  9. #include <logging/log.h>
  10. LOG_MODULE_REGISTER(lcd_panel, CONFIG_DISPLAY_LOG_LEVEL);
  11. /*********************
  12. * DEFINES
  13. *********************/
  14. #if CONFIG_PANEL_PORT_TYPE != DISPLAY_PORT_QSPI_SYNC
  15. # error "only support QSPI sync mode"
  16. #endif
  17. #if CONFIG_PANEL_HOR_RES != CONFIG_PANEL_TIMING_HACTIVE
  18. # error "CONFIG_PANEL_HOR_RES must equal to CONFIG_PANEL_TIMING_HACTIVE"
  19. #endif
  20. #if CONFIG_PANEL_VER_RES != CONFIG_PANEL_TIMING_VACTIVE
  21. # error "CONFIG_PANEL_VER_RES must equal to CONFIG_PANEL_TIMING_VACTIVE"
  22. #endif
  23. #if defined(CONFIG_PANEL_TE_GPIO)
  24. # error "CONFIG_PANEL_TE_GPIO must not exist"
  25. #endif
  26. #define ESD_CHECK_COUNT \
  27. (CONFIG_PANEL_ESD_CHECK_PERIOD * CONFIG_PANEL_TIMING_REFRESH_RATE_HZ / 1000u)
  28. /**********************
  29. * TYPEDEFS
  30. **********************/
  31. /**********************
  32. * STATIC PROTOTYPES
  33. **********************/
  34. static int _lcd_panel_blanking_on(const struct device *dev);
  35. static int _lcd_panel_blanking_off(const struct device *dev);
  36. static int _lcd_panel_set_orientation(const struct device *dev,
  37. const enum display_orientation orientation);
  38. static int _panel_te_set_enable(const struct device *dev, bool enabled);
  39. __unused static int _panel_pm_early_suspend(const struct device *dev, bool in_turnoff);
  40. #if ESD_CHECK_COUNT > 0
  41. static void _panel_pm_suspend_handler(struct k_work *work);
  42. #endif
  43. static int _panel_pm_late_resume(const struct device *dev);
  44. static void _panel_pm_resume_handler(struct k_work *work);
  45. /**********************
  46. * STATIC VARIABLES
  47. **********************/
  48. #ifdef CONFIG_LCD_WORK_QUEUE
  49. K_THREAD_STACK_DEFINE(pm_workq_stack, CONFIG_LCD_WORK_Q_STACK_SIZE);
  50. #endif
  51. static const struct lcd_panel_config *const lcd_panel_configs[] = {
  52. #ifdef CONFIG_PANEL_ST77903
  53. &lcd_panel_st77903_config,
  54. #endif
  55. };
  56. /**********************
  57. * MACROS
  58. **********************/
  59. /**********************
  60. * STATIC FUNCTIONS
  61. **********************/
  62. static void _panel_reset(const struct device *dev)
  63. {
  64. struct lcd_panel_data *data = dev->data;
  65. const struct lcd_panel_config *config = data->config;
  66. const struct lcd_panel_pincfg *pincfg = dev->config;
  67. display_controller_enable(data->lcdc_dev, &config->videoport);
  68. display_controller_set_mode(data->lcdc_dev, &config->videomode);
  69. LOG_DBG("Resetting display");
  70. #ifdef CONFIG_PANEL_RESET_GPIO
  71. gpio_pin_set(data->reset_gpio, pincfg->reset_cfg.gpion, 1);
  72. #endif
  73. #ifdef CONFIG_PANEL_POWER_GPIO
  74. gpio_pin_set(data->power_gpio, pincfg->power_cfg.gpion, 1);
  75. #endif
  76. #ifdef CONFIG_PANEL_POWER1_GPIO
  77. gpio_pin_set(data->power1_gpio, pincfg->power1_cfg.gpion, 1);
  78. #endif
  79. #ifdef CONFIG_PANEL_RESET_GPIO
  80. k_msleep(config->tw_reset);
  81. gpio_pin_set(data->reset_gpio, pincfg->reset_cfg.gpion, 0);
  82. k_msleep(config->ts_reset);
  83. #endif
  84. }
  85. static int _panel_detect(const struct device *dev)
  86. {
  87. struct lcd_panel_data *data = dev->data;
  88. if (0 == ARRAY_SIZE(lcd_panel_configs))
  89. return -ENODEV;
  90. if (1 == ARRAY_SIZE(lcd_panel_configs)) {
  91. data->config = lcd_panel_configs[0];
  92. return 0;
  93. }
  94. for (int i = 0; i < ARRAY_SIZE(lcd_panel_configs); i++) {
  95. data->config = lcd_panel_configs[i];
  96. if (data->config->ops->detect == NULL)
  97. return 0;
  98. _panel_reset(dev);
  99. if (!data->config->ops->detect(dev))
  100. return 0;
  101. }
  102. /* FIXME: fallback to the frist panel */
  103. data->config = lcd_panel_configs[0];
  104. return 0;
  105. }
  106. static void _panel_power_on(const struct device *dev)
  107. {
  108. struct lcd_panel_data *data = dev->data;
  109. const struct lcd_panel_config *config = data->config;
  110. _panel_reset(dev);
  111. if (config->ops->init)
  112. config->ops->init(dev);
  113. /* Display On */
  114. _lcd_panel_blanking_off(dev);
  115. }
  116. static void _panel_power_off(const struct device *dev, bool in_turnoff)
  117. {
  118. struct lcd_panel_data *data = dev->data;
  119. const struct lcd_panel_config *config = data->config;
  120. const struct lcd_panel_pincfg *pincfg = dev->config;
  121. _lcd_panel_blanking_on(dev);
  122. /* FIXME: place it in config->ops->blanking_on() ? */
  123. if (config->td_slpin > 0) {
  124. if (in_turnoff) {
  125. k_busy_wait(config->td_slpin * 1000u);
  126. } else {
  127. k_msleep(config->td_slpin);
  128. }
  129. }
  130. #ifdef CONFIG_PANEL_RESET_GPIO
  131. gpio_pin_set(data->reset_gpio, pincfg->reset_cfg.gpion, 1);
  132. #endif
  133. #ifdef CONFIG_PANEL_POWER_GPIO
  134. gpio_pin_set(data->power_gpio, pincfg->power_cfg.gpion, 0);
  135. #endif
  136. #ifdef CONFIG_PANEL_POWER1_GPIO
  137. gpio_pin_set(data->power1_gpio, pincfg->power1_cfg.gpion, 0);
  138. #endif
  139. }
  140. static void _panel_apply_brightness(const struct device *dev, uint8_t brightness)
  141. {
  142. struct lcd_panel_data *data = dev->data;
  143. const struct lcd_panel_config *config = data->config;
  144. bool ready;
  145. if (data->brightness == brightness)
  146. return;
  147. ready = (data->brightness_delay == 0);
  148. if (!ready) {
  149. data->brightness_delay -= data->first_frame_cplt;
  150. }
  151. if (ready || brightness == 0) {
  152. if (config->ops->set_brightness) {
  153. config->ops->set_brightness(dev, brightness);
  154. } else {
  155. #ifdef CONFIG_PANEL_BACKLIGHT_CTRL
  156. const struct lcd_panel_pincfg *pincfg = dev->config;
  157. #ifdef CONFIG_PANEL_BACKLIGHT_PWM
  158. pwm_pin_set_cycles(data->backlight_dev, pincfg->backlight_cfg.chan, pincfg->backlight_cfg.period,
  159. (uint32_t)brightness * pincfg->backlight_cfg.period / 255, pincfg->backlight_cfg.flag);
  160. #else
  161. gpio_pin_set(data->backlight_dev, pincfg->backlight_cfg.gpion, brightness ? 1 : 0);
  162. #endif
  163. #endif /* CONFIG_PANEL_BACKLIGHT_CTRL */
  164. }
  165. data->brightness = brightness;
  166. }
  167. }
  168. /**********************
  169. * DEVICE DECLARATION
  170. **********************/
  171. DEVICE_DECLARE(lcd_panel);
  172. /**********************
  173. * TE CALLBACK
  174. **********************/
  175. static void _panel_te_handler(void)
  176. {
  177. const struct device *dev = DEVICE_GET(lcd_panel);
  178. struct lcd_panel_data *data = dev->data;
  179. uint32_t timestamp = k_cycle_get_32();
  180. sys_trace_void(SYS_TRACE_ID_VSYNC);
  181. if (data->callback && data->callback->vsync) {
  182. data->callback->vsync(data->callback, timestamp);
  183. }
  184. sys_trace_end_call(SYS_TRACE_ID_VSYNC);
  185. }
  186. static void _panel_te_timer_handler(struct k_timer *timer)
  187. {
  188. _panel_te_handler();
  189. }
  190. static int _panel_te_set_enable(const struct device *dev, bool enabled)
  191. {
  192. struct lcd_panel_data *data = dev->data;
  193. if (enabled) {
  194. k_timer_start(&data->te_timer, K_MSEC(0), K_MSEC(CONFIG_PANEL_TE_SCANLINE));
  195. } else {
  196. k_timer_stop(&data->te_timer);
  197. }
  198. return 0;
  199. }
  200. /**********************
  201. * DE/LCDC CALLBACK
  202. **********************/
  203. static void _panel_start_de_transfer(void *arg)
  204. {
  205. const struct device *dev = arg;
  206. struct lcd_panel_data *data = dev->data;
  207. const struct lcd_panel_config *config = data->config;
  208. int res;
  209. if (config->fb_mem) {
  210. k_panic(); /* should not goto this path */
  211. }
  212. res = display_controller_write_pixels(data->lcdc_dev, config->cmd_ramwr, config->cmd_hsync,
  213. &data->refr_desc, NULL);
  214. if (res) {
  215. LOG_ERR("write pixels failed");
  216. }
  217. }
  218. static void _panel_complete_transfer(void *arg)
  219. {
  220. const struct device *dev = arg;
  221. struct lcd_panel_data *data = dev->data;
  222. const struct lcd_panel_config *config = data->config;
  223. _panel_apply_brightness(dev, data->pending_brightness);
  224. k_timer_start(&data->te_timer, K_MSEC(CONFIG_PANEL_TE_SCANLINE), K_MSEC(0));
  225. #if ESD_CHECK_COUNT > 0
  226. if (++data->esd_check_cnt >= ESD_CHECK_COUNT) {
  227. data->esd_check_cnt = 0;
  228. if (config->ops->check_esd && config->ops->check_esd(dev)) {
  229. LOG_ERR("ESD check failed");
  230. data->pm_changing = 1;
  231. #ifdef CONFIG_LCD_WORK_QUEUE
  232. k_work_submit_to_queue(&data->pm_workq, &data->suspend_work);
  233. k_work_submit_to_queue(&data->pm_workq, &data->resume_work);
  234. #else
  235. k_work_submit(&data->suspend_work);
  236. k_work_submit(&data->resume_work);
  237. #endif
  238. }
  239. }
  240. #endif /* ESD_CHECK_COUNT > 0 */
  241. }
  242. /**********************
  243. * DEVICE API
  244. **********************/
  245. static int _lcd_panel_blanking_on(const struct device *dev)
  246. {
  247. struct lcd_panel_data *data = dev->data;
  248. const struct lcd_panel_config *config = data->config;
  249. #ifdef CONFIG_PM_DEVICE
  250. if (data->pm_state != PM_DEVICE_STATE_ACTIVE &&
  251. data->pm_state != PM_DEVICE_STATE_LOW_POWER)
  252. return -EPERM;
  253. #endif
  254. if (data->disp_on == 0)
  255. return 0;
  256. LOG_INF("display blanking on");
  257. data->disp_on = 0;
  258. data->brightness_delay = CONFIG_PANEL_BRIGHTNESS_DELAY_PERIODS;
  259. _panel_apply_brightness(dev, 0);
  260. if (config->fb_mem == NULL) { /* wait 100 ms timeout */
  261. display_engine_control(data->de_dev, DISPLAY_ENGINE_CTRL_DISPLAY_SYNC_STOP, (void *)(uintptr_t)100, NULL);
  262. }
  263. display_controller_disable(data->lcdc_dev);
  264. _panel_te_set_enable(dev, false);
  265. if (config->ops->blanking_on) {
  266. display_controller_enable(data->lcdc_dev, &config->videoport);
  267. config->ops->blanking_on(dev);
  268. display_controller_disable(data->lcdc_dev);
  269. }
  270. return 0;
  271. }
  272. static int _lcd_panel_blanking_off(const struct device *dev)
  273. {
  274. struct lcd_panel_data *data = dev->data;
  275. const struct lcd_panel_config *config = data->config;
  276. #ifdef CONFIG_PM_DEVICE
  277. if (data->pm_state != PM_DEVICE_STATE_ACTIVE &&
  278. data->pm_state != PM_DEVICE_STATE_LOW_POWER)
  279. return -EPERM;
  280. #endif
  281. if (data->disp_on == 1)
  282. return 0;
  283. LOG_INF("display blanking off");
  284. display_controller_enable(data->lcdc_dev, &config->videoport);
  285. display_controller_set_mode(data->lcdc_dev, &config->videomode);
  286. data->disp_on = 1;
  287. if (config->ops->blanking_off)
  288. config->ops->blanking_off(dev);
  289. if (config->fb_mem) {
  290. /* initialize the fb memory to black color */
  291. memset(config->fb_mem, 0, data->refr_desc.buf_size);
  292. /* start lcd */
  293. display_controller_set_source(data->lcdc_dev, DISPLAY_CONTROLLER_SOURCE_DMA, NULL);
  294. int res = display_controller_write_pixels(data->lcdc_dev, config->cmd_ramwr,
  295. config->cmd_hsync, &data->refr_desc, config->fb_mem);
  296. if (res) {
  297. LOG_ERR("write pixels failed");
  298. }
  299. } else {
  300. /* source from DE */
  301. display_controller_set_source(data->lcdc_dev, DISPLAY_CONTROLLER_SOURCE_ENGINE, NULL);
  302. /* refresh not started yet, simply use k_timer as te signal */
  303. _panel_te_set_enable(dev, true);
  304. }
  305. return 0;
  306. }
  307. static int _lcd_panel_read(const struct device *dev,
  308. const uint16_t x,
  309. const uint16_t y,
  310. const struct display_buffer_descriptor *desc,
  311. void *buf)
  312. {
  313. return -ENOTSUP;
  314. }
  315. static int _lcd_panel_write(const struct device *dev,
  316. const uint16_t x,
  317. const uint16_t y,
  318. const struct display_buffer_descriptor *desc,
  319. const void *buf)
  320. {
  321. return -ENOTSUP;
  322. }
  323. static void *_lcd_panel_get_framebuffer(const struct device *dev)
  324. {
  325. struct lcd_panel_data *data = dev->data;
  326. return data->config->fb_mem;
  327. }
  328. static int _lcd_panel_set_brightness(const struct device *dev,
  329. const uint8_t brightness)
  330. {
  331. struct lcd_panel_data *data = dev->data;
  332. if (data->disp_on == 0)
  333. return -EPERM;
  334. if (brightness == data->pending_brightness)
  335. return 0;
  336. LOG_INF("display set_brightness %u", brightness);
  337. data->pending_brightness = brightness;
  338. /* delayed set in TE interrupt handler */
  339. return 0;
  340. }
  341. static int _lcd_panel_set_contrast(const struct device *dev, const uint8_t contrast)
  342. {
  343. return -ENOTSUP;
  344. }
  345. static void _lcd_panel_get_capabilities(const struct device *dev,
  346. struct display_capabilities *capabilities)
  347. {
  348. struct lcd_panel_data *data = dev->data;
  349. const struct lcd_panel_config *config = data->config;
  350. memset(capabilities, 0, sizeof(struct display_capabilities));
  351. capabilities->x_resolution = CONFIG_PANEL_HOR_RES;
  352. capabilities->y_resolution = CONFIG_PANEL_VER_RES;
  353. capabilities->refresh_rate = config->videomode.refresh_rate;
  354. capabilities->supported_pixel_formats = SUPPORTED_PANEL_PIXEL_FORMATS;
  355. capabilities->current_pixel_format = config->videomode.pixel_format;
  356. capabilities->current_orientation = (enum display_orientation)(CONFIG_PANEL_ROTATION / 90);
  357. capabilities->screen_info = (CONFIG_PANEL_ROUND_SHAPE ? SCREEN_INFO_ROUND_SHAPE : 0) |
  358. SCREEN_INFO_ZERO_BUFFER | SCREEN_INFO_X_ALIGNMENT_WIDTH |
  359. SCREEN_INFO_Y_ALIGNMENT_HEIGHT;
  360. }
  361. static int _lcd_panel_set_pixel_format(const struct device *dev,
  362. const enum display_pixel_format pixel_format)
  363. {
  364. struct lcd_panel_data *data = dev->data;
  365. const struct lcd_panel_config *config = data->config;
  366. if (pixel_format == config->videomode.pixel_format) {
  367. return 0;
  368. }
  369. LOG_ERR("Pixel format change not implemented");
  370. return -ENOTSUP;
  371. }
  372. static int _lcd_panel_set_orientation(const struct device *dev,
  373. const enum display_orientation orientation)
  374. {
  375. enum display_orientation current_orientation =
  376. (enum display_orientation)(CONFIG_PANEL_ROTATION / 90);
  377. return (orientation == current_orientation) ? 0 : -ENOTSUP;
  378. }
  379. static int _lcd_panel_register_callback(const struct device *dev,
  380. const struct display_callback *callback)
  381. {
  382. struct lcd_panel_data *data = dev->data;
  383. if (data->callback == NULL) {
  384. data->callback = callback;
  385. return 0;
  386. }
  387. return -EBUSY;
  388. }
  389. static int _lcd_panel_unregister_callback(const struct device *dev,
  390. const struct display_callback *callback)
  391. {
  392. struct lcd_panel_data *data = dev->data;
  393. if (data->callback == callback) {
  394. data->callback = NULL;
  395. return 0;
  396. }
  397. return -EINVAL;
  398. }
  399. static int _lcd_panel_init(const struct device *dev)
  400. {
  401. struct lcd_panel_data *data = dev->data;
  402. const struct lcd_panel_pincfg *pincfg = dev->config;
  403. #ifdef CONFIG_PANEL_BACKLIGHT_PWM
  404. data->backlight_dev = device_get_binding(pincfg->backlight_cfg.dev_name);
  405. if (data->backlight_dev == NULL) {
  406. LOG_ERR("Couldn't find pwm device\n");
  407. return -ENODEV;
  408. }
  409. pwm_pin_set_cycles(data->backlight_dev, pincfg->backlight_cfg.chan,
  410. pincfg->backlight_cfg.period, 0, pincfg->backlight_cfg.flag);
  411. #elif defined(CONFIG_PANEL_BACKLIGHT_GPIO)
  412. data->backlight_dev = device_get_binding(pincfg->backlight_cfg.gpio_dev_name);
  413. if (data->backlight_dev == NULL) {
  414. LOG_ERR("Couldn't find backlight pin");
  415. return -ENODEV;
  416. }
  417. if (gpio_pin_configure(data->backlight_dev, pincfg->backlight_cfg.gpion,
  418. GPIO_OUTPUT_INACTIVE | pincfg->backlight_cfg.flag)) {
  419. LOG_ERR("Couldn't configure backlight pin");
  420. return -ENODEV;
  421. }
  422. #endif /* CONFIG_PANEL_BACKLIGHT_PWM */
  423. #ifdef CONFIG_PANEL_RESET_GPIO
  424. data->reset_gpio = device_get_binding(pincfg->reset_cfg.gpio_dev_name);
  425. if (data->reset_gpio == NULL) {
  426. LOG_ERR("Couldn't find reset pin");
  427. return -ENODEV;
  428. }
  429. if (gpio_pin_configure(data->reset_gpio, pincfg->reset_cfg.gpion,
  430. GPIO_OUTPUT_ACTIVE | pincfg->reset_cfg.flag)) {
  431. LOG_ERR("Couldn't configure reset pin");
  432. return -ENODEV;
  433. }
  434. #endif /* CONFIG_PANEL_RESET_GPIO */
  435. #ifdef CONFIG_PANEL_POWER_GPIO
  436. data->power_gpio = device_get_binding(pincfg->power_cfg.gpio_dev_name);
  437. if (data->power_gpio == NULL) {
  438. LOG_ERR("Couldn't find power pin");
  439. return -ENODEV;
  440. }
  441. if (gpio_pin_configure(data->power_gpio, pincfg->power_cfg.gpion,
  442. GPIO_OUTPUT_INACTIVE | pincfg->power_cfg.flag)) {
  443. LOG_ERR("Couldn't configure power pin");
  444. return -ENODEV;
  445. }
  446. #endif /* CONFIG_PANEL_POWER_GPIO */
  447. #ifdef CONFIG_PANEL_POWER1_GPIO
  448. data->power1_gpio = device_get_binding(pincfg->power1_cfg.gpio_dev_name);
  449. if (data->power1_gpio == NULL) {
  450. LOG_ERR("Couldn't find power1 pin");
  451. return -ENODEV;
  452. }
  453. if (gpio_pin_configure(data->power1_gpio, pincfg->power1_cfg.gpion,
  454. GPIO_OUTPUT_INACTIVE | pincfg->power1_cfg.flag)) {
  455. LOG_ERR("Couldn't configure power1 pin");
  456. return -ENODEV;
  457. }
  458. #endif /* CONFIG_PANEL_POWER1_GPIO */
  459. k_timer_init(&data->te_timer, _panel_te_timer_handler, NULL);
  460. data->lcdc_dev = device_get_binding(CONFIG_LCDC_DEV_NAME);
  461. if (data->lcdc_dev == NULL) {
  462. LOG_ERR("Could not get LCD controller device");
  463. return -EPERM;
  464. }
  465. display_controller_control(data->lcdc_dev,
  466. DISPLAY_CONTROLLER_CTRL_COMPLETE_CB, _panel_complete_transfer, (void *)dev);
  467. if (_panel_detect(dev)) {
  468. LOG_ERR("No panel connected");
  469. return -ENODEV;
  470. }
  471. data->de_dev = device_get_binding(CONFIG_DISPLAY_ENGINE_DEV_NAME);
  472. if (data->de_dev == NULL) {
  473. LOG_ERR("Could not get display engine device");
  474. return -ENODEV;
  475. }
  476. display_engine_control(data->de_dev,
  477. DISPLAY_ENGINE_CTRL_DISPLAY_PORT, (void *)&data->config->videoport, NULL);
  478. display_engine_control(data->de_dev,
  479. DISPLAY_ENGINE_CTRL_DISPLAY_MODE, (void *)&data->config->videomode, NULL);
  480. display_engine_control(data->de_dev,
  481. DISPLAY_ENGINE_CTRL_DISPLAY_START_CB, _panel_start_de_transfer, (void *)dev);
  482. data->refr_desc.pixel_format = data->config->videomode.pixel_format;
  483. data->refr_desc.width = CONFIG_PANEL_HOR_RES;
  484. data->refr_desc.height = CONFIG_PANEL_VER_RES;
  485. data->refr_desc.pitch = CONFIG_PANEL_HOR_RES *
  486. display_format_get_bits_per_pixel(data->refr_desc.pixel_format) / 8;
  487. data->refr_desc.buf_size = data->refr_desc.height * data->refr_desc.pitch;
  488. data->disp_on = 0;
  489. data->pending_brightness = CONFIG_PANEL_BRIGHTNESS;
  490. data->brightness_delay = CONFIG_PANEL_BRIGHTNESS_DELAY_PERIODS;
  491. data->pm_state = PM_DEVICE_STATE_SUSPENDED;
  492. #if ESD_CHECK_COUNT > 0
  493. k_work_init(&data->suspend_work, _panel_pm_suspend_handler);
  494. #endif
  495. k_work_init(&data->resume_work, _panel_pm_resume_handler);
  496. #ifdef CONFIG_LCD_WORK_QUEUE
  497. k_work_queue_start(&data->pm_workq, pm_workq_stack, K_THREAD_STACK_SIZEOF(pm_workq_stack), 6, NULL);
  498. #endif
  499. _panel_pm_late_resume(dev);
  500. return 0;
  501. }
  502. /**********************
  503. * POWER MANAGEMENT
  504. **********************/
  505. static void _panel_pm_notify(struct lcd_panel_data *data, uint32_t pm_action)
  506. {
  507. if (data->callback && data->callback->pm_notify) {
  508. data->callback->pm_notify(data->callback, pm_action);
  509. }
  510. }
  511. static int _panel_pm_early_suspend(const struct device *dev, bool in_turnoff)
  512. {
  513. struct lcd_panel_data *data = dev->data;
  514. if (data->pm_state != PM_DEVICE_STATE_ACTIVE &&
  515. data->pm_state != PM_DEVICE_STATE_LOW_POWER) {
  516. return -EPERM;
  517. }
  518. LOG_INF("panel early-suspend");
  519. _panel_pm_notify(data, PM_DEVICE_ACTION_EARLY_SUSPEND);
  520. _panel_power_off(dev, in_turnoff);
  521. #if ESD_CHECK_COUNT > 0
  522. data->esd_check_cnt = 0;
  523. #endif
  524. data->pm_state = PM_DEVICE_STATE_SUSPENDED;
  525. return 0;
  526. }
  527. #if ESD_CHECK_COUNT > 0
  528. static void _panel_pm_suspend_handler(struct k_work *work)
  529. {
  530. const struct device *dev = DEVICE_GET(lcd_panel);
  531. lcd_panel_wake_lock();
  532. _panel_pm_early_suspend(dev, false);
  533. lcd_panel_wake_unlock();
  534. }
  535. #endif
  536. static int _panel_pm_late_resume(const struct device *dev)
  537. {
  538. struct lcd_panel_data *data = dev->data;
  539. if (data->pm_state != PM_DEVICE_STATE_SUSPENDED) {
  540. return -EPERM;
  541. }
  542. LOG_INF("panel late-resume");
  543. #ifdef CONFIG_LCD_WORK_QUEUE
  544. k_work_submit_to_queue(&data->pm_workq, &data->resume_work);
  545. #else
  546. k_work_submit(&data->resume_work);
  547. #endif
  548. return 0;
  549. }
  550. static void _panel_pm_resume_handler(struct k_work *work)
  551. {
  552. const struct device *dev = DEVICE_GET(lcd_panel);
  553. struct lcd_panel_data *data = dev->data;
  554. LOG_INF("panel resuming");
  555. data->pm_state = PM_DEVICE_STATE_ACTIVE;
  556. data->pm_changing = 0;
  557. lcd_panel_wake_lock();
  558. _panel_power_on(dev);
  559. _panel_pm_notify(data, PM_DEVICE_ACTION_LATE_RESUME);
  560. lcd_panel_wake_unlock();
  561. LOG_INF("panel active");
  562. }
  563. #ifdef CONFIG_PM_DEVICE
  564. static int _lcd_panel_pm_control(const struct device *dev, enum pm_device_action action)
  565. {
  566. struct lcd_panel_data *data = dev->data;
  567. int ret = 0;
  568. if (data->pm_state == PM_DEVICE_STATE_OFF)
  569. return -EPERM;
  570. switch (action) {
  571. case PM_DEVICE_ACTION_EARLY_SUSPEND:
  572. ret = _panel_pm_early_suspend(dev, false);
  573. board_lcd_suspend(false, true);
  574. break;
  575. case PM_DEVICE_ACTION_LATE_RESUME:
  576. board_lcd_resume(false, true);
  577. ret = _panel_pm_late_resume(dev);
  578. break;
  579. case PM_DEVICE_ACTION_SUSPEND:
  580. if (data->pm_changing || data->pm_state == PM_DEVICE_STATE_ACTIVE) {
  581. ret = -EPERM;
  582. }
  583. break;
  584. case PM_DEVICE_ACTION_TURN_OFF:
  585. _panel_pm_early_suspend(dev, true);
  586. data->pm_state = PM_DEVICE_STATE_OFF;
  587. LOG_INF("panel turn-off");
  588. break;
  589. default:
  590. break;
  591. }
  592. return ret;
  593. }
  594. #endif /* CONFIG_PM_DEVICE */
  595. /**********************
  596. * DEVICE DEFINITION
  597. **********************/
  598. static const struct display_driver_api lcd_panel_driver_api = {
  599. .blanking_on = _lcd_panel_blanking_on,
  600. .blanking_off = _lcd_panel_blanking_off,
  601. .write = _lcd_panel_write,
  602. .read = _lcd_panel_read,
  603. .get_framebuffer = _lcd_panel_get_framebuffer,
  604. .set_brightness = _lcd_panel_set_brightness,
  605. .set_contrast = _lcd_panel_set_contrast,
  606. .get_capabilities = _lcd_panel_get_capabilities,
  607. .set_pixel_format = _lcd_panel_set_pixel_format,
  608. .set_orientation = _lcd_panel_set_orientation,
  609. .register_callback = _lcd_panel_register_callback,
  610. .unregister_callback = _lcd_panel_unregister_callback,
  611. };
  612. static const struct lcd_panel_pincfg lcd_panel_pin_config = {
  613. #ifdef CONFIG_PANEL_POWER_GPIO
  614. .power_cfg = CONFIG_PANEL_POWER_GPIO,
  615. #endif
  616. #ifdef CONFIG_PANEL_POWER1_GPIO
  617. .power1_cfg = CONFIG_PANEL_POWER1_GPIO,
  618. #endif
  619. #ifdef CONFIG_PANEL_RESET_GPIO
  620. .reset_cfg = CONFIG_PANEL_RESET_GPIO,
  621. #endif
  622. #ifdef CONFIG_PANEL_TE_GPIO
  623. .te_cfg = CONFIG_PANEL_TE_GPIO,
  624. #endif
  625. #ifdef CONFIG_PANEL_BACKLIGHT_PWM
  626. .backlight_cfg = CONFIG_PANEL_BACKLIGHT_PWM,
  627. #elif defined(CONFIG_PANEL_BACKLIGHT_GPIO)
  628. .backlight_cfg = CONFIG_PANEL_BACKLIGHT_GPIO,
  629. #endif
  630. };
  631. static struct lcd_panel_data lcd_panel_data;
  632. #if IS_ENABLED(CONFIG_PANEL)
  633. DEVICE_DEFINE(lcd_panel, CONFIG_LCD_DISPLAY_DEV_NAME, _lcd_panel_init, _lcd_panel_pm_control,
  634. &lcd_panel_data, &lcd_panel_pin_config, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY,
  635. &lcd_panel_driver_api);
  636. #endif