Richard Purdie | d00ba72 | 2009-01-08 20:52:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Generic Backlight Driver |
| 3 | * |
| 4 | * Copyright (c) 2004-2008 Richard Purdie |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | */ |
| 11 | |
Jingoo Han | 8c7610f | 2012-05-29 15:07:18 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Richard Purdie | d00ba72 | 2009-01-08 20:52:37 +0000 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/fb.h> |
| 20 | #include <linux/backlight.h> |
| 21 | |
| 22 | static int genericbl_intensity; |
| 23 | static struct backlight_device *generic_backlight_device; |
| 24 | static struct generic_bl_info *bl_machinfo; |
| 25 | |
| 26 | /* Flag to signal when the battery is low */ |
| 27 | #define GENERICBL_BATTLOW BL_CORE_DRIVER1 |
| 28 | |
| 29 | static int genericbl_send_intensity(struct backlight_device *bd) |
| 30 | { |
| 31 | int intensity = bd->props.brightness; |
| 32 | |
| 33 | if (bd->props.power != FB_BLANK_UNBLANK) |
| 34 | intensity = 0; |
| 35 | if (bd->props.state & BL_CORE_FBBLANK) |
| 36 | intensity = 0; |
| 37 | if (bd->props.state & BL_CORE_SUSPENDED) |
| 38 | intensity = 0; |
| 39 | if (bd->props.state & GENERICBL_BATTLOW) |
| 40 | intensity &= bl_machinfo->limit_mask; |
| 41 | |
| 42 | bl_machinfo->set_bl_intensity(intensity); |
| 43 | |
| 44 | genericbl_intensity = intensity; |
| 45 | |
| 46 | if (bl_machinfo->kick_battery) |
| 47 | bl_machinfo->kick_battery(); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int genericbl_get_intensity(struct backlight_device *bd) |
| 53 | { |
| 54 | return genericbl_intensity; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Called when the battery is low to limit the backlight intensity. |
| 59 | * If limit==0 clear any limit, otherwise limit the intensity |
| 60 | */ |
Axel Lin | 1bff3a2 | 2011-10-31 17:11:52 -0700 | [diff] [blame] | 61 | void genericbl_limit_intensity(int limit) |
Richard Purdie | d00ba72 | 2009-01-08 20:52:37 +0000 | [diff] [blame] | 62 | { |
| 63 | struct backlight_device *bd = generic_backlight_device; |
| 64 | |
| 65 | mutex_lock(&bd->ops_lock); |
| 66 | if (limit) |
| 67 | bd->props.state |= GENERICBL_BATTLOW; |
| 68 | else |
| 69 | bd->props.state &= ~GENERICBL_BATTLOW; |
| 70 | backlight_update_status(generic_backlight_device); |
| 71 | mutex_unlock(&bd->ops_lock); |
| 72 | } |
Axel Lin | 1bff3a2 | 2011-10-31 17:11:52 -0700 | [diff] [blame] | 73 | EXPORT_SYMBOL(genericbl_limit_intensity); |
Richard Purdie | d00ba72 | 2009-01-08 20:52:37 +0000 | [diff] [blame] | 74 | |
Emese Revfy | 9905a43 | 2009-12-14 00:58:57 +0100 | [diff] [blame] | 75 | static const struct backlight_ops genericbl_ops = { |
Richard Purdie | d00ba72 | 2009-01-08 20:52:37 +0000 | [diff] [blame] | 76 | .options = BL_CORE_SUSPENDRESUME, |
| 77 | .get_brightness = genericbl_get_intensity, |
| 78 | .update_status = genericbl_send_intensity, |
| 79 | }; |
| 80 | |
| 81 | static int genericbl_probe(struct platform_device *pdev) |
| 82 | { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 83 | struct backlight_properties props; |
Richard Purdie | d00ba72 | 2009-01-08 20:52:37 +0000 | [diff] [blame] | 84 | struct generic_bl_info *machinfo = pdev->dev.platform_data; |
| 85 | const char *name = "generic-bl"; |
| 86 | struct backlight_device *bd; |
| 87 | |
| 88 | bl_machinfo = machinfo; |
| 89 | if (!machinfo->limit_mask) |
| 90 | machinfo->limit_mask = -1; |
| 91 | |
| 92 | if (machinfo->name) |
| 93 | name = machinfo->name; |
| 94 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 95 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 96 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 97 | props.max_brightness = machinfo->max_intensity; |
| 98 | bd = backlight_device_register(name, &pdev->dev, NULL, &genericbl_ops, |
| 99 | &props); |
Richard Purdie | d00ba72 | 2009-01-08 20:52:37 +0000 | [diff] [blame] | 100 | if (IS_ERR (bd)) |
| 101 | return PTR_ERR (bd); |
| 102 | |
| 103 | platform_set_drvdata(pdev, bd); |
| 104 | |
Richard Purdie | d00ba72 | 2009-01-08 20:52:37 +0000 | [diff] [blame] | 105 | bd->props.power = FB_BLANK_UNBLANK; |
| 106 | bd->props.brightness = machinfo->default_intensity; |
| 107 | backlight_update_status(bd); |
| 108 | |
| 109 | generic_backlight_device = bd; |
| 110 | |
Jingoo Han | 8c7610f | 2012-05-29 15:07:18 -0700 | [diff] [blame] | 111 | pr_info("Generic Backlight Driver Initialized.\n"); |
Richard Purdie | d00ba72 | 2009-01-08 20:52:37 +0000 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int genericbl_remove(struct platform_device *pdev) |
| 116 | { |
| 117 | struct backlight_device *bd = platform_get_drvdata(pdev); |
| 118 | |
| 119 | bd->props.power = 0; |
| 120 | bd->props.brightness = 0; |
| 121 | backlight_update_status(bd); |
| 122 | |
| 123 | backlight_device_unregister(bd); |
| 124 | |
Jingoo Han | 8c7610f | 2012-05-29 15:07:18 -0700 | [diff] [blame] | 125 | pr_info("Generic Backlight Driver Unloaded\n"); |
Richard Purdie | d00ba72 | 2009-01-08 20:52:37 +0000 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static struct platform_driver genericbl_driver = { |
| 130 | .probe = genericbl_probe, |
| 131 | .remove = genericbl_remove, |
| 132 | .driver = { |
| 133 | .name = "generic-bl", |
| 134 | }, |
| 135 | }; |
| 136 | |
Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 137 | module_platform_driver(genericbl_driver); |
Richard Purdie | d00ba72 | 2009-01-08 20:52:37 +0000 | [diff] [blame] | 138 | |
| 139 | MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>"); |
| 140 | MODULE_DESCRIPTION("Generic Backlight Driver"); |
| 141 | MODULE_LICENSE("GPL"); |