drm/amdkfd: Add memory exception handling
[linux/fpc-iii.git] / drivers / gpu / drm / tilcdc / tilcdc_drv.c
blob095fca91525c78a5586c9a575f7c6216a25d2694
1 /*
2 * Copyright (C) 2012 Texas Instruments
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 /* LCDC DRM driver, based on da8xx-fb */
20 #include "tilcdc_drv.h"
21 #include "tilcdc_regs.h"
22 #include "tilcdc_tfp410.h"
23 #include "tilcdc_slave.h"
24 #include "tilcdc_panel.h"
26 #include "drm_fb_helper.h"
28 static LIST_HEAD(module_list);
29 static bool slave_probing;
31 void tilcdc_module_init(struct tilcdc_module *mod, const char *name,
32 const struct tilcdc_module_ops *funcs)
34 mod->name = name;
35 mod->funcs = funcs;
36 INIT_LIST_HEAD(&mod->list);
37 list_add(&mod->list, &module_list);
40 void tilcdc_module_cleanup(struct tilcdc_module *mod)
42 list_del(&mod->list);
45 void tilcdc_slave_probedefer(bool defered)
47 slave_probing = defered;
50 static struct of_device_id tilcdc_of_match[];
52 static struct drm_framebuffer *tilcdc_fb_create(struct drm_device *dev,
53 struct drm_file *file_priv, struct drm_mode_fb_cmd2 *mode_cmd)
55 return drm_fb_cma_create(dev, file_priv, mode_cmd);
58 static void tilcdc_fb_output_poll_changed(struct drm_device *dev)
60 struct tilcdc_drm_private *priv = dev->dev_private;
61 drm_fbdev_cma_hotplug_event(priv->fbdev);
64 static const struct drm_mode_config_funcs mode_config_funcs = {
65 .fb_create = tilcdc_fb_create,
66 .output_poll_changed = tilcdc_fb_output_poll_changed,
69 static int modeset_init(struct drm_device *dev)
71 struct tilcdc_drm_private *priv = dev->dev_private;
72 struct tilcdc_module *mod;
74 drm_mode_config_init(dev);
76 priv->crtc = tilcdc_crtc_create(dev);
78 list_for_each_entry(mod, &module_list, list) {
79 DBG("loading module: %s", mod->name);
80 mod->funcs->modeset_init(mod, dev);
83 if ((priv->num_encoders == 0) || (priv->num_connectors == 0)) {
84 /* oh nos! */
85 dev_err(dev->dev, "no encoders/connectors found\n");
86 drm_mode_config_cleanup(dev);
87 return -ENXIO;
90 dev->mode_config.min_width = 0;
91 dev->mode_config.min_height = 0;
92 dev->mode_config.max_width = tilcdc_crtc_max_width(priv->crtc);
93 dev->mode_config.max_height = 2048;
94 dev->mode_config.funcs = &mode_config_funcs;
96 return 0;
99 #ifdef CONFIG_CPU_FREQ
100 static int cpufreq_transition(struct notifier_block *nb,
101 unsigned long val, void *data)
103 struct tilcdc_drm_private *priv = container_of(nb,
104 struct tilcdc_drm_private, freq_transition);
105 if (val == CPUFREQ_POSTCHANGE) {
106 if (priv->lcd_fck_rate != clk_get_rate(priv->clk)) {
107 priv->lcd_fck_rate = clk_get_rate(priv->clk);
108 tilcdc_crtc_update_clk(priv->crtc);
112 return 0;
114 #endif
117 * DRM operations:
120 static int tilcdc_unload(struct drm_device *dev)
122 struct tilcdc_drm_private *priv = dev->dev_private;
124 drm_fbdev_cma_fini(priv->fbdev);
125 drm_kms_helper_poll_fini(dev);
126 drm_mode_config_cleanup(dev);
127 drm_vblank_cleanup(dev);
129 pm_runtime_get_sync(dev->dev);
130 drm_irq_uninstall(dev);
131 pm_runtime_put_sync(dev->dev);
133 #ifdef CONFIG_CPU_FREQ
134 cpufreq_unregister_notifier(&priv->freq_transition,
135 CPUFREQ_TRANSITION_NOTIFIER);
136 #endif
138 if (priv->clk)
139 clk_put(priv->clk);
141 if (priv->mmio)
142 iounmap(priv->mmio);
144 flush_workqueue(priv->wq);
145 destroy_workqueue(priv->wq);
147 dev->dev_private = NULL;
149 pm_runtime_disable(dev->dev);
151 kfree(priv);
153 return 0;
156 static int tilcdc_load(struct drm_device *dev, unsigned long flags)
158 struct platform_device *pdev = dev->platformdev;
159 struct device_node *node = pdev->dev.of_node;
160 struct tilcdc_drm_private *priv;
161 struct tilcdc_module *mod;
162 struct resource *res;
163 u32 bpp = 0;
164 int ret;
166 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
167 if (!priv) {
168 dev_err(dev->dev, "failed to allocate private data\n");
169 return -ENOMEM;
172 dev->dev_private = priv;
174 priv->wq = alloc_ordered_workqueue("tilcdc", 0);
175 if (!priv->wq) {
176 ret = -ENOMEM;
177 goto fail_free_priv;
180 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
181 if (!res) {
182 dev_err(dev->dev, "failed to get memory resource\n");
183 ret = -EINVAL;
184 goto fail_free_wq;
187 priv->mmio = ioremap_nocache(res->start, resource_size(res));
188 if (!priv->mmio) {
189 dev_err(dev->dev, "failed to ioremap\n");
190 ret = -ENOMEM;
191 goto fail_free_wq;
194 priv->clk = clk_get(dev->dev, "fck");
195 if (IS_ERR(priv->clk)) {
196 dev_err(dev->dev, "failed to get functional clock\n");
197 ret = -ENODEV;
198 goto fail_iounmap;
201 priv->disp_clk = clk_get(dev->dev, "dpll_disp_ck");
202 if (IS_ERR(priv->clk)) {
203 dev_err(dev->dev, "failed to get display clock\n");
204 ret = -ENODEV;
205 goto fail_put_clk;
208 #ifdef CONFIG_CPU_FREQ
209 priv->lcd_fck_rate = clk_get_rate(priv->clk);
210 priv->freq_transition.notifier_call = cpufreq_transition;
211 ret = cpufreq_register_notifier(&priv->freq_transition,
212 CPUFREQ_TRANSITION_NOTIFIER);
213 if (ret) {
214 dev_err(dev->dev, "failed to register cpufreq notifier\n");
215 goto fail_put_disp_clk;
217 #endif
219 if (of_property_read_u32(node, "max-bandwidth", &priv->max_bandwidth))
220 priv->max_bandwidth = TILCDC_DEFAULT_MAX_BANDWIDTH;
222 DBG("Maximum Bandwidth Value %d", priv->max_bandwidth);
224 if (of_property_read_u32(node, "ti,max-width", &priv->max_width))
225 priv->max_width = TILCDC_DEFAULT_MAX_WIDTH;
227 DBG("Maximum Horizontal Pixel Width Value %dpixels", priv->max_width);
229 if (of_property_read_u32(node, "ti,max-pixelclock",
230 &priv->max_pixelclock))
231 priv->max_pixelclock = TILCDC_DEFAULT_MAX_PIXELCLOCK;
233 DBG("Maximum Pixel Clock Value %dKHz", priv->max_pixelclock);
235 pm_runtime_enable(dev->dev);
237 /* Determine LCD IP Version */
238 pm_runtime_get_sync(dev->dev);
239 switch (tilcdc_read(dev, LCDC_PID_REG)) {
240 case 0x4c100102:
241 priv->rev = 1;
242 break;
243 case 0x4f200800:
244 case 0x4f201000:
245 priv->rev = 2;
246 break;
247 default:
248 dev_warn(dev->dev, "Unknown PID Reg value 0x%08x, "
249 "defaulting to LCD revision 1\n",
250 tilcdc_read(dev, LCDC_PID_REG));
251 priv->rev = 1;
252 break;
255 pm_runtime_put_sync(dev->dev);
257 ret = modeset_init(dev);
258 if (ret < 0) {
259 dev_err(dev->dev, "failed to initialize mode setting\n");
260 goto fail_cpufreq_unregister;
263 ret = drm_vblank_init(dev, 1);
264 if (ret < 0) {
265 dev_err(dev->dev, "failed to initialize vblank\n");
266 goto fail_mode_config_cleanup;
269 pm_runtime_get_sync(dev->dev);
270 ret = drm_irq_install(dev, platform_get_irq(dev->platformdev, 0));
271 pm_runtime_put_sync(dev->dev);
272 if (ret < 0) {
273 dev_err(dev->dev, "failed to install IRQ handler\n");
274 goto fail_vblank_cleanup;
277 platform_set_drvdata(pdev, dev);
280 list_for_each_entry(mod, &module_list, list) {
281 DBG("%s: preferred_bpp: %d", mod->name, mod->preferred_bpp);
282 bpp = mod->preferred_bpp;
283 if (bpp > 0)
284 break;
287 priv->fbdev = drm_fbdev_cma_init(dev, bpp,
288 dev->mode_config.num_crtc,
289 dev->mode_config.num_connector);
290 if (IS_ERR(priv->fbdev)) {
291 ret = PTR_ERR(priv->fbdev);
292 goto fail_irq_uninstall;
295 drm_kms_helper_poll_init(dev);
297 return 0;
299 fail_irq_uninstall:
300 pm_runtime_get_sync(dev->dev);
301 drm_irq_uninstall(dev);
302 pm_runtime_put_sync(dev->dev);
304 fail_vblank_cleanup:
305 drm_vblank_cleanup(dev);
307 fail_mode_config_cleanup:
308 drm_mode_config_cleanup(dev);
310 fail_cpufreq_unregister:
311 pm_runtime_disable(dev->dev);
312 #ifdef CONFIG_CPU_FREQ
313 cpufreq_unregister_notifier(&priv->freq_transition,
314 CPUFREQ_TRANSITION_NOTIFIER);
315 fail_put_disp_clk:
316 clk_put(priv->disp_clk);
317 #endif
319 fail_put_clk:
320 clk_put(priv->clk);
322 fail_iounmap:
323 iounmap(priv->mmio);
325 fail_free_wq:
326 flush_workqueue(priv->wq);
327 destroy_workqueue(priv->wq);
329 fail_free_priv:
330 dev->dev_private = NULL;
331 kfree(priv);
332 return ret;
335 static void tilcdc_preclose(struct drm_device *dev, struct drm_file *file)
337 struct tilcdc_drm_private *priv = dev->dev_private;
339 tilcdc_crtc_cancel_page_flip(priv->crtc, file);
342 static void tilcdc_lastclose(struct drm_device *dev)
344 struct tilcdc_drm_private *priv = dev->dev_private;
345 drm_fbdev_cma_restore_mode(priv->fbdev);
348 static irqreturn_t tilcdc_irq(int irq, void *arg)
350 struct drm_device *dev = arg;
351 struct tilcdc_drm_private *priv = dev->dev_private;
352 return tilcdc_crtc_irq(priv->crtc);
355 static void tilcdc_irq_preinstall(struct drm_device *dev)
357 tilcdc_clear_irqstatus(dev, 0xffffffff);
360 static int tilcdc_irq_postinstall(struct drm_device *dev)
362 struct tilcdc_drm_private *priv = dev->dev_private;
364 /* enable FIFO underflow irq: */
365 if (priv->rev == 1)
366 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_UNDERFLOW_INT_ENA);
367 else
368 tilcdc_set(dev, LCDC_INT_ENABLE_SET_REG, LCDC_V2_UNDERFLOW_INT_ENA);
370 return 0;
373 static void tilcdc_irq_uninstall(struct drm_device *dev)
375 struct tilcdc_drm_private *priv = dev->dev_private;
377 /* disable irqs that we might have enabled: */
378 if (priv->rev == 1) {
379 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
380 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
381 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_V1_END_OF_FRAME_INT_ENA);
382 } else {
383 tilcdc_clear(dev, LCDC_INT_ENABLE_SET_REG,
384 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
385 LCDC_V2_END_OF_FRAME0_INT_ENA | LCDC_V2_END_OF_FRAME1_INT_ENA |
386 LCDC_FRAME_DONE);
391 static void enable_vblank(struct drm_device *dev, bool enable)
393 struct tilcdc_drm_private *priv = dev->dev_private;
394 u32 reg, mask;
396 if (priv->rev == 1) {
397 reg = LCDC_DMA_CTRL_REG;
398 mask = LCDC_V1_END_OF_FRAME_INT_ENA;
399 } else {
400 reg = LCDC_INT_ENABLE_SET_REG;
401 mask = LCDC_V2_END_OF_FRAME0_INT_ENA |
402 LCDC_V2_END_OF_FRAME1_INT_ENA | LCDC_FRAME_DONE;
405 if (enable)
406 tilcdc_set(dev, reg, mask);
407 else
408 tilcdc_clear(dev, reg, mask);
411 static int tilcdc_enable_vblank(struct drm_device *dev, int crtc)
413 enable_vblank(dev, true);
414 return 0;
417 static void tilcdc_disable_vblank(struct drm_device *dev, int crtc)
419 enable_vblank(dev, false);
422 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_PM_SLEEP)
423 static const struct {
424 const char *name;
425 uint8_t rev;
426 uint8_t save;
427 uint32_t reg;
428 } registers[] = {
429 #define REG(rev, save, reg) { #reg, rev, save, reg }
430 /* exists in revision 1: */
431 REG(1, false, LCDC_PID_REG),
432 REG(1, true, LCDC_CTRL_REG),
433 REG(1, false, LCDC_STAT_REG),
434 REG(1, true, LCDC_RASTER_CTRL_REG),
435 REG(1, true, LCDC_RASTER_TIMING_0_REG),
436 REG(1, true, LCDC_RASTER_TIMING_1_REG),
437 REG(1, true, LCDC_RASTER_TIMING_2_REG),
438 REG(1, true, LCDC_DMA_CTRL_REG),
439 REG(1, true, LCDC_DMA_FB_BASE_ADDR_0_REG),
440 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_0_REG),
441 REG(1, true, LCDC_DMA_FB_BASE_ADDR_1_REG),
442 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_1_REG),
443 /* new in revision 2: */
444 REG(2, false, LCDC_RAW_STAT_REG),
445 REG(2, false, LCDC_MASKED_STAT_REG),
446 REG(2, false, LCDC_INT_ENABLE_SET_REG),
447 REG(2, false, LCDC_INT_ENABLE_CLR_REG),
448 REG(2, false, LCDC_END_OF_INT_IND_REG),
449 REG(2, true, LCDC_CLK_ENABLE_REG),
450 REG(2, true, LCDC_INT_ENABLE_SET_REG),
451 #undef REG
453 #endif
455 #ifdef CONFIG_DEBUG_FS
456 static int tilcdc_regs_show(struct seq_file *m, void *arg)
458 struct drm_info_node *node = (struct drm_info_node *) m->private;
459 struct drm_device *dev = node->minor->dev;
460 struct tilcdc_drm_private *priv = dev->dev_private;
461 unsigned i;
463 pm_runtime_get_sync(dev->dev);
465 seq_printf(m, "revision: %d\n", priv->rev);
467 for (i = 0; i < ARRAY_SIZE(registers); i++)
468 if (priv->rev >= registers[i].rev)
469 seq_printf(m, "%s:\t %08x\n", registers[i].name,
470 tilcdc_read(dev, registers[i].reg));
472 pm_runtime_put_sync(dev->dev);
474 return 0;
477 static int tilcdc_mm_show(struct seq_file *m, void *arg)
479 struct drm_info_node *node = (struct drm_info_node *) m->private;
480 struct drm_device *dev = node->minor->dev;
481 return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm);
484 static struct drm_info_list tilcdc_debugfs_list[] = {
485 { "regs", tilcdc_regs_show, 0 },
486 { "mm", tilcdc_mm_show, 0 },
487 { "fb", drm_fb_cma_debugfs_show, 0 },
490 static int tilcdc_debugfs_init(struct drm_minor *minor)
492 struct drm_device *dev = minor->dev;
493 struct tilcdc_module *mod;
494 int ret;
496 ret = drm_debugfs_create_files(tilcdc_debugfs_list,
497 ARRAY_SIZE(tilcdc_debugfs_list),
498 minor->debugfs_root, minor);
500 list_for_each_entry(mod, &module_list, list)
501 if (mod->funcs->debugfs_init)
502 mod->funcs->debugfs_init(mod, minor);
504 if (ret) {
505 dev_err(dev->dev, "could not install tilcdc_debugfs_list\n");
506 return ret;
509 return ret;
512 static void tilcdc_debugfs_cleanup(struct drm_minor *minor)
514 struct tilcdc_module *mod;
515 drm_debugfs_remove_files(tilcdc_debugfs_list,
516 ARRAY_SIZE(tilcdc_debugfs_list), minor);
518 list_for_each_entry(mod, &module_list, list)
519 if (mod->funcs->debugfs_cleanup)
520 mod->funcs->debugfs_cleanup(mod, minor);
522 #endif
524 static const struct file_operations fops = {
525 .owner = THIS_MODULE,
526 .open = drm_open,
527 .release = drm_release,
528 .unlocked_ioctl = drm_ioctl,
529 #ifdef CONFIG_COMPAT
530 .compat_ioctl = drm_compat_ioctl,
531 #endif
532 .poll = drm_poll,
533 .read = drm_read,
534 .llseek = no_llseek,
535 .mmap = drm_gem_cma_mmap,
538 static struct drm_driver tilcdc_driver = {
539 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET,
540 .load = tilcdc_load,
541 .unload = tilcdc_unload,
542 .preclose = tilcdc_preclose,
543 .lastclose = tilcdc_lastclose,
544 .set_busid = drm_platform_set_busid,
545 .irq_handler = tilcdc_irq,
546 .irq_preinstall = tilcdc_irq_preinstall,
547 .irq_postinstall = tilcdc_irq_postinstall,
548 .irq_uninstall = tilcdc_irq_uninstall,
549 .get_vblank_counter = drm_vblank_count,
550 .enable_vblank = tilcdc_enable_vblank,
551 .disable_vblank = tilcdc_disable_vblank,
552 .gem_free_object = drm_gem_cma_free_object,
553 .gem_vm_ops = &drm_gem_cma_vm_ops,
554 .dumb_create = drm_gem_cma_dumb_create,
555 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
556 .dumb_destroy = drm_gem_dumb_destroy,
557 #ifdef CONFIG_DEBUG_FS
558 .debugfs_init = tilcdc_debugfs_init,
559 .debugfs_cleanup = tilcdc_debugfs_cleanup,
560 #endif
561 .fops = &fops,
562 .name = "tilcdc",
563 .desc = "TI LCD Controller DRM",
564 .date = "20121205",
565 .major = 1,
566 .minor = 0,
570 * Power management:
573 #ifdef CONFIG_PM_SLEEP
574 static int tilcdc_pm_suspend(struct device *dev)
576 struct drm_device *ddev = dev_get_drvdata(dev);
577 struct tilcdc_drm_private *priv = ddev->dev_private;
578 unsigned i, n = 0;
580 drm_kms_helper_poll_disable(ddev);
582 /* Save register state: */
583 for (i = 0; i < ARRAY_SIZE(registers); i++)
584 if (registers[i].save && (priv->rev >= registers[i].rev))
585 priv->saved_register[n++] = tilcdc_read(ddev, registers[i].reg);
587 return 0;
590 static int tilcdc_pm_resume(struct device *dev)
592 struct drm_device *ddev = dev_get_drvdata(dev);
593 struct tilcdc_drm_private *priv = ddev->dev_private;
594 unsigned i, n = 0;
596 /* Restore register state: */
597 for (i = 0; i < ARRAY_SIZE(registers); i++)
598 if (registers[i].save && (priv->rev >= registers[i].rev))
599 tilcdc_write(ddev, registers[i].reg, priv->saved_register[n++]);
601 drm_kms_helper_poll_enable(ddev);
603 return 0;
605 #endif
607 static const struct dev_pm_ops tilcdc_pm_ops = {
608 SET_SYSTEM_SLEEP_PM_OPS(tilcdc_pm_suspend, tilcdc_pm_resume)
612 * Platform driver:
615 static int tilcdc_pdev_probe(struct platform_device *pdev)
617 /* bail out early if no DT data: */
618 if (!pdev->dev.of_node) {
619 dev_err(&pdev->dev, "device-tree data is missing\n");
620 return -ENXIO;
623 /* defer probing if slave is in deferred probing */
624 if (slave_probing == true)
625 return -EPROBE_DEFER;
627 return drm_platform_init(&tilcdc_driver, pdev);
630 static int tilcdc_pdev_remove(struct platform_device *pdev)
632 drm_put_dev(platform_get_drvdata(pdev));
634 return 0;
637 static struct of_device_id tilcdc_of_match[] = {
638 { .compatible = "ti,am33xx-tilcdc", },
639 { },
641 MODULE_DEVICE_TABLE(of, tilcdc_of_match);
643 static struct platform_driver tilcdc_platform_driver = {
644 .probe = tilcdc_pdev_probe,
645 .remove = tilcdc_pdev_remove,
646 .driver = {
647 .name = "tilcdc",
648 .pm = &tilcdc_pm_ops,
649 .of_match_table = tilcdc_of_match,
653 static int __init tilcdc_drm_init(void)
655 DBG("init");
656 tilcdc_tfp410_init();
657 tilcdc_slave_init();
658 tilcdc_panel_init();
659 return platform_driver_register(&tilcdc_platform_driver);
662 static void __exit tilcdc_drm_fini(void)
664 DBG("fini");
665 platform_driver_unregister(&tilcdc_platform_driver);
666 tilcdc_panel_fini();
667 tilcdc_slave_fini();
668 tilcdc_tfp410_fini();
671 module_init(tilcdc_drm_init);
672 module_exit(tilcdc_drm_fini);
674 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
675 MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
676 MODULE_LICENSE("GPL");