xref: /linux/drivers/gpu/drm/panel/panel-samsung-s6e3fa7.c (revision e77a8005748547fb1f10645097f13ccdd804d7e5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for the Samsung S6E3FA7 panel.
4  *
5  * Copyright (c) 2022-2024, The Linux Foundation. All rights reserved.
6  * Generated with linux-mdss-dsi-panel-driver-generator from vendor device tree:
7  * Copyright (c) 2013, The Linux Foundation. All rights reserved.
8  */
9 
10 #include <linux/backlight.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 
16 #include <video/mipi_display.h>
17 
18 #include <drm/drm_mipi_dsi.h>
19 #include <drm/drm_modes.h>
20 #include <drm/drm_panel.h>
21 
22 struct s6e3fa7_panel {
23 	struct drm_panel panel;
24 	struct mipi_dsi_device *dsi;
25 	struct gpio_desc *reset_gpio;
26 };
27 
28 static inline struct s6e3fa7_panel *to_s6e3fa7_panel(struct drm_panel *panel)
29 {
30 	return container_of(panel, struct s6e3fa7_panel, panel);
31 }
32 
33 static void s6e3fa7_panel_reset(struct s6e3fa7_panel *ctx)
34 {
35 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
36 	usleep_range(1000, 2000);
37 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
38 	usleep_range(10000, 11000);
39 }
40 
41 static int s6e3fa7_panel_on(struct mipi_dsi_device *dsi)
42 {
43 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
44 
45 	mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx);
46 	mipi_dsi_msleep(&dsi_ctx, 120);
47 	mipi_dsi_dcs_set_tear_on_multi(&dsi_ctx, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
48 
49 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xf0, 0x5a, 0x5a);
50 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xf4,
51 				     0xbb, 0x23, 0x19, 0x3a, 0x9f, 0x0f, 0x09, 0xc0,
52 				     0x00, 0xb4, 0x37, 0x70, 0x79, 0x69);
53 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xf0, 0xa5, 0xa5);
54 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x20);
55 
56 	mipi_dsi_dcs_set_display_on_multi(&dsi_ctx);
57 
58 	return dsi_ctx.accum_err;
59 }
60 
61 static int s6e3fa7_panel_prepare(struct drm_panel *panel)
62 {
63 	struct s6e3fa7_panel *ctx = to_s6e3fa7_panel(panel);
64 	int ret;
65 
66 	s6e3fa7_panel_reset(ctx);
67 
68 	ret = s6e3fa7_panel_on(ctx->dsi);
69 	if (ret < 0)
70 		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
71 
72 	return ret;
73 }
74 
75 static int s6e3fa7_panel_unprepare(struct drm_panel *panel)
76 {
77 	struct s6e3fa7_panel *ctx = to_s6e3fa7_panel(panel);
78 
79 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
80 
81 	return 0;
82 }
83 
84 static int s6e3fa7_panel_disable(struct drm_panel *panel)
85 {
86 	struct s6e3fa7_panel *ctx = to_s6e3fa7_panel(panel);
87 	struct mipi_dsi_device *dsi = ctx->dsi;
88 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
89 
90 	mipi_dsi_dcs_set_display_off_multi(&dsi_ctx);
91 	mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx);
92 	mipi_dsi_msleep(&dsi_ctx, 120);
93 
94 	return dsi_ctx.accum_err;
95 }
96 
97 static const struct drm_display_mode s6e3fa7_panel_mode = {
98 	.clock = (1080 + 32 + 32 + 78) * (2220 + 32 + 4 + 78) * 60 / 1000,
99 	.hdisplay = 1080,
100 	.hsync_start = 1080 + 32,
101 	.hsync_end = 1080 + 32 + 32,
102 	.htotal = 1080 + 32 + 32 + 78,
103 	.vdisplay = 2220,
104 	.vsync_start = 2220 + 32,
105 	.vsync_end = 2220 + 32 + 4,
106 	.vtotal = 2220 + 32 + 4 + 78,
107 	.width_mm = 62,
108 	.height_mm = 127,
109 };
110 
111 static int s6e3fa7_panel_get_modes(struct drm_panel *panel,
112 				 struct drm_connector *connector)
113 {
114 	struct drm_display_mode *mode;
115 
116 	mode = drm_mode_duplicate(connector->dev, &s6e3fa7_panel_mode);
117 	if (!mode)
118 		return -ENOMEM;
119 
120 	drm_mode_set_name(mode);
121 
122 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
123 	connector->display_info.width_mm = mode->width_mm;
124 	connector->display_info.height_mm = mode->height_mm;
125 	drm_mode_probed_add(connector, mode);
126 
127 	return 1;
128 }
129 
130 static const struct drm_panel_funcs s6e3fa7_panel_funcs = {
131 	.prepare = s6e3fa7_panel_prepare,
132 	.unprepare = s6e3fa7_panel_unprepare,
133 	.disable = s6e3fa7_panel_disable,
134 	.get_modes = s6e3fa7_panel_get_modes,
135 };
136 
137 static int s6e3fa7_panel_bl_update_status(struct backlight_device *bl)
138 {
139 	struct mipi_dsi_device *dsi = bl_get_data(bl);
140 	u16 brightness = backlight_get_brightness(bl);
141 	int ret;
142 
143 	ret = mipi_dsi_dcs_set_display_brightness_large(dsi, brightness);
144 	if (ret < 0)
145 		return ret;
146 
147 	return 0;
148 }
149 
150 static int s6e3fa7_panel_bl_get_brightness(struct backlight_device *bl)
151 {
152 	struct mipi_dsi_device *dsi = bl_get_data(bl);
153 	u16 brightness;
154 	int ret;
155 
156 	ret = mipi_dsi_dcs_get_display_brightness_large(dsi, &brightness);
157 	if (ret < 0)
158 		return ret;
159 
160 	return brightness;
161 }
162 
163 static const struct backlight_ops s6e3fa7_panel_bl_ops = {
164 	.update_status = s6e3fa7_panel_bl_update_status,
165 	.get_brightness = s6e3fa7_panel_bl_get_brightness,
166 };
167 
168 static struct backlight_device *
169 s6e3fa7_panel_create_backlight(struct mipi_dsi_device *dsi)
170 {
171 	struct device *dev = &dsi->dev;
172 	const struct backlight_properties props = {
173 		.type = BACKLIGHT_RAW,
174 		.brightness = 1023,
175 		.max_brightness = 1023,
176 	};
177 
178 	return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
179 					      &s6e3fa7_panel_bl_ops, &props);
180 }
181 
182 static int s6e3fa7_panel_probe(struct mipi_dsi_device *dsi)
183 {
184 	struct device *dev = &dsi->dev;
185 	struct s6e3fa7_panel *ctx;
186 	int ret;
187 
188 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
189 	if (!ctx)
190 		return -ENOMEM;
191 
192 	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
193 	if (IS_ERR(ctx->reset_gpio))
194 		return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
195 				     "Failed to get reset-gpios\n");
196 
197 	ctx->dsi = dsi;
198 	mipi_dsi_set_drvdata(dsi, ctx);
199 
200 	dsi->lanes = 4;
201 	dsi->format = MIPI_DSI_FMT_RGB888;
202 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO_BURST |
203 			  MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM;
204 
205 	drm_panel_init(&ctx->panel, dev, &s6e3fa7_panel_funcs,
206 		       DRM_MODE_CONNECTOR_DSI);
207 	ctx->panel.prepare_prev_first = true;
208 
209 	ctx->panel.backlight = s6e3fa7_panel_create_backlight(dsi);
210 	if (IS_ERR(ctx->panel.backlight))
211 		return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
212 				     "Failed to create backlight\n");
213 
214 	drm_panel_add(&ctx->panel);
215 
216 	ret = mipi_dsi_attach(dsi);
217 	if (ret < 0) {
218 		dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
219 		drm_panel_remove(&ctx->panel);
220 		return ret;
221 	}
222 
223 	return 0;
224 }
225 
226 static void s6e3fa7_panel_remove(struct mipi_dsi_device *dsi)
227 {
228 	struct s6e3fa7_panel *ctx = mipi_dsi_get_drvdata(dsi);
229 	int ret;
230 
231 	ret = mipi_dsi_detach(dsi);
232 	if (ret < 0)
233 		dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
234 
235 	drm_panel_remove(&ctx->panel);
236 }
237 
238 static const struct of_device_id s6e3fa7_panel_of_match[] = {
239 	{ .compatible = "samsung,s6e3fa7-ams559nk06" },
240 	{ /* sentinel */ }
241 };
242 MODULE_DEVICE_TABLE(of, s6e3fa7_panel_of_match);
243 
244 static struct mipi_dsi_driver s6e3fa7_panel_driver = {
245 	.probe = s6e3fa7_panel_probe,
246 	.remove = s6e3fa7_panel_remove,
247 	.driver = {
248 		.name = "panel-samsung-s6e3fa7",
249 		.of_match_table = s6e3fa7_panel_of_match,
250 	},
251 };
252 module_mipi_dsi_driver(s6e3fa7_panel_driver);
253 
254 MODULE_AUTHOR("Richard Acayan <mailingradian@gmail.com>");
255 MODULE_DESCRIPTION("DRM driver for Samsung S6E3FA7 command mode DSI panel");
256 MODULE_LICENSE("GPL");
257