backlight.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <linux/adb.h>
  13. #include <linux/pmu.h>
  14. #include <asm/atomic.h>
  15. #include <asm/prom.h>
  16. #include <asm/backlight.h>
  17. #define OLD_BACKLIGHT_MAX 15
  18. static void pmac_backlight_key_worker(struct work_struct *work);
  19. static void pmac_backlight_set_legacy_worker(struct work_struct *work);
  20. static DECLARE_WORK(pmac_backlight_key_work, pmac_backlight_key_worker);
  21. static DECLARE_WORK(pmac_backlight_set_legacy_work, pmac_backlight_set_legacy_worker);
  22. /* Although these variables are used in interrupt context, it makes no sense to
  23. * protect them. No user is able to produce enough key events per second and
  24. * notice the errors that might happen.
  25. */
  26. static int pmac_backlight_key_queued;
  27. static int pmac_backlight_set_legacy_queued;
  28. /* The via-pmu code allows the backlight to be grabbed, in which case the
  29. * in-kernel control of the brightness needs to be disabled. This should
  30. * only be used by really old PowerBooks.
  31. */
  32. static atomic_t kernel_backlight_disabled = ATOMIC_INIT(0);
  33. /* Protect the pmac_backlight variable below.
  34. You should hold this lock when using the pmac_backlight pointer to
  35. prevent its potential removal. */
  36. DEFINE_MUTEX(pmac_backlight_mutex);
  37. /* Main backlight storage
  38. *
  39. * Backlight drivers in this variable are required to have the "ops"
  40. * attribute set and to have an update_status function.
  41. *
  42. * We can only store one backlight here, but since Apple laptops have only one
  43. * internal display, it doesn't matter. Other backlight drivers can be used
  44. * independently.
  45. *
  46. */
  47. struct backlight_device *pmac_backlight;
  48. int pmac_has_backlight_type(const char *type)
  49. {
  50. struct device_node* bk_node = find_devices("backlight");
  51. if (bk_node) {
  52. const char *prop = get_property(bk_node,
  53. "backlight-control", NULL);
  54. if (prop && strncmp(prop, type, strlen(type)) == 0)
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. int pmac_backlight_curve_lookup(struct fb_info *info, int value)
  60. {
  61. int level = (FB_BACKLIGHT_LEVELS - 1);
  62. if (info && info->bl_dev) {
  63. int i, max = 0;
  64. /* Look for biggest value */
  65. for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
  66. max = max((int)info->bl_curve[i], max);
  67. /* Look for nearest value */
  68. for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
  69. int diff = abs(info->bl_curve[i] - value);
  70. if (diff < max) {
  71. max = diff;
  72. level = i;
  73. }
  74. }
  75. }
  76. return level;
  77. }
  78. static void pmac_backlight_key_worker(struct work_struct *work)
  79. {
  80. if (atomic_read(&kernel_backlight_disabled))
  81. return;
  82. mutex_lock(&pmac_backlight_mutex);
  83. if (pmac_backlight) {
  84. struct backlight_properties *props;
  85. int brightness;
  86. props = &pmac_backlight->props;
  87. brightness = props->brightness +
  88. ((pmac_backlight_key_queued?-1:1) *
  89. (props->max_brightness / 15));
  90. if (brightness < 0)
  91. brightness = 0;
  92. else if (brightness > props->max_brightness)
  93. brightness = props->max_brightness;
  94. props->brightness = brightness;
  95. backlight_update_status(pmac_backlight);
  96. }
  97. mutex_unlock(&pmac_backlight_mutex);
  98. }
  99. /* This function is called in interrupt context */
  100. void pmac_backlight_key(int direction)
  101. {
  102. if (atomic_read(&kernel_backlight_disabled))
  103. return;
  104. /* we can receive multiple interrupts here, but the scheduled work
  105. * will run only once, with the last value
  106. */
  107. pmac_backlight_key_queued = direction;
  108. schedule_work(&pmac_backlight_key_work);
  109. }
  110. static int __pmac_backlight_set_legacy_brightness(int brightness)
  111. {
  112. int error = -ENXIO;
  113. mutex_lock(&pmac_backlight_mutex);
  114. if (pmac_backlight) {
  115. struct backlight_properties *props;
  116. props = &pmac_backlight->props;
  117. props->brightness = brightness *
  118. (props->max_brightness + 1) /
  119. (OLD_BACKLIGHT_MAX + 1);
  120. if (props->brightness > props->max_brightness)
  121. props->brightness = props->max_brightness;
  122. else if (props->brightness < 0)
  123. props->brightness = 0;
  124. backlight_update_status(pmac_backlight);
  125. error = 0;
  126. }
  127. mutex_unlock(&pmac_backlight_mutex);
  128. return error;
  129. }
  130. static void pmac_backlight_set_legacy_worker(struct work_struct *work)
  131. {
  132. if (atomic_read(&kernel_backlight_disabled))
  133. return;
  134. __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
  135. }
  136. /* This function is called in interrupt context */
  137. void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
  138. if (atomic_read(&kernel_backlight_disabled))
  139. return;
  140. pmac_backlight_set_legacy_queued = brightness;
  141. schedule_work(&pmac_backlight_set_legacy_work);
  142. }
  143. int pmac_backlight_set_legacy_brightness(int brightness)
  144. {
  145. return __pmac_backlight_set_legacy_brightness(brightness);
  146. }
  147. int pmac_backlight_get_legacy_brightness()
  148. {
  149. int result = -ENXIO;
  150. mutex_lock(&pmac_backlight_mutex);
  151. if (pmac_backlight) {
  152. struct backlight_properties *props;
  153. props = &pmac_backlight->props;
  154. result = props->brightness *
  155. (OLD_BACKLIGHT_MAX + 1) /
  156. (props->max_brightness + 1);
  157. }
  158. mutex_unlock(&pmac_backlight_mutex);
  159. return result;
  160. }
  161. void pmac_backlight_disable()
  162. {
  163. atomic_inc(&kernel_backlight_disabled);
  164. }
  165. void pmac_backlight_enable()
  166. {
  167. atomic_dec(&kernel_backlight_disabled);
  168. }
  169. EXPORT_SYMBOL_GPL(pmac_backlight);
  170. EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
  171. EXPORT_SYMBOL_GPL(pmac_has_backlight_type);