summaryrefslogtreecommitdiff
path: root/drivers/video/backlight/max8831_bl.c
blob: e93d7e9cdebef8058ba0f1615679e9a23c70e7a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Backlight LEDs driver for MAX8831
 *
 * Copyright (c) 2008-2012, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fb.h>
#include <linux/backlight.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/mfd/max8831.h>

struct max8831_backlight_data {
	struct device		*max8831_dev;
	int			id;
	int			current_brightness;
};

static int max8831_backlight_set(struct backlight_device *bl, int brightness)
{
	struct max8831_backlight_data *data = bl_get_data(bl);
	struct device *dev = data->max8831_dev;

	if (data->id == MAX8831_BL_LEDS) {

		if (brightness == 0) {
			max8831_update_bits(dev, MAX8831_CTRL,
			(MAX8831_CTRL_LED1_ENB | MAX8831_CTRL_LED2_ENB), 0);
		} else {
			max8831_update_bits(dev, MAX8831_CTRL,
			(MAX8831_CTRL_LED1_ENB | MAX8831_CTRL_LED2_ENB),
			(MAX8831_CTRL_LED1_ENB | MAX8831_CTRL_LED2_ENB));

			max8831_write(dev, MAX8831_CURRENT_CTRL_LED1,
								brightness);
			max8831_write(dev, MAX8831_CURRENT_CTRL_LED2,
								brightness);
		}
	}
	data->current_brightness = brightness;
	return 0;
}
static int max8831_backlight_update_status(struct backlight_device *bl)
{
	int brightness = bl->props.brightness;

	if (bl->props.power != FB_BLANK_UNBLANK)
		brightness = 0;

	if (bl->props.fb_blank != FB_BLANK_UNBLANK)
		brightness = 0;

	return max8831_backlight_set(bl, brightness);
}

static int max8831_backlight_get_brightness(struct backlight_device *bl)
{
	struct max8831_backlight_data *data = bl_get_data(bl);
	return data->current_brightness;
}

static const struct backlight_ops max8831_backlight_ops = {
	.update_status	= max8831_backlight_update_status,
	.get_brightness	= max8831_backlight_get_brightness,
};

static int __devinit max8831_bl_probe(struct platform_device *pdev)
{
	struct max8831_backlight_data *data;
	struct backlight_device *bl;
	struct backlight_properties props;

	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
	if (data == NULL)
		return -ENOMEM;

	data->max8831_dev = pdev->dev.parent;
	data->current_brightness = 0;
	data->id = pdev->id;

	props.type = BACKLIGHT_RAW;
	props.max_brightness = MAX8831_BL_LEDS_MAX_CURR;
	bl = backlight_device_register(pdev->name, data->max8831_dev, data,
				       &max8831_backlight_ops, &props);
	if (IS_ERR(bl)) {
		dev_err(&pdev->dev, "failed to register backlight\n");
		return PTR_ERR(bl);
	}

	bl->props.brightness = MAX8831_BL_LEDS_MAX_CURR;

	platform_set_drvdata(pdev, bl);
	backlight_update_status(bl);
	return 0;
}

static int __devexit max8831_bl_remove(struct platform_device *pdev)
{
	struct backlight_device *bl = platform_get_drvdata(pdev);
	backlight_device_unregister(bl);
	return 0;
}
#ifdef CONFIG_PM
static int max8831_bl_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct backlight_device *bl = platform_get_drvdata(pdev);
	return max8831_backlight_set(bl, 0);
}

static int max8831_bl_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct backlight_device *bl = platform_get_drvdata(pdev);
	backlight_update_status(bl);
	return 0;
}

static const struct dev_pm_ops max8831_bl_pm_ops = {
	.suspend	= max8831_bl_suspend,
	.resume		= max8831_bl_resume,
};
#endif

static struct platform_driver max8831_bl_driver = {
	.driver = {
		.name	= "max8831_display_bl",
		.owner	= THIS_MODULE,
#ifdef CONFIG_PM
		.pm	= &max8831_bl_pm_ops,
#endif
	},
	.probe	= max8831_bl_probe,
	.remove	= __devexit_p(max8831_bl_remove),
};
module_platform_driver(max8831_bl_driver);

MODULE_AUTHOR("Chaitanya Bandi <bandik@nvidia.com>");
MODULE_DESCRIPTION("MAX8831 Backlight display driver");
MODULE_LICENSE("GPL v2");