corgi_bl.c 4.2 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *  Backlight Driver for Sharp Zaurus Handhelds (various models)
L
Linus Torvalds 已提交
3
 *
4
 *  Copyright (c) 2004-2006 Richard Purdie
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16
 *
 *  Based on Sharp's 2.4 Backlight Driver
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
17
#include <linux/platform_device.h>
18
#include <linux/mutex.h>
L
Linus Torvalds 已提交
19 20
#include <linux/fb.h>
#include <linux/backlight.h>
21
#include <asm/arch/sharpsl.h>
22
#include <asm/hardware/sharpsl_pm.h>
L
Linus Torvalds 已提交
23

24
static int corgibl_intensity;
25
static DEFINE_MUTEX(bl_mutex);
26
static struct backlight_properties corgibl_data;
27
static struct backlight_device *corgi_backlight_device;
28
static struct corgibl_machinfo *bl_machinfo;
29 30 31 32

static unsigned long corgibl_flags;
#define CORGIBL_SUSPENDED     0x01
#define CORGIBL_BATTLOW       0x02
L
Linus Torvalds 已提交
33

34
static int corgibl_send_intensity(struct backlight_device *bd)
L
Linus Torvalds 已提交
35 36
{
	void (*corgi_kick_batt)(void);
37
	int intensity = bd->props->brightness;
L
Linus Torvalds 已提交
38

39 40 41
	if (bd->props->power != FB_BLANK_UNBLANK)
		intensity = 0;
	if (bd->props->fb_blank != FB_BLANK_UNBLANK)
L
Linus Torvalds 已提交
42
		intensity = 0;
43 44 45
	if (corgibl_flags & CORGIBL_SUSPENDED)
		intensity = 0;
	if (corgibl_flags & CORGIBL_BATTLOW)
46
		intensity &= bl_machinfo->limit_mask;
47

48 49 50
 	mutex_lock(&bl_mutex);
	bl_machinfo->set_bl_intensity(intensity);
	mutex_unlock(&bl_mutex);
51

52 53
	corgibl_intensity = intensity;

54 55 56 57 58
 	corgi_kick_batt = symbol_get(sharpsl_battery_kick);
 	if (corgi_kick_batt) {
 		corgi_kick_batt();
 		symbol_put(sharpsl_battery_kick);
 	}
L
Linus Torvalds 已提交
59

60
	return 0;
L
Linus Torvalds 已提交
61 62 63
}

#ifdef CONFIG_PM
64
static int corgibl_suspend(struct platform_device *pdev, pm_message_t state)
L
Linus Torvalds 已提交
65
{
66 67
	struct backlight_device *bd = platform_get_drvdata(pdev);

68
	corgibl_flags |= CORGIBL_SUSPENDED;
69
	corgibl_send_intensity(bd);
L
Linus Torvalds 已提交
70 71 72
	return 0;
}

73
static int corgibl_resume(struct platform_device *pdev)
L
Linus Torvalds 已提交
74
{
75 76
	struct backlight_device *bd = platform_get_drvdata(pdev);

77
	corgibl_flags &= ~CORGIBL_SUSPENDED;
78
	corgibl_send_intensity(bd);
L
Linus Torvalds 已提交
79 80 81 82 83 84 85
	return 0;
}
#else
#define corgibl_suspend	NULL
#define corgibl_resume	NULL
#endif

86
static int corgibl_get_intensity(struct backlight_device *bd)
L
Linus Torvalds 已提交
87
{
88
	return corgibl_intensity;
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96
}

/*
 * Called when the battery is low to limit the backlight intensity.
 * If limit==0 clear any limit, otherwise limit the intensity
 */
void corgibl_limit_intensity(int limit)
{
97 98 99 100 101
	if (limit)
		corgibl_flags |= CORGIBL_BATTLOW;
	else
		corgibl_flags &= ~CORGIBL_BATTLOW;
	corgibl_send_intensity(corgi_backlight_device);
L
Linus Torvalds 已提交
102 103 104 105 106 107
}
EXPORT_SYMBOL(corgibl_limit_intensity);


static struct backlight_properties corgibl_data = {
	.get_brightness = corgibl_get_intensity,
108
	.update_status  = corgibl_send_intensity,
L
Linus Torvalds 已提交
109 110
};

111
static int corgibl_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
112
{
113
	struct corgibl_machinfo *machinfo = pdev->dev.platform_data;
114

115
	bl_machinfo = machinfo;
116
	corgibl_data.max_brightness = machinfo->max_intensity;
117 118
	if (!machinfo->limit_mask)
		machinfo->limit_mask = -1;
119

L
Linus Torvalds 已提交
120
	corgi_backlight_device = backlight_device_register ("corgi-bl",
121
		&pdev->dev, NULL, &corgibl_data);
L
Linus Torvalds 已提交
122 123 124
	if (IS_ERR (corgi_backlight_device))
		return PTR_ERR (corgi_backlight_device);

125 126
	platform_set_drvdata(pdev, corgi_backlight_device);

127
	corgibl_data.power = FB_BLANK_UNBLANK;
128
	corgibl_data.brightness = machinfo->default_intensity;
129
	corgibl_send_intensity(corgi_backlight_device);
L
Linus Torvalds 已提交
130 131 132 133 134

	printk("Corgi Backlight Driver Initialized.\n");
	return 0;
}

135
static int corgibl_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
136
{
137 138
	struct backlight_device *bd = platform_get_drvdata(pdev);

139 140
	corgibl_data.power = 0;
	corgibl_data.brightness = 0;
141
	corgibl_send_intensity(bd);
142

143
	backlight_device_unregister(bd);
L
Linus Torvalds 已提交
144 145 146 147 148

	printk("Corgi Backlight Driver Unloaded\n");
	return 0;
}

149
static struct platform_driver corgibl_driver = {
L
Linus Torvalds 已提交
150 151 152 153
	.probe		= corgibl_probe,
	.remove		= corgibl_remove,
	.suspend	= corgibl_suspend,
	.resume		= corgibl_resume,
154 155 156
	.driver		= {
		.name	= "corgi-bl",
	},
L
Linus Torvalds 已提交
157 158 159 160
};

static int __init corgibl_init(void)
{
161
	return platform_driver_register(&corgibl_driver);
L
Linus Torvalds 已提交
162 163 164 165
}

static void __exit corgibl_exit(void)
{
166
	platform_driver_unregister(&corgibl_driver);
L
Linus Torvalds 已提交
167 168 169 170 171 172 173
}

module_init(corgibl_init);
module_exit(corgibl_exit);

MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
MODULE_DESCRIPTION("Corgi Backlight Driver");
174
MODULE_LICENSE("GPL");