ast_drv.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 __devinit
  54. ast_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  55. {
  56. return drm_get_pci_dev(pdev, ent, &driver);
  57. }
  58. static void
  59. ast_pci_remove(struct pci_dev *pdev)
  60. {
  61. struct drm_device *dev = pci_get_drvdata(pdev);
  62. drm_put_dev(dev);
  63. }
  64. static int ast_drm_freeze(struct drm_device *dev)
  65. {
  66. drm_kms_helper_poll_disable(dev);
  67. pci_save_state(dev->pdev);
  68. console_lock();
  69. ast_fbdev_set_suspend(dev, 1);
  70. console_unlock();
  71. return 0;
  72. }
  73. static int ast_drm_thaw(struct drm_device *dev)
  74. {
  75. int error = 0;
  76. ast_post_gpu(dev);
  77. drm_mode_config_reset(dev);
  78. mutex_lock(&dev->mode_config.mutex);
  79. drm_helper_resume_force_mode(dev);
  80. mutex_unlock(&dev->mode_config.mutex);
  81. console_lock();
  82. ast_fbdev_set_suspend(dev, 0);
  83. console_unlock();
  84. return error;
  85. }
  86. static int ast_drm_resume(struct drm_device *dev)
  87. {
  88. int ret;
  89. if (pci_enable_device(dev->pdev))
  90. return -EIO;
  91. ret = ast_drm_thaw(dev);
  92. if (ret)
  93. return ret;
  94. drm_kms_helper_poll_enable(dev);
  95. return 0;
  96. }
  97. static int ast_pm_suspend(struct device *dev)
  98. {
  99. struct pci_dev *pdev = to_pci_dev(dev);
  100. struct drm_device *ddev = pci_get_drvdata(pdev);
  101. int error;
  102. error = ast_drm_freeze(ddev);
  103. if (error)
  104. return error;
  105. pci_disable_device(pdev);
  106. pci_set_power_state(pdev, PCI_D3hot);
  107. return 0;
  108. }
  109. static int ast_pm_resume(struct device *dev)
  110. {
  111. struct pci_dev *pdev = to_pci_dev(dev);
  112. struct drm_device *ddev = pci_get_drvdata(pdev);
  113. return ast_drm_resume(ddev);
  114. }
  115. static int ast_pm_freeze(struct device *dev)
  116. {
  117. struct pci_dev *pdev = to_pci_dev(dev);
  118. struct drm_device *ddev = pci_get_drvdata(pdev);
  119. if (!ddev || !ddev->dev_private)
  120. return -ENODEV;
  121. return ast_drm_freeze(ddev);
  122. }
  123. static int ast_pm_thaw(struct device *dev)
  124. {
  125. struct pci_dev *pdev = to_pci_dev(dev);
  126. struct drm_device *ddev = pci_get_drvdata(pdev);
  127. return ast_drm_thaw(ddev);
  128. }
  129. static int ast_pm_poweroff(struct device *dev)
  130. {
  131. struct pci_dev *pdev = to_pci_dev(dev);
  132. struct drm_device *ddev = pci_get_drvdata(pdev);
  133. return ast_drm_freeze(ddev);
  134. }
  135. static const struct dev_pm_ops ast_pm_ops = {
  136. .suspend = ast_pm_suspend,
  137. .resume = ast_pm_resume,
  138. .freeze = ast_pm_freeze,
  139. .thaw = ast_pm_thaw,
  140. .poweroff = ast_pm_poweroff,
  141. .restore = ast_pm_resume,
  142. };
  143. static struct pci_driver ast_pci_driver = {
  144. .name = DRIVER_NAME,
  145. .id_table = pciidlist,
  146. .probe = ast_pci_probe,
  147. .remove = ast_pci_remove,
  148. .driver.pm = &ast_pm_ops,
  149. };
  150. static const struct file_operations ast_fops = {
  151. .owner = THIS_MODULE,
  152. .open = drm_open,
  153. .release = drm_release,
  154. .unlocked_ioctl = drm_ioctl,
  155. .mmap = ast_mmap,
  156. .poll = drm_poll,
  157. .fasync = drm_fasync,
  158. #ifdef CONFIG_COMPAT
  159. .compat_ioctl = drm_compat_ioctl,
  160. #endif
  161. .read = drm_read,
  162. };
  163. static struct drm_driver driver = {
  164. .driver_features = DRIVER_USE_MTRR | DRIVER_MODESET | DRIVER_GEM,
  165. .dev_priv_size = 0,
  166. .load = ast_driver_load,
  167. .unload = ast_driver_unload,
  168. .fops = &ast_fops,
  169. .name = DRIVER_NAME,
  170. .desc = DRIVER_DESC,
  171. .date = DRIVER_DATE,
  172. .major = DRIVER_MAJOR,
  173. .minor = DRIVER_MINOR,
  174. .patchlevel = DRIVER_PATCHLEVEL,
  175. .gem_init_object = ast_gem_init_object,
  176. .gem_free_object = ast_gem_free_object,
  177. .dumb_create = ast_dumb_create,
  178. .dumb_map_offset = ast_dumb_mmap_offset,
  179. .dumb_destroy = ast_dumb_destroy,
  180. };
  181. static int __init ast_init(void)
  182. {
  183. #ifdef CONFIG_VGA_CONSOLE
  184. if (vgacon_text_force() && ast_modeset == -1)
  185. return -EINVAL;
  186. #endif
  187. if (ast_modeset == 0)
  188. return -EINVAL;
  189. return drm_pci_init(&driver, &ast_pci_driver);
  190. }
  191. static void __exit ast_exit(void)
  192. {
  193. drm_pci_exit(&driver, &ast_pci_driver);
  194. }
  195. module_init(ast_init);
  196. module_exit(ast_exit);
  197. MODULE_AUTHOR(DRIVER_AUTHOR);
  198. MODULE_DESCRIPTION(DRIVER_DESC);
  199. MODULE_LICENSE("GPL and additional rights");