1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Red Hat
4 * Copyright (C) 2015 Sony Mobile Communications Inc.
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
14 #include <linux/regulator/consumer.h>
16 #include <video/mipi_display.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_device.h>
20 #include <drm/drm_mipi_dsi.h>
21 #include <drm/drm_panel.h>
23 struct sharp_nt_panel {
24 struct drm_panel base;
25 struct mipi_dsi_device *dsi;
27 struct regulator *supply;
28 struct gpio_desc *reset_gpio;
33 static inline struct sharp_nt_panel *to_sharp_nt_panel(struct drm_panel *panel)
35 return container_of(panel, struct sharp_nt_panel, base);
38 static int sharp_nt_panel_init(struct sharp_nt_panel *sharp_nt)
40 struct mipi_dsi_device *dsi = sharp_nt->dsi;
43 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
45 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
51 /* Novatek two-lane operation */
52 ret = mipi_dsi_dcs_write(dsi, 0xae, (u8[]){ 0x03 }, 1);
56 /* Set both MCU and RGB I/F to 24bpp */
57 ret = mipi_dsi_dcs_set_pixel_format(dsi, MIPI_DCS_PIXEL_FMT_24BIT |
58 (MIPI_DCS_PIXEL_FMT_24BIT << 4));
65 static int sharp_nt_panel_on(struct sharp_nt_panel *sharp_nt)
67 struct mipi_dsi_device *dsi = sharp_nt->dsi;
70 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
72 ret = mipi_dsi_dcs_set_display_on(dsi);
79 static int sharp_nt_panel_off(struct sharp_nt_panel *sharp_nt)
81 struct mipi_dsi_device *dsi = sharp_nt->dsi;
84 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
86 ret = mipi_dsi_dcs_set_display_off(dsi);
90 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
97 static int sharp_nt_panel_unprepare(struct drm_panel *panel)
99 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
102 if (!sharp_nt->prepared)
105 ret = sharp_nt_panel_off(sharp_nt);
107 dev_err(panel->dev, "failed to set panel off: %d\n", ret);
111 regulator_disable(sharp_nt->supply);
112 if (sharp_nt->reset_gpio)
113 gpiod_set_value(sharp_nt->reset_gpio, 0);
115 sharp_nt->prepared = false;
120 static int sharp_nt_panel_prepare(struct drm_panel *panel)
122 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
125 if (sharp_nt->prepared)
128 ret = regulator_enable(sharp_nt->supply);
134 if (sharp_nt->reset_gpio) {
135 gpiod_set_value(sharp_nt->reset_gpio, 1);
137 gpiod_set_value(sharp_nt->reset_gpio, 0);
139 gpiod_set_value(sharp_nt->reset_gpio, 1);
143 ret = sharp_nt_panel_init(sharp_nt);
145 dev_err(panel->dev, "failed to init panel: %d\n", ret);
149 ret = sharp_nt_panel_on(sharp_nt);
151 dev_err(panel->dev, "failed to set panel on: %d\n", ret);
155 sharp_nt->prepared = true;
160 regulator_disable(sharp_nt->supply);
161 if (sharp_nt->reset_gpio)
162 gpiod_set_value(sharp_nt->reset_gpio, 0);
166 static const struct drm_display_mode default_mode = {
167 .clock = (540 + 48 + 32 + 80) * (960 + 3 + 10 + 15) * 60 / 1000,
169 .hsync_start = 540 + 48,
170 .hsync_end = 540 + 48 + 32,
171 .htotal = 540 + 48 + 32 + 80,
173 .vsync_start = 960 + 3,
174 .vsync_end = 960 + 3 + 10,
175 .vtotal = 960 + 3 + 10 + 15,
178 static int sharp_nt_panel_get_modes(struct drm_panel *panel,
179 struct drm_connector *connector)
181 struct drm_display_mode *mode;
183 mode = drm_mode_duplicate(connector->dev, &default_mode);
185 dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
186 default_mode.hdisplay, default_mode.vdisplay,
187 drm_mode_vrefresh(&default_mode));
191 drm_mode_set_name(mode);
193 drm_mode_probed_add(connector, mode);
195 connector->display_info.width_mm = 54;
196 connector->display_info.height_mm = 95;
201 static const struct drm_panel_funcs sharp_nt_panel_funcs = {
202 .unprepare = sharp_nt_panel_unprepare,
203 .prepare = sharp_nt_panel_prepare,
204 .get_modes = sharp_nt_panel_get_modes,
207 static int sharp_nt_panel_add(struct sharp_nt_panel *sharp_nt)
209 struct device *dev = &sharp_nt->dsi->dev;
212 sharp_nt->supply = devm_regulator_get(dev, "avdd");
213 if (IS_ERR(sharp_nt->supply))
214 return PTR_ERR(sharp_nt->supply);
216 sharp_nt->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
217 if (IS_ERR(sharp_nt->reset_gpio)) {
218 dev_err(dev, "cannot get reset-gpios %ld\n",
219 PTR_ERR(sharp_nt->reset_gpio));
220 sharp_nt->reset_gpio = NULL;
222 gpiod_set_value(sharp_nt->reset_gpio, 0);
225 drm_panel_init(&sharp_nt->base, &sharp_nt->dsi->dev,
226 &sharp_nt_panel_funcs, DRM_MODE_CONNECTOR_DSI);
228 ret = drm_panel_of_backlight(&sharp_nt->base);
232 drm_panel_add(&sharp_nt->base);
237 static void sharp_nt_panel_del(struct sharp_nt_panel *sharp_nt)
239 if (sharp_nt->base.dev)
240 drm_panel_remove(&sharp_nt->base);
243 static int sharp_nt_panel_probe(struct mipi_dsi_device *dsi)
245 struct sharp_nt_panel *sharp_nt;
249 dsi->format = MIPI_DSI_FMT_RGB888;
250 dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
251 MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
252 MIPI_DSI_MODE_VIDEO_HSE |
253 MIPI_DSI_CLOCK_NON_CONTINUOUS |
254 MIPI_DSI_MODE_NO_EOT_PACKET;
256 sharp_nt = devm_kzalloc(&dsi->dev, sizeof(*sharp_nt), GFP_KERNEL);
260 mipi_dsi_set_drvdata(dsi, sharp_nt);
264 ret = sharp_nt_panel_add(sharp_nt);
268 ret = mipi_dsi_attach(dsi);
270 sharp_nt_panel_del(sharp_nt);
277 static void sharp_nt_panel_remove(struct mipi_dsi_device *dsi)
279 struct sharp_nt_panel *sharp_nt = mipi_dsi_get_drvdata(dsi);
282 ret = drm_panel_disable(&sharp_nt->base);
284 dev_err(&dsi->dev, "failed to disable panel: %d\n", ret);
286 ret = mipi_dsi_detach(dsi);
288 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
290 sharp_nt_panel_del(sharp_nt);
293 static void sharp_nt_panel_shutdown(struct mipi_dsi_device *dsi)
295 struct sharp_nt_panel *sharp_nt = mipi_dsi_get_drvdata(dsi);
297 drm_panel_disable(&sharp_nt->base);
300 static const struct of_device_id sharp_nt_of_match[] = {
301 { .compatible = "sharp,ls043t1le01-qhd", },
304 MODULE_DEVICE_TABLE(of, sharp_nt_of_match);
306 static struct mipi_dsi_driver sharp_nt_panel_driver = {
308 .name = "panel-sharp-ls043t1le01-qhd",
309 .of_match_table = sharp_nt_of_match,
311 .probe = sharp_nt_panel_probe,
312 .remove = sharp_nt_panel_remove,
313 .shutdown = sharp_nt_panel_shutdown,
315 module_mipi_dsi_driver(sharp_nt_panel_driver);
318 MODULE_DESCRIPTION("Sharp LS043T1LE01 NT35565-based qHD (540x960) video mode panel driver");
319 MODULE_LICENSE("GPL v2");