1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/ 4 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com> 5 */ 6 7 #include <linux/console.h> 8 #include <linux/of.h> 9 #include <linux/module.h> 10 #include <linux/pm_runtime.h> 11 #include <linux/aperture.h> 12 13 #include <drm/clients/drm_client_setup.h> 14 #include <drm/drm_atomic.h> 15 #include <drm/drm_atomic_helper.h> 16 #include <drm/drm_crtc.h> 17 #include <drm/drm_drv.h> 18 #include <drm/drm_fbdev_dma.h> 19 #include <drm/drm_gem_dma_helper.h> 20 #include <drm/drm_managed.h> 21 #include <drm/drm_module.h> 22 #include <drm/drm_probe_helper.h> 23 24 #include "tidss_dispc.h" 25 #include "tidss_drv.h" 26 #include "tidss_kms.h" 27 #include "tidss_irq.h" 28 #include "tidss_oldi.h" 29 30 /* Power management */ 31 32 int tidss_runtime_get(struct tidss_device *tidss) 33 { 34 int r; 35 36 dev_dbg(tidss->dev, "%s\n", __func__); 37 38 r = pm_runtime_resume_and_get(tidss->dev); 39 WARN_ON(r < 0); 40 return r; 41 } 42 43 void tidss_runtime_put(struct tidss_device *tidss) 44 { 45 int r; 46 47 dev_dbg(tidss->dev, "%s\n", __func__); 48 49 pm_runtime_mark_last_busy(tidss->dev); 50 51 r = pm_runtime_put_autosuspend(tidss->dev); 52 WARN_ON(r < 0); 53 } 54 55 static int __maybe_unused tidss_pm_runtime_suspend(struct device *dev) 56 { 57 struct tidss_device *tidss = dev_get_drvdata(dev); 58 59 dev_dbg(dev, "%s\n", __func__); 60 61 return dispc_runtime_suspend(tidss->dispc); 62 } 63 64 static int __maybe_unused tidss_pm_runtime_resume(struct device *dev) 65 { 66 struct tidss_device *tidss = dev_get_drvdata(dev); 67 int r; 68 69 dev_dbg(dev, "%s\n", __func__); 70 71 r = dispc_runtime_resume(tidss->dispc); 72 if (r) 73 return r; 74 75 return 0; 76 } 77 78 static int __maybe_unused tidss_suspend(struct device *dev) 79 { 80 struct tidss_device *tidss = dev_get_drvdata(dev); 81 82 dev_dbg(dev, "%s\n", __func__); 83 84 return drm_mode_config_helper_suspend(&tidss->ddev); 85 } 86 87 static int __maybe_unused tidss_resume(struct device *dev) 88 { 89 struct tidss_device *tidss = dev_get_drvdata(dev); 90 91 dev_dbg(dev, "%s\n", __func__); 92 93 return drm_mode_config_helper_resume(&tidss->ddev); 94 } 95 96 static __maybe_unused const struct dev_pm_ops tidss_pm_ops = { 97 SET_SYSTEM_SLEEP_PM_OPS(tidss_suspend, tidss_resume) 98 SET_RUNTIME_PM_OPS(tidss_pm_runtime_suspend, tidss_pm_runtime_resume, NULL) 99 }; 100 101 /* DRM device Information */ 102 103 static void tidss_release(struct drm_device *ddev) 104 { 105 drm_kms_helper_poll_fini(ddev); 106 } 107 108 DEFINE_DRM_GEM_DMA_FOPS(tidss_fops); 109 110 static const struct drm_driver tidss_driver = { 111 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 112 .fops = &tidss_fops, 113 .release = tidss_release, 114 DRM_GEM_DMA_DRIVER_OPS_VMAP, 115 DRM_FBDEV_DMA_DRIVER_OPS, 116 .name = "tidss", 117 .desc = "TI Keystone DSS", 118 .major = 1, 119 .minor = 0, 120 }; 121 122 static int tidss_probe(struct platform_device *pdev) 123 { 124 struct device *dev = &pdev->dev; 125 struct tidss_device *tidss; 126 struct drm_device *ddev; 127 int ret; 128 int irq; 129 130 dev_dbg(dev, "%s\n", __func__); 131 132 tidss = devm_drm_dev_alloc(&pdev->dev, &tidss_driver, 133 struct tidss_device, ddev); 134 if (IS_ERR(tidss)) 135 return PTR_ERR(tidss); 136 137 ddev = &tidss->ddev; 138 139 tidss->dev = dev; 140 tidss->feat = of_device_get_match_data(dev); 141 142 platform_set_drvdata(pdev, tidss); 143 144 spin_lock_init(&tidss->irq_lock); 145 146 ret = dispc_init(tidss); 147 if (ret) { 148 dev_err(dev, "failed to initialize dispc: %d\n", ret); 149 return ret; 150 } 151 152 ret = tidss_oldi_init(tidss); 153 if (ret) 154 return dev_err_probe(dev, ret, "failed to init OLDI\n"); 155 156 pm_runtime_enable(dev); 157 158 pm_runtime_set_autosuspend_delay(dev, 1000); 159 pm_runtime_use_autosuspend(dev); 160 161 #ifndef CONFIG_PM 162 /* If we don't have PM, we need to call resume manually */ 163 dispc_runtime_resume(tidss->dispc); 164 #endif 165 166 ret = tidss_modeset_init(tidss); 167 if (ret < 0) { 168 if (ret != -EPROBE_DEFER) 169 dev_err(dev, "failed to init DRM/KMS (%d)\n", ret); 170 goto err_runtime_suspend; 171 } 172 173 irq = platform_get_irq(pdev, 0); 174 if (irq < 0) { 175 ret = irq; 176 goto err_runtime_suspend; 177 } 178 tidss->irq = irq; 179 180 ret = tidss_irq_install(ddev, irq); 181 if (ret) { 182 dev_err(dev, "tidss_irq_install failed: %d\n", ret); 183 goto err_runtime_suspend; 184 } 185 186 drm_kms_helper_poll_init(ddev); 187 188 drm_mode_config_reset(ddev); 189 190 ret = drm_dev_register(ddev, 0); 191 if (ret) { 192 dev_err(dev, "failed to register DRM device\n"); 193 goto err_irq_uninstall; 194 } 195 196 /* Remove possible early fb before setting up the fbdev */ 197 ret = aperture_remove_all_conflicting_devices(tidss_driver.name); 198 if (ret) 199 goto err_drm_dev_unreg; 200 201 drm_client_setup(ddev, NULL); 202 203 dev_dbg(dev, "%s done\n", __func__); 204 205 return 0; 206 207 err_drm_dev_unreg: 208 drm_dev_unregister(ddev); 209 210 err_irq_uninstall: 211 tidss_irq_uninstall(ddev); 212 213 err_runtime_suspend: 214 #ifndef CONFIG_PM 215 dispc_runtime_suspend(tidss->dispc); 216 #endif 217 pm_runtime_dont_use_autosuspend(dev); 218 pm_runtime_disable(dev); 219 220 tidss_oldi_deinit(tidss); 221 222 return ret; 223 } 224 225 static void tidss_remove(struct platform_device *pdev) 226 { 227 struct device *dev = &pdev->dev; 228 struct tidss_device *tidss = platform_get_drvdata(pdev); 229 struct drm_device *ddev = &tidss->ddev; 230 231 dev_dbg(dev, "%s\n", __func__); 232 233 drm_dev_unregister(ddev); 234 235 drm_atomic_helper_shutdown(ddev); 236 237 tidss_irq_uninstall(ddev); 238 239 #ifndef CONFIG_PM 240 /* If we don't have PM, we need to call suspend manually */ 241 dispc_runtime_suspend(tidss->dispc); 242 #endif 243 pm_runtime_dont_use_autosuspend(dev); 244 pm_runtime_disable(dev); 245 246 tidss_oldi_deinit(tidss); 247 248 /* devm allocated dispc goes away with the dev so mark it NULL */ 249 dispc_remove(tidss); 250 251 dev_dbg(dev, "%s done\n", __func__); 252 } 253 254 static void tidss_shutdown(struct platform_device *pdev) 255 { 256 drm_atomic_helper_shutdown(platform_get_drvdata(pdev)); 257 } 258 259 static const struct of_device_id tidss_of_table[] = { 260 { .compatible = "ti,k2g-dss", .data = &dispc_k2g_feats, }, 261 { .compatible = "ti,am625-dss", .data = &dispc_am625_feats, }, 262 { .compatible = "ti,am62a7-dss", .data = &dispc_am62a7_feats, }, 263 { .compatible = "ti,am62l-dss", .data = &dispc_am62l_feats, }, 264 { .compatible = "ti,am65x-dss", .data = &dispc_am65x_feats, }, 265 { .compatible = "ti,j721e-dss", .data = &dispc_j721e_feats, }, 266 { } 267 }; 268 269 MODULE_DEVICE_TABLE(of, tidss_of_table); 270 271 static struct platform_driver tidss_platform_driver = { 272 .probe = tidss_probe, 273 .remove = tidss_remove, 274 .shutdown = tidss_shutdown, 275 .driver = { 276 .name = "tidss", 277 .pm = pm_ptr(&tidss_pm_ops), 278 .of_match_table = tidss_of_table, 279 .suppress_bind_attrs = true, 280 }, 281 }; 282 283 drm_module_platform_driver(tidss_platform_driver); 284 285 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>"); 286 MODULE_DESCRIPTION("TI Keystone DSS Driver"); 287 MODULE_LICENSE("GPL v2"); 288