Version:  2.0.40 2.2.26 2.4.37 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10

Linux/drivers/gpu/drm/shmobile/shmob_drm_drv.c

  1 /*
  2  * shmob_drm_drv.c  --  SH Mobile DRM driver
  3  *
  4  * Copyright (C) 2012 Renesas Electronics Corporation
  5  *
  6  * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7  *
  8  * This program is free software; you can redistribute it and/or modify
  9  * it under the terms of the GNU General Public License as published by
 10  * the Free Software Foundation; either version 2 of the License, or
 11  * (at your option) any later version.
 12  */
 13 
 14 #include <linux/clk.h>
 15 #include <linux/io.h>
 16 #include <linux/mm.h>
 17 #include <linux/module.h>
 18 #include <linux/platform_device.h>
 19 #include <linux/pm.h>
 20 #include <linux/slab.h>
 21 
 22 #include <drm/drmP.h>
 23 #include <drm/drm_crtc_helper.h>
 24 #include <drm/drm_gem_cma_helper.h>
 25 
 26 #include "shmob_drm_crtc.h"
 27 #include "shmob_drm_drv.h"
 28 #include "shmob_drm_kms.h"
 29 #include "shmob_drm_plane.h"
 30 #include "shmob_drm_regs.h"
 31 
 32 /* -----------------------------------------------------------------------------
 33  * Hardware initialization
 34  */
 35 
 36 static int shmob_drm_init_interface(struct shmob_drm_device *sdev)
 37 {
 38         static const u32 ldmt1r[] = {
 39                 [SHMOB_DRM_IFACE_RGB8] = LDMT1R_MIFTYP_RGB8,
 40                 [SHMOB_DRM_IFACE_RGB9] = LDMT1R_MIFTYP_RGB9,
 41                 [SHMOB_DRM_IFACE_RGB12A] = LDMT1R_MIFTYP_RGB12A,
 42                 [SHMOB_DRM_IFACE_RGB12B] = LDMT1R_MIFTYP_RGB12B,
 43                 [SHMOB_DRM_IFACE_RGB16] = LDMT1R_MIFTYP_RGB16,
 44                 [SHMOB_DRM_IFACE_RGB18] = LDMT1R_MIFTYP_RGB18,
 45                 [SHMOB_DRM_IFACE_RGB24] = LDMT1R_MIFTYP_RGB24,
 46                 [SHMOB_DRM_IFACE_YUV422] = LDMT1R_MIFTYP_YCBCR,
 47                 [SHMOB_DRM_IFACE_SYS8A] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8A,
 48                 [SHMOB_DRM_IFACE_SYS8B] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8B,
 49                 [SHMOB_DRM_IFACE_SYS8C] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8C,
 50                 [SHMOB_DRM_IFACE_SYS8D] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8D,
 51                 [SHMOB_DRM_IFACE_SYS9] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS9,
 52                 [SHMOB_DRM_IFACE_SYS12] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS12,
 53                 [SHMOB_DRM_IFACE_SYS16A] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16A,
 54                 [SHMOB_DRM_IFACE_SYS16B] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16B,
 55                 [SHMOB_DRM_IFACE_SYS16C] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16C,
 56                 [SHMOB_DRM_IFACE_SYS18] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS18,
 57                 [SHMOB_DRM_IFACE_SYS24] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS24,
 58         };
 59 
 60         if (sdev->pdata->iface.interface >= ARRAY_SIZE(ldmt1r)) {
 61                 dev_err(sdev->dev, "invalid interface type %u\n",
 62                         sdev->pdata->iface.interface);
 63                 return -EINVAL;
 64         }
 65 
 66         sdev->ldmt1r = ldmt1r[sdev->pdata->iface.interface];
 67         return 0;
 68 }
 69 
 70 static int shmob_drm_setup_clocks(struct shmob_drm_device *sdev,
 71                                             enum shmob_drm_clk_source clksrc)
 72 {
 73         struct clk *clk;
 74         char *clkname;
 75 
 76         switch (clksrc) {
 77         case SHMOB_DRM_CLK_BUS:
 78                 clkname = "bus_clk";
 79                 sdev->lddckr = LDDCKR_ICKSEL_BUS;
 80                 break;
 81         case SHMOB_DRM_CLK_PERIPHERAL:
 82                 clkname = "peripheral_clk";
 83                 sdev->lddckr = LDDCKR_ICKSEL_MIPI;
 84                 break;
 85         case SHMOB_DRM_CLK_EXTERNAL:
 86                 clkname = NULL;
 87                 sdev->lddckr = LDDCKR_ICKSEL_HDMI;
 88                 break;
 89         default:
 90                 return -EINVAL;
 91         }
 92 
 93         clk = devm_clk_get(sdev->dev, clkname);
 94         if (IS_ERR(clk)) {
 95                 dev_err(sdev->dev, "cannot get dot clock %s\n", clkname);
 96                 return PTR_ERR(clk);
 97         }
 98 
 99         sdev->clock = clk;
100         return 0;
101 }
102 
103 /* -----------------------------------------------------------------------------
104  * DRM operations
105  */
106 
107 static int shmob_drm_unload(struct drm_device *dev)
108 {
109         drm_kms_helper_poll_fini(dev);
110         drm_mode_config_cleanup(dev);
111         drm_vblank_cleanup(dev);
112         drm_irq_uninstall(dev);
113 
114         dev->dev_private = NULL;
115 
116         return 0;
117 }
118 
119 static int shmob_drm_load(struct drm_device *dev, unsigned long flags)
120 {
121         struct shmob_drm_platform_data *pdata = dev->dev->platform_data;
122         struct platform_device *pdev = dev->platformdev;
123         struct shmob_drm_device *sdev;
124         struct resource *res;
125         unsigned int i;
126         int ret;
127 
128         if (pdata == NULL) {
129                 dev_err(dev->dev, "no platform data\n");
130                 return -EINVAL;
131         }
132 
133         sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
134         if (sdev == NULL) {
135                 dev_err(dev->dev, "failed to allocate private data\n");
136                 return -ENOMEM;
137         }
138 
139         sdev->dev = &pdev->dev;
140         sdev->pdata = pdata;
141         spin_lock_init(&sdev->irq_lock);
142 
143         sdev->ddev = dev;
144         dev->dev_private = sdev;
145 
146         /* I/O resources and clocks */
147         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
148         if (res == NULL) {
149                 dev_err(&pdev->dev, "failed to get memory resource\n");
150                 return -EINVAL;
151         }
152 
153         sdev->mmio = devm_ioremap_nocache(&pdev->dev, res->start,
154                                           resource_size(res));
155         if (sdev->mmio == NULL) {
156                 dev_err(&pdev->dev, "failed to remap memory resource\n");
157                 return -ENOMEM;
158         }
159 
160         ret = shmob_drm_setup_clocks(sdev, pdata->clk_source);
161         if (ret < 0)
162                 return ret;
163 
164         ret = shmob_drm_init_interface(sdev);
165         if (ret < 0)
166                 return ret;
167 
168         ret = shmob_drm_modeset_init(sdev);
169         if (ret < 0) {
170                 dev_err(&pdev->dev, "failed to initialize mode setting\n");
171                 return ret;
172         }
173 
174         for (i = 0; i < 4; ++i) {
175                 ret = shmob_drm_plane_create(sdev, i);
176                 if (ret < 0) {
177                         dev_err(&pdev->dev, "failed to create plane %u\n", i);
178                         goto done;
179                 }
180         }
181 
182         ret = drm_vblank_init(dev, 1);
183         if (ret < 0) {
184                 dev_err(&pdev->dev, "failed to initialize vblank\n");
185                 goto done;
186         }
187 
188         ret = drm_irq_install(dev, platform_get_irq(dev->platformdev, 0));
189         if (ret < 0) {
190                 dev_err(&pdev->dev, "failed to install