backlight.c 5.4 KB

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