blob: 0ae155be9c89ad43685e2303bce941c6954273e1 [file] [log] [blame]
Richard Purdied00ba722009-01-08 20:52:37 +00001/*
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 Han8c7610f2012-05-29 15:07:18 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Richard Purdied00ba722009-01-08 20:52:37 +000014#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
22static int genericbl_intensity;
23static struct backlight_device *generic_backlight_device;
24static struct generic_bl_info *bl_machinfo;
25
26/* Flag to signal when the battery is low */
27#define GENERICBL_BATTLOW BL_CORE_DRIVER1
28
29static 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
52static 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 Lin1bff3a22011-10-31 17:11:52 -070061void genericbl_limit_intensity(int limit)
Richard Purdied00ba722009-01-08 20:52:37 +000062{
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 Lin1bff3a22011-10-31 17:11:52 -070073EXPORT_SYMBOL(genericbl_limit_intensity);
Richard Purdied00ba722009-01-08 20:52:37 +000074
Emese Revfy9905a432009-12-14 00:58:57 +010075static const struct backlight_ops genericbl_ops = {
Richard Purdied00ba722009-01-08 20:52:37 +000076 .options = BL_CORE_SUSPENDRESUME,
77 .get_brightness = genericbl_get_intensity,
78 .update_status = genericbl_send_intensity,
79};
80
81static int genericbl_probe(struct platform_device *pdev)
82{
Matthew Garretta19a6ee2010-02-17 16:39:44 -050083 struct backlight_properties props;
Richard Purdied00ba722009-01-08 20:52:37 +000084 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 Garretta19a6ee2010-02-17 16:39:44 -050095 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -070096 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -050097 props.max_brightness = machinfo->max_intensity;
98 bd = backlight_device_register(name, &pdev->dev, NULL, &genericbl_ops,
99 &props);
Jingoo Han933bd9b2012-12-17 16:00:17 -0800100 if (IS_ERR(bd))
101 return PTR_ERR(bd);
Richard Purdied00ba722009-01-08 20:52:37 +0000102
103 platform_set_drvdata(pdev, bd);
104
Richard Purdied00ba722009-01-08 20:52:37 +0000105 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 Han8c7610f2012-05-29 15:07:18 -0700111 pr_info("Generic Backlight Driver Initialized.\n");
Richard Purdied00ba722009-01-08 20:52:37 +0000112 return 0;
113}
114
115static 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 Han8c7610f2012-05-29 15:07:18 -0700125 pr_info("Generic Backlight Driver Unloaded\n");
Richard Purdied00ba722009-01-08 20:52:37 +0000126 return 0;
127}
128
129static struct platform_driver genericbl_driver = {
130 .probe = genericbl_probe,
131 .remove = genericbl_remove,
132 .driver = {
133 .name = "generic-bl",
134 },
135};
136
Axel Lin81178e02012-01-10 15:09:11 -0800137module_platform_driver(genericbl_driver);
Richard Purdied00ba722009-01-08 20:52:37 +0000138
139MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
140MODULE_DESCRIPTION("Generic Backlight Driver");
141MODULE_LICENSE("GPL");