xref: /linux/drivers/gpu/drm/panel/panel-samsung-s6e88a0-ams452ef01.c (revision 6315d93541f8a5f77c5ef5c4f25233e66d189603)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2019, Michael Srba
3 
4 #include <linux/delay.h>
5 #include <linux/gpio/consumer.h>
6 #include <linux/module.h>
7 #include <linux/of.h>
8 #include <linux/regulator/consumer.h>
9 
10 #include <video/mipi_display.h>
11 
12 #include <drm/drm_mipi_dsi.h>
13 #include <drm/drm_modes.h>
14 #include <drm/drm_panel.h>
15 
16 struct s6e88a0_ams452ef01 {
17 	struct drm_panel panel;
18 	struct mipi_dsi_device *dsi;
19 	struct regulator_bulk_data supplies[2];
20 	struct gpio_desc *reset_gpio;
21 };
22 
23 static inline struct
24 s6e88a0_ams452ef01 *to_s6e88a0_ams452ef01(struct drm_panel *panel)
25 {
26 	return container_of(panel, struct s6e88a0_ams452ef01, panel);
27 }
28 
29 static void s6e88a0_ams452ef01_reset(struct s6e88a0_ams452ef01 *ctx)
30 {
31 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
32 	usleep_range(5000, 6000);
33 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
34 	usleep_range(1000, 2000);
35 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
36 	usleep_range(10000, 11000);
37 }
38 
39 static int s6e88a0_ams452ef01_on(struct s6e88a0_ams452ef01 *ctx)
40 {
41 	struct mipi_dsi_device *dsi = ctx->dsi;
42 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi };
43 
44 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
45 
46 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xf0, 0x5a, 0x5a); // enable LEVEL2 commands
47 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xcc, 0x4c); // set Pixel Clock Divider polarity
48 
49 	mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx);
50 	mipi_dsi_msleep(&dsi_ctx, 120);
51 
52 	// set default brightness/gama
53 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xca,
54 				     0x01, 0x00, 0x01, 0x00, 0x01, 0x00,// V255 RR,GG,BB
55 				     0x80, 0x80, 0x80,			// V203 R,G,B
56 				     0x80, 0x80, 0x80,			// V151 R,G,B
57 				     0x80, 0x80, 0x80,			// V87  R,G,B
58 				     0x80, 0x80, 0x80,			// V51  R,G,B
59 				     0x80, 0x80, 0x80,			// V35  R,G,B
60 				     0x80, 0x80, 0x80,			// V23  R,G,B
61 				     0x80, 0x80, 0x80,			// V11  R,G,B
62 				     0x6b, 0x68, 0x71,			// V3   R,G,B
63 				     0x00, 0x00, 0x00);			// V1   R,G,B
64 	// set default Amoled Off Ratio
65 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xb2, 0x40, 0x0a, 0x17, 0x00, 0x0a);
66 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xb6, 0x2c, 0x0b); // set default elvss voltage
67 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
68 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xf7, 0x03); // gamma/aor update
69 	mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xf0, 0xa5, 0xa5); // disable LEVEL2 commands
70 
71 	mipi_dsi_dcs_set_display_on_multi(&dsi_ctx);
72 
73 	return dsi_ctx.accum_err;
74 }
75 
76 static void s6e88a0_ams452ef01_off(struct s6e88a0_ams452ef01 *ctx)
77 {
78 	struct mipi_dsi_device *dsi = ctx->dsi;
79 	struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi};
80 
81 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
82 
83 	mipi_dsi_dcs_set_display_off_multi(&dsi_ctx);
84 	mipi_dsi_msleep(&dsi_ctx, 35);
85 	mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx);
86 	mipi_dsi_msleep(&dsi_ctx, 120);
87 }
88 
89 static int s6e88a0_ams452ef01_prepare(struct drm_panel *panel)
90 {
91 	struct s6e88a0_ams452ef01 *ctx = to_s6e88a0_ams452ef01(panel);
92 	int ret;
93 
94 	ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
95 	if (ret < 0)
96 		return ret;
97 
98 	s6e88a0_ams452ef01_reset(ctx);
99 
100 	ret = s6e88a0_ams452ef01_on(ctx);
101 	if (ret < 0) {
102 		gpiod_set_value_cansleep(ctx->reset_gpio, 0);
103 		regulator_bulk_disable(ARRAY_SIZE(ctx->supplies),
104 				       ctx->supplies);
105 		return ret;
106 	}
107 
108 	return 0;
109 }
110 
111 static int s6e88a0_ams452ef01_unprepare(struct drm_panel *panel)
112 {
113 	struct s6e88a0_ams452ef01 *ctx = to_s6e88a0_ams452ef01(panel);
114 
115 	s6e88a0_ams452ef01_off(ctx);
116 
117 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
118 	regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
119 
120 	return 0;
121 }
122 
123 static const struct drm_display_mode s6e88a0_ams452ef01_mode = {
124 	.clock = (540 + 88 + 4 + 20) * (960 + 14 + 2 + 8) * 60 / 1000,
125 	.hdisplay = 540,
126 	.hsync_start = 540 + 88,
127 	.hsync_end = 540 + 88 + 4,
128 	.htotal = 540 + 88 + 4 + 20,
129 	.vdisplay = 960,
130 	.vsync_start = 960 + 14,
131 	.vsync_end = 960 + 14 + 2,
132 	.vtotal = 960 + 14 + 2 + 8,
133 	.width_mm = 56,
134 	.height_mm = 100,
135 };
136 
137 static int s6e88a0_ams452ef01_get_modes(struct drm_panel *panel,
138 					struct drm_connector *connector)
139 {
140 	struct drm_display_mode *mode;
141 
142 	mode = drm_mode_duplicate(connector->dev, &s6e88a0_ams452ef01_mode);
143 	if (!mode)
144 		return -ENOMEM;
145 
146 	drm_mode_set_name(mode);
147 
148 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
149 	connector->display_info.width_mm = mode->width_mm;
150 	connector->display_info.height_mm = mode->height_mm;
151 	drm_mode_probed_add(connector, mode);
152 
153 	return 1;
154 }
155 
156 static const struct drm_panel_funcs s6e88a0_ams452ef01_panel_funcs = {
157 	.unprepare = s6e88a0_ams452ef01_unprepare,
158 	.prepare = s6e88a0_ams452ef01_prepare,
159 	.get_modes = s6e88a0_ams452ef01_get_modes,
160 };
161 
162 static int s6e88a0_ams452ef01_probe(struct mipi_dsi_device *dsi)
163 {
164 	struct device *dev = &dsi->dev;
165 	struct s6e88a0_ams452ef01 *ctx;
166 	int ret;
167 
168 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
169 	if (!ctx)
170 		return -ENOMEM;
171 
172 	ctx->supplies[0].supply = "vdd3";
173 	ctx->supplies[1].supply = "vci";
174 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
175 				      ctx->supplies);
176 	if (ret < 0) {
177 		dev_err(dev, "Failed to get regulators: %d\n", ret);
178 		return ret;
179 	}
180 
181 	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
182 	if (IS_ERR(ctx->reset_gpio)) {
183 		ret = PTR_ERR(ctx->reset_gpio);
184 		dev_err(dev, "Failed to get reset-gpios: %d\n", ret);
185 		return ret;
186 	}
187 
188 	ctx->dsi = dsi;
189 	mipi_dsi_set_drvdata(dsi, ctx);
190 
191 	dsi->lanes = 2;
192 	dsi->format = MIPI_DSI_FMT_RGB888;
193 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST;
194 
195 	drm_panel_init(&ctx->panel, dev, &s6e88a0_ams452ef01_panel_funcs,
196 		       DRM_MODE_CONNECTOR_DSI);
197 
198 	drm_panel_add(&ctx->panel);
199 
200 	ret = mipi_dsi_attach(dsi);
201 	if (ret < 0) {
202 		dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
203 		drm_panel_remove(&ctx->panel);
204 		return ret;
205 	}
206 
207 	return 0;
208 }
209 
210 static void s6e88a0_ams452ef01_remove(struct mipi_dsi_device *dsi)
211 {
212 	struct s6e88a0_ams452ef01 *ctx = mipi_dsi_get_drvdata(dsi);
213 	int ret;
214 
215 	ret = mipi_dsi_detach(dsi);
216 	if (ret < 0)
217 		dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
218 
219 	drm_panel_remove(&ctx->panel);
220 }
221 
222 static const struct of_device_id s6e88a0_ams452ef01_of_match[] = {
223 	{ .compatible = "samsung,s6e88a0-ams452ef01" },
224 	{ /* sentinel */ },
225 };
226 MODULE_DEVICE_TABLE(of, s6e88a0_ams452ef01_of_match);
227 
228 static struct mipi_dsi_driver s6e88a0_ams452ef01_driver = {
229 	.probe = s6e88a0_ams452ef01_probe,
230 	.remove = s6e88a0_ams452ef01_remove,
231 	.driver = {
232 		.name = "panel-s6e88a0-ams452ef01",
233 		.of_match_table = s6e88a0_ams452ef01_of_match,
234 	},
235 };
236 module_mipi_dsi_driver(s6e88a0_ams452ef01_driver);
237 
238 MODULE_AUTHOR("Michael Srba <Michael.Srba@seznam.cz>");
239 MODULE_DESCRIPTION("MIPI-DSI based Panel Driver for AMS452EF01 AMOLED LCD with a S6E88A0 controller");
240 MODULE_LICENSE("GPL v2");
241