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.
17 #include <asm/dma-iommu.h>
20 #include <drm/drm_crtc_helper.h>
21 #include <drm/drm_fb_helper.h>
22 #include <drm/drm_gem_cma_helper.h>
23 #include <drm/drm_of.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/module.h>
27 #include <linux/of_graph.h>
28 #include <linux/component.h>
29 #include <linux/console.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 dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping;
56 if (!is_support_iommu)
59 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
63 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
65 return arm_iommu_attach_device(dev, mapping);
68 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
71 if (!is_support_iommu)
74 arm_iommu_detach_device(dev);
77 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
78 const struct rockchip_crtc_funcs *crtc_funcs)
80 int pipe = drm_crtc_index(crtc);
81 struct rockchip_drm_private *priv = crtc->dev->dev_private;
83 if (pipe >= ROCKCHIP_MAX_CRTC)
86 priv->crtc_funcs[pipe] = crtc_funcs;
91 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
93 int pipe = drm_crtc_index(crtc);
94 struct rockchip_drm_private *priv = crtc->dev->dev_private;
96 if (pipe >= ROCKCHIP_MAX_CRTC)
99 priv->crtc_funcs[pipe] = NULL;
102 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
105 struct drm_crtc *crtc;
108 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
115 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
118 struct rockchip_drm_private *priv = dev->dev_private;
119 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
121 if (crtc && priv->crtc_funcs[pipe] &&
122 priv->crtc_funcs[pipe]->enable_vblank)
123 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
128 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
131 struct rockchip_drm_private *priv = dev->dev_private;
132 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
134 if (crtc && priv->crtc_funcs[pipe] &&
135 priv->crtc_funcs[pipe]->enable_vblank)
136 priv->crtc_funcs[pipe]->disable_vblank(crtc);
139 static int rockchip_drm_bind(struct device *dev)
141 struct drm_device *drm_dev;
142 struct rockchip_drm_private *private;
143 struct dma_iommu_mapping *mapping = NULL;
146 drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
148 return PTR_ERR(drm_dev);
150 dev_set_drvdata(dev, drm_dev);
152 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
158 drm_dev->dev_private = private;
160 INIT_LIST_HEAD(&private->psr_list);
161 spin_lock_init(&private->psr_list_lock);
163 drm_mode_config_init(drm_dev);
165 rockchip_drm_mode_config_init(drm_dev);
167 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
169 if (!dev->dma_parms) {
171 goto err_config_cleanup;
174 if (is_support_iommu) {
175 /* TODO(djkurtz): fetch the mapping start/size from somewhere */
176 mapping = arm_iommu_create_mapping(&platform_bus_type,
179 if (IS_ERR(mapping)) {
180 ret = PTR_ERR(mapping);
181 goto err_config_cleanup;
184 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
186 goto err_release_mapping;
188 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
190 ret = arm_iommu_attach_device(dev, mapping);
192 goto err_release_mapping;
195 /* Try to bind all sub drivers. */
196 ret = component_bind_all(dev, drm_dev);
198 goto err_detach_device;
200 /* init kms poll for handling hpd */
201 drm_kms_helper_poll_init(drm_dev);
204 * enable drm irq mode.
205 * - with irq_enabled = true, we can use the vblank feature.
207 drm_dev->irq_enabled = true;
209 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
211 goto err_kms_helper_poll_fini;
213 drm_mode_config_reset(drm_dev);
215 ret = rockchip_drm_fbdev_init(drm_dev);
217 goto err_vblank_cleanup;
219 ret = drm_dev_register(drm_dev, 0);
223 if (is_support_iommu)
224 arm_iommu_release_mapping(mapping);
227 rockchip_drm_fbdev_fini(drm_dev);
229 drm_vblank_cleanup(drm_dev);
230 err_kms_helper_poll_fini:
231 drm_kms_helper_poll_fini(drm_dev);
232 component_unbind_all(dev, drm_dev);
234 if (is_support_iommu)
235 arm_iommu_detach_device(dev);
237 if (is_support_iommu)
238 arm_iommu_release_mapping(mapping);
240 drm_mode_config_cleanup(drm_dev);
241 drm_dev->dev_private = NULL;
243 drm_dev_unref(drm_dev);
247 static void rockchip_drm_unbind(struct device *dev)
249 struct drm_device *drm_dev = dev_get_drvdata(dev);
251 rockchip_drm_fbdev_fini(drm_dev);
252 drm_vblank_cleanup(drm_dev);
253 drm_kms_helper_poll_fini(drm_dev);
254 component_unbind_all(dev, drm_dev);
255 if (is_support_iommu)
256 arm_iommu_detach_device(dev);
257 drm_mode_config_cleanup(drm_dev);
258 drm_dev->dev_private = NULL;
259 drm_dev_unregister(drm_dev);
260 drm_dev_unref(drm_dev);
261 dev_set_drvdata(dev, NULL);
264 static void rockchip_drm_lastclose(struct drm_device *dev)
266 struct rockchip_drm_private *priv = dev->dev_private;
268 drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
271 static const struct file_operations rockchip_drm_driver_fops = {
272 .owner = THIS_MODULE,
274 .mmap = rockchip_gem_mmap,
277 .unlocked_ioctl = drm_ioctl,
278 .compat_ioctl = drm_compat_ioctl,
279 .release = drm_release,
282 static struct drm_driver rockchip_drm_driver = {
283 .driver_features = DRIVER_MODESET | DRIVER_GEM |
284 DRIVER_PRIME | DRIVER_ATOMIC,
285 .lastclose = rockchip_drm_lastclose,
286 .get_vblank_counter = drm_vblank_no_hw_counter,
287 .enable_vblank = rockchip_drm_crtc_enable_vblank,
288 .disable_vblank = rockchip_drm_crtc_disable_vblank,
289 .gem_vm_ops = &drm_gem_cma_vm_ops,
290 .gem_free_object_unlocked = rockchip_gem_free_object,
291 .dumb_create = rockchip_gem_dumb_create,
292 .dumb_map_offset = rockchip_gem_dumb_map_offset,
293 .dumb_destroy = drm_gem_dumb_destroy,
294 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
295 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
296 .gem_prime_import = drm_gem_prime_import,
297 .gem_prime_export = drm_gem_prime_export,
298 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
299 .gem_prime_vmap = rockchip_gem_prime_vmap,
300 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
301 .gem_prime_mmap = rockchip_gem_mmap_buf,
302 .fops = &rockchip_drm_driver_fops,
306 .major = DRIVER_MAJOR,
307 .minor = DRIVER_MINOR,
310 #ifdef CONFIG_PM_SLEEP
311 static void rockchip_drm_fb_suspend(struct drm_device *drm)
313 struct rockchip_drm_private *priv = drm->dev_private;
316 drm_fb_helper_set_suspend(&priv->fbdev_helper, 1);
320 static void rockchip_drm_fb_resume(struct drm_device *drm)
322 struct rockchip_drm_private *priv = drm->dev_private;
325 drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
329 static int rockchip_drm_sys_suspend(struct device *dev)
331 struct drm_device *drm = dev_get_drvdata(dev);
332 struct rockchip_drm_private *priv = drm->dev_private;
334 drm_kms_helper_poll_disable(drm);
335 rockchip_drm_fb_suspend(drm);
337 priv->state = drm_atomic_helper_suspend(drm);
338 if (IS_ERR(priv->state)) {
339 rockchip_drm_fb_resume(drm);
340 drm_kms_helper_poll_enable(drm);
341 return PTR_ERR(priv->state);
347 static int rockchip_drm_sys_resume(struct device *dev)
349 struct drm_device *drm = dev_get_drvdata(dev);
350 struct rockchip_drm_private *priv = drm->dev_private;
352 drm_atomic_helper_resume(drm, priv->state);
353 rockchip_drm_fb_resume(drm);
354 drm_kms_helper_poll_enable(drm);
360 static const struct dev_pm_ops rockchip_drm_pm_ops = {
361 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
362 rockchip_drm_sys_resume)
365 static int compare_of(struct device *dev, void *data)
367 struct device_node *np = data;
369 return dev->of_node == np;
372 static void rockchip_add_endpoints(struct device *dev,
373 struct component_match **match,
374 struct device_node *port)
376 struct device_node *ep, *remote;
378 for_each_child_of_node(port, ep) {
379 remote = of_graph_get_remote_port_parent(ep);
380 if (!remote || !of_device_is_available(remote)) {
383 } else if (!of_device_is_available(remote->parent)) {
384 dev_warn(dev, "parent device of %s is not available\n",
390 drm_of_component_match_add(dev, match, compare_of, remote);
395 static const struct component_master_ops rockchip_drm_ops = {
396 .bind = rockchip_drm_bind,
397 .unbind = rockchip_drm_unbind,
400 static int rockchip_drm_platform_probe(struct platform_device *pdev)
402 struct device *dev = &pdev->dev;
403 struct component_match *match = NULL;
404 struct device_node *np = dev->of_node;
405 struct device_node *port;
411 * Bind the crtc ports first, so that
412 * drm_of_find_possible_crtcs called from encoder .bind callbacks
416 struct device_node *iommu;
418 port = of_parse_phandle(np, "ports", i);
422 if (!of_device_is_available(port->parent)) {
427 iommu = of_parse_phandle(port->parent, "iommus", 0);
428 if (!iommu || !of_device_is_available(iommu->parent)) {
429 dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
430 port->parent->full_name);
432 * if there is a crtc not support iommu, force set all
433 * crtc use non-iommu buffer.
435 is_support_iommu = false;
439 drm_of_component_match_add(dev, &match, compare_of,
445 dev_err(dev, "missing 'ports' property\n");
450 dev_err(dev, "No available vop found for display-subsystem.\n");
454 * For each bound crtc, bind the encoders attached to its
458 port = of_parse_phandle(np, "ports", i);
462 if (!of_device_is_available(port->parent)) {
467 rockchip_add_endpoints(dev, &match, port);
471 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
474 static int rockchip_drm_platform_remove(struct platform_device *pdev)
476 component_master_del(&pdev->dev, &rockchip_drm_ops);
481 static const struct of_device_id rockchip_drm_dt_ids[] = {
482 { .compatible = "rockchip,display-subsystem", },
485 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
487 static struct platform_driver rockchip_drm_platform_driver = {
488 .probe = rockchip_drm_platform_probe,
489 .remove = rockchip_drm_platform_remove,
491 .name = "rockchip-drm",
492 .of_match_table = rockchip_drm_dt_ids,
493 .pm = &rockchip_drm_pm_ops,
497 module_platform_driver(rockchip_drm_platform_driver);
499 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
500 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
501 MODULE_LICENSE("GPL v2");