nv50_gpio.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2010 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 "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_hw.h"
  27. #include "nouveau_gpio.h"
  28. #include "nv50_display.h"
  29. static int
  30. nv50_gpio_location(int line, u32 *reg, u32 *shift)
  31. {
  32. const uint32_t nv50_gpio_reg[4] = { 0xe104, 0xe108, 0xe280, 0xe284 };
  33. if (line >= 32)
  34. return -EINVAL;
  35. *reg = nv50_gpio_reg[line >> 3];
  36. *shift = (line & 7) << 2;
  37. return 0;
  38. }
  39. int
  40. nv50_gpio_drive(struct drm_device *dev, int line, int dir, int out)
  41. {
  42. u32 reg, shift;
  43. if (nv50_gpio_location(line, &reg, &shift))
  44. return -EINVAL;
  45. nv_mask(dev, reg, 7 << shift, (((dir ^ 1) << 1) | out) << shift);
  46. return 0;
  47. }
  48. int
  49. nv50_gpio_sense(struct drm_device *dev, int line)
  50. {
  51. u32 reg, shift;
  52. if (nv50_gpio_location(line, &reg, &shift))
  53. return -EINVAL;
  54. return !!(nv_rd32(dev, reg) & (4 << shift));
  55. }
  56. void
  57. nv50_gpio_irq_enable(struct drm_device *dev, int line, bool on)
  58. {
  59. u32 reg = line < 16 ? 0xe050 : 0xe070;
  60. u32 mask = 0x00010001 << (line & 0xf);
  61. nv_wr32(dev, reg + 4, mask);
  62. nv_mask(dev, reg + 0, mask, on ? mask : 0);
  63. }
  64. int
  65. nvd0_gpio_drive(struct drm_device *dev, int line, int dir, int out)
  66. {
  67. u32 data = ((dir ^ 1) << 13) | (out << 12);
  68. nv_mask(dev, 0x00d610 + (line * 4), 0x00003000, data);
  69. nv_mask(dev, 0x00d604, 0x00000001, 0x00000001); /* update? */
  70. return 0;
  71. }
  72. int
  73. nvd0_gpio_sense(struct drm_device *dev, int line)
  74. {
  75. return !!(nv_rd32(dev, 0x00d610 + (line * 4)) & 0x00004000);
  76. }
  77. static void
  78. nv50_gpio_isr(struct drm_device *dev)
  79. {
  80. struct drm_nouveau_private *dev_priv = dev->dev_private;
  81. u32 intr0, intr1 = 0;
  82. u32 hi, lo;
  83. intr0 = nv_rd32(dev, 0xe054) & nv_rd32(dev, 0xe050);
  84. if (dev_priv->chipset >= 0x90)
  85. intr1 = nv_rd32(dev, 0xe074) & nv_rd32(dev, 0xe070);
  86. hi = (intr0 & 0x0000ffff) | (intr1 << 16);
  87. lo = (intr0 >> 16) | (intr1 & 0xffff0000);
  88. nouveau_gpio_isr(dev, 0, hi | lo);
  89. nv_wr32(dev, 0xe054, intr0);
  90. if (dev_priv->chipset >= 0x90)
  91. nv_wr32(dev, 0xe074, intr1);
  92. }
  93. int
  94. nv50_gpio_init(struct drm_device *dev)
  95. {
  96. struct drm_nouveau_private *dev_priv = dev->dev_private;
  97. /* disable, and ack any pending gpio interrupts */
  98. nv_wr32(dev, 0xe050, 0x00000000);
  99. nv_wr32(dev, 0xe054, 0xffffffff);
  100. if (dev_priv->chipset >= 0x90) {
  101. nv_wr32(dev, 0xe070, 0x00000000);
  102. nv_wr32(dev, 0xe074, 0xffffffff);
  103. }
  104. nouveau_irq_register(dev, 21, nv50_gpio_isr);
  105. return 0;
  106. }
  107. void
  108. nv50_gpio_fini(struct drm_device *dev)
  109. {
  110. struct drm_nouveau_private *dev_priv = dev->dev_private;
  111. nv_wr32(dev, 0xe050, 0x00000000);
  112. if (dev_priv->chipset >= 0x90)
  113. nv_wr32(dev, 0xe070, 0x00000000);
  114. nouveau_irq_unregister(dev, 21);
  115. }