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