panel-panasonic-vvx10f034n00.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (C) 2015 Red Hat
  3. * Copyright (C) 2015 Sony Mobile Communications Inc.
  4. * Author: Werner Johansson <werner.johansson@sonymobile.com>
  5. *
  6. * Based on AUO panel driver by Rob Clark <robdclark@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/backlight.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <drm/drmP.h>
  25. #include <drm/drm_crtc.h>
  26. #include <drm/drm_mipi_dsi.h>
  27. #include <drm/drm_panel.h>
  28. #include <video/mipi_display.h>
  29. /*
  30. * When power is turned off to this panel a minimum off time of 500ms has to be
  31. * observed before powering back on as there's no external reset pin. Keep
  32. * track of earliest wakeup time and delay subsequent prepare call accordingly
  33. */
  34. #define MIN_POFF_MS (500)
  35. struct wuxga_nt_panel {
  36. struct drm_panel base;
  37. struct mipi_dsi_device *dsi;
  38. struct backlight_device *backlight;
  39. struct regulator *supply;
  40. bool prepared;
  41. bool enabled;
  42. ktime_t earliest_wake;
  43. const struct drm_display_mode *mode;
  44. };
  45. static inline struct wuxga_nt_panel *to_wuxga_nt_panel(struct drm_panel *panel)
  46. {
  47. return container_of(panel, struct wuxga_nt_panel, base);
  48. }
  49. static int wuxga_nt_panel_on(struct wuxga_nt_panel *wuxga_nt)
  50. {
  51. return mipi_dsi_turn_on_peripheral(wuxga_nt->dsi);
  52. }
  53. static int wuxga_nt_panel_disable(struct drm_panel *panel)
  54. {
  55. struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
  56. int mipi_ret, bl_ret = 0;
  57. if (!wuxga_nt->enabled)
  58. return 0;
  59. mipi_ret = mipi_dsi_shutdown_peripheral(wuxga_nt->dsi);
  60. if (wuxga_nt->backlight) {
  61. wuxga_nt->backlight->props.power = FB_BLANK_POWERDOWN;
  62. wuxga_nt->backlight->props.state |= BL_CORE_FBBLANK;
  63. bl_ret = backlight_update_status(wuxga_nt->backlight);
  64. }
  65. wuxga_nt->enabled = false;
  66. return mipi_ret ? mipi_ret : bl_ret;
  67. }
  68. static int wuxga_nt_panel_unprepare(struct drm_panel *panel)
  69. {
  70. struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
  71. if (!wuxga_nt->prepared)
  72. return 0;
  73. regulator_disable(wuxga_nt->supply);
  74. wuxga_nt->earliest_wake = ktime_add_ms(ktime_get_real(), MIN_POFF_MS);
  75. wuxga_nt->prepared = false;
  76. return 0;
  77. }
  78. static int wuxga_nt_panel_prepare(struct drm_panel *panel)
  79. {
  80. struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
  81. int ret;
  82. s64 enablewait;
  83. if (wuxga_nt->prepared)
  84. return 0;
  85. /*
  86. * If the user re-enabled the panel before the required off-time then
  87. * we need to wait the remaining period before re-enabling regulator
  88. */
  89. enablewait = ktime_ms_delta(wuxga_nt->earliest_wake, ktime_get_real());
  90. /* Sanity check, this should never happen */
  91. if (enablewait > MIN_POFF_MS)
  92. enablewait = MIN_POFF_MS;
  93. if (enablewait > 0)
  94. msleep(enablewait);
  95. ret = regulator_enable(wuxga_nt->supply);
  96. if (ret < 0)
  97. return ret;
  98. /*
  99. * A minimum delay of 250ms is required after power-up until commands
  100. * can be sent
  101. */
  102. msleep(250);
  103. ret = wuxga_nt_panel_on(wuxga_nt);
  104. if (ret < 0) {
  105. dev_err(panel->dev, "failed to set panel on: %d\n", ret);
  106. goto poweroff;
  107. }
  108. wuxga_nt->prepared = true;
  109. return 0;
  110. poweroff:
  111. regulator_disable(wuxga_nt->supply);
  112. return ret;
  113. }
  114. static int wuxga_nt_panel_enable(struct drm_panel *panel)
  115. {
  116. struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
  117. if (wuxga_nt->enabled)
  118. return 0;
  119. if (wuxga_nt->backlight) {
  120. wuxga_nt->backlight->props.power = FB_BLANK_UNBLANK;
  121. wuxga_nt->backlight->props.state &= ~BL_CORE_FBBLANK;
  122. backlight_update_status(wuxga_nt->backlight);
  123. }
  124. wuxga_nt->enabled = true;
  125. return 0;
  126. }
  127. static const struct drm_display_mode default_mode = {
  128. .clock = 164402,
  129. .hdisplay = 1920,
  130. .hsync_start = 1920 + 152,
  131. .hsync_end = 1920 + 152 + 52,
  132. .htotal = 1920 + 152 + 52 + 20,
  133. .vdisplay = 1200,
  134. .vsync_start = 1200 + 24,
  135. .vsync_end = 1200 + 24 + 6,
  136. .vtotal = 1200 + 24 + 6 + 48,
  137. .vrefresh = 60,
  138. };
  139. static int wuxga_nt_panel_get_modes(struct drm_panel *panel)
  140. {
  141. struct drm_display_mode *mode;
  142. mode = drm_mode_duplicate(panel->drm, &default_mode);
  143. if (!mode) {
  144. dev_err(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
  145. default_mode.hdisplay, default_mode.vdisplay,
  146. default_mode.vrefresh);
  147. return -ENOMEM;
  148. }
  149. drm_mode_set_name(mode);
  150. drm_mode_probed_add(panel->connector, mode);
  151. panel->connector->display_info.width_mm = 217;
  152. panel->connector->display_info.height_mm = 136;
  153. return 1;
  154. }
  155. static const struct drm_panel_funcs wuxga_nt_panel_funcs = {
  156. .disable = wuxga_nt_panel_disable,
  157. .unprepare = wuxga_nt_panel_unprepare,
  158. .prepare = wuxga_nt_panel_prepare,
  159. .enable = wuxga_nt_panel_enable,
  160. .get_modes = wuxga_nt_panel_get_modes,
  161. };
  162. static const struct of_device_id wuxga_nt_of_match[] = {
  163. { .compatible = "panasonic,vvx10f034n00", },
  164. { }
  165. };
  166. MODULE_DEVICE_TABLE(of, wuxga_nt_of_match);
  167. static int wuxga_nt_panel_add(struct wuxga_nt_panel *wuxga_nt)
  168. {
  169. struct device *dev = &wuxga_nt->dsi->dev;
  170. struct device_node *np;
  171. int ret;
  172. wuxga_nt->mode = &default_mode;
  173. wuxga_nt->supply = devm_regulator_get(dev, "power");
  174. if (IS_ERR(wuxga_nt->supply))
  175. return PTR_ERR(wuxga_nt->supply);
  176. np = of_parse_phandle(dev->of_node, "backlight", 0);
  177. if (np) {
  178. wuxga_nt->backlight = of_find_backlight_by_node(np);
  179. of_node_put(np);
  180. if (!wuxga_nt->backlight)
  181. return -EPROBE_DEFER;
  182. }
  183. drm_panel_init(&wuxga_nt->base);
  184. wuxga_nt->base.funcs = &wuxga_nt_panel_funcs;
  185. wuxga_nt->base.dev = &wuxga_nt->dsi->dev;
  186. ret = drm_panel_add(&wuxga_nt->base);
  187. if (ret < 0)
  188. goto put_backlight;
  189. return 0;
  190. put_backlight:
  191. if (wuxga_nt->backlight)
  192. put_device(&wuxga_nt->backlight->dev);
  193. return ret;
  194. }
  195. static void wuxga_nt_panel_del(struct wuxga_nt_panel *wuxga_nt)
  196. {
  197. if (wuxga_nt->base.dev)
  198. drm_panel_remove(&wuxga_nt->base);
  199. if (wuxga_nt->backlight)
  200. put_device(&wuxga_nt->backlight->dev);
  201. }
  202. static int wuxga_nt_panel_probe(struct mipi_dsi_device *dsi)
  203. {
  204. struct wuxga_nt_panel *wuxga_nt;
  205. int ret;
  206. dsi->lanes = 4;
  207. dsi->format = MIPI_DSI_FMT_RGB888;
  208. dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
  209. MIPI_DSI_MODE_VIDEO_HSE |
  210. MIPI_DSI_CLOCK_NON_CONTINUOUS |
  211. MIPI_DSI_MODE_LPM;
  212. wuxga_nt = devm_kzalloc(&dsi->dev, sizeof(*wuxga_nt), GFP_KERNEL);
  213. if (!wuxga_nt)
  214. return -ENOMEM;
  215. mipi_dsi_set_drvdata(dsi, wuxga_nt);
  216. wuxga_nt->dsi = dsi;
  217. ret = wuxga_nt_panel_add(wuxga_nt);
  218. if (ret < 0)
  219. return ret;
  220. return mipi_dsi_attach(dsi);
  221. }
  222. static int wuxga_nt_panel_remove(struct mipi_dsi_device *dsi)
  223. {
  224. struct wuxga_nt_panel *wuxga_nt = mipi_dsi_get_drvdata(dsi);
  225. int ret;
  226. ret = wuxga_nt_panel_disable(&wuxga_nt->base);
  227. if (ret < 0)
  228. dev_err(&dsi->dev, "failed to disable panel: %d\n", ret);
  229. ret = mipi_dsi_detach(dsi);
  230. if (ret < 0)
  231. dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
  232. wuxga_nt_panel_del(wuxga_nt);
  233. return 0;
  234. }
  235. static void wuxga_nt_panel_shutdown(struct mipi_dsi_device *dsi)
  236. {
  237. struct wuxga_nt_panel *wuxga_nt = mipi_dsi_get_drvdata(dsi);
  238. wuxga_nt_panel_disable(&wuxga_nt->base);
  239. }
  240. static struct mipi_dsi_driver wuxga_nt_panel_driver = {
  241. .driver = {
  242. .name = "panel-panasonic-vvx10f034n00",
  243. .of_match_table = wuxga_nt_of_match,
  244. },
  245. .probe = wuxga_nt_panel_probe,
  246. .remove = wuxga_nt_panel_remove,
  247. .shutdown = wuxga_nt_panel_shutdown,
  248. };
  249. module_mipi_dsi_driver(wuxga_nt_panel_driver);
  250. MODULE_AUTHOR("Werner Johansson <werner.johansson@sonymobile.com>");
  251. MODULE_DESCRIPTION("Panasonic VVX10F034N00 Novatek NT1397-based WUXGA (1920x1200) video mode panel driver");
  252. MODULE_LICENSE("GPL v2");