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