Commit | Line | Data |
---|---|---|
e692da4d SH |
1 | /* |
2 | * Freescale i.MX drm driver | |
3 | * | |
4 | * Copyright (C) 2011 Sascha Hauer, Pengutronix | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version 2 | |
9 | * of the License, or (at your option) any later version. | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | */ | |
17b5001b | 16 | #include <linux/component.h> |
e692da4d | 17 | #include <linux/device.h> |
5f2f9115 | 18 | #include <linux/dma-buf.h> |
e7d6231e RK |
19 | #include <linux/fb.h> |
20 | #include <linux/module.h> | |
e692da4d | 21 | #include <linux/platform_device.h> |
5f2f9115 | 22 | #include <linux/reservation.h> |
e692da4d | 23 | #include <drm/drmP.h> |
5f2f9115 LY |
24 | #include <drm/drm_atomic.h> |
25 | #include <drm/drm_atomic_helper.h> | |
e692da4d SH |
26 | #include <drm/drm_fb_helper.h> |
27 | #include <drm/drm_crtc_helper.h> | |
e692da4d SH |
28 | #include <drm/drm_gem_cma_helper.h> |
29 | #include <drm/drm_fb_cma_helper.h> | |
3cb9ae4f | 30 | #include <drm/drm_plane_helper.h> |
6457b971 | 31 | #include <drm/drm_of.h> |
310944d1 | 32 | #include <video/imx-ipu-v3.h> |
e692da4d SH |
33 | |
34 | #include "imx-drm.h" | |
35 | ||
36 | #define MAX_CRTC 4 | |
37 | ||
655b43cc PZ |
38 | struct imx_drm_component { |
39 | struct device_node *of_node; | |
40 | struct list_head list; | |
41 | }; | |
42 | ||
e692da4d SH |
43 | struct imx_drm_device { |
44 | struct drm_device *drm; | |
887eceac | 45 | struct imx_drm_crtc *crtc[MAX_CRTC]; |
d2ab8ad9 | 46 | unsigned int pipes; |
e692da4d | 47 | struct drm_fbdev_cma *fbhelper; |
5f2f9115 | 48 | struct drm_atomic_state *state; |
e692da4d SH |
49 | }; |
50 | ||
51 | struct imx_drm_crtc { | |
52 | struct drm_crtc *crtc; | |
e692da4d | 53 | struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs; |
e692da4d SH |
54 | }; |
55 | ||
c1ff5a7a | 56 | #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION) |
8acba02f RK |
57 | static int legacyfb_depth = 16; |
58 | module_param(legacyfb_depth, int, 0444); | |
c1ff5a7a | 59 | #endif |
8acba02f | 60 | |
d2ab8ad9 | 61 | unsigned int imx_drm_crtc_id(struct imx_drm_crtc *crtc) |
b8d181e4 | 62 | { |
d4b20e4d | 63 | return drm_crtc_index(crtc->crtc); |
b8d181e4 | 64 | } |
9c74360f | 65 | EXPORT_SYMBOL_GPL(imx_drm_crtc_id); |
b8d181e4 | 66 | |
e692da4d SH |
67 | static void imx_drm_driver_lastclose(struct drm_device *drm) |
68 | { | |
69 | struct imx_drm_device *imxdrm = drm->dev_private; | |
70 | ||
3f3a7280 | 71 | drm_fbdev_cma_restore_mode(imxdrm->fbhelper); |
e692da4d SH |
72 | } |
73 | ||
74 | static int imx_drm_driver_unload(struct drm_device *drm) | |
75 | { | |
76 | struct imx_drm_device *imxdrm = drm->dev_private; | |
3e68439b RK |
77 | |
78 | drm_kms_helper_poll_fini(drm); | |
e692da4d | 79 | |
8acba02f RK |
80 | if (imxdrm->fbhelper) |
81 | drm_fbdev_cma_fini(imxdrm->fbhelper); | |
8acba02f | 82 | |
17b5001b RK |
83 | component_unbind_all(drm->dev, drm); |
84 | ||
020a9ea7 | 85 | drm_vblank_cleanup(drm); |
020a9ea7 | 86 | drm_mode_config_cleanup(drm); |
e692da4d | 87 | |
bfe945c8 SG |
88 | platform_set_drvdata(drm->platformdev, NULL); |
89 | ||
e692da4d SH |
90 | return 0; |
91 | } | |
92 | ||
e692da4d SH |
93 | int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc) |
94 | { | |
d4b20e4d | 95 | return drm_crtc_vblank_get(imx_drm_crtc->crtc); |
e692da4d SH |
96 | } |
97 | EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get); | |
98 | ||
99 | void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc) | |
100 | { | |
d4b20e4d | 101 | drm_crtc_vblank_put(imx_drm_crtc->crtc); |
e692da4d SH |
102 | } |
103 | EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put); | |
104 | ||
105 | void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc) | |
106 | { | |
d4b20e4d | 107 | drm_crtc_handle_vblank(imx_drm_crtc->crtc); |
e692da4d SH |
108 | } |
109 | EXPORT_SYMBOL_GPL(imx_drm_handle_vblank); | |
110 | ||
88e72717 | 111 | static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe) |
e692da4d SH |
112 | { |
113 | struct imx_drm_device *imxdrm = drm->dev_private; | |
88e72717 | 114 | struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe]; |
e692da4d SH |
115 | int ret; |
116 | ||
e692da4d SH |
117 | if (!imx_drm_crtc) |
118 | return -EINVAL; | |
119 | ||
120 | if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank) | |
121 | return -ENOSYS; | |
122 | ||
123 | ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank( | |
124 | imx_drm_crtc->crtc); | |
125 | ||
126 | return ret; | |
127 | } | |
128 | ||
88e72717 | 129 | static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe) |
e692da4d SH |
130 | { |
131 | struct imx_drm_device *imxdrm = drm->dev_private; | |
88e72717 | 132 | struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe]; |
e692da4d | 133 | |
e692da4d SH |
134 | if (!imx_drm_crtc) |
135 | return; | |
136 | ||
137 | if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank) | |
138 | return; | |
139 | ||
140 | imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc); | |
141 | } | |
142 | ||
143 | static const struct file_operations imx_drm_driver_fops = { | |
144 | .owner = THIS_MODULE, | |
145 | .open = drm_open, | |
146 | .release = drm_release, | |
147 | .unlocked_ioctl = drm_ioctl, | |
148 | .mmap = drm_gem_cma_mmap, | |
149 | .poll = drm_poll, | |
e692da4d SH |
150 | .read = drm_read, |
151 | .llseek = noop_llseek, | |
152 | }; | |
153 | ||
8a51a33b RK |
154 | void imx_drm_connector_destroy(struct drm_connector *connector) |
155 | { | |
34ea3d38 | 156 | drm_connector_unregister(connector); |
8a51a33b RK |
157 | drm_connector_cleanup(connector); |
158 | } | |
159 | EXPORT_SYMBOL_GPL(imx_drm_connector_destroy); | |
160 | ||
161 | void imx_drm_encoder_destroy(struct drm_encoder *encoder) | |
162 | { | |
163 | drm_encoder_cleanup(encoder); | |
164 | } | |
165 | EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy); | |
166 | ||
3e68439b RK |
167 | static void imx_drm_output_poll_changed(struct drm_device *drm) |
168 | { | |
3e68439b RK |
169 | struct imx_drm_device *imxdrm = drm->dev_private; |
170 | ||
171 | drm_fbdev_cma_hotplug_event(imxdrm->fbhelper); | |
3e68439b RK |
172 | } |
173 | ||
c6c1f9bc LY |
174 | static int imx_drm_atomic_check(struct drm_device *dev, |
175 | struct drm_atomic_state *state) | |
176 | { | |
177 | int ret; | |
178 | ||
179 | ret = drm_atomic_helper_check_modeset(dev, state); | |
180 | if (ret) | |
181 | return ret; | |
182 | ||
183 | ret = drm_atomic_helper_check_planes(dev, state); | |
184 | if (ret) | |
185 | return ret; | |
186 | ||
187 | /* | |
188 | * Check modeset again in case crtc_state->mode_changed is | |
189 | * updated in plane's ->atomic_check callback. | |
190 | */ | |
191 | ret = drm_atomic_helper_check_modeset(dev, state); | |
192 | if (ret) | |
193 | return ret; | |
194 | ||
195 | return ret; | |
196 | } | |
197 | ||
7ae847dd | 198 | static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = { |
1df8b530 | 199 | .fb_create = drm_fb_cma_create, |
3e68439b | 200 | .output_poll_changed = imx_drm_output_poll_changed, |
c6c1f9bc | 201 | .atomic_check = imx_drm_atomic_check, |
5f2f9115 LY |
202 | .atomic_commit = drm_atomic_helper_commit, |
203 | }; | |
204 | ||
205 | static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state) | |
206 | { | |
207 | struct drm_device *dev = state->dev; | |
208 | struct drm_crtc *crtc; | |
209 | struct drm_crtc_state *crtc_state; | |
210 | struct drm_plane_state *plane_state; | |
211 | struct drm_gem_cma_object *cma_obj; | |
212 | struct fence *excl; | |
213 | unsigned shared_count; | |
214 | struct fence **shared; | |
215 | unsigned int i, j; | |
216 | int ret; | |
217 | ||
218 | /* Wait for fences. */ | |
219 | for_each_crtc_in_state(state, crtc, crtc_state, i) { | |
220 | plane_state = crtc->primary->state; | |
221 | if (plane_state->fb) { | |
222 | cma_obj = drm_fb_cma_get_gem_obj(plane_state->fb, 0); | |
223 | if (cma_obj->base.dma_buf) { | |
224 | ret = reservation_object_get_fences_rcu( | |
225 | cma_obj->base.dma_buf->resv, &excl, | |
226 | &shared_count, &shared); | |
227 | if (unlikely(ret)) | |
228 | DRM_ERROR("failed to get fences " | |
229 | "for buffer\n"); | |
230 | ||
231 | if (excl) { | |
232 | fence_wait(excl, false); | |
233 | fence_put(excl); | |
234 | } | |
235 | for (j = 0; j < shared_count; i++) { | |
236 | fence_wait(shared[j], false); | |
237 | fence_put(shared[j]); | |
238 | } | |
239 | } | |
240 | } | |
241 | } | |
242 | ||
243 | drm_atomic_helper_commit_modeset_disables(dev, state); | |
244 | ||
245 | drm_atomic_helper_commit_planes(dev, state, true); | |
246 | ||
247 | drm_atomic_helper_commit_modeset_enables(dev, state); | |
248 | ||
249 | drm_atomic_helper_commit_hw_done(state); | |
250 | ||
251 | drm_atomic_helper_wait_for_vblanks(dev, state); | |
252 | ||
253 | drm_atomic_helper_cleanup_planes(dev, state); | |
254 | } | |
255 | ||
256 | static struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = { | |
257 | .atomic_commit_tail = imx_drm_atomic_commit_tail, | |
1df8b530 RK |
258 | }; |
259 | ||
e692da4d | 260 | /* |
17b5001b RK |
261 | * Main DRM initialisation. This binds, initialises and registers |
262 | * with DRM the subcomponents of the driver. | |
e692da4d SH |
263 | */ |
264 | static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags) | |
265 | { | |
b85f2b5d | 266 | struct imx_drm_device *imxdrm; |
e355e7dd | 267 | struct drm_connector *connector; |
e692da4d SH |
268 | int ret; |
269 | ||
b85f2b5d RK |
270 | imxdrm = devm_kzalloc(drm->dev, sizeof(*imxdrm), GFP_KERNEL); |
271 | if (!imxdrm) | |
272 | return -ENOMEM; | |
273 | ||
e692da4d SH |
274 | imxdrm->drm = drm; |
275 | ||
276 | drm->dev_private = imxdrm; | |
277 | ||
278 | /* | |
279 | * enable drm irq mode. | |
4423843c | 280 | * - with irq_enabled = true, we can use the vblank feature. |
e692da4d SH |
281 | * |
282 | * P.S. note that we wouldn't use drm irq handler but | |
283 | * just specific driver own one instead because | |
284 | * drm framework supports only one irq handler and | |
285 | * drivers can well take care of their interrupts | |
286 | */ | |
4423843c | 287 | drm->irq_enabled = true; |
e692da4d | 288 | |
1df8b530 RK |
289 | /* |
290 | * set max width and height as default value(4096x4096). | |
291 | * this value would be used to check framebuffer size limitation | |
292 | * at drm_mode_addfb(). | |
293 | */ | |
294 | drm->mode_config.min_width = 64; | |
295 | drm->mode_config.min_height = 64; | |
296 | drm->mode_config.max_width = 4096; | |
297 | drm->mode_config.max_height = 4096; | |
298 | drm->mode_config.funcs = &imx_drm_mode_config_funcs; | |
5f2f9115 | 299 | drm->mode_config.helper_private = &imx_drm_mode_config_helpers; |
1df8b530 | 300 | |
e692da4d | 301 | drm_mode_config_init(drm); |
e692da4d | 302 | |
020a9ea7 | 303 | ret = drm_vblank_init(drm, MAX_CRTC); |
e692da4d | 304 | if (ret) |
8007875f | 305 | goto err_kms; |
e692da4d | 306 | |
a72f8bee | 307 | platform_set_drvdata(drm->platformdev, drm); |
8d71de61 | 308 | |
17b5001b RK |
309 | /* Now try and bind all our sub-components */ |
310 | ret = component_bind_all(drm->dev, drm); | |
311 | if (ret) | |
ccec7f62 | 312 | goto err_vblank; |
e355e7dd RK |
313 | |
314 | /* | |
315 | * All components are now added, we can publish the connector sysfs | |
316 | * entries to userspace. This will generate hotplug events and so | |
317 | * userspace will expect to be able to access DRM at this point. | |
318 | */ | |
319 | list_for_each_entry(connector, &drm->mode_config.connector_list, head) { | |
34ea3d38 | 320 | ret = drm_connector_register(connector); |
e355e7dd RK |
321 | if (ret) { |
322 | dev_err(drm->dev, | |
34ea3d38 | 323 | "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n", |
e355e7dd | 324 | connector->base.id, |
4a2a450f | 325 | connector->name, ret); |
e355e7dd RK |
326 | goto err_unbind; |
327 | } | |
328 | } | |
329 | ||
255c35f8 LY |
330 | drm_mode_config_reset(drm); |
331 | ||
8acba02f RK |
332 | /* |
333 | * All components are now initialised, so setup the fb helper. | |
334 | * The fb helper takes copies of key hardware information, so the | |
335 | * crtcs/connectors/encoders must not change after this point. | |
336 | */ | |
c1ff5a7a | 337 | #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION) |
8acba02f RK |
338 | if (legacyfb_depth != 16 && legacyfb_depth != 32) { |
339 | dev_warn(drm->dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n"); | |
340 | legacyfb_depth = 16; | |
341 | } | |
342 | imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth, | |
343 | drm->mode_config.num_crtc, MAX_CRTC); | |
344 | if (IS_ERR(imxdrm->fbhelper)) { | |
345 | ret = PTR_ERR(imxdrm->fbhelper); | |
346 | imxdrm->fbhelper = NULL; | |
347 | goto err_unbind; | |
348 | } | |
349 | #endif | |
3e68439b RK |
350 | |
351 | drm_kms_helper_poll_init(drm); | |
352 | ||
8007875f | 353 | return 0; |
a72f8bee | 354 | |
e355e7dd RK |
355 | err_unbind: |
356 | component_unbind_all(drm->dev, drm); | |
ccec7f62 | 357 | err_vblank: |
8007875f RK |
358 | drm_vblank_cleanup(drm); |
359 | err_kms: | |
8007875f | 360 | drm_mode_config_cleanup(drm); |
e692da4d SH |
361 | |
362 | return ret; | |
363 | } | |
364 | ||
e692da4d SH |
365 | /* |
366 | * imx_drm_add_crtc - add a new crtc | |
e692da4d | 367 | */ |
32266b45 | 368 | int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc, |
43895599 | 369 | struct imx_drm_crtc **new_crtc, struct drm_plane *primary_plane, |
e692da4d | 370 | const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs, |
655b43cc | 371 | struct device_node *port) |
e692da4d | 372 | { |
32266b45 | 373 | struct imx_drm_device *imxdrm = drm->dev_private; |
e692da4d | 374 | struct imx_drm_crtc *imx_drm_crtc; |
e692da4d | 375 | |
fd6040ed RK |
376 | /* |
377 | * The vblank arrays are dimensioned by MAX_CRTC - we can't | |
378 | * pass IDs greater than this to those functions. | |
379 | */ | |
e7d6231e RK |
380 | if (imxdrm->pipes >= MAX_CRTC) |
381 | return -EINVAL; | |
fd6040ed | 382 | |
e7d6231e RK |
383 | if (imxdrm->drm->open_count) |
384 | return -EBUSY; | |
e692da4d SH |
385 | |
386 | imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL); | |
e7d6231e RK |
387 | if (!imx_drm_crtc) |
388 | return -ENOMEM; | |
e692da4d SH |
389 | |
390 | imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs; | |
e692da4d | 391 | imx_drm_crtc->crtc = crtc; |
e692da4d | 392 | |
6457b971 RK |
393 | crtc->port = port; |
394 | ||
d4b20e4d | 395 | imxdrm->crtc[imxdrm->pipes++] = imx_drm_crtc; |
e692da4d SH |
396 | |
397 | *new_crtc = imx_drm_crtc; | |
398 | ||
ec9557d7 RK |
399 | drm_crtc_helper_add(crtc, |
400 | imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs); | |
401 | ||
43895599 | 402 | drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL, |
f9882876 | 403 | imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs, NULL); |
ec9557d7 | 404 | |
e692da4d | 405 | return 0; |
e692da4d SH |
406 | } |
407 | EXPORT_SYMBOL_GPL(imx_drm_add_crtc); | |
408 | ||
409 | /* | |
410 | * imx_drm_remove_crtc - remove a crtc | |
411 | */ | |
412 | int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc) | |
413 | { | |
ccec7f62 | 414 | struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private; |
d4b20e4d | 415 | unsigned int pipe = drm_crtc_index(imx_drm_crtc->crtc); |
e692da4d SH |
416 | |
417 | drm_crtc_cleanup(imx_drm_crtc->crtc); | |
418 | ||
d4b20e4d | 419 | imxdrm->crtc[pipe] = NULL; |
e692da4d | 420 | |
e692da4d SH |
421 | kfree(imx_drm_crtc); |
422 | ||
423 | return 0; | |
424 | } | |
425 | EXPORT_SYMBOL_GPL(imx_drm_remove_crtc); | |
426 | ||
6457b971 RK |
427 | int imx_drm_encoder_parse_of(struct drm_device *drm, |
428 | struct drm_encoder *encoder, struct device_node *np) | |
9e2d410d | 429 | { |
6457b971 | 430 | uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np); |
9e2d410d | 431 | |
6457b971 RK |
432 | /* |
433 | * If we failed to find the CRTC(s) which this encoder is | |
434 | * supposed to be connected to, it's because the CRTC has | |
435 | * not been registered yet. Defer probing, and hope that | |
436 | * the required CRTC is added later. | |
437 | */ | |
438 | if (crtc_mask == 0) | |
439 | return -EPROBE_DEFER; | |
655b43cc | 440 | |
6457b971 | 441 | encoder->possible_crtcs = crtc_mask; |
63bc5164 | 442 | |
6457b971 RK |
443 | /* FIXME: this is the mask of outputs which can clone this output. */ |
444 | encoder->possible_clones = ~0; | |
9e2d410d RK |
445 | |
446 | return 0; | |
447 | } | |
6457b971 | 448 | EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of); |
9e2d410d | 449 | |
baa70943 | 450 | static const struct drm_ioctl_desc imx_drm_ioctls[] = { |
e692da4d SH |
451 | /* none so far */ |
452 | }; | |
453 | ||
454 | static struct drm_driver imx_drm_driver = { | |
8535c022 LY |
455 | .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | |
456 | DRIVER_ATOMIC, | |
e692da4d SH |
457 | .load = imx_drm_driver_load, |
458 | .unload = imx_drm_driver_unload, | |
e692da4d | 459 | .lastclose = imx_drm_driver_lastclose, |
4b193663 | 460 | .gem_free_object_unlocked = drm_gem_cma_free_object, |
e692da4d SH |
461 | .gem_vm_ops = &drm_gem_cma_vm_ops, |
462 | .dumb_create = drm_gem_cma_dumb_create, | |
463 | .dumb_map_offset = drm_gem_cma_dumb_map_offset, | |
43387b37 | 464 | .dumb_destroy = drm_gem_dumb_destroy, |
e692da4d | 465 | |
bd3665c9 PZ |
466 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, |
467 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, | |
468 | .gem_prime_import = drm_gem_prime_import, | |
469 | .gem_prime_export = drm_gem_prime_export, | |
470 | .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, | |
471 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, | |
472 | .gem_prime_vmap = drm_gem_cma_prime_vmap, | |
473 | .gem_prime_vunmap = drm_gem_cma_prime_vunmap, | |
474 | .gem_prime_mmap = drm_gem_cma_prime_mmap, | |
b44f8408 | 475 | .get_vblank_counter = drm_vblank_no_hw_counter, |
e692da4d SH |
476 | .enable_vblank = imx_drm_enable_vblank, |
477 | .disable_vblank = imx_drm_disable_vblank, | |
478 | .ioctls = imx_drm_ioctls, | |
479 | .num_ioctls = ARRAY_SIZE(imx_drm_ioctls), | |
480 | .fops = &imx_drm_driver_fops, | |
481 | .name = "imx-drm", | |
482 | .desc = "i.MX DRM graphics", | |
483 | .date = "20120507", | |
484 | .major = 1, | |
485 | .minor = 0, | |
486 | .patchlevel = 0, | |
487 | }; | |
488 | ||
17b5001b RK |
489 | static int compare_of(struct device *dev, void *data) |
490 | { | |
655b43cc | 491 | struct device_node *np = data; |
17b5001b | 492 | |
310944d1 PZ |
493 | /* Special case for DI, dev->of_node may not be set yet */ |
494 | if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) { | |
495 | struct ipu_client_platformdata *pdata = dev->platform_data; | |
496 | ||
497 | return pdata->of_node == np; | |
498 | } | |
499 | ||
655b43cc PZ |
500 | /* Special case for LDB, one device for two channels */ |
501 | if (of_node_cmp(np->name, "lvds-channel") == 0) { | |
502 | np = of_get_parent(np); | |
503 | of_node_put(np); | |
17b5001b RK |
504 | } |
505 | ||
655b43cc PZ |
506 | return dev->of_node == np; |
507 | } | |
17b5001b | 508 | |
17b5001b RK |
509 | static int imx_drm_bind(struct device *dev) |
510 | { | |
511 | return drm_platform_init(&imx_drm_driver, to_platform_device(dev)); | |
512 | } | |
513 | ||
514 | static void imx_drm_unbind(struct device *dev) | |
515 | { | |
516 | drm_put_dev(dev_get_drvdata(dev)); | |
517 | } | |
518 | ||
519 | static const struct component_master_ops imx_drm_ops = { | |
17b5001b RK |
520 | .bind = imx_drm_bind, |
521 | .unbind = imx_drm_unbind, | |
522 | }; | |
523 | ||
e692da4d SH |
524 | static int imx_drm_platform_probe(struct platform_device *pdev) |
525 | { | |
9cace32f | 526 | int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops); |
655b43cc | 527 | |
9cace32f LD |
528 | if (!ret) |
529 | ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)); | |
655b43cc | 530 | |
9cace32f | 531 | return ret; |
e692da4d SH |
532 | } |
533 | ||
534 | static int imx_drm_platform_remove(struct platform_device *pdev) | |
535 | { | |
17b5001b | 536 | component_master_del(&pdev->dev, &imx_drm_ops); |
e692da4d SH |
537 | return 0; |
538 | } | |
539 | ||
bfe945c8 SG |
540 | #ifdef CONFIG_PM_SLEEP |
541 | static int imx_drm_suspend(struct device *dev) | |
542 | { | |
543 | struct drm_device *drm_dev = dev_get_drvdata(dev); | |
5f2f9115 | 544 | struct imx_drm_device *imxdrm; |
bfe945c8 SG |
545 | |
546 | /* The drm_dev is NULL before .load hook is called */ | |
547 | if (drm_dev == NULL) | |
548 | return 0; | |
549 | ||
550 | drm_kms_helper_poll_disable(drm_dev); | |
551 | ||
5f2f9115 LY |
552 | imxdrm = drm_dev->dev_private; |
553 | imxdrm->state = drm_atomic_helper_suspend(drm_dev); | |
554 | if (IS_ERR(imxdrm->state)) { | |
555 | drm_kms_helper_poll_enable(drm_dev); | |
556 | return PTR_ERR(imxdrm->state); | |
557 | } | |
558 | ||
bfe945c8 SG |
559 | return 0; |
560 | } | |
561 | ||
562 | static int imx_drm_resume(struct device *dev) | |
563 | { | |
564 | struct drm_device *drm_dev = dev_get_drvdata(dev); | |
5f2f9115 | 565 | struct imx_drm_device *imx_drm; |
bfe945c8 SG |
566 | |
567 | if (drm_dev == NULL) | |
568 | return 0; | |
569 | ||
5f2f9115 LY |
570 | imx_drm = drm_dev->dev_private; |
571 | drm_atomic_helper_resume(drm_dev, imx_drm->state); | |
bfe945c8 SG |
572 | drm_kms_helper_poll_enable(drm_dev); |
573 | ||
574 | return 0; | |
575 | } | |
576 | #endif | |
577 | ||
578 | static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume); | |
579 | ||
17b5001b | 580 | static const struct of_device_id imx_drm_dt_ids[] = { |
655b43cc | 581 | { .compatible = "fsl,imx-display-subsystem", }, |
17b5001b RK |
582 | { /* sentinel */ }, |
583 | }; | |
584 | MODULE_DEVICE_TABLE(of, imx_drm_dt_ids); | |
585 | ||
e692da4d SH |
586 | static struct platform_driver imx_drm_pdrv = { |
587 | .probe = imx_drm_platform_probe, | |
99c28f10 | 588 | .remove = imx_drm_platform_remove, |
e692da4d | 589 | .driver = { |
e692da4d | 590 | .name = "imx-drm", |
bfe945c8 | 591 | .pm = &imx_drm_pm_ops, |
17b5001b | 592 | .of_match_table = imx_drm_dt_ids, |
e692da4d SH |
593 | }, |
594 | }; | |
b85f2b5d | 595 | module_platform_driver(imx_drm_pdrv); |
e692da4d SH |
596 | |
597 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); | |
598 | MODULE_DESCRIPTION("i.MX drm driver core"); | |
599 | MODULE_LICENSE("GPL"); |