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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * LED Kernel Multi-Control Trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Control multi leds at one time using ioctl from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright 2017 Allen Zhang <zwp@rock-chips.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Based on Richard Purdie's ledtrig-timer.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * it under the terms of the GNU General Public License version 2 as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "../leds.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "../leds-multi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) struct multi_ctrl_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct led_ctrl_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct led_classdev *led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct delayed_work delay_trig_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct led_ctrl_data old_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) struct multi_ctrl_scroll_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct led_ctrl_scroll_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	volatile bool data_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct delayed_work scroll_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) struct multi_ctrl_breath_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct led_ctrl_breath_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	volatile bool data_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct delayed_work breath_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) enum leds_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	LEDS_MODE_MULTI_SET = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	LEDS_MODE_MULTI_SCROLL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	LEDS_MODE_MULTI_BREATH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	LEDS_MODE_MULTI_INVALID = 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define bits(nr)	((u64)1 << (nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static DECLARE_RWSEM(multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static LIST_HEAD(multi_leds_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static int led_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static struct miscdevice multi_ctrl_miscdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static struct multi_ctrl_scroll_data *multi_ctrl_scroll_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static struct multi_ctrl_breath_data *multi_ctrl_breath_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static char *mult_ctrl_trigger[TRIG_MAX] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	"none",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	"default-on",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	"timer",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	"oneshot",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static struct led_ctrl_data leds_data[MAX_LEDS_NUMBER];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static struct led_ctrl_scroll_data leds_scroll_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static struct led_ctrl_breath_data leds_breath_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static enum leds_mode leds_pre_mode = LEDS_MODE_MULTI_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static u64 multi_ctrl_calc_next_scroll_bitmap(u64 cur_bitmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	u64 bitmap = cur_bitmap << leds_scroll_data.shifts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	u64 ret_bitmap = bitmap & (bits(led_num) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (bitmap > (bits(led_num) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		ret_bitmap |= bitmap >> led_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		ret_bitmap = bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	return ret_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static void multi_ctrl_scroll_work_fn(struct work_struct *ws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct multi_ctrl_data *ctrl_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	int bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	static u64 update_bits = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	down_read(&multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (!multi_ctrl_scroll_info->data_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		update_bits = bits(led_num) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		multi_ctrl_scroll_info->data_valid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		pr_info("%s, new scroll work is queued\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (unlikely(update_bits > (bits(led_num) - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		pr_warn("%s,update_bits is exceed the max led numbers!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		update_bits = bits(led_num) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (leds_pre_mode != LEDS_MODE_MULTI_SCROLL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		update_bits = bits(led_num) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		leds_pre_mode = LEDS_MODE_MULTI_SCROLL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	list_for_each_entry(ctrl_data, &multi_leds_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		struct led_classdev *led_cdev = ctrl_data->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		if (bit >= led_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			dev_err(led_cdev->dev, "exceed the max number of muti_leds_list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		cancel_delayed_work_sync(&ctrl_data->delay_trig_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		/* only change the led status when bits updated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		if (update_bits & bits(bit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			if (leds_scroll_data.init_bitmap & bits(bit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 				led_trigger_set_by_name(led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 							mult_ctrl_trigger[TRIG_DEF_ON]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				led_trigger_remove(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		bit++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	update_bits = leds_scroll_data.init_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	leds_scroll_data.init_bitmap = multi_ctrl_calc_next_scroll_bitmap(update_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	update_bits ^= leds_scroll_data.init_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	up_read(&multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	schedule_delayed_work(&multi_ctrl_scroll_info->scroll_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			      msecs_to_jiffies(leds_scroll_data.shift_delay_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static void multi_ctrl_breath_work_fn(struct work_struct *ws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct multi_ctrl_data *ctrl_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	u32 bri_every_step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	static u32 pre_brightness = LED_HALF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	static u64 pre_bg_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	static u64 pre_br_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	static u64 pre_black_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	down_read(&multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (!multi_ctrl_breath_info->data_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		pre_bg_bitmap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		pre_br_bitmap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		pre_black_bitmap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		multi_ctrl_breath_info->data_valid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		pr_info("%s, new breath work is queued\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	bri_every_step = LED_FULL / leds_breath_data.breath_steps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	list_for_each_entry(ctrl_data, &multi_leds_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		struct led_classdev *led_cdev = ctrl_data->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		if (bit >= led_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			dev_err(led_cdev->dev, "exceed the max number of muti_leds_list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		cancel_delayed_work_sync(&ctrl_data->delay_trig_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		if (pre_bg_bitmap == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			if (leds_breath_data.background_bitmap & bits(bit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 				led_trigger_set_by_name(led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 							mult_ctrl_trigger[TRIG_DEF_ON]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		if (leds_breath_data.breath_bitmap & bits(bit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			/* only update the leds indicated by bitmap*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			led_cdev->brightness =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				(pre_brightness + bri_every_step) % LED_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			if (unlikely(pre_br_bitmap == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				led_trigger_set_by_name(led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 							mult_ctrl_trigger[TRIG_DEF_ON]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 				led_set_brightness_async(led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 							 led_cdev->brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		if (pre_black_bitmap == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			if ((~(leds_breath_data.background_bitmap |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			     leds_breath_data.breath_bitmap)) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			    bits(bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				led_trigger_remove(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		bit++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	pre_brightness = (pre_brightness + bri_every_step) % LED_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	pre_bg_bitmap = leds_breath_data.background_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	pre_br_bitmap = leds_breath_data.breath_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	pre_black_bitmap = ~(pre_bg_bitmap | pre_br_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	leds_pre_mode = LEDS_MODE_MULTI_BREATH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	up_read(&multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	schedule_delayed_work(&multi_ctrl_breath_info->breath_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			      msecs_to_jiffies(leds_breath_data.change_delay_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static void multi_ctrl_delay_trig_func(struct work_struct *ws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	struct multi_ctrl_data *ctrl_data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		container_of(ws, struct multi_ctrl_data, delay_trig_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	struct led_classdev *led_cdev = ctrl_data->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct led_ctrl_data *led_data = ctrl_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	/* set brightness*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (led_data->brightness == LED_OFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		led_trigger_remove(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	/* set delay_on and delay_off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	led_cdev->blink_delay_off = led_data->delay_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	led_cdev->blink_delay_on = led_data->delay_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	led_cdev->brightness = led_data->brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	led_trigger_set_by_name(led_cdev, mult_ctrl_trigger[led_data->trigger]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (led_data->trigger == TRIG_ONESHOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		led_blink_set_oneshot(led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				      &led_cdev->blink_delay_on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 				      &led_cdev->blink_delay_off, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static int multi_ctrl_set_led(struct multi_ctrl_data *ctrl_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct led_ctrl_data *led_data = ctrl_data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct led_classdev *led_cdev = ctrl_data->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (!led_data || led_data->trigger >= TRIG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (led_data->delayed_trigger_ms &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	    (led_data->trigger == TRIG_TIMER ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	    led_data->trigger == TRIG_ONESHOT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		schedule_delayed_work(&ctrl_data->delay_trig_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				      msecs_to_jiffies(led_data->delayed_trigger_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		/* set brightness*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		if (led_data->brightness == LED_OFF ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		    led_data->trigger == TRIG_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			led_trigger_remove(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		/* set delay_on and delay_off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		led_cdev->blink_delay_off = led_data->delay_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		led_cdev->blink_delay_on = led_data->delay_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		led_cdev->brightness = led_data->brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		led_trigger_set_by_name(led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 					mult_ctrl_trigger[led_data->trigger]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		if (led_data->trigger == TRIG_ONESHOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			led_blink_set_oneshot(led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 					      &led_cdev->blink_delay_on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 					      &led_cdev->blink_delay_off, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static void cancel_all_work_sync(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	multi_ctrl_scroll_info->data_valid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	multi_ctrl_breath_info->data_valid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	cancel_delayed_work_sync(&multi_ctrl_scroll_info->scroll_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	cancel_delayed_work_sync(&multi_ctrl_breath_info->breath_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static int multi_ctrl_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static int multi_ctrl_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static long multi_ctrl_ioctl(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			     unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	cancel_all_work_sync();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	case LEDS_MULTI_CTRL_IOCTL_MULTI_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		struct led_ctrl_data *argp = (struct led_ctrl_data *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		struct multi_ctrl_data *ctrl_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		bool mode_changed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		down_read(&multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		if (leds_pre_mode != LEDS_MODE_MULTI_SET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			leds_pre_mode = LEDS_MODE_MULTI_SET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			mode_changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		if (copy_from_user(leds_data, argp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 				   sizeof(struct led_ctrl_data) * led_num)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			pr_err("%s, copy from user failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			up_read(&multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		list_for_each_entry(ctrl_data, &multi_leds_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			struct led_classdev *led_cdev = ctrl_data->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			cancel_delayed_work_sync(&ctrl_data->delay_trig_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			if (i >= led_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 				dev_err(led_cdev->dev, "exceed the max number of muti_leds_list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			ctrl_data->data = &leds_data[i++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			if (!memcmp(&ctrl_data->old_data, ctrl_data->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				    sizeof(struct led_ctrl_data)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				    !mode_changed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			multi_ctrl_set_led(ctrl_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			memcpy(&ctrl_data->old_data, ctrl_data->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			       sizeof(struct led_ctrl_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		up_read(&multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	case LEDS_MULTI_CTRL_IOCTL_GET_LED_NUMBER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		int __user *p = (int __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		ret = put_user(led_num, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	case LEDS_MULTI_CTRL_IOCTL_MULTI_SET_SCROLL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		if (led_num > sizeof(leds_scroll_data.init_bitmap) * 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			pr_err("registered leds is exeeded the size of scroll bitmap!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		if (copy_from_user(&leds_scroll_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 				   (struct led_ctrl_scroll_data *)arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				   sizeof(struct led_ctrl_scroll_data))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			pr_err("%s, set scroll mode, copy from user failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			       __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		schedule_delayed_work(&multi_ctrl_scroll_info->scroll_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	case LEDS_MULTI_CTRL_IOCTL_MULTI_SET_BREATH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		if (led_num > sizeof(leds_breath_data.breath_bitmap) * 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			pr_err("registered leds is exeeded the size of breath bitmap!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		 * background color bitmap will be set to default-on mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		 * so we can't set a bit in background bits if it's one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		 * bit in breath color bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		leds_breath_data.background_bitmap &=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 					~leds_breath_data.breath_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		if (copy_from_user(&leds_breath_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 				   (struct led_ctrl_breath_data *)arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 				   sizeof(leds_breath_data))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 			pr_err("%s, set breath mode, copy from user failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			       __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		schedule_delayed_work(&multi_ctrl_breath_info->breath_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static const struct file_operations multi_ctrl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	.open = multi_ctrl_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	.release = multi_ctrl_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	.unlocked_ioctl = multi_ctrl_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	.compat_ioctl   = multi_ctrl_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) int led_multi_control_register(struct led_classdev *led_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	struct multi_ctrl_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	if (led_num++ >= MAX_LEDS_NUMBER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	data = kzalloc(sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		dev_err(led_cdev->dev, "malloc multi_ctrl_data failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	data->led_cdev = led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	data->old_data.brightness = led_cdev->brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	data->old_data.delay_off = led_cdev->blink_delay_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	data->old_data.delay_on = led_cdev->blink_delay_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	down_write(&multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	list_add_tail(&data->node, &multi_leds_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	up_write(&multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	INIT_DELAYED_WORK(&data->delay_trig_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			  multi_ctrl_delay_trig_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) int led_multi_control_unregister(struct led_classdev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	struct multi_ctrl_data *ctrl_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	if (led_num-- < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	down_write(&multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	list_for_each_entry(ctrl_data, &multi_leds_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		if (ctrl_data->led_cdev == cdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			cancel_delayed_work_sync(&ctrl_data->delay_trig_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			list_del(&ctrl_data->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	up_write(&multi_leds_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	kfree(ctrl_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) int led_multi_control_init(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	multi_ctrl_scroll_info =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			kzalloc(sizeof(*multi_ctrl_scroll_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	if (!multi_ctrl_scroll_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		pr_err("malloc multi_ctrl_scroll_info failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	multi_ctrl_breath_info =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			kzalloc(sizeof(*multi_ctrl_breath_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	if (!multi_ctrl_breath_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		pr_err("malloc leds_breath_info failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		goto breath_info_malloc_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	multi_ctrl_miscdev.fops = &multi_ctrl_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	multi_ctrl_miscdev.parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	multi_ctrl_miscdev.name = "led_multi_ctrl";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	ret = misc_register(&multi_ctrl_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		pr_err("Can't register misc dev for led multi-control.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		goto miscdev_register_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	led_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	multi_ctrl_scroll_info->data = &leds_scroll_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	INIT_DELAYED_WORK(&multi_ctrl_scroll_info->scroll_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			  multi_ctrl_scroll_work_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	multi_ctrl_breath_info->data = &leds_breath_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	INIT_DELAYED_WORK(&multi_ctrl_breath_info->breath_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			  multi_ctrl_breath_work_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) miscdev_register_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	kfree(multi_ctrl_breath_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	multi_ctrl_breath_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) breath_info_malloc_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	kfree(multi_ctrl_scroll_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	multi_ctrl_scroll_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) int led_multi_control_exit(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	misc_deregister(&multi_ctrl_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	cancel_delayed_work_sync(&multi_ctrl_scroll_info->scroll_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	cancel_delayed_work_sync(&multi_ctrl_breath_info->breath_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	if (multi_ctrl_scroll_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		kfree(multi_ctrl_scroll_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		multi_ctrl_scroll_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	if (multi_ctrl_breath_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		kfree(multi_ctrl_breath_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		multi_ctrl_breath_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) MODULE_AUTHOR("Allen.zhang <zwp@rock-chips.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) MODULE_DESCRIPTION("Multi-Contorl LED trigger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) MODULE_LICENSE("GPL");