nouveau_backlight.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "nouveau_drm.h"
  34. #include "nouveau_reg.h"
  35. #include "nouveau_encoder.h"
  36. static int
  37. nv40_get_intensity(struct backlight_device *bd)
  38. {
  39. struct nouveau_drm *drm = bl_get_data(bd);
  40. struct nouveau_device *device = nv_device(drm->device);
  41. int val = (nv_rd32(device, NV40_PMC_BACKLIGHT) &
  42. NV40_PMC_BACKLIGHT_MASK) >> 16;
  43. return val;
  44. }
  45. static int
  46. nv40_set_intensity(struct backlight_device *bd)
  47. {
  48. struct nouveau_drm *drm = bl_get_data(bd);
  49. struct nouveau_device *device = nv_device(drm->device);
  50. int val = bd->props.brightness;
  51. int reg = nv_rd32(device, NV40_PMC_BACKLIGHT);
  52. nv_wr32(device, 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 nouveau_drm *drm = nouveau_drm(connector->dev);
  65. struct nouveau_device *device = nv_device(drm->device);
  66. struct backlight_properties props;
  67. struct backlight_device *bd;
  68. if (!(nv_rd32(device, 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, drm,
  74. &nv40_bl_ops, &props);
  75. drm->backlight = bd;
  76. bd->props.brightness = nv40_get_intensity(bd);
  77. backlight_update_status(bd);
  78. return 0;
  79. }
  80. static int
  81. nv50_get_intensity(struct backlight_device *bd)
  82. {
  83. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  84. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  85. struct nouveau_device *device = nv_device(drm->device);
  86. int or = nv_encoder->or;
  87. u32 div = 1025;
  88. u32 val;
  89. val = nv_rd32(device, NV50_PDISP_SOR_PWM_CTL(or));
  90. val &= NV50_PDISP_SOR_PWM_CTL_VAL;
  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 nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  98. struct nouveau_device *device = nv_device(drm->device);
  99. int or = nv_encoder->or;
  100. u32 div = 1025;
  101. u32 val = (bd->props.brightness * div) / 100;
  102. nv_wr32(device, 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 nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  116. struct nouveau_device *device = nv_device(drm->device);
  117. int or = nv_encoder->or;
  118. u32 div, val;
  119. div = nv_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
  120. val = nv_rd32(device, NV50_PDISP_SOR_PWM_CTL(or));
  121. val &= NVA3_PDISP_SOR_PWM_CTL_VAL;
  122. if (div && div >= val)
  123. return ((val * 100) + (div / 2)) / div;
  124. return 100;
  125. }
  126. static int
  127. nva3_set_intensity(struct backlight_device *bd)
  128. {
  129. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  130. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  131. struct nouveau_device *device = nv_device(drm->device);
  132. int or = nv_encoder->or;
  133. u32 div, val;
  134. div = nv_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
  135. val = (bd->props.brightness * div) / 100;
  136. if (div) {
  137. nv_wr32(device, NV50_PDISP_SOR_PWM_CTL(or), val |
  138. NV50_PDISP_SOR_PWM_CTL_NEW |
  139. NVA3_PDISP_SOR_PWM_CTL_UNK);
  140. return 0;
  141. }
  142. return -EINVAL;
  143. }
  144. static const struct backlight_ops nva3_bl_ops = {
  145. .options = BL_CORE_SUSPENDRESUME,
  146. .get_brightness = nva3_get_intensity,
  147. .update_status = nva3_set_intensity,
  148. };
  149. static int
  150. nv50_backlight_init(struct drm_connector *connector)
  151. {
  152. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  153. struct nouveau_device *device = nv_device(drm->device);
  154. struct nouveau_encoder *nv_encoder;
  155. struct backlight_properties props;
  156. struct backlight_device *bd;
  157. const struct backlight_ops *ops;
  158. nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
  159. if (!nv_encoder) {
  160. nv_encoder = find_encoder(connector, DCB_OUTPUT_DP);
  161. if (!nv_encoder)
  162. return -ENODEV;
  163. }
  164. if (!nv_rd32(device, NV50_PDISP_SOR_PWM_CTL(nv_encoder->or)))
  165. return 0;
  166. if (device->chipset <= 0xa0 ||
  167. device->chipset == 0xaa ||
  168. device->chipset == 0xac)
  169. ops = &nv50_bl_ops;
  170. else
  171. ops = &nva3_bl_ops;
  172. memset(&props, 0, sizeof(struct backlight_properties));
  173. props.type = BACKLIGHT_RAW;
  174. props.max_brightness = 100;
  175. bd = backlight_device_register("nv_backlight", &connector->kdev,
  176. nv_encoder, ops, &props);
  177. if (IS_ERR(bd))
  178. return PTR_ERR(bd);
  179. drm->backlight = bd;
  180. bd->props.brightness = bd->ops->get_brightness(bd);
  181. backlight_update_status(bd);
  182. return 0;
  183. }
  184. int
  185. nouveau_backlight_init(struct drm_device *dev)
  186. {
  187. struct nouveau_drm *drm = nouveau_drm(dev);
  188. struct nouveau_device *device = nv_device(drm->device);
  189. struct drm_connector *connector;
  190. #ifdef CONFIG_ACPI
  191. if (acpi_video_backlight_support()) {
  192. NV_INFO(drm, "ACPI backlight interface available, "
  193. "not registering our own\n");
  194. return 0;
  195. }
  196. #endif
  197. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  198. if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS &&
  199. connector->connector_type != DRM_MODE_CONNECTOR_eDP)
  200. continue;
  201. switch (device->card_type) {
  202. case NV_40:
  203. return nv40_backlight_init(connector);
  204. case NV_50:
  205. return nv50_backlight_init(connector);
  206. default:
  207. break;
  208. }
  209. }
  210. return 0;
  211. }
  212. void
  213. nouveau_backlight_exit(struct drm_device *dev)
  214. {
  215. struct nouveau_drm *drm = nouveau_drm(dev);
  216. if (drm->backlight) {
  217. backlight_device_unregister(drm->backlight);
  218. drm->backlight = NULL;
  219. }
  220. }