Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 1 | /* |
| 2 | * ARC PGU DRM driver. |
| 3 | * |
| 4 | * Copyright (C) 2016 Synopsys, Inc. (www.synopsys.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/clk.h> |
| 18 | #include <drm/drm_crtc_helper.h> |
| 19 | #include <drm/drm_fb_cma_helper.h> |
| 20 | #include <drm/drm_gem_cma_helper.h> |
| 21 | #include <drm/drm_atomic_helper.h> |
Alexey Brodkin | 8aeab99 | 2016-04-27 16:02:39 +0300 | [diff] [blame] | 22 | #include <linux/of_reserved_mem.h> |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 23 | |
| 24 | #include "arcpgu.h" |
| 25 | #include "arcpgu_regs.h" |
| 26 | |
| 27 | static void arcpgu_fb_output_poll_changed(struct drm_device *dev) |
| 28 | { |
| 29 | struct arcpgu_drm_private *arcpgu = dev->dev_private; |
| 30 | |
| 31 | if (arcpgu->fbdev) |
| 32 | drm_fbdev_cma_hotplug_event(arcpgu->fbdev); |
| 33 | } |
| 34 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 35 | static struct drm_mode_config_funcs arcpgu_drm_modecfg_funcs = { |
| 36 | .fb_create = drm_fb_cma_create, |
| 37 | .output_poll_changed = arcpgu_fb_output_poll_changed, |
| 38 | .atomic_check = drm_atomic_helper_check, |
Daniel Vetter | 17b58b2 | 2016-06-08 14:19:03 +0200 | [diff] [blame^] | 39 | .atomic_commit = drm_atomic_helper_commit, |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static void arcpgu_setup_mode_config(struct drm_device *drm) |
| 43 | { |
| 44 | drm_mode_config_init(drm); |
| 45 | drm->mode_config.min_width = 0; |
| 46 | drm->mode_config.min_height = 0; |
| 47 | drm->mode_config.max_width = 1920; |
| 48 | drm->mode_config.max_height = 1080; |
| 49 | drm->mode_config.funcs = &arcpgu_drm_modecfg_funcs; |
| 50 | } |
| 51 | |
| 52 | int arcpgu_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 53 | { |
| 54 | int ret; |
| 55 | |
| 56 | ret = drm_gem_mmap(filp, vma); |
| 57 | if (ret) |
| 58 | return ret; |
| 59 | |
| 60 | vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static const struct file_operations arcpgu_drm_ops = { |
| 65 | .owner = THIS_MODULE, |
| 66 | .open = drm_open, |
| 67 | .release = drm_release, |
| 68 | .unlocked_ioctl = drm_ioctl, |
| 69 | #ifdef CONFIG_COMPAT |
| 70 | .compat_ioctl = drm_compat_ioctl, |
| 71 | #endif |
| 72 | .poll = drm_poll, |
| 73 | .read = drm_read, |
| 74 | .llseek = no_llseek, |
| 75 | .mmap = arcpgu_gem_mmap, |
| 76 | }; |
| 77 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 78 | static void arcpgu_lastclose(struct drm_device *drm) |
| 79 | { |
| 80 | struct arcpgu_drm_private *arcpgu = drm->dev_private; |
| 81 | |
| 82 | drm_fbdev_cma_restore_mode(arcpgu->fbdev); |
| 83 | } |
| 84 | |
| 85 | static int arcpgu_load(struct drm_device *drm) |
| 86 | { |
| 87 | struct platform_device *pdev = to_platform_device(drm->dev); |
| 88 | struct arcpgu_drm_private *arcpgu; |
| 89 | struct device_node *encoder_node; |
| 90 | struct resource *res; |
| 91 | int ret; |
| 92 | |
| 93 | arcpgu = devm_kzalloc(&pdev->dev, sizeof(*arcpgu), GFP_KERNEL); |
| 94 | if (arcpgu == NULL) |
| 95 | return -ENOMEM; |
| 96 | |
| 97 | drm->dev_private = arcpgu; |
| 98 | |
| 99 | arcpgu->clk = devm_clk_get(drm->dev, "pxlclk"); |
| 100 | if (IS_ERR(arcpgu->clk)) |
| 101 | return PTR_ERR(arcpgu->clk); |
| 102 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 103 | arcpgu_setup_mode_config(drm); |
| 104 | |
| 105 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 106 | arcpgu->regs = devm_ioremap_resource(&pdev->dev, res); |
| 107 | if (IS_ERR(arcpgu->regs)) { |
| 108 | dev_err(drm->dev, "Could not remap IO mem\n"); |
| 109 | return PTR_ERR(arcpgu->regs); |
| 110 | } |
| 111 | |
| 112 | dev_info(drm->dev, "arc_pgu ID: 0x%x\n", |
| 113 | arc_pgu_read(arcpgu, ARCPGU_REG_ID)); |
| 114 | |
Alexey Brodkin | 8aeab99 | 2016-04-27 16:02:39 +0300 | [diff] [blame] | 115 | /* Get the optional framebuffer memory resource */ |
| 116 | ret = of_reserved_mem_device_init(drm->dev); |
| 117 | if (ret && ret != -ENODEV) |
| 118 | return ret; |
| 119 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 120 | if (dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32))) |
| 121 | return -ENODEV; |
| 122 | |
| 123 | if (arc_pgu_setup_crtc(drm) < 0) |
| 124 | return -ENODEV; |
| 125 | |
| 126 | /* find the encoder node and initialize it */ |
| 127 | encoder_node = of_parse_phandle(drm->dev->of_node, "encoder-slave", 0); |
| 128 | if (!encoder_node) { |
| 129 | dev_err(drm->dev, "failed to get an encoder slave node\n"); |
| 130 | return -ENODEV; |
| 131 | } |
| 132 | |
| 133 | ret = arcpgu_drm_hdmi_init(drm, encoder_node); |
| 134 | if (ret < 0) |
| 135 | return ret; |
| 136 | |
| 137 | drm_mode_config_reset(drm); |
| 138 | drm_kms_helper_poll_init(drm); |
| 139 | |
| 140 | arcpgu->fbdev = drm_fbdev_cma_init(drm, 16, |
| 141 | drm->mode_config.num_crtc, |
| 142 | drm->mode_config.num_connector); |
| 143 | if (IS_ERR(arcpgu->fbdev)) { |
| 144 | ret = PTR_ERR(arcpgu->fbdev); |
| 145 | arcpgu->fbdev = NULL; |
| 146 | return -ENODEV; |
| 147 | } |
| 148 | |
| 149 | platform_set_drvdata(pdev, arcpgu); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int arcpgu_unload(struct drm_device *drm) |
| 154 | { |
| 155 | struct arcpgu_drm_private *arcpgu = drm->dev_private; |
| 156 | |
| 157 | if (arcpgu->fbdev) { |
| 158 | drm_fbdev_cma_fini(arcpgu->fbdev); |
| 159 | arcpgu->fbdev = NULL; |
| 160 | } |
| 161 | drm_kms_helper_poll_fini(drm); |
| 162 | drm_vblank_cleanup(drm); |
| 163 | drm_mode_config_cleanup(drm); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static struct drm_driver arcpgu_drm_driver = { |
| 169 | .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | |
| 170 | DRIVER_ATOMIC, |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 171 | .lastclose = arcpgu_lastclose, |
| 172 | .name = "drm-arcpgu", |
| 173 | .desc = "ARC PGU Controller", |
| 174 | .date = "20160219", |
| 175 | .major = 1, |
| 176 | .minor = 0, |
| 177 | .patchlevel = 0, |
| 178 | .fops = &arcpgu_drm_ops, |
| 179 | .dumb_create = drm_gem_cma_dumb_create, |
| 180 | .dumb_map_offset = drm_gem_cma_dumb_map_offset, |
| 181 | .dumb_destroy = drm_gem_dumb_destroy, |
| 182 | .get_vblank_counter = drm_vblank_no_hw_counter, |
| 183 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, |
| 184 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, |
Daniel Vetter | e1512ee | 2016-05-30 19:53:16 +0200 | [diff] [blame] | 185 | .gem_free_object_unlocked = drm_gem_cma_free_object, |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 186 | .gem_vm_ops = &drm_gem_cma_vm_ops, |
| 187 | .gem_prime_export = drm_gem_prime_export, |
| 188 | .gem_prime_import = drm_gem_prime_import, |
| 189 | .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, |
| 190 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, |
| 191 | .gem_prime_vmap = drm_gem_cma_prime_vmap, |
| 192 | .gem_prime_vunmap = drm_gem_cma_prime_vunmap, |
| 193 | .gem_prime_mmap = drm_gem_cma_prime_mmap, |
| 194 | }; |
| 195 | |
| 196 | static int arcpgu_probe(struct platform_device *pdev) |
| 197 | { |
| 198 | struct drm_device *drm; |
| 199 | int ret; |
| 200 | |
| 201 | drm = drm_dev_alloc(&arcpgu_drm_driver, &pdev->dev); |
| 202 | if (!drm) |
| 203 | return -ENOMEM; |
| 204 | |
| 205 | ret = arcpgu_load(drm); |
| 206 | if (ret) |
| 207 | goto err_unref; |
| 208 | |
| 209 | ret = drm_dev_register(drm, 0); |
| 210 | if (ret) |
| 211 | goto err_unload; |
| 212 | |
| 213 | ret = drm_connector_register_all(drm); |
| 214 | if (ret) |
| 215 | goto err_unregister; |
| 216 | |
| 217 | return 0; |
| 218 | |
| 219 | err_unregister: |
| 220 | drm_dev_unregister(drm); |
| 221 | |
| 222 | err_unload: |
| 223 | arcpgu_unload(drm); |
| 224 | |
| 225 | err_unref: |
| 226 | drm_dev_unref(drm); |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | static int arcpgu_remove(struct platform_device *pdev) |
| 232 | { |
| 233 | struct drm_device *drm = platform_get_drvdata(pdev); |
| 234 | |
| 235 | drm_connector_unregister_all(drm); |
| 236 | drm_dev_unregister(drm); |
| 237 | arcpgu_unload(drm); |
| 238 | drm_dev_unref(drm); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static const struct of_device_id arcpgu_of_table[] = { |
| 244 | {.compatible = "snps,arcpgu"}, |
| 245 | {} |
| 246 | }; |
| 247 | |
| 248 | MODULE_DEVICE_TABLE(of, arcpgu_of_table); |
| 249 | |
| 250 | static struct platform_driver arcpgu_platform_driver = { |
| 251 | .probe = arcpgu_probe, |
| 252 | .remove = arcpgu_remove, |
| 253 | .driver = { |
| 254 | .name = "arcpgu", |
| 255 | .of_match_table = arcpgu_of_table, |
| 256 | }, |
| 257 | }; |
| 258 | |
| 259 | module_platform_driver(arcpgu_platform_driver); |
| 260 | |
| 261 | MODULE_AUTHOR("Carlos Palminha <palminha@synopsys.com>"); |
| 262 | MODULE_DESCRIPTION("ARC PGU DRM driver"); |
| 263 | MODULE_LICENSE("GPL"); |