1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/backlight.h>
4 #include <linux/delay.h>
5 #include <linux/gpio/consumer.h>
6 #include <linux/module.h>
8 #include <linux/regulator/consumer.h>
10 #include <drm/drm_mipi_dsi.h>
11 #include <drm/drm_modes.h>
12 #include <drm/drm_panel.h>
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
;
22 static inline struct tm5p5_nt35596
*to_tm5p5_nt35596(struct drm_panel
*panel
)
24 return container_of(panel
, struct tm5p5_nt35596
, panel
);
27 #define dsi_generic_write_seq(dsi, seq...) do { \
28 static const u8 d[] = { seq }; \
30 ret = mipi_dsi_generic_write(dsi, d, ARRAY_SIZE(d)); \
35 #define dsi_dcs_write_seq(dsi, seq...) do { \
36 static const u8 d[] = { seq }; \
38 ret = mipi_dsi_dcs_write_buffer(dsi, d, ARRAY_SIZE(d)); \
43 static void tm5p5_nt35596_reset(struct tm5p5_nt35596
*ctx
)
45 gpiod_set_value_cansleep(ctx
->reset_gpio
, 1);
46 usleep_range(1000, 2000);
47 gpiod_set_value_cansleep(ctx
->reset_gpio
, 0);
48 usleep_range(1000, 2000);
49 gpiod_set_value_cansleep(ctx
->reset_gpio
, 1);
50 usleep_range(15000, 16000);
53 static int tm5p5_nt35596_on(struct tm5p5_nt35596
*ctx
)
55 struct mipi_dsi_device
*dsi
= ctx
->dsi
;
57 dsi_generic_write_seq(dsi
, 0xff, 0x05);
58 dsi_generic_write_seq(dsi
, 0xfb, 0x01);
59 dsi_generic_write_seq(dsi
, 0xc5, 0x31);
60 dsi_generic_write_seq(dsi
, 0xff, 0x04);
61 dsi_generic_write_seq(dsi
, 0x01, 0x84);
62 dsi_generic_write_seq(dsi
, 0x05, 0x25);
63 dsi_generic_write_seq(dsi
, 0x06, 0x01);
64 dsi_generic_write_seq(dsi
, 0x07, 0x20);
65 dsi_generic_write_seq(dsi
, 0x08, 0x06);
66 dsi_generic_write_seq(dsi
, 0x09, 0x08);
67 dsi_generic_write_seq(dsi
, 0x0a, 0x10);
68 dsi_generic_write_seq(dsi
, 0x0b, 0x10);
69 dsi_generic_write_seq(dsi
, 0x0c, 0x10);
70 dsi_generic_write_seq(dsi
, 0x0d, 0x14);
71 dsi_generic_write_seq(dsi
, 0x0e, 0x14);
72 dsi_generic_write_seq(dsi
, 0x0f, 0x14);
73 dsi_generic_write_seq(dsi
, 0x10, 0x14);
74 dsi_generic_write_seq(dsi
, 0x11, 0x14);
75 dsi_generic_write_seq(dsi
, 0x12, 0x14);
76 dsi_generic_write_seq(dsi
, 0x17, 0xf3);
77 dsi_generic_write_seq(dsi
, 0x18, 0xc0);
78 dsi_generic_write_seq(dsi
, 0x19, 0xc0);
79 dsi_generic_write_seq(dsi
, 0x1a, 0xc0);
80 dsi_generic_write_seq(dsi
, 0x1b, 0xb3);
81 dsi_generic_write_seq(dsi
, 0x1c, 0xb3);
82 dsi_generic_write_seq(dsi
, 0x1d, 0xb3);
83 dsi_generic_write_seq(dsi
, 0x1e, 0xb3);
84 dsi_generic_write_seq(dsi
, 0x1f, 0xb3);
85 dsi_generic_write_seq(dsi
, 0x20, 0xb3);
86 dsi_generic_write_seq(dsi
, 0xfb, 0x01);
87 dsi_generic_write_seq(dsi
, 0xff, 0x00);
88 dsi_generic_write_seq(dsi
, 0xfb, 0x01);
89 dsi_generic_write_seq(dsi
, 0x35, 0x01);
90 dsi_generic_write_seq(dsi
, 0xd3, 0x06);
91 dsi_generic_write_seq(dsi
, 0xd4, 0x04);
92 dsi_generic_write_seq(dsi
, 0x5e, 0x0d);
93 dsi_generic_write_seq(dsi
, 0x11, 0x00);
95 dsi_generic_write_seq(dsi
, 0x29, 0x00);
96 dsi_generic_write_seq(dsi
, 0x53, 0x24);
101 static int tm5p5_nt35596_off(struct tm5p5_nt35596
*ctx
)
103 struct mipi_dsi_device
*dsi
= ctx
->dsi
;
104 struct device
*dev
= &dsi
->dev
;
107 ret
= mipi_dsi_dcs_set_display_off(dsi
);
109 dev_err(dev
, "Failed to set display off: %d\n", ret
);
114 ret
= mipi_dsi_dcs_enter_sleep_mode(dsi
);
116 dev_err(dev
, "Failed to enter sleep mode: %d\n", ret
);
120 dsi_dcs_write_seq(dsi
, 0x4f, 0x01);
125 static int tm5p5_nt35596_prepare(struct drm_panel
*panel
)
127 struct tm5p5_nt35596
*ctx
= to_tm5p5_nt35596(panel
);
128 struct device
*dev
= &ctx
->dsi
->dev
;
134 ret
= regulator_bulk_enable(ARRAY_SIZE(ctx
->supplies
), ctx
->supplies
);
136 dev_err(dev
, "Failed to enable regulators: %d\n", ret
);
140 tm5p5_nt35596_reset(ctx
);
142 ret
= tm5p5_nt35596_on(ctx
);
144 dev_err(dev
, "Failed to initialize panel: %d\n", ret
);
145 gpiod_set_value_cansleep(ctx
->reset_gpio
, 0);
146 regulator_bulk_disable(ARRAY_SIZE(ctx
->supplies
),
151 ctx
->prepared
= true;
155 static int tm5p5_nt35596_unprepare(struct drm_panel
*panel
)
157 struct tm5p5_nt35596
*ctx
= to_tm5p5_nt35596(panel
);
158 struct device
*dev
= &ctx
->dsi
->dev
;
164 ret
= tm5p5_nt35596_off(ctx
);
166 dev_err(dev
, "Failed to un-initialize panel: %d\n", ret
);
168 gpiod_set_value_cansleep(ctx
->reset_gpio
, 0);
169 regulator_bulk_disable(ARRAY_SIZE(ctx
->supplies
),
172 ctx
->prepared
= false;
176 static const struct drm_display_mode tm5p5_nt35596_mode
= {
177 .clock
= (1080 + 100 + 8 + 16) * (1920 + 4 + 2 + 4) * 60 / 1000,
179 .hsync_start
= 1080 + 100,
180 .hsync_end
= 1080 + 100 + 8,
181 .htotal
= 1080 + 100 + 8 + 16,
183 .vsync_start
= 1920 + 4,
184 .vsync_end
= 1920 + 4 + 2,
185 .vtotal
= 1920 + 4 + 2 + 4,
190 static int tm5p5_nt35596_get_modes(struct drm_panel
*panel
,
191 struct drm_connector
*connector
)
193 struct drm_display_mode
*mode
;
195 mode
= drm_mode_duplicate(connector
->dev
, &tm5p5_nt35596_mode
);
199 drm_mode_set_name(mode
);
201 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
202 connector
->display_info
.width_mm
= mode
->width_mm
;
203 connector
->display_info
.height_mm
= mode
->height_mm
;
204 drm_mode_probed_add(connector
, mode
);
209 static const struct drm_panel_funcs tm5p5_nt35596_panel_funcs
= {
210 .prepare
= tm5p5_nt35596_prepare
,
211 .unprepare
= tm5p5_nt35596_unprepare
,
212 .get_modes
= tm5p5_nt35596_get_modes
,
215 static int tm5p5_nt35596_bl_update_status(struct backlight_device
*bl
)
217 struct mipi_dsi_device
*dsi
= bl_get_data(bl
);
218 u16 brightness
= bl
->props
.brightness
;
221 if (bl
->props
.power
!= FB_BLANK_UNBLANK
||
222 bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
||
223 bl
->props
.state
& (BL_CORE_SUSPENDED
| BL_CORE_FBBLANK
))
226 dsi
->mode_flags
&= ~MIPI_DSI_MODE_LPM
;
228 ret
= mipi_dsi_dcs_set_display_brightness(dsi
, brightness
);
232 dsi
->mode_flags
|= MIPI_DSI_MODE_LPM
;
237 static int tm5p5_nt35596_bl_get_brightness(struct backlight_device
*bl
)
239 struct mipi_dsi_device
*dsi
= bl_get_data(bl
);
240 u16 brightness
= bl
->props
.brightness
;
243 dsi
->mode_flags
&= ~MIPI_DSI_MODE_LPM
;
245 ret
= mipi_dsi_dcs_get_display_brightness(dsi
, &brightness
);
249 dsi
->mode_flags
|= MIPI_DSI_MODE_LPM
;
251 return brightness
& 0xff;
254 static const struct backlight_ops tm5p5_nt35596_bl_ops
= {
255 .update_status
= tm5p5_nt35596_bl_update_status
,
256 .get_brightness
= tm5p5_nt35596_bl_get_brightness
,
259 static struct backlight_device
*
260 tm5p5_nt35596_create_backlight(struct mipi_dsi_device
*dsi
)
262 struct device
*dev
= &dsi
->dev
;
263 const struct backlight_properties props
= {
264 .type
= BACKLIGHT_RAW
,
266 .max_brightness
= 255,
269 return devm_backlight_device_register(dev
, dev_name(dev
), dev
, dsi
,
270 &tm5p5_nt35596_bl_ops
, &props
);
273 static int tm5p5_nt35596_probe(struct mipi_dsi_device
*dsi
)
275 struct device
*dev
= &dsi
->dev
;
276 struct tm5p5_nt35596
*ctx
;
279 ctx
= devm_kzalloc(dev
, sizeof(*ctx
), GFP_KERNEL
);
283 ctx
->supplies
[0].supply
= "vdd";
284 ctx
->supplies
[1].supply
= "vddio";
285 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(ctx
->supplies
),
288 dev_err(dev
, "Failed to get regulators: %d\n", ret
);
292 ctx
->reset_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_LOW
);
293 if (IS_ERR(ctx
->reset_gpio
)) {
294 ret
= PTR_ERR(ctx
->reset_gpio
);
295 dev_err(dev
, "Failed to get reset-gpios: %d\n", ret
);
300 mipi_dsi_set_drvdata(dsi
, ctx
);
303 dsi
->format
= MIPI_DSI_FMT_RGB888
;
304 dsi
->mode_flags
= MIPI_DSI_MODE_VIDEO
| MIPI_DSI_MODE_VIDEO_BURST
|
305 MIPI_DSI_MODE_VIDEO_HSE
| MIPI_DSI_MODE_EOT_PACKET
|
306 MIPI_DSI_CLOCK_NON_CONTINUOUS
| MIPI_DSI_MODE_LPM
;
308 drm_panel_init(&ctx
->panel
, dev
, &tm5p5_nt35596_panel_funcs
,
309 DRM_MODE_CONNECTOR_DSI
);
311 ctx
->panel
.backlight
= tm5p5_nt35596_create_backlight(dsi
);
312 if (IS_ERR(ctx
->panel
.backlight
)) {
313 ret
= PTR_ERR(ctx
->panel
.backlight
);
314 dev_err(dev
, "Failed to create backlight: %d\n", ret
);
318 drm_panel_add(&ctx
->panel
);
320 ret
= mipi_dsi_attach(dsi
);
322 dev_err(dev
, "Failed to attach to DSI host: %d\n", ret
);
329 static int tm5p5_nt35596_remove(struct mipi_dsi_device
*dsi
)
331 struct tm5p5_nt35596
*ctx
= mipi_dsi_get_drvdata(dsi
);
334 ret
= mipi_dsi_detach(dsi
);
337 "Failed to detach from DSI host: %d\n", ret
);
339 drm_panel_remove(&ctx
->panel
);
344 static const struct of_device_id tm5p5_nt35596_of_match
[] = {
345 { .compatible
= "asus,z00t-tm5p5-n35596" },
348 MODULE_DEVICE_TABLE(of
, tm5p5_nt35596_of_match
);
350 static struct mipi_dsi_driver tm5p5_nt35596_driver
= {
351 .probe
= tm5p5_nt35596_probe
,
352 .remove
= tm5p5_nt35596_remove
,
354 .name
= "panel-tm5p5-nt35596",
355 .of_match_table
= tm5p5_nt35596_of_match
,
358 module_mipi_dsi_driver(tm5p5_nt35596_driver
);
360 MODULE_AUTHOR("Konrad Dybcio <konradybcio@gmail.com>");
361 MODULE_DESCRIPTION("DRM driver for tm5p5 nt35596 1080p video mode dsi panel");
362 MODULE_LICENSE("GPL v2");