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>
22 #include <linux/dma-mapping.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/of_graph.h>
25 #include <linux/component.h>
26 
27 #include "rockchip_drm_drv.h"
28 #include "rockchip_drm_fb.h"
29 #include "rockchip_drm_fbdev.h"
30 #include "rockchip_drm_gem.h"
31 
32 #define DRIVER_NAME	"rockchip"
33 #define DRIVER_DESC	"RockChip Soc DRM"
34 #define DRIVER_DATE	"20140818"
35 #define DRIVER_MAJOR	1
36 #define DRIVER_MINOR	0
37 
38 /*
39  * Attach a (component) device to the shared drm dma mapping from master drm
40  * device.  This is used by the VOPs to map GEM buffers to a common DMA
41  * mapping.
42  */
43 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
44 				   struct device *dev)
45 {
46 	struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping;
47 	int ret;
48 
49 	ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
50 	if (ret)
51 		return ret;
52 
53 	dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
54 
55 	return arm_iommu_attach_device(dev, mapping);
56 }
57 EXPORT_SYMBOL_GPL(rockchip_drm_dma_attach_device);
58 
59 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
60 				    struct device *dev)
61 {
62 	arm_iommu_detach_device(dev);
63 }
64 EXPORT_SYMBOL_GPL(rockchip_drm_dma_detach_device);
65 
66 int rockchip_register_crtc_funcs(struct drm_device *dev,
67 				 const struct rockchip_crtc_funcs *crtc_funcs,
68 				 int pipe)
69 {
70 	struct rockchip_drm_private *priv = dev->dev_private;
71 
72 	if (pipe > ROCKCHIP_MAX_CRTC)
73 		return -EINVAL;
74 
75 	priv->crtc_funcs[pipe] = crtc_funcs;
76 
77 	return 0;
78 }
79 EXPORT_SYMBOL_GPL(rockchip_register_crtc_funcs);
80 
81 void rockchip_unregister_crtc_funcs(struct drm_device *dev, int pipe)
82 {
83 	struct rockchip_drm_private *priv = dev->dev_private;
84 
85 	if (pipe > ROCKCHIP_MAX_CRTC)
86 		return;
87 
88 	priv->crtc_funcs[pipe] = NULL;
89 }
90 EXPORT_SYMBOL_GPL(rockchip_unregister_crtc_funcs);
91 
92 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
93 						int pipe)
94 {
95 	struct drm_crtc *crtc;
96 	int i = 0;
97 
98 	list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
99 		if (i++ == pipe)
100 			return crtc;
101 
102 	return NULL;
103 }
104 
105 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
106 {
107 	struct rockchip_drm_private *priv = dev->dev_private;
108 	struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
109 
110 	if (crtc && priv->crtc_funcs[pipe] &&
111 	    priv->crtc_funcs[pipe]->enable_vblank)
112 		return priv->crtc_funcs[pipe]->enable_vblank(crtc);
113 
114 	return 0;
115 }
116 
117 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
118 {
119 	struct rockchip_drm_private *priv = dev->dev_private;
120 	struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
121 
122 	if (crtc && priv->crtc_funcs[pipe] &&
123 	    priv->crtc_funcs[pipe]->enable_vblank)
124 		priv->crtc_funcs[pipe]->disable_vblank(crtc);
125 }
126 
127 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
128 {
129 	struct rockchip_drm_private *private;
130 	struct dma_iommu_mapping *mapping;
131 	struct device *dev = drm_dev->dev;
132 	struct drm_connector *connector;
133 	int ret;
134 
135 	private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
136 	if (!private)
137 		return -ENOMEM;
138 
139 	drm_dev->dev_private = private;
140 
141 	drm_mode_config_init(drm_dev);
142 
143 	rockchip_drm_mode_config_init(drm_dev);
144 
145 	dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
146 				      GFP_KERNEL);
147 	if (!dev->dma_parms) {
148 		ret = -ENOMEM;
149 		goto err_config_cleanup;
150 	}
151 
152 	/* TODO(djkurtz): fetch the mapping start/size from somewhere */
153 	mapping = arm_iommu_create_mapping(&platform_bus_type, 0x00000000,
154 					   SZ_2G);
155 	if (IS_ERR(mapping)) {
156 		ret = PTR_ERR(mapping);
157 		goto err_config_cleanup;
158 	}
159 
160 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
161 	if (ret)
162 		goto err_release_mapping;
163 
164 	dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
165 
166 	ret = arm_iommu_attach_device(dev, mapping);
167 	if (ret)
168 		goto err_release_mapping;
169 
170 	/* Try to bind all sub drivers. */
171 	ret = component_bind_all(dev, drm_dev);
172 	if (ret)
173 		goto err_detach_device;
174 
175 	/*
176 	 * All components are now added, we can publish the connector sysfs
177 	 * entries to userspace.  This will generate hotplug events and so
178 	 * userspace will expect to be able to access DRM at this point.
179 	 */
180 	list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
181 			head) {
182 		ret = drm_connector_register(connector);
183 		if (ret) {
184 			dev_err(drm_dev->dev,
185 				"[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
186 				connector->base.id,
187 				connector->name, ret);
188 			goto err_unbind;
189 		}
190 	}
191 
192 	/* init kms poll for handling hpd */
193 	drm_kms_helper_poll_init(drm_dev);
194 
195 	/*
196 	 * enable drm irq mode.
197 	 * - with irq_enabled = true, we can use the vblank feature.
198 	 */
199 	drm_dev->irq_enabled = true;
200 
201 	ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
202 	if (ret)
203 		goto err_kms_helper_poll_fini;
204 
205 	/*
206 	 * with vblank_disable_allowed = true, vblank interrupt will be disabled
207 	 * by drm timer once a current process gives up ownership of
208 	 * vblank event.(after drm_vblank_put function is called)
209 	 */
210 	drm_dev->vblank_disable_allowed = true;
211 
212 	ret = rockchip_drm_fbdev_init(drm_dev);
213 	if (ret)
214 		goto err_vblank_cleanup;
215 
216 	return 0;
217 err_vblank_cleanup:
218 	drm_vblank_cleanup(drm_dev);
219 err_kms_helper_poll_fini:
220 	drm_kms_helper_poll_fini(drm_dev);
221 err_unbind:
222 	component_unbind_all(dev, drm_dev);
223 err_detach_device:
224 	arm_iommu_detach_device(dev);
225 err_release_mapping:
226 	arm_iommu_release_mapping(dev->archdata.mapping);
227 err_config_cleanup:
228 	drm_mode_config_cleanup(drm_dev);
229 	drm_dev->dev_private = NULL;
230 	return ret;
231 }
232 
233 static int rockchip_drm_unload(struct drm_device *drm_dev)
234 {
235 	struct device *dev = drm_dev->dev;
236 
237 	rockchip_drm_fbdev_fini(drm_dev);
238 	drm_vblank_cleanup(drm_dev);
239 	drm_kms_helper_poll_fini(drm_dev);
240 	component_unbind_all(dev, drm_dev);
241 	arm_iommu_detach_device(dev);
242 	arm_iommu_release_mapping(dev->archdata.mapping);
243 	drm_mode_config_cleanup(drm_dev);
244 	drm_dev->dev_private = NULL;
245 
246 	return 0;
247 }
248 
249 void rockchip_drm_lastclose(struct drm_device *dev)
250 {
251 	struct rockchip_drm_private *priv = dev->dev_private;
252 
253 	drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
254 }
255 
256 static const struct file_operations rockchip_drm_driver_fops = {
257 	.owner = THIS_MODULE,
258 	.open = drm_open,
259 	.mmap = rockchip_gem_mmap,
260 	.poll = drm_poll,
261 	.read = drm_read,
262 	.unlocked_ioctl = drm_ioctl,
263 #ifdef CONFIG_COMPAT
264 	.compat_ioctl = drm_compat_ioctl,
265 #endif
266 	.release = drm_release,
267 };
268 
269 const struct vm_operations_struct rockchip_drm_vm_ops = {
270 	.open = drm_gem_vm_open,
271 	.close = drm_gem_vm_close,
272 };
273 
274 static struct drm_driver rockchip_drm_driver = {
275 	.driver_features	= DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
276 	.load			= rockchip_drm_load,
277 	.unload			= rockchip_drm_unload,
278 	.lastclose		= rockchip_drm_lastclose,
279 	.get_vblank_counter	= drm_vblank_count,
280 	.enable_vblank		= rockchip_drm_crtc_enable_vblank,
281 	.disable_vblank		= rockchip_drm_crtc_disable_vblank,
282 	.gem_vm_ops		= &rockchip_drm_vm_ops,
283 	.gem_free_object	= rockchip_gem_free_object,
284 	.dumb_create		= rockchip_gem_dumb_create,
285 	.dumb_map_offset	= rockchip_gem_dumb_map_offset,
286 	.dumb_destroy		= drm_gem_dumb_destroy,
287 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
288 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
289 	.gem_prime_import	= drm_gem_prime_import,
290 	.gem_prime_export	= drm_gem_prime_export,
291 	.gem_prime_get_sg_table	= rockchip_gem_prime_get_sg_table,
292 	.gem_prime_vmap		= rockchip_gem_prime_vmap,
293 	.gem_prime_vunmap	= rockchip_gem_prime_vunmap,
294 	.gem_prime_mmap		= rockchip_gem_mmap_buf,
295 	.fops			= &rockchip_drm_driver_fops,
296 	.name	= DRIVER_NAME,
297 	.desc	= DRIVER_DESC,
298 	.date	= DRIVER_DATE,
299 	.major	= DRIVER_MAJOR,
300 	.minor	= DRIVER_MINOR,
301 };
302 
303 #ifdef CONFIG_PM_SLEEP
304 static int rockchip_drm_sys_suspend(struct device *dev)
305 {
306 	struct drm_device *drm = dev_get_drvdata(dev);
307 	struct drm_connector *connector;
308 
309 	if (!drm)
310 		return 0;
311 
312 	drm_modeset_lock_all(drm);
313 	list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
314 		int old_dpms = connector->dpms;
315 
316 		if (connector->funcs->dpms)
317 			connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
318 
319 		/* Set the old mode back to the connector for resume */
320 		connector->dpms = old_dpms;
321 	}
322 	drm_modeset_unlock_all(drm);
323 
324 	return 0;
325 }
326 
327 static int rockchip_drm_sys_resume(struct device *dev)
328 {
329 	struct drm_device *drm = dev_get_drvdata(dev);
330 	struct drm_connector *connector;
331 	enum drm_connector_status status;
332 	bool changed = false;
333 
334 	if (!drm)
335 		return 0;
336 
337 	drm_modeset_lock_all(drm);
338 	list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
339 		int desired_mode = connector->dpms;
340 
341 		/*
342 		 * at suspend time, we save dpms to connector->dpms,
343 		 * restore the old_dpms, and at current time, the connector
344 		 * dpms status must be DRM_MODE_DPMS_OFF.
345 		 */
346 		connector->dpms = DRM_MODE_DPMS_OFF;
347 
348 		/*
349 		 * If the connector has been disconnected during suspend,
350 		 * disconnect it from the encoder and leave it off. We'll notify
351 		 * userspace at the end.
352 		 */
353 		if (desired_mode == DRM_MODE_DPMS_ON) {
354 			status = connector->funcs->detect(connector, true);
355 			if (status == connector_status_disconnected) {
356 				connector->encoder = NULL;
357 				connector->status = status;
358 				changed = true;
359 				continue;
360 			}
361 		}
362 		if (connector->funcs->dpms)
363 			connector->funcs->dpms(connector, desired_mode);
364 	}
365 	drm_modeset_unlock_all(drm);
366 
367 	drm_helper_resume_force_mode(drm);
368 
369 	if (changed)
370 		drm_kms_helper_hotplug_event(drm);
371 
372 	return 0;
373 }
374 #endif
375 
376 static const struct dev_pm_ops rockchip_drm_pm_ops = {
377 	SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
378 				rockchip_drm_sys_resume)
379 };
380 
381 /*
382  * @node: device tree node containing encoder input ports
383  * @encoder: drm_encoder
384  */
385 int rockchip_drm_encoder_get_mux_id(struct device_node *node,
386 				    struct drm_encoder *encoder)
387 {
388 	struct device_node *ep;
389 	struct drm_crtc *crtc = encoder->crtc;
390 	struct of_endpoint endpoint;
391 	struct device_node *port;
392 	int ret;
393 
394 	if (!node || !crtc)
395 		return -EINVAL;
396 
397 	for_each_endpoint_of_node(node, ep) {
398 		port = of_graph_get_remote_port(ep);
399 		of_node_put(port);
400 		if (port == crtc->port) {
401 			ret = of_graph_parse_endpoint(ep, &endpoint);
402 			of_node_put(ep);
403 			return ret ?: endpoint.id;
404 		}
405 	}
406 
407 	return -EINVAL;
408 }
409 EXPORT_SYMBOL_GPL(rockchip_drm_encoder_get_mux_id);
410 
411 static int compare_of(struct device *dev, void *data)
412 {
413 	struct device_node *np = data;
414 
415 	return dev->of_node == np;
416 }
417 
418 static void rockchip_add_endpoints(struct device *dev,
419 				   struct component_match **match,
420 				   struct device_node *port)
421 {
422 	struct device_node *ep, *remote;
423 
424 	for_each_child_of_node(port, ep) {
425 		remote = of_graph_get_remote_port_parent(ep);
426 		if (!remote || !of_device_is_available(remote)) {
427 			of_node_put(remote);
428 			continue;
429 		} else if (!of_device_is_available(remote->parent)) {
430 			dev_warn(dev, "parent device of %s is not available\n",
431 				 remote->full_name);
432 			of_node_put(remote);
433 			continue;
434 		}
435 
436 		component_match_add(dev, match, compare_of, remote);
437 		of_node_put(remote);
438 	}
439 }
440 
441 static int rockchip_drm_bind(struct device *dev)
442 {
443 	struct drm_device *drm;
444 	int ret;
445 
446 	drm = drm_dev_alloc(&rockchip_drm_driver, dev);
447 	if (!drm)
448 		return -ENOMEM;
449 
450 	ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
451 	if (ret)
452 		goto err_free;
453 
454 	ret = drm_dev_register(drm, 0);
455 	if (ret)
456 		goto err_free;
457 
458 	dev_set_drvdata(dev, drm);
459 
460 	return 0;
461 
462 err_free:
463 	drm_dev_unref(drm);
464 	return ret;
465 }
466 
467 static void rockchip_drm_unbind(struct device *dev)
468 {
469 	struct drm_device *drm = dev_get_drvdata(dev);
470 
471 	drm_dev_unregister(drm);
472 	drm_dev_unref(drm);
473 	dev_set_drvdata(dev, NULL);
474 }
475 
476 static const struct component_master_ops rockchip_drm_ops = {
477 	.bind = rockchip_drm_bind,
478 	.unbind = rockchip_drm_unbind,
479 };
480 
481 static int rockchip_drm_platform_probe(struct platform_device *pdev)
482 {
483 	struct device *dev = &pdev->dev;
484 	struct component_match *match = NULL;
485 	struct device_node *np = dev->of_node;
486 	struct device_node *port;
487 	int i;
488 
489 	if (!np)
490 		return -ENODEV;
491 	/*
492 	 * Bind the crtc ports first, so that
493 	 * drm_of_find_possible_crtcs called from encoder .bind callbacks
494 	 * works as expected.
495 	 */
496 	for (i = 0;; i++) {
497 		port = of_parse_phandle(np, "ports", i);
498 		if (!port)
499 			break;
500 
501 		if (!of_device_is_available(port->parent)) {
502 			of_node_put(port);
503 			continue;
504 		}
505 
506 		component_match_add(dev, &match, compare_of, port->parent);
507 		of_node_put(port);
508 	}
509 
510 	if (i == 0) {
511 		dev_err(dev, "missing 'ports' property\n");
512 		return -ENODEV;
513 	}
514 
515 	if (!match) {
516 		dev_err(dev, "No available vop found for display-subsystem.\n");
517 		return -ENODEV;
518 	}
519 	/*
520 	 * For each bound crtc, bind the encoders attached to its
521 	 * remote endpoint.
522 	 */
523 	for (i = 0;; i++) {
524 		port = of_parse_phandle(np, "ports", i);
525 		if (!port)
526 			break;
527 
528 		if (!of_device_is_available(port->parent)) {
529 			of_node_put(port);
530 			continue;
531 		}
532 
533 		rockchip_add_endpoints(dev, &match, port);
534 		of_node_put(port);
535 	}
536 
537 	return component_master_add_with_match(dev, &rockchip_drm_ops, match);
538 }
539 
540 static int rockchip_drm_platform_remove(struct platform_device *pdev)
541 {
542 	component_master_del(&pdev->dev, &rockchip_drm_ops);
543 
544 	return 0;
545 }
546 
547 static const struct of_device_id rockchip_drm_dt_ids[] = {
548 	{ .compatible = "rockchip,display-subsystem", },
549 	{ /* sentinel */ },
550 };
551 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
552 
553 static struct platform_driver rockchip_drm_platform_driver = {
554 	.probe = rockchip_drm_platform_probe,
555 	.remove = rockchip_drm_platform_remove,
556 	.driver = {
557 		.owner = THIS_MODULE,
558 		.name = "rockchip-drm",
559 		.of_match_table = rockchip_drm_dt_ids,
560 		.pm = &rockchip_drm_pm_ops,
561 	},
562 };
563 
564 module_platform_driver(rockchip_drm_platform_driver);
565 
566 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
567 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
568 MODULE_LICENSE("GPL v2");
569