backlight.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 = of_find_node_by_name(NULL, "backlight");
  51. if (bk_node) {
  52. const char *prop = of_get_property(bk_node,
  53. "backlight-control", NULL);
  54. if (prop && strncmp(prop, type, strlen(type)) == 0) {
  55. of_node_put(bk_node);
  56. return 1;
  57. }
  58. of_node_put(bk_node);
  59. }
  60. return 0;
  61. }
  62. int pmac_backlight_curve_lookup(struct fb_info *info, int value)
  63. {
  64. int level = (FB_BACKLIGHT_LEVELS - 1);
  65. if (info && info->bl_dev) {
  66. int i, max = 0;
  67. /* Look for biggest value */
  68. for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
  69. max = max((int)info->bl_curve[i], max);
  70. /* Look for nearest value */
  71. for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
  72. int diff = abs(info->bl_curve[i] - value);
  73. if (diff < max) {
  74. max = diff;
  75. level = i;
  76. }
  77. }
  78. }
  79. return level;
  80. }
  81. static void pmac_backlight_key_worker(struct work_struct *work)
  82. {
  83. if (atomic_read(&kernel_backlight_disabled))
  84. return;
  85. mutex_lock(&pmac_backlight_mutex);
  86. if (pmac_backlight) {
  87. struct backlight_properties *props;
  88. int brightness;
  89. props = &pmac_backlight->props;
  90. brightness = props->brightness +
  91. ((pmac_backlight_key_queued?-1:1) *
  92. (props->max_brightness / 15));
  93. if (brightness < 0)
  94. brightness = 0;
  95. else if (brightness > props->max_brightness)
  96. brightness = props->max_brightness;
  97. props->brightness = brightness;
  98. backlight_update_status(pmac_backlight);
  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. 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. backlight_update_status(pmac_backlight);
  128. error = 0;
  129. }
  130. mutex_unlock(&pmac_backlight_mutex);
  131. return error;
  132. }
  133. static void pmac_backlight_set_legacy_worker(struct work_struct *work)
  134. {
  135. if (atomic_read(&kernel_backlight_disabled))
  136. return;
  137. __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
  138. }
  139. /* This function is called in interrupt context */
  140. void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
  141. if (atomic_read(&kernel_backlight_disabled))
  142. return;
  143. pmac_backlight_set_legacy_queued = brightness;
  144. schedule_work(&pmac_backlight_set_legacy_work);
  145. }
  146. int pmac_backlight_set_legacy_brightness(int brightness)
  147. {
  148. return __pmac_backlight_set_legacy_brightness(brightness);
  149. }
  150. int pmac_backlight_get_legacy_brightness()
  151. {
  152. int result = -ENXIO;
  153. mutex_lock(&pmac_backlight_mutex);
  154. if (pmac_backlight) {
  155. struct backlight_properties *props;
  156. props = &pmac_backlight->props;
  157. result = props->brightness *
  158. (OLD_BACKLIGHT_MAX + 1) /
  159. (props->max_brightness + 1);
  160. }
  161. mutex_unlock(&pmac_backlight_mutex);
  162. return result;
  163. }
  164. void pmac_backlight_disable()
  165. {
  166. atomic_inc(&kernel_backlight_disabled);
  167. }
  168. void pmac_backlight_enable()
  169. {
  170. atomic_dec(&kernel_backlight_disabled);
  171. }
  172. EXPORT_SYMBOL_GPL(pmac_backlight);
  173. EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
  174. EXPORT_SYMBOL_GPL(pmac_has_backlight_type);