1 // SPDX-License-Identifier: GPL-2.0-only
2 /***************************************************************************
3 * Copyright (C) 2010-2012 by Bruno Prémont <bonbons@linux-vserver.org> *
5 * Based on Logitech G13 driver (v0.4) *
6 * Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu> *
8 ***************************************************************************/
10 #include <linux/hid.h>
13 #include <linux/backlight.h>
15 #include "hid-picolcd.h"
17 static int picolcd_get_brightness(struct backlight_device *bdev)
19 struct picolcd_data *data = bl_get_data(bdev);
20 return data->lcd_brightness;
23 static int picolcd_set_brightness(struct backlight_device *bdev)
25 struct picolcd_data *data = bl_get_data(bdev);
26 struct hid_report *report = picolcd_out_report(REPORT_BRIGHTNESS, data->hdev);
29 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
32 data->lcd_brightness = bdev->props.brightness & 0x0ff;
33 data->lcd_power = bdev->props.power;
34 spin_lock_irqsave(&data->lock, flags);
35 hid_set_field(report->field[0], 0, data->lcd_power == FB_BLANK_UNBLANK ? data->lcd_brightness : 0);
36 if (!(data->status & PICOLCD_FAILED))
37 hid_hw_request(data->hdev, report, HID_REQ_SET_REPORT);
38 spin_unlock_irqrestore(&data->lock, flags);
42 static int picolcd_check_bl_fb(struct backlight_device *bdev, struct fb_info *fb)
44 return fb && fb == picolcd_fbinfo((struct picolcd_data *)bl_get_data(bdev));
47 static const struct backlight_ops picolcd_blops = {
48 .update_status = picolcd_set_brightness,
49 .get_brightness = picolcd_get_brightness,
50 .check_fb = picolcd_check_bl_fb,
53 int picolcd_init_backlight(struct picolcd_data *data, struct hid_report *report)
55 struct device *dev = &data->hdev->dev;
56 struct backlight_device *bdev;
57 struct backlight_properties props;
60 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
61 report->field[0]->report_size != 8) {
62 dev_err(dev, "unsupported BRIGHTNESS report");
66 memset(&props, 0, sizeof(props));
67 props.type = BACKLIGHT_RAW;
68 props.max_brightness = 0xff;
69 bdev = backlight_device_register(dev_name(dev), dev, data,
70 &picolcd_blops, &props);
72 dev_err(dev, "failed to register backlight\n");
75 bdev->props.brightness = 0xff;
76 data->lcd_brightness = 0xff;
77 data->backlight = bdev;
78 picolcd_set_brightness(bdev);
82 void picolcd_exit_backlight(struct picolcd_data *data)
84 struct backlight_device *bdev = data->backlight;
86 data->backlight = NULL;
87 backlight_device_unregister(bdev);
90 int picolcd_resume_backlight(struct picolcd_data *data)
94 return picolcd_set_brightness(data->backlight);
98 void picolcd_suspend_backlight(struct picolcd_data *data)
100 int bl_power = data->lcd_power;
101 if (!data->backlight)
104 data->backlight->props.power = FB_BLANK_POWERDOWN;
105 picolcd_set_brightness(data->backlight);
106 data->lcd_power = data->backlight->props.power = bl_power;
108 #endif /* CONFIG_PM */