backlight.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Miscellaneous procedures for dealing with the PowerMac hardware.
  3. * Contains support for the backlight.
  4. *
  5. * Copyright (C) 2000 Benjamin Herrenschmidt
  6. * Copyright (C) 2006 Michael Hanselmann <linux-kernel@hansmi.ch>
  7. *
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/fb.h>
  11. #include <linux/backlight.h>
  12. #include <asm/prom.h>
  13. #include <asm/backlight.h>
  14. #define OLD_BACKLIGHT_MAX 15
  15. /* Protect the pmac_backlight variable */
  16. DEFINE_MUTEX(pmac_backlight_mutex);
  17. /* Main backlight storage
  18. *
  19. * Backlight drivers in this variable are required to have the "props"
  20. * attribute set and to have an update_status function.
  21. *
  22. * We can only store one backlight here, but since Apple laptops have only one
  23. * internal display, it doesn't matter. Other backlight drivers can be used
  24. * independently.
  25. *
  26. * Lock ordering:
  27. * pmac_backlight_mutex (global, main backlight)
  28. * pmac_backlight->sem (backlight class)
  29. */
  30. struct backlight_device *pmac_backlight;
  31. int pmac_has_backlight_type(const char *type)
  32. {
  33. struct device_node* bk_node = find_devices("backlight");
  34. if (bk_node) {
  35. const char *prop = get_property(bk_node,
  36. "backlight-control", NULL);
  37. if (prop && strncmp(prop, type, strlen(type)) == 0)
  38. return 1;
  39. }
  40. return 0;
  41. }
  42. int pmac_backlight_curve_lookup(struct fb_info *info, int value)
  43. {
  44. int level = (FB_BACKLIGHT_LEVELS - 1);
  45. if (info && info->bl_dev) {
  46. int i, max = 0;
  47. /* Look for biggest value */
  48. for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
  49. max = max((int)info->bl_curve[i], max);
  50. /* Look for nearest value */
  51. for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
  52. int diff = abs(info->bl_curve[i] - value);
  53. if (diff < max) {
  54. max = diff;
  55. level = i;
  56. }
  57. }
  58. }
  59. return level;
  60. }
  61. static void pmac_backlight_key(int direction)
  62. {
  63. mutex_lock(&pmac_backlight_mutex);
  64. if (pmac_backlight) {
  65. struct backlight_properties *props;
  66. int brightness;
  67. down(&pmac_backlight->sem);
  68. props = pmac_backlight->props;
  69. brightness = props->brightness +
  70. ((direction?-1:1) * (props->max_brightness / 15));
  71. if (brightness < 0)
  72. brightness = 0;
  73. else if (brightness > props->max_brightness)
  74. brightness = props->max_brightness;
  75. props->brightness = brightness;
  76. props->update_status(pmac_backlight);
  77. up(&pmac_backlight->sem);
  78. }
  79. mutex_unlock(&pmac_backlight_mutex);
  80. }
  81. void pmac_backlight_key_up()
  82. {
  83. pmac_backlight_key(0);
  84. }
  85. void pmac_backlight_key_down()
  86. {
  87. pmac_backlight_key(1);
  88. }
  89. int pmac_backlight_set_legacy_brightness(int brightness)
  90. {
  91. int error = -ENXIO;
  92. mutex_lock(&pmac_backlight_mutex);
  93. if (pmac_backlight) {
  94. struct backlight_properties *props;
  95. down(&pmac_backlight->sem);
  96. props = pmac_backlight->props;
  97. props->brightness = brightness *
  98. (props->max_brightness + 1) /
  99. (OLD_BACKLIGHT_MAX + 1);
  100. if (props->brightness > props->max_brightness)
  101. props->brightness = props->max_brightness;
  102. else if (props->brightness < 0)
  103. props->brightness = 0;
  104. props->update_status(pmac_backlight);
  105. up(&pmac_backlight->sem);
  106. error = 0;
  107. }
  108. mutex_unlock(&pmac_backlight_mutex);
  109. return error;
  110. }
  111. int pmac_backlight_get_legacy_brightness()
  112. {
  113. int result = -ENXIO;
  114. mutex_lock(&pmac_backlight_mutex);
  115. if (pmac_backlight) {
  116. struct backlight_properties *props;
  117. down(&pmac_backlight->sem);
  118. props = pmac_backlight->props;
  119. result = props->brightness *
  120. (OLD_BACKLIGHT_MAX + 1) /
  121. (props->max_brightness + 1);
  122. up(&pmac_backlight->sem);
  123. }
  124. mutex_unlock(&pmac_backlight_mutex);
  125. return result;
  126. }