aboutsummaryrefslogtreecommitdiff
path: root/drivers/video/backlight/gta01_bl.c
blob: bd7d41f55689291e2f6e0fa9db504a613cfc5bd2 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 *  Backlight Driver for FIC GTA01 (Neo1973) GSM Phone
 *
 * Copyright (C) 2006-2007 by Openmoko, Inc.
 * Author: Harald Welte <laforge@openmoko.org>
 * All rights reserved.
 *
 *  based on corgi_cl.c, Copyright (c) 2004-2006 Richard Purdie
 *
 * 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, version 2.
 *
 * 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., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 *
 * Javi Roman <javiroman@kernel-labs.org>:
 * 	implement PWM, instead of simple on/off switching
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/mutex.h>
#include <linux/fb.h>
#include <linux/backlight.h>
#include <linux/clk.h>

#include <asm/arch/hardware.h>
#include <asm/arch/gta01.h>
#include <asm/arch/pwm.h>

#include <asm/plat-s3c/regs-timer.h>

static struct backlight_properties gta01bl_prop;
static struct backlight_device *gta01_backlight_device;
static struct gta01bl_machinfo *bl_machinfo;

static unsigned long gta01bl_flags;

struct gta01bl_data {
	int intensity;
	struct mutex mutex;
	struct clk *clk;
	struct s3c2410_pwm pwm;
};

static struct gta01bl_data gta01bl;

#define GTA01BL_SUSPENDED     0x01
#define GTA01BL_BATTLOW       0x02

/* On the GTA01 / Neo1973, we use a 50 or 66MHz PCLK, which gives
 * us a 6.25..8.25MHz DIV8 clock, which is further divided by a
 * prescaler of 4, resulting in a 1.56..2.06MHz tick.  This results in a
 * minimum frequency of 24..31Hz.  At 400Hz, we need to set the count
 * to something like 3906..5156, providing us a way sufficient resolution
 * for display brightness adjustment. */
#define GTA01BL_COUNTER 5156

static int gta01bl_send_intensity(struct backlight_device *bd)
{
	int intensity = bd->props.brightness;

	if (bd->props.power != FB_BLANK_UNBLANK)
		intensity = 0;
	if (bd->props.fb_blank != FB_BLANK_UNBLANK)
		intensity = 0;
	if (gta01bl_flags & GTA01BL_SUSPENDED)
		intensity = 0;
	if (gta01bl_flags & GTA01BL_BATTLOW)
		intensity &= bl_machinfo->limit_mask;

	mutex_lock(&gta01bl.mutex);
#ifdef GTA01_BACKLIGHT_ONOFF_ONLY
	if (intensity)
		s3c2410_gpio_setpin(GTA01_GPIO_BACKLIGHT, 1);
	else
		s3c2410_gpio_setpin(GTA01_GPIO_BACKLIGHT, 0);
#else
	if (intensity == bd->props.max_brightness) {
		s3c2410_gpio_setpin(GTA01_GPIO_BACKLIGHT, 1);
		s3c2410_gpio_cfgpin(GTA01_GPIO_BACKLIGHT, S3C2410_GPIO_OUTPUT);
	} else  {
		s3c2410_pwm_duty_cycle(intensity & 0xffff, &gta01bl.pwm);
		s3c2410_gpio_cfgpin(GTA01_GPIO_BACKLIGHT, S3C2410_GPB0_TOUT0);
	}
#endif
	mutex_unlock(&gta01bl.mutex);

	gta01bl.intensity = intensity;
	return 0;
}

static int gta01bl_init_hw(void)
{
	int rc;

	rc = s3c2410_pwm_init(&gta01bl.pwm);
	if (rc)
		return rc;

	gta01bl.pwm.timerid = PWM0;
	gta01bl.pwm.prescaler = (4 - 1);
	gta01bl.pwm.divider = S3C2410_TCFG1_MUX0_DIV8;
	gta01bl.pwm.counter = GTA01BL_COUNTER;
	gta01bl.pwm.comparer = gta01bl.pwm.counter;

	rc = s3c2410_pwm_enable(&gta01bl.pwm);
	if (rc)
		return rc;

	s3c2410_pwm_start(&gta01bl.pwm);

	gta01bl_prop.max_brightness = gta01bl.pwm.counter;

	return 0;
}

