提交 6ca01765 编写于 作者: R Richard Purdie 提交者: Linus Torvalds

[PATCH] backlight: Backlight Class Improvements

Backlight class attributes are currently easy to implement incorrectly.
Moving certain handling into the backlight core prevents this whilst at the
same time makes the drivers simpler and consistent.  The following changes are
included:

The brightness attribute only sets and reads the brightness variable in the
backlight_properties structure.

The power attribute only sets and reads the power variable in the
backlight_properties structure.

Any framebuffer blanking events change a variable fb_blank in the
backlight_properties structure.

The backlight driver has only two functions to implement.  One function is
called when any of the above properties change (to update the backlight
brightness), the second is called to return the current backlight brightness
value.  A new attribute "actual_brightness" is added to return this brightness
as determined by the driver having combined all the above factors (and any
driver/device specific factors).

Additionally, the backlight core takes care of checking the maximum brightness
is not exceeded and of turning off the backlight before device removal.

The corgi backlight driver is updated to reflect these changes.
Signed-off-by: NRichard Purdie <rpurdie@rpsys.net>
Signed-off-by: NAntonino Daplas <adaplas@pol.net>
Signed-off-by: NAndrew Morton <akpm@osdl.org>
Signed-off-by: NLinus Torvalds <torvalds@osdl.org>
上级 9b0e1c5d
...@@ -16,14 +16,12 @@ ...@@ -16,14 +16,12 @@
static ssize_t backlight_show_power(struct class_device *cdev, char *buf) static ssize_t backlight_show_power(struct class_device *cdev, char *buf)
{ {
int rc; int rc = -ENXIO;
struct backlight_device *bd = to_backlight_device(cdev); struct backlight_device *bd = to_backlight_device(cdev);
down(&bd->sem); down(&bd->sem);
if (likely(bd->props && bd->props->get_power)) if (likely(bd->props))
rc = sprintf(buf, "%d\n", bd->props->get_power(bd)); rc = sprintf(buf, "%d\n", bd->props->power);
else
rc = -ENXIO;
up(&bd->sem); up(&bd->sem);
return rc; return rc;
...@@ -31,7 +29,7 @@ static ssize_t backlight_show_power(struct class_device *cdev, char *buf) ...@@ -31,7 +29,7 @@ static ssize_t backlight_show_power(struct class_device *cdev, char *buf)
static ssize_t backlight_store_power(struct class_device *cdev, const char *buf, size_t count) static ssize_t backlight_store_power(struct class_device *cdev, const char *buf, size_t count)
{ {
int rc, power; int rc = -ENXIO, power;
char *endp; char *endp;
struct backlight_device *bd = to_backlight_device(cdev); struct backlight_device *bd = to_backlight_device(cdev);
...@@ -40,12 +38,13 @@ static ssize_t backlight_store_power(struct class_device *cdev, const char *buf, ...@@ -40,12 +38,13 @@ static ssize_t backlight_store_power(struct class_device *cdev, const char *buf,
return -EINVAL; return -EINVAL;
down(&bd->sem); down(&bd->sem);
if (likely(bd->props && bd->props->set_power)) { if (likely(bd->props)) {
pr_debug("backlight: set power to %d\n", power); pr_debug("backlight: set power to %d\n", power);
bd->props->set_power(bd, power); bd->props->power = power;
if (likely(bd->props->update_status))
bd->props->update_status(bd);
rc = count; rc = count;
} else }
rc = -ENXIO;
up(&bd->sem); up(&bd->sem);
return rc; return rc;
...@@ -53,14 +52,12 @@ static ssize_t backlight_store_power(struct class_device *cdev, const char *buf, ...@@ -53,14 +52,12 @@ static ssize_t backlight_store_power(struct class_device *cdev, const char *buf,
static ssize_t backlight_show_brightness(struct class_device *cdev, char *buf) static ssize_t backlight_show_brightness(struct class_device *cdev, char *buf)
{ {
int rc; int rc = -ENXIO;
struct backlight_device *bd = to_backlight_device(cdev); struct backlight_device *bd = to_backlight_device(cdev);
down(&bd->sem); down(&bd->sem);
if (likely(bd->props && bd->props->get_brightness)) if (likely(bd->props))
rc = sprintf(buf, "%d\n", bd->props->get_brightness(bd)); rc = sprintf(buf, "%d\n", bd->props->brightness);
else
rc = -ENXIO;
up(&bd->sem); up(&bd->sem);
return rc; return rc;
...@@ -68,7 +65,7 @@ static ssize_t backlight_show_brightness(struct class_device *cdev, char *buf) ...@@ -68,7 +65,7 @@ static ssize_t backlight_show_brightness(struct class_device *cdev, char *buf)
static ssize_t backlight_store_brightness(struct class_device *cdev, const char *buf, size_t count) static ssize_t backlight_store_brightness(struct class_device *cdev, const char *buf, size_t count)
{ {
int rc, brightness; int rc = -ENXIO, brightness;
char *endp; char *endp;
struct backlight_device *bd = to_backlight_device(cdev); struct backlight_device *bd = to_backlight_device(cdev);
...@@ -77,12 +74,18 @@ static ssize_t backlight_store_brightness(struct class_device *cdev, const char ...@@ -77,12 +74,18 @@ static ssize_t backlight_store_brightness(struct class_device *cdev, const char
return -EINVAL; return -EINVAL;
down(&bd->sem); down(&bd->sem);
if (likely(bd->props && bd->props->set_brightness)) { if (likely(bd->props)) {
pr_debug("backlight: set brightness to %d\n", brightness); if (brightness > bd->props->max_brightness)
bd->props->set_brightness(bd, brightness); rc = -EINVAL;
else {
pr_debug("backlight: set brightness to %d\n",
brightness);
bd->props->brightness = brightness;
if (likely(bd->props->update_status))
bd->props->update_status(bd);
rc = count; rc = count;
} else }
rc = -ENXIO; }
up(&bd->sem); up(&bd->sem);
return rc; return rc;
...@@ -90,14 +93,26 @@ static ssize_t backlight_store_brightness(struct class_device *cdev, const char ...@@ -90,14 +93,26 @@ static ssize_t backlight_store_brightness(struct class_device *cdev, const char
static ssize_t backlight_show_max_brightness(struct class_device *cdev, char *buf) static ssize_t backlight_show_max_brightness(struct class_device *cdev, char *buf)
{ {
int rc; int rc = -ENXIO;
struct backlight_device *bd = to_backlight_device(cdev); struct backlight_device *bd = to_backlight_device(cdev);
down(&bd->sem); down(&bd->sem);
if (likely(bd->props)) if (likely(bd->props))
rc = sprintf(buf, "%d\n", bd->props->max_brightness); rc = sprintf(buf, "%d\n", bd->props->max_brightness);
else up(&bd->sem);
rc = -ENXIO;
return rc;
}
static ssize_t backlight_show_actual_brightness(struct class_device *cdev,
char *buf)
{
int rc = -ENXIO;
struct backlight_device *bd = to_backlight_device(cdev);
down(&bd->sem);
if (likely(bd->props && bd->props->get_brightness))
rc = sprintf(buf, "%d\n", bd->props->get_brightness(bd));
up(&bd->sem); up(&bd->sem);
return rc; return rc;
...@@ -123,7 +138,10 @@ static struct class backlight_class = { ...@@ -123,7 +138,10 @@ static struct class backlight_class = {
static struct class_device_attribute bl_class_device_attributes[] = { static struct class_device_attribute bl_class_device_attributes[] = {
DECLARE_ATTR(power, 0644, backlight_show_power, backlight_store_power), DECLARE_ATTR(power, 0644, backlight_show_power, backlight_store_power),
DECLARE_ATTR(brightness, 0644, backlight_show_brightness, backlight_store_brightness), DECLARE_ATTR(brightness, 0644, backlight_show_brightness,
backlight_store_brightness),
DECLARE_ATTR(actual_brightness, 0444, backlight_show_actual_brightness,
NULL),
DECLARE_ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL), DECLARE_ATTR(max_brightness, 0444, backlight_show_max_brightness, NULL),
}; };
...@@ -144,8 +162,12 @@ static int fb_notifier_callback(struct notifier_block *self, ...@@ -144,8 +162,12 @@ static int fb_notifier_callback(struct notifier_block *self,
bd = container_of(self, struct backlight_device, fb_notif); bd = container_of(self, struct backlight_device, fb_notif);
down(&bd->sem); down(&bd->sem);
if (bd->props) if (bd->props)
if (!bd->props->check_fb || bd->props->check_fb(evdata->info)) if (!bd->props->check_fb ||
bd->props->set_power(bd, *(int *)evdata->data); bd->props->check_fb(evdata->info)) {
bd->props->fb_blank = *(int *)evdata->data;
if (likely(bd->props && bd->props->update_status))
bd->props->update_status(bd);
}
up(&bd->sem); up(&bd->sem);
return 0; return 0;
} }
...@@ -231,6 +253,12 @@ void backlight_device_unregister(struct backlight_device *bd) ...@@ -231,6 +253,12 @@ void backlight_device_unregister(struct backlight_device *bd)
&bl_class_device_attributes[i]); &bl_class_device_attributes[i]);
down(&bd->sem); down(&bd->sem);
if (likely(bd->props && bd->props->update_status)) {
bd->props->brightness = 0;
bd->props->power = 0;
bd->props->update_status(bd);
}
bd->props = NULL; bd->props = NULL;
up(&bd->sem); up(&bd->sem);
......
...@@ -25,24 +25,30 @@ ...@@ -25,24 +25,30 @@
#define CORGI_DEFAULT_INTENSITY 0x1f #define CORGI_DEFAULT_INTENSITY 0x1f
#define CORGI_LIMIT_MASK 0x0b #define CORGI_LIMIT_MASK 0x0b
static int corgibl_powermode = FB_BLANK_UNBLANK; static int corgibl_intensity;
static int current_intensity = 0;
static int corgibl_limit = 0;
static void (*corgibl_mach_set_intensity)(int intensity); static void (*corgibl_mach_set_intensity)(int intensity);
static spinlock_t bl_lock = SPIN_LOCK_UNLOCKED; static spinlock_t bl_lock = SPIN_LOCK_UNLOCKED;
static struct backlight_properties corgibl_data; static struct backlight_properties corgibl_data;
static struct backlight_device *corgi_backlight_device;
static unsigned long corgibl_flags;
#define CORGIBL_SUSPENDED 0x01
#define CORGIBL_BATTLOW 0x02
static void corgibl_send_intensity(int intensity) static int corgibl_send_intensity(struct backlight_device *bd)
{ {
unsigned long flags; unsigned long flags;
void (*corgi_kick_batt)(void); void (*corgi_kick_batt)(void);
int intensity = bd->props->brightness;
if (corgibl_powermode != FB_BLANK_UNBLANK) { if (bd->props->power != FB_BLANK_UNBLANK)
intensity = 0;
if (bd->props->fb_blank != FB_BLANK_UNBLANK)
intensity = 0;
if (corgibl_flags & CORGIBL_SUSPENDED)
intensity = 0; intensity = 0;
} else { if (corgibl_flags & CORGIBL_BATTLOW)
if (corgibl_limit)
intensity &= CORGI_LIMIT_MASK; intensity &= CORGI_LIMIT_MASK;
}
spin_lock_irqsave(&bl_lock, flags); spin_lock_irqsave(&bl_lock, flags);
...@@ -50,45 +56,29 @@ static void corgibl_send_intensity(int intensity) ...@@ -50,45 +56,29 @@ static void corgibl_send_intensity(int intensity)
spin_unlock_irqrestore(&bl_lock, flags); spin_unlock_irqrestore(&bl_lock, flags);
corgibl_intensity = intensity;
corgi_kick_batt = symbol_get(sharpsl_battery_kick); corgi_kick_batt = symbol_get(sharpsl_battery_kick);
if (corgi_kick_batt) { if (corgi_kick_batt) {
corgi_kick_batt(); corgi_kick_batt();
symbol_put(sharpsl_battery_kick); symbol_put(sharpsl_battery_kick);
} }
}
static void corgibl_blank(int blank) return 0;
{
switch(blank) {
case FB_BLANK_NORMAL:
case FB_BLANK_VSYNC_SUSPEND:
case FB_BLANK_HSYNC_SUSPEND:
case FB_BLANK_POWERDOWN:
if (corgibl_powermode == FB_BLANK_UNBLANK) {
corgibl_send_intensity(0);
corgibl_powermode = blank;
}
break;
case FB_BLANK_UNBLANK:
if (corgibl_powermode != FB_BLANK_UNBLANK) {
corgibl_powermode = blank;
corgibl_send_intensity(current_intensity);
}
break;
}
} }
#ifdef CONFIG_PM #ifdef CONFIG_PM
static int corgibl_suspend(struct platform_device *dev, pm_message_t state) static int corgibl_suspend(struct platform_device *dev, pm_message_t state)
{ {
corgibl_blank(FB_BLANK_POWERDOWN); corgibl_flags |= CORGIBL_SUSPENDED;
corgibl_send_intensity(corgi_backlight_device);
return 0; return 0;
} }
static int corgibl_resume(struct platform_device *dev) static int corgibl_resume(struct platform_device *dev)
{ {
corgibl_blank(FB_BLANK_UNBLANK); corgibl_flags &= ~CORGIBL_SUSPENDED;
corgibl_send_intensity(corgi_backlight_device);
return 0; return 0;
} }
#else #else
...@@ -96,54 +86,38 @@ static int corgibl_resume(struct platform_device *dev) ...@@ -96,54 +86,38 @@ static int corgibl_resume(struct platform_device *dev)
#define corgibl_resume NULL #define corgibl_resume NULL
#endif #endif
static int corgibl_get_intensity(struct backlight_device *bd)
static int corgibl_set_power(struct backlight_device *bd, int state)
{
corgibl_blank(state);
return 0;
}
static int corgibl_get_power(struct backlight_device *bd)
{ {
return corgibl_powermode; return corgibl_intensity;
} }
static int corgibl_set_intensity(struct backlight_device *bd, int intensity) static int corgibl_set_intensity(struct backlight_device *bd)
{ {
if (intensity > corgibl_data.max_brightness) corgibl_send_intensity(corgi_backlight_device);
intensity = corgibl_data.max_brightness;
corgibl_send_intensity(intensity);
current_intensity=intensity;
return 0; return 0;
} }
static int corgibl_get_intensity(struct backlight_device *bd)
{
return current_intensity;
}
/* /*
* Called when the battery is low to limit the backlight intensity. * Called when the battery is low to limit the backlight intensity.
* If limit==0 clear any limit, otherwise limit the intensity * If limit==0 clear any limit, otherwise limit the intensity
*/ */
void corgibl_limit_intensity(int limit) void corgibl_limit_intensity(int limit)
{ {
corgibl_limit = (limit ? 1 : 0); if (limit)
corgibl_send_intensity(current_intensity); corgibl_flags |= CORGIBL_BATTLOW;
else
corgibl_flags &= ~CORGIBL_BATTLOW;
corgibl_send_intensity(corgi_backlight_device);
} }
EXPORT_SYMBOL(corgibl_limit_intensity); EXPORT_SYMBOL(corgibl_limit_intensity);
static struct backlight_properties corgibl_data = { static struct backlight_properties corgibl_data = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.get_power = corgibl_get_power,
.set_power = corgibl_set_power,
.get_brightness = corgibl_get_intensity, .get_brightness = corgibl_get_intensity,
.set_brightness = corgibl_set_intensity, .update_status = corgibl_set_intensity,
}; };
static struct backlight_device *corgi_backlight_device;
static int __init corgibl_probe(struct platform_device *pdev) static int __init corgibl_probe(struct platform_device *pdev)
{ {
struct corgibl_machinfo *machinfo = pdev->dev.platform_data; struct corgibl_machinfo *machinfo = pdev->dev.platform_data;
...@@ -156,8 +130,9 @@ static int __init corgibl_probe(struct platform_device *pdev) ...@@ -156,8 +130,9 @@ static int __init corgibl_probe(struct platform_device *pdev)
if (IS_ERR (corgi_backlight_device)) if (IS_ERR (corgi_backlight_device))
return PTR_ERR (corgi_backlight_device); return PTR_ERR (corgi_backlight_device);
corgibl_set_intensity(NULL, CORGI_DEFAULT_INTENSITY); corgibl_data.power = FB_BLANK_UNBLANK;
corgibl_limit_intensity(0); corgibl_data.brightness = CORGI_DEFAULT_INTENSITY;
corgibl_send_intensity(corgi_backlight_device);
printk("Corgi Backlight Driver Initialized.\n"); printk("Corgi Backlight Driver Initialized.\n");
return 0; return 0;
...@@ -167,8 +142,6 @@ static int corgibl_remove(struct platform_device *dev) ...@@ -167,8 +142,6 @@ static int corgibl_remove(struct platform_device *dev)
{ {
backlight_device_unregister(corgi_backlight_device); backlight_device_unregister(corgi_backlight_device);
corgibl_set_intensity(NULL, 0);
printk("Corgi Backlight Driver Unloaded\n"); printk("Corgi Backlight Driver Unloaded\n");
return 0; return 0;
} }
......
...@@ -19,20 +19,25 @@ struct fb_info; ...@@ -19,20 +19,25 @@ struct fb_info;
struct backlight_properties { struct backlight_properties {
/* Owner module */ /* Owner module */
struct module *owner; struct module *owner;
/* Get the backlight power status (0: full on, 1..3: power saving
modes; 4: full off), see FB_BLANK_XXX */ /* Notify the backlight driver some property has changed */
int (*get_power)(struct backlight_device *); int (*update_status)(struct backlight_device *);
/* Enable or disable power to the LCD (0: on; 4: off, see FB_BLANK_XXX) */ /* Return the current backlight brightness (accounting for power,
int (*set_power)(struct backlight_device *, int power); fb_blank etc.) */
/* Maximal value for brightness (read-only) */
int max_brightness;
/* Get current backlight brightness */
int (*get_brightness)(struct backlight_device *); int (*get_brightness)(struct backlight_device *);
/* Set backlight brightness (0..max_brightness) */
int (*set_brightness)(struct backlight_device *, int brightness);
/* Check if given framebuffer device is the one bound to this backlight; /* Check if given framebuffer device is the one bound to this backlight;
return 0 if not, !=0 if it is. If NULL, backlight always matches the fb. */ return 0 if not, !=0 if it is. If NULL, backlight always matches the fb. */
int (*check_fb)(struct fb_info *); int (*check_fb)(struct fb_info *);
/* Current User requested brightness (0 - max_brightness) */
int brightness;
/* Maximal value for brightness (read-only) */
int max_brightness;
/* Current FB Power mode (0: full on, 1..3: power saving
modes; 4: full off), see FB_BLANK_XXX */
int power;
/* FB Blanking active? (values as for power) */
int fb_blank;
}; };
struct backlight_device { struct backlight_device {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册