Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * rcar_du_drv.c -- R-Car Display Unit DRM driver |
| 3 | * |
| 4 | * Copyright (C) 2013 Renesas Corporation |
| 5 | * |
| 6 | * Contact: 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 "rcar_du_crtc.h" |
| 27 | #include "rcar_du_drv.h" |
| 28 | #include "rcar_du_kms.h" |
| 29 | #include "rcar_du_regs.h" |
| 30 | |
| 31 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 32 | * DRM operations |
| 33 | */ |
| 34 | |
| 35 | static int rcar_du_unload(struct drm_device *dev) |
| 36 | { |
| 37 | drm_kms_helper_poll_fini(dev); |
| 38 | drm_mode_config_cleanup(dev); |
| 39 | drm_vblank_cleanup(dev); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 40 | |
Laurent Pinchart | f66ee30 | 2013-06-14 14:15:01 +0200 | [diff] [blame] | 41 | dev->irq_enabled = 0; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 42 | dev->dev_private = NULL; |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int rcar_du_load(struct drm_device *dev, unsigned long flags) |
| 48 | { |
| 49 | struct platform_device *pdev = dev->platformdev; |
| 50 | struct rcar_du_platform_data *pdata = pdev->dev.platform_data; |
| 51 | struct rcar_du_device *rcdu; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 52 | struct resource *mem; |
| 53 | int ret; |
| 54 | |
| 55 | if (pdata == NULL) { |
| 56 | dev_err(dev->dev, "no platform data\n"); |
| 57 | return -ENODEV; |
| 58 | } |
| 59 | |
| 60 | rcdu = devm_kzalloc(&pdev->dev, sizeof(*rcdu), GFP_KERNEL); |
| 61 | if (rcdu == NULL) { |
| 62 | dev_err(dev->dev, "failed to allocate private data\n"); |
| 63 | return -ENOMEM; |
| 64 | } |
| 65 | |
| 66 | rcdu->dev = &pdev->dev; |
| 67 | rcdu->pdata = pdata; |
Laurent Pinchart | 481d342 | 2013-06-14 13:38:33 +0200 | [diff] [blame] | 68 | rcdu->info = (struct rcar_du_device_info *)pdev->id_entry->driver_data; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 69 | rcdu->ddev = dev; |
| 70 | dev->dev_private = rcdu; |
| 71 | |
Laurent Pinchart | f66ee30 | 2013-06-14 14:15:01 +0200 | [diff] [blame] | 72 | /* I/O resources */ |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 73 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Laurent Pinchart | d5b6dcc | 2013-06-17 02:29:07 +0200 | [diff] [blame] | 74 | rcdu->mmio = devm_ioremap_resource(&pdev->dev, mem); |
| 75 | if (IS_ERR(rcdu->mmio)) |
| 76 | return PTR_ERR(rcdu->mmio); |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 77 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 78 | /* DRM/KMS objects */ |
| 79 | ret = rcar_du_modeset_init(rcdu); |
| 80 | if (ret < 0) { |
| 81 | dev_err(&pdev->dev, "failed to initialize DRM/KMS\n"); |
| 82 | goto done; |
| 83 | } |
| 84 | |
Laurent Pinchart | f66ee30 | 2013-06-14 14:15:01 +0200 | [diff] [blame] | 85 | /* vblank handling */ |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 86 | ret = drm_vblank_init(dev, (1 << rcdu->num_crtcs) - 1); |
| 87 | if (ret < 0) { |
| 88 | dev_err(&pdev->dev, "failed to initialize vblank\n"); |
| 89 | goto done; |
| 90 | } |
| 91 | |
Laurent Pinchart | f66ee30 | 2013-06-14 14:15:01 +0200 | [diff] [blame] | 92 | dev->irq_enabled = 1; |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 93 | |
| 94 | platform_set_drvdata(pdev, rcdu); |
| 95 | |
| 96 | done: |
| 97 | if (ret) |
| 98 | rcar_du_unload(dev); |
| 99 | |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | static void rcar_du_preclose(struct drm_device *dev, struct drm_file *file) |
| 104 | { |
| 105 | struct rcar_du_device *rcdu = dev->dev_private; |
| 106 | unsigned int i; |
| 107 | |
Laurent Pinchart | 990d07a | 2013-06-16 22:22:23 +0200 | [diff] [blame] | 108 | for (i = 0; i < rcdu->num_crtcs; ++i) |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 109 | rcar_du_crtc_cancel_page_flip(&rcdu->crtcs[i], file); |
| 110 | } |
| 111 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 112 | static int rcar_du_enable_vblank(struct drm_device *dev, int crtc) |
| 113 | { |
| 114 | struct rcar_du_device *rcdu = dev->dev_private; |
| 115 | |
| 116 | rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], true); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static void rcar_du_disable_vblank(struct drm_device *dev, int crtc) |
| 122 | { |
| 123 | struct rcar_du_device *rcdu = dev->dev_private; |
| 124 | |
| 125 | rcar_du_crtc_enable_vblank(&rcdu->crtcs[crtc], false); |
| 126 | } |
| 127 | |
| 128 | static const struct file_operations rcar_du_fops = { |
| 129 | .owner = THIS_MODULE, |
| 130 | .open = drm_open, |
| 131 | .release = drm_release, |
| 132 | .unlocked_ioctl = drm_ioctl, |
| 133 | #ifdef CONFIG_COMPAT |
| 134 | .compat_ioctl = drm_compat_ioctl, |
| 135 | #endif |
| 136 | .poll = drm_poll, |
| 137 | .read = drm_read, |
| 138 | .fasync = drm_fasync, |
| 139 | .llseek = no_llseek, |
| 140 | .mmap = drm_gem_cma_mmap, |
| 141 | }; |
| 142 | |
| 143 | static struct drm_driver rcar_du_driver = { |
Laurent Pinchart | f66ee30 | 2013-06-14 14:15:01 +0200 | [diff] [blame] | 144 | .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 145 | .load = rcar_du_load, |
| 146 | .unload = rcar_du_unload, |
| 147 | .preclose = rcar_du_preclose, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 148 | .get_vblank_counter = drm_vblank_count, |
| 149 | .enable_vblank = rcar_du_enable_vblank, |
| 150 | .disable_vblank = rcar_du_disable_vblank, |
| 151 | .gem_free_object = drm_gem_cma_free_object, |
| 152 | .gem_vm_ops = &drm_gem_cma_vm_ops, |
| 153 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, |
| 154 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, |
Laurent Pinchart | ffb4040 | 2013-07-10 15:23:35 +0200 | [diff] [blame] | 155 | .gem_prime_import = drm_gem_prime_import, |
| 156 | .gem_prime_export = drm_gem_prime_export, |
| 157 | .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, |
| 158 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, |
| 159 | .gem_prime_vmap = drm_gem_cma_prime_vmap, |
| 160 | .gem_prime_vunmap = drm_gem_cma_prime_vunmap, |
| 161 | .gem_prime_mmap = drm_gem_cma_prime_mmap, |
Laurent Pinchart | 59e3264 | 2013-07-04 20:05:51 +0200 | [diff] [blame] | 162 | .dumb_create = rcar_du_dumb_create, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 163 | .dumb_map_offset = drm_gem_cma_dumb_map_offset, |
| 164 | .dumb_destroy = drm_gem_cma_dumb_destroy, |
| 165 | .fops = &rcar_du_fops, |
| 166 | .name = "rcar-du", |
| 167 | .desc = "Renesas R-Car Display Unit", |
| 168 | .date = "20130110", |
| 169 | .major = 1, |
| 170 | .minor = 0, |
| 171 | }; |
| 172 | |
| 173 | /* ----------------------------------------------------------------------------- |
| 174 | * Power management |
| 175 | */ |
| 176 | |
| 177 | #if CONFIG_PM_SLEEP |
| 178 | static int rcar_du_pm_suspend(struct device *dev) |
| 179 | { |
| 180 | struct rcar_du_device *rcdu = dev_get_drvdata(dev); |
| 181 | |
| 182 | drm_kms_helper_poll_disable(rcdu->ddev); |
| 183 | /* TODO Suspend the CRTC */ |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int rcar_du_pm_resume(struct device *dev) |
| 189 | { |
| 190 | struct rcar_du_device *rcdu = dev_get_drvdata(dev); |
| 191 | |
| 192 | /* TODO Resume the CRTC */ |
| 193 | |
| 194 | drm_kms_helper_poll_enable(rcdu->ddev); |
| 195 | return 0; |
| 196 | } |
| 197 | #endif |
| 198 | |
| 199 | static const struct dev_pm_ops rcar_du_pm_ops = { |
| 200 | SET_SYSTEM_SLEEP_PM_OPS(rcar_du_pm_suspend, rcar_du_pm_resume) |
| 201 | }; |
| 202 | |
| 203 | /* ----------------------------------------------------------------------------- |
| 204 | * Platform driver |
| 205 | */ |
| 206 | |
| 207 | static int rcar_du_probe(struct platform_device *pdev) |
| 208 | { |
| 209 | return drm_platform_init(&rcar_du_driver, pdev); |
| 210 | } |
| 211 | |
| 212 | static int rcar_du_remove(struct platform_device *pdev) |
| 213 | { |
| 214 | drm_platform_exit(&rcar_du_driver, pdev); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
Laurent Pinchart | 481d342 | 2013-06-14 13:38:33 +0200 | [diff] [blame] | 219 | static const struct rcar_du_device_info rcar_du_r8a7779_info = { |
| 220 | .features = 0, |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 221 | .num_crtcs = 2, |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 222 | .routes = { |
| 223 | /* R8A7779 has two RGB outputs and one (currently unsupported) |
| 224 | * TCON output. |
| 225 | */ |
| 226 | [RCAR_DU_OUTPUT_DPAD0] = { |
| 227 | .possible_crtcs = BIT(0), |
| 228 | .encoder_type = DRM_MODE_ENCODER_NONE, |
| 229 | }, |
| 230 | [RCAR_DU_OUTPUT_DPAD1] = { |
| 231 | .possible_crtcs = BIT(1) | BIT(0), |
| 232 | .encoder_type = DRM_MODE_ENCODER_NONE, |
| 233 | }, |
| 234 | }, |
Laurent Pinchart | 481d342 | 2013-06-14 13:38:33 +0200 | [diff] [blame] | 235 | }; |
| 236 | |
Laurent Pinchart | ef2d84b | 2013-06-14 14:16:35 +0200 | [diff] [blame] | 237 | static const struct rcar_du_device_info rcar_du_r8a7790_info = { |
Laurent Pinchart | 38b62fb | 2013-06-15 02:40:57 +0200 | [diff] [blame] | 238 | .features = RCAR_DU_FEATURE_CRTC_IRQ_CLOCK | RCAR_DU_FEATURE_ALIGN_128B |
| 239 | | RCAR_DU_FEATURE_DEFR8, |
Laurent Pinchart | a5f0ef5 | 2013-06-17 00:29:25 +0200 | [diff] [blame] | 240 | .num_crtcs = 3, |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 241 | .routes = { |
| 242 | /* R8A7790 has one RGB output, two LVDS outputs and one |
| 243 | * (currently unsupported) TCON output. |
| 244 | */ |
| 245 | [RCAR_DU_OUTPUT_DPAD0] = { |
| 246 | .possible_crtcs = BIT(2) | BIT(1) | BIT(0), |
| 247 | .encoder_type = DRM_MODE_ENCODER_NONE, |
| 248 | }, |
| 249 | [RCAR_DU_OUTPUT_LVDS0] = { |
| 250 | .possible_crtcs = BIT(0), |
| 251 | .encoder_type = DRM_MODE_ENCODER_LVDS, |
| 252 | }, |
| 253 | [RCAR_DU_OUTPUT_LVDS1] = { |
| 254 | .possible_crtcs = BIT(2) | BIT(1), |
| 255 | .encoder_type = DRM_MODE_ENCODER_LVDS, |
| 256 | }, |
| 257 | }, |
Laurent Pinchart | ef2d84b | 2013-06-14 14:16:35 +0200 | [diff] [blame] | 258 | }; |
| 259 | |
Laurent Pinchart | 481d342 | 2013-06-14 13:38:33 +0200 | [diff] [blame] | 260 | static const struct platform_device_id rcar_du_id_table[] = { |
| 261 | { "rcar-du-r8a7779", (kernel_ulong_t)&rcar_du_r8a7779_info }, |
Laurent Pinchart | ef2d84b | 2013-06-14 14:16:35 +0200 | [diff] [blame] | 262 | { "rcar-du-r8a7790", (kernel_ulong_t)&rcar_du_r8a7790_info }, |
Laurent Pinchart | 481d342 | 2013-06-14 13:38:33 +0200 | [diff] [blame] | 263 | { } |
| 264 | }; |
| 265 | |
| 266 | MODULE_DEVICE_TABLE(platform, rcar_du_id_table); |
| 267 | |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 268 | static struct platform_driver rcar_du_platform_driver = { |
| 269 | .probe = rcar_du_probe, |
| 270 | .remove = rcar_du_remove, |
| 271 | .driver = { |
| 272 | .owner = THIS_MODULE, |
| 273 | .name = "rcar-du", |
| 274 | .pm = &rcar_du_pm_ops, |
| 275 | }, |
Laurent Pinchart | 481d342 | 2013-06-14 13:38:33 +0200 | [diff] [blame] | 276 | .id_table = rcar_du_id_table, |
Laurent Pinchart | 4bf8e19 | 2013-06-19 13:54:11 +0200 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | module_platform_driver(rcar_du_platform_driver); |
| 280 | |
| 281 | MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); |
| 282 | MODULE_DESCRIPTION("Renesas R-Car Display Unit DRM Driver"); |
| 283 | MODULE_LICENSE("GPL"); |