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;
31 static inline struct sharp_nt_panel *to_sharp_nt_panel(struct drm_panel *panel)
33 return container_of(panel, struct sharp_nt_panel, base);
36 static int sharp_nt_panel_init(struct sharp_nt_panel *sharp_nt)
38 struct mipi_dsi_device *dsi = sharp_nt->dsi;
41 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
43 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
49 /* Novatek two-lane operation */
50 ret = mipi_dsi_dcs_write(dsi, 0xae, (u8[]){ 0x03 }, 1);
54 /* Set both MCU and RGB I/F to 24bpp */
55 ret = mipi_dsi_dcs_set_pixel_format(dsi, MIPI_DCS_PIXEL_FMT_24BIT |
56 (MIPI_DCS_PIXEL_FMT_24BIT << 4));
63 static int sharp_nt_panel_on(struct sharp_nt_panel *sharp_nt)
65 struct mipi_dsi_device *dsi = sharp_nt->dsi;
68 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
70 ret = mipi_dsi_dcs_set_display_on(dsi);
77 static int sharp_nt_panel_off(struct sharp_nt_panel *sharp_nt)
79 struct mipi_dsi_device *dsi = sharp_nt->dsi;
82 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
84 ret = mipi_dsi_dcs_set_display_off(dsi);
88 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
95 static int sharp_nt_panel_unprepare(struct drm_panel *panel)
97 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
100 ret = sharp_nt_panel_off(sharp_nt);
102 dev_err(panel->dev, "failed to set panel off: %d\n", ret);
106 regulator_disable(sharp_nt->supply);
107 if (sharp_nt->reset_gpio)
108 gpiod_set_value(sharp_nt->reset_gpio, 0);
113 static int sharp_nt_panel_prepare(struct drm_panel *panel)
115 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
118 ret = regulator_enable(sharp_nt->supply);
124 if (sharp_nt->reset_gpio) {
125 gpiod_set_value(sharp_nt->reset_gpio, 1);
127 gpiod_set_value(sharp_nt->reset_gpio, 0);
129 gpiod_set_value(sharp_nt->reset_gpio, 1);
133 ret = sharp_nt_panel_init(sharp_nt);
135 dev_err(panel->dev, "failed to init panel: %d\n", ret);
139 ret = sharp_nt_panel_on(sharp_nt);
141 dev_err(panel->dev, "failed to set panel on: %d\n", ret);
148 regulator_disable(sharp_nt->supply);
149 if (sharp_nt->reset_gpio)
150 gpiod_set_value(sharp_nt->reset_gpio, 0);
154 static const struct drm_display_mode default_mode = {
155 .clock = (540 + 48 + 32 + 80) * (960 + 3 + 10 + 15) * 60 / 1000,
157 .hsync_start = 540 + 48,
158 .hsync_end = 540 + 48 + 32,
159 .htotal = 540 + 48 + 32 + 80,
161 .vsync_start = 960 + 3,
162 .vsync_end = 960 + 3 + 10,
163 .vtotal = 960 + 3 + 10 + 15,
166 static int sharp_nt_panel_get_modes(struct drm_panel *panel,
167 struct drm_connector *connector)
169 struct drm_display_mode *mode;
171 mode = drm_mode_duplicate(connector->dev, &default_mode);
173 dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
174 default_mode.hdisplay, default_mode.vdisplay,
175 drm_mode_vrefresh(&default_mode));
179 drm_mode_set_name(mode);
181 drm_mode_probed_add(connector, mode);
183 connector->display_info.width_mm = 54;
184 connector->display_info.height_mm = 95;
189 static const struct drm_panel_funcs sharp_nt_panel_funcs = {
190 .unprepare = sharp_nt_panel_unprepare,
191 .prepare = sharp_nt_panel_prepare,
192 .get_modes = sharp_nt_panel_get_modes,
195 static int sharp_nt_panel_add(struct sharp_nt_panel *sharp_nt)
197 struct device *dev = &sharp_nt->dsi->dev;
200 sharp_nt->supply = devm_regulator_get(dev, "avdd");
201 if (IS_ERR(sharp_nt->supply))
202 return PTR_ERR(sharp_nt->supply);
204 sharp_nt->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
205 if (IS_ERR(sharp_nt->reset_gpio)) {
206 dev_err(dev, "cannot get reset-gpios %ld\n",
207 PTR_ERR(sharp_nt->reset_gpio));
208 sharp_nt->reset_gpio = NULL;
210 gpiod_set_value(sharp_nt->reset_gpio, 0);
213 drm_panel_init(&sharp_nt->base, &sharp_nt->dsi->dev,
214 &sharp_nt_panel_funcs, DRM_MODE_CONNECTOR_DSI);
216 ret = drm_panel_of_backlight(&sharp_nt->base);
220 drm_panel_add(&sharp_nt->base);
225 static void sharp_nt_panel_del(struct sharp_nt_panel *sharp_nt)
227 if (sharp_nt->base.dev)
228 drm_panel_remove(&sharp_nt->base);
231 static int sharp_nt_panel_probe(struct mipi_dsi_device *dsi)
233 struct sharp_nt_panel *sharp_nt;
237 dsi->format = MIPI_DSI_FMT_RGB888;
238 dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
239 MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
240 MIPI_DSI_MODE_VIDEO_HSE |
241 MIPI_DSI_CLOCK_NON_CONTINUOUS |
242 MIPI_DSI_MODE_NO_EOT_PACKET;
244 sharp_nt = devm_kzalloc(&dsi->dev, sizeof(*sharp_nt), GFP_KERNEL);
248 mipi_dsi_set_drvdata(dsi, sharp_nt);
252 ret = sharp_nt_panel_add(sharp_nt);
256 ret = mipi_dsi_attach(dsi);
258 sharp_nt_panel_del(sharp_nt);
265 static void sharp_nt_panel_remove(struct mipi_dsi_device *dsi)
267 struct sharp_nt_panel *sharp_nt = mipi_dsi_get_drvdata(dsi);
270 ret = mipi_dsi_detach(dsi);
272 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
274 sharp_nt_panel_del(sharp_nt);
277 static const struct of_device_id sharp_nt_of_match[] = {
278 { .compatible = "sharp,ls043t1le01-qhd", },
281 MODULE_DEVICE_TABLE(of, sharp_nt_of_match);
283 static struct mipi_dsi_driver sharp_nt_panel_driver = {
285 .name = "panel-sharp-ls043t1le01-qhd",
286 .of_match_table = sharp_nt_of_match,
288 .probe = sharp_nt_panel_probe,
289 .remove = sharp_nt_panel_remove,
291 module_mipi_dsi_driver(sharp_nt_panel_driver);
294 MODULE_DESCRIPTION("Sharp LS043T1LE01 NT35565-based qHD (540x960) video mode panel driver");
295 MODULE_LICENSE("GPL v2");