panel-samsung-sofef00.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2020 Caleb Connolly <[email protected]>
  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/of_device.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/swab.h>
  13. #include <linux/backlight.h>
  14. #include <video/mipi_display.h>
  15. #include <drm/drm_mipi_dsi.h>
  16. #include <drm/drm_modes.h>
  17. #include <drm/drm_panel.h>
  18. struct sofef00_panel {
  19. struct drm_panel panel;
  20. struct mipi_dsi_device *dsi;
  21. struct regulator *supply;
  22. struct gpio_desc *reset_gpio;
  23. const struct drm_display_mode *mode;
  24. bool prepared;
  25. };
  26. static inline
  27. struct sofef00_panel *to_sofef00_panel(struct drm_panel *panel)
  28. {
  29. return container_of(panel, struct sofef00_panel, panel);
  30. }
  31. #define dsi_dcs_write_seq(dsi, seq...) do { \
  32. static const u8 d[] = { seq }; \
  33. int ret; \
  34. ret = mipi_dsi_dcs_write_buffer(dsi, d, ARRAY_SIZE(d)); \
  35. if (ret < 0) \
  36. return ret; \
  37. } while (0)
  38. static void sofef00_panel_reset(struct sofef00_panel *ctx)
  39. {
  40. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  41. usleep_range(5000, 6000);
  42. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  43. usleep_range(2000, 3000);
  44. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  45. usleep_range(12000, 13000);
  46. }
  47. static int sofef00_panel_on(struct sofef00_panel *ctx)
  48. {
  49. struct mipi_dsi_device *dsi = ctx->dsi;
  50. struct device *dev = &dsi->dev;
  51. int ret;
  52. dsi->mode_flags |= MIPI_DSI_MODE_LPM;
  53. ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
  54. if (ret < 0) {
  55. dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
  56. return ret;
  57. }
  58. usleep_range(10000, 11000);
  59. dsi_dcs_write_seq(dsi, 0xf0, 0x5a, 0x5a);
  60. ret = mipi_dsi_dcs_set_tear_on(dsi, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
  61. if (ret < 0) {
  62. dev_err(dev, "Failed to set tear on: %d\n", ret);
  63. return ret;
  64. }
  65. dsi_dcs_write_seq(dsi, 0xf0, 0xa5, 0xa5);
  66. dsi_dcs_write_seq(dsi, 0xf0, 0x5a, 0x5a);
  67. dsi_dcs_write_seq(dsi, 0xb0, 0x07);
  68. dsi_dcs_write_seq(dsi, 0xb6, 0x12);
  69. dsi_dcs_write_seq(dsi, 0xf0, 0xa5, 0xa5);
  70. dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x20);
  71. dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
  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 sofef00_panel_off(struct sofef00_panel *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(40);
  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(160);
  97. return 0;
  98. }
  99. static int sofef00_panel_prepare(struct drm_panel *panel)
  100. {
  101. struct sofef00_panel *ctx = to_sofef00_panel(panel);
  102. struct device *dev = &ctx->dsi->dev;
  103. int ret;
  104. if (ctx->prepared)
  105. return 0;
  106. ret = regulator_enable(ctx->supply);
  107. if (ret < 0) {
  108. dev_err(dev, "Failed to enable regulator: %d\n", ret);
  109. return ret;
  110. }
  111. sofef00_panel_reset(ctx);
  112. ret = sofef00_panel_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, 1);
  116. return ret;
  117. }
  118. ctx->prepared = true;
  119. return 0;
  120. }
  121. static int sofef00_panel_unprepare(struct drm_panel *panel)
  122. {
  123. struct sofef00_panel *ctx = to_sofef00_panel(panel);
  124. struct device *dev = &ctx->dsi->dev;
  125. int ret;
  126. if (!ctx->prepared)
  127. return 0;
  128. ret = sofef00_panel_off(ctx);
  129. if (ret < 0)
  130. dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
  131. regulator_disable(ctx->supply);
  132. ctx->prepared = false;
  133. return 0;
  134. }
  135. static const struct drm_display_mode enchilada_panel_mode = {
  136. .clock = (1080 + 112 + 16 + 36) * (2280 + 36 + 8 + 12) * 60 / 1000,
  137. .hdisplay = 1080,
  138. .hsync_start = 1080 + 112,
  139. .hsync_end = 1080 + 112 + 16,
  140. .htotal = 1080 + 112 + 16 + 36,
  141. .vdisplay = 2280,
  142. .vsync_start = 2280 + 36,
  143. .vsync_end = 2280 + 36 + 8,
  144. .vtotal = 2280 + 36 + 8 + 12,
  145. .width_mm = 68,
  146. .height_mm = 145,
  147. };
  148. static const struct drm_display_mode fajita_panel_mode = {
  149. .clock = (1080 + 72 + 16 + 36) * (2340 + 32 + 4 + 18) * 60 / 1000,
  150. .hdisplay = 1080,
  151. .hsync_start = 1080 + 72,
  152. .hsync_end = 1080 + 72 + 16,
  153. .htotal = 1080 + 72 + 16 + 36,
  154. .vdisplay = 2340,
  155. .vsync_start = 2340 + 32,
  156. .vsync_end = 2340 + 32 + 4,
  157. .vtotal = 2340 + 32 + 4 + 18,
  158. .width_mm = 68,
  159. .height_mm = 145,
  160. };
  161. static int sofef00_panel_get_modes(struct drm_panel *panel, struct drm_connector *connector)
  162. {
  163. struct drm_display_mode *mode;
  164. struct sofef00_panel *ctx = to_sofef00_panel(panel);
  165. mode = drm_mode_duplicate(connector->dev, ctx->mode);
  166. if (!mode)
  167. return -ENOMEM;
  168. drm_mode_set_name(mode);
  169. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  170. connector->display_info.width_mm = mode->width_mm;
  171. connector->display_info.height_mm = mode->height_mm;
  172. drm_mode_probed_add(connector, mode);
  173. return 1;
  174. }
  175. static const struct drm_panel_funcs sofef00_panel_panel_funcs = {
  176. .prepare = sofef00_panel_prepare,
  177. .unprepare = sofef00_panel_unprepare,
  178. .get_modes = sofef00_panel_get_modes,
  179. };
  180. static int sofef00_panel_bl_update_status(struct backlight_device *bl)
  181. {
  182. struct mipi_dsi_device *dsi = bl_get_data(bl);
  183. int err;
  184. u16 brightness;
  185. brightness = (u16)backlight_get_brightness(bl);
  186. // This panel needs the high and low bytes swapped for the brightness value
  187. brightness = __swab16(brightness);
  188. err = mipi_dsi_dcs_set_display_brightness(dsi, brightness);
  189. if (err < 0)
  190. return err;
  191. return 0;
  192. }
  193. static const struct backlight_ops sofef00_panel_bl_ops = {
  194. .update_status = sofef00_panel_bl_update_status,
  195. };
  196. static struct backlight_device *
  197. sofef00_create_backlight(struct mipi_dsi_device *dsi)
  198. {
  199. struct device *dev = &dsi->dev;
  200. const struct backlight_properties props = {
  201. .type = BACKLIGHT_PLATFORM,
  202. .brightness = 1023,
  203. .max_brightness = 1023,
  204. };
  205. return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
  206. &sofef00_panel_bl_ops, &props);
  207. }
  208. static int sofef00_panel_probe(struct mipi_dsi_device *dsi)
  209. {
  210. struct device *dev = &dsi->dev;
  211. struct sofef00_panel *ctx;
  212. int ret;
  213. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  214. if (!ctx)
  215. return -ENOMEM;
  216. ctx->mode = of_device_get_match_data(dev);
  217. if (!ctx->mode) {
  218. dev_err(dev, "Missing device mode\n");
  219. return -ENODEV;
  220. }
  221. ctx->supply = devm_regulator_get(dev, "vddio");
  222. if (IS_ERR(ctx->supply))
  223. return dev_err_probe(dev, PTR_ERR(ctx->supply),
  224. "Failed to get vddio regulator\n");
  225. ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  226. if (IS_ERR(ctx->reset_gpio))
  227. return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
  228. "Failed to get reset-gpios\n");
  229. ctx->dsi = dsi;
  230. mipi_dsi_set_drvdata(dsi, ctx);
  231. dsi->lanes = 4;
  232. dsi->format = MIPI_DSI_FMT_RGB888;
  233. drm_panel_init(&ctx->panel, dev, &sofef00_panel_panel_funcs,
  234. DRM_MODE_CONNECTOR_DSI);
  235. ctx->panel.backlight = sofef00_create_backlight(dsi);
  236. if (IS_ERR(ctx->panel.backlight))
  237. return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
  238. "Failed to create backlight\n");
  239. drm_panel_add(&ctx->panel);
  240. ret = mipi_dsi_attach(dsi);
  241. if (ret < 0) {
  242. dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
  243. drm_panel_remove(&ctx->panel);
  244. return ret;
  245. }
  246. return 0;
  247. }
  248. static void sofef00_panel_remove(struct mipi_dsi_device *dsi)
  249. {
  250. struct sofef00_panel *ctx = mipi_dsi_get_drvdata(dsi);
  251. int ret;
  252. ret = mipi_dsi_detach(dsi);
  253. if (ret < 0)
  254. dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
  255. drm_panel_remove(&ctx->panel);
  256. }
  257. static const struct of_device_id sofef00_panel_of_match[] = {
  258. { // OnePlus 6 / enchilada
  259. .compatible = "samsung,sofef00",
  260. .data = &enchilada_panel_mode,
  261. },
  262. { // OnePlus 6T / fajita
  263. .compatible = "samsung,s6e3fc2x01",
  264. .data = &fajita_panel_mode,
  265. },
  266. { /* sentinel */ }
  267. };
  268. MODULE_DEVICE_TABLE(of, sofef00_panel_of_match);
  269. static struct mipi_dsi_driver sofef00_panel_driver = {
  270. .probe = sofef00_panel_probe,
  271. .remove = sofef00_panel_remove,
  272. .driver = {
  273. .name = "panel-oneplus6",
  274. .of_match_table = sofef00_panel_of_match,
  275. },
  276. };
  277. module_mipi_dsi_driver(sofef00_panel_driver);
  278. MODULE_AUTHOR("Caleb Connolly <[email protected]>");
  279. MODULE_DESCRIPTION("DRM driver for Samsung AMOLED DSI panels found in OnePlus 6/6T phones");
  280. MODULE_LICENSE("GPL v2");