Linux Audio

Check our new training course

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