panel-asus-z00t-tm5p5-n35596.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/backlight.h>
  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 <drm/drm_mipi_dsi.h>
  9. #include <drm/drm_modes.h>
  10. #include <drm/drm_panel.h>
  11. struct tm5p5_nt35596 {
  12. struct drm_panel panel;
  13. struct mipi_dsi_device *dsi;
  14. struct regulator_bulk_data supplies[2];
  15. struct gpio_desc *reset_gpio;
  16. bool prepared;
  17. };
  18. static inline struct tm5p5_nt35596 *to_tm5p5_nt35596(struct drm_panel *panel)
  19. {
  20. return container_of(panel, struct tm5p5_nt35596, panel);
  21. }
  22. #define dsi_generic_write_seq(dsi, seq...) do { \
  23. static const u8 d[] = { seq }; \
  24. int ret; \
  25. ret = mipi_dsi_generic_write(dsi, d, ARRAY_SIZE(d)); \
  26. if (ret < 0) \
  27. return ret; \
  28. } while (0)
  29. #define dsi_dcs_write_seq(dsi, seq...) do { \
  30. static const u8 d[] = { seq }; \
  31. int ret; \
  32. ret = mipi_dsi_dcs_write_buffer(dsi, d, ARRAY_SIZE(d)); \
  33. if (ret < 0) \
  34. return ret; \
  35. } while (0)
  36. static void tm5p5_nt35596_reset(struct tm5p5_nt35596 *ctx)
  37. {
  38. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  39. usleep_range(1000, 2000);
  40. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  41. usleep_range(1000, 2000);
  42. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  43. usleep_range(15000, 16000);
  44. }
  45. static int tm5p5_nt35596_on(struct tm5p5_nt35596 *ctx)
  46. {
  47. struct mipi_dsi_device *dsi = ctx->dsi;
  48. dsi_generic_write_seq(dsi, 0xff, 0x05);
  49. dsi_generic_write_seq(dsi, 0xfb, 0x01);
  50. dsi_generic_write_seq(dsi, 0xc5, 0x31);
  51. dsi_generic_write_seq(dsi, 0xff, 0x04);
  52. dsi_generic_write_seq(dsi, 0x01, 0x84);
  53. dsi_generic_write_seq(dsi, 0x05, 0x25);
  54. dsi_generic_write_seq(dsi, 0x06, 0x01);
  55. dsi_generic_write_seq(dsi, 0x07, 0x20);
  56. dsi_generic_write_seq(dsi, 0x08, 0x06);
  57. dsi_generic_write_seq(dsi, 0x09, 0x08);
  58. dsi_generic_write_seq(dsi, 0x0a, 0x10);
  59. dsi_generic_write_seq(dsi, 0x0b, 0x10);
  60. dsi_generic_write_seq(dsi, 0x0c, 0x10);
  61. dsi_generic_write_seq(dsi, 0x0d, 0x14);
  62. dsi_generic_write_seq(dsi, 0x0e, 0x14);
  63. dsi_generic_write_seq(dsi, 0x0f, 0x14);
  64. dsi_generic_write_seq(dsi, 0x10, 0x14);
  65. dsi_generic_write_seq(dsi, 0x11, 0x14);
  66. dsi_generic_write_seq(dsi, 0x12, 0x14);
  67. dsi_generic_write_seq(dsi, 0x17, 0xf3);
  68. dsi_generic_write_seq(dsi, 0x18, 0xc0);
  69. dsi_generic_write_seq(dsi, 0x19, 0xc0);
  70. dsi_generic_write_seq(dsi, 0x1a, 0xc0);
  71. dsi_generic_write_seq(dsi, 0x1b, 0xb3);
  72. dsi_generic_write_seq(dsi, 0x1c, 0xb3);
  73. dsi_generic_write_seq(dsi, 0x1d, 0xb3);
  74. dsi_generic_write_seq(dsi, 0x1e, 0xb3);
  75. dsi_generic_write_seq(dsi, 0x1f, 0xb3);
  76. dsi_generic_write_seq(dsi, 0x20, 0xb3);
  77. dsi_generic_write_seq(dsi, 0xfb, 0x01);
  78. dsi_generic_write_seq(dsi, 0xff, 0x00);
  79. dsi_generic_write_seq(dsi, 0xfb, 0x01);
  80. dsi_generic_write_seq(dsi, 0x35, 0x01);
  81. dsi_generic_write_seq(dsi, 0xd3, 0x06);
  82. dsi_generic_write_seq(dsi, 0xd4, 0x04);
  83. dsi_generic_write_seq(dsi, 0x5e, 0x0d);
  84. dsi_generic_write_seq(dsi, 0x11, 0x00);
  85. msleep(100);
  86. dsi_generic_write_seq(dsi, 0x29, 0x00);
  87. dsi_generic_write_seq(dsi, 0x53, 0x24);
  88. return 0;
  89. }
  90. static int tm5p5_nt35596_off(struct tm5p5_nt35596 *ctx)
  91. {
  92. struct mipi_dsi_device *dsi = ctx->dsi;
  93. struct device *dev = &dsi->dev;
  94. int ret;
  95. ret = mipi_dsi_dcs_set_display_off(dsi);
  96. if (ret < 0) {
  97. dev_err(dev, "Failed to set display off: %d\n", ret);
  98. return ret;
  99. }
  100. msleep(60);
  101. ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
  102. if (ret < 0) {
  103. dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
  104. return ret;
  105. }
  106. dsi_dcs_write_seq(dsi, 0x4f, 0x01);
  107. return 0;
  108. }
  109. static int tm5p5_nt35596_prepare(struct drm_panel *panel)
  110. {
  111. struct tm5p5_nt35596 *ctx = to_tm5p5_nt35596(panel);
  112. struct device *dev = &ctx->dsi->dev;
  113. int ret;
  114. if (ctx->prepared)
  115. return 0;
  116. ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  117. if (ret < 0) {
  118. dev_err(dev, "Failed to enable regulators: %d\n", ret);
  119. return ret;
  120. }
  121. tm5p5_nt35596_reset(ctx);
  122. ret = tm5p5_nt35596_on(ctx);
  123. if (ret < 0) {
  124. dev_err(dev, "Failed to initialize panel: %d\n", ret);
  125. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  126. regulator_bulk_disable(ARRAY_SIZE(ctx->supplies),
  127. ctx->supplies);
  128. return ret;
  129. }
  130. ctx->prepared = true;
  131. return 0;
  132. }
  133. static int tm5p5_nt35596_unprepare(struct drm_panel *panel)
  134. {
  135. struct tm5p5_nt35596 *ctx = to_tm5p5_nt35596(panel);
  136. struct device *dev = &ctx->dsi->dev;
  137. int ret;
  138. if (!ctx->prepared)
  139. return 0;
  140. ret = tm5p5_nt35596_off(ctx);
  141. if (ret < 0)
  142. dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
  143. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  144. regulator_bulk_disable(ARRAY_SIZE(ctx->supplies),
  145. ctx->supplies);
  146. ctx->prepared = false;
  147. return 0;
  148. }
  149. static const struct drm_display_mode tm5p5_nt35596_mode = {
  150. .clock = (1080 + 100 + 8 + 16) * (1920 + 4 + 2 + 4) * 60 / 1000,
  151. .hdisplay = 1080,
  152. .hsync_start = 1080 + 100,
  153. .hsync_end = 1080 + 100 + 8,
  154. .htotal = 1080 + 100 + 8 + 16,
  155. .vdisplay = 1920,
  156. .vsync_start = 1920 + 4,
  157. .vsync_end = 1920 + 4 + 2,
  158. .vtotal = 1920 + 4 + 2 + 4,
  159. .width_mm = 68,
  160. .height_mm = 121,
  161. };
  162. static int tm5p5_nt35596_get_modes(struct drm_panel *panel,
  163. struct drm_connector *connector)
  164. {
  165. struct drm_display_mode *mode;
  166. mode = drm_mode_duplicate(connector->dev, &tm5p5_nt35596_mode);
  167. if (!mode)
  168. return -ENOMEM;
  169. drm_mode_set_name(mode);
  170. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  171. connector->display_info.width_mm = mode->width_mm;
  172. connector->display_info.height_mm = mode->height_mm;
  173. drm_mode_probed_add(connector, mode);
  174. return 1;
  175. }
  176. static const struct drm_panel_funcs tm5p5_nt35596_panel_funcs = {
  177. .prepare = tm5p5_nt35596_prepare,
  178. .unprepare = tm5p5_nt35596_unprepare,
  179. .get_modes = tm5p5_nt35596_get_modes,
  180. };
  181. static int tm5p5_nt35596_bl_update_status(struct backlight_device *bl)
  182. {
  183. struct mipi_dsi_device *dsi = bl_get_data(bl);
  184. u16 brightness = bl->props.brightness;
  185. int ret;
  186. if (bl->props.power != FB_BLANK_UNBLANK ||
  187. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  188. bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  189. brightness = 0;
  190. dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
  191. ret = mipi_dsi_dcs_set_display_brightness(dsi, brightness);
  192. if (ret < 0)
  193. return ret;
  194. dsi->mode_flags |= MIPI_DSI_MODE_LPM;
  195. return 0;
  196. }
  197. static int tm5p5_nt35596_bl_get_brightness(struct backlight_device *bl)
  198. {
  199. struct mipi_dsi_device *dsi = bl_get_data(bl);
  200. u16 brightness = bl->props.brightness;
  201. int ret;
  202. dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
  203. ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness);
  204. if (ret < 0)
  205. return ret;
  206. dsi->mode_flags |= MIPI_DSI_MODE_LPM;
  207. return brightness & 0xff;
  208. }
  209. static const struct backlight_ops tm5p5_nt35596_bl_ops = {
  210. .update_status = tm5p5_nt35596_bl_update_status,
  211. .get_brightness = tm5p5_nt35596_bl_get_brightness,
  212. };
  213. static struct backlight_device *
  214. tm5p5_nt35596_create_backlight(struct mipi_dsi_device *dsi)
  215. {
  216. struct device *dev = &dsi->dev;
  217. const struct backlight_properties props = {
  218. .type = BACKLIGHT_RAW,
  219. .brightness = 255,
  220. .max_brightness = 255,
  221. };
  222. return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
  223. &tm5p5_nt35596_bl_ops, &props);
  224. }
  225. static int tm5p5_nt35596_probe(struct mipi_dsi_device *dsi)
  226. {
  227. struct device *dev = &dsi->dev;
  228. struct tm5p5_nt35596 *ctx;
  229. int ret;
  230. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  231. if (!ctx)
  232. return -ENOMEM;
  233. ctx->supplies[0].supply = "vdd";
  234. ctx->supplies[1].supply = "vddio";
  235. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
  236. ctx->supplies);
  237. if (ret < 0) {
  238. dev_err(dev, "Failed to get regulators: %d\n", ret);
  239. return ret;
  240. }
  241. ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  242. if (IS_ERR(ctx->reset_gpio)) {
  243. ret = PTR_ERR(ctx->reset_gpio);
  244. dev_err(dev, "Failed to get reset-gpios: %d\n", ret);
  245. return ret;
  246. }
  247. ctx->dsi = dsi;
  248. mipi_dsi_set_drvdata(dsi, ctx);
  249. dsi->lanes = 4;
  250. dsi->format = MIPI_DSI_FMT_RGB888;
  251. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
  252. MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_EOT_PACKET |
  253. MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM;
  254. drm_panel_init(&ctx->panel, dev, &tm5p5_nt35596_panel_funcs,
  255. DRM_MODE_CONNECTOR_DSI);
  256. ctx->panel.backlight = tm5p5_nt35596_create_backlight(dsi);
  257. if (IS_ERR(ctx->panel.backlight)) {
  258. ret = PTR_ERR(ctx->panel.backlight);
  259. dev_err(dev, "Failed to create backlight: %d\n", ret);
  260. return ret;
  261. }
  262. drm_panel_add(&ctx->panel);
  263. ret = mipi_dsi_attach(dsi);
  264. if (ret < 0) {
  265. dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
  266. return ret;
  267. }
  268. return 0;
  269. }
  270. static int tm5p5_nt35596_remove(struct mipi_dsi_device *dsi)
  271. {
  272. struct tm5p5_nt35596 *ctx = mipi_dsi_get_drvdata(dsi);
  273. int ret;
  274. ret = mipi_dsi_detach(dsi);
  275. if (ret < 0)
  276. dev_err(&dsi->dev,
  277. "Failed to detach from DSI host: %d\n", ret);
  278. drm_panel_remove(&ctx->panel);
  279. return 0;
  280. }
  281. static const struct of_device_id tm5p5_nt35596_of_match[] = {
  282. { .compatible = "asus,z00t-tm5p5-n35596" },
  283. { /* sentinel */ }
  284. };
  285. MODULE_DEVICE_TABLE(of, tm5p5_nt35596_of_match);
  286. static struct mipi_dsi_driver tm5p5_nt35596_driver = {
  287. .probe = tm5p5_nt35596_probe,
  288. .remove = tm5p5_nt35596_remove,
  289. .driver = {
  290. .name = "panel-tm5p5-nt35596",
  291. .of_match_table = tm5p5_nt35596_of_match,
  292. },
  293. };
  294. module_mipi_dsi_driver(tm5p5_nt35596_driver);
  295. MODULE_AUTHOR("Konrad Dybcio <konradybcio@gmail.com>");
  296. MODULE_DESCRIPTION("DRM driver for tm5p5 nt35596 1080p video mode dsi panel");
  297. MODULE_LICENSE("GPL v2");