via-pmu-backlight.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 struct backlight_properties pmu_backlight_data;
  16. static DEFINE_SPINLOCK(pmu_backlight_lock);
  17. static int sleeping;
  18. static u8 bl_curve[FB_BACKLIGHT_LEVELS];
  19. static void pmu_backlight_init_curve(u8 off, u8 min, u8 max)
  20. {
  21. unsigned 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. unsigned long flags;
  61. int level = bd->props->brightness;
  62. spin_lock_irqsave(&pmu_backlight_lock, flags);
  63. /* Don't update brightness when sleeping */
  64. if (sleeping)
  65. goto out;
  66. if (bd->props->power != FB_BLANK_UNBLANK ||
  67. bd->props->fb_blank != FB_BLANK_UNBLANK)
  68. level = 0;
  69. if (level > 0) {
  70. int pmulevel = pmu_backlight_get_level_brightness(level);
  71. pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT, pmulevel);
  72. pmu_wait_complete(&req);
  73. pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
  74. PMU_POW_BACKLIGHT | PMU_POW_ON);
  75. pmu_wait_complete(&req);
  76. } else {
  77. pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
  78. PMU_POW_BACKLIGHT | PMU_POW_OFF);
  79. pmu_wait_complete(&req);
  80. }
  81. out:
  82. spin_unlock_irqrestore(&pmu_backlight_lock, flags);
  83. return 0;
  84. }
  85. static int pmu_backlight_get_brightness(struct backlight_device *bd)
  86. {
  87. return bd->props->brightness;
  88. }
  89. static struct backlight_properties pmu_backlight_data = {
  90. .owner = THIS_MODULE,
  91. .get_brightness = pmu_backlight_get_brightness,
  92. .update_status = pmu_backlight_update_status,
  93. .max_brightness = (FB_BACKLIGHT_LEVELS - 1),
  94. };
  95. #ifdef CONFIG_PM
  96. void pmu_backlight_set_sleep(int sleep)
  97. {
  98. unsigned long flags;
  99. spin_lock_irqsave(&pmu_backlight_lock, flags);
  100. sleeping = sleep;
  101. spin_unlock_irqrestore(&pmu_backlight_lock, flags);
  102. }
  103. #endif /* CONFIG_PM */
  104. void __init pmu_backlight_init()
  105. {
  106. struct backlight_device *bd;
  107. char name[10];
  108. int level, autosave;
  109. /* Special case for the old PowerBook since I can't test on it */
  110. autosave =
  111. machine_is_compatible("AAPL,3400/2400") ||
  112. machine_is_compatible("AAPL,3500");
  113. if (!autosave &&
  114. !pmac_has_backlight_type("pmu") &&
  115. !machine_is_compatible("AAPL,PowerBook1998") &&
  116. !machine_is_compatible("PowerBook1,1"))
  117. return;
  118. snprintf(name, sizeof(name), "pmubl");
  119. bd = backlight_device_register(name, NULL, &pmu_backlight_data);
  120. if (IS_ERR(bd)) {
  121. printk("pmubl: Backlight registration failed\n");
  122. goto error;
  123. }
  124. pmu_backlight_init_curve(0x7F, 0x46, 0x0E);
  125. level = pmu_backlight_data.max_brightness;
  126. if (autosave) {
  127. /* read autosaved value if available */
  128. struct adb_request req;
  129. pmu_request(&req, NULL, 2, 0xd9, 0);
  130. pmu_wait_complete(&req);
  131. level = pmu_backlight_curve_lookup(
  132. (req.reply[0] >> 4) *
  133. pmu_backlight_data.max_brightness / 15);
  134. }
  135. down(&bd->sem);
  136. bd->props->brightness = level;
  137. bd->props->power = FB_BLANK_UNBLANK;
  138. bd->props->update_status(bd);
  139. up(&bd->sem);
  140. mutex_lock(&pmac_backlight_mutex);
  141. if (!pmac_backlight)
  142. pmac_backlight = bd;
  143. mutex_unlock(&pmac_backlight_mutex);
  144. printk("pmubl: Backlight initialized (%s)\n", name);
  145. return;
  146. error:
  147. return;
  148. }