nv_backlight.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifdef CONFIG_PMAC_BACKLIGHT
  15. #include <asm/backlight.h>
  16. #endif
  17. #include "nv_local.h"
  18. #include "nv_type.h"
  19. #include "nv_proto.h"
  20. /* We do not have any information about which values are allowed, thus
  21. * we used safe values.
  22. */
  23. #define MIN_LEVEL 0x158
  24. #define MAX_LEVEL 0x534
  25. #define LEVEL_STEP ((MAX_LEVEL - MIN_LEVEL) / FB_BACKLIGHT_MAX)
  26. static int nvidia_bl_get_level_brightness(struct nvidia_par *par,
  27. int level)
  28. {
  29. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  30. int nlevel;
  31. /* Get and convert the value */
  32. /* No locking of bl_curve since we read a single value */
  33. nlevel = MIN_LEVEL + info->bl_curve[level] * LEVEL_STEP;
  34. if (nlevel < 0)
  35. nlevel = 0;
  36. else if (nlevel < MIN_LEVEL)
  37. nlevel = MIN_LEVEL;
  38. else if (nlevel > MAX_LEVEL)
  39. nlevel = MAX_LEVEL;
  40. return nlevel;
  41. }
  42. static int nvidia_bl_update_status(struct backlight_device *bd)
  43. {
  44. struct nvidia_par *par = bl_get_data(bd);
  45. u32 tmp_pcrt, tmp_pmc, fpcontrol;
  46. int level;
  47. if (!par->FlatPanel)
  48. return 0;
  49. if (bd->props.power != FB_BLANK_UNBLANK ||
  50. bd->props.fb_blank != FB_BLANK_UNBLANK)
  51. level = 0;
  52. else
  53. level = bd->props.brightness;
  54. tmp_pmc = NV_RD32(par->PMC, 0x10F0) & 0x0000FFFF;
  55. tmp_pcrt = NV_RD32(par->PCRTC0, 0x081C) & 0xFFFFFFFC;
  56. fpcontrol = NV_RD32(par->PRAMDAC, 0x0848) & 0xCFFFFFCC;
  57. if (level > 0) {
  58. tmp_pcrt |= 0x1;
  59. tmp_pmc |= (1 << 31); /* backlight bit */
  60. tmp_pmc |= nvidia_bl_get_level_brightness(par, level) << 16;
  61. fpcontrol |= par->fpSyncs;
  62. } else
  63. fpcontrol |= 0x20000022;
  64. NV_WR32(par->PCRTC0, 0x081C, tmp_pcrt);
  65. NV_WR32(par->PMC, 0x10F0, tmp_pmc);
  66. NV_WR32(par->PRAMDAC, 0x848, fpcontrol);
  67. return 0;
  68. }
  69. static int nvidia_bl_get_brightness(struct backlight_device *bd)
  70. {
  71. return bd->props.brightness;
  72. }
  73. static struct backlight_ops nvidia_bl_ops = {
  74. .get_brightness = nvidia_bl_get_brightness,
  75. .update_status = nvidia_bl_update_status,
  76. };
  77. void nvidia_bl_init(struct nvidia_par *par)
  78. {
  79. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  80. struct backlight_device *bd;
  81. char name[12];
  82. if (!par->FlatPanel)
  83. return;
  84. #ifdef CONFIG_PMAC_BACKLIGHT
  85. if (!machine_is(powermac) ||
  86. !pmac_has_backlight_type("mnca"))
  87. return;
  88. #endif
  89. snprintf(name, sizeof(name), "nvidiabl%d", info->node);
  90. bd = backlight_device_register(name, info->dev, par, &nvidia_bl_ops);
  91. if (IS_ERR(bd)) {
  92. info->bl_dev = NULL;
  93. printk(KERN_WARNING "nvidia: Backlight registration failed\n");
  94. goto error;
  95. }
  96. info->bl_dev = bd;
  97. fb_bl_default_curve(info, 0,
  98. 0x158 * FB_BACKLIGHT_MAX / MAX_LEVEL,
  99. 0x534 * FB_BACKLIGHT_MAX / MAX_LEVEL);
  100. bd->props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
  101. bd->props.brightness = bd->props.max_brightness;
  102. bd->props.power = FB_BLANK_UNBLANK;
  103. backlight_update_status(bd);
  104. printk("nvidia: Backlight initialized (%s)\n", name);
  105. return;
  106. error:
  107. return;
  108. }
  109. void nvidia_bl_exit(struct nvidia_par *par)
  110. {
  111. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  112. struct backlight_device *bd = info->bl_dev;
  113. backlight_device_unregister(bd);
  114. printk("nvidia: Backlight unloaded\n");
  115. }