corgi_bl.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Backlight Driver for Sharp Corgi
  3. *
  4. * Copyright (c) 2004-2005 Richard Purdie
  5. *
  6. * Based on Sharp's 2.4 Backlight Driver
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/device.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/fb.h>
  19. #include <linux/backlight.h>
  20. #include <asm/arch-pxa/corgi.h>
  21. #include <asm/hardware/scoop.h>
  22. #define CORGI_MAX_INTENSITY 0x3e
  23. #define CORGI_DEFAULT_INTENSITY 0x1f
  24. #define CORGI_LIMIT_MASK 0x0b
  25. static int corgibl_powermode = FB_BLANK_UNBLANK;
  26. static int current_intensity = 0;
  27. static int corgibl_limit = 0;
  28. static spinlock_t bl_lock = SPIN_LOCK_UNLOCKED;
  29. static void corgibl_send_intensity(int intensity)
  30. {
  31. unsigned long flags;
  32. void (*corgi_kick_batt)(void);
  33. if (corgibl_powermode != FB_BLANK_UNBLANK) {
  34. intensity = 0;
  35. } else {
  36. if (corgibl_limit)
  37. intensity &= CORGI_LIMIT_MASK;
  38. }
  39. spin_lock_irqsave(&bl_lock, flags);
  40. corgibl_mach_set_intensity(intensity);
  41. spin_unlock_irqrestore(&bl_lock, flags);
  42. }
  43. static void corgibl_blank(int blank)
  44. {
  45. switch(blank) {
  46. case FB_BLANK_NORMAL:
  47. case FB_BLANK_VSYNC_SUSPEND:
  48. case FB_BLANK_HSYNC_SUSPEND:
  49. case FB_BLANK_POWERDOWN:
  50. if (corgibl_powermode == FB_BLANK_UNBLANK) {
  51. corgibl_send_intensity(0);
  52. corgibl_powermode = blank;
  53. }
  54. break;
  55. case FB_BLANK_UNBLANK:
  56. if (corgibl_powermode != FB_BLANK_UNBLANK) {
  57. corgibl_powermode = blank;
  58. corgibl_send_intensity(current_intensity);
  59. }
  60. break;
  61. }
  62. }
  63. #ifdef CONFIG_PM
  64. static int corgibl_suspend(struct device *dev, pm_message_t state, u32 level)
  65. {
  66. if (level == SUSPEND_POWER_DOWN)
  67. corgibl_blank(FB_BLANK_POWERDOWN);
  68. return 0;
  69. }
  70. static int corgibl_resume(struct device *dev, u32 level)
  71. {
  72. if (level == RESUME_POWER_ON)
  73. corgibl_blank(FB_BLANK_UNBLANK);
  74. return 0;
  75. }
  76. #else
  77. #define corgibl_suspend NULL
  78. #define corgibl_resume NULL
  79. #endif
  80. static int corgibl_set_power(struct backlight_device *bd, int state)
  81. {
  82. corgibl_blank(state);
  83. return 0;
  84. }
  85. static int corgibl_get_power(struct backlight_device *bd)
  86. {
  87. return corgibl_powermode;
  88. }
  89. static int corgibl_set_intensity(struct backlight_device *bd, int intensity)
  90. {
  91. if (intensity > corgibl_data.max_brightness)
  92. intensity = corgibl_data.max_brightness;
  93. corgibl_send_intensity(intensity);
  94. current_intensity=intensity;
  95. return 0;
  96. }
  97. static int corgibl_get_intensity(struct backlight_device *bd)
  98. {
  99. return current_intensity;
  100. }
  101. /*
  102. * Called when the battery is low to limit the backlight intensity.
  103. * If limit==0 clear any limit, otherwise limit the intensity
  104. */
  105. void corgibl_limit_intensity(int limit)
  106. {
  107. corgibl_limit = (limit ? 1 : 0);
  108. corgibl_send_intensity(current_intensity);
  109. }
  110. EXPORT_SYMBOL(corgibl_limit_intensity);
  111. static struct backlight_properties corgibl_data = {
  112. .owner = THIS_MODULE,
  113. .get_power = corgibl_get_power,
  114. .set_power = corgibl_set_power,
  115. .get_brightness = corgibl_get_intensity,
  116. .set_brightness = corgibl_set_intensity,
  117. };
  118. static struct backlight_device *corgi_backlight_device;
  119. static int __init corgibl_probe(struct device *dev)
  120. {
  121. struct corgibl_machinfo *machinfo = dev->platform_data;
  122. corgibl_data.max_brightness = machinfo->max_intensity;
  123. corgibl_mach_set_intensity = machinfo->set_bl_intensity;
  124. corgi_backlight_device = backlight_device_register ("corgi-bl",
  125. NULL, &corgibl_data);
  126. if (IS_ERR (corgi_backlight_device))
  127. return PTR_ERR (corgi_backlight_device);
  128. corgibl_set_intensity(NULL, CORGI_DEFAULT_INTENSITY);
  129. corgibl_limit_intensity(0);
  130. printk("Corgi Backlight Driver Initialized.\n");
  131. return 0;
  132. }
  133. static int corgibl_remove(struct device *dev)
  134. {
  135. backlight_device_unregister(corgi_backlight_device);
  136. corgibl_set_intensity(NULL, 0);
  137. printk("Corgi Backlight Driver Unloaded\n");
  138. return 0;
  139. }
  140. static struct device_driver corgibl_driver = {
  141. .name = "corgi-bl",
  142. .bus = &platform_bus_type,
  143. .probe = corgibl_probe,
  144. .remove = corgibl_remove,
  145. .suspend = corgibl_suspend,
  146. .resume = corgibl_resume,
  147. };
  148. static int __init corgibl_init(void)
  149. {
  150. return driver_register(&corgibl_driver);
  151. }
  152. static void __exit corgibl_exit(void)
  153. {
  154. driver_unregister(&corgibl_driver);
  155. }
  156. module_init(corgibl_init);
  157. module_exit(corgibl_exit);
  158. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  159. MODULE_DESCRIPTION("Corgi Backlight Driver");
  160. MODULE_LICENSE("GPLv2");