ast_drv.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sub license, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  18. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * The above copyright notice and this permission notice (including the
  21. * next paragraph) shall be included in all copies or substantial portions
  22. * of the Software.
  23. *
  24. */
  25. /*
  26. * Authors: Dave Airlie <airlied@redhat.com>
  27. */
  28. #include <linux/module.h>
  29. #include <linux/console.h>
  30. #include <drm/drmP.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include "ast_drv.h"
  33. int ast_modeset = -1;
  34. MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
  35. module_param_named(modeset, ast_modeset, int, 0400);
  36. #define PCI_VENDOR_ASPEED 0x1a03
  37. static struct drm_driver driver;
  38. #define AST_VGA_DEVICE(id, info) { \
  39. .class = PCI_BASE_CLASS_DISPLAY << 16, \
  40. .class_mask = 0xff0000, \
  41. .vendor = PCI_VENDOR_ASPEED, \
  42. .device = id, \
  43. .subvendor = PCI_ANY_ID, \
  44. .subdevice = PCI_ANY_ID, \
  45. .driver_data = (unsigned long) info }
  46. static DEFINE_PCI_DEVICE_TABLE(pciidlist) = {
  47. AST_VGA_DEVICE(PCI_CHIP_AST2000, NULL),
  48. AST_VGA_DEVICE(PCI_CHIP_AST2100, NULL),
  49. /* AST_VGA_DEVICE(PCI_CHIP_AST1180, NULL), - don't bind to 1180 for now */
  50. {0, 0, 0},
  51. };
  52. MODULE_DEVICE_TABLE(pci, pciidlist);
  53. static int ast_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  54. {
  55. return drm_get_pci_dev(pdev, ent, &driver);
  56. }
  57. static void
  58. ast_pci_remove(struct pci_dev *pdev)
  59. {
  60. struct drm_device *dev = pci_get_drvdata(pdev);
  61. drm_put_dev(dev);
  62. }
  63. static int ast_drm_freeze(struct drm_device *dev)
  64. {
  65. drm_kms_helper_poll_disable(dev);
  66. pci_save_state(dev->pdev);
  67. console_lock();
  68. ast_fbdev_set_suspend(dev, 1);
  69. console_unlock();
  70. return 0;
  71. }
  72. static int ast_drm_thaw(struct drm_device *dev)
  73. {
  74. int error = 0;
  75. ast_post_gpu(dev);
  76. drm_mode_config_reset(dev);
  77. drm_modeset_lock_all(dev);
  78. drm_helper_resume_force_mode(dev);
  79. drm_modeset_unlock_all(dev);
  80. console_lock();
  81. ast_fbdev_set_suspend(dev, 0);
  82. console_unlock();
  83. return error;
  84. }
  85. static int ast_drm_resume(struct drm_device *dev)
  86. {
  87. int ret;
  88. if (pci_enable_device(dev->pdev))
  89. return -EIO;
  90. ret = ast_drm_thaw(dev);
  91. if (ret)
  92. return ret;
  93. drm_kms_helper_poll_enable(dev);
  94. return 0;
  95. }
  96. static int ast_pm_suspend(struct device *dev)
  97. {
  98. struct pci_dev *pdev = to_pci_dev(dev);
  99. struct drm_device *ddev = pci_get_drvdata(pdev);
  100. int error;
  101. error = ast_drm_freeze(ddev);
  102. if (error)
  103. return error;
  104. pci_disable_device(pdev);
  105. pci_set_power_state(pdev, PCI_D3hot);
  106. return 0;
  107. }
  108. static int ast_pm_resume(struct device *dev)
  109. {
  110. struct pci_dev *pdev = to_pci_dev(dev);
  111. struct drm_device *ddev = pci_get_drvdata(pdev);
  112. return ast_drm_resume(ddev);
  113. }
  114. static int ast_pm_freeze(struct device *dev)
  115. {
  116. struct pci_dev *pdev = to_pci_dev(dev);
  117. struct drm_device *ddev = pci_get_drvdata(pdev);
  118. if (!ddev || !ddev->dev_private)
  119. return -ENODEV;
  120. return ast_drm_freeze(ddev);
  121. }
  122. static int ast_pm_thaw(struct device *dev)
  123. {
  124. struct pci_dev *pdev = to_pci_dev(dev);
  125. struct drm_device *ddev = pci_get_drvdata(pdev);
  126. return ast_drm_thaw(ddev);
  127. }
  128. static int ast_pm_poweroff(struct device *dev)
  129. {
  130. struct pci_dev *pdev = to_pci_dev(dev);
  131. struct drm_device *ddev = pci_get_drvdata(pdev);
  132. return ast_drm_freeze(ddev);
  133. }
  134. static const struct dev_pm_ops ast_pm_ops = {
  135. .suspend = ast_pm_suspend,
  136. .resume = ast_pm_resume,
  137. .freeze = ast_pm_freeze,
  138. .thaw = ast_pm_thaw,
  139. .poweroff = ast_pm_poweroff,
  140. .restore = ast_pm_resume,
  141. };
  142. static struct pci_driver ast_pci_driver = {
  143. .name = DRIVER_NAME,
  144. .id_table = pciidlist,
  145. .probe = ast_pci_probe,
  146. .remove = ast_pci_remove,
  147. .driver.pm = &ast_pm_ops,
  148. };
  149. static const struct file_operations ast_fops = {
  150. .owner = THIS_MODULE,
  151. .open = drm_open,
  152. .release = drm_release,
  153. .unlocked_ioctl = drm_ioctl,
  154. .mmap = ast_mmap,
  155. .poll = drm_poll,
  156. .fasync = drm_fasync,
  157. #ifdef CONFIG_COMPAT
  158. .compat_ioctl = drm_compat_ioctl,
  159. #endif
  160. .read = drm_read,
  161. };
  162. static struct drm_driver driver = {
  163. .driver_features = DRIVER_USE_MTRR | DRIVER_MODESET | DRIVER_GEM,
  164. .dev_priv_size = 0,
  165. .load = ast_driver_load,
  166. .unload = ast_driver_unload,
  167. .fops = &ast_fops,
  168. .name = DRIVER_NAME,
  169. .desc = DRIVER_DESC,
  170. .date = DRIVER_DATE,
  171. .major = DRIVER_MAJOR,
  172. .minor = DRIVER_MINOR,
  173. .patchlevel = DRIVER_PATCHLEVEL,
  174. .gem_init_object = ast_gem_init_object,
  175. .gem_free_object = ast_gem_free_object,
  176. .dumb_create = ast_dumb_create,
  177. .dumb_map_offset = ast_dumb_mmap_offset,
  178. .dumb_destroy = ast_dumb_destroy,
  179. };
  180. static int __init ast_init(void)
  181. {
  182. #ifdef CONFIG_VGA_CONSOLE
  183. if (vgacon_text_force() && ast_modeset == -1)
  184. return -EINVAL;
  185. #endif
  186. if (ast_modeset == 0)
  187. return -EINVAL;
  188. return drm_pci_init(&driver, &ast_pci_driver);
  189. }
  190. static void __exit ast_exit(void)
  191. {
  192. drm_pci_exit(&driver, &ast_pci_driver);
  193. }
  194. module_init(ast_init);
  195. module_exit(ast_exit);
  196. MODULE_AUTHOR(DRIVER_AUTHOR);
  197. MODULE_DESCRIPTION(DRIVER_DESC);
  198. MODULE_LICENSE("GPL and additional rights");