nouveau_backlight.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include "nouveau_encoder.h"
  38. static int
  39. nv40_get_intensity(struct backlight_device *bd)
  40. {
  41. struct drm_device *dev = bl_get_data(bd);
  42. int val = (nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK)
  43. >> 16;
  44. return val;
  45. }
  46. static int
  47. nv40_set_intensity(struct backlight_device *bd)
  48. {
  49. struct drm_device *dev = bl_get_data(bd);
  50. int val = bd->props.brightness;
  51. int reg = nv_rd32(dev, NV40_PMC_BACKLIGHT);
  52. nv_wr32(dev, NV40_PMC_BACKLIGHT,
  53. (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK));
  54. return 0;
  55. }
  56. static const struct backlight_ops nv40_bl_ops = {
  57. .options = BL_CORE_SUSPENDRESUME,
  58. .get_brightness = nv40_get_intensity,
  59. .update_status = nv40_set_intensity,
  60. };
  61. static int
  62. nv40_backlight_init(struct drm_connector *connector)
  63. {
  64. struct drm_device *dev = connector->dev;
  65. struct drm_nouveau_private *dev_priv = dev->dev_private;
  66. struct backlight_properties props;
  67. struct backlight_device *bd;
  68. if (!(nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
  69. return 0;
  70. memset(&props, 0, sizeof(struct backlight_properties));
  71. props.type = BACKLIGHT_RAW;
  72. props.max_brightness = 31;
  73. bd = backlight_device_register("nv_backlight", &connector->kdev, dev,
  74. &nv40_bl_ops, &props);
  75. if (IS_ERR(bd))
  76. return PTR_ERR(bd);
  77. dev_priv->backlight = bd;
  78. bd->props.brightness = nv40_get_intensity(bd);
  79. backlight_update_status(bd);
  80. return 0;
  81. }
  82. static int
  83. nv50_get_intensity(struct backlight_device *bd)
  84. {
  85. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  86. struct drm_device *dev = nv_encoder->base.base.dev;
  87. int or = nv_encoder->or;
  88. return nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT + (or * 0x800));
  89. }
  90. static int
  91. nv50_set_intensity(struct backlight_device *bd)
  92. {
  93. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  94. struct drm_device *dev = nv_encoder->base.base.dev;
  95. int val = bd->props.brightness;
  96. int or = nv_encoder->or;
  97. nv_wr32(dev, NV50_PDISPLAY_SOR_BACKLIGHT + (or * 0x800),
  98. val | NV50_PDISPLAY_SOR_BACKLIGHT_ENABLE);
  99. return 0;
  100. }
  101. static const struct backlight_ops nv50_bl_ops = {
  102. .options = BL_CORE_SUSPENDRESUME,
  103. .get_brightness = nv50_get_intensity,
  104. .update_status = nv50_set_intensity,
  105. };
  106. static int
  107. nv50_backlight_init(struct drm_connector *connector)
  108. {
  109. struct drm_device *dev = connector->dev;
  110. struct drm_nouveau_private *dev_priv = dev->dev_private;
  111. struct nouveau_encoder *nv_encoder;
  112. struct backlight_properties props;
  113. struct backlight_device *bd;
  114. int or;
  115. nv_encoder = find_encoder(connector, OUTPUT_LVDS);
  116. if (!nv_encoder) {
  117. nv_encoder = find_encoder(connector, OUTPUT_DP);
  118. if (!nv_encoder)
  119. return -ENODEV;
  120. }
  121. or = nv_encoder->or;
  122. if (!nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT + (or * 0x800)))
  123. return 0;
  124. memset(&props, 0, sizeof(struct backlight_properties));
  125. props.type = BACKLIGHT_RAW;
  126. props.max_brightness = 1025;
  127. bd = backlight_device_register("nv_backlight", &connector->kdev,
  128. nv_encoder, &nv50_bl_ops, &props);
  129. if (IS_ERR(bd))
  130. return PTR_ERR(bd);
  131. dev_priv->backlight = bd;
  132. bd->props.brightness = nv50_get_intensity(bd);
  133. backlight_update_status(bd);
  134. return 0;
  135. }
  136. int
  137. nouveau_backlight_init(struct drm_device *dev)
  138. {
  139. struct drm_nouveau_private *dev_priv = dev->dev_private;
  140. struct drm_connector *connector;
  141. #ifdef CONFIG_ACPI
  142. if (acpi_video_backlight_support()) {
  143. NV_INFO(dev, "ACPI backlight interface available, "
  144. "not registering our own\n");
  145. return 0;
  146. }
  147. #endif
  148. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  149. if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS &&
  150. connector->connector_type != DRM_MODE_CONNECTOR_eDP)
  151. continue;
  152. switch (dev_priv->card_type) {
  153. case NV_40:
  154. return nv40_backlight_init(connector);
  155. case NV_50:
  156. return nv50_backlight_init(connector);
  157. default:
  158. break;
  159. }
  160. }
  161. return 0;
  162. }
  163. void
  164. nouveau_backlight_exit(struct drm_device *dev)
  165. {
  166. struct drm_nouveau_private *dev_priv = dev->dev_private;
  167. if (dev_priv->backlight) {
  168. backlight_device_unregister(dev_priv->backlight);
  169. dev_priv->backlight = NULL;
  170. }
  171. }