backlight_omapfb.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2010 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "backlight_omapfb.h"
  15. #include "fileaccess.h"
  16. #include "log.h"
  17. #include "util.h"
  18. #include <string.h>
  19. #define BASEPATH "/devices/platform/omapfb/panel"
  20. static int omapfb_write_brightness(struct backlight_omapfb *bo)
  21. {
  22. int err, level;
  23. if (bo->locked)
  24. level = 0;
  25. else
  26. level = bo->current_level;
  27. err = file_write_int(bo->level_file, level, 10);
  28. if (err)
  29. return err;
  30. return 0;
  31. }
  32. static int backlight_omapfb_max_brightness(struct backlight *b)
  33. {
  34. struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);
  35. return bo->max_level;
  36. }
  37. static int backlight_omapfb_current_brightness(struct backlight *b)
  38. {
  39. struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);
  40. return bo->current_level;
  41. }
  42. static int backlight_omapfb_set_brightness(struct backlight *b, int value)
  43. {
  44. struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);
  45. int err;
  46. value = min(b->max_brightness(b), value);
  47. value = max(b->min_brightness(b), value);
  48. if (value == bo->current_level)
  49. return 0;
  50. bo->current_level = value;
  51. err = omapfb_write_brightness(bo);
  52. backlight_notify_state_change(b);
  53. return err;
  54. }
  55. static int backlight_omapfb_screen_lock(struct backlight *b, int lock)
  56. {
  57. struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);
  58. int err;
  59. lock = !!lock;
  60. if (lock == bo->locked)
  61. return 0;
  62. bo->locked = lock;
  63. err = omapfb_write_brightness(bo);
  64. if (err)
  65. return err;
  66. backlight_notify_state_change(b);
  67. return 0;
  68. }
  69. static int backlight_omapfb_screen_is_locked(struct backlight *b)
  70. {
  71. struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);
  72. return bo->locked;
  73. }
  74. static int backlight_omapfb_read_file(struct backlight_omapfb *bo)
  75. {
  76. int err, value;
  77. err = file_read_int(bo->level_file, &value, 0);
  78. if (err) {
  79. logerr("WARNING: Failed to read backlight level file\n");
  80. return err;
  81. }
  82. bo->current_level = value;
  83. return 0;
  84. }
  85. static void backlight_omapfb_destroy(struct backlight *b)
  86. {
  87. struct backlight_omapfb *bo = container_of(b, struct backlight_omapfb, backlight);
  88. file_close(bo->level_file);
  89. free(bo);
  90. }
  91. static struct backlight * backlight_omapfb_probe(void)
  92. {
  93. struct backlight_omapfb *bo;
  94. struct fileaccess *file, *level_file;
  95. int max_level;
  96. int err;
  97. file = sysfs_file_open(O_RDONLY, "%s/backlight_max", BASEPATH);
  98. if (!file)
  99. goto error;
  100. err = file_read_int(file, &max_level, 0);
  101. file_close(file);
  102. if (err)
  103. goto error;
  104. level_file = sysfs_file_open(O_RDWR, "%s/backlight_level", BASEPATH);
  105. if (!level_file)
  106. goto error;
  107. bo = zalloc(sizeof(*bo));
  108. if (!bo)
  109. goto err_close;
  110. bo->level_file = level_file;
  111. bo->max_level = max_level;
  112. backlight_init(&bo->backlight, "omapfb");
  113. bo->backlight.max_brightness = backlight_omapfb_max_brightness;
  114. bo->backlight.current_brightness = backlight_omapfb_current_brightness;
  115. bo->backlight.set_brightness = backlight_omapfb_set_brightness;
  116. bo->backlight.screen_lock = backlight_omapfb_screen_lock;
  117. bo->backlight.screen_is_locked = backlight_omapfb_screen_is_locked;
  118. bo->backlight.destroy = backlight_omapfb_destroy;
  119. err = backlight_omapfb_read_file(bo);
  120. if (err)
  121. goto err_free;
  122. backlight_notify_state_change(&bo->backlight);
  123. return &bo->backlight;
  124. err_free:
  125. free(bo);
  126. err_close:
  127. file_close(level_file);
  128. error:
  129. return NULL;
  130. }
  131. BACKLIGHT_PROBE(omapfb, backlight_omapfb_probe);