hid-picolcd_backlight.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /***************************************************************************
  2. * Copyright (C) 2010-2012 by Bruno Prémont <bonbons@linux-vserver.org> *
  3. * *
  4. * Based on Logitech G13 driver (v0.4) *
  5. * Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu> *
  6. * *
  7. * This program is free software: you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation, version 2 of the License. *
  10. * *
  11. * This driver is distributed in the hope that it will be useful, but *
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  14. * General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this software. If not see <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include <linux/hid.h>
  20. #include "usbhid/usbhid.h"
  21. #include <linux/usb.h>
  22. #include <linux/fb.h>
  23. #include <linux/backlight.h>
  24. #include "hid-picolcd.h"
  25. static int picolcd_get_brightness(struct backlight_device *bdev)
  26. {
  27. struct picolcd_data *data = bl_get_data(bdev);
  28. return data->lcd_brightness;
  29. }
  30. static int picolcd_set_brightness(struct backlight_device *bdev)
  31. {
  32. struct picolcd_data *data = bl_get_data(bdev);
  33. struct hid_report *report = picolcd_out_report(REPORT_BRIGHTNESS, data->hdev);
  34. unsigned long flags;
  35. if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
  36. return -ENODEV;
  37. data->lcd_brightness = bdev->props.brightness & 0x0ff;
  38. data->lcd_power = bdev->props.power;
  39. spin_lock_irqsave(&data->lock, flags);
  40. hid_set_field(report->field[0], 0, data->lcd_power == FB_BLANK_UNBLANK ? data->lcd_brightness : 0);
  41. if (!(data->status & PICOLCD_FAILED))
  42. usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
  43. spin_unlock_irqrestore(&data->lock, flags);
  44. return 0;
  45. }
  46. static int picolcd_check_bl_fb(struct backlight_device *bdev, struct fb_info *fb)
  47. {
  48. return fb && fb == picolcd_fbinfo((struct picolcd_data *)bl_get_data(bdev));
  49. }
  50. static const struct backlight_ops picolcd_blops = {
  51. .update_status = picolcd_set_brightness,
  52. .get_brightness = picolcd_get_brightness,
  53. .check_fb = picolcd_check_bl_fb,
  54. };
  55. int picolcd_init_backlight(struct picolcd_data *data, struct hid_report *report)
  56. {
  57. struct device *dev = &data->hdev->dev;
  58. struct backlight_device *bdev;
  59. struct backlight_properties props;
  60. if (!report)
  61. return -ENODEV;
  62. if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
  63. report->field[0]->report_size != 8) {
  64. dev_err(dev, "unsupported BRIGHTNESS report");
  65. return -EINVAL;
  66. }
  67. memset(&props, 0, sizeof(props));
  68. props.type = BACKLIGHT_RAW;
  69. props.max_brightness = 0xff;
  70. bdev = backlight_device_register(dev_name(dev), dev, data,
  71. &picolcd_blops, &props);
  72. if (IS_ERR(bdev)) {
  73. dev_err(dev, "failed to register backlight\n");
  74. return PTR_ERR(bdev);
  75. }
  76. bdev->props.brightness = 0xff;
  77. data->lcd_brightness = 0xff;
  78. data->backlight = bdev;
  79. picolcd_set_brightness(bdev);
  80. return 0;
  81. }
  82. void picolcd_exit_backlight(struct picolcd_data *data)
  83. {
  84. struct backlight_device *bdev = data->backlight;
  85. data->backlight = NULL;
  86. if (bdev)
  87. backlight_device_unregister(bdev);
  88. }
  89. int picolcd_resume_backlight(struct picolcd_data *data)
  90. {
  91. if (!data->backlight)
  92. return 0;
  93. return picolcd_set_brightness(data->backlight);
  94. }
  95. #ifdef CONFIG_PM
  96. void picolcd_suspend_backlight(struct picolcd_data *data)
  97. {
  98. int bl_power = data->lcd_power;
  99. if (!data->backlight)
  100. return;
  101. data->backlight->props.power = FB_BLANK_POWERDOWN;
  102. picolcd_set_brightness(data->backlight);
  103. data->lcd_power = data->backlight->props.power = bl_power;
  104. }
  105. #endif /* CONFIG_PM */