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 <linux/dma-iommu.h>
20 #include <drm/drm_crtc_helper.h>
21 #include <drm/drm_fb_helper.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/module.h>
25 #include <linux/of_graph.h>
26 #include <linux/component.h>
28 #include "rockchip_drm_drv.h"
29 #include "rockchip_drm_fb.h"
30 #include "rockchip_drm_fbdev.h"
31 #include "rockchip_drm_gem.h"
33 #define DRIVER_NAME "rockchip"
34 #define DRIVER_DESC "RockChip Soc DRM"
35 #define DRIVER_DATE "20140818"
36 #define DRIVER_MAJOR 1
37 #define DRIVER_MINOR 0
40 * Attach a (component) device to the shared drm dma mapping from master drm
41 * device. This is used by the VOPs to map GEM buffers to a common DMA
44 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
47 struct rockchip_drm_private *private = drm_dev->dev_private;
48 struct iommu_domain *domain = private->domain;
51 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
55 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
56 ret = iommu_attach_device(domain, dev);
58 dev_err(dev, "Failed to attach iommu device\n");
62 if (!common_iommu_setup_dma_ops(dev, 0x10000000, SZ_2G, domain->ops)) {
63 dev_err(dev, "Failed to set dma_ops\n");
64 iommu_detach_device(domain, dev);
71 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
74 struct rockchip_drm_private *private = drm_dev->dev_private;
75 struct iommu_domain *domain = private->domain;
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 struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
108 struct drm_crtc *crtc;
111 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
118 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
121 struct rockchip_drm_private *priv = dev->dev_private;
122 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
124 if (crtc && priv->crtc_funcs[pipe] &&
125 priv->crtc_funcs[pipe]->enable_vblank)
126 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
131 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
134 struct rockchip_drm_private *priv = dev->dev_private;
135 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
137 if (crtc && priv->crtc_funcs[pipe] &&
138 priv->crtc_funcs[pipe]->enable_vblank)
139 priv->crtc_funcs[pipe]->disable_vblank(crtc);
142 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
144 struct rockchip_drm_private *private;
145 struct device *dev = drm_dev->dev;
146 struct drm_connector *connector;
147 struct iommu_group *group;
150 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
154 mutex_init(&private->commit.lock);
155 INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
157 drm_dev->dev_private = private;
159 drm_mode_config_init(drm_dev);
161 rockchip_drm_mode_config_init(drm_dev);
163 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
165 if (!dev->dma_parms) {
167 goto err_config_cleanup;
170 private->domain = iommu_domain_alloc(&platform_bus_type);
171 if (!private->domain)
174 ret = iommu_get_dma_cookie(private->domain);
176 goto err_free_domain;
178 group = iommu_group_get(dev);
180 group = iommu_group_alloc();
182 dev_err(dev, "Failed to allocate IOMMU group\n");
186 ret = iommu_group_add_device(group, dev);
187 iommu_group_put(group);
189 dev_err(dev, "failed to add device to IOMMU group\n");
194 * Attach virtual iommu device, sub iommu device can share the same
197 ret = rockchip_drm_dma_attach_device(drm_dev, dev);
199 goto err_group_remove_device;
201 /* Try to bind all sub drivers. */
202 ret = component_bind_all(dev, drm_dev);
204 goto err_detach_device;
207 * All components are now added, we can publish the connector sysfs
208 * entries to userspace. This will generate hotplug events and so
209 * userspace will expect to be able to access DRM at this point.
211 list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
213 ret = drm_connector_register(connector);
215 dev_err(drm_dev->dev,
216 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
218 connector->name, ret);
223 /* init kms poll for handling hpd */
224 drm_kms_helper_poll_init(drm_dev);
227 * enable drm irq mode.
228 * - with irq_enabled = true, we can use the vblank feature.
230 drm_dev->irq_enabled = true;
232 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
234 goto err_kms_helper_poll_fini;
237 * with vblank_disable_allowed = true, vblank interrupt will be disabled
238 * by drm timer once a current process gives up ownership of
239 * vblank event.(after drm_vblank_put function is called)
241 drm_dev->vblank_disable_allowed = true;
243 drm_mode_config_reset(drm_dev);
245 ret = rockchip_drm_fbdev_init(drm_dev);
247 goto err_vblank_cleanup;
251 drm_vblank_cleanup(drm_dev);
252 err_kms_helper_poll_fini:
253 drm_kms_helper_poll_fini(drm_dev);
255 component_unbind_all(dev, drm_dev);
257 rockchip_drm_dma_detach_device(drm_dev, dev);
258 err_group_remove_device:
259 iommu_group_remove_device(dev);
261 iommu_put_dma_cookie(private->domain);
263 iommu_domain_free(private->domain);
265 drm_mode_config_cleanup(drm_dev);
266 drm_dev->dev_private = NULL;
270 static int rockchip_drm_unload(struct drm_device *drm_dev)
272 struct device *dev = drm_dev->dev;
273 struct rockchip_drm_private *private = drm_dev->dev_private;
275 rockchip_drm_fbdev_fini(drm_dev);
276 drm_vblank_cleanup(drm_dev);
277 drm_kms_helper_poll_fini(drm_dev);
278 component_unbind_all(dev, drm_dev);
279 rockchip_drm_dma_detach_device(drm_dev, dev);
280 iommu_group_remove_device(dev);
281 iommu_put_dma_cookie(private->domain);
282 iommu_domain_free(private->domain);
283 drm_mode_config_cleanup(drm_dev);
284 drm_dev->dev_private = NULL;
289 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
290 struct drm_file *file_priv)
292 struct rockchip_drm_private *priv = crtc->dev->dev_private;
293 int pipe = drm_crtc_index(crtc);
295 if (pipe < ROCKCHIP_MAX_CRTC &&
296 priv->crtc_funcs[pipe] &&
297 priv->crtc_funcs[pipe]->cancel_pending_vblank)
298 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
301 static void rockchip_drm_preclose(struct drm_device *dev,
302 struct drm_file *file_priv)
304 struct drm_crtc *crtc;
306 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
307 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
310 void rockchip_drm_lastclose(struct drm_device *dev)
312 struct rockchip_drm_private *priv = dev->dev_private;
314 drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
317 static const struct file_operations rockchip_drm_driver_fops = {
318 .owner = THIS_MODULE,
320 .mmap = rockchip_gem_mmap,
323 .unlocked_ioctl = drm_ioctl,
325 .compat_ioctl = drm_compat_ioctl,
327 .release = drm_release,
330 const struct vm_operations_struct rockchip_drm_vm_ops = {
331 .open = drm_gem_vm_open,
332 .close = drm_gem_vm_close,
335 static struct drm_driver rockchip_drm_driver = {
336 .driver_features = DRIVER_MODESET | DRIVER_GEM |
337 DRIVER_PRIME | DRIVER_ATOMIC,
338 .load = rockchip_drm_load,
339 .unload = rockchip_drm_unload,
340 .preclose = rockchip_drm_preclose,
341 .lastclose = rockchip_drm_lastclose,
342 .get_vblank_counter = drm_vblank_no_hw_counter,
343 .enable_vblank = rockchip_drm_crtc_enable_vblank,
344 .disable_vblank = rockchip_drm_crtc_disable_vblank,
345 .gem_vm_ops = &rockchip_drm_vm_ops,
346 .gem_free_object = rockchip_gem_free_object,
347 .dumb_create = rockchip_gem_dumb_create,
348 .dumb_map_offset = rockchip_gem_dumb_map_offset,
349 .dumb_destroy = drm_gem_dumb_destroy,
350 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
351 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
352 .gem_prime_import = drm_gem_prime_import,
353 .gem_prime_export = drm_gem_prime_export,
354 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
355 .gem_prime_vmap = rockchip_gem_prime_vmap,
356 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
357 .gem_prime_mmap = rockchip_gem_mmap_buf,
358 .fops = &rockchip_drm_driver_fops,
362 .major = DRIVER_MAJOR,
363 .minor = DRIVER_MINOR,
366 #ifdef CONFIG_PM_SLEEP
367 static int rockchip_drm_sys_suspend(struct device *dev)
369 struct drm_device *drm = dev_get_drvdata(dev);
370 struct drm_connector *connector;
375 drm_modeset_lock_all(drm);
376 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
377 int old_dpms = connector->dpms;
379 if (connector->funcs->dpms)
380 connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
382 /* Set the old mode back to the connector for resume */
383 connector->dpms = old_dpms;
385 drm_modeset_unlock_all(drm);
390 static int rockchip_drm_sys_resume(struct device *dev)
392 struct drm_device *drm = dev_get_drvdata(dev);
393 struct drm_connector *connector;
394 enum drm_connector_status status;
395 bool changed = false;
400 drm_modeset_lock_all(drm);
401 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
402 int desired_mode = connector->dpms;
405 * at suspend time, we save dpms to connector->dpms,
406 * restore the old_dpms, and at current time, the connector
407 * dpms status must be DRM_MODE_DPMS_OFF.
409 connector->dpms = DRM_MODE_DPMS_OFF;
412 * If the connector has been disconnected during suspend,
413 * disconnect it from the encoder and leave it off. We'll notify
414 * userspace at the end.
416 if (desired_mode == DRM_MODE_DPMS_ON) {
417 status = connector->funcs->detect(connector, true);
418 if (status == connector_status_disconnected) {
419 connector->encoder = NULL;
420 connector->status = status;
425 if (connector->funcs->dpms)
426 connector->funcs->dpms(connector, desired_mode);
428 drm_modeset_unlock_all(drm);
430 drm_helper_resume_force_mode(drm);
433 drm_kms_helper_hotplug_event(drm);
439 static const struct dev_pm_ops rockchip_drm_pm_ops = {
440 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
441 rockchip_drm_sys_resume)
445 * @node: device tree node containing encoder input ports
446 * @encoder: drm_encoder
448 int rockchip_drm_encoder_get_mux_id(struct device_node *node,
449 struct drm_encoder *encoder)
451 struct device_node *ep;
452 struct drm_crtc *crtc = encoder->crtc;
453 struct of_endpoint endpoint;
454 struct device_node *port;
460 for_each_endpoint_of_node(node, ep) {
461 port = of_graph_get_remote_port(ep);
463 if (port == crtc->port) {
464 ret = of_graph_parse_endpoint(ep, &endpoint);
466 return ret ?: endpoint.id;
472 EXPORT_SYMBOL_GPL(rockchip_drm_encoder_get_mux_id);
474 static int compare_of(struct device *dev, void *data)
476 struct device_node *np = data;
478 return dev->of_node == np;
481 static void rockchip_add_endpoints(struct device *dev,
482 struct component_match **match,
483 struct device_node *port)
485 struct device_node *ep, *remote;
487 for_each_child_of_node(port, ep) {
488 remote = of_graph_get_remote_port_parent(ep);
489 if (!remote || !of_device_is_available(remote)) {
492 } else if (!of_device_is_available(remote->parent)) {
493 dev_warn(dev, "parent device of %s is not available\n",
499 component_match_add(dev, match, compare_of, remote);
504 static int rockchip_drm_bind(struct device *dev)
506 struct drm_device *drm;
509 drm = drm_dev_alloc(&rockchip_drm_driver, dev);
513 ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
517 ret = drm_dev_register(drm, 0);
521 dev_set_drvdata(dev, drm);
530 static void rockchip_drm_unbind(struct device *dev)
532 struct drm_device *drm = dev_get_drvdata(dev);
534 drm_dev_unregister(drm);
536 dev_set_drvdata(dev, NULL);
539 static const struct component_master_ops rockchip_drm_ops = {
540 .bind = rockchip_drm_bind,
541 .unbind = rockchip_drm_unbind,
544 static int rockchip_drm_platform_probe(struct platform_device *pdev)
546 struct device *dev = &pdev->dev;
547 struct component_match *match = NULL;
548 struct device_node *np = dev->of_node;
549 struct device_node *port;
555 * Bind the crtc ports first, so that
556 * drm_of_find_possible_crtcs called from encoder .bind callbacks
560 port = of_parse_phandle(np, "ports", i);
564 if (!of_device_is_available(port->parent)) {
569 component_match_add(dev, &match, compare_of, port->parent);
574 dev_err(dev, "missing 'ports' property\n");
579 dev_err(dev, "No available vop found for display-subsystem.\n");
583 * For each bound crtc, bind the encoders attached to its
587 port = of_parse_phandle(np, "ports", i);
591 if (!of_device_is_available(port->parent)) {
596 rockchip_add_endpoints(dev, &match, port);
600 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
603 static int rockchip_drm_platform_remove(struct platform_device *pdev)
605 component_master_del(&pdev->dev, &rockchip_drm_ops);
610 static const struct of_device_id rockchip_drm_dt_ids[] = {
611 { .compatible = "rockchip,display-subsystem", },
614 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
616 static struct platform_driver rockchip_drm_platform_driver = {
617 .probe = rockchip_drm_platform_probe,
618 .remove = rockchip_drm_platform_remove,
620 .name = "rockchip-drm",
621 .of_match_table = rockchip_drm_dt_ids,
622 .pm = &rockchip_drm_pm_ops,
626 module_platform_driver(rockchip_drm_platform_driver);
628 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
629 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
630 MODULE_LICENSE("GPL v2");