panel-samsung-s6e88a0-ams452ef01.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2019, Michael Srba
  3. #include <linux/delay.h>
  4. #include <linux/gpio/consumer.h>
  5. #include <linux/module.h>
  6. #include <linux/of.h>
  7. #include <linux/regulator/consumer.h>
  8. #include <video/mipi_display.h>
  9. #include <drm/drm_mipi_dsi.h>
  10. #include <drm/drm_modes.h>
  11. #include <drm/drm_panel.h>
  12. struct s6e88a0_ams452ef01 {
  13. struct drm_panel panel;
  14. struct mipi_dsi_device *dsi;
  15. struct regulator_bulk_data supplies[2];
  16. struct gpio_desc *reset_gpio;
  17. bool prepared;
  18. };
  19. static inline struct
  20. s6e88a0_ams452ef01 *to_s6e88a0_ams452ef01(struct drm_panel *panel)
  21. {
  22. return container_of(panel, struct s6e88a0_ams452ef01, panel);
  23. }
  24. #define dsi_dcs_write_seq(dsi, seq...) do { \
  25. static const u8 d[] = { seq }; \
  26. int ret; \
  27. ret = mipi_dsi_dcs_write_buffer(dsi, d, ARRAY_SIZE(d)); \
  28. if (ret < 0) \
  29. return ret; \
  30. } while (0)
  31. static void s6e88a0_ams452ef01_reset(struct s6e88a0_ams452ef01 *ctx)
  32. {
  33. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  34. usleep_range(5000, 6000);
  35. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  36. usleep_range(1000, 2000);
  37. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  38. usleep_range(10000, 11000);
  39. }
  40. static int s6e88a0_ams452ef01_on(struct s6e88a0_ams452ef01 *ctx)
  41. {
  42. struct mipi_dsi_device *dsi = ctx->dsi;
  43. struct device *dev = &dsi->dev;
  44. int ret;
  45. dsi->mode_flags |= MIPI_DSI_MODE_LPM;
  46. dsi_dcs_write_seq(dsi, 0xf0, 0x5a, 0x5a); // enable LEVEL2 commands
  47. dsi_dcs_write_seq(dsi, 0xcc, 0x4c); // set Pixel Clock Divider polarity
  48. ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
  49. if (ret < 0) {
  50. dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
  51. return ret;
  52. }
  53. msleep(120);
  54. // set default brightness/gama
  55. dsi_dcs_write_seq(dsi, 0xca,
  56. 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, // V255 RR,GG,BB
  57. 0x80, 0x80, 0x80, // V203 R,G,B
  58. 0x80, 0x80, 0x80, // V151 R,G,B
  59. 0x80, 0x80, 0x80, // V87 R,G,B
  60. 0x80, 0x80, 0x80, // V51 R,G,B
  61. 0x80, 0x80, 0x80, // V35 R,G,B
  62. 0x80, 0x80, 0x80, // V23 R,G,B
  63. 0x80, 0x80, 0x80, // V11 R,G,B
  64. 0x6b, 0x68, 0x71, // V3 R,G,B
  65. 0x00, 0x00, 0x00); // V1 R,G,B
  66. // set default Amoled Off Ratio
  67. dsi_dcs_write_seq(dsi, 0xb2, 0x40, 0x0a, 0x17, 0x00, 0x0a);
  68. dsi_dcs_write_seq(dsi, 0xb6, 0x2c, 0x0b); // set default elvss voltage
  69. dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
  70. dsi_dcs_write_seq(dsi, 0xf7, 0x03); // gamma/aor update
  71. dsi_dcs_write_seq(dsi, 0xf0, 0xa5, 0xa5); // disable LEVEL2 commands
  72. ret = mipi_dsi_dcs_set_display_on(dsi);
  73. if (ret < 0) {
  74. dev_err(dev, "Failed to set display on: %d\n", ret);
  75. return ret;
  76. }
  77. return 0;
  78. }
  79. static int s6e88a0_ams452ef01_off(struct s6e88a0_ams452ef01 *ctx)
  80. {
  81. struct mipi_dsi_device *dsi = ctx->dsi;
  82. struct device *dev = &dsi->dev;
  83. int ret;
  84. dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
  85. ret = mipi_dsi_dcs_set_display_off(dsi);
  86. if (ret < 0) {
  87. dev_err(dev, "Failed to set display off: %d\n", ret);
  88. return ret;
  89. }
  90. msleep(35);
  91. ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
  92. if (ret < 0) {
  93. dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
  94. return ret;
  95. }
  96. msleep(120);
  97. return 0;
  98. }
  99. static int s6e88a0_ams452ef01_prepare(struct drm_panel *panel)
  100. {
  101. struct s6e88a0_ams452ef01 *ctx = to_s6e88a0_ams452ef01(panel);
  102. struct device *dev = &ctx->dsi->dev;
  103. int ret;
  104. if (ctx->prepared)
  105. return 0;
  106. ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  107. if (ret < 0) {
  108. dev_err(dev, "Failed to enable regulators: %d\n", ret);
  109. return ret;
  110. }
  111. s6e88a0_ams452ef01_reset(ctx);
  112. ret = s6e88a0_ams452ef01_on(ctx);
  113. if (ret < 0) {
  114. dev_err(dev, "Failed to initialize panel: %d\n", ret);
  115. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  116. regulator_bulk_disable(ARRAY_SIZE(ctx->supplies),
  117. ctx->supplies);
  118. return ret;
  119. }
  120. ctx->prepared = true;
  121. return 0;
  122. }
  123. static int s6e88a0_ams452ef01_unprepare(struct drm_panel *panel)
  124. {
  125. struct s6e88a0_ams452ef01 *ctx = to_s6e88a0_ams452ef01(panel);
  126. struct device *dev = &ctx->dsi->dev;
  127. int ret;
  128. if (!ctx->prepared)
  129. return 0;
  130. ret = s6e88a0_ams452ef01_off(ctx);
  131. if (ret < 0)
  132. dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
  133. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  134. regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  135. ctx->prepared = false;
  136. return 0;
  137. }
  138. static const struct drm_display_mode s6e88a0_ams452ef01_mode = {
  139. .clock = (540 + 88 + 4 + 20) * (960 + 14 + 2 + 8) * 60 / 1000,
  140. .hdisplay = 540,
  141. .hsync_start = 540 + 88,
  142. .hsync_end = 540 + 88 + 4,
  143. .htotal = 540 + 88 + 4 + 20,
  144. .vdisplay = 960,
  145. .vsync_start = 960 + 14,
  146. .vsync_end = 960 + 14 + 2,
  147. .vtotal = 960 + 14 + 2 + 8,
  148. .width_mm = 56,
  149. .height_mm = 100,
  150. };
  151. static int s6e88a0_ams452ef01_get_modes(struct drm_panel *panel,
  152. struct drm_connector *connector)
  153. {
  154. struct drm_display_mode *mode;
  155. mode = drm_mode_duplicate(connector->dev, &s6e88a0_ams452ef01_mode);
  156. if (!mode)
  157. return -ENOMEM;
  158. drm_mode_set_name(mode);
  159. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  160. connector->display_info.width_mm = mode->width_mm;
  161. connector->display_info.height_mm = mode->height_mm;
  162. drm_mode_probed_add(connector, mode);
  163. return 1;
  164. }
  165. static const struct drm_panel_funcs s6e88a0_ams452ef01_panel_funcs = {
  166. .unprepare = s6e88a0_ams452ef01_unprepare,
  167. .prepare = s6e88a0_ams452ef01_prepare,
  168. .get_modes = s6e88a0_ams452ef01_get_modes,
  169. };
  170. static int s6e88a0_ams452ef01_probe(struct mipi_dsi_device *dsi)
  171. {
  172. struct device *dev = &dsi->dev;
  173. struct s6e88a0_ams452ef01 *ctx;
  174. int ret;
  175. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  176. if (!ctx)
  177. return -ENOMEM;
  178. ctx->supplies[0].supply = "vdd3";
  179. ctx->supplies[1].supply = "vci";
  180. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
  181. ctx->supplies);
  182. if (ret < 0) {
  183. dev_err(dev, "Failed to get regulators: %d\n", ret);
  184. return ret;
  185. }
  186. ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  187. if (IS_ERR(ctx->reset_gpio)) {
  188. ret = PTR_ERR(ctx->reset_gpio);
  189. dev_err(dev, "Failed to get reset-gpios: %d\n", ret);
  190. return ret;
  191. }
  192. ctx->dsi = dsi;
  193. mipi_dsi_set_drvdata(dsi, ctx);
  194. dsi->lanes = 2;
  195. dsi->format = MIPI_DSI_FMT_RGB888;
  196. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST;
  197. drm_panel_init(&ctx->panel, dev, &s6e88a0_ams452ef01_panel_funcs,
  198. DRM_MODE_CONNECTOR_DSI);
  199. drm_panel_add(&ctx->panel);
  200. ret = mipi_dsi_attach(dsi);
  201. if (ret < 0) {
  202. dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
  203. drm_panel_remove(&ctx->panel);
  204. return ret;
  205. }
  206. return 0;
  207. }
  208. static void s6e88a0_ams452ef01_remove(struct mipi_dsi_device *dsi)
  209. {
  210. struct s6e88a0_ams452ef01 *ctx = mipi_dsi_get_drvdata(dsi);
  211. int ret;
  212. ret = mipi_dsi_detach(dsi);
  213. if (ret < 0)
  214. dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
  215. drm_panel_remove(&ctx->panel);
  216. }
  217. static const struct of_device_id s6e88a0_ams452ef01_of_match[] = {
  218. { .compatible = "samsung,s6e88a0-ams452ef01" },
  219. { /* sentinel */ },
  220. };
  221. MODULE_DEVICE_TABLE(of, s6e88a0_ams452ef01_of_match);
  222. static struct mipi_dsi_driver s6e88a0_ams452ef01_driver = {
  223. .probe = s6e88a0_ams452ef01_probe,
  224. .remove = s6e88a0_ams452ef01_remove,
  225. .driver = {
  226. .name = "panel-s6e88a0-ams452ef01",
  227. .of_match_table = s6e88a0_ams452ef01_of_match,
  228. },
  229. };
  230. module_mipi_dsi_driver(s6e88a0_ams452ef01_driver);
  231. MODULE_AUTHOR("Michael Srba <[email protected]>");
  232. MODULE_DESCRIPTION("MIPI-DSI based Panel Driver for AMS452EF01 AMOLED LCD with a S6E88A0 controller");
  233. MODULE_LICENSE("GPL v2");