Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Freescale i.MX drm driver
4 *
5 * Copyright (C) 2011 Sascha Hauer, Pengutronix
6 */
7
8#include <linux/component.h>
9#include <linux/device.h>
10#include <linux/dma-buf.h>
11#include <linux/module.h>
12#include <linux/platform_device.h>
13
14#include <video/imx-ipu-v3.h>
15
16#include <drm/drm_atomic.h>
17#include <drm/drm_atomic_helper.h>
18#include <drm/drm_drv.h>
19#include <drm/drm_fb_cma_helper.h>
20#include <drm/drm_fb_helper.h>
21#include <drm/drm_gem_cma_helper.h>
22#include <drm/drm_gem_framebuffer_helper.h>
23#include <drm/drm_of.h>
24#include <drm/drm_plane_helper.h>
25#include <drm/drm_probe_helper.h>
26#include <drm/drm_vblank.h>
27
28#include "imx-drm.h"
29#include "ipuv3-plane.h"
30
31#define MAX_CRTC 4
32
33static int legacyfb_depth = 16;
34module_param(legacyfb_depth, int, 0444);
35
36DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops);
37
38void imx_drm_connector_destroy(struct drm_connector *connector)
39{
40 drm_connector_unregister(connector);
41 drm_connector_cleanup(connector);
42}
43EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
44
45void imx_drm_encoder_destroy(struct drm_encoder *encoder)
46{
47 drm_encoder_cleanup(encoder);
48}
49EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
50
51static int imx_drm_atomic_check(struct drm_device *dev,
52 struct drm_atomic_state *state)
53{
54 int ret;
55
56 ret = drm_atomic_helper_check(dev, state);
57 if (ret)
58 return ret;
59
60 /*
61 * Check modeset again in case crtc_state->mode_changed is
62 * updated in plane's ->atomic_check callback.
63 */
64 ret = drm_atomic_helper_check_modeset(dev, state);
65 if (ret)
66 return ret;
67
68 /* Assign PRG/PRE channels and check if all constrains are satisfied. */
69 ret = ipu_planes_assign_pre(dev, state);
70 if (ret)
71 return ret;
72
73 return ret;
74}
75
76static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
77 .fb_create = drm_gem_fb_create,
78 .atomic_check = imx_drm_atomic_check,
79 .atomic_commit = drm_atomic_helper_commit,
80};
81
82static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
83{
84 struct drm_device *dev = state->dev;
85 struct drm_plane *plane;
86 struct drm_plane_state *old_plane_state, *new_plane_state;
87 bool plane_disabling = false;
88 int i;
89
90 drm_atomic_helper_commit_modeset_disables(dev, state);
91
92 drm_atomic_helper_commit_planes(dev, state,
93 DRM_PLANE_COMMIT_ACTIVE_ONLY |
94 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
95
96 drm_atomic_helper_commit_modeset_enables(dev, state);
97
98 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
99 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
100 plane_disabling = true;
101 }
102
103 /*
104 * The flip done wait is only strictly required by imx-drm if a deferred
105 * plane disable is in-flight. As the core requires blocking commits
106 * to wait for the flip it is done here unconditionally. This keeps the
107 * workitem around a bit longer than required for the majority of
108 * non-blocking commits, but we accept that for the sake of simplicity.
109 */
110 drm_atomic_helper_wait_for_flip_done(dev, state);
111
112 if (plane_disabling) {
113 for_each_old_plane_in_state(state, plane, old_plane_state, i)
114 ipu_plane_disable_deferred(plane);
115
116 }
117
118 drm_atomic_helper_commit_hw_done(state);
119}
120
121static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
122 .atomic_commit_tail = imx_drm_atomic_commit_tail,
123};
124
125
126int imx_drm_encoder_parse_of(struct drm_device *drm,
127 struct drm_encoder *encoder, struct device_node *np)
128{
129 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
130
131 /*
132 * If we failed to find the CRTC(s) which this encoder is
133 * supposed to be connected to, it's because the CRTC has
134 * not been registered yet. Defer probing, and hope that
135 * the required CRTC is added later.
136 */
137 if (crtc_mask == 0)
138 return -EPROBE_DEFER;
139
140 encoder->possible_crtcs = crtc_mask;
141
142 /* FIXME: this is the mask of outputs which can clone this output. */
143 encoder->possible_clones = ~0;
144
145 return 0;
146}
147EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
148
149static const struct drm_ioctl_desc imx_drm_ioctls[] = {
150 /* none so far */
151};
152
153static struct drm_driver imx_drm_driver = {
154 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
155 .gem_free_object_unlocked = drm_gem_cma_free_object,
156 .gem_vm_ops = &drm_gem_cma_vm_ops,
157 .dumb_create = drm_gem_cma_dumb_create,
158
159 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
160 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
161 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
162 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
163 .gem_prime_vmap = drm_gem_cma_prime_vmap,
164 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
165 .gem_prime_mmap = drm_gem_cma_prime_mmap,
166 .ioctls = imx_drm_ioctls,
167 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
168 .fops = &imx_drm_driver_fops,
169 .name = "imx-drm",
170 .desc = "i.MX DRM graphics",
171 .date = "20120507",
172 .major = 1,
173 .minor = 0,
174 .patchlevel = 0,
175};
176
177static int compare_of(struct device *dev, void *data)
178{
179 struct device_node *np = data;
180
181 /* Special case for DI, dev->of_node may not be set yet */
182 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
183 struct ipu_client_platformdata *pdata = dev->platform_data;
184
185 return pdata->of_node == np;
186 }
187
188 /* Special case for LDB, one device for two channels */
189 if (of_node_name_eq(np, "lvds-channel")) {
190 np = of_get_parent(np);
191 of_node_put(np);
192 }
193
194 return dev->of_node == np;
195}
196
197static int imx_drm_bind(struct device *dev)
198{
199 struct drm_device *drm;
200 int ret;
201
202 drm = drm_dev_alloc(&imx_drm_driver, dev);
203 if (IS_ERR(drm))
204 return PTR_ERR(drm);
205
206 /*
207 * enable drm irq mode.
208 * - with irq_enabled = true, we can use the vblank feature.
209 *
210 * P.S. note that we wouldn't use drm irq handler but
211 * just specific driver own one instead because
212 * drm framework supports only one irq handler and
213 * drivers can well take care of their interrupts
214 */
215 drm->irq_enabled = true;
216
217 /*
218 * set max width and height as default value(4096x4096).
219 * this value would be used to check framebuffer size limitation
220 * at drm_mode_addfb().
221 */
222 drm->mode_config.min_width = 1;
223 drm->mode_config.min_height = 1;
224 drm->mode_config.max_width = 4096;
225 drm->mode_config.max_height = 4096;
226 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
227 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
228 drm->mode_config.allow_fb_modifiers = true;
229 drm->mode_config.normalize_zpos = true;
230
231 drm_mode_config_init(drm);
232
233 ret = drm_vblank_init(drm, MAX_CRTC);
234 if (ret)
235 goto err_kms;
236
237 dev_set_drvdata(dev, drm);
238
239 /* Now try and bind all our sub-components */
240 ret = component_bind_all(dev, drm);
241 if (ret)
242 goto err_kms;
243
244 drm_mode_config_reset(drm);
245
246 /*
247 * All components are now initialised, so setup the fb helper.
248 * The fb helper takes copies of key hardware information, so the
249 * crtcs/connectors/encoders must not change after this point.
250 */
251 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
252 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
253 legacyfb_depth = 16;
254 }
255
256 drm_kms_helper_poll_init(drm);
257
258 ret = drm_dev_register(drm, 0);
259 if (ret)
260 goto err_poll_fini;
261
262 drm_fbdev_generic_setup(drm, legacyfb_depth);
263
264 return 0;
265
266err_poll_fini:
267 drm_kms_helper_poll_fini(drm);
268 component_unbind_all(drm->dev, drm);
269err_kms:
270 drm_mode_config_cleanup(drm);
271 drm_dev_put(drm);
272
273 return ret;
274}
275
276static void imx_drm_unbind(struct device *dev)
277{
278 struct drm_device *drm = dev_get_drvdata(dev);
279
280 drm_dev_unregister(drm);
281
282 drm_kms_helper_poll_fini(drm);
283
284 drm_mode_config_cleanup(drm);
285
286 component_unbind_all(drm->dev, drm);
287 dev_set_drvdata(dev, NULL);
288
289 drm_dev_put(drm);
290}
291
292static const struct component_master_ops imx_drm_ops = {
293 .bind = imx_drm_bind,
294 .unbind = imx_drm_unbind,
295};
296
297static int imx_drm_platform_probe(struct platform_device *pdev)
298{
299 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
300
301 if (!ret)
302 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
303
304 return ret;
305}
306
307static int imx_drm_platform_remove(struct platform_device *pdev)
308{
309 component_master_del(&pdev->dev, &imx_drm_ops);
310 return 0;
311}
312
313#ifdef CONFIG_PM_SLEEP
314static int imx_drm_suspend(struct device *dev)
315{
316 struct drm_device *drm_dev = dev_get_drvdata(dev);
317
318 return drm_mode_config_helper_suspend(drm_dev);
319}
320
321static int imx_drm_resume(struct device *dev)
322{
323 struct drm_device *drm_dev = dev_get_drvdata(dev);
324
325 return drm_mode_config_helper_resume(drm_dev);
326}
327#endif
328
329static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
330
331static const struct of_device_id imx_drm_dt_ids[] = {
332 { .compatible = "fsl,imx-display-subsystem", },
333 { /* sentinel */ },
334};
335MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
336
337static struct platform_driver imx_drm_pdrv = {
338 .probe = imx_drm_platform_probe,
339 .remove = imx_drm_platform_remove,
340 .driver = {
341 .name = "imx-drm",
342 .pm = &imx_drm_pm_ops,
343 .of_match_table = imx_drm_dt_ids,
344 },
345};
346
347static struct platform_driver * const drivers[] = {
348 &imx_drm_pdrv,
349 &ipu_drm_driver,
350};
351
352static int __init imx_drm_init(void)
353{
354 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
355}
356module_init(imx_drm_init);
357
358static void __exit imx_drm_exit(void)
359{
360 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
361}
362module_exit(imx_drm_exit);
363
364MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
365MODULE_DESCRIPTION("i.MX drm driver core");
366MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Freescale i.MX drm driver
4 *
5 * Copyright (C) 2011 Sascha Hauer, Pengutronix
6 */
7
8#include <linux/component.h>
9#include <linux/device.h>
10#include <linux/dma-buf.h>
11#include <linux/module.h>
12#include <linux/platform_device.h>
13
14#include <video/imx-ipu-v3.h>
15
16#include <drm/drm_atomic.h>
17#include <drm/drm_atomic_helper.h>
18#include <drm/drm_drv.h>
19#include <drm/drm_fb_cma_helper.h>
20#include <drm/drm_fb_helper.h>
21#include <drm/drm_gem_cma_helper.h>
22#include <drm/drm_gem_framebuffer_helper.h>
23#include <drm/drm_managed.h>
24#include <drm/drm_of.h>
25#include <drm/drm_plane_helper.h>
26#include <drm/drm_probe_helper.h>
27#include <drm/drm_vblank.h>
28
29#include "imx-drm.h"
30#include "ipuv3-plane.h"
31
32#define MAX_CRTC 4
33
34static int legacyfb_depth = 16;
35module_param(legacyfb_depth, int, 0444);
36
37DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops);
38
39void imx_drm_connector_destroy(struct drm_connector *connector)
40{
41 drm_connector_unregister(connector);
42 drm_connector_cleanup(connector);
43}
44EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
45
46static int imx_drm_atomic_check(struct drm_device *dev,
47 struct drm_atomic_state *state)
48{
49 int ret;
50
51 ret = drm_atomic_helper_check(dev, state);
52 if (ret)
53 return ret;
54
55 /*
56 * Check modeset again in case crtc_state->mode_changed is
57 * updated in plane's ->atomic_check callback.
58 */
59 ret = drm_atomic_helper_check_modeset(dev, state);
60 if (ret)
61 return ret;
62
63 /* Assign PRG/PRE channels and check if all constrains are satisfied. */
64 ret = ipu_planes_assign_pre(dev, state);
65 if (ret)
66 return ret;
67
68 return ret;
69}
70
71static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
72 .fb_create = drm_gem_fb_create,
73 .atomic_check = imx_drm_atomic_check,
74 .atomic_commit = drm_atomic_helper_commit,
75};
76
77static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
78{
79 struct drm_device *dev = state->dev;
80 struct drm_plane *plane;
81 struct drm_plane_state *old_plane_state, *new_plane_state;
82 bool plane_disabling = false;
83 int i;
84 bool fence_cookie = dma_fence_begin_signalling();
85
86 drm_atomic_helper_commit_modeset_disables(dev, state);
87
88 drm_atomic_helper_commit_planes(dev, state,
89 DRM_PLANE_COMMIT_ACTIVE_ONLY |
90 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
91
92 drm_atomic_helper_commit_modeset_enables(dev, state);
93
94 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
95 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
96 plane_disabling = true;
97 }
98
99 /*
100 * The flip done wait is only strictly required by imx-drm if a deferred
101 * plane disable is in-flight. As the core requires blocking commits
102 * to wait for the flip it is done here unconditionally. This keeps the
103 * workitem around a bit longer than required for the majority of
104 * non-blocking commits, but we accept that for the sake of simplicity.
105 */
106 drm_atomic_helper_wait_for_flip_done(dev, state);
107
108 if (plane_disabling) {
109 for_each_old_plane_in_state(state, plane, old_plane_state, i)
110 ipu_plane_disable_deferred(plane);
111
112 }
113
114 drm_atomic_helper_commit_hw_done(state);
115 dma_fence_end_signalling(fence_cookie);
116}
117
118static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
119 .atomic_commit_tail = imx_drm_atomic_commit_tail,
120};
121
122
123int imx_drm_encoder_parse_of(struct drm_device *drm,
124 struct drm_encoder *encoder, struct device_node *np)
125{
126 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
127
128 /*
129 * If we failed to find the CRTC(s) which this encoder is
130 * supposed to be connected to, it's because the CRTC has
131 * not been registered yet. Defer probing, and hope that
132 * the required CRTC is added later.
133 */
134 if (crtc_mask == 0)
135 return -EPROBE_DEFER;
136
137 encoder->possible_crtcs = crtc_mask;
138
139 /* FIXME: cloning support not clear, disable it all for now */
140 encoder->possible_clones = 0;
141
142 return 0;
143}
144EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
145
146static const struct drm_ioctl_desc imx_drm_ioctls[] = {
147 /* none so far */
148};
149
150static int imx_drm_dumb_create(struct drm_file *file_priv,
151 struct drm_device *drm,
152 struct drm_mode_create_dumb *args)
153{
154 u32 width = args->width;
155 int ret;
156
157 args->width = ALIGN(width, 8);
158
159 ret = drm_gem_cma_dumb_create(file_priv, drm, args);
160 if (ret)
161 return ret;
162
163 args->width = width;
164 return ret;
165}
166
167static const struct drm_driver imx_drm_driver = {
168 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
169 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(imx_drm_dumb_create),
170 .ioctls = imx_drm_ioctls,
171 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
172 .fops = &imx_drm_driver_fops,
173 .name = "imx-drm",
174 .desc = "i.MX DRM graphics",
175 .date = "20120507",
176 .major = 1,
177 .minor = 0,
178 .patchlevel = 0,
179};
180
181static int compare_of(struct device *dev, void *data)
182{
183 struct device_node *np = data;
184
185 /* Special case for DI, dev->of_node may not be set yet */
186 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
187 struct ipu_client_platformdata *pdata = dev->platform_data;
188
189 return pdata->of_node == np;
190 }
191
192 /* Special case for LDB, one device for two channels */
193 if (of_node_name_eq(np, "lvds-channel")) {
194 np = of_get_parent(np);
195 of_node_put(np);
196 }
197
198 return dev->of_node == np;
199}
200
201static int imx_drm_bind(struct device *dev)
202{
203 struct drm_device *drm;
204 int ret;
205
206 drm = drm_dev_alloc(&imx_drm_driver, dev);
207 if (IS_ERR(drm))
208 return PTR_ERR(drm);
209
210 /*
211 * enable drm irq mode.
212 * - with irq_enabled = true, we can use the vblank feature.
213 *
214 * P.S. note that we wouldn't use drm irq handler but
215 * just specific driver own one instead because
216 * drm framework supports only one irq handler and
217 * drivers can well take care of their interrupts
218 */
219 drm->irq_enabled = true;
220
221 /*
222 * set max width and height as default value(4096x4096).
223 * this value would be used to check framebuffer size limitation
224 * at drm_mode_addfb().
225 */
226 drm->mode_config.min_width = 1;
227 drm->mode_config.min_height = 1;
228 drm->mode_config.max_width = 4096;
229 drm->mode_config.max_height = 4096;
230 drm->mode_config.funcs = &imx_drm_mode_config_funcs;
231 drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
232 drm->mode_config.normalize_zpos = true;
233
234 ret = drmm_mode_config_init(drm);
235 if (ret)
236 goto err_kms;
237
238 ret = drm_vblank_init(drm, MAX_CRTC);
239 if (ret)
240 goto err_kms;
241
242 dev_set_drvdata(dev, drm);
243
244 /* Now try and bind all our sub-components */
245 ret = component_bind_all(dev, drm);
246 if (ret)
247 goto err_kms;
248
249 drm_mode_config_reset(drm);
250
251 /*
252 * All components are now initialised, so setup the fb helper.
253 * The fb helper takes copies of key hardware information, so the
254 * crtcs/connectors/encoders must not change after this point.
255 */
256 if (legacyfb_depth != 16 && legacyfb_depth != 32) {
257 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
258 legacyfb_depth = 16;
259 }
260
261 drm_kms_helper_poll_init(drm);
262
263 ret = drm_dev_register(drm, 0);
264 if (ret)
265 goto err_poll_fini;
266
267 drm_fbdev_generic_setup(drm, legacyfb_depth);
268
269 return 0;
270
271err_poll_fini:
272 drm_kms_helper_poll_fini(drm);
273 component_unbind_all(drm->dev, drm);
274err_kms:
275 drm_dev_put(drm);
276
277 return ret;
278}
279
280static void imx_drm_unbind(struct device *dev)
281{
282 struct drm_device *drm = dev_get_drvdata(dev);
283
284 drm_dev_unregister(drm);
285
286 drm_kms_helper_poll_fini(drm);
287
288 component_unbind_all(drm->dev, drm);
289
290 drm_dev_put(drm);
291
292 dev_set_drvdata(dev, NULL);
293}
294
295static const struct component_master_ops imx_drm_ops = {
296 .bind = imx_drm_bind,
297 .unbind = imx_drm_unbind,
298};
299
300static int imx_drm_platform_probe(struct platform_device *pdev)
301{
302 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
303
304 if (!ret)
305 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
306
307 return ret;
308}
309
310static int imx_drm_platform_remove(struct platform_device *pdev)
311{
312 component_master_del(&pdev->dev, &imx_drm_ops);
313 return 0;
314}
315
316#ifdef CONFIG_PM_SLEEP
317static int imx_drm_suspend(struct device *dev)
318{
319 struct drm_device *drm_dev = dev_get_drvdata(dev);
320
321 return drm_mode_config_helper_suspend(drm_dev);
322}
323
324static int imx_drm_resume(struct device *dev)
325{
326 struct drm_device *drm_dev = dev_get_drvdata(dev);
327
328 return drm_mode_config_helper_resume(drm_dev);
329}
330#endif
331
332static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
333
334static const struct of_device_id imx_drm_dt_ids[] = {
335 { .compatible = "fsl,imx-display-subsystem", },
336 { /* sentinel */ },
337};
338MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
339
340static struct platform_driver imx_drm_pdrv = {
341 .probe = imx_drm_platform_probe,
342 .remove = imx_drm_platform_remove,
343 .driver = {
344 .name = "imx-drm",
345 .pm = &imx_drm_pm_ops,
346 .of_match_table = imx_drm_dt_ids,
347 },
348};
349
350static struct platform_driver * const drivers[] = {
351 &imx_drm_pdrv,
352 &ipu_drm_driver,
353};
354
355static int __init imx_drm_init(void)
356{
357 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
358}
359module_init(imx_drm_init);
360
361static void __exit imx_drm_exit(void)
362{
363 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
364}
365module_exit(imx_drm_exit);
366
367MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
368MODULE_DESCRIPTION("i.MX drm driver core");
369MODULE_LICENSE("GPL");