Commit | Line | Data |
---|---|---|
2048e328 MY |
1 | /* |
2 | * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd | |
3 | * Author:Mark Yao <mark.yao@rock-chips.com> | |
4 | * | |
5 | * based on exynos_drm_drv.c | |
6 | * | |
7 | * This software is licensed under the terms of the GNU General Public | |
8 | * License version 2, as published by the Free Software Foundation, and | |
9 | * may be copied, distributed, and modified under those terms. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | */ | |
16 | ||
17 | #include <asm/dma-iommu.h> | |
18 | ||
19 | #include <drm/drmP.h> | |
20 | #include <drm/drm_crtc_helper.h> | |
21 | #include <drm/drm_fb_helper.h> | |
80f67cd8 | 22 | #include <drm/drm_gem_cma_helper.h> |
2048e328 MY |
23 | #include <linux/dma-mapping.h> |
24 | #include <linux/pm_runtime.h> | |
00fe6148 | 25 | #include <linux/module.h> |
2048e328 MY |
26 | #include <linux/of_graph.h> |
27 | #include <linux/component.h> | |
5a587383 | 28 | #include <linux/console.h> |
2048e328 MY |
29 | |
30 | #include "rockchip_drm_drv.h" | |
31 | #include "rockchip_drm_fb.h" | |
32 | #include "rockchip_drm_fbdev.h" | |
33 | #include "rockchip_drm_gem.h" | |
34 | ||
35 | #define DRIVER_NAME "rockchip" | |
36 | #define DRIVER_DESC "RockChip Soc DRM" | |
37 | #define DRIVER_DATE "20140818" | |
38 | #define DRIVER_MAJOR 1 | |
39 | #define DRIVER_MINOR 0 | |
40 | ||
2d90d477 | 41 | static bool is_support_iommu = true; |
f706974a | 42 | static struct drm_driver rockchip_drm_driver; |
2d90d477 | 43 | |
2048e328 MY |
44 | /* |
45 | * Attach a (component) device to the shared drm dma mapping from master drm | |
46 | * device. This is used by the VOPs to map GEM buffers to a common DMA | |
47 | * mapping. | |
48 | */ | |
49 | int rockchip_drm_dma_attach_device(struct drm_device *drm_dev, | |
50 | struct device *dev) | |
51 | { | |
52 | struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping; | |
53 | int ret; | |
54 | ||
2d90d477 MY |
55 | if (!is_support_iommu) |
56 | return 0; | |
57 | ||
2048e328 MY |
58 | ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); |
59 | if (ret) | |
60 | return ret; | |
61 | ||
62 | dma_set_max_seg_size(dev, DMA_BIT_MASK(32)); | |
63 | ||
64 | return arm_iommu_attach_device(dev, mapping); | |
65 | } | |
2048e328 MY |
66 | |
67 | void rockchip_drm_dma_detach_device(struct drm_device *drm_dev, | |
68 | struct device *dev) | |
69 | { | |
2d90d477 MY |
70 | if (!is_support_iommu) |
71 | return; | |
72 | ||
2048e328 MY |
73 | arm_iommu_detach_device(dev); |
74 | } | |
2048e328 | 75 | |
b5f7b755 MY |
76 | int rockchip_register_crtc_funcs(struct drm_crtc *crtc, |
77 | const struct rockchip_crtc_funcs *crtc_funcs) | |
2048e328 | 78 | { |
b5f7b755 MY |
79 | int pipe = drm_crtc_index(crtc); |
80 | struct rockchip_drm_private *priv = crtc->dev->dev_private; | |
2048e328 MY |
81 | |
82 | if (pipe > ROCKCHIP_MAX_CRTC) | |
83 | return -EINVAL; | |
84 | ||
85 | priv->crtc_funcs[pipe] = crtc_funcs; | |
86 | ||
87 | return 0; | |
88 | } | |
2048e328 | 89 | |
b5f7b755 | 90 | void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc) |
2048e328 | 91 | { |
b5f7b755 MY |
92 | int pipe = drm_crtc_index(crtc); |
93 | struct rockchip_drm_private *priv = crtc->dev->dev_private; | |
2048e328 MY |
94 | |
95 | if (pipe > ROCKCHIP_MAX_CRTC) | |
96 | return; | |
97 | ||
98 | priv->crtc_funcs[pipe] = NULL; | |
99 | } | |
2048e328 MY |
100 | |
101 | static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm, | |
102 | int pipe) | |
103 | { | |
104 | struct drm_crtc *crtc; | |
105 | int i = 0; | |
106 | ||
107 | list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) | |
108 | if (i++ == pipe) | |
109 | return crtc; | |
110 | ||
111 | return NULL; | |
112 | } | |
113 | ||
88e72717 TR |
114 | static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev, |
115 | unsigned int pipe) | |
2048e328 MY |
116 | { |
117 | struct rockchip_drm_private *priv = dev->dev_private; | |
118 | struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe); | |
119 | ||
120 | if (crtc && priv->crtc_funcs[pipe] && | |
121 | priv->crtc_funcs[pipe]->enable_vblank) | |
122 | return priv->crtc_funcs[pipe]->enable_vblank(crtc); | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
88e72717 TR |
127 | static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev, |
128 | unsigned int pipe) | |
2048e328 MY |
129 | { |
130 | struct rockchip_drm_private *priv = dev->dev_private; | |
131 | struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe); | |
132 | ||
133 | if (crtc && priv->crtc_funcs[pipe] && | |
134 | priv->crtc_funcs[pipe]->enable_vblank) | |
135 | priv->crtc_funcs[pipe]->disable_vblank(crtc); | |
136 | } | |
137 | ||
f706974a | 138 | static int rockchip_drm_bind(struct device *dev) |
2048e328 | 139 | { |
f706974a | 140 | struct drm_device *drm_dev; |
2048e328 | 141 | struct rockchip_drm_private *private; |
2d90d477 | 142 | struct dma_iommu_mapping *mapping = NULL; |
2048e328 MY |
143 | int ret; |
144 | ||
f706974a TV |
145 | drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev); |
146 | if (!drm_dev) | |
2048e328 MY |
147 | return -ENOMEM; |
148 | ||
f706974a TV |
149 | dev_set_drvdata(dev, drm_dev); |
150 | ||
151 | private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL); | |
152 | if (!private) { | |
153 | ret = -ENOMEM; | |
9127f99c | 154 | goto err_free; |
f706974a TV |
155 | } |
156 | ||
2048e328 MY |
157 | drm_dev->dev_private = private; |
158 | ||
159 | drm_mode_config_init(drm_dev); | |
160 | ||
161 | rockchip_drm_mode_config_init(drm_dev); | |
162 | ||
163 | dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms), | |
164 | GFP_KERNEL); | |
165 | if (!dev->dma_parms) { | |
166 | ret = -ENOMEM; | |
167 | goto err_config_cleanup; | |
168 | } | |
169 | ||
2d90d477 MY |
170 | if (is_support_iommu) { |
171 | /* TODO(djkurtz): fetch the mapping start/size from somewhere */ | |
172 | mapping = arm_iommu_create_mapping(&platform_bus_type, | |
173 | 0x00000000, | |
174 | SZ_2G); | |
175 | if (IS_ERR(mapping)) { | |
176 | ret = PTR_ERR(mapping); | |
177 | goto err_config_cleanup; | |
178 | } | |
2048e328 | 179 | |
2d90d477 MY |
180 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); |
181 | if (ret) | |
182 | goto err_release_mapping; | |
2048e328 | 183 | |
2d90d477 | 184 | dma_set_max_seg_size(dev, DMA_BIT_MASK(32)); |
2048e328 | 185 | |
2d90d477 MY |
186 | ret = arm_iommu_attach_device(dev, mapping); |
187 | if (ret) | |
188 | goto err_release_mapping; | |
189 | } | |
2048e328 MY |
190 | |
191 | /* Try to bind all sub drivers. */ | |
192 | ret = component_bind_all(dev, drm_dev); | |
193 | if (ret) | |
194 | goto err_detach_device; | |
195 | ||
196 | /* init kms poll for handling hpd */ | |
197 | drm_kms_helper_poll_init(drm_dev); | |
198 | ||
199 | /* | |
200 | * enable drm irq mode. | |
201 | * - with irq_enabled = true, we can use the vblank feature. | |
202 | */ | |
203 | drm_dev->irq_enabled = true; | |
204 | ||
205 | ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC); | |
206 | if (ret) | |
207 | goto err_kms_helper_poll_fini; | |
208 | ||
63ebb9fa MY |
209 | drm_mode_config_reset(drm_dev); |
210 | ||
2048e328 MY |
211 | ret = rockchip_drm_fbdev_init(drm_dev); |
212 | if (ret) | |
213 | goto err_vblank_cleanup; | |
214 | ||
9127f99c TF |
215 | ret = drm_dev_register(drm_dev, 0); |
216 | if (ret) | |
217 | goto err_fbdev_fini; | |
218 | ||
2d90d477 MY |
219 | if (is_support_iommu) |
220 | arm_iommu_release_mapping(mapping); | |
2048e328 | 221 | return 0; |
9127f99c TF |
222 | err_fbdev_fini: |
223 | rockchip_drm_fbdev_fini(drm_dev); | |
2048e328 MY |
224 | err_vblank_cleanup: |
225 | drm_vblank_cleanup(drm_dev); | |
226 | err_kms_helper_poll_fini: | |
227 | drm_kms_helper_poll_fini(drm_dev); | |
228 | component_unbind_all(dev, drm_dev); | |
229 | err_detach_device: | |
2d90d477 MY |
230 | if (is_support_iommu) |
231 | arm_iommu_detach_device(dev); | |
2048e328 | 232 | err_release_mapping: |
2d90d477 MY |
233 | if (is_support_iommu) |
234 | arm_iommu_release_mapping(mapping); | |
2048e328 MY |
235 | err_config_cleanup: |
236 | drm_mode_config_cleanup(drm_dev); | |
237 | drm_dev->dev_private = NULL; | |
f706974a TV |
238 | err_free: |
239 | drm_dev_unref(drm_dev); | |
2048e328 MY |
240 | return ret; |
241 | } | |
242 | ||
f706974a | 243 | static void rockchip_drm_unbind(struct device *dev) |
2048e328 | 244 | { |
f706974a | 245 | struct drm_device *drm_dev = dev_get_drvdata(dev); |
2048e328 MY |
246 | |
247 | rockchip_drm_fbdev_fini(drm_dev); | |
248 | drm_vblank_cleanup(drm_dev); | |
249 | drm_kms_helper_poll_fini(drm_dev); | |
250 | component_unbind_all(dev, drm_dev); | |
2d90d477 MY |
251 | if (is_support_iommu) |
252 | arm_iommu_detach_device(dev); | |
2048e328 MY |
253 | drm_mode_config_cleanup(drm_dev); |
254 | drm_dev->dev_private = NULL; | |
f706974a TV |
255 | drm_dev_unregister(drm_dev); |
256 | drm_dev_unref(drm_dev); | |
257 | dev_set_drvdata(dev, NULL); | |
2048e328 MY |
258 | } |
259 | ||
8ff490ae | 260 | static void rockchip_drm_lastclose(struct drm_device *dev) |
2048e328 MY |
261 | { |
262 | struct rockchip_drm_private *priv = dev->dev_private; | |
263 | ||
264 | drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper); | |
265 | } | |
266 | ||
267 | static const struct file_operations rockchip_drm_driver_fops = { | |
268 | .owner = THIS_MODULE, | |
269 | .open = drm_open, | |
270 | .mmap = rockchip_gem_mmap, | |
271 | .poll = drm_poll, | |
272 | .read = drm_read, | |
273 | .unlocked_ioctl = drm_ioctl, | |
274 | #ifdef CONFIG_COMPAT | |
275 | .compat_ioctl = drm_compat_ioctl, | |
276 | #endif | |
277 | .release = drm_release, | |
278 | }; | |
279 | ||
2048e328 | 280 | static struct drm_driver rockchip_drm_driver = { |
63ebb9fa MY |
281 | .driver_features = DRIVER_MODESET | DRIVER_GEM | |
282 | DRIVER_PRIME | DRIVER_ATOMIC, | |
2048e328 | 283 | .lastclose = rockchip_drm_lastclose, |
b44f8408 | 284 | .get_vblank_counter = drm_vblank_no_hw_counter, |
2048e328 MY |
285 | .enable_vblank = rockchip_drm_crtc_enable_vblank, |
286 | .disable_vblank = rockchip_drm_crtc_disable_vblank, | |
80f67cd8 | 287 | .gem_vm_ops = &drm_gem_cma_vm_ops, |
c2466ac3 | 288 | .gem_free_object_unlocked = rockchip_gem_free_object, |
2048e328 MY |
289 | .dumb_create = rockchip_gem_dumb_create, |
290 | .dumb_map_offset = rockchip_gem_dumb_map_offset, | |
291 | .dumb_destroy = drm_gem_dumb_destroy, | |
292 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, | |
293 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, | |
294 | .gem_prime_import = drm_gem_prime_import, | |
295 | .gem_prime_export = drm_gem_prime_export, | |
296 | .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table, | |
297 | .gem_prime_vmap = rockchip_gem_prime_vmap, | |
298 | .gem_prime_vunmap = rockchip_gem_prime_vunmap, | |
299 | .gem_prime_mmap = rockchip_gem_mmap_buf, | |
300 | .fops = &rockchip_drm_driver_fops, | |
301 | .name = DRIVER_NAME, | |
302 | .desc = DRIVER_DESC, | |
303 | .date = DRIVER_DATE, | |
304 | .major = DRIVER_MAJOR, | |
305 | .minor = DRIVER_MINOR, | |
306 | }; | |
307 | ||
308 | #ifdef CONFIG_PM_SLEEP | |
5a587383 | 309 | void rockchip_drm_fb_suspend(struct drm_device *drm) |
2048e328 | 310 | { |
5a587383 | 311 | struct rockchip_drm_private *priv = drm->dev_private; |
2048e328 | 312 | |
5a587383 TV |
313 | console_lock(); |
314 | drm_fb_helper_set_suspend(&priv->fbdev_helper, 1); | |
315 | console_unlock(); | |
316 | } | |
2048e328 | 317 | |
5a587383 TV |
318 | void rockchip_drm_fb_resume(struct drm_device *drm) |
319 | { | |
320 | struct rockchip_drm_private *priv = drm->dev_private; | |
2048e328 | 321 | |
5a587383 TV |
322 | console_lock(); |
323 | drm_fb_helper_set_suspend(&priv->fbdev_helper, 0); | |
324 | console_unlock(); | |
325 | } | |
2048e328 | 326 | |
5a587383 TV |
327 | static int rockchip_drm_sys_suspend(struct device *dev) |
328 | { | |
329 | struct drm_device *drm = dev_get_drvdata(dev); | |
330 | struct rockchip_drm_private *priv = drm->dev_private; | |
331 | ||
332 | drm_kms_helper_poll_disable(drm); | |
333 | rockchip_drm_fb_suspend(drm); | |
334 | ||
335 | priv->state = drm_atomic_helper_suspend(drm); | |
336 | if (IS_ERR(priv->state)) { | |
337 | rockchip_drm_fb_resume(drm); | |
338 | drm_kms_helper_poll_enable(drm); | |
339 | return PTR_ERR(priv->state); | |
2048e328 | 340 | } |
2048e328 MY |
341 | |
342 | return 0; | |
343 | } | |
344 | ||
345 | static int rockchip_drm_sys_resume(struct device *dev) | |
346 | { | |
347 | struct drm_device *drm = dev_get_drvdata(dev); | |
5a587383 | 348 | struct rockchip_drm_private *priv = drm->dev_private; |
2048e328 | 349 | |
5a587383 TV |
350 | drm_atomic_helper_resume(drm, priv->state); |
351 | rockchip_drm_fb_resume(drm); | |
352 | drm_kms_helper_poll_enable(drm); | |
2048e328 MY |
353 | |
354 | return 0; | |
355 | } | |
356 | #endif | |
357 | ||
358 | static const struct dev_pm_ops rockchip_drm_pm_ops = { | |
359 | SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend, | |
360 | rockchip_drm_sys_resume) | |
361 | }; | |
362 | ||
2048e328 MY |
363 | static int compare_of(struct device *dev, void *data) |
364 | { | |
365 | struct device_node *np = data; | |
366 | ||
367 | return dev->of_node == np; | |
368 | } | |
369 | ||
5bad7d29 MY |
370 | static void rockchip_add_endpoints(struct device *dev, |
371 | struct component_match **match, | |
372 | struct device_node *port) | |
373 | { | |
374 | struct device_node *ep, *remote; | |
375 | ||
376 | for_each_child_of_node(port, ep) { | |
377 | remote = of_graph_get_remote_port_parent(ep); | |
378 | if (!remote || !of_device_is_available(remote)) { | |
379 | of_node_put(remote); | |
380 | continue; | |
381 | } else if (!of_device_is_available(remote->parent)) { | |
382 | dev_warn(dev, "parent device of %s is not available\n", | |
383 | remote->full_name); | |
384 | of_node_put(remote); | |
385 | continue; | |
386 | } | |
387 | ||
388 | component_match_add(dev, match, compare_of, remote); | |
389 | of_node_put(remote); | |
390 | } | |
391 | } | |
392 | ||
2048e328 MY |
393 | static const struct component_master_ops rockchip_drm_ops = { |
394 | .bind = rockchip_drm_bind, | |
395 | .unbind = rockchip_drm_unbind, | |
396 | }; | |
397 | ||
398 | static int rockchip_drm_platform_probe(struct platform_device *pdev) | |
399 | { | |
5bad7d29 MY |
400 | struct device *dev = &pdev->dev; |
401 | struct component_match *match = NULL; | |
402 | struct device_node *np = dev->of_node; | |
403 | struct device_node *port; | |
404 | int i; | |
2048e328 | 405 | |
5bad7d29 | 406 | if (!np) |
2048e328 | 407 | return -ENODEV; |
5bad7d29 MY |
408 | /* |
409 | * Bind the crtc ports first, so that | |
410 | * drm_of_find_possible_crtcs called from encoder .bind callbacks | |
411 | * works as expected. | |
412 | */ | |
413 | for (i = 0;; i++) { | |
2d90d477 MY |
414 | struct device_node *iommu; |
415 | ||
5bad7d29 MY |
416 | port = of_parse_phandle(np, "ports", i); |
417 | if (!port) | |
418 | break; | |
419 | ||
420 | if (!of_device_is_available(port->parent)) { | |
421 | of_node_put(port); | |
422 | continue; | |
423 | } | |
2048e328 | 424 | |
2d90d477 MY |
425 | iommu = of_parse_phandle(port->parent, "iommus", 0); |
426 | if (!iommu || !of_device_is_available(iommu->parent)) { | |
427 | dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n", | |
428 | port->parent->full_name); | |
429 | /* | |
430 | * if there is a crtc not support iommu, force set all | |
431 | * crtc use non-iommu buffer. | |
432 | */ | |
433 | is_support_iommu = false; | |
434 | } | |
435 | ||
6d5fa28c | 436 | of_node_put(iommu); |
5bad7d29 MY |
437 | component_match_add(dev, &match, compare_of, port->parent); |
438 | of_node_put(port); | |
439 | } | |
440 | ||
441 | if (i == 0) { | |
442 | dev_err(dev, "missing 'ports' property\n"); | |
443 | return -ENODEV; | |
444 | } | |
445 | ||
446 | if (!match) { | |
447 | dev_err(dev, "No available vop found for display-subsystem.\n"); | |
448 | return -ENODEV; | |
449 | } | |
450 | /* | |
451 | * For each bound crtc, bind the encoders attached to its | |
452 | * remote endpoint. | |
453 | */ | |
454 | for (i = 0;; i++) { | |
455 | port = of_parse_phandle(np, "ports", i); | |
456 | if (!port) | |
457 | break; | |
458 | ||
459 | if (!of_device_is_available(port->parent)) { | |
460 | of_node_put(port); | |
461 | continue; | |
462 | } | |
463 | ||
464 | rockchip_add_endpoints(dev, &match, port); | |
465 | of_node_put(port); | |
466 | } | |
467 | ||
468 | return component_master_add_with_match(dev, &rockchip_drm_ops, match); | |
2048e328 MY |
469 | } |
470 | ||
471 | static int rockchip_drm_platform_remove(struct platform_device *pdev) | |
472 | { | |
473 | component_master_del(&pdev->dev, &rockchip_drm_ops); | |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
478 | static const struct of_device_id rockchip_drm_dt_ids[] = { | |
479 | { .compatible = "rockchip,display-subsystem", }, | |
480 | { /* sentinel */ }, | |
481 | }; | |
482 | MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids); | |
483 | ||
484 | static struct platform_driver rockchip_drm_platform_driver = { | |
485 | .probe = rockchip_drm_platform_probe, | |
486 | .remove = rockchip_drm_platform_remove, | |
487 | .driver = { | |
2048e328 MY |
488 | .name = "rockchip-drm", |
489 | .of_match_table = rockchip_drm_dt_ids, | |
490 | .pm = &rockchip_drm_pm_ops, | |
491 | }, | |
492 | }; | |
493 | ||
494 | module_platform_driver(rockchip_drm_platform_driver); | |
495 | ||
496 | MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>"); | |
497 | MODULE_DESCRIPTION("ROCKCHIP DRM Driver"); | |
498 | MODULE_LICENSE("GPL v2"); |