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