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)  * Backlight Lowlevel Control Abstraction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2003,2004 Hewlett-Packard Company
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^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) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/fb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #ifdef CONFIG_PMAC_BACKLIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * DOC: overview
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * The backlight core supports implementing backlight drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * A backlight driver registers a driver using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * devm_backlight_device_register(). The properties of the backlight
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * driver such as type and max_brightness must be specified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * When the core detect changes in for example brightness or power state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * the update_status() operation is called. The backlight driver shall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * implement this operation and use it to adjust backlight.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * Several sysfs attributes are provided by the backlight core::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * - brightness         R/W, set the requested brightness level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * - actual_brightness  RO, the brightness level used by the HW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * - max_brightness     RO, the maximum  brightness level supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * See Documentation/ABI/stable/sysfs-class-backlight for the full list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * The backlight can be adjusted using the sysfs interface, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * the backlight driver may also support adjusting backlight using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * a hot-key or some other platform or firmware specific way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * The driver must implement the get_brightness() operation if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * the HW do not support all the levels that can be specified in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * brightness, thus providing user-space access to the actual level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * via the actual_brightness attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * When the backlight changes this is reported to user-space using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * an uevent connected to the actual_brightness attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * When brightness is set by platform specific means, for example
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * a hot-key to adjust backlight, the driver must notify the backlight
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * core that brightness has changed using backlight_force_update().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * The backlight driver core receives notifications from fbdev and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * if the event is FB_EVENT_BLANK and if the value of blank, from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * FBIOBLANK ioctrl, results in a change in the backlight state the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * update_status() operation is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static struct list_head backlight_dev_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static struct mutex backlight_dev_list_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static struct blocking_notifier_head backlight_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static const char *const backlight_types[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	[BACKLIGHT_RAW] = "raw",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	[BACKLIGHT_PLATFORM] = "platform",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	[BACKLIGHT_FIRMWARE] = "firmware",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static const char *const backlight_scale_types[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	[BACKLIGHT_SCALE_UNKNOWN]	= "unknown",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	[BACKLIGHT_SCALE_LINEAR]	= "linear",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	[BACKLIGHT_SCALE_NON_LINEAR]	= "non-linear",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			   defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * fb_notifier_callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * This callback gets called when something important happens inside a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * framebuffer driver. The backlight core only cares about FB_BLANK_UNBLANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * which is reported to the driver using backlight_update_status()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * as a state change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * There may be several fbdev's connected to the backlight device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * in which case they are kept track of. A state change is only reported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  * if there is a change in backlight for the specified fbdev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static int fb_notifier_callback(struct notifier_block *self,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 				unsigned long event, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct backlight_device *bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct fb_event *evdata = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	int node = evdata->info->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int fb_blank = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/* If we aren't interested in this event, skip it immediately ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (event != FB_EVENT_BLANK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	bd = container_of(self, struct backlight_device, fb_notif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	mutex_lock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (!bd->ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (bd->ops->check_fb && !bd->ops->check_fb(bd, evdata->info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	fb_blank = *(int *)evdata->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (fb_blank == FB_BLANK_UNBLANK && !bd->fb_bl_on[node]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		bd->fb_bl_on[node] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		if (!bd->use_count++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			bd->props.state &= ~BL_CORE_FBBLANK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			bd->props.fb_blank = FB_BLANK_UNBLANK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			backlight_update_status(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	} else if (fb_blank != FB_BLANK_UNBLANK && bd->fb_bl_on[node]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		bd->fb_bl_on[node] = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		if (!(--bd->use_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			bd->props.state |= BL_CORE_FBBLANK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			bd->props.fb_blank = fb_blank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			backlight_update_status(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	mutex_unlock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int backlight_register_fb(struct backlight_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	bd->fb_notif.notifier_call = fb_notifier_callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return fb_register_client(&bd->fb_notif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static void backlight_unregister_fb(struct backlight_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	fb_unregister_client(&bd->fb_notif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static inline int backlight_register_fb(struct backlight_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static inline void backlight_unregister_fb(struct backlight_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) #endif /* CONFIG_FB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static void backlight_generate_event(struct backlight_device *bd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				     enum backlight_update_reason reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	char *envp[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	switch (reason) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	case BACKLIGHT_UPDATE_SYSFS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		envp[0] = "SOURCE=sysfs";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	case BACKLIGHT_UPDATE_HOTKEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		envp[0] = "SOURCE=hotkey";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		envp[0] = "SOURCE=unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	envp[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	struct backlight_device *bd = to_backlight_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return sprintf(buf, "%d\n", bd->props.power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct backlight_device *bd = to_backlight_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	unsigned long power, old_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	rc = kstrtoul(buf, 0, &power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	rc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	mutex_lock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (bd->ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		pr_debug("set power to %lu\n", power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		if (bd->props.power != power) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			old_power = bd->props.power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			bd->props.power = power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			rc = backlight_update_status(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 				bd->props.power = old_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 				rc = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			rc = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	mutex_unlock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static DEVICE_ATTR_RW(bl_power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static ssize_t brightness_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	struct backlight_device *bd = to_backlight_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	return sprintf(buf, "%d\n", bd->props.brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int backlight_device_set_brightness(struct backlight_device *bd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				    unsigned long brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	int rc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	mutex_lock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (bd->ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		if (brightness > bd->props.max_brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			pr_debug("set brightness to %lu\n", brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			bd->props.brightness = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			rc = backlight_update_status(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	mutex_unlock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) EXPORT_SYMBOL(backlight_device_set_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static ssize_t brightness_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct backlight_device *bd = to_backlight_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	unsigned long brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	rc = kstrtoul(buf, 0, &brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	rc = backlight_device_set_brightness(bd, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return rc ? rc : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static DEVICE_ATTR_RW(brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static ssize_t type_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	struct backlight_device *bd = to_backlight_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static DEVICE_ATTR_RO(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static ssize_t max_brightness_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct backlight_device *bd = to_backlight_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	return sprintf(buf, "%d\n", bd->props.max_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static DEVICE_ATTR_RO(max_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static ssize_t actual_brightness_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	int rc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	struct backlight_device *bd = to_backlight_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	mutex_lock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (bd->ops && bd->ops->get_brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		rc = sprintf(buf, "%d\n", bd->props.brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	mutex_unlock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static DEVICE_ATTR_RO(actual_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static ssize_t scale_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	struct backlight_device *bd = to_backlight_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (WARN_ON(bd->props.scale > BACKLIGHT_SCALE_NON_LINEAR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		return sprintf(buf, "unknown\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	return sprintf(buf, "%s\n", backlight_scale_types[bd->props.scale]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static DEVICE_ATTR_RO(scale);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static struct class *backlight_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static int backlight_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	struct backlight_device *bd = to_backlight_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	mutex_lock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		bd->props.state |= BL_CORE_SUSPENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		backlight_update_status(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	mutex_unlock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static int backlight_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct backlight_device *bd = to_backlight_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	mutex_lock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		bd->props.state &= ~BL_CORE_SUSPENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		backlight_update_status(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	mutex_unlock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			 backlight_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static void bl_device_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	struct backlight_device *bd = to_backlight_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	kfree(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static struct attribute *bl_device_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	&dev_attr_bl_power.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	&dev_attr_brightness.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	&dev_attr_actual_brightness.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	&dev_attr_max_brightness.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	&dev_attr_scale.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	&dev_attr_type.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) ATTRIBUTE_GROUPS(bl_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)  * backlight_force_update - tell the backlight subsystem that hardware state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)  *   has changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)  * @bd: the backlight device to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)  * @reason: reason for update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  * Updates the internal state of the backlight in response to a hardware event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  * and generates an uevent to notify userspace. A backlight driver shall call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)  * backlight_force_update() when the backlight is changed using, for example,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)  * a hot-key. The updated brightness is read using get_brightness() and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)  * brightness value is reported using an uevent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) void backlight_force_update(struct backlight_device *bd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			    enum backlight_update_reason reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	mutex_lock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (bd->ops && bd->ops->get_brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		bd->props.brightness = bd->ops->get_brightness(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	mutex_unlock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	backlight_generate_event(bd, reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) EXPORT_SYMBOL(backlight_force_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /* deprecated - use devm_backlight_device_register() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct backlight_device *backlight_device_register(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	struct device *parent, void *devdata, const struct backlight_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	const struct backlight_properties *props)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	struct backlight_device *new_bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	pr_debug("backlight_device_register: name=%s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (!new_bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	mutex_init(&new_bd->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	mutex_init(&new_bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	new_bd->dev.class = backlight_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	new_bd->dev.parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	new_bd->dev.release = bl_device_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	dev_set_name(&new_bd->dev, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	dev_set_drvdata(&new_bd->dev, devdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	/* Set default properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	if (props) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		memcpy(&new_bd->props, props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		       sizeof(struct backlight_properties));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			WARN(1, "%s: invalid backlight type", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			new_bd->props.type = BACKLIGHT_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		new_bd->props.type = BACKLIGHT_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	rc = device_register(&new_bd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		put_device(&new_bd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	rc = backlight_register_fb(new_bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		device_unregister(&new_bd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	new_bd->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) #ifdef CONFIG_PMAC_BACKLIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	mutex_lock(&pmac_backlight_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (!pmac_backlight)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		pmac_backlight = new_bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	mutex_unlock(&pmac_backlight_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	mutex_lock(&backlight_dev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	list_add(&new_bd->entry, &backlight_dev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	mutex_unlock(&backlight_dev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	blocking_notifier_call_chain(&backlight_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 				     BACKLIGHT_REGISTERED, new_bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	return new_bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) EXPORT_SYMBOL(backlight_device_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /** backlight_device_get_by_type - find first backlight device of a type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)  * @type: the type of backlight device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)  * Look up the first backlight device of the specified type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)  * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)  * Pointer to backlight device if any was found. Otherwise NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) struct backlight_device *backlight_device_get_by_type(enum backlight_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	struct backlight_device *bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	mutex_lock(&backlight_dev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	list_for_each_entry(bd, &backlight_dev_list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		if (bd->props.type == type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	mutex_unlock(&backlight_dev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	return found ? bd : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) EXPORT_SYMBOL(backlight_device_get_by_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)  * backlight_device_get_by_name - Get backlight device by name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)  * @name: Device name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)  * This function looks up a backlight device by its name. It obtains a reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)  * on the backlight device and it is the caller's responsibility to drop the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  * reference by calling backlight_put().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)  * A pointer to the backlight device if found, otherwise NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct backlight_device *backlight_device_get_by_name(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	dev = class_find_device_by_name(backlight_class, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	return dev ? to_backlight_device(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) EXPORT_SYMBOL(backlight_device_get_by_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) /* deprecated - use devm_backlight_device_unregister() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) void backlight_device_unregister(struct backlight_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	if (!bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	mutex_lock(&backlight_dev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	list_del(&bd->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	mutex_unlock(&backlight_dev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) #ifdef CONFIG_PMAC_BACKLIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	mutex_lock(&pmac_backlight_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (pmac_backlight == bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		pmac_backlight = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	mutex_unlock(&pmac_backlight_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	blocking_notifier_call_chain(&backlight_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 				     BACKLIGHT_UNREGISTERED, bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	mutex_lock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	bd->ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	mutex_unlock(&bd->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	backlight_unregister_fb(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	device_unregister(&bd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) EXPORT_SYMBOL(backlight_device_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) static void devm_backlight_device_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	struct backlight_device *backlight = *(struct backlight_device **)res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	backlight_device_unregister(backlight);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) static int devm_backlight_device_match(struct device *dev, void *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 					void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	struct backlight_device **r = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	return *r == data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)  * backlight_register_notifier - get notified of backlight (un)registration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)  * @nb: notifier block with the notifier to call on backlight (un)registration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)  * Register a notifier to get notified when backlight devices get registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)  * or unregistered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)  * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)  * 0 on success, otherwise a negative error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) int backlight_register_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	return blocking_notifier_chain_register(&backlight_notifier, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) EXPORT_SYMBOL(backlight_register_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)  * backlight_unregister_notifier - unregister a backlight notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)  * @nb: notifier block to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)  * Register a notifier to get notified when backlight devices get registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)  * or unregistered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)  * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)  * 0 on success, otherwise a negative error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) int backlight_unregister_notifier(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	return blocking_notifier_chain_unregister(&backlight_notifier, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) EXPORT_SYMBOL(backlight_unregister_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)  * devm_backlight_device_register - register a new backlight device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)  * @dev: the device to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)  * @name: the name of the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)  * @parent: a pointer to the parent device (often the same as @dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)  * @devdata: an optional pointer to be stored for private driver use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)  * @ops: the backlight operations structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)  * @props: the backlight properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)  * Creates and registers new backlight device. When a backlight device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)  * is registered the configuration must be specified in the @props
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)  * parameter. See description of &backlight_properties.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)  * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)  * struct backlight on success, or an ERR_PTR on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) struct backlight_device *devm_backlight_device_register(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	const char *name, struct device *parent, void *devdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	const struct backlight_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	const struct backlight_properties *props)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	struct backlight_device **ptr, *backlight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	backlight = backlight_device_register(name, parent, devdata, ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 						props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	if (!IS_ERR(backlight)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		*ptr = backlight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		devres_add(dev, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		devres_free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	return backlight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) EXPORT_SYMBOL(devm_backlight_device_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)  * devm_backlight_device_unregister - unregister backlight device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)  * @dev: the device to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)  * @bd: the backlight device to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)  * Deallocates a backlight allocated with devm_backlight_device_register().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)  * Normally this function will not need to be called and the resource management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)  * code will ensure that the resources are freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) void devm_backlight_device_unregister(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 				struct backlight_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	rc = devres_release(dev, devm_backlight_device_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 				devm_backlight_device_match, bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	WARN_ON(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) EXPORT_SYMBOL(devm_backlight_device_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) static int of_parent_match(struct device *dev, const void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	return dev->parent && dev->parent->of_node == data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)  * of_find_backlight_by_node() - find backlight device by device-tree node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)  * @node: device-tree node of the backlight device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)  * Returns a pointer to the backlight device corresponding to the given DT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)  * node or NULL if no such backlight device exists or if the device hasn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)  * been probed yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)  * This function obtains a reference on the backlight device and it is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)  * caller's responsibility to drop the reference by calling put_device() on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)  * the backlight device's .dev field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) struct backlight_device *of_find_backlight_by_node(struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	dev = class_find_device(backlight_class, NULL, node, of_parent_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	return dev ? to_backlight_device(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) EXPORT_SYMBOL(of_find_backlight_by_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) static struct backlight_device *of_find_backlight(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	struct backlight_device *bd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		np = of_parse_phandle(dev->of_node, "backlight", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 			bd = of_find_backlight_by_node(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 			of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 			if (!bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 				return ERR_PTR(-EPROBE_DEFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	return bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) static void devm_backlight_release(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	struct backlight_device *bd = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	if (bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		put_device(&bd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)  * devm_of_find_backlight - find backlight for a device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)  * @dev: the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)  * This function looks for a property named 'backlight' on the DT node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)  * connected to @dev and looks up the backlight device. The lookup is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)  * device managed so the reference to the backlight device is automatically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)  * dropped on driver detach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)  * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)  * A pointer to the backlight device if found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)  * Error pointer -EPROBE_DEFER if the DT property is set, but no backlight
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)  * device is found. NULL if there's no backlight property.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) struct backlight_device *devm_of_find_backlight(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	struct backlight_device *bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	bd = of_find_backlight(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	if (IS_ERR_OR_NULL(bd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		return bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	ret = devm_add_action(dev, devm_backlight_release, bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		put_device(&bd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	return bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) EXPORT_SYMBOL(devm_of_find_backlight);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) static void __exit backlight_class_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	class_destroy(backlight_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) static int __init backlight_class_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	backlight_class = class_create(THIS_MODULE, "backlight");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	if (IS_ERR(backlight_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 		pr_warn("Unable to create backlight class; errno = %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 			PTR_ERR(backlight_class));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		return PTR_ERR(backlight_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	backlight_class->dev_groups = bl_device_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	backlight_class->pm = &backlight_class_dev_pm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	INIT_LIST_HEAD(&backlight_dev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	mutex_init(&backlight_dev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)  * if this is compiled into the kernel, we need to ensure that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)  * class is registered before users of the class try to register lcd's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) postcore_initcall(backlight_class_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) module_exit(backlight_class_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");