backlight.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 */
  34. DEFINE_MUTEX(pmac_backlight_mutex);
  35. /* Main backlight storage
  36. *
  37. * Backlight drivers in this variable are required to have the "props"
  38. * attribute set and to have an update_status function.
  39. *
  40. * We can only store one backlight here, but since Apple laptops have only one
  41. * internal display, it doesn't matter. Other backlight drivers can be used
  42. * independently.
  43. *
  44. * Lock ordering:
  45. * pmac_backlight_mutex (global, main backlight)
  46. * pmac_backlight->sem (backlight class)
  47. */
  48. struct backlight_device *pmac_backlight;
  49. int pmac_has_backlight_type(const char *type)
  50. {
  51. struct device_node* bk_node = find_devices("backlight");
  52. if (bk_node) {
  53. const char *prop = get_property(bk_node,
  54. "backlight-control", NULL);
  55. if (prop && strncmp(prop, type, strlen(type)) == 0)
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. int pmac_backlight_curve_lookup(struct fb_info *info, int value)
  61. {
  62. int level = (FB_BACKLIGHT_LEVELS - 1);
  63. if (info && info->bl_dev) {
  64. int i, max = 0;
  65. /* Look for biggest value */
  66. for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
  67. max = max((int)info->bl_curve[i], max);
  68. /* Look for nearest value */
  69. for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
  70. int diff = abs(info->bl_curve[i] - value);
  71. if (diff < max) {
  72. max = diff;
  73. level = i;
  74. }
  75. }
  76. }
  77. return level;
  78. }
  79. static void pmac_backlight_key_worker(struct work_struct *work)
  80. {
  81. if (atomic_read(&kernel_backlight_disabled))
  82. return;
  83. mutex_lock(&pmac_backlight_mutex);
  84. if (pmac_backlight) {
  85. struct backlight_properties *props;
  86. int brightness;
  87. down(&pmac_backlight->sem);
  88. props = pmac_backlight->props;
  89. brightness = props->brightness +
  90. ((pmac_backlight_key_queued?-1:1) *
  91. (props->max_brightness / 15));
  92. if (brightness < 0)
  93. brightness = 0;
  94. else if (brightness > props->max_brightness)
  95. brightness = props->max_brightness;
  96. props->brightness = brightness;
  97. props->update_status(pmac_backlight);
  98. up(&pmac_backlight->sem);
  99. }
  100. mutex_unlock(&pmac_backlight_mutex);
  101. }
  102. /* This function is called in interrupt context */
  103. void pmac_backlight_key(int direction)
  104. {
  105. if (atomic_read(&kernel_backlight_disabled))
  106. return;
  107. /* we can receive multiple interrupts here, but the scheduled work
  108. * will run only once, with the last value
  109. */
  110. pmac_backlight_key_queued = direction;
  111. schedule_work(&pmac_backlight_key_work);
  112. }
  113. static int __pmac_backlight_set_legacy_brightness(int brightness)
  114. {
  115. int error = -ENXIO;
  116. mutex_lock(&pmac_backlight_mutex);
  117. if (pmac_backlight) {
  118. struct backlight_properties *props;
  119. down(&pmac_backlight->sem);
  120. props = pmac_backlight->props;
  121. props->brightness = brightness *
  122. (props->max_brightness + 1) /
  123. (OLD_BACKLIGHT_MAX + 1);
  124. if (props->brightness > props->max_brightness)
  125. props->brightness = props->max_brightness;
  126. else if (props->brightness < 0)
  127. props->brightness = 0;
  128. props->update_status(pmac_backlight);
  129. up(&pmac_backlight->sem);
  130. error = 0;
  131. }
  132. mutex_unlock(&pmac_backlight_mutex);
  133. return error;
  134. }
  135. static void pmac_backlight_set_legacy_worker(struct work_struct *work)
  136. {
  137. if (atomic_read(&kernel_backlight_disabled))
  138. return;
  139. __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
  140. }
  141. /* This function is called in interrupt context */
  142. void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
  143. if (atomic_read(&kernel_backlight_disabled))
  144. return;
  145. pmac_backlight_set_legacy_queued = brightness;
  146. schedule_work(&pmac_backlight_set_legacy_work);
  147. }
  148. int pmac_backlight_set_legacy_brightness(int brightness)
  149. {
  150. return __pmac_backlight_set_legacy_brightness(brightness);
  151. }
  152. int pmac_backlight_get_legacy_brightness()
  153. {
  154. int result = -ENXIO;
  155. mutex_lock(&pmac_backlight_mutex);
  156. if (pmac_backlight) {
  157. struct backlight_properties *props;
  158. down(&pmac_backlight->sem);
  159. props = pmac_backlight->props;
  160. result = props->brightness *
  161. (OLD_BACKLIGHT_MAX + 1) /
  162. (props->max_brightness + 1);
  163. up(&pmac_backlight->sem);
  164. }
  165. mutex_unlock(&pmac_backlight_mutex);
  166. return result;
  167. }
  168. void pmac_backlight_disable()
  169. {
  170. atomic_inc(&kernel_backlight_disabled);
  171. }
  172. void pmac_backlight_enable()
  173. {
  174. atomic_dec(&kernel_backlight_disabled);
  175. }
  176. EXPORT_SYMBOL_GPL(pmac_backlight);
  177. EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
  178. EXPORT_SYMBOL_GPL(pmac_has_backlight_type);