nouveau_backlight.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 const 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 const 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_connector *connector)
  77. {
  78. struct drm_device *dev = connector->dev;
  79. struct drm_nouveau_private *dev_priv = dev->dev_private;
  80. struct backlight_properties props;
  81. struct backlight_device *bd;
  82. if (!(nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
  83. return 0;
  84. memset(&props, 0, sizeof(struct backlight_properties));
  85. props.type = BACKLIGHT_RAW;
  86. props.max_brightness = 31;
  87. bd = backlight_device_register("nv_backlight", &connector->kdev, dev,
  88. &nv40_bl_ops, &props);
  89. if (IS_ERR(bd))
  90. return PTR_ERR(bd);
  91. dev_priv->backlight = bd;
  92. bd->props.brightness = nv40_get_intensity(bd);
  93. backlight_update_status(bd);
  94. return 0;
  95. }
  96. static int nouveau_nv50_backlight_init(struct drm_connector *connector)
  97. {
  98. struct drm_device *dev = connector->dev;
  99. struct drm_nouveau_private *dev_priv = dev->dev_private;
  100. struct backlight_properties props;
  101. struct backlight_device *bd;
  102. if (!nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT))
  103. return 0;
  104. memset(&props, 0, sizeof(struct backlight_properties));
  105. props.type = BACKLIGHT_RAW;
  106. props.max_brightness = 1025;
  107. bd = backlight_device_register("nv_backlight", &connector->kdev, dev,
  108. &nv50_bl_ops, &props);
  109. if (IS_ERR(bd))
  110. return PTR_ERR(bd);
  111. dev_priv->backlight = bd;
  112. bd->props.brightness = nv50_get_intensity(bd);
  113. backlight_update_status(bd);
  114. return 0;
  115. }
  116. int nouveau_backlight_init(struct drm_connector *connector)
  117. {
  118. struct drm_device *dev = connector->dev;
  119. struct drm_nouveau_private *dev_priv = dev->dev_private;
  120. #ifdef CONFIG_ACPI
  121. if (acpi_video_backlight_support()) {
  122. NV_INFO(dev, "ACPI backlight interface available, "
  123. "not registering our own\n");
  124. return 0;
  125. }
  126. #endif
  127. switch (dev_priv->card_type) {
  128. case NV_40:
  129. return nouveau_nv40_backlight_init(connector);
  130. case NV_50:
  131. return nouveau_nv50_backlight_init(connector);
  132. default:
  133. break;
  134. }
  135. return 0;
  136. }
  137. void nouveau_backlight_exit(struct drm_connector *connector)
  138. {
  139. struct drm_device *dev = connector->dev;
  140. struct drm_nouveau_private *dev_priv = dev->dev_private;
  141. if (dev_priv->backlight) {
  142. backlight_device_unregister(dev_priv->backlight);
  143. dev_priv->backlight = NULL;
  144. }
  145. }