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;
84
	struct backlight_device *bd = to_backlight_device(dev);
85
	unsigned long power;
L
Linus Torvalds 已提交
86

87 88 89
	rc = strict_strtoul(buf, 0, &power);
	if (rc)
		return rc;
L
Linus Torvalds 已提交
90

91
	rc = -ENXIO;
92 93
	mutex_lock(&bd->ops_lock);
	if (bd->ops) {
94
		pr_debug("backlight: set power to %lu\n", power);
95 96 97 98
		if (bd->props.power != power) {
			bd->props.power = power;
			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;
118
	struct backlight_device *bd = to_backlight_device(dev);
119
	unsigned long brightness;
L
Linus Torvalds 已提交
120

121 122 123 124 125
	rc = strict_strtoul(buf, 0, &brightness);
	if (rc)
		return rc;

	rc = -ENXIO;
L
Linus Torvalds 已提交
126

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

	return rc;
}

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

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

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

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

	return rc;
}

168
static struct class *backlight_class;
169 170

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

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

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

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

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

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

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

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

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

232
	new_bd->ops = ops;
L
Linus Torvalds 已提交
233

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

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

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

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

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

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