18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) STMicroelectronics SA 2017
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Authors: Philippe Cornu <philippe.cornu@st.com>
68c2ecf20Sopenharmony_ci *          Yannick Fertre <yannick.fertre@st.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/backlight.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <video/mipi_display.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <drm/drm_mipi_dsi.h>
188c2ecf20Sopenharmony_ci#include <drm/drm_modes.h>
198c2ecf20Sopenharmony_ci#include <drm/drm_panel.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define OTM8009A_BACKLIGHT_DEFAULT	240
228c2ecf20Sopenharmony_ci#define OTM8009A_BACKLIGHT_MAX		255
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* Manufacturer Command Set */
258c2ecf20Sopenharmony_ci#define MCS_ADRSFT	0x0000	/* Address Shift Function */
268c2ecf20Sopenharmony_ci#define MCS_PANSET	0xB3A6	/* Panel Type Setting */
278c2ecf20Sopenharmony_ci#define MCS_SD_CTRL	0xC0A2	/* Source Driver Timing Setting */
288c2ecf20Sopenharmony_ci#define MCS_P_DRV_M	0xC0B4	/* Panel Driving Mode */
298c2ecf20Sopenharmony_ci#define MCS_OSC_ADJ	0xC181	/* Oscillator Adjustment for Idle/Normal mode */
308c2ecf20Sopenharmony_ci#define MCS_RGB_VID_SET	0xC1A1	/* RGB Video Mode Setting */
318c2ecf20Sopenharmony_ci#define MCS_SD_PCH_CTRL	0xC480	/* Source Driver Precharge Control */
328c2ecf20Sopenharmony_ci#define MCS_NO_DOC1	0xC48A	/* Command not documented */
338c2ecf20Sopenharmony_ci#define MCS_PWR_CTRL1	0xC580	/* Power Control Setting 1 */
348c2ecf20Sopenharmony_ci#define MCS_PWR_CTRL2	0xC590	/* Power Control Setting 2 for Normal Mode */
358c2ecf20Sopenharmony_ci#define MCS_PWR_CTRL4	0xC5B0	/* Power Control Setting 4 for DC Voltage */
368c2ecf20Sopenharmony_ci#define MCS_PANCTRLSET1	0xCB80	/* Panel Control Setting 1 */
378c2ecf20Sopenharmony_ci#define MCS_PANCTRLSET2	0xCB90	/* Panel Control Setting 2 */
388c2ecf20Sopenharmony_ci#define MCS_PANCTRLSET3	0xCBA0	/* Panel Control Setting 3 */
398c2ecf20Sopenharmony_ci#define MCS_PANCTRLSET4	0xCBB0	/* Panel Control Setting 4 */
408c2ecf20Sopenharmony_ci#define MCS_PANCTRLSET5	0xCBC0	/* Panel Control Setting 5 */
418c2ecf20Sopenharmony_ci#define MCS_PANCTRLSET6	0xCBD0	/* Panel Control Setting 6 */
428c2ecf20Sopenharmony_ci#define MCS_PANCTRLSET7	0xCBE0	/* Panel Control Setting 7 */
438c2ecf20Sopenharmony_ci#define MCS_PANCTRLSET8	0xCBF0	/* Panel Control Setting 8 */
448c2ecf20Sopenharmony_ci#define MCS_PANU2D1	0xCC80	/* Panel U2D Setting 1 */
458c2ecf20Sopenharmony_ci#define MCS_PANU2D2	0xCC90	/* Panel U2D Setting 2 */
468c2ecf20Sopenharmony_ci#define MCS_PANU2D3	0xCCA0	/* Panel U2D Setting 3 */
478c2ecf20Sopenharmony_ci#define MCS_PAND2U1	0xCCB0	/* Panel D2U Setting 1 */
488c2ecf20Sopenharmony_ci#define MCS_PAND2U2	0xCCC0	/* Panel D2U Setting 2 */
498c2ecf20Sopenharmony_ci#define MCS_PAND2U3	0xCCD0	/* Panel D2U Setting 3 */
508c2ecf20Sopenharmony_ci#define MCS_GOAVST	0xCE80	/* GOA VST Setting */
518c2ecf20Sopenharmony_ci#define MCS_GOACLKA1	0xCEA0	/* GOA CLKA1 Setting */
528c2ecf20Sopenharmony_ci#define MCS_GOACLKA3	0xCEB0	/* GOA CLKA3 Setting */
538c2ecf20Sopenharmony_ci#define MCS_GOAECLK	0xCFC0	/* GOA ECLK Setting */
548c2ecf20Sopenharmony_ci#define MCS_NO_DOC2	0xCFD0	/* Command not documented */
558c2ecf20Sopenharmony_ci#define MCS_GVDDSET	0xD800	/* GVDD/NGVDD */
568c2ecf20Sopenharmony_ci#define MCS_VCOMDC	0xD900	/* VCOM Voltage Setting */
578c2ecf20Sopenharmony_ci#define MCS_GMCT2_2P	0xE100	/* Gamma Correction 2.2+ Setting */
588c2ecf20Sopenharmony_ci#define MCS_GMCT2_2N	0xE200	/* Gamma Correction 2.2- Setting */
598c2ecf20Sopenharmony_ci#define MCS_NO_DOC3	0xF5B6	/* Command not documented */
608c2ecf20Sopenharmony_ci#define MCS_CMD2_ENA1	0xFF00	/* Enable Access Command2 "CMD2" */
618c2ecf20Sopenharmony_ci#define MCS_CMD2_ENA2	0xFF80	/* Enable Access Orise Command2 */
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistruct otm8009a {
648c2ecf20Sopenharmony_ci	struct device *dev;
658c2ecf20Sopenharmony_ci	struct drm_panel panel;
668c2ecf20Sopenharmony_ci	struct backlight_device *bl_dev;
678c2ecf20Sopenharmony_ci	struct gpio_desc *reset_gpio;
688c2ecf20Sopenharmony_ci	struct regulator *supply;
698c2ecf20Sopenharmony_ci	bool prepared;
708c2ecf20Sopenharmony_ci	bool enabled;
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic const struct drm_display_mode default_mode = {
748c2ecf20Sopenharmony_ci	.clock = 29700,
758c2ecf20Sopenharmony_ci	.hdisplay = 480,
768c2ecf20Sopenharmony_ci	.hsync_start = 480 + 98,
778c2ecf20Sopenharmony_ci	.hsync_end = 480 + 98 + 32,
788c2ecf20Sopenharmony_ci	.htotal = 480 + 98 + 32 + 98,
798c2ecf20Sopenharmony_ci	.vdisplay = 800,
808c2ecf20Sopenharmony_ci	.vsync_start = 800 + 15,
818c2ecf20Sopenharmony_ci	.vsync_end = 800 + 15 + 10,
828c2ecf20Sopenharmony_ci	.vtotal = 800 + 15 + 10 + 14,
838c2ecf20Sopenharmony_ci	.flags = 0,
848c2ecf20Sopenharmony_ci	.width_mm = 52,
858c2ecf20Sopenharmony_ci	.height_mm = 86,
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic inline struct otm8009a *panel_to_otm8009a(struct drm_panel *panel)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	return container_of(panel, struct otm8009a, panel);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic void otm8009a_dcs_write_buf(struct otm8009a *ctx, const void *data,
948c2ecf20Sopenharmony_ci				   size_t len)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (mipi_dsi_dcs_write_buffer(dsi, data, len) < 0)
998c2ecf20Sopenharmony_ci		dev_warn(ctx->dev, "mipi dsi dcs write buffer failed\n");
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic void otm8009a_dcs_write_buf_hs(struct otm8009a *ctx, const void *data,
1038c2ecf20Sopenharmony_ci				      size_t len)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/* data will be sent in dsi hs mode (ie. no lpm) */
1088c2ecf20Sopenharmony_ci	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	otm8009a_dcs_write_buf(ctx, data, len);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	/* restore back the dsi lpm mode */
1138c2ecf20Sopenharmony_ci	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#define dcs_write_seq(ctx, seq...)			\
1178c2ecf20Sopenharmony_ci({							\
1188c2ecf20Sopenharmony_ci	static const u8 d[] = { seq };			\
1198c2ecf20Sopenharmony_ci	otm8009a_dcs_write_buf(ctx, d, ARRAY_SIZE(d));	\
1208c2ecf20Sopenharmony_ci})
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#define dcs_write_cmd_at(ctx, cmd, seq...)		\
1238c2ecf20Sopenharmony_ci({							\
1248c2ecf20Sopenharmony_ci	dcs_write_seq(ctx, MCS_ADRSFT, (cmd) & 0xFF);	\
1258c2ecf20Sopenharmony_ci	dcs_write_seq(ctx, (cmd) >> 8, seq);		\
1268c2ecf20Sopenharmony_ci})
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic int otm8009a_init_sequence(struct otm8009a *ctx)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
1318c2ecf20Sopenharmony_ci	int ret;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* Enter CMD2 */
1348c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_CMD2_ENA1, 0x80, 0x09, 0x01);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	/* Enter Orise Command2 */
1378c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_CMD2_ENA2, 0x80, 0x09);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_SD_PCH_CTRL, 0x30);
1408c2ecf20Sopenharmony_ci	mdelay(10);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_NO_DOC1, 0x40);
1438c2ecf20Sopenharmony_ci	mdelay(10);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PWR_CTRL4 + 1, 0xA9);
1468c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PWR_CTRL2 + 1, 0x34);
1478c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_P_DRV_M, 0x50);
1488c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_VCOMDC, 0x4E);
1498c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_OSC_ADJ, 0x66); /* 65Hz */
1508c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PWR_CTRL2 + 2, 0x01);
1518c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PWR_CTRL2 + 5, 0x34);
1528c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PWR_CTRL2 + 4, 0x33);
1538c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_GVDDSET, 0x79, 0x79);
1548c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_SD_CTRL + 1, 0x1B);
1558c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PWR_CTRL1 + 2, 0x83);
1568c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_SD_PCH_CTRL + 1, 0x83);
1578c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_RGB_VID_SET, 0x0E);
1588c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANSET, 0x00, 0x01);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_GOAVST, 0x85, 0x01, 0x00, 0x84, 0x01, 0x00);
1618c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_GOACLKA1, 0x18, 0x04, 0x03, 0x39, 0x00, 0x00,
1628c2ecf20Sopenharmony_ci			 0x00, 0x18, 0x03, 0x03, 0x3A, 0x00, 0x00, 0x00);
1638c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_GOACLKA3, 0x18, 0x02, 0x03, 0x3B, 0x00, 0x00,
1648c2ecf20Sopenharmony_ci			 0x00, 0x18, 0x01, 0x03, 0x3C, 0x00, 0x00, 0x00);
1658c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_GOAECLK, 0x01, 0x01, 0x20, 0x20, 0x00, 0x00,
1668c2ecf20Sopenharmony_ci			 0x01, 0x02, 0x00, 0x00);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_NO_DOC2, 0x00);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANCTRLSET1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1718c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANCTRLSET2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1728c2ecf20Sopenharmony_ci			 0, 0, 0, 0, 0);
1738c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANCTRLSET3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1748c2ecf20Sopenharmony_ci			 0, 0, 0, 0, 0);
1758c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANCTRLSET4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1768c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANCTRLSET5, 0, 4, 4, 4, 4, 4, 0, 0, 0, 0,
1778c2ecf20Sopenharmony_ci			 0, 0, 0, 0, 0);
1788c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANCTRLSET6, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4,
1798c2ecf20Sopenharmony_ci			 4, 0, 0, 0, 0);
1808c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANCTRLSET7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1818c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANCTRLSET8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1828c2ecf20Sopenharmony_ci			 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANU2D1, 0x00, 0x26, 0x09, 0x0B, 0x01, 0x25,
1858c2ecf20Sopenharmony_ci			 0x00, 0x00, 0x00, 0x00);
1868c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANU2D2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1878c2ecf20Sopenharmony_ci			 0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0x0A, 0x0C, 0x02);
1888c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PANU2D3, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00,
1898c2ecf20Sopenharmony_ci			 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
1908c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PAND2U1, 0x00, 0x25, 0x0C, 0x0A, 0x02, 0x26,
1918c2ecf20Sopenharmony_ci			 0x00, 0x00, 0x00, 0x00);
1928c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PAND2U2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1938c2ecf20Sopenharmony_ci			 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x0B, 0x09, 0x01);
1948c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PAND2U3, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00,
1958c2ecf20Sopenharmony_ci			 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_PWR_CTRL1 + 1, 0x66);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_NO_DOC3, 0x06);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_GMCT2_2P, 0x00, 0x09, 0x0F, 0x0E, 0x07, 0x10,
2028c2ecf20Sopenharmony_ci			 0x0B, 0x0A, 0x04, 0x07, 0x0B, 0x08, 0x0F, 0x10, 0x0A,
2038c2ecf20Sopenharmony_ci			 0x01);
2048c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_GMCT2_2N, 0x00, 0x09, 0x0F, 0x0E, 0x07, 0x10,
2058c2ecf20Sopenharmony_ci			 0x0B, 0x0A, 0x04, 0x07, 0x0B, 0x08, 0x0F, 0x10, 0x0A,
2068c2ecf20Sopenharmony_ci			 0x01);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	/* Exit CMD2 */
2098c2ecf20Sopenharmony_ci	dcs_write_cmd_at(ctx, MCS_CMD2_ENA1, 0xFF, 0xFF, 0xFF);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	ret = mipi_dsi_dcs_nop(dsi);
2128c2ecf20Sopenharmony_ci	if (ret)
2138c2ecf20Sopenharmony_ci		return ret;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
2168c2ecf20Sopenharmony_ci	if (ret)
2178c2ecf20Sopenharmony_ci		return ret;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	/* Wait for sleep out exit */
2208c2ecf20Sopenharmony_ci	mdelay(120);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	/* Default portrait 480x800 rgb24 */
2238c2ecf20Sopenharmony_ci	dcs_write_seq(ctx, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	ret = mipi_dsi_dcs_set_column_address(dsi, 0,
2268c2ecf20Sopenharmony_ci					      default_mode.hdisplay - 1);
2278c2ecf20Sopenharmony_ci	if (ret)
2288c2ecf20Sopenharmony_ci		return ret;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	ret = mipi_dsi_dcs_set_page_address(dsi, 0, default_mode.vdisplay - 1);
2318c2ecf20Sopenharmony_ci	if (ret)
2328c2ecf20Sopenharmony_ci		return ret;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	/* See otm8009a driver documentation for pixel format descriptions */
2358c2ecf20Sopenharmony_ci	ret = mipi_dsi_dcs_set_pixel_format(dsi, MIPI_DCS_PIXEL_FMT_24BIT |
2368c2ecf20Sopenharmony_ci					    MIPI_DCS_PIXEL_FMT_24BIT << 4);
2378c2ecf20Sopenharmony_ci	if (ret)
2388c2ecf20Sopenharmony_ci		return ret;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	/* Disable CABC feature */
2418c2ecf20Sopenharmony_ci	dcs_write_seq(ctx, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	ret = mipi_dsi_dcs_set_display_on(dsi);
2448c2ecf20Sopenharmony_ci	if (ret)
2458c2ecf20Sopenharmony_ci		return ret;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	ret = mipi_dsi_dcs_nop(dsi);
2488c2ecf20Sopenharmony_ci	if (ret)
2498c2ecf20Sopenharmony_ci		return ret;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	/* Send Command GRAM memory write (no parameters) */
2528c2ecf20Sopenharmony_ci	dcs_write_seq(ctx, MIPI_DCS_WRITE_MEMORY_START);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	/* Wait a short while to let the panel be ready before the 1st frame */
2558c2ecf20Sopenharmony_ci	mdelay(10);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	return 0;
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic int otm8009a_disable(struct drm_panel *panel)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	struct otm8009a *ctx = panel_to_otm8009a(panel);
2638c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
2648c2ecf20Sopenharmony_ci	int ret;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	if (!ctx->enabled)
2678c2ecf20Sopenharmony_ci		return 0; /* This is not an issue so we return 0 here */
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	backlight_disable(ctx->bl_dev);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	ret = mipi_dsi_dcs_set_display_off(dsi);
2728c2ecf20Sopenharmony_ci	if (ret)
2738c2ecf20Sopenharmony_ci		return ret;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
2768c2ecf20Sopenharmony_ci	if (ret)
2778c2ecf20Sopenharmony_ci		return ret;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	msleep(120);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	ctx->enabled = false;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	return 0;
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic int otm8009a_unprepare(struct drm_panel *panel)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	struct otm8009a *ctx = panel_to_otm8009a(panel);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (!ctx->prepared)
2918c2ecf20Sopenharmony_ci		return 0;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	if (ctx->reset_gpio) {
2948c2ecf20Sopenharmony_ci		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
2958c2ecf20Sopenharmony_ci		msleep(20);
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	regulator_disable(ctx->supply);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	ctx->prepared = false;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	return 0;
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic int otm8009a_prepare(struct drm_panel *panel)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	struct otm8009a *ctx = panel_to_otm8009a(panel);
3088c2ecf20Sopenharmony_ci	int ret;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (ctx->prepared)
3118c2ecf20Sopenharmony_ci		return 0;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	ret = regulator_enable(ctx->supply);
3148c2ecf20Sopenharmony_ci	if (ret < 0) {
3158c2ecf20Sopenharmony_ci		dev_err(panel->dev, "failed to enable supply: %d\n", ret);
3168c2ecf20Sopenharmony_ci		return ret;
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	if (ctx->reset_gpio) {
3208c2ecf20Sopenharmony_ci		gpiod_set_value_cansleep(ctx->reset_gpio, 0);
3218c2ecf20Sopenharmony_ci		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
3228c2ecf20Sopenharmony_ci		msleep(20);
3238c2ecf20Sopenharmony_ci		gpiod_set_value_cansleep(ctx->reset_gpio, 0);
3248c2ecf20Sopenharmony_ci		msleep(100);
3258c2ecf20Sopenharmony_ci	}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	ret = otm8009a_init_sequence(ctx);
3288c2ecf20Sopenharmony_ci	if (ret)
3298c2ecf20Sopenharmony_ci		return ret;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	ctx->prepared = true;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	return 0;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic int otm8009a_enable(struct drm_panel *panel)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	struct otm8009a *ctx = panel_to_otm8009a(panel);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	if (ctx->enabled)
3418c2ecf20Sopenharmony_ci		return 0;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	backlight_enable(ctx->bl_dev);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	ctx->enabled = true;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	return 0;
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic int otm8009a_get_modes(struct drm_panel *panel,
3518c2ecf20Sopenharmony_ci			      struct drm_connector *connector)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	struct drm_display_mode *mode;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	mode = drm_mode_duplicate(connector->dev, &default_mode);
3568c2ecf20Sopenharmony_ci	if (!mode) {
3578c2ecf20Sopenharmony_ci		dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
3588c2ecf20Sopenharmony_ci			default_mode.hdisplay, default_mode.vdisplay,
3598c2ecf20Sopenharmony_ci			drm_mode_vrefresh(&default_mode));
3608c2ecf20Sopenharmony_ci		return -ENOMEM;
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	drm_mode_set_name(mode);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
3668c2ecf20Sopenharmony_ci	drm_mode_probed_add(connector, mode);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	connector->display_info.width_mm = mode->width_mm;
3698c2ecf20Sopenharmony_ci	connector->display_info.height_mm = mode->height_mm;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	return 1;
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cistatic const struct drm_panel_funcs otm8009a_drm_funcs = {
3758c2ecf20Sopenharmony_ci	.disable   = otm8009a_disable,
3768c2ecf20Sopenharmony_ci	.unprepare = otm8009a_unprepare,
3778c2ecf20Sopenharmony_ci	.prepare   = otm8009a_prepare,
3788c2ecf20Sopenharmony_ci	.enable    = otm8009a_enable,
3798c2ecf20Sopenharmony_ci	.get_modes = otm8009a_get_modes,
3808c2ecf20Sopenharmony_ci};
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci/*
3838c2ecf20Sopenharmony_ci * DSI-BASED BACKLIGHT
3848c2ecf20Sopenharmony_ci */
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic int otm8009a_backlight_update_status(struct backlight_device *bd)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	struct otm8009a *ctx = bl_get_data(bd);
3898c2ecf20Sopenharmony_ci	u8 data[2];
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	if (!ctx->prepared) {
3928c2ecf20Sopenharmony_ci		dev_dbg(&bd->dev, "lcd not ready yet for setting its backlight!\n");
3938c2ecf20Sopenharmony_ci		return -ENXIO;
3948c2ecf20Sopenharmony_ci	}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	if (bd->props.power <= FB_BLANK_NORMAL) {
3978c2ecf20Sopenharmony_ci		/* Power on the backlight with the requested brightness
3988c2ecf20Sopenharmony_ci		 * Note We can not use mipi_dsi_dcs_set_display_brightness()
3998c2ecf20Sopenharmony_ci		 * as otm8009a driver support only 8-bit brightness (1 param).
4008c2ecf20Sopenharmony_ci		 */
4018c2ecf20Sopenharmony_ci		data[0] = MIPI_DCS_SET_DISPLAY_BRIGHTNESS;
4028c2ecf20Sopenharmony_ci		data[1] = bd->props.brightness;
4038c2ecf20Sopenharmony_ci		otm8009a_dcs_write_buf_hs(ctx, data, ARRAY_SIZE(data));
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci		/* set Brightness Control & Backlight on */
4068c2ecf20Sopenharmony_ci		data[1] = 0x24;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	} else {
4098c2ecf20Sopenharmony_ci		/* Power off the backlight: set Brightness Control & Bl off */
4108c2ecf20Sopenharmony_ci		data[1] = 0;
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	/* Update Brightness Control & Backlight */
4148c2ecf20Sopenharmony_ci	data[0] = MIPI_DCS_WRITE_CONTROL_DISPLAY;
4158c2ecf20Sopenharmony_ci	otm8009a_dcs_write_buf_hs(ctx, data, ARRAY_SIZE(data));
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	return 0;
4188c2ecf20Sopenharmony_ci}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_cistatic const struct backlight_ops otm8009a_backlight_ops = {
4218c2ecf20Sopenharmony_ci	.update_status = otm8009a_backlight_update_status,
4228c2ecf20Sopenharmony_ci};
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic int otm8009a_probe(struct mipi_dsi_device *dsi)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	struct device *dev = &dsi->dev;
4278c2ecf20Sopenharmony_ci	struct otm8009a *ctx;
4288c2ecf20Sopenharmony_ci	int ret;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
4318c2ecf20Sopenharmony_ci	if (!ctx)
4328c2ecf20Sopenharmony_ci		return -ENOMEM;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
4358c2ecf20Sopenharmony_ci	if (IS_ERR(ctx->reset_gpio)) {
4368c2ecf20Sopenharmony_ci		dev_err(dev, "cannot get reset-gpio\n");
4378c2ecf20Sopenharmony_ci		return PTR_ERR(ctx->reset_gpio);
4388c2ecf20Sopenharmony_ci	}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	ctx->supply = devm_regulator_get(dev, "power");
4418c2ecf20Sopenharmony_ci	if (IS_ERR(ctx->supply)) {
4428c2ecf20Sopenharmony_ci		ret = PTR_ERR(ctx->supply);
4438c2ecf20Sopenharmony_ci		if (ret != -EPROBE_DEFER)
4448c2ecf20Sopenharmony_ci			dev_err(dev, "failed to request regulator: %d\n", ret);
4458c2ecf20Sopenharmony_ci		return ret;
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	mipi_dsi_set_drvdata(dsi, ctx);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	ctx->dev = dev;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	dsi->lanes = 2;
4538c2ecf20Sopenharmony_ci	dsi->format = MIPI_DSI_FMT_RGB888;
4548c2ecf20Sopenharmony_ci	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
4558c2ecf20Sopenharmony_ci			  MIPI_DSI_MODE_LPM | MIPI_DSI_CLOCK_NON_CONTINUOUS;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	drm_panel_init(&ctx->panel, dev, &otm8009a_drm_funcs,
4588c2ecf20Sopenharmony_ci		       DRM_MODE_CONNECTOR_DSI);
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	ctx->bl_dev = devm_backlight_device_register(dev, dev_name(dev),
4618c2ecf20Sopenharmony_ci						     dev, ctx,
4628c2ecf20Sopenharmony_ci						     &otm8009a_backlight_ops,
4638c2ecf20Sopenharmony_ci						     NULL);
4648c2ecf20Sopenharmony_ci	if (IS_ERR(ctx->bl_dev)) {
4658c2ecf20Sopenharmony_ci		ret = PTR_ERR(ctx->bl_dev);
4668c2ecf20Sopenharmony_ci		dev_err(dev, "failed to register backlight: %d\n", ret);
4678c2ecf20Sopenharmony_ci		return ret;
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	ctx->bl_dev->props.max_brightness = OTM8009A_BACKLIGHT_MAX;
4718c2ecf20Sopenharmony_ci	ctx->bl_dev->props.brightness = OTM8009A_BACKLIGHT_DEFAULT;
4728c2ecf20Sopenharmony_ci	ctx->bl_dev->props.power = FB_BLANK_POWERDOWN;
4738c2ecf20Sopenharmony_ci	ctx->bl_dev->props.type = BACKLIGHT_RAW;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	drm_panel_add(&ctx->panel);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	ret = mipi_dsi_attach(dsi);
4788c2ecf20Sopenharmony_ci	if (ret < 0) {
4798c2ecf20Sopenharmony_ci		dev_err(dev, "mipi_dsi_attach failed. Is host ready?\n");
4808c2ecf20Sopenharmony_ci		drm_panel_remove(&ctx->panel);
4818c2ecf20Sopenharmony_ci		return ret;
4828c2ecf20Sopenharmony_ci	}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	return 0;
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cistatic int otm8009a_remove(struct mipi_dsi_device *dsi)
4888c2ecf20Sopenharmony_ci{
4898c2ecf20Sopenharmony_ci	struct otm8009a *ctx = mipi_dsi_get_drvdata(dsi);
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	mipi_dsi_detach(dsi);
4928c2ecf20Sopenharmony_ci	drm_panel_remove(&ctx->panel);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	return 0;
4958c2ecf20Sopenharmony_ci}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistatic const struct of_device_id orisetech_otm8009a_of_match[] = {
4988c2ecf20Sopenharmony_ci	{ .compatible = "orisetech,otm8009a" },
4998c2ecf20Sopenharmony_ci	{ }
5008c2ecf20Sopenharmony_ci};
5018c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, orisetech_otm8009a_of_match);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_cistatic struct mipi_dsi_driver orisetech_otm8009a_driver = {
5048c2ecf20Sopenharmony_ci	.probe  = otm8009a_probe,
5058c2ecf20Sopenharmony_ci	.remove = otm8009a_remove,
5068c2ecf20Sopenharmony_ci	.driver = {
5078c2ecf20Sopenharmony_ci		.name = "panel-orisetech-otm8009a",
5088c2ecf20Sopenharmony_ci		.of_match_table = orisetech_otm8009a_of_match,
5098c2ecf20Sopenharmony_ci	},
5108c2ecf20Sopenharmony_ci};
5118c2ecf20Sopenharmony_cimodule_mipi_dsi_driver(orisetech_otm8009a_driver);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ciMODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
5148c2ecf20Sopenharmony_ciMODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
5158c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("DRM driver for Orise Tech OTM8009A MIPI DSI panel");
5168c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
517