backlight.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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(void *data);
  19. static void pmac_backlight_set_legacy_worker(void *data);
  20. static DECLARE_WORK(pmac_backlight_key_work, pmac_backlight_key_worker, NULL);
  21. static DECLARE_WORK(pmac_backlight_set_legacy_work, pmac_backlight_set_legacy_worker, NULL);
  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. char *prop = get_property(bk_node, "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(void *data)
  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. down(&pmac_backlight->sem);
  87. props = pmac_backlight->props;
  88. brightness = props->brightness +
  89. ((pmac_backlight_key_queued?-1:1) *
  90. (props->max_brightness / 15));
  91. if (brightness < 0)
  92. brightness = 0;
  93. else if (brightness > props->max_brightness)
  94. brightness = props->max_brightness;
  95. props->brightness = brightness;
  96. props->update_status(pmac_backlight);
  97. up(&pmac_backlight->sem);
  98. }
  99. mutex_unlock(&pmac_backlight_mutex);
  100. }
  101. /* This function is called in interrupt context */
  102. void pmac_backlight_key(int direction)
  103. {
  104. if (atomic_read(&kernel_backlight_disabled))
  105. return;
  106. /* we can receive multiple interrupts here, but the scheduled work
  107. * will run only once, with the last value
  108. */
  109. pmac_backlight_key_queued = direction;
  110. schedule_work(&pmac_backlight_key_work);
  111. }
  112. static int __pmac_backlight_set_legacy_brightness(int brightness)
  113. {
  114. int error = -ENXIO;
  115. mutex_lock(&pmac_backlight_mutex);
  116. if (pmac_backlight) {
  117. struct backlight_properties *props;
  118. down(&pmac_backlight->sem);
  119. props = pmac_backlight->props;
  120. props->brightness = brightness *
  121. (props->max_brightness + 1) /
  122. (OLD_BACKLIGHT_MAX + 1);
  123. if (props->brightness > props->max_brightness)
  124. props->brightness = props->max_brightness;
  125. else if (props->brightness < 0)
  126. props->brightness = 0;
  127. props->update_status(pmac_backlight);
  128. up(&pmac_backlight->sem);
  129. error = 0;
  130. }
  131. mutex_unlock(&pmac_backlight_mutex);
  132. return error;
  133. }
  134. static void pmac_backlight_set_legacy_worker(void *data)
  135. {
  136. if (atomic_read(&kernel_backlight_disabled))
  137. return;
  138. __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
  139. }
  140. /* This function is called in interrupt context */
  141. void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
  142. if (atomic_read(&kernel_backlight_disabled))
  143. return;
  144. pmac_backlight_set_legacy_queued = brightness;
  145. schedule_work(&pmac_backlight_set_legacy_work);
  146. }
  147. int pmac_backlight_set_legacy_brightness(int brightness)
  148. {
  149. return __pmac_backlight_set_legacy_brightness(brightness);
  150. }
  151. int pmac_backlight_get_legacy_brightness()
  152. {
  153. int result = -ENXIO;
  154. mutex_lock(&pmac_backlight_mutex);
  155. if (pmac_backlight) {
  156. struct backlight_properties *props;
  157. down(&pmac_backlight->sem);
  158. props = pmac_backlight->props;
  159. result = props->brightness *
  160. (OLD_BACKLIGHT_MAX + 1) /
  161. (props->max_brightness + 1);
  162. up(&pmac_backlight->sem);
  163. }
  164. mutex_unlock(&pmac_backlight_mutex);
  165. return result;
  166. }
  167. void pmac_backlight_disable()
  168. {
  169. atomic_inc(&kernel_backlight_disabled);
  170. }
  171. void pmac_backlight_enable()
  172. {
  173. atomic_dec(&kernel_backlight_disabled);
  174. }
  175. EXPORT_SYMBOL_GPL(pmac_backlight);
  176. EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
  177. EXPORT_SYMBOL_GPL(pmac_has_backlight_type);