ast_drv.c 5.5 KB

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