Commit | Line | Data |
---|---|---|
caab277b | 1 | // SPDX-License-Identifier: GPL-2.0-only |
086ceb6b WJ |
2 | /* |
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> | |
086ceb6b WJ |
8 | */ |
9 | ||
10 | #include <linux/backlight.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/of.h> | |
13 | #include <linux/regulator/consumer.h> | |
14 | ||
15 | #include <drm/drmP.h> | |
16 | #include <drm/drm_crtc.h> | |
17 | #include <drm/drm_mipi_dsi.h> | |
18 | #include <drm/drm_panel.h> | |
19 | ||
20 | #include <video/mipi_display.h> | |
21 | ||
22 | /* | |
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 | ||
29 | struct wuxga_nt_panel { | |
30 | struct drm_panel base; | |
31 | struct mipi_dsi_device *dsi; | |
32 | ||
33 | struct backlight_device *backlight; | |
34 | struct regulator *supply; | |
35 | ||
36 | bool prepared; | |
37 | bool enabled; | |
38 | ||
39 | ktime_t earliest_wake; | |
40 | ||
41 | const struct drm_display_mode *mode; | |
42 | }; | |
43 | ||
44 | static inline struct wuxga_nt_panel *to_wuxga_nt_panel(struct drm_panel *panel) | |
45 | { | |
46 | return container_of(panel, struct wuxga_nt_panel, base); | |
47 | } | |
48 | ||
49 | static int wuxga_nt_panel_on(struct wuxga_nt_panel *wuxga_nt) | |
50 | { | |
4ac51116 | 51 | return mipi_dsi_turn_on_peripheral(wuxga_nt->dsi); |
086ceb6b WJ |
52 | } |
53 | ||
54 | static int wuxga_nt_panel_disable(struct drm_panel *panel) | |
55 | { | |
56 | struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel); | |
4ac51116 | 57 | int mipi_ret, bl_ret = 0; |
086ceb6b WJ |
58 | |
59 | if (!wuxga_nt->enabled) | |
60 | return 0; | |
61 | ||
4ac51116 | 62 | mipi_ret = mipi_dsi_shutdown_peripheral(wuxga_nt->dsi); |
086ceb6b WJ |
63 | |
64 | if (wuxga_nt->backlight) { | |
65 | wuxga_nt->backlight->props.power = FB_BLANK_POWERDOWN; | |
66 | wuxga_nt->backlight->props.state |= BL_CORE_FBBLANK; | |
4ac51116 | 67 | bl_ret = backlight_update_status(wuxga_nt->backlight); |
086ceb6b WJ |
68 | } |
69 | ||
70 | wuxga_nt->enabled = false; | |
71 | ||
4ac51116 | 72 | return mipi_ret ? mipi_ret : bl_ret; |
086ceb6b WJ |
73 | } |
74 | ||
75 | static int wuxga_nt_panel_unprepare(struct drm_panel *panel) | |
76 | { | |
77 | struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel); | |
78 | ||
79 | if (!wuxga_nt->prepared) | |
80 | return 0; | |
81 | ||
82 | regulator_disable(wuxga_nt->supply); | |
83 | wuxga_nt->earliest_wake = ktime_add_ms(ktime_get_real(), MIN_POFF_MS); | |
84 | wuxga_nt->prepared = false; | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
89 | static int wuxga_nt_panel_prepare(struct drm_panel *panel) | |
90 | { | |
91 | struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel); | |
92 | int ret; | |
93 | s64 enablewait; | |
94 | ||
95 | if (wuxga_nt->prepared) | |
96 | return 0; | |
97 | ||
98 | /* | |
99 | * If the user re-enabled the panel before the required off-time then | |
100 | * we need to wait the remaining period before re-enabling regulator | |
101 | */ | |
102 | enablewait = ktime_ms_delta(wuxga_nt->earliest_wake, ktime_get_real()); | |
103 | ||
104 | /* Sanity check, this should never happen */ | |
105 | if (enablewait > MIN_POFF_MS) | |
106 | enablewait = MIN_POFF_MS; | |
107 | ||
108 | if (enablewait > 0) | |
109 | msleep(enablewait); | |
110 | ||
111 | ret = regulator_enable(wuxga_nt->supply); | |
112 | if (ret < 0) | |
113 | return ret; | |
114 | ||
115 | /* | |
116 | * A minimum delay of 250ms is required after power-up until commands | |
117 | * can be sent | |
118 | */ | |
119 | msleep(250); | |
120 | ||
121 | ret = wuxga_nt_panel_on(wuxga_nt); | |
122 | if (ret < 0) { | |
123 | dev_err(panel->dev, "failed to set panel on: %d\n", ret); | |
124 | goto poweroff; | |
125 | } | |
126 | ||
127 | wuxga_nt->prepared = true; | |
128 | ||
129 | return 0; | |
130 | ||
131 | poweroff: | |
132 | regulator_disable(wuxga_nt->supply); | |
133 | ||
134 | return ret; | |
135 | } | |
136 | ||
137 | static int wuxga_nt_panel_enable(struct drm_panel *panel) | |
138 | { | |
139 | struct wuxga_nt_panel *wuxga_nt = to_wuxga_nt_panel(panel); | |
140 | ||
141 | if (wuxga_nt->enabled) | |
142 | return 0; | |
143 | ||
144 | if (wuxga_nt->backlight) { | |
145 | wuxga_nt->backlight->props.power = FB_BLANK_UNBLANK; | |
146 | wuxga_nt->backlight->props.state &= ~BL_CORE_FBBLANK; | |
147 | backlight_update_status(wuxga_nt->backlight); | |
148 | } | |
149 | ||
150 | wuxga_nt->enabled = true; | |
151 | ||
152 | return 0; | |
153 | } | |
154 | ||
155 | static const struct drm_display_mode default_mode = { | |
156 | .clock = 164402, | |
157 | .hdisplay = 1920, | |
158 | .hsync_start = 1920 + 152, | |
159 | .hsync_end = 1920 + 152 + 52, | |
160 | .htotal = 1920 + 152 + 52 + 20, | |
161 | .vdisplay = 1200, | |
162 | .vsync_start = 1200 + 24, | |
163 | .vsync_end = 1200 + 24 + 6, | |
164 | .vtotal = 1200 + 24 + 6 + 48, | |
165 | .vrefresh = 60, | |
166 | }; | |
167 | ||
168 | static int wuxga_nt_panel_get_modes(struct drm_panel *panel) | |
169 | { | |
170 | struct drm_display_mode *mode; | |
171 | ||
172 | mode = drm_mode_duplicate(panel->drm, &default_mode); | |
173 | if (!mode) { | |
174 | dev_err(panel->drm->dev, "failed to add mode %ux%ux@%u\n", | |
175 | default_mode.hdisplay, default_mode.vdisplay, | |
176 | default_mode.vrefresh); | |
177 | return -ENOMEM; | |
178 | } | |
179 | ||
180 | drm_mode_set_name(mode); | |
181 | ||
182 | drm_mode_probed_add(panel->connector, mode); | |
183 | ||
184 | panel->connector->display_info.width_mm = 217; | |
185 | panel->connector->display_info.height_mm = 136; | |
186 | ||
187 | return 1; | |
188 | } | |
189 | ||
190 | static const struct drm_panel_funcs wuxga_nt_panel_funcs = { | |
191 | .disable = wuxga_nt_panel_disable, | |
192 | .unprepare = wuxga_nt_panel_unprepare, | |
193 | .prepare = wuxga_nt_panel_prepare, | |
194 | .enable = wuxga_nt_panel_enable, | |
195 | .get_modes = wuxga_nt_panel_get_modes, | |
196 | }; | |
197 | ||
198 | static const struct of_device_id wuxga_nt_of_match[] = { | |
199 | { .compatible = "panasonic,vvx10f034n00", }, | |
200 | { } | |
201 | }; | |
202 | MODULE_DEVICE_TABLE(of, wuxga_nt_of_match); | |
203 | ||
204 | static int wuxga_nt_panel_add(struct wuxga_nt_panel *wuxga_nt) | |
205 | { | |
206 | struct device *dev = &wuxga_nt->dsi->dev; | |
207 | struct device_node *np; | |
208 | int ret; | |
209 | ||
210 | wuxga_nt->mode = &default_mode; | |
211 | ||
212 | wuxga_nt->supply = devm_regulator_get(dev, "power"); | |
213 | if (IS_ERR(wuxga_nt->supply)) | |
214 | return PTR_ERR(wuxga_nt->supply); | |
215 | ||
216 | np = of_parse_phandle(dev->of_node, "backlight", 0); | |
217 | if (np) { | |
218 | wuxga_nt->backlight = of_find_backlight_by_node(np); | |
219 | of_node_put(np); | |
220 | ||
221 | if (!wuxga_nt->backlight) | |
222 | return -EPROBE_DEFER; | |
223 | } | |
224 | ||
225 | drm_panel_init(&wuxga_nt->base); | |
226 | wuxga_nt->base.funcs = &wuxga_nt_panel_funcs; | |
227 | wuxga_nt->base.dev = &wuxga_nt->dsi->dev; | |
228 | ||
229 | ret = drm_panel_add(&wuxga_nt->base); | |
230 | if (ret < 0) | |
231 | goto put_backlight; | |
232 | ||
233 | return 0; | |
234 | ||
235 | put_backlight: | |
236 | if (wuxga_nt->backlight) | |
237 | put_device(&wuxga_nt->backlight->dev); | |
238 | ||
239 | return ret; | |
240 | } | |
241 | ||
242 | static void wuxga_nt_panel_del(struct wuxga_nt_panel *wuxga_nt) | |
243 | { | |
244 | if (wuxga_nt->base.dev) | |
245 | drm_panel_remove(&wuxga_nt->base); | |
246 | ||
247 | if (wuxga_nt->backlight) | |
248 | put_device(&wuxga_nt->backlight->dev); | |
249 | } | |
250 | ||
251 | static int wuxga_nt_panel_probe(struct mipi_dsi_device *dsi) | |
252 | { | |
253 | struct wuxga_nt_panel *wuxga_nt; | |
254 | int ret; | |
255 | ||
256 | dsi->lanes = 4; | |
257 | dsi->format = MIPI_DSI_FMT_RGB888; | |
258 | dsi->mode_flags = MIPI_DSI_MODE_VIDEO | | |
259 | MIPI_DSI_MODE_VIDEO_HSE | | |
260 | MIPI_DSI_CLOCK_NON_CONTINUOUS | | |
261 | MIPI_DSI_MODE_LPM; | |
262 | ||
263 | wuxga_nt = devm_kzalloc(&dsi->dev, sizeof(*wuxga_nt), GFP_KERNEL); | |
264 | if (!wuxga_nt) | |
265 | return -ENOMEM; | |
266 | ||
267 | mipi_dsi_set_drvdata(dsi, wuxga_nt); | |
268 | ||
269 | wuxga_nt->dsi = dsi; | |
270 | ||
271 | ret = wuxga_nt_panel_add(wuxga_nt); | |
272 | if (ret < 0) | |
273 | return ret; | |
274 | ||
275 | return mipi_dsi_attach(dsi); | |
276 | } | |
277 | ||
278 | static int wuxga_nt_panel_remove(struct mipi_dsi_device *dsi) | |
279 | { | |
280 | struct wuxga_nt_panel *wuxga_nt = mipi_dsi_get_drvdata(dsi); | |
281 | int ret; | |
282 | ||
283 | ret = wuxga_nt_panel_disable(&wuxga_nt->base); | |
284 | if (ret < 0) | |
285 | dev_err(&dsi->dev, "failed to disable panel: %d\n", ret); | |
286 | ||
287 | ret = mipi_dsi_detach(dsi); | |
288 | if (ret < 0) | |
289 | dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret); | |
290 | ||
086ceb6b WJ |
291 | wuxga_nt_panel_del(wuxga_nt); |
292 | ||
293 | return 0; | |
294 | } | |
295 | ||
296 | static void wuxga_nt_panel_shutdown(struct mipi_dsi_device *dsi) | |
297 | { | |
298 | struct wuxga_nt_panel *wuxga_nt = mipi_dsi_get_drvdata(dsi); | |
299 | ||
300 | wuxga_nt_panel_disable(&wuxga_nt->base); | |
301 | } | |
302 | ||
303 | static struct mipi_dsi_driver wuxga_nt_panel_driver = { | |
304 | .driver = { | |
305 | .name = "panel-panasonic-vvx10f034n00", | |
306 | .of_match_table = wuxga_nt_of_match, | |
307 | }, | |
308 | .probe = wuxga_nt_panel_probe, | |
309 | .remove = wuxga_nt_panel_remove, | |
310 | .shutdown = wuxga_nt_panel_shutdown, | |
311 | }; | |
312 | module_mipi_dsi_driver(wuxga_nt_panel_driver); | |
313 | ||
314 | MODULE_AUTHOR("Werner Johansson <werner.johansson@sonymobile.com>"); | |
315 | MODULE_DESCRIPTION("Panasonic VVX10F034N00 Novatek NT1397-based WUXGA (1920x1200) video mode panel driver"); | |
316 | MODULE_LICENSE("GPL v2"); |