xref: /linux/drivers/hid/hid-picolcd_backlight.c (revision 89aa02edaa30e4327ebc8fca9b80795bbfd4ce9b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /***************************************************************************
3  *   Copyright (C) 2010-2012 by Bruno Prémont <bonbons@linux-vserver.org>  *
4  *                                                                         *
5  *   Based on Logitech G13 driver (v0.4)                                   *
6  *     Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu>   *
7  *                                                                         *
8  ***************************************************************************/
9 
10 #include <linux/hid.h>
11 
12 #include <linux/backlight.h>
13 
14 #include "hid-picolcd.h"
15 
16 static int picolcd_get_brightness(struct backlight_device *bdev)
17 {
18 	struct picolcd_data *data = bl_get_data(bdev);
19 	return data->lcd_brightness;
20 }
21 
22 static int picolcd_set_brightness(struct backlight_device *bdev)
23 {
24 	struct picolcd_data *data = bl_get_data(bdev);
25 	struct hid_report *report = picolcd_out_report(REPORT_BRIGHTNESS, data->hdev);
26 	unsigned long flags;
27 
28 	if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
29 		return -ENODEV;
30 
31 	data->lcd_brightness = bdev->props.brightness & 0x0ff;
32 	data->lcd_power      = bdev->props.power;
33 	spin_lock_irqsave(&data->lock, flags);
34 	hid_set_field(report->field[0], 0, data->lcd_power == FB_BLANK_UNBLANK ? data->lcd_brightness : 0);
35 	if (!(data->status & PICOLCD_FAILED))
36 		hid_hw_request(data->hdev, report, HID_REQ_SET_REPORT);
37 	spin_unlock_irqrestore(&data->lock, flags);
38 	return 0;
39 }
40 
41 static const struct backlight_ops picolcd_blops = {
42 	.update_status  = picolcd_set_brightness,
43 	.get_brightness = picolcd_get_brightness,
44 };
45 
46 int picolcd_init_backlight(struct picolcd_data *data, struct hid_report *report)
47 {
48 	struct device *dev = &data->hdev->dev;
49 	struct backlight_device *bdev;
50 	struct backlight_properties props;
51 	if (!report)
52 		return -ENODEV;
53 	if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
54 			report->field[0]->report_size != 8) {
55 		dev_err(dev, "unsupported BRIGHTNESS report");
56 		return -EINVAL;
57 	}
58 
59 	memset(&props, 0, sizeof(props));
60 	props.type = BACKLIGHT_RAW;
61 	props.max_brightness = 0xff;
62 	bdev = backlight_device_register(dev_name(dev), dev, data,
63 			&picolcd_blops, &props);
64 	if (IS_ERR(bdev)) {
65 		dev_err(dev, "failed to register backlight\n");
66 		return PTR_ERR(bdev);
67 	}
68 	bdev->props.brightness     = 0xff;
69 	data->lcd_brightness       = 0xff;
70 	data->backlight            = bdev;
71 	picolcd_set_brightness(bdev);
72 	return 0;
73 }
74 
75 void picolcd_exit_backlight(struct picolcd_data *data)
76 {
77 	struct backlight_device *bdev = data->backlight;
78 
79 	data->backlight = NULL;
80 	backlight_device_unregister(bdev);
81 }
82 
83 int picolcd_resume_backlight(struct picolcd_data *data)
84 {
85 	if (!data->backlight)
86 		return 0;
87 	return picolcd_set_brightness(data->backlight);
88 }
89 
90 #ifdef CONFIG_PM
91 void picolcd_suspend_backlight(struct picolcd_data *data)
92 {
93 	int bl_power = data->lcd_power;
94 	if (!data->backlight)
95 		return;
96 
97 	data->backlight->props.power = FB_BLANK_POWERDOWN;
98 	picolcd_set_brightness(data->backlight);
99 	data->lcd_power = data->backlight->props.power = bl_power;
100 }
101 #endif /* CONFIG_PM */
102 
103