2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author:Mark Yao <mark.yao@rock-chips.com>
5 * based on exynos_drm_drv.c
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <drm/drm_of.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/dma-iommu.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/module.h>
26 #include <linux/of_graph.h>
27 #include <linux/component.h>
28 #include <linux/console.h>
29 #include <linux/iommu.h>
31 #include "rockchip_drm_drv.h"
32 #include "rockchip_drm_fb.h"
33 #include "rockchip_drm_fbdev.h"
34 #include "rockchip_drm_gem.h"
36 #define DRIVER_NAME "rockchip"
37 #define DRIVER_DESC "RockChip Soc DRM"
38 #define DRIVER_DATE "20140818"
39 #define DRIVER_MAJOR 1
40 #define DRIVER_MINOR 0
42 static bool is_support_iommu = true;
43 static struct drm_driver rockchip_drm_driver;
46 * Attach a (component) device to the shared drm dma mapping from master drm
47 * device. This is used by the VOPs to map GEM buffers to a common DMA
50 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
53 struct rockchip_drm_private *private = drm_dev->dev_private;
56 if (!is_support_iommu)
59 ret = iommu_attach_device(private->domain, dev);
61 dev_err(dev, "Failed to attach iommu device\n");
68 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
71 struct rockchip_drm_private *private = drm_dev->dev_private;
72 struct iommu_domain *domain = private->domain;
74 if (!is_support_iommu)
77 iommu_detach_device(domain, dev);
80 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
81 const struct rockchip_crtc_funcs *crtc_funcs)
83 int pipe = drm_crtc_index(crtc);
84 struct rockchip_drm_private *priv = crtc->dev->dev_private;
86 if (pipe >= ROCKCHIP_MAX_CRTC)
89 priv->crtc_funcs[pipe] = crtc_funcs;
94 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
96 int pipe = drm_crtc_index(crtc);
97 struct rockchip_drm_private *priv = crtc->dev->dev_private;
99 if (pipe >= ROCKCHIP_MAX_CRTC)
102 priv->crtc_funcs[pipe] = NULL;
105 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
108 struct rockchip_drm_private *priv = dev->dev_private;
109 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
111 if (crtc && priv->crtc_funcs[pipe] &&
112 priv->crtc_funcs[pipe]->enable_vblank)
113 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
118 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
121 struct rockchip_drm_private *priv = dev->dev_private;
122 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
124 if (crtc && priv->crtc_funcs[pipe] &&
125 priv->crtc_funcs[pipe]->enable_vblank)
126 priv->crtc_funcs[pipe]->disable_vblank(crtc);
129 static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
131 struct rockchip_drm_private *private = drm_dev->dev_private;
132 struct iommu_domain_geometry *geometry;
135 if (!is_support_iommu)
138 private->domain = iommu_domain_alloc(&platform_bus_type);
139 if (!private->domain)
142 geometry = &private->domain->geometry;
143 start = geometry->aperture_start;
144 end = geometry->aperture_end;
146 DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
148 drm_mm_init(&private->mm, start, end - start + 1);
149 mutex_init(&private->mm_lock);
154 static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
156 struct rockchip_drm_private *private = drm_dev->dev_private;
158 if (!is_support_iommu)
161 drm_mm_takedown(&private->mm);
162 iommu_domain_free(private->domain);
165 static int rockchip_drm_bind(struct device *dev)
167 struct drm_device *drm_dev;
168 struct rockchip_drm_private *private;
171 drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
173 return PTR_ERR(drm_dev);
175 dev_set_drvdata(dev, drm_dev);
177 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
183 drm_dev->dev_private = private;
185 INIT_LIST_HEAD(&private->psr_list);
186 spin_lock_init(&private->psr_list_lock);
188 drm_mode_config_init(drm_dev);
190 rockchip_drm_mode_config_init(drm_dev);
192 ret = rockchip_drm_init_iommu(drm_dev);
194 goto err_config_cleanup;
196 /* Try to bind all sub drivers. */
197 ret = component_bind_all(dev, drm_dev);
199 goto err_iommu_cleanup;
201 /* init kms poll for handling hpd */
202 drm_kms_helper_poll_init(drm_dev);
205 * enable drm irq mode.
206 * - with irq_enabled = true, we can use the vblank feature.
208 drm_dev->irq_enabled = true;
210 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
212 goto err_kms_helper_poll_fini;
214 drm_mode_config_reset(drm_dev);
216 ret = rockchip_drm_fbdev_init(drm_dev);
218 goto err_vblank_cleanup;
220 ret = drm_dev_register(drm_dev, 0);
226 rockchip_drm_fbdev_fini(drm_dev);
228 drm_vblank_cleanup(drm_dev);
229 err_kms_helper_poll_fini:
230 drm_kms_helper_poll_fini(drm_dev);
231 component_unbind_all(dev, drm_dev);
233 rockchip_iommu_cleanup(drm_dev);
235 drm_mode_config_cleanup(drm_dev);
236 drm_dev->dev_private = NULL;
238 drm_dev_unref(drm_dev);
242 static void rockchip_drm_unbind(struct device *dev)
244 struct drm_device *drm_dev = dev_get_drvdata(dev);
246 rockchip_drm_fbdev_fini(drm_dev);
247 drm_vblank_cleanup(drm_dev);
248 drm_kms_helper_poll_fini(drm_dev);
249 component_unbind_all(dev, drm_dev);
250 rockchip_iommu_cleanup(drm_dev);
251 drm_mode_config_cleanup(drm_dev);
252 drm_dev->dev_private = NULL;
253 drm_dev_unregister(drm_dev);
254 drm_dev_unref(drm_dev);
255 dev_set_drvdata(dev, NULL);
258 static void rockchip_drm_lastclose(struct drm_device *dev)
260 struct rockchip_drm_private *priv = dev->dev_private;
262 drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
265 static const struct file_operations rockchip_drm_driver_fops = {
266 .owner = THIS_MODULE,
268 .mmap = rockchip_gem_mmap,
271 .unlocked_ioctl = drm_ioctl,
272 .compat_ioctl = drm_compat_ioctl,
273 .release = drm_release,
276 static struct drm_driver rockchip_drm_driver = {
277 .driver_features = DRIVER_MODESET | DRIVER_GEM |
278 DRIVER_PRIME | DRIVER_ATOMIC,
279 .lastclose = rockchip_drm_lastclose,
280 .get_vblank_counter = drm_vblank_no_hw_counter,
281 .enable_vblank = rockchip_drm_crtc_enable_vblank,
282 .disable_vblank = rockchip_drm_crtc_disable_vblank,
283 .gem_vm_ops = &drm_gem_cma_vm_ops,
284 .gem_free_object_unlocked = rockchip_gem_free_object,
285 .dumb_create = rockchip_gem_dumb_create,
286 .dumb_map_offset = rockchip_gem_dumb_map_offset,
287 .dumb_destroy = drm_gem_dumb_destroy,
288 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
289 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
290 .gem_prime_import = drm_gem_prime_import,
291 .gem_prime_export = drm_gem_prime_export,
292 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
293 .gem_prime_vmap = rockchip_gem_prime_vmap,
294 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
295 .gem_prime_mmap = rockchip_gem_mmap_buf,
296 .fops = &rockchip_drm_driver_fops,
300 .major = DRIVER_MAJOR,
301 .minor = DRIVER_MINOR,
304 #ifdef CONFIG_PM_SLEEP
305 static void rockchip_drm_fb_suspend(struct drm_device *drm)
307 struct rockchip_drm_private *priv = drm->dev_private;
310 drm_fb_helper_set_suspend(&priv->fbdev_helper, 1);
314 static void rockchip_drm_fb_resume(struct drm_device *drm)
316 struct rockchip_drm_private *priv = drm->dev_private;
319 drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
323 static int rockchip_drm_sys_suspend(struct device *dev)
325 struct drm_device *drm = dev_get_drvdata(dev);
326 struct rockchip_drm_private *priv = drm->dev_private;
328 drm_kms_helper_poll_disable(drm);
329 rockchip_drm_fb_suspend(drm);
331 priv->state = drm_atomic_helper_suspend(drm);
332 if (IS_ERR(priv->state)) {
333 rockchip_drm_fb_resume(drm);
334 drm_kms_helper_poll_enable(drm);
335 return PTR_ERR(priv->state);
341 static int rockchip_drm_sys_resume(struct device *dev)
343 struct drm_device *drm = dev_get_drvdata(dev);
344 struct rockchip_drm_private *priv = drm->dev_private;
346 drm_atomic_helper_resume(drm, priv->state);
347 rockchip_drm_fb_resume(drm);
348 drm_kms_helper_poll_enable(drm);
354 static const struct dev_pm_ops rockchip_drm_pm_ops = {
355 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
356 rockchip_drm_sys_resume)
359 static int compare_of(struct device *dev, void *data)
361 struct device_node *np = data;
363 return dev->of_node == np;
366 static void rockchip_add_endpoints(struct device *dev,
367 struct component_match **match,
368 struct device_node *port)
370 struct device_node *ep, *remote;
372 for_each_child_of_node(port, ep) {
373 remote = of_graph_get_remote_port_parent(ep);
374 if (!remote || !of_device_is_available(remote)) {
377 } else if (!of_device_is_available(remote->parent)) {
378 dev_warn(dev, "parent device of %s is not available\n",
384 drm_of_component_match_add(dev, match, compare_of, remote);
389 static const struct component_master_ops rockchip_drm_ops = {
390 .bind = rockchip_drm_bind,
391 .unbind = rockchip_drm_unbind,
394 static int rockchip_drm_platform_probe(struct platform_device *pdev)
396 struct device *dev = &pdev->dev;
397 struct component_match *match = NULL;
398 struct device_node *np = dev->of_node;
399 struct device_node *port;
405 * Bind the crtc ports first, so that
406 * drm_of_find_possible_crtcs called from encoder .bind callbacks
410 struct device_node *iommu;
412 port = of_parse_phandle(np, "ports", i);
416 if (!of_device_is_available(port->parent)) {
421 iommu = of_parse_phandle(port->parent, "iommus", 0);
422 if (!iommu || !of_device_is_available(iommu->parent)) {
423 dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
424 port->parent->full_name);
426 * if there is a crtc not support iommu, force set all
427 * crtc use non-iommu buffer.
429 is_support_iommu = false;
433 drm_of_component_match_add(dev, &match, compare_of,
439 dev_err(dev, "missing 'ports' property\n");
444 dev_err(dev, "No available vop found for display-subsystem.\n");
448 * For each bound crtc, bind the encoders attached to its
452 port = of_parse_phandle(np, "ports", i);
456 if (!of_device_is_available(port->parent)) {
461 rockchip_add_endpoints(dev, &match, port);
465 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
468 static int rockchip_drm_platform_remove(struct platform_device *pdev)
470 component_master_del(&pdev->dev, &rockchip_drm_ops);
475 static const struct of_device_id rockchip_drm_dt_ids[] = {
476 { .compatible = "rockchip,display-subsystem", },
479 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
481 static struct platform_driver rockchip_drm_platform_driver = {
482 .probe = rockchip_drm_platform_probe,
483 .remove = rockchip_drm_platform_remove,
485 .name = "rockchip-drm",
486 .of_match_table = rockchip_drm_dt_ids,
487 .pm = &rockchip_drm_pm_ops,
491 module_platform_driver(rockchip_drm_platform_driver);
493 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
494 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
495 MODULE_LICENSE("GPL v2");