nv_backlight.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Backlight code for nVidia based graphic cards
  4. *
  5. * Copyright 2004 Antonino Daplas <[email protected]>
  6. * Copyright (c) 2006 Michael Hanselmann <[email protected]>
  7. */
  8. #include <linux/backlight.h>
  9. #include <linux/fb.h>
  10. #include <linux/pci.h>
  11. #ifdef CONFIG_PMAC_BACKLIGHT
  12. #include <asm/backlight.h>
  13. #endif
  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 = bl_get_data(bd);
  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 const struct backlight_ops nvidia_bl_ops = {
  67. .update_status = nvidia_bl_update_status,
  68. };
  69. void nvidia_bl_init(struct nvidia_par *par)
  70. {
  71. struct backlight_properties props;
  72. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  73. struct backlight_device *bd;
  74. char name[12];
  75. if (!par->FlatPanel)
  76. return;
  77. #ifdef CONFIG_PMAC_BACKLIGHT
  78. if (!machine_is(powermac) ||
  79. !pmac_has_backlight_type("mnca"))
  80. return;
  81. #endif
  82. snprintf(name, sizeof(name), "nvidiabl%d", info->node);
  83. memset(&props, 0, sizeof(struct backlight_properties));
  84. props.type = BACKLIGHT_RAW;
  85. props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
  86. bd = backlight_device_register(name, info->dev, par, &nvidia_bl_ops,
  87. &props);
  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.brightness = bd->props.max_brightness;
  98. bd->props.power = FB_BLANK_UNBLANK;
  99. backlight_update_status(bd);
  100. printk("nvidia: Backlight initialized (%s)\n", name);
  101. error:
  102. return;
  103. }
  104. void nvidia_bl_exit(struct nvidia_par *par)
  105. {
  106. struct fb_info *info = pci_get_drvdata(par->pci_dev);
  107. struct backlight_device *bd = info->bl_dev;
  108. backlight_device_unregister(bd);
  109. printk("nvidia: Backlight unloaded\n");
  110. }