backlight.c 7.5 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
		bd->props.power = power;
98
		backlight_update_status(bd);
L
Linus Torvalds 已提交
99
		rc = count;
100
	}
101
	mutex_unlock(&bd->ops_lock);
L
Linus Torvalds 已提交
102 103 104 105

	return rc;
}

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

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

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

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

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

	return rc;
}

145 146
static ssize_t backlight_show_max_brightness(struct device *dev,
		struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
147
{
148
	struct backlight_device *bd = to_backlight_device(dev);
L
Linus Torvalds 已提交
149

150
	return sprintf(buf, "%d\n", bd->props.max_brightness);
151 152
}

153 154
static ssize_t backlight_show_actual_brightness(struct device *dev,
		struct device_attribute *attr, char *buf)
155 156
{
	int rc = -ENXIO;
157
	struct backlight_device *bd = to_backlight_device(dev);
158

159 160 161 162
	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 已提交
163 164 165 166

	return rc;
}

167 168 169
struct class *backlight_class;

static void bl_device_release(struct device *dev)
L
Linus Torvalds 已提交
170 171 172 173 174
{
	struct backlight_device *bd = to_backlight_device(dev);
	kfree(bd);
}

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

/**
 * 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).
190 191
 * @devdata: an optional pointer to be stored for private driver use. The
 *   methods may retrieve it by using bl_get_data(bd).
192
 * @ops: the backlight operations structure.
L
Linus Torvalds 已提交
193
 *
194
 * Creates and registers new backlight device. Returns either an
L
Linus Torvalds 已提交
195 196
 * ERR_PTR() or a pointer to the newly allocated device.
 */
197
struct backlight_device *backlight_device_register(const char *name,
198
		struct device *parent, void *devdata, struct backlight_ops *ops)
L
Linus Torvalds 已提交
199 200
{
	struct backlight_device *new_bd;
201
	int rc;
L
Linus Torvalds 已提交
202

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

205
	new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
206
	if (!new_bd)
207
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
208

209
	mutex_init(&new_bd->update_lock);
210
	mutex_init(&new_bd->ops_lock);
L
Linus Torvalds 已提交
211

212 213 214 215 216 217 218
	new_bd->dev.class = backlight_class;
	new_bd->dev.parent = parent;
	new_bd->dev.release = bl_device_release;
	strlcpy(new_bd->dev.bus_id, name, BUS_ID_SIZE);
	dev_set_drvdata(&new_bd->dev, devdata);

	rc = device_register(&new_bd->dev);
219
	if (rc) {
D
Dmitry Torokhov 已提交
220
		kfree(new_bd);
L
Linus Torvalds 已提交
221 222 223
		return ERR_PTR(rc);
	}

224
	rc = backlight_register_fb(new_bd);
D
Dmitry Torokhov 已提交
225
	if (rc) {
226
		device_unregister(&new_bd->dev);
D
Dmitry Torokhov 已提交
227 228 229
		return ERR_PTR(rc);
	}

230
	new_bd->ops = ops;
L
Linus Torvalds 已提交
231

232 233 234 235 236 237 238
#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 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	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;

254 255 256 257 258 259
#ifdef CONFIG_PMAC_BACKLIGHT
	mutex_lock(&pmac_backlight_mutex);
	if (pmac_backlight == bd)
		pmac_backlight = NULL;
	mutex_unlock(&pmac_backlight_mutex);
#endif
260 261 262
	mutex_lock(&bd->ops_lock);
	bd->ops = NULL;
	mutex_unlock(&bd->ops_lock);
L
Linus Torvalds 已提交
263

264
	backlight_unregister_fb(bd);
265
	device_unregister(&bd->dev);
L
Linus Torvalds 已提交
266 267 268 269 270
}
EXPORT_SYMBOL(backlight_device_unregister);

static void __exit backlight_class_exit(void)
{
271
	class_destroy(backlight_class);
L
Linus Torvalds 已提交
272 273 274 275
}

static int __init backlight_class_init(void)
{
276 277 278 279 280 281 282 283 284
	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 已提交
285 286 287 288 289 290 291 292 293 294 295 296
}

/*
 * 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");