xref: /openbmc/linux/drivers/gpu/drm/panel/panel-raydium-rm68200.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12b7ed18bSPhilippe CORNU // SPDX-License-Identifier: GPL-2.0
22b7ed18bSPhilippe CORNU /*
32b7ed18bSPhilippe CORNU  * Copyright (C) STMicroelectronics SA 2017
42b7ed18bSPhilippe CORNU  *
52b7ed18bSPhilippe CORNU  * Authors: Philippe Cornu <philippe.cornu@st.com>
62b7ed18bSPhilippe CORNU  *          Yannick Fertre <yannick.fertre@st.com>
72b7ed18bSPhilippe CORNU  */
82b7ed18bSPhilippe CORNU 
9cb23eae3SSam Ravnborg #include <linux/delay.h>
102b7ed18bSPhilippe CORNU #include <linux/gpio/consumer.h>
11924735c4SSam Ravnborg #include <linux/mod_devicetable.h>
12cb23eae3SSam Ravnborg #include <linux/module.h>
132b7ed18bSPhilippe CORNU #include <linux/regulator/consumer.h>
142b7ed18bSPhilippe CORNU 
152b7ed18bSPhilippe CORNU #include <video/mipi_display.h>
162b7ed18bSPhilippe CORNU 
172b7ed18bSPhilippe CORNU #include <drm/drm_mipi_dsi.h>
18cb23eae3SSam Ravnborg #include <drm/drm_modes.h>
192b7ed18bSPhilippe CORNU #include <drm/drm_panel.h>
202b7ed18bSPhilippe CORNU 
212b7ed18bSPhilippe CORNU /*** Manufacturer Command Set ***/
222b7ed18bSPhilippe CORNU #define MCS_CMD_MODE_SW		0xFE /* CMD Mode Switch */
232b7ed18bSPhilippe CORNU #define MCS_CMD1_UCS		0x00 /* User Command Set (UCS = CMD1) */
242b7ed18bSPhilippe CORNU #define MCS_CMD2_P0		0x01 /* Manufacture Command Set Page0 (CMD2 P0) */
252b7ed18bSPhilippe CORNU #define MCS_CMD2_P1		0x02 /* Manufacture Command Set Page1 (CMD2 P1) */
262b7ed18bSPhilippe CORNU #define MCS_CMD2_P2		0x03 /* Manufacture Command Set Page2 (CMD2 P2) */
272b7ed18bSPhilippe CORNU #define MCS_CMD2_P3		0x04 /* Manufacture Command Set Page3 (CMD2 P3) */
282b7ed18bSPhilippe CORNU 
292b7ed18bSPhilippe CORNU /* CMD2 P0 commands (Display Options and Power) */
302b7ed18bSPhilippe CORNU #define MCS_STBCTR		0x12 /* TE1 Output Setting Zig-Zag Connection */
312b7ed18bSPhilippe CORNU #define MCS_SGOPCTR		0x16 /* Source Bias Current */
322b7ed18bSPhilippe CORNU #define MCS_SDCTR		0x1A /* Source Output Delay Time */
332b7ed18bSPhilippe CORNU #define MCS_INVCTR		0x1B /* Inversion Type */
342b7ed18bSPhilippe CORNU #define MCS_EXT_PWR_IC		0x24 /* External PWR IC Control */
352b7ed18bSPhilippe CORNU #define MCS_SETAVDD		0x27 /* PFM Control for AVDD Output */
362b7ed18bSPhilippe CORNU #define MCS_SETAVEE		0x29 /* PFM Control for AVEE Output */
372b7ed18bSPhilippe CORNU #define MCS_BT2CTR		0x2B /* DDVDL Charge Pump Control */
382b7ed18bSPhilippe CORNU #define MCS_BT3CTR		0x2F /* VGH Charge Pump Control */
392b7ed18bSPhilippe CORNU #define MCS_BT4CTR		0x34 /* VGL Charge Pump Control */
402b7ed18bSPhilippe CORNU #define MCS_VCMCTR		0x46 /* VCOM Output Level Control */
412b7ed18bSPhilippe CORNU #define MCS_SETVGN		0x52 /* VG M/S N Control */
422b7ed18bSPhilippe CORNU #define MCS_SETVGP		0x54 /* VG M/S P Control */
432b7ed18bSPhilippe CORNU #define MCS_SW_CTRL		0x5F /* Interface Control for PFM and MIPI */
442b7ed18bSPhilippe CORNU 
452b7ed18bSPhilippe CORNU /* CMD2 P2 commands (GOA Timing Control) - no description in datasheet */
462b7ed18bSPhilippe CORNU #define GOA_VSTV1		0x00
472b7ed18bSPhilippe CORNU #define GOA_VSTV2		0x07
482b7ed18bSPhilippe CORNU #define GOA_VCLK1		0x0E
492b7ed18bSPhilippe CORNU #define GOA_VCLK2		0x17
502b7ed18bSPhilippe CORNU #define GOA_VCLK_OPT1		0x20
512b7ed18bSPhilippe CORNU #define GOA_BICLK1		0x2A
522b7ed18bSPhilippe CORNU #define GOA_BICLK2		0x37
532b7ed18bSPhilippe CORNU #define GOA_BICLK3		0x44
542b7ed18bSPhilippe CORNU #define GOA_BICLK4		0x4F
552b7ed18bSPhilippe CORNU #define GOA_BICLK_OPT1		0x5B
562b7ed18bSPhilippe CORNU #define GOA_BICLK_OPT2		0x60
572b7ed18bSPhilippe CORNU #define MCS_GOA_GPO1		0x6D
582b7ed18bSPhilippe CORNU #define MCS_GOA_GPO2		0x71
592b7ed18bSPhilippe CORNU #define MCS_GOA_EQ		0x74
602b7ed18bSPhilippe CORNU #define MCS_GOA_CLK_GALLON	0x7C
612b7ed18bSPhilippe CORNU #define MCS_GOA_FS_SEL0		0x7E
622b7ed18bSPhilippe CORNU #define MCS_GOA_FS_SEL1		0x87
632b7ed18bSPhilippe CORNU #define MCS_GOA_FS_SEL2		0x91
642b7ed18bSPhilippe CORNU #define MCS_GOA_FS_SEL3		0x9B
652b7ed18bSPhilippe CORNU #define MCS_GOA_BS_SEL0		0xAC
662b7ed18bSPhilippe CORNU #define MCS_GOA_BS_SEL1		0xB5
672b7ed18bSPhilippe CORNU #define MCS_GOA_BS_SEL2		0xBF
682b7ed18bSPhilippe CORNU #define MCS_GOA_BS_SEL3		0xC9
692b7ed18bSPhilippe CORNU #define MCS_GOA_BS_SEL4		0xD3
702b7ed18bSPhilippe CORNU 
712b7ed18bSPhilippe CORNU /* CMD2 P3 commands (Gamma) */
722b7ed18bSPhilippe CORNU #define MCS_GAMMA_VP		0x60 /* Gamma VP1~VP16 */
732b7ed18bSPhilippe CORNU #define MCS_GAMMA_VN		0x70 /* Gamma VN1~VN16 */
742b7ed18bSPhilippe CORNU 
752b7ed18bSPhilippe CORNU struct rm68200 {
762b7ed18bSPhilippe CORNU 	struct device *dev;
772b7ed18bSPhilippe CORNU 	struct drm_panel panel;
782b7ed18bSPhilippe CORNU 	struct gpio_desc *reset_gpio;
792b7ed18bSPhilippe CORNU 	struct regulator *supply;
802b7ed18bSPhilippe CORNU 	bool prepared;
812b7ed18bSPhilippe CORNU 	bool enabled;
822b7ed18bSPhilippe CORNU };
832b7ed18bSPhilippe CORNU 
842b7ed18bSPhilippe CORNU static const struct drm_display_mode default_mode = {
8537e6011bSYannick Fertre 	.clock = 54000,
862b7ed18bSPhilippe CORNU 	.hdisplay = 720,
8737e6011bSYannick Fertre 	.hsync_start = 720 + 48,
8837e6011bSYannick Fertre 	.hsync_end = 720 + 48 + 9,
8937e6011bSYannick Fertre 	.htotal = 720 + 48 + 9 + 48,
902b7ed18bSPhilippe CORNU 	.vdisplay = 1280,
912b7ed18bSPhilippe CORNU 	.vsync_start = 1280 + 12,
9237e6011bSYannick Fertre 	.vsync_end = 1280 + 12 + 5,
9337e6011bSYannick Fertre 	.vtotal = 1280 + 12 + 5 + 12,
942b7ed18bSPhilippe CORNU 	.flags = 0,
952b7ed18bSPhilippe CORNU 	.width_mm = 68,
962b7ed18bSPhilippe CORNU 	.height_mm = 122,
972b7ed18bSPhilippe CORNU };
982b7ed18bSPhilippe CORNU 
panel_to_rm68200(struct drm_panel * panel)992b7ed18bSPhilippe CORNU static inline struct rm68200 *panel_to_rm68200(struct drm_panel *panel)
1002b7ed18bSPhilippe CORNU {
1012b7ed18bSPhilippe CORNU 	return container_of(panel, struct rm68200, panel);
1022b7ed18bSPhilippe CORNU }
1032b7ed18bSPhilippe CORNU 
rm68200_dcs_write_buf(struct rm68200 * ctx,const void * data,size_t len)1042b7ed18bSPhilippe CORNU static void rm68200_dcs_write_buf(struct rm68200 *ctx, const void *data,
1052b7ed18bSPhilippe CORNU 				  size_t len)
1062b7ed18bSPhilippe CORNU {
1072b7ed18bSPhilippe CORNU 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
1082b7ed18bSPhilippe CORNU 	int err;
1092b7ed18bSPhilippe CORNU 
1102b7ed18bSPhilippe CORNU 	err = mipi_dsi_dcs_write_buffer(dsi, data, len);
1112b7ed18bSPhilippe CORNU 	if (err < 0)
112c8cf6990SSam Ravnborg 		dev_err_ratelimited(ctx->dev, "MIPI DSI DCS write buffer failed: %d\n", err);
1132b7ed18bSPhilippe CORNU }
1142b7ed18bSPhilippe CORNU 
rm68200_dcs_write_cmd(struct rm68200 * ctx,u8 cmd,u8 value)1152b7ed18bSPhilippe CORNU static void rm68200_dcs_write_cmd(struct rm68200 *ctx, u8 cmd, u8 value)
1162b7ed18bSPhilippe CORNU {
1172b7ed18bSPhilippe CORNU 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
1182b7ed18bSPhilippe CORNU 	int err;
1192b7ed18bSPhilippe CORNU 
1202b7ed18bSPhilippe CORNU 	err = mipi_dsi_dcs_write(dsi, cmd, &value, 1);
1212b7ed18bSPhilippe CORNU 	if (err < 0)
122c8cf6990SSam Ravnborg 		dev_err_ratelimited(ctx->dev, "MIPI DSI DCS write failed: %d\n", err);
1232b7ed18bSPhilippe CORNU }
1242b7ed18bSPhilippe CORNU 
1252b7ed18bSPhilippe CORNU #define dcs_write_seq(ctx, seq...)				\
1262b7ed18bSPhilippe CORNU ({								\
1272b7ed18bSPhilippe CORNU 	static const u8 d[] = { seq };				\
1282b7ed18bSPhilippe CORNU 								\
1292b7ed18bSPhilippe CORNU 	rm68200_dcs_write_buf(ctx, d, ARRAY_SIZE(d));		\
1302b7ed18bSPhilippe CORNU })
1312b7ed18bSPhilippe CORNU 
1322b7ed18bSPhilippe CORNU /*
1332b7ed18bSPhilippe CORNU  * This panel is not able to auto-increment all cmd addresses so for some of
1342b7ed18bSPhilippe CORNU  * them, we need to send them one by one...
1352b7ed18bSPhilippe CORNU  */
1362b7ed18bSPhilippe CORNU #define dcs_write_cmd_seq(ctx, cmd, seq...)			\
1372b7ed18bSPhilippe CORNU ({								\
1382b7ed18bSPhilippe CORNU 	static const u8 d[] = { seq };				\
1392b7ed18bSPhilippe CORNU 	unsigned int i;						\
1402b7ed18bSPhilippe CORNU 								\
1412b7ed18bSPhilippe CORNU 	for (i = 0; i < ARRAY_SIZE(d) ; i++)			\
1422b7ed18bSPhilippe CORNU 		rm68200_dcs_write_cmd(ctx, cmd + i, d[i]);	\
1432b7ed18bSPhilippe CORNU })
1442b7ed18bSPhilippe CORNU 
rm68200_init_sequence(struct rm68200 * ctx)1452b7ed18bSPhilippe CORNU static void rm68200_init_sequence(struct rm68200 *ctx)
1462b7ed18bSPhilippe CORNU {
1472b7ed18bSPhilippe CORNU 	/* Enter CMD2 with page 0 */
1482b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_CMD_MODE_SW, MCS_CMD2_P0);
1492b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_EXT_PWR_IC, 0xC0, 0x53, 0x00);
1502b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_BT2CTR, 0xE5);
1512b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_SETAVDD, 0x0A);
1522b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_SETAVEE, 0x0A);
1532b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_SGOPCTR, 0x52);
1542b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_BT3CTR, 0x53);
1552b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_BT4CTR, 0x5A);
1562b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_INVCTR, 0x00);
1572b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_STBCTR, 0x0A);
1582b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_SDCTR, 0x06);
1592b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_VCMCTR, 0x56);
1602b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_SETVGN, 0xA0, 0x00);
1612b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_SETVGP, 0xA0, 0x00);
1622b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_SW_CTRL, 0x11); /* 2 data lanes, see doc */
1632b7ed18bSPhilippe CORNU 
1642b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_CMD_MODE_SW, MCS_CMD2_P2);
1652b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, GOA_VSTV1, 0x05);
1662b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, 0x02, 0x0B);
1672b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, 0x03, 0x0F);
1682b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, 0x04, 0x7D, 0x00, 0x50);
1692b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, GOA_VSTV2, 0x05, 0x16, 0x0D, 0x11, 0x7D, 0x00,
1702b7ed18bSPhilippe CORNU 			  0x50);
1712b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, GOA_VCLK1, 0x07, 0x08, 0x01, 0x02, 0x00, 0x7D,
1722b7ed18bSPhilippe CORNU 			  0x00, 0x85, 0x08);
1732b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, GOA_VCLK2, 0x03, 0x04, 0x05, 0x06, 0x00, 0x7D,
1742b7ed18bSPhilippe CORNU 			  0x00, 0x85, 0x08);
1752b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, GOA_VCLK_OPT1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1762b7ed18bSPhilippe CORNU 		      0x00, 0x00, 0x00, 0x00);
1772b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, GOA_BICLK1, 0x07, 0x08);
1782b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, 0x2D, 0x01);
1792b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, 0x2F, 0x02, 0x00, 0x40, 0x05, 0x08, 0x54, 0x7D,
1802b7ed18bSPhilippe CORNU 		      0x00);
1812b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, GOA_BICLK2, 0x03, 0x04, 0x05, 0x06, 0x00);
1822b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, 0x3D, 0x40);
1832b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, 0x3F, 0x05, 0x08, 0x54, 0x7D, 0x00);
1842b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, GOA_BICLK3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1852b7ed18bSPhilippe CORNU 		      0x00, 0x00, 0x00, 0x00, 0x00);
1862b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, GOA_BICLK4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1872b7ed18bSPhilippe CORNU 		      0x00, 0x00);
1882b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, 0x58, 0x00, 0x00, 0x00);
1892b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, GOA_BICLK_OPT1, 0x00, 0x00, 0x00, 0x00, 0x00);
1902b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, GOA_BICLK_OPT2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1912b7ed18bSPhilippe CORNU 		      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
1922b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_GOA_GPO1, 0x00, 0x00, 0x00, 0x00);
1932b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_GOA_GPO2, 0x00, 0x20, 0x00);
1942b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_GOA_EQ, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
1952b7ed18bSPhilippe CORNU 		      0x00, 0x00);
1962b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_GOA_CLK_GALLON, 0x00, 0x00);
1972b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_GOA_FS_SEL0, 0xBF, 0x02, 0x06, 0x14, 0x10,
1982b7ed18bSPhilippe CORNU 			  0x16, 0x12, 0x08, 0x3F);
1992b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_GOA_FS_SEL1, 0x3F, 0x3F, 0x3F, 0x3F, 0x0C,
2002b7ed18bSPhilippe CORNU 			  0x0A, 0x0E, 0x3F, 0x3F, 0x00);
2012b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_GOA_FS_SEL2, 0x04, 0x3F, 0x3F, 0x3F, 0x3F,
2022b7ed18bSPhilippe CORNU 			  0x05, 0x01, 0x3F, 0x3F, 0x0F);
2032b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_GOA_FS_SEL3, 0x0B, 0x0D, 0x3F, 0x3F, 0x3F,
2042b7ed18bSPhilippe CORNU 			  0x3F);
2052b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, 0xA2, 0x3F, 0x09, 0x13, 0x17, 0x11, 0x15);
2062b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, 0xA9, 0x07, 0x03, 0x3F);
2072b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_GOA_BS_SEL0, 0x3F, 0x05, 0x01, 0x17, 0x13,
2082b7ed18bSPhilippe CORNU 			  0x15, 0x11, 0x0F, 0x3F);
2092b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_GOA_BS_SEL1, 0x3F, 0x3F, 0x3F, 0x3F, 0x0B,
2102b7ed18bSPhilippe CORNU 			  0x0D, 0x09, 0x3F, 0x3F, 0x07);
2112b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_GOA_BS_SEL2, 0x03, 0x3F, 0x3F, 0x3F, 0x3F,
2122b7ed18bSPhilippe CORNU 			  0x02, 0x06, 0x3F, 0x3F, 0x08);
2132b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_GOA_BS_SEL3, 0x0C, 0x0A, 0x3F, 0x3F, 0x3F,
2142b7ed18bSPhilippe CORNU 			  0x3F, 0x3F, 0x0E, 0x10, 0x14);
2152b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_GOA_BS_SEL4, 0x12, 0x16, 0x00, 0x04, 0x3F);
2162b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, 0xDC, 0x02);
2172b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, 0xDE, 0x12);
2182b7ed18bSPhilippe CORNU 
2192b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_CMD_MODE_SW, 0x0E); /* No documentation */
2202b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, 0x01, 0x75);
2212b7ed18bSPhilippe CORNU 
2222b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_CMD_MODE_SW, MCS_CMD2_P3);
2232b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_GAMMA_VP, 0x00, 0x0C, 0x12, 0x0E, 0x06,
2242b7ed18bSPhilippe CORNU 			  0x12, 0x0E, 0x0B, 0x15, 0x0B, 0x10, 0x07, 0x0F,
2252b7ed18bSPhilippe CORNU 			  0x12, 0x0C, 0x00);
2262b7ed18bSPhilippe CORNU 	dcs_write_cmd_seq(ctx, MCS_GAMMA_VN, 0x00, 0x0C, 0x12, 0x0E, 0x06,
2272b7ed18bSPhilippe CORNU 			  0x12, 0x0E, 0x0B, 0x15, 0x0B, 0x10, 0x07, 0x0F,
2282b7ed18bSPhilippe CORNU 			  0x12, 0x0C, 0x00);
2292b7ed18bSPhilippe CORNU 
2302b7ed18bSPhilippe CORNU 	/* Exit CMD2 */
2312b7ed18bSPhilippe CORNU 	dcs_write_seq(ctx, MCS_CMD_MODE_SW, MCS_CMD1_UCS);
2322b7ed18bSPhilippe CORNU }
2332b7ed18bSPhilippe CORNU 
rm68200_disable(struct drm_panel * panel)2342b7ed18bSPhilippe CORNU static int rm68200_disable(struct drm_panel *panel)
2352b7ed18bSPhilippe CORNU {
2362b7ed18bSPhilippe CORNU 	struct rm68200 *ctx = panel_to_rm68200(panel);
2372b7ed18bSPhilippe CORNU 
2382b7ed18bSPhilippe CORNU 	if (!ctx->enabled)
2392b7ed18bSPhilippe CORNU 		return 0;
2402b7ed18bSPhilippe CORNU 
2412b7ed18bSPhilippe CORNU 	ctx->enabled = false;
2422b7ed18bSPhilippe CORNU 
2432b7ed18bSPhilippe CORNU 	return 0;
2442b7ed18bSPhilippe CORNU }
2452b7ed18bSPhilippe CORNU 
rm68200_unprepare(struct drm_panel * panel)2462b7ed18bSPhilippe CORNU static int rm68200_unprepare(struct drm_panel *panel)
2472b7ed18bSPhilippe CORNU {
2482b7ed18bSPhilippe CORNU 	struct rm68200 *ctx = panel_to_rm68200(panel);
2492b7ed18bSPhilippe CORNU 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
2502b7ed18bSPhilippe CORNU 	int ret;
2512b7ed18bSPhilippe CORNU 
2522b7ed18bSPhilippe CORNU 	if (!ctx->prepared)
2532b7ed18bSPhilippe CORNU 		return 0;
2542b7ed18bSPhilippe CORNU 
2552b7ed18bSPhilippe CORNU 	ret = mipi_dsi_dcs_set_display_off(dsi);
2562b7ed18bSPhilippe CORNU 	if (ret)
257c8cf6990SSam Ravnborg 		dev_warn(panel->dev, "failed to set display off: %d\n", ret);
2582b7ed18bSPhilippe CORNU 
2592b7ed18bSPhilippe CORNU 	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
2602b7ed18bSPhilippe CORNU 	if (ret)
261c8cf6990SSam Ravnborg 		dev_warn(panel->dev, "failed to enter sleep mode: %d\n", ret);
2622b7ed18bSPhilippe CORNU 
2632b7ed18bSPhilippe CORNU 	msleep(120);
2642b7ed18bSPhilippe CORNU 
2652b7ed18bSPhilippe CORNU 	if (ctx->reset_gpio) {
2662b7ed18bSPhilippe CORNU 		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
2672b7ed18bSPhilippe CORNU 		msleep(20);
2682b7ed18bSPhilippe CORNU 	}
2692b7ed18bSPhilippe CORNU 
2702b7ed18bSPhilippe CORNU 	regulator_disable(ctx->supply);
2712b7ed18bSPhilippe CORNU 
2722b7ed18bSPhilippe CORNU 	ctx->prepared = false;
2732b7ed18bSPhilippe CORNU 
2742b7ed18bSPhilippe CORNU 	return 0;
2752b7ed18bSPhilippe CORNU }
2762b7ed18bSPhilippe CORNU 
rm68200_prepare(struct drm_panel * panel)2772b7ed18bSPhilippe CORNU static int rm68200_prepare(struct drm_panel *panel)
2782b7ed18bSPhilippe CORNU {
2792b7ed18bSPhilippe CORNU 	struct rm68200 *ctx = panel_to_rm68200(panel);
2802b7ed18bSPhilippe CORNU 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
2812b7ed18bSPhilippe CORNU 	int ret;
2822b7ed18bSPhilippe CORNU 
2832b7ed18bSPhilippe CORNU 	if (ctx->prepared)
2842b7ed18bSPhilippe CORNU 		return 0;
2852b7ed18bSPhilippe CORNU 
2862b7ed18bSPhilippe CORNU 	ret = regulator_enable(ctx->supply);
2872b7ed18bSPhilippe CORNU 	if (ret < 0) {
288c8cf6990SSam Ravnborg 		dev_err(ctx->dev, "failed to enable supply: %d\n", ret);
2892b7ed18bSPhilippe CORNU 		return ret;
2902b7ed18bSPhilippe CORNU 	}
2912b7ed18bSPhilippe CORNU 
2922b7ed18bSPhilippe CORNU 	if (ctx->reset_gpio) {
2932b7ed18bSPhilippe CORNU 		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
2942b7ed18bSPhilippe CORNU 		msleep(20);
2952b7ed18bSPhilippe CORNU 		gpiod_set_value_cansleep(ctx->reset_gpio, 0);
2962b7ed18bSPhilippe CORNU 		msleep(100);
2972b7ed18bSPhilippe CORNU 	}
2982b7ed18bSPhilippe CORNU 
2992b7ed18bSPhilippe CORNU 	rm68200_init_sequence(ctx);
3002b7ed18bSPhilippe CORNU 
3012b7ed18bSPhilippe CORNU 	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
3022b7ed18bSPhilippe CORNU 	if (ret)
3032b7ed18bSPhilippe CORNU 		return ret;
3042b7ed18bSPhilippe CORNU 
3052b7ed18bSPhilippe CORNU 	msleep(125);
3062b7ed18bSPhilippe CORNU 
3072b7ed18bSPhilippe CORNU 	ret = mipi_dsi_dcs_set_display_on(dsi);
3082b7ed18bSPhilippe CORNU 	if (ret)
3092b7ed18bSPhilippe CORNU 		return ret;
3102b7ed18bSPhilippe CORNU 
3112b7ed18bSPhilippe CORNU 	msleep(20);
3122b7ed18bSPhilippe CORNU 
3132b7ed18bSPhilippe CORNU 	ctx->prepared = true;
3142b7ed18bSPhilippe CORNU 
3152b7ed18bSPhilippe CORNU 	return 0;
3162b7ed18bSPhilippe CORNU }
3172b7ed18bSPhilippe CORNU 
rm68200_enable(struct drm_panel * panel)3182b7ed18bSPhilippe CORNU static int rm68200_enable(struct drm_panel *panel)
3192b7ed18bSPhilippe CORNU {
3202b7ed18bSPhilippe CORNU 	struct rm68200 *ctx = panel_to_rm68200(panel);
3212b7ed18bSPhilippe CORNU 
3222b7ed18bSPhilippe CORNU 	if (ctx->enabled)
3232b7ed18bSPhilippe CORNU 		return 0;
3242b7ed18bSPhilippe CORNU 
3252b7ed18bSPhilippe CORNU 	ctx->enabled = true;
3262b7ed18bSPhilippe CORNU 
3272b7ed18bSPhilippe CORNU 	return 0;
3282b7ed18bSPhilippe CORNU }
3292b7ed18bSPhilippe CORNU 
rm68200_get_modes(struct drm_panel * panel,struct drm_connector * connector)3300ce8ddd8SSam Ravnborg static int rm68200_get_modes(struct drm_panel *panel,
3310ce8ddd8SSam Ravnborg 			     struct drm_connector *connector)
3322b7ed18bSPhilippe CORNU {
3332b7ed18bSPhilippe CORNU 	struct drm_display_mode *mode;
3342b7ed18bSPhilippe CORNU 
335aa6c4364SSam Ravnborg 	mode = drm_mode_duplicate(connector->dev, &default_mode);
3362b7ed18bSPhilippe CORNU 	if (!mode) {
337c8cf6990SSam Ravnborg 		dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
3382b7ed18bSPhilippe CORNU 			default_mode.hdisplay, default_mode.vdisplay,
3390425662fSVille Syrjälä 			drm_mode_vrefresh(&default_mode));
3402b7ed18bSPhilippe CORNU 		return -ENOMEM;
3412b7ed18bSPhilippe CORNU 	}
3422b7ed18bSPhilippe CORNU 
3432b7ed18bSPhilippe CORNU 	drm_mode_set_name(mode);
3442b7ed18bSPhilippe CORNU 
3452b7ed18bSPhilippe CORNU 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
3460ce8ddd8SSam Ravnborg 	drm_mode_probed_add(connector, mode);
3472b7ed18bSPhilippe CORNU 
3480ce8ddd8SSam Ravnborg 	connector->display_info.width_mm = mode->width_mm;
3490ce8ddd8SSam Ravnborg 	connector->display_info.height_mm = mode->height_mm;
3502b7ed18bSPhilippe CORNU 
3512b7ed18bSPhilippe CORNU 	return 1;
3522b7ed18bSPhilippe CORNU }
3532b7ed18bSPhilippe CORNU 
3542b7ed18bSPhilippe CORNU static const struct drm_panel_funcs rm68200_drm_funcs = {
3552b7ed18bSPhilippe CORNU 	.disable = rm68200_disable,
3562b7ed18bSPhilippe CORNU 	.unprepare = rm68200_unprepare,
3572b7ed18bSPhilippe CORNU 	.prepare = rm68200_prepare,
3582b7ed18bSPhilippe CORNU 	.enable = rm68200_enable,
3592b7ed18bSPhilippe CORNU 	.get_modes = rm68200_get_modes,
3602b7ed18bSPhilippe CORNU };
3612b7ed18bSPhilippe CORNU 
rm68200_probe(struct mipi_dsi_device * dsi)3622b7ed18bSPhilippe CORNU static int rm68200_probe(struct mipi_dsi_device *dsi)
3632b7ed18bSPhilippe CORNU {
3642b7ed18bSPhilippe CORNU 	struct device *dev = &dsi->dev;
3652b7ed18bSPhilippe CORNU 	struct rm68200 *ctx;
3662b7ed18bSPhilippe CORNU 	int ret;
3672b7ed18bSPhilippe CORNU 
3682b7ed18bSPhilippe CORNU 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
3692b7ed18bSPhilippe CORNU 	if (!ctx)
3702b7ed18bSPhilippe CORNU 		return -ENOMEM;
3712b7ed18bSPhilippe CORNU 
3722b7ed18bSPhilippe CORNU 	ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
3732b7ed18bSPhilippe CORNU 	if (IS_ERR(ctx->reset_gpio)) {
3742b7ed18bSPhilippe CORNU 		ret = PTR_ERR(ctx->reset_gpio);
3752b7ed18bSPhilippe CORNU 		dev_err(dev, "cannot get reset GPIO: %d\n", ret);
3762b7ed18bSPhilippe CORNU 		return ret;
3772b7ed18bSPhilippe CORNU 	}
3782b7ed18bSPhilippe CORNU 
3792b7ed18bSPhilippe CORNU 	ctx->supply = devm_regulator_get(dev, "power");
3802b7ed18bSPhilippe CORNU 	if (IS_ERR(ctx->supply)) {
3812b7ed18bSPhilippe CORNU 		ret = PTR_ERR(ctx->supply);
382135281dfSYannick Fertré 		if (ret != -EPROBE_DEFER)
3832b7ed18bSPhilippe CORNU 			dev_err(dev, "cannot get regulator: %d\n", ret);
3842b7ed18bSPhilippe CORNU 		return ret;
3852b7ed18bSPhilippe CORNU 	}
3862b7ed18bSPhilippe CORNU 
3872b7ed18bSPhilippe CORNU 	mipi_dsi_set_drvdata(dsi, ctx);
3882b7ed18bSPhilippe CORNU 
3892b7ed18bSPhilippe CORNU 	ctx->dev = dev;
3902b7ed18bSPhilippe CORNU 
3912b7ed18bSPhilippe CORNU 	dsi->lanes = 2;
3922b7ed18bSPhilippe CORNU 	dsi->format = MIPI_DSI_FMT_RGB888;
3932b7ed18bSPhilippe CORNU 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
394fb4f3c92SYannick Fertre 			  MIPI_DSI_MODE_LPM | MIPI_DSI_CLOCK_NON_CONTINUOUS;
3952b7ed18bSPhilippe CORNU 
3969a2654c0SLaurent Pinchart 	drm_panel_init(&ctx->panel, dev, &rm68200_drm_funcs,
3979a2654c0SLaurent Pinchart 		       DRM_MODE_CONNECTOR_DSI);
3982b7ed18bSPhilippe CORNU 
399924735c4SSam Ravnborg 	ret = drm_panel_of_backlight(&ctx->panel);
400924735c4SSam Ravnborg 	if (ret)
401924735c4SSam Ravnborg 		return ret;
402924735c4SSam Ravnborg 
4032b7ed18bSPhilippe CORNU 	drm_panel_add(&ctx->panel);
4042b7ed18bSPhilippe CORNU 
4052b7ed18bSPhilippe CORNU 	ret = mipi_dsi_attach(dsi);
4062b7ed18bSPhilippe CORNU 	if (ret < 0) {
4072b7ed18bSPhilippe CORNU 		dev_err(dev, "mipi_dsi_attach() failed: %d\n", ret);
4082b7ed18bSPhilippe CORNU 		drm_panel_remove(&ctx->panel);
4092b7ed18bSPhilippe CORNU 		return ret;
4102b7ed18bSPhilippe CORNU 	}
4112b7ed18bSPhilippe CORNU 
4122b7ed18bSPhilippe CORNU 	return 0;
4132b7ed18bSPhilippe CORNU }
4142b7ed18bSPhilippe CORNU 
rm68200_remove(struct mipi_dsi_device * dsi)415*79abca2bSUwe Kleine-König static void rm68200_remove(struct mipi_dsi_device *dsi)
4162b7ed18bSPhilippe CORNU {
4172b7ed18bSPhilippe CORNU 	struct rm68200 *ctx = mipi_dsi_get_drvdata(dsi);
4182b7ed18bSPhilippe CORNU 
4192b7ed18bSPhilippe CORNU 	mipi_dsi_detach(dsi);
4202b7ed18bSPhilippe CORNU 	drm_panel_remove(&ctx->panel);
4212b7ed18bSPhilippe CORNU }
4222b7ed18bSPhilippe CORNU 
4232b7ed18bSPhilippe CORNU static const struct of_device_id raydium_rm68200_of_match[] = {
4242b7ed18bSPhilippe CORNU 	{ .compatible = "raydium,rm68200" },
4252b7ed18bSPhilippe CORNU 	{ }
4262b7ed18bSPhilippe CORNU };
4272b7ed18bSPhilippe CORNU MODULE_DEVICE_TABLE(of, raydium_rm68200_of_match);
4282b7ed18bSPhilippe CORNU 
4292b7ed18bSPhilippe CORNU static struct mipi_dsi_driver raydium_rm68200_driver = {
4302b7ed18bSPhilippe CORNU 	.probe = rm68200_probe,
4312b7ed18bSPhilippe CORNU 	.remove = rm68200_remove,
4322b7ed18bSPhilippe CORNU 	.driver = {
4332b7ed18bSPhilippe CORNU 		.name = "panel-raydium-rm68200",
4342b7ed18bSPhilippe CORNU 		.of_match_table = raydium_rm68200_of_match,
4352b7ed18bSPhilippe CORNU 	},
4362b7ed18bSPhilippe CORNU };
4372b7ed18bSPhilippe CORNU module_mipi_dsi_driver(raydium_rm68200_driver);
4382b7ed18bSPhilippe CORNU 
4392b7ed18bSPhilippe CORNU MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
4402b7ed18bSPhilippe CORNU MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
4412b7ed18bSPhilippe CORNU MODULE_DESCRIPTION("DRM Driver for Raydium RM68200 MIPI DSI panel");
4422b7ed18bSPhilippe CORNU MODULE_LICENSE("GPL v2");
443