generic_bl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mutex.h>
  17. #include <linux/fb.h>
  18. #include <linux/backlight.h>
  19. static int genericbl_intensity;
  20. static struct backlight_device *generic_backlight_device;
  21. static struct generic_bl_info *bl_machinfo;
  22. /* Flag to signal when the battery is low */
  23. #define GENERICBL_BATTLOW BL_CORE_DRIVER1
  24. static int genericbl_send_intensity(struct backlight_device *bd)
  25. {
  26. int intensity = bd->props.brightness;
  27. if (bd->props.power != FB_BLANK_UNBLANK)
  28. intensity = 0;
  29. if (bd->props.state & BL_CORE_FBBLANK)
  30. intensity = 0;
  31. if (bd->props.state & BL_CORE_SUSPENDED)
  32. intensity = 0;
  33. if (bd->props.state & GENERICBL_BATTLOW)
  34. intensity &= bl_machinfo->limit_mask;
  35. bl_machinfo->set_bl_intensity(intensity);
  36. genericbl_intensity = intensity;
  37. if (bl_machinfo->kick_battery)
  38. bl_machinfo->kick_battery();
  39. return 0;
  40. }
  41. static int genericbl_get_intensity(struct backlight_device *bd)
  42. {
  43. return genericbl_intensity;
  44. }
  45. /*
  46. * Called when the battery is low to limit the backlight intensity.
  47. * If limit==0 clear any limit, otherwise limit the intensity
  48. */
  49. void genericbl_limit_intensity(int limit)
  50. {
  51. struct backlight_device *bd = generic_backlight_device;
  52. mutex_lock(&bd->ops_lock);
  53. if (limit)
  54. bd->props.state |= GENERICBL_BATTLOW;
  55. else
  56. bd->props.state &= ~GENERICBL_BATTLOW;
  57. backlight_update_status(generic_backlight_device);
  58. mutex_unlock(&bd->ops_lock);
  59. }
  60. EXPORT_SYMBOL(genericbl_limit_intensity);
  61. static const struct backlight_ops genericbl_ops = {
  62. .options = BL_CORE_SUSPENDRESUME,
  63. .get_brightness = genericbl_get_intensity,
  64. .update_status = genericbl_send_intensity,
  65. };
  66. static int genericbl_probe(struct platform_device *pdev)
  67. {
  68. struct backlight_properties props;
  69. struct generic_bl_info *machinfo = pdev->dev.platform_data;
  70. const char *name = "generic-bl";
  71. struct backlight_device *bd;
  72. bl_machinfo = machinfo;
  73. if (!machinfo->limit_mask)
  74. machinfo->limit_mask = -1;
  75. if (machinfo->name)
  76. name = machinfo->name;
  77. memset(&props, 0, sizeof(struct backlight_properties));
  78. props.type = BACKLIGHT_RAW;
  79. props.max_brightness = machinfo->max_intensity;
  80. bd = backlight_device_register(name, &pdev->dev, NULL, &genericbl_ops,
  81. &props);
  82. if (IS_ERR (bd))
  83. return PTR_ERR (bd);
  84. platform_set_drvdata(pdev, bd);
  85. bd->props.power = FB_BLANK_UNBLANK;
  86. bd->props.brightness = machinfo->default_intensity;
  87. backlight_update_status(bd);
  88. generic_backlight_device = bd;
  89. pr_info("Generic Backlight Driver Initialized.\n");
  90. return 0;
  91. }
  92. static int genericbl_remove(struct platform_device *pdev)
  93. {
  94. struct backlight_device *bd = platform_get_drvdata(pdev);
  95. bd->props.power = 0;
  96. bd->props.brightness = 0;
  97. backlight_update_status(bd);
  98. backlight_device_unregister(bd);
  99. pr_info("Generic Backlight Driver Unloaded\n");
  100. return 0;
  101. }
  102. static struct platform_driver genericbl_driver = {
  103. .probe = genericbl_probe,
  104. .remove = genericbl_remove,
  105. .driver = {
  106. .name = "generic-bl",
  107. },
  108. };
  109. module_platform_driver(genericbl_driver);
  110. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  111. MODULE_DESCRIPTION("Generic Backlight Driver");
  112. MODULE_LICENSE("GPL");