nouveau_backlight.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. u32 div = 1025;
  89. u32 val;
  90. val = nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT + (or * 0x800));
  91. return ((val * 100) + (div / 2)) / div;
  92. }
  93. static int
  94. nv50_set_intensity(struct backlight_device *bd)
  95. {
  96. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  97. struct drm_device *dev = nv_encoder->base.base.dev;
  98. int or = nv_encoder->or;
  99. u32 div = 1025;
  100. u32 val = (bd->props.brightness * div) / 100;
  101. nv_wr32(dev, NV50_PDISPLAY_SOR_BACKLIGHT + (or * 0x800),
  102. val | NV50_PDISPLAY_SOR_BACKLIGHT_ENABLE);
  103. return 0;
  104. }
  105. static const struct backlight_ops nv50_bl_ops = {
  106. .options = BL_CORE_SUSPENDRESUME,
  107. .get_brightness = nv50_get_intensity,
  108. .update_status = nv50_set_intensity,
  109. };
  110. static int
  111. nv50_backlight_init(struct drm_connector *connector)
  112. {
  113. struct drm_device *dev = connector->dev;
  114. struct drm_nouveau_private *dev_priv = dev->dev_private;
  115. struct nouveau_encoder *nv_encoder;
  116. struct backlight_properties props;
  117. struct backlight_device *bd;
  118. int or;
  119. nv_encoder = find_encoder(connector, OUTPUT_LVDS);
  120. if (!nv_encoder) {
  121. nv_encoder = find_encoder(connector, OUTPUT_DP);
  122. if (!nv_encoder)
  123. return -ENODEV;
  124. }
  125. or = nv_encoder->or;
  126. if (!nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT + (or * 0x800)))
  127. return 0;
  128. memset(&props, 0, sizeof(struct backlight_properties));
  129. props.type = BACKLIGHT_RAW;
  130. props.max_brightness = 100;
  131. bd = backlight_device_register("nv_backlight", &connector->kdev,
  132. nv_encoder, &nv50_bl_ops, &props);
  133. if (IS_ERR(bd))
  134. return PTR_ERR(bd);
  135. dev_priv->backlight = bd;
  136. bd->props.brightness = nv50_get_intensity(bd);
  137. backlight_update_status(bd);
  138. return 0;
  139. }
  140. int
  141. nouveau_backlight_init(struct drm_device *dev)
  142. {
  143. struct drm_nouveau_private *dev_priv = dev->dev_private;
  144. struct drm_connector *connector;
  145. #ifdef CONFIG_ACPI
  146. if (acpi_video_backlight_support()) {
  147. NV_INFO(dev, "ACPI backlight interface available, "
  148. "not registering our own\n");
  149. return 0;
  150. }
  151. #endif
  152. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  153. if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS &&
  154. connector->connector_type != DRM_MODE_CONNECTOR_eDP)
  155. continue;
  156. switch (dev_priv->card_type) {
  157. case NV_40:
  158. return nv40_backlight_init(connector);
  159. case NV_50:
  160. return nv50_backlight_init(connector);
  161. default:
  162. break;
  163. }
  164. }
  165. return 0;
  166. }
  167. void
  168. nouveau_backlight_exit(struct drm_device *dev)
  169. {
  170. struct drm_nouveau_private *dev_priv = dev->dev_private;
  171. if (dev_priv->backlight) {
  172. backlight_device_unregister(dev_priv->backlight);
  173. dev_priv->backlight = NULL;
  174. }
  175. }