panel-samsung-sofef00.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2020 Caleb Connolly <caleb@connolly.tech>
  3. * Generated with linux-mdss-dsi-panel-driver-generator from vendor device tree:
  4. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <linux/backlight.h>
  12. #include <video/mipi_display.h>
  13. #include <drm/drm_mipi_dsi.h>
  14. #include <drm/drm_modes.h>
  15. #include <drm/drm_panel.h>
  16. struct sofef00_panel {
  17. struct drm_panel panel;
  18. struct mipi_dsi_device *dsi;
  19. struct regulator *supply;
  20. struct gpio_desc *reset_gpio;
  21. };
  22. static inline
  23. struct sofef00_panel *to_sofef00_panel(struct drm_panel *panel)
  24. {
  25. return container_of(panel, struct sofef00_panel, panel);
  26. }
  27. static void sofef00_panel_reset(struct sofef00_panel *ctx)
  28. {
  29. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  30. usleep_range(5000, 6000);
  31. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  32. usleep_range(2000, 3000);
  33. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  34. usleep_range(12000, 13000);
  35. }
  36. static int sofef00_panel_on(struct sofef00_panel *ctx)
  37. {
  38. struct mipi_dsi_device *dsi = ctx->dsi;
  39. struct device *dev = &dsi->dev;
  40. int ret;
  41. dsi->mode_flags |= MIPI_DSI_MODE_LPM;
  42. ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
  43. if (ret < 0) {
  44. dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
  45. return ret;
  46. }
  47. usleep_range(10000, 11000);
  48. mipi_dsi_dcs_write_seq(dsi, 0xf0, 0x5a, 0x5a);
  49. ret = mipi_dsi_dcs_set_tear_on(dsi, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
  50. if (ret < 0) {
  51. dev_err(dev, "Failed to set tear on: %d\n", ret);
  52. return ret;
  53. }
  54. mipi_dsi_dcs_write_seq(dsi, 0xf0, 0xa5, 0xa5);
  55. mipi_dsi_dcs_write_seq(dsi, 0xf0, 0x5a, 0x5a);
  56. mipi_dsi_dcs_write_seq(dsi, 0xb0, 0x07);
  57. mipi_dsi_dcs_write_seq(dsi, 0xb6, 0x12);
  58. mipi_dsi_dcs_write_seq(dsi, 0xf0, 0xa5, 0xa5);
  59. mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x20);
  60. mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
  61. ret = mipi_dsi_dcs_set_display_on(dsi);
  62. if (ret < 0) {
  63. dev_err(dev, "Failed to set display on: %d\n", ret);
  64. return ret;
  65. }
  66. return 0;
  67. }
  68. static int sofef00_panel_off(struct sofef00_panel *ctx)
  69. {
  70. struct mipi_dsi_device *dsi = ctx->dsi;
  71. struct device *dev = &dsi->dev;
  72. int ret;
  73. dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
  74. ret = mipi_dsi_dcs_set_display_off(dsi);
  75. if (ret < 0) {
  76. dev_err(dev, "Failed to set display off: %d\n", ret);
  77. return ret;
  78. }
  79. msleep(40);
  80. ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
  81. if (ret < 0) {
  82. dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
  83. return ret;
  84. }
  85. msleep(160);
  86. return 0;
  87. }
  88. static int sofef00_panel_prepare(struct drm_panel *panel)
  89. {
  90. struct sofef00_panel *ctx = to_sofef00_panel(panel);
  91. struct device *dev = &ctx->dsi->dev;
  92. int ret;
  93. ret = regulator_enable(ctx->supply);
  94. if (ret < 0) {
  95. dev_err(dev, "Failed to enable regulator: %d\n", ret);
  96. return ret;
  97. }
  98. sofef00_panel_reset(ctx);
  99. ret = sofef00_panel_on(ctx);
  100. if (ret < 0) {
  101. dev_err(dev, "Failed to initialize panel: %d\n", ret);
  102. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  103. return ret;
  104. }
  105. return 0;
  106. }
  107. static int sofef00_panel_unprepare(struct drm_panel *panel)
  108. {
  109. struct sofef00_panel *ctx = to_sofef00_panel(panel);
  110. struct device *dev = &ctx->dsi->dev;
  111. int ret;
  112. ret = sofef00_panel_off(ctx);
  113. if (ret < 0)
  114. dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
  115. regulator_disable(ctx->supply);
  116. return 0;
  117. }
  118. static const struct drm_display_mode enchilada_panel_mode = {
  119. .clock = (1080 + 112 + 16 + 36) * (2280 + 36 + 8 + 12) * 60 / 1000,
  120. .hdisplay = 1080,
  121. .hsync_start = 1080 + 112,
  122. .hsync_end = 1080 + 112 + 16,
  123. .htotal = 1080 + 112 + 16 + 36,
  124. .vdisplay = 2280,
  125. .vsync_start = 2280 + 36,
  126. .vsync_end = 2280 + 36 + 8,
  127. .vtotal = 2280 + 36 + 8 + 12,
  128. .width_mm = 68,
  129. .height_mm = 145,
  130. };
  131. static int sofef00_panel_get_modes(struct drm_panel *panel, struct drm_connector *connector)
  132. {
  133. struct drm_display_mode *mode;
  134. mode = drm_mode_duplicate(connector->dev, &enchilada_panel_mode);
  135. if (!mode)
  136. return -ENOMEM;
  137. drm_mode_set_name(mode);
  138. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  139. connector->display_info.width_mm = mode->width_mm;
  140. connector->display_info.height_mm = mode->height_mm;
  141. drm_mode_probed_add(connector, mode);
  142. return 1;
  143. }
  144. static const struct drm_panel_funcs sofef00_panel_panel_funcs = {
  145. .prepare = sofef00_panel_prepare,
  146. .unprepare = sofef00_panel_unprepare,
  147. .get_modes = sofef00_panel_get_modes,
  148. };
  149. static int sofef00_panel_bl_update_status(struct backlight_device *bl)
  150. {
  151. struct mipi_dsi_device *dsi = bl_get_data(bl);
  152. int err;
  153. u16 brightness = (u16)backlight_get_brightness(bl);
  154. err = mipi_dsi_dcs_set_display_brightness_large(dsi, brightness);
  155. if (err < 0)
  156. return err;
  157. return 0;
  158. }
  159. static const struct backlight_ops sofef00_panel_bl_ops = {
  160. .update_status = sofef00_panel_bl_update_status,
  161. };
  162. static struct backlight_device *
  163. sofef00_create_backlight(struct mipi_dsi_device *dsi)
  164. {
  165. struct device *dev = &dsi->dev;
  166. const struct backlight_properties props = {
  167. .type = BACKLIGHT_PLATFORM,
  168. .brightness = 1023,
  169. .max_brightness = 1023,
  170. };
  171. return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
  172. &sofef00_panel_bl_ops, &props);
  173. }
  174. static int sofef00_panel_probe(struct mipi_dsi_device *dsi)
  175. {
  176. struct device *dev = &dsi->dev;
  177. struct sofef00_panel *ctx;
  178. int ret;
  179. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  180. if (!ctx)
  181. return -ENOMEM;
  182. ctx->supply = devm_regulator_get(dev, "vddio");
  183. if (IS_ERR(ctx->supply))
  184. return dev_err_probe(dev, PTR_ERR(ctx->supply),
  185. "Failed to get vddio regulator\n");
  186. ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  187. if (IS_ERR(ctx->reset_gpio))
  188. return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
  189. "Failed to get reset-gpios\n");
  190. ctx->dsi = dsi;
  191. mipi_dsi_set_drvdata(dsi, ctx);
  192. dsi->lanes = 4;
  193. dsi->format = MIPI_DSI_FMT_RGB888;
  194. drm_panel_init(&ctx->panel, dev, &sofef00_panel_panel_funcs,
  195. DRM_MODE_CONNECTOR_DSI);
  196. ctx->panel.backlight = sofef00_create_backlight(dsi);
  197. if (IS_ERR(ctx->panel.backlight))
  198. return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
  199. "Failed to create backlight\n");
  200. drm_panel_add(&ctx->panel);
  201. ret = mipi_dsi_attach(dsi);
  202. if (ret < 0) {
  203. dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
  204. drm_panel_remove(&ctx->panel);
  205. return ret;
  206. }
  207. return 0;
  208. }
  209. static void sofef00_panel_remove(struct mipi_dsi_device *dsi)
  210. {
  211. struct sofef00_panel *ctx = mipi_dsi_get_drvdata(dsi);
  212. int ret;
  213. ret = mipi_dsi_detach(dsi);
  214. if (ret < 0)
  215. dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
  216. drm_panel_remove(&ctx->panel);
  217. }
  218. static const struct of_device_id sofef00_panel_of_match[] = {
  219. { .compatible = "samsung,sofef00" },
  220. { /* sentinel */ }
  221. };
  222. MODULE_DEVICE_TABLE(of, sofef00_panel_of_match);
  223. static struct mipi_dsi_driver sofef00_panel_driver = {
  224. .probe = sofef00_panel_probe,
  225. .remove = sofef00_panel_remove,
  226. .driver = {
  227. .name = "panel-oneplus6",
  228. .of_match_table = sofef00_panel_of_match,
  229. },
  230. };
  231. module_mipi_dsi_driver(sofef00_panel_driver);
  232. MODULE_AUTHOR("Caleb Connolly <caleb@connolly.tech>");
  233. MODULE_DESCRIPTION("DRM driver for Samsung AMOLED DSI panels found in OnePlus 6/6T phones");
  234. MODULE_LICENSE("GPL v2");