1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <linux/backlight.h>
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 <drm/drm_mipi_dsi.h>
11 #include <drm/drm_modes.h>
12 #include <drm/drm_panel.h>
13
14 struct tm5p5_nt35596 {
15 struct drm_panel panel;
16 struct mipi_dsi_device *dsi;
17 struct regulator_bulk_data supplies[2];
18 struct gpio_desc *reset_gpio;
19 bool prepared;
20 };
21
to_tm5p5_nt35596(struct drm_panel * panel)22 static inline struct tm5p5_nt35596 *to_tm5p5_nt35596(struct drm_panel *panel)
23 {
24 return container_of(panel, struct tm5p5_nt35596, panel);
25 }
26
tm5p5_nt35596_reset(struct tm5p5_nt35596 * ctx)27 static void tm5p5_nt35596_reset(struct tm5p5_nt35596 *ctx)
28 {
29 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
30 usleep_range(1000, 2000);
31 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
32 usleep_range(1000, 2000);
33 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
34 usleep_range(15000, 16000);
35 }
36
tm5p5_nt35596_on(struct tm5p5_nt35596 * ctx)37 static int tm5p5_nt35596_on(struct tm5p5_nt35596 *ctx)
38 {
39 struct mipi_dsi_device *dsi = ctx->dsi;
40
41 mipi_dsi_generic_write_seq(dsi, 0xff, 0x05);
42 mipi_dsi_generic_write_seq(dsi, 0xfb, 0x01);
43 mipi_dsi_generic_write_seq(dsi, 0xc5, 0x31);
44 mipi_dsi_generic_write_seq(dsi, 0xff, 0x04);
45 mipi_dsi_generic_write_seq(dsi, 0x01, 0x84);
46 mipi_dsi_generic_write_seq(dsi, 0x05, 0x25);
47 mipi_dsi_generic_write_seq(dsi, 0x06, 0x01);
48 mipi_dsi_generic_write_seq(dsi, 0x07, 0x20);
49 mipi_dsi_generic_write_seq(dsi, 0x08, 0x06);
50 mipi_dsi_generic_write_seq(dsi, 0x09, 0x08);
51 mipi_dsi_generic_write_seq(dsi, 0x0a, 0x10);
52 mipi_dsi_generic_write_seq(dsi, 0x0b, 0x10);
53 mipi_dsi_generic_write_seq(dsi, 0x0c, 0x10);
54 mipi_dsi_generic_write_seq(dsi, 0x0d, 0x14);
55 mipi_dsi_generic_write_seq(dsi, 0x0e, 0x14);
56 mipi_dsi_generic_write_seq(dsi, 0x0f, 0x14);
57 mipi_dsi_generic_write_seq(dsi, 0x10, 0x14);
58 mipi_dsi_generic_write_seq(dsi, 0x11, 0x14);
59 mipi_dsi_generic_write_seq(dsi, 0x12, 0x14);
60 mipi_dsi_generic_write_seq(dsi, 0x17, 0xf3);
61 mipi_dsi_generic_write_seq(dsi, 0x18, 0xc0);
62 mipi_dsi_generic_write_seq(dsi, 0x19, 0xc0);
63 mipi_dsi_generic_write_seq(dsi, 0x1a, 0xc0);
64 mipi_dsi_generic_write_seq(dsi, 0x1b, 0xb3);
65 mipi_dsi_generic_write_seq(dsi, 0x1c, 0xb3);
66 mipi_dsi_generic_write_seq(dsi, 0x1d, 0xb3);
67 mipi_dsi_generic_write_seq(dsi, 0x1e, 0xb3);
68 mipi_dsi_generic_write_seq(dsi, 0x1f, 0xb3);
69 mipi_dsi_generic_write_seq(dsi, 0x20, 0xb3);
70 mipi_dsi_generic_write_seq(dsi, 0xfb, 0x01);
71 mipi_dsi_generic_write_seq(dsi, 0xff, 0x00);
72 mipi_dsi_generic_write_seq(dsi, 0xfb, 0x01);
73 mipi_dsi_generic_write_seq(dsi, 0x35, 0x01);
74 mipi_dsi_generic_write_seq(dsi, 0xd3, 0x06);
75 mipi_dsi_generic_write_seq(dsi, 0xd4, 0x04);
76 mipi_dsi_generic_write_seq(dsi, 0x5e, 0x0d);
77 mipi_dsi_generic_write_seq(dsi, 0x11, 0x00);
78 msleep(100);
79 mipi_dsi_generic_write_seq(dsi, 0x29, 0x00);
80 mipi_dsi_generic_write_seq(dsi, 0x53, 0x24);
81
82 return 0;
83 }
84
tm5p5_nt35596_off(struct tm5p5_nt35596 * ctx)85 static int tm5p5_nt35596_off(struct tm5p5_nt35596 *ctx)
86 {
87 struct mipi_dsi_device *dsi = ctx->dsi;
88 struct device *dev = &dsi->dev;
89 int ret;
90
91 ret = mipi_dsi_dcs_set_display_off(dsi);
92 if (ret < 0) {
93 dev_err(dev, "Failed to set display off: %d\n", ret);
94 return ret;
95 }
96 msleep(60);
97
98 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
99 if (ret < 0) {
100 dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
101 return ret;
102 }
103
104 mipi_dsi_dcs_write_seq(dsi, 0x4f, 0x01);
105
106 return 0;
107 }
108
tm5p5_nt35596_prepare(struct drm_panel * panel)109 static int tm5p5_nt35596_prepare(struct drm_panel *panel)
110 {
111 struct tm5p5_nt35596 *ctx = to_tm5p5_nt35596(panel);
112 struct device *dev = &ctx->dsi->dev;
113 int ret;
114
115 if (ctx->prepared)
116 return 0;
117
118 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
119 if (ret < 0) {
120 dev_err(dev, "Failed to enable regulators: %d\n", ret);
121 return ret;
122 }
123
124 tm5p5_nt35596_reset(ctx);
125
126 ret = tm5p5_nt35596_on(ctx);
127 if (ret < 0) {
128 dev_err(dev, "Failed to initialize panel: %d\n", ret);
129 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
130 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies),
131 ctx->supplies);
132 return ret;
133 }
134
135 ctx->prepared = true;
136 return 0;
137 }
138
tm5p5_nt35596_unprepare(struct drm_panel * panel)139 static int tm5p5_nt35596_unprepare(struct drm_panel *panel)
140 {
141 struct tm5p5_nt35596 *ctx = to_tm5p5_nt35596(panel);
142 struct device *dev = &ctx->dsi->dev;
143 int ret;
144
145 if (!ctx->prepared)
146 return 0;
147
148 ret = tm5p5_nt35596_off(ctx);
149 if (ret < 0)
150 dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
151
152 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
153 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies),
154 ctx->supplies);
155
156 ctx->prepared = false;
157 return 0;
158 }
159
160 static const struct drm_display_mode tm5p5_nt35596_mode = {
161 .clock = (1080 + 100 + 8 + 16) * (1920 + 4 + 2 + 4) * 60 / 1000,
162 .hdisplay = 1080,
163 .hsync_start = 1080 + 100,
164 .hsync_end = 1080 + 100 + 8,
165 .htotal = 1080 + 100 + 8 + 16,
166 .vdisplay = 1920,
167 .vsync_start = 1920 + 4,
168 .vsync_end = 1920 + 4 + 2,
169 .vtotal = 1920 + 4 + 2 + 4,
170 .width_mm = 68,
171 .height_mm = 121,
172 };
173
tm5p5_nt35596_get_modes(struct drm_panel * panel,struct drm_connector * connector)174 static int tm5p5_nt35596_get_modes(struct drm_panel *panel,
175 struct drm_connector *connector)
176 {
177 struct drm_display_mode *mode;
178
179 mode = drm_mode_duplicate(connector->dev, &tm5p5_nt35596_mode);
180 if (!mode)
181 return -ENOMEM;
182
183 drm_mode_set_name(mode);
184
185 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
186 connector->display_info.width_mm = mode->width_mm;
187 connector->display_info.height_mm = mode->height_mm;
188 drm_mode_probed_add(connector, mode);
189
190 return 1;
191 }
192
193 static const struct drm_panel_funcs tm5p5_nt35596_panel_funcs = {
194 .prepare = tm5p5_nt35596_prepare,
195 .unprepare = tm5p5_nt35596_unprepare,
196 .get_modes = tm5p5_nt35596_get_modes,
197 };
198
tm5p5_nt35596_bl_update_status(struct backlight_device * bl)199 static int tm5p5_nt35596_bl_update_status(struct backlight_device *bl)
200 {
201 struct mipi_dsi_device *dsi = bl_get_data(bl);
202 u16 brightness = backlight_get_brightness(bl);
203 int ret;
204
205 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
206
207 ret = mipi_dsi_dcs_set_display_brightness(dsi, brightness);
208 if (ret < 0)
209 return ret;
210
211 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
212
213 return 0;
214 }
215
tm5p5_nt35596_bl_get_brightness(struct backlight_device * bl)216 static int tm5p5_nt35596_bl_get_brightness(struct backlight_device *bl)
217 {
218 struct mipi_dsi_device *dsi = bl_get_data(bl);
219 u16 brightness = bl->props.brightness;
220 int ret;
221
222 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
223
224 ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness);
225 if (ret < 0)
226 return ret;
227
228 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
229
230 return brightness & 0xff;
231 }
232
233 static const struct backlight_ops tm5p5_nt35596_bl_ops = {
234 .update_status = tm5p5_nt35596_bl_update_status,
235 .get_brightness = tm5p5_nt35596_bl_get_brightness,
236 };
237
238 static struct backlight_device *
tm5p5_nt35596_create_backlight(struct mipi_dsi_device * dsi)239 tm5p5_nt35596_create_backlight(struct mipi_dsi_device *dsi)
240 {
241 struct device *dev = &dsi->dev;
242 const struct backlight_properties props = {
243 .type = BACKLIGHT_RAW,
244 .brightness = 255,
245 .max_brightness = 255,
246 };
247
248 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
249 &tm5p5_nt35596_bl_ops, &props);
250 }
251
tm5p5_nt35596_probe(struct mipi_dsi_device * dsi)252 static int tm5p5_nt35596_probe(struct mipi_dsi_device *dsi)
253 {
254 struct device *dev = &dsi->dev;
255 struct tm5p5_nt35596 *ctx;
256 int ret;
257
258 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
259 if (!ctx)
260 return -ENOMEM;
261
262 ctx->supplies[0].supply = "vdd";
263 ctx->supplies[1].supply = "vddio";
264 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
265 ctx->supplies);
266 if (ret < 0) {
267 dev_err(dev, "Failed to get regulators: %d\n", ret);
268 return ret;
269 }
270
271 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
272 if (IS_ERR(ctx->reset_gpio)) {
273 ret = PTR_ERR(ctx->reset_gpio);
274 dev_err(dev, "Failed to get reset-gpios: %d\n", ret);
275 return ret;
276 }
277
278 ctx->dsi = dsi;
279 mipi_dsi_set_drvdata(dsi, ctx);
280
281 dsi->lanes = 4;
282 dsi->format = MIPI_DSI_FMT_RGB888;
283 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
284 MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_NO_EOT_PACKET |
285 MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM;
286
287 drm_panel_init(&ctx->panel, dev, &tm5p5_nt35596_panel_funcs,
288 DRM_MODE_CONNECTOR_DSI);
289
290 ctx->panel.backlight = tm5p5_nt35596_create_backlight(dsi);
291 if (IS_ERR(ctx->panel.backlight)) {
292 ret = PTR_ERR(ctx->panel.backlight);
293 dev_err(dev, "Failed to create backlight: %d\n", ret);
294 return ret;
295 }
296
297 drm_panel_add(&ctx->panel);
298
299 ret = mipi_dsi_attach(dsi);
300 if (ret < 0) {
301 dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
302 return ret;
303 }
304
305 return 0;
306 }
307
tm5p5_nt35596_remove(struct mipi_dsi_device * dsi)308 static void tm5p5_nt35596_remove(struct mipi_dsi_device *dsi)
309 {
310 struct tm5p5_nt35596 *ctx = mipi_dsi_get_drvdata(dsi);
311 int ret;
312
313 ret = mipi_dsi_detach(dsi);
314 if (ret < 0)
315 dev_err(&dsi->dev,
316 "Failed to detach from DSI host: %d\n", ret);
317
318 drm_panel_remove(&ctx->panel);
319 }
320
321 static const struct of_device_id tm5p5_nt35596_of_match[] = {
322 { .compatible = "asus,z00t-tm5p5-n35596" },
323 { /* sentinel */ }
324 };
325 MODULE_DEVICE_TABLE(of, tm5p5_nt35596_of_match);
326
327 static struct mipi_dsi_driver tm5p5_nt35596_driver = {
328 .probe = tm5p5_nt35596_probe,
329 .remove = tm5p5_nt35596_remove,
330 .driver = {
331 .name = "panel-tm5p5-nt35596",
332 .of_match_table = tm5p5_nt35596_of_match,
333 },
334 };
335 module_mipi_dsi_driver(tm5p5_nt35596_driver);
336
337 MODULE_AUTHOR("Konrad Dybcio <konradybcio@gmail.com>");
338 MODULE_DESCRIPTION("DRM driver for tm5p5 nt35596 1080p video mode dsi panel");
339 MODULE_LICENSE("GPL v2");
340