panel-raydium-rm68200.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2017
  4. *
  5. * Authors: Philippe Cornu <philippe.cornu@st.com>
  6. * Yannick Fertre <yannick.fertre@st.com>
  7. */
  8. #include <linux/backlight.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <video/mipi_display.h>
  12. #include <drm/drmP.h>
  13. #include <drm/drm_mipi_dsi.h>
  14. #include <drm/drm_panel.h>
  15. /*** Manufacturer Command Set ***/
  16. #define MCS_CMD_MODE_SW 0xFE /* CMD Mode Switch */
  17. #define MCS_CMD1_UCS 0x00 /* User Command Set (UCS = CMD1) */
  18. #define MCS_CMD2_P0 0x01 /* Manufacture Command Set Page0 (CMD2 P0) */
  19. #define MCS_CMD2_P1 0x02 /* Manufacture Command Set Page1 (CMD2 P1) */
  20. #define MCS_CMD2_P2 0x03 /* Manufacture Command Set Page2 (CMD2 P2) */
  21. #define MCS_CMD2_P3 0x04 /* Manufacture Command Set Page3 (CMD2 P3) */
  22. /* CMD2 P0 commands (Display Options and Power) */
  23. #define MCS_STBCTR 0x12 /* TE1 Output Setting Zig-Zag Connection */
  24. #define MCS_SGOPCTR 0x16 /* Source Bias Current */
  25. #define MCS_SDCTR 0x1A /* Source Output Delay Time */
  26. #define MCS_INVCTR 0x1B /* Inversion Type */
  27. #define MCS_EXT_PWR_IC 0x24 /* External PWR IC Control */
  28. #define MCS_SETAVDD 0x27 /* PFM Control for AVDD Output */
  29. #define MCS_SETAVEE 0x29 /* PFM Control for AVEE Output */
  30. #define MCS_BT2CTR 0x2B /* DDVDL Charge Pump Control */
  31. #define MCS_BT3CTR 0x2F /* VGH Charge Pump Control */
  32. #define MCS_BT4CTR 0x34 /* VGL Charge Pump Control */
  33. #define MCS_VCMCTR 0x46 /* VCOM Output Level Control */
  34. #define MCS_SETVGN 0x52 /* VG M/S N Control */
  35. #define MCS_SETVGP 0x54 /* VG M/S P Control */
  36. #define MCS_SW_CTRL 0x5F /* Interface Control for PFM and MIPI */
  37. /* CMD2 P2 commands (GOA Timing Control) - no description in datasheet */
  38. #define GOA_VSTV1 0x00
  39. #define GOA_VSTV2 0x07
  40. #define GOA_VCLK1 0x0E
  41. #define GOA_VCLK2 0x17
  42. #define GOA_VCLK_OPT1 0x20
  43. #define GOA_BICLK1 0x2A
  44. #define GOA_BICLK2 0x37
  45. #define GOA_BICLK3 0x44
  46. #define GOA_BICLK4 0x4F
  47. #define GOA_BICLK_OPT1 0x5B
  48. #define GOA_BICLK_OPT2 0x60
  49. #define MCS_GOA_GPO1 0x6D
  50. #define MCS_GOA_GPO2 0x71
  51. #define MCS_GOA_EQ 0x74
  52. #define MCS_GOA_CLK_GALLON 0x7C
  53. #define MCS_GOA_FS_SEL0 0x7E
  54. #define MCS_GOA_FS_SEL1 0x87
  55. #define MCS_GOA_FS_SEL2 0x91
  56. #define MCS_GOA_FS_SEL3 0x9B
  57. #define MCS_GOA_BS_SEL0 0xAC
  58. #define MCS_GOA_BS_SEL1 0xB5
  59. #define MCS_GOA_BS_SEL2 0xBF
  60. #define MCS_GOA_BS_SEL3 0xC9
  61. #define MCS_GOA_BS_SEL4 0xD3
  62. /* CMD2 P3 commands (Gamma) */
  63. #define MCS_GAMMA_VP 0x60 /* Gamma VP1~VP16 */
  64. #define MCS_GAMMA_VN 0x70 /* Gamma VN1~VN16 */
  65. struct rm68200 {
  66. struct device *dev;
  67. struct drm_panel panel;
  68. struct gpio_desc *reset_gpio;
  69. struct regulator *supply;
  70. struct backlight_device *backlight;
  71. bool prepared;
  72. bool enabled;
  73. };
  74. static const struct drm_display_mode default_mode = {
  75. .clock = 52582,
  76. .hdisplay = 720,
  77. .hsync_start = 720 + 38,
  78. .hsync_end = 720 + 38 + 8,
  79. .htotal = 720 + 38 + 8 + 38,
  80. .vdisplay = 1280,
  81. .vsync_start = 1280 + 12,
  82. .vsync_end = 1280 + 12 + 4,
  83. .vtotal = 1280 + 12 + 4 + 12,
  84. .vrefresh = 50,
  85. .flags = 0,
  86. .width_mm = 68,
  87. .height_mm = 122,
  88. };
  89. static inline struct rm68200 *panel_to_rm68200(struct drm_panel *panel)
  90. {
  91. return container_of(panel, struct rm68200, panel);
  92. }
  93. static void rm68200_dcs_write_buf(struct rm68200 *ctx, const void *data,
  94. size_t len)
  95. {
  96. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  97. int err;
  98. err = mipi_dsi_dcs_write_buffer(dsi, data, len);
  99. if (err < 0)
  100. DRM_ERROR_RATELIMITED("MIPI DSI DCS write buffer failed: %d\n",
  101. err);
  102. }
  103. static void rm68200_dcs_write_cmd(struct rm68200 *ctx, u8 cmd, u8 value)
  104. {
  105. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  106. int err;
  107. err = mipi_dsi_dcs_write(dsi, cmd, &value, 1);
  108. if (err < 0)
  109. DRM_ERROR_RATELIMITED("MIPI DSI DCS write failed: %d\n", err);
  110. }
  111. #define dcs_write_seq(ctx, seq...) \
  112. ({ \
  113. static const u8 d[] = { seq }; \
  114. \
  115. rm68200_dcs_write_buf(ctx, d, ARRAY_SIZE(d)); \
  116. })
  117. /*
  118. * This panel is not able to auto-increment all cmd addresses so for some of
  119. * them, we need to send them one by one...
  120. */
  121. #define dcs_write_cmd_seq(ctx, cmd, seq...) \
  122. ({ \
  123. static const u8 d[] = { seq }; \
  124. unsigned int i; \
  125. \
  126. for (i = 0; i < ARRAY_SIZE(d) ; i++) \
  127. rm68200_dcs_write_cmd(ctx, cmd + i, d[i]); \
  128. })
  129. static void rm68200_init_sequence(struct rm68200 *ctx)
  130. {
  131. /* Enter CMD2 with page 0 */
  132. dcs_write_seq(ctx, MCS_CMD_MODE_SW, MCS_CMD2_P0);
  133. dcs_write_cmd_seq(ctx, MCS_EXT_PWR_IC, 0xC0, 0x53, 0x00);
  134. dcs_write_seq(ctx, MCS_BT2CTR, 0xE5);
  135. dcs_write_seq(ctx, MCS_SETAVDD, 0x0A);
  136. dcs_write_seq(ctx, MCS_SETAVEE, 0x0A);
  137. dcs_write_seq(ctx, MCS_SGOPCTR, 0x52);
  138. dcs_write_seq(ctx, MCS_BT3CTR, 0x53);
  139. dcs_write_seq(ctx, MCS_BT4CTR, 0x5A);
  140. dcs_write_seq(ctx, MCS_INVCTR, 0x00);
  141. dcs_write_seq(ctx, MCS_STBCTR, 0x0A);
  142. dcs_write_seq(ctx, MCS_SDCTR, 0x06);
  143. dcs_write_seq(ctx, MCS_VCMCTR, 0x56);
  144. dcs_write_seq(ctx, MCS_SETVGN, 0xA0, 0x00);
  145. dcs_write_seq(ctx, MCS_SETVGP, 0xA0, 0x00);
  146. dcs_write_seq(ctx, MCS_SW_CTRL, 0x11); /* 2 data lanes, see doc */
  147. dcs_write_seq(ctx, MCS_CMD_MODE_SW, MCS_CMD2_P2);
  148. dcs_write_seq(ctx, GOA_VSTV1, 0x05);
  149. dcs_write_seq(ctx, 0x02, 0x0B);
  150. dcs_write_seq(ctx, 0x03, 0x0F);
  151. dcs_write_seq(ctx, 0x04, 0x7D, 0x00, 0x50);
  152. dcs_write_cmd_seq(ctx, GOA_VSTV2, 0x05, 0x16, 0x0D, 0x11, 0x7D, 0x00,
  153. 0x50);
  154. dcs_write_cmd_seq(ctx, GOA_VCLK1, 0x07, 0x08, 0x01, 0x02, 0x00, 0x7D,
  155. 0x00, 0x85, 0x08);
  156. dcs_write_cmd_seq(ctx, GOA_VCLK2, 0x03, 0x04, 0x05, 0x06, 0x00, 0x7D,
  157. 0x00, 0x85, 0x08);
  158. dcs_write_seq(ctx, GOA_VCLK_OPT1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  159. 0x00, 0x00, 0x00, 0x00);
  160. dcs_write_cmd_seq(ctx, GOA_BICLK1, 0x07, 0x08);
  161. dcs_write_seq(ctx, 0x2D, 0x01);
  162. dcs_write_seq(ctx, 0x2F, 0x02, 0x00, 0x40, 0x05, 0x08, 0x54, 0x7D,
  163. 0x00);
  164. dcs_write_cmd_seq(ctx, GOA_BICLK2, 0x03, 0x04, 0x05, 0x06, 0x00);
  165. dcs_write_seq(ctx, 0x3D, 0x40);
  166. dcs_write_seq(ctx, 0x3F, 0x05, 0x08, 0x54, 0x7D, 0x00);
  167. dcs_write_seq(ctx, GOA_BICLK3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  168. 0x00, 0x00, 0x00, 0x00, 0x00);
  169. dcs_write_seq(ctx, GOA_BICLK4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  170. 0x00, 0x00);
  171. dcs_write_seq(ctx, 0x58, 0x00, 0x00, 0x00);
  172. dcs_write_seq(ctx, GOA_BICLK_OPT1, 0x00, 0x00, 0x00, 0x00, 0x00);
  173. dcs_write_seq(ctx, GOA_BICLK_OPT2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  175. dcs_write_seq(ctx, MCS_GOA_GPO1, 0x00, 0x00, 0x00, 0x00);
  176. dcs_write_seq(ctx, MCS_GOA_GPO2, 0x00, 0x20, 0x00);
  177. dcs_write_seq(ctx, MCS_GOA_EQ, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  178. 0x00, 0x00);
  179. dcs_write_seq(ctx, MCS_GOA_CLK_GALLON, 0x00, 0x00);
  180. dcs_write_cmd_seq(ctx, MCS_GOA_FS_SEL0, 0xBF, 0x02, 0x06, 0x14, 0x10,
  181. 0x16, 0x12, 0x08, 0x3F);
  182. dcs_write_cmd_seq(ctx, MCS_GOA_FS_SEL1, 0x3F, 0x3F, 0x3F, 0x3F, 0x0C,
  183. 0x0A, 0x0E, 0x3F, 0x3F, 0x00);
  184. dcs_write_cmd_seq(ctx, MCS_GOA_FS_SEL2, 0x04, 0x3F, 0x3F, 0x3F, 0x3F,
  185. 0x05, 0x01, 0x3F, 0x3F, 0x0F);
  186. dcs_write_cmd_seq(ctx, MCS_GOA_FS_SEL3, 0x0B, 0x0D, 0x3F, 0x3F, 0x3F,
  187. 0x3F);
  188. dcs_write_cmd_seq(ctx, 0xA2, 0x3F, 0x09, 0x13, 0x17, 0x11, 0x15);
  189. dcs_write_cmd_seq(ctx, 0xA9, 0x07, 0x03, 0x3F);
  190. dcs_write_cmd_seq(ctx, MCS_GOA_BS_SEL0, 0x3F, 0x05, 0x01, 0x17, 0x13,
  191. 0x15, 0x11, 0x0F, 0x3F);
  192. dcs_write_cmd_seq(ctx, MCS_GOA_BS_SEL1, 0x3F, 0x3F, 0x3F, 0x3F, 0x0B,
  193. 0x0D, 0x09, 0x3F, 0x3F, 0x07);
  194. dcs_write_cmd_seq(ctx, MCS_GOA_BS_SEL2, 0x03, 0x3F, 0x3F, 0x3F, 0x3F,
  195. 0x02, 0x06, 0x3F, 0x3F, 0x08);
  196. dcs_write_cmd_seq(ctx, MCS_GOA_BS_SEL3, 0x0C, 0x0A, 0x3F, 0x3F, 0x3F,
  197. 0x3F, 0x3F, 0x0E, 0x10, 0x14);
  198. dcs_write_cmd_seq(ctx, MCS_GOA_BS_SEL4, 0x12, 0x16, 0x00, 0x04, 0x3F);
  199. dcs_write_seq(ctx, 0xDC, 0x02);
  200. dcs_write_seq(ctx, 0xDE, 0x12);
  201. dcs_write_seq(ctx, MCS_CMD_MODE_SW, 0x0E); /* No documentation */
  202. dcs_write_seq(ctx, 0x01, 0x75);
  203. dcs_write_seq(ctx, MCS_CMD_MODE_SW, MCS_CMD2_P3);
  204. dcs_write_cmd_seq(ctx, MCS_GAMMA_VP, 0x00, 0x0C, 0x12, 0x0E, 0x06,
  205. 0x12, 0x0E, 0x0B, 0x15, 0x0B, 0x10, 0x07, 0x0F,
  206. 0x12, 0x0C, 0x00);
  207. dcs_write_cmd_seq(ctx, MCS_GAMMA_VN, 0x00, 0x0C, 0x12, 0x0E, 0x06,
  208. 0x12, 0x0E, 0x0B, 0x15, 0x0B, 0x10, 0x07, 0x0F,
  209. 0x12, 0x0C, 0x00);
  210. /* Exit CMD2 */
  211. dcs_write_seq(ctx, MCS_CMD_MODE_SW, MCS_CMD1_UCS);
  212. }
  213. static int rm68200_disable(struct drm_panel *panel)
  214. {
  215. struct rm68200 *ctx = panel_to_rm68200(panel);
  216. if (!ctx->enabled)
  217. return 0;
  218. backlight_disable(ctx->backlight);
  219. ctx->enabled = false;
  220. return 0;
  221. }
  222. static int rm68200_unprepare(struct drm_panel *panel)
  223. {
  224. struct rm68200 *ctx = panel_to_rm68200(panel);
  225. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  226. int ret;
  227. if (!ctx->prepared)
  228. return 0;
  229. ret = mipi_dsi_dcs_set_display_off(dsi);
  230. if (ret)
  231. DRM_WARN("failed to set display off: %d\n", ret);
  232. ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
  233. if (ret)
  234. DRM_WARN("failed to enter sleep mode: %d\n", ret);
  235. msleep(120);
  236. if (ctx->reset_gpio) {
  237. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  238. msleep(20);
  239. }
  240. regulator_disable(ctx->supply);
  241. ctx->prepared = false;
  242. return 0;
  243. }
  244. static int rm68200_prepare(struct drm_panel *panel)
  245. {
  246. struct rm68200 *ctx = panel_to_rm68200(panel);
  247. struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
  248. int ret;
  249. if (ctx->prepared)
  250. return 0;
  251. ret = regulator_enable(ctx->supply);
  252. if (ret < 0) {
  253. DRM_ERROR("failed to enable supply: %d\n", ret);
  254. return ret;
  255. }
  256. if (ctx->reset_gpio) {
  257. gpiod_set_value_cansleep(ctx->reset_gpio, 1);
  258. msleep(20);
  259. gpiod_set_value_cansleep(ctx->reset_gpio, 0);
  260. msleep(100);
  261. }
  262. rm68200_init_sequence(ctx);
  263. ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
  264. if (ret)
  265. return ret;
  266. msleep(125);
  267. ret = mipi_dsi_dcs_set_display_on(dsi);
  268. if (ret)
  269. return ret;
  270. msleep(20);
  271. ctx->prepared = true;
  272. return 0;
  273. }
  274. static int rm68200_enable(struct drm_panel *panel)
  275. {
  276. struct rm68200 *ctx = panel_to_rm68200(panel);
  277. if (ctx->enabled)
  278. return 0;
  279. backlight_enable(ctx->backlight);
  280. ctx->enabled = true;
  281. return 0;
  282. }
  283. static int rm68200_get_modes(struct drm_panel *panel)
  284. {
  285. struct drm_display_mode *mode;
  286. mode = drm_mode_duplicate(panel->drm, &default_mode);
  287. if (!mode) {
  288. DRM_ERROR("failed to add mode %ux%ux@%u\n",
  289. default_mode.hdisplay, default_mode.vdisplay,
  290. default_mode.vrefresh);
  291. return -ENOMEM;
  292. }
  293. drm_mode_set_name(mode);
  294. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  295. drm_mode_probed_add(panel->connector, mode);
  296. panel->connector->display_info.width_mm = mode->width_mm;
  297. panel->connector->display_info.height_mm = mode->height_mm;
  298. return 1;
  299. }
  300. static const struct drm_panel_funcs rm68200_drm_funcs = {
  301. .disable = rm68200_disable,
  302. .unprepare = rm68200_unprepare,
  303. .prepare = rm68200_prepare,
  304. .enable = rm68200_enable,
  305. .get_modes = rm68200_get_modes,
  306. };
  307. static int rm68200_probe(struct mipi_dsi_device *dsi)
  308. {
  309. struct device *dev = &dsi->dev;
  310. struct rm68200 *ctx;
  311. int ret;
  312. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  313. if (!ctx)
  314. return -ENOMEM;
  315. ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  316. if (IS_ERR(ctx->reset_gpio)) {
  317. ret = PTR_ERR(ctx->reset_gpio);
  318. dev_err(dev, "cannot get reset GPIO: %d\n", ret);
  319. return ret;
  320. }
  321. ctx->supply = devm_regulator_get(dev, "power");
  322. if (IS_ERR(ctx->supply)) {
  323. ret = PTR_ERR(ctx->supply);
  324. dev_err(dev, "cannot get regulator: %d\n", ret);
  325. return ret;
  326. }
  327. ctx->backlight = devm_of_find_backlight(dev);
  328. if (IS_ERR(ctx->backlight))
  329. return PTR_ERR(ctx->backlight);
  330. mipi_dsi_set_drvdata(dsi, ctx);
  331. ctx->dev = dev;
  332. dsi->lanes = 2;
  333. dsi->format = MIPI_DSI_FMT_RGB888;
  334. dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
  335. MIPI_DSI_MODE_LPM;
  336. drm_panel_init(&ctx->panel);
  337. ctx->panel.dev = dev;
  338. ctx->panel.funcs = &rm68200_drm_funcs;
  339. drm_panel_add(&ctx->panel);
  340. ret = mipi_dsi_attach(dsi);
  341. if (ret < 0) {
  342. dev_err(dev, "mipi_dsi_attach() failed: %d\n", ret);
  343. drm_panel_remove(&ctx->panel);
  344. return ret;
  345. }
  346. return 0;
  347. }
  348. static int rm68200_remove(struct mipi_dsi_device *dsi)
  349. {
  350. struct rm68200 *ctx = mipi_dsi_get_drvdata(dsi);
  351. mipi_dsi_detach(dsi);
  352. drm_panel_remove(&ctx->panel);
  353. return 0;
  354. }
  355. static const struct of_device_id raydium_rm68200_of_match[] = {
  356. { .compatible = "raydium,rm68200" },
  357. { }
  358. };
  359. MODULE_DEVICE_TABLE(of, raydium_rm68200_of_match);
  360. static struct mipi_dsi_driver raydium_rm68200_driver = {
  361. .probe = rm68200_probe,
  362. .remove = rm68200_remove,
  363. .driver = {
  364. .name = "panel-raydium-rm68200",
  365. .of_match_table = raydium_rm68200_of_match,
  366. },
  367. };
  368. module_mipi_dsi_driver(raydium_rm68200_driver);
  369. MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
  370. MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
  371. MODULE_DESCRIPTION("DRM Driver for Raydium RM68200 MIPI DSI panel");
  372. MODULE_LICENSE("GPL v2");