backlight.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/config.h>
  10. #include <linux/kernel.h>
  11. #include <linux/fb.h>
  12. #include <linux/backlight.h>
  13. #include <asm/prom.h>
  14. #include <asm/backlight.h>
  15. #define OLD_BACKLIGHT_MAX 15
  16. /* Protect the pmac_backlight variable */
  17. DEFINE_MUTEX(pmac_backlight_mutex);
  18. /* Main backlight storage
  19. *
  20. * Backlight drivers in this variable are required to have the "props"
  21. * attribute set and to have an update_status function.
  22. *
  23. * We can only store one backlight here, but since Apple laptops have only one
  24. * internal display, it doesn't matter. Other backlight drivers can be used
  25. * independently.
  26. *
  27. * Lock ordering:
  28. * pmac_backlight_mutex (global, main backlight)
  29. * pmac_backlight->sem (backlight class)
  30. */
  31. struct backlight_device *pmac_backlight;
  32. int pmac_has_backlight_type(const char *type)
  33. {
  34. struct device_node* bk_node = find_devices("backlight");
  35. if (bk_node) {
  36. char *prop = get_property(bk_node, "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 / OLD_BACKLIGHT_MAX;
  99. props->update_status(pmac_backlight);
  100. up(&pmac_backlight->sem);
  101. error = 0;
  102. }
  103. mutex_unlock(&pmac_backlight_mutex);
  104. return error;
  105. }
  106. int pmac_backlight_get_legacy_brightness()
  107. {
  108. int result = -ENXIO;
  109. mutex_lock(&pmac_backlight_mutex);
  110. if (pmac_backlight) {
  111. struct backlight_properties *props;
  112. down(&pmac_backlight->sem);
  113. props = pmac_backlight->props;
  114. result = props->brightness *
  115. OLD_BACKLIGHT_MAX / props->max_brightness;
  116. up(&pmac_backlight->sem);
  117. }
  118. mutex_unlock(&pmac_backlight_mutex);
  119. return result;
  120. }