panel-innolux-p079zca.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/backlight.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_mipi_dsi.h>
  17. #include <drm/drm_panel.h>
  18. #include <video/mipi_display.h>
  19. struct innolux_panel {
  20. struct drm_panel base;
  21. struct mipi_dsi_device *link;
  22. struct backlight_device *backlight;
  23. struct regulator *supply;
  24. struct gpio_desc *enable_gpio;
  25. bool prepared;
  26. bool enabled;
  27. };
  28. static inline struct innolux_panel *to_innolux_panel(struct drm_panel *panel)
  29. {
  30. return container_of(panel, struct innolux_panel, base);
  31. }
  32. static int innolux_panel_disable(struct drm_panel *panel)
  33. {
  34. struct innolux_panel *innolux = to_innolux_panel(panel);
  35. int err;
  36. if (!innolux->enabled)
  37. return 0;
  38. backlight_disable(innolux->backlight);
  39. err = mipi_dsi_dcs_set_display_off(innolux->link);
  40. if (err < 0)
  41. DRM_DEV_ERROR(panel->dev, "failed to set display off: %d\n",
  42. err);
  43. innolux->enabled = false;
  44. return 0;
  45. }
  46. static int innolux_panel_unprepare(struct drm_panel *panel)
  47. {
  48. struct innolux_panel *innolux = to_innolux_panel(panel);
  49. int err;
  50. if (!innolux->prepared)
  51. return 0;
  52. err = mipi_dsi_dcs_enter_sleep_mode(innolux->link);
  53. if (err < 0) {
  54. DRM_DEV_ERROR(panel->dev, "failed to enter sleep mode: %d\n",
  55. err);
  56. return err;
  57. }
  58. gpiod_set_value_cansleep(innolux->enable_gpio, 0);
  59. /* T8: 80ms - 1000ms */
  60. msleep(80);
  61. err = regulator_disable(innolux->supply);
  62. if (err < 0)
  63. return err;
  64. innolux->prepared = false;
  65. return 0;
  66. }
  67. static int innolux_panel_prepare(struct drm_panel *panel)
  68. {
  69. struct innolux_panel *innolux = to_innolux_panel(panel);
  70. int err, regulator_err;
  71. if (innolux->prepared)
  72. return 0;
  73. gpiod_set_value_cansleep(innolux->enable_gpio, 0);
  74. err = regulator_enable(innolux->supply);
  75. if (err < 0)
  76. return err;
  77. /* T2: 15ms - 1000ms */
  78. usleep_range(15000, 16000);
  79. gpiod_set_value_cansleep(innolux->enable_gpio, 1);
  80. /* T4: 15ms - 1000ms */
  81. usleep_range(15000, 16000);
  82. err = mipi_dsi_dcs_exit_sleep_mode(innolux->link);
  83. if (err < 0) {
  84. DRM_DEV_ERROR(panel->dev, "failed to exit sleep mode: %d\n",
  85. err);
  86. goto poweroff;
  87. }
  88. /* T6: 120ms - 1000ms*/
  89. msleep(120);
  90. err = mipi_dsi_dcs_set_display_on(innolux->link);
  91. if (err < 0) {
  92. DRM_DEV_ERROR(panel->dev, "failed to set display on: %d\n",
  93. err);
  94. goto poweroff;
  95. }
  96. /* T7: 5ms */
  97. usleep_range(5000, 6000);
  98. innolux->prepared = true;
  99. return 0;
  100. poweroff:
  101. regulator_err = regulator_disable(innolux->supply);
  102. if (regulator_err)
  103. DRM_DEV_ERROR(panel->dev, "failed to disable regulator: %d\n",
  104. regulator_err);
  105. gpiod_set_value_cansleep(innolux->enable_gpio, 0);
  106. return err;
  107. }
  108. static int innolux_panel_enable(struct drm_panel *panel)
  109. {
  110. struct innolux_panel *innolux = to_innolux_panel(panel);
  111. int ret;
  112. if (innolux->enabled)
  113. return 0;
  114. ret = backlight_enable(innolux->backlight);
  115. if (ret) {
  116. DRM_DEV_ERROR(panel->drm->dev,
  117. "Failed to enable backlight %d\n", ret);
  118. return ret;
  119. }
  120. innolux->enabled = true;
  121. return 0;
  122. }
  123. static const struct drm_display_mode default_mode = {
  124. .clock = 56900,
  125. .hdisplay = 768,
  126. .hsync_start = 768 + 40,
  127. .hsync_end = 768 + 40 + 40,
  128. .htotal = 768 + 40 + 40 + 40,
  129. .vdisplay = 1024,
  130. .vsync_start = 1024 + 20,
  131. .vsync_end = 1024 + 20 + 4,
  132. .vtotal = 1024 + 20 + 4 + 20,
  133. .vrefresh = 60,
  134. };
  135. static int innolux_panel_get_modes(struct drm_panel *panel)
  136. {
  137. struct drm_display_mode *mode;
  138. mode = drm_mode_duplicate(panel->drm, &default_mode);
  139. if (!mode) {
  140. DRM_DEV_ERROR(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
  141. default_mode.hdisplay, default_mode.vdisplay,
  142. default_mode.vrefresh);
  143. return -ENOMEM;
  144. }
  145. drm_mode_set_name(mode);
  146. drm_mode_probed_add(panel->connector, mode);
  147. panel->connector->display_info.width_mm = 120;
  148. panel->connector->display_info.height_mm = 160;
  149. panel->connector->display_info.bpc = 8;
  150. return 1;
  151. }
  152. static const struct drm_panel_funcs innolux_panel_funcs = {
  153. .disable = innolux_panel_disable,
  154. .unprepare = innolux_panel_unprepare,
  155. .prepare = innolux_panel_prepare,
  156. .enable = innolux_panel_enable,
  157. .get_modes = innolux_panel_get_modes,
  158. };
  159. static const struct of_device_id innolux_of_match[] = {
  160. { .compatible = "innolux,p079zca", },
  161. { }
  162. };
  163. MODULE_DEVICE_TABLE(of, innolux_of_match);
  164. static int innolux_panel_add(struct innolux_panel *innolux)
  165. {
  166. struct device *dev = &innolux->link->dev;
  167. int err;
  168. innolux->supply = devm_regulator_get(dev, "power");
  169. if (IS_ERR(innolux->supply))
  170. return PTR_ERR(innolux->supply);
  171. innolux->enable_gpio = devm_gpiod_get_optional(dev, "enable",
  172. GPIOD_OUT_HIGH);
  173. if (IS_ERR(innolux->enable_gpio)) {
  174. err = PTR_ERR(innolux->enable_gpio);
  175. dev_dbg(dev, "failed to get enable gpio: %d\n", err);
  176. innolux->enable_gpio = NULL;
  177. }
  178. innolux->backlight = devm_of_find_backlight(dev);
  179. if (IS_ERR(innolux->backlight))
  180. return PTR_ERR(innolux->backlight);
  181. drm_panel_init(&innolux->base);
  182. innolux->base.funcs = &innolux_panel_funcs;
  183. innolux->base.dev = &innolux->link->dev;
  184. return drm_panel_add(&innolux->base);
  185. }
  186. static void innolux_panel_del(struct innolux_panel *innolux)
  187. {
  188. if (innolux->base.dev)
  189. drm_panel_remove(&innolux->base);
  190. }
  191. static int innolux_panel_probe(struct mipi_dsi_device *dsi)
  192. {
  193. struct innolux_panel *innolux;
  194. int err;
  195. dsi->lanes = 4;
  196. dsi->format = MIPI_DSI_FMT_RGB888;
  197. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
  198. MIPI_DSI_MODE_LPM;
  199. innolux = devm_kzalloc(&dsi->dev, sizeof(*innolux), GFP_KERNEL);
  200. if (!innolux)
  201. return -ENOMEM;
  202. mipi_dsi_set_drvdata(dsi, innolux);
  203. innolux->link = dsi;
  204. err = innolux_panel_add(innolux);
  205. if (err < 0)
  206. return err;
  207. err = mipi_dsi_attach(dsi);
  208. return err;
  209. }
  210. static int innolux_panel_remove(struct mipi_dsi_device *dsi)
  211. {
  212. struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi);
  213. int err;
  214. err = innolux_panel_unprepare(&innolux->base);
  215. if (err < 0)
  216. DRM_DEV_ERROR(&dsi->dev, "failed to unprepare panel: %d\n",
  217. err);
  218. err = innolux_panel_disable(&innolux->base);
  219. if (err < 0)
  220. DRM_DEV_ERROR(&dsi->dev, "failed to disable panel: %d\n", err);
  221. err = mipi_dsi_detach(dsi);
  222. if (err < 0)
  223. DRM_DEV_ERROR(&dsi->dev, "failed to detach from DSI host: %d\n",
  224. err);
  225. drm_panel_detach(&innolux->base);
  226. innolux_panel_del(innolux);
  227. return 0;
  228. }
  229. static void innolux_panel_shutdown(struct mipi_dsi_device *dsi)
  230. {
  231. struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi);
  232. innolux_panel_unprepare(&innolux->base);
  233. innolux_panel_disable(&innolux->base);
  234. }
  235. static struct mipi_dsi_driver innolux_panel_driver = {
  236. .driver = {
  237. .name = "panel-innolux-p079zca",
  238. .of_match_table = innolux_of_match,
  239. },
  240. .probe = innolux_panel_probe,
  241. .remove = innolux_panel_remove,
  242. .shutdown = innolux_panel_shutdown,
  243. };
  244. module_mipi_dsi_driver(innolux_panel_driver);
  245. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  246. MODULE_DESCRIPTION("Innolux P079ZCA panel driver");
  247. MODULE_LICENSE("GPL v2");