hid-picolcd_lcd.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/lcd.h>
  24. #include "hid-picolcd.h"
  25. /*
  26. * lcd class device
  27. */
  28. static int picolcd_get_contrast(struct lcd_device *ldev)
  29. {
  30. struct picolcd_data *data = lcd_get_data(ldev);
  31. return data->lcd_contrast;
  32. }
  33. static int picolcd_set_contrast(struct lcd_device *ldev, int contrast)
  34. {
  35. struct picolcd_data *data = lcd_get_data(ldev);
  36. struct hid_report *report = picolcd_out_report(REPORT_CONTRAST, data->hdev);
  37. unsigned long flags;
  38. if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
  39. return -ENODEV;
  40. data->lcd_contrast = contrast & 0x0ff;
  41. spin_lock_irqsave(&data->lock, flags);
  42. hid_set_field(report->field[0], 0, data->lcd_contrast);
  43. if (!(data->status & PICOLCD_FAILED))
  44. usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
  45. spin_unlock_irqrestore(&data->lock, flags);
  46. return 0;
  47. }
  48. static int picolcd_check_lcd_fb(struct lcd_device *ldev, struct fb_info *fb)
  49. {
  50. return fb && fb == picolcd_fbinfo((struct picolcd_data *)lcd_get_data(ldev));
  51. }
  52. static struct lcd_ops picolcd_lcdops = {
  53. .get_contrast = picolcd_get_contrast,
  54. .set_contrast = picolcd_set_contrast,
  55. .check_fb = picolcd_check_lcd_fb,
  56. };
  57. int picolcd_init_lcd(struct picolcd_data *data, struct hid_report *report)
  58. {
  59. struct device *dev = &data->hdev->dev;
  60. struct lcd_device *ldev;
  61. if (!report)
  62. return -ENODEV;
  63. if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
  64. report->field[0]->report_size != 8) {
  65. dev_err(dev, "unsupported CONTRAST report");
  66. return -EINVAL;
  67. }
  68. ldev = lcd_device_register(dev_name(dev), dev, data, &picolcd_lcdops);
  69. if (IS_ERR(ldev)) {
  70. dev_err(dev, "failed to register LCD\n");
  71. return PTR_ERR(ldev);
  72. }
  73. ldev->props.max_contrast = 0x0ff;
  74. data->lcd_contrast = 0xe5;
  75. data->lcd = ldev;
  76. picolcd_set_contrast(ldev, 0xe5);
  77. return 0;
  78. }
  79. void picolcd_exit_lcd(struct picolcd_data *data)
  80. {
  81. struct lcd_device *ldev = data->lcd;
  82. data->lcd = NULL;
  83. if (ldev)
  84. lcd_device_unregister(ldev);
  85. }
  86. int picolcd_resume_lcd(struct picolcd_data *data)
  87. {
  88. if (!data->lcd)
  89. return 0;
  90. return picolcd_set_contrast(data->lcd, data->lcd_contrast);
  91. }