nv_backlight.c 3.4 KB

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