nouveau_backlight.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2009 Red Hat <mjg@redhat.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a 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, sublicense, 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 above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. /*
  26. * Authors:
  27. * Matthew Garrett <mjg@redhat.com>
  28. *
  29. * Register locations derived from NVClock by Roderick Colenbrander
  30. */
  31. #include <linux/backlight.h>
  32. #include <linux/acpi.h>
  33. #include "drmP.h"
  34. #include "nouveau_drv.h"
  35. #include "nouveau_drm.h"
  36. #include "nouveau_reg.h"
  37. static int nv40_get_intensity(struct backlight_device *bd)
  38. {
  39. struct drm_device *dev = bl_get_data(bd);
  40. int val = (nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK)
  41. >> 16;
  42. return val;
  43. }
  44. static int nv40_set_intensity(struct backlight_device *bd)
  45. {
  46. struct drm_device *dev = bl_get_data(bd);
  47. int val = bd->props.brightness;
  48. int reg = nv_rd32(dev, NV40_PMC_BACKLIGHT);
  49. nv_wr32(dev, NV40_PMC_BACKLIGHT,
  50. (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK));
  51. return 0;
  52. }
  53. static struct backlight_ops nv40_bl_ops = {
  54. .options = BL_CORE_SUSPENDRESUME,
  55. .get_brightness = nv40_get_intensity,
  56. .update_status = nv40_set_intensity,
  57. };
  58. static int nv50_get_intensity(struct backlight_device *bd)
  59. {
  60. struct drm_device *dev = bl_get_data(bd);
  61. return nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT);
  62. }
  63. static int nv50_set_intensity(struct backlight_device *bd)
  64. {
  65. struct drm_device *dev = bl_get_data(bd);
  66. int val = bd->props.brightness;
  67. nv_wr32(dev, NV50_PDISPLAY_SOR_BACKLIGHT,
  68. val | NV50_PDISPLAY_SOR_BACKLIGHT_ENABLE);
  69. return 0;
  70. }
  71. static struct backlight_ops nv50_bl_ops = {
  72. .options = BL_CORE_SUSPENDRESUME,
  73. .get_brightness = nv50_get_intensity,
  74. .update_status = nv50_set_intensity,
  75. };
  76. static int nouveau_nv40_backlight_init(struct drm_device *dev)
  77. {
  78. struct backlight_properties props;
  79. struct drm_nouveau_private *dev_priv = dev->dev_private;
  80. struct backlight_device *bd;
  81. if (!(nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
  82. return 0;
  83. memset(&props, 0, sizeof(struct backlight_properties));
  84. props.max_brightness = 31;
  85. bd = backlight_device_register("nv_backlight", &dev->pdev->dev, dev,
  86. &nv40_bl_ops, &props);
  87. if (IS_ERR(bd))
  88. return PTR_ERR(bd);
  89. dev_priv->backlight = bd;
  90. bd->props.brightness = nv40_get_intensity(bd);
  91. backlight_update_status(bd);
  92. return 0;
  93. }
  94. static int nouveau_nv50_backlight_init(struct drm_device *dev)
  95. {
  96. struct backlight_properties props;
  97. struct drm_nouveau_private *dev_priv = dev->dev_private;
  98. struct backlight_device *bd;
  99. if (!nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT))
  100. return 0;
  101. memset(&props, 0, sizeof(struct backlight_properties));
  102. props.max_brightness = 1025;
  103. bd = backlight_device_register("nv_backlight", &dev->pdev->dev, dev,
  104. &nv50_bl_ops, &props);
  105. if (IS_ERR(bd))
  106. return PTR_ERR(bd);
  107. dev_priv->backlight = bd;
  108. bd->props.brightness = nv50_get_intensity(bd);
  109. backlight_update_status(bd);
  110. return 0;
  111. }
  112. int nouveau_backlight_init(struct drm_device *dev)
  113. {
  114. struct drm_nouveau_private *dev_priv = dev->dev_private;
  115. #ifdef CONFIG_ACPI
  116. if (acpi_video_backlight_support()) {
  117. NV_INFO(dev, "ACPI backlight interface available, "
  118. "not registering our own\n");
  119. return 0;
  120. }
  121. #endif
  122. switch (dev_priv->card_type) {
  123. case NV_40:
  124. return nouveau_nv40_backlight_init(dev);
  125. case NV_50:
  126. return nouveau_nv50_backlight_init(dev);
  127. default:
  128. break;
  129. }
  130. return 0;
  131. }
  132. void nouveau_backlight_exit(struct drm_device *dev)
  133. {
  134. struct drm_nouveau_private *dev_priv = dev->dev_private;
  135. if (dev_priv->backlight) {
  136. backlight_device_unregister(dev_priv->backlight);
  137. dev_priv->backlight = NULL;
  138. }
  139. }