Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6-block.git] / drivers / video / backlight / locomolcd.c
CommitLineData
987132bb
PM
1/*
2 * Backlight control code for Sharp Zaurus SL-5500
3 *
4 * Copyright 2005 John Lenz <lenz@cs.wisc.edu>
5 * Maintainer: Pavel Machek <pavel@suse.cz> (unless John wants to :-)
6 * GPL v2
7 *
8 * This driver assumes single CPU. That's okay, because collie is
9 * slightly old hardware, and noone is going to retrofit second CPU to
10 * old PDA.
11 */
12
13/* LCD power functions */
987132bb
PM
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/device.h>
18#include <linux/interrupt.h>
e4423781
RP
19#include <linux/fb.h>
20#include <linux/backlight.h>
987132bb
PM
21
22#include <asm/hardware/locomo.h>
23#include <asm/irq.h>
36a95bf5
PM
24#include <asm/mach/sharpsl_param.h>
25#include <asm/mach-types.h>
987132bb 26
36a95bf5 27#include "../../../arch/arm/mach-sa1100/generic.h"
987132bb 28
e4423781 29static struct backlight_device *locomolcd_bl_device;
987132bb 30static struct locomo_dev *locomolcd_dev;
e4423781
RP
31static unsigned long locomolcd_flags;
32#define LOCOMOLCD_SUSPENDED 0x01
987132bb
PM
33
34static void locomolcd_on(int comadj)
35{
8d48427e
RP
36 locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHA_ON, 0);
37 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHA_ON, 1);
987132bb
PM
38 mdelay(2);
39
8d48427e
RP
40 locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHD_ON, 0);
41 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHD_ON, 1);
987132bb
PM
42 mdelay(2);
43
44 locomo_m62332_senddata(locomolcd_dev, comadj, 0);
45 mdelay(5);
46
8d48427e
RP
47 locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VEE_ON, 0);
48 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VEE_ON, 1);
987132bb
PM
49 mdelay(10);
50
51 /* TFTCRST | CPSOUT=0 | CPSEN */
52 locomo_writel(0x01, locomolcd_dev->mapbase + LOCOMO_TC);
53
54 /* Set CPSD */
55 locomo_writel(6, locomolcd_dev->mapbase + LOCOMO_CPSD);
56
57 /* TFTCRST | CPSOUT=0 | CPSEN */
58 locomo_writel((0x04 | 0x01), locomolcd_dev->mapbase + LOCOMO_TC);
59 mdelay(10);
60
8d48427e
RP
61 locomo_gpio_set_dir(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_MOD, 0);
62 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_MOD, 1);
987132bb
PM
63}
64
65static void locomolcd_off(int comadj)
66{
67 /* TFTCRST=1 | CPSOUT=1 | CPSEN = 0 */
68 locomo_writel(0x06, locomolcd_dev->mapbase + LOCOMO_TC);
69 mdelay(1);
70
8d48427e 71 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHA_ON, 0);
987132bb
PM
72 mdelay(110);
73
8d48427e 74 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VEE_ON, 0);
987132bb
PM
75 mdelay(700);
76
77 /* TFTCRST=0 | CPSOUT=0 | CPSEN = 0 */
78 locomo_writel(0, locomolcd_dev->mapbase + LOCOMO_TC);
8d48427e
RP
79 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_MOD, 0);
80 locomo_gpio_write(locomolcd_dev->dev.parent, LOCOMO_GPIO_LCD_VSHD_ON, 0);
987132bb
PM
81}
82
83void locomolcd_power(int on)
84{
36a95bf5 85 int comadj = sharpsl_param.comadj;
987132bb
PM
86 unsigned long flags;
87
88 local_irq_save(flags);
89
90 if (!locomolcd_dev) {
91 local_irq_restore(flags);
92 return;
93 }
94
95 /* read comadj */
e4423781
RP
96 if (comadj == -1 && machine_is_collie())
97 comadj = 128;
98 if (comadj == -1 && machine_is_poodle())
99 comadj = 118;
987132bb
PM
100
101 if (on)
102 locomolcd_on(comadj);
103 else
104 locomolcd_off(comadj);
105
106 local_irq_restore(flags);
107}
108EXPORT_SYMBOL(locomolcd_power);
109
e4423781
RP
110
111static int current_intensity;
112
113static int locomolcd_set_intensity(struct backlight_device *bd)
114{
599a52d1 115 int intensity = bd->props.brightness;
e4423781 116
599a52d1 117 if (bd->props.power != FB_BLANK_UNBLANK)
e4423781 118 intensity = 0;
599a52d1 119 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
e4423781
RP
120 intensity = 0;
121 if (locomolcd_flags & LOCOMOLCD_SUSPENDED)
122 intensity = 0;
123
124 switch (intensity) {
125 /* AC and non-AC are handled differently, but produce same results in sharp code? */
126 case 0: locomo_frontlight_set(locomolcd_dev, 0, 0, 161); break;
127 case 1: locomo_frontlight_set(locomolcd_dev, 117, 0, 161); break;
128 case 2: locomo_frontlight_set(locomolcd_dev, 163, 0, 148); break;
129 case 3: locomo_frontlight_set(locomolcd_dev, 194, 0, 161); break;
130 case 4: locomo_frontlight_set(locomolcd_dev, 194, 1, 161); break;
131
132 default:
133 return -ENODEV;
134 }
135 current_intensity = intensity;
136 return 0;
137}
138
139static int locomolcd_get_intensity(struct backlight_device *bd)
140{
141 return current_intensity;
142}
143
9905a43b 144static const struct backlight_ops locomobl_data = {
e4423781
RP
145 .get_brightness = locomolcd_get_intensity,
146 .update_status = locomolcd_set_intensity,
e4423781
RP
147};
148
149#ifdef CONFIG_PM
150static int locomolcd_suspend(struct locomo_dev *dev, pm_message_t state)
151{
152 locomolcd_flags |= LOCOMOLCD_SUSPENDED;
153 locomolcd_set_intensity(locomolcd_bl_device);
154 return 0;
155}
156
157static int locomolcd_resume(struct locomo_dev *dev)
158{
159 locomolcd_flags &= ~LOCOMOLCD_SUSPENDED;
160 locomolcd_set_intensity(locomolcd_bl_device);
161 return 0;
162}
163#else
164#define locomolcd_suspend NULL
165#define locomolcd_resume NULL
166#endif
167
8d48427e 168static int locomolcd_probe(struct locomo_dev *ldev)
987132bb
PM
169{
170 unsigned long flags;
171
172 local_irq_save(flags);
8d48427e 173 locomolcd_dev = ldev;
987132bb 174
8d48427e 175 locomo_gpio_set_dir(ldev->dev.parent, LOCOMO_GPIO_FL_VR, 0);
e4423781 176
987132bb
PM
177 /* the poodle_lcd_power function is called for the first time
178 * from fs_initcall, which is before locomo is activated.
179 * We need to recall poodle_lcd_power here*/
e4423781
RP
180 if (machine_is_poodle())
181 locomolcd_power(1);
182
987132bb 183 local_irq_restore(flags);
e4423781 184
27c5d745 185 locomolcd_bl_device = backlight_device_register("locomo-bl", &ldev->dev, NULL, &locomobl_data);
e4423781
RP
186
187 if (IS_ERR (locomolcd_bl_device))
188 return PTR_ERR (locomolcd_bl_device);
189
190 /* Set up frontlight so that screen is readable */
599a52d1
RP
191 locomolcd_bl_device->props.max_brightness = 4,
192 locomolcd_bl_device->props.brightness = 2;
e4423781
RP
193 locomolcd_set_intensity(locomolcd_bl_device);
194
987132bb
PM
195 return 0;
196}
197
e4423781 198static int locomolcd_remove(struct locomo_dev *dev)
987132bb
PM
199{
200 unsigned long flags;
e4423781 201
2a391a18
RP
202 locomolcd_bl_device->props.brightness = 0;
203 locomolcd_bl_device->props.power = 0;
4437cd1e
HMH
204 locomolcd_set_intensity(locomolcd_bl_device);
205
e4423781 206 backlight_device_unregister(locomolcd_bl_device);
987132bb
PM
207 local_irq_save(flags);
208 locomolcd_dev = NULL;
209 local_irq_restore(flags);
210 return 0;
211}
212
213static struct locomo_driver poodle_lcd_driver = {
214 .drv = {
215 .name = "locomo-backlight",
216 },
217 .devid = LOCOMO_DEVID_BACKLIGHT,
e4423781
RP
218 .probe = locomolcd_probe,
219 .remove = locomolcd_remove,
220 .suspend = locomolcd_suspend,
221 .resume = locomolcd_resume,
987132bb
PM
222};
223
e4423781
RP
224
225static int __init locomolcd_init(void)
987132bb
PM
226{
227 int ret = locomo_driver_register(&poodle_lcd_driver);
e4423781
RP
228 if (ret)
229 return ret;
987132bb
PM
230
231#ifdef CONFIG_SA1100_COLLIE
232 sa1100fb_lcd_power = locomolcd_power;
233#endif
234 return 0;
235}
987132bb 236
e4423781
RP
237static void __exit locomolcd_exit(void)
238{
239 locomo_driver_unregister(&poodle_lcd_driver);
240}
241
242module_init(locomolcd_init);
243module_exit(locomolcd_exit);
244
245MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>, Pavel Machek <pavel@suse.cz>");
246MODULE_DESCRIPTION("Collie LCD driver");
247MODULE_LICENSE("GPL");