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 bool prepared;
23 };
24
25 static inline struct
to_s6e88a0_ams452ef01(struct drm_panel * panel)26 s6e88a0_ams452ef01 *to_s6e88a0_ams452ef01(struct drm_panel *panel)
27 {
28 return container_of(panel, struct s6e88a0_ams452ef01, panel);
29 }
30
s6e88a0_ams452ef01_reset(struct s6e88a0_ams452ef01 * ctx)31 static void s6e88a0_ams452ef01_reset(struct s6e88a0_ams452ef01 *ctx)
32 {
33 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
34 usleep_range(5000, 6000);
35 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
36 usleep_range(1000, 2000);
37 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
38 usleep_range(10000, 11000);
39 }
40
s6e88a0_ams452ef01_on(struct s6e88a0_ams452ef01 * ctx)41 static int s6e88a0_ams452ef01_on(struct s6e88a0_ams452ef01 *ctx)
42 {
43 struct mipi_dsi_device *dsi = ctx->dsi;
44 struct device *dev = &dsi->dev;
45 int ret;
46
47 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
48
49 mipi_dsi_dcs_write_seq(dsi, 0xf0, 0x5a, 0x5a); // enable LEVEL2 commands
50 mipi_dsi_dcs_write_seq(dsi, 0xcc, 0x4c); // set Pixel Clock Divider polarity
51
52 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
53 if (ret < 0) {
54 dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
55 return ret;
56 }
57 msleep(120);
58
59 // set default brightness/gama
60 mipi_dsi_dcs_write_seq(dsi, 0xca,
61 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, // V255 RR,GG,BB
62 0x80, 0x80, 0x80, // V203 R,G,B
63 0x80, 0x80, 0x80, // V151 R,G,B
64 0x80, 0x80, 0x80, // V87 R,G,B
65 0x80, 0x80, 0x80, // V51 R,G,B
66 0x80, 0x80, 0x80, // V35 R,G,B
67 0x80, 0x80, 0x80, // V23 R,G,B
68 0x80, 0x80, 0x80, // V11 R,G,B
69 0x6b, 0x68, 0x71, // V3 R,G,B
70 0x00, 0x00, 0x00); // V1 R,G,B
71 // set default Amoled Off Ratio
72 mipi_dsi_dcs_write_seq(dsi, 0xb2, 0x40, 0x0a, 0x17, 0x00, 0x0a);
73 mipi_dsi_dcs_write_seq(dsi, 0xb6, 0x2c, 0x0b); // set default elvss voltage
74 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
75 mipi_dsi_dcs_write_seq(dsi, 0xf7, 0x03); // gamma/aor update
76 mipi_dsi_dcs_write_seq(dsi, 0xf0, 0xa5, 0xa5); // disable LEVEL2 commands
77
78 ret = mipi_dsi_dcs_set_display_on(dsi);
79 if (ret < 0) {
80 dev_err(dev, "Failed to set display on: %d\n", ret);
81 return ret;
82 }
83
84 return 0;
85 }
86
s6e88a0_ams452ef01_off(struct s6e88a0_ams452ef01 * ctx)87 static int s6e88a0_ams452ef01_off(struct s6e88a0_ams452ef01 *ctx)
88 {
89 struct mipi_dsi_device *dsi = ctx->dsi;
90 struct device *dev = &dsi->dev;
91 int ret;
92
93 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
94
95 ret = mipi_dsi_dcs_set_display_off(dsi);
96 if (ret < 0) {
97 dev_err(dev, "Failed to set display off: %d\n", ret);
98 return ret;
99 }
100 msleep(35);
101
102 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
103 if (ret < 0) {
104 dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
105 return ret;
106 }
107 msleep(120);
108
109 return 0;
110 }
111
s6e88a0_ams452ef01_prepare(struct drm_panel * panel)112 static int s6e88a0_ams452ef01_prepare(struct drm_panel *panel)
113 {
114 struct s6e88a0_ams452ef01 *ctx = to_s6e88a0_ams452ef01(panel);
115 struct device *dev = &ctx->dsi->dev;
116 int ret;
117
118 if (ctx->prepared)
119 return 0;
120
121 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
122 if (ret < 0) {
123 dev_err(dev, "Failed to enable regulators: %d\n", ret);
124 return ret;
125 }
126
127 s6e88a0_ams452ef01_reset(ctx);
128
129 ret = s6e88a0_ams452ef01_on(ctx);
130 if (ret < 0) {
131 dev_err(dev, "Failed to initialize panel: %d\n", ret);
132 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
133 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies),
134 ctx->supplies);
135 return ret;
136 }
137
138 ctx->prepared = true;
139 return 0;
140 }
141
s6e88a0_ams452ef01_unprepare(struct drm_panel * panel)142 static int s6e88a0_ams452ef01_unprepare(struct drm_panel *panel)
143 {
144 struct s6e88a0_ams452ef01 *ctx = to_s6e88a0_ams452ef01(panel);
145 struct device *dev = &ctx->dsi->dev;
146 int ret;
147
148 if (!ctx->prepared)
149 return 0;
150
151 ret = s6e88a0_ams452ef01_off(ctx);
152 if (ret < 0)
153 dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
154
155 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
156 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
157
158 ctx->prepared = false;
159 return 0;
160 }
161
162 static const struct drm_display_mode s6e88a0_ams452ef01_mode = {
163 .clock = (540 + 88 + 4 + 20) * (960 + 14 + 2 + 8) * 60 / 1000,
164 .hdisplay = 540,
165 .hsync_start = 540 + 88,
166 .hsync_end = 540 + 88 + 4,
167 .htotal = 540 + 88 + 4 + 20,
168 .vdisplay = 960,
169 .vsync_start = 960 + 14,
170 .vsync_end = 960 + 14 + 2,
171 .vtotal = 960 + 14 + 2 + 8,
172 .width_mm = 56,
173 .height_mm = 100,
174 };
175
s6e88a0_ams452ef01_get_modes(struct drm_panel * panel,struct drm_connector * connector)176 static int s6e88a0_ams452ef01_get_modes(struct drm_panel *panel,
177 struct drm_connector *connector)
178 {
179 struct drm_display_mode *mode;
180
181 mode = drm_mode_duplicate(connector->dev, &s6e88a0_ams452ef01_mode);
182 if (!mode)
183 return -ENOMEM;
184
185 drm_mode_set_name(mode);
186
187 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
188 connector->display_info.width_mm = mode->width_mm;
189 connector->display_info.height_mm = mode->height_mm;
190 drm_mode_probed_add(connector, mode);
191
192 return 1;
193 }
194
195 static const struct drm_panel_funcs s6e88a0_ams452ef01_panel_funcs = {
196 .unprepare = s6e88a0_ams452ef01_unprepare,
197 .prepare = s6e88a0_ams452ef01_prepare,
198 .get_modes = s6e88a0_ams452ef01_get_modes,
199 };
200
s6e88a0_ams452ef01_probe(struct mipi_dsi_device * dsi)201 static int s6e88a0_ams452ef01_probe(struct mipi_dsi_device *dsi)
202 {
203 struct device *dev = &dsi->dev;
204 struct s6e88a0_ams452ef01 *ctx;
205 int ret;
206
207 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
208 if (!ctx)
209 return -ENOMEM;
210
211 ctx->supplies[0].supply = "vdd3";
212 ctx->supplies[1].supply = "vci";
213 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
214 ctx->supplies);
215 if (ret < 0) {
216 dev_err(dev, "Failed to get regulators: %d\n", ret);
217 return ret;
218 }
219
220 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
221 if (IS_ERR(ctx->reset_gpio)) {
222 ret = PTR_ERR(ctx->reset_gpio);
223 dev_err(dev, "Failed to get reset-gpios: %d\n", ret);
224 return ret;
225 }
226
227 ctx->dsi = dsi;
228 mipi_dsi_set_drvdata(dsi, ctx);
229
230 dsi->lanes = 2;
231 dsi->format = MIPI_DSI_FMT_RGB888;
232 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST;
233
234 drm_panel_init(&ctx->panel, dev, &s6e88a0_ams452ef01_panel_funcs,
235 DRM_MODE_CONNECTOR_DSI);
236
237 drm_panel_add(&ctx->panel);
238
239 ret = mipi_dsi_attach(dsi);
240 if (ret < 0) {
241 dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
242 drm_panel_remove(&ctx->panel);
243 return ret;
244 }
245
246 return 0;
247 }
248
s6e88a0_ams452ef01_remove(struct mipi_dsi_device * dsi)249 static void s6e88a0_ams452ef01_remove(struct mipi_dsi_device *dsi)
250 {
251 struct s6e88a0_ams452ef01 *ctx = mipi_dsi_get_drvdata(dsi);
252 int ret;
253
254 ret = mipi_dsi_detach(dsi);
255 if (ret < 0)
256 dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
257
258 drm_panel_remove(&ctx->panel);
259 }
260
261 static const struct of_device_id s6e88a0_ams452ef01_of_match[] = {
262 { .compatible = "samsung,s6e88a0-ams452ef01" },
263 { /* sentinel */ },
264 };
265 MODULE_DEVICE_TABLE(of, s6e88a0_ams452ef01_of_match);
266
267 static struct mipi_dsi_driver s6e88a0_ams452ef01_driver = {
268 .probe = s6e88a0_ams452ef01_probe,
269 .remove = s6e88a0_ams452ef01_remove,
270 .driver = {
271 .name = "panel-s6e88a0-ams452ef01",
272 .of_match_table = s6e88a0_ams452ef01_of_match,
273 },
274 };
275 module_mipi_dsi_driver(s6e88a0_ams452ef01_driver);
276
277 MODULE_AUTHOR("Michael Srba <Michael.Srba@seznam.cz>");
278 MODULE_DESCRIPTION("MIPI-DSI based Panel Driver for AMS452EF01 AMOLED LCD with a S6E88A0 controller");
279 MODULE_LICENSE("GPL v2");
280