panel_device.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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 <drivers/hrtimer.h>
  10. #include <logging/log.h>
  11. LOG_MODULE_REGISTER(lcd_panel, CONFIG_DISPLAY_LOG_LEVEL);
  12. /*********************
  13. * DEFINES
  14. *********************/
  15. /* time to wait between pixel transfer and next command */
  16. #ifdef CONFIG_PANEL_FULL_SCREEN_OPT_AREA
  17. # define CONFIG_PANEL_PIXEL_WR_DELAY_US (0)
  18. #else
  19. # define CONFIG_PANEL_PIXEL_WR_DELAY_US (0)
  20. #endif
  21. /**********************
  22. * TYPEDEFS
  23. **********************/
  24. /**********************
  25. * STATIC PROTOTYPES
  26. **********************/
  27. static int _lcd_panel_blanking_on(const struct device *dev);
  28. static int _lcd_panel_blanking_off(const struct device *dev);
  29. static int _lcd_panel_set_orientation(const struct device *dev,
  30. const enum display_orientation orientation);
  31. static int _panel_te_set_enable(const struct device *dev, bool enabled);
  32. static void _panel_submit_esd_check_work(struct lcd_panel_data *data);
  33. static void _panel_cancel_esd_check_work(struct lcd_panel_data *data);
  34. __unused static int _panel_pm_early_suspend(const struct device *dev, bool in_turnoff);
  35. static int _panel_pm_late_resume(const struct device *dev);
  36. static void _panel_pm_resume_handler(struct k_work *work);
  37. /**********************
  38. * STATIC VARIABLES
  39. **********************/
  40. #ifdef CONFIG_LCD_WORK_QUEUE
  41. K_THREAD_STACK_DEFINE(pm_workq_stack, CONFIG_LCD_WORK_Q_STACK_SIZE);
  42. #endif
  43. static const struct lcd_panel_config *const lcd_panel_configs[] = {
  44. #ifdef CONFIG_PANEL_ER76288A
  45. &lcd_panel_er76288a_config,
  46. #endif
  47. #ifdef CONFIG_PANEL_FT2308
  48. &lcd_panel_ft2308_config,
  49. #endif
  50. #ifdef CONFIG_PANEL_GC9C01
  51. &lcd_panel_gc9c01_config,
  52. #endif
  53. #ifdef CONFIG_PANEL_ICNA3310B
  54. &lcd_panel_icna3310b_config,
  55. #endif
  56. #ifdef CONFIG_PANEL_ICNA3311
  57. &lcd_panel_icna3311_config,
  58. #endif
  59. #ifdef CONFIG_PANEL_RM690B0
  60. &lcd_panel_rm690b0_config,
  61. #endif
  62. #ifdef CONFIG_PANEL_RM69090
  63. &lcd_panel_rm69090_config,
  64. #endif
  65. #ifdef CONFIG_PANEL_SH8601Z0
  66. &lcd_panel_sh8601z0_config,
  67. #endif
  68. #ifdef CONFIG_PANEL_ST77916
  69. &lcd_panel_st77916_config,
  70. #endif
  71. /* TR-panels */
  72. #ifdef CONFIG_PANEL_LPM015M135A
  73. &lcd_panel_lpm015m135a_config,
  74. #endif
  75. };
  76. static bool bDevEnterSuspend = false; // 0: not enter suspend, 1: enter suspend
  77. /**********************
  78. * MACROS
  79. **********************/
  80. /**********************
  81. * STATIC FUNCTIONS
  82. **********************/
  83. #if IS_TR_PANEL
  84. static inline void _panel_reset_pin_set(const struct device *dev, bool active)
  85. {
  86. #ifdef CONFIG_PANEL_RESET_GPIO
  87. struct lcd_panel_data *data = dev->data;
  88. const struct lcd_panel_pincfg *pincfg = dev->config;
  89. gpio_pin_set(data->reset_gpio, pincfg->reset_cfg.gpion, active ? 1 : 0);
  90. #endif
  91. }
  92. #endif /* IS_TR_PANEL */
  93. static void _panel_reset(const struct device *dev)
  94. {
  95. struct lcd_panel_data *data = dev->data;
  96. const struct lcd_panel_config *config = data->config;
  97. const struct lcd_panel_pincfg *pincfg = dev->config;
  98. display_controller_enable(data->lcdc_dev, &config->videoport);
  99. display_controller_set_mode(data->lcdc_dev, &config->videomode);
  100. LOG_DBG("Resetting display");
  101. #ifdef CONFIG_PANEL_RESET_GPIO
  102. gpio_pin_set(data->reset_gpio, pincfg->reset_cfg.gpion, 1);
  103. #endif
  104. #ifdef CONFIG_PANEL_POWER_GPIO
  105. gpio_pin_set(data->power_gpio, pincfg->power_cfg.gpion, 1);
  106. #endif
  107. #ifdef CONFIG_PANEL_POWER1_GPIO
  108. //gpio_pin_set(data->power1_gpio, pincfg->power1_cfg.gpion, 1);
  109. #endif
  110. #if IS_TR_PANEL == 0
  111. #ifdef CONFIG_PANEL_RESET_GPIO
  112. k_msleep(config->tw_reset);
  113. gpio_pin_set(data->reset_gpio, pincfg->reset_cfg.gpion, 0);
  114. k_msleep(config->ts_reset);
  115. #endif
  116. #endif /* IS_TR_PANEL == 0 */
  117. }
  118. static int _panel_detect(const struct device *dev)
  119. {
  120. struct lcd_panel_data *data = dev->data;
  121. if (0 == ARRAY_SIZE(lcd_panel_configs))
  122. return -ENODEV;
  123. if (1 == ARRAY_SIZE(lcd_panel_configs)) {
  124. data->config = lcd_panel_configs[0];
  125. return 0;
  126. }
  127. for (int i = 0; i < ARRAY_SIZE(lcd_panel_configs); i++) {
  128. data->config = lcd_panel_configs[i];
  129. if (data->config->ops->detect == NULL)
  130. return 0;
  131. _panel_reset(dev);
  132. if (!data->config->ops->detect(dev))
  133. return 0;
  134. }
  135. /* FIXME: fallback to the frist panel */
  136. data->config = lcd_panel_configs[0];
  137. return 0;
  138. }
  139. static void _panel_power_on(const struct device *dev)
  140. {
  141. struct lcd_panel_data *data = dev->data;
  142. const struct lcd_panel_config *config = data->config;
  143. _panel_reset(dev);
  144. if (config->ops->init)
  145. config->ops->init(dev);
  146. /* Display On */
  147. _lcd_panel_blanking_off(dev);
  148. }
  149. static void _panel_power_off(const struct device *dev, bool in_turnoff)
  150. {
  151. struct lcd_panel_data *data = dev->data;
  152. const struct lcd_panel_config *config = data->config;
  153. const struct lcd_panel_pincfg *pincfg = dev->config;
  154. _lcd_panel_blanking_on(dev);
  155. /* FIXME: place it in config->ops->blanking_on() ? */
  156. if (config->td_slpin > 0) {
  157. if (in_turnoff) {
  158. k_busy_wait(config->td_slpin * 1000u);
  159. } else {
  160. k_msleep(config->td_slpin);
  161. }
  162. }
  163. #ifdef CONFIG_PANEL_RESET_GPIO
  164. gpio_pin_set(data->reset_gpio, pincfg->reset_cfg.gpion, 1);
  165. #endif
  166. #ifdef CONFIG_PANEL_POWER_GPIO
  167. gpio_pin_set(data->power_gpio, pincfg->power_cfg.gpion, 0);
  168. #endif
  169. #ifdef CONFIG_PANEL_POWER1_GPIO
  170. gpio_pin_set(data->power1_gpio, pincfg->power1_cfg.gpion, 0);
  171. #endif
  172. }
  173. static void _panel_apply_brightness(const struct device *dev, uint8_t brightness)
  174. {
  175. struct lcd_panel_data *data = dev->data;
  176. const struct lcd_panel_config *config = data->config;
  177. bool ready;
  178. if (data->brightness == brightness)
  179. return;
  180. ready = (data->brightness_delay == 0) && (data->first_frame_cplt > 0);
  181. if (!ready) {
  182. data->brightness_delay -= data->first_frame_cplt;
  183. }
  184. if (ready || brightness == 0) {
  185. if (config->ops->set_brightness) {
  186. config->ops->set_brightness(dev, brightness);
  187. } else {
  188. #ifdef CONFIG_PANEL_BACKLIGHT_CTRL
  189. const struct lcd_panel_pincfg *pincfg = dev->config;
  190. #ifdef CONFIG_PANEL_BACKLIGHT_PWM
  191. pwm_pin_set_cycles(data->backlight_dev, pincfg->backlight_cfg.chan, pincfg->backlight_cfg.period,
  192. (uint32_t)brightness * pincfg->backlight_cfg.period / 255, pincfg->backlight_cfg.flag);
  193. #else
  194. gpio_pin_set(data->backlight_dev, pincfg->backlight_cfg.gpion, brightness ? 1 : 0);
  195. #endif
  196. #endif /* CONFIG_PANEL_BACKLIGHT_CTRL */
  197. }
  198. data->brightness = brightness;
  199. }
  200. }
  201. static void _panel_apply_post_change(const struct device *dev)
  202. {
  203. struct lcd_panel_data *data = dev->data;
  204. _panel_apply_brightness(dev, data->pending_brightness);
  205. }
  206. /**********************
  207. * DEVICE DECLARATION
  208. **********************/
  209. DEVICE_DECLARE(lcd_panel);
  210. /**********************
  211. * ESD CHECK
  212. **********************/
  213. #if CONFIG_PANEL_ESD_CHECK_PERIOD > 0
  214. static void _panel_esd_check_custom(const struct device *dev)
  215. {
  216. struct lcd_panel_data *data = dev->data;
  217. const struct lcd_panel_config *config = data->config;
  218. if (config->ops->check_esd) {
  219. data->esd_check_pended = data->transfering;
  220. if (!data->transfering) {
  221. int res = config->ops->check_esd(dev);
  222. data->esd_check_failed = res ? 1 : 0;
  223. }
  224. }
  225. }
  226. static void _panel_esd_check_handler(struct k_work *work)
  227. {
  228. const struct device *dev = DEVICE_GET(lcd_panel);
  229. struct lcd_panel_data *data = dev->data;
  230. const struct lcd_panel_config *config = data->config;
  231. if (config->ops->check_esd) {
  232. unsigned int key = irq_lock();
  233. _panel_esd_check_custom(dev);
  234. irq_unlock(key);
  235. }
  236. if (data->esd_check_failed) {
  237. LOG_ERR("ESD check fail, reboot panel");
  238. goto fail_exit;
  239. }
  240. if (!data->te_active) {
  241. LOG_ERR("TE no signal, reboot panel");
  242. goto fail_exit;
  243. }
  244. data->te_active = 0;
  245. _panel_submit_esd_check_work(data);
  246. return;
  247. fail_exit:
  248. lcd_panel_wake_lock();
  249. _panel_pm_early_suspend(dev, false);
  250. _panel_pm_late_resume(dev);
  251. lcd_panel_wake_unlock();
  252. }
  253. #endif /* CONFIG_TPKEY_ESD_CHECK_PERIOD > 0 */
  254. static void _panel_submit_esd_check_work(struct lcd_panel_data *data)
  255. {
  256. #if CONFIG_PANEL_ESD_CHECK_PERIOD > 0
  257. #ifdef CONFIG_LCD_WORK_QUEUE
  258. k_delayed_work_submit_to_queue(&data->pm_workq, &data->esd_check_work,
  259. K_MSEC(CONFIG_PANEL_ESD_CHECK_PERIOD));
  260. #else
  261. k_delayed_work_submit(&data->esd_check_work, K_MSEC(CONFIG_PANEL_ESD_CHECK_PERIOD));
  262. #endif
  263. #endif /* CONFIG_PANEL_ESD_CHECK_PERIOD > 0 */
  264. }
  265. static void _panel_cancel_esd_check_work(struct lcd_panel_data *data)
  266. {
  267. #if CONFIG_PANEL_ESD_CHECK_PERIOD > 0
  268. data->esd_check_pended = 0;
  269. data->esd_check_failed = 0;
  270. k_delayed_work_cancel(&data->esd_check_work);
  271. #endif /* CONFIG_PANEL_ESD_CHECK_PERIOD > 0 */
  272. }
  273. /**********************
  274. * TE CALLBACK
  275. **********************/
  276. static void _panel_te_handler(void)
  277. {
  278. const struct device *dev = DEVICE_GET(lcd_panel);
  279. struct lcd_panel_data *data = dev->data;
  280. uint32_t timestamp = k_cycle_get_32();
  281. sys_trace_void(SYS_TRACE_ID_VSYNC);
  282. data->te_active = 1;
  283. if (data->transfering) {
  284. sys_trace_void(SYS_TRACE_ID_LCD_POST_OVERTIME);
  285. } else {
  286. _panel_apply_post_change(dev);
  287. #if CONFIG_PANEL_ESD_CHECK_PERIOD > 0
  288. if (data->esd_check_pended)
  289. _panel_esd_check_custom(dev);
  290. #endif
  291. }
  292. if (data->callback && data->callback->vsync) {
  293. data->callback->vsync(data->callback, timestamp);
  294. }
  295. sys_trace_end_call(SYS_TRACE_ID_VSYNC);
  296. }
  297. #ifdef CONFIG_PANEL_TE_GPIO
  298. static void _panel_te_gpio_handler(const struct device *port,
  299. struct gpio_callback *cb, gpio_port_pins_t pins)
  300. {
  301. _panel_te_handler();
  302. }
  303. static int _panel_te_set_enable(const struct device *dev, bool enabled)
  304. {
  305. struct lcd_panel_data *data = dev->data;
  306. const struct lcd_panel_pincfg *pincfg = dev->config;
  307. if (gpio_pin_interrupt_configure(data->te_gpio, pincfg->te_cfg.gpion,
  308. enabled ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE)) {
  309. LOG_ERR("Couldn't config te interrupt (en=%d)", enabled);
  310. return -ENODEV;
  311. }
  312. return 0;
  313. }
  314. #else /* CONFIG_PANEL_TE_GPIO */
  315. static void _panel_te_timer_handler(struct k_timer *timer)
  316. {
  317. _panel_te_handler();
  318. }
  319. static int _panel_te_set_enable(const struct device *dev, bool enabled)
  320. {
  321. struct lcd_panel_data *data = dev->data;
  322. if (enabled) {
  323. k_timer_start(&data->te_timer, K_MSEC(0), K_MSEC(CONFIG_PANEL_VSYNC_PERIOD_MS));
  324. } else {
  325. k_timer_stop(&data->te_timer);
  326. }
  327. return 0;
  328. }
  329. #endif /* CONFIG_PANEL_TE_GPIO */
  330. /**********************
  331. * DE/LCDC CALLBACK
  332. **********************/
  333. static void _panel_prepare_de_transfer(void *arg, const display_rect_t *area)
  334. {
  335. const struct device *dev = arg;
  336. struct lcd_panel_data *data = dev->data;
  337. const struct lcd_panel_config *config = data->config;
  338. uint8_t bytes_per_pixel;
  339. assert(data->disp_on == 1 && data->transfering == 0);
  340. sys_trace_u32x4(SYS_TRACE_ID_LCD_POST,
  341. area->x, area->y, area->x + area->w - 1, area->y + area->h - 1);
  342. if (data->pm_state == PM_DEVICE_STATE_LOW_POWER)
  343. board_lcd_resume(true, false);
  344. if (config->ops->write_prepare) {
  345. int res = config->ops->write_prepare(dev, area->x, area->y, area->w, area->h);
  346. if (res) {
  347. LOG_ERR("write area (%u, %u, %u, %u) failed", area->x, area->y, area->w, area->h);
  348. }
  349. }
  350. if (config->videomode.pixel_format == PIXEL_FORMAT_BGR_565) {
  351. bytes_per_pixel = 2;
  352. } else if (config->videomode.pixel_format == PIXEL_FORMAT_RGB_888) {
  353. bytes_per_pixel = 3;
  354. } else {
  355. bytes_per_pixel = 4;
  356. }
  357. data->refr_desc.pixel_format = config->videomode.pixel_format;
  358. data->refr_desc.width = area->w;
  359. data->refr_desc.height = area->h;
  360. data->refr_desc.pitch = area->w * bytes_per_pixel;
  361. data->transfering = 1;
  362. data->transfering_last = (area->y + area->h >= CONFIG_PANEL_VER_RES);
  363. /* source from DE */
  364. display_controller_set_source(data->lcdc_dev, DISPLAY_CONTROLLER_SOURCE_ENGINE, NULL);
  365. }
  366. static void _panel_start_de_transfer(void *arg)
  367. {
  368. const struct device *dev = arg;
  369. struct lcd_panel_data *data = dev->data;
  370. const struct lcd_panel_config *config = data->config;
  371. int res;
  372. #if IS_TR_PANEL
  373. _panel_reset_pin_set(dev, false);
  374. #endif
  375. res = display_controller_write_pixels(data->lcdc_dev, config->cmd_ramwr, DC_INVALID_CMD,
  376. &data->refr_desc, NULL);
  377. if (res) {
  378. LOG_ERR("write pixels failed");
  379. data->transfering = 0;
  380. }
  381. }
  382. static void _panel_complete_transfer(void *arg)
  383. {
  384. const struct device *dev = arg;
  385. struct lcd_panel_data *data = dev->data;
  386. #if IS_TR_PANEL
  387. _panel_reset_pin_set(dev, true);
  388. #endif
  389. if (data->pm_state == PM_DEVICE_STATE_LOW_POWER)
  390. board_lcd_suspend(true, false);
  391. sys_trace_end_call(SYS_TRACE_ID_LCD_POST);
  392. data->transfering = 0;
  393. if (data->transfering_last) {
  394. data->first_frame_cplt = 1;
  395. }
  396. #if CONFIG_PANEL_PIXEL_WR_DELAY_US > 0
  397. if (data->transfering_last == 0) {
  398. k_busy_wait(CONFIG_PANEL_PIXEL_WR_DELAY_US);
  399. }
  400. #endif
  401. if (data->callback && data->callback->complete) {
  402. data->callback->complete(data->callback);
  403. }
  404. }
  405. /**********************
  406. * DEVICE API
  407. **********************/
  408. static int _lcd_panel_blanking_on(const struct device *dev)
  409. {
  410. struct lcd_panel_data *data = dev->data;
  411. const struct lcd_panel_config *config = data->config;
  412. if (data->pm_state != PM_DEVICE_STATE_ACTIVE &&
  413. data->pm_state != PM_DEVICE_STATE_LOW_POWER)
  414. return -EPERM;
  415. if (data->disp_on == 0)
  416. return 0;
  417. LOG_INF("display blanking on");
  418. data->disp_on = 0;
  419. data->in_sleep = 1;
  420. data->first_frame_cplt = 0;
  421. data->brightness_delay = CONFIG_PANEL_BRIGHTNESS_DELAY_PERIODS;
  422. _panel_te_set_enable(dev, false);
  423. _panel_apply_brightness(dev, 0);
  424. if (config->ops->blanking_on)
  425. config->ops->blanking_on(dev);
  426. display_controller_disable(data->lcdc_dev);
  427. return 0;
  428. }
  429. static int _lcd_panel_blanking_off(const struct device *dev)
  430. {
  431. struct lcd_panel_data *data = dev->data;
  432. const struct lcd_panel_config *config = data->config;
  433. if (data->pm_state != PM_DEVICE_STATE_ACTIVE &&
  434. data->pm_state != PM_DEVICE_STATE_LOW_POWER)
  435. return -EPERM;
  436. if (data->disp_on == 1)
  437. return 0;
  438. LOG_INF("display blanking off");
  439. display_controller_enable(data->lcdc_dev, &config->videoport);
  440. display_controller_set_mode(data->lcdc_dev, &config->videomode);
  441. data->disp_on = 1;
  442. if (config->ops->blanking_off)
  443. config->ops->blanking_off(dev);
  444. _panel_te_set_enable(dev, true);
  445. return 0;
  446. }
  447. static int _lcd_panel_read(const struct device *dev,
  448. const uint16_t x,
  449. const uint16_t y,
  450. const struct display_buffer_descriptor *desc,
  451. void *buf)
  452. {
  453. return -ENOTSUP;
  454. }
  455. static int _lcd_panel_write(const struct device *dev,
  456. const uint16_t x,
  457. const uint16_t y,
  458. const struct display_buffer_descriptor *desc,
  459. const void *buf)
  460. {
  461. struct lcd_panel_data *data = dev->data;
  462. const struct lcd_panel_config *config = data->config;
  463. int res;
  464. #if IS_TR_PANEL
  465. if (desc->width != CONFIG_PANEL_HOR_RES || desc->height != CONFIG_PANEL_VER_RES) {
  466. LOG_ERR("TR panel requires full-screen write");
  467. return -EINVAL;
  468. }
  469. #endif
  470. if (data->disp_on == 0 || data->transfering != 0) {
  471. LOG_ERR("disp not ready");
  472. return -EBUSY;
  473. }
  474. sys_trace_u32x4(SYS_TRACE_ID_LCD_POST,
  475. x, y, x + desc->width - 1, y + desc->height - 1);
  476. if (data->pm_state == PM_DEVICE_STATE_LOW_POWER)
  477. board_lcd_resume(true, false);
  478. if (config->ops->write_prepare) {
  479. res = config->ops->write_prepare(dev, x, y, desc->width, desc->height);
  480. if (res) {
  481. LOG_ERR("write area (%u, %u, %u, %u) failed", x, y, desc->width, desc->height);
  482. return res;
  483. }
  484. }
  485. data->transfering = 1;
  486. data->transfering_last = (y + desc->height >= CONFIG_PANEL_VER_RES);
  487. #if IS_TR_PANEL
  488. _panel_reset_pin_set(dev, false);
  489. #endif
  490. /**
  491. * CARE:
  492. * For QSPI (Sync) series interface, RAMWR cmd will send automatically by LCDC.
  493. * For other interfaces, RAMWR cmd must send in write_prepare() ops manually.
  494. */
  495. #if 0
  496. _panel_transmit(dev, DDIC_CMD_RAMWR, NULL, 0);
  497. display_controller_set_source(data->lcdc_dev, DISPLAY_CONTROLLER_SOURCE_MCU, NULL);
  498. res = display_controller_write_pixels(data->lcdc_dev,
  499. config->cmd_ramwc, DC_INVALID_CMD, desc, buf);
  500. #else
  501. display_controller_set_source(data->lcdc_dev, DISPLAY_CONTROLLER_SOURCE_DMA, NULL);
  502. res = display_controller_write_pixels(data->lcdc_dev, config->cmd_ramwr,
  503. DC_INVALID_CMD, desc, buf);
  504. #endif
  505. if (res) {
  506. LOG_ERR("write pixels failed");
  507. data->transfering = 0;
  508. }
  509. return res;
  510. }
  511. static void *_lcd_panel_get_framebuffer(const struct device *dev)
  512. {
  513. return NULL;
  514. }
  515. static int _lcd_panel_set_brightness(const struct device *dev,
  516. const uint8_t brightness)
  517. {
  518. struct lcd_panel_data *data = dev->data;
  519. if (data->disp_on == 0)
  520. return -EPERM;
  521. if (brightness == data->pending_brightness)
  522. return 0;
  523. LOG_INF("display set_brightness %u", brightness);
  524. data->pending_brightness = brightness;
  525. /* delayed set in TE interrupt handler */
  526. return 0;
  527. }
  528. static int _lcd_panel_get_brightness(const struct device *dev)
  529. {
  530. struct lcd_panel_data *data = dev->data;
  531. return data->brightness;
  532. }
  533. static int _lcd_panel_set_contrast(const struct device *dev, const uint8_t contrast)
  534. {
  535. return -ENOTSUP;
  536. }
  537. static void _lcd_panel_get_capabilities(const struct device *dev,
  538. struct display_capabilities *capabilities)
  539. {
  540. struct lcd_panel_data *data = dev->data;
  541. const struct lcd_panel_config *config = data->config;
  542. memset(capabilities, 0, sizeof(struct display_capabilities));
  543. capabilities->x_resolution = CONFIG_PANEL_HOR_RES;
  544. capabilities->y_resolution = CONFIG_PANEL_VER_RES;
  545. capabilities->refresh_rate = config->videomode.refresh_rate;
  546. capabilities->supported_pixel_formats = SUPPORTED_PANEL_PIXEL_FORMATS;
  547. capabilities->current_pixel_format = config->videomode.pixel_format;
  548. capabilities->current_orientation = (enum display_orientation)(CONFIG_PANEL_ROTATION / 90);
  549. capabilities->screen_info = (CONFIG_PANEL_ROUND_SHAPE ? SCREEN_INFO_ROUND_SHAPE : 0);
  550. #if IS_TR_PANEL
  551. capabilities->screen_info |= (SCREEN_INFO_X_ALIGNMENT_WIDTH | SCREEN_INFO_Y_ALIGNMENT_HEIGHT);
  552. #endif
  553. }
  554. static int _lcd_panel_set_pixel_format(const struct device *dev,
  555. const enum display_pixel_format pixel_format)
  556. {
  557. struct lcd_panel_data *data = dev->data;
  558. const struct lcd_panel_config *config = data->config;
  559. if (pixel_format == config->videomode.pixel_format) {
  560. return 0;
  561. }
  562. LOG_ERR("Pixel format change not implemented");
  563. return -ENOTSUP;
  564. }
  565. static int _lcd_panel_set_orientation(const struct device *dev,
  566. const enum display_orientation orientation)
  567. {
  568. enum display_orientation current_orientation =
  569. (enum display_orientation)(CONFIG_PANEL_ROTATION / 90);
  570. return (orientation == current_orientation) ? 0 : -ENOTSUP;
  571. }
  572. static int _lcd_panel_register_callback(const struct device *dev,
  573. const struct display_callback *callback)
  574. {
  575. struct lcd_panel_data *data = dev->data;
  576. if (data->callback == NULL) {
  577. data->callback = callback;
  578. return 0;
  579. }
  580. return -EBUSY;
  581. }
  582. static int _lcd_panel_unregister_callback(const struct device *dev,
  583. const struct display_callback *callback)
  584. {
  585. struct lcd_panel_data *data = dev->data;
  586. if (data->callback == callback) {
  587. data->callback = NULL;
  588. return 0;
  589. }
  590. return -EINVAL;
  591. }
  592. static int _lcd_panel_init(const struct device *dev)
  593. {
  594. struct lcd_panel_data *data = dev->data;
  595. const struct lcd_panel_pincfg *pincfg = dev->config;
  596. const struct device *de_dev = NULL;
  597. #ifdef CONFIG_PANEL_BACKLIGHT_PWM
  598. data->backlight_dev = device_get_binding(pincfg->backlight_cfg.dev_name);
  599. if (data->backlight_dev == NULL) {
  600. LOG_ERR("Couldn't find pwm device\n");
  601. return -ENODEV;
  602. }
  603. pwm_pin_set_cycles(data->backlight_dev, pincfg->backlight_cfg.chan,
  604. pincfg->backlight_cfg.period, 0, pincfg->backlight_cfg.flag);
  605. #elif defined(CONFIG_PANEL_BACKLIGHT_GPIO)
  606. data->backlight_dev = device_get_binding(pincfg->backlight_cfg.gpio_dev_name);
  607. if (data->backlight_dev == NULL) {
  608. LOG_ERR("Couldn't find backlight pin");
  609. return -ENODEV;
  610. }
  611. if (gpio_pin_configure(data->backlight_dev, pincfg->backlight_cfg.gpion,
  612. GPIO_OUTPUT_INACTIVE | pincfg->backlight_cfg.flag)) {
  613. LOG_ERR("Couldn't configure backlight pin");
  614. return -ENODEV;
  615. }
  616. #endif /* CONFIG_PANEL_BACKLIGHT_PWM */
  617. #ifdef CONFIG_PANEL_RESET_GPIO
  618. data->reset_gpio = device_get_binding(pincfg->reset_cfg.gpio_dev_name);
  619. if (data->reset_gpio == NULL) {
  620. LOG_ERR("Couldn't find reset pin");
  621. return -ENODEV;
  622. }
  623. if (gpio_pin_configure(data->reset_gpio, pincfg->reset_cfg.gpion,
  624. GPIO_OUTPUT_ACTIVE | pincfg->reset_cfg.flag)) {
  625. LOG_ERR("Couldn't configure reset pin");
  626. return -ENODEV;
  627. }
  628. #endif /* CONFIG_PANEL_RESET_GPIO */
  629. #ifdef CONFIG_PANEL_POWER_GPIO
  630. data->power_gpio = device_get_binding(pincfg->power_cfg.gpio_dev_name);
  631. if (data->power_gpio == NULL) {
  632. LOG_ERR("Couldn't find power pin");
  633. return -ENODEV;
  634. }
  635. if (gpio_pin_configure(data->power_gpio, pincfg->power_cfg.gpion,
  636. GPIO_OUTPUT_INACTIVE | pincfg->power_cfg.flag)) {
  637. LOG_ERR("Couldn't configure power pin");
  638. return -ENODEV;
  639. }
  640. #endif /* CONFIG_PANEL_POWER_GPIO */
  641. #ifdef CONFIG_PANEL_POWER1_GPIO
  642. data->power1_gpio = device_get_binding(pincfg->power1_cfg.gpio_dev_name);
  643. if (data->power1_gpio == NULL) {
  644. LOG_ERR("Couldn't find power1 pin");
  645. return -ENODEV;
  646. }
  647. if (gpio_pin_configure(data->power1_gpio, pincfg->power1_cfg.gpion,
  648. GPIO_OUTPUT_INACTIVE | pincfg->power1_cfg.flag)) {
  649. LOG_ERR("Couldn't configure power1 pin");
  650. return -ENODEV;
  651. }
  652. #endif /* CONFIG_PANEL_POWER1_GPIO */
  653. #ifdef CONFIG_PANEL_TE_GPIO
  654. data->te_gpio = device_get_binding(pincfg->te_cfg.gpio_dev_name);
  655. if (data->te_gpio == NULL) {
  656. LOG_ERR("Couldn't find te pin");
  657. return -ENODEV;
  658. }
  659. if (gpio_pin_configure(data->te_gpio, pincfg->te_cfg.gpion,
  660. GPIO_INPUT | pincfg->te_cfg.flag)) {
  661. LOG_ERR("Couldn't configure te pin");
  662. return -ENODEV;
  663. }
  664. /* FIXME: make sure interrupt disabled */
  665. gpio_pin_interrupt_configure(data->te_gpio, pincfg->te_cfg.gpion, GPIO_INT_DISABLE);
  666. gpio_init_callback(&data->te_gpio_cb, _panel_te_gpio_handler, BIT(pincfg->te_cfg.gpion));
  667. gpio_add_callback(data->te_gpio, &data->te_gpio_cb);
  668. #else
  669. k_timer_init(&data->te_timer, _panel_te_timer_handler, NULL);
  670. #endif /* CONFIG_PANEL_TE_GPIO */
  671. data->lcdc_dev = device_get_binding(CONFIG_LCDC_DEV_NAME);
  672. if (data->lcdc_dev == NULL) {
  673. LOG_ERR("Could not get LCD controller device");
  674. return -EPERM;
  675. }
  676. display_controller_control(data->lcdc_dev,
  677. DISPLAY_CONTROLLER_CTRL_COMPLETE_CB, _panel_complete_transfer, (void *)dev);
  678. if (_panel_detect(dev)) {
  679. LOG_ERR("No panel connected");
  680. return -ENODEV;
  681. }
  682. de_dev = device_get_binding(CONFIG_DISPLAY_ENGINE_DEV_NAME);
  683. if (de_dev == NULL) {
  684. LOG_WRN("Could not get display engine device");
  685. } else {
  686. display_engine_control(de_dev,
  687. DISPLAY_ENGINE_CTRL_DISPLAY_PORT, (void *)&data->config->videoport, NULL);
  688. display_engine_control(de_dev,
  689. DISPLAY_ENGINE_CTRL_DISPLAY_MODE, (void *)&data->config->videomode, NULL);
  690. display_engine_control(de_dev,
  691. DISPLAY_ENGINE_CTRL_DISPLAY_PREPARE_CB, _panel_prepare_de_transfer, (void *)dev);
  692. display_engine_control(de_dev,
  693. DISPLAY_ENGINE_CTRL_DISPLAY_START_CB, _panel_start_de_transfer, (void *)dev);
  694. }
  695. data->disp_on = 0;
  696. data->in_sleep = 1;
  697. data->te_active = 0;
  698. data->pending_brightness = CONFIG_PANEL_BRIGHTNESS;
  699. data->brightness_delay = CONFIG_PANEL_BRIGHTNESS_DELAY_PERIODS;
  700. data->pm_state = PM_DEVICE_STATE_SUSPENDED;
  701. #if CONFIG_PANEL_ESD_CHECK_PERIOD > 0
  702. k_delayed_work_init(&data->esd_check_work, _panel_esd_check_handler);
  703. #endif
  704. k_work_init(&data->resume_work, _panel_pm_resume_handler);
  705. #ifdef CONFIG_LCD_WORK_QUEUE
  706. k_work_queue_start(&data->pm_workq, pm_workq_stack, K_THREAD_STACK_SIZEOF(pm_workq_stack), 6, NULL);
  707. #endif
  708. _panel_pm_late_resume(dev);
  709. bDevEnterSuspend = false;
  710. return 0;
  711. }
  712. /**********************
  713. * POWER MANAGEMENT
  714. **********************/
  715. static void _panel_pm_notify(struct lcd_panel_data *data, uint32_t pm_action)
  716. {
  717. if (data->callback && data->callback->pm_notify) {
  718. data->callback->pm_notify(data->callback, pm_action);
  719. }
  720. }
  721. static int _panel_pm_early_suspend(const struct device *dev, bool in_turnoff)
  722. {
  723. struct lcd_panel_data *data = dev->data;
  724. if (data->pm_state != PM_DEVICE_STATE_ACTIVE &&
  725. data->pm_state != PM_DEVICE_STATE_LOW_POWER) {
  726. return -EPERM;
  727. }
  728. LOG_INF("panel early-suspend");
  729. _panel_cancel_esd_check_work(data);
  730. _panel_pm_notify(data, PM_DEVICE_ACTION_EARLY_SUSPEND);
  731. _panel_power_off(dev, in_turnoff);
  732. data->pm_state = PM_DEVICE_STATE_SUSPENDED;
  733. bDevEnterSuspend = true;
  734. return 0;
  735. }
  736. static int _panel_pm_late_resume(const struct device *dev)
  737. {
  738. struct lcd_panel_data *data = dev->data;
  739. if (data->pm_state != PM_DEVICE_STATE_SUSPENDED) {
  740. return -EPERM;
  741. }
  742. LOG_INF("panel late-resume");
  743. data->pm_changing = 1;
  744. #ifdef CONFIG_LCD_WORK_QUEUE
  745. k_work_submit_to_queue(&data->pm_workq, &data->resume_work);
  746. #else
  747. k_work_submit(&data->resume_work);
  748. #endif
  749. return 0;
  750. }
  751. static struct hrtimer g_panel_backlight_ht_ctrl;
  752. static void htimer_fun(struct hrtimer *ttimer, void *expiry_fn_arg)
  753. {
  754. const struct device *dev = DEVICE_GET(lcd_panel);
  755. struct lcd_panel_data *data = dev->data;
  756. const struct lcd_panel_pincfg *pincfg = dev->config;
  757. #ifdef CONFIG_PANEL_POWER1_GPIO
  758. gpio_pin_set(data->power1_gpio, pincfg->power1_cfg.gpion, 1);
  759. #endif
  760. }
  761. static void htimer_panel_backlight(unsigned int ms)
  762. {
  763. hrtimer_init(&g_panel_backlight_ht_ctrl, htimer_fun, NULL);
  764. hrtimer_start(&g_panel_backlight_ht_ctrl, 1000*ms, 0);
  765. }
  766. static void _panel_pm_resume_handler(struct k_work *work)
  767. {
  768. const struct device *dev = DEVICE_GET(lcd_panel);
  769. struct lcd_panel_data *data = dev->data;
  770. LOG_INF("panel resuming");
  771. lcd_panel_wake_lock();
  772. data->pm_state = PM_DEVICE_STATE_ACTIVE;
  773. data->transfering = 0; /* reset flag in case fail again */
  774. data->pm_changing = 0;
  775. _panel_power_on(dev);
  776. _panel_pm_notify(data, PM_DEVICE_ACTION_LATE_RESUME);
  777. _panel_submit_esd_check_work(data);
  778. lcd_panel_wake_unlock();
  779. #ifdef CONFIG_PANEL_POWER1_GPIO
  780. if (bDevEnterSuspend)
  781. {
  782. bDevEnterSuspend = false;
  783. htimer_panel_backlight(40);
  784. }
  785. else
  786. {
  787. htimer_panel_backlight(500);
  788. }
  789. #endif
  790. LOG_INF("panel active");
  791. }
  792. #ifdef CONFIG_PM_DEVICE
  793. static void _panel_pm_change_low_power(const struct device *dev, uint32_t pm_state)
  794. {
  795. struct lcd_panel_data *data = dev->data;
  796. const struct lcd_panel_config *config = data->config;
  797. unsigned int key;
  798. while (1) {
  799. key = irq_lock();
  800. if (!data->transfering)
  801. break;
  802. irq_unlock(key);
  803. k_msleep(1); /* wait transfer finish */
  804. }
  805. data->pm_state = pm_state;
  806. if (data->pm_state == PM_DEVICE_STATE_LOW_POWER) {
  807. if (config->ops->lowpower_enter)
  808. config->ops->lowpower_enter(dev);
  809. if (config->ops->set_aod_brightness) {
  810. config->ops->set_aod_brightness(dev, CONFIG_PANEL_AOD_BRIGHTNESS);
  811. } else {
  812. _panel_apply_brightness(dev, CONFIG_PANEL_AOD_BRIGHTNESS);
  813. }
  814. _panel_te_set_enable(dev, false);
  815. board_lcd_suspend(true, true);
  816. } else {
  817. board_lcd_resume(true, true);
  818. if (config->ops->lowpower_exit)
  819. config->ops->lowpower_exit(dev);
  820. _panel_te_set_enable(dev, true);
  821. }
  822. irq_unlock(key);
  823. }
  824. static int _panel_pm_enter_low_power(const struct device *dev)
  825. {
  826. struct lcd_panel_data *data = dev->data;
  827. if (data->pm_state != PM_DEVICE_STATE_ACTIVE) {
  828. return -EPERM;
  829. }
  830. _panel_cancel_esd_check_work(data);
  831. _panel_pm_notify(data, PM_DEVICE_ACTION_LOW_POWER);
  832. _panel_pm_change_low_power(dev, PM_DEVICE_STATE_LOW_POWER);
  833. LOG_INF("panel enter low-power");
  834. return 0;
  835. }
  836. static int _panel_pm_exit_low_power(const struct device *dev)
  837. {
  838. struct lcd_panel_data *data = dev->data;
  839. if (data->pm_state != PM_DEVICE_STATE_LOW_POWER) {
  840. return -EPERM;
  841. }
  842. _panel_pm_notify(data, PM_DEVICE_ACTION_LATE_RESUME);
  843. _panel_pm_change_low_power(dev, PM_DEVICE_STATE_ACTIVE);
  844. _panel_submit_esd_check_work(data);
  845. LOG_INF("panel exit low-power");
  846. return 0;
  847. }
  848. static int _lcd_panel_pm_control(const struct device *dev, enum pm_device_action action)
  849. {
  850. struct lcd_panel_data *data = dev->data;
  851. int ret = 0;
  852. if (data->pm_state == PM_DEVICE_STATE_OFF)
  853. return -EPERM;
  854. switch (action) {
  855. case PM_DEVICE_ACTION_EARLY_SUSPEND:
  856. if (soc_get_aod_mode()) {
  857. ret = _panel_pm_enter_low_power(dev);
  858. } else {
  859. ret = _panel_pm_early_suspend(dev, false);
  860. board_lcd_suspend(false, true);
  861. }
  862. break;
  863. case PM_DEVICE_ACTION_LATE_RESUME:
  864. if (data->pm_state == PM_DEVICE_STATE_LOW_POWER) {
  865. ret = _panel_pm_exit_low_power(dev);
  866. } else {
  867. board_lcd_resume(false, true);
  868. ret = _panel_pm_late_resume(dev);
  869. }
  870. break;
  871. case PM_DEVICE_ACTION_SUSPEND:
  872. if (data->pm_changing || data->pm_state == PM_DEVICE_STATE_ACTIVE) {
  873. ret = -EPERM;
  874. }
  875. break;
  876. case PM_DEVICE_ACTION_TURN_OFF:
  877. _panel_pm_early_suspend(dev, true);
  878. data->pm_state = PM_DEVICE_STATE_OFF;
  879. LOG_INF("panel turn-off");
  880. break;
  881. default:
  882. break;
  883. }
  884. return ret;
  885. }
  886. #endif /* CONFIG_PM_DEVICE */
  887. /**********************
  888. * DEVICE DEFINITION
  889. **********************/
  890. static const struct display_driver_api lcd_panel_driver_api = {
  891. .blanking_on = _lcd_panel_blanking_on,
  892. .blanking_off = _lcd_panel_blanking_off,
  893. .write = _lcd_panel_write,
  894. .read = _lcd_panel_read,
  895. .get_framebuffer = _lcd_panel_get_framebuffer,
  896. .set_brightness = _lcd_panel_set_brightness,
  897. .get_brightness = _lcd_panel_get_brightness,
  898. .set_contrast = _lcd_panel_set_contrast,
  899. .get_capabilities = _lcd_panel_get_capabilities,
  900. .set_pixel_format = _lcd_panel_set_pixel_format,
  901. .set_orientation = _lcd_panel_set_orientation,
  902. .register_callback = _lcd_panel_register_callback,
  903. .unregister_callback = _lcd_panel_unregister_callback,
  904. };
  905. static const struct lcd_panel_pincfg lcd_panel_pin_config = {
  906. #ifdef CONFIG_PANEL_POWER_GPIO
  907. .power_cfg = CONFIG_PANEL_POWER_GPIO,
  908. #endif
  909. #ifdef CONFIG_PANEL_POWER1_GPIO
  910. .power1_cfg = CONFIG_PANEL_POWER1_GPIO,
  911. #endif
  912. #ifdef CONFIG_PANEL_RESET_GPIO
  913. .reset_cfg = CONFIG_PANEL_RESET_GPIO,
  914. #endif
  915. #ifdef CONFIG_PANEL_TE_GPIO
  916. .te_cfg = CONFIG_PANEL_TE_GPIO,
  917. #endif
  918. #ifdef CONFIG_PANEL_BACKLIGHT_PWM
  919. .backlight_cfg = CONFIG_PANEL_BACKLIGHT_PWM,
  920. #elif defined(CONFIG_PANEL_BACKLIGHT_GPIO)
  921. .backlight_cfg = CONFIG_PANEL_BACKLIGHT_GPIO,
  922. #endif
  923. };
  924. static struct lcd_panel_data lcd_panel_data;
  925. #if IS_ENABLED(CONFIG_PANEL)
  926. DEVICE_DEFINE(lcd_panel, CONFIG_LCD_DISPLAY_DEV_NAME, _lcd_panel_init, _lcd_panel_pm_control,
  927. &lcd_panel_data, &lcd_panel_pin_config, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY,
  928. &lcd_panel_driver_api);
  929. #endif