nv_backlight.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 const 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 backlight_properties props;
  80. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  81. struct backlight_device *bd;
  82. char name[12];
  83. if (!par->FlatPanel)
  84. return;
  85. #ifdef CONFIG_PMAC_BACKLIGHT
  86. if (!machine_is(powermac) ||
  87. !pmac_has_backlight_type("mnca"))
  88. return;
  89. #endif
  90. snprintf(name, sizeof(name), "nvidiabl%d", info->node);
  91. memset(&props, 0, sizeof(struct backlight_properties));
  92. props.type = BACKLIGHT_RAW;
  93. props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
  94. bd = backlight_device_register(name, info->dev, par, &nvidia_bl_ops,
  95. &props);
  96. if (IS_ERR(bd)) {
  97. info->bl_dev = NULL;
  98. printk(KERN_WARNING "nvidia: Backlight registration failed\n");
  99. goto error;
  100. }
  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. bd->props.brightness = bd->props.max_brightness;
  106. bd->props.power = FB_BLANK_UNBLANK;
  107. backlight_update_status(bd);
  108. printk("nvidia: Backlight initialized (%s)\n", name);
  109. return;
  110. error:
  111. return;
  112. }
  113. void nvidia_bl_exit(struct nvidia_par *par)
  114. {
  115. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  116. struct backlight_device *bd = info->bl_dev;
  117. backlight_device_unregister(bd);
  118. printk("nvidia: Backlight unloaded\n");
  119. }