panel-elida-kd35t133.c 9.1 KB

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