panel-elida-kd35t133.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Elida kd35t133 5.5" MIPI-DSI panel driver
  4. * Copyright (C) 2020 Theobroma Systems Design und Consulting GmbH
  5. *
  6. * based on
  7. *
  8. * Rockteck jh057n00900 5.5" MIPI-DSI panel driver
  9. * Copyright (C) Purism SPC 2019
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/media-bus-format.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <video/display_timing.h>
  18. #include <video/mipi_display.h>
  19. #include <drm/drm_mipi_dsi.h>
  20. #include <drm/drm_modes.h>
  21. #include <drm/drm_panel.h>
  22. /* Manufacturer specific Commands send via DSI */
  23. #define KD35T133_CMD_INTERFACEMODECTRL 0xb0
  24. #define KD35T133_CMD_FRAMERATECTRL 0xb1
  25. #define KD35T133_CMD_DISPLAYINVERSIONCTRL 0xb4
  26. #define KD35T133_CMD_DISPLAYFUNCTIONCTRL 0xb6
  27. #define KD35T133_CMD_POWERCONTROL1 0xc0
  28. #define KD35T133_CMD_POWERCONTROL2 0xc1
  29. #define KD35T133_CMD_VCOMCONTROL 0xc5
  30. #define KD35T133_CMD_POSITIVEGAMMA 0xe0
  31. #define KD35T133_CMD_NEGATIVEGAMMA 0xe1
  32. #define KD35T133_CMD_SETIMAGEFUNCTION 0xe9
  33. #define KD35T133_CMD_ADJUSTCONTROL3 0xf7
  34. struct kd35t133 {
  35. struct device *dev;
  36. struct drm_panel panel;
  37. struct gpio_desc *reset_gpio;
  38. struct regulator *vdd;
  39. struct regulator *iovcc;
  40. bool prepared;
  41. };
  42. static inline struct kd35t133 *panel_to_kd35t133(struct drm_panel *panel)
  43. {
  44. return container_of(panel, struct kd35t133, panel);
  45. }
  46. #define dsi_dcs_write_seq(dsi, cmd, seq...) do { \
  47. static const u8 b[] = { cmd, seq }; \
  48. int ret; \
  49. ret = mipi_dsi_dcs_write_buffer(dsi, b, ARRAY_SIZE(b)); \
  50. if (ret < 0) \
  51. return ret; \
  52. } while (0)
  53. static int kd35t133_init_sequence(struct kd35t133 *ctx)
  54. {
  55. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  56. struct device *dev = ctx->dev;
  57. /*
  58. * Init sequence was supplied by the panel vendor with minimal
  59. * documentation.
  60. */
  61. dsi_dcs_write_seq(dsi, KD35T133_CMD_POSITIVEGAMMA,
  62. 0x00, 0x13, 0x18, 0x04, 0x0f, 0x06, 0x3a, 0x56,
  63. 0x4d, 0x03, 0x0a, 0x06, 0x30, 0x3e, 0x0f);
  64. dsi_dcs_write_seq(dsi, KD35T133_CMD_NEGATIVEGAMMA,
  65. 0x00, 0x13, 0x18, 0x01, 0x11, 0x06, 0x38, 0x34,
  66. 0x4d, 0x06, 0x0d, 0x0b, 0x31, 0x37, 0x0f);
  67. dsi_dcs_write_seq(dsi, KD35T133_CMD_POWERCONTROL1, 0x18, 0x17);
  68. dsi_dcs_write_seq(dsi, KD35T133_CMD_POWERCONTROL2, 0x41);
  69. dsi_dcs_write_seq(dsi, KD35T133_CMD_VCOMCONTROL, 0x00, 0x1a, 0x80);
  70. dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x48);
  71. dsi_dcs_write_seq(dsi, MIPI_DCS_SET_PIXEL_FORMAT, 0x55);
  72. dsi_dcs_write_seq(dsi, KD35T133_CMD_INTERFACEMODECTRL, 0x00);
  73. dsi_dcs_write_seq(dsi, KD35T133_CMD_FRAMERATECTRL, 0xa0);
  74. dsi_dcs_write_seq(dsi, KD35T133_CMD_DISPLAYINVERSIONCTRL, 0x02);
  75. dsi_dcs_write_seq(dsi, KD35T133_CMD_DISPLAYFUNCTIONCTRL,
  76. 0x20, 0x02);
  77. dsi_dcs_write_seq(dsi, KD35T133_CMD_SETIMAGEFUNCTION, 0x00);
  78. dsi_dcs_write_seq(dsi, KD35T133_CMD_ADJUSTCONTROL3,
  79. 0xa9, 0x51, 0x2c, 0x82);
  80. mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_INVERT_MODE, NULL, 0);
  81. dev_dbg(dev, "Panel init sequence done\n");
  82. return 0;
  83. }
  84. static int kd35t133_unprepare(struct drm_panel *panel)
  85. {
  86. struct kd35t133 *ctx = panel_to_kd35t133(panel);
  87. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  88. int ret;
  89. if (!ctx->prepared)
  90. return 0;
  91. ret = mipi_dsi_dcs_set_display_off(dsi);
  92. if (ret < 0)
  93. dev_err(ctx->dev, "failed to set display off: %d\n", ret);
  94. ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
  95. if (ret < 0) {
  96. dev_err(ctx->dev, "failed to enter sleep mode: %d\n", ret);
  97. return ret;
  98. }
  99. regulator_disable(ctx->iovcc);
  100. regulator_disable(ctx->vdd);
  101. ctx->prepared = false;
  102. return 0;
  103. }
  104. static int kd35t133_prepare(struct drm_panel *panel)
  105. {
  106. struct kd35t133 *ctx = panel_to_kd35t133(panel);
  107. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  108. int ret;
  109. if (ctx->prepared)
  110. return 0;
  111. dev_dbg(ctx->dev, "Resetting the panel\n");
  112. ret = regulator_enable(ctx->vdd);
  113. if (ret < 0) {
  114. dev_err(ctx->dev, "Failed to enable vdd supply: %d\n", ret);
  115. return ret;
  116. }
  117. ret = regulator_enable(ctx->iovcc);
  118. if (ret < 0) {
  119. dev_err(ctx->dev, "Failed to enable iovcc supply: %d\n", ret);
  120. goto disable_vdd;
  121. }
  122. msleep(20);
  123. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  124. usleep_range(10, 20);
  125. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  126. msleep(20);
  127. ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
  128. if (ret < 0) {
  129. dev_err(ctx->dev, "Failed to exit sleep mode: %d\n", ret);
  130. goto disable_iovcc;
  131. }
  132. msleep(250);
  133. ret = kd35t133_init_sequence(ctx);
  134. if (ret < 0) {
  135. dev_err(ctx->dev, "Panel init sequence failed: %d\n", ret);
  136. goto disable_iovcc;
  137. }
  138. ret = mipi_dsi_dcs_set_display_on(dsi);
  139. if (ret < 0) {
  140. dev_err(ctx->dev, "Failed to set display on: %d\n", ret);
  141. goto disable_iovcc;
  142. }
  143. msleep(50);
  144. ctx->prepared = true;
  145. return 0;
  146. disable_iovcc:
  147. regulator_disable(ctx->iovcc);
  148. disable_vdd:
  149. regulator_disable(ctx->vdd);
  150. return ret;
  151. }
  152. static const struct drm_display_mode default_mode = {
  153. .hdisplay = 320,
  154. .hsync_start = 320 + 130,
  155. .hsync_end = 320 + 130 + 4,
  156. .htotal = 320 + 130 + 4 + 130,
  157. .vdisplay = 480,
  158. .vsync_start = 480 + 2,
  159. .vsync_end = 480 + 2 + 1,
  160. .vtotal = 480 + 2 + 1 + 2,
  161. .clock = 17000,
  162. .width_mm = 42,
  163. .height_mm = 82,
  164. };
  165. static int kd35t133_get_modes(struct drm_panel *panel,
  166. struct drm_connector *connector)
  167. {
  168. struct kd35t133 *ctx = panel_to_kd35t133(panel);
  169. struct drm_display_mode *mode;
  170. mode = drm_mode_duplicate(connector->dev, &default_mode);
  171. if (!mode) {
  172. dev_err(ctx->dev, "Failed to add mode %ux%u@%u\n",
  173. default_mode.hdisplay, default_mode.vdisplay,
  174. drm_mode_vrefresh(&default_mode));
  175. return -ENOMEM;
  176. }
  177. drm_mode_set_name(mode);
  178. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  179. connector->display_info.width_mm = mode->width_mm;
  180. connector->display_info.height_mm = mode->height_mm;
  181. drm_mode_probed_add(connector, mode);
  182. return 1;
  183. }
  184. static const struct drm_panel_funcs kd35t133_funcs = {
  185. .unprepare = kd35t133_unprepare,
  186. .prepare = kd35t133_prepare,
  187. .get_modes = kd35t133_get_modes,
  188. };
  189. static int kd35t133_probe(struct mipi_dsi_device *dsi)
  190. {
  191. struct device *dev = &dsi->dev;
  192. struct kd35t133 *ctx;
  193. int ret;
  194. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  195. if (!ctx)
  196. return -ENOMEM;
  197. ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  198. if (IS_ERR(ctx->reset_gpio)) {
  199. dev_err(dev, "cannot get reset gpio\n");
  200. return PTR_ERR(ctx->reset_gpio);
  201. }
  202. ctx->vdd = devm_regulator_get(dev, "vdd");
  203. if (IS_ERR(ctx->vdd)) {
  204. ret = PTR_ERR(ctx->vdd);
  205. if (ret != -EPROBE_DEFER)
  206. dev_err(dev, "Failed to request vdd regulator: %d\n", ret);
  207. return ret;
  208. }
  209. ctx->iovcc = devm_regulator_get(dev, "iovcc");
  210. if (IS_ERR(ctx->iovcc)) {
  211. ret = PTR_ERR(ctx->iovcc);
  212. if (ret != -EPROBE_DEFER)
  213. dev_err(dev, "Failed to request iovcc regulator: %d\n", ret);
  214. return ret;
  215. }
  216. mipi_dsi_set_drvdata(dsi, ctx);
  217. ctx->dev = dev;
  218. dsi->lanes = 1;
  219. dsi->format = MIPI_DSI_FMT_RGB888;
  220. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
  221. MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_EOT_PACKET |
  222. MIPI_DSI_CLOCK_NON_CONTINUOUS;
  223. drm_panel_init(&ctx->panel, &dsi->dev, &kd35t133_funcs,
  224. DRM_MODE_CONNECTOR_DSI);
  225. ret = drm_panel_of_backlight(&ctx->panel);
  226. if (ret)
  227. return ret;
  228. drm_panel_add(&ctx->panel);
  229. ret = mipi_dsi_attach(dsi);
  230. if (ret < 0) {
  231. dev_err(dev, "mipi_dsi_attach failed: %d\n", ret);
  232. drm_panel_remove(&ctx->panel);
  233. return ret;
  234. }
  235. return 0;
  236. }
  237. static void kd35t133_shutdown(struct mipi_dsi_device *dsi)
  238. {
  239. struct kd35t133 *ctx = mipi_dsi_get_drvdata(dsi);
  240. int ret;
  241. ret = drm_panel_unprepare(&ctx->panel);
  242. if (ret < 0)
  243. dev_err(&dsi->dev, "Failed to unprepare panel: %d\n", ret);
  244. ret = drm_panel_disable(&ctx->panel);
  245. if (ret < 0)
  246. dev_err(&dsi->dev, "Failed to disable panel: %d\n", ret);
  247. }
  248. static int kd35t133_remove(struct mipi_dsi_device *dsi)
  249. {
  250. struct kd35t133 *ctx = mipi_dsi_get_drvdata(dsi);
  251. int ret;
  252. kd35t133_shutdown(dsi);
  253. ret = mipi_dsi_detach(dsi);
  254. if (ret < 0)
  255. dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
  256. drm_panel_remove(&ctx->panel);
  257. return 0;
  258. }
  259. static const struct of_device_id kd35t133_of_match[] = {
  260. { .compatible = "elida,kd35t133" },
  261. { /* sentinel */ }
  262. };
  263. MODULE_DEVICE_TABLE(of, kd35t133_of_match);
  264. static struct mipi_dsi_driver kd35t133_driver = {
  265. .driver = {
  266. .name = "panel-elida-kd35t133",
  267. .of_match_table = kd35t133_of_match,
  268. },
  269. .probe = kd35t133_probe,
  270. .remove = kd35t133_remove,
  271. .shutdown = kd35t133_shutdown,
  272. };
  273. module_mipi_dsi_driver(kd35t133_driver);
  274. MODULE_AUTHOR("Heiko Stuebner <heiko.stuebner@theobroma-systems.com>");
  275. MODULE_DESCRIPTION("DRM driver for Elida kd35t133 MIPI DSI panel");
  276. MODULE_LICENSE("GPL v2");