2 * Hisilicon Kirin SoCs drm master driver
4 * Copyright (c) 2016 Linaro Limited.
5 * Copyright (c) 2014-2016 Hisilicon Limited.
8 * Xinliang Liu <z.liuxinliang@hisilicon.com>
9 * Xinliang Liu <xinliang.liu@linaro.org>
10 * Xinwei Kong <kong.kongxinwei@hisilicon.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/of_platform.h>
19 #include <linux/component.h>
20 #include <linux/of_graph.h>
23 #include <drm/drm_gem_cma_helper.h>
24 #include <drm/drm_fb_cma_helper.h>
25 #include <drm/drm_atomic_helper.h>
26 #include <drm/drm_crtc_helper.h>
27 #include <drm/drm_of.h>
29 #include "kirin_drm_drv.h"
31 static struct kirin_dc_ops *dc_ops;
33 static int kirin_drm_kms_cleanup(struct drm_device *dev)
35 struct kirin_drm_private *priv = dev->dev_private;
38 drm_fbdev_cma_fini(priv->fbdev);
42 drm_kms_helper_poll_fini(dev);
43 dc_ops->cleanup(to_platform_device(dev->dev));
44 drm_mode_config_cleanup(dev);
45 devm_kfree(dev->dev, priv);
46 dev->dev_private = NULL;
51 static void kirin_fbdev_output_poll_changed(struct drm_device *dev)
53 struct kirin_drm_private *priv = dev->dev_private;
55 drm_fbdev_cma_hotplug_event(priv->fbdev);
58 static const struct drm_mode_config_funcs kirin_drm_mode_config_funcs = {
59 .fb_create = drm_fb_cma_create,
60 .output_poll_changed = kirin_fbdev_output_poll_changed,
61 .atomic_check = drm_atomic_helper_check,
62 .atomic_commit = drm_atomic_helper_commit,
65 static void kirin_drm_mode_config_init(struct drm_device *dev)
67 dev->mode_config.min_width = 0;
68 dev->mode_config.min_height = 0;
70 dev->mode_config.max_width = 2048;
71 dev->mode_config.max_height = 2048;
73 dev->mode_config.funcs = &kirin_drm_mode_config_funcs;
76 static int kirin_drm_kms_init(struct drm_device *dev)
78 struct kirin_drm_private *priv;
81 priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL);
85 dev->dev_private = priv;
86 dev_set_drvdata(dev->dev, dev);
88 /* dev->mode_config initialization */
89 drm_mode_config_init(dev);
90 kirin_drm_mode_config_init(dev);
92 /* display controller init */
93 ret = dc_ops->init(to_platform_device(dev->dev));
95 goto err_mode_config_cleanup;
97 /* bind and init sub drivers */
98 ret = component_bind_all(dev->dev, dev);
100 DRM_ERROR("failed to bind all component.\n");
105 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
107 DRM_ERROR("failed to initialize vblank.\n");
110 /* with irq_enabled = true, we can use the vblank feature. */
111 dev->irq_enabled = true;
113 /* reset all the states of crtc/plane/encoder/connector */
114 drm_mode_config_reset(dev);
116 /* init kms poll for handling hpd */
117 drm_kms_helper_poll_init(dev);
119 priv->fbdev = drm_fbdev_cma_init(dev, 32,
120 dev->mode_config.num_connector);
122 if (IS_ERR(priv->fbdev)) {
123 DRM_ERROR("failed to initialize fbdev.\n");
124 ret = PTR_ERR(priv->fbdev);
125 goto err_cleanup_poll;
130 drm_kms_helper_poll_fini(dev);
132 component_unbind_all(dev->dev, dev);
134 dc_ops->cleanup(to_platform_device(dev->dev));
135 err_mode_config_cleanup:
136 drm_mode_config_cleanup(dev);
137 devm_kfree(dev->dev, priv);
138 dev->dev_private = NULL;
143 DEFINE_DRM_GEM_CMA_FOPS(kirin_drm_fops);
145 static int kirin_gem_cma_dumb_create(struct drm_file *file,
146 struct drm_device *dev,
147 struct drm_mode_create_dumb *args)
149 return drm_gem_cma_dumb_create_internal(file, dev, args);
152 static struct drm_driver kirin_drm_driver = {
153 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
155 .fops = &kirin_drm_fops,
157 .gem_free_object_unlocked = drm_gem_cma_free_object,
158 .gem_vm_ops = &drm_gem_cma_vm_ops,
159 .dumb_create = kirin_gem_cma_dumb_create,
161 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
162 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
163 .gem_prime_export = drm_gem_prime_export,
164 .gem_prime_import = drm_gem_prime_import,
165 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
166 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
167 .gem_prime_vmap = drm_gem_cma_prime_vmap,
168 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
169 .gem_prime_mmap = drm_gem_cma_prime_mmap,
172 .desc = "Hisilicon Kirin SoCs' DRM Driver",
178 static int compare_of(struct device *dev, void *data)
180 return dev->of_node == data;
183 static int kirin_drm_bind(struct device *dev)
185 struct drm_driver *driver = &kirin_drm_driver;
186 struct drm_device *drm_dev;
189 drm_dev = drm_dev_alloc(driver, dev);
191 return PTR_ERR(drm_dev);
193 ret = kirin_drm_kms_init(drm_dev);
195 goto err_drm_dev_unref;
197 ret = drm_dev_register(drm_dev, 0);
199 goto err_kms_cleanup;
204 kirin_drm_kms_cleanup(drm_dev);
206 drm_dev_unref(drm_dev);
211 static void kirin_drm_unbind(struct device *dev)
213 struct drm_device *drm_dev = dev_get_drvdata(dev);
215 drm_dev_unregister(drm_dev);
216 kirin_drm_kms_cleanup(drm_dev);
217 drm_dev_unref(drm_dev);
220 static const struct component_master_ops kirin_drm_ops = {
221 .bind = kirin_drm_bind,
222 .unbind = kirin_drm_unbind,
225 static int kirin_drm_platform_probe(struct platform_device *pdev)
227 struct device *dev = &pdev->dev;
228 struct device_node *np = dev->of_node;
229 struct component_match *match = NULL;
230 struct device_node *remote;
232 dc_ops = (struct kirin_dc_ops *)of_device_get_match_data(dev);
234 DRM_ERROR("failed to get dt id data\n");
238 remote = of_graph_get_remote_node(np, 0, 0);
240 return PTR_ERR(remote);
242 drm_of_component_match_add(dev, &match, compare_of, remote);
245 return component_master_add_with_match(dev, &kirin_drm_ops, match);
250 static int kirin_drm_platform_remove(struct platform_device *pdev)
252 component_master_del(&pdev->dev, &kirin_drm_ops);
257 static const struct of_device_id kirin_drm_dt_ids[] = {
258 { .compatible = "hisilicon,hi6220-ade",
263 MODULE_DEVICE_TABLE(of, kirin_drm_dt_ids);
265 static struct platform_driver kirin_drm_platform_driver = {
266 .probe = kirin_drm_platform_probe,
267 .remove = kirin_drm_platform_remove,
270 .of_match_table = kirin_drm_dt_ids,
274 module_platform_driver(kirin_drm_platform_driver);
276 MODULE_AUTHOR("Xinliang Liu <xinliang.liu@linaro.org>");
277 MODULE_AUTHOR("Xinliang Liu <z.liuxinliang@hisilicon.com>");
278 MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
279 MODULE_DESCRIPTION("hisilicon Kirin SoCs' DRM master driver");
280 MODULE_LICENSE("GPL v2");