Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /***************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *   Copyright (C) 2010-2012 by Bruno Prémont <bonbons@linux-vserver.org>  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *                                                                         *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *   Based on Logitech G13 driver (v0.4)                                   *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *     Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu>   *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *                                                                         *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  ***************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/hid-debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "hid-ids.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/fb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/lcd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "hid-picolcd.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) void picolcd_leds_set(struct picolcd_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct hid_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (!data->led[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	spin_lock_irqsave(&data->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	hid_set_field(report->field[0], 0, data->led_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (!(data->status & PICOLCD_FAILED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		hid_hw_request(data->hdev, report, HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	spin_unlock_irqrestore(&data->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct hid_device *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct picolcd_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	int i, state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	dev  = led_cdev->dev->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	for (i = 0; i < 8; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		if (led_cdev != data->led[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		state = (data->led_state >> i) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		if (value == LED_OFF && state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			data->led_state &= ~(1 << i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			picolcd_leds_set(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		} else if (value != LED_OFF && !state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			data->led_state |= 1 << i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			picolcd_leds_set(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct hid_device *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct picolcd_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int i, value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	dev  = led_cdev->dev->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	hdev = to_hid_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	data = hid_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	for (i = 0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		if (led_cdev == data->led[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			value = (data->led_state >> i) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return value ? LED_FULL : LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct device *dev = &data->hdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct led_classdev *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	size_t name_sz = strlen(dev_name(dev)) + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (!report)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			report->field[0]->report_size != 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		dev_err(dev, "unsupported LED_STATE report");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	for (i = 0; i < 8; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		if (!led) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			dev_err(dev, "can't allocate memory for LED %d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		name = (void *)(&led[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		led->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		led->brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		led->max_brightness = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		led->brightness_get = picolcd_led_get_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		led->brightness_set = picolcd_led_set_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		data->led[i] = led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		ret = led_classdev_register(dev, data->led[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			data->led[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			kfree(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			dev_err(dev, "can't register LED %d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	for (i = 0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		if (data->led[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			led = data->led[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			data->led[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			led_classdev_unregister(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			kfree(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) void picolcd_exit_leds(struct picolcd_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct led_classdev *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	for (i = 0; i < 8; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		led = data->led[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		data->led[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		if (!led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		led_classdev_unregister(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		kfree(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)