backlight.c 7.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Backlight Lowlevel Control Abstraction
 *
 * Copyright (C) 2003,2004 Hewlett-Packard Company
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/backlight.h>
#include <linux/notifier.h>
#include <linux/ctype.h>
#include <linux/err.h>
#include <linux/fb.h>

17 18 19
#ifdef CONFIG_PMAC_BACKLIGHT
#include <asm/backlight.h>
#endif
20 21 22 23 24 25 26 27 28 29 30 31 32 33

#if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
			   defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
/* This callback gets called when something important happens inside a
 * framebuffer driver. We're looking if that important event is blanking,
 * and if it is, we're switching backlight power as well ...
 */
static int fb_notifier_callback(struct notifier_block *self,
				unsigned long event, void *data)
{
	struct backlight_device *bd;
	struct fb_event *evdata = data;

	/* If we aren't interested in this event, skip it immediately ... */
34
	if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
35 36 37
		return 0;

	bd = container_of(self, struct backlight_device, fb_notif);
38 39 40 41 42
	mutex_lock(&bd->ops_lock);
	if (bd->ops)
		if (!bd->ops->check_fb ||
		    bd->ops->check_fb(evdata->info)) {
			bd->props.fb_blank = *(int *)evdata->data;
43
			backlight_update_status(bd);
44
		}
45
	mutex_unlock(&bd->ops_lock);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	return 0;
}

static int backlight_register_fb(struct backlight_device *bd)
{
	memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
	bd->fb_notif.notifier_call = fb_notifier_callback;

	return fb_register_client(&bd->fb_notif);
}

static void backlight_unregister_fb(struct backlight_device *bd)
{
	fb_unregister_client(&bd->fb_notif);
}
#else
static inline int backlight_register_fb(struct backlight_device *bd)
{
	return 0;
}

static inline void backlight_unregister_fb(struct backlight_device *bd)
{
}
#endif /* CONFIG_FB */

72 73
static ssize_t backlight_show_power(struct device *dev,
		struct device_attribute *attr,char *buf)
L
Linus Torvalds 已提交
74
{
75
	struct backlight_device *bd = to_backlight_device(dev);
L
Linus Torvalds 已提交
76

77
	return sprintf(buf, "%d\n", bd->props.power);
L
Linus Torvalds 已提交
78 79
}

