blob: 399adf3c4224dd7fa75bdb56b0f9a62af9c853ee [file] [log] [blame]
Mark Yao2048e322014-08-22 18:36:26 +08001/*
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>
22#include <linux/dma-mapping.h>
23#include <linux/pm_runtime.h>
Paul Gortmaker00fe6142015-05-01 20:02:30 -040024#include <linux/module.h>
Mark Yao2048e322014-08-22 18:36:26 +080025#include <linux/of_graph.h>
26#include <linux/component.h>
27
28#include "rockchip_drm_drv.h"
29#include "rockchip_drm_fb.h"
30#include "rockchip_drm_fbdev.h"
31#include "rockchip_drm_gem.h"
32
33#define DRIVER_NAME "rockchip"
34#define DRIVER_DESC "RockChip Soc DRM"
35#define DRIVER_DATE "20140818"
36#define DRIVER_MAJOR 1
37#define DRIVER_MINOR 0
38
Mark Yao2d90d472016-04-19 10:13:27 +080039static bool is_support_iommu = true;
40
Mark Yao2048e322014-08-22 18:36:26 +080041/*
42 * Attach a (component) device to the shared drm dma mapping from master drm
43 * device. This is used by the VOPs to map GEM buffers to a common DMA
44 * mapping.
45 */
46int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
47 struct device *dev)
48{
49 struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping;
50 int ret;
51
Mark Yao2d90d472016-04-19 10:13:27 +080052 if (!is_support_iommu)
53 return 0;
54
Mark Yao2048e322014-08-22 18:36:26 +080055 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
56 if (ret)
57 return ret;
58
59 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
60
61 return arm_iommu_attach_device(dev, mapping);
62}
Mark Yao2048e322014-08-22 18:36:26 +080063
64void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
65 struct device *dev)
66{
Mark Yao2d90d472016-04-19 10:13:27 +080067 if (!is_support_iommu)
68 return;
69
Mark Yao2048e322014-08-22 18:36:26 +080070 arm_iommu_detach_device(dev);
71}
Mark Yao2048e322014-08-22 18:36:26 +080072
Mark Yaob5f7b752015-11-23 15:21:08 +080073int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
74 const struct rockchip_crtc_funcs *crtc_funcs)
Mark Yao2048e322014-08-22 18:36:26 +080075{
Mark Yaob5f7b752015-11-23 15:21:08 +080076 int pipe = drm_crtc_index(crtc);
77 struct rockchip_drm_private *priv = crtc->dev->dev_private;
Mark Yao2048e322014-08-22 18:36:26 +080078
79 if (pipe > ROCKCHIP_MAX_CRTC)
80 return -EINVAL;
81
82 priv->crtc_funcs[pipe] = crtc_funcs;
83
84 return 0;
85}
Mark Yao2048e322014-08-22 18:36:26 +080086
Mark Yaob5f7b752015-11-23 15:21:08 +080087void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
Mark Yao2048e322014-08-22 18:36:26 +080088{
Mark Yaob5f7b752015-11-23 15:21:08 +080089 int pipe = drm_crtc_index(crtc);
90 struct rockchip_drm_private *priv = crtc->dev->dev_private;
Mark Yao2048e322014-08-22 18:36:26 +080091
92 if (pipe > ROCKCHIP_MAX_CRTC)
93 return;
94
95 priv->crtc_funcs[pipe] = NULL;
96}
Mark Yao2048e322014-08-22 18:36:26 +080097
98static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
99 int pipe)
100{
101 struct drm_crtc *crtc;
102 int i = 0;
103
104 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
105 if (i++ == pipe)
106 return crtc;
107
108 return NULL;
109}
110
Thierry Reding88e72712015-09-24 18:35:31 +0200111static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
112 unsigned int pipe)
Mark Yao2048e322014-08-22 18:36:26 +0800113{
114 struct rockchip_drm_private *priv = dev->dev_private;
115 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
116
117 if (crtc && priv->crtc_funcs[pipe] &&
118 priv->crtc_funcs[pipe]->enable_vblank)
119 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
120
121 return 0;
122}
123
Thierry Reding88e72712015-09-24 18:35:31 +0200124static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
125 unsigned int pipe)
Mark Yao2048e322014-08-22 18:36:26 +0800126{
127 struct rockchip_drm_private *priv = dev->dev_private;
128 struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
129
130 if (crtc && priv->crtc_funcs[pipe] &&
131 priv->crtc_funcs[pipe]->enable_vblank)
132 priv->crtc_funcs[pipe]->disable_vblank(crtc);
133}
134
135static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
136{
137 struct rockchip_drm_private *private;
Mark Yao2d90d472016-04-19 10:13:27 +0800138 struct dma_iommu_mapping *mapping = NULL;
Mark Yao2048e322014-08-22 18:36:26 +0800139 struct device *dev = drm_dev->dev;
Daniel Kurtzd3007da2015-01-30 10:14:17 +0100140 struct drm_connector *connector;
Mark Yao2048e322014-08-22 18:36:26 +0800141 int ret;
142
143 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
144 if (!private)
145 return -ENOMEM;
146
Mark Yaof32fad52015-12-16 18:09:38 +0800147 mutex_init(&private->commit.lock);
148 INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
149
Mark Yao2048e322014-08-22 18:36:26 +0800150 drm_dev->dev_private = private;
151
152 drm_mode_config_init(drm_dev);
153
154 rockchip_drm_mode_config_init(drm_dev);
155
156 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
157 GFP_KERNEL);
158 if (!dev->dma_parms) {
159 ret = -ENOMEM;
160 goto err_config_cleanup;
161 }
162
Mark Yao2d90d472016-04-19 10:13:27 +0800163 if (is_support_iommu) {
164 /* TODO(djkurtz): fetch the mapping start/size from somewhere */
165 mapping = arm_iommu_create_mapping(&platform_bus_type,
166 0x00000000,
167 SZ_2G);
168 if (IS_ERR(mapping)) {
169 ret = PTR_ERR(mapping);
170 goto err_config_cleanup;
171 }
172
173 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
174 if (ret)
175 goto err_release_mapping;
176
177 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
178
179 ret = arm_iommu_attach_device(dev, mapping);
180 if (ret)
181 goto err_release_mapping;
Mark Yao2048e322014-08-22 18:36:26 +0800182 }
183
Mark Yao2048e322014-08-22 18:36:26 +0800184 /* Try to bind all sub drivers. */
185 ret = component_bind_all(dev, drm_dev);
186 if (ret)
187 goto err_detach_device;
188
Daniel Kurtzd3007da2015-01-30 10:14:17 +0100189 /*
190 * All components are now added, we can publish the connector sysfs
191 * entries to userspace. This will generate hotplug events and so
192 * userspace will expect to be able to access DRM at this point.
193 */
194 list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
195 head) {
196 ret = drm_connector_register(connector);
197 if (ret) {
198 dev_err(drm_dev->dev,
199 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
200 connector->base.id,
201 connector->name, ret);
202 goto err_unbind;
203 }
204 }
205
Mark Yao2048e322014-08-22 18:36:26 +0800206 /* init kms poll for handling hpd */
207 drm_kms_helper_poll_init(drm_dev);
208
209 /*
210 * enable drm irq mode.
211 * - with irq_enabled = true, we can use the vblank feature.
212 */
213 drm_dev->irq_enabled = true;
214
215 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
216 if (ret)
217 goto err_kms_helper_poll_fini;
218
219 /*
220 * with vblank_disable_allowed = true, vblank interrupt will be disabled
221 * by drm timer once a current process gives up ownership of
222 * vblank event.(after drm_vblank_put function is called)
223 */
224 drm_dev->vblank_disable_allowed = true;
225
Mark Yao63ebb9f2015-11-30 18:22:42 +0800226 drm_mode_config_reset(drm_dev);
227
Mark Yao2048e322014-08-22 18:36:26 +0800228 ret = rockchip_drm_fbdev_init(drm_dev);
229 if (ret)
230 goto err_vblank_cleanup;
231
Mark Yao2d90d472016-04-19 10:13:27 +0800232 if (is_support_iommu)
233 arm_iommu_release_mapping(mapping);
Mark Yao2048e322014-08-22 18:36:26 +0800234 return 0;
235err_vblank_cleanup:
236 drm_vblank_cleanup(drm_dev);
237err_kms_helper_poll_fini:
238 drm_kms_helper_poll_fini(drm_dev);
Daniel Kurtzd3007da2015-01-30 10:14:17 +0100239err_unbind:
Mark Yao2048e322014-08-22 18:36:26 +0800240 component_unbind_all(dev, drm_dev);
241err_detach_device:
Mark Yao2d90d472016-04-19 10:13:27 +0800242 if (is_support_iommu)
243 arm_iommu_detach_device(dev);
Mark Yao2048e322014-08-22 18:36:26 +0800244err_release_mapping:
Mark Yao2d90d472016-04-19 10:13:27 +0800245 if (is_support_iommu)
246 arm_iommu_release_mapping(mapping);
Mark Yao2048e322014-08-22 18:36:26 +0800247err_config_cleanup:
248 drm_mode_config_cleanup(drm_dev);
249 drm_dev->dev_private = NULL;
250 return ret;
251}
252
253static int rockchip_drm_unload(struct drm_device *drm_dev)
254{
255 struct device *dev = drm_dev->dev;
256
257 rockchip_drm_fbdev_fini(drm_dev);
258 drm_vblank_cleanup(drm_dev);
259 drm_kms_helper_poll_fini(drm_dev);
260 component_unbind_all(dev, drm_dev);
Mark Yao2d90d472016-04-19 10:13:27 +0800261 if (is_support_iommu)
262 arm_iommu_detach_device(dev);
Mark Yao2048e322014-08-22 18:36:26 +0800263 drm_mode_config_cleanup(drm_dev);
264 drm_dev->dev_private = NULL;
265
266 return 0;
267}
268
John Keepingf1350462016-03-11 17:21:17 +0000269static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
270 struct drm_file *file_priv)
271{
272 struct rockchip_drm_private *priv = crtc->dev->dev_private;
273 int pipe = drm_crtc_index(crtc);
274
275 if (pipe < ROCKCHIP_MAX_CRTC &&
276 priv->crtc_funcs[pipe] &&
277 priv->crtc_funcs[pipe]->cancel_pending_vblank)
278 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
279}
280
281static void rockchip_drm_preclose(struct drm_device *dev,
282 struct drm_file *file_priv)
283{
284 struct drm_crtc *crtc;
285
286 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
287 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
288}
289
Mark Yao2048e322014-08-22 18:36:26 +0800290void rockchip_drm_lastclose(struct drm_device *dev)
291{
292 struct rockchip_drm_private *priv = dev->dev_private;
293
294 drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
295}
296
297static const struct file_operations rockchip_drm_driver_fops = {
298 .owner = THIS_MODULE,
299 .open = drm_open,
300 .mmap = rockchip_gem_mmap,
301 .poll = drm_poll,
302 .read = drm_read,
303 .unlocked_ioctl = drm_ioctl,
304#ifdef CONFIG_COMPAT
305 .compat_ioctl = drm_compat_ioctl,
306#endif
307 .release = drm_release,
308};
309
310const struct vm_operations_struct rockchip_drm_vm_ops = {
311 .open = drm_gem_vm_open,
312 .close = drm_gem_vm_close,
313};
314
315static struct drm_driver rockchip_drm_driver = {
Mark Yao63ebb9f2015-11-30 18:22:42 +0800316 .driver_features = DRIVER_MODESET | DRIVER_GEM |
317 DRIVER_PRIME | DRIVER_ATOMIC,
Mark Yao2048e322014-08-22 18:36:26 +0800318 .load = rockchip_drm_load,
319 .unload = rockchip_drm_unload,
John Keepingf1350462016-03-11 17:21:17 +0000320 .preclose = rockchip_drm_preclose,
Mark Yao2048e322014-08-22 18:36:26 +0800321 .lastclose = rockchip_drm_lastclose,
Ville Syrjäläb44f8402015-09-30 16:46:48 +0300322 .get_vblank_counter = drm_vblank_no_hw_counter,
Mark Yao2048e322014-08-22 18:36:26 +0800323 .enable_vblank = rockchip_drm_crtc_enable_vblank,
324 .disable_vblank = rockchip_drm_crtc_disable_vblank,
325 .gem_vm_ops = &rockchip_drm_vm_ops,
326 .gem_free_object = rockchip_gem_free_object,
327 .dumb_create = rockchip_gem_dumb_create,
328 .dumb_map_offset = rockchip_gem_dumb_map_offset,
329 .dumb_destroy = drm_gem_dumb_destroy,
330 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
331 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
332 .gem_prime_import = drm_gem_prime_import,
333 .gem_prime_export = drm_gem_prime_export,
334 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
335 .gem_prime_vmap = rockchip_gem_prime_vmap,
336 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
337 .gem_prime_mmap = rockchip_gem_mmap_buf,
338 .fops = &rockchip_drm_driver_fops,
339 .name = DRIVER_NAME,
340 .desc = DRIVER_DESC,
341 .date = DRIVER_DATE,
342 .major = DRIVER_MAJOR,
343 .minor = DRIVER_MINOR,
344};
345
346#ifdef CONFIG_PM_SLEEP
347static int rockchip_drm_sys_suspend(struct device *dev)
348{
349 struct drm_device *drm = dev_get_drvdata(dev);
350 struct drm_connector *connector;
351
352 if (!drm)
353 return 0;
354
355 drm_modeset_lock_all(drm);
356 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
357 int old_dpms = connector->dpms;
358
359 if (connector->funcs->dpms)
360 connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
361
362 /* Set the old mode back to the connector for resume */
363 connector->dpms = old_dpms;
364 }
365 drm_modeset_unlock_all(drm);
366
367 return 0;
368}
369
370static int rockchip_drm_sys_resume(struct device *dev)
371{
372 struct drm_device *drm = dev_get_drvdata(dev);
373 struct drm_connector *connector;
374 enum drm_connector_status status;
375 bool changed = false;
376
377 if (!drm)
378 return 0;
379
380 drm_modeset_lock_all(drm);
381 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
382 int desired_mode = connector->dpms;
383
384 /*
385 * at suspend time, we save dpms to connector->dpms,
386 * restore the old_dpms, and at current time, the connector
387 * dpms status must be DRM_MODE_DPMS_OFF.
388 */
389 connector->dpms = DRM_MODE_DPMS_OFF;
390
391 /*
392 * If the connector has been disconnected during suspend,
393 * disconnect it from the encoder and leave it off. We'll notify
394 * userspace at the end.
395 */
396 if (desired_mode == DRM_MODE_DPMS_ON) {
397 status = connector->funcs->detect(connector, true);
398 if (status == connector_status_disconnected) {
399 connector->encoder = NULL;
400 connector->status = status;
401 changed = true;
402 continue;
403 }
404 }
405 if (connector->funcs->dpms)
406 connector->funcs->dpms(connector, desired_mode);
407 }
408 drm_modeset_unlock_all(drm);
409
410 drm_helper_resume_force_mode(drm);
411
412 if (changed)
413 drm_kms_helper_hotplug_event(drm);
414
415 return 0;
416}
417#endif
418
419static const struct dev_pm_ops rockchip_drm_pm_ops = {
420 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
421 rockchip_drm_sys_resume)
422};
423
Mark Yao2048e322014-08-22 18:36:26 +0800424static int compare_of(struct device *dev, void *data)
425{
426 struct device_node *np = data;
427
428 return dev->of_node == np;
429}
430
Mark Yao5bad7d22015-11-10 16:47:19 +0800431static void rockchip_add_endpoints(struct device *dev,
432 struct component_match **match,
433 struct device_node *port)
434{
435 struct device_node *ep, *remote;
436
437 for_each_child_of_node(port, ep) {
438 remote = of_graph_get_remote_port_parent(ep);
439 if (!remote || !of_device_is_available(remote)) {
440 of_node_put(remote);
441 continue;
442 } else if (!of_device_is_available(remote->parent)) {
443 dev_warn(dev, "parent device of %s is not available\n",
444 remote->full_name);
445 of_node_put(remote);
446 continue;
447 }
448
449 component_match_add(dev, match, compare_of, remote);
450 of_node_put(remote);
451 }
452}
453
Mark Yao2048e322014-08-22 18:36:26 +0800454static int rockchip_drm_bind(struct device *dev)
455{
456 struct drm_device *drm;
457 int ret;
458
459 drm = drm_dev_alloc(&rockchip_drm_driver, dev);
460 if (!drm)
461 return -ENOMEM;
462
Mark Yao2048e322014-08-22 18:36:26 +0800463 ret = drm_dev_register(drm, 0);
464 if (ret)
465 goto err_free;
466
467 dev_set_drvdata(dev, drm);
468
469 return 0;
470
471err_free:
472 drm_dev_unref(drm);
473 return ret;
474}
475
476static void rockchip_drm_unbind(struct device *dev)
477{
478 struct drm_device *drm = dev_get_drvdata(dev);
479
480 drm_dev_unregister(drm);
481 drm_dev_unref(drm);
482 dev_set_drvdata(dev, NULL);
483}
484
485static const struct component_master_ops rockchip_drm_ops = {
486 .bind = rockchip_drm_bind,
487 .unbind = rockchip_drm_unbind,
488};
489
490static int rockchip_drm_platform_probe(struct platform_device *pdev)
491{
Mark Yao5bad7d22015-11-10 16:47:19 +0800492 struct device *dev = &pdev->dev;
493 struct component_match *match = NULL;
494 struct device_node *np = dev->of_node;
495 struct device_node *port;
496 int i;
Mark Yao2048e322014-08-22 18:36:26 +0800497
Mark Yao5bad7d22015-11-10 16:47:19 +0800498 if (!np)
Mark Yao2048e322014-08-22 18:36:26 +0800499 return -ENODEV;
Mark Yao5bad7d22015-11-10 16:47:19 +0800500 /*
501 * Bind the crtc ports first, so that
502 * drm_of_find_possible_crtcs called from encoder .bind callbacks
503 * works as expected.
504 */
505 for (i = 0;; i++) {
Mark Yao2d90d472016-04-19 10:13:27 +0800506 struct device_node *iommu;
507
Mark Yao5bad7d22015-11-10 16:47:19 +0800508 port = of_parse_phandle(np, "ports", i);
509 if (!port)
510 break;
Mark Yao2048e322014-08-22 18:36:26 +0800511
Mark Yao5bad7d22015-11-10 16:47:19 +0800512 if (!of_device_is_available(port->parent)) {
513 of_node_put(port);
514 continue;
515 }
516
Mark Yao2d90d472016-04-19 10:13:27 +0800517 iommu = of_parse_phandle(port->parent, "iommus", 0);
518 if (!iommu || !of_device_is_available(iommu->parent)) {
519 dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
520 port->parent->full_name);
521 /*
522 * if there is a crtc not support iommu, force set all
523 * crtc use non-iommu buffer.
524 */
525 is_support_iommu = false;
526 }
527
Mark Yao5bad7d22015-11-10 16:47:19 +0800528 component_match_add(dev, &match, compare_of, port->parent);
529 of_node_put(port);
530 }
531
532 if (i == 0) {
533 dev_err(dev, "missing 'ports' property\n");
534 return -ENODEV;
535 }
536
537 if (!match) {
538 dev_err(dev, "No available vop found for display-subsystem.\n");
539 return -ENODEV;
540 }
541 /*
542 * For each bound crtc, bind the encoders attached to its
543 * remote endpoint.
544 */
545 for (i = 0;; i++) {
546 port = of_parse_phandle(np, "ports", i);
547 if (!port)
548 break;
549
550 if (!of_device_is_available(port->parent)) {
551 of_node_put(port);
552 continue;
553 }
554
555 rockchip_add_endpoints(dev, &match, port);
556 of_node_put(port);
557 }
558
559 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
Mark Yao2048e322014-08-22 18:36:26 +0800560}
561
562static int rockchip_drm_platform_remove(struct platform_device *pdev)
563{
564 component_master_del(&pdev->dev, &rockchip_drm_ops);
565
566 return 0;
567}
568
569static const struct of_device_id rockchip_drm_dt_ids[] = {
570 { .compatible = "rockchip,display-subsystem", },
571 { /* sentinel */ },
572};
573MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
574
575static struct platform_driver rockchip_drm_platform_driver = {
576 .probe = rockchip_drm_platform_probe,
577 .remove = rockchip_drm_platform_remove,
578 .driver = {
Mark Yao2048e322014-08-22 18:36:26 +0800579 .name = "rockchip-drm",
580 .of_match_table = rockchip_drm_dt_ids,
581 .pm = &rockchip_drm_pm_ops,
582 },
583};
584
585module_platform_driver(rockchip_drm_platform_driver);
586
587MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
588MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
589MODULE_LICENSE("GPL v2");