nv_backlight.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Backlight code for nVidia based graphic cards
  3. *
  4. * Copyright 2004 Antonino Daplas <adaplas@pol.net>
  5. * Copyright (c) 2006 Michael Hanselmann <linux-kernel@hansmi.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/backlight.h>
  12. #include <linux/fb.h>
  13. #include <linux/pci.h>
  14. #include "nv_local.h"
  15. #include "nv_type.h"
  16. #include "nv_proto.h"
  17. #ifdef CONFIG_PMAC_BACKLIGHT
  18. #include <asm/backlight.h>
  19. #include <asm/machdep.h>
  20. #endif
  21. /* We do not have any information about which values are allowed, thus
  22. * we used safe values.
  23. */
  24. #define MIN_LEVEL 0x158
  25. #define MAX_LEVEL 0x534
  26. static struct backlight_properties nvidia_bl_data;
  27. static int nvidia_bl_get_level_brightness(struct nvidia_par *par,
  28. int level)
  29. {
  30. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  31. int nlevel;
  32. /* Get and convert the value */
  33. mutex_lock(&info->bl_mutex);
  34. nlevel = info->bl_curve[level] * FB_BACKLIGHT_MAX / MAX_LEVEL;
  35. mutex_unlock(&info->bl_mutex);
  36. if (nlevel < 0)
  37. nlevel = 0;
  38. else if (nlevel < MIN_LEVEL)
  39. nlevel = MIN_LEVEL;
  40. else if (nlevel > MAX_LEVEL)
  41. nlevel = MAX_LEVEL;
  42. return nlevel;
  43. }
  44. static int nvidia_bl_update_status(struct backlight_device *bd)
  45. {
  46. struct nvidia_par *par = class_get_devdata(&bd->class_dev);
  47. u32 tmp_pcrt, tmp_pmc, fpcontrol;
  48. int level;
  49. if (!par->FlatPanel)
  50. return 0;
  51. if (bd->props->power != FB_BLANK_UNBLANK ||
  52. bd->props->fb_blank != FB_BLANK_UNBLANK)
  53. level = 0;
  54. else
  55. level = bd->props->brightness;
  56. tmp_pmc = NV_RD32(par->PMC, 0x10F0) & 0x0000FFFF;
  57. tmp_pcrt = NV_RD32(par->PCRTC0, 0x081C) & 0xFFFFFFFC;
  58. fpcontrol = NV_RD32(par->PRAMDAC, 0x0848) & 0xCFFFFFCC;
  59. if (level > 0) {
  60. tmp_pcrt |= 0x1;
  61. tmp_pmc |= (1 << 31); /* backlight bit */
  62. tmp_pmc |= nvidia_bl_get_level_brightness(par, level) << 16;
  63. fpcontrol |= par->fpSyncs;
  64. } else
  65. fpcontrol |= 0x20000022;
  66. NV_WR32(par->PCRTC0, 0x081C, tmp_pcrt);
  67. NV_WR32(par->PMC, 0x10F0, tmp_pmc);
  68. NV_WR32(par->PRAMDAC, 0x848, fpcontrol);
  69. return 0;
  70. }
  71. static int nvidia_bl_get_brightness(struct backlight_device *bd)
  72. {
  73. return bd->props->brightness;
  74. }
  75. static struct backlight_properties nvidia_bl_data = {
  76. .owner = THIS_MODULE,
  77. .get_brightness = nvidia_bl_get_brightness,
  78. .update_status = nvidia_bl_update_status,
  79. .max_brightness = (FB_BACKLIGHT_LEVELS - 1),
  80. };
  81. void nvidia_bl_init(struct nvidia_par *par)
  82. {
  83. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  84. struct backlight_device *bd;
  85. char name[12];
  86. if (!par->FlatPanel)
  87. return;
  88. #ifdef CONFIG_PMAC_BACKLIGHT
  89. if (!machine_is(powermac) ||
  90. !pmac_has_backlight_type("mnca"))
  91. return;
  92. #endif
  93. snprintf(name, sizeof(name), "nvidiabl%d", info->node);
  94. bd = backlight_device_register(name, par, &nvidia_bl_data);
  95. if (IS_ERR(bd)) {
  96. info->bl_dev = NULL;
  97. printk("nvidia: Backlight registration failed\n");
  98. goto error;
  99. }
  100. mutex_lock(&info->bl_mutex);
  101. info->bl_dev = bd;
  102. fb_bl_default_curve(info, 0,
  103. 0x158 * FB_BACKLIGHT_MAX / MAX_LEVEL,
  104. 0x534 * FB_BACKLIGHT_MAX / MAX_LEVEL);
  105. mutex_unlock(&info->bl_mutex);
  106. up(&bd->sem);
  107. bd->props->brightness = nvidia_bl_data.max_brightness;
  108. bd->props->power = FB_BLANK_UNBLANK;
  109. bd->props->update_status(bd);
  110. down(&bd->sem);
  111. #ifdef CONFIG_PMAC_BACKLIGHT
  112. mutex_lock(&pmac_backlight_mutex);
  113. if (!pmac_backlight)
  114. pmac_backlight = bd;
  115. mutex_unlock(&pmac_backlight_mutex);
  116. #endif
  117. printk("nvidia: Backlight initialized (%s)\n", name);
  118. return;
  119. error:
  120. return;
  121. }
  122. void nvidia_bl_exit(struct nvidia_par *par)
  123. {
  124. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  125. #ifdef CONFIG_PMAC_BACKLIGHT
  126. mutex_lock(&pmac_backlight_mutex);
  127. #endif
  128. mutex_lock(&info->bl_mutex);
  129. if (info->bl_dev) {
  130. #ifdef CONFIG_PMAC_BACKLIGHT
  131. if (pmac_backlight == info->bl_dev)
  132. pmac_backlight = NULL;
  133. #endif
  134. backlight_device_unregister(info->bl_dev);
  135. printk("nvidia: Backlight unloaded\n");
  136. }
  137. mutex_unlock(&info->bl_mutex);
  138. #ifdef CONFIG_PMAC_BACKLIGHT
  139. mutex_unlock(&pmac_backlight_mutex);
  140. #endif
  141. }