nouveau_backlight.c 7.0 KB

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