drm/tilcdc: Enable and disable interrupts in crtc start() and stop()
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / tilcdc / tilcdc_drv.c
1 /*
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 <linux/component.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/suspend.h>
23 #include <drm/drm_atomic.h>
24 #include <drm/drm_atomic_helper.h>
25
26 #include "tilcdc_drv.h"
27 #include "tilcdc_regs.h"
28 #include "tilcdc_tfp410.h"
29 #include "tilcdc_panel.h"
30 #include "tilcdc_external.h"
31
32 #include "drm_fb_helper.h"
33
34 static LIST_HEAD(module_list);
35
36 void tilcdc_module_init(struct tilcdc_module *mod, const char *name,
37                 const struct tilcdc_module_ops *funcs)
38 {
39         mod->name = name;
40         mod->funcs = funcs;
41         INIT_LIST_HEAD(&mod->list);
42         list_add(&mod->list, &module_list);
43 }
44
45 void tilcdc_module_cleanup(struct tilcdc_module *mod)
46 {
47         list_del(&mod->list);
48 }
49
50 static struct of_device_id tilcdc_of_match[];
51
52 static struct drm_framebuffer *tilcdc_fb_create(struct drm_device *dev,
53                 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
54 {
55         return drm_fb_cma_create(dev, file_priv, mode_cmd);
56 }
57
58 static void tilcdc_fb_output_poll_changed(struct drm_device *dev)
59 {
60         struct tilcdc_drm_private *priv = dev->dev_private;
61         drm_fbdev_cma_hotplug_event(priv->fbdev);
62 }
63
64 int tilcdc_atomic_check(struct drm_device *dev,
65                         struct drm_atomic_state *state)
66 {
67         int ret;
68
69         ret = drm_atomic_helper_check_modeset(dev, state);
70         if (ret)
71                 return ret;
72
73         ret = drm_atomic_helper_check_planes(dev, state);
74         if (ret)
75                 return ret;
76
77         /*
78          * tilcdc ->atomic_check can update ->mode_changed if pixel format
79          * changes, hence will we check modeset changes again.
80          */
81         ret = drm_atomic_helper_check_modeset(dev, state);
82         if (ret)
83                 return ret;
84
85         return ret;
86 }
87
88 static int tilcdc_commit(struct drm_device *dev,
89                   struct drm_atomic_state *state,
90                   bool async)
91 {
92         int ret;
93
94         ret = drm_atomic_helper_prepare_planes(dev, state);
95         if (ret)
96                 return ret;
97
98         drm_atomic_helper_swap_state(state, true);
99
100         /*
101          * Everything below can be run asynchronously without the need to grab
102          * any modeset locks at all under one condition: It must be guaranteed
103          * that the asynchronous work has either been cancelled (if the driver
104          * supports it, which at least requires that the framebuffers get
105          * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
106          * before the new state gets committed on the software side with
107          * drm_atomic_helper_swap_state().
108          *
109          * This scheme allows new atomic state updates to be prepared and
110          * checked in parallel to the asynchronous completion of the previous
111          * update. Which is important since compositors need to figure out the
112          * composition of the next frame right after having submitted the
113          * current layout.
114          */
115
116         drm_atomic_helper_commit_modeset_disables(dev, state);
117
118         drm_atomic_helper_commit_planes(dev, state, false);
119
120         drm_atomic_helper_commit_modeset_enables(dev, state);
121
122         drm_atomic_helper_wait_for_vblanks(dev, state);
123
124         drm_atomic_helper_cleanup_planes(dev, state);
125
126         drm_atomic_state_free(state);
127
128         return 0;
129 }
130
131 static const struct drm_mode_config_funcs mode_config_funcs = {
132         .fb_create = tilcdc_fb_create,
133         .output_poll_changed = tilcdc_fb_output_poll_changed,
134         .atomic_check = tilcdc_atomic_check,
135         .atomic_commit = tilcdc_commit,
136 };
137
138 static int modeset_init(struct drm_device *dev)
139 {
140         struct tilcdc_drm_private *priv = dev->dev_private;
141         struct tilcdc_module *mod;
142
143         drm_mode_config_init(dev);
144
145         priv->crtc = tilcdc_crtc_create(dev);
146
147         list_for_each_entry(mod, &module_list, list) {
148                 DBG("loading module: %s", mod->name);
149                 mod->funcs->modeset_init(mod, dev);
150         }
151
152         dev->mode_config.min_width = 0;
153         dev->mode_config.min_height = 0;
154         dev->mode_config.max_width = tilcdc_crtc_max_width(priv->crtc);
155         dev->mode_config.max_height = 2048;
156         dev->mode_config.funcs = &mode_config_funcs;
157
158         return 0;
159 }
160
161 #ifdef CONFIG_CPU_FREQ
162 static int cpufreq_transition(struct notifier_block *nb,
163                                      unsigned long val, void *data)
164 {
165         struct tilcdc_drm_private *priv = container_of(nb,
166                         struct tilcdc_drm_private, freq_transition);
167         if (val == CPUFREQ_POSTCHANGE) {
168                 if (priv->lcd_fck_rate != clk_get_rate(priv->clk)) {
169                         priv->lcd_fck_rate = clk_get_rate(priv->clk);
170                         tilcdc_crtc_update_clk(priv->crtc);
171                 }
172         }
173
174         return 0;
175 }
176 #endif
177
178 /*
179  * DRM operations:
180  */
181
182 static int tilcdc_unload(struct drm_device *dev)
183 {
184         struct tilcdc_drm_private *priv = dev->dev_private;
185
186         tilcdc_crtc_dpms(priv->crtc, DRM_MODE_DPMS_OFF);
187
188         tilcdc_remove_external_encoders(dev);
189
190         drm_fbdev_cma_fini(priv->fbdev);
191         drm_kms_helper_poll_fini(dev);
192         drm_mode_config_cleanup(dev);
193         drm_vblank_cleanup(dev);
194
195         drm_irq_uninstall(dev);
196
197 #ifdef CONFIG_CPU_FREQ
198         cpufreq_unregister_notifier(&priv->freq_transition,
199                         CPUFREQ_TRANSITION_NOTIFIER);
200 #endif
201
202         if (priv->clk)
203                 clk_put(priv->clk);
204
205         if (priv->mmio)
206                 iounmap(priv->mmio);
207
208         flush_workqueue(priv->wq);
209         destroy_workqueue(priv->wq);
210
211         dev->dev_private = NULL;
212
213         pm_runtime_disable(dev->dev);
214
215         return 0;
216 }
217
218 static size_t tilcdc_num_regs(void);
219
220 static int tilcdc_load(struct drm_device *dev, unsigned long flags)
221 {
222         struct platform_device *pdev = dev->platformdev;
223         struct device_node *node = pdev->dev.of_node;
224         struct tilcdc_drm_private *priv;
225         struct tilcdc_module *mod;
226         struct resource *res;
227         u32 bpp = 0;
228         int ret;
229
230         priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL);
231         if (priv)
232                 priv->saved_register =
233                         devm_kcalloc(dev->dev, tilcdc_num_regs(),
234                                      sizeof(*priv->saved_register), GFP_KERNEL);
235         if (!priv || !priv->saved_register) {
236                 dev_err(dev->dev, "failed to allocate private data\n");
237                 return -ENOMEM;
238         }
239
240         dev->dev_private = priv;
241
242         priv->is_componentized =
243                 tilcdc_get_external_components(dev->dev, NULL) > 0;
244
245         priv->wq = alloc_ordered_workqueue("tilcdc", 0);
246         if (!priv->wq) {
247                 ret = -ENOMEM;
248                 goto fail_unset_priv;
249         }
250
251         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
252         if (!res) {
253                 dev_err(dev->dev, "failed to get memory resource\n");
254                 ret = -EINVAL;
255                 goto fail_free_wq;
256         }
257
258         priv->mmio = ioremap_nocache(res->start, resource_size(res));
259         if (!priv->mmio) {
260                 dev_err(dev->dev, "failed to ioremap\n");
261                 ret = -ENOMEM;
262                 goto fail_free_wq;
263         }
264
265         priv->clk = clk_get(dev->dev, "fck");
266         if (IS_ERR(priv->clk)) {
267                 dev_err(dev->dev, "failed to get functional clock\n");
268                 ret = -ENODEV;
269                 goto fail_iounmap;
270         }
271
272 #ifdef CONFIG_CPU_FREQ
273         priv->lcd_fck_rate = clk_get_rate(priv->clk);
274         priv->freq_transition.notifier_call = cpufreq_transition;
275         ret = cpufreq_register_notifier(&priv->freq_transition,
276                         CPUFREQ_TRANSITION_NOTIFIER);
277         if (ret) {
278                 dev_err(dev->dev, "failed to register cpufreq notifier\n");
279                 goto fail_put_clk;
280         }
281 #endif
282
283         if (of_property_read_u32(node, "max-bandwidth", &priv->max_bandwidth))
284                 priv->max_bandwidth = TILCDC_DEFAULT_MAX_BANDWIDTH;
285
286         DBG("Maximum Bandwidth Value %d", priv->max_bandwidth);
287
288         if (of_property_read_u32(node, "ti,max-width", &priv->max_width))
289                 priv->max_width = TILCDC_DEFAULT_MAX_WIDTH;
290
291         DBG("Maximum Horizontal Pixel Width Value %dpixels", priv->max_width);
292
293         if (of_property_read_u32(node, "ti,max-pixelclock",
294                                         &priv->max_pixelclock))
295                 priv->max_pixelclock = TILCDC_DEFAULT_MAX_PIXELCLOCK;
296
297         DBG("Maximum Pixel Clock Value %dKHz", priv->max_pixelclock);
298
299         pm_runtime_enable(dev->dev);
300
301         /* Determine LCD IP Version */
302         pm_runtime_get_sync(dev->dev);
303         switch (tilcdc_read(dev, LCDC_PID_REG)) {
304         case 0x4c100102:
305                 priv->rev = 1;
306                 break;
307         case 0x4f200800:
308         case 0x4f201000:
309                 priv->rev = 2;
310                 break;
311         default:
312                 dev_warn(dev->dev, "Unknown PID Reg value 0x%08x, "
313                                 "defaulting to LCD revision 1\n",
314                                 tilcdc_read(dev, LCDC_PID_REG));
315                 priv->rev = 1;
316                 break;
317         }
318
319         pm_runtime_put_sync(dev->dev);
320
321         ret = modeset_init(dev);
322         if (ret < 0) {
323                 dev_err(dev->dev, "failed to initialize mode setting\n");
324                 goto fail_cpufreq_unregister;
325         }
326
327         platform_set_drvdata(pdev, dev);
328
329         if (priv->is_componentized) {
330                 ret = component_bind_all(dev->dev, dev);
331                 if (ret < 0)
332                         goto fail_mode_config_cleanup;
333
334                 ret = tilcdc_add_external_encoders(dev, &bpp);
335                 if (ret < 0)
336                         goto fail_component_cleanup;
337         }
338
339         if ((priv->num_encoders == 0) || (priv->num_connectors == 0)) {
340                 dev_err(dev->dev, "no encoders/connectors found\n");
341                 ret = -ENXIO;
342                 goto fail_external_cleanup;
343         }
344
345         ret = drm_vblank_init(dev, 1);
346         if (ret < 0) {
347                 dev_err(dev->dev, "failed to initialize vblank\n");
348                 goto fail_external_cleanup;
349         }
350
351         ret = drm_irq_install(dev, platform_get_irq(dev->platformdev, 0));
352         if (ret < 0) {
353                 dev_err(dev->dev, "failed to install IRQ handler\n");
354                 goto fail_vblank_cleanup;
355         }
356
357         list_for_each_entry(mod, &module_list, list) {
358                 DBG("%s: preferred_bpp: %d", mod->name, mod->preferred_bpp);
359                 bpp = mod->preferred_bpp;
360                 if (bpp > 0)
361                         break;
362         }
363
364         drm_helper_disable_unused_functions(dev);
365
366         drm_mode_config_reset(dev);
367
368         priv->fbdev = drm_fbdev_cma_init(dev, bpp,
369                         dev->mode_config.num_crtc,
370                         dev->mode_config.num_connector);
371         if (IS_ERR(priv->fbdev)) {
372                 ret = PTR_ERR(priv->fbdev);
373                 goto fail_irq_uninstall;
374         }
375
376         drm_kms_helper_poll_init(dev);
377
378         return 0;
379
380 fail_irq_uninstall:
381         drm_irq_uninstall(dev);
382
383 fail_vblank_cleanup:
384         drm_vblank_cleanup(dev);
385
386 fail_mode_config_cleanup:
387         drm_mode_config_cleanup(dev);
388
389 fail_component_cleanup:
390         if (priv->is_componentized)
391                 component_unbind_all(dev->dev, dev);
392
393 fail_external_cleanup:
394         tilcdc_remove_external_encoders(dev);
395
396 fail_cpufreq_unregister:
397         pm_runtime_disable(dev->dev);
398 #ifdef CONFIG_CPU_FREQ
399         cpufreq_unregister_notifier(&priv->freq_transition,
400                         CPUFREQ_TRANSITION_NOTIFIER);
401
402 fail_put_clk:
403 #endif
404         clk_put(priv->clk);
405
406 fail_iounmap:
407         iounmap(priv->mmio);
408
409 fail_free_wq:
410         flush_workqueue(priv->wq);
411         destroy_workqueue(priv->wq);
412
413 fail_unset_priv:
414         dev->dev_private = NULL;
415
416         return ret;
417 }
418
419 static void tilcdc_lastclose(struct drm_device *dev)
420 {
421         struct tilcdc_drm_private *priv = dev->dev_private;
422         drm_fbdev_cma_restore_mode(priv->fbdev);
423 }
424
425 static irqreturn_t tilcdc_irq(int irq, void *arg)
426 {
427         struct drm_device *dev = arg;
428         struct tilcdc_drm_private *priv = dev->dev_private;
429         return tilcdc_crtc_irq(priv->crtc);
430 }
431
432 static int tilcdc_enable_vblank(struct drm_device *dev, unsigned int pipe)
433 {
434         return 0;
435 }
436
437 static void tilcdc_disable_vblank(struct drm_device *dev, unsigned int pipe)
438 {
439         return;
440 }
441
442 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_PM_SLEEP)
443 static const struct {
444         const char *name;
445         uint8_t  rev;
446         uint8_t  save;
447         uint32_t reg;
448 } registers[] =         {
449 #define REG(rev, save, reg) { #reg, rev, save, reg }
450                 /* exists in revision 1: */
451                 REG(1, false, LCDC_PID_REG),
452                 REG(1, true,  LCDC_CTRL_REG),
453                 REG(1, false, LCDC_STAT_REG),
454                 REG(1, true,  LCDC_RASTER_CTRL_REG),
455                 REG(1, true,  LCDC_RASTER_TIMING_0_REG),
456                 REG(1, true,  LCDC_RASTER_TIMING_1_REG),
457                 REG(1, true,  LCDC_RASTER_TIMING_2_REG),
458                 REG(1, true,  LCDC_DMA_CTRL_REG),
459                 REG(1, true,  LCDC_DMA_FB_BASE_ADDR_0_REG),
460                 REG(1, true,  LCDC_DMA_FB_CEILING_ADDR_0_REG),
461                 REG(1, true,  LCDC_DMA_FB_BASE_ADDR_1_REG),
462                 REG(1, true,  LCDC_DMA_FB_CEILING_ADDR_1_REG),
463                 /* new in revision 2: */
464                 REG(2, false, LCDC_RAW_STAT_REG),
465                 REG(2, false, LCDC_MASKED_STAT_REG),
466                 REG(2, true, LCDC_INT_ENABLE_SET_REG),
467                 REG(2, false, LCDC_INT_ENABLE_CLR_REG),
468                 REG(2, false, LCDC_END_OF_INT_IND_REG),
469                 REG(2, true,  LCDC_CLK_ENABLE_REG),
470 #undef REG
471 };
472
473 static size_t tilcdc_num_regs(void)
474 {
475         return ARRAY_SIZE(registers);
476 }
477 #else
478 static size_t tilcdc_num_regs(void)
479 {
480         return 0;
481 }
482 #endif
483
484 #ifdef CONFIG_DEBUG_FS
485 static int tilcdc_regs_show(struct seq_file *m, void *arg)
486 {
487         struct drm_info_node *node = (struct drm_info_node *) m->private;
488         struct drm_device *dev = node->minor->dev;
489         struct tilcdc_drm_private *priv = dev->dev_private;
490         unsigned i;
491
492         pm_runtime_get_sync(dev->dev);
493
494         seq_printf(m, "revision: %d\n", priv->rev);
495
496         for (i = 0; i < ARRAY_SIZE(registers); i++)
497                 if (priv->rev >= registers[i].rev)
498                         seq_printf(m, "%s:\t %08x\n", registers[i].name,
499                                         tilcdc_read(dev, registers[i].reg));
500
501         pm_runtime_put_sync(dev->dev);
502
503         return 0;
504 }
505
506 static int tilcdc_mm_show(struct seq_file *m, void *arg)
507 {
508         struct drm_info_node *node = (struct drm_info_node *) m->private;
509         struct drm_device *dev = node->minor->dev;
510         return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm);
511 }
512
513 static struct drm_info_list tilcdc_debugfs_list[] = {
514                 { "regs", tilcdc_regs_show, 0 },
515                 { "mm",   tilcdc_mm_show,   0 },
516                 { "fb",   drm_fb_cma_debugfs_show, 0 },
517 };
518
519 static int tilcdc_debugfs_init(struct drm_minor *minor)
520 {
521         struct drm_device *dev = minor->dev;
522         struct tilcdc_module *mod;
523         int ret;
524
525         ret = drm_debugfs_create_files(tilcdc_debugfs_list,
526                         ARRAY_SIZE(tilcdc_debugfs_list),
527                         minor->debugfs_root, minor);
528
529         list_for_each_entry(mod, &module_list, list)
530                 if (mod->funcs->debugfs_init)
531                         mod->funcs->debugfs_init(mod, minor);
532
533         if (ret) {
534                 dev_err(dev->dev, "could not install tilcdc_debugfs_list\n");
535                 return ret;
536         }
537
538         return ret;
539 }
540
541 static void tilcdc_debugfs_cleanup(struct drm_minor *minor)
542 {
543         struct tilcdc_module *mod;
544         drm_debugfs_remove_files(tilcdc_debugfs_list,
545                         ARRAY_SIZE(tilcdc_debugfs_list), minor);
546
547         list_for_each_entry(mod, &module_list, list)
548                 if (mod->funcs->debugfs_cleanup)
549                         mod->funcs->debugfs_cleanup(mod, minor);
550 }
551 #endif
552
553 static const struct file_operations fops = {
554         .owner              = THIS_MODULE,
555         .open               = drm_open,
556         .release            = drm_release,
557         .unlocked_ioctl     = drm_ioctl,
558 #ifdef CONFIG_COMPAT
559         .compat_ioctl       = drm_compat_ioctl,
560 #endif
561         .poll               = drm_poll,
562         .read               = drm_read,
563         .llseek             = no_llseek,
564         .mmap               = drm_gem_cma_mmap,
565 };
566
567 static struct drm_driver tilcdc_driver = {
568         .driver_features    = (DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET |
569                                DRIVER_PRIME | DRIVER_ATOMIC),
570         .load               = tilcdc_load,
571         .unload             = tilcdc_unload,
572         .lastclose          = tilcdc_lastclose,
573         .irq_handler        = tilcdc_irq,
574         .get_vblank_counter = drm_vblank_no_hw_counter,
575         .enable_vblank      = tilcdc_enable_vblank,
576         .disable_vblank     = tilcdc_disable_vblank,
577         .gem_free_object_unlocked = drm_gem_cma_free_object,
578         .gem_vm_ops         = &drm_gem_cma_vm_ops,
579         .dumb_create        = drm_gem_cma_dumb_create,
580         .dumb_map_offset    = drm_gem_cma_dumb_map_offset,
581         .dumb_destroy       = drm_gem_dumb_destroy,
582
583         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
584         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
585         .gem_prime_import       = drm_gem_prime_import,
586         .gem_prime_export       = drm_gem_prime_export,
587         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
588         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
589         .gem_prime_vmap         = drm_gem_cma_prime_vmap,
590         .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
591         .gem_prime_mmap         = drm_gem_cma_prime_mmap,
592 #ifdef CONFIG_DEBUG_FS
593         .debugfs_init       = tilcdc_debugfs_init,
594         .debugfs_cleanup    = tilcdc_debugfs_cleanup,
595 #endif
596         .fops               = &fops,
597         .name               = "tilcdc",
598         .desc               = "TI LCD Controller DRM",
599         .date               = "20121205",
600         .major              = 1,
601         .minor              = 0,
602 };
603
604 /*
605  * Power management:
606  */
607
608 #ifdef CONFIG_PM_SLEEP
609 static int tilcdc_pm_suspend(struct device *dev)
610 {
611         struct drm_device *ddev = dev_get_drvdata(dev);
612         struct tilcdc_drm_private *priv = ddev->dev_private;
613         unsigned i, n = 0;
614
615         drm_kms_helper_poll_disable(ddev);
616
617         /* Select sleep pin state */
618         pinctrl_pm_select_sleep_state(dev);
619
620         if (pm_runtime_suspended(dev)) {
621                 priv->ctx_valid = false;
622                 return 0;
623         }
624
625         /* Disable the LCDC controller, to avoid locking up the PRCM */
626         priv->saved_dpms_state = tilcdc_crtc_current_dpms_state(priv->crtc);
627         tilcdc_crtc_dpms(priv->crtc, DRM_MODE_DPMS_OFF);
628
629         /* Save register state: */
630         for (i = 0; i < ARRAY_SIZE(registers); i++)
631                 if (registers[i].save && (priv->rev >= registers[i].rev))
632                         priv->saved_register[n++] = tilcdc_read(ddev, registers[i].reg);
633
634         priv->ctx_valid = true;
635
636         return 0;
637 }
638
639 static int tilcdc_pm_resume(struct device *dev)
640 {
641         struct drm_device *ddev = dev_get_drvdata(dev);
642         struct tilcdc_drm_private *priv = ddev->dev_private;
643         unsigned i, n = 0;
644
645         /* Select default pin state */
646         pinctrl_pm_select_default_state(dev);
647
648         if (priv->ctx_valid == true) {
649                 /* Restore register state: */
650                 for (i = 0; i < ARRAY_SIZE(registers); i++)
651                         if (registers[i].save &&
652                             (priv->rev >= registers[i].rev))
653                                 tilcdc_write(ddev, registers[i].reg,
654                                              priv->saved_register[n++]);
655         }
656
657         tilcdc_crtc_dpms(priv->crtc, priv->saved_dpms_state);
658
659         drm_kms_helper_poll_enable(ddev);
660
661         return 0;
662 }
663 #endif
664
665 static const struct dev_pm_ops tilcdc_pm_ops = {
666         SET_SYSTEM_SLEEP_PM_OPS(tilcdc_pm_suspend, tilcdc_pm_resume)
667 };
668
669 /*
670  * Platform driver:
671  */
672
673 static int tilcdc_bind(struct device *dev)
674 {
675         return drm_platform_init(&tilcdc_driver, to_platform_device(dev));
676 }
677
678 static void tilcdc_unbind(struct device *dev)
679 {
680         struct drm_device *ddev = dev_get_drvdata(dev);
681
682         /* Check if a subcomponent has already triggered the unloading. */
683         if (!ddev->dev_private)
684                 return;
685
686         drm_put_dev(dev_get_drvdata(dev));
687 }
688
689 static const struct component_master_ops tilcdc_comp_ops = {
690         .bind = tilcdc_bind,
691         .unbind = tilcdc_unbind,
692 };
693
694 static int tilcdc_pdev_probe(struct platform_device *pdev)
695 {
696         struct component_match *match = NULL;
697         int ret;
698
699         /* bail out early if no DT data: */
700         if (!pdev->dev.of_node) {
701                 dev_err(&pdev->dev, "device-tree data is missing\n");
702                 return -ENXIO;
703         }
704
705         ret = tilcdc_get_external_components(&pdev->dev, &match);
706         if (ret < 0)
707                 return ret;
708         else if (ret == 0)
709                 return drm_platform_init(&tilcdc_driver, pdev);
710         else
711                 return component_master_add_with_match(&pdev->dev,
712                                                        &tilcdc_comp_ops,
713                                                        match);
714 }
715
716 static int tilcdc_pdev_remove(struct platform_device *pdev)
717 {
718         int ret;
719
720         ret = tilcdc_get_external_components(&pdev->dev, NULL);
721         if (ret < 0)
722                 return ret;
723         else if (ret == 0)
724                 drm_put_dev(platform_get_drvdata(pdev));
725         else
726                 component_master_del(&pdev->dev, &tilcdc_comp_ops);
727
728         return 0;
729 }
730
731 static struct of_device_id tilcdc_of_match[] = {
732                 { .compatible = "ti,am33xx-tilcdc", },
733                 { },
734 };
735 MODULE_DEVICE_TABLE(of, tilcdc_of_match);
736
737 static struct platform_driver tilcdc_platform_driver = {
738         .probe      = tilcdc_pdev_probe,
739         .remove     = tilcdc_pdev_remove,
740         .driver     = {
741                 .name   = "tilcdc",
742                 .pm     = &tilcdc_pm_ops,
743                 .of_match_table = tilcdc_of_match,
744         },
745 };
746
747 static int __init tilcdc_drm_init(void)
748 {
749         DBG("init");
750         tilcdc_tfp410_init();
751         tilcdc_panel_init();
752         return platform_driver_register(&tilcdc_platform_driver);
753 }
754
755 static void __exit tilcdc_drm_fini(void)
756 {
757         DBG("fini");
758         platform_driver_unregister(&tilcdc_platform_driver);
759         tilcdc_panel_fini();
760         tilcdc_tfp410_fini();
761 }
762
763 module_init(tilcdc_drm_init);
764 module_exit(tilcdc_drm_fini);
765
766 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
767 MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
768 MODULE_LICENSE("GPL");