| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/device.h>
- #include <linux/uaccess.h>
- #include <linux/slab.h>
- #include <linux/pci.h>
- #include <linux/i2c.h>
- #include <linux/acpi.h>
- #include <linux/interrupt.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/mutex.h>
- #include <linux/delay.h>
- #include "light_ring.h"
- extern struct kobject *vfiec_kobj;
- static struct kobject *backlight_kobj = NULL;
- static int brightness_val = 0;
- static int max_brightness_val = 1;
- #define BACKLIGHT_PATH "/sys/class/backlight/intel_backlight/brightness"
- // Read the current brightness value from sysfs
- static int read_current_brightness(void)
- {
- struct file *filp = NULL;
- char buf[32] = {0};
- loff_t pos = 0;
- int ret = -EIO;
- int brightness = -1;
-
- filp = filp_open(BACKLIGHT_PATH, O_RDONLY, 0);
- if (IS_ERR(filp)) {
- pr_err("Failed to open backlight file: %ld\n", PTR_ERR(filp));
- return -EIO;
- }
-
- ret = kernel_read(filp, buf, sizeof(buf) - 1, &pos);
- if (ret > 0) {
- buf[ret] = '\0';
- if (kstrtoint(buf, 10, &brightness) != 0) {
- pr_err("Failed to parse brightness value\n");
- brightness = -EINVAL;
- }
- }
-
- filp_close(filp, NULL);
- return brightness;
- }
- static int read_max_brightness(void)
- {
- struct file *filp = NULL;
- loff_t pos = 0;
- int max_brightness;
- filp = filp_open("/sys/class/backlight/intel_backlight/max_brightness", O_RDONLY, 0);
- if (!IS_ERR(filp)) {
- char max_buf[32] = {0};
- kernel_read(filp, max_buf, sizeof(max_buf) - 1, &pos);
- if (kstrtoint(max_buf, 10, &max_brightness) != 0)
- {
- filp_close(filp, NULL);
- return -EINVAL;
- }
- filp_close(filp, NULL);
- }
- return max_brightness;
- }
- // Write brightness value to sysfs
- static int write_brightness_value(int value)
- {
- struct file *filp = NULL;
- char buf[32] = {0};
- loff_t pos = 0;
- int ret = -EIO;
- int max_brightness;
-
- // First read the maximum value for boundary checking
- filp = filp_open("/sys/class/backlight/intel_backlight/max_brightness", O_RDONLY, 0);
- if (!IS_ERR(filp)) {
- char max_buf[32] = {0};
- kernel_read(filp, max_buf, sizeof(max_buf) - 1, &pos);
- if (kstrtoint(max_buf, 10, &max_brightness) == 0) {
- if (value < 0 || value > max_brightness) {
- pr_err("Brightness value %d out of range [0, %d]\n", value, max_brightness);
- filp_close(filp, NULL);
- return -EINVAL;
- }
- }
- filp_close(filp, NULL);
- }
-
- // Write brightness value
- filp = filp_open(BACKLIGHT_PATH, O_WRONLY, 0);
- if (IS_ERR(filp)) {
- pr_err("Failed to open backlight file for write: %ld\n", PTR_ERR(filp));
- return -EIO;
- }
-
- snprintf(buf, sizeof(buf), "%d", value);
- ret = kernel_write(filp, buf, strlen(buf), &pos);
-
- filp_close(filp, NULL);
-
- return (ret > 0) ? 0 : -EIO;
- }
- /* ==================== mode ==================== */
- static ssize_t brightness_show(struct kobject *kobj, struct kobj_attribute *attr,
- char *buf)
- {
- static int val = 0;
- brightness_val = read_current_brightness();
- if(max_brightness_val == 0 || max_brightness_val > 960)
- {
- max_brightness_val = 960;
- }
- val = brightness_val*100/max_brightness_val;
- if(val > 100)
- {
- val = 100;
- }
- return sprintf(buf, "%d\n", val);
- }
- static ssize_t brightness_store(struct kobject *kobj, struct kobj_attribute *attr,
- const char *buf, size_t count)
- {
- int ret = 0;
- long val;
- if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 100)
- {
- return -EINVAL;
- }
- brightness_val = val*max_brightness_val/100;
- if(brightness_val > 960)
- {
- brightness_val = 960;
- }
- ret = write_brightness_value(brightness_val);
- return count;
- }
- static struct kobj_attribute brightness =
- __ATTR(brightness, 0644, brightness_show, brightness_store);
- static struct attribute *backlight_attrs[] = {
- &brightness.attr,
- NULL,
- };
- static struct attribute_group backlight_attr_group = {
- .attrs = backlight_attrs,
- };
- int backlight_init(void)
- {
- int ret;
- #ifndef C181
- return 0;
- #endif
- max_brightness_val = read_max_brightness();
- if(max_brightness_val <= 0)
- {
- pr_err("Failed to read max brightness value\n");
- return -EIO;
- }
- brightness_val = read_current_brightness();
- /* Create /sys/kernel/vfiec/lightring */
- backlight_kobj = kobject_create_and_add("backlight", vfiec_kobj);
- if (!backlight_kobj)
- {
- ret = -ENOMEM;
- }
- /* Create property file */
- ret = sysfs_create_group(backlight_kobj, &backlight_attr_group);
- if (ret)
- {
- pr_err("Faibacklight to create sysfs group: %d\n", ret);
- }
- return ret;
- }
- void backlight_exit(void)
- {
- #ifndef C181
- return ;
- #endif
- sysfs_remove_group(backlight_kobj, &backlight_attr_group);
- kobject_put(backlight_kobj);
- }
|