panel-lincolntech-lcd197.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2024 BayLibre, SAS
  4. * Author: Jerome Brunet <jbrunet@baylibre.com>
  5. */
  6. #include <linux/gpio/consumer.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/regulator/consumer.h>
  10. #include <video/mipi_display.h>
  11. #include <drm/drm_device.h>
  12. #include <drm/drm_probe_helper.h>
  13. #include <drm/drm_mipi_dsi.h>
  14. #include <drm/drm_modes.h>
  15. #include <drm/drm_panel.h>
  16. struct lincoln_lcd197_panel {
  17. struct drm_panel panel;
  18. struct mipi_dsi_device *dsi;
  19. struct regulator *supply;
  20. struct gpio_desc *enable_gpio;
  21. struct gpio_desc *reset_gpio;
  22. };
  23. static inline
  24. struct lincoln_lcd197_panel *to_lincoln_lcd197_panel(struct drm_panel *panel)
  25. {
  26. return container_of(panel, struct lincoln_lcd197_panel, panel);
  27. }
  28. static int lincoln_lcd197_panel_prepare(struct drm_panel *panel)
  29. {
  30. struct lincoln_lcd197_panel *lcd = to_lincoln_lcd197_panel(panel);
  31. struct mipi_dsi_multi_context ctx = { .dsi = lcd->dsi };
  32. int err;
  33. gpiod_set_value_cansleep(lcd->enable_gpio, 0);
  34. err = regulator_enable(lcd->supply);
  35. if (err < 0)
  36. return err;
  37. gpiod_set_value_cansleep(lcd->enable_gpio, 1);
  38. usleep_range(1000, 2000);
  39. gpiod_set_value_cansleep(lcd->reset_gpio, 1);
  40. usleep_range(5000, 6000);
  41. gpiod_set_value_cansleep(lcd->reset_gpio, 0);
  42. msleep(50);
  43. mipi_dsi_dcs_write_seq_multi(&ctx, 0xb9, 0xff, 0x83, 0x99);
  44. mipi_dsi_dcs_write_seq_multi(&ctx, 0xd2, 0x55);
  45. mipi_dsi_dcs_write_seq_multi(&ctx, 0xb1, 0x02, 0x04, 0x70, 0x90, 0x01,
  46. 0x32, 0x33, 0x11, 0x11, 0x4d, 0x57, 0x56, 0x73,
  47. 0x02, 0x02);
  48. mipi_dsi_dcs_write_seq_multi(&ctx, 0xb2, 0x00, 0x80, 0x80, 0xae, 0x0a,
  49. 0x0e, 0x75, 0x11, 0x00, 0x00, 0x00);
  50. mipi_dsi_dcs_write_seq_multi(&ctx, 0xb4, 0x00, 0xff, 0x04, 0xa4, 0x02,
  51. 0xa0, 0x00, 0x00, 0x10, 0x00, 0x00, 0x02, 0x00,
  52. 0x24, 0x02, 0x04, 0x0a, 0x21, 0x03, 0x00, 0x00,
  53. 0x08, 0xa6, 0x88, 0x04, 0xa4, 0x02, 0xa0, 0x00,
  54. 0x00, 0x10, 0x00, 0x00, 0x02, 0x00, 0x24, 0x02,
  55. 0x04, 0x0a, 0x00, 0x00, 0x08, 0xa6, 0x00, 0x08,
  56. 0x11);
  57. mipi_dsi_dcs_write_seq_multi(&ctx, 0xd3, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x18, 0x18, 0x32, 0x10, 0x09, 0x00, 0x09,
  59. 0x32, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x11, 0x00, 0x02, 0x02, 0x03, 0x00,
  61. 0x00, 0x00, 0x0a, 0x40);
  62. mipi_dsi_dcs_write_seq_multi(&ctx, 0xd5, 0x18, 0x18, 0x18, 0x18, 0x21,
  63. 0x20, 0x18, 0x18, 0x19, 0x19, 0x19, 0x19, 0x18,
  64. 0x18, 0x18, 0x18, 0x03, 0x02, 0x01, 0x00, 0x2f,
  65. 0x2f, 0x30, 0x30, 0x31, 0x31, 0x18, 0x18, 0x18,
  66. 0x18, 0x18, 0x18);
  67. mipi_dsi_dcs_write_seq_multi(&ctx, 0xd6, 0x18, 0x18, 0x18, 0x18, 0x20,
  68. 0x21, 0x19, 0x19, 0x18, 0x18, 0x19, 0x19, 0x18,
  69. 0x18, 0x18, 0x18, 0x00, 0x01, 0x02, 0x03, 0x2f,
  70. 0x2f, 0x30, 0x30, 0x31, 0x31, 0x18, 0x18, 0x18,
  71. 0x18, 0x18, 0x18);
  72. mipi_dsi_dcs_write_seq_multi(&ctx, 0xbd, 0x01);
  73. mipi_dsi_dcs_write_seq_multi(&ctx, 0xd8, 0x0a, 0xbe, 0xfa, 0xa0, 0x0a,
  74. 0xbe, 0xfa, 0xa0);
  75. mipi_dsi_dcs_write_seq_multi(&ctx, 0xd8, 0x0f, 0xff, 0xff, 0xe0, 0x0f,
  76. 0xff, 0xff, 0xe0);
  77. mipi_dsi_dcs_write_seq_multi(&ctx, 0xbd, 0x02);
  78. mipi_dsi_dcs_write_seq_multi(&ctx, 0xd8, 0x0f, 0xff, 0xff, 0xe0, 0x0f,
  79. 0xff, 0xff, 0xe0);
  80. mipi_dsi_dcs_write_seq_multi(&ctx, 0xe0, 0x01, 0x11, 0x1c, 0x17, 0x39,
  81. 0x43, 0x54, 0x51, 0x5a, 0x64, 0x6c, 0x74, 0x7a,
  82. 0x83, 0x8d, 0x92, 0x99, 0xa4, 0xa9, 0xb4, 0xaa,
  83. 0xba, 0xbe, 0x63, 0x5e, 0x69, 0x73, 0x01, 0x11,
  84. 0x1c, 0x17, 0x39, 0x43, 0x54, 0x51, 0x5a, 0x64,
  85. 0x6c, 0x74, 0x7a, 0x83, 0x8d, 0x92, 0x99, 0xa4,
  86. 0xa7, 0xb2, 0xa9, 0xba, 0xbe, 0x63, 0x5e, 0x69,
  87. 0x73);
  88. mipi_dsi_usleep_range(&ctx, 200, 300);
  89. mipi_dsi_dcs_write_seq_multi(&ctx, 0xb6, 0x92, 0x92);
  90. mipi_dsi_dcs_write_seq_multi(&ctx, 0xcc, 0x00);
  91. mipi_dsi_dcs_write_seq_multi(&ctx, 0xbf, 0x40, 0x41, 0x50, 0x49);
  92. mipi_dsi_dcs_write_seq_multi(&ctx, 0xc6, 0xff, 0xf9);
  93. mipi_dsi_dcs_write_seq_multi(&ctx, 0xc0, 0x25, 0x5a);
  94. mipi_dsi_dcs_write_seq_multi(&ctx, MIPI_DCS_SET_ADDRESS_MODE, 0x02);
  95. mipi_dsi_dcs_exit_sleep_mode_multi(&ctx);
  96. mipi_dsi_msleep(&ctx, 120);
  97. if (ctx.accum_err) {
  98. gpiod_set_value_cansleep(lcd->enable_gpio, 0);
  99. gpiod_set_value_cansleep(lcd->reset_gpio, 1);
  100. regulator_disable(lcd->supply);
  101. }
  102. return ctx.accum_err;
  103. }
  104. static int lincoln_lcd197_panel_unprepare(struct drm_panel *panel)
  105. {
  106. struct lincoln_lcd197_panel *lcd = to_lincoln_lcd197_panel(panel);
  107. struct mipi_dsi_multi_context ctx = { .dsi = lcd->dsi };
  108. mipi_dsi_dcs_enter_sleep_mode_multi(&ctx);
  109. mipi_dsi_usleep_range(&ctx, 5000, 6000);
  110. gpiod_set_value_cansleep(lcd->enable_gpio, 0);
  111. gpiod_set_value_cansleep(lcd->reset_gpio, 1);
  112. regulator_disable(lcd->supply);
  113. return ctx.accum_err;
  114. }
  115. static int lincoln_lcd197_panel_enable(struct drm_panel *panel)
  116. {
  117. struct lincoln_lcd197_panel *lcd = to_lincoln_lcd197_panel(panel);
  118. struct mipi_dsi_multi_context ctx = { .dsi = lcd->dsi };
  119. mipi_dsi_dcs_set_display_on_multi(&ctx);
  120. mipi_dsi_msleep(&ctx, 20);
  121. return ctx.accum_err;
  122. }
  123. static int lincoln_lcd197_panel_disable(struct drm_panel *panel)
  124. {
  125. struct lincoln_lcd197_panel *lcd = to_lincoln_lcd197_panel(panel);
  126. struct mipi_dsi_multi_context ctx = { .dsi = lcd->dsi };
  127. mipi_dsi_dcs_set_display_off_multi(&ctx);
  128. mipi_dsi_msleep(&ctx, 50);
  129. return ctx.accum_err;
  130. }
  131. static const struct drm_display_mode lcd197_mode = {
  132. .clock = 154002,
  133. .hdisplay = 1080,
  134. .hsync_start = 1080 + 20,
  135. .hsync_end = 1080 + 20 + 6,
  136. .htotal = 1080 + 204,
  137. .vdisplay = 1920,
  138. .vsync_start = 1920 + 4,
  139. .vsync_end = 1920 + 4 + 4,
  140. .vtotal = 1920 + 79,
  141. .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
  142. .width_mm = 79,
  143. .height_mm = 125,
  144. .type = DRM_MODE_TYPE_DRIVER,
  145. };
  146. static int lincoln_lcd197_panel_get_modes(struct drm_panel *panel,
  147. struct drm_connector *connector)
  148. {
  149. return drm_connector_helper_get_modes_fixed(connector, &lcd197_mode);
  150. }
  151. static const struct drm_panel_funcs lincoln_lcd197_panel_funcs = {
  152. .prepare = lincoln_lcd197_panel_prepare,
  153. .unprepare = lincoln_lcd197_panel_unprepare,
  154. .enable = lincoln_lcd197_panel_enable,
  155. .disable = lincoln_lcd197_panel_disable,
  156. .get_modes = lincoln_lcd197_panel_get_modes,
  157. };
  158. static int lincoln_lcd197_panel_probe(struct mipi_dsi_device *dsi)
  159. {
  160. struct lincoln_lcd197_panel *lcd;
  161. struct device *dev = &dsi->dev;
  162. int err;
  163. dsi->lanes = 4;
  164. dsi->format = MIPI_DSI_FMT_RGB888;
  165. dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
  166. MIPI_DSI_MODE_VIDEO_BURST);
  167. lcd = devm_kzalloc(&dsi->dev, sizeof(*lcd), GFP_KERNEL);
  168. if (!lcd)
  169. return -ENOMEM;
  170. mipi_dsi_set_drvdata(dsi, lcd);
  171. lcd->dsi = dsi;
  172. lcd->supply = devm_regulator_get(dev, "power");
  173. if (IS_ERR(lcd->supply))
  174. return dev_err_probe(dev, PTR_ERR(lcd->supply),
  175. "failed to get power supply");
  176. lcd->enable_gpio = devm_gpiod_get(dev, "enable",
  177. GPIOD_OUT_HIGH);
  178. if (IS_ERR(lcd->enable_gpio))
  179. return dev_err_probe(dev, PTR_ERR(lcd->enable_gpio),
  180. "failed to get enable gpio");
  181. lcd->reset_gpio = devm_gpiod_get(dev, "reset",
  182. GPIOD_OUT_HIGH);
  183. if (IS_ERR(lcd->reset_gpio))
  184. return dev_err_probe(dev, PTR_ERR(lcd->reset_gpio),
  185. "failed to get reset gpio");
  186. drm_panel_init(&lcd->panel, dev,
  187. &lincoln_lcd197_panel_funcs, DRM_MODE_CONNECTOR_DSI);
  188. err = drm_panel_of_backlight(&lcd->panel);
  189. if (err)
  190. return err;
  191. drm_panel_add(&lcd->panel);
  192. err = mipi_dsi_attach(dsi);
  193. if (err)
  194. drm_panel_remove(&lcd->panel);
  195. return err;
  196. }
  197. static void lincoln_lcd197_panel_remove(struct mipi_dsi_device *dsi)
  198. {
  199. struct lincoln_lcd197_panel *lcd = mipi_dsi_get_drvdata(dsi);
  200. int err;
  201. err = mipi_dsi_detach(dsi);
  202. if (err < 0)
  203. dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
  204. drm_panel_remove(&lcd->panel);
  205. }
  206. static const struct of_device_id lincoln_lcd197_of_match[] = {
  207. { .compatible = "lincolntech,lcd197", },
  208. { /* sentinel */ }
  209. };
  210. MODULE_DEVICE_TABLE(of, lincoln_lcd197_of_match);
  211. static struct mipi_dsi_driver lincoln_lcd197_panel_driver = {
  212. .driver = {
  213. .name = "panel-lincolntech-lcd197",
  214. .of_match_table = lincoln_lcd197_of_match,
  215. },
  216. .probe = lincoln_lcd197_panel_probe,
  217. .remove = lincoln_lcd197_panel_remove,
  218. };
  219. module_mipi_dsi_driver(lincoln_lcd197_panel_driver);
  220. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  221. MODULE_DESCRIPTION("Lincoln Technologies LCD197 panel driver");
  222. MODULE_LICENSE("GPL");