nv_backlight.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #define LEVEL_STEP ((MAX_LEVEL - MIN_LEVEL) / FB_BACKLIGHT_MAX)
  27. static struct backlight_properties nvidia_bl_data;
  28. /* Call with fb_info->bl_mutex held */
  29. static int nvidia_bl_get_level_brightness(struct nvidia_par *par,
  30. int level)
  31. {
  32. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  33. int nlevel;
  34. /* Get and convert the value */
  35. nlevel = MIN_LEVEL + info->bl_curve[level] * LEVEL_STEP;
  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. /* Call with fb_info->bl_mutex held */
  45. static int __nvidia_bl_update_status(struct backlight_device *bd)
  46. {
  47. struct nvidia_par *par = class_get_devdata(&bd->class_dev);
  48. u32 tmp_pcrt, tmp_pmc, fpcontrol;
  49. int level;
  50. if (!par->FlatPanel)
  51. return 0;
  52. if (bd->props->power != FB_BLANK_UNBLANK ||
  53. bd->props->fb_blank != FB_BLANK_UNBLANK)
  54. level = 0;
  55. else
  56. level = bd->props->brightness;
  57. tmp_pmc = NV_RD32(par->PMC, 0x10F0) & 0x0000FFFF;
  58. tmp_pcrt = NV_RD32(par->PCRTC0, 0x081C) & 0xFFFFFFFC;
  59. fpcontrol = NV_RD32(par->PRAMDAC, 0x0848) & 0xCFFFFFCC;
  60. if (level > 0) {
  61. tmp_pcrt |= 0x1;
  62. tmp_pmc |= (1 << 31); /* backlight bit */
  63. tmp_pmc |= nvidia_bl_get_level_brightness(par, level) << 16;
  64. fpcontrol |= par->fpSyncs;
  65. } else
  66. fpcontrol |= 0x20000022;
  67. NV_WR32(par->PCRTC0, 0x081C, tmp_pcrt);
  68. NV_WR32(par->PMC, 0x10F0, tmp_pmc);
  69. NV_WR32(par->PRAMDAC, 0x848, fpcontrol);
  70. return 0;
  71. }
  72. static int nvidia_bl_update_status(struct backlight_device *bd)
  73. {
  74. struct nvidia_par *par = class_get_devdata(&bd->class_dev);
  75. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  76. int ret;
  77. mutex_lock(&info->bl_mutex);
  78. ret = __nvidia_bl_update_status(bd);
  79. mutex_unlock(&info->bl_mutex);
  80. return ret;
  81. }
  82. static int nvidia_bl_get_brightness(struct backlight_device *bd)
  83. {
  84. return bd->props->brightness;
  85. }
  86. static struct backlight_properties nvidia_bl_data = {
  87. .owner = THIS_MODULE,
  88. .get_brightness = nvidia_bl_get_brightness,
  89. .update_status = nvidia_bl_update_status,
  90. .max_brightness = (FB_BACKLIGHT_LEVELS - 1),
  91. };
  92. void nvidia_bl_set_power(struct fb_info *info, int power)
  93. {
  94. mutex_lock(&info->bl_mutex);
  95. if (info->bl_dev) {
  96. down(&info->bl_dev->sem);
  97. info->bl_dev->props->power = power;
  98. __nvidia_bl_update_status(info->bl_dev);
  99. up(&info->bl_dev->sem);
  100. }
  101. mutex_unlock(&info->bl_mutex);
  102. }
  103. void nvidia_bl_init(struct nvidia_par *par)
  104. {
  105. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  106. struct backlight_device *bd;
  107. char name[12];
  108. if (!par->FlatPanel)
  109. return;
  110. #ifdef CONFIG_PMAC_BACKLIGHT
  111. if (!machine_is(powermac) ||
  112. !pmac_has_backlight_type("mnca"))
  113. return;
  114. #endif
  115. snprintf(name, sizeof(name), "nvidiabl%d", info->node);
  116. bd = backlight_device_register(name, par, &nvidia_bl_data);
  117. if (IS_ERR(bd)) {
  118. info->bl_dev = NULL;
  119. printk(KERN_WARNING "nvidia: Backlight registration failed\n");
  120. goto error;
  121. }
  122. mutex_lock(&info->bl_mutex);
  123. info->bl_dev = bd;
  124. fb_bl_default_curve(info, 0,
  125. 0x158 * FB_BACKLIGHT_MAX / MAX_LEVEL,
  126. 0x534 * FB_BACKLIGHT_MAX / MAX_LEVEL);
  127. mutex_unlock(&info->bl_mutex);
  128. down(&bd->sem);
  129. bd->props->brightness = nvidia_bl_data.max_brightness;
  130. bd->props->power = FB_BLANK_UNBLANK;
  131. bd->props->update_status(bd);
  132. up(&bd->sem);
  133. #ifdef CONFIG_PMAC_BACKLIGHT
  134. mutex_lock(&pmac_backlight_mutex);
  135. if (!pmac_backlight)
  136. pmac_backlight = bd;
  137. mutex_unlock(&pmac_backlight_mutex);
  138. #endif
  139. printk("nvidia: Backlight initialized (%s)\n", name);
  140. return;
  141. error:
  142. return;
  143. }
  144. void nvidia_bl_exit(struct nvidia_par *par)
  145. {
  146. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  147. #ifdef CONFIG_PMAC_BACKLIGHT
  148. mutex_lock(&pmac_backlight_mutex);
  149. #endif
  150. mutex_lock(&info->bl_mutex);
  151. if (info->bl_dev) {
  152. #ifdef CONFIG_PMAC_BACKLIGHT
  153. if (pmac_backlight == info->bl_dev)
  154. pmac_backlight = NULL;
  155. #endif
  156. backlight_device_unregister(info->bl_dev);
  157. printk("nvidia: Backlight unloaded\n");
  158. }
  159. mutex_unlock(&info->bl_mutex);
  160. #ifdef CONFIG_PMAC_BACKLIGHT
  161. mutex_unlock(&pmac_backlight_mutex);
  162. #endif
  163. }