corgi_bl.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/mach-types.h>
  21. #include <asm/arch/sharpsl.h>
  22. #define CORGI_DEFAULT_INTENSITY 0x1f
  23. #define CORGI_LIMIT_MASK 0x0b
  24. static int corgibl_powermode = FB_BLANK_UNBLANK;
  25. static int current_intensity = 0;
  26. static int corgibl_limit = 0;
  27. static void (*corgibl_mach_set_intensity)(int intensity);
  28. static spinlock_t bl_lock = SPIN_LOCK_UNLOCKED;
  29. static struct backlight_properties corgibl_data;
  30. static void corgibl_send_intensity(int intensity)
  31. {
  32. unsigned long flags;
  33. void (*corgi_kick_batt)(void);
  34. if (corgibl_powermode != FB_BLANK_UNBLANK) {
  35. intensity = 0;
  36. } else {
  37. if (corgibl_limit)
  38. intensity &= CORGI_LIMIT_MASK;
  39. }
  40. spin_lock_irqsave(&bl_lock, flags);
  41. corgibl_mach_set_intensity(intensity);
  42. spin_unlock_irqrestore(&bl_lock, flags);
  43. }
  44. static void corgibl_blank(int blank)
  45. {
  46. switch(blank) {
  47. case FB_BLANK_NORMAL:
  48. case FB_BLANK_VSYNC_SUSPEND:
  49. case FB_BLANK_HSYNC_SUSPEND:
  50. case FB_BLANK_POWERDOWN:
  51. if (corgibl_powermode == FB_BLANK_UNBLANK) {
  52. corgibl_send_intensity(0);
  53. corgibl_powermode = blank;
  54. }
  55. break;
  56. case FB_BLANK_UNBLANK:
  57. if (corgibl_powermode != FB_BLANK_UNBLANK) {
  58. corgibl_powermode = blank;
  59. corgibl_send_intensity(current_intensity);
  60. }
  61. break;
  62. }
  63. }
  64. #ifdef CONFIG_PM
  65. static int corgibl_suspend(struct device *dev, pm_message_t state, u32 level)
  66. {
  67. if (level == SUSPEND_POWER_DOWN)
  68. corgibl_blank(FB_BLANK_POWERDOWN);
  69. return 0;
  70. }
  71. static int corgibl_resume(struct device *dev, u32 level)
  72. {
  73. if (level == RESUME_POWER_ON)
  74. corgibl_blank(FB_BLANK_UNBLANK);
  75. return 0;
  76. }
  77. #else
  78. #define corgibl_suspend NULL
  79. #define corgibl_resume NULL
  80. #endif
  81. static int corgibl_set_power(struct backlight_device *bd, int state)
  82. {
  83. corgibl_blank(state);
  84. return 0;
  85. }
  86. static int corgibl_get_power(struct backlight_device *bd)
  87. {
  88. return corgibl_powermode;
  89. }
  90. static int corgibl_set_intensity(struct backlight_device *bd, int intensity)
  91. {
  92. if (intensity > corgibl_data.max_brightness)
  93. intensity = corgibl_data.max_brightness;
  94. corgibl_send_intensity(intensity);
  95. current_intensity=intensity;
  96. return 0;
  97. }
  98. static int corgibl_get_intensity(struct backlight_device *bd)
  99. {
  100. return current_intensity;
  101. }
  102. /*
  103. * Called when the battery is low to limit the backlight intensity.
  104. * If limit==0 clear any limit, otherwise limit the intensity
  105. */
  106. void corgibl_limit_intensity(int limit)
  107. {
  108. corgibl_limit = (limit ? 1 : 0);
  109. corgibl_send_intensity(current_intensity);
  110. }
  111. EXPORT_SYMBOL(corgibl_limit_intensity);
  112. static struct backlight_properties corgibl_data = {
  113. .owner = THIS_MODULE,
  114. .get_power = corgibl_get_power,
  115. .set_power = corgibl_set_power,
  116. .get_brightness = corgibl_get_intensity,
  117. .set_brightness = corgibl_set_intensity,
  118. };
  119. static struct backlight_device *corgi_backlight_device;
  120. static int __init corgibl_probe(struct device *dev)
  121. {
  122. struct corgibl_machinfo *machinfo = dev->platform_data;
  123. corgibl_data.max_brightness = machinfo->max_intensity;
  124. corgibl_mach_set_intensity = machinfo->set_bl_intensity;
  125. corgi_backlight_device = backlight_device_register ("corgi-bl",
  126. NULL, &corgibl_data);
  127. if (IS_ERR (corgi_backlight_device))
  128. return PTR_ERR (corgi_backlight_device);
  129. corgibl_set_intensity(NULL, CORGI_DEFAULT_INTENSITY);
  130. corgibl_limit_intensity(0);
  131. printk("Corgi Backlight Driver Initialized.\n");
  132. return 0;
  133. }
  134. static int corgibl_remove(struct device *dev)
  135. {
  136. backlight_device_unregister(corgi_backlight_device);
  137. corgibl_set_intensity(NULL, 0);
  138. printk("Corgi Backlight Driver Unloaded\n");
  139. return 0;
  140. }
  141. static struct device_driver corgibl_driver = {
  142. .name = "corgi-bl",
  143. .bus = &platform_bus_type,
  144. .probe = corgibl_probe,
  145. .remove = corgibl_remove,
  146. .suspend = corgibl_suspend,
  147. .resume = corgibl_resume,
  148. };
  149. static int __init corgibl_init(void)
  150. {
  151. return driver_register(&corgibl_driver);
  152. }
  153. static void __exit corgibl_exit(void)
  154. {
  155. driver_unregister(&corgibl_driver);
  156. }
  157. module_init(corgibl_init);
  158. module_exit(corgibl_exit);
  159. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  160. MODULE_DESCRIPTION("Corgi Backlight Driver");
  161. MODULE_LICENSE("GPLv2");