nouveau_backlight.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_PDISP_SOR_PWM_CTL(or));
  91. val &= NV50_PDISP_SOR_PWM_CTL_VAL;
  92. return ((val * 100) + (div / 2)) / div;
  93. }
  94. static int
  95. nv50_set_intensity(struct backlight_device *bd)
  96. {
  97. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  98. struct drm_device *dev = nv_encoder->base.base.dev;
  99. int or = nv_encoder->or;
  100. u32 div = 1025;
  101. u32 val = (bd->props.brightness * div) / 100;
  102. nv_wr32(dev, NV50_PDISP_SOR_PWM_CTL(or),
  103. NV50_PDISP_SOR_PWM_CTL_NEW | val);
  104. return 0;
  105. }
  106. static const struct backlight_ops nv50_bl_ops = {
  107. .options = BL_CORE_SUSPENDRESUME,
  108. .get_brightness = nv50_get_intensity,
  109. .update_status = nv50_set_intensity,
  110. };
  111. static int
  112. nva3_get_intensity(struct backlight_device *bd)
  113. {
  114. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  115. struct drm_device *dev = nv_encoder->base.base.dev;
  116. int or = nv_encoder->or;
  117. u32 div, val;
  118. div = nv_rd32(dev, NV50_PDISP_SOR_PWM_DIV(or));
  119. val = nv_rd32(dev, NV50_PDISP_SOR_PWM_CTL(or));
  120. val &= NVA3_PDISP_SOR_PWM_CTL_VAL;
  121. if (div && div >= val)
  122. return ((val * 100) + (div / 2)) / div;
  123. return 100;
  124. }
  125. static int
  126. nva3_set_intensity(struct backlight_device *bd)
  127. {
  128. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  129. struct drm_device *dev = nv_encoder->base.base.dev;
  130. int or = nv_encoder->or;
  131. u32 div, val;
  132. div = nv_rd32(dev, NV50_PDISP_SOR_PWM_DIV(or));
  133. val = (bd->props.brightness * div) / 100;
  134. if (div) {
  135. nv_wr32(dev, NV50_PDISP_SOR_PWM_CTL(or), val |
  136. NV50_PDISP_SOR_PWM_CTL_NEW |
  137. NVA3_PDISP_SOR_PWM_CTL_UNK);
  138. return 0;
  139. }
  140. return -EINVAL;
  141. }
  142. static const struct backlight_ops nva3_bl_ops = {
  143. .options = BL_CORE_SUSPENDRESUME,
  144. .get_brightness = nva3_get_intensity,
  145. .update_status = nva3_set_intensity,
  146. };
  147. static int
  148. nv50_backlight_init(struct drm_connector *connector)
  149. {
  150. struct drm_device *dev = connector->dev;
  151. struct drm_nouveau_private *dev_priv = dev->dev_private;
  152. struct nouveau_encoder *nv_encoder;
  153. struct backlight_properties props;
  154. struct backlight_device *bd;
  155. const struct backlight_ops *ops;
  156. nv_encoder = find_encoder(connector, OUTPUT_LVDS);
  157. if (!nv_encoder) {
  158. nv_encoder = find_encoder(connector, OUTPUT_DP);
  159. if (!nv_encoder)
  160. return -ENODEV;
  161. }
  162. if (!nv_rd32(dev, NV50_PDISP_SOR_PWM_CTL(nv_encoder->or)))
  163. return 0;
  164. if (dev_priv->chipset <= 0xa0 ||
  165. dev_priv->chipset == 0xaa ||
  166. dev_priv->chipset == 0xac)
  167. ops = &nv50_bl_ops;
  168. else
  169. ops = &nva3_bl_ops;
  170. memset(&props, 0, sizeof(struct backlight_properties));
  171. props.type = BACKLIGHT_RAW;
  172. props.max_brightness = 100;
  173. bd = backlight_device_register("nv_backlight", &connector->kdev,
  174. nv_encoder, ops, &props);
  175. if (IS_ERR(bd))
  176. return PTR_ERR(bd);
  177. dev_priv->backlight = bd;
  178. bd->props.brightness = bd->ops->get_brightness(bd);
  179. backlight_update_status(bd);
  180. return 0;
  181. }
  182. int
  183. nouveau_backlight_init(struct drm_device *dev)
  184. {
  185. struct drm_nouveau_private *dev_priv = dev->dev_private;
  186. struct drm_connector *connector;
  187. #ifdef CONFIG_ACPI
  188. if (acpi_video_backlight_support()) {
  189. NV_INFO(dev, "ACPI backlight interface available, "
  190. "not registering our own\n");
  191. return 0;
  192. }
  193. #endif
  194. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  195. if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS &&
  196. connector->connector_type != DRM_MODE_CONNECTOR_eDP)
  197. continue;
  198. switch (dev_priv->card_type) {
  199. case NV_40:
  200. return nv40_backlight_init(connector);
  201. case NV_50:
  202. return nv50_backlight_init(connector);
  203. default:
  204. break;
  205. }
  206. }
  207. return 0;
  208. }
  209. void
  210. nouveau_backlight_exit(struct drm_device *dev)
  211. {
  212. struct drm_nouveau_private *dev_priv = dev->dev_private;
  213. if (dev_priv->backlight) {
  214. backlight_device_unregister(dev_priv->backlight);
  215. dev_priv->backlight = NULL;
  216. }
  217. }