1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/hid.h>
21
22#include <linux/fb.h>
23#include <linux/backlight.h>
24
25#include "hid-picolcd.h"
26
27static int picolcd_get_brightness(struct backlight_device *bdev)
28{
29 struct picolcd_data *data = bl_get_data(bdev);
30 return data->lcd_brightness;
31}
32
33static int picolcd_set_brightness(struct backlight_device *bdev)
34{
35 struct picolcd_data *data = bl_get_data(bdev);
36 struct hid_report *report = picolcd_out_report(REPORT_BRIGHTNESS, data->hdev);
37 unsigned long flags;
38
39 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
40 return -ENODEV;
41
42 data->lcd_brightness = bdev->props.brightness & 0x0ff;
43 data->lcd_power = bdev->props.power;
44 spin_lock_irqsave(&data->lock, flags);
45 hid_set_field(report->field[0], 0, data->lcd_power == FB_BLANK_UNBLANK ? data->lcd_brightness : 0);
46 if (!(data->status & PICOLCD_FAILED))
47 hid_hw_request(data->hdev, report, HID_REQ_SET_REPORT);
48 spin_unlock_irqrestore(&data->lock, flags);
49 return 0;
50}
51
52static int picolcd_check_bl_fb(struct backlight_device *bdev, struct fb_info *fb)
53{
54 return fb && fb == picolcd_fbinfo((struct picolcd_data *)bl_get_data(bdev));
55}
56
57static const struct backlight_ops picolcd_blops = {
58 .update_status = picolcd_set_brightness,
59 .get_brightness = picolcd_get_brightness,
60 .check_fb = picolcd_check_bl_fb,
61};
62
63int picolcd_init_backlight(struct picolcd_data *data, struct hid_report *report)
64{
65 struct device *dev = &data->hdev->dev;
66 struct backlight_device *bdev;
67 struct backlight_properties props;
68 if (!report)
69 return -ENODEV;
70 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
71 report->field[0]->report_size != 8) {
72 dev_err(dev, "unsupported BRIGHTNESS report");
73 return -EINVAL;
74 }
75
76 memset(&props, 0, sizeof(props));
77 props.type = BACKLIGHT_RAW;
78 props.max_brightness = 0xff;
79 bdev = backlight_device_register(dev_name(dev), dev, data,
80 &picolcd_blops, &props);
81 if (IS_ERR(bdev)) {
82 dev_err(dev, "failed to register backlight\n");
83 return PTR_ERR(bdev);
84 }
85 bdev->props.brightness = 0xff;
86 data->lcd_brightness = 0xff;
87 data->backlight = bdev;
88 picolcd_set_brightness(bdev);
89 return 0;
90}
91
92void picolcd_exit_backlight(struct picolcd_data *data)
93{
94 struct backlight_device *bdev = data->backlight;
95
96 data->backlight = NULL;
97 backlight_device_unregister(bdev);
98}
99
100int picolcd_resume_backlight(struct picolcd_data *data)
101{
102 if (!data->backlight)
103 return 0;
104 return picolcd_set_brightness(data->backlight);
105}
106
107#ifdef CONFIG_PM
108void picolcd_suspend_backlight(struct picolcd_data *data)
109{
110 int bl_power = data->lcd_power;
111 if (!data->backlight)
112 return;
113
114 data->backlight->props.power = FB_BLANK_POWERDOWN;
115 picolcd_set_brightness(data->backlight);
116 data->lcd_power = data->backlight->props.power = bl_power;
117}
118#endif
119
120