xref: /openbmc/linux/drivers/gpu/drm/arm/malidp_drv.c (revision 12ae57aa)
1ad49f860SLiviu Dudau /*
2ad49f860SLiviu Dudau  * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
3ad49f860SLiviu Dudau  * Author: Liviu Dudau <Liviu.Dudau@arm.com>
4ad49f860SLiviu Dudau  *
5ad49f860SLiviu Dudau  * This program is free software and is provided to you under the terms of the
6ad49f860SLiviu Dudau  * GNU General Public License version 2 as published by the Free Software
7ad49f860SLiviu Dudau  * Foundation, and any use by you of this program is subject to the terms
8ad49f860SLiviu Dudau  * of such GNU licence.
9ad49f860SLiviu Dudau  *
10ad49f860SLiviu Dudau  * ARM Mali DP500/DP550/DP650 KMS/DRM driver
11ad49f860SLiviu Dudau  */
12ad49f860SLiviu Dudau 
13ad49f860SLiviu Dudau #include <linux/module.h>
14ad49f860SLiviu Dudau #include <linux/clk.h>
15ad49f860SLiviu Dudau #include <linux/component.h>
16ad49f860SLiviu Dudau #include <linux/of_device.h>
17ad49f860SLiviu Dudau #include <linux/of_graph.h>
18ad49f860SLiviu Dudau #include <linux/of_reserved_mem.h>
19ad49f860SLiviu Dudau 
20ad49f860SLiviu Dudau #include <drm/drmP.h>
21ad49f860SLiviu Dudau #include <drm/drm_atomic.h>
22ad49f860SLiviu Dudau #include <drm/drm_atomic_helper.h>
23ad49f860SLiviu Dudau #include <drm/drm_crtc.h>
24ad49f860SLiviu Dudau #include <drm/drm_crtc_helper.h>
25ad49f860SLiviu Dudau #include <drm/drm_fb_helper.h>
26ad49f860SLiviu Dudau #include <drm/drm_fb_cma_helper.h>
27ad49f860SLiviu Dudau #include <drm/drm_gem_cma_helper.h>
28ad49f860SLiviu Dudau #include <drm/drm_of.h>
29ad49f860SLiviu Dudau 
30ad49f860SLiviu Dudau #include "malidp_drv.h"
31ad49f860SLiviu Dudau #include "malidp_regs.h"
32ad49f860SLiviu Dudau #include "malidp_hw.h"
33ad49f860SLiviu Dudau 
34ad49f860SLiviu Dudau #define MALIDP_CONF_VALID_TIMEOUT	250
35ad49f860SLiviu Dudau 
36ad49f860SLiviu Dudau /*
37ad49f860SLiviu Dudau  * set the "config valid" bit and wait until the hardware acts on it
38ad49f860SLiviu Dudau  */
39ad49f860SLiviu Dudau static int malidp_set_and_wait_config_valid(struct drm_device *drm)
40ad49f860SLiviu Dudau {
41ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
42ad49f860SLiviu Dudau 	struct malidp_hw_device *hwdev = malidp->dev;
43ad49f860SLiviu Dudau 	int ret;
44ad49f860SLiviu Dudau 
45ad49f860SLiviu Dudau 	hwdev->set_config_valid(hwdev);
46ad49f860SLiviu Dudau 	/* don't wait for config_valid flag if we are in config mode */
47ad49f860SLiviu Dudau 	if (hwdev->in_config_mode(hwdev))
48ad49f860SLiviu Dudau 		return 0;
49ad49f860SLiviu Dudau 
50ad49f860SLiviu Dudau 	ret = wait_event_interruptible_timeout(malidp->wq,
51ad49f860SLiviu Dudau 			atomic_read(&malidp->config_valid) == 1,
52ad49f860SLiviu Dudau 			msecs_to_jiffies(MALIDP_CONF_VALID_TIMEOUT));
53ad49f860SLiviu Dudau 
54ad49f860SLiviu Dudau 	return (ret > 0) ? 0 : -ETIMEDOUT;
55ad49f860SLiviu Dudau }
56ad49f860SLiviu Dudau 
57ad49f860SLiviu Dudau static void malidp_output_poll_changed(struct drm_device *drm)
58ad49f860SLiviu Dudau {
59ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
60ad49f860SLiviu Dudau 
61ad49f860SLiviu Dudau 	drm_fbdev_cma_hotplug_event(malidp->fbdev);
62ad49f860SLiviu Dudau }
63ad49f860SLiviu Dudau 
64ad49f860SLiviu Dudau static void malidp_atomic_commit_hw_done(struct drm_atomic_state *state)
65ad49f860SLiviu Dudau {
66ad49f860SLiviu Dudau 	struct drm_pending_vblank_event *event;
67ad49f860SLiviu Dudau 	struct drm_device *drm = state->dev;
68ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
69ad49f860SLiviu Dudau 	int ret = malidp_set_and_wait_config_valid(drm);
70ad49f860SLiviu Dudau 
71ad49f860SLiviu Dudau 	if (ret)
72ad49f860SLiviu Dudau 		DRM_DEBUG_DRIVER("timed out waiting for updated configuration\n");
73ad49f860SLiviu Dudau 
74ad49f860SLiviu Dudau 	event = malidp->crtc.state->event;
75ad49f860SLiviu Dudau 	if (event) {
76ad49f860SLiviu Dudau 		malidp->crtc.state->event = NULL;
77ad49f860SLiviu Dudau 
78ad49f860SLiviu Dudau 		spin_lock_irq(&drm->event_lock);
79ad49f860SLiviu Dudau 		if (drm_crtc_vblank_get(&malidp->crtc) == 0)
80ad49f860SLiviu Dudau 			drm_crtc_arm_vblank_event(&malidp->crtc, event);
81ad49f860SLiviu Dudau 		else
82ad49f860SLiviu Dudau 			drm_crtc_send_vblank_event(&malidp->crtc, event);
83ad49f860SLiviu Dudau 		spin_unlock_irq(&drm->event_lock);
84ad49f860SLiviu Dudau 	}
85ad49f860SLiviu Dudau 	drm_atomic_helper_commit_hw_done(state);
86ad49f860SLiviu Dudau }
87ad49f860SLiviu Dudau 
88ad49f860SLiviu Dudau static void malidp_atomic_commit_tail(struct drm_atomic_state *state)
89ad49f860SLiviu Dudau {
90ad49f860SLiviu Dudau 	struct drm_device *drm = state->dev;
91ad49f860SLiviu Dudau 
92ad49f860SLiviu Dudau 	drm_atomic_helper_commit_modeset_disables(drm, state);
93ad49f860SLiviu Dudau 	drm_atomic_helper_commit_modeset_enables(drm, state);
94ad49f860SLiviu Dudau 	drm_atomic_helper_commit_planes(drm, state, true);
95ad49f860SLiviu Dudau 
96ad49f860SLiviu Dudau 	malidp_atomic_commit_hw_done(state);
97ad49f860SLiviu Dudau 
98ad49f860SLiviu Dudau 	drm_atomic_helper_wait_for_vblanks(drm, state);
99ad49f860SLiviu Dudau 
100ad49f860SLiviu Dudau 	drm_atomic_helper_cleanup_planes(drm, state);
101ad49f860SLiviu Dudau }
102ad49f860SLiviu Dudau 
103ad49f860SLiviu Dudau static struct drm_mode_config_helper_funcs malidp_mode_config_helpers = {
104ad49f860SLiviu Dudau 	.atomic_commit_tail = malidp_atomic_commit_tail,
105ad49f860SLiviu Dudau };
106ad49f860SLiviu Dudau 
107ad49f860SLiviu Dudau static const struct drm_mode_config_funcs malidp_mode_config_funcs = {
108ad49f860SLiviu Dudau 	.fb_create = drm_fb_cma_create,
109ad49f860SLiviu Dudau 	.output_poll_changed = malidp_output_poll_changed,
110ad49f860SLiviu Dudau 	.atomic_check = drm_atomic_helper_check,
111ad49f860SLiviu Dudau 	.atomic_commit = drm_atomic_helper_commit,
112ad49f860SLiviu Dudau };
113ad49f860SLiviu Dudau 
114ad49f860SLiviu Dudau static int malidp_enable_vblank(struct drm_device *drm, unsigned int crtc)
115ad49f860SLiviu Dudau {
116ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
117ad49f860SLiviu Dudau 	struct malidp_hw_device *hwdev = malidp->dev;
118ad49f860SLiviu Dudau 
119ad49f860SLiviu Dudau 	malidp_hw_enable_irq(hwdev, MALIDP_DE_BLOCK,
120ad49f860SLiviu Dudau 			     hwdev->map.de_irq_map.vsync_irq);
121ad49f860SLiviu Dudau 	return 0;
122ad49f860SLiviu Dudau }
123ad49f860SLiviu Dudau 
124ad49f860SLiviu Dudau static void malidp_disable_vblank(struct drm_device *drm, unsigned int pipe)
125ad49f860SLiviu Dudau {
126ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
127ad49f860SLiviu Dudau 	struct malidp_hw_device *hwdev = malidp->dev;
128ad49f860SLiviu Dudau 
129ad49f860SLiviu Dudau 	malidp_hw_disable_irq(hwdev, MALIDP_DE_BLOCK,
130ad49f860SLiviu Dudau 			      hwdev->map.de_irq_map.vsync_irq);
131ad49f860SLiviu Dudau }
132ad49f860SLiviu Dudau 
133ad49f860SLiviu Dudau static int malidp_init(struct drm_device *drm)
134ad49f860SLiviu Dudau {
135ad49f860SLiviu Dudau 	int ret;
136ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
137ad49f860SLiviu Dudau 	struct malidp_hw_device *hwdev = malidp->dev;
138ad49f860SLiviu Dudau 
139ad49f860SLiviu Dudau 	drm_mode_config_init(drm);
140ad49f860SLiviu Dudau 
141ad49f860SLiviu Dudau 	drm->mode_config.min_width = hwdev->min_line_size;
142ad49f860SLiviu Dudau 	drm->mode_config.min_height = hwdev->min_line_size;
143ad49f860SLiviu Dudau 	drm->mode_config.max_width = hwdev->max_line_size;
144ad49f860SLiviu Dudau 	drm->mode_config.max_height = hwdev->max_line_size;
145ad49f860SLiviu Dudau 	drm->mode_config.funcs = &malidp_mode_config_funcs;
146ad49f860SLiviu Dudau 	drm->mode_config.helper_private = &malidp_mode_config_helpers;
147ad49f860SLiviu Dudau 
148ad49f860SLiviu Dudau 	ret = malidp_crtc_init(drm);
149ad49f860SLiviu Dudau 	if (ret) {
150ad49f860SLiviu Dudau 		drm_mode_config_cleanup(drm);
151ad49f860SLiviu Dudau 		return ret;
152ad49f860SLiviu Dudau 	}
153ad49f860SLiviu Dudau 
154ad49f860SLiviu Dudau 	return 0;
155ad49f860SLiviu Dudau }
156ad49f860SLiviu Dudau 
157ad49f860SLiviu Dudau static int malidp_irq_init(struct platform_device *pdev)
158ad49f860SLiviu Dudau {
159ad49f860SLiviu Dudau 	int irq_de, irq_se, ret = 0;
160ad49f860SLiviu Dudau 	struct drm_device *drm = dev_get_drvdata(&pdev->dev);
161ad49f860SLiviu Dudau 
162ad49f860SLiviu Dudau 	/* fetch the interrupts from DT */
163ad49f860SLiviu Dudau 	irq_de = platform_get_irq_byname(pdev, "DE");
164ad49f860SLiviu Dudau 	if (irq_de < 0) {
165ad49f860SLiviu Dudau 		DRM_ERROR("no 'DE' IRQ specified!\n");
166ad49f860SLiviu Dudau 		return irq_de;
167ad49f860SLiviu Dudau 	}
168ad49f860SLiviu Dudau 	irq_se = platform_get_irq_byname(pdev, "SE");
169ad49f860SLiviu Dudau 	if (irq_se < 0) {
170ad49f860SLiviu Dudau 		DRM_ERROR("no 'SE' IRQ specified!\n");
171ad49f860SLiviu Dudau 		return irq_se;
172ad49f860SLiviu Dudau 	}
173ad49f860SLiviu Dudau 
174ad49f860SLiviu Dudau 	ret = malidp_de_irq_init(drm, irq_de);
175ad49f860SLiviu Dudau 	if (ret)
176ad49f860SLiviu Dudau 		return ret;
177ad49f860SLiviu Dudau 
178ad49f860SLiviu Dudau 	ret = malidp_se_irq_init(drm, irq_se);
179ad49f860SLiviu Dudau 	if (ret) {
180ad49f860SLiviu Dudau 		malidp_de_irq_fini(drm);
181ad49f860SLiviu Dudau 		return ret;
182ad49f860SLiviu Dudau 	}
183ad49f860SLiviu Dudau 
184ad49f860SLiviu Dudau 	return 0;
185ad49f860SLiviu Dudau }
186ad49f860SLiviu Dudau 
187ad49f860SLiviu Dudau static void malidp_lastclose(struct drm_device *drm)
188ad49f860SLiviu Dudau {
189ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
190ad49f860SLiviu Dudau 
191ad49f860SLiviu Dudau 	drm_fbdev_cma_restore_mode(malidp->fbdev);
192ad49f860SLiviu Dudau }
193ad49f860SLiviu Dudau 
194ad49f860SLiviu Dudau static const struct file_operations fops = {
195ad49f860SLiviu Dudau 	.owner = THIS_MODULE,
196ad49f860SLiviu Dudau 	.open = drm_open,
197ad49f860SLiviu Dudau 	.release = drm_release,
198ad49f860SLiviu Dudau 	.unlocked_ioctl = drm_ioctl,
199ad49f860SLiviu Dudau #ifdef CONFIG_COMPAT
200ad49f860SLiviu Dudau 	.compat_ioctl = drm_compat_ioctl,
201ad49f860SLiviu Dudau #endif
202ad49f860SLiviu Dudau 	.poll = drm_poll,
203ad49f860SLiviu Dudau 	.read = drm_read,
204ad49f860SLiviu Dudau 	.llseek = noop_llseek,
205ad49f860SLiviu Dudau 	.mmap = drm_gem_cma_mmap,
206ad49f860SLiviu Dudau };
207ad49f860SLiviu Dudau 
208ad49f860SLiviu Dudau static struct drm_driver malidp_driver = {
209ad49f860SLiviu Dudau 	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC |
210ad49f860SLiviu Dudau 			   DRIVER_PRIME,
211ad49f860SLiviu Dudau 	.lastclose = malidp_lastclose,
212ad49f860SLiviu Dudau 	.get_vblank_counter = drm_vblank_no_hw_counter,
213ad49f860SLiviu Dudau 	.enable_vblank = malidp_enable_vblank,
214ad49f860SLiviu Dudau 	.disable_vblank = malidp_disable_vblank,
215ad49f860SLiviu Dudau 	.gem_free_object_unlocked = drm_gem_cma_free_object,
216ad49f860SLiviu Dudau 	.gem_vm_ops = &drm_gem_cma_vm_ops,
217ad49f860SLiviu Dudau 	.dumb_create = drm_gem_cma_dumb_create,
218ad49f860SLiviu Dudau 	.dumb_map_offset = drm_gem_cma_dumb_map_offset,
219ad49f860SLiviu Dudau 	.dumb_destroy = drm_gem_dumb_destroy,
220ad49f860SLiviu Dudau 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
221ad49f860SLiviu Dudau 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
222ad49f860SLiviu Dudau 	.gem_prime_export = drm_gem_prime_export,
223ad49f860SLiviu Dudau 	.gem_prime_import = drm_gem_prime_import,
224ad49f860SLiviu Dudau 	.gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
225ad49f860SLiviu Dudau 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
226ad49f860SLiviu Dudau 	.gem_prime_vmap = drm_gem_cma_prime_vmap,
227ad49f860SLiviu Dudau 	.gem_prime_vunmap = drm_gem_cma_prime_vunmap,
228ad49f860SLiviu Dudau 	.gem_prime_mmap = drm_gem_cma_prime_mmap,
229ad49f860SLiviu Dudau 	.fops = &fops,
230ad49f860SLiviu Dudau 	.name = "mali-dp",
231ad49f860SLiviu Dudau 	.desc = "ARM Mali Display Processor driver",
232ad49f860SLiviu Dudau 	.date = "20160106",
233ad49f860SLiviu Dudau 	.major = 1,
234ad49f860SLiviu Dudau 	.minor = 0,
235ad49f860SLiviu Dudau };
236ad49f860SLiviu Dudau 
237ad49f860SLiviu Dudau static const struct of_device_id  malidp_drm_of_match[] = {
238ad49f860SLiviu Dudau 	{
239ad49f860SLiviu Dudau 		.compatible = "arm,mali-dp500",
240ad49f860SLiviu Dudau 		.data = &malidp_device[MALIDP_500]
241ad49f860SLiviu Dudau 	},
242ad49f860SLiviu Dudau 	{
243ad49f860SLiviu Dudau 		.compatible = "arm,mali-dp550",
244ad49f860SLiviu Dudau 		.data = &malidp_device[MALIDP_550]
245ad49f860SLiviu Dudau 	},
246ad49f860SLiviu Dudau 	{
247ad49f860SLiviu Dudau 		.compatible = "arm,mali-dp650",
248ad49f860SLiviu Dudau 		.data = &malidp_device[MALIDP_650]
249ad49f860SLiviu Dudau 	},
250ad49f860SLiviu Dudau 	{},
251ad49f860SLiviu Dudau };
252ad49f860SLiviu Dudau MODULE_DEVICE_TABLE(of, malidp_drm_of_match);
253ad49f860SLiviu Dudau 
254ad49f860SLiviu Dudau #define MAX_OUTPUT_CHANNELS	3
255ad49f860SLiviu Dudau 
256ad49f860SLiviu Dudau static int malidp_bind(struct device *dev)
257ad49f860SLiviu Dudau {
258ad49f860SLiviu Dudau 	struct resource *res;
259ad49f860SLiviu Dudau 	struct drm_device *drm;
2603c31760eSBrian Starkey 	struct device_node *ep;
261ad49f860SLiviu Dudau 	struct malidp_drm *malidp;
262ad49f860SLiviu Dudau 	struct malidp_hw_device *hwdev;
263ad49f860SLiviu Dudau 	struct platform_device *pdev = to_platform_device(dev);
264ad49f860SLiviu Dudau 	/* number of lines for the R, G and B output */
265ad49f860SLiviu Dudau 	u8 output_width[MAX_OUTPUT_CHANNELS];
266ad49f860SLiviu Dudau 	int ret = 0, i;
267ad49f860SLiviu Dudau 	u32 version, out_depth = 0;
268ad49f860SLiviu Dudau 
269ad49f860SLiviu Dudau 	malidp = devm_kzalloc(dev, sizeof(*malidp), GFP_KERNEL);
270ad49f860SLiviu Dudau 	if (!malidp)
271ad49f860SLiviu Dudau 		return -ENOMEM;
272ad49f860SLiviu Dudau 
273ad49f860SLiviu Dudau 	hwdev = devm_kzalloc(dev, sizeof(*hwdev), GFP_KERNEL);
274ad49f860SLiviu Dudau 	if (!hwdev)
275ad49f860SLiviu Dudau 		return -ENOMEM;
276ad49f860SLiviu Dudau 
277ad49f860SLiviu Dudau 	/*
278ad49f860SLiviu Dudau 	 * copy the associated data from malidp_drm_of_match to avoid
279ad49f860SLiviu Dudau 	 * having to keep a reference to the OF node after binding
280ad49f860SLiviu Dudau 	 */
281ad49f860SLiviu Dudau 	memcpy(hwdev, of_device_get_match_data(dev), sizeof(*hwdev));
282ad49f860SLiviu Dudau 	malidp->dev = hwdev;
283ad49f860SLiviu Dudau 
284ad49f860SLiviu Dudau 	INIT_LIST_HEAD(&malidp->event_list);
285ad49f860SLiviu Dudau 
286ad49f860SLiviu Dudau 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
287ad49f860SLiviu Dudau 	hwdev->regs = devm_ioremap_resource(dev, res);
2881a9d71f8SWei Yongjun 	if (IS_ERR(hwdev->regs))
289ad49f860SLiviu Dudau 		return PTR_ERR(hwdev->regs);
290ad49f860SLiviu Dudau 
291ad49f860SLiviu Dudau 	hwdev->pclk = devm_clk_get(dev, "pclk");
292ad49f860SLiviu Dudau 	if (IS_ERR(hwdev->pclk))
293ad49f860SLiviu Dudau 		return PTR_ERR(hwdev->pclk);
294ad49f860SLiviu Dudau 
295ad49f860SLiviu Dudau 	hwdev->aclk = devm_clk_get(dev, "aclk");
296ad49f860SLiviu Dudau 	if (IS_ERR(hwdev->aclk))
297ad49f860SLiviu Dudau 		return PTR_ERR(hwdev->aclk);
298ad49f860SLiviu Dudau 
299ad49f860SLiviu Dudau 	hwdev->mclk = devm_clk_get(dev, "mclk");
300ad49f860SLiviu Dudau 	if (IS_ERR(hwdev->mclk))
301ad49f860SLiviu Dudau 		return PTR_ERR(hwdev->mclk);
302ad49f860SLiviu Dudau 
303ad49f860SLiviu Dudau 	hwdev->pxlclk = devm_clk_get(dev, "pxlclk");
304ad49f860SLiviu Dudau 	if (IS_ERR(hwdev->pxlclk))
305ad49f860SLiviu Dudau 		return PTR_ERR(hwdev->pxlclk);
306ad49f860SLiviu Dudau 
307ad49f860SLiviu Dudau 	/* Get the optional framebuffer memory resource */
308ad49f860SLiviu Dudau 	ret = of_reserved_mem_device_init(dev);
309ad49f860SLiviu Dudau 	if (ret && ret != -ENODEV)
310ad49f860SLiviu Dudau 		return ret;
311ad49f860SLiviu Dudau 
312ad49f860SLiviu Dudau 	drm = drm_dev_alloc(&malidp_driver, dev);
313ad49f860SLiviu Dudau 	if (!drm) {
314ad49f860SLiviu Dudau 		ret = -ENOMEM;
315ad49f860SLiviu Dudau 		goto alloc_fail;
316ad49f860SLiviu Dudau 	}
317ad49f860SLiviu Dudau 
318ad49f860SLiviu Dudau 	/* Enable APB clock in order to get access to the registers */
319ad49f860SLiviu Dudau 	clk_prepare_enable(hwdev->pclk);
320ad49f860SLiviu Dudau 	/*
321ad49f860SLiviu Dudau 	 * Enable AXI clock and main clock so that prefetch can start once
322ad49f860SLiviu Dudau 	 * the registers are set
323ad49f860SLiviu Dudau 	 */
324ad49f860SLiviu Dudau 	clk_prepare_enable(hwdev->aclk);
325ad49f860SLiviu Dudau 	clk_prepare_enable(hwdev->mclk);
326ad49f860SLiviu Dudau 
327ad49f860SLiviu Dudau 	ret = hwdev->query_hw(hwdev);
328ad49f860SLiviu Dudau 	if (ret) {
329ad49f860SLiviu Dudau 		DRM_ERROR("Invalid HW configuration\n");
330ad49f860SLiviu Dudau 		goto query_hw_fail;
331ad49f860SLiviu Dudau 	}
332ad49f860SLiviu Dudau 
333ad49f860SLiviu Dudau 	version = malidp_hw_read(hwdev, hwdev->map.dc_base + MALIDP_DE_CORE_ID);
334ad49f860SLiviu Dudau 	DRM_INFO("found ARM Mali-DP%3x version r%dp%d\n", version >> 16,
335ad49f860SLiviu Dudau 		 (version >> 12) & 0xf, (version >> 8) & 0xf);
336ad49f860SLiviu Dudau 
337ad49f860SLiviu Dudau 	/* set the number of lines used for output of RGB data */
338ad49f860SLiviu Dudau 	ret = of_property_read_u8_array(dev->of_node,
339ad49f860SLiviu Dudau 					"arm,malidp-output-port-lines",
340ad49f860SLiviu Dudau 					output_width, MAX_OUTPUT_CHANNELS);
341ad49f860SLiviu Dudau 	if (ret)
342ad49f860SLiviu Dudau 		goto query_hw_fail;
343ad49f860SLiviu Dudau 
344ad49f860SLiviu Dudau 	for (i = 0; i < MAX_OUTPUT_CHANNELS; i++)
345ad49f860SLiviu Dudau 		out_depth = (out_depth << 8) | (output_width[i] & 0xf);
346ad49f860SLiviu Dudau 	malidp_hw_write(hwdev, out_depth, hwdev->map.out_depth_base);
347ad49f860SLiviu Dudau 
348ad49f860SLiviu Dudau 	drm->dev_private = malidp;
349ad49f860SLiviu Dudau 	dev_set_drvdata(dev, drm);
350ad49f860SLiviu Dudau 	atomic_set(&malidp->config_valid, 0);
351ad49f860SLiviu Dudau 	init_waitqueue_head(&malidp->wq);
352ad49f860SLiviu Dudau 
353ad49f860SLiviu Dudau 	ret = malidp_init(drm);
354ad49f860SLiviu Dudau 	if (ret < 0)
355ad49f860SLiviu Dudau 		goto init_fail;
356ad49f860SLiviu Dudau 
357ad49f860SLiviu Dudau 	ret = drm_dev_register(drm, 0);
358ad49f860SLiviu Dudau 	if (ret)
359ad49f860SLiviu Dudau 		goto register_fail;
360ad49f860SLiviu Dudau 
361ad49f860SLiviu Dudau 	/* Set the CRTC's port so that the encoder component can find it */
3623c31760eSBrian Starkey 	ep = of_graph_get_next_endpoint(dev->of_node, NULL);
36312ae57aaSWei Yongjun 	if (!ep) {
36412ae57aaSWei Yongjun 		ret = -EINVAL;
3653c31760eSBrian Starkey 		goto port_fail;
36612ae57aaSWei Yongjun 	}
3673c31760eSBrian Starkey 	malidp->crtc.port = of_get_next_parent(ep);
368ad49f860SLiviu Dudau 
369ad49f860SLiviu Dudau 	ret = component_bind_all(dev, drm);
370ad49f860SLiviu Dudau 	if (ret) {
371ad49f860SLiviu Dudau 		DRM_ERROR("Failed to bind all components\n");
372ad49f860SLiviu Dudau 		goto bind_fail;
373ad49f860SLiviu Dudau 	}
374ad49f860SLiviu Dudau 
375ad49f860SLiviu Dudau 	ret = malidp_irq_init(pdev);
376ad49f860SLiviu Dudau 	if (ret < 0)
377ad49f860SLiviu Dudau 		goto irq_init_fail;
378ad49f860SLiviu Dudau 
379ad49f860SLiviu Dudau 	ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
380ad49f860SLiviu Dudau 	if (ret < 0) {
381ad49f860SLiviu Dudau 		DRM_ERROR("failed to initialise vblank\n");
382ad49f860SLiviu Dudau 		goto vblank_fail;
383ad49f860SLiviu Dudau 	}
384ad49f860SLiviu Dudau 
385ad49f860SLiviu Dudau 	drm_mode_config_reset(drm);
386ad49f860SLiviu Dudau 
387ad49f860SLiviu Dudau 	malidp->fbdev = drm_fbdev_cma_init(drm, 32, drm->mode_config.num_crtc,
388ad49f860SLiviu Dudau 					   drm->mode_config.num_connector);
389ad49f860SLiviu Dudau 
390ad49f860SLiviu Dudau 	if (IS_ERR(malidp->fbdev)) {
391ad49f860SLiviu Dudau 		ret = PTR_ERR(malidp->fbdev);
392ad49f860SLiviu Dudau 		malidp->fbdev = NULL;
393ad49f860SLiviu Dudau 		goto fbdev_fail;
394ad49f860SLiviu Dudau 	}
395ad49f860SLiviu Dudau 
396ad49f860SLiviu Dudau 	drm_kms_helper_poll_init(drm);
397ad49f860SLiviu Dudau 	return 0;
398ad49f860SLiviu Dudau 
399ad49f860SLiviu Dudau fbdev_fail:
400ad49f860SLiviu Dudau 	drm_vblank_cleanup(drm);
401ad49f860SLiviu Dudau vblank_fail:
402ad49f860SLiviu Dudau 	malidp_se_irq_fini(drm);
403ad49f860SLiviu Dudau 	malidp_de_irq_fini(drm);
404ad49f860SLiviu Dudau irq_init_fail:
405ad49f860SLiviu Dudau 	component_unbind_all(dev, drm);
406ad49f860SLiviu Dudau bind_fail:
4073c31760eSBrian Starkey 	of_node_put(malidp->crtc.port);
4083c31760eSBrian Starkey 	malidp->crtc.port = NULL;
4093c31760eSBrian Starkey port_fail:
410ad49f860SLiviu Dudau 	drm_dev_unregister(drm);
411ad49f860SLiviu Dudau register_fail:
412ad49f860SLiviu Dudau 	malidp_de_planes_destroy(drm);
413ad49f860SLiviu Dudau 	drm_mode_config_cleanup(drm);
414ad49f860SLiviu Dudau init_fail:
415ad49f860SLiviu Dudau 	drm->dev_private = NULL;
416ad49f860SLiviu Dudau 	dev_set_drvdata(dev, NULL);
417ad49f860SLiviu Dudau query_hw_fail:
418ad49f860SLiviu Dudau 	clk_disable_unprepare(hwdev->mclk);
419ad49f860SLiviu Dudau 	clk_disable_unprepare(hwdev->aclk);
420ad49f860SLiviu Dudau 	clk_disable_unprepare(hwdev->pclk);
421ad49f860SLiviu Dudau 	drm_dev_unref(drm);
422ad49f860SLiviu Dudau alloc_fail:
423ad49f860SLiviu Dudau 	of_reserved_mem_device_release(dev);
424ad49f860SLiviu Dudau 
425ad49f860SLiviu Dudau 	return ret;
426ad49f860SLiviu Dudau }
427ad49f860SLiviu Dudau 
428ad49f860SLiviu Dudau static void malidp_unbind(struct device *dev)
429ad49f860SLiviu Dudau {
430ad49f860SLiviu Dudau 	struct drm_device *drm = dev_get_drvdata(dev);
431ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
432ad49f860SLiviu Dudau 	struct malidp_hw_device *hwdev = malidp->dev;
433ad49f860SLiviu Dudau 
434ad49f860SLiviu Dudau 	if (malidp->fbdev) {
435ad49f860SLiviu Dudau 		drm_fbdev_cma_fini(malidp->fbdev);
436ad49f860SLiviu Dudau 		malidp->fbdev = NULL;
437ad49f860SLiviu Dudau 	}
438ad49f860SLiviu Dudau 	drm_kms_helper_poll_fini(drm);
439ad49f860SLiviu Dudau 	malidp_se_irq_fini(drm);
440ad49f860SLiviu Dudau 	malidp_de_irq_fini(drm);
441ad49f860SLiviu Dudau 	drm_vblank_cleanup(drm);
442ad49f860SLiviu Dudau 	component_unbind_all(dev, drm);
4433c31760eSBrian Starkey 	of_node_put(malidp->crtc.port);
4443c31760eSBrian Starkey 	malidp->crtc.port = NULL;
445ad49f860SLiviu Dudau 	drm_dev_unregister(drm);
446ad49f860SLiviu Dudau 	malidp_de_planes_destroy(drm);
447ad49f860SLiviu Dudau 	drm_mode_config_cleanup(drm);
448ad49f860SLiviu Dudau 	drm->dev_private = NULL;
449ad49f860SLiviu Dudau 	dev_set_drvdata(dev, NULL);
450ad49f860SLiviu Dudau 	clk_disable_unprepare(hwdev->mclk);
451ad49f860SLiviu Dudau 	clk_disable_unprepare(hwdev->aclk);
452ad49f860SLiviu Dudau 	clk_disable_unprepare(hwdev->pclk);
453ad49f860SLiviu Dudau 	drm_dev_unref(drm);
454ad49f860SLiviu Dudau 	of_reserved_mem_device_release(dev);
455ad49f860SLiviu Dudau }
456ad49f860SLiviu Dudau 
457ad49f860SLiviu Dudau static const struct component_master_ops malidp_master_ops = {
458ad49f860SLiviu Dudau 	.bind = malidp_bind,
459ad49f860SLiviu Dudau 	.unbind = malidp_unbind,
460ad49f860SLiviu Dudau };
461ad49f860SLiviu Dudau 
462ad49f860SLiviu Dudau static int malidp_compare_dev(struct device *dev, void *data)
463ad49f860SLiviu Dudau {
464ad49f860SLiviu Dudau 	struct device_node *np = data;
465ad49f860SLiviu Dudau 
466ad49f860SLiviu Dudau 	return dev->of_node == np;
467ad49f860SLiviu Dudau }
468ad49f860SLiviu Dudau 
469ad49f860SLiviu Dudau static int malidp_platform_probe(struct platform_device *pdev)
470ad49f860SLiviu Dudau {
471ad49f860SLiviu Dudau 	struct device_node *port, *ep;
472ad49f860SLiviu Dudau 	struct component_match *match = NULL;
473ad49f860SLiviu Dudau 
474ad49f860SLiviu Dudau 	if (!pdev->dev.of_node)
475ad49f860SLiviu Dudau 		return -ENODEV;
476ad49f860SLiviu Dudau 
477ad49f860SLiviu Dudau 	/* there is only one output port inside each device, find it */
478ad49f860SLiviu Dudau 	ep = of_graph_get_next_endpoint(pdev->dev.of_node, NULL);
479ad49f860SLiviu Dudau 	if (!ep)
480ad49f860SLiviu Dudau 		return -ENODEV;
481ad49f860SLiviu Dudau 
482ad49f860SLiviu Dudau 	if (!of_device_is_available(ep)) {
483ad49f860SLiviu Dudau 		of_node_put(ep);
484ad49f860SLiviu Dudau 		return -ENODEV;
485ad49f860SLiviu Dudau 	}
486ad49f860SLiviu Dudau 
487ad49f860SLiviu Dudau 	/* add the remote encoder port as component */
488ad49f860SLiviu Dudau 	port = of_graph_get_remote_port_parent(ep);
489ad49f860SLiviu Dudau 	of_node_put(ep);
490ad49f860SLiviu Dudau 	if (!port || !of_device_is_available(port)) {
491ad49f860SLiviu Dudau 		of_node_put(port);
492ad49f860SLiviu Dudau 		return -EAGAIN;
493ad49f860SLiviu Dudau 	}
494ad49f860SLiviu Dudau 
495ad49f860SLiviu Dudau 	component_match_add(&pdev->dev, &match, malidp_compare_dev, port);
496ad49f860SLiviu Dudau 	return component_master_add_with_match(&pdev->dev, &malidp_master_ops,
497ad49f860SLiviu Dudau 					       match);
498ad49f860SLiviu Dudau }
499ad49f860SLiviu Dudau 
500ad49f860SLiviu Dudau static int malidp_platform_remove(struct platform_device *pdev)
501ad49f860SLiviu Dudau {
502ad49f860SLiviu Dudau 	component_master_del(&pdev->dev, &malidp_master_ops);
503ad49f860SLiviu Dudau 	return 0;
504ad49f860SLiviu Dudau }
505ad49f860SLiviu Dudau 
506ad49f860SLiviu Dudau static struct platform_driver malidp_platform_driver = {
507ad49f860SLiviu Dudau 	.probe		= malidp_platform_probe,
508ad49f860SLiviu Dudau 	.remove		= malidp_platform_remove,
509ad49f860SLiviu Dudau 	.driver	= {
510ad49f860SLiviu Dudau 		.name = "mali-dp",
511ad49f860SLiviu Dudau 		.of_match_table	= malidp_drm_of_match,
512ad49f860SLiviu Dudau 	},
513ad49f860SLiviu Dudau };
514ad49f860SLiviu Dudau 
515ad49f860SLiviu Dudau module_platform_driver(malidp_platform_driver);
516ad49f860SLiviu Dudau 
517ad49f860SLiviu Dudau MODULE_AUTHOR("Liviu Dudau <Liviu.Dudau@arm.com>");
518ad49f860SLiviu Dudau MODULE_DESCRIPTION("ARM Mali DP DRM driver");
519ad49f860SLiviu Dudau MODULE_LICENSE("GPL v2");
520