1 // SPDX-License-Identifier: GPL-2.0-only
3 * Hisilicon Kirin SoCs drm master driver
5 * Copyright (c) 2016 Linaro Limited.
6 * Copyright (c) 2014-2016 Hisilicon Limited.
9 * Xinliang Liu <z.liuxinliang@hisilicon.com>
10 * Xinliang Liu <xinliang.liu@linaro.org>
11 * Xinwei Kong <kong.kongxinwei@hisilicon.com>
14 #include <linux/of_platform.h>
15 #include <linux/component.h>
16 #include <linux/module.h>
17 #include <linux/of_graph.h>
18 #include <linux/platform_device.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_fb_cma_helper.h>
23 #include <drm/drm_fb_helper.h>
24 #include <drm/drm_gem_cma_helper.h>
25 #include <drm/drm_gem_framebuffer_helper.h>
26 #include <drm/drm_of.h>
27 #include <drm/drm_probe_helper.h>
28 #include <drm/drm_vblank.h>
30 #include "kirin_drm_drv.h"
32 static struct kirin_drm_data *driver_data;
34 static int kirin_drm_kms_cleanup(struct drm_device *dev)
36 drm_kms_helper_poll_fini(dev);
37 driver_data->cleanup(to_platform_device(dev->dev));
38 drm_mode_config_cleanup(dev);
43 static int kirin_drm_kms_init(struct drm_device *dev)
47 dev_set_drvdata(dev->dev, dev);
49 /* dev->mode_config initialization */
50 drm_mode_config_init(dev);
51 dev->mode_config.min_width = 0;
52 dev->mode_config.min_height = 0;
53 dev->mode_config.max_width = driver_data->config_max_width;
54 dev->mode_config.max_height = driver_data->config_max_width;
55 dev->mode_config.funcs = driver_data->mode_config_funcs;
57 /* display controller init */
58 ret = driver_data->init(to_platform_device(dev->dev));
60 goto err_mode_config_cleanup;
62 /* bind and init sub drivers */
63 ret = component_bind_all(dev->dev, dev);
65 DRM_ERROR("failed to bind all component.\n");
70 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
72 DRM_ERROR("failed to initialize vblank.\n");
75 /* with irq_enabled = true, we can use the vblank feature. */
76 dev->irq_enabled = true;
78 /* reset all the states of crtc/plane/encoder/connector */
79 drm_mode_config_reset(dev);
81 /* init kms poll for handling hpd */
82 drm_kms_helper_poll_init(dev);
87 component_unbind_all(dev->dev, dev);
89 driver_data->cleanup(to_platform_device(dev->dev));
90 err_mode_config_cleanup:
91 drm_mode_config_cleanup(dev);
96 static int compare_of(struct device *dev, void *data)
98 return dev->of_node == data;
101 static int kirin_drm_bind(struct device *dev)
103 struct drm_device *drm_dev;
106 drm_dev = drm_dev_alloc(driver_data->driver, dev);
108 return PTR_ERR(drm_dev);
110 ret = kirin_drm_kms_init(drm_dev);
112 goto err_drm_dev_put;
114 ret = drm_dev_register(drm_dev, 0);
116 goto err_kms_cleanup;
118 drm_fbdev_generic_setup(drm_dev, 32);
123 kirin_drm_kms_cleanup(drm_dev);
125 drm_dev_put(drm_dev);
130 static void kirin_drm_unbind(struct device *dev)
132 struct drm_device *drm_dev = dev_get_drvdata(dev);
134 drm_dev_unregister(drm_dev);
135 kirin_drm_kms_cleanup(drm_dev);
136 drm_dev_put(drm_dev);
139 static const struct component_master_ops kirin_drm_ops = {
140 .bind = kirin_drm_bind,
141 .unbind = kirin_drm_unbind,
144 static int kirin_drm_platform_probe(struct platform_device *pdev)
146 struct device *dev = &pdev->dev;
147 struct device_node *np = dev->of_node;
148 struct component_match *match = NULL;
149 struct device_node *remote;
151 driver_data = (struct kirin_drm_data *)of_device_get_match_data(dev);
153 DRM_ERROR("failed to get dt id data\n");
157 remote = of_graph_get_remote_node(np, 0, 0);
161 drm_of_component_match_add(dev, &match, compare_of, remote);
164 return component_master_add_with_match(dev, &kirin_drm_ops, match);
167 static int kirin_drm_platform_remove(struct platform_device *pdev)
169 component_master_del(&pdev->dev, &kirin_drm_ops);
174 static const struct of_device_id kirin_drm_dt_ids[] = {
175 { .compatible = "hisilicon,hi6220-ade",
176 .data = &ade_driver_data,
180 MODULE_DEVICE_TABLE(of, kirin_drm_dt_ids);
182 static struct platform_driver kirin_drm_platform_driver = {
183 .probe = kirin_drm_platform_probe,
184 .remove = kirin_drm_platform_remove,
187 .of_match_table = kirin_drm_dt_ids,
191 module_platform_driver(kirin_drm_platform_driver);
193 MODULE_AUTHOR("Xinliang Liu <xinliang.liu@linaro.org>");
194 MODULE_AUTHOR("Xinliang Liu <z.liuxinliang@hisilicon.com>");
195 MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
196 MODULE_DESCRIPTION("hisilicon Kirin SoCs' DRM master driver");
197 MODULE_LICENSE("GPL v2");