blob: 25f3add2a75456263c621059298d4c57f7b4addd [file] [log] [blame]
Rob Clark16ea9752013-01-08 15:04:28 -06001/*
2 * Copyright (C) 2012 Texas Instruments
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
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.
8 *
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.
13 *
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/>.
16 */
17
18/* LCDC DRM driver, based on da8xx-fb */
19
20#include "tilcdc_drv.h"
21#include "tilcdc_regs.h"
22#include "tilcdc_tfp410.h"
Rob Clark6e8de0bd2013-01-22 16:02:21 -060023#include "tilcdc_slave.h"
Rob Clark16ea9752013-01-08 15:04:28 -060024
25#include "drm_fb_helper.h"
26
27static LIST_HEAD(module_list);
28
29void tilcdc_module_init(struct tilcdc_module *mod, const char *name,
30 const struct tilcdc_module_ops *funcs)
31{
32 mod->name = name;
33 mod->funcs = funcs;
34 INIT_LIST_HEAD(&mod->list);
35 list_add(&mod->list, &module_list);
36}
37
38void tilcdc_module_cleanup(struct tilcdc_module *mod)
39{
40 list_del(&mod->list);
41}
42
43static struct of_device_id tilcdc_of_match[];
44
45static struct drm_framebuffer *tilcdc_fb_create(struct drm_device *dev,
46 struct drm_file *file_priv, struct drm_mode_fb_cmd2 *mode_cmd)
47{
48 return drm_fb_cma_create(dev, file_priv, mode_cmd);
49}
50
51static void tilcdc_fb_output_poll_changed(struct drm_device *dev)
52{
53 struct tilcdc_drm_private *priv = dev->dev_private;
54 if (priv->fbdev)
55 drm_fbdev_cma_hotplug_event(priv->fbdev);
56}
57
58static const struct drm_mode_config_funcs mode_config_funcs = {
59 .fb_create = tilcdc_fb_create,
60 .output_poll_changed = tilcdc_fb_output_poll_changed,
61};
62
63static int modeset_init(struct drm_device *dev)
64{
65 struct tilcdc_drm_private *priv = dev->dev_private;
66 struct tilcdc_module *mod;
67
68 drm_mode_config_init(dev);
69
70 priv->crtc = tilcdc_crtc_create(dev);
71
72 list_for_each_entry(mod, &module_list, list) {
73 DBG("loading module: %s", mod->name);
74 mod->funcs->modeset_init(mod, dev);
75 }
76
77 if ((priv->num_encoders = 0) || (priv->num_connectors == 0)) {
78 /* oh nos! */
79 dev_err(dev->dev, "no encoders/connectors found\n");
80 return -ENXIO;
81 }
82
83 dev->mode_config.min_width = 0;
84 dev->mode_config.min_height = 0;
85 dev->mode_config.max_width = tilcdc_crtc_max_width(priv->crtc);
86 dev->mode_config.max_height = 2048;
87 dev->mode_config.funcs = &mode_config_funcs;
88
89 return 0;
90}
91
92#ifdef CONFIG_CPU_FREQ
93static int cpufreq_transition(struct notifier_block *nb,
94 unsigned long val, void *data)
95{
96 struct tilcdc_drm_private *priv = container_of(nb,
97 struct tilcdc_drm_private, freq_transition);
98 if (val == CPUFREQ_POSTCHANGE) {
99 if (priv->lcd_fck_rate != clk_get_rate(priv->clk)) {
100 priv->lcd_fck_rate = clk_get_rate(priv->clk);
101 tilcdc_crtc_update_clk(priv->crtc);
102 }
103 }
104
105 return 0;
106}
107#endif
108
109/*
110 * DRM operations:
111 */
112
113static int tilcdc_unload(struct drm_device *dev)
114{
115 struct tilcdc_drm_private *priv = dev->dev_private;
116 struct tilcdc_module *mod, *cur;
117
118 drm_kms_helper_poll_fini(dev);
119 drm_mode_config_cleanup(dev);
120 drm_vblank_cleanup(dev);
121
122 pm_runtime_get_sync(dev->dev);
123 drm_irq_uninstall(dev);
124 pm_runtime_put_sync(dev->dev);
125
126#ifdef CONFIG_CPU_FREQ
127 cpufreq_unregister_notifier(&priv->freq_transition,
128 CPUFREQ_TRANSITION_NOTIFIER);
129#endif
130
131 if (priv->clk)
132 clk_put(priv->clk);
133
134 if (priv->mmio)
135 iounmap(priv->mmio);
136
137 flush_workqueue(priv->wq);
138 destroy_workqueue(priv->wq);
139
140 dev->dev_private = NULL;
141
142 pm_runtime_disable(dev->dev);
143
144 list_for_each_entry_safe(mod, cur, &module_list, list) {
145 DBG("destroying module: %s", mod->name);
146 mod->funcs->destroy(mod);
147 }
148
149 kfree(priv);
150
151 return 0;
152}
153
154static int tilcdc_load(struct drm_device *dev, unsigned long flags)
155{
156 struct platform_device *pdev = dev->platformdev;
157 struct device_node *node = pdev->dev.of_node;
158 struct tilcdc_drm_private *priv;
159 struct resource *res;
160 int ret;
161
162 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
163 if (!priv) {
164 dev_err(dev->dev, "failed to allocate private data\n");
165 return -ENOMEM;
166 }
167
168 dev->dev_private = priv;
169
170 priv->wq = alloc_ordered_workqueue("tilcdc", 0);
171
172 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
173 if (!res) {
174 dev_err(dev->dev, "failed to get memory resource\n");
175 ret = -EINVAL;
176 goto fail;
177 }
178
179 priv->mmio = ioremap_nocache(res->start, resource_size(res));
180 if (!priv->mmio) {
181 dev_err(dev->dev, "failed to ioremap\n");
182 ret = -ENOMEM;
183 goto fail;
184 }
185
186 priv->clk = clk_get(dev->dev, "fck");
187 if (IS_ERR(priv->clk)) {
188 dev_err(dev->dev, "failed to get functional clock\n");
189 ret = -ENODEV;
190 goto fail;
191 }
192
193 priv->disp_clk = clk_get(dev->dev, "dpll_disp_ck");
194 if (IS_ERR(priv->clk)) {
195 dev_err(dev->dev, "failed to get display clock\n");
196 ret = -ENODEV;
197 goto fail;
198 }
199
200#ifdef CONFIG_CPU_FREQ
201 priv->lcd_fck_rate = clk_get_rate(priv->clk);
202 priv->freq_transition.notifier_call = cpufreq_transition;
203 ret = cpufreq_register_notifier(&priv->freq_transition,
204 CPUFREQ_TRANSITION_NOTIFIER);
205 if (ret) {
206 dev_err(dev->dev, "failed to register cpufreq notifier\n");
207 goto fail;
208 }
209#endif
210
211 if (of_property_read_u32(node, "max-bandwidth", &priv->max_bandwidth))
212 priv->max_bandwidth = 1280 * 1024 * 60;
213
214 pm_runtime_enable(dev->dev);
215
216 /* Determine LCD IP Version */
217 pm_runtime_get_sync(dev->dev);
218 switch (tilcdc_read(dev, LCDC_PID_REG)) {
219 case 0x4c100102:
220 priv->rev = 1;
221 break;
222 case 0x4f200800:
223 case 0x4f201000:
224 priv->rev = 2;
225 break;
226 default:
227 dev_warn(dev->dev, "Unknown PID Reg value 0x%08x, "
228 "defaulting to LCD revision 1\n",
229 tilcdc_read(dev, LCDC_PID_REG));
230 priv->rev = 1;
231 break;
232 }
233
234 pm_runtime_put_sync(dev->dev);
235
236 ret = modeset_init(dev);
237 if (ret < 0) {
238 dev_err(dev->dev, "failed to initialize mode setting\n");
239 goto fail;
240 }
241
242 ret = drm_vblank_init(dev, 1);
243 if (ret < 0) {
244 dev_err(dev->dev, "failed to initialize vblank\n");
245 goto fail;
246 }
247
248 pm_runtime_get_sync(dev->dev);
249 ret = drm_irq_install(dev);
250 pm_runtime_put_sync(dev->dev);
251 if (ret < 0) {
252 dev_err(dev->dev, "failed to install IRQ handler\n");
253 goto fail;
254 }
255
256 platform_set_drvdata(pdev, dev);
257
258 priv->fbdev = drm_fbdev_cma_init(dev, 16,
259 dev->mode_config.num_crtc,
260 dev->mode_config.num_connector);
261
262 drm_kms_helper_poll_init(dev);
263
264 return 0;
265
266fail:
267 tilcdc_unload(dev);
268 return ret;
269}
270
271static void tilcdc_preclose(struct drm_device *dev, struct drm_file *file)
272{
273 struct tilcdc_drm_private *priv = dev->dev_private;
274
275 tilcdc_crtc_cancel_page_flip(priv->crtc, file);
276}
277
278static void tilcdc_lastclose(struct drm_device *dev)
279{
280 struct tilcdc_drm_private *priv = dev->dev_private;
281 drm_fbdev_cma_restore_mode(priv->fbdev);
282}
283
284static irqreturn_t tilcdc_irq(DRM_IRQ_ARGS)
285{
286 struct drm_device *dev = arg;
287 struct tilcdc_drm_private *priv = dev->dev_private;
288 return tilcdc_crtc_irq(priv->crtc);
289}
290
291static void tilcdc_irq_preinstall(struct drm_device *dev)
292{
293 tilcdc_clear_irqstatus(dev, 0xffffffff);
294}
295
296static int tilcdc_irq_postinstall(struct drm_device *dev)
297{
298 struct tilcdc_drm_private *priv = dev->dev_private;
299
300 /* enable FIFO underflow irq: */
301 if (priv->rev == 1) {
302 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_UNDERFLOW_INT_ENA);
303 } else {
304 tilcdc_set(dev, LCDC_INT_ENABLE_SET_REG, LCDC_V2_UNDERFLOW_INT_ENA);
305 }
306
307 return 0;
308}
309
310static void tilcdc_irq_uninstall(struct drm_device *dev)
311{
312 struct tilcdc_drm_private *priv = dev->dev_private;
313
314 /* disable irqs that we might have enabled: */
315 if (priv->rev == 1) {
316 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
317 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
318 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_V1_END_OF_FRAME_INT_ENA);
319 } else {
320 tilcdc_clear(dev, LCDC_INT_ENABLE_SET_REG,
321 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
322 LCDC_V2_END_OF_FRAME0_INT_ENA | LCDC_V2_END_OF_FRAME1_INT_ENA |
323 LCDC_FRAME_DONE);
324 }
325
326}
327
328static void enable_vblank(struct drm_device *dev, bool enable)
329{
330 struct tilcdc_drm_private *priv = dev->dev_private;
331 u32 reg, mask;
332
333 if (priv->rev == 1) {
334 reg = LCDC_DMA_CTRL_REG;
335 mask = LCDC_V1_END_OF_FRAME_INT_ENA;
336 } else {
337 reg = LCDC_INT_ENABLE_SET_REG;
338 mask = LCDC_V2_END_OF_FRAME0_INT_ENA |
339 LCDC_V2_END_OF_FRAME1_INT_ENA | LCDC_FRAME_DONE;
340 }
341
342 if (enable)
343 tilcdc_set(dev, reg, mask);
344 else
345 tilcdc_clear(dev, reg, mask);
346}
347
348static int tilcdc_enable_vblank(struct drm_device *dev, int crtc)
349{
350 enable_vblank(dev, true);
351 return 0;
352}
353
354static void tilcdc_disable_vblank(struct drm_device *dev, int crtc)
355{
356 enable_vblank(dev, false);
357}
358
359#if defined(CONFIG_DEBUG_FS) || defined(CONFIG_PM_SLEEP)
360static const struct {
361 const char *name;
362 uint8_t rev;
363 uint8_t save;
364 uint32_t reg;
365} registers[] = {
366#define REG(rev, save, reg) { #reg, rev, save, reg }
367 /* exists in revision 1: */
368 REG(1, false, LCDC_PID_REG),
369 REG(1, true, LCDC_CTRL_REG),
370 REG(1, false, LCDC_STAT_REG),
371 REG(1, true, LCDC_RASTER_CTRL_REG),
372 REG(1, true, LCDC_RASTER_TIMING_0_REG),
373 REG(1, true, LCDC_RASTER_TIMING_1_REG),
374 REG(1, true, LCDC_RASTER_TIMING_2_REG),
375 REG(1, true, LCDC_DMA_CTRL_REG),
376 REG(1, true, LCDC_DMA_FB_BASE_ADDR_0_REG),
377 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_0_REG),
378 REG(1, true, LCDC_DMA_FB_BASE_ADDR_1_REG),
379 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_1_REG),
380 /* new in revision 2: */
381 REG(2, false, LCDC_RAW_STAT_REG),
382 REG(2, false, LCDC_MASKED_STAT_REG),
383 REG(2, false, LCDC_INT_ENABLE_SET_REG),
384 REG(2, false, LCDC_INT_ENABLE_CLR_REG),
385 REG(2, false, LCDC_END_OF_INT_IND_REG),
386 REG(2, true, LCDC_CLK_ENABLE_REG),
387 REG(2, true, LCDC_INT_ENABLE_SET_REG),
388#undef REG
389};
390#endif
391
392#ifdef CONFIG_DEBUG_FS
393static int tilcdc_regs_show(struct seq_file *m, void *arg)
394{
395 struct drm_info_node *node = (struct drm_info_node *) m->private;
396 struct drm_device *dev = node->minor->dev;
397 struct tilcdc_drm_private *priv = dev->dev_private;
398 unsigned i;
399
400 pm_runtime_get_sync(dev->dev);
401
402 seq_printf(m, "revision: %d\n", priv->rev);
403
404 for (i = 0; i < ARRAY_SIZE(registers); i++)
405 if (priv->rev >= registers[i].rev)
406 seq_printf(m, "%s:\t %08x\n", registers[i].name,
407 tilcdc_read(dev, registers[i].reg));
408
409 pm_runtime_put_sync(dev->dev);
410
411 return 0;
412}
413
414static int tilcdc_mm_show(struct seq_file *m, void *arg)
415{
416 struct drm_info_node *node = (struct drm_info_node *) m->private;
417 struct drm_device *dev = node->minor->dev;
418 return drm_mm_dump_table(m, dev->mm_private);
419}
420
421static struct drm_info_list tilcdc_debugfs_list[] = {
422 { "regs", tilcdc_regs_show, 0 },
423 { "mm", tilcdc_mm_show, 0 },
424 { "fb", drm_fb_cma_debugfs_show, 0 },
425};
426
427static int tilcdc_debugfs_init(struct drm_minor *minor)
428{
429 struct drm_device *dev = minor->dev;
430 struct tilcdc_module *mod;
431 int ret;
432
433 ret = drm_debugfs_create_files(tilcdc_debugfs_list,
434 ARRAY_SIZE(tilcdc_debugfs_list),
435 minor->debugfs_root, minor);
436
437 list_for_each_entry(mod, &module_list, list)
438 if (mod->funcs->debugfs_init)
439 mod->funcs->debugfs_init(mod, minor);
440
441 if (ret) {
442 dev_err(dev->dev, "could not install tilcdc_debugfs_list\n");
443 return ret;
444 }
445
446 return ret;
447}
448
449static void tilcdc_debugfs_cleanup(struct drm_minor *minor)
450{
451 struct tilcdc_module *mod;
452 drm_debugfs_remove_files(tilcdc_debugfs_list,
453 ARRAY_SIZE(tilcdc_debugfs_list), minor);
454
455 list_for_each_entry(mod, &module_list, list)
456 if (mod->funcs->debugfs_cleanup)
457 mod->funcs->debugfs_cleanup(mod, minor);
458}
459#endif
460
461static const struct file_operations fops = {
462 .owner = THIS_MODULE,
463 .open = drm_open,
464 .release = drm_release,
465 .unlocked_ioctl = drm_ioctl,
466#ifdef CONFIG_COMPAT
467 .compat_ioctl = drm_compat_ioctl,
468#endif
469 .poll = drm_poll,
470 .read = drm_read,
471 .fasync = drm_fasync,
472 .llseek = no_llseek,
473 .mmap = drm_gem_cma_mmap,
474};
475
476static struct drm_driver tilcdc_driver = {
477 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET,
478 .load = tilcdc_load,
479 .unload = tilcdc_unload,
480 .preclose = tilcdc_preclose,
481 .lastclose = tilcdc_lastclose,
482 .irq_handler = tilcdc_irq,
483 .irq_preinstall = tilcdc_irq_preinstall,
484 .irq_postinstall = tilcdc_irq_postinstall,
485 .irq_uninstall = tilcdc_irq_uninstall,
486 .get_vblank_counter = drm_vblank_count,
487 .enable_vblank = tilcdc_enable_vblank,
488 .disable_vblank = tilcdc_disable_vblank,
489 .gem_free_object = drm_gem_cma_free_object,
490 .gem_vm_ops = &drm_gem_cma_vm_ops,
491 .dumb_create = drm_gem_cma_dumb_create,
492 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
493 .dumb_destroy = drm_gem_cma_dumb_destroy,
494#ifdef CONFIG_DEBUG_FS
495 .debugfs_init = tilcdc_debugfs_init,
496 .debugfs_cleanup = tilcdc_debugfs_cleanup,
497#endif
498 .fops = &fops,
499 .name = "tilcdc",
500 .desc = "TI LCD Controller DRM",
501 .date = "20121205",
502 .major = 1,
503 .minor = 0,
504};
505
506/*
507 * Power management:
508 */
509
510#ifdef CONFIG_PM_SLEEP
511static int tilcdc_pm_suspend(struct device *dev)
512{
513 struct drm_device *ddev = dev_get_drvdata(dev);
514 struct tilcdc_drm_private *priv = ddev->dev_private;
515 unsigned i, n = 0;
516
517 drm_kms_helper_poll_disable(ddev);
518
519 /* Save register state: */
520 for (i = 0; i < ARRAY_SIZE(registers); i++)
521 if (registers[i].save && (priv->rev >= registers[i].rev))
522 priv->saved_register[n++] = tilcdc_read(ddev, registers[i].reg);
523
524 return 0;
525}
526
527static int tilcdc_pm_resume(struct device *dev)
528{
529 struct drm_device *ddev = dev_get_drvdata(dev);
530 struct tilcdc_drm_private *priv = ddev->dev_private;
531 unsigned i, n = 0;
532
533 /* Restore register state: */
534 for (i = 0; i < ARRAY_SIZE(registers); i++)
535 if (registers[i].save && (priv->rev >= registers[i].rev))
536 tilcdc_write(ddev, registers[i].reg, priv->saved_register[n++]);
537
538 drm_kms_helper_poll_enable(ddev);
539
540 return 0;
541}
542#endif
543
544static const struct dev_pm_ops tilcdc_pm_ops = {
545 SET_SYSTEM_SLEEP_PM_OPS(tilcdc_pm_suspend, tilcdc_pm_resume)
546};
547
548/*
549 * Platform driver:
550 */
551
552static int tilcdc_pdev_probe(struct platform_device *pdev)
553{
554 /* bail out early if no DT data: */
555 if (!pdev->dev.of_node) {
556 dev_err(&pdev->dev, "device-tree data is missing\n");
557 return -ENXIO;
558 }
559
560 return drm_platform_init(&tilcdc_driver, pdev);
561}
562
563static int tilcdc_pdev_remove(struct platform_device *pdev)
564{
565 drm_platform_exit(&tilcdc_driver, pdev);
566
567 return 0;
568}
569
570static struct of_device_id tilcdc_of_match[] = {
571 { .compatible = "ti,am33xx-tilcdc", },
572 { },
573};
574MODULE_DEVICE_TABLE(of, tilcdc_of_match);
575
576static struct platform_driver tilcdc_platform_driver = {
577 .probe = tilcdc_pdev_probe,
578 .remove = tilcdc_pdev_remove,
579 .driver = {
580 .owner = THIS_MODULE,
581 .name = "tilcdc",
582 .pm = &tilcdc_pm_ops,
583 .of_match_table = tilcdc_of_match,
584 },
585};
586
587static int __init tilcdc_drm_init(void)
588{
589 DBG("init");
590 tilcdc_tfp410_init();
Rob Clark6e8de0bd2013-01-22 16:02:21 -0600591 tilcdc_slave_init();
Rob Clark16ea9752013-01-08 15:04:28 -0600592 return platform_driver_register(&tilcdc_platform_driver);
593}
594
595static void __exit tilcdc_drm_fini(void)
596{
597 DBG("fini");
598 tilcdc_tfp410_fini();
Rob Clark6e8de0bd2013-01-22 16:02:21 -0600599 tilcdc_slave_fini();
Rob Clark16ea9752013-01-08 15:04:28 -0600600 platform_driver_unregister(&tilcdc_platform_driver);
601}
602
Rob Clark6e8de0bd2013-01-22 16:02:21 -0600603late_initcall(tilcdc_drm_init);
Rob Clark16ea9752013-01-08 15:04:28 -0600604module_exit(tilcdc_drm_fini);
605
606MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
607MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
608MODULE_LICENSE("GPL");