via-pmu-backlight.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Backlight code for via-pmu
  3. *
  4. * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
  5. * Copyright (C) 2001-2002 Benjamin Herrenschmidt
  6. * Copyright (C) 2006 Michael Hanselmann <linux-kernel@hansmi.ch>
  7. *
  8. */
  9. #include <asm/ptrace.h>
  10. #include <linux/adb.h>
  11. #include <linux/pmu.h>
  12. #include <asm/backlight.h>
  13. #include <asm/prom.h>
  14. #define MAX_PMU_LEVEL 0xFF
  15. static const struct backlight_ops pmu_backlight_data;
  16. static DEFINE_SPINLOCK(pmu_backlight_lock);
  17. static int sleeping, uses_pmu_bl;
  18. static u8 bl_curve[FB_BACKLIGHT_LEVELS];
  19. static void pmu_backlight_init_curve(u8 off, u8 min, u8 max)
  20. {
  21. int i, flat, count, range = (max - min);
  22. bl_curve[0] = off;
  23. for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
  24. bl_curve[flat] = min;
  25. count = FB_BACKLIGHT_LEVELS * 15 / 16;
  26. for (i = 0; i < count; ++i)
  27. bl_curve[flat + i] = min + (range * (i + 1) / count);
  28. }
  29. static int pmu_backlight_curve_lookup(int value)
  30. {
  31. int level = (FB_BACKLIGHT_LEVELS - 1);
  32. int i, max = 0;
  33. /* Look for biggest value */
  34. for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
  35. max = max((int)bl_curve[i], max);
  36. /* Look for nearest value */
  37. for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
  38. int diff = abs(bl_curve[i] - value);
  39. if (diff < max) {
  40. max = diff;
  41. level = i;
  42. }
  43. }
  44. return level;
  45. }
  46. static int pmu_backlight_get_level_brightness(int level)
  47. {
  48. int pmulevel;
  49. /* Get and convert the value */
  50. pmulevel = bl_curve[level] * FB_BACKLIGHT_MAX / MAX_PMU_LEVEL;
  51. if (pmulevel < 0)
  52. pmulevel = 0;
  53. else if (pmulevel > MAX_PMU_LEVEL)
  54. pmulevel = MAX_PMU_LEVEL;
  55. return pmulevel;
  56. }
  57. static int __pmu_backlight_update_status(struct backlight_device *bd)
  58. {
  59. struct adb_request req;
  60. int level = bd->props.brightness;
  61. if (bd->props.power != FB_BLANK_UNBLANK ||
  62. bd->props.fb_blank != FB_BLANK_UNBLANK)
  63. level = 0;
  64. if (level > 0) {
  65. int pmulevel = pmu_backlight_get_level_brightness(level);
  66. pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT, pmulevel);
  67. pmu_wait_complete(&req);
  68. pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
  69. PMU_POW_BACKLIGHT | PMU_POW_ON);
  70. pmu_wait_complete(&req);
  71. } else {
  72. pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
  73. PMU_POW_BACKLIGHT | PMU_POW_OFF);
  74. pmu_wait_complete(&req);
  75. }
  76. return 0;
  77. }
  78. static int pmu_backlight_update_status(struct backlight_device *bd)
  79. {
  80. unsigned long flags;
  81. int rc = 0;
  82. spin_lock_irqsave(&pmu_backlight_lock, flags);
  83. /* Don't update brightness when sleeping */
  84. if (!sleeping)
  85. rc = __pmu_backlight_update_status(bd);
  86. spin_unlock_irqrestore(&pmu_backlight_lock, flags);
  87. return rc;
  88. }
  89. static const struct backlight_ops pmu_backlight_data = {
  90. .update_status = pmu_backlight_update_status,
  91. };
  92. #ifdef CONFIG_PM
  93. void pmu_backlight_set_sleep(int sleep)
  94. {
  95. unsigned long flags;
  96. spin_lock_irqsave(&pmu_backlight_lock, flags);
  97. sleeping = sleep;
  98. if (pmac_backlight && uses_pmu_bl) {
  99. if (sleep) {
  100. struct adb_request req;
  101. pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
  102. PMU_POW_BACKLIGHT | PMU_POW_OFF);
  103. pmu_wait_complete(&req);
  104. } else
  105. __pmu_backlight_update_status(pmac_backlight);
  106. }
  107. spin_unlock_irqrestore(&pmu_backlight_lock, flags);
  108. }
  109. #endif /* CONFIG_PM */
  110. void __init pmu_backlight_init()
  111. {
  112. struct backlight_properties props;
  113. struct backlight_device *bd;
  114. char name[10];
  115. int level, autosave;
  116. /* Special case for the old PowerBook since I can't test on it */
  117. autosave =
  118. of_machine_is_compatible("AAPL,3400/2400") ||
  119. of_machine_is_compatible("AAPL,3500");
  120. if (!autosave &&
  121. !pmac_has_backlight_type("pmu") &&
  122. !of_machine_is_compatible("AAPL,PowerBook1998") &&
  123. !of_machine_is_compatible("PowerBook1,1"))
  124. return;
  125. snprintf(name, sizeof(name), "pmubl");
  126. memset(&props, 0, sizeof(struct backlight_properties));
  127. props.type = BACKLIGHT_PLATFORM;
  128. props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
  129. bd = backlight_device_register(name, NULL, NULL, &pmu_backlight_data,
  130. &props);
  131. if (IS_ERR(bd)) {
  132. printk(KERN_ERR "PMU Backlight registration failed\n");
  133. return;
  134. }
  135. uses_pmu_bl = 1;
  136. pmu_backlight_init_curve(0x7F, 0x46, 0x0E);
  137. level = bd->props.max_brightness;
  138. if (autosave) {
  139. /* read autosaved value if available */
  140. struct adb_request req;
  141. pmu_request(&req, NULL, 2, 0xd9, 0);
  142. pmu_wait_complete(&req);
  143. level = pmu_backlight_curve_lookup(
  144. (req.reply[0] >> 4) *
  145. bd->props.max_brightness / 15);
  146. }
  147. bd->props.brightness = level;
  148. bd->props.power = FB_BLANK_UNBLANK;
  149. backlight_update_status(bd);
  150. printk(KERN_INFO "PMU Backlight initialized (%s)\n", name);
  151. }