generic_bl.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. static const struct backlight_ops genericbl_ops = {
  45. .options = BL_CORE_SUSPENDRESUME,
  46. .get_brightness = genericbl_get_intensity,
  47. .update_status = genericbl_send_intensity,
  48. };
  49. static int genericbl_probe(struct platform_device *pdev)
  50. {
  51. struct backlight_properties props;
  52. struct generic_bl_info *machinfo = dev_get_platdata(&pdev->dev);
  53. const char *name = "generic-bl";
  54. struct backlight_device *bd;
  55. bl_machinfo = machinfo;
  56. if (!machinfo->limit_mask)
  57. machinfo->limit_mask = -1;
  58. if (machinfo->name)
  59. name = machinfo->name;
  60. memset(&props, 0, sizeof(struct backlight_properties));
  61. props.type = BACKLIGHT_RAW;
  62. props.max_brightness = machinfo->max_intensity;
  63. bd = devm_backlight_device_register(&pdev->dev, name, &pdev->dev,
  64. NULL, &genericbl_ops, &props);
  65. if (IS_ERR(bd))
  66. return PTR_ERR(bd);
  67. platform_set_drvdata(pdev, bd);
  68. bd->props.power = FB_BLANK_UNBLANK;
  69. bd->props.brightness = machinfo->default_intensity;
  70. backlight_update_status(bd);
  71. generic_backlight_device = bd;
  72. dev_info(&pdev->dev, "Generic Backlight Driver Initialized.\n");
  73. return 0;
  74. }
  75. static int genericbl_remove(struct platform_device *pdev)
  76. {
  77. struct backlight_device *bd = platform_get_drvdata(pdev);
  78. bd->props.power = 0;
  79. bd->props.brightness = 0;
  80. backlight_update_status(bd);
  81. dev_info(&pdev->dev, "Generic Backlight Driver Unloaded\n");
  82. return 0;
  83. }
  84. static struct platform_driver genericbl_driver = {
  85. .probe = genericbl_probe,
  86. .remove = genericbl_remove,
  87. .driver = {
  88. .name = "generic-bl",
  89. },
  90. };
  91. module_platform_driver(genericbl_driver);
  92. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  93. MODULE_DESCRIPTION("Generic Backlight Driver");
  94. MODULE_LICENSE("GPL");