corgi_bl.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. /* Skip 0x20 as it will blank the display */
  40. if (intensity >= 0x20)
  41. intensity++;
  42. spin_lock_irqsave(&bl_lock, flags);
  43. /* Bits 0-4 are accessed via the SSP interface */
  44. corgi_ssp_blduty_set(intensity & 0x1f);
  45. /* Bit 5 is via SCOOP */
  46. if (intensity & 0x0020)
  47. set_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_BACKLIGHT_CONT);
  48. else
  49. reset_scoop_gpio(&corgiscoop_device.dev, CORGI_SCP_BACKLIGHT_CONT);
  50. spin_unlock_irqrestore(&bl_lock, flags);
  51. }
  52. static void corgibl_blank(int blank)
  53. {
  54. switch(blank) {
  55. case FB_BLANK_NORMAL:
  56. case FB_BLANK_VSYNC_SUSPEND:
  57. case FB_BLANK_HSYNC_SUSPEND:
  58. case FB_BLANK_POWERDOWN:
  59. if (corgibl_powermode == FB_BLANK_UNBLANK) {
  60. corgibl_send_intensity(0);
  61. corgibl_powermode = blank;
  62. }
  63. break;
  64. case FB_BLANK_UNBLANK:
  65. if (corgibl_powermode != FB_BLANK_UNBLANK) {
  66. corgibl_powermode = blank;
  67. corgibl_send_intensity(current_intensity);
  68. }
  69. break;
  70. }
  71. }
  72. #ifdef CONFIG_PM
  73. static int corgibl_suspend(struct device *dev, pm_message_t state, u32 level)
  74. {
  75. if (level == SUSPEND_POWER_DOWN)
  76. corgibl_blank(FB_BLANK_POWERDOWN);
  77. return 0;
  78. }
  79. static int corgibl_resume(struct device *dev, u32 level)
  80. {
  81. if (level == RESUME_POWER_ON)
  82. corgibl_blank(FB_BLANK_UNBLANK);
  83. return 0;
  84. }
  85. #else
  86. #define corgibl_suspend NULL
  87. #define corgibl_resume NULL
  88. #endif
  89. static int corgibl_set_power(struct backlight_device *bd, int state)
  90. {
  91. corgibl_blank(state);
  92. return 0;
  93. }
  94. static int corgibl_get_power(struct backlight_device *bd)
  95. {
  96. return corgibl_powermode;
  97. }
  98. static int corgibl_set_intensity(struct backlight_device *bd, int intensity)
  99. {
  100. if (intensity > CORGI_MAX_INTENSITY)
  101. intensity = CORGI_MAX_INTENSITY;
  102. corgibl_send_intensity(intensity);
  103. current_intensity=intensity;
  104. return 0;
  105. }
  106. static int corgibl_get_intensity(struct backlight_device *bd)
  107. {
  108. return current_intensity;
  109. }
  110. /*
  111. * Called when the battery is low to limit the backlight intensity.
  112. * If limit==0 clear any limit, otherwise limit the intensity
  113. */
  114. void corgibl_limit_intensity(int limit)
  115. {
  116. corgibl_limit = (limit ? 1 : 0);
  117. corgibl_send_intensity(current_intensity);
  118. }
  119. EXPORT_SYMBOL(corgibl_limit_intensity);
  120. static struct backlight_properties corgibl_data = {
  121. .owner = THIS_MODULE,
  122. .get_power = corgibl_get_power,
  123. .set_power = corgibl_set_power,
  124. .max_brightness = CORGI_MAX_INTENSITY,
  125. .get_brightness = corgibl_get_intensity,
  126. .set_brightness = corgibl_set_intensity,
  127. };
  128. static struct backlight_device *corgi_backlight_device;
  129. static int __init corgibl_probe(struct device *dev)
  130. {
  131. corgi_backlight_device = backlight_device_register ("corgi-bl",
  132. NULL, &corgibl_data);
  133. if (IS_ERR (corgi_backlight_device))
  134. return PTR_ERR (corgi_backlight_device);
  135. corgibl_set_intensity(NULL, CORGI_DEFAULT_INTENSITY);
  136. printk("Corgi Backlight Driver Initialized.\n");
  137. return 0;
  138. }
  139. static int corgibl_remove(struct device *dev)
  140. {
  141. backlight_device_unregister(corgi_backlight_device);
  142. corgibl_set_intensity(NULL, 0);
  143. printk("Corgi Backlight Driver Unloaded\n");
  144. return 0;
  145. }
  146. static struct device_driver corgibl_driver = {
  147. .name = "corgi-bl",
  148. .bus = &platform_bus_type,
  149. .probe = corgibl_probe,
  150. .remove = corgibl_remove,
  151. .suspend = corgibl_suspend,
  152. .resume = corgibl_resume,
  153. };
  154. static int __init corgibl_init(void)
  155. {
  156. return driver_register(&corgibl_driver);
  157. }
  158. static void __exit corgibl_exit(void)
  159. {
  160. driver_unregister(&corgibl_driver);
  161. }
  162. module_init(corgibl_init);
  163. module_exit(corgibl_exit);
  164. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  165. MODULE_DESCRIPTION("Corgi Backlight Driver");
  166. MODULE_LICENSE("GPLv2");