generic_bl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Generic Backlight Driver
  3. *
  4. * Copyright (c) 2004-2008 Richard Purdie
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mutex.h>
  16. #include <linux/fb.h>
  17. #include <linux/backlight.h>
  18. static int genericbl_intensity;
  19. static struct backlight_device *generic_backlight_device;
  20. static struct generic_bl_info *bl_machinfo;
  21. /* Flag to signal when the battery is low */
  22. #define GENERICBL_BATTLOW BL_CORE_DRIVER1
  23. static int genericbl_send_intensity(struct backlight_device *bd)
  24. {
  25. int intensity = bd->props.brightness;
  26. if (bd->props.power != FB_BLANK_UNBLANK)
  27. intensity = 0;
  28. if (bd->props.state & BL_CORE_FBBLANK)
  29. intensity = 0;
  30. if (bd->props.state & BL_CORE_SUSPENDED)
  31. intensity = 0;
  32. if (bd->props.state & GENERICBL_BATTLOW)
  33. intensity &= bl_machinfo->limit_mask;
  34. bl_machinfo->set_bl_intensity(intensity);
  35. genericbl_intensity = intensity;
  36. if (bl_machinfo->kick_battery)
  37. bl_machinfo->kick_battery();
  38. return 0;
  39. }
  40. static int genericbl_get_intensity(struct backlight_device *bd)
  41. {
  42. return genericbl_intensity;
  43. }
  44. /*
  45. * Called when the battery is low to limit the backlight intensity.
  46. * If limit==0 clear any limit, otherwise limit the intensity
  47. */
  48. void corgibl_limit_intensity(int limit)
  49. {
  50. struct backlight_device *bd = generic_backlight_device;
  51. mutex_lock(&bd->ops_lock);
  52. if (limit)
  53. bd->props.state |= GENERICBL_BATTLOW;
  54. else
  55. bd->props.state &= ~GENERICBL_BATTLOW;
  56. backlight_update_status(generic_backlight_device);
  57. mutex_unlock(&bd->ops_lock);
  58. }
  59. EXPORT_SYMBOL(corgibl_limit_intensity);
  60. static const struct backlight_ops genericbl_ops = {
  61. .options = BL_CORE_SUSPENDRESUME,
  62. .get_brightness = genericbl_get_intensity,
  63. .update_status = genericbl_send_intensity,
  64. };
  65. static int genericbl_probe(struct platform_device *pdev)
  66. {
  67. struct backlight_properties props;
  68. struct generic_bl_info *machinfo = pdev->dev.platform_data;
  69. const char *name = "generic-bl";
  70. struct backlight_device *bd;
  71. bl_machinfo = machinfo;
  72. if (!machinfo->limit_mask)
  73. machinfo->limit_mask = -1;
  74. if (machinfo->name)
  75. name = machinfo->name;
  76. memset(&props, 0, sizeof(struct backlight_properties));
  77. props.type = BACKLIGHT_RAW;
  78. props.max_brightness = machinfo->max_intensity;
  79. bd = backlight_device_register(name, &pdev->dev, NULL, &genericbl_ops,
  80. &props);
  81. if (IS_ERR (bd))
  82. return PTR_ERR (bd);
  83. platform_set_drvdata(pdev, bd);
  84. bd->props.power = FB_BLANK_UNBLANK;
  85. bd->props.brightness = machinfo->default_intensity;
  86. backlight_update_status(bd);
  87. generic_backlight_device = bd;
  88. printk("Generic Backlight Driver Initialized.\n");
  89. return 0;
  90. }
  91. static int genericbl_remove(struct platform_device *pdev)
  92. {
  93. struct backlight_device *bd = platform_get_drvdata(pdev);
  94. bd->props.power = 0;
  95. bd->props.brightness = 0;
  96. backlight_update_status(bd);
  97. backlight_device_unregister(bd);
  98. printk("Generic Backlight Driver Unloaded\n");
  99. return 0;
  100. }
  101. static struct platform_driver genericbl_driver = {
  102. .probe = genericbl_probe,
  103. .remove = genericbl_remove,
  104. .driver = {
  105. .name = "generic-bl",
  106. },
  107. };
  108. static int __init genericbl_init(void)
  109. {
  110. return platform_driver_register(&genericbl_driver);
  111. }
  112. static void __exit genericbl_exit(void)
  113. {
  114. platform_driver_unregister(&genericbl_driver);
  115. }
  116. module_init(genericbl_init);
  117. module_exit(genericbl_exit);
  118. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  119. MODULE_DESCRIPTION("Generic Backlight Driver");
  120. MODULE_LICENSE("GPL");