nouveau_backlight.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "drmP.h"
  33. #include "nouveau_drv.h"
  34. #include "nouveau_drm.h"
  35. #include "nouveau_reg.h"
  36. static int nv40_get_intensity(struct backlight_device *bd)
  37. {
  38. struct drm_device *dev = bl_get_data(bd);
  39. int val = (nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK)
  40. >> 16;
  41. return val;
  42. }
  43. static int nv40_set_intensity(struct backlight_device *bd)
  44. {
  45. struct drm_device *dev = bl_get_data(bd);
  46. int val = bd->props.brightness;
  47. int reg = nv_rd32(dev, NV40_PMC_BACKLIGHT);
  48. nv_wr32(dev, NV40_PMC_BACKLIGHT,
  49. (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK));
  50. return 0;
  51. }
  52. static struct backlight_ops nv40_bl_ops = {
  53. .options = BL_CORE_SUSPENDRESUME,
  54. .get_brightness = nv40_get_intensity,
  55. .update_status = nv40_set_intensity,
  56. };
  57. static int nv50_get_intensity(struct backlight_device *bd)
  58. {
  59. struct drm_device *dev = bl_get_data(bd);
  60. return nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT);
  61. }
  62. static int nv50_set_intensity(struct backlight_device *bd)
  63. {
  64. struct drm_device *dev = bl_get_data(bd);
  65. int val = bd->props.brightness;
  66. nv_wr32(dev, NV50_PDISPLAY_SOR_BACKLIGHT,
  67. val | NV50_PDISPLAY_SOR_BACKLIGHT_ENABLE);
  68. return 0;
  69. }
  70. static struct backlight_ops nv50_bl_ops = {
  71. .options = BL_CORE_SUSPENDRESUME,
  72. .get_brightness = nv50_get_intensity,
  73. .update_status = nv50_set_intensity,
  74. };
  75. static int nouveau_nv40_backlight_init(struct drm_device *dev)
  76. {
  77. struct drm_nouveau_private *dev_priv = dev->dev_private;
  78. struct backlight_device *bd;
  79. if (!(nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
  80. return 0;
  81. bd = backlight_device_register("nv_backlight", &dev->pdev->dev, dev,
  82. &nv40_bl_ops);
  83. if (IS_ERR(bd))
  84. return PTR_ERR(bd);
  85. dev_priv->backlight = bd;
  86. bd->props.max_brightness = 31;
  87. bd->props.brightness = nv40_get_intensity(bd);
  88. backlight_update_status(bd);
  89. return 0;
  90. }
  91. static int nouveau_nv50_backlight_init(struct drm_device *dev)
  92. {
  93. struct drm_nouveau_private *dev_priv = dev->dev_private;
  94. struct backlight_device *bd;
  95. if (!nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT))
  96. return 0;
  97. bd = backlight_device_register("nv_backlight", &dev->pdev->dev, dev,
  98. &nv50_bl_ops);
  99. if (IS_ERR(bd))
  100. return PTR_ERR(bd);
  101. dev_priv->backlight = bd;
  102. bd->props.max_brightness = 1025;
  103. bd->props.brightness = nv50_get_intensity(bd);
  104. backlight_update_status(bd);
  105. return 0;
  106. }
  107. int nouveau_backlight_init(struct drm_device *dev)
  108. {
  109. struct drm_nouveau_private *dev_priv = dev->dev_private;
  110. switch (dev_priv->card_type) {
  111. case NV_40:
  112. return nouveau_nv40_backlight_init(dev);
  113. case NV_50:
  114. return nouveau_nv50_backlight_init(dev);
  115. default:
  116. break;
  117. }
  118. return 0;
  119. }
  120. void nouveau_backlight_exit(struct drm_device *dev)
  121. {
  122. struct drm_nouveau_private *dev_priv = dev->dev_private;
  123. if (dev_priv->backlight) {
  124. backlight_device_unregister(dev_priv->backlight);
  125. dev_priv->backlight = NULL;
  126. }
  127. }