nouveau_backlight.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 "nouveau_drm.h"
  33. #include "nouveau_reg.h"
  34. #include "nouveau_encoder.h"
  35. static int
  36. nv40_get_intensity(struct backlight_device *bd)
  37. {
  38. struct nouveau_drm *drm = bl_get_data(bd);
  39. struct nvif_object *device = &drm->device.object;
  40. int val = (nvif_rd32(device, NV40_PMC_BACKLIGHT) &
  41. NV40_PMC_BACKLIGHT_MASK) >> 16;
  42. return val;
  43. }
  44. static int
  45. nv40_set_intensity(struct backlight_device *bd)
  46. {
  47. struct nouveau_drm *drm = bl_get_data(bd);
  48. struct nvif_object *device = &drm->device.object;
  49. int val = bd->props.brightness;
  50. int reg = nvif_rd32(device, NV40_PMC_BACKLIGHT);
  51. nvif_wr32(device, NV40_PMC_BACKLIGHT,
  52. (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK));
  53. return 0;
  54. }
  55. static const struct backlight_ops nv40_bl_ops = {
  56. .options = BL_CORE_SUSPENDRESUME,
  57. .get_brightness = nv40_get_intensity,
  58. .update_status = nv40_set_intensity,
  59. };
  60. static int
  61. nv40_backlight_init(struct drm_connector *connector)
  62. {
  63. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  64. struct nvif_object *device = &drm->device.object;
  65. struct backlight_properties props;
  66. struct backlight_device *bd;
  67. if (!(nvif_rd32(device, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
  68. return 0;
  69. memset(&props, 0, sizeof(struct backlight_properties));
  70. props.type = BACKLIGHT_RAW;
  71. props.max_brightness = 31;
  72. bd = backlight_device_register("nv_backlight", connector->kdev, drm,
  73. &nv40_bl_ops, &props);
  74. if (IS_ERR(bd))
  75. return PTR_ERR(bd);
  76. drm->backlight = bd;
  77. bd->props.brightness = nv40_get_intensity(bd);
  78. backlight_update_status(bd);
  79. return 0;
  80. }
  81. static int
  82. nv50_get_intensity(struct backlight_device *bd)
  83. {
  84. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  85. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  86. struct nvif_object *device = &drm->device.object;
  87. int or = nv_encoder->or;
  88. u32 div = 1025;
  89. u32 val;
  90. val = nvif_rd32(device, 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 nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  99. struct nvif_object *device = &drm->device.object;
  100. int or = nv_encoder->or;
  101. u32 div = 1025;
  102. u32 val = (bd->props.brightness * div) / 100;
  103. nvif_wr32(device, NV50_PDISP_SOR_PWM_CTL(or),
  104. NV50_PDISP_SOR_PWM_CTL_NEW | val);
  105. return 0;
  106. }
  107. static const struct backlight_ops nv50_bl_ops = {
  108. .options = BL_CORE_SUSPENDRESUME,
  109. .get_brightness = nv50_get_intensity,
  110. .update_status = nv50_set_intensity,
  111. };
  112. static int
  113. nva3_get_intensity(struct backlight_device *bd)
  114. {
  115. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  116. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  117. struct nvif_object *device = &drm->device.object;
  118. int or = nv_encoder->or;
  119. u32 div, val;
  120. div = nvif_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
  121. val = nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(or));
  122. val &= NVA3_PDISP_SOR_PWM_CTL_VAL;
  123. if (div && div >= val)
  124. return ((val * 100) + (div / 2)) / div;
  125. return 100;
  126. }
  127. static int
  128. nva3_set_intensity(struct backlight_device *bd)
  129. {
  130. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  131. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  132. struct nvif_object *device = &drm->device.object;
  133. int or = nv_encoder->or;
  134. u32 div, val;
  135. div = nvif_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
  136. val = (bd->props.brightness * div) / 100;
  137. if (div) {
  138. nvif_wr32(device, NV50_PDISP_SOR_PWM_CTL(or), val |
  139. NV50_PDISP_SOR_PWM_CTL_NEW |
  140. NVA3_PDISP_SOR_PWM_CTL_UNK);
  141. return 0;
  142. }
  143. return -EINVAL;
  144. }
  145. static const struct backlight_ops nva3_bl_ops = {
  146. .options = BL_CORE_SUSPENDRESUME,
  147. .get_brightness = nva3_get_intensity,
  148. .update_status = nva3_set_intensity,
  149. };
  150. static int
  151. nv50_backlight_init(struct drm_connector *connector)
  152. {
  153. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  154. struct nvif_object *device = &drm->device.object;
  155. struct nouveau_encoder *nv_encoder;
  156. struct backlight_properties props;
  157. struct backlight_device *bd;
  158. const struct backlight_ops *ops;
  159. nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
  160. if (!nv_encoder) {
  161. nv_encoder = find_encoder(connector, DCB_OUTPUT_DP);
  162. if (!nv_encoder)
  163. return -ENODEV;
  164. }
  165. if (!nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(nv_encoder->or)))
  166. return 0;
  167. if (drm->device.info.chipset <= 0xa0 ||
  168. drm->device.info.chipset == 0xaa ||
  169. drm->device.info.chipset == 0xac)
  170. ops = &nv50_bl_ops;
  171. else
  172. ops = &nva3_bl_ops;
  173. memset(&props, 0, sizeof(struct backlight_properties));
  174. props.type = BACKLIGHT_RAW;
  175. props.max_brightness = 100;
  176. bd = backlight_device_register("nv_backlight", connector->kdev,
  177. nv_encoder, ops, &props);
  178. if (IS_ERR(bd))
  179. return PTR_ERR(bd);
  180. drm->backlight = bd;
  181. bd->props.brightness = bd->ops->get_brightness(bd);
  182. backlight_update_status(bd);
  183. return 0;
  184. }
  185. int
  186. nouveau_backlight_init(struct drm_device *dev)
  187. {
  188. struct nouveau_drm *drm = nouveau_drm(dev);
  189. struct nvif_device *device = &drm->device;
  190. struct drm_connector *connector;
  191. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  192. if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS &&
  193. connector->connector_type != DRM_MODE_CONNECTOR_eDP)
  194. continue;
  195. switch (device->info.family) {
  196. case NV_DEVICE_INFO_V0_CURIE:
  197. return nv40_backlight_init(connector);
  198. case NV_DEVICE_INFO_V0_TESLA:
  199. case NV_DEVICE_INFO_V0_FERMI:
  200. case NV_DEVICE_INFO_V0_KEPLER:
  201. return nv50_backlight_init(connector);
  202. default:
  203. break;
  204. }
  205. }
  206. return 0;
  207. }
  208. void
  209. nouveau_backlight_exit(struct drm_device *dev)
  210. {
  211. struct nouveau_drm *drm = nouveau_drm(dev);
  212. if (drm->backlight) {
  213. backlight_device_unregister(drm->backlight);
  214. drm->backlight = NULL;
  215. }
  216. }