blob: 627dfcf8adb4db6f8c8dc8065d29356f3c91673d [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Werner Johansson086ceb62015-10-30 17:38:28 -07002/*
3 * Copyright (C) 2015 Red Hat
4 * Copyright (C) 2015 Sony Mobile Communications Inc.
5 * Author: Werner Johansson <werner.johansson@sonymobile.com>
6 *
7 * Based on AUO panel driver by Rob Clark <robdclark@gmail.com>
Werner Johansson086ceb62015-10-30 17:38:28 -07008 */
9
Sam Ravnborgcb23eae2019-05-26 20:05:32 +020010#include <linux/delay.h>
Werner Johansson086ceb62015-10-30 17:38:28 -070011#include <linux/module.h>
12#include <linux/of.h>
13#include <linux/regulator/consumer.h>
14
Sam Ravnborgcb23eae2019-05-26 20:05:32 +020015#include <video/mipi_display.h>
16
Werner Johansson086ceb62015-10-30 17:38:28 -070017#include <drm/drm_crtc.h>
Sam Ravnborgcb23eae2019-05-26 20:05:32 +020018#include <drm/drm_device.h>
Werner Johansson086ceb62015-10-30 17:38:28 -070019#include <drm/drm_mipi_dsi.h>
20#include <drm/drm_panel.h>
21
Werner Johansson086ceb62015-10-30 17:38:28 -070022/*
23 * When power is turned off to this panel a minimum off time of 500ms has to be
24 * observed before powering back on as there's no external reset pin. Keep
25 * track of earliest wakeup time and delay subsequent prepare call accordingly
26 */
27#define MIN_POFF_MS (500)
28
29struct wuxga_nt_panel {
30 struct drm_panel base;
31 struct mipi_dsi_device *dsi;
32
Werner Johansson086ceb62015-10-30 17:38:28 -070033 struct regulator *supply;
34
35 bool prepared;
36 bool enabled;
37
38 ktime_t earliest_wake;
39
40 const struct drm_display_mode *mode;
41};
42
43static inline struct wuxga_nt_panel *to_wuxga_nt_panel(struct drm_panel *panel)
44{
45 return container_of(panel, struct wuxga_nt_panel, base);
46}
47
48static int wuxga_nt_panel_on(struct wuxga_nt_panel *wuxga_nt)
49{
Sean Paul4ac51112018-01-17 16:37:43 -050050 return mipi_dsi_turn_on_peripheral(wuxga_nt->dsi);
Werner Johansson086ceb62015-10-30 17:38:28 -070051}
52
53static int wuxga_nt_panel_disable(struct drm_panel *panel)
54{
55 struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
Sean Paul4ac51112018-01-17 16:37:43 -050056 int mipi_ret, bl_ret = 0;
Werner Johansson086ceb62015-10-30 17:38:28 -070057
58 if (!wuxga_nt->enabled)
59 return 0;
60
Sean Paul4ac51112018-01-17 16:37:43 -050061 mipi_ret = mipi_dsi_shutdown_peripheral(wuxga_nt->dsi);
Werner Johansson086ceb62015-10-30 17:38:28 -070062
Werner Johansson086ceb62015-10-30 17:38:28 -070063 wuxga_nt->enabled = false;
64
Sean Paul4ac51112018-01-17 16:37:43 -050065 return mipi_ret ? mipi_ret : bl_ret;
Werner Johansson086ceb62015-10-30 17:38:28 -070066}
67
68static int wuxga_nt_panel_unprepare(struct drm_panel *panel)
69{
70 struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
71
72 if (!wuxga_nt->prepared)
73 return 0;
74
75 regulator_disable(wuxga_nt->supply);
76 wuxga_nt->earliest_wake = ktime_add_ms(ktime_get_real(), MIN_POFF_MS);
77 wuxga_nt->prepared = false;
78
79 return 0;
80}
81
82static int wuxga_nt_panel_prepare(struct drm_panel *panel)
83{
84 struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
85 int ret;
86 s64 enablewait;
87
88 if (wuxga_nt->prepared)
89 return 0;
90
91 /*
92 * If the user re-enabled the panel before the required off-time then
93 * we need to wait the remaining period before re-enabling regulator
94 */
95 enablewait = ktime_ms_delta(wuxga_nt->earliest_wake, ktime_get_real());
96
97 /* Sanity check, this should never happen */
98 if (enablewait > MIN_POFF_MS)
99 enablewait = MIN_POFF_MS;
100
101 if (enablewait > 0)
102 msleep(enablewait);
103
104 ret = regulator_enable(wuxga_nt->supply);
105 if (ret < 0)
106 return ret;
107
108 /*
109 * A minimum delay of 250ms is required after power-up until commands
110 * can be sent
111 */
112 msleep(250);
113
114 ret = wuxga_nt_panel_on(wuxga_nt);
115 if (ret < 0) {
116 dev_err(panel->dev, "failed to set panel on: %d\n", ret);
117 goto poweroff;
118 }
119
120 wuxga_nt->prepared = true;
121
122 return 0;
123
124poweroff:
125 regulator_disable(wuxga_nt->supply);
126
127 return ret;
128}
129
130static int wuxga_nt_panel_enable(struct drm_panel *panel)
131{
132 struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel);
133
134 if (wuxga_nt->enabled)
135 return 0;
136
Werner Johansson086ceb62015-10-30 17:38:28 -0700137 wuxga_nt->enabled = true;
138
139 return 0;
140}
141
142static const struct drm_display_mode default_mode = {
143 .clock = 164402,
144 .hdisplay = 1920,
145 .hsync_start = 1920 + 152,
146 .hsync_end = 1920 + 152 + 52,
147 .htotal = 1920 + 152 + 52 + 20,
148 .vdisplay = 1200,
149 .vsync_start = 1200 + 24,
150 .vsync_end = 1200 + 24 + 6,
151 .vtotal = 1200 + 24 + 6 + 48,
Werner Johansson086ceb62015-10-30 17:38:28 -0700152};
153
Sam Ravnborg0ce8ddd2019-12-07 15:03:33 +0100154static int wuxga_nt_panel_get_modes(struct drm_panel *panel,
155 struct drm_connector *connector)
Werner Johansson086ceb62015-10-30 17:38:28 -0700156{
157 struct drm_display_mode *mode;
158
Sam Ravnborgaa6c4362019-12-07 15:03:35 +0100159 mode = drm_mode_duplicate(connector->dev, &default_mode);
Werner Johansson086ceb62015-10-30 17:38:28 -0700160 if (!mode) {
Sam Ravnborgaa6c4362019-12-07 15:03:35 +0100161 dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
162 default_mode.hdisplay, default_mode.vdisplay,
Ville Syrjälä04256622020-04-28 20:19:27 +0300163 drm_mode_vrefresh(&default_mode));
Werner Johansson086ceb62015-10-30 17:38:28 -0700164 return -ENOMEM;
165 }
166
167 drm_mode_set_name(mode);
168
Sam Ravnborg0ce8ddd2019-12-07 15:03:33 +0100169 drm_mode_probed_add(connector, mode);
Werner Johansson086ceb62015-10-30 17:38:28 -0700170
Sam Ravnborg0ce8ddd2019-12-07 15:03:33 +0100171 connector->display_info.width_mm = 217;
172 connector->display_info.height_mm = 136;
Werner Johansson086ceb62015-10-30 17:38:28 -0700173
174 return 1;
175}
176
177static const struct drm_panel_funcs wuxga_nt_panel_funcs = {
178 .disable = wuxga_nt_panel_disable,
179 .unprepare = wuxga_nt_panel_unprepare,
180 .prepare = wuxga_nt_panel_prepare,
181 .enable = wuxga_nt_panel_enable,
182 .get_modes = wuxga_nt_panel_get_modes,
183};
184
185static const struct of_device_id wuxga_nt_of_match[] = {
186 { .compatible = "panasonic,vvx10f034n00", },
187 { }
188};
189MODULE_DEVICE_TABLE(of, wuxga_nt_of_match);
190
191static int wuxga_nt_panel_add(struct wuxga_nt_panel *wuxga_nt)
192{
193 struct device *dev = &wuxga_nt->dsi->dev;
Werner Johansson086ceb62015-10-30 17:38:28 -0700194 int ret;
195
196 wuxga_nt->mode = &default_mode;
197
198 wuxga_nt->supply = devm_regulator_get(dev, "power");
199 if (IS_ERR(wuxga_nt->supply))
200 return PTR_ERR(wuxga_nt->supply);
201
Laurent Pinchart6dbe0c42019-08-23 22:32:43 +0300202 drm_panel_init(&wuxga_nt->base, &wuxga_nt->dsi->dev,
Laurent Pinchart9a2654c2019-09-04 16:28:03 +0300203 &wuxga_nt_panel_funcs, DRM_MODE_CONNECTOR_DSI);
Werner Johansson086ceb62015-10-30 17:38:28 -0700204
Sam Ravnborg581ee322019-12-07 15:03:43 +0100205 ret = drm_panel_of_backlight(&wuxga_nt->base);
206 if (ret)
207 return ret;
Werner Johansson086ceb62015-10-30 17:38:28 -0700208
Sam Ravnborg581ee322019-12-07 15:03:43 +0100209 return drm_panel_add(&wuxga_nt->base);
Werner Johansson086ceb62015-10-30 17:38:28 -0700210}
211
212static void wuxga_nt_panel_del(struct wuxga_nt_panel *wuxga_nt)
213{
214 if (wuxga_nt->base.dev)
215 drm_panel_remove(&wuxga_nt->base);
Werner Johansson086ceb62015-10-30 17:38:28 -0700216}
217
218static int wuxga_nt_panel_probe(struct mipi_dsi_device *dsi)
219{
220 struct wuxga_nt_panel *wuxga_nt;
221 int ret;
222
223 dsi->lanes = 4;
224 dsi->format = MIPI_DSI_FMT_RGB888;
225 dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
226 MIPI_DSI_MODE_VIDEO_HSE |
227 MIPI_DSI_CLOCK_NON_CONTINUOUS |
228 MIPI_DSI_MODE_LPM;
229
230 wuxga_nt = devm_kzalloc(&dsi->dev, sizeof(*wuxga_nt), GFP_KERNEL);
231 if (!wuxga_nt)
232 return -ENOMEM;
233
234 mipi_dsi_set_drvdata(dsi, wuxga_nt);
235
236 wuxga_nt->dsi = dsi;
237
238 ret = wuxga_nt_panel_add(wuxga_nt);
239 if (ret < 0)
240 return ret;
241
242 return mipi_dsi_attach(dsi);
243}
244
245static int wuxga_nt_panel_remove(struct mipi_dsi_device *dsi)
246{
247 struct wuxga_nt_panel *wuxga_nt = mipi_dsi_get_drvdata(dsi);
248 int ret;
249
Sam Ravnborg581ee322019-12-07 15:03:43 +0100250 ret = drm_panel_disable(&wuxga_nt->base);
Werner Johansson086ceb62015-10-30 17:38:28 -0700251 if (ret < 0)
252 dev_err(&dsi->dev, "failed to disable panel: %d\n", ret);
253
254 ret = mipi_dsi_detach(dsi);
255 if (ret < 0)
256 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
257
Werner Johansson086ceb62015-10-30 17:38:28 -0700258 wuxga_nt_panel_del(wuxga_nt);
259
260 return 0;
261}
262
263static void wuxga_nt_panel_shutdown(struct mipi_dsi_device *dsi)
264{
265 struct wuxga_nt_panel *wuxga_nt = mipi_dsi_get_drvdata(dsi);
266
Sam Ravnborg581ee322019-12-07 15:03:43 +0100267 drm_panel_disable(&wuxga_nt->base);
Werner Johansson086ceb62015-10-30 17:38:28 -0700268}
269
270static struct mipi_dsi_driver wuxga_nt_panel_driver = {
271 .driver = {
272 .name = "panel-panasonic-vvx10f034n00",
273 .of_match_table = wuxga_nt_of_match,
274 },
275 .probe = wuxga_nt_panel_probe,
276 .remove = wuxga_nt_panel_remove,
277 .shutdown = wuxga_nt_panel_shutdown,
278};
279module_mipi_dsi_driver(wuxga_nt_panel_driver);
280
281MODULE_AUTHOR("Werner Johansson <werner.johansson@sonymobile.com>");
282MODULE_DESCRIPTION("Panasonic VVX10F034N00 Novatek NT1397-based WUXGA (1920x1200) video mode panel driver");
283MODULE_LICENSE("GPL v2");