1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2016 Linaro Ltd.
4 * Copyright 2016 ZTE Corporation.
5 */
6
7 #include <linux/clk.h>
8 #include <linux/component.h>
9 #include <linux/list.h>
10 #include <linux/module.h>
11 #include <linux/of_graph.h>
12 #include <linux/of_platform.h>
13 #include <linux/spinlock.h>
14
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_drv.h>
18 #include <drm/drm_fb_cma_helper.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #include <drm/drm_of.h>
23 #include <drm/drm_probe_helper.h>
24 #include <drm/drm_vblank.h>
25
26 #include "zx_drm_drv.h"
27 #include "zx_vou.h"
28
29 static const struct drm_mode_config_funcs zx_drm_mode_config_funcs = {
30 .fb_create = drm_gem_fb_create,
31 .atomic_check = drm_atomic_helper_check,
32 .atomic_commit = drm_atomic_helper_commit,
33 };
34
35 DEFINE_DRM_GEM_CMA_FOPS(zx_drm_fops);
36
37 static struct drm_driver zx_drm_driver = {
38 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
39 DRM_GEM_CMA_DRIVER_OPS,
40 .fops = &zx_drm_fops,
41 .name = "zx-vou",
42 .desc = "ZTE VOU Controller DRM",
43 .date = "20160811",
44 .major = 1,
45 .minor = 0,
46 };
47
zx_drm_bind(struct device * dev)48 static int zx_drm_bind(struct device *dev)
49 {
50 struct drm_device *drm;
51 int ret;
52
53 drm = drm_dev_alloc(&zx_drm_driver, dev);
54 if (IS_ERR(drm))
55 return PTR_ERR(drm);
56
57 dev_set_drvdata(dev, drm);
58
59 drm_mode_config_init(drm);
60 drm->mode_config.min_width = 16;
61 drm->mode_config.min_height = 16;
62 drm->mode_config.max_width = 4096;
63 drm->mode_config.max_height = 4096;
64 drm->mode_config.funcs = &zx_drm_mode_config_funcs;
65
66 ret = component_bind_all(dev, drm);
67 if (ret) {
68 DRM_DEV_ERROR(dev, "failed to bind all components: %d\n", ret);
69 goto out_unregister;
70 }
71
72 ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
73 if (ret < 0) {
74 DRM_DEV_ERROR(dev, "failed to init vblank: %d\n", ret);
75 goto out_unbind;
76 }
77
78 /*
79 * We will manage irq handler on our own. In this case, irq_enabled
80 * need to be true for using vblank core support.
81 */
82 drm->irq_enabled = true;
83
84 drm_mode_config_reset(drm);
85 drm_kms_helper_poll_init(drm);
86
87 ret = drm_dev_register(drm, 0);
88 if (ret)
89 goto out_poll_fini;
90
91 drm_fbdev_generic_setup(drm, 32);
92
93 return 0;
94
95 out_poll_fini:
96 drm_kms_helper_poll_fini(drm);
97 drm_mode_config_cleanup(drm);
98 out_unbind:
99 component_unbind_all(dev, drm);
100 out_unregister:
101 dev_set_drvdata(dev, NULL);
102 drm_dev_put(drm);
103 return ret;
104 }
105
zx_drm_unbind(struct device * dev)106 static void zx_drm_unbind(struct device *dev)
107 {
108 struct drm_device *drm = dev_get_drvdata(dev);
109
110 drm_dev_unregister(drm);
111 drm_kms_helper_poll_fini(drm);
112 drm_atomic_helper_shutdown(drm);
113 drm_mode_config_cleanup(drm);
114 component_unbind_all(dev, drm);
115 dev_set_drvdata(dev, NULL);
116 drm_dev_put(drm);
117 }
118
119 static const struct component_master_ops zx_drm_master_ops = {
120 .bind = zx_drm_bind,
121 .unbind = zx_drm_unbind,
122 };
123
compare_of(struct device * dev,void * data)124 static int compare_of(struct device *dev, void *data)
125 {
126 return dev->of_node == data;
127 }
128
zx_drm_probe(struct platform_device * pdev)129 static int zx_drm_probe(struct platform_device *pdev)
130 {
131 struct device *dev = &pdev->dev;
132 struct device_node *parent = dev->of_node;
133 struct device_node *child;
134 struct component_match *match = NULL;
135 int ret;
136
137 ret = devm_of_platform_populate(dev);
138 if (ret)
139 return ret;
140
141 for_each_available_child_of_node(parent, child)
142 component_match_add(dev, &match, compare_of, child);
143
144 return component_master_add_with_match(dev, &zx_drm_master_ops, match);
145 }
146
zx_drm_remove(struct platform_device * pdev)147 static int zx_drm_remove(struct platform_device *pdev)
148 {
149 component_master_del(&pdev->dev, &zx_drm_master_ops);
150 return 0;
151 }
152
153 static const struct of_device_id zx_drm_of_match[] = {
154 { .compatible = "zte,zx296718-vou", },
155 { /* end */ },
156 };
157 MODULE_DEVICE_TABLE(of, zx_drm_of_match);
158
159 static struct platform_driver zx_drm_platform_driver = {
160 .probe = zx_drm_probe,
161 .remove = zx_drm_remove,
162 .driver = {
163 .name = "zx-drm",
164 .of_match_table = zx_drm_of_match,
165 },
166 };
167
168 static struct platform_driver *drivers[] = {
169 &zx_crtc_driver,
170 &zx_hdmi_driver,
171 &zx_tvenc_driver,
172 &zx_vga_driver,
173 &zx_drm_platform_driver,
174 };
175
zx_drm_init(void)176 static int zx_drm_init(void)
177 {
178 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
179 }
180 module_init(zx_drm_init);
181
zx_drm_exit(void)182 static void zx_drm_exit(void)
183 {
184 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
185 }
186 module_exit(zx_drm_exit);
187
188 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
189 MODULE_DESCRIPTION("ZTE ZX VOU DRM driver");
190 MODULE_LICENSE("GPL v2");
191