80 81
static ssize_t backlight_store_power(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
82
{
83
	int rc = -ENXIO;
L
Linus Torvalds 已提交
84
	char *endp;
85
	struct backlight_device *bd = to_backlight_device(dev);
86 87
	int power = simple_strtoul(buf, &endp, 0);
	size_t size = endp - buf;
L
Linus Torvalds 已提交
88

89 90 91
	if (*endp && isspace(*endp))
		size++;
	if (size != count)
L
Linus Torvalds 已提交
92 93
		return -EINVAL;

94 95
	mutex_lock(&bd->ops_lock);
	if (bd->ops) {
L
Linus Torvalds 已提交
96
		pr_debug("backlight: set power to %d\n", power);
97 98 99 100
		if (bd->props.power != power) {
			bd->props.power = power;
			backlight_update_status(bd);
		}
L
Linus Torvalds 已提交
101
		rc = count;
102
	}
103
	mutex_unlock(&bd->ops_lock);
L
Linus Torvalds 已提交
104 105 106 107

	return rc;
}

108 109
static ssize_t backlight_show_brightness(struct device *dev,
		struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
110
{
111
	struct backlight_device *bd = to_backlight_device(dev);
L
Linus Torvalds 已提交
112

113
	return sprintf(buf, "%d\n", bd->props.brightness);
L
Linus Torvalds 已提交
114 115
}

116 117
static ssize_t backlight_store_brightness(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
118
{
119
	int rc = -ENXIO;
L
Linus Torvalds 已提交
120
	char *endp;
121
	struct backlight_device *bd = to_backlight_device(dev);
122 123
	int brightness = simple_strtoul(buf, &endp, 0);
	size_t size = endp - buf;
L
Linus Torvalds 已提交
124

125 126 127
	if (*endp && isspace(*endp))
		size++;
	if (size != count)
L
Linus Torvalds 已提交
128 129
		return -EINVAL;

130 131 132
	mutex_lock(&bd->ops_lock);
	if (bd->ops) {
		if (brightness > bd->props.max_brightness)
133 134 135 136
			rc = -EINVAL;
		else {
			pr_debug("backlight: set brightness to %d\n",
				 brightness);
137 138 139 140
			if (bd->props.brightness != brightness) {
				bd->props.brightness = brightness;
				backlight_update_status(bd);
			}
141 142 143
			rc = count;
		}
	}
144
	mutex_unlock(&bd->ops_lock);
L
Linus Torvalds 已提交
145 146 147 148

	return rc;
}

149 150
static ssize_t backlight_show_max_brightness(struct device *dev,
		struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
151
{
152
	struct backlight_device *bd = to_backlight_device(dev);
L
Linus Torvalds 已提交
153

154
	return sprintf(buf, "%d\n", bd->props.max_brightness);
155 156
}

157 158
static ssize_t backlight_show_actual_brightness(struct device *dev,
		struct device_attribute *attr, char *buf)
159 160
{
	int rc = -ENXIO;
161
	struct backlight_device *bd = to_backlight_device(dev);
162

163 164 165 166
	mutex_lock(&bd->ops_lock);
	if (bd->ops && bd->ops->get_brightness)
		rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
	mutex_unlock(&bd->ops_lock);
L
Linus Torvalds 已提交
167 168 169 170

	return rc;
}

171
static struct class *backlight_class;
172 173

static void bl_device_release(struct device *dev)
L
Linus Torvalds 已提交
174 175 176 177 178
{
	struct backlight_device *bd = to_backlight_device(dev);
	kfree(bd);
}

179 180 181
static struct device_attribute bl_device_attributes[] = {
	__ATTR(bl_power, 0644, backlight_show_power, backlight_store_power),
	__ATTR(brightness, 0644, backlight_show_brightness,
182
		     backlight_store_brightness),
183
	__ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
184
		     NULL),
185 186
	__ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
	__ATTR_NULL,
L
Linus Torvalds 已提交
187 188 189 190 191 192 193
};

/**
 * backlight_device_register - create and register a new object of
 *   backlight_device class.
 * @name: the name of the new object(must be the same as the name of the
 *   respective framebuffer device).
194
 * @parent: a pointer to the parent device
195 196
 * @devdata: an optional pointer to be stored for private driver use. The
 *   methods may retrieve it by using bl_get_data(bd).
197
 * @ops: the backlight operations structure.
L
Linus Torvalds 已提交
198
 *
199
 * Creates and registers new backlight device. Returns either an
L
Linus Torvalds 已提交
200 201
 * ERR_PTR() or a pointer to the newly allocated device.
 */
202
struct backlight_device *backlight_device_register(const char *name,
203
		struct device *parent, void *devdata, struct backlight_ops *ops)
L
Linus Torvalds 已提交
204 205
{
	struct backlight_device *new_bd;
206
	int rc;
L
Linus Torvalds 已提交
207

208
	pr_debug("backlight_device_register: name=%s\n", name);
L
Linus Torvalds 已提交
209

210
	new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
211
	if (!new_bd)
212
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
213

214
	mutex_init(&new_bd->update_lock);
215
	mutex_init(&new_bd->ops_lock);
L
Linus Torvalds 已提交
216

217 218 219
	new_bd->dev.class = backlight_class;
	new_bd->dev.parent = parent;
	new_bd->dev.release = bl_device_release;
220
	dev_set_name(&new_bd->dev, name);
221 222 223
	dev_set_drvdata(&new_bd->dev, devdata);

	rc = device_register(&new_bd->dev);
224
	if (rc) {
D
Dmitry Torokhov 已提交
225
		kfree(new_bd);
L
Linus Torvalds 已提交
226 227 228
		return ERR_PTR(rc);
	}

229
	rc = backlight_register_fb(new_bd);
D
Dmitry Torokhov 已提交
230
	if (rc) {
231
		device_unregister(&new_bd->dev);
D
Dmitry Torokhov 已提交
232 233 234
		return ERR_PTR(rc);
	}

235
	new_bd->ops = ops;
L
Linus Torvalds 已提交
236

237 238 239 240 241 242 243
#ifdef CONFIG_PMAC_BACKLIGHT
	mutex_lock(&pmac_backlight_mutex);
	if (!pmac_backlight)
		pmac_backlight = new_bd;
	mutex_unlock(&pmac_backlight_mutex);
#endif

L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	return new_bd;
}
EXPORT_SYMBOL(backlight_device_register);

/**
 * backlight_device_unregister - unregisters a backlight device object.
 * @bd: the backlight device object to be unregistered and freed.
 *
 * Unregisters a previously registered via backlight_device_register object.
 */
void backlight_device_unregister(struct backlight_device *bd)
{
	if (!bd)
		return;

259 260 261 262 263 264
#ifdef CONFIG_PMAC_BACKLIGHT
	mutex_lock(&pmac_backlight_mutex);
	if (pmac_backlight == bd)
		pmac_backlight = NULL;
	mutex_unlock(&pmac_backlight_mutex);
#endif
265 266 267
	mutex_lock(&bd->ops_lock);
	bd->ops = NULL;
	mutex_unlock(&bd->ops_lock);
L
Linus Torvalds 已提交
268

269
	backlight_unregister_fb(bd);
270
	device_unregister(&bd->dev);
L
Linus Torvalds 已提交
271 272 273 274 275
}
EXPORT_SYMBOL(backlight_device_unregister);

static void __exit backlight_class_exit(void)
{
276
	class_destroy(backlight_class);
L
Linus Torvalds 已提交
277 278 279 280
}

static int __init backlight_class_init(void)
{
281 282 283 284 285 286 287 288 289
	backlight_class = class_create(THIS_MODULE, "backlight");
	if (IS_ERR(backlight_class)) {
		printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
				PTR_ERR(backlight_class));
		return PTR_ERR(backlight_class);
	}

	backlight_class->dev_attrs = bl_device_attributes;
	return 0;
L
Linus Torvalds 已提交
290 291 292 293 294 295 296 297 298 299 300 301
}

/*
 * if this is compiled into the kernel, we need to ensure that the
 * class is registered before users of the class try to register lcd's
 */
postcore_initcall(backlight_class_init);
module_exit(backlight_class_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");