panel-panasonic-vvx10f034n00.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. struct mipi_dsi_device *dsi = wuxga_nt->dsi;
  52. int ret;
  53. ret = mipi_dsi_turn_on_peripheral(dsi);
  54. if (ret < 0)
  55. return ret;
  56. return 0;
  57. }
  58. static int wuxga_nt_panel_disable(struct drm_panel *panel)
  59. {
  60. struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
  61. if (!wuxga_nt->enabled)
  62. return 0;
  63. mipi_dsi_shutdown_peripheral(wuxga_nt->dsi);
  64. if (wuxga_nt->backlight) {
  65. wuxga_nt->backlight->props.power = FB_BLANK_POWERDOWN;
  66. wuxga_nt->backlight->props.state |= BL_CORE_FBBLANK;
  67. backlight_update_status(wuxga_nt->backlight);
  68. }
  69. wuxga_nt->enabled = false;
  70. return 0;
  71. }
  72. static int wuxga_nt_panel_unprepare(struct drm_panel *panel)
  73. {
  74. struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
  75. if (!wuxga_nt->prepared)
  76. return 0;
  77. regulator_disable(wuxga_nt->supply);
  78. wuxga_nt->earliest_wake = ktime_add_ms(ktime_get_real(), MIN_POFF_MS);
  79. wuxga_nt->prepared = false;
  80. return 0;
  81. }
  82. static int wuxga_nt_panel_prepare(struct drm_panel *panel)
  83. {
  84. struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
  85. int ret;
  86. s64 enablewait;
  87. if (wuxga_nt->prepared)
  88. return 0;
  89. /*
  90. * If the user re-enabled the panel before the required off-time then
  91. * we need to wait the remaining period before re-enabling regulator
  92. */
  93. enablewait = ktime_ms_delta(wuxga_nt->earliest_wake, ktime_get_real());
  94. /* Sanity check, this should never happen */
  95. if (enablewait > MIN_POFF_MS)
  96. enablewait = MIN_POFF_MS;
  97. if (enablewait > 0)
  98. msleep(enablewait);
  99. ret = regulator_enable(wuxga_nt->supply);
  100. if (ret < 0)
  101. return ret;
  102. /*
  103. * A minimum delay of 250ms is required after power-up until commands
  104. * can be sent
  105. */
  106. msleep(250);
  107. ret = wuxga_nt_panel_on(wuxga_nt);
  108. if (ret < 0) {
  109. dev_err(panel->dev, "failed to set panel on: %d\n", ret);
  110. goto poweroff;
  111. }
  112. wuxga_nt->prepared = true;
  113. return 0;
  114. poweroff:
  115. regulator_disable(wuxga_nt->supply);
  116. return ret;
  117. }
  118. static int wuxga_nt_panel_enable(struct drm_panel *panel)
  119. {
  120. struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
  121. if (wuxga_nt->enabled)
  122. return 0;
  123. if (wuxga_nt->backlight) {
  124. wuxga_nt->backlight->props.power = FB_BLANK_UNBLANK;
  125. wuxga_nt->backlight->props.state &= ~BL_CORE_FBBLANK;
  126. backlight_update_status(wuxga_nt->backlight);
  127. }
  128. wuxga_nt->enabled = true;
  129. return 0;
  130. }
  131. static const struct drm_display_mode default_mode = {
  132. .clock = 164402,
  133. .hdisplay = 1920,
  134. .hsync_start = 1920 + 152,
  135. .hsync_end = 1920 + 152 + 52,
  136. .htotal = 1920 + 152 + 52 + 20,
  137. .vdisplay = 1200,
  138. .vsync_start = 1200 + 24,
  139. .vsync_end = 1200 + 24 + 6,
  140. .vtotal = 1200 + 24 + 6 + 48,
  141. .vrefresh = 60,
  142. };
  143. static int wuxga_nt_panel_get_modes(struct drm_panel *panel)
  144. {
  145. struct drm_display_mode *mode;
  146. mode = drm_mode_duplicate(panel->drm, &default_mode);
  147. if (!mode) {
  148. dev_err(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
  149. default_mode.hdisplay, default_mode.vdisplay,
  150. default_mode.vrefresh);
  151. return -ENOMEM;
  152. }
  153. drm_mode_set_name(mode);
  154. drm_mode_probed_add(panel->connector, mode);
  155. panel->connector->display_info.width_mm = 217;
  156. panel->connector->display_info.height_mm = 136;
  157. return 1;
  158. }
  159. static const struct drm_panel_funcs wuxga_nt_panel_funcs = {
  160. .disable = wuxga_nt_panel_disable,
  161. .unprepare = wuxga_nt_panel_unprepare,
  162. .prepare = wuxga_nt_panel_prepare,
  163. .enable = wuxga_nt_panel_enable,
  164. .get_modes = wuxga_nt_panel_get_modes,
  165. };
  166. static const struct of_device_id wuxga_nt_of_match[] = {
  167. { .compatible = "panasonic,vvx10f034n00", },
  168. { }
  169. };
  170. MODULE_DEVICE_TABLE(of, wuxga_nt_of_match);
  171. static int wuxga_nt_panel_add(struct wuxga_nt_panel *wuxga_nt)
  172. {
  173. struct device *dev = &wuxga_nt->dsi->dev;
  174. struct device_node *np;
  175. int ret;
  176. wuxga_nt->mode = &default_mode;
  177. wuxga_nt->supply = devm_regulator_get(dev, "power");
  178. if (IS_ERR(wuxga_nt->supply))
  179. return PTR_ERR(wuxga_nt->supply);
  180. np = of_parse_phandle(dev->of_node, "backlight", 0);
  181. if (np) {
  182. wuxga_nt->backlight = of_find_backlight_by_node(np);
  183. of_node_put(np);
  184. if (!wuxga_nt->backlight)
  185. return -EPROBE_DEFER;
  186. }
  187. drm_panel_init(&wuxga_nt->base);
  188. wuxga_nt->base.funcs = &wuxga_nt_panel_funcs;
  189. wuxga_nt->base.dev = &wuxga_nt->dsi->dev;
  190. ret = drm_panel_add(&wuxga_nt->base);
  191. if (ret < 0)
  192. goto put_backlight;
  193. return 0;
  194. put_backlight:
  195. if (wuxga_nt->backlight)
  196. put_device(&wuxga_nt->backlight->dev);
  197. return ret;
  198. }
  199. static void wuxga_nt_panel_del(struct wuxga_nt_panel *wuxga_nt)
  200. {
  201. if (wuxga_nt->base.dev)
  202. drm_panel_remove(&wuxga_nt->base);
  203. if (wuxga_nt->backlight)
  204. put_device(&wuxga_nt->backlight->dev);
  205. }
  206. static int wuxga_nt_panel_probe(struct mipi_dsi_device *dsi)
  207. {
  208. struct wuxga_nt_panel *wuxga_nt;
  209. int ret;
  210. dsi->lanes = 4;
  211. dsi->format = MIPI_DSI_FMT_RGB888;
  212. dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
  213. MIPI_DSI_MODE_VIDEO_HSE |
  214. MIPI_DSI_CLOCK_NON_CONTINUOUS |
  215. MIPI_DSI_MODE_LPM;
  216. wuxga_nt = devm_kzalloc(&dsi->dev, sizeof(*wuxga_nt), GFP_KERNEL);
  217. if (!wuxga_nt)
  218. return -ENOMEM;
  219. mipi_dsi_set_drvdata(dsi, wuxga_nt);
  220. wuxga_nt->dsi = dsi;
  221. ret = wuxga_nt_panel_add(wuxga_nt);
  222. if (ret < 0)
  223. return ret;
  224. return mipi_dsi_attach(dsi);
  225. }
  226. static int wuxga_nt_panel_remove(struct mipi_dsi_device *dsi)
  227. {
  228. struct wuxga_nt_panel *wuxga_nt = mipi_dsi_get_drvdata(dsi);
  229. int ret;
  230. ret = wuxga_nt_panel_disable(&wuxga_nt->base);
  231. if (ret < 0)
  232. dev_err(&dsi->dev, "failed to disable panel: %d\n", ret);
  233. ret = mipi_dsi_detach(dsi);
  234. if (ret < 0)
  235. dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
  236. drm_panel_detach(&wuxga_nt->base);
  237. wuxga_nt_panel_del(wuxga_nt);
  238. return 0;
  239. }
  240. static void wuxga_nt_panel_shutdown(struct mipi_dsi_device *dsi)
  241. {
  242. struct wuxga_nt_panel *wuxga_nt = mipi_dsi_get_drvdata(dsi);
  243. wuxga_nt_panel_disable(&wuxga_nt->base);
  244. }
  245. static struct mipi_dsi_driver wuxga_nt_panel_driver = {
  246. .driver = {
  247. .name = "panel-panasonic-vvx10f034n00",
  248. .of_match_table = wuxga_nt_of_match,
  249. },
  250. .probe = wuxga_nt_panel_probe,
  251. .remove = wuxga_nt_panel_remove,
  252. .shutdown = wuxga_nt_panel_shutdown,
  253. };
  254. module_mipi_dsi_driver(wuxga_nt_panel_driver);
  255. MODULE_AUTHOR("Werner Johansson <werner.johansson@sonymobile.com>");
  256. MODULE_DESCRIPTION("Panasonic VVX10F034N00 Novatek NT1397-based WUXGA (1920x1200) video mode panel driver");
  257. MODULE_LICENSE("GPL v2");