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>
11 #include <linux/hid-debug.h>
12 #include <linux/input.h>
16 #include <linux/vmalloc.h>
17 #include <linux/backlight.h>
18 #include <linux/lcd.h>
20 #include <linux/leds.h>
22 #include <linux/seq_file.h>
23 #include <linux/debugfs.h>
25 #include <linux/completion.h>
26 #include <linux/uaccess.h>
27 #include <linux/module.h>
29 #include "hid-picolcd.h"
32 void picolcd_leds_set(struct picolcd_data *data)
34 struct hid_report *report;
39 report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
40 if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
43 spin_lock_irqsave(&data->lock, flags);
44 hid_set_field(report->field[0], 0, data->led_state);
45 if (!(data->status & PICOLCD_FAILED))
46 hid_hw_request(data->hdev, report, HID_REQ_SET_REPORT);
47 spin_unlock_irqrestore(&data->lock, flags);
50 static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
51 enum led_brightness value)
54 struct hid_device *hdev;
55 struct picolcd_data *data;
58 dev = led_cdev->dev->parent;
59 hdev = to_hid_device(dev);
60 data = hid_get_drvdata(hdev);
63 for (i = 0; i < 8; i++) {
64 if (led_cdev != data->led[i])
66 state = (data->led_state >> i) & 1;
67 if (value == LED_OFF && state) {
68 data->led_state &= ~(1 << i);
69 picolcd_leds_set(data);
70 } else if (value != LED_OFF && !state) {
71 data->led_state |= 1 << i;
72 picolcd_leds_set(data);
78 static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
81 struct hid_device *hdev;
82 struct picolcd_data *data;
85 dev = led_cdev->dev->parent;
86 hdev = to_hid_device(dev);
87 data = hid_get_drvdata(hdev);
88 for (i = 0; i < 8; i++)
89 if (led_cdev == data->led[i]) {
90 value = (data->led_state >> i) & 1;
93 return value ? LED_FULL : LED_OFF;
96 int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
98 struct device *dev = &data->hdev->dev;
99 struct led_classdev *led;
100 size_t name_sz = strlen(dev_name(dev)) + 8;
106 if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
107 report->field[0]->report_size != 8) {
108 dev_err(dev, "unsupported LED_STATE report");
112 for (i = 0; i < 8; i++) {
113 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
115 dev_err(dev, "can't allocate memory for LED %d\n", i);
119 name = (void *)(&led[1]);
120 snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
123 led->max_brightness = 1;
124 led->brightness_get = picolcd_led_get_brightness;
125 led->brightness_set = picolcd_led_set_brightness;
128 ret = led_classdev_register(dev, data->led[i]);
132 dev_err(dev, "can't register LED %d\n", i);
138 for (i = 0; i < 8; i++)
142 led_classdev_unregister(led);
148 void picolcd_exit_leds(struct picolcd_data *data)
150 struct led_classdev *led;
153 for (i = 0; i < 8; i++) {
158 led_classdev_unregister(led);