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 <linux/dma-mapping.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/module.h>
26 #include <linux/of_graph.h>
27 #include <linux/component.h>
29 #include "rockchip_drm_drv.h"
30 #include "rockchip_drm_fb.h"
31 #include "rockchip_drm_fbdev.h"
32 #include "rockchip_drm_gem.h"
34 #define DRIVER_NAME "rockchip"
35 #define DRIVER_DESC "RockChip Soc DRM"
36 #define DRIVER_DATE "20140818"
37 #define DRIVER_MAJOR 1
38 #define DRIVER_MINOR 0
40 static bool is_support_iommu = true;
43 * Attach a (component) device to the shared drm dma mapping from master drm
44 * device. This is used by the VOPs to map GEM buffers to a common DMA
47 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
50 struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping;
53 if (!is_support_iommu)
56 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
60 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
62 return arm_iommu_attach_device(dev, mapping);
65 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
68 if (!is_support_iommu)
71 arm_iommu_detach_device(dev);
74 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
75 const struct rockchip_crtc_funcs *crtc_funcs)
77 int pipe = drm_crtc_index(crtc);
78 struct rockchip_drm_private *priv = crtc->dev->dev_private;
80 if (pipe > ROCKCHIP_MAX_CRTC)
83 priv->crtc_funcs[pipe] = crtc_funcs;
88 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
90 int pipe = drm_crtc_index(crtc);
91 struct rockchip_drm_private *priv = crtc->dev->dev_private;
93 if (pipe > ROCKCHIP_MAX_CRTC)
96 priv->crtc_funcs[pipe] = NULL;
99 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
102 struct drm_crtc *crtc;
105 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
112 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
115 struct rockchip_drm_private *priv = dev->dev_private;
116 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
118 if (crtc && priv->crtc_funcs[pipe] &&
119 priv->crtc_funcs[pipe]->enable_vblank)
120 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
125 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
128 struct rockchip_drm_private *priv = dev->dev_private;
129 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
131 if (crtc && priv->crtc_funcs[pipe] &&
132 priv->crtc_funcs[pipe]->enable_vblank)
133 priv->crtc_funcs[pipe]->disable_vblank(crtc);
136 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
138 struct rockchip_drm_private *private;
139 struct dma_iommu_mapping *mapping = NULL;
140 struct device *dev = drm_dev->dev;
141 struct drm_connector *connector;
144 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
148 mutex_init(&private->commit.lock);
149 INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
151 drm_dev->dev_private = private;
153 drm_mode_config_init(drm_dev);
155 rockchip_drm_mode_config_init(drm_dev);
157 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
159 if (!dev->dma_parms) {
161 goto err_config_cleanup;
164 if (is_support_iommu) {
165 /* TODO(djkurtz): fetch the mapping start/size from somewhere */
166 mapping = arm_iommu_create_mapping(&platform_bus_type,
169 if (IS_ERR(mapping)) {
170 ret = PTR_ERR(mapping);
171 goto err_config_cleanup;
174 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
176 goto err_release_mapping;
178 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
180 ret = arm_iommu_attach_device(dev, mapping);
182 goto err_release_mapping;
185 /* Try to bind all sub drivers. */
186 ret = component_bind_all(dev, drm_dev);
188 goto err_detach_device;
191 * All components are now added, we can publish the connector sysfs
192 * entries to userspace. This will generate hotplug events and so
193 * userspace will expect to be able to access DRM at this point.
195 list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
197 ret = drm_connector_register(connector);
199 dev_err(drm_dev->dev,
200 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
202 connector->name, ret);
207 /* init kms poll for handling hpd */
208 drm_kms_helper_poll_init(drm_dev);
211 * enable drm irq mode.
212 * - with irq_enabled = true, we can use the vblank feature.
214 drm_dev->irq_enabled = true;
216 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
218 goto err_kms_helper_poll_fini;
220 drm_mode_config_reset(drm_dev);
222 ret = rockchip_drm_fbdev_init(drm_dev);
224 goto err_vblank_cleanup;
226 if (is_support_iommu)
227 arm_iommu_release_mapping(mapping);
230 drm_vblank_cleanup(drm_dev);
231 err_kms_helper_poll_fini:
232 drm_kms_helper_poll_fini(drm_dev);
234 component_unbind_all(dev, drm_dev);
236 if (is_support_iommu)
237 arm_iommu_detach_device(dev);
239 if (is_support_iommu)
240 arm_iommu_release_mapping(mapping);
242 drm_mode_config_cleanup(drm_dev);
243 drm_dev->dev_private = NULL;
247 static int rockchip_drm_unload(struct drm_device *drm_dev)
249 struct device *dev = drm_dev->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;
263 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
264 struct drm_file *file_priv)
266 struct rockchip_drm_private *priv = crtc->dev->dev_private;
267 int pipe = drm_crtc_index(crtc);
269 if (pipe < ROCKCHIP_MAX_CRTC &&
270 priv->crtc_funcs[pipe] &&
271 priv->crtc_funcs[pipe]->cancel_pending_vblank)
272 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
275 static void rockchip_drm_preclose(struct drm_device *dev,
276 struct drm_file *file_priv)
278 struct drm_crtc *crtc;
280 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
281 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
284 void rockchip_drm_lastclose(struct drm_device *dev)
286 struct rockchip_drm_private *priv = dev->dev_private;
288 drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
291 static const struct file_operations rockchip_drm_driver_fops = {
292 .owner = THIS_MODULE,
294 .mmap = rockchip_gem_mmap,
297 .unlocked_ioctl = drm_ioctl,
299 .compat_ioctl = drm_compat_ioctl,
301 .release = drm_release,
304 static struct drm_driver rockchip_drm_driver = {
305 .driver_features = DRIVER_MODESET | DRIVER_GEM |
306 DRIVER_PRIME | DRIVER_ATOMIC,
307 .load = rockchip_drm_load,
308 .unload = rockchip_drm_unload,
309 .preclose = rockchip_drm_preclose,
310 .lastclose = rockchip_drm_lastclose,
311 .get_vblank_counter = drm_vblank_no_hw_counter,
312 .enable_vblank = rockchip_drm_crtc_enable_vblank,
313 .disable_vblank = rockchip_drm_crtc_disable_vblank,
314 .gem_vm_ops = &drm_gem_cma_vm_ops,
315 .gem_free_object_unlocked = rockchip_gem_free_object,
316 .dumb_create = rockchip_gem_dumb_create,
317 .dumb_map_offset = rockchip_gem_dumb_map_offset,
318 .dumb_destroy = drm_gem_dumb_destroy,
319 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
320 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
321 .gem_prime_import = drm_gem_prime_import,
322 .gem_prime_export = drm_gem_prime_export,
323 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
324 .gem_prime_vmap = rockchip_gem_prime_vmap,
325 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
326 .gem_prime_mmap = rockchip_gem_mmap_buf,
327 .fops = &rockchip_drm_driver_fops,
331 .major = DRIVER_MAJOR,
332 .minor = DRIVER_MINOR,
335 #ifdef CONFIG_PM_SLEEP
336 static int rockchip_drm_sys_suspend(struct device *dev)
338 struct drm_device *drm = dev_get_drvdata(dev);
339 struct drm_connector *connector;
344 drm_modeset_lock_all(drm);
345 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
346 int old_dpms = connector->dpms;
348 if (connector->funcs->dpms)
349 connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
351 /* Set the old mode back to the connector for resume */
352 connector->dpms = old_dpms;
354 drm_modeset_unlock_all(drm);
359 static int rockchip_drm_sys_resume(struct device *dev)
361 struct drm_device *drm = dev_get_drvdata(dev);
362 struct drm_connector *connector;
363 enum drm_connector_status status;
364 bool changed = false;
369 drm_modeset_lock_all(drm);
370 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
371 int desired_mode = connector->dpms;
374 * at suspend time, we save dpms to connector->dpms,
375 * restore the old_dpms, and at current time, the connector
376 * dpms status must be DRM_MODE_DPMS_OFF.
378 connector->dpms = DRM_MODE_DPMS_OFF;
381 * If the connector has been disconnected during suspend,
382 * disconnect it from the encoder and leave it off. We'll notify
383 * userspace at the end.
385 if (desired_mode == DRM_MODE_DPMS_ON) {
386 status = connector->funcs->detect(connector, true);
387 if (status == connector_status_disconnected) {
388 connector->encoder = NULL;
389 connector->status = status;
394 if (connector->funcs->dpms)
395 connector->funcs->dpms(connector, desired_mode);
397 drm_modeset_unlock_all(drm);
399 drm_helper_resume_force_mode(drm);
402 drm_kms_helper_hotplug_event(drm);
408 static const struct dev_pm_ops rockchip_drm_pm_ops = {
409 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
410 rockchip_drm_sys_resume)
413 static int compare_of(struct device *dev, void *data)
415 struct device_node *np = data;
417 return dev->of_node == np;
420 static void rockchip_add_endpoints(struct device *dev,
421 struct component_match **match,
422 struct device_node *port)
424 struct device_node *ep, *remote;
426 for_each_child_of_node(port, ep) {
427 remote = of_graph_get_remote_port_parent(ep);
428 if (!remote || !of_device_is_available(remote)) {
431 } else if (!of_device_is_available(remote->parent)) {
432 dev_warn(dev, "parent device of %s is not available\n",
438 component_match_add(dev, match, compare_of, remote);
443 static int rockchip_drm_bind(struct device *dev)
445 struct drm_device *drm;
448 drm = drm_dev_alloc(&rockchip_drm_driver, dev);
452 ret = drm_dev_register(drm, 0);
456 dev_set_drvdata(dev, drm);
465 static void rockchip_drm_unbind(struct device *dev)
467 struct drm_device *drm = dev_get_drvdata(dev);
469 drm_dev_unregister(drm);
471 dev_set_drvdata(dev, NULL);
474 static const struct component_master_ops rockchip_drm_ops = {
475 .bind = rockchip_drm_bind,
476 .unbind = rockchip_drm_unbind,
479 static int rockchip_drm_platform_probe(struct platform_device *pdev)
481 struct device *dev = &pdev->dev;
482 struct component_match *match = NULL;
483 struct device_node *np = dev->of_node;
484 struct device_node *port;
490 * Bind the crtc ports first, so that
491 * drm_of_find_possible_crtcs called from encoder .bind callbacks
495 struct device_node *iommu;
497 port = of_parse_phandle(np, "ports", i);
501 if (!of_device_is_available(port->parent)) {
506 iommu = of_parse_phandle(port->parent, "iommus", 0);
507 if (!iommu || !of_device_is_available(iommu->parent)) {
508 dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
509 port->parent->full_name);
511 * if there is a crtc not support iommu, force set all
512 * crtc use non-iommu buffer.
514 is_support_iommu = false;
517 component_match_add(dev, &match, compare_of, port->parent);
522 dev_err(dev, "missing 'ports' property\n");
527 dev_err(dev, "No available vop found for display-subsystem.\n");
531 * For each bound crtc, bind the encoders attached to its
535 port = of_parse_phandle(np, "ports", i);
539 if (!of_device_is_available(port->parent)) {
544 rockchip_add_endpoints(dev, &match, port);
548 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
551 static int rockchip_drm_platform_remove(struct platform_device *pdev)
553 component_master_del(&pdev->dev, &rockchip_drm_ops);
558 static const struct of_device_id rockchip_drm_dt_ids[] = {
559 { .compatible = "rockchip,display-subsystem", },
562 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
564 static struct platform_driver rockchip_drm_platform_driver = {
565 .probe = rockchip_drm_platform_probe,
566 .remove = rockchip_drm_platform_remove,
568 .name = "rockchip-drm",
569 .of_match_table = rockchip_drm_dt_ids,
570 .pm = &rockchip_drm_pm_ops,
574 module_platform_driver(rockchip_drm_platform_driver);
576 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
577 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
578 MODULE_LICENSE("GPL v2");