panel-elida-kd35t133.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Elida kd35t133 3.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. enum drm_panel_orientation orientation;
  41. };
  42. static inline struct kd35t133 *panel_to_kd35t133(struct drm_panel *panel)
  43. {
  44. return container_of(panel, struct kd35t133, panel);
  45. }
  46. static int kd35t133_init_sequence(struct kd35t133 *ctx)
  47. {
  48. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  49. struct device *dev = ctx->dev;
  50. /*
  51. * Init sequence was supplied by the panel vendor with minimal
  52. * documentation.
  53. */
  54. mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_POSITIVEGAMMA,
  55. 0x00, 0x13, 0x18, 0x04, 0x0f, 0x06, 0x3a, 0x56,
  56. 0x4d, 0x03, 0x0a, 0x06, 0x30, 0x3e, 0x0f);
  57. mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_NEGATIVEGAMMA,
  58. 0x00, 0x13, 0x18, 0x01, 0x11, 0x06, 0x38, 0x34,
  59. 0x4d, 0x06, 0x0d, 0x0b, 0x31, 0x37, 0x0f);
  60. mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_POWERCONTROL1, 0x18, 0x17);
  61. mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_POWERCONTROL2, 0x41);
  62. mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_VCOMCONTROL, 0x00, 0x1a, 0x80);
  63. mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x48);
  64. mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_PIXEL_FORMAT, 0x55);
  65. mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_INTERFACEMODECTRL, 0x00);
  66. mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_FRAMERATECTRL, 0xa0);
  67. mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_DISPLAYINVERSIONCTRL, 0x02);
  68. mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_DISPLAYFUNCTIONCTRL,
  69. 0x20, 0x02);
  70. mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_SETIMAGEFUNCTION, 0x00);
  71. mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_ADJUSTCONTROL3,
  72. 0xa9, 0x51, 0x2c, 0x82);
  73. mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_INVERT_MODE, NULL, 0);
  74. dev_dbg(dev, "Panel init sequence done\n");
  75. return 0;
  76. }
  77. static int kd35t133_unprepare(struct drm_panel *panel)
  78. {
  79. struct kd35t133 *ctx = panel_to_kd35t133(panel);
  80. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  81. int ret;
  82. ret = mipi_dsi_dcs_set_display_off(dsi);
  83. if (ret < 0)
  84. dev_err(ctx->dev, "failed to set display off: %d\n", ret);
  85. ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
  86. if (ret < 0) {
  87. dev_err(ctx->dev, "failed to enter sleep mode: %d\n", ret);
  88. return ret;
  89. }
  90. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  91. regulator_disable(ctx->iovcc);
  92. regulator_disable(ctx->vdd);
  93. return 0;
  94. }
  95. static int kd35t133_prepare(struct drm_panel *panel)
  96. {
  97. struct kd35t133 *ctx = panel_to_kd35t133(panel);
  98. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  99. int ret;
  100. dev_dbg(ctx->dev, "Resetting the panel\n");
  101. ret = regulator_enable(ctx->vdd);
  102. if (ret < 0) {
  103. dev_err(ctx->dev, "Failed to enable vdd supply: %d\n", ret);
  104. return ret;
  105. }
  106. ret = regulator_enable(ctx->iovcc);
  107. if (ret < 0) {
  108. dev_err(ctx->dev, "Failed to enable iovcc supply: %d\n", ret);
  109. goto disable_vdd;
  110. }
  111. msleep(20);
  112. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  113. usleep_range(10, 20);
  114. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  115. msleep(20);
  116. ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
  117. if (ret < 0) {
  118. dev_err(ctx->dev, "Failed to exit sleep mode: %d\n", ret);
  119. goto disable_iovcc;
  120. }
  121. msleep(250);
  122. ret = kd35t133_init_sequence(ctx);
  123. if (ret < 0) {
  124. dev_err(ctx->dev, "Panel init sequence failed: %d\n", ret);
  125. goto disable_iovcc;
  126. }
  127. ret = mipi_dsi_dcs_set_display_on(dsi);
  128. if (ret < 0) {
  129. dev_err(ctx->dev, "Failed to set display on: %d\n", ret);
  130. goto disable_iovcc;
  131. }
  132. msleep(50);
  133. return 0;
  134. disable_iovcc:
  135. regulator_disable(ctx->iovcc);
  136. disable_vdd:
  137. regulator_disable(ctx->vdd);
  138. return ret;
  139. }
  140. static const struct drm_display_mode default_mode = {
  141. .hdisplay = 320,
  142. .hsync_start = 320 + 130,
  143. .hsync_end = 320 + 130 + 4,
  144. .htotal = 320 + 130 + 4 + 130,
  145. .vdisplay = 480,
  146. .vsync_start = 480 + 2,
  147. .vsync_end = 480 + 2 + 1,
  148. .vtotal = 480 + 2 + 1 + 2,
  149. .clock = 17000,
  150. .width_mm = 42,
  151. .height_mm = 82,
  152. };
  153. static int kd35t133_get_modes(struct drm_panel *panel,
  154. struct drm_connector *connector)
  155. {
  156. struct kd35t133 *ctx = panel_to_kd35t133(panel);
  157. struct drm_display_mode *mode;
  158. mode = drm_mode_duplicate(connector->dev, &default_mode);
  159. if (!mode) {
  160. dev_err(ctx->dev, "Failed to add mode %ux%u@%u\n",
  161. default_mode.hdisplay, default_mode.vdisplay,
  162. drm_mode_vrefresh(&default_mode));
  163. return -ENOMEM;
  164. }
  165. drm_mode_set_name(mode);
  166. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  167. connector->display_info.width_mm = mode->width_mm;
  168. connector->display_info.height_mm = mode->height_mm;
  169. drm_mode_probed_add(connector, mode);
  170. return 1;
  171. }
  172. static enum drm_panel_orientation kd35t133_get_orientation(struct drm_panel *panel)
  173. {
  174. struct kd35t133 *ctx = panel_to_kd35t133(panel);
  175. return ctx->orientation;
  176. }
  177. static const struct drm_panel_funcs kd35t133_funcs = {
  178. .unprepare = kd35t133_unprepare,
  179. .prepare = kd35t133_prepare,
  180. .get_modes = kd35t133_get_modes,
  181. .get_orientation = kd35t133_get_orientation,
  182. };
  183. static int kd35t133_probe(struct mipi_dsi_device *dsi)
  184. {
  185. struct device *dev = &dsi->dev;
  186. struct kd35t133 *ctx;
  187. int ret;
  188. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  189. if (!ctx)
  190. return -ENOMEM;
  191. ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  192. if (IS_ERR(ctx->reset_gpio)) {
  193. dev_err(dev, "cannot get reset gpio\n");
  194. return PTR_ERR(ctx->reset_gpio);
  195. }
  196. ctx->vdd = devm_regulator_get(dev, "vdd");
  197. if (IS_ERR(ctx->vdd)) {
  198. ret = PTR_ERR(ctx->vdd);
  199. if (ret != -EPROBE_DEFER)
  200. dev_err(dev, "Failed to request vdd regulator: %d\n", ret);
  201. return ret;
  202. }
  203. ctx->iovcc = devm_regulator_get(dev, "iovcc");
  204. if (IS_ERR(ctx->iovcc)) {
  205. ret = PTR_ERR(ctx->iovcc);
  206. if (ret != -EPROBE_DEFER)
  207. dev_err(dev, "Failed to request iovcc regulator: %d\n", ret);
  208. return ret;
  209. }
  210. ret = of_drm_get_panel_orientation(dev->of_node, &ctx->orientation);
  211. if (ret < 0) {
  212. dev_err(dev, "%pOF: failed to get orientation %d\n", dev->of_node, ret);
  213. return ret;
  214. }
  215. mipi_dsi_set_drvdata(dsi, ctx);
  216. ctx->dev = dev;
  217. dsi->lanes = 1;
  218. dsi->format = MIPI_DSI_FMT_RGB888;
  219. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
  220. MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_NO_EOT_PACKET |
  221. MIPI_DSI_CLOCK_NON_CONTINUOUS;
  222. drm_panel_init(&ctx->panel, &dsi->dev, &kd35t133_funcs,
  223. DRM_MODE_CONNECTOR_DSI);
  224. ret = drm_panel_of_backlight(&ctx->panel);
  225. if (ret)
  226. return ret;
  227. drm_panel_add(&ctx->panel);
  228. ret = mipi_dsi_attach(dsi);
  229. if (ret < 0) {
  230. dev_err(dev, "mipi_dsi_attach failed: %d\n", ret);
  231. drm_panel_remove(&ctx->panel);
  232. return ret;
  233. }
  234. return 0;
  235. }
  236. static void kd35t133_remove(struct mipi_dsi_device *dsi)
  237. {
  238. struct kd35t133 *ctx = mipi_dsi_get_drvdata(dsi);
  239. int ret;
  240. ret = mipi_dsi_detach(dsi);
  241. if (ret < 0)
  242. dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
  243. drm_panel_remove(&ctx->panel);
  244. }
  245. static const struct of_device_id kd35t133_of_match[] = {
  246. { .compatible = "elida,kd35t133" },
  247. { /* sentinel */ }
  248. };
  249. MODULE_DEVICE_TABLE(of, kd35t133_of_match);
  250. static struct mipi_dsi_driver kd35t133_driver = {
  251. .driver = {
  252. .name = "panel-elida-kd35t133",
  253. .of_match_table = kd35t133_of_match,
  254. },
  255. .probe = kd35t133_probe,
  256. .remove = kd35t133_remove,
  257. };
  258. module_mipi_dsi_driver(kd35t133_driver);
  259. MODULE_AUTHOR("Heiko Stuebner <heiko.stuebner@theobroma-systems.com>");
  260. MODULE_DESCRIPTION("DRM driver for Elida kd35t133 MIPI DSI panel");
  261. MODULE_LICENSE("GPL v2");