1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * shmob_drm_drv.c -- SH Mobile DRM driver 4 * 5 * Copyright (C) 2012 Renesas Electronics Corporation 6 * 7 * Laurent Pinchart (laurent.pinchart@ideasonboard.com) 8 */ 9 10 #include <linux/clk.h> 11 #include <linux/io.h> 12 #include <linux/mm.h> 13 #include <linux/module.h> 14 #include <linux/of.h> 15 #include <linux/platform_device.h> 16 #include <linux/pm.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/slab.h> 19 20 #include <drm/drm_atomic_helper.h> 21 #include <drm/drm_client_setup.h> 22 #include <drm/drm_drv.h> 23 #include <drm/drm_fbdev_dma.h> 24 #include <drm/drm_fourcc.h> 25 #include <drm/drm_gem_dma_helper.h> 26 #include <drm/drm_modeset_helper.h> 27 #include <drm/drm_module.h> 28 #include <drm/drm_probe_helper.h> 29 #include <drm/drm_vblank.h> 30 31 #include "shmob_drm_drv.h" 32 #include "shmob_drm_kms.h" 33 #include "shmob_drm_plane.h" 34 #include "shmob_drm_regs.h" 35 36 /* ----------------------------------------------------------------------------- 37 * Hardware initialization 38 */ 39 40 static int shmob_drm_setup_clocks(struct shmob_drm_device *sdev, 41 enum shmob_drm_clk_source clksrc) 42 { 43 struct clk *clk; 44 char *clkname; 45 46 switch (clksrc) { 47 case SHMOB_DRM_CLK_BUS: 48 clkname = "fck"; 49 sdev->lddckr = LDDCKR_ICKSEL_BUS; 50 break; 51 case SHMOB_DRM_CLK_PERIPHERAL: 52 clkname = "media"; 53 sdev->lddckr = LDDCKR_ICKSEL_MIPI; 54 break; 55 case SHMOB_DRM_CLK_EXTERNAL: 56 clkname = "lclk"; 57 sdev->lddckr = LDDCKR_ICKSEL_HDMI; 58 break; 59 default: 60 return -EINVAL; 61 } 62 63 clk = devm_clk_get(sdev->dev, clkname); 64 if (IS_ERR(clk)) { 65 dev_err(sdev->dev, "cannot get dot clock %s\n", clkname); 66 return PTR_ERR(clk); 67 } 68 69 sdev->clock = clk; 70 return 0; 71 } 72 73 /* ----------------------------------------------------------------------------- 74 * DRM operations 75 */ 76 77 static irqreturn_t shmob_drm_irq(int irq, void *arg) 78 { 79 struct drm_device *dev = arg; 80 struct shmob_drm_device *sdev = to_shmob_device(dev); 81 unsigned long flags; 82 u32 status; 83 84 /* Acknowledge interrupts. Putting interrupt enable and interrupt flag 85 * bits in the same register is really brain-dead design and requires 86 * taking a spinlock. 87 */ 88 spin_lock_irqsave(&sdev->irq_lock, flags); 89 status = lcdc_read(sdev, LDINTR); 90 lcdc_write(sdev, LDINTR, status ^ LDINTR_STATUS_MASK); 91 spin_unlock_irqrestore(&sdev->irq_lock, flags); 92 93 if (status & LDINTR_VES) { 94 drm_crtc_handle_vblank(&sdev->crtc.base); 95 shmob_drm_crtc_finish_page_flip(&sdev->crtc); 96 } 97 98 return IRQ_HANDLED; 99 } 100 101 DEFINE_DRM_GEM_DMA_FOPS(shmob_drm_fops); 102 103 static const struct drm_driver shmob_drm_driver = { 104 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 105 DRM_GEM_DMA_DRIVER_OPS, 106 DRM_FBDEV_DMA_DRIVER_OPS, 107 .fops = &shmob_drm_fops, 108 .name = "shmob-drm", 109 .desc = "Renesas SH Mobile DRM", 110 .date = "20120424", 111 .major = 1, 112 .minor = 0, 113 }; 114 115 /* ----------------------------------------------------------------------------- 116 * Power management 117 */ 118 119 static int shmob_drm_pm_suspend(struct device *dev) 120 { 121 struct shmob_drm_device *sdev = dev_get_drvdata(dev); 122 123 return drm_mode_config_helper_suspend(&sdev->ddev); 124 } 125 126 static int shmob_drm_pm_resume(struct device *dev) 127 { 128 struct shmob_drm_device *sdev = dev_get_drvdata(dev); 129 130 return drm_mode_config_helper_resume(&sdev->ddev); 131 } 132 133 static int shmob_drm_pm_runtime_suspend(struct device *dev) 134 { 135 struct shmob_drm_device *sdev = dev_get_drvdata(dev); 136 137 if (sdev->clock) 138 clk_disable_unprepare(sdev->clock); 139 140 return 0; 141 } 142 143 static int shmob_drm_pm_runtime_resume(struct device *dev) 144 { 145 struct shmob_drm_device *sdev = dev_get_drvdata(dev); 146 int ret; 147 148 if (sdev->clock) { 149 ret = clk_prepare_enable(sdev->clock); 150 if (ret < 0) 151 return ret; 152 } 153 154 return 0; 155 } 156 157 static const struct dev_pm_ops shmob_drm_pm_ops = { 158 SYSTEM_SLEEP_PM_OPS(shmob_drm_pm_suspend, shmob_drm_pm_resume) 159 RUNTIME_PM_OPS(shmob_drm_pm_runtime_suspend, 160 shmob_drm_pm_runtime_resume, NULL) 161 }; 162 163 /* ----------------------------------------------------------------------------- 164 * Platform driver 165 */ 166 167 static void shmob_drm_remove(struct platform_device *pdev) 168 { 169 struct shmob_drm_device *sdev = platform_get_drvdata(pdev); 170 struct drm_device *ddev = &sdev->ddev; 171 172 drm_dev_unregister(ddev); 173 drm_atomic_helper_shutdown(ddev); 174 drm_kms_helper_poll_fini(ddev); 175 } 176 177 static void shmob_drm_shutdown(struct platform_device *pdev) 178 { 179 struct shmob_drm_device *sdev = platform_get_drvdata(pdev); 180 181 drm_atomic_helper_shutdown(&sdev->ddev); 182 } 183 184 static int shmob_drm_probe(struct platform_device *pdev) 185 { 186 struct shmob_drm_platform_data *pdata = pdev->dev.platform_data; 187 const struct shmob_drm_config *config; 188 struct shmob_drm_device *sdev; 189 struct drm_device *ddev; 190 int ret; 191 192 config = of_device_get_match_data(&pdev->dev); 193 if (!config && !pdata) { 194 dev_err(&pdev->dev, "no platform data\n"); 195 return -EINVAL; 196 } 197 198 /* 199 * Allocate and initialize the DRM device, driver private data, I/O 200 * resources and clocks. 201 */ 202 sdev = devm_drm_dev_alloc(&pdev->dev, &shmob_drm_driver, 203 struct shmob_drm_device, ddev); 204 if (IS_ERR(sdev)) 205 return PTR_ERR(sdev); 206 207 ddev = &sdev->ddev; 208 sdev->dev = &pdev->dev; 209 if (config) { 210 sdev->config = *config; 211 } else { 212 sdev->pdata = pdata; 213 sdev->config.clk_source = pdata->clk_source; 214 sdev->config.clk_div = pdata->iface.clk_div; 215 } 216 spin_lock_init(&sdev->irq_lock); 217 218 platform_set_drvdata(pdev, sdev); 219 220 sdev->mmio = devm_platform_ioremap_resource(pdev, 0); 221 if (IS_ERR(sdev->mmio)) 222 return PTR_ERR(sdev->mmio); 223 224 ret = shmob_drm_setup_clocks(sdev, sdev->config.clk_source); 225 if (ret < 0) 226 return ret; 227 228 ret = devm_pm_runtime_enable(&pdev->dev); 229 if (ret) 230 return ret; 231 232 ret = drm_vblank_init(ddev, 1); 233 if (ret < 0) { 234 dev_err(&pdev->dev, "failed to initialize vblank\n"); 235 return ret; 236 } 237 238 ret = shmob_drm_modeset_init(sdev); 239 if (ret < 0) 240 return dev_err_probe(&pdev->dev, ret, 241 "failed to initialize mode setting\n"); 242 243 ret = platform_get_irq(pdev, 0); 244 if (ret < 0) 245 goto err_modeset_cleanup; 246 sdev->irq = ret; 247 248 ret = devm_request_irq(&pdev->dev, sdev->irq, shmob_drm_irq, 0, 249 ddev->driver->name, ddev); 250 if (ret < 0) { 251 dev_err(&pdev->dev, "failed to install IRQ handler\n"); 252 goto err_modeset_cleanup; 253 } 254 255 /* 256 * Register the DRM device with the core and the connectors with 257 * sysfs. 258 */ 259 ret = drm_dev_register(ddev, 0); 260 if (ret < 0) 261 goto err_modeset_cleanup; 262 263 drm_client_setup_with_fourcc(ddev, DRM_FORMAT_RGB565); 264 265 return 0; 266 267 err_modeset_cleanup: 268 drm_kms_helper_poll_fini(ddev); 269 return ret; 270 } 271 272 static const struct shmob_drm_config shmob_arm_config = { 273 .clk_source = SHMOB_DRM_CLK_BUS, 274 .clk_div = 5, 275 }; 276 277 static const struct of_device_id shmob_drm_of_table[] __maybe_unused = { 278 { .compatible = "renesas,r8a7740-lcdc", .data = &shmob_arm_config, }, 279 { .compatible = "renesas,sh73a0-lcdc", .data = &shmob_arm_config, }, 280 { /* sentinel */ } 281 }; 282 283 static struct platform_driver shmob_drm_platform_driver = { 284 .probe = shmob_drm_probe, 285 .remove = shmob_drm_remove, 286 .shutdown = shmob_drm_shutdown, 287 .driver = { 288 .name = "shmob-drm", 289 .of_match_table = of_match_ptr(shmob_drm_of_table), 290 .pm = &shmob_drm_pm_ops, 291 }, 292 }; 293 294 drm_module_platform_driver(shmob_drm_platform_driver); 295 296 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); 297 MODULE_DESCRIPTION("Renesas SH Mobile DRM Driver"); 298 MODULE_LICENSE("GPL"); 299