drm/msm/hdmi: Enable HPD after HDMI IRQ is set up
[linux/fpc-iii.git] / drivers / gpu / drm / panel / panel-sharp-ls043t1le01.c
blobe5cae0050f52d45a1c0c8fd387efb893d521196f
1 /*
2 * Copyright (C) 2015 Red Hat
3 * Copyright (C) 2015 Sony Mobile Communications Inc.
4 * Author: Werner Johansson <werner.johansson@sonymobile.com>
6 * Based on AUO panel driver by Rob Clark <robdclark@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/backlight.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/regulator/consumer.h>
27 #include <drm/drmP.h>
28 #include <drm/drm_crtc.h>
29 #include <drm/drm_mipi_dsi.h>
30 #include <drm/drm_panel.h>
32 #include <video/mipi_display.h>
34 struct sharp_nt_panel {
35 struct drm_panel base;
36 struct mipi_dsi_device *dsi;
38 struct backlight_device *backlight;
39 struct regulator *supply;
40 struct gpio_desc *reset_gpio;
42 bool prepared;
43 bool enabled;
45 const struct drm_display_mode *mode;
48 static inline struct sharp_nt_panel *to_sharp_nt_panel(struct drm_panel *panel)
50 return container_of(panel, struct sharp_nt_panel, base);
53 static int sharp_nt_panel_init(struct sharp_nt_panel *sharp_nt)
55 struct mipi_dsi_device *dsi = sharp_nt->dsi;
56 int ret;
58 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
60 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
61 if (ret < 0)
62 return ret;
64 msleep(120);
66 /* Novatek two-lane operation */
67 ret = mipi_dsi_dcs_write(dsi, 0xae, (u8[]){ 0x03 }, 1);
68 if (ret < 0)
69 return ret;
71 /* Set both MCU and RGB I/F to 24bpp */
72 ret = mipi_dsi_dcs_set_pixel_format(dsi, MIPI_DCS_PIXEL_FMT_24BIT |
73 (MIPI_DCS_PIXEL_FMT_24BIT << 4));
74 if (ret < 0)
75 return ret;
77 return 0;
80 static int sharp_nt_panel_on(struct sharp_nt_panel *sharp_nt)
82 struct mipi_dsi_device *dsi = sharp_nt->dsi;
83 int ret;
85 dsi->mode_flags |= MIPI_DSI_MODE_LPM;
87 ret = mipi_dsi_dcs_set_display_on(dsi);
88 if (ret < 0)
89 return ret;
91 return 0;
94 static int sharp_nt_panel_off(struct sharp_nt_panel *sharp_nt)
96 struct mipi_dsi_device *dsi = sharp_nt->dsi;
97 int ret;
99 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
101 ret = mipi_dsi_dcs_set_display_off(dsi);
102 if (ret < 0)
103 return ret;
105 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
106 if (ret < 0)
107 return ret;
109 return 0;
113 static int sharp_nt_panel_disable(struct drm_panel *panel)
115 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
117 if (!sharp_nt->enabled)
118 return 0;
120 backlight_disable(sharp_nt->backlight);
122 sharp_nt->enabled = false;
124 return 0;
127 static int sharp_nt_panel_unprepare(struct drm_panel *panel)
129 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
130 int ret;
132 if (!sharp_nt->prepared)
133 return 0;
135 ret = sharp_nt_panel_off(sharp_nt);
136 if (ret < 0) {
137 dev_err(panel->dev, "failed to set panel off: %d\n", ret);
138 return ret;
141 regulator_disable(sharp_nt->supply);
142 if (sharp_nt->reset_gpio)
143 gpiod_set_value(sharp_nt->reset_gpio, 0);
145 sharp_nt->prepared = false;
147 return 0;
150 static int sharp_nt_panel_prepare(struct drm_panel *panel)
152 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
153 int ret;
155 if (sharp_nt->prepared)
156 return 0;
158 ret = regulator_enable(sharp_nt->supply);
159 if (ret < 0)
160 return ret;
162 msleep(20);
164 if (sharp_nt->reset_gpio) {
165 gpiod_set_value(sharp_nt->reset_gpio, 1);
166 msleep(1);
167 gpiod_set_value(sharp_nt->reset_gpio, 0);
168 msleep(1);
169 gpiod_set_value(sharp_nt->reset_gpio, 1);
170 msleep(10);
173 ret = sharp_nt_panel_init(sharp_nt);
174 if (ret < 0) {
175 dev_err(panel->dev, "failed to init panel: %d\n", ret);
176 goto poweroff;
179 ret = sharp_nt_panel_on(sharp_nt);
180 if (ret < 0) {
181 dev_err(panel->dev, "failed to set panel on: %d\n", ret);
182 goto poweroff;
185 sharp_nt->prepared = true;
187 return 0;
189 poweroff:
190 regulator_disable(sharp_nt->supply);
191 if (sharp_nt->reset_gpio)
192 gpiod_set_value(sharp_nt->reset_gpio, 0);
193 return ret;
196 static int sharp_nt_panel_enable(struct drm_panel *panel)
198 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel);
200 if (sharp_nt->enabled)
201 return 0;
203 backlight_enable(sharp_nt->backlight);
205 sharp_nt->enabled = true;
207 return 0;
210 static const struct drm_display_mode default_mode = {
211 .clock = 41118,
212 .hdisplay = 540,
213 .hsync_start = 540 + 48,
214 .hsync_end = 540 + 48 + 80,
215 .htotal = 540 + 48 + 80 + 32,
216 .vdisplay = 960,
217 .vsync_start = 960 + 3,
218 .vsync_end = 960 + 3 + 15,
219 .vtotal = 960 + 3 + 15 + 1,
220 .vrefresh = 60,
223 static int sharp_nt_panel_get_modes(struct drm_panel *panel)
225 struct drm_display_mode *mode;
227 mode = drm_mode_duplicate(panel->drm, &default_mode);
228 if (!mode) {
229 dev_err(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
230 default_mode.hdisplay, default_mode.vdisplay,
231 default_mode.vrefresh);
232 return -ENOMEM;
235 drm_mode_set_name(mode);
237 drm_mode_probed_add(panel->connector, mode);
239 panel->connector->display_info.width_mm = 54;
240 panel->connector->display_info.height_mm = 95;
242 return 1;
245 static const struct drm_panel_funcs sharp_nt_panel_funcs = {
246 .disable = sharp_nt_panel_disable,
247 .unprepare = sharp_nt_panel_unprepare,
248 .prepare = sharp_nt_panel_prepare,
249 .enable = sharp_nt_panel_enable,
250 .get_modes = sharp_nt_panel_get_modes,
253 static int sharp_nt_panel_add(struct sharp_nt_panel *sharp_nt)
255 struct device *dev = &sharp_nt->dsi->dev;
257 sharp_nt->mode = &default_mode;
259 sharp_nt->supply = devm_regulator_get(dev, "avdd");
260 if (IS_ERR(sharp_nt->supply))
261 return PTR_ERR(sharp_nt->supply);
263 sharp_nt->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
264 if (IS_ERR(sharp_nt->reset_gpio)) {
265 dev_err(dev, "cannot get reset-gpios %ld\n",
266 PTR_ERR(sharp_nt->reset_gpio));
267 sharp_nt->reset_gpio = NULL;
268 } else {
269 gpiod_set_value(sharp_nt->reset_gpio, 0);
272 sharp_nt->backlight = devm_of_find_backlight(dev);
274 if (IS_ERR(sharp_nt->backlight))
275 return PTR_ERR(sharp_nt->backlight);
277 drm_panel_init(&sharp_nt->base);
278 sharp_nt->base.funcs = &sharp_nt_panel_funcs;
279 sharp_nt->base.dev = &sharp_nt->dsi->dev;
281 return drm_panel_add(&sharp_nt->base);
284 static void sharp_nt_panel_del(struct sharp_nt_panel *sharp_nt)
286 if (sharp_nt->base.dev)
287 drm_panel_remove(&sharp_nt->base);
290 static int sharp_nt_panel_probe(struct mipi_dsi_device *dsi)
292 struct sharp_nt_panel *sharp_nt;
293 int ret;
295 dsi->lanes = 2;
296 dsi->format = MIPI_DSI_FMT_RGB888;
297 dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
298 MIPI_DSI_MODE_VIDEO_HSE |
299 MIPI_DSI_CLOCK_NON_CONTINUOUS |
300 MIPI_DSI_MODE_EOT_PACKET;
302 sharp_nt = devm_kzalloc(&dsi->dev, sizeof(*sharp_nt), GFP_KERNEL);
303 if (!sharp_nt)
304 return -ENOMEM;
306 mipi_dsi_set_drvdata(dsi, sharp_nt);
308 sharp_nt->dsi = dsi;
310 ret = sharp_nt_panel_add(sharp_nt);
311 if (ret < 0)
312 return ret;
314 return mipi_dsi_attach(dsi);
317 static int sharp_nt_panel_remove(struct mipi_dsi_device *dsi)
319 struct sharp_nt_panel *sharp_nt = mipi_dsi_get_drvdata(dsi);
320 int ret;
322 ret = sharp_nt_panel_disable(&sharp_nt->base);
323 if (ret < 0)
324 dev_err(&dsi->dev, "failed to disable panel: %d\n", ret);
326 ret = mipi_dsi_detach(dsi);
327 if (ret < 0)
328 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
330 sharp_nt_panel_del(sharp_nt);
332 return 0;
335 static void sharp_nt_panel_shutdown(struct mipi_dsi_device *dsi)
337 struct sharp_nt_panel *sharp_nt = mipi_dsi_get_drvdata(dsi);
339 sharp_nt_panel_disable(&sharp_nt->base);
342 static const struct of_device_id sharp_nt_of_match[] = {
343 { .compatible = "sharp,ls043t1le01-qhd", },
346 MODULE_DEVICE_TABLE(of, sharp_nt_of_match);
348 static struct mipi_dsi_driver sharp_nt_panel_driver = {
349 .driver = {
350 .name = "panel-sharp-ls043t1le01-qhd",
351 .of_match_table = sharp_nt_of_match,
353 .probe = sharp_nt_panel_probe,
354 .remove = sharp_nt_panel_remove,
355 .shutdown = sharp_nt_panel_shutdown,
357 module_mipi_dsi_driver(sharp_nt_panel_driver);
359 MODULE_AUTHOR("Werner Johansson <werner.johansson@sonymobile.com>");
360 MODULE_DESCRIPTION("Sharp LS043T1LE01 NT35565-based qHD (540x960) video mode panel driver");
361 MODULE_LICENSE("GPL v2");