• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Freescale Semiconductor, Inc.
3  *
4  * Freescale DCU drm device driver
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/clk-provider.h>
14 #include <linux/console.h>
15 #include <linux/io.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/mm.h>
18 #include <linux/module.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regmap.h>
24 
25 #include <drm/drmP.h>
26 #include <drm/drm_atomic_helper.h>
27 #include <drm/drm_crtc_helper.h>
28 #include <drm/drm_fb_cma_helper.h>
29 #include <drm/drm_gem_cma_helper.h>
30 
31 #include "fsl_dcu_drm_crtc.h"
32 #include "fsl_dcu_drm_drv.h"
33 #include "fsl_tcon.h"
34 
fsl_dcu_drm_is_volatile_reg(struct device * dev,unsigned int reg)35 static bool fsl_dcu_drm_is_volatile_reg(struct device *dev, unsigned int reg)
36 {
37 	if (reg == DCU_INT_STATUS || reg == DCU_UPDATE_MODE)
38 		return true;
39 
40 	return false;
41 }
42 
43 static const struct regmap_config fsl_dcu_regmap_config = {
44 	.reg_bits = 32,
45 	.reg_stride = 4,
46 	.val_bits = 32,
47 
48 	.volatile_reg = fsl_dcu_drm_is_volatile_reg,
49 };
50 
fsl_dcu_drm_irq_init(struct drm_device * dev)51 static int fsl_dcu_drm_irq_init(struct drm_device *dev)
52 {
53 	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
54 	int ret;
55 
56 	ret = drm_irq_install(dev, fsl_dev->irq);
57 	if (ret < 0)
58 		dev_err(dev->dev, "failed to install IRQ handler\n");
59 
60 	regmap_write(fsl_dev->regmap, DCU_INT_STATUS, 0);
61 	regmap_write(fsl_dev->regmap, DCU_INT_MASK, ~0);
62 
63 	return ret;
64 }
65 
fsl_dcu_load(struct drm_device * dev,unsigned long flags)66 static int fsl_dcu_load(struct drm_device *dev, unsigned long flags)
67 {
68 	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
69 	int ret;
70 
71 	ret = fsl_dcu_drm_modeset_init(fsl_dev);
72 	if (ret < 0) {
73 		dev_err(dev->dev, "failed to initialize mode setting\n");
74 		return ret;
75 	}
76 
77 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
78 	if (ret < 0) {
79 		dev_err(dev->dev, "failed to initialize vblank\n");
80 		goto done;
81 	}
82 
83 	ret = fsl_dcu_drm_irq_init(dev);
84 	if (ret < 0)
85 		goto done;
86 	dev->irq_enabled = true;
87 
88 	fsl_dcu_fbdev_init(dev);
89 
90 	return 0;
91 done:
92 	drm_kms_helper_poll_fini(dev);
93 
94 	if (fsl_dev->fbdev)
95 		drm_fbdev_cma_fini(fsl_dev->fbdev);
96 
97 	drm_mode_config_cleanup(dev);
98 	drm_vblank_cleanup(dev);
99 	drm_irq_uninstall(dev);
100 	dev->dev_private = NULL;
101 
102 	return ret;
103 }
104 
fsl_dcu_unload(struct drm_device * dev)105 static int fsl_dcu_unload(struct drm_device *dev)
106 {
107 	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
108 
109 	drm_kms_helper_poll_fini(dev);
110 
111 	if (fsl_dev->fbdev)
112 		drm_fbdev_cma_fini(fsl_dev->fbdev);
113 
114 	drm_mode_config_cleanup(dev);
115 	drm_vblank_cleanup(dev);
116 	drm_irq_uninstall(dev);
117 
118 	dev->dev_private = NULL;
119 
120 	return 0;
121 }
122 
fsl_dcu_drm_irq(int irq,void * arg)123 static irqreturn_t fsl_dcu_drm_irq(int irq, void *arg)
124 {
125 	struct drm_device *dev = arg;
126 	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
127 	unsigned int int_status;
128 	int ret;
129 
130 	ret = regmap_read(fsl_dev->regmap, DCU_INT_STATUS, &int_status);
131 	if (ret) {
132 		dev_err(dev->dev, "read DCU_INT_STATUS failed\n");
133 		return IRQ_NONE;
134 	}
135 
136 	if (int_status & DCU_INT_STATUS_VBLANK)
137 		drm_handle_vblank(dev, 0);
138 
139 	regmap_write(fsl_dev->regmap, DCU_INT_STATUS, int_status);
140 
141 	return IRQ_HANDLED;
142 }
143 
fsl_dcu_drm_enable_vblank(struct drm_device * dev,unsigned int pipe)144 static int fsl_dcu_drm_enable_vblank(struct drm_device *dev, unsigned int pipe)
145 {
146 	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
147 	unsigned int value;
148 
149 	regmap_read(fsl_dev->regmap, DCU_INT_MASK, &value);
150 	value &= ~DCU_INT_MASK_VBLANK;
151 	regmap_write(fsl_dev->regmap, DCU_INT_MASK, value);
152 
153 	return 0;
154 }
155 
fsl_dcu_drm_disable_vblank(struct drm_device * dev,unsigned int pipe)156 static void fsl_dcu_drm_disable_vblank(struct drm_device *dev,
157 				       unsigned int pipe)
158 {
159 	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
160 	unsigned int value;
161 
162 	regmap_read(fsl_dev->regmap, DCU_INT_MASK, &value);
163 	value |= DCU_INT_MASK_VBLANK;
164 	regmap_write(fsl_dev->regmap, DCU_INT_MASK, value);
165 }
166 
fsl_dcu_drm_lastclose(struct drm_device * dev)167 static void fsl_dcu_drm_lastclose(struct drm_device *dev)
168 {
169 	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
170 
171 	drm_fbdev_cma_restore_mode(fsl_dev->fbdev);
172 }
173 
174 static const struct file_operations fsl_dcu_drm_fops = {
175 	.owner		= THIS_MODULE,
176 	.open		= drm_open,
177 	.release	= drm_release,
178 	.unlocked_ioctl	= drm_ioctl,
179 #ifdef CONFIG_COMPAT
180 	.compat_ioctl	= drm_compat_ioctl,
181 #endif
182 	.poll		= drm_poll,
183 	.read		= drm_read,
184 	.llseek		= no_llseek,
185 	.mmap		= drm_gem_cma_mmap,
186 };
187 
188 static struct drm_driver fsl_dcu_drm_driver = {
189 	.driver_features	= DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET
190 				| DRIVER_PRIME | DRIVER_ATOMIC,
191 	.lastclose		= fsl_dcu_drm_lastclose,
192 	.load			= fsl_dcu_load,
193 	.unload			= fsl_dcu_unload,
194 	.irq_handler		= fsl_dcu_drm_irq,
195 	.get_vblank_counter	= drm_vblank_no_hw_counter,
196 	.enable_vblank		= fsl_dcu_drm_enable_vblank,
197 	.disable_vblank		= fsl_dcu_drm_disable_vblank,
198 	.gem_free_object_unlocked = drm_gem_cma_free_object,
199 	.gem_vm_ops		= &drm_gem_cma_vm_ops,
200 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
201 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
202 	.gem_prime_import	= drm_gem_prime_import,
203 	.gem_prime_export	= drm_gem_prime_export,
204 	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
205 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
206 	.gem_prime_vmap		= drm_gem_cma_prime_vmap,
207 	.gem_prime_vunmap	= drm_gem_cma_prime_vunmap,
208 	.gem_prime_mmap		= drm_gem_cma_prime_mmap,
209 	.dumb_create		= drm_gem_cma_dumb_create,
210 	.dumb_map_offset	= drm_gem_cma_dumb_map_offset,
211 	.dumb_destroy		= drm_gem_dumb_destroy,
212 	.fops			= &fsl_dcu_drm_fops,
213 	.name			= "fsl-dcu-drm",
214 	.desc			= "Freescale DCU DRM",
215 	.date			= "20160425",
216 	.major			= 1,
217 	.minor			= 1,
218 };
219 
220 #ifdef CONFIG_PM_SLEEP
fsl_dcu_drm_pm_suspend(struct device * dev)221 static int fsl_dcu_drm_pm_suspend(struct device *dev)
222 {
223 	struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev);
224 
225 	if (!fsl_dev)
226 		return 0;
227 
228 	disable_irq(fsl_dev->irq);
229 	drm_kms_helper_poll_disable(fsl_dev->drm);
230 
231 	console_lock();
232 	drm_fbdev_cma_set_suspend(fsl_dev->fbdev, 1);
233 	console_unlock();
234 
235 	fsl_dev->state = drm_atomic_helper_suspend(fsl_dev->drm);
236 	if (IS_ERR(fsl_dev->state)) {
237 		console_lock();
238 		drm_fbdev_cma_set_suspend(fsl_dev->fbdev, 0);
239 		console_unlock();
240 
241 		drm_kms_helper_poll_enable(fsl_dev->drm);
242 		enable_irq(fsl_dev->irq);
243 		return PTR_ERR(fsl_dev->state);
244 	}
245 
246 	clk_disable_unprepare(fsl_dev->clk);
247 
248 	return 0;
249 }
250 
fsl_dcu_drm_pm_resume(struct device * dev)251 static int fsl_dcu_drm_pm_resume(struct device *dev)
252 {
253 	struct fsl_dcu_drm_device *fsl_dev = dev_get_drvdata(dev);
254 	int ret;
255 
256 	if (!fsl_dev)
257 		return 0;
258 
259 	ret = clk_prepare_enable(fsl_dev->clk);
260 	if (ret < 0) {
261 		dev_err(dev, "failed to enable dcu clk\n");
262 		return ret;
263 	}
264 
265 	if (fsl_dev->tcon)
266 		fsl_tcon_bypass_enable(fsl_dev->tcon);
267 	fsl_dcu_drm_init_planes(fsl_dev->drm);
268 	enable_irq(fsl_dev->irq);
269 	drm_atomic_helper_resume(fsl_dev->drm, fsl_dev->state);
270 
271 	console_lock();
272 	drm_fbdev_cma_set_suspend(fsl_dev->fbdev, 0);
273 	console_unlock();
274 
275 	drm_kms_helper_poll_enable(fsl_dev->drm);
276 
277 	return 0;
278 }
279 #endif
280 
281 static const struct dev_pm_ops fsl_dcu_drm_pm_ops = {
282 	SET_SYSTEM_SLEEP_PM_OPS(fsl_dcu_drm_pm_suspend, fsl_dcu_drm_pm_resume)
283 };
284 
285 static const struct fsl_dcu_soc_data fsl_dcu_ls1021a_data = {
286 	.name = "ls1021a",
287 	.total_layer = 16,
288 	.max_layer = 4,
289 	.layer_regs = LS1021A_LAYER_REG_NUM,
290 };
291 
292 static const struct fsl_dcu_soc_data fsl_dcu_vf610_data = {
293 	.name = "vf610",
294 	.total_layer = 64,
295 	.max_layer = 6,
296 	.layer_regs = VF610_LAYER_REG_NUM,
297 };
298 
299 static const struct of_device_id fsl_dcu_of_match[] = {
300 	{
301 		.compatible = "fsl,ls1021a-dcu",
302 		.data = &fsl_dcu_ls1021a_data,
303 	}, {
304 		.compatible = "fsl,vf610-dcu",
305 		.data = &fsl_dcu_vf610_data,
306 	}, {
307 	},
308 };
309 MODULE_DEVICE_TABLE(of, fsl_dcu_of_match);
310 
fsl_dcu_drm_probe(struct platform_device * pdev)311 static int fsl_dcu_drm_probe(struct platform_device *pdev)
312 {
313 	struct fsl_dcu_drm_device *fsl_dev;
314 	struct drm_device *drm;
315 	struct device *dev = &pdev->dev;
316 	struct resource *res;
317 	void __iomem *base;
318 	struct drm_driver *driver = &fsl_dcu_drm_driver;
319 	struct clk *pix_clk_in;
320 	char pix_clk_name[32];
321 	const char *pix_clk_in_name;
322 	const struct of_device_id *id;
323 	int ret;
324 	u8 div_ratio_shift = 0;
325 
326 	fsl_dev = devm_kzalloc(dev, sizeof(*fsl_dev), GFP_KERNEL);
327 	if (!fsl_dev)
328 		return -ENOMEM;
329 
330 	id = of_match_node(fsl_dcu_of_match, pdev->dev.of_node);
331 	if (!id)
332 		return -ENODEV;
333 	fsl_dev->soc = id->data;
334 
335 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
336 	if (!res) {
337 		dev_err(dev, "could not get memory IO resource\n");
338 		return -ENODEV;
339 	}
340 
341 	base = devm_ioremap_resource(dev, res);
342 	if (IS_ERR(base)) {
343 		ret = PTR_ERR(base);
344 		return ret;
345 	}
346 
347 	fsl_dev->irq = platform_get_irq(pdev, 0);
348 	if (fsl_dev->irq < 0) {
349 		dev_err(dev, "failed to get irq\n");
350 		return -ENXIO;
351 	}
352 
353 	fsl_dev->regmap = devm_regmap_init_mmio(dev, base,
354 			&fsl_dcu_regmap_config);
355 	if (IS_ERR(fsl_dev->regmap)) {
356 		dev_err(dev, "regmap init failed\n");
357 		return PTR_ERR(fsl_dev->regmap);
358 	}
359 
360 	fsl_dev->clk = devm_clk_get(dev, "dcu");
361 	if (IS_ERR(fsl_dev->clk)) {
362 		dev_err(dev, "failed to get dcu clock\n");
363 		return PTR_ERR(fsl_dev->clk);
364 	}
365 	ret = clk_prepare_enable(fsl_dev->clk);
366 	if (ret < 0) {
367 		dev_err(dev, "failed to enable dcu clk\n");
368 		return ret;
369 	}
370 
371 	pix_clk_in = devm_clk_get(dev, "pix");
372 	if (IS_ERR(pix_clk_in)) {
373 		/* legancy binding, use dcu clock as pixel clock input */
374 		pix_clk_in = fsl_dev->clk;
375 	}
376 
377 	if (of_property_read_bool(dev->of_node, "big-endian"))
378 		div_ratio_shift = 24;
379 
380 	pix_clk_in_name = __clk_get_name(pix_clk_in);
381 	snprintf(pix_clk_name, sizeof(pix_clk_name), "%s_pix", pix_clk_in_name);
382 	fsl_dev->pix_clk = clk_register_divider(dev, pix_clk_name,
383 			pix_clk_in_name, 0, base + DCU_DIV_RATIO,
384 			div_ratio_shift, 8, CLK_DIVIDER_ROUND_CLOSEST, NULL);
385 	if (IS_ERR(fsl_dev->pix_clk)) {
386 		dev_err(dev, "failed to register pix clk\n");
387 		ret = PTR_ERR(fsl_dev->pix_clk);
388 		goto disable_clk;
389 	}
390 
391 	fsl_dev->tcon = fsl_tcon_init(dev);
392 
393 	drm = drm_dev_alloc(driver, dev);
394 	if (IS_ERR(drm)) {
395 		ret = PTR_ERR(drm);
396 		goto unregister_pix_clk;
397 	}
398 
399 	fsl_dev->dev = dev;
400 	fsl_dev->drm = drm;
401 	fsl_dev->np = dev->of_node;
402 	drm->dev_private = fsl_dev;
403 	dev_set_drvdata(dev, fsl_dev);
404 
405 	ret = drm_dev_register(drm, 0);
406 	if (ret < 0)
407 		goto unref;
408 
409 	DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n", driver->name,
410 		 driver->major, driver->minor, driver->patchlevel,
411 		 driver->date, drm->primary->index);
412 
413 	return 0;
414 
415 unref:
416 	drm_dev_unref(drm);
417 unregister_pix_clk:
418 	clk_unregister(fsl_dev->pix_clk);
419 disable_clk:
420 	clk_disable_unprepare(fsl_dev->clk);
421 	return ret;
422 }
423 
fsl_dcu_drm_remove(struct platform_device * pdev)424 static int fsl_dcu_drm_remove(struct platform_device *pdev)
425 {
426 	struct fsl_dcu_drm_device *fsl_dev = platform_get_drvdata(pdev);
427 
428 	clk_disable_unprepare(fsl_dev->clk);
429 	clk_unregister(fsl_dev->pix_clk);
430 	drm_put_dev(fsl_dev->drm);
431 
432 	return 0;
433 }
434 
435 static struct platform_driver fsl_dcu_drm_platform_driver = {
436 	.probe		= fsl_dcu_drm_probe,
437 	.remove		= fsl_dcu_drm_remove,
438 	.driver		= {
439 		.name	= "fsl-dcu",
440 		.pm	= &fsl_dcu_drm_pm_ops,
441 		.of_match_table = fsl_dcu_of_match,
442 	},
443 };
444 
445 module_platform_driver(fsl_dcu_drm_platform_driver);
446 
447 MODULE_DESCRIPTION("Freescale DCU DRM Driver");
448 MODULE_LICENSE("GPL");
449