#ifdef CONFIG_PM
static int gta01bl_suspend(struct platform_device *dev, pm_message_t state)
{
	gta01bl_flags |= GTA01BL_SUSPENDED;
	gta01bl_send_intensity(gta01_backlight_device);
	return 0;
}

static int gta01bl_resume(struct platform_device *dev)
{
	mutex_lock(&gta01bl.mutex);
	gta01bl_init_hw();
	mutex_unlock(&gta01bl.mutex);

	gta01bl_flags &= ~GTA01BL_SUSPENDED;
	gta01bl_send_intensity(gta01_backlight_device);
	return 0;
}
#else
#define gta01bl_suspend	NULL
#define gta01bl_resume	NULL
#endif

static int gta01bl_get_intensity(struct backlight_device *bd)
{
	return gta01bl.intensity;
}

static int gta01bl_set_intensity(struct backlight_device *bd)
{
	gta01bl_send_intensity(gta01_backlight_device);
	return 0;
}

/*
 * Called when the battery is low to limit the backlight intensity.
 * If limit==0 clear any limit, otherwise limit the intensity
 */
void gta01bl_limit_intensity(int limit)
{
	if (limit)
		gta01bl_flags |= GTA01BL_BATTLOW;
	else
		gta01bl_flags &= ~GTA01BL_BATTLOW;
	gta01bl_send_intensity(gta01_backlight_device);
}
EXPORT_SYMBOL_GPL(gta01bl_limit_intensity);


static struct backlight_ops gta01bl_ops = {
	.get_brightness = gta01bl_get_intensity,
	.update_status  = gta01bl_set_intensity,
};

static int __init gta01bl_probe(struct platform_device *pdev)
{
	struct gta01bl_machinfo *machinfo = pdev->dev.platform_data;
	int rc;

#ifdef GTA01_BACKLIGHT_ONOFF_ONLY
	s3c2410_gpio_cfgpin(GTA01_GPIO_BACKLIGHT, S3C2410_GPIO_OUTPUT);
	gta01bl_prop.max_brightness = 1;
#else
	rc = gta01bl_init_hw();
	if (rc < 0)
		return rc;
#endif
	mutex_init(&gta01bl.mutex);

	if (!machinfo->limit_mask)
		machinfo->limit_mask = -1;

	gta01_backlight_device = backlight_device_register("gta01-bl",
							   &pdev->dev, NULL,
							   &gta01bl_ops);
	if (IS_ERR(gta01_backlight_device))
		return PTR_ERR(gta01_backlight_device);

	gta01bl_prop.power = FB_BLANK_UNBLANK;
	gta01bl_prop.brightness = gta01bl_prop.max_brightness;
	memcpy(&gta01_backlight_device->props,
	       &gta01bl_prop, sizeof(gta01bl_prop));
	gta01bl_send_intensity(gta01_backlight_device);

	return 0;
}

static int gta01bl_remove(struct platform_device *dev)
{
#ifndef GTA01_BACKLIGHT_ONOFF_ONLY
	s3c2410_pwm_disable(&gta01bl.pwm);
#endif
	backlight_device_unregister(gta01_backlight_device);
	mutex_destroy(&gta01bl.mutex);

	s3c2410_gpio_cfgpin(GTA01_GPIO_BACKLIGHT, S3C2410_GPIO_OUTPUT);
	s3c2410_gpio_setpin(GTA01_GPIO_BACKLIGHT, 1);

	return 0;
}

static struct platform_driver gta01bl_driver = {
	.probe		= gta01bl_probe,
	.remove		= gta01bl_remove,
	.suspend	= gta01bl_suspend,
	.resume		= gta01bl_resume,
	.driver		= {
		.name	= "gta01-bl",
	},
};

static int __init gta01bl_init(void)
{
	return platform_driver_register(&gta01bl_driver);
}

static void __exit gta01bl_exit(void)
{
	platform_driver_unregister(&gta01bl_driver);
}

module_init(gta01bl_init);
module_exit(gta01bl_exit);

MODULE_DESCRIPTION("FIC GTA01 (Neo1973) Backlight Driver");
MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
MODULE_LICENSE("GPL");