Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2012 Russell King
4 */
5
6#include <linux/aperture.h>
7#include <linux/clk.h>
8#include <linux/component.h>
9#include <linux/module.h>
10#include <linux/of.h>
11#include <linux/of_graph.h>
12#include <linux/platform_device.h>
13
14#include <drm/drm_atomic_helper.h>
15#include <drm/drm_client_setup.h>
16#include <drm/drm_drv.h>
17#include <drm/drm_ioctl.h>
18#include <drm/drm_managed.h>
19#include <drm/drm_prime.h>
20#include <drm/drm_probe_helper.h>
21#include <drm/drm_of.h>
22#include <drm/drm_vblank.h>
23
24#include "armada_crtc.h"
25#include "armada_drm.h"
26#include "armada_gem.h"
27#include "armada_fb.h"
28#include "armada_hw.h"
29#include <drm/armada_drm.h>
30#include "armada_ioctlP.h"
31
32static const struct drm_ioctl_desc armada_ioctls[] = {
33 DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
34 DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
35 DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
36};
37
38DEFINE_DRM_GEM_FOPS(armada_drm_fops);
39
40static const struct drm_driver armada_drm_driver = {
41 .gem_prime_import = armada_gem_prime_import,
42 .dumb_create = armada_gem_dumb_create,
43 ARMADA_FBDEV_DRIVER_OPS,
44 .major = 1,
45 .minor = 0,
46 .name = "armada-drm",
47 .desc = "Armada SoC DRM",
48 .date = "20120730",
49 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
50 .ioctls = armada_ioctls,
51 .num_ioctls = ARRAY_SIZE(armada_ioctls),
52 .fops = &armada_drm_fops,
53};
54
55static const struct drm_mode_config_funcs armada_drm_mode_config_funcs = {
56 .fb_create = armada_fb_create,
57 .atomic_check = drm_atomic_helper_check,
58 .atomic_commit = drm_atomic_helper_commit,
59};
60
61static int armada_drm_bind(struct device *dev)
62{
63 struct armada_private *priv;
64 struct resource *mem = NULL;
65 int ret, n;
66
67 for (n = 0; ; n++) {
68 struct resource *r = platform_get_resource(to_platform_device(dev),
69 IORESOURCE_MEM, n);
70 if (!r)
71 break;
72
73 /* Resources above 64K are graphics memory */
74 if (resource_size(r) > SZ_64K)
75 mem = r;
76 else
77 return -EINVAL;
78 }
79
80 if (!mem)
81 return -ENXIO;
82
83 if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
84 "armada-drm"))
85 return -EBUSY;
86
87 priv = devm_drm_dev_alloc(dev, &armada_drm_driver,
88 struct armada_private, drm);
89 if (IS_ERR(priv)) {
90 dev_err(dev, "[" DRM_NAME ":%s] devm_drm_dev_alloc failed: %li\n",
91 __func__, PTR_ERR(priv));
92 return PTR_ERR(priv);
93 }
94
95 /* Remove early framebuffers */
96 ret = aperture_remove_all_conflicting_devices(armada_drm_driver.name);
97 if (ret) {
98 dev_err(dev, "[" DRM_NAME ":%s] can't kick out simple-fb: %d\n",
99 __func__, ret);
100 return ret;
101 }
102
103 dev_set_drvdata(dev, &priv->drm);
104
105 /* Mode setting support */
106 drm_mode_config_init(&priv->drm);
107 priv->drm.mode_config.min_width = 320;
108 priv->drm.mode_config.min_height = 200;
109
110 /*
111 * With vscale enabled, the maximum width is 1920 due to the
112 * 1920 by 3 lines RAM
113 */
114 priv->drm.mode_config.max_width = 1920;
115 priv->drm.mode_config.max_height = 2048;
116
117 priv->drm.mode_config.preferred_depth = 24;
118 priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs;
119 drm_mm_init(&priv->linear, mem->start, resource_size(mem));
120 mutex_init(&priv->linear_lock);
121
122 ret = component_bind_all(dev, &priv->drm);
123 if (ret)
124 goto err_kms;
125
126 ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
127 if (ret)
128 goto err_comp;
129
130 drm_mode_config_reset(&priv->drm);
131
132 drm_kms_helper_poll_init(&priv->drm);
133
134 ret = drm_dev_register(&priv->drm, 0);
135 if (ret)
136 goto err_poll;
137
138#ifdef CONFIG_DEBUG_FS
139 armada_drm_debugfs_init(priv->drm.primary);
140#endif
141
142 drm_client_setup(&priv->drm, NULL);
143
144 return 0;
145
146 err_poll:
147 drm_kms_helper_poll_fini(&priv->drm);
148 err_comp:
149 component_unbind_all(dev, &priv->drm);
150 err_kms:
151 drm_mode_config_cleanup(&priv->drm);
152 drm_mm_takedown(&priv->linear);
153 dev_set_drvdata(dev, NULL);
154 return ret;
155}
156
157static void armada_drm_unbind(struct device *dev)
158{
159 struct drm_device *drm = dev_get_drvdata(dev);
160 struct armada_private *priv = drm_to_armada_dev(drm);
161
162 drm_kms_helper_poll_fini(&priv->drm);
163
164 drm_dev_unregister(&priv->drm);
165
166 drm_atomic_helper_shutdown(&priv->drm);
167
168 component_unbind_all(dev, &priv->drm);
169
170 drm_mode_config_cleanup(&priv->drm);
171 drm_mm_takedown(&priv->linear);
172 dev_set_drvdata(dev, NULL);
173}
174
175static void armada_add_endpoints(struct device *dev,
176 struct component_match **match, struct device_node *dev_node)
177{
178 struct device_node *ep, *remote;
179
180 for_each_endpoint_of_node(dev_node, ep) {
181 remote = of_graph_get_remote_port_parent(ep);
182 if (remote && of_device_is_available(remote))
183 drm_of_component_match_add(dev, match, component_compare_of,
184 remote);
185 of_node_put(remote);
186 }
187}
188
189static const struct component_master_ops armada_master_ops = {
190 .bind = armada_drm_bind,
191 .unbind = armada_drm_unbind,
192};
193
194static int armada_drm_probe(struct platform_device *pdev)
195{
196 struct component_match *match = NULL;
197 struct device *dev = &pdev->dev;
198 int ret;
199
200 ret = drm_of_component_probe(dev, component_compare_dev_name, &armada_master_ops);
201 if (ret != -EINVAL)
202 return ret;
203
204 if (dev->platform_data) {
205 char **devices = dev->platform_data;
206 struct device *d;
207 int i;
208
209 for (i = 0; devices[i]; i++)
210 component_match_add(dev, &match, component_compare_dev_name,
211 devices[i]);
212
213 if (i == 0) {
214 dev_err(dev, "missing 'ports' property\n");
215 return -ENODEV;
216 }
217
218 for (i = 0; devices[i]; i++) {
219 d = bus_find_device_by_name(&platform_bus_type, NULL,
220 devices[i]);
221 if (d && d->of_node)
222 armada_add_endpoints(dev, &match, d->of_node);
223 put_device(d);
224 }
225 }
226
227 return component_master_add_with_match(&pdev->dev, &armada_master_ops,
228 match);
229}
230
231static void armada_drm_remove(struct platform_device *pdev)
232{
233 component_master_del(&pdev->dev, &armada_master_ops);
234}
235
236static void armada_drm_shutdown(struct platform_device *pdev)
237{
238 drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
239}
240
241static const struct platform_device_id armada_drm_platform_ids[] = {
242 {
243 .name = "armada-drm",
244 }, {
245 .name = "armada-510-drm",
246 },
247 { },
248};
249MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
250
251static struct platform_driver armada_drm_platform_driver = {
252 .probe = armada_drm_probe,
253 .remove = armada_drm_remove,
254 .shutdown = armada_drm_shutdown,
255 .driver = {
256 .name = "armada-drm",
257 },
258 .id_table = armada_drm_platform_ids,
259};
260
261static int __init armada_drm_init(void)
262{
263 int ret;
264
265 if (drm_firmware_drivers_only())
266 return -ENODEV;
267
268 ret = platform_driver_register(&armada_lcd_platform_driver);
269 if (ret)
270 return ret;
271 ret = platform_driver_register(&armada_drm_platform_driver);
272 if (ret)
273 platform_driver_unregister(&armada_lcd_platform_driver);
274 return ret;
275}
276module_init(armada_drm_init);
277
278static void __exit armada_drm_exit(void)
279{
280 platform_driver_unregister(&armada_drm_platform_driver);
281 platform_driver_unregister(&armada_lcd_platform_driver);
282}
283module_exit(armada_drm_exit);
284
285MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
286MODULE_DESCRIPTION("Armada DRM Driver");
287MODULE_LICENSE("GPL");
288MODULE_ALIAS("platform:armada-drm");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2012 Russell King
4 */
5
6#include <linux/clk.h>
7#include <linux/component.h>
8#include <linux/module.h>
9#include <linux/of_graph.h>
10#include <linux/platform_device.h>
11
12#include <drm/drm_atomic_helper.h>
13#include <drm/drm_drv.h>
14#include <drm/drm_ioctl.h>
15#include <drm/drm_managed.h>
16#include <drm/drm_prime.h>
17#include <drm/drm_probe_helper.h>
18#include <drm/drm_fb_helper.h>
19#include <drm/drm_of.h>
20#include <drm/drm_vblank.h>
21
22#include "armada_crtc.h"
23#include "armada_drm.h"
24#include "armada_gem.h"
25#include "armada_fb.h"
26#include "armada_hw.h"
27#include <drm/armada_drm.h>
28#include "armada_ioctlP.h"
29
30static struct drm_ioctl_desc armada_ioctls[] = {
31 DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
32 DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
33 DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
34};
35
36DEFINE_DRM_GEM_FOPS(armada_drm_fops);
37
38static struct drm_driver armada_drm_driver = {
39 .lastclose = drm_fb_helper_lastclose,
40 .gem_free_object_unlocked = armada_gem_free_object,
41 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
42 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
43 .gem_prime_export = armada_gem_prime_export,
44 .gem_prime_import = armada_gem_prime_import,
45 .dumb_create = armada_gem_dumb_create,
46 .gem_vm_ops = &armada_gem_vm_ops,
47 .major = 1,
48 .minor = 0,
49 .name = "armada-drm",
50 .desc = "Armada SoC DRM",
51 .date = "20120730",
52 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
53 .ioctls = armada_ioctls,
54 .fops = &armada_drm_fops,
55};
56
57static const struct drm_mode_config_funcs armada_drm_mode_config_funcs = {
58 .fb_create = armada_fb_create,
59 .output_poll_changed = drm_fb_helper_output_poll_changed,
60 .atomic_check = drm_atomic_helper_check,
61 .atomic_commit = drm_atomic_helper_commit,
62};
63
64static int armada_drm_bind(struct device *dev)
65{
66 struct armada_private *priv;
67 struct resource *mem = NULL;
68 int ret, n;
69
70 for (n = 0; ; n++) {
71 struct resource *r = platform_get_resource(to_platform_device(dev),
72 IORESOURCE_MEM, n);
73 if (!r)
74 break;
75
76 /* Resources above 64K are graphics memory */
77 if (resource_size(r) > SZ_64K)
78 mem = r;
79 else
80 return -EINVAL;
81 }
82
83 if (!mem)
84 return -ENXIO;
85
86 if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
87 "armada-drm"))
88 return -EBUSY;
89
90 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
91 if (!priv)
92 return -ENOMEM;
93
94 /*
95 * The drm_device structure must be at the start of
96 * armada_private for drm_dev_put() to work correctly.
97 */
98 BUILD_BUG_ON(offsetof(struct armada_private, drm) != 0);
99
100 ret = drm_dev_init(&priv->drm, &armada_drm_driver, dev);
101 if (ret) {
102 dev_err(dev, "[" DRM_NAME ":%s] drm_dev_init failed: %d\n",
103 __func__, ret);
104 kfree(priv);
105 return ret;
106 }
107 drmm_add_final_kfree(&priv->drm, priv);
108
109 /* Remove early framebuffers */
110 ret = drm_fb_helper_remove_conflicting_framebuffers(NULL,
111 "armada-drm-fb",
112 false);
113 if (ret) {
114 dev_err(dev, "[" DRM_NAME ":%s] can't kick out simple-fb: %d\n",
115 __func__, ret);
116 kfree(priv);
117 return ret;
118 }
119
120 priv->drm.dev_private = priv;
121
122 dev_set_drvdata(dev, &priv->drm);
123
124 /* Mode setting support */
125 drm_mode_config_init(&priv->drm);
126 priv->drm.mode_config.min_width = 320;
127 priv->drm.mode_config.min_height = 200;
128
129 /*
130 * With vscale enabled, the maximum width is 1920 due to the
131 * 1920 by 3 lines RAM
132 */
133 priv->drm.mode_config.max_width = 1920;
134 priv->drm.mode_config.max_height = 2048;
135
136 priv->drm.mode_config.preferred_depth = 24;
137 priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs;
138 drm_mm_init(&priv->linear, mem->start, resource_size(mem));
139 mutex_init(&priv->linear_lock);
140
141 ret = component_bind_all(dev, &priv->drm);
142 if (ret)
143 goto err_kms;
144
145 ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
146 if (ret)
147 goto err_comp;
148
149 priv->drm.irq_enabled = true;
150
151 drm_mode_config_reset(&priv->drm);
152
153 ret = armada_fbdev_init(&priv->drm);
154 if (ret)
155 goto err_comp;
156
157 drm_kms_helper_poll_init(&priv->drm);
158
159 ret = drm_dev_register(&priv->drm, 0);
160 if (ret)
161 goto err_poll;
162
163#ifdef CONFIG_DEBUG_FS
164 armada_drm_debugfs_init(priv->drm.primary);
165#endif
166
167 return 0;
168
169 err_poll:
170 drm_kms_helper_poll_fini(&priv->drm);
171 armada_fbdev_fini(&priv->drm);
172 err_comp:
173 component_unbind_all(dev, &priv->drm);
174 err_kms:
175 drm_mode_config_cleanup(&priv->drm);
176 drm_mm_takedown(&priv->linear);
177 drm_dev_put(&priv->drm);
178 return ret;
179}
180
181static void armada_drm_unbind(struct device *dev)
182{
183 struct drm_device *drm = dev_get_drvdata(dev);
184 struct armada_private *priv = drm->dev_private;
185
186 drm_kms_helper_poll_fini(&priv->drm);
187 armada_fbdev_fini(&priv->drm);
188
189 drm_dev_unregister(&priv->drm);
190
191 drm_atomic_helper_shutdown(&priv->drm);
192
193 component_unbind_all(dev, &priv->drm);
194
195 drm_mode_config_cleanup(&priv->drm);
196 drm_mm_takedown(&priv->linear);
197
198 drm_dev_put(&priv->drm);
199}
200
201static int compare_of(struct device *dev, void *data)
202{
203 return dev->of_node == data;
204}
205
206static int compare_dev_name(struct device *dev, void *data)
207{
208 const char *name = data;
209 return !strcmp(dev_name(dev), name);
210}
211
212static void armada_add_endpoints(struct device *dev,
213 struct component_match **match, struct device_node *dev_node)
214{
215 struct device_node *ep, *remote;
216
217 for_each_endpoint_of_node(dev_node, ep) {
218 remote = of_graph_get_remote_port_parent(ep);
219 if (remote && of_device_is_available(remote))
220 drm_of_component_match_add(dev, match, compare_of,
221 remote);
222 of_node_put(remote);
223 }
224}
225
226static const struct component_master_ops armada_master_ops = {
227 .bind = armada_drm_bind,
228 .unbind = armada_drm_unbind,
229};
230
231static int armada_drm_probe(struct platform_device *pdev)
232{
233 struct component_match *match = NULL;
234 struct device *dev = &pdev->dev;
235 int ret;
236
237 ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
238 if (ret != -EINVAL)
239 return ret;
240
241 if (dev->platform_data) {
242 char **devices = dev->platform_data;
243 struct device *d;
244 int i;
245
246 for (i = 0; devices[i]; i++)
247 component_match_add(dev, &match, compare_dev_name,
248 devices[i]);
249
250 if (i == 0) {
251 dev_err(dev, "missing 'ports' property\n");
252 return -ENODEV;
253 }
254
255 for (i = 0; devices[i]; i++) {
256 d = bus_find_device_by_name(&platform_bus_type, NULL,
257 devices[i]);
258 if (d && d->of_node)
259 armada_add_endpoints(dev, &match, d->of_node);
260 put_device(d);
261 }
262 }
263
264 return component_master_add_with_match(&pdev->dev, &armada_master_ops,
265 match);
266}
267
268static int armada_drm_remove(struct platform_device *pdev)
269{
270 component_master_del(&pdev->dev, &armada_master_ops);
271 return 0;
272}
273
274static const struct platform_device_id armada_drm_platform_ids[] = {
275 {
276 .name = "armada-drm",
277 }, {
278 .name = "armada-510-drm",
279 },
280 { },
281};
282MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
283
284static struct platform_driver armada_drm_platform_driver = {
285 .probe = armada_drm_probe,
286 .remove = armada_drm_remove,
287 .driver = {
288 .name = "armada-drm",
289 },
290 .id_table = armada_drm_platform_ids,
291};
292
293static int __init armada_drm_init(void)
294{
295 int ret;
296
297 armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
298
299 ret = platform_driver_register(&armada_lcd_platform_driver);
300 if (ret)
301 return ret;
302 ret = platform_driver_register(&armada_drm_platform_driver);
303 if (ret)
304 platform_driver_unregister(&armada_lcd_platform_driver);
305 return ret;
306}
307module_init(armada_drm_init);
308
309static void __exit armada_drm_exit(void)
310{
311 platform_driver_unregister(&armada_drm_platform_driver);
312 platform_driver_unregister(&armada_lcd_platform_driver);
313}
314module_exit(armada_drm_exit);
315
316MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
317MODULE_DESCRIPTION("Armada DRM Driver");
318MODULE_LICENSE("GPL");
319MODULE_ALIAS("platform:armada-drm");