1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Red Hat
4 * Copyright (C) 2015 Sony Mobile Communications Inc.
5 * Author: Werner Johansson <werner.johansson@sonymobile.com>
7 * Based on AUO panel driver by Rob Clark <robdclark@gmail.com>
10 #include <linux/backlight.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
14 #include <linux/regulator/consumer.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_mipi_dsi.h>
19 #include <drm/drm_panel.h>
21 #include <video/mipi_display.h>
23 struct sharp_nt_panel {
24 struct drm_panel base;
25 struct mipi_dsi_device *dsi;
27 struct backlight_device *backlight;
28 struct regulator *supply;
29 struct gpio_desc *reset_gpio;
34 const struct drm_display_mode *mode;
37 static inline struct sharp_nt_panel *to_sharp_nt_panel(struct drm_panel *panel)
39 return container_of(panel, struct sharp_nt_panel, base);
42 static int sharp_nt_panel_init(struct sharp_nt_panel *sharp_nt)
44 struct mipi_dsi_device *dsi = sharp_nt->dsi;
47 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
49 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
55 /* Novatek two-lane operation */
56 ret = mipi_dsi_dcs_write(dsi, 0xae, (u8[]){ 0x03 }, 1);
60 /* Set both MCU and RGB I/F to 24bpp */
61 ret = mipi_dsi_dcs_set_pixel_format(dsi, MIPI_DCS_PIXEL_FMT_24BIT |
62 (MIPI_DCS_PIXEL_FMT_24BIT << 4));
69 static int sharp_nt_panel_on(struct sharp_nt_panel *sharp_nt)
71 struct mipi_dsi_device *dsi = sharp_nt->dsi;
74 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
76 ret = mipi_dsi_dcs_set_display_on(dsi);
83 static int sharp_nt_panel_off(struct sharp_nt_panel *sharp_nt)
85 struct mipi_dsi_device *dsi = sharp_nt->dsi;
88 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
90 ret = mipi_dsi_dcs_set_display_off(dsi);
94 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
102 static int sharp_nt_panel_disable(struct drm_panel *panel)
104 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
106 if (!sharp_nt->enabled)
109 backlight_disable(sharp_nt->backlight);
111 sharp_nt->enabled = false;
116 static int sharp_nt_panel_unprepare(struct drm_panel *panel)
118 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
121 if (!sharp_nt->prepared)
124 ret = sharp_nt_panel_off(sharp_nt);
126 dev_err(panel->dev, "failed to set panel off: %d\n", ret);
130 regulator_disable(sharp_nt->supply);
131 if (sharp_nt->reset_gpio)
132 gpiod_set_value(sharp_nt->reset_gpio, 0);
134 sharp_nt->prepared = false;
139 static int sharp_nt_panel_prepare(struct drm_panel *panel)
141 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
144 if (sharp_nt->prepared)
147 ret = regulator_enable(sharp_nt->supply);
153 if (sharp_nt->reset_gpio) {
154 gpiod_set_value(sharp_nt->reset_gpio, 1);
156 gpiod_set_value(sharp_nt->reset_gpio, 0);
158 gpiod_set_value(sharp_nt->reset_gpio, 1);
162 ret = sharp_nt_panel_init(sharp_nt);
164 dev_err(panel->dev, "failed to init panel: %d\n", ret);
168 ret = sharp_nt_panel_on(sharp_nt);
170 dev_err(panel->dev, "failed to set panel on: %d\n", ret);
174 sharp_nt->prepared = true;
179 regulator_disable(sharp_nt->supply);
180 if (sharp_nt->reset_gpio)
181 gpiod_set_value(sharp_nt->reset_gpio, 0);
185 static int sharp_nt_panel_enable(struct drm_panel *panel)
187 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
189 if (sharp_nt->enabled)
192 backlight_enable(sharp_nt->backlight);
194 sharp_nt->enabled = true;
199 static const struct drm_display_mode default_mode = {
202 .hsync_start = 540 + 48,
203 .hsync_end = 540 + 48 + 80,
204 .htotal = 540 + 48 + 80 + 32,
206 .vsync_start = 960 + 3,
207 .vsync_end = 960 + 3 + 15,
208 .vtotal = 960 + 3 + 15 + 1,
212 static int sharp_nt_panel_get_modes(struct drm_panel *panel)
214 struct drm_display_mode *mode;
216 mode = drm_mode_duplicate(panel->drm, &default_mode);
218 dev_err(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
219 default_mode.hdisplay, default_mode.vdisplay,
220 default_mode.vrefresh);
224 drm_mode_set_name(mode);
226 drm_mode_probed_add(panel->connector, mode);
228 panel->connector->display_info.width_mm = 54;
229 panel->connector->display_info.height_mm = 95;
234 static const struct drm_panel_funcs sharp_nt_panel_funcs = {
235 .disable = sharp_nt_panel_disable,
236 .unprepare = sharp_nt_panel_unprepare,
237 .prepare = sharp_nt_panel_prepare,
238 .enable = sharp_nt_panel_enable,
239 .get_modes = sharp_nt_panel_get_modes,
242 static int sharp_nt_panel_add(struct sharp_nt_panel *sharp_nt)
244 struct device *dev = &sharp_nt->dsi->dev;
246 sharp_nt->mode = &default_mode;
248 sharp_nt->supply = devm_regulator_get(dev, "avdd");
249 if (IS_ERR(sharp_nt->supply))
250 return PTR_ERR(sharp_nt->supply);
252 sharp_nt->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
253 if (IS_ERR(sharp_nt->reset_gpio)) {
254 dev_err(dev, "cannot get reset-gpios %ld\n",
255 PTR_ERR(sharp_nt->reset_gpio));
256 sharp_nt->reset_gpio = NULL;
258 gpiod_set_value(sharp_nt->reset_gpio, 0);
261 sharp_nt->backlight = devm_of_find_backlight(dev);
263 if (IS_ERR(sharp_nt->backlight))
264 return PTR_ERR(sharp_nt->backlight);
266 drm_panel_init(&sharp_nt->base);
267 sharp_nt->base.funcs = &sharp_nt_panel_funcs;
268 sharp_nt->base.dev = &sharp_nt->dsi->dev;
270 return drm_panel_add(&sharp_nt->base);
273 static void sharp_nt_panel_del(struct sharp_nt_panel *sharp_nt)
275 if (sharp_nt->base.dev)
276 drm_panel_remove(&sharp_nt->base);
279 static int sharp_nt_panel_probe(struct mipi_dsi_device *dsi)
281 struct sharp_nt_panel *sharp_nt;
285 dsi->format = MIPI_DSI_FMT_RGB888;
286 dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
287 MIPI_DSI_MODE_VIDEO_HSE |
288 MIPI_DSI_CLOCK_NON_CONTINUOUS |
289 MIPI_DSI_MODE_EOT_PACKET;
291 sharp_nt = devm_kzalloc(&dsi->dev, sizeof(*sharp_nt), GFP_KERNEL);
295 mipi_dsi_set_drvdata(dsi, sharp_nt);
299 ret = sharp_nt_panel_add(sharp_nt);
303 return mipi_dsi_attach(dsi);
306 static int sharp_nt_panel_remove(struct mipi_dsi_device *dsi)
308 struct sharp_nt_panel *sharp_nt = mipi_dsi_get_drvdata(dsi);
311 ret = sharp_nt_panel_disable(&sharp_nt->base);
313 dev_err(&dsi->dev, "failed to disable panel: %d\n", ret);
315 ret = mipi_dsi_detach(dsi);
317 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
319 sharp_nt_panel_del(sharp_nt);
324 static void sharp_nt_panel_shutdown(struct mipi_dsi_device *dsi)
326 struct sharp_nt_panel *sharp_nt = mipi_dsi_get_drvdata(dsi);
328 sharp_nt_panel_disable(&sharp_nt->base);
331 static const struct of_device_id sharp_nt_of_match[] = {
332 { .compatible = "sharp,ls043t1le01-qhd", },
335 MODULE_DEVICE_TABLE(of, sharp_nt_of_match);
337 static struct mipi_dsi_driver sharp_nt_panel_driver = {
339 .name = "panel-sharp-ls043t1le01-qhd",
340 .of_match_table = sharp_nt_of_match,
342 .probe = sharp_nt_panel_probe,
343 .remove = sharp_nt_panel_remove,
344 .shutdown = sharp_nt_panel_shutdown,
346 module_mipi_dsi_driver(sharp_nt_panel_driver);
348 MODULE_AUTHOR("Werner Johansson <werner.johansson@sonymobile.com>");
349 MODULE_DESCRIPTION("Sharp LS043T1LE01 NT35565-based qHD (540x960) video mode panel driver");
350 MODULE_LICENSE("GPL v2");