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 const 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 	drm_mode_config_reset(drm);
79 	drm_kms_helper_poll_init(drm);
80 
81 	ret = drm_dev_register(drm, 0);
82 	if (ret)
83 		goto out_poll_fini;
84 
85 	drm_fbdev_generic_setup(drm, 32);
86 
87 	return 0;
88 
89 out_poll_fini:
90 	drm_kms_helper_poll_fini(drm);
91 	drm_mode_config_cleanup(drm);
92 out_unbind:
93 	component_unbind_all(dev, drm);
94 out_unregister:
95 	dev_set_drvdata(dev, NULL);
96 	drm_dev_put(drm);
97 	return ret;
98 }
99 
zx_drm_unbind(struct device * dev)100 static void zx_drm_unbind(struct device *dev)
101 {
102 	struct drm_device *drm = dev_get_drvdata(dev);
103 
104 	drm_dev_unregister(drm);
105 	drm_kms_helper_poll_fini(drm);
106 	drm_atomic_helper_shutdown(drm);
107 	drm_mode_config_cleanup(drm);
108 	component_unbind_all(dev, drm);
109 	dev_set_drvdata(dev, NULL);
110 	drm_dev_put(drm);
111 }
112 
113 static const struct component_master_ops zx_drm_master_ops = {
114 	.bind = zx_drm_bind,
115 	.unbind = zx_drm_unbind,
116 };
117 
compare_of(struct device * dev,void * data)118 static int compare_of(struct device *dev, void *data)
119 {
120 	return dev->of_node == data;
121 }
122 
zx_drm_probe(struct platform_device * pdev)123 static int zx_drm_probe(struct platform_device *pdev)
124 {
125 	struct device *dev = &pdev->dev;
126 	struct device_node *parent = dev->of_node;
127 	struct device_node *child;
128 	struct component_match *match = NULL;
129 	int ret;
130 
131 	ret = devm_of_platform_populate(dev);
132 	if (ret)
133 		return ret;
134 
135 	for_each_available_child_of_node(parent, child)
136 		component_match_add(dev, &match, compare_of, child);
137 
138 	return component_master_add_with_match(dev, &zx_drm_master_ops, match);
139 }
140 
zx_drm_remove(struct platform_device * pdev)141 static int zx_drm_remove(struct platform_device *pdev)
142 {
143 	component_master_del(&pdev->dev, &zx_drm_master_ops);
144 	return 0;
145 }
146 
147 static const struct of_device_id zx_drm_of_match[] = {
148 	{ .compatible = "zte,zx296718-vou", },
149 	{ /* end */ },
150 };
151 MODULE_DEVICE_TABLE(of, zx_drm_of_match);
152 
153 static struct platform_driver zx_drm_platform_driver = {
154 	.probe = zx_drm_probe,
155 	.remove = zx_drm_remove,
156 	.driver	= {
157 		.name = "zx-drm",
158 		.of_match_table	= zx_drm_of_match,
159 	},
160 };
161 
162 static struct platform_driver *drivers[] = {
163 	&zx_crtc_driver,
164 	&zx_hdmi_driver,
165 	&zx_tvenc_driver,
166 	&zx_vga_driver,
167 	&zx_drm_platform_driver,
168 };
169 
zx_drm_init(void)170 static int zx_drm_init(void)
171 {
172 	return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
173 }
174 module_init(zx_drm_init);
175 
zx_drm_exit(void)176 static void zx_drm_exit(void)
177 {
178 	platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
179 }
180 module_exit(zx_drm_exit);
181 
182 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
183 MODULE_DESCRIPTION("ZTE ZX VOU DRM driver");
184 MODULE_LICENSE("GPL v2");
185