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 | |
Markus Elfring | f15a8e9 | 2016-07-15 21:52:40 +0200 | [diff] [blame] | 31 | drm_fbdev_cma_hotplug_event(arcpgu->fbdev); |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 32 | } |
| 33 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 34 | static struct drm_mode_config_funcs arcpgu_drm_modecfg_funcs = { |
| 35 | .fb_create = drm_fb_cma_create, |
| 36 | .output_poll_changed = arcpgu_fb_output_poll_changed, |
| 37 | .atomic_check = drm_atomic_helper_check, |
Daniel Vetter | 17b58b2 | 2016-06-08 14:19:03 +0200 | [diff] [blame] | 38 | .atomic_commit = drm_atomic_helper_commit, |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | static void arcpgu_setup_mode_config(struct drm_device *drm) |
| 42 | { |
| 43 | drm_mode_config_init(drm); |
| 44 | drm->mode_config.min_width = 0; |
| 45 | drm->mode_config.min_height = 0; |
| 46 | drm->mode_config.max_width = 1920; |
| 47 | drm->mode_config.max_height = 1080; |
| 48 | drm->mode_config.funcs = &arcpgu_drm_modecfg_funcs; |
| 49 | } |
| 50 | |
Wei Yongjun | ee29a9e | 2016-07-19 12:04:05 +0000 | [diff] [blame] | 51 | static int arcpgu_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 52 | { |
| 53 | int ret; |
| 54 | |
| 55 | ret = drm_gem_mmap(filp, vma); |
| 56 | if (ret) |
| 57 | return ret; |
| 58 | |
| 59 | vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static const struct file_operations arcpgu_drm_ops = { |
| 64 | .owner = THIS_MODULE, |
| 65 | .open = drm_open, |
| 66 | .release = drm_release, |
| 67 | .unlocked_ioctl = drm_ioctl, |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 68 | .compat_ioctl = drm_compat_ioctl, |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 69 | .poll = drm_poll, |
| 70 | .read = drm_read, |
| 71 | .llseek = no_llseek, |
| 72 | .mmap = arcpgu_gem_mmap, |
| 73 | }; |
| 74 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 75 | static void arcpgu_lastclose(struct drm_device *drm) |
| 76 | { |
| 77 | struct arcpgu_drm_private *arcpgu = drm->dev_private; |
| 78 | |
| 79 | drm_fbdev_cma_restore_mode(arcpgu->fbdev); |
| 80 | } |
| 81 | |
| 82 | static int arcpgu_load(struct drm_device *drm) |
| 83 | { |
| 84 | struct platform_device *pdev = to_platform_device(drm->dev); |
| 85 | struct arcpgu_drm_private *arcpgu; |
| 86 | struct device_node *encoder_node; |
| 87 | struct resource *res; |
| 88 | int ret; |
| 89 | |
| 90 | arcpgu = devm_kzalloc(&pdev->dev, sizeof(*arcpgu), GFP_KERNEL); |
| 91 | if (arcpgu == NULL) |
| 92 | return -ENOMEM; |
| 93 | |
| 94 | drm->dev_private = arcpgu; |
| 95 | |
| 96 | arcpgu->clk = devm_clk_get(drm->dev, "pxlclk"); |
| 97 | if (IS_ERR(arcpgu->clk)) |
| 98 | return PTR_ERR(arcpgu->clk); |
| 99 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 100 | arcpgu_setup_mode_config(drm); |
| 101 | |
| 102 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 103 | arcpgu->regs = devm_ioremap_resource(&pdev->dev, res); |
Wei Yongjun | 7725936 | 2016-07-19 12:01:37 +0000 | [diff] [blame] | 104 | if (IS_ERR(arcpgu->regs)) |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 105 | return PTR_ERR(arcpgu->regs); |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 106 | |
| 107 | dev_info(drm->dev, "arc_pgu ID: 0x%x\n", |
| 108 | arc_pgu_read(arcpgu, ARCPGU_REG_ID)); |
| 109 | |
Alexey Brodkin | 8aeab99 | 2016-04-27 16:02:39 +0300 | [diff] [blame] | 110 | /* Get the optional framebuffer memory resource */ |
| 111 | ret = of_reserved_mem_device_init(drm->dev); |
| 112 | if (ret && ret != -ENODEV) |
| 113 | return ret; |
| 114 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 115 | if (dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32))) |
| 116 | return -ENODEV; |
| 117 | |
| 118 | if (arc_pgu_setup_crtc(drm) < 0) |
| 119 | return -ENODEV; |
| 120 | |
| 121 | /* find the encoder node and initialize it */ |
| 122 | encoder_node = of_parse_phandle(drm->dev->of_node, "encoder-slave", 0); |
Ruud Derwig | a189d28 | 2016-06-06 10:47:46 +0300 | [diff] [blame] | 123 | if (encoder_node) { |
| 124 | ret = arcpgu_drm_hdmi_init(drm, encoder_node); |
Peter Chen | 8e7446c | 2016-07-15 11:17:05 +0800 | [diff] [blame] | 125 | of_node_put(encoder_node); |
Ruud Derwig | a189d28 | 2016-06-06 10:47:46 +0300 | [diff] [blame] | 126 | if (ret < 0) |
| 127 | return ret; |
| 128 | } else { |
Wei Yongjun | ee29a9e | 2016-07-19 12:04:05 +0000 | [diff] [blame] | 129 | ret = arcpgu_drm_sim_init(drm, NULL); |
Ruud Derwig | a189d28 | 2016-06-06 10:47:46 +0300 | [diff] [blame] | 130 | if (ret < 0) |
| 131 | return ret; |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 132 | } |
| 133 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 134 | drm_mode_config_reset(drm); |
| 135 | drm_kms_helper_poll_init(drm); |
| 136 | |
| 137 | arcpgu->fbdev = drm_fbdev_cma_init(drm, 16, |
| 138 | drm->mode_config.num_crtc, |
| 139 | drm->mode_config.num_connector); |
| 140 | if (IS_ERR(arcpgu->fbdev)) { |
| 141 | ret = PTR_ERR(arcpgu->fbdev); |
| 142 | arcpgu->fbdev = NULL; |
| 143 | return -ENODEV; |
| 144 | } |
| 145 | |
| 146 | platform_set_drvdata(pdev, arcpgu); |
| 147 | return 0; |
| 148 | } |
| 149 | |
Wei Yongjun | ee29a9e | 2016-07-19 12:04:05 +0000 | [diff] [blame] | 150 | static int arcpgu_unload(struct drm_device *drm) |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 151 | { |
| 152 | struct arcpgu_drm_private *arcpgu = drm->dev_private; |
| 153 | |
| 154 | if (arcpgu->fbdev) { |
| 155 | drm_fbdev_cma_fini(arcpgu->fbdev); |
| 156 | arcpgu->fbdev = NULL; |
| 157 | } |
| 158 | drm_kms_helper_poll_fini(drm); |
| 159 | drm_vblank_cleanup(drm); |
| 160 | drm_mode_config_cleanup(drm); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static struct drm_driver arcpgu_drm_driver = { |
| 166 | .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | |
| 167 | DRIVER_ATOMIC, |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 168 | .lastclose = arcpgu_lastclose, |
| 169 | .name = "drm-arcpgu", |
| 170 | .desc = "ARC PGU Controller", |
| 171 | .date = "20160219", |
| 172 | .major = 1, |
| 173 | .minor = 0, |
| 174 | .patchlevel = 0, |
| 175 | .fops = &arcpgu_drm_ops, |
| 176 | .dumb_create = drm_gem_cma_dumb_create, |
| 177 | .dumb_map_offset = drm_gem_cma_dumb_map_offset, |
| 178 | .dumb_destroy = drm_gem_dumb_destroy, |
| 179 | .get_vblank_counter = drm_vblank_no_hw_counter, |
| 180 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, |
| 181 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, |
Daniel Vetter | e1512ee | 2016-05-30 19:53:16 +0200 | [diff] [blame] | 182 | .gem_free_object_unlocked = drm_gem_cma_free_object, |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 183 | .gem_vm_ops = &drm_gem_cma_vm_ops, |
| 184 | .gem_prime_export = drm_gem_prime_export, |
| 185 | .gem_prime_import = drm_gem_prime_import, |
| 186 | .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, |
| 187 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, |
| 188 | .gem_prime_vmap = drm_gem_cma_prime_vmap, |
| 189 | .gem_prime_vunmap = drm_gem_cma_prime_vunmap, |
| 190 | .gem_prime_mmap = drm_gem_cma_prime_mmap, |
| 191 | }; |
| 192 | |
| 193 | static int arcpgu_probe(struct platform_device *pdev) |
| 194 | { |
| 195 | struct drm_device *drm; |
| 196 | int ret; |
| 197 | |
| 198 | drm = drm_dev_alloc(&arcpgu_drm_driver, &pdev->dev); |
Tom Gundersen | 0f28860 | 2016-09-21 16:59:19 +0200 | [diff] [blame] | 199 | if (IS_ERR(drm)) |
| 200 | return PTR_ERR(drm); |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 201 | |
| 202 | ret = arcpgu_load(drm); |
| 203 | if (ret) |
| 204 | goto err_unref; |
| 205 | |
| 206 | ret = drm_dev_register(drm, 0); |
| 207 | if (ret) |
| 208 | goto err_unload; |
| 209 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 210 | return 0; |
| 211 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 212 | err_unload: |
| 213 | arcpgu_unload(drm); |
| 214 | |
| 215 | err_unref: |
| 216 | drm_dev_unref(drm); |
| 217 | |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | static int arcpgu_remove(struct platform_device *pdev) |
| 222 | { |
| 223 | struct drm_device *drm = platform_get_drvdata(pdev); |
| 224 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 225 | drm_dev_unregister(drm); |
| 226 | arcpgu_unload(drm); |
| 227 | drm_dev_unref(drm); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static const struct of_device_id arcpgu_of_table[] = { |
| 233 | {.compatible = "snps,arcpgu"}, |
| 234 | {} |
| 235 | }; |
| 236 | |
| 237 | MODULE_DEVICE_TABLE(of, arcpgu_of_table); |
| 238 | |
| 239 | static struct platform_driver arcpgu_platform_driver = { |
| 240 | .probe = arcpgu_probe, |
| 241 | .remove = arcpgu_remove, |
| 242 | .driver = { |
| 243 | .name = "arcpgu", |
| 244 | .of_match_table = arcpgu_of_table, |
| 245 | }, |
| 246 | }; |
| 247 | |
| 248 | module_platform_driver(arcpgu_platform_driver); |
| 249 | |
| 250 | MODULE_AUTHOR("Carlos Palminha <palminha@synopsys.com>"); |
| 251 | MODULE_DESCRIPTION("ARC PGU DRM driver"); |
| 252 | MODULE_LICENSE("GPL"); |